1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006
8 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This mode acts as a graphical user interface to GDB. You can interact with
30 ;; GDB through the GUD buffer in the usual way, but there are also further
31 ;; buffers which control the execution and describe the state of your program.
32 ;; It separates the input/output of your program from that of GDB, if
33 ;; required, and watches expressions in the speedbar. It also uses features of
34 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
35 ;; (see the GDB Graphical Interface section in the Emacs info manual).
37 ;; By default, M-x gdb will start the debugger. However, if you have customised
38 ;; gud-gdb-command-name, then start it with M-x gdba.
40 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
41 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
42 ;; You don't need to know about annotations to use this mode as a debugger,
43 ;; but if you are interested developing the mode itself, then see the
44 ;; Annotations section in the GDB info manual.
46 ;; GDB developers plan to make the annotation interface obsolete. A new
47 ;; interface called GDB/MI (machine interface) has been designed to replace
48 ;; it. Some GDB/MI commands are used in this file through the CLI command
49 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
50 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
51 ;; still under development and is part of a process to migrate Emacs from
52 ;; annotations to GDB/MI.
54 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
55 ;; onwards to use watch expressions. It works best with GDB 6.4 where
56 ;; watch expressions will update more quickly.
58 ;;; Windows Platforms:
60 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
61 ;; explicitly in your program if you want timely display of I/O in Emacs.
62 ;; Alternatively you can make the output stream unbuffered, for example, by
66 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
69 ;; and compiling with -DUNBUFFERED while debugging.
73 ;; 1) Strings that are watched don't update in the speedbar when their
75 ;; 2) Watch expressions go out of scope when the inferior is re-run.
76 ;; 3) Cannot handle multiple debug sessions.
80 ;; 1) Use MI command -data-read-memory for memory window.
81 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
82 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
83 ;; 4) With gud-print and gud-pstar, print the variable name in the GUD
84 ;; buffer instead of the value's history number.
91 (defvar speedbar-initial-expansion-list-name)
93 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
94 (defvar gdb-previous-frame-address nil)
95 (defvar gdb-memory-address "main")
96 (defvar gdb-previous-frame nil)
97 (defvar gdb-selected-frame nil)
98 (defvar gdb-frame-number nil)
99 (defvar gdb-current-language nil)
100 (defvar gdb-var-list nil "List of variables in watch window.")
101 (defvar gdb-var-changed nil "Non-nil means that `gdb-var-list' has changed.")
102 (defvar gdb-main-file nil "Source file from which program execution begins.")
103 (defvar gdb-overlay-arrow-position nil)
104 (defvar gdb-server-prefix nil)
105 (defvar gdb-flush-pending-output nil)
106 (defvar gdb-location-alist nil
107 "Alist of breakpoint numbers and full filenames.")
108 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
109 and #define directives otherwise.")
110 (defvar gdb-error "Non-nil when GDB is reporting an error.")
111 (defvar gdb-macro-info nil
112 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
113 (defvar gdb-buffer-fringe-width nil)
115 (defvar gdb-buffer-type nil
116 "One of the symbols bound in `gdb-buffer-rules'.")
117 (make-variable-buffer-local 'gdb-buffer-type)
119 (defvar gdb-input-queue ()
120 "A list of gdb command objects.")
122 (defvar gdb-prompting nil
123 "True when gdb is idle with no pending input.")
125 (defvar gdb-output-sink 'user
126 "The disposition of the output of the current gdb command.
127 Possible values are these symbols:
129 `user' -- gdb output should be copied to the GUD buffer
132 `inferior' -- gdb output should be copied to the inferior-io buffer.
134 `pre-emacs' -- output should be ignored util the post-prompt
135 annotation is received. Then the output-sink
137 `emacs' -- output should be collected in the partial-output-buffer
138 for subsequent processing by a command. This is the
139 disposition of output generated by commands that
140 gdb mode sends to gdb on its own behalf.
141 `post-emacs' -- ignore output until the prompt annotation is
142 received, then go to USER disposition.
144 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
145 \(`user' and `emacs').")
147 (defvar gdb-current-item nil
148 "The most recent command item sent to gdb.")
150 (defvar gdb-pending-triggers '()
151 "A list of trigger functions that have run later than their output
154 (defvar gdb-first-post-prompt nil)
155 (defvar gdb-version nil)
156 (defvar gdb-locals-font-lock-keywords nil)
157 (defvar gdb-source-file-list nil
158 "List of source files for the current executable")
159 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
161 (defvar gdb-locals-font-lock-keywords-1
163 ;; var = (struct struct_tag) value
164 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
165 (1 font-lock-variable-name-face)
166 (3 font-lock-keyword-face)
167 (4 font-lock-type-face))
168 ;; var = (type) value
169 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
170 (1 font-lock-variable-name-face)
171 (3 font-lock-type-face))
173 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
174 (1 font-lock-variable-name-face))
176 "Font lock keywords used in `gdb-local-mode'.")
178 (defvar gdb-locals-font-lock-keywords-2
181 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
182 (1 font-lock-variable-name-face)
183 (3 font-lock-type-face))
185 "Font lock keywords used in `gdb-local-mode'.")
187 ;; Variables for GDB 6.4+
188 (defvar gdb-register-names nil "List of register names.")
189 (defvar gdb-changed-registers nil
190 "List of changed register numbers (strings).")
193 (defun gdba (command-line)
194 "Run gdb on program FILE in buffer *gud-FILE*.
195 The directory containing FILE becomes the initial working directory
196 and source-file directory for your debugger.
198 If `gdb-many-windows' is nil (the default value) then gdb just
199 pops up the GUD buffer unless `gdb-show-main' is t. In this case
200 it starts with two windows: one displaying the GUD buffer and the
201 other with the source file with the main routine of the inferior.
203 If `gdb-many-windows' is t, regardless of the value of
204 `gdb-show-main', the layout below will appear unless
205 `gdb-use-separate-io-buffer' is nil when the source buffer
206 occupies the full width of the frame. Keybindings are given in
209 Watch expressions appear in the speedbar/slowbar.
211 The following commands help control operation :
213 `gdb-many-windows' - Toggle the number of windows gdb uses.
214 `gdb-restore-windows' - To restore the window layout.
216 See Info node `(emacs)GDB Graphical Interface' for a more
217 detailed description of this mode.
220 +--------------------------------------------------------------+
222 +-------------------------------+------------------------------+
223 | GUD buffer (I/O of GDB) | Locals buffer |
227 +-------------------------------+------------------------------+
228 | Source buffer | I/O buffer (of inferior) |
236 +-------------------------------+------------------------------+
237 | Stack buffer | Breakpoints buffer |
238 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
239 | | RET gdb-goto-breakpoint |
240 | | d gdb-delete-breakpoint |
241 +-------------------------------+------------------------------+"
243 (interactive (list (gud-query-cmdline 'gdba)))
245 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
249 (defvar gdb-debug-log nil)
252 (defcustom gdb-enable-debug-log nil
253 "Non-nil means record the process input and output in `gdb-debug-log'."
258 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
259 "Shell command for generating a list of defined macros in a source file.
260 This list is used to display the #define directive associated
261 with an identifier as a tooltip. It works in a debug session with
262 GDB, when gud-tooltip-mode is t.
264 Set `gdb-cpp-define-alist-flags' for any include paths or
270 (defcustom gdb-cpp-define-alist-flags ""
271 "Preprocessor flags for `gdb-cpp-define-alist-program'."
276 (defcustom gdb-show-main nil
277 "Non-nil means display source file containing the main routine at startup.
278 Also display the main routine in the disassembly buffer if present."
283 (defcustom gdb-use-separate-io-buffer nil
284 "Non-nil means display output from the inferior in a separate buffer."
289 (defun gdb-use-separate-io-buffer (arg)
290 "Toggle separate IO for inferior.
291 With arg, use separate IO iff arg is positive."
293 (setq gdb-use-separate-io-buffer
295 (not gdb-use-separate-io-buffer)
296 (> (prefix-numeric-value arg) 0)))
297 (message (format "Separate inferior IO %sabled"
298 (if gdb-use-separate-io-buffer "en" "dis")))
299 (if (and gud-comint-buffer
300 (buffer-name gud-comint-buffer))
302 (if gdb-use-separate-io-buffer
303 (gdb-restore-windows)
304 (kill-buffer (gdb-inferior-io-name)))
307 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
309 (defun gdb-create-define-alist ()
310 "Create an alist of #define directives for GUD tooltips."
311 (let* ((file (buffer-file-name))
313 (with-output-to-string
314 (with-current-buffer standard-output
315 (call-process shell-file-name
316 (if (file-exists-p file) file nil)
317 (list t nil) nil "-c"
318 (concat gdb-cpp-define-alist-program " "
319 gdb-cpp-define-alist-flags)))))
320 (define-list (split-string output "\n" t))
322 (setq gdb-define-alist nil)
323 (dolist (define define-list)
324 (setq name (nth 1 (split-string define "[( ]")))
325 (push (cons name define) gdb-define-alist))))
327 (defun gdb-tooltip-print (expr)
329 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
330 (goto-char (point-min))
332 (if (search-forward "=" nil t)
333 (concat expr (buffer-substring (- (point) 2) (point-max)))
335 ;; remove newline for gud-tooltip-echo-area
336 (substring string 0 (- (length string) 1))))
337 (or gud-tooltip-echo-area tooltip-use-echo-area)))
339 ;; If expr is a macro for a function don't print because of possible dangerous
340 ;; side-effects. Also printing a function within a tooltip generates an
341 ;; unexpected starting annotation (phase error).
342 (defun gdb-tooltip-print-1 (expr)
343 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
344 (goto-char (point-min))
345 (if (search-forward "expands to: " nil t)
346 (unless (looking-at "\\S-+.*(.*).*")
348 (list (concat gdb-server-prefix "print " expr "\n")
349 `(lambda () (gdb-tooltip-print ,expr))))))))
351 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
353 (defun gdb-set-gud-minor-mode-existing-buffers ()
354 "Create list of source files for current GDB session."
355 (goto-char (point-min))
356 (when (search-forward "read in on demand:" nil t)
357 (while (re-search-forward gdb-source-file-regexp nil t)
358 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
359 (dolist (buffer (buffer-list))
360 (with-current-buffer buffer
361 (when (and buffer-file-name
362 (member (file-name-nondirectory buffer-file-name)
363 gdb-source-file-list))
364 (set (make-local-variable 'gud-minor-mode) 'gdba)
365 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
366 (when gud-tooltip-mode
367 (make-local-variable 'gdb-define-alist)
368 (gdb-create-define-alist)
369 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
371 (defun gdb-find-watch-expression ()
372 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
373 (varno (nth 1 var)) (expr))
374 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varno)
375 (dolist (var1 gdb-var-list)
376 (if (string-equal (nth 1 var1) (match-string 1 varno))
377 (setq expr (concat (car var1) "." (match-string 2 varno)))))
381 (setq gdb-debug-log nil)
382 (set (make-local-variable 'gud-minor-mode) 'gdba)
383 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
385 (gud-def gud-break (if (not (string-match "Machine" mode-name))
386 (gud-call "break %f:%l" arg)
390 (gud-call "break *%a" arg)))
391 "\C-b" "Set breakpoint at current line or address.")
393 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
394 (gud-call "clear %f:%l" arg)
398 (gud-call "clear *%a" arg)))
399 "\C-d" "Remove breakpoint at current line or address.")
401 (gud-def gud-until (if (not (string-match "Machine" mode-name))
402 (gud-call "until %f:%l" arg)
406 (gud-call "until *%a" arg)))
407 "\C-u" "Continue to current line or address.")
409 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
410 nil "Start or continue execution.")
412 ;; For debugging Emacs only.
416 "pp1 " (if (eq (buffer-local-value
417 'major-mode (window-buffer)) 'speedbar-mode)
418 (gdb-find-watch-expression) "%e")) arg)
419 nil "Print the emacs s-expression.")
421 (define-key gud-minor-mode-map [left-margin mouse-1]
422 'gdb-mouse-set-clear-breakpoint)
423 (define-key gud-minor-mode-map [left-fringe mouse-1]
424 'gdb-mouse-set-clear-breakpoint)
425 (define-key gud-minor-mode-map [left-fringe mouse-2]
427 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
429 (define-key gud-minor-mode-map [left-margin mouse-2]
431 (define-key gud-minor-mode-map [left-margin mouse-3]
432 'gdb-mouse-toggle-breakpoint-margin)
433 (define-key gud-minor-mode-map [left-fringe mouse-3]
434 'gdb-mouse-toggle-breakpoint-fringe)
436 (setq comint-input-sender 'gdb-send)
439 (setq gdb-frame-address (if gdb-show-main "main" nil))
440 (setq gdb-previous-frame-address nil
441 gdb-memory-address "main"
442 gdb-previous-frame nil
443 gdb-selected-frame nil
444 gdb-current-language nil
448 gdb-first-post-prompt t
452 gdb-pending-triggers nil
453 gdb-output-sink 'user
454 gdb-server-prefix "server "
455 gdb-flush-pending-output nil
456 gdb-location-alist nil
457 gdb-source-file-list nil
460 gdb-buffer-fringe-width (car (window-fringes)))
462 (setq gdb-buffer-type 'gdba)
464 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
466 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
467 (setq gdb-version nil)
468 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
472 (if (eq window-system 'w32)
473 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
474 (gdb-enqueue-input (list "set height 0\n" 'ignore))
475 (gdb-enqueue-input (list "set width 0\n" 'ignore))
477 (if (string-equal gdb-version "pre-6.4")
479 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
480 'gdb-set-gud-minor-mode-existing-buffers))
481 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
483 (list "server interpreter mi -data-list-register-names\n"
484 'gdb-get-register-names))
485 ; Needs GDB 6.2 onwards.
487 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
488 'gdb-set-gud-minor-mode-existing-buffers-1))
489 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
491 ;; find source file and compilation directory here
492 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
493 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
494 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
496 (run-hooks 'gdba-mode-hook))
498 (defun gdb-get-version ()
499 (goto-char (point-min))
500 (if (and (re-search-forward gdb-error-regexp nil t)
501 (string-match ".*(missing implementation)" (match-string 1)))
502 (setq gdb-version "pre-6.4")
503 (setq gdb-version "6.4+"))
506 (defun gdb-mouse-until (event)
507 "Execute source lines by dragging the overlay arrow (fringe) with the mouse."
509 (if gud-overlay-arrow-position
510 (let ((start (event-start event))
511 (end (event-end event))
512 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
513 (if (not (string-match "Machine" mode-name))
514 (if (equal buffer (window-buffer (posn-window end)))
515 (with-current-buffer buffer
516 (when (or (equal start end)
517 (equal (posn-point start)
519 gud-overlay-arrow-position)))
520 (setq line (line-number-at-pos (posn-point end)))
521 (gud-call (concat "until " (number-to-string line))))))
522 (if (equal (marker-buffer gdb-overlay-arrow-position)
523 (window-buffer (posn-window end)))
524 (when (or (equal start end)
525 (equal (posn-point start)
527 gdb-overlay-arrow-position)))
529 (goto-line (line-number-at-pos (posn-point end)))
531 (gud-call (concat "until *%a")))))))))
533 (defcustom gdb-speedbar-auto-raise t
534 "If non-nil raise speedbar every time display of watch expressions is\
540 (defun gdb-speedbar-auto-raise (arg)
541 "Toggle automatic raising of the speedbar for watch expressions.
542 With arg, automatically raise speedbar iff arg is positive."
544 (setq gdb-speedbar-auto-raise
546 (not gdb-speedbar-auto-raise)
547 (> (prefix-numeric-value arg) 0)))
548 (message (format "Auto raising %sabled"
549 (if gdb-speedbar-auto-raise "en" "dis"))))
551 (defcustom gdb-use-colon-colon-notation nil
552 "If non-nil use FUN::VAR format to display variables in the speedbar."
557 (defun gud-watch (&optional event)
558 "Watch expression at point."
559 (interactive (list last-input-event))
560 (if event (posn-set-point (event-end event)))
562 (save-selected-window
563 (let ((expr (tooltip-identifier-from-point (point))))
564 (if (and (string-equal gdb-current-language "c")
565 gdb-use-colon-colon-notation gdb-selected-frame)
566 (setq expr (concat gdb-selected-frame "::" expr)))
567 (catch 'already-watched
568 (dolist (var gdb-var-list)
569 (if (string-equal expr (car var)) (throw 'already-watched nil)))
570 (set-text-properties 0 (length expr) nil expr)
573 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
574 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
575 (concat"-var-create - * " expr "\n"))
576 `(lambda () (gdb-var-create-handler ,expr))))))))
578 (defconst gdb-var-create-regexp
579 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
581 (defun gdb-var-create-handler (expr)
582 (goto-char (point-min))
583 (if (re-search-forward gdb-var-create-regexp nil t)
584 (let ((var (list expr
589 (push var gdb-var-list)
591 (unless (string-equal
592 speedbar-initial-expansion-list-name "GUD")
593 (speedbar-change-initial-expansion-list "GUD"))
596 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
597 (concat "server interpreter mi \"-var-evaluate-expression "
599 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
600 `(lambda () (gdb-var-evaluate-expression-handler
602 (setq gdb-var-changed t))
603 (if (search-forward "Undefined command" nil t)
604 (message-box "Watching expressions requires gdb 6.0 onwards")
605 (message "No symbol \"%s\" in current context." expr))))
607 (defun gdb-var-evaluate-expression-handler (varnum changed)
608 (goto-char (point-min))
609 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
612 (dolist (var gdb-var-list)
613 (if (string-equal varnum (cadr var))
615 (if changed (setcar (nthcdr 5 var) t))
616 (setcar (nthcdr 4 var) (read (match-string 1)))
617 (setcar (nthcdr num gdb-var-list) var)
618 (throw 'var-found nil)))
619 (setq num (+ num 1)))))
620 (setq gdb-var-changed t))
622 (defun gdb-var-list-children (varnum)
624 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
625 `(lambda () (gdb-var-list-children-handler ,varnum)))))
627 (defconst gdb-var-list-children-regexp
628 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
631 (defun gdb-var-list-children-handler (varnum)
632 (goto-char (point-min))
633 (let ((var-list nil))
634 (catch 'child-already-watched
635 (dolist (var gdb-var-list)
636 (if (string-equal varnum (cadr var))
639 (while (re-search-forward gdb-var-list-children-regexp nil t)
640 (let ((varchild (list (match-string 2)
645 (dolist (var1 gdb-var-list)
646 (if (string-equal (cadr var1) (cadr varchild))
647 (throw 'child-already-watched nil)))
648 (push varchild var-list)
652 "server interpreter mi \"-var-evaluate-expression "
653 (nth 1 varchild) "\"\n")
654 `(lambda () (gdb-var-evaluate-expression-handler
655 ,(nth 1 varchild) nil)))))))
656 (push var var-list)))
657 (setq gdb-var-list (nreverse var-list)))))
659 (defun gdb-var-update ()
660 (when (not (member 'gdb-var-update gdb-pending-triggers))
662 (list "server interpreter mi \"-var-update *\"\n"
663 'gdb-var-update-handler))
664 (push 'gdb-var-update gdb-pending-triggers)))
666 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
668 (defun gdb-var-update-handler ()
669 (goto-char (point-min))
670 (while (re-search-forward gdb-var-update-regexp nil t)
672 (let ((varnum (match-string 1)))
673 (dolist (var gdb-var-list)
676 (concat "server interpreter mi \"-var-evaluate-expression "
678 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))
679 (throw 'var-found-1 nil)))))
680 (setq gdb-pending-triggers
681 (delq 'gdb-var-update gdb-pending-triggers))
682 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
683 ;; Dummy command to update speedbar at right time.
684 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
685 ;; Keep gdb-pending-triggers non-nil till end.
686 (push 'gdb-speedbar-timer gdb-pending-triggers)))
688 (defun gdb-speedbar-timer-fn ()
689 (setq gdb-pending-triggers
690 (delq 'gdb-speedbar-timer gdb-pending-triggers))
691 (with-current-buffer gud-comint-buffer
692 (speedbar-timer-fn)))
694 (defun gdb-var-delete ()
695 "Delete watch expression at point from the speedbar."
697 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
699 (let ((text (speedbar-line-text)))
700 (string-match "\\(\\S-+\\)" text)
701 (let* ((expr (match-string 1 text))
702 (var (assoc expr gdb-var-list))
704 (unless (string-match "\\." varnum)
707 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
709 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
710 (concat "-var-delete " varnum "\n"))
712 (setq gdb-var-list (delq var gdb-var-list))
713 (dolist (varchild gdb-var-list)
714 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
715 (setq gdb-var-list (delq varchild gdb-var-list))))
716 (setq gdb-var-changed t))))))
718 (defun gdb-edit-value (text token indent)
719 "Assign a value to a variable displayed in the speedbar."
720 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
721 (varnum (cadr var)) (value))
722 (setq value (read-string "New value: "))
725 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
726 (concat "server interpreter mi \"-var-assign "
727 varnum " " value "\"\n")
728 (concat "-var-assign " varnum " " value "\n"))
731 (defcustom gdb-show-changed-values t
732 "If non-nil highlight values that have recently changed in the speedbar.
733 The highlighting is done with `font-lock-warning-face'."
738 (defun gdb-speedbar-expand-node (text token indent)
739 "Expand the node the user clicked on.
740 TEXT is the text of the button we clicked on, a + or - item.
741 TOKEN is data related to this node.
742 INDENT is the current indentation depth."
743 (cond ((string-match "+" text) ;expand this node
745 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
746 (string-equal gdb-version "pre-6.4"))
747 (gdb-var-list-children token)
748 (gdb-var-list-children-1 token)))
749 ((string-match "-" text) ;contract this node
750 (dolist (var gdb-var-list)
751 (if (string-match (concat token "\\.") (nth 1 var))
752 (setq gdb-var-list (delq var gdb-var-list))))
753 (setq gdb-var-changed t)
754 (with-current-buffer gud-comint-buffer
755 (speedbar-timer-fn)))))
757 (defun gdb-get-target-string ()
758 (with-current-buffer gud-comint-buffer
765 ;; Each buffer has a TYPE -- a symbol that identifies the function
766 ;; of that particular buffer.
768 ;; The usual gdb interaction buffer is given the type `gdba' and
769 ;; is constructed specially.
771 ;; Others are constructed by gdb-get-create-buffer and
772 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
774 (defvar gdb-buffer-rules-assoc '())
776 (defun gdb-get-buffer (key)
777 "Return the gdb buffer tagged with type KEY.
778 The key should be one of the cars in `gdb-buffer-rules-assoc'."
780 (gdb-look-for-tagged-buffer key (buffer-list))))
782 (defun gdb-get-create-buffer (key)
783 "Create a new gdb buffer of the type specified by KEY.
784 The key should be one of the cars in `gdb-buffer-rules-assoc'."
785 (or (gdb-get-buffer key)
786 (let* ((rules (assoc key gdb-buffer-rules-assoc))
787 (name (funcall (gdb-rules-name-maker rules)))
788 (new (get-buffer-create name)))
789 (with-current-buffer new
791 (if (cdr (cdr rules))
792 (setq trigger (funcall (car (cdr (cdr rules))))))
793 (setq gdb-buffer-type key)
794 (set (make-local-variable 'gud-minor-mode)
795 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
796 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
797 (if trigger (funcall trigger)))
800 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
802 (defun gdb-look-for-tagged-buffer (key bufs)
804 (while (and (not retval) bufs)
805 (set-buffer (car bufs))
806 (if (eq gdb-buffer-type key)
807 (setq retval (car bufs)))
808 (setq bufs (cdr bufs)))
812 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
813 ;; at least one and possible more functions. The functions have these
814 ;; roles in defining a buffer type:
816 ;; NAME - Return a name for this buffer type.
818 ;; The remaining function(s) are optional:
820 ;; MODE - called in a new buffer with no arguments, should establish
821 ;; the proper mode for the buffer.
824 (defun gdb-set-buffer-rules (buffer-type &rest rules)
825 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
827 (setcdr binding rules)
828 (push (cons buffer-type rules)
829 gdb-buffer-rules-assoc))))
831 ;; GUD buffers are an exception to the rules
832 (gdb-set-buffer-rules 'gdba 'error)
834 ;; Partial-output buffer : This accumulates output from a command executed on
835 ;; behalf of emacs (rather than the user).
837 (gdb-set-buffer-rules 'gdb-partial-output-buffer
838 'gdb-partial-output-name)
840 (defun gdb-partial-output-name ()
841 (concat "*partial-output-"
842 (gdb-get-target-string)
846 (gdb-set-buffer-rules 'gdb-inferior-io
847 'gdb-inferior-io-name
848 'gdb-inferior-io-mode)
850 (defun gdb-inferior-io-name ()
851 (concat "*input/output of "
852 (gdb-get-target-string)
855 (defun gdb-display-separate-io-buffer ()
856 "Display IO of inferior in a separate window."
858 (if gdb-use-separate-io-buffer
860 (gdb-get-create-buffer 'gdb-inferior-io))))
862 (defconst gdb-frame-parameters
863 '((height . 14) (width . 80)
865 (tool-bar-lines . nil)
866 (menu-bar-lines . nil)
869 (defun gdb-frame-separate-io-buffer ()
870 "Display IO of inferior in a new frame."
872 (if gdb-use-separate-io-buffer
873 (let ((special-display-regexps (append special-display-regexps '(".*")))
874 (special-display-frame-alist gdb-frame-parameters))
875 (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
877 (defvar gdb-inferior-io-mode-map
878 (let ((map (make-sparse-keymap)))
879 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
880 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
881 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
882 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
883 (define-key map "\C-d" 'gdb-separate-io-eof)
886 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
887 "Major mode for gdb inferior-io."
888 :syntax-table nil :abbrev-table nil
889 ;; We want to use comint because it has various nifty and familiar
890 ;; features. We don't need a process, but comint wants one, so create
892 (make-comint-in-buffer
893 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
894 (current-buffer) "hexl")
895 (setq comint-input-sender 'gdb-inferior-io-sender))
897 (defun gdb-inferior-io-sender (proc string)
898 ;; PROC is the pseudo-process created to satisfy comint.
899 (with-current-buffer (process-buffer proc)
900 (setq proc (get-buffer-process gud-comint-buffer))
901 (process-send-string proc string)
902 (process-send-string proc "\n")))
904 (defun gdb-separate-io-interrupt ()
905 "Interrupt the program being debugged."
908 (get-buffer-process gud-comint-buffer) comint-ptyp))
910 (defun gdb-separate-io-quit ()
911 "Send quit signal to the program being debugged."
914 (get-buffer-process gud-comint-buffer) comint-ptyp))
916 (defun gdb-separate-io-stop ()
917 "Stop the program being debugged."
920 (get-buffer-process gud-comint-buffer) comint-ptyp))
922 (defun gdb-separate-io-eof ()
923 "Send end-of-file to the program being debugged."
926 (get-buffer-process gud-comint-buffer)))
929 ;; gdb communications
932 ;; INPUT: things sent to gdb
934 ;; The queues are lists. Each element is either a string (indicating user or
935 ;; user-like input) or a list of the form:
937 ;; (INPUT-STRING HANDLER-FN)
939 ;; The handler function will be called from the partial-output buffer when the
940 ;; command completes. This is the way to write commands which invoke gdb
941 ;; commands autonomously.
943 ;; These lists are consumed tail first.
946 (defun gdb-send (proc string)
947 "A comint send filter for gdb.
948 This filter may simply queue input for a later time."
949 (with-current-buffer gud-comint-buffer
950 (let ((inhibit-read-only t))
951 (remove-text-properties (point-min) (point-max) '(face))))
952 (let ((item (concat string "\n")))
955 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
956 (process-send-string proc item))
957 (gdb-enqueue-input item))))
959 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
960 ;; is a query, or other non-top-level prompt.
962 (defun gdb-enqueue-input (item)
966 (setq gdb-prompting nil))
967 (push item gdb-input-queue)))
969 (defun gdb-dequeue-input ()
970 (let ((queue gdb-input-queue))
972 (let ((last (car (last queue))))
973 (unless (nbutlast queue) (setq gdb-input-queue '()))
976 (defun gdb-send-item (item)
977 (setq gdb-flush-pending-output nil)
978 (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
979 (setq gdb-current-item item)
980 (let ((process (get-buffer-process gud-comint-buffer)))
981 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
984 (setq gdb-output-sink 'user)
985 (process-send-string process item))
987 (gdb-clear-partial-output)
988 (setq gdb-output-sink 'pre-emacs)
989 (process-send-string process
991 ;; case: eq gud-minor-mode 'gdbmi
992 (gdb-clear-partial-output)
993 (setq gdb-output-sink 'emacs)
994 (process-send-string process (car item)))))
997 ;; output -- things gdb prints to emacs
999 ;; GDB output is a stream interrupted by annotations.
1000 ;; Annotations can be recognized by their beginning
1001 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1003 ;; The tag is a string obeying symbol syntax.
1005 ;; The optional part `<opt>' can be either the empty string
1006 ;; or a space followed by more data relating to the annotation.
1007 ;; For example, the SOURCE annotation is followed by a filename,
1008 ;; line number and various useless goo. This data must not include
1012 (defcustom gud-gdba-command-name "gdb -annotate=3"
1013 "Default command to execute an executable under the GDB-UI debugger."
1018 (defvar gdb-annotation-rules
1019 '(("pre-prompt" gdb-pre-prompt)
1020 ("prompt" gdb-prompt)
1021 ("commands" gdb-subprompt)
1022 ("overload-choice" gdb-subprompt)
1023 ("query" gdb-subprompt)
1024 ;; Need this prompt for GDB 6.1
1025 ("nquery" gdb-subprompt)
1026 ("prompt-for-continue" gdb-subprompt)
1027 ("post-prompt" gdb-post-prompt)
1028 ("source" gdb-source)
1029 ("starting" gdb-starting)
1030 ("exited" gdb-exited)
1031 ("signalled" gdb-exited)
1032 ("signal" gdb-stopping)
1033 ("breakpoint" gdb-stopping)
1034 ("watchpoint" gdb-stopping)
1035 ("frame-begin" gdb-frame-begin)
1036 ("stopped" gdb-stopped)
1037 ("error-begin" gdb-error)
1039 ) "An assoc mapping annotation tags to functions which process them.")
1042 (setq gdb-flush-pending-output t)
1043 (setq gud-running nil)
1044 (setq gdb-output-sink 'user)
1045 (setq gdb-input-queue nil)
1046 (setq gdb-pending-triggers nil)
1047 (setq gdb-prompting t))
1049 (defconst gdb-source-spec-regexp
1050 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1052 ;; Do not use this except as an annotation handler.
1053 (defun gdb-source (args)
1054 (string-match gdb-source-spec-regexp args)
1055 ;; Extract the frame position from the marker.
1056 (setq gud-last-frame
1058 (match-string 1 args)
1059 (string-to-number (match-string 2 args))))
1060 (setq gdb-frame-address (match-string 3 args))
1061 ;; cover for auto-display output which comes *before*
1062 ;; stopped annotation
1063 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1065 (defun gdb-pre-prompt (ignored)
1066 "An annotation handler for `pre-prompt'.
1067 This terminates the collection of output from a previous command if that
1068 happens to be in effect."
1069 (setq gdb-error nil)
1070 (let ((sink gdb-output-sink))
1074 (setq gdb-output-sink 'post-emacs))
1077 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1079 (defun gdb-prompt (ignored)
1080 "An annotation handler for `prompt'.
1081 This sends the next command (if any) to gdb."
1082 (when gdb-first-prompt
1084 (setq gdb-first-prompt nil))
1085 (let ((sink gdb-output-sink))
1088 ((eq sink 'post-emacs)
1089 (setq gdb-output-sink 'user)
1091 (car (cdr gdb-current-item))))
1092 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1093 (funcall handler))))
1096 (error "Phase error in gdb-prompt (got %s)" sink))))
1097 (let ((input (gdb-dequeue-input)))
1099 (gdb-send-item input)
1101 (setq gdb-prompting t)
1102 (gud-display-frame)))))
1104 (defun gdb-subprompt (ignored)
1105 "An annotation handler for non-top-level prompts."
1106 (setq gdb-prompting t))
1108 (defun gdb-starting (ignored)
1109 "An annotation handler for `starting'.
1110 This says that I/O for the subprocess is now the program being debugged,
1112 (setq gdb-active-process t)
1113 (let ((sink gdb-output-sink))
1117 (setq gud-running t)
1118 (if gdb-use-separate-io-buffer
1119 (setq gdb-output-sink 'inferior))))
1122 (error "Unexpected `starting' annotation")))))
1124 (defun gdb-stopping (ignored)
1125 "An annotation handler for `breakpoint' and other annotations.
1126 They say that I/O for the subprocess is now GDB, not the program
1128 (if gdb-use-separate-io-buffer
1129 (let ((sink gdb-output-sink))
1131 ((eq sink 'inferior)
1132 (setq gdb-output-sink 'user))
1135 (error "Unexpected stopping annotation"))))))
1137 (defun gdb-exited (ignored)
1138 "An annotation handler for `exited' and `signalled'.
1139 They say that I/O for the subprocess is now GDB, not the program
1140 being debugged and that the program is no longer running. This
1141 function is used to change the focus of GUD tooltips to #define
1143 (setq gdb-active-process nil)
1144 (setq gud-overlay-arrow-position nil)
1145 (setq gdb-overlay-arrow-position nil)
1146 (gdb-stopping ignored))
1148 (defun gdb-frame-begin (ignored)
1149 (let ((sink gdb-output-sink))
1151 ((eq sink 'inferior)
1152 (setq gdb-output-sink 'user))
1154 ((eq sink 'emacs) t)
1157 (error "Unexpected frame-begin annotation (%S)" sink)))))
1159 (defun gdb-stopped (ignored)
1160 "An annotation handler for `stopped'.
1161 It is just like `gdb-stopping', except that if we already set the output
1162 sink to `user' in `gdb-stopping', that is fine."
1163 (setq gud-running nil)
1164 (setq gdb-active-process t)
1165 (let ((sink gdb-output-sink))
1167 ((eq sink 'inferior)
1168 (setq gdb-output-sink 'user))
1172 (error "Unexpected stopped annotation")))))
1174 (defun gdb-error (ignored)
1175 (setq gdb-error (not gdb-error)))
1177 (defun gdb-post-prompt (ignored)
1178 "An annotation handler for `post-prompt'.
1179 This begins the collection of output from the current command if that
1180 happens to be appropriate."
1181 ;; Don't add to queue if there outstanding items or GDB is not known yet.
1182 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1183 (gdb-get-selected-frame)
1184 (gdb-invalidate-frames)
1185 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1186 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
1187 (gdb-invalidate-breakpoints)
1188 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1189 ;; so gdb-frame-address is updated.
1190 ;; (gdb-invalidate-assembler)
1192 (if (string-equal gdb-version "pre-6.4")
1193 (gdb-invalidate-registers)
1194 (gdb-get-changed-registers)
1195 (gdb-invalidate-registers-1))
1197 (gdb-invalidate-memory)
1198 (if (string-equal gdb-version "pre-6.4")
1199 (gdb-invalidate-locals)
1200 (gdb-invalidate-locals-1))
1202 (gdb-invalidate-threads)
1203 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1204 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1205 ;; Only needed/used with speedbar/watch expressions.
1206 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1207 (setq gdb-var-changed t) ; force update
1208 (dolist (var gdb-var-list)
1209 (setcar (nthcdr 5 var) nil))
1210 (if (string-equal gdb-version "pre-6.4")
1212 (gdb-var-update-1)))))
1213 (setq gdb-first-post-prompt nil)
1214 (let ((sink gdb-output-sink))
1217 ((eq sink 'pre-emacs)
1218 (setq gdb-output-sink 'emacs))
1221 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1223 (defun gud-gdba-marker-filter (string)
1224 "A gud marker filter for gdb. Handle a burst of output from GDB."
1225 (if gdb-flush-pending-output
1227 (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
1228 ;; Recall the left over gud-marker-acc from last time.
1229 (setq gud-marker-acc (concat gud-marker-acc string))
1230 ;; Start accumulating output for the GUD buffer.
1233 ;; Process all the complete markers in this chunk.
1234 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1235 (let ((annotation (match-string 1 gud-marker-acc)))
1237 ;; Stuff prior to the match is just ordinary output.
1238 ;; It is either concatenated to OUTPUT or directed
1243 (substring gud-marker-acc 0 (match-beginning 0))))
1245 ;; Take that stuff off the gud-marker-acc.
1246 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1248 ;; Parse the tag from the annotation, and maybe its arguments.
1249 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1250 (let* ((annotation-type (match-string 1 annotation))
1251 (annotation-arguments (match-string 2 annotation))
1252 (annotation-rule (assoc annotation-type
1253 gdb-annotation-rules)))
1254 ;; Call the handler for this annotation.
1256 (funcall (car (cdr annotation-rule))
1257 annotation-arguments)
1258 ;; Else the annotation is not recognized. Ignore it silently,
1259 ;; so that GDB can add new annotations without causing
1263 ;; Does the remaining text end in a partial line?
1264 ;; If it does, then keep part of the gud-marker-acc until we get more.
1265 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1268 ;; Everything before the potential marker start can be output.
1270 (gdb-concat-output output
1271 (substring gud-marker-acc 0
1272 (match-beginning 0))))
1274 ;; Everything after, we save, to combine with later input.
1275 (setq gud-marker-acc (substring gud-marker-acc
1276 (match-beginning 0))))
1278 ;; In case we know the gud-marker-acc contains no partial annotations:
1280 (setq output (gdb-concat-output output gud-marker-acc))
1281 (setq gud-marker-acc "")))
1284 (defun gdb-concat-output (so-far new)
1286 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1287 (let ((sink gdb-output-sink))
1289 ((eq sink 'user) (concat so-far new))
1290 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1292 (gdb-append-to-partial-output new)
1294 ((eq sink 'inferior)
1295 (gdb-append-to-inferior-io new)
1299 (error "Bogon output sink %S" sink)))))
1301 (defun gdb-append-to-partial-output (string)
1302 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1303 (goto-char (point-max))
1306 (defun gdb-clear-partial-output ()
1307 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1310 (defun gdb-append-to-inferior-io (string)
1311 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1312 (goto-char (point-max))
1313 (insert-before-markers string))
1314 (if (not (string-equal string ""))
1315 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io))))
1317 (defun gdb-clear-inferior-io ()
1318 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1322 ;; One trick is to have a command who's output is always available in a buffer
1323 ;; of it's own, and is always up to date. We build several buffers of this
1326 ;; There are two aspects to this: gdb has to tell us when the output for that
1327 ;; command might have changed, and we have to be able to run the command
1328 ;; behind the user's back.
1330 ;; The output phasing associated with the variable gdb-output-sink
1331 ;; help us to run commands behind the user's back.
1333 ;; Below is the code for specificly managing buffers of output from one
1337 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1338 ;; It adds an input for the command we are tracking. It should be the
1339 ;; annotation rule binding of whatever gdb sends to tell us this command
1340 ;; might have changed it's output.
1342 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1343 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1344 ;; input in the input queue (see comment about ``gdb communications'' above).
1346 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1348 `(defun ,name (&optional ignored)
1349 (if (and ,demand-predicate
1351 gdb-pending-triggers)))
1354 (list ,gdb-command ',output-handler))
1355 (push ',name gdb-pending-triggers)))))
1357 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1359 (setq gdb-pending-triggers
1361 gdb-pending-triggers))
1362 (let ((buf (gdb-get-buffer ',buf-key)))
1364 (with-current-buffer buf
1365 (let* ((window (get-buffer-window buf 0))
1366 (p (window-point window))
1367 (buffer-read-only nil))
1369 (insert-buffer-substring (gdb-get-create-buffer
1370 'gdb-partial-output-buffer))
1371 (set-window-point window p)))))
1372 ;; put customisation here
1375 (defmacro def-gdb-auto-updated-buffer (buffer-key
1376 trigger-name gdb-command
1377 output-handler-name custom-defun)
1379 (def-gdb-auto-update-trigger ,trigger-name
1380 ;; The demand predicate:
1381 (gdb-get-buffer ',buffer-key)
1383 ,output-handler-name)
1384 (def-gdb-auto-update-handler ,output-handler-name
1385 ,trigger-name ,buffer-key ,custom-defun)))
1389 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1391 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1392 'gdb-breakpoints-buffer-name
1393 'gdb-breakpoints-mode)
1395 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1396 ;; This defines the auto update rule for buffers of type
1397 ;; `gdb-breakpoints-buffer'.
1399 ;; It defines a function to serve as the annotation handler that
1400 ;; handles the `foo-invalidated' message. That function is called:
1401 gdb-invalidate-breakpoints
1403 ;; To update the buffer, this command is sent to gdb.
1404 "server info breakpoints\n"
1406 ;; This also defines a function to be the handler for the output
1407 ;; from the command above. That function will copy the output into
1408 ;; the appropriately typed buffer. That function will be called:
1409 gdb-info-breakpoints-handler
1410 ;; buffer specific functions
1411 gdb-info-breakpoints-custom)
1413 (defconst breakpoint-xpm-data
1415 static char *magick[] = {
1416 /* columns rows colors chars-per-pixel */
1432 "XPM data used for breakpoint icon.")
1434 (defconst breakpoint-enabled-pbm-data
1437 0 0 0 0 1 1 1 1 0 0 0 0
1438 0 0 0 1 1 1 1 1 1 0 0 0
1439 0 0 1 1 1 1 1 1 1 1 0 0
1440 0 1 1 1 1 1 1 1 1 1 1 0
1441 0 1 1 1 1 1 1 1 1 1 1 0
1442 0 1 1 1 1 1 1 1 1 1 1 0
1443 0 1 1 1 1 1 1 1 1 1 1 0
1444 0 0 1 1 1 1 1 1 1 1 0 0
1445 0 0 0 1 1 1 1 1 1 0 0 0
1446 0 0 0 0 1 1 1 1 0 0 0 0"
1447 "PBM data used for enabled breakpoint icon.")
1449 (defconst breakpoint-disabled-pbm-data
1461 0 0 0 1 0 1 0 1 0 0"
1462 "PBM data used for disabled breakpoint icon.")
1464 (defvar breakpoint-enabled-icon nil
1465 "Icon for enabled breakpoint in display margin.")
1467 (defvar breakpoint-disabled-icon nil
1468 "Icon for disabled breakpoint in display margin.")
1470 ;; Bitmap for breakpoint in fringe
1471 (and (display-images-p)
1472 (define-fringe-bitmap 'breakpoint
1473 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1475 (defface breakpoint-enabled
1479 "Face for enabled breakpoint icon in fringe."
1482 (defface breakpoint-disabled
1483 ;; We use different values of grey for different background types,
1484 ;; so that on low-color displays it will end up as something visible
1485 ;; if it has to be approximated.
1486 '((((background dark)) :foreground "grey60")
1487 (((background light)) :foreground "grey40"))
1488 "Face for disabled breakpoint icon in fringe."
1491 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1492 (defun gdb-info-breakpoints-custom ()
1493 (let ((flag) (bptno))
1494 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1495 (dolist (buffer (buffer-list))
1496 (with-current-buffer buffer
1497 (if (and (memq gud-minor-mode '(gdba gdbmi))
1498 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1499 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1500 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1502 (goto-char (point-min))
1503 (while (< (point) (- (point-max) 1))
1505 (if (looking-at "[^\t].*?breakpoint")
1507 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1508 (setq bptno (match-string 1))
1509 (setq flag (char-after (match-beginning 2)))
1511 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1513 (let ((buffer-read-only nil))
1514 (add-text-properties (match-beginning 1) (match-end 1)
1515 '(face font-lock-function-name-face)))
1516 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1517 (let ((line (match-string 2)) (buffer-read-only nil)
1518 (file (match-string 1)))
1519 (add-text-properties (line-beginning-position)
1521 '(mouse-face highlight
1522 help-echo "mouse-2, RET: visit breakpoint"))
1523 (unless (file-exists-p file)
1524 (setq file (cdr (assoc bptno gdb-location-alist))))
1526 (not (string-equal file "File not found")))
1527 (with-current-buffer
1528 (find-file-noselect file 'nowarn)
1529 (set (make-local-variable 'gud-minor-mode)
1531 (set (make-local-variable 'tool-bar-map)
1533 ;; Only want one breakpoint icon at each
1536 (goto-line (string-to-number line))
1537 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1540 (concat gdb-server-prefix "list "
1541 (match-string-no-properties 1) ":1\n")
1544 (list (concat gdb-server-prefix "info source\n")
1545 `(lambda () (gdb-get-location
1546 ,bptno ,line ,flag))))))))))
1548 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1550 (defun gdb-mouse-set-clear-breakpoint (event)
1551 "Set/clear breakpoint in left fringe/margin with mouse click."
1553 (mouse-minibuffer-check event)
1554 (let ((posn (event-end event)))
1555 (if (numberp (posn-point posn))
1556 (with-selected-window (posn-window posn)
1558 (goto-char (posn-point posn))
1559 (if (or (posn-object posn)
1560 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1563 (gud-break nil)))))))
1565 (defun gdb-mouse-toggle-breakpoint-margin (event)
1566 "Enable/disable breakpoint in left margin with mouse click."
1568 (mouse-minibuffer-check event)
1569 (let ((posn (event-end event)))
1570 (if (numberp (posn-point posn))
1571 (with-selected-window (posn-window posn)
1573 (goto-char (posn-point posn))
1574 (if (posn-object posn)
1577 (let ((bptno (get-text-property
1578 0 'gdb-bptno (car (posn-string posn)))))
1579 (concat gdb-server-prefix
1580 (if (get-text-property
1581 0 'gdb-enabled (car (posn-string posn)))
1587 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1588 "Enable/disable breakpoint in left fringe with mouse click."
1590 (mouse-minibuffer-check event)
1591 (let* ((posn (event-end event))
1592 (pos (posn-point posn))
1595 (with-selected-window (posn-window posn)
1597 (set-buffer (window-buffer (selected-window)))
1599 (dolist (overlay (overlays-in pos pos))
1600 (when (overlay-get overlay 'put-break)
1601 (setq obj (overlay-get overlay 'before-string))))
1605 (concat gdb-server-prefix
1606 (if (get-text-property 0 'gdb-enabled obj)
1609 (get-text-property 0 'gdb-bptno obj) "\n")
1612 (defun gdb-breakpoints-buffer-name ()
1613 (with-current-buffer gud-comint-buffer
1614 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1616 (defun gdb-display-breakpoints-buffer ()
1617 "Display status of user-settable breakpoints."
1620 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1622 (defun gdb-frame-breakpoints-buffer ()
1623 "Display status of user-settable breakpoints in a new frame."
1625 (let ((special-display-regexps (append special-display-regexps '(".*")))
1626 (special-display-frame-alist gdb-frame-parameters))
1627 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
1629 (defvar gdb-breakpoints-mode-map
1630 (let ((map (make-sparse-keymap))
1631 (menu (make-sparse-keymap "Breakpoints")))
1632 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1633 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1634 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1635 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1636 (suppress-keymap map)
1637 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1638 (define-key map " " 'gdb-toggle-breakpoint)
1639 (define-key map "D" 'gdb-delete-breakpoint)
1640 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1641 (define-key map "q" 'gdb-delete-frame-or-window)
1642 (define-key map "\r" 'gdb-goto-breakpoint)
1643 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1644 (define-key map [follow-link] 'mouse-face)
1647 (defun gdb-delete-frame-or-window ()
1648 "Delete frame if there is only one window. Otherwise delete the window."
1650 (if (one-window-p) (delete-frame)
1653 (defun gdb-breakpoints-mode ()
1654 "Major mode for gdb breakpoints.
1656 \\{gdb-breakpoints-mode-map}"
1657 (kill-all-local-variables)
1658 (setq major-mode 'gdb-breakpoints-mode)
1659 (setq mode-name "Breakpoints")
1660 (use-local-map gdb-breakpoints-mode-map)
1661 (setq buffer-read-only t)
1662 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1663 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1664 'gdb-invalidate-breakpoints
1665 'gdbmi-invalidate-breakpoints))
1667 (defconst gdb-breakpoint-regexp
1668 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1670 (defun gdb-toggle-breakpoint ()
1671 "Enable/disable breakpoint at current line."
1674 (beginning-of-line 1)
1675 (if (looking-at gdb-breakpoint-regexp)
1678 (concat gdb-server-prefix
1679 (if (eq ?y (char-after (match-beginning 2)))
1682 (match-string 1) "\n") 'ignore))
1683 (error "Not recognized as break/watchpoint line"))))
1685 (defun gdb-delete-breakpoint ()
1686 "Delete the breakpoint at current line."
1688 (beginning-of-line 1)
1689 (if (looking-at gdb-breakpoint-regexp)
1692 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1693 (error "Not recognized as break/watchpoint line")))
1695 (defun gdb-goto-breakpoint (&optional event)
1696 "Display the breakpoint location specified at current line."
1697 (interactive (list last-input-event))
1698 (if event (posn-set-point (event-end event)))
1699 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
1700 (let ((window (get-buffer-window gud-comint-buffer)))
1701 (if window (save-selected-window (select-window window))))
1703 (beginning-of-line 1)
1704 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1705 (let ((bptno (match-string 1))
1706 (file (match-string 2))
1707 (line (match-string 3)))
1708 (save-selected-window
1709 (let* ((buf (find-file-noselect
1710 (if (file-exists-p file) file
1711 (cdr (assoc bptno gdb-location-alist)))))
1712 (window (display-buffer buf)))
1713 (with-current-buffer buf
1714 (goto-line (string-to-number line))
1715 (set-window-point window (point))))))
1716 (error "No location specified."))))
1719 ;; Frames buffer. This displays a perpetually correct bactracktrace
1720 ;; (from the command `where').
1722 ;; Alas, if your stack is deep, it is costly.
1724 (gdb-set-buffer-rules 'gdb-stack-buffer
1725 'gdb-stack-buffer-name
1728 (def-gdb-auto-updated-buffer gdb-stack-buffer
1729 gdb-invalidate-frames
1731 gdb-info-frames-handler
1732 gdb-info-frames-custom)
1734 (defun gdb-info-frames-custom ()
1735 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1737 (let ((buffer-read-only nil)
1739 (goto-char (point-min))
1740 (while (< (point) (point-max))
1741 (setq bl (line-beginning-position)
1742 el (line-end-position))
1743 (when (looking-at "#")
1744 (add-text-properties bl el
1745 '(mouse-face highlight
1746 help-echo "mouse-2, RET: Select frame")))
1748 (when (looking-at "^#\\([0-9]+\\)")
1749 (when (string-equal (match-string 1) gdb-frame-number)
1750 (put-text-property bl (+ bl 4)
1751 'face '(:inverse-video t)))
1752 (when (re-search-forward
1754 (if (string-equal (match-string 1) "0") "" " in ")
1755 "\\([^ ]+\\) (") el t)
1756 (put-text-property (match-beginning 1) (match-end 1)
1757 'face font-lock-function-name-face)
1758 (setq bl (match-end 0))
1759 (while (re-search-forward "<\\([^>]+\\)>" el t)
1760 (put-text-property (match-beginning 1) (match-end 1)
1761 'face font-lock-function-name-face))
1763 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1764 (put-text-property (match-beginning 1) (match-end 1)
1765 'face font-lock-variable-name-face))))
1766 (forward-line 1))))))
1768 (defun gdb-stack-buffer-name ()
1769 (with-current-buffer gud-comint-buffer
1770 (concat "*stack frames of " (gdb-get-target-string) "*")))
1772 (defun gdb-display-stack-buffer ()
1773 "Display backtrace of current stack."
1776 (gdb-get-create-buffer 'gdb-stack-buffer)))
1778 (defun gdb-frame-stack-buffer ()
1779 "Display backtrace of current stack in a new frame."
1781 (let ((special-display-regexps (append special-display-regexps '(".*")))
1782 (special-display-frame-alist gdb-frame-parameters))
1783 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1785 (defvar gdb-frames-mode-map
1786 (let ((map (make-sparse-keymap)))
1787 (suppress-keymap map)
1788 (define-key map "q" 'kill-this-buffer)
1789 (define-key map "\r" 'gdb-frames-select)
1790 (define-key map [mouse-2] 'gdb-frames-select)
1791 (define-key map [follow-link] 'mouse-face)
1794 (defun gdb-frames-mode ()
1795 "Major mode for gdb frames.
1797 \\{gdb-frames-mode-map}"
1798 (kill-all-local-variables)
1799 (setq major-mode 'gdb-frames-mode)
1800 (setq mode-name "Frames")
1801 (setq buffer-read-only t)
1802 (use-local-map gdb-frames-mode-map)
1803 (run-mode-hooks 'gdb-frames-mode-hook)
1804 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1805 'gdb-invalidate-frames
1806 'gdbmi-invalidate-frames))
1808 (defun gdb-get-frame-number ()
1811 (let* ((start (line-beginning-position))
1812 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
1813 (n (or (and pos (match-string-no-properties 1)) "0")))
1816 (defun gdb-frames-select (&optional event)
1817 "Select the frame and display the relevant source."
1818 (interactive (list last-input-event))
1819 (if event (posn-set-point (event-end event)))
1821 (list (concat gdb-server-prefix "frame "
1822 (gdb-get-frame-number) "\n") 'ignore))
1823 (gud-display-frame))
1826 ;; Threads buffer. This displays a selectable thread list.
1828 (gdb-set-buffer-rules 'gdb-threads-buffer
1829 'gdb-threads-buffer-name
1832 (def-gdb-auto-updated-buffer gdb-threads-buffer
1833 gdb-invalidate-threads
1834 (concat gdb-server-prefix "info threads\n")
1835 gdb-info-threads-handler
1836 gdb-info-threads-custom)
1838 (defun gdb-info-threads-custom ()
1839 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1840 (let ((buffer-read-only nil))
1841 (goto-char (point-min))
1842 (while (< (point) (point-max))
1843 (unless (looking-at "No ")
1844 (add-text-properties (line-beginning-position) (line-end-position)
1845 '(mouse-face highlight
1846 help-echo "mouse-2, RET: select thread")))
1847 (forward-line 1)))))
1849 (defun gdb-threads-buffer-name ()
1850 (with-current-buffer gud-comint-buffer
1851 (concat "*threads of " (gdb-get-target-string) "*")))
1853 (defun gdb-display-threads-buffer ()
1854 "Display IDs of currently known threads."
1857 (gdb-get-create-buffer 'gdb-threads-buffer)))
1859 (defun gdb-frame-threads-buffer ()
1860 "Display IDs of currently known threads in a new frame."
1862 (let ((special-display-regexps (append special-display-regexps '(".*")))
1863 (special-display-frame-alist gdb-frame-parameters))
1864 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1866 (defvar gdb-threads-mode-map
1867 (let ((map (make-sparse-keymap)))
1868 (suppress-keymap map)
1869 (define-key map "q" 'kill-this-buffer)
1870 (define-key map "\r" 'gdb-threads-select)
1871 (define-key map [mouse-2] 'gdb-threads-select)
1872 (define-key map [follow-link] 'mouse-face)
1875 (defvar gdb-threads-font-lock-keywords
1877 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
1878 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1879 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
1881 "Font lock keywords used in `gdb-threads-mode'.")
1883 (defun gdb-threads-mode ()
1884 "Major mode for gdb frames.
1886 \\{gdb-threads-mode-map}"
1887 (kill-all-local-variables)
1888 (setq major-mode 'gdb-threads-mode)
1889 (setq mode-name "Threads")
1890 (setq buffer-read-only t)
1891 (use-local-map gdb-threads-mode-map)
1892 (set (make-local-variable 'font-lock-defaults)
1893 '(gdb-threads-font-lock-keywords))
1894 (run-mode-hooks 'gdb-threads-mode-hook)
1895 'gdb-invalidate-threads)
1897 (defun gdb-get-thread-number ()
1899 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1900 (match-string-no-properties 1)))
1902 (defun gdb-threads-select (&optional event)
1903 "Select the thread and display the relevant source."
1904 (interactive (list last-input-event))
1905 (if event (posn-set-point (event-end event)))
1907 (list (concat gdb-server-prefix "thread "
1908 (gdb-get-thread-number) "\n") 'ignore))
1909 (gud-display-frame))
1912 ;; Registers buffer.
1914 (defcustom gdb-all-registers nil
1915 "Non-nil means include floating-point registers."
1920 (gdb-set-buffer-rules 'gdb-registers-buffer
1921 'gdb-registers-buffer-name
1922 'gdb-registers-mode)
1924 (def-gdb-auto-updated-buffer gdb-registers-buffer
1925 gdb-invalidate-registers
1927 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
1928 gdb-info-registers-handler
1929 gdb-info-registers-custom)
1931 (defun gdb-info-registers-custom ()
1932 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1934 (let ((buffer-read-only nil)
1936 (goto-char (point-min))
1937 (while (< (point) (point-max))
1938 (setq start (line-beginning-position))
1939 (setq end (line-end-position))
1940 (when (looking-at "^[^ ]+")
1941 (unless (string-equal (match-string 0) "The")
1942 (put-text-property start (match-end 0)
1943 'face font-lock-variable-name-face)
1944 (add-text-properties start end
1945 '(help-echo "mouse-2: edit value"
1946 mouse-face highlight))))
1947 (forward-line 1))))))
1949 (defun gdb-edit-register-value (&optional event)
1950 (interactive (list last-input-event))
1952 (if event (posn-set-point (event-end event)))
1954 (let* ((register (current-word))
1955 (value (read-string (format "New value (%s): " register))))
1957 (list (concat gdb-server-prefix "set $" register "=" value "\n")
1960 (defvar gdb-registers-mode-map
1961 (let ((map (make-sparse-keymap)))
1962 (suppress-keymap map)
1963 (define-key map "\r" 'gdb-edit-register-value)
1964 (define-key map [mouse-2] 'gdb-edit-register-value)
1965 (define-key map " " 'gdb-all-registers)
1966 (define-key map "q" 'kill-this-buffer)
1969 (defun gdb-registers-mode ()
1970 "Major mode for gdb registers.
1972 \\{gdb-registers-mode-map}"
1973 (kill-all-local-variables)
1974 (setq major-mode 'gdb-registers-mode)
1975 (setq mode-name "Registers")
1976 (setq buffer-read-only t)
1977 (use-local-map gdb-registers-mode-map)
1978 (run-mode-hooks 'gdb-registers-mode-hook)
1979 (if (string-equal gdb-version "pre-6.4")
1981 (if gdb-all-registers (setq mode-name "Registers:All"))
1982 'gdb-invalidate-registers)
1983 'gdb-invalidate-registers-1))
1985 (defun gdb-registers-buffer-name ()
1986 (with-current-buffer gud-comint-buffer
1987 (concat "*registers of " (gdb-get-target-string) "*")))
1989 (defun gdb-display-registers-buffer ()
1990 "Display integer register contents."
1993 (gdb-get-create-buffer 'gdb-registers-buffer)))
1995 (defun gdb-frame-registers-buffer ()
1996 "Display integer register contents in a new frame."
1998 (let ((special-display-regexps (append special-display-regexps '(".*")))
1999 (special-display-frame-alist gdb-frame-parameters))
2000 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
2002 (defun gdb-all-registers ()
2003 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2005 (when (string-equal gdb-version "pre-6.4")
2006 (if gdb-all-registers
2008 (setq gdb-all-registers nil)
2009 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
2010 (setq mode-name "Registers")))
2011 (setq gdb-all-registers t)
2012 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
2013 (setq mode-name "Registers:All")))
2014 (message (format "Display of floating-point registers %sabled"
2015 (if gdb-all-registers "en" "dis")))
2016 (gdb-invalidate-registers)))
2021 (defcustom gdb-memory-repeat-count 32
2022 "Number of data items in memory window."
2027 (defcustom gdb-memory-format "x"
2028 "Display format of data items in memory window."
2029 :type '(choice (const :tag "Hexadecimal" "x")
2030 (const :tag "Signed decimal" "d")
2031 (const :tag "Unsigned decimal" "u")
2032 (const :tag "Octal" "o")
2033 (const :tag "Binary" "t"))
2037 (defcustom gdb-memory-unit "w"
2038 "Unit size of data items in memory window."
2039 :type '(choice (const :tag "Byte" "b")
2040 (const :tag "Halfword" "h")
2041 (const :tag "Word" "w")
2042 (const :tag "Giant word" "g"))
2046 (gdb-set-buffer-rules 'gdb-memory-buffer
2047 'gdb-memory-buffer-name
2050 (def-gdb-auto-updated-buffer gdb-memory-buffer
2051 gdb-invalidate-memory
2052 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2053 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2054 gdb-read-memory-handler
2055 gdb-read-memory-custom)
2057 (defun gdb-read-memory-custom ()
2059 (goto-char (point-min))
2060 (if (looking-at "0x[[:xdigit:]]+")
2061 (setq gdb-memory-address (match-string 0)))))
2063 (defvar gdb-memory-mode-map
2064 (let ((map (make-sparse-keymap)))
2065 (suppress-keymap map)
2066 (define-key map "q" 'kill-this-buffer)
2069 (defun gdb-memory-set-address (event)
2070 "Set the start memory address."
2072 (save-selected-window
2073 (select-window (posn-window (event-start event)))
2074 (let ((arg (read-from-minibuffer "Memory address: ")))
2075 (setq gdb-memory-address arg))
2076 (gdb-invalidate-memory)))
2078 (defun gdb-memory-set-repeat-count (event)
2079 "Set the number of data items in memory window."
2081 (save-selected-window
2082 (select-window (posn-window (event-start event)))
2083 (let* ((arg (read-from-minibuffer "Repeat count: "))
2084 (count (string-to-number arg)))
2086 (error "Positive numbers only")
2087 (customize-set-variable 'gdb-memory-repeat-count count)
2088 (gdb-invalidate-memory)))))
2090 (defun gdb-memory-format-binary ()
2091 "Set the display format to binary."
2093 (customize-set-variable 'gdb-memory-format "t")
2094 (gdb-invalidate-memory))
2096 (defun gdb-memory-format-octal ()
2097 "Set the display format to octal."
2099 (customize-set-variable 'gdb-memory-format "o")
2100 (gdb-invalidate-memory))
2102 (defun gdb-memory-format-unsigned ()
2103 "Set the display format to unsigned decimal."
2105 (customize-set-variable 'gdb-memory-format "u")
2106 (gdb-invalidate-memory))
2108 (defun gdb-memory-format-signed ()
2109 "Set the display format to decimal."
2111 (customize-set-variable 'gdb-memory-format "d")
2112 (gdb-invalidate-memory))
2114 (defun gdb-memory-format-hexadecimal ()
2115 "Set the display format to hexadecimal."
2117 (customize-set-variable 'gdb-memory-format "x")
2118 (gdb-invalidate-memory))
2120 (defvar gdb-memory-format-map
2121 (let ((map (make-sparse-keymap)))
2122 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2124 "Keymap to select format in the header line.")
2126 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2127 "Menu of display formats in the header line.")
2129 (define-key gdb-memory-format-menu [binary]
2130 '(menu-item "Binary" gdb-memory-format-binary
2131 :button (:radio . (equal gdb-memory-format "t"))))
2132 (define-key gdb-memory-format-menu [octal]
2133 '(menu-item "Octal" gdb-memory-format-octal
2134 :button (:radio . (equal gdb-memory-format "o"))))
2135 (define-key gdb-memory-format-menu [unsigned]
2136 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2137 :button (:radio . (equal gdb-memory-format "u"))))
2138 (define-key gdb-memory-format-menu [signed]
2139 '(menu-item "Signed Decimal" gdb-memory-format-signed
2140 :button (:radio . (equal gdb-memory-format "d"))))
2141 (define-key gdb-memory-format-menu [hexadecimal]
2142 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2143 :button (:radio . (equal gdb-memory-format "x"))))
2145 (defun gdb-memory-format-menu (event)
2147 (x-popup-menu event gdb-memory-format-menu))
2149 (defun gdb-memory-format-menu-1 (event)
2151 (save-selected-window
2152 (select-window (posn-window (event-start event)))
2153 (let* ((selection (gdb-memory-format-menu event))
2154 (binding (and selection (lookup-key gdb-memory-format-menu
2155 (vector (car selection))))))
2156 (if binding (call-interactively binding)))))
2158 (defun gdb-memory-unit-giant ()
2159 "Set the unit size to giant words (eight bytes)."
2161 (customize-set-variable 'gdb-memory-unit "g")
2162 (gdb-invalidate-memory))
2164 (defun gdb-memory-unit-word ()
2165 "Set the unit size to words (four bytes)."
2167 (customize-set-variable 'gdb-memory-unit "w")
2168 (gdb-invalidate-memory))
2170 (defun gdb-memory-unit-halfword ()
2171 "Set the unit size to halfwords (two bytes)."
2173 (customize-set-variable 'gdb-memory-unit "h")
2174 (gdb-invalidate-memory))
2176 (defun gdb-memory-unit-byte ()
2177 "Set the unit size to bytes."
2179 (customize-set-variable 'gdb-memory-unit "b")
2180 (gdb-invalidate-memory))
2182 (defvar gdb-memory-unit-map
2183 (let ((map (make-sparse-keymap)))
2184 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2186 "Keymap to select units in the header line.")
2188 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2189 "Menu of units in the header line.")
2191 (define-key gdb-memory-unit-menu [giantwords]
2192 '(menu-item "Giant words" gdb-memory-unit-giant
2193 :button (:radio . (equal gdb-memory-unit "g"))))
2194 (define-key gdb-memory-unit-menu [words]
2195 '(menu-item "Words" gdb-memory-unit-word
2196 :button (:radio . (equal gdb-memory-unit "w"))))
2197 (define-key gdb-memory-unit-menu [halfwords]
2198 '(menu-item "Halfwords" gdb-memory-unit-halfword
2199 :button (:radio . (equal gdb-memory-unit "h"))))
2200 (define-key gdb-memory-unit-menu [bytes]
2201 '(menu-item "Bytes" gdb-memory-unit-byte
2202 :button (:radio . (equal gdb-memory-unit "b"))))
2204 (defun gdb-memory-unit-menu (event)
2206 (x-popup-menu event gdb-memory-unit-menu))
2208 (defun gdb-memory-unit-menu-1 (event)
2210 (save-selected-window
2211 (select-window (posn-window (event-start event)))
2212 (let* ((selection (gdb-memory-unit-menu event))
2213 (binding (and selection (lookup-key gdb-memory-unit-menu
2214 (vector (car selection))))))
2215 (if binding (call-interactively binding)))))
2217 ;;from make-mode-line-mouse-map
2218 (defun gdb-make-header-line-mouse-map (mouse function) "\
2219 Return a keymap with single entry for mouse key MOUSE on the header line.
2220 MOUSE is defined to run function FUNCTION with no args in the buffer
2221 corresponding to the mode line clicked."
2222 (let ((map (make-sparse-keymap)))
2223 (define-key map (vector 'header-line mouse) function)
2224 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2227 (defvar gdb-memory-font-lock-keywords
2228 '(;; <__function.name+n>
2229 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2231 "Font lock keywords used in `gdb-memory-mode'.")
2233 (defun gdb-memory-mode ()
2234 "Major mode for examining memory.
2236 \\{gdb-memory-mode-map}"
2237 (kill-all-local-variables)
2238 (setq major-mode 'gdb-memory-mode)
2239 (setq mode-name "Memory")
2240 (setq buffer-read-only t)
2241 (use-local-map gdb-memory-mode-map)
2242 (setq header-line-format
2248 'face font-lock-warning-face
2249 'help-echo "mouse-1: Decrement address"
2250 'mouse-face 'mode-line-highlight
2252 (gdb-make-header-line-mouse-map
2254 #'(lambda () (interactive)
2255 (let ((gdb-memory-address
2256 ;; Let GDB do the arithmetic.
2258 gdb-memory-address " - "
2260 (* gdb-memory-repeat-count
2261 (cond ((string= gdb-memory-unit "b") 1)
2262 ((string= gdb-memory-unit "h") 2)
2263 ((string= gdb-memory-unit "w") 4)
2264 ((string= gdb-memory-unit "g") 8)))))))
2265 (gdb-invalidate-memory)))))
2268 'face font-lock-warning-face
2269 'help-echo "mouse-1: Increment address"
2270 'mouse-face 'mode-line-highlight
2271 'local-map (gdb-make-header-line-mouse-map
2273 #'(lambda () (interactive)
2274 (let ((gdb-memory-address nil))
2275 (gdb-invalidate-memory)))))
2277 (propertize gdb-memory-address
2278 'face font-lock-warning-face
2279 'help-echo "mouse-1: Set memory address"
2280 'mouse-face 'mode-line-highlight
2281 'local-map (gdb-make-header-line-mouse-map
2283 #'gdb-memory-set-address))
2285 (propertize (number-to-string gdb-memory-repeat-count)
2286 'face font-lock-warning-face
2287 'help-echo "mouse-1: Set repeat count"
2288 'mouse-face 'mode-line-highlight
2289 'local-map (gdb-make-header-line-mouse-map
2291 #'gdb-memory-set-repeat-count))
2293 (propertize gdb-memory-format
2294 'face font-lock-warning-face
2295 'help-echo "mouse-3: Select display format"
2296 'mouse-face 'mode-line-highlight
2297 'local-map gdb-memory-format-map)
2299 (propertize gdb-memory-unit
2300 'face font-lock-warning-face
2301 'help-echo "mouse-3: Select unit size"
2302 'mouse-face 'mode-line-highlight
2303 'local-map gdb-memory-unit-map))))
2304 (set (make-local-variable 'font-lock-defaults)
2305 '(gdb-memory-font-lock-keywords))
2306 (run-mode-hooks 'gdb-memory-mode-hook)
2307 'gdb-invalidate-memory)
2309 (defun gdb-memory-buffer-name ()
2310 (with-current-buffer gud-comint-buffer
2311 (concat "*memory of " (gdb-get-target-string) "*")))
2313 (defun gdb-display-memory-buffer ()
2314 "Display memory contents."
2317 (gdb-get-create-buffer 'gdb-memory-buffer)))
2319 (defun gdb-frame-memory-buffer ()
2320 "Display memory contents in a new frame."
2322 (let ((special-display-regexps (append special-display-regexps '(".*")))
2323 (special-display-frame-alist gdb-frame-parameters))
2324 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
2329 (gdb-set-buffer-rules 'gdb-locals-buffer
2330 'gdb-locals-buffer-name
2333 (def-gdb-auto-update-trigger gdb-invalidate-locals
2334 (gdb-get-buffer 'gdb-locals-buffer)
2335 "server info locals\n"
2336 gdb-info-locals-handler)
2338 (defvar gdb-locals-watch-map
2339 (let ((map (make-sparse-keymap)))
2340 (define-key map "\r" '(lambda () (interactive)
2343 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2344 (mouse-set-point event)
2348 "Keymap to create watch expression of a complex data type local variable.")
2350 (defconst gdb-struct-string
2351 (concat (propertize "[struct/union]"
2352 'mouse-face 'highlight
2353 'help-echo "mouse-2: create watch expression"
2354 'local-map gdb-locals-watch-map) "\n"))
2356 (defconst gdb-array-string
2357 (concat " " (propertize "[array]"
2358 'mouse-face 'highlight
2359 'help-echo "mouse-2: create watch expression"
2360 'local-map gdb-locals-watch-map) "\n"))
2362 ;; Abbreviate for arrays and structures.
2363 ;; These can be expanded using gud-display.
2364 (defun gdb-info-locals-handler ()
2365 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2366 gdb-pending-triggers))
2367 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2368 (with-current-buffer buf
2369 (goto-char (point-min))
2370 (while (re-search-forward "^[ }].*\n" nil t)
2371 (replace-match "" nil nil))
2372 (goto-char (point-min))
2373 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2374 (replace-match gdb-struct-string nil nil))
2375 (goto-char (point-min))
2376 (while (re-search-forward "\\s-*{.*\n" nil t)
2377 (replace-match gdb-array-string nil nil))))
2378 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2380 (with-current-buffer buf
2381 (let* ((window (get-buffer-window buf 0))
2382 (p (window-point window))
2383 (buffer-read-only nil))
2385 (insert-buffer-substring (gdb-get-create-buffer
2386 'gdb-partial-output-buffer))
2387 (set-window-point window p)))))
2388 (run-hooks 'gdb-info-locals-hook))
2390 (defvar gdb-locals-mode-map
2391 (let ((map (make-sparse-keymap)))
2392 (suppress-keymap map)
2393 (define-key map "q" 'kill-this-buffer)
2396 (defun gdb-locals-mode ()
2397 "Major mode for gdb locals.
2399 \\{gdb-locals-mode-map}"
2400 (kill-all-local-variables)
2401 (setq major-mode 'gdb-locals-mode)
2402 (setq mode-name (concat "Locals:" gdb-selected-frame))
2403 (setq buffer-read-only t)
2404 (use-local-map gdb-locals-mode-map)
2405 (set (make-local-variable 'font-lock-defaults)
2406 '(gdb-locals-font-lock-keywords))
2407 (run-mode-hooks 'gdb-locals-mode-hook)
2408 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2409 (string-equal gdb-version "pre-6.4"))
2410 'gdb-invalidate-locals
2411 'gdb-invalidate-locals-1))
2413 (defun gdb-locals-buffer-name ()
2414 (with-current-buffer gud-comint-buffer
2415 (concat "*locals of " (gdb-get-target-string) "*")))
2417 (defun gdb-display-locals-buffer ()
2418 "Display local variables of current stack and their values."
2421 (gdb-get-create-buffer 'gdb-locals-buffer)))
2423 (defun gdb-frame-locals-buffer ()
2424 "Display local variables of current stack and their values in a new frame."
2426 (let ((special-display-regexps (append special-display-regexps '(".*")))
2427 (special-display-frame-alist gdb-frame-parameters))
2428 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
2431 ;;;; Window management
2432 (defun gdb-display-buffer (buf &optional size)
2433 (let ((answer (get-buffer-window buf 0))
2436 (display-buffer buf nil 0) ;Raise the frame if necessary.
2437 ;; The buffer is not yet displayed.
2438 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2439 (let ((window (get-lru-window)))
2441 (not (eq window (get-buffer-window gud-comint-buffer))))
2443 (set-window-buffer window buf)
2444 (setq answer window))
2445 (setq must-split t)))
2447 (let* ((largest (get-largest-window))
2448 (cur-size (window-height largest))
2449 (new-size (and size (< size cur-size) (- cur-size size))))
2450 (setq answer (split-window largest new-size))
2451 (set-window-buffer answer buf)
2452 (set-window-dedicated-p answer t)))
2456 ;;; Shared keymap initialization:
2458 (let ((menu (make-sparse-keymap "GDB-Windows")))
2459 (define-key gud-menu-map [displays]
2460 `(menu-item "GDB-Windows" ,menu
2461 :visible (memq gud-minor-mode '(gdbmi gdba))))
2462 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2463 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2464 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2465 (define-key menu [disassembly]
2466 '("Disassembly" . gdb-display-assembler-buffer))
2467 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2468 (define-key menu [inferior]
2469 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2470 :enable gdb-use-separate-io-buffer))
2471 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2472 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2473 (define-key menu [breakpoints]
2474 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2476 (let ((menu (make-sparse-keymap "GDB-Frames")))
2477 (define-key gud-menu-map [frames]
2478 `(menu-item "GDB-Frames" ,menu
2479 :visible (memq gud-minor-mode '(gdbmi gdba))))
2480 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2481 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2482 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2483 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2484 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2485 (define-key menu [inferior]
2486 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2487 :enable gdb-use-separate-io-buffer))
2488 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2489 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2490 (define-key menu [breakpoints]
2491 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2493 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2494 (define-key gud-menu-map [ui]
2495 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2496 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2497 (define-key menu [gdb-use-separate-io]
2498 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2499 :visible (eq gud-minor-mode 'gdba)
2500 :help "Toggle separate IO for inferior."
2501 :button (:toggle . gdb-use-separate-io-buffer)))
2502 (define-key menu [gdb-many-windows]
2503 '(menu-item "Display Other Windows" gdb-many-windows
2504 :help "Toggle display of locals, stack and breakpoint information"
2505 :button (:toggle . gdb-many-windows)))
2506 (define-key menu [gdb-restore-windows]
2507 '(menu-item "Restore Window Layout" gdb-restore-windows
2508 :help "Restore standard layout for debug session.")))
2510 (defun gdb-frame-gdb-buffer ()
2511 "Display GUD buffer in a new frame."
2513 (let ((special-display-regexps (append special-display-regexps '(".*")))
2514 (special-display-frame-alist gdb-frame-parameters)
2515 (same-window-regexps nil))
2516 (display-buffer gud-comint-buffer)))
2518 (defun gdb-display-gdb-buffer ()
2519 "Display GUD buffer."
2521 (let ((same-window-regexps nil))
2522 (pop-to-buffer gud-comint-buffer)))
2524 (defun gdb-set-window-buffer (name)
2525 (set-window-buffer (selected-window) (get-buffer name))
2526 (set-window-dedicated-p (selected-window) t))
2528 (defun gdb-setup-windows ()
2529 "Layout the window pattern for `gdb-many-windows'."
2530 (gdb-display-locals-buffer)
2531 (gdb-display-stack-buffer)
2532 (delete-other-windows)
2533 (gdb-display-breakpoints-buffer)
2534 (delete-other-windows)
2536 (pop-to-buffer gud-comint-buffer)
2537 (split-window nil ( / ( * (window-height) 3) 4))
2538 (split-window nil ( / (window-height) 3))
2539 (split-window-horizontally)
2541 (gdb-set-window-buffer (gdb-locals-buffer-name))
2544 (if gud-last-last-frame
2545 (gud-find-file (car gud-last-last-frame))
2546 (gud-find-file gdb-main-file)))
2547 (when gdb-use-separate-io-buffer
2548 (split-window-horizontally)
2550 (gdb-set-window-buffer
2551 (gdb-get-create-buffer 'gdb-inferior-io)))
2553 (gdb-set-window-buffer (gdb-stack-buffer-name))
2554 (split-window-horizontally)
2556 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2559 (defcustom gdb-many-windows nil
2560 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2561 In this case it starts with two windows: one displaying the GUD
2562 buffer and the other with the source file with the main routine
2563 of the inferior. Non-nil means display the layout shown for
2569 (defun gdb-many-windows (arg)
2570 "Toggle the number of windows in the basic arrangement.
2571 With arg, display additional buffers iff arg is positive."
2573 (setq gdb-many-windows
2575 (not gdb-many-windows)
2576 (> (prefix-numeric-value arg) 0)))
2577 (message (format "Display of other windows %sabled"
2578 (if gdb-many-windows "en" "dis")))
2579 (if (and gud-comint-buffer
2580 (buffer-name gud-comint-buffer))
2582 (gdb-restore-windows)
2585 (defun gdb-restore-windows ()
2586 "Restore the basic arrangement of windows used by gdba.
2587 This arrangement depends on the value of `gdb-many-windows'."
2589 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2590 (delete-other-windows)
2591 (if gdb-many-windows
2593 (when (or gud-last-last-frame gdb-show-main)
2597 (if gud-last-last-frame
2598 (gud-find-file (car gud-last-last-frame))
2599 (gud-find-file gdb-main-file)))
2603 "Exit a debugging session cleanly.
2604 Kills the gdb buffers and resets the source buffers."
2605 (dolist (buffer (buffer-list))
2606 (unless (eq buffer gud-comint-buffer)
2607 (with-current-buffer buffer
2608 (if (memq gud-minor-mode '(gdbmi gdba))
2609 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2611 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2612 (setq gud-minor-mode nil)
2613 (kill-local-variable 'tool-bar-map)
2614 (kill-local-variable 'gdb-define-alist))))))
2615 (when (markerp gdb-overlay-arrow-position)
2616 (move-marker gdb-overlay-arrow-position nil)
2617 (setq gdb-overlay-arrow-position nil))
2618 (setq overlay-arrow-variable-list
2619 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2620 (setq gud-running nil)
2621 (setq gdb-active-process nil)
2622 (setq gdb-var-list nil)
2623 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2625 (defun gdb-source-info ()
2626 "Find the source file where the program starts and displays it with related
2628 (goto-char (point-min))
2629 (if (and (search-forward "Located in " nil t)
2630 (looking-at "\\S-+"))
2631 (setq gdb-main-file (match-string 0)))
2632 (goto-char (point-min))
2633 (if (search-forward "Includes preprocessor macro info." nil t)
2634 (setq gdb-macro-info t))
2635 (if gdb-many-windows
2637 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2639 (let ((pop-up-windows t))
2640 (display-buffer (gud-find-file gdb-main-file))))))
2642 (defun gdb-get-location (bptno line flag)
2643 "Find the directory containing the relevant source file.
2644 Put in buffer and place breakpoint icon."
2645 (goto-char (point-min))
2646 (catch 'file-not-found
2647 (if (search-forward "Located in " nil t)
2648 (when (looking-at "\\S-+")
2649 (delete (cons bptno "File not found") gdb-location-alist)
2650 (push (cons bptno (match-string 0)) gdb-location-alist))
2652 (unless (assoc bptno gdb-location-alist)
2653 (push (cons bptno "File not found") gdb-location-alist)
2654 (message-box "Cannot find source file for breakpoint location.\n\
2655 Add directory to search path for source files using the GDB command, dir."))
2656 (throw 'file-not-found nil))
2657 (with-current-buffer
2658 (find-file-noselect (match-string 0))
2659 (save-current-buffer
2660 (set (make-local-variable 'gud-minor-mode) 'gdba)
2661 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2662 ;; only want one breakpoint icon at each location
2664 (goto-line (string-to-number line))
2665 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2667 (add-hook 'find-file-hook 'gdb-find-file-hook)
2669 (defun gdb-find-file-hook ()
2670 "Set up buffer for debugging if file is part of the source code
2671 of the current session."
2672 (if (and (buffer-name gud-comint-buffer)
2673 ;; in case gud or gdb-ui is just loaded
2675 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2677 (if (member buffer-file-name gdb-source-file-list)
2678 (with-current-buffer (find-buffer-visiting buffer-file-name)
2679 (set (make-local-variable 'gud-minor-mode)
2680 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2681 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2684 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2685 "Put string PUTSTRING in front of POS in the current buffer.
2686 PUTSTRING is displayed by putting an overlay into the current buffer with a
2687 `before-string' string that has a `display' property whose value is
2689 (let ((string (make-string 1 ?x))
2690 (buffer (current-buffer)))
2691 (setq putstring (copy-sequence putstring))
2692 (let ((overlay (make-overlay pos pos buffer))
2694 (list (list 'margin 'left-margin) putstring))))
2695 (put-text-property 0 1 'display prop string)
2697 (add-text-properties 0 1 sprops string))
2698 (overlay-put overlay 'put-break t)
2699 (overlay-put overlay 'before-string string))))
2701 ;;from remove-images
2702 (defun gdb-remove-strings (start end &optional buffer)
2703 "Remove strings between START and END in BUFFER.
2704 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2705 BUFFER nil or omitted means use the current buffer."
2707 (setq buffer (current-buffer)))
2708 (dolist (overlay (overlays-in start end))
2709 (when (overlay-get overlay 'put-break)
2710 (delete-overlay overlay))))
2712 (defun gdb-put-breakpoint-icon (enabled bptno)
2713 (let ((start (- (line-beginning-position) 1))
2714 (end (+ (line-end-position) 1))
2715 (putstring (if enabled "B" "b"))
2716 (source-window (get-buffer-window (current-buffer) 0)))
2717 (add-text-properties
2718 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2721 (add-text-properties
2722 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2723 (add-text-properties
2724 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2725 (gdb-remove-breakpoint-icons start end)
2726 (if (display-images-p)
2727 (if (>= (or left-fringe-width
2728 (if source-window (car (window-fringes source-window)))
2729 gdb-buffer-fringe-width) 8)
2732 `(left-fringe breakpoint
2735 'breakpoint-disabled))
2737 'gdb-enabled enabled)
2738 (when (< left-margin-width 2)
2739 (save-current-buffer
2740 (setq left-margin-width 2)
2744 left-margin-width right-margin-width))))
2747 (or breakpoint-enabled-icon
2748 (setq breakpoint-enabled-icon
2749 (find-image `((:type xpm :data
2750 ,breakpoint-xpm-data
2751 :ascent 100 :pointer hand)
2753 ,breakpoint-enabled-pbm-data
2754 :ascent 100 :pointer hand)))))
2755 (or breakpoint-disabled-icon
2756 (setq breakpoint-disabled-icon
2757 (find-image `((:type xpm :data
2758 ,breakpoint-xpm-data
2759 :conversion disabled
2760 :ascent 100 :pointer hand)
2762 ,breakpoint-disabled-pbm-data
2763 :ascent 100 :pointer hand))))))
2767 (when (< left-margin-width 2)
2768 (save-current-buffer
2769 (setq left-margin-width 2)
2770 (let ((window (get-buffer-window (current-buffer) 0)))
2773 window left-margin-width right-margin-width)))))
2775 (propertize putstring
2776 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2779 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2780 (gdb-remove-strings start end)
2781 (if (display-images-p)
2782 (remove-images start end))
2784 (setq left-margin-width 0)
2785 (let ((window (get-buffer-window (current-buffer) 0)))
2788 window left-margin-width right-margin-width)))))
2792 ;; Assembler buffer.
2794 (gdb-set-buffer-rules 'gdb-assembler-buffer
2795 'gdb-assembler-buffer-name
2796 'gdb-assembler-mode)
2798 (def-gdb-auto-update-handler gdb-assembler-handler
2799 gdb-invalidate-assembler
2800 gdb-assembler-buffer
2801 gdb-assembler-custom)
2803 (defun gdb-assembler-custom ()
2804 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2805 (pos 1) (address) (flag) (bptno))
2806 (with-current-buffer buffer
2808 (if (not (equal gdb-frame-address "main"))
2810 (goto-char (point-min))
2811 (if (and gdb-frame-address
2812 (search-forward gdb-frame-address nil t))
2816 (or gdb-overlay-arrow-position
2817 (setq gdb-overlay-arrow-position (make-marker)))
2818 (set-marker gdb-overlay-arrow-position
2819 (point) (current-buffer))))))
2820 ;; remove all breakpoint-icons in assembler buffer before updating.
2821 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2822 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2823 (goto-char (point-min))
2824 (while (< (point) (- (point-max) 1))
2826 (if (looking-at "[^\t].*?breakpoint")
2829 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2830 (setq bptno (match-string 1))
2831 (setq flag (char-after (match-beginning 2)))
2832 (setq address (match-string 3))
2833 (with-current-buffer buffer
2835 (goto-char (point-min))
2836 (if (search-forward address nil t)
2837 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2838 (if (not (equal gdb-frame-address "main"))
2839 (with-current-buffer buffer
2840 (set-window-point (get-buffer-window buffer 0) pos)))))
2842 (defvar gdb-assembler-mode-map
2843 (let ((map (make-sparse-keymap)))
2844 (suppress-keymap map)
2845 (define-key map "q" 'kill-this-buffer)
2848 (defvar gdb-assembler-font-lock-keywords
2849 '(;; <__function.name+n>
2850 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2851 (1 font-lock-function-name-face))
2852 ;; 0xNNNNNNNN <__function.name+n>: opcode
2853 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2854 (4 font-lock-keyword-face))
2855 ;; %register(at least i386)
2856 ("%\\sw+" . font-lock-variable-name-face)
2857 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2858 (1 font-lock-comment-face)
2859 (2 font-lock-function-name-face))
2860 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2861 "Font lock keywords used in `gdb-assembler-mode'.")
2863 (defun gdb-assembler-mode ()
2864 "Major mode for viewing code assembler.
2866 \\{gdb-assembler-mode-map}"
2867 (kill-all-local-variables)
2868 (setq major-mode 'gdb-assembler-mode)
2869 (setq mode-name (concat "Machine:" gdb-selected-frame))
2870 (setq gdb-overlay-arrow-position nil)
2871 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2872 (setq fringes-outside-margins t)
2873 (setq buffer-read-only t)
2874 (use-local-map gdb-assembler-mode-map)
2875 (gdb-invalidate-assembler)
2876 (set (make-local-variable 'font-lock-defaults)
2877 '(gdb-assembler-font-lock-keywords))
2878 (run-mode-hooks 'gdb-assembler-mode-hook)
2879 'gdb-invalidate-assembler)
2881 (defun gdb-assembler-buffer-name ()
2882 (with-current-buffer gud-comint-buffer
2883 (concat "*disassembly of " (gdb-get-target-string) "*")))
2885 (defun gdb-display-assembler-buffer ()
2886 "Display disassembly view."
2888 (setq gdb-previous-frame nil)
2890 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2892 (defun gdb-frame-assembler-buffer ()
2893 "Display disassembly view in a new frame."
2895 (setq gdb-previous-frame nil)
2896 (let ((special-display-regexps (append special-display-regexps '(".*")))
2897 (special-display-frame-alist gdb-frame-parameters))
2898 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2900 ;; modified because if gdb-frame-address has changed value a new command
2901 ;; must be enqueued to update the buffer with the new output
2902 (defun gdb-invalidate-assembler (&optional ignored)
2903 (if (gdb-get-buffer 'gdb-assembler-buffer)
2905 (unless (and gdb-selected-frame
2906 (string-equal gdb-selected-frame gdb-previous-frame))
2907 (if (or (not (member 'gdb-invalidate-assembler
2908 gdb-pending-triggers))
2909 (not (string-equal gdb-frame-address
2910 gdb-previous-frame-address)))
2912 ;; take previous disassemble command, if any, off the queue
2913 (with-current-buffer gud-comint-buffer
2914 (let ((queue gdb-input-queue))
2915 (dolist (item queue)
2916 (if (equal (cdr item) '(gdb-assembler-handler))
2917 (setq gdb-input-queue
2918 (delete item gdb-input-queue))))))
2921 (concat gdb-server-prefix "disassemble "
2922 (if (member gdb-frame-address '(nil "main")) nil "0x")
2923 gdb-frame-address "\n")
2924 'gdb-assembler-handler))
2925 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2926 (setq gdb-previous-frame-address gdb-frame-address)
2927 (setq gdb-previous-frame gdb-selected-frame)))))))
2929 (defun gdb-get-selected-frame ()
2930 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2933 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2934 (push 'gdb-get-selected-frame
2935 gdb-pending-triggers))))
2937 (defun gdb-frame-handler ()
2938 (setq gdb-pending-triggers
2939 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2940 (goto-char (point-min))
2941 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
2942 (setq gdb-frame-number (match-string 1)))
2943 (goto-char (point-min))
2944 (if (re-search-forward
2945 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
2947 (setq gdb-selected-frame (match-string 2))
2948 (if (gdb-get-buffer 'gdb-locals-buffer)
2949 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2950 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2951 (if (gdb-get-buffer 'gdb-assembler-buffer)
2952 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
2953 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2954 (setq gdb-frame-address (match-string 1))))
2955 (goto-char (point-min))
2956 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2957 (setq gdb-current-language (match-string 1)))
2958 (gdb-invalidate-assembler))
2961 ;; Code specific to GDB 6.4
2962 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
2964 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
2965 "Create list of source files for current GDB session.
2966 If buffers already exist for any of these files, gud-minor-mode
2968 (goto-char (point-min))
2969 (while (re-search-forward gdb-source-file-regexp-1 nil t)
2970 (push (match-string 1) gdb-source-file-list))
2971 (dolist (buffer (buffer-list))
2972 (with-current-buffer buffer
2973 (when (member buffer-file-name gdb-source-file-list)
2974 (set (make-local-variable 'gud-minor-mode)
2975 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
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)
2986 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2987 (concat "server interpreter mi \"-var-list-children --all-values "
2989 (concat "-var-list-children --all-values " varnum "\n"))
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 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) '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 (buffer-local-value 'gud-minor-mode gud-comint-buffer) '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 (and (gdb-get-buffer 'gdb-registers-buffer)
3124 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3128 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3129 "server interpreter mi -data-list-changed-registers\n"
3130 "-data-list-changed-registers\n")
3131 'gdb-get-changed-registers-handler))
3132 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3134 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3136 (defun gdb-get-changed-registers-handler ()
3137 (setq gdb-pending-triggers
3138 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3139 (setq gdb-changed-registers nil)
3140 (goto-char (point-min))
3141 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3142 (push (match-string 1) gdb-changed-registers)))
3147 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3148 (gdb-set-buffer-rules 'gdb-locals-buffer
3149 'gdb-locals-buffer-name
3152 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3153 (gdb-get-buffer 'gdb-locals-buffer)
3154 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3155 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3156 "-stack-list-locals --simple-values\n")
3157 gdb-stack-list-locals-handler)
3159 (defconst gdb-stack-list-locals-regexp
3160 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3162 (defvar gdb-locals-watch-map-1
3163 (let ((map (make-sparse-keymap)))
3164 (define-key map [mouse-2] 'gud-watch)
3166 "Keymap to create watch expression of a complex data type local variable.")
3168 ;; Dont display values of arrays or structures.
3169 ;; These can be expanded using gud-watch.
3170 (defun gdb-stack-list-locals-handler ()
3171 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3172 gdb-pending-triggers))
3173 (let (local locals-list)
3174 (goto-char (point-min))
3175 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3176 (let ((local (list (match-string 1)
3179 (if (looking-at ",value=\\(\".*\"\\)}")
3180 (setcar (nthcdr 2 local) (read (match-string 1))))
3181 (push local locals-list)))
3182 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3183 (and buf (with-current-buffer buf
3184 (let* ((window (get-buffer-window buf 0))
3185 (p (window-point window))
3186 (buffer-read-only nil))
3188 (dolist (local locals-list)
3189 (setq name (car local))
3190 (if (or (not (nth 2 local))
3191 (string-match "\\*$" (nth 1 local)))
3192 (add-text-properties 0 (length name)
3193 `(mouse-face highlight
3194 help-echo "mouse-2: create watch expression"
3195 local-map ,gdb-locals-watch-map-1)
3198 (concat name "\t" (nth 1 local)
3199 "\t" (nth 2 local) "\n")))
3200 (set-window-point window p)))))))
3202 (defun gdb-get-register-names ()
3203 "Create a list of register names."
3204 (goto-char (point-min))
3205 (setq gdb-register-names nil)
3206 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3207 (push (match-string 1) gdb-register-names)))
3211 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3212 ;;; gdb-ui.el ends here