Get name of kernel module in Driver Code - KBUILD_MODNAME

Kernel build system defines KBUILD_MODNAME automatically from each Makefile.

Following is the sample code which prints the name of module when loaded and unloaded.


#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
char *modname = __stringify(KBUILD_MODNAME);
static int test_hello_init(void)
{
printk("%s: In init\n", __func__);
printk("%s: Module Name:%s Loaded\n", __func__, modname);
return 0;
}
static void test_hello_exit(void)
{
printk("%s: In exit\n", __func__);
printk("%s: Module Name:%s Removed\n", __func__, modname);
}
module_init(test_hello_init);
module_exit(test_hello_exit);
view raw modname_test.c hosted with ❤ by GitHub
Ouptut:


Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto