DEV Community

Cover image for How to debug rust applications with VIM
Andres Granada
Andres Granada

Posted on

How to debug rust applications with VIM

Some time ago a started to learn Rust language, but recently I changed my edition tool. I migrated from Visual Studio Code to VIM (Neo Vim actually) and the most difficult thing was learning how to debug my Rust code using my new editor. Happily, I found a package to debug Rust applications, its name is Vimspector.

Requirements

According to your operative system, you would need to install different packages.

For Mac OS X:

# Install LLDB application
xcode-select --install
Enter fullscreen mode Exit fullscreen mode

Installing [Neo]Vim package

Package installation depends on the package manager you have been using. In my case, I've been using VIM-Plug, the installation could be done by adding the dependency to the configuration file:

" Using vim-plug
Plug 'puremourning/vimspector'

" Using NeoBundle
NeoBundle 'puremourning/vimspector'

" Using Vundle
Plugin 'puremourning/vimspector'

Enter fullscreen mode Exit fullscreen mode

To enable the plugin shortcuts easily you can add the next line to your configuration file:

" You can use 'VISUAL_STUDIO' or 'HUMAN'
" Visual Studio config avoid to the mapping of <F3> key, sometimes used to map file explorer buffer.
let g:vimspector_enable_mappings = 'VISUAL_STUDIO'
Enter fullscreen mode Exit fullscreen mode

To know the fast config key mappings you can use this link or directly from the code.

Before using the debugger you need to prepare the general debugging configuration file, that file is located at </path/to/vim/plugins>/vimspector/gadgets/<os>/.gadgets.d and its name is lldb-vscode.json, where the /path/to/vim/plugins is the path in your file system where the [Neo]Vim plugins are installed. example paths are /Users/andres/.config/nvim/plugins and /home/andres/.config/nvim/plugins for Mac and Linux respectively.

The content of the file lldb-vscode.json is:

{
  "adapters": {
    "lldb-vscode": {
      "variables": {
        "LLVM": {
          "shell": "brew --prefix llvm"
        }
      },
      "attach": {
        "pidProperty": "pid",
        "pidSelect": "none"
      },
      "command": [
        "${LLVM}/bin/lldb-vscode"
      ],
      "env": {
        "LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY": "YES"
      },
      "name": "lldb"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Also, you need to add your project configuration file .vimspector.json in the root of your Rust project (at the same level as your Cargo.toml file). The initial content could be:

{
  "configurations": {
    "Rust - Test": {
      "adapter": "CodeLLDB",
      "configuration": {
        "request": "launch",
        "program": "${workspaceRoot}/target/debug/<project name>"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Finally, you can create breakpoints in your code, Using VISUAL_STUDIO mode you can create a breakpoint with <F9>.

Finally, this post is only a way to debug in Mac OS X, in a future post I will go deep on how to do it on Linux operative systems.

Read, Enjoy, Share...

Top comments (0)