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, 2007, 2008
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 3, 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.
39 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
40 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
41 ;; You don't need to know about annotations to use this mode as a debugger,
42 ;; but if you are interested developing the mode itself, see the Annotations
43 ;; section in the GDB info manual.
45 ;; GDB developers plan to make the annotation interface obsolete. A new
46 ;; interface called GDB/MI (machine interface) has been designed to replace it.
47 ;; Some GDB/MI commands are used in this file through the CLI command
48 ;; 'interpreter mi <mi-command>'. To help with the process of fully migrating
49 ;; Emacs from annotations to GDB/MI, there is an experimental package called
50 ;; gdb-mi in the Emacs Lisp Package Archive ("http://tromey.com/elpa/"). It
51 ;; comprises of modified gud.el and a file called gdb-mi.el which replaces
52 ;; gdb-ui.el. When installed, this overrides the current files and invoking
53 ;; M-x gdb will use GDB/MI directly (starts with "gdb -i=mi"). When deleted
54 ;; ('d' followed by 'x' in Package Menu mode), the files are deleted and old
55 ;; functionality restored. This provides a convenient way to review the
56 ;; current status/contribute to its improvement. For someone who just wants to
57 ;; use GDB, however, the current mode in Emacs 22 is a much better option.
58 ;; There is also a file, also called gdb-mi.el, a version of which is included
59 ;; the GDB distribution. This will probably only work with versions
60 ;; distributed with GDB 6.5 or later. Unlike the version in ELPA it works on
61 ;; top of gdb-ui.el and you can only start it with M-x gdbmi.
63 ;; This mode SHOULD WORK WITH GDB 5.0 or later but you will NEED AT LEAST
64 ;; GDB 6.0 to use watch expressions. It works best with GDB 6.4 or later
65 ;; where watch expressions will update more quickly.
67 ;;; Windows Platforms:
69 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
70 ;; explicitly in your program if you want timely display of I/O in Emacs.
71 ;; Alternatively you can make the output stream unbuffered, for example, by
75 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
78 ;; and compiling with -DUNBUFFERED while debugging.
80 ;; If you are using Cygwin GDB and find that the source is not being displayed
81 ;; in Emacs when you step through it, possible solutions are to:
83 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
84 ;; (Since 22.1 Emacs builds under Cygwin.)
85 ;; 2) Use MinGW GDB instead.
86 ;; 3) Use cygwin-mount.el
90 ;; 1) Cannot handle multiple debug sessions.
91 ;; 2) If you wish to call procedures from your program in GDB
92 ;; e.g "call myproc ()", "p mysquare (5)" then use level 2 annotations
93 ;; "gdb --annotate=2 myprog" to keep source buffer/selected frame fixed.
94 ;; 3) After detaching from a process, clicking on the "GO" icon on toolbar
95 ;; (gud-go) sends "continue" to GDB (should be "run").
99 ;; 1) Use MI command -data-read-memory for memory window.
100 ;; 2) Use tree-buffer.el (from ECB) instead of the speedbar for
101 ;; watch-expressions? Handling of watch-expressions needs to be
102 ;; overhauled to work for large arrays/structures by creating variable
103 ;; objects for visible watch-expressions only.
104 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
110 (defvar tool-bar-map)
111 (defvar speedbar-initial-expansion-list-name)
113 (defvar gdb-pc-address nil "Initialization for Assembler buffer.
114 Set to \"main\" at start if `gdb-show-main' is t.")
115 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
116 (defvar gdb-previous-frame-pc-address nil)
117 (defvar gdb-memory-address "main")
118 (defvar gdb-previous-frame nil)
119 (defvar gdb-selected-frame nil)
120 (defvar gdb-frame-number nil)
121 (defvar gdb-current-language nil)
122 (defvar gdb-var-list nil
123 "List of variables in watch window.
124 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS FP)
125 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
126 address for root variables.")
127 (defvar gdb-main-file nil "Source file from which program execution begins.")
128 (defvar gud-old-arrow nil)
129 (defvar gdb-thread-indicator nil)
130 (defvar gdb-overlay-arrow-position nil)
131 (defvar gdb-stack-position nil)
132 (defvar gdb-server-prefix nil)
133 (defvar gdb-flush-pending-output nil)
134 (defvar gdb-location-alist nil
135 "Alist of breakpoint numbers and full filenames. Only used for files that
137 (defvar gdb-active-process nil
138 "GUD tooltips display variable values when t, and macro definitions otherwise.")
139 (defvar gdb-error "Non-nil when GDB is reporting an error.")
140 (defvar gdb-macro-info nil
141 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
142 (defvar gdb-buffer-fringe-width nil)
143 (defvar gdb-signalled nil)
144 (defvar gdb-source-window nil)
145 (defvar gdb-inferior-status nil)
146 (defvar gdb-continuation nil)
147 (defvar gdb-look-up-stack nil)
148 (defvar gdb-frame-begin nil
149 "Non-nil when GDB generates frame-begin annotation.")
150 (defvar gdb-printing t)
151 (defvar gdb-parent-bptno-enabled nil)
152 (defvar gdb-ready nil)
153 (defvar gdb-stack-update nil)
154 (defvar gdb-early-user-input nil)
156 (defvar gdb-buffer-type nil
157 "One of the symbols bound in `gdb-buffer-rules'.")
158 (make-variable-buffer-local 'gdb-buffer-type)
160 (defvar gdb-input-queue ()
161 "A list of gdb command objects.")
163 (defvar gdb-prompting nil
164 "True when gdb is idle with no pending input.")
166 (defvar gdb-output-sink nil
167 "The disposition of the output of the current gdb command.
168 Possible values are these symbols:
170 `user' -- gdb output should be copied to the GUD buffer
173 `inferior' -- gdb output should be copied to the inferior-io buffer.
175 `pre-emacs' -- output should be ignored util the post-prompt
176 annotation is received. Then the output-sink
178 `emacs' -- output should be collected in the partial-output-buffer
179 for subsequent processing by a command. This is the
180 disposition of output generated by commands that
181 gdb mode sends to gdb on its own behalf.
182 `post-emacs' -- ignore output until the prompt annotation is
183 received, then go to USER disposition.
185 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
186 \(`user' and `emacs').")
188 (defvar gdb-current-item nil
189 "The most recent command item sent to gdb.")
191 (defvar gdb-pending-triggers '()
192 "A list of trigger functions that have run later than their output handlers.")
194 (defvar gdb-first-post-prompt nil)
195 (defvar gdb-version nil)
196 (defvar gdb-locals-font-lock-keywords nil)
197 (defvar gdb-source-file-list nil
198 "List of source files for the current executable.")
199 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
201 (defvar gdb-locals-font-lock-keywords-1
202 '(;; var = (struct struct_tag) value
203 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
204 (1 font-lock-variable-name-face)
205 (3 font-lock-keyword-face)
206 (4 font-lock-type-face))
207 ;; var = (type) value
208 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
209 (1 font-lock-variable-name-face)
210 (3 font-lock-type-face))
212 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
213 (1 font-lock-variable-name-face)))
214 "Font lock keywords used in `gdb-local-mode'.")
216 (defvar gdb-locals-font-lock-keywords-2
217 '(;; var = type value
218 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
219 (1 font-lock-variable-name-face)
220 (3 font-lock-type-face)))
221 "Font lock keywords used in `gdb-local-mode'.")
223 ;; Variables for GDB 6.4+
224 (defvar gdb-register-names nil "List of register names.")
225 (defvar gdb-changed-registers nil
226 "List of changed register numbers (strings).")
229 (defun gdb (command-line)
230 "Run gdb on program FILE in buffer *gud-FILE*.
231 The directory containing FILE becomes the initial working
232 directory and source-file directory for your debugger.
234 If `gdb-many-windows' is nil (the default value) then gdb just
235 pops up the GUD buffer unless `gdb-show-main' is t. In this case
236 it starts with two windows: one displaying the GUD buffer and the
237 other with the source file with the main routine of the inferior.
239 If `gdb-many-windows' is t, regardless of the value of
240 `gdb-show-main', the layout below will appear unless
241 `gdb-use-separate-io-buffer' is nil when the source buffer
242 occupies the full width of the frame. Keybindings are shown in
245 Watch expressions appear in the speedbar/slowbar.
247 The following commands help control operation :
249 `gdb-many-windows' - Toggle the number of windows gdb uses.
250 `gdb-restore-windows' - To restore the window layout.
252 See Info node `(emacs)GDB Graphical Interface' for a more
253 detailed description of this mode.
255 +----------------------------------------------------------------------+
257 +-----------------------------------+----------------------------------+
258 | GUD buffer (I/O of GDB) | Locals buffer |
259 |-----------------------------------+----------------------------------+
261 | Source buffer | I/O buffer for debugged program |
263 |-----------------------------------+----------------------------------+
264 | Stack buffer | Breakpoints/threads buffer |
265 +-----------------------------------+----------------------------------+
267 To run GDB in text command mode, replace the GDB \"--annotate=3\"
268 option with \"--fullname\" either in the minibuffer for the
269 current Emacs session, or the custom variable
270 `gud-gdb-command-name' for all future sessions. You need to use
271 text command mode to debug multiple programs within one Emacs
273 (interactive (list (gud-query-cmdline 'gdb)))
275 (when (and gud-comint-buffer
276 (buffer-name gud-comint-buffer)
277 (get-buffer-process gud-comint-buffer)
278 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
279 (gdb-restore-windows)
281 "Multiple debugging requires restarting in text command mode"))
283 (gud-common-init command-line nil 'gud-gdba-marker-filter)
284 (set (make-local-variable 'gud-minor-mode) 'gdba)
285 (setq comint-input-sender 'gdb-send)
287 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
288 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
289 "Set temporary breakpoint at current line.")
290 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line.")
291 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
292 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
293 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
294 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
295 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
296 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
298 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
299 "\C-j" "Set execution address to current line.")
301 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
302 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
303 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
304 (gud-def gud-pstar "print* %e" nil
305 "Evaluate C dereferenced pointer expression at point.")
307 ;; For debugging Emacs only.
308 (gud-def gud-pv "pv1 %e" "\C-v" "Print the value of the lisp variable.")
310 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
311 (gud-def gud-run "run" nil "Run the program.")
313 (local-set-key "\C-i" 'gud-gdb-complete-command)
314 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
315 (setq paragraph-start comint-prompt-regexp)
316 (setq gdb-output-sink 'user)
317 (setq gdb-first-prompt t)
318 (setq gud-running nil)
320 (setq gdb-stack-update nil)
321 (setq gdb-flush-pending-output nil)
322 (setq gdb-early-user-input nil)
323 (setq gud-filter-pending-text nil)
324 (gdb-thread-identification)
325 (run-hooks 'gdb-mode-hook))
327 ;; Keep as an alias for compatibility with Emacs 22.1.
329 (defalias 'gdba 'gdb)
332 "Gdb Graphical Mode options specifically for running Gdb in Emacs."
336 (defcustom gdb-debug-log-max 128
337 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
339 :type '(choice (integer :tag "Number of elements")
340 (const :tag "Unlimited" nil))
343 (defvar gdb-debug-log nil
344 "List of commands sent to and replies received from GDB.
345 Most recent commands are listed first. This list stores only the last
346 `gdb-debug-log-max' values. This variable is used to debug GDB-UI.")
349 (defcustom gdb-enable-debug nil
350 "Non-nil means record the process input and output in `gdb-debug-log'."
355 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
356 "Shell command for generating a list of defined macros in a source file.
357 This list is used to display the #define directive associated
358 with an identifier as a tooltip. It works in a debug session with
359 GDB, when `gud-tooltip-mode' is t.
361 Set `gdb-cpp-define-alist-flags' for any include paths or
367 (defcustom gdb-cpp-define-alist-flags ""
368 "Preprocessor flags for `gdb-cpp-define-alist-program'."
373 (defcustom gdb-show-main nil
374 "Non-nil means display source file containing the main routine at startup.
375 Also display the main routine in the disassembly buffer if present."
380 (defcustom gdb-many-windows nil
381 "If nil, just pop up the GUD buffer unless `gdb-show-main' is t.
382 In this case start with two windows: one displaying the GUD
383 buffer and the other with the source file with the main routine
384 of the debugged program. Non-nil means display the layout shown
390 (defcustom gdb-use-separate-io-buffer nil
391 "Non-nil means display output from the debugged program in a separate buffer."
396 (defun gdb-force-mode-line-update (status)
397 (let ((buffer gud-comint-buffer))
398 (if (and buffer (buffer-name buffer))
399 (with-current-buffer buffer
400 (setq mode-line-process
402 (process-status (get-buffer-process buffer)) status))
403 ;; Force mode line redisplay soon.
404 (force-mode-line-update)))))
406 (defun gdb-many-windows (arg)
407 "Toggle the number of windows in the basic arrangement.
408 With prefix argument ARG, display additional buffers if ARG is positive,
409 otherwise use a single window."
411 (setq gdb-many-windows
413 (not gdb-many-windows)
414 (> (prefix-numeric-value arg) 0)))
415 (message (format "Display of other windows %sabled"
416 (if gdb-many-windows "en" "dis")))
417 (if (and gud-comint-buffer
418 (buffer-name gud-comint-buffer))
420 (gdb-restore-windows)
423 (defun gdb-use-separate-io-buffer (arg)
424 "Toggle separate IO for debugged program.
425 With prefix argument ARG, use separate IO if ARG is positive,
428 (setq gdb-use-separate-io-buffer
430 (not gdb-use-separate-io-buffer)
431 (> (prefix-numeric-value arg) 0)))
432 (message (format "Separate IO %sabled"
433 (if gdb-use-separate-io-buffer "en" "dis")))
434 (if (and gud-comint-buffer
435 (buffer-name gud-comint-buffer))
437 (if gdb-use-separate-io-buffer
438 (if gdb-many-windows (gdb-restore-windows))
439 (kill-buffer (gdb-inferior-io-name)))
442 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
444 (defun gdb-create-define-alist ()
445 "Create an alist of #define directives for GUD tooltips."
446 (let* ((file (buffer-file-name))
448 (with-output-to-string
449 (with-current-buffer standard-output
450 (and file (file-exists-p file)
451 (call-process shell-file-name file
452 (list t nil) nil "-c"
453 (concat gdb-cpp-define-alist-program " "
454 gdb-cpp-define-alist-flags))))))
455 (define-list (split-string output "\n" t)) (name))
456 (setq gdb-define-alist nil)
457 (dolist (define define-list)
458 (setq name (nth 1 (split-string define "[( ]")))
459 (push (cons name define) gdb-define-alist))))
461 (defun gdb-tooltip-print (expr)
463 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
464 (goto-char (point-min))
466 (if (search-forward "=" nil t)
467 (concat expr (buffer-substring (- (point) 2) (point-max)))
469 ;; remove newline for gud-tooltip-echo-area
470 (substring string 0 (- (length string) 1))))
471 (or gud-tooltip-echo-area tooltip-use-echo-area)))
473 ;; If expr is a macro for a function don't print because of possible dangerous
474 ;; side-effects. Also printing a function within a tooltip generates an
475 ;; unexpected starting annotation (phase error).
476 (defun gdb-tooltip-print-1 (expr)
477 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
478 (goto-char (point-min))
479 (if (search-forward "expands to: " nil t)
480 (unless (looking-at "\\S-+.*(.*).*")
482 (list (concat gdb-server-prefix "print " expr "\n")
483 `(lambda () (gdb-tooltip-print ,expr))))))))
485 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
487 (defun gdb-init-buffer ()
488 (set (make-local-variable 'gud-minor-mode)
489 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
490 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
491 (when gud-tooltip-mode
492 (make-local-variable 'gdb-define-alist)
493 (gdb-create-define-alist)
494 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
496 (defun gdb-set-gud-minor-mode-existing-buffers ()
497 "Create list of source files for current GDB session."
498 (goto-char (point-min))
499 (when (search-forward "read in on demand:" nil t)
500 (while (re-search-forward gdb-source-file-regexp nil t)
501 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
502 gdb-source-file-list))
503 (dolist (buffer (buffer-list))
504 (with-current-buffer buffer
505 (when (and buffer-file-name
506 (member (file-name-nondirectory buffer-file-name)
507 gdb-source-file-list))
508 (gdb-init-buffer)))))
509 (gdb-force-mode-line-update
510 (propertize "ready" 'face font-lock-variable-name-face)))
512 (defun gdb-find-watch-expression ()
513 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
514 (varnum (car var)) expr array)
515 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
516 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
517 (component-list (split-string (match-string 2 varnum) "\\." t)))
518 (setq expr (nth 1 var1))
519 (setq varnumlet (car var1))
520 (dolist (component component-list)
521 (setq var2 (assoc varnumlet gdb-var-list))
522 (setq expr (concat expr
523 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
524 (concat "[" component "]")
525 (concat "." component))))
526 (setq varnumlet (concat varnumlet "." component)))
530 (gud-def gud-break (if (not (string-match "Machine" mode-name))
531 (gud-call "break %f:%l" arg)
535 (gud-call "break *%a" arg)))
536 "\C-b" "Set breakpoint at current line or address.")
538 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
539 (gud-call "clear %f:%l" arg)
543 (gud-call "clear *%a" arg)))
544 "\C-d" "Remove breakpoint at current line or address.")
546 (gud-def gud-until (if (not (string-match "Machine" mode-name))
547 (gud-call "until %f:%l" arg)
551 (gud-call "until *%a" arg)))
552 "\C-u" "Continue to current line or address.")
554 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
555 nil "Start or continue execution.")
557 ;; For debugging Emacs only.
561 "pp1 " (if (eq (buffer-local-value
562 'major-mode (window-buffer)) 'speedbar-mode)
563 (gdb-find-watch-expression) "%e")) arg)
564 nil "Print the Emacs s-expression.")
566 (define-key gud-minor-mode-map [left-margin mouse-1]
567 'gdb-mouse-set-clear-breakpoint)
568 (define-key gud-minor-mode-map [left-fringe mouse-1]
569 'gdb-mouse-set-clear-breakpoint)
570 (define-key gud-minor-mode-map [left-margin C-mouse-1]
571 'gdb-mouse-toggle-breakpoint-margin)
572 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
573 'gdb-mouse-toggle-breakpoint-fringe)
575 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
577 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
579 (define-key gud-minor-mode-map [left-margin mouse-3]
581 (define-key gud-minor-mode-map [left-fringe mouse-3]
584 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
586 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
588 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
590 (define-key gud-minor-mode-map [left-margin C-mouse-3]
594 (setq gdb-pc-address (if gdb-show-main "main" nil))
595 (setq gdb-previous-frame-pc-address nil
596 gdb-memory-address "main"
597 gdb-previous-frame nil
598 gdb-selected-frame nil
599 gdb-current-language nil
603 gdb-first-post-prompt t
607 gdb-pending-triggers nil
608 gdb-output-sink 'user
609 gdb-server-prefix "server "
610 gdb-location-alist nil
611 gdb-source-file-list nil
614 gdb-buffer-fringe-width (car (window-fringes))
617 gdb-source-window nil
618 gdb-inferior-status nil
620 gdb-look-up-stack nil
624 gdb-thread-indicator nil)
626 (setq gdb-buffer-type 'gdba)
628 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
630 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
631 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
635 (if (eq window-system 'w32)
636 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
637 (gdb-enqueue-input (list "set height 0\n" 'ignore))
638 (gdb-enqueue-input (list "set width 0\n" 'ignore))
640 (if (string-equal gdb-version "pre-6.4")
642 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
643 'gdb-set-gud-minor-mode-existing-buffers))
644 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
646 (list "server interpreter mi -data-list-register-names\n"
647 'gdb-get-register-names))
648 ; Needs GDB 6.2 onwards.
650 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
651 'gdb-set-gud-minor-mode-existing-buffers-1))
652 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
654 ;; Find source file and compilation directory here.
655 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
656 (gdb-enqueue-input (list "server list\n" 'ignore))
657 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
659 (run-hooks 'gdb-mode-hook))
661 (defun gdb-get-version ()
662 (goto-char (point-min))
663 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
664 (setq gdb-version "pre-6.4")
665 (setq gdb-version "6.4+"))
668 (defmacro gdb-if-arrow (arrow-position &rest body)
670 (let ((buffer (marker-buffer ,arrow-position)) (line))
671 (if (equal buffer (window-buffer (posn-window end)))
672 (with-current-buffer buffer
673 (when (or (equal start end)
674 (equal (posn-point start)
675 (marker-position ,arrow-position)))
678 (defun gdb-mouse-until (event)
679 "Continue running until a source line past the current line.
680 The destination source line can be selected either by clicking
681 with mouse-3 on the fringe/margin or dragging the arrow
682 with mouse-1 (default bindings)."
684 (let ((start (event-start event))
685 (end (event-end event)))
686 (gdb-if-arrow gud-overlay-arrow-position
687 (setq line (line-number-at-pos (posn-point end)))
688 (gud-call (concat "until " (number-to-string line))))
689 (gdb-if-arrow gdb-overlay-arrow-position
691 (goto-line (line-number-at-pos (posn-point end)))
693 (gud-call (concat "until *%a"))))))
695 (defun gdb-mouse-jump (event)
696 "Set execution address/line.
697 The destination source line can be selected either by clicking with C-mouse-3
698 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
699 Unlike `gdb-mouse-until' the destination address can be before the current
700 line, and no execution takes place."
702 (let ((start (event-start event))
703 (end (event-end event)))
704 (gdb-if-arrow gud-overlay-arrow-position
705 (setq line (line-number-at-pos (posn-point end)))
707 (gud-call (concat "tbreak " (number-to-string line)))
708 (gud-call (concat "jump " (number-to-string line)))))
709 (gdb-if-arrow gdb-overlay-arrow-position
711 (goto-line (line-number-at-pos (posn-point end)))
714 (gud-call (concat "tbreak *%a"))
715 (gud-call (concat "jump *%a")))))))
717 (defcustom gdb-speedbar-auto-raise nil
718 "If non-nil raise speedbar every time display of watch expressions is\
724 (defun gdb-speedbar-auto-raise (arg)
725 "Toggle automatic raising of the speedbar for watch expressions.
726 With prefix argument ARG, automatically raise speedbar if ARG is
727 positive, otherwise don't automatically raise it."
729 (setq gdb-speedbar-auto-raise
731 (not gdb-speedbar-auto-raise)
732 (> (prefix-numeric-value arg) 0)))
733 (message (format "Auto raising %sabled"
734 (if gdb-speedbar-auto-raise "en" "dis"))))
736 (defcustom gdb-use-colon-colon-notation nil
737 "If non-nil use FUN::VAR format to display variables in the speedbar."
742 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
743 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
745 (defun gud-watch (&optional arg event)
746 "Watch expression at point.
747 With arg, enter name of variable to be watched in the minibuffer."
748 (interactive (list current-prefix-arg last-input-event))
749 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
750 (if (memq minor-mode '(gdbmi gdba))
752 (if event (posn-set-point (event-end event)))
754 (save-selected-window
757 (completing-read "Name of variable: "
758 'gud-gdb-complete-command)
759 (if (and transient-mark-mode mark-active)
760 (buffer-substring (region-beginning) (region-end))
761 (concat (if (eq major-mode 'gdb-registers-mode) "$")
762 (tooltip-identifier-from-point (point)))))))
763 (set-text-properties 0 (length expr) nil expr)
766 (if (eq minor-mode 'gdba)
768 "server interpreter mi \"-var-create - * " expr "\"\n")
769 (concat"-var-create - * " expr "\n"))
770 `(lambda () (gdb-var-create-handler ,expr)))))))
771 (message "gud-watch is a no-op in this mode."))))
773 (defconst gdb-var-create-regexp
774 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",\\(?:.*value=\\(\".*\"\\),\\)?.*type=\"\\(.*?\\)\"")
776 (defun gdb-var-create-handler (expr)
777 (goto-char (point-min))
778 (if (re-search-forward gdb-var-create-regexp nil t)
781 (if (and (string-equal gdb-current-language "c")
782 gdb-use-colon-colon-notation gdb-selected-frame)
783 (setq expr (concat gdb-selected-frame "::" expr))
787 (if (match-string 3) (read (match-string 3)))
788 nil gdb-frame-address)))
789 (push var gdb-var-list)
791 (unless (string-equal
792 speedbar-initial-expansion-list-name "GUD")
793 (speedbar-change-initial-expansion-list "GUD"))
797 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
799 (concat "server interpreter mi \"0-var-evaluate-expression "
801 (concat "0-var-evaluate-expression " (car var) "\n"))
802 `(lambda () (gdb-var-evaluate-expression-handler
804 (if (search-forward "Undefined command" nil t)
805 (message-box "Watching expressions requires GDB 6.0 onwards")
806 (message-box "No symbol \"%s\" in current context." expr))))
808 (defun gdb-speedbar-update ()
809 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
810 (not (member 'gdb-speedbar-timer gdb-pending-triggers)))
811 ;; Dummy command to update speedbar even when idle.
812 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
813 ;; Keep gdb-pending-triggers non-nil till end.
814 (push 'gdb-speedbar-timer gdb-pending-triggers)))
816 (defun gdb-speedbar-timer-fn ()
817 (if gdb-speedbar-auto-raise
818 (raise-frame speedbar-frame))
819 (setq gdb-pending-triggers
820 (delq 'gdb-speedbar-timer gdb-pending-triggers))
823 (defun gdb-var-evaluate-expression-handler (varnum changed)
824 (goto-char (point-min))
825 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t)
826 (setq gdb-pending-triggers
827 (delq (string-to-number (match-string 1)) gdb-pending-triggers))
828 (let ((var (assoc varnum gdb-var-list)))
830 (if changed (setcar (nthcdr 5 var) 'changed))
831 (setcar (nthcdr 4 var) (read (match-string 2)))))
832 (gdb-speedbar-update))
834 (defun gdb-var-list-children (varnum)
836 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
837 `(lambda () (gdb-var-list-children-handler ,varnum)))))
839 (defconst gdb-var-list-children-regexp
840 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
841 numchild=\"\\(.*?\\)\"\\(}\\|,.*?\\(type=\"\\(.*?\\)\"\\)?.*?}\\)")
843 (defun gdb-var-list-children-handler (varnum)
844 (goto-char (point-min))
845 (let ((var-list nil))
846 (catch 'child-already-watched
847 (dolist (var gdb-var-list)
848 (if (string-equal varnum (car var))
851 (while (re-search-forward gdb-var-list-children-regexp nil t)
852 (let ((varchild (list (match-string 1)
857 (if (assoc (car varchild) gdb-var-list)
858 (throw 'child-already-watched nil))
859 (push varchild var-list)
863 "server interpreter mi \"0-var-evaluate-expression "
864 (car varchild) "\"\n")
865 `(lambda () (gdb-var-evaluate-expression-handler
866 ,(car varchild) nil)))))))
867 (push var var-list)))
868 (setq gdb-var-list (nreverse var-list)))))
870 (defun gdb-var-update ()
871 (when (not (member 'gdb-var-update gdb-pending-triggers))
873 (list "server interpreter mi \"-var-update *\"\n"
874 'gdb-var-update-handler))
875 (push 'gdb-var-update gdb-pending-triggers)))
877 (defconst gdb-var-update-regexp
878 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
879 type_changed=\".*?\".*?}")
881 (defun gdb-var-update-handler ()
882 (dolist (var gdb-var-list)
883 (setcar (nthcdr 5 var) nil))
884 (goto-char (point-min))
886 (while (re-search-forward gdb-var-update-regexp nil t)
887 (let ((varnum (match-string 1)))
888 (if (string-equal (match-string 2) "false")
889 (let ((var (assoc varnum gdb-var-list)))
890 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
892 (push n gdb-pending-triggers)
895 (concat "server interpreter mi \"" (number-to-string n)
896 "-var-evaluate-expression " varnum "\"\n")
897 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))))))
898 (setq gdb-pending-triggers
899 (delq 'gdb-var-update gdb-pending-triggers)))
901 (defun gdb-var-set-format (format)
902 "Set the output format for a variable displayed in the speedbar."
903 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
907 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
908 (concat "server interpreter mi \"-var-set-format "
909 varnum " " format "\"\n")
910 (concat "-var-set-format " varnum " " format "\n"))
911 `(lambda () (gdb-var-set-format-handler ,varnum))))))
913 (defconst gdb-var-set-format-regexp
914 "format=\"\\(.*?\\)\",.*value=\"\\(.*?\\)\"")
916 (defun gdb-var-set-format-handler (varnum)
917 (goto-char (point-min))
918 (if (re-search-forward gdb-var-set-format-regexp nil t)
919 (let ((var (assoc varnum gdb-var-list)))
920 (setcar (nthcdr 4 var) (match-string 2))
921 (gdb-var-update-1))))
923 (defun gdb-var-delete-1 (varnum)
926 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
927 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
928 (concat "-var-delete " varnum "\n"))
930 (setq gdb-var-list (delq var gdb-var-list))
931 (dolist (varchild gdb-var-list)
932 (if (string-match (concat (car var) "\\.") (car varchild))
933 (setq gdb-var-list (delq varchild gdb-var-list)))))
935 (defun gdb-var-delete ()
936 "Delete watch expression at point from the speedbar."
938 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
940 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
942 (if (string-match "\\." (car var))
943 (message-box "Can only delete a root expression")
944 (gdb-var-delete-1 varnum)))))
946 (defun gdb-var-delete-children (varnum)
947 "Delete children of variable object at point from the speedbar."
950 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
951 (concat "server interpreter mi \"-var-delete -c " varnum "\"\n")
952 (concat "-var-delete -c " varnum "\n")) 'ignore)))
954 (defun gdb-edit-value (text token indent)
955 "Assign a value to a variable displayed in the speedbar."
956 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
957 (varnum (car var)) (value))
958 (setq value (read-string "New value: "))
961 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
962 (concat "server interpreter mi \"-var-assign "
963 varnum " " value "\"\n")
964 (concat "-var-assign " varnum " " value "\n"))
965 `(lambda () (gdb-edit-value-handler ,value))))))
967 (defun gdb-edit-value-handler (value)
968 (goto-char (point-min))
969 (if (re-search-forward gdb-error-regexp nil t)
970 (message-box "Invalid number or expression (%s)" value)))
972 (defcustom gdb-show-changed-values t
973 "If non-nil change the face of out of scope variables and changed values.
974 Out of scope variables are suppressed with `shadow' face.
975 Changed values are highlighted with the face `font-lock-warning-face'."
980 (defcustom gdb-max-children 40
981 "Maximum number of children before expansion requires confirmation."
986 (defcustom gdb-delete-out-of-scope t
987 "If non-nil delete watch expressions automatically when they go out of scope."
992 (defun gdb-speedbar-expand-node (text token indent)
993 "Expand the node the user clicked on.
994 TEXT is the text of the button we clicked on, a + or - item.
995 TOKEN is data related to this node.
996 INDENT is the current indentation depth."
997 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
999 (cond ((string-match "+" text) ;expand this node
1000 (let* ((var (assoc token gdb-var-list))
1001 (expr (nth 1 var)) (children (nth 2 var)))
1002 (if (or (<= (string-to-number children) gdb-max-children)
1005 "%s has %s children. Continue? " expr children)))
1006 (if (and (eq (buffer-local-value
1007 'gud-minor-mode gud-comint-buffer) 'gdba)
1008 (string-equal gdb-version "pre-6.4"))
1009 (gdb-var-list-children token)
1010 (gdb-var-list-children-1 token)))))
1011 ((string-match "-" text) ;contract this node
1012 (dolist (var gdb-var-list)
1013 (if (string-match (concat token "\\.") (car var))
1014 (setq gdb-var-list (delq var gdb-var-list))))
1015 (gdb-var-delete-children token)
1016 (speedbar-change-expand-button-char ?+)
1017 (speedbar-delete-subblock indent))
1018 (t (error "Ooops... not sure what to do")))
1019 (speedbar-center-buffer-smartly))
1020 (message-box "GUD session has been killed")))
1022 (defun gdb-get-target-string ()
1023 (with-current-buffer gud-comint-buffer
1030 ;; Each buffer has a TYPE -- a symbol that identifies the function
1031 ;; of that particular buffer.
1033 ;; The usual gdb interaction buffer is given the type `gdba' and
1034 ;; is constructed specially.
1036 ;; Others are constructed by gdb-get-buffer-create and
1037 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
1039 (defvar gdb-buffer-rules-assoc '())
1041 (defun gdb-get-buffer (key)
1042 "Return the gdb buffer tagged with type KEY.
1043 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1045 (gdb-look-for-tagged-buffer key (buffer-list))))
1047 (defun gdb-get-buffer-create (key)
1048 "Create a new gdb buffer of the type specified by KEY.
1049 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1050 (or (gdb-get-buffer key)
1051 (let* ((rules (assoc key gdb-buffer-rules-assoc))
1052 (name (funcall (gdb-rules-name-maker rules)))
1053 (new (get-buffer-create name)))
1054 (with-current-buffer new
1056 (if (cdr (cdr rules))
1057 (setq trigger (funcall (car (cdr (cdr rules))))))
1058 (setq gdb-buffer-type key)
1059 (set (make-local-variable 'gud-minor-mode)
1060 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1061 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1062 (if trigger (funcall trigger)))
1065 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
1067 (defun gdb-look-for-tagged-buffer (key bufs)
1069 (while (and (not retval) bufs)
1070 (set-buffer (car bufs))
1071 (if (eq gdb-buffer-type key)
1072 (setq retval (car bufs)))
1073 (setq bufs (cdr bufs)))
1077 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1078 ;; at least one and possible more functions. The functions have these
1079 ;; roles in defining a buffer type:
1081 ;; NAME - Return a name for this buffer type.
1083 ;; The remaining function(s) are optional:
1085 ;; MODE - called in a new buffer with no arguments, should establish
1086 ;; the proper mode for the buffer.
1089 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1090 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
1092 (setcdr binding rules)
1093 (push (cons buffer-type rules)
1094 gdb-buffer-rules-assoc))))
1096 ;; GUD buffers are an exception to the rules
1097 (gdb-set-buffer-rules 'gdba 'error)
1099 ;; Partial-output buffer : This accumulates output from a command executed on
1100 ;; behalf of emacs (rather than the user).
1102 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1103 'gdb-partial-output-name)
1105 (defun gdb-partial-output-name ()
1106 (concat " *partial-output-"
1107 (gdb-get-target-string)
1111 (gdb-set-buffer-rules 'gdb-inferior-io
1112 'gdb-inferior-io-name
1113 'gdb-inferior-io-mode)
1115 (defun gdb-inferior-io-name ()
1116 (concat "*input/output of "
1117 (gdb-get-target-string)
1120 (defun gdb-display-separate-io-buffer ()
1121 "Display IO of debugged program in a separate window."
1123 (if gdb-use-separate-io-buffer
1125 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1127 (defconst gdb-frame-parameters
1128 '((height . 14) (width . 80)
1130 (tool-bar-lines . nil)
1131 (menu-bar-lines . nil)
1132 (minibuffer . nil)))
1134 (defun gdb-frame-separate-io-buffer ()
1135 "Display IO of debugged program in a new frame."
1137 (if gdb-use-separate-io-buffer
1138 (let ((special-display-regexps (append special-display-regexps '(".*")))
1139 (special-display-frame-alist gdb-frame-parameters))
1140 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1142 (defvar gdb-inferior-io-mode-map
1143 (let ((map (make-sparse-keymap)))
1144 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1145 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1146 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1147 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1148 (define-key map "\C-d" 'gdb-separate-io-eof)
1151 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1152 "Major mode for gdb inferior-io."
1153 :syntax-table nil :abbrev-table nil
1154 ;; We want to use comint because it has various nifty and familiar
1155 ;; features. We don't need a process, but comint wants one, so create
1157 (make-comint-in-buffer
1158 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1159 (current-buffer) "hexl")
1160 (setq comint-input-sender 'gdb-inferior-io-sender))
1162 (defun gdb-inferior-io-sender (proc string)
1163 ;; PROC is the pseudo-process created to satisfy comint.
1164 (with-current-buffer (process-buffer proc)
1165 (setq proc (get-buffer-process gud-comint-buffer))
1166 (process-send-string proc string)
1167 (process-send-string proc "\n")))
1169 (defun gdb-separate-io-interrupt ()
1170 "Interrupt the program being debugged."
1173 (get-buffer-process gud-comint-buffer) comint-ptyp))
1175 (defun gdb-separate-io-quit ()
1176 "Send quit signal to the program being debugged."
1179 (get-buffer-process gud-comint-buffer) comint-ptyp))
1181 (defun gdb-separate-io-stop ()
1182 "Stop the program being debugged."
1185 (get-buffer-process gud-comint-buffer) comint-ptyp))
1187 (defun gdb-separate-io-eof ()
1188 "Send end-of-file to the program being debugged."
1191 (get-buffer-process gud-comint-buffer)))
1194 ;; gdb communications
1197 ;; INPUT: things sent to gdb
1199 ;; The queues are lists. Each element is either a string (indicating user or
1200 ;; user-like input) or a list of the form:
1202 ;; (INPUT-STRING HANDLER-FN)
1204 ;; The handler function will be called from the partial-output buffer when the
1205 ;; command completes. This is the way to write commands which invoke gdb
1206 ;; commands autonomously.
1208 ;; These lists are consumed tail first.
1211 (defun gdb-send (proc string)
1212 "A comint send filter for gdb.
1213 This filter may simply queue input for a later time."
1216 (with-current-buffer gud-comint-buffer
1217 (let ((inhibit-read-only t))
1218 (remove-text-properties (point-min) (point-max) '(face))))
1221 (let ((item (concat string "\n")))
1222 (if gdb-enable-debug (push (cons 'send item) gdb-debug-log))
1223 (process-send-string proc item)))
1224 (if (string-match "\\\\\\'" string)
1225 (setq gdb-continuation (concat gdb-continuation string "\n"))
1227 gdb-continuation string
1228 (if (not comint-input-sender-no-newline) "\n"))))
1229 (gdb-enqueue-input item)
1230 (setq gdb-continuation nil)))))
1231 (push (concat string "\n") gdb-early-user-input)))
1233 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1234 ;; is a query, or other non-top-level prompt.
1236 (defun gdb-enqueue-input (item)
1237 (if (not gud-running)
1240 (gdb-send-item item)
1241 (setq gdb-prompting nil))
1242 (push item gdb-input-queue))))
1244 (defun gdb-dequeue-input ()
1245 (let ((queue gdb-input-queue))
1247 (let ((last (car (last queue))))
1248 (unless (nbutlast queue) (setq gdb-input-queue '()))
1250 ;; This should be nil here anyway but set it just to make sure.
1251 (setq gdb-pending-triggers nil))))
1253 (defun gdb-send-item (item)
1254 (setq gdb-flush-pending-output nil)
1255 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1256 (setq gdb-current-item item)
1257 (let ((process (get-buffer-process gud-comint-buffer)))
1258 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1261 (setq gdb-output-sink 'user)
1262 (process-send-string process item))
1264 (gdb-clear-partial-output)
1265 (setq gdb-output-sink 'pre-emacs)
1266 (process-send-string process
1268 ;; case: eq gud-minor-mode 'gdbmi
1269 (gdb-clear-partial-output)
1270 (setq gdb-output-sink 'emacs)
1271 (process-send-string process (car item)))))
1274 ;; output -- things gdb prints to emacs
1276 ;; GDB output is a stream interrupted by annotations.
1277 ;; Annotations can be recognized by their beginning
1278 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1280 ;; The tag is a string obeying symbol syntax.
1282 ;; The optional part `<opt>' can be either the empty string
1283 ;; or a space followed by more data relating to the annotation.
1284 ;; For example, the SOURCE annotation is followed by a filename,
1285 ;; line number and various useless goo. This data must not include
1289 (defcustom gud-gdb-command-name "gdb --annotate=3"
1290 "Default command to execute an executable under the GDB debugger.
1291 The option \"--annotate=3\" must be included in this value if you
1292 want the GDB Graphical Interface."
1297 (defvar gdb-annotation-rules
1298 '(("pre-prompt" gdb-pre-prompt)
1299 ("prompt" gdb-prompt)
1300 ("commands" gdb-subprompt)
1301 ("overload-choice" gdb-subprompt)
1302 ("query" gdb-subprompt)
1303 ;; Need this prompt for GDB 6.1
1304 ("nquery" gdb-subprompt)
1305 ("prompt-for-continue" gdb-subprompt)
1306 ("post-prompt" gdb-post-prompt)
1307 ("source" gdb-source)
1308 ("starting" gdb-starting)
1309 ("exited" gdb-exited)
1310 ("signalled" gdb-signalled)
1311 ("signal" gdb-signal)
1312 ("breakpoint" gdb-stopping)
1313 ("watchpoint" gdb-stopping)
1314 ("frame-begin" gdb-frame-begin)
1315 ("stopped" gdb-stopped)
1316 ("error-begin" gdb-error)
1318 ("new-thread" (lambda (ignored) (gdb-get-buffer-create 'gdb-threads-buffer))))
1319 "An assoc mapping annotation tags to functions which process them.")
1322 (setq gdb-flush-pending-output t)
1323 (setq gud-running nil)
1324 (gdb-force-mode-line-update
1325 (propertize "stopped" 'face font-lock-warning-face))
1326 (setq gdb-output-sink 'user)
1327 (setq gdb-input-queue nil)
1328 (setq gdb-pending-triggers nil)
1329 (setq gdb-prompting t))
1331 (defconst gdb-source-spec-regexp
1332 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1334 ;; Do not use this except as an annotation handler.
1335 (defun gdb-source (args)
1336 (string-match gdb-source-spec-regexp args)
1337 ;; Extract the frame position from the marker.
1338 (setq gud-last-frame
1340 (match-string 1 args)
1341 (string-to-number (match-string 2 args))))
1342 (setq gdb-pc-address (match-string 3 args))
1343 ;; cover for auto-display output which comes *before*
1344 ;; stopped annotation
1345 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1347 (defun gdb-pre-prompt (ignored)
1348 "An annotation handler for `pre-prompt'.
1349 This terminates the collection of output from a previous command if that
1350 happens to be in effect."
1351 (setq gdb-error nil)
1352 (let ((sink gdb-output-sink))
1356 (setq gdb-output-sink 'post-emacs))
1359 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1361 (defun gdb-prompt (ignored)
1362 "An annotation handler for `prompt'.
1363 This sends the next command (if any) to gdb."
1364 (when gdb-first-prompt
1365 (gdb-force-mode-line-update
1366 (propertize "initializing..." 'face font-lock-variable-name-face))
1368 (setq gdb-first-prompt nil))
1369 (let ((sink gdb-output-sink))
1372 ((eq sink 'post-emacs)
1373 (setq gdb-output-sink 'user)
1375 (car (cdr gdb-current-item))))
1376 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1377 (funcall handler))))
1380 (error "Phase error in gdb-prompt (got %s)" sink))))
1381 (let ((input (gdb-dequeue-input)))
1383 (gdb-send-item input)
1385 (setq gdb-prompting t)
1387 (setq gdb-early-user-input (nreverse gdb-early-user-input))
1388 (while gdb-early-user-input
1389 (gdb-enqueue-input (car gdb-early-user-input))
1390 (setq gdb-early-user-input (cdr gdb-early-user-input)))))))
1392 (defun gdb-subprompt (ignored)
1393 "An annotation handler for non-top-level prompts."
1394 (setq gdb-prompting t))
1396 (defun gdb-starting (ignored)
1397 "An annotation handler for `starting'.
1398 This says that I/O for the subprocess is now the program being debugged,
1400 (setq gdb-active-process t)
1401 (setq gdb-printing t)
1402 (let ((sink gdb-output-sink))
1406 (setq gud-running t)
1407 (setq gdb-stack-update t)
1408 ;; Temporarily set gud-running to nil to force "info stack" onto queue.
1409 (let ((gud-running nil))
1410 (gdb-invalidate-frames))
1411 (setq gdb-inferior-status "running")
1412 (setq gdb-signalled nil)
1413 (gdb-force-mode-line-update
1414 (propertize gdb-inferior-status 'face font-lock-type-face))
1415 (gdb-remove-text-properties)
1416 (setq gud-old-arrow gud-overlay-arrow-position)
1417 (setq gud-overlay-arrow-position nil)
1418 (setq gdb-overlay-arrow-position nil)
1419 (setq gdb-stack-position nil)
1420 (if gdb-use-separate-io-buffer
1421 (setq gdb-output-sink 'inferior))))
1424 (error "Unexpected `starting' annotation")))))
1426 (defun gdb-signal (ignored)
1427 (setq gdb-inferior-status "signal")
1428 (gdb-force-mode-line-update
1429 (propertize gdb-inferior-status 'face font-lock-warning-face))
1430 (gdb-stopping ignored))
1432 (defun gdb-stopping (ignored)
1433 "An annotation handler for `breakpoint' and other annotations.
1434 They say that I/O for the subprocess is now GDB, not the program
1436 (if gdb-use-separate-io-buffer
1437 (let ((sink gdb-output-sink))
1439 ((eq sink 'inferior)
1440 (setq gdb-output-sink 'user))
1443 (error "Unexpected stopping annotation"))))))
1445 (defun gdb-exited (ignored)
1446 "An annotation handler for `exited' and `signalled'.
1447 They say that I/O for the subprocess is now GDB, not the program
1448 being debugged and that the program is no longer running. This
1449 function is used to change the focus of GUD tooltips to #define
1451 (setq gdb-active-process nil)
1452 (setq gud-overlay-arrow-position nil)
1453 (setq gdb-overlay-arrow-position nil)
1454 (setq gdb-stack-position nil)
1455 (setq gud-old-arrow nil)
1456 (setq gdb-inferior-status "exited")
1457 (gdb-force-mode-line-update
1458 (propertize gdb-inferior-status 'face font-lock-warning-face))
1459 (gdb-stopping ignored))
1461 (defun gdb-signalled (ignored)
1462 (setq gdb-signalled t))
1464 (defun gdb-frame-begin (ignored)
1465 (setq gdb-frame-begin t)
1466 (setq gdb-printing nil)
1467 (let ((sink gdb-output-sink))
1469 ((eq sink 'inferior)
1470 (setq gdb-output-sink 'user))
1472 ((eq sink 'emacs) t)
1475 (error "Unexpected frame-begin annotation (%S)" sink)))))
1477 (defcustom gdb-same-frame (not focus-follows-mouse)
1478 "Non-nil means pop up GUD buffer in same frame."
1483 (defcustom gdb-find-source-frame nil
1484 "Non-nil means try to find a source frame further up stack e.g after signal."
1489 (defun gdb-find-source-frame (arg)
1490 "Toggle looking for a source frame further up call stack.
1491 The code associated with current (innermost) frame may not have
1492 been compiled with debug information, e.g., C library routine.
1493 With prefix argument ARG, look for a source frame further up
1494 stack to display in the source buffer if ARG is positive,
1495 otherwise don't look further up."
1497 (setq gdb-find-source-frame
1499 (not gdb-find-source-frame)
1500 (> (prefix-numeric-value arg) 0)))
1501 (message (format "Looking for source frame %sabled"
1502 (if gdb-find-source-frame "en" "dis"))))
1504 (defun gdb-stopped (ignored)
1505 "An annotation handler for `stopped'.
1506 It is just like `gdb-stopping', except that if we already set the output
1507 sink to `user' in `gdb-stopping', that is fine."
1508 (setq gud-running nil)
1509 (unless (or gud-overlay-arrow-position gud-last-frame)
1510 (if (and gdb-frame-begin gdb-printing)
1511 (setq gud-overlay-arrow-position gud-old-arrow)
1512 ;;Pop up GUD buffer to display current frame when it doesn't have source
1513 ;;information i.e if not compiled with -g as with libc routines generally.
1515 (gdb-display-gdb-buffer)
1516 (gdb-frame-gdb-buffer))
1517 (if gdb-find-source-frame
1518 ;;Try to find source further up stack e.g after signal.
1519 (setq gdb-look-up-stack
1520 (if (gdb-get-buffer 'gdb-stack-buffer)
1523 (gdb-get-buffer-create 'gdb-stack-buffer)
1524 (gdb-invalidate-frames)
1526 (unless (member gdb-inferior-status '("exited" "signal"))
1527 (setq gdb-active-process t) ;Just for attaching case.
1528 (setq gdb-inferior-status "stopped")
1529 (gdb-force-mode-line-update
1530 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1531 (let ((sink gdb-output-sink))
1533 ((eq sink 'inferior)
1534 (setq gdb-output-sink 'user))
1538 (error "Unexpected stopped annotation"))))
1539 (if gdb-signalled (gdb-exited ignored)))
1541 (defun gdb-error (ignored)
1542 (setq gdb-error (not gdb-error)))
1544 (defun gdb-post-prompt (ignored)
1545 "An annotation handler for `post-prompt'.
1546 This begins the collection of output from the current command if that
1547 happens to be appropriate."
1548 ;; Don't add to queue if there outstanding items or gdb-version is not known
1550 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1551 (gdb-get-selected-frame)
1552 (gdb-invalidate-frames)
1553 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1554 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1555 (gdb-invalidate-breakpoints)
1556 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1557 ;; so gdb-pc-address is updated.
1558 ;; (gdb-invalidate-assembler)
1560 (if (string-equal gdb-version "pre-6.4")
1561 (gdb-invalidate-registers)
1562 (gdb-get-changed-registers)
1563 (gdb-invalidate-registers-1))
1565 (gdb-invalidate-memory)
1566 (if (string-equal gdb-version "pre-6.4")
1567 (gdb-invalidate-locals)
1568 (gdb-invalidate-locals-1))
1570 (gdb-invalidate-threads)
1571 (unless (or (null gdb-var-list)
1572 (eq system-type 'darwin)) ;Breaks on Darwin's GDB-5.3.
1573 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1574 ;; Only needed/used with speedbar/watch expressions.
1575 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1576 (if (string-equal gdb-version "pre-6.4")
1578 (gdb-var-update-1)))))
1579 (setq gdb-first-post-prompt nil)
1580 (let ((sink gdb-output-sink))
1583 ((eq sink 'pre-emacs)
1584 (setq gdb-output-sink 'emacs))
1587 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1589 (defconst gdb-buffer-list
1590 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1592 (defun gdb-remove-text-properties ()
1593 (dolist (buffertype gdb-buffer-list)
1594 (let ((buffer (gdb-get-buffer buffertype)))
1596 (with-current-buffer buffer
1597 (let ((inhibit-read-only t))
1598 (remove-text-properties
1599 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1601 ;; GUD displays the selected GDB frame. This might might not be the current
1602 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1603 ;; visited breakpoint is, use that window.
1604 (defun gdb-display-source-buffer (buffer)
1605 (let* ((last-window (if gud-last-last-frame
1607 (gud-find-file (car gud-last-last-frame)))))
1608 (source-window (or last-window
1609 (if (and gdb-source-window
1610 (window-live-p gdb-source-window))
1611 gdb-source-window))))
1613 (setq gdb-source-window source-window)
1614 (set-window-buffer source-window buffer))
1617 ;; Derived from gud-gdb-marker-regexp
1618 (defvar gdb-fullname-regexp
1619 (concat "\\(.:?[^" ":" "\n]*\\)" ":" "\\([0-9]*\\)" ":" ".*"))
1621 (defun gud-gdba-marker-filter (string)
1622 "A gud marker filter for gdb. Handle a burst of output from GDB."
1623 (if gdb-flush-pending-output
1625 (when gdb-enable-debug
1626 (push (cons 'recv string) gdb-debug-log)
1627 (if (and gdb-debug-log-max
1628 (> (length gdb-debug-log) gdb-debug-log-max))
1629 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1630 ;; Recall the left over gud-marker-acc from last time.
1631 (setq gud-marker-acc (concat gud-marker-acc string))
1632 ;; Start accumulating output for the GUD buffer.
1635 ;; Process all the complete markers in this chunk.
1636 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1637 (let ((annotation (match-string 1 gud-marker-acc))
1638 (before (substring gud-marker-acc 0 (match-beginning 0)))
1639 (after (substring gud-marker-acc (match-end 0))))
1641 ;; Parse the tag from the annotation, and maybe its arguments.
1642 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1643 (let* ((annotation-type (match-string 1 annotation))
1644 (annotation-arguments (match-string 2 annotation))
1645 (annotation-rule (assoc annotation-type
1646 gdb-annotation-rules))
1647 (fullname (string-match gdb-fullname-regexp annotation-type)))
1649 ;; Stuff prior to the match is just ordinary output.
1650 ;; It is either concatenated to OUTPUT or directed
1653 (gdb-concat-output output
1654 (concat before (if fullname "\n"))))
1656 ;; Take that stuff off the gud-marker-acc.
1657 (setq gud-marker-acc after)
1659 ;; Call the handler for this annotation.
1661 (funcall (car (cdr annotation-rule))
1662 annotation-arguments)
1664 ;; Switch to gud-gdb-marker-filter if appropriate.
1667 ;; Extract the frame position from the marker.
1668 (setq gud-last-frame (cons (match-string 1 annotation)
1670 (match-string 2 annotation))))
1672 (set (make-local-variable 'gud-minor-mode) 'gdb)
1673 (set (make-local-variable 'gud-marker-filter)
1674 'gud-gdb-marker-filter)))
1676 ;; Else the annotation is not recognized. Ignore it silently,
1677 ;; so that GDB can add new annotations without causing
1681 ;; Does the remaining text end in a partial line?
1682 ;; If it does, then keep part of the gud-marker-acc until we get more.
1683 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1686 ;; Everything before the potential marker start can be output.
1688 (gdb-concat-output output
1689 (substring gud-marker-acc 0
1690 (match-beginning 0))))
1692 ;; Everything after, we save, to combine with later input.
1693 (setq gud-marker-acc (substring gud-marker-acc
1694 (match-beginning 0))))
1696 ;; In case we know the gud-marker-acc contains no partial annotations:
1698 (setq output (gdb-concat-output output gud-marker-acc))
1699 (setq gud-marker-acc "")))
1702 (defun gdb-concat-output (so-far new)
1704 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1705 (let ((sink gdb-output-sink))
1707 ((eq sink 'user) (concat so-far new))
1708 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1710 (gdb-append-to-partial-output new)
1712 ((eq sink 'inferior)
1713 (gdb-append-to-inferior-io new)
1717 (error "Bogon output sink %S" sink)))))
1719 (defun gdb-append-to-partial-output (string)
1720 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1721 (goto-char (point-max))
1724 (defun gdb-clear-partial-output ()
1725 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1728 (defun gdb-append-to-inferior-io (string)
1729 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1730 (goto-char (point-max))
1731 (insert-before-markers string))
1732 (if (not (string-equal string ""))
1733 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1735 (defun gdb-clear-inferior-io ()
1736 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1740 ;; One trick is to have a command who's output is always available in a buffer
1741 ;; of it's own, and is always up to date. We build several buffers of this
1744 ;; There are two aspects to this: gdb has to tell us when the output for that
1745 ;; command might have changed, and we have to be able to run the command
1746 ;; behind the user's back.
1748 ;; The output phasing associated with the variable gdb-output-sink
1749 ;; help us to run commands behind the user's back.
1751 ;; Below is the code for specificly managing buffers of output from one
1755 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1756 ;; It adds an input for the command we are tracking. It should be the
1757 ;; annotation rule binding of whatever gdb sends to tell us this command
1758 ;; might have changed it's output.
1760 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1761 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1762 ;; input in the input queue (see comment about ``gdb communications'' above).
1764 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1766 `(defun ,name (&optional ignored)
1767 (if (and ,demand-predicate
1769 gdb-pending-triggers)))
1772 (list ,gdb-command ',output-handler))
1773 (push ',name gdb-pending-triggers)))))
1775 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1777 (setq gdb-pending-triggers
1779 gdb-pending-triggers))
1780 (let ((buf (gdb-get-buffer ',buf-key)))
1782 (with-current-buffer buf
1783 (let* ((window (get-buffer-window buf 0))
1784 (start (window-start window))
1785 (p (window-point window))
1786 (buffer-read-only nil))
1788 (insert-buffer-substring (gdb-get-buffer-create
1789 'gdb-partial-output-buffer))
1790 (set-window-start window start)
1791 (set-window-point window p)))))
1792 ;; put customisation here
1795 (defmacro def-gdb-auto-updated-buffer (buffer-key
1796 trigger-name gdb-command
1797 output-handler-name custom-defun)
1799 (def-gdb-auto-update-trigger ,trigger-name
1800 ;; The demand predicate:
1801 (gdb-get-buffer ',buffer-key)
1803 ,output-handler-name)
1804 (def-gdb-auto-update-handler ,output-handler-name
1805 ,trigger-name ,buffer-key ,custom-defun)))
1809 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1811 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1812 'gdb-breakpoints-buffer-name
1813 'gdb-breakpoints-mode)
1815 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1816 ;; This defines the auto update rule for buffers of type
1817 ;; `gdb-breakpoints-buffer'.
1819 ;; It defines a function to serve as the annotation handler that
1820 ;; handles the `foo-invalidated' message. That function is called:
1821 gdb-invalidate-breakpoints
1823 ;; To update the buffer, this command is sent to gdb.
1824 "server info breakpoints\n"
1826 ;; This also defines a function to be the handler for the output
1827 ;; from the command above. That function will copy the output into
1828 ;; the appropriately typed buffer. That function will be called:
1829 gdb-info-breakpoints-handler
1830 ;; buffer specific functions
1831 gdb-info-breakpoints-custom)
1833 (defconst breakpoint-xpm-data
1835 static char *magick[] = {
1836 /* columns rows colors chars-per-pixel */
1852 "XPM data used for breakpoint icon.")
1854 (defconst breakpoint-enabled-pbm-data
1857 0 0 0 0 1 1 1 1 0 0 0 0
1858 0 0 0 1 1 1 1 1 1 0 0 0
1859 0 0 1 1 1 1 1 1 1 1 0 0
1860 0 1 1 1 1 1 1 1 1 1 1 0
1861 0 1 1 1 1 1 1 1 1 1 1 0
1862 0 1 1 1 1 1 1 1 1 1 1 0
1863 0 1 1 1 1 1 1 1 1 1 1 0
1864 0 0 1 1 1 1 1 1 1 1 0 0
1865 0 0 0 1 1 1 1 1 1 0 0 0
1866 0 0 0 0 1 1 1 1 0 0 0 0"
1867 "PBM data used for enabled breakpoint icon.")
1869 (defconst breakpoint-disabled-pbm-data
1881 0 0 0 1 0 1 0 1 0 0"
1882 "PBM data used for disabled breakpoint icon.")
1884 (defvar breakpoint-enabled-icon nil
1885 "Icon for enabled breakpoint in display margin.")
1887 (defvar breakpoint-disabled-icon nil
1888 "Icon for disabled breakpoint in display margin.")
1890 (and (display-images-p)
1891 ;; Bitmap for breakpoint in fringe
1892 (define-fringe-bitmap 'breakpoint
1893 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1894 ;; Bitmap for gud-overlay-arrow in fringe
1895 (define-fringe-bitmap 'hollow-right-triangle
1896 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1898 (defface breakpoint-enabled
1902 "Face for enabled breakpoint icon in fringe."
1905 (defface breakpoint-disabled
1906 '((((class color) (min-colors 88)) :foreground "grey70")
1907 ;; Ensure that on low-color displays that we end up something visible.
1908 (((class color) (min-colors 8) (background light))
1909 :foreground "black")
1910 (((class color) (min-colors 8) (background dark))
1911 :foreground "white")
1912 (((type tty) (class mono))
1914 (t :background "gray"))
1915 "Face for disabled breakpoint icon in fringe."
1918 (defconst gdb-breakpoint-regexp
1919 "\\(?:\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\|\\([0-9]+\\.[0-9]+\\)\\)\\s-+\\(.\\)\\s-+")
1921 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1922 (defun gdb-info-breakpoints-custom ()
1923 (let ((flag) (bptno))
1924 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1925 (dolist (buffer (buffer-list))
1926 (with-current-buffer buffer
1927 (if (and (memq gud-minor-mode '(gdba gdbmi))
1928 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1929 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1930 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1932 (let ((buffer-read-only nil))
1933 (goto-char (point-min))
1934 (while (< (point) (- (point-max) 1))
1936 (if (looking-at gdb-breakpoint-regexp)
1938 (setq bptno (or (match-string 1) (match-string 2)))
1939 (setq flag (char-after (match-beginning 3)))
1940 (if (match-string 1)
1941 (setq gdb-parent-bptno-enabled (eq flag ?y)))
1942 (add-text-properties
1943 (match-beginning 3) (match-end 3)
1945 '(face font-lock-warning-face)
1946 '(face font-lock-type-face)))
1948 (el (line-end-position)))
1949 (when (re-search-forward " in \\(.*\\) at" el t)
1950 (add-text-properties
1951 (match-beginning 1) (match-end 1)
1952 '(face font-lock-function-name-face)))
1953 (if (re-search-forward
1954 ".*\\s-+\\(\\S-+\\):\\([0-9]+\\)$" nil t)
1955 (let ((line (match-string 2))
1956 (file (match-string 1)))
1957 (add-text-properties bl el
1958 '(mouse-face highlight
1959 help-echo "mouse-2, RET: visit breakpoint"))
1960 (unless (file-exists-p file)
1961 (setq file (cdr (assoc bptno gdb-location-alist))))
1963 (not (string-equal file "File not found")))
1964 (with-current-buffer
1965 (find-file-noselect file 'nowarn)
1967 ;; Only want one breakpoint icon at each
1970 (goto-line (string-to-number line))
1971 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1974 (concat gdb-server-prefix "list "
1975 (match-string-no-properties 1) ":1\n")
1978 (list (concat gdb-server-prefix "info source\n")
1979 `(lambda () (gdb-get-location
1980 ,bptno ,line ,flag))))))
1981 (if (re-search-forward
1982 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1984 (add-text-properties
1985 (match-beginning 1) (match-end 1)
1986 '(face font-lock-function-name-face))
1988 (re-search-backward "\\s-\\(\\S-*\\)"
1990 (add-text-properties
1991 (match-beginning 1) (match-end 1)
1992 '(face font-lock-variable-name-face)))))))
1994 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom))
1996 ;; Breakpoints buffer is always present. Hack to just update
1997 ;; current frame if there's been no execution.
1998 (if gdb-stack-update
1999 (setq gdb-stack-update nil)
2000 (if (gdb-get-buffer 'gdb-stack-buffer) (gdb-info-stack-custom))))
2002 (declare-function gud-remove "gdb-ui" t t) ; gud-def
2003 (declare-function gud-break "gdb-ui" t t) ; gud-def
2005 (defun gdb-mouse-set-clear-breakpoint (event)
2006 "Set/clear breakpoint in left fringe/margin at mouse click.
2007 If not in a source or disassembly buffer just set point."
2009 (mouse-minibuffer-check event)
2010 (let ((posn (event-end event)))
2011 (with-selected-window (posn-window posn)
2012 (if (or (buffer-file-name) (eq major-mode 'gdb-assembler-mode))
2013 (if (numberp (posn-point posn))
2015 (goto-char (posn-point posn))
2016 (if (or (posn-object posn)
2017 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2021 (posn-set-point posn))))
2023 (defun gdb-mouse-toggle-breakpoint-margin (event)
2024 "Enable/disable breakpoint in left margin with mouse click."
2026 (mouse-minibuffer-check event)
2027 (let ((posn (event-end event)))
2028 (if (numberp (posn-point posn))
2029 (with-selected-window (posn-window posn)
2031 (goto-char (posn-point posn))
2032 (if (posn-object posn)
2033 (let* ((bptno (get-text-property
2034 0 'gdb-bptno (car (posn-string posn)))))
2035 (string-match "\\([0-9+]\\)*" bptno)
2038 (concat gdb-server-prefix
2039 (if (get-text-property
2040 0 'gdb-enabled (car (posn-string posn)))
2043 (match-string 1 bptno) "\n")
2046 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2047 "Enable/disable breakpoint in left fringe with mouse click."
2049 (mouse-minibuffer-check event)
2050 (let* ((posn (event-end event))
2051 (pos (posn-point posn))
2054 (with-selected-window (posn-window posn)
2056 (set-buffer (window-buffer (selected-window)))
2058 (dolist (overlay (overlays-in pos pos))
2059 (when (overlay-get overlay 'put-break)
2060 (setq obj (overlay-get overlay 'before-string))))
2062 (let* ((bptno (get-text-property 0 'gdb-bptno obj)))
2063 (string-match "\\([0-9+]\\)*" bptno)
2066 (concat gdb-server-prefix
2067 (if (get-text-property 0 'gdb-enabled obj)
2070 (match-string 1 bptno) "\n")
2073 (defun gdb-breakpoints-buffer-name ()
2074 (with-current-buffer gud-comint-buffer
2075 (concat "*breakpoints of " (gdb-get-target-string) "*")))
2077 (defun gdb-display-breakpoints-buffer ()
2078 "Display status of user-settable breakpoints."
2081 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
2083 (defun gdb-frame-breakpoints-buffer ()
2084 "Display status of user-settable breakpoints in a new frame."
2086 (let ((special-display-regexps (append special-display-regexps '(".*")))
2087 (special-display-frame-alist gdb-frame-parameters))
2088 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
2090 (defvar gdb-breakpoints-mode-map
2091 (let ((map (make-sparse-keymap))
2092 (menu (make-sparse-keymap "Breakpoints")))
2093 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2094 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2095 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2096 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2097 (suppress-keymap map)
2098 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2099 (define-key map " " 'gdb-toggle-breakpoint)
2100 (define-key map "D" 'gdb-delete-breakpoint)
2101 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2102 (define-key map "q" 'gdb-delete-frame-or-window)
2103 (define-key map "\r" 'gdb-goto-breakpoint)
2104 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2105 (define-key map [follow-link] 'mouse-face)
2108 (defun gdb-delete-frame-or-window ()
2109 "Delete frame if there is only one window. Otherwise delete the window."
2111 (if (one-window-p) (delete-frame)
2114 ;;from make-mode-line-mouse-map
2115 (defun gdb-make-header-line-mouse-map (mouse function) "\
2116 Return a keymap with single entry for mouse key MOUSE on the header line.
2117 MOUSE is defined to run function FUNCTION with no args in the buffer
2118 corresponding to the mode line clicked."
2119 (let ((map (make-sparse-keymap)))
2120 (define-key map (vector 'header-line mouse) function)
2121 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2124 (defvar gdb-breakpoints-header
2125 `(,(propertize "Breakpoints"
2126 'help-echo "mouse-1: select"
2127 'mouse-face 'mode-line-highlight
2130 (gdb-make-header-line-mouse-map
2132 (lambda (event) (interactive "e")
2133 (save-selected-window
2134 (select-window (posn-window (event-start event)))
2135 (set-window-dedicated-p (selected-window) nil)
2137 (gdb-get-buffer-create 'gdb-breakpoints-buffer))
2138 (set-window-dedicated-p (selected-window) t)))))
2140 ,(propertize "Threads"
2141 'help-echo "mouse-1: select"
2142 'mouse-face 'mode-line-highlight
2145 (gdb-make-header-line-mouse-map
2147 (lambda (event) (interactive "e")
2148 (save-selected-window
2149 (select-window (posn-window (event-start event)))
2150 (set-window-dedicated-p (selected-window) nil)
2152 (gdb-get-buffer-create 'gdb-threads-buffer))
2153 (set-window-dedicated-p (selected-window) t)))))))
2155 (defun gdb-breakpoints-mode ()
2156 "Major mode for gdb breakpoints.
2158 \\{gdb-breakpoints-mode-map}"
2159 (kill-all-local-variables)
2160 (setq major-mode 'gdb-breakpoints-mode)
2161 (setq mode-name "Breakpoints")
2162 (use-local-map gdb-breakpoints-mode-map)
2163 (setq buffer-read-only t)
2164 (buffer-disable-undo)
2165 (setq header-line-format gdb-breakpoints-header)
2166 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2167 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2168 'gdb-invalidate-breakpoints
2169 'gdbmi-invalidate-breakpoints))
2171 (defun gdb-toggle-breakpoint ()
2172 "Enable/disable breakpoint at current line."
2175 (beginning-of-line 1)
2176 (if (looking-at gdb-breakpoint-regexp)
2179 (concat gdb-server-prefix
2180 (if (eq ?y (char-after (match-beginning 3)))
2183 (or (match-string 1) (match-string 2)) "\n") 'ignore))
2184 (error "Not recognized as break/watchpoint line"))))
2186 (defun gdb-delete-breakpoint ()
2187 "Delete the breakpoint at current line."
2190 (beginning-of-line 1)
2191 (if (looking-at gdb-breakpoint-regexp)
2192 (if (match-string 1)
2195 (concat gdb-server-prefix "delete " (match-string 1) "\n")
2197 (message-box "This breakpoint cannot be deleted on its own."))
2198 (error "Not recognized as break/watchpoint line"))))
2200 (defun gdb-goto-breakpoint (&optional event)
2201 "Display the breakpoint location specified at current line."
2202 (interactive (list last-input-event))
2203 (if event (posn-set-point (event-end event)))
2205 (beginning-of-line 1)
2206 (if (looking-at "\\([0-9]+\\.?[0-9]*\\) .*\\s-+\\(\\S-+\\):\\([0-9]+\\)$")
2207 (let ((bptno (match-string 1))
2208 (file (match-string 2))
2209 (line (match-string 3)))
2210 (save-selected-window
2211 (let* ((buffer (find-file-noselect
2212 (if (file-exists-p file) file
2213 (cdr (assoc bptno gdb-location-alist)))))
2214 (window (or (gdb-display-source-buffer buffer)
2215 (display-buffer buffer))))
2216 (setq gdb-source-window window)
2217 (with-current-buffer buffer
2218 (goto-line (string-to-number line))
2219 (set-window-point window (point))))))
2220 (error "No location specified."))))
2223 ;; Frames buffer. This displays a perpetually correct backtrace
2224 ;; (from the command `where').
2226 ;; Alas, if your stack is deep, it is costly.
2228 (defcustom gdb-max-frames 40
2229 "Maximum number of frames displayed in call stack."
2234 (gdb-set-buffer-rules 'gdb-stack-buffer
2235 'gdb-stack-buffer-name
2238 (def-gdb-auto-updated-buffer gdb-stack-buffer
2239 gdb-invalidate-frames
2240 (concat "server info stack " (number-to-string gdb-max-frames) "\n")
2241 gdb-info-stack-handler
2242 gdb-info-stack-custom)
2244 ;; This may be more important for embedded targets where unwinding the
2245 ;; stack may take a long time.
2246 (defadvice gdb-invalidate-frames (around gdb-invalidate-frames-advice
2247 (&optional ignored) activate compile)
2248 "Only queue \"info stack\" if execution has occurred."
2249 (if gdb-stack-update ad-do-it))
2251 (defun gdb-info-stack-custom ()
2252 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2255 (unless (eq gdb-look-up-stack 'delete)
2256 (let ((buffer-read-only nil)
2258 (goto-char (point-min))
2259 (while (< (point) (point-max))
2260 (setq bl (line-beginning-position)
2261 el (line-end-position))
2262 (when (looking-at "#")
2263 (add-text-properties bl el
2264 '(mouse-face highlight
2265 help-echo "mouse-2, RET: Select frame")))
2267 (when (looking-at "^#\\([0-9]+\\)")
2268 (when (string-equal (match-string 1) gdb-frame-number)
2269 (if (gud-tool-bar-item-visible-no-fringe)
2271 (put-text-property bl (+ bl 4)
2272 'face '(:inverse-video t))
2274 (or gdb-stack-position
2275 (setq gdb-stack-position (make-marker)))
2276 (set-marker gdb-stack-position (point))
2277 (setq move-to gdb-stack-position)))
2278 (when (re-search-forward "\\([^ ]+\\) (" el t)
2279 (put-text-property (match-beginning 1) (match-end 1)
2280 'face font-lock-function-name-face)
2281 (setq bl (match-end 0))
2282 (while (re-search-forward "<\\([^>]+\\)>" el t)
2283 (put-text-property (match-beginning 1) (match-end 1)
2284 'face font-lock-function-name-face))
2286 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2287 (put-text-property (match-beginning 1) (match-end 1)
2288 'face font-lock-variable-name-face))))
2291 (when (looking-at "(More stack frames follow...)")
2292 (add-text-properties
2293 (match-beginning 0) (match-end 0)
2294 '(mouse-face highlight
2297 "mouse-2, RET: customize gdb-max-frames to see more frames"
2299 (when gdb-look-up-stack
2300 (goto-char (point-min))
2301 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2302 (let ((start (line-beginning-position))
2303 (file (match-string 1))
2304 (line (match-string 2)))
2305 (re-search-backward "^#*\\([0-9]+\\)" start t)
2307 (list (concat gdb-server-prefix "frame "
2308 (match-string 1) "\n") 'gdb-set-hollow))
2310 (list (concat gdb-server-prefix "frame 0\n") 'ignore))))))
2312 (let ((window (get-buffer-window (current-buffer) 0)))
2314 (with-selected-window window
2316 (unless (pos-visible-in-window-p)
2317 (recenter '(center)))))))))
2318 (if (eq gdb-look-up-stack 'delete)
2319 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2320 (setq gdb-look-up-stack nil))
2322 (defun gdb-set-hollow ()
2323 (if gud-last-last-frame
2324 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2325 (setq fringe-indicator-alist
2326 '((overlay-arrow . hollow-right-triangle))))))
2328 (defun gdb-stack-buffer-name ()
2329 (with-current-buffer gud-comint-buffer
2330 (concat "*stack frames of " (gdb-get-target-string) "*")))
2332 (defun gdb-display-stack-buffer ()
2333 "Display backtrace of current stack."
2336 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2338 (defun gdb-frame-stack-buffer ()
2339 "Display backtrace of current stack in a new frame."
2341 (let ((special-display-regexps (append special-display-regexps '(".*")))
2342 (special-display-frame-alist gdb-frame-parameters))
2343 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2345 (defvar gdb-frames-mode-map
2346 (let ((map (make-sparse-keymap)))
2347 (suppress-keymap map)
2348 (define-key map "q" 'kill-this-buffer)
2349 (define-key map "\r" 'gdb-frames-select)
2350 (define-key map "F" 'gdb-frames-force-update)
2351 (define-key map [mouse-2] 'gdb-frames-select)
2352 (define-key map [follow-link] 'mouse-face)
2355 (declare-function gdbmi-invalidate-frames "ext:gdb-mi" nil t)
2357 (defun gdb-frames-force-update ()
2358 "Force update of call stack.
2359 Use when the displayed call stack gets out of sync with the
2360 actual one, e.g after using the Gdb command \"return\" or setting
2361 $pc directly from the GUD buffer. This command isn't normally needed."
2363 (setq gdb-stack-update t)
2364 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2365 (gdb-invalidate-frames)
2366 (gdbmi-invalidate-frames)))
2368 (defun gdb-frames-mode ()
2369 "Major mode for gdb call stack.
2371 \\{gdb-frames-mode-map}"
2372 (kill-all-local-variables)
2373 (setq major-mode 'gdb-frames-mode)
2374 (setq mode-name "Frames")
2375 (setq gdb-stack-position nil)
2376 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2377 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2378 (setq buffer-read-only t)
2379 (buffer-disable-undo)
2380 (gdb-thread-identification)
2381 (use-local-map gdb-frames-mode-map)
2382 (run-mode-hooks 'gdb-frames-mode-hook)
2383 (setq gdb-stack-update t)
2384 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2385 'gdb-invalidate-frames
2386 'gdbmi-invalidate-frames))
2388 (defun gdb-get-frame-number ()
2391 (let* ((start (line-beginning-position))
2392 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2393 (n (or (and pos (match-string 1)) "0")))
2396 (defun gdb-frames-select (&optional event)
2397 "Select the frame and display the relevant source."
2398 (interactive (list last-input-event))
2399 (if event (posn-set-point (event-end event)))
2400 (if (get-text-property (point) 'gdb-max-frames)
2402 (message-box "After setting gdb-max-frames, you need to enter\n\
2403 another GDB command e.g pwd, to see new frames")
2404 (customize-variable-other-window 'gdb-max-frames))
2406 (list (concat gdb-server-prefix "frame "
2407 (gdb-get-frame-number) "\n") 'ignore))))
2410 ;; Threads buffer. This displays a selectable thread list.
2412 (gdb-set-buffer-rules 'gdb-threads-buffer
2413 'gdb-threads-buffer-name
2416 (def-gdb-auto-updated-buffer gdb-threads-buffer
2417 gdb-invalidate-threads
2418 (concat gdb-server-prefix "info threads\n")
2419 gdb-info-threads-handler
2420 gdb-info-threads-custom)
2422 (defun gdb-info-threads-custom ()
2423 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2424 (let ((buffer-read-only nil))
2426 (goto-char (point-min))
2427 (if (re-search-forward "\\* \\([0-9]+\\)" nil t)
2428 (setq gdb-thread-indicator
2429 (propertize (concat " [" (match-string 1) "]")
2430 ; FIXME: this help-echo doesn't work
2431 'help-echo "thread id")))
2432 (goto-char (point-min))
2433 (while (< (point) (point-max))
2434 (unless (looking-at "No ")
2435 (add-text-properties (line-beginning-position) (line-end-position)
2436 '(mouse-face highlight
2437 help-echo "mouse-2, RET: select thread")))
2438 (forward-line 1))))))
2440 (defun gdb-threads-buffer-name ()
2441 (with-current-buffer gud-comint-buffer
2442 (concat "*threads of " (gdb-get-target-string) "*")))
2444 (defun gdb-display-threads-buffer ()
2445 "Display IDs of currently known threads."
2448 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2450 (defun gdb-frame-threads-buffer ()
2451 "Display IDs of currently known threads in a new frame."
2453 (let ((special-display-regexps (append special-display-regexps '(".*")))
2454 (special-display-frame-alist gdb-frame-parameters))
2455 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2457 (defvar gdb-threads-mode-map
2458 (let ((map (make-sparse-keymap)))
2459 (suppress-keymap map)
2460 (define-key map "q" 'kill-this-buffer)
2461 (define-key map "\r" 'gdb-threads-select)
2462 (define-key map [mouse-2] 'gdb-threads-select)
2463 (define-key map [follow-link] 'mouse-face)
2466 (defvar gdb-threads-font-lock-keywords
2467 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2468 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2469 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2470 "Font lock keywords used in `gdb-threads-mode'.")
2472 (defun gdb-threads-mode ()
2473 "Major mode for gdb threads.
2475 \\{gdb-threads-mode-map}"
2476 (kill-all-local-variables)
2477 (setq major-mode 'gdb-threads-mode)
2478 (setq mode-name "Threads")
2479 (setq buffer-read-only t)
2480 (buffer-disable-undo)
2481 (setq header-line-format gdb-breakpoints-header)
2482 (use-local-map gdb-threads-mode-map)
2483 (set (make-local-variable 'font-lock-defaults)
2484 '(gdb-threads-font-lock-keywords))
2485 (run-mode-hooks 'gdb-threads-mode-hook)
2486 ;; Force "info threads" onto queue.
2487 (lambda () (let ((gud-running nil)) (gdb-invalidate-threads))))
2489 (defun gdb-get-thread-number ()
2491 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2492 (match-string-no-properties 1)))
2494 (defun gdb-threads-select (&optional event)
2495 "Select the thread and display the relevant source."
2496 (interactive (list last-input-event))
2497 (if event (posn-set-point (event-end event)))
2499 (list (concat gdb-server-prefix "thread "
2500 (gdb-get-thread-number) "\n") 'ignore))
2501 (gud-display-frame))
2503 (defun gdb-thread-identification ()
2504 (setq mode-line-buffer-identification
2505 (list (car mode-line-buffer-identification)
2506 '(gdb-thread-indicator gdb-thread-indicator))))
2508 ;; Registers buffer.
2510 (defcustom gdb-all-registers nil
2511 "Non-nil means include floating-point registers."
2516 (gdb-set-buffer-rules 'gdb-registers-buffer
2517 'gdb-registers-buffer-name
2518 'gdb-registers-mode)
2520 (def-gdb-auto-updated-buffer gdb-registers-buffer
2521 gdb-invalidate-registers
2523 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2524 gdb-info-registers-handler
2525 gdb-info-registers-custom)
2527 (defun gdb-info-registers-custom ()
2528 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2530 (let ((buffer-read-only nil)
2532 (goto-char (point-min))
2533 (while (< (point) (point-max))
2534 (setq start (line-beginning-position))
2535 (setq end (line-end-position))
2536 (when (looking-at "^[^ ]+")
2537 (unless (string-equal (match-string 0) "The")
2538 (put-text-property start (match-end 0)
2539 'face font-lock-variable-name-face)
2540 (add-text-properties start end
2541 '(help-echo "mouse-2: edit value"
2542 mouse-face highlight))))
2543 (forward-line 1))))))
2545 (defun gdb-edit-register-value (&optional event)
2546 (interactive (list last-input-event))
2548 (if event (posn-set-point (event-end event)))
2550 (let* ((register (current-word))
2551 (value (read-string (format "New value (%s): " register))))
2553 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2556 (defvar gdb-registers-mode-map
2557 (let ((map (make-sparse-keymap)))
2558 (suppress-keymap map)
2559 (define-key map "\r" 'gdb-edit-register-value)
2560 (define-key map [mouse-2] 'gdb-edit-register-value)
2561 (define-key map " " 'gdb-all-registers)
2562 (define-key map "q" 'kill-this-buffer)
2565 (defvar gdb-locals-header
2566 `(,(propertize "Locals"
2567 'help-echo "mouse-1: select"
2568 'mouse-face 'mode-line-highlight
2571 (gdb-make-header-line-mouse-map
2573 (lambda (event) (interactive "e")
2574 (save-selected-window
2575 (select-window (posn-window (event-start event)))
2576 (set-window-dedicated-p (selected-window) nil)
2578 (gdb-get-buffer-create 'gdb-locals-buffer))
2579 (set-window-dedicated-p (selected-window) t)))))
2581 ,(propertize "Registers"
2582 'help-echo "mouse-1: select"
2583 'mouse-face 'mode-line-highlight
2586 (gdb-make-header-line-mouse-map
2588 (lambda (event) (interactive "e")
2589 (save-selected-window
2590 (select-window (posn-window (event-start event)))
2591 (set-window-dedicated-p (selected-window) nil)
2593 (gdb-get-buffer-create 'gdb-registers-buffer))
2594 (set-window-dedicated-p (selected-window) t)))))))
2596 (defun gdb-registers-mode ()
2597 "Major mode for gdb registers.
2599 \\{gdb-registers-mode-map}"
2600 (kill-all-local-variables)
2601 (setq major-mode 'gdb-registers-mode)
2602 (setq mode-name "Registers")
2603 (setq header-line-format gdb-locals-header)
2604 (setq buffer-read-only t)
2605 (buffer-disable-undo)
2606 (gdb-thread-identification)
2607 (use-local-map gdb-registers-mode-map)
2608 (run-mode-hooks 'gdb-registers-mode-hook)
2609 (if (string-equal gdb-version "pre-6.4")
2611 (if gdb-all-registers (setq mode-name "Registers:All"))
2612 'gdb-invalidate-registers)
2613 'gdb-invalidate-registers-1))
2615 (defun gdb-registers-buffer-name ()
2616 (with-current-buffer gud-comint-buffer
2617 (concat "*registers of " (gdb-get-target-string) "*")))
2619 (defun gdb-display-registers-buffer ()
2620 "Display integer register contents."
2623 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2625 (defun gdb-frame-registers-buffer ()
2626 "Display integer register contents in a new frame."
2628 (let ((special-display-regexps (append special-display-regexps '(".*")))
2629 (special-display-frame-alist gdb-frame-parameters))
2630 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2632 (defun gdb-all-registers ()
2633 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2635 (when (string-equal gdb-version "pre-6.4")
2636 (if gdb-all-registers
2638 (setq gdb-all-registers nil)
2639 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2640 (setq mode-name "Registers")))
2641 (setq gdb-all-registers t)
2642 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2643 (setq mode-name "Registers:All")))
2644 (message (format "Display of floating-point registers %sabled"
2645 (if gdb-all-registers "en" "dis")))
2646 (gdb-invalidate-registers)))
2651 (defcustom gdb-memory-repeat-count 32
2652 "Number of data items in memory window."
2657 (defcustom gdb-memory-format "x"
2658 "Display format of data items in memory window."
2659 :type '(choice (const :tag "Hexadecimal" "x")
2660 (const :tag "Signed decimal" "d")
2661 (const :tag "Unsigned decimal" "u")
2662 (const :tag "Octal" "o")
2663 (const :tag "Binary" "t"))
2667 (defcustom gdb-memory-unit "w"
2668 "Unit size of data items in memory window."
2669 :type '(choice (const :tag "Byte" "b")
2670 (const :tag "Halfword" "h")
2671 (const :tag "Word" "w")
2672 (const :tag "Giant word" "g"))
2676 (gdb-set-buffer-rules 'gdb-memory-buffer
2677 'gdb-memory-buffer-name
2680 (def-gdb-auto-updated-buffer gdb-memory-buffer
2681 gdb-invalidate-memory
2682 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2683 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2684 gdb-read-memory-handler
2685 gdb-read-memory-custom)
2687 (defun gdb-read-memory-custom ()
2689 (goto-char (point-min))
2690 (if (looking-at "0x[[:xdigit:]]+")
2691 (setq gdb-memory-address (match-string 0)))))
2693 (defvar gdb-memory-mode-map
2694 (let ((map (make-sparse-keymap)))
2695 (suppress-keymap map)
2696 (define-key map "q" 'kill-this-buffer)
2699 (defun gdb-memory-set-address (event)
2700 "Set the start memory address."
2702 (save-selected-window
2703 (select-window (posn-window (event-start event)))
2704 (let ((arg (read-from-minibuffer "Memory address: ")))
2705 (setq gdb-memory-address arg))
2706 (gdb-invalidate-memory)))
2708 (defun gdb-memory-set-repeat-count (event)
2709 "Set the number of data items in memory window."
2711 (save-selected-window
2712 (select-window (posn-window (event-start event)))
2713 (let* ((arg (read-from-minibuffer "Repeat count: "))
2714 (count (string-to-number arg)))
2716 (error "Positive numbers only")
2717 (customize-set-variable 'gdb-memory-repeat-count count)
2718 (gdb-invalidate-memory)))))
2720 (defun gdb-memory-format-binary ()
2721 "Set the display format to binary."
2723 (customize-set-variable 'gdb-memory-format "t")
2724 (gdb-invalidate-memory))
2726 (defun gdb-memory-format-octal ()
2727 "Set the display format to octal."
2729 (customize-set-variable 'gdb-memory-format "o")
2730 (gdb-invalidate-memory))
2732 (defun gdb-memory-format-unsigned ()
2733 "Set the display format to unsigned decimal."
2735 (customize-set-variable 'gdb-memory-format "u")
2736 (gdb-invalidate-memory))
2738 (defun gdb-memory-format-signed ()
2739 "Set the display format to decimal."
2741 (customize-set-variable 'gdb-memory-format "d")
2742 (gdb-invalidate-memory))
2744 (defun gdb-memory-format-hexadecimal ()
2745 "Set the display format to hexadecimal."
2747 (customize-set-variable 'gdb-memory-format "x")
2748 (gdb-invalidate-memory))
2750 (defvar gdb-memory-format-map
2751 (let ((map (make-sparse-keymap)))
2752 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2754 "Keymap to select format in the header line.")
2756 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2757 "Menu of display formats in the header line.")
2759 (define-key gdb-memory-format-menu [binary]
2760 '(menu-item "Binary" gdb-memory-format-binary
2761 :button (:radio . (equal gdb-memory-format "t"))))
2762 (define-key gdb-memory-format-menu [octal]
2763 '(menu-item "Octal" gdb-memory-format-octal
2764 :button (:radio . (equal gdb-memory-format "o"))))
2765 (define-key gdb-memory-format-menu [unsigned]
2766 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2767 :button (:radio . (equal gdb-memory-format "u"))))
2768 (define-key gdb-memory-format-menu [signed]
2769 '(menu-item "Signed Decimal" gdb-memory-format-signed
2770 :button (:radio . (equal gdb-memory-format "d"))))
2771 (define-key gdb-memory-format-menu [hexadecimal]
2772 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2773 :button (:radio . (equal gdb-memory-format "x"))))
2775 (defun gdb-memory-format-menu (event)
2777 (x-popup-menu event gdb-memory-format-menu))
2779 (defun gdb-memory-format-menu-1 (event)
2781 (save-selected-window
2782 (select-window (posn-window (event-start event)))
2783 (let* ((selection (gdb-memory-format-menu event))
2784 (binding (and selection (lookup-key gdb-memory-format-menu
2785 (vector (car selection))))))
2786 (if binding (call-interactively binding)))))
2788 (defun gdb-memory-unit-giant ()
2789 "Set the unit size to giant words (eight bytes)."
2791 (customize-set-variable 'gdb-memory-unit "g")
2792 (gdb-invalidate-memory))
2794 (defun gdb-memory-unit-word ()
2795 "Set the unit size to words (four bytes)."
2797 (customize-set-variable 'gdb-memory-unit "w")
2798 (gdb-invalidate-memory))
2800 (defun gdb-memory-unit-halfword ()
2801 "Set the unit size to halfwords (two bytes)."
2803 (customize-set-variable 'gdb-memory-unit "h")
2804 (gdb-invalidate-memory))
2806 (defun gdb-memory-unit-byte ()
2807 "Set the unit size to bytes."
2809 (customize-set-variable 'gdb-memory-unit "b")
2810 (gdb-invalidate-memory))
2812 (defvar gdb-memory-unit-map
2813 (let ((map (make-sparse-keymap)))
2814 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2816 "Keymap to select units in the header line.")
2818 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2819 "Menu of units in the header line.")
2821 (define-key gdb-memory-unit-menu [giantwords]
2822 '(menu-item "Giant words" gdb-memory-unit-giant
2823 :button (:radio . (equal gdb-memory-unit "g"))))
2824 (define-key gdb-memory-unit-menu [words]
2825 '(menu-item "Words" gdb-memory-unit-word
2826 :button (:radio . (equal gdb-memory-unit "w"))))
2827 (define-key gdb-memory-unit-menu [halfwords]
2828 '(menu-item "Halfwords" gdb-memory-unit-halfword
2829 :button (:radio . (equal gdb-memory-unit "h"))))
2830 (define-key gdb-memory-unit-menu [bytes]
2831 '(menu-item "Bytes" gdb-memory-unit-byte
2832 :button (:radio . (equal gdb-memory-unit "b"))))
2834 (defun gdb-memory-unit-menu (event)
2836 (x-popup-menu event gdb-memory-unit-menu))
2838 (defun gdb-memory-unit-menu-1 (event)
2840 (save-selected-window
2841 (select-window (posn-window (event-start event)))
2842 (let* ((selection (gdb-memory-unit-menu event))
2843 (binding (and selection (lookup-key gdb-memory-unit-menu
2844 (vector (car selection))))))
2845 (if binding (call-interactively binding)))))
2847 (defvar gdb-memory-font-lock-keywords
2848 '(;; <__function.name+n>
2849 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2851 "Font lock keywords used in `gdb-memory-mode'.")
2853 (defun gdb-memory-mode ()
2854 "Major mode for examining memory.
2856 \\{gdb-memory-mode-map}"
2857 (kill-all-local-variables)
2858 (setq major-mode 'gdb-memory-mode)
2859 (setq mode-name "Memory")
2860 (setq buffer-read-only t)
2861 (buffer-disable-undo)
2862 (use-local-map gdb-memory-mode-map)
2863 (setq header-line-format
2869 'face font-lock-warning-face
2870 'help-echo "mouse-1: decrement address"
2871 'mouse-face 'mode-line-highlight
2873 (gdb-make-header-line-mouse-map
2875 (lambda () (interactive)
2876 (let ((gdb-memory-address
2877 ;; Let GDB do the arithmetic.
2879 gdb-memory-address " - "
2881 (* gdb-memory-repeat-count
2882 (cond ((string= gdb-memory-unit "b") 1)
2883 ((string= gdb-memory-unit "h") 2)
2884 ((string= gdb-memory-unit "w") 4)
2885 ((string= gdb-memory-unit "g") 8)))))))
2886 (gdb-invalidate-memory)))))
2889 'face font-lock-warning-face
2890 'help-echo "mouse-1: increment address"
2891 'mouse-face 'mode-line-highlight
2892 'local-map (gdb-make-header-line-mouse-map
2894 (lambda () (interactive)
2895 (let ((gdb-memory-address nil))
2896 (gdb-invalidate-memory)))))
2898 (propertize gdb-memory-address
2899 'face font-lock-warning-face
2900 'help-echo "mouse-1: set memory address"
2901 'mouse-face 'mode-line-highlight
2902 'local-map (gdb-make-header-line-mouse-map
2904 #'gdb-memory-set-address))
2906 (propertize (number-to-string gdb-memory-repeat-count)
2907 'face font-lock-warning-face
2908 'help-echo "mouse-1: set repeat count"
2909 'mouse-face 'mode-line-highlight
2910 'local-map (gdb-make-header-line-mouse-map
2912 #'gdb-memory-set-repeat-count))
2914 (propertize gdb-memory-format
2915 'face font-lock-warning-face
2916 'help-echo "mouse-3: select display format"
2917 'mouse-face 'mode-line-highlight
2918 'local-map gdb-memory-format-map)
2920 (propertize gdb-memory-unit
2921 'face font-lock-warning-face
2922 'help-echo "mouse-3: select unit size"
2923 'mouse-face 'mode-line-highlight
2924 'local-map gdb-memory-unit-map))))
2925 (set (make-local-variable 'font-lock-defaults)
2926 '(gdb-memory-font-lock-keywords))
2927 (run-mode-hooks 'gdb-memory-mode-hook)
2928 'gdb-invalidate-memory)
2930 (defun gdb-memory-buffer-name ()
2931 (with-current-buffer gud-comint-buffer
2932 (concat "*memory of " (gdb-get-target-string) "*")))
2934 (defun gdb-display-memory-buffer ()
2935 "Display memory contents."
2938 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2940 (defun gdb-frame-memory-buffer ()
2941 "Display memory contents in a new frame."
2943 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2944 (special-display-frame-alist
2945 (cons '(left-fringe . 0)
2946 (cons '(right-fringe . 0)
2947 (cons '(width . 83) gdb-frame-parameters)))))
2948 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2953 (gdb-set-buffer-rules 'gdb-locals-buffer
2954 'gdb-locals-buffer-name
2957 (def-gdb-auto-update-trigger gdb-invalidate-locals
2958 (gdb-get-buffer 'gdb-locals-buffer)
2959 "server info locals\n"
2960 gdb-info-locals-handler)
2962 (defvar gdb-locals-watch-map
2963 (let ((map (make-sparse-keymap)))
2964 (suppress-keymap map)
2965 (define-key map "\r" (lambda () (interactive)
2968 (define-key map [mouse-2] (lambda (event) (interactive "e")
2969 (mouse-set-point event)
2973 "Keymap to create watch expression of a complex data type local variable.")
2975 (defconst gdb-struct-string
2976 (concat (propertize "[struct/union]"
2977 'mouse-face 'highlight
2978 'help-echo "mouse-2: create watch expression"
2979 'local-map gdb-locals-watch-map) "\n"))
2981 (defconst gdb-array-string
2982 (concat " " (propertize "[array]"
2983 'mouse-face 'highlight
2984 'help-echo "mouse-2: create watch expression"
2985 'local-map gdb-locals-watch-map) "\n"))
2987 ;; Abbreviate for arrays and structures.
2988 ;; These can be expanded using gud-display.
2989 (defun gdb-info-locals-handler ()
2990 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2991 gdb-pending-triggers))
2992 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2993 (with-current-buffer buf
2994 (goto-char (point-min))
2995 ;; Need this in case "set print pretty" is on.
2996 (while (re-search-forward "^[ }].*\n" nil t)
2997 (replace-match "" nil nil))
2998 (goto-char (point-min))
2999 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
3000 (replace-match gdb-struct-string nil nil))
3001 (goto-char (point-min))
3002 (while (re-search-forward "\\s-*{[^.].*\n" nil t)
3003 (replace-match gdb-array-string nil nil))))
3004 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3006 (with-current-buffer buf
3007 (let* ((window (get-buffer-window buf 0))
3008 (start (window-start window))
3009 (p (window-point window))
3010 (buffer-read-only nil))
3012 (insert-buffer-substring (gdb-get-buffer-create
3013 'gdb-partial-output-buffer))
3014 (set-window-start window start)
3015 (set-window-point window p)))))
3016 (run-hooks 'gdb-info-locals-hook))
3018 (defvar gdb-locals-mode-map
3019 (let ((map (make-sparse-keymap)))
3020 (suppress-keymap map)
3021 (define-key map "q" 'kill-this-buffer)
3024 (defun gdb-locals-mode ()
3025 "Major mode for gdb locals.
3027 \\{gdb-locals-mode-map}"
3028 (kill-all-local-variables)
3029 (setq major-mode 'gdb-locals-mode)
3030 (setq mode-name (concat "Locals:" gdb-selected-frame))
3031 (use-local-map gdb-locals-mode-map)
3032 (setq buffer-read-only t)
3033 (buffer-disable-undo)
3034 (setq header-line-format gdb-locals-header)
3035 (gdb-thread-identification)
3036 (set (make-local-variable 'font-lock-defaults)
3037 '(gdb-locals-font-lock-keywords))
3038 (run-mode-hooks 'gdb-locals-mode-hook)
3039 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3040 (string-equal gdb-version "pre-6.4"))
3041 'gdb-invalidate-locals
3042 'gdb-invalidate-locals-1))
3044 (defun gdb-locals-buffer-name ()
3045 (with-current-buffer gud-comint-buffer
3046 (concat "*locals of " (gdb-get-target-string) "*")))
3048 (defun gdb-display-locals-buffer ()
3049 "Display local variables of current stack and their values."
3052 (gdb-get-buffer-create 'gdb-locals-buffer) t))
3054 (defun gdb-frame-locals-buffer ()
3055 "Display local variables of current stack and their values in a new frame."
3057 (let ((special-display-regexps (append special-display-regexps '(".*")))
3058 (special-display-frame-alist gdb-frame-parameters))
3059 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
3062 ;;;; Window management
3063 (defun gdb-display-buffer (buf dedicated &optional frame)
3064 (let ((answer (get-buffer-window buf (or frame 0))))
3066 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3067 (let ((window (get-lru-window)))
3068 (let* ((largest (get-largest-window))
3069 (cur-size (window-height largest)))
3070 (setq answer (split-window largest))
3071 (set-window-buffer answer buf)
3072 (set-window-dedicated-p answer dedicated)))
3076 ;;; Shared keymap initialization:
3078 (let ((menu (make-sparse-keymap "GDB-Windows")))
3079 (define-key gud-menu-map [displays]
3080 `(menu-item "GDB-Windows" ,menu
3081 :help "Open a GDB-UI buffer in a new window."
3082 :visible (memq gud-minor-mode '(gdbmi gdba))))
3083 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3084 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3085 (define-key menu [inferior]
3086 '(menu-item "Separate IO" gdb-display-separate-io-buffer
3087 :enable gdb-use-separate-io-buffer))
3088 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3089 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3090 (define-key menu [disassembly]
3091 '("Disassembly" . gdb-display-assembler-buffer))
3092 (define-key menu [breakpoints]
3093 '("Breakpoints" . gdb-display-breakpoints-buffer))
3094 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3095 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
3097 (let ((menu (make-sparse-keymap "GDB-Frames")))
3098 (define-key gud-menu-map [frames]
3099 `(menu-item "GDB-Frames" ,menu
3100 :help "Open a GDB-UI buffer in a new frame."
3101 :visible (memq gud-minor-mode '(gdbmi gdba))))
3102 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3103 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3104 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3105 (define-key menu [inferior]
3106 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
3107 :enable gdb-use-separate-io-buffer))
3108 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3109 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
3110 (define-key menu [breakpoints]
3111 '("Breakpoints" . gdb-frame-breakpoints-buffer))
3112 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3113 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
3115 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
3116 (define-key gud-menu-map [ui]
3117 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
3118 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
3119 (define-key menu [gdb-customize]
3120 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3121 :help "Customize Gdb Graphical Mode options."))
3122 (define-key menu [gdb-find-source-frame]
3123 '(menu-item "Look For Source Frame" gdb-find-source-frame
3124 :visible (eq gud-minor-mode 'gdba)
3125 :help "Toggle looking for source frame further up call stack."
3126 :button (:toggle . gdb-find-source-frame)))
3127 (define-key menu [gdb-use-separate-io]
3128 '(menu-item "Separate IO" gdb-use-separate-io-buffer
3129 :visible (eq gud-minor-mode 'gdba)
3130 :help "Toggle separate IO for debugged program."
3131 :button (:toggle . gdb-use-separate-io-buffer)))
3132 (define-key menu [gdb-many-windows]
3133 '(menu-item "Display Other Windows" gdb-many-windows
3134 :help "Toggle display of locals, stack and breakpoint information"
3135 :button (:toggle . gdb-many-windows)))
3136 (define-key menu [gdb-restore-windows]
3137 '(menu-item "Restore Window Layout" gdb-restore-windows
3138 :help "Restore standard layout for debug session.")))
3140 (defun gdb-frame-gdb-buffer ()
3141 "Display GUD buffer in a new frame."
3143 (let ((special-display-regexps (append special-display-regexps '(".*")))
3144 (special-display-frame-alist
3145 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3146 gdb-frame-parameters)))
3147 (same-window-regexps nil))
3148 (display-buffer gud-comint-buffer)))
3150 (defun gdb-display-gdb-buffer ()
3151 "Display GUD buffer."
3153 (let ((same-window-regexps nil))
3154 (select-window (display-buffer gud-comint-buffer nil 0))))
3156 (defun gdb-set-window-buffer (name)
3157 (set-window-buffer (selected-window) (get-buffer name))
3158 (set-window-dedicated-p (selected-window) t))
3160 (defun gdb-setup-windows ()
3161 "Layout the window pattern for `gdb-many-windows'."
3162 (gdb-display-locals-buffer)
3163 (gdb-display-stack-buffer)
3164 (delete-other-windows)
3165 (gdb-display-breakpoints-buffer)
3166 (delete-other-windows)
3168 (pop-to-buffer gud-comint-buffer)
3169 (split-window nil ( / ( * (window-height) 3) 4))
3170 (split-window nil ( / (window-height) 3))
3171 (split-window-horizontally)
3173 (gdb-set-window-buffer (gdb-locals-buffer-name))
3176 (if gud-last-last-frame
3177 (gud-find-file (car gud-last-last-frame))
3179 (gud-find-file gdb-main-file)
3180 ;; Put buffer list in window if we
3181 ;; can't find a source file.
3182 (list-buffers-noselect))))
3183 (setq gdb-source-window (selected-window))
3184 (when gdb-use-separate-io-buffer
3185 (split-window-horizontally)
3187 (gdb-set-window-buffer
3188 (gdb-get-buffer-create 'gdb-inferior-io)))
3190 (gdb-set-window-buffer (gdb-stack-buffer-name))
3191 (split-window-horizontally)
3193 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3196 (defun gdb-restore-windows ()
3197 "Restore the basic arrangement of windows used by gdba.
3198 This arrangement depends on the value of `gdb-many-windows'."
3200 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3201 (delete-other-windows)
3202 (if gdb-many-windows
3204 (when (or gud-last-last-frame gdb-show-main)
3208 (if gud-last-last-frame
3209 (gud-find-file (car gud-last-last-frame))
3210 (gud-find-file gdb-main-file)))
3211 (setq gdb-source-window (selected-window))
3215 "Exit a debugging session cleanly.
3216 Kills the gdb buffers, and resets variables and the source buffers."
3217 (dolist (buffer (buffer-list))
3218 (unless (eq buffer gud-comint-buffer)
3219 (with-current-buffer buffer
3220 (if (memq gud-minor-mode '(gdbmi gdba))
3221 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3223 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3224 (setq gud-minor-mode nil)
3225 (kill-local-variable 'tool-bar-map)
3226 (kill-local-variable 'gdb-define-alist))))))
3227 (setq gdb-overlay-arrow-position nil)
3228 (setq overlay-arrow-variable-list
3229 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3230 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3231 (setq gdb-stack-position nil)
3232 (setq overlay-arrow-variable-list
3233 (delq 'gdb-stack-position overlay-arrow-variable-list))
3234 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3235 (setq gud-running nil)
3236 (setq gdb-active-process nil)
3237 (setq gdb-var-list nil)
3238 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3240 (defun gdb-source-info ()
3241 "Find the source file where the program starts and displays it with related
3243 (goto-char (point-min))
3244 (if (and (search-forward "Located in " nil t)
3245 (looking-at "\\S-+"))
3246 (setq gdb-main-file (match-string 0)))
3247 (goto-char (point-min))
3248 (if (search-forward "Includes preprocessor macro info." nil t)
3249 (setq gdb-macro-info t))
3250 (if gdb-many-windows
3252 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3254 (let ((pop-up-windows t))
3255 (display-buffer (gud-find-file gdb-main-file)))))
3258 (defun gdb-get-location (bptno line flag)
3259 "Find the directory containing the relevant source file.
3260 Put in buffer and place breakpoint icon."
3261 (goto-char (point-min))
3262 (catch 'file-not-found
3263 (if (search-forward "Located in " nil t)
3264 (when (looking-at "\\S-+")
3265 (delete (cons bptno "File not found") gdb-location-alist)
3266 (push (cons bptno (match-string 0)) gdb-location-alist))
3268 (unless (assoc bptno gdb-location-alist)
3269 (push (cons bptno "File not found") gdb-location-alist)
3270 (message-box "Cannot find source file for breakpoint location.\n\
3271 Add directory to search path for source files using the GDB command, dir."))
3272 (throw 'file-not-found nil))
3273 (with-current-buffer
3274 (find-file-noselect (match-string 0))
3276 ;; only want one breakpoint icon at each location
3278 (goto-line (string-to-number line))
3279 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
3281 (add-hook 'find-file-hook 'gdb-find-file-hook)
3283 (defun gdb-find-file-hook ()
3284 "Set up buffer for debugging if file is part of the source code
3285 of the current session."
3286 (if (and (buffer-name gud-comint-buffer)
3287 ;; in case gud or gdb-ui is just loaded
3289 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3291 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
3292 (if (member (if (string-equal gdb-version "pre-6.4")
3293 (file-name-nondirectory buffer-file-name)
3295 gdb-source-file-list)
3296 (with-current-buffer (find-buffer-visiting buffer-file-name)
3297 (gdb-init-buffer)))))
3300 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3301 "Put string PUTSTRING in front of POS in the current buffer.
3302 PUTSTRING is displayed by putting an overlay into the current buffer with a
3303 `before-string' string that has a `display' property whose value is
3305 (let ((string (make-string 1 ?x))
3306 (buffer (current-buffer)))
3307 (setq putstring (copy-sequence putstring))
3308 (let ((overlay (make-overlay pos pos buffer))
3310 (list (list 'margin 'left-margin) putstring))))
3311 (put-text-property 0 1 'display prop string)
3313 (add-text-properties 0 1 sprops string))
3314 (overlay-put overlay 'put-break t)
3315 (overlay-put overlay 'before-string string))))
3317 ;;from remove-images
3318 (defun gdb-remove-strings (start end &optional buffer)
3319 "Remove strings between START and END in BUFFER.
3320 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3321 BUFFER nil or omitted means use the current buffer."
3323 (setq buffer (current-buffer)))
3324 (dolist (overlay (overlays-in start end))
3325 (when (overlay-get overlay 'put-break)
3326 (delete-overlay overlay))))
3328 (defun gdb-put-breakpoint-icon (enabled bptno)
3329 (if (string-match "[0-9+]+\\." bptno)
3330 (setq enabled gdb-parent-bptno-enabled))
3331 (let ((start (- (line-beginning-position) 1))
3332 (end (+ (line-end-position) 1))
3333 (putstring (if enabled "B" "b"))
3334 (source-window (get-buffer-window (current-buffer) 0)))
3335 (add-text-properties
3336 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3339 (add-text-properties
3340 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3341 (add-text-properties
3342 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3343 (gdb-remove-breakpoint-icons start end)
3344 (if (display-images-p)
3345 (if (>= (or left-fringe-width
3346 (if source-window (car (window-fringes source-window)))
3347 gdb-buffer-fringe-width) 8)
3350 `(left-fringe breakpoint
3353 'breakpoint-disabled))
3355 'gdb-enabled enabled)
3356 (when (< left-margin-width 2)
3357 (save-current-buffer
3358 (setq left-margin-width 2)
3362 left-margin-width right-margin-width))))
3365 (or breakpoint-enabled-icon
3366 (setq breakpoint-enabled-icon
3367 (find-image `((:type xpm :data
3368 ,breakpoint-xpm-data
3369 :ascent 100 :pointer hand)
3371 ,breakpoint-enabled-pbm-data
3372 :ascent 100 :pointer hand)))))
3373 (or breakpoint-disabled-icon
3374 (setq breakpoint-disabled-icon
3375 (find-image `((:type xpm :data
3376 ,breakpoint-xpm-data
3377 :conversion disabled
3378 :ascent 100 :pointer hand)
3380 ,breakpoint-disabled-pbm-data
3381 :ascent 100 :pointer hand))))))
3385 (when (< left-margin-width 2)
3386 (save-current-buffer
3387 (setq left-margin-width 2)
3388 (let ((window (get-buffer-window (current-buffer) 0)))
3391 window left-margin-width right-margin-width)))))
3393 (propertize putstring
3394 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3397 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3398 (gdb-remove-strings start end)
3399 (if (display-images-p)
3400 (remove-images start end))
3402 (setq left-margin-width 0)
3403 (let ((window (get-buffer-window (current-buffer) 0)))
3406 window left-margin-width right-margin-width)))))
3410 ;; Assembler buffer.
3412 (gdb-set-buffer-rules 'gdb-assembler-buffer
3413 'gdb-assembler-buffer-name
3414 'gdb-assembler-mode)
3416 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3417 ;; window-start but keep the overlay arrow/current line visible.
3418 (defun gdb-assembler-handler ()
3419 (setq gdb-pending-triggers
3420 (delq 'gdb-invalidate-assembler
3421 gdb-pending-triggers))
3422 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3424 (with-current-buffer buf
3425 (let* ((window (get-buffer-window buf 0))
3426 (p (window-point window))
3427 (buffer-read-only nil))
3429 (insert-buffer-substring (gdb-get-buffer-create
3430 'gdb-partial-output-buffer))
3431 (set-window-point window p)))))
3432 ;; put customisation here
3433 (gdb-assembler-custom))
3435 (defun gdb-assembler-custom ()
3436 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3437 (pos 1) (address) (flag) (bptno))
3438 (with-current-buffer buffer
3440 (if (not (equal gdb-pc-address "main"))
3442 (goto-char (point-min))
3443 (if (and gdb-pc-address
3444 (search-forward gdb-pc-address nil t))
3448 (setq fringe-indicator-alist
3449 (if (string-equal gdb-frame-number "0")
3451 '((overlay-arrow . hollow-right-triangle))))
3452 (or gdb-overlay-arrow-position
3453 (setq gdb-overlay-arrow-position (make-marker)))
3454 (set-marker gdb-overlay-arrow-position (point))))))
3455 ;; remove all breakpoint-icons in assembler buffer before updating.
3456 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3457 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3458 (goto-char (point-min))
3459 (while (< (point) (- (point-max) 1))
3462 "\\([0-9]+\\.?[0-9]*\\).*?\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3463 (setq bptno (match-string 1))
3464 (setq flag (char-after (match-beginning 2)))
3465 (setq address (match-string 3))
3466 (with-current-buffer buffer
3468 (goto-char (point-min))
3469 (if (re-search-forward (concat "^0x0*" address) nil t)
3470 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
3471 (if (not (equal gdb-pc-address "main"))
3472 (with-current-buffer buffer
3473 (set-window-point (get-buffer-window buffer 0) pos)))))
3475 (defvar gdb-assembler-mode-map
3476 (let ((map (make-sparse-keymap)))
3477 (suppress-keymap map)
3478 (define-key map "q" 'kill-this-buffer)
3481 (defvar gdb-assembler-font-lock-keywords
3482 '(;; <__function.name+n>
3483 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3484 (1 font-lock-function-name-face))
3485 ;; 0xNNNNNNNN <__function.name+n>: opcode
3486 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3487 (4 font-lock-keyword-face))
3488 ;; %register(at least i386)
3489 ("%\\sw+" . font-lock-variable-name-face)
3490 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3491 (1 font-lock-comment-face)
3492 (2 font-lock-function-name-face))
3493 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3494 "Font lock keywords used in `gdb-assembler-mode'.")
3496 (defun gdb-assembler-mode ()
3497 "Major mode for viewing code assembler.
3499 \\{gdb-assembler-mode-map}"
3500 (kill-all-local-variables)
3501 (setq major-mode 'gdb-assembler-mode)
3502 (setq mode-name (concat "Machine:" gdb-selected-frame))
3503 (setq gdb-overlay-arrow-position nil)
3504 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3505 (setq fringes-outside-margins t)
3506 (setq buffer-read-only t)
3507 (buffer-disable-undo)
3508 (gdb-thread-identification)
3509 (use-local-map gdb-assembler-mode-map)
3510 (gdb-invalidate-assembler)
3511 (set (make-local-variable 'font-lock-defaults)
3512 '(gdb-assembler-font-lock-keywords))
3513 (run-mode-hooks 'gdb-assembler-mode-hook)
3514 'gdb-invalidate-assembler)
3516 (defun gdb-assembler-buffer-name ()
3517 (with-current-buffer gud-comint-buffer
3518 (concat "*disassembly of " (gdb-get-target-string) "*")))
3520 (defun gdb-display-assembler-buffer ()
3521 "Display disassembly view."
3523 (setq gdb-previous-frame nil)
3525 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3527 (defun gdb-frame-assembler-buffer ()
3528 "Display disassembly view in a new frame."
3530 (setq gdb-previous-frame nil)
3531 (let ((special-display-regexps (append special-display-regexps '(".*")))
3532 (special-display-frame-alist gdb-frame-parameters))
3533 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3535 ;; modified because if gdb-pc-address has changed value a new command
3536 ;; must be enqueued to update the buffer with the new output
3537 (defun gdb-invalidate-assembler (&optional ignored)
3538 (if (gdb-get-buffer 'gdb-assembler-buffer)
3540 (unless (and gdb-selected-frame
3541 (string-equal gdb-selected-frame gdb-previous-frame))
3542 (if (or (not (member 'gdb-invalidate-assembler
3543 gdb-pending-triggers))
3544 (not (equal (string-to-number gdb-pc-address)
3546 gdb-previous-frame-pc-address))))
3548 ;; take previous disassemble command, if any, off the queue
3549 (with-current-buffer gud-comint-buffer
3550 (let ((queue gdb-input-queue))
3551 (dolist (item queue)
3552 (if (equal (cdr item) '(gdb-assembler-handler))
3553 (setq gdb-input-queue
3554 (delete item gdb-input-queue))))))
3557 (concat gdb-server-prefix "disassemble " gdb-pc-address "\n")
3558 'gdb-assembler-handler))
3559 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3560 (setq gdb-previous-frame-pc-address gdb-pc-address)
3561 (setq gdb-previous-frame gdb-selected-frame)))))))
3563 (defun gdb-get-selected-frame ()
3564 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3566 (if (string-equal gdb-version "pre-6.4")
3568 (list (concat gdb-server-prefix "info frame\n")
3569 'gdb-frame-handler))
3571 (list "server interpreter mi -stack-info-frame\n"
3572 'gdb-frame-handler-1)))
3573 (push 'gdb-get-selected-frame gdb-pending-triggers))))
3575 (defun gdb-frame-handler ()
3576 (setq gdb-pending-triggers
3577 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3578 (goto-char (point-min))
3579 (when (re-search-forward
3580 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3581 (setq gdb-frame-number (match-string 1))
3582 (setq gdb-frame-address (match-string 2)))
3583 (goto-char (point-min))
3584 (when (re-search-forward ".*=\\s-+\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3585 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3587 (setq gdb-selected-frame (match-string 2))
3588 (if (gdb-get-buffer 'gdb-locals-buffer)
3589 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3590 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3591 (if (gdb-get-buffer 'gdb-assembler-buffer)
3592 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3593 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3594 (setq gdb-pc-address (match-string 1))
3595 (if (and (match-string 3) gud-overlay-arrow-position)
3596 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3597 (position (marker-position gud-overlay-arrow-position)))
3599 (string-equal (file-name-nondirectory
3600 (buffer-file-name buffer))
3601 (file-name-nondirectory (match-string 3))))
3602 (with-current-buffer buffer
3603 (setq fringe-indicator-alist
3604 (if (string-equal gdb-frame-number "0")
3606 '((overlay-arrow . hollow-right-triangle))))
3607 (set-marker gud-overlay-arrow-position position))))))
3608 (goto-char (point-min))
3609 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3610 (setq gdb-current-language (match-string 1)))
3611 (gdb-invalidate-assembler))
3614 ;; Code specific to GDB 6.4
3615 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3617 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3618 "Create list of source files for current GDB session.
3619 If buffers already exist for any of these files, `gud-minor-mode'
3621 (goto-char (point-min))
3622 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3623 (push (match-string 1) gdb-source-file-list))
3624 (dolist (buffer (buffer-list))
3625 (with-current-buffer buffer
3626 (when (member buffer-file-name gdb-source-file-list)
3627 (gdb-init-buffer))))
3628 (gdb-force-mode-line-update
3629 (propertize "ready" 'face font-lock-variable-name-face)))
3631 ;; Used for -stack-info-frame but could be used for -stack-list-frames too.
3632 (defconst gdb-stack-list-frames-regexp
3633 ".*?level=\"\\(.*?\\)\",.*?addr=\"\\(.*?\\)\",.*?func=\"\\(.*?\\)\",\
3634 \\(?:.*?file=\".*?\",.*?fullname=\"\\(.*?\\)\",.*?line=\"\\(.*?\\)\".*?}\\|\
3635 from=\"\\(.*?\\)\"\\)")
3637 (defun gdb-frame-handler-1 ()
3638 (setq gdb-pending-triggers
3639 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3640 (goto-char (point-min))
3641 (when (re-search-forward gdb-stack-list-frames-regexp nil t)
3642 (setq gdb-frame-number (match-string 1))
3643 (setq gdb-pc-address (match-string 2))
3644 (setq gdb-selected-frame (match-string 3))
3645 (if (gdb-get-buffer 'gdb-locals-buffer)
3646 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3647 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3648 (if (gdb-get-buffer 'gdb-assembler-buffer)
3649 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3650 (setq mode-name (concat "Machine:" gdb-selected-frame)))))
3651 (if (and (match-string 4) (match-string 5) gud-overlay-arrow-position)
3652 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3653 (position (marker-position gud-overlay-arrow-position)))
3655 (string-equal (file-name-nondirectory
3656 (buffer-file-name buffer))
3657 (file-name-nondirectory (match-string 4))))
3658 (with-current-buffer buffer
3659 (setq fringe-indicator-alist
3660 (if (string-equal gdb-frame-number "0")
3662 '((overlay-arrow . hollow-right-triangle))))
3663 (set-marker gud-overlay-arrow-position position)))))
3664 (gdb-invalidate-assembler))
3666 ; Uses "-var-list-children --all-values". Needs GDB 6.4 onwards.
3667 (defun gdb-var-list-children-1 (varnum)
3670 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3671 (concat "server interpreter mi \"-var-list-children --all-values \\\""
3673 (concat "-var-list-children --all-values \"" varnum "\"\n"))
3674 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3676 (defconst gdb-var-list-children-regexp-1
3677 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3678 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3679 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3681 (defun gdb-var-list-children-handler-1 (varnum)
3682 (goto-char (point-min))
3683 (let ((var-list nil))
3684 (catch 'child-already-watched
3685 (dolist (var gdb-var-list)
3686 (if (string-equal varnum (car var))
3689 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3690 (let ((varchild (list (match-string 1)
3694 (read (match-string 4))
3696 (if (assoc (car varchild) gdb-var-list)
3697 (throw 'child-already-watched nil))
3698 (push varchild var-list))))
3699 (push var var-list)))
3700 (setq gdb-var-list (nreverse var-list))))
3701 (gdb-speedbar-update))
3703 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3704 (defun gdb-var-update-1 ()
3705 (if (not (member 'gdb-var-update gdb-pending-triggers))
3709 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3710 "server interpreter mi \"-var-update --all-values *\"\n"
3711 "-var-update --all-values *\n")
3712 'gdb-var-update-handler-1))
3713 (push 'gdb-var-update gdb-pending-triggers))))
3715 (defconst gdb-var-update-regexp-1
3716 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3717 in_scope=\"\\(.*?\\)\".*?}")
3719 (defun gdb-var-update-handler-1 ()
3720 (dolist (var gdb-var-list)
3721 (setcar (nthcdr 5 var) nil))
3722 (goto-char (point-min))
3723 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3724 (let* ((varnum (match-string 1))
3725 (var (assoc varnum gdb-var-list)))
3727 (let ((match (match-string 3)))
3728 (cond ((string-equal match "false")
3729 (if gdb-delete-out-of-scope
3730 (gdb-var-delete-1 varnum)
3731 (setcar (nthcdr 5 var) 'out-of-scope)))
3732 ((string-equal match "true")
3733 (setcar (nthcdr 5 var) 'changed)
3734 (setcar (nthcdr 4 var)
3735 (read (match-string 2))))
3736 ((string-equal match "invalid")
3737 (gdb-var-delete-1 varnum)))))))
3738 (setq gdb-pending-triggers
3739 (delq 'gdb-var-update gdb-pending-triggers))
3740 (gdb-speedbar-update))
3742 ;; Registers buffer.
3744 (gdb-set-buffer-rules 'gdb-registers-buffer
3745 'gdb-registers-buffer-name
3746 'gdb-registers-mode)
3748 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3749 (gdb-get-buffer 'gdb-registers-buffer)
3750 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3751 "server interpreter mi \"-data-list-register-values x\"\n"
3752 "-data-list-register-values x\n")
3753 gdb-data-list-register-values-handler)
3755 (defconst gdb-data-list-register-values-regexp
3756 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3758 (defun gdb-data-list-register-values-handler ()
3759 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3760 gdb-pending-triggers))
3761 (goto-char (point-min))
3762 (if (re-search-forward gdb-error-regexp nil t)
3763 (let ((err (match-string 1)))
3764 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3765 (let ((buffer-read-only nil))
3767 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3769 (goto-char (point-min)))))
3770 (let ((register-list (reverse gdb-register-names))
3771 (register nil) (register-string nil) (register-values nil))
3772 (goto-char (point-min))
3773 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3774 (setq register (pop register-list))
3775 (setq register-string (concat register "\t" (match-string 2) "\n"))
3776 (if (member (match-string 1) gdb-changed-registers)
3777 (put-text-property 0 (length register-string)
3778 'face 'font-lock-warning-face
3780 (setq register-values
3781 (concat register-values register-string)))
3782 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3783 (with-current-buffer buf
3784 (let* ((window (get-buffer-window buf 0))
3785 (start (window-start window))
3786 (p (window-point window))
3787 (buffer-read-only nil))
3789 (insert register-values)
3790 (set-window-start window start)
3791 (set-window-point window p))))))
3792 (gdb-data-list-register-values-custom))
3794 (defun gdb-data-list-register-values-custom ()
3795 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3797 (let ((buffer-read-only nil)
3799 (goto-char (point-min))
3800 (while (< (point) (point-max))
3801 (setq start (line-beginning-position))
3802 (setq end (line-end-position))
3803 (when (looking-at "^[^\t]+")
3804 (unless (string-equal (match-string 0) "No registers.")
3805 (put-text-property start (match-end 0)
3806 'face font-lock-variable-name-face)
3807 (add-text-properties start end
3808 '(help-echo "mouse-2: edit value"
3809 mouse-face highlight))))
3810 (forward-line 1))))))
3812 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3813 (defun gdb-get-changed-registers ()
3814 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3815 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3819 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3820 "server interpreter mi -data-list-changed-registers\n"
3821 "-data-list-changed-registers\n")
3822 'gdb-get-changed-registers-handler))
3823 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3825 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3827 (defun gdb-get-changed-registers-handler ()
3828 (setq gdb-pending-triggers
3829 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3830 (setq gdb-changed-registers nil)
3831 (goto-char (point-min))
3832 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3833 (push (match-string 1) gdb-changed-registers)))
3838 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3839 (gdb-set-buffer-rules 'gdb-locals-buffer
3840 'gdb-locals-buffer-name
3843 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3844 (gdb-get-buffer 'gdb-locals-buffer)
3845 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3846 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3847 "-stack-list-locals --simple-values\n")
3848 gdb-stack-list-locals-handler)
3850 (defconst gdb-stack-list-locals-regexp
3851 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3853 (defvar gdb-locals-watch-map-1
3854 (let ((map (make-sparse-keymap)))
3855 (suppress-keymap map)
3856 (define-key map "\r" 'gud-watch)
3857 (define-key map [mouse-2] 'gud-watch)
3859 "Keymap to create watch expression of a complex data type local variable.")
3861 (defvar gdb-edit-locals-map-1
3862 (let ((map (make-sparse-keymap)))
3863 (suppress-keymap map)
3864 (define-key map "\r" 'gdb-edit-locals-value)
3865 (define-key map [mouse-2] 'gdb-edit-locals-value)
3867 "Keymap to edit value of a simple data type local variable.")
3869 (defun gdb-edit-locals-value (&optional event)
3870 "Assign a value to a variable displayed in the locals buffer."
3871 (interactive (list last-input-event))
3873 (if event (posn-set-point (event-end event)))
3875 (let* ((var (current-word))
3876 (value (read-string (format "New value (%s): " var))))
3878 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3881 ;; Dont display values of arrays or structures.
3882 ;; These can be expanded using gud-watch.
3883 (defun gdb-stack-list-locals-handler ()
3884 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3885 gdb-pending-triggers))
3886 (goto-char (point-min))
3887 (if (re-search-forward gdb-error-regexp nil t)
3888 (let ((err (match-string 1)))
3889 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3890 (let ((buffer-read-only nil))
3893 (goto-char (point-min)))))
3894 (let (local locals-list)
3895 (goto-char (point-min))
3896 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3897 (let ((local (list (match-string 1)
3900 (if (looking-at ",value=\\(\".*\"\\).*?}")
3901 (setcar (nthcdr 2 local) (read (match-string 1))))
3902 (push local locals-list)))
3903 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3904 (and buf (with-current-buffer buf
3905 (let* ((window (get-buffer-window buf 0))
3906 (start (window-start window))
3907 (p (window-point window))
3908 (buffer-read-only nil) (name) (value))
3910 (dolist (local locals-list)
3911 (setq name (car local))
3912 (setq value (nth 2 local))
3914 (string-match "^\\0x" value))
3915 (add-text-properties 0 (length name)
3916 `(mouse-face highlight
3917 help-echo "mouse-2: create watch expression"
3918 local-map ,gdb-locals-watch-map-1)
3920 (add-text-properties 0 (length value)
3921 `(mouse-face highlight
3922 help-echo "mouse-2: edit value"
3923 local-map ,gdb-edit-locals-map-1)
3926 (concat name "\t" (nth 1 local)
3928 (set-window-start window start)
3929 (set-window-point window p))))))))
3931 (defun gdb-get-register-names ()
3932 "Create a list of register names."
3933 (goto-char (point-min))
3934 (setq gdb-register-names nil)
3935 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3936 (push (match-string 1) gdb-register-names)))
3940 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3941 ;;; gdb-ui.el ends here