1

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
FallenWarrior
  • 656
  • 3
  • 16
  • 2
    `main` in GDB ≠ `fn main` in Rust. It looks like your question might be answered by the answers of [Unable to set a breakpoint on main while debugging a program compiled with Rust 1.10 with GDB](https://stackoverflow.com/q/38416394/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Sep 15 '20 at 19:37

1 Answers1

1
(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.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63