Setting up Neovim for Rust Debugging — termdebug and vimspector
Overview
For Vim/Neovim, setting up Debug Adapter Protocol (DAP) for your preferred languages might not be so straight forward. Plugins like vimspector and nvim-dap which support DAP are available but the setup is not as easy compared to Visual Studio Code.
In my previous article I demonstrated to you how to debug Rust code using VS Code. In this article I will show you how to debug Rust code in Neovim using termdebug (built-in plugin bundled with Vim starting 8.1) and vimspector (a vim plugin supports DAP).
cargo.toml
I will be using cargo to manage the project and packages. To ensure the debugging symbols are generated correctly, add these lines in cargo.toml
. If you are using a workspace, put them in the root cargo.toml
.
[profile.dev]
opt-level = 0
debug = true[profile.release]
opt-level = 3
debug = false
termdebug
termdebug is available starting Vim 8.1 and ported to Neovim. To use termdebug for Rust debugging, from Neovim
:packadd termdebug
Alternatively you can add this line to your .vimrc or init.vim.
Then change the debugger to rust-gdb
.
:let termdebugger="rust-gdb"
Start termdebug
. This command also accepts the binary file to be debugged as the parameter, e.g. :Termdebug <executable file with debug symbols>
:Termdebug
To prevent gdb
from starting a shell, enter the line below. Alternatively you can put it in .gdbinit
in the home folder.
(gdb) set startup-with-shell off
For macOS user, if you encounter the following errors, refer to this link to give gdb
permission to control other processes.
Starting program: /xx/xxx
Unable to find Mach task port for process-id 48815: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
I need to perform a :Cargo build
to generate the binary with debug symbols. I use rust.vim plugin that provides Rust file detection, syntax highlighting, formatting, Syntastic integration, and…