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.

#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");
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.


Comments

  1. Hi thanks, could you please give information on udev structure also.

    ReplyDelete
    Replies
    1. Sure will post a series on udev. Thanks for your comment

      Delete

Post a Comment

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto