Full name: Debugging With Attributed Record Formats.
Definition
Debug information is data that a compiler writes beside the machine code so that a debugger, a program that stops and inspects another program, can show that code in terms of the source. It records the source line of each instruction, the address range of each function, the name, type and location of each variable, and how to find the state of the calling function. DWARF is the standard format for debug information in ELF §12.6 (Executable and Linking Format) files. The DWARF Debugging Information Format Committee maintains it.
The idea
A processor runs instructions at numeric addresses, and its registers (the small storage cells inside the processor) and memory hold plain numbers. A programmer examining a crash wants other facts: the failing instruction belongs to line 21 of geo.c, inside the function dist2, and the variable d holds 25. Only the compiler knew these facts, while it translated the source. Debug information is the compiler’s written record of them, which a debugger reads to translate from an address to a source line and from a variable’s name to the register or memory cell that holds its value.
The index at the back of a book is a fair picture of the address-to-line part. One topic can appear on several pages far apart, just as one source line can own several separate stretches of machine code, because the compiler is free to copy and reorder the instructions it generates. The picture stops at time: an index says only where text is, while a debugger also needs to know where a variable is at each moment, and that place can change from one instruction to the next.
How it works
The name DWARF is a pun on ELF, the file format it was developed with; Brian Russell, its original developer, gave it the expansion Debugging With Attributed Record Formats. DWARF data lives in sections of the ELF file whose names begin with .debug_. These sections are not marked to be loaded, so they make the file larger but add nothing to the memory image that runs on the device. The main parts are these.
Debugging information entries, in
.debug_info: a tree of records, one for each program element. Each record has a tag that says what it describes, such asDW_TAG_subprogramfor a function orDW_TAG_variablefor a variable, and attributes such as its name, its type and the address ranges of its code.The line table, in
.debug_line: rows that pair an instruction address with a source position (file, line and column) and flags. The flagis_stmtmarks addresses that are good places to stop at the start of a statement.Location descriptions: for each variable, where its value is (a register, a place on the stack, or a constant) and for which addresses.
Call frame information, in
.debug_frame: for every code address, rules that say where the return address (the address at which the calling function continues) and the calling function’s saved registers are. With them a debugger can unwind the stack (the memory area in which active function calls keep their data): it rebuilds the chain of active calls that a backtrace shows.
To save space, DWARF stores the line table as a small program whose operations advance the address, change the line, or emit a row. A row applies from its own address up to the address of the next row. A final row flagged end_sequence carries the first address after a run of contiguous code. Line number 0 means that the instruction cannot be attributed to any source line.
A debugger uses these parts as follows.
To show where the program stopped, it reads the program counter §2.9, the register that holds the address of the current instruction, and shows the file and line of the row that covers that address.
To name the function, it finds the entry whose address ranges contain the address. When the compiler copied a function into its caller (inlining), a
DW_TAG_inlined_subroutineentry names the copied function and records the line of the call, so the debugger can show the copy as if it had been called.To print a variable, it follows the variable’s type to learn its size and layout, and evaluates its location description at the current address. If no location covers that address, the debugger reports the variable as optimized out.
To stop at a line, it plants a breakpoint §14.29, a marker that halts the program when execution reaches an address, at the addresses of that line’s rows that carry
is_stmt.To show the chain of callers, it applies the call frame rules repeatedly.
Worked example
Take this C source file geo.c, with its line numbers on the left. A struct pt holds two 4-byte integers x and y.
10 static int sq(int v) {
11 return v * v;
12 }
20 int dist2(const struct pt *p) {
21 int d = sq(p->x) + sq(p->y);
22 return d;
23 }
With optimization on, the compiler inlines sq twice: it copies line 11 into dist2 once for each call. Suppose every instruction is 4 bytes long and dist2 starts at address 0x0400. The compiler emits these instructions, written in a generic assembly notation, with one line-table row each:
| Address | Instruction | Line |
|---|---|---|
0x0400 |
load r1, [p+0] |
21 |
0x0404 |
mul r1, r1, r1 |
11 |
0x0408 |
load r2, [p+4] |
21 |
0x040C |
mul r2, r2, r2 |
11 |
0x0410 |
add r0, r1, r2 |
21 |
0x0414 |
ret |
22 |
0x0418 |
(end of sequence) |
Each row covers the addresses from its own address up to the next row’s address, here bytes, one instruction. Line 21 owns three separate ranges: the 4 bytes from 0x0400, from 0x0408 and from 0x0410, bytes in all. Line 11 owns two ranges, from 0x0404 and from 0x040C, one for each inlined copy. Line 22 owns the 4 bytes from 0x0414. Check: bytes, and . If the compiler instead started both loads first, to hide the wait for memory, line 21 would own the 8 bytes from 0x0400 and the 4 bytes from 0x0410: still two pieces, now because of reordering.
What a debugger shows follows from the table.
Stepping one instruction at a time, the displayed line goes 21, 11, 21, 11, 21, 22, although the processor runs straight through.
A breakpoint on line 11 needs a location in each copy, at
0x0404and0x040C, and stops twice in every call. A breakpoint on line 21 is normally planted at0x0400; when it stops, the addition on the same line has not run yet.The variable
dexists only after the addition: its location description says registerr0for the addresses from0x0414to0x0417. Stopped at0x0408, the debugger reportsdas optimized out.
Stated precisely
Consider one sequence of the line table: rows , where is an instruction address and a source position, with (within a sequence the address can only stay or grow), followed by the end address , the first address after the sequence. Row covers the addresses with . The set of addresses attributed to source line is
Each row is the compiler’s statement of which source position the instruction at that address was generated from. The format promises nothing more: not that is one range, not that its ranges run in address order or once per execution of the line, and not that each instruction came from only one line. When the compiler merges the work of two lines into one instruction, the table can name only one of them, or line 0.
Reading the table correctly needs three conditions: the debug information comes from the same compilation and link as the machine code being examined; nothing modified that machine code afterwards; and the compiler recorded its transformations correctly. A debugger cannot check the third, and a failure of the first is silent: the debugger shows wrong lines and values, not an error. A build ID, an identifier that the linker §12.4 can compute from the program’s contents and store in a note section of the program file, and that a separate debug file keeps, lets tools match the two files.
Limits and common mistakes
Debug information tells where code came from, not the order in which source statements run. Under optimization a line can be reached several times or never, so the number of hits of a line breakpoint is not the number of times the line ran.
An unoptimized build (-O0) is a different program from the optimized one, and a bug can appear in only one of them. The option that adds debug information, -g, is meant not to change the generated code, and GCC §12.17 (GNU Compiler Collection; GNU stands for GNU’s Not Unix) can check this with -fcompare-debug. GCC’s level -Og applies only optimizations that keep debugging accurate.
Debug information gives anyone who obtains the file a map of the code: function and variable names, types and source paths. Shipped files are commonly stripped of it, with a separate copy, matched by build ID, kept for analyzing crash reports. The debug sections are often larger than the code, but they are not loaded, so a measurement of memory use counts only the loaded sections.
A breakpoint halts the processor and changes timing, so a debugging session says nothing about timing without the debugger.
DWARF is not the only format: Microsoft’s compilers write debug information into PDB (Program Database) files, and older Unix systems used a format called stabs.
Questions answered
Why can one source line map to many non-contiguous instruction ranges?
One source line can map to many separate instruction ranges because debug information records, for each instruction address, the line that the instruction was generated from, and the compiler may place a line’s instructions wherever the meaning of the program allows. In the DWARF (Debugging With Attributed Record Formats) format, the line table is a list of rows, each pairing an address with a line and covering the addresses up to the next row, so the addresses of one line form a union of ranges. The usual causes are these.
Inlining: the compiler copies a called function’s body into each caller, so the function’s lines appear once for every call site.
Instruction scheduling: the compiler reorders instructions, for example to start a slow memory read early, which interleaves neighboring lines.
Loop transformations: unrolling repeats a loop body, and moving unchanging work out of a loop places part of a line before the loop.
Several parts in one line: the line
for (i = 0; i < n; i++)produces its start, its test and its increment in different places, even without optimization.Duplication and splitting: the compiler may copy a block into several paths, or move rarely run code to a separate section.
As a result, a breakpoint on such a line may need several locations, stepping appears to jump back and forth, and stopping at the first range of a line does not mean that the rest of the line has run.
Related entries: Symbol table (§12.8), Link-time optimization (LTO) (§12.10), GDB (§6.28), ELF (§12.6), Compiler optimization (§3.39), Breakpoint and watchpoint (§14.29). Further reading: [111].