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.
I need to debug the call to init_module. Is this function called when I run insmode ?
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
Do you know if there is a way to debug with GDB?
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.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With