improve and sort-of clean up dap/gdb debugging config and keymaps

the main goal here is to provide common keybinds for controlling a
debugger session between dap-mode and gdb-mi/gud ... or at least, as
common as is possible
This commit is contained in:
Gered 2024-12-09 20:48:06 -05:00
parent 38626cd6e0
commit 28d6f2feb4
3 changed files with 104 additions and 80 deletions

103
emacs.d/debugging.el Normal file
View file

@ -0,0 +1,103 @@
;; NOTE: the "design" of this is mainly to work around the issue of "dap-mode"
;; generally being active all of the time, while "gud-minor-mode" is only
;; active while in a gdb session. so, the preferred(?) method of setting
;; keybinds in (use-package ... :bind ...) is not really doable from what
;; i've seen.
;; setting global keybinds and using these functions to wrap high-level
;; tasks common in any debugging session is my current way to work around
;; this while providing common keybinds whether debugging via dap or gud.
(defun gered/debug-quit ()
"Quits out of the current debugging session."
(interactive)
(cond (gud-minor-mode (gud-basic-call "quit"))
(dap-mode (dap-disconnect (dap--cur-active-session-or-die)))))
(defun gered/debug-start ()
"Starts execution of the program in the current debugging session."
(interactive)
(cond (gud-minor-mode (gud-basic-call "start"))
(dap-mode nil)))
(defun gered/debug-stop ()
"Terminates execution of the running program within the current debugging session."
(interactive)
(cond (gud-minor-mode (gud-basic-call "kill"))
(dap-mode (dap-disconnect (dap--cur-active-session-or-die)))))
(defun gered/debug-continue ()
"Continued execution of the running program within the current debugging session."
(interactive)
(cond (gud-minor-mode (gud-cont 1))
(dap-mode (dap-continue (dap--cur-active-session-or-die)
(dap--debug-session-thread-id (dap--cur-active-session-or-die))))))
(defun gered/debug-step-into ()
"Steps execution to the next line."
(interactive)
(cond (gud-minor-mode (gud-step 1))
(dap-mode (dap-step-in (dap--cur-session-or-die)))))
(defun gered/debug-step-over ()
"Steps exection to the next line, skipping over functions."
(interactive)
(cond (gud-minor-mode (gud-next 1))
(dap-mode (dap-next (dap--cur-active-session-or-die)))))
(defun gered/debug-step-out ()
"Steps execution out of the current function."
(interactive)
(cond (gud-minor-mode (gud-finish 1))
(dap-mode (dap-step-out (dap--cur-session-or-die)))))
(defun gered/debug-set-breakpoint ()
"Sets a debugger breakpoint on the current line."
(interactive)
(cond (gud-minor-mode (gud-break 1))
(dap-mode (dap-breakpoint-add))))
(defun gered/debug-remove-breakpoint ()
"Removes a debugger breakpoint from the current line."
(interactive)
(cond (gud-minor-mode (gud-remove 1))
(dap-mode (dap-breakpoint-delete
(-some->> (dap--get-breakpoints)
(gethash buffer-file-name)
dap--get-breakpoint-at-point)
buffer-file-name))))
(defun gered/debug-switch-stack-frame ()
(interactive)
(cond (gud-minor-mode nil)
(dap-mode (dap-switch-stack-frame))))
(use-package gdb-mi
:config
(setq gdb-many-windows t)
(setq gdb-restore-window-configuration-after-quit 'if-gdb-many-windows)
(setq gdb-debuginfod-enable-setting nil)
(gud-tooltip-mode t))
(use-package dap-mode
:ensure t
:bind
( :map dap-mode
(("M-<prior>" . 'dap-up-stack-frame)
("M-<next>" . 'dap-down-stack-frame)))
:commands dap-mode
:init
(setq dap-auto-configure-features '(sessions locals tooltip breakpoints expressions))
:config
(use-package dap-gdb))
;; TODO: figure out something better than adding all as global keymaps?
(global-set-key (kbd "C-q") 'gered/debug-quit)
(global-set-key (kbd "<f5>") 'gered/debug-start)
(global-set-key (kbd "S-<f5>") 'gered/debug-stop)
(global-set-key (kbd "<f9>") 'gered/debug-continue)
(global-set-key (kbd "<f7>") 'gered/debug-step-into)
(global-set-key (kbd "<f8>") 'gered/debug-step-over)
(global-set-key (kbd "S-<f8>") 'gered/debug-step-out)
(global-set-key (kbd "C-c C-g b") 'gered/debug-set-breakpoint)
(global-set-key (kbd "C-c C-g B") 'gered/debug-remove-breakpoint)
(global-set-key (kbd "C-c C-g s") 'gered/debug-switch-stack-frame)

View file

@ -14,64 +14,6 @@
(treesit-auto-add-to-auto-mode-alist 'all) (treesit-auto-add-to-auto-mode-alist 'all)
(global-treesit-auto-mode)) (global-treesit-auto-mode))
(defun gud-quit ()
"Quits out of the current GDB session."
(interactive)
(gud-basic-call "quit"))
(defun gud-start ()
"Starts execution of the program in GDB."
(interactive)
(gud-basic-call "start"))
(defun gud-kill ()
"Terminates execution of the running program in GDB."
(interactive)
(gud-basic-call "kill"))
(defun gud-step-into ()
"Steps execution to the next line."
(interactive)
(gud-step 1))
(defun gud-step-over ()
"Steps execution to the next line, skipping over functions."
(interactive)
(gud-next 1))
(defun gud-step-out ()
"Steps execution out of the current function."
(interactive)
(gud-finish 1))
(defun gud-set-breakpoint ()
"Sets a debugger breakpoint on the current line."
(interactive)
(gud-break 1))
(defun gud-remove-breakpoint ()
"Removes a debugger breakpoint from the current line."
(interactive)
(gud-remove 1))
(use-package gdb-mi
:bind
( :map gud-minor-mode-map
("<f5>" . 'gud-start)
("S-<f5>" . 'gud-kill)
("<f9>" . 'gud-cont)
("<f7>" . 'gud-step-into)
("<f8>" . 'gud-step-over)
("S-<f8>" . 'gud-step-out)
("C-c C-d b" . 'gud-set-breakpoint)
("C-c C-d B" . 'gud-remove-breakpoint)
("C-q" . 'gud-quit))
:config
(setq gdb-many-windows t)
(setq gdb-restore-window-configuration-after-quit 'if-gdb-many-windows)
(setq gdb-debuginfod-enable-setting nil)
(gud-tooltip-mode t))
(defun gered/enable-lsp () (defun gered/enable-lsp ()
(interactive) (interactive)
(add-hook 'prog-mode-hook 'lsp) (add-hook 'prog-mode-hook 'lsp)
@ -107,25 +49,3 @@
:ensure t :ensure t
:after lsp-mode :after lsp-mode
:commands lsp-treemacs-errors-list) :commands lsp-treemacs-errors-list)
(use-package dap-mode
:ensure t
:bind
( :map dap-mode-map
("<f9>" . 'dap-continue)
("<f7>" . 'dap-step-in)
("<f8>" . 'dap-next)
("S-<f8>" . 'dap-step-out)
("C-c C-d b" . 'dap-breakpoint-add)
("C-c C-d B" . 'dap-breakpoint-delete)
("C-c C-d e" . 'dap-eval-thing-at-point)
("C-c C-d E" . 'dap-eval)
("C-c C-d s" . 'dap-switch-stack-frame)
("C-q" . 'dap-disconnect)
("M-<prior>" . 'dap-up-stack-frame)
("M-<next>" . 'dap-down-stack-frame))
:commands dap-mode
:init
(setq dap-auto-configure-features '(sessions locals tooltip breakpoints expressions))
:config
(use-package dap-gdb))

View file

@ -5,6 +5,7 @@
"minibuffer.el" "minibuffer.el"
"editing.el" "editing.el"
"dev.el" "dev.el"
"debugging.el"
"git.el" "git.el"
"lang-modes.el" "lang-modes.el"
"keybinds.el" "keybinds.el"