repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/debugging.lua

Raw
File outline
local puk = require 'std.puk'
local dbg = require 'std.dbg'
local map = require 'std.map'

local function configure_lldb(dap)
	--FIXME: i cannot get this shit to work!!! too stoopid!
	local lldb_dap_exe = vim.fn.exepath 'lldb-dap'
	if lldb_dap_exe ~= '' then
		dap.adapters.lldb = {
			type = 'executable',
			command = lldb_dap_exe,
			name = 'lldb',
		}
		dap.configurations.c = {
			{
				name = 'Launch',
				type = 'lldb',
				request = 'launch',
				program = function()
					return coroutine.create(function(dap_run_co)
						-- local path = vim.fn.input({
						-- 	prompt = 'Path to executable: ',
						-- 	default = vim.fn.getcwd() .. '/',
						-- 	completion = 'file'
						-- })

						local path = require("dap.utils").pick_file({ filter = "output/*" })
						local choice = (path and path ~= "") and path or dap.ABORT
						coroutine.resume(dap_run_co, choice)
					end)
				end,
				cwd = '${workspaceFolder}',
				stopOnEntry = false,
				initCommands = {
					'settings set target.x86-disassembly-flavor intel',
					'settings set symbols.auto-download background',
					'settings set symbols.enable-background-lookup true',
					'settings set symbols.enable-lldb-index-cache true'
				},
				env = function()
					local variables = {}
					for k, v in pairs(vim.fn.environ()) do
						table.insert(variables, string.format("%s=%s", k, v))
					end
					return variables
				end,
				--TODO: if exe takes arguments, use vim.input here to provide them
				args = {},

				-- 💀
				-- 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
				--
				-- Otherwise you might get the following error:
				--
				--    Error on launch: Failed to attach to the target process
				--
				-- But you should be aware of the implications:
				-- https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html
				runInTerminal = true,
			},
		}
		dap.configurations.cpp = dap.configurations.c
		dap.configurations.rust = dap.configurations.c
	end
end

puk.defer 'dap' (function(dap)
	dap.set_log_level(dbg.get() and 'TRACE' or 'ERROR')
	puk 'nio'
	local dapui = puk 'dapui'
	local dap_disasm = puk 'dap-disasm'
	local dap_virtual_text = puk 'nvim-dap-virtual-text'
	configure_lldb(dap)
	dapui.setup()
	dap_disasm.setup()
	dap_virtual_text.setup()
	dap.listeners.before.attach.dapui_config = function() dapui.open() end
	dap.listeners.before.launch.dapui_config = function() dapui.open() end
	dap.listeners.before.event_terminated.dapui_config = function() dapui.close() end
	dap.listeners.before.event_exited.dapui_config = function() dapui.close() end
	map.normal {
		description = 'toggle breakpoint',
		category = 'debug',
		keys = '<leader>db',
		command = dap.toggle_breakpoint,
	}
	map.normal {
		description = 'run to cursor',
		category = 'debug',
		keys = '<leader>dr',
		command = dap.run_to_cursor,
	}
	map.normal {
		description = 'run last',
		category = 'debug',
		keys = '<leader>dl',
		command = dap.run_last,
	}
	map.normal {
		description = 'continue/start debugger',
		category = 'debug',
		keys = '<leader>dd',
		command = dap.continue,
	}
	map.normal {
		description = 'step into',
		category = 'debug',
		keys = '<Right>',
		command = dap.step_into,
	}
	map.normal {
		description = 'step over',
		category = 'debug',
		keys = '<Down>',
		command = dap.step_over,
	}
	map.normal {
		description = 'step out',
		category = 'debug',
		keys = '<Left>',
		command = dap.step_out,
	}
	map.normal {
		description = 'restart frame',
		category = 'debug',
		keys = '<Up>',
		command = dap.restart_frame,
	}
	map.normal.visual {
		description = 'eval under cursor',
		category = 'debug',
		keys = '<leader>de',
		command = dapui.eval,
	}
	map.normal {
		description = 'show element',
		category = 'debug',
		keys = '<leader>de',
		command = function() dapui.float_element(nil, { position = 'center' }) end,
	}
end)