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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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.
56 ;; 1) Use MI command -data-read-memory for memory window.
57 ;; 2) Highlight changed register values (use MI commands
58 ;; -data-list-register-values and -data-list-changed-registers instead
59 ;; of 'info registers'.
60 ;; 3) Use tree-widget.el instead of the speedbar for watch-expressions?
61 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
62 ;; 5) After release of 22.1 use '-var-list-children --all-values'
63 ;; and '-stack-list-locals 2' which need GDB 6.1 onwards.
69 (defvar gdb-current-address "main" "Initialisation for Assembler buffer.")
70 (defvar gdb-previous-address nil)
71 (defvar gdb-memory-address "main")
72 (defvar gdb-previous-frame nil)
73 (defvar gdb-current-frame nil)
74 (defvar gdb-current-stack-level nil)
75 (defvar gdb-current-language nil)
76 (defvar gdb-var-list nil "List of variables in watch window.")
77 (defvar gdb-var-changed nil "Non-nil means that gdb-var-list has changed.")
78 (defvar gdb-buffer-type nil)
79 (defvar gdb-overlay-arrow-position nil)
80 (defvar gdb-server-prefix nil)
81 (defvar gdb-flush-pending-output nil)
82 (defvar gdb-location-list nil "Alist of breakpoint numbers and full filenames.")
83 (defvar gdb-find-file-unhook nil)
85 (defvar gdb-buffer-type nil
86 "One of the symbols bound in `gdb-buffer-rules'.")
88 (defvar gdb-input-queue ()
89 "A list of gdb command objects.")
91 (defvar gdb-prompting nil
92 "True when gdb is idle with no pending input.")
94 (defvar gdb-output-sink 'user
95 "The disposition of the output of the current gdb command.
96 Possible values are these symbols:
98 `user' -- gdb output should be copied to the GUD buffer
101 `inferior' -- gdb output should be copied to the inferior-io buffer
103 `pre-emacs' -- output should be ignored util the post-prompt
104 annotation is received. Then the output-sink
106 `emacs' -- output should be collected in the partial-output-buffer
107 for subsequent processing by a command. This is the
108 disposition of output generated by commands that
109 gdb mode sends to gdb on its own behalf.
110 `post-emacs' -- ignore output until the prompt annotation is
111 received, then go to USER disposition.
113 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
114 \(`user' and `emacs').")
116 (defvar gdb-current-item nil
117 "The most recent command item sent to gdb.")
119 (defvar gdb-pending-triggers '()
120 "A list of trigger functions that have run later than their output
123 ;; end of gdb variables
126 (defun gdba (command-line)
127 "Run gdb on program FILE in buffer *gud-FILE*.
128 The directory containing FILE becomes the initial working directory
129 and source-file directory for your debugger.
131 If `gdb-many-windows' is nil (the default value) then gdb just
132 pops up the GUD buffer unless `gdb-show-main' is t. In this case
133 it starts with two windows: one displaying the GUD buffer and the
134 other with the source file with the main routine of the inferior.
136 If `gdb-many-windows' is t, regardless of the value of
137 `gdb-show-main', the layout below will appear unless
138 `gdb-use-inferior-io-buffer' is nil when the source buffer
139 occupies the full width of the frame. Keybindings are given in
142 Watch expressions appear in the speedbar/slowbar.
144 The following commands help control operation :
146 `gdb-many-windows' - Toggle the number of windows gdb uses.
147 `gdb-restore-windows' - To restore the window layout.
149 See Info node `(emacs)GDB Graphical Interface' for a more
150 detailed description of this mode.
153 ---------------------------------------------------------------------
155 ---------------------------------------------------------------------
156 GUD buffer (I/O of GDB) | Locals buffer
160 ---------------------------------------------------------------------
161 Source buffer | Input/Output (of inferior) buffer
169 ---------------------------------------------------------------------
170 Stack buffer | Breakpoints buffer
171 RET gdb-frames-select | SPC gdb-toggle-breakpoint
172 | RET gdb-goto-breakpoint
173 | d gdb-delete-breakpoint
174 ---------------------------------------------------------------------"
176 (interactive (list (gud-query-cmdline 'gdba)))
178 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
182 (defvar gdb-debug-log nil)
184 (defcustom gdb-enable-debug-log nil
185 "Non-nil means record the process input and output in `gdb-debug-log'."
190 (defcustom gdb-use-inferior-io-buffer nil
191 "Non-nil means display output from the inferior in a separate buffer."
196 (defun gdb-set-gud-minor-mode (buffer)
197 "Set gud-minor-mode from find-file if appropriate."
198 (goto-char (point-min))
199 (unless (search-forward "No source file named " nil t)
202 (list (concat gdb-server-prefix "info source\n")
203 `(lambda () (gdb-set-gud-minor-mode-1 ,buffer))))
204 (error (setq gdb-find-file-unhook t)))))
206 (defun gdb-set-gud-minor-mode-1 (buffer)
207 (goto-char (point-min))
208 (if (and (search-forward "Located in " nil t)
210 (string-equal (buffer-file-name buffer)
212 (with-current-buffer buffer
213 (set (make-local-variable 'gud-minor-mode) 'gdba)
214 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))))
216 (defun gdb-set-gud-minor-mode-existing-buffers ()
217 (dolist (buffer (buffer-list))
218 (let ((file (buffer-file-name buffer)))
222 (list (concat gdb-server-prefix "list "
223 (file-name-nondirectory file) ":1\n")
224 `(lambda () (gdb-set-gud-minor-mode ,buffer)))))))))
227 (setq gdb-debug-log nil)
228 (set (make-local-variable 'gud-minor-mode) 'gdba)
229 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
231 (gud-def gud-break (if (not (string-equal mode-name "Machine"))
232 (gud-call "break %f:%l" arg)
236 (gud-call "break *%a" arg)))
237 "\C-b" "Set breakpoint at current line or address.")
239 (gud-def gud-remove (if (not (string-equal mode-name "Machine"))
240 (gud-call "clear %f:%l" arg)
244 (gud-call "clear *%a" arg)))
245 "\C-d" "Remove breakpoint at current line or address.")
247 (gud-def gud-until (if (not (string-equal mode-name "Machine"))
248 (gud-call "until %f:%l" arg)
252 (gud-call "until *%a" arg)))
253 "\C-u" "Continue to current line or address.")
255 (define-key gud-minor-mode-map [left-margin mouse-1]
256 'gdb-mouse-set-clear-breakpoint)
257 (define-key gud-minor-mode-map [left-fringe mouse-1]
258 'gdb-mouse-set-clear-breakpoint)
259 (define-key gud-minor-mode-map [left-margin mouse-3]
260 'gdb-mouse-toggle-breakpoint)
261 ; Currently only works in margin.
262 ; (define-key gud-minor-mode-map [left-fringe mouse-3]
263 ; 'gdb-mouse-toggle-breakpoint)
265 (setq comint-input-sender 'gdb-send)
268 (setq gdb-current-address "main")
269 (setq gdb-previous-address nil)
270 (setq gdb-memory-address "main")
271 (setq gdb-previous-frame nil)
272 (setq gdb-current-frame nil)
273 (setq gdb-current-stack-level nil)
274 (setq gdb-var-list nil)
275 (setq gdb-var-changed nil)
276 (setq gdb-first-prompt nil)
277 (setq gdb-prompting nil)
278 (setq gdb-input-queue nil)
279 (setq gdb-current-item nil)
280 (setq gdb-pending-triggers nil)
281 (setq gdb-output-sink 'user)
282 (setq gdb-server-prefix "server ")
283 (setq gdb-flush-pending-output nil)
284 (setq gdb-location-list nil)
285 (setq gdb-find-file-unhook nil)
287 (setq gdb-buffer-type 'gdba)
289 (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
291 (if (eq window-system 'w32)
292 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
293 (gdb-enqueue-input (list "set height 0\n" 'ignore))
294 (gdb-enqueue-input (list "set width 0\n" 'ignore))
295 ;; find source file and compilation directory here
296 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
297 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
298 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
300 (gdb-set-gud-minor-mode-existing-buffers)
301 (run-hooks 'gdba-mode-hook))
303 (defcustom gdb-use-colon-colon-notation nil
304 "If non-nil use FUN::VAR format to display variables in the speedbar." ;
310 "Watch expression at point."
313 (let ((expr (tooltip-identifier-from-point (point))))
314 (if (and (string-equal gdb-current-language "c")
315 gdb-use-colon-colon-notation gdb-current-frame)
316 (setq expr (concat gdb-current-frame "::" expr)))
317 (catch 'already-watched
318 (dolist (var gdb-var-list)
319 (if (string-equal expr (car var)) (throw 'already-watched nil)))
320 (set-text-properties 0 (length expr) nil expr)
323 (if (eq gud-minor-mode 'gdba)
324 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
325 (concat"-var-create - * " expr "\n"))
326 `(lambda () (gdb-var-create-handler ,expr))))))
327 (select-window (get-buffer-window gud-comint-buffer 0)))
329 (defconst gdb-var-create-regexp
330 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
332 (defun gdb-var-create-handler (expr)
333 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
334 (goto-char (point-min))
335 (if (re-search-forward gdb-var-create-regexp nil t)
336 (let ((var (list expr
341 (push var gdb-var-list)
343 (if (equal (nth 2 var) "0")
346 (if (with-current-buffer
347 gud-comint-buffer (eq gud-minor-mode 'gdba))
348 (concat "server interpreter mi \"-var-evaluate-expression "
350 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
351 `(lambda () (gdb-var-evaluate-expression-handler
353 (setq gdb-var-changed t)))
354 (if (re-search-forward "Undefined command" nil t)
355 (message-box "Watching expressions requires gdb 6.0 onwards")
356 (message "No symbol %s in current context." expr)))))
358 (defun gdb-var-evaluate-expression-handler (varnum changed)
359 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
360 (goto-char (point-min))
361 (re-search-forward ".*value=\"\\(.*?\\)\"" nil t)
364 (dolist (var gdb-var-list)
365 (if (string-equal varnum (cadr var))
367 (if changed (setcar (nthcdr 5 var) t))
368 (setcar (nthcdr 4 var) (match-string 1))
369 (setcar (nthcdr num gdb-var-list) var)
370 (throw 'var-found nil)))
371 (setq num (+ num 1))))))
372 (setq gdb-var-changed t))
374 (defun gdb-var-list-children (varnum)
376 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
377 `(lambda () (gdb-var-list-children-handler ,varnum)))))
379 (defconst gdb-var-list-children-regexp
380 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\"")
382 (defun gdb-var-list-children-handler (varnum)
383 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
384 (goto-char (point-min))
385 (let ((var-list nil))
386 (catch 'child-already-watched
387 (dolist (var gdb-var-list)
388 (if (string-equal varnum (cadr var))
391 (while (re-search-forward gdb-var-list-children-regexp nil t)
392 (let ((varchild (list (match-string 2)
396 (if (looking-at ",type=\"\\(.*?\\)\"")
397 (setcar (nthcdr 3 varchild) (match-string 1)))
398 (dolist (var1 gdb-var-list)
399 (if (string-equal (cadr var1) (cadr varchild))
400 (throw 'child-already-watched nil)))
401 (push varchild var-list)
402 (if (equal (nth 2 varchild) "0")
406 "server interpreter mi \"-var-evaluate-expression "
407 (nth 1 varchild) "\"\n")
408 `(lambda () (gdb-var-evaluate-expression-handler
409 ,(nth 1 varchild) nil))))))))
410 (push var var-list)))
411 (setq gdb-var-list (nreverse var-list))))))
413 (defun gdb-var-update ()
414 (if (not (member 'gdb-var-update gdb-pending-triggers))
418 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
419 "server interpreter mi \"-var-update *\"\n"
421 'gdb-var-update-handler))
422 (push 'gdb-var-update gdb-pending-triggers))))
424 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
426 (defun gdb-var-update-handler ()
427 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
428 (goto-char (point-min))
429 (while (re-search-forward gdb-var-update-regexp nil t)
430 (let ((varnum (match-string 1)))
433 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
434 (concat "server interpreter mi \"-var-evaluate-expression "
436 (concat "-var-evaluate-expression " varnum "\n"))
437 `(lambda () (gdb-var-evaluate-expression-handler
439 (setq gdb-pending-triggers
440 (delq 'gdb-var-update gdb-pending-triggers))
441 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
442 ;; dummy command to update speedbar at right time
443 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
444 ;; keep gdb-pending-triggers non-nil till end
445 (push 'gdb-speedbar-timer gdb-pending-triggers)))
447 (defun gdb-speedbar-timer-fn ()
448 (setq gdb-pending-triggers
449 (delq 'gdb-speedbar-timer gdb-pending-triggers))
450 (with-current-buffer gud-comint-buffer
451 (speedbar-timer-fn)))
453 (defun gdb-var-delete ()
454 "Delete watched expression from the speedbar."
456 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
457 (let ((text (speedbar-line-text)))
458 (string-match "\\(\\S-+\\)" text)
459 (let* ((expr (match-string 1 text))
460 (var (assoc expr gdb-var-list))
462 (unless (string-match "\\." varnum)
465 (if (with-current-buffer gud-comint-buffer
466 (eq gud-minor-mode 'gdba))
467 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
468 (concat "-var-delete " varnum "\n"))
470 (setq gdb-var-list (delq var gdb-var-list))
471 (dolist (varchild gdb-var-list)
472 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
473 (setq gdb-var-list (delq varchild gdb-var-list))))
474 (setq gdb-var-changed t))))))
476 (defun gdb-edit-value (text token indent)
477 "Assign a value to a variable displayed in the speedbar."
478 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
479 (varnum (cadr var)) (value))
480 (setq value (read-string "New value: "))
483 (if (with-current-buffer gud-comint-buffer
484 (eq gud-minor-mode 'gdba))
485 (concat "server interpreter mi \"-var-assign " varnum " " value "\"\n")
486 (concat "-var-assign " varnum " " value "\n"))
489 (defcustom gdb-show-changed-values t
490 "If non-nil highlight values that have recently changed in the speedbar.
491 The highlighting is done with `font-lock-warning-face'."
496 (defun gdb-speedbar-expand-node (text token indent)
497 "Expand the node the user clicked on.
498 TEXT is the text of the button we clicked on, a + or - item.
499 TOKEN is data related to this node.
500 INDENT is the current indentation depth."
501 (cond ((string-match "+" text) ;expand this node
502 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
503 (gdb-var-list-children token)
504 (gdbmi-var-list-children token)))
505 ((string-match "-" text) ;contract this node
506 (dolist (var gdb-var-list)
507 (if (string-match (concat token "\\.") (nth 1 var))
508 (setq gdb-var-list (delq var gdb-var-list))))
509 (setq gdb-var-changed t))))
511 (defun gdb-get-target-string ()
512 (with-current-buffer gud-comint-buffer
519 ;; Each buffer has a TYPE -- a symbol that identifies the function
520 ;; of that particular buffer.
522 ;; The usual gdb interaction buffer is given the type `gdba' and
523 ;; is constructed specially.
525 ;; Others are constructed by gdb-get-create-buffer and
526 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
528 (defvar gdb-buffer-rules-assoc '())
530 (defun gdb-get-buffer (key)
531 "Return the gdb buffer tagged with type KEY.
532 The key should be one of the cars in `gdb-buffer-rules-assoc'."
534 (gdb-look-for-tagged-buffer key (buffer-list))))
536 (defun gdb-get-create-buffer (key)
537 "Create a new gdb buffer of the type specified by KEY.
538 The key should be one of the cars in `gdb-buffer-rules-assoc'."
539 (or (gdb-get-buffer key)
540 (let* ((rules (assoc key gdb-buffer-rules-assoc))
541 (name (funcall (gdb-rules-name-maker rules)))
542 (new (get-buffer-create name)))
543 (with-current-buffer new
545 (if (cdr (cdr rules))
546 (setq trigger (funcall (car (cdr (cdr rules))))))
547 (set (make-local-variable 'gdb-buffer-type) key)
548 (set (make-local-variable 'gud-minor-mode)
549 (with-current-buffer gud-comint-buffer gud-minor-mode))
550 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
551 (if trigger (funcall trigger)))
554 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
556 (defun gdb-look-for-tagged-buffer (key bufs)
558 (while (and (not retval) bufs)
559 (set-buffer (car bufs))
560 (if (eq gdb-buffer-type key)
561 (setq retval (car bufs)))
562 (setq bufs (cdr bufs)))
566 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
567 ;; at least one and possible more functions. The functions have these
568 ;; roles in defining a buffer type:
570 ;; NAME - Return a name for this buffer type.
572 ;; The remaining function(s) are optional:
574 ;; MODE - called in a new buffer with no arguments, should establish
575 ;; the proper mode for the buffer.
578 (defun gdb-set-buffer-rules (buffer-type &rest rules)
579 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
581 (setcdr binding rules)
582 (push (cons buffer-type rules)
583 gdb-buffer-rules-assoc))))
585 ;; GUD buffers are an exception to the rules
586 (gdb-set-buffer-rules 'gdba 'error)
589 ;; Partial-output buffer : This accumulates output from a command executed on
590 ;; behalf of emacs (rather than the user).
592 (gdb-set-buffer-rules 'gdb-partial-output-buffer
593 'gdb-partial-output-name)
595 (defun gdb-partial-output-name ()
596 (concat "*partial-output-"
597 (gdb-get-target-string)
601 (gdb-set-buffer-rules 'gdb-inferior-io
602 'gdb-inferior-io-name
603 'gdb-inferior-io-mode)
605 (defun gdb-inferior-io-name ()
606 (concat "*input/output of "
607 (gdb-get-target-string)
610 (defun gdb-display-inferior-io-buffer ()
611 "Display IO of inferior in a separate window."
613 (if gdb-use-inferior-io-buffer
615 (gdb-get-create-buffer 'gdb-inferior-io))))
617 (defun gdb-frame-inferior-io-buffer ()
618 "Display IO of inferior in a new frame."
620 (if gdb-use-inferior-io-buffer
621 (let ((special-display-regexps (append special-display-regexps '(".*")))
622 (special-display-frame-alist gdb-frame-parameters))
623 (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
625 (defvar gdb-inferior-io-mode-map
626 (let ((map (make-sparse-keymap)))
627 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
628 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
629 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
630 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
633 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
634 "Major mode for gdb inferior-io."
635 :syntax-table nil :abbrev-table nil
636 ;; We want to use comint because it has various nifty and familiar
637 ;; features. We don't need a process, but comint wants one, so create
639 (make-comint-in-buffer
640 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
641 (current-buffer) "hexl")
642 (setq comint-input-sender 'gdb-inferior-io-sender))
644 (defun gdb-inferior-io-sender (proc string)
645 ;; PROC is the pseudo-process created to satisfy comint.
646 (with-current-buffer (process-buffer proc)
647 (setq proc (get-buffer-process gud-comint-buffer))
648 (process-send-string proc string)
649 (process-send-string proc "\n")))
651 (defun gdb-inferior-io-interrupt ()
652 "Interrupt the program being debugged."
655 (get-buffer-process gud-comint-buffer) comint-ptyp))
657 (defun gdb-inferior-io-quit ()
658 "Send quit signal to the program being debugged."
661 (get-buffer-process gud-comint-buffer) comint-ptyp))
663 (defun gdb-inferior-io-stop ()
664 "Stop the program being debugged."
667 (get-buffer-process gud-comint-buffer) comint-ptyp))
669 (defun gdb-inferior-io-eof ()
670 "Send end-of-file to the program being debugged."
673 (get-buffer-process gud-comint-buffer)))
677 ;; gdb communications
680 ;; INPUT: things sent to gdb
682 ;; The queues are lists. Each element is either a string (indicating user or
683 ;; user-like input) or a list of the form:
685 ;; (INPUT-STRING HANDLER-FN)
687 ;; The handler function will be called from the partial-output buffer when the
688 ;; command completes. This is the way to write commands which invoke gdb
689 ;; commands autonomously.
691 ;; These lists are consumed tail first.
694 (defun gdb-send (proc string)
695 "A comint send filter for gdb.
696 This filter may simply queue input for a later time."
697 (let ((item (concat string "\n")))
700 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
701 (process-send-string proc item))
702 (gdb-enqueue-input item))))
704 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
705 ;; is a query, or other non-top-level prompt.
707 (defun gdb-enqueue-input (item)
711 (setq gdb-prompting nil))
712 (push item gdb-input-queue)))
714 (defun gdb-dequeue-input ()
715 (let ((queue gdb-input-queue))
717 (let ((last (car (last queue))))
718 (unless (nbutlast queue) (setq gdb-input-queue '()))
721 (defun gdb-send-item (item)
722 (setq gdb-flush-pending-output nil)
723 (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
724 (setq gdb-current-item item)
725 (with-current-buffer gud-comint-buffer
726 (if (eq gud-minor-mode 'gdba)
729 (setq gdb-output-sink 'user)
730 (process-send-string (get-buffer-process gud-comint-buffer) item))
732 (gdb-clear-partial-output)
733 (setq gdb-output-sink 'pre-emacs)
734 (process-send-string (get-buffer-process gud-comint-buffer)
736 ;; case: eq gud-minor-mode 'gdbmi
737 (gdb-clear-partial-output)
738 (setq gdb-output-sink 'emacs)
739 (process-send-string (get-buffer-process gud-comint-buffer)
743 ;; output -- things gdb prints to emacs
745 ;; GDB output is a stream interrupted by annotations.
746 ;; Annotations can be recognized by their beginning
747 ;; with \C-j\C-z\C-z<tag><opt>\C-j
749 ;; The tag is a string obeying symbol syntax.
751 ;; The optional part `<opt>' can be either the empty string
752 ;; or a space followed by more data relating to the annotation.
753 ;; For example, the SOURCE annotation is followed by a filename,
754 ;; line number and various useless goo. This data must not include
758 (defcustom gud-gdba-command-name "gdb -annotate=3"
759 "Default command to execute an executable under the GDB-UI debugger."
764 (defvar gdb-annotation-rules
765 '(("pre-prompt" gdb-pre-prompt)
766 ("prompt" gdb-prompt)
767 ("commands" gdb-subprompt)
768 ("overload-choice" gdb-subprompt)
769 ("query" gdb-subprompt)
770 ;; Need this prompt for GDB 6.1
771 ("nquery" gdb-subprompt)
772 ("prompt-for-continue" gdb-subprompt)
773 ("post-prompt" gdb-post-prompt)
774 ("source" gdb-source)
775 ("starting" gdb-starting)
776 ("exited" gdb-stopping)
777 ("signalled" gdb-stopping)
778 ("signal" gdb-stopping)
779 ("breakpoint" gdb-stopping)
780 ("watchpoint" gdb-stopping)
781 ("frame-begin" gdb-frame-begin)
782 ("stopped" gdb-stopped)
783 ) "An assoc mapping annotation tags to functions which process them.")
786 (setq gdb-flush-pending-output t)
787 (setq gud-running nil)
788 (setq gdb-output-sink 'user)
789 (setq gdb-input-queue nil)
790 (setq gdb-pending-triggers nil)
791 (setq gdb-prompting t))
793 (defconst gdb-source-spec-regexp
794 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:\\(0x[a-f0-9]*\\)")
796 ;; Do not use this except as an annotation handler.
797 (defun gdb-source (args)
798 (string-match gdb-source-spec-regexp args)
799 ;; Extract the frame position from the marker.
802 (match-string 1 args)
803 (string-to-int (match-string 2 args))))
804 (setq gdb-current-address (match-string 3 args))
805 ;; cover for auto-display output which comes *before*
806 ;; stopped annotation
807 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
809 (defun gdb-pre-prompt (ignored)
810 "An annotation handler for `pre-prompt'.
811 This terminates the collection of output from a previous command if that
812 happens to be in effect."
813 (let ((sink gdb-output-sink))
817 (setq gdb-output-sink 'post-emacs))
820 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
822 (defun gdb-prompt (ignored)
823 "An annotation handler for `prompt'.
824 This sends the next command (if any) to gdb."
825 (when gdb-first-prompt (gdb-ann3))
826 (let ((sink gdb-output-sink))
829 ((eq sink 'post-emacs)
830 (setq gdb-output-sink 'user)
832 (car (cdr gdb-current-item))))
833 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
837 (error "Phase error in gdb-prompt (got %s)" sink))))
838 (let ((input (gdb-dequeue-input)))
840 (gdb-send-item input)
842 (setq gdb-prompting t)
843 (gud-display-frame)))))
845 (defun gdb-subprompt (ignored)
846 "An annotation handler for non-top-level prompts."
847 (setq gdb-prompting t))
849 (defun gdb-starting (ignored)
850 "An annotation handler for `starting'.
851 This says that I/O for the subprocess is now the program being debugged,
853 (let ((sink gdb-output-sink))
858 (if gdb-use-inferior-io-buffer
859 (setq gdb-output-sink 'inferior))))
862 (error "Unexpected `starting' annotation")))))
864 (defun gdb-stopping (ignored)
865 "An annotation handler for `exited' and other annotations.
866 They say that I/O for the subprocess is now GDB, not the program
868 (if gdb-use-inferior-io-buffer
869 (let ((sink gdb-output-sink))
872 (setq gdb-output-sink 'user))
875 (error "Unexpected stopping annotation"))))))
877 (defun gdb-frame-begin (ignored)
878 (let ((sink gdb-output-sink))
881 (setq gdb-output-sink 'user))
886 (error "Unexpected frame-begin annotation (%S)" sink)))))
888 (defun gdb-stopped (ignored)
889 "An annotation handler for `stopped'.
890 It is just like `gdb-stopping', except that if we already set the output
891 sink to `user' in `gdb-stopping', that is fine."
892 (setq gud-running nil)
893 (let ((sink gdb-output-sink))
896 (setq gdb-output-sink 'user))
900 (error "Unexpected stopped annotation")))))
902 (defun gdb-post-prompt (ignored)
903 "An annotation handler for `post-prompt'.
904 This begins the collection of output from the current command if that
905 happens to be appropriate."
906 (unless gdb-pending-triggers
907 (gdb-get-current-frame)
908 (gdb-invalidate-frames)
909 (gdb-invalidate-breakpoints)
910 (gdb-invalidate-assembler)
911 (gdb-invalidate-registers)
912 (gdb-invalidate-memory)
913 (gdb-invalidate-locals)
914 (gdb-invalidate-threads)
915 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
916 ;; FIXME: with GDB-6 on Darwin, this might very well work.
917 ;; only needed/used with speedbar/watch expressions
918 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
919 (setq gdb-var-changed t) ; force update
920 (dolist (var gdb-var-list)
921 (setcar (nthcdr 5 var) nil))
923 (let ((sink gdb-output-sink))
926 ((eq sink 'pre-emacs)
927 (setq gdb-output-sink 'emacs))
930 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
932 (defun gud-gdba-marker-filter (string)
933 "A gud marker filter for gdb. Handle a burst of output from GDB."
934 (if gdb-flush-pending-output
936 (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
937 ;; Recall the left over gud-marker-acc from last time
938 (setq gud-marker-acc (concat gud-marker-acc string))
939 ;; Start accumulating output for the GUD buffer
942 ;; Process all the complete markers in this chunk.
943 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
944 (let ((annotation (match-string 1 gud-marker-acc)))
946 ;; Stuff prior to the match is just ordinary output.
947 ;; It is either concatenated to OUTPUT or directed
952 (substring gud-marker-acc 0 (match-beginning 0))))
954 ;; Take that stuff off the gud-marker-acc.
955 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
957 ;; Parse the tag from the annotation, and maybe its arguments.
958 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
959 (let* ((annotation-type (match-string 1 annotation))
960 (annotation-arguments (match-string 2 annotation))
961 (annotation-rule (assoc annotation-type
962 gdb-annotation-rules)))
963 ;; Call the handler for this annotation.
965 (funcall (car (cdr annotation-rule))
966 annotation-arguments)
967 ;; Else the annotation is not recognized. Ignore it silently,
968 ;; so that GDB can add new annotations without causing
972 ;; Does the remaining text end in a partial line?
973 ;; If it does, then keep part of the gud-marker-acc until we get more.
974 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
977 ;; Everything before the potential marker start can be output.
979 (gdb-concat-output output
980 (substring gud-marker-acc 0
981 (match-beginning 0))))
983 ;; Everything after, we save, to combine with later input.
984 (setq gud-marker-acc (substring gud-marker-acc (match-beginning 0))))
986 ;; In case we know the gud-marker-acc contains no partial annotations:
988 (setq output (gdb-concat-output output gud-marker-acc))
989 (setq gud-marker-acc "")))
992 (defun gdb-concat-output (so-far new)
993 (let ((sink gdb-output-sink))
995 ((eq sink 'user) (concat so-far new))
996 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
998 (gdb-append-to-partial-output new)
1000 ((eq sink 'inferior)
1001 (gdb-append-to-inferior-io new)
1005 (error "Bogon output sink %S" sink)))))
1007 (defun gdb-append-to-partial-output (string)
1008 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1009 (goto-char (point-max))
1012 (defun gdb-clear-partial-output ()
1013 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1016 (defun gdb-append-to-inferior-io (string)
1017 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1018 (goto-char (point-max))
1019 (insert-before-markers string))
1020 (if (not (string-equal string ""))
1021 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io))))
1023 (defun gdb-clear-inferior-io ()
1024 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1028 ;; One trick is to have a command who's output is always available in a buffer
1029 ;; of it's own, and is always up to date. We build several buffers of this
1032 ;; There are two aspects to this: gdb has to tell us when the output for that
1033 ;; command might have changed, and we have to be able to run the command
1034 ;; behind the user's back.
1036 ;; The output phasing associated with the variable gdb-output-sink
1037 ;; help us to run commands behind the user's back.
1039 ;; Below is the code for specificly managing buffers of output from one
1043 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1044 ;; It adds an input for the command we are tracking. It should be the
1045 ;; annotation rule binding of whatever gdb sends to tell us this command
1046 ;; might have changed it's output.
1048 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1049 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1050 ;; input in the input queue (see comment about ``gdb communications'' above).
1052 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1054 `(defun ,name (&optional ignored)
1055 (if (and (,demand-predicate)
1057 gdb-pending-triggers)))
1060 (list ,gdb-command ',output-handler))
1061 (push ',name gdb-pending-triggers)))))
1063 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1065 (setq gdb-pending-triggers
1067 gdb-pending-triggers))
1068 (let ((buf (gdb-get-buffer ',buf-key)))
1070 (with-current-buffer buf
1072 (buffer-read-only nil))
1074 (insert-buffer-substring (gdb-get-create-buffer
1075 'gdb-partial-output-buffer))
1077 ;; put customisation here
1080 (defmacro def-gdb-auto-updated-buffer (buffer-key trigger-name gdb-command
1081 output-handler-name custom-defun)
1083 (def-gdb-auto-update-trigger ,trigger-name
1084 ;; The demand predicate:
1085 (lambda () (gdb-get-buffer ',buffer-key))
1087 ,output-handler-name)
1088 (def-gdb-auto-update-handler ,output-handler-name
1089 ,trigger-name ,buffer-key ,custom-defun)))
1093 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1095 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1096 'gdb-breakpoints-buffer-name
1097 'gdb-breakpoints-mode)
1099 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1100 ;; This defines the auto update rule for buffers of type
1101 ;; `gdb-breakpoints-buffer'.
1103 ;; It defines a function to serve as the annotation handler that
1104 ;; handles the `foo-invalidated' message. That function is called:
1105 gdb-invalidate-breakpoints
1107 ;; To update the buffer, this command is sent to gdb.
1108 "server info breakpoints\n"
1110 ;; This also defines a function to be the handler for the output
1111 ;; from the command above. That function will copy the output into
1112 ;; the appropriately typed buffer. That function will be called:
1113 gdb-info-breakpoints-handler
1114 ;; buffer specific functions
1115 gdb-info-breakpoints-custom)
1117 (defconst breakpoint-xpm-data
1119 static char *magick[] = {
1120 /* columns rows colors chars-per-pixel */
1136 "XPM data used for breakpoint icon.")
1138 (defconst breakpoint-enabled-pbm-data
1141 0 0 0 0 1 1 1 1 0 0 0 0
1142 0 0 0 1 1 1 1 1 1 0 0 0
1143 0 0 1 1 1 1 1 1 1 1 0 0
1144 0 1 1 1 1 1 1 1 1 1 1 0
1145 0 1 1 1 1 1 1 1 1 1 1 0
1146 0 1 1 1 1 1 1 1 1 1 1 0
1147 0 1 1 1 1 1 1 1 1 1 1 0
1148 0 0 1 1 1 1 1 1 1 1 0 0
1149 0 0 0 1 1 1 1 1 1 0 0 0
1150 0 0 0 0 1 1 1 1 0 0 0 0"
1151 "PBM data used for enabled breakpoint icon.")
1153 (defconst breakpoint-disabled-pbm-data
1165 0 0 0 1 0 1 0 1 0 0"
1166 "PBM data used for disabled breakpoint icon.")
1168 (defvar breakpoint-enabled-icon nil
1169 "Icon for enabled breakpoint in display margin.")
1171 (defvar breakpoint-disabled-icon nil
1172 "Icon for disabled breakpoint in display margin.")
1174 ;; Bitmap for breakpoint in fringe
1175 (and (display-images-p)
1176 (define-fringe-bitmap 'breakpoint
1177 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1179 (defface breakpoint-enabled
1183 "Face for enabled breakpoint icon in fringe."
1185 ;; compatibility alias for old name
1186 (put 'breakpoint-enabled-bitmap-face 'face-alias 'breakpoint-enabled)
1188 (defface breakpoint-disabled
1189 ;; We use different values of grey for different background types,
1190 ;; so that on low-color displays it will end up as something visible
1191 ;; if it has to be approximated.
1192 '((((background dark)) :foreground "grey60")
1193 (((background light)) :foreground "grey40"))
1194 "Face for disabled breakpoint icon in fringe."
1196 ;; compatibility alias for old name
1197 (put 'breakpoint-disabled-bitmap-face 'face-alias 'breakpoint-disabled)
1199 ;;-put breakpoint icons in relevant margins (even those set in the GUD buffer)
1200 (defun gdb-info-breakpoints-custom ()
1201 (let ((flag) (bptno))
1202 ;; remove all breakpoint-icons in source buffers but not assembler buffer
1203 (dolist (buffer (buffer-list))
1204 (with-current-buffer buffer
1205 (if (and (eq gud-minor-mode 'gdba)
1206 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1207 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1208 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1210 (goto-char (point-min))
1211 (while (< (point) (- (point-max) 1))
1213 (if (looking-at "[^\t].*breakpoint")
1215 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1216 (setq bptno (match-string 1))
1217 (setq flag (char-after (match-beginning 2)))
1219 (if (re-search-forward " in .* at\\s-+" nil t)
1221 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1222 (let ((line (match-string 2)) (buffer-read-only nil)
1223 (file (match-string 1)))
1224 (add-text-properties (point-at-bol) (point-at-eol)
1225 '(mouse-face highlight
1226 help-echo "mouse-2, RET: visit breakpoint"))
1227 (unless (file-exists-p file)
1228 (setq file (cdr (assoc bptno gdb-location-list))))
1229 (unless (string-equal file "File not found")
1231 (with-current-buffer (find-file-noselect file)
1232 (set (make-local-variable 'gud-minor-mode)
1234 (set (make-local-variable 'tool-bar-map)
1236 ;; only want one breakpoint icon at each location
1238 (goto-line (string-to-number line))
1239 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1241 (list (concat "list "
1242 (match-string-no-properties 1) ":1\n")
1245 (list "info source\n"
1246 `(lambda () (gdb-get-location
1247 ,bptno ,line ,flag)))))))))))
1249 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1251 (defun gdb-mouse-set-clear-breakpoint (event)
1252 "Set/clear breakpoint in left fringe/margin with mouse click."
1254 (mouse-minibuffer-check event)
1255 (let ((posn (event-end event)))
1256 (if (numberp (posn-point posn))
1257 (with-selected-window (posn-window posn)
1259 (goto-char (posn-point posn))
1260 (if (or (posn-object posn)
1261 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1264 (gud-break nil)))))))
1266 (defun gdb-mouse-toggle-breakpoint (event)
1267 "Enable/disable breakpoint in left fringe/margin with mouse click."
1269 (mouse-minibuffer-check event)
1270 (let ((posn (event-end event)))
1271 (if (numberp (posn-point posn))
1272 (with-selected-window (posn-window posn)
1274 (goto-char (posn-point posn))
1278 ; (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1282 (let ((bptno (get-text-property
1283 0 'gdb-bptno (car (posn-string posn)))))
1285 (if (get-text-property
1286 0 'gdb-enabled (car (posn-string posn)))
1289 bptno "\n")) 'ignore))))))))
1291 (defun gdb-breakpoints-buffer-name ()
1292 (with-current-buffer gud-comint-buffer
1293 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1295 (defun gdb-display-breakpoints-buffer ()
1296 "Display status of user-settable breakpoints."
1299 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1301 (defconst gdb-frame-parameters
1302 '((height . 14) (width . 80)
1304 (tool-bar-lines . nil)
1305 (menu-bar-lines . nil)
1306 (minibuffer . nil)))
1308 (defun gdb-frame-breakpoints-buffer ()
1309 "Display status of user-settable breakpoints in a new frame."
1311 (let ((special-display-regexps (append special-display-regexps '(".*")))
1312 (special-display-frame-alist gdb-frame-parameters))
1313 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
1315 (defvar gdb-breakpoints-mode-map
1316 (let ((map (make-sparse-keymap))
1317 (menu (make-sparse-keymap "Breakpoints")))
1318 (define-key menu [quit] '("Quit" . kill-this-buffer))
1319 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1320 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1321 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1322 (suppress-keymap map)
1323 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1324 (define-key map " " 'gdb-toggle-breakpoint)
1325 (define-key map "d" 'gdb-delete-breakpoint)
1326 (define-key map "q" 'kill-this-buffer)
1327 (define-key map "\r" 'gdb-goto-breakpoint)
1328 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1329 (define-key map [follow-link] 'mouse-face)
1332 (defun gdb-breakpoints-mode ()
1333 "Major mode for gdb breakpoints.
1335 \\{gdb-breakpoints-mode-map}"
1336 (kill-all-local-variables)
1337 (setq major-mode 'gdb-breakpoints-mode)
1338 (setq mode-name "Breakpoints")
1339 (use-local-map gdb-breakpoints-mode-map)
1340 (setq buffer-read-only t)
1341 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1342 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1343 'gdb-invalidate-breakpoints
1344 'gdbmi-invalidate-breakpoints))
1346 (defun gdb-toggle-breakpoint ()
1347 "Enable/disable breakpoint at current line."
1350 (beginning-of-line 1)
1351 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1352 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1354 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1357 (concat gdb-server-prefix
1358 (if (eq ?y (char-after (match-beginning 2)))
1361 (match-string 1) "\n") 'ignore))
1362 (error "Not recognized as break/watchpoint line"))))
1364 (defun gdb-delete-breakpoint ()
1365 "Delete the breakpoint at current line."
1367 (beginning-of-line 1)
1368 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1369 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1371 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1374 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1375 (error "Not recognized as break/watchpoint line")))
1377 (defun gdb-goto-breakpoint (&optional event)
1378 "Display the breakpoint location specified at current line."
1379 (interactive (list last-input-event))
1380 (if event (mouse-set-point event))
1382 (beginning-of-line 1)
1383 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1384 (looking-at "\\([0-9]+\\) .* in .* at\\s-+\\(\\S-*\\):\\([0-9]+\\)")
1386 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\\(\\S-*\\):\\([0-9]+\\)"))
1387 (let ((bptno (match-string 1))
1388 (file (match-string 2))
1389 (line (match-string 3)))
1390 (save-selected-window
1391 (let* ((buf (find-file-noselect
1392 (if (file-exists-p file) file
1393 (cdr (assoc bptno gdb-location-list)))))
1394 (window (display-buffer buf)))
1395 (with-current-buffer buf
1396 (goto-line (string-to-number line))
1397 (set-window-point window (point))))))
1398 (error "Not recognized as break/watchpoint line"))))
1401 ;; Frames buffer. This displays a perpetually correct bactracktrace
1402 ;; (from the command `where').
1404 ;; Alas, if your stack is deep, it is costly.
1406 (gdb-set-buffer-rules 'gdb-stack-buffer
1407 'gdb-stack-buffer-name
1410 (def-gdb-auto-updated-buffer gdb-stack-buffer
1411 gdb-invalidate-frames
1413 gdb-info-frames-handler
1414 gdb-info-frames-custom)
1416 (defun gdb-info-frames-custom ()
1417 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1419 (let ((buffer-read-only nil))
1420 (goto-char (point-min))
1421 (while (< (point) (point-max))
1422 (add-text-properties (point-at-bol) (point-at-eol)
1423 '(mouse-face highlight
1424 help-echo "mouse-2, RET: Select frame"))
1426 (when (and (looking-at "^#\\([0-9]+\\)")
1427 (equal (match-string 1) gdb-current-stack-level))
1428 (put-text-property (point-at-bol) (point-at-eol)
1429 'face '(:inverse-video t)))
1430 (forward-line 1))))))
1432 (defun gdb-stack-buffer-name ()
1433 (with-current-buffer gud-comint-buffer
1434 (concat "*stack frames of " (gdb-get-target-string) "*")))
1436 (defun gdb-display-stack-buffer ()
1437 "Display backtrace of current stack."
1440 (gdb-get-create-buffer 'gdb-stack-buffer)))
1442 (defun gdb-frame-stack-buffer ()
1443 "Display backtrace of current stack in a new frame."
1445 (let ((special-display-regexps (append special-display-regexps '(".*")))
1446 (special-display-frame-alist gdb-frame-parameters))
1447 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1449 (defvar gdb-frames-mode-map
1450 (let ((map (make-sparse-keymap)))
1451 (suppress-keymap map)
1452 (define-key map "q" 'kill-this-buffer)
1453 (define-key map "\r" 'gdb-frames-select)
1454 (define-key map [mouse-2] 'gdb-frames-select)
1455 (define-key map [follow-link] 'mouse-face)
1458 (defun gdb-frames-mode ()
1459 "Major mode for gdb frames.
1461 \\{gdb-frames-mode-map}"
1462 (kill-all-local-variables)
1463 (setq major-mode 'gdb-frames-mode)
1464 (setq mode-name "Frames")
1465 (setq buffer-read-only t)
1466 (use-local-map gdb-frames-mode-map)
1468 (run-mode-hooks 'gdb-frames-mode-hook)
1469 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1470 'gdb-invalidate-frames
1471 'gdbmi-invalidate-frames))
1473 (defun gdb-get-frame-number ()
1475 (let* ((pos (re-search-backward "^#*\\([0-9]*\\)" nil t))
1476 (n (or (and pos (match-string-no-properties 1)) "0")))
1479 (defun gdb-frames-select (&optional event)
1480 "Select the frame and display the relevant source."
1481 (interactive (list last-input-event))
1482 (if event (mouse-set-point event))
1484 (list (concat gdb-server-prefix "frame " (gdb-get-frame-number) "\n") 'ignore))
1485 (gud-display-frame))
1488 ;; Threads buffer. This displays a selectable thread list.
1490 (gdb-set-buffer-rules 'gdb-threads-buffer
1491 'gdb-threads-buffer-name
1494 (def-gdb-auto-updated-buffer gdb-threads-buffer
1495 gdb-invalidate-threads
1496 (concat gdb-server-prefix "info threads\n")
1497 gdb-info-threads-handler
1498 gdb-info-threads-custom)
1500 (defun gdb-info-threads-custom ()
1501 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1502 (let ((buffer-read-only nil))
1503 (goto-char (point-min))
1504 (while (< (point) (point-max))
1505 (add-text-properties (point-at-bol) (point-at-eol)
1506 '(mouse-face highlight
1507 help-echo "mouse-2, RET: select thread"))
1508 (forward-line 1)))))
1510 (defun gdb-threads-buffer-name ()
1511 (with-current-buffer gud-comint-buffer
1512 (concat "*threads of " (gdb-get-target-string) "*")))
1514 (defun gdb-display-threads-buffer ()
1515 "Display IDs of currently known threads."
1518 (gdb-get-create-buffer 'gdb-threads-buffer)))
1520 (defun gdb-frame-threads-buffer ()
1521 "Display IDs of currently known threads in a new frame."
1523 (let ((special-display-regexps (append special-display-regexps '(".*")))
1524 (special-display-frame-alist gdb-frame-parameters))
1525 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1527 (defvar gdb-threads-mode-map
1528 (let ((map (make-sparse-keymap)))
1529 (suppress-keymap map)
1530 (define-key map "q" 'kill-this-buffer)
1531 (define-key map "\r" 'gdb-threads-select)
1532 (define-key map [mouse-2] 'gdb-threads-select)
1535 (defun gdb-threads-mode ()
1536 "Major mode for gdb frames.
1538 \\{gdb-threads-mode-map}"
1539 (kill-all-local-variables)
1540 (setq major-mode 'gdb-threads-mode)
1541 (setq mode-name "Threads")
1542 (setq buffer-read-only t)
1543 (use-local-map gdb-threads-mode-map)
1544 (run-mode-hooks 'gdb-threads-mode-hook)
1545 'gdb-invalidate-threads)
1547 (defun gdb-get-thread-number ()
1549 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1550 (match-string-no-properties 1)))
1552 (defun gdb-threads-select (&optional event)
1553 "Select the thread and display the relevant source."
1554 (interactive (list last-input-event))
1555 (if event (mouse-set-point event))
1557 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1558 (gud-display-frame))
1561 ;; Registers buffer.
1563 (gdb-set-buffer-rules 'gdb-registers-buffer
1564 'gdb-registers-buffer-name
1565 'gdb-registers-mode)
1567 (def-gdb-auto-updated-buffer gdb-registers-buffer
1568 gdb-invalidate-registers
1569 (concat gdb-server-prefix "info registers\n")
1570 gdb-info-registers-handler
1571 gdb-info-registers-custom)
1573 (defun gdb-info-registers-custom ())
1575 (defvar gdb-registers-mode-map
1576 (let ((map (make-sparse-keymap)))
1577 (suppress-keymap map)
1578 (define-key map "q" 'kill-this-buffer)
1581 (defun gdb-registers-mode ()
1582 "Major mode for gdb registers.
1584 \\{gdb-registers-mode-map}"
1585 (kill-all-local-variables)
1586 (setq major-mode 'gdb-registers-mode)
1587 (setq mode-name "Registers")
1588 (setq buffer-read-only t)
1589 (use-local-map gdb-registers-mode-map)
1590 (run-mode-hooks 'gdb-registers-mode-hook)
1591 'gdb-invalidate-registers)
1593 (defun gdb-registers-buffer-name ()
1594 (with-current-buffer gud-comint-buffer
1595 (concat "*registers of " (gdb-get-target-string) "*")))
1597 (defun gdb-display-registers-buffer ()
1598 "Display integer register contents."
1601 (gdb-get-create-buffer 'gdb-registers-buffer)))
1603 (defun gdb-frame-registers-buffer ()
1604 "Display integer register contents in a new frame."
1606 (let ((special-display-regexps (append special-display-regexps '(".*")))
1607 (special-display-frame-alist gdb-frame-parameters))
1608 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
1612 (defcustom gdb-memory-repeat-count 32
1613 "Number of data items in memory window."
1618 (defcustom gdb-memory-format "x"
1619 "Display format of data items in memory window."
1620 :type '(choice (const :tag "Hexadecimal" "x")
1621 (const :tag "Signed decimal" "d")
1622 (const :tag "Unsigned decimal" "u")
1623 (const :tag "Octal" "o")
1624 (const :tag "Binary" "t"))
1628 (defcustom gdb-memory-unit "w"
1629 "Unit size of data items in memory window."
1630 :type '(choice (const :tag "Byte" "b")
1631 (const :tag "Halfword" "h")
1632 (const :tag "Word" "w")
1633 (const :tag "Giant word" "g"))
1637 (gdb-set-buffer-rules 'gdb-memory-buffer
1638 'gdb-memory-buffer-name
1641 (def-gdb-auto-updated-buffer gdb-memory-buffer
1642 gdb-invalidate-memory
1643 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
1644 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
1645 gdb-read-memory-handler
1646 gdb-read-memory-custom)
1648 (defun gdb-read-memory-custom ())
1650 (defvar gdb-memory-mode-map
1651 (let ((map (make-sparse-keymap)))
1652 (suppress-keymap map)
1653 (define-key map "q" 'kill-this-buffer)
1656 (defun gdb-memory-set-address (event)
1657 "Set the start memory address."
1659 (save-selected-window
1660 (select-window (posn-window (event-start event)))
1661 (let ((arg (read-from-minibuffer "Memory address: ")))
1662 (setq gdb-memory-address arg))
1663 (gdb-invalidate-memory)))
1665 (defun gdb-memory-set-repeat-count (event)
1666 "Set the number of data items in memory window."
1668 (save-selected-window
1669 (select-window (posn-window (event-start event)))
1670 (let* ((arg (read-from-minibuffer "Repeat count: "))
1671 (count (string-to-int arg)))
1673 (error "Non-negative numbers only")
1674 (customize-set-variable 'gdb-memory-repeat-count count)
1675 (gdb-invalidate-memory)))))
1677 (defun gdb-memory-format-binary ()
1678 "Set the display format to binary."
1680 (customize-set-variable 'gdb-memory-format "t")
1681 (gdb-invalidate-memory))
1683 (defun gdb-memory-format-octal ()
1684 "Set the display format to octal."
1686 (customize-set-variable 'gdb-memory-format "o")
1687 (gdb-invalidate-memory))
1689 (defun gdb-memory-format-unsigned ()
1690 "Set the display format to unsigned decimal."
1692 (customize-set-variable 'gdb-memory-format "u")
1693 (gdb-invalidate-memory))
1695 (defun gdb-memory-format-signed ()
1696 "Set the display format to decimal."
1698 (customize-set-variable 'gdb-memory-format "d")
1699 (gdb-invalidate-memory))
1701 (defun gdb-memory-format-hexadecimal ()
1702 "Set the display format to hexadecimal."
1704 (customize-set-variable 'gdb-memory-format "x")
1705 (gdb-invalidate-memory))
1707 (defvar gdb-memory-format-keymap
1708 (let ((map (make-sparse-keymap)))
1709 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
1711 "Keymap to select format in the header line.")
1713 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
1714 "Menu of display formats in the header line.")
1716 (define-key gdb-memory-format-menu [binary]
1717 '(menu-item "Binary" gdb-memory-format-binary
1718 :button (:radio . (equal gdb-memory-format "t"))))
1719 (define-key gdb-memory-format-menu [octal]
1720 '(menu-item "Octal" gdb-memory-format-octal
1721 :button (:radio . (equal gdb-memory-format "o"))))
1722 (define-key gdb-memory-format-menu [unsigned]
1723 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
1724 :button (:radio . (equal gdb-memory-format "u"))))
1725 (define-key gdb-memory-format-menu [signed]
1726 '(menu-item "Signed Decimal" gdb-memory-format-signed
1727 :button (:radio . (equal gdb-memory-format "d"))))
1728 (define-key gdb-memory-format-menu [hexadecimal]
1729 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
1730 :button (:radio . (equal gdb-memory-format "x"))))
1732 (defun gdb-memory-format-menu (event)
1734 (x-popup-menu event gdb-memory-format-menu))
1736 (defun gdb-memory-format-menu-1 (event)
1738 (save-selected-window
1739 (select-window (posn-window (event-start event)))
1740 (let* ((selection (gdb-memory-format-menu event))
1741 (binding (and selection (lookup-key gdb-memory-format-menu
1742 (vector (car selection))))))
1743 (if binding (call-interactively binding)))))
1745 (defun gdb-memory-unit-giant ()
1746 "Set the unit size to giant words (eight bytes)."
1748 (customize-set-variable 'gdb-memory-unit "g")
1749 (gdb-invalidate-memory))
1751 (defun gdb-memory-unit-word ()
1752 "Set the unit size to words (four bytes)."
1754 (customize-set-variable 'gdb-memory-unit "w")
1755 (gdb-invalidate-memory))
1757 (defun gdb-memory-unit-halfword ()
1758 "Set the unit size to halfwords (two bytes)."
1760 (customize-set-variable 'gdb-memory-unit "h")
1761 (gdb-invalidate-memory))
1763 (defun gdb-memory-unit-byte ()
1764 "Set the unit size to bytes."
1766 (customize-set-variable 'gdb-memory-unit "b")
1767 (gdb-invalidate-memory))
1769 (defvar gdb-memory-unit-keymap
1770 (let ((map (make-sparse-keymap)))
1771 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
1773 "Keymap to select units in the header line.")
1775 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
1776 "Menu of units in the header line.")
1778 (define-key gdb-memory-unit-menu [giantwords]
1779 '(menu-item "Giant words" gdb-memory-unit-giant
1780 :button (:radio . (equal gdb-memory-unit "g"))))
1781 (define-key gdb-memory-unit-menu [words]
1782 '(menu-item "Words" gdb-memory-unit-word
1783 :button (:radio . (equal gdb-memory-unit "w"))))
1784 (define-key gdb-memory-unit-menu [halfwords]
1785 '(menu-item "Halfwords" gdb-memory-unit-halfword
1786 :button (:radio . (equal gdb-memory-unit "h"))))
1787 (define-key gdb-memory-unit-menu [bytes]
1788 '(menu-item "Bytes" gdb-memory-unit-byte
1789 :button (:radio . (equal gdb-memory-unit "b"))))
1791 (defun gdb-memory-unit-menu (event)
1793 (x-popup-menu event gdb-memory-unit-menu))
1795 (defun gdb-memory-unit-menu-1 (event)
1797 (save-selected-window
1798 (select-window (posn-window (event-start event)))
1799 (let* ((selection (gdb-memory-unit-menu event))
1800 (binding (and selection (lookup-key gdb-memory-unit-menu
1801 (vector (car selection))))))
1802 (if binding (call-interactively binding)))))
1804 ;;from make-mode-line-mouse-map
1805 (defun gdb-make-header-line-mouse-map (mouse function) "\
1806 Return a keymap with single entry for mouse key MOUSE on the header line.
1807 MOUSE is defined to run function FUNCTION with no args in the buffer
1808 corresponding to the mode line clicked."
1809 (let ((map (make-sparse-keymap)))
1810 (define-key map (vector 'header-line mouse) function)
1811 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1814 (defun gdb-memory-mode ()
1815 "Major mode for examining memory.
1817 \\{gdb-memory-mode-map}"
1818 (kill-all-local-variables)
1819 (setq major-mode 'gdb-memory-mode)
1820 (setq mode-name "Memory")
1821 (setq buffer-read-only t)
1822 (use-local-map gdb-memory-mode-map)
1823 (setq header-line-format
1827 (propertize gdb-memory-address
1828 'face font-lock-warning-face
1829 'help-echo "mouse-1: Set memory address"
1830 'local-map (gdb-make-header-line-mouse-map
1832 #'gdb-memory-set-address))
1834 (propertize (number-to-string gdb-memory-repeat-count)
1835 'face font-lock-warning-face
1836 'help-echo "mouse-1: Set repeat count"
1837 'local-map (gdb-make-header-line-mouse-map
1839 #'gdb-memory-set-repeat-count))
1841 (propertize gdb-memory-format
1842 'face font-lock-warning-face
1843 'help-echo "mouse-3: Select display format"
1844 'local-map gdb-memory-format-keymap)
1846 (propertize gdb-memory-unit
1847 'face font-lock-warning-face
1848 'help-echo "mouse-3: Select unit size"
1849 'local-map gdb-memory-unit-keymap))))
1850 (run-mode-hooks 'gdb-memory-mode-hook)
1851 'gdb-invalidate-memory)
1853 (defun gdb-memory-buffer-name ()
1854 (with-current-buffer gud-comint-buffer
1855 (concat "*memory of " (gdb-get-target-string) "*")))
1857 (defun gdb-display-memory-buffer ()
1858 "Display memory contents."
1861 (gdb-get-create-buffer 'gdb-memory-buffer)))
1863 (defun gdb-frame-memory-buffer ()
1864 "Display memory contents in a new frame."
1866 (let ((special-display-regexps (append special-display-regexps '(".*")))
1867 (special-display-frame-alist gdb-frame-parameters))
1868 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
1873 (gdb-set-buffer-rules 'gdb-locals-buffer
1874 'gdb-locals-buffer-name
1877 (def-gdb-auto-updated-buffer gdb-locals-buffer
1878 gdb-invalidate-locals
1879 "server info locals\n"
1880 gdb-info-locals-handler
1881 gdb-info-locals-custom)
1883 ;; Abbreviate for arrays and structures.
1884 ;; These can be expanded using gud-display.
1885 (defun gdb-info-locals-handler nil
1886 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
1887 gdb-pending-triggers))
1888 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
1889 (with-current-buffer buf
1890 (goto-char (point-min))
1891 (while (re-search-forward "^ .*\n" nil t)
1892 (replace-match "" nil nil))
1893 (goto-char (point-min))
1894 (while (re-search-forward "{[-0-9, {}\]*\n" nil t)
1895 (replace-match "(array);\n" nil nil))
1896 (goto-char (point-min))
1897 (while (re-search-forward "{.*=.*\n" nil t)
1898 (replace-match "(structure);\n" nil nil))))
1899 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
1900 (and buf (with-current-buffer buf
1902 (buffer-read-only nil))
1903 (delete-region (point-min) (point-max))
1904 (insert-buffer-substring (gdb-get-create-buffer
1905 'gdb-partial-output-buffer))
1907 (run-hooks 'gdb-info-locals-hook))
1909 (defun gdb-info-locals-custom ()
1912 (defvar gdb-locals-mode-map
1913 (let ((map (make-sparse-keymap)))
1914 (suppress-keymap map)
1915 (define-key map "q" 'kill-this-buffer)
1918 (defun gdb-locals-mode ()
1919 "Major mode for gdb locals.
1921 \\{gdb-locals-mode-map}"
1922 (kill-all-local-variables)
1923 (setq major-mode 'gdb-locals-mode)
1924 (setq mode-name (concat "Locals:" gdb-current-frame))
1925 (setq buffer-read-only t)
1926 (use-local-map gdb-locals-mode-map)
1927 (run-mode-hooks 'gdb-locals-mode-hook)
1928 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1929 'gdb-invalidate-locals
1930 'gdbmi-invalidate-locals))
1932 (defun gdb-locals-buffer-name ()
1933 (with-current-buffer gud-comint-buffer
1934 (concat "*locals of " (gdb-get-target-string) "*")))
1936 (defun gdb-display-locals-buffer ()
1937 "Display local variables of current stack and their values."
1940 (gdb-get-create-buffer 'gdb-locals-buffer)))
1942 (defun gdb-frame-locals-buffer ()
1943 "Display local variables of current stack and their values in a new frame."
1945 (let ((special-display-regexps (append special-display-regexps '(".*")))
1946 (special-display-frame-alist gdb-frame-parameters))
1947 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
1950 ;;;; Window management
1951 (defun gdb-display-buffer (buf &optional size)
1952 (let ((answer (get-buffer-window buf 0))
1955 (display-buffer buf nil 0) ;Raise the frame if necessary.
1956 ;; The buffer is not yet displayed.
1957 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
1958 (let ((window (get-lru-window)))
1960 (not (eq window (get-buffer-window gud-comint-buffer))))
1962 (set-window-buffer window buf)
1963 (setq answer window))
1964 (setq must-split t)))
1966 (let* ((largest (get-largest-window))
1967 (cur-size (window-height largest))
1968 (new-size (and size (< size cur-size) (- cur-size size))))
1969 (setq answer (split-window largest new-size))
1970 (set-window-buffer answer buf)
1971 (set-window-dedicated-p answer t)))
1975 ;;; Shared keymap initialization:
1977 (let ((menu (make-sparse-keymap "GDB-Windows")))
1978 (define-key gud-menu-map [displays]
1979 `(menu-item "GDB-Windows" ,menu :visible (eq gud-minor-mode 'gdba)))
1980 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
1981 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
1982 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
1983 (define-key menu [assembler] '("Machine" . gdb-display-assembler-buffer))
1984 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
1985 (define-key menu [inferior]
1986 '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
1987 :enable gdb-use-inferior-io-buffer))
1988 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
1989 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
1990 (define-key menu [breakpoints] '("Breakpoints" . gdb-display-breakpoints-buffer)))
1992 (let ((menu (make-sparse-keymap "GDB-Frames")))
1993 (define-key gud-menu-map [frames]
1994 `(menu-item "GDB-Frames" ,menu :visible (eq gud-minor-mode 'gdba)))
1995 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
1996 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
1997 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
1998 (define-key menu [assembler] '("Machine" . gdb-frame-assembler-buffer))
1999 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2000 (define-key menu [inferior]
2001 '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
2002 :enable gdb-use-inferior-io-buffer))
2003 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2004 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2005 (define-key menu [breakpoints] '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2007 (let ((menu (make-sparse-keymap "GDB-UI")))
2008 (define-key gud-menu-map [ui]
2009 `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
2010 (define-key menu [gdb-restore-windows]
2011 '(menu-item "Restore Window Layout" gdb-restore-windows
2012 :help "Restore standard layout for debug session."))
2013 (define-key menu [gdb-many-windows]
2014 '(menu-item "Display Other Windows" gdb-many-windows
2015 :help "Toggle display of locals, stack and breakpoint information"
2016 :button (:toggle . gdb-many-windows)))
2017 (define-key menu [gdb-use-inferior-io]
2018 (menu-bar-make-toggle toggle-gdb-use-inferior-io-buffer
2019 gdb-use-inferior-io-buffer
2020 "Separate inferior IO" "Use separate IO %s"
2021 "Toggle separate IO for inferior.")))
2023 (defadvice toggle-gdb-use-inferior-io-buffer (after gdb-kill-io-buffer activate)
2024 (unless gdb-use-inferior-io-buffer
2025 (kill-buffer (gdb-inferior-io-name))))
2027 (defun gdb-frame-gdb-buffer ()
2028 "Display GUD buffer in a new frame."
2030 (select-frame (make-frame gdb-frame-parameters))
2031 (switch-to-buffer (gdb-get-create-buffer 'gdba))
2032 (set-window-dedicated-p (selected-window) t))
2034 (defun gdb-display-gdb-buffer ()
2035 "Display GUD buffer."
2038 (gdb-get-create-buffer 'gdba)))
2040 (defvar gdb-main-file nil "Source file from which program execution begins.")
2042 (defcustom gdb-show-main nil
2043 "Nil means don't display source file containing the main routine."
2048 (defun gdb-set-window-buffer (name)
2049 (set-window-buffer (selected-window) (get-buffer name))
2050 (set-window-dedicated-p (selected-window) t))
2052 (defun gdb-setup-windows ()
2053 "Layout the window pattern for gdb-many-windows."
2054 (gdb-display-locals-buffer)
2055 (gdb-display-stack-buffer)
2056 (delete-other-windows)
2057 (gdb-display-breakpoints-buffer)
2058 (delete-other-windows)
2060 (pop-to-buffer gud-comint-buffer)
2061 (split-window nil ( / ( * (window-height) 3) 4))
2062 (split-window nil ( / (window-height) 3))
2063 (split-window-horizontally)
2065 (gdb-set-window-buffer (gdb-locals-buffer-name))
2068 (if gud-last-last-frame
2069 (gud-find-file (car gud-last-last-frame))
2070 (gud-find-file gdb-main-file)))
2071 (when gdb-use-inferior-io-buffer
2072 (split-window-horizontally)
2074 (gdb-set-window-buffer
2075 (gdb-get-create-buffer 'gdb-inferior-io)))
2077 (gdb-set-window-buffer (gdb-stack-buffer-name))
2078 (split-window-horizontally)
2080 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2083 (defcustom gdb-many-windows nil
2084 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2085 In this case it starts with two windows: one displaying the GUD
2086 buffer and the other with the source file with the main routine
2087 of the inferior. Non-nil means display the layout shown for
2093 (defun gdb-many-windows (arg)
2094 "Toggle the number of windows in the basic arrangement."
2096 (setq gdb-many-windows
2098 (not gdb-many-windows)
2099 (> (prefix-numeric-value arg) 0)))
2101 (gdb-restore-windows)
2104 (defun gdb-restore-windows ()
2105 "Restore the basic arrangement of windows used by gdba.
2106 This arrangement depends on the value of `gdb-many-windows'."
2108 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2109 (delete-other-windows)
2110 (if gdb-many-windows
2115 (if gud-last-last-frame
2116 (gud-find-file (car gud-last-last-frame))
2117 (gud-find-file gdb-main-file)))
2121 "Exit a debugging session cleanly.
2122 Kills the gdb buffers and resets the source buffers."
2123 (dolist (buffer (buffer-list))
2124 (unless (eq buffer gud-comint-buffer)
2125 (with-current-buffer buffer
2126 (if (memq gud-minor-mode '(gdbmi gdba))
2127 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2129 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2130 (setq gud-minor-mode nil)
2131 (kill-local-variable 'tool-bar-map)
2132 (setq gud-running nil))))))
2133 (when (markerp gdb-overlay-arrow-position)
2134 (move-marker gdb-overlay-arrow-position nil)
2135 (setq gdb-overlay-arrow-position nil))
2136 (setq overlay-arrow-variable-list
2137 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list)))
2139 (defun gdb-source-info ()
2140 "Find the source file where the program starts and displays it with related
2142 (goto-char (point-min))
2143 (if (and (search-forward "Located in " nil t)
2144 (looking-at "\\S-*"))
2145 (setq gdb-main-file (match-string 0)))
2146 (if gdb-many-windows
2148 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2150 (let ((pop-up-windows t))
2151 (display-buffer (gud-find-file gdb-main-file))))))
2153 (defun gdb-get-location (bptno line flag)
2154 "Find the directory containing the relevant source file.
2155 Put in buffer and place breakpoint icon."
2156 (goto-char (point-min))
2157 (catch 'file-not-found
2158 (if (search-forward "Located in " nil t)
2159 (if (looking-at "\\S-*")
2160 (push (cons bptno (match-string 0)) gdb-location-list))
2162 (push (cons bptno "File not found") gdb-location-list)
2163 (message-box "Cannot find source file for breakpoint location.\n\
2164 Add directory to search path for source files using the GDB command, dir.")
2165 (throw 'file-not-found nil))
2166 (with-current-buffer
2167 (find-file-noselect (match-string 0))
2168 (save-current-buffer
2169 (set (make-local-variable 'gud-minor-mode) 'gdba)
2170 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2171 ;; only want one breakpoint icon at each location
2173 (goto-line (string-to-number line))
2174 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2176 (add-hook 'find-file-hook 'gdb-find-file-hook)
2178 (defun gdb-find-file-hook ()
2179 (if (and (not gdb-find-file-unhook)
2180 ;; in case gud or gdb-ui is just loaded
2182 (buffer-name gud-comint-buffer)
2183 (with-current-buffer gud-comint-buffer
2184 (eq gud-minor-mode 'gdba)))
2187 (list (concat gdb-server-prefix "list "
2188 (file-name-nondirectory buffer-file-name)
2190 `(lambda () (gdb-set-gud-minor-mode ,(current-buffer)))))
2191 (error (setq gdb-find-file-unhook t)))))
2194 (defun gdb-put-string (putstring pos &optional dprop)
2195 "Put string PUTSTRING in front of POS in the current buffer.
2196 PUTSTRING is displayed by putting an overlay into the current buffer with a
2197 `before-string' STRING that has a `display' property whose value is
2199 (let ((string (make-string 1 ?x))
2200 (buffer (current-buffer)))
2201 (setq putstring (copy-sequence putstring))
2202 (let ((overlay (make-overlay pos pos buffer))
2204 (list (list 'margin 'left-margin) putstring))))
2205 (put-text-property 0 (length string) 'display prop string)
2206 (overlay-put overlay 'put-break t)
2207 (overlay-put overlay 'before-string string))))
2209 ;;from remove-images
2210 (defun gdb-remove-strings (start end &optional buffer)
2211 "Remove strings between START and END in BUFFER.
2212 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2213 BUFFER nil or omitted means use the current buffer."
2215 (setq buffer (current-buffer)))
2216 (dolist (overlay (overlays-in start end))
2217 (when (overlay-get overlay 'put-break)
2218 (delete-overlay overlay))))
2220 (defun gdb-put-breakpoint-icon (enabled bptno)
2221 (let ((start (progn (beginning-of-line) (- (point) 1)))
2222 (end (progn (end-of-line) (+ (point) 1)))
2223 (putstring (if enabled "B" "b")))
2224 (add-text-properties
2225 0 1 '(help-echo "mouse-1: set/clear bkpt, mouse-3: enable/disable bkpt")
2227 (if enabled (add-text-properties
2228 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2229 (add-text-properties
2230 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2231 (gdb-remove-breakpoint-icons start end)
2232 (if (display-images-p)
2233 (if (>= (car (window-fringes)) 8)
2236 `(left-fringe breakpoint
2239 'breakpoint-disabled)))
2240 (when (< left-margin-width 2)
2241 (save-current-buffer
2242 (setq left-margin-width 2)
2243 (if (get-buffer-window (current-buffer) 0)
2245 (get-buffer-window (current-buffer) 0)
2246 left-margin-width right-margin-width))))
2249 (or breakpoint-enabled-icon
2250 (setq breakpoint-enabled-icon
2251 (find-image `((:type xpm :data
2252 ,breakpoint-xpm-data
2253 :ascent 100 :pointer hand)
2255 ,breakpoint-enabled-pbm-data
2256 :ascent 100 :pointer hand)))))
2257 (or breakpoint-disabled-icon
2258 (setq breakpoint-disabled-icon
2259 (find-image `((:type xpm :data
2260 ,breakpoint-xpm-data
2261 :conversion disabled
2264 ,breakpoint-disabled-pbm-data
2269 (when (< left-margin-width 2)
2270 (save-current-buffer
2271 (setq left-margin-width 2)
2272 (if (get-buffer-window (current-buffer) 0)
2274 (get-buffer-window (current-buffer) 0)
2275 left-margin-width right-margin-width))))
2277 (propertize putstring
2278 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2281 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2282 (gdb-remove-strings start end)
2283 (if (display-images-p)
2284 (remove-images start end))
2286 (setq left-margin-width 0)
2287 (if (get-buffer-window (current-buffer) 0)
2289 (get-buffer-window (current-buffer) 0)
2290 left-margin-width right-margin-width))))
2294 ;; Assembler buffer.
2296 (gdb-set-buffer-rules 'gdb-assembler-buffer
2297 'gdb-assembler-buffer-name
2298 'gdb-assembler-mode)
2300 (def-gdb-auto-updated-buffer gdb-assembler-buffer
2301 gdb-invalidate-assembler
2302 (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
2303 gdb-assembler-handler
2304 gdb-assembler-custom)
2306 (defun gdb-assembler-custom ()
2307 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2308 (pos 1) (address) (flag) (bptno))
2309 (with-current-buffer buffer
2310 (if (not (equal gdb-current-address "main"))
2312 (goto-char (point-min))
2313 (if (re-search-forward gdb-current-address nil t)
2317 (or gdb-overlay-arrow-position
2318 (setq gdb-overlay-arrow-position (make-marker)))
2319 (set-marker gdb-overlay-arrow-position
2320 (point) (current-buffer))))))
2321 ;; remove all breakpoint-icons in assembler buffer before updating.
2322 (gdb-remove-breakpoint-icons (point-min) (point-max)))
2323 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2324 (goto-char (point-min))
2325 (while (< (point) (- (point-max) 1))
2327 (if (looking-at "[^\t].*breakpoint")
2330 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x\\(\\S-+\\)")
2331 (setq bptno (match-string 1))
2332 (setq flag (char-after (match-beginning 2)))
2333 (setq address (match-string 3))
2334 ;; remove leading 0s from output of info break.
2335 (if (string-match "^0+\\(.*\\)" address)
2336 (setq address (match-string 1 address)))
2337 (with-current-buffer buffer
2338 (goto-char (point-min))
2339 (if (re-search-forward address nil t)
2340 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
2341 (if (not (equal gdb-current-address "main"))
2342 (set-window-point (get-buffer-window buffer 0) pos))))
2344 (defvar gdb-assembler-mode-map
2345 (let ((map (make-sparse-keymap)))
2346 (suppress-keymap map)
2347 (define-key map "q" 'kill-this-buffer)
2350 (defvar gdb-assembler-font-lock-keywords
2351 '(;; <__function.name+n>
2352 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2353 (1 font-lock-function-name-face))
2354 ;; 0xNNNNNNNN <__function.name+n>: opcode
2355 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2356 (4 font-lock-keyword-face))
2357 ;; %register(at least i386)
2358 ("%\\sw+" . font-lock-variable-name-face)
2359 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2360 (1 font-lock-comment-face)
2361 (2 font-lock-function-name-face))
2362 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2363 "Font lock keywords used in `gdb-assembler-mode'.")
2365 (defun gdb-assembler-mode ()
2366 "Major mode for viewing code assembler.
2368 \\{gdb-assembler-mode-map}"
2369 (kill-all-local-variables)
2370 (setq major-mode 'gdb-assembler-mode)
2371 (setq mode-name "Machine")
2372 (setq gdb-overlay-arrow-position nil)
2373 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2374 (setq fringes-outside-margins t)
2375 (setq buffer-read-only t)
2376 (use-local-map gdb-assembler-mode-map)
2377 (gdb-invalidate-assembler)
2378 (set (make-local-variable 'font-lock-defaults)
2379 '(gdb-assembler-font-lock-keywords))
2380 (run-mode-hooks 'gdb-assembler-mode-hook)
2381 'gdb-invalidate-assembler)
2383 (defun gdb-assembler-buffer-name ()
2384 (with-current-buffer gud-comint-buffer
2385 (concat "*Machine Code " (gdb-get-target-string) "*")))
2387 (defun gdb-display-assembler-buffer ()
2388 "Display disassembly view."
2391 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2393 (defun gdb-frame-assembler-buffer ()
2394 "Display disassembly view in a new frame."
2396 (let ((special-display-regexps (append special-display-regexps '(".*")))
2397 (special-display-frame-alist gdb-frame-parameters))
2398 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2400 ;; modified because if gdb-current-address has changed value a new command
2401 ;; must be enqueued to update the buffer with the new output
2402 (defun gdb-invalidate-assembler (&optional ignored)
2403 (if (gdb-get-buffer 'gdb-assembler-buffer)
2405 (unless (string-equal gdb-current-frame gdb-previous-frame)
2406 (if (or (not (member 'gdb-invalidate-assembler
2407 gdb-pending-triggers))
2408 (not (string-equal gdb-current-address
2409 gdb-previous-address)))
2411 ;; take previous disassemble command off the queue
2412 (with-current-buffer gud-comint-buffer
2413 (let ((queue gdb-input-queue))
2414 (dolist (item queue)
2415 (if (equal (cdr item) '(gdb-assembler-handler))
2416 (setq gdb-input-queue
2417 (delete item gdb-input-queue))))))
2419 (list (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
2420 'gdb-assembler-handler))
2421 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2422 (setq gdb-previous-address gdb-current-address)
2423 (setq gdb-previous-frame gdb-current-frame)))))))
2425 (defun gdb-get-current-frame ()
2426 (if (not (member 'gdb-get-current-frame gdb-pending-triggers))
2429 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2430 (push 'gdb-get-current-frame
2431 gdb-pending-triggers))))
2433 (defun gdb-frame-handler ()
2434 (setq gdb-pending-triggers
2435 (delq 'gdb-get-current-frame gdb-pending-triggers))
2436 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2437 (goto-char (point-min))
2438 (if (looking-at "Stack level \\([0-9]+\\)")
2439 (setq gdb-current-stack-level (match-string 1)))
2441 (if (looking-at ".*=\\s-+0x\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? ")
2443 (setq gdb-current-frame (match-string 2))
2444 (if (gdb-get-buffer 'gdb-locals-buffer)
2445 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2446 (setq mode-name (concat "Locals:" gdb-current-frame))))
2447 (let ((address (match-string 1)))
2448 ;; remove leading 0s from output of info frame command.
2449 (if (string-match "^0+\\(.*\\)" address)
2450 (setq gdb-current-address
2451 (concat "0x" (match-string 1 address)))
2452 (setq gdb-current-address (concat "0x" address))))
2453 (if (not (re-search-forward "(\\S-*:[0-9]*);" nil t))
2454 ;;update with new frame for machine code if necessary
2455 (gdb-invalidate-assembler)))))
2456 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2457 (setq gdb-current-language (match-string 1))))
2461 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2462 ;;; gdb-ui.el ends here