5.3.15 · D1Build Systems & Toolchain

Foundations — GDB debugging — breakpoints, watchpoints, step, next, backtrace

2,218 words10 min readBack to topic

This page builds every word, symbol, and picture the main GDB note leans on — starting from "what even is a running program in memory?" Nothing below assumes you have seen assembly, hexadecimal, or a stack diagram before. Each block earns the next.


0. What a running program actually is

Before we can stop a program, we must picture what is there to stop.

Figure — GDB debugging — breakpoints, watchpoints, step, next, backtrace

The topic needs this because a breakpoint is nothing more than making the IP stop moving at a box you chose. Everything else is detail.


1. Memory and addresses

What 0x means (hexadecimal)

Question: What is 0x1f in ordinary decimal?
.

2. Instructions vs. source lines

The parent note talks about both "one source line" (for step/next) and "one instruction" (for the trap). These are not the same, and confusing them causes real bugs in understanding.

Figure — GDB debugging — breakpoints, watchpoints, step, next, backtrace

3. The debug-symbol map (-g)


4. Optimization and why we turn it off (-O0)


5. Functions, the stack, and frames

The command bt (backtrace) prints "the chain of function calls." To read it you must first picture the stack.

Figure — GDB debugging — breakpoints, watchpoints, step, next, backtrace
Question: If main calls factorial and it crashes, which frame is number 0?
factorial — the top of the stack, where execution currently sits.

6. Traps and signals


7. Hardware debug registers (why hardware watchpoints are fast)


8. The slowdown symbols (, , , )

The parent's cost formula uses four letters. Here is each, from zero:

Question: In , which symbol becomes ≈0 for a hardware watchpoint?
(per-line trap cost) → so .

The prerequisite map

Bytes and addresses (hex 0x)

Machine instructions and the IP

Source line vs instruction

Traps and the 0xCC byte

Signals SIGTRAP and SIGSEGV

Breakpoints

Step and Next

Debug symbols with -g

Names for variables and lines

Optimization -O0

Watchpoints on result

Debug registers DR0 to DR3

Slowdown S = 1 + Nc over T0

Stack and frames

Backtrace bt

GDB debugging session


Equipment checklist

Cover the right side and test yourself. If any line is fuzzy, reread its section above before tackling the parent note.

A byte holds values in what range, and how many hex digits show it?
0 to 255, exactly two hex digits (00ff).
0x in front of a number means...
the number is written in hexadecimal (base 16).
The instruction pointer (IP) tells the CPU...
which instruction it will run next; a breakpoint stops it from advancing past a chosen point.
One C source line compiles to...
usually several machine instructions.
step and next measure progress in units of...
source lines (not single instructions).
-g gives GDB...
a debug-symbol dictionary mapping addresses to variable names and line numbers.
-O0 matters because...
it stops the optimizer from deleting/merging variables, keeping them inspectable.
A stack frame is...
the private memory block holding one function call's local variables.
bt prints...
the stack of frames — who called whom — top (current) to bottom (main).
0xCC (INT3) does what?
raises a trap so the CPU stops and signals GDB (SIGTRAP).
SIGSEGV versus SIGTRAP?
SIGSEGV = illegal memory access (crash); SIGTRAP = a breakpoint/trap fired.
Debug registers DR0–DR3 let the CPU...
watch ~4 addresses in hardware and trap on write, with near-zero overhead.
In , the letters mean...
=lines run, =per-line trap cost, =normal time, =slowdown factor.