skip to content

Under rust-gdb, a Linux service built with `cargo build --release` shows no locals or source lines; why, and what must the binary carry?

level: middleimportance: should knowfreq 30%

answer

  1. what the release profile leaves out
  2. rustc's -C debuginfo levels
  3. line tables versus types and variables
  4. optimised code still drops locals

basics

~20 s

Cargo's release profile emits no debug info by default, so GDB has only symbols. Full debug info (rustc's -C debuginfo=2, Cargo's debug = true) is what shows variables and feeds the printers; line-tables-only gives file and line, no locals.

solid answer

~40 s

A release build defaults to no debug info, so GDB sees only symbol names: it can stop on a function, but shows disassembly, no `file:line` and no locals. The cure is rustc's `-C debuginfo` level, which Cargo's `debug` profile key sets. `line-tables-only` adds file and line for backtraces and line breakpoints but no variables. `limited` (1) still has no type or variable-level information. Only `full` (2, the `-g` alias) records variables and types, and the providers need the types to recognise a `Vec` or a `String`. Even then, optimised code keeps only the values it still needs at that point, so expect some locals to be missing. Keep `-C strip=debuginfo` off for builds you mean to debug.

code

rust · 20 lines
rust
struct Order {
    id: u32,
    qty: u16,
}

#[inline(never)]
fn total_qty(orders: &[Order]) -> u32 {
    let mut sum = 0u32;
    for o in orders {
        sum += o.qty as u32;
    }
    sum
}

fn main() {
    let n: u16 = std::env::args().count() as u16;
    let orders = vec![Order { id: 1, qty: 2 * n }, Order { id: 2, qty: 5 }];
    let t = total_qty(&orders);
    println!("{} {}", t, orders[0].id);
}

go deeper

for a junior

Recall that a release build carries no debug info by default and that a debug build or debug info turned on is what makes variables and source lines appear.

for a middle

Explain the -C debuginfo levels: line-tables-only for file and line, limited without variables, full for variables and the types the printers match.

for a senior

Separate the three failure states quickly: no debug info, debug info stripped or split away, and optimised-out values, and pick the cheapest build that reproduces the bug.

for a principal

Weigh shipping release binaries with full or line-tables-only debug info, or keeping split debug files, against binary size, build time and the ability to debug production crashes.

## Why the release build shows nothing GDB can only describe what the binary tells it. **Debug information** is the part of a compiled program that maps machine addresses to source files and lines, and records each function's parameters and locals with their types. rustc writes it only when asked, through the codegen option `-C debuginfo`. Cargo passes that option from its profile's `debug` key, and the release profile's default is `debug = false`, which is level 0: no debug info at all. A Linux service built with `cargo build --release` therefore reaches GDB with symbol names only. You can still break on a function by name if the symbol table survives, but GDB shows assembly instead of source and has no list of locals to print. The rust-gdb wrapper cannot help here. Its pretty-printers interpret debug info. They do not create it. ## The debug info levels | `-C debuginfo` value | What the binary records | What you get in the debugger | |---|---|---| | `0` or `none` | nothing | function names from symbols, assembly only | | `line-tables-only` | file and line tables | `file:line` in frames and line breakpoints, no variables | | `1` or `limited` | more module-level info, no type or variable-level info | still no locals or arguments | | `2` or `full` (`-g`) | variables, parameters and types | locals, arguments and the Rust printers | The rustc book describes `line-tables-only` as the minimal amount of debug info for backtraces with file and line, with no variable or parameter information. The printers depend on **type** information, because they classify each value by its full type name, such as `alloc::vec::Vec<orders::Order, alloc::alloc::Global>`. So the lowest level that gives you a readable `Vec<Order>` is `full`. ## Optimised code still hides values Full debug info does not undo optimisation. At `opt-level = 3` the compiler keeps values in registers, merges them or deletes them once they are no longer needed. The debug info then reports a variable as unavailable or leaves it out of the frame entirely. In a test service built with `-C opt-level=3 -C debuginfo=full`, a breakpoint in a `#[inline(never)]` function showed the `orders` slice argument with both elements. The running `sum` and the loop variable did not appear at all. ## Getting debug info into a release build 1. Turn on full debug info for the build you deploy or reproduce with. In Cargo that is the `debug` key in the release profile or in a custom profile that inherits from it; the profile settings themselves are Cargo's subject. 2. If you only need readable backtraces and crash locations, `line-tables-only` is a cheaper middle ground, but it will not show variables. 3. Reproduce with the same optimisation level as production when the bug depends on it. Drop to a debug build when you need to see every local. ## Other ways the debug info goes missing - **Stripping.** `-C strip=debuginfo` removes debug info sections at link time. The rustc book says this leaves backtraces mostly intact but may make a debugger like GDB ineffectual. `-C strip=symbols` also removes the rest of the symbol table. - **Split debug info.** With `-C split-debuginfo=packed` or `unpacked` on Linux, the DWARF lives in a `.dwp` file or in `.dwo` files beside the objects. Those files must travel with the binary for GDB to find them. `off`, the ELF default, keeps everything inside the executable. - **Post-processing.** A packaging step that strips binaries undoes all of the above. ## Telling the cases apart quickly - **No source lines and no locals**: the binary has no debug info, or it was stripped. `objdump -h` shows whether any `.debug_*` sections are left before you even start GDB. - **Source lines but no locals**: the build used `line-tables-only` or `limited`. - **Locals present, some unavailable**: full debug info on optimised code. The debug info is fine; the values are gone. - **Locals shown as raw `buf` and `len` fields**: debug info is fine, and the Rust printers did not load. That is a wrapper problem, not a build problem. Getting this right first saves rebuilding a service three times with settings that cannot help.

  • Why is `-C debuginfo=limited` not enough to print a `Vec<Order>` under rust-gdb?
    rustc's own description of level 1 is debug info without type or variable-level information. The debugger has no list of locals to show, and the providers have no type names to match against their patterns. Cargo's documentation says `limited` adds more detailed module-level info than `line-tables-only`, but nothing that lets you inspect values.
  • With full debug info in an optimised build, why does `sum` still not appear?
    Optimisation keeps a value only while it is needed, often in a register, and may fold a loop accumulator into other arithmetic. The debug info can only describe locations that still exist, so the variable is reported as unavailable or omitted. Reproduce in a debug build, or read the value where it is passed to or returned from a non-inlined function.

  • rust-gdb's pretty-printers can recover variables from a release binary without debug info
  • line-tables-only debug info is enough to inspect local variables
  • Full debug info makes every local visible even at opt-level 3
  • Stripping debug info at link time removes the panic backtraces too
  • Raw struct fields and no locals at all are the same problem