何时使用
#if defined(CONFIG_DYNAMIC_DEBUG)
#include <linux/dynamic_debug.h>
/* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
#define pr_debug(fmt, ...) \
dynamic_pr_debug(fmt, ##__VA_ARGS__)
#elif defined(DEBUG)
#define pr_debug(fmt, ...) \
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_debug(fmt, ...) \
no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif
/*
可以看到pr_debug 会用到,其他用到的暂不举例,一般是是有宏CONFIG_DYNAMIC_DEBUG 条件判断的
官方介绍
| Symbol: DYNAMIC_DEBUG [=y] |
| Type : boolean |
| Prompt: Enable dynamic printk() support |
| Location: |
| -> Kernel hacking |
| -> printk and dmesg options |
| Defined at lib/Kconfig.debug:47 |
| Depends on: PRINTK [=y] && DEBUG_FS [=y]
操作方法
通过看Kconfig文件或者执行make menuconfig 查看如何打开kernel动态调试宏
可以把编译选项编译进内核或者手动修改文件(debugfs 文件控制)
CONFIG_PRINTK=y
CONFIG_DEBUG_FS=y
CONFIG_DYNAMIC_DEBUG=y
现在内核已经支持,只需后面几步操作即可看到动态打印效果
实验
正常在下面目录有很多打印
/sys/kernel/debug/dynamic_debug/control
可以用cat 命令打开,最好结合grep 命令使用
查看举例
"internal phy\012"
drivers/net/ethernet/stmicro/stmmac/dwmac-meson.c:163 [dwmac_meson]network_interface_setup =_ "wol not set\012"
drivers/net/ethernet/stmicro/stmmac/dwmac-meson.c:166 [dwmac_meson]network_interface_setup =_ "Ethernet :got wol %d .set it\012"
drivers/net/ethernet/stmicro/stmmac/dwmac-meson.c:218 [dwmac_meson]network_interface_setup =_ "Ethernet: gpio reset ok\012"
drivers/net/ethernet/stmicro/stmmac/dwmac-meson.c:227 [dwmac_meson]network_interface_setup =_
注意动态打印信息级别为7,在小于8(控制台打印机别)可以打印出来
1、所以加一条命令
echo 8 > /proc/sys/kernel/printk
2、打开输出
# echo -n 'file stmmac_main.c +p' > /sys/kernel/debug/dynamic_debug/control
对比
snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
priv->plat->bus_id);
snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
priv->plat->phy_addr);
pr_debug("stmmac_init_phy: trying to attach to %s\n",
phy_id_fmt);
phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
interface);
}
3、结果
[ 1923.119522@3] stmmac_init_phy: trying to attach to stmmac-0:00 // pr
[ 1923.122955@3]
[ 1923.129107@3] stmmac_init_phy: eth0: attached to PHY (UID 0x1cc816) Link = 1
[ 1923.136176@3] stmmac_init_phy: eth0: attached to PHY (UID 0x1cc816) Link = 1
[ 1923.143889@0] (init_dma_desc_rings) dma_rx_phy=0x1f850000 dma_tx_phy=0x1f852000