Debugging your Linux Kernel Module

Navaneeth Krishnan
1 min readDec 20, 2018

--

Imagine you have been spending hours and hours writing a Linux kernel module and then when you run, there’s something missing. So how do you debug :)?

Make sure your Makefile for the LKM looks something like this

obj-m += <Module name>.o
MY_CFLAGS += -g -DDEBUG
ccflags-y += ${MY_CFLAGS}
CC += ${MY_CFLAGS}
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
debug:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
EXTRA_CFLAGS="$(MY_CFLAGS)"
clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Note the MY_CFLAGS, thats the thing which makes the kernel module debuggable.

Once you insmod your Linux Kernel Module. Find your LKM’s ELF text address by running this command

cat /sys/module/e1000_ynk/sections/.text 

root@ubuntu:/home/nyadunan/network_driver# cat /sys/module/e1000_ynk/sections/.text .bss
0xffffffffc03c9000
cat: .bss: No such file or directory

The text section for my LKM is 0xffffffffc03c9000

Now from the location of your .ko file run the following commands

(gdb) add-symbol-file <module-name> 0xffffffffc03c9000
add symbol table from file “e1000_ynk.ko” at
.text_addr = 0xc03c9000
(y or n) y
Reading symbols from e1000_ynk.ko…done.
(gdb) p
The history is empty.
(gdb) bt
No stack.
(gdb) info variables
All defined variables:

File ./include/linux/fs.h:
static const char * constkernel_read_file_str[9];

File /home/nyadunan/network_driver/e1000_ynk.c:
static const struct pci_device_id desig_init_pci_device_id_tbl[2];

File /home/nyadunan/network_driver/e1000_ynk.mod.c:
struct module __this_module;
static const char __UNIQUE_ID_alias17[43];
static const char __UNIQUE_ID_name15[15];
static const char __UNIQUE_ID_retpoline16[12];
static const char __UNIQUE_ID_srcversion18[35];
static const char __UNIQUE_ID_vermagic14[43];
static const char __module_depends[9];
(gdb) info locals

And there you go :-) . Thats how you debug your LKM

--

--

Navaneeth Krishnan

Linux and Computer Networks Enthusiast, follows the holy grail of operating systems!