I am writing an OS in assembly (the bootloader and the kernel) and I am debugging it using QEMU.
I want to set some debug breakpoints to pause the executions and execute only one instruction at the time (single step).Also i want to read and set the registers and memory contents at runtime. I read that a way to do that is to use the GDB. The problem is I haven't used the GDB before and after I searched it I realize that the GDB uses labels puted by the C compiler (gcc) in the object file,doesn't it? But as I said I am writing in assembly (using NASM) and I a have pure raw binary so how to do that? Does exist another way? Maybe can I create my own GDB server or something like that?
Asked
Active
Viewed 992 times
-1

ARISTOS
- 336
- 3
- 7
-
gdb can use raw addresses. See the help or the manual. Also, nothing stops you from building your asm code with debug info so you get to use labels and line numbers. – Jester Aug 18 '17 at 19:15
-
You can compile your assembly file with debugging info on (option depends on the assembler, but with NASM you can use -g -F dwarf). You compile _C/C++_ with debugging info on (GCC is `-g` option). You use a linker to create an ELF file (this ELF file contains the debug info). If you are not using GRUB and wrote your own bootloader then you'll need to use something like `objcopy` to convert the ELF exectuable into a binary file. – Michael Petch Aug 19 '17 at 02:14
-
If you are using NASM to generate BINary files and not ELF you'll have to restructure the way you build because NASM doesn't support much in the way of debugging with `-f bin` option. – Michael Petch Aug 19 '17 at 02:19
-
if you had a project on github one might be able to give you more assistance. The question as it is may be too broad for SO asit depends on how you are building your bootloader and your kernel and tying them together. – Michael Petch Aug 19 '17 at 02:29
-
As well GDB doesn't do well debugging 16-bit code. It can be done but there are limitations. If it is all 32-bit code then it is easier. – Michael Petch Aug 19 '17 at 03:26
-
[This other SO answer](https://stackoverflow.com/a/33619597/3857942) shows how you can generate ELF objects with debugging info for both GCC and NASM files and then link them together to make an ELF executable. Then - rather than create BIN files directly you generate ELF exectutables with debug info and then use something like `objcopy` to convert the ELF excecutables to BIN files. You use the BIN files in QEMU and the ELF files in the GDB debugger for symbols. The answer I link to also shows how you can start up a remote debugging session with debug symbols in GDB/QEMU. – Michael Petch Aug 19 '17 at 15:50
-
The answer linked in y last comment has a complete example with bootloader in NASM that switched into protected mode, loads the kernel into memory and then FAR JMPs to the _C_ entry point. The complete code can be found under the heading _Code After Making All Recommended Changes_ – Michael Petch Aug 19 '17 at 15:56
1 Answers
0
I realize that the GDB uses labels puted by the C compiler (gcc) in the object file
GDB does do that when the labels are available. When they are not, GDB will display raw addresses.
Nothing stops you from setting breakpoints on raw addresses even when other "labels" are available.

Employed Russian
- 199,314
- 34
- 295
- 362