Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make debug functionality better. #777

Merged
merged 18 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lua/modules/configs/tool/dap/dap-cpptools.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local dap = require("dap")

-- https://github.com/mfussenegger/nvim-dap/wiki/C-C---Rust-(gdb-via--vscode-cpptools)
local gdb_abspath = vim.fn.exepath("gdb")
local cpptools_abspath = vim.fn.stdpath("data") .. "/dap/cpptools/extension/debugAdapters/bin/OpenDebugAD7"
local path_to_program = require("modules.configs.tool.dap.dap-utils").path_to_program()
local get_env = require("modules.configs.tool.dap.dap-utils").get_env()
local setupCommands = {
text = "-enable-pretty-printing",
description = "enable pretty printing",
ignoreFailures = false,
}

dap.adapters.cppdbg = {
id = "cppdbg",
type = "executable",
command = cpptools_abspath,
}
dap.configurations.cpp = {
{
name = "Launch file",
type = "cppdbg",
request = "launch",
program = path_to_program,
cwd = "${workspaceFolder}",
env = get_env,
setupCommands = setupCommands,
stopAtEntry = false,
},
{
name = "Attach to gdbserver :1234",
type = "cppdbg",
request = "launch",
MIMode = "gdb",
miDebuggerServerAddress = "localhost:1234",
miDebuggerPath = gdb_abspath,
program = path_to_program,
cwd = "${workspaceFolder}",
env = get_env,
setupCommands = setupCommands,
},
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
34 changes: 28 additions & 6 deletions lua/modules/configs/tool/dap/dap-debugpy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,41 @@ local function isempty(s)
return s == nil or s == ""
end

dap.adapters.python = {
type = "executable",
command = "python",
args = { "-m", "debugpy.adapter" },
}
local debugpy_abspath = vim.fn.stdpath("data") .. "/dap/debugpy/"

-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#python
dap.adapters.python = function(cb, config)
if config.request == "attach" then
---@diagnostic disable-next-line: undefined-field
local port = (config.connect or config).port
---@diagnostic disable-next-line: undefined-field
local host = (config.connect or config).host or "127.0.0.1"
cb({
type = "server",
port = assert(port, "`connect.port` is required for a python `attach` configuration"),
host = host,
options = {
source_filetype = "python",
},
})
else
cb({
type = "executable",
command = debugpy_abspath .. "bin/python",
args = { "-m", "debugpy.adapter" },
options = {
source_filetype = "python",
},
})
end
end
dap.configurations.python = {
{
-- The first three options are required by nvim-dap
type = "python", -- the type here established the link to the adapter definition: `dap.adapters.python`
request = "launch",
name = "Launch file",
-- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options

program = "${file}", -- This configuration will launch the current file if used.
pythonPath = function()
if not isempty(vim.env.CONDA_PREFIX) then
Expand Down
19 changes: 10 additions & 9 deletions lua/modules/configs/tool/dap/dap-lldb.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
local dap = require("dap")

-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#ccrust-via-lldb-vscode
local lldb_abspath = vim.fn.exepath("lldb-vscode")
ayamir marked this conversation as resolved.
Show resolved Hide resolved
local path_to_program = require("modules.configs.tool.dap.dap-utils").path_to_program()
local get_env = require("modules.configs.tool.dap.dap-utils").get_env()
local input_args = require("modules.configs.tool.dap.dap-utils").input_args()

dap.adapters.lldb = {
type = "executable",
command = "lldb-vscode",
command = lldb_abspath,
name = "lldb",
}
dap.configurations.cpp = {
{
name = "Launch",
type = "lldb",
request = "launch",
program = function()
return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
end,
program = path_to_program,
cwd = "${workspaceFolder}",
env = get_env,
args = input_args,
stopOnEntry = false,
args = function()
local input = vim.fn.input("Input args: ")
return vim.fn.split(input, " ", true)
end,

-- if you change `runInTerminal` to true, you might need to change the yama/ptrace_scope setting:
--
-- echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
Expand Down
20 changes: 20 additions & 0 deletions lua/modules/configs/tool/dap/dap-utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local M = {}

function M.path_to_program()
return vim.fn.input("Path to program: ", vim.fn.expand("%:p:r"), "file")
end

function M.get_env()
local variables = {}
for k, v in pairs(vim.fn.environ()) do
table.insert(variables, string.format("%s=%s", k, v))
end
return variables
end

function M.input_args()
local input = vim.fn.input("Input args: ")
return vim.fn.split(input, " ", true)
end

return M
3 changes: 2 additions & 1 deletion lua/modules/configs/tool/dap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ return function()
vim.fn.sign_define("DapLogPoint", { text = icons.dap.LogPoint, texthl = "DapLogPoint", linehl = "", numhl = "" })

-- Config lang adaptors
require("tool.dap.dap-lldb")
-- require("tool.dap.dap-lldb")
require("tool.dap.dap-cpptools")
require("tool.dap.dap-debugpy")
require("tool.dap.dap-dlv")
end