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 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
907 (cond ((string-match "+" text) ;expand this node
908 (if (and (eq (buffer-local-value
909 'gud-minor-mode gud-comint-buffer) 'gdba)
910 (string-equal gdb-version "pre-6.4"))
911 (gdb-var-list-children token)
912 (gdb-var-list-children-1 token)))
913 ((string-match "-" text) ;contract this node
914 (dolist (var gdb-var-list)
915 (if (string-match (concat token "\\.") (car var))
916 (setq gdb-var-list (delq var gdb-var-list))))
917 (speedbar-change-expand-button-char ?+)
918 (speedbar-delete-subblock indent))
919 (t (error "Ooops... not sure what to do")))
920 (speedbar-center-buffer-smartly))
921 (message-box "GUD session has been killed")))
923 (defun gdb-get-target-string ()
924 (with-current-buffer gud-comint-buffer
931 ;; Each buffer has a TYPE -- a symbol that identifies the function
932 ;; of that particular buffer.
934 ;; The usual gdb interaction buffer is given the type `gdba' and
935 ;; is constructed specially.
937 ;; Others are constructed by gdb-get-buffer-create and
938 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
940 (defvar gdb-buffer-rules-assoc '())
942 (defun gdb-get-buffer (key)
943 "Return the gdb buffer tagged with type KEY.
944 The key should be one of the cars in `gdb-buffer-rules-assoc'."
946 (gdb-look-for-tagged-buffer key (buffer-list))))
948 (defun gdb-get-buffer-create (key)
949 "Create a new gdb buffer of the type specified by KEY.
950 The key should be one of the cars in `gdb-buffer-rules-assoc'."
951 (or (gdb-get-buffer key)
952 (let* ((rules (assoc key gdb-buffer-rules-assoc))
953 (name (funcall (gdb-rules-name-maker rules)))
954 (new (get-buffer-create name)))
955 (with-current-buffer new
957 (if (cdr (cdr rules))
958 (setq trigger (funcall (car (cdr (cdr rules))))))
959 (setq gdb-buffer-type key)
960 (set (make-local-variable 'gud-minor-mode)
961 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
962 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
963 (if trigger (funcall trigger)))
966 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
968 (defun gdb-look-for-tagged-buffer (key bufs)
970 (while (and (not retval) bufs)
971 (set-buffer (car bufs))
972 (if (eq gdb-buffer-type key)
973 (setq retval (car bufs)))
974 (setq bufs (cdr bufs)))
978 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
979 ;; at least one and possible more functions. The functions have these
980 ;; roles in defining a buffer type:
982 ;; NAME - Return a name for this buffer type.
984 ;; The remaining function(s) are optional:
986 ;; MODE - called in a new buffer with no arguments, should establish
987 ;; the proper mode for the buffer.
990 (defun gdb-set-buffer-rules (buffer-type &rest rules)
991 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
993 (setcdr binding rules)
994 (push (cons buffer-type rules)
995 gdb-buffer-rules-assoc))))
997 ;; GUD buffers are an exception to the rules
998 (gdb-set-buffer-rules 'gdba 'error)
1000 ;; Partial-output buffer : This accumulates output from a command executed on
1001 ;; behalf of emacs (rather than the user).
1003 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1004 'gdb-partial-output-name)
1006 (defun gdb-partial-output-name ()
1007 (concat "*partial-output-"
1008 (gdb-get-target-string)
1012 (gdb-set-buffer-rules 'gdb-inferior-io
1013 'gdb-inferior-io-name
1014 'gdb-inferior-io-mode)
1016 (defun gdb-inferior-io-name ()
1017 (concat "*input/output of "
1018 (gdb-get-target-string)
1021 (defun gdb-display-separate-io-buffer ()
1022 "Display IO of debugged program in a separate window."
1024 (if gdb-use-separate-io-buffer
1026 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1028 (defconst gdb-frame-parameters
1029 '((height . 14) (width . 80)
1031 (tool-bar-lines . nil)
1032 (menu-bar-lines . nil)
1033 (minibuffer . nil)))
1035 (defun gdb-frame-separate-io-buffer ()
1036 "Display IO of debugged program in a new frame."
1038 (if gdb-use-separate-io-buffer
1039 (let ((special-display-regexps (append special-display-regexps '(".*")))
1040 (special-display-frame-alist gdb-frame-parameters))
1041 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1043 (defvar gdb-inferior-io-mode-map
1044 (let ((map (make-sparse-keymap)))
1045 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1046 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1047 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1048 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1049 (define-key map "\C-d" 'gdb-separate-io-eof)
1052 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1053 "Major mode for gdb inferior-io."
1054 :syntax-table nil :abbrev-table nil
1055 ;; We want to use comint because it has various nifty and familiar
1056 ;; features. We don't need a process, but comint wants one, so create
1058 (make-comint-in-buffer
1059 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1060 (current-buffer) "hexl")
1061 (setq comint-input-sender 'gdb-inferior-io-sender))
1063 (defun gdb-inferior-io-sender (proc string)
1064 ;; PROC is the pseudo-process created to satisfy comint.
1065 (with-current-buffer (process-buffer proc)
1066 (setq proc (get-buffer-process gud-comint-buffer))
1067 (process-send-string proc string)
1068 (process-send-string proc "\n")))
1070 (defun gdb-separate-io-interrupt ()
1071 "Interrupt the program being debugged."
1074 (get-buffer-process gud-comint-buffer) comint-ptyp))
1076 (defun gdb-separate-io-quit ()
1077 "Send quit signal to the program being debugged."
1080 (get-buffer-process gud-comint-buffer) comint-ptyp))
1082 (defun gdb-separate-io-stop ()
1083 "Stop the program being debugged."
1086 (get-buffer-process gud-comint-buffer) comint-ptyp))
1088 (defun gdb-separate-io-eof ()
1089 "Send end-of-file to the program being debugged."
1092 (get-buffer-process gud-comint-buffer)))
1095 ;; gdb communications
1098 ;; INPUT: things sent to gdb
1100 ;; The queues are lists. Each element is either a string (indicating user or
1101 ;; user-like input) or a list of the form:
1103 ;; (INPUT-STRING HANDLER-FN)
1105 ;; The handler function will be called from the partial-output buffer when the
1106 ;; command completes. This is the way to write commands which invoke gdb
1107 ;; commands autonomously.
1109 ;; These lists are consumed tail first.
1112 (defun gdb-send (proc string)
1113 "A comint send filter for gdb.
1114 This filter may simply queue input for a later time."
1115 (with-current-buffer gud-comint-buffer
1116 (let ((inhibit-read-only t))
1117 (remove-text-properties (point-min) (point-max) '(face))))
1120 (let ((item (concat string "\n")))
1121 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1122 (process-send-string proc item)))
1123 (if (and (string-match "\\\\$" string)
1124 (not comint-input-sender-no-newline)) ;;Try to catch C-d.
1125 (setq gdb-continuation (concat gdb-continuation string "\n"))
1126 (let ((item (concat gdb-continuation string "\n")))
1127 (gdb-enqueue-input item)
1128 (setq gdb-continuation nil)))))
1130 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1131 ;; is a query, or other non-top-level prompt.
1133 (defun gdb-enqueue-input (item)
1134 (if (not gud-running)
1137 (gdb-send-item item)
1138 (setq gdb-prompting nil))
1139 (push item gdb-input-queue))))
1141 (defun gdb-dequeue-input ()
1142 (let ((queue gdb-input-queue))
1144 (let ((last (car (last queue))))
1145 (unless (nbutlast queue) (setq gdb-input-queue '()))
1148 (defun gdb-send-item (item)
1149 (setq gdb-flush-pending-output nil)
1150 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1151 (setq gdb-current-item item)
1152 (let ((process (get-buffer-process gud-comint-buffer)))
1153 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1156 (setq gdb-output-sink 'user)
1157 (process-send-string process item))
1159 (gdb-clear-partial-output)
1160 (setq gdb-output-sink 'pre-emacs)
1161 (process-send-string process
1163 ;; case: eq gud-minor-mode 'gdbmi
1164 (gdb-clear-partial-output)
1165 (setq gdb-output-sink 'emacs)
1166 (process-send-string process (car item)))))
1169 ;; output -- things gdb prints to emacs
1171 ;; GDB output is a stream interrupted by annotations.
1172 ;; Annotations can be recognized by their beginning
1173 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1175 ;; The tag is a string obeying symbol syntax.
1177 ;; The optional part `<opt>' can be either the empty string
1178 ;; or a space followed by more data relating to the annotation.
1179 ;; For example, the SOURCE annotation is followed by a filename,
1180 ;; line number and various useless goo. This data must not include
1184 (defcustom gud-gdba-command-name "gdb -annotate=3"
1185 "Default command to execute an executable under the GDB-UI debugger."
1190 (defvar gdb-annotation-rules
1191 '(("pre-prompt" gdb-pre-prompt)
1192 ("prompt" gdb-prompt)
1193 ("commands" gdb-subprompt)
1194 ("overload-choice" gdb-subprompt)
1195 ("query" gdb-subprompt)
1196 ;; Need this prompt for GDB 6.1
1197 ("nquery" gdb-subprompt)
1198 ("prompt-for-continue" gdb-subprompt)
1199 ("post-prompt" gdb-post-prompt)
1200 ("source" gdb-source)
1201 ("starting" gdb-starting)
1202 ("exited" gdb-exited)
1203 ("signalled" gdb-signalled)
1204 ("signal" gdb-signal)
1205 ("breakpoint" gdb-stopping)
1206 ("watchpoint" gdb-stopping)
1207 ("frame-begin" gdb-frame-begin)
1208 ("stopped" gdb-stopped)
1209 ("error-begin" gdb-error)
1211 ) "An assoc mapping annotation tags to functions which process them.")
1214 (setq gdb-flush-pending-output t)
1215 (setq gud-running nil)
1216 (gdb-force-mode-line-update
1217 (propertize "stopped"'face font-lock-warning-face))
1218 (setq gdb-output-sink 'user)
1219 (setq gdb-input-queue nil)
1220 (setq gdb-pending-triggers nil)
1221 (setq gdb-prompting t))
1223 (defconst gdb-source-spec-regexp
1224 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1226 ;; Do not use this except as an annotation handler.
1227 (defun gdb-source (args)
1228 (string-match gdb-source-spec-regexp args)
1229 ;; Extract the frame position from the marker.
1230 (setq gud-last-frame
1232 (match-string 1 args)
1233 (string-to-number (match-string 2 args))))
1234 (setq gdb-pc-address (match-string 3 args))
1235 ;; cover for auto-display output which comes *before*
1236 ;; stopped annotation
1237 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1239 (defun gdb-pre-prompt (ignored)
1240 "An annotation handler for `pre-prompt'.
1241 This terminates the collection of output from a previous command if that
1242 happens to be in effect."
1243 (setq gdb-error nil)
1244 (let ((sink gdb-output-sink))
1248 (setq gdb-output-sink 'post-emacs))
1251 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1253 (defun gdb-prompt (ignored)
1254 "An annotation handler for `prompt'.
1255 This sends the next command (if any) to gdb."
1256 (when gdb-first-prompt
1257 (gdb-force-mode-line-update
1258 (propertize "initializing..." 'face font-lock-variable-name-face))
1260 (setq gdb-first-prompt nil))
1261 (let ((sink gdb-output-sink))
1264 ((eq sink 'post-emacs)
1265 (setq gdb-output-sink 'user)
1267 (car (cdr gdb-current-item))))
1268 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1269 (funcall handler))))
1272 (error "Phase error in gdb-prompt (got %s)" sink))))
1273 (let ((input (gdb-dequeue-input)))
1275 (gdb-send-item input)
1277 (setq gdb-prompting t)
1278 (gud-display-frame)))))
1280 (defun gdb-subprompt (ignored)
1281 "An annotation handler for non-top-level prompts."
1282 (setq gdb-prompting t))
1284 (defun gdb-starting (ignored)
1285 "An annotation handler for `starting'.
1286 This says that I/O for the subprocess is now the program being debugged,
1288 (setq gdb-active-process t)
1289 (setq gdb-printing t)
1290 (let ((sink gdb-output-sink))
1294 (setq gud-running t)
1295 (setq gdb-inferior-status "running")
1296 (setq gdb-signalled nil)
1297 (gdb-force-mode-line-update
1298 (propertize gdb-inferior-status 'face font-lock-type-face))
1299 (gdb-remove-text-properties)
1300 (setq gud-old-arrow gud-overlay-arrow-position)
1301 (setq gud-overlay-arrow-position nil)
1302 (setq gdb-overlay-arrow-position nil)
1303 (setq gdb-stack-position nil)
1304 (if gdb-use-separate-io-buffer
1305 (setq gdb-output-sink 'inferior))))
1308 (error "Unexpected `starting' annotation")))))
1310 (defun gdb-signal (ignored)
1311 (setq gdb-inferior-status "signal")
1312 (gdb-force-mode-line-update
1313 (propertize gdb-inferior-status 'face font-lock-warning-face))
1314 (gdb-stopping ignored))
1316 (defun gdb-stopping (ignored)
1317 "An annotation handler for `breakpoint' and other annotations.
1318 They say that I/O for the subprocess is now GDB, not the program
1320 (if gdb-use-separate-io-buffer
1321 (let ((sink gdb-output-sink))
1323 ((eq sink 'inferior)
1324 (setq gdb-output-sink 'user))
1327 (error "Unexpected stopping annotation"))))))
1329 (defun gdb-exited (ignored)
1330 "An annotation handler for `exited' and `signalled'.
1331 They say that I/O for the subprocess is now GDB, not the program
1332 being debugged and that the program is no longer running. This
1333 function is used to change the focus of GUD tooltips to #define
1335 (setq gdb-active-process nil)
1336 (setq gud-overlay-arrow-position nil)
1337 (setq gdb-overlay-arrow-position nil)
1338 (setq gdb-stack-position nil)
1339 (setq gud-old-arrow nil)
1340 (setq gdb-inferior-status "exited")
1341 (gdb-force-mode-line-update
1342 (propertize gdb-inferior-status 'face font-lock-warning-face))
1343 (gdb-stopping ignored))
1345 (defun gdb-signalled (ignored)
1346 (setq gdb-signalled t))
1348 (defun gdb-frame-begin (ignored)
1349 (setq gdb-frame-begin t)
1350 (setq gdb-printing nil)
1351 (let ((sink gdb-output-sink))
1353 ((eq sink 'inferior)
1354 (setq gdb-output-sink 'user))
1356 ((eq sink 'emacs) t)
1359 (error "Unexpected frame-begin annotation (%S)" sink)))))
1361 (defcustom gdb-same-frame focus-follows-mouse
1362 "Non-nil means pop up GUD buffer in same frame."
1367 (defcustom gdb-find-source-frame nil
1368 "Non-nil means try to find a source frame further up stack e.g after signal."
1373 (defun gdb-find-source-frame (arg)
1374 "Toggle trying to find a source frame further up stack.
1375 With arg, look for a source frame further up stack iff arg is positive."
1377 (setq gdb-find-source-frame
1379 (not gdb-find-source-frame)
1380 (> (prefix-numeric-value arg) 0)))
1381 (message (format "Looking for source frame %sabled"
1382 (if gdb-find-source-frame "en" "dis"))))
1384 (defun gdb-stopped (ignored)
1385 "An annotation handler for `stopped'.
1386 It is just like `gdb-stopping', except that if we already set the output
1387 sink to `user' in `gdb-stopping', that is fine."
1388 (setq gud-running nil)
1389 (unless (or gud-overlay-arrow-position gud-last-frame)
1390 (if (and gdb-frame-begin gdb-printing)
1391 (setq gud-overlay-arrow-position gud-old-arrow)
1392 ;;Pop up GUD buffer to display current frame when it doesn't have source
1393 ;;information i.e id not compiled with -g as with libc routines generally.
1395 (gdb-display-gdb-buffer)
1396 (gdb-frame-gdb-buffer))
1397 (if gdb-find-source-frame
1398 ;;Try to find source further up stack e.g after signal.
1399 (setq gdb-look-up-stack
1400 (if (gdb-get-buffer 'gdb-stack-buffer)
1403 (gdb-get-buffer-create 'gdb-stack-buffer)
1404 (gdb-invalidate-frames)
1406 (unless (member gdb-inferior-status '("exited" "signal"))
1407 (setq gdb-inferior-status "stopped")
1408 (gdb-force-mode-line-update
1409 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1410 (let ((sink gdb-output-sink))
1412 ((eq sink 'inferior)
1413 (setq gdb-output-sink 'user))
1417 (error "Unexpected stopped annotation"))))
1418 (if gdb-signalled (gdb-exited ignored)))
1420 (defun gdb-error (ignored)
1421 (setq gdb-error (not gdb-error)))
1423 (defun gdb-post-prompt (ignored)
1424 "An annotation handler for `post-prompt'.
1425 This begins the collection of output from the current command if that
1426 happens to be appropriate."
1427 ;; Don't add to queue if there outstanding items or gdb-version is not known
1429 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1430 (gdb-get-selected-frame)
1431 (gdb-invalidate-frames)
1432 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1433 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1434 (gdb-invalidate-breakpoints)
1435 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1436 ;; so gdb-pc-address is updated.
1437 ;; (gdb-invalidate-assembler)
1439 (if (string-equal gdb-version "pre-6.4")
1440 (gdb-invalidate-registers)
1441 (gdb-get-changed-registers)
1442 (gdb-invalidate-registers-1))
1444 (gdb-invalidate-memory)
1445 (if (string-equal gdb-version "pre-6.4")
1446 (gdb-invalidate-locals)
1447 (gdb-invalidate-locals-1))
1449 (gdb-invalidate-threads)
1450 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1451 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1452 ;; Only needed/used with speedbar/watch expressions.
1453 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1454 (if (string-equal gdb-version "pre-6.4")
1456 (gdb-var-update-1)))))
1457 (setq gdb-first-post-prompt nil)
1458 (let ((sink gdb-output-sink))
1461 ((eq sink 'pre-emacs)
1462 (setq gdb-output-sink 'emacs))
1465 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1467 (defconst gdb-buffer-list
1468 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1470 (defun gdb-remove-text-properties ()
1471 (dolist (buffertype gdb-buffer-list)
1472 (let ((buffer (gdb-get-buffer buffertype)))
1474 (with-current-buffer buffer
1475 (let ((inhibit-read-only t))
1476 (remove-text-properties
1477 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1479 ;; GUD displays the selected GDB frame. This might might not be the current
1480 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1481 ;; visited breakpoint is, use that window.
1482 (defun gdb-display-source-buffer (buffer)
1483 (let* ((last-window (if gud-last-last-frame
1485 (gud-find-file (car gud-last-last-frame)))))
1486 (source-window (or last-window
1487 (if (and gdb-source-window
1488 (window-live-p gdb-source-window))
1489 gdb-source-window))))
1491 (setq gdb-source-window source-window)
1492 (set-window-buffer source-window buffer))
1495 (defun gud-gdba-marker-filter (string)
1496 "A gud marker filter for gdb. Handle a burst of output from GDB."
1497 (if gdb-flush-pending-output
1499 (when gdb-enable-debug
1500 (push (cons 'recv string) gdb-debug-ring)
1501 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1502 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1503 ;; Recall the left over gud-marker-acc from last time.
1504 (setq gud-marker-acc (concat gud-marker-acc string))
1505 ;; Start accumulating output for the GUD buffer.
1508 ;; Process all the complete markers in this chunk.
1509 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1510 (let ((annotation (match-string 1 gud-marker-acc)))
1512 ;; Stuff prior to the match is just ordinary output.
1513 ;; It is either concatenated to OUTPUT or directed
1518 (substring gud-marker-acc 0 (match-beginning 0))))
1520 ;; Take that stuff off the gud-marker-acc.
1521 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1523 ;; Parse the tag from the annotation, and maybe its arguments.
1524 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1525 (let* ((annotation-type (match-string 1 annotation))
1526 (annotation-arguments (match-string 2 annotation))
1527 (annotation-rule (assoc annotation-type
1528 gdb-annotation-rules)))
1529 ;; Call the handler for this annotation.
1531 (funcall (car (cdr annotation-rule))
1532 annotation-arguments)
1533 ;; Else the annotation is not recognized. Ignore it silently,
1534 ;; so that GDB can add new annotations without causing
1538 ;; Does the remaining text end in a partial line?
1539 ;; If it does, then keep part of the gud-marker-acc until we get more.
1540 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1543 ;; Everything before the potential marker start can be output.
1545 (gdb-concat-output output
1546 (substring gud-marker-acc 0
1547 (match-beginning 0))))
1549 ;; Everything after, we save, to combine with later input.
1550 (setq gud-marker-acc (substring gud-marker-acc
1551 (match-beginning 0))))
1553 ;; In case we know the gud-marker-acc contains no partial annotations:
1555 (setq output (gdb-concat-output output gud-marker-acc))
1556 (setq gud-marker-acc "")))
1559 (defun gdb-concat-output (so-far new)
1561 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1562 (let ((sink gdb-output-sink))
1564 ((eq sink 'user) (concat so-far new))
1565 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1567 (gdb-append-to-partial-output new)
1569 ((eq sink 'inferior)
1570 (gdb-append-to-inferior-io new)
1574 (error "Bogon output sink %S" sink)))))
1576 (defun gdb-append-to-partial-output (string)
1577 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1578 (goto-char (point-max))
1581 (defun gdb-clear-partial-output ()
1582 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1585 (defun gdb-append-to-inferior-io (string)
1586 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1587 (goto-char (point-max))
1588 (insert-before-markers string))
1589 (if (not (string-equal string ""))
1590 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1592 (defun gdb-clear-inferior-io ()
1593 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1597 ;; One trick is to have a command who's output is always available in a buffer
1598 ;; of it's own, and is always up to date. We build several buffers of this
1601 ;; There are two aspects to this: gdb has to tell us when the output for that
1602 ;; command might have changed, and we have to be able to run the command
1603 ;; behind the user's back.
1605 ;; The output phasing associated with the variable gdb-output-sink
1606 ;; help us to run commands behind the user's back.
1608 ;; Below is the code for specificly managing buffers of output from one
1612 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1613 ;; It adds an input for the command we are tracking. It should be the
1614 ;; annotation rule binding of whatever gdb sends to tell us this command
1615 ;; might have changed it's output.
1617 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1618 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1619 ;; input in the input queue (see comment about ``gdb communications'' above).
1621 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1623 `(defun ,name (&optional ignored)
1624 (if (and ,demand-predicate
1626 gdb-pending-triggers)))
1629 (list ,gdb-command ',output-handler))
1630 (push ',name gdb-pending-triggers)))))
1632 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1634 (setq gdb-pending-triggers
1636 gdb-pending-triggers))
1637 (let ((buf (gdb-get-buffer ',buf-key)))
1639 (with-current-buffer buf
1640 (let* ((window (get-buffer-window buf 0))
1641 (start (window-start window))
1642 (p (window-point window))
1643 (buffer-read-only nil))
1645 (insert-buffer-substring (gdb-get-buffer-create
1646 'gdb-partial-output-buffer))
1647 (set-window-start window start)
1648 (set-window-point window p)))))
1649 ;; put customisation here
1652 (defmacro def-gdb-auto-updated-buffer (buffer-key
1653 trigger-name gdb-command
1654 output-handler-name custom-defun)
1656 (def-gdb-auto-update-trigger ,trigger-name
1657 ;; The demand predicate:
1658 (gdb-get-buffer ',buffer-key)
1660 ,output-handler-name)
1661 (def-gdb-auto-update-handler ,output-handler-name
1662 ,trigger-name ,buffer-key ,custom-defun)))
1666 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1668 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1669 'gdb-breakpoints-buffer-name
1670 'gdb-breakpoints-mode)
1672 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1673 ;; This defines the auto update rule for buffers of type
1674 ;; `gdb-breakpoints-buffer'.
1676 ;; It defines a function to serve as the annotation handler that
1677 ;; handles the `foo-invalidated' message. That function is called:
1678 gdb-invalidate-breakpoints
1680 ;; To update the buffer, this command is sent to gdb.
1681 "server info breakpoints\n"
1683 ;; This also defines a function to be the handler for the output
1684 ;; from the command above. That function will copy the output into
1685 ;; the appropriately typed buffer. That function will be called:
1686 gdb-info-breakpoints-handler
1687 ;; buffer specific functions
1688 gdb-info-breakpoints-custom)
1690 (defconst breakpoint-xpm-data
1692 static char *magick[] = {
1693 /* columns rows colors chars-per-pixel */
1709 "XPM data used for breakpoint icon.")
1711 (defconst breakpoint-enabled-pbm-data
1714 0 0 0 0 1 1 1 1 0 0 0 0
1715 0 0 0 1 1 1 1 1 1 0 0 0
1716 0 0 1 1 1 1 1 1 1 1 0 0
1717 0 1 1 1 1 1 1 1 1 1 1 0
1718 0 1 1 1 1 1 1 1 1 1 1 0
1719 0 1 1 1 1 1 1 1 1 1 1 0
1720 0 1 1 1 1 1 1 1 1 1 1 0
1721 0 0 1 1 1 1 1 1 1 1 0 0
1722 0 0 0 1 1 1 1 1 1 0 0 0
1723 0 0 0 0 1 1 1 1 0 0 0 0"
1724 "PBM data used for enabled breakpoint icon.")
1726 (defconst breakpoint-disabled-pbm-data
1738 0 0 0 1 0 1 0 1 0 0"
1739 "PBM data used for disabled breakpoint icon.")
1741 (defvar breakpoint-enabled-icon nil
1742 "Icon for enabled breakpoint in display margin.")
1744 (defvar breakpoint-disabled-icon nil
1745 "Icon for disabled breakpoint in display margin.")
1747 (and (display-images-p)
1748 ;; Bitmap for breakpoint in fringe
1749 (define-fringe-bitmap 'breakpoint
1750 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1751 ;; Bitmap for gud-overlay-arrow in fringe
1752 (define-fringe-bitmap 'hollow-right-triangle
1753 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1755 (defface breakpoint-enabled
1759 "Face for enabled breakpoint icon in fringe."
1762 (defface breakpoint-disabled
1763 ;; We use different values of grey for different background types,
1764 ;; so that on low-color displays it will end up as something visible
1765 ;; if it has to be approximated.
1766 '((t :foreground "grey70"))
1767 "Face for disabled breakpoint icon in fringe."
1770 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1771 (defun gdb-info-breakpoints-custom ()
1772 (let ((flag) (bptno))
1773 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1774 (dolist (buffer (buffer-list))
1775 (with-current-buffer buffer
1776 (if (and (memq gud-minor-mode '(gdba gdbmi))
1777 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1778 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1779 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1781 (let ((buffer-read-only nil))
1782 (goto-char (point-min))
1783 (while (< (point) (- (point-max) 1))
1785 (if (looking-at gdb-breakpoint-regexp)
1787 (setq bptno (match-string 1))
1788 (setq flag (char-after (match-beginning 2)))
1789 (add-text-properties
1790 (match-beginning 2) (match-end 2)
1792 '(face font-lock-warning-face)
1793 '(face font-lock-type-face)))
1795 (el (line-end-position)))
1796 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1798 (add-text-properties
1799 (match-beginning 1) (match-end 1)
1800 '(face font-lock-function-name-face))
1801 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1802 (let ((line (match-string 2))
1803 (file (match-string 1)))
1804 (add-text-properties bl el
1805 '(mouse-face highlight
1806 help-echo "mouse-2, RET: visit breakpoint"))
1807 (unless (file-exists-p file)
1808 (setq file (cdr (assoc bptno gdb-location-alist))))
1810 (not (string-equal file "File not found")))
1811 (with-current-buffer
1812 (find-file-noselect file 'nowarn)
1813 (set (make-local-variable 'gud-minor-mode)
1815 (set (make-local-variable 'tool-bar-map)
1817 ;; Only want one breakpoint icon at each
1820 (goto-line (string-to-number line))
1821 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1824 (concat gdb-server-prefix "list "
1825 (match-string-no-properties 1) ":1\n")
1828 (list (concat gdb-server-prefix "info source\n")
1829 `(lambda () (gdb-get-location
1830 ,bptno ,line ,flag)))))))
1831 (if (re-search-forward
1832 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1834 (add-text-properties
1835 (match-beginning 1) (match-end 1)
1836 '(face font-lock-function-name-face))
1838 (re-search-backward "\\s-\\(\\S-*\\)"
1840 (add-text-properties
1841 (match-beginning 1) (match-end 1)
1842 '(face font-lock-variable-name-face)))))))
1844 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1846 (defun gdb-mouse-set-clear-breakpoint (event)
1847 "Set/clear breakpoint in left fringe/margin with mouse click."
1849 (mouse-minibuffer-check event)
1850 (let ((posn (event-end event)))
1851 (if (numberp (posn-point posn))
1852 (with-selected-window (posn-window posn)
1854 (goto-char (posn-point posn))
1855 (if (or (posn-object posn)
1856 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1859 (gud-break nil)))))))
1861 (defun gdb-mouse-toggle-breakpoint-margin (event)
1862 "Enable/disable breakpoint in left margin with mouse click."
1864 (mouse-minibuffer-check event)
1865 (let ((posn (event-end event)))
1866 (if (numberp (posn-point posn))
1867 (with-selected-window (posn-window posn)
1869 (goto-char (posn-point posn))
1870 (if (posn-object posn)
1873 (let ((bptno (get-text-property
1874 0 'gdb-bptno (car (posn-string posn)))))
1875 (concat gdb-server-prefix
1876 (if (get-text-property
1877 0 'gdb-enabled (car (posn-string posn)))
1883 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1884 "Enable/disable breakpoint in left fringe with mouse click."
1886 (mouse-minibuffer-check event)
1887 (let* ((posn (event-end event))
1888 (pos (posn-point posn))
1891 (with-selected-window (posn-window posn)
1893 (set-buffer (window-buffer (selected-window)))
1895 (dolist (overlay (overlays-in pos pos))
1896 (when (overlay-get overlay 'put-break)
1897 (setq obj (overlay-get overlay 'before-string))))
1901 (concat gdb-server-prefix
1902 (if (get-text-property 0 'gdb-enabled obj)
1905 (get-text-property 0 'gdb-bptno obj) "\n")
1908 (defun gdb-breakpoints-buffer-name ()
1909 (with-current-buffer gud-comint-buffer
1910 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1912 (defun gdb-display-breakpoints-buffer ()
1913 "Display status of user-settable breakpoints."
1916 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
1918 (defun gdb-frame-breakpoints-buffer ()
1919 "Display status of user-settable breakpoints in a new frame."
1921 (let ((special-display-regexps (append special-display-regexps '(".*")))
1922 (special-display-frame-alist gdb-frame-parameters))
1923 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1925 (defvar gdb-breakpoints-mode-map
1926 (let ((map (make-sparse-keymap))
1927 (menu (make-sparse-keymap "Breakpoints")))
1928 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1929 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1930 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1931 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1932 (suppress-keymap map)
1933 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1934 (define-key map " " 'gdb-toggle-breakpoint)
1935 (define-key map "D" 'gdb-delete-breakpoint)
1936 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1937 (define-key map "q" 'gdb-delete-frame-or-window)
1938 (define-key map "\r" 'gdb-goto-breakpoint)
1939 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1940 (define-key map [follow-link] 'mouse-face)
1943 (defun gdb-delete-frame-or-window ()
1944 "Delete frame if there is only one window. Otherwise delete the window."
1946 (if (one-window-p) (delete-frame)
1949 (defun gdb-breakpoints-mode ()
1950 "Major mode for gdb breakpoints.
1952 \\{gdb-breakpoints-mode-map}"
1953 (kill-all-local-variables)
1954 (setq major-mode 'gdb-breakpoints-mode)
1955 (setq mode-name "Breakpoints")
1956 (use-local-map gdb-breakpoints-mode-map)
1957 (setq buffer-read-only t)
1958 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1959 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1960 'gdb-invalidate-breakpoints
1961 'gdbmi-invalidate-breakpoints))
1963 (defconst gdb-breakpoint-regexp
1964 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1966 (defun gdb-toggle-breakpoint ()
1967 "Enable/disable breakpoint at current line."
1970 (beginning-of-line 1)
1971 (if (looking-at gdb-breakpoint-regexp)
1974 (concat gdb-server-prefix
1975 (if (eq ?y (char-after (match-beginning 2)))
1978 (match-string 1) "\n") 'ignore))
1979 (error "Not recognized as break/watchpoint line"))))
1981 (defun gdb-delete-breakpoint ()
1982 "Delete the breakpoint at current line."
1984 (beginning-of-line 1)
1985 (if (looking-at gdb-breakpoint-regexp)
1988 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1989 (error "Not recognized as break/watchpoint line")))
1991 (defun gdb-goto-breakpoint (&optional event)
1992 "Display the breakpoint location specified at current line."
1993 (interactive (list last-input-event))
1994 (if event (posn-set-point (event-end event)))
1996 (beginning-of-line 1)
1997 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1998 (let ((bptno (match-string 1))
1999 (file (match-string 2))
2000 (line (match-string 3)))
2001 (save-selected-window
2002 (let* ((buffer (find-file-noselect
2003 (if (file-exists-p file) file
2004 (cdr (assoc bptno gdb-location-alist)))))
2005 (window (or (gdb-display-source-buffer buffer)
2006 (display-buffer buffer))))
2007 (setq gdb-source-window window)
2008 (with-current-buffer buffer
2009 (goto-line (string-to-number line))
2010 (set-window-point window (point))))))
2011 (error "No location specified."))))
2014 ;; Frames buffer. This displays a perpetually correct bactracktrace
2015 ;; (from the command `where').
2017 ;; Alas, if your stack is deep, it is costly.
2019 (gdb-set-buffer-rules 'gdb-stack-buffer
2020 'gdb-stack-buffer-name
2023 (def-gdb-auto-updated-buffer gdb-stack-buffer
2024 gdb-invalidate-frames
2025 "server info stack\n"
2026 gdb-info-stack-handler
2027 gdb-info-stack-custom)
2029 (defun gdb-info-stack-custom ()
2030 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2032 (unless (eq gdb-look-up-stack 'delete)
2033 (let ((buffer-read-only nil)
2035 (goto-char (point-min))
2036 (while (< (point) (point-max))
2037 (setq bl (line-beginning-position)
2038 el (line-end-position))
2039 (when (looking-at "#")
2040 (add-text-properties bl el
2041 '(mouse-face highlight
2042 help-echo "mouse-2, RET: Select frame")))
2044 (when (looking-at "^#\\([0-9]+\\)")
2045 (when (string-equal (match-string 1) gdb-frame-number)
2046 (if (> (car (window-fringes)) 0)
2048 (or gdb-stack-position
2049 (setq gdb-stack-position (make-marker)))
2050 (set-marker gdb-stack-position (point)))
2051 (put-text-property bl (+ bl 4)
2052 'face '(:inverse-video t))))
2053 (when (re-search-forward
2055 (if (string-equal (match-string 1) "0") "" " in ")
2056 "\\([^ ]+\\) (") el t)
2057 (put-text-property (match-beginning 1) (match-end 1)
2058 'face font-lock-function-name-face)
2059 (setq bl (match-end 0))
2060 (while (re-search-forward "<\\([^>]+\\)>" el t)
2061 (put-text-property (match-beginning 1) (match-end 1)
2062 'face font-lock-function-name-face))
2064 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2065 (put-text-property (match-beginning 1) (match-end 1)
2066 'face font-lock-variable-name-face))))
2068 (when gdb-look-up-stack
2069 (goto-char (point-min))
2070 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2071 (let ((start (line-beginning-position))
2072 (file (match-string 1))
2073 (line (match-string 2)))
2074 (re-search-backward "^#*\\([0-9]+\\)" start t)
2076 (list (concat gdb-server-prefix "frame "
2077 (match-string 1) "\n") 'gdb-set-hollow))
2079 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2080 (if (eq gdb-look-up-stack 'delete)
2081 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2082 (setq gdb-look-up-stack nil))
2084 (defun gdb-set-hollow ()
2085 (if gud-last-last-frame
2086 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2087 (setq fringe-indicator-alist
2088 '((overlay-arrow . hollow-right-triangle))))))
2090 (defun gdb-stack-buffer-name ()
2091 (with-current-buffer gud-comint-buffer
2092 (concat "*stack frames of " (gdb-get-target-string) "*")))
2094 (defun gdb-display-stack-buffer ()
2095 "Display backtrace of current stack."
2098 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2100 (defun gdb-frame-stack-buffer ()
2101 "Display backtrace of current stack in a new frame."
2103 (let ((special-display-regexps (append special-display-regexps '(".*")))
2104 (special-display-frame-alist gdb-frame-parameters))
2105 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2107 (defvar gdb-frames-mode-map
2108 (let ((map (make-sparse-keymap)))
2109 (suppress-keymap map)
2110 (define-key map "q" 'kill-this-buffer)
2111 (define-key map "\r" 'gdb-frames-select)
2112 (define-key map [mouse-2] 'gdb-frames-select)
2113 (define-key map [follow-link] 'mouse-face)
2116 (defun gdb-frames-mode ()
2117 "Major mode for gdb call stack.
2119 \\{gdb-frames-mode-map}"
2120 (kill-all-local-variables)
2121 (setq major-mode 'gdb-frames-mode)
2122 (setq mode-name "Frames")
2123 (setq gdb-stack-position nil)
2124 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2125 (setq buffer-read-only t)
2126 (use-local-map gdb-frames-mode-map)
2127 (run-mode-hooks 'gdb-frames-mode-hook)
2128 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2129 'gdb-invalidate-frames
2130 'gdbmi-invalidate-frames))
2132 (defun gdb-get-frame-number ()
2135 (let* ((start (line-beginning-position))
2136 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2137 (n (or (and pos (match-string-no-properties 1)) "0")))
2140 (defun gdb-frames-select (&optional event)
2141 "Select the frame and display the relevant source."
2142 (interactive (list last-input-event))
2143 (if event (posn-set-point (event-end event)))
2145 (list (concat gdb-server-prefix "frame "
2146 (gdb-get-frame-number) "\n") 'ignore)))
2149 ;; Threads buffer. This displays a selectable thread list.
2151 (gdb-set-buffer-rules 'gdb-threads-buffer
2152 'gdb-threads-buffer-name
2155 (def-gdb-auto-updated-buffer gdb-threads-buffer
2156 gdb-invalidate-threads
2157 (concat gdb-server-prefix "info threads\n")
2158 gdb-info-threads-handler
2159 gdb-info-threads-custom)
2161 (defun gdb-info-threads-custom ()
2162 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2163 (let ((buffer-read-only nil))
2165 (goto-char (point-min))
2166 (while (< (point) (point-max))
2167 (unless (looking-at "No ")
2168 (add-text-properties (line-beginning-position) (line-end-position)
2169 '(mouse-face highlight
2170 help-echo "mouse-2, RET: select thread")))
2171 (forward-line 1))))))
2173 (defun gdb-threads-buffer-name ()
2174 (with-current-buffer gud-comint-buffer
2175 (concat "*threads of " (gdb-get-target-string) "*")))
2177 (defun gdb-display-threads-buffer ()
2178 "Display IDs of currently known threads."
2181 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2183 (defun gdb-frame-threads-buffer ()
2184 "Display IDs of currently known threads in a new frame."
2186 (let ((special-display-regexps (append special-display-regexps '(".*")))
2187 (special-display-frame-alist gdb-frame-parameters))
2188 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2190 (defvar gdb-threads-mode-map
2191 (let ((map (make-sparse-keymap)))
2192 (suppress-keymap map)
2193 (define-key map "q" 'kill-this-buffer)
2194 (define-key map "\r" 'gdb-threads-select)
2195 (define-key map [mouse-2] 'gdb-threads-select)
2196 (define-key map [follow-link] 'mouse-face)
2199 (defvar gdb-threads-font-lock-keywords
2200 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2201 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2202 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2203 "Font lock keywords used in `gdb-threads-mode'.")
2205 (defun gdb-threads-mode ()
2206 "Major mode for gdb threads.
2208 \\{gdb-threads-mode-map}"
2209 (kill-all-local-variables)
2210 (setq major-mode 'gdb-threads-mode)
2211 (setq mode-name "Threads")
2212 (setq buffer-read-only t)
2213 (use-local-map gdb-threads-mode-map)
2214 (set (make-local-variable 'font-lock-defaults)
2215 '(gdb-threads-font-lock-keywords))
2216 (run-mode-hooks 'gdb-threads-mode-hook)
2217 'gdb-invalidate-threads)
2219 (defun gdb-get-thread-number ()
2221 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2222 (match-string-no-properties 1)))
2224 (defun gdb-threads-select (&optional event)
2225 "Select the thread and display the relevant source."
2226 (interactive (list last-input-event))
2227 (if event (posn-set-point (event-end event)))
2229 (list (concat gdb-server-prefix "thread "
2230 (gdb-get-thread-number) "\n") 'ignore))
2231 (gud-display-frame))
2234 ;; Registers buffer.
2236 (defcustom gdb-all-registers nil
2237 "Non-nil means include floating-point registers."
2242 (gdb-set-buffer-rules 'gdb-registers-buffer
2243 'gdb-registers-buffer-name
2244 'gdb-registers-mode)
2246 (def-gdb-auto-updated-buffer gdb-registers-buffer
2247 gdb-invalidate-registers
2249 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2250 gdb-info-registers-handler
2251 gdb-info-registers-custom)
2253 (defun gdb-info-registers-custom ()
2254 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2256 (let ((buffer-read-only nil)
2258 (goto-char (point-min))
2259 (while (< (point) (point-max))
2260 (setq start (line-beginning-position))
2261 (setq end (line-end-position))
2262 (when (looking-at "^[^ ]+")
2263 (unless (string-equal (match-string 0) "The")
2264 (put-text-property start (match-end 0)
2265 'face font-lock-variable-name-face)
2266 (add-text-properties start end
2267 '(help-echo "mouse-2: edit value"
2268 mouse-face highlight))))
2269 (forward-line 1))))))
2271 (defun gdb-edit-register-value (&optional event)
2272 (interactive (list last-input-event))
2274 (if event (posn-set-point (event-end event)))
2276 (let* ((register (current-word))
2277 (value (read-string (format "New value (%s): " register))))
2279 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2282 (defvar gdb-registers-mode-map
2283 (let ((map (make-sparse-keymap)))
2284 (suppress-keymap map)
2285 (define-key map "\r" 'gdb-edit-register-value)
2286 (define-key map [mouse-2] 'gdb-edit-register-value)
2287 (define-key map " " 'gdb-all-registers)
2288 (define-key map "q" 'kill-this-buffer)
2291 (defun gdb-registers-mode ()
2292 "Major mode for gdb registers.
2294 \\{gdb-registers-mode-map}"
2295 (kill-all-local-variables)
2296 (setq major-mode 'gdb-registers-mode)
2297 (setq mode-name "Registers")
2298 (setq buffer-read-only t)
2299 (use-local-map gdb-registers-mode-map)
2300 (run-mode-hooks 'gdb-registers-mode-hook)
2301 (if (string-equal gdb-version "pre-6.4")
2303 (if gdb-all-registers (setq mode-name "Registers:All"))
2304 'gdb-invalidate-registers)
2305 'gdb-invalidate-registers-1))
2307 (defun gdb-registers-buffer-name ()
2308 (with-current-buffer gud-comint-buffer
2309 (concat "*registers of " (gdb-get-target-string) "*")))
2311 (defun gdb-display-registers-buffer ()
2312 "Display integer register contents."
2315 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2317 (defun gdb-frame-registers-buffer ()
2318 "Display integer register contents in a new frame."
2320 (let ((special-display-regexps (append special-display-regexps '(".*")))
2321 (special-display-frame-alist gdb-frame-parameters))
2322 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2324 (defun gdb-all-registers ()
2325 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2327 (when (string-equal gdb-version "pre-6.4")
2328 (if gdb-all-registers
2330 (setq gdb-all-registers nil)
2331 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2332 (setq mode-name "Registers")))
2333 (setq gdb-all-registers t)
2334 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2335 (setq mode-name "Registers:All")))
2336 (message (format "Display of floating-point registers %sabled"
2337 (if gdb-all-registers "en" "dis")))
2338 (gdb-invalidate-registers)))
2343 (defcustom gdb-memory-repeat-count 32
2344 "Number of data items in memory window."
2349 (defcustom gdb-memory-format "x"
2350 "Display format of data items in memory window."
2351 :type '(choice (const :tag "Hexadecimal" "x")
2352 (const :tag "Signed decimal" "d")
2353 (const :tag "Unsigned decimal" "u")
2354 (const :tag "Octal" "o")
2355 (const :tag "Binary" "t"))
2359 (defcustom gdb-memory-unit "w"
2360 "Unit size of data items in memory window."
2361 :type '(choice (const :tag "Byte" "b")
2362 (const :tag "Halfword" "h")
2363 (const :tag "Word" "w")
2364 (const :tag "Giant word" "g"))
2368 (gdb-set-buffer-rules 'gdb-memory-buffer
2369 'gdb-memory-buffer-name
2372 (def-gdb-auto-updated-buffer gdb-memory-buffer
2373 gdb-invalidate-memory
2374 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2375 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2376 gdb-read-memory-handler
2377 gdb-read-memory-custom)
2379 (defun gdb-read-memory-custom ()
2381 (goto-char (point-min))
2382 (if (looking-at "0x[[:xdigit:]]+")
2383 (setq gdb-memory-address (match-string 0)))))
2385 (defvar gdb-memory-mode-map
2386 (let ((map (make-sparse-keymap)))
2387 (suppress-keymap map)
2388 (define-key map "q" 'kill-this-buffer)
2391 (defun gdb-memory-set-address (event)
2392 "Set the start memory address."
2394 (save-selected-window
2395 (select-window (posn-window (event-start event)))
2396 (let ((arg (read-from-minibuffer "Memory address: ")))
2397 (setq gdb-memory-address arg))
2398 (gdb-invalidate-memory)))
2400 (defun gdb-memory-set-repeat-count (event)
2401 "Set the number of data items in memory window."
2403 (save-selected-window
2404 (select-window (posn-window (event-start event)))
2405 (let* ((arg (read-from-minibuffer "Repeat count: "))
2406 (count (string-to-number arg)))
2408 (error "Positive numbers only")
2409 (customize-set-variable 'gdb-memory-repeat-count count)
2410 (gdb-invalidate-memory)))))
2412 (defun gdb-memory-format-binary ()
2413 "Set the display format to binary."
2415 (customize-set-variable 'gdb-memory-format "t")
2416 (gdb-invalidate-memory))
2418 (defun gdb-memory-format-octal ()
2419 "Set the display format to octal."
2421 (customize-set-variable 'gdb-memory-format "o")
2422 (gdb-invalidate-memory))
2424 (defun gdb-memory-format-unsigned ()
2425 "Set the display format to unsigned decimal."
2427 (customize-set-variable 'gdb-memory-format "u")
2428 (gdb-invalidate-memory))
2430 (defun gdb-memory-format-signed ()
2431 "Set the display format to decimal."
2433 (customize-set-variable 'gdb-memory-format "d")
2434 (gdb-invalidate-memory))
2436 (defun gdb-memory-format-hexadecimal ()
2437 "Set the display format to hexadecimal."
2439 (customize-set-variable 'gdb-memory-format "x")
2440 (gdb-invalidate-memory))
2442 (defvar gdb-memory-format-map
2443 (let ((map (make-sparse-keymap)))
2444 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2446 "Keymap to select format in the header line.")
2448 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2449 "Menu of display formats in the header line.")
2451 (define-key gdb-memory-format-menu [binary]
2452 '(menu-item "Binary" gdb-memory-format-binary
2453 :button (:radio . (equal gdb-memory-format "t"))))
2454 (define-key gdb-memory-format-menu [octal]
2455 '(menu-item "Octal" gdb-memory-format-octal
2456 :button (:radio . (equal gdb-memory-format "o"))))
2457 (define-key gdb-memory-format-menu [unsigned]
2458 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2459 :button (:radio . (equal gdb-memory-format "u"))))
2460 (define-key gdb-memory-format-menu [signed]
2461 '(menu-item "Signed Decimal" gdb-memory-format-signed
2462 :button (:radio . (equal gdb-memory-format "d"))))
2463 (define-key gdb-memory-format-menu [hexadecimal]
2464 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2465 :button (:radio . (equal gdb-memory-format "x"))))
2467 (defun gdb-memory-format-menu (event)
2469 (x-popup-menu event gdb-memory-format-menu))
2471 (defun gdb-memory-format-menu-1 (event)
2473 (save-selected-window
2474 (select-window (posn-window (event-start event)))
2475 (let* ((selection (gdb-memory-format-menu event))
2476 (binding (and selection (lookup-key gdb-memory-format-menu
2477 (vector (car selection))))))
2478 (if binding (call-interactively binding)))))
2480 (defun gdb-memory-unit-giant ()
2481 "Set the unit size to giant words (eight bytes)."
2483 (customize-set-variable 'gdb-memory-unit "g")
2484 (gdb-invalidate-memory))
2486 (defun gdb-memory-unit-word ()
2487 "Set the unit size to words (four bytes)."
2489 (customize-set-variable 'gdb-memory-unit "w")
2490 (gdb-invalidate-memory))
2492 (defun gdb-memory-unit-halfword ()
2493 "Set the unit size to halfwords (two bytes)."
2495 (customize-set-variable 'gdb-memory-unit "h")
2496 (gdb-invalidate-memory))
2498 (defun gdb-memory-unit-byte ()
2499 "Set the unit size to bytes."
2501 (customize-set-variable 'gdb-memory-unit "b")
2502 (gdb-invalidate-memory))
2504 (defvar gdb-memory-unit-map
2505 (let ((map (make-sparse-keymap)))
2506 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2508 "Keymap to select units in the header line.")
2510 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2511 "Menu of units in the header line.")
2513 (define-key gdb-memory-unit-menu [giantwords]
2514 '(menu-item "Giant words" gdb-memory-unit-giant
2515 :button (:radio . (equal gdb-memory-unit "g"))))
2516 (define-key gdb-memory-unit-menu [words]
2517 '(menu-item "Words" gdb-memory-unit-word
2518 :button (:radio . (equal gdb-memory-unit "w"))))
2519 (define-key gdb-memory-unit-menu [halfwords]
2520 '(menu-item "Halfwords" gdb-memory-unit-halfword
2521 :button (:radio . (equal gdb-memory-unit "h"))))
2522 (define-key gdb-memory-unit-menu [bytes]
2523 '(menu-item "Bytes" gdb-memory-unit-byte
2524 :button (:radio . (equal gdb-memory-unit "b"))))
2526 (defun gdb-memory-unit-menu (event)
2528 (x-popup-menu event gdb-memory-unit-menu))
2530 (defun gdb-memory-unit-menu-1 (event)
2532 (save-selected-window
2533 (select-window (posn-window (event-start event)))
2534 (let* ((selection (gdb-memory-unit-menu event))
2535 (binding (and selection (lookup-key gdb-memory-unit-menu
2536 (vector (car selection))))))
2537 (if binding (call-interactively binding)))))
2539 ;;from make-mode-line-mouse-map
2540 (defun gdb-make-header-line-mouse-map (mouse function) "\
2541 Return a keymap with single entry for mouse key MOUSE on the header line.
2542 MOUSE is defined to run function FUNCTION with no args in the buffer
2543 corresponding to the mode line clicked."
2544 (let ((map (make-sparse-keymap)))
2545 (define-key map (vector 'header-line mouse) function)
2546 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2549 (defvar gdb-memory-font-lock-keywords
2550 '(;; <__function.name+n>
2551 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2553 "Font lock keywords used in `gdb-memory-mode'.")
2555 (defun gdb-memory-mode ()
2556 "Major mode for examining memory.
2558 \\{gdb-memory-mode-map}"
2559 (kill-all-local-variables)
2560 (setq major-mode 'gdb-memory-mode)
2561 (setq mode-name "Memory")
2562 (setq buffer-read-only t)
2563 (use-local-map gdb-memory-mode-map)
2564 (setq header-line-format
2570 'face font-lock-warning-face
2571 'help-echo "mouse-1: Decrement address"
2572 'mouse-face 'mode-line-highlight
2574 (gdb-make-header-line-mouse-map
2576 (lambda () (interactive)
2577 (let ((gdb-memory-address
2578 ;; Let GDB do the arithmetic.
2580 gdb-memory-address " - "
2582 (* gdb-memory-repeat-count
2583 (cond ((string= gdb-memory-unit "b") 1)
2584 ((string= gdb-memory-unit "h") 2)
2585 ((string= gdb-memory-unit "w") 4)
2586 ((string= gdb-memory-unit "g") 8)))))))
2587 (gdb-invalidate-memory)))))
2590 'face font-lock-warning-face
2591 'help-echo "mouse-1: Increment address"
2592 'mouse-face 'mode-line-highlight
2593 'local-map (gdb-make-header-line-mouse-map
2595 (lambda () (interactive)
2596 (let ((gdb-memory-address nil))
2597 (gdb-invalidate-memory)))))
2599 (propertize gdb-memory-address
2600 'face font-lock-warning-face
2601 'help-echo "mouse-1: Set memory address"
2602 'mouse-face 'mode-line-highlight
2603 'local-map (gdb-make-header-line-mouse-map
2605 #'gdb-memory-set-address))
2607 (propertize (number-to-string gdb-memory-repeat-count)
2608 'face font-lock-warning-face
2609 'help-echo "mouse-1: Set repeat count"
2610 'mouse-face 'mode-line-highlight
2611 'local-map (gdb-make-header-line-mouse-map
2613 #'gdb-memory-set-repeat-count))
2615 (propertize gdb-memory-format
2616 'face font-lock-warning-face
2617 'help-echo "mouse-3: Select display format"
2618 'mouse-face 'mode-line-highlight
2619 'local-map gdb-memory-format-map)
2621 (propertize gdb-memory-unit
2622 'face font-lock-warning-face
2623 'help-echo "mouse-3: Select unit size"
2624 'mouse-face 'mode-line-highlight
2625 'local-map gdb-memory-unit-map))))
2626 (set (make-local-variable 'font-lock-defaults)
2627 '(gdb-memory-font-lock-keywords))
2628 (run-mode-hooks 'gdb-memory-mode-hook)
2629 'gdb-invalidate-memory)
2631 (defun gdb-memory-buffer-name ()
2632 (with-current-buffer gud-comint-buffer
2633 (concat "*memory of " (gdb-get-target-string) "*")))
2635 (defun gdb-display-memory-buffer ()
2636 "Display memory contents."
2639 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2641 (defun gdb-frame-memory-buffer ()
2642 "Display memory contents in a new frame."
2644 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2645 (special-display-frame-alist
2646 (cons '(left-fringe . 0)
2647 (cons '(right-fringe . 0)
2648 (cons '(width . 83) gdb-frame-parameters)))))
2649 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2654 (gdb-set-buffer-rules 'gdb-locals-buffer
2655 'gdb-locals-buffer-name
2658 (def-gdb-auto-update-trigger gdb-invalidate-locals
2659 (gdb-get-buffer 'gdb-locals-buffer)
2660 "server info locals\n"
2661 gdb-info-locals-handler)
2663 (defvar gdb-locals-watch-map
2664 (let ((map (make-sparse-keymap)))
2665 (suppress-keymap map)
2666 (define-key map "\r" (lambda () (interactive)
2669 (define-key map [mouse-2] (lambda (event) (interactive "e")
2670 (mouse-set-point event)
2674 "Keymap to create watch expression of a complex data type local variable.")
2676 (defconst gdb-struct-string
2677 (concat (propertize "[struct/union]"
2678 'mouse-face 'highlight
2679 'help-echo "mouse-2: create watch expression"
2680 'local-map gdb-locals-watch-map) "\n"))
2682 (defconst gdb-array-string
2683 (concat " " (propertize "[array]"
2684 'mouse-face 'highlight
2685 'help-echo "mouse-2: create watch expression"
2686 'local-map gdb-locals-watch-map) "\n"))
2688 ;; Abbreviate for arrays and structures.
2689 ;; These can be expanded using gud-display.
2690 (defun gdb-info-locals-handler ()
2691 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2692 gdb-pending-triggers))
2693 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2694 (with-current-buffer buf
2695 (goto-char (point-min))
2696 (while (re-search-forward "^[ }].*\n" nil t)
2697 (replace-match "" nil nil))
2698 (goto-char (point-min))
2699 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2700 (replace-match gdb-struct-string nil nil))
2701 (goto-char (point-min))
2702 (while (re-search-forward "\\s-*{.*\n" nil t)
2703 (replace-match gdb-array-string nil nil))))
2704 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2706 (with-current-buffer buf
2707 (let* ((window (get-buffer-window buf 0))
2708 (start (window-start window))
2709 (p (window-point window))
2710 (buffer-read-only nil))
2712 (insert-buffer-substring (gdb-get-buffer-create
2713 'gdb-partial-output-buffer))
2714 (set-window-start window start)
2715 (set-window-point window p))
2717 (run-hooks 'gdb-info-locals-hook))
2719 (defvar gdb-locals-mode-map
2720 (let ((map (make-sparse-keymap)))
2721 (suppress-keymap map)
2722 (define-key map "q" 'kill-this-buffer)
2725 (defun gdb-locals-mode ()
2726 "Major mode for gdb locals.
2728 \\{gdb-locals-mode-map}"
2729 (kill-all-local-variables)
2730 (setq major-mode 'gdb-locals-mode)
2731 (setq mode-name (concat "Locals:" gdb-selected-frame))
2732 (setq buffer-read-only t)
2733 (use-local-map gdb-locals-mode-map)
2734 (set (make-local-variable 'font-lock-defaults)
2735 '(gdb-locals-font-lock-keywords))
2736 (run-mode-hooks 'gdb-locals-mode-hook)
2737 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2738 (string-equal gdb-version "pre-6.4"))
2739 'gdb-invalidate-locals
2740 'gdb-invalidate-locals-1))
2742 (defun gdb-locals-buffer-name ()
2743 (with-current-buffer gud-comint-buffer
2744 (concat "*locals of " (gdb-get-target-string) "*")))
2746 (defun gdb-display-locals-buffer ()
2747 "Display local variables of current stack and their values."
2750 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2752 (defun gdb-frame-locals-buffer ()
2753 "Display local variables of current stack and their values in a new frame."
2755 (let ((special-display-regexps (append special-display-regexps '(".*")))
2756 (special-display-frame-alist gdb-frame-parameters))
2757 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2760 ;;;; Window management
2761 (defun gdb-display-buffer (buf dedicated &optional size)
2762 (let ((answer (get-buffer-window buf 0))
2765 (display-buffer buf nil 0) ;Raise the frame if necessary.
2766 ;; The buffer is not yet displayed.
2767 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2768 (let ((window (get-lru-window)))
2770 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2771 ,gdb-source-window))))
2773 (set-window-buffer window buf)
2774 (setq answer window))
2775 (setq must-split t)))
2777 (let* ((largest (get-largest-window))
2778 (cur-size (window-height largest))
2779 (new-size (and size (< size cur-size) (- cur-size size))))
2780 (setq answer (split-window largest new-size))
2781 (set-window-buffer answer buf)
2782 (set-window-dedicated-p answer dedicated)))
2786 ;;; Shared keymap initialization:
2788 (let ((menu (make-sparse-keymap "GDB-Windows")))
2789 (define-key gud-menu-map [displays]
2790 `(menu-item "GDB-Windows" ,menu
2791 :visible (memq gud-minor-mode '(gdbmi gdba))))
2792 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2793 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2794 (define-key menu [inferior]
2795 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2796 :enable gdb-use-separate-io-buffer))
2797 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2798 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2799 (define-key menu [disassembly]
2800 '("Disassembly" . gdb-display-assembler-buffer))
2801 (define-key menu [breakpoints]
2802 '("Breakpoints" . gdb-display-breakpoints-buffer))
2803 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2804 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2806 (let ((menu (make-sparse-keymap "GDB-Frames")))
2807 (define-key gud-menu-map [frames]
2808 `(menu-item "GDB-Frames" ,menu
2809 :visible (memq gud-minor-mode '(gdbmi gdba))))
2810 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2811 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2812 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2813 (define-key menu [inferior]
2814 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2815 :enable gdb-use-separate-io-buffer))
2816 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2817 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2818 (define-key menu [breakpoints]
2819 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2820 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2821 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2823 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2824 (define-key gud-menu-map [ui]
2825 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2826 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2827 (define-key menu [gdb-find-source-frame]
2828 '(menu-item "Look For Source Frame" gdb-find-source-frame
2829 :visible (eq gud-minor-mode 'gdba)
2830 :help "Toggle look for source frame."
2831 :button (:toggle . gdb-find-source-frame)))
2832 (define-key menu [gdb-use-separate-io]
2833 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2834 :visible (eq gud-minor-mode 'gdba)
2835 :help "Toggle separate IO for debugged program."
2836 :button (:toggle . gdb-use-separate-io-buffer)))
2837 (define-key menu [gdb-many-windows]
2838 '(menu-item "Display Other Windows" gdb-many-windows
2839 :help "Toggle display of locals, stack and breakpoint information"
2840 :button (:toggle . gdb-many-windows)))
2841 (define-key menu [gdb-restore-windows]
2842 '(menu-item "Restore Window Layout" gdb-restore-windows
2843 :help "Restore standard layout for debug session.")))
2845 (defun gdb-frame-gdb-buffer ()
2846 "Display GUD buffer in a new frame."
2848 (let ((special-display-regexps (append special-display-regexps '(".*")))
2849 (special-display-frame-alist
2850 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2851 gdb-frame-parameters)))
2852 (same-window-regexps nil))
2853 (display-buffer gud-comint-buffer)))
2855 (defun gdb-display-gdb-buffer ()
2856 "Display GUD buffer."
2858 (let ((same-window-regexps nil))
2859 (pop-to-buffer gud-comint-buffer)))
2861 (defun gdb-set-window-buffer (name)
2862 (set-window-buffer (selected-window) (get-buffer name))
2863 (set-window-dedicated-p (selected-window) t))
2865 (defun gdb-setup-windows ()
2866 "Layout the window pattern for `gdb-many-windows'."
2867 (gdb-display-locals-buffer)
2868 (gdb-display-stack-buffer)
2869 (delete-other-windows)
2870 (gdb-display-breakpoints-buffer)
2871 (delete-other-windows)
2873 (pop-to-buffer gud-comint-buffer)
2874 (split-window nil ( / ( * (window-height) 3) 4))
2875 (split-window nil ( / (window-height) 3))
2876 (split-window-horizontally)
2878 (gdb-set-window-buffer (gdb-locals-buffer-name))
2881 (if gud-last-last-frame
2882 (gud-find-file (car gud-last-last-frame))
2884 (gud-find-file gdb-main-file)
2885 ;; Put buffer list in window if we
2886 ;; can't find a source file.
2887 (list-buffers-noselect))))
2888 (setq gdb-source-window (selected-window))
2889 (when gdb-use-separate-io-buffer
2890 (split-window-horizontally)
2892 (gdb-set-window-buffer
2893 (gdb-get-buffer-create 'gdb-inferior-io)))
2895 (gdb-set-window-buffer (gdb-stack-buffer-name))
2896 (split-window-horizontally)
2898 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2901 (defun gdb-restore-windows ()
2902 "Restore the basic arrangement of windows used by gdba.
2903 This arrangement depends on the value of `gdb-many-windows'."
2905 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2906 (delete-other-windows)
2907 (if gdb-many-windows
2909 (when (or gud-last-last-frame gdb-show-main)
2913 (if gud-last-last-frame
2914 (gud-find-file (car gud-last-last-frame))
2915 (gud-find-file gdb-main-file)))
2916 (setq gdb-source-window (selected-window))
2920 "Exit a debugging session cleanly.
2921 Kills the gdb buffers, and resets variables and the source buffers."
2922 (dolist (buffer (buffer-list))
2923 (unless (eq buffer gud-comint-buffer)
2924 (with-current-buffer buffer
2925 (if (memq gud-minor-mode '(gdbmi gdba))
2926 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2928 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2929 (setq gud-minor-mode nil)
2930 (kill-local-variable 'tool-bar-map)
2931 (kill-local-variable 'gdb-define-alist))))))
2932 (setq gdb-overlay-arrow-position nil)
2933 (setq overlay-arrow-variable-list
2934 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2935 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2936 (setq gdb-stack-position nil)
2937 (setq overlay-arrow-variable-list
2938 (delq 'gdb-stack-position overlay-arrow-variable-list))
2939 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2940 (setq gud-running nil)
2941 (setq gdb-active-process nil)
2942 (setq gdb-var-list nil)
2943 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2945 (defun gdb-source-info ()
2946 "Find the source file where the program starts and displays it with related
2948 (goto-char (point-min))
2949 (if (and (search-forward "Located in " nil t)
2950 (looking-at "\\S-+"))
2951 (setq gdb-main-file (match-string 0)))
2952 (goto-char (point-min))
2953 (if (search-forward "Includes preprocessor macro info." nil t)
2954 (setq gdb-macro-info t))
2955 (if gdb-many-windows
2957 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2959 (let ((pop-up-windows t))
2960 (display-buffer (gud-find-file gdb-main-file))))))
2962 (defun gdb-get-location (bptno line flag)
2963 "Find the directory containing the relevant source file.
2964 Put in buffer and place breakpoint icon."
2965 (goto-char (point-min))
2966 (catch 'file-not-found
2967 (if (search-forward "Located in " nil t)
2968 (when (looking-at "\\S-+")
2969 (delete (cons bptno "File not found") gdb-location-alist)
2970 (push (cons bptno (match-string 0)) gdb-location-alist))
2972 (unless (assoc bptno gdb-location-alist)
2973 (push (cons bptno "File not found") gdb-location-alist)
2974 (message-box "Cannot find source file for breakpoint location.\n\
2975 Add directory to search path for source files using the GDB command, dir."))
2976 (throw 'file-not-found nil))
2977 (with-current-buffer
2978 (find-file-noselect (match-string 0))
2979 (save-current-buffer
2980 (set (make-local-variable 'gud-minor-mode) 'gdba)
2981 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2982 ;; only want one breakpoint icon at each location
2984 (goto-line (string-to-number line))
2985 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2987 (add-hook 'find-file-hook 'gdb-find-file-hook)
2989 (defun gdb-find-file-hook ()
2990 "Set up buffer for debugging if file is part of the source code
2991 of the current session."
2992 (if (and (buffer-name gud-comint-buffer)
2993 ;; in case gud or gdb-ui is just loaded
2995 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2997 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2998 (if (member (if (string-equal gdb-version "pre-6.4")
2999 (file-name-nondirectory buffer-file-name)
3001 gdb-source-file-list)
3002 (with-current-buffer (find-buffer-visiting buffer-file-name)
3003 (set (make-local-variable 'gud-minor-mode)
3004 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3005 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3008 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3009 "Put string PUTSTRING in front of POS in the current buffer.
3010 PUTSTRING is displayed by putting an overlay into the current buffer with a
3011 `before-string' string that has a `display' property whose value is
3013 (let ((string (make-string 1 ?x))
3014 (buffer (current-buffer)))
3015 (setq putstring (copy-sequence putstring))
3016 (let ((overlay (make-overlay pos pos buffer))
3018 (list (list 'margin 'left-margin) putstring))))
3019 (put-text-property 0 1 'display prop string)
3021 (add-text-properties 0 1 sprops string))
3022 (overlay-put overlay 'put-break t)
3023 (overlay-put overlay 'before-string string))))
3025 ;;from remove-images
3026 (defun gdb-remove-strings (start end &optional buffer)
3027 "Remove strings between START and END in BUFFER.
3028 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3029 BUFFER nil or omitted means use the current buffer."
3031 (setq buffer (current-buffer)))
3032 (dolist (overlay (overlays-in start end))
3033 (when (overlay-get overlay 'put-break)
3034 (delete-overlay overlay))))
3036 (defun gdb-put-breakpoint-icon (enabled bptno)
3037 (let ((start (- (line-beginning-position) 1))
3038 (end (+ (line-end-position) 1))
3039 (putstring (if enabled "B" "b"))
3040 (source-window (get-buffer-window (current-buffer) 0)))
3041 (add-text-properties
3042 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3045 (add-text-properties
3046 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3047 (add-text-properties
3048 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3049 (gdb-remove-breakpoint-icons start end)
3050 (if (display-images-p)
3051 (if (>= (or left-fringe-width
3052 (if source-window (car (window-fringes source-window)))
3053 gdb-buffer-fringe-width) 8)
3056 `(left-fringe breakpoint
3059 'breakpoint-disabled))
3061 'gdb-enabled enabled)
3062 (when (< left-margin-width 2)
3063 (save-current-buffer
3064 (setq left-margin-width 2)
3068 left-margin-width right-margin-width))))
3071 (or breakpoint-enabled-icon
3072 (setq breakpoint-enabled-icon
3073 (find-image `((:type xpm :data
3074 ,breakpoint-xpm-data
3075 :ascent 100 :pointer hand)
3077 ,breakpoint-enabled-pbm-data
3078 :ascent 100 :pointer hand)))))
3079 (or breakpoint-disabled-icon
3080 (setq breakpoint-disabled-icon
3081 (find-image `((:type xpm :data
3082 ,breakpoint-xpm-data
3083 :conversion disabled
3084 :ascent 100 :pointer hand)
3086 ,breakpoint-disabled-pbm-data
3087 :ascent 100 :pointer hand))))))
3091 (when (< left-margin-width 2)
3092 (save-current-buffer
3093 (setq left-margin-width 2)
3094 (let ((window (get-buffer-window (current-buffer) 0)))
3097 window left-margin-width right-margin-width)))))
3099 (propertize putstring
3100 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3103 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3104 (gdb-remove-strings start end)
3105 (if (display-images-p)
3106 (remove-images start end))
3108 (setq left-margin-width 0)
3109 (let ((window (get-buffer-window (current-buffer) 0)))
3112 window left-margin-width right-margin-width)))))
3116 ;; Assembler buffer.
3118 (gdb-set-buffer-rules 'gdb-assembler-buffer
3119 'gdb-assembler-buffer-name
3120 'gdb-assembler-mode)
3122 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3123 ;; window-start but keep the overlay arrow/current line visible.
3124 (defun gdb-assembler-handler ()
3125 (setq gdb-pending-triggers
3126 (delq 'gdb-invalidate-assembler
3127 gdb-pending-triggers))
3128 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3130 (with-current-buffer buf
3131 (let* ((window (get-buffer-window buf 0))
3132 (p (window-point window))
3133 (buffer-read-only nil))
3135 (insert-buffer-substring (gdb-get-buffer-create
3136 'gdb-partial-output-buffer))
3137 (set-window-point window p)))))
3138 ;; put customisation here
3139 (gdb-assembler-custom))
3141 (defun gdb-assembler-custom ()
3142 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3143 (pos 1) (address) (flag) (bptno))
3144 (with-current-buffer buffer
3146 (if (not (equal gdb-pc-address "main"))
3148 (goto-char (point-min))
3149 (if (and gdb-pc-address
3150 (search-forward gdb-pc-address nil t))
3154 (setq fringe-indicator-alist
3155 (if (string-equal gdb-frame-number "0")
3157 '((overlay-arrow . hollow-right-triangle))))
3158 (or gdb-overlay-arrow-position
3159 (setq gdb-overlay-arrow-position (make-marker)))
3160 (set-marker gdb-overlay-arrow-position (point))))))
3161 ;; remove all breakpoint-icons in assembler buffer before updating.
3162 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3163 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3164 (goto-char (point-min))
3165 (while (< (point) (- (point-max) 1))
3167 (if (looking-at "[^\t].*?breakpoint")
3170 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3171 (setq bptno (match-string 1))
3172 (setq flag (char-after (match-beginning 2)))
3173 (setq address (match-string 3))
3174 (with-current-buffer buffer
3176 (goto-char (point-min))
3177 (if (search-forward address nil t)
3178 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3179 (if (not (equal gdb-pc-address "main"))
3180 (with-current-buffer buffer
3181 (set-window-point (get-buffer-window buffer 0) pos)))))
3183 (defvar gdb-assembler-mode-map
3184 (let ((map (make-sparse-keymap)))
3185 (suppress-keymap map)
3186 (define-key map "q" 'kill-this-buffer)
3189 (defvar gdb-assembler-font-lock-keywords
3190 '(;; <__function.name+n>
3191 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3192 (1 font-lock-function-name-face))
3193 ;; 0xNNNNNNNN <__function.name+n>: opcode
3194 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3195 (4 font-lock-keyword-face))
3196 ;; %register(at least i386)
3197 ("%\\sw+" . font-lock-variable-name-face)
3198 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3199 (1 font-lock-comment-face)
3200 (2 font-lock-function-name-face))
3201 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3202 "Font lock keywords used in `gdb-assembler-mode'.")
3204 (defun gdb-assembler-mode ()
3205 "Major mode for viewing code assembler.
3207 \\{gdb-assembler-mode-map}"
3208 (kill-all-local-variables)
3209 (setq major-mode 'gdb-assembler-mode)
3210 (setq mode-name (concat "Machine:" gdb-selected-frame))
3211 (setq gdb-overlay-arrow-position nil)
3212 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3213 (setq fringes-outside-margins t)
3214 (setq buffer-read-only t)
3215 (use-local-map gdb-assembler-mode-map)
3216 (gdb-invalidate-assembler)
3217 (set (make-local-variable 'font-lock-defaults)
3218 '(gdb-assembler-font-lock-keywords))
3219 (run-mode-hooks 'gdb-assembler-mode-hook)
3220 'gdb-invalidate-assembler)
3222 (defun gdb-assembler-buffer-name ()
3223 (with-current-buffer gud-comint-buffer
3224 (concat "*disassembly of " (gdb-get-target-string) "*")))
3226 (defun gdb-display-assembler-buffer ()
3227 "Display disassembly view."
3229 (setq gdb-previous-frame nil)
3231 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3233 (defun gdb-frame-assembler-buffer ()
3234 "Display disassembly view in a new frame."
3236 (setq gdb-previous-frame nil)
3237 (let ((special-display-regexps (append special-display-regexps '(".*")))
3238 (special-display-frame-alist gdb-frame-parameters))
3239 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3241 ;; modified because if gdb-pc-address has changed value a new command
3242 ;; must be enqueued to update the buffer with the new output
3243 (defun gdb-invalidate-assembler (&optional ignored)
3244 (if (gdb-get-buffer 'gdb-assembler-buffer)
3246 (unless (and gdb-selected-frame
3247 (string-equal gdb-selected-frame gdb-previous-frame))
3248 (if (or (not (member 'gdb-invalidate-assembler
3249 gdb-pending-triggers))
3250 (not (string-equal gdb-pc-address
3251 gdb-previous-frame-address)))
3253 ;; take previous disassemble command, if any, off the queue
3254 (with-current-buffer gud-comint-buffer
3255 (let ((queue gdb-input-queue))
3256 (dolist (item queue)
3257 (if (equal (cdr item) '(gdb-assembler-handler))
3258 (setq gdb-input-queue
3259 (delete item gdb-input-queue))))))
3262 (concat gdb-server-prefix "disassemble "
3263 (if (member gdb-pc-address '(nil "main")) nil "0x")
3264 gdb-pc-address "\n")
3265 'gdb-assembler-handler))
3266 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3267 (setq gdb-previous-frame-address gdb-pc-address)
3268 (setq gdb-previous-frame gdb-selected-frame)))))))
3270 (defun gdb-get-selected-frame ()
3271 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3274 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3275 (push 'gdb-get-selected-frame
3276 gdb-pending-triggers))))
3278 (defun gdb-frame-handler ()
3279 (setq gdb-pending-triggers
3280 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3281 (goto-char (point-min))
3282 (when (re-search-forward
3283 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3284 (setq gdb-frame-number (match-string 1))
3285 (setq gdb-frame-address (match-string 2)))
3286 (goto-char (point-min))
3287 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3288 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3290 (setq gdb-selected-frame (match-string 2))
3291 (if (gdb-get-buffer 'gdb-locals-buffer)
3292 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3293 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3294 (if (gdb-get-buffer 'gdb-assembler-buffer)
3295 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3296 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3297 (setq gdb-pc-address (match-string 1))
3298 (if (and (match-string 3) gud-overlay-arrow-position)
3299 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3300 (position (marker-position gud-overlay-arrow-position)))
3302 (string-equal (buffer-name buffer)
3303 (file-name-nondirectory (match-string 3))))
3304 (with-current-buffer buffer
3305 (setq fringe-indicator-alist
3306 (if (string-equal gdb-frame-number "0")
3308 '((overlay-arrow . hollow-right-triangle))))
3309 (set-marker gud-overlay-arrow-position position))))))
3310 (goto-char (point-min))
3311 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3312 (setq gdb-current-language (match-string 1)))
3313 (gdb-invalidate-assembler))
3316 ;; Code specific to GDB 6.4
3317 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3319 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3320 "Create list of source files for current GDB session.
3321 If buffers already exist for any of these files, gud-minor-mode
3323 (goto-char (point-min))
3324 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3325 (push (match-string 1) gdb-source-file-list))
3326 (dolist (buffer (buffer-list))
3327 (with-current-buffer buffer
3328 (when (member buffer-file-name gdb-source-file-list)
3329 (set (make-local-variable 'gud-minor-mode)
3330 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3331 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3332 (when gud-tooltip-mode
3333 (make-local-variable 'gdb-define-alist)
3334 (gdb-create-define-alist)
3335 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3336 (gdb-force-mode-line-update
3337 (propertize "ready" 'face font-lock-variable-name-face)))
3339 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3340 (defun gdb-var-list-children-1 (varnum)
3343 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3344 (concat "server interpreter mi \"-var-list-children --all-values "
3346 (concat "-var-list-children --all-values " varnum "\n"))
3347 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3349 (defconst gdb-var-list-children-regexp-1
3350 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3351 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3352 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3354 (defun gdb-var-list-children-handler-1 (varnum)
3355 (goto-char (point-min))
3356 (let ((var-list nil))
3357 (catch 'child-already-watched
3358 (dolist (var gdb-var-list)
3359 (if (string-equal varnum (car var))
3362 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3363 (let ((varchild (list (match-string 1)
3367 (read (match-string 4))
3369 (if (assoc (car varchild) gdb-var-list)
3370 (throw 'child-already-watched nil))
3371 (push varchild var-list))))
3372 (push var var-list)))
3373 (setq gdb-var-list (nreverse var-list))))
3374 (gdb-speedbar-update))
3376 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3377 (defun gdb-var-update-1 ()
3378 (if (not (member 'gdb-var-update gdb-pending-triggers))
3382 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3383 "server interpreter mi \"-var-update --all-values *\"\n"
3384 "-var-update --all-values *\n")
3385 'gdb-var-update-handler-1))
3386 (push 'gdb-var-update gdb-pending-triggers))))
3388 (defconst gdb-var-update-regexp-1
3389 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3390 in_scope=\"\\(.*?\\)\".*?}")
3392 (defun gdb-var-update-handler-1 ()
3393 (dolist (var gdb-var-list)
3394 (setcar (nthcdr 5 var) nil))
3395 (goto-char (point-min))
3396 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3397 (let* ((varnum (match-string 1))
3398 (var (assoc varnum gdb-var-list)))
3400 (if (string-equal (match-string 3) "false")
3401 (setcar (nthcdr 5 var) 'out-of-scope)
3402 (setcar (nthcdr 5 var) 'changed)
3403 (setcar (nthcdr 4 var)
3404 (read (match-string 2)))))))
3405 (setq gdb-pending-triggers
3406 (delq 'gdb-var-update gdb-pending-triggers))
3407 (gdb-speedbar-update))
3409 ;; Registers buffer.
3411 (gdb-set-buffer-rules 'gdb-registers-buffer
3412 'gdb-registers-buffer-name
3413 'gdb-registers-mode)
3415 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3416 (gdb-get-buffer 'gdb-registers-buffer)
3417 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3418 "server interpreter mi \"-data-list-register-values x\"\n"
3419 "-data-list-register-values x\n")
3420 gdb-data-list-register-values-handler)
3422 (defconst gdb-data-list-register-values-regexp
3423 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3425 (defun gdb-data-list-register-values-handler ()
3426 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3427 gdb-pending-triggers))
3428 (goto-char (point-min))
3429 (if (re-search-forward gdb-error-regexp nil t)
3430 (let ((err (match-string 1)))
3431 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3432 (let ((buffer-read-only nil))
3434 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3436 (goto-char (point-min)))))
3437 (let ((register-list (reverse gdb-register-names))
3438 (register nil) (register-string nil) (register-values nil))
3439 (goto-char (point-min))
3440 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3441 (setq register (pop register-list))
3442 (setq register-string (concat register "\t" (match-string 2) "\n"))
3443 (if (member (match-string 1) gdb-changed-registers)
3444 (put-text-property 0 (length register-string)
3445 'face 'font-lock-warning-face
3447 (setq register-values
3448 (concat register-values register-string)))
3449 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3450 (with-current-buffer buf
3451 (let* ((window (get-buffer-window buf 0))
3452 (start (window-start window))
3453 (p (window-point window))
3454 (buffer-read-only nil))
3456 (insert register-values)
3457 (set-window-start window start)
3458 (set-window-point window p))))))
3459 (gdb-data-list-register-values-custom))
3461 (defun gdb-data-list-register-values-custom ()
3462 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3464 (let ((buffer-read-only nil)
3466 (goto-char (point-min))
3467 (while (< (point) (point-max))
3468 (setq start (line-beginning-position))
3469 (setq end (line-end-position))
3470 (when (looking-at "^[^\t]+")
3471 (unless (string-equal (match-string 0) "No registers.")
3472 (put-text-property start (match-end 0)
3473 'face font-lock-variable-name-face)
3474 (add-text-properties start end
3475 '(help-echo "mouse-2: edit value"
3476 mouse-face highlight))))
3477 (forward-line 1))))))
3479 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3480 (defun gdb-get-changed-registers ()
3481 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3482 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3486 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3487 "server interpreter mi -data-list-changed-registers\n"
3488 "-data-list-changed-registers\n")
3489 'gdb-get-changed-registers-handler))
3490 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3492 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3494 (defun gdb-get-changed-registers-handler ()
3495 (setq gdb-pending-triggers
3496 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3497 (setq gdb-changed-registers nil)
3498 (goto-char (point-min))
3499 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3500 (push (match-string 1) gdb-changed-registers)))
3505 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3506 (gdb-set-buffer-rules 'gdb-locals-buffer
3507 'gdb-locals-buffer-name
3510 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3511 (gdb-get-buffer 'gdb-locals-buffer)
3512 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3513 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3514 "-stack-list-locals --simple-values\n")
3515 gdb-stack-list-locals-handler)
3517 (defconst gdb-stack-list-locals-regexp
3518 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3520 (defvar gdb-locals-watch-map-1
3521 (let ((map (make-sparse-keymap)))
3522 (suppress-keymap map)
3523 (define-key map "\r" 'gud-watch)
3524 (define-key map [mouse-2] 'gud-watch)
3526 "Keymap to create watch expression of a complex data type local variable.")
3528 (defvar gdb-edit-locals-map-1
3529 (let ((map (make-sparse-keymap)))
3530 (suppress-keymap map)
3531 (define-key map "\r" 'gdb-edit-locals-value)
3532 (define-key map [mouse-2] 'gdb-edit-locals-value)
3534 "Keymap to edit value of a simple data type local variable.")
3536 (defun gdb-edit-locals-value (&optional event)
3537 "Assign a value to a variable displayed in the locals buffer."
3538 (interactive (list last-input-event))
3540 (if event (posn-set-point (event-end event)))
3542 (let* ((var (current-word))
3543 (value (read-string (format "New value (%s): " var))))
3545 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3548 ;; Dont display values of arrays or structures.
3549 ;; These can be expanded using gud-watch.
3550 (defun gdb-stack-list-locals-handler ()
3551 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3552 gdb-pending-triggers))
3553 (goto-char (point-min))
3554 (if (re-search-forward gdb-error-regexp nil t)
3555 (let ((err (match-string 1)))
3556 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3557 (let ((buffer-read-only nil))
3560 (goto-char (point-min)))))
3561 (let (local locals-list)
3562 (goto-char (point-min))
3563 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3564 (let ((local (list (match-string 1)
3567 (if (looking-at ",value=\\(\".*\"\\).*?}")
3568 (setcar (nthcdr 2 local) (read (match-string 1))))
3569 (push local locals-list)))
3570 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3571 (and buf (with-current-buffer buf
3572 (let* ((window (get-buffer-window buf 0))
3573 (start (window-start window))
3574 (p (window-point window))
3575 (buffer-read-only nil) (name) (value))
3577 (dolist (local locals-list)
3578 (setq name (car local))
3579 (setq value (nth 2 local))
3581 (string-match "^\\0x" value))
3582 (add-text-properties 0 (length name)
3583 `(mouse-face highlight
3584 help-echo "mouse-2: create watch expression"
3585 local-map ,gdb-locals-watch-map-1)
3587 (add-text-properties 0 (length value)
3588 `(mouse-face highlight
3589 help-echo "mouse-2: edit value"
3590 local-map ,gdb-edit-locals-map-1)
3593 (concat name "\t" (nth 1 local)
3595 (set-window-start window start)
3596 (set-window-point window p))))))))
3598 (defun gdb-get-register-names ()
3599 "Create a list of register names."
3600 (goto-char (point-min))
3601 (setq gdb-register-names nil)
3602 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3603 (push (match-string 1) gdb-register-names)))
3607 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3608 ;;; gdb-ui.el ends here