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) Highlight changed register values (use MI commands
81 ;; -data-list-register-values and -data-list-changed-registers instead
82 ;; of 'info registers' after release of 22.1.
83 ;; 3) Use tree-widget.el instead of the speedbar for watch-expressions?
84 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
85 ;; 5) After release of 22.1, use "-var-list-children --all-values"
86 ;; and "-stack-list-locals --simple-values" which need GDB 6.1 onwards.
87 ;; 6) After release of 22.1, use "-var-update --all-values" which needs
89 ;; 7) With gud-print and gud-pstar, print the variable name in the GUD
90 ;; buffer instead of the value's history number.
97 (defvar speedbar-initial-expansion-list-name)
99 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
100 (defvar gdb-previous-frame-address nil)
101 (defvar gdb-memory-address "main")
102 (defvar gdb-previous-frame nil)
103 (defvar gdb-selected-frame nil)
104 (defvar gdb-frame-number nil)
105 (defvar gdb-current-language nil)
106 (defvar gdb-var-list nil "List of variables in watch window.")
107 (defvar gdb-var-changed nil "Non-nil means that `gdb-var-list' has changed.")
108 (defvar gdb-main-file nil "Source file from which program execution begins.")
109 (defvar gdb-overlay-arrow-position nil)
110 (defvar gdb-server-prefix nil)
111 (defvar gdb-flush-pending-output nil)
112 (defvar gdb-location-alist nil
113 "Alist of breakpoint numbers and full filenames.")
114 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
115 and #define directives otherwise.")
116 (defvar gdb-error "Non-nil when GDB is reporting an error.")
117 (defvar gdb-macro-info nil
118 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
119 (defvar gdb-buffer-fringe-width nil)
121 (defvar gdb-buffer-type nil
122 "One of the symbols bound in `gdb-buffer-rules'.")
123 (make-variable-buffer-local 'gdb-buffer-type)
125 (defvar gdb-input-queue ()
126 "A list of gdb command objects.")
128 (defvar gdb-prompting nil
129 "True when gdb is idle with no pending input.")
131 (defvar gdb-output-sink 'user
132 "The disposition of the output of the current gdb command.
133 Possible values are these symbols:
135 `user' -- gdb output should be copied to the GUD buffer
138 `inferior' -- gdb output should be copied to the inferior-io buffer.
140 `pre-emacs' -- output should be ignored util the post-prompt
141 annotation is received. Then the output-sink
143 `emacs' -- output should be collected in the partial-output-buffer
144 for subsequent processing by a command. This is the
145 disposition of output generated by commands that
146 gdb mode sends to gdb on its own behalf.
147 `post-emacs' -- ignore output until the prompt annotation is
148 received, then go to USER disposition.
150 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
151 \(`user' and `emacs').")
153 (defvar gdb-current-item nil
154 "The most recent command item sent to gdb.")
156 (defvar gdb-pending-triggers '()
157 "A list of trigger functions that have run later than their output
160 (defvar gdb-first-post-prompt nil)
161 (defvar gdb-version nil)
162 (defvar gdb-locals-font-lock-keywords nil)
163 (defvar gdb-source-file-list nil
164 "List of source files for the current executable")
165 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
167 (defvar gdb-locals-font-lock-keywords-1
169 ;; var = (struct struct_tag) value
170 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
171 (1 font-lock-variable-name-face)
172 (3 font-lock-keyword-face)
173 (4 font-lock-type-face))
174 ;; var = (type) value
175 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
176 (1 font-lock-variable-name-face)
177 (3 font-lock-type-face))
179 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
180 (1 font-lock-variable-name-face))
182 "Font lock keywords used in `gdb-local-mode'.")
184 (defvar gdb-locals-font-lock-keywords-2
187 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
188 (1 font-lock-variable-name-face)
189 (3 font-lock-type-face))
191 "Font lock keywords used in `gdb-local-mode'.")
193 ;; Variables for GDB 6.4+
195 (defvar gdb-register-names nil "List of register names.")
196 (defvar gdb-changed-registers nil
197 "List of changed register numbers (strings).")
200 (defun gdba (command-line)
201 "Run gdb on program FILE in buffer *gud-FILE*.
202 The directory containing FILE becomes the initial working directory
203 and source-file directory for your debugger.
205 If `gdb-many-windows' is nil (the default value) then gdb just
206 pops up the GUD buffer unless `gdb-show-main' is t. In this case
207 it starts with two windows: one displaying the GUD buffer and the
208 other with the source file with the main routine of the inferior.
210 If `gdb-many-windows' is t, regardless of the value of
211 `gdb-show-main', the layout below will appear unless
212 `gdb-use-inferior-io-buffer' is nil when the source buffer
213 occupies the full width of the frame. Keybindings are given in
216 Watch expressions appear in the speedbar/slowbar.
218 The following commands help control operation :
220 `gdb-many-windows' - Toggle the number of windows gdb uses.
221 `gdb-restore-windows' - To restore the window layout.
223 See Info node `(emacs)GDB Graphical Interface' for a more
224 detailed description of this mode.
227 +--------------------------------------------------------------+
229 +-------------------------------+------------------------------+
230 | GUD buffer (I/O of GDB) | Locals buffer |
234 +-------------------------------+------------------------------+
235 | Source buffer | I/O buffer (of inferior) |
243 +-------------------------------+------------------------------+
244 | Stack buffer | Breakpoints buffer |
245 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
246 | | RET gdb-goto-breakpoint |
247 | | d gdb-delete-breakpoint |
248 +-------------------------------+------------------------------+"
250 (interactive (list (gud-query-cmdline 'gdba)))
252 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
256 (defvar gdb-debug-log nil)
259 (defcustom gdb-enable-debug-log nil
260 "Non-nil means record the process input and output in `gdb-debug-log'."
265 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
266 "Shell command for generating a list of defined macros in a source file.
267 This list is used to display the #define directive associated
268 with an identifier as a tooltip. It works in a debug session with
269 GDB, when gud-tooltip-mode is t.
271 Set `gdb-cpp-define-alist-flags' for any include paths or
277 (defcustom gdb-cpp-define-alist-flags ""
278 "Preprocessor flags for `gdb-cpp-define-alist-program'."
283 (defcustom gdb-show-main nil
284 "Non-nil means display source file containing the main routine at startup.
285 Also display the main routine in the disassembly buffer if present."
290 (defcustom gdb-use-inferior-io-buffer nil
291 "Non-nil means display output from the inferior in a separate buffer."
296 (defun gdb-use-inferior-io-buffer (arg)
297 "Toggle separate IO for inferior.
298 With arg, use separate IO iff arg is positive."
300 (setq gdb-use-inferior-io-buffer
302 (not gdb-use-inferior-io-buffer)
303 (> (prefix-numeric-value arg) 0)))
304 (message (format "Separate inferior IO %sabled"
305 (if gdb-use-inferior-io-buffer "en" "dis")))
306 (if (and gud-comint-buffer
307 (buffer-name gud-comint-buffer))
309 (if gdb-use-inferior-io-buffer
310 (gdb-restore-windows)
311 (kill-buffer (gdb-inferior-io-name)))
314 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
316 (defun gdb-create-define-alist ()
317 "Create an alist of #define directives for GUD tooltips."
318 (let* ((file (buffer-file-name))
320 (with-output-to-string
321 (with-current-buffer standard-output
322 (call-process shell-file-name
323 (if (file-exists-p file) file nil)
324 (list t nil) nil "-c"
325 (concat gdb-cpp-define-alist-program " "
326 gdb-cpp-define-alist-flags)))))
327 (define-list (split-string output "\n" t))
329 (setq gdb-define-alist nil)
330 (dolist (define define-list)
331 (setq name (nth 1 (split-string define "[( ]")))
332 (push (cons name define) gdb-define-alist))))
334 (defun gdb-tooltip-print ()
336 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
337 (let ((string (buffer-string)))
338 ;; remove newline for gud-tooltip-echo-area
339 (substring string 0 (- (length string) 1))))
340 (or gud-tooltip-echo-area tooltip-use-echo-area)))
342 ;; If expr is a macro for a function don't print because of possible dangerous
343 ;; side-effects. Also printing a function within a tooltip generates an
344 ;; unexpected starting annotation (phase error).
345 (defun gdb-tooltip-print-1 (expr)
346 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
347 (goto-char (point-min))
348 (if (search-forward "expands to: " nil t)
349 (unless (looking-at "\\S-+.*(.*).*")
351 (list (concat gdb-server-prefix "print " expr "\n")
352 'gdb-tooltip-print))))))
354 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
356 (defun gdb-set-gud-minor-mode-existing-buffers ()
357 "Create list of source files for current GDB session."
358 (goto-char (point-min))
359 (when (search-forward "read in on demand:" nil t)
360 (while (re-search-forward gdb-source-file-regexp nil t)
361 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
362 (dolist (buffer (buffer-list))
363 (with-current-buffer buffer
364 (when (and buffer-file-name
365 (member (file-name-nondirectory buffer-file-name)
366 gdb-source-file-list))
367 (set (make-local-variable 'gud-minor-mode) 'gdba)
368 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
369 (when gud-tooltip-mode
370 (make-local-variable 'gdb-define-alist)
371 (gdb-create-define-alist)
372 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
374 (defun gdb-find-watch-expression ()
375 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
376 (varno (nth 1 var)) (expr))
377 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varno)
378 (dolist (var1 gdb-var-list)
379 (if (string-equal (nth 1 var1) (match-string 1 varno))
380 (setq expr (concat (car var1) "." (match-string 2 varno)))))
384 (setq gdb-debug-log nil)
385 (set (make-local-variable 'gud-minor-mode) 'gdba)
386 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
388 (gud-def gud-break (if (not (string-match "Machine" mode-name))
389 (gud-call "break %f:%l" arg)
393 (gud-call "break *%a" arg)))
394 "\C-b" "Set breakpoint at current line or address.")
396 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
397 (gud-call "clear %f:%l" arg)
401 (gud-call "clear *%a" arg)))
402 "\C-d" "Remove breakpoint at current line or address.")
404 (gud-def gud-until (if (not (string-match "Machine" mode-name))
405 (gud-call "until %f:%l" arg)
409 (gud-call "until *%a" arg)))
410 "\C-u" "Continue to current line or address.")
412 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
413 nil "Start or continue execution.")
415 ;; For debugging Emacs only.
419 "pp1 " (if (eq (buffer-local-value
420 'major-mode (window-buffer)) 'speedbar-mode)
421 (gdb-find-watch-expression) "%e")) arg)
422 nil "Print the emacs s-expression.")
424 (define-key gud-minor-mode-map [left-margin mouse-1]
425 'gdb-mouse-set-clear-breakpoint)
426 (define-key gud-minor-mode-map [left-fringe mouse-1]
427 'gdb-mouse-set-clear-breakpoint)
428 (define-key gud-minor-mode-map [left-fringe mouse-2]
430 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
432 (define-key gud-minor-mode-map [left-margin mouse-2]
434 (define-key gud-minor-mode-map [left-margin mouse-3]
435 'gdb-mouse-toggle-breakpoint-margin)
436 (define-key gud-minor-mode-map [left-fringe mouse-3]
437 'gdb-mouse-toggle-breakpoint-fringe)
439 (setq comint-input-sender 'gdb-send)
442 (setq gdb-frame-address (if gdb-show-main "main" nil))
443 (setq gdb-previous-frame-address nil
444 gdb-memory-address "main"
445 gdb-previous-frame nil
446 gdb-selected-frame nil
447 gdb-current-language nil
451 gdb-first-post-prompt t
455 gdb-pending-triggers nil
456 gdb-output-sink 'user
457 gdb-server-prefix "server "
458 gdb-flush-pending-output nil
459 gdb-location-alist nil
460 gdb-source-file-list nil
463 gdb-buffer-fringe-width (car (window-fringes)))
465 (setq gdb-buffer-type 'gdba)
467 (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
469 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
470 (setq gdb-version nil)
471 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
475 (if (eq window-system 'w32)
476 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
477 (gdb-enqueue-input (list "set height 0\n" 'ignore))
478 (gdb-enqueue-input (list "set width 0\n" 'ignore))
480 (if (string-equal gdb-version "pre-6.4")
482 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
483 'gdb-set-gud-minor-mode-existing-buffers))
484 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
486 (list "server interpreter mi -data-list-register-names\n"
487 'gdb-get-register-names))
488 ; Needs GDB 6.2 onwards.
490 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
491 'gdb-set-gud-minor-mode-existing-buffers-1))
492 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
494 ;; find source file and compilation directory here
495 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
496 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
497 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
499 (run-hooks 'gdba-mode-hook))
501 (defun gdb-get-version ()
502 (goto-char (point-min))
503 (if (and (re-search-forward gdb-error-regexp nil t)
504 (string-match ".*(missing implementation)" (match-string 1)))
505 (setq gdb-version "pre-6.4")
506 (setq gdb-version "6.4+"))
509 (defun gdb-mouse-until (event)
510 "Execute source lines by dragging the overlay arrow (fringe) with the mouse."
512 (if gud-overlay-arrow-position
513 (let ((start (event-start event))
514 (end (event-end event))
515 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
516 (if (not (string-match "Machine" mode-name))
517 (if (equal buffer (window-buffer (posn-window end)))
518 (with-current-buffer buffer
519 (when (or (equal start end)
520 (equal (posn-point start)
522 gud-overlay-arrow-position)))
523 (setq line (line-number-at-pos (posn-point end)))
524 (gud-call (concat "until " (number-to-string line))))))
525 (if (equal (marker-buffer gdb-overlay-arrow-position)
526 (window-buffer (posn-window end)))
527 (when (or (equal start end)
528 (equal (posn-point start)
530 gdb-overlay-arrow-position)))
532 (goto-line (line-number-at-pos (posn-point end)))
534 (gud-call (concat "until *%a")))))))))
536 (defcustom gdb-speedbar-auto-raise t
537 "If non-nil raise speedbar every time display of watch expressions is\
543 (defun gdb-speedbar-auto-raise (arg)
544 "Toggle automatic raising of the speedbar for watch expressions."
546 (setq gdb-speedbar-auto-raise
548 (not gdb-speedbar-auto-raise)
549 (> (prefix-numeric-value arg) 0)))
550 (message (format "Auto raising %sabled"
551 (if gdb-speedbar-auto-raise "en" "dis"))))
553 (defcustom gdb-use-colon-colon-notation nil
554 "If non-nil use FUN::VAR format to display variables in the speedbar."
559 (defun gud-watch (&optional event)
560 "Watch expression at point."
561 (interactive (list last-input-event))
562 (if event (posn-set-point (event-end event)))
564 (save-selected-window
565 (let ((expr (tooltip-identifier-from-point (point))))
566 (if (and (string-equal gdb-current-language "c")
567 gdb-use-colon-colon-notation gdb-selected-frame)
568 (setq expr (concat gdb-selected-frame "::" expr)))
569 (catch 'already-watched
570 (dolist (var gdb-var-list)
571 (if (string-equal expr (car var)) (throw 'already-watched nil)))
572 (set-text-properties 0 (length expr) nil expr)
575 (if (eq gud-minor-mode 'gdba)
576 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
577 (concat"-var-create - * " expr "\n"))
578 `(lambda () (gdb-var-create-handler ,expr))))))))
580 (defconst gdb-var-create-regexp
581 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
583 (defun gdb-var-create-handler (expr)
584 (goto-char (point-min))
585 (if (re-search-forward gdb-var-create-regexp nil t)
586 (let ((var (list expr
591 (push var gdb-var-list)
593 (unless (string-equal
594 speedbar-initial-expansion-list-name "GUD")
595 (speedbar-change-initial-expansion-list "GUD"))
598 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
600 (concat "server interpreter mi \"-var-evaluate-expression "
602 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
603 `(lambda () (gdb-var-evaluate-expression-handler
605 (setq gdb-var-changed t))
606 (if (search-forward "Undefined command" nil t)
607 (message-box "Watching expressions requires gdb 6.0 onwards")
608 (message "No symbol \"%s\" in current context." expr))))
610 (defun gdb-var-evaluate-expression-handler (varnum changed)
611 (goto-char (point-min))
612 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
615 (dolist (var gdb-var-list)
616 (if (string-equal varnum (cadr var))
618 (if changed (setcar (nthcdr 5 var) t))
619 (setcar (nthcdr 4 var) (read (match-string 1)))
620 (setcar (nthcdr num gdb-var-list) var)
621 (throw 'var-found nil)))
622 (setq num (+ num 1)))))
623 (setq gdb-var-changed t))
625 (defun gdb-var-list-children (varnum)
627 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
628 `(lambda () (gdb-var-list-children-handler ,varnum)))))
630 (defconst gdb-var-list-children-regexp
631 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
634 (defun gdb-var-list-children-handler (varnum)
635 (goto-char (point-min))
636 (let ((var-list nil))
637 (catch 'child-already-watched
638 (dolist (var gdb-var-list)
639 (if (string-equal varnum (cadr var))
642 (while (re-search-forward gdb-var-list-children-regexp nil t)
643 (let ((varchild (list (match-string 2)
648 (dolist (var1 gdb-var-list)
649 (if (string-equal (cadr var1) (cadr varchild))
650 (throw 'child-already-watched nil)))
651 (push varchild var-list)
655 "server interpreter mi \"-var-evaluate-expression "
656 (nth 1 varchild) "\"\n")
657 `(lambda () (gdb-var-evaluate-expression-handler
658 ,(nth 1 varchild) nil)))))))
659 (push var var-list)))
660 (setq gdb-var-list (nreverse var-list)))))
662 (defun gdb-var-update ()
663 (when (not (member 'gdb-var-update gdb-pending-triggers))
665 (list "server interpreter mi \"-var-update *\"\n"
666 'gdb-var-update-handler))
667 (push 'gdb-var-update gdb-pending-triggers)))
669 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
671 (defun gdb-var-update-handler ()
672 (goto-char (point-min))
673 (while (re-search-forward gdb-var-update-regexp nil t)
675 (let ((varnum (match-string 1)))
676 (dolist (var gdb-var-list)
679 (concat "server interpreter mi \"-var-evaluate-expression "
681 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))
682 (throw 'var-found-1 nil)))))
683 (setq gdb-pending-triggers
684 (delq 'gdb-var-update gdb-pending-triggers))
685 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
686 ;; Dummy command to update speedbar at right time.
687 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
688 ;; Keep gdb-pending-triggers non-nil till end.
689 (push 'gdb-speedbar-timer gdb-pending-triggers)))
691 (defun gdb-speedbar-timer-fn ()
692 (setq gdb-pending-triggers
693 (delq 'gdb-speedbar-timer gdb-pending-triggers))
694 (with-current-buffer gud-comint-buffer
695 (speedbar-timer-fn)))
697 (defun gdb-var-delete ()
698 "Delete watch expression at point from the speedbar."
700 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
702 (let ((text (speedbar-line-text)))
703 (string-match "\\(\\S-+\\)" text)
704 (let* ((expr (match-string 1 text))
705 (var (assoc expr gdb-var-list))
707 (unless (string-match "\\." varnum)
710 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
712 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
713 (concat "-var-delete " varnum "\n"))
715 (setq gdb-var-list (delq var gdb-var-list))
716 (dolist (varchild gdb-var-list)
717 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
718 (setq gdb-var-list (delq varchild gdb-var-list))))
719 (setq gdb-var-changed t))))))
721 (defun gdb-edit-value (text token indent)
722 "Assign a value to a variable displayed in the speedbar."
723 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
724 (varnum (cadr var)) (value))
725 (setq value (read-string "New value: "))
728 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
729 (concat "server interpreter mi \"-var-assign "
730 varnum " " value "\"\n")
731 (concat "-var-assign " varnum " " value "\n"))
734 (defcustom gdb-show-changed-values t
735 "If non-nil highlight values that have recently changed in the speedbar.
736 The highlighting is done with `font-lock-warning-face'."
741 (defun gdb-speedbar-expand-node (text token indent)
742 "Expand the node the user clicked on.
743 TEXT is the text of the button we clicked on, a + or - item.
744 TOKEN is data related to this node.
745 INDENT is the current indentation depth."
746 (cond ((string-match "+" text) ;expand this node
747 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
748 (if (string-equal gdb-version "pre-6.4")
749 (gdb-var-list-children token)
750 (gdb-var-list-children-1 token))
753 (gdbmi-var-list-children token))))
754 ((string-match "-" text) ;contract this node
755 (dolist (var gdb-var-list)
756 (if (string-match (concat token "\\.") (nth 1 var))
757 (setq gdb-var-list (delq var gdb-var-list))))
758 (setq gdb-var-changed t)
759 (with-current-buffer gud-comint-buffer
760 (speedbar-timer-fn)))))
762 (defun gdb-get-target-string ()
763 (with-current-buffer gud-comint-buffer
770 ;; Each buffer has a TYPE -- a symbol that identifies the function
771 ;; of that particular buffer.
773 ;; The usual gdb interaction buffer is given the type `gdba' and
774 ;; is constructed specially.
776 ;; Others are constructed by gdb-get-create-buffer and
777 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
779 (defvar gdb-buffer-rules-assoc '())
781 (defun gdb-get-buffer (key)
782 "Return the gdb buffer tagged with type KEY.
783 The key should be one of the cars in `gdb-buffer-rules-assoc'."
785 (gdb-look-for-tagged-buffer key (buffer-list))))
787 (defun gdb-get-create-buffer (key)
788 "Create a new gdb buffer of the type specified by KEY.
789 The key should be one of the cars in `gdb-buffer-rules-assoc'."
790 (or (gdb-get-buffer key)
791 (let* ((rules (assoc key gdb-buffer-rules-assoc))
792 (name (funcall (gdb-rules-name-maker rules)))
793 (new (get-buffer-create name)))
794 (with-current-buffer new
796 (if (cdr (cdr rules))
797 (setq trigger (funcall (car (cdr (cdr rules))))))
798 (setq gdb-buffer-type key)
799 (set (make-local-variable 'gud-minor-mode)
800 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
801 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
802 (if trigger (funcall trigger)))
805 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
807 (defun gdb-look-for-tagged-buffer (key bufs)
809 (while (and (not retval) bufs)
810 (set-buffer (car bufs))
811 (if (eq gdb-buffer-type key)
812 (setq retval (car bufs)))
813 (setq bufs (cdr bufs)))
817 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
818 ;; at least one and possible more functions. The functions have these
819 ;; roles in defining a buffer type:
821 ;; NAME - Return a name for this buffer type.
823 ;; The remaining function(s) are optional:
825 ;; MODE - called in a new buffer with no arguments, should establish
826 ;; the proper mode for the buffer.
829 (defun gdb-set-buffer-rules (buffer-type &rest rules)
830 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
832 (setcdr binding rules)
833 (push (cons buffer-type rules)
834 gdb-buffer-rules-assoc))))
836 ;; GUD buffers are an exception to the rules
837 (gdb-set-buffer-rules 'gdba 'error)
839 ;; Partial-output buffer : This accumulates output from a command executed on
840 ;; behalf of emacs (rather than the user).
842 (gdb-set-buffer-rules 'gdb-partial-output-buffer
843 'gdb-partial-output-name)
845 (defun gdb-partial-output-name ()
846 (concat "*partial-output-"
847 (gdb-get-target-string)
851 (gdb-set-buffer-rules 'gdb-inferior-io
852 'gdb-inferior-io-name
853 'gdb-inferior-io-mode)
855 (defun gdb-inferior-io-name ()
856 (concat "*input/output of "
857 (gdb-get-target-string)
860 (defun gdb-display-inferior-io-buffer ()
861 "Display IO of inferior in a separate window."
863 (if gdb-use-inferior-io-buffer
865 (gdb-get-create-buffer 'gdb-inferior-io))))
867 (defconst gdb-frame-parameters
868 '((height . 14) (width . 80)
870 (tool-bar-lines . nil)
871 (menu-bar-lines . nil)
874 (defun gdb-frame-inferior-io-buffer ()
875 "Display IO of inferior in a new frame."
877 (if gdb-use-inferior-io-buffer
878 (let ((special-display-regexps (append special-display-regexps '(".*")))
879 (special-display-frame-alist gdb-frame-parameters))
880 (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
882 (defvar gdb-inferior-io-mode-map
883 (let ((map (make-sparse-keymap)))
884 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
885 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
886 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
887 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
888 (define-key map "\C-d" 'gdb-inferior-io-eof)
891 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
892 "Major mode for gdb inferior-io."
893 :syntax-table nil :abbrev-table nil
894 ;; We want to use comint because it has various nifty and familiar
895 ;; features. We don't need a process, but comint wants one, so create
897 (make-comint-in-buffer
898 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
899 (current-buffer) "hexl")
900 (setq comint-input-sender 'gdb-inferior-io-sender))
902 (defun gdb-inferior-io-sender (proc string)
903 ;; PROC is the pseudo-process created to satisfy comint.
904 (with-current-buffer (process-buffer proc)
905 (setq proc (get-buffer-process gud-comint-buffer))
906 (process-send-string proc string)
907 (process-send-string proc "\n")))
909 (defun gdb-inferior-io-interrupt ()
910 "Interrupt the program being debugged."
913 (get-buffer-process gud-comint-buffer) comint-ptyp))
915 (defun gdb-inferior-io-quit ()
916 "Send quit signal to the program being debugged."
919 (get-buffer-process gud-comint-buffer) comint-ptyp))
921 (defun gdb-inferior-io-stop ()
922 "Stop the program being debugged."
925 (get-buffer-process gud-comint-buffer) comint-ptyp))
927 (defun gdb-inferior-io-eof ()
928 "Send end-of-file to the program being debugged."
931 (get-buffer-process gud-comint-buffer)))
934 ;; gdb communications
937 ;; INPUT: things sent to gdb
939 ;; The queues are lists. Each element is either a string (indicating user or
940 ;; user-like input) or a list of the form:
942 ;; (INPUT-STRING HANDLER-FN)
944 ;; The handler function will be called from the partial-output buffer when the
945 ;; command completes. This is the way to write commands which invoke gdb
946 ;; commands autonomously.
948 ;; These lists are consumed tail first.
951 (defun gdb-send (proc string)
952 "A comint send filter for gdb.
953 This filter may simply queue input for a later time."
954 (with-current-buffer gud-comint-buffer
955 (let ((inhibit-read-only t))
956 (remove-text-properties (point-min) (point-max) '(face))))
957 (let ((item (concat string "\n")))
960 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
961 (process-send-string proc item))
962 (gdb-enqueue-input item))))
964 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
965 ;; is a query, or other non-top-level prompt.
967 (defun gdb-enqueue-input (item)
971 (setq gdb-prompting nil))
972 (push item gdb-input-queue)))
974 (defun gdb-dequeue-input ()
975 (let ((queue gdb-input-queue))
977 (let ((last (car (last queue))))
978 (unless (nbutlast queue) (setq gdb-input-queue '()))
981 (defun gdb-send-item (item)
982 (setq gdb-flush-pending-output nil)
983 (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
984 (setq gdb-current-item item)
985 (let ((process (get-buffer-process gud-comint-buffer)))
986 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
989 (setq gdb-output-sink 'user)
990 (process-send-string process item))
992 (gdb-clear-partial-output)
993 (setq gdb-output-sink 'pre-emacs)
994 (process-send-string process
996 ;; case: eq gud-minor-mode 'gdbmi
997 (gdb-clear-partial-output)
998 (setq gdb-output-sink 'emacs)
999 (process-send-string process (car item)))))
1002 ;; output -- things gdb prints to emacs
1004 ;; GDB output is a stream interrupted by annotations.
1005 ;; Annotations can be recognized by their beginning
1006 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1008 ;; The tag is a string obeying symbol syntax.
1010 ;; The optional part `<opt>' can be either the empty string
1011 ;; or a space followed by more data relating to the annotation.
1012 ;; For example, the SOURCE annotation is followed by a filename,
1013 ;; line number and various useless goo. This data must not include
1017 (defcustom gud-gdba-command-name "gdb -annotate=3"
1018 "Default command to execute an executable under the GDB-UI debugger."
1023 (defvar gdb-annotation-rules
1024 '(("pre-prompt" gdb-pre-prompt)
1025 ("prompt" gdb-prompt)
1026 ("commands" gdb-subprompt)
1027 ("overload-choice" gdb-subprompt)
1028 ("query" gdb-subprompt)
1029 ;; Need this prompt for GDB 6.1
1030 ("nquery" gdb-subprompt)
1031 ("prompt-for-continue" gdb-subprompt)
1032 ("post-prompt" gdb-post-prompt)
1033 ("source" gdb-source)
1034 ("starting" gdb-starting)
1035 ("exited" gdb-exited)
1036 ("signalled" gdb-exited)
1037 ("signal" gdb-stopping)
1038 ("breakpoint" gdb-stopping)
1039 ("watchpoint" gdb-stopping)
1040 ("frame-begin" gdb-frame-begin)
1041 ("stopped" gdb-stopped)
1042 ("error-begin" gdb-error)
1044 ) "An assoc mapping annotation tags to functions which process them.")
1047 (setq gdb-flush-pending-output t)
1048 (setq gud-running nil)
1049 (setq gdb-output-sink 'user)
1050 (setq gdb-input-queue nil)
1051 (setq gdb-pending-triggers nil)
1052 (setq gdb-prompting t))
1054 (defconst gdb-source-spec-regexp
1055 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1057 ;; Do not use this except as an annotation handler.
1058 (defun gdb-source (args)
1059 (string-match gdb-source-spec-regexp args)
1060 ;; Extract the frame position from the marker.
1061 (setq gud-last-frame
1063 (match-string 1 args)
1064 (string-to-number (match-string 2 args))))
1065 (setq gdb-frame-address (match-string 3 args))
1066 ;; cover for auto-display output which comes *before*
1067 ;; stopped annotation
1068 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1070 (defun gdb-pre-prompt (ignored)
1071 "An annotation handler for `pre-prompt'.
1072 This terminates the collection of output from a previous command if that
1073 happens to be in effect."
1074 (setq gdb-error nil)
1075 (let ((sink gdb-output-sink))
1079 (setq gdb-output-sink 'post-emacs))
1082 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1084 (defun gdb-prompt (ignored)
1085 "An annotation handler for `prompt'.
1086 This sends the next command (if any) to gdb."
1087 (when gdb-first-prompt
1089 (setq gdb-first-prompt nil))
1090 (let ((sink gdb-output-sink))
1093 ((eq sink 'post-emacs)
1094 (setq gdb-output-sink 'user)
1096 (car (cdr gdb-current-item))))
1097 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1098 (funcall handler))))
1101 (error "Phase error in gdb-prompt (got %s)" sink))))
1102 (let ((input (gdb-dequeue-input)))
1104 (gdb-send-item input)
1106 (setq gdb-prompting t)
1107 (gud-display-frame)))))
1109 (defun gdb-subprompt (ignored)
1110 "An annotation handler for non-top-level prompts."
1111 (setq gdb-prompting t))
1113 (defun gdb-starting (ignored)
1114 "An annotation handler for `starting'.
1115 This says that I/O for the subprocess is now the program being debugged,
1117 (setq gdb-active-process t)
1118 (let ((sink gdb-output-sink))
1122 (setq gud-running t)
1123 (if gdb-use-inferior-io-buffer
1124 (setq gdb-output-sink 'inferior))))
1127 (error "Unexpected `starting' annotation")))))
1129 (defun gdb-stopping (ignored)
1130 "An annotation handler for `breakpoint' and other annotations.
1131 They say that I/O for the subprocess is now GDB, not the program
1133 (if gdb-use-inferior-io-buffer
1134 (let ((sink gdb-output-sink))
1136 ((eq sink 'inferior)
1137 (setq gdb-output-sink 'user))
1140 (error "Unexpected stopping annotation"))))))
1142 (defun gdb-exited (ignored)
1143 "An annotation handler for `exited' and `signalled'.
1144 They say that I/O for the subprocess is now GDB, not the program
1145 being debugged and that the program is no longer running. This
1146 function is used to change the focus of GUD tooltips to #define
1148 (setq gdb-active-process nil)
1149 (setq gud-overlay-arrow-position nil)
1150 (setq gdb-overlay-arrow-position nil)
1151 (gdb-stopping ignored))
1153 (defun gdb-frame-begin (ignored)
1154 (let ((sink gdb-output-sink))
1156 ((eq sink 'inferior)
1157 (setq gdb-output-sink 'user))
1159 ((eq sink 'emacs) t)
1162 (error "Unexpected frame-begin annotation (%S)" sink)))))
1164 (defun gdb-stopped (ignored)
1165 "An annotation handler for `stopped'.
1166 It is just like `gdb-stopping', except that if we already set the output
1167 sink to `user' in `gdb-stopping', that is fine."
1168 (setq gud-running nil)
1169 (setq gdb-active-process t)
1170 (let ((sink gdb-output-sink))
1172 ((eq sink 'inferior)
1173 (setq gdb-output-sink 'user))
1177 (error "Unexpected stopped annotation")))))
1179 (defun gdb-error (ignored)
1180 (setq gdb-error (not gdb-error)))
1182 (defun gdb-post-prompt (ignored)
1183 "An annotation handler for `post-prompt'.
1184 This begins the collection of output from the current command if that
1185 happens to be appropriate."
1186 ;; Don't add to queue if there outstanding items or GDB is not known yet.
1187 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1188 (gdb-get-selected-frame)
1189 (gdb-invalidate-frames)
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 (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 (define-key map "q" 'kill-this-buffer)
1644 (define-key map "\r" 'gdb-goto-breakpoint)
1645 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1646 (define-key map [follow-link] 'mouse-face)
1649 (defun gdb-breakpoints-mode ()
1650 "Major mode for gdb breakpoints.
1652 \\{gdb-breakpoints-mode-map}"
1653 (kill-all-local-variables)
1654 (setq major-mode 'gdb-breakpoints-mode)
1655 (setq mode-name "Breakpoints")
1656 (use-local-map gdb-breakpoints-mode-map)
1657 (setq buffer-read-only t)
1658 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1659 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1660 'gdb-invalidate-breakpoints
1661 'gdbmi-invalidate-breakpoints))
1663 (defun gdb-toggle-breakpoint ()
1664 "Enable/disable breakpoint at current line."
1667 (beginning-of-line 1)
1668 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1669 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1671 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1674 (concat gdb-server-prefix
1675 (if (eq ?y (char-after (match-beginning 2)))
1678 (match-string 1) "\n") 'ignore))
1679 (error "Not recognized as break/watchpoint line"))))
1681 (defun gdb-delete-breakpoint ()
1682 "Delete the breakpoint at current line."
1684 (beginning-of-line 1)
1685 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1686 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)")
1688 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1691 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1692 (error "Not recognized as break/watchpoint line")))
1694 (defun gdb-goto-breakpoint (&optional event)
1695 "Display the breakpoint location specified at current line."
1696 (interactive (list last-input-event))
1697 (if event (posn-set-point (event-end event)))
1698 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
1699 (let ((window (get-buffer-window gud-comint-buffer)))
1700 (if window (save-selected-window (select-window window))))
1702 (beginning-of-line 1)
1703 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1704 (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1706 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+.\\s-+\\S-+\\s-+\
1707 \\(\\S-+\\):\\([0-9]+\\)"))
1708 (let ((bptno (match-string 1))
1709 (file (match-string 2))
1710 (line (match-string 3)))
1711 (save-selected-window
1712 (let* ((buf (find-file-noselect
1713 (if (file-exists-p file) file
1714 (cdr (assoc bptno gdb-location-alist)))))
1715 (window (display-buffer buf)))
1716 (with-current-buffer buf
1717 (goto-line (string-to-number line))
1718 (set-window-point window (point))))))
1719 (error "Not recognized as break/watchpoint line"))))
1722 ;; Frames buffer. This displays a perpetually correct bactracktrace
1723 ;; (from the command `where').
1725 ;; Alas, if your stack is deep, it is costly.
1727 (gdb-set-buffer-rules 'gdb-stack-buffer
1728 'gdb-stack-buffer-name
1731 (def-gdb-auto-updated-buffer gdb-stack-buffer
1732 gdb-invalidate-frames
1734 gdb-info-frames-handler
1735 gdb-info-frames-custom)
1737 (defun gdb-info-frames-custom ()
1738 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1740 (let ((buffer-read-only nil)
1742 (goto-char (point-min))
1743 (while (< (point) (point-max))
1744 (setq bl (line-beginning-position)
1745 el (line-end-position))
1746 (when (looking-at "#")
1747 (add-text-properties bl el
1748 '(mouse-face highlight
1749 help-echo "mouse-2, RET: Select frame")))
1751 (when (looking-at "^#\\([0-9]+\\)")
1752 (when (string-equal (match-string 1) gdb-frame-number)
1753 (put-text-property bl (+ bl 4)
1754 'face '(:inverse-video t)))
1755 (when (re-search-forward
1757 (if (string-equal (match-string 1) "0") "" " in ")
1758 "\\([^ ]+\\) (") el t)
1759 (put-text-property (match-beginning 1) (match-end 1)
1760 'face font-lock-function-name-face)
1761 (setq bl (match-end 0))
1762 (while (re-search-forward "<\\([^>]+\\)>" el t)
1763 (put-text-property (match-beginning 1) (match-end 1)
1764 'face font-lock-function-name-face))
1766 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1767 (put-text-property (match-beginning 1) (match-end 1)
1768 'face font-lock-variable-name-face))))
1769 (forward-line 1))))))
1771 (defun gdb-stack-buffer-name ()
1772 (with-current-buffer gud-comint-buffer
1773 (concat "*stack frames of " (gdb-get-target-string) "*")))
1775 (defun gdb-display-stack-buffer ()
1776 "Display backtrace of current stack."
1779 (gdb-get-create-buffer 'gdb-stack-buffer)))
1781 (defun gdb-frame-stack-buffer ()
1782 "Display backtrace of current stack in a new frame."
1784 (let ((special-display-regexps (append special-display-regexps '(".*")))
1785 (special-display-frame-alist gdb-frame-parameters))
1786 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1788 (defvar gdb-frames-mode-map
1789 (let ((map (make-sparse-keymap)))
1790 (suppress-keymap map)
1791 (define-key map "q" 'kill-this-buffer)
1792 (define-key map "\r" 'gdb-frames-select)
1793 (define-key map [mouse-2] 'gdb-frames-select)
1794 (define-key map [follow-link] 'mouse-face)
1797 (defun gdb-frames-mode ()
1798 "Major mode for gdb frames.
1800 \\{gdb-frames-mode-map}"
1801 (kill-all-local-variables)
1802 (setq major-mode 'gdb-frames-mode)
1803 (setq mode-name "Frames")
1804 (setq buffer-read-only t)
1805 (use-local-map gdb-frames-mode-map)
1806 (run-mode-hooks 'gdb-frames-mode-hook)
1807 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1808 'gdb-invalidate-frames
1809 'gdbmi-invalidate-frames))
1811 (defun gdb-get-frame-number ()
1814 (let* ((start (line-beginning-position))
1815 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
1816 (n (or (and pos (match-string-no-properties 1)) "0")))
1819 (defun gdb-frames-select (&optional event)
1820 "Select the frame and display the relevant source."
1821 (interactive (list last-input-event))
1822 (if event (posn-set-point (event-end event)))
1824 (list (concat gdb-server-prefix "frame "
1825 (gdb-get-frame-number) "\n") 'ignore))
1826 (gud-display-frame))
1829 ;; Threads buffer. This displays a selectable thread list.
1831 (gdb-set-buffer-rules 'gdb-threads-buffer
1832 'gdb-threads-buffer-name
1835 (def-gdb-auto-updated-buffer gdb-threads-buffer
1836 gdb-invalidate-threads
1837 (concat gdb-server-prefix "info threads\n")
1838 gdb-info-threads-handler
1839 gdb-info-threads-custom)
1841 (defun gdb-info-threads-custom ()
1842 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1843 (let ((buffer-read-only nil))
1844 (goto-char (point-min))
1845 (while (< (point) (point-max))
1846 (unless (looking-at "No ")
1847 (add-text-properties (line-beginning-position) (line-end-position)
1848 '(mouse-face highlight
1849 help-echo "mouse-2, RET: select thread")))
1850 (forward-line 1)))))
1852 (defun gdb-threads-buffer-name ()
1853 (with-current-buffer gud-comint-buffer
1854 (concat "*threads of " (gdb-get-target-string) "*")))
1856 (defun gdb-display-threads-buffer ()
1857 "Display IDs of currently known threads."
1860 (gdb-get-create-buffer 'gdb-threads-buffer)))
1862 (defun gdb-frame-threads-buffer ()
1863 "Display IDs of currently known threads in a new frame."
1865 (let ((special-display-regexps (append special-display-regexps '(".*")))
1866 (special-display-frame-alist gdb-frame-parameters))
1867 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1869 (defvar gdb-threads-mode-map
1870 (let ((map (make-sparse-keymap)))
1871 (suppress-keymap map)
1872 (define-key map "q" 'kill-this-buffer)
1873 (define-key map "\r" 'gdb-threads-select)
1874 (define-key map [mouse-2] 'gdb-threads-select)
1875 (define-key map [follow-link] 'mouse-face)
1878 (defvar gdb-threads-font-lock-keywords
1880 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
1881 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1882 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
1884 "Font lock keywords used in `gdb-threads-mode'.")
1886 (defun gdb-threads-mode ()
1887 "Major mode for gdb frames.
1889 \\{gdb-threads-mode-map}"
1890 (kill-all-local-variables)
1891 (setq major-mode 'gdb-threads-mode)
1892 (setq mode-name "Threads")
1893 (setq buffer-read-only t)
1894 (use-local-map gdb-threads-mode-map)
1895 (set (make-local-variable 'font-lock-defaults)
1896 '(gdb-threads-font-lock-keywords))
1897 (run-mode-hooks 'gdb-threads-mode-hook)
1898 'gdb-invalidate-threads)
1900 (defun gdb-get-thread-number ()
1902 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1903 (match-string-no-properties 1)))
1905 (defun gdb-threads-select (&optional event)
1906 "Select the thread and display the relevant source."
1907 (interactive (list last-input-event))
1908 (if event (posn-set-point (event-end event)))
1910 (list (concat gdb-server-prefix "thread "
1911 (gdb-get-thread-number) "\n") 'ignore))
1912 (gud-display-frame))
1915 ;; Registers buffer.
1917 (defcustom gdb-all-registers nil
1918 "Non-nil means include floating-point registers."
1923 (gdb-set-buffer-rules 'gdb-registers-buffer
1924 'gdb-registers-buffer-name
1925 'gdb-registers-mode)
1927 (def-gdb-auto-updated-buffer gdb-registers-buffer
1928 gdb-invalidate-registers
1930 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
1931 gdb-info-registers-handler
1932 gdb-info-registers-custom)
1934 (defun gdb-info-registers-custom ()
1935 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1937 (let ((buffer-read-only nil)
1939 (goto-char (point-min))
1940 (while (< (point) (point-max))
1941 (setq start (line-beginning-position))
1942 (setq end (line-end-position))
1943 (when (looking-at "^[^ ]+")
1944 (unless (string-equal (match-string 0) "The")
1945 (put-text-property start (match-end 0)
1946 'face font-lock-variable-name-face)
1947 (add-text-properties start end
1948 '(help-echo "mouse-2: edit value"
1949 mouse-face highlight))))
1950 (forward-line 1))))))
1952 (defun gdb-edit-register-value (&optional event)
1953 (interactive (list last-input-event))
1955 (if event (posn-set-point (event-end event)))
1957 (let* ((register (current-word))
1958 (value (read-string (format "New value (%s): " register))))
1960 (list (concat gdb-server-prefix "set $" register "=" value "\n")
1963 (defvar gdb-registers-mode-map
1964 (let ((map (make-sparse-keymap)))
1965 (suppress-keymap map)
1966 (define-key map "\r" 'gdb-edit-register-value)
1967 (define-key map [mouse-2] 'gdb-edit-register-value)
1968 (define-key map " " 'gdb-all-registers)
1969 (define-key map "q" 'kill-this-buffer)
1972 (defun gdb-registers-mode ()
1973 "Major mode for gdb registers.
1975 \\{gdb-registers-mode-map}"
1976 (kill-all-local-variables)
1977 (setq major-mode 'gdb-registers-mode)
1978 (setq mode-name "Registers")
1979 (setq buffer-read-only t)
1980 (use-local-map gdb-registers-mode-map)
1981 (run-mode-hooks 'gdb-registers-mode-hook)
1982 (if (string-equal gdb-version "pre-6.4")
1984 (if gdb-all-registers (setq mode-name "Registers:All"))
1985 'gdb-invalidate-registers)
1986 'gdb-invalidate-registers-1))
1988 (defun gdb-registers-buffer-name ()
1989 (with-current-buffer gud-comint-buffer
1990 (concat "*registers of " (gdb-get-target-string) "*")))
1992 (defun gdb-display-registers-buffer ()
1993 "Display integer register contents."
1996 (gdb-get-create-buffer 'gdb-registers-buffer)))
1998 (defun gdb-frame-registers-buffer ()
1999 "Display integer register contents in a new frame."
2001 (let ((special-display-regexps (append special-display-regexps '(".*")))
2002 (special-display-frame-alist gdb-frame-parameters))
2003 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
2005 (defun gdb-all-registers ()
2006 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2008 (when (string-equal gdb-version "pre-6.4")
2009 (if gdb-all-registers
2011 (setq gdb-all-registers nil)
2012 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
2013 (setq mode-name "Registers")))
2014 (setq gdb-all-registers t)
2015 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
2016 (setq mode-name "Registers:All")))
2017 (message (format "Display of floating-point registers %sabled"
2018 (if gdb-all-registers "en" "dis")))
2019 (gdb-invalidate-registers)))
2024 (defcustom gdb-memory-repeat-count 32
2025 "Number of data items in memory window."
2030 (defcustom gdb-memory-format "x"
2031 "Display format of data items in memory window."
2032 :type '(choice (const :tag "Hexadecimal" "x")
2033 (const :tag "Signed decimal" "d")
2034 (const :tag "Unsigned decimal" "u")
2035 (const :tag "Octal" "o")
2036 (const :tag "Binary" "t"))
2040 (defcustom gdb-memory-unit "w"
2041 "Unit size of data items in memory window."
2042 :type '(choice (const :tag "Byte" "b")
2043 (const :tag "Halfword" "h")
2044 (const :tag "Word" "w")
2045 (const :tag "Giant word" "g"))
2049 (gdb-set-buffer-rules 'gdb-memory-buffer
2050 'gdb-memory-buffer-name
2053 (def-gdb-auto-updated-buffer gdb-memory-buffer
2054 gdb-invalidate-memory
2055 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2056 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2057 gdb-read-memory-handler
2058 gdb-read-memory-custom)
2060 (defun gdb-read-memory-custom ()
2062 (goto-char (point-min))
2063 (if (looking-at "0x[[:xdigit:]]+")
2064 (setq gdb-memory-address (match-string 0)))))
2066 (defvar gdb-memory-mode-map
2067 (let ((map (make-sparse-keymap)))
2068 (suppress-keymap map)
2069 (define-key map "q" 'kill-this-buffer)
2072 (defun gdb-memory-set-address (event)
2073 "Set the start memory address."
2075 (save-selected-window
2076 (select-window (posn-window (event-start event)))
2077 (let ((arg (read-from-minibuffer "Memory address: ")))
2078 (setq gdb-memory-address arg))
2079 (gdb-invalidate-memory)))
2081 (defun gdb-memory-set-repeat-count (event)
2082 "Set the number of data items in memory window."
2084 (save-selected-window
2085 (select-window (posn-window (event-start event)))
2086 (let* ((arg (read-from-minibuffer "Repeat count: "))
2087 (count (string-to-number arg)))
2089 (error "Positive numbers only")
2090 (customize-set-variable 'gdb-memory-repeat-count count)
2091 (gdb-invalidate-memory)))))
2093 (defun gdb-memory-format-binary ()
2094 "Set the display format to binary."
2096 (customize-set-variable 'gdb-memory-format "t")
2097 (gdb-invalidate-memory))
2099 (defun gdb-memory-format-octal ()
2100 "Set the display format to octal."
2102 (customize-set-variable 'gdb-memory-format "o")
2103 (gdb-invalidate-memory))
2105 (defun gdb-memory-format-unsigned ()
2106 "Set the display format to unsigned decimal."
2108 (customize-set-variable 'gdb-memory-format "u")
2109 (gdb-invalidate-memory))
2111 (defun gdb-memory-format-signed ()
2112 "Set the display format to decimal."
2114 (customize-set-variable 'gdb-memory-format "d")
2115 (gdb-invalidate-memory))
2117 (defun gdb-memory-format-hexadecimal ()
2118 "Set the display format to hexadecimal."
2120 (customize-set-variable 'gdb-memory-format "x")
2121 (gdb-invalidate-memory))
2123 (defvar gdb-memory-format-map
2124 (let ((map (make-sparse-keymap)))
2125 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2127 "Keymap to select format in the header line.")
2129 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2130 "Menu of display formats in the header line.")
2132 (define-key gdb-memory-format-menu [binary]
2133 '(menu-item "Binary" gdb-memory-format-binary
2134 :button (:radio . (equal gdb-memory-format "t"))))
2135 (define-key gdb-memory-format-menu [octal]
2136 '(menu-item "Octal" gdb-memory-format-octal
2137 :button (:radio . (equal gdb-memory-format "o"))))
2138 (define-key gdb-memory-format-menu [unsigned]
2139 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2140 :button (:radio . (equal gdb-memory-format "u"))))
2141 (define-key gdb-memory-format-menu [signed]
2142 '(menu-item "Signed Decimal" gdb-memory-format-signed
2143 :button (:radio . (equal gdb-memory-format "d"))))
2144 (define-key gdb-memory-format-menu [hexadecimal]
2145 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2146 :button (:radio . (equal gdb-memory-format "x"))))
2148 (defun gdb-memory-format-menu (event)
2150 (x-popup-menu event gdb-memory-format-menu))
2152 (defun gdb-memory-format-menu-1 (event)
2154 (save-selected-window
2155 (select-window (posn-window (event-start event)))
2156 (let* ((selection (gdb-memory-format-menu event))
2157 (binding (and selection (lookup-key gdb-memory-format-menu
2158 (vector (car selection))))))
2159 (if binding (call-interactively binding)))))
2161 (defun gdb-memory-unit-giant ()
2162 "Set the unit size to giant words (eight bytes)."
2164 (customize-set-variable 'gdb-memory-unit "g")
2165 (gdb-invalidate-memory))
2167 (defun gdb-memory-unit-word ()
2168 "Set the unit size to words (four bytes)."
2170 (customize-set-variable 'gdb-memory-unit "w")
2171 (gdb-invalidate-memory))
2173 (defun gdb-memory-unit-halfword ()
2174 "Set the unit size to halfwords (two bytes)."
2176 (customize-set-variable 'gdb-memory-unit "h")
2177 (gdb-invalidate-memory))
2179 (defun gdb-memory-unit-byte ()
2180 "Set the unit size to bytes."
2182 (customize-set-variable 'gdb-memory-unit "b")
2183 (gdb-invalidate-memory))
2185 (defvar gdb-memory-unit-map
2186 (let ((map (make-sparse-keymap)))
2187 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2189 "Keymap to select units in the header line.")
2191 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2192 "Menu of units in the header line.")
2194 (define-key gdb-memory-unit-menu [giantwords]
2195 '(menu-item "Giant words" gdb-memory-unit-giant
2196 :button (:radio . (equal gdb-memory-unit "g"))))
2197 (define-key gdb-memory-unit-menu [words]
2198 '(menu-item "Words" gdb-memory-unit-word
2199 :button (:radio . (equal gdb-memory-unit "w"))))
2200 (define-key gdb-memory-unit-menu [halfwords]
2201 '(menu-item "Halfwords" gdb-memory-unit-halfword
2202 :button (:radio . (equal gdb-memory-unit "h"))))
2203 (define-key gdb-memory-unit-menu [bytes]
2204 '(menu-item "Bytes" gdb-memory-unit-byte
2205 :button (:radio . (equal gdb-memory-unit "b"))))
2207 (defun gdb-memory-unit-menu (event)
2209 (x-popup-menu event gdb-memory-unit-menu))
2211 (defun gdb-memory-unit-menu-1 (event)
2213 (save-selected-window
2214 (select-window (posn-window (event-start event)))
2215 (let* ((selection (gdb-memory-unit-menu event))
2216 (binding (and selection (lookup-key gdb-memory-unit-menu
2217 (vector (car selection))))))
2218 (if binding (call-interactively binding)))))
2220 ;;from make-mode-line-mouse-map
2221 (defun gdb-make-header-line-mouse-map (mouse function) "\
2222 Return a keymap with single entry for mouse key MOUSE on the header line.
2223 MOUSE is defined to run function FUNCTION with no args in the buffer
2224 corresponding to the mode line clicked."
2225 (let ((map (make-sparse-keymap)))
2226 (define-key map (vector 'header-line mouse) function)
2227 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2230 (defvar gdb-memory-font-lock-keywords
2231 '(;; <__function.name+n>
2232 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2234 "Font lock keywords used in `gdb-memory-mode'.")
2236 (defun gdb-memory-mode ()
2237 "Major mode for examining memory.
2239 \\{gdb-memory-mode-map}"
2240 (kill-all-local-variables)
2241 (setq major-mode 'gdb-memory-mode)
2242 (setq mode-name "Memory")
2243 (setq buffer-read-only t)
2244 (use-local-map gdb-memory-mode-map)
2245 (setq header-line-format
2251 'face font-lock-warning-face
2252 'help-echo "mouse-1: Decrement address"
2253 'mouse-face 'mode-line-highlight
2255 (gdb-make-header-line-mouse-map
2257 #'(lambda () (interactive)
2258 (let ((gdb-memory-address
2259 ;; Let GDB do the arithmetic.
2261 gdb-memory-address " - "
2263 (* gdb-memory-repeat-count
2264 (cond ((string= gdb-memory-unit "b") 1)
2265 ((string= gdb-memory-unit "h") 2)
2266 ((string= gdb-memory-unit "w") 4)
2267 ((string= gdb-memory-unit "g") 8)))))))
2268 (gdb-invalidate-memory)))))
2271 'face font-lock-warning-face
2272 'help-echo "mouse-1: Increment address"
2273 'mouse-face 'mode-line-highlight
2274 'local-map (gdb-make-header-line-mouse-map
2276 #'(lambda () (interactive)
2277 (let ((gdb-memory-address nil))
2278 (gdb-invalidate-memory)))))
2280 (propertize gdb-memory-address
2281 'face font-lock-warning-face
2282 'help-echo "mouse-1: Set memory address"
2283 'mouse-face 'mode-line-highlight
2284 'local-map (gdb-make-header-line-mouse-map
2286 #'gdb-memory-set-address))
2288 (propertize (number-to-string gdb-memory-repeat-count)
2289 'face font-lock-warning-face
2290 'help-echo "mouse-1: Set repeat count"
2291 'mouse-face 'mode-line-highlight
2292 'local-map (gdb-make-header-line-mouse-map
2294 #'gdb-memory-set-repeat-count))
2296 (propertize gdb-memory-format
2297 'face font-lock-warning-face
2298 'help-echo "mouse-3: Select display format"
2299 'mouse-face 'mode-line-highlight
2300 'local-map gdb-memory-format-map)
2302 (propertize gdb-memory-unit
2303 'face font-lock-warning-face
2304 'help-echo "mouse-3: Select unit size"
2305 'mouse-face 'mode-line-highlight
2306 'local-map gdb-memory-unit-map))))
2307 (set (make-local-variable 'font-lock-defaults)
2308 '(gdb-memory-font-lock-keywords))
2309 (run-mode-hooks 'gdb-memory-mode-hook)
2310 'gdb-invalidate-memory)
2312 (defun gdb-memory-buffer-name ()
2313 (with-current-buffer gud-comint-buffer
2314 (concat "*memory of " (gdb-get-target-string) "*")))
2316 (defun gdb-display-memory-buffer ()
2317 "Display memory contents."
2320 (gdb-get-create-buffer 'gdb-memory-buffer)))
2322 (defun gdb-frame-memory-buffer ()
2323 "Display memory contents in a new frame."
2325 (let ((special-display-regexps (append special-display-regexps '(".*")))
2326 (special-display-frame-alist gdb-frame-parameters))
2327 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
2332 (gdb-set-buffer-rules 'gdb-locals-buffer
2333 'gdb-locals-buffer-name
2336 (def-gdb-auto-update-trigger gdb-invalidate-locals
2337 (gdb-get-buffer 'gdb-locals-buffer)
2338 "server info locals\n"
2339 gdb-info-locals-handler)
2341 (defvar gdb-locals-watch-map
2342 (let ((map (make-sparse-keymap)))
2343 (define-key map "\r" '(lambda () (interactive)
2346 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2347 (mouse-set-point event)
2351 "Keymap to create watch expression of a complex data type local variable.")
2353 (defconst gdb-struct-string
2354 (concat (propertize "[struct/union]"
2355 'mouse-face 'highlight
2356 'help-echo "mouse-2: create watch expression"
2357 'local-map gdb-locals-watch-map) "\n"))
2359 (defconst gdb-array-string
2360 (concat " " (propertize "[array]"
2361 'mouse-face 'highlight
2362 'help-echo "mouse-2: create watch expression"
2363 'local-map gdb-locals-watch-map) "\n"))
2365 ;; Abbreviate for arrays and structures.
2366 ;; These can be expanded using gud-display.
2367 (defun gdb-info-locals-handler ()
2368 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2369 gdb-pending-triggers))
2370 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2371 (with-current-buffer buf
2372 (goto-char (point-min))
2373 (while (re-search-forward "^[ }].*\n" nil t)
2374 (replace-match "" nil nil))
2375 (goto-char (point-min))
2376 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2377 (replace-match gdb-struct-string nil nil))
2378 (goto-char (point-min))
2379 (while (re-search-forward "\\s-*{.*\n" nil t)
2380 (replace-match gdb-array-string nil nil))))
2381 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2383 (with-current-buffer buf
2384 (let* ((window (get-buffer-window buf 0))
2385 (p (window-point window))
2386 (buffer-read-only nil))
2388 (insert-buffer-substring (gdb-get-create-buffer
2389 'gdb-partial-output-buffer))
2390 (set-window-point window p)))))
2391 (run-hooks 'gdb-info-locals-hook))
2393 (defvar gdb-locals-mode-map
2394 (let ((map (make-sparse-keymap)))
2395 (suppress-keymap map)
2396 (define-key map "q" 'kill-this-buffer)
2399 (defun gdb-locals-mode ()
2400 "Major mode for gdb locals.
2402 \\{gdb-locals-mode-map}"
2403 (kill-all-local-variables)
2404 (setq major-mode 'gdb-locals-mode)
2405 (setq mode-name (concat "Locals:" gdb-selected-frame))
2406 (setq buffer-read-only t)
2407 (use-local-map gdb-locals-mode-map)
2408 (set (make-local-variable 'font-lock-defaults)
2409 '(gdb-locals-font-lock-keywords))
2410 (run-mode-hooks 'gdb-locals-mode-hook)
2411 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2412 (if (string-equal gdb-version "pre-6.4")
2413 'gdb-invalidate-locals
2414 'gdb-invalidate-locals-1)
2415 'gdbmi-invalidate-locals))
2417 (defun gdb-locals-buffer-name ()
2418 (with-current-buffer gud-comint-buffer
2419 (concat "*locals of " (gdb-get-target-string) "*")))
2421 (defun gdb-display-locals-buffer ()
2422 "Display local variables of current stack and their values."
2425 (gdb-get-create-buffer 'gdb-locals-buffer)))
2427 (defun gdb-frame-locals-buffer ()
2428 "Display local variables of current stack and their values in a new frame."
2430 (let ((special-display-regexps (append special-display-regexps '(".*")))
2431 (special-display-frame-alist gdb-frame-parameters))
2432 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
2435 ;;;; Window management
2436 (defun gdb-display-buffer (buf &optional size)
2437 (let ((answer (get-buffer-window buf 0))
2440 (display-buffer buf nil 0) ;Raise the frame if necessary.
2441 ;; The buffer is not yet displayed.
2442 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2443 (let ((window (get-lru-window)))
2445 (not (eq window (get-buffer-window gud-comint-buffer))))
2447 (set-window-buffer window buf)
2448 (setq answer window))
2449 (setq must-split t)))
2451 (let* ((largest (get-largest-window))
2452 (cur-size (window-height largest))
2453 (new-size (and size (< size cur-size) (- cur-size size))))
2454 (setq answer (split-window largest new-size))
2455 (set-window-buffer answer buf)
2456 (set-window-dedicated-p answer t)))
2460 ;;; Shared keymap initialization:
2462 (let ((menu (make-sparse-keymap "GDB-Windows")))
2463 (define-key gud-menu-map [displays]
2464 `(menu-item "GDB-Windows" ,menu
2465 :visible (memq gud-minor-mode '(gdbmi gdba))))
2466 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2467 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2468 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2469 (define-key menu [disassembly]
2470 '("Disassembly" . gdb-display-assembler-buffer))
2471 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2472 (define-key menu [inferior]
2473 '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
2474 :enable gdb-use-inferior-io-buffer))
2475 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2476 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2477 (define-key menu [breakpoints]
2478 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2480 (let ((menu (make-sparse-keymap "GDB-Frames")))
2481 (define-key gud-menu-map [frames]
2482 `(menu-item "GDB-Frames" ,menu
2483 :visible (memq gud-minor-mode '(gdbmi gdba))))
2484 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2485 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2486 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2487 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2488 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2489 (define-key menu [inferior]
2490 '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
2491 :enable gdb-use-inferior-io-buffer))
2492 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2493 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2494 (define-key menu [breakpoints]
2495 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2497 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2498 (define-key gud-menu-map [ui]
2499 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2500 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2501 (define-key menu [gdb-use-inferior-io]
2502 '(menu-item "Separate inferior IO" gdb-use-inferior-io-buffer
2503 :visible (eq gud-minor-mode 'gdba)
2504 :help "Toggle separate IO for inferior."
2505 :button (:toggle . gdb-use-inferior-io-buffer)))
2506 (define-key menu [gdb-many-windows]
2507 '(menu-item "Display Other Windows" gdb-many-windows
2508 :help "Toggle display of locals, stack and breakpoint information"
2509 :button (:toggle . gdb-many-windows)))
2510 (define-key menu [gdb-restore-windows]
2511 '(menu-item "Restore Window Layout" gdb-restore-windows
2512 :help "Restore standard layout for debug session.")))
2514 (defun gdb-frame-gdb-buffer ()
2515 "Display GUD buffer in a new frame."
2517 (let ((special-display-regexps (append special-display-regexps '(".*")))
2518 (special-display-frame-alist gdb-frame-parameters)
2519 (same-window-regexps nil))
2520 (display-buffer gud-comint-buffer)))
2522 (defun gdb-display-gdb-buffer ()
2523 "Display GUD buffer."
2525 (let ((same-window-regexps nil))
2526 (pop-to-buffer gud-comint-buffer)))
2528 (defun gdb-set-window-buffer (name)
2529 (set-window-buffer (selected-window) (get-buffer name))
2530 (set-window-dedicated-p (selected-window) t))
2532 (defun gdb-setup-windows ()
2533 "Layout the window pattern for `gdb-many-windows'."
2534 (gdb-display-locals-buffer)
2535 (gdb-display-stack-buffer)
2536 (delete-other-windows)
2537 (gdb-display-breakpoints-buffer)
2538 (delete-other-windows)
2540 (pop-to-buffer gud-comint-buffer)
2541 (split-window nil ( / ( * (window-height) 3) 4))
2542 (split-window nil ( / (window-height) 3))
2543 (split-window-horizontally)
2545 (gdb-set-window-buffer (gdb-locals-buffer-name))
2548 (if gud-last-last-frame
2549 (gud-find-file (car gud-last-last-frame))
2550 (gud-find-file gdb-main-file)))
2551 (when gdb-use-inferior-io-buffer
2552 (split-window-horizontally)
2554 (gdb-set-window-buffer
2555 (gdb-get-create-buffer 'gdb-inferior-io)))
2557 (gdb-set-window-buffer (gdb-stack-buffer-name))
2558 (split-window-horizontally)
2560 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2563 (defcustom gdb-many-windows nil
2564 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2565 In this case it starts with two windows: one displaying the GUD
2566 buffer and the other with the source file with the main routine
2567 of the inferior. Non-nil means display the layout shown for
2573 (defun gdb-many-windows (arg)
2574 "Toggle the number of windows in the basic arrangement.
2575 With arg, display additional buffers iff arg is positive."
2577 (setq gdb-many-windows
2579 (not gdb-many-windows)
2580 (> (prefix-numeric-value arg) 0)))
2581 (message (format "Display of other windows %sabled"
2582 (if gdb-many-windows "en" "dis")))
2583 (if (and gud-comint-buffer
2584 (buffer-name gud-comint-buffer))
2586 (gdb-restore-windows)
2589 (defun gdb-restore-windows ()
2590 "Restore the basic arrangement of windows used by gdba.
2591 This arrangement depends on the value of `gdb-many-windows'."
2593 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2594 (delete-other-windows)
2595 (if gdb-many-windows
2597 (when (or gud-last-last-frame gdb-show-main)
2601 (if gud-last-last-frame
2602 (gud-find-file (car gud-last-last-frame))
2603 (gud-find-file gdb-main-file)))
2607 "Exit a debugging session cleanly.
2608 Kills the gdb buffers and resets the source buffers."
2609 (dolist (buffer (buffer-list))
2610 (unless (eq buffer gud-comint-buffer)
2611 (with-current-buffer buffer
2612 (if (memq gud-minor-mode '(gdbmi gdba))
2613 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2615 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2616 (setq gud-minor-mode nil)
2617 (kill-local-variable 'tool-bar-map)
2618 (kill-local-variable 'gdb-define-alist))))))
2619 (when (markerp gdb-overlay-arrow-position)
2620 (move-marker gdb-overlay-arrow-position nil)
2621 (setq gdb-overlay-arrow-position nil))
2622 (setq overlay-arrow-variable-list
2623 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2624 (setq gud-running nil)
2625 (setq gdb-active-process nil)
2626 (setq gdb-var-list nil)
2627 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2629 (defun gdb-source-info ()
2630 "Find the source file where the program starts and displays it with related
2632 (goto-char (point-min))
2633 (if (and (search-forward "Located in " nil t)
2634 (looking-at "\\S-+"))
2635 (setq gdb-main-file (match-string 0)))
2636 (goto-char (point-min))
2637 (if (search-forward "Includes preprocessor macro info." nil t)
2638 (setq gdb-macro-info t))
2639 (if gdb-many-windows
2641 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2643 (let ((pop-up-windows t))
2644 (display-buffer (gud-find-file gdb-main-file))))))
2646 (defun gdb-get-location (bptno line flag)
2647 "Find the directory containing the relevant source file.
2648 Put in buffer and place breakpoint icon."
2649 (goto-char (point-min))
2650 (catch 'file-not-found
2651 (if (search-forward "Located in " nil t)
2652 (when (looking-at "\\S-+")
2653 (delete (cons bptno "File not found") gdb-location-alist)
2654 (push (cons bptno (match-string 0)) gdb-location-alist))
2656 (unless (assoc bptno gdb-location-alist)
2657 (push (cons bptno "File not found") gdb-location-alist)
2658 (message-box "Cannot find source file for breakpoint location.\n\
2659 Add directory to search path for source files using the GDB command, dir."))
2660 (throw 'file-not-found nil))
2661 (with-current-buffer
2662 (find-file-noselect (match-string 0))
2663 (save-current-buffer
2664 (set (make-local-variable 'gud-minor-mode) 'gdba)
2665 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2666 ;; only want one breakpoint icon at each location
2668 (goto-line (string-to-number line))
2669 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2671 (add-hook 'find-file-hook 'gdb-find-file-hook)
2673 (defun gdb-find-file-hook ()
2674 "Set up buffer for debugging if file is part of the source code
2675 of the current session."
2676 (if (and (buffer-name gud-comint-buffer)
2677 ;; in case gud or gdb-ui is just loaded
2679 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2681 (if (member buffer-file-name gdb-source-file-list)
2682 (with-current-buffer (find-buffer-visiting buffer-file-name)
2683 (set (make-local-variable 'gud-minor-mode) 'gdba)
2684 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2687 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2688 "Put string PUTSTRING in front of POS in the current buffer.
2689 PUTSTRING is displayed by putting an overlay into the current buffer with a
2690 `before-string' string that has a `display' property whose value is
2692 (let ((string (make-string 1 ?x))
2693 (buffer (current-buffer)))
2694 (setq putstring (copy-sequence putstring))
2695 (let ((overlay (make-overlay pos pos buffer))
2697 (list (list 'margin 'left-margin) putstring))))
2698 (put-text-property 0 1 'display prop string)
2700 (add-text-properties 0 1 sprops string))
2701 (overlay-put overlay 'put-break t)
2702 (overlay-put overlay 'before-string string))))
2704 ;;from remove-images
2705 (defun gdb-remove-strings (start end &optional buffer)
2706 "Remove strings between START and END in BUFFER.
2707 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2708 BUFFER nil or omitted means use the current buffer."
2710 (setq buffer (current-buffer)))
2711 (dolist (overlay (overlays-in start end))
2712 (when (overlay-get overlay 'put-break)
2713 (delete-overlay overlay))))
2715 (defun gdb-put-breakpoint-icon (enabled bptno)
2716 (let ((start (- (line-beginning-position) 1))
2717 (end (+ (line-end-position) 1))
2718 (putstring (if enabled "B" "b"))
2719 (source-window (get-buffer-window (current-buffer) 0)))
2720 (add-text-properties
2721 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2724 (add-text-properties
2725 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2726 (add-text-properties
2727 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2728 (gdb-remove-breakpoint-icons start end)
2729 (if (display-images-p)
2730 (if (>= (or left-fringe-width
2731 (if source-window (car (window-fringes source-window)))
2732 gdb-buffer-fringe-width) 8)
2735 `(left-fringe breakpoint
2738 'breakpoint-disabled))
2740 'gdb-enabled enabled)
2741 (when (< left-margin-width 2)
2742 (save-current-buffer
2743 (setq left-margin-width 2)
2747 left-margin-width right-margin-width))))
2750 (or breakpoint-enabled-icon
2751 (setq breakpoint-enabled-icon
2752 (find-image `((:type xpm :data
2753 ,breakpoint-xpm-data
2754 :ascent 100 :pointer hand)
2756 ,breakpoint-enabled-pbm-data
2757 :ascent 100 :pointer hand)))))
2758 (or breakpoint-disabled-icon
2759 (setq breakpoint-disabled-icon
2760 (find-image `((:type xpm :data
2761 ,breakpoint-xpm-data
2762 :conversion disabled
2763 :ascent 100 :pointer hand)
2765 ,breakpoint-disabled-pbm-data
2766 :ascent 100 :pointer hand))))))
2770 (when (< left-margin-width 2)
2771 (save-current-buffer
2772 (setq left-margin-width 2)
2773 (let ((window (get-buffer-window (current-buffer) 0)))
2776 window left-margin-width right-margin-width)))))
2778 (propertize putstring
2779 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2782 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2783 (gdb-remove-strings start end)
2784 (if (display-images-p)
2785 (remove-images start end))
2787 (setq left-margin-width 0)
2788 (let ((window (get-buffer-window (current-buffer) 0)))
2791 window left-margin-width right-margin-width)))))
2795 ;; Assembler buffer.
2797 (gdb-set-buffer-rules 'gdb-assembler-buffer
2798 'gdb-assembler-buffer-name
2799 'gdb-assembler-mode)
2801 (def-gdb-auto-update-handler gdb-assembler-handler
2802 gdb-invalidate-assembler
2803 gdb-assembler-buffer
2804 gdb-assembler-custom)
2806 (defun gdb-assembler-custom ()
2807 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2808 (pos 1) (address) (flag) (bptno))
2809 (with-current-buffer buffer
2811 (if (not (equal gdb-frame-address "main"))
2813 (goto-char (point-min))
2814 (if (and gdb-frame-address
2815 (search-forward gdb-frame-address nil t))
2819 (or gdb-overlay-arrow-position
2820 (setq gdb-overlay-arrow-position (make-marker)))
2821 (set-marker gdb-overlay-arrow-position
2822 (point) (current-buffer))))))
2823 ;; remove all breakpoint-icons in assembler buffer before updating.
2824 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2825 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2826 (goto-char (point-min))
2827 (while (< (point) (- (point-max) 1))
2829 (if (looking-at "[^\t].*?breakpoint")
2832 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2833 (setq bptno (match-string 1))
2834 (setq flag (char-after (match-beginning 2)))
2835 (setq address (match-string 3))
2836 (with-current-buffer buffer
2838 (goto-char (point-min))
2839 (if (search-forward address nil t)
2840 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2841 (if (not (equal gdb-frame-address "main"))
2842 (with-current-buffer buffer
2843 (set-window-point (get-buffer-window buffer 0) pos)))))
2845 (defvar gdb-assembler-mode-map
2846 (let ((map (make-sparse-keymap)))
2847 (suppress-keymap map)
2848 (define-key map "q" 'kill-this-buffer)
2851 (defvar gdb-assembler-font-lock-keywords
2852 '(;; <__function.name+n>
2853 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2854 (1 font-lock-function-name-face))
2855 ;; 0xNNNNNNNN <__function.name+n>: opcode
2856 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2857 (4 font-lock-keyword-face))
2858 ;; %register(at least i386)
2859 ("%\\sw+" . font-lock-variable-name-face)
2860 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2861 (1 font-lock-comment-face)
2862 (2 font-lock-function-name-face))
2863 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2864 "Font lock keywords used in `gdb-assembler-mode'.")
2866 (defun gdb-assembler-mode ()
2867 "Major mode for viewing code assembler.
2869 \\{gdb-assembler-mode-map}"
2870 (kill-all-local-variables)
2871 (setq major-mode 'gdb-assembler-mode)
2872 (setq mode-name (concat "Machine:" gdb-selected-frame))
2873 (setq gdb-overlay-arrow-position nil)
2874 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2875 (setq fringes-outside-margins t)
2876 (setq buffer-read-only t)
2877 (use-local-map gdb-assembler-mode-map)
2878 (gdb-invalidate-assembler)
2879 (set (make-local-variable 'font-lock-defaults)
2880 '(gdb-assembler-font-lock-keywords))
2881 (run-mode-hooks 'gdb-assembler-mode-hook)
2882 'gdb-invalidate-assembler)
2884 (defun gdb-assembler-buffer-name ()
2885 (with-current-buffer gud-comint-buffer
2886 (concat "*disassembly of " (gdb-get-target-string) "*")))
2888 (defun gdb-display-assembler-buffer ()
2889 "Display disassembly view."
2891 (setq gdb-previous-frame nil)
2893 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2895 (defun gdb-frame-assembler-buffer ()
2896 "Display disassembly view in a new frame."
2898 (setq gdb-previous-frame nil)
2899 (let ((special-display-regexps (append special-display-regexps '(".*")))
2900 (special-display-frame-alist gdb-frame-parameters))
2901 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2903 ;; modified because if gdb-frame-address has changed value a new command
2904 ;; must be enqueued to update the buffer with the new output
2905 (defun gdb-invalidate-assembler (&optional ignored)
2906 (if (gdb-get-buffer 'gdb-assembler-buffer)
2908 (unless (and gdb-selected-frame
2909 (string-equal gdb-selected-frame gdb-previous-frame))
2910 (if (or (not (member 'gdb-invalidate-assembler
2911 gdb-pending-triggers))
2912 (not (string-equal gdb-frame-address
2913 gdb-previous-frame-address)))
2915 ;; take previous disassemble command, if any, off the queue
2916 (with-current-buffer gud-comint-buffer
2917 (let ((queue gdb-input-queue))
2918 (dolist (item queue)
2919 (if (equal (cdr item) '(gdb-assembler-handler))
2920 (setq gdb-input-queue
2921 (delete item gdb-input-queue))))))
2924 (concat gdb-server-prefix "disassemble "
2925 (if (member gdb-frame-address '(nil "main")) nil "0x")
2926 gdb-frame-address "\n")
2927 'gdb-assembler-handler))
2928 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2929 (setq gdb-previous-frame-address gdb-frame-address)
2930 (setq gdb-previous-frame gdb-selected-frame)))))))
2932 (defun gdb-get-selected-frame ()
2933 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2936 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2937 (push 'gdb-get-selected-frame
2938 gdb-pending-triggers))))
2940 (defun gdb-frame-handler ()
2941 (setq gdb-pending-triggers
2942 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2943 (goto-char (point-min))
2944 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
2945 (setq gdb-frame-number (match-string 1)))
2946 (goto-char (point-min))
2947 (if (re-search-forward
2948 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
2950 (setq gdb-selected-frame (match-string 2))
2951 (if (gdb-get-buffer 'gdb-locals-buffer)
2952 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2953 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2954 (if (gdb-get-buffer 'gdb-assembler-buffer)
2955 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
2956 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2957 (setq gdb-frame-address (match-string 1))))
2958 (goto-char (point-min))
2959 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2960 (setq gdb-current-language (match-string 1)))
2961 (gdb-invalidate-assembler))
2964 ;; Code specific to GDB 6.4
2965 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
2967 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
2968 "Create list of source files for current GDB session."
2969 (goto-char (point-min))
2970 (while (re-search-forward gdb-source-file-regexp-1 nil t)
2971 (push (match-string 1) gdb-source-file-list))
2972 (dolist (buffer (buffer-list))
2973 (with-current-buffer buffer
2974 (when (member buffer-file-name gdb-source-file-list)
2975 (set (make-local-variable 'gud-minor-mode) 'gdba)
2976 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2977 (when gud-tooltip-mode
2978 (make-local-variable 'gdb-define-alist)
2979 (gdb-create-define-alist)
2980 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
2982 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
2983 (defun gdb-var-list-children-1 (varnum)
2985 (list (concat "server interpreter mi \"-var-update " varnum "\"\n")
2988 (list (concat "server interpreter mi \"-var-list-children --all-values "
2990 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
2992 (defconst gdb-var-list-children-regexp-1
2993 "name=\"\\(.+?\\)\",exp=\"\\(.+?\\)\",numchild=\"\\(.+?\\)\",\
2994 value=\\(\".*?\"\\),type=\"\\(.+?\\)\"}")
2996 (defun gdb-var-list-children-handler-1 (varnum)
2997 (goto-char (point-min))
2998 (let ((var-list nil))
2999 (catch 'child-already-watched
3000 (dolist (var gdb-var-list)
3001 (if (string-equal varnum (cadr var))
3004 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3005 (let ((varchild (list (match-string 2)
3009 (read (match-string 4))
3011 (dolist (var1 gdb-var-list)
3012 (if (string-equal (cadr var1) (cadr varchild))
3013 (throw 'child-already-watched nil)))
3014 (push varchild var-list))))
3015 (push var var-list)))
3016 (setq gdb-var-changed t)
3017 (setq gdb-var-list (nreverse var-list)))))
3019 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3020 (defun gdb-var-update-1 ()
3021 (if (not (member 'gdb-var-update gdb-pending-triggers))
3025 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
3026 "server interpreter mi \"-var-update --all-values *\"\n"
3027 "-var-update --all-values *\n")
3028 'gdb-var-update-handler-1))
3029 (push 'gdb-var-update gdb-pending-triggers))))
3031 (defconst gdb-var-update-regexp-1 "name=\"\\(.*?\\)\",value=\\(\".*?\"\\),")
3033 (defun gdb-var-update-handler-1 ()
3034 (goto-char (point-min))
3035 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3036 (let ((varnum (match-string 1)))
3039 (dolist (var gdb-var-list)
3040 (if (string-equal varnum (cadr var))
3042 (setcar (nthcdr 5 var) t)
3043 (setcar (nthcdr 4 var) (read (match-string 2)))
3044 (setcar (nthcdr num gdb-var-list) var)
3045 (throw 'var-found1 nil)))
3046 (setq num (+ num 1))))))
3047 (setq gdb-var-changed t))
3048 (setq gdb-pending-triggers
3049 (delq 'gdb-var-update gdb-pending-triggers))
3050 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3051 ;; dummy command to update speedbar at right time
3052 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
3053 ;; keep gdb-pending-triggers non-nil till end
3054 (push 'gdb-speedbar-timer gdb-pending-triggers)))
3056 ;; Registers buffer.
3058 (gdb-set-buffer-rules 'gdb-registers-buffer
3059 'gdb-registers-buffer-name
3060 'gdb-registers-mode)
3062 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3063 (gdb-get-buffer 'gdb-registers-buffer)
3064 (if (eq gud-minor-mode 'gdba)
3065 "server interpreter mi \"-data-list-register-values x\"\n"
3066 "-data-list-register-values x\n")
3067 gdb-data-list-register-values-handler)
3069 (defconst gdb-data-list-register-values-regexp
3070 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
3072 (defun gdb-data-list-register-values-handler ()
3073 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3074 gdb-pending-triggers))
3075 (goto-char (point-min))
3076 (if (re-search-forward gdb-error-regexp nil t)
3077 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3078 (let ((buffer-read-only nil))
3080 (insert (match-string 1))
3081 (goto-char (point-min))))
3082 (let ((register-list (reverse gdb-register-names))
3083 (register nil) (register-string nil) (register-values nil))
3084 (goto-char (point-min))
3085 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3086 (setq register (pop register-list))
3087 (setq register-string (concat register "\t" (match-string 2) "\n"))
3088 (if (member (match-string 1) gdb-changed-registers)
3089 (put-text-property 0 (length register-string)
3090 'face 'font-lock-warning-face
3092 (setq register-values
3093 (concat register-values register-string)))
3094 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3095 (with-current-buffer buf
3096 (let ((p (window-point (get-buffer-window buf 0)))
3097 (buffer-read-only nil))
3099 (insert register-values)
3100 (set-window-point (get-buffer-window buf 0) p))))))
3101 (gdb-data-list-register-values-custom))
3103 (defun gdb-data-list-register-values-custom ()
3104 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3106 (let ((buffer-read-only nil)
3108 (goto-char (point-min))
3109 (while (< (point) (point-max))
3110 (setq start (line-beginning-position))
3111 (setq end (line-end-position))
3112 (when (looking-at "^[^\t]+")
3113 (unless (string-equal (match-string 0) "No registers.")
3114 (put-text-property start (match-end 0)
3115 'face font-lock-variable-name-face)
3116 (add-text-properties start end
3117 '(help-echo "mouse-2: edit value"
3118 mouse-face highlight))))
3119 (forward-line 1))))))
3121 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3122 (defun gdb-get-changed-registers ()
3123 (if (not (member 'gdb-get-changed-registers gdb-pending-triggers))
3127 (if (eq gud-minor-mode 'gdba)
3128 "server interpreter mi -data-list-changed-registers\n"
3129 "-data-list-changed-registers\n")
3130 'gdb-get-changed-registers-handler))
3131 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3133 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3135 (defun gdb-get-changed-registers-handler ()
3136 (setq gdb-pending-triggers
3137 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3138 (setq gdb-changed-registers nil)
3139 (goto-char (point-min))
3140 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3141 (push (match-string 1) gdb-changed-registers)))
3146 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3147 (gdb-set-buffer-rules 'gdb-locals-buffer
3148 'gdb-locals-buffer-name
3151 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3152 (gdb-get-buffer 'gdb-locals-buffer)
3153 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3154 gdb-stack-list-locals-handler)
3156 (defconst gdb-stack-list-locals-regexp
3157 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3159 (defvar gdb-locals-watch-map-1
3160 (let ((map (make-sparse-keymap)))
3161 (define-key map [mouse-2] 'gud-watch)
3163 "Keymap to create watch expression of a complex data type local variable.")
3165 ;; Dont display values of arrays or structures.
3166 ;; These can be expanded using gud-watch.
3167 (defun gdb-stack-list-locals-handler ()
3168 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3169 gdb-pending-triggers))
3170 (let (local locals-list)
3171 (goto-char (point-min))
3172 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3173 (let ((local (list (match-string 1)
3176 (if (looking-at ",value=\\(\".*\"\\)}")
3177 (setcar (nthcdr 2 local) (read (match-string 1))))
3178 (push local locals-list)))
3179 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3180 (and buf (with-current-buffer buf
3181 (let* ((window (get-buffer-window buf 0))
3182 (p (window-point window))
3183 (buffer-read-only nil))
3185 (dolist (local locals-list)
3186 (setq name (car local))
3187 (if (or (not (nth 2 local))
3188 (string-match "\\*$" (nth 1 local)))
3189 (add-text-properties 0 (length name)
3190 `(mouse-face highlight
3191 help-echo "mouse-2: create watch expression"
3192 local-map ,gdb-locals-watch-map-1)
3195 (concat name "\t" (nth 1 local)
3196 "\t" (nth 2 local) "\n")))
3197 (set-window-point window p)))))))
3199 (defun gdb-get-register-names ()
3200 "Create a list of register names."
3201 (goto-char (point-min))
3202 (setq gdb-register-names nil)
3203 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3204 (push (match-string 1) gdb-register-names)))
3208 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3209 ;;; gdb-ui.el ends here