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 🙂

21 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?

    Liked by 1 person

      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

  4. Thanks for this article. Every extension and method of debugging Rust code failed except for this article. However, there is one issue I am experiencing. I tried to use the Python script to get rid of the extra info, however it instead has made it worse. Before updating the gdbinit file, the info for the program variable showed:

    ‘{data_ptr = 0x92fcb0 “(“, length = 9632848}’

    But after updating the file, it shows:

    ‘”(“, ‘\000’ , “\001”, ‘\000’ , “\200\375\222\000\000\000\000\000x\375\222”, ‘\000 ,’

    and continues on with addition numbers and “repeats” statements”.

    The gdbinit file was initially:

    python
    import sys
    import os.path
    import glob
    globbed_path = sorted(glob.glob(os.path.dirname(sys.executable) + ‘/../../share/gcc-/python/libstdcxx/v6/printers.py‘), reverse=True)
    if len(globbed_path) > 0:
    sys.path.insert(0, os.path.dirname(globbed_path[0]))
    from printers import register_libstdcxx_printers
    register_libstdcxx_printers (None)
    end

    And I added the text you mentioned at the end so the file now contains:

    python
    import sys
    import os.path
    import glob
    globbed_path = sorted(glob.glob(os.path.dirname(sys.executable) + ‘/../../share/gcc-/python/libstdcxx/v6/printers.py‘), reverse=True)
    if len(globbed_path) > 0:
    sys.path.insert(0, os.path.dirname(globbed_path[0]))
    from printers import register_libstdcxx_printers
    register_libstdcxx_printers (None)
    end

    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

    I have never worked with Python so I am sure there is something simple I am missing with this. I have confirmed that the folder C:\Rust\src\src\etc does exist and I currently have Python 3.5 and 3.7 installed (from previous attempts at debugging Rust code in VSCode). My PATH variable contains the folders for Python and are:

    %USERPROFILE%\AppData\Local\Programs\Python\Python35-32\Scripts\
    %USERPROFILE%\AppData\Local\Programs\Python\Python35-32\
    %USERPROFILE%\AppData\Local\Programs\Python\Python37-32\Scripts\
    %USERPROFILE%\AppData\Local\Programs\Python\Python37-32\

    If you can provide any help or suggestions on how to resolve then this method is perfect! Thanks in advance for any additional help you can provide for this error.

    Like

  5. Did a little more testing and found that if I am currently at the breakpoint on Line 6 and hover the mouse over “program”, then I see that long string of numbers and “repeats” statements. However, if I also add a breakpoint on Line 7, and the IDE is current at that breakpoint, if I then hover the mouse over the “program” variable on Line 6, the tooltip then appears as shown in the image in the article. Any idea of why the tooltip appears different depending on whether the IDE is currently at the Line 6 or Line 7 breakpoint and, if so, how to change the script so that the tooltip will show the nicer text (as in your image) while at the Line 6 breakpoint instead of having to go to the next breakpoint? Thanks again.

    Like

  6. I have noticed you don’t monetize your page, don’t waste
    your traffic, you can earn extra cash every month because you’ve
    got high quality content. If you want to know how to make extra money,
    search for: Mertiso’s tips best adsense alternative

    Like

  7. I see you don’t monetize your website, don’t
    waste your traffic, you can earn additional bucks every month.
    You can use the best adsense alternative for any type of website (they approve all websites),
    for more info simply search in gooogle: boorfe’s tips monetize your website

    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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s