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 "ready"))
439 (defun gdb-find-watch-expression ()
440 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
441 (varnum (car var)) expr array)
442 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
443 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
444 (component-list (split-string (match-string 2 varnum) "\\." t)))
445 (setq expr (nth 1 var1))
446 (setq varnumlet (car var1))
447 (dolist (component component-list)
448 (setq var2 (assoc varnumlet gdb-var-list))
449 (setq expr (concat expr
450 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
451 (concat "[" component "]")
452 (concat "." component))))
453 (setq varnumlet (concat varnumlet "." component)))
457 (set (make-local-variable 'gud-minor-mode) 'gdba)
458 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
460 (gud-def gud-break (if (not (string-match "Machine" mode-name))
461 (gud-call "break %f:%l" arg)
465 (gud-call "break *%a" arg)))
466 "\C-b" "Set breakpoint at current line or address.")
468 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
469 (gud-call "clear %f:%l" arg)
473 (gud-call "clear *%a" arg)))
474 "\C-d" "Remove breakpoint at current line or address.")
476 (gud-def gud-until (if (not (string-match "Machine" mode-name))
477 (gud-call "until %f:%l" arg)
481 (gud-call "until *%a" arg)))
482 "\C-u" "Continue to current line or address.")
484 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
485 nil "Start or continue execution.")
487 ;; For debugging Emacs only.
491 "pp1 " (if (eq (buffer-local-value
492 'major-mode (window-buffer)) 'speedbar-mode)
493 (gdb-find-watch-expression) "%e")) arg)
494 nil "Print the emacs s-expression.")
496 (define-key gud-minor-mode-map [left-margin mouse-1]
497 'gdb-mouse-set-clear-breakpoint)
498 (define-key gud-minor-mode-map [left-fringe mouse-1]
499 'gdb-mouse-set-clear-breakpoint)
500 (define-key gud-minor-mode-map [left-margin C-mouse-1]
501 'gdb-mouse-toggle-breakpoint-margin)
502 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
503 'gdb-mouse-toggle-breakpoint-fringe)
505 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
507 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
509 (define-key gud-minor-mode-map [left-margin mouse-3]
511 (define-key gud-minor-mode-map [left-fringe mouse-3]
514 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
516 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
518 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
520 (define-key gud-minor-mode-map [left-margin C-mouse-3]
523 (setq comint-input-sender 'gdb-send)
526 (setq gdb-pc-address (if gdb-show-main "main" nil))
527 (setq gdb-previous-frame-address nil
528 gdb-memory-address "main"
529 gdb-previous-frame nil
530 gdb-selected-frame nil
531 gdb-current-language nil
535 gdb-first-post-prompt t
539 gdb-pending-triggers nil
540 gdb-output-sink 'user
541 gdb-server-prefix "server "
542 gdb-flush-pending-output nil
543 gdb-location-alist nil
544 gdb-source-file-list nil
547 gdb-buffer-fringe-width (car (window-fringes))
550 gdb-source-window nil
551 gdb-inferior-status nil
553 gdb-look-up-stack nil
558 (setq gdb-buffer-type 'gdba)
560 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
562 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
563 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
567 (if (eq window-system 'w32)
568 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
569 (gdb-enqueue-input (list "set height 0\n" 'ignore))
570 (gdb-enqueue-input (list "set width 0\n" 'ignore))
572 (if (string-equal gdb-version "pre-6.4")
574 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
575 'gdb-set-gud-minor-mode-existing-buffers))
576 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
578 (list "server interpreter mi -data-list-register-names\n"
579 'gdb-get-register-names))
580 ; Needs GDB 6.2 onwards.
582 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
583 'gdb-set-gud-minor-mode-existing-buffers-1))
584 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
586 ;; Find source file and compilation directory here.
587 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
588 (gdb-enqueue-input (list "server list\n" 'ignore))
589 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
591 (run-hooks 'gdba-mode-hook))
593 (defun gdb-get-version ()
594 (goto-char (point-min))
595 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
596 (setq gdb-version "pre-6.4")
597 (setq gdb-version "6.4+"))
600 (defun gdb-mouse-until (event)
601 "Continue running until a source line past the current line.
602 The destination source line can be selected either by clicking with mouse-2
603 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
605 (if gud-overlay-arrow-position
606 (let ((start (event-start event))
607 (end (event-end event))
608 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
609 (if (not (string-match "Machine" mode-name))
610 (if (equal buffer (window-buffer (posn-window end)))
611 (with-current-buffer buffer
612 (when (or (equal start end)
613 (equal (posn-point start)
615 gud-overlay-arrow-position)))
616 (setq line (line-number-at-pos (posn-point end)))
617 (gud-call (concat "until " (number-to-string line))))))
618 (if (equal (marker-buffer gdb-overlay-arrow-position)
619 (window-buffer (posn-window end)))
620 (when (or (equal start end)
621 (equal (posn-point start)
623 gdb-overlay-arrow-position)))
625 (goto-line (line-number-at-pos (posn-point end)))
627 (gud-call (concat "until *%a")))))))))
629 (defun gdb-mouse-jump (event)
630 "Set execution address/line.
631 The destination source line can be selected either by clicking with mouse-2
632 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
633 Unlike gdb-mouse-until the destination address can be before the current
634 line, and no execution takes place."
636 (if gud-overlay-arrow-position
637 (let ((start (event-start event))
638 (end (event-end event))
639 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
640 (if (not (string-match "Machine" mode-name))
641 (if (equal buffer (window-buffer (posn-window end)))
642 (with-current-buffer buffer
643 (when (or (equal start end)
644 (equal (posn-point start)
646 gud-overlay-arrow-position)))
647 (setq line (line-number-at-pos (posn-point end)))
648 (progn (gud-call (concat "tbreak " (number-to-string line)))
649 (gud-call (concat "jump " (number-to-string line)))))))
650 (if (equal (marker-buffer gdb-overlay-arrow-position)
651 (window-buffer (posn-window end)))
652 (when (or (equal start end)
653 (equal (posn-point start)
655 gdb-overlay-arrow-position)))
657 (goto-line (line-number-at-pos (posn-point end)))
660 (gud-call (concat "tbreak *%a"))
661 (gud-call (concat "jump *%a"))))))))))
663 (defcustom gdb-speedbar-auto-raise nil
664 "If non-nil raise speedbar every time display of watch expressions is\
670 (defun gdb-speedbar-auto-raise (arg)
671 "Toggle automatic raising of the speedbar for watch expressions.
672 With arg, automatically raise speedbar iff arg is positive."
674 (setq gdb-speedbar-auto-raise
676 (not gdb-speedbar-auto-raise)
677 (> (prefix-numeric-value arg) 0)))
678 (message (format "Auto raising %sabled"
679 (if gdb-speedbar-auto-raise "en" "dis"))))
681 (defcustom gdb-use-colon-colon-notation nil
682 "If non-nil use FUN::VAR format to display variables in the speedbar."
687 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
688 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
690 (defun gud-watch (&optional arg event)
691 "Watch expression at point.
692 With arg, enter name of variable to be watched in the minibuffer."
693 (interactive (list current-prefix-arg last-input-event))
694 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
695 (if (memq minor-mode '(gdbmi gdba))
697 (if event (posn-set-point (event-end event)))
699 (save-selected-window
702 (completing-read "Name of variable: "
703 'gud-gdb-complete-command)
704 (if (and transient-mark-mode mark-active)
705 (buffer-substring (region-beginning) (region-end))
706 (tooltip-identifier-from-point (point))))))
708 (catch 'already-watched
709 (dolist (var gdb-var-list)
710 (unless (string-match "\\." (car var))
711 (if (string-equal expr (nth 1 var))
712 (throw 'already-watched nil))))
713 (set-text-properties 0 (length expr) nil expr)
716 (if (eq minor-mode 'gdba)
718 "server interpreter mi \"-var-create - * " expr "\"\n")
719 (concat"-var-create - * " expr "\n"))
720 `(lambda () (gdb-var-create-handler ,expr))))))))
721 (message "gud-watch is a no-op in this mode."))))
723 (defconst gdb-var-create-regexp
724 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",.*type=\"\\(.*?\\)\"")
726 (defun gdb-var-create-handler (expr)
727 (goto-char (point-min))
728 (if (re-search-forward gdb-var-create-regexp nil t)
731 (if (and (string-equal gdb-current-language "c")
732 gdb-use-colon-colon-notation gdb-selected-frame)
733 (setq expr (concat gdb-selected-frame "::" expr))
737 nil nil gdb-frame-address)))
738 (push var gdb-var-list)
739 (unless (string-equal
740 speedbar-initial-expansion-list-name "GUD")
741 (speedbar-change-initial-expansion-list "GUD"))
744 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
745 (concat "server interpreter mi \"0-var-evaluate-expression "
747 (concat "0-var-evaluate-expression " (car var) "\n"))
748 `(lambda () (gdb-var-evaluate-expression-handler
750 (if (search-forward "Undefined command" nil t)
751 (message-box "Watching expressions requires GDB 6.0 onwards")
752 (message-box "No symbol \"%s\" in current context." expr))))
754 (defun gdb-speedbar-update ()
755 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
756 ;; Dummy command to update speedbar even when idle.
757 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
758 ;; Keep gdb-pending-triggers non-nil till end.
759 (push 'gdb-speedbar-timer gdb-pending-triggers)))
761 (defun gdb-speedbar-timer-fn ()
762 (setq gdb-pending-triggers
763 (delq 'gdb-speedbar-timer gdb-pending-triggers))
766 (defun gdb-var-evaluate-expression-handler (varnum changed)
767 (goto-char (point-min))
768 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t)
769 (setq gdb-pending-triggers
770 (delq (string-to-number (match-string 1)) gdb-pending-triggers))
771 (let ((var (assoc varnum gdb-var-list)))
773 (if changed (setcar (nthcdr 5 var) 'changed))
774 (setcar (nthcdr 4 var) (read (match-string 2)))))
775 (gdb-speedbar-update))
777 (defun gdb-var-list-children (varnum)
779 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
780 `(lambda () (gdb-var-list-children-handler ,varnum)))))
782 (defconst gdb-var-list-children-regexp
783 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
784 numchild=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\".*?}")
786 (defun gdb-var-list-children-handler (varnum)
787 (goto-char (point-min))
788 (let ((var-list nil))
789 (catch 'child-already-watched
790 (dolist (var gdb-var-list)
791 (if (string-equal varnum (car var))
794 (while (re-search-forward gdb-var-list-children-regexp nil t)
795 (let ((varchild (list (match-string 1)
800 (if (assoc (car varchild) gdb-var-list)
801 (throw 'child-already-watched nil))
802 (push varchild var-list)
806 "server interpreter mi \"0-var-evaluate-expression "
807 (car varchild) "\"\n")
808 `(lambda () (gdb-var-evaluate-expression-handler
809 ,(car varchild) nil)))))))
810 (push var var-list)))
811 (setq gdb-var-list (nreverse var-list)))))
813 (defun gdb-var-update ()
814 (when (not (member 'gdb-var-update gdb-pending-triggers))
816 (list "server interpreter mi \"-var-update *\"\n"
817 'gdb-var-update-handler))
818 (push 'gdb-var-update gdb-pending-triggers)))
820 (defconst gdb-var-update-regexp
821 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
822 type_changed=\".*?\".*?}")
824 (defun gdb-var-update-handler ()
825 (dolist (var gdb-var-list)
826 (setcar (nthcdr 5 var) nil))
827 (goto-char (point-min))
829 (while (re-search-forward gdb-var-update-regexp nil t)
830 (let ((varnum (match-string 1)))
831 (if (string-equal (match-string 2) "false")
832 (let ((var (assoc varnum gdb-var-list)))
833 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
835 (push n gdb-pending-triggers)
838 (concat "server interpreter mi \"" (number-to-string n)
839 "-var-evaluate-expression " varnum "\"\n")
840 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))))))
841 (setq gdb-pending-triggers
842 (delq 'gdb-var-update gdb-pending-triggers)))
844 (defun gdb-var-delete ()
845 "Delete watch expression at point from the speedbar."
847 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
849 (let ((text (speedbar-line-text)))
850 ;; Can't use \\S-+ for whitespace because
851 ;; speedbar has a whacky syntax table.
852 (string-match "\\([^ \t]+\\)" text)
853 (let ((expr (match-string 1 text)) var varnum)
855 (dolist (var1 gdb-var-list)
856 (when (string-equal expr (nth 1 var1))
858 (setq varnum (car var1))
859 (throw 'expr-found nil))))
860 (unless (string-match "\\." (car var))
863 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
865 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
866 (concat "-var-delete " varnum "\n"))
868 (setq gdb-var-list (delq var gdb-var-list))
869 (dolist (varchild gdb-var-list)
870 (if (string-match (concat (car var) "\\.") (car varchild))
871 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
873 (defun gdb-edit-value (text token indent)
874 "Assign a value to a variable displayed in the speedbar."
875 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
876 (varnum (car var)) (value))
877 (setq value (read-string "New value: "))
880 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
881 (concat "server interpreter mi \"-var-assign "
882 varnum " " value "\"\n")
883 (concat "-var-assign " varnum " " value "\n"))
884 `(lambda () (gdb-edit-value-handler ,value))))))
886 (defun gdb-edit-value-handler (value)
887 (goto-char (point-min))
888 (if (re-search-forward gdb-error-regexp nil t)
889 (message-box "Invalid number or expression (%s)" value)))
891 (defcustom gdb-show-changed-values t
892 "If non-nil change the face of out of scope variables and changed values.
893 Out of scope variables are suppressed with `shadow' face.
894 Changed values are highlighted with the face `font-lock-warning-face'."
899 (defun gdb-speedbar-expand-node (text token indent)
900 "Expand the node the user clicked on.
901 TEXT is the text of the button we clicked on, a + or - item.
902 TOKEN is data related to this node.
903 INDENT is the current indentation depth."
904 (cond ((string-match "+" text) ;expand this node
906 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
907 (string-equal gdb-version "pre-6.4"))
908 (gdb-var-list-children token)
909 (gdb-var-list-children-1 token)))
910 ((string-match "-" text) ;contract this node
911 (dolist (var gdb-var-list)
912 (if (string-match (concat token "\\.") (car var))
913 (setq gdb-var-list (delq var gdb-var-list))))
914 (speedbar-change-expand-button-char ?+)
915 (speedbar-delete-subblock indent))
916 (t (error "Ooops... not sure what to do")))
917 (speedbar-center-buffer-smartly))
919 (defun gdb-get-target-string ()
920 (with-current-buffer gud-comint-buffer
927 ;; Each buffer has a TYPE -- a symbol that identifies the function
928 ;; of that particular buffer.
930 ;; The usual gdb interaction buffer is given the type `gdba' and
931 ;; is constructed specially.
933 ;; Others are constructed by gdb-get-buffer-create and
934 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
936 (defvar gdb-buffer-rules-assoc '())
938 (defun gdb-get-buffer (key)
939 "Return the gdb buffer tagged with type KEY.
940 The key should be one of the cars in `gdb-buffer-rules-assoc'."
942 (gdb-look-for-tagged-buffer key (buffer-list))))
944 (defun gdb-get-buffer-create (key)
945 "Create a new gdb buffer of the type specified by KEY.
946 The key should be one of the cars in `gdb-buffer-rules-assoc'."
947 (or (gdb-get-buffer key)
948 (let* ((rules (assoc key gdb-buffer-rules-assoc))
949 (name (funcall (gdb-rules-name-maker rules)))
950 (new (get-buffer-create name)))
951 (with-current-buffer new
953 (if (cdr (cdr rules))
954 (setq trigger (funcall (car (cdr (cdr rules))))))
955 (setq gdb-buffer-type key)
956 (set (make-local-variable 'gud-minor-mode)
957 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
958 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
959 (if trigger (funcall trigger)))
962 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
964 (defun gdb-look-for-tagged-buffer (key bufs)
966 (while (and (not retval) bufs)
967 (set-buffer (car bufs))
968 (if (eq gdb-buffer-type key)
969 (setq retval (car bufs)))
970 (setq bufs (cdr bufs)))
974 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
975 ;; at least one and possible more functions. The functions have these
976 ;; roles in defining a buffer type:
978 ;; NAME - Return a name for this buffer type.
980 ;; The remaining function(s) are optional:
982 ;; MODE - called in a new buffer with no arguments, should establish
983 ;; the proper mode for the buffer.
986 (defun gdb-set-buffer-rules (buffer-type &rest rules)
987 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
989 (setcdr binding rules)
990 (push (cons buffer-type rules)
991 gdb-buffer-rules-assoc))))
993 ;; GUD buffers are an exception to the rules
994 (gdb-set-buffer-rules 'gdba 'error)
996 ;; Partial-output buffer : This accumulates output from a command executed on
997 ;; behalf of emacs (rather than the user).
999 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1000 'gdb-partial-output-name)
1002 (defun gdb-partial-output-name ()
1003 (concat "*partial-output-"
1004 (gdb-get-target-string)
1008 (gdb-set-buffer-rules 'gdb-inferior-io
1009 'gdb-inferior-io-name
1010 'gdb-inferior-io-mode)
1012 (defun gdb-inferior-io-name ()
1013 (concat "*input/output of "
1014 (gdb-get-target-string)
1017 (defun gdb-display-separate-io-buffer ()
1018 "Display IO of debugged program in a separate window."
1020 (if gdb-use-separate-io-buffer
1022 (gdb-get-buffer-create 'gdb-inferior-io))))
1024 (defconst gdb-frame-parameters
1025 '((height . 14) (width . 80)
1027 (tool-bar-lines . nil)
1028 (menu-bar-lines . nil)
1029 (minibuffer . nil)))
1031 (defun gdb-frame-separate-io-buffer ()
1032 "Display IO of inferior in a new frame."
1034 (if gdb-use-separate-io-buffer
1035 (let ((special-display-regexps (append special-display-regexps '(".*")))
1036 (special-display-frame-alist gdb-frame-parameters))
1037 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1039 (defvar gdb-inferior-io-mode-map
1040 (let ((map (make-sparse-keymap)))
1041 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1042 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1043 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1044 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1045 (define-key map "\C-d" 'gdb-separate-io-eof)
1048 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1049 "Major mode for gdb inferior-io."
1050 :syntax-table nil :abbrev-table nil
1051 ;; We want to use comint because it has various nifty and familiar
1052 ;; features. We don't need a process, but comint wants one, so create
1054 (make-comint-in-buffer
1055 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1056 (current-buffer) "hexl")
1057 (setq comint-input-sender 'gdb-inferior-io-sender))
1059 (defun gdb-inferior-io-sender (proc string)
1060 ;; PROC is the pseudo-process created to satisfy comint.
1061 (with-current-buffer (process-buffer proc)
1062 (setq proc (get-buffer-process gud-comint-buffer))
1063 (process-send-string proc string)
1064 (process-send-string proc "\n")))
1066 (defun gdb-separate-io-interrupt ()
1067 "Interrupt the program being debugged."
1070 (get-buffer-process gud-comint-buffer) comint-ptyp))
1072 (defun gdb-separate-io-quit ()
1073 "Send quit signal to the program being debugged."
1076 (get-buffer-process gud-comint-buffer) comint-ptyp))
1078 (defun gdb-separate-io-stop ()
1079 "Stop the program being debugged."
1082 (get-buffer-process gud-comint-buffer) comint-ptyp))
1084 (defun gdb-separate-io-eof ()
1085 "Send end-of-file to the program being debugged."
1088 (get-buffer-process gud-comint-buffer)))
1091 ;; gdb communications
1094 ;; INPUT: things sent to gdb
1096 ;; The queues are lists. Each element is either a string (indicating user or
1097 ;; user-like input) or a list of the form:
1099 ;; (INPUT-STRING HANDLER-FN)
1101 ;; The handler function will be called from the partial-output buffer when the
1102 ;; command completes. This is the way to write commands which invoke gdb
1103 ;; commands autonomously.
1105 ;; These lists are consumed tail first.
1108 (defun gdb-send (proc string)
1109 "A comint send filter for gdb.
1110 This filter may simply queue input for a later time."
1111 (with-current-buffer gud-comint-buffer
1112 (let ((inhibit-read-only t))
1113 (remove-text-properties (point-min) (point-max) '(face))))
1116 (let ((item (concat string "\n")))
1117 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1118 (process-send-string proc item)))
1119 (if (and (string-match "\\\\$" string)
1120 (not comint-input-sender-no-newline)) ;;Try to catch C-d.
1121 (setq gdb-continuation (concat gdb-continuation string "\n"))
1122 (let ((item (concat gdb-continuation string "\n")))
1123 (gdb-enqueue-input item)
1124 (setq gdb-continuation nil)))))
1126 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1127 ;; is a query, or other non-top-level prompt.
1129 (defun gdb-enqueue-input (item)
1130 (if (not gud-running)
1133 (gdb-send-item item)
1134 (setq gdb-prompting nil))
1135 (push item gdb-input-queue))))
1137 (defun gdb-dequeue-input ()
1138 (let ((queue gdb-input-queue))
1140 (let ((last (car (last queue))))
1141 (unless (nbutlast queue) (setq gdb-input-queue '()))
1144 (defun gdb-send-item (item)
1145 (setq gdb-flush-pending-output nil)
1146 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1147 (setq gdb-current-item item)
1148 (let ((process (get-buffer-process gud-comint-buffer)))
1149 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1152 (setq gdb-output-sink 'user)
1153 (process-send-string process item))
1155 (gdb-clear-partial-output)
1156 (setq gdb-output-sink 'pre-emacs)
1157 (process-send-string process
1159 ;; case: eq gud-minor-mode 'gdbmi
1160 (gdb-clear-partial-output)
1161 (setq gdb-output-sink 'emacs)
1162 (process-send-string process (car item)))))
1165 ;; output -- things gdb prints to emacs
1167 ;; GDB output is a stream interrupted by annotations.
1168 ;; Annotations can be recognized by their beginning
1169 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1171 ;; The tag is a string obeying symbol syntax.
1173 ;; The optional part `<opt>' can be either the empty string
1174 ;; or a space followed by more data relating to the annotation.
1175 ;; For example, the SOURCE annotation is followed by a filename,
1176 ;; line number and various useless goo. This data must not include
1180 (defcustom gud-gdba-command-name "gdb -annotate=3"
1181 "Default command to execute an executable under the GDB-UI debugger."
1186 (defvar gdb-annotation-rules
1187 '(("pre-prompt" gdb-pre-prompt)
1188 ("prompt" gdb-prompt)
1189 ("commands" gdb-subprompt)
1190 ("overload-choice" gdb-subprompt)
1191 ("query" gdb-subprompt)
1192 ;; Need this prompt for GDB 6.1
1193 ("nquery" gdb-subprompt)
1194 ("prompt-for-continue" gdb-subprompt)
1195 ("post-prompt" gdb-post-prompt)
1196 ("source" gdb-source)
1197 ("starting" gdb-starting)
1198 ("exited" gdb-exited)
1199 ("signalled" gdb-signalled)
1200 ("signal" gdb-signal)
1201 ("breakpoint" gdb-stopping)
1202 ("watchpoint" gdb-stopping)
1203 ("frame-begin" gdb-frame-begin)
1204 ("stopped" gdb-stopped)
1205 ("error-begin" gdb-error)
1207 ) "An assoc mapping annotation tags to functions which process them.")
1210 (setq gdb-flush-pending-output t)
1211 (setq gud-running nil)
1212 (gdb-force-mode-line-update "stopped")
1213 (setq gdb-output-sink 'user)
1214 (setq gdb-input-queue nil)
1215 (setq gdb-pending-triggers nil)
1216 (setq gdb-prompting t))
1218 (defconst gdb-source-spec-regexp
1219 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1221 ;; Do not use this except as an annotation handler.
1222 (defun gdb-source (args)
1223 (string-match gdb-source-spec-regexp args)
1224 ;; Extract the frame position from the marker.
1225 (setq gud-last-frame
1227 (match-string 1 args)
1228 (string-to-number (match-string 2 args))))
1229 (setq gdb-pc-address (match-string 3 args))
1230 ;; cover for auto-display output which comes *before*
1231 ;; stopped annotation
1232 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1234 (defun gdb-pre-prompt (ignored)
1235 "An annotation handler for `pre-prompt'.
1236 This terminates the collection of output from a previous command if that
1237 happens to be in effect."
1238 (setq gdb-error nil)
1239 (let ((sink gdb-output-sink))
1243 (setq gdb-output-sink 'post-emacs))
1246 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1248 (defun gdb-prompt (ignored)
1249 "An annotation handler for `prompt'.
1250 This sends the next command (if any) to gdb."
1251 (when gdb-first-prompt
1252 (gdb-force-mode-line-update "initializing...")
1254 (setq gdb-first-prompt nil))
1255 (let ((sink gdb-output-sink))
1258 ((eq sink 'post-emacs)
1259 (setq gdb-output-sink 'user)
1261 (car (cdr gdb-current-item))))
1262 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1263 (funcall handler))))
1266 (error "Phase error in gdb-prompt (got %s)" sink))))
1267 (let ((input (gdb-dequeue-input)))
1269 (gdb-send-item input)
1271 (setq gdb-prompting t)
1272 (gud-display-frame)))))
1274 (defun gdb-subprompt (ignored)
1275 "An annotation handler for non-top-level prompts."
1276 (setq gdb-prompting t))
1278 (defun gdb-starting (ignored)
1279 "An annotation handler for `starting'.
1280 This says that I/O for the subprocess is now the program being debugged,
1282 (setq gdb-active-process t)
1283 (setq gdb-printing t)
1284 (let ((sink gdb-output-sink))
1288 (setq gud-running t)
1289 (setq gdb-inferior-status "running")
1290 (gdb-force-mode-line-update gdb-inferior-status)
1291 (gdb-remove-text-properties)
1292 (setq gud-old-arrow gud-overlay-arrow-position)
1293 (setq gud-overlay-arrow-position nil)
1294 (setq gdb-overlay-arrow-position nil)
1295 (if gdb-use-separate-io-buffer
1296 (setq gdb-output-sink 'inferior))))
1299 (error "Unexpected `starting' annotation")))))
1301 (defun gdb-signal (ignored)
1302 (setq gdb-inferior-status "signal")
1303 (gdb-force-mode-line-update gdb-inferior-status)
1304 (gdb-stopping ignored))
1306 (defun gdb-stopping (ignored)
1307 "An annotation handler for `breakpoint' and other annotations.
1308 They say that I/O for the subprocess is now GDB, not the program
1310 (if gdb-use-separate-io-buffer
1311 (let ((sink gdb-output-sink))
1313 ((eq sink 'inferior)
1314 (setq gdb-output-sink 'user))
1317 (error "Unexpected stopping annotation"))))))
1319 (defun gdb-exited (ignored)
1320 "An annotation handler for `exited' and `signalled'.
1321 They say that I/O for the subprocess is now GDB, not the program
1322 being debugged and that the program is no longer running. This
1323 function is used to change the focus of GUD tooltips to #define
1325 (setq gdb-active-process nil)
1326 (setq gud-overlay-arrow-position nil)
1327 (setq gdb-overlay-arrow-position nil)
1328 (setq gud-old-arrow nil)
1329 (setq gdb-inferior-status "exited")
1330 (gdb-force-mode-line-update gdb-inferior-status)
1331 (gdb-stopping ignored))
1333 (defun gdb-signalled (ignored)
1334 (setq gdb-signalled t))
1336 (defun gdb-frame-begin (ignored)
1337 (setq gdb-frame-begin t)
1338 (setq gdb-printing nil)
1339 (let ((sink gdb-output-sink))
1341 ((eq sink 'inferior)
1342 (setq gdb-output-sink 'user))
1344 ((eq sink 'emacs) t)
1347 (error "Unexpected frame-begin annotation (%S)" sink)))))
1349 (defcustom gdb-same-frame focus-follows-mouse
1350 "Non-nil means pop up GUD buffer in same frame."
1355 (defun gdb-stopped (ignored)
1356 "An annotation handler for `stopped'.
1357 It is just like `gdb-stopping', except that if we already set the output
1358 sink to `user' in `gdb-stopping', that is fine."
1359 (setq gud-running nil)
1360 (unless (or gud-overlay-arrow-position gud-last-frame)
1361 (if (and gdb-frame-begin gdb-printing)
1362 (setq gud-overlay-arrow-position gud-old-arrow)
1363 ;;Pop up GUD buffer to display current frame when it doesn't have source
1364 ;;information i.e id not compiled with -g as with libc routines generally.
1366 (gdb-display-gdb-buffer)
1367 (gdb-frame-gdb-buffer))
1368 ;;Try to find source further up stack e.g after signal.
1369 (setq gdb-look-up-stack
1370 (if (gdb-get-buffer 'gdb-stack-buffer)
1373 (gdb-get-buffer-create 'gdb-stack-buffer)
1374 (gdb-invalidate-frames)
1376 (unless (member gdb-inferior-status '("exited" "signal"))
1377 (setq gdb-inferior-status "stopped")
1378 (gdb-force-mode-line-update gdb-inferior-status))
1379 (let ((sink gdb-output-sink))
1381 ((eq sink 'inferior)
1382 (setq gdb-output-sink 'user))
1386 (error "Unexpected stopped annotation"))))
1387 (if gdb-signalled (gdb-exited ignored)))
1389 (defun gdb-error (ignored)
1390 (setq gdb-error (not gdb-error)))
1392 (defun gdb-post-prompt (ignored)
1393 "An annotation handler for `post-prompt'.
1394 This begins the collection of output from the current command if that
1395 happens to be appropriate."
1396 ;; Don't add to queue if there outstanding items or gdb-version is not known
1398 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1399 (gdb-get-selected-frame)
1400 (gdb-invalidate-frames)
1401 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1402 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1403 (gdb-invalidate-breakpoints)
1404 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1405 ;; so gdb-pc-address is updated.
1406 ;; (gdb-invalidate-assembler)
1408 (if (string-equal gdb-version "pre-6.4")
1409 (gdb-invalidate-registers)
1410 (gdb-get-changed-registers)
1411 (gdb-invalidate-registers-1))
1413 (gdb-invalidate-memory)
1414 (if (string-equal gdb-version "pre-6.4")
1415 (gdb-invalidate-locals)
1416 (gdb-invalidate-locals-1))
1418 (gdb-invalidate-threads)
1419 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1420 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1421 ;; Only needed/used with speedbar/watch expressions.
1422 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1423 (if (string-equal gdb-version "pre-6.4")
1425 (gdb-var-update-1)))))
1426 (setq gdb-first-post-prompt nil)
1427 (let ((sink gdb-output-sink))
1430 ((eq sink 'pre-emacs)
1431 (setq gdb-output-sink 'emacs))
1434 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1436 (defconst gdb-buffer-list
1437 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1439 (defun gdb-remove-text-properties ()
1440 (dolist (buffertype gdb-buffer-list)
1441 (let ((buffer (gdb-get-buffer buffertype)))
1443 (with-current-buffer buffer
1444 (let ((inhibit-read-only t))
1445 (remove-text-properties
1446 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1448 ;; GUD displays the selected GDB frame. This might might not be the current
1449 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1450 ;; visited breakpoint is, use that window.
1451 (defun gdb-display-source-buffer (buffer)
1452 (let* ((last-window (if gud-last-last-frame
1454 (gud-find-file (car gud-last-last-frame)))))
1455 (source-window (or last-window
1456 (if (and gdb-source-window
1457 (window-live-p gdb-source-window))
1458 gdb-source-window))))
1460 (setq gdb-source-window source-window)
1461 (set-window-buffer source-window buffer))
1464 (defun gud-gdba-marker-filter (string)
1465 "A gud marker filter for gdb. Handle a burst of output from GDB."
1466 (if gdb-flush-pending-output
1468 (when gdb-enable-debug
1469 (push (cons 'recv string) gdb-debug-ring)
1470 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1471 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1472 ;; Recall the left over gud-marker-acc from last time.
1473 (setq gud-marker-acc (concat gud-marker-acc string))
1474 ;; Start accumulating output for the GUD buffer.
1477 ;; Process all the complete markers in this chunk.
1478 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1479 (let ((annotation (match-string 1 gud-marker-acc)))
1481 ;; Stuff prior to the match is just ordinary output.
1482 ;; It is either concatenated to OUTPUT or directed
1487 (substring gud-marker-acc 0 (match-beginning 0))))
1489 ;; Take that stuff off the gud-marker-acc.
1490 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1492 ;; Parse the tag from the annotation, and maybe its arguments.
1493 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1494 (let* ((annotation-type (match-string 1 annotation))
1495 (annotation-arguments (match-string 2 annotation))
1496 (annotation-rule (assoc annotation-type
1497 gdb-annotation-rules)))
1498 ;; Call the handler for this annotation.
1500 (funcall (car (cdr annotation-rule))
1501 annotation-arguments)
1502 ;; Else the annotation is not recognized. Ignore it silently,
1503 ;; so that GDB can add new annotations without causing
1507 ;; Does the remaining text end in a partial line?
1508 ;; If it does, then keep part of the gud-marker-acc until we get more.
1509 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1512 ;; Everything before the potential marker start can be output.
1514 (gdb-concat-output output
1515 (substring gud-marker-acc 0
1516 (match-beginning 0))))
1518 ;; Everything after, we save, to combine with later input.
1519 (setq gud-marker-acc (substring gud-marker-acc
1520 (match-beginning 0))))
1522 ;; In case we know the gud-marker-acc contains no partial annotations:
1524 (setq output (gdb-concat-output output gud-marker-acc))
1525 (setq gud-marker-acc "")))
1528 (defun gdb-concat-output (so-far new)
1530 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1531 (let ((sink gdb-output-sink))
1533 ((eq sink 'user) (concat so-far new))
1534 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1536 (gdb-append-to-partial-output new)
1538 ((eq sink 'inferior)
1539 (gdb-append-to-inferior-io new)
1543 (error "Bogon output sink %S" sink)))))
1545 (defun gdb-append-to-partial-output (string)
1546 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1547 (goto-char (point-max))
1550 (defun gdb-clear-partial-output ()
1551 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1554 (defun gdb-append-to-inferior-io (string)
1555 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1556 (goto-char (point-max))
1557 (insert-before-markers string))
1558 (if (not (string-equal string ""))
1559 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1561 (defun gdb-clear-inferior-io ()
1562 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1566 ;; One trick is to have a command who's output is always available in a buffer
1567 ;; of it's own, and is always up to date. We build several buffers of this
1570 ;; There are two aspects to this: gdb has to tell us when the output for that
1571 ;; command might have changed, and we have to be able to run the command
1572 ;; behind the user's back.
1574 ;; The output phasing associated with the variable gdb-output-sink
1575 ;; help us to run commands behind the user's back.
1577 ;; Below is the code for specificly managing buffers of output from one
1581 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1582 ;; It adds an input for the command we are tracking. It should be the
1583 ;; annotation rule binding of whatever gdb sends to tell us this command
1584 ;; might have changed it's output.
1586 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1587 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1588 ;; input in the input queue (see comment about ``gdb communications'' above).
1590 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1592 `(defun ,name (&optional ignored)
1593 (if (and ,demand-predicate
1595 gdb-pending-triggers)))
1598 (list ,gdb-command ',output-handler))
1599 (push ',name gdb-pending-triggers)))))
1601 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1603 (setq gdb-pending-triggers
1605 gdb-pending-triggers))
1606 (let ((buf (gdb-get-buffer ',buf-key)))
1608 (with-current-buffer buf
1609 (let* ((window (get-buffer-window buf 0))
1610 (start (window-start window))
1611 (p (window-point window))
1612 (buffer-read-only nil))
1614 (insert-buffer-substring (gdb-get-buffer-create
1615 'gdb-partial-output-buffer))
1616 (set-window-start window start)
1617 (set-window-point window p)))))
1618 ;; put customisation here
1621 (defmacro def-gdb-auto-updated-buffer (buffer-key
1622 trigger-name gdb-command
1623 output-handler-name custom-defun)
1625 (def-gdb-auto-update-trigger ,trigger-name
1626 ;; The demand predicate:
1627 (gdb-get-buffer ',buffer-key)
1629 ,output-handler-name)
1630 (def-gdb-auto-update-handler ,output-handler-name
1631 ,trigger-name ,buffer-key ,custom-defun)))
1635 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1637 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1638 'gdb-breakpoints-buffer-name
1639 'gdb-breakpoints-mode)
1641 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1642 ;; This defines the auto update rule for buffers of type
1643 ;; `gdb-breakpoints-buffer'.
1645 ;; It defines a function to serve as the annotation handler that
1646 ;; handles the `foo-invalidated' message. That function is called:
1647 gdb-invalidate-breakpoints
1649 ;; To update the buffer, this command is sent to gdb.
1650 "server info breakpoints\n"
1652 ;; This also defines a function to be the handler for the output
1653 ;; from the command above. That function will copy the output into
1654 ;; the appropriately typed buffer. That function will be called:
1655 gdb-info-breakpoints-handler
1656 ;; buffer specific functions
1657 gdb-info-breakpoints-custom)
1659 (defconst breakpoint-xpm-data
1661 static char *magick[] = {
1662 /* columns rows colors chars-per-pixel */
1678 "XPM data used for breakpoint icon.")
1680 (defconst breakpoint-enabled-pbm-data
1683 0 0 0 0 1 1 1 1 0 0 0 0
1684 0 0 0 1 1 1 1 1 1 0 0 0
1685 0 0 1 1 1 1 1 1 1 1 0 0
1686 0 1 1 1 1 1 1 1 1 1 1 0
1687 0 1 1 1 1 1 1 1 1 1 1 0
1688 0 1 1 1 1 1 1 1 1 1 1 0
1689 0 1 1 1 1 1 1 1 1 1 1 0
1690 0 0 1 1 1 1 1 1 1 1 0 0
1691 0 0 0 1 1 1 1 1 1 0 0 0
1692 0 0 0 0 1 1 1 1 0 0 0 0"
1693 "PBM data used for enabled breakpoint icon.")
1695 (defconst breakpoint-disabled-pbm-data
1707 0 0 0 1 0 1 0 1 0 0"
1708 "PBM data used for disabled breakpoint icon.")
1710 (defvar breakpoint-enabled-icon nil
1711 "Icon for enabled breakpoint in display margin.")
1713 (defvar breakpoint-disabled-icon nil
1714 "Icon for disabled breakpoint in display margin.")
1716 (and (display-images-p)
1717 ;; Bitmap for breakpoint in fringe
1718 (define-fringe-bitmap 'breakpoint
1719 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1720 ;; Bitmap for gud-overlay-arrow in fringe
1721 (define-fringe-bitmap 'hollow-right-triangle
1722 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1724 (defface breakpoint-enabled
1728 "Face for enabled breakpoint icon in fringe."
1731 (defface breakpoint-disabled
1732 ;; We use different values of grey for different background types,
1733 ;; so that on low-color displays it will end up as something visible
1734 ;; if it has to be approximated.
1735 '((t :foreground "grey70"))
1736 "Face for disabled breakpoint icon in fringe."
1739 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1740 (defun gdb-info-breakpoints-custom ()
1741 (let ((flag) (bptno))
1742 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1743 (dolist (buffer (buffer-list))
1744 (with-current-buffer buffer
1745 (if (and (memq gud-minor-mode '(gdba gdbmi))
1746 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1747 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1748 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1750 (goto-char (point-min))
1751 (while (< (point) (- (point-max) 1))
1753 (if (looking-at "[^\t].*?breakpoint")
1755 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1756 (setq bptno (match-string 1))
1757 (setq flag (char-after (match-beginning 2)))
1759 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1761 (let ((buffer-read-only nil))
1762 (add-text-properties (match-beginning 1) (match-end 1)
1763 '(face font-lock-function-name-face)))
1764 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1765 (let ((line (match-string 2)) (buffer-read-only nil)
1766 (file (match-string 1)))
1767 (add-text-properties (line-beginning-position)
1769 '(mouse-face highlight
1770 help-echo "mouse-2, RET: visit breakpoint"))
1771 (unless (file-exists-p file)
1772 (setq file (cdr (assoc bptno gdb-location-alist))))
1774 (not (string-equal file "File not found")))
1775 (with-current-buffer
1776 (find-file-noselect file 'nowarn)
1777 (set (make-local-variable 'gud-minor-mode)
1779 (set (make-local-variable 'tool-bar-map)
1781 ;; Only want one breakpoint icon at each
1784 (goto-line (string-to-number line))
1785 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1788 (concat gdb-server-prefix "list "
1789 (match-string-no-properties 1) ":1\n")
1792 (list (concat gdb-server-prefix "info source\n")
1793 `(lambda () (gdb-get-location
1794 ,bptno ,line ,flag))))))))))
1796 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1798 (defun gdb-mouse-set-clear-breakpoint (event)
1799 "Set/clear breakpoint in left fringe/margin with mouse click."
1801 (mouse-minibuffer-check event)
1802 (let ((posn (event-end event)))
1803 (if (numberp (posn-point posn))
1804 (with-selected-window (posn-window posn)
1806 (goto-char (posn-point posn))
1807 (if (or (posn-object posn)
1808 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1811 (gud-break nil)))))))
1813 (defun gdb-mouse-toggle-breakpoint-margin (event)
1814 "Enable/disable breakpoint in left margin with mouse click."
1816 (mouse-minibuffer-check event)
1817 (let ((posn (event-end event)))
1818 (if (numberp (posn-point posn))
1819 (with-selected-window (posn-window posn)
1821 (goto-char (posn-point posn))
1822 (if (posn-object posn)
1825 (let ((bptno (get-text-property
1826 0 'gdb-bptno (car (posn-string posn)))))
1827 (concat gdb-server-prefix
1828 (if (get-text-property
1829 0 'gdb-enabled (car (posn-string posn)))
1835 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1836 "Enable/disable breakpoint in left fringe with mouse click."
1838 (mouse-minibuffer-check event)
1839 (let* ((posn (event-end event))
1840 (pos (posn-point posn))
1843 (with-selected-window (posn-window posn)
1845 (set-buffer (window-buffer (selected-window)))
1847 (dolist (overlay (overlays-in pos pos))
1848 (when (overlay-get overlay 'put-break)
1849 (setq obj (overlay-get overlay 'before-string))))
1853 (concat gdb-server-prefix
1854 (if (get-text-property 0 'gdb-enabled obj)
1857 (get-text-property 0 'gdb-bptno obj) "\n")
1860 (defun gdb-breakpoints-buffer-name ()
1861 (with-current-buffer gud-comint-buffer
1862 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1864 (defun gdb-display-breakpoints-buffer ()
1865 "Display status of user-settable breakpoints."
1868 (gdb-get-buffer-create 'gdb-breakpoints-buffer)))
1870 (defun gdb-frame-breakpoints-buffer ()
1871 "Display status of user-settable breakpoints in a new frame."
1873 (let ((special-display-regexps (append special-display-regexps '(".*")))
1874 (special-display-frame-alist gdb-frame-parameters))
1875 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1877 (defvar gdb-breakpoints-mode-map
1878 (let ((map (make-sparse-keymap))
1879 (menu (make-sparse-keymap "Breakpoints")))
1880 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1881 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1882 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1883 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1884 (suppress-keymap map)
1885 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1886 (define-key map " " 'gdb-toggle-breakpoint)
1887 (define-key map "D" 'gdb-delete-breakpoint)
1888 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1889 (define-key map "q" 'gdb-delete-frame-or-window)
1890 (define-key map "\r" 'gdb-goto-breakpoint)
1891 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1892 (define-key map [follow-link] 'mouse-face)
1895 (defun gdb-delete-frame-or-window ()
1896 "Delete frame if there is only one window. Otherwise delete the window."
1898 (if (one-window-p) (delete-frame)
1901 (defun gdb-breakpoints-mode ()
1902 "Major mode for gdb breakpoints.
1904 \\{gdb-breakpoints-mode-map}"
1905 (kill-all-local-variables)
1906 (setq major-mode 'gdb-breakpoints-mode)
1907 (setq mode-name "Breakpoints")
1908 (use-local-map gdb-breakpoints-mode-map)
1909 (setq buffer-read-only t)
1910 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1911 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1912 'gdb-invalidate-breakpoints
1913 'gdbmi-invalidate-breakpoints))
1915 (defconst gdb-breakpoint-regexp
1916 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1918 (defun gdb-toggle-breakpoint ()
1919 "Enable/disable breakpoint at current line."
1922 (beginning-of-line 1)
1923 (if (looking-at gdb-breakpoint-regexp)
1926 (concat gdb-server-prefix
1927 (if (eq ?y (char-after (match-beginning 2)))
1930 (match-string 1) "\n") 'ignore))
1931 (error "Not recognized as break/watchpoint line"))))
1933 (defun gdb-delete-breakpoint ()
1934 "Delete the breakpoint at current line."
1936 (beginning-of-line 1)
1937 (if (looking-at gdb-breakpoint-regexp)
1940 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1941 (error "Not recognized as break/watchpoint line")))
1943 (defun gdb-goto-breakpoint (&optional event)
1944 "Display the breakpoint location specified at current line."
1945 (interactive (list last-input-event))
1946 (if event (posn-set-point (event-end event)))
1948 (beginning-of-line 1)
1949 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1950 (let ((bptno (match-string 1))
1951 (file (match-string 2))
1952 (line (match-string 3)))
1953 (save-selected-window
1954 (let* ((buffer (find-file-noselect
1955 (if (file-exists-p file) file
1956 (cdr (assoc bptno gdb-location-alist)))))
1957 (window (or (gdb-display-source-buffer buffer)
1958 (display-buffer buffer))))
1959 (setq gdb-source-window window)
1960 (with-current-buffer buffer
1961 (goto-line (string-to-number line))
1962 (set-window-point window (point))))))
1963 (error "No location specified."))))
1966 ;; Frames buffer. This displays a perpetually correct bactracktrace
1967 ;; (from the command `where').
1969 ;; Alas, if your stack is deep, it is costly.
1971 (gdb-set-buffer-rules 'gdb-stack-buffer
1972 'gdb-stack-buffer-name
1975 (def-gdb-auto-updated-buffer gdb-stack-buffer
1976 gdb-invalidate-frames
1977 "server info stack\n"
1978 gdb-info-stack-handler
1979 gdb-info-stack-custom)
1981 (defun gdb-info-stack-custom ()
1982 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1984 (unless (eq gdb-look-up-stack 'delete)
1985 (let ((buffer-read-only nil)
1987 (goto-char (point-min))
1988 (while (< (point) (point-max))
1989 (setq bl (line-beginning-position)
1990 el (line-end-position))
1991 (when (looking-at "#")
1992 (add-text-properties bl el
1993 '(mouse-face highlight
1994 help-echo "mouse-2, RET: Select frame")))
1996 (when (looking-at "^#\\([0-9]+\\)")
1997 (when (string-equal (match-string 1) gdb-frame-number)
1998 (put-text-property bl (+ bl 4)
1999 'face '(:inverse-video t)))
2000 (when (re-search-forward
2002 (if (string-equal (match-string 1) "0") "" " in ")
2003 "\\([^ ]+\\) (") el t)
2004 (put-text-property (match-beginning 1) (match-end 1)
2005 'face font-lock-function-name-face)
2006 (setq bl (match-end 0))
2007 (while (re-search-forward "<\\([^>]+\\)>" el t)
2008 (put-text-property (match-beginning 1) (match-end 1)
2009 'face font-lock-function-name-face))
2011 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2012 (put-text-property (match-beginning 1) (match-end 1)
2013 'face font-lock-variable-name-face))))
2015 (when gdb-look-up-stack
2016 (goto-char (point-min))
2017 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2018 (let ((start (line-beginning-position))
2019 (file (match-string 1))
2020 (line (match-string 2)))
2021 (re-search-backward "^#*\\([0-9]+\\)" start t)
2023 (list (concat gdb-server-prefix "frame "
2024 (match-string 1) "\n") 'gdb-set-hollow))
2026 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2027 (if (eq gdb-look-up-stack 'delete)
2028 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2029 (setq gdb-look-up-stack nil))
2031 (defun gdb-set-hollow ()
2032 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2033 (setq fringe-indicator-alist
2034 '((overlay-arrow . hollow-right-triangle)))))
2036 (defun gdb-stack-buffer-name ()
2037 (with-current-buffer gud-comint-buffer
2038 (concat "*stack frames of " (gdb-get-target-string) "*")))
2040 (defun gdb-display-stack-buffer ()
2041 "Display backtrace of current stack."
2044 (gdb-get-buffer-create 'gdb-stack-buffer)))
2046 (defun gdb-frame-stack-buffer ()
2047 "Display backtrace of current stack in a new frame."
2049 (let ((special-display-regexps (append special-display-regexps '(".*")))
2050 (special-display-frame-alist gdb-frame-parameters))
2051 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2053 (defvar gdb-frames-mode-map
2054 (let ((map (make-sparse-keymap)))
2055 (suppress-keymap map)
2056 (define-key map "q" 'kill-this-buffer)
2057 (define-key map "\r" 'gdb-frames-select)
2058 (define-key map [mouse-2] 'gdb-frames-select)
2059 (define-key map [follow-link] 'mouse-face)
2062 (defun gdb-frames-mode ()
2063 "Major mode for gdb call stack.
2065 \\{gdb-frames-mode-map}"
2066 (kill-all-local-variables)
2067 (setq major-mode 'gdb-frames-mode)
2068 (setq mode-name "Frames")
2069 (setq buffer-read-only t)
2070 (use-local-map gdb-frames-mode-map)
2071 (run-mode-hooks 'gdb-frames-mode-hook)
2072 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2073 'gdb-invalidate-frames
2074 'gdbmi-invalidate-frames))
2076 (defun gdb-get-frame-number ()
2079 (let* ((start (line-beginning-position))
2080 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2081 (n (or (and pos (match-string-no-properties 1)) "0")))
2084 (defun gdb-frames-select (&optional event)
2085 "Select the frame and display the relevant source."
2086 (interactive (list last-input-event))
2087 (if event (posn-set-point (event-end event)))
2089 (list (concat gdb-server-prefix "frame "
2090 (gdb-get-frame-number) "\n") 'ignore)))
2093 ;; Threads buffer. This displays a selectable thread list.
2095 (gdb-set-buffer-rules 'gdb-threads-buffer
2096 'gdb-threads-buffer-name
2099 (def-gdb-auto-updated-buffer gdb-threads-buffer
2100 gdb-invalidate-threads
2101 (concat gdb-server-prefix "info threads\n")
2102 gdb-info-threads-handler
2103 gdb-info-threads-custom)
2105 (defun gdb-info-threads-custom ()
2106 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2107 (let ((buffer-read-only nil))
2109 (goto-char (point-min))
2110 (while (< (point) (point-max))
2111 (unless (looking-at "No ")
2112 (add-text-properties (line-beginning-position) (line-end-position)
2113 '(mouse-face highlight
2114 help-echo "mouse-2, RET: select thread")))
2115 (forward-line 1))))))
2117 (defun gdb-threads-buffer-name ()
2118 (with-current-buffer gud-comint-buffer
2119 (concat "*threads of " (gdb-get-target-string) "*")))
2121 (defun gdb-display-threads-buffer ()
2122 "Display IDs of currently known threads."
2125 (gdb-get-buffer-create 'gdb-threads-buffer)))
2127 (defun gdb-frame-threads-buffer ()
2128 "Display IDs of currently known threads in a new frame."
2130 (let ((special-display-regexps (append special-display-regexps '(".*")))
2131 (special-display-frame-alist gdb-frame-parameters))
2132 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2134 (defvar gdb-threads-mode-map
2135 (let ((map (make-sparse-keymap)))
2136 (suppress-keymap map)
2137 (define-key map "q" 'kill-this-buffer)
2138 (define-key map "\r" 'gdb-threads-select)
2139 (define-key map [mouse-2] 'gdb-threads-select)
2140 (define-key map [follow-link] 'mouse-face)
2143 (defvar gdb-threads-font-lock-keywords
2144 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2145 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2146 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2147 "Font lock keywords used in `gdb-threads-mode'.")
2149 (defun gdb-threads-mode ()
2150 "Major mode for gdb threads.
2152 \\{gdb-threads-mode-map}"
2153 (kill-all-local-variables)
2154 (setq major-mode 'gdb-threads-mode)
2155 (setq mode-name "Threads")
2156 (setq buffer-read-only t)
2157 (use-local-map gdb-threads-mode-map)
2158 (set (make-local-variable 'font-lock-defaults)
2159 '(gdb-threads-font-lock-keywords))
2160 (run-mode-hooks 'gdb-threads-mode-hook)
2161 'gdb-invalidate-threads)
2163 (defun gdb-get-thread-number ()
2165 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2166 (match-string-no-properties 1)))
2168 (defun gdb-threads-select (&optional event)
2169 "Select the thread and display the relevant source."
2170 (interactive (list last-input-event))
2171 (if event (posn-set-point (event-end event)))
2173 (list (concat gdb-server-prefix "thread "
2174 (gdb-get-thread-number) "\n") 'ignore))
2175 (gud-display-frame))
2178 ;; Registers buffer.
2180 (defcustom gdb-all-registers nil
2181 "Non-nil means include floating-point registers."
2186 (gdb-set-buffer-rules 'gdb-registers-buffer
2187 'gdb-registers-buffer-name
2188 'gdb-registers-mode)
2190 (def-gdb-auto-updated-buffer gdb-registers-buffer
2191 gdb-invalidate-registers
2193 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2194 gdb-info-registers-handler
2195 gdb-info-registers-custom)
2197 (defun gdb-info-registers-custom ()
2198 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2200 (let ((buffer-read-only nil)
2202 (goto-char (point-min))
2203 (while (< (point) (point-max))
2204 (setq start (line-beginning-position))
2205 (setq end (line-end-position))
2206 (when (looking-at "^[^ ]+")
2207 (unless (string-equal (match-string 0) "The")
2208 (put-text-property start (match-end 0)
2209 'face font-lock-variable-name-face)
2210 (add-text-properties start end
2211 '(help-echo "mouse-2: edit value"
2212 mouse-face highlight))))
2213 (forward-line 1))))))
2215 (defun gdb-edit-register-value (&optional event)
2216 (interactive (list last-input-event))
2218 (if event (posn-set-point (event-end event)))
2220 (let* ((register (current-word))
2221 (value (read-string (format "New value (%s): " register))))
2223 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2226 (defvar gdb-registers-mode-map
2227 (let ((map (make-sparse-keymap)))
2228 (suppress-keymap map)
2229 (define-key map "\r" 'gdb-edit-register-value)
2230 (define-key map [mouse-2] 'gdb-edit-register-value)
2231 (define-key map " " 'gdb-all-registers)
2232 (define-key map "q" 'kill-this-buffer)
2235 (defun gdb-registers-mode ()
2236 "Major mode for gdb registers.
2238 \\{gdb-registers-mode-map}"
2239 (kill-all-local-variables)
2240 (setq major-mode 'gdb-registers-mode)
2241 (setq mode-name "Registers")
2242 (setq buffer-read-only t)
2243 (use-local-map gdb-registers-mode-map)
2244 (run-mode-hooks 'gdb-registers-mode-hook)
2245 (if (string-equal gdb-version "pre-6.4")
2247 (if gdb-all-registers (setq mode-name "Registers:All"))
2248 'gdb-invalidate-registers)
2249 'gdb-invalidate-registers-1))
2251 (defun gdb-registers-buffer-name ()
2252 (with-current-buffer gud-comint-buffer
2253 (concat "*registers of " (gdb-get-target-string) "*")))
2255 (defun gdb-display-registers-buffer ()
2256 "Display integer register contents."
2259 (gdb-get-buffer-create 'gdb-registers-buffer)))
2261 (defun gdb-frame-registers-buffer ()
2262 "Display integer register contents in a new frame."
2264 (let ((special-display-regexps (append special-display-regexps '(".*")))
2265 (special-display-frame-alist gdb-frame-parameters))
2266 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2268 (defun gdb-all-registers ()
2269 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2271 (when (string-equal gdb-version "pre-6.4")
2272 (if gdb-all-registers
2274 (setq gdb-all-registers nil)
2275 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2276 (setq mode-name "Registers")))
2277 (setq gdb-all-registers t)
2278 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2279 (setq mode-name "Registers:All")))
2280 (message (format "Display of floating-point registers %sabled"
2281 (if gdb-all-registers "en" "dis")))
2282 (gdb-invalidate-registers)))
2287 (defcustom gdb-memory-repeat-count 32
2288 "Number of data items in memory window."
2293 (defcustom gdb-memory-format "x"
2294 "Display format of data items in memory window."
2295 :type '(choice (const :tag "Hexadecimal" "x")
2296 (const :tag "Signed decimal" "d")
2297 (const :tag "Unsigned decimal" "u")
2298 (const :tag "Octal" "o")
2299 (const :tag "Binary" "t"))
2303 (defcustom gdb-memory-unit "w"
2304 "Unit size of data items in memory window."
2305 :type '(choice (const :tag "Byte" "b")
2306 (const :tag "Halfword" "h")
2307 (const :tag "Word" "w")
2308 (const :tag "Giant word" "g"))
2312 (gdb-set-buffer-rules 'gdb-memory-buffer
2313 'gdb-memory-buffer-name
2316 (def-gdb-auto-updated-buffer gdb-memory-buffer
2317 gdb-invalidate-memory
2318 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2319 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2320 gdb-read-memory-handler
2321 gdb-read-memory-custom)
2323 (defun gdb-read-memory-custom ()
2325 (goto-char (point-min))
2326 (if (looking-at "0x[[:xdigit:]]+")
2327 (setq gdb-memory-address (match-string 0)))))
2329 (defvar gdb-memory-mode-map
2330 (let ((map (make-sparse-keymap)))
2331 (suppress-keymap map)
2332 (define-key map "q" 'kill-this-buffer)
2335 (defun gdb-memory-set-address (event)
2336 "Set the start memory address."
2338 (save-selected-window
2339 (select-window (posn-window (event-start event)))
2340 (let ((arg (read-from-minibuffer "Memory address: ")))
2341 (setq gdb-memory-address arg))
2342 (gdb-invalidate-memory)))
2344 (defun gdb-memory-set-repeat-count (event)
2345 "Set the number of data items in memory window."
2347 (save-selected-window
2348 (select-window (posn-window (event-start event)))
2349 (let* ((arg (read-from-minibuffer "Repeat count: "))
2350 (count (string-to-number arg)))
2352 (error "Positive numbers only")
2353 (customize-set-variable 'gdb-memory-repeat-count count)
2354 (gdb-invalidate-memory)))))
2356 (defun gdb-memory-format-binary ()
2357 "Set the display format to binary."
2359 (customize-set-variable 'gdb-memory-format "t")
2360 (gdb-invalidate-memory))
2362 (defun gdb-memory-format-octal ()
2363 "Set the display format to octal."
2365 (customize-set-variable 'gdb-memory-format "o")
2366 (gdb-invalidate-memory))
2368 (defun gdb-memory-format-unsigned ()
2369 "Set the display format to unsigned decimal."
2371 (customize-set-variable 'gdb-memory-format "u")
2372 (gdb-invalidate-memory))
2374 (defun gdb-memory-format-signed ()
2375 "Set the display format to decimal."
2377 (customize-set-variable 'gdb-memory-format "d")
2378 (gdb-invalidate-memory))
2380 (defun gdb-memory-format-hexadecimal ()
2381 "Set the display format to hexadecimal."
2383 (customize-set-variable 'gdb-memory-format "x")
2384 (gdb-invalidate-memory))
2386 (defvar gdb-memory-format-map
2387 (let ((map (make-sparse-keymap)))
2388 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2390 "Keymap to select format in the header line.")
2392 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2393 "Menu of display formats in the header line.")
2395 (define-key gdb-memory-format-menu [binary]
2396 '(menu-item "Binary" gdb-memory-format-binary
2397 :button (:radio . (equal gdb-memory-format "t"))))
2398 (define-key gdb-memory-format-menu [octal]
2399 '(menu-item "Octal" gdb-memory-format-octal
2400 :button (:radio . (equal gdb-memory-format "o"))))
2401 (define-key gdb-memory-format-menu [unsigned]
2402 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2403 :button (:radio . (equal gdb-memory-format "u"))))
2404 (define-key gdb-memory-format-menu [signed]
2405 '(menu-item "Signed Decimal" gdb-memory-format-signed
2406 :button (:radio . (equal gdb-memory-format "d"))))
2407 (define-key gdb-memory-format-menu [hexadecimal]
2408 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2409 :button (:radio . (equal gdb-memory-format "x"))))
2411 (defun gdb-memory-format-menu (event)
2413 (x-popup-menu event gdb-memory-format-menu))
2415 (defun gdb-memory-format-menu-1 (event)
2417 (save-selected-window
2418 (select-window (posn-window (event-start event)))
2419 (let* ((selection (gdb-memory-format-menu event))
2420 (binding (and selection (lookup-key gdb-memory-format-menu
2421 (vector (car selection))))))
2422 (if binding (call-interactively binding)))))
2424 (defun gdb-memory-unit-giant ()
2425 "Set the unit size to giant words (eight bytes)."
2427 (customize-set-variable 'gdb-memory-unit "g")
2428 (gdb-invalidate-memory))
2430 (defun gdb-memory-unit-word ()
2431 "Set the unit size to words (four bytes)."
2433 (customize-set-variable 'gdb-memory-unit "w")
2434 (gdb-invalidate-memory))
2436 (defun gdb-memory-unit-halfword ()
2437 "Set the unit size to halfwords (two bytes)."
2439 (customize-set-variable 'gdb-memory-unit "h")
2440 (gdb-invalidate-memory))
2442 (defun gdb-memory-unit-byte ()
2443 "Set the unit size to bytes."
2445 (customize-set-variable 'gdb-memory-unit "b")
2446 (gdb-invalidate-memory))
2448 (defvar gdb-memory-unit-map
2449 (let ((map (make-sparse-keymap)))
2450 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2452 "Keymap to select units in the header line.")
2454 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2455 "Menu of units in the header line.")
2457 (define-key gdb-memory-unit-menu [giantwords]
2458 '(menu-item "Giant words" gdb-memory-unit-giant
2459 :button (:radio . (equal gdb-memory-unit "g"))))
2460 (define-key gdb-memory-unit-menu [words]
2461 '(menu-item "Words" gdb-memory-unit-word
2462 :button (:radio . (equal gdb-memory-unit "w"))))
2463 (define-key gdb-memory-unit-menu [halfwords]
2464 '(menu-item "Halfwords" gdb-memory-unit-halfword
2465 :button (:radio . (equal gdb-memory-unit "h"))))
2466 (define-key gdb-memory-unit-menu [bytes]
2467 '(menu-item "Bytes" gdb-memory-unit-byte
2468 :button (:radio . (equal gdb-memory-unit "b"))))
2470 (defun gdb-memory-unit-menu (event)
2472 (x-popup-menu event gdb-memory-unit-menu))
2474 (defun gdb-memory-unit-menu-1 (event)
2476 (save-selected-window
2477 (select-window (posn-window (event-start event)))
2478 (let* ((selection (gdb-memory-unit-menu event))
2479 (binding (and selection (lookup-key gdb-memory-unit-menu
2480 (vector (car selection))))))
2481 (if binding (call-interactively binding)))))
2483 ;;from make-mode-line-mouse-map
2484 (defun gdb-make-header-line-mouse-map (mouse function) "\
2485 Return a keymap with single entry for mouse key MOUSE on the header line.
2486 MOUSE is defined to run function FUNCTION with no args in the buffer
2487 corresponding to the mode line clicked."
2488 (let ((map (make-sparse-keymap)))
2489 (define-key map (vector 'header-line mouse) function)
2490 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2493 (defvar gdb-memory-font-lock-keywords
2494 '(;; <__function.name+n>
2495 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2497 "Font lock keywords used in `gdb-memory-mode'.")
2499 (defun gdb-memory-mode ()
2500 "Major mode for examining memory.
2502 \\{gdb-memory-mode-map}"
2503 (kill-all-local-variables)
2504 (setq major-mode 'gdb-memory-mode)
2505 (setq mode-name "Memory")
2506 (setq buffer-read-only t)
2507 (use-local-map gdb-memory-mode-map)
2508 (setq header-line-format
2514 'face font-lock-warning-face
2515 'help-echo "mouse-1: Decrement address"
2516 'mouse-face 'mode-line-highlight
2518 (gdb-make-header-line-mouse-map
2520 #'(lambda () (interactive)
2521 (let ((gdb-memory-address
2522 ;; Let GDB do the arithmetic.
2524 gdb-memory-address " - "
2526 (* gdb-memory-repeat-count
2527 (cond ((string= gdb-memory-unit "b") 1)
2528 ((string= gdb-memory-unit "h") 2)
2529 ((string= gdb-memory-unit "w") 4)
2530 ((string= gdb-memory-unit "g") 8)))))))
2531 (gdb-invalidate-memory)))))
2534 'face font-lock-warning-face
2535 'help-echo "mouse-1: Increment address"
2536 'mouse-face 'mode-line-highlight
2537 'local-map (gdb-make-header-line-mouse-map
2539 #'(lambda () (interactive)
2540 (let ((gdb-memory-address nil))
2541 (gdb-invalidate-memory)))))
2543 (propertize gdb-memory-address
2544 'face font-lock-warning-face
2545 'help-echo "mouse-1: Set memory address"
2546 'mouse-face 'mode-line-highlight
2547 'local-map (gdb-make-header-line-mouse-map
2549 #'gdb-memory-set-address))
2551 (propertize (number-to-string gdb-memory-repeat-count)
2552 'face font-lock-warning-face
2553 'help-echo "mouse-1: Set repeat count"
2554 'mouse-face 'mode-line-highlight
2555 'local-map (gdb-make-header-line-mouse-map
2557 #'gdb-memory-set-repeat-count))
2559 (propertize gdb-memory-format
2560 'face font-lock-warning-face
2561 'help-echo "mouse-3: Select display format"
2562 'mouse-face 'mode-line-highlight
2563 'local-map gdb-memory-format-map)
2565 (propertize gdb-memory-unit
2566 'face font-lock-warning-face
2567 'help-echo "mouse-3: Select unit size"
2568 'mouse-face 'mode-line-highlight
2569 'local-map gdb-memory-unit-map))))
2570 (set (make-local-variable 'font-lock-defaults)
2571 '(gdb-memory-font-lock-keywords))
2572 (run-mode-hooks 'gdb-memory-mode-hook)
2573 'gdb-invalidate-memory)
2575 (defun gdb-memory-buffer-name ()
2576 (with-current-buffer gud-comint-buffer
2577 (concat "*memory of " (gdb-get-target-string) "*")))
2579 (defun gdb-display-memory-buffer ()
2580 "Display memory contents."
2583 (gdb-get-buffer-create 'gdb-memory-buffer)))
2585 (defun gdb-frame-memory-buffer ()
2586 "Display memory contents in a new frame."
2588 (let ((special-display-regexps (append special-display-regexps '(".*")))
2589 (special-display-frame-alist gdb-frame-parameters))
2590 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2595 (gdb-set-buffer-rules 'gdb-locals-buffer
2596 'gdb-locals-buffer-name
2599 (def-gdb-auto-update-trigger gdb-invalidate-locals
2600 (gdb-get-buffer 'gdb-locals-buffer)
2601 "server info locals\n"
2602 gdb-info-locals-handler)
2604 (defvar gdb-locals-watch-map
2605 (let ((map (make-sparse-keymap)))
2606 (define-key map "\r" '(lambda () (interactive)
2609 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2610 (mouse-set-point event)
2614 "Keymap to create watch expression of a complex data type local variable.")
2616 (defconst gdb-struct-string
2617 (concat (propertize "[struct/union]"
2618 'mouse-face 'highlight
2619 'help-echo "mouse-2: create watch expression"
2620 'local-map gdb-locals-watch-map) "\n"))
2622 (defconst gdb-array-string
2623 (concat " " (propertize "[array]"
2624 'mouse-face 'highlight
2625 'help-echo "mouse-2: create watch expression"
2626 'local-map gdb-locals-watch-map) "\n"))
2628 ;; Abbreviate for arrays and structures.
2629 ;; These can be expanded using gud-display.
2630 (defun gdb-info-locals-handler ()
2631 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2632 gdb-pending-triggers))
2633 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2634 (with-current-buffer buf
2635 (goto-char (point-min))
2636 (while (re-search-forward "^[ }].*\n" nil t)
2637 (replace-match "" nil nil))
2638 (goto-char (point-min))
2639 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2640 (replace-match gdb-struct-string nil nil))
2641 (goto-char (point-min))
2642 (while (re-search-forward "\\s-*{.*\n" nil t)
2643 (replace-match gdb-array-string nil nil))))
2644 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2646 (with-current-buffer buf
2647 (let* ((window (get-buffer-window buf 0))
2648 (start (window-start window))
2649 (p (window-point window))
2650 (buffer-read-only nil))
2652 (insert-buffer-substring (gdb-get-buffer-create
2653 'gdb-partial-output-buffer))
2654 (set-window-start window start)
2655 (set-window-point window p))
2657 (run-hooks 'gdb-info-locals-hook))
2659 (defvar gdb-locals-mode-map
2660 (let ((map (make-sparse-keymap)))
2661 (suppress-keymap map)
2662 (define-key map "q" 'kill-this-buffer)
2665 (defun gdb-locals-mode ()
2666 "Major mode for gdb locals.
2668 \\{gdb-locals-mode-map}"
2669 (kill-all-local-variables)
2670 (setq major-mode 'gdb-locals-mode)
2671 (setq mode-name (concat "Locals:" gdb-selected-frame))
2672 (setq buffer-read-only t)
2673 (use-local-map gdb-locals-mode-map)
2674 (set (make-local-variable 'font-lock-defaults)
2675 '(gdb-locals-font-lock-keywords))
2676 (run-mode-hooks 'gdb-locals-mode-hook)
2677 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2678 (string-equal gdb-version "pre-6.4"))
2679 'gdb-invalidate-locals
2680 'gdb-invalidate-locals-1))
2682 (defun gdb-locals-buffer-name ()
2683 (with-current-buffer gud-comint-buffer
2684 (concat "*locals of " (gdb-get-target-string) "*")))
2686 (defun gdb-display-locals-buffer ()
2687 "Display local variables of current stack and their values."
2690 (gdb-get-buffer-create 'gdb-locals-buffer)))
2692 (defun gdb-frame-locals-buffer ()
2693 "Display local variables of current stack and their values in a new frame."
2695 (let ((special-display-regexps (append special-display-regexps '(".*")))
2696 (special-display-frame-alist gdb-frame-parameters))
2697 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2700 ;;;; Window management
2701 (defun gdb-display-buffer (buf &optional size)
2702 (let ((answer (get-buffer-window buf 0))
2705 (display-buffer buf nil 0) ;Raise the frame if necessary.
2706 ;; The buffer is not yet displayed.
2707 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2708 (let ((window (get-lru-window)))
2710 (not (eq window (get-buffer-window gud-comint-buffer))))
2712 (set-window-buffer window buf)
2713 (setq answer window))
2714 (setq must-split t)))
2716 (let* ((largest (get-largest-window))
2717 (cur-size (window-height largest))
2718 (new-size (and size (< size cur-size) (- cur-size size))))
2719 (setq answer (split-window largest new-size))
2720 (set-window-buffer answer buf)
2721 (set-window-dedicated-p answer t)))
2725 ;;; Shared keymap initialization:
2727 (let ((menu (make-sparse-keymap "GDB-Windows")))
2728 (define-key gud-menu-map [displays]
2729 `(menu-item "GDB-Windows" ,menu
2730 :visible (memq gud-minor-mode '(gdbmi gdba))))
2731 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2732 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2733 (define-key menu [inferior]
2734 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2735 :enable gdb-use-separate-io-buffer))
2736 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2737 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2738 (define-key menu [disassembly]
2739 '("Disassembly" . gdb-display-assembler-buffer))
2740 (define-key menu [breakpoints]
2741 '("Breakpoints" . gdb-display-breakpoints-buffer))
2742 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2743 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2745 (let ((menu (make-sparse-keymap "GDB-Frames")))
2746 (define-key gud-menu-map [frames]
2747 `(menu-item "GDB-Frames" ,menu
2748 :visible (memq gud-minor-mode '(gdbmi gdba))))
2749 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2750 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2751 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2752 (define-key menu [inferior]
2753 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2754 :enable gdb-use-separate-io-buffer))
2755 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2756 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2757 (define-key menu [breakpoints]
2758 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2759 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2760 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2762 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2763 (define-key gud-menu-map [ui]
2764 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2765 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2766 (define-key menu [gdb-use-separate-io]
2767 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2768 :visible (eq gud-minor-mode 'gdba)
2769 :help "Toggle separate IO for inferior."
2770 :button (:toggle . gdb-use-separate-io-buffer)))
2771 (define-key menu [gdb-many-windows]
2772 '(menu-item "Display Other Windows" gdb-many-windows
2773 :help "Toggle display of locals, stack and breakpoint information"
2774 :button (:toggle . gdb-many-windows)))
2775 (define-key menu [gdb-restore-windows]
2776 '(menu-item "Restore Window Layout" gdb-restore-windows
2777 :help "Restore standard layout for debug session.")))
2779 (defun gdb-frame-gdb-buffer ()
2780 "Display GUD buffer in a new frame."
2782 (let ((special-display-regexps (append special-display-regexps '(".*")))
2783 (special-display-frame-alist
2784 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2785 gdb-frame-parameters)))
2786 (same-window-regexps nil))
2787 (display-buffer gud-comint-buffer)))
2789 (defun gdb-display-gdb-buffer ()
2790 "Display GUD buffer."
2792 (let ((same-window-regexps nil))
2793 (pop-to-buffer gud-comint-buffer)))
2795 (defun gdb-set-window-buffer (name)
2796 (set-window-buffer (selected-window) (get-buffer name))
2797 (set-window-dedicated-p (selected-window) t))
2799 (defun gdb-setup-windows ()
2800 "Layout the window pattern for `gdb-many-windows'."
2801 (gdb-display-locals-buffer)
2802 (gdb-display-stack-buffer)
2803 (delete-other-windows)
2804 (gdb-display-breakpoints-buffer)
2805 (delete-other-windows)
2807 (pop-to-buffer gud-comint-buffer)
2808 (split-window nil ( / ( * (window-height) 3) 4))
2809 (split-window nil ( / (window-height) 3))
2810 (split-window-horizontally)
2812 (gdb-set-window-buffer (gdb-locals-buffer-name))
2815 (if gud-last-last-frame
2816 (gud-find-file (car gud-last-last-frame))
2818 (gud-find-file gdb-main-file)
2819 ;; Put buffer list in window if we
2820 ;; can't find a source file.
2821 (list-buffers-noselect))))
2822 (setq gdb-source-window (selected-window))
2823 (when gdb-use-separate-io-buffer
2824 (split-window-horizontally)
2826 (gdb-set-window-buffer
2827 (gdb-get-buffer-create 'gdb-inferior-io)))
2829 (gdb-set-window-buffer (gdb-stack-buffer-name))
2830 (split-window-horizontally)
2832 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2835 (defun gdb-restore-windows ()
2836 "Restore the basic arrangement of windows used by gdba.
2837 This arrangement depends on the value of `gdb-many-windows'."
2839 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2840 (delete-other-windows)
2841 (if gdb-many-windows
2843 (when (or gud-last-last-frame gdb-show-main)
2847 (if gud-last-last-frame
2848 (gud-find-file (car gud-last-last-frame))
2849 (gud-find-file gdb-main-file)))
2850 (setq gdb-source-window (selected-window))
2854 "Exit a debugging session cleanly.
2855 Kills the gdb buffers, and resets variables and the source buffers."
2856 (dolist (buffer (buffer-list))
2857 (unless (eq buffer gud-comint-buffer)
2858 (with-current-buffer buffer
2859 (if (memq gud-minor-mode '(gdbmi gdba))
2860 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2862 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2863 (setq gud-minor-mode nil)
2864 (kill-local-variable 'tool-bar-map)
2865 (kill-local-variable 'gdb-define-alist))))))
2866 (when (markerp gdb-overlay-arrow-position)
2867 (move-marker gdb-overlay-arrow-position nil)
2868 (setq gdb-overlay-arrow-position nil))
2869 (setq overlay-arrow-variable-list
2870 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2871 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2872 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2873 (setq gud-running nil)
2874 (setq gdb-active-process nil)
2875 (setq gdb-var-list nil)
2876 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2878 (defun gdb-source-info ()
2879 "Find the source file where the program starts and displays it with related
2881 (goto-char (point-min))
2882 (if (and (search-forward "Located in " nil t)
2883 (looking-at "\\S-+"))
2884 (setq gdb-main-file (match-string 0)))
2885 (goto-char (point-min))
2886 (if (search-forward "Includes preprocessor macro info." nil t)
2887 (setq gdb-macro-info t))
2888 (if gdb-many-windows
2890 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2892 (let ((pop-up-windows t))
2893 (display-buffer (gud-find-file gdb-main-file))))))
2895 (defun gdb-get-location (bptno line flag)
2896 "Find the directory containing the relevant source file.
2897 Put in buffer and place breakpoint icon."
2898 (goto-char (point-min))
2899 (catch 'file-not-found
2900 (if (search-forward "Located in " nil t)
2901 (when (looking-at "\\S-+")
2902 (delete (cons bptno "File not found") gdb-location-alist)
2903 (push (cons bptno (match-string 0)) gdb-location-alist))
2905 (unless (assoc bptno gdb-location-alist)
2906 (push (cons bptno "File not found") gdb-location-alist)
2907 (message-box "Cannot find source file for breakpoint location.\n\
2908 Add directory to search path for source files using the GDB command, dir."))
2909 (throw 'file-not-found nil))
2910 (with-current-buffer
2911 (find-file-noselect (match-string 0))
2912 (save-current-buffer
2913 (set (make-local-variable 'gud-minor-mode) 'gdba)
2914 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2915 ;; only want one breakpoint icon at each location
2917 (goto-line (string-to-number line))
2918 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2920 (add-hook 'find-file-hook 'gdb-find-file-hook)
2922 (defun gdb-find-file-hook ()
2923 "Set up buffer for debugging if file is part of the source code
2924 of the current session."
2925 (if (and (buffer-name gud-comint-buffer)
2926 ;; in case gud or gdb-ui is just loaded
2928 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2930 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2931 (if (member (if (string-equal gdb-version "pre-6.4")
2932 (file-name-nondirectory buffer-file-name)
2934 gdb-source-file-list)
2935 (with-current-buffer (find-buffer-visiting buffer-file-name)
2936 (set (make-local-variable 'gud-minor-mode)
2937 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2938 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2941 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2942 "Put string PUTSTRING in front of POS in the current buffer.
2943 PUTSTRING is displayed by putting an overlay into the current buffer with a
2944 `before-string' string that has a `display' property whose value is
2946 (let ((string (make-string 1 ?x))
2947 (buffer (current-buffer)))
2948 (setq putstring (copy-sequence putstring))
2949 (let ((overlay (make-overlay pos pos buffer))
2951 (list (list 'margin 'left-margin) putstring))))
2952 (put-text-property 0 1 'display prop string)
2954 (add-text-properties 0 1 sprops string))
2955 (overlay-put overlay 'put-break t)
2956 (overlay-put overlay 'before-string string))))
2958 ;;from remove-images
2959 (defun gdb-remove-strings (start end &optional buffer)
2960 "Remove strings between START and END in BUFFER.
2961 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2962 BUFFER nil or omitted means use the current buffer."
2964 (setq buffer (current-buffer)))
2965 (dolist (overlay (overlays-in start end))
2966 (when (overlay-get overlay 'put-break)
2967 (delete-overlay overlay))))
2969 (defun gdb-put-breakpoint-icon (enabled bptno)
2970 (let ((start (- (line-beginning-position) 1))
2971 (end (+ (line-end-position) 1))
2972 (putstring (if enabled "B" "b"))
2973 (source-window (get-buffer-window (current-buffer) 0)))
2974 (add-text-properties
2975 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2978 (add-text-properties
2979 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2980 (add-text-properties
2981 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2982 (gdb-remove-breakpoint-icons start end)
2983 (if (display-images-p)
2984 (if (>= (or left-fringe-width
2985 (if source-window (car (window-fringes source-window)))
2986 gdb-buffer-fringe-width) 8)
2989 `(left-fringe breakpoint
2992 'breakpoint-disabled))
2994 'gdb-enabled enabled)
2995 (when (< left-margin-width 2)
2996 (save-current-buffer
2997 (setq left-margin-width 2)
3001 left-margin-width right-margin-width))))
3004 (or breakpoint-enabled-icon
3005 (setq breakpoint-enabled-icon
3006 (find-image `((:type xpm :data
3007 ,breakpoint-xpm-data
3008 :ascent 100 :pointer hand)
3010 ,breakpoint-enabled-pbm-data
3011 :ascent 100 :pointer hand)))))
3012 (or breakpoint-disabled-icon
3013 (setq breakpoint-disabled-icon
3014 (find-image `((:type xpm :data
3015 ,breakpoint-xpm-data
3016 :conversion disabled
3017 :ascent 100 :pointer hand)
3019 ,breakpoint-disabled-pbm-data
3020 :ascent 100 :pointer hand))))))
3024 (when (< left-margin-width 2)
3025 (save-current-buffer
3026 (setq left-margin-width 2)
3027 (let ((window (get-buffer-window (current-buffer) 0)))
3030 window left-margin-width right-margin-width)))))
3032 (propertize putstring
3033 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3036 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3037 (gdb-remove-strings start end)
3038 (if (display-images-p)
3039 (remove-images start end))
3041 (setq left-margin-width 0)
3042 (let ((window (get-buffer-window (current-buffer) 0)))
3045 window left-margin-width right-margin-width)))))
3049 ;; Assembler buffer.
3051 (gdb-set-buffer-rules 'gdb-assembler-buffer
3052 'gdb-assembler-buffer-name
3053 'gdb-assembler-mode)
3055 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3056 ;; window-start but keep the overlay arrow/current line visible.
3057 (defun gdb-assembler-handler ()
3058 (setq gdb-pending-triggers
3059 (delq 'gdb-invalidate-assembler
3060 gdb-pending-triggers))
3061 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3063 (with-current-buffer buf
3064 (let* ((window (get-buffer-window buf 0))
3065 (p (window-point window))
3066 (buffer-read-only nil))
3068 (insert-buffer-substring (gdb-get-buffer-create
3069 'gdb-partial-output-buffer))
3070 (set-window-point window p)))))
3071 ;; put customisation here
3072 (gdb-assembler-custom))
3074 (defun gdb-assembler-custom ()
3075 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3076 (pos 1) (address) (flag) (bptno))
3077 (with-current-buffer buffer
3079 (if (not (equal gdb-pc-address "main"))
3081 (goto-char (point-min))
3082 (if (and gdb-pc-address
3083 (search-forward gdb-pc-address nil t))
3087 (setq fringe-indicator-alist
3088 (if (string-equal gdb-frame-number "0")
3090 '((overlay-arrow . hollow-right-triangle))))
3091 (or gdb-overlay-arrow-position
3092 (setq gdb-overlay-arrow-position (make-marker)))
3093 (set-marker gdb-overlay-arrow-position
3094 (point) (current-buffer))))))
3095 ;; remove all breakpoint-icons in assembler buffer before updating.
3096 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3097 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3098 (goto-char (point-min))
3099 (while (< (point) (- (point-max) 1))
3101 (if (looking-at "[^\t].*?breakpoint")
3104 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3105 (setq bptno (match-string 1))
3106 (setq flag (char-after (match-beginning 2)))
3107 (setq address (match-string 3))
3108 (with-current-buffer buffer
3110 (goto-char (point-min))
3111 (if (search-forward address nil t)
3112 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3113 (if (not (equal gdb-pc-address "main"))
3114 (with-current-buffer buffer
3115 (set-window-point (get-buffer-window buffer 0) pos)))))
3117 (defvar gdb-assembler-mode-map
3118 (let ((map (make-sparse-keymap)))
3119 (suppress-keymap map)
3120 (define-key map "q" 'kill-this-buffer)
3123 (defvar gdb-assembler-font-lock-keywords
3124 '(;; <__function.name+n>
3125 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3126 (1 font-lock-function-name-face))
3127 ;; 0xNNNNNNNN <__function.name+n>: opcode
3128 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3129 (4 font-lock-keyword-face))
3130 ;; %register(at least i386)
3131 ("%\\sw+" . font-lock-variable-name-face)
3132 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3133 (1 font-lock-comment-face)
3134 (2 font-lock-function-name-face))
3135 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3136 "Font lock keywords used in `gdb-assembler-mode'.")
3138 (defun gdb-assembler-mode ()
3139 "Major mode for viewing code assembler.
3141 \\{gdb-assembler-mode-map}"
3142 (kill-all-local-variables)
3143 (setq major-mode 'gdb-assembler-mode)
3144 (setq mode-name (concat "Machine:" gdb-selected-frame))
3145 (setq gdb-overlay-arrow-position nil)
3146 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3147 (setq fringes-outside-margins t)
3148 (setq buffer-read-only t)
3149 (use-local-map gdb-assembler-mode-map)
3150 (gdb-invalidate-assembler)
3151 (set (make-local-variable 'font-lock-defaults)
3152 '(gdb-assembler-font-lock-keywords))
3153 (run-mode-hooks 'gdb-assembler-mode-hook)
3154 'gdb-invalidate-assembler)
3156 (defun gdb-assembler-buffer-name ()
3157 (with-current-buffer gud-comint-buffer
3158 (concat "*disassembly of " (gdb-get-target-string) "*")))
3160 (defun gdb-display-assembler-buffer ()
3161 "Display disassembly view."
3163 (setq gdb-previous-frame nil)
3165 (gdb-get-buffer-create 'gdb-assembler-buffer)))
3167 (defun gdb-frame-assembler-buffer ()
3168 "Display disassembly view in a new frame."
3170 (setq gdb-previous-frame nil)
3171 (let ((special-display-regexps (append special-display-regexps '(".*")))
3172 (special-display-frame-alist gdb-frame-parameters))
3173 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3175 ;; modified because if gdb-pc-address has changed value a new command
3176 ;; must be enqueued to update the buffer with the new output
3177 (defun gdb-invalidate-assembler (&optional ignored)
3178 (if (gdb-get-buffer 'gdb-assembler-buffer)
3180 (unless (and gdb-selected-frame
3181 (string-equal gdb-selected-frame gdb-previous-frame))
3182 (if (or (not (member 'gdb-invalidate-assembler
3183 gdb-pending-triggers))
3184 (not (string-equal gdb-pc-address
3185 gdb-previous-frame-address)))
3187 ;; take previous disassemble command, if any, off the queue
3188 (with-current-buffer gud-comint-buffer
3189 (let ((queue gdb-input-queue))
3190 (dolist (item queue)
3191 (if (equal (cdr item) '(gdb-assembler-handler))
3192 (setq gdb-input-queue
3193 (delete item gdb-input-queue))))))
3196 (concat gdb-server-prefix "disassemble "
3197 (if (member gdb-pc-address '(nil "main")) nil "0x")
3198 gdb-pc-address "\n")
3199 'gdb-assembler-handler))
3200 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3201 (setq gdb-previous-frame-address gdb-pc-address)
3202 (setq gdb-previous-frame gdb-selected-frame)))))))
3204 (defun gdb-get-selected-frame ()
3205 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3208 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3209 (push 'gdb-get-selected-frame
3210 gdb-pending-triggers))))
3212 (defun gdb-frame-handler ()
3213 (setq gdb-pending-triggers
3214 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3215 (goto-char (point-min))
3216 (when (re-search-forward
3217 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3218 (setq gdb-frame-number (match-string 1))
3219 (setq gdb-frame-address (match-string 2)))
3220 (goto-char (point-min))
3221 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3222 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3224 (setq gdb-selected-frame (match-string 2))
3225 (if (gdb-get-buffer 'gdb-locals-buffer)
3226 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3227 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3228 (if (gdb-get-buffer 'gdb-assembler-buffer)
3229 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3230 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3231 (setq gdb-pc-address (match-string 1))
3232 (if (and (match-string 3) gud-overlay-arrow-position)
3233 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3234 (position (marker-position gud-overlay-arrow-position)))
3236 (string-equal (buffer-name buffer)
3237 (file-name-nondirectory (match-string 3))))
3238 (with-current-buffer buffer
3239 (setq fringe-indicator-alist
3240 (if (string-equal gdb-frame-number "0")
3242 '((overlay-arrow . hollow-right-triangle))))
3243 (set-marker gud-overlay-arrow-position position))))))
3244 (goto-char (point-min))
3245 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3246 (setq gdb-current-language (match-string 1)))
3247 (gdb-invalidate-assembler))
3250 ;; Code specific to GDB 6.4
3251 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3253 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3254 "Create list of source files for current GDB session.
3255 If buffers already exist for any of these files, gud-minor-mode
3257 (goto-char (point-min))
3258 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3259 (push (match-string 1) gdb-source-file-list))
3260 (dolist (buffer (buffer-list))
3261 (with-current-buffer buffer
3262 (when (member buffer-file-name gdb-source-file-list)
3263 (set (make-local-variable 'gud-minor-mode)
3264 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3265 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3266 (when gud-tooltip-mode
3267 (make-local-variable 'gdb-define-alist)
3268 (gdb-create-define-alist)
3269 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3270 (gdb-force-mode-line-update "ready"))
3272 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3273 (defun gdb-var-list-children-1 (varnum)
3276 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3277 (concat "server interpreter mi \"-var-list-children --all-values "
3279 (concat "-var-list-children --all-values " varnum "\n"))
3280 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3282 (defconst gdb-var-list-children-regexp-1
3283 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3284 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3286 (defun gdb-var-list-children-handler-1 (varnum)
3287 (goto-char (point-min))
3288 (let ((var-list nil))
3289 (catch 'child-already-watched
3290 (dolist (var gdb-var-list)
3291 (if (string-equal varnum (car var))
3294 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3295 (let ((varchild (list (match-string 1)
3299 (read (match-string 4))
3301 (if (assoc (car varchild) gdb-var-list)
3302 (throw 'child-already-watched nil))
3303 (push varchild var-list))))
3304 (push var var-list)))
3305 (setq gdb-var-list (nreverse var-list))))
3306 (gdb-speedbar-update))
3308 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3309 (defun gdb-var-update-1 ()
3310 (if (not (member 'gdb-var-update gdb-pending-triggers))
3314 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3315 "server interpreter mi \"-var-update --all-values *\"\n"
3316 "-var-update --all-values *\n")
3317 'gdb-var-update-handler-1))
3318 (push 'gdb-var-update gdb-pending-triggers))))
3320 (defconst gdb-var-update-regexp-1
3321 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3322 in_scope=\"\\(.*?\\)\".*?}")
3324 (defun gdb-var-update-handler-1 ()
3325 (dolist (var gdb-var-list)
3326 (setcar (nthcdr 5 var) nil))
3327 (goto-char (point-min))
3328 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3329 (let* ((varnum (match-string 1))
3330 (var (assoc varnum gdb-var-list)))
3332 (if (string-equal (match-string 3) "false")
3333 (setcar (nthcdr 5 var) 'out-of-scope)
3334 (setcar (nthcdr 5 var) 'changed)
3335 (setcar (nthcdr 4 var)
3336 (read (match-string 2)))))))
3337 (setq gdb-pending-triggers
3338 (delq 'gdb-var-update gdb-pending-triggers))
3339 (gdb-speedbar-update))
3341 ;; Registers buffer.
3343 (gdb-set-buffer-rules 'gdb-registers-buffer
3344 'gdb-registers-buffer-name
3345 'gdb-registers-mode)
3347 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3348 (gdb-get-buffer 'gdb-registers-buffer)
3349 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3350 "server interpreter mi \"-data-list-register-values x\"\n"
3351 "-data-list-register-values x\n")
3352 gdb-data-list-register-values-handler)
3354 (defconst gdb-data-list-register-values-regexp
3355 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3357 (defun gdb-data-list-register-values-handler ()
3358 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3359 gdb-pending-triggers))
3360 (goto-char (point-min))
3361 (if (re-search-forward gdb-error-regexp nil t)
3362 (let ((err (match-string 1)))
3363 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3364 (let ((buffer-read-only nil))
3366 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3368 (goto-char (point-min)))))
3369 (let ((register-list (reverse gdb-register-names))
3370 (register nil) (register-string nil) (register-values nil))
3371 (goto-char (point-min))
3372 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3373 (setq register (pop register-list))
3374 (setq register-string (concat register "\t" (match-string 2) "\n"))
3375 (if (member (match-string 1) gdb-changed-registers)
3376 (put-text-property 0 (length register-string)
3377 'face 'font-lock-warning-face
3379 (setq register-values
3380 (concat register-values register-string)))
3381 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3382 (with-current-buffer buf
3383 (let* ((window (get-buffer-window buf 0))
3384 (start (window-start window))
3385 (p (window-point window))
3386 (buffer-read-only nil))
3388 (insert register-values)
3389 (set-window-start window start)
3390 (set-window-point window p))))))
3391 (gdb-data-list-register-values-custom))
3393 (defun gdb-data-list-register-values-custom ()
3394 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3396 (let ((buffer-read-only nil)
3398 (goto-char (point-min))
3399 (while (< (point) (point-max))
3400 (setq start (line-beginning-position))
3401 (setq end (line-end-position))
3402 (when (looking-at "^[^\t]+")
3403 (unless (string-equal (match-string 0) "No registers.")
3404 (put-text-property start (match-end 0)
3405 'face font-lock-variable-name-face)
3406 (add-text-properties start end
3407 '(help-echo "mouse-2: edit value"
3408 mouse-face highlight))))
3409 (forward-line 1))))))
3411 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3412 (defun gdb-get-changed-registers ()
3413 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3414 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3418 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3419 "server interpreter mi -data-list-changed-registers\n"
3420 "-data-list-changed-registers\n")
3421 'gdb-get-changed-registers-handler))
3422 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3424 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3426 (defun gdb-get-changed-registers-handler ()
3427 (setq gdb-pending-triggers
3428 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3429 (setq gdb-changed-registers nil)
3430 (goto-char (point-min))
3431 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3432 (push (match-string 1) gdb-changed-registers)))
3437 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3438 (gdb-set-buffer-rules 'gdb-locals-buffer
3439 'gdb-locals-buffer-name
3442 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3443 (gdb-get-buffer 'gdb-locals-buffer)
3444 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3445 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3446 "-stack-list-locals --simple-values\n")
3447 gdb-stack-list-locals-handler)
3449 (defconst gdb-stack-list-locals-regexp
3450 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3452 (defvar gdb-locals-watch-map-1
3453 (let ((map (make-sparse-keymap)))
3454 (define-key map [mouse-2] 'gud-watch)
3456 "Keymap to create watch expression of a complex data type local variable.")
3458 ;; Dont display values of arrays or structures.
3459 ;; These can be expanded using gud-watch.
3460 (defun gdb-stack-list-locals-handler ()
3461 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3462 gdb-pending-triggers))
3463 (goto-char (point-min))
3464 (if (re-search-forward gdb-error-regexp nil t)
3465 (let ((err (match-string 1)))
3466 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3467 (let ((buffer-read-only nil))
3470 (goto-char (point-min)))))
3471 (let (local locals-list)
3472 (goto-char (point-min))
3473 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3474 (let ((local (list (match-string 1)
3477 (if (looking-at ",value=\\(\".*\"\\).*?}")
3478 (setcar (nthcdr 2 local) (read (match-string 1))))
3479 (push local locals-list)))
3480 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3481 (and buf (with-current-buffer buf
3482 (let* ((window (get-buffer-window buf 0))
3483 (start (window-start window))
3484 (p (window-point window))
3485 (buffer-read-only nil))
3487 (dolist (local locals-list)
3488 (setq name (car local))
3489 (if (or (not (nth 2 local))
3490 (string-match "^\\0x" (nth 2 local)))
3491 (add-text-properties 0 (length name)
3492 `(mouse-face highlight
3493 help-echo "mouse-2: create watch expression"
3494 local-map ,gdb-locals-watch-map-1)
3497 (concat name "\t" (nth 1 local)
3498 "\t" (nth 2 local) "\n")))
3499 (set-window-start window start)
3500 (set-window-point window p))))))))
3502 (defun gdb-get-register-names ()
3503 "Create a list of register names."
3504 (goto-char (point-min))
3505 (setq gdb-register-names nil)
3506 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3507 (push (match-string 1) gdb-register-names)))
3511 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3512 ;;; gdb-ui.el ends here