1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006
8 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This mode acts as a graphical user interface to GDB. You can interact with
30 ;; GDB through the GUD buffer in the usual way, but there are also further
31 ;; buffers which control the execution and describe the state of your program.
32 ;; It separates the input/output of your program from that of GDB, if
33 ;; required, and watches expressions in the speedbar. It also uses features of
34 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
35 ;; (see the GDB Graphical Interface section in the Emacs info manual).
37 ;; By default, M-x gdb will start the debugger. However, if you have customised
38 ;; gud-gdb-command-name, then start it with M-x gdba.
40 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
41 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
42 ;; You don't need to know about annotations to use this mode as a debugger,
43 ;; but if you are interested developing the mode itself, then see the
44 ;; Annotations section in the GDB info manual.
46 ;; GDB developers plan to make the annotation interface obsolete. A new
47 ;; interface called GDB/MI (machine interface) has been designed to replace
48 ;; it. Some GDB/MI commands are used in this file through the CLI command
49 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
50 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
51 ;; still under development and is part of a process to migrate Emacs from
52 ;; annotations to GDB/MI.
54 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
55 ;; onwards to use watch expressions. It works best with GDB 6.4 where
56 ;; watch expressions will update more quickly.
58 ;;; Windows Platforms:
60 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
61 ;; explicitly in your program if you want timely display of I/O in Emacs.
62 ;; Alternatively you can make the output stream unbuffered, for example, by
66 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
69 ;; and compiling with -DUNBUFFERED while debugging.
73 ;; 1) Strings that are watched don't update in the speedbar when their
74 ;; contents change unless the first character changes.
75 ;; 2) Cannot handle multiple debug sessions.
76 ;; 3) Initially, the assembler buffer does not display the cursor at the
77 ;; current line if the line is not visible in the window (but when testing
78 ;; gdb-assembler-custom with a lisp debugger it does!).
80 ;;; Problems with watch expressions, GDB/MI:
82 ;; 1) They go out of scope when the inferior is re-run.
83 ;; 2) -stack-list-locals has a type field but also prints type in values field.
84 ;; 3) VARNUM increments even when variable object is not created (maybe trivial).
88 ;; 1) Use MI command -data-read-memory for memory window.
89 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
90 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
91 ;; 4) With gud-print and gud-pstar, print the variable name in the GUD
92 ;; buffer instead of the value's history number.
99 (defvar speedbar-initial-expansion-list-name)
101 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
102 (defvar gdb-previous-frame-address nil)
103 (defvar gdb-memory-address "main")
104 (defvar gdb-previous-frame nil)
105 (defvar gdb-selected-frame nil)
106 (defvar gdb-frame-number nil)
107 (defvar gdb-current-language nil)
108 (defvar gdb-var-list nil
109 "List of variables in watch window.
110 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS) where
111 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
112 (defvar gdb-force-update t
113 "Non-nil means that view of watch expressions will be updated in the speedbar.")
114 (defvar gdb-main-file nil "Source file from which program execution begins.")
115 (defvar gdb-overlay-arrow-position nil)
116 (defvar gdb-server-prefix nil)
117 (defvar gdb-flush-pending-output nil)
118 (defvar gdb-location-alist nil
119 "Alist of breakpoint numbers and full filenames.")
120 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
121 and #define directives otherwise.")
122 (defvar gdb-error "Non-nil when GDB is reporting an error.")
123 (defvar gdb-macro-info nil
124 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
125 (defvar gdb-buffer-fringe-width nil)
126 (defvar gdb-signalled nil)
127 (defvar gdb-source-window nil)
129 (defvar gdb-buffer-type nil
130 "One of the symbols bound in `gdb-buffer-rules'.")
131 (make-variable-buffer-local 'gdb-buffer-type)
133 (defvar gdb-input-queue ()
134 "A list of gdb command objects.")
136 (defvar gdb-prompting nil
137 "True when gdb is idle with no pending input.")
139 (defvar gdb-output-sink 'user
140 "The disposition of the output of the current gdb command.
141 Possible values are these symbols:
143 `user' -- gdb output should be copied to the GUD buffer
146 `inferior' -- gdb output should be copied to the inferior-io buffer.
148 `pre-emacs' -- output should be ignored util the post-prompt
149 annotation is received. Then the output-sink
151 `emacs' -- output should be collected in the partial-output-buffer
152 for subsequent processing by a command. This is the
153 disposition of output generated by commands that
154 gdb mode sends to gdb on its own behalf.
155 `post-emacs' -- ignore output until the prompt annotation is
156 received, then go to USER disposition.
158 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
159 \(`user' and `emacs').")
161 (defvar gdb-current-item nil
162 "The most recent command item sent to gdb.")
164 (defvar gdb-pending-triggers '()
165 "A list of trigger functions that have run later than their output
168 (defvar gdb-first-post-prompt nil)
169 (defvar gdb-version nil)
170 (defvar gdb-locals-font-lock-keywords nil)
171 (defvar gdb-source-file-list nil
172 "List of source files for the current executable")
173 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
175 (defvar gdb-locals-font-lock-keywords-1
177 ;; var = (struct struct_tag) value
178 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
179 (1 font-lock-variable-name-face)
180 (3 font-lock-keyword-face)
181 (4 font-lock-type-face))
182 ;; var = (type) value
183 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
184 (1 font-lock-variable-name-face)
185 (3 font-lock-type-face))
187 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
188 (1 font-lock-variable-name-face))
190 "Font lock keywords used in `gdb-local-mode'.")
192 (defvar gdb-locals-font-lock-keywords-2
195 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
196 (1 font-lock-variable-name-face)
197 (3 font-lock-type-face))
199 "Font lock keywords used in `gdb-local-mode'.")
201 ;; Variables for GDB 6.4+
202 (defvar gdb-register-names nil "List of register names.")
203 (defvar gdb-changed-registers nil
204 "List of changed register numbers (strings).")
207 (defun gdba (command-line)
208 "Run gdb on program FILE in buffer *gud-FILE*.
209 The directory containing FILE becomes the initial working directory
210 and source-file directory for your debugger.
212 If `gdb-many-windows' is nil (the default value) then gdb just
213 pops up the GUD buffer unless `gdb-show-main' is t. In this case
214 it starts with two windows: one displaying the GUD buffer and the
215 other with the source file with the main routine of the inferior.
217 If `gdb-many-windows' is t, regardless of the value of
218 `gdb-show-main', the layout below will appear unless
219 `gdb-use-separate-io-buffer' is nil when the source buffer
220 occupies the full width of the frame. Keybindings are shown in
223 Watch expressions appear in the speedbar/slowbar.
225 The following commands help control operation :
227 `gdb-many-windows' - Toggle the number of windows gdb uses.
228 `gdb-restore-windows' - To restore the window layout.
230 See Info node `(emacs)GDB Graphical Interface' for a more
231 detailed description of this mode.
234 +----------------------------------------------------------------------+
236 +-----------------------------------+----------------------------------+
237 | GUD buffer (I/O of GDB) | Locals buffer |
241 +-----------------------------------+----------------------------------+
242 | Source buffer | I/O buffer (of debugged program) |
250 +-----------------------------------+----------------------------------+
251 | Stack buffer | Breakpoints buffer |
252 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
253 | | RET gdb-goto-breakpoint |
254 | | D gdb-delete-breakpoint |
255 +-----------------------------------+----------------------------------+"
257 (interactive (list (gud-query-cmdline 'gdba)))
259 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
262 (defcustom gdb-debug-ring-max 128
263 "Maximum size of `gdb-debug-ring'."
268 (defvar gdb-debug-ring nil
269 "List of commands, most recent first, sent to and replies received from GDB.
270 This variable is used to debug GDB-UI.")
273 (defcustom gdb-enable-debug nil
274 "Non-nil means record the process input and output in `gdb-debug-ring'."
279 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
280 "Shell command for generating a list of defined macros in a source file.
281 This list is used to display the #define directive associated
282 with an identifier as a tooltip. It works in a debug session with
283 GDB, when gud-tooltip-mode is t.
285 Set `gdb-cpp-define-alist-flags' for any include paths or
291 (defcustom gdb-cpp-define-alist-flags ""
292 "Preprocessor flags for `gdb-cpp-define-alist-program'."
297 (defcustom gdb-show-main nil
298 "Non-nil means display source file containing the main routine at startup.
299 Also display the main routine in the disassembly buffer if present."
304 (defcustom gdb-many-windows nil
305 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
306 In this case it starts with two windows: one displaying the GUD
307 buffer and the other with the source file with the main routine
308 of the inferior. Non-nil means display the layout shown for
314 (defcustom gdb-use-separate-io-buffer nil
315 "Non-nil means display output from the inferior in a separate buffer."
320 (defun gdb-many-windows (arg)
321 "Toggle the number of windows in the basic arrangement.
322 With arg, display additional buffers iff arg is positive."
324 (setq gdb-many-windows
326 (not gdb-many-windows)
327 (> (prefix-numeric-value arg) 0)))
328 (message (format "Display of other windows %sabled"
329 (if gdb-many-windows "en" "dis")))
330 (if (and gud-comint-buffer
331 (buffer-name gud-comint-buffer))
333 (gdb-restore-windows)
336 (defun gdb-use-separate-io-buffer (arg)
337 "Toggle separate IO for inferior.
338 With arg, use separate IO iff arg is positive."
340 (setq gdb-use-separate-io-buffer
342 (not gdb-use-separate-io-buffer)
343 (> (prefix-numeric-value arg) 0)))
344 (message (format "Separate inferior IO %sabled"
345 (if gdb-use-separate-io-buffer "en" "dis")))
346 (if (and gud-comint-buffer
347 (buffer-name gud-comint-buffer))
349 (if gdb-use-separate-io-buffer
350 (if gdb-many-windows (gdb-restore-windows))
351 (kill-buffer (gdb-inferior-io-name)))
354 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
356 (defun gdb-create-define-alist ()
357 "Create an alist of #define directives for GUD tooltips."
358 (let* ((file (buffer-file-name))
360 (with-output-to-string
361 (with-current-buffer standard-output
362 (call-process shell-file-name
363 (if (file-exists-p file) file nil)
364 (list t nil) nil "-c"
365 (concat gdb-cpp-define-alist-program " "
366 gdb-cpp-define-alist-flags)))))
367 (define-list (split-string output "\n" t))
369 (setq gdb-define-alist nil)
370 (dolist (define define-list)
371 (setq name (nth 1 (split-string define "[( ]")))
372 (push (cons name define) gdb-define-alist))))
374 (defun gdb-tooltip-print (expr)
376 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
377 (goto-char (point-min))
379 (if (search-forward "=" nil t)
380 (concat expr (buffer-substring (- (point) 2) (point-max)))
382 ;; remove newline for gud-tooltip-echo-area
383 (substring string 0 (- (length string) 1))))
384 (or gud-tooltip-echo-area tooltip-use-echo-area)))
386 ;; If expr is a macro for a function don't print because of possible dangerous
387 ;; side-effects. Also printing a function within a tooltip generates an
388 ;; unexpected starting annotation (phase error).
389 (defun gdb-tooltip-print-1 (expr)
390 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
391 (goto-char (point-min))
392 (if (search-forward "expands to: " nil t)
393 (unless (looking-at "\\S-+.*(.*).*")
395 (list (concat gdb-server-prefix "print " expr "\n")
396 `(lambda () (gdb-tooltip-print ,expr))))))))
398 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
400 (defun gdb-set-gud-minor-mode-existing-buffers ()
401 "Create list of source files for current GDB session."
402 (goto-char (point-min))
403 (when (search-forward "read in on demand:" nil t)
404 (while (re-search-forward gdb-source-file-regexp nil t)
405 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
406 (dolist (buffer (buffer-list))
407 (with-current-buffer buffer
408 (when (and buffer-file-name
409 (member (file-name-nondirectory buffer-file-name)
410 gdb-source-file-list))
411 (set (make-local-variable 'gud-minor-mode) 'gdba)
412 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
413 (when gud-tooltip-mode
414 (make-local-variable 'gdb-define-alist)
415 (gdb-create-define-alist)
416 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
418 (defun gdb-find-watch-expression ()
419 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
420 (varnum (car var)) expr array)
421 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
422 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
423 (component-list (split-string (match-string 2 varnum) "\\." t)))
424 (setq expr (nth 1 var1))
425 (setq varnumlet (car var1))
426 (dolist (component component-list)
427 (setq var2 (assoc varnumlet gdb-var-list))
428 (setq expr (concat expr
429 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
430 (concat "[" component "]")
431 (concat "." component))))
432 (setq varnumlet (concat varnumlet "." component)))
436 (set (make-local-variable 'gud-minor-mode) 'gdba)
437 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
439 (gud-def gud-break (if (not (string-match "Machine" mode-name))
440 (gud-call "break %f:%l" arg)
444 (gud-call "break *%a" arg)))
445 "\C-b" "Set breakpoint at current line or address.")
447 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
448 (gud-call "clear %f:%l" arg)
452 (gud-call "clear *%a" arg)))
453 "\C-d" "Remove breakpoint at current line or address.")
455 (gud-def gud-until (if (not (string-match "Machine" mode-name))
456 (gud-call "until %f:%l" arg)
460 (gud-call "until *%a" arg)))
461 "\C-u" "Continue to current line or address.")
463 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
464 nil "Start or continue execution.")
466 ;; For debugging Emacs only.
470 "pp1 " (if (eq (buffer-local-value
471 'major-mode (window-buffer)) 'speedbar-mode)
472 (gdb-find-watch-expression) "%e")) arg)
473 nil "Print the emacs s-expression.")
475 (define-key gud-minor-mode-map [left-margin mouse-1]
476 'gdb-mouse-set-clear-breakpoint)
477 (define-key gud-minor-mode-map [left-fringe mouse-1]
478 'gdb-mouse-set-clear-breakpoint)
479 (define-key gud-minor-mode-map [left-fringe mouse-2]
481 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
483 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
485 (define-key gud-minor-mode-map [left-margin mouse-2]
487 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
489 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
491 (define-key gud-minor-mode-map [left-fringe C-mouse-2]
493 (define-key gud-minor-mode-map [left-margin C-mouse-2]
495 (define-key gud-minor-mode-map [left-margin mouse-3]
496 'gdb-mouse-toggle-breakpoint-margin)
497 (define-key gud-minor-mode-map [left-fringe mouse-3]
498 'gdb-mouse-toggle-breakpoint-fringe)
500 (setq comint-input-sender 'gdb-send)
503 (setq gdb-frame-address (if gdb-show-main "main" nil))
504 (setq gdb-previous-frame-address nil
505 gdb-memory-address "main"
506 gdb-previous-frame nil
507 gdb-selected-frame nil
508 gdb-current-language nil
512 gdb-first-post-prompt t
516 gdb-pending-triggers nil
517 gdb-output-sink 'user
518 gdb-server-prefix "server "
519 gdb-flush-pending-output nil
520 gdb-location-alist nil
521 gdb-source-file-list nil
524 gdb-buffer-fringe-width (car (window-fringes))
527 gdb-source-window nil)
529 (setq gdb-buffer-type 'gdba)
531 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
533 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
534 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
538 (if (eq window-system 'w32)
539 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
540 (gdb-enqueue-input (list "set height 0\n" 'ignore))
541 (gdb-enqueue-input (list "set width 0\n" 'ignore))
543 (if (string-equal gdb-version "pre-6.4")
545 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
546 'gdb-set-gud-minor-mode-existing-buffers))
547 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
549 (list "server interpreter mi -data-list-register-names\n"
550 'gdb-get-register-names))
551 ; Needs GDB 6.2 onwards.
553 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
554 'gdb-set-gud-minor-mode-existing-buffers-1))
555 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
557 ;; find source file and compilation directory here
558 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
559 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
560 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
562 (run-hooks 'gdba-mode-hook))
564 (defun gdb-get-version ()
565 (goto-char (point-min))
566 (if (and (re-search-forward gdb-error-regexp nil t)
567 (string-match ".*(missing implementation)" (match-string 1)))
568 (setq gdb-version "pre-6.4")
569 (setq gdb-version "6.4+"))
572 (defun gdb-mouse-until (event)
573 "Continue running until a source line past the current line.
574 The destination source line can be selected either by clicking with mouse-2
575 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
577 (if gud-overlay-arrow-position
578 (let ((start (event-start event))
579 (end (event-end event))
580 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
581 (if (not (string-match "Machine" mode-name))
582 (if (equal buffer (window-buffer (posn-window end)))
583 (with-current-buffer buffer
584 (when (or (equal start end)
585 (equal (posn-point start)
587 gud-overlay-arrow-position)))
588 (setq line (line-number-at-pos (posn-point end)))
589 (gud-call (concat "until " (number-to-string line))))))
590 (if (equal (marker-buffer gdb-overlay-arrow-position)
591 (window-buffer (posn-window end)))
592 (when (or (equal start end)
593 (equal (posn-point start)
595 gdb-overlay-arrow-position)))
597 (goto-line (line-number-at-pos (posn-point end)))
599 (gud-call (concat "until *%a")))))))))
601 (defun gdb-mouse-jump (event)
602 "Set execution address/line.
603 The destination source line can be selected either by clicking with mouse-2
604 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
605 Unlike gdb-mouse-until the destination address can be before the current
606 line, and no execution takes place."
608 (if gud-overlay-arrow-position
609 (let ((start (event-start event))
610 (end (event-end event))
611 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
612 (if (not (string-match "Machine" mode-name))
613 (if (equal buffer (window-buffer (posn-window end)))
614 (with-current-buffer buffer
615 (when (or (equal start end)
616 (equal (posn-point start)
618 gud-overlay-arrow-position)))
619 (setq line (line-number-at-pos (posn-point end)))
620 (progn (gud-call (concat "tbreak " (number-to-string line)))
621 (gud-call (concat "jump " (number-to-string line)))))))
622 (if (equal (marker-buffer gdb-overlay-arrow-position)
623 (window-buffer (posn-window end)))
624 (when (or (equal start end)
625 (equal (posn-point start)
627 gdb-overlay-arrow-position)))
629 (goto-line (line-number-at-pos (posn-point end)))
632 (gud-call (concat "tbreak *%a"))
633 (gud-call (concat "jump *%a"))))))))))
635 (defcustom gdb-speedbar-auto-raise nil
636 "If non-nil raise speedbar every time display of watch expressions is\
642 (defun gdb-speedbar-auto-raise (arg)
643 "Toggle automatic raising of the speedbar for watch expressions.
644 With arg, automatically raise speedbar iff arg is positive."
646 (setq gdb-speedbar-auto-raise
648 (not gdb-speedbar-auto-raise)
649 (> (prefix-numeric-value arg) 0)))
650 (message (format "Auto raising %sabled"
651 (if gdb-speedbar-auto-raise "en" "dis"))))
653 (defcustom gdb-use-colon-colon-notation nil
654 "If non-nil use FUN::VAR format to display variables in the speedbar."
659 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
660 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
662 (defun gud-watch (&optional arg event)
663 "Watch expression at point.
664 With arg, enter name of variable to be watched in the minibuffer."
665 (interactive (list current-prefix-arg last-input-event))
666 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
667 (if (memq minor-mode '(gdbmi gdba))
669 (if event (posn-set-point (event-end event)))
671 (save-selected-window
673 (read-string "Name of variable: ")
674 (tooltip-identifier-from-point (point)))))
675 (catch 'already-watched
676 (dolist (var gdb-var-list)
677 (unless (string-match "\\." (car var))
678 (if (string-equal expr (nth 1 var))
679 (throw 'already-watched nil))))
680 (set-text-properties 0 (length expr) nil expr)
683 (if (eq minor-mode 'gdba)
685 "server interpreter mi \"-var-create - * " expr "\"\n")
686 (concat"-var-create - * " expr "\n"))
687 `(lambda () (gdb-var-create-handler ,expr))))))))
688 (message "gud-watch is a no-op in this mode."))))
690 (defconst gdb-var-create-regexp
691 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
693 (defun gdb-var-create-handler (expr)
694 (goto-char (point-min))
695 (if (re-search-forward gdb-var-create-regexp nil t)
698 (if (and (string-equal gdb-current-language "c")
699 gdb-use-colon-colon-notation gdb-selected-frame)
700 (setq expr (concat gdb-selected-frame "::" expr))
705 (push var gdb-var-list)
707 (unless (string-equal
708 speedbar-initial-expansion-list-name "GUD")
709 (speedbar-change-initial-expansion-list "GUD"))
712 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
713 (concat "server interpreter mi \"-var-evaluate-expression "
715 (concat "-var-evaluate-expression " (car var) "\n"))
716 `(lambda () (gdb-var-evaluate-expression-handler
718 (if (search-forward "Undefined command" nil t)
719 (message-box "Watching expressions requires gdb 6.0 onwards")
720 (message-box "No symbol \"%s\" in current context." expr))))
722 (defun gdb-var-evaluate-expression-handler (varnum changed)
723 (goto-char (point-min))
724 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
725 (let ((var (assoc varnum gdb-var-list)))
727 (if changed (setcar (nthcdr 5 var) 'changed))
728 (setcar (nthcdr 4 var) (read (match-string 1))))))
730 (defun gdb-var-list-children (varnum)
732 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
733 `(lambda () (gdb-var-list-children-handler ,varnum)))))
735 (defconst gdb-var-list-children-regexp
736 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
739 (defun gdb-var-list-children-handler (varnum)
740 (goto-char (point-min))
741 (let ((var-list nil))
742 (catch 'child-already-watched
743 (dolist (var gdb-var-list)
744 (if (string-equal varnum (car var))
747 (while (re-search-forward gdb-var-list-children-regexp nil t)
748 (let ((varchild (list (match-string 1)
753 (if (assoc (car varchild) gdb-var-list)
754 (throw 'child-already-watched nil))
755 (push varchild var-list)
759 "server interpreter mi \"-var-evaluate-expression "
760 (car varchild) "\"\n")
761 `(lambda () (gdb-var-evaluate-expression-handler
762 ,(car varchild) nil)))))))
763 (push var var-list)))
764 (setq gdb-var-list (nreverse var-list)))))
766 (defun gdb-var-update ()
767 (when (not (member 'gdb-var-update gdb-pending-triggers))
769 (list "server interpreter mi \"-var-update *\"\n"
770 'gdb-var-update-handler))
771 (push 'gdb-var-update gdb-pending-triggers)))
773 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\",in_scope=\"\\(.*?\\)\"")
775 (defun gdb-var-update-handler ()
776 (dolist (var gdb-var-list)
777 (setcar (nthcdr 5 var) nil))
778 (goto-char (point-min))
779 (while (re-search-forward gdb-var-update-regexp nil t)
780 (let ((varnum (match-string 1)))
781 (if (string-equal (match-string 2) "false")
782 (let ((var (assoc varnum gdb-var-list)))
783 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
786 (concat "server interpreter mi \"-var-evaluate-expression "
788 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
789 (setq gdb-pending-triggers
790 (delq 'gdb-var-update gdb-pending-triggers))
791 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
792 ;; Dummy command to update speedbar at right time.
793 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
794 ;; Keep gdb-pending-triggers non-nil till end.
795 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
797 (defun gdb-speedbar-refresh ()
798 (setq gdb-pending-triggers
799 (delq 'gdb-speedbar-refresh gdb-pending-triggers))
800 (with-current-buffer gud-comint-buffer
801 (let ((speedbar-verbosity-level 0)
802 (speedbar-shown-directories nil))
804 (speedbar-refresh)))))
806 (defun gdb-var-delete ()
807 "Delete watch expression at point from the speedbar."
809 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
811 (let ((text (speedbar-line-text)))
812 (string-match "\\(\\S-+\\)" text)
813 (let ((expr (match-string 1 text)) var varnum)
815 (dolist (var1 gdb-var-list)
816 (when (string-equal expr (nth 1 var1))
818 (setq varnum (car var1))
819 (throw 'expr-found nil))))
820 (unless (string-match "\\." (car var))
823 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
825 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
826 (concat "-var-delete " varnum "\n"))
828 (setq gdb-var-list (delq var gdb-var-list))
829 (dolist (varchild gdb-var-list)
830 (if (string-match (concat (car var) "\\.") (car varchild))
831 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
833 (defun gdb-edit-value (text token indent)
834 "Assign a value to a variable displayed in the speedbar."
835 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
836 (varnum (car var)) (value))
837 (setq value (read-string "New value: "))
840 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
841 (concat "server interpreter mi \"-var-assign "
842 varnum " " value "\"\n")
843 (concat "-var-assign " varnum " " value "\n"))
844 `(lambda () (gdb-edit-value-handler ,value))))))
846 (defun gdb-edit-value-handler (value)
847 (goto-char (point-min))
848 (if (re-search-forward gdb-error-regexp nil t)
849 (message-box "Invalid number or expression (%s)" value)))
851 (defcustom gdb-show-changed-values t
852 "If non-nil change the face of out of scope variables and changed values.
853 Out of scope variables are suppressed with `shadow' face.
854 Changed values are highlighted with the face `font-lock-warning-face'."
859 (defun gdb-speedbar-expand-node (text token indent)
860 "Expand the node the user clicked on.
861 TEXT is the text of the button we clicked on, a + or - item.
862 TOKEN is data related to this node.
863 INDENT is the current indentation depth."
864 (cond ((string-match "+" text) ;expand this node
866 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
867 (string-equal gdb-version "pre-6.4"))
868 (gdb-var-list-children token)
869 (gdb-var-list-children-1 token)))
870 ((string-match "-" text) ;contract this node
871 (dolist (var gdb-var-list)
872 (if (string-match (concat token "\\.") (car var))
873 (setq gdb-var-list (delq var gdb-var-list))))
874 (speedbar-change-expand-button-char ?+)
875 (speedbar-delete-subblock indent))
876 (t (error "Ooops... not sure what to do")))
877 (speedbar-center-buffer-smartly))
879 (defun gdb-get-target-string ()
880 (with-current-buffer gud-comint-buffer
887 ;; Each buffer has a TYPE -- a symbol that identifies the function
888 ;; of that particular buffer.
890 ;; The usual gdb interaction buffer is given the type `gdba' and
891 ;; is constructed specially.
893 ;; Others are constructed by gdb-get-buffer-create and
894 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
896 (defvar gdb-buffer-rules-assoc '())
898 (defun gdb-get-buffer (key)
899 "Return the gdb buffer tagged with type KEY.
900 The key should be one of the cars in `gdb-buffer-rules-assoc'."
902 (gdb-look-for-tagged-buffer key (buffer-list))))
904 (defun gdb-get-buffer-create (key)
905 "Create a new gdb buffer of the type specified by KEY.
906 The key should be one of the cars in `gdb-buffer-rules-assoc'."
907 (or (gdb-get-buffer key)
908 (let* ((rules (assoc key gdb-buffer-rules-assoc))
909 (name (funcall (gdb-rules-name-maker rules)))
910 (new (get-buffer-create name)))
911 (with-current-buffer new
913 (if (cdr (cdr rules))
914 (setq trigger (funcall (car (cdr (cdr rules))))))
915 (setq gdb-buffer-type key)
916 (set (make-local-variable 'gud-minor-mode)
917 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
918 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
919 (if trigger (funcall trigger)))
922 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
924 (defun gdb-look-for-tagged-buffer (key bufs)
926 (while (and (not retval) bufs)
927 (set-buffer (car bufs))
928 (if (eq gdb-buffer-type key)
929 (setq retval (car bufs)))
930 (setq bufs (cdr bufs)))
934 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
935 ;; at least one and possible more functions. The functions have these
936 ;; roles in defining a buffer type:
938 ;; NAME - Return a name for this buffer type.
940 ;; The remaining function(s) are optional:
942 ;; MODE - called in a new buffer with no arguments, should establish
943 ;; the proper mode for the buffer.
946 (defun gdb-set-buffer-rules (buffer-type &rest rules)
947 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
949 (setcdr binding rules)
950 (push (cons buffer-type rules)
951 gdb-buffer-rules-assoc))))
953 ;; GUD buffers are an exception to the rules
954 (gdb-set-buffer-rules 'gdba 'error)
956 ;; Partial-output buffer : This accumulates output from a command executed on
957 ;; behalf of emacs (rather than the user).
959 (gdb-set-buffer-rules 'gdb-partial-output-buffer
960 'gdb-partial-output-name)
962 (defun gdb-partial-output-name ()
963 (concat "*partial-output-"
964 (gdb-get-target-string)
968 (gdb-set-buffer-rules 'gdb-inferior-io
969 'gdb-inferior-io-name
970 'gdb-inferior-io-mode)
972 (defun gdb-inferior-io-name ()
973 (concat "*input/output of "
974 (gdb-get-target-string)
977 (defun gdb-display-separate-io-buffer ()
978 "Display IO of debugged program in a separate window."
980 (if gdb-use-separate-io-buffer
982 (gdb-get-buffer-create 'gdb-inferior-io))))
984 (defconst gdb-frame-parameters
985 '((height . 14) (width . 80)
987 (tool-bar-lines . nil)
988 (menu-bar-lines . nil)
991 (defun gdb-frame-separate-io-buffer ()
992 "Display IO of inferior in a new frame."
994 (if gdb-use-separate-io-buffer
995 (let ((special-display-regexps (append special-display-regexps '(".*")))
996 (special-display-frame-alist gdb-frame-parameters))
997 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
999 (defvar gdb-inferior-io-mode-map
1000 (let ((map (make-sparse-keymap)))
1001 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1002 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1003 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1004 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1005 (define-key map "\C-d" 'gdb-separate-io-eof)
1008 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1009 "Major mode for gdb inferior-io."
1010 :syntax-table nil :abbrev-table nil
1011 ;; We want to use comint because it has various nifty and familiar
1012 ;; features. We don't need a process, but comint wants one, so create
1014 (make-comint-in-buffer
1015 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1016 (current-buffer) "hexl")
1017 (setq comint-input-sender 'gdb-inferior-io-sender))
1019 (defun gdb-inferior-io-sender (proc string)
1020 ;; PROC is the pseudo-process created to satisfy comint.
1021 (with-current-buffer (process-buffer proc)
1022 (setq proc (get-buffer-process gud-comint-buffer))
1023 (process-send-string proc string)
1024 (process-send-string proc "\n")))
1026 (defun gdb-separate-io-interrupt ()
1027 "Interrupt the program being debugged."
1030 (get-buffer-process gud-comint-buffer) comint-ptyp))
1032 (defun gdb-separate-io-quit ()
1033 "Send quit signal to the program being debugged."
1036 (get-buffer-process gud-comint-buffer) comint-ptyp))
1038 (defun gdb-separate-io-stop ()
1039 "Stop the program being debugged."
1042 (get-buffer-process gud-comint-buffer) comint-ptyp))
1044 (defun gdb-separate-io-eof ()
1045 "Send end-of-file to the program being debugged."
1048 (get-buffer-process gud-comint-buffer)))
1051 ;; gdb communications
1054 ;; INPUT: things sent to gdb
1056 ;; The queues are lists. Each element is either a string (indicating user or
1057 ;; user-like input) or a list of the form:
1059 ;; (INPUT-STRING HANDLER-FN)
1061 ;; The handler function will be called from the partial-output buffer when the
1062 ;; command completes. This is the way to write commands which invoke gdb
1063 ;; commands autonomously.
1065 ;; These lists are consumed tail first.
1068 (defun gdb-send (proc string)
1069 "A comint send filter for gdb.
1070 This filter may simply queue input for a later time."
1071 (with-current-buffer gud-comint-buffer
1072 (let ((inhibit-read-only t))
1073 (remove-text-properties (point-min) (point-max) '(face))))
1074 (let ((item (concat string "\n")))
1077 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1078 (process-send-string proc item))
1079 (gdb-enqueue-input item))))
1081 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1082 ;; is a query, or other non-top-level prompt.
1084 (defun gdb-enqueue-input (item)
1085 (if (not gud-running)
1088 (gdb-send-item item)
1089 (setq gdb-prompting nil))
1090 (push item gdb-input-queue))))
1092 (defun gdb-dequeue-input ()
1093 (let ((queue gdb-input-queue))
1095 (let ((last (car (last queue))))
1096 (unless (nbutlast queue) (setq gdb-input-queue '()))
1099 (defun gdb-send-item (item)
1100 (setq gdb-flush-pending-output nil)
1101 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1102 (setq gdb-current-item item)
1103 (let ((process (get-buffer-process gud-comint-buffer)))
1104 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1107 (setq gdb-output-sink 'user)
1108 (process-send-string process item))
1110 (gdb-clear-partial-output)
1111 (setq gdb-output-sink 'pre-emacs)
1112 (process-send-string process
1114 ;; case: eq gud-minor-mode 'gdbmi
1115 (gdb-clear-partial-output)
1116 (setq gdb-output-sink 'emacs)
1117 (process-send-string process (car item)))))
1120 ;; output -- things gdb prints to emacs
1122 ;; GDB output is a stream interrupted by annotations.
1123 ;; Annotations can be recognized by their beginning
1124 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1126 ;; The tag is a string obeying symbol syntax.
1128 ;; The optional part `<opt>' can be either the empty string
1129 ;; or a space followed by more data relating to the annotation.
1130 ;; For example, the SOURCE annotation is followed by a filename,
1131 ;; line number and various useless goo. This data must not include
1135 (defcustom gud-gdba-command-name "gdb -annotate=3"
1136 "Default command to execute an executable under the GDB-UI debugger."
1141 (defvar gdb-annotation-rules
1142 '(("pre-prompt" gdb-pre-prompt)
1143 ("prompt" gdb-prompt)
1144 ("commands" gdb-subprompt)
1145 ("overload-choice" gdb-subprompt)
1146 ("query" gdb-subprompt)
1147 ;; Need this prompt for GDB 6.1
1148 ("nquery" gdb-subprompt)
1149 ("prompt-for-continue" gdb-subprompt)
1150 ("post-prompt" gdb-post-prompt)
1151 ("source" gdb-source)
1152 ("starting" gdb-starting)
1153 ("exited" gdb-exited)
1154 ("signalled" gdb-signalled)
1155 ("signal" gdb-stopping)
1156 ("breakpoint" gdb-stopping)
1157 ("watchpoint" gdb-stopping)
1158 ("frame-begin" gdb-frame-begin)
1159 ("stopped" gdb-stopped)
1160 ("error-begin" gdb-error)
1162 ) "An assoc mapping annotation tags to functions which process them.")
1165 (setq gdb-flush-pending-output t)
1166 (setq gud-running nil)
1167 (setq gdb-output-sink 'user)
1168 (setq gdb-input-queue nil)
1169 (setq gdb-pending-triggers nil)
1170 (setq gdb-prompting t))
1172 (defconst gdb-source-spec-regexp
1173 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1175 ;; Do not use this except as an annotation handler.
1176 (defun gdb-source (args)
1177 (string-match gdb-source-spec-regexp args)
1178 ;; Extract the frame position from the marker.
1179 (setq gud-last-frame
1181 (match-string 1 args)
1182 (string-to-number (match-string 2 args))))
1183 (setq gdb-frame-address (match-string 3 args))
1184 ;; cover for auto-display output which comes *before*
1185 ;; stopped annotation
1186 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1188 (defun gdb-pre-prompt (ignored)
1189 "An annotation handler for `pre-prompt'.
1190 This terminates the collection of output from a previous command if that
1191 happens to be in effect."
1192 (setq gdb-error nil)
1193 (let ((sink gdb-output-sink))
1197 (setq gdb-output-sink 'post-emacs))
1200 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1202 (defun gdb-prompt (ignored)
1203 "An annotation handler for `prompt'.
1204 This sends the next command (if any) to gdb."
1205 (when gdb-first-prompt
1207 (setq gdb-first-prompt nil))
1208 (let ((sink gdb-output-sink))
1211 ((eq sink 'post-emacs)
1212 (setq gdb-output-sink 'user)
1214 (car (cdr gdb-current-item))))
1215 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1216 (funcall handler))))
1219 (error "Phase error in gdb-prompt (got %s)" sink))))
1220 (let ((input (gdb-dequeue-input)))
1222 (gdb-send-item input)
1224 (setq gdb-prompting t)
1225 (gud-display-frame)))))
1227 (defun gdb-subprompt (ignored)
1228 "An annotation handler for non-top-level prompts."
1229 (setq gdb-prompting t))
1231 (defun gdb-starting (ignored)
1232 "An annotation handler for `starting'.
1233 This says that I/O for the subprocess is now the program being debugged,
1235 (setq gdb-active-process t)
1236 (let ((sink gdb-output-sink))
1240 (setq gud-running t)
1241 (gdb-remove-text-properties)
1242 (setq gud-overlay-arrow-position nil)
1243 (setq gdb-overlay-arrow-position nil)
1244 (if gdb-use-separate-io-buffer
1245 (setq gdb-output-sink 'inferior))))
1248 (error "Unexpected `starting' annotation")))))
1250 (defun gdb-stopping (ignored)
1251 "An annotation handler for `breakpoint' and other annotations.
1252 They say that I/O for the subprocess is now GDB, not the program
1254 (if gdb-use-separate-io-buffer
1255 (let ((sink gdb-output-sink))
1257 ((eq sink 'inferior)
1258 (setq gdb-output-sink 'user))
1261 (error "Unexpected stopping annotation"))))))
1263 (defun gdb-exited (ignored)
1264 "An annotation handler for `exited' and `signalled'.
1265 They say that I/O for the subprocess is now GDB, not the program
1266 being debugged and that the program is no longer running. This
1267 function is used to change the focus of GUD tooltips to #define
1269 (setq gdb-active-process nil)
1270 (setq gud-overlay-arrow-position nil)
1271 (setq gdb-overlay-arrow-position nil)
1272 (gdb-stopping ignored))
1274 (defun gdb-signalled (ignored)
1275 (setq gdb-signalled t))
1277 (defun gdb-frame-begin (ignored)
1278 (let ((sink gdb-output-sink))
1280 ((eq sink 'inferior)
1281 (setq gdb-output-sink 'user))
1283 ((eq sink 'emacs) t)
1286 (error "Unexpected frame-begin annotation (%S)" sink)))))
1288 (defun gdb-stopped (ignored)
1289 "An annotation handler for `stopped'.
1290 It is just like `gdb-stopping', except that if we already set the output
1291 sink to `user' in `gdb-stopping', that is fine."
1292 (setq gud-running nil)
1293 (let ((sink gdb-output-sink))
1295 ((eq sink 'inferior)
1296 (setq gdb-output-sink 'user))
1300 (error "Unexpected stopped annotation"))))
1301 (if gdb-signalled (gdb-exited ignored)))
1303 (defun gdb-error (ignored)
1304 (setq gdb-error (not gdb-error)))
1306 (defun gdb-post-prompt (ignored)
1307 "An annotation handler for `post-prompt'.
1308 This begins the collection of output from the current command if that
1309 happens to be appropriate."
1310 ;; Don't add to queue if there outstanding items or gdb-version is not known
1312 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1313 (gdb-get-selected-frame)
1314 (gdb-invalidate-frames)
1315 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1316 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1317 (gdb-invalidate-breakpoints)
1318 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1319 ;; so gdb-frame-address is updated.
1320 ;; (gdb-invalidate-assembler)
1322 (if (string-equal gdb-version "pre-6.4")
1323 (gdb-invalidate-registers)
1324 (gdb-get-changed-registers)
1325 (gdb-invalidate-registers-1))
1327 (gdb-invalidate-memory)
1328 (if (string-equal gdb-version "pre-6.4")
1329 (gdb-invalidate-locals)
1330 (gdb-invalidate-locals-1))
1332 (gdb-invalidate-threads)
1333 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1334 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1335 ;; Only needed/used with speedbar/watch expressions.
1336 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1337 (setq gdb-force-update t)
1338 (if (string-equal gdb-version "pre-6.4")
1340 (gdb-var-update-1)))))
1341 (setq gdb-first-post-prompt nil)
1342 (let ((sink gdb-output-sink))
1345 ((eq sink 'pre-emacs)
1346 (setq gdb-output-sink 'emacs))
1349 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1351 (defconst gdb-buffer-list
1352 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1354 (defun gdb-remove-text-properties ()
1355 (dolist (buffertype gdb-buffer-list)
1356 (let ((buffer (gdb-get-buffer buffertype)))
1358 (with-current-buffer buffer
1359 (let ((inhibit-read-only t))
1360 (remove-text-properties
1361 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1363 ;; GUD displays the selected GDB frame. This might might not be the current
1364 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1365 ;; visited breakpoint is, use that window.
1366 (defun gdb-display-source-buffer (buffer)
1367 (let* ((last-window (if gud-last-last-frame
1369 (gud-find-file (car gud-last-last-frame)))))
1370 (source-window (or last-window
1371 (if (and gdb-source-window
1372 (window-live-p gdb-source-window))
1373 gdb-source-window))))
1375 (setq gdb-source-window source-window)
1376 (set-window-buffer source-window buffer))
1379 (defun gud-gdba-marker-filter (string)
1380 "A gud marker filter for gdb. Handle a burst of output from GDB."
1381 (if gdb-flush-pending-output
1383 (when gdb-enable-debug
1384 (push (cons 'recv string) gdb-debug-ring)
1385 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1386 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1387 ;; Recall the left over gud-marker-acc from last time.
1388 (setq gud-marker-acc (concat gud-marker-acc string))
1389 ;; Start accumulating output for the GUD buffer.
1392 ;; Process all the complete markers in this chunk.
1393 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1394 (let ((annotation (match-string 1 gud-marker-acc)))
1396 ;; Stuff prior to the match is just ordinary output.
1397 ;; It is either concatenated to OUTPUT or directed
1402 (substring gud-marker-acc 0 (match-beginning 0))))
1404 ;; Take that stuff off the gud-marker-acc.
1405 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1407 ;; Parse the tag from the annotation, and maybe its arguments.
1408 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1409 (let* ((annotation-type (match-string 1 annotation))
1410 (annotation-arguments (match-string 2 annotation))
1411 (annotation-rule (assoc annotation-type
1412 gdb-annotation-rules)))
1413 ;; Call the handler for this annotation.
1415 (funcall (car (cdr annotation-rule))
1416 annotation-arguments)
1417 ;; Else the annotation is not recognized. Ignore it silently,
1418 ;; so that GDB can add new annotations without causing
1422 ;; Does the remaining text end in a partial line?
1423 ;; If it does, then keep part of the gud-marker-acc until we get more.
1424 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1427 ;; Everything before the potential marker start can be output.
1429 (gdb-concat-output output
1430 (substring gud-marker-acc 0
1431 (match-beginning 0))))
1433 ;; Everything after, we save, to combine with later input.
1434 (setq gud-marker-acc (substring gud-marker-acc
1435 (match-beginning 0))))
1437 ;; In case we know the gud-marker-acc contains no partial annotations:
1439 (setq output (gdb-concat-output output gud-marker-acc))
1440 (setq gud-marker-acc "")))
1443 (defun gdb-concat-output (so-far new)
1445 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1446 (let ((sink gdb-output-sink))
1448 ((eq sink 'user) (concat so-far new))
1449 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1451 (gdb-append-to-partial-output new)
1453 ((eq sink 'inferior)
1454 (gdb-append-to-inferior-io new)
1458 (error "Bogon output sink %S" sink)))))
1460 (defun gdb-append-to-partial-output (string)
1461 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1462 (goto-char (point-max))
1465 (defun gdb-clear-partial-output ()
1466 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1469 (defun gdb-append-to-inferior-io (string)
1470 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1471 (goto-char (point-max))
1472 (insert-before-markers string))
1473 (if (not (string-equal string ""))
1474 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1476 (defun gdb-clear-inferior-io ()
1477 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1481 ;; One trick is to have a command who's output is always available in a buffer
1482 ;; of it's own, and is always up to date. We build several buffers of this
1485 ;; There are two aspects to this: gdb has to tell us when the output for that
1486 ;; command might have changed, and we have to be able to run the command
1487 ;; behind the user's back.
1489 ;; The output phasing associated with the variable gdb-output-sink
1490 ;; help us to run commands behind the user's back.
1492 ;; Below is the code for specificly managing buffers of output from one
1496 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1497 ;; It adds an input for the command we are tracking. It should be the
1498 ;; annotation rule binding of whatever gdb sends to tell us this command
1499 ;; might have changed it's output.
1501 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1502 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1503 ;; input in the input queue (see comment about ``gdb communications'' above).
1505 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1507 `(defun ,name (&optional ignored)
1508 (if (and ,demand-predicate
1510 gdb-pending-triggers)))
1513 (list ,gdb-command ',output-handler))
1514 (push ',name gdb-pending-triggers)))))
1516 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1518 (setq gdb-pending-triggers
1520 gdb-pending-triggers))
1521 (let ((buf (gdb-get-buffer ',buf-key)))
1523 (with-current-buffer buf
1524 (let* ((window (get-buffer-window buf 0))
1525 (start (window-start window))
1526 (p (window-point window))
1527 (buffer-read-only nil))
1529 (insert-buffer-substring (gdb-get-buffer-create
1530 'gdb-partial-output-buffer))
1531 (set-window-start window start)
1532 (set-window-point window p)))))
1533 ;; put customisation here
1536 (defmacro def-gdb-auto-updated-buffer (buffer-key
1537 trigger-name gdb-command
1538 output-handler-name custom-defun)
1540 (def-gdb-auto-update-trigger ,trigger-name
1541 ;; The demand predicate:
1542 (gdb-get-buffer ',buffer-key)
1544 ,output-handler-name)
1545 (def-gdb-auto-update-handler ,output-handler-name
1546 ,trigger-name ,buffer-key ,custom-defun)))
1550 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1552 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1553 'gdb-breakpoints-buffer-name
1554 'gdb-breakpoints-mode)
1556 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1557 ;; This defines the auto update rule for buffers of type
1558 ;; `gdb-breakpoints-buffer'.
1560 ;; It defines a function to serve as the annotation handler that
1561 ;; handles the `foo-invalidated' message. That function is called:
1562 gdb-invalidate-breakpoints
1564 ;; To update the buffer, this command is sent to gdb.
1565 "server info breakpoints\n"
1567 ;; This also defines a function to be the handler for the output
1568 ;; from the command above. That function will copy the output into
1569 ;; the appropriately typed buffer. That function will be called:
1570 gdb-info-breakpoints-handler
1571 ;; buffer specific functions
1572 gdb-info-breakpoints-custom)
1574 (defconst breakpoint-xpm-data
1576 static char *magick[] = {
1577 /* columns rows colors chars-per-pixel */
1593 "XPM data used for breakpoint icon.")
1595 (defconst breakpoint-enabled-pbm-data
1598 0 0 0 0 1 1 1 1 0 0 0 0
1599 0 0 0 1 1 1 1 1 1 0 0 0
1600 0 0 1 1 1 1 1 1 1 1 0 0
1601 0 1 1 1 1 1 1 1 1 1 1 0
1602 0 1 1 1 1 1 1 1 1 1 1 0
1603 0 1 1 1 1 1 1 1 1 1 1 0
1604 0 1 1 1 1 1 1 1 1 1 1 0
1605 0 0 1 1 1 1 1 1 1 1 0 0
1606 0 0 0 1 1 1 1 1 1 0 0 0
1607 0 0 0 0 1 1 1 1 0 0 0 0"
1608 "PBM data used for enabled breakpoint icon.")
1610 (defconst breakpoint-disabled-pbm-data
1622 0 0 0 1 0 1 0 1 0 0"
1623 "PBM data used for disabled breakpoint icon.")
1625 (defvar breakpoint-enabled-icon nil
1626 "Icon for enabled breakpoint in display margin.")
1628 (defvar breakpoint-disabled-icon nil
1629 "Icon for disabled breakpoint in display margin.")
1631 ;; Bitmap for breakpoint in fringe
1632 (and (display-images-p)
1633 (define-fringe-bitmap 'breakpoint
1634 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1636 (defface breakpoint-enabled
1640 "Face for enabled breakpoint icon in fringe."
1643 (defface breakpoint-disabled
1644 ;; We use different values of grey for different background types,
1645 ;; so that on low-color displays it will end up as something visible
1646 ;; if it has to be approximated.
1647 '((((background dark)) :foreground "grey60")
1648 (((background light)) :foreground "grey40"))
1649 "Face for disabled breakpoint icon in fringe."
1652 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1653 (defun gdb-info-breakpoints-custom ()
1654 (let ((flag) (bptno))
1655 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1656 (dolist (buffer (buffer-list))
1657 (with-current-buffer buffer
1658 (if (and (memq gud-minor-mode '(gdba gdbmi))
1659 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1660 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1661 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1663 (goto-char (point-min))
1664 (while (< (point) (- (point-max) 1))
1666 (if (looking-at "[^\t].*?breakpoint")
1668 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1669 (setq bptno (match-string 1))
1670 (setq flag (char-after (match-beginning 2)))
1672 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1674 (let ((buffer-read-only nil))
1675 (add-text-properties (match-beginning 1) (match-end 1)
1676 '(face font-lock-function-name-face)))
1677 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1678 (let ((line (match-string 2)) (buffer-read-only nil)
1679 (file (match-string 1)))
1680 (add-text-properties (line-beginning-position)
1682 '(mouse-face highlight
1683 help-echo "mouse-2, RET: visit breakpoint"))
1684 (unless (file-exists-p file)
1685 (setq file (cdr (assoc bptno gdb-location-alist))))
1687 (not (string-equal file "File not found")))
1688 (with-current-buffer
1689 (find-file-noselect file 'nowarn)
1690 (set (make-local-variable 'gud-minor-mode)
1692 (set (make-local-variable 'tool-bar-map)
1694 ;; Only want one breakpoint icon at each
1697 (goto-line (string-to-number line))
1698 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1701 (concat gdb-server-prefix "list "
1702 (match-string-no-properties 1) ":1\n")
1705 (list (concat gdb-server-prefix "info source\n")
1706 `(lambda () (gdb-get-location
1707 ,bptno ,line ,flag))))))))))
1709 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1711 (defun gdb-mouse-set-clear-breakpoint (event)
1712 "Set/clear breakpoint in left fringe/margin with mouse click."
1714 (mouse-minibuffer-check event)
1715 (let ((posn (event-end event)))
1716 (if (numberp (posn-point posn))
1717 (with-selected-window (posn-window posn)
1719 (goto-char (posn-point posn))
1720 (if (or (posn-object posn)
1721 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1724 (gud-break nil)))))))
1726 (defun gdb-mouse-toggle-breakpoint-margin (event)
1727 "Enable/disable breakpoint in left margin with mouse click."
1729 (mouse-minibuffer-check event)
1730 (let ((posn (event-end event)))
1731 (if (numberp (posn-point posn))
1732 (with-selected-window (posn-window posn)
1734 (goto-char (posn-point posn))
1735 (if (posn-object posn)
1738 (let ((bptno (get-text-property
1739 0 'gdb-bptno (car (posn-string posn)))))
1740 (concat gdb-server-prefix
1741 (if (get-text-property
1742 0 'gdb-enabled (car (posn-string posn)))
1748 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1749 "Enable/disable breakpoint in left fringe with mouse click."
1751 (mouse-minibuffer-check event)
1752 (let* ((posn (event-end event))
1753 (pos (posn-point posn))
1756 (with-selected-window (posn-window posn)
1758 (set-buffer (window-buffer (selected-window)))
1760 (dolist (overlay (overlays-in pos pos))
1761 (when (overlay-get overlay 'put-break)
1762 (setq obj (overlay-get overlay 'before-string))))
1766 (concat gdb-server-prefix
1767 (if (get-text-property 0 'gdb-enabled obj)
1770 (get-text-property 0 'gdb-bptno obj) "\n")
1773 (defun gdb-breakpoints-buffer-name ()
1774 (with-current-buffer gud-comint-buffer
1775 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1777 (defun gdb-display-breakpoints-buffer ()
1778 "Display status of user-settable breakpoints."
1781 (gdb-get-buffer-create 'gdb-breakpoints-buffer)))
1783 (defun gdb-frame-breakpoints-buffer ()
1784 "Display status of user-settable breakpoints in a new frame."
1786 (let ((special-display-regexps (append special-display-regexps '(".*")))
1787 (special-display-frame-alist gdb-frame-parameters))
1788 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1790 (defvar gdb-breakpoints-mode-map
1791 (let ((map (make-sparse-keymap))
1792 (menu (make-sparse-keymap "Breakpoints")))
1793 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1794 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1795 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1796 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1797 (suppress-keymap map)
1798 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1799 (define-key map " " 'gdb-toggle-breakpoint)
1800 (define-key map "D" 'gdb-delete-breakpoint)
1801 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1802 (define-key map "q" 'gdb-delete-frame-or-window)
1803 (define-key map "\r" 'gdb-goto-breakpoint)
1804 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1805 (define-key map [follow-link] 'mouse-face)
1808 (defun gdb-delete-frame-or-window ()
1809 "Delete frame if there is only one window. Otherwise delete the window."
1811 (if (one-window-p) (delete-frame)
1814 (defun gdb-breakpoints-mode ()
1815 "Major mode for gdb breakpoints.
1817 \\{gdb-breakpoints-mode-map}"
1818 (kill-all-local-variables)
1819 (setq major-mode 'gdb-breakpoints-mode)
1820 (setq mode-name "Breakpoints")
1821 (use-local-map gdb-breakpoints-mode-map)
1822 (setq buffer-read-only t)
1823 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1824 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1825 'gdb-invalidate-breakpoints
1826 'gdbmi-invalidate-breakpoints))
1828 (defconst gdb-breakpoint-regexp
1829 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1831 (defun gdb-toggle-breakpoint ()
1832 "Enable/disable breakpoint at current line."
1835 (beginning-of-line 1)
1836 (if (looking-at gdb-breakpoint-regexp)
1839 (concat gdb-server-prefix
1840 (if (eq ?y (char-after (match-beginning 2)))
1843 (match-string 1) "\n") 'ignore))
1844 (error "Not recognized as break/watchpoint line"))))
1846 (defun gdb-delete-breakpoint ()
1847 "Delete the breakpoint at current line."
1849 (beginning-of-line 1)
1850 (if (looking-at gdb-breakpoint-regexp)
1853 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1854 (error "Not recognized as break/watchpoint line")))
1856 (defun gdb-goto-breakpoint (&optional event)
1857 "Display the breakpoint location specified at current line."
1858 (interactive (list last-input-event))
1859 (if event (posn-set-point (event-end event)))
1861 (beginning-of-line 1)
1862 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1863 (let ((bptno (match-string 1))
1864 (file (match-string 2))
1865 (line (match-string 3)))
1866 (save-selected-window
1867 (let* ((buffer (find-file-noselect
1868 (if (file-exists-p file) file
1869 (cdr (assoc bptno gdb-location-alist)))))
1870 (window (unless (gdb-display-source-buffer buffer)
1871 (display-buffer buffer))))
1872 (setq gdb-source-window window)
1873 (with-current-buffer buffer
1874 (goto-line (string-to-number line))
1875 (set-window-point window (point))))))
1876 (error "No location specified."))))
1879 ;; Frames buffer. This displays a perpetually correct bactracktrace
1880 ;; (from the command `where').
1882 ;; Alas, if your stack is deep, it is costly.
1884 (gdb-set-buffer-rules 'gdb-stack-buffer
1885 'gdb-stack-buffer-name
1888 (def-gdb-auto-updated-buffer gdb-stack-buffer
1889 gdb-invalidate-frames
1891 gdb-info-frames-handler
1892 gdb-info-frames-custom)
1894 (defun gdb-info-frames-custom ()
1895 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1897 (let ((buffer-read-only nil)
1899 (goto-char (point-min))
1900 (while (< (point) (point-max))
1901 (setq bl (line-beginning-position)
1902 el (line-end-position))
1903 (when (looking-at "#")
1904 (add-text-properties bl el
1905 '(mouse-face highlight
1906 help-echo "mouse-2, RET: Select frame")))
1908 (when (looking-at "^#\\([0-9]+\\)")
1909 (when (string-equal (match-string 1) gdb-frame-number)
1910 (put-text-property bl (+ bl 4)
1911 'face '(:inverse-video t)))
1912 (when (re-search-forward
1914 (if (string-equal (match-string 1) "0") "" " in ")
1915 "\\([^ ]+\\) (") el t)
1916 (put-text-property (match-beginning 1) (match-end 1)
1917 'face font-lock-function-name-face)
1918 (setq bl (match-end 0))
1919 (while (re-search-forward "<\\([^>]+\\)>" el t)
1920 (put-text-property (match-beginning 1) (match-end 1)
1921 'face font-lock-function-name-face))
1923 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1924 (put-text-property (match-beginning 1) (match-end 1)
1925 'face font-lock-variable-name-face))))
1926 (forward-line 1))))))
1928 (defun gdb-stack-buffer-name ()
1929 (with-current-buffer gud-comint-buffer
1930 (concat "*stack frames of " (gdb-get-target-string) "*")))
1932 (defun gdb-display-stack-buffer ()
1933 "Display backtrace of current stack."
1936 (gdb-get-buffer-create 'gdb-stack-buffer)))
1938 (defun gdb-frame-stack-buffer ()
1939 "Display backtrace of current stack in a new frame."
1941 (let ((special-display-regexps (append special-display-regexps '(".*")))
1942 (special-display-frame-alist gdb-frame-parameters))
1943 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
1945 (defvar gdb-frames-mode-map
1946 (let ((map (make-sparse-keymap)))
1947 (suppress-keymap map)
1948 (define-key map "q" 'kill-this-buffer)
1949 (define-key map "\r" 'gdb-frames-select)
1950 (define-key map [mouse-2] 'gdb-frames-select)
1951 (define-key map [follow-link] 'mouse-face)
1954 (defun gdb-frames-mode ()
1955 "Major mode for gdb frames.
1957 \\{gdb-frames-mode-map}"
1958 (kill-all-local-variables)
1959 (setq major-mode 'gdb-frames-mode)
1960 (setq mode-name "Frames")
1961 (setq buffer-read-only t)
1962 (use-local-map gdb-frames-mode-map)
1963 (run-mode-hooks 'gdb-frames-mode-hook)
1964 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1965 'gdb-invalidate-frames
1966 'gdbmi-invalidate-frames))
1968 (defun gdb-get-frame-number ()
1971 (let* ((start (line-beginning-position))
1972 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
1973 (n (or (and pos (match-string-no-properties 1)) "0")))
1976 (defun gdb-frames-select (&optional event)
1977 "Select the frame and display the relevant source."
1978 (interactive (list last-input-event))
1979 (if event (posn-set-point (event-end event)))
1981 (list (concat gdb-server-prefix "frame "
1982 (gdb-get-frame-number) "\n") 'ignore))
1983 (gud-display-frame))
1986 ;; Threads buffer. This displays a selectable thread list.
1988 (gdb-set-buffer-rules 'gdb-threads-buffer
1989 'gdb-threads-buffer-name
1992 (def-gdb-auto-updated-buffer gdb-threads-buffer
1993 gdb-invalidate-threads
1994 (concat gdb-server-prefix "info threads\n")
1995 gdb-info-threads-handler
1996 gdb-info-threads-custom)
1998 (defun gdb-info-threads-custom ()
1999 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2000 (let ((buffer-read-only nil))
2001 (goto-char (point-min))
2002 (while (< (point) (point-max))
2003 (unless (looking-at "No ")
2004 (add-text-properties (line-beginning-position) (line-end-position)
2005 '(mouse-face highlight
2006 help-echo "mouse-2, RET: select thread")))
2007 (forward-line 1)))))
2009 (defun gdb-threads-buffer-name ()
2010 (with-current-buffer gud-comint-buffer
2011 (concat "*threads of " (gdb-get-target-string) "*")))
2013 (defun gdb-display-threads-buffer ()
2014 "Display IDs of currently known threads."
2017 (gdb-get-buffer-create 'gdb-threads-buffer)))
2019 (defun gdb-frame-threads-buffer ()
2020 "Display IDs of currently known threads in a new frame."
2022 (let ((special-display-regexps (append special-display-regexps '(".*")))
2023 (special-display-frame-alist gdb-frame-parameters))
2024 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2026 (defvar gdb-threads-mode-map
2027 (let ((map (make-sparse-keymap)))
2028 (suppress-keymap map)
2029 (define-key map "q" 'kill-this-buffer)
2030 (define-key map "\r" 'gdb-threads-select)
2031 (define-key map [mouse-2] 'gdb-threads-select)
2032 (define-key map [follow-link] 'mouse-face)
2035 (defvar gdb-threads-font-lock-keywords
2037 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2038 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2039 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
2041 "Font lock keywords used in `gdb-threads-mode'.")
2043 (defun gdb-threads-mode ()
2044 "Major mode for gdb frames.
2046 \\{gdb-threads-mode-map}"
2047 (kill-all-local-variables)
2048 (setq major-mode 'gdb-threads-mode)
2049 (setq mode-name "Threads")
2050 (setq buffer-read-only t)
2051 (use-local-map gdb-threads-mode-map)
2052 (set (make-local-variable 'font-lock-defaults)
2053 '(gdb-threads-font-lock-keywords))
2054 (run-mode-hooks 'gdb-threads-mode-hook)
2055 'gdb-invalidate-threads)
2057 (defun gdb-get-thread-number ()
2059 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2060 (match-string-no-properties 1)))
2062 (defun gdb-threads-select (&optional event)
2063 "Select the thread and display the relevant source."
2064 (interactive (list last-input-event))
2065 (if event (posn-set-point (event-end event)))
2067 (list (concat gdb-server-prefix "thread "
2068 (gdb-get-thread-number) "\n") 'ignore))
2069 (gud-display-frame))
2072 ;; Registers buffer.
2074 (defcustom gdb-all-registers nil
2075 "Non-nil means include floating-point registers."
2080 (gdb-set-buffer-rules 'gdb-registers-buffer
2081 'gdb-registers-buffer-name
2082 'gdb-registers-mode)
2084 (def-gdb-auto-updated-buffer gdb-registers-buffer
2085 gdb-invalidate-registers
2087 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2088 gdb-info-registers-handler
2089 gdb-info-registers-custom)
2091 (defun gdb-info-registers-custom ()
2092 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2094 (let ((buffer-read-only nil)
2096 (goto-char (point-min))
2097 (while (< (point) (point-max))
2098 (setq start (line-beginning-position))
2099 (setq end (line-end-position))
2100 (when (looking-at "^[^ ]+")
2101 (unless (string-equal (match-string 0) "The")
2102 (put-text-property start (match-end 0)
2103 'face font-lock-variable-name-face)
2104 (add-text-properties start end
2105 '(help-echo "mouse-2: edit value"
2106 mouse-face highlight))))
2107 (forward-line 1))))))
2109 (defun gdb-edit-register-value (&optional event)
2110 (interactive (list last-input-event))
2112 (if event (posn-set-point (event-end event)))
2114 (let* ((register (current-word))
2115 (value (read-string (format "New value (%s): " register))))
2117 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2120 (defvar gdb-registers-mode-map
2121 (let ((map (make-sparse-keymap)))
2122 (suppress-keymap map)
2123 (define-key map "\r" 'gdb-edit-register-value)
2124 (define-key map [mouse-2] 'gdb-edit-register-value)
2125 (define-key map " " 'gdb-all-registers)
2126 (define-key map "q" 'kill-this-buffer)
2129 (defun gdb-registers-mode ()
2130 "Major mode for gdb registers.
2132 \\{gdb-registers-mode-map}"
2133 (kill-all-local-variables)
2134 (setq major-mode 'gdb-registers-mode)
2135 (setq mode-name "Registers")
2136 (setq buffer-read-only t)
2137 (use-local-map gdb-registers-mode-map)
2138 (run-mode-hooks 'gdb-registers-mode-hook)
2139 (if (string-equal gdb-version "pre-6.4")
2141 (if gdb-all-registers (setq mode-name "Registers:All"))
2142 'gdb-invalidate-registers)
2143 'gdb-invalidate-registers-1))
2145 (defun gdb-registers-buffer-name ()
2146 (with-current-buffer gud-comint-buffer
2147 (concat "*registers of " (gdb-get-target-string) "*")))
2149 (defun gdb-display-registers-buffer ()
2150 "Display integer register contents."
2153 (gdb-get-buffer-create 'gdb-registers-buffer)))
2155 (defun gdb-frame-registers-buffer ()
2156 "Display integer register contents in a new frame."
2158 (let ((special-display-regexps (append special-display-regexps '(".*")))
2159 (special-display-frame-alist gdb-frame-parameters))
2160 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2162 (defun gdb-all-registers ()
2163 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2165 (when (string-equal gdb-version "pre-6.4")
2166 (if gdb-all-registers
2168 (setq gdb-all-registers nil)
2169 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2170 (setq mode-name "Registers")))
2171 (setq gdb-all-registers t)
2172 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2173 (setq mode-name "Registers:All")))
2174 (message (format "Display of floating-point registers %sabled"
2175 (if gdb-all-registers "en" "dis")))
2176 (gdb-invalidate-registers)))
2181 (defcustom gdb-memory-repeat-count 32
2182 "Number of data items in memory window."
2187 (defcustom gdb-memory-format "x"
2188 "Display format of data items in memory window."
2189 :type '(choice (const :tag "Hexadecimal" "x")
2190 (const :tag "Signed decimal" "d")
2191 (const :tag "Unsigned decimal" "u")
2192 (const :tag "Octal" "o")
2193 (const :tag "Binary" "t"))
2197 (defcustom gdb-memory-unit "w"
2198 "Unit size of data items in memory window."
2199 :type '(choice (const :tag "Byte" "b")
2200 (const :tag "Halfword" "h")
2201 (const :tag "Word" "w")
2202 (const :tag "Giant word" "g"))
2206 (gdb-set-buffer-rules 'gdb-memory-buffer
2207 'gdb-memory-buffer-name
2210 (def-gdb-auto-updated-buffer gdb-memory-buffer
2211 gdb-invalidate-memory
2212 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2213 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2214 gdb-read-memory-handler
2215 gdb-read-memory-custom)
2217 (defun gdb-read-memory-custom ()
2219 (goto-char (point-min))
2220 (if (looking-at "0x[[:xdigit:]]+")
2221 (setq gdb-memory-address (match-string 0)))))
2223 (defvar gdb-memory-mode-map
2224 (let ((map (make-sparse-keymap)))
2225 (suppress-keymap map)
2226 (define-key map "q" 'kill-this-buffer)
2229 (defun gdb-memory-set-address (event)
2230 "Set the start memory address."
2232 (save-selected-window
2233 (select-window (posn-window (event-start event)))
2234 (let ((arg (read-from-minibuffer "Memory address: ")))
2235 (setq gdb-memory-address arg))
2236 (gdb-invalidate-memory)))
2238 (defun gdb-memory-set-repeat-count (event)
2239 "Set the number of data items in memory window."
2241 (save-selected-window
2242 (select-window (posn-window (event-start event)))
2243 (let* ((arg (read-from-minibuffer "Repeat count: "))
2244 (count (string-to-number arg)))
2246 (error "Positive numbers only")
2247 (customize-set-variable 'gdb-memory-repeat-count count)
2248 (gdb-invalidate-memory)))))
2250 (defun gdb-memory-format-binary ()
2251 "Set the display format to binary."
2253 (customize-set-variable 'gdb-memory-format "t")
2254 (gdb-invalidate-memory))
2256 (defun gdb-memory-format-octal ()
2257 "Set the display format to octal."
2259 (customize-set-variable 'gdb-memory-format "o")
2260 (gdb-invalidate-memory))
2262 (defun gdb-memory-format-unsigned ()
2263 "Set the display format to unsigned decimal."
2265 (customize-set-variable 'gdb-memory-format "u")
2266 (gdb-invalidate-memory))
2268 (defun gdb-memory-format-signed ()
2269 "Set the display format to decimal."
2271 (customize-set-variable 'gdb-memory-format "d")
2272 (gdb-invalidate-memory))
2274 (defun gdb-memory-format-hexadecimal ()
2275 "Set the display format to hexadecimal."
2277 (customize-set-variable 'gdb-memory-format "x")
2278 (gdb-invalidate-memory))
2280 (defvar gdb-memory-format-map
2281 (let ((map (make-sparse-keymap)))
2282 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2284 "Keymap to select format in the header line.")
2286 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2287 "Menu of display formats in the header line.")
2289 (define-key gdb-memory-format-menu [binary]
2290 '(menu-item "Binary" gdb-memory-format-binary
2291 :button (:radio . (equal gdb-memory-format "t"))))
2292 (define-key gdb-memory-format-menu [octal]
2293 '(menu-item "Octal" gdb-memory-format-octal
2294 :button (:radio . (equal gdb-memory-format "o"))))
2295 (define-key gdb-memory-format-menu [unsigned]
2296 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2297 :button (:radio . (equal gdb-memory-format "u"))))
2298 (define-key gdb-memory-format-menu [signed]
2299 '(menu-item "Signed Decimal" gdb-memory-format-signed
2300 :button (:radio . (equal gdb-memory-format "d"))))
2301 (define-key gdb-memory-format-menu [hexadecimal]
2302 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2303 :button (:radio . (equal gdb-memory-format "x"))))
2305 (defun gdb-memory-format-menu (event)
2307 (x-popup-menu event gdb-memory-format-menu))
2309 (defun gdb-memory-format-menu-1 (event)
2311 (save-selected-window
2312 (select-window (posn-window (event-start event)))
2313 (let* ((selection (gdb-memory-format-menu event))
2314 (binding (and selection (lookup-key gdb-memory-format-menu
2315 (vector (car selection))))))
2316 (if binding (call-interactively binding)))))
2318 (defun gdb-memory-unit-giant ()
2319 "Set the unit size to giant words (eight bytes)."
2321 (customize-set-variable 'gdb-memory-unit "g")
2322 (gdb-invalidate-memory))
2324 (defun gdb-memory-unit-word ()
2325 "Set the unit size to words (four bytes)."
2327 (customize-set-variable 'gdb-memory-unit "w")
2328 (gdb-invalidate-memory))
2330 (defun gdb-memory-unit-halfword ()
2331 "Set the unit size to halfwords (two bytes)."
2333 (customize-set-variable 'gdb-memory-unit "h")
2334 (gdb-invalidate-memory))
2336 (defun gdb-memory-unit-byte ()
2337 "Set the unit size to bytes."
2339 (customize-set-variable 'gdb-memory-unit "b")
2340 (gdb-invalidate-memory))
2342 (defvar gdb-memory-unit-map
2343 (let ((map (make-sparse-keymap)))
2344 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2346 "Keymap to select units in the header line.")
2348 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2349 "Menu of units in the header line.")
2351 (define-key gdb-memory-unit-menu [giantwords]
2352 '(menu-item "Giant words" gdb-memory-unit-giant
2353 :button (:radio . (equal gdb-memory-unit "g"))))
2354 (define-key gdb-memory-unit-menu [words]
2355 '(menu-item "Words" gdb-memory-unit-word
2356 :button (:radio . (equal gdb-memory-unit "w"))))
2357 (define-key gdb-memory-unit-menu [halfwords]
2358 '(menu-item "Halfwords" gdb-memory-unit-halfword
2359 :button (:radio . (equal gdb-memory-unit "h"))))
2360 (define-key gdb-memory-unit-menu [bytes]
2361 '(menu-item "Bytes" gdb-memory-unit-byte
2362 :button (:radio . (equal gdb-memory-unit "b"))))
2364 (defun gdb-memory-unit-menu (event)
2366 (x-popup-menu event gdb-memory-unit-menu))
2368 (defun gdb-memory-unit-menu-1 (event)
2370 (save-selected-window
2371 (select-window (posn-window (event-start event)))
2372 (let* ((selection (gdb-memory-unit-menu event))
2373 (binding (and selection (lookup-key gdb-memory-unit-menu
2374 (vector (car selection))))))
2375 (if binding (call-interactively binding)))))
2377 ;;from make-mode-line-mouse-map
2378 (defun gdb-make-header-line-mouse-map (mouse function) "\
2379 Return a keymap with single entry for mouse key MOUSE on the header line.
2380 MOUSE is defined to run function FUNCTION with no args in the buffer
2381 corresponding to the mode line clicked."
2382 (let ((map (make-sparse-keymap)))
2383 (define-key map (vector 'header-line mouse) function)
2384 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2387 (defvar gdb-memory-font-lock-keywords
2388 '(;; <__function.name+n>
2389 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2391 "Font lock keywords used in `gdb-memory-mode'.")
2393 (defun gdb-memory-mode ()
2394 "Major mode for examining memory.
2396 \\{gdb-memory-mode-map}"
2397 (kill-all-local-variables)
2398 (setq major-mode 'gdb-memory-mode)
2399 (setq mode-name "Memory")
2400 (setq buffer-read-only t)
2401 (use-local-map gdb-memory-mode-map)
2402 (setq header-line-format
2408 'face font-lock-warning-face
2409 'help-echo "mouse-1: Decrement address"
2410 'mouse-face 'mode-line-highlight
2412 (gdb-make-header-line-mouse-map
2414 #'(lambda () (interactive)
2415 (let ((gdb-memory-address
2416 ;; Let GDB do the arithmetic.
2418 gdb-memory-address " - "
2420 (* gdb-memory-repeat-count
2421 (cond ((string= gdb-memory-unit "b") 1)
2422 ((string= gdb-memory-unit "h") 2)
2423 ((string= gdb-memory-unit "w") 4)
2424 ((string= gdb-memory-unit "g") 8)))))))
2425 (gdb-invalidate-memory)))))
2428 'face font-lock-warning-face
2429 'help-echo "mouse-1: Increment address"
2430 'mouse-face 'mode-line-highlight
2431 'local-map (gdb-make-header-line-mouse-map
2433 #'(lambda () (interactive)
2434 (let ((gdb-memory-address nil))
2435 (gdb-invalidate-memory)))))
2437 (propertize gdb-memory-address
2438 'face font-lock-warning-face
2439 'help-echo "mouse-1: Set memory address"
2440 'mouse-face 'mode-line-highlight
2441 'local-map (gdb-make-header-line-mouse-map
2443 #'gdb-memory-set-address))
2445 (propertize (number-to-string gdb-memory-repeat-count)
2446 'face font-lock-warning-face
2447 'help-echo "mouse-1: Set repeat count"
2448 'mouse-face 'mode-line-highlight
2449 'local-map (gdb-make-header-line-mouse-map
2451 #'gdb-memory-set-repeat-count))
2453 (propertize gdb-memory-format
2454 'face font-lock-warning-face
2455 'help-echo "mouse-3: Select display format"
2456 'mouse-face 'mode-line-highlight
2457 'local-map gdb-memory-format-map)
2459 (propertize gdb-memory-unit
2460 'face font-lock-warning-face
2461 'help-echo "mouse-3: Select unit size"
2462 'mouse-face 'mode-line-highlight
2463 'local-map gdb-memory-unit-map))))
2464 (set (make-local-variable 'font-lock-defaults)
2465 '(gdb-memory-font-lock-keywords))
2466 (run-mode-hooks 'gdb-memory-mode-hook)
2467 'gdb-invalidate-memory)
2469 (defun gdb-memory-buffer-name ()
2470 (with-current-buffer gud-comint-buffer
2471 (concat "*memory of " (gdb-get-target-string) "*")))
2473 (defun gdb-display-memory-buffer ()
2474 "Display memory contents."
2477 (gdb-get-buffer-create 'gdb-memory-buffer)))
2479 (defun gdb-frame-memory-buffer ()
2480 "Display memory contents in a new frame."
2482 (let ((special-display-regexps (append special-display-regexps '(".*")))
2483 (special-display-frame-alist gdb-frame-parameters))
2484 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2489 (gdb-set-buffer-rules 'gdb-locals-buffer
2490 'gdb-locals-buffer-name
2493 (def-gdb-auto-update-trigger gdb-invalidate-locals
2494 (gdb-get-buffer 'gdb-locals-buffer)
2495 "server info locals\n"
2496 gdb-info-locals-handler)
2498 (defvar gdb-locals-watch-map
2499 (let ((map (make-sparse-keymap)))
2500 (define-key map "\r" '(lambda () (interactive)
2503 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2504 (mouse-set-point event)
2508 "Keymap to create watch expression of a complex data type local variable.")
2510 (defconst gdb-struct-string
2511 (concat (propertize "[struct/union]"
2512 'mouse-face 'highlight
2513 'help-echo "mouse-2: create watch expression"
2514 'local-map gdb-locals-watch-map) "\n"))
2516 (defconst gdb-array-string
2517 (concat " " (propertize "[array]"
2518 'mouse-face 'highlight
2519 'help-echo "mouse-2: create watch expression"
2520 'local-map gdb-locals-watch-map) "\n"))
2522 ;; Abbreviate for arrays and structures.
2523 ;; These can be expanded using gud-display.
2524 (defun gdb-info-locals-handler ()
2525 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2526 gdb-pending-triggers))
2527 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2528 (with-current-buffer buf
2529 (goto-char (point-min))
2530 (while (re-search-forward "^[ }].*\n" nil t)
2531 (replace-match "" nil nil))
2532 (goto-char (point-min))
2533 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2534 (replace-match gdb-struct-string nil nil))
2535 (goto-char (point-min))
2536 (while (re-search-forward "\\s-*{.*\n" nil t)
2537 (replace-match gdb-array-string nil nil))))
2538 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2540 (with-current-buffer buf
2541 (let* ((window (get-buffer-window buf 0))
2542 (start (window-start window))
2543 (p (window-point window))
2544 (buffer-read-only nil))
2546 (insert-buffer-substring (gdb-get-buffer-create
2547 'gdb-partial-output-buffer))
2548 (set-window-start window start)
2549 (set-window-point window p))
2551 (run-hooks 'gdb-info-locals-hook))
2553 (defvar gdb-locals-mode-map
2554 (let ((map (make-sparse-keymap)))
2555 (suppress-keymap map)
2556 (define-key map "q" 'kill-this-buffer)
2559 (defun gdb-locals-mode ()
2560 "Major mode for gdb locals.
2562 \\{gdb-locals-mode-map}"
2563 (kill-all-local-variables)
2564 (setq major-mode 'gdb-locals-mode)
2565 (setq mode-name (concat "Locals:" gdb-selected-frame))
2566 (setq buffer-read-only t)
2567 (use-local-map gdb-locals-mode-map)
2568 (set (make-local-variable 'font-lock-defaults)
2569 '(gdb-locals-font-lock-keywords))
2570 (run-mode-hooks 'gdb-locals-mode-hook)
2571 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2572 (string-equal gdb-version "pre-6.4"))
2573 'gdb-invalidate-locals
2574 'gdb-invalidate-locals-1))
2576 (defun gdb-locals-buffer-name ()
2577 (with-current-buffer gud-comint-buffer
2578 (concat "*locals of " (gdb-get-target-string) "*")))
2580 (defun gdb-display-locals-buffer ()
2581 "Display local variables of current stack and their values."
2584 (gdb-get-buffer-create 'gdb-locals-buffer)))
2586 (defun gdb-frame-locals-buffer ()
2587 "Display local variables of current stack and their values in a new frame."
2589 (let ((special-display-regexps (append special-display-regexps '(".*")))
2590 (special-display-frame-alist gdb-frame-parameters))
2591 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2594 ;;;; Window management
2595 (defun gdb-display-buffer (buf &optional size)
2596 (let ((answer (get-buffer-window buf 0))
2599 (display-buffer buf nil 0) ;Raise the frame if necessary.
2600 ;; The buffer is not yet displayed.
2601 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2602 (let ((window (get-lru-window)))
2604 (not (eq window (get-buffer-window gud-comint-buffer))))
2606 (set-window-buffer window buf)
2607 (setq answer window))
2608 (setq must-split t)))
2610 (let* ((largest (get-largest-window))
2611 (cur-size (window-height largest))
2612 (new-size (and size (< size cur-size) (- cur-size size))))
2613 (setq answer (split-window largest new-size))
2614 (set-window-buffer answer buf)
2615 (set-window-dedicated-p answer t)))
2619 ;;; Shared keymap initialization:
2621 (let ((menu (make-sparse-keymap "GDB-Windows")))
2622 (define-key gud-menu-map [displays]
2623 `(menu-item "GDB-Windows" ,menu
2624 :visible (memq gud-minor-mode '(gdbmi gdba))))
2625 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2626 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2627 (define-key menu [inferior]
2628 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2629 :enable gdb-use-separate-io-buffer))
2630 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2631 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2632 (define-key menu [disassembly]
2633 '("Disassembly" . gdb-display-assembler-buffer))
2634 (define-key menu [breakpoints]
2635 '("Breakpoints" . gdb-display-breakpoints-buffer))
2636 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2637 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2639 (let ((menu (make-sparse-keymap "GDB-Frames")))
2640 (define-key gud-menu-map [frames]
2641 `(menu-item "GDB-Frames" ,menu
2642 :visible (memq gud-minor-mode '(gdbmi gdba))))
2643 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2644 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2645 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2646 (define-key menu [inferior]
2647 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2648 :enable gdb-use-separate-io-buffer))
2649 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2650 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2651 (define-key menu [breakpoints]
2652 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2653 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2654 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2656 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2657 (define-key gud-menu-map [ui]
2658 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2659 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2660 (define-key menu [gdb-use-separate-io]
2661 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2662 :visible (eq gud-minor-mode 'gdba)
2663 :help "Toggle separate IO for inferior."
2664 :button (:toggle . gdb-use-separate-io-buffer)))
2665 (define-key menu [gdb-many-windows]
2666 '(menu-item "Display Other Windows" gdb-many-windows
2667 :help "Toggle display of locals, stack and breakpoint information"
2668 :button (:toggle . gdb-many-windows)))
2669 (define-key menu [gdb-restore-windows]
2670 '(menu-item "Restore Window Layout" gdb-restore-windows
2671 :help "Restore standard layout for debug session.")))
2673 (defun gdb-frame-gdb-buffer ()
2674 "Display GUD buffer in a new frame."
2676 (let ((special-display-regexps (append special-display-regexps '(".*")))
2677 (special-display-frame-alist gdb-frame-parameters)
2678 (same-window-regexps nil))
2679 (display-buffer gud-comint-buffer)))
2681 (defun gdb-display-gdb-buffer ()
2682 "Display GUD buffer."
2684 (let ((same-window-regexps nil))
2685 (pop-to-buffer gud-comint-buffer)))
2687 (defun gdb-set-window-buffer (name)
2688 (set-window-buffer (selected-window) (get-buffer name))
2689 (set-window-dedicated-p (selected-window) t))
2691 (defun gdb-setup-windows ()
2692 "Layout the window pattern for `gdb-many-windows'."
2693 (gdb-display-locals-buffer)
2694 (gdb-display-stack-buffer)
2695 (delete-other-windows)
2696 (gdb-display-breakpoints-buffer)
2697 (delete-other-windows)
2699 (pop-to-buffer gud-comint-buffer)
2700 (split-window nil ( / ( * (window-height) 3) 4))
2701 (split-window nil ( / (window-height) 3))
2702 (split-window-horizontally)
2704 (gdb-set-window-buffer (gdb-locals-buffer-name))
2707 (if gud-last-last-frame
2708 (gud-find-file (car gud-last-last-frame))
2709 (gud-find-file gdb-main-file)))
2710 (when gdb-use-separate-io-buffer
2711 (split-window-horizontally)
2713 (gdb-set-window-buffer
2714 (gdb-get-buffer-create 'gdb-inferior-io)))
2716 (gdb-set-window-buffer (gdb-stack-buffer-name))
2717 (split-window-horizontally)
2719 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2722 (defun gdb-restore-windows ()
2723 "Restore the basic arrangement of windows used by gdba.
2724 This arrangement depends on the value of `gdb-many-windows'."
2726 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2727 (delete-other-windows)
2728 (if gdb-many-windows
2730 (when (or gud-last-last-frame gdb-show-main)
2734 (if gud-last-last-frame
2735 (gud-find-file (car gud-last-last-frame))
2736 (gud-find-file gdb-main-file)))
2740 "Exit a debugging session cleanly.
2741 Kills the gdb buffers, and resets variables and the source buffers."
2742 (dolist (buffer (buffer-list))
2743 (unless (eq buffer gud-comint-buffer)
2744 (with-current-buffer buffer
2745 (if (memq gud-minor-mode '(gdbmi gdba))
2746 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2748 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2749 (setq gud-minor-mode nil)
2750 (kill-local-variable 'tool-bar-map)
2751 (kill-local-variable 'gdb-define-alist))))))
2752 (when (markerp gdb-overlay-arrow-position)
2753 (move-marker gdb-overlay-arrow-position nil)
2754 (setq gdb-overlay-arrow-position nil))
2755 (setq overlay-arrow-variable-list
2756 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2757 (if (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
2759 (setq gud-running nil)
2760 (setq gdb-active-process nil)
2761 (setq gdb-var-list nil)
2762 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2764 (defun gdb-source-info ()
2765 "Find the source file where the program starts and displays it with related
2767 (goto-char (point-min))
2768 (if (and (search-forward "Located in " nil t)
2769 (looking-at "\\S-+"))
2770 (setq gdb-main-file (match-string 0)))
2771 (goto-char (point-min))
2772 (if (search-forward "Includes preprocessor macro info." nil t)
2773 (setq gdb-macro-info t))
2774 (if gdb-many-windows
2776 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2778 (let ((pop-up-windows t))
2779 (display-buffer (gud-find-file gdb-main-file))))))
2781 (defun gdb-get-location (bptno line flag)
2782 "Find the directory containing the relevant source file.
2783 Put in buffer and place breakpoint icon."
2784 (goto-char (point-min))
2785 (catch 'file-not-found
2786 (if (search-forward "Located in " nil t)
2787 (when (looking-at "\\S-+")
2788 (delete (cons bptno "File not found") gdb-location-alist)
2789 (push (cons bptno (match-string 0)) gdb-location-alist))
2791 (unless (assoc bptno gdb-location-alist)
2792 (push (cons bptno "File not found") gdb-location-alist)
2793 (message-box "Cannot find source file for breakpoint location.\n\
2794 Add directory to search path for source files using the GDB command, dir."))
2795 (throw 'file-not-found nil))
2796 (with-current-buffer
2797 (find-file-noselect (match-string 0))
2798 (save-current-buffer
2799 (set (make-local-variable 'gud-minor-mode) 'gdba)
2800 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2801 ;; only want one breakpoint icon at each location
2803 (goto-line (string-to-number line))
2804 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2806 (add-hook 'find-file-hook 'gdb-find-file-hook)
2808 (defun gdb-find-file-hook ()
2809 "Set up buffer for debugging if file is part of the source code
2810 of the current session."
2811 (if (and (buffer-name gud-comint-buffer)
2812 ;; in case gud or gdb-ui is just loaded
2814 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2816 (if (member buffer-file-name gdb-source-file-list)
2817 (with-current-buffer (find-buffer-visiting buffer-file-name)
2818 (set (make-local-variable 'gud-minor-mode)
2819 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2820 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2823 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2824 "Put string PUTSTRING in front of POS in the current buffer.
2825 PUTSTRING is displayed by putting an overlay into the current buffer with a
2826 `before-string' string that has a `display' property whose value is
2828 (let ((string (make-string 1 ?x))
2829 (buffer (current-buffer)))
2830 (setq putstring (copy-sequence putstring))
2831 (let ((overlay (make-overlay pos pos buffer))
2833 (list (list 'margin 'left-margin) putstring))))
2834 (put-text-property 0 1 'display prop string)
2836 (add-text-properties 0 1 sprops string))
2837 (overlay-put overlay 'put-break t)
2838 (overlay-put overlay 'before-string string))))
2840 ;;from remove-images
2841 (defun gdb-remove-strings (start end &optional buffer)
2842 "Remove strings between START and END in BUFFER.
2843 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2844 BUFFER nil or omitted means use the current buffer."
2846 (setq buffer (current-buffer)))
2847 (dolist (overlay (overlays-in start end))
2848 (when (overlay-get overlay 'put-break)
2849 (delete-overlay overlay))))
2851 (defun gdb-put-breakpoint-icon (enabled bptno)
2852 (let ((start (- (line-beginning-position) 1))
2853 (end (+ (line-end-position) 1))
2854 (putstring (if enabled "B" "b"))
2855 (source-window (get-buffer-window (current-buffer) 0)))
2856 (add-text-properties
2857 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2860 (add-text-properties
2861 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2862 (add-text-properties
2863 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2864 (gdb-remove-breakpoint-icons start end)
2865 (if (display-images-p)
2866 (if (>= (or left-fringe-width
2867 (if source-window (car (window-fringes source-window)))
2868 gdb-buffer-fringe-width) 8)
2871 `(left-fringe breakpoint
2874 'breakpoint-disabled))
2876 'gdb-enabled enabled)
2877 (when (< left-margin-width 2)
2878 (save-current-buffer
2879 (setq left-margin-width 2)
2883 left-margin-width right-margin-width))))
2886 (or breakpoint-enabled-icon
2887 (setq breakpoint-enabled-icon
2888 (find-image `((:type xpm :data
2889 ,breakpoint-xpm-data
2890 :ascent 100 :pointer hand)
2892 ,breakpoint-enabled-pbm-data
2893 :ascent 100 :pointer hand)))))
2894 (or breakpoint-disabled-icon
2895 (setq breakpoint-disabled-icon
2896 (find-image `((:type xpm :data
2897 ,breakpoint-xpm-data
2898 :conversion disabled
2899 :ascent 100 :pointer hand)
2901 ,breakpoint-disabled-pbm-data
2902 :ascent 100 :pointer hand))))))
2906 (when (< left-margin-width 2)
2907 (save-current-buffer
2908 (setq left-margin-width 2)
2909 (let ((window (get-buffer-window (current-buffer) 0)))
2912 window left-margin-width right-margin-width)))))
2914 (propertize putstring
2915 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2918 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2919 (gdb-remove-strings start end)
2920 (if (display-images-p)
2921 (remove-images start end))
2923 (setq left-margin-width 0)
2924 (let ((window (get-buffer-window (current-buffer) 0)))
2927 window left-margin-width right-margin-width)))))
2931 ;; Assembler buffer.
2933 (gdb-set-buffer-rules 'gdb-assembler-buffer
2934 'gdb-assembler-buffer-name
2935 'gdb-assembler-mode)
2937 (def-gdb-auto-update-handler gdb-assembler-handler
2938 gdb-invalidate-assembler
2939 gdb-assembler-buffer
2940 gdb-assembler-custom)
2942 (defun gdb-assembler-custom ()
2943 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2944 (pos 1) (address) (flag) (bptno))
2945 (with-current-buffer buffer
2947 (if (not (equal gdb-frame-address "main"))
2949 (goto-char (point-min))
2950 (if (and gdb-frame-address
2951 (search-forward gdb-frame-address nil t))
2955 (or gdb-overlay-arrow-position
2956 (setq gdb-overlay-arrow-position (make-marker)))
2957 (set-marker gdb-overlay-arrow-position
2958 (point) (current-buffer))))))
2959 ;; remove all breakpoint-icons in assembler buffer before updating.
2960 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2961 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2962 (goto-char (point-min))
2963 (while (< (point) (- (point-max) 1))
2965 (if (looking-at "[^\t].*?breakpoint")
2968 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2969 (setq bptno (match-string 1))
2970 (setq flag (char-after (match-beginning 2)))
2971 (setq address (match-string 3))
2972 (with-current-buffer buffer
2974 (goto-char (point-min))
2975 (if (search-forward address nil t)
2976 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2977 (if (not (equal gdb-frame-address "main"))
2978 (with-current-buffer buffer
2979 (set-window-point (get-buffer-window buffer 0) pos)))))
2981 (defvar gdb-assembler-mode-map
2982 (let ((map (make-sparse-keymap)))
2983 (suppress-keymap map)
2984 (define-key map "q" 'kill-this-buffer)
2987 (defvar gdb-assembler-font-lock-keywords
2988 '(;; <__function.name+n>
2989 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2990 (1 font-lock-function-name-face))
2991 ;; 0xNNNNNNNN <__function.name+n>: opcode
2992 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2993 (4 font-lock-keyword-face))
2994 ;; %register(at least i386)
2995 ("%\\sw+" . font-lock-variable-name-face)
2996 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2997 (1 font-lock-comment-face)
2998 (2 font-lock-function-name-face))
2999 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3000 "Font lock keywords used in `gdb-assembler-mode'.")
3002 (defun gdb-assembler-mode ()
3003 "Major mode for viewing code assembler.
3005 \\{gdb-assembler-mode-map}"
3006 (kill-all-local-variables)
3007 (setq major-mode 'gdb-assembler-mode)
3008 (setq mode-name (concat "Machine:" gdb-selected-frame))
3009 (setq gdb-overlay-arrow-position nil)
3010 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3011 (setq fringes-outside-margins t)
3012 (setq buffer-read-only t)
3013 (use-local-map gdb-assembler-mode-map)
3014 (gdb-invalidate-assembler)
3015 (set (make-local-variable 'font-lock-defaults)
3016 '(gdb-assembler-font-lock-keywords))
3017 (run-mode-hooks 'gdb-assembler-mode-hook)
3018 'gdb-invalidate-assembler)
3020 (defun gdb-assembler-buffer-name ()
3021 (with-current-buffer gud-comint-buffer
3022 (concat "*disassembly of " (gdb-get-target-string) "*")))
3024 (defun gdb-display-assembler-buffer ()
3025 "Display disassembly view."
3027 (setq gdb-previous-frame nil)
3029 (gdb-get-buffer-create 'gdb-assembler-buffer)))
3031 (defun gdb-frame-assembler-buffer ()
3032 "Display disassembly view in a new frame."
3034 (setq gdb-previous-frame nil)
3035 (let ((special-display-regexps (append special-display-regexps '(".*")))
3036 (special-display-frame-alist gdb-frame-parameters))
3037 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3039 ;; modified because if gdb-frame-address has changed value a new command
3040 ;; must be enqueued to update the buffer with the new output
3041 (defun gdb-invalidate-assembler (&optional ignored)
3042 (if (gdb-get-buffer 'gdb-assembler-buffer)
3044 (unless (and gdb-selected-frame
3045 (string-equal gdb-selected-frame gdb-previous-frame))
3046 (if (or (not (member 'gdb-invalidate-assembler
3047 gdb-pending-triggers))
3048 (not (string-equal gdb-frame-address
3049 gdb-previous-frame-address)))
3051 ;; take previous disassemble command, if any, off the queue
3052 (with-current-buffer gud-comint-buffer
3053 (let ((queue gdb-input-queue))
3054 (dolist (item queue)
3055 (if (equal (cdr item) '(gdb-assembler-handler))
3056 (setq gdb-input-queue
3057 (delete item gdb-input-queue))))))
3060 (concat gdb-server-prefix "disassemble "
3061 (if (member gdb-frame-address '(nil "main")) nil "0x")
3062 gdb-frame-address "\n")
3063 'gdb-assembler-handler))
3064 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3065 (setq gdb-previous-frame-address gdb-frame-address)
3066 (setq gdb-previous-frame gdb-selected-frame)))))))
3068 (defun gdb-get-selected-frame ()
3069 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3072 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3073 (push 'gdb-get-selected-frame
3074 gdb-pending-triggers))))
3076 (defun gdb-frame-handler ()
3077 (setq gdb-pending-triggers
3078 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3079 (goto-char (point-min))
3080 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
3081 (setq gdb-frame-number (match-string 1)))
3082 (goto-char (point-min))
3083 (if (re-search-forward
3084 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
3086 (setq gdb-selected-frame (match-string 2))
3087 (if (gdb-get-buffer 'gdb-locals-buffer)
3088 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3089 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3090 (if (gdb-get-buffer 'gdb-assembler-buffer)
3091 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3092 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3093 (setq gdb-frame-address (match-string 1))))
3094 (goto-char (point-min))
3095 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
3096 (setq gdb-current-language (match-string 1)))
3097 (gdb-invalidate-assembler))
3100 ;; Code specific to GDB 6.4
3101 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3103 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3104 "Create list of source files for current GDB session.
3105 If buffers already exist for any of these files, gud-minor-mode
3107 (goto-char (point-min))
3108 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3109 (push (match-string 1) gdb-source-file-list))
3110 (dolist (buffer (buffer-list))
3111 (with-current-buffer buffer
3112 (when (member buffer-file-name gdb-source-file-list)
3113 (set (make-local-variable 'gud-minor-mode)
3114 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3115 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3116 (when gud-tooltip-mode
3117 (make-local-variable 'gdb-define-alist)
3118 (gdb-create-define-alist)
3119 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
3121 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3122 (defun gdb-var-list-children-1 (varnum)
3125 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3126 (concat "server interpreter mi \"-var-list-children --all-values "
3128 (concat "-var-list-children --all-values " varnum "\n"))
3129 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3131 (defconst gdb-var-list-children-regexp-1
3132 "name=\"\\(.+?\\)\",exp=\"\\(.+?\\)\",numchild=\"\\(.+?\\)\",\
3133 value=\\(\".*?\"\\),type=\"\\(.+?\\)\"}")
3135 (defun gdb-var-list-children-handler-1 (varnum)
3136 (goto-char (point-min))
3137 (let ((var-list nil))
3138 (catch 'child-already-watched
3139 (dolist (var gdb-var-list)
3140 (if (string-equal varnum (car var))
3143 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3144 (let ((varchild (list (match-string 1)
3148 (read (match-string 4))
3150 (if (assoc (car varchild) gdb-var-list)
3151 (throw 'child-already-watched nil))
3152 (push varchild var-list))))
3153 (push var var-list)))
3154 (setq gdb-var-list (nreverse var-list)))))
3156 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3157 (defun gdb-var-update-1 ()
3158 (if (not (member 'gdb-var-update gdb-pending-triggers))
3162 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3163 "server interpreter mi \"-var-update --all-values *\"\n"
3164 "-var-update --all-values *\n")
3165 'gdb-var-update-handler-1))
3166 (push 'gdb-var-update gdb-pending-triggers))))
3168 (defconst gdb-var-update-regexp-1
3169 "name=\"\\(.*?\\)\",\\(?:value=\\(\".*?\"\\),\\)?in_scope=\"\\(.*?\\)\"")
3171 (defun gdb-var-update-handler-1 ()
3172 (dolist (var gdb-var-list)
3173 (setcar (nthcdr 5 var) nil))
3174 (goto-char (point-min))
3175 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3176 (let* ((varnum (match-string 1))
3177 (var (assoc varnum gdb-var-list)))
3179 (if (string-equal (match-string 3) "false")
3180 (setcar (nthcdr 5 var) 'out-of-scope)
3181 (setcar (nthcdr 5 var) 'changed)
3182 (setcar (nthcdr 4 var)
3183 (read (match-string 2)))))))
3184 (setq gdb-pending-triggers
3185 (delq 'gdb-var-update gdb-pending-triggers))
3186 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3187 ;; dummy command to update speedbar at right time
3188 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
3189 ;; keep gdb-pending-triggers non-nil till end
3190 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
3192 ;; Registers buffer.
3194 (gdb-set-buffer-rules 'gdb-registers-buffer
3195 'gdb-registers-buffer-name
3196 'gdb-registers-mode)
3198 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3199 (gdb-get-buffer 'gdb-registers-buffer)
3200 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3201 "server interpreter mi \"-data-list-register-values x\"\n"
3202 "-data-list-register-values x\n")
3203 gdb-data-list-register-values-handler)
3205 (defconst gdb-data-list-register-values-regexp
3206 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
3208 (defun gdb-data-list-register-values-handler ()
3209 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3210 gdb-pending-triggers))
3211 (goto-char (point-min))
3212 (if (re-search-forward gdb-error-regexp nil t)
3213 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3214 (let ((buffer-read-only nil))
3216 (insert (match-string 1))
3217 (goto-char (point-min))))
3218 (let ((register-list (reverse gdb-register-names))
3219 (register nil) (register-string nil) (register-values nil))
3220 (goto-char (point-min))
3221 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3222 (setq register (pop register-list))
3223 (setq register-string (concat register "\t" (match-string 2) "\n"))
3224 (if (member (match-string 1) gdb-changed-registers)
3225 (put-text-property 0 (length register-string)
3226 'face 'font-lock-warning-face
3228 (setq register-values
3229 (concat register-values register-string)))
3230 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3231 (with-current-buffer buf
3232 (let* ((window (get-buffer-window buf 0))
3233 (start (window-start window))
3234 (p (window-point window))
3235 (buffer-read-only nil))
3237 (insert register-values)
3238 (set-window-start window start)
3239 (set-window-point window p))))))
3240 (gdb-data-list-register-values-custom))
3242 (defun gdb-data-list-register-values-custom ()
3243 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3245 (let ((buffer-read-only nil)
3247 (goto-char (point-min))
3248 (while (< (point) (point-max))
3249 (setq start (line-beginning-position))
3250 (setq end (line-end-position))
3251 (when (looking-at "^[^\t]+")
3252 (unless (string-equal (match-string 0) "No registers.")
3253 (put-text-property start (match-end 0)
3254 'face font-lock-variable-name-face)
3255 (add-text-properties start end
3256 '(help-echo "mouse-2: edit value"
3257 mouse-face highlight))))
3258 (forward-line 1))))))
3260 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3261 (defun gdb-get-changed-registers ()
3262 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3263 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3267 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3268 "server interpreter mi -data-list-changed-registers\n"
3269 "-data-list-changed-registers\n")
3270 'gdb-get-changed-registers-handler))
3271 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3273 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3275 (defun gdb-get-changed-registers-handler ()
3276 (setq gdb-pending-triggers
3277 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3278 (setq gdb-changed-registers nil)
3279 (goto-char (point-min))
3280 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3281 (push (match-string 1) gdb-changed-registers)))
3286 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3287 (gdb-set-buffer-rules 'gdb-locals-buffer
3288 'gdb-locals-buffer-name
3291 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3292 (gdb-get-buffer 'gdb-locals-buffer)
3293 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3294 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3295 "-stack-list-locals --simple-values\n")
3296 gdb-stack-list-locals-handler)
3298 (defconst gdb-stack-list-locals-regexp
3299 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3301 (defvar gdb-locals-watch-map-1
3302 (let ((map (make-sparse-keymap)))
3303 (define-key map [mouse-2] 'gud-watch)
3305 "Keymap to create watch expression of a complex data type local variable.")
3307 ;; Dont display values of arrays or structures.
3308 ;; These can be expanded using gud-watch.
3309 (defun gdb-stack-list-locals-handler ()
3310 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3311 gdb-pending-triggers))
3312 (let (local locals-list)
3313 (goto-char (point-min))
3314 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3315 (let ((local (list (match-string 1)
3318 (if (looking-at ",value=\\(\".*\"\\)}")
3319 (setcar (nthcdr 2 local) (read (match-string 1))))
3320 (push local locals-list)))
3321 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3322 (and buf (with-current-buffer buf
3323 (let* ((window (get-buffer-window buf 0))
3324 (start (window-start window))
3325 (p (window-point window))
3326 (buffer-read-only nil))
3328 (dolist (local locals-list)
3329 (setq name (car local))
3330 (if (or (not (nth 2 local))
3331 (string-match "\\0x" (nth 2 local)))
3332 (add-text-properties 0 (length name)
3333 `(mouse-face highlight
3334 help-echo "mouse-2: create watch expression"
3335 local-map ,gdb-locals-watch-map-1)
3338 (concat name "\t" (nth 1 local)
3339 "\t" (nth 2 local) "\n")))
3340 (set-window-start window start)
3341 (set-window-point window p)))))))
3343 (defun gdb-get-register-names ()
3344 "Create a list of register names."
3345 (goto-char (point-min))
3346 (setq gdb-register-names nil)
3347 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3348 (push (match-string 1) gdb-register-names)))
3352 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3353 ;;; gdb-ui.el ends here