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

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which NASM options embed source-line debug information?

    Answer

    -g -F dwarf

    -g writes debug information and -F dwarf selects the DWARF format gdb reads.

  2. Which command starts the program under gdb?

    Answer

    run

    run starts the process, and it stops at the first breakpoint.

  3. What does break _start do?

    Answer

    It stops the program before the instruction at _start runs

    A breakpoint stops execution when control reaches that address, before the instruction executes.

  4. Which gdb command executes exactly one machine instruction?

    Answer

    stepi

    stepi is the instruction-level step; next and step move by source line.

  5. Which command shows the values of all registers?

    Answer

    info registers

    info registers prints the general-purpose registers and their values.

  6. What does x/8xb &msg print?

    Answer

    Eight bytes at the address of msg, in hex

    x examines memory: 8 units, in hex, one byte each, at the given address.

  7. Which tool runs the program and stops at breakpoints?

    Answer

    gdb

    gdb is the debugger; objdump only disassembles a file.

  8. Why does a debug build keep the labels?

    Answer

    The debug information maps addresses back to names

    DWARF records the address of each label, which is how gdb shows _start and my_label.

  9. What does continue do after a breakpoint?

    Answer

    It runs until the next breakpoint or the end

    continue resumes the program and stops again only at a breakpoint or exit.

  10. Which gdb command disassembles the current function?

    Answer

    disassemble

    disassemble prints the machine instructions of the code around the current position.

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