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-alist nil
83 "Alist of breakpoint numbers and full filenames.")
84 (defvar gdb-find-file-unhook nil)
85 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
86 and #define directives otherwise.")
88 (defvar gdb-buffer-type nil
89 "One of the symbols bound in `gdb-buffer-rules'.")
91 (defvar gdb-input-queue ()
92 "A list of gdb command objects.")
94 (defvar gdb-prompting nil
95 "True when gdb is idle with no pending input.")
97 (defvar gdb-output-sink 'user
98 "The disposition of the output of the current gdb command.
99 Possible values are these symbols:
101 `user' -- gdb output should be copied to the GUD buffer
104 `inferior' -- gdb output should be copied to the inferior-io buffer
106 `pre-emacs' -- output should be ignored util the post-prompt
107 annotation is received. Then the output-sink
109 `emacs' -- output should be collected in the partial-output-buffer
110 for subsequent processing by a command. This is the
111 disposition of output generated by commands that
112 gdb mode sends to gdb on its own behalf.
113 `post-emacs' -- ignore output until the prompt annotation is
114 received, then go to USER disposition.
116 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
117 \(`user' and `emacs').")
119 (defvar gdb-current-item nil
120 "The most recent command item sent to gdb.")
122 (defvar gdb-pending-triggers '()
123 "A list of trigger functions that have run later than their output
126 ;; end of gdb variables
129 (defun gdba (command-line)
130 "Run gdb on program FILE in buffer *gud-FILE*.
131 The directory containing FILE becomes the initial working directory
132 and source-file directory for your debugger.
134 If `gdb-many-windows' is nil (the default value) then gdb just
135 pops up the GUD buffer unless `gdb-show-main' is t. In this case
136 it starts with two windows: one displaying the GUD buffer and the
137 other with the source file with the main routine of the inferior.
139 If `gdb-many-windows' is t, regardless of the value of
140 `gdb-show-main', the layout below will appear unless
141 `gdb-use-inferior-io-buffer' is nil when the source buffer
142 occupies the full width of the frame. Keybindings are given in
145 Watch expressions appear in the speedbar/slowbar.
147 The following commands help control operation :
149 `gdb-many-windows' - Toggle the number of windows gdb uses.
150 `gdb-restore-windows' - To restore the window layout.
152 See Info node `(emacs)GDB Graphical Interface' for a more
153 detailed description of this mode.
156 ---------------------------------------------------------------------
158 ---------------------------------------------------------------------
159 GUD buffer (I/O of GDB) | Locals buffer
163 ---------------------------------------------------------------------
164 Source buffer | Input/Output (of inferior) buffer
172 ---------------------------------------------------------------------
173 Stack buffer | Breakpoints buffer
174 RET gdb-frames-select | SPC gdb-toggle-breakpoint
175 | RET gdb-goto-breakpoint
176 | d gdb-delete-breakpoint
177 ---------------------------------------------------------------------"
179 (interactive (list (gud-query-cmdline 'gdba)))
181 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
185 (defvar gdb-debug-log nil)
187 (defcustom gdb-enable-debug-log nil
188 "Non-nil means record the process input and output in `gdb-debug-log'."
193 (defcustom gdb-use-inferior-io-buffer nil
194 "Non-nil means display output from the inferior in a separate buffer."
199 (defun gdb-set-gud-minor-mode (buffer)
200 "Set gud-minor-mode from find-file if appropriate."
201 (goto-char (point-min))
202 (unless (search-forward "No source file named " nil t)
205 (list (concat gdb-server-prefix "info source\n")
206 `(lambda () (gdb-set-gud-minor-mode-1 ,buffer))))
207 (error (setq gdb-find-file-unhook t)))))
209 (defun gdb-set-gud-minor-mode-1 (buffer)
210 (goto-char (point-min))
211 (if (and (search-forward "Located in " nil t)
213 (string-equal (buffer-file-name buffer)
215 (with-current-buffer buffer
216 (set (make-local-variable 'gud-minor-mode) 'gdba)
217 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))))
219 (defun gdb-set-gud-minor-mode-existing-buffers ()
220 (dolist (buffer (buffer-list))
221 (let ((file (buffer-file-name buffer)))
225 (list (concat gdb-server-prefix "list "
226 (file-name-nondirectory file) ":1\n")
227 `(lambda () (gdb-set-gud-minor-mode ,buffer)))))))))
230 (setq gdb-debug-log nil)
231 (set (make-local-variable 'gud-minor-mode) 'gdba)
232 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
234 (gud-def gud-break (if (not (string-equal mode-name "Machine"))
235 (gud-call "break %f:%l" arg)
239 (gud-call "break *%a" arg)))
240 "\C-b" "Set breakpoint at current line or address.")
242 (gud-def gud-remove (if (not (string-equal mode-name "Machine"))
243 (gud-call "clear %f:%l" arg)
247 (gud-call "clear *%a" arg)))
248 "\C-d" "Remove breakpoint at current line or address.")
250 (gud-def gud-until (if (not (string-equal mode-name "Machine"))
251 (gud-call "until %f:%l" arg)
255 (gud-call "until *%a" arg)))
256 "\C-u" "Continue to current line or address.")
258 (define-key gud-minor-mode-map [left-margin mouse-1]
259 'gdb-mouse-set-clear-breakpoint)
260 (define-key gud-minor-mode-map [left-fringe mouse-1]
261 'gdb-mouse-set-clear-breakpoint)
262 (define-key gud-minor-mode-map [left-margin mouse-3]
263 'gdb-mouse-toggle-breakpoint)
264 ; Currently only works in margin.
265 ; (define-key gud-minor-mode-map [left-fringe mouse-3]
266 ; 'gdb-mouse-toggle-breakpoint)
268 (setq comint-input-sender 'gdb-send)
271 (setq gdb-current-address "main")
272 (setq gdb-previous-address nil)
273 (setq gdb-memory-address "main")
274 (setq gdb-previous-frame nil)
275 (setq gdb-current-frame nil)
276 (setq gdb-current-stack-level nil)
277 (setq gdb-var-list nil)
278 (setq gdb-var-changed nil)
279 (setq gdb-first-prompt nil)
280 (setq gdb-prompting nil)
281 (setq gdb-input-queue nil)
282 (setq gdb-current-item nil)
283 (setq gdb-pending-triggers nil)
284 (setq gdb-output-sink 'user)
285 (setq gdb-server-prefix "server ")
286 (setq gdb-flush-pending-output nil)
287 (setq gdb-location-alist nil)
288 (setq gdb-find-file-unhook nil)
290 (setq gdb-buffer-type 'gdba)
292 (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
294 (if (eq window-system 'w32)
295 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
296 (gdb-enqueue-input (list "set height 0\n" 'ignore))
297 (gdb-enqueue-input (list "set width 0\n" 'ignore))
298 ;; find source file and compilation directory here
299 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
300 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
301 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
303 (gdb-set-gud-minor-mode-existing-buffers)
304 (run-hooks 'gdba-mode-hook))
306 (defcustom gdb-use-colon-colon-notation nil
307 "If non-nil use FUN::VAR format to display variables in the speedbar."
313 "Watch expression at point."
316 (let ((expr (tooltip-identifier-from-point (point))))
317 (if (and (string-equal gdb-current-language "c")
318 gdb-use-colon-colon-notation gdb-current-frame)
319 (setq expr (concat gdb-current-frame "::" expr)))
320 (catch 'already-watched
321 (dolist (var gdb-var-list)
322 (if (string-equal expr (car var)) (throw 'already-watched nil)))
323 (set-text-properties 0 (length expr) nil expr)
326 (if (eq gud-minor-mode 'gdba)
327 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
328 (concat"-var-create - * " expr "\n"))
329 `(lambda () (gdb-var-create-handler ,expr))))))
330 (select-window (get-buffer-window gud-comint-buffer 0)))
332 (defconst gdb-var-create-regexp
333 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
335 (defun gdb-var-create-handler (expr)
336 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
337 (goto-char (point-min))
338 (if (re-search-forward gdb-var-create-regexp nil t)
339 (let ((var (list expr
344 (push var gdb-var-list)
346 (if (equal (nth 2 var) "0")
349 (if (with-current-buffer
350 gud-comint-buffer (eq gud-minor-mode 'gdba))
351 (concat "server interpreter mi \"-var-evaluate-expression "
353 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
354 `(lambda () (gdb-var-evaluate-expression-handler
356 (setq gdb-var-changed t)))
357 (if (re-search-forward "Undefined command" nil t)
358 (message-box "Watching expressions requires gdb 6.0 onwards")
359 (message "No symbol %s in current context." expr)))))
361 (defun gdb-var-evaluate-expression-handler (varnum changed)
362 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
363 (goto-char (point-min))
364 (re-search-forward ".*value=\"\\(.*?\\)\"" nil t)
367 (dolist (var gdb-var-list)
368 (if (string-equal varnum (cadr var))
370 (if changed (setcar (nthcdr 5 var) t))
371 (setcar (nthcdr 4 var) (match-string 1))
372 (setcar (nthcdr num gdb-var-list) var)
373 (throw 'var-found nil)))
374 (setq num (+ num 1))))))
375 (setq gdb-var-changed t))
377 (defun gdb-var-list-children (varnum)
379 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
380 `(lambda () (gdb-var-list-children-handler ,varnum)))))
382 (defconst gdb-var-list-children-regexp
383 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\"")
385 (defun gdb-var-list-children-handler (varnum)
386 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
387 (goto-char (point-min))
388 (let ((var-list nil))
389 (catch 'child-already-watched
390 (dolist (var gdb-var-list)
391 (if (string-equal varnum (cadr var))
394 (while (re-search-forward gdb-var-list-children-regexp nil t)
395 (let ((varchild (list (match-string 2)
399 (if (looking-at ",type=\"\\(.*?\\)\"")
400 (setcar (nthcdr 3 varchild) (match-string 1)))
401 (dolist (var1 gdb-var-list)
402 (if (string-equal (cadr var1) (cadr varchild))
403 (throw 'child-already-watched nil)))
404 (push varchild var-list)
405 (if (equal (nth 2 varchild) "0")
409 "server interpreter mi \"-var-evaluate-expression "
410 (nth 1 varchild) "\"\n")
411 `(lambda () (gdb-var-evaluate-expression-handler
412 ,(nth 1 varchild) nil))))))))
413 (push var var-list)))
414 (setq gdb-var-list (nreverse var-list))))))
416 (defun gdb-var-update ()
417 (if (not (member 'gdb-var-update gdb-pending-triggers))
421 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
422 "server interpreter mi \"-var-update *\"\n"
424 'gdb-var-update-handler))
425 (push 'gdb-var-update gdb-pending-triggers))))
427 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
429 (defun gdb-var-update-handler ()
430 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
431 (goto-char (point-min))
432 (while (re-search-forward gdb-var-update-regexp nil t)
433 (let ((varnum (match-string 1)))
436 (if (with-current-buffer gud-comint-buffer
437 (eq gud-minor-mode 'gdba))
438 (concat "server interpreter mi \"-var-evaluate-expression "
440 (concat "-var-evaluate-expression " varnum "\n"))
441 `(lambda () (gdb-var-evaluate-expression-handler
443 (setq gdb-pending-triggers
444 (delq 'gdb-var-update gdb-pending-triggers))
445 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
446 ;; dummy command to update speedbar at right time
447 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
448 ;; keep gdb-pending-triggers non-nil till end
449 (push 'gdb-speedbar-timer gdb-pending-triggers)))
451 (defun gdb-speedbar-timer-fn ()
452 (setq gdb-pending-triggers
453 (delq 'gdb-speedbar-timer gdb-pending-triggers))
454 (with-current-buffer gud-comint-buffer
455 (speedbar-timer-fn)))
457 (defun gdb-var-delete ()
458 "Delete watched expression from the speedbar."
460 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
461 (let ((text (speedbar-line-text)))
462 (string-match "\\(\\S-+\\)" text)
463 (let* ((expr (match-string 1 text))
464 (var (assoc expr gdb-var-list))
466 (unless (string-match "\\." varnum)
469 (if (with-current-buffer gud-comint-buffer
470 (eq gud-minor-mode 'gdba))
471 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
472 (concat "-var-delete " varnum "\n"))
474 (setq gdb-var-list (delq var gdb-var-list))
475 (dolist (varchild gdb-var-list)
476 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
477 (setq gdb-var-list (delq varchild gdb-var-list))))
478 (setq gdb-var-changed t))))))
480 (defun gdb-edit-value (text token indent)
481 "Assign a value to a variable displayed in the speedbar."
482 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
483 (varnum (cadr var)) (value))
484 (setq value (read-string "New value: "))
487 (if (with-current-buffer gud-comint-buffer
488 (eq gud-minor-mode 'gdba))
489 (concat "server interpreter mi \"-var-assign "
490 varnum " " value "\"\n")
491 (concat "-var-assign " varnum " " value "\n"))
494 (defcustom gdb-show-changed-values t
495 "If non-nil highlight values that have recently changed in the speedbar.
496 The highlighting is done with `font-lock-warning-face'."
501 (defun gdb-speedbar-expand-node (text token indent)
502 "Expand the node the user clicked on.
503 TEXT is the text of the button we clicked on, a + or - item.
504 TOKEN is data related to this node.
505 INDENT is the current indentation depth."
506 (cond ((string-match "+" text) ;expand this node
507 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
508 (gdb-var-list-children token)
509 (gdbmi-var-list-children token)))
510 ((string-match "-" text) ;contract this node
511 (dolist (var gdb-var-list)
512 (if (string-match (concat token "\\.") (nth 1 var))
513 (setq gdb-var-list (delq var gdb-var-list))))
514 (setq gdb-var-changed t))))
516 (defun gdb-get-target-string ()
517 (with-current-buffer gud-comint-buffer
524 ;; Each buffer has a TYPE -- a symbol that identifies the function
525 ;; of that particular buffer.
527 ;; The usual gdb interaction buffer is given the type `gdba' and
528 ;; is constructed specially.
530 ;; Others are constructed by gdb-get-create-buffer and
531 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
533 (defvar gdb-buffer-rules-assoc '())
535 (defun gdb-get-buffer (key)
536 "Return the gdb buffer tagged with type KEY.
537 The key should be one of the cars in `gdb-buffer-rules-assoc'."
539 (gdb-look-for-tagged-buffer key (buffer-list))))
541 (defun gdb-get-create-buffer (key)
542 "Create a new gdb buffer of the type specified by KEY.
543 The key should be one of the cars in `gdb-buffer-rules-assoc'."
544 (or (gdb-get-buffer key)
545 (let* ((rules (assoc key gdb-buffer-rules-assoc))
546 (name (funcall (gdb-rules-name-maker rules)))
547 (new (get-buffer-create name)))
548 (with-current-buffer new
550 (if (cdr (cdr rules))
551 (setq trigger (funcall (car (cdr (cdr rules))))))
552 (set (make-local-variable 'gdb-buffer-type) key)
553 (set (make-local-variable 'gud-minor-mode)
554 (with-current-buffer gud-comint-buffer gud-minor-mode))
555 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
556 (if trigger (funcall trigger)))
559 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
561 (defun gdb-look-for-tagged-buffer (key bufs)
563 (while (and (not retval) bufs)
564 (set-buffer (car bufs))
565 (if (eq gdb-buffer-type key)
566 (setq retval (car bufs)))
567 (setq bufs (cdr bufs)))
571 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
572 ;; at least one and possible more functions. The functions have these
573 ;; roles in defining a buffer type:
575 ;; NAME - Return a name for this buffer type.
577 ;; The remaining function(s) are optional:
579 ;; MODE - called in a new buffer with no arguments, should establish
580 ;; the proper mode for the buffer.
583 (defun gdb-set-buffer-rules (buffer-type &rest rules)
584 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
586 (setcdr binding rules)
587 (push (cons buffer-type rules)
588 gdb-buffer-rules-assoc))))
590 ;; GUD buffers are an exception to the rules
591 (gdb-set-buffer-rules 'gdba 'error)
594 ;; Partial-output buffer : This accumulates output from a command executed on
595 ;; behalf of emacs (rather than the user).
597 (gdb-set-buffer-rules 'gdb-partial-output-buffer
598 'gdb-partial-output-name)
600 (defun gdb-partial-output-name ()
601 (concat "*partial-output-"
602 (gdb-get-target-string)
606 (gdb-set-buffer-rules 'gdb-inferior-io
607 'gdb-inferior-io-name
608 'gdb-inferior-io-mode)
610 (defun gdb-inferior-io-name ()
611 (concat "*input/output of "
612 (gdb-get-target-string)
615 (defun gdb-display-inferior-io-buffer ()
616 "Display IO of inferior in a separate window."
618 (if gdb-use-inferior-io-buffer
620 (gdb-get-create-buffer 'gdb-inferior-io))))
622 (defun gdb-frame-inferior-io-buffer ()
623 "Display IO of inferior in a new frame."
625 (if gdb-use-inferior-io-buffer
626 (let ((special-display-regexps (append special-display-regexps '(".*")))
627 (special-display-frame-alist gdb-frame-parameters))
628 (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
630 (defvar gdb-inferior-io-mode-map
631 (let ((map (make-sparse-keymap)))
632 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
633 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
634 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
635 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
638 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
639 "Major mode for gdb inferior-io."
640 :syntax-table nil :abbrev-table nil
641 ;; We want to use comint because it has various nifty and familiar
642 ;; features. We don't need a process, but comint wants one, so create
644 (make-comint-in-buffer
645 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
646 (current-buffer) "hexl")
647 (setq comint-input-sender 'gdb-inferior-io-sender))
649 (defun gdb-inferior-io-sender (proc string)
650 ;; PROC is the pseudo-process created to satisfy comint.
651 (with-current-buffer (process-buffer proc)
652 (setq proc (get-buffer-process gud-comint-buffer))
653 (process-send-string proc string)
654 (process-send-string proc "\n")))
656 (defun gdb-inferior-io-interrupt ()
657 "Interrupt the program being debugged."
660 (get-buffer-process gud-comint-buffer) comint-ptyp))
662 (defun gdb-inferior-io-quit ()
663 "Send quit signal to the program being debugged."
666 (get-buffer-process gud-comint-buffer) comint-ptyp))
668 (defun gdb-inferior-io-stop ()
669 "Stop the program being debugged."
672 (get-buffer-process gud-comint-buffer) comint-ptyp))
674 (defun gdb-inferior-io-eof ()
675 "Send end-of-file to the program being debugged."
678 (get-buffer-process gud-comint-buffer)))
682 ;; gdb communications
685 ;; INPUT: things sent to gdb
687 ;; The queues are lists. Each element is either a string (indicating user or
688 ;; user-like input) or a list of the form:
690 ;; (INPUT-STRING HANDLER-FN)
692 ;; The handler function will be called from the partial-output buffer when the
693 ;; command completes. This is the way to write commands which invoke gdb
694 ;; commands autonomously.
696 ;; These lists are consumed tail first.
699 (defun gdb-send (proc string)
700 "A comint send filter for gdb.
701 This filter may simply queue input for a later time."
702 (let ((item (concat string "\n")))
705 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
706 (process-send-string proc item))
707 (gdb-enqueue-input item))))
709 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
710 ;; is a query, or other non-top-level prompt.
712 (defun gdb-enqueue-input (item)
716 (setq gdb-prompting nil))
717 (push item gdb-input-queue)))
719 (defun gdb-dequeue-input ()
720 (let ((queue gdb-input-queue))
722 (let ((last (car (last queue))))
723 (unless (nbutlast queue) (setq gdb-input-queue '()))
726 (defun gdb-send-item (item)
727 (setq gdb-flush-pending-output nil)
728 (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
729 (setq gdb-current-item item)
730 (with-current-buffer gud-comint-buffer
731 (if (eq gud-minor-mode 'gdba)
734 (setq gdb-output-sink 'user)
735 (process-send-string (get-buffer-process gud-comint-buffer) item))
737 (gdb-clear-partial-output)
738 (setq gdb-output-sink 'pre-emacs)
739 (process-send-string (get-buffer-process gud-comint-buffer)
741 ;; case: eq gud-minor-mode 'gdbmi
742 (gdb-clear-partial-output)
743 (setq gdb-output-sink 'emacs)
744 (process-send-string (get-buffer-process gud-comint-buffer)
748 ;; output -- things gdb prints to emacs
750 ;; GDB output is a stream interrupted by annotations.
751 ;; Annotations can be recognized by their beginning
752 ;; with \C-j\C-z\C-z<tag><opt>\C-j
754 ;; The tag is a string obeying symbol syntax.
756 ;; The optional part `<opt>' can be either the empty string
757 ;; or a space followed by more data relating to the annotation.
758 ;; For example, the SOURCE annotation is followed by a filename,
759 ;; line number and various useless goo. This data must not include
763 (defcustom gud-gdba-command-name "gdb -annotate=3"
764 "Default command to execute an executable under the GDB-UI debugger."
769 (defvar gdb-annotation-rules
770 '(("pre-prompt" gdb-pre-prompt)
771 ("prompt" gdb-prompt)
772 ("commands" gdb-subprompt)
773 ("overload-choice" gdb-subprompt)
774 ("query" gdb-subprompt)
775 ;; Need this prompt for GDB 6.1
776 ("nquery" gdb-subprompt)
777 ("prompt-for-continue" gdb-subprompt)
778 ("post-prompt" gdb-post-prompt)
779 ("source" gdb-source)
780 ("starting" gdb-starting)
781 ("exited" gdb-exited)
782 ("signalled" gdb-exited)
783 ("signal" gdb-stopping)
784 ("breakpoint" gdb-stopping)
785 ("watchpoint" gdb-stopping)
786 ("frame-begin" gdb-frame-begin)
787 ("stopped" gdb-stopped)
788 ) "An assoc mapping annotation tags to functions which process them.")
791 (setq gdb-flush-pending-output t)
792 (setq gud-running nil)
793 (setq gdb-output-sink 'user)
794 (setq gdb-input-queue nil)
795 (setq gdb-pending-triggers nil)
796 (setq gdb-prompting t))
798 (defconst gdb-source-spec-regexp
799 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:\\(0x[a-f0-9]*\\)")
801 ;; Do not use this except as an annotation handler.
802 (defun gdb-source (args)
803 (string-match gdb-source-spec-regexp args)
804 ;; Extract the frame position from the marker.
807 (match-string 1 args)
808 (string-to-int (match-string 2 args))))
809 (setq gdb-current-address (match-string 3 args))
810 ;; cover for auto-display output which comes *before*
811 ;; stopped annotation
812 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
814 (defun gdb-pre-prompt (ignored)
815 "An annotation handler for `pre-prompt'.
816 This terminates the collection of output from a previous command if that
817 happens to be in effect."
818 (let ((sink gdb-output-sink))
822 (setq gdb-output-sink 'post-emacs))
825 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
827 (defun gdb-prompt (ignored)
828 "An annotation handler for `prompt'.
829 This sends the next command (if any) to gdb."
830 (when gdb-first-prompt (gdb-ann3))
831 (let ((sink gdb-output-sink))
834 ((eq sink 'post-emacs)
835 (setq gdb-output-sink 'user)
837 (car (cdr gdb-current-item))))
838 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
842 (error "Phase error in gdb-prompt (got %s)" sink))))
843 (let ((input (gdb-dequeue-input)))
845 (gdb-send-item input)
847 (setq gdb-prompting t)
848 (gud-display-frame)))))
850 (defun gdb-subprompt (ignored)
851 "An annotation handler for non-top-level prompts."
852 (setq gdb-prompting t))
854 (defun gdb-starting (ignored)
855 "An annotation handler for `starting'.
856 This says that I/O for the subprocess is now the program being debugged,
858 (setq gdb-active-process t)
859 (let ((sink gdb-output-sink))
864 (if gdb-use-inferior-io-buffer
865 (setq gdb-output-sink 'inferior))))
868 (error "Unexpected `starting' annotation")))))
870 (defun gdb-stopping (ignored)
871 "An annotation handler for `breakpoint' and other annotations.
872 They say that I/O for the subprocess is now GDB, not the program
874 (if gdb-use-inferior-io-buffer
875 (let ((sink gdb-output-sink))
878 (setq gdb-output-sink 'user))
881 (error "Unexpected stopping annotation"))))))
883 (defun gdb-exited (ignored)
884 "An annotation handler for `exited' and `signalled'.
885 They say that I/O for the subprocess is now GDB, not the program
886 being debugged and that the program is no longer running. This
887 function is used to change the focus of GUD tooltips to #define
889 (setq gdb-active-process nil)
890 (gdb-stopping ignored))
893 (defun gdb-frame-begin (ignored)
894 (let ((sink gdb-output-sink))
897 (setq gdb-output-sink 'user))
902 (error "Unexpected frame-begin annotation (%S)" sink)))))
904 (defun gdb-stopped (ignored)
905 "An annotation handler for `stopped'.
906 It is just like `gdb-stopping', except that if we already set the output
907 sink to `user' in `gdb-stopping', that is fine."
908 (setq gud-running nil)
909 (let ((sink gdb-output-sink))
912 (setq gdb-output-sink 'user))
916 (error "Unexpected stopped annotation")))))
918 (defun gdb-post-prompt (ignored)
919 "An annotation handler for `post-prompt'.
920 This begins the collection of output from the current command if that
921 happens to be appropriate."
922 (unless gdb-pending-triggers
923 (gdb-get-current-frame)
924 (gdb-invalidate-frames)
925 (gdb-invalidate-breakpoints)
926 (gdb-invalidate-assembler)
927 (gdb-invalidate-registers)
928 (gdb-invalidate-memory)
929 (gdb-invalidate-locals)
930 (gdb-invalidate-threads)
931 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
932 ;; FIXME: with GDB-6 on Darwin, this might very well work.
933 ;; only needed/used with speedbar/watch expressions
934 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
935 (setq gdb-var-changed t) ; force update
936 (dolist (var gdb-var-list)
937 (setcar (nthcdr 5 var) nil))
939 (let ((sink gdb-output-sink))
942 ((eq sink 'pre-emacs)
943 (setq gdb-output-sink 'emacs))
946 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
948 (defun gud-gdba-marker-filter (string)
949 "A gud marker filter for gdb. Handle a burst of output from GDB."
950 (if gdb-flush-pending-output
952 (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
953 ;; Recall the left over gud-marker-acc from last time
954 (setq gud-marker-acc (concat gud-marker-acc string))
955 ;; Start accumulating output for the GUD buffer
958 ;; Process all the complete markers in this chunk.
959 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
960 (let ((annotation (match-string 1 gud-marker-acc)))
962 ;; Stuff prior to the match is just ordinary output.
963 ;; It is either concatenated to OUTPUT or directed
968 (substring gud-marker-acc 0 (match-beginning 0))))
970 ;; Take that stuff off the gud-marker-acc.
971 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
973 ;; Parse the tag from the annotation, and maybe its arguments.
974 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
975 (let* ((annotation-type (match-string 1 annotation))
976 (annotation-arguments (match-string 2 annotation))
977 (annotation-rule (assoc annotation-type
978 gdb-annotation-rules)))
979 ;; Call the handler for this annotation.
981 (funcall (car (cdr annotation-rule))
982 annotation-arguments)
983 ;; Else the annotation is not recognized. Ignore it silently,
984 ;; so that GDB can add new annotations without causing
988 ;; Does the remaining text end in a partial line?
989 ;; If it does, then keep part of the gud-marker-acc until we get more.
990 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
993 ;; Everything before the potential marker start can be output.
995 (gdb-concat-output output
996 (substring gud-marker-acc 0
997 (match-beginning 0))))
999 ;; Everything after, we save, to combine with later input.
1000 (setq gud-marker-acc (substring gud-marker-acc
1001 (match-beginning 0))))
1003 ;; In case we know the gud-marker-acc contains no partial annotations:
1005 (setq output (gdb-concat-output output gud-marker-acc))
1006 (setq gud-marker-acc "")))
1009 (defun gdb-concat-output (so-far new)
1010 (let ((sink gdb-output-sink))
1012 ((eq sink 'user) (concat so-far new))
1013 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1015 (gdb-append-to-partial-output new)
1017 ((eq sink 'inferior)
1018 (gdb-append-to-inferior-io new)
1022 (error "Bogon output sink %S" sink)))))
1024 (defun gdb-append-to-partial-output (string)
1025 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1026 (goto-char (point-max))
1029 (defun gdb-clear-partial-output ()
1030 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1033 (defun gdb-append-to-inferior-io (string)
1034 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1035 (goto-char (point-max))
1036 (insert-before-markers string))
1037 (if (not (string-equal string ""))
1038 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io))))
1040 (defun gdb-clear-inferior-io ()
1041 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1045 ;; One trick is to have a command who's output is always available in a buffer
1046 ;; of it's own, and is always up to date. We build several buffers of this
1049 ;; There are two aspects to this: gdb has to tell us when the output for that
1050 ;; command might have changed, and we have to be able to run the command
1051 ;; behind the user's back.
1053 ;; The output phasing associated with the variable gdb-output-sink
1054 ;; help us to run commands behind the user's back.
1056 ;; Below is the code for specificly managing buffers of output from one
1060 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1061 ;; It adds an input for the command we are tracking. It should be the
1062 ;; annotation rule binding of whatever gdb sends to tell us this command
1063 ;; might have changed it's output.
1065 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1066 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1067 ;; input in the input queue (see comment about ``gdb communications'' above).
1069 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1071 `(defun ,name (&optional ignored)
1072 (if (and (,demand-predicate)
1074 gdb-pending-triggers)))
1077 (list ,gdb-command ',output-handler))
1078 (push ',name gdb-pending-triggers)))))
1080 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1082 (setq gdb-pending-triggers
1084 gdb-pending-triggers))
1085 (let ((buf (gdb-get-buffer ',buf-key)))
1087 (with-current-buffer buf
1089 (buffer-read-only nil))
1091 (insert-buffer-substring (gdb-get-create-buffer
1092 'gdb-partial-output-buffer))
1094 ;; put customisation here
1097 (defmacro def-gdb-auto-updated-buffer (buffer-key
1098 trigger-name gdb-command
1099 output-handler-name custom-defun)
1101 (def-gdb-auto-update-trigger ,trigger-name
1102 ;; The demand predicate:
1103 (lambda () (gdb-get-buffer ',buffer-key))
1105 ,output-handler-name)
1106 (def-gdb-auto-update-handler ,output-handler-name
1107 ,trigger-name ,buffer-key ,custom-defun)))
1111 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1113 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1114 'gdb-breakpoints-buffer-name
1115 'gdb-breakpoints-mode)
1117 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1118 ;; This defines the auto update rule for buffers of type
1119 ;; `gdb-breakpoints-buffer'.
1121 ;; It defines a function to serve as the annotation handler that
1122 ;; handles the `foo-invalidated' message. That function is called:
1123 gdb-invalidate-breakpoints
1125 ;; To update the buffer, this command is sent to gdb.
1126 "server info breakpoints\n"
1128 ;; This also defines a function to be the handler for the output
1129 ;; from the command above. That function will copy the output into
1130 ;; the appropriately typed buffer. That function will be called:
1131 gdb-info-breakpoints-handler
1132 ;; buffer specific functions
1133 gdb-info-breakpoints-custom)
1135 (defconst breakpoint-xpm-data
1137 static char *magick[] = {
1138 /* columns rows colors chars-per-pixel */
1154 "XPM data used for breakpoint icon.")
1156 (defconst breakpoint-enabled-pbm-data
1159 0 0 0 0 1 1 1 1 0 0 0 0
1160 0 0 0 1 1 1 1 1 1 0 0 0
1161 0 0 1 1 1 1 1 1 1 1 0 0
1162 0 1 1 1 1 1 1 1 1 1 1 0
1163 0 1 1 1 1 1 1 1 1 1 1 0
1164 0 1 1 1 1 1 1 1 1 1 1 0
1165 0 1 1 1 1 1 1 1 1 1 1 0
1166 0 0 1 1 1 1 1 1 1 1 0 0
1167 0 0 0 1 1 1 1 1 1 0 0 0
1168 0 0 0 0 1 1 1 1 0 0 0 0"
1169 "PBM data used for enabled breakpoint icon.")
1171 (defconst breakpoint-disabled-pbm-data
1183 0 0 0 1 0 1 0 1 0 0"
1184 "PBM data used for disabled breakpoint icon.")
1186 (defvar breakpoint-enabled-icon nil
1187 "Icon for enabled breakpoint in display margin.")
1189 (defvar breakpoint-disabled-icon nil
1190 "Icon for disabled breakpoint in display margin.")
1192 ;; Bitmap for breakpoint in fringe
1193 (and (display-images-p)
1194 (define-fringe-bitmap 'breakpoint
1195 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1197 (defface breakpoint-enabled
1201 "Face for enabled breakpoint icon in fringe."
1203 ;; compatibility alias for old name
1204 (put 'breakpoint-enabled-bitmap-face 'face-alias 'breakpoint-enabled)
1206 (defface breakpoint-disabled
1207 ;; We use different values of grey for different background types,
1208 ;; so that on low-color displays it will end up as something visible
1209 ;; if it has to be approximated.
1210 '((((background dark)) :foreground "grey60")
1211 (((background light)) :foreground "grey40"))
1212 "Face for disabled breakpoint icon in fringe."
1214 ;; compatibility alias for old name
1215 (put 'breakpoint-disabled-bitmap-face 'face-alias 'breakpoint-disabled)
1217 ;;-put breakpoint icons in relevant margins (even those set in the GUD buffer)
1218 (defun gdb-info-breakpoints-custom ()
1219 (let ((flag) (bptno))
1220 ;; remove all breakpoint-icons in source buffers but not assembler buffer
1221 (dolist (buffer (buffer-list))
1222 (with-current-buffer buffer
1223 (if (and (eq gud-minor-mode 'gdba)
1224 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1225 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1226 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1228 (goto-char (point-min))
1229 (while (< (point) (- (point-max) 1))
1231 (if (looking-at "[^\t].*breakpoint")
1233 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1234 (setq bptno (match-string 1))
1235 (setq flag (char-after (match-beginning 2)))
1237 (if (re-search-forward " in .* at\\s-+" nil t)
1239 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1240 (let ((line (match-string 2)) (buffer-read-only nil)
1241 (file (match-string 1)))
1242 (add-text-properties (point-at-bol) (point-at-eol)
1243 '(mouse-face highlight
1244 help-echo "mouse-2, RET: visit breakpoint"))
1245 (unless (file-exists-p file)
1246 (setq file (cdr (assoc bptno gdb-location-alist))))
1247 (unless (string-equal file "File not found")
1249 (with-current-buffer (find-file-noselect file)
1250 (set (make-local-variable 'gud-minor-mode)
1252 (set (make-local-variable 'tool-bar-map)
1254 ;; only want one breakpoint icon at each
1257 (goto-line (string-to-number line))
1258 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1262 (match-string-no-properties 1) ":1\n")
1265 (list "info source\n"
1266 `(lambda () (gdb-get-location
1267 ,bptno ,line ,flag)))))))))))
1269 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1271 (defun gdb-mouse-set-clear-breakpoint (event)
1272 "Set/clear breakpoint in left fringe/margin with mouse click."
1274 (mouse-minibuffer-check event)
1275 (let ((posn (event-end event)))
1276 (if (numberp (posn-point posn))
1277 (with-selected-window (posn-window posn)
1279 (goto-char (posn-point posn))
1280 (if (or (posn-object posn)
1281 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1284 (gud-break nil)))))))
1286 (defun gdb-mouse-toggle-breakpoint (event)
1287 "Enable/disable breakpoint in left fringe/margin with mouse click."
1289 (mouse-minibuffer-check event)
1290 (let ((posn (event-end event)))
1291 (if (numberp (posn-point posn))
1292 (with-selected-window (posn-window posn)
1294 (goto-char (posn-point posn))
1298 ; (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1302 (let ((bptno (get-text-property
1303 0 'gdb-bptno (car (posn-string posn)))))
1305 (if (get-text-property
1306 0 'gdb-enabled (car (posn-string posn)))
1309 bptno "\n")) 'ignore))))))))
1311 (defun gdb-breakpoints-buffer-name ()
1312 (with-current-buffer gud-comint-buffer
1313 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1315 (defun gdb-display-breakpoints-buffer ()
1316 "Display status of user-settable breakpoints."
1319 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1321 (defconst gdb-frame-parameters
1322 '((height . 14) (width . 80)
1324 (tool-bar-lines . nil)
1325 (menu-bar-lines . nil)
1326 (minibuffer . nil)))
1328 (defun gdb-frame-breakpoints-buffer ()
1329 "Display status of user-settable breakpoints in a new frame."
1331 (let ((special-display-regexps (append special-display-regexps '(".*")))
1332 (special-display-frame-alist gdb-frame-parameters))
1333 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
1335 (defvar gdb-breakpoints-mode-map
1336 (let ((map (make-sparse-keymap))
1337 (menu (make-sparse-keymap "Breakpoints")))
1338 (define-key menu [quit] '("Quit" . kill-this-buffer))
1339 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1340 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1341 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1342 (suppress-keymap map)
1343 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1344 (define-key map " " 'gdb-toggle-breakpoint)
1345 (define-key map "d" 'gdb-delete-breakpoint)
1346 (define-key map "q" 'kill-this-buffer)
1347 (define-key map "\r" 'gdb-goto-breakpoint)
1348 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1349 (define-key map [follow-link] 'mouse-face)
1352 (defun gdb-breakpoints-mode ()
1353 "Major mode for gdb breakpoints.
1355 \\{gdb-breakpoints-mode-map}"
1356 (kill-all-local-variables)
1357 (setq major-mode 'gdb-breakpoints-mode)
1358 (setq mode-name "Breakpoints")
1359 (use-local-map gdb-breakpoints-mode-map)
1360 (setq buffer-read-only t)
1361 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1362 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1363 'gdb-invalidate-breakpoints
1364 'gdbmi-invalidate-breakpoints))
1366 (defun gdb-toggle-breakpoint ()
1367 "Enable/disable breakpoint at current line."
1370 (beginning-of-line 1)
1371 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1372 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1374 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1377 (concat gdb-server-prefix
1378 (if (eq ?y (char-after (match-beginning 2)))
1381 (match-string 1) "\n") 'ignore))
1382 (error "Not recognized as break/watchpoint line"))))
1384 (defun gdb-delete-breakpoint ()
1385 "Delete the breakpoint at current line."
1387 (beginning-of-line 1)
1388 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1389 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1391 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1394 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1395 (error "Not recognized as break/watchpoint line")))
1397 (defun gdb-goto-breakpoint (&optional event)
1398 "Display the breakpoint location specified at current line."
1399 (interactive (list last-input-event))
1400 (if event (mouse-set-point event))
1402 (beginning-of-line 1)
1403 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1404 (looking-at "\\([0-9]+\\) .* in .* at\\s-+\\(\\S-*\\):\\([0-9]+\\)")
1406 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\
1407 \\(\\S-*\\):\\([0-9]+\\)"))
1408 (let ((bptno (match-string 1))
1409 (file (match-string 2))
1410 (line (match-string 3)))
1411 (save-selected-window
1412 (let* ((buf (find-file-noselect
1413 (if (file-exists-p file) file
1414 (cdr (assoc bptno gdb-location-alist)))))
1415 (window (display-buffer buf)))
1416 (with-current-buffer buf
1417 (goto-line (string-to-number line))
1418 (set-window-point window (point))))))
1419 (error "Not recognized as break/watchpoint line"))))
1422 ;; Frames buffer. This displays a perpetually correct bactracktrace
1423 ;; (from the command `where').
1425 ;; Alas, if your stack is deep, it is costly.
1427 (gdb-set-buffer-rules 'gdb-stack-buffer
1428 'gdb-stack-buffer-name
1431 (def-gdb-auto-updated-buffer gdb-stack-buffer
1432 gdb-invalidate-frames
1434 gdb-info-frames-handler
1435 gdb-info-frames-custom)
1437 (defun gdb-info-frames-custom ()
1438 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1440 (let ((buffer-read-only nil))
1441 (goto-char (point-min))
1442 (while (< (point) (point-max))
1443 (add-text-properties (point-at-bol) (point-at-eol)
1444 '(mouse-face highlight
1445 help-echo "mouse-2, RET: Select frame"))
1447 (when (and (looking-at "^#\\([0-9]+\\)")
1448 (equal (match-string 1) gdb-current-stack-level))
1449 (put-text-property (point-at-bol) (point-at-eol)
1450 'face '(:inverse-video t)))
1451 (forward-line 1))))))
1453 (defun gdb-stack-buffer-name ()
1454 (with-current-buffer gud-comint-buffer
1455 (concat "*stack frames of " (gdb-get-target-string) "*")))
1457 (defun gdb-display-stack-buffer ()
1458 "Display backtrace of current stack."
1461 (gdb-get-create-buffer 'gdb-stack-buffer)))
1463 (defun gdb-frame-stack-buffer ()
1464 "Display backtrace of current stack in a new frame."
1466 (let ((special-display-regexps (append special-display-regexps '(".*")))
1467 (special-display-frame-alist gdb-frame-parameters))
1468 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1470 (defvar gdb-frames-mode-map
1471 (let ((map (make-sparse-keymap)))
1472 (suppress-keymap map)
1473 (define-key map "q" 'kill-this-buffer)
1474 (define-key map "\r" 'gdb-frames-select)
1475 (define-key map [mouse-2] 'gdb-frames-select)
1476 (define-key map [follow-link] 'mouse-face)
1479 (defun gdb-frames-mode ()
1480 "Major mode for gdb frames.
1482 \\{gdb-frames-mode-map}"
1483 (kill-all-local-variables)
1484 (setq major-mode 'gdb-frames-mode)
1485 (setq mode-name "Frames")
1486 (setq buffer-read-only t)
1487 (use-local-map gdb-frames-mode-map)
1489 (run-mode-hooks 'gdb-frames-mode-hook)
1490 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1491 'gdb-invalidate-frames
1492 'gdbmi-invalidate-frames))
1494 (defun gdb-get-frame-number ()
1496 (let* ((pos (re-search-backward "^#*\\([0-9]*\\)" nil t))
1497 (n (or (and pos (match-string-no-properties 1)) "0")))
1500 (defun gdb-frames-select (&optional event)
1501 "Select the frame and display the relevant source."
1502 (interactive (list last-input-event))
1503 (if event (mouse-set-point event))
1505 (list (concat gdb-server-prefix "frame "
1506 (gdb-get-frame-number) "\n") 'ignore))
1507 (gud-display-frame))
1510 ;; Threads buffer. This displays a selectable thread list.
1512 (gdb-set-buffer-rules 'gdb-threads-buffer
1513 'gdb-threads-buffer-name
1516 (def-gdb-auto-updated-buffer gdb-threads-buffer
1517 gdb-invalidate-threads
1518 (concat gdb-server-prefix "info threads\n")
1519 gdb-info-threads-handler
1520 gdb-info-threads-custom)
1522 (defun gdb-info-threads-custom ()
1523 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1524 (let ((buffer-read-only nil))
1525 (goto-char (point-min))
1526 (while (< (point) (point-max))
1527 (add-text-properties (point-at-bol) (point-at-eol)
1528 '(mouse-face highlight
1529 help-echo "mouse-2, RET: select thread"))
1530 (forward-line 1)))))
1532 (defun gdb-threads-buffer-name ()
1533 (with-current-buffer gud-comint-buffer
1534 (concat "*threads of " (gdb-get-target-string) "*")))
1536 (defun gdb-display-threads-buffer ()
1537 "Display IDs of currently known threads."
1540 (gdb-get-create-buffer 'gdb-threads-buffer)))
1542 (defun gdb-frame-threads-buffer ()
1543 "Display IDs of currently known threads in a new frame."
1545 (let ((special-display-regexps (append special-display-regexps '(".*")))
1546 (special-display-frame-alist gdb-frame-parameters))
1547 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1549 (defvar gdb-threads-mode-map
1550 (let ((map (make-sparse-keymap)))
1551 (suppress-keymap map)
1552 (define-key map "q" 'kill-this-buffer)
1553 (define-key map "\r" 'gdb-threads-select)
1554 (define-key map [mouse-2] 'gdb-threads-select)
1557 (defun gdb-threads-mode ()
1558 "Major mode for gdb frames.
1560 \\{gdb-threads-mode-map}"
1561 (kill-all-local-variables)
1562 (setq major-mode 'gdb-threads-mode)
1563 (setq mode-name "Threads")
1564 (setq buffer-read-only t)
1565 (use-local-map gdb-threads-mode-map)
1566 (run-mode-hooks 'gdb-threads-mode-hook)
1567 'gdb-invalidate-threads)
1569 (defun gdb-get-thread-number ()
1571 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1572 (match-string-no-properties 1)))
1574 (defun gdb-threads-select (&optional event)
1575 "Select the thread and display the relevant source."
1576 (interactive (list last-input-event))
1577 (if event (mouse-set-point event))
1579 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1580 (gud-display-frame))
1583 ;; Registers buffer.
1585 (gdb-set-buffer-rules 'gdb-registers-buffer
1586 'gdb-registers-buffer-name
1587 'gdb-registers-mode)
1589 (def-gdb-auto-updated-buffer gdb-registers-buffer
1590 gdb-invalidate-registers
1591 (concat gdb-server-prefix "info registers\n")
1592 gdb-info-registers-handler
1593 gdb-info-registers-custom)
1595 (defun gdb-info-registers-custom ())
1597 (defvar gdb-registers-mode-map
1598 (let ((map (make-sparse-keymap)))
1599 (suppress-keymap map)
1600 (define-key map "q" 'kill-this-buffer)
1603 (defun gdb-registers-mode ()
1604 "Major mode for gdb registers.
1606 \\{gdb-registers-mode-map}"
1607 (kill-all-local-variables)
1608 (setq major-mode 'gdb-registers-mode)
1609 (setq mode-name "Registers")
1610 (setq buffer-read-only t)
1611 (use-local-map gdb-registers-mode-map)
1612 (run-mode-hooks 'gdb-registers-mode-hook)
1613 'gdb-invalidate-registers)
1615 (defun gdb-registers-buffer-name ()
1616 (with-current-buffer gud-comint-buffer
1617 (concat "*registers of " (gdb-get-target-string) "*")))
1619 (defun gdb-display-registers-buffer ()
1620 "Display integer register contents."
1623 (gdb-get-create-buffer 'gdb-registers-buffer)))
1625 (defun gdb-frame-registers-buffer ()
1626 "Display integer register contents in a new frame."
1628 (let ((special-display-regexps (append special-display-regexps '(".*")))
1629 (special-display-frame-alist gdb-frame-parameters))
1630 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
1634 (defcustom gdb-memory-repeat-count 32
1635 "Number of data items in memory window."
1640 (defcustom gdb-memory-format "x"
1641 "Display format of data items in memory window."
1642 :type '(choice (const :tag "Hexadecimal" "x")
1643 (const :tag "Signed decimal" "d")
1644 (const :tag "Unsigned decimal" "u")
1645 (const :tag "Octal" "o")
1646 (const :tag "Binary" "t"))
1650 (defcustom gdb-memory-unit "w"
1651 "Unit size of data items in memory window."
1652 :type '(choice (const :tag "Byte" "b")
1653 (const :tag "Halfword" "h")
1654 (const :tag "Word" "w")
1655 (const :tag "Giant word" "g"))
1659 (gdb-set-buffer-rules 'gdb-memory-buffer
1660 'gdb-memory-buffer-name
1663 (def-gdb-auto-updated-buffer gdb-memory-buffer
1664 gdb-invalidate-memory
1665 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
1666 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
1667 gdb-read-memory-handler
1668 gdb-read-memory-custom)
1670 (defun gdb-read-memory-custom ())
1672 (defvar gdb-memory-mode-map
1673 (let ((map (make-sparse-keymap)))
1674 (suppress-keymap map)
1675 (define-key map "q" 'kill-this-buffer)
1678 (defun gdb-memory-set-address (event)
1679 "Set the start memory address."
1681 (save-selected-window
1682 (select-window (posn-window (event-start event)))
1683 (let ((arg (read-from-minibuffer "Memory address: ")))
1684 (setq gdb-memory-address arg))
1685 (gdb-invalidate-memory)))
1687 (defun gdb-memory-set-repeat-count (event)
1688 "Set the number of data items in memory window."
1690 (save-selected-window
1691 (select-window (posn-window (event-start event)))
1692 (let* ((arg (read-from-minibuffer "Repeat count: "))
1693 (count (string-to-int arg)))
1695 (error "Non-negative numbers only")
1696 (customize-set-variable 'gdb-memory-repeat-count count)
1697 (gdb-invalidate-memory)))))
1699 (defun gdb-memory-format-binary ()
1700 "Set the display format to binary."
1702 (customize-set-variable 'gdb-memory-format "t")
1703 (gdb-invalidate-memory))
1705 (defun gdb-memory-format-octal ()
1706 "Set the display format to octal."
1708 (customize-set-variable 'gdb-memory-format "o")
1709 (gdb-invalidate-memory))
1711 (defun gdb-memory-format-unsigned ()
1712 "Set the display format to unsigned decimal."
1714 (customize-set-variable 'gdb-memory-format "u")
1715 (gdb-invalidate-memory))
1717 (defun gdb-memory-format-signed ()
1718 "Set the display format to decimal."
1720 (customize-set-variable 'gdb-memory-format "d")
1721 (gdb-invalidate-memory))
1723 (defun gdb-memory-format-hexadecimal ()
1724 "Set the display format to hexadecimal."
1726 (customize-set-variable 'gdb-memory-format "x")
1727 (gdb-invalidate-memory))
1729 (defvar gdb-memory-format-keymap
1730 (let ((map (make-sparse-keymap)))
1731 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
1733 "Keymap to select format in the header line.")
1735 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
1736 "Menu of display formats in the header line.")
1738 (define-key gdb-memory-format-menu [binary]
1739 '(menu-item "Binary" gdb-memory-format-binary
1740 :button (:radio . (equal gdb-memory-format "t"))))
1741 (define-key gdb-memory-format-menu [octal]
1742 '(menu-item "Octal" gdb-memory-format-octal
1743 :button (:radio . (equal gdb-memory-format "o"))))
1744 (define-key gdb-memory-format-menu [unsigned]
1745 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
1746 :button (:radio . (equal gdb-memory-format "u"))))
1747 (define-key gdb-memory-format-menu [signed]
1748 '(menu-item "Signed Decimal" gdb-memory-format-signed
1749 :button (:radio . (equal gdb-memory-format "d"))))
1750 (define-key gdb-memory-format-menu [hexadecimal]
1751 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
1752 :button (:radio . (equal gdb-memory-format "x"))))
1754 (defun gdb-memory-format-menu (event)
1756 (x-popup-menu event gdb-memory-format-menu))
1758 (defun gdb-memory-format-menu-1 (event)
1760 (save-selected-window
1761 (select-window (posn-window (event-start event)))
1762 (let* ((selection (gdb-memory-format-menu event))
1763 (binding (and selection (lookup-key gdb-memory-format-menu
1764 (vector (car selection))))))
1765 (if binding (call-interactively binding)))))
1767 (defun gdb-memory-unit-giant ()
1768 "Set the unit size to giant words (eight bytes)."
1770 (customize-set-variable 'gdb-memory-unit "g")
1771 (gdb-invalidate-memory))
1773 (defun gdb-memory-unit-word ()
1774 "Set the unit size to words (four bytes)."
1776 (customize-set-variable 'gdb-memory-unit "w")
1777 (gdb-invalidate-memory))
1779 (defun gdb-memory-unit-halfword ()
1780 "Set the unit size to halfwords (two bytes)."
1782 (customize-set-variable 'gdb-memory-unit "h")
1783 (gdb-invalidate-memory))
1785 (defun gdb-memory-unit-byte ()
1786 "Set the unit size to bytes."
1788 (customize-set-variable 'gdb-memory-unit "b")
1789 (gdb-invalidate-memory))
1791 (defvar gdb-memory-unit-keymap
1792 (let ((map (make-sparse-keymap)))
1793 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
1795 "Keymap to select units in the header line.")
1797 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
1798 "Menu of units in the header line.")
1800 (define-key gdb-memory-unit-menu [giantwords]
1801 '(menu-item "Giant words" gdb-memory-unit-giant
1802 :button (:radio . (equal gdb-memory-unit "g"))))
1803 (define-key gdb-memory-unit-menu [words]
1804 '(menu-item "Words" gdb-memory-unit-word
1805 :button (:radio . (equal gdb-memory-unit "w"))))
1806 (define-key gdb-memory-unit-menu [halfwords]
1807 '(menu-item "Halfwords" gdb-memory-unit-halfword
1808 :button (:radio . (equal gdb-memory-unit "h"))))
1809 (define-key gdb-memory-unit-menu [bytes]
1810 '(menu-item "Bytes" gdb-memory-unit-byte
1811 :button (:radio . (equal gdb-memory-unit "b"))))
1813 (defun gdb-memory-unit-menu (event)
1815 (x-popup-menu event gdb-memory-unit-menu))
1817 (defun gdb-memory-unit-menu-1 (event)
1819 (save-selected-window
1820 (select-window (posn-window (event-start event)))
1821 (let* ((selection (gdb-memory-unit-menu event))
1822 (binding (and selection (lookup-key gdb-memory-unit-menu
1823 (vector (car selection))))))
1824 (if binding (call-interactively binding)))))
1826 ;;from make-mode-line-mouse-map
1827 (defun gdb-make-header-line-mouse-map (mouse function) "\
1828 Return a keymap with single entry for mouse key MOUSE on the header line.
1829 MOUSE is defined to run function FUNCTION with no args in the buffer
1830 corresponding to the mode line clicked."
1831 (let ((map (make-sparse-keymap)))
1832 (define-key map (vector 'header-line mouse) function)
1833 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1836 (defun gdb-memory-mode ()
1837 "Major mode for examining memory.
1839 \\{gdb-memory-mode-map}"
1840 (kill-all-local-variables)
1841 (setq major-mode 'gdb-memory-mode)
1842 (setq mode-name "Memory")
1843 (setq buffer-read-only t)
1844 (use-local-map gdb-memory-mode-map)
1845 (setq header-line-format
1849 (propertize gdb-memory-address
1850 'face font-lock-warning-face
1851 'help-echo "mouse-1: Set memory address"
1852 'local-map (gdb-make-header-line-mouse-map
1854 #'gdb-memory-set-address))
1856 (propertize (number-to-string gdb-memory-repeat-count)
1857 'face font-lock-warning-face
1858 'help-echo "mouse-1: Set repeat count"
1859 'local-map (gdb-make-header-line-mouse-map
1861 #'gdb-memory-set-repeat-count))
1863 (propertize gdb-memory-format
1864 'face font-lock-warning-face
1865 'help-echo "mouse-3: Select display format"
1866 'local-map gdb-memory-format-keymap)
1868 (propertize gdb-memory-unit
1869 'face font-lock-warning-face
1870 'help-echo "mouse-3: Select unit size"
1871 'local-map gdb-memory-unit-keymap))))
1872 (run-mode-hooks 'gdb-memory-mode-hook)
1873 'gdb-invalidate-memory)
1875 (defun gdb-memory-buffer-name ()
1876 (with-current-buffer gud-comint-buffer
1877 (concat "*memory of " (gdb-get-target-string) "*")))
1879 (defun gdb-display-memory-buffer ()
1880 "Display memory contents."
1883 (gdb-get-create-buffer 'gdb-memory-buffer)))
1885 (defun gdb-frame-memory-buffer ()
1886 "Display memory contents in a new frame."
1888 (let ((special-display-regexps (append special-display-regexps '(".*")))
1889 (special-display-frame-alist gdb-frame-parameters))
1890 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
1895 (gdb-set-buffer-rules 'gdb-locals-buffer
1896 'gdb-locals-buffer-name
1899 (def-gdb-auto-updated-buffer gdb-locals-buffer
1900 gdb-invalidate-locals
1901 "server info locals\n"
1902 gdb-info-locals-handler
1903 gdb-info-locals-custom)
1905 ;; Abbreviate for arrays and structures.
1906 ;; These can be expanded using gud-display.
1907 (defun gdb-info-locals-handler nil
1908 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
1909 gdb-pending-triggers))
1910 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
1911 (with-current-buffer buf
1912 (goto-char (point-min))
1913 (while (re-search-forward "^ .*\n" nil t)
1914 (replace-match "" nil nil))
1915 (goto-char (point-min))
1916 (while (re-search-forward "{[-0-9, {}\]*\n" nil t)
1917 (replace-match "(array);\n" nil nil))
1918 (goto-char (point-min))
1919 (while (re-search-forward "{.*=.*\n" nil t)
1920 (replace-match "(structure);\n" nil nil))))
1921 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
1922 (and buf (with-current-buffer buf
1924 (buffer-read-only nil))
1925 (delete-region (point-min) (point-max))
1926 (insert-buffer-substring (gdb-get-create-buffer
1927 'gdb-partial-output-buffer))
1929 (run-hooks 'gdb-info-locals-hook))
1931 (defun gdb-info-locals-custom ()
1934 (defvar gdb-locals-mode-map
1935 (let ((map (make-sparse-keymap)))
1936 (suppress-keymap map)
1937 (define-key map "q" 'kill-this-buffer)
1940 (defun gdb-locals-mode ()
1941 "Major mode for gdb locals.
1943 \\{gdb-locals-mode-map}"
1944 (kill-all-local-variables)
1945 (setq major-mode 'gdb-locals-mode)
1946 (setq mode-name (concat "Locals:" gdb-current-frame))
1947 (setq buffer-read-only t)
1948 (use-local-map gdb-locals-mode-map)
1949 (run-mode-hooks 'gdb-locals-mode-hook)
1950 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1951 'gdb-invalidate-locals
1952 'gdbmi-invalidate-locals))
1954 (defun gdb-locals-buffer-name ()
1955 (with-current-buffer gud-comint-buffer
1956 (concat "*locals of " (gdb-get-target-string) "*")))
1958 (defun gdb-display-locals-buffer ()
1959 "Display local variables of current stack and their values."
1962 (gdb-get-create-buffer 'gdb-locals-buffer)))
1964 (defun gdb-frame-locals-buffer ()
1965 "Display local variables of current stack and their values in a new frame."
1967 (let ((special-display-regexps (append special-display-regexps '(".*")))
1968 (special-display-frame-alist gdb-frame-parameters))
1969 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
1972 ;;;; Window management
1973 (defun gdb-display-buffer (buf &optional size)
1974 (let ((answer (get-buffer-window buf 0))
1977 (display-buffer buf nil 0) ;Raise the frame if necessary.
1978 ;; The buffer is not yet displayed.
1979 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
1980 (let ((window (get-lru-window)))
1982 (not (eq window (get-buffer-window gud-comint-buffer))))
1984 (set-window-buffer window buf)
1985 (setq answer window))
1986 (setq must-split t)))
1988 (let* ((largest (get-largest-window))
1989 (cur-size (window-height largest))
1990 (new-size (and size (< size cur-size) (- cur-size size))))
1991 (setq answer (split-window largest new-size))
1992 (set-window-buffer answer buf)
1993 (set-window-dedicated-p answer t)))
1997 ;;; Shared keymap initialization:
1999 (let ((menu (make-sparse-keymap "GDB-Windows")))
2000 (define-key gud-menu-map [displays]
2001 `(menu-item "GDB-Windows" ,menu :visible (eq gud-minor-mode 'gdba)))
2002 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2003 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2004 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2005 (define-key menu [assembler] '("Machine" . gdb-display-assembler-buffer))
2006 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2007 (define-key menu [inferior]
2008 '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
2009 :enable gdb-use-inferior-io-buffer))
2010 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2011 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2012 (define-key menu [breakpoints]
2013 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2015 (let ((menu (make-sparse-keymap "GDB-Frames")))
2016 (define-key gud-menu-map [frames]
2017 `(menu-item "GDB-Frames" ,menu :visible (eq gud-minor-mode 'gdba)))
2018 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2019 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2020 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2021 (define-key menu [assembler] '("Machine" . gdb-frame-assembler-buffer))
2022 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2023 (define-key menu [inferior]
2024 '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
2025 :enable gdb-use-inferior-io-buffer))
2026 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2027 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2028 (define-key menu [breakpoints]
2029 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2031 (let ((menu (make-sparse-keymap "GDB-UI")))
2032 (define-key gud-menu-map [ui]
2033 `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
2034 (define-key menu [gdb-restore-windows]
2035 '(menu-item "Restore Window Layout" gdb-restore-windows
2036 :help "Restore standard layout for debug session."))
2037 (define-key menu [gdb-many-windows]
2038 '(menu-item "Display Other Windows" gdb-many-windows
2039 :help "Toggle display of locals, stack and breakpoint information"
2040 :button (:toggle . gdb-many-windows)))
2041 (define-key menu [gdb-use-inferior-io]
2042 (menu-bar-make-toggle toggle-gdb-use-inferior-io-buffer
2043 gdb-use-inferior-io-buffer
2044 "Separate inferior IO" "Use separate IO %s"
2045 "Toggle separate IO for inferior.")))
2047 (defadvice toggle-gdb-use-inferior-io-buffer (after gdb-kill-io-buffer activate)
2048 (unless gdb-use-inferior-io-buffer
2049 (kill-buffer (gdb-inferior-io-name))))
2051 (defun gdb-frame-gdb-buffer ()
2052 "Display GUD buffer in a new frame."
2054 (select-frame (make-frame gdb-frame-parameters))
2055 (switch-to-buffer (gdb-get-create-buffer 'gdba))
2056 (set-window-dedicated-p (selected-window) t))
2058 (defun gdb-display-gdb-buffer ()
2059 "Display GUD buffer."
2062 (gdb-get-create-buffer 'gdba)))
2064 (defvar gdb-main-file nil "Source file from which program execution begins.")
2066 (defcustom gdb-show-main nil
2067 "Nil means don't display source file containing the main routine."
2072 (defun gdb-set-window-buffer (name)
2073 (set-window-buffer (selected-window) (get-buffer name))
2074 (set-window-dedicated-p (selected-window) t))
2076 (defun gdb-setup-windows ()
2077 "Layout the window pattern for gdb-many-windows."
2078 (gdb-display-locals-buffer)
2079 (gdb-display-stack-buffer)
2080 (delete-other-windows)
2081 (gdb-display-breakpoints-buffer)
2082 (delete-other-windows)
2084 (pop-to-buffer gud-comint-buffer)
2085 (split-window nil ( / ( * (window-height) 3) 4))
2086 (split-window nil ( / (window-height) 3))
2087 (split-window-horizontally)
2089 (gdb-set-window-buffer (gdb-locals-buffer-name))
2092 (if gud-last-last-frame
2093 (gud-find-file (car gud-last-last-frame))
2094 (gud-find-file gdb-main-file)))
2095 (when gdb-use-inferior-io-buffer
2096 (split-window-horizontally)
2098 (gdb-set-window-buffer
2099 (gdb-get-create-buffer 'gdb-inferior-io)))
2101 (gdb-set-window-buffer (gdb-stack-buffer-name))
2102 (split-window-horizontally)
2104 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2107 (defcustom gdb-many-windows nil
2108 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2109 In this case it starts with two windows: one displaying the GUD
2110 buffer and the other with the source file with the main routine
2111 of the inferior. Non-nil means display the layout shown for
2117 (defun gdb-many-windows (arg)
2118 "Toggle the number of windows in the basic arrangement."
2120 (setq gdb-many-windows
2122 (not gdb-many-windows)
2123 (> (prefix-numeric-value arg) 0)))
2125 (gdb-restore-windows)
2128 (defun gdb-restore-windows ()
2129 "Restore the basic arrangement of windows used by gdba.
2130 This arrangement depends on the value of `gdb-many-windows'."
2132 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2133 (delete-other-windows)
2134 (if gdb-many-windows
2139 (if gud-last-last-frame
2140 (gud-find-file (car gud-last-last-frame))
2141 (gud-find-file gdb-main-file)))
2145 "Exit a debugging session cleanly.
2146 Kills the gdb buffers and resets the source buffers."
2147 (dolist (buffer (buffer-list))
2148 (unless (eq buffer gud-comint-buffer)
2149 (with-current-buffer buffer
2150 (if (memq gud-minor-mode '(gdbmi gdba))
2151 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2153 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2154 (setq gud-minor-mode nil)
2155 (kill-local-variable 'tool-bar-map)
2156 (setq gud-running nil)
2157 (setq gdb-active-process nil))))))
2158 (when (markerp gdb-overlay-arrow-position)
2159 (move-marker gdb-overlay-arrow-position nil)
2160 (setq gdb-overlay-arrow-position nil))
2161 (setq overlay-arrow-variable-list
2162 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list)))
2164 (defun gdb-source-info ()
2165 "Find the source file where the program starts and displays it with related
2167 (goto-char (point-min))
2168 (if (and (search-forward "Located in " nil t)
2169 (looking-at "\\S-*"))
2170 (setq gdb-main-file (match-string 0)))
2171 (if gdb-many-windows
2173 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2175 (let ((pop-up-windows t))
2176 (display-buffer (gud-find-file gdb-main-file))))))
2178 (defun gdb-get-location (bptno line flag)
2179 "Find the directory containing the relevant source file.
2180 Put in buffer and place breakpoint icon."
2181 (goto-char (point-min))
2182 (catch 'file-not-found
2183 (if (search-forward "Located in " nil t)
2184 (if (looking-at "\\S-*")
2185 (push (cons bptno (match-string 0)) gdb-location-alist))
2187 (push (cons bptno "File not found") gdb-location-alist)
2188 (message-box "Cannot find source file for breakpoint location.\n\
2189 Add directory to search path for source files using the GDB command, dir.")
2190 (throw 'file-not-found nil))
2191 (with-current-buffer
2192 (find-file-noselect (match-string 0))
2193 (save-current-buffer
2194 (set (make-local-variable 'gud-minor-mode) 'gdba)
2195 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2196 ;; only want one breakpoint icon at each location
2198 (goto-line (string-to-number line))
2199 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2201 (add-hook 'find-file-hook 'gdb-find-file-hook)
2203 (defun gdb-find-file-hook ()
2204 (if (and (not gdb-find-file-unhook)
2205 ;; in case gud or gdb-ui is just loaded
2207 (buffer-name gud-comint-buffer)
2208 (with-current-buffer gud-comint-buffer
2209 (eq gud-minor-mode 'gdba)))
2212 (list (concat gdb-server-prefix "list "
2213 (file-name-nondirectory buffer-file-name)
2215 `(lambda () (gdb-set-gud-minor-mode ,(current-buffer)))))
2216 (error (setq gdb-find-file-unhook t)))))
2219 (defun gdb-put-string (putstring pos &optional dprop)
2220 "Put string PUTSTRING in front of POS in the current buffer.
2221 PUTSTRING is displayed by putting an overlay into the current buffer with a
2222 `before-string' STRING that has a `display' property whose value is
2224 (let ((string (make-string 1 ?x))
2225 (buffer (current-buffer)))
2226 (setq putstring (copy-sequence putstring))
2227 (let ((overlay (make-overlay pos pos buffer))
2229 (list (list 'margin 'left-margin) putstring))))
2230 (put-text-property 0 (length string) 'display prop string)
2231 (overlay-put overlay 'put-break t)
2232 (overlay-put overlay 'before-string string))))
2234 ;;from remove-images
2235 (defun gdb-remove-strings (start end &optional buffer)
2236 "Remove strings between START and END in BUFFER.
2237 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2238 BUFFER nil or omitted means use the current buffer."
2240 (setq buffer (current-buffer)))
2241 (dolist (overlay (overlays-in start end))
2242 (when (overlay-get overlay 'put-break)
2243 (delete-overlay overlay))))
2245 (defun gdb-put-breakpoint-icon (enabled bptno)
2246 (let ((start (progn (beginning-of-line) (- (point) 1)))
2247 (end (progn (end-of-line) (+ (point) 1)))
2248 (putstring (if enabled "B" "b")))
2249 (add-text-properties
2250 0 1 '(help-echo "mouse-1: set/clear bkpt, mouse-3: enable/disable bkpt")
2252 (if enabled (add-text-properties
2253 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2254 (add-text-properties
2255 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2256 (gdb-remove-breakpoint-icons start end)
2257 (if (display-images-p)
2258 (if (>= (car (window-fringes)) 8)
2261 `(left-fringe breakpoint
2264 'breakpoint-disabled)))
2265 (when (< left-margin-width 2)
2266 (save-current-buffer
2267 (setq left-margin-width 2)
2268 (if (get-buffer-window (current-buffer) 0)
2270 (get-buffer-window (current-buffer) 0)
2271 left-margin-width right-margin-width))))
2274 (or breakpoint-enabled-icon
2275 (setq breakpoint-enabled-icon
2276 (find-image `((:type xpm :data
2277 ,breakpoint-xpm-data
2278 :ascent 100 :pointer hand)
2280 ,breakpoint-enabled-pbm-data
2281 :ascent 100 :pointer hand)))))
2282 (or breakpoint-disabled-icon
2283 (setq breakpoint-disabled-icon
2284 (find-image `((:type xpm :data
2285 ,breakpoint-xpm-data
2286 :conversion disabled
2289 ,breakpoint-disabled-pbm-data
2294 (when (< left-margin-width 2)
2295 (save-current-buffer
2296 (setq left-margin-width 2)
2297 (if (get-buffer-window (current-buffer) 0)
2299 (get-buffer-window (current-buffer) 0)
2300 left-margin-width right-margin-width))))
2302 (propertize putstring
2303 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2306 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2307 (gdb-remove-strings start end)
2308 (if (display-images-p)
2309 (remove-images start end))
2311 (setq left-margin-width 0)
2312 (if (get-buffer-window (current-buffer) 0)
2314 (get-buffer-window (current-buffer) 0)
2315 left-margin-width right-margin-width))))
2319 ;; Assembler buffer.
2321 (gdb-set-buffer-rules 'gdb-assembler-buffer
2322 'gdb-assembler-buffer-name
2323 'gdb-assembler-mode)
2325 (def-gdb-auto-updated-buffer gdb-assembler-buffer
2326 gdb-invalidate-assembler
2327 (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
2328 gdb-assembler-handler
2329 gdb-assembler-custom)
2331 (defun gdb-assembler-custom ()
2332 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2333 (pos 1) (address) (flag) (bptno))
2334 (with-current-buffer buffer
2335 (if (not (equal gdb-current-address "main"))
2337 (goto-char (point-min))
2338 (if (re-search-forward gdb-current-address nil t)
2342 (or gdb-overlay-arrow-position
2343 (setq gdb-overlay-arrow-position (make-marker)))
2344 (set-marker gdb-overlay-arrow-position
2345 (point) (current-buffer))))))
2346 ;; remove all breakpoint-icons in assembler buffer before updating.
2347 (gdb-remove-breakpoint-icons (point-min) (point-max)))
2348 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2349 (goto-char (point-min))
2350 (while (< (point) (- (point-max) 1))
2352 (if (looking-at "[^\t].*breakpoint")
2355 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x\\(\\S-+\\)")
2356 (setq bptno (match-string 1))
2357 (setq flag (char-after (match-beginning 2)))
2358 (setq address (match-string 3))
2359 ;; remove leading 0s from output of info break.
2360 (if (string-match "^0+\\(.*\\)" address)
2361 (setq address (match-string 1 address)))
2362 (with-current-buffer buffer
2363 (goto-char (point-min))
2364 (if (re-search-forward address nil t)
2365 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
2366 (if (not (equal gdb-current-address "main"))
2367 (set-window-point (get-buffer-window buffer 0) pos))))
2369 (defvar gdb-assembler-mode-map
2370 (let ((map (make-sparse-keymap)))
2371 (suppress-keymap map)
2372 (define-key map "q" 'kill-this-buffer)
2375 (defvar gdb-assembler-font-lock-keywords
2376 '(;; <__function.name+n>
2377 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2378 (1 font-lock-function-name-face))
2379 ;; 0xNNNNNNNN <__function.name+n>: opcode
2380 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2381 (4 font-lock-keyword-face))
2382 ;; %register(at least i386)
2383 ("%\\sw+" . font-lock-variable-name-face)
2384 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2385 (1 font-lock-comment-face)
2386 (2 font-lock-function-name-face))
2387 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2388 "Font lock keywords used in `gdb-assembler-mode'.")
2390 (defun gdb-assembler-mode ()
2391 "Major mode for viewing code assembler.
2393 \\{gdb-assembler-mode-map}"
2394 (kill-all-local-variables)
2395 (setq major-mode 'gdb-assembler-mode)
2396 (setq mode-name "Machine")
2397 (setq gdb-overlay-arrow-position nil)
2398 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2399 (setq fringes-outside-margins t)
2400 (setq buffer-read-only t)
2401 (use-local-map gdb-assembler-mode-map)
2402 (gdb-invalidate-assembler)
2403 (set (make-local-variable 'font-lock-defaults)
2404 '(gdb-assembler-font-lock-keywords))
2405 (run-mode-hooks 'gdb-assembler-mode-hook)
2406 'gdb-invalidate-assembler)
2408 (defun gdb-assembler-buffer-name ()
2409 (with-current-buffer gud-comint-buffer
2410 (concat "*Machine Code " (gdb-get-target-string) "*")))
2412 (defun gdb-display-assembler-buffer ()
2413 "Display disassembly view."
2416 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2418 (defun gdb-frame-assembler-buffer ()
2419 "Display disassembly view in a new frame."
2421 (let ((special-display-regexps (append special-display-regexps '(".*")))
2422 (special-display-frame-alist gdb-frame-parameters))
2423 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2425 ;; modified because if gdb-current-address has changed value a new command
2426 ;; must be enqueued to update the buffer with the new output
2427 (defun gdb-invalidate-assembler (&optional ignored)
2428 (if (gdb-get-buffer 'gdb-assembler-buffer)
2430 (unless (string-equal gdb-current-frame gdb-previous-frame)
2431 (if (or (not (member 'gdb-invalidate-assembler
2432 gdb-pending-triggers))
2433 (not (string-equal gdb-current-address
2434 gdb-previous-address)))
2436 ;; take previous disassemble command off the queue
2437 (with-current-buffer gud-comint-buffer
2438 (let ((queue gdb-input-queue))
2439 (dolist (item queue)
2440 (if (equal (cdr item) '(gdb-assembler-handler))
2441 (setq gdb-input-queue
2442 (delete item gdb-input-queue))))))
2444 (list (concat gdb-server-prefix "disassemble "
2445 gdb-current-address "\n")
2446 'gdb-assembler-handler))
2447 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2448 (setq gdb-previous-address gdb-current-address)
2449 (setq gdb-previous-frame gdb-current-frame)))))))
2451 (defun gdb-get-current-frame ()
2452 (if (not (member 'gdb-get-current-frame gdb-pending-triggers))
2455 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2456 (push 'gdb-get-current-frame
2457 gdb-pending-triggers))))
2459 (defun gdb-frame-handler ()
2460 (setq gdb-pending-triggers
2461 (delq 'gdb-get-current-frame gdb-pending-triggers))
2462 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2463 (goto-char (point-min))
2464 (if (looking-at "Stack level \\([0-9]+\\)")
2465 (setq gdb-current-stack-level (match-string 1)))
2467 (if (looking-at ".*=\\s-+0x\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? ")
2469 (setq gdb-current-frame (match-string 2))
2470 (if (gdb-get-buffer 'gdb-locals-buffer)
2471 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2472 (setq mode-name (concat "Locals:" gdb-current-frame))))
2473 (let ((address (match-string 1)))
2474 ;; remove leading 0s from output of info frame command.
2475 (if (string-match "^0+\\(.*\\)" address)
2476 (setq gdb-current-address
2477 (concat "0x" (match-string 1 address)))
2478 (setq gdb-current-address (concat "0x" address))))
2479 (if (not (re-search-forward "(\\S-*:[0-9]*);" nil t))
2480 ;;update with new frame for machine code if necessary
2481 (gdb-invalidate-assembler)))))
2482 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2483 (setq gdb-current-language (match-string 1))))
2487 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2488 ;;; gdb-ui.el ends here