环境:
Ubuntu 14.04 64bit
Bochs 2.6.6
GDB 7.7
想要源码级调试C内核,需要在编译Bochs时加上 --enable-gdb-stub 参数
sudo ./configure --enable-all-optimizations --enable-long-phy-address --enable-disasm --enable-alignment-check --enable-pci --enable-cdrom --enable-gameport --enable-large-ramfile --enable-gdb-stub --enable-show-ips --with-all-libs
配置bochsrc文件:
gdbstub:enable=1,port=1234,text_base=0,data_base=0,bss_base=0
打开两个终端,在第一个终端中运行Bochs:
$:bochs -f bochsrc
在第二个终端中运行GDB:
$:gdb ./src/kernel.elf
进入gdb命令行,在gdb命令行中输入命令:
break main(在main函数开始处下断点)
target remote localhost:1234(连接到Bochs)
c(运行到断点处停下)
...后续GDB调试命令...
注意:
1、使用Bochs+GDB源码调试,必须以-g编译C源码,当使用ld链接时,去掉-s选项
2、可以用list查看源文件代码,但不能使用s或n单步调试和查看变量值,出现如下错误:
(gdb) n
Cannot find bounds of current function
(gdb) n
Cannot find bounds of current function
(gdb) n
Cannot find bounds of current function
出现这种情况,可能与编译Bochs选项有关,去掉 --enable-x86-64, --enable-vmx两个选项后重新编译Bochs,然后在bochsrc文件中注释掉这两行:
cpu:model=core2_penryn_t9600,count=1,ips=50000000,reset_on_triple_fault=1,ignore_bad_msrs=1,msrs="msrs.def"
cpu:cpuid_limit_winnt=0
有图有真相:
|