我为 MBR 部分编写了一个 x86 汇编程序。我编译如下:
nasm hellombr.asm -f bin -o hellombr.img
然后我在 qemu 中运行它:
qemu -fda hellombr.img -boot a
问题是如何在源代码级别调试我的程序?
You should let nasm
create the debugging symbols in an ELF file and then dump this to a flat binary to be used in the MBR. You can then instruct GDB to read the necessary symbols from the ELF file.
The complete procedure would then become something like this:
$ nasm hellombr.asm -f elf -g -o hellombr.elf $ objcopy -O binary hellombr.elf hellombr.img $ qemu -s -S -fda hellombr.img -boot a $ gdb (gdb) symbol-file hellombr.elf (gdb) target remote localhost:1234
For an explanation of the flags I pass to qemu
see this answer.
不要使用 qemu,而是使用bochs。它完全兼容,尽管速度较慢。它也是一个模拟器,但如果你从源代码中制作它,使用这些标志并像这样构建它:
./configure --enable-debugger --enable-disasm --disable-docbook
make
make install
您可以在代码中放置断点,单步执行,查看 GDT、IDT 以及您需要了解的所有内容。
一个非常好的(且简单)的方法是将 IDA 与 bochs 一起使用,您可以在此处找到一篇很棒的博客文章,以及引导加载程序开发的其他一些提示/建议。