Printing Option values in rust-lldb

My experience on macOS trying to print values of Rust enum types from a debugger is that it's hopeless. Example:

#[derive(Debug)]
enum E {
  A,
  B,
}

#[derive(Debug)]
enum E2 {
  A(usize),
  B(usize),
}

fn main() {
    let s = Some(5);
    let s2 = Some("hi");
    let s3 = Some(E::A);
    let s4 = Some(E2::A(1));
    println!("goodbye {:?} {:?} {:?} {:?}", s, s2, s3, s4);
}

When I run rustc a.rs -g and then rust-lldb -- a and set a breakpoint on line 18, I get this output:

* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x000000010000462f a`a::main::h8bbda9164eb1d7fd at a.rs:18:5
   15  	    let s2 = Some("hi");
   16  	    let s3 = Some(E::A);
   17  	    let s4 = Some(E2::A(1));
-> 18  	    println!("goodbye {:?} {:?} {:?} {:?}", s, s2, s3, s4);
   19  	}
Target 0: (a) stopped.
(lldb) p s
(core::option::Option<i32>) $0 =
(lldb) p s2
(core::option::Option<&str>) $1 =
(lldb) p s3
(core::option::Option<a::E>) $2 =
(lldb) p s4
(core::option::Option<a::E2>) $3 =
(lldb)

Is this everyone else's experience as well? Is there any hope for obtaining more useful output from the debugger here for enums in general? If not, is it possible to write pretty printers for some of the most common types?

I'm running this version, apparently:

lldb-1200.0.44.2
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)

FWIW, on Windows and through the vscode lldb support, things work:

image

(I have no experience using the lldb TUI nor any idea what the CodeLLDB plugin is doing, unfortunately.)

Enums essentially don't work in stock lldb, see lldb can not print Option<i32> · Issue #79530 · rust-lang/rust · GitHub for an explanation. CodeLLDB contains special support for Rust which allows it to show enums.

That "special support" CodeLLDB has is equivalent or even using rust/rust-lldb at 297a8018b525c28ef10ee6a91d61954839b508b9 · rust-lang/rust · GitHub, right?

I'm not certain, but my guess would be it is vscode-lldb/rust.py at master · vadimcn/vscode-lldb · GitHub

1 Like