Repository navigation
Conversation
GCC and clang type a MIPS object's .debug_* sections SHT_MIPS_DWARF (0x7000001e), not SHT_PROGBITS, and embld carried only PROGBITS into the image. So a clang-built MIPS program linked by embld had no debug information at all: gdb said "No symbol table is loaded". GNU ld keeps them, and so does embld now. The merged sections are written as PROGBITS, as before.
Every local was a child of its function's DIE, so `for (int i ...)` twice was two variables named i at one level. gdb printed the first one's i in the second loop, even at -O0, where it read that loop's own stale slot. An inner block's `s` hid the function's `s` everywhere. Against clang's -O0 build of the same program, EmbCC's -O0 showed 162 wrong or out-of-scope values on a Cortex-M3. A local declared in a block, or in a for statement's first clause, now lives in a DW_TAG_lexical_block over its block's code. irgen gives each such block a scope (ir_func.scope_up) and stamps it on every instruction it makes there (ir_ins.scope). An instruction carries its scope through every pass as it carries its line. The inliner gives spliced code the call's scope, and an instruction a pass invents reads 0, the function's. The DWARF writer takes each block's extent from its instructions' ins_pc, which the backend now records under -g whenever a function has such a block. A block the optimizer left with no code gets no DIE, nor do its variables. The scope cannot be irgen's var_scope_lo/hi: mem2reg and sccp renumber instructions and drop those, so at -O2 every block was gone. The for statement's first-clause locals also get the for statement as their var_scope, as a block's locals have their block (C11 6.8.5p5). Two sibling loops' counters may then share a stack slot. Every parameter and variable, globals included, now has DW_AT_decl_file and DW_AT_decl_line, as GCC and clang write them. Three goldens read a fixed number of llvm-dwarfdump lines after a variable's name, and now read two more.
tests/golden/debug-optimized.sh runs one program on QEMU's Cortex-M3, Cortex-M0 and RV32 boards under gdb. The program has loops, calls, a struct, a 64-bit value, a search, and names declared again in sibling and inner blocks. gdb stops on every line and reads every parameter and local in scope (debug-optimized/trace.py). The program is built by EmbCC at -O0, -O2 and -Os, and by clang at -O0. Every value EmbCC's DWARF gives must be the one clang's build shows at the same visit of the same line (debug-optimized/cmp.py). A location list that names the wrong register, a stale value, or a variable shown outside its block fails. An optimized-out value counts as unavailable, not wrong. The reference is clang because EmbCC can be wrong the same way at every level: before lexical blocks, EmbCC's -O0 and -O2 agreed with each other and both showed the first loop's i in the second loop. That build fails this test (162 values at -O0); this branch has none wrong, with 82-83% of values available at -O2 and -Os on all three boards. Not MIPS yet: gdb maps no address of clang's MIPS image to a line, so there is no reference there. CI installs gdb-multiarch, so this and debug-live.sh run on every pull request instead of skipping.
A call the inliner took left no trace in the DWARF. Its code was the caller's, its parameters and locals were gone, and gdb's backtrace in it named only the caller, at the callee's lines. The inliner now makes the spliced code a scope of the caller's: the inlined function, inside the call's scope, with the call's line and column (ir_func.scope_fn, scope_cline, scope_ccol). The callee's own scopes are renumbered under it, its blocks and the calls inlined into it before. The callee's parameters and locals become caller variables in those scopes. They are the caller's slots V + k by then, so the caller's mem2reg follows them, and they get location lists like any local. Each one records its origin, the callee's variable. The DWARF writer emits one abstract instance per inlined function, a DW_TAG_subprogram with DW_AT_inline and its parameters and locals without locations, ahead of the subprograms as the types are. It is built from the copies, because a static function inlined everywhere has no IR function left. Each inlined call is a DW_TAG_inlined_subroutine: the abstract instance as its origin, its code's extent, the call's file, line and column. Its variables refer to their abstract counterparts and say where they are in that copy. gdb shows the call as a frame of its own (`main > clamp`), with clamp's v, lo, hi and r.
main is called once, from a reset handler or crt0, so inlining it saves a call and a return at most. Inlined, it is an inline frame of its caller. gdb's backtrace at -O2 then went past the entry function into 0x0 (debug-frame.sh on RV32 and RV64, once inlined calls had DIEs), and `break main` stopped in the reset handler's code. GCC keeps main out of line too. The remark says callee-is-main.
A stop was named by its line and its visit of that line in the whole run, so any line visited a different number of times in the two builds was not compared anywhere: a rotated loop's test, but also every line of a function called more often from a loop rotated differently. A third of the stops went unchecked. trace.py now records each stop's call stack, and which frames gdb shows as inlined. cmp.py names a stop by its call (which call of each function on the stack), its line and its visit within that call, and skips only the lines whose visit count differs within one call: 77 to 98% of stops are compared, from 69%. A call EmbCC inlined cannot be told from the next call of the same function when no stop comes between them. There, a value must be one clang's build shows at that line in a call from the same caller invocation. prog.c gains clamp(), small enough to inline at -O2, called in a loop. When the inliner maps each inlined variable to the next slot, the test fails on all three boards (5 to 10 values).
A lexical block or an inlined call spanned the first to the last byte of its code. Where the optimizer interleaves other code with it, such as a rotated loop's test or the next unrolled call, that span covers code that is not the block's. A debugger stopped there sees the block's variables, or an inlined frame that is not running. Each scope's code is now collected as its ranges, touching ones merged. A scope in one piece keeps DW_AT_low_pc and DW_AT_high_pc. One in several gets DW_AT_ranges into .debug_rnglists (DWARF 5: start_end entries) or .debug_ranges (DWARF 4: a base address selection entry of 0 and address pairs), each address relocated. The lists follow the unit's own list, whose size is known before any DIE is written, so a block's offset is known as its DIE is. A .dwo keeps the extent. The libc's format.c, tz.c and regex.c at -O2 have 26 to 31 such blocks each. llvm-dwarfdump --verify passes for DWARF 4 and 5, with and without -ffunction-sections.
collect_locals recorded each block's scope in a table that gen_stmt searched by pointer, and grew scope_up one element at a time. The 200,000 one-declaration blocks of many-declarations.sh's typedefs.c made both quadratic: the file no longer compiled in the test's 120 seconds (CI on #197). The scope is now a field of the statement (dbg_scope), and scope_up doubles. The DWARF writer also built each scope's children and variables by scanning all of them for every scope. It now builds the lists once. The file compiles in 8 seconds again.
debug-live.sh used to skip on CI for want of gdb, and now that CI has
gdb-multiarch it failed linking: the x86-64 QEMU harness needs newlib,
which a CI runner has not. It now skips for that, by name.
debug-optimized.sh:
- Where the reference's own gdb cannot read a value, it is not
compared. CI's older gdb cannot read clang's RV32 frame at main's
last stop ("Cannot access memory").
- crc8 runs over 6 bytes, not 21: its inner loop was two thirds of the
stops, and the test took 297 s on a CI runner. It now takes 49 s
here, with 366 stops per board.
- MIPS is back, with clang's reference at DWARF 4. gdb maps no address
of a linked MIPS image whose unit starts at a DWARF 5 .debug_addr
entry, so clang's DWARF 5 image had no lines to it. With DWARF 4 all
three levels agree, with 72% of values available at -O2.
teo1747
marked this pull request as ready for review
November 9, 2026 00:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Debugging optimized code. This is the start of the 1.0 debugging promise: "
btandinfo localsare right at every stop of the GDB test on every board family, at-O2."Location lists already existed. They give 82–83% of values at
-O2on Cortex-M3, M0 and RV32 (llvm-dwarfdump --statisticsputs their libc coverage near clang's). What was wrong was scope.for (int i …)loop gdb printed the first loop'si, even at-O0.forclauses now live inDW_TAG_lexical_blocks.mem2regandsccp, so at-O2every block was gone.DW_AT_decl_file/DW_AT_decl_line.SHT_MIPS_DWARFsections. A clang- or GCC-built MIPS object's debug info was silently dropped from the image.debug-optimized.sh.-O0,-O2and-Os, must match clang's-O0build at the same visit of the same line.main's compiler fails it with 162 wrong or out-of-scope values at-O0; this branch has none.gdb-multiarch, so this test anddebug-live.shrun on every pull request.Still open:
DW_TAG_inlined_subroutine.