(这节的前提是已经使能了printk)
pr_debug和pr_dbg和其他的pr_xx的使用方法是不一样的。 其他诸如pr_info等更加高级别的pr_xx api只需要使能动态打印,同时使控制台的打印级别小于需要执行的pr_xx api即可 但是pr_debug不可以这样子,使能它需要更多的步骤 这是因为pr_debug的定义和其他pr_xx是不一样的
#if defined(CONFIG_DYNAMIC_DEBUG) /* 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的定义受3个宏来控制: 如果定义了CONFIG_DYNAMIC_DEBUG 则pr_debug为: dynamic_pr_debug(fmt, ##VA_ARGS) 如果定义了DEBUG 则pr_debug为: printk(KERN_DEBUG pr_fmt(fmt), ##VA_ARGS)这就和我们常见的pr_info类api一样了,此时只要设置控制台打印级别小于7即为8就可以输出pr_debug了 else则为no_printk(KERN_DEBUG pr_fmt(fmt), ##VA_ARGS)不做输出。
所以就有2种方法去输出pr_debug 1.使能DEBUG: 更快捷的是在你的文件.h或者.c加上 #defien DEBUG 但此时只有你加上这句话的文件才使能了DEBUG 如果想要全局的使能,可以将DEBUG通过KCFLAGS直接传递,这样可以全局生效: KCFLAGS=-DDEBUG 同时修改控制台打印级别为8: echo 8 > /proc/sys/kernel/printk
2.使能CONFIG_DYNAMIC_DEBUG 此时pr_info为: dynamic_pr_debug(fmt, ##VA_ARGS) 配置内核为: Kernel hacking —> printk and dmesg options —> [*] Enable dynamic printk() support 此时使能了CONFIG_DYNAMIC_DEBUG,但还是不可以直接用,因为dynamic_pr_debug跟debugfs挂钩了。 接下来需要: 1.echo 8 > /proc/sys/kernel/printk 2.mount -t debugfs none /sys/kernel/debug 然后就会有这个节点:/sys/kernel/debug/dynamic_debug/control cat /sys/kernel/debug/dynamic_debug/control会打印出所有加了pr_debug的文件 也可以只看某个文件: cat /sys/kernel/debug/dynamic_debug/control | grep ali_audio_rpc.c 接下来,最后一步打开pr_debug
Enable all messages echo -n '+p' > /sys/kernel/debug/dynamic_debug/control Enable all the messages in file ali_audio_rpc.c echo -n 'file ali_audio_rpc.c +p' > /sys/kernel/debug/dynamic_debug/control Enable all messages in the function ali_audio_dev_init() echo -n 'func ali_audio_dev_ini +p' > /sys/kernel/debug/dynamic_debug/control Enable the message at line 1561 of file ali_audio_rpc.c echo -n 'file ali_audio_rpc.c line 1561 +p' > /sys/kernel/debug/dynamic_debug/control如果要取消这种属性只需要: echo -n 'file ali_audio_rpc.c -p' > /sys/kernel/debug/dynamic_debug/control//之前的+变为-
+ -以及p的含义看下面,这是摘抄内核\Documentation\dynamic-debug-howto.txt中的内容 - remove the given flags + add the given flags = set the flags to the given flagsThe flags are:
p enables the pr_debug() callsite. f Include the function name in the printed message l Include line number in the printed message m Include module name in the printed message t Include thread ID in messages not generated from interrupt context _ No flags are set. (Or'd with others on input)