Subject: Re: bug#30044: Emacs: Gud-mode: Debugging with gud: Window switching problem

From: Noam Postavsky <[email-masked]>
Date: Wed, 10 Jan 2018 20:58:51 -0500

Claus Fischer <[email-masked]> writes:

> If that is so, the solution for me would be simple: let gud not
> remember the window, but let it search for the window with buffer
> name *gud...* and switch to that one. I have only one such buffer.

That is basically what we have already, relevant code excerpts below.
When gdb prints that a breakpoint is hit, Emacs runs the process filter
which gud-mode sets up, `gud-filter'.  It uses `get-buffer-window' to
find the corresponding window and switches to it while calling
`gud-display-frame'.  And then `gud-display-line' shows the source
buffer in any window but the current one (i.e., the one showing the gdb
interaction).

So to fix this, we would need to follow along this code path and see
where things go wrong.  This will be pretty much impossible without a
reliable way of triggering the problem, as suggested in
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=520647#10

    (defun gud-filter (proc string)
      ...
                (with-current-buffer (process-buffer proc)
                    (setq process-window
                          (and gud-last-frame
                               (>= (point) (process-mark proc))
                               (get-buffer-window (current-buffer)))))
      ...
                ;; Put the arrow on the source line.
                ;; This must be outside of the save-excursion
                ;; in case the source file is our current buffer.
                (if process-window
                    (with-selected-window process-window
                      (gud-display-frame))

    (defun gud-display-frame ()
      ...
        (gud-display-line (car gud-last-frame) (cdr gud-last-frame))

    ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
    ;; and that its line LINE is visible.
    ;; Put the overlay-arrow on the line LINE in that buffer.
    ;; Most of the trickiness in here comes from wanting to preserve the current
    ;; region-restriction if that's possible.  We use an explicit display-buffer
    ;; to get around the fact that this is called inside a save-excursion.

    (defun gud-display-line (true-file line)
      (let* ((last-nonmenu-event t)  ; Prevent use of dialog box for questions.
             (buffer
              (with-current-buffer gud-comint-buffer
                (gud-find-file true-file)))
             (window (and buffer
                          (or (get-buffer-window buffer)
                              (display-buffer buffer '(nil (inhibit-same-window . t))))))
      ...