Printing single vector elements in gdb

I’ve been trying to print out single elements of a vector in gdb to no avail, has anyone encountered a similar problem?

(gdb) p int_vec
$1 = alloc::vec::Vec<i32> {buf: alloc::raw_vec::RawVec<i32, alloc::heap::Heap> {ptr: core::ptr::Unique<i32> 
{pointer: core::nonzero::NonZero<*const i32> (0x7ffff6c20020), _marker: core::marker::PhantomData<i32>}, 
cap: 7, a: alloc::heap::Heap}, len: 7}
(gdb) p int_vec[0]
Cannot subscript non-array type
(gdb) p int_vec.get(1)
Could not find function named 'alloc::vec::Vec<i32>::get'

Did you try with rust-gdb? This should make your first gdb command works. I’m not sure for the rest.

Yup, this is with rust-gdb

After searching, the simpler solution seems to be:

p int_vec.buf.ptr.pointer.__0[0]

Now, another point. If you use rust-gdb, printing a Vec should look like:

$1 = Vec<i32>(len: 7, cap: 7) = {1, 2, 3, 4, 5, 6, 7}

And not all this unreadable stuff. The two most probable reason for that is that your version of gdb is too old, or that is was compiled without the python support. If you run info pretty-printer in gdb, it should include rust_pretty_printer_lookup_function.

1 Like

I’m running gdb 8.1 so the version is probably not the issue. You’re right about python support though, when I run info pretty-printer it says undefined info command: "pretty-printer"

I’ll have to do some searching for how to compile gdb with Python support. Thanks for the help!

I can see that the Rust-specific pretty-printer exists, but when I do

p self.elements.buf.ptr.pointer.__0[0]

where self.elements is a Vec, I get the following error:

That operation is not available on integers of more than 8 bytes.

I suspect it’s because my vector is filled with structs, not integers, but that still leaves me not knowing how to print the individual elements of a vector.