Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug the init_module() call of a Linux kernel module?

I am doing first steps into Linux kernel development. I have some code producing a .ko kernel module that I install with insmod. I would like a way to debug what happens when I install the module but I am facing some difficulties.

  1. I need to debug the call to init_module. Is this function called when I run insmode ?

  2. I try to use insmod "/my/url/fil.ko" -m to debug what happens but each time I got error -1 Unknown symbol in module while in /cat/log/message I can see the error unknown parameter -m

  3. Do you know if there is a way to debug with GDB?

like image 456
Abruzzo Forte e Gentile Avatar asked Jul 17 '26 14:07

Abruzzo Forte e Gentile


2 Answers

Yes, the init_module function gets called as soon as you load it to the kernel using insmod. You can just add a line of printk and verify it being printed as soon as you insert the module.

You cannot pass a parameter such as -m to debug the kernel module.

You can only pass parameters that are intended to be handled within the kernel module that you have written, using MODULE_PARAMS.

like image 73
Amarnath Revanna Avatar answered Jul 20 '26 06:07

Amarnath Revanna


QEMU + GDB step debug module_init

First get kernel module QEMU + GDB debugging in general working before trying out module_init: How to debug Linux kernel modules with QEMU?

module_init is harder because we don't know where the kernel module will get loaded before it does.

Then, here are two non-ideal but usable techniques to break into module_init:

  1. Find the module load address, and reuse it later.

    The module load location is deterministic after each boot, so we can find:

    • the base address of the .text section: How to get the address of a kernel module that was inserted using insmod?

    • the location of the module_init symbol inside the .text section:

      ./readelf -s fops.ko | grep myinit
      

    Then, add them up, and tell GDB to break at that point.

  2. Step into the module_init call.

    On kernel 4.16, first break at:

    do_init_module
    

    Then step until:

    ret = fn();
    

    Then step into that, and you fall inside the module_init function.

This QEMU + Buildroot setup can be used to conveniently test both of these methods.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!