Debugging with gdb, setting a breakpoint in main()

So I'm trying to do some debugging with gdb and noticed that setting my breakpoint at main() seems to be ignored by the debugger, it just goes ahead and executes the whole program when I give gdb the run command.

I did some research and apparently this is caused by Rust having a different main() function that actually executes the main() of your program. But based on the thread I was reading on this forum, that issue has already been fixed?

I've just upgraded gdb to 8.1, which supposedly has Rust support, and I'm still running into this problem. Setting the breakpoint at program_name::main is a quick workaround though.

2 Likes

I'm using gdb with Rust like this:

$ cargo test
...
$ rust-gdb ./target/debug/examples/some_exe
(gdb) b some_exe.rs:372

Which would set a breakpoint in line 372 of the Rust code example called some_exe.rs. I hope this helps ...

1 Like

Very useful!

Now if only I could figure out how to get gdb inside emacs to not keep displacing the buffers I want (my gdb command line, the source line I am stopped at) with their input/output buffer every time I println anything. But this isn't a rust issue...

Thanks,

-kb

... the source line I am stopped at ...

I just use Ctrl-x o within gdb to display the surrounding Rust code. Pressing it again allows you to scroll, and to go back to the command line, press it again ...

I hope that helps.

1 Like

Another really nice option for viewing the source while debugging is cgdb. It’s a curses wrapper around gdb. I never use gdb, even when debugging embedded rust over JTAG.