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-stack-position nil)
119 (defvar gdb-server-prefix nil)
120 (defvar gdb-flush-pending-output nil)
121 (defvar gdb-location-alist nil
122 "Alist of breakpoint numbers and full filenames.")
123 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
124 and #define directives otherwise.")
125 (defvar gdb-error "Non-nil when GDB is reporting an error.")
126 (defvar gdb-macro-info nil
127 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
128 (defvar gdb-buffer-fringe-width nil)
129 (defvar gdb-signalled nil)
130 (defvar gdb-source-window nil)
131 (defvar gdb-inferior-status nil)
132 (defvar gdb-continuation nil)
133 (defvar gdb-look-up-stack nil)
134 (defvar gdb-frame-begin nil
135 "Non-nil when GDB generates frame-begin annotation.")
136 (defvar gdb-printing t)
138 (defvar gdb-buffer-type nil
139 "One of the symbols bound in `gdb-buffer-rules'.")
140 (make-variable-buffer-local 'gdb-buffer-type)
142 (defvar gdb-input-queue ()
143 "A list of gdb command objects.")
145 (defvar gdb-prompting nil
146 "True when gdb is idle with no pending input.")
148 (defvar gdb-output-sink 'user
149 "The disposition of the output of the current gdb command.
150 Possible values are these symbols:
152 `user' -- gdb output should be copied to the GUD buffer
155 `inferior' -- gdb output should be copied to the inferior-io buffer.
157 `pre-emacs' -- output should be ignored util the post-prompt
158 annotation is received. Then the output-sink
160 `emacs' -- output should be collected in the partial-output-buffer
161 for subsequent processing by a command. This is the
162 disposition of output generated by commands that
163 gdb mode sends to gdb on its own behalf.
164 `post-emacs' -- ignore output until the prompt annotation is
165 received, then go to USER disposition.
167 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
168 \(`user' and `emacs').")
170 (defvar gdb-current-item nil
171 "The most recent command item sent to gdb.")
173 (defvar gdb-pending-triggers '()
174 "A list of trigger functions that have run later than their output
177 (defvar gdb-first-post-prompt nil)
178 (defvar gdb-version nil)
179 (defvar gdb-locals-font-lock-keywords nil)
180 (defvar gdb-source-file-list nil
181 "List of source files for the current executable")
182 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
184 (defvar gdb-locals-font-lock-keywords-1
186 ;; var = (struct struct_tag) value
187 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
188 (1 font-lock-variable-name-face)
189 (3 font-lock-keyword-face)
190 (4 font-lock-type-face))
191 ;; var = (type) value
192 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
193 (1 font-lock-variable-name-face)
194 (3 font-lock-type-face))
196 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
197 (1 font-lock-variable-name-face))
199 "Font lock keywords used in `gdb-local-mode'.")
201 (defvar gdb-locals-font-lock-keywords-2
204 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
205 (1 font-lock-variable-name-face)
206 (3 font-lock-type-face))
208 "Font lock keywords used in `gdb-local-mode'.")
210 ;; Variables for GDB 6.4+
211 (defvar gdb-register-names nil "List of register names.")
212 (defvar gdb-changed-registers nil
213 "List of changed register numbers (strings).")
216 (defun gdba (command-line)
217 "Run gdb on program FILE in buffer *gud-FILE*.
218 The directory containing FILE becomes the initial working directory
219 and source-file directory for your debugger.
221 If `gdb-many-windows' is nil (the default value) then gdb just
222 pops up the GUD buffer unless `gdb-show-main' is t. In this case
223 it starts with two windows: one displaying the GUD buffer and the
224 other with the source file with the main routine of the inferior.
226 If `gdb-many-windows' is t, regardless of the value of
227 `gdb-show-main', the layout below will appear unless
228 `gdb-use-separate-io-buffer' is nil when the source buffer
229 occupies the full width of the frame. Keybindings are shown in
232 Watch expressions appear in the speedbar/slowbar.
234 The following commands help control operation :
236 `gdb-many-windows' - Toggle the number of windows gdb uses.
237 `gdb-restore-windows' - To restore the window layout.
239 See Info node `(emacs)GDB Graphical Interface' for a more
240 detailed description of this mode.
243 +----------------------------------------------------------------------+
245 +-----------------------------------+----------------------------------+
246 | GUD buffer (I/O of GDB) | Locals buffer |
250 +-----------------------------------+----------------------------------+
251 | Source buffer | I/O buffer (of debugged program) |
259 +-----------------------------------+----------------------------------+
260 | Stack buffer | Breakpoints buffer |
261 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
262 | | RET gdb-goto-breakpoint |
263 | | D gdb-delete-breakpoint |
264 +-----------------------------------+----------------------------------+"
266 (interactive (list (gud-query-cmdline 'gdba)))
268 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
272 (defcustom gdb-debug-ring-max 128
273 "Maximum size of `gdb-debug-ring'."
278 (defvar gdb-debug-ring nil
279 "List of commands, most recent first, sent to and replies received from GDB.
280 This variable is used to debug GDB-UI.")
283 (defcustom gdb-enable-debug nil
284 "Non-nil means record the process input and output in `gdb-debug-ring'."
289 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
290 "Shell command for generating a list of defined macros in a source file.
291 This list is used to display the #define directive associated
292 with an identifier as a tooltip. It works in a debug session with
293 GDB, when gud-tooltip-mode is t.
295 Set `gdb-cpp-define-alist-flags' for any include paths or
301 (defcustom gdb-cpp-define-alist-flags ""
302 "Preprocessor flags for `gdb-cpp-define-alist-program'."
307 (defcustom gdb-show-main nil
308 "Non-nil means display source file containing the main routine at startup.
309 Also display the main routine in the disassembly buffer if present."
314 (defcustom gdb-many-windows nil
315 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
316 In this case it starts with two windows: one displaying the GUD
317 buffer and the other with the source file with the main routine
318 of the debugged program. Non-nil means display the layout shown for
324 (defcustom gdb-use-separate-io-buffer nil
325 "Non-nil means display output from the debugged program in a separate buffer."
330 (defun gdb-force-mode-line-update (status)
331 (let ((buffer gud-comint-buffer))
332 (if (and buffer (buffer-name buffer))
333 (with-current-buffer buffer
334 (setq mode-line-process
336 (process-status (get-buffer-process buffer)) status))
337 ;; Force mode line redisplay soon.
338 (force-mode-line-update)))))
340 (defun gdb-many-windows (arg)
341 "Toggle the number of windows in the basic arrangement.
342 With arg, display additional buffers iff arg is positive."
344 (setq gdb-many-windows
346 (not gdb-many-windows)
347 (> (prefix-numeric-value arg) 0)))
348 (message (format "Display of other windows %sabled"
349 (if gdb-many-windows "en" "dis")))
350 (if (and gud-comint-buffer
351 (buffer-name gud-comint-buffer))
353 (gdb-restore-windows)
356 (defun gdb-use-separate-io-buffer (arg)
357 "Toggle separate IO for debugged program.
358 With arg, use separate IO iff arg is positive."
360 (setq gdb-use-separate-io-buffer
362 (not gdb-use-separate-io-buffer)
363 (> (prefix-numeric-value arg) 0)))
364 (message (format "Separate IO %sabled"
365 (if gdb-use-separate-io-buffer "en" "dis")))
366 (if (and gud-comint-buffer
367 (buffer-name gud-comint-buffer))
369 (if gdb-use-separate-io-buffer
370 (if gdb-many-windows (gdb-restore-windows))
371 (kill-buffer (gdb-inferior-io-name)))
374 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
376 (defun gdb-create-define-alist ()
377 "Create an alist of #define directives for GUD tooltips."
378 (let* ((file (buffer-file-name))
380 (with-output-to-string
381 (with-current-buffer standard-output
382 (call-process shell-file-name
383 (if (file-exists-p file) file nil)
384 (list t nil) nil "-c"
385 (concat gdb-cpp-define-alist-program " "
386 gdb-cpp-define-alist-flags)))))
387 (define-list (split-string output "\n" t)) (name))
388 (setq gdb-define-alist nil)
389 (dolist (define define-list)
390 (setq name (nth 1 (split-string define "[( ]")))
391 (push (cons name define) gdb-define-alist))))
393 (defun gdb-tooltip-print (expr)
395 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
396 (goto-char (point-min))
398 (if (search-forward "=" nil t)
399 (concat expr (buffer-substring (- (point) 2) (point-max)))
401 ;; remove newline for gud-tooltip-echo-area
402 (substring string 0 (- (length string) 1))))
403 (or gud-tooltip-echo-area tooltip-use-echo-area)))
405 ;; If expr is a macro for a function don't print because of possible dangerous
406 ;; side-effects. Also printing a function within a tooltip generates an
407 ;; unexpected starting annotation (phase error).
408 (defun gdb-tooltip-print-1 (expr)
409 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
410 (goto-char (point-min))
411 (if (search-forward "expands to: " nil t)
412 (unless (looking-at "\\S-+.*(.*).*")
414 (list (concat gdb-server-prefix "print " expr "\n")
415 `(lambda () (gdb-tooltip-print ,expr))))))))
417 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
419 (defun gdb-set-gud-minor-mode-existing-buffers ()
420 "Create list of source files for current GDB session."
421 (goto-char (point-min))
422 (when (search-forward "read in on demand:" nil t)
423 (while (re-search-forward gdb-source-file-regexp nil t)
424 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
425 gdb-source-file-list))
426 (dolist (buffer (buffer-list))
427 (with-current-buffer buffer
428 (when (and buffer-file-name
429 (member (file-name-nondirectory buffer-file-name)
430 gdb-source-file-list))
431 (set (make-local-variable 'gud-minor-mode) 'gdba)
432 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
433 (when gud-tooltip-mode
434 (make-local-variable 'gdb-define-alist)
435 (gdb-create-define-alist)
436 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
437 (gdb-force-mode-line-update
438 (propertize "ready" 'face font-lock-variable-name-face)))
440 (defun gdb-find-watch-expression ()
441 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
442 (varnum (car var)) expr array)
443 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
444 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
445 (component-list (split-string (match-string 2 varnum) "\\." t)))
446 (setq expr (nth 1 var1))
447 (setq varnumlet (car var1))
448 (dolist (component component-list)
449 (setq var2 (assoc varnumlet gdb-var-list))
450 (setq expr (concat expr
451 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
452 (concat "[" component "]")
453 (concat "." component))))
454 (setq varnumlet (concat varnumlet "." component)))
458 (set (make-local-variable 'gud-minor-mode) 'gdba)
459 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
461 (gud-def gud-break (if (not (string-match "Machine" mode-name))
462 (gud-call "break %f:%l" arg)
466 (gud-call "break *%a" arg)))
467 "\C-b" "Set breakpoint at current line or address.")
469 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
470 (gud-call "clear %f:%l" arg)
474 (gud-call "clear *%a" arg)))
475 "\C-d" "Remove breakpoint at current line or address.")
477 (gud-def gud-until (if (not (string-match "Machine" mode-name))
478 (gud-call "until %f:%l" arg)
482 (gud-call "until *%a" arg)))
483 "\C-u" "Continue to current line or address.")
485 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
486 nil "Start or continue execution.")
488 ;; For debugging Emacs only.
492 "pp1 " (if (eq (buffer-local-value
493 'major-mode (window-buffer)) 'speedbar-mode)
494 (gdb-find-watch-expression) "%e")) arg)
495 nil "Print the emacs s-expression.")
497 (define-key gud-minor-mode-map [left-margin mouse-1]
498 'gdb-mouse-set-clear-breakpoint)
499 (define-key gud-minor-mode-map [left-fringe mouse-1]
500 'gdb-mouse-set-clear-breakpoint)
501 (define-key gud-minor-mode-map [left-margin C-mouse-1]
502 'gdb-mouse-toggle-breakpoint-margin)
503 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
504 'gdb-mouse-toggle-breakpoint-fringe)
506 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
508 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
510 (define-key gud-minor-mode-map [left-margin mouse-3]
512 (define-key gud-minor-mode-map [left-fringe mouse-3]
515 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
517 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
519 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
521 (define-key gud-minor-mode-map [left-margin C-mouse-3]
524 (setq comint-input-sender 'gdb-send)
527 (setq gdb-pc-address (if gdb-show-main "main" nil))
528 (setq gdb-previous-frame-address nil
529 gdb-memory-address "main"
530 gdb-previous-frame nil
531 gdb-selected-frame nil
532 gdb-current-language nil
536 gdb-first-post-prompt t
540 gdb-pending-triggers nil
541 gdb-output-sink 'user
542 gdb-server-prefix "server "
543 gdb-flush-pending-output nil
544 gdb-location-alist nil
545 gdb-source-file-list nil
548 gdb-buffer-fringe-width (car (window-fringes))
551 gdb-source-window nil
552 gdb-inferior-status nil
554 gdb-look-up-stack nil
559 (setq gdb-buffer-type 'gdba)
561 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
563 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
564 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
568 (if (eq window-system 'w32)
569 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
570 (gdb-enqueue-input (list "set height 0\n" 'ignore))
571 (gdb-enqueue-input (list "set width 0\n" 'ignore))
573 (if (string-equal gdb-version "pre-6.4")
575 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
576 'gdb-set-gud-minor-mode-existing-buffers))
577 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
579 (list "server interpreter mi -data-list-register-names\n"
580 'gdb-get-register-names))
581 ; Needs GDB 6.2 onwards.
583 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
584 'gdb-set-gud-minor-mode-existing-buffers-1))
585 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
587 ;; Find source file and compilation directory here.
588 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
589 (gdb-enqueue-input (list "server list\n" 'ignore))
590 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
592 (run-hooks 'gdba-mode-hook))
594 (defun gdb-get-version ()
595 (goto-char (point-min))
596 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
597 (setq gdb-version "pre-6.4")
598 (setq gdb-version "6.4+"))
601 (defun gdb-mouse-until (event)
602 "Continue running until a source line past the current line.
603 The destination source line can be selected either by clicking with mouse-2
604 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
606 (if gud-overlay-arrow-position
607 (let ((start (event-start event))
608 (end (event-end event))
609 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
610 (if (not (string-match "Machine" mode-name))
611 (if (equal buffer (window-buffer (posn-window end)))
612 (with-current-buffer buffer
613 (when (or (equal start end)
614 (equal (posn-point start)
616 gud-overlay-arrow-position)))
617 (setq line (line-number-at-pos (posn-point end)))
618 (gud-call (concat "until " (number-to-string line))))))
619 (if (equal (marker-buffer gdb-overlay-arrow-position)
620 (window-buffer (posn-window end)))
621 (when (or (equal start end)
622 (equal (posn-point start)
624 gdb-overlay-arrow-position)))
626 (goto-line (line-number-at-pos (posn-point end)))
628 (gud-call (concat "until *%a")))))))))
630 (defun gdb-mouse-jump (event)
631 "Set execution address/line.
632 The destination source line can be selected either by clicking with mouse-2
633 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
634 Unlike gdb-mouse-until the destination address can be before the current
635 line, and no execution takes place."
637 (if gud-overlay-arrow-position
638 (let ((start (event-start event))
639 (end (event-end event))
640 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
641 (if (not (string-match "Machine" mode-name))
642 (if (equal buffer (window-buffer (posn-window end)))
643 (with-current-buffer buffer
644 (when (or (equal start end)
645 (equal (posn-point start)
647 gud-overlay-arrow-position)))
648 (setq line (line-number-at-pos (posn-point end)))
649 (progn (gud-call (concat "tbreak " (number-to-string line)))
650 (gud-call (concat "jump " (number-to-string line)))))))
651 (if (equal (marker-buffer gdb-overlay-arrow-position)
652 (window-buffer (posn-window end)))
653 (when (or (equal start end)
654 (equal (posn-point start)
656 gdb-overlay-arrow-position)))
658 (goto-line (line-number-at-pos (posn-point end)))
661 (gud-call (concat "tbreak *%a"))
662 (gud-call (concat "jump *%a"))))))))))
664 (defcustom gdb-speedbar-auto-raise nil
665 "If non-nil raise speedbar every time display of watch expressions is\
671 (defun gdb-speedbar-auto-raise (arg)
672 "Toggle automatic raising of the speedbar for watch expressions.
673 With arg, automatically raise speedbar iff arg is positive."
675 (setq gdb-speedbar-auto-raise
677 (not gdb-speedbar-auto-raise)
678 (> (prefix-numeric-value arg) 0)))
679 (message (format "Auto raising %sabled"
680 (if gdb-speedbar-auto-raise "en" "dis"))))
682 (defcustom gdb-use-colon-colon-notation nil
683 "If non-nil use FUN::VAR format to display variables in the speedbar."
688 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
689 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
691 (defun gud-watch (&optional arg event)
692 "Watch expression at point.
693 With arg, enter name of variable to be watched in the minibuffer."
694 (interactive (list current-prefix-arg last-input-event))
695 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
696 (if (memq minor-mode '(gdbmi gdba))
698 (if event (posn-set-point (event-end event)))
700 (save-selected-window
703 (completing-read "Name of variable: "
704 'gud-gdb-complete-command)
705 (if (and transient-mark-mode mark-active)
706 (buffer-substring (region-beginning) (region-end))
707 (tooltip-identifier-from-point (point))))))
709 (catch 'already-watched
710 (dolist (var gdb-var-list)
711 (unless (string-match "\\." (car var))
712 (if (string-equal expr (nth 1 var))
713 (throw 'already-watched nil))))
714 (set-text-properties 0 (length expr) nil expr)
717 (if (eq minor-mode 'gdba)
719 "server interpreter mi \"-var-create - * " expr "\"\n")
720 (concat"-var-create - * " expr "\n"))
721 `(lambda () (gdb-var-create-handler ,expr))))))))
722 (message "gud-watch is a no-op in this mode."))))
724 (defconst gdb-var-create-regexp
725 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",.*type=\"\\(.*?\\)\"")
727 (defun gdb-var-create-handler (expr)
728 (goto-char (point-min))
729 (if (re-search-forward gdb-var-create-regexp nil t)
732 (if (and (string-equal gdb-current-language "c")
733 gdb-use-colon-colon-notation gdb-selected-frame)
734 (setq expr (concat gdb-selected-frame "::" expr))
738 nil nil gdb-frame-address)))
739 (push var gdb-var-list)
740 (unless (string-equal
741 speedbar-initial-expansion-list-name "GUD")
742 (speedbar-change-initial-expansion-list "GUD"))
745 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
746 (concat "server interpreter mi \"0-var-evaluate-expression "
748 (concat "0-var-evaluate-expression " (car var) "\n"))
749 `(lambda () (gdb-var-evaluate-expression-handler
751 (if (search-forward "Undefined command" nil t)
752 (message-box "Watching expressions requires GDB 6.0 onwards")
753 (message-box "No symbol \"%s\" in current context." expr))))
755 (defun gdb-speedbar-update ()
756 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
757 ;; Dummy command to update speedbar even when idle.
758 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
759 ;; Keep gdb-pending-triggers non-nil till end.
760 (push 'gdb-speedbar-timer gdb-pending-triggers)))
762 (defun gdb-speedbar-timer-fn ()
763 (setq gdb-pending-triggers
764 (delq 'gdb-speedbar-timer gdb-pending-triggers))
767 (defun gdb-var-evaluate-expression-handler (varnum changed)
768 (goto-char (point-min))
769 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t)
770 (setq gdb-pending-triggers
771 (delq (string-to-number (match-string 1)) gdb-pending-triggers))
772 (let ((var (assoc varnum gdb-var-list)))
774 (if changed (setcar (nthcdr 5 var) 'changed))
775 (setcar (nthcdr 4 var) (read (match-string 2)))))
776 (gdb-speedbar-update))
778 (defun gdb-var-list-children (varnum)
780 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
781 `(lambda () (gdb-var-list-children-handler ,varnum)))))
783 (defconst gdb-var-list-children-regexp
784 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
785 numchild=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\".*?}")
787 (defun gdb-var-list-children-handler (varnum)
788 (goto-char (point-min))
789 (let ((var-list nil))
790 (catch 'child-already-watched
791 (dolist (var gdb-var-list)
792 (if (string-equal varnum (car var))
795 (while (re-search-forward gdb-var-list-children-regexp nil t)
796 (let ((varchild (list (match-string 1)
801 (if (assoc (car varchild) gdb-var-list)
802 (throw 'child-already-watched nil))
803 (push varchild var-list)
807 "server interpreter mi \"0-var-evaluate-expression "
808 (car varchild) "\"\n")
809 `(lambda () (gdb-var-evaluate-expression-handler
810 ,(car varchild) nil)))))))
811 (push var var-list)))
812 (setq gdb-var-list (nreverse var-list)))))
814 (defun gdb-var-update ()
815 (when (not (member 'gdb-var-update gdb-pending-triggers))
817 (list "server interpreter mi \"-var-update *\"\n"
818 'gdb-var-update-handler))
819 (push 'gdb-var-update gdb-pending-triggers)))
821 (defconst gdb-var-update-regexp
822 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
823 type_changed=\".*?\".*?}")
825 (defun gdb-var-update-handler ()
826 (dolist (var gdb-var-list)
827 (setcar (nthcdr 5 var) nil))
828 (goto-char (point-min))
830 (while (re-search-forward gdb-var-update-regexp nil t)
831 (let ((varnum (match-string 1)))
832 (if (string-equal (match-string 2) "false")
833 (let ((var (assoc varnum gdb-var-list)))
834 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
836 (push n gdb-pending-triggers)
839 (concat "server interpreter mi \"" (number-to-string n)
840 "-var-evaluate-expression " varnum "\"\n")
841 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))))))
842 (setq gdb-pending-triggers
843 (delq 'gdb-var-update gdb-pending-triggers)))
845 (defun gdb-var-delete ()
846 "Delete watch expression at point from the speedbar."
848 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
850 (let ((text (speedbar-line-text)))
851 ;; Can't use \\S-+ for whitespace because
852 ;; speedbar has a whacky syntax table.
853 (string-match "\\([^ \t]+\\)" text)
854 (let ((expr (match-string 1 text)) var varnum)
856 (dolist (var1 gdb-var-list)
857 (when (string-equal expr (nth 1 var1))
859 (setq varnum (car var1))
860 (throw 'expr-found nil))))
861 (unless (string-match "\\." (car var))
864 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
866 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
867 (concat "-var-delete " varnum "\n"))
869 (setq gdb-var-list (delq var gdb-var-list))
870 (dolist (varchild gdb-var-list)
871 (if (string-match (concat (car var) "\\.") (car varchild))
872 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
874 (defun gdb-edit-value (text token indent)
875 "Assign a value to a variable displayed in the speedbar."
876 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
877 (varnum (car var)) (value))
878 (setq value (read-string "New value: "))
881 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
882 (concat "server interpreter mi \"-var-assign "
883 varnum " " value "\"\n")
884 (concat "-var-assign " varnum " " value "\n"))
885 `(lambda () (gdb-edit-value-handler ,value))))))
887 (defun gdb-edit-value-handler (value)
888 (goto-char (point-min))
889 (if (re-search-forward gdb-error-regexp nil t)
890 (message-box "Invalid number or expression (%s)" value)))
892 (defcustom gdb-show-changed-values t
893 "If non-nil change the face of out of scope variables and changed values.
894 Out of scope variables are suppressed with `shadow' face.
895 Changed values are highlighted with the face `font-lock-warning-face'."
900 (defun gdb-speedbar-expand-node (text token indent)
901 "Expand the node the user clicked on.
902 TEXT is the text of the button we clicked on, a + or - item.
903 TOKEN is data related to this node.
904 INDENT is the current indentation depth."
905 (cond ((string-match "+" text) ;expand this node
907 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
908 (string-equal gdb-version "pre-6.4"))
909 (gdb-var-list-children token)
910 (gdb-var-list-children-1 token)))
911 ((string-match "-" text) ;contract this node
912 (dolist (var gdb-var-list)
913 (if (string-match (concat token "\\.") (car var))
914 (setq gdb-var-list (delq var gdb-var-list))))
915 (speedbar-change-expand-button-char ?+)
916 (speedbar-delete-subblock indent))
917 (t (error "Ooops... not sure what to do")))
918 (speedbar-center-buffer-smartly))
920 (defun gdb-get-target-string ()
921 (with-current-buffer gud-comint-buffer
928 ;; Each buffer has a TYPE -- a symbol that identifies the function
929 ;; of that particular buffer.
931 ;; The usual gdb interaction buffer is given the type `gdba' and
932 ;; is constructed specially.
934 ;; Others are constructed by gdb-get-buffer-create and
935 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
937 (defvar gdb-buffer-rules-assoc '())
939 (defun gdb-get-buffer (key)
940 "Return the gdb buffer tagged with type KEY.
941 The key should be one of the cars in `gdb-buffer-rules-assoc'."
943 (gdb-look-for-tagged-buffer key (buffer-list))))
945 (defun gdb-get-buffer-create (key)
946 "Create a new gdb buffer of the type specified by KEY.
947 The key should be one of the cars in `gdb-buffer-rules-assoc'."
948 (or (gdb-get-buffer key)
949 (let* ((rules (assoc key gdb-buffer-rules-assoc))
950 (name (funcall (gdb-rules-name-maker rules)))
951 (new (get-buffer-create name)))
952 (with-current-buffer new
954 (if (cdr (cdr rules))
955 (setq trigger (funcall (car (cdr (cdr rules))))))
956 (setq gdb-buffer-type key)
957 (set (make-local-variable 'gud-minor-mode)
958 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
959 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
960 (if trigger (funcall trigger)))
963 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
965 (defun gdb-look-for-tagged-buffer (key bufs)
967 (while (and (not retval) bufs)
968 (set-buffer (car bufs))
969 (if (eq gdb-buffer-type key)
970 (setq retval (car bufs)))
971 (setq bufs (cdr bufs)))
975 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
976 ;; at least one and possible more functions. The functions have these
977 ;; roles in defining a buffer type:
979 ;; NAME - Return a name for this buffer type.
981 ;; The remaining function(s) are optional:
983 ;; MODE - called in a new buffer with no arguments, should establish
984 ;; the proper mode for the buffer.
987 (defun gdb-set-buffer-rules (buffer-type &rest rules)
988 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
990 (setcdr binding rules)
991 (push (cons buffer-type rules)
992 gdb-buffer-rules-assoc))))
994 ;; GUD buffers are an exception to the rules
995 (gdb-set-buffer-rules 'gdba 'error)
997 ;; Partial-output buffer : This accumulates output from a command executed on
998 ;; behalf of emacs (rather than the user).
1000 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1001 'gdb-partial-output-name)
1003 (defun gdb-partial-output-name ()
1004 (concat "*partial-output-"
1005 (gdb-get-target-string)
1009 (gdb-set-buffer-rules 'gdb-inferior-io
1010 'gdb-inferior-io-name
1011 'gdb-inferior-io-mode)
1013 (defun gdb-inferior-io-name ()
1014 (concat "*input/output of "
1015 (gdb-get-target-string)
1018 (defun gdb-display-separate-io-buffer ()
1019 "Display IO of debugged program in a separate window."
1021 (if gdb-use-separate-io-buffer
1023 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1025 (defconst gdb-frame-parameters
1026 '((height . 14) (width . 80)
1028 (tool-bar-lines . nil)
1029 (menu-bar-lines . nil)
1030 (minibuffer . nil)))
1032 (defun gdb-frame-separate-io-buffer ()
1033 "Display IO of debugged program in a new frame."
1035 (if gdb-use-separate-io-buffer
1036 (let ((special-display-regexps (append special-display-regexps '(".*")))
1037 (special-display-frame-alist gdb-frame-parameters))
1038 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1040 (defvar gdb-inferior-io-mode-map
1041 (let ((map (make-sparse-keymap)))
1042 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1043 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1044 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1045 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1046 (define-key map "\C-d" 'gdb-separate-io-eof)
1049 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1050 "Major mode for gdb inferior-io."
1051 :syntax-table nil :abbrev-table nil
1052 ;; We want to use comint because it has various nifty and familiar
1053 ;; features. We don't need a process, but comint wants one, so create
1055 (make-comint-in-buffer
1056 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1057 (current-buffer) "hexl")
1058 (setq comint-input-sender 'gdb-inferior-io-sender))
1060 (defun gdb-inferior-io-sender (proc string)
1061 ;; PROC is the pseudo-process created to satisfy comint.
1062 (with-current-buffer (process-buffer proc)
1063 (setq proc (get-buffer-process gud-comint-buffer))
1064 (process-send-string proc string)
1065 (process-send-string proc "\n")))
1067 (defun gdb-separate-io-interrupt ()
1068 "Interrupt the program being debugged."
1071 (get-buffer-process gud-comint-buffer) comint-ptyp))
1073 (defun gdb-separate-io-quit ()
1074 "Send quit signal to the program being debugged."
1077 (get-buffer-process gud-comint-buffer) comint-ptyp))
1079 (defun gdb-separate-io-stop ()
1080 "Stop the program being debugged."
1083 (get-buffer-process gud-comint-buffer) comint-ptyp))
1085 (defun gdb-separate-io-eof ()
1086 "Send end-of-file to the program being debugged."
1089 (get-buffer-process gud-comint-buffer)))
1092 ;; gdb communications
1095 ;; INPUT: things sent to gdb
1097 ;; The queues are lists. Each element is either a string (indicating user or
1098 ;; user-like input) or a list of the form:
1100 ;; (INPUT-STRING HANDLER-FN)
1102 ;; The handler function will be called from the partial-output buffer when the
1103 ;; command completes. This is the way to write commands which invoke gdb
1104 ;; commands autonomously.
1106 ;; These lists are consumed tail first.
1109 (defun gdb-send (proc string)
1110 "A comint send filter for gdb.
1111 This filter may simply queue input for a later time."
1112 (with-current-buffer gud-comint-buffer
1113 (let ((inhibit-read-only t))
1114 (remove-text-properties (point-min) (point-max) '(face))))
1117 (let ((item (concat string "\n")))
1118 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1119 (process-send-string proc item)))
1120 (if (and (string-match "\\\\$" string)
1121 (not comint-input-sender-no-newline)) ;;Try to catch C-d.
1122 (setq gdb-continuation (concat gdb-continuation string "\n"))
1123 (let ((item (concat gdb-continuation string "\n")))
1124 (gdb-enqueue-input item)
1125 (setq gdb-continuation nil)))))
1127 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1128 ;; is a query, or other non-top-level prompt.
1130 (defun gdb-enqueue-input (item)
1131 (if (not gud-running)
1134 (gdb-send-item item)
1135 (setq gdb-prompting nil))
1136 (push item gdb-input-queue))))
1138 (defun gdb-dequeue-input ()
1139 (let ((queue gdb-input-queue))
1141 (let ((last (car (last queue))))
1142 (unless (nbutlast queue) (setq gdb-input-queue '()))
1145 (defun gdb-send-item (item)
1146 (setq gdb-flush-pending-output nil)
1147 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1148 (setq gdb-current-item item)
1149 (let ((process (get-buffer-process gud-comint-buffer)))
1150 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1153 (setq gdb-output-sink 'user)
1154 (process-send-string process item))
1156 (gdb-clear-partial-output)
1157 (setq gdb-output-sink 'pre-emacs)
1158 (process-send-string process
1160 ;; case: eq gud-minor-mode 'gdbmi
1161 (gdb-clear-partial-output)
1162 (setq gdb-output-sink 'emacs)
1163 (process-send-string process (car item)))))
1166 ;; output -- things gdb prints to emacs
1168 ;; GDB output is a stream interrupted by annotations.
1169 ;; Annotations can be recognized by their beginning
1170 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1172 ;; The tag is a string obeying symbol syntax.
1174 ;; The optional part `<opt>' can be either the empty string
1175 ;; or a space followed by more data relating to the annotation.
1176 ;; For example, the SOURCE annotation is followed by a filename,
1177 ;; line number and various useless goo. This data must not include
1181 (defcustom gud-gdba-command-name "gdb -annotate=3"
1182 "Default command to execute an executable under the GDB-UI debugger."
1187 (defvar gdb-annotation-rules
1188 '(("pre-prompt" gdb-pre-prompt)
1189 ("prompt" gdb-prompt)
1190 ("commands" gdb-subprompt)
1191 ("overload-choice" gdb-subprompt)
1192 ("query" gdb-subprompt)
1193 ;; Need this prompt for GDB 6.1
1194 ("nquery" gdb-subprompt)
1195 ("prompt-for-continue" gdb-subprompt)
1196 ("post-prompt" gdb-post-prompt)
1197 ("source" gdb-source)
1198 ("starting" gdb-starting)
1199 ("exited" gdb-exited)
1200 ("signalled" gdb-signalled)
1201 ("signal" gdb-signal)
1202 ("breakpoint" gdb-stopping)
1203 ("watchpoint" gdb-stopping)
1204 ("frame-begin" gdb-frame-begin)
1205 ("stopped" gdb-stopped)
1206 ("error-begin" gdb-error)
1208 ) "An assoc mapping annotation tags to functions which process them.")
1211 (setq gdb-flush-pending-output t)
1212 (setq gud-running nil)
1213 (gdb-force-mode-line-update
1214 (propertize "stopped"'face font-lock-warning-face))
1215 (setq gdb-output-sink 'user)
1216 (setq gdb-input-queue nil)
1217 (setq gdb-pending-triggers nil)
1218 (setq gdb-prompting t))
1220 (defconst gdb-source-spec-regexp
1221 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1223 ;; Do not use this except as an annotation handler.
1224 (defun gdb-source (args)
1225 (string-match gdb-source-spec-regexp args)
1226 ;; Extract the frame position from the marker.
1227 (setq gud-last-frame
1229 (match-string 1 args)
1230 (string-to-number (match-string 2 args))))
1231 (setq gdb-pc-address (match-string 3 args))
1232 ;; cover for auto-display output which comes *before*
1233 ;; stopped annotation
1234 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1236 (defun gdb-pre-prompt (ignored)
1237 "An annotation handler for `pre-prompt'.
1238 This terminates the collection of output from a previous command if that
1239 happens to be in effect."
1240 (setq gdb-error nil)
1241 (let ((sink gdb-output-sink))
1245 (setq gdb-output-sink 'post-emacs))
1248 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1250 (defun gdb-prompt (ignored)
1251 "An annotation handler for `prompt'.
1252 This sends the next command (if any) to gdb."
1253 (when gdb-first-prompt
1254 (gdb-force-mode-line-update
1255 (propertize "initializing..." 'face font-lock-variable-name-face))
1257 (setq gdb-first-prompt nil))
1258 (let ((sink gdb-output-sink))
1261 ((eq sink 'post-emacs)
1262 (setq gdb-output-sink 'user)
1264 (car (cdr gdb-current-item))))
1265 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1266 (funcall handler))))
1269 (error "Phase error in gdb-prompt (got %s)" sink))))
1270 (let ((input (gdb-dequeue-input)))
1272 (gdb-send-item input)
1274 (setq gdb-prompting t)
1275 (gud-display-frame)))))
1277 (defun gdb-subprompt (ignored)
1278 "An annotation handler for non-top-level prompts."
1279 (setq gdb-prompting t))
1281 (defun gdb-starting (ignored)
1282 "An annotation handler for `starting'.
1283 This says that I/O for the subprocess is now the program being debugged,
1285 (setq gdb-active-process t)
1286 (setq gdb-printing t)
1287 (let ((sink gdb-output-sink))
1291 (setq gud-running t)
1292 (setq gdb-inferior-status "running")
1293 (setq gdb-signalled nil)
1294 (gdb-force-mode-line-update
1295 (propertize gdb-inferior-status 'face font-lock-type-face))
1296 (gdb-remove-text-properties)
1297 (setq gud-old-arrow gud-overlay-arrow-position)
1298 (setq gud-overlay-arrow-position nil)
1299 (setq gdb-overlay-arrow-position nil)
1300 (setq gdb-stack-position nil)
1301 (if gdb-use-separate-io-buffer
1302 (setq gdb-output-sink 'inferior))))
1305 (error "Unexpected `starting' annotation")))))
1307 (defun gdb-signal (ignored)
1308 (setq gdb-inferior-status "signal")
1309 (gdb-force-mode-line-update
1310 (propertize gdb-inferior-status 'face font-lock-warning-face))
1311 (gdb-stopping ignored))
1313 (defun gdb-stopping (ignored)
1314 "An annotation handler for `breakpoint' and other annotations.
1315 They say that I/O for the subprocess is now GDB, not the program
1317 (if gdb-use-separate-io-buffer
1318 (let ((sink gdb-output-sink))
1320 ((eq sink 'inferior)
1321 (setq gdb-output-sink 'user))
1324 (error "Unexpected stopping annotation"))))))
1326 (defun gdb-exited (ignored)
1327 "An annotation handler for `exited' and `signalled'.
1328 They say that I/O for the subprocess is now GDB, not the program
1329 being debugged and that the program is no longer running. This
1330 function is used to change the focus of GUD tooltips to #define
1332 (setq gdb-active-process nil)
1333 (setq gud-overlay-arrow-position nil)
1334 (setq gdb-overlay-arrow-position nil)
1335 (setq gdb-stack-position nil)
1336 (setq gud-old-arrow nil)
1337 (setq gdb-inferior-status "exited")
1338 (gdb-force-mode-line-update
1339 (propertize gdb-inferior-status 'face font-lock-warning-face))
1340 (gdb-stopping ignored))
1342 (defun gdb-signalled (ignored)
1343 (setq gdb-signalled t))
1345 (defun gdb-frame-begin (ignored)
1346 (setq gdb-frame-begin t)
1347 (setq gdb-printing nil)
1348 (let ((sink gdb-output-sink))
1350 ((eq sink 'inferior)
1351 (setq gdb-output-sink 'user))
1353 ((eq sink 'emacs) t)
1356 (error "Unexpected frame-begin annotation (%S)" sink)))))
1358 (defcustom gdb-same-frame focus-follows-mouse
1359 "Non-nil means pop up GUD buffer in same frame."
1364 (defcustom gdb-find-source-frame nil
1365 "Non-nil means try to find a source frame further up stack e.g after signal."
1370 (defun gdb-find-source-frame (arg)
1371 "Toggle trying to find a source frame further up stack.
1372 With arg, look for a source frame further up stack iff arg is positive."
1374 (setq gdb-find-source-frame
1376 (not gdb-find-source-frame)
1377 (> (prefix-numeric-value arg) 0)))
1378 (message (format "Looking for source frame %sabled"
1379 (if gdb-find-source-frame "en" "dis"))))
1381 (defun gdb-stopped (ignored)
1382 "An annotation handler for `stopped'.
1383 It is just like `gdb-stopping', except that if we already set the output
1384 sink to `user' in `gdb-stopping', that is fine."
1385 (setq gud-running nil)
1386 (unless (or gud-overlay-arrow-position gud-last-frame)
1387 (if (and gdb-frame-begin gdb-printing)
1388 (setq gud-overlay-arrow-position gud-old-arrow)
1389 ;;Pop up GUD buffer to display current frame when it doesn't have source
1390 ;;information i.e id not compiled with -g as with libc routines generally.
1392 (gdb-display-gdb-buffer)
1393 (gdb-frame-gdb-buffer))
1394 (if gdb-find-source-frame
1395 ;;Try to find source further up stack e.g after signal.
1396 (setq gdb-look-up-stack
1397 (if (gdb-get-buffer 'gdb-stack-buffer)
1400 (gdb-get-buffer-create 'gdb-stack-buffer)
1401 (gdb-invalidate-frames)
1403 (unless (member gdb-inferior-status '("exited" "signal"))
1404 (setq gdb-inferior-status "stopped")
1405 (gdb-force-mode-line-update
1406 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1407 (let ((sink gdb-output-sink))
1409 ((eq sink 'inferior)
1410 (setq gdb-output-sink 'user))
1414 (error "Unexpected stopped annotation"))))
1415 (if gdb-signalled (gdb-exited ignored)))
1417 (defun gdb-error (ignored)
1418 (setq gdb-error (not gdb-error)))
1420 (defun gdb-post-prompt (ignored)
1421 "An annotation handler for `post-prompt'.
1422 This begins the collection of output from the current command if that
1423 happens to be appropriate."
1424 ;; Don't add to queue if there outstanding items or gdb-version is not known
1426 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1427 (gdb-get-selected-frame)
1428 (gdb-invalidate-frames)
1429 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1430 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1431 (gdb-invalidate-breakpoints)
1432 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1433 ;; so gdb-pc-address is updated.
1434 ;; (gdb-invalidate-assembler)
1436 (if (string-equal gdb-version "pre-6.4")
1437 (gdb-invalidate-registers)
1438 (gdb-get-changed-registers)
1439 (gdb-invalidate-registers-1))
1441 (gdb-invalidate-memory)
1442 (if (string-equal gdb-version "pre-6.4")
1443 (gdb-invalidate-locals)
1444 (gdb-invalidate-locals-1))
1446 (gdb-invalidate-threads)
1447 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1448 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1449 ;; Only needed/used with speedbar/watch expressions.
1450 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1451 (if (string-equal gdb-version "pre-6.4")
1453 (gdb-var-update-1)))))
1454 (setq gdb-first-post-prompt nil)
1455 (let ((sink gdb-output-sink))
1458 ((eq sink 'pre-emacs)
1459 (setq gdb-output-sink 'emacs))
1462 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1464 (defconst gdb-buffer-list
1465 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1467 (defun gdb-remove-text-properties ()
1468 (dolist (buffertype gdb-buffer-list)
1469 (let ((buffer (gdb-get-buffer buffertype)))
1471 (with-current-buffer buffer
1472 (let ((inhibit-read-only t))
1473 (remove-text-properties
1474 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1476 ;; GUD displays the selected GDB frame. This might might not be the current
1477 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1478 ;; visited breakpoint is, use that window.
1479 (defun gdb-display-source-buffer (buffer)
1480 (let* ((last-window (if gud-last-last-frame
1482 (gud-find-file (car gud-last-last-frame)))))
1483 (source-window (or last-window
1484 (if (and gdb-source-window
1485 (window-live-p gdb-source-window))
1486 gdb-source-window))))
1488 (setq gdb-source-window source-window)
1489 (set-window-buffer source-window buffer))
1492 (defun gud-gdba-marker-filter (string)
1493 "A gud marker filter for gdb. Handle a burst of output from GDB."
1494 (if gdb-flush-pending-output
1496 (when gdb-enable-debug
1497 (push (cons 'recv string) gdb-debug-ring)
1498 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1499 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1500 ;; Recall the left over gud-marker-acc from last time.
1501 (setq gud-marker-acc (concat gud-marker-acc string))
1502 ;; Start accumulating output for the GUD buffer.
1505 ;; Process all the complete markers in this chunk.
1506 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1507 (let ((annotation (match-string 1 gud-marker-acc)))
1509 ;; Stuff prior to the match is just ordinary output.
1510 ;; It is either concatenated to OUTPUT or directed
1515 (substring gud-marker-acc 0 (match-beginning 0))))
1517 ;; Take that stuff off the gud-marker-acc.
1518 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1520 ;; Parse the tag from the annotation, and maybe its arguments.
1521 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1522 (let* ((annotation-type (match-string 1 annotation))
1523 (annotation-arguments (match-string 2 annotation))
1524 (annotation-rule (assoc annotation-type
1525 gdb-annotation-rules)))
1526 ;; Call the handler for this annotation.
1528 (funcall (car (cdr annotation-rule))
1529 annotation-arguments)
1530 ;; Else the annotation is not recognized. Ignore it silently,
1531 ;; so that GDB can add new annotations without causing
1535 ;; Does the remaining text end in a partial line?
1536 ;; If it does, then keep part of the gud-marker-acc until we get more.
1537 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1540 ;; Everything before the potential marker start can be output.
1542 (gdb-concat-output output
1543 (substring gud-marker-acc 0
1544 (match-beginning 0))))
1546 ;; Everything after, we save, to combine with later input.
1547 (setq gud-marker-acc (substring gud-marker-acc
1548 (match-beginning 0))))
1550 ;; In case we know the gud-marker-acc contains no partial annotations:
1552 (setq output (gdb-concat-output output gud-marker-acc))
1553 (setq gud-marker-acc "")))
1556 (defun gdb-concat-output (so-far new)
1558 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1559 (let ((sink gdb-output-sink))
1561 ((eq sink 'user) (concat so-far new))
1562 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1564 (gdb-append-to-partial-output new)
1566 ((eq sink 'inferior)
1567 (gdb-append-to-inferior-io new)
1571 (error "Bogon output sink %S" sink)))))
1573 (defun gdb-append-to-partial-output (string)
1574 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1575 (goto-char (point-max))
1578 (defun gdb-clear-partial-output ()
1579 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1582 (defun gdb-append-to-inferior-io (string)
1583 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1584 (goto-char (point-max))
1585 (insert-before-markers string))
1586 (if (not (string-equal string ""))
1587 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1589 (defun gdb-clear-inferior-io ()
1590 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1594 ;; One trick is to have a command who's output is always available in a buffer
1595 ;; of it's own, and is always up to date. We build several buffers of this
1598 ;; There are two aspects to this: gdb has to tell us when the output for that
1599 ;; command might have changed, and we have to be able to run the command
1600 ;; behind the user's back.
1602 ;; The output phasing associated with the variable gdb-output-sink
1603 ;; help us to run commands behind the user's back.
1605 ;; Below is the code for specificly managing buffers of output from one
1609 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1610 ;; It adds an input for the command we are tracking. It should be the
1611 ;; annotation rule binding of whatever gdb sends to tell us this command
1612 ;; might have changed it's output.
1614 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1615 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1616 ;; input in the input queue (see comment about ``gdb communications'' above).
1618 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1620 `(defun ,name (&optional ignored)
1621 (if (and ,demand-predicate
1623 gdb-pending-triggers)))
1626 (list ,gdb-command ',output-handler))
1627 (push ',name gdb-pending-triggers)))))
1629 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1631 (setq gdb-pending-triggers
1633 gdb-pending-triggers))
1634 (let ((buf (gdb-get-buffer ',buf-key)))
1636 (with-current-buffer buf
1637 (let* ((window (get-buffer-window buf 0))
1638 (start (window-start window))
1639 (p (window-point window))
1640 (buffer-read-only nil))
1642 (insert-buffer-substring (gdb-get-buffer-create
1643 'gdb-partial-output-buffer))
1644 (set-window-start window start)
1645 (set-window-point window p)))))
1646 ;; put customisation here
1649 (defmacro def-gdb-auto-updated-buffer (buffer-key
1650 trigger-name gdb-command
1651 output-handler-name custom-defun)
1653 (def-gdb-auto-update-trigger ,trigger-name
1654 ;; The demand predicate:
1655 (gdb-get-buffer ',buffer-key)
1657 ,output-handler-name)
1658 (def-gdb-auto-update-handler ,output-handler-name
1659 ,trigger-name ,buffer-key ,custom-defun)))
1663 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1665 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1666 'gdb-breakpoints-buffer-name
1667 'gdb-breakpoints-mode)
1669 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1670 ;; This defines the auto update rule for buffers of type
1671 ;; `gdb-breakpoints-buffer'.
1673 ;; It defines a function to serve as the annotation handler that
1674 ;; handles the `foo-invalidated' message. That function is called:
1675 gdb-invalidate-breakpoints
1677 ;; To update the buffer, this command is sent to gdb.
1678 "server info breakpoints\n"
1680 ;; This also defines a function to be the handler for the output
1681 ;; from the command above. That function will copy the output into
1682 ;; the appropriately typed buffer. That function will be called:
1683 gdb-info-breakpoints-handler
1684 ;; buffer specific functions
1685 gdb-info-breakpoints-custom)
1687 (defconst breakpoint-xpm-data
1689 static char *magick[] = {
1690 /* columns rows colors chars-per-pixel */
1706 "XPM data used for breakpoint icon.")
1708 (defconst breakpoint-enabled-pbm-data
1711 0 0 0 0 1 1 1 1 0 0 0 0
1712 0 0 0 1 1 1 1 1 1 0 0 0
1713 0 0 1 1 1 1 1 1 1 1 0 0
1714 0 1 1 1 1 1 1 1 1 1 1 0
1715 0 1 1 1 1 1 1 1 1 1 1 0
1716 0 1 1 1 1 1 1 1 1 1 1 0
1717 0 1 1 1 1 1 1 1 1 1 1 0
1718 0 0 1 1 1 1 1 1 1 1 0 0
1719 0 0 0 1 1 1 1 1 1 0 0 0
1720 0 0 0 0 1 1 1 1 0 0 0 0"
1721 "PBM data used for enabled breakpoint icon.")
1723 (defconst breakpoint-disabled-pbm-data
1735 0 0 0 1 0 1 0 1 0 0"
1736 "PBM data used for disabled breakpoint icon.")
1738 (defvar breakpoint-enabled-icon nil
1739 "Icon for enabled breakpoint in display margin.")
1741 (defvar breakpoint-disabled-icon nil
1742 "Icon for disabled breakpoint in display margin.")
1744 (and (display-images-p)
1745 ;; Bitmap for breakpoint in fringe
1746 (define-fringe-bitmap 'breakpoint
1747 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1748 ;; Bitmap for gud-overlay-arrow in fringe
1749 (define-fringe-bitmap 'hollow-right-triangle
1750 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1752 (defface breakpoint-enabled
1756 "Face for enabled breakpoint icon in fringe."
1759 (defface breakpoint-disabled
1760 ;; We use different values of grey for different background types,
1761 ;; so that on low-color displays it will end up as something visible
1762 ;; if it has to be approximated.
1763 '((t :foreground "grey70"))
1764 "Face for disabled breakpoint icon in fringe."
1767 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1768 (defun gdb-info-breakpoints-custom ()
1769 (let ((flag) (bptno))
1770 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1771 (dolist (buffer (buffer-list))
1772 (with-current-buffer buffer
1773 (if (and (memq gud-minor-mode '(gdba gdbmi))
1774 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1775 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1776 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1778 (let ((buffer-read-only nil))
1779 (goto-char (point-min))
1780 (while (< (point) (- (point-max) 1))
1782 (if (looking-at gdb-breakpoint-regexp)
1784 (setq bptno (match-string 1))
1785 (setq flag (char-after (match-beginning 2)))
1786 (add-text-properties
1787 (match-beginning 2) (match-end 2)
1789 '(face font-lock-warning-face)
1790 '(face font-lock-type-face)))
1792 (el (line-end-position)))
1793 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1795 (add-text-properties
1796 (match-beginning 1) (match-end 1)
1797 '(face font-lock-function-name-face))
1798 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1799 (let ((line (match-string 2))
1800 (file (match-string 1)))
1801 (add-text-properties bl el
1802 '(mouse-face highlight
1803 help-echo "mouse-2, RET: visit breakpoint"))
1804 (unless (file-exists-p file)
1805 (setq file (cdr (assoc bptno gdb-location-alist))))
1807 (not (string-equal file "File not found")))
1808 (with-current-buffer
1809 (find-file-noselect file 'nowarn)
1810 (set (make-local-variable 'gud-minor-mode)
1812 (set (make-local-variable 'tool-bar-map)
1814 ;; Only want one breakpoint icon at each
1817 (goto-line (string-to-number line))
1818 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1821 (concat gdb-server-prefix "list "
1822 (match-string-no-properties 1) ":1\n")
1825 (list (concat gdb-server-prefix "info source\n")
1826 `(lambda () (gdb-get-location
1827 ,bptno ,line ,flag)))))))
1828 (if (re-search-forward
1829 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1831 (add-text-properties
1832 (match-beginning 1) (match-end 1)
1833 '(face font-lock-function-name-face))
1835 (re-search-backward "\\s-\\(\\S-*\\)"
1837 (add-text-properties
1838 (match-beginning 1) (match-end 1)
1839 '(face font-lock-variable-name-face)))))))
1841 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1843 (defun gdb-mouse-set-clear-breakpoint (event)
1844 "Set/clear breakpoint in left fringe/margin with mouse click."
1846 (mouse-minibuffer-check event)
1847 (let ((posn (event-end event)))
1848 (if (numberp (posn-point posn))
1849 (with-selected-window (posn-window posn)
1851 (goto-char (posn-point posn))
1852 (if (or (posn-object posn)
1853 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1856 (gud-break nil)))))))
1858 (defun gdb-mouse-toggle-breakpoint-margin (event)
1859 "Enable/disable breakpoint in left margin with mouse click."
1861 (mouse-minibuffer-check event)
1862 (let ((posn (event-end event)))
1863 (if (numberp (posn-point posn))
1864 (with-selected-window (posn-window posn)
1866 (goto-char (posn-point posn))
1867 (if (posn-object posn)
1870 (let ((bptno (get-text-property
1871 0 'gdb-bptno (car (posn-string posn)))))
1872 (concat gdb-server-prefix
1873 (if (get-text-property
1874 0 'gdb-enabled (car (posn-string posn)))
1880 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1881 "Enable/disable breakpoint in left fringe with mouse click."
1883 (mouse-minibuffer-check event)
1884 (let* ((posn (event-end event))
1885 (pos (posn-point posn))
1888 (with-selected-window (posn-window posn)
1890 (set-buffer (window-buffer (selected-window)))
1892 (dolist (overlay (overlays-in pos pos))
1893 (when (overlay-get overlay 'put-break)
1894 (setq obj (overlay-get overlay 'before-string))))
1898 (concat gdb-server-prefix
1899 (if (get-text-property 0 'gdb-enabled obj)
1902 (get-text-property 0 'gdb-bptno obj) "\n")
1905 (defun gdb-breakpoints-buffer-name ()
1906 (with-current-buffer gud-comint-buffer
1907 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1909 (defun gdb-display-breakpoints-buffer ()
1910 "Display status of user-settable breakpoints."
1913 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
1915 (defun gdb-frame-breakpoints-buffer ()
1916 "Display status of user-settable breakpoints in a new frame."
1918 (let ((special-display-regexps (append special-display-regexps '(".*")))
1919 (special-display-frame-alist gdb-frame-parameters))
1920 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1922 (defvar gdb-breakpoints-mode-map
1923 (let ((map (make-sparse-keymap))
1924 (menu (make-sparse-keymap "Breakpoints")))
1925 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1926 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1927 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1928 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1929 (suppress-keymap map)
1930 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1931 (define-key map " " 'gdb-toggle-breakpoint)
1932 (define-key map "D" 'gdb-delete-breakpoint)
1933 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1934 (define-key map "q" 'gdb-delete-frame-or-window)
1935 (define-key map "\r" 'gdb-goto-breakpoint)
1936 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1937 (define-key map [follow-link] 'mouse-face)
1940 (defun gdb-delete-frame-or-window ()
1941 "Delete frame if there is only one window. Otherwise delete the window."
1943 (if (one-window-p) (delete-frame)
1946 (defun gdb-breakpoints-mode ()
1947 "Major mode for gdb breakpoints.
1949 \\{gdb-breakpoints-mode-map}"
1950 (kill-all-local-variables)
1951 (setq major-mode 'gdb-breakpoints-mode)
1952 (setq mode-name "Breakpoints")
1953 (use-local-map gdb-breakpoints-mode-map)
1954 (setq buffer-read-only t)
1955 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1956 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1957 'gdb-invalidate-breakpoints
1958 'gdbmi-invalidate-breakpoints))
1960 (defconst gdb-breakpoint-regexp
1961 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1963 (defun gdb-toggle-breakpoint ()
1964 "Enable/disable breakpoint at current line."
1967 (beginning-of-line 1)
1968 (if (looking-at gdb-breakpoint-regexp)
1971 (concat gdb-server-prefix
1972 (if (eq ?y (char-after (match-beginning 2)))
1975 (match-string 1) "\n") 'ignore))
1976 (error "Not recognized as break/watchpoint line"))))
1978 (defun gdb-delete-breakpoint ()
1979 "Delete the breakpoint at current line."
1981 (beginning-of-line 1)
1982 (if (looking-at gdb-breakpoint-regexp)
1985 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1986 (error "Not recognized as break/watchpoint line")))
1988 (defun gdb-goto-breakpoint (&optional event)
1989 "Display the breakpoint location specified at current line."
1990 (interactive (list last-input-event))
1991 (if event (posn-set-point (event-end event)))
1993 (beginning-of-line 1)
1994 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1995 (let ((bptno (match-string 1))
1996 (file (match-string 2))
1997 (line (match-string 3)))
1998 (save-selected-window
1999 (let* ((buffer (find-file-noselect
2000 (if (file-exists-p file) file
2001 (cdr (assoc bptno gdb-location-alist)))))
2002 (window (or (gdb-display-source-buffer buffer)
2003 (display-buffer buffer))))
2004 (setq gdb-source-window window)
2005 (with-current-buffer buffer
2006 (goto-line (string-to-number line))
2007 (set-window-point window (point))))))
2008 (error "No location specified."))))
2011 ;; Frames buffer. This displays a perpetually correct bactracktrace
2012 ;; (from the command `where').
2014 ;; Alas, if your stack is deep, it is costly.
2016 (gdb-set-buffer-rules 'gdb-stack-buffer
2017 'gdb-stack-buffer-name
2020 (def-gdb-auto-updated-buffer gdb-stack-buffer
2021 gdb-invalidate-frames
2022 "server info stack\n"
2023 gdb-info-stack-handler
2024 gdb-info-stack-custom)
2026 (defun gdb-info-stack-custom ()
2027 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2029 (unless (eq gdb-look-up-stack 'delete)
2030 (let ((buffer-read-only nil)
2032 (goto-char (point-min))
2033 (while (< (point) (point-max))
2034 (setq bl (line-beginning-position)
2035 el (line-end-position))
2036 (when (looking-at "#")
2037 (add-text-properties bl el
2038 '(mouse-face highlight
2039 help-echo "mouse-2, RET: Select frame")))
2041 (when (looking-at "^#\\([0-9]+\\)")
2042 (when (string-equal (match-string 1) gdb-frame-number)
2043 (if (> (car (window-fringes)) 0)
2045 (or gdb-stack-position
2046 (setq gdb-stack-position (make-marker)))
2047 (set-marker gdb-stack-position (point)))
2048 (set-marker gdb-stack-position nil)
2049 (put-text-property bl (+ bl 4)
2050 'face '(:inverse-video t))))
2051 (when (re-search-forward
2053 (if (string-equal (match-string 1) "0") "" " in ")
2054 "\\([^ ]+\\) (") el t)
2055 (put-text-property (match-beginning 1) (match-end 1)
2056 'face font-lock-function-name-face)
2057 (setq bl (match-end 0))
2058 (while (re-search-forward "<\\([^>]+\\)>" el t)
2059 (put-text-property (match-beginning 1) (match-end 1)
2060 'face font-lock-function-name-face))
2062 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2063 (put-text-property (match-beginning 1) (match-end 1)
2064 'face font-lock-variable-name-face))))
2066 (when gdb-look-up-stack
2067 (goto-char (point-min))
2068 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2069 (let ((start (line-beginning-position))
2070 (file (match-string 1))
2071 (line (match-string 2)))
2072 (re-search-backward "^#*\\([0-9]+\\)" start t)
2074 (list (concat gdb-server-prefix "frame "
2075 (match-string 1) "\n") 'gdb-set-hollow))
2077 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2078 (if (eq gdb-look-up-stack 'delete)
2079 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2080 (setq gdb-look-up-stack nil))
2082 (defun gdb-set-hollow ()
2083 (if gud-last-last-frame
2084 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2085 (setq fringe-indicator-alist
2086 '((overlay-arrow . hollow-right-triangle))))))
2088 (defun gdb-stack-buffer-name ()
2089 (with-current-buffer gud-comint-buffer
2090 (concat "*stack frames of " (gdb-get-target-string) "*")))
2092 (defun gdb-display-stack-buffer ()
2093 "Display backtrace of current stack."
2096 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2098 (defun gdb-frame-stack-buffer ()
2099 "Display backtrace of current stack in a new frame."
2101 (let ((special-display-regexps (append special-display-regexps '(".*")))
2102 (special-display-frame-alist gdb-frame-parameters))
2103 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2105 (defvar gdb-frames-mode-map
2106 (let ((map (make-sparse-keymap)))
2107 (suppress-keymap map)
2108 (define-key map "q" 'kill-this-buffer)
2109 (define-key map "\r" 'gdb-frames-select)
2110 (define-key map [mouse-2] 'gdb-frames-select)
2111 (define-key map [follow-link] 'mouse-face)
2114 (defun gdb-frames-mode ()
2115 "Major mode for gdb call stack.
2117 \\{gdb-frames-mode-map}"
2118 (kill-all-local-variables)
2119 (setq major-mode 'gdb-frames-mode)
2120 (setq mode-name "Frames")
2121 (setq gdb-stack-position nil)
2122 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2123 (setq buffer-read-only t)
2124 (use-local-map gdb-frames-mode-map)
2125 (run-mode-hooks 'gdb-frames-mode-hook)
2126 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2127 'gdb-invalidate-frames
2128 'gdbmi-invalidate-frames))
2130 (defun gdb-get-frame-number ()
2133 (let* ((start (line-beginning-position))
2134 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2135 (n (or (and pos (match-string-no-properties 1)) "0")))
2138 (defun gdb-frames-select (&optional event)
2139 "Select the frame and display the relevant source."
2140 (interactive (list last-input-event))
2141 (if event (posn-set-point (event-end event)))
2143 (list (concat gdb-server-prefix "frame "
2144 (gdb-get-frame-number) "\n") 'ignore)))
2147 ;; Threads buffer. This displays a selectable thread list.
2149 (gdb-set-buffer-rules 'gdb-threads-buffer
2150 'gdb-threads-buffer-name
2153 (def-gdb-auto-updated-buffer gdb-threads-buffer
2154 gdb-invalidate-threads
2155 (concat gdb-server-prefix "info threads\n")
2156 gdb-info-threads-handler
2157 gdb-info-threads-custom)
2159 (defun gdb-info-threads-custom ()
2160 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2161 (let ((buffer-read-only nil))
2163 (goto-char (point-min))
2164 (while (< (point) (point-max))
2165 (unless (looking-at "No ")
2166 (add-text-properties (line-beginning-position) (line-end-position)
2167 '(mouse-face highlight
2168 help-echo "mouse-2, RET: select thread")))
2169 (forward-line 1))))))
2171 (defun gdb-threads-buffer-name ()
2172 (with-current-buffer gud-comint-buffer
2173 (concat "*threads of " (gdb-get-target-string) "*")))
2175 (defun gdb-display-threads-buffer ()
2176 "Display IDs of currently known threads."
2179 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2181 (defun gdb-frame-threads-buffer ()
2182 "Display IDs of currently known threads in a new frame."
2184 (let ((special-display-regexps (append special-display-regexps '(".*")))
2185 (special-display-frame-alist gdb-frame-parameters))
2186 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2188 (defvar gdb-threads-mode-map
2189 (let ((map (make-sparse-keymap)))
2190 (suppress-keymap map)
2191 (define-key map "q" 'kill-this-buffer)
2192 (define-key map "\r" 'gdb-threads-select)
2193 (define-key map [mouse-2] 'gdb-threads-select)
2194 (define-key map [follow-link] 'mouse-face)
2197 (defvar gdb-threads-font-lock-keywords
2198 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2199 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2200 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2201 "Font lock keywords used in `gdb-threads-mode'.")
2203 (defun gdb-threads-mode ()
2204 "Major mode for gdb threads.
2206 \\{gdb-threads-mode-map}"
2207 (kill-all-local-variables)
2208 (setq major-mode 'gdb-threads-mode)
2209 (setq mode-name "Threads")
2210 (setq buffer-read-only t)
2211 (use-local-map gdb-threads-mode-map)
2212 (set (make-local-variable 'font-lock-defaults)
2213 '(gdb-threads-font-lock-keywords))
2214 (run-mode-hooks 'gdb-threads-mode-hook)
2215 'gdb-invalidate-threads)
2217 (defun gdb-get-thread-number ()
2219 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2220 (match-string-no-properties 1)))
2222 (defun gdb-threads-select (&optional event)
2223 "Select the thread and display the relevant source."
2224 (interactive (list last-input-event))
2225 (if event (posn-set-point (event-end event)))
2227 (list (concat gdb-server-prefix "thread "
2228 (gdb-get-thread-number) "\n") 'ignore))
2229 (gud-display-frame))
2232 ;; Registers buffer.
2234 (defcustom gdb-all-registers nil
2235 "Non-nil means include floating-point registers."
2240 (gdb-set-buffer-rules 'gdb-registers-buffer
2241 'gdb-registers-buffer-name
2242 'gdb-registers-mode)
2244 (def-gdb-auto-updated-buffer gdb-registers-buffer
2245 gdb-invalidate-registers
2247 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2248 gdb-info-registers-handler
2249 gdb-info-registers-custom)
2251 (defun gdb-info-registers-custom ()
2252 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2254 (let ((buffer-read-only nil)
2256 (goto-char (point-min))
2257 (while (< (point) (point-max))
2258 (setq start (line-beginning-position))
2259 (setq end (line-end-position))
2260 (when (looking-at "^[^ ]+")
2261 (unless (string-equal (match-string 0) "The")
2262 (put-text-property start (match-end 0)
2263 'face font-lock-variable-name-face)
2264 (add-text-properties start end
2265 '(help-echo "mouse-2: edit value"
2266 mouse-face highlight))))
2267 (forward-line 1))))))
2269 (defun gdb-edit-register-value (&optional event)
2270 (interactive (list last-input-event))
2272 (if event (posn-set-point (event-end event)))
2274 (let* ((register (current-word))
2275 (value (read-string (format "New value (%s): " register))))
2277 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2280 (defvar gdb-registers-mode-map
2281 (let ((map (make-sparse-keymap)))
2282 (suppress-keymap map)
2283 (define-key map "\r" 'gdb-edit-register-value)
2284 (define-key map [mouse-2] 'gdb-edit-register-value)
2285 (define-key map " " 'gdb-all-registers)
2286 (define-key map "q" 'kill-this-buffer)
2289 (defun gdb-registers-mode ()
2290 "Major mode for gdb registers.
2292 \\{gdb-registers-mode-map}"
2293 (kill-all-local-variables)
2294 (setq major-mode 'gdb-registers-mode)
2295 (setq mode-name "Registers")
2296 (setq buffer-read-only t)
2297 (use-local-map gdb-registers-mode-map)
2298 (run-mode-hooks 'gdb-registers-mode-hook)
2299 (if (string-equal gdb-version "pre-6.4")
2301 (if gdb-all-registers (setq mode-name "Registers:All"))
2302 'gdb-invalidate-registers)
2303 'gdb-invalidate-registers-1))
2305 (defun gdb-registers-buffer-name ()
2306 (with-current-buffer gud-comint-buffer
2307 (concat "*registers of " (gdb-get-target-string) "*")))
2309 (defun gdb-display-registers-buffer ()
2310 "Display integer register contents."
2313 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2315 (defun gdb-frame-registers-buffer ()
2316 "Display integer register contents in a new frame."
2318 (let ((special-display-regexps (append special-display-regexps '(".*")))
2319 (special-display-frame-alist gdb-frame-parameters))
2320 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2322 (defun gdb-all-registers ()
2323 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2325 (when (string-equal gdb-version "pre-6.4")
2326 (if gdb-all-registers
2328 (setq gdb-all-registers nil)
2329 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2330 (setq mode-name "Registers")))
2331 (setq gdb-all-registers t)
2332 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2333 (setq mode-name "Registers:All")))
2334 (message (format "Display of floating-point registers %sabled"
2335 (if gdb-all-registers "en" "dis")))
2336 (gdb-invalidate-registers)))
2341 (defcustom gdb-memory-repeat-count 32
2342 "Number of data items in memory window."
2347 (defcustom gdb-memory-format "x"
2348 "Display format of data items in memory window."
2349 :type '(choice (const :tag "Hexadecimal" "x")
2350 (const :tag "Signed decimal" "d")
2351 (const :tag "Unsigned decimal" "u")
2352 (const :tag "Octal" "o")
2353 (const :tag "Binary" "t"))
2357 (defcustom gdb-memory-unit "w"
2358 "Unit size of data items in memory window."
2359 :type '(choice (const :tag "Byte" "b")
2360 (const :tag "Halfword" "h")
2361 (const :tag "Word" "w")
2362 (const :tag "Giant word" "g"))
2366 (gdb-set-buffer-rules 'gdb-memory-buffer
2367 'gdb-memory-buffer-name
2370 (def-gdb-auto-updated-buffer gdb-memory-buffer
2371 gdb-invalidate-memory
2372 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2373 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2374 gdb-read-memory-handler
2375 gdb-read-memory-custom)
2377 (defun gdb-read-memory-custom ()
2379 (goto-char (point-min))
2380 (if (looking-at "0x[[:xdigit:]]+")
2381 (setq gdb-memory-address (match-string 0)))))
2383 (defvar gdb-memory-mode-map
2384 (let ((map (make-sparse-keymap)))
2385 (suppress-keymap map)
2386 (define-key map "q" 'kill-this-buffer)
2389 (defun gdb-memory-set-address (event)
2390 "Set the start memory address."
2392 (save-selected-window
2393 (select-window (posn-window (event-start event)))
2394 (let ((arg (read-from-minibuffer "Memory address: ")))
2395 (setq gdb-memory-address arg))
2396 (gdb-invalidate-memory)))
2398 (defun gdb-memory-set-repeat-count (event)
2399 "Set the number of data items in memory window."
2401 (save-selected-window
2402 (select-window (posn-window (event-start event)))
2403 (let* ((arg (read-from-minibuffer "Repeat count: "))
2404 (count (string-to-number arg)))
2406 (error "Positive numbers only")
2407 (customize-set-variable 'gdb-memory-repeat-count count)
2408 (gdb-invalidate-memory)))))
2410 (defun gdb-memory-format-binary ()
2411 "Set the display format to binary."
2413 (customize-set-variable 'gdb-memory-format "t")
2414 (gdb-invalidate-memory))
2416 (defun gdb-memory-format-octal ()
2417 "Set the display format to octal."
2419 (customize-set-variable 'gdb-memory-format "o")
2420 (gdb-invalidate-memory))
2422 (defun gdb-memory-format-unsigned ()
2423 "Set the display format to unsigned decimal."
2425 (customize-set-variable 'gdb-memory-format "u")
2426 (gdb-invalidate-memory))
2428 (defun gdb-memory-format-signed ()
2429 "Set the display format to decimal."
2431 (customize-set-variable 'gdb-memory-format "d")
2432 (gdb-invalidate-memory))
2434 (defun gdb-memory-format-hexadecimal ()
2435 "Set the display format to hexadecimal."
2437 (customize-set-variable 'gdb-memory-format "x")
2438 (gdb-invalidate-memory))
2440 (defvar gdb-memory-format-map
2441 (let ((map (make-sparse-keymap)))
2442 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2444 "Keymap to select format in the header line.")
2446 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2447 "Menu of display formats in the header line.")
2449 (define-key gdb-memory-format-menu [binary]
2450 '(menu-item "Binary" gdb-memory-format-binary
2451 :button (:radio . (equal gdb-memory-format "t"))))
2452 (define-key gdb-memory-format-menu [octal]
2453 '(menu-item "Octal" gdb-memory-format-octal
2454 :button (:radio . (equal gdb-memory-format "o"))))
2455 (define-key gdb-memory-format-menu [unsigned]
2456 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2457 :button (:radio . (equal gdb-memory-format "u"))))
2458 (define-key gdb-memory-format-menu [signed]
2459 '(menu-item "Signed Decimal" gdb-memory-format-signed
2460 :button (:radio . (equal gdb-memory-format "d"))))
2461 (define-key gdb-memory-format-menu [hexadecimal]
2462 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2463 :button (:radio . (equal gdb-memory-format "x"))))
2465 (defun gdb-memory-format-menu (event)
2467 (x-popup-menu event gdb-memory-format-menu))
2469 (defun gdb-memory-format-menu-1 (event)
2471 (save-selected-window
2472 (select-window (posn-window (event-start event)))
2473 (let* ((selection (gdb-memory-format-menu event))
2474 (binding (and selection (lookup-key gdb-memory-format-menu
2475 (vector (car selection))))))
2476 (if binding (call-interactively binding)))))
2478 (defun gdb-memory-unit-giant ()
2479 "Set the unit size to giant words (eight bytes)."
2481 (customize-set-variable 'gdb-memory-unit "g")
2482 (gdb-invalidate-memory))
2484 (defun gdb-memory-unit-word ()
2485 "Set the unit size to words (four bytes)."
2487 (customize-set-variable 'gdb-memory-unit "w")
2488 (gdb-invalidate-memory))
2490 (defun gdb-memory-unit-halfword ()
2491 "Set the unit size to halfwords (two bytes)."
2493 (customize-set-variable 'gdb-memory-unit "h")
2494 (gdb-invalidate-memory))
2496 (defun gdb-memory-unit-byte ()
2497 "Set the unit size to bytes."
2499 (customize-set-variable 'gdb-memory-unit "b")
2500 (gdb-invalidate-memory))
2502 (defvar gdb-memory-unit-map
2503 (let ((map (make-sparse-keymap)))
2504 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2506 "Keymap to select units in the header line.")
2508 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2509 "Menu of units in the header line.")
2511 (define-key gdb-memory-unit-menu [giantwords]
2512 '(menu-item "Giant words" gdb-memory-unit-giant
2513 :button (:radio . (equal gdb-memory-unit "g"))))
2514 (define-key gdb-memory-unit-menu [words]
2515 '(menu-item "Words" gdb-memory-unit-word
2516 :button (:radio . (equal gdb-memory-unit "w"))))
2517 (define-key gdb-memory-unit-menu [halfwords]
2518 '(menu-item "Halfwords" gdb-memory-unit-halfword
2519 :button (:radio . (equal gdb-memory-unit "h"))))
2520 (define-key gdb-memory-unit-menu [bytes]
2521 '(menu-item "Bytes" gdb-memory-unit-byte
2522 :button (:radio . (equal gdb-memory-unit "b"))))
2524 (defun gdb-memory-unit-menu (event)
2526 (x-popup-menu event gdb-memory-unit-menu))
2528 (defun gdb-memory-unit-menu-1 (event)
2530 (save-selected-window
2531 (select-window (posn-window (event-start event)))
2532 (let* ((selection (gdb-memory-unit-menu event))
2533 (binding (and selection (lookup-key gdb-memory-unit-menu
2534 (vector (car selection))))))
2535 (if binding (call-interactively binding)))))
2537 ;;from make-mode-line-mouse-map
2538 (defun gdb-make-header-line-mouse-map (mouse function) "\
2539 Return a keymap with single entry for mouse key MOUSE on the header line.
2540 MOUSE is defined to run function FUNCTION with no args in the buffer
2541 corresponding to the mode line clicked."
2542 (let ((map (make-sparse-keymap)))
2543 (define-key map (vector 'header-line mouse) function)
2544 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2547 (defvar gdb-memory-font-lock-keywords
2548 '(;; <__function.name+n>
2549 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2551 "Font lock keywords used in `gdb-memory-mode'.")
2553 (defun gdb-memory-mode ()
2554 "Major mode for examining memory.
2556 \\{gdb-memory-mode-map}"
2557 (kill-all-local-variables)
2558 (setq major-mode 'gdb-memory-mode)
2559 (setq mode-name "Memory")
2560 (setq buffer-read-only t)
2561 (use-local-map gdb-memory-mode-map)
2562 (setq header-line-format
2568 'face font-lock-warning-face
2569 'help-echo "mouse-1: Decrement address"
2570 'mouse-face 'mode-line-highlight
2572 (gdb-make-header-line-mouse-map
2574 (lambda () (interactive)
2575 (let ((gdb-memory-address
2576 ;; Let GDB do the arithmetic.
2578 gdb-memory-address " - "
2580 (* gdb-memory-repeat-count
2581 (cond ((string= gdb-memory-unit "b") 1)
2582 ((string= gdb-memory-unit "h") 2)
2583 ((string= gdb-memory-unit "w") 4)
2584 ((string= gdb-memory-unit "g") 8)))))))
2585 (gdb-invalidate-memory)))))
2588 'face font-lock-warning-face
2589 'help-echo "mouse-1: Increment address"
2590 'mouse-face 'mode-line-highlight
2591 'local-map (gdb-make-header-line-mouse-map
2593 (lambda () (interactive)
2594 (let ((gdb-memory-address nil))
2595 (gdb-invalidate-memory)))))
2597 (propertize gdb-memory-address
2598 'face font-lock-warning-face
2599 'help-echo "mouse-1: Set memory address"
2600 'mouse-face 'mode-line-highlight
2601 'local-map (gdb-make-header-line-mouse-map
2603 #'gdb-memory-set-address))
2605 (propertize (number-to-string gdb-memory-repeat-count)
2606 'face font-lock-warning-face
2607 'help-echo "mouse-1: Set repeat count"
2608 'mouse-face 'mode-line-highlight
2609 'local-map (gdb-make-header-line-mouse-map
2611 #'gdb-memory-set-repeat-count))
2613 (propertize gdb-memory-format
2614 'face font-lock-warning-face
2615 'help-echo "mouse-3: Select display format"
2616 'mouse-face 'mode-line-highlight
2617 'local-map gdb-memory-format-map)
2619 (propertize gdb-memory-unit
2620 'face font-lock-warning-face
2621 'help-echo "mouse-3: Select unit size"
2622 'mouse-face 'mode-line-highlight
2623 'local-map gdb-memory-unit-map))))
2624 (set (make-local-variable 'font-lock-defaults)
2625 '(gdb-memory-font-lock-keywords))
2626 (run-mode-hooks 'gdb-memory-mode-hook)
2627 'gdb-invalidate-memory)
2629 (defun gdb-memory-buffer-name ()
2630 (with-current-buffer gud-comint-buffer
2631 (concat "*memory of " (gdb-get-target-string) "*")))
2633 (defun gdb-display-memory-buffer ()
2634 "Display memory contents."
2637 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2639 (defun gdb-frame-memory-buffer ()
2640 "Display memory contents in a new frame."
2642 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2643 (special-display-frame-alist
2644 (cons '(left-fringe . 0)
2645 (cons '(right-fringe . 0)
2646 (cons '(width . 83) gdb-frame-parameters)))))
2647 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2652 (gdb-set-buffer-rules 'gdb-locals-buffer
2653 'gdb-locals-buffer-name
2656 (def-gdb-auto-update-trigger gdb-invalidate-locals
2657 (gdb-get-buffer 'gdb-locals-buffer)
2658 "server info locals\n"
2659 gdb-info-locals-handler)
2661 (defvar gdb-locals-watch-map
2662 (let ((map (make-sparse-keymap)))
2663 (suppress-keymap map)
2664 (define-key map "\r" (lambda () (interactive)
2667 (define-key map [mouse-2] (lambda (event) (interactive "e")
2668 (mouse-set-point event)
2672 "Keymap to create watch expression of a complex data type local variable.")
2674 (defconst gdb-struct-string
2675 (concat (propertize "[struct/union]"
2676 'mouse-face 'highlight
2677 'help-echo "mouse-2: create watch expression"
2678 'local-map gdb-locals-watch-map) "\n"))
2680 (defconst gdb-array-string
2681 (concat " " (propertize "[array]"
2682 'mouse-face 'highlight
2683 'help-echo "mouse-2: create watch expression"
2684 'local-map gdb-locals-watch-map) "\n"))
2686 ;; Abbreviate for arrays and structures.
2687 ;; These can be expanded using gud-display.
2688 (defun gdb-info-locals-handler ()
2689 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2690 gdb-pending-triggers))
2691 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2692 (with-current-buffer buf
2693 (goto-char (point-min))
2694 (while (re-search-forward "^[ }].*\n" nil t)
2695 (replace-match "" nil nil))
2696 (goto-char (point-min))
2697 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2698 (replace-match gdb-struct-string nil nil))
2699 (goto-char (point-min))
2700 (while (re-search-forward "\\s-*{.*\n" nil t)
2701 (replace-match gdb-array-string nil nil))))
2702 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2704 (with-current-buffer buf
2705 (let* ((window (get-buffer-window buf 0))
2706 (start (window-start window))
2707 (p (window-point window))
2708 (buffer-read-only nil))
2710 (insert-buffer-substring (gdb-get-buffer-create
2711 'gdb-partial-output-buffer))
2712 (set-window-start window start)
2713 (set-window-point window p))
2715 (run-hooks 'gdb-info-locals-hook))
2717 (defvar gdb-locals-mode-map
2718 (let ((map (make-sparse-keymap)))
2719 (suppress-keymap map)
2720 (define-key map "q" 'kill-this-buffer)
2723 (defun gdb-locals-mode ()
2724 "Major mode for gdb locals.
2726 \\{gdb-locals-mode-map}"
2727 (kill-all-local-variables)
2728 (setq major-mode 'gdb-locals-mode)
2729 (setq mode-name (concat "Locals:" gdb-selected-frame))
2730 (setq buffer-read-only t)
2731 (use-local-map gdb-locals-mode-map)
2732 (set (make-local-variable 'font-lock-defaults)
2733 '(gdb-locals-font-lock-keywords))
2734 (run-mode-hooks 'gdb-locals-mode-hook)
2735 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2736 (string-equal gdb-version "pre-6.4"))
2737 'gdb-invalidate-locals
2738 'gdb-invalidate-locals-1))
2740 (defun gdb-locals-buffer-name ()
2741 (with-current-buffer gud-comint-buffer
2742 (concat "*locals of " (gdb-get-target-string) "*")))
2744 (defun gdb-display-locals-buffer ()
2745 "Display local variables of current stack and their values."
2748 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2750 (defun gdb-frame-locals-buffer ()
2751 "Display local variables of current stack and their values in a new frame."
2753 (let ((special-display-regexps (append special-display-regexps '(".*")))
2754 (special-display-frame-alist gdb-frame-parameters))
2755 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2758 ;;;; Window management
2759 (defun gdb-display-buffer (buf dedicated &optional size)
2760 (let ((answer (get-buffer-window buf 0))
2763 (display-buffer buf nil 0) ;Raise the frame if necessary.
2764 ;; The buffer is not yet displayed.
2765 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2766 (let ((window (get-lru-window)))
2768 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2769 ,gdb-source-window))))
2771 (set-window-buffer window buf)
2772 (setq answer window))
2773 (setq must-split t)))
2775 (let* ((largest (get-largest-window))
2776 (cur-size (window-height largest))
2777 (new-size (and size (< size cur-size) (- cur-size size))))
2778 (setq answer (split-window largest new-size))
2779 (set-window-buffer answer buf)
2780 (set-window-dedicated-p answer dedicated)))
2784 ;;; Shared keymap initialization:
2786 (let ((menu (make-sparse-keymap "GDB-Windows")))
2787 (define-key gud-menu-map [displays]
2788 `(menu-item "GDB-Windows" ,menu
2789 :visible (memq gud-minor-mode '(gdbmi gdba))))
2790 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2791 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2792 (define-key menu [inferior]
2793 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2794 :enable gdb-use-separate-io-buffer))
2795 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2796 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2797 (define-key menu [disassembly]
2798 '("Disassembly" . gdb-display-assembler-buffer))
2799 (define-key menu [breakpoints]
2800 '("Breakpoints" . gdb-display-breakpoints-buffer))
2801 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2802 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2804 (let ((menu (make-sparse-keymap "GDB-Frames")))
2805 (define-key gud-menu-map [frames]
2806 `(menu-item "GDB-Frames" ,menu
2807 :visible (memq gud-minor-mode '(gdbmi gdba))))
2808 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2809 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2810 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2811 (define-key menu [inferior]
2812 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2813 :enable gdb-use-separate-io-buffer))
2814 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2815 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2816 (define-key menu [breakpoints]
2817 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2818 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2819 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2821 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2822 (define-key gud-menu-map [ui]
2823 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2824 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2825 (define-key menu [gdb-find-source-frame]
2826 '(menu-item "Look For Source Frame" gdb-find-source-frame
2827 :visible (eq gud-minor-mode 'gdba)
2828 :help "Toggle look for source frame."
2829 :button (:toggle . gdb-find-source-frame)))
2830 (define-key menu [gdb-use-separate-io]
2831 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2832 :visible (eq gud-minor-mode 'gdba)
2833 :help "Toggle separate IO for debugged program."
2834 :button (:toggle . gdb-use-separate-io-buffer)))
2835 (define-key menu [gdb-many-windows]
2836 '(menu-item "Display Other Windows" gdb-many-windows
2837 :help "Toggle display of locals, stack and breakpoint information"
2838 :button (:toggle . gdb-many-windows)))
2839 (define-key menu [gdb-restore-windows]
2840 '(menu-item "Restore Window Layout" gdb-restore-windows
2841 :help "Restore standard layout for debug session.")))
2843 (defun gdb-frame-gdb-buffer ()
2844 "Display GUD buffer in a new frame."
2846 (let ((special-display-regexps (append special-display-regexps '(".*")))
2847 (special-display-frame-alist
2848 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2849 gdb-frame-parameters)))
2850 (same-window-regexps nil))
2851 (display-buffer gud-comint-buffer)))
2853 (defun gdb-display-gdb-buffer ()
2854 "Display GUD buffer."
2856 (let ((same-window-regexps nil))
2857 (pop-to-buffer gud-comint-buffer)))
2859 (defun gdb-set-window-buffer (name)
2860 (set-window-buffer (selected-window) (get-buffer name))
2861 (set-window-dedicated-p (selected-window) t))
2863 (defun gdb-setup-windows ()
2864 "Layout the window pattern for `gdb-many-windows'."
2865 (gdb-display-locals-buffer)
2866 (gdb-display-stack-buffer)
2867 (delete-other-windows)
2868 (gdb-display-breakpoints-buffer)
2869 (delete-other-windows)
2871 (pop-to-buffer gud-comint-buffer)
2872 (split-window nil ( / ( * (window-height) 3) 4))
2873 (split-window nil ( / (window-height) 3))
2874 (split-window-horizontally)
2876 (gdb-set-window-buffer (gdb-locals-buffer-name))
2879 (if gud-last-last-frame
2880 (gud-find-file (car gud-last-last-frame))
2882 (gud-find-file gdb-main-file)
2883 ;; Put buffer list in window if we
2884 ;; can't find a source file.
2885 (list-buffers-noselect))))
2886 (setq gdb-source-window (selected-window))
2887 (when gdb-use-separate-io-buffer
2888 (split-window-horizontally)
2890 (gdb-set-window-buffer
2891 (gdb-get-buffer-create 'gdb-inferior-io)))
2893 (gdb-set-window-buffer (gdb-stack-buffer-name))
2894 (split-window-horizontally)
2896 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2899 (defun gdb-restore-windows ()
2900 "Restore the basic arrangement of windows used by gdba.
2901 This arrangement depends on the value of `gdb-many-windows'."
2903 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2904 (delete-other-windows)
2905 (if gdb-many-windows
2907 (when (or gud-last-last-frame gdb-show-main)
2911 (if gud-last-last-frame
2912 (gud-find-file (car gud-last-last-frame))
2913 (gud-find-file gdb-main-file)))
2914 (setq gdb-source-window (selected-window))
2918 "Exit a debugging session cleanly.
2919 Kills the gdb buffers, and resets variables and the source buffers."
2920 (dolist (buffer (buffer-list))
2921 (unless (eq buffer gud-comint-buffer)
2922 (with-current-buffer buffer
2923 (if (memq gud-minor-mode '(gdbmi gdba))
2924 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2926 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2927 (setq gud-minor-mode nil)
2928 (kill-local-variable 'tool-bar-map)
2929 (kill-local-variable 'gdb-define-alist))))))
2930 (setq gdb-overlay-arrow-position nil)
2931 (setq overlay-arrow-variable-list
2932 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2933 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2934 (setq gdb-stack-position nil)
2935 (setq overlay-arrow-variable-list
2936 (delq 'gdb-stack-position overlay-arrow-variable-list))
2937 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2938 (setq gud-running nil)
2939 (setq gdb-active-process nil)
2940 (setq gdb-var-list nil)
2941 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2943 (defun gdb-source-info ()
2944 "Find the source file where the program starts and displays it with related
2946 (goto-char (point-min))
2947 (if (and (search-forward "Located in " nil t)
2948 (looking-at "\\S-+"))
2949 (setq gdb-main-file (match-string 0)))
2950 (goto-char (point-min))
2951 (if (search-forward "Includes preprocessor macro info." nil t)
2952 (setq gdb-macro-info t))
2953 (if gdb-many-windows
2955 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2957 (let ((pop-up-windows t))
2958 (display-buffer (gud-find-file gdb-main-file))))))
2960 (defun gdb-get-location (bptno line flag)
2961 "Find the directory containing the relevant source file.
2962 Put in buffer and place breakpoint icon."
2963 (goto-char (point-min))
2964 (catch 'file-not-found
2965 (if (search-forward "Located in " nil t)
2966 (when (looking-at "\\S-+")
2967 (delete (cons bptno "File not found") gdb-location-alist)
2968 (push (cons bptno (match-string 0)) gdb-location-alist))
2970 (unless (assoc bptno gdb-location-alist)
2971 (push (cons bptno "File not found") gdb-location-alist)
2972 (message-box "Cannot find source file for breakpoint location.\n\
2973 Add directory to search path for source files using the GDB command, dir."))
2974 (throw 'file-not-found nil))
2975 (with-current-buffer
2976 (find-file-noselect (match-string 0))
2977 (save-current-buffer
2978 (set (make-local-variable 'gud-minor-mode) 'gdba)
2979 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2980 ;; only want one breakpoint icon at each location
2982 (goto-line (string-to-number line))
2983 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2985 (add-hook 'find-file-hook 'gdb-find-file-hook)
2987 (defun gdb-find-file-hook ()
2988 "Set up buffer for debugging if file is part of the source code
2989 of the current session."
2990 (if (and (buffer-name gud-comint-buffer)
2991 ;; in case gud or gdb-ui is just loaded
2993 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2995 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2996 (if (member (if (string-equal gdb-version "pre-6.4")
2997 (file-name-nondirectory buffer-file-name)
2999 gdb-source-file-list)
3000 (with-current-buffer (find-buffer-visiting buffer-file-name)
3001 (set (make-local-variable 'gud-minor-mode)
3002 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3003 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3006 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3007 "Put string PUTSTRING in front of POS in the current buffer.
3008 PUTSTRING is displayed by putting an overlay into the current buffer with a
3009 `before-string' string that has a `display' property whose value is
3011 (let ((string (make-string 1 ?x))
3012 (buffer (current-buffer)))
3013 (setq putstring (copy-sequence putstring))
3014 (let ((overlay (make-overlay pos pos buffer))
3016 (list (list 'margin 'left-margin) putstring))))
3017 (put-text-property 0 1 'display prop string)
3019 (add-text-properties 0 1 sprops string))
3020 (overlay-put overlay 'put-break t)
3021 (overlay-put overlay 'before-string string))))
3023 ;;from remove-images
3024 (defun gdb-remove-strings (start end &optional buffer)
3025 "Remove strings between START and END in BUFFER.
3026 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3027 BUFFER nil or omitted means use the current buffer."
3029 (setq buffer (current-buffer)))
3030 (dolist (overlay (overlays-in start end))
3031 (when (overlay-get overlay 'put-break)
3032 (delete-overlay overlay))))
3034 (defun gdb-put-breakpoint-icon (enabled bptno)
3035 (let ((start (- (line-beginning-position) 1))
3036 (end (+ (line-end-position) 1))
3037 (putstring (if enabled "B" "b"))
3038 (source-window (get-buffer-window (current-buffer) 0)))
3039 (add-text-properties
3040 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3043 (add-text-properties
3044 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3045 (add-text-properties
3046 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3047 (gdb-remove-breakpoint-icons start end)
3048 (if (display-images-p)
3049 (if (>= (or left-fringe-width
3050 (if source-window (car (window-fringes source-window)))
3051 gdb-buffer-fringe-width) 8)
3054 `(left-fringe breakpoint
3057 'breakpoint-disabled))
3059 'gdb-enabled enabled)
3060 (when (< left-margin-width 2)
3061 (save-current-buffer
3062 (setq left-margin-width 2)
3066 left-margin-width right-margin-width))))
3069 (or breakpoint-enabled-icon
3070 (setq breakpoint-enabled-icon
3071 (find-image `((:type xpm :data
3072 ,breakpoint-xpm-data
3073 :ascent 100 :pointer hand)
3075 ,breakpoint-enabled-pbm-data
3076 :ascent 100 :pointer hand)))))
3077 (or breakpoint-disabled-icon
3078 (setq breakpoint-disabled-icon
3079 (find-image `((:type xpm :data
3080 ,breakpoint-xpm-data
3081 :conversion disabled
3082 :ascent 100 :pointer hand)
3084 ,breakpoint-disabled-pbm-data
3085 :ascent 100 :pointer hand))))))
3089 (when (< left-margin-width 2)
3090 (save-current-buffer
3091 (setq left-margin-width 2)
3092 (let ((window (get-buffer-window (current-buffer) 0)))
3095 window left-margin-width right-margin-width)))))
3097 (propertize putstring
3098 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3101 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3102 (gdb-remove-strings start end)
3103 (if (display-images-p)
3104 (remove-images start end))
3106 (setq left-margin-width 0)
3107 (let ((window (get-buffer-window (current-buffer) 0)))
3110 window left-margin-width right-margin-width)))))
3114 ;; Assembler buffer.
3116 (gdb-set-buffer-rules 'gdb-assembler-buffer
3117 'gdb-assembler-buffer-name
3118 'gdb-assembler-mode)
3120 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3121 ;; window-start but keep the overlay arrow/current line visible.
3122 (defun gdb-assembler-handler ()
3123 (setq gdb-pending-triggers
3124 (delq 'gdb-invalidate-assembler
3125 gdb-pending-triggers))
3126 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3128 (with-current-buffer buf
3129 (let* ((window (get-buffer-window buf 0))
3130 (p (window-point window))
3131 (buffer-read-only nil))
3133 (insert-buffer-substring (gdb-get-buffer-create
3134 'gdb-partial-output-buffer))
3135 (set-window-point window p)))))
3136 ;; put customisation here
3137 (gdb-assembler-custom))
3139 (defun gdb-assembler-custom ()
3140 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3141 (pos 1) (address) (flag) (bptno))
3142 (with-current-buffer buffer
3144 (if (not (equal gdb-pc-address "main"))
3146 (goto-char (point-min))
3147 (if (and gdb-pc-address
3148 (search-forward gdb-pc-address nil t))
3152 (setq fringe-indicator-alist
3153 (if (string-equal gdb-frame-number "0")
3155 '((overlay-arrow . hollow-right-triangle))))
3156 (or gdb-overlay-arrow-position
3157 (setq gdb-overlay-arrow-position (make-marker)))
3158 (set-marker gdb-overlay-arrow-position (point))))))
3159 ;; remove all breakpoint-icons in assembler buffer before updating.
3160 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3161 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3162 (goto-char (point-min))
3163 (while (< (point) (- (point-max) 1))
3165 (if (looking-at "[^\t].*?breakpoint")
3168 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3169 (setq bptno (match-string 1))
3170 (setq flag (char-after (match-beginning 2)))
3171 (setq address (match-string 3))
3172 (with-current-buffer buffer
3174 (goto-char (point-min))
3175 (if (search-forward address nil t)
3176 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3177 (if (not (equal gdb-pc-address "main"))
3178 (with-current-buffer buffer
3179 (set-window-point (get-buffer-window buffer 0) pos)))))
3181 (defvar gdb-assembler-mode-map
3182 (let ((map (make-sparse-keymap)))
3183 (suppress-keymap map)
3184 (define-key map "q" 'kill-this-buffer)
3187 (defvar gdb-assembler-font-lock-keywords
3188 '(;; <__function.name+n>
3189 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3190 (1 font-lock-function-name-face))
3191 ;; 0xNNNNNNNN <__function.name+n>: opcode
3192 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3193 (4 font-lock-keyword-face))
3194 ;; %register(at least i386)
3195 ("%\\sw+" . font-lock-variable-name-face)
3196 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3197 (1 font-lock-comment-face)
3198 (2 font-lock-function-name-face))
3199 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3200 "Font lock keywords used in `gdb-assembler-mode'.")
3202 (defun gdb-assembler-mode ()
3203 "Major mode for viewing code assembler.
3205 \\{gdb-assembler-mode-map}"
3206 (kill-all-local-variables)
3207 (setq major-mode 'gdb-assembler-mode)
3208 (setq mode-name (concat "Machine:" gdb-selected-frame))
3209 (setq gdb-overlay-arrow-position nil)
3210 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3211 (setq fringes-outside-margins t)
3212 (setq buffer-read-only t)
3213 (use-local-map gdb-assembler-mode-map)
3214 (gdb-invalidate-assembler)
3215 (set (make-local-variable 'font-lock-defaults)
3216 '(gdb-assembler-font-lock-keywords))
3217 (run-mode-hooks 'gdb-assembler-mode-hook)
3218 'gdb-invalidate-assembler)
3220 (defun gdb-assembler-buffer-name ()
3221 (with-current-buffer gud-comint-buffer
3222 (concat "*disassembly of " (gdb-get-target-string) "*")))
3224 (defun gdb-display-assembler-buffer ()
3225 "Display disassembly view."
3227 (setq gdb-previous-frame nil)
3229 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3231 (defun gdb-frame-assembler-buffer ()
3232 "Display disassembly view in a new frame."
3234 (setq gdb-previous-frame nil)
3235 (let ((special-display-regexps (append special-display-regexps '(".*")))
3236 (special-display-frame-alist gdb-frame-parameters))
3237 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3239 ;; modified because if gdb-pc-address has changed value a new command
3240 ;; must be enqueued to update the buffer with the new output
3241 (defun gdb-invalidate-assembler (&optional ignored)
3242 (if (gdb-get-buffer 'gdb-assembler-buffer)
3244 (unless (and gdb-selected-frame
3245 (string-equal gdb-selected-frame gdb-previous-frame))
3246 (if (or (not (member 'gdb-invalidate-assembler
3247 gdb-pending-triggers))
3248 (not (string-equal gdb-pc-address
3249 gdb-previous-frame-address)))
3251 ;; take previous disassemble command, if any, off the queue
3252 (with-current-buffer gud-comint-buffer
3253 (let ((queue gdb-input-queue))
3254 (dolist (item queue)
3255 (if (equal (cdr item) '(gdb-assembler-handler))
3256 (setq gdb-input-queue
3257 (delete item gdb-input-queue))))))
3260 (concat gdb-server-prefix "disassemble "
3261 (if (member gdb-pc-address '(nil "main")) nil "0x")
3262 gdb-pc-address "\n")
3263 'gdb-assembler-handler))
3264 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3265 (setq gdb-previous-frame-address gdb-pc-address)
3266 (setq gdb-previous-frame gdb-selected-frame)))))))
3268 (defun gdb-get-selected-frame ()
3269 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3272 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3273 (push 'gdb-get-selected-frame
3274 gdb-pending-triggers))))
3276 (defun gdb-frame-handler ()
3277 (setq gdb-pending-triggers
3278 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3279 (goto-char (point-min))
3280 (when (re-search-forward
3281 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3282 (setq gdb-frame-number (match-string 1))
3283 (setq gdb-frame-address (match-string 2)))
3284 (goto-char (point-min))
3285 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3286 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3288 (setq gdb-selected-frame (match-string 2))
3289 (if (gdb-get-buffer 'gdb-locals-buffer)
3290 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3291 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3292 (if (gdb-get-buffer 'gdb-assembler-buffer)
3293 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3294 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3295 (setq gdb-pc-address (match-string 1))
3296 (if (and (match-string 3) gud-overlay-arrow-position)
3297 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3298 (position (marker-position gud-overlay-arrow-position)))
3300 (string-equal (buffer-name buffer)
3301 (file-name-nondirectory (match-string 3))))
3302 (with-current-buffer buffer
3303 (setq fringe-indicator-alist
3304 (if (string-equal gdb-frame-number "0")
3306 '((overlay-arrow . hollow-right-triangle))))
3307 (set-marker gud-overlay-arrow-position position))))))
3308 (goto-char (point-min))
3309 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3310 (setq gdb-current-language (match-string 1)))
3311 (gdb-invalidate-assembler))
3314 ;; Code specific to GDB 6.4
3315 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3317 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3318 "Create list of source files for current GDB session.
3319 If buffers already exist for any of these files, gud-minor-mode
3321 (goto-char (point-min))
3322 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3323 (push (match-string 1) gdb-source-file-list))
3324 (dolist (buffer (buffer-list))
3325 (with-current-buffer buffer
3326 (when (member buffer-file-name gdb-source-file-list)
3327 (set (make-local-variable 'gud-minor-mode)
3328 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3329 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3330 (when gud-tooltip-mode
3331 (make-local-variable 'gdb-define-alist)
3332 (gdb-create-define-alist)
3333 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3334 (gdb-force-mode-line-update
3335 (propertize "ready" 'face font-lock-variable-name-face)))
3337 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3338 (defun gdb-var-list-children-1 (varnum)
3341 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3342 (concat "server interpreter mi \"-var-list-children --all-values "
3344 (concat "-var-list-children --all-values " varnum "\n"))
3345 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3347 (defconst gdb-var-list-children-regexp-1
3348 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3349 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3351 (defun gdb-var-list-children-handler-1 (varnum)
3352 (goto-char (point-min))
3353 (let ((var-list nil))
3354 (catch 'child-already-watched
3355 (dolist (var gdb-var-list)
3356 (if (string-equal varnum (car var))
3359 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3360 (let ((varchild (list (match-string 1)
3364 (read (match-string 4))
3366 (if (assoc (car varchild) gdb-var-list)
3367 (throw 'child-already-watched nil))
3368 (push varchild var-list))))
3369 (push var var-list)))
3370 (setq gdb-var-list (nreverse var-list))))
3371 (gdb-speedbar-update))
3373 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3374 (defun gdb-var-update-1 ()
3375 (if (not (member 'gdb-var-update gdb-pending-triggers))
3379 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3380 "server interpreter mi \"-var-update --all-values *\"\n"
3381 "-var-update --all-values *\n")
3382 'gdb-var-update-handler-1))
3383 (push 'gdb-var-update gdb-pending-triggers))))
3385 (defconst gdb-var-update-regexp-1
3386 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3387 in_scope=\"\\(.*?\\)\".*?}")
3389 (defun gdb-var-update-handler-1 ()
3390 (dolist (var gdb-var-list)
3391 (setcar (nthcdr 5 var) nil))
3392 (goto-char (point-min))
3393 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3394 (let* ((varnum (match-string 1))
3395 (var (assoc varnum gdb-var-list)))
3397 (if (string-equal (match-string 3) "false")
3398 (setcar (nthcdr 5 var) 'out-of-scope)
3399 (setcar (nthcdr 5 var) 'changed)
3400 (setcar (nthcdr 4 var)
3401 (read (match-string 2)))))))
3402 (setq gdb-pending-triggers
3403 (delq 'gdb-var-update gdb-pending-triggers))
3404 (gdb-speedbar-update))
3406 ;; Registers buffer.
3408 (gdb-set-buffer-rules 'gdb-registers-buffer
3409 'gdb-registers-buffer-name
3410 'gdb-registers-mode)
3412 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3413 (gdb-get-buffer 'gdb-registers-buffer)
3414 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3415 "server interpreter mi \"-data-list-register-values x\"\n"
3416 "-data-list-register-values x\n")
3417 gdb-data-list-register-values-handler)
3419 (defconst gdb-data-list-register-values-regexp
3420 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3422 (defun gdb-data-list-register-values-handler ()
3423 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3424 gdb-pending-triggers))
3425 (goto-char (point-min))
3426 (if (re-search-forward gdb-error-regexp nil t)
3427 (let ((err (match-string 1)))
3428 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3429 (let ((buffer-read-only nil))
3431 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3433 (goto-char (point-min)))))
3434 (let ((register-list (reverse gdb-register-names))
3435 (register nil) (register-string nil) (register-values nil))
3436 (goto-char (point-min))
3437 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3438 (setq register (pop register-list))
3439 (setq register-string (concat register "\t" (match-string 2) "\n"))
3440 (if (member (match-string 1) gdb-changed-registers)
3441 (put-text-property 0 (length register-string)
3442 'face 'font-lock-warning-face
3444 (setq register-values
3445 (concat register-values register-string)))
3446 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3447 (with-current-buffer buf
3448 (let* ((window (get-buffer-window buf 0))
3449 (start (window-start window))
3450 (p (window-point window))
3451 (buffer-read-only nil))
3453 (insert register-values)
3454 (set-window-start window start)
3455 (set-window-point window p))))))
3456 (gdb-data-list-register-values-custom))
3458 (defun gdb-data-list-register-values-custom ()
3459 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3461 (let ((buffer-read-only nil)
3463 (goto-char (point-min))
3464 (while (< (point) (point-max))
3465 (setq start (line-beginning-position))
3466 (setq end (line-end-position))
3467 (when (looking-at "^[^\t]+")
3468 (unless (string-equal (match-string 0) "No registers.")
3469 (put-text-property start (match-end 0)
3470 'face font-lock-variable-name-face)
3471 (add-text-properties start end
3472 '(help-echo "mouse-2: edit value"
3473 mouse-face highlight))))
3474 (forward-line 1))))))
3476 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3477 (defun gdb-get-changed-registers ()
3478 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3479 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3483 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3484 "server interpreter mi -data-list-changed-registers\n"
3485 "-data-list-changed-registers\n")
3486 'gdb-get-changed-registers-handler))
3487 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3489 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3491 (defun gdb-get-changed-registers-handler ()
3492 (setq gdb-pending-triggers
3493 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3494 (setq gdb-changed-registers nil)
3495 (goto-char (point-min))
3496 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3497 (push (match-string 1) gdb-changed-registers)))
3502 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3503 (gdb-set-buffer-rules 'gdb-locals-buffer
3504 'gdb-locals-buffer-name
3507 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3508 (gdb-get-buffer 'gdb-locals-buffer)
3509 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3510 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3511 "-stack-list-locals --simple-values\n")
3512 gdb-stack-list-locals-handler)
3514 (defconst gdb-stack-list-locals-regexp
3515 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3517 (defvar gdb-locals-watch-map-1
3518 (let ((map (make-sparse-keymap)))
3519 (suppress-keymap map)
3520 (define-key map "\r" 'gud-watch)
3521 (define-key map [mouse-2] 'gud-watch)
3523 "Keymap to create watch expression of a complex data type local variable.")
3525 (defvar gdb-edit-locals-map-1
3526 (let ((map (make-sparse-keymap)))
3527 (suppress-keymap map)
3528 (define-key map "\r" 'gdb-edit-locals-value)
3529 (define-key map [mouse-2] 'gdb-edit-locals-value)
3531 "Keymap to edit value of a simple data type local variable.")
3533 (defun gdb-edit-locals-value (&optional event)
3534 "Assign a value to a variable displayed in the locals buffer."
3535 (interactive (list last-input-event))
3537 (if event (posn-set-point (event-end event)))
3539 (let* ((var (current-word))
3540 (value (read-string (format "New value (%s): " var))))
3542 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3545 ;; Dont display values of arrays or structures.
3546 ;; These can be expanded using gud-watch.
3547 (defun gdb-stack-list-locals-handler ()
3548 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3549 gdb-pending-triggers))
3550 (goto-char (point-min))
3551 (if (re-search-forward gdb-error-regexp nil t)
3552 (let ((err (match-string 1)))
3553 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3554 (let ((buffer-read-only nil))
3557 (goto-char (point-min)))))
3558 (let (local locals-list)
3559 (goto-char (point-min))
3560 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3561 (let ((local (list (match-string 1)
3564 (if (looking-at ",value=\\(\".*\"\\).*?}")
3565 (setcar (nthcdr 2 local) (read (match-string 1))))
3566 (push local locals-list)))
3567 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3568 (and buf (with-current-buffer buf
3569 (let* ((window (get-buffer-window buf 0))
3570 (start (window-start window))
3571 (p (window-point window))
3572 (buffer-read-only nil) (name) (value))
3574 (dolist (local locals-list)
3575 (setq name (car local))
3576 (setq value (nth 2 local))
3578 (string-match "^\\0x" value))
3579 (add-text-properties 0 (length name)
3580 `(mouse-face highlight
3581 help-echo "mouse-2: create watch expression"
3582 local-map ,gdb-locals-watch-map-1)
3584 (add-text-properties 0 (length value)
3585 `(mouse-face highlight
3586 help-echo "mouse-2: edit value"
3587 local-map ,gdb-edit-locals-map-1)
3590 (concat name "\t" (nth 1 local)
3592 (set-window-start window start)
3593 (set-window-point window p))))))))
3595 (defun gdb-get-register-names ()
3596 "Create a list of register names."
3597 (goto-char (point-min))
3598 (setq gdb-register-names nil)
3599 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3600 (push (match-string 1) gdb-register-names)))
3604 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3605 ;;; gdb-ui.el ends here