Debugging Rust on Windows?


#1

HI,

I am a super newbie to rust and trying to learn rust with lots of enthusiasm.

I am using Visual studio code with RustyCode extension and GDB to debug. But I don’t get it working correctly.

Some help would be much appreciated. Snapshot of what the error look like below


#2

I’d suspect that VS code doesn’t deal correctly with cygwin paths. Did you try to start it from a non-cygwin shell?


#3

My guess is that you are trying to use cygwin’s gdb for debugging. This probably won’t work, you will need a gdb for proper Windows. I haven’t tried to use gdb with such setup so can’t help you here.

As an alternative it is possible to use Visual Studio (not Code) to debug Rust programs but last time I tried it it only supported stepping thru code (step by step or stopping at breakpoints) but it wasn’t able to display content of any variable. You will need to compile your program to *-pc-windows-msvc target instead of *-pc-windows-gnu. Then you can debug it in VS as described here or here. But as I said it is not very useful.


#4

Thanks for the insight.

I have switched to http://tdm-gcc.tdragon.net/ . Which I came to know from here http://stackoverflow.com/questions/33570021/how-to-set-up-gdb-for-debugging-rust-programs-in-windows

But now there is some weird behavior.

I created a small video of what I am trying to say.


#5

What is your debug configuration in VS Code? (launch.json file)


#6

{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “Debug”,
“type”: “gdb”,
“request”: “launch”,
“target”: “target/debug/vscodetest”,
“cwd”: “${workspaceRoot}”
}
]
}


#7

Not 100% sure, but here are my guesses:

  • The Python error is because Rust binaries contain a gdb auto-load section for pretty printer scripts, which rely on a custom module shipped with Rust. If you run rust-gdb script (from Rust distro’s /etc folder) instead of plain gdb, it will set up module search paths correctly, and this should work.
  • The stepping-over-a-macro problem should be already fixed in the nightly channel.
  • The variables are allocated stack space in prologue of the function, but aren’t initialized until you step past their declarations/assignments. (Yes, ideally they wouldn’t be in scope until declaration. I think rustc actually tries to do the right thing here, not sure why it isn’t working).
  • I am guessing that the closing function brace corresponds to a point in code after the epilogue, so the variables don’t exist anymore. We should probably adjust this.

#8

What do you mean by "If you run rust-gdb script (from Rust distro’s /etc folder) "? Can you explain?

With Linux Rust archive in rustc/lib/rustlib/etc there are

  • gdb_load_rust_pretty_printers.py
  • gdb_rust_pretty_printing.py
  • debugger_pretty_printers_common.py

And in How to set up GDB for debugging Rust programs in Windows? , to enable pretty-printers, It’s indicated

import gdb_rust_pretty_printing
gdb_rust_pretty_printing.register_printers(gdb)

#9

There is also a file rust-gdb which I suppose a shell script and can be run using that. I did it in windows using git-bash. But as of now I did not understand what it suppose to do :slight_smile:

It gave me error like this:

nisu@NWISU02 MINGW64 /c/Users/nisu/.cargo/rustc-1.11.0/src/etc
$ sh rust-gdb
GNU gdb (GDB) 7.9.1
Copyright © 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and “show warranty” for details.
This GDB was configured as “x86_64-w64-mingw32”.
Type “show configuration” for configuration details.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type “help”.
Type “apropos word” to search for commands related to “word”.
---- Loading Rust pretty-printers ----
Traceback (most recent call last):
File “”, line 4, in
ImportError: No module named gdb_rust_pretty_printing
c:\rustdebug\gdb64\bin…/bin/gdbinit:18: Error in sourced command file:
Error while executing Python code.
Warning: C:\Program Files\Rust stable GNU 1.11/lib/rustlib/etc: No such file or directory.
(gdb)


#10

Thanks I will try with the nightly build.


#11

No error, just this warning

---- Loading Rust pretty-printers ----
Warning: D:\Applications\Rust stable GNU 1.11/lib/rustlib/etc: No such file or directory.
(gdb) quit

with Rust 1.11 GNU and this in gdbinit file

python
print "---- Loading Rust pretty-printers ----"
sys.path.insert(0, "D:/Applications/Rust stable GNU 1.11/bin/rustlib/etc")
import gdb_rust_pretty_printing
gdb_rust_pretty_printing.register_printers(gdb)
end

But how to call rust-gdb in VS Code?


#12

I can confirm the macro debugging works now. Thanks for the help. I also fixed the pretty print by giving the path of the etc folder correctly.

Once again thanks for the help. A blog post will follow soon How to get it working with Visual Studio Code on Windows :slight_smile:


#13

Hmm I did not try that. I used a usual gdb and that worked.


#14

I’m interesting by your blog post. Post your Url here :wink:


#15

Here it is https://sherryummen.in/2016/09/02/debugging-rust-on-windows-using-visual-studio-code/ let me know if things work or do not work :slight_smile:


#16

That probably won’t work. In a similar way to Eclipse/RustDT, the IDE will usually need to start the gdb executable directly. It probably doesn’t support starting a bash script (in part because it needs to control the stdin/stdout of the GDB process). So if you want to enable pretty printers you need to set them up in a way like described here: http://stackoverflow.com/questions/33570021/how-to-set-up-gdb-for-debugging-rust-programs-in-windows

And yes, use TDM-GCC’s GDB, not Cygwin’s GDB.


#17

Yo, I noticed you have step “10. Install Python”. I don’t think this is necessary. The TDM-GDB download that was mentioned here already includes Python support. See: http://tdm-gcc.tdragon.net/download , it says gdb-7.9.1-tdm64-2.zip 12.3 MB · gdb 7.9.1 GNU debugger — includes Python support..
I got my pretty printers to work without having to install Python separately.


#18

hey thanks. Ill update the blog post.


#19

Do you test with the Release 7.12 of GDB? (Debugging programs written in Rust is now supported with GDB 7.12).


#20

Hmm I do not know how to compile that source. I am not getting any exe of that. If you know please let me know then I can try it out