How do you display the contents of a Rust vector using LLDB

Hello All!

I’m trying to use LLDB to debug Rust applications developed under vcode. I’ve spent a lot of time trying to figure out how to display the values in a Rust vector, but can’t find the correct format. Does anybody have any idea how to do this?

Cheers!!

Dermot

Can you try rust-lldb, which comes with rust distribution?

1 Like

Rustup installs a rust-lldb that runs lldb and adds a bunch of data formatters for you.

On my Ubuntu machine I apt installed lldb and then got a bunch of errors about python imports from rust-lldb. If that happens to you:

$ dpkg -L python3-lldb-14
<...>
/usr/lib/llvm-14/lib/python3.10/dist-packages
<...>
$ export PYTHONPATH=/usr/lib/llvm-14/lib/python3.10/dist-packages:$PYTHONPATH

Now rust-lldb should open properly. Not sure why I had to do that, could be something specific to my machine.

$ rust-lldb hello
(lldb) command script import "/home/david.spickett/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/etc/lldb_lookup.py"
<...>
(lldb) type summary add -F lldb_lookup.summary_lookup  -e -x -h "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
(lldb) type category enable Rust
(lldb) target create "hello"
<...>
(lldb) c
Process 229980 resuming
Process 229980 stopped
* thread #1, name = 'hello', stop reason = breakpoint 1.1
    frame #0: 0x0000aaaaaaab1ae8 hello`hello::main::h18697f96bf272b77 at hello.rs:3:19
   1    fn main() {
   2        // Statements here are executed when the compiled binary is called.
-> 3        let mut _xs = vec![1i32, 2, 3];
   4
   5        // Print text to the console.
   6        println!("Hello World!");
   7    }
(lldb) n
Process 229980 stopped
* thread #1, name = 'hello', stop reason = step over
    frame #0: 0x0000aaaaaaab1b40 hello`hello::main::h18697f96bf272b77 at hello.rs:6:5
   3        let mut _xs = vec![1i32, 2, 3];
   4
   5        // Print text to the console.
-> 6        println!("Hello World!");
   7    }
(lldb) p _xs
(alloc::vec::Vec<int, alloc::alloc::Global>) $0 = size=3 {
  [0] = 1
  [1] = 2
  [2] = 3
}

(note that main is the C main so you have to continue twice once for the C main then again to the rust main)

And here’s the manual way if you can’t get rust-lldb to work:

(lldb) n
Process 229542 stopped
* thread #1, name = 'hello', stop reason = step over
    frame #0: 0x0000aaaaaaab1b40 hello`hello::main::h18697f96bf272b77 at hello.rs:6:5
   3        let mut _xs = vec![1i32, 2, 3];
   4
   5        // Print text to the console.
-> 6        println!("Hello World!");
   7    }
(lldb) p _xs
(alloc::vec::Vec<int, alloc::alloc::Global>) {
  buf = {
    ptr = {
      pointer = {
        pointer = 0x0000aaaaaab0eba0
      }
      _marker = {}
    }
    cap = 3
    alloc = {}
  }
  len = 3
}

Unfortunately the size has to be a constant:

(lldb) expr *(int(*)[_xs.len])_xs.buf.ptr.pointer.pointer

error: Error [IRForTarget]: Size of result type 'int[_xs.len]' couldn't be determined

Fix that and it works:

(lldb) expr *(int(*)[3])_xs.buf.ptr.pointer.pointer
(int[3]) $4 = ([0] = 1, [1] = 2, [2] = 3)

Edit: credit here goes to @jasonmolenda who answered a similar question on Stack Overflow - debugging - View array in LLDB: equivalent of GDB's '@' operator in Xcode 4.1 - Stack Overflow.

If you mean Visual Studio Code, you’ll want to change the settings of the debug plugin to point at rust-lldb instead of lldb.

Thank you very much, David!

That just saved me an awful lot of time and effort!

Cheers!!

Dermot

1 Like

Thanks for the extra info, David!