skip to content

Under rust-lldb, why does `p name.len()` fail on a Rust `String` while `p name` prints "katana", and what do you use instead?

level: middleimportance: should knowfreq 28%

answer

  1. two different parsers
  2. dwim-print picks a path
  3. falls back to another language
  4. frame variable walks fields

basics

~20 s

LLDB has no Rust expression parser: name.len() goes to the expression evaluator, which runs it as Objective C++ and finds no member len. p name is a plain variable path, printed through the formatters. Read fields with frame variable instead.

solid answer

~50 s

In LLDB, `p` is an abbreviation for `dwim-print --`. A bare variable such as `name` is printed the way `frame variable` would print it, so the Rust formatters show `"katana"`. Anything else goes to the expression evaluator, which cannot evaluate Rust; a failing expression carries the note `Expression evaluation in Rust not supported. Falling back to default language. Ran expression as 'Objective C++'.` To C++, `alloc::string::String` is a struct with a `vec` field and no `len` member, so method calls, trait methods, generic calls and macros like `vec![]` all fail. Arithmetic on scalar locals still works. For everything else use `frame variable` paths over the real or synthetic children: `frame variable name.vec.len`, `frame variable v[1]`, `frame variable hits[0]`. When you need real Rust logic, such as a hashed lookup, compute it in the program and rebuild.

code

plaintext · 12 lines
plaintext
(lldb) p name.len()
              ˄
              ╰─ error: no member named 'len' in 'alloc::string::String'
note: Expression evaluation in Rust not supported. Falling back to default language. Ran expression as 'Objective C++'.
(lldb) p t
(unsigned int) 1105
(lldb) expression t + 1
(unsigned int) $0 = 1106
(lldb) frame variable name.vec.len
(unsigned long) name.vec.len = 6
(lldb) frame variable hits[0]
((alloc::string::String, alloc::vec::Vec<u32, alloc::alloc::Global>)) hits[0] = ("POST /login", size=1)

go deeper

for a junior

Remember that LLDB cannot run Rust method calls; print variables and their fields instead of calling methods on them.

for a middle

Explain dwim-print: variable paths go through frame variable and the formatters, while everything else is compiled as Objective C++ and fails on Rust methods, generics and macros.

for a senior

Show the workaround habits: frame variable paths over synthetic children, -R for the raw struct, and moving real Rust logic into the program when inspection is not enough.

for a principal

Note that paths into std's private fields drift with toolchain updates, so lasting checks belong in code and logs, not in shared debugger scripts.

## Two ways LLDB turns text into a value LLDB has two separate paths from what you type to what it prints, and `p` chooses between them. - **`frame variable`** (`fr v`, or just `v`) looks up locals and arguments in the debug info and walks a path through them. LLDB's tutorial calls it "not a full expression parser", supporting only simple operators such as `&`, `*`, `->` and `[]`. Because the Rust formatters supply synthetic children, `[]` indexes those children: `frame variable v[1]` is the second `Vec` element. - **`expression`** (`expr`) parses the text as source code in some language, compiles it, and can run it inside the stopped process. - **`p`** is, in LLDB's own help, "an abbreviation for 'dwim-print --'". It prints a plain variable the way `frame variable` does and passes anything else to the expression evaluator. So `p name` and `p name.len()` take different paths even though they look alike. ## What happens to Rust text in the expression evaluator LLDB has no Rust expression parser. When an expression in a Rust frame fails, LLDB adds this note: `note: Expression evaluation in Rust not supported. Falling back to default language. Ran expression as 'Objective C++'.` Everything follows from the text being compiled as C++ against the types the debug info describes: 1. **Method calls fail.** `name.len()` gives `error: no member named 'len' in 'alloc::string::String'`; `user.is_some()` on an `Option<String>` fails the same way. Inherent and trait methods are not members of the struct the debug info describes. 2. **Rust-syntax function calls fail.** `expression total(&hits)` gives `error: reference to 'total' is ambiguous`, and `main::total(&hits)` is read as a C++ declaration of a reference variable. 3. **Macros fail.** `expression vec![1]` gives `error: unexpected namespace name 'vec': expected expression`. 4. **Generics and traits are out of reach in Rust syntax.** A generic function exists in the binary only as the concrete copies the compiler generated, and the C++ parser knows nothing of trait resolution. 5. **Scalar arithmetic works.** `expression t + 1` on a `u32` local prints `(unsigned int) $0 = 1106`, because C++ understands integer addition. `expression name` also works and prints through the formatters, since it only names a variable. GDB, by contrast, has its own Rust expression parser; that belongs to the rust-gdb lesson. ## Getting the answer without calling anything | You want | Command | Output in the session | |---|---|---| | A `String`'s byte length | `frame variable name.vec.len` | `(unsigned long) name.vec.len = 6` | | One `Vec` element | `frame variable v[1]` | `(unsigned int) v[1] = 20` | | One `HashMap` entry | `frame variable hits[0]` | `("POST /login", size=1)` | | An `Option`'s payload | `p user` | `Some("ana")` | | The unformatted struct | `frame variable -R name` | `vec`, `buf`, `inner`, `cap`, `len` | Points to keep in mind when you use these paths: - **Field paths follow std's private layout.** `String` holding a `vec` with a `len` is today's layout, not an API. The providers themselves carry branches for older layouts; their `Vec` docstring records the `RawVec` changes in Rust 1.75 and 1.76. - **`hits[0]` is the table's first occupied bucket, not the first key inserted.** The synthetic children walk the hash table in bucket order. - **For real Rust logic, change the program.** A `HashMap::get` needs hashing, and a trait method needs dispatch. Add a temporary `eprintln!` or `dbg!`, rebuild and rerun; the debugger cannot run that code for you. ## A working habit for Rust sessions 1. Start with `frame variable` (or `v`) to see every local through the formatters. 2. Drill in with paths and `[]`: fields by name, elements and entries by synthetic index. 3. Switch to `frame variable -R` when a display looks wrong, to see what is stored. 4. Keep `expression` for what C++ can say about plain data: integer arithmetic, comparisons, a pointer cast. 5. For anything that needs Rust semantics, change the program, rebuild and rerun. ## Why interviewers ask this The question separates people who have only read about debugging Rust from people who have tried it. The tell is expecting a Rust REPL at a breakpoint. A good answer names the fallback to Objective C++, explains why `p name` still looks right (a variable path, then formatters), and reaches for `frame variable` paths without being prompted. A stronger answer adds that the paths are tied to std's internals, so they belong in an investigation, not in scripts that must keep working across toolchain updates.

  • Why does `frame variable hits[0]` work on a `HashMap` that has no indexing by position?
    `frame variable` does no Rust evaluation; its `[]` indexes the value's children. The rust-lldb formatters give a `HashMap` synthetic children, one per occupied bucket, so `hits[0]` is the first occupied bucket shown as a `(key, value)` tuple. That order is the hash table's, not insertion order, and nothing about `HashMap`'s API is involved.
  • Why should a team not build debugging scripts on paths like `name.vec.len`?
    Those paths walk private fields of std types, and std's layout changes between Rust releases; the providers' own `Vec` docstring records `RawVec` changes in 1.75 and 1.76. A path that works on one toolchain can fail after an update. Use paths interactively, and when a check must last, put it in the program, such as a debug assertion or a log line.

  • rust-lldb understands Rust syntax, so any method call works at a breakpoint.
  • `p` and `frame variable` are the same command under different names.
  • `p name.len()` fails because the binary was built without debug info.
  • LLDB prints a String by calling its Display impl in the process.
  • Frame variable paths are a stable API across Rust releases.