A debugger doesn’t speed up your program or inject print statements into it. It pauses the process at an instruction boundary and exposes the registers.
Truncated output or a wrong exit code doesn’t require editing source code—just pause the process right before the suspicious instruction. In this lesson, I run our “hello” program under GDB, set a breakpoint at _start, step through instructions one by one, and compare the count in a register against the bytes sitting in memory. The debugger does this using ptrace.
Mental Model: What a Debugger Actually Does
| What it looks like | What happens under the hood |
|---|---|
Debugging means adding printf to the code |
The process is paused externally; source code stays untouched |
“Pausing” is like calling sleep |
Stopping raises SIGTRAP; control transfers to the debugger |
| “Stepping” runs one line of C | stepi executes a single machine instruction |
| The program “reports” its own state | Registers and memory are read on demand while paused |
Testbed and Launch Flags
The testbed is the “hello” program from lesson 04; running it under the debugger will show whether the byte count in memory matches what the kernel receives.
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; hello_broken.asm — the hello program from lesson 04 set up for debugging.
default rel
%define SYS_WRITE 1
%define SYS_EXIT 60
%define STDOUT 1
section .rodata
message: db "hello from gdb!!", 10
section .text
global _start
; ------------------------------------------------------------------------------
; _start — entry point; the kernel jumps here using the ELF header address.
; Input: rsp -> argc, argv, envp (unused)
; Output: never returns; exit code 0
; Clobbers: rax, rcx, rdx, rsi, rdi, r11 (syscall contract; flags untouched)
; ------------------------------------------------------------------------------
_start:
; Length is passed separately to write; this value needs to be checked.
; syscall: write(STDOUT, message, 16)
mov eax, SYS_WRITE
mov edi, STDOUT
lea rsi, [message]
mov edx, 16
syscall
; syscall: exit(0)
mov edi, 0
mov eax, SYS_EXIT
syscall
section .note.GNU-stack noalloc noexec nowrite progbitsThe launch flags give us a reproducible session:
-q— suppresses the startup banner and version info;--nx— skips~/.gdbinitand the system init file so personal aliases don’t alter output;-batch— runs commands from-exand exits without entering an interactive prompt.
I put set debuginfod enabled off first in every session: it disables network queries to distro debug-symbol servers, which can otherwise make GDB hang waiting on debuginfod. In lesson 03, we used this setting in batch mode; here it applies to both interactive and batch sessions.
Three Commands Before Your First Stop
gdb -q --nx ./build/hello_broken— loads the binary and its DWARF symbols.break _start— sets a breakpoint at the entry point.run— starts the process and stops at the first breakpoint.
break _start prints an address matching the e_entry field from lesson 04. A pure assembly binary has no main symbol, so break main won’t work.
The Session: Break, Step, and Inspect
$ cd lessons/05-gdb-basics
$ make build$ gdb -q --nx ./build/hello_broken
Reading symbols from ./build/hello_broken...
(gdb) set debuginfod enabled off
(gdb) break _start
Breakpoint 1 at 0x401000: file hello_broken.asm, line 26.
(gdb) run
Starting program: .../build/hello_broken
Breakpoint 1, _start () at hello_broken.asm:26
26 mov eax, SYS_WRITE
(gdb) info registers rip
rip 0x401000 0x401000 <_start>
(gdb) stepi
27 mov edi, STDOUT
(gdb) info registers rip
rip 0x401005 0x401005 <_start+5>
(gdb) stepi
28 lea rsi, [message]
(gdb) stepi
29 mov edx, 16
(gdb) stepi
30 syscall
(gdb) info registers rip rdx rsi
rip 0x401016 0x401016 <_start+22>
rdx 0x10 16
rsi 0x402000 4202496Putting set debuginfod enabled off at the start prevents hanging on a network lookup—details above.
What stepi Shows
In GDB, single-stepping one instruction at a time is done with stepi. After each step, rip points to the next instruction to execute, not the one that just finished.
0x401000— address ofmov eax, SYS_WRITE; after stepping,ripbecomes0x401005because that instruction is 5 bytes long;0x401005—mov edi, STDOUT, also 5 bytes; next address is0x40100a;0x40100a—lea rsi, [message], 7 bytes long; next address is0x401011;0x401011—mov edx, 16, 5 bytes long; next address is0x401016.
The disassembler confirms the instruction sizes:
$ objdump -d -M intel build/hello_broken | sed -n '/<_start>/,/syscall/p'
0000000000401000 <_start>:
401000: b8 01 00 00 00 mov eax,0x1
401005: bf 01 00 00 00 mov edi,0x1
40100a: 48 8d 35 ef 0f 00 00 lea rsi,[rip+0xfef] # 402000 <message>
401011: ba 10 00 00 00 mov edx,0x10
401016: 0f 05 syscallFor navigating upcoming lessons, three more debugger commands and one display setting come in handy:
nexti(abbreviatedni) — likestepi, except it steps over acallinstruction instead of diving inside the called function;finish— runs the current function until it returns (ret) and immediately displays the return value;bt(backtrace) — prints the active call stack;set disassembly-flavor intel— switchesx/iinstruction inspection to Intel syntax instead of GDB’s default AT&T syntax.
Registers and Memory
info registers rip rdx rsi prints three values:
rip—0x401016, the address of thesyscallinstruction, which hasn’t executed yet;rdx—0x10, or16in decimal;rsi—0x402000, the address of the first byte ofmessage.
We inspect the buffer using x/17xb. The full syntax is x/[N][format][size], where each part controls something different:
N— how many units to display;format— how to render each unit: hex, decimal, string, or instruction;size— how many bytes each unit spans.
For a C99 programmer, the size letters map directly to fixed-width integer types:
| Size | Bytes | C99 Type |
|---|---|---|
b |
1 | uint8_t |
h |
2 | uint16_t |
w |
4 | uint32_t |
g |
8 | uint64_t |
GDB supports 11 output formats in total; six core ones cover almost every memory query:
| Format | Output |
|---|---|
x |
hexadecimal integer |
d |
signed decimal integer |
u |
unsigned decimal integer |
c |
ASCII character |
s |
null-terminated string |
i |
CPU instruction |
In x/17xb, read the specifiers right to left:
b— one-byte unit size;x— hexadecimal output;17— display 17 units.
For example, x/4wd <address> prints four uint32_t values in decimal, and x/8gx <address> prints eight uint64_t values in hex.
The ampersand & is mandatory: it takes the symbol’s address. Without it, GDB treats message as the value stored at that symbol rather than its address—for a string, that would try to interpret its leading bytes as a pointer. On an untyped NASM symbol, the debugger refuses outright: x/17xb message prints 'message' has unknown type; cast it to its declared type. With &message, GDB gets the first byte’s address and reads 17 bytes starting there.
(gdb) x/17xb &message
0x402000 <message>: 0x68 0x65 0x6c 0x6c 0x6f 0x20 0x66 0x72
0x402008: 0x6f 0x6d 0x20 0x67 0x64 0x62 0x21 0x21
0x402010: 0x0a0x402000 <message>— the address and its associated symbol;- two 8-byte rows — the characters
hello from gdb!!; 0x402010: 0x0a— the 17th byte, a newline.
The byte count in the buffer and the count in rdx don’t match. Comparing them is how we verify the write contract.
Common Pitfalls
-
"
break mainwill pause the program, just like in C."
Fact: GDB repliesFunction "main" not defined.and offers a pending breakpoint.
Cause: the binary has nomainsymbol;_startis the entry point.
Fix: usebreak _startor break by address withbreak *0x401000. -
“You can just type
runwithout a breakpoint and inspect registers.”
Fact: the program printshello from gdb!!and immediately reports[Inferior 1 (process ...) exited normally].
Cause: without a breakpoint, GDB lets the process run to completion, leaving nothing left to inspect.
Fix: setbreakbefore callingrun, and inspect state prior tosyscall. -
“You can wrap GDB with
strace -fto trace the debugged process.”
Fact: running it fails immediately:textwarning: Could not trace the inferior process. warning: ptrace: Operation not permitted During startup program exited with code 127.Cause:
ptraceallows only one tracer per process;stracealready claimed that role on GDB’s child.
Fix: run GDB directly, and automate sessions with-batch -ex.
Lab
Goal. Find out why ./build/hello_broken | wc -c outputs 16 when the buffer holds 17 bytes, and fix the program.
Contract and requirements.
- keep the message in
.rodataunchanged—fix only how the length is passed; - once fixed, the program prints 17 bytes including the trailing newline;
- the exit code remains
0; - the length is computed by the assembler, not hardcoded as a literal number.
Verification criteria.
strace -e write ./build/hello_broken > /dev/nullshows the length the kernel receives.gdb -q --nx ./build/hello_brokenwithbreak _startandx/17xb &messageshows 17 bytes in the buffer.- Once fixed,
./build/hello_broken | wc -cprints17, and./build/hello_broken | xxd -g 1ends with byte0a.
Where the missing byte goes and how to verify it
The byte isn’t lost in the buffer or the kernel: mov edx, 16 explicitly passes 16 bytes to write. x/17xb &message shows 17 bytes in memory while rdx holds 0x10.
The fix is defining the length right next to the string and loading it into rdx:
message: db "hello from gdb!!", 10
message_len: equ $ - message
...
mov edx, message_lenAfter rebuilding:
$ ./build/hello_broken | wc -c
17
$ ./build/hello_broken | xxd -g 1
00000000: 68 65 6c 6c 6f 20 66 72 6f 6d 20 67 64 62 21 21 hello from gdb!!
00000010: 0a .strace -e write now shows write(1, "hello from gdb!!\n", 17). If rdx still shows 16, check where you set the length—not the syscall itself.
Lab source code, Makefile, and automated tests for this lesson: lessons/05-gdb-basics.
Summary
-qsuppresses the banner,--nxskips user init files, and-batchavoids the interactive prompt.set debuginfod enabled offshould be the standard first command in any reproducible session: it disables network symbol lookups and avoids hangs.- GDB pauses a process and reads its state via
ptrace. - Breakpoints can target a symbol or an address; pure assembly binaries have no
mainentry point. stepiexecutes one machine instruction, advancingripby its exact byte length.info registersandx/Nxbexpose program state without touching a single line of source code.
.data differs from .bss, why 0x11223344 sits backward in memory, and how to observe that in a memory dump.