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:
81 ;; 1) They go out of scope when the inferior is re-run.
82 ;; 2) -stack-list-locals has a type field but also prints type in values field.
83 ;; 3) VARNUM increments even when variable object is not created
87 ;; 1) M-x gdb doesn't work with "run" command in .gdbinit, use M-x gdba instead.
90 ;; 1) Use MI command -data-read-memory for memory window.
91 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
92 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
93 ;; 4) With gud-print and gud-pstar, print the variable name in the GUD
94 ;; buffer instead of the value's history number.
100 (defvar tool-bar-map)
101 (defvar speedbar-initial-expansion-list-name)
103 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
104 (defvar gdb-previous-frame-address nil)
105 (defvar gdb-memory-address "main")
106 (defvar gdb-previous-frame nil)
107 (defvar gdb-selected-frame nil)
108 (defvar gdb-frame-number nil)
109 (defvar gdb-current-language nil)
110 (defvar gdb-var-list nil
111 "List of variables in watch window.
112 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS) where
113 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
114 (defvar gdb-force-update t
115 "Non-nil means that view of watch expressions will be updated in the speedbar.")
116 (defvar gdb-main-file nil "Source file from which program execution begins.")
117 (defvar gdb-overlay-arrow-position nil)
118 (defvar gdb-server-prefix nil)
119 (defvar gdb-flush-pending-output nil)
120 (defvar gdb-location-alist nil
121 "Alist of breakpoint numbers and full filenames.")
122 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
123 and #define directives otherwise.")
124 (defvar gdb-error "Non-nil when GDB is reporting an error.")
125 (defvar gdb-macro-info nil
126 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
127 (defvar gdb-buffer-fringe-width nil)
128 (defvar gdb-signalled nil)
129 (defvar gdb-source-window nil)
130 (defvar gdb-inferior-status nil)
131 (defvar gdb-continuation nil)
133 (defvar gdb-buffer-type nil
134 "One of the symbols bound in `gdb-buffer-rules'.")
135 (make-variable-buffer-local 'gdb-buffer-type)
137 (defvar gdb-input-queue ()
138 "A list of gdb command objects.")
140 (defvar gdb-prompting nil
141 "True when gdb is idle with no pending input.")
143 (defvar gdb-output-sink 'user
144 "The disposition of the output of the current gdb command.
145 Possible values are these symbols:
147 `user' -- gdb output should be copied to the GUD buffer
150 `inferior' -- gdb output should be copied to the inferior-io buffer.
152 `pre-emacs' -- output should be ignored util the post-prompt
153 annotation is received. Then the output-sink
155 `emacs' -- output should be collected in the partial-output-buffer
156 for subsequent processing by a command. This is the
157 disposition of output generated by commands that
158 gdb mode sends to gdb on its own behalf.
159 `post-emacs' -- ignore output until the prompt annotation is
160 received, then go to USER disposition.
162 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
163 \(`user' and `emacs').")
165 (defvar gdb-current-item nil
166 "The most recent command item sent to gdb.")
168 (defvar gdb-pending-triggers '()
169 "A list of trigger functions that have run later than their output
172 (defvar gdb-first-post-prompt nil)
173 (defvar gdb-version nil)
174 (defvar gdb-locals-font-lock-keywords nil)
175 (defvar gdb-source-file-list nil
176 "List of source files for the current executable")
177 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
179 (defvar gdb-locals-font-lock-keywords-1
181 ;; var = (struct struct_tag) value
182 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
183 (1 font-lock-variable-name-face)
184 (3 font-lock-keyword-face)
185 (4 font-lock-type-face))
186 ;; var = (type) value
187 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
188 (1 font-lock-variable-name-face)
189 (3 font-lock-type-face))
191 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
192 (1 font-lock-variable-name-face))
194 "Font lock keywords used in `gdb-local-mode'.")
196 (defvar gdb-locals-font-lock-keywords-2
199 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
200 (1 font-lock-variable-name-face)
201 (3 font-lock-type-face))
203 "Font lock keywords used in `gdb-local-mode'.")
205 ;; Variables for GDB 6.4+
206 (defvar gdb-register-names nil "List of register names.")
207 (defvar gdb-changed-registers nil
208 "List of changed register numbers (strings).")
211 (defun gdba (command-line)
212 "Run gdb on program FILE in buffer *gud-FILE*.
213 The directory containing FILE becomes the initial working directory
214 and source-file directory for your debugger.
216 If `gdb-many-windows' is nil (the default value) then gdb just
217 pops up the GUD buffer unless `gdb-show-main' is t. In this case
218 it starts with two windows: one displaying the GUD buffer and the
219 other with the source file with the main routine of the inferior.
221 If `gdb-many-windows' is t, regardless of the value of
222 `gdb-show-main', the layout below will appear unless
223 `gdb-use-separate-io-buffer' is nil when the source buffer
224 occupies the full width of the frame. Keybindings are shown in
227 Watch expressions appear in the speedbar/slowbar.
229 The following commands help control operation :
231 `gdb-many-windows' - Toggle the number of windows gdb uses.
232 `gdb-restore-windows' - To restore the window layout.
234 See Info node `(emacs)GDB Graphical Interface' for a more
235 detailed description of this mode.
238 +----------------------------------------------------------------------+
240 +-----------------------------------+----------------------------------+
241 | GUD buffer (I/O of GDB) | Locals buffer |
245 +-----------------------------------+----------------------------------+
246 | Source buffer | I/O buffer (of debugged program) |
254 +-----------------------------------+----------------------------------+
255 | Stack buffer | Breakpoints buffer |
256 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
257 | | RET gdb-goto-breakpoint |
258 | | D gdb-delete-breakpoint |
259 +-----------------------------------+----------------------------------+"
261 (interactive (list (gud-query-cmdline 'gdba)))
263 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
267 (defcustom gdb-debug-ring-max 128
268 "Maximum size of `gdb-debug-ring'."
273 (defvar gdb-debug-ring nil
274 "List of commands, most recent first, sent to and replies received from GDB.
275 This variable is used to debug GDB-UI.")
278 (defcustom gdb-enable-debug nil
279 "Non-nil means record the process input and output in `gdb-debug-ring'."
284 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
285 "Shell command for generating a list of defined macros in a source file.
286 This list is used to display the #define directive associated
287 with an identifier as a tooltip. It works in a debug session with
288 GDB, when gud-tooltip-mode is t.
290 Set `gdb-cpp-define-alist-flags' for any include paths or
296 (defcustom gdb-cpp-define-alist-flags ""
297 "Preprocessor flags for `gdb-cpp-define-alist-program'."
302 (defcustom gdb-show-main nil
303 "Non-nil means display source file containing the main routine at startup.
304 Also display the main routine in the disassembly buffer if present."
309 (defcustom gdb-many-windows nil
310 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
311 In this case it starts with two windows: one displaying the GUD
312 buffer and the other with the source file with the main routine
313 of the inferior. Non-nil means display the layout shown for
319 (defcustom gdb-use-separate-io-buffer nil
320 "Non-nil means display output from the inferior in a separate buffer."
325 (defun gdb-force-mode-line-update (status)
326 (let ((buffer gud-comint-buffer))
327 (if (and buffer (buffer-name buffer))
328 (with-current-buffer buffer
329 (setq mode-line-process
331 (process-status (get-buffer-process buffer)) status))
332 ;; Force mode line redisplay soon.
333 (force-mode-line-update)))))
335 (defun gdb-many-windows (arg)
336 "Toggle the number of windows in the basic arrangement.
337 With arg, display additional buffers iff arg is positive."
339 (setq gdb-many-windows
341 (not gdb-many-windows)
342 (> (prefix-numeric-value arg) 0)))
343 (message (format "Display of other windows %sabled"
344 (if gdb-many-windows "en" "dis")))
345 (if (and gud-comint-buffer
346 (buffer-name gud-comint-buffer))
348 (gdb-restore-windows)
351 (defun gdb-use-separate-io-buffer (arg)
352 "Toggle separate IO for inferior.
353 With arg, use separate IO iff arg is positive."
355 (setq gdb-use-separate-io-buffer
357 (not gdb-use-separate-io-buffer)
358 (> (prefix-numeric-value arg) 0)))
359 (message (format "Separate inferior IO %sabled"
360 (if gdb-use-separate-io-buffer "en" "dis")))
361 (if (and gud-comint-buffer
362 (buffer-name gud-comint-buffer))
364 (if gdb-use-separate-io-buffer
365 (if gdb-many-windows (gdb-restore-windows))
366 (kill-buffer (gdb-inferior-io-name)))
369 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
371 (defun gdb-create-define-alist ()
372 "Create an alist of #define directives for GUD tooltips."
373 (let* ((file (buffer-file-name))
375 (with-output-to-string
376 (with-current-buffer standard-output
377 (call-process shell-file-name
378 (if (file-exists-p file) file nil)
379 (list t nil) nil "-c"
380 (concat gdb-cpp-define-alist-program " "
381 gdb-cpp-define-alist-flags)))))
382 (define-list (split-string output "\n" t))
384 (setq gdb-define-alist nil)
385 (dolist (define define-list)
386 (setq name (nth 1 (split-string define "[( ]")))
387 (push (cons name define) gdb-define-alist))))
389 (defun gdb-tooltip-print (expr)
391 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
392 (goto-char (point-min))
394 (if (search-forward "=" nil t)
395 (concat expr (buffer-substring (- (point) 2) (point-max)))
397 ;; remove newline for gud-tooltip-echo-area
398 (substring string 0 (- (length string) 1))))
399 (or gud-tooltip-echo-area tooltip-use-echo-area)))
401 ;; If expr is a macro for a function don't print because of possible dangerous
402 ;; side-effects. Also printing a function within a tooltip generates an
403 ;; unexpected starting annotation (phase error).
404 (defun gdb-tooltip-print-1 (expr)
405 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
406 (goto-char (point-min))
407 (if (search-forward "expands to: " nil t)
408 (unless (looking-at "\\S-+.*(.*).*")
410 (list (concat gdb-server-prefix "print " expr "\n")
411 `(lambda () (gdb-tooltip-print ,expr))))))))
413 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
415 (defun gdb-set-gud-minor-mode-existing-buffers ()
416 "Create list of source files for current GDB session."
417 (goto-char (point-min))
418 (when (search-forward "read in on demand:" nil t)
419 (while (re-search-forward gdb-source-file-regexp nil t)
420 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
421 gdb-source-file-list))
422 (dolist (buffer (buffer-list))
423 (with-current-buffer buffer
424 (when (and buffer-file-name
425 (member (file-name-nondirectory buffer-file-name)
426 gdb-source-file-list))
427 (set (make-local-variable 'gud-minor-mode) 'gdba)
428 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
429 (when gud-tooltip-mode
430 (make-local-variable 'gdb-define-alist)
431 (gdb-create-define-alist)
432 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
434 (defun gdb-find-watch-expression ()
435 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
436 (varnum (car var)) expr array)
437 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
438 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
439 (component-list (split-string (match-string 2 varnum) "\\." t)))
440 (setq expr (nth 1 var1))
441 (setq varnumlet (car var1))
442 (dolist (component component-list)
443 (setq var2 (assoc varnumlet gdb-var-list))
444 (setq expr (concat expr
445 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
446 (concat "[" component "]")
447 (concat "." component))))
448 (setq varnumlet (concat varnumlet "." component)))
452 (set (make-local-variable 'gud-minor-mode) 'gdba)
453 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
455 (gud-def gud-break (if (not (string-match "Machine" mode-name))
456 (gud-call "break %f:%l" arg)
460 (gud-call "break *%a" arg)))
461 "\C-b" "Set breakpoint at current line or address.")
463 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
464 (gud-call "clear %f:%l" arg)
468 (gud-call "clear *%a" arg)))
469 "\C-d" "Remove breakpoint at current line or address.")
471 (gud-def gud-until (if (not (string-match "Machine" mode-name))
472 (gud-call "until %f:%l" arg)
476 (gud-call "until *%a" arg)))
477 "\C-u" "Continue to current line or address.")
479 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
480 nil "Start or continue execution.")
482 ;; For debugging Emacs only.
486 "pp1 " (if (eq (buffer-local-value
487 'major-mode (window-buffer)) 'speedbar-mode)
488 (gdb-find-watch-expression) "%e")) arg)
489 nil "Print the emacs s-expression.")
491 (define-key gud-minor-mode-map [left-margin mouse-1]
492 'gdb-mouse-set-clear-breakpoint)
493 (define-key gud-minor-mode-map [left-fringe mouse-1]
494 'gdb-mouse-set-clear-breakpoint)
495 (define-key gud-minor-mode-map [left-fringe mouse-2]
497 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
499 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
501 (define-key gud-minor-mode-map [left-margin mouse-2]
503 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
505 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
507 (define-key gud-minor-mode-map [left-fringe C-mouse-2]
509 (define-key gud-minor-mode-map [left-margin C-mouse-2]
511 (define-key gud-minor-mode-map [left-margin mouse-3]
512 'gdb-mouse-toggle-breakpoint-margin)
513 (define-key gud-minor-mode-map [left-fringe mouse-3]
514 'gdb-mouse-toggle-breakpoint-fringe)
516 (setq comint-input-sender 'gdb-send)
519 (setq gdb-frame-address (if gdb-show-main "main" nil))
520 (setq gdb-previous-frame-address nil
521 gdb-memory-address "main"
522 gdb-previous-frame nil
523 gdb-selected-frame nil
524 gdb-current-language nil
529 gdb-first-post-prompt t
533 gdb-pending-triggers nil
534 gdb-output-sink 'user
535 gdb-server-prefix "server "
536 gdb-flush-pending-output nil
537 gdb-location-alist nil
538 gdb-source-file-list nil
541 gdb-buffer-fringe-width (car (window-fringes))
544 gdb-source-window nil
545 gdb-inferior-status nil
546 gdb-continuation nil)
548 (setq gdb-buffer-type 'gdba)
550 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
552 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
553 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
557 (if (eq window-system 'w32)
558 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
559 (gdb-enqueue-input (list "set height 0\n" 'ignore))
560 (gdb-enqueue-input (list "set width 0\n" 'ignore))
562 (if (string-equal gdb-version "pre-6.4")
564 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
565 'gdb-set-gud-minor-mode-existing-buffers))
566 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
568 (list "server interpreter mi -data-list-register-names\n"
569 'gdb-get-register-names))
570 ; Needs GDB 6.2 onwards.
572 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
573 'gdb-set-gud-minor-mode-existing-buffers-1))
574 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
576 ;; Find source file and compilation directory here.
577 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
578 (gdb-enqueue-input (list "server list\n" 'ignore))
579 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
581 (run-hooks 'gdba-mode-hook))
583 (defun gdb-get-version ()
584 (goto-char (point-min))
585 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
586 (setq gdb-version "pre-6.4")
587 (setq gdb-version "6.4+"))
590 (defun gdb-mouse-until (event)
591 "Continue running until a source line past the current line.
592 The destination source line can be selected either by clicking with mouse-2
593 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
595 (if gud-overlay-arrow-position
596 (let ((start (event-start event))
597 (end (event-end event))
598 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
599 (if (not (string-match "Machine" mode-name))
600 (if (equal buffer (window-buffer (posn-window end)))
601 (with-current-buffer buffer
602 (when (or (equal start end)
603 (equal (posn-point start)
605 gud-overlay-arrow-position)))
606 (setq line (line-number-at-pos (posn-point end)))
607 (gud-call (concat "until " (number-to-string line))))))
608 (if (equal (marker-buffer gdb-overlay-arrow-position)
609 (window-buffer (posn-window end)))
610 (when (or (equal start end)
611 (equal (posn-point start)
613 gdb-overlay-arrow-position)))
615 (goto-line (line-number-at-pos (posn-point end)))
617 (gud-call (concat "until *%a")))))))))
619 (defun gdb-mouse-jump (event)
620 "Set execution address/line.
621 The destination source line can be selected either by clicking with mouse-2
622 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
623 Unlike gdb-mouse-until the destination address can be before the current
624 line, and no execution takes place."
626 (if gud-overlay-arrow-position
627 (let ((start (event-start event))
628 (end (event-end event))
629 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
630 (if (not (string-match "Machine" mode-name))
631 (if (equal buffer (window-buffer (posn-window end)))
632 (with-current-buffer buffer
633 (when (or (equal start end)
634 (equal (posn-point start)
636 gud-overlay-arrow-position)))
637 (setq line (line-number-at-pos (posn-point end)))
638 (progn (gud-call (concat "tbreak " (number-to-string line)))
639 (gud-call (concat "jump " (number-to-string line)))))))
640 (if (equal (marker-buffer gdb-overlay-arrow-position)
641 (window-buffer (posn-window end)))
642 (when (or (equal start end)
643 (equal (posn-point start)
645 gdb-overlay-arrow-position)))
647 (goto-line (line-number-at-pos (posn-point end)))
650 (gud-call (concat "tbreak *%a"))
651 (gud-call (concat "jump *%a"))))))))))
653 (defcustom gdb-speedbar-auto-raise nil
654 "If non-nil raise speedbar every time display of watch expressions is\
660 (defun gdb-speedbar-auto-raise (arg)
661 "Toggle automatic raising of the speedbar for watch expressions.
662 With arg, automatically raise speedbar iff arg is positive."
664 (setq gdb-speedbar-auto-raise
666 (not gdb-speedbar-auto-raise)
667 (> (prefix-numeric-value arg) 0)))
668 (message (format "Auto raising %sabled"
669 (if gdb-speedbar-auto-raise "en" "dis"))))
671 (defcustom gdb-use-colon-colon-notation nil
672 "If non-nil use FUN::VAR format to display variables in the speedbar."
677 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
678 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
680 (defun gud-watch (&optional arg event)
681 "Watch expression at point.
682 With arg, enter name of variable to be watched in the minibuffer."
683 (interactive (list current-prefix-arg last-input-event))
684 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
685 (if (memq minor-mode '(gdbmi gdba))
687 (if event (posn-set-point (event-end event)))
689 (save-selected-window
691 (completing-read "Name of variable: "
692 'gud-gdb-complete-command)
693 (tooltip-identifier-from-point (point)))))
694 (catch 'already-watched
695 (dolist (var gdb-var-list)
696 (unless (string-match "\\." (car var))
697 (if (string-equal expr (nth 1 var))
698 (throw 'already-watched nil))))
699 (set-text-properties 0 (length expr) nil expr)
702 (if (eq minor-mode 'gdba)
704 "server interpreter mi \"-var-create - * " expr "\"\n")
705 (concat"-var-create - * " expr "\n"))
706 `(lambda () (gdb-var-create-handler ,expr))))))))
707 (message "gud-watch is a no-op in this mode."))))
709 (defconst gdb-var-create-regexp
710 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",.*type=\"\\(.*?\\)\"")
712 (defun gdb-var-create-handler (expr)
713 (goto-char (point-min))
714 (if (re-search-forward gdb-var-create-regexp nil t)
717 (if (and (string-equal gdb-current-language "c")
718 gdb-use-colon-colon-notation gdb-selected-frame)
719 (setq expr (concat gdb-selected-frame "::" expr))
724 (push var gdb-var-list)
726 (unless (string-equal
727 speedbar-initial-expansion-list-name "GUD")
728 (speedbar-change-initial-expansion-list "GUD"))
731 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
732 (concat "server interpreter mi \"-var-evaluate-expression "
734 (concat "-var-evaluate-expression " (car var) "\n"))
735 `(lambda () (gdb-var-evaluate-expression-handler
737 (if (search-forward "Undefined command" nil t)
738 (message-box "Watching expressions requires gdb 6.0 onwards")
739 (message-box "No symbol \"%s\" in current context." expr))))
741 (defun gdb-var-evaluate-expression-handler (varnum changed)
742 (goto-char (point-min))
743 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
744 (let ((var (assoc varnum gdb-var-list)))
746 (if changed (setcar (nthcdr 5 var) 'changed))
747 (setcar (nthcdr 4 var) (read (match-string 1))))))
749 (defun gdb-var-list-children (varnum)
751 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
752 `(lambda () (gdb-var-list-children-handler ,varnum)))))
754 (defconst gdb-var-list-children-regexp
755 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
756 numchild=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\".*?}")
758 (defun gdb-var-list-children-handler (varnum)
759 (goto-char (point-min))
760 (let ((var-list nil))
761 (catch 'child-already-watched
762 (dolist (var gdb-var-list)
763 (if (string-equal varnum (car var))
766 (while (re-search-forward gdb-var-list-children-regexp nil t)
767 (let ((varchild (list (match-string 1)
772 (if (assoc (car varchild) gdb-var-list)
773 (throw 'child-already-watched nil))
774 (push varchild var-list)
778 "server interpreter mi \"-var-evaluate-expression "
779 (car varchild) "\"\n")
780 `(lambda () (gdb-var-evaluate-expression-handler
781 ,(car varchild) nil)))))))
782 (push var var-list)))
783 (setq gdb-var-list (nreverse var-list)))))
785 (defun gdb-var-update ()
786 (when (not (member 'gdb-var-update gdb-pending-triggers))
788 (list "server interpreter mi \"-var-update *\"\n"
789 'gdb-var-update-handler))
790 (push 'gdb-var-update gdb-pending-triggers)))
792 (defconst gdb-var-update-regexp
793 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
794 type_changed=\".*?\".*?}")
796 (defun gdb-var-update-handler ()
797 (dolist (var gdb-var-list)
798 (setcar (nthcdr 5 var) nil))
799 (goto-char (point-min))
800 (while (re-search-forward gdb-var-update-regexp nil t)
801 (let ((varnum (match-string 1)))
802 (if (string-equal (match-string 2) "false")
803 (let ((var (assoc varnum gdb-var-list)))
804 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
807 (concat "server interpreter mi \"-var-evaluate-expression "
809 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
810 (setq gdb-pending-triggers
811 (delq 'gdb-var-update gdb-pending-triggers))
812 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
813 ;; Dummy command to update speedbar at right time.
814 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
815 ;; Keep gdb-pending-triggers non-nil till end.
816 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
818 (defun gdb-speedbar-refresh ()
819 (setq gdb-pending-triggers
820 (delq 'gdb-speedbar-refresh gdb-pending-triggers))
821 (with-current-buffer gud-comint-buffer
822 (let ((speedbar-verbosity-level 0)
823 (speedbar-shown-directories nil))
825 (speedbar-refresh)))))
827 (defun gdb-var-delete ()
828 "Delete watch expression at point from the speedbar."
830 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
832 (let ((text (speedbar-line-text)))
833 (string-match "\\(\\S-+\\)" text)
834 (let ((expr (match-string 1 text)) var varnum)
836 (dolist (var1 gdb-var-list)
837 (when (string-equal expr (nth 1 var1))
839 (setq varnum (car var1))
840 (throw 'expr-found nil))))
841 (unless (string-match "\\." (car var))
844 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
846 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
847 (concat "-var-delete " varnum "\n"))
849 (setq gdb-var-list (delq var gdb-var-list))
850 (dolist (varchild gdb-var-list)
851 (if (string-match (concat (car var) "\\.") (car varchild))
852 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
854 (defun gdb-edit-value (text token indent)
855 "Assign a value to a variable displayed in the speedbar."
856 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
857 (varnum (car var)) (value))
858 (setq value (read-string "New value: "))
861 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
862 (concat "server interpreter mi \"-var-assign "
863 varnum " " value "\"\n")
864 (concat "-var-assign " varnum " " value "\n"))
865 `(lambda () (gdb-edit-value-handler ,value))))))
867 (defun gdb-edit-value-handler (value)
868 (goto-char (point-min))
869 (if (re-search-forward gdb-error-regexp nil t)
870 (message-box "Invalid number or expression (%s)" value)))
872 (defcustom gdb-show-changed-values t
873 "If non-nil change the face of out of scope variables and changed values.
874 Out of scope variables are suppressed with `shadow' face.
875 Changed values are highlighted with the face `font-lock-warning-face'."
880 (defun gdb-speedbar-expand-node (text token indent)
881 "Expand the node the user clicked on.
882 TEXT is the text of the button we clicked on, a + or - item.
883 TOKEN is data related to this node.
884 INDENT is the current indentation depth."
885 (cond ((string-match "+" text) ;expand this node
887 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
888 (string-equal gdb-version "pre-6.4"))
889 (gdb-var-list-children token)
890 (gdb-var-list-children-1 token)))
891 ((string-match "-" text) ;contract this node
892 (dolist (var gdb-var-list)
893 (if (string-match (concat token "\\.") (car var))
894 (setq gdb-var-list (delq var gdb-var-list))))
895 (speedbar-change-expand-button-char ?+)
896 (speedbar-delete-subblock indent))
897 (t (error "Ooops... not sure what to do")))
898 (speedbar-center-buffer-smartly))
900 (defun gdb-get-target-string ()
901 (with-current-buffer gud-comint-buffer
908 ;; Each buffer has a TYPE -- a symbol that identifies the function
909 ;; of that particular buffer.
911 ;; The usual gdb interaction buffer is given the type `gdba' and
912 ;; is constructed specially.
914 ;; Others are constructed by gdb-get-buffer-create and
915 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
917 (defvar gdb-buffer-rules-assoc '())
919 (defun gdb-get-buffer (key)
920 "Return the gdb buffer tagged with type KEY.
921 The key should be one of the cars in `gdb-buffer-rules-assoc'."
923 (gdb-look-for-tagged-buffer key (buffer-list))))
925 (defun gdb-get-buffer-create (key)
926 "Create a new gdb buffer of the type specified by KEY.
927 The key should be one of the cars in `gdb-buffer-rules-assoc'."
928 (or (gdb-get-buffer key)
929 (let* ((rules (assoc key gdb-buffer-rules-assoc))
930 (name (funcall (gdb-rules-name-maker rules)))
931 (new (get-buffer-create name)))
932 (with-current-buffer new
934 (if (cdr (cdr rules))
935 (setq trigger (funcall (car (cdr (cdr rules))))))
936 (setq gdb-buffer-type key)
937 (set (make-local-variable 'gud-minor-mode)
938 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
939 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
940 (if trigger (funcall trigger)))
943 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
945 (defun gdb-look-for-tagged-buffer (key bufs)
947 (while (and (not retval) bufs)
948 (set-buffer (car bufs))
949 (if (eq gdb-buffer-type key)
950 (setq retval (car bufs)))
951 (setq bufs (cdr bufs)))
955 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
956 ;; at least one and possible more functions. The functions have these
957 ;; roles in defining a buffer type:
959 ;; NAME - Return a name for this buffer type.
961 ;; The remaining function(s) are optional:
963 ;; MODE - called in a new buffer with no arguments, should establish
964 ;; the proper mode for the buffer.
967 (defun gdb-set-buffer-rules (buffer-type &rest rules)
968 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
970 (setcdr binding rules)
971 (push (cons buffer-type rules)
972 gdb-buffer-rules-assoc))))
974 ;; GUD buffers are an exception to the rules
975 (gdb-set-buffer-rules 'gdba 'error)
977 ;; Partial-output buffer : This accumulates output from a command executed on
978 ;; behalf of emacs (rather than the user).
980 (gdb-set-buffer-rules 'gdb-partial-output-buffer
981 'gdb-partial-output-name)
983 (defun gdb-partial-output-name ()
984 (concat "*partial-output-"
985 (gdb-get-target-string)
989 (gdb-set-buffer-rules 'gdb-inferior-io
990 'gdb-inferior-io-name
991 'gdb-inferior-io-mode)
993 (defun gdb-inferior-io-name ()
994 (concat "*input/output of "
995 (gdb-get-target-string)
998 (defun gdb-display-separate-io-buffer ()
999 "Display IO of debugged program in a separate window."
1001 (if gdb-use-separate-io-buffer
1003 (gdb-get-buffer-create 'gdb-inferior-io))))
1005 (defconst gdb-frame-parameters
1006 '((height . 14) (width . 80)
1008 (tool-bar-lines . nil)
1009 (menu-bar-lines . nil)
1010 (minibuffer . nil)))
1012 (defun gdb-frame-separate-io-buffer ()
1013 "Display IO of inferior in a new frame."
1015 (if gdb-use-separate-io-buffer
1016 (let ((special-display-regexps (append special-display-regexps '(".*")))
1017 (special-display-frame-alist gdb-frame-parameters))
1018 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1020 (defvar gdb-inferior-io-mode-map
1021 (let ((map (make-sparse-keymap)))
1022 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1023 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1024 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1025 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1026 (define-key map "\C-d" 'gdb-separate-io-eof)
1029 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1030 "Major mode for gdb inferior-io."
1031 :syntax-table nil :abbrev-table nil
1032 ;; We want to use comint because it has various nifty and familiar
1033 ;; features. We don't need a process, but comint wants one, so create
1035 (make-comint-in-buffer
1036 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1037 (current-buffer) "hexl")
1038 (setq comint-input-sender 'gdb-inferior-io-sender))
1040 (defun gdb-inferior-io-sender (proc string)
1041 ;; PROC is the pseudo-process created to satisfy comint.
1042 (with-current-buffer (process-buffer proc)
1043 (setq proc (get-buffer-process gud-comint-buffer))
1044 (process-send-string proc string)
1045 (process-send-string proc "\n")))
1047 (defun gdb-separate-io-interrupt ()
1048 "Interrupt the program being debugged."
1051 (get-buffer-process gud-comint-buffer) comint-ptyp))
1053 (defun gdb-separate-io-quit ()
1054 "Send quit signal to the program being debugged."
1057 (get-buffer-process gud-comint-buffer) comint-ptyp))
1059 (defun gdb-separate-io-stop ()
1060 "Stop the program being debugged."
1063 (get-buffer-process gud-comint-buffer) comint-ptyp))
1065 (defun gdb-separate-io-eof ()
1066 "Send end-of-file to the program being debugged."
1069 (get-buffer-process gud-comint-buffer)))
1072 ;; gdb communications
1075 ;; INPUT: things sent to gdb
1077 ;; The queues are lists. Each element is either a string (indicating user or
1078 ;; user-like input) or a list of the form:
1080 ;; (INPUT-STRING HANDLER-FN)
1082 ;; The handler function will be called from the partial-output buffer when the
1083 ;; command completes. This is the way to write commands which invoke gdb
1084 ;; commands autonomously.
1086 ;; These lists are consumed tail first.
1089 (defun gdb-send (proc string)
1090 "A comint send filter for gdb.
1091 This filter may simply queue input for a later time."
1092 (with-current-buffer gud-comint-buffer
1093 (let ((inhibit-read-only t))
1094 (remove-text-properties (point-min) (point-max) '(face))))
1097 (let ((item (concat string "\n")))
1098 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1099 (process-send-string proc item)))
1100 (if (string-match "\\\\$" string)
1101 (setq gdb-continuation (concat gdb-continuation string "\n"))
1102 (let ((item (concat gdb-continuation string "\n")))
1103 (gdb-enqueue-input item)
1104 (setq gdb-continuation nil)))))
1106 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1107 ;; is a query, or other non-top-level prompt.
1109 (defun gdb-enqueue-input (item)
1110 (if (not gud-running)
1113 (gdb-send-item item)
1114 (setq gdb-prompting nil))
1115 (push item gdb-input-queue))))
1117 (defun gdb-dequeue-input ()
1118 (let ((queue gdb-input-queue))
1120 (let ((last (car (last queue))))
1121 (unless (nbutlast queue) (setq gdb-input-queue '()))
1124 (defun gdb-send-item (item)
1125 (setq gdb-flush-pending-output nil)
1126 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1127 (setq gdb-current-item item)
1128 (let ((process (get-buffer-process gud-comint-buffer)))
1129 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1132 (setq gdb-output-sink 'user)
1133 (process-send-string process item))
1135 (gdb-clear-partial-output)
1136 (setq gdb-output-sink 'pre-emacs)
1137 (process-send-string process
1139 ;; case: eq gud-minor-mode 'gdbmi
1140 (gdb-clear-partial-output)
1141 (setq gdb-output-sink 'emacs)
1142 (process-send-string process (car item)))))
1145 ;; output -- things gdb prints to emacs
1147 ;; GDB output is a stream interrupted by annotations.
1148 ;; Annotations can be recognized by their beginning
1149 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1151 ;; The tag is a string obeying symbol syntax.
1153 ;; The optional part `<opt>' can be either the empty string
1154 ;; or a space followed by more data relating to the annotation.
1155 ;; For example, the SOURCE annotation is followed by a filename,
1156 ;; line number and various useless goo. This data must not include
1160 (defcustom gud-gdba-command-name "gdb -annotate=3"
1161 "Default command to execute an executable under the GDB-UI debugger."
1166 (defvar gdb-annotation-rules
1167 '(("pre-prompt" gdb-pre-prompt)
1168 ("prompt" gdb-prompt)
1169 ("commands" gdb-subprompt)
1170 ("overload-choice" gdb-subprompt)
1171 ("query" gdb-subprompt)
1172 ;; Need this prompt for GDB 6.1
1173 ("nquery" gdb-subprompt)
1174 ("prompt-for-continue" gdb-subprompt)
1175 ("post-prompt" gdb-post-prompt)
1176 ("source" gdb-source)
1177 ("starting" gdb-starting)
1178 ("exited" gdb-exited)
1179 ("signalled" gdb-signalled)
1180 ("signal" gdb-signal)
1181 ("breakpoint" gdb-stopping)
1182 ("watchpoint" gdb-stopping)
1183 ("frame-begin" gdb-frame-begin)
1184 ("stopped" gdb-stopped)
1185 ("error-begin" gdb-error)
1187 ) "An assoc mapping annotation tags to functions which process them.")
1190 (setq gdb-flush-pending-output t)
1191 (setq gud-running nil)
1192 (gdb-force-mode-line-update "stopped")
1193 (setq gdb-output-sink 'user)
1194 (setq gdb-input-queue nil)
1195 (setq gdb-pending-triggers nil)
1196 (setq gdb-prompting t))
1198 (defconst gdb-source-spec-regexp
1199 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1201 ;; Do not use this except as an annotation handler.
1202 (defun gdb-source (args)
1203 (string-match gdb-source-spec-regexp args)
1204 ;; Extract the frame position from the marker.
1205 (setq gud-last-frame
1207 (match-string 1 args)
1208 (string-to-number (match-string 2 args))))
1209 (setq gdb-frame-address (match-string 3 args))
1210 ;; cover for auto-display output which comes *before*
1211 ;; stopped annotation
1212 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1214 (defun gdb-pre-prompt (ignored)
1215 "An annotation handler for `pre-prompt'.
1216 This terminates the collection of output from a previous command if that
1217 happens to be in effect."
1218 (setq gdb-error nil)
1219 (let ((sink gdb-output-sink))
1223 (setq gdb-output-sink 'post-emacs))
1226 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1228 (defun gdb-prompt (ignored)
1229 "An annotation handler for `prompt'.
1230 This sends the next command (if any) to gdb."
1231 (when gdb-first-prompt
1233 (setq gdb-first-prompt nil))
1234 (let ((sink gdb-output-sink))
1237 ((eq sink 'post-emacs)
1238 (setq gdb-output-sink 'user)
1240 (car (cdr gdb-current-item))))
1241 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1242 (funcall handler))))
1245 (error "Phase error in gdb-prompt (got %s)" sink))))
1246 (let ((input (gdb-dequeue-input)))
1248 (gdb-send-item input)
1250 (setq gdb-prompting t)
1251 (gud-display-frame)))))
1253 (defun gdb-subprompt (ignored)
1254 "An annotation handler for non-top-level prompts."
1255 (setq gdb-prompting t))
1257 (defun gdb-starting (ignored)
1258 "An annotation handler for `starting'.
1259 This says that I/O for the subprocess is now the program being debugged,
1261 (setq gdb-active-process t)
1262 (let ((sink gdb-output-sink))
1266 (setq gud-running t)
1267 (setq gdb-inferior-status "running")
1268 (gdb-force-mode-line-update gdb-inferior-status)
1269 (gdb-remove-text-properties)
1270 (setq gud-overlay-arrow-position nil)
1271 (setq gdb-overlay-arrow-position nil)
1272 (if gdb-use-separate-io-buffer
1273 (setq gdb-output-sink 'inferior))))
1276 (error "Unexpected `starting' annotation")))))
1278 (defun gdb-signal (ignored)
1279 (setq gdb-inferior-status "signal")
1280 (gdb-force-mode-line-update gdb-inferior-status)
1281 (gdb-stopping ignored))
1283 (defun gdb-stopping (ignored)
1284 "An annotation handler for `breakpoint' and other annotations.
1285 They say that I/O for the subprocess is now GDB, not the program
1287 (if gdb-use-separate-io-buffer
1288 (let ((sink gdb-output-sink))
1290 ((eq sink 'inferior)
1291 (setq gdb-output-sink 'user))
1294 (error "Unexpected stopping annotation"))))))
1296 (defun gdb-exited (ignored)
1297 "An annotation handler for `exited' and `signalled'.
1298 They say that I/O for the subprocess is now GDB, not the program
1299 being debugged and that the program is no longer running. This
1300 function is used to change the focus of GUD tooltips to #define
1302 (setq gdb-active-process nil)
1303 (setq gud-overlay-arrow-position nil)
1304 (setq gdb-overlay-arrow-position nil)
1305 (setq gdb-inferior-status "exited")
1306 (gdb-force-mode-line-update gdb-inferior-status)
1307 (gdb-stopping ignored))
1309 (defun gdb-signalled (ignored)
1310 (setq gdb-signalled t))
1312 (defun gdb-frame-begin (ignored)
1313 (let ((sink gdb-output-sink))
1315 ((eq sink 'inferior)
1316 (setq gdb-output-sink 'user))
1318 ((eq sink 'emacs) t)
1321 (error "Unexpected frame-begin annotation (%S)" sink)))))
1323 (defun gdb-stopped (ignored)
1324 "An annotation handler for `stopped'.
1325 It is just like `gdb-stopping', except that if we already set the output
1326 sink to `user' in `gdb-stopping', that is fine."
1327 (setq gud-running nil)
1328 (unless (or gud-overlay-arrow-position gud-last-frame
1329 (not gud-last-last-frame))
1330 (gud-display-line (car gud-last-last-frame) (cdr gud-last-last-frame)))
1331 (unless (member gdb-inferior-status '("exited" "signal"))
1332 (setq gdb-inferior-status "stopped")
1333 (gdb-force-mode-line-update gdb-inferior-status))
1334 (let ((sink gdb-output-sink))
1336 ((eq sink 'inferior)
1337 (setq gdb-output-sink 'user))
1341 (error "Unexpected stopped annotation"))))
1342 (if gdb-signalled (gdb-exited ignored)))
1344 (defun gdb-error (ignored)
1345 (setq gdb-error (not gdb-error)))
1347 (defun gdb-post-prompt (ignored)
1348 "An annotation handler for `post-prompt'.
1349 This begins the collection of output from the current command if that
1350 happens to be appropriate."
1351 ;; Don't add to queue if there outstanding items or gdb-version is not known
1353 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1354 (gdb-get-selected-frame)
1355 (gdb-invalidate-frames)
1356 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1357 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1358 (gdb-invalidate-breakpoints)
1359 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1360 ;; so gdb-frame-address is updated.
1361 ;; (gdb-invalidate-assembler)
1363 (if (string-equal gdb-version "pre-6.4")
1364 (gdb-invalidate-registers)
1365 (gdb-get-changed-registers)
1366 (gdb-invalidate-registers-1))
1368 (gdb-invalidate-memory)
1369 (if (string-equal gdb-version "pre-6.4")
1370 (gdb-invalidate-locals)
1371 (gdb-invalidate-locals-1))
1373 (gdb-invalidate-threads)
1374 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1375 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1376 ;; Only needed/used with speedbar/watch expressions.
1377 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1378 (setq gdb-force-update t)
1379 (if (string-equal gdb-version "pre-6.4")
1381 (gdb-var-update-1)))))
1382 (setq gdb-first-post-prompt nil)
1383 (let ((sink gdb-output-sink))
1386 ((eq sink 'pre-emacs)
1387 (setq gdb-output-sink 'emacs))
1390 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1392 (defconst gdb-buffer-list
1393 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1395 (defun gdb-remove-text-properties ()
1396 (dolist (buffertype gdb-buffer-list)
1397 (let ((buffer (gdb-get-buffer buffertype)))
1399 (with-current-buffer buffer
1400 (let ((inhibit-read-only t))
1401 (remove-text-properties
1402 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1404 ;; GUD displays the selected GDB frame. This might might not be the current
1405 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1406 ;; visited breakpoint is, use that window.
1407 (defun gdb-display-source-buffer (buffer)
1408 (let* ((last-window (if gud-last-last-frame
1410 (gud-find-file (car gud-last-last-frame)))))
1411 (source-window (or last-window
1412 (if (and gdb-source-window
1413 (window-live-p gdb-source-window))
1414 gdb-source-window))))
1416 (setq gdb-source-window source-window)
1417 (set-window-buffer source-window buffer))
1420 (defun gud-gdba-marker-filter (string)
1421 "A gud marker filter for gdb. Handle a burst of output from GDB."
1422 (if gdb-flush-pending-output
1424 (when gdb-enable-debug
1425 (push (cons 'recv string) gdb-debug-ring)
1426 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1427 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1428 ;; Recall the left over gud-marker-acc from last time.
1429 (setq gud-marker-acc (concat gud-marker-acc string))
1430 ;; Start accumulating output for the GUD buffer.
1433 ;; Process all the complete markers in this chunk.
1434 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1435 (let ((annotation (match-string 1 gud-marker-acc)))
1437 ;; Stuff prior to the match is just ordinary output.
1438 ;; It is either concatenated to OUTPUT or directed
1443 (substring gud-marker-acc 0 (match-beginning 0))))
1445 ;; Take that stuff off the gud-marker-acc.
1446 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1448 ;; Parse the tag from the annotation, and maybe its arguments.
1449 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1450 (let* ((annotation-type (match-string 1 annotation))
1451 (annotation-arguments (match-string 2 annotation))
1452 (annotation-rule (assoc annotation-type
1453 gdb-annotation-rules)))
1454 ;; Call the handler for this annotation.
1456 (funcall (car (cdr annotation-rule))
1457 annotation-arguments)
1458 ;; Else the annotation is not recognized. Ignore it silently,
1459 ;; so that GDB can add new annotations without causing
1463 ;; Does the remaining text end in a partial line?
1464 ;; If it does, then keep part of the gud-marker-acc until we get more.
1465 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1468 ;; Everything before the potential marker start can be output.
1470 (gdb-concat-output output
1471 (substring gud-marker-acc 0
1472 (match-beginning 0))))
1474 ;; Everything after, we save, to combine with later input.
1475 (setq gud-marker-acc (substring gud-marker-acc
1476 (match-beginning 0))))
1478 ;; In case we know the gud-marker-acc contains no partial annotations:
1480 (setq output (gdb-concat-output output gud-marker-acc))
1481 (setq gud-marker-acc "")))
1484 (defun gdb-concat-output (so-far new)
1486 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1487 (let ((sink gdb-output-sink))
1489 ((eq sink 'user) (concat so-far new))
1490 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1492 (gdb-append-to-partial-output new)
1494 ((eq sink 'inferior)
1495 (gdb-append-to-inferior-io new)
1499 (error "Bogon output sink %S" sink)))))
1501 (defun gdb-append-to-partial-output (string)
1502 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1503 (goto-char (point-max))
1506 (defun gdb-clear-partial-output ()
1507 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1510 (defun gdb-append-to-inferior-io (string)
1511 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1512 (goto-char (point-max))
1513 (insert-before-markers string))
1514 (if (not (string-equal string ""))
1515 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1517 (defun gdb-clear-inferior-io ()
1518 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1522 ;; One trick is to have a command who's output is always available in a buffer
1523 ;; of it's own, and is always up to date. We build several buffers of this
1526 ;; There are two aspects to this: gdb has to tell us when the output for that
1527 ;; command might have changed, and we have to be able to run the command
1528 ;; behind the user's back.
1530 ;; The output phasing associated with the variable gdb-output-sink
1531 ;; help us to run commands behind the user's back.
1533 ;; Below is the code for specificly managing buffers of output from one
1537 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1538 ;; It adds an input for the command we are tracking. It should be the
1539 ;; annotation rule binding of whatever gdb sends to tell us this command
1540 ;; might have changed it's output.
1542 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1543 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1544 ;; input in the input queue (see comment about ``gdb communications'' above).
1546 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1548 `(defun ,name (&optional ignored)
1549 (if (and ,demand-predicate
1551 gdb-pending-triggers)))
1554 (list ,gdb-command ',output-handler))
1555 (push ',name gdb-pending-triggers)))))
1557 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1559 (setq gdb-pending-triggers
1561 gdb-pending-triggers))
1562 (let ((buf (gdb-get-buffer ',buf-key)))
1564 (with-current-buffer buf
1565 (let* ((window (get-buffer-window buf 0))
1566 (start (window-start window))
1567 (p (window-point window))
1568 (buffer-read-only nil))
1570 (insert-buffer-substring (gdb-get-buffer-create
1571 'gdb-partial-output-buffer))
1572 (set-window-start window start)
1573 (set-window-point window p)))))
1574 ;; put customisation here
1577 (defmacro def-gdb-auto-updated-buffer (buffer-key
1578 trigger-name gdb-command
1579 output-handler-name custom-defun)
1581 (def-gdb-auto-update-trigger ,trigger-name
1582 ;; The demand predicate:
1583 (gdb-get-buffer ',buffer-key)
1585 ,output-handler-name)
1586 (def-gdb-auto-update-handler ,output-handler-name
1587 ,trigger-name ,buffer-key ,custom-defun)))
1591 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1593 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1594 'gdb-breakpoints-buffer-name
1595 'gdb-breakpoints-mode)
1597 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1598 ;; This defines the auto update rule for buffers of type
1599 ;; `gdb-breakpoints-buffer'.
1601 ;; It defines a function to serve as the annotation handler that
1602 ;; handles the `foo-invalidated' message. That function is called:
1603 gdb-invalidate-breakpoints
1605 ;; To update the buffer, this command is sent to gdb.
1606 "server info breakpoints\n"
1608 ;; This also defines a function to be the handler for the output
1609 ;; from the command above. That function will copy the output into
1610 ;; the appropriately typed buffer. That function will be called:
1611 gdb-info-breakpoints-handler
1612 ;; buffer specific functions
1613 gdb-info-breakpoints-custom)
1615 (defconst breakpoint-xpm-data
1617 static char *magick[] = {
1618 /* columns rows colors chars-per-pixel */
1634 "XPM data used for breakpoint icon.")
1636 (defconst breakpoint-enabled-pbm-data
1639 0 0 0 0 1 1 1 1 0 0 0 0
1640 0 0 0 1 1 1 1 1 1 0 0 0
1641 0 0 1 1 1 1 1 1 1 1 0 0
1642 0 1 1 1 1 1 1 1 1 1 1 0
1643 0 1 1 1 1 1 1 1 1 1 1 0
1644 0 1 1 1 1 1 1 1 1 1 1 0
1645 0 1 1 1 1 1 1 1 1 1 1 0
1646 0 0 1 1 1 1 1 1 1 1 0 0
1647 0 0 0 1 1 1 1 1 1 0 0 0
1648 0 0 0 0 1 1 1 1 0 0 0 0"
1649 "PBM data used for enabled breakpoint icon.")
1651 (defconst breakpoint-disabled-pbm-data
1663 0 0 0 1 0 1 0 1 0 0"
1664 "PBM data used for disabled breakpoint icon.")
1666 (defvar breakpoint-enabled-icon nil
1667 "Icon for enabled breakpoint in display margin.")
1669 (defvar breakpoint-disabled-icon nil
1670 "Icon for disabled breakpoint in display margin.")
1672 (and (display-images-p)
1673 ;; Bitmap for breakpoint in fringe
1674 (define-fringe-bitmap 'breakpoint
1675 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1676 ;; Bitmap for gud-overlay-arrow in fringe
1677 (define-fringe-bitmap 'hollow-right-triangle
1678 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1680 (defface breakpoint-enabled
1684 "Face for enabled breakpoint icon in fringe."
1687 (defface breakpoint-disabled
1688 ;; We use different values of grey for different background types,
1689 ;; so that on low-color displays it will end up as something visible
1690 ;; if it has to be approximated.
1691 '((t :foreground "grey70"))
1692 "Face for disabled breakpoint icon in fringe."
1695 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1696 (defun gdb-info-breakpoints-custom ()
1697 (let ((flag) (bptno))
1698 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1699 (dolist (buffer (buffer-list))
1700 (with-current-buffer buffer
1701 (if (and (memq gud-minor-mode '(gdba gdbmi))
1702 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1703 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1704 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1706 (goto-char (point-min))
1707 (while (< (point) (- (point-max) 1))
1709 (if (looking-at "[^\t].*?breakpoint")
1711 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1712 (setq bptno (match-string 1))
1713 (setq flag (char-after (match-beginning 2)))
1715 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1717 (let ((buffer-read-only nil))
1718 (add-text-properties (match-beginning 1) (match-end 1)
1719 '(face font-lock-function-name-face)))
1720 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1721 (let ((line (match-string 2)) (buffer-read-only nil)
1722 (file (match-string 1)))
1723 (add-text-properties (line-beginning-position)
1725 '(mouse-face highlight
1726 help-echo "mouse-2, RET: visit breakpoint"))
1727 (unless (file-exists-p file)
1728 (setq file (cdr (assoc bptno gdb-location-alist))))
1730 (not (string-equal file "File not found")))
1731 (with-current-buffer
1732 (find-file-noselect file 'nowarn)
1733 (set (make-local-variable 'gud-minor-mode)
1735 (set (make-local-variable 'tool-bar-map)
1737 ;; Only want one breakpoint icon at each
1740 (goto-line (string-to-number line))
1741 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1744 (concat gdb-server-prefix "list "
1745 (match-string-no-properties 1) ":1\n")
1748 (list (concat gdb-server-prefix "info source\n")
1749 `(lambda () (gdb-get-location
1750 ,bptno ,line ,flag))))))))))
1752 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1754 (defun gdb-mouse-set-clear-breakpoint (event)
1755 "Set/clear breakpoint in left fringe/margin with mouse click."
1757 (mouse-minibuffer-check event)
1758 (let ((posn (event-end event)))
1759 (if (numberp (posn-point posn))
1760 (with-selected-window (posn-window posn)
1762 (goto-char (posn-point posn))
1763 (if (or (posn-object posn)
1764 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1767 (gud-break nil)))))))
1769 (defun gdb-mouse-toggle-breakpoint-margin (event)
1770 "Enable/disable breakpoint in left margin with mouse click."
1772 (mouse-minibuffer-check event)
1773 (let ((posn (event-end event)))
1774 (if (numberp (posn-point posn))
1775 (with-selected-window (posn-window posn)
1777 (goto-char (posn-point posn))
1778 (if (posn-object posn)
1781 (let ((bptno (get-text-property
1782 0 'gdb-bptno (car (posn-string posn)))))
1783 (concat gdb-server-prefix
1784 (if (get-text-property
1785 0 'gdb-enabled (car (posn-string posn)))
1791 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1792 "Enable/disable breakpoint in left fringe with mouse click."
1794 (mouse-minibuffer-check event)
1795 (let* ((posn (event-end event))
1796 (pos (posn-point posn))
1799 (with-selected-window (posn-window posn)
1801 (set-buffer (window-buffer (selected-window)))
1803 (dolist (overlay (overlays-in pos pos))
1804 (when (overlay-get overlay 'put-break)
1805 (setq obj (overlay-get overlay 'before-string))))
1809 (concat gdb-server-prefix
1810 (if (get-text-property 0 'gdb-enabled obj)
1813 (get-text-property 0 'gdb-bptno obj) "\n")
1816 (defun gdb-breakpoints-buffer-name ()
1817 (with-current-buffer gud-comint-buffer
1818 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1820 (defun gdb-display-breakpoints-buffer ()
1821 "Display status of user-settable breakpoints."
1824 (gdb-get-buffer-create 'gdb-breakpoints-buffer)))
1826 (defun gdb-frame-breakpoints-buffer ()
1827 "Display status of user-settable breakpoints in a new frame."
1829 (let ((special-display-regexps (append special-display-regexps '(".*")))
1830 (special-display-frame-alist gdb-frame-parameters))
1831 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1833 (defvar gdb-breakpoints-mode-map
1834 (let ((map (make-sparse-keymap))
1835 (menu (make-sparse-keymap "Breakpoints")))
1836 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1837 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1838 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1839 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1840 (suppress-keymap map)
1841 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1842 (define-key map " " 'gdb-toggle-breakpoint)
1843 (define-key map "D" 'gdb-delete-breakpoint)
1844 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1845 (define-key map "q" 'gdb-delete-frame-or-window)
1846 (define-key map "\r" 'gdb-goto-breakpoint)
1847 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1848 (define-key map [follow-link] 'mouse-face)
1851 (defun gdb-delete-frame-or-window ()
1852 "Delete frame if there is only one window. Otherwise delete the window."
1854 (if (one-window-p) (delete-frame)
1857 (defun gdb-breakpoints-mode ()
1858 "Major mode for gdb breakpoints.
1860 \\{gdb-breakpoints-mode-map}"
1861 (kill-all-local-variables)
1862 (setq major-mode 'gdb-breakpoints-mode)
1863 (setq mode-name "Breakpoints")
1864 (use-local-map gdb-breakpoints-mode-map)
1865 (setq buffer-read-only t)
1866 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1867 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1868 'gdb-invalidate-breakpoints
1869 'gdbmi-invalidate-breakpoints))
1871 (defconst gdb-breakpoint-regexp
1872 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1874 (defun gdb-toggle-breakpoint ()
1875 "Enable/disable breakpoint at current line."
1878 (beginning-of-line 1)
1879 (if (looking-at gdb-breakpoint-regexp)
1882 (concat gdb-server-prefix
1883 (if (eq ?y (char-after (match-beginning 2)))
1886 (match-string 1) "\n") 'ignore))
1887 (error "Not recognized as break/watchpoint line"))))
1889 (defun gdb-delete-breakpoint ()
1890 "Delete the breakpoint at current line."
1892 (beginning-of-line 1)
1893 (if (looking-at gdb-breakpoint-regexp)
1896 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1897 (error "Not recognized as break/watchpoint line")))
1899 (defun gdb-goto-breakpoint (&optional event)
1900 "Display the breakpoint location specified at current line."
1901 (interactive (list last-input-event))
1902 (if event (posn-set-point (event-end event)))
1904 (beginning-of-line 1)
1905 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1906 (let ((bptno (match-string 1))
1907 (file (match-string 2))
1908 (line (match-string 3)))
1909 (save-selected-window
1910 (let* ((buffer (find-file-noselect
1911 (if (file-exists-p file) file
1912 (cdr (assoc bptno gdb-location-alist)))))
1913 (window (unless (gdb-display-source-buffer buffer)
1914 (display-buffer buffer))))
1915 (setq gdb-source-window window)
1916 (with-current-buffer buffer
1917 (goto-line (string-to-number line))
1918 (set-window-point window (point))))))
1919 (error "No location specified."))))
1922 ;; Frames buffer. This displays a perpetually correct bactracktrace
1923 ;; (from the command `where').
1925 ;; Alas, if your stack is deep, it is costly.
1927 (gdb-set-buffer-rules 'gdb-stack-buffer
1928 'gdb-stack-buffer-name
1931 (def-gdb-auto-updated-buffer gdb-stack-buffer
1932 gdb-invalidate-frames
1933 "server info stack\n"
1934 gdb-info-stack-handler
1935 gdb-info-stack-custom)
1937 (defun gdb-info-stack-custom ()
1938 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1940 (let ((buffer-read-only nil)
1942 (goto-char (point-min))
1943 (while (< (point) (point-max))
1944 (setq bl (line-beginning-position)
1945 el (line-end-position))
1946 (when (looking-at "#")
1947 (add-text-properties bl el
1948 '(mouse-face highlight
1949 help-echo "mouse-2, RET: Select frame")))
1951 (when (looking-at "^#\\([0-9]+\\)")
1952 (when (string-equal (match-string 1) gdb-frame-number)
1953 (put-text-property bl (+ bl 4)
1954 'face '(:inverse-video t)))
1955 (when (re-search-forward
1957 (if (string-equal (match-string 1) "0") "" " in ")
1958 "\\([^ ]+\\) (") el t)
1959 (put-text-property (match-beginning 1) (match-end 1)
1960 'face font-lock-function-name-face)
1961 (setq bl (match-end 0))
1962 (while (re-search-forward "<\\([^>]+\\)>" el t)
1963 (put-text-property (match-beginning 1) (match-end 1)
1964 'face font-lock-function-name-face))
1966 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1967 (put-text-property (match-beginning 1) (match-end 1)
1968 'face font-lock-variable-name-face))))
1969 (forward-line 1))))))
1971 (defun gdb-stack-buffer-name ()
1972 (with-current-buffer gud-comint-buffer
1973 (concat "*stack frames of " (gdb-get-target-string) "*")))
1975 (defun gdb-display-stack-buffer ()
1976 "Display backtrace of current stack."
1979 (gdb-get-buffer-create 'gdb-stack-buffer)))
1981 (defun gdb-frame-stack-buffer ()
1982 "Display backtrace of current stack in a new frame."
1984 (let ((special-display-regexps (append special-display-regexps '(".*")))
1985 (special-display-frame-alist gdb-frame-parameters))
1986 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
1988 (defvar gdb-frames-mode-map
1989 (let ((map (make-sparse-keymap)))
1990 (suppress-keymap map)
1991 (define-key map "q" 'kill-this-buffer)
1992 (define-key map "\r" 'gdb-frames-select)
1993 (define-key map [mouse-2] 'gdb-frames-select)
1994 (define-key map [follow-link] 'mouse-face)
1997 (defun gdb-frames-mode ()
1998 "Major mode for gdb call stack.
2000 \\{gdb-frames-mode-map}"
2001 (kill-all-local-variables)
2002 (setq major-mode 'gdb-frames-mode)
2003 (setq mode-name "Frames")
2004 (setq buffer-read-only t)
2005 (use-local-map gdb-frames-mode-map)
2006 (run-mode-hooks 'gdb-frames-mode-hook)
2007 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2008 'gdb-invalidate-frames
2009 'gdbmi-invalidate-frames))
2011 (defun gdb-get-frame-number ()
2014 (let* ((start (line-beginning-position))
2015 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2016 (n (or (and pos (match-string-no-properties 1)) "0")))
2019 (defun gdb-frames-select (&optional event)
2020 "Select the frame and display the relevant source."
2021 (interactive (list last-input-event))
2022 (if event (posn-set-point (event-end event)))
2024 (list (concat gdb-server-prefix "frame "
2025 (gdb-get-frame-number) "\n") 'ignore))
2026 (gud-display-frame))
2029 ;; Threads buffer. This displays a selectable thread list.
2031 (gdb-set-buffer-rules 'gdb-threads-buffer
2032 'gdb-threads-buffer-name
2035 (def-gdb-auto-updated-buffer gdb-threads-buffer
2036 gdb-invalidate-threads
2037 (concat gdb-server-prefix "info threads\n")
2038 gdb-info-threads-handler
2039 gdb-info-threads-custom)
2041 (defun gdb-info-threads-custom ()
2042 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2043 (let ((buffer-read-only nil))
2044 (goto-char (point-min))
2045 (while (< (point) (point-max))
2046 (unless (looking-at "No ")
2047 (add-text-properties (line-beginning-position) (line-end-position)
2048 '(mouse-face highlight
2049 help-echo "mouse-2, RET: select thread")))
2050 (forward-line 1)))))
2052 (defun gdb-threads-buffer-name ()
2053 (with-current-buffer gud-comint-buffer
2054 (concat "*threads of " (gdb-get-target-string) "*")))
2056 (defun gdb-display-threads-buffer ()
2057 "Display IDs of currently known threads."
2060 (gdb-get-buffer-create 'gdb-threads-buffer)))
2062 (defun gdb-frame-threads-buffer ()
2063 "Display IDs of currently known threads in a new frame."
2065 (let ((special-display-regexps (append special-display-regexps '(".*")))
2066 (special-display-frame-alist gdb-frame-parameters))
2067 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2069 (defvar gdb-threads-mode-map
2070 (let ((map (make-sparse-keymap)))
2071 (suppress-keymap map)
2072 (define-key map "q" 'kill-this-buffer)
2073 (define-key map "\r" 'gdb-threads-select)
2074 (define-key map [mouse-2] 'gdb-threads-select)
2075 (define-key map [follow-link] 'mouse-face)
2078 (defvar gdb-threads-font-lock-keywords
2079 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2080 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2081 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2082 "Font lock keywords used in `gdb-threads-mode'.")
2084 (defun gdb-threads-mode ()
2085 "Major mode for gdb threads.
2087 \\{gdb-threads-mode-map}"
2088 (kill-all-local-variables)
2089 (setq major-mode 'gdb-threads-mode)
2090 (setq mode-name "Threads")
2091 (setq buffer-read-only t)
2092 (use-local-map gdb-threads-mode-map)
2093 (set (make-local-variable 'font-lock-defaults)
2094 '(gdb-threads-font-lock-keywords))
2095 (run-mode-hooks 'gdb-threads-mode-hook)
2096 'gdb-invalidate-threads)
2098 (defun gdb-get-thread-number ()
2100 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2101 (match-string-no-properties 1)))
2103 (defun gdb-threads-select (&optional event)
2104 "Select the thread and display the relevant source."
2105 (interactive (list last-input-event))
2106 (if event (posn-set-point (event-end event)))
2108 (list (concat gdb-server-prefix "thread "
2109 (gdb-get-thread-number) "\n") 'ignore))
2110 (gud-display-frame))
2113 ;; Registers buffer.
2115 (defcustom gdb-all-registers nil
2116 "Non-nil means include floating-point registers."
2121 (gdb-set-buffer-rules 'gdb-registers-buffer
2122 'gdb-registers-buffer-name
2123 'gdb-registers-mode)
2125 (def-gdb-auto-updated-buffer gdb-registers-buffer
2126 gdb-invalidate-registers
2128 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2129 gdb-info-registers-handler
2130 gdb-info-registers-custom)
2132 (defun gdb-info-registers-custom ()
2133 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2135 (let ((buffer-read-only nil)
2137 (goto-char (point-min))
2138 (while (< (point) (point-max))
2139 (setq start (line-beginning-position))
2140 (setq end (line-end-position))
2141 (when (looking-at "^[^ ]+")
2142 (unless (string-equal (match-string 0) "The")
2143 (put-text-property start (match-end 0)
2144 'face font-lock-variable-name-face)
2145 (add-text-properties start end
2146 '(help-echo "mouse-2: edit value"
2147 mouse-face highlight))))
2148 (forward-line 1))))))
2150 (defun gdb-edit-register-value (&optional event)
2151 (interactive (list last-input-event))
2153 (if event (posn-set-point (event-end event)))
2155 (let* ((register (current-word))
2156 (value (read-string (format "New value (%s): " register))))
2158 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2161 (defvar gdb-registers-mode-map
2162 (let ((map (make-sparse-keymap)))
2163 (suppress-keymap map)
2164 (define-key map "\r" 'gdb-edit-register-value)
2165 (define-key map [mouse-2] 'gdb-edit-register-value)
2166 (define-key map " " 'gdb-all-registers)
2167 (define-key map "q" 'kill-this-buffer)
2170 (defun gdb-registers-mode ()
2171 "Major mode for gdb registers.
2173 \\{gdb-registers-mode-map}"
2174 (kill-all-local-variables)
2175 (setq major-mode 'gdb-registers-mode)
2176 (setq mode-name "Registers")
2177 (setq buffer-read-only t)
2178 (use-local-map gdb-registers-mode-map)
2179 (run-mode-hooks 'gdb-registers-mode-hook)
2180 (if (string-equal gdb-version "pre-6.4")
2182 (if gdb-all-registers (setq mode-name "Registers:All"))
2183 'gdb-invalidate-registers)
2184 'gdb-invalidate-registers-1))
2186 (defun gdb-registers-buffer-name ()
2187 (with-current-buffer gud-comint-buffer
2188 (concat "*registers of " (gdb-get-target-string) "*")))
2190 (defun gdb-display-registers-buffer ()
2191 "Display integer register contents."
2194 (gdb-get-buffer-create 'gdb-registers-buffer)))
2196 (defun gdb-frame-registers-buffer ()
2197 "Display integer register contents in a new frame."
2199 (let ((special-display-regexps (append special-display-regexps '(".*")))
2200 (special-display-frame-alist gdb-frame-parameters))
2201 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2203 (defun gdb-all-registers ()
2204 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2206 (when (string-equal gdb-version "pre-6.4")
2207 (if gdb-all-registers
2209 (setq gdb-all-registers nil)
2210 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2211 (setq mode-name "Registers")))
2212 (setq gdb-all-registers t)
2213 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2214 (setq mode-name "Registers:All")))
2215 (message (format "Display of floating-point registers %sabled"
2216 (if gdb-all-registers "en" "dis")))
2217 (gdb-invalidate-registers)))
2222 (defcustom gdb-memory-repeat-count 32
2223 "Number of data items in memory window."
2228 (defcustom gdb-memory-format "x"
2229 "Display format of data items in memory window."
2230 :type '(choice (const :tag "Hexadecimal" "x")
2231 (const :tag "Signed decimal" "d")
2232 (const :tag "Unsigned decimal" "u")
2233 (const :tag "Octal" "o")
2234 (const :tag "Binary" "t"))
2238 (defcustom gdb-memory-unit "w"
2239 "Unit size of data items in memory window."
2240 :type '(choice (const :tag "Byte" "b")
2241 (const :tag "Halfword" "h")
2242 (const :tag "Word" "w")
2243 (const :tag "Giant word" "g"))
2247 (gdb-set-buffer-rules 'gdb-memory-buffer
2248 'gdb-memory-buffer-name
2251 (def-gdb-auto-updated-buffer gdb-memory-buffer
2252 gdb-invalidate-memory
2253 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2254 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2255 gdb-read-memory-handler
2256 gdb-read-memory-custom)
2258 (defun gdb-read-memory-custom ()
2260 (goto-char (point-min))
2261 (if (looking-at "0x[[:xdigit:]]+")
2262 (setq gdb-memory-address (match-string 0)))))
2264 (defvar gdb-memory-mode-map
2265 (let ((map (make-sparse-keymap)))
2266 (suppress-keymap map)
2267 (define-key map "q" 'kill-this-buffer)
2270 (defun gdb-memory-set-address (event)
2271 "Set the start memory address."
2273 (save-selected-window
2274 (select-window (posn-window (event-start event)))
2275 (let ((arg (read-from-minibuffer "Memory address: ")))
2276 (setq gdb-memory-address arg))
2277 (gdb-invalidate-memory)))
2279 (defun gdb-memory-set-repeat-count (event)
2280 "Set the number of data items in memory window."
2282 (save-selected-window
2283 (select-window (posn-window (event-start event)))
2284 (let* ((arg (read-from-minibuffer "Repeat count: "))
2285 (count (string-to-number arg)))
2287 (error "Positive numbers only")
2288 (customize-set-variable 'gdb-memory-repeat-count count)
2289 (gdb-invalidate-memory)))))
2291 (defun gdb-memory-format-binary ()
2292 "Set the display format to binary."
2294 (customize-set-variable 'gdb-memory-format "t")
2295 (gdb-invalidate-memory))
2297 (defun gdb-memory-format-octal ()
2298 "Set the display format to octal."
2300 (customize-set-variable 'gdb-memory-format "o")
2301 (gdb-invalidate-memory))
2303 (defun gdb-memory-format-unsigned ()
2304 "Set the display format to unsigned decimal."
2306 (customize-set-variable 'gdb-memory-format "u")
2307 (gdb-invalidate-memory))
2309 (defun gdb-memory-format-signed ()
2310 "Set the display format to decimal."
2312 (customize-set-variable 'gdb-memory-format "d")
2313 (gdb-invalidate-memory))
2315 (defun gdb-memory-format-hexadecimal ()
2316 "Set the display format to hexadecimal."
2318 (customize-set-variable 'gdb-memory-format "x")
2319 (gdb-invalidate-memory))
2321 (defvar gdb-memory-format-map
2322 (let ((map (make-sparse-keymap)))
2323 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2325 "Keymap to select format in the header line.")
2327 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2328 "Menu of display formats in the header line.")
2330 (define-key gdb-memory-format-menu [binary]
2331 '(menu-item "Binary" gdb-memory-format-binary
2332 :button (:radio . (equal gdb-memory-format "t"))))
2333 (define-key gdb-memory-format-menu [octal]
2334 '(menu-item "Octal" gdb-memory-format-octal
2335 :button (:radio . (equal gdb-memory-format "o"))))
2336 (define-key gdb-memory-format-menu [unsigned]
2337 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2338 :button (:radio . (equal gdb-memory-format "u"))))
2339 (define-key gdb-memory-format-menu [signed]
2340 '(menu-item "Signed Decimal" gdb-memory-format-signed
2341 :button (:radio . (equal gdb-memory-format "d"))))
2342 (define-key gdb-memory-format-menu [hexadecimal]
2343 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2344 :button (:radio . (equal gdb-memory-format "x"))))
2346 (defun gdb-memory-format-menu (event)
2348 (x-popup-menu event gdb-memory-format-menu))
2350 (defun gdb-memory-format-menu-1 (event)
2352 (save-selected-window
2353 (select-window (posn-window (event-start event)))
2354 (let* ((selection (gdb-memory-format-menu event))
2355 (binding (and selection (lookup-key gdb-memory-format-menu
2356 (vector (car selection))))))
2357 (if binding (call-interactively binding)))))
2359 (defun gdb-memory-unit-giant ()
2360 "Set the unit size to giant words (eight bytes)."
2362 (customize-set-variable 'gdb-memory-unit "g")
2363 (gdb-invalidate-memory))
2365 (defun gdb-memory-unit-word ()
2366 "Set the unit size to words (four bytes)."
2368 (customize-set-variable 'gdb-memory-unit "w")
2369 (gdb-invalidate-memory))
2371 (defun gdb-memory-unit-halfword ()
2372 "Set the unit size to halfwords (two bytes)."
2374 (customize-set-variable 'gdb-memory-unit "h")
2375 (gdb-invalidate-memory))
2377 (defun gdb-memory-unit-byte ()
2378 "Set the unit size to bytes."
2380 (customize-set-variable 'gdb-memory-unit "b")
2381 (gdb-invalidate-memory))
2383 (defvar gdb-memory-unit-map
2384 (let ((map (make-sparse-keymap)))
2385 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2387 "Keymap to select units in the header line.")
2389 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2390 "Menu of units in the header line.")
2392 (define-key gdb-memory-unit-menu [giantwords]
2393 '(menu-item "Giant words" gdb-memory-unit-giant
2394 :button (:radio . (equal gdb-memory-unit "g"))))
2395 (define-key gdb-memory-unit-menu [words]
2396 '(menu-item "Words" gdb-memory-unit-word
2397 :button (:radio . (equal gdb-memory-unit "w"))))
2398 (define-key gdb-memory-unit-menu [halfwords]
2399 '(menu-item "Halfwords" gdb-memory-unit-halfword
2400 :button (:radio . (equal gdb-memory-unit "h"))))
2401 (define-key gdb-memory-unit-menu [bytes]
2402 '(menu-item "Bytes" gdb-memory-unit-byte
2403 :button (:radio . (equal gdb-memory-unit "b"))))
2405 (defun gdb-memory-unit-menu (event)
2407 (x-popup-menu event gdb-memory-unit-menu))
2409 (defun gdb-memory-unit-menu-1 (event)
2411 (save-selected-window
2412 (select-window (posn-window (event-start event)))
2413 (let* ((selection (gdb-memory-unit-menu event))
2414 (binding (and selection (lookup-key gdb-memory-unit-menu
2415 (vector (car selection))))))
2416 (if binding (call-interactively binding)))))
2418 ;;from make-mode-line-mouse-map
2419 (defun gdb-make-header-line-mouse-map (mouse function) "\
2420 Return a keymap with single entry for mouse key MOUSE on the header line.
2421 MOUSE is defined to run function FUNCTION with no args in the buffer
2422 corresponding to the mode line clicked."
2423 (let ((map (make-sparse-keymap)))
2424 (define-key map (vector 'header-line mouse) function)
2425 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2428 (defvar gdb-memory-font-lock-keywords
2429 '(;; <__function.name+n>
2430 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2432 "Font lock keywords used in `gdb-memory-mode'.")
2434 (defun gdb-memory-mode ()
2435 "Major mode for examining memory.
2437 \\{gdb-memory-mode-map}"
2438 (kill-all-local-variables)
2439 (setq major-mode 'gdb-memory-mode)
2440 (setq mode-name "Memory")
2441 (setq buffer-read-only t)
2442 (use-local-map gdb-memory-mode-map)
2443 (setq header-line-format
2449 'face font-lock-warning-face
2450 'help-echo "mouse-1: Decrement address"
2451 'mouse-face 'mode-line-highlight
2453 (gdb-make-header-line-mouse-map
2455 #'(lambda () (interactive)
2456 (let ((gdb-memory-address
2457 ;; Let GDB do the arithmetic.
2459 gdb-memory-address " - "
2461 (* gdb-memory-repeat-count
2462 (cond ((string= gdb-memory-unit "b") 1)
2463 ((string= gdb-memory-unit "h") 2)
2464 ((string= gdb-memory-unit "w") 4)
2465 ((string= gdb-memory-unit "g") 8)))))))
2466 (gdb-invalidate-memory)))))
2469 'face font-lock-warning-face
2470 'help-echo "mouse-1: Increment address"
2471 'mouse-face 'mode-line-highlight
2472 'local-map (gdb-make-header-line-mouse-map
2474 #'(lambda () (interactive)
2475 (let ((gdb-memory-address nil))
2476 (gdb-invalidate-memory)))))
2478 (propertize gdb-memory-address
2479 'face font-lock-warning-face
2480 'help-echo "mouse-1: Set memory address"
2481 'mouse-face 'mode-line-highlight
2482 'local-map (gdb-make-header-line-mouse-map
2484 #'gdb-memory-set-address))
2486 (propertize (number-to-string gdb-memory-repeat-count)
2487 'face font-lock-warning-face
2488 'help-echo "mouse-1: Set repeat count"
2489 'mouse-face 'mode-line-highlight
2490 'local-map (gdb-make-header-line-mouse-map
2492 #'gdb-memory-set-repeat-count))
2494 (propertize gdb-memory-format
2495 'face font-lock-warning-face
2496 'help-echo "mouse-3: Select display format"
2497 'mouse-face 'mode-line-highlight
2498 'local-map gdb-memory-format-map)
2500 (propertize gdb-memory-unit
2501 'face font-lock-warning-face
2502 'help-echo "mouse-3: Select unit size"
2503 'mouse-face 'mode-line-highlight
2504 'local-map gdb-memory-unit-map))))
2505 (set (make-local-variable 'font-lock-defaults)
2506 '(gdb-memory-font-lock-keywords))
2507 (run-mode-hooks 'gdb-memory-mode-hook)
2508 'gdb-invalidate-memory)
2510 (defun gdb-memory-buffer-name ()
2511 (with-current-buffer gud-comint-buffer
2512 (concat "*memory of " (gdb-get-target-string) "*")))
2514 (defun gdb-display-memory-buffer ()
2515 "Display memory contents."
2518 (gdb-get-buffer-create 'gdb-memory-buffer)))
2520 (defun gdb-frame-memory-buffer ()
2521 "Display memory contents in a new frame."
2523 (let ((special-display-regexps (append special-display-regexps '(".*")))
2524 (special-display-frame-alist gdb-frame-parameters))
2525 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2530 (gdb-set-buffer-rules 'gdb-locals-buffer
2531 'gdb-locals-buffer-name
2534 (def-gdb-auto-update-trigger gdb-invalidate-locals
2535 (gdb-get-buffer 'gdb-locals-buffer)
2536 "server info locals\n"
2537 gdb-info-locals-handler)
2539 (defvar gdb-locals-watch-map
2540 (let ((map (make-sparse-keymap)))
2541 (define-key map "\r" '(lambda () (interactive)
2544 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2545 (mouse-set-point event)
2549 "Keymap to create watch expression of a complex data type local variable.")
2551 (defconst gdb-struct-string
2552 (concat (propertize "[struct/union]"
2553 'mouse-face 'highlight
2554 'help-echo "mouse-2: create watch expression"
2555 'local-map gdb-locals-watch-map) "\n"))
2557 (defconst gdb-array-string
2558 (concat " " (propertize "[array]"
2559 'mouse-face 'highlight
2560 'help-echo "mouse-2: create watch expression"
2561 'local-map gdb-locals-watch-map) "\n"))
2563 ;; Abbreviate for arrays and structures.
2564 ;; These can be expanded using gud-display.
2565 (defun gdb-info-locals-handler ()
2566 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2567 gdb-pending-triggers))
2568 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2569 (with-current-buffer buf
2570 (goto-char (point-min))
2571 (while (re-search-forward "^[ }].*\n" nil t)
2572 (replace-match "" nil nil))
2573 (goto-char (point-min))
2574 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2575 (replace-match gdb-struct-string nil nil))
2576 (goto-char (point-min))
2577 (while (re-search-forward "\\s-*{.*\n" nil t)
2578 (replace-match gdb-array-string nil nil))))
2579 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2581 (with-current-buffer buf
2582 (let* ((window (get-buffer-window buf 0))
2583 (start (window-start window))
2584 (p (window-point window))
2585 (buffer-read-only nil))
2587 (insert-buffer-substring (gdb-get-buffer-create
2588 'gdb-partial-output-buffer))
2589 (set-window-start window start)
2590 (set-window-point window p))
2592 (run-hooks 'gdb-info-locals-hook))
2594 (defvar gdb-locals-mode-map
2595 (let ((map (make-sparse-keymap)))
2596 (suppress-keymap map)
2597 (define-key map "q" 'kill-this-buffer)
2600 (defun gdb-locals-mode ()
2601 "Major mode for gdb locals.
2603 \\{gdb-locals-mode-map}"
2604 (kill-all-local-variables)
2605 (setq major-mode 'gdb-locals-mode)
2606 (setq mode-name (concat "Locals:" gdb-selected-frame))
2607 (setq buffer-read-only t)
2608 (use-local-map gdb-locals-mode-map)
2609 (set (make-local-variable 'font-lock-defaults)
2610 '(gdb-locals-font-lock-keywords))
2611 (run-mode-hooks 'gdb-locals-mode-hook)
2612 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2613 (string-equal gdb-version "pre-6.4"))
2614 'gdb-invalidate-locals
2615 'gdb-invalidate-locals-1))
2617 (defun gdb-locals-buffer-name ()
2618 (with-current-buffer gud-comint-buffer
2619 (concat "*locals of " (gdb-get-target-string) "*")))
2621 (defun gdb-display-locals-buffer ()
2622 "Display local variables of current stack and their values."
2625 (gdb-get-buffer-create 'gdb-locals-buffer)))
2627 (defun gdb-frame-locals-buffer ()
2628 "Display local variables of current stack and their values in a new frame."
2630 (let ((special-display-regexps (append special-display-regexps '(".*")))
2631 (special-display-frame-alist gdb-frame-parameters))
2632 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2635 ;;;; Window management
2636 (defun gdb-display-buffer (buf &optional size)
2637 (let ((answer (get-buffer-window buf 0))
2640 (display-buffer buf nil 0) ;Raise the frame if necessary.
2641 ;; The buffer is not yet displayed.
2642 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2643 (let ((window (get-lru-window)))
2645 (not (eq window (get-buffer-window gud-comint-buffer))))
2647 (set-window-buffer window buf)
2648 (setq answer window))
2649 (setq must-split t)))
2651 (let* ((largest (get-largest-window))
2652 (cur-size (window-height largest))
2653 (new-size (and size (< size cur-size) (- cur-size size))))
2654 (setq answer (split-window largest new-size))
2655 (set-window-buffer answer buf)
2656 (set-window-dedicated-p answer t)))
2660 ;;; Shared keymap initialization:
2662 (let ((menu (make-sparse-keymap "GDB-Windows")))
2663 (define-key gud-menu-map [displays]
2664 `(menu-item "GDB-Windows" ,menu
2665 :visible (memq gud-minor-mode '(gdbmi gdba))))
2666 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2667 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2668 (define-key menu [inferior]
2669 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2670 :enable gdb-use-separate-io-buffer))
2671 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2672 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2673 (define-key menu [disassembly]
2674 '("Disassembly" . gdb-display-assembler-buffer))
2675 (define-key menu [breakpoints]
2676 '("Breakpoints" . gdb-display-breakpoints-buffer))
2677 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2678 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2680 (let ((menu (make-sparse-keymap "GDB-Frames")))
2681 (define-key gud-menu-map [frames]
2682 `(menu-item "GDB-Frames" ,menu
2683 :visible (memq gud-minor-mode '(gdbmi gdba))))
2684 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2685 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2686 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2687 (define-key menu [inferior]
2688 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2689 :enable gdb-use-separate-io-buffer))
2690 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2691 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2692 (define-key menu [breakpoints]
2693 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2694 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2695 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2697 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2698 (define-key gud-menu-map [ui]
2699 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2700 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2701 (define-key menu [gdb-use-separate-io]
2702 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2703 :visible (eq gud-minor-mode 'gdba)
2704 :help "Toggle separate IO for inferior."
2705 :button (:toggle . gdb-use-separate-io-buffer)))
2706 (define-key menu [gdb-many-windows]
2707 '(menu-item "Display Other Windows" gdb-many-windows
2708 :help "Toggle display of locals, stack and breakpoint information"
2709 :button (:toggle . gdb-many-windows)))
2710 (define-key menu [gdb-restore-windows]
2711 '(menu-item "Restore Window Layout" gdb-restore-windows
2712 :help "Restore standard layout for debug session.")))
2714 (defun gdb-frame-gdb-buffer ()
2715 "Display GUD buffer in a new frame."
2717 (let ((special-display-regexps (append special-display-regexps '(".*")))
2718 (special-display-frame-alist gdb-frame-parameters)
2719 (same-window-regexps nil))
2720 (display-buffer gud-comint-buffer)))
2722 (defun gdb-display-gdb-buffer ()
2723 "Display GUD buffer."
2725 (let ((same-window-regexps nil))
2726 (pop-to-buffer gud-comint-buffer)))
2728 (defun gdb-set-window-buffer (name)
2729 (set-window-buffer (selected-window) (get-buffer name))
2730 (set-window-dedicated-p (selected-window) t))
2732 (defun gdb-setup-windows ()
2733 "Layout the window pattern for `gdb-many-windows'."
2734 (gdb-display-locals-buffer)
2735 (gdb-display-stack-buffer)
2736 (delete-other-windows)
2737 (gdb-display-breakpoints-buffer)
2738 (delete-other-windows)
2740 (pop-to-buffer gud-comint-buffer)
2741 (split-window nil ( / ( * (window-height) 3) 4))
2742 (split-window nil ( / (window-height) 3))
2743 (split-window-horizontally)
2745 (gdb-set-window-buffer (gdb-locals-buffer-name))
2748 (if gud-last-last-frame
2749 (gud-find-file (car gud-last-last-frame))
2751 (gud-find-file gdb-main-file)
2752 ;; Put buffer list in window if we
2753 ;; can't find a source file.
2754 (list-buffers-noselect))))
2755 (when gdb-use-separate-io-buffer
2756 (split-window-horizontally)
2758 (gdb-set-window-buffer
2759 (gdb-get-buffer-create 'gdb-inferior-io)))
2761 (gdb-set-window-buffer (gdb-stack-buffer-name))
2762 (split-window-horizontally)
2764 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2767 (defun gdb-restore-windows ()
2768 "Restore the basic arrangement of windows used by gdba.
2769 This arrangement depends on the value of `gdb-many-windows'."
2771 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2772 (delete-other-windows)
2773 (if gdb-many-windows
2775 (when (or gud-last-last-frame gdb-show-main)
2779 (if gud-last-last-frame
2780 (gud-find-file (car gud-last-last-frame))
2781 (gud-find-file gdb-main-file)))
2785 "Exit a debugging session cleanly.
2786 Kills the gdb buffers, and resets variables and the source buffers."
2787 (dolist (buffer (buffer-list))
2788 (unless (eq buffer gud-comint-buffer)
2789 (with-current-buffer buffer
2790 (if (memq gud-minor-mode '(gdbmi gdba))
2791 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2793 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2794 (setq gud-minor-mode nil)
2795 (kill-local-variable 'tool-bar-map)
2796 (kill-local-variable 'gdb-define-alist))))))
2797 (when (markerp gdb-overlay-arrow-position)
2798 (move-marker gdb-overlay-arrow-position nil)
2799 (setq gdb-overlay-arrow-position nil))
2800 (setq overlay-arrow-variable-list
2801 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2802 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2803 (if (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
2805 (setq gud-running nil)
2806 (setq gdb-active-process nil)
2807 (setq gdb-var-list nil)
2808 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2810 (defun gdb-source-info ()
2811 "Find the source file where the program starts and displays it with related
2813 (goto-char (point-min))
2814 (if (and (search-forward "Located in " nil t)
2815 (looking-at "\\S-+"))
2816 (setq gdb-main-file (match-string 0)))
2817 (goto-char (point-min))
2818 (if (search-forward "Includes preprocessor macro info." nil t)
2819 (setq gdb-macro-info t))
2820 (if gdb-many-windows
2822 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2824 (let ((pop-up-windows t))
2825 (display-buffer (gud-find-file gdb-main-file))))))
2827 (defun gdb-get-location (bptno line flag)
2828 "Find the directory containing the relevant source file.
2829 Put in buffer and place breakpoint icon."
2830 (goto-char (point-min))
2831 (catch 'file-not-found
2832 (if (search-forward "Located in " nil t)
2833 (when (looking-at "\\S-+")
2834 (delete (cons bptno "File not found") gdb-location-alist)
2835 (push (cons bptno (match-string 0)) gdb-location-alist))
2837 (unless (assoc bptno gdb-location-alist)
2838 (push (cons bptno "File not found") gdb-location-alist)
2839 (message-box "Cannot find source file for breakpoint location.\n\
2840 Add directory to search path for source files using the GDB command, dir."))
2841 (throw 'file-not-found nil))
2842 (with-current-buffer
2843 (find-file-noselect (match-string 0))
2844 (save-current-buffer
2845 (set (make-local-variable 'gud-minor-mode) 'gdba)
2846 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2847 ;; only want one breakpoint icon at each location
2849 (goto-line (string-to-number line))
2850 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2852 (add-hook 'find-file-hook 'gdb-find-file-hook)
2854 (defun gdb-find-file-hook ()
2855 "Set up buffer for debugging if file is part of the source code
2856 of the current session."
2857 (if (and (buffer-name gud-comint-buffer)
2858 ;; in case gud or gdb-ui is just loaded
2860 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2862 (if (member buffer-file-name gdb-source-file-list)
2863 (with-current-buffer (find-buffer-visiting buffer-file-name)
2864 (set (make-local-variable 'gud-minor-mode)
2865 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2866 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2869 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2870 "Put string PUTSTRING in front of POS in the current buffer.
2871 PUTSTRING is displayed by putting an overlay into the current buffer with a
2872 `before-string' string that has a `display' property whose value is
2874 (let ((string (make-string 1 ?x))
2875 (buffer (current-buffer)))
2876 (setq putstring (copy-sequence putstring))
2877 (let ((overlay (make-overlay pos pos buffer))
2879 (list (list 'margin 'left-margin) putstring))))
2880 (put-text-property 0 1 'display prop string)
2882 (add-text-properties 0 1 sprops string))
2883 (overlay-put overlay 'put-break t)
2884 (overlay-put overlay 'before-string string))))
2886 ;;from remove-images
2887 (defun gdb-remove-strings (start end &optional buffer)
2888 "Remove strings between START and END in BUFFER.
2889 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2890 BUFFER nil or omitted means use the current buffer."
2892 (setq buffer (current-buffer)))
2893 (dolist (overlay (overlays-in start end))
2894 (when (overlay-get overlay 'put-break)
2895 (delete-overlay overlay))))
2897 (defun gdb-put-breakpoint-icon (enabled bptno)
2898 (let ((start (- (line-beginning-position) 1))
2899 (end (+ (line-end-position) 1))
2900 (putstring (if enabled "B" "b"))
2901 (source-window (get-buffer-window (current-buffer) 0)))
2902 (add-text-properties
2903 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2906 (add-text-properties
2907 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2908 (add-text-properties
2909 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2910 (gdb-remove-breakpoint-icons start end)
2911 (if (display-images-p)
2912 (if (>= (or left-fringe-width
2913 (if source-window (car (window-fringes source-window)))
2914 gdb-buffer-fringe-width) 8)
2917 `(left-fringe breakpoint
2920 'breakpoint-disabled))
2922 'gdb-enabled enabled)
2923 (when (< left-margin-width 2)
2924 (save-current-buffer
2925 (setq left-margin-width 2)
2929 left-margin-width right-margin-width))))
2932 (or breakpoint-enabled-icon
2933 (setq breakpoint-enabled-icon
2934 (find-image `((:type xpm :data
2935 ,breakpoint-xpm-data
2936 :ascent 100 :pointer hand)
2938 ,breakpoint-enabled-pbm-data
2939 :ascent 100 :pointer hand)))))
2940 (or breakpoint-disabled-icon
2941 (setq breakpoint-disabled-icon
2942 (find-image `((:type xpm :data
2943 ,breakpoint-xpm-data
2944 :conversion disabled
2945 :ascent 100 :pointer hand)
2947 ,breakpoint-disabled-pbm-data
2948 :ascent 100 :pointer hand))))))
2952 (when (< left-margin-width 2)
2953 (save-current-buffer
2954 (setq left-margin-width 2)
2955 (let ((window (get-buffer-window (current-buffer) 0)))
2958 window left-margin-width right-margin-width)))))
2960 (propertize putstring
2961 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2964 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2965 (gdb-remove-strings start end)
2966 (if (display-images-p)
2967 (remove-images start end))
2969 (setq left-margin-width 0)
2970 (let ((window (get-buffer-window (current-buffer) 0)))
2973 window left-margin-width right-margin-width)))))
2977 ;; Assembler buffer.
2979 (gdb-set-buffer-rules 'gdb-assembler-buffer
2980 'gdb-assembler-buffer-name
2981 'gdb-assembler-mode)
2983 ;; We can't use def-gdb-auto-update-handler because we don't want to use
2984 ;; window-start but keep the overlay arrow/current line visible.
2985 (defun gdb-assembler-handler ()
2986 (setq gdb-pending-triggers
2987 (delq 'gdb-invalidate-assembler
2988 gdb-pending-triggers))
2989 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
2991 (with-current-buffer buf
2992 (let* ((window (get-buffer-window buf 0))
2993 (p (window-point window))
2994 (buffer-read-only nil))
2996 (insert-buffer-substring (gdb-get-buffer-create
2997 'gdb-partial-output-buffer))
2998 (set-window-point window p)))))
2999 ;; put customisation here
3000 (gdb-assembler-custom))
3002 (defun gdb-assembler-custom ()
3003 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3004 (pos 1) (address) (flag) (bptno))
3005 (with-current-buffer buffer
3007 (if (not (equal gdb-frame-address "main"))
3009 (goto-char (point-min))
3010 (if (and gdb-frame-address
3011 (search-forward gdb-frame-address nil t))
3015 (setq fringe-indicator-alist
3016 (if (string-equal gdb-frame-number "0")
3018 '((overlay-arrow . hollow-right-triangle))))
3019 (or gdb-overlay-arrow-position
3020 (setq gdb-overlay-arrow-position (make-marker)))
3021 (set-marker gdb-overlay-arrow-position
3022 (point) (current-buffer))))))
3023 ;; remove all breakpoint-icons in assembler buffer before updating.
3024 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3025 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3026 (goto-char (point-min))
3027 (while (< (point) (- (point-max) 1))
3029 (if (looking-at "[^\t].*?breakpoint")
3032 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3033 (setq bptno (match-string 1))
3034 (setq flag (char-after (match-beginning 2)))
3035 (setq address (match-string 3))
3036 (with-current-buffer buffer
3038 (goto-char (point-min))
3039 (if (search-forward address nil t)
3040 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3041 (if (not (equal gdb-frame-address "main"))
3042 (with-current-buffer buffer
3043 (set-window-point (get-buffer-window buffer 0) pos)))))
3045 (defvar gdb-assembler-mode-map
3046 (let ((map (make-sparse-keymap)))
3047 (suppress-keymap map)
3048 (define-key map "q" 'kill-this-buffer)
3051 (defvar gdb-assembler-font-lock-keywords
3052 '(;; <__function.name+n>
3053 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3054 (1 font-lock-function-name-face))
3055 ;; 0xNNNNNNNN <__function.name+n>: opcode
3056 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3057 (4 font-lock-keyword-face))
3058 ;; %register(at least i386)
3059 ("%\\sw+" . font-lock-variable-name-face)
3060 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3061 (1 font-lock-comment-face)
3062 (2 font-lock-function-name-face))
3063 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3064 "Font lock keywords used in `gdb-assembler-mode'.")
3066 (defun gdb-assembler-mode ()
3067 "Major mode for viewing code assembler.
3069 \\{gdb-assembler-mode-map}"
3070 (kill-all-local-variables)
3071 (setq major-mode 'gdb-assembler-mode)
3072 (setq mode-name (concat "Machine:" gdb-selected-frame))
3073 (setq gdb-overlay-arrow-position nil)
3074 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3075 (setq fringes-outside-margins t)
3076 (setq buffer-read-only t)
3077 (use-local-map gdb-assembler-mode-map)
3078 (gdb-invalidate-assembler)
3079 (set (make-local-variable 'font-lock-defaults)
3080 '(gdb-assembler-font-lock-keywords))
3081 (run-mode-hooks 'gdb-assembler-mode-hook)
3082 'gdb-invalidate-assembler)
3084 (defun gdb-assembler-buffer-name ()
3085 (with-current-buffer gud-comint-buffer
3086 (concat "*disassembly of " (gdb-get-target-string) "*")))
3088 (defun gdb-display-assembler-buffer ()
3089 "Display disassembly view."
3091 (setq gdb-previous-frame nil)
3093 (gdb-get-buffer-create 'gdb-assembler-buffer)))
3095 (defun gdb-frame-assembler-buffer ()
3096 "Display disassembly view in a new frame."
3098 (setq gdb-previous-frame nil)
3099 (let ((special-display-regexps (append special-display-regexps '(".*")))
3100 (special-display-frame-alist gdb-frame-parameters))
3101 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3103 ;; modified because if gdb-frame-address has changed value a new command
3104 ;; must be enqueued to update the buffer with the new output
3105 (defun gdb-invalidate-assembler (&optional ignored)
3106 (if (gdb-get-buffer 'gdb-assembler-buffer)
3108 (unless (and gdb-selected-frame
3109 (string-equal gdb-selected-frame gdb-previous-frame))
3110 (if (or (not (member 'gdb-invalidate-assembler
3111 gdb-pending-triggers))
3112 (not (string-equal gdb-frame-address
3113 gdb-previous-frame-address)))
3115 ;; take previous disassemble command, if any, off the queue
3116 (with-current-buffer gud-comint-buffer
3117 (let ((queue gdb-input-queue))
3118 (dolist (item queue)
3119 (if (equal (cdr item) '(gdb-assembler-handler))
3120 (setq gdb-input-queue
3121 (delete item gdb-input-queue))))))
3124 (concat gdb-server-prefix "disassemble "
3125 (if (member gdb-frame-address '(nil "main")) nil "0x")
3126 gdb-frame-address "\n")
3127 'gdb-assembler-handler))
3128 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3129 (setq gdb-previous-frame-address gdb-frame-address)
3130 (setq gdb-previous-frame gdb-selected-frame)))))))
3132 (defun gdb-get-selected-frame ()
3133 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3136 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3137 (push 'gdb-get-selected-frame
3138 gdb-pending-triggers))))
3140 (defun gdb-frame-handler ()
3141 (setq gdb-pending-triggers
3142 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3143 (goto-char (point-min))
3144 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
3145 (setq gdb-frame-number (match-string 1)))
3146 (goto-char (point-min))
3147 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-+?\\)\
3148 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*;? "
3150 (setq gdb-selected-frame (match-string 2))
3151 (if (gdb-get-buffer 'gdb-locals-buffer)
3152 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3153 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3154 (if (gdb-get-buffer 'gdb-assembler-buffer)
3155 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3156 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3157 (setq gdb-frame-address (match-string 1))
3158 (if (and (match-string 3) gud-overlay-arrow-position)
3159 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3160 (position (marker-position gud-overlay-arrow-position)))
3161 (when (and buffer (string-equal (buffer-name buffer) (match-string 3)))
3162 (with-current-buffer buffer
3163 (setq fringe-indicator-alist
3164 (if (string-equal gdb-frame-number "0")
3166 '((overlay-arrow . hollow-right-triangle))))
3167 (set-marker gud-overlay-arrow-position position))))))
3168 (goto-char (point-min))
3169 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3170 (setq gdb-current-language (match-string 1)))
3171 (gdb-invalidate-assembler))
3174 ;; Code specific to GDB 6.4
3175 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3177 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3178 "Create list of source files for current GDB session.
3179 If buffers already exist for any of these files, gud-minor-mode
3181 (goto-char (point-min))
3182 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3183 (push (match-string 1) gdb-source-file-list))
3184 (dolist (buffer (buffer-list))
3185 (with-current-buffer buffer
3186 (when (member buffer-file-name gdb-source-file-list)
3187 (set (make-local-variable 'gud-minor-mode)
3188 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3189 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3190 (when gud-tooltip-mode
3191 (make-local-variable 'gdb-define-alist)
3192 (gdb-create-define-alist)
3193 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
3195 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3196 (defun gdb-var-list-children-1 (varnum)
3199 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3200 (concat "server interpreter mi \"-var-list-children --all-values "
3202 (concat "-var-list-children --all-values " varnum "\n"))
3203 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3205 (defconst gdb-var-list-children-regexp-1
3206 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3207 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3209 (defun gdb-var-list-children-handler-1 (varnum)
3210 (goto-char (point-min))
3211 (let ((var-list nil))
3212 (catch 'child-already-watched
3213 (dolist (var gdb-var-list)
3214 (if (string-equal varnum (car var))
3217 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3218 (let ((varchild (list (match-string 1)
3222 (read (match-string 4))
3224 (if (assoc (car varchild) gdb-var-list)
3225 (throw 'child-already-watched nil))
3226 (push varchild var-list))))
3227 (push var var-list)))
3228 (setq gdb-var-list (nreverse var-list)))))
3230 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3231 (defun gdb-var-update-1 ()
3232 (if (not (member 'gdb-var-update gdb-pending-triggers))
3236 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3237 "server interpreter mi \"-var-update --all-values *\"\n"
3238 "-var-update --all-values *\n")
3239 'gdb-var-update-handler-1))
3240 (push 'gdb-var-update gdb-pending-triggers))))
3242 (defconst gdb-var-update-regexp-1
3243 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3244 in_scope=\"\\(.*?\\)\".*?}")
3246 (defun gdb-var-update-handler-1 ()
3247 (dolist (var gdb-var-list)
3248 (setcar (nthcdr 5 var) nil))
3249 (goto-char (point-min))
3250 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3251 (let* ((varnum (match-string 1))
3252 (var (assoc varnum gdb-var-list)))
3254 (if (string-equal (match-string 3) "false")
3255 (setcar (nthcdr 5 var) 'out-of-scope)
3256 (setcar (nthcdr 5 var) 'changed)
3257 (setcar (nthcdr 4 var)
3258 (read (match-string 2)))))))
3259 (setq gdb-pending-triggers
3260 (delq 'gdb-var-update gdb-pending-triggers))
3261 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3262 ;; dummy command to update speedbar at right time
3263 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
3264 ;; keep gdb-pending-triggers non-nil till end
3265 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
3267 ;; Registers buffer.
3269 (gdb-set-buffer-rules 'gdb-registers-buffer
3270 'gdb-registers-buffer-name
3271 'gdb-registers-mode)
3273 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3274 (gdb-get-buffer 'gdb-registers-buffer)
3275 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3276 "server interpreter mi \"-data-list-register-values x\"\n"
3277 "-data-list-register-values x\n")
3278 gdb-data-list-register-values-handler)
3280 (defconst gdb-data-list-register-values-regexp
3281 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3283 (defun gdb-data-list-register-values-handler ()
3284 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3285 gdb-pending-triggers))
3286 (goto-char (point-min))
3287 (if (re-search-forward gdb-error-regexp nil t)
3288 (let ((err (match-string 1)))
3289 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3290 (let ((buffer-read-only nil))
3293 (goto-char (point-min)))))
3294 (let ((register-list (reverse gdb-register-names))
3295 (register nil) (register-string nil) (register-values nil))
3296 (goto-char (point-min))
3297 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3298 (setq register (pop register-list))
3299 (setq register-string (concat register "\t" (match-string 2) "\n"))
3300 (if (member (match-string 1) gdb-changed-registers)
3301 (put-text-property 0 (length register-string)
3302 'face 'font-lock-warning-face
3304 (setq register-values
3305 (concat register-values register-string)))
3306 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3307 (with-current-buffer buf
3308 (let* ((window (get-buffer-window buf 0))
3309 (start (window-start window))
3310 (p (window-point window))
3311 (buffer-read-only nil))
3313 (insert register-values)
3314 (set-window-start window start)
3315 (set-window-point window p))))))
3316 (gdb-data-list-register-values-custom))
3318 (defun gdb-data-list-register-values-custom ()
3319 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3321 (let ((buffer-read-only nil)
3323 (goto-char (point-min))
3324 (while (< (point) (point-max))
3325 (setq start (line-beginning-position))
3326 (setq end (line-end-position))
3327 (when (looking-at "^[^\t]+")
3328 (unless (string-equal (match-string 0) "No registers.")
3329 (put-text-property start (match-end 0)
3330 'face font-lock-variable-name-face)
3331 (add-text-properties start end
3332 '(help-echo "mouse-2: edit value"
3333 mouse-face highlight))))
3334 (forward-line 1))))))
3336 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3337 (defun gdb-get-changed-registers ()
3338 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3339 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3343 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3344 "server interpreter mi -data-list-changed-registers\n"
3345 "-data-list-changed-registers\n")
3346 'gdb-get-changed-registers-handler))
3347 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3349 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3351 (defun gdb-get-changed-registers-handler ()
3352 (setq gdb-pending-triggers
3353 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3354 (setq gdb-changed-registers nil)
3355 (goto-char (point-min))
3356 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3357 (push (match-string 1) gdb-changed-registers)))
3362 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3363 (gdb-set-buffer-rules 'gdb-locals-buffer
3364 'gdb-locals-buffer-name
3367 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3368 (gdb-get-buffer 'gdb-locals-buffer)
3369 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3370 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3371 "-stack-list-locals --simple-values\n")
3372 gdb-stack-list-locals-handler)
3374 (defconst gdb-stack-list-locals-regexp
3375 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3377 (defvar gdb-locals-watch-map-1
3378 (let ((map (make-sparse-keymap)))
3379 (define-key map [mouse-2] 'gud-watch)
3381 "Keymap to create watch expression of a complex data type local variable.")
3383 ;; Dont display values of arrays or structures.
3384 ;; These can be expanded using gud-watch.
3385 (defun gdb-stack-list-locals-handler ()
3386 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3387 gdb-pending-triggers))
3388 (let (local locals-list)
3389 (goto-char (point-min))
3390 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3391 (let ((local (list (match-string 1)
3394 (if (looking-at ",value=\\(\".*\"\\).*?}")
3395 (setcar (nthcdr 2 local) (read (match-string 1))))
3396 (push local locals-list)))
3397 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3398 (and buf (with-current-buffer buf
3399 (let* ((window (get-buffer-window buf 0))
3400 (start (window-start window))
3401 (p (window-point window))
3402 (buffer-read-only nil))
3404 (dolist (local locals-list)
3405 (setq name (car local))
3406 (if (or (not (nth 2 local))
3407 (string-match "\\0x" (nth 2 local)))
3408 (add-text-properties 0 (length name)
3409 `(mouse-face highlight
3410 help-echo "mouse-2: create watch expression"
3411 local-map ,gdb-locals-watch-map-1)
3414 (concat name "\t" (nth 1 local)
3415 "\t" (nth 2 local) "\n")))
3416 (set-window-start window start)
3417 (set-window-point window p)))))))
3419 (defun gdb-get-register-names ()
3420 "Create a list of register names."
3421 (goto-char (point-min))
3422 (setq gdb-register-names nil)
3423 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3424 (push (match-string 1) gdb-register-names)))
3428 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3429 ;;; gdb-ui.el ends here