Under rust-gdb, why can `print` show `customer` or read `order.qty`, yet reject a `match` expression or a generic call like `largest(&orders)`?
answer
- GDB's own Rust parser
- statement-like expressions unsupported
- no type inference for generics
- turbofish written by hand
- extern for another crate's path
basics
~20 sGDB parses Rust syntax itself and evaluates paths, fields, literals and calls to compiled functions. Its manual lists no if or match, closures or tuples, and no inference for generics, so a generic call needs explicit parameters such as largest::<u32>.
solid answer
~40 sGDB has its own Rust language mode: it parses Rust-syntax expressions and prints types and values from the debug info, so `print customer` and `print order.qty` work. It is an evaluator, not a compiler. GDB's manual lists the holes: no statement-like expressions such as `if` or `match`, no closures, no tuple expressions, `Drop` never runs on values it creates, and no type inference for generics, so you write the parameters yourself, as in `largest::<u32>(...)`. Only code that exists in the binary can be called. The manual also lists gaps it traced to rustc's debug info when written, as of rustc 1.8: no trait method calls, no operator overloading, no `Self`. Name lookup prepends the current crate, so `extern other::NAME` reaches another crate.
go deeper
Recall that GDB understands Rust syntax for printing variables and fields, but it is not a Rust compiler.
Explain which expressions work, such as paths, fields, literals and calls to compiled functions, and which do not, such as match, closures and tuples.
Explain why generic calls need explicit type parameters and an emitted instantiation, why trait calls fail, and how extern reaches another crate's items.
Weigh building small non-generic inspection helpers into debug builds, so on-call engineers can query state from GDB, against the extra code paths and their maintenance.
## GDB's Rust mode GDB supports several source languages and switches to its **Rust mode** when the frame it stops in comes from Rust code, which the debug info records. In that mode it parses expressions with Rust syntax and prints types and values the Rust way. The manual describes type and value printing, and expression parsing, as reasonably complete, with a few peculiarities and holes. The rust-gdb wrapper does not change any of this. Its Python printers only change how values are **displayed**, not what `print` can **evaluate**. ## What print handles well - **Variables and paths**: `print customer`, `print orders`, a static by its path. - **Field access** on structs and through references: `print order.qty`. - **Literals and arithmetic** on values you can read: comparing a length or adding two quantities. - **Calls to functions that exist as compiled code**. GDB runs code that is already in the binary; it cannot compile a new instantiation of a generic function, and a function that was inlined everywhere or never used may not exist at all. ## What the evaluator rejects The GDB manual's Rust section lists these limits: | Expression | Status in GDB's Rust mode | |---|---| | `if`, `match` and other statement-like expressions | not supported | | closures (lambda expressions) | not supported | | tuple expressions such as `(a, b)` | not implemented | | a generic call without type parameters | no type inference; write `f::<T>` | | values the evaluator creates | `Drop` is never run on them | A second list in the manual names holes in the debug information rustc emitted when that section was written, which it dates to rustc 1.8. Method calls through traits, operator overloading, generic type names inside a monomorphised function, `Self` and names brought in by `use` are all unavailable. Treat that list at the strength the manual states it, as a note from that time, but expect those forms to fail first. Two of these trip people up most: 1. **`match` on an enum.** You cannot write `print match customer { Some(c) => c.name, None => ... }`. Print `customer` instead; GDB's Rust mode shows the variant and its fields, and you read the field from there. 2. **Generic calls.** For `fn largest<T: PartialOrd>(items: &[T]) -> &T`, GDB will not infer `T` from `&orders`. Write the parameters with the turbofish, and only for an instantiation rustc actually emitted. Tab completion is no help here: the manual notes it offers `crate::f<u32>`, which the parser rejects, because Rust requires `::` before the generic arguments. ## Why the limits exist Each gap follows from evaluating against a compiled program: - **Generics** exist only as the monomorphised copies rustc emitted. There is no generic code left to instantiate. - **Macros** such as `vec!` or `format!` were expanded before code generation. Nothing of them is left for the evaluator. - **Trait dispatch** is resolved by the compiler. The manual traces the trait-call gap to what rustc's debug info described. - **Drop** calls are code the compiler inserts at the end of a scope. The manual says the evaluator does not implement `Drop`, so values it builds are never destroyed. ## Name lookup across crates GDB implements Rust name lookup, so it sometimes **prepends the current crate**. Stopped in crate `billing`, `print ::rates::TAX_RATE` makes GDB look for `billing::rates::TAX_RATE`. To reach crate `rates`, use GDB's `extern` extension: `print extern rates::TAX_RATE`. Breakpoint locations follow a related rule. A linespec is never relative to the current crate, so `break B::f` means function `f` in a crate named `B`, and linespecs cannot use `self::` or `super::`. ## Working around the gaps - Read values rather than recomputing them. Let the printers show a `Vec` or `HashMap` instead of calling its methods. - Call a non-generic helper that is compiled into the binary when you really need computation, such as a `#[inline(never)]` debug function in a test build. - For conditions, compare fields and literals rather than calling trait methods such as `eq`. What these rules tell an interviewer is that a candidate knows the debugger evaluates expressions against the compiled program. It does not run the Rust compiler. That is why generics, traits and expression blocks are exactly where it breaks.
- Stopped in crate `billing`, why does `print ::rates::TAX_RATE` fail to find crate `rates`' static?GDB applies Rust name-lookup rules and sometimes prepends the current crate, so it looks for `billing::rates::TAX_RATE`. GDB adds an `extern` extension for exactly this case: `print extern rates::TAX_RATE` refers to the otherwise unreachable global scope where crate `rates` lives.
- If the printers make a `Vec` look like a list, why can't you call `orders.len()` reliably?Printers only change how GDB displays a value; they add nothing to the evaluator. A method call needs compiled code for that exact instantiation in the binary. `Vec::<Order>::len` is generic and usually inlined, so it may not exist. Read the length the printer shows, or the raw `len` field, instead.
- rust-gdb's printers let print evaluate any Rust expression
- GDB compiles a generic function on demand when you call it
- A match or if expression works in print like it does in code
- print ::other::NAME always starts from the global root of all crates
- Values GDB creates are dropped like values in the program