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 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This mode acts as a graphical user interface to GDB. You can interact with
29 ;; GDB through the GUD buffer in the usual way, but there are also further
30 ;; buffers which control the execution and describe the state of your program.
31 ;; It separates the input/output of your program from that of GDB, if
32 ;; required, and watches expressions in the speedbar. It also uses features of
33 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
34 ;; (see the GDB Graphical Interface section in the Emacs info manual).
36 ;; By default, M-x gdb will start the debugger. However, if you have customised
37 ;; gud-gdb-command-name, then start it with M-x gdba.
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, then see the
43 ;; Annotations 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
47 ;; it. Some GDB/MI commands are used in this file through the CLI command
48 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
49 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
50 ;; still under development and is part of a process to migrate Emacs from
51 ;; annotations to GDB/MI.
53 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
54 ;; onwards to use watch expressions. It works best with GDB 6.4 where
55 ;; watch expressions will update more quickly.
57 ;;; Windows Platforms:
59 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
60 ;; explicitly in your program if you want timely display of I/O in Emacs.
61 ;; Alternatively you can make the output stream unbuffered, for example, by
65 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
68 ;; and compiling with -DUNBUFFERED while debugging.
72 ;; 1) Strings that are watched don't update in the speedbar when their
74 ;; 2) Watch expressions go out of scope when the inferior is re-run.
75 ;; 3) Cannot handle multiple debug sessions.
79 ;; 1) Use MI command -data-read-memory for memory window.
80 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
81 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
82 ;; 4) With gud-print and gud-pstar, print the variable name in the GUD
83 ;; buffer instead of the value's history number.
90 (defvar speedbar-initial-expansion-list-name)
92 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
93 (defvar gdb-previous-frame-address nil)
94 (defvar gdb-memory-address "main")
95 (defvar gdb-previous-frame nil)
96 (defvar gdb-selected-frame nil)
97 (defvar gdb-frame-number nil)
98 (defvar gdb-current-language nil)
99 (defvar gdb-var-list nil "List of variables in watch window.")
100 (defvar gdb-var-changed nil "Non-nil means that `gdb-var-list' has changed.")
101 (defvar gdb-main-file nil "Source file from which program execution begins.")
102 (defvar gdb-overlay-arrow-position nil)
103 (defvar gdb-server-prefix nil)
104 (defvar gdb-flush-pending-output nil)
105 (defvar gdb-location-alist nil
106 "Alist of breakpoint numbers and full filenames.")
107 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
108 and #define directives otherwise.")
109 (defvar gdb-error "Non-nil when GDB is reporting an error.")
110 (defvar gdb-macro-info nil
111 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
112 (defvar gdb-buffer-fringe-width nil)
114 (defvar gdb-buffer-type nil
115 "One of the symbols bound in `gdb-buffer-rules'.")
116 (make-variable-buffer-local 'gdb-buffer-type)
118 (defvar gdb-input-queue ()
119 "A list of gdb command objects.")
121 (defvar gdb-prompting nil
122 "True when gdb is idle with no pending input.")
124 (defvar gdb-output-sink 'user
125 "The disposition of the output of the current gdb command.
126 Possible values are these symbols:
128 `user' -- gdb output should be copied to the GUD buffer
131 `inferior' -- gdb output should be copied to the inferior-io buffer.
133 `pre-emacs' -- output should be ignored util the post-prompt
134 annotation is received. Then the output-sink
136 `emacs' -- output should be collected in the partial-output-buffer
137 for subsequent processing by a command. This is the
138 disposition of output generated by commands that
139 gdb mode sends to gdb on its own behalf.
140 `post-emacs' -- ignore output until the prompt annotation is
141 received, then go to USER disposition.
143 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
144 \(`user' and `emacs').")
146 (defvar gdb-current-item nil
147 "The most recent command item sent to gdb.")
149 (defvar gdb-pending-triggers '()
150 "A list of trigger functions that have run later than their output
153 (defvar gdb-first-post-prompt nil)
154 (defvar gdb-version nil)
155 (defvar gdb-locals-font-lock-keywords nil)
156 (defvar gdb-source-file-list nil
157 "List of source files for the current executable")
158 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
160 (defvar gdb-locals-font-lock-keywords-1
162 ;; var = (struct struct_tag) value
163 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
164 (1 font-lock-variable-name-face)
165 (3 font-lock-keyword-face)
166 (4 font-lock-type-face))
167 ;; var = (type) value
168 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
169 (1 font-lock-variable-name-face)
170 (3 font-lock-type-face))
172 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
173 (1 font-lock-variable-name-face))
175 "Font lock keywords used in `gdb-local-mode'.")
177 (defvar gdb-locals-font-lock-keywords-2
180 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
181 (1 font-lock-variable-name-face)
182 (3 font-lock-type-face))
184 "Font lock keywords used in `gdb-local-mode'.")
186 ;; Variables for GDB 6.4+
188 (defvar gdb-register-names nil "List of register names.")
189 (defvar gdb-changed-registers nil
190 "List of changed register numbers (strings).")
193 (defun gdba (command-line)
194 "Run gdb on program FILE in buffer *gud-FILE*.
195 The directory containing FILE becomes the initial working directory
196 and source-file directory for your debugger.
198 If `gdb-many-windows' is nil (the default value) then gdb just
199 pops up the GUD buffer unless `gdb-show-main' is t. In this case
200 it starts with two windows: one displaying the GUD buffer and the
201 other with the source file with the main routine of the inferior.
203 If `gdb-many-windows' is t, regardless of the value of
204 `gdb-show-main', the layout below will appear unless
205 `gdb-use-inferior-io-buffer' is nil when the source buffer
206 occupies the full width of the frame. Keybindings are given in
209 Watch expressions appear in the speedbar/slowbar.
211 The following commands help control operation :
213 `gdb-many-windows' - Toggle the number of windows gdb uses.
214 `gdb-restore-windows' - To restore the window layout.
216 See Info node `(emacs)GDB Graphical Interface' for a more
217 detailed description of this mode.
220 +--------------------------------------------------------------+
222 +-------------------------------+------------------------------+
223 | GUD buffer (I/O of GDB) | Locals buffer |
227 +-------------------------------+------------------------------+
228 | Source buffer | I/O buffer (of inferior) |
236 +-------------------------------+------------------------------+
237 | Stack buffer | Breakpoints buffer |
238 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
239 | | RET gdb-goto-breakpoint |
240 | | d gdb-delete-breakpoint |
241 +-------------------------------+------------------------------+"
243 (interactive (list (gud-query-cmdline 'gdba)))
245 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
249 (defvar gdb-debug-log nil)
252 (defcustom gdb-enable-debug-log nil
253 "Non-nil means record the process input and output in `gdb-debug-log'."
258 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
259 "Shell command for generating a list of defined macros in a source file.
260 This list is used to display the #define directive associated
261 with an identifier as a tooltip. It works in a debug session with
262 GDB, when gud-tooltip-mode is t.
264 Set `gdb-cpp-define-alist-flags' for any include paths or
270 (defcustom gdb-cpp-define-alist-flags ""
271 "Preprocessor flags for `gdb-cpp-define-alist-program'."
276 (defcustom gdb-show-main nil
277 "Non-nil means display source file containing the main routine at startup.
278 Also display the main routine in the disassembly buffer if present."
283 (defcustom gdb-use-inferior-io-buffer nil
284 "Non-nil means display output from the inferior in a separate buffer."
289 (defun gdb-use-inferior-io-buffer (arg)
290 "Toggle separate IO for inferior.
291 With arg, use separate IO iff arg is positive."
293 (setq gdb-use-inferior-io-buffer
295 (not gdb-use-inferior-io-buffer)
296 (> (prefix-numeric-value arg) 0)))
297 (message (format "Separate inferior IO %sabled"
298 (if gdb-use-inferior-io-buffer "en" "dis")))
299 (if (and gud-comint-buffer
300 (buffer-name gud-comint-buffer))
302 (if gdb-use-inferior-io-buffer
303 (gdb-restore-windows)
304 (kill-buffer (gdb-inferior-io-name)))
307 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
309 (defun gdb-create-define-alist ()
310 "Create an alist of #define directives for GUD tooltips."
311 (let* ((file (buffer-file-name))
313 (with-output-to-string
314 (with-current-buffer standard-output
315 (call-process shell-file-name
316 (if (file-exists-p file) file nil)
317 (list t nil) nil "-c"
318 (concat gdb-cpp-define-alist-program " "
319 gdb-cpp-define-alist-flags)))))
320 (define-list (split-string output "\n" t))
322 (setq gdb-define-alist nil)
323 (dolist (define define-list)
324 (setq name (nth 1 (split-string define "[( ]")))
325 (push (cons name define) gdb-define-alist))))
327 (defun gdb-tooltip-print (expr)
329 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
330 (goto-char (point-min))
332 (if (search-forward "=" nil t)
333 (concat expr (buffer-substring (- (point) 2) (point-max)))
335 ;; remove newline for gud-tooltip-echo-area
336 (substring string 0 (- (length string) 1))))
337 (or gud-tooltip-echo-area tooltip-use-echo-area)))
339 ;; If expr is a macro for a function don't print because of possible dangerous
340 ;; side-effects. Also printing a function within a tooltip generates an
341 ;; unexpected starting annotation (phase error).
342 (defun gdb-tooltip-print-1 (expr)
343 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
344 (goto-char (point-min))
345 (if (search-forward "expands to: " nil t)
346 (unless (looking-at "\\S-+.*(.*).*")
348 (list (concat gdb-server-prefix "print " expr "\n")
349 `(lambda () (gdb-tooltip-print ,expr))))))))
351 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
353 (defun gdb-set-gud-minor-mode-existing-buffers ()
354 "Create list of source files for current GDB session."
355 (goto-char (point-min))
356 (when (search-forward "read in on demand:" nil t)
357 (while (re-search-forward gdb-source-file-regexp nil t)
358 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
359 (dolist (buffer (buffer-list))
360 (with-current-buffer buffer
361 (when (and buffer-file-name
362 (member (file-name-nondirectory buffer-file-name)
363 gdb-source-file-list))
364 (set (make-local-variable 'gud-minor-mode) 'gdba)
365 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
366 (when gud-tooltip-mode
367 (make-local-variable 'gdb-define-alist)
368 (gdb-create-define-alist)
369 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
371 (defun gdb-find-watch-expression ()
372 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
373 (varno (nth 1 var)) (expr))
374 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varno)
375 (dolist (var1 gdb-var-list)
376 (if (string-equal (nth 1 var1) (match-string 1 varno))
377 (setq expr (concat (car var1) "." (match-string 2 varno)))))
381 (setq gdb-debug-log nil)
382 (set (make-local-variable 'gud-minor-mode) 'gdba)
383 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
385 (gud-def gud-break (if (not (string-match "Machine" mode-name))
386 (gud-call "break %f:%l" arg)
390 (gud-call "break *%a" arg)))
391 "\C-b" "Set breakpoint at current line or address.")
393 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
394 (gud-call "clear %f:%l" arg)
398 (gud-call "clear *%a" arg)))
399 "\C-d" "Remove breakpoint at current line or address.")
401 (gud-def gud-until (if (not (string-match "Machine" mode-name))
402 (gud-call "until %f:%l" arg)
406 (gud-call "until *%a" arg)))
407 "\C-u" "Continue to current line or address.")
409 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
410 nil "Start or continue execution.")
412 ;; For debugging Emacs only.
416 "pp1 " (if (eq (buffer-local-value
417 'major-mode (window-buffer)) 'speedbar-mode)
418 (gdb-find-watch-expression) "%e")) arg)
419 nil "Print the emacs s-expression.")
421 (define-key gud-minor-mode-map [left-margin mouse-1]
422 'gdb-mouse-set-clear-breakpoint)
423 (define-key gud-minor-mode-map [left-fringe mouse-1]
424 'gdb-mouse-set-clear-breakpoint)
425 (define-key gud-minor-mode-map [left-fringe mouse-2]
427 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
429 (define-key gud-minor-mode-map [left-margin mouse-2]
431 (define-key gud-minor-mode-map [left-margin mouse-3]
432 'gdb-mouse-toggle-breakpoint-margin)
433 (define-key gud-minor-mode-map [left-fringe mouse-3]
434 'gdb-mouse-toggle-breakpoint-fringe)
436 (setq comint-input-sender 'gdb-send)
439 (setq gdb-frame-address (if gdb-show-main "main" nil))
440 (setq gdb-previous-frame-address nil
441 gdb-memory-address "main"
442 gdb-previous-frame nil
443 gdb-selected-frame nil
444 gdb-current-language nil
448 gdb-first-post-prompt t
452 gdb-pending-triggers nil
453 gdb-output-sink 'user
454 gdb-server-prefix "server "
455 gdb-flush-pending-output nil
456 gdb-location-alist nil
457 gdb-source-file-list nil
460 gdb-buffer-fringe-width (car (window-fringes)))
462 (setq gdb-buffer-type 'gdba)
464 (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
466 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
467 (setq gdb-version nil)
468 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
472 (if (eq window-system 'w32)
473 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
474 (gdb-enqueue-input (list "set height 0\n" 'ignore))
475 (gdb-enqueue-input (list "set width 0\n" 'ignore))
477 (if (string-equal gdb-version "pre-6.4")
479 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
480 'gdb-set-gud-minor-mode-existing-buffers))
481 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
483 (list "server interpreter mi -data-list-register-names\n"
484 'gdb-get-register-names))
485 ; Needs GDB 6.2 onwards.
487 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
488 'gdb-set-gud-minor-mode-existing-buffers-1))
489 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
491 ;; find source file and compilation directory here
492 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
493 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
494 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
496 (run-hooks 'gdba-mode-hook))
498 (defun gdb-get-version ()
499 (goto-char (point-min))
500 (if (and (re-search-forward gdb-error-regexp nil t)
501 (string-match ".*(missing implementation)" (match-string 1)))
502 (setq gdb-version "pre-6.4")
503 (setq gdb-version "6.4+"))
506 (defun gdb-mouse-until (event)
507 "Execute source lines by dragging the overlay arrow (fringe) with the mouse."
509 (if gud-overlay-arrow-position
510 (let ((start (event-start event))
511 (end (event-end event))
512 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
513 (if (not (string-match "Machine" mode-name))
514 (if (equal buffer (window-buffer (posn-window end)))
515 (with-current-buffer buffer
516 (when (or (equal start end)
517 (equal (posn-point start)
519 gud-overlay-arrow-position)))
520 (setq line (line-number-at-pos (posn-point end)))
521 (gud-call (concat "until " (number-to-string line))))))
522 (if (equal (marker-buffer gdb-overlay-arrow-position)
523 (window-buffer (posn-window end)))
524 (when (or (equal start end)
525 (equal (posn-point start)
527 gdb-overlay-arrow-position)))
529 (goto-line (line-number-at-pos (posn-point end)))
531 (gud-call (concat "until *%a")))))))))
533 (defcustom gdb-speedbar-auto-raise t
534 "If non-nil raise speedbar every time display of watch expressions is\
540 (defun gdb-speedbar-auto-raise (arg)
541 "Toggle automatic raising of the speedbar for watch expressions.
542 With arg, automatically raise speedbar iff arg is positive."
544 (setq gdb-speedbar-auto-raise
546 (not gdb-speedbar-auto-raise)
547 (> (prefix-numeric-value arg) 0)))
548 (message (format "Auto raising %sabled"
549 (if gdb-speedbar-auto-raise "en" "dis"))))
551 (defcustom gdb-use-colon-colon-notation nil
552 "If non-nil use FUN::VAR format to display variables in the speedbar."
557 (defun gud-watch (&optional event)
558 "Watch expression at point."
559 (interactive (list last-input-event))
560 (if event (posn-set-point (event-end event)))
562 (save-selected-window
563 (let ((expr (tooltip-identifier-from-point (point))))
564 (if (and (string-equal gdb-current-language "c")
565 gdb-use-colon-colon-notation gdb-selected-frame)
566 (setq expr (concat gdb-selected-frame "::" expr)))
567 (catch 'already-watched
568 (dolist (var gdb-var-list)
569 (if (string-equal expr (car var)) (throw 'already-watched nil)))
570 (set-text-properties 0 (length expr) nil expr)
573 (if (eq gud-minor-mode 'gdba)
574 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
575 (concat"-var-create - * " expr "\n"))
576 `(lambda () (gdb-var-create-handler ,expr))))))))
578 (defconst gdb-var-create-regexp
579 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
581 (defun gdb-var-create-handler (expr)
582 (goto-char (point-min))
583 (if (re-search-forward gdb-var-create-regexp nil t)
584 (let ((var (list expr
589 (push var gdb-var-list)
591 (unless (string-equal
592 speedbar-initial-expansion-list-name "GUD")
593 (speedbar-change-initial-expansion-list "GUD"))
596 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
598 (concat "server interpreter mi \"-var-evaluate-expression "
600 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
601 `(lambda () (gdb-var-evaluate-expression-handler
603 (setq gdb-var-changed t))
604 (if (search-forward "Undefined command" nil t)
605 (message-box "Watching expressions requires gdb 6.0 onwards")
606 (message "No symbol \"%s\" in current context." expr))))
608 (defun gdb-var-evaluate-expression-handler (varnum changed)
609 (goto-char (point-min))
610 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
613 (dolist (var gdb-var-list)
614 (if (string-equal varnum (cadr var))
616 (if changed (setcar (nthcdr 5 var) t))
617 (setcar (nthcdr 4 var) (read (match-string 1)))
618 (setcar (nthcdr num gdb-var-list) var)
619 (throw 'var-found nil)))
620 (setq num (+ num 1)))))
621 (setq gdb-var-changed t))
623 (defun gdb-var-list-children (varnum)
625 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
626 `(lambda () (gdb-var-list-children-handler ,varnum)))))
628 (defconst gdb-var-list-children-regexp
629 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
632 (defun gdb-var-list-children-handler (varnum)
633 (goto-char (point-min))
634 (let ((var-list nil))
635 (catch 'child-already-watched
636 (dolist (var gdb-var-list)
637 (if (string-equal varnum (cadr var))
640 (while (re-search-forward gdb-var-list-children-regexp nil t)
641 (let ((varchild (list (match-string 2)
646 (dolist (var1 gdb-var-list)
647 (if (string-equal (cadr var1) (cadr varchild))
648 (throw 'child-already-watched nil)))
649 (push varchild var-list)
653 "server interpreter mi \"-var-evaluate-expression "
654 (nth 1 varchild) "\"\n")
655 `(lambda () (gdb-var-evaluate-expression-handler
656 ,(nth 1 varchild) nil)))))))
657 (push var var-list)))
658 (setq gdb-var-list (nreverse var-list)))))
660 (defun gdb-var-update ()
661 (when (not (member 'gdb-var-update gdb-pending-triggers))
663 (list "server interpreter mi \"-var-update *\"\n"
664 'gdb-var-update-handler))
665 (push 'gdb-var-update gdb-pending-triggers)))
667 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
669 (defun gdb-var-update-handler ()
670 (goto-char (point-min))
671 (while (re-search-forward gdb-var-update-regexp nil t)
673 (let ((varnum (match-string 1)))
674 (dolist (var gdb-var-list)
677 (concat "server interpreter mi \"-var-evaluate-expression "
679 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))
680 (throw 'var-found-1 nil)))))
681 (setq gdb-pending-triggers
682 (delq 'gdb-var-update gdb-pending-triggers))
683 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
684 ;; Dummy command to update speedbar at right time.
685 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
686 ;; Keep gdb-pending-triggers non-nil till end.
687 (push 'gdb-speedbar-timer gdb-pending-triggers)))
689 (defun gdb-speedbar-timer-fn ()
690 (setq gdb-pending-triggers
691 (delq 'gdb-speedbar-timer gdb-pending-triggers))
692 (with-current-buffer gud-comint-buffer
693 (speedbar-timer-fn)))
695 (defun gdb-var-delete ()
696 "Delete watch expression at point from the speedbar."
698 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
700 (let ((text (speedbar-line-text)))
701 (string-match "\\(\\S-+\\)" text)
702 (let* ((expr (match-string 1 text))
703 (var (assoc expr gdb-var-list))
705 (unless (string-match "\\." varnum)
708 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
710 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
711 (concat "-var-delete " varnum "\n"))
713 (setq gdb-var-list (delq var gdb-var-list))
714 (dolist (varchild gdb-var-list)
715 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
716 (setq gdb-var-list (delq varchild gdb-var-list))))
717 (setq gdb-var-changed t))))))
719 (defun gdb-edit-value (text token indent)
720 "Assign a value to a variable displayed in the speedbar."
721 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
722 (varnum (cadr var)) (value))
723 (setq value (read-string "New value: "))
726 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
727 (concat "server interpreter mi \"-var-assign "
728 varnum " " value "\"\n")
729 (concat "-var-assign " varnum " " value "\n"))
732 (defcustom gdb-show-changed-values t
733 "If non-nil highlight values that have recently changed in the speedbar.
734 The highlighting is done with `font-lock-warning-face'."
739 (defun gdb-speedbar-expand-node (text token indent)
740 "Expand the node the user clicked on.
741 TEXT is the text of the button we clicked on, a + or - item.
742 TOKEN is data related to this node.
743 INDENT is the current indentation depth."
744 (cond ((string-match "+" text) ;expand this node
745 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
746 (if (string-equal gdb-version "pre-6.4")
747 (gdb-var-list-children token)
748 (gdb-var-list-children-1 token))
751 (gdbmi-var-list-children token))))
752 ((string-match "-" text) ;contract this node
753 (dolist (var gdb-var-list)
754 (if (string-match (concat token "\\.") (nth 1 var))
755 (setq gdb-var-list (delq var gdb-var-list))))
756 (setq gdb-var-changed t)
757 (with-current-buffer gud-comint-buffer
758 (speedbar-timer-fn)))))
760 (defun gdb-get-target-string ()
761 (with-current-buffer gud-comint-buffer
768 ;; Each buffer has a TYPE -- a symbol that identifies the function
769 ;; of that particular buffer.
771 ;; The usual gdb interaction buffer is given the type `gdba' and
772 ;; is constructed specially.
774 ;; Others are constructed by gdb-get-create-buffer and
775 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
777 (defvar gdb-buffer-rules-assoc '())
779 (defun gdb-get-buffer (key)
780 "Return the gdb buffer tagged with type KEY.
781 The key should be one of the cars in `gdb-buffer-rules-assoc'."
783 (gdb-look-for-tagged-buffer key (buffer-list))))
785 (defun gdb-get-create-buffer (key)
786 "Create a new gdb buffer of the type specified by KEY.
787 The key should be one of the cars in `gdb-buffer-rules-assoc'."
788 (or (gdb-get-buffer key)
789 (let* ((rules (assoc key gdb-buffer-rules-assoc))
790 (name (funcall (gdb-rules-name-maker rules)))
791 (new (get-buffer-create name)))
792 (with-current-buffer new
794 (if (cdr (cdr rules))
795 (setq trigger (funcall (car (cdr (cdr rules))))))
796 (setq gdb-buffer-type key)
797 (set (make-local-variable 'gud-minor-mode)
798 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
799 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
800 (if trigger (funcall trigger)))
803 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
805 (defun gdb-look-for-tagged-buffer (key bufs)
807 (while (and (not retval) bufs)
808 (set-buffer (car bufs))
809 (if (eq gdb-buffer-type key)
810 (setq retval (car bufs)))
811 (setq bufs (cdr bufs)))
815 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
816 ;; at least one and possible more functions. The functions have these
817 ;; roles in defining a buffer type:
819 ;; NAME - Return a name for this buffer type.
821 ;; The remaining function(s) are optional:
823 ;; MODE - called in a new buffer with no arguments, should establish
824 ;; the proper mode for the buffer.
827 (defun gdb-set-buffer-rules (buffer-type &rest rules)
828 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
830 (setcdr binding rules)
831 (push (cons buffer-type rules)
832 gdb-buffer-rules-assoc))))
834 ;; GUD buffers are an exception to the rules
835 (gdb-set-buffer-rules 'gdba 'error)
837 ;; Partial-output buffer : This accumulates output from a command executed on
838 ;; behalf of emacs (rather than the user).
840 (gdb-set-buffer-rules 'gdb-partial-output-buffer
841 'gdb-partial-output-name)
843 (defun gdb-partial-output-name ()
844 (concat "*partial-output-"
845 (gdb-get-target-string)
849 (gdb-set-buffer-rules 'gdb-inferior-io
850 'gdb-inferior-io-name
851 'gdb-inferior-io-mode)
853 (defun gdb-inferior-io-name ()
854 (concat "*input/output of "
855 (gdb-get-target-string)
858 (defun gdb-display-inferior-io-buffer ()
859 "Display IO of inferior in a separate window."
861 (if gdb-use-inferior-io-buffer
863 (gdb-get-create-buffer 'gdb-inferior-io))))
865 (defconst gdb-frame-parameters
866 '((height . 14) (width . 80)
868 (tool-bar-lines . nil)
869 (menu-bar-lines . nil)
872 (defun gdb-frame-inferior-io-buffer ()
873 "Display IO of inferior in a new frame."
875 (if gdb-use-inferior-io-buffer
876 (let ((special-display-regexps (append special-display-regexps '(".*")))
877 (special-display-frame-alist gdb-frame-parameters))
878 (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
880 (defvar gdb-inferior-io-mode-map
881 (let ((map (make-sparse-keymap)))
882 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
883 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
884 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
885 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
886 (define-key map "\C-d" 'gdb-inferior-io-eof)
889 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
890 "Major mode for gdb inferior-io."
891 :syntax-table nil :abbrev-table nil
892 ;; We want to use comint because it has various nifty and familiar
893 ;; features. We don't need a process, but comint wants one, so create
895 (make-comint-in-buffer
896 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
897 (current-buffer) "hexl")
898 (setq comint-input-sender 'gdb-inferior-io-sender))
900 (defun gdb-inferior-io-sender (proc string)
901 ;; PROC is the pseudo-process created to satisfy comint.
902 (with-current-buffer (process-buffer proc)
903 (setq proc (get-buffer-process gud-comint-buffer))
904 (process-send-string proc string)
905 (process-send-string proc "\n")))
907 (defun gdb-inferior-io-interrupt ()
908 "Interrupt the program being debugged."
911 (get-buffer-process gud-comint-buffer) comint-ptyp))
913 (defun gdb-inferior-io-quit ()
914 "Send quit signal to the program being debugged."
917 (get-buffer-process gud-comint-buffer) comint-ptyp))
919 (defun gdb-inferior-io-stop ()
920 "Stop the program being debugged."
923 (get-buffer-process gud-comint-buffer) comint-ptyp))
925 (defun gdb-inferior-io-eof ()
926 "Send end-of-file to the program being debugged."
929 (get-buffer-process gud-comint-buffer)))
932 ;; gdb communications
935 ;; INPUT: things sent to gdb
937 ;; The queues are lists. Each element is either a string (indicating user or
938 ;; user-like input) or a list of the form:
940 ;; (INPUT-STRING HANDLER-FN)
942 ;; The handler function will be called from the partial-output buffer when the
943 ;; command completes. This is the way to write commands which invoke gdb
944 ;; commands autonomously.
946 ;; These lists are consumed tail first.
949 (defun gdb-send (proc string)
950 "A comint send filter for gdb.
951 This filter may simply queue input for a later time."
952 (with-current-buffer gud-comint-buffer
953 (let ((inhibit-read-only t))
954 (remove-text-properties (point-min) (point-max) '(face))))
955 (let ((item (concat string "\n")))
958 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
959 (process-send-string proc item))
960 (gdb-enqueue-input item))))
962 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
963 ;; is a query, or other non-top-level prompt.
965 (defun gdb-enqueue-input (item)
969 (setq gdb-prompting nil))
970 (push item gdb-input-queue)))
972 (defun gdb-dequeue-input ()
973 (let ((queue gdb-input-queue))
975 (let ((last (car (last queue))))
976 (unless (nbutlast queue) (setq gdb-input-queue '()))
979 (defun gdb-send-item (item)
980 (setq gdb-flush-pending-output nil)
981 (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
982 (setq gdb-current-item item)
983 (let ((process (get-buffer-process gud-comint-buffer)))
984 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
987 (setq gdb-output-sink 'user)
988 (process-send-string process item))
990 (gdb-clear-partial-output)
991 (setq gdb-output-sink 'pre-emacs)
992 (process-send-string process
994 ;; case: eq gud-minor-mode 'gdbmi
995 (gdb-clear-partial-output)
996 (setq gdb-output-sink 'emacs)
997 (process-send-string process (car item)))))
1000 ;; output -- things gdb prints to emacs
1002 ;; GDB output is a stream interrupted by annotations.
1003 ;; Annotations can be recognized by their beginning
1004 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1006 ;; The tag is a string obeying symbol syntax.
1008 ;; The optional part `<opt>' can be either the empty string
1009 ;; or a space followed by more data relating to the annotation.
1010 ;; For example, the SOURCE annotation is followed by a filename,
1011 ;; line number and various useless goo. This data must not include
1015 (defcustom gud-gdba-command-name "gdb -annotate=3"
1016 "Default command to execute an executable under the GDB-UI debugger."
1021 (defvar gdb-annotation-rules
1022 '(("pre-prompt" gdb-pre-prompt)
1023 ("prompt" gdb-prompt)
1024 ("commands" gdb-subprompt)
1025 ("overload-choice" gdb-subprompt)
1026 ("query" gdb-subprompt)
1027 ;; Need this prompt for GDB 6.1
1028 ("nquery" gdb-subprompt)
1029 ("prompt-for-continue" gdb-subprompt)
1030 ("post-prompt" gdb-post-prompt)
1031 ("source" gdb-source)
1032 ("starting" gdb-starting)
1033 ("exited" gdb-exited)
1034 ("signalled" gdb-exited)
1035 ("signal" gdb-stopping)
1036 ("breakpoint" gdb-stopping)
1037 ("watchpoint" gdb-stopping)
1038 ("frame-begin" gdb-frame-begin)
1039 ("stopped" gdb-stopped)
1040 ("error-begin" gdb-error)
1042 ) "An assoc mapping annotation tags to functions which process them.")
1045 (setq gdb-flush-pending-output t)
1046 (setq gud-running nil)
1047 (setq gdb-output-sink 'user)
1048 (setq gdb-input-queue nil)
1049 (setq gdb-pending-triggers nil)
1050 (setq gdb-prompting t))
1052 (defconst gdb-source-spec-regexp
1053 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1055 ;; Do not use this except as an annotation handler.
1056 (defun gdb-source (args)
1057 (string-match gdb-source-spec-regexp args)
1058 ;; Extract the frame position from the marker.
1059 (setq gud-last-frame
1061 (match-string 1 args)
1062 (string-to-number (match-string 2 args))))
1063 (setq gdb-frame-address (match-string 3 args))
1064 ;; cover for auto-display output which comes *before*
1065 ;; stopped annotation
1066 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1068 (defun gdb-pre-prompt (ignored)
1069 "An annotation handler for `pre-prompt'.
1070 This terminates the collection of output from a previous command if that
1071 happens to be in effect."
1072 (setq gdb-error nil)
1073 (let ((sink gdb-output-sink))
1077 (setq gdb-output-sink 'post-emacs))
1080 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1082 (defun gdb-prompt (ignored)
1083 "An annotation handler for `prompt'.
1084 This sends the next command (if any) to gdb."
1085 (when gdb-first-prompt
1087 (setq gdb-first-prompt nil))
1088 (let ((sink gdb-output-sink))
1091 ((eq sink 'post-emacs)
1092 (setq gdb-output-sink 'user)
1094 (car (cdr gdb-current-item))))
1095 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1096 (funcall handler))))
1099 (error "Phase error in gdb-prompt (got %s)" sink))))
1100 (let ((input (gdb-dequeue-input)))
1102 (gdb-send-item input)
1104 (setq gdb-prompting t)
1105 (gud-display-frame)))))
1107 (defun gdb-subprompt (ignored)
1108 "An annotation handler for non-top-level prompts."
1109 (setq gdb-prompting t))
1111 (defun gdb-starting (ignored)
1112 "An annotation handler for `starting'.
1113 This says that I/O for the subprocess is now the program being debugged,
1115 (setq gdb-active-process t)
1116 (let ((sink gdb-output-sink))
1120 (setq gud-running t)
1121 (if gdb-use-inferior-io-buffer
1122 (setq gdb-output-sink 'inferior))))
1125 (error "Unexpected `starting' annotation")))))
1127 (defun gdb-stopping (ignored)
1128 "An annotation handler for `breakpoint' and other annotations.
1129 They say that I/O for the subprocess is now GDB, not the program
1131 (if gdb-use-inferior-io-buffer
1132 (let ((sink gdb-output-sink))
1134 ((eq sink 'inferior)
1135 (setq gdb-output-sink 'user))
1138 (error "Unexpected stopping annotation"))))))
1140 (defun gdb-exited (ignored)
1141 "An annotation handler for `exited' and `signalled'.
1142 They say that I/O for the subprocess is now GDB, not the program
1143 being debugged and that the program is no longer running. This
1144 function is used to change the focus of GUD tooltips to #define
1146 (setq gdb-active-process nil)
1147 (setq gud-overlay-arrow-position nil)
1148 (setq gdb-overlay-arrow-position nil)
1149 (gdb-stopping ignored))
1151 (defun gdb-frame-begin (ignored)
1152 (let ((sink gdb-output-sink))
1154 ((eq sink 'inferior)
1155 (setq gdb-output-sink 'user))
1157 ((eq sink 'emacs) t)
1160 (error "Unexpected frame-begin annotation (%S)" sink)))))
1162 (defun gdb-stopped (ignored)
1163 "An annotation handler for `stopped'.
1164 It is just like `gdb-stopping', except that if we already set the output
1165 sink to `user' in `gdb-stopping', that is fine."
1166 (setq gud-running nil)
1167 (setq gdb-active-process t)
1168 (let ((sink gdb-output-sink))
1170 ((eq sink 'inferior)
1171 (setq gdb-output-sink 'user))
1175 (error "Unexpected stopped annotation")))))
1177 (defun gdb-error (ignored)
1178 (setq gdb-error (not gdb-error)))
1180 (defun gdb-post-prompt (ignored)
1181 "An annotation handler for `post-prompt'.
1182 This begins the collection of output from the current command if that
1183 happens to be appropriate."
1184 ;; Don't add to queue if there outstanding items or GDB is not known yet.
1185 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1186 (gdb-get-selected-frame)
1187 (gdb-invalidate-frames)
1188 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1189 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
1190 (gdb-invalidate-breakpoints)
1191 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1192 ;; so gdb-frame-address is updated.
1193 ;; (gdb-invalidate-assembler)
1195 (if (string-equal gdb-version "pre-6.4")
1196 (gdb-invalidate-registers)
1197 (if (gdb-get-buffer 'gdb-registers-buffer) (gdb-get-changed-registers))
1198 (gdb-invalidate-registers-1))
1200 (gdb-invalidate-memory)
1201 (if (string-equal gdb-version "pre-6.4")
1202 (gdb-invalidate-locals)
1203 (gdb-invalidate-locals-1))
1205 (gdb-invalidate-threads)
1206 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1207 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1208 ;; Only needed/used with speedbar/watch expressions.
1209 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1210 (setq gdb-var-changed t) ; force update
1211 (dolist (var gdb-var-list)
1212 (setcar (nthcdr 5 var) nil))
1213 (if (string-equal gdb-version "pre-6.4")
1215 (gdb-var-update-1)))))
1216 (setq gdb-first-post-prompt nil)
1217 (let ((sink gdb-output-sink))
1220 ((eq sink 'pre-emacs)
1221 (setq gdb-output-sink 'emacs))
1224 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1226 (defun gud-gdba-marker-filter (string)
1227 "A gud marker filter for gdb. Handle a burst of output from GDB."
1228 (if gdb-flush-pending-output
1230 (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
1231 ;; Recall the left over gud-marker-acc from last time.
1232 (setq gud-marker-acc (concat gud-marker-acc string))
1233 ;; Start accumulating output for the GUD buffer.
1236 ;; Process all the complete markers in this chunk.
1237 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1238 (let ((annotation (match-string 1 gud-marker-acc)))
1240 ;; Stuff prior to the match is just ordinary output.
1241 ;; It is either concatenated to OUTPUT or directed
1246 (substring gud-marker-acc 0 (match-beginning 0))))
1248 ;; Take that stuff off the gud-marker-acc.
1249 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1251 ;; Parse the tag from the annotation, and maybe its arguments.
1252 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1253 (let* ((annotation-type (match-string 1 annotation))
1254 (annotation-arguments (match-string 2 annotation))
1255 (annotation-rule (assoc annotation-type
1256 gdb-annotation-rules)))
1257 ;; Call the handler for this annotation.
1259 (funcall (car (cdr annotation-rule))
1260 annotation-arguments)
1261 ;; Else the annotation is not recognized. Ignore it silently,
1262 ;; so that GDB can add new annotations without causing
1266 ;; Does the remaining text end in a partial line?
1267 ;; If it does, then keep part of the gud-marker-acc until we get more.
1268 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1271 ;; Everything before the potential marker start can be output.
1273 (gdb-concat-output output
1274 (substring gud-marker-acc 0
1275 (match-beginning 0))))
1277 ;; Everything after, we save, to combine with later input.
1278 (setq gud-marker-acc (substring gud-marker-acc
1279 (match-beginning 0))))
1281 ;; In case we know the gud-marker-acc contains no partial annotations:
1283 (setq output (gdb-concat-output output gud-marker-acc))
1284 (setq gud-marker-acc "")))
1287 (defun gdb-concat-output (so-far new)
1289 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1290 (let ((sink gdb-output-sink))
1292 ((eq sink 'user) (concat so-far new))
1293 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1295 (gdb-append-to-partial-output new)
1297 ((eq sink 'inferior)
1298 (gdb-append-to-inferior-io new)
1302 (error "Bogon output sink %S" sink)))))
1304 (defun gdb-append-to-partial-output (string)
1305 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1306 (goto-char (point-max))
1309 (defun gdb-clear-partial-output ()
1310 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1313 (defun gdb-append-to-inferior-io (string)
1314 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1315 (goto-char (point-max))
1316 (insert-before-markers string))
1317 (if (not (string-equal string ""))
1318 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io))))
1320 (defun gdb-clear-inferior-io ()
1321 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1325 ;; One trick is to have a command who's output is always available in a buffer
1326 ;; of it's own, and is always up to date. We build several buffers of this
1329 ;; There are two aspects to this: gdb has to tell us when the output for that
1330 ;; command might have changed, and we have to be able to run the command
1331 ;; behind the user's back.
1333 ;; The output phasing associated with the variable gdb-output-sink
1334 ;; help us to run commands behind the user's back.
1336 ;; Below is the code for specificly managing buffers of output from one
1340 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1341 ;; It adds an input for the command we are tracking. It should be the
1342 ;; annotation rule binding of whatever gdb sends to tell us this command
1343 ;; might have changed it's output.
1345 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1346 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1347 ;; input in the input queue (see comment about ``gdb communications'' above).
1349 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1351 `(defun ,name (&optional ignored)
1352 (if (and ,demand-predicate
1354 gdb-pending-triggers)))
1357 (list ,gdb-command ',output-handler))
1358 (push ',name gdb-pending-triggers)))))
1360 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1362 (setq gdb-pending-triggers
1364 gdb-pending-triggers))
1365 (let ((buf (gdb-get-buffer ',buf-key)))
1367 (with-current-buffer buf
1368 (let* ((window (get-buffer-window buf 0))
1369 (p (window-point window))
1370 (buffer-read-only nil))
1372 (insert-buffer-substring (gdb-get-create-buffer
1373 'gdb-partial-output-buffer))
1374 (set-window-point window p)))))
1375 ;; put customisation here
1378 (defmacro def-gdb-auto-updated-buffer (buffer-key
1379 trigger-name gdb-command
1380 output-handler-name custom-defun)
1382 (def-gdb-auto-update-trigger ,trigger-name
1383 ;; The demand predicate:
1384 (gdb-get-buffer ',buffer-key)
1386 ,output-handler-name)
1387 (def-gdb-auto-update-handler ,output-handler-name
1388 ,trigger-name ,buffer-key ,custom-defun)))
1392 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1394 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1395 'gdb-breakpoints-buffer-name
1396 'gdb-breakpoints-mode)
1398 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1399 ;; This defines the auto update rule for buffers of type
1400 ;; `gdb-breakpoints-buffer'.
1402 ;; It defines a function to serve as the annotation handler that
1403 ;; handles the `foo-invalidated' message. That function is called:
1404 gdb-invalidate-breakpoints
1406 ;; To update the buffer, this command is sent to gdb.
1407 "server info breakpoints\n"
1409 ;; This also defines a function to be the handler for the output
1410 ;; from the command above. That function will copy the output into
1411 ;; the appropriately typed buffer. That function will be called:
1412 gdb-info-breakpoints-handler
1413 ;; buffer specific functions
1414 gdb-info-breakpoints-custom)
1416 (defconst breakpoint-xpm-data
1418 static char *magick[] = {
1419 /* columns rows colors chars-per-pixel */
1435 "XPM data used for breakpoint icon.")
1437 (defconst breakpoint-enabled-pbm-data
1440 0 0 0 0 1 1 1 1 0 0 0 0
1441 0 0 0 1 1 1 1 1 1 0 0 0
1442 0 0 1 1 1 1 1 1 1 1 0 0
1443 0 1 1 1 1 1 1 1 1 1 1 0
1444 0 1 1 1 1 1 1 1 1 1 1 0
1445 0 1 1 1 1 1 1 1 1 1 1 0
1446 0 1 1 1 1 1 1 1 1 1 1 0
1447 0 0 1 1 1 1 1 1 1 1 0 0
1448 0 0 0 1 1 1 1 1 1 0 0 0
1449 0 0 0 0 1 1 1 1 0 0 0 0"
1450 "PBM data used for enabled breakpoint icon.")
1452 (defconst breakpoint-disabled-pbm-data
1464 0 0 0 1 0 1 0 1 0 0"
1465 "PBM data used for disabled breakpoint icon.")
1467 (defvar breakpoint-enabled-icon nil
1468 "Icon for enabled breakpoint in display margin.")
1470 (defvar breakpoint-disabled-icon nil
1471 "Icon for disabled breakpoint in display margin.")
1473 ;; Bitmap for breakpoint in fringe
1474 (and (display-images-p)
1475 (define-fringe-bitmap 'breakpoint
1476 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1478 (defface breakpoint-enabled
1482 "Face for enabled breakpoint icon in fringe."
1485 (defface breakpoint-disabled
1486 ;; We use different values of grey for different background types,
1487 ;; so that on low-color displays it will end up as something visible
1488 ;; if it has to be approximated.
1489 '((((background dark)) :foreground "grey60")
1490 (((background light)) :foreground "grey40"))
1491 "Face for disabled breakpoint icon in fringe."
1494 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1495 (defun gdb-info-breakpoints-custom ()
1496 (let ((flag) (bptno))
1497 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1498 (dolist (buffer (buffer-list))
1499 (with-current-buffer buffer
1500 (if (and (eq gud-minor-mode 'gdba)
1501 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1502 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1503 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1505 (goto-char (point-min))
1506 (while (< (point) (- (point-max) 1))
1508 (if (looking-at "[^\t].*?breakpoint")
1510 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1511 (setq bptno (match-string 1))
1512 (setq flag (char-after (match-beginning 2)))
1514 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1516 (let ((buffer-read-only nil))
1517 (add-text-properties (match-beginning 1) (match-end 1)
1518 '(face font-lock-function-name-face)))
1519 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1520 (let ((line (match-string 2)) (buffer-read-only nil)
1521 (file (match-string 1)))
1522 (add-text-properties (line-beginning-position)
1524 '(mouse-face highlight
1525 help-echo "mouse-2, RET: visit breakpoint"))
1526 (unless (file-exists-p file)
1527 (setq file (cdr (assoc bptno gdb-location-alist))))
1529 (not (string-equal file "File not found")))
1530 (with-current-buffer
1531 (find-file-noselect file 'nowarn)
1532 (set (make-local-variable 'gud-minor-mode)
1534 (set (make-local-variable 'tool-bar-map)
1536 ;; Only want one breakpoint icon at each
1539 (goto-line (string-to-number line))
1540 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1543 (concat gdb-server-prefix "list "
1544 (match-string-no-properties 1) ":1\n")
1547 (list (concat gdb-server-prefix "info source\n")
1548 `(lambda () (gdb-get-location
1549 ,bptno ,line ,flag))))))))))
1551 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1553 (defun gdb-mouse-set-clear-breakpoint (event)
1554 "Set/clear breakpoint in left fringe/margin with mouse click."
1556 (mouse-minibuffer-check event)
1557 (let ((posn (event-end event)))
1558 (if (numberp (posn-point posn))
1559 (with-selected-window (posn-window posn)
1561 (goto-char (posn-point posn))
1562 (if (or (posn-object posn)
1563 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1566 (gud-break nil)))))))
1568 (defun gdb-mouse-toggle-breakpoint-margin (event)
1569 "Enable/disable breakpoint in left margin with mouse click."
1571 (mouse-minibuffer-check event)
1572 (let ((posn (event-end event)))
1573 (if (numberp (posn-point posn))
1574 (with-selected-window (posn-window posn)
1576 (goto-char (posn-point posn))
1577 (if (posn-object posn)
1580 (let ((bptno (get-text-property
1581 0 'gdb-bptno (car (posn-string posn)))))
1582 (concat gdb-server-prefix
1583 (if (get-text-property
1584 0 'gdb-enabled (car (posn-string posn)))
1590 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1591 "Enable/disable breakpoint in left fringe with mouse click."
1593 (mouse-minibuffer-check event)
1594 (let* ((posn (event-end event))
1595 (pos (posn-point posn))
1598 (with-selected-window (posn-window posn)
1600 (set-buffer (window-buffer (selected-window)))
1602 (dolist (overlay (overlays-in pos pos))
1603 (when (overlay-get overlay 'put-break)
1604 (setq obj (overlay-get overlay 'before-string))))
1608 (concat gdb-server-prefix
1609 (if (get-text-property 0 'gdb-enabled obj)
1612 (get-text-property 0 'gdb-bptno obj) "\n")
1615 (defun gdb-breakpoints-buffer-name ()
1616 (with-current-buffer gud-comint-buffer
1617 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1619 (defun gdb-display-breakpoints-buffer ()
1620 "Display status of user-settable breakpoints."
1623 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1625 (defun gdb-frame-breakpoints-buffer ()
1626 "Display status of user-settable breakpoints in a new frame."
1628 (let ((special-display-regexps (append special-display-regexps '(".*")))
1629 (special-display-frame-alist gdb-frame-parameters))
1630 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
1632 (defvar gdb-breakpoints-mode-map
1633 (let ((map (make-sparse-keymap))
1634 (menu (make-sparse-keymap "Breakpoints")))
1635 (define-key menu [quit] '("Quit" . kill-this-buffer))
1636 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1637 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1638 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1639 (suppress-keymap map)
1640 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1641 (define-key map " " 'gdb-toggle-breakpoint)
1642 (define-key map "D" 'gdb-delete-breakpoint)
1643 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1644 (define-key map "q" 'gdb-delete-frame-or-window)
1645 (define-key map "\r" 'gdb-goto-breakpoint)
1646 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1647 (define-key map [follow-link] 'mouse-face)
1650 (defun gdb-delete-frame-or-window ()
1651 "Delete frame if there is only one window. Otherwise delete the window."
1653 (if (one-window-p) (delete-frame)
1656 (defun gdb-breakpoints-mode ()
1657 "Major mode for gdb breakpoints.
1659 \\{gdb-breakpoints-mode-map}"
1660 (kill-all-local-variables)
1661 (setq major-mode 'gdb-breakpoints-mode)
1662 (setq mode-name "Breakpoints")
1663 (use-local-map gdb-breakpoints-mode-map)
1664 (setq buffer-read-only t)
1665 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1666 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1667 'gdb-invalidate-breakpoints
1668 'gdbmi-invalidate-breakpoints))
1670 (defun gdb-toggle-breakpoint ()
1671 "Enable/disable breakpoint at current line."
1674 (beginning-of-line 1)
1675 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1676 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1678 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1681 (concat gdb-server-prefix
1682 (if (eq ?y (char-after (match-beginning 2)))
1685 (match-string 1) "\n") 'ignore))
1686 (error "Not recognized as break/watchpoint line"))))
1688 (defun gdb-delete-breakpoint ()
1689 "Delete the breakpoint at current line."
1691 (beginning-of-line 1)
1692 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1693 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)")
1695 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1698 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1699 (error "Not recognized as break/watchpoint line")))
1701 (defun gdb-goto-breakpoint (&optional event)
1702 "Display the breakpoint location specified at current line."
1703 (interactive (list last-input-event))
1704 (if event (posn-set-point (event-end event)))
1705 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
1706 (let ((window (get-buffer-window gud-comint-buffer)))
1707 (if window (save-selected-window (select-window window))))
1709 (beginning-of-line 1)
1710 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1711 (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1713 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+.\\s-+\\S-+\\s-+\
1714 \\(\\S-+\\):\\([0-9]+\\)"))
1715 (let ((bptno (match-string 1))
1716 (file (match-string 2))
1717 (line (match-string 3)))
1718 (save-selected-window
1719 (let* ((buf (find-file-noselect
1720 (if (file-exists-p file) file
1721 (cdr (assoc bptno gdb-location-alist)))))
1722 (window (display-buffer buf)))
1723 (with-current-buffer buf
1724 (goto-line (string-to-number line))
1725 (set-window-point window (point))))))
1726 (error "Not recognized as break/watchpoint line"))))
1729 ;; Frames buffer. This displays a perpetually correct bactracktrace
1730 ;; (from the command `where').
1732 ;; Alas, if your stack is deep, it is costly.
1734 (gdb-set-buffer-rules 'gdb-stack-buffer
1735 'gdb-stack-buffer-name
1738 (def-gdb-auto-updated-buffer gdb-stack-buffer
1739 gdb-invalidate-frames
1741 gdb-info-frames-handler
1742 gdb-info-frames-custom)
1744 (defun gdb-info-frames-custom ()
1745 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1747 (let ((buffer-read-only nil)
1749 (goto-char (point-min))
1750 (while (< (point) (point-max))
1751 (setq bl (line-beginning-position)
1752 el (line-end-position))
1753 (when (looking-at "#")
1754 (add-text-properties bl el
1755 '(mouse-face highlight
1756 help-echo "mouse-2, RET: Select frame")))
1758 (when (looking-at "^#\\([0-9]+\\)")
1759 (when (string-equal (match-string 1) gdb-frame-number)
1760 (put-text-property bl (+ bl 4)
1761 'face '(:inverse-video t)))
1762 (when (re-search-forward
1764 (if (string-equal (match-string 1) "0") "" " in ")
1765 "\\([^ ]+\\) (") el t)
1766 (put-text-property (match-beginning 1) (match-end 1)
1767 'face font-lock-function-name-face)
1768 (setq bl (match-end 0))
1769 (while (re-search-forward "<\\([^>]+\\)>" el t)
1770 (put-text-property (match-beginning 1) (match-end 1)
1771 'face font-lock-function-name-face))
1773 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1774 (put-text-property (match-beginning 1) (match-end 1)
1775 'face font-lock-variable-name-face))))
1776 (forward-line 1))))))
1778 (defun gdb-stack-buffer-name ()
1779 (with-current-buffer gud-comint-buffer
1780 (concat "*stack frames of " (gdb-get-target-string) "*")))
1782 (defun gdb-display-stack-buffer ()
1783 "Display backtrace of current stack."
1786 (gdb-get-create-buffer 'gdb-stack-buffer)))
1788 (defun gdb-frame-stack-buffer ()
1789 "Display backtrace of current stack in a new frame."
1791 (let ((special-display-regexps (append special-display-regexps '(".*")))
1792 (special-display-frame-alist gdb-frame-parameters))
1793 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1795 (defvar gdb-frames-mode-map
1796 (let ((map (make-sparse-keymap)))
1797 (suppress-keymap map)
1798 (define-key map "q" 'kill-this-buffer)
1799 (define-key map "\r" 'gdb-frames-select)
1800 (define-key map [mouse-2] 'gdb-frames-select)
1801 (define-key map [follow-link] 'mouse-face)
1804 (defun gdb-frames-mode ()
1805 "Major mode for gdb frames.
1807 \\{gdb-frames-mode-map}"
1808 (kill-all-local-variables)
1809 (setq major-mode 'gdb-frames-mode)
1810 (setq mode-name "Frames")
1811 (setq buffer-read-only t)
1812 (use-local-map gdb-frames-mode-map)
1813 (run-mode-hooks 'gdb-frames-mode-hook)
1814 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1815 'gdb-invalidate-frames
1816 'gdbmi-invalidate-frames))
1818 (defun gdb-get-frame-number ()
1821 (let* ((start (line-beginning-position))
1822 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
1823 (n (or (and pos (match-string-no-properties 1)) "0")))
1826 (defun gdb-frames-select (&optional event)
1827 "Select the frame and display the relevant source."
1828 (interactive (list last-input-event))
1829 (if event (posn-set-point (event-end event)))
1831 (list (concat gdb-server-prefix "frame "
1832 (gdb-get-frame-number) "\n") 'ignore))
1833 (gud-display-frame))
1836 ;; Threads buffer. This displays a selectable thread list.
1838 (gdb-set-buffer-rules 'gdb-threads-buffer
1839 'gdb-threads-buffer-name
1842 (def-gdb-auto-updated-buffer gdb-threads-buffer
1843 gdb-invalidate-threads
1844 (concat gdb-server-prefix "info threads\n")
1845 gdb-info-threads-handler
1846 gdb-info-threads-custom)
1848 (defun gdb-info-threads-custom ()
1849 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1850 (let ((buffer-read-only nil))
1851 (goto-char (point-min))
1852 (while (< (point) (point-max))
1853 (unless (looking-at "No ")
1854 (add-text-properties (line-beginning-position) (line-end-position)
1855 '(mouse-face highlight
1856 help-echo "mouse-2, RET: select thread")))
1857 (forward-line 1)))))
1859 (defun gdb-threads-buffer-name ()
1860 (with-current-buffer gud-comint-buffer
1861 (concat "*threads of " (gdb-get-target-string) "*")))
1863 (defun gdb-display-threads-buffer ()
1864 "Display IDs of currently known threads."
1867 (gdb-get-create-buffer 'gdb-threads-buffer)))
1869 (defun gdb-frame-threads-buffer ()
1870 "Display IDs of currently known threads in a new frame."
1872 (let ((special-display-regexps (append special-display-regexps '(".*")))
1873 (special-display-frame-alist gdb-frame-parameters))
1874 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1876 (defvar gdb-threads-mode-map
1877 (let ((map (make-sparse-keymap)))
1878 (suppress-keymap map)
1879 (define-key map "q" 'kill-this-buffer)
1880 (define-key map "\r" 'gdb-threads-select)
1881 (define-key map [mouse-2] 'gdb-threads-select)
1882 (define-key map [follow-link] 'mouse-face)
1885 (defvar gdb-threads-font-lock-keywords
1887 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
1888 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1889 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
1891 "Font lock keywords used in `gdb-threads-mode'.")
1893 (defun gdb-threads-mode ()
1894 "Major mode for gdb frames.
1896 \\{gdb-threads-mode-map}"
1897 (kill-all-local-variables)
1898 (setq major-mode 'gdb-threads-mode)
1899 (setq mode-name "Threads")
1900 (setq buffer-read-only t)
1901 (use-local-map gdb-threads-mode-map)
1902 (set (make-local-variable 'font-lock-defaults)
1903 '(gdb-threads-font-lock-keywords))
1904 (run-mode-hooks 'gdb-threads-mode-hook)
1905 'gdb-invalidate-threads)
1907 (defun gdb-get-thread-number ()
1909 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1910 (match-string-no-properties 1)))
1912 (defun gdb-threads-select (&optional event)
1913 "Select the thread and display the relevant source."
1914 (interactive (list last-input-event))
1915 (if event (posn-set-point (event-end event)))
1917 (list (concat gdb-server-prefix "thread "
1918 (gdb-get-thread-number) "\n") 'ignore))
1919 (gud-display-frame))
1922 ;; Registers buffer.
1924 (defcustom gdb-all-registers nil
1925 "Non-nil means include floating-point registers."
1930 (gdb-set-buffer-rules 'gdb-registers-buffer
1931 'gdb-registers-buffer-name
1932 'gdb-registers-mode)
1934 (def-gdb-auto-updated-buffer gdb-registers-buffer
1935 gdb-invalidate-registers
1937 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
1938 gdb-info-registers-handler
1939 gdb-info-registers-custom)
1941 (defun gdb-info-registers-custom ()
1942 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1944 (let ((buffer-read-only nil)
1946 (goto-char (point-min))
1947 (while (< (point) (point-max))
1948 (setq start (line-beginning-position))
1949 (setq end (line-end-position))
1950 (when (looking-at "^[^ ]+")
1951 (unless (string-equal (match-string 0) "The")
1952 (put-text-property start (match-end 0)
1953 'face font-lock-variable-name-face)
1954 (add-text-properties start end
1955 '(help-echo "mouse-2: edit value"
1956 mouse-face highlight))))
1957 (forward-line 1))))))
1959 (defun gdb-edit-register-value (&optional event)
1960 (interactive (list last-input-event))
1962 (if event (posn-set-point (event-end event)))
1964 (let* ((register (current-word))
1965 (value (read-string (format "New value (%s): " register))))
1967 (list (concat gdb-server-prefix "set $" register "=" value "\n")
1970 (defvar gdb-registers-mode-map
1971 (let ((map (make-sparse-keymap)))
1972 (suppress-keymap map)
1973 (define-key map "\r" 'gdb-edit-register-value)
1974 (define-key map [mouse-2] 'gdb-edit-register-value)
1975 (define-key map " " 'gdb-all-registers)
1976 (define-key map "q" 'kill-this-buffer)
1979 (defun gdb-registers-mode ()
1980 "Major mode for gdb registers.
1982 \\{gdb-registers-mode-map}"
1983 (kill-all-local-variables)
1984 (setq major-mode 'gdb-registers-mode)
1985 (setq mode-name "Registers")
1986 (setq buffer-read-only t)
1987 (use-local-map gdb-registers-mode-map)
1988 (run-mode-hooks 'gdb-registers-mode-hook)
1989 (if (string-equal gdb-version "pre-6.4")
1991 (if gdb-all-registers (setq mode-name "Registers:All"))
1992 'gdb-invalidate-registers)
1993 'gdb-invalidate-registers-1))
1995 (defun gdb-registers-buffer-name ()
1996 (with-current-buffer gud-comint-buffer
1997 (concat "*registers of " (gdb-get-target-string) "*")))
1999 (defun gdb-display-registers-buffer ()
2000 "Display integer register contents."
2003 (gdb-get-create-buffer 'gdb-registers-buffer)))
2005 (defun gdb-frame-registers-buffer ()
2006 "Display integer register contents in a new frame."
2008 (let ((special-display-regexps (append special-display-regexps '(".*")))
2009 (special-display-frame-alist gdb-frame-parameters))
2010 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
2012 (defun gdb-all-registers ()
2013 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2015 (when (string-equal gdb-version "pre-6.4")
2016 (if gdb-all-registers
2018 (setq gdb-all-registers nil)
2019 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
2020 (setq mode-name "Registers")))
2021 (setq gdb-all-registers t)
2022 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
2023 (setq mode-name "Registers:All")))
2024 (message (format "Display of floating-point registers %sabled"
2025 (if gdb-all-registers "en" "dis")))
2026 (gdb-invalidate-registers)))
2031 (defcustom gdb-memory-repeat-count 32
2032 "Number of data items in memory window."
2037 (defcustom gdb-memory-format "x"
2038 "Display format of data items in memory window."
2039 :type '(choice (const :tag "Hexadecimal" "x")
2040 (const :tag "Signed decimal" "d")
2041 (const :tag "Unsigned decimal" "u")
2042 (const :tag "Octal" "o")
2043 (const :tag "Binary" "t"))
2047 (defcustom gdb-memory-unit "w"
2048 "Unit size of data items in memory window."
2049 :type '(choice (const :tag "Byte" "b")
2050 (const :tag "Halfword" "h")
2051 (const :tag "Word" "w")
2052 (const :tag "Giant word" "g"))
2056 (gdb-set-buffer-rules 'gdb-memory-buffer
2057 'gdb-memory-buffer-name
2060 (def-gdb-auto-updated-buffer gdb-memory-buffer
2061 gdb-invalidate-memory
2062 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2063 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2064 gdb-read-memory-handler
2065 gdb-read-memory-custom)
2067 (defun gdb-read-memory-custom ()
2069 (goto-char (point-min))
2070 (if (looking-at "0x[[:xdigit:]]+")
2071 (setq gdb-memory-address (match-string 0)))))
2073 (defvar gdb-memory-mode-map
2074 (let ((map (make-sparse-keymap)))
2075 (suppress-keymap map)
2076 (define-key map "q" 'kill-this-buffer)
2079 (defun gdb-memory-set-address (event)
2080 "Set the start memory address."
2082 (save-selected-window
2083 (select-window (posn-window (event-start event)))
2084 (let ((arg (read-from-minibuffer "Memory address: ")))
2085 (setq gdb-memory-address arg))
2086 (gdb-invalidate-memory)))
2088 (defun gdb-memory-set-repeat-count (event)
2089 "Set the number of data items in memory window."
2091 (save-selected-window
2092 (select-window (posn-window (event-start event)))
2093 (let* ((arg (read-from-minibuffer "Repeat count: "))
2094 (count (string-to-number arg)))
2096 (error "Positive numbers only")
2097 (customize-set-variable 'gdb-memory-repeat-count count)
2098 (gdb-invalidate-memory)))))
2100 (defun gdb-memory-format-binary ()
2101 "Set the display format to binary."
2103 (customize-set-variable 'gdb-memory-format "t")
2104 (gdb-invalidate-memory))
2106 (defun gdb-memory-format-octal ()
2107 "Set the display format to octal."
2109 (customize-set-variable 'gdb-memory-format "o")
2110 (gdb-invalidate-memory))
2112 (defun gdb-memory-format-unsigned ()
2113 "Set the display format to unsigned decimal."
2115 (customize-set-variable 'gdb-memory-format "u")
2116 (gdb-invalidate-memory))
2118 (defun gdb-memory-format-signed ()
2119 "Set the display format to decimal."
2121 (customize-set-variable 'gdb-memory-format "d")
2122 (gdb-invalidate-memory))
2124 (defun gdb-memory-format-hexadecimal ()
2125 "Set the display format to hexadecimal."
2127 (customize-set-variable 'gdb-memory-format "x")
2128 (gdb-invalidate-memory))
2130 (defvar gdb-memory-format-map
2131 (let ((map (make-sparse-keymap)))
2132 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2134 "Keymap to select format in the header line.")
2136 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2137 "Menu of display formats in the header line.")
2139 (define-key gdb-memory-format-menu [binary]
2140 '(menu-item "Binary" gdb-memory-format-binary
2141 :button (:radio . (equal gdb-memory-format "t"))))
2142 (define-key gdb-memory-format-menu [octal]
2143 '(menu-item "Octal" gdb-memory-format-octal
2144 :button (:radio . (equal gdb-memory-format "o"))))
2145 (define-key gdb-memory-format-menu [unsigned]
2146 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2147 :button (:radio . (equal gdb-memory-format "u"))))
2148 (define-key gdb-memory-format-menu [signed]
2149 '(menu-item "Signed Decimal" gdb-memory-format-signed
2150 :button (:radio . (equal gdb-memory-format "d"))))
2151 (define-key gdb-memory-format-menu [hexadecimal]
2152 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2153 :button (:radio . (equal gdb-memory-format "x"))))
2155 (defun gdb-memory-format-menu (event)
2157 (x-popup-menu event gdb-memory-format-menu))
2159 (defun gdb-memory-format-menu-1 (event)
2161 (save-selected-window
2162 (select-window (posn-window (event-start event)))
2163 (let* ((selection (gdb-memory-format-menu event))
2164 (binding (and selection (lookup-key gdb-memory-format-menu
2165 (vector (car selection))))))
2166 (if binding (call-interactively binding)))))
2168 (defun gdb-memory-unit-giant ()
2169 "Set the unit size to giant words (eight bytes)."
2171 (customize-set-variable 'gdb-memory-unit "g")
2172 (gdb-invalidate-memory))
2174 (defun gdb-memory-unit-word ()
2175 "Set the unit size to words (four bytes)."
2177 (customize-set-variable 'gdb-memory-unit "w")
2178 (gdb-invalidate-memory))
2180 (defun gdb-memory-unit-halfword ()
2181 "Set the unit size to halfwords (two bytes)."
2183 (customize-set-variable 'gdb-memory-unit "h")
2184 (gdb-invalidate-memory))
2186 (defun gdb-memory-unit-byte ()
2187 "Set the unit size to bytes."
2189 (customize-set-variable 'gdb-memory-unit "b")
2190 (gdb-invalidate-memory))
2192 (defvar gdb-memory-unit-map
2193 (let ((map (make-sparse-keymap)))
2194 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2196 "Keymap to select units in the header line.")
2198 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2199 "Menu of units in the header line.")
2201 (define-key gdb-memory-unit-menu [giantwords]
2202 '(menu-item "Giant words" gdb-memory-unit-giant
2203 :button (:radio . (equal gdb-memory-unit "g"))))
2204 (define-key gdb-memory-unit-menu [words]
2205 '(menu-item "Words" gdb-memory-unit-word
2206 :button (:radio . (equal gdb-memory-unit "w"))))
2207 (define-key gdb-memory-unit-menu [halfwords]
2208 '(menu-item "Halfwords" gdb-memory-unit-halfword
2209 :button (:radio . (equal gdb-memory-unit "h"))))
2210 (define-key gdb-memory-unit-menu [bytes]
2211 '(menu-item "Bytes" gdb-memory-unit-byte
2212 :button (:radio . (equal gdb-memory-unit "b"))))
2214 (defun gdb-memory-unit-menu (event)
2216 (x-popup-menu event gdb-memory-unit-menu))
2218 (defun gdb-memory-unit-menu-1 (event)
2220 (save-selected-window
2221 (select-window (posn-window (event-start event)))
2222 (let* ((selection (gdb-memory-unit-menu event))
2223 (binding (and selection (lookup-key gdb-memory-unit-menu
2224 (vector (car selection))))))
2225 (if binding (call-interactively binding)))))
2227 ;;from make-mode-line-mouse-map
2228 (defun gdb-make-header-line-mouse-map (mouse function) "\
2229 Return a keymap with single entry for mouse key MOUSE on the header line.
2230 MOUSE is defined to run function FUNCTION with no args in the buffer
2231 corresponding to the mode line clicked."
2232 (let ((map (make-sparse-keymap)))
2233 (define-key map (vector 'header-line mouse) function)
2234 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2237 (defvar gdb-memory-font-lock-keywords
2238 '(;; <__function.name+n>
2239 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2241 "Font lock keywords used in `gdb-memory-mode'.")
2243 (defun gdb-memory-mode ()
2244 "Major mode for examining memory.
2246 \\{gdb-memory-mode-map}"
2247 (kill-all-local-variables)
2248 (setq major-mode 'gdb-memory-mode)
2249 (setq mode-name "Memory")
2250 (setq buffer-read-only t)
2251 (use-local-map gdb-memory-mode-map)
2252 (setq header-line-format
2258 'face font-lock-warning-face
2259 'help-echo "mouse-1: Decrement address"
2260 'mouse-face 'mode-line-highlight
2262 (gdb-make-header-line-mouse-map
2264 #'(lambda () (interactive)
2265 (let ((gdb-memory-address
2266 ;; Let GDB do the arithmetic.
2268 gdb-memory-address " - "
2270 (* gdb-memory-repeat-count
2271 (cond ((string= gdb-memory-unit "b") 1)
2272 ((string= gdb-memory-unit "h") 2)
2273 ((string= gdb-memory-unit "w") 4)
2274 ((string= gdb-memory-unit "g") 8)))))))
2275 (gdb-invalidate-memory)))))
2278 'face font-lock-warning-face
2279 'help-echo "mouse-1: Increment address"
2280 'mouse-face 'mode-line-highlight
2281 'local-map (gdb-make-header-line-mouse-map
2283 #'(lambda () (interactive)
2284 (let ((gdb-memory-address nil))
2285 (gdb-invalidate-memory)))))
2287 (propertize gdb-memory-address
2288 'face font-lock-warning-face
2289 'help-echo "mouse-1: Set memory address"
2290 'mouse-face 'mode-line-highlight
2291 'local-map (gdb-make-header-line-mouse-map
2293 #'gdb-memory-set-address))
2295 (propertize (number-to-string gdb-memory-repeat-count)
2296 'face font-lock-warning-face
2297 'help-echo "mouse-1: Set repeat count"
2298 'mouse-face 'mode-line-highlight
2299 'local-map (gdb-make-header-line-mouse-map
2301 #'gdb-memory-set-repeat-count))
2303 (propertize gdb-memory-format
2304 'face font-lock-warning-face
2305 'help-echo "mouse-3: Select display format"
2306 'mouse-face 'mode-line-highlight
2307 'local-map gdb-memory-format-map)
2309 (propertize gdb-memory-unit
2310 'face font-lock-warning-face
2311 'help-echo "mouse-3: Select unit size"
2312 'mouse-face 'mode-line-highlight
2313 'local-map gdb-memory-unit-map))))
2314 (set (make-local-variable 'font-lock-defaults)
2315 '(gdb-memory-font-lock-keywords))
2316 (run-mode-hooks 'gdb-memory-mode-hook)
2317 'gdb-invalidate-memory)
2319 (defun gdb-memory-buffer-name ()
2320 (with-current-buffer gud-comint-buffer
2321 (concat "*memory of " (gdb-get-target-string) "*")))
2323 (defun gdb-display-memory-buffer ()
2324 "Display memory contents."
2327 (gdb-get-create-buffer 'gdb-memory-buffer)))
2329 (defun gdb-frame-memory-buffer ()
2330 "Display memory contents in a new frame."
2332 (let ((special-display-regexps (append special-display-regexps '(".*")))
2333 (special-display-frame-alist gdb-frame-parameters))
2334 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
2339 (gdb-set-buffer-rules 'gdb-locals-buffer
2340 'gdb-locals-buffer-name
2343 (def-gdb-auto-update-trigger gdb-invalidate-locals
2344 (gdb-get-buffer 'gdb-locals-buffer)
2345 "server info locals\n"
2346 gdb-info-locals-handler)
2348 (defvar gdb-locals-watch-map
2349 (let ((map (make-sparse-keymap)))
2350 (define-key map "\r" '(lambda () (interactive)
2353 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2354 (mouse-set-point event)
2358 "Keymap to create watch expression of a complex data type local variable.")
2360 (defconst gdb-struct-string
2361 (concat (propertize "[struct/union]"
2362 'mouse-face 'highlight
2363 'help-echo "mouse-2: create watch expression"
2364 'local-map gdb-locals-watch-map) "\n"))
2366 (defconst gdb-array-string
2367 (concat " " (propertize "[array]"
2368 'mouse-face 'highlight
2369 'help-echo "mouse-2: create watch expression"
2370 'local-map gdb-locals-watch-map) "\n"))
2372 ;; Abbreviate for arrays and structures.
2373 ;; These can be expanded using gud-display.
2374 (defun gdb-info-locals-handler ()
2375 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2376 gdb-pending-triggers))
2377 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2378 (with-current-buffer buf
2379 (goto-char (point-min))
2380 (while (re-search-forward "^[ }].*\n" nil t)
2381 (replace-match "" nil nil))
2382 (goto-char (point-min))
2383 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2384 (replace-match gdb-struct-string nil nil))
2385 (goto-char (point-min))
2386 (while (re-search-forward "\\s-*{.*\n" nil t)
2387 (replace-match gdb-array-string nil nil))))
2388 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2390 (with-current-buffer buf
2391 (let* ((window (get-buffer-window buf 0))
2392 (p (window-point window))
2393 (buffer-read-only nil))
2395 (insert-buffer-substring (gdb-get-create-buffer
2396 'gdb-partial-output-buffer))
2397 (set-window-point window p)))))
2398 (run-hooks 'gdb-info-locals-hook))
2400 (defvar gdb-locals-mode-map
2401 (let ((map (make-sparse-keymap)))
2402 (suppress-keymap map)
2403 (define-key map "q" 'kill-this-buffer)
2406 (defun gdb-locals-mode ()
2407 "Major mode for gdb locals.
2409 \\{gdb-locals-mode-map}"
2410 (kill-all-local-variables)
2411 (setq major-mode 'gdb-locals-mode)
2412 (setq mode-name (concat "Locals:" gdb-selected-frame))
2413 (setq buffer-read-only t)
2414 (use-local-map gdb-locals-mode-map)
2415 (set (make-local-variable 'font-lock-defaults)
2416 '(gdb-locals-font-lock-keywords))
2417 (run-mode-hooks 'gdb-locals-mode-hook)
2418 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2419 (if (string-equal gdb-version "pre-6.4")
2420 'gdb-invalidate-locals
2421 'gdb-invalidate-locals-1)
2422 'gdbmi-invalidate-locals))
2424 (defun gdb-locals-buffer-name ()
2425 (with-current-buffer gud-comint-buffer
2426 (concat "*locals of " (gdb-get-target-string) "*")))
2428 (defun gdb-display-locals-buffer ()
2429 "Display local variables of current stack and their values."
2432 (gdb-get-create-buffer 'gdb-locals-buffer)))
2434 (defun gdb-frame-locals-buffer ()
2435 "Display local variables of current stack and their values in a new frame."
2437 (let ((special-display-regexps (append special-display-regexps '(".*")))
2438 (special-display-frame-alist gdb-frame-parameters))
2439 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
2442 ;;;; Window management
2443 (defun gdb-display-buffer (buf &optional size)
2444 (let ((answer (get-buffer-window buf 0))
2447 (display-buffer buf nil 0) ;Raise the frame if necessary.
2448 ;; The buffer is not yet displayed.
2449 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2450 (let ((window (get-lru-window)))
2452 (not (eq window (get-buffer-window gud-comint-buffer))))
2454 (set-window-buffer window buf)
2455 (setq answer window))
2456 (setq must-split t)))
2458 (let* ((largest (get-largest-window))
2459 (cur-size (window-height largest))
2460 (new-size (and size (< size cur-size) (- cur-size size))))
2461 (setq answer (split-window largest new-size))
2462 (set-window-buffer answer buf)
2463 (set-window-dedicated-p answer t)))
2467 ;;; Shared keymap initialization:
2469 (let ((menu (make-sparse-keymap "GDB-Windows")))
2470 (define-key gud-menu-map [displays]
2471 `(menu-item "GDB-Windows" ,menu
2472 :visible (memq gud-minor-mode '(gdbmi gdba))))
2473 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2474 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2475 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2476 (define-key menu [disassembly]
2477 '("Disassembly" . gdb-display-assembler-buffer))
2478 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2479 (define-key menu [inferior]
2480 '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
2481 :enable gdb-use-inferior-io-buffer))
2482 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2483 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2484 (define-key menu [breakpoints]
2485 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2487 (let ((menu (make-sparse-keymap "GDB-Frames")))
2488 (define-key gud-menu-map [frames]
2489 `(menu-item "GDB-Frames" ,menu
2490 :visible (memq gud-minor-mode '(gdbmi gdba))))
2491 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2492 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2493 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2494 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2495 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2496 (define-key menu [inferior]
2497 '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
2498 :enable gdb-use-inferior-io-buffer))
2499 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2500 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2501 (define-key menu [breakpoints]
2502 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2504 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2505 (define-key gud-menu-map [ui]
2506 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2507 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2508 (define-key menu [gdb-use-inferior-io]
2509 '(menu-item "Separate inferior IO" gdb-use-inferior-io-buffer
2510 :visible (eq gud-minor-mode 'gdba)
2511 :help "Toggle separate IO for inferior."
2512 :button (:toggle . gdb-use-inferior-io-buffer)))
2513 (define-key menu [gdb-many-windows]
2514 '(menu-item "Display Other Windows" gdb-many-windows
2515 :help "Toggle display of locals, stack and breakpoint information"
2516 :button (:toggle . gdb-many-windows)))
2517 (define-key menu [gdb-restore-windows]
2518 '(menu-item "Restore Window Layout" gdb-restore-windows
2519 :help "Restore standard layout for debug session.")))
2521 (defun gdb-frame-gdb-buffer ()
2522 "Display GUD buffer in a new frame."
2524 (let ((special-display-regexps (append special-display-regexps '(".*")))
2525 (special-display-frame-alist gdb-frame-parameters)
2526 (same-window-regexps nil))
2527 (display-buffer gud-comint-buffer)))
2529 (defun gdb-display-gdb-buffer ()
2530 "Display GUD buffer."
2532 (let ((same-window-regexps nil))
2533 (pop-to-buffer gud-comint-buffer)))
2535 (defun gdb-set-window-buffer (name)
2536 (set-window-buffer (selected-window) (get-buffer name))
2537 (set-window-dedicated-p (selected-window) t))
2539 (defun gdb-setup-windows ()
2540 "Layout the window pattern for `gdb-many-windows'."
2541 (gdb-display-locals-buffer)
2542 (gdb-display-stack-buffer)
2543 (delete-other-windows)
2544 (gdb-display-breakpoints-buffer)
2545 (delete-other-windows)
2547 (pop-to-buffer gud-comint-buffer)
2548 (split-window nil ( / ( * (window-height) 3) 4))
2549 (split-window nil ( / (window-height) 3))
2550 (split-window-horizontally)
2552 (gdb-set-window-buffer (gdb-locals-buffer-name))
2555 (if gud-last-last-frame
2556 (gud-find-file (car gud-last-last-frame))
2557 (gud-find-file gdb-main-file)))
2558 (when gdb-use-inferior-io-buffer
2559 (split-window-horizontally)
2561 (gdb-set-window-buffer
2562 (gdb-get-create-buffer 'gdb-inferior-io)))
2564 (gdb-set-window-buffer (gdb-stack-buffer-name))
2565 (split-window-horizontally)
2567 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2570 (defcustom gdb-many-windows nil
2571 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2572 In this case it starts with two windows: one displaying the GUD
2573 buffer and the other with the source file with the main routine
2574 of the inferior. Non-nil means display the layout shown for
2580 (defun gdb-many-windows (arg)
2581 "Toggle the number of windows in the basic arrangement.
2582 With arg, display additional buffers iff arg is positive."
2584 (setq gdb-many-windows
2586 (not gdb-many-windows)
2587 (> (prefix-numeric-value arg) 0)))
2588 (message (format "Display of other windows %sabled"
2589 (if gdb-many-windows "en" "dis")))
2590 (if (and gud-comint-buffer
2591 (buffer-name gud-comint-buffer))
2593 (gdb-restore-windows)
2596 (defun gdb-restore-windows ()
2597 "Restore the basic arrangement of windows used by gdba.
2598 This arrangement depends on the value of `gdb-many-windows'."
2600 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2601 (delete-other-windows)
2602 (if gdb-many-windows
2604 (when (or gud-last-last-frame gdb-show-main)
2608 (if gud-last-last-frame
2609 (gud-find-file (car gud-last-last-frame))
2610 (gud-find-file gdb-main-file)))
2614 "Exit a debugging session cleanly.
2615 Kills the gdb buffers and resets the source buffers."
2616 (dolist (buffer (buffer-list))
2617 (unless (eq buffer gud-comint-buffer)
2618 (with-current-buffer buffer
2619 (if (memq gud-minor-mode '(gdbmi gdba))
2620 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2622 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2623 (setq gud-minor-mode nil)
2624 (kill-local-variable 'tool-bar-map)
2625 (kill-local-variable 'gdb-define-alist))))))
2626 (when (markerp gdb-overlay-arrow-position)
2627 (move-marker gdb-overlay-arrow-position nil)
2628 (setq gdb-overlay-arrow-position nil))
2629 (setq overlay-arrow-variable-list
2630 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2631 (setq gud-running nil)
2632 (setq gdb-active-process nil)
2633 (setq gdb-var-list nil)
2634 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2636 (defun gdb-source-info ()
2637 "Find the source file where the program starts and displays it with related
2639 (goto-char (point-min))
2640 (if (and (search-forward "Located in " nil t)
2641 (looking-at "\\S-+"))
2642 (setq gdb-main-file (match-string 0)))
2643 (goto-char (point-min))
2644 (if (search-forward "Includes preprocessor macro info." nil t)
2645 (setq gdb-macro-info t))
2646 (if gdb-many-windows
2648 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2650 (let ((pop-up-windows t))
2651 (display-buffer (gud-find-file gdb-main-file))))))
2653 (defun gdb-get-location (bptno line flag)
2654 "Find the directory containing the relevant source file.
2655 Put in buffer and place breakpoint icon."
2656 (goto-char (point-min))
2657 (catch 'file-not-found
2658 (if (search-forward "Located in " nil t)
2659 (when (looking-at "\\S-+")
2660 (delete (cons bptno "File not found") gdb-location-alist)
2661 (push (cons bptno (match-string 0)) gdb-location-alist))
2663 (unless (assoc bptno gdb-location-alist)
2664 (push (cons bptno "File not found") gdb-location-alist)
2665 (message-box "Cannot find source file for breakpoint location.\n\
2666 Add directory to search path for source files using the GDB command, dir."))
2667 (throw 'file-not-found nil))
2668 (with-current-buffer
2669 (find-file-noselect (match-string 0))
2670 (save-current-buffer
2671 (set (make-local-variable 'gud-minor-mode) 'gdba)
2672 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2673 ;; only want one breakpoint icon at each location
2675 (goto-line (string-to-number line))
2676 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2678 (add-hook 'find-file-hook 'gdb-find-file-hook)
2680 (defun gdb-find-file-hook ()
2681 "Set up buffer for debugging if file is part of the source code
2682 of the current session."
2683 (if (and (buffer-name gud-comint-buffer)
2684 ;; in case gud or gdb-ui is just loaded
2686 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2688 (if (member buffer-file-name gdb-source-file-list)
2689 (with-current-buffer (find-buffer-visiting buffer-file-name)
2690 (set (make-local-variable 'gud-minor-mode) 'gdba)
2691 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2694 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2695 "Put string PUTSTRING in front of POS in the current buffer.
2696 PUTSTRING is displayed by putting an overlay into the current buffer with a
2697 `before-string' string that has a `display' property whose value is
2699 (let ((string (make-string 1 ?x))
2700 (buffer (current-buffer)))
2701 (setq putstring (copy-sequence putstring))
2702 (let ((overlay (make-overlay pos pos buffer))
2704 (list (list 'margin 'left-margin) putstring))))
2705 (put-text-property 0 1 'display prop string)
2707 (add-text-properties 0 1 sprops string))
2708 (overlay-put overlay 'put-break t)
2709 (overlay-put overlay 'before-string string))))
2711 ;;from remove-images
2712 (defun gdb-remove-strings (start end &optional buffer)
2713 "Remove strings between START and END in BUFFER.
2714 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2715 BUFFER nil or omitted means use the current buffer."
2717 (setq buffer (current-buffer)))
2718 (dolist (overlay (overlays-in start end))
2719 (when (overlay-get overlay 'put-break)
2720 (delete-overlay overlay))))
2722 (defun gdb-put-breakpoint-icon (enabled bptno)
2723 (let ((start (- (line-beginning-position) 1))
2724 (end (+ (line-end-position) 1))
2725 (putstring (if enabled "B" "b"))
2726 (source-window (get-buffer-window (current-buffer) 0)))
2727 (add-text-properties
2728 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2731 (add-text-properties
2732 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2733 (add-text-properties
2734 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2735 (gdb-remove-breakpoint-icons start end)
2736 (if (display-images-p)
2737 (if (>= (or left-fringe-width
2738 (if source-window (car (window-fringes source-window)))
2739 gdb-buffer-fringe-width) 8)
2742 `(left-fringe breakpoint
2745 'breakpoint-disabled))
2747 'gdb-enabled enabled)
2748 (when (< left-margin-width 2)
2749 (save-current-buffer
2750 (setq left-margin-width 2)
2754 left-margin-width right-margin-width))))
2757 (or breakpoint-enabled-icon
2758 (setq breakpoint-enabled-icon
2759 (find-image `((:type xpm :data
2760 ,breakpoint-xpm-data
2761 :ascent 100 :pointer hand)
2763 ,breakpoint-enabled-pbm-data
2764 :ascent 100 :pointer hand)))))
2765 (or breakpoint-disabled-icon
2766 (setq breakpoint-disabled-icon
2767 (find-image `((:type xpm :data
2768 ,breakpoint-xpm-data
2769 :conversion disabled
2770 :ascent 100 :pointer hand)
2772 ,breakpoint-disabled-pbm-data
2773 :ascent 100 :pointer hand))))))
2777 (when (< left-margin-width 2)
2778 (save-current-buffer
2779 (setq left-margin-width 2)
2780 (let ((window (get-buffer-window (current-buffer) 0)))
2783 window left-margin-width right-margin-width)))))
2785 (propertize putstring
2786 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2789 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2790 (gdb-remove-strings start end)
2791 (if (display-images-p)
2792 (remove-images start end))
2794 (setq left-margin-width 0)
2795 (let ((window (get-buffer-window (current-buffer) 0)))
2798 window left-margin-width right-margin-width)))))
2802 ;; Assembler buffer.
2804 (gdb-set-buffer-rules 'gdb-assembler-buffer
2805 'gdb-assembler-buffer-name
2806 'gdb-assembler-mode)
2808 (def-gdb-auto-update-handler gdb-assembler-handler
2809 gdb-invalidate-assembler
2810 gdb-assembler-buffer
2811 gdb-assembler-custom)
2813 (defun gdb-assembler-custom ()
2814 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2815 (pos 1) (address) (flag) (bptno))
2816 (with-current-buffer buffer
2818 (if (not (equal gdb-frame-address "main"))
2820 (goto-char (point-min))
2821 (if (and gdb-frame-address
2822 (search-forward gdb-frame-address nil t))
2826 (or gdb-overlay-arrow-position
2827 (setq gdb-overlay-arrow-position (make-marker)))
2828 (set-marker gdb-overlay-arrow-position
2829 (point) (current-buffer))))))
2830 ;; remove all breakpoint-icons in assembler buffer before updating.
2831 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2832 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2833 (goto-char (point-min))
2834 (while (< (point) (- (point-max) 1))
2836 (if (looking-at "[^\t].*?breakpoint")
2839 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2840 (setq bptno (match-string 1))
2841 (setq flag (char-after (match-beginning 2)))
2842 (setq address (match-string 3))
2843 (with-current-buffer buffer
2845 (goto-char (point-min))
2846 (if (search-forward address nil t)
2847 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2848 (if (not (equal gdb-frame-address "main"))
2849 (with-current-buffer buffer
2850 (set-window-point (get-buffer-window buffer 0) pos)))))
2852 (defvar gdb-assembler-mode-map
2853 (let ((map (make-sparse-keymap)))
2854 (suppress-keymap map)
2855 (define-key map "q" 'kill-this-buffer)
2858 (defvar gdb-assembler-font-lock-keywords
2859 '(;; <__function.name+n>
2860 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2861 (1 font-lock-function-name-face))
2862 ;; 0xNNNNNNNN <__function.name+n>: opcode
2863 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2864 (4 font-lock-keyword-face))
2865 ;; %register(at least i386)
2866 ("%\\sw+" . font-lock-variable-name-face)
2867 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2868 (1 font-lock-comment-face)
2869 (2 font-lock-function-name-face))
2870 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2871 "Font lock keywords used in `gdb-assembler-mode'.")
2873 (defun gdb-assembler-mode ()
2874 "Major mode for viewing code assembler.
2876 \\{gdb-assembler-mode-map}"
2877 (kill-all-local-variables)
2878 (setq major-mode 'gdb-assembler-mode)
2879 (setq mode-name (concat "Machine:" gdb-selected-frame))
2880 (setq gdb-overlay-arrow-position nil)
2881 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2882 (setq fringes-outside-margins t)
2883 (setq buffer-read-only t)
2884 (use-local-map gdb-assembler-mode-map)
2885 (gdb-invalidate-assembler)
2886 (set (make-local-variable 'font-lock-defaults)
2887 '(gdb-assembler-font-lock-keywords))
2888 (run-mode-hooks 'gdb-assembler-mode-hook)
2889 'gdb-invalidate-assembler)
2891 (defun gdb-assembler-buffer-name ()
2892 (with-current-buffer gud-comint-buffer
2893 (concat "*disassembly of " (gdb-get-target-string) "*")))
2895 (defun gdb-display-assembler-buffer ()
2896 "Display disassembly view."
2898 (setq gdb-previous-frame nil)
2900 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2902 (defun gdb-frame-assembler-buffer ()
2903 "Display disassembly view in a new frame."
2905 (setq gdb-previous-frame nil)
2906 (let ((special-display-regexps (append special-display-regexps '(".*")))
2907 (special-display-frame-alist gdb-frame-parameters))
2908 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2910 ;; modified because if gdb-frame-address has changed value a new command
2911 ;; must be enqueued to update the buffer with the new output
2912 (defun gdb-invalidate-assembler (&optional ignored)
2913 (if (gdb-get-buffer 'gdb-assembler-buffer)
2915 (unless (and gdb-selected-frame
2916 (string-equal gdb-selected-frame gdb-previous-frame))
2917 (if (or (not (member 'gdb-invalidate-assembler
2918 gdb-pending-triggers))
2919 (not (string-equal gdb-frame-address
2920 gdb-previous-frame-address)))
2922 ;; take previous disassemble command, if any, off the queue
2923 (with-current-buffer gud-comint-buffer
2924 (let ((queue gdb-input-queue))
2925 (dolist (item queue)
2926 (if (equal (cdr item) '(gdb-assembler-handler))
2927 (setq gdb-input-queue
2928 (delete item gdb-input-queue))))))
2931 (concat gdb-server-prefix "disassemble "
2932 (if (member gdb-frame-address '(nil "main")) nil "0x")
2933 gdb-frame-address "\n")
2934 'gdb-assembler-handler))
2935 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2936 (setq gdb-previous-frame-address gdb-frame-address)
2937 (setq gdb-previous-frame gdb-selected-frame)))))))
2939 (defun gdb-get-selected-frame ()
2940 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2943 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2944 (push 'gdb-get-selected-frame
2945 gdb-pending-triggers))))
2947 (defun gdb-frame-handler ()
2948 (setq gdb-pending-triggers
2949 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2950 (goto-char (point-min))
2951 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
2952 (setq gdb-frame-number (match-string 1)))
2953 (goto-char (point-min))
2954 (if (re-search-forward
2955 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
2957 (setq gdb-selected-frame (match-string 2))
2958 (if (gdb-get-buffer 'gdb-locals-buffer)
2959 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2960 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2961 (if (gdb-get-buffer 'gdb-assembler-buffer)
2962 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
2963 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2964 (setq gdb-frame-address (match-string 1))))
2965 (goto-char (point-min))
2966 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2967 (setq gdb-current-language (match-string 1)))
2968 (gdb-invalidate-assembler))
2971 ;; Code specific to GDB 6.4
2972 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
2974 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
2975 "Create list of source files for current GDB session."
2976 (goto-char (point-min))
2977 (while (re-search-forward gdb-source-file-regexp-1 nil t)
2978 (push (match-string 1) gdb-source-file-list))
2979 (dolist (buffer (buffer-list))
2980 (with-current-buffer buffer
2981 (when (member buffer-file-name gdb-source-file-list)
2982 (set (make-local-variable 'gud-minor-mode) 'gdba)
2983 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2984 (when gud-tooltip-mode
2985 (make-local-variable 'gdb-define-alist)
2986 (gdb-create-define-alist)
2987 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
2989 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
2990 (defun gdb-var-list-children-1 (varnum)
2992 (list (concat "server interpreter mi \"-var-update " varnum "\"\n")
2995 (list (concat "server interpreter mi \"-var-list-children --all-values "
2997 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
2999 (defconst gdb-var-list-children-regexp-1
3000 "name=\"\\(.+?\\)\",exp=\"\\(.+?\\)\",numchild=\"\\(.+?\\)\",\
3001 value=\\(\".*?\"\\),type=\"\\(.+?\\)\"}")
3003 (defun gdb-var-list-children-handler-1 (varnum)
3004 (goto-char (point-min))
3005 (let ((var-list nil))
3006 (catch 'child-already-watched
3007 (dolist (var gdb-var-list)
3008 (if (string-equal varnum (cadr var))
3011 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3012 (let ((varchild (list (match-string 2)
3016 (read (match-string 4))
3018 (dolist (var1 gdb-var-list)
3019 (if (string-equal (cadr var1) (cadr varchild))
3020 (throw 'child-already-watched nil)))
3021 (push varchild var-list))))
3022 (push var var-list)))
3023 (setq gdb-var-changed t)
3024 (setq gdb-var-list (nreverse var-list)))))
3026 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3027 (defun gdb-var-update-1 ()
3028 (if (not (member 'gdb-var-update gdb-pending-triggers))
3032 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
3033 "server interpreter mi \"-var-update --all-values *\"\n"
3034 "-var-update --all-values *\n")
3035 'gdb-var-update-handler-1))
3036 (push 'gdb-var-update gdb-pending-triggers))))
3038 (defconst gdb-var-update-regexp-1 "name=\"\\(.*?\\)\",value=\\(\".*?\"\\),")
3040 (defun gdb-var-update-handler-1 ()
3041 (goto-char (point-min))
3042 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3043 (let ((varnum (match-string 1)))
3046 (dolist (var gdb-var-list)
3047 (if (string-equal varnum (cadr var))
3049 (setcar (nthcdr 5 var) t)
3050 (setcar (nthcdr 4 var) (read (match-string 2)))
3051 (setcar (nthcdr num gdb-var-list) var)
3052 (throw 'var-found1 nil)))
3053 (setq num (+ num 1))))))
3054 (setq gdb-var-changed t))
3055 (setq gdb-pending-triggers
3056 (delq 'gdb-var-update gdb-pending-triggers))
3057 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3058 ;; dummy command to update speedbar at right time
3059 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
3060 ;; keep gdb-pending-triggers non-nil till end
3061 (push 'gdb-speedbar-timer gdb-pending-triggers)))
3063 ;; Registers buffer.
3065 (gdb-set-buffer-rules 'gdb-registers-buffer
3066 'gdb-registers-buffer-name
3067 'gdb-registers-mode)
3069 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3070 (gdb-get-buffer 'gdb-registers-buffer)
3071 (if (eq gud-minor-mode 'gdba)
3072 "server interpreter mi \"-data-list-register-values x\"\n"
3073 "-data-list-register-values x\n")
3074 gdb-data-list-register-values-handler)
3076 (defconst gdb-data-list-register-values-regexp
3077 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
3079 (defun gdb-data-list-register-values-handler ()
3080 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3081 gdb-pending-triggers))
3082 (goto-char (point-min))
3083 (if (re-search-forward gdb-error-regexp nil t)
3084 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3085 (let ((buffer-read-only nil))
3087 (insert (match-string 1))
3088 (goto-char (point-min))))
3089 (let ((register-list (reverse gdb-register-names))
3090 (register nil) (register-string nil) (register-values nil))
3091 (goto-char (point-min))
3092 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3093 (setq register (pop register-list))
3094 (setq register-string (concat register "\t" (match-string 2) "\n"))
3095 (if (member (match-string 1) gdb-changed-registers)
3096 (put-text-property 0 (length register-string)
3097 'face 'font-lock-warning-face
3099 (setq register-values
3100 (concat register-values register-string)))
3101 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3102 (with-current-buffer buf
3103 (let ((p (window-point (get-buffer-window buf 0)))
3104 (buffer-read-only nil))
3106 (insert register-values)
3107 (set-window-point (get-buffer-window buf 0) p))))))
3108 (gdb-data-list-register-values-custom))
3110 (defun gdb-data-list-register-values-custom ()
3111 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3113 (let ((buffer-read-only nil)
3115 (goto-char (point-min))
3116 (while (< (point) (point-max))
3117 (setq start (line-beginning-position))
3118 (setq end (line-end-position))
3119 (when (looking-at "^[^\t]+")
3120 (unless (string-equal (match-string 0) "No registers.")
3121 (put-text-property start (match-end 0)
3122 'face font-lock-variable-name-face)
3123 (add-text-properties start end
3124 '(help-echo "mouse-2: edit value"
3125 mouse-face highlight))))
3126 (forward-line 1))))))
3128 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3129 (defun gdb-get-changed-registers ()
3130 (if (not (member 'gdb-get-changed-registers gdb-pending-triggers))
3134 (if (eq gud-minor-mode 'gdba)
3135 "server interpreter mi -data-list-changed-registers\n"
3136 "-data-list-changed-registers\n")
3137 'gdb-get-changed-registers-handler))
3138 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3140 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3142 (defun gdb-get-changed-registers-handler ()
3143 (setq gdb-pending-triggers
3144 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3145 (setq gdb-changed-registers nil)
3146 (goto-char (point-min))
3147 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3148 (push (match-string 1) gdb-changed-registers)))
3153 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3154 (gdb-set-buffer-rules 'gdb-locals-buffer
3155 'gdb-locals-buffer-name
3158 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3159 (gdb-get-buffer 'gdb-locals-buffer)
3160 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3161 gdb-stack-list-locals-handler)
3163 (defconst gdb-stack-list-locals-regexp
3164 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3166 (defvar gdb-locals-watch-map-1
3167 (let ((map (make-sparse-keymap)))
3168 (define-key map [mouse-2] 'gud-watch)
3170 "Keymap to create watch expression of a complex data type local variable.")
3172 ;; Dont display values of arrays or structures.
3173 ;; These can be expanded using gud-watch.
3174 (defun gdb-stack-list-locals-handler ()
3175 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3176 gdb-pending-triggers))
3177 (let (local locals-list)
3178 (goto-char (point-min))
3179 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3180 (let ((local (list (match-string 1)
3183 (if (looking-at ",value=\\(\".*\"\\)}")
3184 (setcar (nthcdr 2 local) (read (match-string 1))))
3185 (push local locals-list)))
3186 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3187 (and buf (with-current-buffer buf
3188 (let* ((window (get-buffer-window buf 0))
3189 (p (window-point window))
3190 (buffer-read-only nil))
3192 (dolist (local locals-list)
3193 (setq name (car local))
3194 (if (or (not (nth 2 local))
3195 (string-match "\\*$" (nth 1 local)))
3196 (add-text-properties 0 (length name)
3197 `(mouse-face highlight
3198 help-echo "mouse-2: create watch expression"
3199 local-map ,gdb-locals-watch-map-1)
3202 (concat name "\t" (nth 1 local)
3203 "\t" (nth 2 local) "\n")))
3204 (set-window-point window p)))))))
3206 (defun gdb-get-register-names ()
3207 "Create a list of register names."
3208 (goto-char (point-min))
3209 (setq gdb-register-names nil)
3210 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3211 (push (match-string 1) gdb-register-names)))
3215 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3216 ;;; gdb-ui.el ends here