Sunday, January 28, 2018

Debugging Running Kernel Module in Linux

Debugging Running Kernel Module  in Linux 

Suppose you have a kernel module( kthread_signal.ko) that increments a counter ( + 1 every second),
and you want to check through gdb , this can be done using folllowing steps.

1. First locate address file of module eg .text .data .bss

e.g
root@embsys-VirtualBox:/sys/module/kthread_signal/sections# pwd
/sys/module/kthread_signal/sections
root@embsys-VirtualBox:/sys/module/kthread_signal/sections# ls -A1
.bss
.gnu.linkonce.this_module
__mcount_loc
.note.gnu.build-id
.rodata.str1.1
.rodata.str1.8
.strtab
.symtab
.text

2.Find the start address of module .
root@embsys-VirtualBox:/sys/module/kthread_signal/sections# cat .text .bss
0xffffffffa012d000
0xffffffffa012f3c0

3.Open Linux binary and snapshot of running kernel ( core).

>gdb /roce/linux-src/linux-4.14.12/vmlinux /proc/kcore

4. Load symbol file with address
>add-symbol-file kthread_signal.ko 0xffffffffa012d000 \
 -s .bss 0xffffffffa012f3c0

(gdb) add-symbol-file kthread_signal.ko 0xffffffffa012d000 \
 -s .bss 0xffffffffa012f3c0
add symbol table from file "kthread_signal.ko" at
        .text_addr = 0xffffffffa012d000
        .bss_addr = 0xffffffffa012f3c0
(y or n) y
Reading symbols from kthread_signal.ko...done.

5. Now print the variable ( local_cnt is global variable)
(gdb) p local_cnt
$1 = 131
(gdb) p local_cnt
$2 = 131
You see tnat counter is not incrementing as kcore is not updated once you put in gdb.
to reload and refresh core

load gdb/kcore again and you can see increase in couter.

eg.

(gdb) core-file /proc/kcore
[New process 1]
Core was generated by `BOOT_IMAGE=/boot/vmlinuz-4.14.12 root=UUID=0976909a-8797-40c6-b85d-9eb9af256cd0'.
#0  0x0000000000000000 in irq_stack_union ()
(gdb) p local_cnt
$41 = 8829
(gdb) p local_cnt
$42 = 8829
(gdb) p local_cnt
$43 = 8829
(gdb) core-file /proc/kcore
[New process 1]
Core was generated by `BOOT_IMAGE=/boot/vmlinuz-4.14.12 root=UUID=0976909a-8797-40c6-b85d-9eb9af256cd0'.
#0  0x0000000000000000 in irq_stack_union ()
(gdb) p local_cnt
$44 = 8833

No comments:

Post a Comment

Featured Post

XDP - Getting Started with XDP (Linux)