Reputation: 193
Asking this seems absurd to me but...
I'm reading Linux Device Drivers to learn how to create kernel modules. The book seems a bit old but I couldn't find a more recent version.
So I set up a Debian 12 virtual machine with VirtualBox, cloned the Linux kernel v6.4.3, recompiled and installed it.
Finally, in a different folder, I wrote the book's first simple "hello world" example which I show at the end of this question. It uses printk()
to print a warning message in init and one in exit.
Doing:
insmod hello.ko
Prints an empty line in my dmesg
buffer, as shown in image below. Same for rmmod
command. So I found this question and decided to change my
printk(KERNEL_WARNING, "Hello, World!\n")
to
pr_err("Hello, world!\n")
and now I have a decent line shown in dmesg. Am I doing something wrong? What is wrong with using printk()
?
Code and image below:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Duas BSD/GPL");
static int hello_init(void)
{
// pr_err("Hello, world!\n"); // uncomment this line to get intended result
printk(KERN_ERR, "Hello, world!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ERR, "Goodbye, world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
dmesg
result below. Blank spaces in dmesg result are generated by insmod hello.ko
compiled with pr_err()
line commented. "Hello world" line was generated by uncommenting pr_err()
line.
Upvotes: 0
Views: 32