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

相关内容

热门资讯

鸿蒙套娃安卓系统视频,融合与创... 你知道吗?最近科技圈可是炸开了锅,因为华为的新操作系统鸿蒙OS又有了新动作。这不,他们竟然把鸿蒙套娃...
xp系统连接安卓手机问题,实用... 你有没有遇到过这样的情况:你的电脑上还运行着那个经典的XP系统,而你的安卓手机却时不时地想要和你亲密...
压缩安卓系统储存空间,高效管理... 手机里的照片越来越多,游戏也越玩越上瘾,可这安卓系统的储存空间却越来越紧张,是不是感觉像是在挤牙膏?...
安卓手游转苹果系统教程,轻松实... 你是不是也和我一样,手头有一堆安卓手游,突然之间想换换口味,体验一下苹果系统的魅力呢?别急,今天就来...
安卓原生系统锁屏暗,安卓系统锁... 亲爱的手机控们,你是否曾为安卓手机锁屏时的暗模式而感到好奇?那种在夜晚或光线不足的环境中,屏幕自动调...
安卓系统表情包下载地址,安卓系... 你是不是也和我一样,对安卓系统的表情包爱不释手?那些搞笑的、可爱的、甚至是有点小调皮的表情,总能让我...
原生安卓系统声音bug,揭秘那... 你有没有遇到过这种情况?手机里突然传来一阵奇怪的声音,让你瞬间从美梦中惊醒,或者正在专心工作时被打扰...
水果收银机安卓系统,便捷高效的... 你有没有想过,在繁忙的超市里,那些摆满新鲜水果的摊位,背后竟然隐藏着一个小小的科技秘密?没错,就是那...
安卓系统变苹果界面了吗,苹果界... 最近手机界可是炸开了锅,不少安卓用户都在议论纷纷:“安卓系统变苹果界面了吗?”这事儿可真不简单,得好...
miui操作系统与安卓系统吗,... 亲爱的读者,你是否曾在手机上看到过MIUI操作系统和安卓系统这两个名字,好奇它们之间有什么区别?今天...
安卓系统怎么卡道具界面,探究原... 手机用久了,是不是感觉安卓系统越来越卡?尤其是那个道具界面,点开就慢吞吞的,真是让人头疼。别急,今天...
安卓系统红包加速器,畅享无阻新... 你有没有发现,现在用手机抢红包简直是一场速度与激情的较量?别急,别急,让我来给你揭秘一款神器——安卓...
安卓经典版系统更新时间,从首次... 你有没有发现,最近你的安卓手机又悄悄地变了个样?没错,就是那个陪伴我们多年的经典版系统,它又来更新啦...
安卓系统开发要多久,约需1-2... 你有没有想过,自己动手开发一个安卓应用,究竟需要多长时间呢?这可是个让人好奇的问题,毕竟安卓系统开发...
原生安卓系统手机壁纸图片,探索... 亲爱的手机控们,你是否曾为寻找一款独特的壁纸而烦恼?今天,就让我带你走进原生安卓系统手机壁纸的奇幻世...
bmw安卓互联系统,智能驾驶新... 你有没有发现,现在开车已经不仅仅是驾驶那么简单了?一辆好车,还得有个好“大脑”,这样才能让你的驾驶体...
安卓手机升级系统卡吗,安卓手机... 你有没有遇到过这种情况:安卓手机升级系统后,突然感觉手机像蜗牛一样慢吞吞的,心里那个急啊!今天,就让...
无线麦克风安卓系统,轻松实现无... 你有没有想过,在一场热闹的K歌派对或者重要的演讲场合,无线麦克风简直就是救星啊!想象你手握麦克风,自...
怎么重新定制安卓系统,打造专属... 你有没有想过,你的安卓手机其实可以变得独一无二,就像是你自己的小宇宙一样?没错,就是重新定制安卓系统...
安卓系统卡西欧照片软件,安卓系... 你有没有发现,手机用久了,尤其是安卓系统,有时候就像老牛拉破车,慢吞吞的,让人抓狂?这不,最近我就遇...