1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006
8 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This mode acts as a graphical user interface to GDB. You can interact with
30 ;; GDB through the GUD buffer in the usual way, but there are also further
31 ;; buffers which control the execution and describe the state of your program.
32 ;; It separates the input/output of your program from that of GDB, if
33 ;; required, and watches expressions in the speedbar. It also uses features of
34 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
35 ;; (see the GDB Graphical Interface section in the Emacs info manual).
37 ;; By default, M-x gdb will start the debugger. However, if you have customised
38 ;; gud-gdb-command-name, then start it with M-x gdba.
40 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
41 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
42 ;; You don't need to know about annotations to use this mode as a debugger,
43 ;; but if you are interested developing the mode itself, then see the
44 ;; Annotations section in the GDB info manual.
46 ;; GDB developers plan to make the annotation interface obsolete. A new
47 ;; interface called GDB/MI (machine interface) has been designed to replace
48 ;; it. Some GDB/MI commands are used in this file through the CLI command
49 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
50 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
51 ;; still under development and is part of a process to migrate Emacs from
52 ;; annotations to GDB/MI.
54 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
55 ;; onwards to use watch expressions. It works best with GDB 6.4 where
56 ;; watch expressions will update more quickly.
58 ;;; Windows Platforms:
60 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
61 ;; explicitly in your program if you want timely display of I/O in Emacs.
62 ;; Alternatively you can make the output stream unbuffered, for example, by
66 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
69 ;; and compiling with -DUNBUFFERED while debugging.
73 ;; 1) Strings that are watched don't update in the speedbar when their
74 ;; contents change unless the first character changes.
75 ;; 2) Cannot handle multiple debug sessions.
76 ;; 3) Initially, the assembler buffer does not display the cursor at the
77 ;; current line if the line is not visible in the window (but when testing
78 ;; gdb-assembler-custom with a lisp debugger it does!).
80 ;;; Problems with watch expressions, GDB/MI:
82 ;; 1) They go out of scope when the inferior is re-run.
83 ;; 2) -stack-list-locals has a type field but also prints type in values field.
84 ;; 3) VARNUM increments even when variable object is not created (maybe trivial).
88 ;; 1) Use MI command -data-read-memory for memory window.
89 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
90 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
91 ;; 4) With gud-print and gud-pstar, print the variable name in the GUD
92 ;; buffer instead of the value's history number.
99 (defvar speedbar-initial-expansion-list-name)
101 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
102 (defvar gdb-previous-frame-address nil)
103 (defvar gdb-memory-address "main")
104 (defvar gdb-previous-frame nil)
105 (defvar gdb-selected-frame nil)
106 (defvar gdb-frame-number nil)
107 (defvar gdb-current-language nil)
108 (defvar gdb-var-list nil
109 "List of variables in watch window.
110 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS) where
111 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
112 (defvar gdb-force-update t
113 "Non-nil means that view of watch expressions will be updated in the speedbar.")
114 (defvar gdb-main-file nil "Source file from which program execution begins.")
115 (defvar gdb-overlay-arrow-position nil)
116 (defvar gdb-server-prefix nil)
117 (defvar gdb-flush-pending-output nil)
118 (defvar gdb-location-alist nil
119 "Alist of breakpoint numbers and full filenames.")
120 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
121 and #define directives otherwise.")
122 (defvar gdb-error "Non-nil when GDB is reporting an error.")
123 (defvar gdb-macro-info nil
124 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
125 (defvar gdb-buffer-fringe-width nil)
126 (defvar gdb-signalled nil)
127 (defvar gdb-source-window nil)
128 (defvar gdb-inferior-status nil)
130 (defvar gdb-buffer-type nil
131 "One of the symbols bound in `gdb-buffer-rules'.")
132 (make-variable-buffer-local 'gdb-buffer-type)
134 (defvar gdb-input-queue ()
135 "A list of gdb command objects.")
137 (defvar gdb-prompting nil
138 "True when gdb is idle with no pending input.")
140 (defvar gdb-output-sink 'user
141 "The disposition of the output of the current gdb command.
142 Possible values are these symbols:
144 `user' -- gdb output should be copied to the GUD buffer
147 `inferior' -- gdb output should be copied to the inferior-io buffer.
149 `pre-emacs' -- output should be ignored util the post-prompt
150 annotation is received. Then the output-sink
152 `emacs' -- output should be collected in the partial-output-buffer
153 for subsequent processing by a command. This is the
154 disposition of output generated by commands that
155 gdb mode sends to gdb on its own behalf.
156 `post-emacs' -- ignore output until the prompt annotation is
157 received, then go to USER disposition.
159 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
160 \(`user' and `emacs').")
162 (defvar gdb-current-item nil
163 "The most recent command item sent to gdb.")
165 (defvar gdb-pending-triggers '()
166 "A list of trigger functions that have run later than their output
169 (defvar gdb-first-post-prompt nil)
170 (defvar gdb-version nil)
171 (defvar gdb-locals-font-lock-keywords nil)
172 (defvar gdb-source-file-list nil
173 "List of source files for the current executable")
174 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
176 (defvar gdb-locals-font-lock-keywords-1
178 ;; var = (struct struct_tag) value
179 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
180 (1 font-lock-variable-name-face)
181 (3 font-lock-keyword-face)
182 (4 font-lock-type-face))
183 ;; var = (type) value
184 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
185 (1 font-lock-variable-name-face)
186 (3 font-lock-type-face))
188 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
189 (1 font-lock-variable-name-face))
191 "Font lock keywords used in `gdb-local-mode'.")
193 (defvar gdb-locals-font-lock-keywords-2
196 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
197 (1 font-lock-variable-name-face)
198 (3 font-lock-type-face))
200 "Font lock keywords used in `gdb-local-mode'.")
202 ;; Variables for GDB 6.4+
203 (defvar gdb-register-names nil "List of register names.")
204 (defvar gdb-changed-registers nil
205 "List of changed register numbers (strings).")
208 (defun gdba (command-line)
209 "Run gdb on program FILE in buffer *gud-FILE*.
210 The directory containing FILE becomes the initial working directory
211 and source-file directory for your debugger.
213 If `gdb-many-windows' is nil (the default value) then gdb just
214 pops up the GUD buffer unless `gdb-show-main' is t. In this case
215 it starts with two windows: one displaying the GUD buffer and the
216 other with the source file with the main routine of the inferior.
218 If `gdb-many-windows' is t, regardless of the value of
219 `gdb-show-main', the layout below will appear unless
220 `gdb-use-separate-io-buffer' is nil when the source buffer
221 occupies the full width of the frame. Keybindings are shown in
224 Watch expressions appear in the speedbar/slowbar.
226 The following commands help control operation :
228 `gdb-many-windows' - Toggle the number of windows gdb uses.
229 `gdb-restore-windows' - To restore the window layout.
231 See Info node `(emacs)GDB Graphical Interface' for a more
232 detailed description of this mode.
235 +----------------------------------------------------------------------+
237 +-----------------------------------+----------------------------------+
238 | GUD buffer (I/O of GDB) | Locals buffer |
242 +-----------------------------------+----------------------------------+
243 | Source buffer | I/O buffer (of debugged program) |
251 +-----------------------------------+----------------------------------+
252 | Stack buffer | Breakpoints buffer |
253 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
254 | | RET gdb-goto-breakpoint |
255 | | D gdb-delete-breakpoint |
256 +-----------------------------------+----------------------------------+"
258 (interactive (list (gud-query-cmdline 'gdba)))
260 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
263 (defcustom gdb-debug-ring-max 128
264 "Maximum size of `gdb-debug-ring'."
269 (defvar gdb-debug-ring nil
270 "List of commands, most recent first, sent to and replies received from GDB.
271 This variable is used to debug GDB-UI.")
274 (defcustom gdb-enable-debug nil
275 "Non-nil means record the process input and output in `gdb-debug-ring'."
280 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
281 "Shell command for generating a list of defined macros in a source file.
282 This list is used to display the #define directive associated
283 with an identifier as a tooltip. It works in a debug session with
284 GDB, when gud-tooltip-mode is t.
286 Set `gdb-cpp-define-alist-flags' for any include paths or
292 (defcustom gdb-cpp-define-alist-flags ""
293 "Preprocessor flags for `gdb-cpp-define-alist-program'."
298 (defcustom gdb-show-main nil
299 "Non-nil means display source file containing the main routine at startup.
300 Also display the main routine in the disassembly buffer if present."
305 (defcustom gdb-many-windows nil
306 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
307 In this case it starts with two windows: one displaying the GUD
308 buffer and the other with the source file with the main routine
309 of the inferior. Non-nil means display the layout shown for
315 (defcustom gdb-use-separate-io-buffer nil
316 "Non-nil means display output from the inferior in a separate buffer."
321 (defun gdb-force-mode-line-update (status)
322 (let ((buffer gud-comint-buffer))
323 (if (and buffer (buffer-name buffer))
324 (with-current-buffer buffer
325 (setq mode-line-process
327 (process-status (get-buffer-process buffer)) status))
328 ;; Force mode line redisplay soon.
329 (force-mode-line-update)))))
331 (defun gdb-many-windows (arg)
332 "Toggle the number of windows in the basic arrangement.
333 With arg, display additional buffers iff arg is positive."
335 (setq gdb-many-windows
337 (not gdb-many-windows)
338 (> (prefix-numeric-value arg) 0)))
339 (message (format "Display of other windows %sabled"
340 (if gdb-many-windows "en" "dis")))
341 (if (and gud-comint-buffer
342 (buffer-name gud-comint-buffer))
344 (gdb-restore-windows)
347 (defun gdb-use-separate-io-buffer (arg)
348 "Toggle separate IO for inferior.
349 With arg, use separate IO iff arg is positive."
351 (setq gdb-use-separate-io-buffer
353 (not gdb-use-separate-io-buffer)
354 (> (prefix-numeric-value arg) 0)))
355 (message (format "Separate inferior IO %sabled"
356 (if gdb-use-separate-io-buffer "en" "dis")))
357 (if (and gud-comint-buffer
358 (buffer-name gud-comint-buffer))
360 (if gdb-use-separate-io-buffer
361 (if gdb-many-windows (gdb-restore-windows))
362 (kill-buffer (gdb-inferior-io-name)))
365 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
367 (defun gdb-create-define-alist ()
368 "Create an alist of #define directives for GUD tooltips."
369 (let* ((file (buffer-file-name))
371 (with-output-to-string
372 (with-current-buffer standard-output
373 (call-process shell-file-name
374 (if (file-exists-p file) file nil)
375 (list t nil) nil "-c"
376 (concat gdb-cpp-define-alist-program " "
377 gdb-cpp-define-alist-flags)))))
378 (define-list (split-string output "\n" t))
380 (setq gdb-define-alist nil)
381 (dolist (define define-list)
382 (setq name (nth 1 (split-string define "[( ]")))
383 (push (cons name define) gdb-define-alist))))
385 (defun gdb-tooltip-print (expr)
387 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
388 (goto-char (point-min))
390 (if (search-forward "=" nil t)
391 (concat expr (buffer-substring (- (point) 2) (point-max)))
393 ;; remove newline for gud-tooltip-echo-area
394 (substring string 0 (- (length string) 1))))
395 (or gud-tooltip-echo-area tooltip-use-echo-area)))
397 ;; If expr is a macro for a function don't print because of possible dangerous
398 ;; side-effects. Also printing a function within a tooltip generates an
399 ;; unexpected starting annotation (phase error).
400 (defun gdb-tooltip-print-1 (expr)
401 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
402 (goto-char (point-min))
403 (if (search-forward "expands to: " nil t)
404 (unless (looking-at "\\S-+.*(.*).*")
406 (list (concat gdb-server-prefix "print " expr "\n")
407 `(lambda () (gdb-tooltip-print ,expr))))))))
409 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
411 (defun gdb-set-gud-minor-mode-existing-buffers ()
412 "Create list of source files for current GDB session."
413 (goto-char (point-min))
414 (when (search-forward "read in on demand:" nil t)
415 (while (re-search-forward gdb-source-file-regexp nil t)
416 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
417 (dolist (buffer (buffer-list))
418 (with-current-buffer buffer
419 (when (and buffer-file-name
420 (member (file-name-nondirectory buffer-file-name)
421 gdb-source-file-list))
422 (set (make-local-variable 'gud-minor-mode) 'gdba)
423 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
424 (when gud-tooltip-mode
425 (make-local-variable 'gdb-define-alist)
426 (gdb-create-define-alist)
427 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
429 (defun gdb-find-watch-expression ()
430 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
431 (varnum (car var)) expr array)
432 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
433 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
434 (component-list (split-string (match-string 2 varnum) "\\." t)))
435 (setq expr (nth 1 var1))
436 (setq varnumlet (car var1))
437 (dolist (component component-list)
438 (setq var2 (assoc varnumlet gdb-var-list))
439 (setq expr (concat expr
440 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
441 (concat "[" component "]")
442 (concat "." component))))
443 (setq varnumlet (concat varnumlet "." component)))
447 (set (make-local-variable 'gud-minor-mode) 'gdba)
448 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
450 (gud-def gud-break (if (not (string-match "Machine" mode-name))
451 (gud-call "break %f:%l" arg)
455 (gud-call "break *%a" arg)))
456 "\C-b" "Set breakpoint at current line or address.")
458 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
459 (gud-call "clear %f:%l" arg)
463 (gud-call "clear *%a" arg)))
464 "\C-d" "Remove breakpoint at current line or address.")
466 (gud-def gud-until (if (not (string-match "Machine" mode-name))
467 (gud-call "until %f:%l" arg)
471 (gud-call "until *%a" arg)))
472 "\C-u" "Continue to current line or address.")
474 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
475 nil "Start or continue execution.")
477 ;; For debugging Emacs only.
481 "pp1 " (if (eq (buffer-local-value
482 'major-mode (window-buffer)) 'speedbar-mode)
483 (gdb-find-watch-expression) "%e")) arg)
484 nil "Print the emacs s-expression.")
486 (define-key gud-minor-mode-map [left-margin mouse-1]
487 'gdb-mouse-set-clear-breakpoint)
488 (define-key gud-minor-mode-map [left-fringe mouse-1]
489 'gdb-mouse-set-clear-breakpoint)
490 (define-key gud-minor-mode-map [left-fringe mouse-2]
492 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
494 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
496 (define-key gud-minor-mode-map [left-margin mouse-2]
498 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
500 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
502 (define-key gud-minor-mode-map [left-fringe C-mouse-2]
504 (define-key gud-minor-mode-map [left-margin C-mouse-2]
506 (define-key gud-minor-mode-map [left-margin mouse-3]
507 'gdb-mouse-toggle-breakpoint-margin)
508 (define-key gud-minor-mode-map [left-fringe mouse-3]
509 'gdb-mouse-toggle-breakpoint-fringe)
511 (setq comint-input-sender 'gdb-send)
514 (setq gdb-frame-address (if gdb-show-main "main" nil))
515 (setq gdb-previous-frame-address nil
516 gdb-memory-address "main"
517 gdb-previous-frame nil
518 gdb-selected-frame nil
519 gdb-current-language nil
523 gdb-first-post-prompt t
527 gdb-pending-triggers nil
528 gdb-output-sink 'user
529 gdb-server-prefix "server "
530 gdb-flush-pending-output nil
531 gdb-location-alist nil
532 gdb-source-file-list nil
535 gdb-buffer-fringe-width (car (window-fringes))
538 gdb-source-window nil
539 gdb-inferior-status nil
540 gdb-continuation nil)
542 (setq gdb-buffer-type 'gdba)
544 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
546 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
547 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
551 (if (eq window-system 'w32)
552 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
553 (gdb-enqueue-input (list "set height 0\n" 'ignore))
554 (gdb-enqueue-input (list "set width 0\n" 'ignore))
556 (if (string-equal gdb-version "pre-6.4")
558 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
559 'gdb-set-gud-minor-mode-existing-buffers))
560 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
562 (list "server interpreter mi -data-list-register-names\n"
563 'gdb-get-register-names))
564 ; Needs GDB 6.2 onwards.
566 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
567 'gdb-set-gud-minor-mode-existing-buffers-1))
568 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
570 ;; find source file and compilation directory here
571 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
572 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
573 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
575 (run-hooks 'gdba-mode-hook))
577 (defun gdb-get-version ()
578 (goto-char (point-min))
579 (if (and (re-search-forward gdb-error-regexp nil t)
580 (string-match ".*(missing implementation)" (match-string 1)))
581 (setq gdb-version "pre-6.4")
582 (setq gdb-version "6.4+"))
585 (defun gdb-mouse-until (event)
586 "Continue running until a source line past the current line.
587 The destination source line can be selected either by clicking with mouse-2
588 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
590 (if gud-overlay-arrow-position
591 (let ((start (event-start event))
592 (end (event-end event))
593 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
594 (if (not (string-match "Machine" mode-name))
595 (if (equal buffer (window-buffer (posn-window end)))
596 (with-current-buffer buffer
597 (when (or (equal start end)
598 (equal (posn-point start)
600 gud-overlay-arrow-position)))
601 (setq line (line-number-at-pos (posn-point end)))
602 (gud-call (concat "until " (number-to-string line))))))
603 (if (equal (marker-buffer gdb-overlay-arrow-position)
604 (window-buffer (posn-window end)))
605 (when (or (equal start end)
606 (equal (posn-point start)
608 gdb-overlay-arrow-position)))
610 (goto-line (line-number-at-pos (posn-point end)))
612 (gud-call (concat "until *%a")))))))))
614 (defun gdb-mouse-jump (event)
615 "Set execution address/line.
616 The destination source line can be selected either by clicking with mouse-2
617 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
618 Unlike gdb-mouse-until the destination address can be before the current
619 line, and no execution takes place."
621 (if gud-overlay-arrow-position
622 (let ((start (event-start event))
623 (end (event-end event))
624 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
625 (if (not (string-match "Machine" mode-name))
626 (if (equal buffer (window-buffer (posn-window end)))
627 (with-current-buffer buffer
628 (when (or (equal start end)
629 (equal (posn-point start)
631 gud-overlay-arrow-position)))
632 (setq line (line-number-at-pos (posn-point end)))
633 (progn (gud-call (concat "tbreak " (number-to-string line)))
634 (gud-call (concat "jump " (number-to-string line)))))))
635 (if (equal (marker-buffer gdb-overlay-arrow-position)
636 (window-buffer (posn-window end)))
637 (when (or (equal start end)
638 (equal (posn-point start)
640 gdb-overlay-arrow-position)))
642 (goto-line (line-number-at-pos (posn-point end)))
645 (gud-call (concat "tbreak *%a"))
646 (gud-call (concat "jump *%a"))))))))))
648 (defcustom gdb-speedbar-auto-raise nil
649 "If non-nil raise speedbar every time display of watch expressions is\
655 (defun gdb-speedbar-auto-raise (arg)
656 "Toggle automatic raising of the speedbar for watch expressions.
657 With arg, automatically raise speedbar iff arg is positive."
659 (setq gdb-speedbar-auto-raise
661 (not gdb-speedbar-auto-raise)
662 (> (prefix-numeric-value arg) 0)))
663 (message (format "Auto raising %sabled"
664 (if gdb-speedbar-auto-raise "en" "dis"))))
666 (defcustom gdb-use-colon-colon-notation nil
667 "If non-nil use FUN::VAR format to display variables in the speedbar."
672 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
673 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
675 (defun gud-watch (&optional arg event)
676 "Watch expression at point.
677 With arg, enter name of variable to be watched in the minibuffer."
678 (interactive (list current-prefix-arg last-input-event))
679 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
680 (if (memq minor-mode '(gdbmi gdba))
682 (if event (posn-set-point (event-end event)))
684 (save-selected-window
686 (completing-read "Name of variable: "
687 'gud-gdb-complete-command)
688 (tooltip-identifier-from-point (point)))))
689 (catch 'already-watched
690 (dolist (var gdb-var-list)
691 (unless (string-match "\\." (car var))
692 (if (string-equal expr (nth 1 var))
693 (throw 'already-watched nil))))
694 (set-text-properties 0 (length expr) nil expr)
697 (if (eq minor-mode 'gdba)
699 "server interpreter mi \"-var-create - * " expr "\"\n")
700 (concat"-var-create - * " expr "\n"))
701 `(lambda () (gdb-var-create-handler ,expr))))))))
702 (message "gud-watch is a no-op in this mode."))))
704 (defconst gdb-var-create-regexp
705 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
707 (defun gdb-var-create-handler (expr)
708 (goto-char (point-min))
709 (if (re-search-forward gdb-var-create-regexp nil t)
712 (if (and (string-equal gdb-current-language "c")
713 gdb-use-colon-colon-notation gdb-selected-frame)
714 (setq expr (concat gdb-selected-frame "::" expr))
719 (push var gdb-var-list)
721 (unless (string-equal
722 speedbar-initial-expansion-list-name "GUD")
723 (speedbar-change-initial-expansion-list "GUD"))
726 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
727 (concat "server interpreter mi \"-var-evaluate-expression "
729 (concat "-var-evaluate-expression " (car var) "\n"))
730 `(lambda () (gdb-var-evaluate-expression-handler
732 (if (search-forward "Undefined command" nil t)
733 (message-box "Watching expressions requires gdb 6.0 onwards")
734 (message-box "No symbol \"%s\" in current context." expr))))
736 (defun gdb-var-evaluate-expression-handler (varnum changed)
737 (goto-char (point-min))
738 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
739 (let ((var (assoc varnum gdb-var-list)))
741 (if changed (setcar (nthcdr 5 var) 'changed))
742 (setcar (nthcdr 4 var) (read (match-string 1))))))
744 (defun gdb-var-list-children (varnum)
746 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
747 `(lambda () (gdb-var-list-children-handler ,varnum)))))
749 (defconst gdb-var-list-children-regexp
750 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
753 (defun gdb-var-list-children-handler (varnum)
754 (goto-char (point-min))
755 (let ((var-list nil))
756 (catch 'child-already-watched
757 (dolist (var gdb-var-list)
758 (if (string-equal varnum (car var))
761 (while (re-search-forward gdb-var-list-children-regexp nil t)
762 (let ((varchild (list (match-string 1)
767 (if (assoc (car varchild) gdb-var-list)
768 (throw 'child-already-watched nil))
769 (push varchild var-list)
773 "server interpreter mi \"-var-evaluate-expression "
774 (car varchild) "\"\n")
775 `(lambda () (gdb-var-evaluate-expression-handler
776 ,(car varchild) nil)))))))
777 (push var var-list)))
778 (setq gdb-var-list (nreverse var-list)))))
780 (defun gdb-var-update ()
781 (when (not (member 'gdb-var-update gdb-pending-triggers))
783 (list "server interpreter mi \"-var-update *\"\n"
784 'gdb-var-update-handler))
785 (push 'gdb-var-update gdb-pending-triggers)))
787 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\",in_scope=\"\\(.*?\\)\"")
789 (defun gdb-var-update-handler ()
790 (dolist (var gdb-var-list)
791 (setcar (nthcdr 5 var) nil))
792 (goto-char (point-min))
793 (while (re-search-forward gdb-var-update-regexp nil t)
794 (let ((varnum (match-string 1)))
795 (if (string-equal (match-string 2) "false")
796 (let ((var (assoc varnum gdb-var-list)))
797 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
800 (concat "server interpreter mi \"-var-evaluate-expression "
802 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
803 (setq gdb-pending-triggers
804 (delq 'gdb-var-update gdb-pending-triggers))
805 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
806 ;; Dummy command to update speedbar at right time.
807 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
808 ;; Keep gdb-pending-triggers non-nil till end.
809 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
811 (defun gdb-speedbar-refresh ()
812 (setq gdb-pending-triggers
813 (delq 'gdb-speedbar-refresh gdb-pending-triggers))
814 (with-current-buffer gud-comint-buffer
815 (let ((speedbar-verbosity-level 0)
816 (speedbar-shown-directories nil))
818 (speedbar-refresh)))))
820 (defun gdb-var-delete ()
821 "Delete watch expression at point from the speedbar."
823 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
825 (let ((text (speedbar-line-text)))
826 (string-match "\\(\\S-+\\)" text)
827 (let ((expr (match-string 1 text)) var varnum)
829 (dolist (var1 gdb-var-list)
830 (when (string-equal expr (nth 1 var1))
832 (setq varnum (car var1))
833 (throw 'expr-found nil))))
834 (unless (string-match "\\." (car var))
837 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
839 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
840 (concat "-var-delete " varnum "\n"))
842 (setq gdb-var-list (delq var gdb-var-list))
843 (dolist (varchild gdb-var-list)
844 (if (string-match (concat (car var) "\\.") (car varchild))
845 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
847 (defun gdb-edit-value (text token indent)
848 "Assign a value to a variable displayed in the speedbar."
849 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
850 (varnum (car var)) (value))
851 (setq value (read-string "New value: "))
854 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
855 (concat "server interpreter mi \"-var-assign "
856 varnum " " value "\"\n")
857 (concat "-var-assign " varnum " " value "\n"))
858 `(lambda () (gdb-edit-value-handler ,value))))))
860 (defun gdb-edit-value-handler (value)
861 (goto-char (point-min))
862 (if (re-search-forward gdb-error-regexp nil t)
863 (message-box "Invalid number or expression (%s)" value)))
865 (defcustom gdb-show-changed-values t
866 "If non-nil change the face of out of scope variables and changed values.
867 Out of scope variables are suppressed with `shadow' face.
868 Changed values are highlighted with the face `font-lock-warning-face'."
873 (defun gdb-speedbar-expand-node (text token indent)
874 "Expand the node the user clicked on.
875 TEXT is the text of the button we clicked on, a + or - item.
876 TOKEN is data related to this node.
877 INDENT is the current indentation depth."
878 (cond ((string-match "+" text) ;expand this node
880 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
881 (string-equal gdb-version "pre-6.4"))
882 (gdb-var-list-children token)
883 (gdb-var-list-children-1 token)))
884 ((string-match "-" text) ;contract this node
885 (dolist (var gdb-var-list)
886 (if (string-match (concat token "\\.") (car var))
887 (setq gdb-var-list (delq var gdb-var-list))))
888 (speedbar-change-expand-button-char ?+)
889 (speedbar-delete-subblock indent))
890 (t (error "Ooops... not sure what to do")))
891 (speedbar-center-buffer-smartly))
893 (defun gdb-get-target-string ()
894 (with-current-buffer gud-comint-buffer
901 ;; Each buffer has a TYPE -- a symbol that identifies the function
902 ;; of that particular buffer.
904 ;; The usual gdb interaction buffer is given the type `gdba' and
905 ;; is constructed specially.
907 ;; Others are constructed by gdb-get-buffer-create and
908 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
910 (defvar gdb-buffer-rules-assoc '())
912 (defun gdb-get-buffer (key)
913 "Return the gdb buffer tagged with type KEY.
914 The key should be one of the cars in `gdb-buffer-rules-assoc'."
916 (gdb-look-for-tagged-buffer key (buffer-list))))
918 (defun gdb-get-buffer-create (key)
919 "Create a new gdb buffer of the type specified by KEY.
920 The key should be one of the cars in `gdb-buffer-rules-assoc'."
921 (or (gdb-get-buffer key)
922 (let* ((rules (assoc key gdb-buffer-rules-assoc))
923 (name (funcall (gdb-rules-name-maker rules)))
924 (new (get-buffer-create name)))
925 (with-current-buffer new
927 (if (cdr (cdr rules))
928 (setq trigger (funcall (car (cdr (cdr rules))))))
929 (setq gdb-buffer-type key)
930 (set (make-local-variable 'gud-minor-mode)
931 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
932 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
933 (if trigger (funcall trigger)))
936 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
938 (defun gdb-look-for-tagged-buffer (key bufs)
940 (while (and (not retval) bufs)
941 (set-buffer (car bufs))
942 (if (eq gdb-buffer-type key)
943 (setq retval (car bufs)))
944 (setq bufs (cdr bufs)))
948 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
949 ;; at least one and possible more functions. The functions have these
950 ;; roles in defining a buffer type:
952 ;; NAME - Return a name for this buffer type.
954 ;; The remaining function(s) are optional:
956 ;; MODE - called in a new buffer with no arguments, should establish
957 ;; the proper mode for the buffer.
960 (defun gdb-set-buffer-rules (buffer-type &rest rules)
961 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
963 (setcdr binding rules)
964 (push (cons buffer-type rules)
965 gdb-buffer-rules-assoc))))
967 ;; GUD buffers are an exception to the rules
968 (gdb-set-buffer-rules 'gdba 'error)
970 ;; Partial-output buffer : This accumulates output from a command executed on
971 ;; behalf of emacs (rather than the user).
973 (gdb-set-buffer-rules 'gdb-partial-output-buffer
974 'gdb-partial-output-name)
976 (defun gdb-partial-output-name ()
977 (concat "*partial-output-"
978 (gdb-get-target-string)
982 (gdb-set-buffer-rules 'gdb-inferior-io
983 'gdb-inferior-io-name
984 'gdb-inferior-io-mode)
986 (defun gdb-inferior-io-name ()
987 (concat "*input/output of "
988 (gdb-get-target-string)
991 (defun gdb-display-separate-io-buffer ()
992 "Display IO of debugged program in a separate window."
994 (if gdb-use-separate-io-buffer
996 (gdb-get-buffer-create 'gdb-inferior-io))))
998 (defconst gdb-frame-parameters
999 '((height . 14) (width . 80)
1001 (tool-bar-lines . nil)
1002 (menu-bar-lines . nil)
1003 (minibuffer . nil)))
1005 (defun gdb-frame-separate-io-buffer ()
1006 "Display IO of inferior in a new frame."
1008 (if gdb-use-separate-io-buffer
1009 (let ((special-display-regexps (append special-display-regexps '(".*")))
1010 (special-display-frame-alist gdb-frame-parameters))
1011 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1013 (defvar gdb-inferior-io-mode-map
1014 (let ((map (make-sparse-keymap)))
1015 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1016 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1017 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1018 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1019 (define-key map "\C-d" 'gdb-separate-io-eof)
1022 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1023 "Major mode for gdb inferior-io."
1024 :syntax-table nil :abbrev-table nil
1025 ;; We want to use comint because it has various nifty and familiar
1026 ;; features. We don't need a process, but comint wants one, so create
1028 (make-comint-in-buffer
1029 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1030 (current-buffer) "hexl")
1031 (setq comint-input-sender 'gdb-inferior-io-sender))
1033 (defun gdb-inferior-io-sender (proc string)
1034 ;; PROC is the pseudo-process created to satisfy comint.
1035 (with-current-buffer (process-buffer proc)
1036 (setq proc (get-buffer-process gud-comint-buffer))
1037 (process-send-string proc string)
1038 (process-send-string proc "\n")))
1040 (defun gdb-separate-io-interrupt ()
1041 "Interrupt the program being debugged."
1044 (get-buffer-process gud-comint-buffer) comint-ptyp))
1046 (defun gdb-separate-io-quit ()
1047 "Send quit signal to the program being debugged."
1050 (get-buffer-process gud-comint-buffer) comint-ptyp))
1052 (defun gdb-separate-io-stop ()
1053 "Stop the program being debugged."
1056 (get-buffer-process gud-comint-buffer) comint-ptyp))
1058 (defun gdb-separate-io-eof ()
1059 "Send end-of-file to the program being debugged."
1062 (get-buffer-process gud-comint-buffer)))
1065 ;; gdb communications
1068 ;; INPUT: things sent to gdb
1070 ;; The queues are lists. Each element is either a string (indicating user or
1071 ;; user-like input) or a list of the form:
1073 ;; (INPUT-STRING HANDLER-FN)
1075 ;; The handler function will be called from the partial-output buffer when the
1076 ;; command completes. This is the way to write commands which invoke gdb
1077 ;; commands autonomously.
1079 ;; These lists are consumed tail first.
1081 (defvar gdb-continuation nil)
1083 (defun gdb-send (proc string)
1084 "A comint send filter for gdb.
1085 This filter may simply queue input for a later time."
1086 (with-current-buffer gud-comint-buffer
1087 (let ((inhibit-read-only t))
1088 (remove-text-properties (point-min) (point-max) '(face))))
1089 (if (string-match "\\\\$" string)
1090 (setq gdb-continuation (concat gdb-continuation string "\n"))
1091 (let ((item (concat gdb-continuation string "\n")))
1094 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1095 (process-send-string proc item))
1096 (gdb-enqueue-input item)))
1097 (setq gdb-continuation nil)))
1099 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1100 ;; is a query, or other non-top-level prompt.
1102 (defun gdb-enqueue-input (item)
1103 (if (not gud-running)
1106 (gdb-send-item item)
1107 (setq gdb-prompting nil))
1108 (push item gdb-input-queue))))
1110 (defun gdb-dequeue-input ()
1111 (let ((queue gdb-input-queue))
1113 (let ((last (car (last queue))))
1114 (unless (nbutlast queue) (setq gdb-input-queue '()))
1117 (defun gdb-send-item (item)
1118 (setq gdb-flush-pending-output nil)
1119 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1120 (setq gdb-current-item item)
1121 (let ((process (get-buffer-process gud-comint-buffer)))
1122 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1125 (setq gdb-output-sink 'user)
1126 (process-send-string process item))
1128 (gdb-clear-partial-output)
1129 (setq gdb-output-sink 'pre-emacs)
1130 (process-send-string process
1132 ;; case: eq gud-minor-mode 'gdbmi
1133 (gdb-clear-partial-output)
1134 (setq gdb-output-sink 'emacs)
1135 (process-send-string process (car item)))))
1138 ;; output -- things gdb prints to emacs
1140 ;; GDB output is a stream interrupted by annotations.
1141 ;; Annotations can be recognized by their beginning
1142 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1144 ;; The tag is a string obeying symbol syntax.
1146 ;; The optional part `<opt>' can be either the empty string
1147 ;; or a space followed by more data relating to the annotation.
1148 ;; For example, the SOURCE annotation is followed by a filename,
1149 ;; line number and various useless goo. This data must not include
1153 (defcustom gud-gdba-command-name "gdb -annotate=3"
1154 "Default command to execute an executable under the GDB-UI debugger."
1159 (defvar gdb-annotation-rules
1160 '(("pre-prompt" gdb-pre-prompt)
1161 ("prompt" gdb-prompt)
1162 ("commands" gdb-subprompt)
1163 ("overload-choice" gdb-subprompt)
1164 ("query" gdb-subprompt)
1165 ;; Need this prompt for GDB 6.1
1166 ("nquery" gdb-subprompt)
1167 ("prompt-for-continue" gdb-subprompt)
1168 ("post-prompt" gdb-post-prompt)
1169 ("source" gdb-source)
1170 ("starting" gdb-starting)
1171 ("exited" gdb-exited)
1172 ("signalled" gdb-signalled)
1173 ("signal" gdb-signal)
1174 ("breakpoint" gdb-stopping)
1175 ("watchpoint" gdb-stopping)
1176 ("frame-begin" gdb-frame-begin)
1177 ("stopped" gdb-stopped)
1178 ("error-begin" gdb-error)
1180 ) "An assoc mapping annotation tags to functions which process them.")
1183 (setq gdb-flush-pending-output t)
1184 (setq gud-running nil)
1185 (gdb-force-mode-line-update "stopped")
1186 (setq gdb-output-sink 'user)
1187 (setq gdb-input-queue nil)
1188 (setq gdb-pending-triggers nil)
1189 (setq gdb-prompting t))
1191 (defconst gdb-source-spec-regexp
1192 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1194 ;; Do not use this except as an annotation handler.
1195 (defun gdb-source (args)
1196 (string-match gdb-source-spec-regexp args)
1197 ;; Extract the frame position from the marker.
1198 (setq gud-last-frame
1200 (match-string 1 args)
1201 (string-to-number (match-string 2 args))))
1202 (setq gdb-frame-address (match-string 3 args))
1203 ;; cover for auto-display output which comes *before*
1204 ;; stopped annotation
1205 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1207 (defun gdb-pre-prompt (ignored)
1208 "An annotation handler for `pre-prompt'.
1209 This terminates the collection of output from a previous command if that
1210 happens to be in effect."
1211 (setq gdb-error nil)
1212 (let ((sink gdb-output-sink))
1216 (setq gdb-output-sink 'post-emacs))
1219 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1221 (defun gdb-prompt (ignored)
1222 "An annotation handler for `prompt'.
1223 This sends the next command (if any) to gdb."
1224 (when gdb-first-prompt
1226 (setq gdb-first-prompt nil))
1227 (let ((sink gdb-output-sink))
1230 ((eq sink 'post-emacs)
1231 (setq gdb-output-sink 'user)
1233 (car (cdr gdb-current-item))))
1234 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1235 (funcall handler))))
1238 (error "Phase error in gdb-prompt (got %s)" sink))))
1239 (let ((input (gdb-dequeue-input)))
1241 (gdb-send-item input)
1243 (setq gdb-prompting t)
1244 (gud-display-frame)))))
1246 (defun gdb-subprompt (ignored)
1247 "An annotation handler for non-top-level prompts."
1248 (setq gdb-prompting t))
1250 (defun gdb-starting (ignored)
1251 "An annotation handler for `starting'.
1252 This says that I/O for the subprocess is now the program being debugged,
1254 (setq gdb-active-process t)
1255 (let ((sink gdb-output-sink))
1259 (setq gud-running t)
1260 (setq gdb-inferior-status "running")
1261 (gdb-force-mode-line-update gdb-inferior-status)
1262 (gdb-remove-text-properties)
1263 (setq gud-overlay-arrow-position nil)
1264 (setq gdb-overlay-arrow-position nil)
1265 (if gdb-use-separate-io-buffer
1266 (setq gdb-output-sink 'inferior))))
1269 (error "Unexpected `starting' annotation")))))
1271 (defun gdb-signal (ignored)
1272 (setq gdb-inferior-status "signal")
1273 (gdb-force-mode-line-update gdb-inferior-status)
1274 (gdb-stopping ignored))
1276 (defun gdb-stopping (ignored)
1277 "An annotation handler for `breakpoint' and other annotations.
1278 They say that I/O for the subprocess is now GDB, not the program
1280 (if gdb-use-separate-io-buffer
1281 (let ((sink gdb-output-sink))
1283 ((eq sink 'inferior)
1284 (setq gdb-output-sink 'user))
1287 (error "Unexpected stopping annotation"))))))
1289 (defun gdb-exited (ignored)
1290 "An annotation handler for `exited' and `signalled'.
1291 They say that I/O for the subprocess is now GDB, not the program
1292 being debugged and that the program is no longer running. This
1293 function is used to change the focus of GUD tooltips to #define
1295 (setq gdb-active-process nil)
1296 (setq gud-overlay-arrow-position nil)
1297 (setq gdb-overlay-arrow-position nil)
1298 (setq gdb-inferior-status "exited")
1299 (gdb-force-mode-line-update gdb-inferior-status)
1300 (gdb-stopping ignored))
1302 (defun gdb-signalled (ignored)
1303 (setq gdb-signalled t))
1305 (defun gdb-frame-begin (ignored)
1306 (let ((sink gdb-output-sink))
1308 ((eq sink 'inferior)
1309 (setq gdb-output-sink 'user))
1311 ((eq sink 'emacs) t)
1314 (error "Unexpected frame-begin annotation (%S)" sink)))))
1316 (defun gdb-stopped (ignored)
1317 "An annotation handler for `stopped'.
1318 It is just like `gdb-stopping', except that if we already set the output
1319 sink to `user' in `gdb-stopping', that is fine."
1320 (setq gud-running nil)
1321 (unless (or gud-overlay-arrow-position gud-last-frame)
1322 (gud-display-line (car gud-last-last-frame) (cdr gud-last-last-frame)))
1323 (unless (member gdb-inferior-status '("exited" "signal"))
1324 (setq gdb-inferior-status "stopped")
1325 (gdb-force-mode-line-update gdb-inferior-status))
1326 (let ((sink gdb-output-sink))
1328 ((eq sink 'inferior)
1329 (setq gdb-output-sink 'user))
1333 (error "Unexpected stopped annotation"))))
1334 (if gdb-signalled (gdb-exited ignored)))
1336 (defun gdb-error (ignored)
1337 (setq gdb-error (not gdb-error)))
1339 (defun gdb-post-prompt (ignored)
1340 "An annotation handler for `post-prompt'.
1341 This begins the collection of output from the current command if that
1342 happens to be appropriate."
1343 ;; Don't add to queue if there outstanding items or gdb-version is not known
1345 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1346 (gdb-get-selected-frame)
1347 (gdb-invalidate-frames)
1348 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1349 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1350 (gdb-invalidate-breakpoints)
1351 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1352 ;; so gdb-frame-address is updated.
1353 ;; (gdb-invalidate-assembler)
1355 (if (string-equal gdb-version "pre-6.4")
1356 (gdb-invalidate-registers)
1357 (gdb-get-changed-registers)
1358 (gdb-invalidate-registers-1))
1360 (gdb-invalidate-memory)
1361 (if (string-equal gdb-version "pre-6.4")
1362 (gdb-invalidate-locals)
1363 (gdb-invalidate-locals-1))
1365 (gdb-invalidate-threads)
1366 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1367 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1368 ;; Only needed/used with speedbar/watch expressions.
1369 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1370 (setq gdb-force-update t)
1371 (if (string-equal gdb-version "pre-6.4")
1373 (gdb-var-update-1)))))
1374 (setq gdb-first-post-prompt nil)
1375 (let ((sink gdb-output-sink))
1378 ((eq sink 'pre-emacs)
1379 (setq gdb-output-sink 'emacs))
1382 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1384 (defconst gdb-buffer-list
1385 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1387 (defun gdb-remove-text-properties ()
1388 (dolist (buffertype gdb-buffer-list)
1389 (let ((buffer (gdb-get-buffer buffertype)))
1391 (with-current-buffer buffer
1392 (let ((inhibit-read-only t))
1393 (remove-text-properties
1394 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1396 ;; GUD displays the selected GDB frame. This might might not be the current
1397 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1398 ;; visited breakpoint is, use that window.
1399 (defun gdb-display-source-buffer (buffer)
1400 (let* ((last-window (if gud-last-last-frame
1402 (gud-find-file (car gud-last-last-frame)))))
1403 (source-window (or last-window
1404 (if (and gdb-source-window
1405 (window-live-p gdb-source-window))
1406 gdb-source-window))))
1408 (setq gdb-source-window source-window)
1409 (set-window-buffer source-window buffer))
1412 (defun gud-gdba-marker-filter (string)
1413 "A gud marker filter for gdb. Handle a burst of output from GDB."
1414 (if gdb-flush-pending-output
1416 (when gdb-enable-debug
1417 (push (cons 'recv string) gdb-debug-ring)
1418 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1419 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1420 ;; Recall the left over gud-marker-acc from last time.
1421 (setq gud-marker-acc (concat gud-marker-acc string))
1422 ;; Start accumulating output for the GUD buffer.
1425 ;; Process all the complete markers in this chunk.
1426 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1427 (let ((annotation (match-string 1 gud-marker-acc)))
1429 ;; Stuff prior to the match is just ordinary output.
1430 ;; It is either concatenated to OUTPUT or directed
1435 (substring gud-marker-acc 0 (match-beginning 0))))
1437 ;; Take that stuff off the gud-marker-acc.
1438 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1440 ;; Parse the tag from the annotation, and maybe its arguments.
1441 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1442 (let* ((annotation-type (match-string 1 annotation))
1443 (annotation-arguments (match-string 2 annotation))
1444 (annotation-rule (assoc annotation-type
1445 gdb-annotation-rules)))
1446 ;; Call the handler for this annotation.
1448 (funcall (car (cdr annotation-rule))
1449 annotation-arguments)
1450 ;; Else the annotation is not recognized. Ignore it silently,
1451 ;; so that GDB can add new annotations without causing
1455 ;; Does the remaining text end in a partial line?
1456 ;; If it does, then keep part of the gud-marker-acc until we get more.
1457 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1460 ;; Everything before the potential marker start can be output.
1462 (gdb-concat-output output
1463 (substring gud-marker-acc 0
1464 (match-beginning 0))))
1466 ;; Everything after, we save, to combine with later input.
1467 (setq gud-marker-acc (substring gud-marker-acc
1468 (match-beginning 0))))
1470 ;; In case we know the gud-marker-acc contains no partial annotations:
1472 (setq output (gdb-concat-output output gud-marker-acc))
1473 (setq gud-marker-acc "")))
1476 (defun gdb-concat-output (so-far new)
1478 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1479 (let ((sink gdb-output-sink))
1481 ((eq sink 'user) (concat so-far new))
1482 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1484 (gdb-append-to-partial-output new)
1486 ((eq sink 'inferior)
1487 (gdb-append-to-inferior-io new)
1491 (error "Bogon output sink %S" sink)))))
1493 (defun gdb-append-to-partial-output (string)
1494 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1495 (goto-char (point-max))
1498 (defun gdb-clear-partial-output ()
1499 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1502 (defun gdb-append-to-inferior-io (string)
1503 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1504 (goto-char (point-max))
1505 (insert-before-markers string))
1506 (if (not (string-equal string ""))
1507 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1509 (defun gdb-clear-inferior-io ()
1510 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1514 ;; One trick is to have a command who's output is always available in a buffer
1515 ;; of it's own, and is always up to date. We build several buffers of this
1518 ;; There are two aspects to this: gdb has to tell us when the output for that
1519 ;; command might have changed, and we have to be able to run the command
1520 ;; behind the user's back.
1522 ;; The output phasing associated with the variable gdb-output-sink
1523 ;; help us to run commands behind the user's back.
1525 ;; Below is the code for specificly managing buffers of output from one
1529 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1530 ;; It adds an input for the command we are tracking. It should be the
1531 ;; annotation rule binding of whatever gdb sends to tell us this command
1532 ;; might have changed it's output.
1534 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1535 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1536 ;; input in the input queue (see comment about ``gdb communications'' above).
1538 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1540 `(defun ,name (&optional ignored)
1541 (if (and ,demand-predicate
1543 gdb-pending-triggers)))
1546 (list ,gdb-command ',output-handler))
1547 (push ',name gdb-pending-triggers)))))
1549 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1551 (setq gdb-pending-triggers
1553 gdb-pending-triggers))
1554 (let ((buf (gdb-get-buffer ',buf-key)))
1556 (with-current-buffer buf
1557 (let* ((window (get-buffer-window buf 0))
1558 (start (window-start window))
1559 (p (window-point window))
1560 (buffer-read-only nil))
1562 (insert-buffer-substring (gdb-get-buffer-create
1563 'gdb-partial-output-buffer))
1564 (set-window-start window start)
1565 (set-window-point window p)))))
1566 ;; put customisation here
1569 (defmacro def-gdb-auto-updated-buffer (buffer-key
1570 trigger-name gdb-command
1571 output-handler-name custom-defun)
1573 (def-gdb-auto-update-trigger ,trigger-name
1574 ;; The demand predicate:
1575 (gdb-get-buffer ',buffer-key)
1577 ,output-handler-name)
1578 (def-gdb-auto-update-handler ,output-handler-name
1579 ,trigger-name ,buffer-key ,custom-defun)))
1583 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1585 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1586 'gdb-breakpoints-buffer-name
1587 'gdb-breakpoints-mode)
1589 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1590 ;; This defines the auto update rule for buffers of type
1591 ;; `gdb-breakpoints-buffer'.
1593 ;; It defines a function to serve as the annotation handler that
1594 ;; handles the `foo-invalidated' message. That function is called:
1595 gdb-invalidate-breakpoints
1597 ;; To update the buffer, this command is sent to gdb.
1598 "server info breakpoints\n"
1600 ;; This also defines a function to be the handler for the output
1601 ;; from the command above. That function will copy the output into
1602 ;; the appropriately typed buffer. That function will be called:
1603 gdb-info-breakpoints-handler
1604 ;; buffer specific functions
1605 gdb-info-breakpoints-custom)
1607 (defconst breakpoint-xpm-data
1609 static char *magick[] = {
1610 /* columns rows colors chars-per-pixel */
1626 "XPM data used for breakpoint icon.")
1628 (defconst breakpoint-enabled-pbm-data
1631 0 0 0 0 1 1 1 1 0 0 0 0
1632 0 0 0 1 1 1 1 1 1 0 0 0
1633 0 0 1 1 1 1 1 1 1 1 0 0
1634 0 1 1 1 1 1 1 1 1 1 1 0
1635 0 1 1 1 1 1 1 1 1 1 1 0
1636 0 1 1 1 1 1 1 1 1 1 1 0
1637 0 1 1 1 1 1 1 1 1 1 1 0
1638 0 0 1 1 1 1 1 1 1 1 0 0
1639 0 0 0 1 1 1 1 1 1 0 0 0
1640 0 0 0 0 1 1 1 1 0 0 0 0"
1641 "PBM data used for enabled breakpoint icon.")
1643 (defconst breakpoint-disabled-pbm-data
1655 0 0 0 1 0 1 0 1 0 0"
1656 "PBM data used for disabled breakpoint icon.")
1658 (defvar breakpoint-enabled-icon nil
1659 "Icon for enabled breakpoint in display margin.")
1661 (defvar breakpoint-disabled-icon nil
1662 "Icon for disabled breakpoint in display margin.")
1664 (and (display-images-p)
1665 ;; Bitmap for breakpoint in fringe
1666 (define-fringe-bitmap 'breakpoint
1667 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1668 ;; Bitmap for gud-overlay-arrow in fringe
1669 (define-fringe-bitmap 'hollow-right-triangle
1670 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1672 (defface breakpoint-enabled
1676 "Face for enabled breakpoint icon in fringe."
1679 (defface breakpoint-disabled
1680 ;; We use different values of grey for different background types,
1681 ;; so that on low-color displays it will end up as something visible
1682 ;; if it has to be approximated.
1683 '((((background dark)) :foreground "grey60")
1684 (((background light)) :foreground "grey40"))
1685 "Face for disabled breakpoint icon in fringe."
1688 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1689 (defun gdb-info-breakpoints-custom ()
1690 (let ((flag) (bptno))
1691 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1692 (dolist (buffer (buffer-list))
1693 (with-current-buffer buffer
1694 (if (and (memq gud-minor-mode '(gdba gdbmi))
1695 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1696 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1697 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1699 (goto-char (point-min))
1700 (while (< (point) (- (point-max) 1))
1702 (if (looking-at "[^\t].*?breakpoint")
1704 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1705 (setq bptno (match-string 1))
1706 (setq flag (char-after (match-beginning 2)))
1708 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1710 (let ((buffer-read-only nil))
1711 (add-text-properties (match-beginning 1) (match-end 1)
1712 '(face font-lock-function-name-face)))
1713 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1714 (let ((line (match-string 2)) (buffer-read-only nil)
1715 (file (match-string 1)))
1716 (add-text-properties (line-beginning-position)
1718 '(mouse-face highlight
1719 help-echo "mouse-2, RET: visit breakpoint"))
1720 (unless (file-exists-p file)
1721 (setq file (cdr (assoc bptno gdb-location-alist))))
1723 (not (string-equal file "File not found")))
1724 (with-current-buffer
1725 (find-file-noselect file 'nowarn)
1726 (set (make-local-variable 'gud-minor-mode)
1728 (set (make-local-variable 'tool-bar-map)
1730 ;; Only want one breakpoint icon at each
1733 (goto-line (string-to-number line))
1734 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1737 (concat gdb-server-prefix "list "
1738 (match-string-no-properties 1) ":1\n")
1741 (list (concat gdb-server-prefix "info source\n")
1742 `(lambda () (gdb-get-location
1743 ,bptno ,line ,flag))))))))))
1745 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1747 (defun gdb-mouse-set-clear-breakpoint (event)
1748 "Set/clear breakpoint in left fringe/margin with mouse click."
1750 (mouse-minibuffer-check event)
1751 (let ((posn (event-end event)))
1752 (if (numberp (posn-point posn))
1753 (with-selected-window (posn-window posn)
1755 (goto-char (posn-point posn))
1756 (if (or (posn-object posn)
1757 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1760 (gud-break nil)))))))
1762 (defun gdb-mouse-toggle-breakpoint-margin (event)
1763 "Enable/disable breakpoint in left margin with mouse click."
1765 (mouse-minibuffer-check event)
1766 (let ((posn (event-end event)))
1767 (if (numberp (posn-point posn))
1768 (with-selected-window (posn-window posn)
1770 (goto-char (posn-point posn))
1771 (if (posn-object posn)
1774 (let ((bptno (get-text-property
1775 0 'gdb-bptno (car (posn-string posn)))))
1776 (concat gdb-server-prefix
1777 (if (get-text-property
1778 0 'gdb-enabled (car (posn-string posn)))
1784 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1785 "Enable/disable breakpoint in left fringe with mouse click."
1787 (mouse-minibuffer-check event)
1788 (let* ((posn (event-end event))
1789 (pos (posn-point posn))
1792 (with-selected-window (posn-window posn)
1794 (set-buffer (window-buffer (selected-window)))
1796 (dolist (overlay (overlays-in pos pos))
1797 (when (overlay-get overlay 'put-break)
1798 (setq obj (overlay-get overlay 'before-string))))
1802 (concat gdb-server-prefix
1803 (if (get-text-property 0 'gdb-enabled obj)
1806 (get-text-property 0 'gdb-bptno obj) "\n")
1809 (defun gdb-breakpoints-buffer-name ()
1810 (with-current-buffer gud-comint-buffer
1811 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1813 (defun gdb-display-breakpoints-buffer ()
1814 "Display status of user-settable breakpoints."
1817 (gdb-get-buffer-create 'gdb-breakpoints-buffer)))
1819 (defun gdb-frame-breakpoints-buffer ()
1820 "Display status of user-settable breakpoints in a new frame."
1822 (let ((special-display-regexps (append special-display-regexps '(".*")))
1823 (special-display-frame-alist gdb-frame-parameters))
1824 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1826 (defvar gdb-breakpoints-mode-map
1827 (let ((map (make-sparse-keymap))
1828 (menu (make-sparse-keymap "Breakpoints")))
1829 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1830 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1831 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1832 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1833 (suppress-keymap map)
1834 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1835 (define-key map " " 'gdb-toggle-breakpoint)
1836 (define-key map "D" 'gdb-delete-breakpoint)
1837 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1838 (define-key map "q" 'gdb-delete-frame-or-window)
1839 (define-key map "\r" 'gdb-goto-breakpoint)
1840 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1841 (define-key map [follow-link] 'mouse-face)
1844 (defun gdb-delete-frame-or-window ()
1845 "Delete frame if there is only one window. Otherwise delete the window."
1847 (if (one-window-p) (delete-frame)
1850 (defun gdb-breakpoints-mode ()
1851 "Major mode for gdb breakpoints.
1853 \\{gdb-breakpoints-mode-map}"
1854 (kill-all-local-variables)
1855 (setq major-mode 'gdb-breakpoints-mode)
1856 (setq mode-name "Breakpoints")
1857 (use-local-map gdb-breakpoints-mode-map)
1858 (setq buffer-read-only t)
1859 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1860 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1861 'gdb-invalidate-breakpoints
1862 'gdbmi-invalidate-breakpoints))
1864 (defconst gdb-breakpoint-regexp
1865 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1867 (defun gdb-toggle-breakpoint ()
1868 "Enable/disable breakpoint at current line."
1871 (beginning-of-line 1)
1872 (if (looking-at gdb-breakpoint-regexp)
1875 (concat gdb-server-prefix
1876 (if (eq ?y (char-after (match-beginning 2)))
1879 (match-string 1) "\n") 'ignore))
1880 (error "Not recognized as break/watchpoint line"))))
1882 (defun gdb-delete-breakpoint ()
1883 "Delete the breakpoint at current line."
1885 (beginning-of-line 1)
1886 (if (looking-at gdb-breakpoint-regexp)
1889 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1890 (error "Not recognized as break/watchpoint line")))
1892 (defun gdb-goto-breakpoint (&optional event)
1893 "Display the breakpoint location specified at current line."
1894 (interactive (list last-input-event))
1895 (if event (posn-set-point (event-end event)))
1897 (beginning-of-line 1)
1898 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1899 (let ((bptno (match-string 1))
1900 (file (match-string 2))
1901 (line (match-string 3)))
1902 (save-selected-window
1903 (let* ((buffer (find-file-noselect
1904 (if (file-exists-p file) file
1905 (cdr (assoc bptno gdb-location-alist)))))
1906 (window (unless (gdb-display-source-buffer buffer)
1907 (display-buffer buffer))))
1908 (setq gdb-source-window window)
1909 (with-current-buffer buffer
1910 (goto-line (string-to-number line))
1911 (set-window-point window (point))))))
1912 (error "No location specified."))))
1915 ;; Frames buffer. This displays a perpetually correct bactracktrace
1916 ;; (from the command `where').
1918 ;; Alas, if your stack is deep, it is costly.
1920 (gdb-set-buffer-rules 'gdb-stack-buffer
1921 'gdb-stack-buffer-name
1924 (def-gdb-auto-updated-buffer gdb-stack-buffer
1925 gdb-invalidate-frames
1926 "server info stack\n"
1927 gdb-info-stack-handler
1928 gdb-info-stack-custom)
1930 (defun gdb-info-stack-custom ()
1931 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1933 (let ((buffer-read-only nil)
1935 (goto-char (point-min))
1936 (while (< (point) (point-max))
1937 (setq bl (line-beginning-position)
1938 el (line-end-position))
1939 (when (looking-at "#")
1940 (add-text-properties bl el
1941 '(mouse-face highlight
1942 help-echo "mouse-2, RET: Select frame")))
1944 (when (looking-at "^#\\([0-9]+\\)")
1945 (when (string-equal (match-string 1) gdb-frame-number)
1946 (put-text-property bl (+ bl 4)
1947 'face '(:inverse-video t)))
1948 (when (re-search-forward
1950 (if (string-equal (match-string 1) "0") "" " in ")
1951 "\\([^ ]+\\) (") el t)
1952 (put-text-property (match-beginning 1) (match-end 1)
1953 'face font-lock-function-name-face)
1954 (setq bl (match-end 0))
1955 (while (re-search-forward "<\\([^>]+\\)>" el t)
1956 (put-text-property (match-beginning 1) (match-end 1)
1957 'face font-lock-function-name-face))
1959 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1960 (put-text-property (match-beginning 1) (match-end 1)
1961 'face font-lock-variable-name-face))))
1962 (forward-line 1))))))
1964 (defun gdb-stack-buffer-name ()
1965 (with-current-buffer gud-comint-buffer
1966 (concat "*stack frames of " (gdb-get-target-string) "*")))
1968 (defun gdb-display-stack-buffer ()
1969 "Display backtrace of current stack."
1972 (gdb-get-buffer-create 'gdb-stack-buffer)))
1974 (defun gdb-frame-stack-buffer ()
1975 "Display backtrace of current stack in a new frame."
1977 (let ((special-display-regexps (append special-display-regexps '(".*")))
1978 (special-display-frame-alist gdb-frame-parameters))
1979 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
1981 (defvar gdb-frames-mode-map
1982 (let ((map (make-sparse-keymap)))
1983 (suppress-keymap map)
1984 (define-key map "q" 'kill-this-buffer)
1985 (define-key map "\r" 'gdb-frames-select)
1986 (define-key map [mouse-2] 'gdb-frames-select)
1987 (define-key map [follow-link] 'mouse-face)
1990 (defun gdb-frames-mode ()
1991 "Major mode for gdb call stack.
1993 \\{gdb-frames-mode-map}"
1994 (kill-all-local-variables)
1995 (setq major-mode 'gdb-frames-mode)
1996 (setq mode-name "Frames")
1997 (setq buffer-read-only t)
1998 (use-local-map gdb-frames-mode-map)
1999 (run-mode-hooks 'gdb-frames-mode-hook)
2000 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2001 'gdb-invalidate-frames
2002 'gdbmi-invalidate-frames))
2004 (defun gdb-get-frame-number ()
2007 (let* ((start (line-beginning-position))
2008 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2009 (n (or (and pos (match-string-no-properties 1)) "0")))
2012 (defun gdb-frames-select (&optional event)
2013 "Select the frame and display the relevant source."
2014 (interactive (list last-input-event))
2015 (if event (posn-set-point (event-end event)))
2017 (list (concat gdb-server-prefix "frame "
2018 (gdb-get-frame-number) "\n") 'ignore))
2019 (gud-display-frame))
2022 ;; Threads buffer. This displays a selectable thread list.
2024 (gdb-set-buffer-rules 'gdb-threads-buffer
2025 'gdb-threads-buffer-name
2028 (def-gdb-auto-updated-buffer gdb-threads-buffer
2029 gdb-invalidate-threads
2030 (concat gdb-server-prefix "info threads\n")
2031 gdb-info-threads-handler
2032 gdb-info-threads-custom)
2034 (defun gdb-info-threads-custom ()
2035 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2036 (let ((buffer-read-only nil))
2037 (goto-char (point-min))
2038 (while (< (point) (point-max))
2039 (unless (looking-at "No ")
2040 (add-text-properties (line-beginning-position) (line-end-position)
2041 '(mouse-face highlight
2042 help-echo "mouse-2, RET: select thread")))
2043 (forward-line 1)))))
2045 (defun gdb-threads-buffer-name ()
2046 (with-current-buffer gud-comint-buffer
2047 (concat "*threads of " (gdb-get-target-string) "*")))
2049 (defun gdb-display-threads-buffer ()
2050 "Display IDs of currently known threads."
2053 (gdb-get-buffer-create 'gdb-threads-buffer)))
2055 (defun gdb-frame-threads-buffer ()
2056 "Display IDs of currently known threads in a new frame."
2058 (let ((special-display-regexps (append special-display-regexps '(".*")))
2059 (special-display-frame-alist gdb-frame-parameters))
2060 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2062 (defvar gdb-threads-mode-map
2063 (let ((map (make-sparse-keymap)))
2064 (suppress-keymap map)
2065 (define-key map "q" 'kill-this-buffer)
2066 (define-key map "\r" 'gdb-threads-select)
2067 (define-key map [mouse-2] 'gdb-threads-select)
2068 (define-key map [follow-link] 'mouse-face)
2071 (defvar gdb-threads-font-lock-keywords
2072 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2073 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2074 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2075 "Font lock keywords used in `gdb-threads-mode'.")
2077 (defun gdb-threads-mode ()
2078 "Major mode for gdb threads.
2080 \\{gdb-threads-mode-map}"
2081 (kill-all-local-variables)
2082 (setq major-mode 'gdb-threads-mode)
2083 (setq mode-name "Threads")
2084 (setq buffer-read-only t)
2085 (use-local-map gdb-threads-mode-map)
2086 (set (make-local-variable 'font-lock-defaults)
2087 '(gdb-threads-font-lock-keywords))
2088 (run-mode-hooks 'gdb-threads-mode-hook)
2089 'gdb-invalidate-threads)
2091 (defun gdb-get-thread-number ()
2093 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2094 (match-string-no-properties 1)))
2096 (defun gdb-threads-select (&optional event)
2097 "Select the thread and display the relevant source."
2098 (interactive (list last-input-event))
2099 (if event (posn-set-point (event-end event)))
2101 (list (concat gdb-server-prefix "thread "
2102 (gdb-get-thread-number) "\n") 'ignore))
2103 (gud-display-frame))
2106 ;; Registers buffer.
2108 (defcustom gdb-all-registers nil
2109 "Non-nil means include floating-point registers."
2114 (gdb-set-buffer-rules 'gdb-registers-buffer
2115 'gdb-registers-buffer-name
2116 'gdb-registers-mode)
2118 (def-gdb-auto-updated-buffer gdb-registers-buffer
2119 gdb-invalidate-registers
2121 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2122 gdb-info-registers-handler
2123 gdb-info-registers-custom)
2125 (defun gdb-info-registers-custom ()
2126 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2128 (let ((buffer-read-only nil)
2130 (goto-char (point-min))
2131 (while (< (point) (point-max))
2132 (setq start (line-beginning-position))
2133 (setq end (line-end-position))
2134 (when (looking-at "^[^ ]+")
2135 (unless (string-equal (match-string 0) "The")
2136 (put-text-property start (match-end 0)
2137 'face font-lock-variable-name-face)
2138 (add-text-properties start end
2139 '(help-echo "mouse-2: edit value"
2140 mouse-face highlight))))
2141 (forward-line 1))))))
2143 (defun gdb-edit-register-value (&optional event)
2144 (interactive (list last-input-event))
2146 (if event (posn-set-point (event-end event)))
2148 (let* ((register (current-word))
2149 (value (read-string (format "New value (%s): " register))))
2151 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2154 (defvar gdb-registers-mode-map
2155 (let ((map (make-sparse-keymap)))
2156 (suppress-keymap map)
2157 (define-key map "\r" 'gdb-edit-register-value)
2158 (define-key map [mouse-2] 'gdb-edit-register-value)
2159 (define-key map " " 'gdb-all-registers)
2160 (define-key map "q" 'kill-this-buffer)
2163 (defun gdb-registers-mode ()
2164 "Major mode for gdb registers.
2166 \\{gdb-registers-mode-map}"
2167 (kill-all-local-variables)
2168 (setq major-mode 'gdb-registers-mode)
2169 (setq mode-name "Registers")
2170 (setq buffer-read-only t)
2171 (use-local-map gdb-registers-mode-map)
2172 (run-mode-hooks 'gdb-registers-mode-hook)
2173 (if (string-equal gdb-version "pre-6.4")
2175 (if gdb-all-registers (setq mode-name "Registers:All"))
2176 'gdb-invalidate-registers)
2177 'gdb-invalidate-registers-1))
2179 (defun gdb-registers-buffer-name ()
2180 (with-current-buffer gud-comint-buffer
2181 (concat "*registers of " (gdb-get-target-string) "*")))
2183 (defun gdb-display-registers-buffer ()
2184 "Display integer register contents."
2187 (gdb-get-buffer-create 'gdb-registers-buffer)))
2189 (defun gdb-frame-registers-buffer ()
2190 "Display integer register contents in a new frame."
2192 (let ((special-display-regexps (append special-display-regexps '(".*")))
2193 (special-display-frame-alist gdb-frame-parameters))
2194 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2196 (defun gdb-all-registers ()
2197 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2199 (when (string-equal gdb-version "pre-6.4")
2200 (if gdb-all-registers
2202 (setq gdb-all-registers nil)
2203 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2204 (setq mode-name "Registers")))
2205 (setq gdb-all-registers t)
2206 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2207 (setq mode-name "Registers:All")))
2208 (message (format "Display of floating-point registers %sabled"
2209 (if gdb-all-registers "en" "dis")))
2210 (gdb-invalidate-registers)))
2215 (defcustom gdb-memory-repeat-count 32
2216 "Number of data items in memory window."
2221 (defcustom gdb-memory-format "x"
2222 "Display format of data items in memory window."
2223 :type '(choice (const :tag "Hexadecimal" "x")
2224 (const :tag "Signed decimal" "d")
2225 (const :tag "Unsigned decimal" "u")
2226 (const :tag "Octal" "o")
2227 (const :tag "Binary" "t"))
2231 (defcustom gdb-memory-unit "w"
2232 "Unit size of data items in memory window."
2233 :type '(choice (const :tag "Byte" "b")
2234 (const :tag "Halfword" "h")
2235 (const :tag "Word" "w")
2236 (const :tag "Giant word" "g"))
2240 (gdb-set-buffer-rules 'gdb-memory-buffer
2241 'gdb-memory-buffer-name
2244 (def-gdb-auto-updated-buffer gdb-memory-buffer
2245 gdb-invalidate-memory
2246 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2247 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2248 gdb-read-memory-handler
2249 gdb-read-memory-custom)
2251 (defun gdb-read-memory-custom ()
2253 (goto-char (point-min))
2254 (if (looking-at "0x[[:xdigit:]]+")
2255 (setq gdb-memory-address (match-string 0)))))
2257 (defvar gdb-memory-mode-map
2258 (let ((map (make-sparse-keymap)))
2259 (suppress-keymap map)
2260 (define-key map "q" 'kill-this-buffer)
2263 (defun gdb-memory-set-address (event)
2264 "Set the start memory address."
2266 (save-selected-window
2267 (select-window (posn-window (event-start event)))
2268 (let ((arg (read-from-minibuffer "Memory address: ")))
2269 (setq gdb-memory-address arg))
2270 (gdb-invalidate-memory)))
2272 (defun gdb-memory-set-repeat-count (event)
2273 "Set the number of data items in memory window."
2275 (save-selected-window
2276 (select-window (posn-window (event-start event)))
2277 (let* ((arg (read-from-minibuffer "Repeat count: "))
2278 (count (string-to-number arg)))
2280 (error "Positive numbers only")
2281 (customize-set-variable 'gdb-memory-repeat-count count)
2282 (gdb-invalidate-memory)))))
2284 (defun gdb-memory-format-binary ()
2285 "Set the display format to binary."
2287 (customize-set-variable 'gdb-memory-format "t")
2288 (gdb-invalidate-memory))
2290 (defun gdb-memory-format-octal ()
2291 "Set the display format to octal."
2293 (customize-set-variable 'gdb-memory-format "o")
2294 (gdb-invalidate-memory))
2296 (defun gdb-memory-format-unsigned ()
2297 "Set the display format to unsigned decimal."
2299 (customize-set-variable 'gdb-memory-format "u")
2300 (gdb-invalidate-memory))
2302 (defun gdb-memory-format-signed ()
2303 "Set the display format to decimal."
2305 (customize-set-variable 'gdb-memory-format "d")
2306 (gdb-invalidate-memory))
2308 (defun gdb-memory-format-hexadecimal ()
2309 "Set the display format to hexadecimal."
2311 (customize-set-variable 'gdb-memory-format "x")
2312 (gdb-invalidate-memory))
2314 (defvar gdb-memory-format-map
2315 (let ((map (make-sparse-keymap)))
2316 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2318 "Keymap to select format in the header line.")
2320 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2321 "Menu of display formats in the header line.")
2323 (define-key gdb-memory-format-menu [binary]
2324 '(menu-item "Binary" gdb-memory-format-binary
2325 :button (:radio . (equal gdb-memory-format "t"))))
2326 (define-key gdb-memory-format-menu [octal]
2327 '(menu-item "Octal" gdb-memory-format-octal
2328 :button (:radio . (equal gdb-memory-format "o"))))
2329 (define-key gdb-memory-format-menu [unsigned]
2330 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2331 :button (:radio . (equal gdb-memory-format "u"))))
2332 (define-key gdb-memory-format-menu [signed]
2333 '(menu-item "Signed Decimal" gdb-memory-format-signed
2334 :button (:radio . (equal gdb-memory-format "d"))))
2335 (define-key gdb-memory-format-menu [hexadecimal]
2336 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2337 :button (:radio . (equal gdb-memory-format "x"))))
2339 (defun gdb-memory-format-menu (event)
2341 (x-popup-menu event gdb-memory-format-menu))
2343 (defun gdb-memory-format-menu-1 (event)
2345 (save-selected-window
2346 (select-window (posn-window (event-start event)))
2347 (let* ((selection (gdb-memory-format-menu event))
2348 (binding (and selection (lookup-key gdb-memory-format-menu
2349 (vector (car selection))))))
2350 (if binding (call-interactively binding)))))
2352 (defun gdb-memory-unit-giant ()
2353 "Set the unit size to giant words (eight bytes)."
2355 (customize-set-variable 'gdb-memory-unit "g")
2356 (gdb-invalidate-memory))
2358 (defun gdb-memory-unit-word ()
2359 "Set the unit size to words (four bytes)."
2361 (customize-set-variable 'gdb-memory-unit "w")
2362 (gdb-invalidate-memory))
2364 (defun gdb-memory-unit-halfword ()
2365 "Set the unit size to halfwords (two bytes)."
2367 (customize-set-variable 'gdb-memory-unit "h")
2368 (gdb-invalidate-memory))
2370 (defun gdb-memory-unit-byte ()
2371 "Set the unit size to bytes."
2373 (customize-set-variable 'gdb-memory-unit "b")
2374 (gdb-invalidate-memory))
2376 (defvar gdb-memory-unit-map
2377 (let ((map (make-sparse-keymap)))
2378 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2380 "Keymap to select units in the header line.")
2382 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2383 "Menu of units in the header line.")
2385 (define-key gdb-memory-unit-menu [giantwords]
2386 '(menu-item "Giant words" gdb-memory-unit-giant
2387 :button (:radio . (equal gdb-memory-unit "g"))))
2388 (define-key gdb-memory-unit-menu [words]
2389 '(menu-item "Words" gdb-memory-unit-word
2390 :button (:radio . (equal gdb-memory-unit "w"))))
2391 (define-key gdb-memory-unit-menu [halfwords]
2392 '(menu-item "Halfwords" gdb-memory-unit-halfword
2393 :button (:radio . (equal gdb-memory-unit "h"))))
2394 (define-key gdb-memory-unit-menu [bytes]
2395 '(menu-item "Bytes" gdb-memory-unit-byte
2396 :button (:radio . (equal gdb-memory-unit "b"))))
2398 (defun gdb-memory-unit-menu (event)
2400 (x-popup-menu event gdb-memory-unit-menu))
2402 (defun gdb-memory-unit-menu-1 (event)
2404 (save-selected-window
2405 (select-window (posn-window (event-start event)))
2406 (let* ((selection (gdb-memory-unit-menu event))
2407 (binding (and selection (lookup-key gdb-memory-unit-menu
2408 (vector (car selection))))))
2409 (if binding (call-interactively binding)))))
2411 ;;from make-mode-line-mouse-map
2412 (defun gdb-make-header-line-mouse-map (mouse function) "\
2413 Return a keymap with single entry for mouse key MOUSE on the header line.
2414 MOUSE is defined to run function FUNCTION with no args in the buffer
2415 corresponding to the mode line clicked."
2416 (let ((map (make-sparse-keymap)))
2417 (define-key map (vector 'header-line mouse) function)
2418 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2421 (defvar gdb-memory-font-lock-keywords
2422 '(;; <__function.name+n>
2423 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2425 "Font lock keywords used in `gdb-memory-mode'.")
2427 (defun gdb-memory-mode ()
2428 "Major mode for examining memory.
2430 \\{gdb-memory-mode-map}"
2431 (kill-all-local-variables)
2432 (setq major-mode 'gdb-memory-mode)
2433 (setq mode-name "Memory")
2434 (setq buffer-read-only t)
2435 (use-local-map gdb-memory-mode-map)
2436 (setq header-line-format
2442 'face font-lock-warning-face
2443 'help-echo "mouse-1: Decrement address"
2444 'mouse-face 'mode-line-highlight
2446 (gdb-make-header-line-mouse-map
2448 #'(lambda () (interactive)
2449 (let ((gdb-memory-address
2450 ;; Let GDB do the arithmetic.
2452 gdb-memory-address " - "
2454 (* gdb-memory-repeat-count
2455 (cond ((string= gdb-memory-unit "b") 1)
2456 ((string= gdb-memory-unit "h") 2)
2457 ((string= gdb-memory-unit "w") 4)
2458 ((string= gdb-memory-unit "g") 8)))))))
2459 (gdb-invalidate-memory)))))
2462 'face font-lock-warning-face
2463 'help-echo "mouse-1: Increment address"
2464 'mouse-face 'mode-line-highlight
2465 'local-map (gdb-make-header-line-mouse-map
2467 #'(lambda () (interactive)
2468 (let ((gdb-memory-address nil))
2469 (gdb-invalidate-memory)))))
2471 (propertize gdb-memory-address
2472 'face font-lock-warning-face
2473 'help-echo "mouse-1: Set memory address"
2474 'mouse-face 'mode-line-highlight
2475 'local-map (gdb-make-header-line-mouse-map
2477 #'gdb-memory-set-address))
2479 (propertize (number-to-string gdb-memory-repeat-count)
2480 'face font-lock-warning-face
2481 'help-echo "mouse-1: Set repeat count"
2482 'mouse-face 'mode-line-highlight
2483 'local-map (gdb-make-header-line-mouse-map
2485 #'gdb-memory-set-repeat-count))
2487 (propertize gdb-memory-format
2488 'face font-lock-warning-face
2489 'help-echo "mouse-3: Select display format"
2490 'mouse-face 'mode-line-highlight
2491 'local-map gdb-memory-format-map)
2493 (propertize gdb-memory-unit
2494 'face font-lock-warning-face
2495 'help-echo "mouse-3: Select unit size"
2496 'mouse-face 'mode-line-highlight
2497 'local-map gdb-memory-unit-map))))
2498 (set (make-local-variable 'font-lock-defaults)
2499 '(gdb-memory-font-lock-keywords))
2500 (run-mode-hooks 'gdb-memory-mode-hook)
2501 'gdb-invalidate-memory)
2503 (defun gdb-memory-buffer-name ()
2504 (with-current-buffer gud-comint-buffer
2505 (concat "*memory of " (gdb-get-target-string) "*")))
2507 (defun gdb-display-memory-buffer ()
2508 "Display memory contents."
2511 (gdb-get-buffer-create 'gdb-memory-buffer)))
2513 (defun gdb-frame-memory-buffer ()
2514 "Display memory contents in a new frame."
2516 (let ((special-display-regexps (append special-display-regexps '(".*")))
2517 (special-display-frame-alist gdb-frame-parameters))
2518 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2523 (gdb-set-buffer-rules 'gdb-locals-buffer
2524 'gdb-locals-buffer-name
2527 (def-gdb-auto-update-trigger gdb-invalidate-locals
2528 (gdb-get-buffer 'gdb-locals-buffer)
2529 "server info locals\n"
2530 gdb-info-locals-handler)
2532 (defvar gdb-locals-watch-map
2533 (let ((map (make-sparse-keymap)))
2534 (define-key map "\r" '(lambda () (interactive)
2537 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2538 (mouse-set-point event)
2542 "Keymap to create watch expression of a complex data type local variable.")
2544 (defconst gdb-struct-string
2545 (concat (propertize "[struct/union]"
2546 'mouse-face 'highlight
2547 'help-echo "mouse-2: create watch expression"
2548 'local-map gdb-locals-watch-map) "\n"))
2550 (defconst gdb-array-string
2551 (concat " " (propertize "[array]"
2552 'mouse-face 'highlight
2553 'help-echo "mouse-2: create watch expression"
2554 'local-map gdb-locals-watch-map) "\n"))
2556 ;; Abbreviate for arrays and structures.
2557 ;; These can be expanded using gud-display.
2558 (defun gdb-info-locals-handler ()
2559 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2560 gdb-pending-triggers))
2561 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2562 (with-current-buffer buf
2563 (goto-char (point-min))
2564 (while (re-search-forward "^[ }].*\n" nil t)
2565 (replace-match "" nil nil))
2566 (goto-char (point-min))
2567 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2568 (replace-match gdb-struct-string nil nil))
2569 (goto-char (point-min))
2570 (while (re-search-forward "\\s-*{.*\n" nil t)
2571 (replace-match gdb-array-string nil nil))))
2572 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2574 (with-current-buffer buf
2575 (let* ((window (get-buffer-window buf 0))
2576 (start (window-start window))
2577 (p (window-point window))
2578 (buffer-read-only nil))
2580 (insert-buffer-substring (gdb-get-buffer-create
2581 'gdb-partial-output-buffer))
2582 (set-window-start window start)
2583 (set-window-point window p))
2585 (run-hooks 'gdb-info-locals-hook))
2587 (defvar gdb-locals-mode-map
2588 (let ((map (make-sparse-keymap)))
2589 (suppress-keymap map)
2590 (define-key map "q" 'kill-this-buffer)
2593 (defun gdb-locals-mode ()
2594 "Major mode for gdb locals.
2596 \\{gdb-locals-mode-map}"
2597 (kill-all-local-variables)
2598 (setq major-mode 'gdb-locals-mode)
2599 (setq mode-name (concat "Locals:" gdb-selected-frame))
2600 (setq buffer-read-only t)
2601 (use-local-map gdb-locals-mode-map)
2602 (set (make-local-variable 'font-lock-defaults)
2603 '(gdb-locals-font-lock-keywords))
2604 (run-mode-hooks 'gdb-locals-mode-hook)
2605 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2606 (string-equal gdb-version "pre-6.4"))
2607 'gdb-invalidate-locals
2608 'gdb-invalidate-locals-1))
2610 (defun gdb-locals-buffer-name ()
2611 (with-current-buffer gud-comint-buffer
2612 (concat "*locals of " (gdb-get-target-string) "*")))
2614 (defun gdb-display-locals-buffer ()
2615 "Display local variables of current stack and their values."
2618 (gdb-get-buffer-create 'gdb-locals-buffer)))
2620 (defun gdb-frame-locals-buffer ()
2621 "Display local variables of current stack and their values in a new frame."
2623 (let ((special-display-regexps (append special-display-regexps '(".*")))
2624 (special-display-frame-alist gdb-frame-parameters))
2625 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2628 ;;;; Window management
2629 (defun gdb-display-buffer (buf &optional size)
2630 (let ((answer (get-buffer-window buf 0))
2633 (display-buffer buf nil 0) ;Raise the frame if necessary.
2634 ;; The buffer is not yet displayed.
2635 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2636 (let ((window (get-lru-window)))
2638 (not (eq window (get-buffer-window gud-comint-buffer))))
2640 (set-window-buffer window buf)
2641 (setq answer window))
2642 (setq must-split t)))
2644 (let* ((largest (get-largest-window))
2645 (cur-size (window-height largest))
2646 (new-size (and size (< size cur-size) (- cur-size size))))
2647 (setq answer (split-window largest new-size))
2648 (set-window-buffer answer buf)
2649 (set-window-dedicated-p answer t)))
2653 ;;; Shared keymap initialization:
2655 (let ((menu (make-sparse-keymap "GDB-Windows")))
2656 (define-key gud-menu-map [displays]
2657 `(menu-item "GDB-Windows" ,menu
2658 :visible (memq gud-minor-mode '(gdbmi gdba))))
2659 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2660 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2661 (define-key menu [inferior]
2662 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2663 :enable gdb-use-separate-io-buffer))
2664 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2665 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2666 (define-key menu [disassembly]
2667 '("Disassembly" . gdb-display-assembler-buffer))
2668 (define-key menu [breakpoints]
2669 '("Breakpoints" . gdb-display-breakpoints-buffer))
2670 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2671 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2673 (let ((menu (make-sparse-keymap "GDB-Frames")))
2674 (define-key gud-menu-map [frames]
2675 `(menu-item "GDB-Frames" ,menu
2676 :visible (memq gud-minor-mode '(gdbmi gdba))))
2677 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2678 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2679 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2680 (define-key menu [inferior]
2681 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2682 :enable gdb-use-separate-io-buffer))
2683 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2684 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2685 (define-key menu [breakpoints]
2686 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2687 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2688 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2690 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2691 (define-key gud-menu-map [ui]
2692 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2693 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2694 (define-key menu [gdb-use-separate-io]
2695 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2696 :visible (eq gud-minor-mode 'gdba)
2697 :help "Toggle separate IO for inferior."
2698 :button (:toggle . gdb-use-separate-io-buffer)))
2699 (define-key menu [gdb-many-windows]
2700 '(menu-item "Display Other Windows" gdb-many-windows
2701 :help "Toggle display of locals, stack and breakpoint information"
2702 :button (:toggle . gdb-many-windows)))
2703 (define-key menu [gdb-restore-windows]
2704 '(menu-item "Restore Window Layout" gdb-restore-windows
2705 :help "Restore standard layout for debug session.")))
2707 (defun gdb-frame-gdb-buffer ()
2708 "Display GUD buffer in a new frame."
2710 (let ((special-display-regexps (append special-display-regexps '(".*")))
2711 (special-display-frame-alist gdb-frame-parameters)
2712 (same-window-regexps nil))
2713 (display-buffer gud-comint-buffer)))
2715 (defun gdb-display-gdb-buffer ()
2716 "Display GUD buffer."
2718 (let ((same-window-regexps nil))
2719 (pop-to-buffer gud-comint-buffer)))
2721 (defun gdb-set-window-buffer (name)
2722 (set-window-buffer (selected-window) (get-buffer name))
2723 (set-window-dedicated-p (selected-window) t))
2725 (defun gdb-setup-windows ()
2726 "Layout the window pattern for `gdb-many-windows'."
2727 (gdb-display-locals-buffer)
2728 (gdb-display-stack-buffer)
2729 (delete-other-windows)
2730 (gdb-display-breakpoints-buffer)
2731 (delete-other-windows)
2733 (pop-to-buffer gud-comint-buffer)
2734 (split-window nil ( / ( * (window-height) 3) 4))
2735 (split-window nil ( / (window-height) 3))
2736 (split-window-horizontally)
2738 (gdb-set-window-buffer (gdb-locals-buffer-name))
2741 (if gud-last-last-frame
2742 (gud-find-file (car gud-last-last-frame))
2743 (gud-find-file gdb-main-file)))
2744 (when gdb-use-separate-io-buffer
2745 (split-window-horizontally)
2747 (gdb-set-window-buffer
2748 (gdb-get-buffer-create 'gdb-inferior-io)))
2750 (gdb-set-window-buffer (gdb-stack-buffer-name))
2751 (split-window-horizontally)
2753 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2756 (defun gdb-restore-windows ()
2757 "Restore the basic arrangement of windows used by gdba.
2758 This arrangement depends on the value of `gdb-many-windows'."
2760 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2761 (delete-other-windows)
2762 (if gdb-many-windows
2764 (when (or gud-last-last-frame gdb-show-main)
2768 (if gud-last-last-frame
2769 (gud-find-file (car gud-last-last-frame))
2770 (gud-find-file gdb-main-file)))
2774 "Exit a debugging session cleanly.
2775 Kills the gdb buffers, and resets variables and the source buffers."
2776 (dolist (buffer (buffer-list))
2777 (unless (eq buffer gud-comint-buffer)
2778 (with-current-buffer buffer
2779 (if (memq gud-minor-mode '(gdbmi gdba))
2780 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2782 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2783 (setq gud-minor-mode nil)
2784 (kill-local-variable 'tool-bar-map)
2785 (kill-local-variable 'gdb-define-alist))))))
2786 (when (markerp gdb-overlay-arrow-position)
2787 (move-marker gdb-overlay-arrow-position nil)
2788 (setq gdb-overlay-arrow-position nil))
2789 (setq overlay-arrow-variable-list
2790 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2791 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2792 (if (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
2794 (setq gud-running nil)
2795 (setq gdb-active-process nil)
2796 (setq gdb-var-list nil)
2797 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2799 (defun gdb-source-info ()
2800 "Find the source file where the program starts and displays it with related
2802 (goto-char (point-min))
2803 (if (and (search-forward "Located in " nil t)
2804 (looking-at "\\S-+"))
2805 (setq gdb-main-file (match-string 0)))
2806 (goto-char (point-min))
2807 (if (search-forward "Includes preprocessor macro info." nil t)
2808 (setq gdb-macro-info t))
2809 (if gdb-many-windows
2811 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2813 (let ((pop-up-windows t))
2814 (display-buffer (gud-find-file gdb-main-file))))))
2816 (defun gdb-get-location (bptno line flag)
2817 "Find the directory containing the relevant source file.
2818 Put in buffer and place breakpoint icon."
2819 (goto-char (point-min))
2820 (catch 'file-not-found
2821 (if (search-forward "Located in " nil t)
2822 (when (looking-at "\\S-+")
2823 (delete (cons bptno "File not found") gdb-location-alist)
2824 (push (cons bptno (match-string 0)) gdb-location-alist))
2826 (unless (assoc bptno gdb-location-alist)
2827 (push (cons bptno "File not found") gdb-location-alist)
2828 (message-box "Cannot find source file for breakpoint location.\n\
2829 Add directory to search path for source files using the GDB command, dir."))
2830 (throw 'file-not-found nil))
2831 (with-current-buffer
2832 (find-file-noselect (match-string 0))
2833 (save-current-buffer
2834 (set (make-local-variable 'gud-minor-mode) 'gdba)
2835 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2836 ;; only want one breakpoint icon at each location
2838 (goto-line (string-to-number line))
2839 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2841 (add-hook 'find-file-hook 'gdb-find-file-hook)
2843 (defun gdb-find-file-hook ()
2844 "Set up buffer for debugging if file is part of the source code
2845 of the current session."
2846 (if (and (buffer-name gud-comint-buffer)
2847 ;; in case gud or gdb-ui is just loaded
2849 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2851 (if (member buffer-file-name gdb-source-file-list)
2852 (with-current-buffer (find-buffer-visiting buffer-file-name)
2853 (set (make-local-variable 'gud-minor-mode)
2854 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2855 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2858 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2859 "Put string PUTSTRING in front of POS in the current buffer.
2860 PUTSTRING is displayed by putting an overlay into the current buffer with a
2861 `before-string' string that has a `display' property whose value is
2863 (let ((string (make-string 1 ?x))
2864 (buffer (current-buffer)))
2865 (setq putstring (copy-sequence putstring))
2866 (let ((overlay (make-overlay pos pos buffer))
2868 (list (list 'margin 'left-margin) putstring))))
2869 (put-text-property 0 1 'display prop string)
2871 (add-text-properties 0 1 sprops string))
2872 (overlay-put overlay 'put-break t)
2873 (overlay-put overlay 'before-string string))))
2875 ;;from remove-images
2876 (defun gdb-remove-strings (start end &optional buffer)
2877 "Remove strings between START and END in BUFFER.
2878 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2879 BUFFER nil or omitted means use the current buffer."
2881 (setq buffer (current-buffer)))
2882 (dolist (overlay (overlays-in start end))
2883 (when (overlay-get overlay 'put-break)
2884 (delete-overlay overlay))))
2886 (defun gdb-put-breakpoint-icon (enabled bptno)
2887 (let ((start (- (line-beginning-position) 1))
2888 (end (+ (line-end-position) 1))
2889 (putstring (if enabled "B" "b"))
2890 (source-window (get-buffer-window (current-buffer) 0)))
2891 (add-text-properties
2892 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2895 (add-text-properties
2896 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2897 (add-text-properties
2898 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2899 (gdb-remove-breakpoint-icons start end)
2900 (if (display-images-p)
2901 (if (>= (or left-fringe-width
2902 (if source-window (car (window-fringes source-window)))
2903 gdb-buffer-fringe-width) 8)
2906 `(left-fringe breakpoint
2909 'breakpoint-disabled))
2911 'gdb-enabled enabled)
2912 (when (< left-margin-width 2)
2913 (save-current-buffer
2914 (setq left-margin-width 2)
2918 left-margin-width right-margin-width))))
2921 (or breakpoint-enabled-icon
2922 (setq breakpoint-enabled-icon
2923 (find-image `((:type xpm :data
2924 ,breakpoint-xpm-data
2925 :ascent 100 :pointer hand)
2927 ,breakpoint-enabled-pbm-data
2928 :ascent 100 :pointer hand)))))
2929 (or breakpoint-disabled-icon
2930 (setq breakpoint-disabled-icon
2931 (find-image `((:type xpm :data
2932 ,breakpoint-xpm-data
2933 :conversion disabled
2934 :ascent 100 :pointer hand)
2936 ,breakpoint-disabled-pbm-data
2937 :ascent 100 :pointer hand))))))
2941 (when (< left-margin-width 2)
2942 (save-current-buffer
2943 (setq left-margin-width 2)
2944 (let ((window (get-buffer-window (current-buffer) 0)))
2947 window left-margin-width right-margin-width)))))
2949 (propertize putstring
2950 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2953 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2954 (gdb-remove-strings start end)
2955 (if (display-images-p)
2956 (remove-images start end))
2958 (setq left-margin-width 0)
2959 (let ((window (get-buffer-window (current-buffer) 0)))
2962 window left-margin-width right-margin-width)))))
2966 ;; Assembler buffer.
2968 (gdb-set-buffer-rules 'gdb-assembler-buffer
2969 'gdb-assembler-buffer-name
2970 'gdb-assembler-mode)
2972 (def-gdb-auto-update-handler gdb-assembler-handler
2973 gdb-invalidate-assembler
2974 gdb-assembler-buffer
2975 gdb-assembler-custom)
2977 (defun gdb-assembler-custom ()
2978 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2979 (pos 1) (address) (flag) (bptno))
2980 (with-current-buffer buffer
2982 (if (not (equal gdb-frame-address "main"))
2984 (goto-char (point-min))
2985 (if (and gdb-frame-address
2986 (search-forward gdb-frame-address nil t))
2990 (or gdb-overlay-arrow-position
2991 (setq gdb-overlay-arrow-position (make-marker)))
2992 (set-marker gdb-overlay-arrow-position
2993 (point) (current-buffer))))))
2994 ;; remove all breakpoint-icons in assembler buffer before updating.
2995 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2996 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2997 (goto-char (point-min))
2998 (while (< (point) (- (point-max) 1))
3000 (if (looking-at "[^\t].*?breakpoint")
3003 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3004 (setq bptno (match-string 1))
3005 (setq flag (char-after (match-beginning 2)))
3006 (setq address (match-string 3))
3007 (with-current-buffer buffer
3009 (goto-char (point-min))
3010 (if (search-forward address nil t)
3011 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3012 (if (not (equal gdb-frame-address "main"))
3013 (with-current-buffer buffer
3014 (set-window-point (get-buffer-window buffer 0) pos)))))
3016 (defvar gdb-assembler-mode-map
3017 (let ((map (make-sparse-keymap)))
3018 (suppress-keymap map)
3019 (define-key map "q" 'kill-this-buffer)
3022 (defvar gdb-assembler-font-lock-keywords
3023 '(;; <__function.name+n>
3024 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3025 (1 font-lock-function-name-face))
3026 ;; 0xNNNNNNNN <__function.name+n>: opcode
3027 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3028 (4 font-lock-keyword-face))
3029 ;; %register(at least i386)
3030 ("%\\sw+" . font-lock-variable-name-face)
3031 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3032 (1 font-lock-comment-face)
3033 (2 font-lock-function-name-face))
3034 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3035 "Font lock keywords used in `gdb-assembler-mode'.")
3037 (defun gdb-assembler-mode ()
3038 "Major mode for viewing code assembler.
3040 \\{gdb-assembler-mode-map}"
3041 (kill-all-local-variables)
3042 (setq major-mode 'gdb-assembler-mode)
3043 (setq mode-name (concat "Machine:" gdb-selected-frame))
3044 (setq gdb-overlay-arrow-position nil)
3045 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3046 (setq fringes-outside-margins t)
3047 (setq buffer-read-only t)
3048 (use-local-map gdb-assembler-mode-map)
3049 (gdb-invalidate-assembler)
3050 (set (make-local-variable 'font-lock-defaults)
3051 '(gdb-assembler-font-lock-keywords))
3052 (run-mode-hooks 'gdb-assembler-mode-hook)
3053 'gdb-invalidate-assembler)
3055 (defun gdb-assembler-buffer-name ()
3056 (with-current-buffer gud-comint-buffer
3057 (concat "*disassembly of " (gdb-get-target-string) "*")))
3059 (defun gdb-display-assembler-buffer ()
3060 "Display disassembly view."
3062 (setq gdb-previous-frame nil)
3064 (gdb-get-buffer-create 'gdb-assembler-buffer)))
3066 (defun gdb-frame-assembler-buffer ()
3067 "Display disassembly view in a new frame."
3069 (setq gdb-previous-frame nil)
3070 (let ((special-display-regexps (append special-display-regexps '(".*")))
3071 (special-display-frame-alist gdb-frame-parameters))
3072 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3074 ;; modified because if gdb-frame-address has changed value a new command
3075 ;; must be enqueued to update the buffer with the new output
3076 (defun gdb-invalidate-assembler (&optional ignored)
3077 (if (gdb-get-buffer 'gdb-assembler-buffer)
3079 (unless (and gdb-selected-frame
3080 (string-equal gdb-selected-frame gdb-previous-frame))
3081 (if (or (not (member 'gdb-invalidate-assembler
3082 gdb-pending-triggers))
3083 (not (string-equal gdb-frame-address
3084 gdb-previous-frame-address)))
3086 ;; take previous disassemble command, if any, off the queue
3087 (with-current-buffer gud-comint-buffer
3088 (let ((queue gdb-input-queue))
3089 (dolist (item queue)
3090 (if (equal (cdr item) '(gdb-assembler-handler))
3091 (setq gdb-input-queue
3092 (delete item gdb-input-queue))))))
3095 (concat gdb-server-prefix "disassemble "
3096 (if (member gdb-frame-address '(nil "main")) nil "0x")
3097 gdb-frame-address "\n")
3098 'gdb-assembler-handler))
3099 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3100 (setq gdb-previous-frame-address gdb-frame-address)
3101 (setq gdb-previous-frame gdb-selected-frame)))))))
3103 (defun gdb-get-selected-frame ()
3104 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3107 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3108 (push 'gdb-get-selected-frame
3109 gdb-pending-triggers))))
3111 (defun gdb-frame-handler ()
3112 (setq gdb-pending-triggers
3113 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3114 (goto-char (point-min))
3115 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
3116 (setq gdb-frame-number (match-string 1)))
3117 (if gud-overlay-arrow-position
3118 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3119 (position (marker-position gud-overlay-arrow-position)))
3121 (with-current-buffer buffer
3122 (setq fringe-indicator-alist
3123 (if (string-equal gdb-frame-number "0")
3125 '((overlay-arrow . hollow-right-triangle))))
3126 (setq gud-overlay-arrow-position (make-marker))
3127 (set-marker gud-overlay-arrow-position position)))))
3128 (goto-char (point-min))
3129 (if (re-search-forward
3130 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
3132 (setq gdb-selected-frame (match-string 2))
3133 (if (gdb-get-buffer 'gdb-locals-buffer)
3134 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3135 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3136 (if (gdb-get-buffer 'gdb-assembler-buffer)
3137 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3138 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3139 (setq gdb-frame-address (match-string 1))))
3140 (goto-char (point-min))
3141 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
3142 (setq gdb-current-language (match-string 1)))
3143 (gdb-invalidate-assembler))
3146 ;; Code specific to GDB 6.4
3147 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3149 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3150 "Create list of source files for current GDB session.
3151 If buffers already exist for any of these files, gud-minor-mode
3153 (goto-char (point-min))
3154 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3155 (push (match-string 1) gdb-source-file-list))
3156 (dolist (buffer (buffer-list))
3157 (with-current-buffer buffer
3158 (when (member buffer-file-name gdb-source-file-list)
3159 (set (make-local-variable 'gud-minor-mode)
3160 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3161 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3162 (when gud-tooltip-mode
3163 (make-local-variable 'gdb-define-alist)
3164 (gdb-create-define-alist)
3165 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
3167 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3168 (defun gdb-var-list-children-1 (varnum)
3171 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3172 (concat "server interpreter mi \"-var-list-children --all-values "
3174 (concat "-var-list-children --all-values " varnum "\n"))
3175 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3177 (defconst gdb-var-list-children-regexp-1
3178 "name=\"\\(.+?\\)\",exp=\"\\(.+?\\)\",numchild=\"\\(.+?\\)\",\
3179 value=\\(\".*?\"\\),type=\"\\(.+?\\)\"}")
3181 (defun gdb-var-list-children-handler-1 (varnum)
3182 (goto-char (point-min))
3183 (let ((var-list nil))
3184 (catch 'child-already-watched
3185 (dolist (var gdb-var-list)
3186 (if (string-equal varnum (car var))
3189 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3190 (let ((varchild (list (match-string 1)
3194 (read (match-string 4))
3196 (if (assoc (car varchild) gdb-var-list)
3197 (throw 'child-already-watched nil))
3198 (push varchild var-list))))
3199 (push var var-list)))
3200 (setq gdb-var-list (nreverse var-list)))))
3202 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3203 (defun gdb-var-update-1 ()
3204 (if (not (member 'gdb-var-update gdb-pending-triggers))
3208 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3209 "server interpreter mi \"-var-update --all-values *\"\n"
3210 "-var-update --all-values *\n")
3211 'gdb-var-update-handler-1))
3212 (push 'gdb-var-update gdb-pending-triggers))))
3214 (defconst gdb-var-update-regexp-1
3215 "name=\"\\(.*?\\)\",\\(?:value=\\(\".*?\"\\),\\)?in_scope=\"\\(.*?\\)\"")
3217 (defun gdb-var-update-handler-1 ()
3218 (dolist (var gdb-var-list)
3219 (setcar (nthcdr 5 var) nil))
3220 (goto-char (point-min))
3221 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3222 (let* ((varnum (match-string 1))
3223 (var (assoc varnum gdb-var-list)))
3225 (if (string-equal (match-string 3) "false")
3226 (setcar (nthcdr 5 var) 'out-of-scope)
3227 (setcar (nthcdr 5 var) 'changed)
3228 (setcar (nthcdr 4 var)
3229 (read (match-string 2)))))))
3230 (setq gdb-pending-triggers
3231 (delq 'gdb-var-update gdb-pending-triggers))
3232 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3233 ;; dummy command to update speedbar at right time
3234 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
3235 ;; keep gdb-pending-triggers non-nil till end
3236 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
3238 ;; Registers buffer.
3240 (gdb-set-buffer-rules 'gdb-registers-buffer
3241 'gdb-registers-buffer-name
3242 'gdb-registers-mode)
3244 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3245 (gdb-get-buffer 'gdb-registers-buffer)
3246 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3247 "server interpreter mi \"-data-list-register-values x\"\n"
3248 "-data-list-register-values x\n")
3249 gdb-data-list-register-values-handler)
3251 (defconst gdb-data-list-register-values-regexp
3252 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
3254 (defun gdb-data-list-register-values-handler ()
3255 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3256 gdb-pending-triggers))
3257 (goto-char (point-min))
3258 (if (re-search-forward gdb-error-regexp nil t)
3259 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3260 (let ((buffer-read-only nil))
3262 (insert (match-string 1))
3263 (goto-char (point-min))))
3264 (let ((register-list (reverse gdb-register-names))
3265 (register nil) (register-string nil) (register-values nil))
3266 (goto-char (point-min))
3267 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3268 (setq register (pop register-list))
3269 (setq register-string (concat register "\t" (match-string 2) "\n"))
3270 (if (member (match-string 1) gdb-changed-registers)
3271 (put-text-property 0 (length register-string)
3272 'face 'font-lock-warning-face
3274 (setq register-values
3275 (concat register-values register-string)))
3276 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3277 (with-current-buffer buf
3278 (let* ((window (get-buffer-window buf 0))
3279 (start (window-start window))
3280 (p (window-point window))
3281 (buffer-read-only nil))
3283 (insert register-values)
3284 (set-window-start window start)
3285 (set-window-point window p))))))
3286 (gdb-data-list-register-values-custom))
3288 (defun gdb-data-list-register-values-custom ()
3289 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3291 (let ((buffer-read-only nil)
3293 (goto-char (point-min))
3294 (while (< (point) (point-max))
3295 (setq start (line-beginning-position))
3296 (setq end (line-end-position))
3297 (when (looking-at "^[^\t]+")
3298 (unless (string-equal (match-string 0) "No registers.")
3299 (put-text-property start (match-end 0)
3300 'face font-lock-variable-name-face)
3301 (add-text-properties start end
3302 '(help-echo "mouse-2: edit value"
3303 mouse-face highlight))))
3304 (forward-line 1))))))
3306 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3307 (defun gdb-get-changed-registers ()
3308 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3309 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3313 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3314 "server interpreter mi -data-list-changed-registers\n"
3315 "-data-list-changed-registers\n")
3316 'gdb-get-changed-registers-handler))
3317 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3319 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3321 (defun gdb-get-changed-registers-handler ()
3322 (setq gdb-pending-triggers
3323 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3324 (setq gdb-changed-registers nil)
3325 (goto-char (point-min))
3326 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3327 (push (match-string 1) gdb-changed-registers)))
3332 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3333 (gdb-set-buffer-rules 'gdb-locals-buffer
3334 'gdb-locals-buffer-name
3337 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3338 (gdb-get-buffer 'gdb-locals-buffer)
3339 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3340 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3341 "-stack-list-locals --simple-values\n")
3342 gdb-stack-list-locals-handler)
3344 (defconst gdb-stack-list-locals-regexp
3345 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3347 (defvar gdb-locals-watch-map-1
3348 (let ((map (make-sparse-keymap)))
3349 (define-key map [mouse-2] 'gud-watch)
3351 "Keymap to create watch expression of a complex data type local variable.")
3353 ;; Dont display values of arrays or structures.
3354 ;; These can be expanded using gud-watch.
3355 (defun gdb-stack-list-locals-handler ()
3356 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3357 gdb-pending-triggers))
3358 (let (local locals-list)
3359 (goto-char (point-min))
3360 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3361 (let ((local (list (match-string 1)
3364 (if (looking-at ",value=\\(\".*\"\\)}")
3365 (setcar (nthcdr 2 local) (read (match-string 1))))
3366 (push local locals-list)))
3367 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3368 (and buf (with-current-buffer buf
3369 (let* ((window (get-buffer-window buf 0))
3370 (start (window-start window))
3371 (p (window-point window))
3372 (buffer-read-only nil))
3374 (dolist (local locals-list)
3375 (setq name (car local))
3376 (if (or (not (nth 2 local))
3377 (string-match "\\0x" (nth 2 local)))
3378 (add-text-properties 0 (length name)
3379 `(mouse-face highlight
3380 help-echo "mouse-2: create watch expression"
3381 local-map ,gdb-locals-watch-map-1)
3384 (concat name "\t" (nth 1 local)
3385 "\t" (nth 2 local) "\n")))
3386 (set-window-start window start)
3387 (set-window-point window p)))))))
3389 (defun gdb-get-register-names ()
3390 "Create a list of register names."
3391 (goto-char (point-min))
3392 (setq gdb-register-names nil)
3393 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3394 (push (match-string 1) gdb-register-names)))
3398 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3399 ;;; gdb-ui.el ends here