Unable to print static variables from a module in GDB

I have the following project:

// main.rs
mod other;

#[derive(Debug)]
pub(crate) struct MainStruct { 
    number: usize,
}
pub(crate) static MY_MAIN: MainStruct = MainStruct { number: 123 };

fn main() {
    loop {
        ::std::thread::sleep_ms(1000);
        println!("{:?} {:?}", MY_MAIN, other::MY_STATIC);
    }
}
// other.rs
#[derive(Debug)]
pub(crate) struct MyStruct {
    number: usize,
}
pub(crate) static MY_STATIC: MyStruct = MyStruct { number: 1337 };

I run the program, and connect to it with gdb -p (PID). I want to print out the statics:

(gdb) p gdb_test<TAB>
gdb_test                    gdb_test::main              gdb_test::other::MyStruct   
gdb_test::MY_MAIN           gdb_test::other             gdb_test::other::{{impl}}   
gdb_test::MainStruct        gdb_test::other::MY_STATIC  gdb_test::{{impl}}          
(gdb) p gdb_test::MY_MAIN
$1 = {number = 123}
(gdb) p gdb_test::other::MY_STATIC
No type "other" within class or namespace "gdb_test".

That is, I’m unable to print out statics that are not in the top level.

The version of gdb I’m running is

~/t/gdb-test> gdb -v
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git

Running rust-gdb does not help.

I’m sure I’ve been able to do this previously (a few months ago).

If I’m not mistaken, since your module isn’t pub mod, the static is assumed to be never reachable from outside the crate. Does pub mod make it visible?

No, it doesn’t seem to help any. I figured it wasn’t a problem of visibility, since gdb_test::other::MY_STATIC comes up in the tab autocomplete, so presumably the debug info is somewhere in the executable.

https://github.com/rust-lang/rust/issues/9227#issuecomment-38005107 suggests you may need to enclose the path in a single quote, i.e.:

p 'gdb_test::other::MY_STATIC'

That issue is a few years old, so I’m not sure if anything’s changed in this area but worth a try.

2 Likes

This does indeed work! Thank you.