linux - dynamic debug logging机制
创始人
2024-06-03 17:24:43
0

引用

  • Dynamic debug — The Linux Kernel documentation


一. 什么叫dynamic debug?

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)

二. 内核API

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

三.相关配置

3.1 内核配置和DEBUG宏是如何影响dynamic debug API的输出的?

CONFIG_DYNAMIC_DEBUG

CONFIG_DEBUG_FS

DEBUG

输出情况

y

define

  1. 默认输出dynamic debug log。

  1. 可用dynamic debug命令控制。

y

no define

  1. 默认不输出dynamic debug log。

  1. 可用dynamic debug命令控制。

n

define

  1. 默认输出dynamic debug log。

  1. 不可用dynamic debug命令控制。

n

no define

  1. 默认不输出dynamic debug log。

  1. 不可用dynamic debug命令控制。

3.2 如何检查配置?

  • CONFIG_DYNAMIC_DEBUG, CONFIG_DEBUG_FS

在系统的编译配置文件 config中,如果为y,表示开启,为n,表示关闭。

  • DEBUG

  1. 方法1:在内核模块c源文件最开始处定义。

#define DEBUG//其他源码
xxxx
  1. 方法2:在内核模块的Makefile中定义。

ccflags-y += -DDEBUG

四. 如何控制dynamic debug logging?

4.1 在系统启动时

在系统启动参数中添加rule,rule为dynamic debug的控制规则。"QUERY"代表控制规则。

dyndbg="QUERY"
module.dyndbg="QUERY"

4.2 在内核模块加载时(foo为示例内核模块名)

当一个内核模块加载时, 会按照以下顺序检查以下三个设定。

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:

  1. parameters given via /etc/modprobe.d/*.conf:

options foo dyndbg=+pt
options foo dyndbg # defaults to +p
  1. foo.dyndbg as given in boot args, foo. is stripped and passed:

foo.dyndbg=" func bar +p; func buz +mp"
  1. args to modprobe:

modprobe foo dyndbg==pmf # override previous settings

4.2 在内核模块runing时

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");

相关内容

热门资讯

苹果怎么倒进安卓系统,一键倒装... 你有没有想过,把苹果手机里的宝贝倒腾到安卓系统里去?听起来是不是有点像变魔术?别急,今天就来手把手教...
安卓系统都能双系统么吗,揭秘双... 你有没有想过,你的安卓手机是不是也能来个“双胞胎”呢?没错,就是那种一个手机里同时运行两个操作系统,...
长安汽车升级安卓系统,安卓系统... 你知道吗?最近长安汽车可是来了一次大变身呢!没错,就是那个我们熟悉的国产汽车品牌,这次他们竟然升级了...
mac电脑装安卓系统,轻松实现... 亲爱的电脑迷们,你是否曾幻想过在你的Mac电脑上运行安卓系统?想象那些你钟爱的安卓应用,在你的Mac...
安卓p系统流畅吗,畅享无忧 你有没有发现,最近安卓P系统成了大家热议的话题呢?不少朋友都在问,这个新系统到底流畅不流畅啊?今天,...
剑灵2安卓系统,畅游东方奇幻世... 你知道吗?最近在安卓系统上,有一款游戏可是火得一塌糊涂,那就是《剑灵2》!这款游戏不仅画面精美,操作...
安卓系统是否指定品牌,品牌定制... 你有没有想过,为什么你的安卓手机总是那么独特,而别人的安卓手机却看起来差不多呢?这背后,其实隐藏着一...
安卓系统和iso系统 照片共享... 你有没有发现,现在手机拍照功能越来越强大,拍出来的照片美得不要不要的!但是,当你想和朋友们分享这些精...
安卓系统领夹麦,便携式音频解决... 你有没有发现,现在手机通话越来越方便了,但是有时候,手机那点小小的麦克风,真的有点力不从心呢!尤其是...
安卓系统经常无法唤醒,探究系统... 你是不是也遇到过这种情况?手机屏幕黑了,手指在屏幕上疯狂地滑动,却怎么也唤醒不了安卓系统。这可真是让...
自己编译安卓系统源码,编译安卓... 你有没有想过,安卓系统其实就像一个巨大的宝藏,里面藏着无数的秘密和可能性?今天,就让我带你一起探索这...
鸿蒙系统属于安卓,基于安卓的全... 你知道吗?最近有个话题在科技圈里可是闹得沸沸扬扬的,那就是鸿蒙系统。没错,就是那个华为自主研发的操作...
lephone是安卓系统吗,深... 你有没有听说过lephone这个品牌呢?最近,身边的朋友都在讨论这个手机,说它性价比超高,但是,有人...
pc双系统安卓系统下载软件,P... 你有没有想过,在电脑上同时运行安卓系统,是不是就像在手机上玩电脑游戏一样酷炫呢?没错,这就是今天我要...
电量壁纸安卓系统设置,安卓系统... 手机电量告急,是不是又得赶紧找充电宝了?别急,今天就来和你聊聊如何通过设置电量壁纸来给安卓系统来个“...
ip是安卓系统吗,通过IP地址... 你有没有想过,那个陪伴你每天刷剧、玩游戏、办公的IP,它是不是安卓系统呢?别急,今天就来揭开这个谜底...
安卓系统谁负责升级,揭秘幕后负... 你有没有想过,你的安卓手机为什么有时候会突然收到系统更新的通知呢?是不是好奇,是谁在背后默默地为你的...
安卓系统需要降级吗,安卓系统升... 你有没有发现,你的安卓手机最近有点儿“老态龙钟”了呢?运行速度慢吞吞的,有时候还卡个不停。这时候,你...
性价比手机安卓系统,盘点安卓系... 你有没有想过,在这个手机更新换代如此迅速的时代,如何用最少的钱,买到最满意的手机呢?没错,我要说的是...
虚拟大师安卓2.0系统,安卓新... 你有没有听说最近虚拟大师安卓2.0系统火得一塌糊涂?这可不是空穴来风,而是真的让不少手机用户都跃跃欲...