在xv6中从bootasm.S进入bootmain()后,如何用GDB单步调试C语句?
Hey, I've run into this exact issue when debugging xv6's bootloader before—here's how to fix that "Cannot find bounds of current function" error so you can step through bootmain.c properly:
Fixing "Cannot find bounds of current function" when debugging xv6's bootmain.c in GDB
1. Make sure debug info is included when compiling bootmain
The default xv6 Makefile often skips generating debug symbols for the bootloader components. Let's fix that:
- Open the xv6 Makefile and find the rule for compiling
bootmain.o. It'll look something like this:bootmain.o: bootmain.c $(CC) $(CFLAGS) -c bootmain.c - Add the
-gflag to force debug symbol generation:bootmain.o: bootmain.c $(CC) $(CFLAGS) -g -c bootmain.c - Rebuild the bootblock to apply the changes:
make clean make bootblock
2. Load bootblock symbols with the correct memory address
The xv6 bootloader loads into memory at 0x7c00, and GDB needs to know this to map debug symbols to the actual running code. Here's what to do:
- Start QEMU in debug mode, waiting for GDB to connect:
qemu-system-i386 -s -S -drive file=xv6.img,index=0,media=disk,format=raw - Launch GDB in another terminal and connect to QEMU:
target remote localhost:1234 - Load the bootblock symbols, specifying the base address
0x7c00:
If you want to load justadd-symbol-file bootblock.o 0x7c00bootmain.o's symbols, first usenmto find its offset in the bootblock:
For example, if the output isnm bootblock | grep bootmain00007d60 T bootmain, load the symbols with that address:add-symbol-file bootmain.o 0x7d60
3. Debug with the right workflow
- Set a breakpoint at
bootmainfirst, then run to it:b bootmain c - Once the program stops at the start of
bootmain, using thes(step) command will now let you step through individual C statements instead of assembly.
Quick troubleshooting tip
If you're still having issues, run info files in GDB to check which symbol files are loaded and their associated memory addresses—this helps confirm if the symbols are mapped to the correct location.
内容的提问来源于stack exchange,提问作者user5280911
相关产品推荐
相关产品推荐

