Debug, Tools

Debugging Rust on Windows using Visual Studio Code

Rust is the next super cool language I have seen after C# . Flame war… 😀 just kidding. But seriously the whole idea of Rust has just blown my mind and I just wanted to learn it.

But coming from C# + Visual Studio background. I am so spoil on using these massive super cool tool and excellent debugging experience. So selecting any language I also tend to see what tools are available.

Rust community is still not  as big as .Net, mainly because its so new language. So I did not find ONE great tool which will serve purpose. But I am sure things will change. There are things coming up, you can read more here

Ok so back to the original purpose of this post. Rust with Visual Studio Code.

The tools we will use :

  1. Visual Studio code here
  2. Rust Nightly (1.13) GNU 64bit here (Why we need this I’ll explain it a bit later )
  3. Rust Source here (I have used 7 zip to extract)
  4. GDB for Windows here
  5. Python here

Visual studio extensions:

  1. Rusty Code here
  2. Native Debug here

 

Now the steps to install or get things working.

  1. Install Rust Nightly 1.13
  2. Create a folder C:/rust/utils
  3. Open command prompt and CD to C:/rust/utils.
  4. Type cargo install racer
  5. Type cargo install rustfmt
  6. Type cargo install rustsym
  7. Put C:/rust/utils/bin in your environment variable PATH
  8. Install GDB for example in location C:/rust/debug
  9. Put this path C:/rust/debug/gdb64/bin in your environment variable PATH.
  10. Install Python
  11. Extract rust src for example in location c:/rust/src
  12. Create an environment variable RUST_SRC_PATH and assign value C:/rust/src/src to it. Its pointing to the actual source folder.
  13. Install Visual Studio Code
  14. Open Visual Studio Code. Press Ctrl + Shift + X which will open extensions pane. Type Rusty Code (install + restart). Then same way install Native Debug extension as well.

Thats all to setup the environment.

Now lets do some debugging.

  1. Code I will use is straight from Rust website.
    fn main() {
    // A simple integer calculator:
    // `+` or `-` means add or subtract by 1
    // `*` or `/` means multiply or divide by 2
    
    let program = "+ + * - /";
    let mut accumulator = 0;
    
    for token in program.chars() {
    match token {
    '+' => accumulator += 1,
    '-' => accumulator -= 1,
    '*' => accumulator *= 2,
    '/' => accumulator /= 2,
    _ => { /* ignore everything else */ }
    }
    }
    
    println!("The program \"{}\" calculates the value {}",
    program, accumulator);
    }
    
  2. Lets use folder c:/rust/learning
  3. One thing Rusty Code does not support at the time of writing this blog is creating a new rust project so you have to do it manually by using cargo.
  4. Open command prompt and CD to C:/rust/learning.
  5. Type cargo new vscodetest –bin
  6. Open the new created folder C:/rust/learning/vscodetest from Visual Studio Code
  7. Press Ctrl + Shift  + P
  8. Type Cargo and select Cargo: Build Debug
  9. Press Ctrl + Shift + D
  10. Press the start button beside the Configurations drop down. Like thisdebug_config
  11. Select GDB
  12. It will create launch.json. Change it to look like this
       {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug",
                "type": "gdb",
                "request": "launch",
                "target": "target/debug/vscodetest",
                "cwd": "${workspaceRoot}"
            }
        ]
    }
  13. Now put a line break at line number 6
  14. Press F5. The breakpoint should hit. Like this debugging_without_pretty.png
  15. If you see in the above screenshot. I am trying to inspect the value of variable program but it seems to show some extra info. We can get rid of it with a small python script.
  16. Go to C:/rust/debug/gdb64/bin and open file gdbinit in VS code. And add this at the end of the file
    python
    print "---- Loading Rust pretty-printers ----"
    
    sys.path.insert(0, "C:/rust/src/src/etc")
    import gdb_rust_pretty_printing
    gdb_rust_pretty_printing.register_printers(gdb)
    
    end 
  17. Save it and go back to VS Code and again press F5. You should see cleaner inspect output like this debugging_with_pretty

That’s all. Not a very easy process but still good enough to start learning rust.

Now why I choose the nightly build mainly because with the version at the time of the post was 1.11 which had a small problem in debugging rust macros. To read about it more you can check my question in Rust user group here

Please let me know if this does not work or you find some problem so that I can help you if I could and also update the blog post. Also I have read various blog post and stack overflow post to get understanding about all this so thanks to all of them, its difficult to post all the link here as most of them I forgot 🙂

17 thoughts on “Debugging Rust on Windows using Visual Studio Code

  1. I followed all the directions exactly and when I try to run the rust program I get an error –
    Failed to load MI Debugger: “c:\rust\learning\vscodetest\target\debug\vscodetest.exe”: not in executable format: File format not recognized
    Any help is welcome.

    Like

  2. I followed the commands to the letter but my breakpoints are not being hit. Instead I get this message:

    No symbol table is loaded. Use the “file” command.

    Any ideas?

    Like

      1. installed toolchains

        stable-x86_64-pc-windows-gnu (default)
        stable-x86_64-pc-windows-msvc

        active toolchain

        stable-x86_64-pc-windows-gnu (default)
        rustc 1.15.1 (021bd294c 2017-02-08)

        i use gnu as default, still receive the error message

        No symbol table is loaded. Use the “file” command.
        Running executable
        [New Thread 10080.0x81c]
        [New Thread 10080.0×2510]

        Like

  3. Thanks, I do not use it for rust, but it’s “pretty printer” in STL and it worked. I was looking for a lot. Sorry for my english.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s