Debugging Rust with LLDB
What is LLDB?
A low-level debugger similar to GDB. macOS uses LLDB instead of GDB by default.
The Rust wrapper rust-lldb provides better output for Rust types.
Running it
# Rust-specific wrapper (improved type output)
rust-lldb ./target/debug/my_program
# Or use lldb directly
lldb ./target/debug/my_program
Key commands
| Command | Description |
|---|---|
b main.rs:10 | Set a breakpoint at line 10 of main.rs |
b 10 | Set a breakpoint at line 10 of the current file |
r | Run the program |
c | Continue execution to the next breakpoint |
n | Execute the next line (next, without entering a function) |
s | Execute the next line (step, entering a function) |
bt | Print the call stack (backtrace) |
p <variable> | Print a variable's value |
q | Exit the debugger (quit) |
Printing variables (frame variable)
frame variable # Print all local variables in the current scope
frame variable self # Print self
frame variable self.name # Print the self.name field
frame variable --show-globals # Include global variables
frame variable --show-types # Include type information
frame variable --show-types --show-values # Print types + values
Evaluating expressions
expr <expression> # Evaluate and execute an expression in the current context
Using GDB (Linux)
rust-gdb ./target/debug/my_program
GDB commands are nearly identical to LLDB commands, but there are some differences:
info locals→ local variables (LLDB'sframe variable)info breakpoints→ list of breakpointsdelete <number>→ delete a breakpoint