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
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.
82 ;; 1) Cannot handle multiple debug sessions.
83 ;; 2) If you wish to call procedures from your program in GDB
84 ;; e.g "call myproc ()", "p mysquare (5)" then use level 2 annotations
85 ;; "gdb --annotate=2 myprog" to keep source buffer/selected frame fixed.
86 ;; 3) After detaching from a process, clicking on the "GO" icon on toolbar
87 ;; (gud-go) sends "continue" to GDB (should be "run").
91 ;; 1) Use MI command -data-read-memory for memory window.
92 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
93 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
100 (defvar speedbar-initial-expansion-list-name)
102 (defvar gdb-pc-address nil "Initialization for Assembler buffer.
103 Set to \"main\" at start if gdb-show-main is t.")
104 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
105 (defvar gdb-previous-frame-address nil)
106 (defvar gdb-memory-address "main")
107 (defvar gdb-previous-frame nil)
108 (defvar gdb-selected-frame nil)
109 (defvar gdb-frame-number nil)
110 (defvar gdb-current-language nil)
111 (defvar gdb-var-list nil
112 "List of variables in watch window.
113 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS FP)
114 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
115 address for root variables.")
116 (defvar gdb-main-file nil "Source file from which program execution begins.")
117 (defvar gud-old-arrow nil)
118 (defvar gdb-overlay-arrow-position nil)
119 (defvar gdb-stack-position nil)
120 (defvar gdb-server-prefix nil)
121 (defvar gdb-flush-pending-output nil)
122 (defvar gdb-location-alist nil
123 "Alist of breakpoint numbers and full filenames. Only used for files that
125 (defvar gdb-active-process nil
126 "GUD tooltips display variable values when t, and macro definitions otherwise.")
127 (defvar gdb-error "Non-nil when GDB is reporting an error.")
128 (defvar gdb-macro-info nil
129 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
130 (defvar gdb-buffer-fringe-width nil)
131 (defvar gdb-signalled nil)
132 (defvar gdb-source-window nil)
133 (defvar gdb-inferior-status nil)
134 (defvar gdb-continuation nil)
135 (defvar gdb-look-up-stack nil)
136 (defvar gdb-frame-begin nil
137 "Non-nil when GDB generates frame-begin annotation.")
138 (defvar gdb-printing t)
139 (defvar gdb-parent-bptno-enabled nil)
141 (defvar gdb-buffer-type nil
142 "One of the symbols bound in `gdb-buffer-rules'.")
143 (make-variable-buffer-local 'gdb-buffer-type)
145 (defvar gdb-input-queue ()
146 "A list of gdb command objects.")
148 (defvar gdb-prompting nil
149 "True when gdb is idle with no pending input.")
151 (defvar gdb-output-sink 'user
152 "The disposition of the output of the current gdb command.
153 Possible values are these symbols:
155 `user' -- gdb output should be copied to the GUD buffer
158 `inferior' -- gdb output should be copied to the inferior-io buffer.
160 `pre-emacs' -- output should be ignored util the post-prompt
161 annotation is received. Then the output-sink
163 `emacs' -- output should be collected in the partial-output-buffer
164 for subsequent processing by a command. This is the
165 disposition of output generated by commands that
166 gdb mode sends to gdb on its own behalf.
167 `post-emacs' -- ignore output until the prompt annotation is
168 received, then go to USER disposition.
170 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
171 \(`user' and `emacs').")
173 (defvar gdb-current-item nil
174 "The most recent command item sent to gdb.")
176 (defvar gdb-pending-triggers '()
177 "A list of trigger functions that have run later than their output
180 (defvar gdb-first-post-prompt nil)
181 (defvar gdb-version nil)
182 (defvar gdb-locals-font-lock-keywords nil)
183 (defvar gdb-source-file-list nil
184 "List of source files for the current executable")
185 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
187 (defvar gdb-locals-font-lock-keywords-1
189 ;; var = (struct struct_tag) value
190 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
191 (1 font-lock-variable-name-face)
192 (3 font-lock-keyword-face)
193 (4 font-lock-type-face))
194 ;; var = (type) value
195 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
196 (1 font-lock-variable-name-face)
197 (3 font-lock-type-face))
199 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
200 (1 font-lock-variable-name-face))
202 "Font lock keywords used in `gdb-local-mode'.")
204 (defvar gdb-locals-font-lock-keywords-2
207 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
208 (1 font-lock-variable-name-face)
209 (3 font-lock-type-face))
211 "Font lock keywords used in `gdb-local-mode'.")
213 ;; Variables for GDB 6.4+
214 (defvar gdb-register-names nil "List of register names.")
215 (defvar gdb-changed-registers nil
216 "List of changed register numbers (strings).")
219 (defun gdb (command-line)
220 "Run gdb on program FILE in buffer *gud-FILE*.
221 The directory containing FILE becomes the initial working
222 directory and source-file directory for your debugger.
224 If `gdb-many-windows' is nil (the default value) then gdb just
225 pops up the GUD buffer unless `gdb-show-main' is t. In this case
226 it starts with two windows: one displaying the GUD buffer and the
227 other with the source file with the main routine of the inferior.
229 If `gdb-many-windows' is t, regardless of the value of
230 `gdb-show-main', the layout below will appear unless
231 `gdb-use-separate-io-buffer' is nil when the source buffer
232 occupies the full width of the frame. Keybindings are shown in
235 Watch expressions appear in the speedbar/slowbar.
237 The following commands help control operation :
239 `gdb-many-windows' - Toggle the number of windows gdb uses.
240 `gdb-restore-windows' - To restore the window layout.
242 See Info node `(emacs)GDB Graphical Interface' for a more
243 detailed description of this mode.
246 +----------------------------------------------------------------------+
248 +-----------------------------------+----------------------------------+
249 | GUD buffer (I/O of GDB) | Locals buffer |
253 +-----------------------------------+----------------------------------+
254 | Source buffer | I/O buffer (of debugged program) |
262 +-----------------------------------+----------------------------------+
263 | Stack buffer | Breakpoints buffer |
264 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
265 | | RET gdb-goto-breakpoint |
266 | | D gdb-delete-breakpoint |
267 +-----------------------------------+----------------------------------+
269 To run GDB in text command mode, replace the GDB \"--annotate=3\"
270 option with \"--fullname\" either in the minibuffer for the
271 current Emacs session, or the custom variable
272 `gud-gdb-command-name' for all future sessions. You need to use
273 text command mode to debug multiple programs within one Emacs
275 (interactive (list (gud-query-cmdline 'gdb)))
277 (when (and gud-comint-buffer
278 (buffer-name gud-comint-buffer)
279 (get-buffer-process gud-comint-buffer)
280 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
281 (gdb-restore-windows)
283 "Multiple debugging requires restarting in text command mode"))
285 (gud-common-init command-line nil 'gud-gdba-marker-filter)
286 (set (make-local-variable 'gud-minor-mode) 'gdba)
288 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
289 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
290 "Set temporary breakpoint at current line.")
291 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
292 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
293 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
294 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
295 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
296 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
297 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
299 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
300 "\C-j" "Set execution address to current line.")
302 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
303 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
304 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
305 (gud-def gud-pstar "print* %e" nil
306 "Evaluate C dereferenced pointer expression at point.")
308 ;; For debugging Emacs only.
309 (gud-def gud-pv "pv1 %e" "\C-v" "Print the value of the lisp variable.")
311 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
312 (gud-def gud-run "run" nil "Run the program.")
314 (local-set-key "\C-i" 'gud-gdb-complete-command)
315 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
316 (setq paragraph-start comint-prompt-regexp)
317 (setq gdb-first-prompt t)
318 (setq gud-running nil)
320 (setq gud-filter-pending-text nil)
321 (run-hooks 'gdb-mode-hook))
323 (defcustom gdb-debug-log-max 128
324 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
326 :type '(choice (integer :tag "Number of elements")
327 (const :tag "Unlimited" nil))
330 (defvar gdb-debug-log nil
331 "List of commands sent to and replies received from GDB. Most
332 recent commands are listed first. This list stores only the last
333 'gdb-debug-log-max' values. This variable is used to debug
337 (defcustom gdb-enable-debug nil
338 "Non-nil means record the process input and output in `gdb-debug-log'."
343 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
344 "Shell command for generating a list of defined macros in a source file.
345 This list is used to display the #define directive associated
346 with an identifier as a tooltip. It works in a debug session with
347 GDB, when gud-tooltip-mode is t.
349 Set `gdb-cpp-define-alist-flags' for any include paths or
355 (defcustom gdb-cpp-define-alist-flags ""
356 "Preprocessor flags for `gdb-cpp-define-alist-program'."
361 (defcustom gdb-show-main nil
362 "Non-nil means display source file containing the main routine at startup.
363 Also display the main routine in the disassembly buffer if present."
368 (defcustom gdb-many-windows nil
369 "If nil, just pop up the GUD buffer unless `gdb-show-main' is t.
370 In this case start with two windows: one displaying the GUD
371 buffer and the other with the source file with the main routine
372 of the debugged program. Non-nil means display the layout shown
378 (defcustom gdb-use-separate-io-buffer nil
379 "Non-nil means display output from the debugged program in a separate buffer."
384 (defun gdb-force-mode-line-update (status)
385 (let ((buffer gud-comint-buffer))
386 (if (and buffer (buffer-name buffer))
387 (with-current-buffer buffer
388 (setq mode-line-process
390 (process-status (get-buffer-process buffer)) status))
391 ;; Force mode line redisplay soon.
392 (force-mode-line-update)))))
394 (defun gdb-many-windows (arg)
395 "Toggle the number of windows in the basic arrangement.
396 With prefix argument ARG, display additional buffers if ARG is positive,
397 otherwise use a single window."
399 (setq gdb-many-windows
401 (not gdb-many-windows)
402 (> (prefix-numeric-value arg) 0)))
403 (message (format "Display of other windows %sabled"
404 (if gdb-many-windows "en" "dis")))
405 (if (and gud-comint-buffer
406 (buffer-name gud-comint-buffer))
408 (gdb-restore-windows)
411 (defun gdb-use-separate-io-buffer (arg)
412 "Toggle separate IO for debugged program.
413 With prefix argument ARG, use separate IO if ARG is positive,
416 (setq gdb-use-separate-io-buffer
418 (not gdb-use-separate-io-buffer)
419 (> (prefix-numeric-value arg) 0)))
420 (message (format "Separate IO %sabled"
421 (if gdb-use-separate-io-buffer "en" "dis")))
422 (if (and gud-comint-buffer
423 (buffer-name gud-comint-buffer))
425 (if gdb-use-separate-io-buffer
426 (if gdb-many-windows (gdb-restore-windows))
427 (kill-buffer (gdb-inferior-io-name)))
430 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
432 (defun gdb-create-define-alist ()
433 "Create an alist of #define directives for GUD tooltips."
434 (let* ((file (buffer-file-name))
436 (with-output-to-string
437 (with-current-buffer standard-output
438 (call-process shell-file-name
439 (if (file-exists-p file) file nil)
440 (list t nil) nil "-c"
441 (concat gdb-cpp-define-alist-program " "
442 gdb-cpp-define-alist-flags)))))
443 (define-list (split-string output "\n" t)) (name))
444 (setq gdb-define-alist nil)
445 (dolist (define define-list)
446 (setq name (nth 1 (split-string define "[( ]")))
447 (push (cons name define) gdb-define-alist))))
449 (defun gdb-tooltip-print (expr)
451 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
452 (goto-char (point-min))
454 (if (search-forward "=" nil t)
455 (concat expr (buffer-substring (- (point) 2) (point-max)))
457 ;; remove newline for gud-tooltip-echo-area
458 (substring string 0 (- (length string) 1))))
459 (or gud-tooltip-echo-area tooltip-use-echo-area)))
461 ;; If expr is a macro for a function don't print because of possible dangerous
462 ;; side-effects. Also printing a function within a tooltip generates an
463 ;; unexpected starting annotation (phase error).
464 (defun gdb-tooltip-print-1 (expr)
465 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
466 (goto-char (point-min))
467 (if (search-forward "expands to: " nil t)
468 (unless (looking-at "\\S-+.*(.*).*")
470 (list (concat gdb-server-prefix "print " expr "\n")
471 `(lambda () (gdb-tooltip-print ,expr))))))))
473 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
475 (defun gdb-set-gud-minor-mode-existing-buffers ()
476 "Create list of source files for current GDB session."
477 (goto-char (point-min))
478 (when (search-forward "read in on demand:" nil t)
479 (while (re-search-forward gdb-source-file-regexp nil t)
480 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
481 gdb-source-file-list))
482 (dolist (buffer (buffer-list))
483 (with-current-buffer buffer
484 (when (and buffer-file-name
485 (member (file-name-nondirectory buffer-file-name)
486 gdb-source-file-list))
487 (set (make-local-variable 'gud-minor-mode) 'gdba)
488 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
489 (when gud-tooltip-mode
490 (make-local-variable 'gdb-define-alist)
491 (gdb-create-define-alist)
492 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
493 (gdb-force-mode-line-update
494 (propertize "ready" 'face font-lock-variable-name-face)))
496 (defun gdb-find-watch-expression ()
497 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
498 (varnum (car var)) expr array)
499 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
500 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
501 (component-list (split-string (match-string 2 varnum) "\\." t)))
502 (setq expr (nth 1 var1))
503 (setq varnumlet (car var1))
504 (dolist (component component-list)
505 (setq var2 (assoc varnumlet gdb-var-list))
506 (setq expr (concat expr
507 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
508 (concat "[" component "]")
509 (concat "." component))))
510 (setq varnumlet (concat varnumlet "." component)))
514 (gud-def gud-break (if (not (string-match "Machine" mode-name))
515 (gud-call "break %f:%l" arg)
519 (gud-call "break *%a" arg)))
520 "\C-b" "Set breakpoint at current line or address.")
522 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
523 (gud-call "clear %f:%l" arg)
527 (gud-call "clear *%a" arg)))
528 "\C-d" "Remove breakpoint at current line or address.")
530 (gud-def gud-until (if (not (string-match "Machine" mode-name))
531 (gud-call "until %f:%l" arg)
535 (gud-call "until *%a" arg)))
536 "\C-u" "Continue to current line or address.")
538 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
539 nil "Start or continue execution.")
541 ;; For debugging Emacs only.
545 "pp1 " (if (eq (buffer-local-value
546 'major-mode (window-buffer)) 'speedbar-mode)
547 (gdb-find-watch-expression) "%e")) arg)
548 nil "Print the emacs s-expression.")
550 (define-key gud-minor-mode-map [left-margin mouse-1]
551 'gdb-mouse-set-clear-breakpoint)
552 (define-key gud-minor-mode-map [left-fringe mouse-1]
553 'gdb-mouse-set-clear-breakpoint)
554 (define-key gud-minor-mode-map [left-margin C-mouse-1]
555 'gdb-mouse-toggle-breakpoint-margin)
556 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
557 'gdb-mouse-toggle-breakpoint-fringe)
559 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
561 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
563 (define-key gud-minor-mode-map [left-margin mouse-3]
565 (define-key gud-minor-mode-map [left-fringe mouse-3]
568 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
570 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
572 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
574 (define-key gud-minor-mode-map [left-margin C-mouse-3]
577 (setq comint-input-sender 'gdb-send)
580 (setq gdb-pc-address (if gdb-show-main "main" nil))
581 (setq gdb-previous-frame-address nil
582 gdb-memory-address "main"
583 gdb-previous-frame nil
584 gdb-selected-frame nil
585 gdb-current-language nil
589 gdb-first-post-prompt t
593 gdb-pending-triggers nil
594 gdb-output-sink 'user
595 gdb-server-prefix "server "
596 gdb-flush-pending-output nil
597 gdb-location-alist nil
598 gdb-source-file-list nil
601 gdb-buffer-fringe-width (car (window-fringes))
604 gdb-source-window nil
605 gdb-inferior-status nil
607 gdb-look-up-stack nil
612 (setq gdb-buffer-type 'gdba)
614 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
616 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
617 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
621 (if (eq window-system 'w32)
622 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
623 (gdb-enqueue-input (list "set height 0\n" 'ignore))
624 (gdb-enqueue-input (list "set width 0\n" 'ignore))
626 (if (string-equal gdb-version "pre-6.4")
628 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
629 'gdb-set-gud-minor-mode-existing-buffers))
630 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
632 (list "server interpreter mi -data-list-register-names\n"
633 'gdb-get-register-names))
634 ; Needs GDB 6.2 onwards.
636 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
637 'gdb-set-gud-minor-mode-existing-buffers-1))
638 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
640 ;; Find source file and compilation directory here.
641 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
642 (gdb-enqueue-input (list "server list\n" 'ignore))
643 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
645 (run-hooks 'gdb-mode-hook))
647 (defun gdb-get-version ()
648 (goto-char (point-min))
649 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
650 (setq gdb-version "pre-6.4")
651 (setq gdb-version "6.4+"))
654 (defmacro gdb-if-arrow (arrow-position &rest body)
656 (let ((buffer (marker-buffer ,arrow-position)) (line))
657 (if (equal buffer (window-buffer (posn-window end)))
658 (with-current-buffer buffer
659 (when (or (equal start end)
660 (equal (posn-point start)
661 (marker-position ,arrow-position)))
664 (defun gdb-mouse-until (event)
665 "Continue running until a source line past the current line.
666 The destination source line can be selected either by clicking
667 with mouse-3 on the fringe/margin or dragging the arrow
668 with mouse-1 (default bindings)."
670 (let ((start (event-start event))
671 (end (event-end event)))
672 (gdb-if-arrow gud-overlay-arrow-position
673 (setq line (line-number-at-pos (posn-point end)))
674 (gud-call (concat "until " (number-to-string line))))
675 (gdb-if-arrow gdb-overlay-arrow-position
677 (goto-line (line-number-at-pos (posn-point end)))
679 (gud-call (concat "until *%a"))))))
681 (defun gdb-mouse-jump (event)
682 "Set execution address/line.
683 The destination source line can be selected either by clicking with C-mouse-3
684 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
685 Unlike gdb-mouse-until the destination address can be before the current
686 line, and no execution takes place."
688 (let ((start (event-start event))
689 (end (event-end event)))
690 (gdb-if-arrow gud-overlay-arrow-position
691 (setq line (line-number-at-pos (posn-point end)))
693 (gud-call (concat "tbreak " (number-to-string line)))
694 (gud-call (concat "jump " (number-to-string line)))))
695 (gdb-if-arrow gdb-overlay-arrow-position
697 (goto-line (line-number-at-pos (posn-point end)))
700 (gud-call (concat "tbreak *%a"))
701 (gud-call (concat "jump *%a")))))))
703 (defcustom gdb-speedbar-auto-raise nil
704 "If non-nil raise speedbar every time display of watch expressions is\
710 (defun gdb-speedbar-auto-raise (arg)
711 "Toggle automatic raising of the speedbar for watch expressions.
712 With prefix argument ARG, automatically raise speedbar if ARG is
713 positive, otherwise don't automatically raise it."
715 (setq gdb-speedbar-auto-raise
717 (not gdb-speedbar-auto-raise)
718 (> (prefix-numeric-value arg) 0)))
719 (message (format "Auto raising %sabled"
720 (if gdb-speedbar-auto-raise "en" "dis"))))
722 (defcustom gdb-use-colon-colon-notation nil
723 "If non-nil use FUN::VAR format to display variables in the speedbar."
728 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
729 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
731 (defun gud-watch (&optional arg event)
732 "Watch expression at point.
733 With arg, enter name of variable to be watched in the minibuffer."
734 (interactive (list current-prefix-arg last-input-event))
735 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
736 (if (memq minor-mode '(gdbmi gdba))
738 (if event (posn-set-point (event-end event)))
740 (save-selected-window
743 (completing-read "Name of variable: "
744 'gud-gdb-complete-command)
745 (if (and transient-mark-mode mark-active)
746 (buffer-substring (region-beginning) (region-end))
747 (tooltip-identifier-from-point (point))))))
749 (set-text-properties 0 (length expr) nil expr)
752 (if (eq minor-mode 'gdba)
754 "server interpreter mi \"-var-create - * " expr "\"\n")
755 (concat"-var-create - * " expr "\n"))
756 `(lambda () (gdb-var-create-handler ,expr)))))))
757 (message "gud-watch is a no-op in this mode."))))
759 (defconst gdb-var-create-regexp
760 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",\\(?:.*value=\\(\".*\"\\),\\)?.*type=\"\\(.*?\\)\"")
762 (defun gdb-var-create-handler (expr)
763 (goto-char (point-min))
764 (if (re-search-forward gdb-var-create-regexp nil t)
767 (if (and (string-equal gdb-current-language "c")
768 gdb-use-colon-colon-notation gdb-selected-frame)
769 (setq expr (concat gdb-selected-frame "::" expr))
773 (if (match-string 3) (read (match-string 3)))
774 nil gdb-frame-address)))
775 (push var gdb-var-list)
776 (unless (string-equal
777 speedbar-initial-expansion-list-name "GUD")
778 (speedbar-change-initial-expansion-list "GUD"))
782 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
784 (concat "server interpreter mi \"0-var-evaluate-expression "
786 (concat "0-var-evaluate-expression " (car var) "\n"))
787 `(lambda () (gdb-var-evaluate-expression-handler
789 (if (search-forward "Undefined command" nil t)
790 (message-box "Watching expressions requires GDB 6.0 onwards")
791 (message-box "No symbol \"%s\" in current context." expr))))
793 (defun gdb-speedbar-update ()
794 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
795 (not (member 'gdb-speedbar-timer gdb-pending-triggers)))
796 ;; Dummy command to update speedbar even when idle.
797 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
798 ;; Keep gdb-pending-triggers non-nil till end.
799 (push 'gdb-speedbar-timer gdb-pending-triggers)))
801 (defun gdb-speedbar-timer-fn ()
802 (setq gdb-pending-triggers
803 (delq 'gdb-speedbar-timer gdb-pending-triggers))
806 (defun gdb-var-evaluate-expression-handler (varnum changed)
807 (goto-char (point-min))
808 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t)
809 (setq gdb-pending-triggers
810 (delq (string-to-number (match-string 1)) gdb-pending-triggers))
811 (let ((var (assoc varnum gdb-var-list)))
813 (if changed (setcar (nthcdr 5 var) 'changed))
814 (setcar (nthcdr 4 var) (read (match-string 2)))))
815 (gdb-speedbar-update))
817 (defun gdb-var-list-children (varnum)
819 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
820 `(lambda () (gdb-var-list-children-handler ,varnum)))))
822 (defconst gdb-var-list-children-regexp
823 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
824 numchild=\"\\(.*?\\)\"\\(}\\|,.*?\\(type=\"\\(.*?\\)\"\\)?.*?}\\)")
826 (defun gdb-var-list-children-handler (varnum)
827 (goto-char (point-min))
828 (let ((var-list nil))
829 (catch 'child-already-watched
830 (dolist (var gdb-var-list)
831 (if (string-equal varnum (car var))
834 (while (re-search-forward gdb-var-list-children-regexp nil t)
835 (let ((varchild (list (match-string 1)
840 (if (assoc (car varchild) gdb-var-list)
841 (throw 'child-already-watched nil))
842 (push varchild var-list)
846 "server interpreter mi \"0-var-evaluate-expression "
847 (car varchild) "\"\n")
848 `(lambda () (gdb-var-evaluate-expression-handler
849 ,(car varchild) nil)))))))
850 (push var var-list)))
851 (setq gdb-var-list (nreverse var-list)))))
853 (defun gdb-var-update ()
854 (when (not (member 'gdb-var-update gdb-pending-triggers))
856 (list "server interpreter mi \"-var-update *\"\n"
857 'gdb-var-update-handler))
858 (push 'gdb-var-update gdb-pending-triggers)))
860 (defconst gdb-var-update-regexp
861 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
862 type_changed=\".*?\".*?}")
864 (defun gdb-var-update-handler ()
865 (dolist (var gdb-var-list)
866 (setcar (nthcdr 5 var) nil))
867 (goto-char (point-min))
869 (while (re-search-forward gdb-var-update-regexp nil t)
870 (let ((varnum (match-string 1)))
871 (if (string-equal (match-string 2) "false")
872 (let ((var (assoc varnum gdb-var-list)))
873 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
875 (push n gdb-pending-triggers)
878 (concat "server interpreter mi \"" (number-to-string n)
879 "-var-evaluate-expression " varnum "\"\n")
880 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))))))
881 (setq gdb-pending-triggers
882 (delq 'gdb-var-update gdb-pending-triggers)))
884 (defun gdb-var-set-format (format)
885 "Set the output format for a variable displayed in the speedbar."
886 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
890 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
891 (concat "server interpreter mi \"-var-set-format "
892 varnum " " format "\"\n")
893 (concat "-var-set-format " varnum " " format "\n"))
897 (defun gdb-var-delete-1 (varnum)
900 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
901 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
902 (concat "-var-delete " varnum "\n"))
904 (setq gdb-var-list (delq var gdb-var-list))
905 (dolist (varchild gdb-var-list)
906 (if (string-match (concat (car var) "\\.") (car varchild))
907 (setq gdb-var-list (delq varchild gdb-var-list)))))
909 (defun gdb-var-delete ()
910 "Delete watch expression at point from the speedbar."
912 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
914 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
916 (if (string-match "\\." (car var))
917 (message-box "Can only delete a root expression")
918 (gdb-var-delete-1 varnum)))))
920 (defun gdb-var-delete-children (varnum)
921 "Delete children of variable object at point from the speedbar."
924 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
925 (concat "server interpreter mi \"-var-delete -c " varnum "\"\n")
926 (concat "-var-delete -c " varnum "\n")) 'ignore)))
928 (defun gdb-edit-value (text token indent)
929 "Assign a value to a variable displayed in the speedbar."
930 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
931 (varnum (car var)) (value))
932 (setq value (read-string "New value: "))
935 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
936 (concat "server interpreter mi \"-var-assign "
937 varnum " " value "\"\n")
938 (concat "-var-assign " varnum " " value "\n"))
939 `(lambda () (gdb-edit-value-handler ,value))))))
941 (defun gdb-edit-value-handler (value)
942 (goto-char (point-min))
943 (if (re-search-forward gdb-error-regexp nil t)
944 (message-box "Invalid number or expression (%s)" value)))
946 (defcustom gdb-show-changed-values t
947 "If non-nil change the face of out of scope variables and changed values.
948 Out of scope variables are suppressed with `shadow' face.
949 Changed values are highlighted with the face `font-lock-warning-face'."
954 (defcustom gdb-max-children 40
955 "Maximum number of children before expansion requires confirmation."
960 (defcustom gdb-delete-out-of-scope t
961 "If non-nil delete watch expressions automatically when they go out of scope."
966 (defun gdb-speedbar-expand-node (text token indent)
967 "Expand the node the user clicked on.
968 TEXT is the text of the button we clicked on, a + or - item.
969 TOKEN is data related to this node.
970 INDENT is the current indentation depth."
971 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
973 (cond ((string-match "+" text) ;expand this node
974 (let* ((var (assoc token gdb-var-list))
975 (expr (nth 1 var)) (children (nth 2 var)))
976 (if (or (<= (string-to-number children) gdb-max-children)
979 "%s has %s children. Continue? " expr children)))
980 (if (and (eq (buffer-local-value
981 'gud-minor-mode gud-comint-buffer) 'gdba)
982 (string-equal gdb-version "pre-6.4"))
983 (gdb-var-list-children token)
984 (gdb-var-list-children-1 token)))))
985 ((string-match "-" text) ;contract this node
986 (dolist (var gdb-var-list)
987 (if (string-match (concat token "\\.") (car var))
988 (setq gdb-var-list (delq var gdb-var-list))))
989 (gdb-var-delete-children token)
990 (speedbar-change-expand-button-char ?+)
991 (speedbar-delete-subblock indent))
992 (t (error "Ooops... not sure what to do")))
993 (speedbar-center-buffer-smartly))
994 (message-box "GUD session has been killed")))
996 (defun gdb-get-target-string ()
997 (with-current-buffer gud-comint-buffer
1004 ;; Each buffer has a TYPE -- a symbol that identifies the function
1005 ;; of that particular buffer.
1007 ;; The usual gdb interaction buffer is given the type `gdba' and
1008 ;; is constructed specially.
1010 ;; Others are constructed by gdb-get-buffer-create and
1011 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
1013 (defvar gdb-buffer-rules-assoc '())
1015 (defun gdb-get-buffer (key)
1016 "Return the gdb buffer tagged with type KEY.
1017 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1019 (gdb-look-for-tagged-buffer key (buffer-list))))
1021 (defun gdb-get-buffer-create (key)
1022 "Create a new gdb buffer of the type specified by KEY.
1023 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1024 (or (gdb-get-buffer key)
1025 (let* ((rules (assoc key gdb-buffer-rules-assoc))
1026 (name (funcall (gdb-rules-name-maker rules)))
1027 (new (get-buffer-create name)))
1028 (with-current-buffer new
1030 (if (cdr (cdr rules))
1031 (setq trigger (funcall (car (cdr (cdr rules))))))
1032 (setq gdb-buffer-type key)
1033 (set (make-local-variable 'gud-minor-mode)
1034 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1035 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1036 (if trigger (funcall trigger)))
1039 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
1041 (defun gdb-look-for-tagged-buffer (key bufs)
1043 (while (and (not retval) bufs)
1044 (set-buffer (car bufs))
1045 (if (eq gdb-buffer-type key)
1046 (setq retval (car bufs)))
1047 (setq bufs (cdr bufs)))
1051 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1052 ;; at least one and possible more functions. The functions have these
1053 ;; roles in defining a buffer type:
1055 ;; NAME - Return a name for this buffer type.
1057 ;; The remaining function(s) are optional:
1059 ;; MODE - called in a new buffer with no arguments, should establish
1060 ;; the proper mode for the buffer.
1063 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1064 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
1066 (setcdr binding rules)
1067 (push (cons buffer-type rules)
1068 gdb-buffer-rules-assoc))))
1070 ;; GUD buffers are an exception to the rules
1071 (gdb-set-buffer-rules 'gdba 'error)
1073 ;; Partial-output buffer : This accumulates output from a command executed on
1074 ;; behalf of emacs (rather than the user).
1076 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1077 'gdb-partial-output-name)
1079 (defun gdb-partial-output-name ()
1080 (concat " *partial-output-"
1081 (gdb-get-target-string)
1085 (gdb-set-buffer-rules 'gdb-inferior-io
1086 'gdb-inferior-io-name
1087 'gdb-inferior-io-mode)
1089 (defun gdb-inferior-io-name ()
1090 (concat "*input/output of "
1091 (gdb-get-target-string)
1094 (defun gdb-display-separate-io-buffer ()
1095 "Display IO of debugged program in a separate window."
1097 (if gdb-use-separate-io-buffer
1099 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1101 (defconst gdb-frame-parameters
1102 '((height . 14) (width . 80)
1104 (tool-bar-lines . nil)
1105 (menu-bar-lines . nil)
1106 (minibuffer . nil)))
1108 (defun gdb-frame-separate-io-buffer ()
1109 "Display IO of debugged program in a new frame."
1111 (if gdb-use-separate-io-buffer
1112 (let ((special-display-regexps (append special-display-regexps '(".*")))
1113 (special-display-frame-alist gdb-frame-parameters))
1114 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1116 (defvar gdb-inferior-io-mode-map
1117 (let ((map (make-sparse-keymap)))
1118 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1119 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1120 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1121 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1122 (define-key map "\C-d" 'gdb-separate-io-eof)
1125 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1126 "Major mode for gdb inferior-io."
1127 :syntax-table nil :abbrev-table nil
1128 ;; We want to use comint because it has various nifty and familiar
1129 ;; features. We don't need a process, but comint wants one, so create
1131 (make-comint-in-buffer
1132 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1133 (current-buffer) "hexl")
1134 (setq comint-input-sender 'gdb-inferior-io-sender))
1136 (defun gdb-inferior-io-sender (proc string)
1137 ;; PROC is the pseudo-process created to satisfy comint.
1138 (with-current-buffer (process-buffer proc)
1139 (setq proc (get-buffer-process gud-comint-buffer))
1140 (process-send-string proc string)
1141 (process-send-string proc "\n")))
1143 (defun gdb-separate-io-interrupt ()
1144 "Interrupt the program being debugged."
1147 (get-buffer-process gud-comint-buffer) comint-ptyp))
1149 (defun gdb-separate-io-quit ()
1150 "Send quit signal to the program being debugged."
1153 (get-buffer-process gud-comint-buffer) comint-ptyp))
1155 (defun gdb-separate-io-stop ()
1156 "Stop the program being debugged."
1159 (get-buffer-process gud-comint-buffer) comint-ptyp))
1161 (defun gdb-separate-io-eof ()
1162 "Send end-of-file to the program being debugged."
1165 (get-buffer-process gud-comint-buffer)))
1168 ;; gdb communications
1171 ;; INPUT: things sent to gdb
1173 ;; The queues are lists. Each element is either a string (indicating user or
1174 ;; user-like input) or a list of the form:
1176 ;; (INPUT-STRING HANDLER-FN)
1178 ;; The handler function will be called from the partial-output buffer when the
1179 ;; command completes. This is the way to write commands which invoke gdb
1180 ;; commands autonomously.
1182 ;; These lists are consumed tail first.
1185 (defun gdb-send (proc string)
1186 "A comint send filter for gdb.
1187 This filter may simply queue input for a later time."
1189 (with-current-buffer gud-comint-buffer
1190 (let ((inhibit-read-only t))
1191 (remove-text-properties (point-min) (point-max) '(face))))
1194 (let ((item (concat string "\n")))
1195 (if gdb-enable-debug (push (cons 'send item) gdb-debug-log))
1196 (process-send-string proc item)))
1197 (if (string-match "\\\\\\'" string)
1198 (setq gdb-continuation (concat gdb-continuation string "\n"))
1199 (let ((item (concat gdb-continuation string
1200 (if (not comint-input-sender-no-newline) "\n"))))
1201 (gdb-enqueue-input item)
1202 (setq gdb-continuation nil))))))
1204 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1205 ;; is a query, or other non-top-level prompt.
1207 (defun gdb-enqueue-input (item)
1208 (if (not gud-running)
1211 (gdb-send-item item)
1212 (setq gdb-prompting nil))
1213 (push item gdb-input-queue))))
1215 (defun gdb-dequeue-input ()
1216 (let ((queue gdb-input-queue))
1218 (let ((last (car (last queue))))
1219 (unless (nbutlast queue) (setq gdb-input-queue '()))
1222 (defun gdb-send-item (item)
1223 (setq gdb-flush-pending-output nil)
1224 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1225 (setq gdb-current-item item)
1226 (let ((process (get-buffer-process gud-comint-buffer)))
1227 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1230 (setq gdb-output-sink 'user)
1231 (process-send-string process item))
1233 (gdb-clear-partial-output)
1234 (setq gdb-output-sink 'pre-emacs)
1235 (process-send-string process
1237 ;; case: eq gud-minor-mode 'gdbmi
1238 (gdb-clear-partial-output)
1239 (setq gdb-output-sink 'emacs)
1240 (process-send-string process (car item)))))
1243 ;; output -- things gdb prints to emacs
1245 ;; GDB output is a stream interrupted by annotations.
1246 ;; Annotations can be recognized by their beginning
1247 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1249 ;; The tag is a string obeying symbol syntax.
1251 ;; The optional part `<opt>' can be either the empty string
1252 ;; or a space followed by more data relating to the annotation.
1253 ;; For example, the SOURCE annotation is followed by a filename,
1254 ;; line number and various useless goo. This data must not include
1258 (defcustom gud-gdb-command-name "gdb --annotate=3"
1259 "Default command to execute an executable under the GDB debugger."
1264 (defvar gdb-annotation-rules
1265 '(("pre-prompt" gdb-pre-prompt)
1266 ("prompt" gdb-prompt)
1267 ("commands" gdb-subprompt)
1268 ("overload-choice" gdb-subprompt)
1269 ("query" gdb-subprompt)
1270 ;; Need this prompt for GDB 6.1
1271 ("nquery" gdb-subprompt)
1272 ("prompt-for-continue" gdb-subprompt)
1273 ("post-prompt" gdb-post-prompt)
1274 ("source" gdb-source)
1275 ("starting" gdb-starting)
1276 ("exited" gdb-exited)
1277 ("signalled" gdb-signalled)
1278 ("signal" gdb-signal)
1279 ("breakpoint" gdb-stopping)
1280 ("watchpoint" gdb-stopping)
1281 ("frame-begin" gdb-frame-begin)
1282 ("stopped" gdb-stopped)
1283 ("error-begin" gdb-error)
1285 ) "An assoc mapping annotation tags to functions which process them.")
1288 (setq gdb-flush-pending-output t)
1289 (setq gud-running nil)
1290 (gdb-force-mode-line-update
1291 (propertize "stopped"'face font-lock-warning-face))
1292 (setq gdb-output-sink 'user)
1293 (setq gdb-input-queue nil)
1294 (setq gdb-pending-triggers nil)
1295 (setq gdb-prompting t))
1297 (defconst gdb-source-spec-regexp
1298 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1300 ;; Do not use this except as an annotation handler.
1301 (defun gdb-source (args)
1302 (string-match gdb-source-spec-regexp args)
1303 ;; Extract the frame position from the marker.
1304 (setq gud-last-frame
1306 (match-string 1 args)
1307 (string-to-number (match-string 2 args))))
1308 (setq gdb-pc-address (match-string 3 args))
1309 ;; cover for auto-display output which comes *before*
1310 ;; stopped annotation
1311 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1313 (defun gdb-pre-prompt (ignored)
1314 "An annotation handler for `pre-prompt'.
1315 This terminates the collection of output from a previous command if that
1316 happens to be in effect."
1317 (setq gdb-error nil)
1318 (let ((sink gdb-output-sink))
1322 (setq gdb-output-sink 'post-emacs))
1325 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1327 (defun gdb-prompt (ignored)
1328 "An annotation handler for `prompt'.
1329 This sends the next command (if any) to gdb."
1330 (when gdb-first-prompt
1331 (gdb-force-mode-line-update
1332 (propertize "initializing..." 'face font-lock-variable-name-face))
1334 (setq gdb-first-prompt nil))
1335 (let ((sink gdb-output-sink))
1338 ((eq sink 'post-emacs)
1339 (setq gdb-output-sink 'user)
1341 (car (cdr gdb-current-item))))
1342 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1343 (funcall handler))))
1346 (error "Phase error in gdb-prompt (got %s)" sink))))
1347 (let ((input (gdb-dequeue-input)))
1349 (gdb-send-item input)
1351 (setq gdb-prompting t)
1352 (gud-display-frame)))))
1354 (defun gdb-subprompt (ignored)
1355 "An annotation handler for non-top-level prompts."
1356 (setq gdb-prompting t))
1358 (defun gdb-starting (ignored)
1359 "An annotation handler for `starting'.
1360 This says that I/O for the subprocess is now the program being debugged,
1362 (setq gdb-active-process t)
1363 (setq gdb-printing t)
1364 (let ((sink gdb-output-sink))
1368 (setq gud-running t)
1369 (setq gdb-inferior-status "running")
1370 (setq gdb-signalled nil)
1371 (gdb-force-mode-line-update
1372 (propertize gdb-inferior-status 'face font-lock-type-face))
1373 (gdb-remove-text-properties)
1374 (setq gud-old-arrow gud-overlay-arrow-position)
1375 (setq gud-overlay-arrow-position nil)
1376 (setq gdb-overlay-arrow-position nil)
1377 (setq gdb-stack-position nil)
1378 (if gdb-use-separate-io-buffer
1379 (setq gdb-output-sink 'inferior))))
1382 (error "Unexpected `starting' annotation")))))
1384 (defun gdb-signal (ignored)
1385 (setq gdb-inferior-status "signal")
1386 (gdb-force-mode-line-update
1387 (propertize gdb-inferior-status 'face font-lock-warning-face))
1388 (gdb-stopping ignored))
1390 (defun gdb-stopping (ignored)
1391 "An annotation handler for `breakpoint' and other annotations.
1392 They say that I/O for the subprocess is now GDB, not the program
1394 (if gdb-use-separate-io-buffer
1395 (let ((sink gdb-output-sink))
1397 ((eq sink 'inferior)
1398 (setq gdb-output-sink 'user))
1401 (error "Unexpected stopping annotation"))))))
1403 (defun gdb-exited (ignored)
1404 "An annotation handler for `exited' and `signalled'.
1405 They say that I/O for the subprocess is now GDB, not the program
1406 being debugged and that the program is no longer running. This
1407 function is used to change the focus of GUD tooltips to #define
1409 (setq gdb-active-process nil)
1410 (setq gud-overlay-arrow-position nil)
1411 (setq gdb-overlay-arrow-position nil)
1412 (setq gdb-stack-position nil)
1413 (setq gud-old-arrow nil)
1414 (setq gdb-inferior-status "exited")
1415 (gdb-force-mode-line-update
1416 (propertize gdb-inferior-status 'face font-lock-warning-face))
1417 (gdb-stopping ignored))
1419 (defun gdb-signalled (ignored)
1420 (setq gdb-signalled t))
1422 (defun gdb-frame-begin (ignored)
1423 (setq gdb-frame-begin t)
1424 (setq gdb-printing nil)
1425 (let ((sink gdb-output-sink))
1427 ((eq sink 'inferior)
1428 (setq gdb-output-sink 'user))
1430 ((eq sink 'emacs) t)
1433 (error "Unexpected frame-begin annotation (%S)" sink)))))
1435 (defcustom gdb-same-frame focus-follows-mouse
1436 "Non-nil means pop up GUD buffer in same frame."
1441 (defcustom gdb-find-source-frame nil
1442 "Non-nil means try to find a source frame further up stack e.g after signal."
1447 (defun gdb-find-source-frame (arg)
1448 "Toggle trying to find a source frame further up stack.
1449 With prefix argument ARG, look for a source frame further up
1450 stack if ARG is positive, otherwise don't look further up."
1452 (setq gdb-find-source-frame
1454 (not gdb-find-source-frame)
1455 (> (prefix-numeric-value arg) 0)))
1456 (message (format "Looking for source frame %sabled"
1457 (if gdb-find-source-frame "en" "dis"))))
1459 (defun gdb-stopped (ignored)
1460 "An annotation handler for `stopped'.
1461 It is just like `gdb-stopping', except that if we already set the output
1462 sink to `user' in `gdb-stopping', that is fine."
1463 (setq gud-running nil)
1464 (unless (or gud-overlay-arrow-position gud-last-frame)
1465 (if (and gdb-frame-begin gdb-printing)
1466 (setq gud-overlay-arrow-position gud-old-arrow)
1467 ;;Pop up GUD buffer to display current frame when it doesn't have source
1468 ;;information i.e if not compiled with -g as with libc routines generally.
1470 (gdb-display-gdb-buffer)
1471 (gdb-frame-gdb-buffer))
1472 (if gdb-find-source-frame
1473 ;;Try to find source further up stack e.g after signal.
1474 (setq gdb-look-up-stack
1475 (if (gdb-get-buffer 'gdb-stack-buffer)
1478 (gdb-get-buffer-create 'gdb-stack-buffer)
1479 (gdb-invalidate-frames)
1481 (unless (member gdb-inferior-status '("exited" "signal"))
1482 (setq gdb-active-process t) ;Just for attaching case.
1483 (setq gdb-inferior-status "stopped")
1484 (gdb-force-mode-line-update
1485 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1486 (let ((sink gdb-output-sink))
1488 ((eq sink 'inferior)
1489 (setq gdb-output-sink 'user))
1493 (error "Unexpected stopped annotation"))))
1494 (if gdb-signalled (gdb-exited ignored)))
1496 (defun gdb-error (ignored)
1497 (setq gdb-error (not gdb-error)))
1499 (defun gdb-post-prompt (ignored)
1500 "An annotation handler for `post-prompt'.
1501 This begins the collection of output from the current command if that
1502 happens to be appropriate."
1503 ;; Don't add to queue if there outstanding items or gdb-version is not known
1505 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1506 (gdb-get-selected-frame)
1507 (gdb-invalidate-frames)
1508 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1509 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1510 (gdb-invalidate-breakpoints)
1511 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1512 ;; so gdb-pc-address is updated.
1513 ;; (gdb-invalidate-assembler)
1515 (if (string-equal gdb-version "pre-6.4")
1516 (gdb-invalidate-registers)
1517 (gdb-get-changed-registers)
1518 (gdb-invalidate-registers-1))
1520 (gdb-invalidate-memory)
1521 (if (string-equal gdb-version "pre-6.4")
1522 (gdb-invalidate-locals)
1523 (gdb-invalidate-locals-1))
1525 (gdb-invalidate-threads)
1526 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1527 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1528 ;; Only needed/used with speedbar/watch expressions.
1529 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1530 (if (string-equal gdb-version "pre-6.4")
1532 (gdb-var-update-1)))))
1533 (setq gdb-first-post-prompt nil)
1534 (let ((sink gdb-output-sink))
1537 ((eq sink 'pre-emacs)
1538 (setq gdb-output-sink 'emacs))
1541 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1543 (defconst gdb-buffer-list
1544 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1546 (defun gdb-remove-text-properties ()
1547 (dolist (buffertype gdb-buffer-list)
1548 (let ((buffer (gdb-get-buffer buffertype)))
1550 (with-current-buffer buffer
1551 (let ((inhibit-read-only t))
1552 (remove-text-properties
1553 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1555 ;; GUD displays the selected GDB frame. This might might not be the current
1556 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1557 ;; visited breakpoint is, use that window.
1558 (defun gdb-display-source-buffer (buffer)
1559 (let* ((last-window (if gud-last-last-frame
1561 (gud-find-file (car gud-last-last-frame)))))
1562 (source-window (or last-window
1563 (if (and gdb-source-window
1564 (window-live-p gdb-source-window))
1565 gdb-source-window))))
1567 (setq gdb-source-window source-window)
1568 (set-window-buffer source-window buffer))
1571 ;; Derived from gud-gdb-marker-regexp
1572 (defvar gdb-fullname-regexp
1573 (concat "\\(.:?[^" ":" "\n]*\\)" ":" "\\([0-9]*\\)" ":" ".*"))
1575 (defun gud-gdba-marker-filter (string)
1576 "A gud marker filter for gdb. Handle a burst of output from GDB."
1577 (if gdb-flush-pending-output
1579 (when gdb-enable-debug
1580 (push (cons 'recv string) gdb-debug-log)
1581 (if (and gdb-debug-log-max
1582 (> (length gdb-debug-log) gdb-debug-log-max))
1583 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1584 ;; Recall the left over gud-marker-acc from last time.
1585 (setq gud-marker-acc (concat gud-marker-acc string))
1586 ;; Start accumulating output for the GUD buffer.
1589 ;; Process all the complete markers in this chunk.
1590 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1591 (let ((annotation (match-string 1 gud-marker-acc))
1592 (before (substring gud-marker-acc 0 (match-beginning 0)))
1593 (after (substring gud-marker-acc (match-end 0))))
1595 ;; Parse the tag from the annotation, and maybe its arguments.
1596 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1597 (let* ((annotation-type (match-string 1 annotation))
1598 (annotation-arguments (match-string 2 annotation))
1599 (annotation-rule (assoc annotation-type
1600 gdb-annotation-rules))
1601 (fullname (string-match gdb-fullname-regexp annotation-type)))
1603 ;; Stuff prior to the match is just ordinary output.
1604 ;; It is either concatenated to OUTPUT or directed
1607 (gdb-concat-output output
1608 (concat before (if fullname "\n"))))
1610 ;; Take that stuff off the gud-marker-acc.
1611 (setq gud-marker-acc after)
1613 ;; Call the handler for this annotation.
1615 (funcall (car (cdr annotation-rule))
1616 annotation-arguments)
1618 ;; Switch to gud-gdb-marker-filter if appropriate.
1621 ;; Extract the frame position from the marker.
1622 (setq gud-last-frame (cons (match-string 1 annotation)
1624 (match-string 2 annotation))))
1626 (set (make-local-variable 'gud-minor-mode) 'gdb)
1627 (set (make-local-variable 'gud-marker-filter)
1628 'gud-gdb-marker-filter)))
1630 ;; Else the annotation is not recognized. Ignore it silently,
1631 ;; so that GDB can add new annotations without causing
1635 ;; Does the remaining text end in a partial line?
1636 ;; If it does, then keep part of the gud-marker-acc until we get more.
1637 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1640 ;; Everything before the potential marker start can be output.
1642 (gdb-concat-output output
1643 (substring gud-marker-acc 0
1644 (match-beginning 0))))
1646 ;; Everything after, we save, to combine with later input.
1647 (setq gud-marker-acc (substring gud-marker-acc
1648 (match-beginning 0))))
1650 ;; In case we know the gud-marker-acc contains no partial annotations:
1652 (setq output (gdb-concat-output output gud-marker-acc))
1653 (setq gud-marker-acc "")))
1656 (defun gdb-concat-output (so-far new)
1658 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1659 (let ((sink gdb-output-sink))
1661 ((eq sink 'user) (concat so-far new))
1662 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1664 (gdb-append-to-partial-output new)
1666 ((eq sink 'inferior)
1667 (gdb-append-to-inferior-io new)
1671 (error "Bogon output sink %S" sink)))))
1673 (defun gdb-append-to-partial-output (string)
1674 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1675 (goto-char (point-max))
1678 (defun gdb-clear-partial-output ()
1679 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1682 (defun gdb-append-to-inferior-io (string)
1683 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1684 (goto-char (point-max))
1685 (insert-before-markers string))
1686 (if (not (string-equal string ""))
1687 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1689 (defun gdb-clear-inferior-io ()
1690 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1694 ;; One trick is to have a command who's output is always available in a buffer
1695 ;; of it's own, and is always up to date. We build several buffers of this
1698 ;; There are two aspects to this: gdb has to tell us when the output for that
1699 ;; command might have changed, and we have to be able to run the command
1700 ;; behind the user's back.
1702 ;; The output phasing associated with the variable gdb-output-sink
1703 ;; help us to run commands behind the user's back.
1705 ;; Below is the code for specificly managing buffers of output from one
1709 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1710 ;; It adds an input for the command we are tracking. It should be the
1711 ;; annotation rule binding of whatever gdb sends to tell us this command
1712 ;; might have changed it's output.
1714 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1715 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1716 ;; input in the input queue (see comment about ``gdb communications'' above).
1718 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1720 `(defun ,name (&optional ignored)
1721 (if (and ,demand-predicate
1723 gdb-pending-triggers)))
1726 (list ,gdb-command ',output-handler))
1727 (push ',name gdb-pending-triggers)))))
1729 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1731 (setq gdb-pending-triggers
1733 gdb-pending-triggers))
1734 (let ((buf (gdb-get-buffer ',buf-key)))
1736 (with-current-buffer buf
1737 (let* ((window (get-buffer-window buf 0))
1738 (start (window-start window))
1739 (p (window-point window))
1740 (buffer-read-only nil))
1742 (insert-buffer-substring (gdb-get-buffer-create
1743 'gdb-partial-output-buffer))
1744 (set-window-start window start)
1745 (set-window-point window p)))))
1746 ;; put customisation here
1749 (defmacro def-gdb-auto-updated-buffer (buffer-key
1750 trigger-name gdb-command
1751 output-handler-name custom-defun)
1753 (def-gdb-auto-update-trigger ,trigger-name
1754 ;; The demand predicate:
1755 (gdb-get-buffer ',buffer-key)
1757 ,output-handler-name)
1758 (def-gdb-auto-update-handler ,output-handler-name
1759 ,trigger-name ,buffer-key ,custom-defun)))
1763 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1765 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1766 'gdb-breakpoints-buffer-name
1767 'gdb-breakpoints-mode)
1769 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1770 ;; This defines the auto update rule for buffers of type
1771 ;; `gdb-breakpoints-buffer'.
1773 ;; It defines a function to serve as the annotation handler that
1774 ;; handles the `foo-invalidated' message. That function is called:
1775 gdb-invalidate-breakpoints
1777 ;; To update the buffer, this command is sent to gdb.
1778 "server info breakpoints\n"
1780 ;; This also defines a function to be the handler for the output
1781 ;; from the command above. That function will copy the output into
1782 ;; the appropriately typed buffer. That function will be called:
1783 gdb-info-breakpoints-handler
1784 ;; buffer specific functions
1785 gdb-info-breakpoints-custom)
1787 (defconst breakpoint-xpm-data
1789 static char *magick[] = {
1790 /* columns rows colors chars-per-pixel */
1806 "XPM data used for breakpoint icon.")
1808 (defconst breakpoint-enabled-pbm-data
1811 0 0 0 0 1 1 1 1 0 0 0 0
1812 0 0 0 1 1 1 1 1 1 0 0 0
1813 0 0 1 1 1 1 1 1 1 1 0 0
1814 0 1 1 1 1 1 1 1 1 1 1 0
1815 0 1 1 1 1 1 1 1 1 1 1 0
1816 0 1 1 1 1 1 1 1 1 1 1 0
1817 0 1 1 1 1 1 1 1 1 1 1 0
1818 0 0 1 1 1 1 1 1 1 1 0 0
1819 0 0 0 1 1 1 1 1 1 0 0 0
1820 0 0 0 0 1 1 1 1 0 0 0 0"
1821 "PBM data used for enabled breakpoint icon.")
1823 (defconst breakpoint-disabled-pbm-data
1835 0 0 0 1 0 1 0 1 0 0"
1836 "PBM data used for disabled breakpoint icon.")
1838 (defvar breakpoint-enabled-icon nil
1839 "Icon for enabled breakpoint in display margin.")
1841 (defvar breakpoint-disabled-icon nil
1842 "Icon for disabled breakpoint in display margin.")
1844 (and (display-images-p)
1845 ;; Bitmap for breakpoint in fringe
1846 (define-fringe-bitmap 'breakpoint
1847 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1848 ;; Bitmap for gud-overlay-arrow in fringe
1849 (define-fringe-bitmap 'hollow-right-triangle
1850 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1852 (defface breakpoint-enabled
1856 "Face for enabled breakpoint icon in fringe."
1859 (defface breakpoint-disabled
1860 '((((class color) (min-colors 88)) :foreground "grey70")
1861 ;; Ensure that on low-color displays that we end up something visible.
1862 (((class color) (min-colors 8) (background light))
1863 :foreground "black")
1864 (((class color) (min-colors 8) (background dark))
1865 :foreground "white")
1866 (((type tty) (class mono))
1868 (t :background "gray"))
1869 "Face for disabled breakpoint icon in fringe."
1872 (defconst gdb-breakpoint-regexp
1873 "\\(?:\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\|\\([0-9]+\\.[0-9]+\\)\\)\\s-+\\(.\\)\\s-+")
1875 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1876 (defun gdb-info-breakpoints-custom ()
1877 (let ((flag) (bptno))
1878 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1879 (dolist (buffer (buffer-list))
1880 (with-current-buffer buffer
1881 (if (and (memq gud-minor-mode '(gdba gdbmi))
1882 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1883 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1884 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1886 (let ((buffer-read-only nil))
1887 (goto-char (point-min))
1888 (while (< (point) (- (point-max) 1))
1890 (if (looking-at gdb-breakpoint-regexp)
1892 (setq bptno (or (match-string 1) (match-string 2)))
1893 (setq flag (char-after (match-beginning 3)))
1894 (if (match-string 1)
1895 (setq gdb-parent-bptno-enabled (eq flag ?y)))
1896 (add-text-properties
1897 (match-beginning 3) (match-end 3)
1899 '(face font-lock-warning-face)
1900 '(face font-lock-type-face)))
1902 (el (line-end-position)))
1903 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1905 (add-text-properties
1906 (match-beginning 1) (match-end 1)
1907 '(face font-lock-function-name-face))
1908 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1909 (let ((line (match-string 2))
1910 (file (match-string 1)))
1911 (add-text-properties bl el
1912 '(mouse-face highlight
1913 help-echo "mouse-2, RET: visit breakpoint"))
1914 (unless (file-exists-p file)
1915 (setq file (cdr (assoc bptno gdb-location-alist))))
1917 (not (string-equal file "File not found")))
1918 (with-current-buffer
1919 (find-file-noselect file 'nowarn)
1920 (set (make-local-variable 'gud-minor-mode)
1922 (set (make-local-variable 'tool-bar-map)
1924 ;; Only want one breakpoint icon at each
1927 (goto-line (string-to-number line))
1928 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1931 (concat gdb-server-prefix "list "
1932 (match-string-no-properties 1) ":1\n")
1935 (list (concat gdb-server-prefix "info source\n")
1936 `(lambda () (gdb-get-location
1937 ,bptno ,line ,flag)))))))
1938 (if (re-search-forward
1939 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1941 (add-text-properties
1942 (match-beginning 1) (match-end 1)
1943 '(face font-lock-function-name-face))
1945 (re-search-backward "\\s-\\(\\S-*\\)"
1947 (add-text-properties
1948 (match-beginning 1) (match-end 1)
1949 '(face font-lock-variable-name-face)))))))
1951 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1953 (defun gdb-mouse-set-clear-breakpoint (event)
1954 "Set/clear breakpoint in left fringe/margin with mouse click."
1956 (mouse-minibuffer-check event)
1957 (let ((posn (event-end event)))
1958 (if (numberp (posn-point posn))
1959 (with-selected-window (posn-window posn)
1961 (goto-char (posn-point posn))
1962 (if (or (posn-object posn)
1963 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1966 (gud-break nil)))))))
1968 (defun gdb-mouse-toggle-breakpoint-margin (event)
1969 "Enable/disable breakpoint in left margin with mouse click."
1971 (mouse-minibuffer-check event)
1972 (let ((posn (event-end event)))
1973 (if (numberp (posn-point posn))
1974 (with-selected-window (posn-window posn)
1976 (goto-char (posn-point posn))
1977 (if (posn-object posn)
1978 (let* ((bptno (get-text-property
1979 0 'gdb-bptno (car (posn-string posn)))))
1980 (string-match "\\([0-9+]\\)*" bptno)
1983 (concat gdb-server-prefix
1984 (if (get-text-property
1985 0 'gdb-enabled (car (posn-string posn)))
1988 (match-string 1 bptno) "\n")
1991 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1992 "Enable/disable breakpoint in left fringe with mouse click."
1994 (mouse-minibuffer-check event)
1995 (let* ((posn (event-end event))
1996 (pos (posn-point posn))
1999 (with-selected-window (posn-window posn)
2001 (set-buffer (window-buffer (selected-window)))
2003 (dolist (overlay (overlays-in pos pos))
2004 (when (overlay-get overlay 'put-break)
2005 (setq obj (overlay-get overlay 'before-string))))
2007 (let* ((bptno (get-text-property 0 'gdb-bptno obj)))
2008 (string-match "\\([0-9+]\\)*" bptno)
2011 (concat gdb-server-prefix
2012 (if (get-text-property 0 'gdb-enabled obj)
2015 (match-string 1 bptno) "\n")
2018 (defun gdb-breakpoints-buffer-name ()
2019 (with-current-buffer gud-comint-buffer
2020 (concat "*breakpoints of " (gdb-get-target-string) "*")))
2022 (defun gdb-display-breakpoints-buffer ()
2023 "Display status of user-settable breakpoints."
2026 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
2028 (defun gdb-frame-breakpoints-buffer ()
2029 "Display status of user-settable breakpoints in a new frame."
2031 (let ((special-display-regexps (append special-display-regexps '(".*")))
2032 (special-display-frame-alist gdb-frame-parameters))
2033 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
2035 (defvar gdb-breakpoints-mode-map
2036 (let ((map (make-sparse-keymap))
2037 (menu (make-sparse-keymap "Breakpoints")))
2038 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2039 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2040 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2041 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2042 (suppress-keymap map)
2043 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2044 (define-key map " " 'gdb-toggle-breakpoint)
2045 (define-key map "D" 'gdb-delete-breakpoint)
2046 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2047 (define-key map "q" 'gdb-delete-frame-or-window)
2048 (define-key map "\r" 'gdb-goto-breakpoint)
2049 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2050 (define-key map [follow-link] 'mouse-face)
2053 (defun gdb-delete-frame-or-window ()
2054 "Delete frame if there is only one window. Otherwise delete the window."
2056 (if (one-window-p) (delete-frame)
2059 (defun gdb-breakpoints-mode ()
2060 "Major mode for gdb breakpoints.
2062 \\{gdb-breakpoints-mode-map}"
2063 (kill-all-local-variables)
2064 (setq major-mode 'gdb-breakpoints-mode)
2065 (setq mode-name "Breakpoints")
2066 (use-local-map gdb-breakpoints-mode-map)
2067 (setq buffer-read-only t)
2068 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2069 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2070 'gdb-invalidate-breakpoints
2071 'gdbmi-invalidate-breakpoints))
2073 (defun gdb-toggle-breakpoint ()
2074 "Enable/disable breakpoint at current line."
2077 (beginning-of-line 1)
2078 (if (looking-at gdb-breakpoint-regexp)
2081 (concat gdb-server-prefix
2082 (if (eq ?y (char-after (match-beginning 3)))
2085 (or (match-string 1) (match-string 2)) "\n") 'ignore))
2086 (error "Not recognized as break/watchpoint line"))))
2088 (defun gdb-delete-breakpoint ()
2089 "Delete the breakpoint at current line."
2092 (beginning-of-line 1)
2093 (if (looking-at gdb-breakpoint-regexp)
2094 (if (match-string 1)
2097 (concat gdb-server-prefix "delete " (match-string 1) "\n")
2099 (message-box "This breakpoint cannot be deleted on its own."))
2100 (error "Not recognized as break/watchpoint line"))))
2102 (defun gdb-goto-breakpoint (&optional event)
2103 "Display the breakpoint location specified at current line."
2104 (interactive (list last-input-event))
2105 (if event (posn-set-point (event-end event)))
2107 (beginning-of-line 1)
2108 (if (looking-at "\\([0-9]+\\.?[0-9]*\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
2109 (let ((bptno (match-string 1))
2110 (file (match-string 2))
2111 (line (match-string 3)))
2112 (save-selected-window
2113 (let* ((buffer (find-file-noselect
2114 (if (file-exists-p file) file
2115 (cdr (assoc bptno gdb-location-alist)))))
2116 (window (or (gdb-display-source-buffer buffer)
2117 (display-buffer buffer))))
2118 (setq gdb-source-window window)
2119 (with-current-buffer buffer
2120 (goto-line (string-to-number line))
2121 (set-window-point window (point))))))
2122 (error "No location specified."))))
2125 ;; Frames buffer. This displays a perpetually correct bactracktrace
2126 ;; (from the command `where').
2128 ;; Alas, if your stack is deep, it is costly.
2130 (defcustom gdb-max-frames 40
2131 "Maximum number of frames displayed in call stack."
2136 (gdb-set-buffer-rules 'gdb-stack-buffer
2137 'gdb-stack-buffer-name
2140 (def-gdb-auto-updated-buffer gdb-stack-buffer
2141 gdb-invalidate-frames
2142 (concat "server info stack " (number-to-string gdb-max-frames) "\n")
2143 gdb-info-stack-handler
2144 gdb-info-stack-custom)
2146 (defun gdb-info-stack-custom ()
2147 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2150 (unless (eq gdb-look-up-stack 'delete)
2151 (let ((buffer-read-only nil)
2153 (goto-char (point-min))
2154 (while (< (point) (point-max))
2155 (setq bl (line-beginning-position)
2156 el (line-end-position))
2157 (when (looking-at "#")
2158 (add-text-properties bl el
2159 '(mouse-face highlight
2160 help-echo "mouse-2, RET: Select frame")))
2162 (when (looking-at "^#\\([0-9]+\\)")
2163 (when (string-equal (match-string 1) gdb-frame-number)
2164 (if (> (car (window-fringes)) 0)
2166 (or gdb-stack-position
2167 (setq gdb-stack-position (make-marker)))
2168 (set-marker gdb-stack-position (point))
2169 (setq move-to gdb-stack-position))
2170 (put-text-property bl (+ bl 4)
2171 'face '(:inverse-video t))
2173 (when (re-search-forward
2175 (if (string-equal (match-string 1) "0") "" " in ")
2176 "\\([^ ]+\\) (") el t)
2177 (put-text-property (match-beginning 1) (match-end 1)
2178 'face font-lock-function-name-face)
2179 (setq bl (match-end 0))
2180 (while (re-search-forward "<\\([^>]+\\)>" el t)
2181 (put-text-property (match-beginning 1) (match-end 1)
2182 'face font-lock-function-name-face))
2184 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2185 (put-text-property (match-beginning 1) (match-end 1)
2186 'face font-lock-variable-name-face))))
2189 (when (looking-at "(More stack frames follow...)")
2190 (add-text-properties (match-beginning 0) (match-end 0)
2191 '(mouse-face highlight
2194 "mouse-2, RET: customize gdb-max-frames to see more frames")))))
2195 (when gdb-look-up-stack
2196 (goto-char (point-min))
2197 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2198 (let ((start (line-beginning-position))
2199 (file (match-string 1))
2200 (line (match-string 2)))
2201 (re-search-backward "^#*\\([0-9]+\\)" start t)
2203 (list (concat gdb-server-prefix "frame "
2204 (match-string 1) "\n") 'gdb-set-hollow))
2206 (list (concat gdb-server-prefix "frame 0\n") 'ignore))))))
2208 (let ((window (get-buffer-window (current-buffer) 0)))
2210 (with-selected-window window
2212 (unless (pos-visible-in-window-p)
2213 (recenter '(center)))))))))
2214 (if (eq gdb-look-up-stack 'delete)
2215 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2216 (setq gdb-look-up-stack nil))
2218 (defun gdb-set-hollow ()
2219 (if gud-last-last-frame
2220 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2221 (setq fringe-indicator-alist
2222 '((overlay-arrow . hollow-right-triangle))))))
2224 (defun gdb-stack-buffer-name ()
2225 (with-current-buffer gud-comint-buffer
2226 (concat "*stack frames of " (gdb-get-target-string) "*")))
2228 (defun gdb-display-stack-buffer ()
2229 "Display backtrace of current stack."
2232 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2234 (defun gdb-frame-stack-buffer ()
2235 "Display backtrace of current stack in a new frame."
2237 (let ((special-display-regexps (append special-display-regexps '(".*")))
2238 (special-display-frame-alist gdb-frame-parameters))
2239 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2241 (defvar gdb-frames-mode-map
2242 (let ((map (make-sparse-keymap)))
2243 (suppress-keymap map)
2244 (define-key map "q" 'kill-this-buffer)
2245 (define-key map "\r" 'gdb-frames-select)
2246 (define-key map [mouse-2] 'gdb-frames-select)
2247 (define-key map [follow-link] 'mouse-face)
2250 (defun gdb-frames-mode ()
2251 "Major mode for gdb call stack.
2253 \\{gdb-frames-mode-map}"
2254 (kill-all-local-variables)
2255 (setq major-mode 'gdb-frames-mode)
2256 (setq mode-name "Frames")
2257 (setq gdb-stack-position nil)
2258 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2259 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2260 (setq buffer-read-only t)
2261 (use-local-map gdb-frames-mode-map)
2262 (run-mode-hooks 'gdb-frames-mode-hook)
2263 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2264 'gdb-invalidate-frames
2265 'gdbmi-invalidate-frames))
2267 (defun gdb-get-frame-number ()
2270 (let* ((start (line-beginning-position))
2271 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2272 (n (or (and pos (match-string 1)) "0")))
2275 (defun gdb-frames-select (&optional event)
2276 "Select the frame and display the relevant source."
2277 (interactive (list last-input-event))
2278 (if event (posn-set-point (event-end event)))
2279 (if (get-text-property (point) 'gdb-max-frames)
2281 (message-box "After setting gdb-max-frames, you need to enter\n\
2282 another GDB command e.g pwd, to see new frames")
2283 (customize-variable-other-window 'gdb-max-frames))
2285 (list (concat gdb-server-prefix "frame "
2286 (gdb-get-frame-number) "\n") 'ignore))))
2289 ;; Threads buffer. This displays a selectable thread list.
2291 (gdb-set-buffer-rules 'gdb-threads-buffer
2292 'gdb-threads-buffer-name
2295 (def-gdb-auto-updated-buffer gdb-threads-buffer
2296 gdb-invalidate-threads
2297 (concat gdb-server-prefix "info threads\n")
2298 gdb-info-threads-handler
2299 gdb-info-threads-custom)
2301 (defun gdb-info-threads-custom ()
2302 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2303 (let ((buffer-read-only nil))
2305 (goto-char (point-min))
2306 (while (< (point) (point-max))
2307 (unless (looking-at "No ")
2308 (add-text-properties (line-beginning-position) (line-end-position)
2309 '(mouse-face highlight
2310 help-echo "mouse-2, RET: select thread")))
2311 (forward-line 1))))))
2313 (defun gdb-threads-buffer-name ()
2314 (with-current-buffer gud-comint-buffer
2315 (concat "*threads of " (gdb-get-target-string) "*")))
2317 (defun gdb-display-threads-buffer ()
2318 "Display IDs of currently known threads."
2321 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2323 (defun gdb-frame-threads-buffer ()
2324 "Display IDs of currently known threads in a new frame."
2326 (let ((special-display-regexps (append special-display-regexps '(".*")))
2327 (special-display-frame-alist gdb-frame-parameters))
2328 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2330 (defvar gdb-threads-mode-map
2331 (let ((map (make-sparse-keymap)))
2332 (suppress-keymap map)
2333 (define-key map "q" 'kill-this-buffer)
2334 (define-key map "\r" 'gdb-threads-select)
2335 (define-key map [mouse-2] 'gdb-threads-select)
2336 (define-key map [follow-link] 'mouse-face)
2339 (defvar gdb-threads-font-lock-keywords
2340 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2341 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2342 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2343 "Font lock keywords used in `gdb-threads-mode'.")
2345 (defun gdb-threads-mode ()
2346 "Major mode for gdb threads.
2348 \\{gdb-threads-mode-map}"
2349 (kill-all-local-variables)
2350 (setq major-mode 'gdb-threads-mode)
2351 (setq mode-name "Threads")
2352 (setq buffer-read-only t)
2353 (use-local-map gdb-threads-mode-map)
2354 (set (make-local-variable 'font-lock-defaults)
2355 '(gdb-threads-font-lock-keywords))
2356 (run-mode-hooks 'gdb-threads-mode-hook)
2357 'gdb-invalidate-threads)
2359 (defun gdb-get-thread-number ()
2361 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2362 (match-string-no-properties 1)))
2364 (defun gdb-threads-select (&optional event)
2365 "Select the thread and display the relevant source."
2366 (interactive (list last-input-event))
2367 (if event (posn-set-point (event-end event)))
2369 (list (concat gdb-server-prefix "thread "
2370 (gdb-get-thread-number) "\n") 'ignore))
2371 (gud-display-frame))
2374 ;; Registers buffer.
2376 (defcustom gdb-all-registers nil
2377 "Non-nil means include floating-point registers."
2382 (gdb-set-buffer-rules 'gdb-registers-buffer
2383 'gdb-registers-buffer-name
2384 'gdb-registers-mode)
2386 (def-gdb-auto-updated-buffer gdb-registers-buffer
2387 gdb-invalidate-registers
2389 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2390 gdb-info-registers-handler
2391 gdb-info-registers-custom)
2393 (defun gdb-info-registers-custom ()
2394 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2396 (let ((buffer-read-only nil)
2398 (goto-char (point-min))
2399 (while (< (point) (point-max))
2400 (setq start (line-beginning-position))
2401 (setq end (line-end-position))
2402 (when (looking-at "^[^ ]+")
2403 (unless (string-equal (match-string 0) "The")
2404 (put-text-property start (match-end 0)
2405 'face font-lock-variable-name-face)
2406 (add-text-properties start end
2407 '(help-echo "mouse-2: edit value"
2408 mouse-face highlight))))
2409 (forward-line 1))))))
2411 (defun gdb-edit-register-value (&optional event)
2412 (interactive (list last-input-event))
2414 (if event (posn-set-point (event-end event)))
2416 (let* ((register (current-word))
2417 (value (read-string (format "New value (%s): " register))))
2419 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2422 (defvar gdb-registers-mode-map
2423 (let ((map (make-sparse-keymap)))
2424 (suppress-keymap map)
2425 (define-key map "\r" 'gdb-edit-register-value)
2426 (define-key map [mouse-2] 'gdb-edit-register-value)
2427 (define-key map " " 'gdb-all-registers)
2428 (define-key map "q" 'kill-this-buffer)
2431 (defun gdb-registers-mode ()
2432 "Major mode for gdb registers.
2434 \\{gdb-registers-mode-map}"
2435 (kill-all-local-variables)
2436 (setq major-mode 'gdb-registers-mode)
2437 (setq mode-name "Registers")
2438 (setq buffer-read-only t)
2439 (use-local-map gdb-registers-mode-map)
2440 (run-mode-hooks 'gdb-registers-mode-hook)
2441 (if (string-equal gdb-version "pre-6.4")
2443 (if gdb-all-registers (setq mode-name "Registers:All"))
2444 'gdb-invalidate-registers)
2445 'gdb-invalidate-registers-1))
2447 (defun gdb-registers-buffer-name ()
2448 (with-current-buffer gud-comint-buffer
2449 (concat "*registers of " (gdb-get-target-string) "*")))
2451 (defun gdb-display-registers-buffer ()
2452 "Display integer register contents."
2455 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2457 (defun gdb-frame-registers-buffer ()
2458 "Display integer register contents in a new frame."
2460 (let ((special-display-regexps (append special-display-regexps '(".*")))
2461 (special-display-frame-alist gdb-frame-parameters))
2462 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2464 (defun gdb-all-registers ()
2465 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2467 (when (string-equal gdb-version "pre-6.4")
2468 (if gdb-all-registers
2470 (setq gdb-all-registers nil)
2471 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2472 (setq mode-name "Registers")))
2473 (setq gdb-all-registers t)
2474 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2475 (setq mode-name "Registers:All")))
2476 (message (format "Display of floating-point registers %sabled"
2477 (if gdb-all-registers "en" "dis")))
2478 (gdb-invalidate-registers)))
2483 (defcustom gdb-memory-repeat-count 32
2484 "Number of data items in memory window."
2489 (defcustom gdb-memory-format "x"
2490 "Display format of data items in memory window."
2491 :type '(choice (const :tag "Hexadecimal" "x")
2492 (const :tag "Signed decimal" "d")
2493 (const :tag "Unsigned decimal" "u")
2494 (const :tag "Octal" "o")
2495 (const :tag "Binary" "t"))
2499 (defcustom gdb-memory-unit "w"
2500 "Unit size of data items in memory window."
2501 :type '(choice (const :tag "Byte" "b")
2502 (const :tag "Halfword" "h")
2503 (const :tag "Word" "w")
2504 (const :tag "Giant word" "g"))
2508 (gdb-set-buffer-rules 'gdb-memory-buffer
2509 'gdb-memory-buffer-name
2512 (def-gdb-auto-updated-buffer gdb-memory-buffer
2513 gdb-invalidate-memory
2514 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2515 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2516 gdb-read-memory-handler
2517 gdb-read-memory-custom)
2519 (defun gdb-read-memory-custom ()
2521 (goto-char (point-min))
2522 (if (looking-at "0x[[:xdigit:]]+")
2523 (setq gdb-memory-address (match-string 0)))))
2525 (defvar gdb-memory-mode-map
2526 (let ((map (make-sparse-keymap)))
2527 (suppress-keymap map)
2528 (define-key map "q" 'kill-this-buffer)
2531 (defun gdb-memory-set-address (event)
2532 "Set the start memory address."
2534 (save-selected-window
2535 (select-window (posn-window (event-start event)))
2536 (let ((arg (read-from-minibuffer "Memory address: ")))
2537 (setq gdb-memory-address arg))
2538 (gdb-invalidate-memory)))
2540 (defun gdb-memory-set-repeat-count (event)
2541 "Set the number of data items in memory window."
2543 (save-selected-window
2544 (select-window (posn-window (event-start event)))
2545 (let* ((arg (read-from-minibuffer "Repeat count: "))
2546 (count (string-to-number arg)))
2548 (error "Positive numbers only")
2549 (customize-set-variable 'gdb-memory-repeat-count count)
2550 (gdb-invalidate-memory)))))
2552 (defun gdb-memory-format-binary ()
2553 "Set the display format to binary."
2555 (customize-set-variable 'gdb-memory-format "t")
2556 (gdb-invalidate-memory))
2558 (defun gdb-memory-format-octal ()
2559 "Set the display format to octal."
2561 (customize-set-variable 'gdb-memory-format "o")
2562 (gdb-invalidate-memory))
2564 (defun gdb-memory-format-unsigned ()
2565 "Set the display format to unsigned decimal."
2567 (customize-set-variable 'gdb-memory-format "u")
2568 (gdb-invalidate-memory))
2570 (defun gdb-memory-format-signed ()
2571 "Set the display format to decimal."
2573 (customize-set-variable 'gdb-memory-format "d")
2574 (gdb-invalidate-memory))
2576 (defun gdb-memory-format-hexadecimal ()
2577 "Set the display format to hexadecimal."
2579 (customize-set-variable 'gdb-memory-format "x")
2580 (gdb-invalidate-memory))
2582 (defvar gdb-memory-format-map
2583 (let ((map (make-sparse-keymap)))
2584 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2586 "Keymap to select format in the header line.")
2588 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2589 "Menu of display formats in the header line.")
2591 (define-key gdb-memory-format-menu [binary]
2592 '(menu-item "Binary" gdb-memory-format-binary
2593 :button (:radio . (equal gdb-memory-format "t"))))
2594 (define-key gdb-memory-format-menu [octal]
2595 '(menu-item "Octal" gdb-memory-format-octal
2596 :button (:radio . (equal gdb-memory-format "o"))))
2597 (define-key gdb-memory-format-menu [unsigned]
2598 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2599 :button (:radio . (equal gdb-memory-format "u"))))
2600 (define-key gdb-memory-format-menu [signed]
2601 '(menu-item "Signed Decimal" gdb-memory-format-signed
2602 :button (:radio . (equal gdb-memory-format "d"))))
2603 (define-key gdb-memory-format-menu [hexadecimal]
2604 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2605 :button (:radio . (equal gdb-memory-format "x"))))
2607 (defun gdb-memory-format-menu (event)
2609 (x-popup-menu event gdb-memory-format-menu))
2611 (defun gdb-memory-format-menu-1 (event)
2613 (save-selected-window
2614 (select-window (posn-window (event-start event)))
2615 (let* ((selection (gdb-memory-format-menu event))
2616 (binding (and selection (lookup-key gdb-memory-format-menu
2617 (vector (car selection))))))
2618 (if binding (call-interactively binding)))))
2620 (defun gdb-memory-unit-giant ()
2621 "Set the unit size to giant words (eight bytes)."
2623 (customize-set-variable 'gdb-memory-unit "g")
2624 (gdb-invalidate-memory))
2626 (defun gdb-memory-unit-word ()
2627 "Set the unit size to words (four bytes)."
2629 (customize-set-variable 'gdb-memory-unit "w")
2630 (gdb-invalidate-memory))
2632 (defun gdb-memory-unit-halfword ()
2633 "Set the unit size to halfwords (two bytes)."
2635 (customize-set-variable 'gdb-memory-unit "h")
2636 (gdb-invalidate-memory))
2638 (defun gdb-memory-unit-byte ()
2639 "Set the unit size to bytes."
2641 (customize-set-variable 'gdb-memory-unit "b")
2642 (gdb-invalidate-memory))
2644 (defvar gdb-memory-unit-map
2645 (let ((map (make-sparse-keymap)))
2646 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2648 "Keymap to select units in the header line.")
2650 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2651 "Menu of units in the header line.")
2653 (define-key gdb-memory-unit-menu [giantwords]
2654 '(menu-item "Giant words" gdb-memory-unit-giant
2655 :button (:radio . (equal gdb-memory-unit "g"))))
2656 (define-key gdb-memory-unit-menu [words]
2657 '(menu-item "Words" gdb-memory-unit-word
2658 :button (:radio . (equal gdb-memory-unit "w"))))
2659 (define-key gdb-memory-unit-menu [halfwords]
2660 '(menu-item "Halfwords" gdb-memory-unit-halfword
2661 :button (:radio . (equal gdb-memory-unit "h"))))
2662 (define-key gdb-memory-unit-menu [bytes]
2663 '(menu-item "Bytes" gdb-memory-unit-byte
2664 :button (:radio . (equal gdb-memory-unit "b"))))
2666 (defun gdb-memory-unit-menu (event)
2668 (x-popup-menu event gdb-memory-unit-menu))
2670 (defun gdb-memory-unit-menu-1 (event)
2672 (save-selected-window
2673 (select-window (posn-window (event-start event)))
2674 (let* ((selection (gdb-memory-unit-menu event))
2675 (binding (and selection (lookup-key gdb-memory-unit-menu
2676 (vector (car selection))))))
2677 (if binding (call-interactively binding)))))
2679 ;;from make-mode-line-mouse-map
2680 (defun gdb-make-header-line-mouse-map (mouse function) "\
2681 Return a keymap with single entry for mouse key MOUSE on the header line.
2682 MOUSE is defined to run function FUNCTION with no args in the buffer
2683 corresponding to the mode line clicked."
2684 (let ((map (make-sparse-keymap)))
2685 (define-key map (vector 'header-line mouse) function)
2686 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2689 (defvar gdb-memory-font-lock-keywords
2690 '(;; <__function.name+n>
2691 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2693 "Font lock keywords used in `gdb-memory-mode'.")
2695 (defun gdb-memory-mode ()
2696 "Major mode for examining memory.
2698 \\{gdb-memory-mode-map}"
2699 (kill-all-local-variables)
2700 (setq major-mode 'gdb-memory-mode)
2701 (setq mode-name "Memory")
2702 (setq buffer-read-only t)
2703 (use-local-map gdb-memory-mode-map)
2704 (setq header-line-format
2710 'face font-lock-warning-face
2711 'help-echo "mouse-1: decrement address"
2712 'mouse-face 'mode-line-highlight
2714 (gdb-make-header-line-mouse-map
2716 (lambda () (interactive)
2717 (let ((gdb-memory-address
2718 ;; Let GDB do the arithmetic.
2720 gdb-memory-address " - "
2722 (* gdb-memory-repeat-count
2723 (cond ((string= gdb-memory-unit "b") 1)
2724 ((string= gdb-memory-unit "h") 2)
2725 ((string= gdb-memory-unit "w") 4)
2726 ((string= gdb-memory-unit "g") 8)))))))
2727 (gdb-invalidate-memory)))))
2730 'face font-lock-warning-face
2731 'help-echo "mouse-1: increment address"
2732 'mouse-face 'mode-line-highlight
2733 'local-map (gdb-make-header-line-mouse-map
2735 (lambda () (interactive)
2736 (let ((gdb-memory-address nil))
2737 (gdb-invalidate-memory)))))
2739 (propertize gdb-memory-address
2740 'face font-lock-warning-face
2741 'help-echo "mouse-1: set memory address"
2742 'mouse-face 'mode-line-highlight
2743 'local-map (gdb-make-header-line-mouse-map
2745 #'gdb-memory-set-address))
2747 (propertize (number-to-string gdb-memory-repeat-count)
2748 'face font-lock-warning-face
2749 'help-echo "mouse-1: set repeat count"
2750 'mouse-face 'mode-line-highlight
2751 'local-map (gdb-make-header-line-mouse-map
2753 #'gdb-memory-set-repeat-count))
2755 (propertize gdb-memory-format
2756 'face font-lock-warning-face
2757 'help-echo "mouse-3: select display format"
2758 'mouse-face 'mode-line-highlight
2759 'local-map gdb-memory-format-map)
2761 (propertize gdb-memory-unit
2762 'face font-lock-warning-face
2763 'help-echo "mouse-3: select unit size"
2764 'mouse-face 'mode-line-highlight
2765 'local-map gdb-memory-unit-map))))
2766 (set (make-local-variable 'font-lock-defaults)
2767 '(gdb-memory-font-lock-keywords))
2768 (run-mode-hooks 'gdb-memory-mode-hook)
2769 'gdb-invalidate-memory)
2771 (defun gdb-memory-buffer-name ()
2772 (with-current-buffer gud-comint-buffer
2773 (concat "*memory of " (gdb-get-target-string) "*")))
2775 (defun gdb-display-memory-buffer ()
2776 "Display memory contents."
2779 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2781 (defun gdb-frame-memory-buffer ()
2782 "Display memory contents in a new frame."
2784 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2785 (special-display-frame-alist
2786 (cons '(left-fringe . 0)
2787 (cons '(right-fringe . 0)
2788 (cons '(width . 83) gdb-frame-parameters)))))
2789 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2794 (gdb-set-buffer-rules 'gdb-locals-buffer
2795 'gdb-locals-buffer-name
2798 (def-gdb-auto-update-trigger gdb-invalidate-locals
2799 (gdb-get-buffer 'gdb-locals-buffer)
2800 "server info locals\n"
2801 gdb-info-locals-handler)
2803 (defvar gdb-locals-watch-map
2804 (let ((map (make-sparse-keymap)))
2805 (suppress-keymap map)
2806 (define-key map "\r" (lambda () (interactive)
2809 (define-key map [mouse-2] (lambda (event) (interactive "e")
2810 (mouse-set-point event)
2814 "Keymap to create watch expression of a complex data type local variable.")
2816 (defconst gdb-struct-string
2817 (concat (propertize "[struct/union]"
2818 'mouse-face 'highlight
2819 'help-echo "mouse-2: create watch expression"
2820 'local-map gdb-locals-watch-map) "\n"))
2822 (defconst gdb-array-string
2823 (concat " " (propertize "[array]"
2824 'mouse-face 'highlight
2825 'help-echo "mouse-2: create watch expression"
2826 'local-map gdb-locals-watch-map) "\n"))
2828 ;; Abbreviate for arrays and structures.
2829 ;; These can be expanded using gud-display.
2830 (defun gdb-info-locals-handler ()
2831 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2832 gdb-pending-triggers))
2833 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2834 (with-current-buffer buf
2835 (goto-char (point-min))
2836 (while (re-search-forward "^[ }].*\n" nil t)
2837 (replace-match "" nil nil))
2838 (goto-char (point-min))
2839 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2840 (replace-match gdb-struct-string nil nil))
2841 (goto-char (point-min))
2842 (while (re-search-forward "\\s-*{.*\n" nil t)
2843 (replace-match gdb-array-string nil nil))))
2844 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2846 (with-current-buffer buf
2847 (let* ((window (get-buffer-window buf 0))
2848 (start (window-start window))
2849 (p (window-point window))
2850 (buffer-read-only nil))
2852 (insert-buffer-substring (gdb-get-buffer-create
2853 'gdb-partial-output-buffer))
2854 (set-window-start window start)
2855 (set-window-point window p))
2857 (run-hooks 'gdb-info-locals-hook))
2859 (defvar gdb-locals-mode-map
2860 (let ((map (make-sparse-keymap)))
2861 (suppress-keymap map)
2862 (define-key map "q" 'kill-this-buffer)
2865 (defun gdb-locals-mode ()
2866 "Major mode for gdb locals.
2868 \\{gdb-locals-mode-map}"
2869 (kill-all-local-variables)
2870 (setq major-mode 'gdb-locals-mode)
2871 (setq mode-name (concat "Locals:" gdb-selected-frame))
2872 (setq buffer-read-only t)
2873 (use-local-map gdb-locals-mode-map)
2874 (set (make-local-variable 'font-lock-defaults)
2875 '(gdb-locals-font-lock-keywords))
2876 (run-mode-hooks 'gdb-locals-mode-hook)
2877 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2878 (string-equal gdb-version "pre-6.4"))
2879 'gdb-invalidate-locals
2880 'gdb-invalidate-locals-1))
2882 (defun gdb-locals-buffer-name ()
2883 (with-current-buffer gud-comint-buffer
2884 (concat "*locals of " (gdb-get-target-string) "*")))
2886 (defun gdb-display-locals-buffer ()
2887 "Display local variables of current stack and their values."
2890 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2892 (defun gdb-frame-locals-buffer ()
2893 "Display local variables of current stack and their values in a new frame."
2895 (let ((special-display-regexps (append special-display-regexps '(".*")))
2896 (special-display-frame-alist gdb-frame-parameters))
2897 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2900 ;;;; Window management
2901 (defun gdb-display-buffer (buf dedicated &optional size)
2902 (let ((answer (get-buffer-window buf 0))
2905 (display-buffer buf nil 0) ;Deiconify the frame if necessary.
2906 ;; The buffer is not yet displayed.
2907 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2908 (let ((window (get-lru-window)))
2910 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2911 ,gdb-source-window))))
2913 (set-window-buffer window buf)
2914 (setq answer window))
2915 (setq must-split t)))
2917 (let* ((largest (get-largest-window))
2918 (cur-size (window-height largest))
2919 (new-size (and size (< size cur-size) (- cur-size size))))
2920 (setq answer (split-window largest new-size))
2921 (set-window-buffer answer buf)
2922 (set-window-dedicated-p answer dedicated)))
2926 ;;; Shared keymap initialization:
2928 (let ((menu (make-sparse-keymap "GDB-Windows")))
2929 (define-key gud-menu-map [displays]
2930 `(menu-item "GDB-Windows" ,menu
2931 :visible (memq gud-minor-mode '(gdbmi gdba))))
2932 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2933 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2934 (define-key menu [inferior]
2935 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2936 :enable gdb-use-separate-io-buffer))
2937 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2938 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2939 (define-key menu [disassembly]
2940 '("Disassembly" . gdb-display-assembler-buffer))
2941 (define-key menu [breakpoints]
2942 '("Breakpoints" . gdb-display-breakpoints-buffer))
2943 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2944 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2946 (let ((menu (make-sparse-keymap "GDB-Frames")))
2947 (define-key gud-menu-map [frames]
2948 `(menu-item "GDB-Frames" ,menu
2949 :visible (memq gud-minor-mode '(gdbmi gdba))))
2950 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2951 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2952 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2953 (define-key menu [inferior]
2954 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2955 :enable gdb-use-separate-io-buffer))
2956 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2957 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2958 (define-key menu [breakpoints]
2959 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2960 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2961 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2963 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2964 (define-key gud-menu-map [ui]
2965 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2966 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2967 (define-key menu [gdb-find-source-frame]
2968 '(menu-item "Look For Source Frame" gdb-find-source-frame
2969 :visible (eq gud-minor-mode 'gdba)
2970 :help "Toggle look for source frame."
2971 :button (:toggle . gdb-find-source-frame)))
2972 (define-key menu [gdb-use-separate-io]
2973 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2974 :visible (eq gud-minor-mode 'gdba)
2975 :help "Toggle separate IO for debugged program."
2976 :button (:toggle . gdb-use-separate-io-buffer)))
2977 (define-key menu [gdb-many-windows]
2978 '(menu-item "Display Other Windows" gdb-many-windows
2979 :help "Toggle display of locals, stack and breakpoint information"
2980 :button (:toggle . gdb-many-windows)))
2981 (define-key menu [gdb-restore-windows]
2982 '(menu-item "Restore Window Layout" gdb-restore-windows
2983 :help "Restore standard layout for debug session.")))
2985 (defun gdb-frame-gdb-buffer ()
2986 "Display GUD buffer in a new frame."
2988 (let ((special-display-regexps (append special-display-regexps '(".*")))
2989 (special-display-frame-alist
2990 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2991 gdb-frame-parameters)))
2992 (same-window-regexps nil))
2993 (display-buffer gud-comint-buffer)))
2995 (defun gdb-display-gdb-buffer ()
2996 "Display GUD buffer."
2998 (let ((same-window-regexps nil))
2999 (pop-to-buffer gud-comint-buffer)))
3001 (defun gdb-set-window-buffer (name)
3002 (set-window-buffer (selected-window) (get-buffer name))
3003 (set-window-dedicated-p (selected-window) t))
3005 (defun gdb-setup-windows ()
3006 "Layout the window pattern for `gdb-many-windows'."
3007 (gdb-display-locals-buffer)
3008 (gdb-display-stack-buffer)
3009 (delete-other-windows)
3010 (gdb-display-breakpoints-buffer)
3011 (delete-other-windows)
3013 (pop-to-buffer gud-comint-buffer)
3014 (split-window nil ( / ( * (window-height) 3) 4))
3015 (split-window nil ( / (window-height) 3))
3016 (split-window-horizontally)
3018 (gdb-set-window-buffer (gdb-locals-buffer-name))
3021 (if gud-last-last-frame
3022 (gud-find-file (car gud-last-last-frame))
3024 (gud-find-file gdb-main-file)
3025 ;; Put buffer list in window if we
3026 ;; can't find a source file.
3027 (list-buffers-noselect))))
3028 (setq gdb-source-window (selected-window))
3029 (when gdb-use-separate-io-buffer
3030 (split-window-horizontally)
3032 (gdb-set-window-buffer
3033 (gdb-get-buffer-create 'gdb-inferior-io)))
3035 (gdb-set-window-buffer (gdb-stack-buffer-name))
3036 (split-window-horizontally)
3038 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3041 (defun gdb-restore-windows ()
3042 "Restore the basic arrangement of windows used by gdba.
3043 This arrangement depends on the value of `gdb-many-windows'."
3045 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3046 (delete-other-windows)
3047 (if gdb-many-windows
3049 (when (or gud-last-last-frame gdb-show-main)
3053 (if gud-last-last-frame
3054 (gud-find-file (car gud-last-last-frame))
3055 (gud-find-file gdb-main-file)))
3056 (setq gdb-source-window (selected-window))
3060 "Exit a debugging session cleanly.
3061 Kills the gdb buffers, and resets variables and the source buffers."
3062 (dolist (buffer (buffer-list))
3063 (unless (eq buffer gud-comint-buffer)
3064 (with-current-buffer buffer
3065 (if (memq gud-minor-mode '(gdbmi gdba))
3066 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3068 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3069 (setq gud-minor-mode nil)
3070 (kill-local-variable 'tool-bar-map)
3071 (kill-local-variable 'gdb-define-alist))))))
3072 (setq gdb-overlay-arrow-position nil)
3073 (setq overlay-arrow-variable-list
3074 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3075 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3076 (setq gdb-stack-position nil)
3077 (setq overlay-arrow-variable-list
3078 (delq 'gdb-stack-position overlay-arrow-variable-list))
3079 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3080 (setq gud-running nil)
3081 (setq gdb-active-process nil)
3082 (setq gdb-var-list nil)
3083 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3085 (defun gdb-source-info ()
3086 "Find the source file where the program starts and displays it with related
3088 (goto-char (point-min))
3089 (if (and (search-forward "Located in " nil t)
3090 (looking-at "\\S-+"))
3091 (setq gdb-main-file (match-string 0)))
3092 (goto-char (point-min))
3093 (if (search-forward "Includes preprocessor macro info." nil t)
3094 (setq gdb-macro-info t))
3095 (if gdb-many-windows
3097 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3099 (let ((pop-up-windows t))
3100 (display-buffer (gud-find-file gdb-main-file)))))
3103 (defun gdb-get-location (bptno line flag)
3104 "Find the directory containing the relevant source file.
3105 Put in buffer and place breakpoint icon."
3106 (goto-char (point-min))
3107 (catch 'file-not-found
3108 (if (search-forward "Located in " nil t)
3109 (when (looking-at "\\S-+")
3110 (delete (cons bptno "File not found") gdb-location-alist)
3111 (push (cons bptno (match-string 0)) gdb-location-alist))
3113 (unless (assoc bptno gdb-location-alist)
3114 (push (cons bptno "File not found") gdb-location-alist)
3115 (message-box "Cannot find source file for breakpoint location.\n\
3116 Add directory to search path for source files using the GDB command, dir."))
3117 (throw 'file-not-found nil))
3118 (with-current-buffer
3119 (find-file-noselect (match-string 0))
3120 (save-current-buffer
3121 (set (make-local-variable 'gud-minor-mode) 'gdba)
3122 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
3123 ;; only want one breakpoint icon at each location
3125 (goto-line (string-to-number line))
3126 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
3128 (add-hook 'find-file-hook 'gdb-find-file-hook)
3130 (defun gdb-find-file-hook ()
3131 "Set up buffer for debugging if file is part of the source code
3132 of the current session."
3133 (if (and (buffer-name gud-comint-buffer)
3134 ;; in case gud or gdb-ui is just loaded
3136 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3138 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
3139 (if (member (if (string-equal gdb-version "pre-6.4")
3140 (file-name-nondirectory buffer-file-name)
3142 gdb-source-file-list)
3143 (with-current-buffer (find-buffer-visiting buffer-file-name)
3144 (set (make-local-variable 'gud-minor-mode)
3145 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3146 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3149 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3150 "Put string PUTSTRING in front of POS in the current buffer.
3151 PUTSTRING is displayed by putting an overlay into the current buffer with a
3152 `before-string' string that has a `display' property whose value is
3154 (let ((string (make-string 1 ?x))
3155 (buffer (current-buffer)))
3156 (setq putstring (copy-sequence putstring))
3157 (let ((overlay (make-overlay pos pos buffer))
3159 (list (list 'margin 'left-margin) putstring))))
3160 (put-text-property 0 1 'display prop string)
3162 (add-text-properties 0 1 sprops string))
3163 (overlay-put overlay 'put-break t)
3164 (overlay-put overlay 'before-string string))))
3166 ;;from remove-images
3167 (defun gdb-remove-strings (start end &optional buffer)
3168 "Remove strings between START and END in BUFFER.
3169 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3170 BUFFER nil or omitted means use the current buffer."
3172 (setq buffer (current-buffer)))
3173 (dolist (overlay (overlays-in start end))
3174 (when (overlay-get overlay 'put-break)
3175 (delete-overlay overlay))))
3177 (defun gdb-put-breakpoint-icon (enabled bptno)
3178 (if (string-match "[0-9+]+\\." bptno)
3179 (setq enabled gdb-parent-bptno-enabled))
3180 (let ((start (- (line-beginning-position) 1))
3181 (end (+ (line-end-position) 1))
3182 (putstring (if enabled "B" "b"))
3183 (source-window (get-buffer-window (current-buffer) 0)))
3184 (add-text-properties
3185 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3188 (add-text-properties
3189 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3190 (add-text-properties
3191 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3192 (gdb-remove-breakpoint-icons start end)
3193 (if (display-images-p)
3194 (if (>= (or left-fringe-width
3195 (if source-window (car (window-fringes source-window)))
3196 gdb-buffer-fringe-width) 8)
3199 `(left-fringe breakpoint
3202 'breakpoint-disabled))
3204 'gdb-enabled enabled)
3205 (when (< left-margin-width 2)
3206 (save-current-buffer
3207 (setq left-margin-width 2)
3211 left-margin-width right-margin-width))))
3214 (or breakpoint-enabled-icon
3215 (setq breakpoint-enabled-icon
3216 (find-image `((:type xpm :data
3217 ,breakpoint-xpm-data
3218 :ascent 100 :pointer hand)
3220 ,breakpoint-enabled-pbm-data
3221 :ascent 100 :pointer hand)))))
3222 (or breakpoint-disabled-icon
3223 (setq breakpoint-disabled-icon
3224 (find-image `((:type xpm :data
3225 ,breakpoint-xpm-data
3226 :conversion disabled
3227 :ascent 100 :pointer hand)
3229 ,breakpoint-disabled-pbm-data
3230 :ascent 100 :pointer hand))))))
3234 (when (< left-margin-width 2)
3235 (save-current-buffer
3236 (setq left-margin-width 2)
3237 (let ((window (get-buffer-window (current-buffer) 0)))
3240 window left-margin-width right-margin-width)))))
3242 (propertize putstring
3243 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3246 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3247 (gdb-remove-strings start end)
3248 (if (display-images-p)
3249 (remove-images start end))
3251 (setq left-margin-width 0)
3252 (let ((window (get-buffer-window (current-buffer) 0)))
3255 window left-margin-width right-margin-width)))))
3259 ;; Assembler buffer.
3261 (gdb-set-buffer-rules 'gdb-assembler-buffer
3262 'gdb-assembler-buffer-name
3263 'gdb-assembler-mode)
3265 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3266 ;; window-start but keep the overlay arrow/current line visible.
3267 (defun gdb-assembler-handler ()
3268 (setq gdb-pending-triggers
3269 (delq 'gdb-invalidate-assembler
3270 gdb-pending-triggers))
3271 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3273 (with-current-buffer buf
3274 (let* ((window (get-buffer-window buf 0))
3275 (p (window-point window))
3276 (buffer-read-only nil))
3278 (insert-buffer-substring (gdb-get-buffer-create
3279 'gdb-partial-output-buffer))
3280 (set-window-point window p)))))
3281 ;; put customisation here
3282 (gdb-assembler-custom))
3284 (defun gdb-assembler-custom ()
3285 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3286 (pos 1) (address) (flag) (bptno))
3287 (with-current-buffer buffer
3289 (if (not (equal gdb-pc-address "main"))
3291 (goto-char (point-min))
3292 (if (and gdb-pc-address
3293 (search-forward gdb-pc-address nil t))
3297 (setq fringe-indicator-alist
3298 (if (string-equal gdb-frame-number "0")
3300 '((overlay-arrow . hollow-right-triangle))))
3301 (or gdb-overlay-arrow-position
3302 (setq gdb-overlay-arrow-position (make-marker)))
3303 (set-marker gdb-overlay-arrow-position (point))))))
3304 ;; remove all breakpoint-icons in assembler buffer before updating.
3305 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3306 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3307 (goto-char (point-min))
3308 (while (< (point) (- (point-max) 1))
3311 "\\([0-9]+\\.?[0-9]*\\).*?\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3312 (setq bptno (match-string 1))
3313 (setq flag (char-after (match-beginning 2)))
3314 (setq address (match-string 3))
3315 (with-current-buffer buffer
3317 (goto-char (point-min))
3318 (if (search-forward address nil t)
3319 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
3320 (if (not (equal gdb-pc-address "main"))
3321 (with-current-buffer buffer
3322 (set-window-point (get-buffer-window buffer 0) pos)))))
3324 (defvar gdb-assembler-mode-map
3325 (let ((map (make-sparse-keymap)))
3326 (suppress-keymap map)
3327 (define-key map "q" 'kill-this-buffer)
3330 (defvar gdb-assembler-font-lock-keywords
3331 '(;; <__function.name+n>
3332 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3333 (1 font-lock-function-name-face))
3334 ;; 0xNNNNNNNN <__function.name+n>: opcode
3335 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3336 (4 font-lock-keyword-face))
3337 ;; %register(at least i386)
3338 ("%\\sw+" . font-lock-variable-name-face)
3339 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3340 (1 font-lock-comment-face)
3341 (2 font-lock-function-name-face))
3342 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3343 "Font lock keywords used in `gdb-assembler-mode'.")
3345 (defun gdb-assembler-mode ()
3346 "Major mode for viewing code assembler.
3348 \\{gdb-assembler-mode-map}"
3349 (kill-all-local-variables)
3350 (setq major-mode 'gdb-assembler-mode)
3351 (setq mode-name (concat "Machine:" gdb-selected-frame))
3352 (setq gdb-overlay-arrow-position nil)
3353 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3354 (setq fringes-outside-margins t)
3355 (setq buffer-read-only t)
3356 (use-local-map gdb-assembler-mode-map)
3357 (gdb-invalidate-assembler)
3358 (set (make-local-variable 'font-lock-defaults)
3359 '(gdb-assembler-font-lock-keywords))
3360 (run-mode-hooks 'gdb-assembler-mode-hook)
3361 'gdb-invalidate-assembler)
3363 (defun gdb-assembler-buffer-name ()
3364 (with-current-buffer gud-comint-buffer
3365 (concat "*disassembly of " (gdb-get-target-string) "*")))
3367 (defun gdb-display-assembler-buffer ()
3368 "Display disassembly view."
3370 (setq gdb-previous-frame nil)
3372 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3374 (defun gdb-frame-assembler-buffer ()
3375 "Display disassembly view in a new frame."
3377 (setq gdb-previous-frame nil)
3378 (let ((special-display-regexps (append special-display-regexps '(".*")))
3379 (special-display-frame-alist gdb-frame-parameters))
3380 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3382 ;; modified because if gdb-pc-address has changed value a new command
3383 ;; must be enqueued to update the buffer with the new output
3384 (defun gdb-invalidate-assembler (&optional ignored)
3385 (if (gdb-get-buffer 'gdb-assembler-buffer)
3387 (unless (and gdb-selected-frame
3388 (string-equal gdb-selected-frame gdb-previous-frame))
3389 (if (or (not (member 'gdb-invalidate-assembler
3390 gdb-pending-triggers))
3391 (not (string-equal gdb-pc-address
3392 gdb-previous-frame-address)))
3394 ;; take previous disassemble command, if any, off the queue
3395 (with-current-buffer gud-comint-buffer
3396 (let ((queue gdb-input-queue))
3397 (dolist (item queue)
3398 (if (equal (cdr item) '(gdb-assembler-handler))
3399 (setq gdb-input-queue
3400 (delete item gdb-input-queue))))))
3403 (concat gdb-server-prefix "disassemble "
3404 (if (member gdb-pc-address '(nil "main")) nil "0x")
3405 gdb-pc-address "\n")
3406 'gdb-assembler-handler))
3407 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3408 (setq gdb-previous-frame-address gdb-pc-address)
3409 (setq gdb-previous-frame gdb-selected-frame)))))))
3411 (defun gdb-get-selected-frame ()
3412 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3415 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3416 (push 'gdb-get-selected-frame
3417 gdb-pending-triggers))))
3419 (defun gdb-frame-handler ()
3420 (setq gdb-pending-triggers
3421 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3422 (goto-char (point-min))
3423 (when (re-search-forward
3424 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3425 (setq gdb-frame-number (match-string 1))
3426 (setq gdb-frame-address (match-string 2)))
3427 (goto-char (point-min))
3428 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3429 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3431 (setq gdb-selected-frame (match-string 2))
3432 (if (gdb-get-buffer 'gdb-locals-buffer)
3433 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3434 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3435 (if (gdb-get-buffer 'gdb-assembler-buffer)
3436 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3437 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3438 (setq gdb-pc-address (match-string 1))
3439 (if (and (match-string 3) gud-overlay-arrow-position)
3440 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3441 (position (marker-position gud-overlay-arrow-position)))
3443 (string-equal (buffer-name buffer)
3444 (file-name-nondirectory (match-string 3))))
3445 (with-current-buffer buffer
3446 (setq fringe-indicator-alist
3447 (if (string-equal gdb-frame-number "0")
3449 '((overlay-arrow . hollow-right-triangle))))
3450 (set-marker gud-overlay-arrow-position position))))))
3451 (goto-char (point-min))
3452 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3453 (setq gdb-current-language (match-string 1)))
3454 (gdb-invalidate-assembler))
3457 ;; Code specific to GDB 6.4
3458 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3460 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3461 "Create list of source files for current GDB session.
3462 If buffers already exist for any of these files, gud-minor-mode
3464 (goto-char (point-min))
3465 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3466 (push (match-string 1) gdb-source-file-list))
3467 (dolist (buffer (buffer-list))
3468 (with-current-buffer buffer
3469 (when (member buffer-file-name gdb-source-file-list)
3470 (set (make-local-variable 'gud-minor-mode)
3471 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3472 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3473 (when gud-tooltip-mode
3474 (make-local-variable 'gdb-define-alist)
3475 (gdb-create-define-alist)
3476 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3477 (gdb-force-mode-line-update
3478 (propertize "ready" 'face font-lock-variable-name-face)))
3480 ; Uses "-var-list-children --all-values". Needs GDB 6.4 onwards.
3481 (defun gdb-var-list-children-1 (varnum)
3484 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3485 (concat "server interpreter mi \"-var-list-children --all-values "
3487 (concat "-var-list-children --all-values " varnum "\n"))
3488 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3490 (defconst gdb-var-list-children-regexp-1
3491 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3492 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3493 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3495 (defun gdb-var-list-children-handler-1 (varnum)
3496 (goto-char (point-min))
3497 (let ((var-list nil))
3498 (catch 'child-already-watched
3499 (dolist (var gdb-var-list)
3500 (if (string-equal varnum (car var))
3503 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3504 (let ((varchild (list (match-string 1)
3508 (read (match-string 4))
3510 (if (assoc (car varchild) gdb-var-list)
3511 (throw 'child-already-watched nil))
3512 (push varchild var-list))))
3513 (push var var-list)))
3514 (setq gdb-var-list (nreverse var-list))))
3515 (gdb-speedbar-update))
3517 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3518 (defun gdb-var-update-1 ()
3519 (if (not (member 'gdb-var-update gdb-pending-triggers))
3523 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3524 "server interpreter mi \"-var-update --all-values *\"\n"
3525 "-var-update --all-values *\n")
3526 'gdb-var-update-handler-1))
3527 (push 'gdb-var-update gdb-pending-triggers))))
3529 (defconst gdb-var-update-regexp-1
3530 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3531 in_scope=\"\\(.*?\\)\".*?}")
3533 (defun gdb-var-update-handler-1 ()
3534 (dolist (var gdb-var-list)
3535 (setcar (nthcdr 5 var) nil))
3536 (goto-char (point-min))
3537 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3538 (let* ((varnum (match-string 1))
3539 (var (assoc varnum gdb-var-list)))
3541 (let ((match (match-string 3)))
3542 (cond ((string-equal match "false")
3543 (if gdb-delete-out-of-scope
3544 (gdb-var-delete-1 varnum)
3545 (setcar (nthcdr 5 var) 'out-of-scope)))
3546 ((string-equal match "true")
3547 (setcar (nthcdr 5 var) 'changed)
3548 (setcar (nthcdr 4 var)
3549 (read (match-string 2))))
3550 ((string-equal match "invalid")
3551 (gdb-var-delete-1 varnum)))))))
3552 (setq gdb-pending-triggers
3553 (delq 'gdb-var-update gdb-pending-triggers))
3554 (gdb-speedbar-update))
3556 ;; Registers buffer.
3558 (gdb-set-buffer-rules 'gdb-registers-buffer
3559 'gdb-registers-buffer-name
3560 'gdb-registers-mode)
3562 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3563 (gdb-get-buffer 'gdb-registers-buffer)
3564 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3565 "server interpreter mi \"-data-list-register-values x\"\n"
3566 "-data-list-register-values x\n")
3567 gdb-data-list-register-values-handler)
3569 (defconst gdb-data-list-register-values-regexp
3570 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3572 (defun gdb-data-list-register-values-handler ()
3573 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3574 gdb-pending-triggers))
3575 (goto-char (point-min))
3576 (if (re-search-forward gdb-error-regexp nil t)
3577 (let ((err (match-string 1)))
3578 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3579 (let ((buffer-read-only nil))
3581 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3583 (goto-char (point-min)))))
3584 (let ((register-list (reverse gdb-register-names))
3585 (register nil) (register-string nil) (register-values nil))
3586 (goto-char (point-min))
3587 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3588 (setq register (pop register-list))
3589 (setq register-string (concat register "\t" (match-string 2) "\n"))
3590 (if (member (match-string 1) gdb-changed-registers)
3591 (put-text-property 0 (length register-string)
3592 'face 'font-lock-warning-face
3594 (setq register-values
3595 (concat register-values register-string)))
3596 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3597 (with-current-buffer buf
3598 (let* ((window (get-buffer-window buf 0))
3599 (start (window-start window))
3600 (p (window-point window))
3601 (buffer-read-only nil))
3603 (insert register-values)
3604 (set-window-start window start)
3605 (set-window-point window p))))))
3606 (gdb-data-list-register-values-custom))
3608 (defun gdb-data-list-register-values-custom ()
3609 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3611 (let ((buffer-read-only nil)
3613 (goto-char (point-min))
3614 (while (< (point) (point-max))
3615 (setq start (line-beginning-position))
3616 (setq end (line-end-position))
3617 (when (looking-at "^[^\t]+")
3618 (unless (string-equal (match-string 0) "No registers.")
3619 (put-text-property start (match-end 0)
3620 'face font-lock-variable-name-face)
3621 (add-text-properties start end
3622 '(help-echo "mouse-2: edit value"
3623 mouse-face highlight))))
3624 (forward-line 1))))))
3626 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3627 (defun gdb-get-changed-registers ()
3628 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3629 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3633 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3634 "server interpreter mi -data-list-changed-registers\n"
3635 "-data-list-changed-registers\n")
3636 'gdb-get-changed-registers-handler))
3637 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3639 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3641 (defun gdb-get-changed-registers-handler ()
3642 (setq gdb-pending-triggers
3643 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3644 (setq gdb-changed-registers nil)
3645 (goto-char (point-min))
3646 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3647 (push (match-string 1) gdb-changed-registers)))
3652 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3653 (gdb-set-buffer-rules 'gdb-locals-buffer
3654 'gdb-locals-buffer-name
3657 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3658 (gdb-get-buffer 'gdb-locals-buffer)
3659 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3660 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3661 "-stack-list-locals --simple-values\n")
3662 gdb-stack-list-locals-handler)
3664 (defconst gdb-stack-list-locals-regexp
3665 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3667 (defvar gdb-locals-watch-map-1
3668 (let ((map (make-sparse-keymap)))
3669 (suppress-keymap map)
3670 (define-key map "\r" 'gud-watch)
3671 (define-key map [mouse-2] 'gud-watch)
3673 "Keymap to create watch expression of a complex data type local variable.")
3675 (defvar gdb-edit-locals-map-1
3676 (let ((map (make-sparse-keymap)))
3677 (suppress-keymap map)
3678 (define-key map "\r" 'gdb-edit-locals-value)
3679 (define-key map [mouse-2] 'gdb-edit-locals-value)
3681 "Keymap to edit value of a simple data type local variable.")
3683 (defun gdb-edit-locals-value (&optional event)
3684 "Assign a value to a variable displayed in the locals buffer."
3685 (interactive (list last-input-event))
3687 (if event (posn-set-point (event-end event)))
3689 (let* ((var (current-word))
3690 (value (read-string (format "New value (%s): " var))))
3692 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3695 ;; Dont display values of arrays or structures.
3696 ;; These can be expanded using gud-watch.
3697 (defun gdb-stack-list-locals-handler ()
3698 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3699 gdb-pending-triggers))
3700 (goto-char (point-min))
3701 (if (re-search-forward gdb-error-regexp nil t)
3702 (let ((err (match-string 1)))
3703 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3704 (let ((buffer-read-only nil))
3707 (goto-char (point-min)))))
3708 (let (local locals-list)
3709 (goto-char (point-min))
3710 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3711 (let ((local (list (match-string 1)
3714 (if (looking-at ",value=\\(\".*\"\\).*?}")
3715 (setcar (nthcdr 2 local) (read (match-string 1))))
3716 (push local locals-list)))
3717 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3718 (and buf (with-current-buffer buf
3719 (let* ((window (get-buffer-window buf 0))
3720 (start (window-start window))
3721 (p (window-point window))
3722 (buffer-read-only nil) (name) (value))
3724 (dolist (local locals-list)
3725 (setq name (car local))
3726 (setq value (nth 2 local))
3728 (string-match "^\\0x" value))
3729 (add-text-properties 0 (length name)
3730 `(mouse-face highlight
3731 help-echo "mouse-2: create watch expression"
3732 local-map ,gdb-locals-watch-map-1)
3734 (add-text-properties 0 (length value)
3735 `(mouse-face highlight
3736 help-echo "mouse-2: edit value"
3737 local-map ,gdb-edit-locals-map-1)
3740 (concat name "\t" (nth 1 local)
3742 (set-window-start window start)
3743 (set-window-point window p))))))))
3745 (defun gdb-get-register-names ()
3746 "Create a list of register names."
3747 (goto-char (point-min))
3748 (setq gdb-register-names nil)
3749 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3750 (push (match-string 1) gdb-register-names)))
3754 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3755 ;;; gdb-ui.el ends here