Debugging with GDB

Assemble with debug info

Add -g to the NASM command to embed source-line information, then run the program under gdb:

nasm -f elf64 -g -F dwarf -o hello.o hello.asm ld -o hello hello.o gdb ./hello

Breakpoints

Set a breakpoint on the entry point or a label, and run. The program stops before the instruction executes:

break _start run break my_label

Stepping and registers

stepi executes one machine instruction. info registers shows all registers, and x/8xb addr dumps eight bytes of memory in hex. These three commands cover most assembly debugging:

stepi info registers rax rbx rcx x/8xb &msg

The code on this page was assembled with NASM 3.01, linked with GNU ld 2.45.1 and debugged with GNU gdb 16.3 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev