linuxlinux-kernellinux-device-driverdmesg

printk messages not showing up in dmesg log


I'm developing a simple driver for fun, and I'm trying to debug it with printk messages. It seems that dmesg doesn't display the last printk message, and the only way to get it to show the last message is by doing another printk, which in turn is also swallowed by dmesg.

Some examples for clarification: in my module's init function I print the following statement:

printk(KERN_INFO "%s loaded with major: %u, minor: 0", ...);

After insmoding for the first time after a boot, I see a "tainted kernel" message with dmesg, however I can't see the message I printed.

Then, I run the following command:

cat /dev/mymodule

which invokes my open function which prints:

printk(KERN_DEBUG "%s opened", ...);

and then invokes my read function which prints:

printk(KERN_DEBUG "%s reading", current->comm);

and then goes to sleep. After running dmesg (while the read function sleeps), I can suddenly see my module init message, and I can also see the "cat opened" message, however the "cat reading" message is missing.

Terminating cat with ctrl+c causes two prints: "sleep interrupted", and then "cat released". Running dmesg shows the "sleep interrupted" message, but the "cat released" message is missing, and so on.

Does anyone know what the problem might be?


Solution

  • You should add \n at end of every printk message not to miss any logs in the dmesg. This will solve your problem.