Definition
GDB is the debugger of the GNU Project: a program that controls another running program, stops it at chosen points, and shows and changes its registers, memory and variables in terms of the source code. GDB can debug a program on the same computer or on a remote target such as a microcontroller §6.1. For a microcontroller, GDB talks to a debug server, a host program such as OpenOCD §6.29 (Open On-Chip Debugger) or the J-Link §6.27 GDB Server, which carries out GDB’s requests through a debug probe connected to the chip.
The idea
A processor executes machine instructions at numeric addresses and keeps values in registers. A programmer thinks in functions, source lines and named variables. When firmware misbehaves, the programmer wants to ask a question such as “what is the value of len when parse_packet reaches line 42?”. GDB answers such questions by translation. The compiler, when asked, writes debug information §12.9 into the program file: tables that map each source line to the addresses of its machine code, and each variable to the register or memory location that holds it. GDB reads these tables and turns each source-level request into actions on addresses of the target.
The main tool is the breakpoint §14.29: a mark at an instruction address that stops the program when it reaches that address, so that its state can be examined. Stopping the program is the point, and also the catch. A paused film can be studied frame by frame, and nothing in the film moves on. A device is different: its processor stops, while the rest of the world, such as timers, radios and the devices at the other end of a connection, keeps going. A breakpoint gives an exact picture of the program’s state at one instant, and it changes how the program relates to time.
How it works
Debugging a microcontroller with GDB takes these steps.
Build the program with debug information, using the compiler option §12.3
-g. The result is an ELF §12.6 (Executable and Linking Format) file that holds the machine code and the debug information, written in the standard debug-information format named DWARF.Start a debug server connected to the probe. OpenOCD listens for GDB on TCP (Transmission Control Protocol) port 3333 by default, and the J-Link GDB Server on port 2331.
Start GDB with the same ELF file, for example
arm-none-eabi-gdb app.elf, and connect it withtarget extended-remote localhost:3333.If the target does not yet hold the program, write it with GDB’s
loadcommand, which sends the ELF file’s loadable sections to the server for writing into flash memory §6.6.
GDB and the server exchange short text messages defined by the GDB Remote Serial Protocol. A message reads the registers, reads or writes a range of memory, inserts or removes a breakpoint, or resumes or steps the target. The server turns each message into probe operations. GDB’s monitor command passes a line of text to the server unchanged; with OpenOCD, monitor reset halt resets and halts the target.
A breakpoint is made in one of two ways; the breakpoint and watchpoint entry treats both in full.
A software breakpoint replaces the instruction at the address with a breakpoint instruction,
BKPTon Arm processors. This needs code that can be rewritten like ordinary memory, such as code in RAM.A hardware breakpoint is a comparator in the debug logic that compares the address of each instruction with a stored address. It leaves the code unchanged, so it works for code in flash memory, but there are few of them: the Flash Patch and Breakpoint unit of an Arm Cortex-M4 §6.3 has at most six for instruction addresses. GDB uses hardware breakpoints automatically at addresses that the server’s memory map marks as flash memory.
When the processor reaches a breakpoint, the debug logic halts it: the processor enters a debug state and executes nothing more. The server notices the halt and tells GDB, which reads the registers and shows the source line. The command continue resumes the processor; next runs to the next source line, stepping over function calls; step does the same but enters called functions; and stepi executes one machine instruction.
Arm Cortex-M3, Cortex-M4 and Cortex-M7 processors, among others, also offer monitor-mode debugging. A breakpoint then raises an exception, a hardware-triggered call to a handler routine, instead of halting the processor. The handler waits for the debugger at a priority chosen by the developer, and interrupts §6.11 (hardware requests that make the processor run a handler routine) of higher priority, such as those of a radio, are still served. The J-Link software supports this mode. It reduces the disturbance a breakpoint causes but does not remove it, because the code that was stopped still misses its own deadlines.
Worked example
A device holds a Bluetooth Low Energy (BLE) §7.1 connection. The two devices meet in a short radio exchange, a connection event, every 30 ms; this is the connection interval §7.7. The connection’s supervision timeout §7.8 is 720 ms: a device that receives no valid packet from its peer for 720 ms declares the connection lost. A developer sets a breakpoint in the code that handles a received packet and, when the processor stops there, studies the variables for 10 seconds.
The processor is halted, so the device’s radio software sends nothing. The peer reaches its timeout §9.34 after connection events without a valid packet, that is, 720 ms into the pause. The developer’s 10 seconds are times the timeout. When the developer types continue, the connection no longer exists. Some radio software also stops with an error as soon as it resumes, because its schedule of radio events has been broken. The variables that GDB showed were correct; the program’s relation to time was not.
Timing measurements suffer in the same way. A function runs for 1 ms on a 64 MHz (megahertz) processor, which is clock cycles. A breakpoint inside the function holds the processor for 10 s. The DWT (Data Watchpoint and Trace) cycle counter, a counter in the processor’s debug logic that counts clock cycles, does not count while the processor is halted, so it reports about 64,000 cycles, or 1 ms. A real-time clock §6.19 that runs at 32,768 ticks per second keeps counting during the halt and reports about ticks, or 10.001 s. Both numbers are right, for different questions: the first measures the processor’s work, the second the time that passed for the outside world.
Stated precisely
What GDB shows is correct only if a chain of correspondences holds.
The ELF file was built from the source code that GDB displays.
The debug information in that file describes that machine code. After compiler optimization §3.39, the changes a compiler makes to produce faster or smaller code, a variable may live in a register for only part of a function, and GDB then reports it as optimized out.
The target runs exactly the machine code in that ELF file. GDB’s
compare-sectionscommand checks this by comparing the file’s sections with the target’s memory.
A break anywhere in the chain gives a display that looks plausible and is wrong.
For timing, let be the time the processor spends executing an operation, the total time the debugger holds the processor halted during that operation, and a deadline that a clock outside the processor measures from the start of the operation. The outside world sees the operation take , so the operation misses whenever , however small is. A counter that stops while the processor is halted reports only .
Limits and common mistakes
A breakpoint does not preserve real-time behavior. Every deadline measured by a clock outside the processor keeps running during the halt: a radio schedule, a watchdog timer §6.16, a peer’s timeout, a control loop. To observe timing without stopping the program, use methods that let it run: a log written to a buffer in RAM and read later, Real-Time Transfer (a method of SEGGER, the maker of J-Link probes, in which the probe reads such a buffer while the program runs), hardware trace (a stream of records of executed instructions that the chip sends out while it runs), a cycle count that the program reads itself, or a pin that the program toggles and a logic analyzer, an instrument that records digital signals over time, captures.
Optimized code does not follow the source line by line. The compiler may copy a function into its callers, reorder statements, or keep a variable only in a register, so stepping jumps around and some variables cannot be shown. Building with -Og, which keeps only optimizations that do not hinder debugging, helps, but then the build being debugged differs from the one that ships.
Hardware breakpoints are few. When more are requested than the debug logic has, GDB reports an error at the moment it tries to insert them, which is when the program resumes. A software breakpoint in flash memory is not possible without rewriting flash memory.
Stepping interacts with interrupts. A step may enter an interrupt handler §6.12, or the debugger may mask interrupts during the step, which changes the program’s behavior.
GDB shows state; it does not prove anything. A correct value at one breakpoint says nothing about other inputs or other paths through the program.
Questions answered
Does a breakpoint preserve real-time behavior or radio deadlines?
No: a breakpoint halts the processor, and nothing outside the processor waits for it. While the processor is halted, the program runs no code, but every clock outside the processor keeps counting: the supervision timeout of a Bluetooth Low Energy peer, after which the peer drops a connection that has carried no valid packet; the radio’s schedule of transmissions; a watchdog timer that resets the chip; and timers that drive outputs. A pause of a few seconds at a breakpoint therefore makes the device miss every deadline shorter than the pause. A connection with a 720 ms supervision timeout, for example, is lost long before a developer has finished reading the variables.
Measurements are distorted too. A counter that stops during the halt, such as the cycle counter in an Arm processor’s debug logic, shows only the processor’s working time, while the outside world also experienced the pause. Breakpoints are the right tool for examining the program’s state at one instant. To observe real-time behavior, use methods that let the program run, such as a log in RAM, a probe that reads that log while the program runs, hardware trace, or pins toggled by the program. Monitor-mode debugging on Cortex-M processors lets interrupts of higher priority continue, but the code under examination is still stopped.
Related entries: SWD (§6.25), Breakpoint and watchpoint (§14.29), Compiler correctness (§3.40), Machine-code proof boundary (§4.48), Debug information (DWARF) (§12.9), DWT cycle counter (§6.20), Supervision timeout (§7.8), J-Link (§6.27), OpenOCD (§6.29). Further reading: [96].