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

会有一股神秘感。 提交于 2019-12-22 10:45:36

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/11888412/how-to-debug-the-init-module-call-of-a-linux-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!