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)
128 (defvar gdb-inferior-status nil)
130 (defvar gdb-buffer-type nil
131 "One of the symbols bound in `gdb-buffer-rules'.")
132 (make-variable-buffer-local 'gdb-buffer-type)
134 (defvar gdb-input-queue ()
135 "A list of gdb command objects.")
137 (defvar gdb-prompting nil
138 "True when gdb is idle with no pending input.")
140 (defvar gdb-output-sink 'user
141 "The disposition of the output of the current gdb command.
142 Possible values are these symbols:
144 `user' -- gdb output should be copied to the GUD buffer
147 `inferior' -- gdb output should be copied to the inferior-io buffer.
149 `pre-emacs' -- output should be ignored util the post-prompt
150 annotation is received. Then the output-sink
152 `emacs' -- output should be collected in the partial-output-buffer
153 for subsequent processing by a command. This is the
154 disposition of output generated by commands that
155 gdb mode sends to gdb on its own behalf.
156 `post-emacs' -- ignore output until the prompt annotation is
157 received, then go to USER disposition.
159 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
160 \(`user' and `emacs').")
162 (defvar gdb-current-item nil
163 "The most recent command item sent to gdb.")
165 (defvar gdb-pending-triggers '()
166 "A list of trigger functions that have run later than their output
169 (defvar gdb-first-post-prompt nil)
170 (defvar gdb-version nil)
171 (defvar gdb-locals-font-lock-keywords nil)
172 (defvar gdb-source-file-list nil
173 "List of source files for the current executable")
174 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
176 (defvar gdb-locals-font-lock-keywords-1
178 ;; var = (struct struct_tag) value
179 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
180 (1 font-lock-variable-name-face)
181 (3 font-lock-keyword-face)
182 (4 font-lock-type-face))
183 ;; var = (type) value
184 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
185 (1 font-lock-variable-name-face)
186 (3 font-lock-type-face))
188 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
189 (1 font-lock-variable-name-face))
191 "Font lock keywords used in `gdb-local-mode'.")
193 (defvar gdb-locals-font-lock-keywords-2
196 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
197 (1 font-lock-variable-name-face)
198 (3 font-lock-type-face))
200 "Font lock keywords used in `gdb-local-mode'.")
202 ;; Variables for GDB 6.4+
203 (defvar gdb-register-names nil "List of register names.")
204 (defvar gdb-changed-registers nil
205 "List of changed register numbers (strings).")
208 (defun gdba (command-line)
209 "Run gdb on program FILE in buffer *gud-FILE*.
210 The directory containing FILE becomes the initial working directory
211 and source-file directory for your debugger.
213 If `gdb-many-windows' is nil (the default value) then gdb just
214 pops up the GUD buffer unless `gdb-show-main' is t. In this case
215 it starts with two windows: one displaying the GUD buffer and the
216 other with the source file with the main routine of the inferior.
218 If `gdb-many-windows' is t, regardless of the value of
219 `gdb-show-main', the layout below will appear unless
220 `gdb-use-separate-io-buffer' is nil when the source buffer
221 occupies the full width of the frame. Keybindings are shown in
224 Watch expressions appear in the speedbar/slowbar.
226 The following commands help control operation :
228 `gdb-many-windows' - Toggle the number of windows gdb uses.
229 `gdb-restore-windows' - To restore the window layout.
231 See Info node `(emacs)GDB Graphical Interface' for a more
232 detailed description of this mode.
235 +----------------------------------------------------------------------+
237 +-----------------------------------+----------------------------------+
238 | GUD buffer (I/O of GDB) | Locals buffer |
242 +-----------------------------------+----------------------------------+
243 | Source buffer | I/O buffer (of debugged program) |
251 +-----------------------------------+----------------------------------+
252 | Stack buffer | Breakpoints buffer |
253 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
254 | | RET gdb-goto-breakpoint |
255 | | D gdb-delete-breakpoint |
256 +-----------------------------------+----------------------------------+"
258 (interactive (list (gud-query-cmdline 'gdba)))
260 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
263 (defcustom gdb-debug-ring-max 128
264 "Maximum size of `gdb-debug-ring'."
269 (defvar gdb-debug-ring nil
270 "List of commands, most recent first, sent to and replies received from GDB.
271 This variable is used to debug GDB-UI.")
274 (defcustom gdb-enable-debug nil
275 "Non-nil means record the process input and output in `gdb-debug-ring'."
280 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
281 "Shell command for generating a list of defined macros in a source file.
282 This list is used to display the #define directive associated
283 with an identifier as a tooltip. It works in a debug session with
284 GDB, when gud-tooltip-mode is t.
286 Set `gdb-cpp-define-alist-flags' for any include paths or
292 (defcustom gdb-cpp-define-alist-flags ""
293 "Preprocessor flags for `gdb-cpp-define-alist-program'."
298 (defcustom gdb-show-main nil
299 "Non-nil means display source file containing the main routine at startup.
300 Also display the main routine in the disassembly buffer if present."
305 (defcustom gdb-many-windows nil
306 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
307 In this case it starts with two windows: one displaying the GUD
308 buffer and the other with the source file with the main routine
309 of the inferior. Non-nil means display the layout shown for
315 (defcustom gdb-use-separate-io-buffer nil
316 "Non-nil means display output from the inferior in a separate buffer."
321 (defun gdb-force-mode-line-update (status)
322 (let ((buffer gud-comint-buffer))
323 (if (and buffer (buffer-name buffer))
324 (with-current-buffer buffer
325 (setq mode-line-process
327 (process-status (get-buffer-process buffer)) status))
328 ;; Force mode line redisplay soon.
329 (force-mode-line-update)))))
331 (defun gdb-many-windows (arg)
332 "Toggle the number of windows in the basic arrangement.
333 With arg, display additional buffers iff arg is positive."
335 (setq gdb-many-windows
337 (not gdb-many-windows)
338 (> (prefix-numeric-value arg) 0)))
339 (message (format "Display of other windows %sabled"
340 (if gdb-many-windows "en" "dis")))
341 (if (and gud-comint-buffer
342 (buffer-name gud-comint-buffer))
344 (gdb-restore-windows)
347 (defun gdb-use-separate-io-buffer (arg)
348 "Toggle separate IO for inferior.
349 With arg, use separate IO iff arg is positive."
351 (setq gdb-use-separate-io-buffer
353 (not gdb-use-separate-io-buffer)
354 (> (prefix-numeric-value arg) 0)))
355 (message (format "Separate inferior IO %sabled"
356 (if gdb-use-separate-io-buffer "en" "dis")))
357 (if (and gud-comint-buffer
358 (buffer-name gud-comint-buffer))
360 (if gdb-use-separate-io-buffer
361 (if gdb-many-windows (gdb-restore-windows))
362 (kill-buffer (gdb-inferior-io-name)))
365 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
367 (defun gdb-create-define-alist ()
368 "Create an alist of #define directives for GUD tooltips."
369 (let* ((file (buffer-file-name))
371 (with-output-to-string
372 (with-current-buffer standard-output
373 (call-process shell-file-name
374 (if (file-exists-p file) file nil)
375 (list t nil) nil "-c"
376 (concat gdb-cpp-define-alist-program " "
377 gdb-cpp-define-alist-flags)))))
378 (define-list (split-string output "\n" t))
380 (setq gdb-define-alist nil)
381 (dolist (define define-list)
382 (setq name (nth 1 (split-string define "[( ]")))
383 (push (cons name define) gdb-define-alist))))
385 (defun gdb-tooltip-print (expr)
387 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
388 (goto-char (point-min))
390 (if (search-forward "=" nil t)
391 (concat expr (buffer-substring (- (point) 2) (point-max)))
393 ;; remove newline for gud-tooltip-echo-area
394 (substring string 0 (- (length string) 1))))
395 (or gud-tooltip-echo-area tooltip-use-echo-area)))
397 ;; If expr is a macro for a function don't print because of possible dangerous
398 ;; side-effects. Also printing a function within a tooltip generates an
399 ;; unexpected starting annotation (phase error).
400 (defun gdb-tooltip-print-1 (expr)
401 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
402 (goto-char (point-min))
403 (if (search-forward "expands to: " nil t)
404 (unless (looking-at "\\S-+.*(.*).*")
406 (list (concat gdb-server-prefix "print " expr "\n")
407 `(lambda () (gdb-tooltip-print ,expr))))))))
409 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
411 (defun gdb-set-gud-minor-mode-existing-buffers ()
412 "Create list of source files for current GDB session."
413 (goto-char (point-min))
414 (when (search-forward "read in on demand:" nil t)
415 (while (re-search-forward gdb-source-file-regexp nil t)
416 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
417 (dolist (buffer (buffer-list))
418 (with-current-buffer buffer
419 (when (and buffer-file-name
420 (member (file-name-nondirectory buffer-file-name)
421 gdb-source-file-list))
422 (set (make-local-variable 'gud-minor-mode) 'gdba)
423 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
424 (when gud-tooltip-mode
425 (make-local-variable 'gdb-define-alist)
426 (gdb-create-define-alist)
427 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
429 (defun gdb-find-watch-expression ()
430 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
431 (varnum (car var)) expr array)
432 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
433 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
434 (component-list (split-string (match-string 2 varnum) "\\." t)))
435 (setq expr (nth 1 var1))
436 (setq varnumlet (car var1))
437 (dolist (component component-list)
438 (setq var2 (assoc varnumlet gdb-var-list))
439 (setq expr (concat expr
440 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
441 (concat "[" component "]")
442 (concat "." component))))
443 (setq varnumlet (concat varnumlet "." component)))
447 (set (make-local-variable 'gud-minor-mode) 'gdba)
448 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
450 (gud-def gud-break (if (not (string-match "Machine" mode-name))
451 (gud-call "break %f:%l" arg)
455 (gud-call "break *%a" arg)))
456 "\C-b" "Set breakpoint at current line or address.")
458 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
459 (gud-call "clear %f:%l" arg)
463 (gud-call "clear *%a" arg)))
464 "\C-d" "Remove breakpoint at current line or address.")
466 (gud-def gud-until (if (not (string-match "Machine" mode-name))
467 (gud-call "until %f:%l" arg)
471 (gud-call "until *%a" arg)))
472 "\C-u" "Continue to current line or address.")
474 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
475 nil "Start or continue execution.")
477 ;; For debugging Emacs only.
481 "pp1 " (if (eq (buffer-local-value
482 'major-mode (window-buffer)) 'speedbar-mode)
483 (gdb-find-watch-expression) "%e")) arg)
484 nil "Print the emacs s-expression.")
486 (define-key gud-minor-mode-map [left-margin mouse-1]
487 'gdb-mouse-set-clear-breakpoint)
488 (define-key gud-minor-mode-map [left-fringe mouse-1]
489 'gdb-mouse-set-clear-breakpoint)
490 (define-key gud-minor-mode-map [left-fringe mouse-2]
492 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
494 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
496 (define-key gud-minor-mode-map [left-margin mouse-2]
498 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
500 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
502 (define-key gud-minor-mode-map [left-fringe C-mouse-2]
504 (define-key gud-minor-mode-map [left-margin C-mouse-2]
506 (define-key gud-minor-mode-map [left-margin mouse-3]
507 'gdb-mouse-toggle-breakpoint-margin)
508 (define-key gud-minor-mode-map [left-fringe mouse-3]
509 'gdb-mouse-toggle-breakpoint-fringe)
511 (setq comint-input-sender 'gdb-send)
514 (setq gdb-frame-address (if gdb-show-main "main" nil))
515 (setq gdb-previous-frame-address nil
516 gdb-memory-address "main"
517 gdb-previous-frame nil
518 gdb-selected-frame nil
519 gdb-current-language nil
523 gdb-first-post-prompt t
527 gdb-pending-triggers nil
528 gdb-output-sink 'user
529 gdb-server-prefix "server "
530 gdb-flush-pending-output nil
531 gdb-location-alist nil
532 gdb-source-file-list nil
535 gdb-buffer-fringe-width (car (window-fringes))
538 gdb-source-window nil
539 gdb-inferior-status nil
540 gdb-continuation nil)
542 (setq gdb-buffer-type 'gdba)
544 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
546 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
547 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
551 (if (eq window-system 'w32)
552 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
553 (gdb-enqueue-input (list "set height 0\n" 'ignore))
554 (gdb-enqueue-input (list "set width 0\n" 'ignore))
556 (if (string-equal gdb-version "pre-6.4")
558 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
559 'gdb-set-gud-minor-mode-existing-buffers))
560 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
562 (list "server interpreter mi -data-list-register-names\n"
563 'gdb-get-register-names))
564 ; Needs GDB 6.2 onwards.
566 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
567 'gdb-set-gud-minor-mode-existing-buffers-1))
568 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
570 ;; find source file and compilation directory here
571 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
572 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
573 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
575 (run-hooks 'gdba-mode-hook))
577 (defun gdb-get-version ()
578 (goto-char (point-min))
579 (if (and (re-search-forward gdb-error-regexp nil t)
580 (string-match ".*(missing implementation)" (match-string 1)))
581 (setq gdb-version "pre-6.4")
582 (setq gdb-version "6.4+"))
585 (defun gdb-mouse-until (event)
586 "Continue running until a source line past the current line.
587 The destination source line can be selected either by clicking with mouse-2
588 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
590 (if gud-overlay-arrow-position
591 (let ((start (event-start event))
592 (end (event-end event))
593 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
594 (if (not (string-match "Machine" mode-name))
595 (if (equal buffer (window-buffer (posn-window end)))
596 (with-current-buffer buffer
597 (when (or (equal start end)
598 (equal (posn-point start)
600 gud-overlay-arrow-position)))
601 (setq line (line-number-at-pos (posn-point end)))
602 (gud-call (concat "until " (number-to-string line))))))
603 (if (equal (marker-buffer gdb-overlay-arrow-position)
604 (window-buffer (posn-window end)))
605 (when (or (equal start end)
606 (equal (posn-point start)
608 gdb-overlay-arrow-position)))
610 (goto-line (line-number-at-pos (posn-point end)))
612 (gud-call (concat "until *%a")))))))))
614 (defun gdb-mouse-jump (event)
615 "Set execution address/line.
616 The destination source line can be selected either by clicking with mouse-2
617 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
618 Unlike gdb-mouse-until the destination address can be before the current
619 line, and no execution takes place."
621 (if gud-overlay-arrow-position
622 (let ((start (event-start event))
623 (end (event-end event))
624 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
625 (if (not (string-match "Machine" mode-name))
626 (if (equal buffer (window-buffer (posn-window end)))
627 (with-current-buffer buffer
628 (when (or (equal start end)
629 (equal (posn-point start)
631 gud-overlay-arrow-position)))
632 (setq line (line-number-at-pos (posn-point end)))
633 (progn (gud-call (concat "tbreak " (number-to-string line)))
634 (gud-call (concat "jump " (number-to-string line)))))))
635 (if (equal (marker-buffer gdb-overlay-arrow-position)
636 (window-buffer (posn-window end)))
637 (when (or (equal start end)
638 (equal (posn-point start)
640 gdb-overlay-arrow-position)))
642 (goto-line (line-number-at-pos (posn-point end)))
645 (gud-call (concat "tbreak *%a"))
646 (gud-call (concat "jump *%a"))))))))))
648 (defcustom gdb-speedbar-auto-raise nil
649 "If non-nil raise speedbar every time display of watch expressions is\
655 (defun gdb-speedbar-auto-raise (arg)
656 "Toggle automatic raising of the speedbar for watch expressions.
657 With arg, automatically raise speedbar iff arg is positive."
659 (setq gdb-speedbar-auto-raise
661 (not gdb-speedbar-auto-raise)
662 (> (prefix-numeric-value arg) 0)))
663 (message (format "Auto raising %sabled"
664 (if gdb-speedbar-auto-raise "en" "dis"))))
666 (defcustom gdb-use-colon-colon-notation nil
667 "If non-nil use FUN::VAR format to display variables in the speedbar."
672 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
673 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
675 (defun gud-watch (&optional arg event)
676 "Watch expression at point.
677 With arg, enter name of variable to be watched in the minibuffer."
678 (interactive (list current-prefix-arg last-input-event))
679 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
680 (if (memq minor-mode '(gdbmi gdba))
682 (if event (posn-set-point (event-end event)))
684 (save-selected-window
686 (completing-read "Name of variable: "
687 'gud-gdb-complete-command)
688 (tooltip-identifier-from-point (point)))))
689 (catch 'already-watched
690 (dolist (var gdb-var-list)
691 (unless (string-match "\\." (car var))
692 (if (string-equal expr (nth 1 var))
693 (throw 'already-watched nil))))
694 (set-text-properties 0 (length expr) nil expr)
697 (if (eq minor-mode 'gdba)
699 "server interpreter mi \"-var-create - * " expr "\"\n")
700 (concat"-var-create - * " expr "\n"))
701 `(lambda () (gdb-var-create-handler ,expr))))))))
702 (message "gud-watch is a no-op in this mode."))))
704 (defconst gdb-var-create-regexp
705 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
707 (defun gdb-var-create-handler (expr)
708 (goto-char (point-min))
709 (if (re-search-forward gdb-var-create-regexp nil t)
712 (if (and (string-equal gdb-current-language "c")
713 gdb-use-colon-colon-notation gdb-selected-frame)
714 (setq expr (concat gdb-selected-frame "::" expr))
719 (push var gdb-var-list)
721 (unless (string-equal
722 speedbar-initial-expansion-list-name "GUD")
723 (speedbar-change-initial-expansion-list "GUD"))
726 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
727 (concat "server interpreter mi \"-var-evaluate-expression "
729 (concat "-var-evaluate-expression " (car var) "\n"))
730 `(lambda () (gdb-var-evaluate-expression-handler
732 (if (search-forward "Undefined command" nil t)
733 (message-box "Watching expressions requires gdb 6.0 onwards")
734 (message-box "No symbol \"%s\" in current context." expr))))
736 (defun gdb-var-evaluate-expression-handler (varnum changed)
737 (goto-char (point-min))
738 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
739 (let ((var (assoc varnum gdb-var-list)))
741 (if changed (setcar (nthcdr 5 var) 'changed))
742 (setcar (nthcdr 4 var) (read (match-string 1))))))
744 (defun gdb-var-list-children (varnum)
746 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
747 `(lambda () (gdb-var-list-children-handler ,varnum)))))
749 (defconst gdb-var-list-children-regexp
750 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
753 (defun gdb-var-list-children-handler (varnum)
754 (goto-char (point-min))
755 (let ((var-list nil))
756 (catch 'child-already-watched
757 (dolist (var gdb-var-list)
758 (if (string-equal varnum (car var))
761 (while (re-search-forward gdb-var-list-children-regexp nil t)
762 (let ((varchild (list (match-string 1)
767 (if (assoc (car varchild) gdb-var-list)
768 (throw 'child-already-watched nil))
769 (push varchild var-list)
773 "server interpreter mi \"-var-evaluate-expression "
774 (car varchild) "\"\n")
775 `(lambda () (gdb-var-evaluate-expression-handler
776 ,(car varchild) nil)))))))
777 (push var var-list)))
778 (setq gdb-var-list (nreverse var-list)))))
780 (defun gdb-var-update ()
781 (when (not (member 'gdb-var-update gdb-pending-triggers))
783 (list "server interpreter mi \"-var-update *\"\n"
784 'gdb-var-update-handler))
785 (push 'gdb-var-update gdb-pending-triggers)))
787 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\",in_scope=\"\\(.*?\\)\"")
789 (defun gdb-var-update-handler ()
790 (dolist (var gdb-var-list)
791 (setcar (nthcdr 5 var) nil))
792 (goto-char (point-min))
793 (while (re-search-forward gdb-var-update-regexp nil t)
794 (let ((varnum (match-string 1)))
795 (if (string-equal (match-string 2) "false")
796 (let ((var (assoc varnum gdb-var-list)))
797 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
800 (concat "server interpreter mi \"-var-evaluate-expression "
802 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
803 (setq gdb-pending-triggers
804 (delq 'gdb-var-update gdb-pending-triggers))
805 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
806 ;; Dummy command to update speedbar at right time.
807 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
808 ;; Keep gdb-pending-triggers non-nil till end.
809 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
811 (defun gdb-speedbar-refresh ()
812 (setq gdb-pending-triggers
813 (delq 'gdb-speedbar-refresh gdb-pending-triggers))
814 (with-current-buffer gud-comint-buffer
815 (let ((speedbar-verbosity-level 0)
816 (speedbar-shown-directories nil))
818 (speedbar-refresh)))))
820 (defun gdb-var-delete ()
821 "Delete watch expression at point from the speedbar."
823 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
825 (let ((text (speedbar-line-text)))
826 (string-match "\\(\\S-+\\)" text)
827 (let ((expr (match-string 1 text)) var varnum)
829 (dolist (var1 gdb-var-list)
830 (when (string-equal expr (nth 1 var1))
832 (setq varnum (car var1))
833 (throw 'expr-found nil))))
834 (unless (string-match "\\." (car var))
837 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
839 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
840 (concat "-var-delete " varnum "\n"))
842 (setq gdb-var-list (delq var gdb-var-list))
843 (dolist (varchild gdb-var-list)
844 (if (string-match (concat (car var) "\\.") (car varchild))
845 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
847 (defun gdb-edit-value (text token indent)
848 "Assign a value to a variable displayed in the speedbar."
849 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
850 (varnum (car var)) (value))
851 (setq value (read-string "New value: "))
854 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
855 (concat "server interpreter mi \"-var-assign "
856 varnum " " value "\"\n")
857 (concat "-var-assign " varnum " " value "\n"))
858 `(lambda () (gdb-edit-value-handler ,value))))))
860 (defun gdb-edit-value-handler (value)
861 (goto-char (point-min))
862 (if (re-search-forward gdb-error-regexp nil t)
863 (message-box "Invalid number or expression (%s)" value)))
865 (defcustom gdb-show-changed-values t
866 "If non-nil change the face of out of scope variables and changed values.
867 Out of scope variables are suppressed with `shadow' face.
868 Changed values are highlighted with the face `font-lock-warning-face'."
873 (defun gdb-speedbar-expand-node (text token indent)
874 "Expand the node the user clicked on.
875 TEXT is the text of the button we clicked on, a + or - item.
876 TOKEN is data related to this node.
877 INDENT is the current indentation depth."
878 (cond ((string-match "+" text) ;expand this node
880 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
881 (string-equal gdb-version "pre-6.4"))
882 (gdb-var-list-children token)
883 (gdb-var-list-children-1 token)))
884 ((string-match "-" text) ;contract this node
885 (dolist (var gdb-var-list)
886 (if (string-match (concat token "\\.") (car var))
887 (setq gdb-var-list (delq var gdb-var-list))))
888 (speedbar-change-expand-button-char ?+)
889 (speedbar-delete-subblock indent))
890 (t (error "Ooops... not sure what to do")))
891 (speedbar-center-buffer-smartly))
893 (defun gdb-get-target-string ()
894 (with-current-buffer gud-comint-buffer
901 ;; Each buffer has a TYPE -- a symbol that identifies the function
902 ;; of that particular buffer.
904 ;; The usual gdb interaction buffer is given the type `gdba' and
905 ;; is constructed specially.
907 ;; Others are constructed by gdb-get-buffer-create and
908 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
910 (defvar gdb-buffer-rules-assoc '())
912 (defun gdb-get-buffer (key)
913 "Return the gdb buffer tagged with type KEY.
914 The key should be one of the cars in `gdb-buffer-rules-assoc'."
916 (gdb-look-for-tagged-buffer key (buffer-list))))
918 (defun gdb-get-buffer-create (key)
919 "Create a new gdb buffer of the type specified by KEY.
920 The key should be one of the cars in `gdb-buffer-rules-assoc'."
921 (or (gdb-get-buffer key)
922 (let* ((rules (assoc key gdb-buffer-rules-assoc))
923 (name (funcall (gdb-rules-name-maker rules)))
924 (new (get-buffer-create name)))
925 (with-current-buffer new
927 (if (cdr (cdr rules))
928 (setq trigger (funcall (car (cdr (cdr rules))))))
929 (setq gdb-buffer-type key)
930 (set (make-local-variable 'gud-minor-mode)
931 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
932 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
933 (if trigger (funcall trigger)))
936 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
938 (defun gdb-look-for-tagged-buffer (key bufs)
940 (while (and (not retval) bufs)
941 (set-buffer (car bufs))
942 (if (eq gdb-buffer-type key)
943 (setq retval (car bufs)))
944 (setq bufs (cdr bufs)))
948 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
949 ;; at least one and possible more functions. The functions have these
950 ;; roles in defining a buffer type:
952 ;; NAME - Return a name for this buffer type.
954 ;; The remaining function(s) are optional:
956 ;; MODE - called in a new buffer with no arguments, should establish
957 ;; the proper mode for the buffer.
960 (defun gdb-set-buffer-rules (buffer-type &rest rules)
961 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
963 (setcdr binding rules)
964 (push (cons buffer-type rules)
965 gdb-buffer-rules-assoc))))
967 ;; GUD buffers are an exception to the rules
968 (gdb-set-buffer-rules 'gdba 'error)
970 ;; Partial-output buffer : This accumulates output from a command executed on
971 ;; behalf of emacs (rather than the user).
973 (gdb-set-buffer-rules 'gdb-partial-output-buffer
974 'gdb-partial-output-name)
976 (defun gdb-partial-output-name ()
977 (concat "*partial-output-"
978 (gdb-get-target-string)
982 (gdb-set-buffer-rules 'gdb-inferior-io
983 'gdb-inferior-io-name
984 'gdb-inferior-io-mode)
986 (defun gdb-inferior-io-name ()
987 (concat "*input/output of "
988 (gdb-get-target-string)
991 (defun gdb-display-separate-io-buffer ()
992 "Display IO of debugged program in a separate window."
994 (if gdb-use-separate-io-buffer
996 (gdb-get-buffer-create 'gdb-inferior-io))))
998 (defconst gdb-frame-parameters
999 '((height . 14) (width . 80)
1001 (tool-bar-lines . nil)
1002 (menu-bar-lines . nil)
1003 (minibuffer . nil)))
1005 (defun gdb-frame-separate-io-buffer ()
1006 "Display IO of inferior in a new frame."
1008 (if gdb-use-separate-io-buffer
1009 (let ((special-display-regexps (append special-display-regexps '(".*")))
1010 (special-display-frame-alist gdb-frame-parameters))
1011 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1013 (defvar gdb-inferior-io-mode-map
1014 (let ((map (make-sparse-keymap)))
1015 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1016 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1017 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1018 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1019 (define-key map "\C-d" 'gdb-separate-io-eof)
1022 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1023 "Major mode for gdb inferior-io."
1024 :syntax-table nil :abbrev-table nil
1025 ;; We want to use comint because it has various nifty and familiar
1026 ;; features. We don't need a process, but comint wants one, so create
1028 (make-comint-in-buffer
1029 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1030 (current-buffer) "hexl")
1031 (setq comint-input-sender 'gdb-inferior-io-sender))
1033 (defun gdb-inferior-io-sender (proc string)
1034 ;; PROC is the pseudo-process created to satisfy comint.
1035 (with-current-buffer (process-buffer proc)
1036 (setq proc (get-buffer-process gud-comint-buffer))
1037 (process-send-string proc string)
1038 (process-send-string proc "\n")))
1040 (defun gdb-separate-io-interrupt ()
1041 "Interrupt the program being debugged."
1044 (get-buffer-process gud-comint-buffer) comint-ptyp))
1046 (defun gdb-separate-io-quit ()
1047 "Send quit signal to the program being debugged."
1050 (get-buffer-process gud-comint-buffer) comint-ptyp))
1052 (defun gdb-separate-io-stop ()
1053 "Stop the program being debugged."
1056 (get-buffer-process gud-comint-buffer) comint-ptyp))
1058 (defun gdb-separate-io-eof ()
1059 "Send end-of-file to the program being debugged."
1062 (get-buffer-process gud-comint-buffer)))
1065 ;; gdb communications
1068 ;; INPUT: things sent to gdb
1070 ;; The queues are lists. Each element is either a string (indicating user or
1071 ;; user-like input) or a list of the form:
1073 ;; (INPUT-STRING HANDLER-FN)
1075 ;; The handler function will be called from the partial-output buffer when the
1076 ;; command completes. This is the way to write commands which invoke gdb
1077 ;; commands autonomously.
1079 ;; These lists are consumed tail first.
1081 (defvar gdb-continuation nil)
1083 (defun gdb-send (proc string)
1084 "A comint send filter for gdb.
1085 This filter may simply queue input for a later time."
1086 (with-current-buffer gud-comint-buffer
1087 (let ((inhibit-read-only t))
1088 (remove-text-properties (point-min) (point-max) '(face))))
1089 (if (string-match "\\\\$" string)
1090 (setq gdb-continuation (concat gdb-continuation string "\n"))
1091 (let ((item (concat gdb-continuation string "\n")))
1094 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1095 (process-send-string proc item))
1096 (gdb-enqueue-input item)))
1097 (setq gdb-continuation nil)))
1099 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1100 ;; is a query, or other non-top-level prompt.
1102 (defun gdb-enqueue-input (item)
1103 (if (not gud-running)
1106 (gdb-send-item item)
1107 (setq gdb-prompting nil))
1108 (push item gdb-input-queue))))
1110 (defun gdb-dequeue-input ()
1111 (let ((queue gdb-input-queue))
1113 (let ((last (car (last queue))))
1114 (unless (nbutlast queue) (setq gdb-input-queue '()))
1117 (defun gdb-send-item (item)
1118 (setq gdb-flush-pending-output nil)
1119 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1120 (setq gdb-current-item item)
1121 (let ((process (get-buffer-process gud-comint-buffer)))
1122 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1125 (setq gdb-output-sink 'user)
1126 (process-send-string process item))
1128 (gdb-clear-partial-output)
1129 (setq gdb-output-sink 'pre-emacs)
1130 (process-send-string process
1132 ;; case: eq gud-minor-mode 'gdbmi
1133 (gdb-clear-partial-output)
1134 (setq gdb-output-sink 'emacs)
1135 (process-send-string process (car item)))))
1138 ;; output -- things gdb prints to emacs
1140 ;; GDB output is a stream interrupted by annotations.
1141 ;; Annotations can be recognized by their beginning
1142 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1144 ;; The tag is a string obeying symbol syntax.
1146 ;; The optional part `<opt>' can be either the empty string
1147 ;; or a space followed by more data relating to the annotation.
1148 ;; For example, the SOURCE annotation is followed by a filename,
1149 ;; line number and various useless goo. This data must not include
1153 (defcustom gud-gdba-command-name "gdb -annotate=3"
1154 "Default command to execute an executable under the GDB-UI debugger."
1159 (defvar gdb-annotation-rules
1160 '(("pre-prompt" gdb-pre-prompt)
1161 ("prompt" gdb-prompt)
1162 ("commands" gdb-subprompt)
1163 ("overload-choice" gdb-subprompt)
1164 ("query" gdb-subprompt)
1165 ;; Need this prompt for GDB 6.1
1166 ("nquery" gdb-subprompt)
1167 ("prompt-for-continue" gdb-subprompt)
1168 ("post-prompt" gdb-post-prompt)
1169 ("source" gdb-source)
1170 ("starting" gdb-starting)
1171 ("exited" gdb-exited)
1172 ("signalled" gdb-signalled)
1173 ("signal" gdb-signal)
1174 ("breakpoint" gdb-stopping)
1175 ("watchpoint" gdb-stopping)
1176 ("frame-begin" gdb-frame-begin)
1177 ("stopped" gdb-stopped)
1178 ("error-begin" gdb-error)
1180 ) "An assoc mapping annotation tags to functions which process them.")
1183 (setq gdb-flush-pending-output t)
1184 (setq gud-running nil)
1185 (gdb-force-mode-line-update "stopped")
1186 (setq gdb-output-sink 'user)
1187 (setq gdb-input-queue nil)
1188 (setq gdb-pending-triggers nil)
1189 (setq gdb-prompting t))
1191 (defconst gdb-source-spec-regexp
1192 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1194 ;; Do not use this except as an annotation handler.
1195 (defun gdb-source (args)
1196 (string-match gdb-source-spec-regexp args)
1197 ;; Extract the frame position from the marker.
1198 (setq gud-last-frame
1200 (match-string 1 args)
1201 (string-to-number (match-string 2 args))))
1202 (setq gdb-frame-address (match-string 3 args))
1203 ;; cover for auto-display output which comes *before*
1204 ;; stopped annotation
1205 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1207 (defun gdb-pre-prompt (ignored)
1208 "An annotation handler for `pre-prompt'.
1209 This terminates the collection of output from a previous command if that
1210 happens to be in effect."
1211 (setq gdb-error nil)
1212 (let ((sink gdb-output-sink))
1216 (setq gdb-output-sink 'post-emacs))
1219 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1221 (defun gdb-prompt (ignored)
1222 "An annotation handler for `prompt'.
1223 This sends the next command (if any) to gdb."
1224 (when gdb-first-prompt
1226 (setq gdb-first-prompt nil))
1227 (let ((sink gdb-output-sink))
1230 ((eq sink 'post-emacs)
1231 (setq gdb-output-sink 'user)
1233 (car (cdr gdb-current-item))))
1234 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1235 (funcall handler))))
1238 (error "Phase error in gdb-prompt (got %s)" sink))))
1239 (let ((input (gdb-dequeue-input)))
1241 (gdb-send-item input)
1243 (setq gdb-prompting t)
1244 (gud-display-frame)))))
1246 (defun gdb-subprompt (ignored)
1247 "An annotation handler for non-top-level prompts."
1248 (setq gdb-prompting t))
1250 (defun gdb-starting (ignored)
1251 "An annotation handler for `starting'.
1252 This says that I/O for the subprocess is now the program being debugged,
1254 (setq gdb-active-process t)
1255 (let ((sink gdb-output-sink))
1259 (setq gud-running t)
1260 (setq gdb-inferior-status "running")
1261 (gdb-force-mode-line-update gdb-inferior-status)
1262 (gdb-remove-text-properties)
1263 (setq gud-overlay-arrow-position nil)
1264 (setq gdb-overlay-arrow-position nil)
1265 (if gdb-use-separate-io-buffer
1266 (setq gdb-output-sink 'inferior))))
1269 (error "Unexpected `starting' annotation")))))
1271 (defun gdb-signal (ignored)
1272 (setq gdb-inferior-status "signal")
1273 (gdb-force-mode-line-update gdb-inferior-status)
1274 (gdb-stopping ignored))
1276 (defun gdb-stopping (ignored)
1277 "An annotation handler for `breakpoint' and other annotations.
1278 They say that I/O for the subprocess is now GDB, not the program
1280 (if gdb-use-separate-io-buffer
1281 (let ((sink gdb-output-sink))
1283 ((eq sink 'inferior)
1284 (setq gdb-output-sink 'user))
1287 (error "Unexpected stopping annotation"))))))
1289 (defun gdb-exited (ignored)
1290 "An annotation handler for `exited' and `signalled'.
1291 They say that I/O for the subprocess is now GDB, not the program
1292 being debugged and that the program is no longer running. This
1293 function is used to change the focus of GUD tooltips to #define
1295 (setq gdb-active-process nil)
1296 (setq gud-overlay-arrow-position nil)
1297 (setq gdb-overlay-arrow-position nil)
1298 (setq gdb-inferior-status "exited")
1299 (gdb-force-mode-line-update gdb-inferior-status)
1300 (gdb-stopping ignored))
1302 (defun gdb-signalled (ignored)
1303 (setq gdb-signalled t))
1305 (defun gdb-frame-begin (ignored)
1306 (let ((sink gdb-output-sink))
1308 ((eq sink 'inferior)
1309 (setq gdb-output-sink 'user))
1311 ((eq sink 'emacs) t)
1314 (error "Unexpected frame-begin annotation (%S)" sink)))))
1316 (defun gdb-stopped (ignored)
1317 "An annotation handler for `stopped'.
1318 It is just like `gdb-stopping', except that if we already set the output
1319 sink to `user' in `gdb-stopping', that is fine."
1320 (setq gud-running nil)
1321 (unless (or gud-overlay-arrow-position gud-last-frame)
1322 (gud-display-line (car gud-last-last-frame) (cdr gud-last-last-frame)))
1323 (unless (member gdb-inferior-status '("exited" "signal"))
1324 (setq gdb-inferior-status "stopped")
1325 (gdb-force-mode-line-update gdb-inferior-status))
1326 (let ((sink gdb-output-sink))
1328 ((eq sink 'inferior)
1329 (setq gdb-output-sink 'user))
1333 (error "Unexpected stopped annotation"))))
1334 (if gdb-signalled (gdb-exited ignored)))
1336 (defun gdb-error (ignored)
1337 (setq gdb-error (not gdb-error)))
1339 (defun gdb-post-prompt (ignored)
1340 "An annotation handler for `post-prompt'.
1341 This begins the collection of output from the current command if that
1342 happens to be appropriate."
1343 ;; Don't add to queue if there outstanding items or gdb-version is not known
1345 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1346 (gdb-get-selected-frame)
1347 (gdb-invalidate-frames)
1348 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1349 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1350 (gdb-invalidate-breakpoints)
1351 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1352 ;; so gdb-frame-address is updated.
1353 ;; (gdb-invalidate-assembler)
1355 (if (string-equal gdb-version "pre-6.4")
1356 (gdb-invalidate-registers)
1357 (gdb-get-changed-registers)
1358 (gdb-invalidate-registers-1))
1360 (gdb-invalidate-memory)
1361 (if (string-equal gdb-version "pre-6.4")
1362 (gdb-invalidate-locals)
1363 (gdb-invalidate-locals-1))
1365 (gdb-invalidate-threads)
1366 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1367 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1368 ;; Only needed/used with speedbar/watch expressions.
1369 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1370 (setq gdb-force-update t)
1371 (if (string-equal gdb-version "pre-6.4")
1373 (gdb-var-update-1)))))
1374 (setq gdb-first-post-prompt nil)
1375 (let ((sink gdb-output-sink))
1378 ((eq sink 'pre-emacs)
1379 (setq gdb-output-sink 'emacs))
1382 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1384 (defconst gdb-buffer-list
1385 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1387 (defun gdb-remove-text-properties ()
1388 (dolist (buffertype gdb-buffer-list)
1389 (let ((buffer (gdb-get-buffer buffertype)))
1391 (with-current-buffer buffer
1392 (let ((inhibit-read-only t))
1393 (remove-text-properties
1394 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1396 ;; GUD displays the selected GDB frame. This might might not be the current
1397 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1398 ;; visited breakpoint is, use that window.
1399 (defun gdb-display-source-buffer (buffer)
1400 (let* ((last-window (if gud-last-last-frame
1402 (gud-find-file (car gud-last-last-frame)))))
1403 (source-window (or last-window
1404 (if (and gdb-source-window
1405 (window-live-p gdb-source-window))
1406 gdb-source-window))))
1408 (setq gdb-source-window source-window)
1409 (set-window-buffer source-window buffer))
1412 (defun gud-gdba-marker-filter (string)
1413 "A gud marker filter for gdb. Handle a burst of output from GDB."
1414 (if gdb-flush-pending-output
1416 (when gdb-enable-debug
1417 (push (cons 'recv string) gdb-debug-ring)
1418 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1419 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1420 ;; Recall the left over gud-marker-acc from last time.
1421 (setq gud-marker-acc (concat gud-marker-acc string))
1422 ;; Start accumulating output for the GUD buffer.
1425 ;; Process all the complete markers in this chunk.
1426 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1427 (let ((annotation (match-string 1 gud-marker-acc)))
1429 ;; Stuff prior to the match is just ordinary output.
1430 ;; It is either concatenated to OUTPUT or directed
1435 (substring gud-marker-acc 0 (match-beginning 0))))
1437 ;; Take that stuff off the gud-marker-acc.
1438 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1440 ;; Parse the tag from the annotation, and maybe its arguments.
1441 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1442 (let* ((annotation-type (match-string 1 annotation))
1443 (annotation-arguments (match-string 2 annotation))
1444 (annotation-rule (assoc annotation-type
1445 gdb-annotation-rules)))
1446 ;; Call the handler for this annotation.
1448 (funcall (car (cdr annotation-rule))
1449 annotation-arguments)
1450 ;; Else the annotation is not recognized. Ignore it silently,
1451 ;; so that GDB can add new annotations without causing
1455 ;; Does the remaining text end in a partial line?
1456 ;; If it does, then keep part of the gud-marker-acc until we get more.
1457 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1460 ;; Everything before the potential marker start can be output.
1462 (gdb-concat-output output
1463 (substring gud-marker-acc 0
1464 (match-beginning 0))))
1466 ;; Everything after, we save, to combine with later input.
1467 (setq gud-marker-acc (substring gud-marker-acc
1468 (match-beginning 0))))
1470 ;; In case we know the gud-marker-acc contains no partial annotations:
1472 (setq output (gdb-concat-output output gud-marker-acc))
1473 (setq gud-marker-acc "")))
1476 (defun gdb-concat-output (so-far new)
1478 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1479 (let ((sink gdb-output-sink))
1481 ((eq sink 'user) (concat so-far new))
1482 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1484 (gdb-append-to-partial-output new)
1486 ((eq sink 'inferior)
1487 (gdb-append-to-inferior-io new)
1491 (error "Bogon output sink %S" sink)))))
1493 (defun gdb-append-to-partial-output (string)
1494 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1495 (goto-char (point-max))
1498 (defun gdb-clear-partial-output ()
1499 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1502 (defun gdb-append-to-inferior-io (string)
1503 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1504 (goto-char (point-max))
1505 (insert-before-markers string))
1506 (if (not (string-equal string ""))
1507 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1509 (defun gdb-clear-inferior-io ()
1510 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1514 ;; One trick is to have a command who's output is always available in a buffer
1515 ;; of it's own, and is always up to date. We build several buffers of this
1518 ;; There are two aspects to this: gdb has to tell us when the output for that
1519 ;; command might have changed, and we have to be able to run the command
1520 ;; behind the user's back.
1522 ;; The output phasing associated with the variable gdb-output-sink
1523 ;; help us to run commands behind the user's back.
1525 ;; Below is the code for specificly managing buffers of output from one
1529 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1530 ;; It adds an input for the command we are tracking. It should be the
1531 ;; annotation rule binding of whatever gdb sends to tell us this command
1532 ;; might have changed it's output.
1534 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1535 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1536 ;; input in the input queue (see comment about ``gdb communications'' above).
1538 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1540 `(defun ,name (&optional ignored)
1541 (if (and ,demand-predicate
1543 gdb-pending-triggers)))
1546 (list ,gdb-command ',output-handler))
1547 (push ',name gdb-pending-triggers)))))
1549 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1551 (setq gdb-pending-triggers
1553 gdb-pending-triggers))
1554 (let ((buf (gdb-get-buffer ',buf-key)))
1556 (with-current-buffer buf
1557 (let* ((window (get-buffer-window buf 0))
1558 (start (window-start window))
1559 (p (window-point window))
1560 (buffer-read-only nil))
1562 (insert-buffer-substring (gdb-get-buffer-create
1563 'gdb-partial-output-buffer))
1564 (set-window-start window start)
1565 (set-window-point window p)))))
1566 ;; put customisation here
1569 (defmacro def-gdb-auto-updated-buffer (buffer-key
1570 trigger-name gdb-command
1571 output-handler-name custom-defun)
1573 (def-gdb-auto-update-trigger ,trigger-name
1574 ;; The demand predicate:
1575 (gdb-get-buffer ',buffer-key)
1577 ,output-handler-name)
1578 (def-gdb-auto-update-handler ,output-handler-name
1579 ,trigger-name ,buffer-key ,custom-defun)))
1583 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1585 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1586 'gdb-breakpoints-buffer-name
1587 'gdb-breakpoints-mode)
1589 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1590 ;; This defines the auto update rule for buffers of type
1591 ;; `gdb-breakpoints-buffer'.
1593 ;; It defines a function to serve as the annotation handler that
1594 ;; handles the `foo-invalidated' message. That function is called:
1595 gdb-invalidate-breakpoints
1597 ;; To update the buffer, this command is sent to gdb.
1598 "server info breakpoints\n"
1600 ;; This also defines a function to be the handler for the output
1601 ;; from the command above. That function will copy the output into
1602 ;; the appropriately typed buffer. That function will be called:
1603 gdb-info-breakpoints-handler
1604 ;; buffer specific functions
1605 gdb-info-breakpoints-custom)
1607 (defconst breakpoint-xpm-data
1609 static char *magick[] = {
1610 /* columns rows colors chars-per-pixel */
1626 "XPM data used for breakpoint icon.")
1628 (defconst breakpoint-enabled-pbm-data
1631 0 0 0 0 1 1 1 1 0 0 0 0
1632 0 0 0 1 1 1 1 1 1 0 0 0
1633 0 0 1 1 1 1 1 1 1 1 0 0
1634 0 1 1 1 1 1 1 1 1 1 1 0
1635 0 1 1 1 1 1 1 1 1 1 1 0
1636 0 1 1 1 1 1 1 1 1 1 1 0
1637 0 1 1 1 1 1 1 1 1 1 1 0
1638 0 0 1 1 1 1 1 1 1 1 0 0
1639 0 0 0 1 1 1 1 1 1 0 0 0
1640 0 0 0 0 1 1 1 1 0 0 0 0"
1641 "PBM data used for enabled breakpoint icon.")
1643 (defconst breakpoint-disabled-pbm-data
1655 0 0 0 1 0 1 0 1 0 0"
1656 "PBM data used for disabled breakpoint icon.")
1658 (defvar breakpoint-enabled-icon nil
1659 "Icon for enabled breakpoint in display margin.")
1661 (defvar breakpoint-disabled-icon nil
1662 "Icon for disabled breakpoint in display margin.")
1664 ;; Bitmap for breakpoint in fringe
1665 (and (display-images-p)
1666 (define-fringe-bitmap 'breakpoint
1667 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1669 (defface breakpoint-enabled
1673 "Face for enabled breakpoint icon in fringe."
1676 (defface breakpoint-disabled
1677 ;; We use different values of grey for different background types,
1678 ;; so that on low-color displays it will end up as something visible
1679 ;; if it has to be approximated.
1680 '((((background dark)) :foreground "grey60")
1681 (((background light)) :foreground "grey40"))
1682 "Face for disabled breakpoint icon in fringe."
1685 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1686 (defun gdb-info-breakpoints-custom ()
1687 (let ((flag) (bptno))
1688 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1689 (dolist (buffer (buffer-list))
1690 (with-current-buffer buffer
1691 (if (and (memq gud-minor-mode '(gdba gdbmi))
1692 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1693 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1694 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1696 (goto-char (point-min))
1697 (while (< (point) (- (point-max) 1))
1699 (if (looking-at "[^\t].*?breakpoint")
1701 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1702 (setq bptno (match-string 1))
1703 (setq flag (char-after (match-beginning 2)))
1705 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1707 (let ((buffer-read-only nil))
1708 (add-text-properties (match-beginning 1) (match-end 1)
1709 '(face font-lock-function-name-face)))
1710 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1711 (let ((line (match-string 2)) (buffer-read-only nil)
1712 (file (match-string 1)))
1713 (add-text-properties (line-beginning-position)
1715 '(mouse-face highlight
1716 help-echo "mouse-2, RET: visit breakpoint"))
1717 (unless (file-exists-p file)
1718 (setq file (cdr (assoc bptno gdb-location-alist))))
1720 (not (string-equal file "File not found")))
1721 (with-current-buffer
1722 (find-file-noselect file 'nowarn)
1723 (set (make-local-variable 'gud-minor-mode)
1725 (set (make-local-variable 'tool-bar-map)
1727 ;; Only want one breakpoint icon at each
1730 (goto-line (string-to-number line))
1731 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1734 (concat gdb-server-prefix "list "
1735 (match-string-no-properties 1) ":1\n")
1738 (list (concat gdb-server-prefix "info source\n")
1739 `(lambda () (gdb-get-location
1740 ,bptno ,line ,flag))))))))))
1742 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1744 (defun gdb-mouse-set-clear-breakpoint (event)
1745 "Set/clear breakpoint in left fringe/margin with mouse click."
1747 (mouse-minibuffer-check event)
1748 (let ((posn (event-end event)))
1749 (if (numberp (posn-point posn))
1750 (with-selected-window (posn-window posn)
1752 (goto-char (posn-point posn))
1753 (if (or (posn-object posn)
1754 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1757 (gud-break nil)))))))
1759 (defun gdb-mouse-toggle-breakpoint-margin (event)
1760 "Enable/disable breakpoint in left margin with mouse click."
1762 (mouse-minibuffer-check event)
1763 (let ((posn (event-end event)))
1764 (if (numberp (posn-point posn))
1765 (with-selected-window (posn-window posn)
1767 (goto-char (posn-point posn))
1768 (if (posn-object posn)
1771 (let ((bptno (get-text-property
1772 0 'gdb-bptno (car (posn-string posn)))))
1773 (concat gdb-server-prefix
1774 (if (get-text-property
1775 0 'gdb-enabled (car (posn-string posn)))
1781 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1782 "Enable/disable breakpoint in left fringe with mouse click."
1784 (mouse-minibuffer-check event)
1785 (let* ((posn (event-end event))
1786 (pos (posn-point posn))
1789 (with-selected-window (posn-window posn)
1791 (set-buffer (window-buffer (selected-window)))
1793 (dolist (overlay (overlays-in pos pos))
1794 (when (overlay-get overlay 'put-break)
1795 (setq obj (overlay-get overlay 'before-string))))
1799 (concat gdb-server-prefix
1800 (if (get-text-property 0 'gdb-enabled obj)
1803 (get-text-property 0 'gdb-bptno obj) "\n")
1806 (defun gdb-breakpoints-buffer-name ()
1807 (with-current-buffer gud-comint-buffer
1808 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1810 (defun gdb-display-breakpoints-buffer ()
1811 "Display status of user-settable breakpoints."
1814 (gdb-get-buffer-create 'gdb-breakpoints-buffer)))
1816 (defun gdb-frame-breakpoints-buffer ()
1817 "Display status of user-settable breakpoints in a new frame."
1819 (let ((special-display-regexps (append special-display-regexps '(".*")))
1820 (special-display-frame-alist gdb-frame-parameters))
1821 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1823 (defvar gdb-breakpoints-mode-map
1824 (let ((map (make-sparse-keymap))
1825 (menu (make-sparse-keymap "Breakpoints")))
1826 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1827 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1828 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1829 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1830 (suppress-keymap map)
1831 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1832 (define-key map " " 'gdb-toggle-breakpoint)
1833 (define-key map "D" 'gdb-delete-breakpoint)
1834 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1835 (define-key map "q" 'gdb-delete-frame-or-window)
1836 (define-key map "\r" 'gdb-goto-breakpoint)
1837 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1838 (define-key map [follow-link] 'mouse-face)
1841 (defun gdb-delete-frame-or-window ()
1842 "Delete frame if there is only one window. Otherwise delete the window."
1844 (if (one-window-p) (delete-frame)
1847 (defun gdb-breakpoints-mode ()
1848 "Major mode for gdb breakpoints.
1850 \\{gdb-breakpoints-mode-map}"
1851 (kill-all-local-variables)
1852 (setq major-mode 'gdb-breakpoints-mode)
1853 (setq mode-name "Breakpoints")
1854 (use-local-map gdb-breakpoints-mode-map)
1855 (setq buffer-read-only t)
1856 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1857 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1858 'gdb-invalidate-breakpoints
1859 'gdbmi-invalidate-breakpoints))
1861 (defconst gdb-breakpoint-regexp
1862 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1864 (defun gdb-toggle-breakpoint ()
1865 "Enable/disable breakpoint at current line."
1868 (beginning-of-line 1)
1869 (if (looking-at gdb-breakpoint-regexp)
1872 (concat gdb-server-prefix
1873 (if (eq ?y (char-after (match-beginning 2)))
1876 (match-string 1) "\n") 'ignore))
1877 (error "Not recognized as break/watchpoint line"))))
1879 (defun gdb-delete-breakpoint ()
1880 "Delete the breakpoint at current line."
1882 (beginning-of-line 1)
1883 (if (looking-at gdb-breakpoint-regexp)
1886 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1887 (error "Not recognized as break/watchpoint line")))
1889 (defun gdb-goto-breakpoint (&optional event)
1890 "Display the breakpoint location specified at current line."
1891 (interactive (list last-input-event))
1892 (if event (posn-set-point (event-end event)))
1894 (beginning-of-line 1)
1895 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1896 (let ((bptno (match-string 1))
1897 (file (match-string 2))
1898 (line (match-string 3)))
1899 (save-selected-window
1900 (let* ((buffer (find-file-noselect
1901 (if (file-exists-p file) file
1902 (cdr (assoc bptno gdb-location-alist)))))
1903 (window (unless (gdb-display-source-buffer buffer)
1904 (display-buffer buffer))))
1905 (setq gdb-source-window window)
1906 (with-current-buffer buffer
1907 (goto-line (string-to-number line))
1908 (set-window-point window (point))))))
1909 (error "No location specified."))))
1912 ;; Frames buffer. This displays a perpetually correct bactracktrace
1913 ;; (from the command `where').
1915 ;; Alas, if your stack is deep, it is costly.
1917 (gdb-set-buffer-rules 'gdb-stack-buffer
1918 'gdb-stack-buffer-name
1921 (def-gdb-auto-updated-buffer gdb-stack-buffer
1922 gdb-invalidate-frames
1924 gdb-info-frames-handler
1925 gdb-info-frames-custom)
1927 (defun gdb-info-frames-custom ()
1928 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1930 (let ((buffer-read-only nil)
1932 (goto-char (point-min))
1933 (while (< (point) (point-max))
1934 (setq bl (line-beginning-position)
1935 el (line-end-position))
1936 (when (looking-at "#")
1937 (add-text-properties bl el
1938 '(mouse-face highlight
1939 help-echo "mouse-2, RET: Select frame")))
1941 (when (looking-at "^#\\([0-9]+\\)")
1942 (when (string-equal (match-string 1) gdb-frame-number)
1943 (put-text-property bl (+ bl 4)
1944 'face '(:inverse-video t)))
1945 (when (re-search-forward
1947 (if (string-equal (match-string 1) "0") "" " in ")
1948 "\\([^ ]+\\) (") el t)
1949 (put-text-property (match-beginning 1) (match-end 1)
1950 'face font-lock-function-name-face)
1951 (setq bl (match-end 0))
1952 (while (re-search-forward "<\\([^>]+\\)>" el t)
1953 (put-text-property (match-beginning 1) (match-end 1)
1954 'face font-lock-function-name-face))
1956 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1957 (put-text-property (match-beginning 1) (match-end 1)
1958 'face font-lock-variable-name-face))))
1959 (forward-line 1))))))
1961 (defun gdb-stack-buffer-name ()
1962 (with-current-buffer gud-comint-buffer
1963 (concat "*stack frames of " (gdb-get-target-string) "*")))
1965 (defun gdb-display-stack-buffer ()
1966 "Display backtrace of current stack."
1969 (gdb-get-buffer-create 'gdb-stack-buffer)))
1971 (defun gdb-frame-stack-buffer ()
1972 "Display backtrace of current stack in a new frame."
1974 (let ((special-display-regexps (append special-display-regexps '(".*")))
1975 (special-display-frame-alist gdb-frame-parameters))
1976 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
1978 (defvar gdb-frames-mode-map
1979 (let ((map (make-sparse-keymap)))
1980 (suppress-keymap map)
1981 (define-key map "q" 'kill-this-buffer)
1982 (define-key map "\r" 'gdb-frames-select)
1983 (define-key map [mouse-2] 'gdb-frames-select)
1984 (define-key map [follow-link] 'mouse-face)
1987 (defun gdb-frames-mode ()
1988 "Major mode for gdb frames.
1990 \\{gdb-frames-mode-map}"
1991 (kill-all-local-variables)
1992 (setq major-mode 'gdb-frames-mode)
1993 (setq mode-name "Frames")
1994 (setq buffer-read-only t)
1995 (use-local-map gdb-frames-mode-map)
1996 (run-mode-hooks 'gdb-frames-mode-hook)
1997 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1998 'gdb-invalidate-frames
1999 'gdbmi-invalidate-frames))
2001 (defun gdb-get-frame-number ()
2004 (let* ((start (line-beginning-position))
2005 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2006 (n (or (and pos (match-string-no-properties 1)) "0")))
2009 (defun gdb-frames-select (&optional event)
2010 "Select the frame and display the relevant source."
2011 (interactive (list last-input-event))
2012 (if event (posn-set-point (event-end event)))
2014 (list (concat gdb-server-prefix "frame "
2015 (gdb-get-frame-number) "\n") 'ignore))
2016 (gud-display-frame))
2019 ;; Threads buffer. This displays a selectable thread list.
2021 (gdb-set-buffer-rules 'gdb-threads-buffer
2022 'gdb-threads-buffer-name
2025 (def-gdb-auto-updated-buffer gdb-threads-buffer
2026 gdb-invalidate-threads
2027 (concat gdb-server-prefix "info threads\n")
2028 gdb-info-threads-handler
2029 gdb-info-threads-custom)
2031 (defun gdb-info-threads-custom ()
2032 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2033 (let ((buffer-read-only nil))
2034 (goto-char (point-min))
2035 (while (< (point) (point-max))
2036 (unless (looking-at "No ")
2037 (add-text-properties (line-beginning-position) (line-end-position)
2038 '(mouse-face highlight
2039 help-echo "mouse-2, RET: select thread")))
2040 (forward-line 1)))))
2042 (defun gdb-threads-buffer-name ()
2043 (with-current-buffer gud-comint-buffer
2044 (concat "*threads of " (gdb-get-target-string) "*")))
2046 (defun gdb-display-threads-buffer ()
2047 "Display IDs of currently known threads."
2050 (gdb-get-buffer-create 'gdb-threads-buffer)))
2052 (defun gdb-frame-threads-buffer ()
2053 "Display IDs of currently known threads in a new frame."
2055 (let ((special-display-regexps (append special-display-regexps '(".*")))
2056 (special-display-frame-alist gdb-frame-parameters))
2057 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2059 (defvar gdb-threads-mode-map
2060 (let ((map (make-sparse-keymap)))
2061 (suppress-keymap map)
2062 (define-key map "q" 'kill-this-buffer)
2063 (define-key map "\r" 'gdb-threads-select)
2064 (define-key map [mouse-2] 'gdb-threads-select)
2065 (define-key map [follow-link] 'mouse-face)
2068 (defvar gdb-threads-font-lock-keywords
2070 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2071 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2072 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
2074 "Font lock keywords used in `gdb-threads-mode'.")
2076 (defun gdb-threads-mode ()
2077 "Major mode for gdb frames.
2079 \\{gdb-threads-mode-map}"
2080 (kill-all-local-variables)
2081 (setq major-mode 'gdb-threads-mode)
2082 (setq mode-name "Threads")
2083 (setq buffer-read-only t)
2084 (use-local-map gdb-threads-mode-map)
2085 (set (make-local-variable 'font-lock-defaults)
2086 '(gdb-threads-font-lock-keywords))
2087 (run-mode-hooks 'gdb-threads-mode-hook)
2088 'gdb-invalidate-threads)
2090 (defun gdb-get-thread-number ()
2092 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2093 (match-string-no-properties 1)))
2095 (defun gdb-threads-select (&optional event)
2096 "Select the thread and display the relevant source."
2097 (interactive (list last-input-event))
2098 (if event (posn-set-point (event-end event)))
2100 (list (concat gdb-server-prefix "thread "
2101 (gdb-get-thread-number) "\n") 'ignore))
2102 (gud-display-frame))
2105 ;; Registers buffer.
2107 (defcustom gdb-all-registers nil
2108 "Non-nil means include floating-point registers."
2113 (gdb-set-buffer-rules 'gdb-registers-buffer
2114 'gdb-registers-buffer-name
2115 'gdb-registers-mode)
2117 (def-gdb-auto-updated-buffer gdb-registers-buffer
2118 gdb-invalidate-registers
2120 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2121 gdb-info-registers-handler
2122 gdb-info-registers-custom)
2124 (defun gdb-info-registers-custom ()
2125 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2127 (let ((buffer-read-only nil)
2129 (goto-char (point-min))
2130 (while (< (point) (point-max))
2131 (setq start (line-beginning-position))
2132 (setq end (line-end-position))
2133 (when (looking-at "^[^ ]+")
2134 (unless (string-equal (match-string 0) "The")
2135 (put-text-property start (match-end 0)
2136 'face font-lock-variable-name-face)
2137 (add-text-properties start end
2138 '(help-echo "mouse-2: edit value"
2139 mouse-face highlight))))
2140 (forward-line 1))))))
2142 (defun gdb-edit-register-value (&optional event)
2143 (interactive (list last-input-event))
2145 (if event (posn-set-point (event-end event)))
2147 (let* ((register (current-word))
2148 (value (read-string (format "New value (%s): " register))))
2150 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2153 (defvar gdb-registers-mode-map
2154 (let ((map (make-sparse-keymap)))
2155 (suppress-keymap map)
2156 (define-key map "\r" 'gdb-edit-register-value)
2157 (define-key map [mouse-2] 'gdb-edit-register-value)
2158 (define-key map " " 'gdb-all-registers)
2159 (define-key map "q" 'kill-this-buffer)
2162 (defun gdb-registers-mode ()
2163 "Major mode for gdb registers.
2165 \\{gdb-registers-mode-map}"
2166 (kill-all-local-variables)
2167 (setq major-mode 'gdb-registers-mode)
2168 (setq mode-name "Registers")
2169 (setq buffer-read-only t)
2170 (use-local-map gdb-registers-mode-map)
2171 (run-mode-hooks 'gdb-registers-mode-hook)
2172 (if (string-equal gdb-version "pre-6.4")
2174 (if gdb-all-registers (setq mode-name "Registers:All"))
2175 'gdb-invalidate-registers)
2176 'gdb-invalidate-registers-1))
2178 (defun gdb-registers-buffer-name ()
2179 (with-current-buffer gud-comint-buffer
2180 (concat "*registers of " (gdb-get-target-string) "*")))
2182 (defun gdb-display-registers-buffer ()
2183 "Display integer register contents."
2186 (gdb-get-buffer-create 'gdb-registers-buffer)))
2188 (defun gdb-frame-registers-buffer ()
2189 "Display integer register contents in a new frame."
2191 (let ((special-display-regexps (append special-display-regexps '(".*")))
2192 (special-display-frame-alist gdb-frame-parameters))
2193 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2195 (defun gdb-all-registers ()
2196 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2198 (when (string-equal gdb-version "pre-6.4")
2199 (if gdb-all-registers
2201 (setq gdb-all-registers nil)
2202 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2203 (setq mode-name "Registers")))
2204 (setq gdb-all-registers t)
2205 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2206 (setq mode-name "Registers:All")))
2207 (message (format "Display of floating-point registers %sabled"
2208 (if gdb-all-registers "en" "dis")))
2209 (gdb-invalidate-registers)))
2214 (defcustom gdb-memory-repeat-count 32
2215 "Number of data items in memory window."
2220 (defcustom gdb-memory-format "x"
2221 "Display format of data items in memory window."
2222 :type '(choice (const :tag "Hexadecimal" "x")
2223 (const :tag "Signed decimal" "d")
2224 (const :tag "Unsigned decimal" "u")
2225 (const :tag "Octal" "o")
2226 (const :tag "Binary" "t"))
2230 (defcustom gdb-memory-unit "w"
2231 "Unit size of data items in memory window."
2232 :type '(choice (const :tag "Byte" "b")
2233 (const :tag "Halfword" "h")
2234 (const :tag "Word" "w")
2235 (const :tag "Giant word" "g"))
2239 (gdb-set-buffer-rules 'gdb-memory-buffer
2240 'gdb-memory-buffer-name
2243 (def-gdb-auto-updated-buffer gdb-memory-buffer
2244 gdb-invalidate-memory
2245 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2246 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2247 gdb-read-memory-handler
2248 gdb-read-memory-custom)
2250 (defun gdb-read-memory-custom ()
2252 (goto-char (point-min))
2253 (if (looking-at "0x[[:xdigit:]]+")
2254 (setq gdb-memory-address (match-string 0)))))
2256 (defvar gdb-memory-mode-map
2257 (let ((map (make-sparse-keymap)))
2258 (suppress-keymap map)
2259 (define-key map "q" 'kill-this-buffer)
2262 (defun gdb-memory-set-address (event)
2263 "Set the start memory address."
2265 (save-selected-window
2266 (select-window (posn-window (event-start event)))
2267 (let ((arg (read-from-minibuffer "Memory address: ")))
2268 (setq gdb-memory-address arg))
2269 (gdb-invalidate-memory)))
2271 (defun gdb-memory-set-repeat-count (event)
2272 "Set the number of data items in memory window."
2274 (save-selected-window
2275 (select-window (posn-window (event-start event)))
2276 (let* ((arg (read-from-minibuffer "Repeat count: "))
2277 (count (string-to-number arg)))
2279 (error "Positive numbers only")
2280 (customize-set-variable 'gdb-memory-repeat-count count)
2281 (gdb-invalidate-memory)))))
2283 (defun gdb-memory-format-binary ()
2284 "Set the display format to binary."
2286 (customize-set-variable 'gdb-memory-format "t")
2287 (gdb-invalidate-memory))
2289 (defun gdb-memory-format-octal ()
2290 "Set the display format to octal."
2292 (customize-set-variable 'gdb-memory-format "o")
2293 (gdb-invalidate-memory))
2295 (defun gdb-memory-format-unsigned ()
2296 "Set the display format to unsigned decimal."
2298 (customize-set-variable 'gdb-memory-format "u")
2299 (gdb-invalidate-memory))
2301 (defun gdb-memory-format-signed ()
2302 "Set the display format to decimal."
2304 (customize-set-variable 'gdb-memory-format "d")
2305 (gdb-invalidate-memory))
2307 (defun gdb-memory-format-hexadecimal ()
2308 "Set the display format to hexadecimal."
2310 (customize-set-variable 'gdb-memory-format "x")
2311 (gdb-invalidate-memory))
2313 (defvar gdb-memory-format-map
2314 (let ((map (make-sparse-keymap)))
2315 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2317 "Keymap to select format in the header line.")
2319 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2320 "Menu of display formats in the header line.")
2322 (define-key gdb-memory-format-menu [binary]
2323 '(menu-item "Binary" gdb-memory-format-binary
2324 :button (:radio . (equal gdb-memory-format "t"))))
2325 (define-key gdb-memory-format-menu [octal]
2326 '(menu-item "Octal" gdb-memory-format-octal
2327 :button (:radio . (equal gdb-memory-format "o"))))
2328 (define-key gdb-memory-format-menu [unsigned]
2329 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2330 :button (:radio . (equal gdb-memory-format "u"))))
2331 (define-key gdb-memory-format-menu [signed]
2332 '(menu-item "Signed Decimal" gdb-memory-format-signed
2333 :button (:radio . (equal gdb-memory-format "d"))))
2334 (define-key gdb-memory-format-menu [hexadecimal]
2335 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2336 :button (:radio . (equal gdb-memory-format "x"))))
2338 (defun gdb-memory-format-menu (event)
2340 (x-popup-menu event gdb-memory-format-menu))
2342 (defun gdb-memory-format-menu-1 (event)
2344 (save-selected-window
2345 (select-window (posn-window (event-start event)))
2346 (let* ((selection (gdb-memory-format-menu event))
2347 (binding (and selection (lookup-key gdb-memory-format-menu
2348 (vector (car selection))))))
2349 (if binding (call-interactively binding)))))
2351 (defun gdb-memory-unit-giant ()
2352 "Set the unit size to giant words (eight bytes)."
2354 (customize-set-variable 'gdb-memory-unit "g")
2355 (gdb-invalidate-memory))
2357 (defun gdb-memory-unit-word ()
2358 "Set the unit size to words (four bytes)."
2360 (customize-set-variable 'gdb-memory-unit "w")
2361 (gdb-invalidate-memory))
2363 (defun gdb-memory-unit-halfword ()
2364 "Set the unit size to halfwords (two bytes)."
2366 (customize-set-variable 'gdb-memory-unit "h")
2367 (gdb-invalidate-memory))
2369 (defun gdb-memory-unit-byte ()
2370 "Set the unit size to bytes."
2372 (customize-set-variable 'gdb-memory-unit "b")
2373 (gdb-invalidate-memory))
2375 (defvar gdb-memory-unit-map
2376 (let ((map (make-sparse-keymap)))
2377 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2379 "Keymap to select units in the header line.")
2381 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2382 "Menu of units in the header line.")
2384 (define-key gdb-memory-unit-menu [giantwords]
2385 '(menu-item "Giant words" gdb-memory-unit-giant
2386 :button (:radio . (equal gdb-memory-unit "g"))))
2387 (define-key gdb-memory-unit-menu [words]
2388 '(menu-item "Words" gdb-memory-unit-word
2389 :button (:radio . (equal gdb-memory-unit "w"))))
2390 (define-key gdb-memory-unit-menu [halfwords]
2391 '(menu-item "Halfwords" gdb-memory-unit-halfword
2392 :button (:radio . (equal gdb-memory-unit "h"))))
2393 (define-key gdb-memory-unit-menu [bytes]
2394 '(menu-item "Bytes" gdb-memory-unit-byte
2395 :button (:radio . (equal gdb-memory-unit "b"))))
2397 (defun gdb-memory-unit-menu (event)
2399 (x-popup-menu event gdb-memory-unit-menu))
2401 (defun gdb-memory-unit-menu-1 (event)
2403 (save-selected-window
2404 (select-window (posn-window (event-start event)))
2405 (let* ((selection (gdb-memory-unit-menu event))
2406 (binding (and selection (lookup-key gdb-memory-unit-menu
2407 (vector (car selection))))))
2408 (if binding (call-interactively binding)))))
2410 ;;from make-mode-line-mouse-map
2411 (defun gdb-make-header-line-mouse-map (mouse function) "\
2412 Return a keymap with single entry for mouse key MOUSE on the header line.
2413 MOUSE is defined to run function FUNCTION with no args in the buffer
2414 corresponding to the mode line clicked."
2415 (let ((map (make-sparse-keymap)))
2416 (define-key map (vector 'header-line mouse) function)
2417 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2420 (defvar gdb-memory-font-lock-keywords
2421 '(;; <__function.name+n>
2422 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2424 "Font lock keywords used in `gdb-memory-mode'.")
2426 (defun gdb-memory-mode ()
2427 "Major mode for examining memory.
2429 \\{gdb-memory-mode-map}"
2430 (kill-all-local-variables)
2431 (setq major-mode 'gdb-memory-mode)
2432 (setq mode-name "Memory")
2433 (setq buffer-read-only t)
2434 (use-local-map gdb-memory-mode-map)
2435 (setq header-line-format
2441 'face font-lock-warning-face
2442 'help-echo "mouse-1: Decrement address"
2443 'mouse-face 'mode-line-highlight
2445 (gdb-make-header-line-mouse-map
2447 #'(lambda () (interactive)
2448 (let ((gdb-memory-address
2449 ;; Let GDB do the arithmetic.
2451 gdb-memory-address " - "
2453 (* gdb-memory-repeat-count
2454 (cond ((string= gdb-memory-unit "b") 1)
2455 ((string= gdb-memory-unit "h") 2)
2456 ((string= gdb-memory-unit "w") 4)
2457 ((string= gdb-memory-unit "g") 8)))))))
2458 (gdb-invalidate-memory)))))
2461 'face font-lock-warning-face
2462 'help-echo "mouse-1: Increment address"
2463 'mouse-face 'mode-line-highlight
2464 'local-map (gdb-make-header-line-mouse-map
2466 #'(lambda () (interactive)
2467 (let ((gdb-memory-address nil))
2468 (gdb-invalidate-memory)))))
2470 (propertize gdb-memory-address
2471 'face font-lock-warning-face
2472 'help-echo "mouse-1: Set memory address"
2473 'mouse-face 'mode-line-highlight
2474 'local-map (gdb-make-header-line-mouse-map
2476 #'gdb-memory-set-address))
2478 (propertize (number-to-string gdb-memory-repeat-count)
2479 'face font-lock-warning-face
2480 'help-echo "mouse-1: Set repeat count"
2481 'mouse-face 'mode-line-highlight
2482 'local-map (gdb-make-header-line-mouse-map
2484 #'gdb-memory-set-repeat-count))
2486 (propertize gdb-memory-format
2487 'face font-lock-warning-face
2488 'help-echo "mouse-3: Select display format"
2489 'mouse-face 'mode-line-highlight
2490 'local-map gdb-memory-format-map)
2492 (propertize gdb-memory-unit
2493 'face font-lock-warning-face
2494 'help-echo "mouse-3: Select unit size"
2495 'mouse-face 'mode-line-highlight
2496 'local-map gdb-memory-unit-map))))
2497 (set (make-local-variable 'font-lock-defaults)
2498 '(gdb-memory-font-lock-keywords))
2499 (run-mode-hooks 'gdb-memory-mode-hook)
2500 'gdb-invalidate-memory)
2502 (defun gdb-memory-buffer-name ()
2503 (with-current-buffer gud-comint-buffer
2504 (concat "*memory of " (gdb-get-target-string) "*")))
2506 (defun gdb-display-memory-buffer ()
2507 "Display memory contents."
2510 (gdb-get-buffer-create 'gdb-memory-buffer)))
2512 (defun gdb-frame-memory-buffer ()
2513 "Display memory contents in a new frame."
2515 (let ((special-display-regexps (append special-display-regexps '(".*")))
2516 (special-display-frame-alist gdb-frame-parameters))
2517 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2522 (gdb-set-buffer-rules 'gdb-locals-buffer
2523 'gdb-locals-buffer-name
2526 (def-gdb-auto-update-trigger gdb-invalidate-locals
2527 (gdb-get-buffer 'gdb-locals-buffer)
2528 "server info locals\n"
2529 gdb-info-locals-handler)
2531 (defvar gdb-locals-watch-map
2532 (let ((map (make-sparse-keymap)))
2533 (define-key map "\r" '(lambda () (interactive)
2536 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2537 (mouse-set-point event)
2541 "Keymap to create watch expression of a complex data type local variable.")
2543 (defconst gdb-struct-string
2544 (concat (propertize "[struct/union]"
2545 'mouse-face 'highlight
2546 'help-echo "mouse-2: create watch expression"
2547 'local-map gdb-locals-watch-map) "\n"))
2549 (defconst gdb-array-string
2550 (concat " " (propertize "[array]"
2551 'mouse-face 'highlight
2552 'help-echo "mouse-2: create watch expression"
2553 'local-map gdb-locals-watch-map) "\n"))
2555 ;; Abbreviate for arrays and structures.
2556 ;; These can be expanded using gud-display.
2557 (defun gdb-info-locals-handler ()
2558 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2559 gdb-pending-triggers))
2560 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2561 (with-current-buffer buf
2562 (goto-char (point-min))
2563 (while (re-search-forward "^[ }].*\n" nil t)
2564 (replace-match "" nil nil))
2565 (goto-char (point-min))
2566 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2567 (replace-match gdb-struct-string nil nil))
2568 (goto-char (point-min))
2569 (while (re-search-forward "\\s-*{.*\n" nil t)
2570 (replace-match gdb-array-string nil nil))))
2571 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2573 (with-current-buffer buf
2574 (let* ((window (get-buffer-window buf 0))
2575 (start (window-start window))
2576 (p (window-point window))
2577 (buffer-read-only nil))
2579 (insert-buffer-substring (gdb-get-buffer-create
2580 'gdb-partial-output-buffer))
2581 (set-window-start window start)
2582 (set-window-point window p))
2584 (run-hooks 'gdb-info-locals-hook))
2586 (defvar gdb-locals-mode-map
2587 (let ((map (make-sparse-keymap)))
2588 (suppress-keymap map)
2589 (define-key map "q" 'kill-this-buffer)
2592 (defun gdb-locals-mode ()
2593 "Major mode for gdb locals.
2595 \\{gdb-locals-mode-map}"
2596 (kill-all-local-variables)
2597 (setq major-mode 'gdb-locals-mode)
2598 (setq mode-name (concat "Locals:" gdb-selected-frame))
2599 (setq buffer-read-only t)
2600 (use-local-map gdb-locals-mode-map)
2601 (set (make-local-variable 'font-lock-defaults)
2602 '(gdb-locals-font-lock-keywords))
2603 (run-mode-hooks 'gdb-locals-mode-hook)
2604 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2605 (string-equal gdb-version "pre-6.4"))
2606 'gdb-invalidate-locals
2607 'gdb-invalidate-locals-1))
2609 (defun gdb-locals-buffer-name ()
2610 (with-current-buffer gud-comint-buffer
2611 (concat "*locals of " (gdb-get-target-string) "*")))
2613 (defun gdb-display-locals-buffer ()
2614 "Display local variables of current stack and their values."
2617 (gdb-get-buffer-create 'gdb-locals-buffer)))
2619 (defun gdb-frame-locals-buffer ()
2620 "Display local variables of current stack and their values in a new frame."
2622 (let ((special-display-regexps (append special-display-regexps '(".*")))
2623 (special-display-frame-alist gdb-frame-parameters))
2624 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2627 ;;;; Window management
2628 (defun gdb-display-buffer (buf &optional size)
2629 (let ((answer (get-buffer-window buf 0))
2632 (display-buffer buf nil 0) ;Raise the frame if necessary.
2633 ;; The buffer is not yet displayed.
2634 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2635 (let ((window (get-lru-window)))
2637 (not (eq window (get-buffer-window gud-comint-buffer))))
2639 (set-window-buffer window buf)
2640 (setq answer window))
2641 (setq must-split t)))
2643 (let* ((largest (get-largest-window))
2644 (cur-size (window-height largest))
2645 (new-size (and size (< size cur-size) (- cur-size size))))
2646 (setq answer (split-window largest new-size))
2647 (set-window-buffer answer buf)
2648 (set-window-dedicated-p answer t)))
2652 ;;; Shared keymap initialization:
2654 (let ((menu (make-sparse-keymap "GDB-Windows")))
2655 (define-key gud-menu-map [displays]
2656 `(menu-item "GDB-Windows" ,menu
2657 :visible (memq gud-minor-mode '(gdbmi gdba))))
2658 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2659 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2660 (define-key menu [inferior]
2661 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2662 :enable gdb-use-separate-io-buffer))
2663 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2664 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2665 (define-key menu [disassembly]
2666 '("Disassembly" . gdb-display-assembler-buffer))
2667 (define-key menu [breakpoints]
2668 '("Breakpoints" . gdb-display-breakpoints-buffer))
2669 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2670 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2672 (let ((menu (make-sparse-keymap "GDB-Frames")))
2673 (define-key gud-menu-map [frames]
2674 `(menu-item "GDB-Frames" ,menu
2675 :visible (memq gud-minor-mode '(gdbmi gdba))))
2676 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2677 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2678 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2679 (define-key menu [inferior]
2680 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2681 :enable gdb-use-separate-io-buffer))
2682 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2683 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2684 (define-key menu [breakpoints]
2685 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2686 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2687 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2689 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2690 (define-key gud-menu-map [ui]
2691 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2692 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2693 (define-key menu [gdb-use-separate-io]
2694 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2695 :visible (eq gud-minor-mode 'gdba)
2696 :help "Toggle separate IO for inferior."
2697 :button (:toggle . gdb-use-separate-io-buffer)))
2698 (define-key menu [gdb-many-windows]
2699 '(menu-item "Display Other Windows" gdb-many-windows
2700 :help "Toggle display of locals, stack and breakpoint information"
2701 :button (:toggle . gdb-many-windows)))
2702 (define-key menu [gdb-restore-windows]
2703 '(menu-item "Restore Window Layout" gdb-restore-windows
2704 :help "Restore standard layout for debug session.")))
2706 (defun gdb-frame-gdb-buffer ()
2707 "Display GUD buffer in a new frame."
2709 (let ((special-display-regexps (append special-display-regexps '(".*")))
2710 (special-display-frame-alist gdb-frame-parameters)
2711 (same-window-regexps nil))
2712 (display-buffer gud-comint-buffer)))
2714 (defun gdb-display-gdb-buffer ()
2715 "Display GUD buffer."
2717 (let ((same-window-regexps nil))
2718 (pop-to-buffer gud-comint-buffer)))
2720 (defun gdb-set-window-buffer (name)
2721 (set-window-buffer (selected-window) (get-buffer name))
2722 (set-window-dedicated-p (selected-window) t))
2724 (defun gdb-setup-windows ()
2725 "Layout the window pattern for `gdb-many-windows'."
2726 (gdb-display-locals-buffer)
2727 (gdb-display-stack-buffer)
2728 (delete-other-windows)
2729 (gdb-display-breakpoints-buffer)
2730 (delete-other-windows)
2732 (pop-to-buffer gud-comint-buffer)
2733 (split-window nil ( / ( * (window-height) 3) 4))
2734 (split-window nil ( / (window-height) 3))
2735 (split-window-horizontally)
2737 (gdb-set-window-buffer (gdb-locals-buffer-name))
2740 (if gud-last-last-frame
2741 (gud-find-file (car gud-last-last-frame))
2742 (gud-find-file gdb-main-file)))
2743 (when gdb-use-separate-io-buffer
2744 (split-window-horizontally)
2746 (gdb-set-window-buffer
2747 (gdb-get-buffer-create 'gdb-inferior-io)))
2749 (gdb-set-window-buffer (gdb-stack-buffer-name))
2750 (split-window-horizontally)
2752 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2755 (defun gdb-restore-windows ()
2756 "Restore the basic arrangement of windows used by gdba.
2757 This arrangement depends on the value of `gdb-many-windows'."
2759 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2760 (delete-other-windows)
2761 (if gdb-many-windows
2763 (when (or gud-last-last-frame gdb-show-main)
2767 (if gud-last-last-frame
2768 (gud-find-file (car gud-last-last-frame))
2769 (gud-find-file gdb-main-file)))
2773 "Exit a debugging session cleanly.
2774 Kills the gdb buffers, and resets variables and the source buffers."
2775 (dolist (buffer (buffer-list))
2776 (unless (eq buffer gud-comint-buffer)
2777 (with-current-buffer buffer
2778 (if (memq gud-minor-mode '(gdbmi gdba))
2779 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2781 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2782 (setq gud-minor-mode nil)
2783 (kill-local-variable 'tool-bar-map)
2784 (kill-local-variable 'gdb-define-alist))))))
2785 (when (markerp gdb-overlay-arrow-position)
2786 (move-marker gdb-overlay-arrow-position nil)
2787 (setq gdb-overlay-arrow-position nil))
2788 (setq overlay-arrow-variable-list
2789 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2790 (if (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
2792 (setq gud-running nil)
2793 (setq gdb-active-process nil)
2794 (setq gdb-var-list nil)
2795 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2797 (defun gdb-source-info ()
2798 "Find the source file where the program starts and displays it with related
2800 (goto-char (point-min))
2801 (if (and (search-forward "Located in " nil t)
2802 (looking-at "\\S-+"))
2803 (setq gdb-main-file (match-string 0)))
2804 (goto-char (point-min))
2805 (if (search-forward "Includes preprocessor macro info." nil t)
2806 (setq gdb-macro-info t))
2807 (if gdb-many-windows
2809 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2811 (let ((pop-up-windows t))
2812 (display-buffer (gud-find-file gdb-main-file))))))
2814 (defun gdb-get-location (bptno line flag)
2815 "Find the directory containing the relevant source file.
2816 Put in buffer and place breakpoint icon."
2817 (goto-char (point-min))
2818 (catch 'file-not-found
2819 (if (search-forward "Located in " nil t)
2820 (when (looking-at "\\S-+")
2821 (delete (cons bptno "File not found") gdb-location-alist)
2822 (push (cons bptno (match-string 0)) gdb-location-alist))
2824 (unless (assoc bptno gdb-location-alist)
2825 (push (cons bptno "File not found") gdb-location-alist)
2826 (message-box "Cannot find source file for breakpoint location.\n\
2827 Add directory to search path for source files using the GDB command, dir."))
2828 (throw 'file-not-found nil))
2829 (with-current-buffer
2830 (find-file-noselect (match-string 0))
2831 (save-current-buffer
2832 (set (make-local-variable 'gud-minor-mode) 'gdba)
2833 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2834 ;; only want one breakpoint icon at each location
2836 (goto-line (string-to-number line))
2837 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2839 (add-hook 'find-file-hook 'gdb-find-file-hook)
2841 (defun gdb-find-file-hook ()
2842 "Set up buffer for debugging if file is part of the source code
2843 of the current session."
2844 (if (and (buffer-name gud-comint-buffer)
2845 ;; in case gud or gdb-ui is just loaded
2847 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2849 (if (member buffer-file-name gdb-source-file-list)
2850 (with-current-buffer (find-buffer-visiting buffer-file-name)
2851 (set (make-local-variable 'gud-minor-mode)
2852 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2853 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2856 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2857 "Put string PUTSTRING in front of POS in the current buffer.
2858 PUTSTRING is displayed by putting an overlay into the current buffer with a
2859 `before-string' string that has a `display' property whose value is
2861 (let ((string (make-string 1 ?x))
2862 (buffer (current-buffer)))
2863 (setq putstring (copy-sequence putstring))
2864 (let ((overlay (make-overlay pos pos buffer))
2866 (list (list 'margin 'left-margin) putstring))))
2867 (put-text-property 0 1 'display prop string)
2869 (add-text-properties 0 1 sprops string))
2870 (overlay-put overlay 'put-break t)
2871 (overlay-put overlay 'before-string string))))
2873 ;;from remove-images
2874 (defun gdb-remove-strings (start end &optional buffer)
2875 "Remove strings between START and END in BUFFER.
2876 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2877 BUFFER nil or omitted means use the current buffer."
2879 (setq buffer (current-buffer)))
2880 (dolist (overlay (overlays-in start end))
2881 (when (overlay-get overlay 'put-break)
2882 (delete-overlay overlay))))
2884 (defun gdb-put-breakpoint-icon (enabled bptno)
2885 (let ((start (- (line-beginning-position) 1))
2886 (end (+ (line-end-position) 1))
2887 (putstring (if enabled "B" "b"))
2888 (source-window (get-buffer-window (current-buffer) 0)))
2889 (add-text-properties
2890 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2893 (add-text-properties
2894 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2895 (add-text-properties
2896 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2897 (gdb-remove-breakpoint-icons start end)
2898 (if (display-images-p)
2899 (if (>= (or left-fringe-width
2900 (if source-window (car (window-fringes source-window)))
2901 gdb-buffer-fringe-width) 8)
2904 `(left-fringe breakpoint
2907 'breakpoint-disabled))
2909 'gdb-enabled enabled)
2910 (when (< left-margin-width 2)
2911 (save-current-buffer
2912 (setq left-margin-width 2)
2916 left-margin-width right-margin-width))))
2919 (or breakpoint-enabled-icon
2920 (setq breakpoint-enabled-icon
2921 (find-image `((:type xpm :data
2922 ,breakpoint-xpm-data
2923 :ascent 100 :pointer hand)
2925 ,breakpoint-enabled-pbm-data
2926 :ascent 100 :pointer hand)))))
2927 (or breakpoint-disabled-icon
2928 (setq breakpoint-disabled-icon
2929 (find-image `((:type xpm :data
2930 ,breakpoint-xpm-data
2931 :conversion disabled
2932 :ascent 100 :pointer hand)
2934 ,breakpoint-disabled-pbm-data
2935 :ascent 100 :pointer hand))))))
2939 (when (< left-margin-width 2)
2940 (save-current-buffer
2941 (setq left-margin-width 2)
2942 (let ((window (get-buffer-window (current-buffer) 0)))
2945 window left-margin-width right-margin-width)))))
2947 (propertize putstring
2948 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2951 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2952 (gdb-remove-strings start end)
2953 (if (display-images-p)
2954 (remove-images start end))
2956 (setq left-margin-width 0)
2957 (let ((window (get-buffer-window (current-buffer) 0)))
2960 window left-margin-width right-margin-width)))))
2964 ;; Assembler buffer.
2966 (gdb-set-buffer-rules 'gdb-assembler-buffer
2967 'gdb-assembler-buffer-name
2968 'gdb-assembler-mode)
2970 (def-gdb-auto-update-handler gdb-assembler-handler
2971 gdb-invalidate-assembler
2972 gdb-assembler-buffer
2973 gdb-assembler-custom)
2975 (defun gdb-assembler-custom ()
2976 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2977 (pos 1) (address) (flag) (bptno))
2978 (with-current-buffer buffer
2980 (if (not (equal gdb-frame-address "main"))
2982 (goto-char (point-min))
2983 (if (and gdb-frame-address
2984 (search-forward gdb-frame-address nil t))
2988 (or gdb-overlay-arrow-position
2989 (setq gdb-overlay-arrow-position (make-marker)))
2990 (set-marker gdb-overlay-arrow-position
2991 (point) (current-buffer))))))
2992 ;; remove all breakpoint-icons in assembler buffer before updating.
2993 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2994 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2995 (goto-char (point-min))
2996 (while (< (point) (- (point-max) 1))
2998 (if (looking-at "[^\t].*?breakpoint")
3001 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3002 (setq bptno (match-string 1))
3003 (setq flag (char-after (match-beginning 2)))
3004 (setq address (match-string 3))
3005 (with-current-buffer buffer
3007 (goto-char (point-min))
3008 (if (search-forward address nil t)
3009 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3010 (if (not (equal gdb-frame-address "main"))
3011 (with-current-buffer buffer
3012 (set-window-point (get-buffer-window buffer 0) pos)))))
3014 (defvar gdb-assembler-mode-map
3015 (let ((map (make-sparse-keymap)))
3016 (suppress-keymap map)
3017 (define-key map "q" 'kill-this-buffer)
3020 (defvar gdb-assembler-font-lock-keywords
3021 '(;; <__function.name+n>
3022 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3023 (1 font-lock-function-name-face))
3024 ;; 0xNNNNNNNN <__function.name+n>: opcode
3025 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3026 (4 font-lock-keyword-face))
3027 ;; %register(at least i386)
3028 ("%\\sw+" . font-lock-variable-name-face)
3029 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3030 (1 font-lock-comment-face)
3031 (2 font-lock-function-name-face))
3032 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3033 "Font lock keywords used in `gdb-assembler-mode'.")
3035 (defun gdb-assembler-mode ()
3036 "Major mode for viewing code assembler.
3038 \\{gdb-assembler-mode-map}"
3039 (kill-all-local-variables)
3040 (setq major-mode 'gdb-assembler-mode)
3041 (setq mode-name (concat "Machine:" gdb-selected-frame))
3042 (setq gdb-overlay-arrow-position nil)
3043 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3044 (setq fringes-outside-margins t)
3045 (setq buffer-read-only t)
3046 (use-local-map gdb-assembler-mode-map)
3047 (gdb-invalidate-assembler)
3048 (set (make-local-variable 'font-lock-defaults)
3049 '(gdb-assembler-font-lock-keywords))
3050 (run-mode-hooks 'gdb-assembler-mode-hook)
3051 'gdb-invalidate-assembler)
3053 (defun gdb-assembler-buffer-name ()
3054 (with-current-buffer gud-comint-buffer
3055 (concat "*disassembly of " (gdb-get-target-string) "*")))
3057 (defun gdb-display-assembler-buffer ()
3058 "Display disassembly view."
3060 (setq gdb-previous-frame nil)
3062 (gdb-get-buffer-create 'gdb-assembler-buffer)))
3064 (defun gdb-frame-assembler-buffer ()
3065 "Display disassembly view in a new frame."
3067 (setq gdb-previous-frame nil)
3068 (let ((special-display-regexps (append special-display-regexps '(".*")))
3069 (special-display-frame-alist gdb-frame-parameters))
3070 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3072 ;; modified because if gdb-frame-address has changed value a new command
3073 ;; must be enqueued to update the buffer with the new output
3074 (defun gdb-invalidate-assembler (&optional ignored)
3075 (if (gdb-get-buffer 'gdb-assembler-buffer)
3077 (unless (and gdb-selected-frame
3078 (string-equal gdb-selected-frame gdb-previous-frame))
3079 (if (or (not (member 'gdb-invalidate-assembler
3080 gdb-pending-triggers))
3081 (not (string-equal gdb-frame-address
3082 gdb-previous-frame-address)))
3084 ;; take previous disassemble command, if any, off the queue
3085 (with-current-buffer gud-comint-buffer
3086 (let ((queue gdb-input-queue))
3087 (dolist (item queue)
3088 (if (equal (cdr item) '(gdb-assembler-handler))
3089 (setq gdb-input-queue
3090 (delete item gdb-input-queue))))))
3093 (concat gdb-server-prefix "disassemble "
3094 (if (member gdb-frame-address '(nil "main")) nil "0x")
3095 gdb-frame-address "\n")
3096 'gdb-assembler-handler))
3097 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3098 (setq gdb-previous-frame-address gdb-frame-address)
3099 (setq gdb-previous-frame gdb-selected-frame)))))))
3101 (defun gdb-get-selected-frame ()
3102 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3105 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3106 (push 'gdb-get-selected-frame
3107 gdb-pending-triggers))))
3109 (defun gdb-frame-handler ()
3110 (setq gdb-pending-triggers
3111 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3112 (goto-char (point-min))
3113 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
3114 (setq gdb-frame-number (match-string 1)))
3115 (goto-char (point-min))
3116 (if (re-search-forward
3117 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
3119 (setq gdb-selected-frame (match-string 2))
3120 (if (gdb-get-buffer 'gdb-locals-buffer)
3121 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3122 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3123 (if (gdb-get-buffer 'gdb-assembler-buffer)
3124 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3125 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3126 (setq gdb-frame-address (match-string 1))))
3127 (goto-char (point-min))
3128 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
3129 (setq gdb-current-language (match-string 1)))
3130 (gdb-invalidate-assembler))
3133 ;; Code specific to GDB 6.4
3134 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3136 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3137 "Create list of source files for current GDB session.
3138 If buffers already exist for any of these files, gud-minor-mode
3140 (goto-char (point-min))
3141 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3142 (push (match-string 1) gdb-source-file-list))
3143 (dolist (buffer (buffer-list))
3144 (with-current-buffer buffer
3145 (when (member buffer-file-name gdb-source-file-list)
3146 (set (make-local-variable 'gud-minor-mode)
3147 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3148 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3149 (when gud-tooltip-mode
3150 (make-local-variable 'gdb-define-alist)
3151 (gdb-create-define-alist)
3152 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
3154 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3155 (defun gdb-var-list-children-1 (varnum)
3158 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3159 (concat "server interpreter mi \"-var-list-children --all-values "
3161 (concat "-var-list-children --all-values " varnum "\n"))
3162 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3164 (defconst gdb-var-list-children-regexp-1
3165 "name=\"\\(.+?\\)\",exp=\"\\(.+?\\)\",numchild=\"\\(.+?\\)\",\
3166 value=\\(\".*?\"\\),type=\"\\(.+?\\)\"}")
3168 (defun gdb-var-list-children-handler-1 (varnum)
3169 (goto-char (point-min))
3170 (let ((var-list nil))
3171 (catch 'child-already-watched
3172 (dolist (var gdb-var-list)
3173 (if (string-equal varnum (car var))
3176 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3177 (let ((varchild (list (match-string 1)
3181 (read (match-string 4))
3183 (if (assoc (car varchild) gdb-var-list)
3184 (throw 'child-already-watched nil))
3185 (push varchild var-list))))
3186 (push var var-list)))
3187 (setq gdb-var-list (nreverse var-list)))))
3189 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3190 (defun gdb-var-update-1 ()
3191 (if (not (member 'gdb-var-update gdb-pending-triggers))
3195 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3196 "server interpreter mi \"-var-update --all-values *\"\n"
3197 "-var-update --all-values *\n")
3198 'gdb-var-update-handler-1))
3199 (push 'gdb-var-update gdb-pending-triggers))))
3201 (defconst gdb-var-update-regexp-1
3202 "name=\"\\(.*?\\)\",\\(?:value=\\(\".*?\"\\),\\)?in_scope=\"\\(.*?\\)\"")
3204 (defun gdb-var-update-handler-1 ()
3205 (dolist (var gdb-var-list)
3206 (setcar (nthcdr 5 var) nil))
3207 (goto-char (point-min))
3208 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3209 (let* ((varnum (match-string 1))
3210 (var (assoc varnum gdb-var-list)))
3212 (if (string-equal (match-string 3) "false")
3213 (setcar (nthcdr 5 var) 'out-of-scope)
3214 (setcar (nthcdr 5 var) 'changed)
3215 (setcar (nthcdr 4 var)
3216 (read (match-string 2)))))))
3217 (setq gdb-pending-triggers
3218 (delq 'gdb-var-update gdb-pending-triggers))
3219 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3220 ;; dummy command to update speedbar at right time
3221 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
3222 ;; keep gdb-pending-triggers non-nil till end
3223 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
3225 ;; Registers buffer.
3227 (gdb-set-buffer-rules 'gdb-registers-buffer
3228 'gdb-registers-buffer-name
3229 'gdb-registers-mode)
3231 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3232 (gdb-get-buffer 'gdb-registers-buffer)
3233 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3234 "server interpreter mi \"-data-list-register-values x\"\n"
3235 "-data-list-register-values x\n")
3236 gdb-data-list-register-values-handler)
3238 (defconst gdb-data-list-register-values-regexp
3239 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
3241 (defun gdb-data-list-register-values-handler ()
3242 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3243 gdb-pending-triggers))
3244 (goto-char (point-min))
3245 (if (re-search-forward gdb-error-regexp nil t)
3246 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3247 (let ((buffer-read-only nil))
3249 (insert (match-string 1))
3250 (goto-char (point-min))))
3251 (let ((register-list (reverse gdb-register-names))
3252 (register nil) (register-string nil) (register-values nil))
3253 (goto-char (point-min))
3254 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3255 (setq register (pop register-list))
3256 (setq register-string (concat register "\t" (match-string 2) "\n"))
3257 (if (member (match-string 1) gdb-changed-registers)
3258 (put-text-property 0 (length register-string)
3259 'face 'font-lock-warning-face
3261 (setq register-values
3262 (concat register-values register-string)))
3263 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3264 (with-current-buffer buf
3265 (let* ((window (get-buffer-window buf 0))
3266 (start (window-start window))
3267 (p (window-point window))
3268 (buffer-read-only nil))
3270 (insert register-values)
3271 (set-window-start window start)
3272 (set-window-point window p))))))
3273 (gdb-data-list-register-values-custom))
3275 (defun gdb-data-list-register-values-custom ()
3276 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3278 (let ((buffer-read-only nil)
3280 (goto-char (point-min))
3281 (while (< (point) (point-max))
3282 (setq start (line-beginning-position))
3283 (setq end (line-end-position))
3284 (when (looking-at "^[^\t]+")
3285 (unless (string-equal (match-string 0) "No registers.")
3286 (put-text-property start (match-end 0)
3287 'face font-lock-variable-name-face)
3288 (add-text-properties start end
3289 '(help-echo "mouse-2: edit value"
3290 mouse-face highlight))))
3291 (forward-line 1))))))
3293 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3294 (defun gdb-get-changed-registers ()
3295 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3296 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3300 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3301 "server interpreter mi -data-list-changed-registers\n"
3302 "-data-list-changed-registers\n")
3303 'gdb-get-changed-registers-handler))
3304 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3306 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3308 (defun gdb-get-changed-registers-handler ()
3309 (setq gdb-pending-triggers
3310 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3311 (setq gdb-changed-registers nil)
3312 (goto-char (point-min))
3313 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3314 (push (match-string 1) gdb-changed-registers)))
3319 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3320 (gdb-set-buffer-rules 'gdb-locals-buffer
3321 'gdb-locals-buffer-name
3324 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3325 (gdb-get-buffer 'gdb-locals-buffer)
3326 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3327 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3328 "-stack-list-locals --simple-values\n")
3329 gdb-stack-list-locals-handler)
3331 (defconst gdb-stack-list-locals-regexp
3332 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3334 (defvar gdb-locals-watch-map-1
3335 (let ((map (make-sparse-keymap)))
3336 (define-key map [mouse-2] 'gud-watch)
3338 "Keymap to create watch expression of a complex data type local variable.")
3340 ;; Dont display values of arrays or structures.
3341 ;; These can be expanded using gud-watch.
3342 (defun gdb-stack-list-locals-handler ()
3343 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3344 gdb-pending-triggers))
3345 (let (local locals-list)
3346 (goto-char (point-min))
3347 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3348 (let ((local (list (match-string 1)
3351 (if (looking-at ",value=\\(\".*\"\\)}")
3352 (setcar (nthcdr 2 local) (read (match-string 1))))
3353 (push local locals-list)))
3354 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3355 (and buf (with-current-buffer buf
3356 (let* ((window (get-buffer-window buf 0))
3357 (start (window-start window))
3358 (p (window-point window))
3359 (buffer-read-only nil))
3361 (dolist (local locals-list)
3362 (setq name (car local))
3363 (if (or (not (nth 2 local))
3364 (string-match "\\0x" (nth 2 local)))
3365 (add-text-properties 0 (length name)
3366 `(mouse-face highlight
3367 help-echo "mouse-2: create watch expression"
3368 local-map ,gdb-locals-watch-map-1)
3371 (concat name "\t" (nth 1 local)
3372 "\t" (nth 2 local) "\n")))
3373 (set-window-start window start)
3374 (set-window-point window p)))))))
3376 (defun gdb-get-register-names ()
3377 "Create a list of register names."
3378 (goto-char (point-min))
3379 (setq gdb-register-names nil)
3380 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3381 (push (match-string 1) gdb-register-names)))
3385 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3386 ;;; gdb-ui.el ends here