Search code examples
linuxkernel-moduleprintk

printk() messages not appearing in console


So I'm trying to learn to write Linux modules and right now I'm experimenting with a basic "Hello World" module:

#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void){

    printk(KERN_ALERT "Hello, world.\n");
    return 0;
}

static void hello_exit(void){

    printk(KERN_ALERT "goodbye.\n");
}

module_init(hello_init);
module_exit(hello_exit);

And I've finally gotten this module to work! When I add with insmod it prints "hello" to kernel.log and when I remove it with remmod it prints "goodbye" to kernel.log.

My trouble is that I decided I want to try and get the output to also print to the console. From what I understand about printk(), is that in order for messages to show up in the console, the console must be set to the appropriate message level in /proc/sys/kernel/printk. (This is all according to https://elinux.org/Debugging_by_printing). My console is set to level 4.

cat /proc/sys/kernel/printk:

4       4       1       7

Since KERN_ALERT is level 2 and my console is set to print out level 4 and below messages, why are the printk messages not appearing on my console? When I run dmesg I can see the messages are clearly in the buffer, but never go to the console. It's not I really need them to print to the console, but I really want to understand how this all works.


Solution

  • 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.