Dynamic debug — The Linux Kernel documentation
Dynamic debug allows you to dynamically enable/disable kernel debug-print code to obtain additional kernel information.
If /proc/dynamic_debug/control exists, your kernel has dynamic debug. You’ll need root access (sudo su) to use this.
Dynamic debug provides:
a Catalog of all prdbgs in your kernel. cat /proc/dynamic_debug/control to see them.
a Simple query/command language to alter prdbgs by selecting on any combination of 0 or 1 of:
source filename
function name
line number (including ranges of line numbers)
module name
format string
class name (as known/declared by each module)
pr_debug()
dev_dbg()
print_hex_dump_debug()
print_hex_dump_bytes()
printk.h - include/linux/printk.h - Linux source code (v6.2.6) - Bootlin
/* If you are writing a driver, please use dev_dbg instead */
#if defined(CONFIG_DYNAMIC_DEBUG) || \(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
#include /*** pr_debug - Print a debug-level message conditionally* @fmt: format string* @...: arguments for the format string** This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is* set. Otherwise, if DEBUG is defined, it's equivalent to a printk with* KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.** It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses* pr_fmt() internally).*/
#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
dev_printk.h - include/linux/dev_printk.h - Linux source code (v6.2.6) - Bootlin
#if defined(CONFIG_DYNAMIC_DEBUG) || \(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
#define dev_dbg(dev, fmt, ...) \dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
#elif defined(DEBUG)
#define dev_dbg(dev, fmt, ...) \dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
#else
#define dev_dbg(dev, fmt, ...) \
({ \if (0) \dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
})
#endif
CONFIG_DYNAMIC_DEBUG CONFIG_DEBUG_FS | DEBUG | 输出情况 |
y | define |
|
y | no define |
|
n | define |
|
n | no define |
|
CONFIG_DYNAMIC_DEBUG, CONFIG_DEBUG_FS
在系统的编译配置文件 config中,如果为y,表示开启,为n,表示关闭。
DEBUG
方法1:在内核模块c源文件最开始处定义。
#define DEBUG//其他源码
xxxx
方法2:在内核模块的Makefile中定义。
ccflags-y += -DDEBUG
在系统启动参数中添加rule,rule为dynamic debug的控制规则。"QUERY"代表控制规则。
dyndbg="QUERY"
module.dyndbg="QUERY"
当一个内核模块加载时, 会按照以下顺序检查以下三个设定。
When modprobe foo is called, modprobe scans /proc/cmdline for foo.params, strips foo., and passes them to the kernel along with params given in modprobe args or /etc/modprob.d/*.conf files, in the following order:
parameters given via /etc/modprobe.d/*.conf:
options foo dyndbg=+pt
options foo dyndbg # defaults to +p
foo.dyndbg as given in boot args, foo. is stripped and passed:
foo.dyndbg=" func bar +p; func buz +mp"
args to modprobe:
modprobe foo dyndbg==pmf # override previous settings
echo "module module_name +p" > /proc/dynamic_debug/control// enable the message at line 1603 of file svcsock.c
:#> ddcmd 'file svcsock.c line 1603 +p'// enable all the messages in file svcsock.c
:#> ddcmd 'file svcsock.c +p'// enable all the messages in the NFS server module
:#> ddcmd 'module nfsd +p'// enable all 12 messages in the function svc_process()
:#> ddcmd 'func svc_process +p'// disable all 12 messages in the function svc_process()
:#> ddcmd 'func svc_process -p'// enable messages for NFS calls READ, READLINK, READDIR and READDIR+.
:#> ddcmd 'format "nfsd: READ" +p'// enable messages in files of which the paths include string "usb"
:#> ddcmd 'file *usb* +p' > /proc/dynamic_debug/control// enable all messages
:#> ddcmd '+p' > /proc/dynamic_debug/control// add module, function to all enabled messages
:#> ddcmd '+mf' > /proc/dynamic_debug/control// boot-args example, with newlines and comments for readability
Kernel command line: ...// see what's going on in dyndbg=value processingdynamic_debug.verbose=3// enable pr_debugs in the btrfs module (can be builtin or loadable)btrfs.dyndbg="+p"// enable pr_debugs in all files under init/// and the function parse_one, #cmt is strippeddyndbg="file init/* +p #cmt ; func parse_one +p"// enable pr_debugs in 2 functions in a module loaded laterpc87360.dyndbg="func pc87360_init_device +p; func pc87360_find +p"
Makefile
#ccflags-y += -DDEBUGobj-m += test.oall:make -C /lib/modules/`uname -r`/build M=$(PWD) modules
clean:make -C /lib/modules/`uname -r`/build M=$(PWD) clean
source code
//#define DEBUG#include
#include
#include
#include static int __init init_my_module(void) {pr_debug("i'm %s\n", "pr_debug");pr_info("i'm %s\n", "pr_info");pr_notice("i'm %s\n", "pr_notice");pr_warn("i'm %s\n", "pr_warn");pr_err("i'm %s\n", "pr_err");pr_crit("i'm %s\n", "pr_crit");pr_alert("i'm %s\n", "pr_alert");pr_emerg("i'm %s\n", "pr_emerg");dev_dbg(NULL, "i'm %s\n", "dev_dbg");dev_info(NULL, "i'm %s\n", "dev_info");dev_notice(NULL, "i'm %s\n", "dev_notice");dev_warn(NULL, "i'm %s\n", "dev_warn");dev_err(NULL, "i'm %s\n", "dev_err");dev_crit(NULL, "i'm %s\n", "dev_crit");dev_alert(NULL, "i'm %s\n", "dev_alert");dev_emerg(NULL, "i'm %s\n", "dev_emerg");return 0;
}static void __exit exit_my_module(void) {printk("Bye, my module!\n");
}module_init(init_my_module);
module_exit(exit_my_module);MODULE_LICENSE("GPL");
MODULE_AUTHOR("vec");
上一篇:Android中级——动画
下一篇:第一个Node程序,Node入门