22.6. Debugging Loadable Modules Using GDB

When debugging a panic that occurred within a module, or using remote GDB against a machine that uses dynamic modules, you need to tell GDB how to obtain symbol information for those modules.

First, you need to build the module(s) with debugging information:

    # cd /sys/modules/linux
    # make clean; make COPTS=-g

If you are using remote GDB, you can run kldstat on the target machine to find out where the module was loaded:

    # kldstat
    Id Refs Address    Size     Name
     1    4 0xc0100000 1c1678   kernel
     2    1 0xc0a9e000 6000     linprocfs.ko
     3    1 0xc0ad7000 2000     warp_saver.ko
     4    1 0xc0adc000 11000    linux.ko

If you are debugging a crash dump, you'll need to walk the linker_files list, starting at linker_files->tqh_first and following the link.tqe_next pointers until you find the entry with the filename you are looking for. The address member of that entry is the load address of the module.

Next, you need to find out the offset of the text section within the module:

    # objdump --section-headers /sys/modules/linux/linux.ko | grep text
      3 .rel.text     000016e0  000038e0  000038e0  000038e0  2**2
     10 .text         00007f34  000062d0  000062d0  000062d0  2**2

The one you want is the .text section, section 10 in the above example. The fourth numerical field (sixth field overall) is the offset in hex of the text section within the file (0x62d0 in our example). Add this to the load address reported by kldstat to obtain the address of the module text in memory.

Take the load address of the module (as reported by kldstat) and add the offset of the text section within the module (0x62d0 + 0xc0adc000 = c0ae22d0 in our example). This is the address that the module code was relocated to. Use the add-symbol-file command in GDB to tell the debugger about the module:

    (kgdb) add-symbol-file /sys/modules/linux/linux.ko 0xc0ae22d0
    add symbol table from file "/sys/modules/linux/linux.ko" at text_addr = 0xc0ae22d0?
    (y or n) y
    Reading symbols from /sys/modules/linux/linux.ko...done.
    (kgdb)

You should now have access to all the symbols in the module.

For questions about FreeBSD, e-mail <questions@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.