5

I wrote a x86 assembly program for MBR section. I compile it as follows:

nasm hellombr.asm -f bin -o hellombr.img

Then I run it in qemu:

qemu -fda hellombr.img -boot a

The question is how can I debug my program at source level?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
hellodanylo
  • 380
  • 3
  • 15
  • Have you looked [here](http://stackoverflow.com/questions/2611624/low-level-qemu-based-debugging)? I know GDB can do source level debugging. –  Nov 27 '11 at 19:37
  • This is the problem, I can not get GDB work with sources. NASM rejects to compile flat binary file with -g flag. – hellodanylo Nov 28 '11 at 02:29

3 Answers3

5

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.

Community
  • 1
  • 1
mtvec
  • 17,846
  • 5
  • 52
  • 83
1

Instead of using qemu, use bochs. It is completely compatible, albeit slower. It is also an emulator but if you make it from sources, using these flags and build it like this:

./configure --enable-debugger --enable-disasm --disable-docbook
make
make install

you can place breakpoints in your code, step through it, view GDT, IDT and everything you needed to know.

Manuel Ferreria
  • 1,216
  • 1
  • 13
  • 23
0

A really good (and simple) way is to use IDA with bochs, you find an excellent blog post on it here, along with some other hints/suggestions for bootloader development.

Necrolis
  • 25,836
  • 3
  • 63
  • 101