FreeBSD Remote Kernel Debugging

This document shows how to do FreeBSD kernel debugging using remote GDB. I wrote it up just for my reference, after having collected instructions spread across a few files in the FreeBSD documentation project.

0a. Check if your kernel is debug-enabled. In the file /usr/src/sys/i386/conf/GENERIC (replace GENERIC with your kernel name if you are using a custom kernel), look for the following line:

makeoptions	DEBUG=-g
If it's not there, add it.

0b. Check if the config file has the following line:

options	DDB
If it's not there, add it.

If you make these changes, recompile the kernel and install it on the system to be debugged ("live system").

1. Connect the serial cable between live system and development system.

2. Edit /etc/ttys in the live system to enable serial cable communication.

ttyd0   "/usr/libexec/getty std.9600"   dialup   off      secure

To:

ttyd0   "/usr/libexec/getty std.9600"   vt100   on      secure

(The above assumes the cable is connected to COM1 port.)

3. Restart the live system, or just say "kill -HUP 1".

4. Go to the kernel compile directory in the development system (in the /usr/obj/... hierarchy).

5. In the development system and in the compile directory, issue:

gdb -k kernel.debug
or:
kgdb kernel.debug

(assuming kernel.debug is the unstripped kernel: if kernel is itself unstripped, use that)

6. Log in to the live system (through telnet or console) and issue:

sysctl debug.enter_debugger=gdb
or in FreeBSD 6.x,
sysctl debug.kdb.enter=1

7. In the developer system, within the gdb prompt, say:

(kgdb) target remote /dev/cuaa0

After this, you should be able to debug the kernel like any other program in GDB.

This file created: 26/Apr/2006
Back home