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).
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
opt-level = 0
debug = true[profile.release]
opt-level = 3
debug = false
Alternatively you can add this line to your .vimrc or init.vim.
Then change the debugger to
termdebug. This command also accepts the binary file to be debugged as the parameter, e.g.
:Termdebug <executable file with debug symbols>
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 more.
gdb console, use the
file command to add the binary.
(gdb) file target/debug/deps/myapp1-a90fa71044df8bea
And now I can set breakpoints using the
:Run to start debugging.
And now I can do
:Over, :Step, :Continue, :Stop for debugging purpose.
For additional info on this plugin type
gdb also provides a terminal based user interface for debugging. Just run
gdb — tui, add the binary using
file command and you can start debugging.
# gdb --tui
This is a vim plugin that you need to install. I use vim-plug to install it
For Rust, it supports CodeLLDB. Follow the installation instructions to install this adapter. In my
init.vim I configure
vimspector to install the adapters for Python, Golang and Rust (see below), and then install them using
Below is my configuration. You may need to change the configuration to fit your setup.
"shell": "brew --prefix llvm"
let g:vimspector_enable_mappings = 'HUMAN'nmap <leader>dd :call vimspector#Launch()<CR>
nmap <leader>dx :VimspectorReset<CR>
nmap <leader>de :VimspectorEval
nmap <leader>dw :VimspectorWatch
nmap <leader>do :VimspectorShowOutputlet g:vimspector_install_gadgets = [ 'debugpy', 'vscode-go', 'CodeLLDB' ]
vimspector installed I still need to create a
.vimspector.json file specific to the language that I use. This is what I used for my Rust code.
<leader>dd to launch
vimspector website for details related to specific configurations and the default key mappings.
Neovim — Debugging Application
Check out this article on Neovim setup for
nvim-dap and many more.