Section 12.9

Debug information (DWARF)

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.

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.

  1. 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.

  2. 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_subroutine entry names the copied function and records the line of the call, so the debugger can show the copy as if it had been called.

  3. 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.

  4. 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.

  5. 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 𝟶𝚡𝟶𝟺𝟶𝟺−𝟶𝚡𝟶𝟺𝟶𝟶=4\mathtt{0x0404} - \mathtt{0x0400} = 4 bytes, one instruction. Line 21 owns three separate ranges: the 4 bytes from 0x0400, from 0x0408 and from 0x0410, 3×4=123 \times 4 = 12 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: 12+8+4=2412 + 8 + 4 = 24 bytes, and 𝟶𝚡𝟶𝟺𝟷𝟾−𝟶𝚡𝟶𝟺𝟶𝟶=𝟶𝚡𝟷𝟾=24\mathtt{0x0418} - \mathtt{0x0400} = \mathtt{0x18} = 24. 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.

Stated precisely

Consider one sequence of the line table: rows (a1,ℓ1),…,(an,ℓn)(a_1, \ell_1), \dots, (a_n, \ell_n), where aka_k is an instruction address and ℓk\ell_k a source position, with a1≤a2≤…≤ana_1 \le a_2 \le \dots \le a_n (within a sequence the address can only stay or grow), followed by the end address an+1a_{n+1}, the first address after the sequence. Row kk covers the addresses xx with ak≤x<ak+1a_k \le x < a_{k+1}. The set of addresses attributed to source line ℓ\ell is A(ℓ)=⋃k:ℓk=ℓ{x:ak≤x<ak+1}.A(\ell) = \bigcup_{k \,:\, \ell_k = \ell} \{\, x : a_k \le x < a_{k+1} \,\}.

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 A(ℓ)A(\ell) 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.

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].