Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show a printk() message in a console?

The information which is printed by printk() can only be seen under the Alt+Ctrl+F1 ~ F7 consoles.

These consoles are very inconvenient for debugging since they can't roll back. I am using the KDE desktop environment and console terminal. How could I redirect the printk() message to a console?

like image 213
Douglas Su Avatar asked Sep 04 '25 01:09

Douglas Su


2 Answers

The syntax of printk is

printk ("log level" "message", <arguments>);

The kernel defines eight log levels in the header file printk.h:

#define KERN_EMERG "<0>" /* system is unusable*/
#define KERN_ALERT "<1>" /* action must be taken immediately*/
#define KERN_CRIT "<2>" /* critical conditions*/
#define KERN_ERR "<3>" /* error conditions*/
#define KERN_WARNING "<4>" /* warning conditions*/
#define KERN_NOTICE "<5>" /* normal but significant condition*/
#define KERN_INFO "<6>" /* informational*/
#define KERN_DEBUG "<7>" /* debug-level messages*/

Each log level corresponds to a number and the lower the number, the higher the importance of the message.

The levels are useful in deciding what should be displayed to the user on the console and what should not be.

Every console has a log level called the console log level and any message with a log level number less than the console log level gets displayed on the console, and other messages which have a log level number higher or equal to the console log level are logged in the kernel log (kernel buffer) which can be looked into using the command dmesg.

The console loglevel can be found by looking into the file /proc/sys/kernel/printk:

cat /proc/sys/kernel/printk

Output:

4 4 1 7

The first number in the output is the console log level, the second is the default log level, third is the minimum log level and the fourth is the maximum log level.

Log level 4 corresponds to KERN_WARNING. Thus all the messages with log levels 3, 2, 1, and 0 will get displayed on the screen as well as logged and the messages with log level 4, 5, 6, and 7 only get logged and can be viewed using dmesg.

The console log level can be changed by writing into the proc entry

echo "6" > /proc/sys/kernel/printk
cat /proc/sys/kernel/printk

Output:

6 4 1 7

Now the console log level is set to 6, which is KERN_INFO.

Here you want to print out every message, so you should set your console level at the highest number, "8":

echo "8" > /proc/sys/kernel/printk
tail -f /var/log/kern.log &

or

cat /proc/kmsg & (Android Environment)
like image 137
Punit Vara Avatar answered Sep 06 '25 18:09

Punit Vara


Use

dmesg -wH &

to force all your kernel messages that are printed to dmesg (and also the virtual terminals, like Ctrl + Alt + F1, depending on your /proc/sys/kernel/printk log level and a level of your message), to also appear at your SSH or GUI console: Konsole, Terminal or whatever you are using! And, if you need to monitor only for the specific messages:

dmesg -wH | grep ERR &

I'm using it to monitor for the "ERROR" messages like

printk(KERN_EMERG "ERROR!\n");

that I printk from my driver.

like image 22
Mike Banon Avatar answered Sep 06 '25 19:09

Mike Banon