How to load debug symbols of external linux module in gdb?

2026-06-02

Stack Overflow: View Question

Tags: c, debugging, linux-kernel, gdb

Score: 4 | Views: 138

The asker has a vmlinux with debug symbols and a .ko module they need to debug during init. The usual recipe — load the module, read its section addresses from /sys/module/<name>/sections/, then add-symbol-file in GDB — doesn't work here because by the time you've gathered that information, module_init() has already run. They need symbols resolved before the module's init code executes.

This is a classic chicken-and-egg: you can't know where the module will be loaded until the kernel allocates memory for it, but you need a breakpoint before that memory is used.

The clean approach: break inside the module loader

The kernel itself loads modules through do_init_module() in kernel/module/main.c (or kernel/module.c on older trees). The trick is to set a breakpoint in the kernel that fires after the module's sections have been allocated and relocated but before do_one_initcall(mod->init) runs.

The kernel's own scripts/gdb/linux/symbols.py (the lx-symbols command) does almost exactly this — it walks the module list and loads symbols automatically. The catch is that it normally runs after modules are loaded. You can either hack it to break at do_init_module entry, or call lx-symbols from inside that breakpoint handler before stepping into do_one_initcall.

Gotchas

The challenge: Debugging code that hasn't been placed in memory yet requires breaking inside the loader itself and resolving symbols against runtime-allocated section addresses before the module's first instruction runs.

All newsletters