neovim, dap and gdb 14.1
At the beginning of December 2023 gdb version 14.1 was released. The feature I’ve waited for is support for the the Debug Adapter Protocol. Today I finally looked into this and got gdb working with nvim-dap.
If you already have setup nvim-dap, it is pretty easy to add support for C/C++ with gdb. First you need to enable DAP for the filetype. I load DAP with lazy only for filetypes I use it for.
Then I created the file plugins/dap/c.lua with
the following content:
local ok, dap = pcall(require, 'dap')
if not ok then
return
end
--
-- See
-- https://sourceware.org/gdb/current/onlinedocs/gdb.html/Interpreters.html
-- https://sourceware.org/gdb/current/onlinedocs/gdb.html/Debugger-Adapter-Protocol.html
dap.adapters.gdb = {
id = 'gdb',
type = 'executable',
command = 'gdb',
args = { '--quiet', '--interpreter=dap' },
}
dap.configurations.c = {
{
name = 'Run executable (GDB)',
type = 'gdb',
request = 'launch',
-- This requires special handling of 'run_last', see
-- https://github.com/mfussenegger/nvim-dap/issues/1025#issuecomment-1695852355
program = function()
local path = vim.fn.input({
prompt = 'Path to executable: ',
default = vim.fn.getcwd() .. '/',
completion = 'file',
})
return (path and path ~= '') and path or dap.ABORT
end,
},
{
name = 'Run executable with arguments (GDB)',
type = 'gdb',
request = 'launch',
-- This requires special handling of 'run_last', see
-- https://github.com/mfussenegger/nvim-dap/issues/1025#issuecomment-1695852355
program = function()
local path = vim.fn.input({
prompt = 'Path to executable: ',
default = vim.fn.getcwd() .. '/',
completion = 'file',
})
return (path and path ~= '') and path or dap.ABORT
end,
args = function()
local args_str = vim.fn.input({
prompt = 'Arguments: ',
})
return vim.split(args_str, ' +')
end,
},
{
name = 'Attach to process (GDB)',
type = 'gdb',
request = 'attach',
processId = require('dap.utils').pick_process,
},
}
And loaded it with the following snippet:
if vim.fn.executable('gdb') == 1 then
require('plugins.dap.c')
end
My neovim dotfiles are here.