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-delete-1 (varnum)
887 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
889 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
890 (concat "-var-delete " varnum "\n"))
892 (setq gdb-var-list (delq var gdb-var-list))
893 (dolist (varchild gdb-var-list)
894 (if (string-match (concat (car var) "\\.") (car varchild))
895 (setq gdb-var-list (delq varchild gdb-var-list)))))
897 (defun gdb-var-delete ()
898 "Delete watch expression at point from the speedbar."
900 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
902 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
904 (if (string-match "\\." (car var))
905 (message-box "Can only delete a root expression")
906 (gdb-var-delete-1 varnum)))))
908 (defun gdb-var-delete-children (varnum)
909 "Delete children of variable object at point from the speedbar."
912 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
913 (concat "server interpreter mi \"-var-delete -c " varnum "\"\n")
914 (concat "-var-delete -c " varnum "\n")) 'ignore)))
916 (defun gdb-edit-value (text token indent)
917 "Assign a value to a variable displayed in the speedbar."
918 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
919 (varnum (car var)) (value))
920 (setq value (read-string "New value: "))
923 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
924 (concat "server interpreter mi \"-var-assign "
925 varnum " " value "\"\n")
926 (concat "-var-assign " varnum " " value "\n"))
927 `(lambda () (gdb-edit-value-handler ,value))))))
929 (defun gdb-edit-value-handler (value)
930 (goto-char (point-min))
931 (if (re-search-forward gdb-error-regexp nil t)
932 (message-box "Invalid number or expression (%s)" value)))
934 (defcustom gdb-show-changed-values t
935 "If non-nil change the face of out of scope variables and changed values.
936 Out of scope variables are suppressed with `shadow' face.
937 Changed values are highlighted with the face `font-lock-warning-face'."
942 (defcustom gdb-max-children 40
943 "Maximum number of children before expansion requires confirmation."
948 (defcustom gdb-delete-out-of-scope t
949 "If non-nil delete watch expressions automatically when they go out of scope."
954 (defun gdb-speedbar-expand-node (text token indent)
955 "Expand the node the user clicked on.
956 TEXT is the text of the button we clicked on, a + or - item.
957 TOKEN is data related to this node.
958 INDENT is the current indentation depth."
959 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
961 (cond ((string-match "+" text) ;expand this node
962 (let* ((var (assoc token gdb-var-list))
963 (expr (nth 1 var)) (children (nth 2 var)))
964 (if (or (<= (string-to-number children) gdb-max-children)
967 "%s has %s children. Continue? " expr children)))
968 (if (and (eq (buffer-local-value
969 'gud-minor-mode gud-comint-buffer) 'gdba)
970 (string-equal gdb-version "pre-6.4"))
971 (gdb-var-list-children token)
972 (gdb-var-list-children-1 token)))))
973 ((string-match "-" text) ;contract this node
974 (dolist (var gdb-var-list)
975 (if (string-match (concat token "\\.") (car var))
976 (setq gdb-var-list (delq var gdb-var-list))))
977 (gdb-var-delete-children token)
978 (speedbar-change-expand-button-char ?+)
979 (speedbar-delete-subblock indent))
980 (t (error "Ooops... not sure what to do")))
981 (speedbar-center-buffer-smartly))
982 (message-box "GUD session has been killed")))
984 (defun gdb-get-target-string ()
985 (with-current-buffer gud-comint-buffer
992 ;; Each buffer has a TYPE -- a symbol that identifies the function
993 ;; of that particular buffer.
995 ;; The usual gdb interaction buffer is given the type `gdba' and
996 ;; is constructed specially.
998 ;; Others are constructed by gdb-get-buffer-create and
999 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
1001 (defvar gdb-buffer-rules-assoc '())
1003 (defun gdb-get-buffer (key)
1004 "Return the gdb buffer tagged with type KEY.
1005 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1007 (gdb-look-for-tagged-buffer key (buffer-list))))
1009 (defun gdb-get-buffer-create (key)
1010 "Create a new gdb buffer of the type specified by KEY.
1011 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1012 (or (gdb-get-buffer key)
1013 (let* ((rules (assoc key gdb-buffer-rules-assoc))
1014 (name (funcall (gdb-rules-name-maker rules)))
1015 (new (get-buffer-create name)))
1016 (with-current-buffer new
1018 (if (cdr (cdr rules))
1019 (setq trigger (funcall (car (cdr (cdr rules))))))
1020 (setq gdb-buffer-type key)
1021 (set (make-local-variable 'gud-minor-mode)
1022 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1023 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1024 (if trigger (funcall trigger)))
1027 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
1029 (defun gdb-look-for-tagged-buffer (key bufs)
1031 (while (and (not retval) bufs)
1032 (set-buffer (car bufs))
1033 (if (eq gdb-buffer-type key)
1034 (setq retval (car bufs)))
1035 (setq bufs (cdr bufs)))
1039 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1040 ;; at least one and possible more functions. The functions have these
1041 ;; roles in defining a buffer type:
1043 ;; NAME - Return a name for this buffer type.
1045 ;; The remaining function(s) are optional:
1047 ;; MODE - called in a new buffer with no arguments, should establish
1048 ;; the proper mode for the buffer.
1051 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1052 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
1054 (setcdr binding rules)
1055 (push (cons buffer-type rules)
1056 gdb-buffer-rules-assoc))))
1058 ;; GUD buffers are an exception to the rules
1059 (gdb-set-buffer-rules 'gdba 'error)
1061 ;; Partial-output buffer : This accumulates output from a command executed on
1062 ;; behalf of emacs (rather than the user).
1064 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1065 'gdb-partial-output-name)
1067 (defun gdb-partial-output-name ()
1068 (concat " *partial-output-"
1069 (gdb-get-target-string)
1073 (gdb-set-buffer-rules 'gdb-inferior-io
1074 'gdb-inferior-io-name
1075 'gdb-inferior-io-mode)
1077 (defun gdb-inferior-io-name ()
1078 (concat "*input/output of "
1079 (gdb-get-target-string)
1082 (defun gdb-display-separate-io-buffer ()
1083 "Display IO of debugged program in a separate window."
1085 (if gdb-use-separate-io-buffer
1087 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1089 (defconst gdb-frame-parameters
1090 '((height . 14) (width . 80)
1092 (tool-bar-lines . nil)
1093 (menu-bar-lines . nil)
1094 (minibuffer . nil)))
1096 (defun gdb-frame-separate-io-buffer ()
1097 "Display IO of debugged program in a new frame."
1099 (if gdb-use-separate-io-buffer
1100 (let ((special-display-regexps (append special-display-regexps '(".*")))
1101 (special-display-frame-alist gdb-frame-parameters))
1102 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1104 (defvar gdb-inferior-io-mode-map
1105 (let ((map (make-sparse-keymap)))
1106 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1107 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1108 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1109 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1110 (define-key map "\C-d" 'gdb-separate-io-eof)
1113 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1114 "Major mode for gdb inferior-io."
1115 :syntax-table nil :abbrev-table nil
1116 ;; We want to use comint because it has various nifty and familiar
1117 ;; features. We don't need a process, but comint wants one, so create
1119 (make-comint-in-buffer
1120 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1121 (current-buffer) "hexl")
1122 (setq comint-input-sender 'gdb-inferior-io-sender))
1124 (defun gdb-inferior-io-sender (proc string)
1125 ;; PROC is the pseudo-process created to satisfy comint.
1126 (with-current-buffer (process-buffer proc)
1127 (setq proc (get-buffer-process gud-comint-buffer))
1128 (process-send-string proc string)
1129 (process-send-string proc "\n")))
1131 (defun gdb-separate-io-interrupt ()
1132 "Interrupt the program being debugged."
1135 (get-buffer-process gud-comint-buffer) comint-ptyp))
1137 (defun gdb-separate-io-quit ()
1138 "Send quit signal to the program being debugged."
1141 (get-buffer-process gud-comint-buffer) comint-ptyp))
1143 (defun gdb-separate-io-stop ()
1144 "Stop the program being debugged."
1147 (get-buffer-process gud-comint-buffer) comint-ptyp))
1149 (defun gdb-separate-io-eof ()
1150 "Send end-of-file to the program being debugged."
1153 (get-buffer-process gud-comint-buffer)))
1156 ;; gdb communications
1159 ;; INPUT: things sent to gdb
1161 ;; The queues are lists. Each element is either a string (indicating user or
1162 ;; user-like input) or a list of the form:
1164 ;; (INPUT-STRING HANDLER-FN)
1166 ;; The handler function will be called from the partial-output buffer when the
1167 ;; command completes. This is the way to write commands which invoke gdb
1168 ;; commands autonomously.
1170 ;; These lists are consumed tail first.
1173 (defun gdb-send (proc string)
1174 "A comint send filter for gdb.
1175 This filter may simply queue input for a later time."
1177 (with-current-buffer gud-comint-buffer
1178 (let ((inhibit-read-only t))
1179 (remove-text-properties (point-min) (point-max) '(face))))
1182 (let ((item (concat string "\n")))
1183 (if gdb-enable-debug (push (cons 'send item) gdb-debug-log))
1184 (process-send-string proc item)))
1185 (if (string-match "\\\\\\'" string)
1186 (setq gdb-continuation (concat gdb-continuation string "\n"))
1187 (let ((item (concat gdb-continuation string
1188 (if (not comint-input-sender-no-newline) "\n"))))
1189 (gdb-enqueue-input item)
1190 (setq gdb-continuation nil))))))
1192 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1193 ;; is a query, or other non-top-level prompt.
1195 (defun gdb-enqueue-input (item)
1196 (if (not gud-running)
1199 (gdb-send-item item)
1200 (setq gdb-prompting nil))
1201 (push item gdb-input-queue))))
1203 (defun gdb-dequeue-input ()
1204 (let ((queue gdb-input-queue))
1206 (let ((last (car (last queue))))
1207 (unless (nbutlast queue) (setq gdb-input-queue '()))
1210 (defun gdb-send-item (item)
1211 (setq gdb-flush-pending-output nil)
1212 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1213 (setq gdb-current-item item)
1214 (let ((process (get-buffer-process gud-comint-buffer)))
1215 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1218 (setq gdb-output-sink 'user)
1219 (process-send-string process item))
1221 (gdb-clear-partial-output)
1222 (setq gdb-output-sink 'pre-emacs)
1223 (process-send-string process
1225 ;; case: eq gud-minor-mode 'gdbmi
1226 (gdb-clear-partial-output)
1227 (setq gdb-output-sink 'emacs)
1228 (process-send-string process (car item)))))
1231 ;; output -- things gdb prints to emacs
1233 ;; GDB output is a stream interrupted by annotations.
1234 ;; Annotations can be recognized by their beginning
1235 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1237 ;; The tag is a string obeying symbol syntax.
1239 ;; The optional part `<opt>' can be either the empty string
1240 ;; or a space followed by more data relating to the annotation.
1241 ;; For example, the SOURCE annotation is followed by a filename,
1242 ;; line number and various useless goo. This data must not include
1246 (defcustom gud-gdb-command-name "gdb --annotate=3"
1247 "Default command to execute an executable under the GDB debugger."
1252 (defvar gdb-annotation-rules
1253 '(("pre-prompt" gdb-pre-prompt)
1254 ("prompt" gdb-prompt)
1255 ("commands" gdb-subprompt)
1256 ("overload-choice" gdb-subprompt)
1257 ("query" gdb-subprompt)
1258 ;; Need this prompt for GDB 6.1
1259 ("nquery" gdb-subprompt)
1260 ("prompt-for-continue" gdb-subprompt)
1261 ("post-prompt" gdb-post-prompt)
1262 ("source" gdb-source)
1263 ("starting" gdb-starting)
1264 ("exited" gdb-exited)
1265 ("signalled" gdb-signalled)
1266 ("signal" gdb-signal)
1267 ("breakpoint" gdb-stopping)
1268 ("watchpoint" gdb-stopping)
1269 ("frame-begin" gdb-frame-begin)
1270 ("stopped" gdb-stopped)
1271 ("error-begin" gdb-error)
1273 ) "An assoc mapping annotation tags to functions which process them.")
1276 (setq gdb-flush-pending-output t)
1277 (setq gud-running nil)
1278 (gdb-force-mode-line-update
1279 (propertize "stopped"'face font-lock-warning-face))
1280 (setq gdb-output-sink 'user)
1281 (setq gdb-input-queue nil)
1282 (setq gdb-pending-triggers nil)
1283 (setq gdb-prompting t))
1285 (defconst gdb-source-spec-regexp
1286 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1288 ;; Do not use this except as an annotation handler.
1289 (defun gdb-source (args)
1290 (string-match gdb-source-spec-regexp args)
1291 ;; Extract the frame position from the marker.
1292 (setq gud-last-frame
1294 (match-string 1 args)
1295 (string-to-number (match-string 2 args))))
1296 (setq gdb-pc-address (match-string 3 args))
1297 ;; cover for auto-display output which comes *before*
1298 ;; stopped annotation
1299 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1301 (defun gdb-pre-prompt (ignored)
1302 "An annotation handler for `pre-prompt'.
1303 This terminates the collection of output from a previous command if that
1304 happens to be in effect."
1305 (setq gdb-error nil)
1306 (let ((sink gdb-output-sink))
1310 (setq gdb-output-sink 'post-emacs))
1313 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1315 (defun gdb-prompt (ignored)
1316 "An annotation handler for `prompt'.
1317 This sends the next command (if any) to gdb."
1318 (when gdb-first-prompt
1319 (gdb-force-mode-line-update
1320 (propertize "initializing..." 'face font-lock-variable-name-face))
1322 (setq gdb-first-prompt nil))
1323 (let ((sink gdb-output-sink))
1326 ((eq sink 'post-emacs)
1327 (setq gdb-output-sink 'user)
1329 (car (cdr gdb-current-item))))
1330 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1331 (funcall handler))))
1334 (error "Phase error in gdb-prompt (got %s)" sink))))
1335 (let ((input (gdb-dequeue-input)))
1337 (gdb-send-item input)
1339 (setq gdb-prompting t)
1340 (gud-display-frame)))))
1342 (defun gdb-subprompt (ignored)
1343 "An annotation handler for non-top-level prompts."
1344 (setq gdb-prompting t))
1346 (defun gdb-starting (ignored)
1347 "An annotation handler for `starting'.
1348 This says that I/O for the subprocess is now the program being debugged,
1350 (setq gdb-active-process t)
1351 (setq gdb-printing t)
1352 (let ((sink gdb-output-sink))
1356 (setq gud-running t)
1357 (setq gdb-inferior-status "running")
1358 (setq gdb-signalled nil)
1359 (gdb-force-mode-line-update
1360 (propertize gdb-inferior-status 'face font-lock-type-face))
1361 (gdb-remove-text-properties)
1362 (setq gud-old-arrow gud-overlay-arrow-position)
1363 (setq gud-overlay-arrow-position nil)
1364 (setq gdb-overlay-arrow-position nil)
1365 (setq gdb-stack-position nil)
1366 (if gdb-use-separate-io-buffer
1367 (setq gdb-output-sink 'inferior))))
1370 (error "Unexpected `starting' annotation")))))
1372 (defun gdb-signal (ignored)
1373 (setq gdb-inferior-status "signal")
1374 (gdb-force-mode-line-update
1375 (propertize gdb-inferior-status 'face font-lock-warning-face))
1376 (gdb-stopping ignored))
1378 (defun gdb-stopping (ignored)
1379 "An annotation handler for `breakpoint' and other annotations.
1380 They say that I/O for the subprocess is now GDB, not the program
1382 (if gdb-use-separate-io-buffer
1383 (let ((sink gdb-output-sink))
1385 ((eq sink 'inferior)
1386 (setq gdb-output-sink 'user))
1389 (error "Unexpected stopping annotation"))))))
1391 (defun gdb-exited (ignored)
1392 "An annotation handler for `exited' and `signalled'.
1393 They say that I/O for the subprocess is now GDB, not the program
1394 being debugged and that the program is no longer running. This
1395 function is used to change the focus of GUD tooltips to #define
1397 (setq gdb-active-process nil)
1398 (setq gud-overlay-arrow-position nil)
1399 (setq gdb-overlay-arrow-position nil)
1400 (setq gdb-stack-position nil)
1401 (setq gud-old-arrow nil)
1402 (setq gdb-inferior-status "exited")
1403 (gdb-force-mode-line-update
1404 (propertize gdb-inferior-status 'face font-lock-warning-face))
1405 (gdb-stopping ignored))
1407 (defun gdb-signalled (ignored)
1408 (setq gdb-signalled t))
1410 (defun gdb-frame-begin (ignored)
1411 (setq gdb-frame-begin t)
1412 (setq gdb-printing nil)
1413 (let ((sink gdb-output-sink))
1415 ((eq sink 'inferior)
1416 (setq gdb-output-sink 'user))
1418 ((eq sink 'emacs) t)
1421 (error "Unexpected frame-begin annotation (%S)" sink)))))
1423 (defcustom gdb-same-frame focus-follows-mouse
1424 "Non-nil means pop up GUD buffer in same frame."
1429 (defcustom gdb-find-source-frame nil
1430 "Non-nil means try to find a source frame further up stack e.g after signal."
1435 (defun gdb-find-source-frame (arg)
1436 "Toggle trying to find a source frame further up stack.
1437 With prefix argument ARG, look for a source frame further up
1438 stack if ARG is positive, otherwise don't look further up."
1440 (setq gdb-find-source-frame
1442 (not gdb-find-source-frame)
1443 (> (prefix-numeric-value arg) 0)))
1444 (message (format "Looking for source frame %sabled"
1445 (if gdb-find-source-frame "en" "dis"))))
1447 (defun gdb-stopped (ignored)
1448 "An annotation handler for `stopped'.
1449 It is just like `gdb-stopping', except that if we already set the output
1450 sink to `user' in `gdb-stopping', that is fine."
1451 (setq gud-running nil)
1452 (unless (or gud-overlay-arrow-position gud-last-frame)
1453 (if (and gdb-frame-begin gdb-printing)
1454 (setq gud-overlay-arrow-position gud-old-arrow)
1455 ;;Pop up GUD buffer to display current frame when it doesn't have source
1456 ;;information i.e if not compiled with -g as with libc routines generally.
1458 (gdb-display-gdb-buffer)
1459 (gdb-frame-gdb-buffer))
1460 (if gdb-find-source-frame
1461 ;;Try to find source further up stack e.g after signal.
1462 (setq gdb-look-up-stack
1463 (if (gdb-get-buffer 'gdb-stack-buffer)
1466 (gdb-get-buffer-create 'gdb-stack-buffer)
1467 (gdb-invalidate-frames)
1469 (unless (member gdb-inferior-status '("exited" "signal"))
1470 (setq gdb-active-process t) ;Just for attaching case.
1471 (setq gdb-inferior-status "stopped")
1472 (gdb-force-mode-line-update
1473 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1474 (let ((sink gdb-output-sink))
1476 ((eq sink 'inferior)
1477 (setq gdb-output-sink 'user))
1481 (error "Unexpected stopped annotation"))))
1482 (if gdb-signalled (gdb-exited ignored)))
1484 (defun gdb-error (ignored)
1485 (setq gdb-error (not gdb-error)))
1487 (defun gdb-post-prompt (ignored)
1488 "An annotation handler for `post-prompt'.
1489 This begins the collection of output from the current command if that
1490 happens to be appropriate."
1491 ;; Don't add to queue if there outstanding items or gdb-version is not known
1493 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1494 (gdb-get-selected-frame)
1495 (gdb-invalidate-frames)
1496 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1497 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1498 (gdb-invalidate-breakpoints)
1499 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1500 ;; so gdb-pc-address is updated.
1501 ;; (gdb-invalidate-assembler)
1503 (if (string-equal gdb-version "pre-6.4")
1504 (gdb-invalidate-registers)
1505 (gdb-get-changed-registers)
1506 (gdb-invalidate-registers-1))
1508 (gdb-invalidate-memory)
1509 (if (string-equal gdb-version "pre-6.4")
1510 (gdb-invalidate-locals)
1511 (gdb-invalidate-locals-1))
1513 (gdb-invalidate-threads)
1514 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1515 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1516 ;; Only needed/used with speedbar/watch expressions.
1517 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1518 (if (string-equal gdb-version "pre-6.4")
1520 (gdb-var-update-1)))))
1521 (setq gdb-first-post-prompt nil)
1522 (let ((sink gdb-output-sink))
1525 ((eq sink 'pre-emacs)
1526 (setq gdb-output-sink 'emacs))
1529 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1531 (defconst gdb-buffer-list
1532 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1534 (defun gdb-remove-text-properties ()
1535 (dolist (buffertype gdb-buffer-list)
1536 (let ((buffer (gdb-get-buffer buffertype)))
1538 (with-current-buffer buffer
1539 (let ((inhibit-read-only t))
1540 (remove-text-properties
1541 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1543 ;; GUD displays the selected GDB frame. This might might not be the current
1544 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1545 ;; visited breakpoint is, use that window.
1546 (defun gdb-display-source-buffer (buffer)
1547 (let* ((last-window (if gud-last-last-frame
1549 (gud-find-file (car gud-last-last-frame)))))
1550 (source-window (or last-window
1551 (if (and gdb-source-window
1552 (window-live-p gdb-source-window))
1553 gdb-source-window))))
1555 (setq gdb-source-window source-window)
1556 (set-window-buffer source-window buffer))
1559 ;; Derived from gud-gdb-marker-regexp
1560 (defvar gdb-fullname-regexp
1561 (concat "\\(.:?[^" ":" "\n]*\\)" ":" "\\([0-9]*\\)" ":" ".*"))
1563 (defun gud-gdba-marker-filter (string)
1564 "A gud marker filter for gdb. Handle a burst of output from GDB."
1565 (if gdb-flush-pending-output
1567 (when gdb-enable-debug
1568 (push (cons 'recv string) gdb-debug-log)
1569 (if (and gdb-debug-log-max
1570 (> (length gdb-debug-log) gdb-debug-log-max))
1571 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1572 ;; Recall the left over gud-marker-acc from last time.
1573 (setq gud-marker-acc (concat gud-marker-acc string))
1574 ;; Start accumulating output for the GUD buffer.
1577 ;; Process all the complete markers in this chunk.
1578 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1579 (let ((annotation (match-string 1 gud-marker-acc))
1580 (before (substring gud-marker-acc 0 (match-beginning 0)))
1581 (after (substring gud-marker-acc (match-end 0))))
1583 ;; Parse the tag from the annotation, and maybe its arguments.
1584 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1585 (let* ((annotation-type (match-string 1 annotation))
1586 (annotation-arguments (match-string 2 annotation))
1587 (annotation-rule (assoc annotation-type
1588 gdb-annotation-rules))
1589 (fullname (string-match gdb-fullname-regexp annotation-type)))
1591 ;; Stuff prior to the match is just ordinary output.
1592 ;; It is either concatenated to OUTPUT or directed
1595 (gdb-concat-output output
1596 (concat before (if fullname "\n"))))
1598 ;; Take that stuff off the gud-marker-acc.
1599 (setq gud-marker-acc after)
1601 ;; Call the handler for this annotation.
1603 (funcall (car (cdr annotation-rule))
1604 annotation-arguments)
1606 ;; Switch to gud-gdb-marker-filter if appropriate.
1609 ;; Extract the frame position from the marker.
1610 (setq gud-last-frame (cons (match-string 1 annotation)
1612 (match-string 2 annotation))))
1614 (set (make-local-variable 'gud-minor-mode) 'gdb)
1615 (set (make-local-variable 'gud-marker-filter)
1616 'gud-gdb-marker-filter)))
1618 ;; Else the annotation is not recognized. Ignore it silently,
1619 ;; so that GDB can add new annotations without causing
1623 ;; Does the remaining text end in a partial line?
1624 ;; If it does, then keep part of the gud-marker-acc until we get more.
1625 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1628 ;; Everything before the potential marker start can be output.
1630 (gdb-concat-output output
1631 (substring gud-marker-acc 0
1632 (match-beginning 0))))
1634 ;; Everything after, we save, to combine with later input.
1635 (setq gud-marker-acc (substring gud-marker-acc
1636 (match-beginning 0))))
1638 ;; In case we know the gud-marker-acc contains no partial annotations:
1640 (setq output (gdb-concat-output output gud-marker-acc))
1641 (setq gud-marker-acc "")))
1644 (defun gdb-concat-output (so-far new)
1646 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1647 (let ((sink gdb-output-sink))
1649 ((eq sink 'user) (concat so-far new))
1650 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1652 (gdb-append-to-partial-output new)
1654 ((eq sink 'inferior)
1655 (gdb-append-to-inferior-io new)
1659 (error "Bogon output sink %S" sink)))))
1661 (defun gdb-append-to-partial-output (string)
1662 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1663 (goto-char (point-max))
1666 (defun gdb-clear-partial-output ()
1667 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1670 (defun gdb-append-to-inferior-io (string)
1671 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1672 (goto-char (point-max))
1673 (insert-before-markers string))
1674 (if (not (string-equal string ""))
1675 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1677 (defun gdb-clear-inferior-io ()
1678 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1682 ;; One trick is to have a command who's output is always available in a buffer
1683 ;; of it's own, and is always up to date. We build several buffers of this
1686 ;; There are two aspects to this: gdb has to tell us when the output for that
1687 ;; command might have changed, and we have to be able to run the command
1688 ;; behind the user's back.
1690 ;; The output phasing associated with the variable gdb-output-sink
1691 ;; help us to run commands behind the user's back.
1693 ;; Below is the code for specificly managing buffers of output from one
1697 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1698 ;; It adds an input for the command we are tracking. It should be the
1699 ;; annotation rule binding of whatever gdb sends to tell us this command
1700 ;; might have changed it's output.
1702 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1703 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1704 ;; input in the input queue (see comment about ``gdb communications'' above).
1706 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1708 `(defun ,name (&optional ignored)
1709 (if (and ,demand-predicate
1711 gdb-pending-triggers)))
1714 (list ,gdb-command ',output-handler))
1715 (push ',name gdb-pending-triggers)))))
1717 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1719 (setq gdb-pending-triggers
1721 gdb-pending-triggers))
1722 (let ((buf (gdb-get-buffer ',buf-key)))
1724 (with-current-buffer buf
1725 (let* ((window (get-buffer-window buf 0))
1726 (start (window-start window))
1727 (p (window-point window))
1728 (buffer-read-only nil))
1730 (insert-buffer-substring (gdb-get-buffer-create
1731 'gdb-partial-output-buffer))
1732 (set-window-start window start)
1733 (set-window-point window p)))))
1734 ;; put customisation here
1737 (defmacro def-gdb-auto-updated-buffer (buffer-key
1738 trigger-name gdb-command
1739 output-handler-name custom-defun)
1741 (def-gdb-auto-update-trigger ,trigger-name
1742 ;; The demand predicate:
1743 (gdb-get-buffer ',buffer-key)
1745 ,output-handler-name)
1746 (def-gdb-auto-update-handler ,output-handler-name
1747 ,trigger-name ,buffer-key ,custom-defun)))
1751 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1753 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1754 'gdb-breakpoints-buffer-name
1755 'gdb-breakpoints-mode)
1757 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1758 ;; This defines the auto update rule for buffers of type
1759 ;; `gdb-breakpoints-buffer'.
1761 ;; It defines a function to serve as the annotation handler that
1762 ;; handles the `foo-invalidated' message. That function is called:
1763 gdb-invalidate-breakpoints
1765 ;; To update the buffer, this command is sent to gdb.
1766 "server info breakpoints\n"
1768 ;; This also defines a function to be the handler for the output
1769 ;; from the command above. That function will copy the output into
1770 ;; the appropriately typed buffer. That function will be called:
1771 gdb-info-breakpoints-handler
1772 ;; buffer specific functions
1773 gdb-info-breakpoints-custom)
1775 (defconst breakpoint-xpm-data
1777 static char *magick[] = {
1778 /* columns rows colors chars-per-pixel */
1794 "XPM data used for breakpoint icon.")
1796 (defconst breakpoint-enabled-pbm-data
1799 0 0 0 0 1 1 1 1 0 0 0 0
1800 0 0 0 1 1 1 1 1 1 0 0 0
1801 0 0 1 1 1 1 1 1 1 1 0 0
1802 0 1 1 1 1 1 1 1 1 1 1 0
1803 0 1 1 1 1 1 1 1 1 1 1 0
1804 0 1 1 1 1 1 1 1 1 1 1 0
1805 0 1 1 1 1 1 1 1 1 1 1 0
1806 0 0 1 1 1 1 1 1 1 1 0 0
1807 0 0 0 1 1 1 1 1 1 0 0 0
1808 0 0 0 0 1 1 1 1 0 0 0 0"
1809 "PBM data used for enabled breakpoint icon.")
1811 (defconst breakpoint-disabled-pbm-data
1823 0 0 0 1 0 1 0 1 0 0"
1824 "PBM data used for disabled breakpoint icon.")
1826 (defvar breakpoint-enabled-icon nil
1827 "Icon for enabled breakpoint in display margin.")
1829 (defvar breakpoint-disabled-icon nil
1830 "Icon for disabled breakpoint in display margin.")
1832 (and (display-images-p)
1833 ;; Bitmap for breakpoint in fringe
1834 (define-fringe-bitmap 'breakpoint
1835 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1836 ;; Bitmap for gud-overlay-arrow in fringe
1837 (define-fringe-bitmap 'hollow-right-triangle
1838 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1840 (defface breakpoint-enabled
1844 "Face for enabled breakpoint icon in fringe."
1847 (defface breakpoint-disabled
1848 '((((class color) (min-colors 88)) :foreground "grey70")
1849 ;; Ensure that on low-color displays that we end up something visible.
1850 (((class color) (min-colors 8) (background light))
1851 :foreground "black")
1852 (((class color) (min-colors 8) (background dark))
1853 :foreground "white")
1854 (((type tty) (class mono))
1856 (t :background "gray"))
1857 "Face for disabled breakpoint icon in fringe."
1860 (defconst gdb-breakpoint-regexp
1861 "\\(?:\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\|\\([0-9]+\\.[0-9]+\\)\\)\\s-+\\(.\\)\\s-+")
1863 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1864 (defun gdb-info-breakpoints-custom ()
1865 (let ((flag) (bptno))
1866 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1867 (dolist (buffer (buffer-list))
1868 (with-current-buffer buffer
1869 (if (and (memq gud-minor-mode '(gdba gdbmi))
1870 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1871 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1872 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1874 (let ((buffer-read-only nil))
1875 (goto-char (point-min))
1876 (while (< (point) (- (point-max) 1))
1878 (if (looking-at gdb-breakpoint-regexp)
1880 (setq bptno (or (match-string 1) (match-string 2)))
1881 (setq flag (char-after (match-beginning 3)))
1882 (if (match-string 1)
1883 (setq gdb-parent-bptno-enabled (eq flag ?y)))
1884 (add-text-properties
1885 (match-beginning 3) (match-end 3)
1887 '(face font-lock-warning-face)
1888 '(face font-lock-type-face)))
1890 (el (line-end-position)))
1891 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1893 (add-text-properties
1894 (match-beginning 1) (match-end 1)
1895 '(face font-lock-function-name-face))
1896 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1897 (let ((line (match-string 2))
1898 (file (match-string 1)))
1899 (add-text-properties bl el
1900 '(mouse-face highlight
1901 help-echo "mouse-2, RET: visit breakpoint"))
1902 (unless (file-exists-p file)
1903 (setq file (cdr (assoc bptno gdb-location-alist))))
1905 (not (string-equal file "File not found")))
1906 (with-current-buffer
1907 (find-file-noselect file 'nowarn)
1908 (set (make-local-variable 'gud-minor-mode)
1910 (set (make-local-variable 'tool-bar-map)
1912 ;; Only want one breakpoint icon at each
1915 (goto-line (string-to-number line))
1916 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1919 (concat gdb-server-prefix "list "
1920 (match-string-no-properties 1) ":1\n")
1923 (list (concat gdb-server-prefix "info source\n")
1924 `(lambda () (gdb-get-location
1925 ,bptno ,line ,flag)))))))
1926 (if (re-search-forward
1927 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1929 (add-text-properties
1930 (match-beginning 1) (match-end 1)
1931 '(face font-lock-function-name-face))
1933 (re-search-backward "\\s-\\(\\S-*\\)"
1935 (add-text-properties
1936 (match-beginning 1) (match-end 1)
1937 '(face font-lock-variable-name-face)))))))
1939 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1941 (declare-function gud-remove "gdb-ui" t t) ; gud-def
1942 (declare-function gud-break "gdb-ui" t t) ; gud-def
1944 (defun gdb-mouse-set-clear-breakpoint (event)
1945 "Set/clear breakpoint in left fringe/margin with mouse click."
1947 (mouse-minibuffer-check event)
1948 (let ((posn (event-end event)))
1949 (if (numberp (posn-point posn))
1950 (with-selected-window (posn-window posn)
1952 (goto-char (posn-point posn))
1953 (if (or (posn-object posn)
1954 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1957 (gud-break nil)))))))
1959 (defun gdb-mouse-toggle-breakpoint-margin (event)
1960 "Enable/disable breakpoint in left margin with mouse click."
1962 (mouse-minibuffer-check event)
1963 (let ((posn (event-end event)))
1964 (if (numberp (posn-point posn))
1965 (with-selected-window (posn-window posn)
1967 (goto-char (posn-point posn))
1968 (if (posn-object posn)
1969 (let* ((bptno (get-text-property
1970 0 'gdb-bptno (car (posn-string posn)))))
1971 (string-match "\\([0-9+]\\)*" bptno)
1974 (concat gdb-server-prefix
1975 (if (get-text-property
1976 0 'gdb-enabled (car (posn-string posn)))
1979 (match-string 1 bptno) "\n")
1982 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1983 "Enable/disable breakpoint in left fringe with mouse click."
1985 (mouse-minibuffer-check event)
1986 (let* ((posn (event-end event))
1987 (pos (posn-point posn))
1990 (with-selected-window (posn-window posn)
1992 (set-buffer (window-buffer (selected-window)))
1994 (dolist (overlay (overlays-in pos pos))
1995 (when (overlay-get overlay 'put-break)
1996 (setq obj (overlay-get overlay 'before-string))))
1998 (let* ((bptno (get-text-property 0 'gdb-bptno obj)))
1999 (string-match "\\([0-9+]\\)*" bptno)
2002 (concat gdb-server-prefix
2003 (if (get-text-property 0 'gdb-enabled obj)
2006 (match-string 1 bptno) "\n")
2009 (defun gdb-breakpoints-buffer-name ()
2010 (with-current-buffer gud-comint-buffer
2011 (concat "*breakpoints of " (gdb-get-target-string) "*")))
2013 (defun gdb-display-breakpoints-buffer ()
2014 "Display status of user-settable breakpoints."
2017 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
2019 (defun gdb-frame-breakpoints-buffer ()
2020 "Display status of user-settable breakpoints in a new frame."
2022 (let ((special-display-regexps (append special-display-regexps '(".*")))
2023 (special-display-frame-alist gdb-frame-parameters))
2024 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
2026 (defvar gdb-breakpoints-mode-map
2027 (let ((map (make-sparse-keymap))
2028 (menu (make-sparse-keymap "Breakpoints")))
2029 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2030 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2031 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2032 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2033 (suppress-keymap map)
2034 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2035 (define-key map " " 'gdb-toggle-breakpoint)
2036 (define-key map "D" 'gdb-delete-breakpoint)
2037 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2038 (define-key map "q" 'gdb-delete-frame-or-window)
2039 (define-key map "\r" 'gdb-goto-breakpoint)
2040 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2041 (define-key map [follow-link] 'mouse-face)
2044 (defun gdb-delete-frame-or-window ()
2045 "Delete frame if there is only one window. Otherwise delete the window."
2047 (if (one-window-p) (delete-frame)
2050 (defun gdb-breakpoints-mode ()
2051 "Major mode for gdb breakpoints.
2053 \\{gdb-breakpoints-mode-map}"
2054 (kill-all-local-variables)
2055 (setq major-mode 'gdb-breakpoints-mode)
2056 (setq mode-name "Breakpoints")
2057 (use-local-map gdb-breakpoints-mode-map)
2058 (setq buffer-read-only t)
2059 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2060 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2061 'gdb-invalidate-breakpoints
2062 'gdbmi-invalidate-breakpoints))
2064 (defun gdb-toggle-breakpoint ()
2065 "Enable/disable breakpoint at current line."
2068 (beginning-of-line 1)
2069 (if (looking-at gdb-breakpoint-regexp)
2072 (concat gdb-server-prefix
2073 (if (eq ?y (char-after (match-beginning 3)))
2076 (or (match-string 1) (match-string 2)) "\n") 'ignore))
2077 (error "Not recognized as break/watchpoint line"))))
2079 (defun gdb-delete-breakpoint ()
2080 "Delete the breakpoint at current line."
2083 (beginning-of-line 1)
2084 (if (looking-at gdb-breakpoint-regexp)
2085 (if (match-string 1)
2088 (concat gdb-server-prefix "delete " (match-string 1) "\n")
2090 (message-box "This breakpoint cannot be deleted on its own."))
2091 (error "Not recognized as break/watchpoint line"))))
2093 (defun gdb-goto-breakpoint (&optional event)
2094 "Display the breakpoint location specified at current line."
2095 (interactive (list last-input-event))
2096 (if event (posn-set-point (event-end event)))
2098 (beginning-of-line 1)
2099 (if (looking-at "\\([0-9]+\\.?[0-9]*\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
2100 (let ((bptno (match-string 1))
2101 (file (match-string 2))
2102 (line (match-string 3)))
2103 (save-selected-window
2104 (let* ((buffer (find-file-noselect
2105 (if (file-exists-p file) file
2106 (cdr (assoc bptno gdb-location-alist)))))
2107 (window (or (gdb-display-source-buffer buffer)
2108 (display-buffer buffer))))
2109 (setq gdb-source-window window)
2110 (with-current-buffer buffer
2111 (goto-line (string-to-number line))
2112 (set-window-point window (point))))))
2113 (error "No location specified."))))
2116 ;; Frames buffer. This displays a perpetually correct bactracktrace
2117 ;; (from the command `where').
2119 ;; Alas, if your stack is deep, it is costly.
2121 (defcustom gdb-max-frames 40
2122 "Maximum number of frames displayed in call stack."
2127 (gdb-set-buffer-rules 'gdb-stack-buffer
2128 'gdb-stack-buffer-name
2131 (def-gdb-auto-updated-buffer gdb-stack-buffer
2132 gdb-invalidate-frames
2133 (concat "server info stack " (number-to-string gdb-max-frames) "\n")
2134 gdb-info-stack-handler
2135 gdb-info-stack-custom)
2137 (defun gdb-info-stack-custom ()
2138 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2141 (unless (eq gdb-look-up-stack 'delete)
2142 (let ((buffer-read-only nil)
2144 (goto-char (point-min))
2145 (while (< (point) (point-max))
2146 (setq bl (line-beginning-position)
2147 el (line-end-position))
2148 (when (looking-at "#")
2149 (add-text-properties bl el
2150 '(mouse-face highlight
2151 help-echo "mouse-2, RET: Select frame")))
2153 (when (looking-at "^#\\([0-9]+\\)")
2154 (when (string-equal (match-string 1) gdb-frame-number)
2155 (if (> (car (window-fringes)) 0)
2157 (or gdb-stack-position
2158 (setq gdb-stack-position (make-marker)))
2159 (set-marker gdb-stack-position (point))
2160 (setq move-to gdb-stack-position))
2161 (put-text-property bl (+ bl 4)
2162 'face '(:inverse-video t))
2164 (when (re-search-forward
2166 (if (string-equal (match-string 1) "0") "" " in ")
2167 "\\([^ ]+\\) (") el t)
2168 (put-text-property (match-beginning 1) (match-end 1)
2169 'face font-lock-function-name-face)
2170 (setq bl (match-end 0))
2171 (while (re-search-forward "<\\([^>]+\\)>" el t)
2172 (put-text-property (match-beginning 1) (match-end 1)
2173 'face font-lock-function-name-face))
2175 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2176 (put-text-property (match-beginning 1) (match-end 1)
2177 'face font-lock-variable-name-face))))
2180 (when (looking-at "(More stack frames follow...)")
2181 (add-text-properties (match-beginning 0) (match-end 0)
2182 '(mouse-face highlight
2185 "mouse-2, RET: customize gdb-max-frames to see more frames")))))
2186 (when gdb-look-up-stack
2187 (goto-char (point-min))
2188 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2189 (let ((start (line-beginning-position))
2190 (file (match-string 1))
2191 (line (match-string 2)))
2192 (re-search-backward "^#*\\([0-9]+\\)" start t)
2194 (list (concat gdb-server-prefix "frame "
2195 (match-string 1) "\n") 'gdb-set-hollow))
2197 (list (concat gdb-server-prefix "frame 0\n") 'ignore))))))
2199 (let ((window (get-buffer-window (current-buffer) 0)))
2201 (with-selected-window window
2203 (unless (pos-visible-in-window-p)
2204 (recenter '(center)))))))))
2205 (if (eq gdb-look-up-stack 'delete)
2206 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2207 (setq gdb-look-up-stack nil))
2209 (defun gdb-set-hollow ()
2210 (if gud-last-last-frame
2211 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2212 (setq fringe-indicator-alist
2213 '((overlay-arrow . hollow-right-triangle))))))
2215 (defun gdb-stack-buffer-name ()
2216 (with-current-buffer gud-comint-buffer
2217 (concat "*stack frames of " (gdb-get-target-string) "*")))
2219 (defun gdb-display-stack-buffer ()
2220 "Display backtrace of current stack."
2223 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2225 (defun gdb-frame-stack-buffer ()
2226 "Display backtrace of current stack in a new frame."
2228 (let ((special-display-regexps (append special-display-regexps '(".*")))
2229 (special-display-frame-alist gdb-frame-parameters))
2230 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2232 (defvar gdb-frames-mode-map
2233 (let ((map (make-sparse-keymap)))
2234 (suppress-keymap map)
2235 (define-key map "q" 'kill-this-buffer)
2236 (define-key map "\r" 'gdb-frames-select)
2237 (define-key map [mouse-2] 'gdb-frames-select)
2238 (define-key map [follow-link] 'mouse-face)
2241 (defun gdb-frames-mode ()
2242 "Major mode for gdb call stack.
2244 \\{gdb-frames-mode-map}"
2245 (kill-all-local-variables)
2246 (setq major-mode 'gdb-frames-mode)
2247 (setq mode-name "Frames")
2248 (setq gdb-stack-position nil)
2249 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2250 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2251 (setq buffer-read-only t)
2252 (use-local-map gdb-frames-mode-map)
2253 (run-mode-hooks 'gdb-frames-mode-hook)
2254 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2255 'gdb-invalidate-frames
2256 'gdbmi-invalidate-frames))
2258 (defun gdb-get-frame-number ()
2261 (let* ((start (line-beginning-position))
2262 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2263 (n (or (and pos (match-string 1)) "0")))
2266 (defun gdb-frames-select (&optional event)
2267 "Select the frame and display the relevant source."
2268 (interactive (list last-input-event))
2269 (if event (posn-set-point (event-end event)))
2270 (if (get-text-property (point) 'gdb-max-frames)
2272 (message-box "After setting gdb-max-frames, you need to enter\n\
2273 another GDB command e.g pwd, to see new frames")
2274 (customize-variable-other-window 'gdb-max-frames))
2276 (list (concat gdb-server-prefix "frame "
2277 (gdb-get-frame-number) "\n") 'ignore))))
2280 ;; Threads buffer. This displays a selectable thread list.
2282 (gdb-set-buffer-rules 'gdb-threads-buffer
2283 'gdb-threads-buffer-name
2286 (def-gdb-auto-updated-buffer gdb-threads-buffer
2287 gdb-invalidate-threads
2288 (concat gdb-server-prefix "info threads\n")
2289 gdb-info-threads-handler
2290 gdb-info-threads-custom)
2292 (defun gdb-info-threads-custom ()
2293 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2294 (let ((buffer-read-only nil))
2296 (goto-char (point-min))
2297 (while (< (point) (point-max))
2298 (unless (looking-at "No ")
2299 (add-text-properties (line-beginning-position) (line-end-position)
2300 '(mouse-face highlight
2301 help-echo "mouse-2, RET: select thread")))
2302 (forward-line 1))))))
2304 (defun gdb-threads-buffer-name ()
2305 (with-current-buffer gud-comint-buffer
2306 (concat "*threads of " (gdb-get-target-string) "*")))
2308 (defun gdb-display-threads-buffer ()
2309 "Display IDs of currently known threads."
2312 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2314 (defun gdb-frame-threads-buffer ()
2315 "Display IDs of currently known threads in a new frame."
2317 (let ((special-display-regexps (append special-display-regexps '(".*")))
2318 (special-display-frame-alist gdb-frame-parameters))
2319 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2321 (defvar gdb-threads-mode-map
2322 (let ((map (make-sparse-keymap)))
2323 (suppress-keymap map)
2324 (define-key map "q" 'kill-this-buffer)
2325 (define-key map "\r" 'gdb-threads-select)
2326 (define-key map [mouse-2] 'gdb-threads-select)
2327 (define-key map [follow-link] 'mouse-face)
2330 (defvar gdb-threads-font-lock-keywords
2331 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2332 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2333 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2334 "Font lock keywords used in `gdb-threads-mode'.")
2336 (defun gdb-threads-mode ()
2337 "Major mode for gdb threads.
2339 \\{gdb-threads-mode-map}"
2340 (kill-all-local-variables)
2341 (setq major-mode 'gdb-threads-mode)
2342 (setq mode-name "Threads")
2343 (setq buffer-read-only t)
2344 (use-local-map gdb-threads-mode-map)
2345 (set (make-local-variable 'font-lock-defaults)
2346 '(gdb-threads-font-lock-keywords))
2347 (run-mode-hooks 'gdb-threads-mode-hook)
2348 'gdb-invalidate-threads)
2350 (defun gdb-get-thread-number ()
2352 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2353 (match-string-no-properties 1)))
2355 (defun gdb-threads-select (&optional event)
2356 "Select the thread and display the relevant source."
2357 (interactive (list last-input-event))
2358 (if event (posn-set-point (event-end event)))
2360 (list (concat gdb-server-prefix "thread "
2361 (gdb-get-thread-number) "\n") 'ignore))
2362 (gud-display-frame))
2365 ;; Registers buffer.
2367 (defcustom gdb-all-registers nil
2368 "Non-nil means include floating-point registers."
2373 (gdb-set-buffer-rules 'gdb-registers-buffer
2374 'gdb-registers-buffer-name
2375 'gdb-registers-mode)
2377 (def-gdb-auto-updated-buffer gdb-registers-buffer
2378 gdb-invalidate-registers
2380 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2381 gdb-info-registers-handler
2382 gdb-info-registers-custom)
2384 (defun gdb-info-registers-custom ()
2385 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2387 (let ((buffer-read-only nil)
2389 (goto-char (point-min))
2390 (while (< (point) (point-max))
2391 (setq start (line-beginning-position))
2392 (setq end (line-end-position))
2393 (when (looking-at "^[^ ]+")
2394 (unless (string-equal (match-string 0) "The")
2395 (put-text-property start (match-end 0)
2396 'face font-lock-variable-name-face)
2397 (add-text-properties start end
2398 '(help-echo "mouse-2: edit value"
2399 mouse-face highlight))))
2400 (forward-line 1))))))
2402 (defun gdb-edit-register-value (&optional event)
2403 (interactive (list last-input-event))
2405 (if event (posn-set-point (event-end event)))
2407 (let* ((register (current-word))
2408 (value (read-string (format "New value (%s): " register))))
2410 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2413 (defvar gdb-registers-mode-map
2414 (let ((map (make-sparse-keymap)))
2415 (suppress-keymap map)
2416 (define-key map "\r" 'gdb-edit-register-value)
2417 (define-key map [mouse-2] 'gdb-edit-register-value)
2418 (define-key map " " 'gdb-all-registers)
2419 (define-key map "q" 'kill-this-buffer)
2422 (defun gdb-registers-mode ()
2423 "Major mode for gdb registers.
2425 \\{gdb-registers-mode-map}"
2426 (kill-all-local-variables)
2427 (setq major-mode 'gdb-registers-mode)
2428 (setq mode-name "Registers")
2429 (setq buffer-read-only t)
2430 (use-local-map gdb-registers-mode-map)
2431 (run-mode-hooks 'gdb-registers-mode-hook)
2432 (if (string-equal gdb-version "pre-6.4")
2434 (if gdb-all-registers (setq mode-name "Registers:All"))
2435 'gdb-invalidate-registers)
2436 'gdb-invalidate-registers-1))
2438 (defun gdb-registers-buffer-name ()
2439 (with-current-buffer gud-comint-buffer
2440 (concat "*registers of " (gdb-get-target-string) "*")))
2442 (defun gdb-display-registers-buffer ()
2443 "Display integer register contents."
2446 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2448 (defun gdb-frame-registers-buffer ()
2449 "Display integer register contents in a new frame."
2451 (let ((special-display-regexps (append special-display-regexps '(".*")))
2452 (special-display-frame-alist gdb-frame-parameters))
2453 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2455 (defun gdb-all-registers ()
2456 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2458 (when (string-equal gdb-version "pre-6.4")
2459 (if gdb-all-registers
2461 (setq gdb-all-registers nil)
2462 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2463 (setq mode-name "Registers")))
2464 (setq gdb-all-registers t)
2465 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2466 (setq mode-name "Registers:All")))
2467 (message (format "Display of floating-point registers %sabled"
2468 (if gdb-all-registers "en" "dis")))
2469 (gdb-invalidate-registers)))
2474 (defcustom gdb-memory-repeat-count 32
2475 "Number of data items in memory window."
2480 (defcustom gdb-memory-format "x"
2481 "Display format of data items in memory window."
2482 :type '(choice (const :tag "Hexadecimal" "x")
2483 (const :tag "Signed decimal" "d")
2484 (const :tag "Unsigned decimal" "u")
2485 (const :tag "Octal" "o")
2486 (const :tag "Binary" "t"))
2490 (defcustom gdb-memory-unit "w"
2491 "Unit size of data items in memory window."
2492 :type '(choice (const :tag "Byte" "b")
2493 (const :tag "Halfword" "h")
2494 (const :tag "Word" "w")
2495 (const :tag "Giant word" "g"))
2499 (gdb-set-buffer-rules 'gdb-memory-buffer
2500 'gdb-memory-buffer-name
2503 (def-gdb-auto-updated-buffer gdb-memory-buffer
2504 gdb-invalidate-memory
2505 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2506 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2507 gdb-read-memory-handler
2508 gdb-read-memory-custom)
2510 (defun gdb-read-memory-custom ()
2512 (goto-char (point-min))
2513 (if (looking-at "0x[[:xdigit:]]+")
2514 (setq gdb-memory-address (match-string 0)))))
2516 (defvar gdb-memory-mode-map
2517 (let ((map (make-sparse-keymap)))
2518 (suppress-keymap map)
2519 (define-key map "q" 'kill-this-buffer)
2522 (defun gdb-memory-set-address (event)
2523 "Set the start memory address."
2525 (save-selected-window
2526 (select-window (posn-window (event-start event)))
2527 (let ((arg (read-from-minibuffer "Memory address: ")))
2528 (setq gdb-memory-address arg))
2529 (gdb-invalidate-memory)))
2531 (defun gdb-memory-set-repeat-count (event)
2532 "Set the number of data items in memory window."
2534 (save-selected-window
2535 (select-window (posn-window (event-start event)))
2536 (let* ((arg (read-from-minibuffer "Repeat count: "))
2537 (count (string-to-number arg)))
2539 (error "Positive numbers only")
2540 (customize-set-variable 'gdb-memory-repeat-count count)
2541 (gdb-invalidate-memory)))))
2543 (defun gdb-memory-format-binary ()
2544 "Set the display format to binary."
2546 (customize-set-variable 'gdb-memory-format "t")
2547 (gdb-invalidate-memory))
2549 (defun gdb-memory-format-octal ()
2550 "Set the display format to octal."
2552 (customize-set-variable 'gdb-memory-format "o")
2553 (gdb-invalidate-memory))
2555 (defun gdb-memory-format-unsigned ()
2556 "Set the display format to unsigned decimal."
2558 (customize-set-variable 'gdb-memory-format "u")
2559 (gdb-invalidate-memory))
2561 (defun gdb-memory-format-signed ()
2562 "Set the display format to decimal."
2564 (customize-set-variable 'gdb-memory-format "d")
2565 (gdb-invalidate-memory))
2567 (defun gdb-memory-format-hexadecimal ()
2568 "Set the display format to hexadecimal."
2570 (customize-set-variable 'gdb-memory-format "x")
2571 (gdb-invalidate-memory))
2573 (defvar gdb-memory-format-map
2574 (let ((map (make-sparse-keymap)))
2575 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2577 "Keymap to select format in the header line.")
2579 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2580 "Menu of display formats in the header line.")
2582 (define-key gdb-memory-format-menu [binary]
2583 '(menu-item "Binary" gdb-memory-format-binary
2584 :button (:radio . (equal gdb-memory-format "t"))))
2585 (define-key gdb-memory-format-menu [octal]
2586 '(menu-item "Octal" gdb-memory-format-octal
2587 :button (:radio . (equal gdb-memory-format "o"))))
2588 (define-key gdb-memory-format-menu [unsigned]
2589 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2590 :button (:radio . (equal gdb-memory-format "u"))))
2591 (define-key gdb-memory-format-menu [signed]
2592 '(menu-item "Signed Decimal" gdb-memory-format-signed
2593 :button (:radio . (equal gdb-memory-format "d"))))
2594 (define-key gdb-memory-format-menu [hexadecimal]
2595 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2596 :button (:radio . (equal gdb-memory-format "x"))))
2598 (defun gdb-memory-format-menu (event)
2600 (x-popup-menu event gdb-memory-format-menu))
2602 (defun gdb-memory-format-menu-1 (event)
2604 (save-selected-window
2605 (select-window (posn-window (event-start event)))
2606 (let* ((selection (gdb-memory-format-menu event))
2607 (binding (and selection (lookup-key gdb-memory-format-menu
2608 (vector (car selection))))))
2609 (if binding (call-interactively binding)))))
2611 (defun gdb-memory-unit-giant ()
2612 "Set the unit size to giant words (eight bytes)."
2614 (customize-set-variable 'gdb-memory-unit "g")
2615 (gdb-invalidate-memory))
2617 (defun gdb-memory-unit-word ()
2618 "Set the unit size to words (four bytes)."
2620 (customize-set-variable 'gdb-memory-unit "w")
2621 (gdb-invalidate-memory))
2623 (defun gdb-memory-unit-halfword ()
2624 "Set the unit size to halfwords (two bytes)."
2626 (customize-set-variable 'gdb-memory-unit "h")
2627 (gdb-invalidate-memory))
2629 (defun gdb-memory-unit-byte ()
2630 "Set the unit size to bytes."
2632 (customize-set-variable 'gdb-memory-unit "b")
2633 (gdb-invalidate-memory))
2635 (defvar gdb-memory-unit-map
2636 (let ((map (make-sparse-keymap)))
2637 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2639 "Keymap to select units in the header line.")
2641 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2642 "Menu of units in the header line.")
2644 (define-key gdb-memory-unit-menu [giantwords]
2645 '(menu-item "Giant words" gdb-memory-unit-giant
2646 :button (:radio . (equal gdb-memory-unit "g"))))
2647 (define-key gdb-memory-unit-menu [words]
2648 '(menu-item "Words" gdb-memory-unit-word
2649 :button (:radio . (equal gdb-memory-unit "w"))))
2650 (define-key gdb-memory-unit-menu [halfwords]
2651 '(menu-item "Halfwords" gdb-memory-unit-halfword
2652 :button (:radio . (equal gdb-memory-unit "h"))))
2653 (define-key gdb-memory-unit-menu [bytes]
2654 '(menu-item "Bytes" gdb-memory-unit-byte
2655 :button (:radio . (equal gdb-memory-unit "b"))))
2657 (defun gdb-memory-unit-menu (event)
2659 (x-popup-menu event gdb-memory-unit-menu))
2661 (defun gdb-memory-unit-menu-1 (event)
2663 (save-selected-window
2664 (select-window (posn-window (event-start event)))
2665 (let* ((selection (gdb-memory-unit-menu event))
2666 (binding (and selection (lookup-key gdb-memory-unit-menu
2667 (vector (car selection))))))
2668 (if binding (call-interactively binding)))))
2670 ;;from make-mode-line-mouse-map
2671 (defun gdb-make-header-line-mouse-map (mouse function) "\
2672 Return a keymap with single entry for mouse key MOUSE on the header line.
2673 MOUSE is defined to run function FUNCTION with no args in the buffer
2674 corresponding to the mode line clicked."
2675 (let ((map (make-sparse-keymap)))
2676 (define-key map (vector 'header-line mouse) function)
2677 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2680 (defvar gdb-memory-font-lock-keywords
2681 '(;; <__function.name+n>
2682 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2684 "Font lock keywords used in `gdb-memory-mode'.")
2686 (defun gdb-memory-mode ()
2687 "Major mode for examining memory.
2689 \\{gdb-memory-mode-map}"
2690 (kill-all-local-variables)
2691 (setq major-mode 'gdb-memory-mode)
2692 (setq mode-name "Memory")
2693 (setq buffer-read-only t)
2694 (use-local-map gdb-memory-mode-map)
2695 (setq header-line-format
2701 'face font-lock-warning-face
2702 'help-echo "mouse-1: decrement address"
2703 'mouse-face 'mode-line-highlight
2705 (gdb-make-header-line-mouse-map
2707 (lambda () (interactive)
2708 (let ((gdb-memory-address
2709 ;; Let GDB do the arithmetic.
2711 gdb-memory-address " - "
2713 (* gdb-memory-repeat-count
2714 (cond ((string= gdb-memory-unit "b") 1)
2715 ((string= gdb-memory-unit "h") 2)
2716 ((string= gdb-memory-unit "w") 4)
2717 ((string= gdb-memory-unit "g") 8)))))))
2718 (gdb-invalidate-memory)))))
2721 'face font-lock-warning-face
2722 'help-echo "mouse-1: increment address"
2723 'mouse-face 'mode-line-highlight
2724 'local-map (gdb-make-header-line-mouse-map
2726 (lambda () (interactive)
2727 (let ((gdb-memory-address nil))
2728 (gdb-invalidate-memory)))))
2730 (propertize gdb-memory-address
2731 'face font-lock-warning-face
2732 'help-echo "mouse-1: set memory address"
2733 'mouse-face 'mode-line-highlight
2734 'local-map (gdb-make-header-line-mouse-map
2736 #'gdb-memory-set-address))
2738 (propertize (number-to-string gdb-memory-repeat-count)
2739 'face font-lock-warning-face
2740 'help-echo "mouse-1: set repeat count"
2741 'mouse-face 'mode-line-highlight
2742 'local-map (gdb-make-header-line-mouse-map
2744 #'gdb-memory-set-repeat-count))
2746 (propertize gdb-memory-format
2747 'face font-lock-warning-face
2748 'help-echo "mouse-3: select display format"
2749 'mouse-face 'mode-line-highlight
2750 'local-map gdb-memory-format-map)
2752 (propertize gdb-memory-unit
2753 'face font-lock-warning-face
2754 'help-echo "mouse-3: select unit size"
2755 'mouse-face 'mode-line-highlight
2756 'local-map gdb-memory-unit-map))))
2757 (set (make-local-variable 'font-lock-defaults)
2758 '(gdb-memory-font-lock-keywords))
2759 (run-mode-hooks 'gdb-memory-mode-hook)
2760 'gdb-invalidate-memory)
2762 (defun gdb-memory-buffer-name ()
2763 (with-current-buffer gud-comint-buffer
2764 (concat "*memory of " (gdb-get-target-string) "*")))
2766 (defun gdb-display-memory-buffer ()
2767 "Display memory contents."
2770 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2772 (defun gdb-frame-memory-buffer ()
2773 "Display memory contents in a new frame."
2775 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2776 (special-display-frame-alist
2777 (cons '(left-fringe . 0)
2778 (cons '(right-fringe . 0)
2779 (cons '(width . 83) gdb-frame-parameters)))))
2780 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2785 (gdb-set-buffer-rules 'gdb-locals-buffer
2786 'gdb-locals-buffer-name
2789 (def-gdb-auto-update-trigger gdb-invalidate-locals
2790 (gdb-get-buffer 'gdb-locals-buffer)
2791 "server info locals\n"
2792 gdb-info-locals-handler)
2794 (defvar gdb-locals-watch-map
2795 (let ((map (make-sparse-keymap)))
2796 (suppress-keymap map)
2797 (define-key map "\r" (lambda () (interactive)
2800 (define-key map [mouse-2] (lambda (event) (interactive "e")
2801 (mouse-set-point event)
2805 "Keymap to create watch expression of a complex data type local variable.")
2807 (defconst gdb-struct-string
2808 (concat (propertize "[struct/union]"
2809 'mouse-face 'highlight
2810 'help-echo "mouse-2: create watch expression"
2811 'local-map gdb-locals-watch-map) "\n"))
2813 (defconst gdb-array-string
2814 (concat " " (propertize "[array]"
2815 'mouse-face 'highlight
2816 'help-echo "mouse-2: create watch expression"
2817 'local-map gdb-locals-watch-map) "\n"))
2819 ;; Abbreviate for arrays and structures.
2820 ;; These can be expanded using gud-display.
2821 (defun gdb-info-locals-handler ()
2822 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2823 gdb-pending-triggers))
2824 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2825 (with-current-buffer buf
2826 (goto-char (point-min))
2827 (while (re-search-forward "^[ }].*\n" nil t)
2828 (replace-match "" nil nil))
2829 (goto-char (point-min))
2830 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2831 (replace-match gdb-struct-string nil nil))
2832 (goto-char (point-min))
2833 (while (re-search-forward "\\s-*{.*\n" nil t)
2834 (replace-match gdb-array-string nil nil))))
2835 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2837 (with-current-buffer buf
2838 (let* ((window (get-buffer-window buf 0))
2839 (start (window-start window))
2840 (p (window-point window))
2841 (buffer-read-only nil))
2843 (insert-buffer-substring (gdb-get-buffer-create
2844 'gdb-partial-output-buffer))
2845 (set-window-start window start)
2846 (set-window-point window p))
2848 (run-hooks 'gdb-info-locals-hook))
2850 (defvar gdb-locals-mode-map
2851 (let ((map (make-sparse-keymap)))
2852 (suppress-keymap map)
2853 (define-key map "q" 'kill-this-buffer)
2856 (defun gdb-locals-mode ()
2857 "Major mode for gdb locals.
2859 \\{gdb-locals-mode-map}"
2860 (kill-all-local-variables)
2861 (setq major-mode 'gdb-locals-mode)
2862 (setq mode-name (concat "Locals:" gdb-selected-frame))
2863 (setq buffer-read-only t)
2864 (use-local-map gdb-locals-mode-map)
2865 (set (make-local-variable 'font-lock-defaults)
2866 '(gdb-locals-font-lock-keywords))
2867 (run-mode-hooks 'gdb-locals-mode-hook)
2868 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2869 (string-equal gdb-version "pre-6.4"))
2870 'gdb-invalidate-locals
2871 'gdb-invalidate-locals-1))
2873 (defun gdb-locals-buffer-name ()
2874 (with-current-buffer gud-comint-buffer
2875 (concat "*locals of " (gdb-get-target-string) "*")))
2877 (defun gdb-display-locals-buffer ()
2878 "Display local variables of current stack and their values."
2881 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2883 (defun gdb-frame-locals-buffer ()
2884 "Display local variables of current stack and their values in a new frame."
2886 (let ((special-display-regexps (append special-display-regexps '(".*")))
2887 (special-display-frame-alist gdb-frame-parameters))
2888 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2891 ;;;; Window management
2892 (defun gdb-display-buffer (buf dedicated &optional size)
2893 (let ((answer (get-buffer-window buf 0))
2896 (display-buffer buf nil 0) ;Deiconify the frame if necessary.
2897 ;; The buffer is not yet displayed.
2898 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2899 (let ((window (get-lru-window)))
2901 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2902 ,gdb-source-window))))
2904 (set-window-buffer window buf)
2905 (setq answer window))
2906 (setq must-split t)))
2908 (let* ((largest (get-largest-window))
2909 (cur-size (window-height largest))
2910 (new-size (and size (< size cur-size) (- cur-size size))))
2911 (setq answer (split-window largest new-size))
2912 (set-window-buffer answer buf)
2913 (set-window-dedicated-p answer dedicated)))
2917 ;;; Shared keymap initialization:
2919 (let ((menu (make-sparse-keymap "GDB-Windows")))
2920 (define-key gud-menu-map [displays]
2921 `(menu-item "GDB-Windows" ,menu
2922 :visible (memq gud-minor-mode '(gdbmi gdba))))
2923 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2924 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2925 (define-key menu [inferior]
2926 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2927 :enable gdb-use-separate-io-buffer))
2928 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2929 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2930 (define-key menu [disassembly]
2931 '("Disassembly" . gdb-display-assembler-buffer))
2932 (define-key menu [breakpoints]
2933 '("Breakpoints" . gdb-display-breakpoints-buffer))
2934 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2935 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2937 (let ((menu (make-sparse-keymap "GDB-Frames")))
2938 (define-key gud-menu-map [frames]
2939 `(menu-item "GDB-Frames" ,menu
2940 :visible (memq gud-minor-mode '(gdbmi gdba))))
2941 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2942 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2943 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2944 (define-key menu [inferior]
2945 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2946 :enable gdb-use-separate-io-buffer))
2947 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2948 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2949 (define-key menu [breakpoints]
2950 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2951 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2952 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2954 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2955 (define-key gud-menu-map [ui]
2956 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2957 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2958 (define-key menu [gdb-find-source-frame]
2959 '(menu-item "Look For Source Frame" gdb-find-source-frame
2960 :visible (eq gud-minor-mode 'gdba)
2961 :help "Toggle look for source frame."
2962 :button (:toggle . gdb-find-source-frame)))
2963 (define-key menu [gdb-use-separate-io]
2964 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2965 :visible (eq gud-minor-mode 'gdba)
2966 :help "Toggle separate IO for debugged program."
2967 :button (:toggle . gdb-use-separate-io-buffer)))
2968 (define-key menu [gdb-many-windows]
2969 '(menu-item "Display Other Windows" gdb-many-windows
2970 :help "Toggle display of locals, stack and breakpoint information"
2971 :button (:toggle . gdb-many-windows)))
2972 (define-key menu [gdb-restore-windows]
2973 '(menu-item "Restore Window Layout" gdb-restore-windows
2974 :help "Restore standard layout for debug session.")))
2976 (defun gdb-frame-gdb-buffer ()
2977 "Display GUD buffer in a new frame."
2979 (let ((special-display-regexps (append special-display-regexps '(".*")))
2980 (special-display-frame-alist
2981 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2982 gdb-frame-parameters)))
2983 (same-window-regexps nil))
2984 (display-buffer gud-comint-buffer)))
2986 (defun gdb-display-gdb-buffer ()
2987 "Display GUD buffer."
2989 (let ((same-window-regexps nil))
2990 (pop-to-buffer gud-comint-buffer)))
2992 (defun gdb-set-window-buffer (name)
2993 (set-window-buffer (selected-window) (get-buffer name))
2994 (set-window-dedicated-p (selected-window) t))
2996 (defun gdb-setup-windows ()
2997 "Layout the window pattern for `gdb-many-windows'."
2998 (gdb-display-locals-buffer)
2999 (gdb-display-stack-buffer)
3000 (delete-other-windows)
3001 (gdb-display-breakpoints-buffer)
3002 (delete-other-windows)
3004 (pop-to-buffer gud-comint-buffer)
3005 (split-window nil ( / ( * (window-height) 3) 4))
3006 (split-window nil ( / (window-height) 3))
3007 (split-window-horizontally)
3009 (gdb-set-window-buffer (gdb-locals-buffer-name))
3012 (if gud-last-last-frame
3013 (gud-find-file (car gud-last-last-frame))
3015 (gud-find-file gdb-main-file)
3016 ;; Put buffer list in window if we
3017 ;; can't find a source file.
3018 (list-buffers-noselect))))
3019 (setq gdb-source-window (selected-window))
3020 (when gdb-use-separate-io-buffer
3021 (split-window-horizontally)
3023 (gdb-set-window-buffer
3024 (gdb-get-buffer-create 'gdb-inferior-io)))
3026 (gdb-set-window-buffer (gdb-stack-buffer-name))
3027 (split-window-horizontally)
3029 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3032 (defun gdb-restore-windows ()
3033 "Restore the basic arrangement of windows used by gdba.
3034 This arrangement depends on the value of `gdb-many-windows'."
3036 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3037 (delete-other-windows)
3038 (if gdb-many-windows
3040 (when (or gud-last-last-frame gdb-show-main)
3044 (if gud-last-last-frame
3045 (gud-find-file (car gud-last-last-frame))
3046 (gud-find-file gdb-main-file)))
3047 (setq gdb-source-window (selected-window))
3051 "Exit a debugging session cleanly.
3052 Kills the gdb buffers, and resets variables and the source buffers."
3053 (dolist (buffer (buffer-list))
3054 (unless (eq buffer gud-comint-buffer)
3055 (with-current-buffer buffer
3056 (if (memq gud-minor-mode '(gdbmi gdba))
3057 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3059 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3060 (setq gud-minor-mode nil)
3061 (kill-local-variable 'tool-bar-map)
3062 (kill-local-variable 'gdb-define-alist))))))
3063 (setq gdb-overlay-arrow-position nil)
3064 (setq overlay-arrow-variable-list
3065 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3066 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3067 (setq gdb-stack-position nil)
3068 (setq overlay-arrow-variable-list
3069 (delq 'gdb-stack-position overlay-arrow-variable-list))
3070 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3071 (setq gud-running nil)
3072 (setq gdb-active-process nil)
3073 (setq gdb-var-list nil)
3074 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3076 (defun gdb-source-info ()
3077 "Find the source file where the program starts and displays it with related
3079 (goto-char (point-min))
3080 (if (and (search-forward "Located in " nil t)
3081 (looking-at "\\S-+"))
3082 (setq gdb-main-file (match-string 0)))
3083 (goto-char (point-min))
3084 (if (search-forward "Includes preprocessor macro info." nil t)
3085 (setq gdb-macro-info t))
3086 (if gdb-many-windows
3088 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3090 (let ((pop-up-windows t))
3091 (display-buffer (gud-find-file gdb-main-file)))))
3094 (defun gdb-get-location (bptno line flag)
3095 "Find the directory containing the relevant source file.
3096 Put in buffer and place breakpoint icon."
3097 (goto-char (point-min))
3098 (catch 'file-not-found
3099 (if (search-forward "Located in " nil t)
3100 (when (looking-at "\\S-+")
3101 (delete (cons bptno "File not found") gdb-location-alist)
3102 (push (cons bptno (match-string 0)) gdb-location-alist))
3104 (unless (assoc bptno gdb-location-alist)
3105 (push (cons bptno "File not found") gdb-location-alist)
3106 (message-box "Cannot find source file for breakpoint location.\n\
3107 Add directory to search path for source files using the GDB command, dir."))
3108 (throw 'file-not-found nil))
3109 (with-current-buffer
3110 (find-file-noselect (match-string 0))
3111 (save-current-buffer
3112 (set (make-local-variable 'gud-minor-mode) 'gdba)
3113 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
3114 ;; only want one breakpoint icon at each location
3116 (goto-line (string-to-number line))
3117 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
3119 (add-hook 'find-file-hook 'gdb-find-file-hook)
3121 (defun gdb-find-file-hook ()
3122 "Set up buffer for debugging if file is part of the source code
3123 of the current session."
3124 (if (and (buffer-name gud-comint-buffer)
3125 ;; in case gud or gdb-ui is just loaded
3127 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3129 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
3130 (if (member (if (string-equal gdb-version "pre-6.4")
3131 (file-name-nondirectory buffer-file-name)
3133 gdb-source-file-list)
3134 (with-current-buffer (find-buffer-visiting buffer-file-name)
3135 (set (make-local-variable 'gud-minor-mode)
3136 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3137 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3140 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3141 "Put string PUTSTRING in front of POS in the current buffer.
3142 PUTSTRING is displayed by putting an overlay into the current buffer with a
3143 `before-string' string that has a `display' property whose value is
3145 (let ((string (make-string 1 ?x))
3146 (buffer (current-buffer)))
3147 (setq putstring (copy-sequence putstring))
3148 (let ((overlay (make-overlay pos pos buffer))
3150 (list (list 'margin 'left-margin) putstring))))
3151 (put-text-property 0 1 'display prop string)
3153 (add-text-properties 0 1 sprops string))
3154 (overlay-put overlay 'put-break t)
3155 (overlay-put overlay 'before-string string))))
3157 ;;from remove-images
3158 (defun gdb-remove-strings (start end &optional buffer)
3159 "Remove strings between START and END in BUFFER.
3160 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3161 BUFFER nil or omitted means use the current buffer."
3163 (setq buffer (current-buffer)))
3164 (dolist (overlay (overlays-in start end))
3165 (when (overlay-get overlay 'put-break)
3166 (delete-overlay overlay))))
3168 (defun gdb-put-breakpoint-icon (enabled bptno)
3169 (if (string-match "[0-9+]+\\." bptno)
3170 (setq enabled gdb-parent-bptno-enabled))
3171 (let ((start (- (line-beginning-position) 1))
3172 (end (+ (line-end-position) 1))
3173 (putstring (if enabled "B" "b"))
3174 (source-window (get-buffer-window (current-buffer) 0)))
3175 (add-text-properties
3176 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3179 (add-text-properties
3180 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3181 (add-text-properties
3182 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3183 (gdb-remove-breakpoint-icons start end)
3184 (if (display-images-p)
3185 (if (>= (or left-fringe-width
3186 (if source-window (car (window-fringes source-window)))
3187 gdb-buffer-fringe-width) 8)
3190 `(left-fringe breakpoint
3193 'breakpoint-disabled))
3195 'gdb-enabled enabled)
3196 (when (< left-margin-width 2)
3197 (save-current-buffer
3198 (setq left-margin-width 2)
3202 left-margin-width right-margin-width))))
3205 (or breakpoint-enabled-icon
3206 (setq breakpoint-enabled-icon
3207 (find-image `((:type xpm :data
3208 ,breakpoint-xpm-data
3209 :ascent 100 :pointer hand)
3211 ,breakpoint-enabled-pbm-data
3212 :ascent 100 :pointer hand)))))
3213 (or breakpoint-disabled-icon
3214 (setq breakpoint-disabled-icon
3215 (find-image `((:type xpm :data
3216 ,breakpoint-xpm-data
3217 :conversion disabled
3218 :ascent 100 :pointer hand)
3220 ,breakpoint-disabled-pbm-data
3221 :ascent 100 :pointer hand))))))
3225 (when (< left-margin-width 2)
3226 (save-current-buffer
3227 (setq left-margin-width 2)
3228 (let ((window (get-buffer-window (current-buffer) 0)))
3231 window left-margin-width right-margin-width)))))
3233 (propertize putstring
3234 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3237 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3238 (gdb-remove-strings start end)
3239 (if (display-images-p)
3240 (remove-images start end))
3242 (setq left-margin-width 0)
3243 (let ((window (get-buffer-window (current-buffer) 0)))
3246 window left-margin-width right-margin-width)))))
3250 ;; Assembler buffer.
3252 (gdb-set-buffer-rules 'gdb-assembler-buffer
3253 'gdb-assembler-buffer-name
3254 'gdb-assembler-mode)
3256 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3257 ;; window-start but keep the overlay arrow/current line visible.
3258 (defun gdb-assembler-handler ()
3259 (setq gdb-pending-triggers
3260 (delq 'gdb-invalidate-assembler
3261 gdb-pending-triggers))
3262 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3264 (with-current-buffer buf
3265 (let* ((window (get-buffer-window buf 0))
3266 (p (window-point window))
3267 (buffer-read-only nil))
3269 (insert-buffer-substring (gdb-get-buffer-create
3270 'gdb-partial-output-buffer))
3271 (set-window-point window p)))))
3272 ;; put customisation here
3273 (gdb-assembler-custom))
3275 (defun gdb-assembler-custom ()
3276 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3277 (pos 1) (address) (flag) (bptno))
3278 (with-current-buffer buffer
3280 (if (not (equal gdb-pc-address "main"))
3282 (goto-char (point-min))
3283 (if (and gdb-pc-address
3284 (search-forward gdb-pc-address nil t))
3288 (setq fringe-indicator-alist
3289 (if (string-equal gdb-frame-number "0")
3291 '((overlay-arrow . hollow-right-triangle))))
3292 (or gdb-overlay-arrow-position
3293 (setq gdb-overlay-arrow-position (make-marker)))
3294 (set-marker gdb-overlay-arrow-position (point))))))
3295 ;; remove all breakpoint-icons in assembler buffer before updating.
3296 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3297 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3298 (goto-char (point-min))
3299 (while (< (point) (- (point-max) 1))
3302 "\\([0-9]+\\.?[0-9]*\\).*?\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3303 (setq bptno (match-string 1))
3304 (setq flag (char-after (match-beginning 2)))
3305 (setq address (match-string 3))
3306 (with-current-buffer buffer
3308 (goto-char (point-min))
3309 (if (search-forward address nil t)
3310 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
3311 (if (not (equal gdb-pc-address "main"))
3312 (with-current-buffer buffer
3313 (set-window-point (get-buffer-window buffer 0) pos)))))
3315 (defvar gdb-assembler-mode-map
3316 (let ((map (make-sparse-keymap)))
3317 (suppress-keymap map)
3318 (define-key map "q" 'kill-this-buffer)
3321 (defvar gdb-assembler-font-lock-keywords
3322 '(;; <__function.name+n>
3323 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3324 (1 font-lock-function-name-face))
3325 ;; 0xNNNNNNNN <__function.name+n>: opcode
3326 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3327 (4 font-lock-keyword-face))
3328 ;; %register(at least i386)
3329 ("%\\sw+" . font-lock-variable-name-face)
3330 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3331 (1 font-lock-comment-face)
3332 (2 font-lock-function-name-face))
3333 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3334 "Font lock keywords used in `gdb-assembler-mode'.")
3336 (defun gdb-assembler-mode ()
3337 "Major mode for viewing code assembler.
3339 \\{gdb-assembler-mode-map}"
3340 (kill-all-local-variables)
3341 (setq major-mode 'gdb-assembler-mode)
3342 (setq mode-name (concat "Machine:" gdb-selected-frame))
3343 (setq gdb-overlay-arrow-position nil)
3344 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3345 (setq fringes-outside-margins t)
3346 (setq buffer-read-only t)
3347 (use-local-map gdb-assembler-mode-map)
3348 (gdb-invalidate-assembler)
3349 (set (make-local-variable 'font-lock-defaults)
3350 '(gdb-assembler-font-lock-keywords))
3351 (run-mode-hooks 'gdb-assembler-mode-hook)
3352 'gdb-invalidate-assembler)
3354 (defun gdb-assembler-buffer-name ()
3355 (with-current-buffer gud-comint-buffer
3356 (concat "*disassembly of " (gdb-get-target-string) "*")))
3358 (defun gdb-display-assembler-buffer ()
3359 "Display disassembly view."
3361 (setq gdb-previous-frame nil)
3363 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3365 (defun gdb-frame-assembler-buffer ()
3366 "Display disassembly view in a new frame."
3368 (setq gdb-previous-frame nil)
3369 (let ((special-display-regexps (append special-display-regexps '(".*")))
3370 (special-display-frame-alist gdb-frame-parameters))
3371 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3373 ;; modified because if gdb-pc-address has changed value a new command
3374 ;; must be enqueued to update the buffer with the new output
3375 (defun gdb-invalidate-assembler (&optional ignored)
3376 (if (gdb-get-buffer 'gdb-assembler-buffer)
3378 (unless (and gdb-selected-frame
3379 (string-equal gdb-selected-frame gdb-previous-frame))
3380 (if (or (not (member 'gdb-invalidate-assembler
3381 gdb-pending-triggers))
3382 (not (string-equal gdb-pc-address
3383 gdb-previous-frame-address)))
3385 ;; take previous disassemble command, if any, off the queue
3386 (with-current-buffer gud-comint-buffer
3387 (let ((queue gdb-input-queue))
3388 (dolist (item queue)
3389 (if (equal (cdr item) '(gdb-assembler-handler))
3390 (setq gdb-input-queue
3391 (delete item gdb-input-queue))))))
3394 (concat gdb-server-prefix "disassemble "
3395 (if (member gdb-pc-address '(nil "main")) nil "0x")
3396 gdb-pc-address "\n")
3397 'gdb-assembler-handler))
3398 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3399 (setq gdb-previous-frame-address gdb-pc-address)
3400 (setq gdb-previous-frame gdb-selected-frame)))))))
3402 (defun gdb-get-selected-frame ()
3403 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3406 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3407 (push 'gdb-get-selected-frame
3408 gdb-pending-triggers))))
3410 (defun gdb-frame-handler ()
3411 (setq gdb-pending-triggers
3412 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3413 (goto-char (point-min))
3414 (when (re-search-forward
3415 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3416 (setq gdb-frame-number (match-string 1))
3417 (setq gdb-frame-address (match-string 2)))
3418 (goto-char (point-min))
3419 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3420 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3422 (setq gdb-selected-frame (match-string 2))
3423 (if (gdb-get-buffer 'gdb-locals-buffer)
3424 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3425 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3426 (if (gdb-get-buffer 'gdb-assembler-buffer)
3427 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3428 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3429 (setq gdb-pc-address (match-string 1))
3430 (if (and (match-string 3) gud-overlay-arrow-position)
3431 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3432 (position (marker-position gud-overlay-arrow-position)))
3434 (string-equal (buffer-name buffer)
3435 (file-name-nondirectory (match-string 3))))
3436 (with-current-buffer buffer
3437 (setq fringe-indicator-alist
3438 (if (string-equal gdb-frame-number "0")
3440 '((overlay-arrow . hollow-right-triangle))))
3441 (set-marker gud-overlay-arrow-position position))))))
3442 (goto-char (point-min))
3443 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3444 (setq gdb-current-language (match-string 1)))
3445 (gdb-invalidate-assembler))
3448 ;; Code specific to GDB 6.4
3449 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3451 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3452 "Create list of source files for current GDB session.
3453 If buffers already exist for any of these files, gud-minor-mode
3455 (goto-char (point-min))
3456 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3457 (push (match-string 1) gdb-source-file-list))
3458 (dolist (buffer (buffer-list))
3459 (with-current-buffer buffer
3460 (when (member buffer-file-name gdb-source-file-list)
3461 (set (make-local-variable 'gud-minor-mode)
3462 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3463 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3464 (when gud-tooltip-mode
3465 (make-local-variable 'gdb-define-alist)
3466 (gdb-create-define-alist)
3467 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3468 (gdb-force-mode-line-update
3469 (propertize "ready" 'face font-lock-variable-name-face)))
3471 ; Uses "-var-list-children --all-values". Needs GDB 6.4 onwards.
3472 (defun gdb-var-list-children-1 (varnum)
3475 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3476 (concat "server interpreter mi \"-var-list-children --all-values "
3478 (concat "-var-list-children --all-values " varnum "\n"))
3479 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3481 (defconst gdb-var-list-children-regexp-1
3482 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3483 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3484 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3486 (defun gdb-var-list-children-handler-1 (varnum)
3487 (goto-char (point-min))
3488 (let ((var-list nil))
3489 (catch 'child-already-watched
3490 (dolist (var gdb-var-list)
3491 (if (string-equal varnum (car var))
3494 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3495 (let ((varchild (list (match-string 1)
3499 (read (match-string 4))
3501 (if (assoc (car varchild) gdb-var-list)
3502 (throw 'child-already-watched nil))
3503 (push varchild var-list))))
3504 (push var var-list)))
3505 (setq gdb-var-list (nreverse var-list))))
3506 (gdb-speedbar-update))
3508 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3509 (defun gdb-var-update-1 ()
3510 (if (not (member 'gdb-var-update gdb-pending-triggers))
3514 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3515 "server interpreter mi \"-var-update --all-values *\"\n"
3516 "-var-update --all-values *\n")
3517 'gdb-var-update-handler-1))
3518 (push 'gdb-var-update gdb-pending-triggers))))
3520 (defconst gdb-var-update-regexp-1
3521 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3522 in_scope=\"\\(.*?\\)\".*?}")
3524 (defun gdb-var-update-handler-1 ()
3525 (dolist (var gdb-var-list)
3526 (setcar (nthcdr 5 var) nil))
3527 (goto-char (point-min))
3528 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3529 (let* ((varnum (match-string 1))
3530 (var (assoc varnum gdb-var-list)))
3532 (let ((match (match-string 3)))
3533 (cond ((string-equal match "false")
3534 (if gdb-delete-out-of-scope
3535 (gdb-var-delete-1 varnum)
3536 (setcar (nthcdr 5 var) 'out-of-scope)))
3537 ((string-equal match "true")
3538 (setcar (nthcdr 5 var) 'changed)
3539 (setcar (nthcdr 4 var)
3540 (read (match-string 2))))
3541 ((string-equal match "invalid")
3542 (gdb-var-delete-1 varnum)))))))
3543 (setq gdb-pending-triggers
3544 (delq 'gdb-var-update gdb-pending-triggers))
3545 (gdb-speedbar-update))
3547 ;; Registers buffer.
3549 (gdb-set-buffer-rules 'gdb-registers-buffer
3550 'gdb-registers-buffer-name
3551 'gdb-registers-mode)
3553 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3554 (gdb-get-buffer 'gdb-registers-buffer)
3555 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3556 "server interpreter mi \"-data-list-register-values x\"\n"
3557 "-data-list-register-values x\n")
3558 gdb-data-list-register-values-handler)
3560 (defconst gdb-data-list-register-values-regexp
3561 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3563 (defun gdb-data-list-register-values-handler ()
3564 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3565 gdb-pending-triggers))
3566 (goto-char (point-min))
3567 (if (re-search-forward gdb-error-regexp nil t)
3568 (let ((err (match-string 1)))
3569 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3570 (let ((buffer-read-only nil))
3572 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3574 (goto-char (point-min)))))
3575 (let ((register-list (reverse gdb-register-names))
3576 (register nil) (register-string nil) (register-values nil))
3577 (goto-char (point-min))
3578 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3579 (setq register (pop register-list))
3580 (setq register-string (concat register "\t" (match-string 2) "\n"))
3581 (if (member (match-string 1) gdb-changed-registers)
3582 (put-text-property 0 (length register-string)
3583 'face 'font-lock-warning-face
3585 (setq register-values
3586 (concat register-values register-string)))
3587 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3588 (with-current-buffer buf
3589 (let* ((window (get-buffer-window buf 0))
3590 (start (window-start window))
3591 (p (window-point window))
3592 (buffer-read-only nil))
3594 (insert register-values)
3595 (set-window-start window start)
3596 (set-window-point window p))))))
3597 (gdb-data-list-register-values-custom))
3599 (defun gdb-data-list-register-values-custom ()
3600 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3602 (let ((buffer-read-only nil)
3604 (goto-char (point-min))
3605 (while (< (point) (point-max))
3606 (setq start (line-beginning-position))
3607 (setq end (line-end-position))
3608 (when (looking-at "^[^\t]+")
3609 (unless (string-equal (match-string 0) "No registers.")
3610 (put-text-property start (match-end 0)
3611 'face font-lock-variable-name-face)
3612 (add-text-properties start end
3613 '(help-echo "mouse-2: edit value"
3614 mouse-face highlight))))
3615 (forward-line 1))))))
3617 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3618 (defun gdb-get-changed-registers ()
3619 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3620 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3624 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3625 "server interpreter mi -data-list-changed-registers\n"
3626 "-data-list-changed-registers\n")
3627 'gdb-get-changed-registers-handler))
3628 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3630 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3632 (defun gdb-get-changed-registers-handler ()
3633 (setq gdb-pending-triggers
3634 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3635 (setq gdb-changed-registers nil)
3636 (goto-char (point-min))
3637 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3638 (push (match-string 1) gdb-changed-registers)))
3643 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3644 (gdb-set-buffer-rules 'gdb-locals-buffer
3645 'gdb-locals-buffer-name
3648 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3649 (gdb-get-buffer 'gdb-locals-buffer)
3650 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3651 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3652 "-stack-list-locals --simple-values\n")
3653 gdb-stack-list-locals-handler)
3655 (defconst gdb-stack-list-locals-regexp
3656 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3658 (defvar gdb-locals-watch-map-1
3659 (let ((map (make-sparse-keymap)))
3660 (suppress-keymap map)
3661 (define-key map "\r" 'gud-watch)
3662 (define-key map [mouse-2] 'gud-watch)
3664 "Keymap to create watch expression of a complex data type local variable.")
3666 (defvar gdb-edit-locals-map-1
3667 (let ((map (make-sparse-keymap)))
3668 (suppress-keymap map)
3669 (define-key map "\r" 'gdb-edit-locals-value)
3670 (define-key map [mouse-2] 'gdb-edit-locals-value)
3672 "Keymap to edit value of a simple data type local variable.")
3674 (defun gdb-edit-locals-value (&optional event)
3675 "Assign a value to a variable displayed in the locals buffer."
3676 (interactive (list last-input-event))
3678 (if event (posn-set-point (event-end event)))
3680 (let* ((var (current-word))
3681 (value (read-string (format "New value (%s): " var))))
3683 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3686 ;; Dont display values of arrays or structures.
3687 ;; These can be expanded using gud-watch.
3688 (defun gdb-stack-list-locals-handler ()
3689 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3690 gdb-pending-triggers))
3691 (goto-char (point-min))
3692 (if (re-search-forward gdb-error-regexp nil t)
3693 (let ((err (match-string 1)))
3694 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3695 (let ((buffer-read-only nil))
3698 (goto-char (point-min)))))
3699 (let (local locals-list)
3700 (goto-char (point-min))
3701 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3702 (let ((local (list (match-string 1)
3705 (if (looking-at ",value=\\(\".*\"\\).*?}")
3706 (setcar (nthcdr 2 local) (read (match-string 1))))
3707 (push local locals-list)))
3708 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3709 (and buf (with-current-buffer buf
3710 (let* ((window (get-buffer-window buf 0))
3711 (start (window-start window))
3712 (p (window-point window))
3713 (buffer-read-only nil) (name) (value))
3715 (dolist (local locals-list)
3716 (setq name (car local))
3717 (setq value (nth 2 local))
3719 (string-match "^\\0x" value))
3720 (add-text-properties 0 (length name)
3721 `(mouse-face highlight
3722 help-echo "mouse-2: create watch expression"
3723 local-map ,gdb-locals-watch-map-1)
3725 (add-text-properties 0 (length value)
3726 `(mouse-face highlight
3727 help-echo "mouse-2: edit value"
3728 local-map ,gdb-edit-locals-map-1)
3731 (concat name "\t" (nth 1 local)
3733 (set-window-start window start)
3734 (set-window-point window p))))))))
3736 (defun gdb-get-register-names ()
3737 "Create a list of register names."
3738 (goto-char (point-min))
3739 (setq gdb-register-names nil)
3740 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3741 (push (match-string 1) gdb-register-names)))
3745 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3746 ;;; gdb-ui.el ends here