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) M-x gdb doesn't work with "run" command in .gdbinit, use M-x gdba instead.
77 ;; 4) M-x gdb doesn't work if the corefile is specified in the command in the
78 ;; minibuffer, use M-x gdba instead (or specify the core in the GUD buffer).
79 ;; 5) If you wish to call procedures from your program in GDB
80 ;; e.g "call myproc ()", "p mysquare (5)" then use level 2 annotations
81 ;; "gdb --annotate=2 myprog" to keep source buffer/selected frame fixed.
83 ;;; Problems with watch expressions, GDB/MI:
84 ;; 1) They go out of scope when the inferior is re-run.
85 ;; 2) -stack-list-locals has a type field but also prints type in values field.
86 ;; 3) VARNUM increments even when variable object is not created
90 ;; 1) Use MI command -data-read-memory for memory window.
91 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
92 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
99 (defvar speedbar-initial-expansion-list-name)
101 (defvar gdb-pc-address nil "Initialization for Assembler buffer.
102 Set to \"main\" at start if gdb-show-main is t.")
103 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
104 (defvar gdb-previous-frame-address nil)
105 (defvar gdb-memory-address "main")
106 (defvar gdb-previous-frame nil)
107 (defvar gdb-selected-frame nil)
108 (defvar gdb-frame-number nil)
109 (defvar gdb-current-language nil)
110 (defvar gdb-var-list nil
111 "List of variables in watch window.
112 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS FP)
113 where STATUS is nil (unchanged), `changed' or `out-of-scope', FP the frame
114 address for root variables.")
115 (defvar gdb-main-file nil "Source file from which program execution begins.")
116 (defvar gud-old-arrow nil)
117 (defvar gdb-overlay-arrow-position nil)
118 (defvar gdb-server-prefix nil)
119 (defvar gdb-flush-pending-output nil)
120 (defvar gdb-location-alist nil
121 "Alist of breakpoint numbers and full filenames.")
122 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
123 and #define directives otherwise.")
124 (defvar gdb-error "Non-nil when GDB is reporting an error.")
125 (defvar gdb-macro-info nil
126 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
127 (defvar gdb-buffer-fringe-width nil)
128 (defvar gdb-signalled nil)
129 (defvar gdb-source-window nil)
130 (defvar gdb-inferior-status nil)
131 (defvar gdb-continuation nil)
132 (defvar gdb-look-up-stack nil)
133 (defvar gdb-frame-begin nil
134 "Non-nil when GDB generates frame-begin annotation.")
135 (defvar gdb-printing t)
137 (defvar gdb-buffer-type nil
138 "One of the symbols bound in `gdb-buffer-rules'.")
139 (make-variable-buffer-local 'gdb-buffer-type)
141 (defvar gdb-input-queue ()
142 "A list of gdb command objects.")
144 (defvar gdb-prompting nil
145 "True when gdb is idle with no pending input.")
147 (defvar gdb-output-sink 'user
148 "The disposition of the output of the current gdb command.
149 Possible values are these symbols:
151 `user' -- gdb output should be copied to the GUD buffer
154 `inferior' -- gdb output should be copied to the inferior-io buffer.
156 `pre-emacs' -- output should be ignored util the post-prompt
157 annotation is received. Then the output-sink
159 `emacs' -- output should be collected in the partial-output-buffer
160 for subsequent processing by a command. This is the
161 disposition of output generated by commands that
162 gdb mode sends to gdb on its own behalf.
163 `post-emacs' -- ignore output until the prompt annotation is
164 received, then go to USER disposition.
166 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
167 \(`user' and `emacs').")
169 (defvar gdb-current-item nil
170 "The most recent command item sent to gdb.")
172 (defvar gdb-pending-triggers '()
173 "A list of trigger functions that have run later than their output
176 (defvar gdb-first-post-prompt nil)
177 (defvar gdb-version nil)
178 (defvar gdb-locals-font-lock-keywords nil)
179 (defvar gdb-source-file-list nil
180 "List of source files for the current executable")
181 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
183 (defvar gdb-locals-font-lock-keywords-1
185 ;; var = (struct struct_tag) value
186 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
187 (1 font-lock-variable-name-face)
188 (3 font-lock-keyword-face)
189 (4 font-lock-type-face))
190 ;; var = (type) value
191 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
192 (1 font-lock-variable-name-face)
193 (3 font-lock-type-face))
195 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
196 (1 font-lock-variable-name-face))
198 "Font lock keywords used in `gdb-local-mode'.")
200 (defvar gdb-locals-font-lock-keywords-2
203 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
204 (1 font-lock-variable-name-face)
205 (3 font-lock-type-face))
207 "Font lock keywords used in `gdb-local-mode'.")
209 ;; Variables for GDB 6.4+
210 (defvar gdb-register-names nil "List of register names.")
211 (defvar gdb-changed-registers nil
212 "List of changed register numbers (strings).")
215 (defun gdba (command-line)
216 "Run gdb on program FILE in buffer *gud-FILE*.
217 The directory containing FILE becomes the initial working directory
218 and source-file directory for your debugger.
220 If `gdb-many-windows' is nil (the default value) then gdb just
221 pops up the GUD buffer unless `gdb-show-main' is t. In this case
222 it starts with two windows: one displaying the GUD buffer and the
223 other with the source file with the main routine of the inferior.
225 If `gdb-many-windows' is t, regardless of the value of
226 `gdb-show-main', the layout below will appear unless
227 `gdb-use-separate-io-buffer' is nil when the source buffer
228 occupies the full width of the frame. Keybindings are shown in
231 Watch expressions appear in the speedbar/slowbar.
233 The following commands help control operation :
235 `gdb-many-windows' - Toggle the number of windows gdb uses.
236 `gdb-restore-windows' - To restore the window layout.
238 See Info node `(emacs)GDB Graphical Interface' for a more
239 detailed description of this mode.
242 +----------------------------------------------------------------------+
244 +-----------------------------------+----------------------------------+
245 | GUD buffer (I/O of GDB) | Locals buffer |
249 +-----------------------------------+----------------------------------+
250 | Source buffer | I/O buffer (of debugged program) |
258 +-----------------------------------+----------------------------------+
259 | Stack buffer | Breakpoints buffer |
260 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
261 | | RET gdb-goto-breakpoint |
262 | | D gdb-delete-breakpoint |
263 +-----------------------------------+----------------------------------+"
265 (interactive (list (gud-query-cmdline 'gdba)))
267 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
271 (defcustom gdb-debug-ring-max 128
272 "Maximum size of `gdb-debug-ring'."
277 (defvar gdb-debug-ring nil
278 "List of commands, most recent first, sent to and replies received from GDB.
279 This variable is used to debug GDB-UI.")
282 (defcustom gdb-enable-debug nil
283 "Non-nil means record the process input and output in `gdb-debug-ring'."
288 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
289 "Shell command for generating a list of defined macros in a source file.
290 This list is used to display the #define directive associated
291 with an identifier as a tooltip. It works in a debug session with
292 GDB, when gud-tooltip-mode is t.
294 Set `gdb-cpp-define-alist-flags' for any include paths or
300 (defcustom gdb-cpp-define-alist-flags ""
301 "Preprocessor flags for `gdb-cpp-define-alist-program'."
306 (defcustom gdb-show-main nil
307 "Non-nil means display source file containing the main routine at startup.
308 Also display the main routine in the disassembly buffer if present."
313 (defcustom gdb-many-windows nil
314 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
315 In this case it starts with two windows: one displaying the GUD
316 buffer and the other with the source file with the main routine
317 of the inferior. Non-nil means display the layout shown for
323 (defcustom gdb-use-separate-io-buffer nil
324 "Non-nil means display output from the inferior in a separate buffer."
329 (defun gdb-force-mode-line-update (status)
330 (let ((buffer gud-comint-buffer))
331 (if (and buffer (buffer-name buffer))
332 (with-current-buffer buffer
333 (setq mode-line-process
335 (process-status (get-buffer-process buffer)) status))
336 ;; Force mode line redisplay soon.
337 (force-mode-line-update)))))
339 (defun gdb-many-windows (arg)
340 "Toggle the number of windows in the basic arrangement.
341 With arg, display additional buffers iff arg is positive."
343 (setq gdb-many-windows
345 (not gdb-many-windows)
346 (> (prefix-numeric-value arg) 0)))
347 (message (format "Display of other windows %sabled"
348 (if gdb-many-windows "en" "dis")))
349 (if (and gud-comint-buffer
350 (buffer-name gud-comint-buffer))
352 (gdb-restore-windows)
355 (defun gdb-use-separate-io-buffer (arg)
356 "Toggle separate IO for inferior.
357 With arg, use separate IO iff arg is positive."
359 (setq gdb-use-separate-io-buffer
361 (not gdb-use-separate-io-buffer)
362 (> (prefix-numeric-value arg) 0)))
363 (message (format "Separate inferior IO %sabled"
364 (if gdb-use-separate-io-buffer "en" "dis")))
365 (if (and gud-comint-buffer
366 (buffer-name gud-comint-buffer))
368 (if gdb-use-separate-io-buffer
369 (if gdb-many-windows (gdb-restore-windows))
370 (kill-buffer (gdb-inferior-io-name)))
373 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
375 (defun gdb-create-define-alist ()
376 "Create an alist of #define directives for GUD tooltips."
377 (let* ((file (buffer-file-name))
379 (with-output-to-string
380 (with-current-buffer standard-output
381 (call-process shell-file-name
382 (if (file-exists-p file) file nil)
383 (list t nil) nil "-c"
384 (concat gdb-cpp-define-alist-program " "
385 gdb-cpp-define-alist-flags)))))
386 (define-list (split-string output "\n" t))
388 (setq gdb-define-alist nil)
389 (dolist (define define-list)
390 (setq name (nth 1 (split-string define "[( ]")))
391 (push (cons name define) gdb-define-alist))))
393 (defun gdb-tooltip-print (expr)
395 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
396 (goto-char (point-min))
398 (if (search-forward "=" nil t)
399 (concat expr (buffer-substring (- (point) 2) (point-max)))
401 ;; remove newline for gud-tooltip-echo-area
402 (substring string 0 (- (length string) 1))))
403 (or gud-tooltip-echo-area tooltip-use-echo-area)))
405 ;; If expr is a macro for a function don't print because of possible dangerous
406 ;; side-effects. Also printing a function within a tooltip generates an
407 ;; unexpected starting annotation (phase error).
408 (defun gdb-tooltip-print-1 (expr)
409 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
410 (goto-char (point-min))
411 (if (search-forward "expands to: " nil t)
412 (unless (looking-at "\\S-+.*(.*).*")
414 (list (concat gdb-server-prefix "print " expr "\n")
415 `(lambda () (gdb-tooltip-print ,expr))))))))
417 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
419 (defun gdb-set-gud-minor-mode-existing-buffers ()
420 "Create list of source files for current GDB session."
421 (goto-char (point-min))
422 (when (search-forward "read in on demand:" nil t)
423 (while (re-search-forward gdb-source-file-regexp nil t)
424 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
425 gdb-source-file-list))
426 (dolist (buffer (buffer-list))
427 (with-current-buffer buffer
428 (when (and buffer-file-name
429 (member (file-name-nondirectory buffer-file-name)
430 gdb-source-file-list))
431 (set (make-local-variable 'gud-minor-mode) 'gdba)
432 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
433 (when gud-tooltip-mode
434 (make-local-variable 'gdb-define-alist)
435 (gdb-create-define-alist)
436 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
437 (gdb-force-mode-line-update
438 (propertize "ready" 'face font-lock-variable-name-face)))
440 (defun gdb-find-watch-expression ()
441 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
442 (varnum (car var)) expr array)
443 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
444 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
445 (component-list (split-string (match-string 2 varnum) "\\." t)))
446 (setq expr (nth 1 var1))
447 (setq varnumlet (car var1))
448 (dolist (component component-list)
449 (setq var2 (assoc varnumlet gdb-var-list))
450 (setq expr (concat expr
451 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
452 (concat "[" component "]")
453 (concat "." component))))
454 (setq varnumlet (concat varnumlet "." component)))
458 (set (make-local-variable 'gud-minor-mode) 'gdba)
459 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
461 (gud-def gud-break (if (not (string-match "Machine" mode-name))
462 (gud-call "break %f:%l" arg)
466 (gud-call "break *%a" arg)))
467 "\C-b" "Set breakpoint at current line or address.")
469 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
470 (gud-call "clear %f:%l" arg)
474 (gud-call "clear *%a" arg)))
475 "\C-d" "Remove breakpoint at current line or address.")
477 (gud-def gud-until (if (not (string-match "Machine" mode-name))
478 (gud-call "until %f:%l" arg)
482 (gud-call "until *%a" arg)))
483 "\C-u" "Continue to current line or address.")
485 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
486 nil "Start or continue execution.")
488 ;; For debugging Emacs only.
492 "pp1 " (if (eq (buffer-local-value
493 'major-mode (window-buffer)) 'speedbar-mode)
494 (gdb-find-watch-expression) "%e")) arg)
495 nil "Print the emacs s-expression.")
497 (define-key gud-minor-mode-map [left-margin mouse-1]
498 'gdb-mouse-set-clear-breakpoint)
499 (define-key gud-minor-mode-map [left-fringe mouse-1]
500 'gdb-mouse-set-clear-breakpoint)
501 (define-key gud-minor-mode-map [left-margin C-mouse-1]
502 'gdb-mouse-toggle-breakpoint-margin)
503 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
504 'gdb-mouse-toggle-breakpoint-fringe)
506 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
508 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
510 (define-key gud-minor-mode-map [left-margin mouse-3]
512 (define-key gud-minor-mode-map [left-fringe mouse-3]
515 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
517 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
519 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
521 (define-key gud-minor-mode-map [left-margin C-mouse-3]
524 (setq comint-input-sender 'gdb-send)
527 (setq gdb-pc-address (if gdb-show-main "main" nil))
528 (setq gdb-previous-frame-address nil
529 gdb-memory-address "main"
530 gdb-previous-frame nil
531 gdb-selected-frame nil
532 gdb-current-language nil
536 gdb-first-post-prompt t
540 gdb-pending-triggers nil
541 gdb-output-sink 'user
542 gdb-server-prefix "server "
543 gdb-flush-pending-output nil
544 gdb-location-alist nil
545 gdb-source-file-list nil
548 gdb-buffer-fringe-width (car (window-fringes))
551 gdb-source-window nil
552 gdb-inferior-status nil
554 gdb-look-up-stack nil
559 (setq gdb-buffer-type 'gdba)
561 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
563 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
564 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
568 (if (eq window-system 'w32)
569 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
570 (gdb-enqueue-input (list "set height 0\n" 'ignore))
571 (gdb-enqueue-input (list "set width 0\n" 'ignore))
573 (if (string-equal gdb-version "pre-6.4")
575 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
576 'gdb-set-gud-minor-mode-existing-buffers))
577 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
579 (list "server interpreter mi -data-list-register-names\n"
580 'gdb-get-register-names))
581 ; Needs GDB 6.2 onwards.
583 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
584 'gdb-set-gud-minor-mode-existing-buffers-1))
585 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
587 ;; Find source file and compilation directory here.
588 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
589 (gdb-enqueue-input (list "server list\n" 'ignore))
590 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
592 (run-hooks 'gdba-mode-hook))
594 (defun gdb-get-version ()
595 (goto-char (point-min))
596 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
597 (setq gdb-version "pre-6.4")
598 (setq gdb-version "6.4+"))
601 (defun gdb-mouse-until (event)
602 "Continue running until a source line past the current line.
603 The destination source line can be selected either by clicking with mouse-2
604 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
606 (if gud-overlay-arrow-position
607 (let ((start (event-start event))
608 (end (event-end event))
609 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
610 (if (not (string-match "Machine" mode-name))
611 (if (equal buffer (window-buffer (posn-window end)))
612 (with-current-buffer buffer
613 (when (or (equal start end)
614 (equal (posn-point start)
616 gud-overlay-arrow-position)))
617 (setq line (line-number-at-pos (posn-point end)))
618 (gud-call (concat "until " (number-to-string line))))))
619 (if (equal (marker-buffer gdb-overlay-arrow-position)
620 (window-buffer (posn-window end)))
621 (when (or (equal start end)
622 (equal (posn-point start)
624 gdb-overlay-arrow-position)))
626 (goto-line (line-number-at-pos (posn-point end)))
628 (gud-call (concat "until *%a")))))))))
630 (defun gdb-mouse-jump (event)
631 "Set execution address/line.
632 The destination source line can be selected either by clicking with mouse-2
633 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
634 Unlike gdb-mouse-until the destination address can be before the current
635 line, and no execution takes place."
637 (if gud-overlay-arrow-position
638 (let ((start (event-start event))
639 (end (event-end event))
640 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
641 (if (not (string-match "Machine" mode-name))
642 (if (equal buffer (window-buffer (posn-window end)))
643 (with-current-buffer buffer
644 (when (or (equal start end)
645 (equal (posn-point start)
647 gud-overlay-arrow-position)))
648 (setq line (line-number-at-pos (posn-point end)))
649 (progn (gud-call (concat "tbreak " (number-to-string line)))
650 (gud-call (concat "jump " (number-to-string line)))))))
651 (if (equal (marker-buffer gdb-overlay-arrow-position)
652 (window-buffer (posn-window end)))
653 (when (or (equal start end)
654 (equal (posn-point start)
656 gdb-overlay-arrow-position)))
658 (goto-line (line-number-at-pos (posn-point end)))
661 (gud-call (concat "tbreak *%a"))
662 (gud-call (concat "jump *%a"))))))))))
664 (defcustom gdb-speedbar-auto-raise nil
665 "If non-nil raise speedbar every time display of watch expressions is\
671 (defun gdb-speedbar-auto-raise (arg)
672 "Toggle automatic raising of the speedbar for watch expressions.
673 With arg, automatically raise speedbar iff arg is positive."
675 (setq gdb-speedbar-auto-raise
677 (not gdb-speedbar-auto-raise)
678 (> (prefix-numeric-value arg) 0)))
679 (message (format "Auto raising %sabled"
680 (if gdb-speedbar-auto-raise "en" "dis"))))
682 (defcustom gdb-use-colon-colon-notation nil
683 "If non-nil use FUN::VAR format to display variables in the speedbar."
688 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
689 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
691 (defun gud-watch (&optional arg event)
692 "Watch expression at point.
693 With arg, enter name of variable to be watched in the minibuffer."
694 (interactive (list current-prefix-arg last-input-event))
695 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
696 (if (memq minor-mode '(gdbmi gdba))
698 (if event (posn-set-point (event-end event)))
700 (save-selected-window
703 (completing-read "Name of variable: "
704 'gud-gdb-complete-command)
705 (if (and transient-mark-mode mark-active)
706 (buffer-substring (region-beginning) (region-end))
707 (tooltip-identifier-from-point (point))))))
709 (catch 'already-watched
710 (dolist (var gdb-var-list)
711 (unless (string-match "\\." (car var))
712 (if (string-equal expr (nth 1 var))
713 (throw 'already-watched nil))))
714 (set-text-properties 0 (length expr) nil expr)
717 (if (eq minor-mode 'gdba)
719 "server interpreter mi \"-var-create - * " expr "\"\n")
720 (concat"-var-create - * " expr "\n"))
721 `(lambda () (gdb-var-create-handler ,expr))))))))
722 (message "gud-watch is a no-op in this mode."))))
724 (defconst gdb-var-create-regexp
725 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",.*type=\"\\(.*?\\)\"")
727 (defun gdb-var-create-handler (expr)
728 (goto-char (point-min))
729 (if (re-search-forward gdb-var-create-regexp nil t)
732 (if (and (string-equal gdb-current-language "c")
733 gdb-use-colon-colon-notation gdb-selected-frame)
734 (setq expr (concat gdb-selected-frame "::" expr))
738 nil nil gdb-frame-address)))
739 (push var gdb-var-list)
740 (unless (string-equal
741 speedbar-initial-expansion-list-name "GUD")
742 (speedbar-change-initial-expansion-list "GUD"))
745 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
746 (concat "server interpreter mi \"0-var-evaluate-expression "
748 (concat "0-var-evaluate-expression " (car var) "\n"))
749 `(lambda () (gdb-var-evaluate-expression-handler
751 (if (search-forward "Undefined command" nil t)
752 (message-box "Watching expressions requires GDB 6.0 onwards")
753 (message-box "No symbol \"%s\" in current context." expr))))
755 (defun gdb-speedbar-update ()
756 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
757 ;; Dummy command to update speedbar even when idle.
758 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
759 ;; Keep gdb-pending-triggers non-nil till end.
760 (push 'gdb-speedbar-timer gdb-pending-triggers)))
762 (defun gdb-speedbar-timer-fn ()
763 (setq gdb-pending-triggers
764 (delq 'gdb-speedbar-timer gdb-pending-triggers))
767 (defun gdb-var-evaluate-expression-handler (varnum changed)
768 (goto-char (point-min))
769 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t)
770 (setq gdb-pending-triggers
771 (delq (string-to-number (match-string 1)) gdb-pending-triggers))
772 (let ((var (assoc varnum gdb-var-list)))
774 (if changed (setcar (nthcdr 5 var) 'changed))
775 (setcar (nthcdr 4 var) (read (match-string 2)))))
776 (gdb-speedbar-update))
778 (defun gdb-var-list-children (varnum)
780 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
781 `(lambda () (gdb-var-list-children-handler ,varnum)))))
783 (defconst gdb-var-list-children-regexp
784 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
785 numchild=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\".*?}")
787 (defun gdb-var-list-children-handler (varnum)
788 (goto-char (point-min))
789 (let ((var-list nil))
790 (catch 'child-already-watched
791 (dolist (var gdb-var-list)
792 (if (string-equal varnum (car var))
795 (while (re-search-forward gdb-var-list-children-regexp nil t)
796 (let ((varchild (list (match-string 1)
801 (if (assoc (car varchild) gdb-var-list)
802 (throw 'child-already-watched nil))
803 (push varchild var-list)
807 "server interpreter mi \"0-var-evaluate-expression "
808 (car varchild) "\"\n")
809 `(lambda () (gdb-var-evaluate-expression-handler
810 ,(car varchild) nil)))))))
811 (push var var-list)))
812 (setq gdb-var-list (nreverse var-list)))))
814 (defun gdb-var-update ()
815 (when (not (member 'gdb-var-update gdb-pending-triggers))
817 (list "server interpreter mi \"-var-update *\"\n"
818 'gdb-var-update-handler))
819 (push 'gdb-var-update gdb-pending-triggers)))
821 (defconst gdb-var-update-regexp
822 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
823 type_changed=\".*?\".*?}")
825 (defun gdb-var-update-handler ()
826 (dolist (var gdb-var-list)
827 (setcar (nthcdr 5 var) nil))
828 (goto-char (point-min))
830 (while (re-search-forward gdb-var-update-regexp nil t)
831 (let ((varnum (match-string 1)))
832 (if (string-equal (match-string 2) "false")
833 (let ((var (assoc varnum gdb-var-list)))
834 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
836 (push n gdb-pending-triggers)
839 (concat "server interpreter mi \"" (number-to-string n)
840 "-var-evaluate-expression " varnum "\"\n")
841 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))))))
842 (setq gdb-pending-triggers
843 (delq 'gdb-var-update gdb-pending-triggers)))
845 (defun gdb-var-delete ()
846 "Delete watch expression at point from the speedbar."
848 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
850 (let ((text (speedbar-line-text)))
851 ;; Can't use \\S-+ for whitespace because
852 ;; speedbar has a whacky syntax table.
853 (string-match "\\([^ \t]+\\)" text)
854 (let ((expr (match-string 1 text)) var varnum)
856 (dolist (var1 gdb-var-list)
857 (when (string-equal expr (nth 1 var1))
859 (setq varnum (car var1))
860 (throw 'expr-found nil))))
861 (unless (string-match "\\." (car var))
864 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
866 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
867 (concat "-var-delete " varnum "\n"))
869 (setq gdb-var-list (delq var gdb-var-list))
870 (dolist (varchild gdb-var-list)
871 (if (string-match (concat (car var) "\\.") (car varchild))
872 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
874 (defun gdb-edit-value (text token indent)
875 "Assign a value to a variable displayed in the speedbar."
876 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
877 (varnum (car var)) (value))
878 (setq value (read-string "New value: "))
881 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
882 (concat "server interpreter mi \"-var-assign "
883 varnum " " value "\"\n")
884 (concat "-var-assign " varnum " " value "\n"))
885 `(lambda () (gdb-edit-value-handler ,value))))))
887 (defun gdb-edit-value-handler (value)
888 (goto-char (point-min))
889 (if (re-search-forward gdb-error-regexp nil t)
890 (message-box "Invalid number or expression (%s)" value)))
892 (defcustom gdb-show-changed-values t
893 "If non-nil change the face of out of scope variables and changed values.
894 Out of scope variables are suppressed with `shadow' face.
895 Changed values are highlighted with the face `font-lock-warning-face'."
900 (defun gdb-speedbar-expand-node (text token indent)
901 "Expand the node the user clicked on.
902 TEXT is the text of the button we clicked on, a + or - item.
903 TOKEN is data related to this node.
904 INDENT is the current indentation depth."
905 (cond ((string-match "+" text) ;expand this node
907 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
908 (string-equal gdb-version "pre-6.4"))
909 (gdb-var-list-children token)
910 (gdb-var-list-children-1 token)))
911 ((string-match "-" text) ;contract this node
912 (dolist (var gdb-var-list)
913 (if (string-match (concat token "\\.") (car var))
914 (setq gdb-var-list (delq var gdb-var-list))))
915 (speedbar-change-expand-button-char ?+)
916 (speedbar-delete-subblock indent))
917 (t (error "Ooops... not sure what to do")))
918 (speedbar-center-buffer-smartly))
920 (defun gdb-get-target-string ()
921 (with-current-buffer gud-comint-buffer
928 ;; Each buffer has a TYPE -- a symbol that identifies the function
929 ;; of that particular buffer.
931 ;; The usual gdb interaction buffer is given the type `gdba' and
932 ;; is constructed specially.
934 ;; Others are constructed by gdb-get-buffer-create and
935 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
937 (defvar gdb-buffer-rules-assoc '())
939 (defun gdb-get-buffer (key)
940 "Return the gdb buffer tagged with type KEY.
941 The key should be one of the cars in `gdb-buffer-rules-assoc'."
943 (gdb-look-for-tagged-buffer key (buffer-list))))
945 (defun gdb-get-buffer-create (key)
946 "Create a new gdb buffer of the type specified by KEY.
947 The key should be one of the cars in `gdb-buffer-rules-assoc'."
948 (or (gdb-get-buffer key)
949 (let* ((rules (assoc key gdb-buffer-rules-assoc))
950 (name (funcall (gdb-rules-name-maker rules)))
951 (new (get-buffer-create name)))
952 (with-current-buffer new
954 (if (cdr (cdr rules))
955 (setq trigger (funcall (car (cdr (cdr rules))))))
956 (setq gdb-buffer-type key)
957 (set (make-local-variable 'gud-minor-mode)
958 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
959 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
960 (if trigger (funcall trigger)))
963 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
965 (defun gdb-look-for-tagged-buffer (key bufs)
967 (while (and (not retval) bufs)
968 (set-buffer (car bufs))
969 (if (eq gdb-buffer-type key)
970 (setq retval (car bufs)))
971 (setq bufs (cdr bufs)))
975 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
976 ;; at least one and possible more functions. The functions have these
977 ;; roles in defining a buffer type:
979 ;; NAME - Return a name for this buffer type.
981 ;; The remaining function(s) are optional:
983 ;; MODE - called in a new buffer with no arguments, should establish
984 ;; the proper mode for the buffer.
987 (defun gdb-set-buffer-rules (buffer-type &rest rules)
988 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
990 (setcdr binding rules)
991 (push (cons buffer-type rules)
992 gdb-buffer-rules-assoc))))
994 ;; GUD buffers are an exception to the rules
995 (gdb-set-buffer-rules 'gdba 'error)
997 ;; Partial-output buffer : This accumulates output from a command executed on
998 ;; behalf of emacs (rather than the user).
1000 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1001 'gdb-partial-output-name)
1003 (defun gdb-partial-output-name ()
1004 (concat "*partial-output-"
1005 (gdb-get-target-string)
1009 (gdb-set-buffer-rules 'gdb-inferior-io
1010 'gdb-inferior-io-name
1011 'gdb-inferior-io-mode)
1013 (defun gdb-inferior-io-name ()
1014 (concat "*input/output of "
1015 (gdb-get-target-string)
1018 (defun gdb-display-separate-io-buffer ()
1019 "Display IO of debugged program in a separate window."
1021 (if gdb-use-separate-io-buffer
1023 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1025 (defconst gdb-frame-parameters
1026 '((height . 14) (width . 80)
1028 (tool-bar-lines . nil)
1029 (menu-bar-lines . nil)
1030 (minibuffer . nil)))
1032 (defun gdb-frame-separate-io-buffer ()
1033 "Display IO of inferior in a new frame."
1035 (if gdb-use-separate-io-buffer
1036 (let ((special-display-regexps (append special-display-regexps '(".*")))
1037 (special-display-frame-alist gdb-frame-parameters))
1038 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1040 (defvar gdb-inferior-io-mode-map
1041 (let ((map (make-sparse-keymap)))
1042 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1043 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1044 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1045 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1046 (define-key map "\C-d" 'gdb-separate-io-eof)
1049 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1050 "Major mode for gdb inferior-io."
1051 :syntax-table nil :abbrev-table nil
1052 ;; We want to use comint because it has various nifty and familiar
1053 ;; features. We don't need a process, but comint wants one, so create
1055 (make-comint-in-buffer
1056 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1057 (current-buffer) "hexl")
1058 (setq comint-input-sender 'gdb-inferior-io-sender))
1060 (defun gdb-inferior-io-sender (proc string)
1061 ;; PROC is the pseudo-process created to satisfy comint.
1062 (with-current-buffer (process-buffer proc)
1063 (setq proc (get-buffer-process gud-comint-buffer))
1064 (process-send-string proc string)
1065 (process-send-string proc "\n")))
1067 (defun gdb-separate-io-interrupt ()
1068 "Interrupt the program being debugged."
1071 (get-buffer-process gud-comint-buffer) comint-ptyp))
1073 (defun gdb-separate-io-quit ()
1074 "Send quit signal to the program being debugged."
1077 (get-buffer-process gud-comint-buffer) comint-ptyp))
1079 (defun gdb-separate-io-stop ()
1080 "Stop the program being debugged."
1083 (get-buffer-process gud-comint-buffer) comint-ptyp))
1085 (defun gdb-separate-io-eof ()
1086 "Send end-of-file to the program being debugged."
1089 (get-buffer-process gud-comint-buffer)))
1092 ;; gdb communications
1095 ;; INPUT: things sent to gdb
1097 ;; The queues are lists. Each element is either a string (indicating user or
1098 ;; user-like input) or a list of the form:
1100 ;; (INPUT-STRING HANDLER-FN)
1102 ;; The handler function will be called from the partial-output buffer when the
1103 ;; command completes. This is the way to write commands which invoke gdb
1104 ;; commands autonomously.
1106 ;; These lists are consumed tail first.
1109 (defun gdb-send (proc string)
1110 "A comint send filter for gdb.
1111 This filter may simply queue input for a later time."
1112 (with-current-buffer gud-comint-buffer
1113 (let ((inhibit-read-only t))
1114 (remove-text-properties (point-min) (point-max) '(face))))
1117 (let ((item (concat string "\n")))
1118 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1119 (process-send-string proc item)))
1120 (if (and (string-match "\\\\$" string)
1121 (not comint-input-sender-no-newline)) ;;Try to catch C-d.
1122 (setq gdb-continuation (concat gdb-continuation string "\n"))
1123 (let ((item (concat gdb-continuation string "\n")))
1124 (gdb-enqueue-input item)
1125 (setq gdb-continuation nil)))))
1127 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1128 ;; is a query, or other non-top-level prompt.
1130 (defun gdb-enqueue-input (item)
1131 (if (not gud-running)
1134 (gdb-send-item item)
1135 (setq gdb-prompting nil))
1136 (push item gdb-input-queue))))
1138 (defun gdb-dequeue-input ()
1139 (let ((queue gdb-input-queue))
1141 (let ((last (car (last queue))))
1142 (unless (nbutlast queue) (setq gdb-input-queue '()))
1145 (defun gdb-send-item (item)
1146 (setq gdb-flush-pending-output nil)
1147 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1148 (setq gdb-current-item item)
1149 (let ((process (get-buffer-process gud-comint-buffer)))
1150 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1153 (setq gdb-output-sink 'user)
1154 (process-send-string process item))
1156 (gdb-clear-partial-output)
1157 (setq gdb-output-sink 'pre-emacs)
1158 (process-send-string process
1160 ;; case: eq gud-minor-mode 'gdbmi
1161 (gdb-clear-partial-output)
1162 (setq gdb-output-sink 'emacs)
1163 (process-send-string process (car item)))))
1166 ;; output -- things gdb prints to emacs
1168 ;; GDB output is a stream interrupted by annotations.
1169 ;; Annotations can be recognized by their beginning
1170 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1172 ;; The tag is a string obeying symbol syntax.
1174 ;; The optional part `<opt>' can be either the empty string
1175 ;; or a space followed by more data relating to the annotation.
1176 ;; For example, the SOURCE annotation is followed by a filename,
1177 ;; line number and various useless goo. This data must not include
1181 (defcustom gud-gdba-command-name "gdb -annotate=3"
1182 "Default command to execute an executable under the GDB-UI debugger."
1187 (defvar gdb-annotation-rules
1188 '(("pre-prompt" gdb-pre-prompt)
1189 ("prompt" gdb-prompt)
1190 ("commands" gdb-subprompt)
1191 ("overload-choice" gdb-subprompt)
1192 ("query" gdb-subprompt)
1193 ;; Need this prompt for GDB 6.1
1194 ("nquery" gdb-subprompt)
1195 ("prompt-for-continue" gdb-subprompt)
1196 ("post-prompt" gdb-post-prompt)
1197 ("source" gdb-source)
1198 ("starting" gdb-starting)
1199 ("exited" gdb-exited)
1200 ("signalled" gdb-signalled)
1201 ("signal" gdb-signal)
1202 ("breakpoint" gdb-stopping)
1203 ("watchpoint" gdb-stopping)
1204 ("frame-begin" gdb-frame-begin)
1205 ("stopped" gdb-stopped)
1206 ("error-begin" gdb-error)
1208 ) "An assoc mapping annotation tags to functions which process them.")
1211 (setq gdb-flush-pending-output t)
1212 (setq gud-running nil)
1213 (gdb-force-mode-line-update
1214 (propertize "stopped"'face font-lock-warning-face))
1215 (setq gdb-output-sink 'user)
1216 (setq gdb-input-queue nil)
1217 (setq gdb-pending-triggers nil)
1218 (setq gdb-prompting t))
1220 (defconst gdb-source-spec-regexp
1221 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1223 ;; Do not use this except as an annotation handler.
1224 (defun gdb-source (args)
1225 (string-match gdb-source-spec-regexp args)
1226 ;; Extract the frame position from the marker.
1227 (setq gud-last-frame
1229 (match-string 1 args)
1230 (string-to-number (match-string 2 args))))
1231 (setq gdb-pc-address (match-string 3 args))
1232 ;; cover for auto-display output which comes *before*
1233 ;; stopped annotation
1234 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1236 (defun gdb-pre-prompt (ignored)
1237 "An annotation handler for `pre-prompt'.
1238 This terminates the collection of output from a previous command if that
1239 happens to be in effect."
1240 (setq gdb-error nil)
1241 (let ((sink gdb-output-sink))
1245 (setq gdb-output-sink 'post-emacs))
1248 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1250 (defun gdb-prompt (ignored)
1251 "An annotation handler for `prompt'.
1252 This sends the next command (if any) to gdb."
1253 (when gdb-first-prompt
1254 (gdb-force-mode-line-update
1255 (propertize "initializing..." 'face font-lock-variable-name-face))
1257 (setq gdb-first-prompt nil))
1258 (let ((sink gdb-output-sink))
1261 ((eq sink 'post-emacs)
1262 (setq gdb-output-sink 'user)
1264 (car (cdr gdb-current-item))))
1265 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1266 (funcall handler))))
1269 (error "Phase error in gdb-prompt (got %s)" sink))))
1270 (let ((input (gdb-dequeue-input)))
1272 (gdb-send-item input)
1274 (setq gdb-prompting t)
1275 (gud-display-frame)))))
1277 (defun gdb-subprompt (ignored)
1278 "An annotation handler for non-top-level prompts."
1279 (setq gdb-prompting t))
1281 (defun gdb-starting (ignored)
1282 "An annotation handler for `starting'.
1283 This says that I/O for the subprocess is now the program being debugged,
1285 (setq gdb-active-process t)
1286 (setq gdb-printing t)
1287 (let ((sink gdb-output-sink))
1291 (setq gud-running t)
1292 (setq gdb-inferior-status "running")
1293 (gdb-force-mode-line-update
1294 (propertize gdb-inferior-status 'face font-lock-type-face))
1295 (gdb-remove-text-properties)
1296 (setq gud-old-arrow gud-overlay-arrow-position)
1297 (setq gud-overlay-arrow-position nil)
1298 (setq gdb-overlay-arrow-position nil)
1299 (if gdb-use-separate-io-buffer
1300 (setq gdb-output-sink 'inferior))))
1303 (error "Unexpected `starting' annotation")))))
1305 (defun gdb-signal (ignored)
1306 (setq gdb-inferior-status "signal")
1307 (gdb-force-mode-line-update
1308 (propertize gdb-inferior-status 'face font-lock-warning-face))
1309 (gdb-stopping ignored))
1311 (defun gdb-stopping (ignored)
1312 "An annotation handler for `breakpoint' and other annotations.
1313 They say that I/O for the subprocess is now GDB, not the program
1315 (if gdb-use-separate-io-buffer
1316 (let ((sink gdb-output-sink))
1318 ((eq sink 'inferior)
1319 (setq gdb-output-sink 'user))
1322 (error "Unexpected stopping annotation"))))))
1324 (defun gdb-exited (ignored)
1325 "An annotation handler for `exited' and `signalled'.
1326 They say that I/O for the subprocess is now GDB, not the program
1327 being debugged and that the program is no longer running. This
1328 function is used to change the focus of GUD tooltips to #define
1330 (setq gdb-active-process nil)
1331 (setq gud-overlay-arrow-position nil)
1332 (setq gdb-overlay-arrow-position nil)
1333 (setq gud-old-arrow nil)
1334 (setq gdb-inferior-status "exited")
1335 (gdb-force-mode-line-update
1336 (propertize gdb-inferior-status 'face font-lock-warning-face))
1337 (gdb-stopping ignored))
1339 (defun gdb-signalled (ignored)
1340 (setq gdb-signalled t))
1342 (defun gdb-frame-begin (ignored)
1343 (setq gdb-frame-begin t)
1344 (setq gdb-printing nil)
1345 (let ((sink gdb-output-sink))
1347 ((eq sink 'inferior)
1348 (setq gdb-output-sink 'user))
1350 ((eq sink 'emacs) t)
1353 (error "Unexpected frame-begin annotation (%S)" sink)))))
1355 (defcustom gdb-same-frame focus-follows-mouse
1356 "Non-nil means pop up GUD buffer in same frame."
1361 (defcustom gdb-find-source-frame nil
1362 "Non-nil means try to find a source frame further up stack e.g after signal."
1367 (defun gdb-find-source-frame (arg)
1368 "Toggle trying to find a source frame further up stack.
1369 With arg, look for a source frame further up stack iff arg is positive."
1371 (setq gdb-find-source-frame
1373 (not gdb-find-source-frame)
1374 (> (prefix-numeric-value arg) 0)))
1375 (message (format "Looking for source frame %sabled"
1376 (if gdb-find-source-frame "en" "dis"))))
1378 (defun gdb-stopped (ignored)
1379 "An annotation handler for `stopped'.
1380 It is just like `gdb-stopping', except that if we already set the output
1381 sink to `user' in `gdb-stopping', that is fine."
1382 (setq gud-running nil)
1383 (unless (or gud-overlay-arrow-position gud-last-frame)
1384 (if (and gdb-frame-begin gdb-printing)
1385 (setq gud-overlay-arrow-position gud-old-arrow)
1386 ;;Pop up GUD buffer to display current frame when it doesn't have source
1387 ;;information i.e id not compiled with -g as with libc routines generally.
1389 (gdb-display-gdb-buffer)
1390 (gdb-frame-gdb-buffer))
1391 (if gdb-find-source-frame
1392 ;;Try to find source further up stack e.g after signal.
1393 (setq gdb-look-up-stack
1394 (if (gdb-get-buffer 'gdb-stack-buffer)
1397 (gdb-get-buffer-create 'gdb-stack-buffer)
1398 (gdb-invalidate-frames)
1400 (unless (member gdb-inferior-status '("exited" "signal"))
1401 (setq gdb-inferior-status "stopped")
1402 (gdb-force-mode-line-update
1403 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1404 (let ((sink gdb-output-sink))
1406 ((eq sink 'inferior)
1407 (setq gdb-output-sink 'user))
1411 (error "Unexpected stopped annotation"))))
1412 (if gdb-signalled (gdb-exited ignored)))
1414 (defun gdb-error (ignored)
1415 (setq gdb-error (not gdb-error)))
1417 (defun gdb-post-prompt (ignored)
1418 "An annotation handler for `post-prompt'.
1419 This begins the collection of output from the current command if that
1420 happens to be appropriate."
1421 ;; Don't add to queue if there outstanding items or gdb-version is not known
1423 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1424 (gdb-get-selected-frame)
1425 (gdb-invalidate-frames)
1426 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1427 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1428 (gdb-invalidate-breakpoints)
1429 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1430 ;; so gdb-pc-address is updated.
1431 ;; (gdb-invalidate-assembler)
1433 (if (string-equal gdb-version "pre-6.4")
1434 (gdb-invalidate-registers)
1435 (gdb-get-changed-registers)
1436 (gdb-invalidate-registers-1))
1438 (gdb-invalidate-memory)
1439 (if (string-equal gdb-version "pre-6.4")
1440 (gdb-invalidate-locals)
1441 (gdb-invalidate-locals-1))
1443 (gdb-invalidate-threads)
1444 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1445 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1446 ;; Only needed/used with speedbar/watch expressions.
1447 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1448 (if (string-equal gdb-version "pre-6.4")
1450 (gdb-var-update-1)))))
1451 (setq gdb-first-post-prompt nil)
1452 (let ((sink gdb-output-sink))
1455 ((eq sink 'pre-emacs)
1456 (setq gdb-output-sink 'emacs))
1459 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1461 (defconst gdb-buffer-list
1462 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1464 (defun gdb-remove-text-properties ()
1465 (dolist (buffertype gdb-buffer-list)
1466 (let ((buffer (gdb-get-buffer buffertype)))
1468 (with-current-buffer buffer
1469 (let ((inhibit-read-only t))
1470 (remove-text-properties
1471 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1473 ;; GUD displays the selected GDB frame. This might might not be the current
1474 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1475 ;; visited breakpoint is, use that window.
1476 (defun gdb-display-source-buffer (buffer)
1477 (let* ((last-window (if gud-last-last-frame
1479 (gud-find-file (car gud-last-last-frame)))))
1480 (source-window (or last-window
1481 (if (and gdb-source-window
1482 (window-live-p gdb-source-window))
1483 gdb-source-window))))
1485 (setq gdb-source-window source-window)
1486 (set-window-buffer source-window buffer))
1489 (defun gud-gdba-marker-filter (string)
1490 "A gud marker filter for gdb. Handle a burst of output from GDB."
1491 (if gdb-flush-pending-output
1493 (when gdb-enable-debug
1494 (push (cons 'recv string) gdb-debug-ring)
1495 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1496 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1497 ;; Recall the left over gud-marker-acc from last time.
1498 (setq gud-marker-acc (concat gud-marker-acc string))
1499 ;; Start accumulating output for the GUD buffer.
1502 ;; Process all the complete markers in this chunk.
1503 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1504 (let ((annotation (match-string 1 gud-marker-acc)))
1506 ;; Stuff prior to the match is just ordinary output.
1507 ;; It is either concatenated to OUTPUT or directed
1512 (substring gud-marker-acc 0 (match-beginning 0))))
1514 ;; Take that stuff off the gud-marker-acc.
1515 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1517 ;; Parse the tag from the annotation, and maybe its arguments.
1518 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1519 (let* ((annotation-type (match-string 1 annotation))
1520 (annotation-arguments (match-string 2 annotation))
1521 (annotation-rule (assoc annotation-type
1522 gdb-annotation-rules)))
1523 ;; Call the handler for this annotation.
1525 (funcall (car (cdr annotation-rule))
1526 annotation-arguments)
1527 ;; Else the annotation is not recognized. Ignore it silently,
1528 ;; so that GDB can add new annotations without causing
1532 ;; Does the remaining text end in a partial line?
1533 ;; If it does, then keep part of the gud-marker-acc until we get more.
1534 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1537 ;; Everything before the potential marker start can be output.
1539 (gdb-concat-output output
1540 (substring gud-marker-acc 0
1541 (match-beginning 0))))
1543 ;; Everything after, we save, to combine with later input.
1544 (setq gud-marker-acc (substring gud-marker-acc
1545 (match-beginning 0))))
1547 ;; In case we know the gud-marker-acc contains no partial annotations:
1549 (setq output (gdb-concat-output output gud-marker-acc))
1550 (setq gud-marker-acc "")))
1553 (defun gdb-concat-output (so-far new)
1555 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1556 (let ((sink gdb-output-sink))
1558 ((eq sink 'user) (concat so-far new))
1559 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1561 (gdb-append-to-partial-output new)
1563 ((eq sink 'inferior)
1564 (gdb-append-to-inferior-io new)
1568 (error "Bogon output sink %S" sink)))))
1570 (defun gdb-append-to-partial-output (string)
1571 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1572 (goto-char (point-max))
1575 (defun gdb-clear-partial-output ()
1576 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1579 (defun gdb-append-to-inferior-io (string)
1580 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1581 (goto-char (point-max))
1582 (insert-before-markers string))
1583 (if (not (string-equal string ""))
1584 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1586 (defun gdb-clear-inferior-io ()
1587 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1591 ;; One trick is to have a command who's output is always available in a buffer
1592 ;; of it's own, and is always up to date. We build several buffers of this
1595 ;; There are two aspects to this: gdb has to tell us when the output for that
1596 ;; command might have changed, and we have to be able to run the command
1597 ;; behind the user's back.
1599 ;; The output phasing associated with the variable gdb-output-sink
1600 ;; help us to run commands behind the user's back.
1602 ;; Below is the code for specificly managing buffers of output from one
1606 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1607 ;; It adds an input for the command we are tracking. It should be the
1608 ;; annotation rule binding of whatever gdb sends to tell us this command
1609 ;; might have changed it's output.
1611 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1612 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1613 ;; input in the input queue (see comment about ``gdb communications'' above).
1615 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1617 `(defun ,name (&optional ignored)
1618 (if (and ,demand-predicate
1620 gdb-pending-triggers)))
1623 (list ,gdb-command ',output-handler))
1624 (push ',name gdb-pending-triggers)))))
1626 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1628 (setq gdb-pending-triggers
1630 gdb-pending-triggers))
1631 (let ((buf (gdb-get-buffer ',buf-key)))
1633 (with-current-buffer buf
1634 (let* ((window (get-buffer-window buf 0))
1635 (start (window-start window))
1636 (p (window-point window))
1637 (buffer-read-only nil))
1639 (insert-buffer-substring (gdb-get-buffer-create
1640 'gdb-partial-output-buffer))
1641 (set-window-start window start)
1642 (set-window-point window p)))))
1643 ;; put customisation here
1646 (defmacro def-gdb-auto-updated-buffer (buffer-key
1647 trigger-name gdb-command
1648 output-handler-name custom-defun)
1650 (def-gdb-auto-update-trigger ,trigger-name
1651 ;; The demand predicate:
1652 (gdb-get-buffer ',buffer-key)
1654 ,output-handler-name)
1655 (def-gdb-auto-update-handler ,output-handler-name
1656 ,trigger-name ,buffer-key ,custom-defun)))
1660 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1662 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1663 'gdb-breakpoints-buffer-name
1664 'gdb-breakpoints-mode)
1666 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1667 ;; This defines the auto update rule for buffers of type
1668 ;; `gdb-breakpoints-buffer'.
1670 ;; It defines a function to serve as the annotation handler that
1671 ;; handles the `foo-invalidated' message. That function is called:
1672 gdb-invalidate-breakpoints
1674 ;; To update the buffer, this command is sent to gdb.
1675 "server info breakpoints\n"
1677 ;; This also defines a function to be the handler for the output
1678 ;; from the command above. That function will copy the output into
1679 ;; the appropriately typed buffer. That function will be called:
1680 gdb-info-breakpoints-handler
1681 ;; buffer specific functions
1682 gdb-info-breakpoints-custom)
1684 (defconst breakpoint-xpm-data
1686 static char *magick[] = {
1687 /* columns rows colors chars-per-pixel */
1703 "XPM data used for breakpoint icon.")
1705 (defconst breakpoint-enabled-pbm-data
1708 0 0 0 0 1 1 1 1 0 0 0 0
1709 0 0 0 1 1 1 1 1 1 0 0 0
1710 0 0 1 1 1 1 1 1 1 1 0 0
1711 0 1 1 1 1 1 1 1 1 1 1 0
1712 0 1 1 1 1 1 1 1 1 1 1 0
1713 0 1 1 1 1 1 1 1 1 1 1 0
1714 0 1 1 1 1 1 1 1 1 1 1 0
1715 0 0 1 1 1 1 1 1 1 1 0 0
1716 0 0 0 1 1 1 1 1 1 0 0 0
1717 0 0 0 0 1 1 1 1 0 0 0 0"
1718 "PBM data used for enabled breakpoint icon.")
1720 (defconst breakpoint-disabled-pbm-data
1732 0 0 0 1 0 1 0 1 0 0"
1733 "PBM data used for disabled breakpoint icon.")
1735 (defvar breakpoint-enabled-icon nil
1736 "Icon for enabled breakpoint in display margin.")
1738 (defvar breakpoint-disabled-icon nil
1739 "Icon for disabled breakpoint in display margin.")
1741 (and (display-images-p)
1742 ;; Bitmap for breakpoint in fringe
1743 (define-fringe-bitmap 'breakpoint
1744 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1745 ;; Bitmap for gud-overlay-arrow in fringe
1746 (define-fringe-bitmap 'hollow-right-triangle
1747 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1749 (defface breakpoint-enabled
1753 "Face for enabled breakpoint icon in fringe."
1756 (defface breakpoint-disabled
1757 ;; We use different values of grey for different background types,
1758 ;; so that on low-color displays it will end up as something visible
1759 ;; if it has to be approximated.
1760 '((t :foreground "grey70"))
1761 "Face for disabled breakpoint icon in fringe."
1764 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1765 (defun gdb-info-breakpoints-custom ()
1766 (let ((flag) (bptno))
1767 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1768 (dolist (buffer (buffer-list))
1769 (with-current-buffer buffer
1770 (if (and (memq gud-minor-mode '(gdba gdbmi))
1771 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1772 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1773 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1775 (let ((buffer-read-only nil))
1776 (goto-char (point-min))
1777 (while (< (point) (- (point-max) 1))
1779 (if (looking-at "[^\t].*?breakpoint")
1781 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1782 (setq bptno (match-string 1))
1783 (setq flag (char-after (match-beginning 2)))
1784 (add-text-properties
1785 (match-beginning 2) (match-end 2)
1787 '(face font-lock-warning-face)
1788 '(face font-lock-type-face)))
1790 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1792 (add-text-properties
1793 (match-beginning 1) (match-end 1)
1794 '(face font-lock-function-name-face))
1795 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1796 (let ((line (match-string 2))
1797 (file (match-string 1)))
1798 (add-text-properties (line-beginning-position)
1800 '(mouse-face highlight
1801 help-echo "mouse-2, RET: visit breakpoint"))
1802 (unless (file-exists-p file)
1803 (setq file (cdr (assoc bptno gdb-location-alist))))
1805 (not (string-equal file "File not found")))
1806 (with-current-buffer
1807 (find-file-noselect file 'nowarn)
1808 (set (make-local-variable 'gud-minor-mode)
1810 (set (make-local-variable 'tool-bar-map)
1812 ;; Only want one breakpoint icon at each
1815 (goto-line (string-to-number line))
1816 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1819 (concat gdb-server-prefix "list "
1820 (match-string-no-properties 1) ":1\n")
1823 (list (concat gdb-server-prefix "info source\n")
1824 `(lambda () (gdb-get-location
1825 ,bptno ,line ,flag))))))))))
1827 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1829 (defun gdb-mouse-set-clear-breakpoint (event)
1830 "Set/clear breakpoint in left fringe/margin with mouse click."
1832 (mouse-minibuffer-check event)
1833 (let ((posn (event-end event)))
1834 (if (numberp (posn-point posn))
1835 (with-selected-window (posn-window posn)
1837 (goto-char (posn-point posn))
1838 (if (or (posn-object posn)
1839 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1842 (gud-break nil)))))))
1844 (defun gdb-mouse-toggle-breakpoint-margin (event)
1845 "Enable/disable breakpoint in left margin with mouse click."
1847 (mouse-minibuffer-check event)
1848 (let ((posn (event-end event)))
1849 (if (numberp (posn-point posn))
1850 (with-selected-window (posn-window posn)
1852 (goto-char (posn-point posn))
1853 (if (posn-object posn)
1856 (let ((bptno (get-text-property
1857 0 'gdb-bptno (car (posn-string posn)))))
1858 (concat gdb-server-prefix
1859 (if (get-text-property
1860 0 'gdb-enabled (car (posn-string posn)))
1866 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1867 "Enable/disable breakpoint in left fringe with mouse click."
1869 (mouse-minibuffer-check event)
1870 (let* ((posn (event-end event))
1871 (pos (posn-point posn))
1874 (with-selected-window (posn-window posn)
1876 (set-buffer (window-buffer (selected-window)))
1878 (dolist (overlay (overlays-in pos pos))
1879 (when (overlay-get overlay 'put-break)
1880 (setq obj (overlay-get overlay 'before-string))))
1884 (concat gdb-server-prefix
1885 (if (get-text-property 0 'gdb-enabled obj)
1888 (get-text-property 0 'gdb-bptno obj) "\n")
1891 (defun gdb-breakpoints-buffer-name ()
1892 (with-current-buffer gud-comint-buffer
1893 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1895 (defun gdb-display-breakpoints-buffer ()
1896 "Display status of user-settable breakpoints."
1899 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
1901 (defun gdb-frame-breakpoints-buffer ()
1902 "Display status of user-settable breakpoints in a new frame."
1904 (let ((special-display-regexps (append special-display-regexps '(".*")))
1905 (special-display-frame-alist gdb-frame-parameters))
1906 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1908 (defvar gdb-breakpoints-mode-map
1909 (let ((map (make-sparse-keymap))
1910 (menu (make-sparse-keymap "Breakpoints")))
1911 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1912 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1913 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1914 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1915 (suppress-keymap map)
1916 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1917 (define-key map " " 'gdb-toggle-breakpoint)
1918 (define-key map "D" 'gdb-delete-breakpoint)
1919 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1920 (define-key map "q" 'gdb-delete-frame-or-window)
1921 (define-key map "\r" 'gdb-goto-breakpoint)
1922 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1923 (define-key map [follow-link] 'mouse-face)
1926 (defun gdb-delete-frame-or-window ()
1927 "Delete frame if there is only one window. Otherwise delete the window."
1929 (if (one-window-p) (delete-frame)
1932 (defun gdb-breakpoints-mode ()
1933 "Major mode for gdb breakpoints.
1935 \\{gdb-breakpoints-mode-map}"
1936 (kill-all-local-variables)
1937 (setq major-mode 'gdb-breakpoints-mode)
1938 (setq mode-name "Breakpoints")
1939 (use-local-map gdb-breakpoints-mode-map)
1940 (setq buffer-read-only t)
1941 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1942 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1943 'gdb-invalidate-breakpoints
1944 'gdbmi-invalidate-breakpoints))
1946 (defconst gdb-breakpoint-regexp
1947 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1949 (defun gdb-toggle-breakpoint ()
1950 "Enable/disable breakpoint at current line."
1953 (beginning-of-line 1)
1954 (if (looking-at gdb-breakpoint-regexp)
1957 (concat gdb-server-prefix
1958 (if (eq ?y (char-after (match-beginning 2)))
1961 (match-string 1) "\n") 'ignore))
1962 (error "Not recognized as break/watchpoint line"))))
1964 (defun gdb-delete-breakpoint ()
1965 "Delete the breakpoint at current line."
1967 (beginning-of-line 1)
1968 (if (looking-at gdb-breakpoint-regexp)
1971 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1972 (error "Not recognized as break/watchpoint line")))
1974 (defun gdb-goto-breakpoint (&optional event)
1975 "Display the breakpoint location specified at current line."
1976 (interactive (list last-input-event))
1977 (if event (posn-set-point (event-end event)))
1979 (beginning-of-line 1)
1980 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1981 (let ((bptno (match-string 1))
1982 (file (match-string 2))
1983 (line (match-string 3)))
1984 (save-selected-window
1985 (let* ((buffer (find-file-noselect
1986 (if (file-exists-p file) file
1987 (cdr (assoc bptno gdb-location-alist)))))
1988 (window (or (gdb-display-source-buffer buffer)
1989 (display-buffer buffer))))
1990 (setq gdb-source-window window)
1991 (with-current-buffer buffer
1992 (goto-line (string-to-number line))
1993 (set-window-point window (point))))))
1994 (error "No location specified."))))
1997 ;; Frames buffer. This displays a perpetually correct bactracktrace
1998 ;; (from the command `where').
2000 ;; Alas, if your stack is deep, it is costly.
2002 (gdb-set-buffer-rules 'gdb-stack-buffer
2003 'gdb-stack-buffer-name
2006 (def-gdb-auto-updated-buffer gdb-stack-buffer
2007 gdb-invalidate-frames
2008 "server info stack\n"
2009 gdb-info-stack-handler
2010 gdb-info-stack-custom)
2012 (defun gdb-info-stack-custom ()
2013 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2015 (unless (eq gdb-look-up-stack 'delete)
2016 (let ((buffer-read-only nil)
2018 (goto-char (point-min))
2019 (while (< (point) (point-max))
2020 (setq bl (line-beginning-position)
2021 el (line-end-position))
2022 (when (looking-at "#")
2023 (add-text-properties bl el
2024 '(mouse-face highlight
2025 help-echo "mouse-2, RET: Select frame")))
2027 (when (looking-at "^#\\([0-9]+\\)")
2028 (when (string-equal (match-string 1) gdb-frame-number)
2029 (put-text-property bl (+ bl 4)
2030 'face '(:inverse-video t)))
2031 (when (re-search-forward
2033 (if (string-equal (match-string 1) "0") "" " in ")
2034 "\\([^ ]+\\) (") el t)
2035 (put-text-property (match-beginning 1) (match-end 1)
2036 'face font-lock-function-name-face)
2037 (setq bl (match-end 0))
2038 (while (re-search-forward "<\\([^>]+\\)>" el t)
2039 (put-text-property (match-beginning 1) (match-end 1)
2040 'face font-lock-function-name-face))
2042 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2043 (put-text-property (match-beginning 1) (match-end 1)
2044 'face font-lock-variable-name-face))))
2046 (when gdb-look-up-stack
2047 (goto-char (point-min))
2048 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2049 (let ((start (line-beginning-position))
2050 (file (match-string 1))
2051 (line (match-string 2)))
2052 (re-search-backward "^#*\\([0-9]+\\)" start t)
2054 (list (concat gdb-server-prefix "frame "
2055 (match-string 1) "\n") 'gdb-set-hollow))
2057 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2058 (if (eq gdb-look-up-stack 'delete)
2059 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2060 (setq gdb-look-up-stack nil))
2062 (defun gdb-set-hollow ()
2063 (if gud-last-last-frame
2064 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2065 (setq fringe-indicator-alist
2066 '((overlay-arrow . hollow-right-triangle))))))
2068 (defun gdb-stack-buffer-name ()
2069 (with-current-buffer gud-comint-buffer
2070 (concat "*stack frames of " (gdb-get-target-string) "*")))
2072 (defun gdb-display-stack-buffer ()
2073 "Display backtrace of current stack."
2076 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2078 (defun gdb-frame-stack-buffer ()
2079 "Display backtrace of current stack in a new frame."
2081 (let ((special-display-regexps (append special-display-regexps '(".*")))
2082 (special-display-frame-alist gdb-frame-parameters))
2083 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2085 (defvar gdb-frames-mode-map
2086 (let ((map (make-sparse-keymap)))
2087 (suppress-keymap map)
2088 (define-key map "q" 'kill-this-buffer)
2089 (define-key map "\r" 'gdb-frames-select)
2090 (define-key map [mouse-2] 'gdb-frames-select)
2091 (define-key map [follow-link] 'mouse-face)
2094 (defun gdb-frames-mode ()
2095 "Major mode for gdb call stack.
2097 \\{gdb-frames-mode-map}"
2098 (kill-all-local-variables)
2099 (setq major-mode 'gdb-frames-mode)
2100 (setq mode-name "Frames")
2101 (setq buffer-read-only t)
2102 (use-local-map gdb-frames-mode-map)
2103 (run-mode-hooks 'gdb-frames-mode-hook)
2104 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2105 'gdb-invalidate-frames
2106 'gdbmi-invalidate-frames))
2108 (defun gdb-get-frame-number ()
2111 (let* ((start (line-beginning-position))
2112 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2113 (n (or (and pos (match-string-no-properties 1)) "0")))
2116 (defun gdb-frames-select (&optional event)
2117 "Select the frame and display the relevant source."
2118 (interactive (list last-input-event))
2119 (if event (posn-set-point (event-end event)))
2121 (list (concat gdb-server-prefix "frame "
2122 (gdb-get-frame-number) "\n") 'ignore)))
2125 ;; Threads buffer. This displays a selectable thread list.
2127 (gdb-set-buffer-rules 'gdb-threads-buffer
2128 'gdb-threads-buffer-name
2131 (def-gdb-auto-updated-buffer gdb-threads-buffer
2132 gdb-invalidate-threads
2133 (concat gdb-server-prefix "info threads\n")
2134 gdb-info-threads-handler
2135 gdb-info-threads-custom)
2137 (defun gdb-info-threads-custom ()
2138 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2139 (let ((buffer-read-only nil))
2141 (goto-char (point-min))
2142 (while (< (point) (point-max))
2143 (unless (looking-at "No ")
2144 (add-text-properties (line-beginning-position) (line-end-position)
2145 '(mouse-face highlight
2146 help-echo "mouse-2, RET: select thread")))
2147 (forward-line 1))))))
2149 (defun gdb-threads-buffer-name ()
2150 (with-current-buffer gud-comint-buffer
2151 (concat "*threads of " (gdb-get-target-string) "*")))
2153 (defun gdb-display-threads-buffer ()
2154 "Display IDs of currently known threads."
2157 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2159 (defun gdb-frame-threads-buffer ()
2160 "Display IDs of currently known threads in a new frame."
2162 (let ((special-display-regexps (append special-display-regexps '(".*")))
2163 (special-display-frame-alist gdb-frame-parameters))
2164 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2166 (defvar gdb-threads-mode-map
2167 (let ((map (make-sparse-keymap)))
2168 (suppress-keymap map)
2169 (define-key map "q" 'kill-this-buffer)
2170 (define-key map "\r" 'gdb-threads-select)
2171 (define-key map [mouse-2] 'gdb-threads-select)
2172 (define-key map [follow-link] 'mouse-face)
2175 (defvar gdb-threads-font-lock-keywords
2176 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2177 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2178 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2179 "Font lock keywords used in `gdb-threads-mode'.")
2181 (defun gdb-threads-mode ()
2182 "Major mode for gdb threads.
2184 \\{gdb-threads-mode-map}"
2185 (kill-all-local-variables)
2186 (setq major-mode 'gdb-threads-mode)
2187 (setq mode-name "Threads")
2188 (setq buffer-read-only t)
2189 (use-local-map gdb-threads-mode-map)
2190 (set (make-local-variable 'font-lock-defaults)
2191 '(gdb-threads-font-lock-keywords))
2192 (run-mode-hooks 'gdb-threads-mode-hook)
2193 'gdb-invalidate-threads)
2195 (defun gdb-get-thread-number ()
2197 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2198 (match-string-no-properties 1)))
2200 (defun gdb-threads-select (&optional event)
2201 "Select the thread and display the relevant source."
2202 (interactive (list last-input-event))
2203 (if event (posn-set-point (event-end event)))
2205 (list (concat gdb-server-prefix "thread "
2206 (gdb-get-thread-number) "\n") 'ignore))
2207 (gud-display-frame))
2210 ;; Registers buffer.
2212 (defcustom gdb-all-registers nil
2213 "Non-nil means include floating-point registers."
2218 (gdb-set-buffer-rules 'gdb-registers-buffer
2219 'gdb-registers-buffer-name
2220 'gdb-registers-mode)
2222 (def-gdb-auto-updated-buffer gdb-registers-buffer
2223 gdb-invalidate-registers
2225 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2226 gdb-info-registers-handler
2227 gdb-info-registers-custom)
2229 (defun gdb-info-registers-custom ()
2230 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2232 (let ((buffer-read-only nil)
2234 (goto-char (point-min))
2235 (while (< (point) (point-max))
2236 (setq start (line-beginning-position))
2237 (setq end (line-end-position))
2238 (when (looking-at "^[^ ]+")
2239 (unless (string-equal (match-string 0) "The")
2240 (put-text-property start (match-end 0)
2241 'face font-lock-variable-name-face)
2242 (add-text-properties start end
2243 '(help-echo "mouse-2: edit value"
2244 mouse-face highlight))))
2245 (forward-line 1))))))
2247 (defun gdb-edit-register-value (&optional event)
2248 (interactive (list last-input-event))
2250 (if event (posn-set-point (event-end event)))
2252 (let* ((register (current-word))
2253 (value (read-string (format "New value (%s): " register))))
2255 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2258 (defvar gdb-registers-mode-map
2259 (let ((map (make-sparse-keymap)))
2260 (suppress-keymap map)
2261 (define-key map "\r" 'gdb-edit-register-value)
2262 (define-key map [mouse-2] 'gdb-edit-register-value)
2263 (define-key map " " 'gdb-all-registers)
2264 (define-key map "q" 'kill-this-buffer)
2267 (defun gdb-registers-mode ()
2268 "Major mode for gdb registers.
2270 \\{gdb-registers-mode-map}"
2271 (kill-all-local-variables)
2272 (setq major-mode 'gdb-registers-mode)
2273 (setq mode-name "Registers")
2274 (setq buffer-read-only t)
2275 (use-local-map gdb-registers-mode-map)
2276 (run-mode-hooks 'gdb-registers-mode-hook)
2277 (if (string-equal gdb-version "pre-6.4")
2279 (if gdb-all-registers (setq mode-name "Registers:All"))
2280 'gdb-invalidate-registers)
2281 'gdb-invalidate-registers-1))
2283 (defun gdb-registers-buffer-name ()
2284 (with-current-buffer gud-comint-buffer
2285 (concat "*registers of " (gdb-get-target-string) "*")))
2287 (defun gdb-display-registers-buffer ()
2288 "Display integer register contents."
2291 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2293 (defun gdb-frame-registers-buffer ()
2294 "Display integer register contents in a new frame."
2296 (let ((special-display-regexps (append special-display-regexps '(".*")))
2297 (special-display-frame-alist gdb-frame-parameters))
2298 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2300 (defun gdb-all-registers ()
2301 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2303 (when (string-equal gdb-version "pre-6.4")
2304 (if gdb-all-registers
2306 (setq gdb-all-registers nil)
2307 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2308 (setq mode-name "Registers")))
2309 (setq gdb-all-registers t)
2310 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2311 (setq mode-name "Registers:All")))
2312 (message (format "Display of floating-point registers %sabled"
2313 (if gdb-all-registers "en" "dis")))
2314 (gdb-invalidate-registers)))
2319 (defcustom gdb-memory-repeat-count 32
2320 "Number of data items in memory window."
2325 (defcustom gdb-memory-format "x"
2326 "Display format of data items in memory window."
2327 :type '(choice (const :tag "Hexadecimal" "x")
2328 (const :tag "Signed decimal" "d")
2329 (const :tag "Unsigned decimal" "u")
2330 (const :tag "Octal" "o")
2331 (const :tag "Binary" "t"))
2335 (defcustom gdb-memory-unit "w"
2336 "Unit size of data items in memory window."
2337 :type '(choice (const :tag "Byte" "b")
2338 (const :tag "Halfword" "h")
2339 (const :tag "Word" "w")
2340 (const :tag "Giant word" "g"))
2344 (gdb-set-buffer-rules 'gdb-memory-buffer
2345 'gdb-memory-buffer-name
2348 (def-gdb-auto-updated-buffer gdb-memory-buffer
2349 gdb-invalidate-memory
2350 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2351 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2352 gdb-read-memory-handler
2353 gdb-read-memory-custom)
2355 (defun gdb-read-memory-custom ()
2357 (goto-char (point-min))
2358 (if (looking-at "0x[[:xdigit:]]+")
2359 (setq gdb-memory-address (match-string 0)))))
2361 (defvar gdb-memory-mode-map
2362 (let ((map (make-sparse-keymap)))
2363 (suppress-keymap map)
2364 (define-key map "q" 'kill-this-buffer)
2367 (defun gdb-memory-set-address (event)
2368 "Set the start memory address."
2370 (save-selected-window
2371 (select-window (posn-window (event-start event)))
2372 (let ((arg (read-from-minibuffer "Memory address: ")))
2373 (setq gdb-memory-address arg))
2374 (gdb-invalidate-memory)))
2376 (defun gdb-memory-set-repeat-count (event)
2377 "Set the number of data items in memory window."
2379 (save-selected-window
2380 (select-window (posn-window (event-start event)))
2381 (let* ((arg (read-from-minibuffer "Repeat count: "))
2382 (count (string-to-number arg)))
2384 (error "Positive numbers only")
2385 (customize-set-variable 'gdb-memory-repeat-count count)
2386 (gdb-invalidate-memory)))))
2388 (defun gdb-memory-format-binary ()
2389 "Set the display format to binary."
2391 (customize-set-variable 'gdb-memory-format "t")
2392 (gdb-invalidate-memory))
2394 (defun gdb-memory-format-octal ()
2395 "Set the display format to octal."
2397 (customize-set-variable 'gdb-memory-format "o")
2398 (gdb-invalidate-memory))
2400 (defun gdb-memory-format-unsigned ()
2401 "Set the display format to unsigned decimal."
2403 (customize-set-variable 'gdb-memory-format "u")
2404 (gdb-invalidate-memory))
2406 (defun gdb-memory-format-signed ()
2407 "Set the display format to decimal."
2409 (customize-set-variable 'gdb-memory-format "d")
2410 (gdb-invalidate-memory))
2412 (defun gdb-memory-format-hexadecimal ()
2413 "Set the display format to hexadecimal."
2415 (customize-set-variable 'gdb-memory-format "x")
2416 (gdb-invalidate-memory))
2418 (defvar gdb-memory-format-map
2419 (let ((map (make-sparse-keymap)))
2420 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2422 "Keymap to select format in the header line.")
2424 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2425 "Menu of display formats in the header line.")
2427 (define-key gdb-memory-format-menu [binary]
2428 '(menu-item "Binary" gdb-memory-format-binary
2429 :button (:radio . (equal gdb-memory-format "t"))))
2430 (define-key gdb-memory-format-menu [octal]
2431 '(menu-item "Octal" gdb-memory-format-octal
2432 :button (:radio . (equal gdb-memory-format "o"))))
2433 (define-key gdb-memory-format-menu [unsigned]
2434 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2435 :button (:radio . (equal gdb-memory-format "u"))))
2436 (define-key gdb-memory-format-menu [signed]
2437 '(menu-item "Signed Decimal" gdb-memory-format-signed
2438 :button (:radio . (equal gdb-memory-format "d"))))
2439 (define-key gdb-memory-format-menu [hexadecimal]
2440 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2441 :button (:radio . (equal gdb-memory-format "x"))))
2443 (defun gdb-memory-format-menu (event)
2445 (x-popup-menu event gdb-memory-format-menu))
2447 (defun gdb-memory-format-menu-1 (event)
2449 (save-selected-window
2450 (select-window (posn-window (event-start event)))
2451 (let* ((selection (gdb-memory-format-menu event))
2452 (binding (and selection (lookup-key gdb-memory-format-menu
2453 (vector (car selection))))))
2454 (if binding (call-interactively binding)))))
2456 (defun gdb-memory-unit-giant ()
2457 "Set the unit size to giant words (eight bytes)."
2459 (customize-set-variable 'gdb-memory-unit "g")
2460 (gdb-invalidate-memory))
2462 (defun gdb-memory-unit-word ()
2463 "Set the unit size to words (four bytes)."
2465 (customize-set-variable 'gdb-memory-unit "w")
2466 (gdb-invalidate-memory))
2468 (defun gdb-memory-unit-halfword ()
2469 "Set the unit size to halfwords (two bytes)."
2471 (customize-set-variable 'gdb-memory-unit "h")
2472 (gdb-invalidate-memory))
2474 (defun gdb-memory-unit-byte ()
2475 "Set the unit size to bytes."
2477 (customize-set-variable 'gdb-memory-unit "b")
2478 (gdb-invalidate-memory))
2480 (defvar gdb-memory-unit-map
2481 (let ((map (make-sparse-keymap)))
2482 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2484 "Keymap to select units in the header line.")
2486 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2487 "Menu of units in the header line.")
2489 (define-key gdb-memory-unit-menu [giantwords]
2490 '(menu-item "Giant words" gdb-memory-unit-giant
2491 :button (:radio . (equal gdb-memory-unit "g"))))
2492 (define-key gdb-memory-unit-menu [words]
2493 '(menu-item "Words" gdb-memory-unit-word
2494 :button (:radio . (equal gdb-memory-unit "w"))))
2495 (define-key gdb-memory-unit-menu [halfwords]
2496 '(menu-item "Halfwords" gdb-memory-unit-halfword
2497 :button (:radio . (equal gdb-memory-unit "h"))))
2498 (define-key gdb-memory-unit-menu [bytes]
2499 '(menu-item "Bytes" gdb-memory-unit-byte
2500 :button (:radio . (equal gdb-memory-unit "b"))))
2502 (defun gdb-memory-unit-menu (event)
2504 (x-popup-menu event gdb-memory-unit-menu))
2506 (defun gdb-memory-unit-menu-1 (event)
2508 (save-selected-window
2509 (select-window (posn-window (event-start event)))
2510 (let* ((selection (gdb-memory-unit-menu event))
2511 (binding (and selection (lookup-key gdb-memory-unit-menu
2512 (vector (car selection))))))
2513 (if binding (call-interactively binding)))))
2515 ;;from make-mode-line-mouse-map
2516 (defun gdb-make-header-line-mouse-map (mouse function) "\
2517 Return a keymap with single entry for mouse key MOUSE on the header line.
2518 MOUSE is defined to run function FUNCTION with no args in the buffer
2519 corresponding to the mode line clicked."
2520 (let ((map (make-sparse-keymap)))
2521 (define-key map (vector 'header-line mouse) function)
2522 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2525 (defvar gdb-memory-font-lock-keywords
2526 '(;; <__function.name+n>
2527 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2529 "Font lock keywords used in `gdb-memory-mode'.")
2531 (defun gdb-memory-mode ()
2532 "Major mode for examining memory.
2534 \\{gdb-memory-mode-map}"
2535 (kill-all-local-variables)
2536 (setq major-mode 'gdb-memory-mode)
2537 (setq mode-name "Memory")
2538 (setq buffer-read-only t)
2539 (use-local-map gdb-memory-mode-map)
2540 (setq header-line-format
2546 'face font-lock-warning-face
2547 'help-echo "mouse-1: Decrement address"
2548 'mouse-face 'mode-line-highlight
2550 (gdb-make-header-line-mouse-map
2552 #'(lambda () (interactive)
2553 (let ((gdb-memory-address
2554 ;; Let GDB do the arithmetic.
2556 gdb-memory-address " - "
2558 (* gdb-memory-repeat-count
2559 (cond ((string= gdb-memory-unit "b") 1)
2560 ((string= gdb-memory-unit "h") 2)
2561 ((string= gdb-memory-unit "w") 4)
2562 ((string= gdb-memory-unit "g") 8)))))))
2563 (gdb-invalidate-memory)))))
2566 'face font-lock-warning-face
2567 'help-echo "mouse-1: Increment address"
2568 'mouse-face 'mode-line-highlight
2569 'local-map (gdb-make-header-line-mouse-map
2571 #'(lambda () (interactive)
2572 (let ((gdb-memory-address nil))
2573 (gdb-invalidate-memory)))))
2575 (propertize gdb-memory-address
2576 'face font-lock-warning-face
2577 'help-echo "mouse-1: Set memory address"
2578 'mouse-face 'mode-line-highlight
2579 'local-map (gdb-make-header-line-mouse-map
2581 #'gdb-memory-set-address))
2583 (propertize (number-to-string gdb-memory-repeat-count)
2584 'face font-lock-warning-face
2585 'help-echo "mouse-1: Set repeat count"
2586 'mouse-face 'mode-line-highlight
2587 'local-map (gdb-make-header-line-mouse-map
2589 #'gdb-memory-set-repeat-count))
2591 (propertize gdb-memory-format
2592 'face font-lock-warning-face
2593 'help-echo "mouse-3: Select display format"
2594 'mouse-face 'mode-line-highlight
2595 'local-map gdb-memory-format-map)
2597 (propertize gdb-memory-unit
2598 'face font-lock-warning-face
2599 'help-echo "mouse-3: Select unit size"
2600 'mouse-face 'mode-line-highlight
2601 'local-map gdb-memory-unit-map))))
2602 (set (make-local-variable 'font-lock-defaults)
2603 '(gdb-memory-font-lock-keywords))
2604 (run-mode-hooks 'gdb-memory-mode-hook)
2605 'gdb-invalidate-memory)
2607 (defun gdb-memory-buffer-name ()
2608 (with-current-buffer gud-comint-buffer
2609 (concat "*memory of " (gdb-get-target-string) "*")))
2611 (defun gdb-display-memory-buffer ()
2612 "Display memory contents."
2615 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2617 (defun gdb-frame-memory-buffer ()
2618 "Display memory contents in a new frame."
2620 (let ((special-display-regexps (append special-display-regexps '(".*")))
2621 (special-display-frame-alist gdb-frame-parameters))
2622 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2627 (gdb-set-buffer-rules 'gdb-locals-buffer
2628 'gdb-locals-buffer-name
2631 (def-gdb-auto-update-trigger gdb-invalidate-locals
2632 (gdb-get-buffer 'gdb-locals-buffer)
2633 "server info locals\n"
2634 gdb-info-locals-handler)
2636 (defvar gdb-locals-watch-map
2637 (let ((map (make-sparse-keymap)))
2638 (define-key map "\r" '(lambda () (interactive)
2641 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2642 (mouse-set-point event)
2646 "Keymap to create watch expression of a complex data type local variable.")
2648 (defconst gdb-struct-string
2649 (concat (propertize "[struct/union]"
2650 'mouse-face 'highlight
2651 'help-echo "mouse-2: create watch expression"
2652 'local-map gdb-locals-watch-map) "\n"))
2654 (defconst gdb-array-string
2655 (concat " " (propertize "[array]"
2656 'mouse-face 'highlight
2657 'help-echo "mouse-2: create watch expression"
2658 'local-map gdb-locals-watch-map) "\n"))
2660 ;; Abbreviate for arrays and structures.
2661 ;; These can be expanded using gud-display.
2662 (defun gdb-info-locals-handler ()
2663 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2664 gdb-pending-triggers))
2665 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2666 (with-current-buffer buf
2667 (goto-char (point-min))
2668 (while (re-search-forward "^[ }].*\n" nil t)
2669 (replace-match "" nil nil))
2670 (goto-char (point-min))
2671 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2672 (replace-match gdb-struct-string nil nil))
2673 (goto-char (point-min))
2674 (while (re-search-forward "\\s-*{.*\n" nil t)
2675 (replace-match gdb-array-string nil nil))))
2676 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2678 (with-current-buffer buf
2679 (let* ((window (get-buffer-window buf 0))
2680 (start (window-start window))
2681 (p (window-point window))
2682 (buffer-read-only nil))
2684 (insert-buffer-substring (gdb-get-buffer-create
2685 'gdb-partial-output-buffer))
2686 (set-window-start window start)
2687 (set-window-point window p))
2689 (run-hooks 'gdb-info-locals-hook))
2691 (defvar gdb-locals-mode-map
2692 (let ((map (make-sparse-keymap)))
2693 (suppress-keymap map)
2694 (define-key map "q" 'kill-this-buffer)
2697 (defun gdb-locals-mode ()
2698 "Major mode for gdb locals.
2700 \\{gdb-locals-mode-map}"
2701 (kill-all-local-variables)
2702 (setq major-mode 'gdb-locals-mode)
2703 (setq mode-name (concat "Locals:" gdb-selected-frame))
2704 (setq buffer-read-only t)
2705 (use-local-map gdb-locals-mode-map)
2706 (set (make-local-variable 'font-lock-defaults)
2707 '(gdb-locals-font-lock-keywords))
2708 (run-mode-hooks 'gdb-locals-mode-hook)
2709 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2710 (string-equal gdb-version "pre-6.4"))
2711 'gdb-invalidate-locals
2712 'gdb-invalidate-locals-1))
2714 (defun gdb-locals-buffer-name ()
2715 (with-current-buffer gud-comint-buffer
2716 (concat "*locals of " (gdb-get-target-string) "*")))
2718 (defun gdb-display-locals-buffer ()
2719 "Display local variables of current stack and their values."
2722 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2724 (defun gdb-frame-locals-buffer ()
2725 "Display local variables of current stack and their values in a new frame."
2727 (let ((special-display-regexps (append special-display-regexps '(".*")))
2728 (special-display-frame-alist gdb-frame-parameters))
2729 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2732 ;;;; Window management
2733 (defun gdb-display-buffer (buf dedicated &optional size)
2734 (let ((answer (get-buffer-window buf 0))
2737 (display-buffer buf nil 0) ;Raise the frame if necessary.
2738 ;; The buffer is not yet displayed.
2739 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2740 (let ((window (get-lru-window)))
2742 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2743 ,gdb-source-window))))
2745 (set-window-buffer window buf)
2746 (setq answer window))
2747 (setq must-split t)))
2749 (let* ((largest (get-largest-window))
2750 (cur-size (window-height largest))
2751 (new-size (and size (< size cur-size) (- cur-size size))))
2752 (setq answer (split-window largest new-size))
2753 (set-window-buffer answer buf)
2754 (set-window-dedicated-p answer dedicated)))
2758 ;;; Shared keymap initialization:
2760 (let ((menu (make-sparse-keymap "GDB-Windows")))
2761 (define-key gud-menu-map [displays]
2762 `(menu-item "GDB-Windows" ,menu
2763 :visible (memq gud-minor-mode '(gdbmi gdba))))
2764 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2765 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2766 (define-key menu [inferior]
2767 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2768 :enable gdb-use-separate-io-buffer))
2769 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2770 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2771 (define-key menu [disassembly]
2772 '("Disassembly" . gdb-display-assembler-buffer))
2773 (define-key menu [breakpoints]
2774 '("Breakpoints" . gdb-display-breakpoints-buffer))
2775 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2776 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2778 (let ((menu (make-sparse-keymap "GDB-Frames")))
2779 (define-key gud-menu-map [frames]
2780 `(menu-item "GDB-Frames" ,menu
2781 :visible (memq gud-minor-mode '(gdbmi gdba))))
2782 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2783 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2784 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2785 (define-key menu [inferior]
2786 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2787 :enable gdb-use-separate-io-buffer))
2788 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2789 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2790 (define-key menu [breakpoints]
2791 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2792 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2793 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2795 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2796 (define-key gud-menu-map [ui]
2797 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2798 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2799 (define-key menu [gdb-find-source-frame]
2800 '(menu-item "Look For Source Frame" gdb-find-source-frame
2801 :visible (eq gud-minor-mode 'gdba)
2802 :help "Toggle look for source frame."
2803 :button (:toggle . gdb-find-source-frame)))
2804 (define-key menu [gdb-use-separate-io]
2805 '(menu-item "Separate Inferior IO" gdb-use-separate-io-buffer
2806 :visible (eq gud-minor-mode 'gdba)
2807 :help "Toggle separate IO for inferior."
2808 :button (:toggle . gdb-use-separate-io-buffer)))
2809 (define-key menu [gdb-many-windows]
2810 '(menu-item "Display Other Windows" gdb-many-windows
2811 :help "Toggle display of locals, stack and breakpoint information"
2812 :button (:toggle . gdb-many-windows)))
2813 (define-key menu [gdb-restore-windows]
2814 '(menu-item "Restore Window Layout" gdb-restore-windows
2815 :help "Restore standard layout for debug session.")))
2817 (defun gdb-frame-gdb-buffer ()
2818 "Display GUD buffer in a new frame."
2820 (let ((special-display-regexps (append special-display-regexps '(".*")))
2821 (special-display-frame-alist
2822 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2823 gdb-frame-parameters)))
2824 (same-window-regexps nil))
2825 (display-buffer gud-comint-buffer)))
2827 (defun gdb-display-gdb-buffer ()
2828 "Display GUD buffer."
2830 (let ((same-window-regexps nil))
2831 (pop-to-buffer gud-comint-buffer)))
2833 (defun gdb-set-window-buffer (name)
2834 (set-window-buffer (selected-window) (get-buffer name))
2835 (set-window-dedicated-p (selected-window) t))
2837 (defun gdb-setup-windows ()
2838 "Layout the window pattern for `gdb-many-windows'."
2839 (gdb-display-locals-buffer)
2840 (gdb-display-stack-buffer)
2841 (delete-other-windows)
2842 (gdb-display-breakpoints-buffer)
2843 (delete-other-windows)
2845 (pop-to-buffer gud-comint-buffer)
2846 (split-window nil ( / ( * (window-height) 3) 4))
2847 (split-window nil ( / (window-height) 3))
2848 (split-window-horizontally)
2850 (gdb-set-window-buffer (gdb-locals-buffer-name))
2853 (if gud-last-last-frame
2854 (gud-find-file (car gud-last-last-frame))
2856 (gud-find-file gdb-main-file)
2857 ;; Put buffer list in window if we
2858 ;; can't find a source file.
2859 (list-buffers-noselect))))
2860 (setq gdb-source-window (selected-window))
2861 (when gdb-use-separate-io-buffer
2862 (split-window-horizontally)
2864 (gdb-set-window-buffer
2865 (gdb-get-buffer-create 'gdb-inferior-io)))
2867 (gdb-set-window-buffer (gdb-stack-buffer-name))
2868 (split-window-horizontally)
2870 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2873 (defun gdb-restore-windows ()
2874 "Restore the basic arrangement of windows used by gdba.
2875 This arrangement depends on the value of `gdb-many-windows'."
2877 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2878 (delete-other-windows)
2879 (if gdb-many-windows
2881 (when (or gud-last-last-frame gdb-show-main)
2885 (if gud-last-last-frame
2886 (gud-find-file (car gud-last-last-frame))
2887 (gud-find-file gdb-main-file)))
2888 (setq gdb-source-window (selected-window))
2892 "Exit a debugging session cleanly.
2893 Kills the gdb buffers, and resets variables and the source buffers."
2894 (dolist (buffer (buffer-list))
2895 (unless (eq buffer gud-comint-buffer)
2896 (with-current-buffer buffer
2897 (if (memq gud-minor-mode '(gdbmi gdba))
2898 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2900 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2901 (setq gud-minor-mode nil)
2902 (kill-local-variable 'tool-bar-map)
2903 (kill-local-variable 'gdb-define-alist))))))
2904 (when (markerp gdb-overlay-arrow-position)
2905 (move-marker gdb-overlay-arrow-position nil)
2906 (setq gdb-overlay-arrow-position nil))
2907 (setq overlay-arrow-variable-list
2908 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2909 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2910 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2911 (setq gud-running nil)
2912 (setq gdb-active-process nil)
2913 (setq gdb-var-list nil)
2914 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2916 (defun gdb-source-info ()
2917 "Find the source file where the program starts and displays it with related
2919 (goto-char (point-min))
2920 (if (and (search-forward "Located in " nil t)
2921 (looking-at "\\S-+"))
2922 (setq gdb-main-file (match-string 0)))
2923 (goto-char (point-min))
2924 (if (search-forward "Includes preprocessor macro info." nil t)
2925 (setq gdb-macro-info t))
2926 (if gdb-many-windows
2928 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2930 (let ((pop-up-windows t))
2931 (display-buffer (gud-find-file gdb-main-file))))))
2933 (defun gdb-get-location (bptno line flag)
2934 "Find the directory containing the relevant source file.
2935 Put in buffer and place breakpoint icon."
2936 (goto-char (point-min))
2937 (catch 'file-not-found
2938 (if (search-forward "Located in " nil t)
2939 (when (looking-at "\\S-+")
2940 (delete (cons bptno "File not found") gdb-location-alist)
2941 (push (cons bptno (match-string 0)) gdb-location-alist))
2943 (unless (assoc bptno gdb-location-alist)
2944 (push (cons bptno "File not found") gdb-location-alist)
2945 (message-box "Cannot find source file for breakpoint location.\n\
2946 Add directory to search path for source files using the GDB command, dir."))
2947 (throw 'file-not-found nil))
2948 (with-current-buffer
2949 (find-file-noselect (match-string 0))
2950 (save-current-buffer
2951 (set (make-local-variable 'gud-minor-mode) 'gdba)
2952 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2953 ;; only want one breakpoint icon at each location
2955 (goto-line (string-to-number line))
2956 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2958 (add-hook 'find-file-hook 'gdb-find-file-hook)
2960 (defun gdb-find-file-hook ()
2961 "Set up buffer for debugging if file is part of the source code
2962 of the current session."
2963 (if (and (buffer-name gud-comint-buffer)
2964 ;; in case gud or gdb-ui is just loaded
2966 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2968 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2969 (if (member (if (string-equal gdb-version "pre-6.4")
2970 (file-name-nondirectory buffer-file-name)
2972 gdb-source-file-list)
2973 (with-current-buffer (find-buffer-visiting buffer-file-name)
2974 (set (make-local-variable 'gud-minor-mode)
2975 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2976 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2979 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2980 "Put string PUTSTRING in front of POS in the current buffer.
2981 PUTSTRING is displayed by putting an overlay into the current buffer with a
2982 `before-string' string that has a `display' property whose value is
2984 (let ((string (make-string 1 ?x))
2985 (buffer (current-buffer)))
2986 (setq putstring (copy-sequence putstring))
2987 (let ((overlay (make-overlay pos pos buffer))
2989 (list (list 'margin 'left-margin) putstring))))
2990 (put-text-property 0 1 'display prop string)
2992 (add-text-properties 0 1 sprops string))
2993 (overlay-put overlay 'put-break t)
2994 (overlay-put overlay 'before-string string))))
2996 ;;from remove-images
2997 (defun gdb-remove-strings (start end &optional buffer)
2998 "Remove strings between START and END in BUFFER.
2999 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3000 BUFFER nil or omitted means use the current buffer."
3002 (setq buffer (current-buffer)))
3003 (dolist (overlay (overlays-in start end))
3004 (when (overlay-get overlay 'put-break)
3005 (delete-overlay overlay))))
3007 (defun gdb-put-breakpoint-icon (enabled bptno)
3008 (let ((start (- (line-beginning-position) 1))
3009 (end (+ (line-end-position) 1))
3010 (putstring (if enabled "B" "b"))
3011 (source-window (get-buffer-window (current-buffer) 0)))
3012 (add-text-properties
3013 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3016 (add-text-properties
3017 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3018 (add-text-properties
3019 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3020 (gdb-remove-breakpoint-icons start end)
3021 (if (display-images-p)
3022 (if (>= (or left-fringe-width
3023 (if source-window (car (window-fringes source-window)))
3024 gdb-buffer-fringe-width) 8)
3027 `(left-fringe breakpoint
3030 'breakpoint-disabled))
3032 'gdb-enabled enabled)
3033 (when (< left-margin-width 2)
3034 (save-current-buffer
3035 (setq left-margin-width 2)
3039 left-margin-width right-margin-width))))
3042 (or breakpoint-enabled-icon
3043 (setq breakpoint-enabled-icon
3044 (find-image `((:type xpm :data
3045 ,breakpoint-xpm-data
3046 :ascent 100 :pointer hand)
3048 ,breakpoint-enabled-pbm-data
3049 :ascent 100 :pointer hand)))))
3050 (or breakpoint-disabled-icon
3051 (setq breakpoint-disabled-icon
3052 (find-image `((:type xpm :data
3053 ,breakpoint-xpm-data
3054 :conversion disabled
3055 :ascent 100 :pointer hand)
3057 ,breakpoint-disabled-pbm-data
3058 :ascent 100 :pointer hand))))))
3062 (when (< left-margin-width 2)
3063 (save-current-buffer
3064 (setq left-margin-width 2)
3065 (let ((window (get-buffer-window (current-buffer) 0)))
3068 window left-margin-width right-margin-width)))))
3070 (propertize putstring
3071 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3074 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3075 (gdb-remove-strings start end)
3076 (if (display-images-p)
3077 (remove-images start end))
3079 (setq left-margin-width 0)
3080 (let ((window (get-buffer-window (current-buffer) 0)))
3083 window left-margin-width right-margin-width)))))
3087 ;; Assembler buffer.
3089 (gdb-set-buffer-rules 'gdb-assembler-buffer
3090 'gdb-assembler-buffer-name
3091 'gdb-assembler-mode)
3093 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3094 ;; window-start but keep the overlay arrow/current line visible.
3095 (defun gdb-assembler-handler ()
3096 (setq gdb-pending-triggers
3097 (delq 'gdb-invalidate-assembler
3098 gdb-pending-triggers))
3099 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3101 (with-current-buffer buf
3102 (let* ((window (get-buffer-window buf 0))
3103 (p (window-point window))
3104 (buffer-read-only nil))
3106 (insert-buffer-substring (gdb-get-buffer-create
3107 'gdb-partial-output-buffer))
3108 (set-window-point window p)))))
3109 ;; put customisation here
3110 (gdb-assembler-custom))
3112 (defun gdb-assembler-custom ()
3113 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3114 (pos 1) (address) (flag) (bptno))
3115 (with-current-buffer buffer
3117 (if (not (equal gdb-pc-address "main"))
3119 (goto-char (point-min))
3120 (if (and gdb-pc-address
3121 (search-forward gdb-pc-address nil t))
3125 (setq fringe-indicator-alist
3126 (if (string-equal gdb-frame-number "0")
3128 '((overlay-arrow . hollow-right-triangle))))
3129 (or gdb-overlay-arrow-position
3130 (setq gdb-overlay-arrow-position (make-marker)))
3131 (set-marker gdb-overlay-arrow-position
3132 (point) (current-buffer))))))
3133 ;; remove all breakpoint-icons in assembler buffer before updating.
3134 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3135 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3136 (goto-char (point-min))
3137 (while (< (point) (- (point-max) 1))
3139 (if (looking-at "[^\t].*?breakpoint")
3142 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3143 (setq bptno (match-string 1))
3144 (setq flag (char-after (match-beginning 2)))
3145 (setq address (match-string 3))
3146 (with-current-buffer buffer
3148 (goto-char (point-min))
3149 (if (search-forward address nil t)
3150 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3151 (if (not (equal gdb-pc-address "main"))
3152 (with-current-buffer buffer
3153 (set-window-point (get-buffer-window buffer 0) pos)))))
3155 (defvar gdb-assembler-mode-map
3156 (let ((map (make-sparse-keymap)))
3157 (suppress-keymap map)
3158 (define-key map "q" 'kill-this-buffer)
3161 (defvar gdb-assembler-font-lock-keywords
3162 '(;; <__function.name+n>
3163 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3164 (1 font-lock-function-name-face))
3165 ;; 0xNNNNNNNN <__function.name+n>: opcode
3166 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3167 (4 font-lock-keyword-face))
3168 ;; %register(at least i386)
3169 ("%\\sw+" . font-lock-variable-name-face)
3170 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3171 (1 font-lock-comment-face)
3172 (2 font-lock-function-name-face))
3173 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3174 "Font lock keywords used in `gdb-assembler-mode'.")
3176 (defun gdb-assembler-mode ()
3177 "Major mode for viewing code assembler.
3179 \\{gdb-assembler-mode-map}"
3180 (kill-all-local-variables)
3181 (setq major-mode 'gdb-assembler-mode)
3182 (setq mode-name (concat "Machine:" gdb-selected-frame))
3183 (setq gdb-overlay-arrow-position nil)
3184 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3185 (setq fringes-outside-margins t)
3186 (setq buffer-read-only t)
3187 (use-local-map gdb-assembler-mode-map)
3188 (gdb-invalidate-assembler)
3189 (set (make-local-variable 'font-lock-defaults)
3190 '(gdb-assembler-font-lock-keywords))
3191 (run-mode-hooks 'gdb-assembler-mode-hook)
3192 'gdb-invalidate-assembler)
3194 (defun gdb-assembler-buffer-name ()
3195 (with-current-buffer gud-comint-buffer
3196 (concat "*disassembly of " (gdb-get-target-string) "*")))
3198 (defun gdb-display-assembler-buffer ()
3199 "Display disassembly view."
3201 (setq gdb-previous-frame nil)
3203 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3205 (defun gdb-frame-assembler-buffer ()
3206 "Display disassembly view in a new frame."
3208 (setq gdb-previous-frame nil)
3209 (let ((special-display-regexps (append special-display-regexps '(".*")))
3210 (special-display-frame-alist gdb-frame-parameters))
3211 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3213 ;; modified because if gdb-pc-address has changed value a new command
3214 ;; must be enqueued to update the buffer with the new output
3215 (defun gdb-invalidate-assembler (&optional ignored)
3216 (if (gdb-get-buffer 'gdb-assembler-buffer)
3218 (unless (and gdb-selected-frame
3219 (string-equal gdb-selected-frame gdb-previous-frame))
3220 (if (or (not (member 'gdb-invalidate-assembler
3221 gdb-pending-triggers))
3222 (not (string-equal gdb-pc-address
3223 gdb-previous-frame-address)))
3225 ;; take previous disassemble command, if any, off the queue
3226 (with-current-buffer gud-comint-buffer
3227 (let ((queue gdb-input-queue))
3228 (dolist (item queue)
3229 (if (equal (cdr item) '(gdb-assembler-handler))
3230 (setq gdb-input-queue
3231 (delete item gdb-input-queue))))))
3234 (concat gdb-server-prefix "disassemble "
3235 (if (member gdb-pc-address '(nil "main")) nil "0x")
3236 gdb-pc-address "\n")
3237 'gdb-assembler-handler))
3238 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3239 (setq gdb-previous-frame-address gdb-pc-address)
3240 (setq gdb-previous-frame gdb-selected-frame)))))))
3242 (defun gdb-get-selected-frame ()
3243 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3246 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3247 (push 'gdb-get-selected-frame
3248 gdb-pending-triggers))))
3250 (defun gdb-frame-handler ()
3251 (setq gdb-pending-triggers
3252 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3253 (goto-char (point-min))
3254 (when (re-search-forward
3255 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3256 (setq gdb-frame-number (match-string 1))
3257 (setq gdb-frame-address (match-string 2)))
3258 (goto-char (point-min))
3259 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3260 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3262 (setq gdb-selected-frame (match-string 2))
3263 (if (gdb-get-buffer 'gdb-locals-buffer)
3264 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3265 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3266 (if (gdb-get-buffer 'gdb-assembler-buffer)
3267 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3268 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3269 (setq gdb-pc-address (match-string 1))
3270 (if (and (match-string 3) gud-overlay-arrow-position)
3271 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3272 (position (marker-position gud-overlay-arrow-position)))
3274 (string-equal (buffer-name buffer)
3275 (file-name-nondirectory (match-string 3))))
3276 (with-current-buffer buffer
3277 (setq fringe-indicator-alist
3278 (if (string-equal gdb-frame-number "0")
3280 '((overlay-arrow . hollow-right-triangle))))
3281 (set-marker gud-overlay-arrow-position position))))))
3282 (goto-char (point-min))
3283 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3284 (setq gdb-current-language (match-string 1)))
3285 (gdb-invalidate-assembler))
3288 ;; Code specific to GDB 6.4
3289 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3291 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3292 "Create list of source files for current GDB session.
3293 If buffers already exist for any of these files, gud-minor-mode
3295 (goto-char (point-min))
3296 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3297 (push (match-string 1) gdb-source-file-list))
3298 (dolist (buffer (buffer-list))
3299 (with-current-buffer buffer
3300 (when (member buffer-file-name gdb-source-file-list)
3301 (set (make-local-variable 'gud-minor-mode)
3302 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3303 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3304 (when gud-tooltip-mode
3305 (make-local-variable 'gdb-define-alist)
3306 (gdb-create-define-alist)
3307 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3308 (gdb-force-mode-line-update
3309 (propertize "ready" 'face font-lock-variable-name-face)))
3311 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3312 (defun gdb-var-list-children-1 (varnum)
3315 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3316 (concat "server interpreter mi \"-var-list-children --all-values "
3318 (concat "-var-list-children --all-values " varnum "\n"))
3319 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3321 (defconst gdb-var-list-children-regexp-1
3322 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3323 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3325 (defun gdb-var-list-children-handler-1 (varnum)
3326 (goto-char (point-min))
3327 (let ((var-list nil))
3328 (catch 'child-already-watched
3329 (dolist (var gdb-var-list)
3330 (if (string-equal varnum (car var))
3333 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3334 (let ((varchild (list (match-string 1)
3338 (read (match-string 4))
3340 (if (assoc (car varchild) gdb-var-list)
3341 (throw 'child-already-watched nil))
3342 (push varchild var-list))))
3343 (push var var-list)))
3344 (setq gdb-var-list (nreverse var-list))))
3345 (gdb-speedbar-update))
3347 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3348 (defun gdb-var-update-1 ()
3349 (if (not (member 'gdb-var-update gdb-pending-triggers))
3353 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3354 "server interpreter mi \"-var-update --all-values *\"\n"
3355 "-var-update --all-values *\n")
3356 'gdb-var-update-handler-1))
3357 (push 'gdb-var-update gdb-pending-triggers))))
3359 (defconst gdb-var-update-regexp-1
3360 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3361 in_scope=\"\\(.*?\\)\".*?}")
3363 (defun gdb-var-update-handler-1 ()
3364 (dolist (var gdb-var-list)
3365 (setcar (nthcdr 5 var) nil))
3366 (goto-char (point-min))
3367 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3368 (let* ((varnum (match-string 1))
3369 (var (assoc varnum gdb-var-list)))
3371 (if (string-equal (match-string 3) "false")
3372 (setcar (nthcdr 5 var) 'out-of-scope)
3373 (setcar (nthcdr 5 var) 'changed)
3374 (setcar (nthcdr 4 var)
3375 (read (match-string 2)))))))
3376 (setq gdb-pending-triggers
3377 (delq 'gdb-var-update gdb-pending-triggers))
3378 (gdb-speedbar-update))
3380 ;; Registers buffer.
3382 (gdb-set-buffer-rules 'gdb-registers-buffer
3383 'gdb-registers-buffer-name
3384 'gdb-registers-mode)
3386 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3387 (gdb-get-buffer 'gdb-registers-buffer)
3388 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3389 "server interpreter mi \"-data-list-register-values x\"\n"
3390 "-data-list-register-values x\n")
3391 gdb-data-list-register-values-handler)
3393 (defconst gdb-data-list-register-values-regexp
3394 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3396 (defun gdb-data-list-register-values-handler ()
3397 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3398 gdb-pending-triggers))
3399 (goto-char (point-min))
3400 (if (re-search-forward gdb-error-regexp nil t)
3401 (let ((err (match-string 1)))
3402 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3403 (let ((buffer-read-only nil))
3405 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3407 (goto-char (point-min)))))
3408 (let ((register-list (reverse gdb-register-names))
3409 (register nil) (register-string nil) (register-values nil))
3410 (goto-char (point-min))
3411 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3412 (setq register (pop register-list))
3413 (setq register-string (concat register "\t" (match-string 2) "\n"))
3414 (if (member (match-string 1) gdb-changed-registers)
3415 (put-text-property 0 (length register-string)
3416 'face 'font-lock-warning-face
3418 (setq register-values
3419 (concat register-values register-string)))
3420 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3421 (with-current-buffer buf
3422 (let* ((window (get-buffer-window buf 0))
3423 (start (window-start window))
3424 (p (window-point window))
3425 (buffer-read-only nil))
3427 (insert register-values)
3428 (set-window-start window start)
3429 (set-window-point window p))))))
3430 (gdb-data-list-register-values-custom))
3432 (defun gdb-data-list-register-values-custom ()
3433 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3435 (let ((buffer-read-only nil)
3437 (goto-char (point-min))
3438 (while (< (point) (point-max))
3439 (setq start (line-beginning-position))
3440 (setq end (line-end-position))
3441 (when (looking-at "^[^\t]+")
3442 (unless (string-equal (match-string 0) "No registers.")
3443 (put-text-property start (match-end 0)
3444 'face font-lock-variable-name-face)
3445 (add-text-properties start end
3446 '(help-echo "mouse-2: edit value"
3447 mouse-face highlight))))
3448 (forward-line 1))))))
3450 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3451 (defun gdb-get-changed-registers ()
3452 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3453 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3457 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3458 "server interpreter mi -data-list-changed-registers\n"
3459 "-data-list-changed-registers\n")
3460 'gdb-get-changed-registers-handler))
3461 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3463 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3465 (defun gdb-get-changed-registers-handler ()
3466 (setq gdb-pending-triggers
3467 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3468 (setq gdb-changed-registers nil)
3469 (goto-char (point-min))
3470 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3471 (push (match-string 1) gdb-changed-registers)))
3476 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3477 (gdb-set-buffer-rules 'gdb-locals-buffer
3478 'gdb-locals-buffer-name
3481 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3482 (gdb-get-buffer 'gdb-locals-buffer)
3483 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3484 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3485 "-stack-list-locals --simple-values\n")
3486 gdb-stack-list-locals-handler)
3488 (defconst gdb-stack-list-locals-regexp
3489 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3491 (defvar gdb-locals-watch-map-1
3492 (let ((map (make-sparse-keymap)))
3493 (define-key map [mouse-2] 'gud-watch)
3495 "Keymap to create watch expression of a complex data type local variable.")
3497 ;; Dont display values of arrays or structures.
3498 ;; These can be expanded using gud-watch.
3499 (defun gdb-stack-list-locals-handler ()
3500 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3501 gdb-pending-triggers))
3502 (goto-char (point-min))
3503 (if (re-search-forward gdb-error-regexp nil t)
3504 (let ((err (match-string 1)))
3505 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3506 (let ((buffer-read-only nil))
3509 (goto-char (point-min)))))
3510 (let (local locals-list)
3511 (goto-char (point-min))
3512 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3513 (let ((local (list (match-string 1)
3516 (if (looking-at ",value=\\(\".*\"\\).*?}")
3517 (setcar (nthcdr 2 local) (read (match-string 1))))
3518 (push local locals-list)))
3519 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3520 (and buf (with-current-buffer buf
3521 (let* ((window (get-buffer-window buf 0))
3522 (start (window-start window))
3523 (p (window-point window))
3524 (buffer-read-only nil))
3526 (dolist (local locals-list)
3527 (setq name (car local))
3528 (if (or (not (nth 2 local))
3529 (string-match "^\\0x" (nth 2 local)))
3530 (add-text-properties 0 (length name)
3531 `(mouse-face highlight
3532 help-echo "mouse-2: create watch expression"
3533 local-map ,gdb-locals-watch-map-1)
3536 (concat name "\t" (nth 1 local)
3537 "\t" (nth 2 local) "\n")))
3538 (set-window-start window start)
3539 (set-window-point window p))))))))
3541 (defun gdb-get-register-names ()
3542 "Create a list of register names."
3543 (goto-char (point-min))
3544 (setq gdb-register-names nil)
3545 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3546 (push (match-string 1) gdb-register-names)))
3550 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3551 ;;; gdb-ui.el ends here