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 (setq gdb-pending-triggers
818 (delq 'gdb-speedbar-timer gdb-pending-triggers))
821 (defun gdb-var-evaluate-expression-handler (varnum changed)
822 (goto-char (point-min))
823 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t)
824 (setq gdb-pending-triggers
825 (delq (string-to-number (match-string 1)) gdb-pending-triggers))
826 (let ((var (assoc varnum gdb-var-list)))
828 (if changed (setcar (nthcdr 5 var) 'changed))
829 (setcar (nthcdr 4 var) (read (match-string 2)))))
830 (gdb-speedbar-update))
832 (defun gdb-var-list-children (varnum)
834 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
835 `(lambda () (gdb-var-list-children-handler ,varnum)))))
837 (defconst gdb-var-list-children-regexp
838 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
839 numchild=\"\\(.*?\\)\"\\(}\\|,.*?\\(type=\"\\(.*?\\)\"\\)?.*?}\\)")
841 (defun gdb-var-list-children-handler (varnum)
842 (goto-char (point-min))
843 (let ((var-list nil))
844 (catch 'child-already-watched
845 (dolist (var gdb-var-list)
846 (if (string-equal varnum (car var))
849 (while (re-search-forward gdb-var-list-children-regexp nil t)
850 (let ((varchild (list (match-string 1)
855 (if (assoc (car varchild) gdb-var-list)
856 (throw 'child-already-watched nil))
857 (push varchild var-list)
861 "server interpreter mi \"0-var-evaluate-expression "
862 (car varchild) "\"\n")
863 `(lambda () (gdb-var-evaluate-expression-handler
864 ,(car varchild) nil)))))))
865 (push var var-list)))
866 (setq gdb-var-list (nreverse var-list)))))
868 (defun gdb-var-update ()
869 (when (not (member 'gdb-var-update gdb-pending-triggers))
871 (list "server interpreter mi \"-var-update *\"\n"
872 'gdb-var-update-handler))
873 (push 'gdb-var-update gdb-pending-triggers)))
875 (defconst gdb-var-update-regexp
876 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
877 type_changed=\".*?\".*?}")
879 (defun gdb-var-update-handler ()
880 (dolist (var gdb-var-list)
881 (setcar (nthcdr 5 var) nil))
882 (goto-char (point-min))
884 (while (re-search-forward gdb-var-update-regexp nil t)
885 (let ((varnum (match-string 1)))
886 (if (string-equal (match-string 2) "false")
887 (let ((var (assoc varnum gdb-var-list)))
888 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
890 (push n gdb-pending-triggers)
893 (concat "server interpreter mi \"" (number-to-string n)
894 "-var-evaluate-expression " varnum "\"\n")
895 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))))))
896 (setq gdb-pending-triggers
897 (delq 'gdb-var-update gdb-pending-triggers)))
899 (defun gdb-var-set-format (format)
900 "Set the output format for a variable displayed in the speedbar."
901 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
905 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
906 (concat "server interpreter mi \"-var-set-format "
907 varnum " " format "\"\n")
908 (concat "-var-set-format " varnum " " format "\n"))
909 `(lambda () (gdb-var-set-format-handler ,varnum))))))
911 (defconst gdb-var-set-format-regexp
912 "format=\"\\(.*?\\)\",.*value=\"\\(.*?\\)\"")
914 (defun gdb-var-set-format-handler (varnum)
915 (goto-char (point-min))
916 (if (re-search-forward gdb-var-set-format-regexp nil t)
917 (let ((var (assoc varnum gdb-var-list)))
918 (setcar (nthcdr 4 var) (match-string 2))
919 (gdb-var-update-1))))
921 (defun gdb-var-delete-1 (varnum)
924 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
925 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
926 (concat "-var-delete " varnum "\n"))
928 (setq gdb-var-list (delq var gdb-var-list))
929 (dolist (varchild gdb-var-list)
930 (if (string-match (concat (car var) "\\.") (car varchild))
931 (setq gdb-var-list (delq varchild gdb-var-list)))))
933 (defun gdb-var-delete ()
934 "Delete watch expression at point from the speedbar."
936 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
938 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
940 (if (string-match "\\." (car var))
941 (message-box "Can only delete a root expression")
942 (gdb-var-delete-1 varnum)))))
944 (defun gdb-var-delete-children (varnum)
945 "Delete children of variable object at point from the speedbar."
948 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
949 (concat "server interpreter mi \"-var-delete -c " varnum "\"\n")
950 (concat "-var-delete -c " varnum "\n")) 'ignore)))
952 (defun gdb-edit-value (text token indent)
953 "Assign a value to a variable displayed in the speedbar."
954 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
955 (varnum (car var)) (value))
956 (setq value (read-string "New value: "))
959 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
960 (concat "server interpreter mi \"-var-assign "
961 varnum " " value "\"\n")
962 (concat "-var-assign " varnum " " value "\n"))
963 `(lambda () (gdb-edit-value-handler ,value))))))
965 (defun gdb-edit-value-handler (value)
966 (goto-char (point-min))
967 (if (re-search-forward gdb-error-regexp nil t)
968 (message-box "Invalid number or expression (%s)" value)))
970 (defcustom gdb-show-changed-values t
971 "If non-nil change the face of out of scope variables and changed values.
972 Out of scope variables are suppressed with `shadow' face.
973 Changed values are highlighted with the face `font-lock-warning-face'."
978 (defcustom gdb-max-children 40
979 "Maximum number of children before expansion requires confirmation."
984 (defcustom gdb-delete-out-of-scope t
985 "If non-nil delete watch expressions automatically when they go out of scope."
990 (defun gdb-speedbar-expand-node (text token indent)
991 "Expand the node the user clicked on.
992 TEXT is the text of the button we clicked on, a + or - item.
993 TOKEN is data related to this node.
994 INDENT is the current indentation depth."
995 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
997 (cond ((string-match "+" text) ;expand this node
998 (let* ((var (assoc token gdb-var-list))
999 (expr (nth 1 var)) (children (nth 2 var)))
1000 (if (or (<= (string-to-number children) gdb-max-children)
1003 "%s has %s children. Continue? " expr children)))
1004 (if (and (eq (buffer-local-value
1005 'gud-minor-mode gud-comint-buffer) 'gdba)
1006 (string-equal gdb-version "pre-6.4"))
1007 (gdb-var-list-children token)
1008 (gdb-var-list-children-1 token)))))
1009 ((string-match "-" text) ;contract this node
1010 (dolist (var gdb-var-list)
1011 (if (string-match (concat token "\\.") (car var))
1012 (setq gdb-var-list (delq var gdb-var-list))))
1013 (gdb-var-delete-children token)
1014 (speedbar-change-expand-button-char ?+)
1015 (speedbar-delete-subblock indent))
1016 (t (error "Ooops... not sure what to do")))
1017 (speedbar-center-buffer-smartly))
1018 (message-box "GUD session has been killed")))
1020 (defun gdb-get-target-string ()
1021 (with-current-buffer gud-comint-buffer
1028 ;; Each buffer has a TYPE -- a symbol that identifies the function
1029 ;; of that particular buffer.
1031 ;; The usual gdb interaction buffer is given the type `gdba' and
1032 ;; is constructed specially.
1034 ;; Others are constructed by gdb-get-buffer-create and
1035 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
1037 (defvar gdb-buffer-rules-assoc '())
1039 (defun gdb-get-buffer (key)
1040 "Return the gdb buffer tagged with type KEY.
1041 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1043 (gdb-look-for-tagged-buffer key (buffer-list))))
1045 (defun gdb-get-buffer-create (key)
1046 "Create a new gdb buffer of the type specified by KEY.
1047 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1048 (or (gdb-get-buffer key)
1049 (let* ((rules (assoc key gdb-buffer-rules-assoc))
1050 (name (funcall (gdb-rules-name-maker rules)))
1051 (new (get-buffer-create name)))
1052 (with-current-buffer new
1054 (if (cdr (cdr rules))
1055 (setq trigger (funcall (car (cdr (cdr rules))))))
1056 (setq gdb-buffer-type key)
1057 (set (make-local-variable 'gud-minor-mode)
1058 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1059 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1060 (if trigger (funcall trigger)))
1063 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
1065 (defun gdb-look-for-tagged-buffer (key bufs)
1067 (while (and (not retval) bufs)
1068 (set-buffer (car bufs))
1069 (if (eq gdb-buffer-type key)
1070 (setq retval (car bufs)))
1071 (setq bufs (cdr bufs)))
1075 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1076 ;; at least one and possible more functions. The functions have these
1077 ;; roles in defining a buffer type:
1079 ;; NAME - Return a name for this buffer type.
1081 ;; The remaining function(s) are optional:
1083 ;; MODE - called in a new buffer with no arguments, should establish
1084 ;; the proper mode for the buffer.
1087 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1088 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
1090 (setcdr binding rules)
1091 (push (cons buffer-type rules)
1092 gdb-buffer-rules-assoc))))
1094 ;; GUD buffers are an exception to the rules
1095 (gdb-set-buffer-rules 'gdba 'error)
1097 ;; Partial-output buffer : This accumulates output from a command executed on
1098 ;; behalf of emacs (rather than the user).
1100 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1101 'gdb-partial-output-name)
1103 (defun gdb-partial-output-name ()
1104 (concat " *partial-output-"
1105 (gdb-get-target-string)
1109 (gdb-set-buffer-rules 'gdb-inferior-io
1110 'gdb-inferior-io-name
1111 'gdb-inferior-io-mode)
1113 (defun gdb-inferior-io-name ()
1114 (concat "*input/output of "
1115 (gdb-get-target-string)
1118 (defun gdb-display-separate-io-buffer ()
1119 "Display IO of debugged program in a separate window."
1121 (if gdb-use-separate-io-buffer
1123 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1125 (defconst gdb-frame-parameters
1126 '((height . 14) (width . 80)
1128 (tool-bar-lines . nil)
1129 (menu-bar-lines . nil)
1130 (minibuffer . nil)))
1132 (defun gdb-frame-separate-io-buffer ()
1133 "Display IO of debugged program in a new frame."
1135 (if gdb-use-separate-io-buffer
1136 (let ((special-display-regexps (append special-display-regexps '(".*")))
1137 (special-display-frame-alist gdb-frame-parameters))
1138 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1140 (defvar gdb-inferior-io-mode-map
1141 (let ((map (make-sparse-keymap)))
1142 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1143 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1144 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1145 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1146 (define-key map "\C-d" 'gdb-separate-io-eof)
1149 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1150 "Major mode for gdb inferior-io."
1151 :syntax-table nil :abbrev-table nil
1152 ;; We want to use comint because it has various nifty and familiar
1153 ;; features. We don't need a process, but comint wants one, so create
1155 (make-comint-in-buffer
1156 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1157 (current-buffer) "hexl")
1158 (setq comint-input-sender 'gdb-inferior-io-sender))
1160 (defun gdb-inferior-io-sender (proc string)
1161 ;; PROC is the pseudo-process created to satisfy comint.
1162 (with-current-buffer (process-buffer proc)
1163 (setq proc (get-buffer-process gud-comint-buffer))
1164 (process-send-string proc string)
1165 (process-send-string proc "\n")))
1167 (defun gdb-separate-io-interrupt ()
1168 "Interrupt the program being debugged."
1171 (get-buffer-process gud-comint-buffer) comint-ptyp))
1173 (defun gdb-separate-io-quit ()
1174 "Send quit signal to the program being debugged."
1177 (get-buffer-process gud-comint-buffer) comint-ptyp))
1179 (defun gdb-separate-io-stop ()
1180 "Stop the program being debugged."
1183 (get-buffer-process gud-comint-buffer) comint-ptyp))
1185 (defun gdb-separate-io-eof ()
1186 "Send end-of-file to the program being debugged."
1189 (get-buffer-process gud-comint-buffer)))
1192 ;; gdb communications
1195 ;; INPUT: things sent to gdb
1197 ;; The queues are lists. Each element is either a string (indicating user or
1198 ;; user-like input) or a list of the form:
1200 ;; (INPUT-STRING HANDLER-FN)
1202 ;; The handler function will be called from the partial-output buffer when the
1203 ;; command completes. This is the way to write commands which invoke gdb
1204 ;; commands autonomously.
1206 ;; These lists are consumed tail first.
1209 (defun gdb-send (proc string)
1210 "A comint send filter for gdb.
1211 This filter may simply queue input for a later time."
1214 (with-current-buffer gud-comint-buffer
1215 (let ((inhibit-read-only t))
1216 (remove-text-properties (point-min) (point-max) '(face))))
1219 (let ((item (concat string "\n")))
1220 (if gdb-enable-debug (push (cons 'send item) gdb-debug-log))
1221 (process-send-string proc item)))
1222 (if (string-match "\\\\\\'" string)
1223 (setq gdb-continuation (concat gdb-continuation string "\n"))
1225 gdb-continuation string
1226 (if (not comint-input-sender-no-newline) "\n"))))
1227 (gdb-enqueue-input item)
1228 (setq gdb-continuation nil)))))
1229 (push (concat string "\n") gdb-early-user-input)))
1231 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1232 ;; is a query, or other non-top-level prompt.
1234 (defun gdb-enqueue-input (item)
1235 (if (not gud-running)
1238 (gdb-send-item item)
1239 (setq gdb-prompting nil))
1240 (push item gdb-input-queue))))
1242 (defun gdb-dequeue-input ()
1243 (let ((queue gdb-input-queue))
1245 (let ((last (car (last queue))))
1246 (unless (nbutlast queue) (setq gdb-input-queue '()))
1248 ;; This should be nil here anyway but set it just to make sure.
1249 (setq gdb-pending-triggers nil))))
1251 (defun gdb-send-item (item)
1252 (setq gdb-flush-pending-output nil)
1253 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1254 (setq gdb-current-item item)
1255 (let ((process (get-buffer-process gud-comint-buffer)))
1256 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1259 (setq gdb-output-sink 'user)
1260 (process-send-string process item))
1262 (gdb-clear-partial-output)
1263 (setq gdb-output-sink 'pre-emacs)
1264 (process-send-string process
1266 ;; case: eq gud-minor-mode 'gdbmi
1267 (gdb-clear-partial-output)
1268 (setq gdb-output-sink 'emacs)
1269 (process-send-string process (car item)))))
1272 ;; output -- things gdb prints to emacs
1274 ;; GDB output is a stream interrupted by annotations.
1275 ;; Annotations can be recognized by their beginning
1276 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1278 ;; The tag is a string obeying symbol syntax.
1280 ;; The optional part `<opt>' can be either the empty string
1281 ;; or a space followed by more data relating to the annotation.
1282 ;; For example, the SOURCE annotation is followed by a filename,
1283 ;; line number and various useless goo. This data must not include
1287 (defcustom gud-gdb-command-name "gdb --annotate=3"
1288 "Default command to execute an executable under the GDB debugger.
1289 The option \"--annotate=3\" must be included in this value if you
1290 want the GDB Graphical Interface."
1295 (defvar gdb-annotation-rules
1296 '(("pre-prompt" gdb-pre-prompt)
1297 ("prompt" gdb-prompt)
1298 ("commands" gdb-subprompt)
1299 ("overload-choice" gdb-subprompt)
1300 ("query" gdb-subprompt)
1301 ;; Need this prompt for GDB 6.1
1302 ("nquery" gdb-subprompt)
1303 ("prompt-for-continue" gdb-subprompt)
1304 ("post-prompt" gdb-post-prompt)
1305 ("source" gdb-source)
1306 ("starting" gdb-starting)
1307 ("exited" gdb-exited)
1308 ("signalled" gdb-signalled)
1309 ("signal" gdb-signal)
1310 ("breakpoint" gdb-stopping)
1311 ("watchpoint" gdb-stopping)
1312 ("frame-begin" gdb-frame-begin)
1313 ("stopped" gdb-stopped)
1314 ("error-begin" gdb-error)
1316 ("new-thread" (lambda (ignored) (gdb-get-buffer-create 'gdb-threads-buffer))))
1317 "An assoc mapping annotation tags to functions which process them.")
1320 (setq gdb-flush-pending-output t)
1321 (setq gud-running nil)
1322 (gdb-force-mode-line-update
1323 (propertize "stopped" 'face font-lock-warning-face))
1324 (setq gdb-output-sink 'user)
1325 (setq gdb-input-queue nil)
1326 (setq gdb-pending-triggers nil)
1327 (setq gdb-prompting t))
1329 (defconst gdb-source-spec-regexp
1330 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1332 ;; Do not use this except as an annotation handler.
1333 (defun gdb-source (args)
1334 (string-match gdb-source-spec-regexp args)
1335 ;; Extract the frame position from the marker.
1336 (setq gud-last-frame
1338 (match-string 1 args)
1339 (string-to-number (match-string 2 args))))
1340 (setq gdb-pc-address (match-string 3 args))
1341 ;; cover for auto-display output which comes *before*
1342 ;; stopped annotation
1343 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1345 (defun gdb-pre-prompt (ignored)
1346 "An annotation handler for `pre-prompt'.
1347 This terminates the collection of output from a previous command if that
1348 happens to be in effect."
1349 (setq gdb-error nil)
1350 (let ((sink gdb-output-sink))
1354 (setq gdb-output-sink 'post-emacs))
1357 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1359 (defun gdb-prompt (ignored)
1360 "An annotation handler for `prompt'.
1361 This sends the next command (if any) to gdb."
1362 (when gdb-first-prompt
1363 (gdb-force-mode-line-update
1364 (propertize "initializing..." 'face font-lock-variable-name-face))
1366 (setq gdb-first-prompt nil))
1367 (let ((sink gdb-output-sink))
1370 ((eq sink 'post-emacs)
1371 (setq gdb-output-sink 'user)
1373 (car (cdr gdb-current-item))))
1374 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1375 (funcall handler))))
1378 (error "Phase error in gdb-prompt (got %s)" sink))))
1379 (let ((input (gdb-dequeue-input)))
1381 (gdb-send-item input)
1383 (setq gdb-prompting t)
1385 (setq gdb-early-user-input (nreverse gdb-early-user-input))
1386 (while gdb-early-user-input
1387 (gdb-enqueue-input (car gdb-early-user-input))
1388 (setq gdb-early-user-input (cdr gdb-early-user-input)))))))
1390 (defun gdb-subprompt (ignored)
1391 "An annotation handler for non-top-level prompts."
1392 (setq gdb-prompting t))
1394 (defun gdb-starting (ignored)
1395 "An annotation handler for `starting'.
1396 This says that I/O for the subprocess is now the program being debugged,
1398 (setq gdb-active-process t)
1399 (setq gdb-printing t)
1400 (let ((sink gdb-output-sink))
1404 (setq gud-running t)
1405 (setq gdb-stack-update t)
1406 ;; Temporarily set gud-running to nil to force "info stack" onto queue.
1407 (let ((gud-running nil))
1408 (gdb-invalidate-frames))
1409 (setq gdb-inferior-status "running")
1410 (setq gdb-signalled nil)
1411 (gdb-force-mode-line-update
1412 (propertize gdb-inferior-status 'face font-lock-type-face))
1413 (gdb-remove-text-properties)
1414 (setq gud-old-arrow gud-overlay-arrow-position)
1415 (setq gud-overlay-arrow-position nil)
1416 (setq gdb-overlay-arrow-position nil)
1417 (setq gdb-stack-position nil)
1418 (if gdb-use-separate-io-buffer
1419 (setq gdb-output-sink 'inferior))))
1422 (error "Unexpected `starting' annotation")))))
1424 (defun gdb-signal (ignored)
1425 (setq gdb-inferior-status "signal")
1426 (gdb-force-mode-line-update
1427 (propertize gdb-inferior-status 'face font-lock-warning-face))
1428 (gdb-stopping ignored))
1430 (defun gdb-stopping (ignored)
1431 "An annotation handler for `breakpoint' and other annotations.
1432 They say that I/O for the subprocess is now GDB, not the program
1434 (if gdb-use-separate-io-buffer
1435 (let ((sink gdb-output-sink))
1437 ((eq sink 'inferior)
1438 (setq gdb-output-sink 'user))
1441 (error "Unexpected stopping annotation"))))))
1443 (defun gdb-exited (ignored)
1444 "An annotation handler for `exited' and `signalled'.
1445 They say that I/O for the subprocess is now GDB, not the program
1446 being debugged and that the program is no longer running. This
1447 function is used to change the focus of GUD tooltips to #define
1449 (setq gdb-active-process nil)
1450 (setq gud-overlay-arrow-position nil)
1451 (setq gdb-overlay-arrow-position nil)
1452 (setq gdb-stack-position nil)
1453 (setq gud-old-arrow nil)
1454 (setq gdb-inferior-status "exited")
1455 (gdb-force-mode-line-update
1456 (propertize gdb-inferior-status 'face font-lock-warning-face))
1457 (gdb-stopping ignored))
1459 (defun gdb-signalled (ignored)
1460 (setq gdb-signalled t))
1462 (defun gdb-frame-begin (ignored)
1463 (setq gdb-frame-begin t)
1464 (setq gdb-printing nil)
1465 (let ((sink gdb-output-sink))
1467 ((eq sink 'inferior)
1468 (setq gdb-output-sink 'user))
1470 ((eq sink 'emacs) t)
1473 (error "Unexpected frame-begin annotation (%S)" sink)))))
1475 (defcustom gdb-same-frame focus-follows-mouse
1476 "Non-nil means pop up GUD buffer in same frame."
1481 (defcustom gdb-find-source-frame nil
1482 "Non-nil means try to find a source frame further up stack e.g after signal."
1487 (defun gdb-find-source-frame (arg)
1488 "Toggle looking for a source frame further up call stack.
1489 The code associated with current (innermost) frame may not have
1490 been compiled with debug information, e.g., C library routine.
1491 With prefix argument ARG, look for a source frame further up
1492 stack to display in the source buffer if ARG is positive,
1493 otherwise don't look further up."
1495 (setq gdb-find-source-frame
1497 (not gdb-find-source-frame)
1498 (> (prefix-numeric-value arg) 0)))
1499 (message (format "Looking for source frame %sabled"
1500 (if gdb-find-source-frame "en" "dis"))))
1502 (defun gdb-stopped (ignored)
1503 "An annotation handler for `stopped'.
1504 It is just like `gdb-stopping', except that if we already set the output
1505 sink to `user' in `gdb-stopping', that is fine."
1506 (setq gud-running nil)
1507 (unless (or gud-overlay-arrow-position gud-last-frame)
1508 (if (and gdb-frame-begin gdb-printing)
1509 (setq gud-overlay-arrow-position gud-old-arrow)
1510 ;;Pop up GUD buffer to display current frame when it doesn't have source
1511 ;;information i.e if not compiled with -g as with libc routines generally.
1513 (gdb-display-gdb-buffer)
1514 (gdb-frame-gdb-buffer))
1515 (if gdb-find-source-frame
1516 ;;Try to find source further up stack e.g after signal.
1517 (setq gdb-look-up-stack
1518 (if (gdb-get-buffer 'gdb-stack-buffer)
1521 (gdb-get-buffer-create 'gdb-stack-buffer)
1522 (gdb-invalidate-frames)
1524 (unless (member gdb-inferior-status '("exited" "signal"))
1525 (setq gdb-active-process t) ;Just for attaching case.
1526 (setq gdb-inferior-status "stopped")
1527 (gdb-force-mode-line-update
1528 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1529 (let ((sink gdb-output-sink))
1531 ((eq sink 'inferior)
1532 (setq gdb-output-sink 'user))
1536 (error "Unexpected stopped annotation"))))
1537 (if gdb-signalled (gdb-exited ignored)))
1539 (defun gdb-error (ignored)
1540 (setq gdb-error (not gdb-error)))
1542 (defun gdb-post-prompt (ignored)
1543 "An annotation handler for `post-prompt'.
1544 This begins the collection of output from the current command if that
1545 happens to be appropriate."
1546 ;; Don't add to queue if there outstanding items or gdb-version is not known
1548 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1549 (gdb-get-selected-frame)
1550 (gdb-invalidate-frames)
1551 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1552 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1553 (gdb-invalidate-breakpoints)
1554 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1555 ;; so gdb-pc-address is updated.
1556 ;; (gdb-invalidate-assembler)
1558 (if (string-equal gdb-version "pre-6.4")
1559 (gdb-invalidate-registers)
1560 (gdb-get-changed-registers)
1561 (gdb-invalidate-registers-1))
1563 (gdb-invalidate-memory)
1564 (if (string-equal gdb-version "pre-6.4")
1565 (gdb-invalidate-locals)
1566 (gdb-invalidate-locals-1))
1568 (gdb-invalidate-threads)
1569 (unless (or (null gdb-var-list)
1570 (eq system-type 'darwin)) ;Breaks on Darwin's GDB-5.3.
1571 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1572 ;; Only needed/used with speedbar/watch expressions.
1573 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1574 (if (string-equal gdb-version "pre-6.4")
1576 (gdb-var-update-1)))))
1577 (setq gdb-first-post-prompt nil)
1578 (let ((sink gdb-output-sink))
1581 ((eq sink 'pre-emacs)
1582 (setq gdb-output-sink 'emacs))
1585 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1587 (defconst gdb-buffer-list
1588 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1590 (defun gdb-remove-text-properties ()
1591 (dolist (buffertype gdb-buffer-list)
1592 (let ((buffer (gdb-get-buffer buffertype)))
1594 (with-current-buffer buffer
1595 (let ((inhibit-read-only t))
1596 (remove-text-properties
1597 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1599 ;; GUD displays the selected GDB frame. This might might not be the current
1600 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1601 ;; visited breakpoint is, use that window.
1602 (defun gdb-display-source-buffer (buffer)
1603 (let* ((last-window (if gud-last-last-frame
1605 (gud-find-file (car gud-last-last-frame)))))
1606 (source-window (or last-window
1607 (if (and gdb-source-window
1608 (window-live-p gdb-source-window))
1609 gdb-source-window))))
1611 (setq gdb-source-window source-window)
1612 (set-window-buffer source-window buffer))
1615 ;; Derived from gud-gdb-marker-regexp
1616 (defvar gdb-fullname-regexp
1617 (concat "\\(.:?[^" ":" "\n]*\\)" ":" "\\([0-9]*\\)" ":" ".*"))
1619 (defun gud-gdba-marker-filter (string)
1620 "A gud marker filter for gdb. Handle a burst of output from GDB."
1621 (if gdb-flush-pending-output
1623 (when gdb-enable-debug
1624 (push (cons 'recv string) gdb-debug-log)
1625 (if (and gdb-debug-log-max
1626 (> (length gdb-debug-log) gdb-debug-log-max))
1627 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1628 ;; Recall the left over gud-marker-acc from last time.
1629 (setq gud-marker-acc (concat gud-marker-acc string))
1630 ;; Start accumulating output for the GUD buffer.
1633 ;; Process all the complete markers in this chunk.
1634 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1635 (let ((annotation (match-string 1 gud-marker-acc))
1636 (before (substring gud-marker-acc 0 (match-beginning 0)))
1637 (after (substring gud-marker-acc (match-end 0))))
1639 ;; Parse the tag from the annotation, and maybe its arguments.
1640 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1641 (let* ((annotation-type (match-string 1 annotation))
1642 (annotation-arguments (match-string 2 annotation))
1643 (annotation-rule (assoc annotation-type
1644 gdb-annotation-rules))
1645 (fullname (string-match gdb-fullname-regexp annotation-type)))
1647 ;; Stuff prior to the match is just ordinary output.
1648 ;; It is either concatenated to OUTPUT or directed
1651 (gdb-concat-output output
1652 (concat before (if fullname "\n"))))
1654 ;; Take that stuff off the gud-marker-acc.
1655 (setq gud-marker-acc after)
1657 ;; Call the handler for this annotation.
1659 (funcall (car (cdr annotation-rule))
1660 annotation-arguments)
1662 ;; Switch to gud-gdb-marker-filter if appropriate.
1665 ;; Extract the frame position from the marker.
1666 (setq gud-last-frame (cons (match-string 1 annotation)
1668 (match-string 2 annotation))))
1670 (set (make-local-variable 'gud-minor-mode) 'gdb)
1671 (set (make-local-variable 'gud-marker-filter)
1672 'gud-gdb-marker-filter)))
1674 ;; Else the annotation is not recognized. Ignore it silently,
1675 ;; so that GDB can add new annotations without causing
1679 ;; Does the remaining text end in a partial line?
1680 ;; If it does, then keep part of the gud-marker-acc until we get more.
1681 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1684 ;; Everything before the potential marker start can be output.
1686 (gdb-concat-output output
1687 (substring gud-marker-acc 0
1688 (match-beginning 0))))
1690 ;; Everything after, we save, to combine with later input.
1691 (setq gud-marker-acc (substring gud-marker-acc
1692 (match-beginning 0))))
1694 ;; In case we know the gud-marker-acc contains no partial annotations:
1696 (setq output (gdb-concat-output output gud-marker-acc))
1697 (setq gud-marker-acc "")))
1700 (defun gdb-concat-output (so-far new)
1702 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1703 (let ((sink gdb-output-sink))
1705 ((eq sink 'user) (concat so-far new))
1706 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1708 (gdb-append-to-partial-output new)
1710 ((eq sink 'inferior)
1711 (gdb-append-to-inferior-io new)
1715 (error "Bogon output sink %S" sink)))))
1717 (defun gdb-append-to-partial-output (string)
1718 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1719 (goto-char (point-max))
1722 (defun gdb-clear-partial-output ()
1723 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1726 (defun gdb-append-to-inferior-io (string)
1727 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1728 (goto-char (point-max))
1729 (insert-before-markers string))
1730 (if (not (string-equal string ""))
1731 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1733 (defun gdb-clear-inferior-io ()
1734 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1738 ;; One trick is to have a command who's output is always available in a buffer
1739 ;; of it's own, and is always up to date. We build several buffers of this
1742 ;; There are two aspects to this: gdb has to tell us when the output for that
1743 ;; command might have changed, and we have to be able to run the command
1744 ;; behind the user's back.
1746 ;; The output phasing associated with the variable gdb-output-sink
1747 ;; help us to run commands behind the user's back.
1749 ;; Below is the code for specificly managing buffers of output from one
1753 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1754 ;; It adds an input for the command we are tracking. It should be the
1755 ;; annotation rule binding of whatever gdb sends to tell us this command
1756 ;; might have changed it's output.
1758 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1759 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1760 ;; input in the input queue (see comment about ``gdb communications'' above).
1762 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1764 `(defun ,name (&optional ignored)
1765 (if (and ,demand-predicate
1767 gdb-pending-triggers)))
1770 (list ,gdb-command ',output-handler))
1771 (push ',name gdb-pending-triggers)))))
1773 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1775 (setq gdb-pending-triggers
1777 gdb-pending-triggers))
1778 (let ((buf (gdb-get-buffer ',buf-key)))
1780 (with-current-buffer buf
1781 (let* ((window (get-buffer-window buf 0))
1782 (start (window-start window))
1783 (p (window-point window))
1784 (buffer-read-only nil))
1786 (insert-buffer-substring (gdb-get-buffer-create
1787 'gdb-partial-output-buffer))
1788 (set-window-start window start)
1789 (set-window-point window p)))))
1790 ;; put customisation here
1793 (defmacro def-gdb-auto-updated-buffer (buffer-key
1794 trigger-name gdb-command
1795 output-handler-name custom-defun)
1797 (def-gdb-auto-update-trigger ,trigger-name
1798 ;; The demand predicate:
1799 (gdb-get-buffer ',buffer-key)
1801 ,output-handler-name)
1802 (def-gdb-auto-update-handler ,output-handler-name
1803 ,trigger-name ,buffer-key ,custom-defun)))
1807 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1809 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1810 'gdb-breakpoints-buffer-name
1811 'gdb-breakpoints-mode)
1813 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1814 ;; This defines the auto update rule for buffers of type
1815 ;; `gdb-breakpoints-buffer'.
1817 ;; It defines a function to serve as the annotation handler that
1818 ;; handles the `foo-invalidated' message. That function is called:
1819 gdb-invalidate-breakpoints
1821 ;; To update the buffer, this command is sent to gdb.
1822 "server info breakpoints\n"
1824 ;; This also defines a function to be the handler for the output
1825 ;; from the command above. That function will copy the output into
1826 ;; the appropriately typed buffer. That function will be called:
1827 gdb-info-breakpoints-handler
1828 ;; buffer specific functions
1829 gdb-info-breakpoints-custom)
1831 (defconst breakpoint-xpm-data
1833 static char *magick[] = {
1834 /* columns rows colors chars-per-pixel */
1850 "XPM data used for breakpoint icon.")
1852 (defconst breakpoint-enabled-pbm-data
1855 0 0 0 0 1 1 1 1 0 0 0 0
1856 0 0 0 1 1 1 1 1 1 0 0 0
1857 0 0 1 1 1 1 1 1 1 1 0 0
1858 0 1 1 1 1 1 1 1 1 1 1 0
1859 0 1 1 1 1 1 1 1 1 1 1 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 0 1 1 1 1 1 1 1 1 0 0
1863 0 0 0 1 1 1 1 1 1 0 0 0
1864 0 0 0 0 1 1 1 1 0 0 0 0"
1865 "PBM data used for enabled breakpoint icon.")
1867 (defconst breakpoint-disabled-pbm-data
1879 0 0 0 1 0 1 0 1 0 0"
1880 "PBM data used for disabled breakpoint icon.")
1882 (defvar breakpoint-enabled-icon nil
1883 "Icon for enabled breakpoint in display margin.")
1885 (defvar breakpoint-disabled-icon nil
1886 "Icon for disabled breakpoint in display margin.")
1888 (and (display-images-p)
1889 ;; Bitmap for breakpoint in fringe
1890 (define-fringe-bitmap 'breakpoint
1891 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1892 ;; Bitmap for gud-overlay-arrow in fringe
1893 (define-fringe-bitmap 'hollow-right-triangle
1894 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1896 (defface breakpoint-enabled
1900 "Face for enabled breakpoint icon in fringe."
1903 (defface breakpoint-disabled
1904 '((((class color) (min-colors 88)) :foreground "grey70")
1905 ;; Ensure that on low-color displays that we end up something visible.
1906 (((class color) (min-colors 8) (background light))
1907 :foreground "black")
1908 (((class color) (min-colors 8) (background dark))
1909 :foreground "white")
1910 (((type tty) (class mono))
1912 (t :background "gray"))
1913 "Face for disabled breakpoint icon in fringe."
1916 (defconst gdb-breakpoint-regexp
1917 "\\(?:\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\|\\([0-9]+\\.[0-9]+\\)\\)\\s-+\\(.\\)\\s-+")
1919 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1920 (defun gdb-info-breakpoints-custom ()
1921 (let ((flag) (bptno))
1922 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1923 (dolist (buffer (buffer-list))
1924 (with-current-buffer buffer
1925 (if (and (memq gud-minor-mode '(gdba gdbmi))
1926 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1927 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1928 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1930 (let ((buffer-read-only nil))
1931 (goto-char (point-min))
1932 (while (< (point) (- (point-max) 1))
1934 (if (looking-at gdb-breakpoint-regexp)
1936 (setq bptno (or (match-string 1) (match-string 2)))
1937 (setq flag (char-after (match-beginning 3)))
1938 (if (match-string 1)
1939 (setq gdb-parent-bptno-enabled (eq flag ?y)))
1940 (add-text-properties
1941 (match-beginning 3) (match-end 3)
1943 '(face font-lock-warning-face)
1944 '(face font-lock-type-face)))
1946 (el (line-end-position)))
1947 (when (re-search-forward " in \\(.*\\) at" el t)
1948 (add-text-properties
1949 (match-beginning 1) (match-end 1)
1950 '(face font-lock-function-name-face)))
1951 (if (re-search-forward ".*\\s-+\\(\\S-+\\):\\([0-9]+\\)$")
1952 (let ((line (match-string 2))
1953 (file (match-string 1)))
1954 (add-text-properties bl el
1955 '(mouse-face highlight
1956 help-echo "mouse-2, RET: visit breakpoint"))
1957 (unless (file-exists-p file)
1958 (setq file (cdr (assoc bptno gdb-location-alist))))
1960 (not (string-equal file "File not found")))
1961 (with-current-buffer
1962 (find-file-noselect file 'nowarn)
1964 ;; Only want one breakpoint icon at each
1967 (goto-line (string-to-number line))
1968 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1971 (concat gdb-server-prefix "list "
1972 (match-string-no-properties 1) ":1\n")
1975 (list (concat gdb-server-prefix "info source\n")
1976 `(lambda () (gdb-get-location
1977 ,bptno ,line ,flag))))))
1978 (if (re-search-forward
1979 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1981 (add-text-properties
1982 (match-beginning 1) (match-end 1)
1983 '(face font-lock-function-name-face))
1985 (re-search-backward "\\s-\\(\\S-*\\)"
1987 (add-text-properties
1988 (match-beginning 1) (match-end 1)
1989 '(face font-lock-variable-name-face)))))))
1991 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom))
1993 ;; Breakpoints buffer is always present. Hack to just update
1994 ;; current frame if there's been no execution.
1995 (if gdb-stack-update
1996 (setq gdb-stack-update nil)
1997 (if (gdb-get-buffer 'gdb-stack-buffer) (gdb-info-stack-custom))))
1999 (declare-function gud-remove "gdb-ui" t t) ; gud-def
2000 (declare-function gud-break "gdb-ui" t t) ; gud-def
2002 (defun gdb-mouse-set-clear-breakpoint (event)
2003 "Set/clear breakpoint in left fringe/margin at mouse click.
2004 If not in a source or disassembly buffer just set point."
2006 (mouse-minibuffer-check event)
2007 (let ((posn (event-end event)))
2008 (with-selected-window (posn-window posn)
2009 (if (or (buffer-file-name) (eq major-mode 'gdb-assembler-mode))
2010 (if (numberp (posn-point posn))
2012 (goto-char (posn-point posn))
2013 (if (or (posn-object posn)
2014 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2018 (posn-set-point posn))))
2020 (defun gdb-mouse-toggle-breakpoint-margin (event)
2021 "Enable/disable breakpoint in left margin with mouse click."
2023 (mouse-minibuffer-check event)
2024 (let ((posn (event-end event)))
2025 (if (numberp (posn-point posn))
2026 (with-selected-window (posn-window posn)
2028 (goto-char (posn-point posn))
2029 (if (posn-object posn)
2030 (let* ((bptno (get-text-property
2031 0 'gdb-bptno (car (posn-string posn)))))
2032 (string-match "\\([0-9+]\\)*" bptno)
2035 (concat gdb-server-prefix
2036 (if (get-text-property
2037 0 'gdb-enabled (car (posn-string posn)))
2040 (match-string 1 bptno) "\n")
2043 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2044 "Enable/disable breakpoint in left fringe with mouse click."
2046 (mouse-minibuffer-check event)
2047 (let* ((posn (event-end event))
2048 (pos (posn-point posn))
2051 (with-selected-window (posn-window posn)
2053 (set-buffer (window-buffer (selected-window)))
2055 (dolist (overlay (overlays-in pos pos))
2056 (when (overlay-get overlay 'put-break)
2057 (setq obj (overlay-get overlay 'before-string))))
2059 (let* ((bptno (get-text-property 0 'gdb-bptno obj)))
2060 (string-match "\\([0-9+]\\)*" bptno)
2063 (concat gdb-server-prefix
2064 (if (get-text-property 0 'gdb-enabled obj)
2067 (match-string 1 bptno) "\n")
2070 (defun gdb-breakpoints-buffer-name ()
2071 (with-current-buffer gud-comint-buffer
2072 (concat "*breakpoints of " (gdb-get-target-string) "*")))
2074 (defun gdb-display-breakpoints-buffer ()
2075 "Display status of user-settable breakpoints."
2078 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
2080 (defun gdb-frame-breakpoints-buffer ()
2081 "Display status of user-settable breakpoints in a new frame."
2083 (let ((special-display-regexps (append special-display-regexps '(".*")))
2084 (special-display-frame-alist gdb-frame-parameters))
2085 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
2087 (defvar gdb-breakpoints-mode-map
2088 (let ((map (make-sparse-keymap))
2089 (menu (make-sparse-keymap "Breakpoints")))
2090 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2091 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2092 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2093 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2094 (suppress-keymap map)
2095 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2096 (define-key map " " 'gdb-toggle-breakpoint)
2097 (define-key map "D" 'gdb-delete-breakpoint)
2098 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2099 (define-key map "q" 'gdb-delete-frame-or-window)
2100 (define-key map "\r" 'gdb-goto-breakpoint)
2101 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2102 (define-key map [follow-link] 'mouse-face)
2105 (defun gdb-delete-frame-or-window ()
2106 "Delete frame if there is only one window. Otherwise delete the window."
2108 (if (one-window-p) (delete-frame)
2111 ;;from make-mode-line-mouse-map
2112 (defun gdb-make-header-line-mouse-map (mouse function) "\
2113 Return a keymap with single entry for mouse key MOUSE on the header line.
2114 MOUSE is defined to run function FUNCTION with no args in the buffer
2115 corresponding to the mode line clicked."
2116 (let ((map (make-sparse-keymap)))
2117 (define-key map (vector 'header-line mouse) function)
2118 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2121 (defvar gdb-breakpoints-header
2122 `(,(propertize "Breakpoints"
2123 'help-echo "mouse-1: select"
2124 'mouse-face 'mode-line-highlight
2127 (gdb-make-header-line-mouse-map
2129 (lambda (event) (interactive "e")
2130 (save-selected-window
2131 (select-window (posn-window (event-start event)))
2132 (set-window-dedicated-p (selected-window) nil)
2134 (gdb-get-buffer-create 'gdb-breakpoints-buffer))
2135 (set-window-dedicated-p (selected-window) t)))))
2137 ,(propertize "Threads"
2138 'help-echo "mouse-1: select"
2139 'mouse-face 'mode-line-highlight
2142 (gdb-make-header-line-mouse-map
2144 (lambda (event) (interactive "e")
2145 (save-selected-window
2146 (select-window (posn-window (event-start event)))
2147 (set-window-dedicated-p (selected-window) nil)
2149 (gdb-get-buffer-create 'gdb-threads-buffer))
2150 (set-window-dedicated-p (selected-window) t)))))))
2152 (defun gdb-breakpoints-mode ()
2153 "Major mode for gdb breakpoints.
2155 \\{gdb-breakpoints-mode-map}"
2156 (kill-all-local-variables)
2157 (setq major-mode 'gdb-breakpoints-mode)
2158 (setq mode-name "Breakpoints")
2159 (use-local-map gdb-breakpoints-mode-map)
2160 (setq buffer-read-only t)
2161 (buffer-disable-undo)
2162 (setq header-line-format gdb-breakpoints-header)
2163 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2164 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2165 'gdb-invalidate-breakpoints
2166 'gdbmi-invalidate-breakpoints))
2168 (defun gdb-toggle-breakpoint ()
2169 "Enable/disable breakpoint at current line."
2172 (beginning-of-line 1)
2173 (if (looking-at gdb-breakpoint-regexp)
2176 (concat gdb-server-prefix
2177 (if (eq ?y (char-after (match-beginning 3)))
2180 (or (match-string 1) (match-string 2)) "\n") 'ignore))
2181 (error "Not recognized as break/watchpoint line"))))
2183 (defun gdb-delete-breakpoint ()
2184 "Delete the breakpoint at current line."
2187 (beginning-of-line 1)
2188 (if (looking-at gdb-breakpoint-regexp)
2189 (if (match-string 1)
2192 (concat gdb-server-prefix "delete " (match-string 1) "\n")
2194 (message-box "This breakpoint cannot be deleted on its own."))
2195 (error "Not recognized as break/watchpoint line"))))
2197 (defun gdb-goto-breakpoint (&optional event)
2198 "Display the breakpoint location specified at current line."
2199 (interactive (list last-input-event))
2200 (if event (posn-set-point (event-end event)))
2202 (beginning-of-line 1)
2203 (if (looking-at "\\([0-9]+\\.?[0-9]*\\) .*\\s-+\\(\\S-+\\):\\([0-9]+\\)$")
2204 (let ((bptno (match-string 1))
2205 (file (match-string 2))
2206 (line (match-string 3)))
2207 (save-selected-window
2208 (let* ((buffer (find-file-noselect
2209 (if (file-exists-p file) file
2210 (cdr (assoc bptno gdb-location-alist)))))
2211 (window (or (gdb-display-source-buffer buffer)
2212 (display-buffer buffer))))
2213 (setq gdb-source-window window)
2214 (with-current-buffer buffer
2215 (goto-line (string-to-number line))
2216 (set-window-point window (point))))))
2217 (error "No location specified."))))
2220 ;; Frames buffer. This displays a perpetually correct backtrace
2221 ;; (from the command `where').
2223 ;; Alas, if your stack is deep, it is costly.
2225 (defcustom gdb-max-frames 40
2226 "Maximum number of frames displayed in call stack."
2231 (gdb-set-buffer-rules 'gdb-stack-buffer
2232 'gdb-stack-buffer-name
2235 (def-gdb-auto-updated-buffer gdb-stack-buffer
2236 gdb-invalidate-frames
2237 (concat "server info stack " (number-to-string gdb-max-frames) "\n")
2238 gdb-info-stack-handler
2239 gdb-info-stack-custom)
2241 ;; This may be more important for embedded targets where unwinding the
2242 ;; stack may take a long time.
2243 (defadvice gdb-invalidate-frames (around gdb-invalidate-frames-advice
2244 (&optional ignored) activate compile)
2245 "Only queue \"info stack\" if execution has occurred."
2246 (if gdb-stack-update ad-do-it))
2248 (defun gdb-info-stack-custom ()
2249 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2252 (unless (eq gdb-look-up-stack 'delete)
2253 (let ((buffer-read-only nil)
2255 (goto-char (point-min))
2256 (while (< (point) (point-max))
2257 (setq bl (line-beginning-position)
2258 el (line-end-position))
2259 (when (looking-at "#")
2260 (add-text-properties bl el
2261 '(mouse-face highlight
2262 help-echo "mouse-2, RET: Select frame")))
2264 (when (looking-at "^#\\([0-9]+\\)")
2265 (when (string-equal (match-string 1) gdb-frame-number)
2266 (if (gud-tool-bar-item-visible-no-fringe)
2268 (put-text-property bl (+ bl 4)
2269 'face '(:inverse-video t))
2271 (or gdb-stack-position
2272 (setq gdb-stack-position (make-marker)))
2273 (set-marker gdb-stack-position (point))
2274 (setq move-to gdb-stack-position)))
2275 (when (re-search-forward "\\([^ ]+\\) (" el t)
2276 (put-text-property (match-beginning 1) (match-end 1)
2277 'face font-lock-function-name-face)
2278 (setq bl (match-end 0))
2279 (while (re-search-forward "<\\([^>]+\\)>" el t)
2280 (put-text-property (match-beginning 1) (match-end 1)
2281 'face font-lock-function-name-face))
2283 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2284 (put-text-property (match-beginning 1) (match-end 1)
2285 'face font-lock-variable-name-face))))
2288 (when (looking-at "(More stack frames follow...)")
2289 (add-text-properties (match-beginning 0) (match-end 0)
2290 '(mouse-face highlight
2293 "mouse-2, RET: customize gdb-max-frames to see more frames")))))
2294 (when gdb-look-up-stack
2295 (goto-char (point-min))
2296 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2297 (let ((start (line-beginning-position))
2298 (file (match-string 1))
2299 (line (match-string 2)))
2300 (re-search-backward "^#*\\([0-9]+\\)" start t)
2302 (list (concat gdb-server-prefix "frame "
2303 (match-string 1) "\n") 'gdb-set-hollow))
2305 (list (concat gdb-server-prefix "frame 0\n") 'ignore))))))
2307 (let ((window (get-buffer-window (current-buffer) 0)))
2309 (with-selected-window window
2311 (unless (pos-visible-in-window-p)
2312 (recenter '(center)))))))))
2313 (if (eq gdb-look-up-stack 'delete)
2314 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2315 (setq gdb-look-up-stack nil))
2317 (defun gdb-set-hollow ()
2318 (if gud-last-last-frame
2319 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2320 (setq fringe-indicator-alist
2321 '((overlay-arrow . hollow-right-triangle))))))
2323 (defun gdb-stack-buffer-name ()
2324 (with-current-buffer gud-comint-buffer
2325 (concat "*stack frames of " (gdb-get-target-string) "*")))
2327 (defun gdb-display-stack-buffer ()
2328 "Display backtrace of current stack."
2331 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2333 (defun gdb-frame-stack-buffer ()
2334 "Display backtrace of current stack in a new frame."
2336 (let ((special-display-regexps (append special-display-regexps '(".*")))
2337 (special-display-frame-alist gdb-frame-parameters))
2338 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2340 (defvar gdb-frames-mode-map
2341 (let ((map (make-sparse-keymap)))
2342 (suppress-keymap map)
2343 (define-key map "q" 'kill-this-buffer)
2344 (define-key map "\r" 'gdb-frames-select)
2345 (define-key map "F" 'gdb-frames-force-update)
2346 (define-key map [mouse-2] 'gdb-frames-select)
2347 (define-key map [follow-link] 'mouse-face)
2350 (declare-function gdbmi-invalidate-frames "ext:gdb-mi" nil t)
2352 (defun gdb-frames-force-update ()
2353 "Force update of call stack.
2354 Use when the displayed call stack gets out of sync with the
2355 actual one, e.g after using the Gdb command \"return\" or setting
2356 $pc directly from the GUD buffer. This command isn't normally needed."
2358 (setq gdb-stack-update t)
2359 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2360 (gdb-invalidate-frames)
2361 (gdbmi-invalidate-frames)))
2363 (defun gdb-frames-mode ()
2364 "Major mode for gdb call stack.
2366 \\{gdb-frames-mode-map}"
2367 (kill-all-local-variables)
2368 (setq major-mode 'gdb-frames-mode)
2369 (setq mode-name "Frames")
2370 (setq gdb-stack-position nil)
2371 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2372 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2373 (setq buffer-read-only t)
2374 (buffer-disable-undo)
2375 (gdb-thread-identification)
2376 (use-local-map gdb-frames-mode-map)
2377 (run-mode-hooks 'gdb-frames-mode-hook)
2378 (setq gdb-stack-update t)
2379 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2380 'gdb-invalidate-frames
2381 'gdbmi-invalidate-frames))
2383 (defun gdb-get-frame-number ()
2386 (let* ((start (line-beginning-position))
2387 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2388 (n (or (and pos (match-string 1)) "0")))
2391 (defun gdb-frames-select (&optional event)
2392 "Select the frame and display the relevant source."
2393 (interactive (list last-input-event))
2394 (if event (posn-set-point (event-end event)))
2395 (if (get-text-property (point) 'gdb-max-frames)
2397 (message-box "After setting gdb-max-frames, you need to enter\n\
2398 another GDB command e.g pwd, to see new frames")
2399 (customize-variable-other-window 'gdb-max-frames))
2401 (list (concat gdb-server-prefix "frame "
2402 (gdb-get-frame-number) "\n") 'ignore))))
2405 ;; Threads buffer. This displays a selectable thread list.
2407 (gdb-set-buffer-rules 'gdb-threads-buffer
2408 'gdb-threads-buffer-name
2411 (def-gdb-auto-updated-buffer gdb-threads-buffer
2412 gdb-invalidate-threads
2413 (concat gdb-server-prefix "info threads\n")
2414 gdb-info-threads-handler
2415 gdb-info-threads-custom)
2417 (defun gdb-info-threads-custom ()
2418 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2419 (let ((buffer-read-only nil))
2421 (goto-char (point-min))
2422 (if (re-search-forward "\\* \\([0-9]+\\)" nil t)
2423 (setq gdb-thread-indicator
2424 (propertize (concat " [" (match-string 1) "]")
2425 ; FIXME: this help-echo doesn't work
2426 'help-echo "thread id")))
2427 (goto-char (point-min))
2428 (while (< (point) (point-max))
2429 (unless (looking-at "No ")
2430 (add-text-properties (line-beginning-position) (line-end-position)
2431 '(mouse-face highlight
2432 help-echo "mouse-2, RET: select thread")))
2433 (forward-line 1))))))
2435 (defun gdb-threads-buffer-name ()
2436 (with-current-buffer gud-comint-buffer
2437 (concat "*threads of " (gdb-get-target-string) "*")))
2439 (defun gdb-display-threads-buffer ()
2440 "Display IDs of currently known threads."
2443 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2445 (defun gdb-frame-threads-buffer ()
2446 "Display IDs of currently known threads in a new frame."
2448 (let ((special-display-regexps (append special-display-regexps '(".*")))
2449 (special-display-frame-alist gdb-frame-parameters))
2450 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2452 (defvar gdb-threads-mode-map
2453 (let ((map (make-sparse-keymap)))
2454 (suppress-keymap map)
2455 (define-key map "q" 'kill-this-buffer)
2456 (define-key map "\r" 'gdb-threads-select)
2457 (define-key map [mouse-2] 'gdb-threads-select)
2458 (define-key map [follow-link] 'mouse-face)
2461 (defvar gdb-threads-font-lock-keywords
2462 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2463 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2464 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2465 "Font lock keywords used in `gdb-threads-mode'.")
2467 (defun gdb-threads-mode ()
2468 "Major mode for gdb threads.
2470 \\{gdb-threads-mode-map}"
2471 (kill-all-local-variables)
2472 (setq major-mode 'gdb-threads-mode)
2473 (setq mode-name "Threads")
2474 (setq buffer-read-only t)
2475 (buffer-disable-undo)
2476 (setq header-line-format gdb-breakpoints-header)
2477 (use-local-map gdb-threads-mode-map)
2478 (set (make-local-variable 'font-lock-defaults)
2479 '(gdb-threads-font-lock-keywords))
2480 (run-mode-hooks 'gdb-threads-mode-hook)
2481 ;; Force "info threads" onto queue.
2482 (lambda () (let ((gud-running nil)) (gdb-invalidate-threads))))
2484 (defun gdb-get-thread-number ()
2486 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2487 (match-string-no-properties 1)))
2489 (defun gdb-threads-select (&optional event)
2490 "Select the thread and display the relevant source."
2491 (interactive (list last-input-event))
2492 (if event (posn-set-point (event-end event)))
2494 (list (concat gdb-server-prefix "thread "
2495 (gdb-get-thread-number) "\n") 'ignore))
2496 (gud-display-frame))
2498 (defun gdb-thread-identification ()
2499 (setq mode-line-buffer-identification
2500 (list (car mode-line-buffer-identification)
2501 '(gdb-thread-indicator gdb-thread-indicator))))
2503 ;; Registers buffer.
2505 (defcustom gdb-all-registers nil
2506 "Non-nil means include floating-point registers."
2511 (gdb-set-buffer-rules 'gdb-registers-buffer
2512 'gdb-registers-buffer-name
2513 'gdb-registers-mode)
2515 (def-gdb-auto-updated-buffer gdb-registers-buffer
2516 gdb-invalidate-registers
2518 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2519 gdb-info-registers-handler
2520 gdb-info-registers-custom)
2522 (defun gdb-info-registers-custom ()
2523 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2525 (let ((buffer-read-only nil)
2527 (goto-char (point-min))
2528 (while (< (point) (point-max))
2529 (setq start (line-beginning-position))
2530 (setq end (line-end-position))
2531 (when (looking-at "^[^ ]+")
2532 (unless (string-equal (match-string 0) "The")
2533 (put-text-property start (match-end 0)
2534 'face font-lock-variable-name-face)
2535 (add-text-properties start end
2536 '(help-echo "mouse-2: edit value"
2537 mouse-face highlight))))
2538 (forward-line 1))))))
2540 (defun gdb-edit-register-value (&optional event)
2541 (interactive (list last-input-event))
2543 (if event (posn-set-point (event-end event)))
2545 (let* ((register (current-word))
2546 (value (read-string (format "New value (%s): " register))))
2548 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2551 (defvar gdb-registers-mode-map
2552 (let ((map (make-sparse-keymap)))
2553 (suppress-keymap map)
2554 (define-key map "\r" 'gdb-edit-register-value)
2555 (define-key map [mouse-2] 'gdb-edit-register-value)
2556 (define-key map " " 'gdb-all-registers)
2557 (define-key map "q" 'kill-this-buffer)
2560 (defvar gdb-locals-header
2561 `(,(propertize "Locals"
2562 'help-echo "mouse-1: select"
2563 'mouse-face 'mode-line-highlight
2566 (gdb-make-header-line-mouse-map
2568 (lambda (event) (interactive "e")
2569 (save-selected-window
2570 (select-window (posn-window (event-start event)))
2571 (set-window-dedicated-p (selected-window) nil)
2573 (gdb-get-buffer-create 'gdb-locals-buffer))
2574 (set-window-dedicated-p (selected-window) t)))))
2576 ,(propertize "Registers"
2577 'help-echo "mouse-1: select"
2578 'mouse-face 'mode-line-highlight
2581 (gdb-make-header-line-mouse-map
2583 (lambda (event) (interactive "e")
2584 (save-selected-window
2585 (select-window (posn-window (event-start event)))
2586 (set-window-dedicated-p (selected-window) nil)
2588 (gdb-get-buffer-create 'gdb-registers-buffer))
2589 (set-window-dedicated-p (selected-window) t)))))))
2591 (defun gdb-registers-mode ()
2592 "Major mode for gdb registers.
2594 \\{gdb-registers-mode-map}"
2595 (kill-all-local-variables)
2596 (setq major-mode 'gdb-registers-mode)
2597 (setq mode-name "Registers")
2598 (setq header-line-format gdb-locals-header)
2599 (setq buffer-read-only t)
2600 (buffer-disable-undo)
2601 (gdb-thread-identification)
2602 (use-local-map gdb-registers-mode-map)
2603 (run-mode-hooks 'gdb-registers-mode-hook)
2604 (if (string-equal gdb-version "pre-6.4")
2606 (if gdb-all-registers (setq mode-name "Registers:All"))
2607 'gdb-invalidate-registers)
2608 'gdb-invalidate-registers-1))
2610 (defun gdb-registers-buffer-name ()
2611 (with-current-buffer gud-comint-buffer
2612 (concat "*registers of " (gdb-get-target-string) "*")))
2614 (defun gdb-display-registers-buffer ()
2615 "Display integer register contents."
2618 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2620 (defun gdb-frame-registers-buffer ()
2621 "Display integer register contents in a new frame."
2623 (let ((special-display-regexps (append special-display-regexps '(".*")))
2624 (special-display-frame-alist gdb-frame-parameters))
2625 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2627 (defun gdb-all-registers ()
2628 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2630 (when (string-equal gdb-version "pre-6.4")
2631 (if gdb-all-registers
2633 (setq gdb-all-registers nil)
2634 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2635 (setq mode-name "Registers")))
2636 (setq gdb-all-registers t)
2637 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2638 (setq mode-name "Registers:All")))
2639 (message (format "Display of floating-point registers %sabled"
2640 (if gdb-all-registers "en" "dis")))
2641 (gdb-invalidate-registers)))
2646 (defcustom gdb-memory-repeat-count 32
2647 "Number of data items in memory window."
2652 (defcustom gdb-memory-format "x"
2653 "Display format of data items in memory window."
2654 :type '(choice (const :tag "Hexadecimal" "x")
2655 (const :tag "Signed decimal" "d")
2656 (const :tag "Unsigned decimal" "u")
2657 (const :tag "Octal" "o")
2658 (const :tag "Binary" "t"))
2662 (defcustom gdb-memory-unit "w"
2663 "Unit size of data items in memory window."
2664 :type '(choice (const :tag "Byte" "b")
2665 (const :tag "Halfword" "h")
2666 (const :tag "Word" "w")
2667 (const :tag "Giant word" "g"))
2671 (gdb-set-buffer-rules 'gdb-memory-buffer
2672 'gdb-memory-buffer-name
2675 (def-gdb-auto-updated-buffer gdb-memory-buffer
2676 gdb-invalidate-memory
2677 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2678 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2679 gdb-read-memory-handler
2680 gdb-read-memory-custom)
2682 (defun gdb-read-memory-custom ()
2684 (goto-char (point-min))
2685 (if (looking-at "0x[[:xdigit:]]+")
2686 (setq gdb-memory-address (match-string 0)))))
2688 (defvar gdb-memory-mode-map
2689 (let ((map (make-sparse-keymap)))
2690 (suppress-keymap map)
2691 (define-key map "q" 'kill-this-buffer)
2694 (defun gdb-memory-set-address (event)
2695 "Set the start memory address."
2697 (save-selected-window
2698 (select-window (posn-window (event-start event)))
2699 (let ((arg (read-from-minibuffer "Memory address: ")))
2700 (setq gdb-memory-address arg))
2701 (gdb-invalidate-memory)))
2703 (defun gdb-memory-set-repeat-count (event)
2704 "Set the number of data items in memory window."
2706 (save-selected-window
2707 (select-window (posn-window (event-start event)))
2708 (let* ((arg (read-from-minibuffer "Repeat count: "))
2709 (count (string-to-number arg)))
2711 (error "Positive numbers only")
2712 (customize-set-variable 'gdb-memory-repeat-count count)
2713 (gdb-invalidate-memory)))))
2715 (defun gdb-memory-format-binary ()
2716 "Set the display format to binary."
2718 (customize-set-variable 'gdb-memory-format "t")
2719 (gdb-invalidate-memory))
2721 (defun gdb-memory-format-octal ()
2722 "Set the display format to octal."
2724 (customize-set-variable 'gdb-memory-format "o")
2725 (gdb-invalidate-memory))
2727 (defun gdb-memory-format-unsigned ()
2728 "Set the display format to unsigned decimal."
2730 (customize-set-variable 'gdb-memory-format "u")
2731 (gdb-invalidate-memory))
2733 (defun gdb-memory-format-signed ()
2734 "Set the display format to decimal."
2736 (customize-set-variable 'gdb-memory-format "d")
2737 (gdb-invalidate-memory))
2739 (defun gdb-memory-format-hexadecimal ()
2740 "Set the display format to hexadecimal."
2742 (customize-set-variable 'gdb-memory-format "x")
2743 (gdb-invalidate-memory))
2745 (defvar gdb-memory-format-map
2746 (let ((map (make-sparse-keymap)))
2747 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2749 "Keymap to select format in the header line.")
2751 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2752 "Menu of display formats in the header line.")
2754 (define-key gdb-memory-format-menu [binary]
2755 '(menu-item "Binary" gdb-memory-format-binary
2756 :button (:radio . (equal gdb-memory-format "t"))))
2757 (define-key gdb-memory-format-menu [octal]
2758 '(menu-item "Octal" gdb-memory-format-octal
2759 :button (:radio . (equal gdb-memory-format "o"))))
2760 (define-key gdb-memory-format-menu [unsigned]
2761 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2762 :button (:radio . (equal gdb-memory-format "u"))))
2763 (define-key gdb-memory-format-menu [signed]
2764 '(menu-item "Signed Decimal" gdb-memory-format-signed
2765 :button (:radio . (equal gdb-memory-format "d"))))
2766 (define-key gdb-memory-format-menu [hexadecimal]
2767 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2768 :button (:radio . (equal gdb-memory-format "x"))))
2770 (defun gdb-memory-format-menu (event)
2772 (x-popup-menu event gdb-memory-format-menu))
2774 (defun gdb-memory-format-menu-1 (event)
2776 (save-selected-window
2777 (select-window (posn-window (event-start event)))
2778 (let* ((selection (gdb-memory-format-menu event))
2779 (binding (and selection (lookup-key gdb-memory-format-menu
2780 (vector (car selection))))))
2781 (if binding (call-interactively binding)))))
2783 (defun gdb-memory-unit-giant ()
2784 "Set the unit size to giant words (eight bytes)."
2786 (customize-set-variable 'gdb-memory-unit "g")
2787 (gdb-invalidate-memory))
2789 (defun gdb-memory-unit-word ()
2790 "Set the unit size to words (four bytes)."
2792 (customize-set-variable 'gdb-memory-unit "w")
2793 (gdb-invalidate-memory))
2795 (defun gdb-memory-unit-halfword ()
2796 "Set the unit size to halfwords (two bytes)."
2798 (customize-set-variable 'gdb-memory-unit "h")
2799 (gdb-invalidate-memory))
2801 (defun gdb-memory-unit-byte ()
2802 "Set the unit size to bytes."
2804 (customize-set-variable 'gdb-memory-unit "b")
2805 (gdb-invalidate-memory))
2807 (defvar gdb-memory-unit-map
2808 (let ((map (make-sparse-keymap)))
2809 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2811 "Keymap to select units in the header line.")
2813 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2814 "Menu of units in the header line.")
2816 (define-key gdb-memory-unit-menu [giantwords]
2817 '(menu-item "Giant words" gdb-memory-unit-giant
2818 :button (:radio . (equal gdb-memory-unit "g"))))
2819 (define-key gdb-memory-unit-menu [words]
2820 '(menu-item "Words" gdb-memory-unit-word
2821 :button (:radio . (equal gdb-memory-unit "w"))))
2822 (define-key gdb-memory-unit-menu [halfwords]
2823 '(menu-item "Halfwords" gdb-memory-unit-halfword
2824 :button (:radio . (equal gdb-memory-unit "h"))))
2825 (define-key gdb-memory-unit-menu [bytes]
2826 '(menu-item "Bytes" gdb-memory-unit-byte
2827 :button (:radio . (equal gdb-memory-unit "b"))))
2829 (defun gdb-memory-unit-menu (event)
2831 (x-popup-menu event gdb-memory-unit-menu))
2833 (defun gdb-memory-unit-menu-1 (event)
2835 (save-selected-window
2836 (select-window (posn-window (event-start event)))
2837 (let* ((selection (gdb-memory-unit-menu event))
2838 (binding (and selection (lookup-key gdb-memory-unit-menu
2839 (vector (car selection))))))
2840 (if binding (call-interactively binding)))))
2842 (defvar gdb-memory-font-lock-keywords
2843 '(;; <__function.name+n>
2844 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2846 "Font lock keywords used in `gdb-memory-mode'.")
2848 (defun gdb-memory-mode ()
2849 "Major mode for examining memory.
2851 \\{gdb-memory-mode-map}"
2852 (kill-all-local-variables)
2853 (setq major-mode 'gdb-memory-mode)
2854 (setq mode-name "Memory")
2855 (setq buffer-read-only t)
2856 (buffer-disable-undo)
2857 (use-local-map gdb-memory-mode-map)
2858 (setq header-line-format
2864 'face font-lock-warning-face
2865 'help-echo "mouse-1: decrement address"
2866 'mouse-face 'mode-line-highlight
2868 (gdb-make-header-line-mouse-map
2870 (lambda () (interactive)
2871 (let ((gdb-memory-address
2872 ;; Let GDB do the arithmetic.
2874 gdb-memory-address " - "
2876 (* gdb-memory-repeat-count
2877 (cond ((string= gdb-memory-unit "b") 1)
2878 ((string= gdb-memory-unit "h") 2)
2879 ((string= gdb-memory-unit "w") 4)
2880 ((string= gdb-memory-unit "g") 8)))))))
2881 (gdb-invalidate-memory)))))
2884 'face font-lock-warning-face
2885 'help-echo "mouse-1: increment address"
2886 'mouse-face 'mode-line-highlight
2887 'local-map (gdb-make-header-line-mouse-map
2889 (lambda () (interactive)
2890 (let ((gdb-memory-address nil))
2891 (gdb-invalidate-memory)))))
2893 (propertize gdb-memory-address
2894 'face font-lock-warning-face
2895 'help-echo "mouse-1: set memory address"
2896 'mouse-face 'mode-line-highlight
2897 'local-map (gdb-make-header-line-mouse-map
2899 #'gdb-memory-set-address))
2901 (propertize (number-to-string gdb-memory-repeat-count)
2902 'face font-lock-warning-face
2903 'help-echo "mouse-1: set repeat count"
2904 'mouse-face 'mode-line-highlight
2905 'local-map (gdb-make-header-line-mouse-map
2907 #'gdb-memory-set-repeat-count))
2909 (propertize gdb-memory-format
2910 'face font-lock-warning-face
2911 'help-echo "mouse-3: select display format"
2912 'mouse-face 'mode-line-highlight
2913 'local-map gdb-memory-format-map)
2915 (propertize gdb-memory-unit
2916 'face font-lock-warning-face
2917 'help-echo "mouse-3: select unit size"
2918 'mouse-face 'mode-line-highlight
2919 'local-map gdb-memory-unit-map))))
2920 (set (make-local-variable 'font-lock-defaults)
2921 '(gdb-memory-font-lock-keywords))
2922 (run-mode-hooks 'gdb-memory-mode-hook)
2923 'gdb-invalidate-memory)
2925 (defun gdb-memory-buffer-name ()
2926 (with-current-buffer gud-comint-buffer
2927 (concat "*memory of " (gdb-get-target-string) "*")))
2929 (defun gdb-display-memory-buffer ()
2930 "Display memory contents."
2933 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2935 (defun gdb-frame-memory-buffer ()
2936 "Display memory contents in a new frame."
2938 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2939 (special-display-frame-alist
2940 (cons '(left-fringe . 0)
2941 (cons '(right-fringe . 0)
2942 (cons '(width . 83) gdb-frame-parameters)))))
2943 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2948 (gdb-set-buffer-rules 'gdb-locals-buffer
2949 'gdb-locals-buffer-name
2952 (def-gdb-auto-update-trigger gdb-invalidate-locals
2953 (gdb-get-buffer 'gdb-locals-buffer)
2954 "server info locals\n"
2955 gdb-info-locals-handler)
2957 (defvar gdb-locals-watch-map
2958 (let ((map (make-sparse-keymap)))
2959 (suppress-keymap map)
2960 (define-key map "\r" (lambda () (interactive)
2963 (define-key map [mouse-2] (lambda (event) (interactive "e")
2964 (mouse-set-point event)
2968 "Keymap to create watch expression of a complex data type local variable.")
2970 (defconst gdb-struct-string
2971 (concat (propertize "[struct/union]"
2972 'mouse-face 'highlight
2973 'help-echo "mouse-2: create watch expression"
2974 'local-map gdb-locals-watch-map) "\n"))
2976 (defconst gdb-array-string
2977 (concat " " (propertize "[array]"
2978 'mouse-face 'highlight
2979 'help-echo "mouse-2: create watch expression"
2980 'local-map gdb-locals-watch-map) "\n"))
2982 ;; Abbreviate for arrays and structures.
2983 ;; These can be expanded using gud-display.
2984 (defun gdb-info-locals-handler ()
2985 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2986 gdb-pending-triggers))
2987 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2988 (with-current-buffer buf
2989 (goto-char (point-min))
2990 ;; Need this in case "set print pretty" is on.
2991 (while (re-search-forward "^[ }].*\n" nil t)
2992 (replace-match "" nil nil))
2993 (goto-char (point-min))
2994 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2995 (replace-match gdb-struct-string nil nil))
2996 (goto-char (point-min))
2997 (while (re-search-forward "\\s-*{[^.].*\n" nil t)
2998 (replace-match gdb-array-string nil nil))))
2999 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3001 (with-current-buffer buf
3002 (let* ((window (get-buffer-window buf 0))
3003 (start (window-start window))
3004 (p (window-point window))
3005 (buffer-read-only nil))
3007 (insert-buffer-substring (gdb-get-buffer-create
3008 'gdb-partial-output-buffer))
3009 (set-window-start window start)
3010 (set-window-point window p)))))
3011 (run-hooks 'gdb-info-locals-hook))
3013 (defvar gdb-locals-mode-map
3014 (let ((map (make-sparse-keymap)))
3015 (suppress-keymap map)
3016 (define-key map "q" 'kill-this-buffer)
3019 (defun gdb-locals-mode ()
3020 "Major mode for gdb locals.
3022 \\{gdb-locals-mode-map}"
3023 (kill-all-local-variables)
3024 (setq major-mode 'gdb-locals-mode)
3025 (setq mode-name (concat "Locals:" gdb-selected-frame))
3026 (use-local-map gdb-locals-mode-map)
3027 (setq buffer-read-only t)
3028 (buffer-disable-undo)
3029 (setq header-line-format gdb-locals-header)
3030 (gdb-thread-identification)
3031 (set (make-local-variable 'font-lock-defaults)
3032 '(gdb-locals-font-lock-keywords))
3033 (run-mode-hooks 'gdb-locals-mode-hook)
3034 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3035 (string-equal gdb-version "pre-6.4"))
3036 'gdb-invalidate-locals
3037 'gdb-invalidate-locals-1))
3039 (defun gdb-locals-buffer-name ()
3040 (with-current-buffer gud-comint-buffer
3041 (concat "*locals of " (gdb-get-target-string) "*")))
3043 (defun gdb-display-locals-buffer ()
3044 "Display local variables of current stack and their values."
3047 (gdb-get-buffer-create 'gdb-locals-buffer) t))
3049 (defun gdb-frame-locals-buffer ()
3050 "Display local variables of current stack and their values in a new frame."
3052 (let ((special-display-regexps (append special-display-regexps '(".*")))
3053 (special-display-frame-alist gdb-frame-parameters))
3054 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
3057 ;;;; Window management
3058 (defun gdb-display-buffer (buf dedicated &optional frame)
3059 (let ((answer (get-buffer-window buf (or frame 0)))
3062 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3063 (if (get-buffer-window gud-comint-buffer)
3064 (select-window (get-buffer-window gud-comint-buffer))
3065 ;; If the buffer is not yet displayed, select the right frame.
3066 (pop-to-buffer gud-comint-buffer))
3067 (let ((window (get-lru-window)))
3069 (not (memq window `(,(get-buffer-window gud-comint-buffer)
3070 ,gdb-source-window))))
3072 (set-window-buffer window buf)
3073 (setq answer window))
3074 (setq must-split t)))
3076 (let* ((largest (get-largest-window))
3077 (cur-size (window-height largest)))
3078 (setq answer (split-window largest))
3079 (set-window-buffer answer buf)
3080 (set-window-dedicated-p answer dedicated)))
3084 ;;; Shared keymap initialization:
3086 (let ((menu (make-sparse-keymap "GDB-Windows")))
3087 (define-key gud-menu-map [displays]
3088 `(menu-item "GDB-Windows" ,menu
3089 :help "Open a GDB-UI buffer in a new window."
3090 :visible (memq gud-minor-mode '(gdbmi gdba))))
3091 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3092 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3093 (define-key menu [inferior]
3094 '(menu-item "Separate IO" gdb-display-separate-io-buffer
3095 :enable gdb-use-separate-io-buffer))
3096 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3097 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3098 (define-key menu [disassembly]
3099 '("Disassembly" . gdb-display-assembler-buffer))
3100 (define-key menu [breakpoints]
3101 '("Breakpoints" . gdb-display-breakpoints-buffer))
3102 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3103 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
3105 (let ((menu (make-sparse-keymap "GDB-Frames")))
3106 (define-key gud-menu-map [frames]
3107 `(menu-item "GDB-Frames" ,menu
3108 :help "Open a GDB-UI buffer in a new frame."
3109 :visible (memq gud-minor-mode '(gdbmi gdba))))
3110 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3111 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3112 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3113 (define-key menu [inferior]
3114 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
3115 :enable gdb-use-separate-io-buffer))
3116 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3117 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
3118 (define-key menu [breakpoints]
3119 '("Breakpoints" . gdb-frame-breakpoints-buffer))
3120 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3121 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
3123 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
3124 (define-key gud-menu-map [ui]
3125 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
3126 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
3127 (define-key menu [gdb-customize]
3128 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3129 :help "Customize Gdb Graphical Mode options."))
3130 (define-key menu [gdb-find-source-frame]
3131 '(menu-item "Look For Source Frame" gdb-find-source-frame
3132 :visible (eq gud-minor-mode 'gdba)
3133 :help "Toggle looking for source frame further up call stack."
3134 :button (:toggle . gdb-find-source-frame)))
3135 (define-key menu [gdb-use-separate-io]
3136 '(menu-item "Separate IO" gdb-use-separate-io-buffer
3137 :visible (eq gud-minor-mode 'gdba)
3138 :help "Toggle separate IO for debugged program."
3139 :button (:toggle . gdb-use-separate-io-buffer)))
3140 (define-key menu [gdb-many-windows]
3141 '(menu-item "Display Other Windows" gdb-many-windows
3142 :help "Toggle display of locals, stack and breakpoint information"
3143 :button (:toggle . gdb-many-windows)))
3144 (define-key menu [gdb-restore-windows]
3145 '(menu-item "Restore Window Layout" gdb-restore-windows
3146 :help "Restore standard layout for debug session.")))
3148 (defun gdb-frame-gdb-buffer ()
3149 "Display GUD buffer in a new frame."
3151 (let ((special-display-regexps (append special-display-regexps '(".*")))
3152 (special-display-frame-alist
3153 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3154 gdb-frame-parameters)))
3155 (same-window-regexps nil))
3156 (display-buffer gud-comint-buffer)))
3158 (defun gdb-display-gdb-buffer ()
3159 "Display GUD buffer."
3161 (let ((same-window-regexps nil))
3162 (pop-to-buffer gud-comint-buffer)))
3164 (defun gdb-set-window-buffer (name)
3165 (set-window-buffer (selected-window) (get-buffer name))
3166 (set-window-dedicated-p (selected-window) t))
3168 (defun gdb-setup-windows ()
3169 "Layout the window pattern for `gdb-many-windows'."
3170 (gdb-display-locals-buffer)
3171 (gdb-display-stack-buffer)
3172 (delete-other-windows)
3173 (gdb-display-breakpoints-buffer)
3174 (delete-other-windows)
3176 (pop-to-buffer gud-comint-buffer)
3177 (split-window nil ( / ( * (window-height) 3) 4))
3178 (split-window nil ( / (window-height) 3))
3179 (split-window-horizontally)
3181 (gdb-set-window-buffer (gdb-locals-buffer-name))
3184 (if gud-last-last-frame
3185 (gud-find-file (car gud-last-last-frame))
3187 (gud-find-file gdb-main-file)
3188 ;; Put buffer list in window if we
3189 ;; can't find a source file.
3190 (list-buffers-noselect))))
3191 (setq gdb-source-window (selected-window))
3192 (when gdb-use-separate-io-buffer
3193 (split-window-horizontally)
3195 (gdb-set-window-buffer
3196 (gdb-get-buffer-create 'gdb-inferior-io)))
3198 (gdb-set-window-buffer (gdb-stack-buffer-name))
3199 (split-window-horizontally)
3201 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3204 (defun gdb-restore-windows ()
3205 "Restore the basic arrangement of windows used by gdba.
3206 This arrangement depends on the value of `gdb-many-windows'."
3208 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3209 (delete-other-windows)
3210 (if gdb-many-windows
3212 (when (or gud-last-last-frame gdb-show-main)
3216 (if gud-last-last-frame
3217 (gud-find-file (car gud-last-last-frame))
3218 (gud-find-file gdb-main-file)))
3219 (setq gdb-source-window (selected-window))
3223 "Exit a debugging session cleanly.
3224 Kills the gdb buffers, and resets variables and the source buffers."
3225 (dolist (buffer (buffer-list))
3226 (unless (eq buffer gud-comint-buffer)
3227 (with-current-buffer buffer
3228 (if (memq gud-minor-mode '(gdbmi gdba))
3229 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3231 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3232 (setq gud-minor-mode nil)
3233 (kill-local-variable 'tool-bar-map)
3234 (kill-local-variable 'gdb-define-alist))))))
3235 (setq gdb-overlay-arrow-position nil)
3236 (setq overlay-arrow-variable-list
3237 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3238 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3239 (setq gdb-stack-position nil)
3240 (setq overlay-arrow-variable-list
3241 (delq 'gdb-stack-position overlay-arrow-variable-list))
3242 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3243 (setq gud-running nil)
3244 (setq gdb-active-process nil)
3245 (setq gdb-var-list nil)
3246 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3248 (defun gdb-source-info ()
3249 "Find the source file where the program starts and displays it with related
3251 (goto-char (point-min))
3252 (if (and (search-forward "Located in " nil t)
3253 (looking-at "\\S-+"))
3254 (setq gdb-main-file (match-string 0)))
3255 (goto-char (point-min))
3256 (if (search-forward "Includes preprocessor macro info." nil t)
3257 (setq gdb-macro-info t))
3258 (if gdb-many-windows
3260 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3262 (let ((pop-up-windows t))
3263 (display-buffer (gud-find-file gdb-main-file)))))
3266 (defun gdb-get-location (bptno line flag)
3267 "Find the directory containing the relevant source file.
3268 Put in buffer and place breakpoint icon."
3269 (goto-char (point-min))
3270 (catch 'file-not-found
3271 (if (search-forward "Located in " nil t)
3272 (when (looking-at "\\S-+")
3273 (delete (cons bptno "File not found") gdb-location-alist)
3274 (push (cons bptno (match-string 0)) gdb-location-alist))
3276 (unless (assoc bptno gdb-location-alist)
3277 (push (cons bptno "File not found") gdb-location-alist)
3278 (message-box "Cannot find source file for breakpoint location.\n\
3279 Add directory to search path for source files using the GDB command, dir."))
3280 (throw 'file-not-found nil))
3281 (with-current-buffer
3282 (find-file-noselect (match-string 0))
3284 ;; only want one breakpoint icon at each location
3286 (goto-line (string-to-number line))
3287 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
3289 (add-hook 'find-file-hook 'gdb-find-file-hook)
3291 (defun gdb-find-file-hook ()
3292 "Set up buffer for debugging if file is part of the source code
3293 of the current session."
3294 (if (and (buffer-name gud-comint-buffer)
3295 ;; in case gud or gdb-ui is just loaded
3297 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3299 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
3300 (if (member (if (string-equal gdb-version "pre-6.4")
3301 (file-name-nondirectory buffer-file-name)
3303 gdb-source-file-list)
3304 (with-current-buffer (find-buffer-visiting buffer-file-name)
3305 (gdb-init-buffer)))))
3308 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3309 "Put string PUTSTRING in front of POS in the current buffer.
3310 PUTSTRING is displayed by putting an overlay into the current buffer with a
3311 `before-string' string that has a `display' property whose value is
3313 (let ((string (make-string 1 ?x))
3314 (buffer (current-buffer)))
3315 (setq putstring (copy-sequence putstring))
3316 (let ((overlay (make-overlay pos pos buffer))
3318 (list (list 'margin 'left-margin) putstring))))
3319 (put-text-property 0 1 'display prop string)
3321 (add-text-properties 0 1 sprops string))
3322 (overlay-put overlay 'put-break t)
3323 (overlay-put overlay 'before-string string))))
3325 ;;from remove-images
3326 (defun gdb-remove-strings (start end &optional buffer)
3327 "Remove strings between START and END in BUFFER.
3328 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3329 BUFFER nil or omitted means use the current buffer."
3331 (setq buffer (current-buffer)))
3332 (dolist (overlay (overlays-in start end))
3333 (when (overlay-get overlay 'put-break)
3334 (delete-overlay overlay))))
3336 (defun gdb-put-breakpoint-icon (enabled bptno)
3337 (if (string-match "[0-9+]+\\." bptno)
3338 (setq enabled gdb-parent-bptno-enabled))
3339 (let ((start (- (line-beginning-position) 1))
3340 (end (+ (line-end-position) 1))
3341 (putstring (if enabled "B" "b"))
3342 (source-window (get-buffer-window (current-buffer) 0)))
3343 (add-text-properties
3344 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3347 (add-text-properties
3348 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3349 (add-text-properties
3350 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3351 (gdb-remove-breakpoint-icons start end)
3352 (if (display-images-p)
3353 (if (>= (or left-fringe-width
3354 (if source-window (car (window-fringes source-window)))
3355 gdb-buffer-fringe-width) 8)
3358 `(left-fringe breakpoint
3361 'breakpoint-disabled))
3363 'gdb-enabled enabled)
3364 (when (< left-margin-width 2)
3365 (save-current-buffer
3366 (setq left-margin-width 2)
3370 left-margin-width right-margin-width))))
3373 (or breakpoint-enabled-icon
3374 (setq breakpoint-enabled-icon
3375 (find-image `((:type xpm :data
3376 ,breakpoint-xpm-data
3377 :ascent 100 :pointer hand)
3379 ,breakpoint-enabled-pbm-data
3380 :ascent 100 :pointer hand)))))
3381 (or breakpoint-disabled-icon
3382 (setq breakpoint-disabled-icon
3383 (find-image `((:type xpm :data
3384 ,breakpoint-xpm-data
3385 :conversion disabled
3386 :ascent 100 :pointer hand)
3388 ,breakpoint-disabled-pbm-data
3389 :ascent 100 :pointer hand))))))
3393 (when (< left-margin-width 2)
3394 (save-current-buffer
3395 (setq left-margin-width 2)
3396 (let ((window (get-buffer-window (current-buffer) 0)))
3399 window left-margin-width right-margin-width)))))
3401 (propertize putstring
3402 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3405 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3406 (gdb-remove-strings start end)
3407 (if (display-images-p)
3408 (remove-images start end))
3410 (setq left-margin-width 0)
3411 (let ((window (get-buffer-window (current-buffer) 0)))
3414 window left-margin-width right-margin-width)))))
3418 ;; Assembler buffer.
3420 (gdb-set-buffer-rules 'gdb-assembler-buffer
3421 'gdb-assembler-buffer-name
3422 'gdb-assembler-mode)
3424 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3425 ;; window-start but keep the overlay arrow/current line visible.
3426 (defun gdb-assembler-handler ()
3427 (setq gdb-pending-triggers
3428 (delq 'gdb-invalidate-assembler
3429 gdb-pending-triggers))
3430 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3432 (with-current-buffer buf
3433 (let* ((window (get-buffer-window buf 0))
3434 (p (window-point window))
3435 (buffer-read-only nil))
3437 (insert-buffer-substring (gdb-get-buffer-create
3438 'gdb-partial-output-buffer))
3439 (set-window-point window p)))))
3440 ;; put customisation here
3441 (gdb-assembler-custom))
3443 (defun gdb-assembler-custom ()
3444 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3445 (pos 1) (address) (flag) (bptno))
3446 (with-current-buffer buffer
3448 (if (not (equal gdb-pc-address "main"))
3450 (goto-char (point-min))
3451 (if (and gdb-pc-address
3452 (search-forward gdb-pc-address nil t))
3456 (setq fringe-indicator-alist
3457 (if (string-equal gdb-frame-number "0")
3459 '((overlay-arrow . hollow-right-triangle))))
3460 (or gdb-overlay-arrow-position
3461 (setq gdb-overlay-arrow-position (make-marker)))
3462 (set-marker gdb-overlay-arrow-position (point))))))
3463 ;; remove all breakpoint-icons in assembler buffer before updating.
3464 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3465 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3466 (goto-char (point-min))
3467 (while (< (point) (- (point-max) 1))
3470 "\\([0-9]+\\.?[0-9]*\\).*?\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3471 (setq bptno (match-string 1))
3472 (setq flag (char-after (match-beginning 2)))
3473 (setq address (match-string 3))
3474 (with-current-buffer buffer
3476 (goto-char (point-min))
3477 (if (re-search-forward (concat "^0x0*" address) nil t)
3478 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
3479 (if (not (equal gdb-pc-address "main"))
3480 (with-current-buffer buffer
3481 (set-window-point (get-buffer-window buffer 0) pos)))))
3483 (defvar gdb-assembler-mode-map
3484 (let ((map (make-sparse-keymap)))
3485 (suppress-keymap map)
3486 (define-key map "q" 'kill-this-buffer)
3489 (defvar gdb-assembler-font-lock-keywords
3490 '(;; <__function.name+n>
3491 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3492 (1 font-lock-function-name-face))
3493 ;; 0xNNNNNNNN <__function.name+n>: opcode
3494 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3495 (4 font-lock-keyword-face))
3496 ;; %register(at least i386)
3497 ("%\\sw+" . font-lock-variable-name-face)
3498 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3499 (1 font-lock-comment-face)
3500 (2 font-lock-function-name-face))
3501 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3502 "Font lock keywords used in `gdb-assembler-mode'.")
3504 (defun gdb-assembler-mode ()
3505 "Major mode for viewing code assembler.
3507 \\{gdb-assembler-mode-map}"
3508 (kill-all-local-variables)
3509 (setq major-mode 'gdb-assembler-mode)
3510 (setq mode-name (concat "Machine:" gdb-selected-frame))
3511 (setq gdb-overlay-arrow-position nil)
3512 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3513 (setq fringes-outside-margins t)
3514 (setq buffer-read-only t)
3515 (buffer-disable-undo)
3516 (gdb-thread-identification)
3517 (use-local-map gdb-assembler-mode-map)
3518 (gdb-invalidate-assembler)
3519 (set (make-local-variable 'font-lock-defaults)
3520 '(gdb-assembler-font-lock-keywords))
3521 (run-mode-hooks 'gdb-assembler-mode-hook)
3522 'gdb-invalidate-assembler)
3524 (defun gdb-assembler-buffer-name ()
3525 (with-current-buffer gud-comint-buffer
3526 (concat "*disassembly of " (gdb-get-target-string) "*")))
3528 (defun gdb-display-assembler-buffer ()
3529 "Display disassembly view."
3531 (setq gdb-previous-frame nil)
3533 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3535 (defun gdb-frame-assembler-buffer ()
3536 "Display disassembly view in a new frame."
3538 (setq gdb-previous-frame nil)
3539 (let ((special-display-regexps (append special-display-regexps '(".*")))
3540 (special-display-frame-alist gdb-frame-parameters))
3541 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3543 ;; modified because if gdb-pc-address has changed value a new command
3544 ;; must be enqueued to update the buffer with the new output
3545 (defun gdb-invalidate-assembler (&optional ignored)
3546 (if (gdb-get-buffer 'gdb-assembler-buffer)
3548 (unless (and gdb-selected-frame
3549 (string-equal gdb-selected-frame gdb-previous-frame))
3550 (if (or (not (member 'gdb-invalidate-assembler
3551 gdb-pending-triggers))
3552 (not (equal (string-to-number gdb-pc-address)
3554 gdb-previous-frame-pc-address))))
3556 ;; take previous disassemble command, if any, off the queue
3557 (with-current-buffer gud-comint-buffer
3558 (let ((queue gdb-input-queue))
3559 (dolist (item queue)
3560 (if (equal (cdr item) '(gdb-assembler-handler))
3561 (setq gdb-input-queue
3562 (delete item gdb-input-queue))))))
3565 (concat gdb-server-prefix "disassemble " gdb-pc-address "\n")
3566 'gdb-assembler-handler))
3567 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3568 (setq gdb-previous-frame-pc-address gdb-pc-address)
3569 (setq gdb-previous-frame gdb-selected-frame)))))))
3571 (defun gdb-get-selected-frame ()
3572 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3574 (if (string-equal gdb-version "pre-6.4")
3576 (list (concat gdb-server-prefix "info frame\n")
3577 'gdb-frame-handler))
3579 (list "server interpreter mi -stack-info-frame\n"
3580 'gdb-frame-handler-1)))
3581 (push 'gdb-get-selected-frame gdb-pending-triggers))))
3583 (defun gdb-frame-handler ()
3584 (setq gdb-pending-triggers
3585 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3586 (goto-char (point-min))
3587 (when (re-search-forward
3588 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3589 (setq gdb-frame-number (match-string 1))
3590 (setq gdb-frame-address (match-string 2)))
3591 (goto-char (point-min))
3592 (when (re-search-forward ".*=\\s-+\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3593 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3595 (setq gdb-selected-frame (match-string 2))
3596 (if (gdb-get-buffer 'gdb-locals-buffer)
3597 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3598 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3599 (if (gdb-get-buffer 'gdb-assembler-buffer)
3600 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3601 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3602 (setq gdb-pc-address (match-string 1))
3603 (if (and (match-string 3) gud-overlay-arrow-position)
3604 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3605 (position (marker-position gud-overlay-arrow-position)))
3607 (string-equal (file-name-nondirectory
3608 (buffer-file-name buffer))
3609 (file-name-nondirectory (match-string 3))))
3610 (with-current-buffer buffer
3611 (setq fringe-indicator-alist
3612 (if (string-equal gdb-frame-number "0")
3614 '((overlay-arrow . hollow-right-triangle))))
3615 (set-marker gud-overlay-arrow-position position))))))
3616 (goto-char (point-min))
3617 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3618 (setq gdb-current-language (match-string 1)))
3619 (gdb-invalidate-assembler))
3622 ;; Code specific to GDB 6.4
3623 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3625 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3626 "Create list of source files for current GDB session.
3627 If buffers already exist for any of these files, `gud-minor-mode'
3629 (goto-char (point-min))
3630 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3631 (push (match-string 1) gdb-source-file-list))
3632 (dolist (buffer (buffer-list))
3633 (with-current-buffer buffer
3634 (when (member buffer-file-name gdb-source-file-list)
3635 (gdb-init-buffer))))
3636 (gdb-force-mode-line-update
3637 (propertize "ready" 'face font-lock-variable-name-face)))
3639 ;; Used for -stack-info-frame but could be used for -stack-list-frames too.
3640 (defconst gdb-stack-list-frames-regexp
3641 ".*?level=\"\\(.*?\\)\",.*?addr=\"\\(.*?\\)\",.*?func=\"\\(.*?\\)\",\
3642 \\(?:.*?file=\".*?\",.*?fullname=\"\\(.*?\\)\",.*?line=\"\\(.*?\\)\".*?}\\|\
3643 from=\"\\(.*?\\)\"\\)")
3645 (defun gdb-frame-handler-1 ()
3646 (setq gdb-pending-triggers
3647 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3648 (goto-char (point-min))
3649 (when (re-search-forward gdb-stack-list-frames-regexp nil t)
3650 (setq gdb-frame-number (match-string 1))
3651 (setq gdb-pc-address (match-string 2))
3652 (setq gdb-selected-frame (match-string 3))
3653 (if (gdb-get-buffer 'gdb-locals-buffer)
3654 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3655 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3656 (if (gdb-get-buffer 'gdb-assembler-buffer)
3657 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3658 (setq mode-name (concat "Machine:" gdb-selected-frame)))))
3659 (gdb-invalidate-assembler))
3661 ; Uses "-var-list-children --all-values". Needs GDB 6.4 onwards.
3662 (defun gdb-var-list-children-1 (varnum)
3665 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3666 (concat "server interpreter mi \"-var-list-children --all-values \\\""
3668 (concat "-var-list-children --all-values \"" varnum "\"\n"))
3669 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3671 (defconst gdb-var-list-children-regexp-1
3672 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3673 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3674 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3676 (defun gdb-var-list-children-handler-1 (varnum)
3677 (goto-char (point-min))
3678 (let ((var-list nil))
3679 (catch 'child-already-watched
3680 (dolist (var gdb-var-list)
3681 (if (string-equal varnum (car var))
3684 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3685 (let ((varchild (list (match-string 1)
3689 (read (match-string 4))
3691 (if (assoc (car varchild) gdb-var-list)
3692 (throw 'child-already-watched nil))
3693 (push varchild var-list))))
3694 (push var var-list)))
3695 (setq gdb-var-list (nreverse var-list))))
3696 (gdb-speedbar-update))
3698 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3699 (defun gdb-var-update-1 ()
3700 (if (not (member 'gdb-var-update gdb-pending-triggers))
3704 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3705 "server interpreter mi \"-var-update --all-values *\"\n"
3706 "-var-update --all-values *\n")
3707 'gdb-var-update-handler-1))
3708 (push 'gdb-var-update gdb-pending-triggers))))
3710 (defconst gdb-var-update-regexp-1
3711 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3712 in_scope=\"\\(.*?\\)\".*?}")
3714 (defun gdb-var-update-handler-1 ()
3715 (dolist (var gdb-var-list)
3716 (setcar (nthcdr 5 var) nil))
3717 (goto-char (point-min))
3718 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3719 (let* ((varnum (match-string 1))
3720 (var (assoc varnum gdb-var-list)))
3722 (let ((match (match-string 3)))
3723 (cond ((string-equal match "false")
3724 (if gdb-delete-out-of-scope
3725 (gdb-var-delete-1 varnum)
3726 (setcar (nthcdr 5 var) 'out-of-scope)))
3727 ((string-equal match "true")
3728 (setcar (nthcdr 5 var) 'changed)
3729 (setcar (nthcdr 4 var)
3730 (read (match-string 2))))
3731 ((string-equal match "invalid")
3732 (gdb-var-delete-1 varnum)))))))
3733 (setq gdb-pending-triggers
3734 (delq 'gdb-var-update gdb-pending-triggers))
3735 (gdb-speedbar-update))
3737 ;; Registers buffer.
3739 (gdb-set-buffer-rules 'gdb-registers-buffer
3740 'gdb-registers-buffer-name
3741 'gdb-registers-mode)
3743 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3744 (gdb-get-buffer 'gdb-registers-buffer)
3745 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3746 "server interpreter mi \"-data-list-register-values x\"\n"
3747 "-data-list-register-values x\n")
3748 gdb-data-list-register-values-handler)
3750 (defconst gdb-data-list-register-values-regexp
3751 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3753 (defun gdb-data-list-register-values-handler ()
3754 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3755 gdb-pending-triggers))
3756 (goto-char (point-min))
3757 (if (re-search-forward gdb-error-regexp nil t)
3758 (let ((err (match-string 1)))
3759 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3760 (let ((buffer-read-only nil))
3762 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3764 (goto-char (point-min)))))
3765 (let ((register-list (reverse gdb-register-names))
3766 (register nil) (register-string nil) (register-values nil))
3767 (goto-char (point-min))
3768 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3769 (setq register (pop register-list))
3770 (setq register-string (concat register "\t" (match-string 2) "\n"))
3771 (if (member (match-string 1) gdb-changed-registers)
3772 (put-text-property 0 (length register-string)
3773 'face 'font-lock-warning-face
3775 (setq register-values
3776 (concat register-values register-string)))
3777 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3778 (with-current-buffer buf
3779 (let* ((window (get-buffer-window buf 0))
3780 (start (window-start window))
3781 (p (window-point window))
3782 (buffer-read-only nil))
3784 (insert register-values)
3785 (set-window-start window start)
3786 (set-window-point window p))))))
3787 (gdb-data-list-register-values-custom))
3789 (defun gdb-data-list-register-values-custom ()
3790 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3792 (let ((buffer-read-only nil)
3794 (goto-char (point-min))
3795 (while (< (point) (point-max))
3796 (setq start (line-beginning-position))
3797 (setq end (line-end-position))
3798 (when (looking-at "^[^\t]+")
3799 (unless (string-equal (match-string 0) "No registers.")
3800 (put-text-property start (match-end 0)
3801 'face font-lock-variable-name-face)
3802 (add-text-properties start end
3803 '(help-echo "mouse-2: edit value"
3804 mouse-face highlight))))
3805 (forward-line 1))))))
3807 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3808 (defun gdb-get-changed-registers ()
3809 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3810 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3814 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3815 "server interpreter mi -data-list-changed-registers\n"
3816 "-data-list-changed-registers\n")
3817 'gdb-get-changed-registers-handler))
3818 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3820 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3822 (defun gdb-get-changed-registers-handler ()
3823 (setq gdb-pending-triggers
3824 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3825 (setq gdb-changed-registers nil)
3826 (goto-char (point-min))
3827 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3828 (push (match-string 1) gdb-changed-registers)))
3833 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3834 (gdb-set-buffer-rules 'gdb-locals-buffer
3835 'gdb-locals-buffer-name
3838 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3839 (gdb-get-buffer 'gdb-locals-buffer)
3840 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3841 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3842 "-stack-list-locals --simple-values\n")
3843 gdb-stack-list-locals-handler)
3845 (defconst gdb-stack-list-locals-regexp
3846 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3848 (defvar gdb-locals-watch-map-1
3849 (let ((map (make-sparse-keymap)))
3850 (suppress-keymap map)
3851 (define-key map "\r" 'gud-watch)
3852 (define-key map [mouse-2] 'gud-watch)
3854 "Keymap to create watch expression of a complex data type local variable.")
3856 (defvar gdb-edit-locals-map-1
3857 (let ((map (make-sparse-keymap)))
3858 (suppress-keymap map)
3859 (define-key map "\r" 'gdb-edit-locals-value)
3860 (define-key map [mouse-2] 'gdb-edit-locals-value)
3862 "Keymap to edit value of a simple data type local variable.")
3864 (defun gdb-edit-locals-value (&optional event)
3865 "Assign a value to a variable displayed in the locals buffer."
3866 (interactive (list last-input-event))
3868 (if event (posn-set-point (event-end event)))
3870 (let* ((var (current-word))
3871 (value (read-string (format "New value (%s): " var))))
3873 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3876 ;; Dont display values of arrays or structures.
3877 ;; These can be expanded using gud-watch.
3878 (defun gdb-stack-list-locals-handler ()
3879 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3880 gdb-pending-triggers))
3881 (goto-char (point-min))
3882 (if (re-search-forward gdb-error-regexp nil t)
3883 (let ((err (match-string 1)))
3884 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3885 (let ((buffer-read-only nil))
3888 (goto-char (point-min)))))
3889 (let (local locals-list)
3890 (goto-char (point-min))
3891 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3892 (let ((local (list (match-string 1)
3895 (if (looking-at ",value=\\(\".*\"\\).*?}")
3896 (setcar (nthcdr 2 local) (read (match-string 1))))
3897 (push local locals-list)))
3898 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3899 (and buf (with-current-buffer buf
3900 (let* ((window (get-buffer-window buf 0))
3901 (start (window-start window))
3902 (p (window-point window))
3903 (buffer-read-only nil) (name) (value))
3905 (dolist (local locals-list)
3906 (setq name (car local))
3907 (setq value (nth 2 local))
3909 (string-match "^\\0x" value))
3910 (add-text-properties 0 (length name)
3911 `(mouse-face highlight
3912 help-echo "mouse-2: create watch expression"
3913 local-map ,gdb-locals-watch-map-1)
3915 (add-text-properties 0 (length value)
3916 `(mouse-face highlight
3917 help-echo "mouse-2: edit value"
3918 local-map ,gdb-edit-locals-map-1)
3921 (concat name "\t" (nth 1 local)
3923 (set-window-start window start)
3924 (set-window-point window p))))))))
3926 (defun gdb-get-register-names ()
3927 "Create a list of register names."
3928 (goto-char (point-min))
3929 (setq gdb-register-names nil)
3930 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3931 (push (match-string 1) gdb-register-names)))
3935 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3936 ;;; gdb-ui.el ends here