THIS_MODULE in Linux Device Drivers
What is THIS_MODULE?
Whenever you create a kernel module, the kernel's build machinery generates a struct module object for you, and makes THIS_MODULE point to it.
This struct contains many fields, some of which can be set with module macros such as MODULE_VERSION.
You can see from the definition of THIS_MODULE macro that it is referring to '__this_module'. This is defined when we build the module file by running 'make'
Let's write a sample module which prints the module name and version using THIS_MODULE.
Output:
You can see from the above screenshot, after running "make", it generated temporary files as well as the module(.ko). The *.mod.c file has struct module __this_module defined.
Whenever you create a kernel module, the kernel's build machinery generates a struct module object for you, and makes THIS_MODULE point to it.
This struct contains many fields, some of which can be set with module macros such as MODULE_VERSION.
You can see from the definition of THIS_MODULE macro that it is referring to '__this_module'. This is defined when we build the module file by running 'make'
Let's write a sample module which prints the module name and version using THIS_MODULE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> | |
#include <linux/kernel.h> | |
extern struct module __this_module; | |
static int myinit(void) | |
{ | |
/* Set by default based on the module file name. */ | |
pr_info("name = %s\n", THIS_MODULE->name); | |
pr_info("version = %s\n", THIS_MODULE->version); | |
pr_info("version = %s\n", (&__this_module)->version); | |
return 0; | |
} | |
static void myexit(void) {} | |
module_init(myinit) | |
module_exit(myexit) | |
MODULE_VERSION("1.0"); | |
MODULE_LICENSE("GPL"); |
Hi thanks, could you please give information on udev structure also.
ReplyDeleteSure will post a series on udev. Thanks for your comment
Delete