Debugging The Linux Kernel

To be able to debug the Linux kernel, you need to enable the kgdb support in the kernel configuration and select a suitable debug port.

make -C packages/os/linux-2.6 menuconfig

Enable these options:

Kernel hacking  --->
  [*] Kernel debugging
  [*] Compile the kernel with debug info
  [*] Use kernel GDB debugger

If you are using a 2.4 kernel, enable

General setup  --->
  [*] Use kernel gdb debugger

Then rebuild the kernel with:


or (quicker):

make -C packages/os/linux-2.6 install
make images

Note that when you enable the kgdb support, the Linux kernel will be built with -g and so may increase significantly in size.

When you boot, something like this should show up on the serial port (use sermon or some other serial sniffer):

  Uncompressing Linux...
  Done. Now booting the kernel.
  [lots more]

The garbled stuff is the gdb stub dumping information ($ start a packet, # ends the packet, and then there's a 2 hex digit checksum). Now start gdb-cris with the vmlinux file as argument:

  gdb-cris vmlinux


  (gdb) set remotebaud 115200
  (gdb) target remote /dev/ttyS0
  Remote debugging using /dev/ttyS0
  breakpoint () at kgdb.c:1530
  1530 __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */

A quick backtrace will tell you where you are:

  (gdb) backtrace
  #0 breakpoint () at kgdb.c:1530
  #1 0x6004a8ca in init_IRQ () at irq.c:487
  #2 0x600bc49c in start_kernel () at init/main.c:535

A couple of things to remember when debugging the kernel:

  • Turn off the watchdog (make menuconfig, under General setup). It will bite you otherwise.
  • It's usually a bad idea to use the same serial port for both Kernel GDB and product debug. Make sure different ports are selected for product debug and Kernel GDB (make menuconfig, Hardware Setup).
  • The kernel is compiled with the -O2 flag. The compiler is likely to have rearranged the statements in the code, and variables may have been optimized away.
  • The Linux kernel contains many inlined functions, multi-statement macros and assembly code. Be prepared to disassemble to really understand what's going on.
  • Things happen under your feet. For example, interrupts caused by the timer occur while you are debugging.
  • Some parts of the code don't really like being stepped through, like the somewhat critical stack frame setup in entry.S.
axis/gdb-debug-kernel.txt · Last modified: 2007/05/30 10:55 by jesper
All text is available under the terms of the GNU Free Documentation License (see Copyrights for details).