Kernel Debugging

Tools, Strategies, and Tips


Tools

man RTFM (man is a fine manual)
bochs x86 emulator. Can be built with either an internal debugger (assembly) or a gdb stub (source-level).
gdb gdb debugger. Connect to bochs to facilitate source-level debugging. (Also works with many other emulators, virtual machines, and can run locally).
less Many of the commands listed here output to the console. To scroll through the output and search for text, pipe it through less. You can find a string by typing /thetext, then press enter. Usage:
$ verbose-command | less
objdump Outputs an object file (ELF). Example usage:
$ objdump -M i8086,att -d file_16bit $ objdump -M att -d file_32bit $ objdump -M att -D file_32bit
The -M att flag causes the output to be in AT&T assembly syntax. The first example disassembles a 16 bit program (-M i8086). The second line ommits that flag, so the disassemby has a default size of 32 bits. The -d flag generates the disassembly; -D includes the data segments of the program, too.
readelf Output ELF file metadata. Useful when writing your createimage for Project 1.
nm Dump symbol addresses, linkage types, and names. Useful for setting break points in bochs or Qemu. Run this through awk to output symbols in a format that bochs can use...
hexdump Dump a file in hex, decimal, octal, and ascii in various widths.
od Dump a file in octal, decimal, hex, and ascii in various widths. Note: file offsets are in octal

Wisdom


Debugging with Compiler Optimizations

Compiler optimizations are turned on by flags such as -O2. For example, your Makefile might have the line:
CFLAGS += -O2
Here are some optimizations that may get in the way of debugging:

Bochs vs Real Computers

Question: Why won't my code work on a real machine when it works perfectly in bochs?
Answer: Your code has bugs.

In general, testing your code on multiple computers is a good practice because each has a slightly different setup; there may be machine-specific settings which hide your bugs.

If you find that your code works in bochs, but not on other machines, consider these differences that are unique to bochs:

Once you get past loading your kernel, your primary challenge from this list will be dealing with uninitialized data (pointers).

Other Peculiarities of Bochs