(scheme-mode-syntax-table): Mark ; as being also the
[bpt/emacs.git] / lisp / progmodes / gdb-ui.el
index b376c38..6c960c4 100644 (file)
@@ -4,7 +4,7 @@
 ;; Maintainer: FSF
 ;; Keywords: unix, tools
 
-;; Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
+;; Copyright (C) 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
 
 ;; This file is part of GNU Emacs.
 
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
-;; This mode acts as a graphical user interface to GDB. You can interact with
+;; This mode acts as a graphical user interface to GDB.  You can interact with
 ;; GDB through the GUD buffer in the usual way, but there are also further
 ;; buffers which control the execution and describe the state of your program.
 ;; It separates the input/output of your program from that of GDB, if
-;; required, and displays expressions and their current values in their own
-;; buffers. It also uses features of Emacs 21 such as the display margin for
-;; breakpoints, and the toolbar (see the GDB Graphical Interface section in
-;; the Emacs info manual).
-
-;; Start the debugger with M-x gdba.
-
-;; This file has evolved from gdba.el from GDB 5.0 written by Tom Lord and Jim
-;; Kingdon and uses GDB's annotation interface. You don't need to know about
-;; annotations to use this mode as a debugger, but if you are interested
-;; developing the mode itself, then see the Annotations section in the GDB
-;; info manual. Some GDB/MI commands are also used through th CLI command
-;; 'interpreter mi <mi-command>'.
+;; required, and watches expressions in the speedbar.  It also uses features of
+;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
+;; (see the GDB Graphical Interface section in the Emacs info manual).
+
+;; By default, M-x gdb will start the debugger. However, if you have customised
+;; gud-gdb-command-name, then start it with M-x gdba.
+
+;; This file has evolved from gdba.el that was included with GDB 5.0 and
+;; written by Tom Lord and Jim Kingdon.  It uses GDB's annotation interface.
+;; You don't need to know about annotations to use this mode as a debugger,
+;; but if you are interested developing the mode itself, then see the
+;; Annotations section in the GDB info manual.
+;;
+;; GDB developers plan to make the annotation interface obsolete.  A new
+;; interface called GDB/MI (machine interface) has been designed to replace
+;; it.  Some GDB/MI commands are used in this file through the CLI command
+;; 'interpreter mi <mi-command>'.  A file called gdb-mi.el is included with
+;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB.  It is
+;; still under development and is part of a process to migrate Emacs from
+;; annotations to GDB/MI.
+;;
+;; Windows Platforms:
+;;
+;; If you are using Emacs and GDB on Windows you will need to flush the buffer
+;; explicitly in your program if you want timely display of I/O in Emacs.
+;; Alternatively you can make the output stream unbuffered, for example, by
+;; using a macro:
+;;
+;;           #ifdef UNBUFFERED
+;;          setvbuf (stdout, (char *) NULL, _IONBF, 0);
+;;          #endif
+;;
+;; and compiling with -DUNBUFFERED while debugging.
 ;;
 ;; Known Bugs:
 ;;
+;; TODO:
+;; 1) Use MI command -data-read-memory for memory window.
+;; 2) Highlight changed register values (use MI commands
+;;    -data-list-register-values and -data-list-changed-registers instead
+;;    of 'info registers'.
+;; 3) Use tree-widget.el instead of the speedbar for watch-expressions?
+;; 4) Mark breakpoint locations on scroll-bar of source buffer?
+;; 5) After release of 22.1 use '-var-list-children --all-values'
+;;    and '-stack-list-locals 2' which need GDB 6.1 onwards.
 
 ;;; Code:
 
 (require 'gud)
 
-(defvar gdb-current-address "main" "Initialisation for Assembler buffer.")
-(defvar gdb-previous-address nil)
+(defvar tool-bar-map)
+
+(defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
+(defvar gdb-previous-frame-address nil)
+(defvar gdb-memory-address "main")
 (defvar gdb-previous-frame nil)
-(defvar gdb-current-frame "main")
+(defvar gdb-selected-frame nil)
+(defvar gdb-frame-number nil)
 (defvar gdb-current-language nil)
-(defvar gdb-view-source t "Non-nil means that source code can be viewed.")
-(defvar gdb-selected-view 'source "Code type that user wishes to view.")
-(defvar gdb-var-list nil "List of variables in watch window")
-(defvar gdb-var-changed nil "Non-nil means that gdb-var-list has changed.")
+(defvar gdb-var-list nil "List of variables in watch window.")
+(defvar gdb-var-changed nil "Non-nil means that `gdb-var-list' has changed.")
+(defvar gdb-main-file nil "Source file from which program execution begins.")
 (defvar gdb-buffer-type nil)
 (defvar gdb-overlay-arrow-position nil)
-(defvar gdb-variables '()
-  "A list of variables that are local to the GUD buffer.")
 (defvar gdb-server-prefix nil)
+(defvar gdb-flush-pending-output nil)
+(defvar gdb-location-alist nil
+  "Alist of breakpoint numbers and full filenames.")
+(defvar gdb-find-file-unhook nil)
+(defvar gdb-active-process nil "GUD tooltips display variable values when t, \
+and #define directives otherwise.")
+(defvar gdb-error "Non-nil when GDB is reporting an error.")
+(defvar gdb-macro-info nil
+  "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
+
+(defvar gdb-buffer-type nil
+  "One of the symbols bound in `gdb-buffer-rules'.")
+
+(defvar gdb-input-queue ()
+  "A list of gdb command objects.")
+
+(defvar gdb-prompting nil
+  "True when gdb is idle with no pending input.")
+
+(defvar gdb-output-sink 'user
+  "The disposition of the output of the current gdb command.
+Possible values are these symbols:
+
+    `user' -- gdb output should be copied to the GUD buffer
+              for the user to see.
+
+    `inferior' -- gdb output should be copied to the inferior-io buffer.
+
+    `pre-emacs' -- output should be ignored util the post-prompt
+                   annotation is received.  Then the output-sink
+                  becomes:...
+    `emacs' -- output should be collected in the partial-output-buffer
+              for subsequent processing by a command.  This is the
+              disposition of output generated by commands that
+              gdb mode sends to gdb on its own behalf.
+    `post-emacs' -- ignore output until the prompt annotation is
+                   received, then go to USER disposition.
+
+gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
+\(`user' and `emacs').")
+
+(defvar gdb-current-item nil
+  "The most recent command item sent to gdb.")
+
+(defvar gdb-pending-triggers '()
+  "A list of trigger functions that have run later than their output
+handlers.")
+
+;; end of gdb variables
+
 ;;;###autoload
 (defun gdba (command-line)
   "Run gdb on program FILE in buffer *gud-FILE*.
@@ -72,19 +151,19 @@ The directory containing FILE becomes the initial working directory
 and source-file directory for your debugger.
 
 If `gdb-many-windows' is nil (the default value) then gdb just
-pops up the GUD buffer unless `gdb-show-main' is t. In this case
+pops up the GUD buffer unless `gdb-show-main' is t.  In this case
 it starts with two windows: one displaying the GUD buffer and the
 other with the source file with the main routine of the inferior.
 
 If `gdb-many-windows' is t, regardless of the value of
 `gdb-show-main', the layout below will appear unless
 `gdb-use-inferior-io-buffer' is nil when the source buffer
-occupies the full width of the frame. Keybindings are given in
+occupies the full width of the frame.  Keybindings are given in
 relevant buffer.
 
 Watch expressions appear in the speedbar/slowbar.
 
-The following interactive lisp functions help control operation :
+The following commands help control operation :
 
 `gdb-many-windows'    - Toggle the number of windows gdb uses.
 `gdb-restore-windows' - To restore the window layout.
@@ -114,8 +193,7 @@ detailed description of this mode.
  RET      gdb-frames-select       | SPC    gdb-toggle-breakpoint
                                   | RET    gdb-goto-breakpoint
                                   |   d    gdb-delete-breakpoint
----------------------------------------------------------------------
-"
+---------------------------------------------------------------------"
   ;;
   (interactive (list (gud-query-cmdline 'gdba)))
   ;;
@@ -125,22 +203,124 @@ detailed description of this mode.
 
 (defvar gdb-debug-log nil)
 
+;;;###autoload
 (defcustom gdb-enable-debug-log nil
- "Non-nil means record the process input and output in `gdb-debug-log'."
 "Non-nil means record the process input and output in `gdb-debug-log'."
   :type 'boolean
-  :group 'gud)
+  :group 'gud
+  :version "22.1")
 
 (defcustom gdb-use-inferior-io-buffer nil
- "Non-nil means display output from the inferior in a separate buffer."
 "Non-nil means display output from the inferior in a separate buffer."
   :type 'boolean
-  :group 'gud)
+  :group 'gud
+  :version "22.1")
+
+(defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
+  "Shell command for generating a list of defined macros in a source file.
+This list is used to display the #define directive associated
+with an identifier as a tooltip.  It works in a debug session with
+GDB, when gud-tooltip-mode is t.
+
+Set `gdb-cpp-define-alist-flags' for any include paths or
+predefined macros."
+  :type 'string
+  :group 'gud
+  :version "22.1")
+
+(defcustom gdb-cpp-define-alist-flags ""
+  "Preprocessor flags for `gdb-cpp-define-alist-program'."
+  :type 'string
+  :group 'gud
+  :version "22.1")
+
+(defcustom gdb-show-main nil
+  "Non-nil means display source file containing the main routine at startup.
+Also display the main routine in the disassembly buffer if present."
+  :type 'boolean
+  :group 'gud
+  :version "22.1")
+
+(defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
+
+(defun gdb-create-define-alist ()
+  "Create an alist of #define directives for GUD tooltips."
+  (let* ((file (buffer-file-name))
+        (output
+         (with-output-to-string
+           (with-current-buffer standard-output
+             (call-process shell-file-name
+                           (if (file-exists-p file) file nil)
+                           (list t nil) nil "-c"
+                           (concat gdb-cpp-define-alist-program " "
+                                   gdb-cpp-define-alist-flags)))))
+       (define-list (split-string output "\n" t))
+       (name))
+    (setq gdb-define-alist nil)
+    (dolist (define define-list)
+      (setq name (nth 1 (split-string define "[( ]")))
+      (push (cons name define) gdb-define-alist))))
+
+(defun gdb-tooltip-print ()
+  (tooltip-show
+   (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
+     (let ((string (buffer-string)))
+       ;; remove newline for gud-tooltip-echo-area
+       (substring string 0 (- (length string) 1))))
+   (or gud-tooltip-echo-area tooltip-use-echo-area)))
+
+;; If expr is a macro for a function don't print because of possible dangerous
+;; side-effects. Also printing a function within a tooltip generates an
+;; unexpected starting annotation (phase error).
+(defun gdb-tooltip-print-1 (expr)
+  (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
+    (goto-char (point-min))
+    (if (search-forward "expands to: " nil t)
+       (unless (looking-at "\\S+.*(.*).*")
+         (gdb-enqueue-input
+          (list  (concat gdb-server-prefix "print " expr "\n")
+                 'gdb-tooltip-print))))))
+
+(defun gdb-set-gud-minor-mode (buffer)
+  "Set `gud-minor-mode' from find-file if appropriate."
+  (goto-char (point-min))
+  (unless (search-forward "No source file named " nil t)
+    (condition-case nil
+       (gdb-enqueue-input
+        (list (concat gdb-server-prefix "info source\n")
+              `(lambda () (gdb-set-gud-minor-mode-1 ,buffer))))
+      (error (setq gdb-find-file-unhook t)))))
+
+(defun gdb-set-gud-minor-mode-1 (buffer)
+  (goto-char (point-min))
+  (when (and (search-forward "Located in " nil t)
+            (looking-at "\\S-+")
+            (string-equal (buffer-file-name buffer)
+                          (match-string 0)))
+    (with-current-buffer buffer
+      (set (make-local-variable 'gud-minor-mode) 'gdba)
+      (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
+      (when gud-tooltip-mode
+       (make-local-variable 'gdb-define-alist)
+       (gdb-create-define-alist)
+       (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
+
+(defun gdb-set-gud-minor-mode-existing-buffers ()
+  (dolist (buffer (buffer-list))
+    (let ((file (buffer-file-name buffer)))
+      (if file
+       (progn
+         (gdb-enqueue-input
+          (list (concat gdb-server-prefix "list "
+                        (file-name-nondirectory file) ":1\n")
+                `(lambda () (gdb-set-gud-minor-mode ,buffer)))))))))
 
 (defun gdb-ann3 ()
   (setq gdb-debug-log nil)
   (set (make-local-variable 'gud-minor-mode) 'gdba)
   (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
   ;;
-  (gud-def gud-break (if (not (string-equal mode-name "Machine"))
+  (gud-def gud-break (if (not (string-match "Machine" mode-name))
                         (gud-call "break %f:%l" arg)
                       (save-excursion
                         (beginning-of-line)
@@ -148,7 +328,7 @@ detailed description of this mode.
                         (gud-call "break *%a" arg)))
           "\C-b" "Set breakpoint at current line or address.")
   ;;
-  (gud-def gud-remove (if (not (string-equal mode-name "Machine"))
+  (gud-def gud-remove (if (not (string-match "Machine" mode-name))
                          (gud-call "clear %f:%l" arg)
                        (save-excursion
                          (beginning-of-line)
@@ -156,7 +336,7 @@ detailed description of this mode.
                          (gud-call "clear *%a" arg)))
           "\C-d" "Remove breakpoint at current line or address.")
   ;;
-  (gud-def gud-until  (if (not (string-equal mode-name "Machine"))
+  (gud-def gud-until  (if (not (string-match "Machine" mode-name))
                          (gud-call "until %f:%l" arg)
                        (save-excursion
                          (beginning-of-line)
@@ -165,29 +345,40 @@ detailed description of this mode.
           "\C-u" "Continue to current line or address.")
 
   (define-key gud-minor-mode-map [left-margin mouse-1]
-    'gdb-mouse-toggle-breakpoint)
+    'gdb-mouse-set-clear-breakpoint)
   (define-key gud-minor-mode-map [left-fringe mouse-1]
+    'gdb-mouse-set-clear-breakpoint)
+  (define-key gud-minor-mode-map [left-margin mouse-3]
     'gdb-mouse-toggle-breakpoint)
+;  Currently only works in margin.
+;  (define-key gud-minor-mode-map [left-fringe mouse-3]
+;    'gdb-mouse-toggle-breakpoint)
 
   (setq comint-input-sender 'gdb-send)
   ;;
-  ;; (re-)initialise
-  (setq gdb-current-address "main")
-  (setq gdb-previous-address nil)
-  (setq gdb-previous-frame nil)
-  (setq gdb-current-frame "main")
-  (setq gdb-view-source t)
-  (setq gdb-selected-view 'source)
-  (setq gdb-var-list nil)
-  (setq gdb-var-changed nil)
-  (setq gdb-first-prompt nil)
-  (setq gdb-prompting nil)
-  (setq gdb-current-item nil)
-  (setq gdb-pending-triggers nil)
-  (setq gdb-output-sink 'user)
-  (setq gdb-server-prefix "server ")
+  ;; (re-)initialize
+  (setq gdb-frame-address (if gdb-show-main "main" nil))
+  (setq gdb-previous-frame-address nil
+       gdb-memory-address "main"
+       gdb-previous-frame nil
+       gdb-selected-frame nil
+       gdb-current-language nil
+       gdb-frame-number nil
+       gdb-var-list nil
+       gdb-var-changed nil
+       gdb-first-prompt nil
+       gdb-prompting nil
+       gdb-input-queue nil
+       gdb-current-item nil
+       gdb-pending-triggers nil
+       gdb-output-sink 'user
+       gdb-server-prefix "server "
+       gdb-flush-pending-output nil
+       gdb-location-alist nil
+       gdb-find-file-unhook nil
+       gdb-error nil
+       gdb-macro-info nil)
   ;;
-  (mapc 'make-local-variable gdb-variables)
   (setq gdb-buffer-type 'gdba)
   ;;
   (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
@@ -195,18 +386,20 @@ detailed description of this mode.
   (if (eq window-system 'w32)
       (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
   (gdb-enqueue-input (list "set height 0\n" 'ignore))
+  (gdb-enqueue-input (list "set width 0\n" 'ignore))
   ;; find source file and compilation directory here
   (gdb-enqueue-input (list "server list main\n"   'ignore))   ; C program
   (gdb-enqueue-input (list "server list MAIN__\n" 'ignore))   ; Fortran program
   (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
   ;;
+  (gdb-set-gud-minor-mode-existing-buffers)
   (run-hooks 'gdba-mode-hook))
 
 (defcustom gdb-use-colon-colon-notation nil
-  "Non-nil means use FUNCTION::VARIABLE format to display variables in the
-speedbar."
+  "If non-nil use FUN::VAR format to display variables in the speedbar."
   :type 'boolean
-  :group 'gud)
+  :group 'gud
+  :version "22.1")
 
 (defun gud-watch ()
   "Watch expression at point."
@@ -214,29 +407,22 @@ speedbar."
   (require 'tooltip)
   (let ((expr (tooltip-identifier-from-point (point))))
     (if (and (string-equal gdb-current-language "c")
-            gdb-use-colon-colon-notation)
-       (setq expr (concat gdb-current-frame "::" expr)))
+            gdb-use-colon-colon-notation gdb-selected-frame)
+       (setq expr (concat gdb-selected-frame "::" expr)))
     (catch 'already-watched
       (dolist (var gdb-var-list)
        (if (string-equal expr (car var)) (throw 'already-watched nil)))
       (set-text-properties 0 (length expr) nil expr)
       (gdb-enqueue-input
-       (list 
+       (list
        (if (eq gud-minor-mode 'gdba)
            (concat "server interpreter mi \"-var-create - * "  expr "\"\n")
          (concat"-var-create - * "  expr "\n"))
             `(lambda () (gdb-var-create-handler ,expr))))))
-  (select-window (get-buffer-window gud-comint-buffer 'visible)))
-
-(defun gdb-goto-info ()
-  "Go to Emacs info node: GDB Graphical Interface."
-  (interactive)
-  (select-frame (make-frame))
-  (require 'info)
-  (Info-goto-node "(emacs)GDB Graphical Interface"))
+  (select-window (get-buffer-window gud-comint-buffer 0)))
 
 (defconst gdb-var-create-regexp
-"name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
+  "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
 
 (defun gdb-var-create-handler (expr)
   (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
@@ -248,25 +434,28 @@ speedbar."
                         (match-string 3)
                         nil nil)))
          (push var gdb-var-list)
-         (setq speedbar-update-flag t)
          (speedbar 1)
          (if (equal (nth 2 var) "0")
              (gdb-enqueue-input
-              (list (concat "server interpreter mi \"-var-evaluate-expression "
-                            (nth 1 var) "\"\n")
+              (list
+               (if (with-current-buffer
+                       gud-comint-buffer (eq gud-minor-mode 'gdba))
+                   (concat "server interpreter mi \"-var-evaluate-expression "
+                           (nth 1 var) "\"\n")
+                 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
                     `(lambda () (gdb-var-evaluate-expression-handler
                                  ,(nth 1 var) nil))))
            (setq gdb-var-changed t)))
       (if (re-search-forward "Undefined command" nil t)
-         (message "Watching expressions requires gdb 6.0 onwards")
-       (message "No symbol %s in current context." expr)))))
+         (message-box "Watching expressions requires gdb 6.0 onwards")
+       (message "No symbol \"%s\" in current context." expr)))))
 
 (defun gdb-var-evaluate-expression-handler (varnum changed)
   (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
     (goto-char (point-min))
     (re-search-forward ".*value=\"\\(.*?\\)\"" nil t)
     (catch 'var-found
-      (let ((var-list nil) (num 0))
+      (let ((num 0))
        (dolist (var gdb-var-list)
          (if (string-equal varnum (cadr var))
              (progn
@@ -279,11 +468,11 @@ speedbar."
 
 (defun gdb-var-list-children (varnum)
   (gdb-enqueue-input
-   (list (concat "server interpreter mi \"-var-list-children "  varnum "\"\n")
-            `(lambda () (gdb-var-list-children-handler ,varnum)))))
+   (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
+        `(lambda () (gdb-var-list-children-handler ,varnum)))))
 
 (defconst gdb-var-list-children-regexp
-"name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\"")
+  "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\"")
 
 (defun gdb-var-list-children-handler (varnum)
   (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
@@ -317,11 +506,11 @@ speedbar."
        (setq gdb-var-list (nreverse var-list))))))
 
 (defun gdb-var-update ()
-  (if (not (member 'gdb-var-update gdb-pending-triggers))
-      (progn
-       (gdb-enqueue-input (list "server interpreter mi \"-var-update *\"\n"
-                                'gdb-var-update-handler))
-       (push 'gdb-var-update gdb-pending-triggers))))
+  (when (not (member 'gdb-var-update gdb-pending-triggers))
+    (gdb-enqueue-input
+     (list "server interpreter mi \"-var-update *\"\n"
+          'gdb-var-update-handler))
+    (push 'gdb-var-update gdb-pending-triggers)))
 
 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
 
@@ -331,17 +520,29 @@ speedbar."
     (while (re-search-forward gdb-var-update-regexp nil t)
        (let ((varnum (match-string 1)))
          (gdb-enqueue-input
-          (list (concat "server interpreter mi \"-var-evaluate-expression "
-                        varnum "\"\n")
-                    `(lambda () (gdb-var-evaluate-expression-handler
-                                 ,varnum t)))))))
+          (list
+           (concat "server interpreter mi \"-var-evaluate-expression "
+                   varnum "\"\n")
+           `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
   (setq gdb-pending-triggers
-   (delq 'gdb-var-update gdb-pending-triggers)))
+   (delq 'gdb-var-update gdb-pending-triggers))
+  (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
+    ;; Dummy command to update speedbar at right time.
+    (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
+    ;; Keep gdb-pending-triggers non-nil till end.
+    (push 'gdb-speedbar-timer gdb-pending-triggers)))
+
+(defun gdb-speedbar-timer-fn ()
+  (setq gdb-pending-triggers
+       (delq 'gdb-speedbar-timer gdb-pending-triggers))
+  (with-current-buffer gud-comint-buffer
+    (speedbar-timer-fn)))
 
 (defun gdb-var-delete ()
-  "Delete watched expression from the speedbar."
+  "Delete watch expression at point from the speedbar."
   (interactive)
-  (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
+  (if (with-current-buffer
+         gud-comint-buffer (memq gud-minor-mode '(gdbmi gdba)))
       (let ((text (speedbar-line-text)))
        (string-match "\\(\\S-+\\)" text)
        (let* ((expr (match-string 1 text))
@@ -349,8 +550,11 @@ speedbar."
               (varnum (cadr var)))
          (unless (string-match "\\." varnum)
            (gdb-enqueue-input
-            (list (concat "server interpreter mi \"-var-delete "
-                          varnum "\"\n")
+            (list
+             (if (with-current-buffer gud-comint-buffer
+                   (eq gud-minor-mode 'gdba))
+                 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
+               (concat "-var-delete " varnum "\n"))
                   'ignore))
            (setq gdb-var-list (delq var gdb-var-list))
            (dolist (varchild gdb-var-list)
@@ -359,20 +563,25 @@ speedbar."
            (setq gdb-var-changed t))))))
 
 (defun gdb-edit-value (text token indent)
-  "Assign a value to a variable displayed in the speedbar"
+  "Assign a value to a variable displayed in the speedbar."
   (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
         (varnum (cadr var)) (value))
     (setq value (read-string "New value: "))
     (gdb-enqueue-input
-     (list (concat "server interpreter mi \"-var-assign "
-                  varnum " " value "\"\n")
+     (list
+      (if (with-current-buffer gud-comint-buffer
+           (eq gud-minor-mode 'gdba))
+         (concat "server interpreter mi \"-var-assign "
+                 varnum " " value "\"\n")
+       (concat "-var-assign " varnum " " value "\n"))
           'ignore))))
 
 (defcustom gdb-show-changed-values t
-  "Non-nil means use font-lock-warning-face to display values that have
-recently changed in the speedbar."
+  "If non-nil highlight values that have recently changed in the speedbar.
+The highlighting is done with `font-lock-warning-face'."
   :type 'boolean
-  :group 'gud)
+  :group 'gud
+  :version "22.1")
 
 (defun gdb-speedbar-expand-node (text token indent)
   "Expand the node the user clicked on.
@@ -380,52 +589,16 @@ TEXT is the text of the button we clicked on, a + or - item.
 TOKEN is data related to this node.
 INDENT is the current indentation depth."
   (cond ((string-match "+" text)        ;expand this node
-        (gdb-var-list-children token))
+        (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
+            (gdb-var-list-children token)
+          (progn
+            (gdbmi-var-update)
+            (gdbmi-var-list-children token))))
        ((string-match "-" text)        ;contract this node
         (dolist (var gdb-var-list)
           (if (string-match (concat token "\\.") (nth 1 var))
               (setq gdb-var-list (delq var gdb-var-list))))
         (setq gdb-var-changed t))))
-\f
-(defvar gdb-buffer-type nil
-  "One of the symbols bound in `gdb-buffer-rules'.")
-
-(defvar gdb-input-queue ()
-  "A list of gdb command objects.")
-
-(defvar gdb-prompting nil
-  "True when gdb is idle with no pending input.")
-
-(defvar gdb-output-sink 'user
-  "The disposition of the output of the current gdb command.
-Possible values are these symbols:
-
-    user -- gdb output should be copied to the GUD buffer
-            for the user to see.
-
-    inferior -- gdb output should be copied to the inferior-io buffer
-
-    pre-emacs -- output should be ignored util the post-prompt
-                 annotation is received.  Then the output-sink
-                becomes:...
-    emacs -- output should be collected in the partial-output-buffer
-            for subsequent processing by a command.  This is the
-            disposition of output generated by commands that
-            gdb mode sends to gdb on its own behalf.
-    post-emacs -- ignore output until the prompt annotation is
-                 received, then go to USER disposition.
-
-gdba (gdb-ui.el) uses all five values,  gdbmi (gdb-mi.el) only two
-(user and emacs).")
-
-(defvar gdb-current-item nil
-  "The most recent command item sent to gdb.")
-
-(defvar gdb-pending-triggers '()
-  "A list of trigger functions that have run later than their output
-handlers.")
-
-;; end of gdb variables
 
 (defun gdb-get-target-string ()
   (with-current-buffer gud-comint-buffer
@@ -460,14 +633,14 @@ The key should be one of the cars in `gdb-buffer-rules-assoc'."
             (name (funcall (gdb-rules-name-maker rules)))
             (new (get-buffer-create name)))
        (with-current-buffer new
-         ;; FIXME: This should be set after calling the function, since the
-         ;; function should run kill-all-local-variables.
-         (set (make-local-variable 'gdb-buffer-type) key)
-         (if (cdr (cdr rules))
-             (funcall (car (cdr (cdr rules)))))
-         (set (make-local-variable 'gud-minor-mode) 
-              (with-current-buffer gud-comint-buffer gud-minor-mode))
-         (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
+         (let ((trigger))
+           (if (cdr (cdr rules))
+               (setq trigger (funcall (car (cdr (cdr rules))))))
+           (set (make-local-variable 'gdb-buffer-type) key)
+           (set (make-local-variable 'gud-minor-mode)
+                (with-current-buffer gud-comint-buffer gud-minor-mode))
+           (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
+           (if trigger (funcall trigger)))
          new))))
 
 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
@@ -526,12 +699,35 @@ The key should be one of the cars in `gdb-buffer-rules-assoc'."
          (gdb-get-target-string)
          "*"))
 
+(defun gdb-display-inferior-io-buffer ()
+  "Display IO of inferior in a separate window."
+  (interactive)
+  (if gdb-use-inferior-io-buffer
+      (gdb-display-buffer
+       (gdb-get-create-buffer 'gdb-inferior-io))))
+
+(defconst gdb-frame-parameters
+  '((height . 14) (width . 80)
+    (unsplittable . t)
+    (tool-bar-lines . nil)
+    (menu-bar-lines . nil)
+    (minibuffer . nil)))
+
+(defun gdb-frame-inferior-io-buffer ()
+  "Display IO of inferior in a new frame."
+  (interactive)
+  (if gdb-use-inferior-io-buffer
+      (let ((special-display-regexps (append special-display-regexps '(".*")))
+           (special-display-frame-alist gdb-frame-parameters))
+       (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
+
 (defvar gdb-inferior-io-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
     (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
     (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
     (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
+    (define-key map "\C-d" 'gdb-inferior-io-eof)
     map))
 
 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
@@ -597,10 +793,15 @@ The key should be one of the cars in `gdb-buffer-rules-assoc'."
 
 (defun gdb-send (proc string)
   "A comint send filter for gdb.
-This filter may simply queue output for a later time."
-  (if gud-running
-      (process-send-string proc (concat string "\n"))
-    (gdb-enqueue-input (concat string "\n"))))
+This filter may simply queue input for a later time."
+  (with-current-buffer gud-comint-buffer
+    (remove-text-properties (point-min) (point-max) '(face)))
+  (let ((item (concat string "\n")))
+    (if gud-running
+      (progn
+       (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
+       (process-send-string proc item))
+      (gdb-enqueue-input item))))
 
 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
 ;; is a query, or other non-top-level prompt.
@@ -618,6 +819,27 @@ This filter may simply queue output for a later time."
         (let ((last (car (last queue))))
           (unless (nbutlast queue) (setq gdb-input-queue '()))
           last))))
+
+(defun gdb-send-item (item)
+  (setq gdb-flush-pending-output nil)
+  (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
+  (setq gdb-current-item item)
+  (with-current-buffer gud-comint-buffer
+    (if (eq gud-minor-mode 'gdba)
+       (if (stringp item)
+           (progn
+             (setq gdb-output-sink 'user)
+             (process-send-string (get-buffer-process gud-comint-buffer) item))
+         (progn
+           (gdb-clear-partial-output)
+           (setq gdb-output-sink 'pre-emacs)
+           (process-send-string (get-buffer-process gud-comint-buffer)
+                                (car item))))
+      ;; case: eq gud-minor-mode 'gdbmi
+      (gdb-clear-partial-output)
+      (setq gdb-output-sink 'emacs)
+      (process-send-string (get-buffer-process gud-comint-buffer)
+                          (car item)))))
 \f
 ;;
 ;; output -- things gdb prints to emacs
@@ -638,7 +860,8 @@ This filter may simply queue output for a later time."
 (defcustom gud-gdba-command-name "gdb -annotate=3"
   "Default command to execute an executable under the GDB-UI debugger."
   :type 'string
-  :group 'gud)
+  :group 'gud
+  :version "22.1")
 
 (defvar gdb-annotation-rules
   '(("pre-prompt" gdb-pre-prompt)
@@ -652,17 +875,27 @@ This filter may simply queue output for a later time."
     ("post-prompt" gdb-post-prompt)
     ("source" gdb-source)
     ("starting" gdb-starting)
-    ("exited" gdb-stopping)
-    ("signalled" gdb-stopping)
+    ("exited" gdb-exited)
+    ("signalled" gdb-exited)
     ("signal" gdb-stopping)
     ("breakpoint" gdb-stopping)
     ("watchpoint" gdb-stopping)
     ("frame-begin" gdb-frame-begin)
     ("stopped" gdb-stopped)
+    ("error-begin" gdb-error)
+    ("error" gdb-error)
     ) "An assoc mapping annotation tags to functions which process them.")
 
+(defun gdb-resync()
+  (setq gdb-flush-pending-output t)
+  (setq gud-running nil)
+  (setq gdb-output-sink 'user)
+  (setq gdb-input-queue nil)
+  (setq gdb-pending-triggers nil)
+  (setq gdb-prompting t))
+
 (defconst gdb-source-spec-regexp
-  "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:\\(0x[a-f0-9]*\\)")
+  "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
 
 ;; Do not use this except as an annotation handler.
 (defun gdb-source (args)
@@ -671,44 +904,23 @@ This filter may simply queue output for a later time."
   (setq gud-last-frame
        (cons
         (match-string 1 args)
-        (string-to-int (match-string 2 args))))
-  (setq gdb-current-address (match-string 3 args))
-  (setq gdb-view-source t)
+        (string-to-number (match-string 2 args))))
+  (setq gdb-frame-address (match-string 3 args))
   ;; cover for auto-display output which comes *before*
   ;; stopped annotation
   (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
 
-(defun gdb-send-item (item)
-  (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
-  (setq gdb-current-item item)
-  (with-current-buffer gud-comint-buffer
-    (if (eq gud-minor-mode 'gdba)
-       (progn
-         (if (stringp item)
-             (progn
-               (setq gdb-output-sink 'user)
-               (process-send-string (get-buffer-process gud-comint-buffer) item))
-           (progn
-             (gdb-clear-partial-output)
-             (setq gdb-output-sink 'pre-emacs)
-             (process-send-string (get-buffer-process gud-comint-buffer)
-                                  (car item)))))
-      ; case: eq gud-minor-mode 'gdbmi
-      (gdb-clear-partial-output)
-      (setq gdb-output-sink 'emacs)
-      (process-send-string (get-buffer-process gud-comint-buffer)
-                        (car item)))))
-
 (defun gdb-pre-prompt (ignored)
-  "An annotation handler for `pre-prompt'. This terminates the collection of
-output from a previous command if that happens to be in effect."
+  "An annotation handler for `pre-prompt'.
+This terminates the collection of output from a previous command if that
+happens to be in effect."
   (let ((sink gdb-output-sink))
     (cond
      ((eq sink 'user) t)
      ((eq sink 'emacs)
       (setq gdb-output-sink 'post-emacs))
      (t
-      (setq gdb-output-sink 'user)
+      (gdb-resync)
       (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
 
 (defun gdb-prompt (ignored)
@@ -725,7 +937,7 @@ This sends the next command (if any) to gdb."
        (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
          (funcall handler))))
      (t
-      (setq gdb-output-sink 'user)
+      (gdb-resync)
       (error "Phase error in gdb-prompt (got %s)" sink))))
   (let ((input (gdb-dequeue-input)))
     (if input
@@ -739,8 +951,10 @@ This sends the next command (if any) to gdb."
   (setq gdb-prompting t))
 
 (defun gdb-starting (ignored)
-  "An annotation handler for `starting'.  This says that I/O for the
-subprocess is now the program being debugged, not GDB."
+  "An annotation handler for `starting'.
+This says that I/O for the subprocess is now the program being debugged,
+not GDB."
+  (setq gdb-active-process t)
   (let ((sink gdb-output-sink))
     (cond
      ((eq sink 'user)
@@ -748,17 +962,31 @@ subprocess is now the program being debugged, not GDB."
        (setq gud-running t)
        (if gdb-use-inferior-io-buffer
            (setq gdb-output-sink 'inferior))))
-     (t (error "Unexpected `starting' annotation")))))
+     (t
+      (gdb-resync)
+      (error "Unexpected `starting' annotation")))))
 
 (defun gdb-stopping (ignored)
-  "An annotation handler for `exited' and other annotations which say that I/O
-for the subprocess is now GDB, not the program being debugged."
+  "An annotation handler for `breakpoint' and other annotations.
+They say that I/O for the subprocess is now GDB, not the program
+being debugged."
   (if gdb-use-inferior-io-buffer
       (let ((sink gdb-output-sink))
        (cond
         ((eq sink 'inferior)
          (setq gdb-output-sink 'user))
-        (t (error "Unexpected stopping annotation"))))))
+        (t
+         (gdb-resync)
+         (error "Unexpected stopping annotation"))))))
+
+(defun gdb-exited (ignored)
+  "An annotation handler for `exited' and `signalled'.
+They say that I/O for the subprocess is now GDB, not the program
+being debugged and that the program is no longer running.  This
+function is used to change the focus of GUD tooltips to #define
+directives."
+  (setq gdb-active-process nil)
+  (gdb-stopping ignored))
 
 (defun gdb-frame-begin (ignored)
   (let ((sink gdb-output-sink))
@@ -767,107 +995,123 @@ for the subprocess is now GDB, not the program being debugged."
       (setq gdb-output-sink 'user))
      ((eq sink 'user) t)
      ((eq sink 'emacs) t)
-     (t (error "Unexpected frame-begin annotation (%S)" sink)))))
+     (t
+      (gdb-resync)
+      (error "Unexpected frame-begin annotation (%S)" sink)))))
 
 (defun gdb-stopped (ignored)
-  "An annotation handler for `stopped'.  It is just like gdb-stopping, except
-that if we already set the output sink to 'user in gdb-stopping, that is fine."
+  "An annotation handler for `stopped'.
+It is just like `gdb-stopping', except that if we already set the output
+sink to `user' in `gdb-stopping', that is fine."
   (setq gud-running nil)
   (let ((sink gdb-output-sink))
     (cond
      ((eq sink 'inferior)
       (setq gdb-output-sink 'user))
      ((eq sink 'user) t)
-     (t (error "Unexpected stopped annotation")))))
+     (t
+      (gdb-resync)
+      (error "Unexpected stopped annotation")))))
+
+(defun gdb-error (ignored)
+  (setq gdb-error (not gdb-error)))
 
 (defun gdb-post-prompt (ignored)
-  "An annotation handler for `post-prompt'. This begins the collection of
-output from the current command if that happens to be appropriate."
-  (if (not gdb-pending-triggers)
-      (progn
-       (gdb-get-current-frame)
-       (gdb-invalidate-frames)
-       (gdb-invalidate-breakpoints)
-       (gdb-invalidate-assembler)
-       (gdb-invalidate-registers)
-       (gdb-invalidate-locals)
-       (gdb-invalidate-threads)
-       (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
-         ;; FIXME: with GDB-6 on Darwin, this might very well work.
-         (dolist (frame (frame-list))
-           (when (string-equal (frame-parameter frame 'name) "Speedbar")
-             (setq gdb-var-changed t)    ; force update
-             (dolist (var gdb-var-list)
-               (setcar (nthcdr 5 var) nil))))
-         (gdb-var-update))))
+  "An annotation handler for `post-prompt'.
+This begins the collection of output from the current command if that
+happens to be appropriate."
+  (unless gdb-pending-triggers
+    (gdb-get-selected-frame)
+    (gdb-invalidate-frames)
+    (gdb-invalidate-breakpoints)
+    ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
+    ;; so gdb-frame-address is updated.
+    ;; (gdb-invalidate-assembler)
+    (gdb-invalidate-registers)
+    (gdb-invalidate-memory)
+    (gdb-invalidate-locals)
+    (gdb-invalidate-threads)
+    (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
+      ;; FIXME: with GDB-6 on Darwin, this might very well work.
+      ;; Only needed/used with speedbar/watch expressions.
+      (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
+       (setq gdb-var-changed t)    ; force update
+       (dolist (var gdb-var-list)
+         (setcar (nthcdr 5 var) nil))
+       (gdb-var-update))))
   (let ((sink gdb-output-sink))
     (cond
      ((eq sink 'user) t)
      ((eq sink 'pre-emacs)
       (setq gdb-output-sink 'emacs))
      (t
-      (setq gdb-output-sink 'user)
+      (gdb-resync)
       (error "Phase error in gdb-post-prompt (got %s)" sink)))))
 
 (defun gud-gdba-marker-filter (string)
-  "A gud marker filter for gdb. Handle a burst of output from GDB."
-  (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
-  ;; Recall the left over gud-marker-acc from last time
-  (setq gud-marker-acc (concat gud-marker-acc string))
-  ;; Start accumulating output for the GUD buffer
-  (let ((output ""))
-    ;;
-    ;; Process all the complete markers in this chunk.
-    (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
-      (let ((annotation (match-string 1 gud-marker-acc)))
-       ;;
-       ;; Stuff prior to the match is just ordinary output.
-       ;; It is either concatenated to OUTPUT or directed
-       ;; elsewhere.
-       (setq output
-             (gdb-concat-output
-              output
-              (substring gud-marker-acc 0 (match-beginning 0))))
-        ;;
-       ;; Take that stuff off the gud-marker-acc.
-       (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
-        ;;
-       ;; Parse the tag from the annotation, and maybe its arguments.
-       (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
-       (let* ((annotation-type (match-string 1 annotation))
-              (annotation-arguments (match-string 2 annotation))
-              (annotation-rule (assoc annotation-type
-                                      gdb-annotation-rules)))
-         ;; Call the handler for this annotation.
-         (if annotation-rule
-             (funcall (car (cdr annotation-rule))
-                      annotation-arguments)
-           ;; Else the annotation is not recognized.  Ignore it silently,
-           ;; so that GDB can add new annotations without causing
-           ;; us to blow up.
-           ))))
-    ;;
-    ;; Does the remaining text end in a partial line?
-    ;; If it does, then keep part of the gud-marker-acc until we get more.
-    (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
-                     gud-marker-acc)
-       (progn
-         ;; Everything before the potential marker start can be output.
+  "A gud marker filter for gdb.  Handle a burst of output from GDB."
+  (if gdb-flush-pending-output
+      nil
+    (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
+    ;; Recall the left over gud-marker-acc from last time.
+    (setq gud-marker-acc (concat gud-marker-acc string))
+    ;; Start accumulating output for the GUD buffer.
+    (let ((output ""))
+      ;;
+      ;; Process all the complete markers in this chunk.
+      (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
+       (let ((annotation (match-string 1 gud-marker-acc)))
+         ;;
+         ;; Stuff prior to the match is just ordinary output.
+         ;; It is either concatenated to OUTPUT or directed
+         ;; elsewhere.
          (setq output
-               (gdb-concat-output output
-                                  (substring gud-marker-acc 0
-                                             (match-beginning 0))))
+               (gdb-concat-output
+                output
+                (substring gud-marker-acc 0 (match-beginning 0))))
+         ;;
+         ;; Take that stuff off the gud-marker-acc.
+         (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
          ;;
-         ;; Everything after, we save, to combine with later input.
-         (setq gud-marker-acc (substring gud-marker-acc (match-beginning 0))))
+         ;; Parse the tag from the annotation, and maybe its arguments.
+         (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
+         (let* ((annotation-type (match-string 1 annotation))
+                (annotation-arguments (match-string 2 annotation))
+                (annotation-rule (assoc annotation-type
+                                        gdb-annotation-rules)))
+           ;; Call the handler for this annotation.
+           (if annotation-rule
+               (funcall (car (cdr annotation-rule))
+                        annotation-arguments)
+             ;; Else the annotation is not recognized.  Ignore it silently,
+             ;; so that GDB can add new annotations without causing
+             ;; us to blow up.
+             ))))
       ;;
-      ;; In case we know the gud-marker-acc contains no partial annotations:
-      (progn
-       (setq output (gdb-concat-output output gud-marker-acc))
-       (setq gud-marker-acc "")))
-    output))
+      ;; Does the remaining text end in a partial line?
+      ;; If it does, then keep part of the gud-marker-acc until we get more.
+      (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
+                       gud-marker-acc)
+         (progn
+           ;; Everything before the potential marker start can be output.
+           (setq output
+                 (gdb-concat-output output
+                                    (substring gud-marker-acc 0
+                                               (match-beginning 0))))
+           ;;
+           ;; Everything after, we save, to combine with later input.
+           (setq gud-marker-acc (substring gud-marker-acc
+                                           (match-beginning 0))))
+       ;;
+       ;; In case we know the gud-marker-acc contains no partial annotations:
+       (progn
+         (setq output (gdb-concat-output output gud-marker-acc))
+         (setq gud-marker-acc "")))
+      output)))
 
 (defun gdb-concat-output (so-far new)
+  (if gdb-error
+      (put-text-property 0 (length new) 'face font-lock-warning-face new))
   (let ((sink gdb-output-sink))
     (cond
      ((eq sink 'user) (concat so-far new))
@@ -878,7 +1122,9 @@ output from the current command if that happens to be appropriate."
      ((eq sink 'inferior)
       (gdb-append-to-inferior-io new)
       so-far)
-     (t (error "Bogon output sink %S" sink)))))
+     (t
+      (gdb-resync)
+      (error "Bogon output sink %S" sink)))))
 
 (defun gdb-append-to-partial-output (string)
   (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
@@ -921,7 +1167,7 @@ output from the current command if that happens to be appropriate."
 ;; annotation rule binding of whatever gdb sends to tell us this command
 ;; might have changed it's output.
 ;;
-;; NAME is the function name.  DEMAND-PREDICATE tests if output is really needed.
+;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
 ;; GDB-COMMAND is a string of such.  OUTPUT-HANDLER is the function bound to the
 ;; input in the input queue (see comment about ``gdb communications'' above).
 
@@ -944,17 +1190,18 @@ output from the current command if that happens to be appropriate."
      (let ((buf (gdb-get-buffer ',buf-key)))
        (and buf
            (with-current-buffer buf
-             (let ((p (point))
+             (let ((p (window-point (get-buffer-window buf 0)))
                    (buffer-read-only nil))
                (erase-buffer)
                (insert-buffer-substring (gdb-get-create-buffer
                                          'gdb-partial-output-buffer))
-               (goto-char p)))))
+               (set-window-point (get-buffer-window buf 0) p)))))
      ;; put customisation here
      (,custom-defun)))
 
-(defmacro def-gdb-auto-updated-buffer (buffer-key trigger-name gdb-command
-                                                 output-handler-name custom-defun)
+(defmacro def-gdb-auto-updated-buffer (buffer-key
+                                      trigger-name gdb-command
+                                      output-handler-name custom-defun)
   `(progn
      (def-gdb-auto-update-trigger ,trigger-name
        ;; The demand predicate:
@@ -990,9 +1237,8 @@ output from the current command if that happens to be appropriate."
   ;; buffer specific functions
   gdb-info-breakpoints-custom)
 
-(defvar gdb-cdir nil "Compilation directory.")
-
-(defconst breakpoint-xpm-data "/* XPM */
+(defconst breakpoint-xpm-data
+  "/* XPM */
 static char *magick[] = {
 /* columns rows colors chars-per-pixel */
 \"10 10 2 1\",
@@ -1013,7 +1259,7 @@ static char *magick[] = {
   "XPM data used for breakpoint icon.")
 
 (defconst breakpoint-enabled-pbm-data
-"P1
+  "P1
 10 10\",
 0 0 0 0 1 1 1 1 0 0 0 0
 0 0 0 1 1 1 1 1 1 0 0 0
@@ -1028,7 +1274,7 @@ static char *magick[] = {
   "PBM data used for enabled breakpoint icon.")
 
 (defconst breakpoint-disabled-pbm-data
-"P1
+  "P1
 10 10\",
 0 0 1 0 1 0 1 0 0 0
 0 1 0 1 0 1 0 1 0 0
@@ -1043,32 +1289,38 @@ static char *magick[] = {
   "PBM data used for disabled breakpoint icon.")
 
 (defvar breakpoint-enabled-icon nil
-  "Icon for enabled breakpoint in display margin")
+  "Icon for enabled breakpoint in display margin.")
 
 (defvar breakpoint-disabled-icon nil
-  "Icon for disabled breakpoint in display margin")
-
-(defvar breakpoint-bitmap nil
-  "Bitmap for breakpoint in fringe")
+  "Icon for disabled breakpoint in display margin.")
 
-(defface breakpoint-enabled-bitmap-face
-  '((t
-     :inherit fringe
-     :foreground "red"))
-  "Face for enabled breakpoint icon in fringe.")
+;; Bitmap for breakpoint in fringe
+(and (display-images-p)
+     (define-fringe-bitmap 'breakpoint
+       "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
 
-(defface breakpoint-disabled-bitmap-face
+(defface breakpoint-enabled
   '((t
-     :inherit fringe
-     :foreground "grey60"))
-  "Face for disabled breakpoint icon in fringe.")
-
+     :foreground "red"
+     :weight bold))
+  "Face for enabled breakpoint icon in fringe."
+  :group 'gud)
+;; Compatibility alias for old name.
+(put 'breakpoint-enabled-bitmap-face 'face-alias 'breakpoint-enabled)
+
+(defface breakpoint-disabled
+  ;; We use different values of grey for different background types,
+  ;; so that on low-color displays it will end up as something visible
+  ;; if it has to be approximated.
+  '((((background dark))  :foreground "grey60")
+    (((background light)) :foreground "grey40"))
+  "Face for disabled breakpoint icon in fringe."
+  :group 'gud)
 
-;;-put breakpoint icons in relevant margins (even those set in the GUD buffer)
+;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
 (defun gdb-info-breakpoints-custom ()
-  (let ((flag)(address))
-    ;;
-    ;; remove all breakpoint-icons in source buffers but not assembler buffer
+  (let ((flag) (bptno))
+    ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
     (dolist (buffer (buffer-list))
       (with-current-buffer buffer
        (if (and (eq gud-minor-mode 'gdba)
@@ -1079,36 +1331,50 @@ static char *magick[] = {
        (goto-char (point-min))
        (while (< (point) (- (point-max) 1))
          (forward-line 1)
-         (if (looking-at "[^\t].*breakpoint")
+         (if (looking-at "[^\t].*?breakpoint")
              (progn
-               (looking-at "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)")
-               (setq flag (char-after (match-beginning 1)))
+               (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
+               (setq bptno (match-string 1))
+               (setq flag (char-after (match-beginning 2)))
                (beginning-of-line)
-               (if (re-search-forward "in\\s-+\\S-+\\s-+at\\s-+" nil t)
+               (if (re-search-forward " in .* at\\s-+" nil t)
                    (progn
-                     (looking-at "\\(\\S-*\\):\\([0-9]+\\)")
+                     (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
                      (let ((line (match-string 2)) (buffer-read-only nil)
                            (file (match-string 1)))
-                       (add-text-properties (point-at-bol) (point-at-eol)
+                       (add-text-properties (line-beginning-position)
+                                            (line-end-position)
                         '(mouse-face highlight
                           help-echo "mouse-2, RET: visit breakpoint"))
-                       (with-current-buffer
-                           (find-file-noselect
-                            (if (file-exists-p file) file
-                              (expand-file-name file gdb-cdir)))
-                         (save-current-buffer
-                           (set (make-local-variable 'gud-minor-mode) 'gdba)
-                           (set (make-local-variable 'tool-bar-map)
-                                gud-tool-bar-map))
-                         ;; only want one breakpoint icon at each location
-                         (save-excursion
-                           (goto-line (string-to-number line))
-                           (gdb-put-breakpoint-icon (eq flag ?y)))))))))
+                       (unless (file-exists-p file)
+                          (setq file (cdr (assoc bptno gdb-location-alist))))
+                       (if (and file
+                                (not (string-equal file "File not found")))
+                           (with-current-buffer
+                               (find-file-noselect file 'nowarn)
+                             (set (make-local-variable 'gud-minor-mode)
+                                  'gdba)
+                             (set (make-local-variable 'tool-bar-map)
+                                  gud-tool-bar-map)
+                             ;; Only want one breakpoint icon at each
+                             ;; location.
+                             (save-excursion
+                               (goto-line (string-to-number line))
+                               (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
+                         (gdb-enqueue-input
+                          (list
+                           (concat "list "
+                                   (match-string-no-properties 1) ":1\n")
+                           'ignore))
+                         (gdb-enqueue-input
+                          (list "info source\n"
+                                `(lambda () (gdb-get-location
+                                             ,bptno ,line ,flag))))))))))
          (end-of-line)))))
   (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
 
-(defun gdb-mouse-toggle-breakpoint (event)
-  "Toggle breakpoint in left fringe/margin with mouse click"
+(defun gdb-mouse-set-clear-breakpoint (event)
+  "Set/clear breakpoint in left fringe/margin with mouse click."
   (interactive "e")
   (mouse-minibuffer-check event)
   (let ((posn (event-end event)))
@@ -1117,12 +1383,32 @@ static char *magick[] = {
          (save-excursion
            (goto-char (posn-point posn))
            (if (or (posn-object posn)
-                   (and breakpoint-bitmap
-                        (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
-                            breakpoint-bitmap)))
+                   (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
+                       'breakpoint))
                (gud-remove nil)
              (gud-break nil)))))))
 
+(defun gdb-mouse-toggle-breakpoint (event)
+  "Enable/disable breakpoint in left fringe/margin with mouse click."
+  (interactive "e")
+  (mouse-minibuffer-check event)
+  (let ((posn (event-end event)))
+    (if (numberp (posn-point posn))
+       (with-selected-window (posn-window posn)
+         (save-excursion
+           (goto-char (posn-point posn))
+           (if (posn-object posn)
+               (gdb-enqueue-input
+                (list
+                 (let ((bptno (get-text-property
+                               0 'gdb-bptno (car (posn-string posn)))))
+                   (concat
+                           (if (get-text-property
+                                0 'gdb-enabled (car (posn-string posn)))
+                               "disable "
+                             "enable ")
+                           bptno "\n")) 'ignore))))))))
+
 (defun gdb-breakpoints-buffer-name ()
   (with-current-buffer gud-comint-buffer
     (concat "*breakpoints of " (gdb-get-target-string) "*")))
@@ -1133,98 +1419,100 @@ static char *magick[] = {
   (gdb-display-buffer
    (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
 
-(defconst gdb-frame-parameters
-  '((height . 12) (width . 60)
-    (unsplittable . t)
-    (tool-bar-lines . nil)
-    (menu-bar-lines . nil)
-    (minibuffer . nil)))
-
 (defun gdb-frame-breakpoints-buffer ()
   "Display status of user-settable breakpoints in a new frame."
   (interactive)
-  (select-frame (make-frame gdb-frame-parameters))
-  (switch-to-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))
-  (set-window-dedicated-p (selected-window) t))
+  (let ((special-display-regexps (append special-display-regexps '(".*")))
+       (special-display-frame-alist gdb-frame-parameters))
+    (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
 
 (defvar gdb-breakpoints-mode-map
   (let ((map (make-sparse-keymap))
        (menu (make-sparse-keymap "Breakpoints")))
-    (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
-    (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
+    (define-key menu [quit] '("Quit"   . kill-this-buffer))
     (define-key menu [goto] '("Goto"   . gdb-goto-breakpoint))
-
+    (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
+    (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
     (suppress-keymap map)
     (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
     (define-key map " " 'gdb-toggle-breakpoint)
     (define-key map "d" 'gdb-delete-breakpoint)
+    (define-key map "q" 'kill-this-buffer)
     (define-key map "\r" 'gdb-goto-breakpoint)
-    (define-key map [mouse-2] 'gdb-mouse-goto-breakpoint)
+    (define-key map [mouse-2] 'gdb-goto-breakpoint)
+    (define-key map [follow-link] 'mouse-face)
     map))
 
 (defun gdb-breakpoints-mode ()
   "Major mode for gdb breakpoints.
 
 \\{gdb-breakpoints-mode-map}"
+  (kill-all-local-variables)
   (setq major-mode 'gdb-breakpoints-mode)
   (setq mode-name "Breakpoints")
   (use-local-map gdb-breakpoints-mode-map)
   (setq buffer-read-only t)
+  (run-mode-hooks 'gdb-breakpoints-mode-hook)
   (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
-      (gdb-invalidate-breakpoints)
-    (gdbmi-invalidate-breakpoints)))
+      'gdb-invalidate-breakpoints
+    'gdbmi-invalidate-breakpoints))
 
 (defun gdb-toggle-breakpoint ()
-  "Enable/disable the breakpoint at current line."
+  "Enable/disable breakpoint at current line."
   (interactive)
   (save-excursion
     (beginning-of-line 1)
-    (if (not (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)"))
-       (error "Not recognized as break/watchpoint line")
-      (gdb-enqueue-input
-       (list
-       (concat
-        (if (eq ?y (char-after (match-beginning 2)))
-            gdb-server-prefix "disable "
-          gdb-server-prefix "enable ")
-        (match-string 1) "\n")
-       'ignore)))))
+    (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
+           (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)\\s-+")
+         (looking-at
+     "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
+       (gdb-enqueue-input
+        (list
+         (concat gdb-server-prefix
+                 (if (eq ?y (char-after (match-beginning 2)))
+                     "disable "
+                   "enable ")
+                 (match-string 1) "\n") 'ignore))
+      (error "Not recognized as break/watchpoint line"))))
 
 (defun gdb-delete-breakpoint ()
   "Delete the breakpoint at current line."
   (interactive)
   (beginning-of-line 1)
-  (if (not (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)"))
-      (error "Not recognized as break/watchpoint line")
-    (gdb-enqueue-input
-     (list (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))))
+  (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
+         (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)")
+       (looking-at
+        "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
+      (gdb-enqueue-input
+       (list
+       (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
+    (error "Not recognized as break/watchpoint line")))
 
-(defun gdb-goto-breakpoint ()
+(defun gdb-goto-breakpoint (&optional event)
   "Display the breakpoint location specified at current line."
-  (interactive)
+  (interactive (list last-input-event))
+  (if event (mouse-set-point event))
   (save-excursion
     (beginning-of-line 1)
-    (re-search-forward "in\\s-+\\S-+\\s-+at\\s-+" nil t)
-    (looking-at "\\(\\S-*\\):\\([0-9]+\\)"))
-  (if (match-string 2)
-      (let ((line (match-string 2))
-           (file (match-string 1)))
-       (save-selected-window
-         (let* ((buf (find-file-noselect (if (file-exists-p file)
-                                             file
-                                           (expand-file-name file gdb-cdir))))
-                (window (gdb-display-buffer buf)))
-                (with-current-buffer buf
-                  (goto-line (string-to-number line))
-                  (set-window-point window (point))))))))
-
-(defun gdb-mouse-goto-breakpoint (event)
-  "Display the breakpoint location that you click on."
-  (interactive "e")
-  (mouse-set-point event)
-  (gdb-goto-breakpoint))
+    (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
+           (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
+         (looking-at
+          "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+.\\s-+\\S-+\\s-+\
+\\(\\S-+\\):\\([0-9]+\\)"))
+       (let ((bptno (match-string 1))
+             (file  (match-string 2))
+             (line  (match-string 3)))
+         (save-selected-window
+           (let* ((buf (find-file-noselect
+                        (if (file-exists-p file) file
+                          (cdr (assoc bptno gdb-location-alist)))))
+                  (window (display-buffer buf)))
+             (with-current-buffer buf
+               (goto-line (string-to-number line))
+               (set-window-point window (point))))))
+      (error "Not recognized as break/watchpoint line"))))
 \f
-;;
+
 ;; Frames buffer.  This displays a perpetually correct bactracktrace
 ;; (from the command `where').
 ;;
@@ -1246,14 +1534,13 @@ static char *magick[] = {
       (let ((buffer-read-only nil))
        (goto-char (point-min))
        (while (< (point) (point-max))
-         (add-text-properties (point-at-bol) (point-at-eol)
+         (add-text-properties (line-beginning-position) (line-end-position)
                             '(mouse-face highlight
                               help-echo "mouse-2, RET: Select frame"))
          (beginning-of-line)
-         (when (and (or (looking-at "^#[0-9]*\\s-*\\S-* in \\(\\S-*\\)")
-                        (looking-at "^#[0-9]*\\s-*\\(\\S-*\\)"))
-                    (equal (match-string 1) gdb-current-frame))
-           (put-text-property (point-at-bol) (point-at-eol)
+         (when (and (looking-at "^#\\([0-9]+\\)")
+                    (equal (match-string 1) gdb-frame-number))
+           (put-text-property (line-beginning-position) (line-end-position)
                               'face '(:inverse-video t)))
          (forward-line 1))))))
 
@@ -1270,50 +1557,50 @@ static char *magick[] = {
 (defun gdb-frame-stack-buffer ()
   "Display backtrace of current stack in a new frame."
   (interactive)
-  (select-frame (make-frame gdb-frame-parameters))
-  (switch-to-buffer (gdb-get-create-buffer 'gdb-stack-buffer))
-  (set-window-dedicated-p (selected-window) t))
+  (let ((special-display-regexps (append special-display-regexps '(".*")))
+       (special-display-frame-alist gdb-frame-parameters))
+    (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
 
 (defvar gdb-frames-mode-map
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map)
+    (define-key map "q" 'kill-this-buffer)
     (define-key map "\r" 'gdb-frames-select)
-    (define-key map [mouse-2] 'gdb-frames-mouse-select)
+    (define-key map [mouse-2] 'gdb-frames-select)
+    (define-key map [follow-link] 'mouse-face)
     map))
 
 (defun gdb-frames-mode ()
   "Major mode for gdb frames.
 
 \\{gdb-frames-mode-map}"
+  (kill-all-local-variables)
   (setq major-mode 'gdb-frames-mode)
   (setq mode-name "Frames")
   (setq buffer-read-only t)
   (use-local-map gdb-frames-mode-map)
   (font-lock-mode -1)
+  (run-mode-hooks 'gdb-frames-mode-hook)
   (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
-      (gdb-invalidate-frames)
-    (gdbmi-invalidate-frames)))
+      'gdb-invalidate-frames
+    'gdbmi-invalidate-frames))
 
 (defun gdb-get-frame-number ()
   (save-excursion
-    (let* ((pos (re-search-backward "^#\\([0-9]*\\)" nil t))
+    (let* ((pos (re-search-backward "^#*\\([0-9]*\\)" nil t))
           (n (or (and pos (match-string-no-properties 1)) "0")))
       n)))
 
-(defun gdb-frames-select ()
+(defun gdb-frames-select (&optional event)
   "Select the frame and display the relevant source."
-  (interactive)
+  (interactive (list last-input-event))
+  (if event (mouse-set-point event))
   (gdb-enqueue-input
-   (list (concat gdb-server-prefix "frame " (gdb-get-frame-number) "\n") 'ignore))
+   (list (concat gdb-server-prefix "frame "
+                (gdb-get-frame-number) "\n") 'ignore))
   (gud-display-frame))
-
-(defun gdb-frames-mouse-select (event)
-  "Select the frame you click on and display the relevant source."
-  (interactive "e")
-  (mouse-set-point event)
-  (gdb-frames-select))
 \f
-;;
+
 ;; Threads buffer.  This displays a selectable thread list.
 ;;
 (gdb-set-buffer-rules 'gdb-threads-buffer
@@ -1331,7 +1618,7 @@ static char *magick[] = {
     (let ((buffer-read-only nil))
       (goto-char (point-min))
       (while (< (point) (point-max))
-       (add-text-properties (point-at-bol) (point-at-eol)
+       (add-text-properties (line-beginning-position) (line-end-position)
                             '(mouse-face highlight
                               help-echo "mouse-2, RET: select thread"))
        (forward-line 1)))))
@@ -1349,55 +1636,60 @@ static char *magick[] = {
 (defun gdb-frame-threads-buffer ()
   "Display IDs of currently known threads in a new frame."
   (interactive)
-  (select-frame (make-frame gdb-frame-parameters))
-  (switch-to-buffer (gdb-get-create-buffer 'gdb-threads-buffer))
-  (set-window-dedicated-p (selected-window) t))
+  (let ((special-display-regexps (append special-display-regexps '(".*")))
+       (special-display-frame-alist gdb-frame-parameters))
+    (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
 
 (defvar gdb-threads-mode-map
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map)
+    (define-key map "q" 'kill-this-buffer)
     (define-key map "\r" 'gdb-threads-select)
-    (define-key map [mouse-2] 'gdb-threads-mouse-select)
+    (define-key map [mouse-2] 'gdb-threads-select)
     map))
 
 (defun gdb-threads-mode ()
   "Major mode for gdb frames.
 
 \\{gdb-threads-mode-map}"
+  (kill-all-local-variables)
   (setq major-mode 'gdb-threads-mode)
   (setq mode-name "Threads")
   (setq buffer-read-only t)
   (use-local-map gdb-threads-mode-map)
-  (gdb-invalidate-threads))
+  (run-mode-hooks 'gdb-threads-mode-hook)
+  'gdb-invalidate-threads)
 
 (defun gdb-get-thread-number ()
   (save-excursion
     (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
     (match-string-no-properties 1)))
 
-(defun gdb-threads-select ()
+(defun gdb-threads-select (&optional event)
   "Select the thread and display the relevant source."
-  (interactive)
+  (interactive (list last-input-event))
+  (if event (mouse-set-point event))
   (gdb-enqueue-input
    (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
   (gud-display-frame))
-
-(defun gdb-threads-mouse-select (event)
-  "Select the thread you click on and display the relevant source."
-  (interactive "e")
-  (mouse-set-point event)
-  (gdb-threads-select))
 \f
-;;
+
 ;; Registers buffer.
 ;;
+(defcustom gdb-all-registers nil
+  "Non-nil means include floating-point registers."
+  :type 'boolean
+  :group 'gud
+  :version "22.1")
+
 (gdb-set-buffer-rules 'gdb-registers-buffer
                      'gdb-registers-buffer-name
                      'gdb-registers-mode)
 
 (def-gdb-auto-updated-buffer gdb-registers-buffer
   gdb-invalidate-registers
-  (concat gdb-server-prefix "info registers\n")
+  (concat
+   gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
   gdb-info-registers-handler
   gdb-info-registers-custom)
 
@@ -1406,17 +1698,23 @@ static char *magick[] = {
 (defvar gdb-registers-mode-map
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map)
-    map))
+    (define-key map " " 'toggle-gdb-all-registers)
+    (define-key map "q" 'kill-this-buffer)
+     map))
 
 (defun gdb-registers-mode ()
   "Major mode for gdb registers.
 
 \\{gdb-registers-mode-map}"
+  (kill-all-local-variables)
   (setq major-mode 'gdb-registers-mode)
-  (setq mode-name "Registers")
+  (setq mode-name "Registers:")
   (setq buffer-read-only t)
   (use-local-map gdb-registers-mode-map)
-  (gdb-invalidate-registers))
+  (run-mode-hooks 'gdb-registers-mode-hook)
+  (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
+      'gdb-invalidate-registers
+    'gdbmi-invalidate-registers))
 
 (defun gdb-registers-buffer-name ()
   (with-current-buffer gud-comint-buffer
@@ -1431,11 +1729,324 @@ static char *magick[] = {
 (defun gdb-frame-registers-buffer ()
   "Display integer register contents in a new frame."
   (interactive)
-  (select-frame (make-frame gdb-frame-parameters))
-  (switch-to-buffer (gdb-get-create-buffer 'gdb-registers-buffer))
-  (set-window-dedicated-p (selected-window) t))
+  (let ((special-display-regexps (append special-display-regexps '(".*")))
+       (special-display-frame-alist gdb-frame-parameters))
+    (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
+
+(defun toggle-gdb-all-registers ()
+  "Toggle the display of floating-point registers."
+  (interactive)
+  (if gdb-all-registers
+      (progn
+       (setq gdb-all-registers nil)
+       (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
+         (setq mode-name "Registers:")))
+       (setq gdb-all-registers t)
+       (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
+         (setq mode-name "Registers:All")))
+  (gdb-invalidate-registers))
 \f
+
+;; Memory buffer.
 ;;
+(defcustom gdb-memory-repeat-count 32
+  "Number of data items in memory window."
+  :type 'integer
+  :group 'gud
+  :version "22.1")
+
+(defcustom gdb-memory-format "x"
+  "Display format of data items in memory window."
+  :type '(choice (const :tag "Hexadecimal" "x")
+                (const :tag "Signed decimal" "d")
+                (const :tag "Unsigned decimal" "u")
+                (const :tag "Octal" "o")
+                (const :tag "Binary" "t"))
+  :group 'gud
+  :version "22.1")
+
+(defcustom gdb-memory-unit "w"
+  "Unit size of data items in memory window."
+  :type '(choice (const :tag "Byte" "b")
+                (const :tag "Halfword" "h")
+                (const :tag "Word" "w")
+                (const :tag "Giant word" "g"))
+  :group 'gud
+  :version "22.1")
+
+(gdb-set-buffer-rules 'gdb-memory-buffer
+                     'gdb-memory-buffer-name
+                     'gdb-memory-mode)
+
+(def-gdb-auto-updated-buffer gdb-memory-buffer
+  gdb-invalidate-memory
+  (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
+         gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
+  gdb-read-memory-handler
+  gdb-read-memory-custom)
+
+(defun gdb-read-memory-custom ()
+  (save-excursion
+    (goto-char (point-min))
+    (if (looking-at "0x[[:xdigit:]]+")
+       (setq gdb-memory-address (match-string 0)))))
+
+(defvar gdb-memory-mode-map
+  (let ((map (make-sparse-keymap)))
+    (suppress-keymap map)
+    (define-key map "q" 'kill-this-buffer)
+     map))
+
+(defun gdb-memory-set-address (event)
+  "Set the start memory address."
+  (interactive "e")
+  (save-selected-window
+    (select-window (posn-window (event-start event)))
+    (let ((arg (read-from-minibuffer "Memory address: ")))
+      (setq gdb-memory-address arg))
+    (gdb-invalidate-memory)))
+
+(defun gdb-memory-set-repeat-count (event)
+  "Set the number of data items in memory window."
+  (interactive "e")
+  (save-selected-window
+    (select-window (posn-window (event-start event)))
+    (let* ((arg (read-from-minibuffer "Repeat count: "))
+         (count (string-to-number arg)))
+      (if (<= count 0)
+         (error "Positive numbers only")
+       (customize-set-variable 'gdb-memory-repeat-count count)
+       (gdb-invalidate-memory)))))
+
+(defun gdb-memory-format-binary ()
+  "Set the display format to binary."
+  (interactive)
+  (customize-set-variable 'gdb-memory-format "t")
+  (gdb-invalidate-memory))
+
+(defun gdb-memory-format-octal ()
+  "Set the display format to octal."
+  (interactive)
+  (customize-set-variable 'gdb-memory-format "o")
+  (gdb-invalidate-memory))
+
+(defun gdb-memory-format-unsigned ()
+  "Set the display format to unsigned decimal."
+  (interactive)
+  (customize-set-variable 'gdb-memory-format "u")
+  (gdb-invalidate-memory))
+
+(defun gdb-memory-format-signed ()
+  "Set the display format to decimal."
+  (interactive)
+  (customize-set-variable 'gdb-memory-format "d")
+  (gdb-invalidate-memory))
+
+(defun gdb-memory-format-hexadecimal ()
+  "Set the display format to hexadecimal."
+  (interactive)
+  (customize-set-variable 'gdb-memory-format "x")
+  (gdb-invalidate-memory))
+
+(defvar gdb-memory-format-keymap
+  (let ((map (make-sparse-keymap)))
+    (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
+    map)
+ "Keymap to select format in the header line.")
+
+(defvar gdb-memory-format-menu (make-sparse-keymap "Format")
+ "Menu of display formats in the header line.")
+
+(define-key gdb-memory-format-menu [binary]
+  '(menu-item "Binary" gdb-memory-format-binary
+             :button (:radio . (equal gdb-memory-format "t"))))
+(define-key gdb-memory-format-menu [octal]
+  '(menu-item "Octal" gdb-memory-format-octal
+             :button (:radio . (equal gdb-memory-format "o"))))
+(define-key gdb-memory-format-menu [unsigned]
+  '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
+             :button (:radio . (equal gdb-memory-format "u"))))
+(define-key gdb-memory-format-menu [signed]
+  '(menu-item "Signed Decimal" gdb-memory-format-signed
+             :button (:radio . (equal gdb-memory-format "d"))))
+(define-key gdb-memory-format-menu [hexadecimal]
+  '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
+             :button (:radio . (equal gdb-memory-format "x"))))
+
+(defun gdb-memory-format-menu (event)
+  (interactive "@e")
+  (x-popup-menu event gdb-memory-format-menu))
+
+(defun gdb-memory-format-menu-1 (event)
+  (interactive "e")
+  (save-selected-window
+    (select-window (posn-window (event-start event)))
+    (let* ((selection (gdb-memory-format-menu event))
+          (binding (and selection (lookup-key gdb-memory-format-menu
+                                              (vector (car selection))))))
+      (if binding (call-interactively binding)))))
+
+(defun gdb-memory-unit-giant ()
+  "Set the unit size to giant words (eight bytes)."
+  (interactive)
+  (customize-set-variable 'gdb-memory-unit "g")
+  (gdb-invalidate-memory))
+
+(defun gdb-memory-unit-word ()
+  "Set the unit size to words (four bytes)."
+  (interactive)
+  (customize-set-variable 'gdb-memory-unit "w")
+  (gdb-invalidate-memory))
+
+(defun gdb-memory-unit-halfword ()
+  "Set the unit size to halfwords (two bytes)."
+  (interactive)
+  (customize-set-variable 'gdb-memory-unit "h")
+  (gdb-invalidate-memory))
+
+(defun gdb-memory-unit-byte ()
+  "Set the unit size to bytes."
+  (interactive)
+  (customize-set-variable 'gdb-memory-unit "b")
+  (gdb-invalidate-memory))
+
+(defvar gdb-memory-unit-keymap
+  (let ((map (make-sparse-keymap)))
+    (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
+    map)
+ "Keymap to select units in the header line.")
+
+(defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
+ "Menu of units in the header line.")
+
+(define-key gdb-memory-unit-menu [giantwords]
+  '(menu-item "Giant words" gdb-memory-unit-giant
+             :button (:radio . (equal gdb-memory-unit "g"))))
+(define-key gdb-memory-unit-menu [words]
+  '(menu-item "Words" gdb-memory-unit-word
+             :button (:radio . (equal gdb-memory-unit "w"))))
+(define-key gdb-memory-unit-menu [halfwords]
+  '(menu-item "Halfwords" gdb-memory-unit-halfword
+             :button (:radio . (equal gdb-memory-unit "h"))))
+(define-key gdb-memory-unit-menu [bytes]
+  '(menu-item "Bytes" gdb-memory-unit-byte
+             :button (:radio . (equal gdb-memory-unit "b"))))
+
+(defun gdb-memory-unit-menu (event)
+  (interactive "@e")
+  (x-popup-menu event gdb-memory-unit-menu))
+
+(defun gdb-memory-unit-menu-1 (event)
+  (interactive "e")
+  (save-selected-window
+    (select-window (posn-window (event-start event)))
+    (let* ((selection (gdb-memory-unit-menu event))
+          (binding (and selection (lookup-key gdb-memory-unit-menu
+                                              (vector (car selection))))))
+      (if binding (call-interactively binding)))))
+
+;;from make-mode-line-mouse-map
+(defun gdb-make-header-line-mouse-map (mouse function) "\
+Return a keymap with single entry for mouse key MOUSE on the header line.
+MOUSE is defined to run function FUNCTION with no args in the buffer
+corresponding to the mode line clicked."
+  (let ((map (make-sparse-keymap)))
+    (define-key map (vector 'header-line mouse) function)
+    (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
+    map))
+
+(defun gdb-memory-mode ()
+  "Major mode for examining memory.
+
+\\{gdb-memory-mode-map}"
+  (kill-all-local-variables)
+  (setq major-mode 'gdb-memory-mode)
+  (setq mode-name "Memory")
+  (setq buffer-read-only t)
+  (use-local-map gdb-memory-mode-map)
+  (setq header-line-format
+       '(:eval
+         (concat
+          "Read address["
+          (propertize
+           "-"
+           'face font-lock-warning-face
+           'help-echo "mouse-1: Decrement address"
+           'mouse-face 'mode-line-highlight
+           'local-map
+           (gdb-make-header-line-mouse-map
+            'mouse-1
+            #'(lambda () (interactive)
+                (let ((gdb-memory-address
+                       ;; Let GDB do the arithmetic.
+                       (concat
+                        gdb-memory-address " - "
+                        (number-to-string
+                         (* gdb-memory-repeat-count
+                            (cond ((string= gdb-memory-unit "b") 1)
+                                  ((string= gdb-memory-unit "h") 2)
+                                  ((string= gdb-memory-unit "w") 4)
+                                  ((string= gdb-memory-unit "g") 8)))))))
+                      (gdb-invalidate-memory)))))
+          "|"
+          (propertize "+"
+                      'face font-lock-warning-face
+                      'help-echo "mouse-1: Increment address"
+                      'mouse-face 'mode-line-highlight
+                      'local-map (gdb-make-header-line-mouse-map
+                                  'mouse-1
+                                  #'(lambda () (interactive)
+                                      (let ((gdb-memory-address nil))
+                                        (gdb-invalidate-memory)))))
+          "]: "
+          (propertize gdb-memory-address
+                      'face font-lock-warning-face
+                      'help-echo "mouse-1: Set memory address"
+                      'mouse-face 'mode-line-highlight
+                      'local-map (gdb-make-header-line-mouse-map
+                                  'mouse-1
+                                  #'gdb-memory-set-address))
+          "  Repeat Count: "
+          (propertize (number-to-string gdb-memory-repeat-count)
+                      'face font-lock-warning-face
+                      'help-echo "mouse-1: Set repeat count"
+                      'mouse-face 'mode-line-highlight
+                      'local-map (gdb-make-header-line-mouse-map
+                                  'mouse-1
+                                  #'gdb-memory-set-repeat-count))
+          "  Display Format: "
+          (propertize gdb-memory-format
+                      'face font-lock-warning-face
+                      'help-echo "mouse-3: Select display format"
+                      'mouse-face 'mode-line-highlight
+                      'local-map gdb-memory-format-keymap)
+          "  Unit Size: "
+          (propertize gdb-memory-unit
+                      'face font-lock-warning-face
+                      'help-echo "mouse-3: Select unit size"
+                      'mouse-face 'mode-line-highlight
+                      'local-map gdb-memory-unit-keymap))))
+  (run-mode-hooks 'gdb-memory-mode-hook)
+  'gdb-invalidate-memory)
+
+(defun gdb-memory-buffer-name ()
+  (with-current-buffer gud-comint-buffer
+    (concat "*memory of " (gdb-get-target-string) "*")))
+
+(defun gdb-display-memory-buffer ()
+  "Display memory contents."
+  (interactive)
+  (gdb-display-buffer
+   (gdb-get-create-buffer 'gdb-memory-buffer)))
+
+(defun gdb-frame-memory-buffer ()
+  "Display memory contents in a new frame."
+  (interactive)
+  (let ((special-display-regexps (append special-display-regexps '(".*")))
+       (special-display-frame-alist gdb-frame-parameters))
+    (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
+\f
+
 ;; Locals buffer.
 ;;
 (gdb-set-buffer-rules 'gdb-locals-buffer
@@ -1456,22 +2067,22 @@ static char *magick[] = {
   (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
     (with-current-buffer buf
       (goto-char (point-min))
-      (while (re-search-forward "^ .*\n" nil t)
+      (while (re-search-forward "^[ }].*\n" nil t)
        (replace-match "" nil nil))
       (goto-char (point-min))
-      (while (re-search-forward "{[-0-9, {}\]*\n" nil t)
-       (replace-match "(array);\n" nil nil))
+      (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
+       (replace-match "(structure);\n" nil nil))
       (goto-char (point-min))
-      (while (re-search-forward "{.*=.*\n" nil t)
-       (replace-match "(structure);\n" nil nil))))
+      (while (re-search-forward "\\s-*{.*\n" nil t)
+       (replace-match " (array);\n" nil nil))))
   (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
     (and buf (with-current-buffer buf
-              (let ((p (point))
+              (let ((p (window-point (get-buffer-window buf 0)))
                     (buffer-read-only nil))
-                (delete-region (point-min) (point-max))
+                (erase-buffer)
                 (insert-buffer-substring (gdb-get-create-buffer
                                           'gdb-partial-output-buffer))
-                (goto-char p)))))
+               (set-window-point (get-buffer-window buf 0) p)))))
   (run-hooks 'gdb-info-locals-hook))
 
 (defun gdb-info-locals-custom ()
@@ -1480,19 +2091,22 @@ static char *magick[] = {
 (defvar gdb-locals-mode-map
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map)
-    map))
+    (define-key map "q" 'kill-this-buffer)
+     map))
 
 (defun gdb-locals-mode ()
   "Major mode for gdb locals.
 
 \\{gdb-locals-mode-map}"
+  (kill-all-local-variables)
   (setq major-mode 'gdb-locals-mode)
-  (setq mode-name "Locals")
+  (setq mode-name (concat "Locals:" gdb-selected-frame))
   (setq buffer-read-only t)
   (use-local-map gdb-locals-mode-map)
+  (run-mode-hooks 'gdb-locals-mode-hook)
   (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
-      (gdb-invalidate-locals)
-    (gdbmi-invalidate-locals)))
+      'gdb-invalidate-locals
+    'gdbmi-invalidate-locals))
 
 (defun gdb-locals-buffer-name ()
   (with-current-buffer gud-comint-buffer
@@ -1507,100 +2121,94 @@ static char *magick[] = {
 (defun gdb-frame-locals-buffer ()
   "Display local variables of current stack and their values in a new frame."
   (interactive)
-  (select-frame (make-frame gdb-frame-parameters))
-  (switch-to-buffer (gdb-get-create-buffer 'gdb-locals-buffer))
-  (set-window-dedicated-p (selected-window) t))
+  (let ((special-display-regexps (append special-display-regexps '(".*")))
+       (special-display-frame-alist gdb-frame-parameters))
+    (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
 \f
 
 ;;;; Window management
-
-;;; The way we abuse the dedicated-p flag is pretty gross, but seems
-;;; to do the right thing.  Seeing as there is no way for Lisp code to
-;;; get at the use_time field of a window, I'm not sure there exists a
-;;; more elegant solution without writing C code.
-
 (defun gdb-display-buffer (buf &optional size)
-  (let ((must-split nil)
-       (answer nil))
-    (unwind-protect
-       (progn
-         (walk-windows
-          #'(lambda (win)
-             (if (eq gud-comint-buffer (window-buffer win))
-                 (set-window-dedicated-p win t))))
-         (setq answer (get-buffer-window buf 'visible))
-         (if (not answer)
-             (let ((window (get-lru-window 'visible)))
-               (if window
-                   (progn
-                     (set-window-buffer window buf)
-                     (setq answer window))
-                 (setq must-split t)))))
-      (walk-windows
-       #'(lambda (win)
-         (if (eq gud-comint-buffer (window-buffer win))
-             (set-window-dedicated-p win nil)))))
-    (if must-split
-       (let* ((largest (get-largest-window 'visible))
-              (cur-size (window-height largest))
-              (new-size (and size (< size cur-size) (- cur-size size))))
-         (setq answer (split-window largest new-size))
-         (set-window-buffer answer buf)))
-    answer))
-
-(defun gdb-display-source-buffer (buffer)
-  (if (eq gdb-selected-view 'source)
-       (gdb-display-buffer buffer)
-    (gdb-display-buffer (gdb-get-buffer 'gdb-assembler-buffer)))
-    (get-buffer-window buffer 'visible))
+  (let ((answer (get-buffer-window buf 0))
+       (must-split nil))
+    (if answer
+       (display-buffer buf nil 0)      ;Raise the frame if necessary.
+      ;; The buffer is not yet displayed.
+      (pop-to-buffer gud-comint-buffer)        ;Select the right frame.
+      (let ((window (get-lru-window)))
+       (if (and window
+           (not (eq window (get-buffer-window gud-comint-buffer))))
+           (progn
+             (set-window-buffer window buf)
+             (setq answer window))
+         (setq must-split t)))
+      (if must-split
+         (let* ((largest (get-largest-window))
+                (cur-size (window-height largest))
+                (new-size (and size (< size cur-size) (- cur-size size))))
+           (setq answer (split-window largest new-size))
+           (set-window-buffer answer buf)
+           (set-window-dedicated-p answer t)))
+      answer)))
 
 \f
 ;;; Shared keymap initialization:
 
-(let ((menu (make-sparse-keymap "GDB-Frames")))
-  (define-key gud-menu-map [frames]
-    `(menu-item "GDB-Frames" ,menu :visible (eq gud-minor-mode 'gdba)))
-  (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
-  (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
-  (define-key menu [assembler] '("Machine" . gdb-frame-assembler-buffer))
-  (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
-  (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
-  (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
-  (define-key menu [breakpoints] '("Breakpoints" . gdb-frame-breakpoints-buffer)))
-
 (let ((menu (make-sparse-keymap "GDB-Windows")))
   (define-key gud-menu-map [displays]
-    `(menu-item "GDB-Windows" ,menu :visible (eq gud-minor-mode 'gdba)))
+    `(menu-item "GDB-Windows" ,menu
+               :visible (memq gud-minor-mode '(gdbmi gdba))))
   (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
-  (define-key menu [assembler] '("Machine" . gdb-display-assembler-buffer))
   (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
+  (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
+  (define-key menu [disassembly]
+    '("Disassembly" . gdb-display-assembler-buffer))
   (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
+  (define-key menu [inferior]
+    '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
+               :enable gdb-use-inferior-io-buffer))
   (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
   (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
-  (define-key menu [breakpoints] '("Breakpoints" . gdb-display-breakpoints-buffer)))
-
-(let ((menu (make-sparse-keymap "View")))
-   (define-key gud-menu-map [view]
-     `(menu-item "View" ,menu :visible (eq gud-minor-mode 'gdba)))
-;  (define-key menu [both] '(menu-item "Both" gdb-view-both
-;             :help "Display both source and assembler"
-;             :button (:radio . (eq gdb-selected-view 'both))))
-   (define-key menu [assembler] '(menu-item "Machine" gdb-view-assembler
-              :help "Display assembler only"
-              :button (:radio . (eq gdb-selected-view 'assembler))))
-   (define-key menu [source] '(menu-item "Source" gdb-view-source-function
-              :help "Display source only"
-              :button (:radio . (eq gdb-selected-view 'source)))))
+  (define-key menu [breakpoints]
+    '("Breakpoints" . gdb-display-breakpoints-buffer)))
+
+(let ((menu (make-sparse-keymap "GDB-Frames")))
+  (define-key gud-menu-map [frames]
+    `(menu-item "GDB-Frames" ,menu
+               :visible (memq gud-minor-mode '(gdbmi gdba))))
+  (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
+  (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
+  (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
+  (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
+  (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
+  (define-key menu [inferior]
+    '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
+               :enable gdb-use-inferior-io-buffer))
+  (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
+  (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
+  (define-key menu [breakpoints]
+    '("Breakpoints" . gdb-frame-breakpoints-buffer)))
 
 (let ((menu (make-sparse-keymap "GDB-UI")))
   (define-key gud-menu-map [ui]
     `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
-  (define-key menu [gdb-restore-windows]
-    '("Restore window layout" . gdb-restore-windows))
+  (define-key menu [gdb-use-inferior-io]
+    ;; See defadvice below.
+    (menu-bar-make-toggle toggle-gdb-use-inferior-io-buffer
+                         gdb-use-inferior-io-buffer
+     "Separate inferior IO" "Use separate IO %s"
+     "Toggle separate IO for inferior."))
   (define-key menu [gdb-many-windows]
-    (menu-bar-make-toggle gdb-many-windows gdb-many-windows
-                         "Display other windows" "Many Windows %s"
-                         "Display locals, stack and breakpoint information")))
+  '(menu-item "Display Other Windows" gdb-many-windows
+             :help "Toggle display of locals, stack and breakpoint information"
+             :button (:toggle . gdb-many-windows)))
+  (define-key menu [gdb-restore-windows]
+  '(menu-item "Restore Window Layout" gdb-restore-windows
+             :help "Restore standard layout for debug session.")))
+
+;; This function is defined above through a macro.
+(defadvice toggle-gdb-use-inferior-io-buffer (after gdb-kill-io-buffer activate)
+  (unless gdb-use-inferior-io-buffer
+    (kill-buffer (gdb-inferior-io-name))))
 
 (defun gdb-frame-gdb-buffer ()
   "Display GUD buffer in a new frame."
@@ -1615,77 +2223,53 @@ static char *magick[] = {
   (gdb-display-buffer
    (gdb-get-create-buffer 'gdba)))
 
-(defvar gdb-main-file nil "Source file from which program execution begins.")
-
-(defun gdb-view-source-function ()
-  "Select source view."
-  (interactive)
-  (if gdb-view-source
-      (gdb-display-buffer
-       (if gud-last-last-frame
-          (gud-find-file (car gud-last-last-frame))
-        (gud-find-file gdb-main-file))))
-  (setq gdb-selected-view 'source))
-
-(defun gdb-view-assembler()
-  "Select disassembly view."
-  (interactive)
-  (gdb-display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))
-  (gdb-invalidate-assembler)
-  (setq gdb-selected-view 'assembler))
-
-;(defun gdb-view-both()
-;(interactive)
-;(setq gdb-selected-view 'both))
-
-(defcustom gdb-show-main nil
-  "Nil means don't display source file containing the main routine."
-  :type 'boolean
-  :group 'gud)
+(defun gdb-set-window-buffer (name)
+  (set-window-buffer (selected-window) (get-buffer name))
+  (set-window-dedicated-p (selected-window) t))
 
 (defun gdb-setup-windows ()
-  "Layout the window pattern for gdb-many-windows."
+  "Layout the window pattern for `gdb-many-windows'."
   (gdb-display-locals-buffer)
   (gdb-display-stack-buffer)
   (delete-other-windows)
   (gdb-display-breakpoints-buffer)
   (delete-other-windows)
-  (switch-to-buffer gud-comint-buffer)
+  ; Don't dedicate.
+  (pop-to-buffer gud-comint-buffer)
   (split-window nil ( / ( * (window-height) 3) 4))
   (split-window nil ( / (window-height) 3))
   (split-window-horizontally)
   (other-window 1)
-  (switch-to-buffer (gdb-locals-buffer-name))
+  (gdb-set-window-buffer (gdb-locals-buffer-name))
   (other-window 1)
   (switch-to-buffer
-   (if (and gdb-view-source
-           (eq gdb-selected-view 'source))
        (if gud-last-last-frame
           (gud-find-file (car gud-last-last-frame))
-        (gud-find-file gdb-main-file))
-     (gdb-get-create-buffer 'gdb-assembler-buffer)))
+        (gud-find-file gdb-main-file)))
   (when gdb-use-inferior-io-buffer
     (split-window-horizontally)
     (other-window 1)
-    (switch-to-buffer (gdb-inferior-io-name)))
+    (gdb-set-window-buffer
+     (gdb-get-create-buffer 'gdb-inferior-io)))
   (other-window 1)
-  (switch-to-buffer (gdb-stack-buffer-name))
+  (gdb-set-window-buffer (gdb-stack-buffer-name))
   (split-window-horizontally)
   (other-window 1)
-  (switch-to-buffer (gdb-breakpoints-buffer-name))
+  (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
   (other-window 1))
 
 (defcustom gdb-many-windows nil
-  "Nil (the default value) means just pop up the GUD buffer
-unless `gdb-show-main' is t. In this case it starts with two
-windows: one displaying the GUD buffer and the other with the
-source file with the main routine of the inferior. Non-nil means
-display the layout shown for `gdba'."
+  "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
+In this case it starts with two windows: one displaying the GUD
+buffer and the other with the source file with the main routine
+of the inferior.  Non-nil means display the layout shown for
+`gdba'."
   :type 'boolean
-  :group 'gud)
+  :group 'gud
+  :version "22.1")
 
 (defun gdb-many-windows (arg)
-"Toggle the number of windows in the basic arrangement."
+  "Toggle the number of windows in the basic arrangement."
   (interactive "P")
   (setq gdb-many-windows
        (if (null arg)
@@ -1699,27 +2283,21 @@ display the layout shown for `gdba'."
   "Restore the basic arrangement of windows used by gdba.
 This arrangement depends on the value of `gdb-many-windows'."
   (interactive)
-  (if gdb-many-windows
-      (progn
-       (switch-to-buffer gud-comint-buffer)
-       (delete-other-windows)
-       (gdb-setup-windows))
-    (switch-to-buffer gud-comint-buffer)
+  (pop-to-buffer gud-comint-buffer)    ;Select the right window and frame.
     (delete-other-windows)
+  (if gdb-many-windows
+      (gdb-setup-windows)
     (split-window)
     (other-window 1)
     (switch-to-buffer
-     (if (and gdb-view-source
-             (eq gdb-selected-view 'source))
         (if gud-last-last-frame
             (gud-find-file (car gud-last-last-frame))
-          (gud-find-file gdb-main-file))
-       (gdb-get-create-buffer 'gdb-assembler-buffer)))
+          (gud-find-file gdb-main-file)))
     (other-window 1)))
 
 (defun gdb-reset ()
-  "Exit a debugging session cleanly by killing the gdb buffers and resetting
- the source buffers."
+  "Exit a debugging session cleanly.
+Kills the gdb buffers and resets the source buffers."
   (dolist (buffer (buffer-list))
     (unless (eq buffer gud-comint-buffer)
       (with-current-buffer buffer
@@ -1729,54 +2307,92 @@ This arrangement depends on the value of `gdb-many-windows'."
              (gdb-remove-breakpoint-icons (point-min) (point-max) t)
              (setq gud-minor-mode nil)
              (kill-local-variable 'tool-bar-map)
-             (setq gud-running nil))))))
+             (kill-local-variable 'gdb-define-alist))))))
   (when (markerp gdb-overlay-arrow-position)
     (move-marker gdb-overlay-arrow-position nil)
     (setq gdb-overlay-arrow-position nil))
   (setq overlay-arrow-variable-list
-       (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list)))
+       (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
+  (setq gud-running nil)
+  (setq gdb-active-process nil)
+  (remove-hook 'after-save-hook 'gdb-create-define-alist t))
 
 (defun gdb-source-info ()
   "Find the source file where the program starts and displays it with related
 buffers."
   (goto-char (point-min))
-  (if (search-forward "directory is " nil t)
-      (if (looking-at "\\S-*:\\(\\S-*\\)")
-         (setq gdb-cdir (match-string 1))
-       (looking-at "\\S-*")
-       (setq gdb-cdir (match-string 0))))
-  (if (search-forward "Located in " nil t)
-      (if (looking-at "\\S-*")
-         (setq gdb-main-file (match-string 0)))
-    (setq gdb-view-source nil))
-  (if gdb-many-windows
+  (if (and (search-forward "Located in " nil t)
+          (looking-at "\\S-+"))
+      (setq gdb-main-file (match-string 0)))
+  (goto-char (point-min))
+  (if (search-forward "Includes preprocessor macro info." nil t)
+      (setq gdb-macro-info t))
+ (if gdb-many-windows
       (gdb-setup-windows)
-    (gdb-get-create-buffer 'gdb-breakpoints-buffer)
-    (when gdb-show-main
-      (switch-to-buffer gud-comint-buffer)
-      (delete-other-windows)
-      (split-window)
-      (other-window 1)
-      (switch-to-buffer
-       (if gdb-view-source
-          (gud-find-file gdb-main-file)
-        (gdb-get-create-buffer 'gdb-assembler-buffer)))
-      (other-window 1))))
+   (gdb-get-create-buffer 'gdb-breakpoints-buffer)
+   (if gdb-show-main
+       (let ((pop-up-windows t))
+        (display-buffer (gud-find-file gdb-main-file))))))
+
+(defun gdb-get-location (bptno line flag)
+  "Find the directory containing the relevant source file.
+Put in buffer and place breakpoint icon."
+  (goto-char (point-min))
+  (catch 'file-not-found
+    (if (search-forward "Located in " nil t)
+       (when (looking-at "\\S-+")
+         (delete (cons bptno "File not found") gdb-location-alist)
+         (push (cons bptno (match-string 0)) gdb-location-alist))
+      (gdb-resync)
+      (unless (assoc bptno gdb-location-alist)
+       (push (cons bptno "File not found") gdb-location-alist)
+       (message-box "Cannot find source file for breakpoint location.\n\
+Add directory to search path for source files using the GDB command, dir."))
+      (throw 'file-not-found nil))
+    (with-current-buffer
+       (find-file-noselect (match-string 0))
+      (save-current-buffer
+       (set (make-local-variable 'gud-minor-mode) 'gdba)
+       (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
+      ;; only want one breakpoint icon at each location
+      (save-excursion
+       (goto-line (string-to-number line))
+       (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
+
+(add-hook 'find-file-hook 'gdb-find-file-hook)
+
+(defun gdb-find-file-hook ()
+"Set up buffer for debugging if file is part of the source code
+of the current session."
+  (if (and (not gdb-find-file-unhook)
+          ;; in case gud or gdb-ui is just loaded
+          gud-comint-buffer
+          (buffer-name gud-comint-buffer)
+          (with-current-buffer gud-comint-buffer
+            (eq gud-minor-mode 'gdba)))
+      (condition-case nil
+       (gdb-enqueue-input
+        (list (concat gdb-server-prefix "list "
+                      (file-name-nondirectory buffer-file-name)
+                      ":1\n")
+              `(lambda () (gdb-set-gud-minor-mode ,(current-buffer)))))
+       (error (setq gdb-find-file-unhook t)))))
 
 ;;from put-image
 (defun gdb-put-string (putstring pos &optional dprop)
   "Put string PUTSTRING in front of POS in the current buffer.
 PUTSTRING is displayed by putting an overlay into the current buffer with a
-`before-string' STRING that has a `display' property whose value is
+`before-string' string that has a `display' property whose value is
 PUTSTRING."
-  (let ((gdb-string "x")
+  (let ((string (make-string 1 ?x))
        (buffer (current-buffer)))
+    (setq putstring (copy-sequence putstring))
     (let ((overlay (make-overlay pos pos buffer))
          (prop (or dprop
                    (list (list 'margin 'left-margin) putstring))))
-      (put-text-property 0 (length gdb-string) 'display prop gdb-string)
+      (put-text-property 0 (length string) 'display prop string)
       (overlay-put overlay 'put-break t)
-      (overlay-put overlay 'before-string gdb-string))))
+      (overlay-put overlay 'before-string string))))
 
 ;;from remove-images
 (defun gdb-remove-strings (start end &optional buffer)
@@ -1785,35 +2401,36 @@ Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
 BUFFER nil or omitted means use the current buffer."
   (unless buffer
     (setq buffer (current-buffer)))
-  (let ((overlays (overlays-in start end)))
-    (while overlays
-      (let ((overlay (car overlays)))
-       (when (overlay-get overlay 'put-break)
-         (delete-overlay overlay)))
-      (setq overlays (cdr overlays)))))
-
-(defun gdb-put-breakpoint-icon (enabled)
-  (let ((start (progn (beginning-of-line) (- (point) 1)))
-       (end (progn (end-of-line) (+ (point) 1))))
+  (dolist (overlay (overlays-in start end))
+    (when (overlay-get overlay 'put-break)
+         (delete-overlay overlay))))
+
+(defun gdb-put-breakpoint-icon (enabled bptno)
+  (let ((start (- (line-beginning-position) 1))
+       (end (+ (line-end-position) 1))
+       (putstring (if enabled "B" "b")))
+    (add-text-properties
+     0 1 '(help-echo "mouse-1: set/clear bkpt, mouse-3: enable/disable bkpt")
+     putstring)
+    (if enabled (add-text-properties
+                0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
+      (add-text-properties
+       0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
     (gdb-remove-breakpoint-icons start end)
     (if (display-images-p)
        (if (>= (car (window-fringes)) 8)
            (gdb-put-string
             nil (1+ start)
-            `(left-fringe
-              ,(or breakpoint-bitmap
-                   (setq breakpoint-bitmap
-                         (define-fringe-bitmap
-                           "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")))
-              ,(if enabled
-                   'breakpoint-enabled-bitmap-face
-                 'breakpoint-disabled-bitmap-face)))
+            `(left-fringe breakpoint
+                          ,(if enabled
+                               'breakpoint-enabled
+                             'breakpoint-disabled)))
          (when (< left-margin-width 2)
            (save-current-buffer
              (setq left-margin-width 2)
-             (if (get-buffer-window (current-buffer) 'visible)
-                 (set-window-margins 
-                  (get-buffer-window (current-buffer) 'visible)
+             (if (get-buffer-window (current-buffer) 0)
+                 (set-window-margins
+                  (get-buffer-window (current-buffer) 0)
                   left-margin-width right-margin-width))))
          (put-image
           (if enabled
@@ -1834,15 +2451,20 @@ BUFFER nil or omitted means use the current buffer."
                                     (:type pbm :data
                                            ,breakpoint-disabled-pbm-data
                                            :ascent 100))))))
-          (+ start 1) nil 'left-margin))
+          (+ start 1)
+          putstring
+          'left-margin))
       (when (< left-margin-width 2)
        (save-current-buffer
          (setq left-margin-width 2)
-         (if (get-buffer-window (current-buffer) 'visible)
-             (set-window-margins 
-              (get-buffer-window (current-buffer) 'visible)
+         (if (get-buffer-window (current-buffer) 0)
+             (set-window-margins
+              (get-buffer-window (current-buffer) 0)
               left-margin-width right-margin-width))))
-      (gdb-put-string (if enabled "B" "b") (1+ start)))))
+      (gdb-put-string
+       (propertize putstring
+                  'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
+       (1+ start)))))
 
 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
   (gdb-remove-strings start end)
@@ -1850,9 +2472,9 @@ BUFFER nil or omitted means use the current buffer."
       (remove-images start end))
   (when remove-margin
     (setq left-margin-width 0)
-    (if (get-buffer-window (current-buffer) 'visible)
-       (set-window-margins 
-        (get-buffer-window (current-buffer) 'visible)
+    (if (get-buffer-window (current-buffer) 0)
+       (set-window-margins
+        (get-buffer-window (current-buffer) 0)
         left-margin-width right-margin-width))))
 
 \f
@@ -1865,141 +2487,168 @@ BUFFER nil or omitted means use the current buffer."
 
 (def-gdb-auto-updated-buffer gdb-assembler-buffer
   gdb-invalidate-assembler
-  (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
+  (concat gdb-server-prefix "disassemble "
+         (if (member gdb-frame-address '(nil "main")) nil "0x")
+         gdb-frame-address "\n")
   gdb-assembler-handler
   gdb-assembler-custom)
 
 (defun gdb-assembler-custom ()
   (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
-       (pos 1) (address) (flag))
+       (pos 1) (address) (flag) (bptno))
     (with-current-buffer buffer
-      (if (not (equal gdb-current-address "main"))
-         (progn
-           (goto-char (point-min))
-           (if (re-search-forward gdb-current-address nil t)
-               (progn
-                 (setq pos (point))
-                 (beginning-of-line)
-                 (or gdb-overlay-arrow-position
-                     (setq gdb-overlay-arrow-position (make-marker)))
-                 (set-marker gdb-overlay-arrow-position
-                             (point) (current-buffer))))))
-      ;; remove all breakpoint-icons in assembler buffer before updating.
-      (gdb-remove-breakpoint-icons (point-min) (point-max)))
+      (save-excursion
+       (if (not (equal gdb-frame-address "main"))
+           (progn
+             (goto-char (point-min))
+             (if (and gdb-frame-address
+                      (re-search-forward gdb-frame-address nil t))
+                 (progn
+                   (setq pos (point))
+                   (beginning-of-line)
+                   (or gdb-overlay-arrow-position
+                       (setq gdb-overlay-arrow-position (make-marker)))
+                   (set-marker gdb-overlay-arrow-position
+                               (point) (current-buffer))))))
+       ;; remove all breakpoint-icons in assembler buffer before updating.
+       (gdb-remove-breakpoint-icons (point-min) (point-max))))
     (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
       (goto-char (point-min))
       (while (< (point) (- (point-max) 1))
        (forward-line 1)
-       (if (looking-at "[^\t].*breakpoint")
+       (if (looking-at "[^\t].*?breakpoint")
            (progn
              (looking-at
-              "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*0x\\(\\S-*\\)")
-             (setq flag (char-after (match-beginning 1)))
-             (setq address (match-string 2))
-             ;; remove leading 0s from output of info break.
-             (if (string-match "^0+\\(.*\\)" address)
-                 (setq address (match-string 1 address)))
+           "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
+             (setq bptno (match-string 1))
+             (setq flag (char-after (match-beginning 2)))
+             (setq address (match-string 3))
              (with-current-buffer buffer
+               (save-excursion
                  (goto-char (point-min))
                  (if (re-search-forward address nil t)
-                     (gdb-put-breakpoint-icon (eq flag ?y))))))))
-    (if (not (equal gdb-current-address "main"))
-       (set-window-point (get-buffer-window buffer 'visible) pos))))
+                     (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
+    (if (not (equal gdb-frame-address "main"))
+       (set-window-point (get-buffer-window buffer 0) pos))))
 
 (defvar gdb-assembler-mode-map
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map)
-    map))
+    (define-key map "q" 'kill-this-buffer)
+     map))
+
+(defvar gdb-assembler-font-lock-keywords
+  '(;; <__function.name+n>
+    ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
+     (1 font-lock-function-name-face))
+    ;; 0xNNNNNNNN <__function.name+n>: opcode
+    ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
+     (4 font-lock-keyword-face))
+    ;; %register(at least i386)
+    ("%\\sw+" . font-lock-variable-name-face)
+    ("^\\(Dump of assembler code for function\\) \\(.+\\):"
+     (1 font-lock-comment-face)
+     (2 font-lock-function-name-face))
+    ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
+  "Font lock keywords used in `gdb-assembler-mode'.")
 
 (defun gdb-assembler-mode ()
   "Major mode for viewing code assembler.
 
 \\{gdb-assembler-mode-map}"
+  (kill-all-local-variables)
   (setq major-mode 'gdb-assembler-mode)
-  (setq mode-name "Machine")
+  (setq mode-name (concat "Machine:" gdb-selected-frame))
   (setq gdb-overlay-arrow-position nil)
   (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
-  (put 'gdb-overlay-arrow-position 'overlay-arrow-string "=>")
   (setq fringes-outside-margins t)
   (setq buffer-read-only t)
   (use-local-map gdb-assembler-mode-map)
-  (gdb-invalidate-assembler))
+  (gdb-invalidate-assembler)
+  (set (make-local-variable 'font-lock-defaults)
+       '(gdb-assembler-font-lock-keywords))
+  (run-mode-hooks 'gdb-assembler-mode-hook)
+  'gdb-invalidate-assembler)
 
 (defun gdb-assembler-buffer-name ()
   (with-current-buffer gud-comint-buffer
-    (concat "*Machine Code " (gdb-get-target-string) "*")))
+    (concat "*Disassembly of " (gdb-get-target-string) "*")))
 
 (defun gdb-display-assembler-buffer ()
   "Display disassembly view."
   (interactive)
+  (setq gdb-previous-frame nil)
   (gdb-display-buffer
    (gdb-get-create-buffer 'gdb-assembler-buffer)))
 
 (defun gdb-frame-assembler-buffer ()
   "Display disassembly view in a new frame."
   (interactive)
-  (select-frame (make-frame gdb-frame-parameters))
-  (switch-to-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))
-  (set-window-dedicated-p (selected-window) t))
+  (setq gdb-previous-frame nil)
+  (let ((special-display-regexps (append special-display-regexps '(".*")))
+       (special-display-frame-alist gdb-frame-parameters))
+    (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
 
-;; modified because if gdb-current-address has changed value a new command
+;; modified because if gdb-frame-address has changed value a new command
 ;; must be enqueued to update the buffer with the new output
 (defun gdb-invalidate-assembler (&optional ignored)
   (if (gdb-get-buffer 'gdb-assembler-buffer)
       (progn
-       (unless (string-equal gdb-current-frame gdb-previous-frame)
+       (unless (and gdb-selected-frame
+                    (string-equal gdb-selected-frame gdb-previous-frame))
          (if (or (not (member 'gdb-invalidate-assembler
                               gdb-pending-triggers))
-                 (not (string-equal gdb-current-address
-                                    gdb-previous-address)))
+                 (not (string-equal gdb-frame-address
+                                    gdb-previous-frame-address)))
          (progn
-           ;; take previous disassemble command off the queue
+           ;; take previous disassemble command, if any, off the queue
            (with-current-buffer gud-comint-buffer
-             (let ((queue gdb-input-queue) (item))
+             (let ((queue gdb-input-queue))
                (dolist (item queue)
                  (if (equal (cdr item) '(gdb-assembler-handler))
                      (setq gdb-input-queue
                            (delete item gdb-input-queue))))))
            (gdb-enqueue-input
-            (list (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
+            (list
+             (concat gdb-server-prefix "disassemble "
+                     (if (member gdb-frame-address '(nil "main")) nil "0x")
+                          gdb-frame-address "\n")
                   'gdb-assembler-handler))
            (push 'gdb-invalidate-assembler gdb-pending-triggers)
-           (setq gdb-previous-address gdb-current-address)
-           (setq gdb-previous-frame gdb-current-frame)))))))
+           (setq gdb-previous-frame-address gdb-frame-address)
+           (setq gdb-previous-frame gdb-selected-frame)))))))
 
-(defun gdb-get-current-frame ()
-  (if (not (member 'gdb-get-current-frame gdb-pending-triggers))
+(defun gdb-get-selected-frame ()
+  (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
       (progn
        (gdb-enqueue-input
         (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
-       (push 'gdb-get-current-frame
+       (push 'gdb-get-selected-frame
               gdb-pending-triggers))))
 
 (defun gdb-frame-handler ()
   (setq gdb-pending-triggers
-   (delq 'gdb-get-current-frame gdb-pending-triggers))
+       (delq 'gdb-get-selected-frame gdb-pending-triggers))
   (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
     (goto-char (point-min))
-    (forward-line)
-    (if (looking-at ".*=\\s-+0x\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? ")
+    (if (re-search-forward  "Stack level \\([0-9]+\\)" nil t)
+       (setq gdb-frame-number (match-string 1)))
+    (goto-char (point-min))
+    (if (re-search-forward
+        ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
        (progn
-         (setq gdb-current-frame (match-string 2))
-         (let ((address (match-string 1)))
-           ;; remove leading 0s from output of info frame command.
-           (if (string-match "^0+\\(.*\\)" address)
-               (setq gdb-current-address
-                     (concat "0x" (match-string 1 address)))
-             (setq gdb-current-address (concat "0x" address))))
-         (if (or (if (not (re-search-forward "(\\S-*:[0-9]*);" nil t))
-                     (progn (setq gdb-view-source nil) t))
-                 (eq gdb-selected-view 'assembler))
-             (progn
-               (gdb-display-buffer
-                (gdb-get-create-buffer 'gdb-assembler-buffer))
-               ;;update with new frame for machine code if necessary
-               (gdb-invalidate-assembler))))))
+         (setq gdb-selected-frame (match-string 2))
+         (if (gdb-get-buffer 'gdb-locals-buffer)
+             (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
+               (setq mode-name (concat "Locals:" gdb-selected-frame))))
+         (if (gdb-get-buffer 'gdb-assembler-buffer)
+             (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
+               (setq mode-name (concat "Machine:" gdb-selected-frame))))
+         (setq gdb-frame-address (match-string 1))))
+    (goto-char (point-min))
     (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
        (setq gdb-current-language (match-string 1))))
+    (gdb-invalidate-assembler))
 
 (provide 'gdb-ui)