参考 console_loglevel 内核模块中的错误

reference console_loglevel ERROR in kernel module

我有一个 Linux 模块,它有调试功能,我只想在调试模式下调用该功能。 现在我有这样的代码:

if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
    dump_my_message(dev, my_msg);

但是在 linux-next 中构建此代码时,会抛出以下错误:

CHK     include/generated/uapi/linux/version.h
Kernel: arch/x86/boot/bzImage is ready  (#2)
  Building modules, stage 2.
  MODPOST 2738 modules
ERROR: "console_printk" [drivers/mymodule.ko] undefined!
scripts/Makefile.modpost:91: recipe for target '__modpost' failed
make[1]: *** [__modpost] Error 1
Makefile:1117: recipe for target 'modules' failed
make: *** [modules] Error 2

你能帮忙看看如何实现这个功能吗?谢谢!!!

编译错误是由于console_printk符号没有导出,所以不能被模块使用

然而,您真正应该使用的是 Dynamic debug 功能及其 pr_debug()/dev_dbg() 功能。

基本上你需要的是确保在你的内核中启用了CONFIG_DYNAMIC_DEBUG,在你想要编写一些调试代码的所有地方使用dev_dbg()然后动态启用你的调试消息,对于示例:

  1. 要启用模块中的所有消息,请在 insmod/modprobe 调用末尾添加 dyndbg=+p

  2. 要有选择地仅启用某些消息,请使用文档中描述的查询语言。例如,要仅启用来自模块中函数 foo()bar() 的消息,请使用:

    insmod mymodule.ko dyndbg="func foo +p; func bar +p"