Given a Rust Vector (potentially, a long one), I would like output a single element while debugging my program. Although indexing the vector directly is desirable, I would also appreciate an approach, which treats the Vec
a structure.
Here are my attempts, which I try to keep as close as possible to this issue, which goes back to 2020: .
Here are my attempts (in Command line and in gdb
):
$ cargo new test_gdb_vec --bin
$ cd test_gdb_vec
$ cat src/main.rs
#->
fn main() {
let x: Vec<usize> = vec![1, 2, 3];
println!("{:?}", x);
}
$ cargo build
#->
Compiling test_gdb_vec v0.1.0 (***)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
$ cargo run
#->
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/test_gdb_vec`
[1, 2, 3]
$ rust-gdb target/debug/test_gdb_vec
(gdb) start
...
Enable debuginfod for this session? (y or [n]) n
...
Temporary breakpoint 1, test_gdb_vec::main () at src/main.rs:2
2 let x: Vec<usize> = vec![1,2,3];
(gdb) next
(gdb) p x
$1 = Vec(size=3) = {1, 2, 3}
(gdb) p x[0]
Cannot subscript non-array type
(gdb) p x.buf.ptr.pointer[0]
There is no member named ptr.
(gdb) p x.buf.inner.ptr.pointer.pointer[0]
$2 = 1
(gdb) p x.buf.inner.ptr.pointer.pointer[1]
$3 = 0
(gdb) p x.buf.inner.ptr.pointer.pointer@3
$4 = [0x5555555afb10, 0x3, 0x7ffff7ffb360 <__x86_cpu_features_p>]
We can see that neither direct indexing works (like it did not in 2020), nor does the instruction copied from the original post (p x.buf.ptr.pointer[0]
). Its direct adaptation does not produce the desired result (1, 2, 3
) either. Any help relative to getting the value of 1 element of a Vec is welcome!
Thanks for Your attention!
Additional information:
rustc 1.84.1 (e71f9a9a9 2025-01-27)
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Given a Rust Vector (potentially, a long one), I would like output a single element while debugging my program. Although indexing the vector directly is desirable, I would also appreciate an approach, which treats the Vec
a structure.
Here are my attempts, which I try to keep as close as possible to this issue, which goes back to 2020: https://github/rust-lang/rust/issues/66482 .
Here are my attempts (in Command line and in gdb
):
$ cargo new test_gdb_vec --bin
$ cd test_gdb_vec
$ cat src/main.rs
#->
fn main() {
let x: Vec<usize> = vec![1, 2, 3];
println!("{:?}", x);
}
$ cargo build
#->
Compiling test_gdb_vec v0.1.0 (***)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
$ cargo run
#->
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/test_gdb_vec`
[1, 2, 3]
$ rust-gdb target/debug/test_gdb_vec
(gdb) start
...
Enable debuginfod for this session? (y or [n]) n
...
Temporary breakpoint 1, test_gdb_vec::main () at src/main.rs:2
2 let x: Vec<usize> = vec![1,2,3];
(gdb) next
(gdb) p x
$1 = Vec(size=3) = {1, 2, 3}
(gdb) p x[0]
Cannot subscript non-array type
(gdb) p x.buf.ptr.pointer[0]
There is no member named ptr.
(gdb) p x.buf.inner.ptr.pointer.pointer[0]
$2 = 1
(gdb) p x.buf.inner.ptr.pointer.pointer[1]
$3 = 0
(gdb) p x.buf.inner.ptr.pointer.pointer@3
$4 = [0x5555555afb10, 0x3, 0x7ffff7ffb360 <__x86_cpu_features_p>]
We can see that neither direct indexing works (like it did not in 2020), nor does the instruction copied from the original post (p x.buf.ptr.pointer[0]
). Its direct adaptation does not produce the desired result (1, 2, 3
) either. Any help relative to getting the value of 1 element of a Vec is welcome!
Thanks for Your attention!
Additional information:
rustc 1.84.1 (e71f9a9a9 2025-01-27)
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Share
Improve this question
asked Mar 24 at 20:03
Alexandre AksenovAlexandre Aksenov
1335 bronze badges
1 Answer
Reset to default 3Cast before you index
p (x.buf.inner.ptr.pointer.pointer as *const usize)[0]
In order to reduce code bloat from monomorphizing generics, the internals of Vec
were refactored to use RawVecInner
which is type agnostic, leading to x.buf.inner.ptr.pointer.pointer
being of type *mut u8
. Hence, when indexing x.buf.inner.ptr.pointer.pointer
directly, you get the values of the individual bytes.
Thus, you need to first cast the pointer to the appropriate type *[const|mut] T
before indexing, and then not only will gdb
step by size_of::<T>()
when it indexes (rather than byte by byte), but it'll also display a T
, not a u8
.
For the curious:
x
is of typeVec<T, A>
(see source).x.buf
is of typeRawVec<T, A>
(see source).x.buf.inner
is of typeRawVecInner<A>
(see source).x.buf.inner.ptr
is of typeUnique<u8>
(see source).x.buf.inner.ptr.pointer
is of typeNonNull<u8>
(see source).x.buf.inner.ptr.pointer.pointer
is of type*mut u8
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744230498a4564222.html
评论列表(0条)