1

We can define a bunch of bindings for gud-mode through gud-mode-map. But it seems these bindings only hold when the active window is one of the dedicated gdb windows (or simply the comint window if you're not using many-windows).

I can't find any keymap that is active for the source code being debugged -- the only minor mode that seems to be active is gdb-many-windows-mode. That does not have a keymap however. So I tried redefining it:

(define-minor-mode gdb-many-windows
  "Redefine many windows to include a keymap"
  :global t
  :group 'gdb
  :version "22.1"
  :keymap my-customized-gdb-mode-map
  (if (and gud-comint-buffer
           (buffer-name gud-comint-buffer))
      (ignore-errors
        (gdb-restore-windows))))

But then I realized this is an always-active global mode. So how would I go about customizing keybindngs that are active for the source buffer??

1 Answer 1

1

The right place is gud-minor-mode-map.

I have got the following bindings in my setup. Pick whatever suits you for your setup.

(defun gdbTZA-gud-run-or-cont (arg)
  "Combination of `gud-run' and `gud-cont'.
If the debugged program is already running use `gdb-cont' and use `gdb-run' otherwise."
  (interactive "p")
  (if (assoc-string gdb-inferior-status '("breakpoint-hit"
                                          "end-stepping-range"
                                          "watchpoint-trigger"
                                          "read-watchpoint-trigger"
                                          "access-watchpoint-trigger"
                                          "function-finished"
                                          "location-reached"
                                          "watchpoint-scope"
                                          "signal-received"))
      (gud-cont arg)
    (gud-run arg)))

(define-key gud-minor-mode-map (kbd "<f4>") #'gud-print)
(define-key gud-minor-mode-map (kbd "<f5>") #'gdbTZA-gud-run-or-cont)
(define-key gud-minor-mode-map (kbd "S-<f5>") #'gud-kill)
(define-key gud-minor-mode-map (kbd "<f6>") #'gdb-many-windows)
(define-key gud-minor-mode-map (kbd "<f9>") #'gud-break)
(define-key gud-minor-mode-map (kbd "<C-f9>") #'gud-tbreak)
(define-key gud-minor-mode-map (kbd "<f10>") #'gud-next)
(define-key gud-minor-mode-map (kbd "C-<f10>") #'gud-until)
(define-key gud-minor-mode-map (kbd "S-C-<f10>") #'gud-tbreak-jump)
(define-key gud-minor-mode-map (kbd "<f11>") #'gud-step)
(define-key gud-minor-mode-map (kbd "S-<f11>") #'gud-finish)

(easy-menu-add-item gud-menu-map nil ["Run Until" gud-until t] "Finish Function")
(easy-menu-add-item gud-menu-map nil ["Add Break At Regexp" gdbTZA-break-at-regexp t] "Temporary Breakpoint")

(defvar gdbTZA-save-point nil
  "List of buffer and point position before command.")

(defun gdbTZA-gud-clear-point (&rest args)
  (setq gdbTZA-save-point nil))

(defun gdbTZA-gud-save-point (&rest args)
  "Save buffer and point in `gdbTZA-save-point' before a gui command."
  (when (and gud-minor-mode
             (null (eq (current-buffer) gud-comint-buffer)))
    (setq gdbTZA-save-point (list (current-buffer) (point)))))

(defun gdbTZA-gud-restore-point (&rest args)
  "Restore point in buffer from `gdbTZA-save-point'."
  (when (and gdbTZA-save-point
             (buffer-live-p (car gdbTZA-save-point)))
    (with-current-buffer (car gdbTZA-save-point)
      (goto-char (nth 1 gdbTZA-save-point))
      )))

(defun gdbTZA-gud-goto-arrow (&rest args)
  "Follow marker even if it goes off screen."
  (when (markerp gud-overlay-arrow-position)
        (with-current-buffer (marker-buffer gud-overlay-arrow-position)
          (goto-char gud-overlay-arrow-position))))

(advice-add #'gud-call :before #'gdbTZA-gud-clear-point)
(advice-add #'gud-print :after #'gdbTZA-gud-save-point)
(advice-add #'gud-break :after #'gdbTZA-gud-save-point)
(advice-add #'gdb-done :after #'gdbTZA-gud-restore-point)

(defun gdbTZA-gdb-mode-hook-function ()
  "Add `gud-tbreak-jump' to the list of gud-gdb functions."
  (gud-def gud-tbreak-jump (if (not (string-match "Disassembly" mode-name))
                   (progn
                 (gud-call "-exec-tbreak %f:%l" arg)
                 (gud-call "-exec-jump %f:%l" arg))
                 (save-excursion
                   (beginning-of-line)
                   (forward-char 2)
                   (gud-call "-exec-tbreak *%a" arg)
                   (gud-call "-exec-jump *%a" arg)))
       "\C-J" "Advance to current line or address."))

(add-hook 'gdb-mode-hook #'gdbTZA-gdb-mode-hook-function)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.