Home > Linux, Linux Kernel, Programming > Loading symbols when debugging the kernel and kernel modules

Loading symbols when debugging the kernel and kernel modules

October 29th, 2009 Leave a comment Print Print Go to comments

Recently I received some comments from a friend about a previous article on linux kernel debugging using kgdb. What he asked me was how could he load symbols from a kernel or a kernel module. So I wrote a quick guide to help you start with kernel debugging. After each step I will show you the gdb output.

First of all you should start gdb!

$ gdb
GNU gdb (GDB) 6.8.50.20090628-cvs-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
.
(gdb)

Then you should load all kernel symbols from the vmlinux file. This can be found at the directory where you compiled the kernel, most probably /usr/src/linux. Remember to compile the kernel using debug information by setting the appropriate option, it will help you a lot!

(gdb) file vmlinux
Reading symbols from /home/fotisl/programs/kgdb/vmlinux...done.
(gdb)

You’re ready to start debugging! Set the target and use the Alt-SysRq-G sequence as it was described at the previous post. You can now set breakpoints, watch anything you want in memory, step or continue running the kernel!

(gdb) target remote /dev/pts/12
Remote debugging using /dev/pts/12
kgdb_breakpoint (key=103, tty=0x0) at kernel/kgdb.c:1721
1721            wmb(); /* Sync point after breakpoint */
(gdb)

Now let’s see how we can debug kernel modules. I will test the l2cap bluetooth kernel module.

You first need to find the object file which contains the module. For l2cap this is net/bluetooth/l2cap.o in the kernel source tree. Transfer this to the host (or the machine running gdb if you’re not using a virtual machine). Then load the module in the virtual machine. This creates a new directory in /sys/module named after the module name, i.e. l2cap. Inside this directory, there is another one named sections which contains the addresses where all sections are loaded. We are interested in the .text section so we read the file /sys/module/l2cap/sections/.text.

$ cat /sys/module/l2cap/sections/.text
0xe0c77000

We know where the .text section is loaded so we can now load the symbols from l2cap.o using the add-symbol-file gdb command.

(gdb) add-symbol-file l2cap.o 0xe0c77000
add symbol table from file "l2cap.o" at
        .text_addr = 0xe0c77000
(y or n) y
Reading symbols from /home/fotisl/programs/kgdb/l2cap.o...done.
(gdb)

If you need to load other sections too, in case they are not contiguous with the text in memory, you need to read their addresses. For example we’ll load both the .text and the .data sections (you should do .bss too but it’s omitted since I wanted to write a quick and dirty guide and it’s already very big!)

Find where both .text and .data are loaded.

$ cat /sys/module/l2cap/sections/.text
0xe0c77000
$ cat /sys/module/l2cap/sections/.data
0xe0c7b438

Then you load apart from the .text section the .data too.

(gdb) add-symbol-file l2cap.o 0xe0c77000 -s .data 0xe0c7b438
add symbol table from file "l2cap.o" at
        .text_addr = 0xe0c77000
        .data_addr = 0xe0c7b438
(y or n) y
Reading symbols from /home/fotisl/programs/kgdb/l2cap.o...done.
(gdb)

You’re now ready to start debugging your kernel module!

Categories: Linux, Linux Kernel, Programming Tags:
  1. February 25th, 2014 at 04:19 | #1

    BTW, the symbol file adding routine can be easily automated using VisualKernel, a Visual Studio extension for debugging Linux Kernel. There’s a tutorial showing a sample debugging session here: http://visualkernel.com/tutorials/kgdb

  2. fauny
    April 3rd, 2014 at 14:30 | #2

    Hi,
    Following the above steps, the screen on the target machine displays:
    kgdb: Waiting for comnnection from remote gdb…

    On the host machine I followed:
    gdb
    (gdb) file vmlinux
    (gdb) target remote /dev/ttyS0
    Remote debugging using /dev/ttyS0

    And, then nothing happens on both the machine.. Please help me for debugging the kernel.
    Any help in this regard will be highly appreciable.

  3. Fotis
    April 12th, 2014 at 22:24 | #3

    Please check my post entitled ‘Debugging the linux kernel using kgdb and VirtualBox’.

  4. September 3rd, 2014 at 12:58 | #4

    Howdy! Do you know if they make any plugins to assist with SEO?

    I’m trying to get my blog to rank for some targeted
    keywords but I’m not seeing very good gains. If you know of any
    please share. Appreciate it!

  1. No trackbacks yet.

This site is using OpenAvatar based on

SEO Powered by Platinum SEO from Techblissonline
%d bloggers like this: