Rust Debug with Breakpoint Support in Visual Studio Code on Windows

You can debug Rust programs in Visual Studio Code on Windows using LLDB.

Debugging Rust in VSCode

On Windows, LLDB only works reliably with 32-bit binaries. As such, this guide will outline how to get debugging working for 32-bit targets.

To get this working:

  1. Install LLDB. You can get it by installing the LLVM tools, but the current official LLVM installer for Windows does not include LLDB. You can get an installer including 32-bit LLDB here. You do not have to add LLDB to your system environment path.
  2. LLDB requires python35.dll, which which can be downloaded here. As we are using a 32-bit version of LLDB, make sure to install the 32-bit version of Python 3.5 as well.You also do not have to add this version of Python to your system environment path.
  3. If you do not have it already, install a 32-bit toolchain for Rust. You do not have to make it the default toolchain. In command prompt, execute:
    rustup install stable-i686-pc-windows-gnu

    To list your currently installed toolchains, excute:

    rustup show
  4. Install the CodeLLDB extension for Visual Studio Code. Restart Visual Studio Code. This enables support for LLDB.
  5. If you did not add LLDB or Python 3.5 to your environment path, configure Visual Studio Code so the extension knows where to find them.You should set the LLDB executable to lldb.exe inside the bin folder where you installed LLVM.Set the Path executable environment variable to the folder where you installed 32-bits Python 3.5, followed by ;{$env:Path}. This tells LLDB where to find Python.
    {
        "lldb.executable": "C:\\Path\\To\llvm-6.0.0-r313613\\bin\\lldb.exe",
        "lldb.executable_env": {
            "Path": "C:\\Path\\To\\Python35;${env:Path}"
        }
    }

    Visual Studio Code LLDB user settings

  6. Add a debug launch configuration for your project. To open the Debug panel, hit Ctrl+Shift+D. Click on the dropdown, and choose “Add configuration…” Choose the LLDB Cargo Launch configuration. The settings depend on our project structure, but the following probably works:
    {
        "type": "lldb",
        "request": "launch",
        "name": "Debug Cargo LLDB",
        "sourceLanguages": ["rust"],
        "cargo": {
            "args": [
                "+stable-i686-pc-windows-gnu",
                "build"
            ]
        },
        "program": "${cargo:program}",
        "args": []
    }

    Visual Studio Code LLDB launch configuration

    The argument +stable-i686-pc-windows-gnu tells cargo to use this specific 32-bit toolchain to compile your code with, regardless of your current default toolchain. This means you can debug with the 32-bit toolchain without setting it as your default.

    If you need to target a specific binary, add "--bin=binary_name" at the end of the cargo.args list.

  7. If you want to, you can additionally add "debug.inlineValues": true to your user or workspace settings to show values inside the editor.
  8. Place some breakpoints, then press Debug or hit F5! ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.