Using GDB with cargo

Hey guys, if I wanted to compile my Rust project using GDB while using the cargo package, what would I need to install?

Addtionally if I copied my executable to another PC without having Rust installed however I have installed bash terminal on Windows with GDB, would it work or do I need a Rust compiler?

It will work just fine. If you want pretty printers for various builtin/standard library types you will need rust-lldb/rust-gdb, but other than that it doesn't really matter if you have rustc installed or not when debugging.

So I can just do cargo build and it will build it and it can be ran from GDB?

GDB works with Rust executables, yes.

1 Like

executable files run standalone not unlike python or java files where you need python or java installed for your files to run.

Yes I understand that, however before I was asking how would gdb work with executables but now I understand it.

I get this error message in gdb

No debugging symbols found in my_program.exe

Any idea why this may be happening?

in cargo I am building it and it does have debuginfo

Finished dev [unoptimized + debuginfo] target(s) in 0.11s

which is great. so now the next thing is in the gdb I do get this:

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Traceback (most recent call last):
  File "<string>", line 3, in <module>
ModuleNotFoundError: No module named 'libstdcxx'
/etc/gdbinit:6: Error in sourced command file:
Error while executing Python code.
Reading symbols from my_program.exe...
(No debugging symbols found in my_program.exe)

And when you try to break into main b main

No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]

Not sure what may be the issue?

Or is it that it can only read linux programs and does not support windows 7 (same issue with windows 10).

That should have worked fine. Did you move the *.exe to a different folder or delete the *.pdb files? GDB will look up the executable's symbols and debug information in its PDB file, so that needs to be accessible.

Yes but when I tried running it straight from target/debug still the same issue.

No mate.

Right well its not quite working, still can't figure out why?

Does cargo even insert debug symbols for gdb for Windows? Is gdb only read linux debug flags or whatever?

Are you using the MSVC (x86_64-pc-windows-msvc) or MinGW toolchain (x86_64-pc-windows-gnu)? GDB only works with MinGW because it requires DWARF debuginfo rather than CodeView debuginfo AFAIK. MSVC uses CodeView debuginfo in separate .pdb files. MinGW uses DWARF debuginfo embedded in the executable itself.

1 Like

I honestly don't know I just used the default one.

I switched to another toolchain stable-gnu that is why I don't know what I was using previously.

stable-gnu seems to work but only with new projects with a simple hello world, with old projects it is having build issues so I might try x86_64-pc-windows-gnu this then.

Only problem is that I ran this and I got this output error:

rustup install x86_64-pc-windows-gnu
warning: (partial) target triple specified instead of toolchain name
error: no candidate toolchains found
error: toolchain 'x86_64-pc-windows-gnu' is not installed

how would I install this toolchain then?

OK removed build.rs and it is fine

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.