Where Does Printk() Print To

Where does printk() print to?

dmesg should display printk messages.

Where does output of print in kernel go?

It depends on the distribution, but many use klogd(8) to get the messages from the kernel and will either log them to a file (sometimes /var/log/dmesg or /var/log/kernel) or to the system log via syslog(3). In the latter case, where the log entries end up will depend on the configuration of syslogd(8).

One note about the dmesg command: Kernel messages are stored in a circular buffer, so large amounts of output will be overwritten.

How to find which function prints printk statement?

Since we are talking about Linux kernel programming, there are several ways to achieve this. The worst one is to define custom macro where you add something additional to print. Now, let's consider better approaches.

Approach 1.
If you are interesting to put function name to only subset of the messages, assuming for debug purposes, the best option is to enable Dynamic Debug option and use special macros instead of direct calls to printk, i.e. pr_debug(), dev_dbg(), netdev_dbg() and so on.

It will allow you to turn on and off any single message at run time along with enabling __func__ to be printed or not.

Approach 2.
Another approach if you want to enable additional arguments to the families of macros, such as pr_*() and dev_*(), you may define special macros at the very beginning of each module or, if you want, include from the header, though it must be the very fist one in each C-file. See example from ipmi_msghandler.c:

#define pr_fmt(fmt) "%s" fmt, "IPMI message handler: "
#define dev_fmt pr_fmt

It can be easily transformed to

#define pr_fmt(fmt) "%s(): " fmt, __func__
#define dev_fmt pr_fmt

The benefit of this method is a possibility to set up different prefixes to different modules, like using their filenames, and it will be applied to all messages of the same family of macros.

Disadvantage is that only pr_*() and dev_*() families do have such facilities.

printk() messages not appearing in console

I hope i can answer to your question. I also faced same issue and tried my level best to print kernel message to console, but nothing works. Then I started searching for the reason...

The reason is, If klogd is not running, the message won’t reach user space unless you read /proc/kmsg. Reference: oreilly or /dev/kmsg. klogd reads kernel log messages and helps process and send those messages to the appropriate files, sockets or users. since absence of daemon, it won't send to standard output. Unless you read the message from ring buffer or buffer overflow, it will remains.

Send printk to serial

Try booting your kernel with the command line 'console=ttyS1,<baud>', where <baud> is the baud rate you configured for that port in the VM settings. The kernel will then use /dev/ttyS1 as the main console, and that's where printk will send all of its output to.



Related Topics



Leave a reply



Submit