Step by step interactive debugger for Rust?

DebuggingRust

Debugging Problem Overview


How can I debug Rust application step by step interactively like I'm able to do with "pry" in Ruby?

I want to be able to see and preferably change the variables in real time when I reach a break point. Is there any production ready finished project?

Debugging Solutions


Solution 1 - Debugging

I find a good level of usability with VS Code and the CodeLLDB extension:

  1. Install VS Code

  2. Search and install the extension Rust or the newer rust-analyzer from within VS Code

  3. Check requisites and setup CodeLLDB for your platform. As of v1.6, no further setup should be needed.

  4. Search and install the extension CodeLLDB from within VS Code

  5. The LLDB Debugger added the main menu item "Run" from where the debugger can be started. When debugging is started for the first time, you must select the environment (the debugger): select LLDB.

  6. When you select LLDB, a launch.json file will be opened, if not, open it, it's under .vscode folder

  7. Your launch.json should look like this:

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "type": "lldb",
                "request": "launch",
                "name": "Debug",
                "program": "${workspaceRoot}/target/debug/hello_world",
                "args": [],
                "cwd": "${workspaceRoot}/target/debug/",
                "sourceLanguages": ["rust"]
            }
        ]
    }
    

  1. If you wanted to keep things generic and only compile a binary that matches the cargo folder name, you could use ${workspaceRootFolderName} variable substitution for the "program" key:

     {
         "version": "0.2.0",
         "configurations": [
             {
                 "type": "lldb",
                 "request": "launch",
                 "name": "Debug",
                 "program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}",
                 "args": [],
                 "cwd": "${workspaceRoot}/target/debug/",
                 "sourceLanguages": ["rust"]
             }
         ]
     }
    

Here are some blog posts about Rust and VS Code:

Solution 2 - Debugging

The Rust compiler produces native binaries with native debug info (symbol) information, so any native debugger will do. That means gdb and lldb, or the Windows debuggers (WinDBG or just Visual Studio) if you're using the MSVC ABI version of Rust. If you want an integrated experience, RustDT is the way to go (setup on Windows: https://stackoverflow.com/questions/33570021/how-to-set-up-gdb-for-debugging-rust-programs-in-windows/33570022#33570022). Please note that you're likely to run into https://stackoverflow.com/questions/36621130/how-can-i-inspect-variable-values-while-debugging-msvc-abi-rust-programs on Windows and https://github.com/rust-lang/rust/issues/33062 on a Mac.

Solution 3 - Debugging

For a graphical debugger, there is gdbgui. It's available for Linux, Windows and MacOS. It uses the browser as the display and to interact with the debugger.

Solution 4 - Debugging

I have gdb 7.11 and rust-gdb command seems to give more rust relevant information compared to the gdb native. E.g. rust-gdb shows rust objects properly with full names, and gdb simply do not show them.
In the following example gdb would now show at all the bold parts.

$1 = Args = {
  inner = **ArgsOs** = {
    inner = **Args** = {
      iter = **IntoIter<std::ffi::os_str::OsString>** = {
        buf = **NonNull<std::ffi::os_str::OsString>** = {
          pointer = **NonZero<*const std::ffi::os_str::OsString>** = {
            0x7ffff6c20060
        }
      },
      phantom = **PhantomData<std::ffi::os_str::OsString>**,
      cap = 1, 
      ptr = 0x7ffff6c20060, end = 0x7ffff6c20078},
      _dont_send_or_sync_me = **PhantomData<*mut ()>**
    }
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser6324692View Question on Stackoverflow
Solution 1 - DebuggingCirelli94View Answer on Stackoverflow
Solution 2 - DebuggingcynicView Answer on Stackoverflow
Solution 3 - Debuggingpsiphi75View Answer on Stackoverflow
Solution 4 - DebuggingOtukView Answer on Stackoverflow