Skip to content

gdb "cannot subscript non-array type" to index a Vec #66482

@Hi-Angel

Description

@Hi-Angel

Attempt to print content of a Vec at index results in error. Since it works fine for C++, I think the problem is in rustc not providing the necessary debug information for this to work.

Vecs are a widely used type, and not being able to print their content at index hurts debugging process noticeably.

Steps to reproduce (in terms of terminal commands)

$ cat -n test2.rs
     1  fn main() {
     2      let x: Vec<usize> = vec![1,2,3];
     3      println!("{:?}", x);
     4  }
$ rustc test2.rs -o a -g
$ gdb ./a
Reading symbols from ./a...
warning: Missing auto-load script at offset 0 in section .debug_gdb_scripts
of file /tmp/a.
Use `info auto-load python-scripts [REGEXP]' to list them.
gdb λ br 3
Breakpoint 1 at 0x5c05: file test2.rs, line 3.
gdb λ r
Starting program: /tmp/a
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Breakpoint 1, test2::main () at test2.rs:3
3           println!("{:?}", x);
gdb λ p x[0]

Expected

A print:

1

Actual

It prints:

Cannot subscript non-array type

Additional information

rustc version: rustc 1.41.0-nightly (1bd30ce2a 2019-11-15)
gdb version: 8.3.1

Activity

added
A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Nov 16, 2019
Hi-Angel

Hi-Angel commented on Dec 12, 2019

@Hi-Angel
Author

Probably related: #33014

tromey

tromey commented on Feb 26, 2020

@tromey
Contributor
Hi-Angel

Hi-Angel commented on Feb 27, 2020

@Hi-Angel
Author

See also https://sourceware.org/bugzilla/show_bug.cgi?id=21178

Thanks! So, to be clear: is it a bug in rust compiler or in gdb? I feel like one of the reports (either this one, or the referenced one) should be closed.

JerrieLau

JerrieLau commented on Jun 2, 2020

@JerrieLau

lldb is fine.

tromey

tromey commented on Jun 2, 2020

@tromey
Contributor

lldb is fine.

I'm very surprised to hear this. How does it work?

Hi-Angel

Hi-Angel commented on Jun 2, 2020

@Hi-Angel
Author

lldb is fine.

I'm very surprised to hear this. How does it work?

Good point. I just tested, and it turns out for me, lldb has same problem as gdb. Here's a session screenshot:

Screenshot_20200603_013552

I deleted my prev. comment since it doesn't seem to be relevant now.

@JerrieLau could you please elaborate how you managed to make it work? Perhaps are you using Rust nightly and they added debug info recently? Anything else?

I tested it with lldb version 10.0.0 and rustc version 1.44.0-nightly (20fc02f83 2020-04-20)

JerrieLau

JerrieLau commented on Jun 3, 2020

@JerrieLau

@Hi-Angel I just use it in my IDE(CLion). Here's a screenshot:
debug

Hi-Angel

Hi-Angel commented on Jun 3, 2020

@Hi-Angel
Author

@JerrieLau what you're doing is different. You're basically treating x as a struct and unwrapping its members down to a pointer to elements. Both gdb and lldb can do that:

Screenshot_20200603_183911

But as a programmer, I don't want to mess around with private internals of Rust types, which I'd need to know, and which can easily be renamed/changed from a Rust version to version.

dario23

dario23 commented on Jun 3, 2020

@dario23
Contributor

the intellij-rust plugin does have some gdb-integration python code though, might be interesting to look at anyways (licensed under MIT).

JerrieLau

JerrieLau commented on Jun 4, 2020

@JerrieLau

@JerrieLau what you're doing is different. You're basically treating x as a struct and unwrapping its members down to a pointer to elements. Both gdb and lldb can do that:

Screenshot_20200603_183911

But as a programmer, I don't want to mess around with private internals of Rust types, which I'd need to know, and which can easily be renamed/changed from a Rust version to version.

Yes. you are right

artemmukhin

artemmukhin commented on Oct 14, 2020

@artemmukhin
Contributor

@JerrieLau @Hi-Angel LLDB supports accessing synthetic children via frame variable <name> command:

(lldb) frame variable vec[0]
(i32) vec[0] = 1

See https://lldb.llvm.org/use/variable.html#synthetic-children for more information:

Unlike many other visualization features, however, the access to synthetic children only works when using frame variable, and is not supported in expression

Unfortunately, just adding vec[0] watch in CLion with LLDB doesn't work yet.

@dario23 As for IJ Rust formatters, they're already upstreamed. But "cannot subscript non-array type" problem with GDB still exists.

wesleywiser

wesleywiser commented on Aug 21, 2023

@wesleywiser
Member

Visited during wg-debugging triage. I can confirm this is still an issue. Marking P-medium as making this work would be pretty useful when debugging Rust programs in gdb.

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
and removed
C-bugCategory: This is a bug.
on Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)C-enhancementCategory: An issue proposing an enhancement or a PR with one.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Enselic@dario23@wesleywiser@tromey@jonas-schievink

        Issue actions

          gdb "cannot subscript non-array type" to index a Vec · Issue #66482 · rust-lang/rust