1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This mode acts as a graphical user interface to GDB. You can interact with
29 ;; GDB through the GUD buffer in the usual way, but there are also further
30 ;; buffers which control the execution and describe the state of your program.
31 ;; It separates the input/output of your program from that of GDB, if
32 ;; required, and watches expressions in the speedbar. It also uses features of
33 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
34 ;; (see the GDB Graphical Interface section in the Emacs info manual).
36 ;; By default, M-x gdb will start the debugger. However, if you have customised
37 ;; gud-gdb-command-name, then start it with M-x gdba.
39 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
40 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
41 ;; You don't need to know about annotations to use this mode as a debugger,
42 ;; but if you are interested developing the mode itself, then see the
43 ;; Annotations section in the GDB info manual.
45 ;; GDB developers plan to make the annotation interface obsolete. A new
46 ;; interface called GDB/MI (machine interface) has been designed to replace
47 ;; it. Some GDB/MI commands are used in this file through the CLI command
48 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
49 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
50 ;; still under development and is part of a process to migrate Emacs from
51 ;; annotations to GDB/MI.
53 ;; This mode SHOULD WORK WITH GDB 5.0 ONWARDS but you will NEED GDB 6.0
54 ;; ONWARDS TO USE WATCH EXPRESSIONS.
58 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
59 ;; explicitly in your program if you want timely display of I/O in Emacs.
60 ;; Alternatively you can make the output stream unbuffered, for example, by
64 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
67 ;; and compiling with -DUNBUFFERED while debugging.
72 ;; 1) Use MI command -data-read-memory for memory window.
73 ;; 2) Highlight changed register values (use MI commands
74 ;; -data-list-register-values and -data-list-changed-registers instead
75 ;; of 'info registers'.
76 ;; 3) Use tree-widget.el instead of the speedbar for watch-expressions?
77 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
78 ;; 5) After release of 22.1 use '-var-list-children --all-values'
79 ;; and '-stack-list-locals 2' which need GDB 6.1 onwards.
87 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
88 (defvar gdb-previous-frame-address nil)
89 (defvar gdb-memory-address "main")
90 (defvar gdb-previous-frame nil)
91 (defvar gdb-selected-frame nil)
92 (defvar gdb-frame-number nil)
93 (defvar gdb-current-language nil)
94 (defvar gdb-var-list nil "List of variables in watch window.")
95 (defvar gdb-var-changed nil "Non-nil means that `gdb-var-list' has changed.")
96 (defvar gdb-main-file nil "Source file from which program execution begins.")
97 (defvar gdb-buffer-type nil)
98 (defvar gdb-overlay-arrow-position nil)
99 (defvar gdb-server-prefix nil)
100 (defvar gdb-flush-pending-output nil)
101 (defvar gdb-location-alist nil
102 "Alist of breakpoint numbers and full filenames.")
103 (defvar gdb-find-file-unhook nil)
104 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
105 and #define directives otherwise.")
106 (defvar gdb-error "Non-nil when GDB is reporting an error.")
107 (defvar gdb-macro-info nil
108 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
109 (defvar gdb-buffer-fringe-width nil)
111 (defvar gdb-buffer-type nil
112 "One of the symbols bound in `gdb-buffer-rules'.")
114 (defvar gdb-input-queue ()
115 "A list of gdb command objects.")
117 (defvar gdb-prompting nil
118 "True when gdb is idle with no pending input.")
120 (defvar gdb-output-sink 'user
121 "The disposition of the output of the current gdb command.
122 Possible values are these symbols:
124 `user' -- gdb output should be copied to the GUD buffer
127 `inferior' -- gdb output should be copied to the inferior-io buffer.
129 `pre-emacs' -- output should be ignored util the post-prompt
130 annotation is received. Then the output-sink
132 `emacs' -- output should be collected in the partial-output-buffer
133 for subsequent processing by a command. This is the
134 disposition of output generated by commands that
135 gdb mode sends to gdb on its own behalf.
136 `post-emacs' -- ignore output until the prompt annotation is
137 received, then go to USER disposition.
139 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
140 \(`user' and `emacs').")
142 (defvar gdb-current-item nil
143 "The most recent command item sent to gdb.")
145 (defvar gdb-pending-triggers '()
146 "A list of trigger functions that have run later than their output
149 ;; end of gdb variables
152 (defun gdba (command-line)
153 "Run gdb on program FILE in buffer *gud-FILE*.
154 The directory containing FILE becomes the initial working directory
155 and source-file directory for your debugger.
157 If `gdb-many-windows' is nil (the default value) then gdb just
158 pops up the GUD buffer unless `gdb-show-main' is t. In this case
159 it starts with two windows: one displaying the GUD buffer and the
160 other with the source file with the main routine of the inferior.
162 If `gdb-many-windows' is t, regardless of the value of
163 `gdb-show-main', the layout below will appear unless
164 `gdb-use-inferior-io-buffer' is nil when the source buffer
165 occupies the full width of the frame. Keybindings are given in
168 Watch expressions appear in the speedbar/slowbar.
170 The following commands help control operation :
172 `gdb-many-windows' - Toggle the number of windows gdb uses.
173 `gdb-restore-windows' - To restore the window layout.
175 See Info node `(emacs)GDB Graphical Interface' for a more
176 detailed description of this mode.
179 +--------------------------------------------------------------+
181 +-------------------------------+------------------------------+
182 | GUD buffer (I/O of GDB) | Locals buffer |
186 +-------------------------------+------------------------------+
187 | Source buffer | I/O buffer (of inferior) |
195 +-------------------------------+------------------------------+
196 | Stack buffer | Breakpoints buffer |
197 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
198 | | RET gdb-goto-breakpoint |
199 | | d gdb-delete-breakpoint |
200 +-------------------------------+------------------------------+"
202 (interactive (list (gud-query-cmdline 'gdba)))
204 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
208 (defvar gdb-debug-log nil)
211 (defcustom gdb-enable-debug-log nil
212 "Non-nil means record the process input and output in `gdb-debug-log'."
217 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
218 "Shell command for generating a list of defined macros in a source file.
219 This list is used to display the #define directive associated
220 with an identifier as a tooltip. It works in a debug session with
221 GDB, when gud-tooltip-mode is t.
223 Set `gdb-cpp-define-alist-flags' for any include paths or
229 (defcustom gdb-cpp-define-alist-flags ""
230 "Preprocessor flags for `gdb-cpp-define-alist-program'."
235 (defcustom gdb-show-main nil
236 "Non-nil means display source file containing the main routine at startup.
237 Also display the main routine in the disassembly buffer if present."
243 (defcustom gdb-use-inferior-io-buffer nil
244 "Non-nil means display output from the inferior in a separate buffer."
249 (defun gdb-use-inferior-io-buffer (arg)
250 "Toggle separate IO for inferior.
251 With arg, use separate IO iff arg is positive."
253 (setq gdb-use-inferior-io-buffer
255 (not gdb-use-inferior-io-buffer)
256 (> (prefix-numeric-value arg) 0)))
257 (if (and gud-comint-buffer
258 (buffer-name gud-comint-buffer))
260 (if gdb-use-inferior-io-buffer
261 (gdb-restore-windows)
262 (kill-buffer (gdb-inferior-io-name)))
265 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
267 (defun gdb-create-define-alist ()
268 "Create an alist of #define directives for GUD tooltips."
269 (let* ((file (buffer-file-name))
271 (with-output-to-string
272 (with-current-buffer standard-output
273 (call-process shell-file-name
274 (if (file-exists-p file) file nil)
275 (list t nil) nil "-c"
276 (concat gdb-cpp-define-alist-program " "
277 gdb-cpp-define-alist-flags)))))
278 (define-list (split-string output "\n" t))
280 (setq gdb-define-alist nil)
281 (dolist (define define-list)
282 (setq name (nth 1 (split-string define "[( ]")))
283 (push (cons name define) gdb-define-alist))))
285 (defun gdb-tooltip-print ()
287 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
288 (let ((string (buffer-string)))
289 ;; remove newline for gud-tooltip-echo-area
290 (substring string 0 (- (length string) 1))))
291 (or gud-tooltip-echo-area tooltip-use-echo-area)))
293 ;; If expr is a macro for a function don't print because of possible dangerous
294 ;; side-effects. Also printing a function within a tooltip generates an
295 ;; unexpected starting annotation (phase error).
296 (defun gdb-tooltip-print-1 (expr)
297 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
298 (goto-char (point-min))
299 (if (search-forward "expands to: " nil t)
300 (unless (looking-at "\\S-+.*(.*).*")
302 (list (concat gdb-server-prefix "print " expr "\n")
303 'gdb-tooltip-print))))))
305 (defun gdb-set-gud-minor-mode (buffer)
306 "Set `gud-minor-mode' from find-file if appropriate."
307 (goto-char (point-min))
308 (unless (search-forward "No source file named " nil t)
311 (list (concat gdb-server-prefix "info source\n")
312 `(lambda () (gdb-set-gud-minor-mode-1 ,buffer))))
313 (error (setq gdb-find-file-unhook t)))))
315 (defun gdb-set-gud-minor-mode-1 (buffer)
316 (goto-char (point-min))
317 (when (and (search-forward "Located in " nil t)
319 (string-equal (buffer-file-name buffer)
321 (with-current-buffer buffer
322 (set (make-local-variable 'gud-minor-mode) 'gdba)
323 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
324 (when gud-tooltip-mode
325 (make-local-variable 'gdb-define-alist)
326 (gdb-create-define-alist)
327 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
329 (defun gdb-set-gud-minor-mode-existing-buffers ()
330 (dolist (buffer (buffer-list))
331 (let ((file (buffer-file-name buffer)))
335 (list (concat gdb-server-prefix "list "
336 (file-name-nondirectory file) ":1\n")
337 `(lambda () (gdb-set-gud-minor-mode ,buffer)))))))))
340 (setq gdb-debug-log nil)
341 (set (make-local-variable 'gud-minor-mode) 'gdba)
342 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
344 (gud-def gud-break (if (not (string-match "Machine" mode-name))
345 (gud-call "break %f:%l" arg)
349 (gud-call "break *%a" arg)))
350 "\C-b" "Set breakpoint at current line or address.")
352 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
353 (gud-call "clear %f:%l" arg)
357 (gud-call "clear *%a" arg)))
358 "\C-d" "Remove breakpoint at current line or address.")
360 (gud-def gud-until (if (not (string-match "Machine" mode-name))
361 (gud-call "until %f:%l" arg)
365 (gud-call "until *%a" arg)))
366 "\C-u" "Continue to current line or address.")
368 (define-key gud-minor-mode-map [left-margin mouse-1]
369 'gdb-mouse-set-clear-breakpoint)
370 (define-key gud-minor-mode-map [left-fringe mouse-1]
371 'gdb-mouse-set-clear-breakpoint)
372 (define-key gud-minor-mode-map [left-fringe mouse-2]
374 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
376 (define-key gud-minor-mode-map [left-margin mouse-3]
377 'gdb-mouse-toggle-breakpoint-margin)
378 (define-key gud-minor-mode-map [left-fringe mouse-3]
379 'gdb-mouse-toggle-breakpoint-fringe)
381 (setq comint-input-sender 'gdb-send)
384 (setq gdb-frame-address (if gdb-show-main "main" nil))
385 (setq gdb-previous-frame-address nil
386 gdb-memory-address "main"
387 gdb-previous-frame nil
388 gdb-selected-frame nil
389 gdb-current-language nil
397 gdb-pending-triggers nil
398 gdb-output-sink 'user
399 gdb-server-prefix "server "
400 gdb-flush-pending-output nil
401 gdb-location-alist nil
402 gdb-find-file-unhook nil
405 gdb-buffer-fringe-width (car (window-fringes)))
407 (setq gdb-buffer-type 'gdba)
409 (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
411 (if (eq window-system 'w32)
412 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
413 (gdb-enqueue-input (list "set height 0\n" 'ignore))
414 (gdb-enqueue-input (list "set width 0\n" 'ignore))
415 ;; find source file and compilation directory here
416 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
417 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
418 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
420 (gdb-set-gud-minor-mode-existing-buffers)
421 (run-hooks 'gdba-mode-hook))
423 (defun gdb-mouse-until (event)
424 "Execute source lines by dragging the overlay arrow (fringe) with the mouse."
426 (if gud-overlay-arrow-position
427 (let ((start (event-start event))
428 (end (event-end event))
429 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
430 (if (not (string-match "Machine" mode-name))
431 (if (equal buffer (window-buffer (posn-window end)))
432 (with-current-buffer buffer
433 (when (or (equal start end)
434 (equal (posn-point start)
436 gud-overlay-arrow-position)))
437 (setq line (line-number-at-pos (posn-point end)))
438 (gud-call (concat "until " (number-to-string line))))))
439 (if (equal (marker-buffer gdb-overlay-arrow-position)
440 (window-buffer (posn-window end)))
441 (when (or (equal start end)
442 (equal (posn-point start)
444 gdb-overlay-arrow-position)))
446 (goto-line (line-number-at-pos (posn-point end)))
448 (gud-call (concat "until *%a")))))))))
450 (defcustom gdb-use-colon-colon-notation nil
451 "If non-nil use FUN::VAR format to display variables in the speedbar."
457 "Watch expression at point."
460 (let ((expr (tooltip-identifier-from-point (point))))
461 (if (and (string-equal gdb-current-language "c")
462 gdb-use-colon-colon-notation gdb-selected-frame)
463 (setq expr (concat gdb-selected-frame "::" expr)))
464 (catch 'already-watched
465 (dolist (var gdb-var-list)
466 (if (string-equal expr (car var)) (throw 'already-watched nil)))
467 (set-text-properties 0 (length expr) nil expr)
470 (if (eq gud-minor-mode 'gdba)
471 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
472 (concat"-var-create - * " expr "\n"))
473 `(lambda () (gdb-var-create-handler ,expr))))))
474 (select-window (get-buffer-window gud-comint-buffer 0)))
476 (defconst gdb-var-create-regexp
477 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
479 (defun gdb-var-create-handler (expr)
480 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
481 (goto-char (point-min))
482 (if (re-search-forward gdb-var-create-regexp nil t)
483 (let ((var (list expr
488 (push var gdb-var-list)
490 (unless (string-equal
491 speedbar-initial-expansion-list-name "GUD")
492 (speedbar-change-initial-expansion-list "GUD"))
493 (if (equal (nth 2 var) "0")
496 (if (with-current-buffer
497 gud-comint-buffer (eq gud-minor-mode 'gdba))
498 (concat "server interpreter mi \"-var-evaluate-expression "
500 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
501 `(lambda () (gdb-var-evaluate-expression-handler
503 (setq gdb-var-changed t)))
504 (if (re-search-forward "Undefined command" nil t)
505 (message-box "Watching expressions requires gdb 6.0 onwards")
506 (message "No symbol \"%s\" in current context." expr)))))
508 (defun gdb-var-evaluate-expression-handler (varnum changed)
509 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
510 (goto-char (point-min))
511 (re-search-forward ".*value=\"\\(.*?\\)\"" nil t)
514 (dolist (var gdb-var-list)
515 (if (string-equal varnum (cadr var))
517 (if changed (setcar (nthcdr 5 var) t))
518 (setcar (nthcdr 4 var) (match-string 1))
519 (setcar (nthcdr num gdb-var-list) var)
520 (throw 'var-found nil)))
521 (setq num (+ num 1))))))
522 (setq gdb-var-changed t))
524 (defun gdb-var-list-children (varnum)
526 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
527 `(lambda () (gdb-var-list-children-handler ,varnum)))))
529 (defconst gdb-var-list-children-regexp
530 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\"")
532 (defun gdb-var-list-children-handler (varnum)
533 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
534 (goto-char (point-min))
535 (let ((var-list nil))
536 (catch 'child-already-watched
537 (dolist (var gdb-var-list)
538 (if (string-equal varnum (cadr var))
541 (while (re-search-forward gdb-var-list-children-regexp nil t)
542 (let ((varchild (list (match-string 2)
546 (if (looking-at ",type=\"\\(.*?\\)\"")
547 (setcar (nthcdr 3 varchild) (match-string 1)))
548 (dolist (var1 gdb-var-list)
549 (if (string-equal (cadr var1) (cadr varchild))
550 (throw 'child-already-watched nil)))
551 (push varchild var-list)
552 (if (equal (nth 2 varchild) "0")
556 "server interpreter mi \"-var-evaluate-expression "
557 (nth 1 varchild) "\"\n")
558 `(lambda () (gdb-var-evaluate-expression-handler
559 ,(nth 1 varchild) nil))))))))
560 (push var var-list)))
561 (setq gdb-var-list (nreverse var-list))))))
563 (defun gdb-var-update ()
564 (when (not (member 'gdb-var-update gdb-pending-triggers))
566 (list "server interpreter mi \"-var-update *\"\n"
567 'gdb-var-update-handler))
568 (push 'gdb-var-update gdb-pending-triggers)))
570 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
572 (defun gdb-var-update-handler ()
573 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
574 (goto-char (point-min))
575 (while (re-search-forward gdb-var-update-regexp nil t)
576 (let ((varnum (match-string 1)))
579 (concat "server interpreter mi \"-var-evaluate-expression "
581 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
582 (setq gdb-pending-triggers
583 (delq 'gdb-var-update gdb-pending-triggers))
584 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
585 ;; Dummy command to update speedbar at right time.
586 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
587 ;; Keep gdb-pending-triggers non-nil till end.
588 (push 'gdb-speedbar-timer gdb-pending-triggers)))
590 (defun gdb-speedbar-timer-fn ()
591 (setq gdb-pending-triggers
592 (delq 'gdb-speedbar-timer gdb-pending-triggers))
593 (with-current-buffer gud-comint-buffer
594 (speedbar-timer-fn)))
596 (defun gdb-var-delete ()
597 "Delete watch expression at point from the speedbar."
599 (if (with-current-buffer
600 gud-comint-buffer (memq gud-minor-mode '(gdbmi gdba)))
601 (let ((text (speedbar-line-text)))
602 (string-match "\\(\\S-+\\)" text)
603 (let* ((expr (match-string 1 text))
604 (var (assoc expr gdb-var-list))
606 (unless (string-match "\\." varnum)
609 (if (with-current-buffer gud-comint-buffer
610 (eq gud-minor-mode 'gdba))
611 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
612 (concat "-var-delete " varnum "\n"))
614 (setq gdb-var-list (delq var gdb-var-list))
615 (dolist (varchild gdb-var-list)
616 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
617 (setq gdb-var-list (delq varchild gdb-var-list))))
618 (setq gdb-var-changed t))))))
620 (defun gdb-edit-value (text token indent)
621 "Assign a value to a variable displayed in the speedbar."
622 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
623 (varnum (cadr var)) (value))
624 (setq value (read-string "New value: "))
627 (if (with-current-buffer gud-comint-buffer
628 (eq gud-minor-mode 'gdba))
629 (concat "server interpreter mi \"-var-assign "
630 varnum " " value "\"\n")
631 (concat "-var-assign " varnum " " value "\n"))
634 (defcustom gdb-show-changed-values t
635 "If non-nil highlight values that have recently changed in the speedbar.
636 The highlighting is done with `font-lock-warning-face'."
641 (defun gdb-speedbar-expand-node (text token indent)
642 "Expand the node the user clicked on.
643 TEXT is the text of the button we clicked on, a + or - item.
644 TOKEN is data related to this node.
645 INDENT is the current indentation depth."
646 (cond ((string-match "+" text) ;expand this node
647 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
648 (gdb-var-list-children token)
651 (gdbmi-var-list-children token))))
652 ((string-match "-" text) ;contract this node
653 (dolist (var gdb-var-list)
654 (if (string-match (concat token "\\.") (nth 1 var))
655 (setq gdb-var-list (delq var gdb-var-list))))
656 (setq gdb-var-changed t)
657 (with-current-buffer gud-comint-buffer
658 (speedbar-timer-fn)))))
660 (defun gdb-get-target-string ()
661 (with-current-buffer gud-comint-buffer
668 ;; Each buffer has a TYPE -- a symbol that identifies the function
669 ;; of that particular buffer.
671 ;; The usual gdb interaction buffer is given the type `gdba' and
672 ;; is constructed specially.
674 ;; Others are constructed by gdb-get-create-buffer and
675 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
677 (defvar gdb-buffer-rules-assoc '())
679 (defun gdb-get-buffer (key)
680 "Return the gdb buffer tagged with type KEY.
681 The key should be one of the cars in `gdb-buffer-rules-assoc'."
683 (gdb-look-for-tagged-buffer key (buffer-list))))
685 (defun gdb-get-create-buffer (key)
686 "Create a new gdb buffer of the type specified by KEY.
687 The key should be one of the cars in `gdb-buffer-rules-assoc'."
688 (or (gdb-get-buffer key)
689 (let* ((rules (assoc key gdb-buffer-rules-assoc))
690 (name (funcall (gdb-rules-name-maker rules)))
691 (new (get-buffer-create name)))
692 (with-current-buffer new
694 (if (cdr (cdr rules))
695 (setq trigger (funcall (car (cdr (cdr rules))))))
696 (set (make-local-variable 'gdb-buffer-type) key)
697 (set (make-local-variable 'gud-minor-mode)
698 (with-current-buffer gud-comint-buffer gud-minor-mode))
699 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
700 (if trigger (funcall trigger)))
703 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
705 (defun gdb-look-for-tagged-buffer (key bufs)
707 (while (and (not retval) bufs)
708 (set-buffer (car bufs))
709 (if (eq gdb-buffer-type key)
710 (setq retval (car bufs)))
711 (setq bufs (cdr bufs)))
715 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
716 ;; at least one and possible more functions. The functions have these
717 ;; roles in defining a buffer type:
719 ;; NAME - Return a name for this buffer type.
721 ;; The remaining function(s) are optional:
723 ;; MODE - called in a new buffer with no arguments, should establish
724 ;; the proper mode for the buffer.
727 (defun gdb-set-buffer-rules (buffer-type &rest rules)
728 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
730 (setcdr binding rules)
731 (push (cons buffer-type rules)
732 gdb-buffer-rules-assoc))))
734 ;; GUD buffers are an exception to the rules
735 (gdb-set-buffer-rules 'gdba 'error)
738 ;; Partial-output buffer : This accumulates output from a command executed on
739 ;; behalf of emacs (rather than the user).
741 (gdb-set-buffer-rules 'gdb-partial-output-buffer
742 'gdb-partial-output-name)
744 (defun gdb-partial-output-name ()
745 (concat "*partial-output-"
746 (gdb-get-target-string)
750 (gdb-set-buffer-rules 'gdb-inferior-io
751 'gdb-inferior-io-name
752 'gdb-inferior-io-mode)
754 (defun gdb-inferior-io-name ()
755 (concat "*input/output of "
756 (gdb-get-target-string)
759 (defun gdb-display-inferior-io-buffer ()
760 "Display IO of inferior in a separate window."
762 (if gdb-use-inferior-io-buffer
764 (gdb-get-create-buffer 'gdb-inferior-io))))
766 (defconst gdb-frame-parameters
767 '((height . 14) (width . 80)
769 (tool-bar-lines . nil)
770 (menu-bar-lines . nil)
773 (defun gdb-frame-inferior-io-buffer ()
774 "Display IO of inferior in a new frame."
776 (if gdb-use-inferior-io-buffer
777 (let ((special-display-regexps (append special-display-regexps '(".*")))
778 (special-display-frame-alist gdb-frame-parameters))
779 (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
781 (defvar gdb-inferior-io-mode-map
782 (let ((map (make-sparse-keymap)))
783 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
784 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
785 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
786 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
787 (define-key map "\C-d" 'gdb-inferior-io-eof)
790 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
791 "Major mode for gdb inferior-io."
792 :syntax-table nil :abbrev-table nil
793 ;; We want to use comint because it has various nifty and familiar
794 ;; features. We don't need a process, but comint wants one, so create
796 (make-comint-in-buffer
797 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
798 (current-buffer) "hexl")
799 (setq comint-input-sender 'gdb-inferior-io-sender))
801 (defun gdb-inferior-io-sender (proc string)
802 ;; PROC is the pseudo-process created to satisfy comint.
803 (with-current-buffer (process-buffer proc)
804 (setq proc (get-buffer-process gud-comint-buffer))
805 (process-send-string proc string)
806 (process-send-string proc "\n")))
808 (defun gdb-inferior-io-interrupt ()
809 "Interrupt the program being debugged."
812 (get-buffer-process gud-comint-buffer) comint-ptyp))
814 (defun gdb-inferior-io-quit ()
815 "Send quit signal to the program being debugged."
818 (get-buffer-process gud-comint-buffer) comint-ptyp))
820 (defun gdb-inferior-io-stop ()
821 "Stop the program being debugged."
824 (get-buffer-process gud-comint-buffer) comint-ptyp))
826 (defun gdb-inferior-io-eof ()
827 "Send end-of-file to the program being debugged."
830 (get-buffer-process gud-comint-buffer)))
834 ;; gdb communications
837 ;; INPUT: things sent to gdb
839 ;; The queues are lists. Each element is either a string (indicating user or
840 ;; user-like input) or a list of the form:
842 ;; (INPUT-STRING HANDLER-FN)
844 ;; The handler function will be called from the partial-output buffer when the
845 ;; command completes. This is the way to write commands which invoke gdb
846 ;; commands autonomously.
848 ;; These lists are consumed tail first.
851 (defun gdb-send (proc string)
852 "A comint send filter for gdb.
853 This filter may simply queue input for a later time."
854 (with-current-buffer gud-comint-buffer
855 (let ((inhibit-read-only t))
856 (remove-text-properties (point-min) (point-max) '(face))))
857 (let ((item (concat string "\n")))
860 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
861 (process-send-string proc item))
862 (gdb-enqueue-input item))))
864 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
865 ;; is a query, or other non-top-level prompt.
867 (defun gdb-enqueue-input (item)
871 (setq gdb-prompting nil))
872 (push item gdb-input-queue)))
874 (defun gdb-dequeue-input ()
875 (let ((queue gdb-input-queue))
877 (let ((last (car (last queue))))
878 (unless (nbutlast queue) (setq gdb-input-queue '()))
881 (defun gdb-send-item (item)
882 (setq gdb-flush-pending-output nil)
883 (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
884 (setq gdb-current-item item)
885 (with-current-buffer gud-comint-buffer
886 (if (eq gud-minor-mode 'gdba)
889 (setq gdb-output-sink 'user)
890 (process-send-string (get-buffer-process gud-comint-buffer) item))
892 (gdb-clear-partial-output)
893 (setq gdb-output-sink 'pre-emacs)
894 (process-send-string (get-buffer-process gud-comint-buffer)
896 ;; case: eq gud-minor-mode 'gdbmi
897 (gdb-clear-partial-output)
898 (setq gdb-output-sink 'emacs)
899 (process-send-string (get-buffer-process gud-comint-buffer)
903 ;; output -- things gdb prints to emacs
905 ;; GDB output is a stream interrupted by annotations.
906 ;; Annotations can be recognized by their beginning
907 ;; with \C-j\C-z\C-z<tag><opt>\C-j
909 ;; The tag is a string obeying symbol syntax.
911 ;; The optional part `<opt>' can be either the empty string
912 ;; or a space followed by more data relating to the annotation.
913 ;; For example, the SOURCE annotation is followed by a filename,
914 ;; line number and various useless goo. This data must not include
918 (defcustom gud-gdba-command-name "gdb -annotate=3"
919 "Default command to execute an executable under the GDB-UI debugger."
924 (defvar gdb-annotation-rules
925 '(("pre-prompt" gdb-pre-prompt)
926 ("prompt" gdb-prompt)
927 ("commands" gdb-subprompt)
928 ("overload-choice" gdb-subprompt)
929 ("query" gdb-subprompt)
930 ;; Need this prompt for GDB 6.1
931 ("nquery" gdb-subprompt)
932 ("prompt-for-continue" gdb-subprompt)
933 ("post-prompt" gdb-post-prompt)
934 ("source" gdb-source)
935 ("starting" gdb-starting)
936 ("exited" gdb-exited)
937 ("signalled" gdb-exited)
938 ("signal" gdb-stopping)
939 ("breakpoint" gdb-stopping)
940 ("watchpoint" gdb-stopping)
941 ("frame-begin" gdb-frame-begin)
942 ("stopped" gdb-stopped)
943 ("error-begin" gdb-error)
945 ) "An assoc mapping annotation tags to functions which process them.")
948 (setq gdb-flush-pending-output t)
949 (setq gud-running nil)
950 (setq gdb-output-sink 'user)
951 (setq gdb-input-queue nil)
952 (setq gdb-pending-triggers nil)
953 (setq gdb-prompting t))
955 (defconst gdb-source-spec-regexp
956 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
958 ;; Do not use this except as an annotation handler.
959 (defun gdb-source (args)
960 (string-match gdb-source-spec-regexp args)
961 ;; Extract the frame position from the marker.
964 (match-string 1 args)
965 (string-to-number (match-string 2 args))))
966 (setq gdb-frame-address (match-string 3 args))
967 ;; cover for auto-display output which comes *before*
968 ;; stopped annotation
969 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
971 (defun gdb-pre-prompt (ignored)
972 "An annotation handler for `pre-prompt'.
973 This terminates the collection of output from a previous command if that
974 happens to be in effect."
975 (let ((sink gdb-output-sink))
979 (setq gdb-output-sink 'post-emacs))
982 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
984 (defun gdb-prompt (ignored)
985 "An annotation handler for `prompt'.
986 This sends the next command (if any) to gdb."
987 (when gdb-first-prompt (gdb-ann3))
988 (let ((sink gdb-output-sink))
991 ((eq sink 'post-emacs)
992 (setq gdb-output-sink 'user)
994 (car (cdr gdb-current-item))))
995 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
999 (error "Phase error in gdb-prompt (got %s)" sink))))
1000 (let ((input (gdb-dequeue-input)))
1002 (gdb-send-item input)
1004 (setq gdb-prompting t)
1005 (gud-display-frame)))))
1007 (defun gdb-subprompt (ignored)
1008 "An annotation handler for non-top-level prompts."
1009 (setq gdb-prompting t))
1011 (defun gdb-starting (ignored)
1012 "An annotation handler for `starting'.
1013 This says that I/O for the subprocess is now the program being debugged,
1015 (setq gdb-active-process t)
1016 (let ((sink gdb-output-sink))
1020 (setq gud-running t)
1021 (if gdb-use-inferior-io-buffer
1022 (setq gdb-output-sink 'inferior))))
1025 (error "Unexpected `starting' annotation")))))
1027 (defun gdb-stopping (ignored)
1028 "An annotation handler for `breakpoint' and other annotations.
1029 They say that I/O for the subprocess is now GDB, not the program
1031 (if gdb-use-inferior-io-buffer
1032 (let ((sink gdb-output-sink))
1034 ((eq sink 'inferior)
1035 (setq gdb-output-sink 'user))
1038 (error "Unexpected stopping annotation"))))))
1040 (defun gdb-exited (ignored)
1041 "An annotation handler for `exited' and `signalled'.
1042 They say that I/O for the subprocess is now GDB, not the program
1043 being debugged and that the program is no longer running. This
1044 function is used to change the focus of GUD tooltips to #define
1046 (setq gdb-active-process nil)
1047 (setq gud-overlay-arrow-position nil)
1048 (setq gdb-overlay-arrow-position nil)
1049 (gdb-stopping ignored))
1051 (defun gdb-frame-begin (ignored)
1052 (let ((sink gdb-output-sink))
1054 ((eq sink 'inferior)
1055 (setq gdb-output-sink 'user))
1057 ((eq sink 'emacs) t)
1060 (error "Unexpected frame-begin annotation (%S)" sink)))))
1062 (defun gdb-stopped (ignored)
1063 "An annotation handler for `stopped'.
1064 It is just like `gdb-stopping', except that if we already set the output
1065 sink to `user' in `gdb-stopping', that is fine."
1066 (setq gud-running nil)
1067 (let ((sink gdb-output-sink))
1069 ((eq sink 'inferior)
1070 (setq gdb-output-sink 'user))
1074 (error "Unexpected stopped annotation")))))
1076 (defun gdb-error (ignored)
1077 (setq gdb-error (not gdb-error)))
1079 (defun gdb-post-prompt (ignored)
1080 "An annotation handler for `post-prompt'.
1081 This begins the collection of output from the current command if that
1082 happens to be appropriate."
1083 (unless gdb-pending-triggers
1084 (gdb-get-selected-frame)
1085 (gdb-invalidate-frames)
1086 (gdb-invalidate-breakpoints)
1087 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1088 ;; so gdb-frame-address is updated.
1089 ;; (gdb-invalidate-assembler)
1090 (gdb-invalidate-registers)
1091 (gdb-invalidate-memory)
1092 (gdb-invalidate-locals)
1093 (gdb-invalidate-threads)
1094 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1095 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1096 ;; Only needed/used with speedbar/watch expressions.
1097 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1098 (setq gdb-var-changed t) ; force update
1099 (dolist (var gdb-var-list)
1100 (setcar (nthcdr 5 var) nil))
1102 (let ((sink gdb-output-sink))
1105 ((eq sink 'pre-emacs)
1106 (setq gdb-output-sink 'emacs))
1109 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1111 (defun gud-gdba-marker-filter (string)
1112 "A gud marker filter for gdb. Handle a burst of output from GDB."
1113 (if gdb-flush-pending-output
1115 (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
1116 ;; Recall the left over gud-marker-acc from last time.
1117 (setq gud-marker-acc (concat gud-marker-acc string))
1118 ;; Start accumulating output for the GUD buffer.
1121 ;; Process all the complete markers in this chunk.
1122 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1123 (let ((annotation (match-string 1 gud-marker-acc)))
1125 ;; Stuff prior to the match is just ordinary output.
1126 ;; It is either concatenated to OUTPUT or directed
1131 (substring gud-marker-acc 0 (match-beginning 0))))
1133 ;; Take that stuff off the gud-marker-acc.
1134 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1136 ;; Parse the tag from the annotation, and maybe its arguments.
1137 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1138 (let* ((annotation-type (match-string 1 annotation))
1139 (annotation-arguments (match-string 2 annotation))
1140 (annotation-rule (assoc annotation-type
1141 gdb-annotation-rules)))
1142 ;; Call the handler for this annotation.
1144 (funcall (car (cdr annotation-rule))
1145 annotation-arguments)
1146 ;; Else the annotation is not recognized. Ignore it silently,
1147 ;; so that GDB can add new annotations without causing
1151 ;; Does the remaining text end in a partial line?
1152 ;; If it does, then keep part of the gud-marker-acc until we get more.
1153 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1156 ;; Everything before the potential marker start can be output.
1158 (gdb-concat-output output
1159 (substring gud-marker-acc 0
1160 (match-beginning 0))))
1162 ;; Everything after, we save, to combine with later input.
1163 (setq gud-marker-acc (substring gud-marker-acc
1164 (match-beginning 0))))
1166 ;; In case we know the gud-marker-acc contains no partial annotations:
1168 (setq output (gdb-concat-output output gud-marker-acc))
1169 (setq gud-marker-acc "")))
1172 (defun gdb-concat-output (so-far new)
1174 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1175 (let ((sink gdb-output-sink))
1177 ((eq sink 'user) (concat so-far new))
1178 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1180 (gdb-append-to-partial-output new)
1182 ((eq sink 'inferior)
1183 (gdb-append-to-inferior-io new)
1187 (error "Bogon output sink %S" sink)))))
1189 (defun gdb-append-to-partial-output (string)
1190 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1191 (goto-char (point-max))
1194 (defun gdb-clear-partial-output ()
1195 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1198 (defun gdb-append-to-inferior-io (string)
1199 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1200 (goto-char (point-max))
1201 (insert-before-markers string))
1202 (if (not (string-equal string ""))
1203 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io))))
1205 (defun gdb-clear-inferior-io ()
1206 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1210 ;; One trick is to have a command who's output is always available in a buffer
1211 ;; of it's own, and is always up to date. We build several buffers of this
1214 ;; There are two aspects to this: gdb has to tell us when the output for that
1215 ;; command might have changed, and we have to be able to run the command
1216 ;; behind the user's back.
1218 ;; The output phasing associated with the variable gdb-output-sink
1219 ;; help us to run commands behind the user's back.
1221 ;; Below is the code for specificly managing buffers of output from one
1225 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1226 ;; It adds an input for the command we are tracking. It should be the
1227 ;; annotation rule binding of whatever gdb sends to tell us this command
1228 ;; might have changed it's output.
1230 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1231 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1232 ;; input in the input queue (see comment about ``gdb communications'' above).
1234 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1236 `(defun ,name (&optional ignored)
1237 (if (and ,demand-predicate
1239 gdb-pending-triggers)))
1242 (list ,gdb-command ',output-handler))
1243 (push ',name gdb-pending-triggers)))))
1245 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1247 (setq gdb-pending-triggers
1249 gdb-pending-triggers))
1250 (let ((buf (gdb-get-buffer ',buf-key)))
1252 (with-current-buffer buf
1253 (let* ((window (get-buffer-window buf 0))
1254 (p (window-point window))
1255 (buffer-read-only nil))
1257 (insert-buffer-substring (gdb-get-create-buffer
1258 'gdb-partial-output-buffer))
1259 (set-window-point window p)))))
1260 ;; put customisation here
1263 (defmacro def-gdb-auto-updated-buffer (buffer-key
1264 trigger-name gdb-command
1265 output-handler-name custom-defun)
1267 (def-gdb-auto-update-trigger ,trigger-name
1268 ;; The demand predicate:
1269 (gdb-get-buffer ',buffer-key)
1271 ,output-handler-name)
1272 (def-gdb-auto-update-handler ,output-handler-name
1273 ,trigger-name ,buffer-key ,custom-defun)))
1277 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1279 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1280 'gdb-breakpoints-buffer-name
1281 'gdb-breakpoints-mode)
1283 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1284 ;; This defines the auto update rule for buffers of type
1285 ;; `gdb-breakpoints-buffer'.
1287 ;; It defines a function to serve as the annotation handler that
1288 ;; handles the `foo-invalidated' message. That function is called:
1289 gdb-invalidate-breakpoints
1291 ;; To update the buffer, this command is sent to gdb.
1292 "server info breakpoints\n"
1294 ;; This also defines a function to be the handler for the output
1295 ;; from the command above. That function will copy the output into
1296 ;; the appropriately typed buffer. That function will be called:
1297 gdb-info-breakpoints-handler
1298 ;; buffer specific functions
1299 gdb-info-breakpoints-custom)
1301 (defconst breakpoint-xpm-data
1303 static char *magick[] = {
1304 /* columns rows colors chars-per-pixel */
1320 "XPM data used for breakpoint icon.")
1322 (defconst breakpoint-enabled-pbm-data
1325 0 0 0 0 1 1 1 1 0 0 0 0
1326 0 0 0 1 1 1 1 1 1 0 0 0
1327 0 0 1 1 1 1 1 1 1 1 0 0
1328 0 1 1 1 1 1 1 1 1 1 1 0
1329 0 1 1 1 1 1 1 1 1 1 1 0
1330 0 1 1 1 1 1 1 1 1 1 1 0
1331 0 1 1 1 1 1 1 1 1 1 1 0
1332 0 0 1 1 1 1 1 1 1 1 0 0
1333 0 0 0 1 1 1 1 1 1 0 0 0
1334 0 0 0 0 1 1 1 1 0 0 0 0"
1335 "PBM data used for enabled breakpoint icon.")
1337 (defconst breakpoint-disabled-pbm-data
1349 0 0 0 1 0 1 0 1 0 0"
1350 "PBM data used for disabled breakpoint icon.")
1352 (defvar breakpoint-enabled-icon nil
1353 "Icon for enabled breakpoint in display margin.")
1355 (defvar breakpoint-disabled-icon nil
1356 "Icon for disabled breakpoint in display margin.")
1358 ;; Bitmap for breakpoint in fringe
1359 (and (display-images-p)
1360 (define-fringe-bitmap 'breakpoint
1361 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1363 (defface breakpoint-enabled
1367 "Face for enabled breakpoint icon in fringe."
1369 ;; Compatibility alias for old name.
1370 (put 'breakpoint-enabled-bitmap-face 'face-alias 'breakpoint-enabled)
1372 (defface breakpoint-disabled
1373 ;; We use different values of grey for different background types,
1374 ;; so that on low-color displays it will end up as something visible
1375 ;; if it has to be approximated.
1376 '((((background dark)) :foreground "grey60")
1377 (((background light)) :foreground "grey40"))
1378 "Face for disabled breakpoint icon in fringe."
1381 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1382 (defun gdb-info-breakpoints-custom ()
1383 (let ((flag) (bptno))
1384 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1385 (dolist (buffer (buffer-list))
1386 (with-current-buffer buffer
1387 (if (and (eq gud-minor-mode 'gdba)
1388 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1389 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1390 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1392 (goto-char (point-min))
1393 (while (< (point) (- (point-max) 1))
1395 (if (looking-at "[^\t].*?breakpoint")
1397 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1398 (setq bptno (match-string 1))
1399 (setq flag (char-after (match-beginning 2)))
1401 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1403 (let ((buffer-read-only nil))
1404 (add-text-properties (match-beginning 1) (match-end 1)
1405 '(face font-lock-function-name-face)))
1406 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1407 (let ((line (match-string 2)) (buffer-read-only nil)
1408 (file (match-string 1)))
1409 (add-text-properties (line-beginning-position)
1411 '(mouse-face highlight
1412 help-echo "mouse-2, RET: visit breakpoint"))
1413 (unless (file-exists-p file)
1414 (setq file (cdr (assoc bptno gdb-location-alist))))
1416 (not (string-equal file "File not found")))
1417 (with-current-buffer
1418 (find-file-noselect file 'nowarn)
1419 (set (make-local-variable 'gud-minor-mode)
1421 (set (make-local-variable 'tool-bar-map)
1423 ;; Only want one breakpoint icon at each
1426 (goto-line (string-to-number line))
1427 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1431 (match-string-no-properties 1) ":1\n")
1434 (list "info source\n"
1435 `(lambda () (gdb-get-location
1436 ,bptno ,line ,flag))))))))))
1438 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1440 (defun gdb-mouse-set-clear-breakpoint (event)
1441 "Set/clear breakpoint in left fringe/margin with mouse click."
1443 (mouse-minibuffer-check event)
1444 (let ((posn (event-end event)))
1445 (if (numberp (posn-point posn))
1446 (with-selected-window (posn-window posn)
1448 (goto-char (posn-point posn))
1449 (if (or (posn-object posn)
1450 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1453 (gud-break nil)))))))
1455 (defun gdb-mouse-toggle-breakpoint-margin (event)
1456 "Enable/disable breakpoint in left margin with mouse click."
1458 (mouse-minibuffer-check event)
1459 (let ((posn (event-end event)))
1460 (if (numberp (posn-point posn))
1461 (with-selected-window (posn-window posn)
1463 (goto-char (posn-point posn))
1464 (if (posn-object posn)
1467 (let ((bptno (get-text-property
1468 0 'gdb-bptno (car (posn-string posn)))))
1470 (if (get-text-property
1471 0 'gdb-enabled (car (posn-string posn)))
1477 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1478 "Enable/disable breakpoint in left fringe with mouse click."
1480 (mouse-minibuffer-check event)
1481 (let* ((posn (event-end event))
1482 (pos (posn-point posn))
1485 (with-selected-window (posn-window posn)
1487 (set-buffer (window-buffer (selected-window)))
1489 (dolist (overlay (overlays-in pos pos))
1490 (when (overlay-get overlay 'put-break)
1491 (setq obj (overlay-get overlay 'before-string))))
1496 (if (get-text-property 0 'gdb-enabled obj)
1499 (get-text-property 0 'gdb-bptno obj) "\n")
1502 (defun gdb-breakpoints-buffer-name ()
1503 (with-current-buffer gud-comint-buffer
1504 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1506 (defun gdb-display-breakpoints-buffer ()
1507 "Display status of user-settable breakpoints."
1510 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1512 (defun gdb-frame-breakpoints-buffer ()
1513 "Display status of user-settable breakpoints in a new frame."
1515 (let ((special-display-regexps (append special-display-regexps '(".*")))
1516 (special-display-frame-alist gdb-frame-parameters))
1517 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
1519 (defvar gdb-breakpoints-mode-map
1520 (let ((map (make-sparse-keymap))
1521 (menu (make-sparse-keymap "Breakpoints")))
1522 (define-key menu [quit] '("Quit" . kill-this-buffer))
1523 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1524 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1525 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1526 (suppress-keymap map)
1527 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1528 (define-key map " " 'gdb-toggle-breakpoint)
1529 (define-key map "d" 'gdb-delete-breakpoint)
1530 (define-key map "q" 'kill-this-buffer)
1531 (define-key map "\r" 'gdb-goto-breakpoint)
1532 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1533 (define-key map [follow-link] 'mouse-face)
1536 (defun gdb-breakpoints-mode ()
1537 "Major mode for gdb breakpoints.
1539 \\{gdb-breakpoints-mode-map}"
1540 (kill-all-local-variables)
1541 (setq major-mode 'gdb-breakpoints-mode)
1542 (setq mode-name "Breakpoints")
1543 (use-local-map gdb-breakpoints-mode-map)
1544 (setq buffer-read-only t)
1545 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1546 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1547 'gdb-invalidate-breakpoints
1548 'gdbmi-invalidate-breakpoints))
1550 (defun gdb-toggle-breakpoint ()
1551 "Enable/disable breakpoint at current line."
1554 (beginning-of-line 1)
1555 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1556 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1558 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1561 (concat gdb-server-prefix
1562 (if (eq ?y (char-after (match-beginning 2)))
1565 (match-string 1) "\n") 'ignore))
1566 (error "Not recognized as break/watchpoint line"))))
1568 (defun gdb-delete-breakpoint ()
1569 "Delete the breakpoint at current line."
1571 (beginning-of-line 1)
1572 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1573 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)")
1575 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1578 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1579 (error "Not recognized as break/watchpoint line")))
1581 (defun gdb-goto-breakpoint (&optional event)
1582 "Display the breakpoint location specified at current line."
1583 (interactive (list last-input-event))
1584 (if event (mouse-set-point event))
1586 (beginning-of-line 1)
1587 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1588 (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1590 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+.\\s-+\\S-+\\s-+\
1591 \\(\\S-+\\):\\([0-9]+\\)"))
1592 (let ((bptno (match-string 1))
1593 (file (match-string 2))
1594 (line (match-string 3)))
1595 (save-selected-window
1596 (let* ((buf (find-file-noselect
1597 (if (file-exists-p file) file
1598 (cdr (assoc bptno gdb-location-alist)))))
1599 (window (display-buffer buf)))
1600 (with-current-buffer buf
1601 (goto-line (string-to-number line))
1602 (set-window-point window (point))))))
1603 (error "Not recognized as break/watchpoint line"))))
1606 ;; Frames buffer. This displays a perpetually correct bactracktrace
1607 ;; (from the command `where').
1609 ;; Alas, if your stack is deep, it is costly.
1611 (gdb-set-buffer-rules 'gdb-stack-buffer
1612 'gdb-stack-buffer-name
1615 (def-gdb-auto-updated-buffer gdb-stack-buffer
1616 gdb-invalidate-frames
1618 gdb-info-frames-handler
1619 gdb-info-frames-custom)
1621 (defun gdb-info-frames-custom ()
1622 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1624 (let ((buffer-read-only nil)
1626 (goto-char (point-min))
1627 (while (< (point) (point-max))
1628 (setq bl (line-beginning-position)
1629 el (line-end-position))
1630 (unless (looking-at "No ")
1631 (add-text-properties bl el
1632 '(mouse-face highlight
1633 help-echo "mouse-2, RET: Select frame")))
1635 (when (looking-at "^#\\([0-9]+\\)")
1636 (when (string-equal (match-string 1) gdb-frame-number)
1637 (put-text-property bl (+ bl 4)
1638 'face '(:inverse-video t)))
1639 (when (re-search-forward
1641 (if (string-equal (match-string 1) "0") "" " in ")
1642 "\\([^ ]+\\) (") el t)
1643 (put-text-property (match-beginning 1) (match-end 1)
1644 'face font-lock-function-name-face)
1645 (setq bl (match-end 0))
1646 (while (re-search-forward "<\\([^>]+\\)>" el t)
1647 (put-text-property (match-beginning 1) (match-end 1)
1648 'face font-lock-function-name-face))
1650 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1651 (put-text-property (match-beginning 1) (match-end 1)
1652 'face font-lock-variable-name-face))))
1653 (forward-line 1))))))
1655 (defun gdb-stack-buffer-name ()
1656 (with-current-buffer gud-comint-buffer
1657 (concat "*stack frames of " (gdb-get-target-string) "*")))
1659 (defun gdb-display-stack-buffer ()
1660 "Display backtrace of current stack."
1663 (gdb-get-create-buffer 'gdb-stack-buffer)))
1665 (defun gdb-frame-stack-buffer ()
1666 "Display backtrace of current stack in a new frame."
1668 (let ((special-display-regexps (append special-display-regexps '(".*")))
1669 (special-display-frame-alist gdb-frame-parameters))
1670 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1672 (defvar gdb-frames-mode-map
1673 (let ((map (make-sparse-keymap)))
1674 (suppress-keymap map)
1675 (define-key map "q" 'kill-this-buffer)
1676 (define-key map "\r" 'gdb-frames-select)
1677 (define-key map [mouse-2] 'gdb-frames-select)
1678 (define-key map [follow-link] 'mouse-face)
1681 (defun gdb-frames-mode ()
1682 "Major mode for gdb frames.
1684 \\{gdb-frames-mode-map}"
1685 (kill-all-local-variables)
1686 (setq major-mode 'gdb-frames-mode)
1687 (setq mode-name "Frames")
1688 (setq buffer-read-only t)
1689 (use-local-map gdb-frames-mode-map)
1691 (run-mode-hooks 'gdb-frames-mode-hook)
1692 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1693 'gdb-invalidate-frames
1694 'gdbmi-invalidate-frames))
1696 (defun gdb-get-frame-number ()
1699 (let* ((pos (re-search-backward "^#*\\([0-9]*\\)" nil t))
1700 (n (or (and pos (match-string-no-properties 1)) "0")))
1703 (defun gdb-frames-select (&optional event)
1704 "Select the frame and display the relevant source."
1705 (interactive (list last-input-event))
1706 (if event (mouse-set-point event))
1708 (list (concat gdb-server-prefix "frame "
1709 (gdb-get-frame-number) "\n") 'ignore))
1710 (gud-display-frame))
1713 ;; Threads buffer. This displays a selectable thread list.
1715 (gdb-set-buffer-rules 'gdb-threads-buffer
1716 'gdb-threads-buffer-name
1719 (def-gdb-auto-updated-buffer gdb-threads-buffer
1720 gdb-invalidate-threads
1721 (concat gdb-server-prefix "info threads\n")
1722 gdb-info-threads-handler
1723 gdb-info-threads-custom)
1725 (defun gdb-info-threads-custom ()
1726 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1727 (let ((buffer-read-only nil))
1728 (goto-char (point-min))
1729 (while (< (point) (point-max))
1730 (unless (looking-at "No ")
1731 (add-text-properties (line-beginning-position) (line-end-position)
1732 '(mouse-face highlight
1733 help-echo "mouse-2, RET: select thread")))
1734 (forward-line 1)))))
1736 (defun gdb-threads-buffer-name ()
1737 (with-current-buffer gud-comint-buffer
1738 (concat "*threads of " (gdb-get-target-string) "*")))
1740 (defun gdb-display-threads-buffer ()
1741 "Display IDs of currently known threads."
1744 (gdb-get-create-buffer 'gdb-threads-buffer)))
1746 (defun gdb-frame-threads-buffer ()
1747 "Display IDs of currently known threads in a new frame."
1749 (let ((special-display-regexps (append special-display-regexps '(".*")))
1750 (special-display-frame-alist gdb-frame-parameters))
1751 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1753 (defvar gdb-threads-mode-map
1754 (let ((map (make-sparse-keymap)))
1755 (suppress-keymap map)
1756 (define-key map "q" 'kill-this-buffer)
1757 (define-key map "\r" 'gdb-threads-select)
1758 (define-key map [mouse-2] 'gdb-threads-select)
1761 (defvar gdb-threads-font-lock-keywords
1763 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
1764 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1765 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
1767 "Font lock keywords used in `gdb-threads-mode'.")
1769 (defun gdb-threads-mode ()
1770 "Major mode for gdb frames.
1772 \\{gdb-threads-mode-map}"
1773 (kill-all-local-variables)
1774 (setq major-mode 'gdb-threads-mode)
1775 (setq mode-name "Threads")
1776 (setq buffer-read-only t)
1777 (use-local-map gdb-threads-mode-map)
1778 (set (make-local-variable 'font-lock-defaults)
1779 '(gdb-threads-font-lock-keywords))
1780 (run-mode-hooks 'gdb-threads-mode-hook)
1781 'gdb-invalidate-threads)
1783 (defun gdb-get-thread-number ()
1785 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1786 (match-string-no-properties 1)))
1788 (defun gdb-threads-select (&optional event)
1789 "Select the thread and display the relevant source."
1790 (interactive (list last-input-event))
1791 (if event (mouse-set-point event))
1793 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1794 (gud-display-frame))
1797 ;; Registers buffer.
1799 (defcustom gdb-all-registers nil
1800 "Non-nil means include floating-point registers."
1805 (gdb-set-buffer-rules 'gdb-registers-buffer
1806 'gdb-registers-buffer-name
1807 'gdb-registers-mode)
1809 (def-gdb-auto-updated-buffer gdb-registers-buffer
1810 gdb-invalidate-registers
1812 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
1813 gdb-info-registers-handler
1814 gdb-info-registers-custom)
1816 (defun gdb-info-registers-custom ()
1817 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1819 (let ((buffer-read-only nil)
1821 (goto-char (point-min))
1822 (while (< (point) (point-max))
1823 (setq bl (line-beginning-position))
1824 (when (looking-at "^[^ ]+")
1825 (unless (string-equal (match-string 0) "The")
1826 (put-text-property bl (match-end 0)
1827 'face font-lock-variable-name-face)))
1828 (forward-line 1))))))
1830 (defvar gdb-registers-mode-map
1831 (let ((map (make-sparse-keymap)))
1832 (suppress-keymap map)
1833 (define-key map " " 'toggle-gdb-all-registers)
1834 (define-key map "q" 'kill-this-buffer)
1837 (defun gdb-registers-mode ()
1838 "Major mode for gdb registers.
1840 \\{gdb-registers-mode-map}"
1841 (kill-all-local-variables)
1842 (setq major-mode 'gdb-registers-mode)
1843 (setq mode-name "Registers:")
1844 (setq buffer-read-only t)
1845 (use-local-map gdb-registers-mode-map)
1846 (run-mode-hooks 'gdb-registers-mode-hook)
1847 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1848 'gdb-invalidate-registers
1849 'gdbmi-invalidate-registers))
1851 (defun gdb-registers-buffer-name ()
1852 (with-current-buffer gud-comint-buffer
1853 (concat "*registers of " (gdb-get-target-string) "*")))
1855 (defun gdb-display-registers-buffer ()
1856 "Display integer register contents."
1859 (gdb-get-create-buffer 'gdb-registers-buffer)))
1861 (defun gdb-frame-registers-buffer ()
1862 "Display integer register contents in a new frame."
1864 (let ((special-display-regexps (append special-display-regexps '(".*")))
1865 (special-display-frame-alist gdb-frame-parameters))
1866 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
1868 (defun toggle-gdb-all-registers ()
1869 "Toggle the display of floating-point registers."
1871 (if gdb-all-registers
1873 (setq gdb-all-registers nil)
1874 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1875 (setq mode-name "Registers:")))
1876 (setq gdb-all-registers t)
1877 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1878 (setq mode-name "Registers:All")))
1879 (gdb-invalidate-registers))
1884 (defcustom gdb-memory-repeat-count 32
1885 "Number of data items in memory window."
1890 (defcustom gdb-memory-format "x"
1891 "Display format of data items in memory window."
1892 :type '(choice (const :tag "Hexadecimal" "x")
1893 (const :tag "Signed decimal" "d")
1894 (const :tag "Unsigned decimal" "u")
1895 (const :tag "Octal" "o")
1896 (const :tag "Binary" "t"))
1900 (defcustom gdb-memory-unit "w"
1901 "Unit size of data items in memory window."
1902 :type '(choice (const :tag "Byte" "b")
1903 (const :tag "Halfword" "h")
1904 (const :tag "Word" "w")
1905 (const :tag "Giant word" "g"))
1909 (gdb-set-buffer-rules 'gdb-memory-buffer
1910 'gdb-memory-buffer-name
1913 (def-gdb-auto-updated-buffer gdb-memory-buffer
1914 gdb-invalidate-memory
1915 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
1916 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
1917 gdb-read-memory-handler
1918 gdb-read-memory-custom)
1920 (defun gdb-read-memory-custom ()
1922 (goto-char (point-min))
1923 (if (looking-at "0x[[:xdigit:]]+")
1924 (setq gdb-memory-address (match-string 0)))))
1926 (defvar gdb-memory-mode-map
1927 (let ((map (make-sparse-keymap)))
1928 (suppress-keymap map)
1929 (define-key map "q" 'kill-this-buffer)
1932 (defun gdb-memory-set-address (event)
1933 "Set the start memory address."
1935 (save-selected-window
1936 (select-window (posn-window (event-start event)))
1937 (let ((arg (read-from-minibuffer "Memory address: ")))
1938 (setq gdb-memory-address arg))
1939 (gdb-invalidate-memory)))
1941 (defun gdb-memory-set-repeat-count (event)
1942 "Set the number of data items in memory window."
1944 (save-selected-window
1945 (select-window (posn-window (event-start event)))
1946 (let* ((arg (read-from-minibuffer "Repeat count: "))
1947 (count (string-to-number arg)))
1949 (error "Positive numbers only")
1950 (customize-set-variable 'gdb-memory-repeat-count count)
1951 (gdb-invalidate-memory)))))
1953 (defun gdb-memory-format-binary ()
1954 "Set the display format to binary."
1956 (customize-set-variable 'gdb-memory-format "t")
1957 (gdb-invalidate-memory))
1959 (defun gdb-memory-format-octal ()
1960 "Set the display format to octal."
1962 (customize-set-variable 'gdb-memory-format "o")
1963 (gdb-invalidate-memory))
1965 (defun gdb-memory-format-unsigned ()
1966 "Set the display format to unsigned decimal."
1968 (customize-set-variable 'gdb-memory-format "u")
1969 (gdb-invalidate-memory))
1971 (defun gdb-memory-format-signed ()
1972 "Set the display format to decimal."
1974 (customize-set-variable 'gdb-memory-format "d")
1975 (gdb-invalidate-memory))
1977 (defun gdb-memory-format-hexadecimal ()
1978 "Set the display format to hexadecimal."
1980 (customize-set-variable 'gdb-memory-format "x")
1981 (gdb-invalidate-memory))
1983 (defvar gdb-memory-format-keymap
1984 (let ((map (make-sparse-keymap)))
1985 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
1987 "Keymap to select format in the header line.")
1989 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
1990 "Menu of display formats in the header line.")
1992 (define-key gdb-memory-format-menu [binary]
1993 '(menu-item "Binary" gdb-memory-format-binary
1994 :button (:radio . (equal gdb-memory-format "t"))))
1995 (define-key gdb-memory-format-menu [octal]
1996 '(menu-item "Octal" gdb-memory-format-octal
1997 :button (:radio . (equal gdb-memory-format "o"))))
1998 (define-key gdb-memory-format-menu [unsigned]
1999 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2000 :button (:radio . (equal gdb-memory-format "u"))))
2001 (define-key gdb-memory-format-menu [signed]
2002 '(menu-item "Signed Decimal" gdb-memory-format-signed
2003 :button (:radio . (equal gdb-memory-format "d"))))
2004 (define-key gdb-memory-format-menu [hexadecimal]
2005 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2006 :button (:radio . (equal gdb-memory-format "x"))))
2008 (defun gdb-memory-format-menu (event)
2010 (x-popup-menu event gdb-memory-format-menu))
2012 (defun gdb-memory-format-menu-1 (event)
2014 (save-selected-window
2015 (select-window (posn-window (event-start event)))
2016 (let* ((selection (gdb-memory-format-menu event))
2017 (binding (and selection (lookup-key gdb-memory-format-menu
2018 (vector (car selection))))))
2019 (if binding (call-interactively binding)))))
2021 (defun gdb-memory-unit-giant ()
2022 "Set the unit size to giant words (eight bytes)."
2024 (customize-set-variable 'gdb-memory-unit "g")
2025 (gdb-invalidate-memory))
2027 (defun gdb-memory-unit-word ()
2028 "Set the unit size to words (four bytes)."
2030 (customize-set-variable 'gdb-memory-unit "w")
2031 (gdb-invalidate-memory))
2033 (defun gdb-memory-unit-halfword ()
2034 "Set the unit size to halfwords (two bytes)."
2036 (customize-set-variable 'gdb-memory-unit "h")
2037 (gdb-invalidate-memory))
2039 (defun gdb-memory-unit-byte ()
2040 "Set the unit size to bytes."
2042 (customize-set-variable 'gdb-memory-unit "b")
2043 (gdb-invalidate-memory))
2045 (defvar gdb-memory-unit-keymap
2046 (let ((map (make-sparse-keymap)))
2047 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2049 "Keymap to select units in the header line.")
2051 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2052 "Menu of units in the header line.")
2054 (define-key gdb-memory-unit-menu [giantwords]
2055 '(menu-item "Giant words" gdb-memory-unit-giant
2056 :button (:radio . (equal gdb-memory-unit "g"))))
2057 (define-key gdb-memory-unit-menu [words]
2058 '(menu-item "Words" gdb-memory-unit-word
2059 :button (:radio . (equal gdb-memory-unit "w"))))
2060 (define-key gdb-memory-unit-menu [halfwords]
2061 '(menu-item "Halfwords" gdb-memory-unit-halfword
2062 :button (:radio . (equal gdb-memory-unit "h"))))
2063 (define-key gdb-memory-unit-menu [bytes]
2064 '(menu-item "Bytes" gdb-memory-unit-byte
2065 :button (:radio . (equal gdb-memory-unit "b"))))
2067 (defun gdb-memory-unit-menu (event)
2069 (x-popup-menu event gdb-memory-unit-menu))
2071 (defun gdb-memory-unit-menu-1 (event)
2073 (save-selected-window
2074 (select-window (posn-window (event-start event)))
2075 (let* ((selection (gdb-memory-unit-menu event))
2076 (binding (and selection (lookup-key gdb-memory-unit-menu
2077 (vector (car selection))))))
2078 (if binding (call-interactively binding)))))
2080 ;;from make-mode-line-mouse-map
2081 (defun gdb-make-header-line-mouse-map (mouse function) "\
2082 Return a keymap with single entry for mouse key MOUSE on the header line.
2083 MOUSE is defined to run function FUNCTION with no args in the buffer
2084 corresponding to the mode line clicked."
2085 (let ((map (make-sparse-keymap)))
2086 (define-key map (vector 'header-line mouse) function)
2087 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2090 (defvar gdb-memory-font-lock-keywords
2091 '(;; <__function.name+n>
2092 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2094 "Font lock keywords used in `gdb-memory-mode'.")
2096 (defun gdb-memory-mode ()
2097 "Major mode for examining memory.
2099 \\{gdb-memory-mode-map}"
2100 (kill-all-local-variables)
2101 (setq major-mode 'gdb-memory-mode)
2102 (setq mode-name "Memory")
2103 (setq buffer-read-only t)
2104 (use-local-map gdb-memory-mode-map)
2105 (setq header-line-format
2111 'face font-lock-warning-face
2112 'help-echo "mouse-1: Decrement address"
2113 'mouse-face 'mode-line-highlight
2115 (gdb-make-header-line-mouse-map
2117 #'(lambda () (interactive)
2118 (let ((gdb-memory-address
2119 ;; Let GDB do the arithmetic.
2121 gdb-memory-address " - "
2123 (* gdb-memory-repeat-count
2124 (cond ((string= gdb-memory-unit "b") 1)
2125 ((string= gdb-memory-unit "h") 2)
2126 ((string= gdb-memory-unit "w") 4)
2127 ((string= gdb-memory-unit "g") 8)))))))
2128 (gdb-invalidate-memory)))))
2131 'face font-lock-warning-face
2132 'help-echo "mouse-1: Increment address"
2133 'mouse-face 'mode-line-highlight
2134 'local-map (gdb-make-header-line-mouse-map
2136 #'(lambda () (interactive)
2137 (let ((gdb-memory-address nil))
2138 (gdb-invalidate-memory)))))
2140 (propertize gdb-memory-address
2141 'face font-lock-warning-face
2142 'help-echo "mouse-1: Set memory address"
2143 'mouse-face 'mode-line-highlight
2144 'local-map (gdb-make-header-line-mouse-map
2146 #'gdb-memory-set-address))
2148 (propertize (number-to-string gdb-memory-repeat-count)
2149 'face font-lock-warning-face
2150 'help-echo "mouse-1: Set repeat count"
2151 'mouse-face 'mode-line-highlight
2152 'local-map (gdb-make-header-line-mouse-map
2154 #'gdb-memory-set-repeat-count))
2156 (propertize gdb-memory-format
2157 'face font-lock-warning-face
2158 'help-echo "mouse-3: Select display format"
2159 'mouse-face 'mode-line-highlight
2160 'local-map gdb-memory-format-keymap)
2162 (propertize gdb-memory-unit
2163 'face font-lock-warning-face
2164 'help-echo "mouse-3: Select unit size"
2165 'mouse-face 'mode-line-highlight
2166 'local-map gdb-memory-unit-keymap))))
2167 (set (make-local-variable 'font-lock-defaults)
2168 '(gdb-memory-font-lock-keywords))
2169 (run-mode-hooks 'gdb-memory-mode-hook)
2170 'gdb-invalidate-memory)
2172 (defun gdb-memory-buffer-name ()
2173 (with-current-buffer gud-comint-buffer
2174 (concat "*memory of " (gdb-get-target-string) "*")))
2176 (defun gdb-display-memory-buffer ()
2177 "Display memory contents."
2180 (gdb-get-create-buffer 'gdb-memory-buffer)))
2182 (defun gdb-frame-memory-buffer ()
2183 "Display memory contents in a new frame."
2185 (let ((special-display-regexps (append special-display-regexps '(".*")))
2186 (special-display-frame-alist gdb-frame-parameters))
2187 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
2192 (gdb-set-buffer-rules 'gdb-locals-buffer
2193 'gdb-locals-buffer-name
2196 (def-gdb-auto-update-trigger gdb-invalidate-locals
2197 (gdb-get-buffer 'gdb-locals-buffer)
2198 "server info locals\n"
2199 gdb-info-locals-handler)
2201 ;; Abbreviate for arrays and structures.
2202 ;; These can be expanded using gud-display.
2203 (defun gdb-info-locals-handler ()
2204 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2205 gdb-pending-triggers))
2206 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2207 (with-current-buffer buf
2208 (goto-char (point-min))
2209 (while (re-search-forward "^[ }].*\n" nil t)
2210 (replace-match "" nil nil))
2211 (goto-char (point-min))
2212 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2213 (replace-match "(structure);\n" nil nil))
2214 (goto-char (point-min))
2215 (while (re-search-forward "\\s-*{.*\n" nil t)
2216 (replace-match " (array);\n" nil nil))))
2217 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2219 (with-current-buffer buf
2220 (let* ((window (get-buffer-window buf 0))
2221 (p (window-point window))
2222 (buffer-read-only nil))
2224 (insert-buffer-substring (gdb-get-create-buffer
2225 'gdb-partial-output-buffer))
2226 (set-window-point window p)))))
2227 (run-hooks 'gdb-info-locals-hook))
2229 (defvar gdb-locals-mode-map
2230 (let ((map (make-sparse-keymap)))
2231 (suppress-keymap map)
2232 (define-key map "q" 'kill-this-buffer)
2235 (defvar gdb-locals-font-lock-keywords
2237 ;; var = (struct struct_tag) value
2238 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
2239 (1 font-lock-variable-name-face)
2240 (3 font-lock-keyword-face)
2241 (4 font-lock-type-face))
2242 ;; var = (type) value
2243 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
2244 (1 font-lock-variable-name-face)
2245 (3 font-lock-type-face))
2247 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
2248 (1 font-lock-variable-name-face))
2250 "Font lock keywords used in `gdb-local-mode'.")
2252 (defun gdb-locals-mode ()
2253 "Major mode for gdb locals.
2255 \\{gdb-locals-mode-map}"
2256 (kill-all-local-variables)
2257 (setq major-mode 'gdb-locals-mode)
2258 (setq mode-name (concat "Locals:" gdb-selected-frame))
2259 (setq buffer-read-only t)
2260 (use-local-map gdb-locals-mode-map)
2261 (set (make-local-variable 'font-lock-defaults)
2262 '(gdb-locals-font-lock-keywords))
2263 (run-mode-hooks 'gdb-locals-mode-hook)
2264 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
2265 'gdb-invalidate-locals
2266 'gdbmi-invalidate-locals))
2268 (defun gdb-locals-buffer-name ()
2269 (with-current-buffer gud-comint-buffer
2270 (concat "*locals of " (gdb-get-target-string) "*")))
2272 (defun gdb-display-locals-buffer ()
2273 "Display local variables of current stack and their values."
2276 (gdb-get-create-buffer 'gdb-locals-buffer)))
2278 (defun gdb-frame-locals-buffer ()
2279 "Display local variables of current stack and their values in a new frame."
2281 (let ((special-display-regexps (append special-display-regexps '(".*")))
2282 (special-display-frame-alist gdb-frame-parameters))
2283 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
2286 ;;;; Window management
2287 (defun gdb-display-buffer (buf &optional size)
2288 (let ((answer (get-buffer-window buf 0))
2291 (display-buffer buf nil 0) ;Raise the frame if necessary.
2292 ;; The buffer is not yet displayed.
2293 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2294 (let ((window (get-lru-window)))
2296 (not (eq window (get-buffer-window gud-comint-buffer))))
2298 (set-window-buffer window buf)
2299 (setq answer window))
2300 (setq must-split t)))
2302 (let* ((largest (get-largest-window))
2303 (cur-size (window-height largest))
2304 (new-size (and size (< size cur-size) (- cur-size size))))
2305 (setq answer (split-window largest new-size))
2306 (set-window-buffer answer buf)
2307 (set-window-dedicated-p answer t)))
2311 ;;; Shared keymap initialization:
2313 (let ((menu (make-sparse-keymap "GDB-Windows")))
2314 (define-key gud-menu-map [displays]
2315 `(menu-item "GDB-Windows" ,menu
2316 :visible (memq gud-minor-mode '(gdbmi gdba))))
2317 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2318 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2319 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2320 (define-key menu [disassembly]
2321 '("Disassembly" . gdb-display-assembler-buffer))
2322 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2323 (define-key menu [inferior]
2324 '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
2325 :enable gdb-use-inferior-io-buffer))
2326 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2327 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2328 (define-key menu [breakpoints]
2329 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2331 (let ((menu (make-sparse-keymap "GDB-Frames")))
2332 (define-key gud-menu-map [frames]
2333 `(menu-item "GDB-Frames" ,menu
2334 :visible (memq gud-minor-mode '(gdbmi gdba))))
2335 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2336 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2337 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2338 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2339 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2340 (define-key menu [inferior]
2341 '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
2342 :enable gdb-use-inferior-io-buffer))
2343 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2344 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2345 (define-key menu [breakpoints]
2346 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2348 (let ((menu (make-sparse-keymap "GDB-UI")))
2349 (define-key gud-menu-map [ui]
2350 `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
2351 (define-key menu [gdb-use-inferior-io]
2352 '(menu-item "Separate inferior IO" gdb-use-inferior-io-buffer
2353 :help "Toggle separate IO for inferior."
2354 :button (:toggle . gdb-use-inferior-io-buffer)))
2355 (define-key menu [gdb-many-windows]
2356 '(menu-item "Display Other Windows" gdb-many-windows
2357 :help "Toggle display of locals, stack and breakpoint information"
2358 :button (:toggle . gdb-many-windows)))
2359 (define-key menu [gdb-restore-windows]
2360 '(menu-item "Restore Window Layout" gdb-restore-windows
2361 :help "Restore standard layout for debug session.")))
2363 (defun gdb-frame-gdb-buffer ()
2364 "Display GUD buffer in a new frame."
2366 (select-frame (make-frame gdb-frame-parameters))
2367 (switch-to-buffer (gdb-get-create-buffer 'gdba))
2368 (set-window-dedicated-p (selected-window) t))
2370 (defun gdb-display-gdb-buffer ()
2371 "Display GUD buffer."
2374 (gdb-get-create-buffer 'gdba)))
2376 (defun gdb-set-window-buffer (name)
2377 (set-window-buffer (selected-window) (get-buffer name))
2378 (set-window-dedicated-p (selected-window) t))
2380 (defun gdb-setup-windows ()
2381 "Layout the window pattern for `gdb-many-windows'."
2382 (gdb-display-locals-buffer)
2383 (gdb-display-stack-buffer)
2384 (delete-other-windows)
2385 (gdb-display-breakpoints-buffer)
2386 (delete-other-windows)
2388 (pop-to-buffer gud-comint-buffer)
2389 (split-window nil ( / ( * (window-height) 3) 4))
2390 (split-window nil ( / (window-height) 3))
2391 (split-window-horizontally)
2393 (gdb-set-window-buffer (gdb-locals-buffer-name))
2396 (if gud-last-last-frame
2397 (gud-find-file (car gud-last-last-frame))
2398 (gud-find-file gdb-main-file)))
2399 (when gdb-use-inferior-io-buffer
2400 (split-window-horizontally)
2402 (gdb-set-window-buffer
2403 (gdb-get-create-buffer 'gdb-inferior-io)))
2405 (gdb-set-window-buffer (gdb-stack-buffer-name))
2406 (split-window-horizontally)
2408 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2411 (defcustom gdb-many-windows nil
2412 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2413 In this case it starts with two windows: one displaying the GUD
2414 buffer and the other with the source file with the main routine
2415 of the inferior. Non-nil means display the layout shown for
2421 (defun gdb-many-windows (arg)
2422 "Toggle the number of windows in the basic arrangement.
2423 With arg, display additional buffers iff arg is positive."
2425 (setq gdb-many-windows
2427 (not gdb-many-windows)
2428 (> (prefix-numeric-value arg) 0)))
2429 (if (and gud-comint-buffer
2430 (buffer-name gud-comint-buffer))
2432 (gdb-restore-windows)
2435 (defun gdb-restore-windows ()
2436 "Restore the basic arrangement of windows used by gdba.
2437 This arrangement depends on the value of `gdb-many-windows'."
2439 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2440 (delete-other-windows)
2441 (if gdb-many-windows
2446 (if gud-last-last-frame
2447 (gud-find-file (car gud-last-last-frame))
2448 (gud-find-file gdb-main-file)))
2452 "Exit a debugging session cleanly.
2453 Kills the gdb buffers and resets the source buffers."
2454 (dolist (buffer (buffer-list))
2455 (unless (eq buffer gud-comint-buffer)
2456 (with-current-buffer buffer
2457 (if (memq gud-minor-mode '(gdbmi gdba))
2458 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2460 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2461 (setq gud-minor-mode nil)
2462 (kill-local-variable 'tool-bar-map)
2463 (kill-local-variable 'gdb-define-alist))))))
2464 (when (markerp gdb-overlay-arrow-position)
2465 (move-marker gdb-overlay-arrow-position nil)
2466 (setq gdb-overlay-arrow-position nil))
2467 (setq overlay-arrow-variable-list
2468 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2469 (setq gud-running nil)
2470 (setq gdb-active-process nil)
2471 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2473 (defun gdb-source-info ()
2474 "Find the source file where the program starts and displays it with related
2476 (goto-char (point-min))
2477 (if (and (search-forward "Located in " nil t)
2478 (looking-at "\\S-+"))
2479 (setq gdb-main-file (match-string 0)))
2480 (goto-char (point-min))
2481 (if (search-forward "Includes preprocessor macro info." nil t)
2482 (setq gdb-macro-info t))
2483 (if gdb-many-windows
2485 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2487 (let ((pop-up-windows t))
2488 (display-buffer (gud-find-file gdb-main-file))))))
2490 (defun gdb-get-location (bptno line flag)
2491 "Find the directory containing the relevant source file.
2492 Put in buffer and place breakpoint icon."
2493 (goto-char (point-min))
2494 (catch 'file-not-found
2495 (if (search-forward "Located in " nil t)
2496 (when (looking-at "\\S-+")
2497 (delete (cons bptno "File not found") gdb-location-alist)
2498 (push (cons bptno (match-string 0)) gdb-location-alist))
2500 (unless (assoc bptno gdb-location-alist)
2501 (push (cons bptno "File not found") gdb-location-alist)
2502 (message-box "Cannot find source file for breakpoint location.\n\
2503 Add directory to search path for source files using the GDB command, dir."))
2504 (throw 'file-not-found nil))
2505 (with-current-buffer
2506 (find-file-noselect (match-string 0))
2507 (save-current-buffer
2508 (set (make-local-variable 'gud-minor-mode) 'gdba)
2509 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2510 ;; only want one breakpoint icon at each location
2512 (goto-line (string-to-number line))
2513 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2515 (add-hook 'find-file-hook 'gdb-find-file-hook)
2517 (defun gdb-find-file-hook ()
2518 "Set up buffer for debugging if file is part of the source code
2519 of the current session."
2520 (if (and (not gdb-find-file-unhook)
2521 ;; in case gud or gdb-ui is just loaded
2523 (buffer-name gud-comint-buffer)
2524 (with-current-buffer gud-comint-buffer
2525 (eq gud-minor-mode 'gdba)))
2528 (list (concat gdb-server-prefix "list "
2529 (file-name-nondirectory buffer-file-name)
2531 `(lambda () (gdb-set-gud-minor-mode ,(current-buffer)))))
2532 (error (setq gdb-find-file-unhook t)))))
2535 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2536 "Put string PUTSTRING in front of POS in the current buffer.
2537 PUTSTRING is displayed by putting an overlay into the current buffer with a
2538 `before-string' string that has a `display' property whose value is
2540 (let ((string (make-string 1 ?x))
2541 (buffer (current-buffer)))
2542 (setq putstring (copy-sequence putstring))
2543 (let ((overlay (make-overlay pos pos buffer))
2545 (list (list 'margin 'left-margin) putstring))))
2546 (put-text-property 0 1 'display prop string)
2548 (add-text-properties 0 1 sprops string))
2549 (overlay-put overlay 'put-break t)
2550 (overlay-put overlay 'before-string string))))
2552 ;;from remove-images
2553 (defun gdb-remove-strings (start end &optional buffer)
2554 "Remove strings between START and END in BUFFER.
2555 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2556 BUFFER nil or omitted means use the current buffer."
2558 (setq buffer (current-buffer)))
2559 (dolist (overlay (overlays-in start end))
2560 (when (overlay-get overlay 'put-break)
2561 (delete-overlay overlay))))
2563 (defun gdb-put-breakpoint-icon (enabled bptno)
2564 (let ((start (- (line-beginning-position) 1))
2565 (end (+ (line-end-position) 1))
2566 (putstring (if enabled "B" "b"))
2567 (source-window (get-buffer-window (current-buffer) 0)))
2568 (add-text-properties
2569 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2572 (add-text-properties
2573 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2574 (add-text-properties
2575 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2576 (gdb-remove-breakpoint-icons start end)
2577 (if (display-images-p)
2578 (if (>= (or left-fringe-width
2579 (if source-window (car (window-fringes source-window)))
2580 gdb-buffer-fringe-width) 8)
2583 `(left-fringe breakpoint
2586 'breakpoint-disabled))
2588 'gdb-enabled enabled)
2589 (when (< left-margin-width 2)
2590 (save-current-buffer
2591 (setq left-margin-width 2)
2595 left-margin-width right-margin-width))))
2598 (or breakpoint-enabled-icon
2599 (setq breakpoint-enabled-icon
2600 (find-image `((:type xpm :data
2601 ,breakpoint-xpm-data
2602 :ascent 100 :pointer hand)
2604 ,breakpoint-enabled-pbm-data
2605 :ascent 100 :pointer hand)))))
2606 (or breakpoint-disabled-icon
2607 (setq breakpoint-disabled-icon
2608 (find-image `((:type xpm :data
2609 ,breakpoint-xpm-data
2610 :conversion disabled
2611 :ascent 100 :pointer hand)
2613 ,breakpoint-disabled-pbm-data
2614 :ascent 100 :pointer hand))))))
2618 (when (< left-margin-width 2)
2619 (save-current-buffer
2620 (setq left-margin-width 2)
2621 (let ((window (get-buffer-window (current-buffer) 0)))
2624 window left-margin-width right-margin-width)))))
2626 (propertize putstring
2627 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2630 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2631 (gdb-remove-strings start end)
2632 (if (display-images-p)
2633 (remove-images start end))
2635 (setq left-margin-width 0)
2636 (let ((window (get-buffer-window (current-buffer) 0)))
2639 window left-margin-width right-margin-width)))))
2643 ;; Assembler buffer.
2645 (gdb-set-buffer-rules 'gdb-assembler-buffer
2646 'gdb-assembler-buffer-name
2647 'gdb-assembler-mode)
2649 (def-gdb-auto-update-handler gdb-assembler-handler
2650 gdb-invalidate-assembler
2651 gdb-assembler-buffer
2652 gdb-assembler-custom)
2654 (defun gdb-assembler-custom ()
2655 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2656 (pos 1) (address) (flag) (bptno))
2657 (with-current-buffer buffer
2659 (if (not (equal gdb-frame-address "main"))
2661 (goto-char (point-min))
2662 (if (and gdb-frame-address
2663 (re-search-forward gdb-frame-address nil t))
2667 (or gdb-overlay-arrow-position
2668 (setq gdb-overlay-arrow-position (make-marker)))
2669 (set-marker gdb-overlay-arrow-position
2670 (point) (current-buffer))))))
2671 ;; remove all breakpoint-icons in assembler buffer before updating.
2672 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2673 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2674 (goto-char (point-min))
2675 (while (< (point) (- (point-max) 1))
2677 (if (looking-at "[^\t].*?breakpoint")
2680 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2681 (setq bptno (match-string 1))
2682 (setq flag (char-after (match-beginning 2)))
2683 (setq address (match-string 3))
2684 (with-current-buffer buffer
2686 (goto-char (point-min))
2687 (if (re-search-forward address nil t)
2688 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2689 (if (not (equal gdb-frame-address "main"))
2690 (set-window-point (get-buffer-window buffer 0) pos))))
2692 (defvar gdb-assembler-mode-map
2693 (let ((map (make-sparse-keymap)))
2694 (suppress-keymap map)
2695 (define-key map "q" 'kill-this-buffer)
2698 (defvar gdb-assembler-font-lock-keywords
2699 '(;; <__function.name+n>
2700 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2701 (1 font-lock-function-name-face))
2702 ;; 0xNNNNNNNN <__function.name+n>: opcode
2703 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2704 (4 font-lock-keyword-face))
2705 ;; %register(at least i386)
2706 ("%\\sw+" . font-lock-variable-name-face)
2707 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2708 (1 font-lock-comment-face)
2709 (2 font-lock-function-name-face))
2710 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2711 "Font lock keywords used in `gdb-assembler-mode'.")
2713 (defun gdb-assembler-mode ()
2714 "Major mode for viewing code assembler.
2716 \\{gdb-assembler-mode-map}"
2717 (kill-all-local-variables)
2718 (setq major-mode 'gdb-assembler-mode)
2719 (setq mode-name (concat "Machine:" gdb-selected-frame))
2720 (setq gdb-overlay-arrow-position nil)
2721 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2722 (setq fringes-outside-margins t)
2723 (setq buffer-read-only t)
2724 (use-local-map gdb-assembler-mode-map)
2725 (gdb-invalidate-assembler)
2726 (set (make-local-variable 'font-lock-defaults)
2727 '(gdb-assembler-font-lock-keywords))
2728 (run-mode-hooks 'gdb-assembler-mode-hook)
2729 'gdb-invalidate-assembler)
2731 (defun gdb-assembler-buffer-name ()
2732 (with-current-buffer gud-comint-buffer
2733 (concat "*Disassembly of " (gdb-get-target-string) "*")))
2735 (defun gdb-display-assembler-buffer ()
2736 "Display disassembly view."
2738 (setq gdb-previous-frame nil)
2740 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2742 (defun gdb-frame-assembler-buffer ()
2743 "Display disassembly view in a new frame."
2745 (setq gdb-previous-frame nil)
2746 (let ((special-display-regexps (append special-display-regexps '(".*")))
2747 (special-display-frame-alist gdb-frame-parameters))
2748 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2750 ;; modified because if gdb-frame-address has changed value a new command
2751 ;; must be enqueued to update the buffer with the new output
2752 (defun gdb-invalidate-assembler (&optional ignored)
2753 (if (gdb-get-buffer 'gdb-assembler-buffer)
2755 (unless (and gdb-selected-frame
2756 (string-equal gdb-selected-frame gdb-previous-frame))
2757 (if (or (not (member 'gdb-invalidate-assembler
2758 gdb-pending-triggers))
2759 (not (string-equal gdb-frame-address
2760 gdb-previous-frame-address)))
2762 ;; take previous disassemble command, if any, off the queue
2763 (with-current-buffer gud-comint-buffer
2764 (let ((queue gdb-input-queue))
2765 (dolist (item queue)
2766 (if (equal (cdr item) '(gdb-assembler-handler))
2767 (setq gdb-input-queue
2768 (delete item gdb-input-queue))))))
2771 (concat gdb-server-prefix "disassemble "
2772 (if (member gdb-frame-address '(nil "main")) nil "0x")
2773 gdb-frame-address "\n")
2774 'gdb-assembler-handler))
2775 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2776 (setq gdb-previous-frame-address gdb-frame-address)
2777 (setq gdb-previous-frame gdb-selected-frame)))))))
2779 (defun gdb-get-selected-frame ()
2780 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2783 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2784 (push 'gdb-get-selected-frame
2785 gdb-pending-triggers))))
2787 (defun gdb-frame-handler ()
2788 (setq gdb-pending-triggers
2789 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2790 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2791 (goto-char (point-min))
2792 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
2793 (setq gdb-frame-number (match-string 1)))
2794 (goto-char (point-min))
2795 (if (re-search-forward
2796 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
2798 (setq gdb-selected-frame (match-string 2))
2799 (if (gdb-get-buffer 'gdb-locals-buffer)
2800 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2801 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2802 (if (gdb-get-buffer 'gdb-assembler-buffer)
2803 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
2804 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2805 (setq gdb-frame-address (match-string 1))))
2806 (goto-char (point-min))
2807 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2808 (setq gdb-current-language (match-string 1))))
2809 (gdb-invalidate-assembler))
2813 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2814 ;;; gdb-ui.el ends here