Cannot get current line of `main` in a Rust program in GDB

432 views Asked by At

Even when compiling with debug information enabled, e.g. a regular cargo build that uses the dev profile, it appears to be impossible to look up in which source line the execution currently is.

To reproduce, generate a new project with cargo; the example project is sufficient.

$ cargo new helloworld-rs
$ cd helloworld-rs
$ cargo build
$ rust-gdb target/debug/helloworld-rs
(gdb) b main
Breakpoint 1 at 0x5430
(gdb) run
Starting program: /tmp/helloworld-rs/target/debug/helloworld-rs
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Breakpoint 1, 0x0000555555559430 in main ()
(gdb) info line
No line number information available.

Compared to C, which has no issues whatsoever:

$ cat > hw.c <<EOF
#include <stdio.h>

int main() {
    printf("Hello, world!");
}
EOF
$ clang -g -o hw hw.c
$ gdb hw
(gdb) b main
Breakpoint 1 at 0x1148: file hw.c, line 4.
(gdb) run
Starting program: /tmp/helloworld-c/hw 

Breakpoint 1, main () at hw.c:4
4      printf("Hello, world!");
(gdb) info line
Line 4 of "hw.c" starts at address 0x555555555148 <main+8> and ends at 0x55555555515b <main+27>.

Am I doing something wrong, is something wrong with my system (up-to-date Arch, at the time of writing), or is this an issue with Rust itself?

1

There are 1 answers

0
Tom Tromey On BEST ANSWER
(gdb) b main

This will set a breakpoint at the main function -- but in Rust, this function is provided by the system, and arranges to call your program's real main.

Instead what you normally want to do is:

(gdb) start

This sets a temporary breakpoint on the main you supplied, and then does run.