Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux-kernel debug printouts?

Is there a better way to debug printouts in the Linux kernel?

Right now littering the code with:

printk(KERN_DBG "%s:%d - %s() <message>", __FILE__, __LINE__, __FUNCTION__ ); 

Which isn't very clean.

There ought to be a way for the whole row to be #ifdef:ed in some nice way.

like image 827
user616128 Avatar asked Feb 14 '11 11:02

user616128


People also ask

How do I enable debug prints in Linux kernel?

To activate debug messages for core code and built-in modules during the boot process, even before userspace and debugfs exists, use dyndbg="QUERY" , module.

How do I check my printk messages?

printk messages go to the kernel log message buffer, which can be exposed in a variety of ways depending on system configuration. The shell command dmesg will show them, and they should also be being copied to files in /var/log by the syslog daemon.

What is the difference between printf and printk?

Difference between Printk() and Printf() in Linux :printf() is a C Standard Library function. printk() is used by the kernel to print. To print something from the application layer it uses printf(). The printk() method can be called at any time from almost anywhere in the kernel.


2 Answers

Use

/* At the top of the file, before any includes */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/printk.h>

/* in code... */
pr_devel("foobar happened\n");

as a basis (the standard practice). You can then add __FILE__ or __LINE__ to the pr_fmt definition if you need.

like image 103
user611775 Avatar answered Sep 21 '22 11:09

user611775


If this is for quick debugging, just printk() works well.

If this is for debugging in more production situation, maybe use pr_debug(), so messages can be enabled at runtime.

Regardless, ("%s: xxx", func) is usually enough. These filenames and line numbers will become annoying very soon. This is also why you haven't found any "standard" solution -- because there is none.

like image 22
adobriyan Avatar answered Sep 21 '22 11:09

adobriyan