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 vairable 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 (EXPRESSION VARNUM NUMCHILD TYPE VALUE STATUS) where
111 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
112 (defvar gdb-force-update t
113 "Non-nil means that view of watch expressions will be updated in the speedbar.")
114 (defvar gdb-main-file nil "Source file from which program execution begins.")
115 (defvar gdb-overlay-arrow-position nil)
116 (defvar gdb-server-prefix nil)
117 (defvar gdb-flush-pending-output nil)
118 (defvar gdb-location-alist nil
119 "Alist of breakpoint numbers and full filenames.")
120 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
121 and #define directives otherwise.")
122 (defvar gdb-error "Non-nil when GDB is reporting an error.")
123 (defvar gdb-macro-info nil
124 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
125 (defvar gdb-buffer-fringe-width nil)
126 (defvar gdb-signalled nil)
127 (defvar gdb-source-window nil)
129 (defvar gdb-buffer-type nil
130 "One of the symbols bound in `gdb-buffer-rules'.")
131 (make-variable-buffer-local 'gdb-buffer-type)
133 (defvar gdb-input-queue ()
134 "A list of gdb command objects.")
136 (defvar gdb-prompting nil
137 "True when gdb is idle with no pending input.")
139 (defvar gdb-output-sink 'user
140 "The disposition of the output of the current gdb command.
141 Possible values are these symbols:
143 `user' -- gdb output should be copied to the GUD buffer
146 `inferior' -- gdb output should be copied to the inferior-io buffer.
148 `pre-emacs' -- output should be ignored util the post-prompt
149 annotation is received. Then the output-sink
151 `emacs' -- output should be collected in the partial-output-buffer
152 for subsequent processing by a command. This is the
153 disposition of output generated by commands that
154 gdb mode sends to gdb on its own behalf.
155 `post-emacs' -- ignore output until the prompt annotation is
156 received, then go to USER disposition.
158 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
159 \(`user' and `emacs').")
161 (defvar gdb-current-item nil
162 "The most recent command item sent to gdb.")
164 (defvar gdb-pending-triggers '()
165 "A list of trigger functions that have run later than their output
168 (defvar gdb-first-post-prompt nil)
169 (defvar gdb-version nil)
170 (defvar gdb-locals-font-lock-keywords nil)
171 (defvar gdb-source-file-list nil
172 "List of source files for the current executable")
173 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
175 (defvar gdb-locals-font-lock-keywords-1
177 ;; var = (struct struct_tag) value
178 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
179 (1 font-lock-variable-name-face)
180 (3 font-lock-keyword-face)
181 (4 font-lock-type-face))
182 ;; var = (type) value
183 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
184 (1 font-lock-variable-name-face)
185 (3 font-lock-type-face))
187 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
188 (1 font-lock-variable-name-face))
190 "Font lock keywords used in `gdb-local-mode'.")
192 (defvar gdb-locals-font-lock-keywords-2
195 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
196 (1 font-lock-variable-name-face)
197 (3 font-lock-type-face))
199 "Font lock keywords used in `gdb-local-mode'.")
201 ;; Variables for GDB 6.4+
202 (defvar gdb-register-names nil "List of register names.")
203 (defvar gdb-changed-registers nil
204 "List of changed register numbers (strings).")
207 (defun gdba (command-line)
208 "Run gdb on program FILE in buffer *gud-FILE*.
209 The directory containing FILE becomes the initial working directory
210 and source-file directory for your debugger.
212 If `gdb-many-windows' is nil (the default value) then gdb just
213 pops up the GUD buffer unless `gdb-show-main' is t. In this case
214 it starts with two windows: one displaying the GUD buffer and the
215 other with the source file with the main routine of the inferior.
217 If `gdb-many-windows' is t, regardless of the value of
218 `gdb-show-main', the layout below will appear unless
219 `gdb-use-separate-io-buffer' is nil when the source buffer
220 occupies the full width of the frame. Keybindings are shown in
223 Watch expressions appear in the speedbar/slowbar.
225 The following commands help control operation :
227 `gdb-many-windows' - Toggle the number of windows gdb uses.
228 `gdb-restore-windows' - To restore the window layout.
230 See Info node `(emacs)GDB Graphical Interface' for a more
231 detailed description of this mode.
234 +----------------------------------------------------------------------+
236 +-----------------------------------+----------------------------------+
237 | GUD buffer (I/O of GDB) | Locals buffer |
241 +-----------------------------------+----------------------------------+
242 | Source buffer | I/O buffer (of debugged program) |
250 +-----------------------------------+----------------------------------+
251 | Stack buffer | Breakpoints buffer |
252 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
253 | | RET gdb-goto-breakpoint |
254 | | D gdb-delete-breakpoint |
255 +-----------------------------------+----------------------------------+"
257 (interactive (list (gud-query-cmdline 'gdba)))
259 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
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-use-separate-io-buffer nil
306 "Non-nil means display output from the inferior in a separate buffer."
311 (defun gdb-use-separate-io-buffer (arg)
312 "Toggle separate IO for inferior.
313 With arg, use separate IO iff arg is positive."
315 (setq gdb-use-separate-io-buffer
317 (not gdb-use-separate-io-buffer)
318 (> (prefix-numeric-value arg) 0)))
319 (message (format "Separate inferior IO %sabled"
320 (if gdb-use-separate-io-buffer "en" "dis")))
321 (if (and gud-comint-buffer
322 (buffer-name gud-comint-buffer))
324 (if gdb-use-separate-io-buffer
325 (gdb-restore-windows)
326 (kill-buffer (gdb-inferior-io-name)))
329 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
331 (defun gdb-create-define-alist ()
332 "Create an alist of #define directives for GUD tooltips."
333 (let* ((file (buffer-file-name))
335 (with-output-to-string
336 (with-current-buffer standard-output
337 (call-process shell-file-name
338 (if (file-exists-p file) file nil)
339 (list t nil) nil "-c"
340 (concat gdb-cpp-define-alist-program " "
341 gdb-cpp-define-alist-flags)))))
342 (define-list (split-string output "\n" t))
344 (setq gdb-define-alist nil)
345 (dolist (define define-list)
346 (setq name (nth 1 (split-string define "[( ]")))
347 (push (cons name define) gdb-define-alist))))
349 (defun gdb-tooltip-print (expr)
351 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
352 (goto-char (point-min))
354 (if (search-forward "=" nil t)
355 (concat expr (buffer-substring (- (point) 2) (point-max)))
357 ;; remove newline for gud-tooltip-echo-area
358 (substring string 0 (- (length string) 1))))
359 (or gud-tooltip-echo-area tooltip-use-echo-area)))
361 ;; If expr is a macro for a function don't print because of possible dangerous
362 ;; side-effects. Also printing a function within a tooltip generates an
363 ;; unexpected starting annotation (phase error).
364 (defun gdb-tooltip-print-1 (expr)
365 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
366 (goto-char (point-min))
367 (if (search-forward "expands to: " nil t)
368 (unless (looking-at "\\S-+.*(.*).*")
370 (list (concat gdb-server-prefix "print " expr "\n")
371 `(lambda () (gdb-tooltip-print ,expr))))))))
373 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
375 (defun gdb-set-gud-minor-mode-existing-buffers ()
376 "Create list of source files for current GDB session."
377 (goto-char (point-min))
378 (when (search-forward "read in on demand:" nil t)
379 (while (re-search-forward gdb-source-file-regexp nil t)
380 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
381 (dolist (buffer (buffer-list))
382 (with-current-buffer buffer
383 (when (and buffer-file-name
384 (member (file-name-nondirectory buffer-file-name)
385 gdb-source-file-list))
386 (set (make-local-variable 'gud-minor-mode) 'gdba)
387 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
388 (when gud-tooltip-mode
389 (make-local-variable 'gdb-define-alist)
390 (gdb-create-define-alist)
391 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
393 (defun gdb-find-watch-expression ()
394 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
395 (varno (nth 1 var)) (expr))
396 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varno)
397 (dolist (var1 gdb-var-list)
398 (if (string-equal (nth 1 var1) (match-string 1 varno))
399 (setq expr (concat (car var1) "." (match-string 2 varno)))))
403 (set (make-local-variable 'gud-minor-mode) 'gdba)
404 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
406 (gud-def gud-break (if (not (string-match "Machine" mode-name))
407 (gud-call "break %f:%l" arg)
411 (gud-call "break *%a" arg)))
412 "\C-b" "Set breakpoint at current line or address.")
414 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
415 (gud-call "clear %f:%l" arg)
419 (gud-call "clear *%a" arg)))
420 "\C-d" "Remove breakpoint at current line or address.")
422 (gud-def gud-until (if (not (string-match "Machine" mode-name))
423 (gud-call "until %f:%l" arg)
427 (gud-call "until *%a" arg)))
428 "\C-u" "Continue to current line or address.")
430 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
431 nil "Start or continue execution.")
433 ;; For debugging Emacs only.
437 "pp1 " (if (eq (buffer-local-value
438 'major-mode (window-buffer)) 'speedbar-mode)
439 (gdb-find-watch-expression) "%e")) arg)
440 nil "Print the emacs s-expression.")
442 (define-key gud-minor-mode-map [left-margin mouse-1]
443 'gdb-mouse-set-clear-breakpoint)
444 (define-key gud-minor-mode-map [left-fringe mouse-1]
445 'gdb-mouse-set-clear-breakpoint)
446 (define-key gud-minor-mode-map [left-fringe mouse-2]
448 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
450 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
452 (define-key gud-minor-mode-map [left-margin mouse-2]
454 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
456 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
458 (define-key gud-minor-mode-map [left-fringe C-mouse-2]
460 (define-key gud-minor-mode-map [left-margin C-mouse-2]
462 (define-key gud-minor-mode-map [left-margin mouse-3]
463 'gdb-mouse-toggle-breakpoint-margin)
464 (define-key gud-minor-mode-map [left-fringe mouse-3]
465 'gdb-mouse-toggle-breakpoint-fringe)
467 (setq comint-input-sender 'gdb-send)
470 (setq gdb-frame-address (if gdb-show-main "main" nil))
471 (setq gdb-previous-frame-address nil
472 gdb-memory-address "main"
473 gdb-previous-frame nil
474 gdb-selected-frame nil
475 gdb-current-language nil
479 gdb-first-post-prompt t
483 gdb-pending-triggers nil
484 gdb-output-sink 'user
485 gdb-server-prefix "server "
486 gdb-flush-pending-output nil
487 gdb-location-alist nil
488 gdb-source-file-list nil
491 gdb-buffer-fringe-width (car (window-fringes))
494 gdb-source-window nil)
496 (setq gdb-buffer-type 'gdba)
498 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
500 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
501 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
505 (if (eq window-system 'w32)
506 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
507 (gdb-enqueue-input (list "set height 0\n" 'ignore))
508 (gdb-enqueue-input (list "set width 0\n" 'ignore))
510 (if (string-equal gdb-version "pre-6.4")
512 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
513 'gdb-set-gud-minor-mode-existing-buffers))
514 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
516 (list "server interpreter mi -data-list-register-names\n"
517 'gdb-get-register-names))
518 ; Needs GDB 6.2 onwards.
520 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
521 'gdb-set-gud-minor-mode-existing-buffers-1))
522 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
524 ;; find source file and compilation directory here
525 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
526 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
527 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
529 (run-hooks 'gdba-mode-hook))
531 (defun gdb-get-version ()
532 (goto-char (point-min))
533 (if (and (re-search-forward gdb-error-regexp nil t)
534 (string-match ".*(missing implementation)" (match-string 1)))
535 (setq gdb-version "pre-6.4")
536 (setq gdb-version "6.4+"))
539 (defun gdb-mouse-until (event)
540 "Continue running until a source line past the current line.
541 The destination source line can be selected either by clicking with mouse-2
542 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
544 (if gud-overlay-arrow-position
545 (let ((start (event-start event))
546 (end (event-end event))
547 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
548 (if (not (string-match "Machine" mode-name))
549 (if (equal buffer (window-buffer (posn-window end)))
550 (with-current-buffer buffer
551 (when (or (equal start end)
552 (equal (posn-point start)
554 gud-overlay-arrow-position)))
555 (setq line (line-number-at-pos (posn-point end)))
556 (gud-call (concat "until " (number-to-string line))))))
557 (if (equal (marker-buffer gdb-overlay-arrow-position)
558 (window-buffer (posn-window end)))
559 (when (or (equal start end)
560 (equal (posn-point start)
562 gdb-overlay-arrow-position)))
564 (goto-line (line-number-at-pos (posn-point end)))
566 (gud-call (concat "until *%a")))))))))
568 (defun gdb-mouse-jump (event)
569 "Set execution address/line.
570 The destination source line can be selected either by clicking with mouse-2
571 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
572 Unlike gdb-mouse-until the destination address can be before the current
573 line, and no execution takes place."
575 (if gud-overlay-arrow-position
576 (let ((start (event-start event))
577 (end (event-end event))
578 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
579 (if (not (string-match "Machine" mode-name))
580 (if (equal buffer (window-buffer (posn-window end)))
581 (with-current-buffer buffer
582 (when (or (equal start end)
583 (equal (posn-point start)
585 gud-overlay-arrow-position)))
586 (setq line (line-number-at-pos (posn-point end)))
587 (progn (gud-call (concat "tbreak " (number-to-string line)))
588 (gud-call (concat "jump " (number-to-string line)))))))
589 (if (equal (marker-buffer gdb-overlay-arrow-position)
590 (window-buffer (posn-window end)))
591 (when (or (equal start end)
592 (equal (posn-point start)
594 gdb-overlay-arrow-position)))
596 (goto-line (line-number-at-pos (posn-point end)))
599 (gud-call (concat "tbreak *%a"))
600 (gud-call (concat "jump *%a"))))))))))
602 (defcustom gdb-speedbar-auto-raise nil
603 "If non-nil raise speedbar every time display of watch expressions is\
609 (defun gdb-speedbar-auto-raise (arg)
610 "Toggle automatic raising of the speedbar for watch expressions.
611 With arg, automatically raise speedbar iff arg is positive."
613 (setq gdb-speedbar-auto-raise
615 (not gdb-speedbar-auto-raise)
616 (> (prefix-numeric-value arg) 0)))
617 (message (format "Auto raising %sabled"
618 (if gdb-speedbar-auto-raise "en" "dis"))))
620 (defcustom gdb-use-colon-colon-notation nil
621 "If non-nil use FUN::VAR format to display variables in the speedbar."
626 (defun gud-watch (&optional event)
627 "Watch expression at point."
628 (interactive (list last-input-event))
629 (if event (posn-set-point (event-end event)))
631 (save-selected-window
632 (let ((expr (tooltip-identifier-from-point (point))))
633 (catch 'already-watched
634 (dolist (var gdb-var-list)
635 (if (string-equal expr (car var)) (throw 'already-watched nil)))
636 (set-text-properties 0 (length expr) nil expr)
639 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
640 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
641 (concat"-var-create - * " expr "\n"))
642 `(lambda () (gdb-var-create-handler ,expr))))))))
644 (defconst gdb-var-create-regexp
645 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
647 (defun gdb-var-create-handler (expr)
648 (goto-char (point-min))
649 (if (re-search-forward gdb-var-create-regexp nil t)
651 (if (and (string-equal gdb-current-language "c")
652 gdb-use-colon-colon-notation gdb-selected-frame)
653 (setq expr (concat gdb-selected-frame "::" expr))
659 (push var gdb-var-list)
661 (unless (string-equal
662 speedbar-initial-expansion-list-name "GUD")
663 (speedbar-change-initial-expansion-list "GUD"))
666 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
667 (concat "server interpreter mi \"-var-evaluate-expression "
669 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
670 `(lambda () (gdb-var-evaluate-expression-handler
671 ,(nth 1 var) nil)))))
672 (if (search-forward "Undefined command" nil t)
673 (message-box "Watching expressions requires gdb 6.0 onwards")
674 (message-box "No symbol \"%s\" in current context." expr))))
676 (defun gdb-var-evaluate-expression-handler (varnum changed)
677 (goto-char (point-min))
678 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
680 (dolist (var gdb-var-list)
681 (when (string-equal varnum (cadr var))
682 (if changed (setcar (nthcdr 5 var) 'changed))
683 (setcar (nthcdr 4 var) (read (match-string 1)))
684 (throw 'var-found nil)))))
686 (defun gdb-var-list-children (varnum)
688 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
689 `(lambda () (gdb-var-list-children-handler ,varnum)))))
691 (defconst gdb-var-list-children-regexp
692 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
695 (defun gdb-var-list-children-handler (varnum)
696 (goto-char (point-min))
697 (let ((var-list nil))
698 (catch 'child-already-watched
699 (dolist (var gdb-var-list)
700 (if (string-equal varnum (cadr var))
703 (while (re-search-forward gdb-var-list-children-regexp nil t)
704 (let ((varchild (list (match-string 2)
709 (dolist (var1 gdb-var-list)
710 (if (string-equal (cadr var1) (cadr varchild))
711 (throw 'child-already-watched nil)))
712 (push varchild var-list)
716 "server interpreter mi \"-var-evaluate-expression "
717 (nth 1 varchild) "\"\n")
718 `(lambda () (gdb-var-evaluate-expression-handler
719 ,(nth 1 varchild) nil)))))))
720 (push var var-list)))
721 (setq gdb-var-list (nreverse var-list)))))
723 (defun gdb-var-update ()
724 (when (not (member 'gdb-var-update gdb-pending-triggers))
726 (list "server interpreter mi \"-var-update *\"\n"
727 'gdb-var-update-handler))
728 (push 'gdb-var-update gdb-pending-triggers)))
730 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\",in_scope=\"\\(.*?\\)\"")
732 (defun gdb-var-update-handler ()
733 (dolist (var gdb-var-list)
734 (setcar (nthcdr 5 var) nil))
735 (goto-char (point-min))
736 (while (re-search-forward gdb-var-update-regexp nil t)
737 (let ((varnum (match-string 1)))
738 (if (string-equal (match-string 2) "false")
740 (dolist (var gdb-var-list)
741 (when (string-equal varnum (cadr var))
742 (setcar (nthcdr 5 var) 'out-of-scope)
743 (throw 'var-found nil))))
746 (concat "server interpreter mi \"-var-evaluate-expression "
748 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
749 (setq gdb-pending-triggers
750 (delq 'gdb-var-update gdb-pending-triggers))
751 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
752 ;; Dummy command to update speedbar at right time.
753 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
754 ;; Keep gdb-pending-triggers non-nil till end.
755 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
757 (defun gdb-speedbar-refresh ()
758 (setq gdb-pending-triggers
759 (delq 'gdb-speedbar-refresh gdb-pending-triggers))
760 (with-current-buffer gud-comint-buffer
761 (let ((speedbar-verbosity-level 0))
763 (speedbar-refresh)))))
765 (defun gdb-var-delete ()
766 "Delete watch expression at point from the speedbar."
768 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
770 (let ((text (speedbar-line-text)))
771 (string-match "\\(\\S-+\\)" text)
772 (let* ((expr (match-string 1 text))
773 (var (assoc expr gdb-var-list))
775 (unless (string-match "\\." varnum)
778 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
780 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
781 (concat "-var-delete " varnum "\n"))
783 (setq gdb-var-list (delq var gdb-var-list))
784 (dolist (varchild gdb-var-list)
785 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
786 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
788 (defun gdb-edit-value (text token indent)
789 "Assign a value to a variable displayed in the speedbar."
790 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
791 (varnum (cadr var)) (value))
792 (setq value (read-string "New value: "))
795 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
796 (concat "server interpreter mi \"-var-assign "
797 varnum " " value "\"\n")
798 (concat "-var-assign " varnum " " value "\n"))
799 `(lambda () (gdb-edit-value-handler ,value))))))
801 (defun gdb-edit-value-handler (value)
802 (goto-char (point-min))
803 (if (re-search-forward gdb-error-regexp nil t)
804 (message-box "Invalid number or expression (%s)" value)))
806 (defcustom gdb-show-changed-values t
807 "If non-nil change the face of out of scope variables and changed values.
808 Out of scope variables are suppressed with `shadow' face.
809 Changed values are highlighted with the face `font-lock-warning-face'."
814 (defun gdb-speedbar-expand-node (text token indent)
815 "Expand the node the user clicked on.
816 TEXT is the text of the button we clicked on, a + or - item.
817 TOKEN is data related to this node.
818 INDENT is the current indentation depth."
819 (cond ((string-match "+" text) ;expand this node
821 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
822 (string-equal gdb-version "pre-6.4"))
823 (gdb-var-list-children token)
824 (gdb-var-list-children-1 token)))
825 ((string-match "-" text) ;contract this node
826 (dolist (var gdb-var-list)
827 (if (string-match (concat token "\\.") (nth 1 var))
828 (setq gdb-var-list (delq var gdb-var-list))))
829 (speedbar-change-expand-button-char ?+)
830 (speedbar-delete-subblock indent))
831 (t (error "Ooops... not sure what to do")))
832 (speedbar-center-buffer-smartly))
834 (defun gdb-get-target-string ()
835 (with-current-buffer gud-comint-buffer
842 ;; Each buffer has a TYPE -- a symbol that identifies the function
843 ;; of that particular buffer.
845 ;; The usual gdb interaction buffer is given the type `gdba' and
846 ;; is constructed specially.
848 ;; Others are constructed by gdb-get-buffer-create and
849 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
851 (defvar gdb-buffer-rules-assoc '())
853 (defun gdb-get-buffer (key)
854 "Return the gdb buffer tagged with type KEY.
855 The key should be one of the cars in `gdb-buffer-rules-assoc'."
857 (gdb-look-for-tagged-buffer key (buffer-list))))
859 (defun gdb-get-buffer-create (key)
860 "Create a new gdb buffer of the type specified by KEY.
861 The key should be one of the cars in `gdb-buffer-rules-assoc'."
862 (or (gdb-get-buffer key)
863 (let* ((rules (assoc key gdb-buffer-rules-assoc))
864 (name (funcall (gdb-rules-name-maker rules)))
865 (new (get-buffer-create name)))
866 (with-current-buffer new
868 (if (cdr (cdr rules))
869 (setq trigger (funcall (car (cdr (cdr rules))))))
870 (setq gdb-buffer-type key)
871 (set (make-local-variable 'gud-minor-mode)
872 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
873 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
874 (if trigger (funcall trigger)))
877 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
879 (defun gdb-look-for-tagged-buffer (key bufs)
881 (while (and (not retval) bufs)
882 (set-buffer (car bufs))
883 (if (eq gdb-buffer-type key)
884 (setq retval (car bufs)))
885 (setq bufs (cdr bufs)))
889 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
890 ;; at least one and possible more functions. The functions have these
891 ;; roles in defining a buffer type:
893 ;; NAME - Return a name for this buffer type.
895 ;; The remaining function(s) are optional:
897 ;; MODE - called in a new buffer with no arguments, should establish
898 ;; the proper mode for the buffer.
901 (defun gdb-set-buffer-rules (buffer-type &rest rules)
902 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
904 (setcdr binding rules)
905 (push (cons buffer-type rules)
906 gdb-buffer-rules-assoc))))
908 ;; GUD buffers are an exception to the rules
909 (gdb-set-buffer-rules 'gdba 'error)
911 ;; Partial-output buffer : This accumulates output from a command executed on
912 ;; behalf of emacs (rather than the user).
914 (gdb-set-buffer-rules 'gdb-partial-output-buffer
915 'gdb-partial-output-name)
917 (defun gdb-partial-output-name ()
918 (concat "*partial-output-"
919 (gdb-get-target-string)
923 (gdb-set-buffer-rules 'gdb-inferior-io
924 'gdb-inferior-io-name
925 'gdb-inferior-io-mode)
927 (defun gdb-inferior-io-name ()
928 (concat "*input/output of "
929 (gdb-get-target-string)
932 (defun gdb-display-separate-io-buffer ()
933 "Display IO of debugged program in a separate window."
935 (if gdb-use-separate-io-buffer
937 (gdb-get-buffer-create 'gdb-inferior-io))))
939 (defconst gdb-frame-parameters
940 '((height . 14) (width . 80)
942 (tool-bar-lines . nil)
943 (menu-bar-lines . nil)
946 (defun gdb-frame-separate-io-buffer ()
947 "Display IO of inferior in a new frame."
949 (if gdb-use-separate-io-buffer
950 (let ((special-display-regexps (append special-display-regexps '(".*")))
951 (special-display-frame-alist gdb-frame-parameters))
952 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
954 (defvar gdb-inferior-io-mode-map
955 (let ((map (make-sparse-keymap)))
956 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
957 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
958 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
959 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
960 (define-key map "\C-d" 'gdb-separate-io-eof)
963 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
964 "Major mode for gdb inferior-io."
965 :syntax-table nil :abbrev-table nil
966 ;; We want to use comint because it has various nifty and familiar
967 ;; features. We don't need a process, but comint wants one, so create
969 (make-comint-in-buffer
970 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
971 (current-buffer) "hexl")
972 (setq comint-input-sender 'gdb-inferior-io-sender))
974 (defun gdb-inferior-io-sender (proc string)
975 ;; PROC is the pseudo-process created to satisfy comint.
976 (with-current-buffer (process-buffer proc)
977 (setq proc (get-buffer-process gud-comint-buffer))
978 (process-send-string proc string)
979 (process-send-string proc "\n")))
981 (defun gdb-separate-io-interrupt ()
982 "Interrupt the program being debugged."
985 (get-buffer-process gud-comint-buffer) comint-ptyp))
987 (defun gdb-separate-io-quit ()
988 "Send quit signal to the program being debugged."
991 (get-buffer-process gud-comint-buffer) comint-ptyp))
993 (defun gdb-separate-io-stop ()
994 "Stop the program being debugged."
997 (get-buffer-process gud-comint-buffer) comint-ptyp))
999 (defun gdb-separate-io-eof ()
1000 "Send end-of-file to the program being debugged."
1003 (get-buffer-process gud-comint-buffer)))
1006 ;; gdb communications
1009 ;; INPUT: things sent to gdb
1011 ;; The queues are lists. Each element is either a string (indicating user or
1012 ;; user-like input) or a list of the form:
1014 ;; (INPUT-STRING HANDLER-FN)
1016 ;; The handler function will be called from the partial-output buffer when the
1017 ;; command completes. This is the way to write commands which invoke gdb
1018 ;; commands autonomously.
1020 ;; These lists are consumed tail first.
1023 (defun gdb-send (proc string)
1024 "A comint send filter for gdb.
1025 This filter may simply queue input for a later time."
1026 (with-current-buffer gud-comint-buffer
1027 (let ((inhibit-read-only t))
1028 (remove-text-properties (point-min) (point-max) '(face))))
1029 (let ((item (concat string "\n")))
1032 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1033 (process-send-string proc item))
1034 (gdb-enqueue-input item))))
1036 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1037 ;; is a query, or other non-top-level prompt.
1039 (defun gdb-enqueue-input (item)
1042 (gdb-send-item item)
1043 (setq gdb-prompting nil))
1044 (push item gdb-input-queue)))
1046 (defun gdb-dequeue-input ()
1047 (let ((queue gdb-input-queue))
1049 (let ((last (car (last queue))))
1050 (unless (nbutlast queue) (setq gdb-input-queue '()))
1053 (defun gdb-send-item (item)
1054 (setq gdb-flush-pending-output nil)
1055 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1056 (setq gdb-current-item item)
1057 (let ((process (get-buffer-process gud-comint-buffer)))
1058 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1061 (setq gdb-output-sink 'user)
1062 (process-send-string process item))
1064 (gdb-clear-partial-output)
1065 (setq gdb-output-sink 'pre-emacs)
1066 (process-send-string process
1068 ;; case: eq gud-minor-mode 'gdbmi
1069 (gdb-clear-partial-output)
1070 (setq gdb-output-sink 'emacs)
1071 (process-send-string process (car item)))))
1074 ;; output -- things gdb prints to emacs
1076 ;; GDB output is a stream interrupted by annotations.
1077 ;; Annotations can be recognized by their beginning
1078 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1080 ;; The tag is a string obeying symbol syntax.
1082 ;; The optional part `<opt>' can be either the empty string
1083 ;; or a space followed by more data relating to the annotation.
1084 ;; For example, the SOURCE annotation is followed by a filename,
1085 ;; line number and various useless goo. This data must not include
1089 (defcustom gud-gdba-command-name "gdb -annotate=3"
1090 "Default command to execute an executable under the GDB-UI debugger."
1095 (defvar gdb-annotation-rules
1096 '(("pre-prompt" gdb-pre-prompt)
1097 ("prompt" gdb-prompt)
1098 ("commands" gdb-subprompt)
1099 ("overload-choice" gdb-subprompt)
1100 ("query" gdb-subprompt)
1101 ;; Need this prompt for GDB 6.1
1102 ("nquery" gdb-subprompt)
1103 ("prompt-for-continue" gdb-subprompt)
1104 ("post-prompt" gdb-post-prompt)
1105 ("source" gdb-source)
1106 ("starting" gdb-starting)
1107 ("exited" gdb-exited)
1108 ("signalled" gdb-signalled)
1109 ("signal" gdb-stopping)
1110 ("breakpoint" gdb-stopping)
1111 ("watchpoint" gdb-stopping)
1112 ("frame-begin" gdb-frame-begin)
1113 ("stopped" gdb-stopped)
1114 ("error-begin" gdb-error)
1116 ) "An assoc mapping annotation tags to functions which process them.")
1119 (setq gdb-flush-pending-output t)
1120 (setq gud-running nil)
1121 (setq gdb-output-sink 'user)
1122 (setq gdb-input-queue nil)
1123 (setq gdb-pending-triggers nil)
1124 (setq gdb-prompting t))
1126 (defconst gdb-source-spec-regexp
1127 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1129 ;; Do not use this except as an annotation handler.
1130 (defun gdb-source (args)
1131 (string-match gdb-source-spec-regexp args)
1132 ;; Extract the frame position from the marker.
1133 (setq gud-last-frame
1135 (match-string 1 args)
1136 (string-to-number (match-string 2 args))))
1137 (setq gdb-frame-address (match-string 3 args))
1138 ;; cover for auto-display output which comes *before*
1139 ;; stopped annotation
1140 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1142 (defun gdb-pre-prompt (ignored)
1143 "An annotation handler for `pre-prompt'.
1144 This terminates the collection of output from a previous command if that
1145 happens to be in effect."
1146 (setq gdb-error nil)
1147 (let ((sink gdb-output-sink))
1151 (setq gdb-output-sink 'post-emacs))
1154 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1156 (defun gdb-prompt (ignored)
1157 "An annotation handler for `prompt'.
1158 This sends the next command (if any) to gdb."
1159 (when gdb-first-prompt
1161 (setq gdb-first-prompt nil))
1162 (let ((sink gdb-output-sink))
1165 ((eq sink 'post-emacs)
1166 (setq gdb-output-sink 'user)
1168 (car (cdr gdb-current-item))))
1169 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1170 (funcall handler))))
1173 (error "Phase error in gdb-prompt (got %s)" sink))))
1174 (let ((input (gdb-dequeue-input)))
1176 (gdb-send-item input)
1178 (setq gdb-prompting t)
1179 (gud-display-frame)))))
1181 (defun gdb-subprompt (ignored)
1182 "An annotation handler for non-top-level prompts."
1183 (setq gdb-prompting t))
1185 (defun gdb-starting (ignored)
1186 "An annotation handler for `starting'.
1187 This says that I/O for the subprocess is now the program being debugged,
1189 (setq gdb-active-process t)
1190 (let ((sink gdb-output-sink))
1194 (setq gud-running t)
1195 (if gdb-use-separate-io-buffer
1196 (setq gdb-output-sink 'inferior))))
1199 (error "Unexpected `starting' annotation")))))
1201 (defun gdb-stopping (ignored)
1202 "An annotation handler for `breakpoint' and other annotations.
1203 They say that I/O for the subprocess is now GDB, not the program
1205 (if gdb-use-separate-io-buffer
1206 (let ((sink gdb-output-sink))
1208 ((eq sink 'inferior)
1209 (setq gdb-output-sink 'user))
1212 (error "Unexpected stopping annotation"))))))
1214 (defun gdb-exited (ignored)
1215 "An annotation handler for `exited' and `signalled'.
1216 They say that I/O for the subprocess is now GDB, not the program
1217 being debugged and that the program is no longer running. This
1218 function is used to change the focus of GUD tooltips to #define
1220 (setq gdb-active-process nil)
1221 (setq gud-overlay-arrow-position nil)
1222 (setq gdb-overlay-arrow-position nil)
1223 (gdb-stopping ignored))
1225 (defun gdb-signalled (ignored)
1226 (setq gdb-signalled t))
1228 (defun gdb-frame-begin (ignored)
1229 (let ((sink gdb-output-sink))
1231 ((eq sink 'inferior)
1232 (setq gdb-output-sink 'user))
1234 ((eq sink 'emacs) t)
1237 (error "Unexpected frame-begin annotation (%S)" sink)))))
1239 (defun gdb-stopped (ignored)
1240 "An annotation handler for `stopped'.
1241 It is just like `gdb-stopping', except that if we already set the output
1242 sink to `user' in `gdb-stopping', that is fine."
1243 (setq gud-running nil)
1244 (let ((sink gdb-output-sink))
1246 ((eq sink 'inferior)
1247 (setq gdb-output-sink 'user))
1251 (error "Unexpected stopped annotation"))))
1252 (if gdb-signalled (gdb-exited ignored)))
1254 (defun gdb-error (ignored)
1255 (setq gdb-error (not gdb-error)))
1257 (defun gdb-post-prompt (ignored)
1258 "An annotation handler for `post-prompt'.
1259 This begins the collection of output from the current command if that
1260 happens to be appropriate."
1261 ;; Don't add to queue if there outstanding items or gdb-version is not known
1263 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1264 (gdb-get-selected-frame)
1265 (gdb-invalidate-frames)
1266 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1267 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1268 (gdb-invalidate-breakpoints)
1269 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1270 ;; so gdb-frame-address is updated.
1271 ;; (gdb-invalidate-assembler)
1273 (if (string-equal gdb-version "pre-6.4")
1274 (gdb-invalidate-registers)
1275 (gdb-get-changed-registers)
1276 (gdb-invalidate-registers-1))
1278 (gdb-invalidate-memory)
1279 (if (string-equal gdb-version "pre-6.4")
1280 (gdb-invalidate-locals)
1281 (gdb-invalidate-locals-1))
1283 (gdb-invalidate-threads)
1284 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1285 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1286 ;; Only needed/used with speedbar/watch expressions.
1287 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1288 (setq gdb-force-update t)
1289 (if (string-equal gdb-version "pre-6.4")
1291 (gdb-var-update-1)))))
1292 (setq gdb-first-post-prompt nil)
1293 (let ((sink gdb-output-sink))
1296 ((eq sink 'pre-emacs)
1297 (setq gdb-output-sink 'emacs))
1300 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1302 ;; GUD displays the selected GDB frame. This might might not be the current
1303 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1304 ;; visited breakpoint is, use that window.
1305 (defun gdb-display-source-buffer (buffer)
1306 (let* ((last-window (if gud-last-last-frame
1308 (gud-find-file (car gud-last-last-frame)))))
1309 (source-window (or last-window
1310 (if (and gdb-source-window
1311 (window-live-p gdb-source-window))
1312 gdb-source-window))))
1314 (setq gdb-source-window source-window)
1315 (set-window-buffer source-window buffer))
1318 (defun gud-gdba-marker-filter (string)
1319 "A gud marker filter for gdb. Handle a burst of output from GDB."
1320 (if gdb-flush-pending-output
1322 (when gdb-enable-debug
1323 (push (cons 'recv string) gdb-debug-ring)
1324 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1325 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1326 ;; Recall the left over gud-marker-acc from last time.
1327 (setq gud-marker-acc (concat gud-marker-acc string))
1328 ;; Start accumulating output for the GUD buffer.
1331 ;; Process all the complete markers in this chunk.
1332 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1333 (let ((annotation (match-string 1 gud-marker-acc)))
1335 ;; Stuff prior to the match is just ordinary output.
1336 ;; It is either concatenated to OUTPUT or directed
1341 (substring gud-marker-acc 0 (match-beginning 0))))
1343 ;; Take that stuff off the gud-marker-acc.
1344 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1346 ;; Parse the tag from the annotation, and maybe its arguments.
1347 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1348 (let* ((annotation-type (match-string 1 annotation))
1349 (annotation-arguments (match-string 2 annotation))
1350 (annotation-rule (assoc annotation-type
1351 gdb-annotation-rules)))
1352 ;; Call the handler for this annotation.
1354 (funcall (car (cdr annotation-rule))
1355 annotation-arguments)
1356 ;; Else the annotation is not recognized. Ignore it silently,
1357 ;; so that GDB can add new annotations without causing
1361 ;; Does the remaining text end in a partial line?
1362 ;; If it does, then keep part of the gud-marker-acc until we get more.
1363 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1366 ;; Everything before the potential marker start can be output.
1368 (gdb-concat-output output
1369 (substring gud-marker-acc 0
1370 (match-beginning 0))))
1372 ;; Everything after, we save, to combine with later input.
1373 (setq gud-marker-acc (substring gud-marker-acc
1374 (match-beginning 0))))
1376 ;; In case we know the gud-marker-acc contains no partial annotations:
1378 (setq output (gdb-concat-output output gud-marker-acc))
1379 (setq gud-marker-acc "")))
1382 (defun gdb-concat-output (so-far new)
1384 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1385 (let ((sink gdb-output-sink))
1387 ((eq sink 'user) (concat so-far new))
1388 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1390 (gdb-append-to-partial-output new)
1392 ((eq sink 'inferior)
1393 (gdb-append-to-inferior-io new)
1397 (error "Bogon output sink %S" sink)))))
1399 (defun gdb-append-to-partial-output (string)
1400 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1401 (goto-char (point-max))
1404 (defun gdb-clear-partial-output ()
1405 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1408 (defun gdb-append-to-inferior-io (string)
1409 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1410 (goto-char (point-max))
1411 (insert-before-markers string))
1412 (if (not (string-equal string ""))
1413 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1415 (defun gdb-clear-inferior-io ()
1416 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1420 ;; One trick is to have a command who's output is always available in a buffer
1421 ;; of it's own, and is always up to date. We build several buffers of this
1424 ;; There are two aspects to this: gdb has to tell us when the output for that
1425 ;; command might have changed, and we have to be able to run the command
1426 ;; behind the user's back.
1428 ;; The output phasing associated with the variable gdb-output-sink
1429 ;; help us to run commands behind the user's back.
1431 ;; Below is the code for specificly managing buffers of output from one
1435 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1436 ;; It adds an input for the command we are tracking. It should be the
1437 ;; annotation rule binding of whatever gdb sends to tell us this command
1438 ;; might have changed it's output.
1440 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1441 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1442 ;; input in the input queue (see comment about ``gdb communications'' above).
1444 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1446 `(defun ,name (&optional ignored)
1447 (if (and ,demand-predicate
1449 gdb-pending-triggers)))
1452 (list ,gdb-command ',output-handler))
1453 (push ',name gdb-pending-triggers)))))
1455 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1457 (setq gdb-pending-triggers
1459 gdb-pending-triggers))
1460 (let ((buf (gdb-get-buffer ',buf-key)))
1462 (with-current-buffer buf
1463 (let* ((window (get-buffer-window buf 0))
1464 (start (window-start window))
1465 (p (window-point window))
1466 (buffer-read-only nil))
1468 (insert-buffer-substring (gdb-get-buffer-create
1469 'gdb-partial-output-buffer))
1470 (set-window-start window start)
1471 (set-window-point window p)))))
1472 ;; put customisation here
1475 (defmacro def-gdb-auto-updated-buffer (buffer-key
1476 trigger-name gdb-command
1477 output-handler-name custom-defun)
1479 (def-gdb-auto-update-trigger ,trigger-name
1480 ;; The demand predicate:
1481 (gdb-get-buffer ',buffer-key)
1483 ,output-handler-name)
1484 (def-gdb-auto-update-handler ,output-handler-name
1485 ,trigger-name ,buffer-key ,custom-defun)))
1489 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1491 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1492 'gdb-breakpoints-buffer-name
1493 'gdb-breakpoints-mode)
1495 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1496 ;; This defines the auto update rule for buffers of type
1497 ;; `gdb-breakpoints-buffer'.
1499 ;; It defines a function to serve as the annotation handler that
1500 ;; handles the `foo-invalidated' message. That function is called:
1501 gdb-invalidate-breakpoints
1503 ;; To update the buffer, this command is sent to gdb.
1504 "server info breakpoints\n"
1506 ;; This also defines a function to be the handler for the output
1507 ;; from the command above. That function will copy the output into
1508 ;; the appropriately typed buffer. That function will be called:
1509 gdb-info-breakpoints-handler
1510 ;; buffer specific functions
1511 gdb-info-breakpoints-custom)
1513 (defconst breakpoint-xpm-data
1515 static char *magick[] = {
1516 /* columns rows colors chars-per-pixel */
1532 "XPM data used for breakpoint icon.")
1534 (defconst breakpoint-enabled-pbm-data
1537 0 0 0 0 1 1 1 1 0 0 0 0
1538 0 0 0 1 1 1 1 1 1 0 0 0
1539 0 0 1 1 1 1 1 1 1 1 0 0
1540 0 1 1 1 1 1 1 1 1 1 1 0
1541 0 1 1 1 1 1 1 1 1 1 1 0
1542 0 1 1 1 1 1 1 1 1 1 1 0
1543 0 1 1 1 1 1 1 1 1 1 1 0
1544 0 0 1 1 1 1 1 1 1 1 0 0
1545 0 0 0 1 1 1 1 1 1 0 0 0
1546 0 0 0 0 1 1 1 1 0 0 0 0"
1547 "PBM data used for enabled breakpoint icon.")
1549 (defconst breakpoint-disabled-pbm-data
1561 0 0 0 1 0 1 0 1 0 0"
1562 "PBM data used for disabled breakpoint icon.")
1564 (defvar breakpoint-enabled-icon nil
1565 "Icon for enabled breakpoint in display margin.")
1567 (defvar breakpoint-disabled-icon nil
1568 "Icon for disabled breakpoint in display margin.")
1570 ;; Bitmap for breakpoint in fringe
1571 (and (display-images-p)
1572 (define-fringe-bitmap 'breakpoint
1573 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1575 (defface breakpoint-enabled
1579 "Face for enabled breakpoint icon in fringe."
1582 (defface breakpoint-disabled
1583 ;; We use different values of grey for different background types,
1584 ;; so that on low-color displays it will end up as something visible
1585 ;; if it has to be approximated.
1586 '((((background dark)) :foreground "grey60")
1587 (((background light)) :foreground "grey40"))
1588 "Face for disabled breakpoint icon in fringe."
1591 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1592 (defun gdb-info-breakpoints-custom ()
1593 (let ((flag) (bptno))
1594 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1595 (dolist (buffer (buffer-list))
1596 (with-current-buffer buffer
1597 (if (and (memq gud-minor-mode '(gdba gdbmi))
1598 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1599 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1600 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1602 (goto-char (point-min))
1603 (while (< (point) (- (point-max) 1))
1605 (if (looking-at "[^\t].*?breakpoint")
1607 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1608 (setq bptno (match-string 1))
1609 (setq flag (char-after (match-beginning 2)))
1611 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1613 (let ((buffer-read-only nil))
1614 (add-text-properties (match-beginning 1) (match-end 1)
1615 '(face font-lock-function-name-face)))
1616 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1617 (let ((line (match-string 2)) (buffer-read-only nil)
1618 (file (match-string 1)))
1619 (add-text-properties (line-beginning-position)
1621 '(mouse-face highlight
1622 help-echo "mouse-2, RET: visit breakpoint"))
1623 (unless (file-exists-p file)
1624 (setq file (cdr (assoc bptno gdb-location-alist))))
1626 (not (string-equal file "File not found")))
1627 (with-current-buffer
1628 (find-file-noselect file 'nowarn)
1629 (set (make-local-variable 'gud-minor-mode)
1631 (set (make-local-variable 'tool-bar-map)
1633 ;; Only want one breakpoint icon at each
1636 (goto-line (string-to-number line))
1637 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1640 (concat gdb-server-prefix "list "
1641 (match-string-no-properties 1) ":1\n")
1644 (list (concat gdb-server-prefix "info source\n")
1645 `(lambda () (gdb-get-location
1646 ,bptno ,line ,flag))))))))))
1648 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1650 (defun gdb-mouse-set-clear-breakpoint (event)
1651 "Set/clear breakpoint in left fringe/margin with mouse click."
1653 (mouse-minibuffer-check event)
1654 (let ((posn (event-end event)))
1655 (if (numberp (posn-point posn))
1656 (with-selected-window (posn-window posn)
1658 (goto-char (posn-point posn))
1659 (if (or (posn-object posn)
1660 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1663 (gud-break nil)))))))
1665 (defun gdb-mouse-toggle-breakpoint-margin (event)
1666 "Enable/disable breakpoint in left margin with mouse click."
1668 (mouse-minibuffer-check event)
1669 (let ((posn (event-end event)))
1670 (if (numberp (posn-point posn))
1671 (with-selected-window (posn-window posn)
1673 (goto-char (posn-point posn))
1674 (if (posn-object posn)
1677 (let ((bptno (get-text-property
1678 0 'gdb-bptno (car (posn-string posn)))))
1679 (concat gdb-server-prefix
1680 (if (get-text-property
1681 0 'gdb-enabled (car (posn-string posn)))
1687 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1688 "Enable/disable breakpoint in left fringe with mouse click."
1690 (mouse-minibuffer-check event)
1691 (let* ((posn (event-end event))
1692 (pos (posn-point posn))
1695 (with-selected-window (posn-window posn)
1697 (set-buffer (window-buffer (selected-window)))
1699 (dolist (overlay (overlays-in pos pos))
1700 (when (overlay-get overlay 'put-break)
1701 (setq obj (overlay-get overlay 'before-string))))
1705 (concat gdb-server-prefix
1706 (if (get-text-property 0 'gdb-enabled obj)
1709 (get-text-property 0 'gdb-bptno obj) "\n")
1712 (defun gdb-breakpoints-buffer-name ()
1713 (with-current-buffer gud-comint-buffer
1714 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1716 (defun gdb-display-breakpoints-buffer ()
1717 "Display status of user-settable breakpoints."
1720 (gdb-get-buffer-create 'gdb-breakpoints-buffer)))
1722 (defun gdb-frame-breakpoints-buffer ()
1723 "Display status of user-settable breakpoints in a new frame."
1725 (let ((special-display-regexps (append special-display-regexps '(".*")))
1726 (special-display-frame-alist gdb-frame-parameters))
1727 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1729 (defvar gdb-breakpoints-mode-map
1730 (let ((map (make-sparse-keymap))
1731 (menu (make-sparse-keymap "Breakpoints")))
1732 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1733 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1734 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1735 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1736 (suppress-keymap map)
1737 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1738 (define-key map " " 'gdb-toggle-breakpoint)
1739 (define-key map "D" 'gdb-delete-breakpoint)
1740 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1741 (define-key map "q" 'gdb-delete-frame-or-window)
1742 (define-key map "\r" 'gdb-goto-breakpoint)
1743 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1744 (define-key map [follow-link] 'mouse-face)
1747 (defun gdb-delete-frame-or-window ()
1748 "Delete frame if there is only one window. Otherwise delete the window."
1750 (if (one-window-p) (delete-frame)
1753 (defun gdb-breakpoints-mode ()
1754 "Major mode for gdb breakpoints.
1756 \\{gdb-breakpoints-mode-map}"
1757 (kill-all-local-variables)
1758 (setq major-mode 'gdb-breakpoints-mode)
1759 (setq mode-name "Breakpoints")
1760 (use-local-map gdb-breakpoints-mode-map)
1761 (setq buffer-read-only t)
1762 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1763 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1764 'gdb-invalidate-breakpoints
1765 'gdbmi-invalidate-breakpoints))
1767 (defconst gdb-breakpoint-regexp
1768 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1770 (defun gdb-toggle-breakpoint ()
1771 "Enable/disable breakpoint at current line."
1774 (beginning-of-line 1)
1775 (if (looking-at gdb-breakpoint-regexp)
1778 (concat gdb-server-prefix
1779 (if (eq ?y (char-after (match-beginning 2)))
1782 (match-string 1) "\n") 'ignore))
1783 (error "Not recognized as break/watchpoint line"))))
1785 (defun gdb-delete-breakpoint ()
1786 "Delete the breakpoint at current line."
1788 (beginning-of-line 1)
1789 (if (looking-at gdb-breakpoint-regexp)
1792 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1793 (error "Not recognized as break/watchpoint line")))
1795 (defun gdb-goto-breakpoint (&optional event)
1796 "Display the breakpoint location specified at current line."
1797 (interactive (list last-input-event))
1798 (if event (posn-set-point (event-end event)))
1800 (beginning-of-line 1)
1801 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1802 (let ((bptno (match-string 1))
1803 (file (match-string 2))
1804 (line (match-string 3)))
1805 (save-selected-window
1806 (let* ((buffer (find-file-noselect
1807 (if (file-exists-p file) file
1808 (cdr (assoc bptno gdb-location-alist)))))
1809 (window (unless (gdb-display-source-buffer buffer)
1810 (display-buffer buffer))))
1811 (setq gdb-source-window window)
1812 (with-current-buffer buffer
1813 (goto-line (string-to-number line))
1814 (set-window-point window (point))))))
1815 (error "No location specified."))))
1818 ;; Frames buffer. This displays a perpetually correct bactracktrace
1819 ;; (from the command `where').
1821 ;; Alas, if your stack is deep, it is costly.
1823 (gdb-set-buffer-rules 'gdb-stack-buffer
1824 'gdb-stack-buffer-name
1827 (def-gdb-auto-updated-buffer gdb-stack-buffer
1828 gdb-invalidate-frames
1830 gdb-info-frames-handler
1831 gdb-info-frames-custom)
1833 (defun gdb-info-frames-custom ()
1834 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1836 (let ((buffer-read-only nil)
1838 (goto-char (point-min))
1839 (while (< (point) (point-max))
1840 (setq bl (line-beginning-position)
1841 el (line-end-position))
1842 (when (looking-at "#")
1843 (add-text-properties bl el
1844 '(mouse-face highlight
1845 help-echo "mouse-2, RET: Select frame")))
1847 (when (looking-at "^#\\([0-9]+\\)")
1848 (when (string-equal (match-string 1) gdb-frame-number)
1849 (put-text-property bl (+ bl 4)
1850 'face '(:inverse-video t)))
1851 (when (re-search-forward
1853 (if (string-equal (match-string 1) "0") "" " in ")
1854 "\\([^ ]+\\) (") el t)
1855 (put-text-property (match-beginning 1) (match-end 1)
1856 'face font-lock-function-name-face)
1857 (setq bl (match-end 0))
1858 (while (re-search-forward "<\\([^>]+\\)>" el t)
1859 (put-text-property (match-beginning 1) (match-end 1)
1860 'face font-lock-function-name-face))
1862 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1863 (put-text-property (match-beginning 1) (match-end 1)
1864 'face font-lock-variable-name-face))))
1865 (forward-line 1))))))
1867 (defun gdb-stack-buffer-name ()
1868 (with-current-buffer gud-comint-buffer
1869 (concat "*stack frames of " (gdb-get-target-string) "*")))
1871 (defun gdb-display-stack-buffer ()
1872 "Display backtrace of current stack."
1875 (gdb-get-buffer-create 'gdb-stack-buffer)))
1877 (defun gdb-frame-stack-buffer ()
1878 "Display backtrace of current stack in a new frame."
1880 (let ((special-display-regexps (append special-display-regexps '(".*")))
1881 (special-display-frame-alist gdb-frame-parameters))
1882 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
1884 (defvar gdb-frames-mode-map
1885 (let ((map (make-sparse-keymap)))
1886 (suppress-keymap map)
1887 (define-key map "q" 'kill-this-buffer)
1888 (define-key map "\r" 'gdb-frames-select)
1889 (define-key map [mouse-2] 'gdb-frames-select)
1890 (define-key map [follow-link] 'mouse-face)
1893 (defun gdb-frames-mode ()
1894 "Major mode for gdb frames.
1896 \\{gdb-frames-mode-map}"
1897 (kill-all-local-variables)
1898 (setq major-mode 'gdb-frames-mode)
1899 (setq mode-name "Frames")
1900 (setq buffer-read-only t)
1901 (use-local-map gdb-frames-mode-map)
1902 (run-mode-hooks 'gdb-frames-mode-hook)
1903 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1904 'gdb-invalidate-frames
1905 'gdbmi-invalidate-frames))
1907 (defun gdb-get-frame-number ()
1910 (let* ((start (line-beginning-position))
1911 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
1912 (n (or (and pos (match-string-no-properties 1)) "0")))
1915 (defun gdb-frames-select (&optional event)
1916 "Select the frame and display the relevant source."
1917 (interactive (list last-input-event))
1918 (if event (posn-set-point (event-end event)))
1920 (list (concat gdb-server-prefix "frame "
1921 (gdb-get-frame-number) "\n") 'ignore))
1922 (gud-display-frame))
1925 ;; Threads buffer. This displays a selectable thread list.
1927 (gdb-set-buffer-rules 'gdb-threads-buffer
1928 'gdb-threads-buffer-name
1931 (def-gdb-auto-updated-buffer gdb-threads-buffer
1932 gdb-invalidate-threads
1933 (concat gdb-server-prefix "info threads\n")
1934 gdb-info-threads-handler
1935 gdb-info-threads-custom)
1937 (defun gdb-info-threads-custom ()
1938 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1939 (let ((buffer-read-only nil))
1940 (goto-char (point-min))
1941 (while (< (point) (point-max))
1942 (unless (looking-at "No ")
1943 (add-text-properties (line-beginning-position) (line-end-position)
1944 '(mouse-face highlight
1945 help-echo "mouse-2, RET: select thread")))
1946 (forward-line 1)))))
1948 (defun gdb-threads-buffer-name ()
1949 (with-current-buffer gud-comint-buffer
1950 (concat "*threads of " (gdb-get-target-string) "*")))
1952 (defun gdb-display-threads-buffer ()
1953 "Display IDs of currently known threads."
1956 (gdb-get-buffer-create 'gdb-threads-buffer)))
1958 (defun gdb-frame-threads-buffer ()
1959 "Display IDs of currently known threads in a new frame."
1961 (let ((special-display-regexps (append special-display-regexps '(".*")))
1962 (special-display-frame-alist gdb-frame-parameters))
1963 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
1965 (defvar gdb-threads-mode-map
1966 (let ((map (make-sparse-keymap)))
1967 (suppress-keymap map)
1968 (define-key map "q" 'kill-this-buffer)
1969 (define-key map "\r" 'gdb-threads-select)
1970 (define-key map [mouse-2] 'gdb-threads-select)
1971 (define-key map [follow-link] 'mouse-face)
1974 (defvar gdb-threads-font-lock-keywords
1976 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
1977 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1978 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
1980 "Font lock keywords used in `gdb-threads-mode'.")
1982 (defun gdb-threads-mode ()
1983 "Major mode for gdb frames.
1985 \\{gdb-threads-mode-map}"
1986 (kill-all-local-variables)
1987 (setq major-mode 'gdb-threads-mode)
1988 (setq mode-name "Threads")
1989 (setq buffer-read-only t)
1990 (use-local-map gdb-threads-mode-map)
1991 (set (make-local-variable 'font-lock-defaults)
1992 '(gdb-threads-font-lock-keywords))
1993 (run-mode-hooks 'gdb-threads-mode-hook)
1994 'gdb-invalidate-threads)
1996 (defun gdb-get-thread-number ()
1998 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1999 (match-string-no-properties 1)))
2001 (defun gdb-threads-select (&optional event)
2002 "Select the thread and display the relevant source."
2003 (interactive (list last-input-event))
2004 (if event (posn-set-point (event-end event)))
2006 (list (concat gdb-server-prefix "thread "
2007 (gdb-get-thread-number) "\n") 'ignore))
2008 (gud-display-frame))
2011 ;; Registers buffer.
2013 (defcustom gdb-all-registers nil
2014 "Non-nil means include floating-point registers."
2019 (gdb-set-buffer-rules 'gdb-registers-buffer
2020 'gdb-registers-buffer-name
2021 'gdb-registers-mode)
2023 (def-gdb-auto-updated-buffer gdb-registers-buffer
2024 gdb-invalidate-registers
2026 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2027 gdb-info-registers-handler
2028 gdb-info-registers-custom)
2030 (defun gdb-info-registers-custom ()
2031 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2033 (let ((buffer-read-only nil)
2035 (goto-char (point-min))
2036 (while (< (point) (point-max))
2037 (setq start (line-beginning-position))
2038 (setq end (line-end-position))
2039 (when (looking-at "^[^ ]+")
2040 (unless (string-equal (match-string 0) "The")
2041 (put-text-property start (match-end 0)
2042 'face font-lock-variable-name-face)
2043 (add-text-properties start end
2044 '(help-echo "mouse-2: edit value"
2045 mouse-face highlight))))
2046 (forward-line 1))))))
2048 (defun gdb-edit-register-value (&optional event)
2049 (interactive (list last-input-event))
2051 (if event (posn-set-point (event-end event)))
2053 (let* ((register (current-word))
2054 (value (read-string (format "New value (%s): " register))))
2056 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2059 (defvar gdb-registers-mode-map
2060 (let ((map (make-sparse-keymap)))
2061 (suppress-keymap map)
2062 (define-key map "\r" 'gdb-edit-register-value)
2063 (define-key map [mouse-2] 'gdb-edit-register-value)
2064 (define-key map " " 'gdb-all-registers)
2065 (define-key map "q" 'kill-this-buffer)
2068 (defun gdb-registers-mode ()
2069 "Major mode for gdb registers.
2071 \\{gdb-registers-mode-map}"
2072 (kill-all-local-variables)
2073 (setq major-mode 'gdb-registers-mode)
2074 (setq mode-name "Registers")
2075 (setq buffer-read-only t)
2076 (use-local-map gdb-registers-mode-map)
2077 (run-mode-hooks 'gdb-registers-mode-hook)
2078 (if (string-equal gdb-version "pre-6.4")
2080 (if gdb-all-registers (setq mode-name "Registers:All"))
2081 'gdb-invalidate-registers)
2082 'gdb-invalidate-registers-1))
2084 (defun gdb-registers-buffer-name ()
2085 (with-current-buffer gud-comint-buffer
2086 (concat "*registers of " (gdb-get-target-string) "*")))
2088 (defun gdb-display-registers-buffer ()
2089 "Display integer register contents."
2092 (gdb-get-buffer-create 'gdb-registers-buffer)))
2094 (defun gdb-frame-registers-buffer ()
2095 "Display integer register contents in a new frame."
2097 (let ((special-display-regexps (append special-display-regexps '(".*")))
2098 (special-display-frame-alist gdb-frame-parameters))
2099 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2101 (defun gdb-all-registers ()
2102 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2104 (when (string-equal gdb-version "pre-6.4")
2105 (if gdb-all-registers
2107 (setq gdb-all-registers nil)
2108 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2109 (setq mode-name "Registers")))
2110 (setq gdb-all-registers t)
2111 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2112 (setq mode-name "Registers:All")))
2113 (message (format "Display of floating-point registers %sabled"
2114 (if gdb-all-registers "en" "dis")))
2115 (gdb-invalidate-registers)))
2120 (defcustom gdb-memory-repeat-count 32
2121 "Number of data items in memory window."
2126 (defcustom gdb-memory-format "x"
2127 "Display format of data items in memory window."
2128 :type '(choice (const :tag "Hexadecimal" "x")
2129 (const :tag "Signed decimal" "d")
2130 (const :tag "Unsigned decimal" "u")
2131 (const :tag "Octal" "o")
2132 (const :tag "Binary" "t"))
2136 (defcustom gdb-memory-unit "w"
2137 "Unit size of data items in memory window."
2138 :type '(choice (const :tag "Byte" "b")
2139 (const :tag "Halfword" "h")
2140 (const :tag "Word" "w")
2141 (const :tag "Giant word" "g"))
2145 (gdb-set-buffer-rules 'gdb-memory-buffer
2146 'gdb-memory-buffer-name
2149 (def-gdb-auto-updated-buffer gdb-memory-buffer
2150 gdb-invalidate-memory
2151 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2152 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2153 gdb-read-memory-handler
2154 gdb-read-memory-custom)
2156 (defun gdb-read-memory-custom ()
2158 (goto-char (point-min))
2159 (if (looking-at "0x[[:xdigit:]]+")
2160 (setq gdb-memory-address (match-string 0)))))
2162 (defvar gdb-memory-mode-map
2163 (let ((map (make-sparse-keymap)))
2164 (suppress-keymap map)
2165 (define-key map "q" 'kill-this-buffer)
2168 (defun gdb-memory-set-address (event)
2169 "Set the start memory address."
2171 (save-selected-window
2172 (select-window (posn-window (event-start event)))
2173 (let ((arg (read-from-minibuffer "Memory address: ")))
2174 (setq gdb-memory-address arg))
2175 (gdb-invalidate-memory)))
2177 (defun gdb-memory-set-repeat-count (event)
2178 "Set the number of data items in memory window."
2180 (save-selected-window
2181 (select-window (posn-window (event-start event)))
2182 (let* ((arg (read-from-minibuffer "Repeat count: "))
2183 (count (string-to-number arg)))
2185 (error "Positive numbers only")
2186 (customize-set-variable 'gdb-memory-repeat-count count)
2187 (gdb-invalidate-memory)))))
2189 (defun gdb-memory-format-binary ()
2190 "Set the display format to binary."
2192 (customize-set-variable 'gdb-memory-format "t")
2193 (gdb-invalidate-memory))
2195 (defun gdb-memory-format-octal ()
2196 "Set the display format to octal."
2198 (customize-set-variable 'gdb-memory-format "o")
2199 (gdb-invalidate-memory))
2201 (defun gdb-memory-format-unsigned ()
2202 "Set the display format to unsigned decimal."
2204 (customize-set-variable 'gdb-memory-format "u")
2205 (gdb-invalidate-memory))
2207 (defun gdb-memory-format-signed ()
2208 "Set the display format to decimal."
2210 (customize-set-variable 'gdb-memory-format "d")
2211 (gdb-invalidate-memory))
2213 (defun gdb-memory-format-hexadecimal ()
2214 "Set the display format to hexadecimal."
2216 (customize-set-variable 'gdb-memory-format "x")
2217 (gdb-invalidate-memory))
2219 (defvar gdb-memory-format-map
2220 (let ((map (make-sparse-keymap)))
2221 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2223 "Keymap to select format in the header line.")
2225 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2226 "Menu of display formats in the header line.")
2228 (define-key gdb-memory-format-menu [binary]
2229 '(menu-item "Binary" gdb-memory-format-binary
2230 :button (:radio . (equal gdb-memory-format "t"))))
2231 (define-key gdb-memory-format-menu [octal]
2232 '(menu-item "Octal" gdb-memory-format-octal
2233 :button (:radio . (equal gdb-memory-format "o"))))
2234 (define-key gdb-memory-format-menu [unsigned]
2235 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2236 :button (:radio . (equal gdb-memory-format "u"))))
2237 (define-key gdb-memory-format-menu [signed]
2238 '(menu-item "Signed Decimal" gdb-memory-format-signed
2239 :button (:radio . (equal gdb-memory-format "d"))))
2240 (define-key gdb-memory-format-menu [hexadecimal]
2241 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2242 :button (:radio . (equal gdb-memory-format "x"))))
2244 (defun gdb-memory-format-menu (event)
2246 (x-popup-menu event gdb-memory-format-menu))
2248 (defun gdb-memory-format-menu-1 (event)
2250 (save-selected-window
2251 (select-window (posn-window (event-start event)))
2252 (let* ((selection (gdb-memory-format-menu event))
2253 (binding (and selection (lookup-key gdb-memory-format-menu
2254 (vector (car selection))))))
2255 (if binding (call-interactively binding)))))
2257 (defun gdb-memory-unit-giant ()
2258 "Set the unit size to giant words (eight bytes)."
2260 (customize-set-variable 'gdb-memory-unit "g")
2261 (gdb-invalidate-memory))
2263 (defun gdb-memory-unit-word ()
2264 "Set the unit size to words (four bytes)."
2266 (customize-set-variable 'gdb-memory-unit "w")
2267 (gdb-invalidate-memory))
2269 (defun gdb-memory-unit-halfword ()
2270 "Set the unit size to halfwords (two bytes)."
2272 (customize-set-variable 'gdb-memory-unit "h")
2273 (gdb-invalidate-memory))
2275 (defun gdb-memory-unit-byte ()
2276 "Set the unit size to bytes."
2278 (customize-set-variable 'gdb-memory-unit "b")
2279 (gdb-invalidate-memory))
2281 (defvar gdb-memory-unit-map
2282 (let ((map (make-sparse-keymap)))
2283 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2285 "Keymap to select units in the header line.")
2287 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2288 "Menu of units in the header line.")
2290 (define-key gdb-memory-unit-menu [giantwords]
2291 '(menu-item "Giant words" gdb-memory-unit-giant
2292 :button (:radio . (equal gdb-memory-unit "g"))))
2293 (define-key gdb-memory-unit-menu [words]
2294 '(menu-item "Words" gdb-memory-unit-word
2295 :button (:radio . (equal gdb-memory-unit "w"))))
2296 (define-key gdb-memory-unit-menu [halfwords]
2297 '(menu-item "Halfwords" gdb-memory-unit-halfword
2298 :button (:radio . (equal gdb-memory-unit "h"))))
2299 (define-key gdb-memory-unit-menu [bytes]
2300 '(menu-item "Bytes" gdb-memory-unit-byte
2301 :button (:radio . (equal gdb-memory-unit "b"))))
2303 (defun gdb-memory-unit-menu (event)
2305 (x-popup-menu event gdb-memory-unit-menu))
2307 (defun gdb-memory-unit-menu-1 (event)
2309 (save-selected-window
2310 (select-window (posn-window (event-start event)))
2311 (let* ((selection (gdb-memory-unit-menu event))
2312 (binding (and selection (lookup-key gdb-memory-unit-menu
2313 (vector (car selection))))))
2314 (if binding (call-interactively binding)))))
2316 ;;from make-mode-line-mouse-map
2317 (defun gdb-make-header-line-mouse-map (mouse function) "\
2318 Return a keymap with single entry for mouse key MOUSE on the header line.
2319 MOUSE is defined to run function FUNCTION with no args in the buffer
2320 corresponding to the mode line clicked."
2321 (let ((map (make-sparse-keymap)))
2322 (define-key map (vector 'header-line mouse) function)
2323 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2326 (defvar gdb-memory-font-lock-keywords
2327 '(;; <__function.name+n>
2328 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2330 "Font lock keywords used in `gdb-memory-mode'.")
2332 (defun gdb-memory-mode ()
2333 "Major mode for examining memory.
2335 \\{gdb-memory-mode-map}"
2336 (kill-all-local-variables)
2337 (setq major-mode 'gdb-memory-mode)
2338 (setq mode-name "Memory")
2339 (setq buffer-read-only t)
2340 (use-local-map gdb-memory-mode-map)
2341 (setq header-line-format
2347 'face font-lock-warning-face
2348 'help-echo "mouse-1: Decrement address"
2349 'mouse-face 'mode-line-highlight
2351 (gdb-make-header-line-mouse-map
2353 #'(lambda () (interactive)
2354 (let ((gdb-memory-address
2355 ;; Let GDB do the arithmetic.
2357 gdb-memory-address " - "
2359 (* gdb-memory-repeat-count
2360 (cond ((string= gdb-memory-unit "b") 1)
2361 ((string= gdb-memory-unit "h") 2)
2362 ((string= gdb-memory-unit "w") 4)
2363 ((string= gdb-memory-unit "g") 8)))))))
2364 (gdb-invalidate-memory)))))
2367 'face font-lock-warning-face
2368 'help-echo "mouse-1: Increment address"
2369 'mouse-face 'mode-line-highlight
2370 'local-map (gdb-make-header-line-mouse-map
2372 #'(lambda () (interactive)
2373 (let ((gdb-memory-address nil))
2374 (gdb-invalidate-memory)))))
2376 (propertize gdb-memory-address
2377 'face font-lock-warning-face
2378 'help-echo "mouse-1: Set memory address"
2379 'mouse-face 'mode-line-highlight
2380 'local-map (gdb-make-header-line-mouse-map
2382 #'gdb-memory-set-address))
2384 (propertize (number-to-string gdb-memory-repeat-count)
2385 'face font-lock-warning-face
2386 'help-echo "mouse-1: Set repeat count"
2387 'mouse-face 'mode-line-highlight
2388 'local-map (gdb-make-header-line-mouse-map
2390 #'gdb-memory-set-repeat-count))
2392 (propertize gdb-memory-format
2393 'face font-lock-warning-face
2394 'help-echo "mouse-3: Select display format"
2395 'mouse-face 'mode-line-highlight
2396 'local-map gdb-memory-format-map)
2398 (propertize gdb-memory-unit
2399 'face font-lock-warning-face
2400 'help-echo "mouse-3: Select unit size"
2401 'mouse-face 'mode-line-highlight
2402 'local-map gdb-memory-unit-map))))
2403 (set (make-local-variable 'font-lock-defaults)
2404 '(gdb-memory-font-lock-keywords))
2405 (run-mode-hooks 'gdb-memory-mode-hook)
2406 'gdb-invalidate-memory)
2408 (defun gdb-memory-buffer-name ()
2409 (with-current-buffer gud-comint-buffer
2410 (concat "*memory of " (gdb-get-target-string) "*")))
2412 (defun gdb-display-memory-buffer ()
2413 "Display memory contents."
2416 (gdb-get-buffer-create 'gdb-memory-buffer)))
2418 (defun gdb-frame-memory-buffer ()
2419 "Display memory contents in a new frame."
2421 (let ((special-display-regexps (append special-display-regexps '(".*")))
2422 (special-display-frame-alist gdb-frame-parameters))
2423 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2428 (gdb-set-buffer-rules 'gdb-locals-buffer
2429 'gdb-locals-buffer-name
2432 (def-gdb-auto-update-trigger gdb-invalidate-locals
2433 (gdb-get-buffer 'gdb-locals-buffer)
2434 "server info locals\n"
2435 gdb-info-locals-handler)
2437 (defvar gdb-locals-watch-map
2438 (let ((map (make-sparse-keymap)))
2439 (define-key map "\r" '(lambda () (interactive)
2442 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2443 (mouse-set-point event)
2447 "Keymap to create watch expression of a complex data type local variable.")
2449 (defconst gdb-struct-string
2450 (concat (propertize "[struct/union]"
2451 'mouse-face 'highlight
2452 'help-echo "mouse-2: create watch expression"
2453 'local-map gdb-locals-watch-map) "\n"))
2455 (defconst gdb-array-string
2456 (concat " " (propertize "[array]"
2457 'mouse-face 'highlight
2458 'help-echo "mouse-2: create watch expression"
2459 'local-map gdb-locals-watch-map) "\n"))
2461 ;; Abbreviate for arrays and structures.
2462 ;; These can be expanded using gud-display.
2463 (defun gdb-info-locals-handler ()
2464 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2465 gdb-pending-triggers))
2466 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2467 (with-current-buffer buf
2468 (goto-char (point-min))
2469 (while (re-search-forward "^[ }].*\n" nil t)
2470 (replace-match "" nil nil))
2471 (goto-char (point-min))
2472 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2473 (replace-match gdb-struct-string nil nil))
2474 (goto-char (point-min))
2475 (while (re-search-forward "\\s-*{.*\n" nil t)
2476 (replace-match gdb-array-string nil nil))))
2477 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2479 (with-current-buffer buf
2480 (let* ((window (get-buffer-window buf 0))
2481 (start (window-start window))
2482 (p (window-point window))
2483 (buffer-read-only nil))
2485 (insert-buffer-substring (gdb-get-buffer-create
2486 'gdb-partial-output-buffer))
2487 (set-window-start window start)
2488 (set-window-point window p))
2490 (run-hooks 'gdb-info-locals-hook))
2492 (defvar gdb-locals-mode-map
2493 (let ((map (make-sparse-keymap)))
2494 (suppress-keymap map)
2495 (define-key map "q" 'kill-this-buffer)
2498 (defun gdb-locals-mode ()
2499 "Major mode for gdb locals.
2501 \\{gdb-locals-mode-map}"
2502 (kill-all-local-variables)
2503 (setq major-mode 'gdb-locals-mode)
2504 (setq mode-name (concat "Locals:" gdb-selected-frame))
2505 (setq buffer-read-only t)
2506 (use-local-map gdb-locals-mode-map)
2507 (set (make-local-variable 'font-lock-defaults)
2508 '(gdb-locals-font-lock-keywords))
2509 (run-mode-hooks 'gdb-locals-mode-hook)
2510 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2511 (string-equal gdb-version "pre-6.4"))
2512 'gdb-invalidate-locals
2513 'gdb-invalidate-locals-1))
2515 (defun gdb-locals-buffer-name ()
2516 (with-current-buffer gud-comint-buffer
2517 (concat "*locals of " (gdb-get-target-string) "*")))
2519 (defun gdb-display-locals-buffer ()
2520 "Display local variables of current stack and their values."
2523 (gdb-get-buffer-create 'gdb-locals-buffer)))
2525 (defun gdb-frame-locals-buffer ()
2526 "Display local variables of current stack and their values in a new frame."
2528 (let ((special-display-regexps (append special-display-regexps '(".*")))
2529 (special-display-frame-alist gdb-frame-parameters))
2530 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2533 ;;;; Window management
2534 (defun gdb-display-buffer (buf &optional size)
2535 (let ((answer (get-buffer-window buf 0))
2538 (display-buffer buf nil 0) ;Raise the frame if necessary.
2539 ;; The buffer is not yet displayed.
2540 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2541 (let ((window (get-lru-window)))
2543 (not (eq window (get-buffer-window gud-comint-buffer))))
2545 (set-window-buffer window buf)
2546 (setq answer window))
2547 (setq must-split t)))
2549 (let* ((largest (get-largest-window))
2550 (cur-size (window-height largest))
2551 (new-size (and size (< size cur-size) (- cur-size size))))
2552 (setq answer (split-window largest new-size))
2553 (set-window-buffer answer buf)
2554 (set-window-dedicated-p answer t)))
2558 ;;; Shared keymap initialization:
2560 (let ((menu (make-sparse-keymap "GDB-Windows")))
2561 (define-key gud-menu-map [displays]
2562 `(menu-item "GDB-Windows" ,menu
2563 :visible (memq gud-minor-mode '(gdbmi gdba))))
2564 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2565 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2566 (define-key menu [inferior]
2567 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2568 :enable gdb-use-separate-io-buffer))
2569 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2570 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2571 (define-key menu [disassembly]
2572 '("Disassembly" . gdb-display-assembler-buffer))
2573 (define-key menu [breakpoints]
2574 '("Breakpoints" . gdb-display-breakpoints-buffer))
2575 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2576 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2578 (let ((menu (make-sparse-keymap "GDB-Frames")))
2579 (define-key gud-menu-map [frames]
2580 `(menu-item "GDB-Frames" ,menu
2581 :visible (memq gud-minor-mode '(gdbmi gdba))))
2582 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2583 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2584 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2585 (define-key menu [inferior]
2586 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2587 :enable gdb-use-separate-io-buffer))
2588 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2589 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2590 (define-key menu [breakpoints]
2591 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2592 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2593 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2595 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2596 (define-key gud-menu-map [ui]
2597 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2598 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2599 (define-key menu [gdb-use-separate-io]
2600 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2601 :visible (eq gud-minor-mode 'gdba)
2602 :help "Toggle separate IO for inferior."
2603 :button (:toggle . gdb-use-separate-io-buffer)))
2604 (define-key menu [gdb-many-windows]
2605 '(menu-item "Display Other Windows" gdb-many-windows
2606 :help "Toggle display of locals, stack and breakpoint information"
2607 :button (:toggle . gdb-many-windows)))
2608 (define-key menu [gdb-restore-windows]
2609 '(menu-item "Restore Window Layout" gdb-restore-windows
2610 :help "Restore standard layout for debug session.")))
2612 (defun gdb-frame-gdb-buffer ()
2613 "Display GUD buffer in a new frame."
2615 (let ((special-display-regexps (append special-display-regexps '(".*")))
2616 (special-display-frame-alist gdb-frame-parameters)
2617 (same-window-regexps nil))
2618 (display-buffer gud-comint-buffer)))
2620 (defun gdb-display-gdb-buffer ()
2621 "Display GUD buffer."
2623 (let ((same-window-regexps nil))
2624 (pop-to-buffer gud-comint-buffer)))
2626 (defun gdb-set-window-buffer (name)
2627 (set-window-buffer (selected-window) (get-buffer name))
2628 (set-window-dedicated-p (selected-window) t))
2630 (defun gdb-setup-windows ()
2631 "Layout the window pattern for `gdb-many-windows'."
2632 (gdb-display-locals-buffer)
2633 (gdb-display-stack-buffer)
2634 (delete-other-windows)
2635 (gdb-display-breakpoints-buffer)
2636 (delete-other-windows)
2638 (pop-to-buffer gud-comint-buffer)
2639 (split-window nil ( / ( * (window-height) 3) 4))
2640 (split-window nil ( / (window-height) 3))
2641 (split-window-horizontally)
2643 (gdb-set-window-buffer (gdb-locals-buffer-name))
2646 (if gud-last-last-frame
2647 (gud-find-file (car gud-last-last-frame))
2648 (gud-find-file gdb-main-file)))
2649 (when gdb-use-separate-io-buffer
2650 (split-window-horizontally)
2652 (gdb-set-window-buffer
2653 (gdb-get-buffer-create 'gdb-inferior-io)))
2655 (gdb-set-window-buffer (gdb-stack-buffer-name))
2656 (split-window-horizontally)
2658 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2661 (defcustom gdb-many-windows nil
2662 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2663 In this case it starts with two windows: one displaying the GUD
2664 buffer and the other with the source file with the main routine
2665 of the inferior. Non-nil means display the layout shown for
2671 (defun gdb-many-windows (arg)
2672 "Toggle the number of windows in the basic arrangement.
2673 With arg, display additional buffers iff arg is positive."
2675 (setq gdb-many-windows
2677 (not gdb-many-windows)
2678 (> (prefix-numeric-value arg) 0)))
2679 (message (format "Display of other windows %sabled"
2680 (if gdb-many-windows "en" "dis")))
2681 (if (and gud-comint-buffer
2682 (buffer-name gud-comint-buffer))
2684 (gdb-restore-windows)
2687 (defun gdb-restore-windows ()
2688 "Restore the basic arrangement of windows used by gdba.
2689 This arrangement depends on the value of `gdb-many-windows'."
2691 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2692 (delete-other-windows)
2693 (if gdb-many-windows
2695 (when (or gud-last-last-frame gdb-show-main)
2699 (if gud-last-last-frame
2700 (gud-find-file (car gud-last-last-frame))
2701 (gud-find-file gdb-main-file)))
2705 "Exit a debugging session cleanly.
2706 Kills the gdb buffers, and resets variables and the source buffers."
2707 (dolist (buffer (buffer-list))
2708 (unless (eq buffer gud-comint-buffer)
2709 (with-current-buffer buffer
2710 (if (memq gud-minor-mode '(gdbmi gdba))
2711 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2713 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2714 (setq gud-minor-mode nil)
2715 (kill-local-variable 'tool-bar-map)
2716 (kill-local-variable 'gdb-define-alist))))))
2717 (when (markerp gdb-overlay-arrow-position)
2718 (move-marker gdb-overlay-arrow-position nil)
2719 (setq gdb-overlay-arrow-position nil))
2720 (setq overlay-arrow-variable-list
2721 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2722 (if (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
2724 (setq gud-running nil)
2725 (setq gdb-active-process nil)
2726 (setq gdb-var-list nil)
2727 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2729 (defun gdb-source-info ()
2730 "Find the source file where the program starts and displays it with related
2732 (goto-char (point-min))
2733 (if (and (search-forward "Located in " nil t)
2734 (looking-at "\\S-+"))
2735 (setq gdb-main-file (match-string 0)))
2736 (goto-char (point-min))
2737 (if (search-forward "Includes preprocessor macro info." nil t)
2738 (setq gdb-macro-info t))
2739 (if gdb-many-windows
2741 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2743 (let ((pop-up-windows t))
2744 (display-buffer (gud-find-file gdb-main-file))))))
2746 (defun gdb-get-location (bptno line flag)
2747 "Find the directory containing the relevant source file.
2748 Put in buffer and place breakpoint icon."
2749 (goto-char (point-min))
2750 (catch 'file-not-found
2751 (if (search-forward "Located in " nil t)
2752 (when (looking-at "\\S-+")
2753 (delete (cons bptno "File not found") gdb-location-alist)
2754 (push (cons bptno (match-string 0)) gdb-location-alist))
2756 (unless (assoc bptno gdb-location-alist)
2757 (push (cons bptno "File not found") gdb-location-alist)
2758 (message-box "Cannot find source file for breakpoint location.\n\
2759 Add directory to search path for source files using the GDB command, dir."))
2760 (throw 'file-not-found nil))
2761 (with-current-buffer
2762 (find-file-noselect (match-string 0))
2763 (save-current-buffer
2764 (set (make-local-variable 'gud-minor-mode) 'gdba)
2765 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2766 ;; only want one breakpoint icon at each location
2768 (goto-line (string-to-number line))
2769 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2771 (add-hook 'find-file-hook 'gdb-find-file-hook)
2773 (defun gdb-find-file-hook ()
2774 "Set up buffer for debugging if file is part of the source code
2775 of the current session."
2776 (if (and (buffer-name gud-comint-buffer)
2777 ;; in case gud or gdb-ui is just loaded
2779 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2781 (if (member buffer-file-name gdb-source-file-list)
2782 (with-current-buffer (find-buffer-visiting buffer-file-name)
2783 (set (make-local-variable 'gud-minor-mode)
2784 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2785 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2788 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2789 "Put string PUTSTRING in front of POS in the current buffer.
2790 PUTSTRING is displayed by putting an overlay into the current buffer with a
2791 `before-string' string that has a `display' property whose value is
2793 (let ((string (make-string 1 ?x))
2794 (buffer (current-buffer)))
2795 (setq putstring (copy-sequence putstring))
2796 (let ((overlay (make-overlay pos pos buffer))
2798 (list (list 'margin 'left-margin) putstring))))
2799 (put-text-property 0 1 'display prop string)
2801 (add-text-properties 0 1 sprops string))
2802 (overlay-put overlay 'put-break t)
2803 (overlay-put overlay 'before-string string))))
2805 ;;from remove-images
2806 (defun gdb-remove-strings (start end &optional buffer)
2807 "Remove strings between START and END in BUFFER.
2808 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2809 BUFFER nil or omitted means use the current buffer."
2811 (setq buffer (current-buffer)))
2812 (dolist (overlay (overlays-in start end))
2813 (when (overlay-get overlay 'put-break)
2814 (delete-overlay overlay))))
2816 (defun gdb-put-breakpoint-icon (enabled bptno)
2817 (let ((start (- (line-beginning-position) 1))
2818 (end (+ (line-end-position) 1))
2819 (putstring (if enabled "B" "b"))
2820 (source-window (get-buffer-window (current-buffer) 0)))
2821 (add-text-properties
2822 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2825 (add-text-properties
2826 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2827 (add-text-properties
2828 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2829 (gdb-remove-breakpoint-icons start end)
2830 (if (display-images-p)
2831 (if (>= (or left-fringe-width
2832 (if source-window (car (window-fringes source-window)))
2833 gdb-buffer-fringe-width) 8)
2836 `(left-fringe breakpoint
2839 'breakpoint-disabled))
2841 'gdb-enabled enabled)
2842 (when (< left-margin-width 2)
2843 (save-current-buffer
2844 (setq left-margin-width 2)
2848 left-margin-width right-margin-width))))
2851 (or breakpoint-enabled-icon
2852 (setq breakpoint-enabled-icon
2853 (find-image `((:type xpm :data
2854 ,breakpoint-xpm-data
2855 :ascent 100 :pointer hand)
2857 ,breakpoint-enabled-pbm-data
2858 :ascent 100 :pointer hand)))))
2859 (or breakpoint-disabled-icon
2860 (setq breakpoint-disabled-icon
2861 (find-image `((:type xpm :data
2862 ,breakpoint-xpm-data
2863 :conversion disabled
2864 :ascent 100 :pointer hand)
2866 ,breakpoint-disabled-pbm-data
2867 :ascent 100 :pointer hand))))))
2871 (when (< left-margin-width 2)
2872 (save-current-buffer
2873 (setq left-margin-width 2)
2874 (let ((window (get-buffer-window (current-buffer) 0)))
2877 window left-margin-width right-margin-width)))))
2879 (propertize putstring
2880 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2883 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2884 (gdb-remove-strings start end)
2885 (if (display-images-p)
2886 (remove-images start end))
2888 (setq left-margin-width 0)
2889 (let ((window (get-buffer-window (current-buffer) 0)))
2892 window left-margin-width right-margin-width)))))
2896 ;; Assembler buffer.
2898 (gdb-set-buffer-rules 'gdb-assembler-buffer
2899 'gdb-assembler-buffer-name
2900 'gdb-assembler-mode)
2902 (def-gdb-auto-update-handler gdb-assembler-handler
2903 gdb-invalidate-assembler
2904 gdb-assembler-buffer
2905 gdb-assembler-custom)
2907 (defun gdb-assembler-custom ()
2908 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2909 (pos 1) (address) (flag) (bptno))
2910 (with-current-buffer buffer
2912 (if (not (equal gdb-frame-address "main"))
2914 (goto-char (point-min))
2915 (if (and gdb-frame-address
2916 (search-forward gdb-frame-address nil t))
2920 (or gdb-overlay-arrow-position
2921 (setq gdb-overlay-arrow-position (make-marker)))
2922 (set-marker gdb-overlay-arrow-position
2923 (point) (current-buffer))))))
2924 ;; remove all breakpoint-icons in assembler buffer before updating.
2925 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2926 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2927 (goto-char (point-min))
2928 (while (< (point) (- (point-max) 1))
2930 (if (looking-at "[^\t].*?breakpoint")
2933 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2934 (setq bptno (match-string 1))
2935 (setq flag (char-after (match-beginning 2)))
2936 (setq address (match-string 3))
2937 (with-current-buffer buffer
2939 (goto-char (point-min))
2940 (if (search-forward address nil t)
2941 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2942 (if (not (equal gdb-frame-address "main"))
2943 (with-current-buffer buffer
2944 (set-window-point (get-buffer-window buffer 0) pos)))))
2946 (defvar gdb-assembler-mode-map
2947 (let ((map (make-sparse-keymap)))
2948 (suppress-keymap map)
2949 (define-key map "q" 'kill-this-buffer)
2952 (defvar gdb-assembler-font-lock-keywords
2953 '(;; <__function.name+n>
2954 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2955 (1 font-lock-function-name-face))
2956 ;; 0xNNNNNNNN <__function.name+n>: opcode
2957 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2958 (4 font-lock-keyword-face))
2959 ;; %register(at least i386)
2960 ("%\\sw+" . font-lock-variable-name-face)
2961 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2962 (1 font-lock-comment-face)
2963 (2 font-lock-function-name-face))
2964 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2965 "Font lock keywords used in `gdb-assembler-mode'.")
2967 (defun gdb-assembler-mode ()
2968 "Major mode for viewing code assembler.
2970 \\{gdb-assembler-mode-map}"
2971 (kill-all-local-variables)
2972 (setq major-mode 'gdb-assembler-mode)
2973 (setq mode-name (concat "Machine:" gdb-selected-frame))
2974 (setq gdb-overlay-arrow-position nil)
2975 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2976 (setq fringes-outside-margins t)
2977 (setq buffer-read-only t)
2978 (use-local-map gdb-assembler-mode-map)
2979 (gdb-invalidate-assembler)
2980 (set (make-local-variable 'font-lock-defaults)
2981 '(gdb-assembler-font-lock-keywords))
2982 (run-mode-hooks 'gdb-assembler-mode-hook)
2983 'gdb-invalidate-assembler)
2985 (defun gdb-assembler-buffer-name ()
2986 (with-current-buffer gud-comint-buffer
2987 (concat "*disassembly of " (gdb-get-target-string) "*")))
2989 (defun gdb-display-assembler-buffer ()
2990 "Display disassembly view."
2992 (setq gdb-previous-frame nil)
2994 (gdb-get-buffer-create 'gdb-assembler-buffer)))
2996 (defun gdb-frame-assembler-buffer ()
2997 "Display disassembly view in a new frame."
2999 (setq gdb-previous-frame nil)
3000 (let ((special-display-regexps (append special-display-regexps '(".*")))
3001 (special-display-frame-alist gdb-frame-parameters))
3002 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3004 ;; modified because if gdb-frame-address has changed value a new command
3005 ;; must be enqueued to update the buffer with the new output
3006 (defun gdb-invalidate-assembler (&optional ignored)
3007 (if (gdb-get-buffer 'gdb-assembler-buffer)
3009 (unless (and gdb-selected-frame
3010 (string-equal gdb-selected-frame gdb-previous-frame))
3011 (if (or (not (member 'gdb-invalidate-assembler
3012 gdb-pending-triggers))
3013 (not (string-equal gdb-frame-address
3014 gdb-previous-frame-address)))
3016 ;; take previous disassemble command, if any, off the queue
3017 (with-current-buffer gud-comint-buffer
3018 (let ((queue gdb-input-queue))
3019 (dolist (item queue)
3020 (if (equal (cdr item) '(gdb-assembler-handler))
3021 (setq gdb-input-queue
3022 (delete item gdb-input-queue))))))
3025 (concat gdb-server-prefix "disassemble "
3026 (if (member gdb-frame-address '(nil "main")) nil "0x")
3027 gdb-frame-address "\n")
3028 'gdb-assembler-handler))
3029 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3030 (setq gdb-previous-frame-address gdb-frame-address)
3031 (setq gdb-previous-frame gdb-selected-frame)))))))
3033 (defun gdb-get-selected-frame ()
3034 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3037 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3038 (push 'gdb-get-selected-frame
3039 gdb-pending-triggers))))
3041 (defun gdb-frame-handler ()
3042 (setq gdb-pending-triggers
3043 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3044 (goto-char (point-min))
3045 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
3046 (setq gdb-frame-number (match-string 1)))
3047 (goto-char (point-min))
3048 (if (re-search-forward
3049 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
3051 (setq gdb-selected-frame (match-string 2))
3052 (if (gdb-get-buffer 'gdb-locals-buffer)
3053 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3054 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3055 (if (gdb-get-buffer 'gdb-assembler-buffer)
3056 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3057 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3058 (setq gdb-frame-address (match-string 1))))
3059 (goto-char (point-min))
3060 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
3061 (setq gdb-current-language (match-string 1)))
3062 (gdb-invalidate-assembler))
3065 ;; Code specific to GDB 6.4
3066 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3068 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3069 "Create list of source files for current GDB session.
3070 If buffers already exist for any of these files, gud-minor-mode
3072 (goto-char (point-min))
3073 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3074 (push (match-string 1) gdb-source-file-list))
3075 (dolist (buffer (buffer-list))
3076 (with-current-buffer buffer
3077 (when (member buffer-file-name gdb-source-file-list)
3078 (set (make-local-variable 'gud-minor-mode)
3079 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3080 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3081 (when gud-tooltip-mode
3082 (make-local-variable 'gdb-define-alist)
3083 (gdb-create-define-alist)
3084 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
3086 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3087 (defun gdb-var-list-children-1 (varnum)
3090 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3091 (concat "server interpreter mi \"-var-list-children --all-values "
3093 (concat "-var-list-children --all-values " varnum "\n"))
3094 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3096 (defconst gdb-var-list-children-regexp-1
3097 "name=\"\\(.+?\\)\",exp=\"\\(.+?\\)\",numchild=\"\\(.+?\\)\",\
3098 value=\\(\".*?\"\\),type=\"\\(.+?\\)\"}")
3100 (defun gdb-var-list-children-handler-1 (varnum)
3101 (goto-char (point-min))
3102 (let ((var-list nil))
3103 (catch 'child-already-watched
3104 (dolist (var gdb-var-list)
3105 (if (string-equal varnum (cadr var))
3108 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3109 (let ((varchild (list (match-string 2)
3113 (read (match-string 4))
3115 (dolist (var1 gdb-var-list)
3116 (if (string-equal (cadr var1) (cadr varchild))
3117 (throw 'child-already-watched nil)))
3118 (push varchild var-list))))
3119 (push var var-list)))
3120 (setq gdb-var-list (nreverse var-list)))))
3122 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3123 (defun gdb-var-update-1 ()
3124 (if (not (member 'gdb-var-update gdb-pending-triggers))
3128 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3129 "server interpreter mi \"-var-update --all-values *\"\n"
3130 "-var-update --all-values *\n")
3131 'gdb-var-update-handler-1))
3132 (push 'gdb-var-update gdb-pending-triggers))))
3134 (defconst gdb-var-update-regexp-1
3135 "name=\"\\(.*?\\)\",\\(?:value=\\(\".*?\"\\),\\)?in_scope=\"\\(.*?\\)\"")
3137 (defun gdb-var-update-handler-1 ()
3138 (dolist (var gdb-var-list)
3139 (setcar (nthcdr 5 var) nil))
3140 (goto-char (point-min))
3141 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3142 (let ((varnum (match-string 1)))
3144 (dolist (var gdb-var-list)
3145 (when (string-equal varnum (cadr var))
3146 (if (string-equal (match-string 3) "false")
3147 (setcar (nthcdr 5 var) 'out-of-scope)
3148 (setcar (nthcdr 5 var) 'changed)
3149 (setcar (nthcdr 4 var)
3150 (read (match-string 2))))
3151 (throw 'var-found nil))))))
3152 (setq gdb-pending-triggers
3153 (delq 'gdb-var-update gdb-pending-triggers))
3154 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3155 ;; dummy command to update speedbar at right time
3156 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
3157 ;; keep gdb-pending-triggers non-nil till end
3158 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
3160 ;; Registers buffer.
3162 (gdb-set-buffer-rules 'gdb-registers-buffer
3163 'gdb-registers-buffer-name
3164 'gdb-registers-mode)
3166 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3167 (gdb-get-buffer 'gdb-registers-buffer)
3168 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3169 "server interpreter mi \"-data-list-register-values x\"\n"
3170 "-data-list-register-values x\n")
3171 gdb-data-list-register-values-handler)
3173 (defconst gdb-data-list-register-values-regexp
3174 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
3176 (defun gdb-data-list-register-values-handler ()
3177 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3178 gdb-pending-triggers))
3179 (goto-char (point-min))
3180 (if (re-search-forward gdb-error-regexp nil t)
3181 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3182 (let ((buffer-read-only nil))
3184 (insert (match-string 1))
3185 (goto-char (point-min))))
3186 (let ((register-list (reverse gdb-register-names))
3187 (register nil) (register-string nil) (register-values nil))
3188 (goto-char (point-min))
3189 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3190 (setq register (pop register-list))
3191 (setq register-string (concat register "\t" (match-string 2) "\n"))
3192 (if (member (match-string 1) gdb-changed-registers)
3193 (put-text-property 0 (length register-string)
3194 'face 'font-lock-warning-face
3196 (setq register-values
3197 (concat register-values register-string)))
3198 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3199 (with-current-buffer buf
3200 (let* ((window (get-buffer-window buf 0))
3201 (start (window-start window))
3202 (p (window-point window))
3203 (buffer-read-only nil))
3205 (insert register-values)
3206 (set-window-start window start)
3207 (set-window-point window p))))))
3208 (gdb-data-list-register-values-custom))
3210 (defun gdb-data-list-register-values-custom ()
3211 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3213 (let ((buffer-read-only nil)
3215 (goto-char (point-min))
3216 (while (< (point) (point-max))
3217 (setq start (line-beginning-position))
3218 (setq end (line-end-position))
3219 (when (looking-at "^[^\t]+")
3220 (unless (string-equal (match-string 0) "No registers.")
3221 (put-text-property start (match-end 0)
3222 'face font-lock-variable-name-face)
3223 (add-text-properties start end
3224 '(help-echo "mouse-2: edit value"
3225 mouse-face highlight))))
3226 (forward-line 1))))))
3228 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3229 (defun gdb-get-changed-registers ()
3230 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3231 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3235 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3236 "server interpreter mi -data-list-changed-registers\n"
3237 "-data-list-changed-registers\n")
3238 'gdb-get-changed-registers-handler))
3239 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3241 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3243 (defun gdb-get-changed-registers-handler ()
3244 (setq gdb-pending-triggers
3245 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3246 (setq gdb-changed-registers nil)
3247 (goto-char (point-min))
3248 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3249 (push (match-string 1) gdb-changed-registers)))
3254 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3255 (gdb-set-buffer-rules 'gdb-locals-buffer
3256 'gdb-locals-buffer-name
3259 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3260 (gdb-get-buffer 'gdb-locals-buffer)
3261 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3262 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3263 "-stack-list-locals --simple-values\n")
3264 gdb-stack-list-locals-handler)
3266 (defconst gdb-stack-list-locals-regexp
3267 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3269 (defvar gdb-locals-watch-map-1
3270 (let ((map (make-sparse-keymap)))
3271 (define-key map [mouse-2] 'gud-watch)
3273 "Keymap to create watch expression of a complex data type local variable.")
3275 ;; Dont display values of arrays or structures.
3276 ;; These can be expanded using gud-watch.
3277 (defun gdb-stack-list-locals-handler ()
3278 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3279 gdb-pending-triggers))
3280 (let (local locals-list)
3281 (goto-char (point-min))
3282 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3283 (let ((local (list (match-string 1)
3286 (if (looking-at ",value=\\(\".*\"\\)}")
3287 (setcar (nthcdr 2 local) (read (match-string 1))))
3288 (push local locals-list)))
3289 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3290 (and buf (with-current-buffer buf
3291 (let* ((window (get-buffer-window buf 0))
3292 (start (window-start window))
3293 (p (window-point window))
3294 (buffer-read-only nil))
3296 (dolist (local locals-list)
3297 (setq name (car local))
3298 (if (or (not (nth 2 local))
3299 (string-match "\\*$" (nth 1 local)))
3300 (add-text-properties 0 (length name)
3301 `(mouse-face highlight
3302 help-echo "mouse-2: create watch expression"
3303 local-map ,gdb-locals-watch-map-1)
3306 (concat name "\t" (nth 1 local)
3307 "\t" (nth 2 local) "\n")))
3308 (set-window-start window start)
3309 (set-window-point window p)))))))
3311 (defun gdb-get-register-names ()
3312 "Create a list of register names."
3313 (goto-char (point-min))
3314 (setq gdb-register-names nil)
3315 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3316 (push (match-string 1) gdb-register-names)))
3320 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3321 ;;; gdb-ui.el ends here