为 pr_debug 和 printk 设置 CFLAGS

发布于 2024-10-19 11:32:30 字数 515 浏览 1 评论 0原文

我正在尝试了解 Linux 内核模块,并希望查看 pr_debug 和 printk 的输出。我正在使用 GNU Make。
我知道要获取 pr_debug 消息,我们必须使用 DDEBUG

那么,如何启用 printk 语句?

假设文件名是kvm.c。这两者有什么区别:

      CFLAGS_kvm.o := -DDEBUG
      CFLAGS_kvm.o += -DDEBUG

这个语句有什么作用:

      CFLAGS_kvm.o := -I.

[编辑]:
看起来我对方括号的使用引起了一些混乱。实际上,我所说的[文件名]是指一些文件,比如 kvm.c。

I am trying to understand a Linux kernel module and would like to see the output of pr_debug and printk. I am using GNU Make.
I understand that to get pr_debug messages, we have to use DDEBUG.

So, how do I enable printk statements ?

Lets say filename is kvm.c. What is the difference between these two:

      CFLAGS_kvm.o := -DDEBUG
      CFLAGS_kvm.o += -DDEBUG

What does this statement do:

      CFLAGS_kvm.o := -I.

[Edit]:
It looks like my usage of square brackets has caused some confusion. Actually by [filename], I meant some file, say kvm.c.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清音悠歌 2024-10-26 11:32:30

来自https://www.kernel.org/doc/local/pr_debug.txt

pr_debug()

Some files call pr_debug(), which is ordinarily an empty macro that discards
its arguments at compile time.  To enable debugging output, build the
appropriate file with -DDEBUG by adding

  CFLAGS_[filename].o := -DDEBUG

to the makefile.

For example, to see all attempts to spawn a usermode helper (such as
/sbin/hotplug), add to lib/Makefile the line:

    CFLAGS_kobject_uevent.o := -DDEBUG

Then boot the new kernel, do something that spawns a usermode helper, and
use the "dmesg" command to view the pr_debug() output.

From https://www.kernel.org/doc/local/pr_debug.txt:

pr_debug()

Some files call pr_debug(), which is ordinarily an empty macro that discards
its arguments at compile time.  To enable debugging output, build the
appropriate file with -DDEBUG by adding

  CFLAGS_[filename].o := -DDEBUG

to the makefile.

For example, to see all attempts to spawn a usermode helper (such as
/sbin/hotplug), add to lib/Makefile the line:

    CFLAGS_kobject_uevent.o := -DDEBUG

Then boot the new kernel, do something that spawns a usermode helper, and
use the "dmesg" command to view the pr_debug() output.
街角迷惘 2024-10-26 11:32:30

我不知道如何激活 printk() - 您通过 Google 搜索了什么?除此之外,我发现这个 这似乎意味着 printk() 几乎总是可用的(但是您必须使用适当的级别标记消息,并且可能可以控制控制台上显示的级别)。

宏名称中的方括号是非正统的 - 因此可能是特定于您的系统的扩展。

从字里行间看,您可能正在谈论 Linux 内核,因此也可能谈论 GNU Make,但是您如果你说出这样的事情,会对每个人都有帮助。

:= 表示法是对变量的立即赋值。 RHS 在读取和处理行时进行评估,而不是像通常情况那样在使用宏时进行评估。这意味着如果 RHS 上引用了宏,则随后对这些宏的更改不会影响该宏的值。考虑:

CFLAGS  = ${OFLAGS} ${IFLAGS} ${DFLAGS} ${WFLAGS}
CFLAGS := ${OFLAGS} ${IFLAGS} ${DFLAGS} ${WFLAGS}

第一个变体指出 CFLAGS 将由 4 个命名宏组成(实际上,它只是复制该行以供以后扩展),但在(大概)在 C 编译命令中使用它之前不会扩展这些值。

第二种变体在读取该行时立即查找 4 个宏的值并将其展开。 4 个引用宏的后续更改不会反映在 CFLAGS 中。

+= 符号将 RHS 添加到宏中,而不是简单地替换它。

I don't know about how to activate printk() - what did you search for with Google? Amongst other things, I found this which seems to imply the printk() is almost always available (but you have to mark the messages with an appropriate level, and there is probably a control over which levels are displayed on the console).

The square brackets in a macro name are unorthodox - and therefore probably an extension specific to your system.

Reading between the lines, it is likely that you are talking about the Linux kernel and therefore GNU Make, but you'd help everyone if you stated such things.

The := notation is an immediate assignment to the variable. The RHS is evaluated when the line is read and processed, not when the macro is used as is normally the case. It means that if there are macros referenced on the RHS, subsequent changes to those macros will not affect the value of this macro. Consider:

CFLAGS  = ${OFLAGS} ${IFLAGS} ${DFLAGS} ${WFLAGS}
CFLAGS := ${OFLAGS} ${IFLAGS} ${DFLAGS} ${WFLAGS}

The first variation notes that CFLAGS will be formed from the 4 named macros (well, actually, it simply copies the line ready for later expansion), but does not expand the values until it is used in (presumably) a C compilation command.

The second variation immediately looks for the values of the 4 macros at the time when the line is read and expands them out. Subsequent changes in the 4 referenced macros are not reflected in CFLAGS.

The += notation adds the RHS to the macro, rather than simply replacing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文