dynamic debug

Dynamic debug

Dynamic debug(dyndbg) 功能,就是允许用户空间通过 debugfs 导出的文件节点 /sys/kernel/debug/dynamic_debug/control,动态的、在运行时控制 Linux 内核 KERN_DEBUG 类型日志的开启和关闭。

开启配置

1
2
CONFIG_DYNAMIC_DEBUG=y
CONFIG_DEBUG_FS=y

当打开了内核配置选项CONFIG_DYNAMIC_DEBUG, 内核中的pr_debug()/dev_dbg()print_hex_dump_debug()/print_hex_dump_bytes()就使用了Dynamic debug特性。

当未打开了内核配置选项CONFIG_DYNAMIC_DEBUG:

  • printk就退化成printk(KERN_DEBUG)
  • print_hex_dump_debug() 就退化成print_hex_dump(KERN_DEBUG)
  • dev_dbg就退化为空。

Dynamic debugdebugfs中有个控制文件节点:/sys/kernel/debug/dynamic_debug/control。它记录了所有使用Dynamic debug技术的文件路径、行号、模块名称、函数名称和要打印的语句。

查看dynamic debug的行为

我们可以通过向读文件/sys/kernel/debug/dynamic_debug/control来查看目前系统上Dynamic debug的配置

1
2
3
4
5
6
7
8
9
10
11
uos@uos-PC [~] ➜  sudo cat /sys/kernel/debug/dynamic_debug/control | head                                                                                              [14:27:15]
# filename:lineno [module]function flags format
drivers/usb/storage/usb.c:1131 [usb_storage]storage_probe =_ "Use Bulk-Only transport with the Transparent SCSI protocol for dynamic id: 0x%04x 0x%04x\012"
drivers/usb/storage/usb.c:1067 [usb_storage]usb_stor_probe2 =_ "waiting for device to settle before scanning\012"
drivers/usb/storage/usb.c:917 [usb_storage]usb_stor_scan_dwork =_ "scan complete\012"
drivers/usb/storage/usb.c:899 [usb_storage]usb_stor_scan_dwork =_ "starting scan\012"
drivers/usb/storage/sierra_ms.c:110 [usb_storage]truinst_show =_ "SWIMS: failed SWoC query\012"
drivers/usb/storage/sierra_ms.c:89 [usb_storage]debug_swoc =_ "SWIMS: Linux Version: %04X\012"
drivers/usb/storage/sierra_ms.c:88 [usb_storage]debug_swoc =_ "SWIMS: Linux SKU: %04X\012"
drivers/usb/storage/sierra_ms.c:87 [usb_storage]debug_swoc =_ "SWIMS: SWoC Rev: %02d\012"
drivers/usb/storage/sierra_ms.c:69 [usb_storage]sierra_get_swoc_info =_ "SWIMS: Attempting to get TRU-Install info\012"

使用方法

控制打印时,还可以使用flags更来定义更详细的输出信息,目前支持的flags如下:

1
2
3
4
5
6
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#使能文件svcsock.c中1603行的输出
echo -n "file svcsock.c line 1603 +p" > /sys/kernel/debug/dynamic_debug/control

# 打开函数svc_process中所有的动态输出
echo -n 'func svc_process +p' > /sys/kernel/debug/dynamic_debug/control

#开启文件中的pr_debug或dev_dbg
echo -n 'file $file_name +p' > /sys/kernel/debug/dynaminc_debug/control

#关闭文件中的打印
echo -n 'file $file_name -p' > /sys/kernel/debug/dynaminc_debug/control

#打开模块中的打印
echo -n 'module $mod_name +p' > /sys/kernel/debug/dynaminc_debug/control

参考链接:


dynamic debug
https://tomwithkernel.github.io/debug/dynamic/
作者
Tom
发布于
2024年11月7日
更新于
2024年11月7日
许可协议