137

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?

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • you can use gdb. Other than that, I'm working on a stepper for MIR (implemented on top of miri), which will allow you to debug on a kind of virtual machine. – oli_obk Jun 02 '16 at 08:17
  • @ker: Oh! Is miri close enough to completion already? I thought it was still in the early stages. – Matthieu M. Jun 02 '16 at 09:43
  • @MatthieuM.: there are still some things that don't work yet (e.g. function pointers), but it's slowly getting there. – oli_obk Jun 02 '16 at 10:21
  • 2
    This was [cross-posted to Reddit](https://www.reddit.com/r/rust/comments/4m66ja/step_by_step_interactive_debugger_for_rust/?ref=share&ref_source=link) – Shepmaster Jun 02 '16 at 14:09
  • 6
    @Dimon it's considered Stack Overflow (and Reddit) etiquette to inform **future searchers** of other places that an answer may be found. That way, they have a better chance of getting useful information. It also potentially saves an **answerer** time, if what they were going to say is already covered in a different location. – Shepmaster Jun 03 '16 at 01:08

5 Answers5

100

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-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:

Cirelli94
  • 1,714
  • 1
  • 15
  • 24
  • Nice answer! It would be great if you could explain why you've added these blog posts, possibly quoting a paragraph to show their relevance. – Kyll Sep 11 '18 at 11:13
  • 1
    Well, I added these posts as real life case of doing it! – Cirelli94 Oct 08 '18 at 07:11
39

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: How to set up GDB for debugging Rust programs in Windows?). Please note that you're likely to run into 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.

Community
  • 1
  • 1
cynic
  • 5,305
  • 1
  • 24
  • 40
  • 2
    And with `gdb 7.12`, there's now proper support, not just "works because it's compiled to native". – domen Feb 06 '17 at 13:51
9

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.

psiphi75
  • 1,985
  • 1
  • 24
  • 36
  • 4
    Replying to myself, although gdbgui is useful, I have found the VS Code debugger works very well now. – psiphi75 Mar 13 '19 at 18:43
5

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 ()>**
    }
  }
}
piertoni
  • 1,933
  • 1
  • 18
  • 30
Otuk
  • 420
  • 1
  • 5
  • 6
5

An answer that start with:

  1. Install IDE xyz

... can't be a solution at all.

Go to your project folder and start rust-gdb target/debug/your_executable then pick some lines where you want to break. Hit 'run' ...

When you don't have an IDE in place (otherwise you wouldn't ask, i guess) it is the best in a Terminal/VIM users world.

Here a some hints how to use:

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Docker Rocker
  • 59
  • 1
  • 3