FreeBSD kernel debugging
The FreeBSD kernel can be debugged with the ddb(4) interactive kernel debugger. Although the latest production release of FreeBSD (7.1 at the time of this writing) adds some very useful features, ddb is still lacking the flexibility of gdb.
The FreeBSD developer’s handbook has a section on kernel debugging using remote gdb, but it is not directly applicable to VMware-based installations. The solution is to use VMware’s feature of creating virtual serial ports as named pipes to emulate a serial connection between two FreeBSD virtual machines.
The first FreeBSD virtual machine, let’s call it the target host to follow the handbook’s terminology, is the machine that the experimental kernel code runs on. The second, the debugging host, is the one that will run gdb and connect over the virtual serial connection to the target host. The target host’s kernel needs to be compiled with the following options:
Furthermore, the serial port needs to be defined in the device flags in the
/boot/device.hints
file of the target host by setting the 0x80
bit, and the
0x10
bit for specifying that the kernel gdb backend is to be accessed via
remote debugging over this port:
Also, edit the target host’s /etc/sysctl.conf
file to include the
following self-explanatory kernel parameters:
After the compilation and installation of the new kernel on the target host,
the /usr/obj/usr/src/sys/TARGET_HOST
directory (assuming you have named the
new kernel TARGET_HOST
) needs to be copied to the debugging host (for example
with scp -r
).
For the following steps both virtual machines need to be turned off. In VMware
go to the tab of the target host, click Edit virtual machine
settings->Add->Serial Port->Output to named pipe. Enter /tmp/com_1
(or
whatever you want) as the named pipe, select This end is the server and The
other end is a virtual machine. Then perform the same steps on the debugging
host’s virtual machine, enter the same named pipe, but select This end
is the client in this case. The /tmp/com_1
named pipe on the machine that
runs VMware (Linux in our case) will be used as a virtual serial connection
between the two FreeBSD guests.
Now power on the target host normally, cause a kernel panic or start the kernel
debugger manually, and type gdb
and then s
:
On the debugging host you need to find the device that corresponds to the
virtual serial port you defined in VMware. On our setup it is /dev/cuad0
.
Then start a kgdb
remote debugging session in the
/usr/obj/usr/src/sys/TARGET_HOST
directory, passing as arguments the serial
port device and the kernel to be debugged:
That’s it. Now let’s do some kernel debugging.