1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>,
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2007, 2008, 2009 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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file was written by by Nick Roberts following the general design
27 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It is currently being developed
28 ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
29 ;; of Code 2009 Project "Emacs GDB/MI migration".
33 ;; This mode acts as a graphical user interface to GDB. You can interact with
34 ;; GDB through the GUD buffer in the usual way, but there are also further
35 ;; buffers which control the execution and describe the state of your program.
36 ;; It separates the input/output of your program from that of GDB and displays
37 ;; expressions and their current values in their own buffers. It also uses
38 ;; features of Emacs 21 such as the fringe/display margin for breakpoints, and
39 ;; the toolbar (see the GDB Graphical Interface section in the Emacs info
42 ;; M-x gdb will start the debugger.
44 ;; This file uses GDB/MI as the primary interface to GDB. It is still under
45 ;; development and is part of a process to migrate Emacs from annotations (as
46 ;; used in gdb-ui.el) to GDB/MI. It runs gdb with GDB/MI (-interp=mi) and
47 ;; access CLI using "-interpreter-exec console cli-command". This code works
48 ;; without gdb-ui.el and uses MI tokens instead of queues. Eventually MI
49 ;; should be asynchronous.
51 ;; This mode will PARTLY WORK WITH RECENT GDB RELEASES (status in modeline
52 ;; doesn't update properly when execution commands are issued from GUD buffer)
53 ;; and WORKS BEST when GDB runs asynchronously: maint set linux-async on.
55 ;; You need development version of GDB 7.0 for the thread buffer to work.
57 ;; This file replaces gdb-ui.el and is for development with GDB. Use the
58 ;; release branch of Emacs 22 for the latest version of gdb-ui.el.
62 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
63 ;; explicitly in your program if you want timely display of I/O in Emacs.
64 ;; Alternatively you can make the output stream unbuffered, for example, by
68 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
71 ;; and compiling with -DUNBUFFERED while debugging.
73 ;; If you are using Cygwin GDB and find that the source is not being displayed
74 ;; in Emacs when you step through it, possible solutions are to:
76 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
77 ;; (Since 22.1 Emacs builds under Cygwin.)
78 ;; 2) Use MinGW GDB instead.
79 ;; 3) Use cygwin-mount.el
83 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
84 ;; some changes to the version that they include as part of Mac OSX.
85 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
86 ;; as earlier versions don not compile on Mac OSX.
90 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
91 ;; line information, e.g., a routine in libc (just a TODO item).
94 ;; 1) Use MI command -data-read-memory for memory window.
95 ;; 2) Watch windows to work with threads.
96 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
97 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
105 (defvar tool-bar-map)
106 (defvar speedbar-initial-expansion-list-name)
108 (defvar gdb-pc-address nil "Initialization for Assembler buffer.
109 Set to \"main\" at start if `gdb-show-main' is t.")
110 (defvar gdb-selected-frame nil)
111 (defvar gdb-selected-file nil)
112 (defvar gdb-selected-line nil)
113 (defvar gdb-frame-number nil)
114 (defvar gdb-current-language nil)
115 (defvar gdb-var-list nil
116 "List of variables in watch window.
117 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS) where
118 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
119 (defvar gdb-main-file nil "Source file from which program execution begins.")
120 (defvar gdb-overlay-arrow-position nil)
121 (defvar gdb-stack-position nil)
122 (defvar gdb-location-alist nil
123 "Alist of breakpoint numbers and full filenames. Only used for files that
125 (defvar gdb-active-process nil
126 "GUD tooltips display variable values when t, and macro definitions otherwise.")
127 (defvar gdb-error "Non-nil when GDB is reporting an error.")
128 (defvar gdb-macro-info nil
129 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
130 (defvar gdb-register-names nil "List of register names.")
131 (defvar gdb-changed-registers nil
132 "List of changed register numbers (strings).")
133 (defvar gdb-buffer-fringe-width nil)
134 (defvar gdb-last-command nil)
135 (defvar gdb-prompt-name nil)
136 (defvar gdb-token-number 0)
137 (defvar gdb-handler-alist '())
138 (defvar gdb-handler-number nil)
139 (defvar gdb-source-file-list nil
140 "List of source files for the current executable.")
141 (defvar gdb-first-done-or-error t)
142 (defvar gdb-source-window nil)
143 (defvar gdb-inferior-status nil)
144 (defvar gdb-continuation nil)
145 (defvar gdb-filter-output nil
146 "Message to be shown in GUD console.
148 This variable is updated in `gdb-done-or-error' and returned by
149 `gud-gdbmi-marker-filter'.")
151 (defvar gdb-buffer-type nil
152 "One of the symbols bound in `gdb-buffer-rules'.")
153 (make-variable-buffer-local 'gdb-buffer-type)
155 (defvar gdb-output-sink 'nil
156 "The disposition of the output of the current gdb command.
157 Possible values are these symbols:
159 `user' -- gdb output should be copied to the GUD buffer
162 `emacs' -- output should be collected in the partial-output-buffer
163 for subsequent processing by a command. This is the
164 disposition of output generated by commands that
165 gdb mode sends to gdb on its own behalf.")
167 (defvar gdb-pending-triggers '()
168 "A list of trigger functions that have run later than their output handlers.")
170 (defcustom gdb-debug-log-max 128
171 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
173 :type '(choice (integer :tag "Number of elements")
174 (const :tag "Unlimited" nil))
177 (defvar gdb-debug-log nil
178 "List of commands sent to and replies received from GDB.
179 Most recent commands are listed first. This list stores only the last
180 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
183 (defcustom gdb-enable-debug nil
184 "Non-nil means record the process input and output in `gdb-debug-log'."
189 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
190 "Shell command for generating a list of defined macros in a source file.
191 This list is used to display the #define directive associated
192 with an identifier as a tooltip. It works in a debug session with
193 GDB, when `gud-tooltip-mode' is t.
195 Set `gdb-cpp-define-alist-flags' for any include paths or
201 (defcustom gdb-cpp-define-alist-flags ""
202 "Preprocessor flags for `gdb-cpp-define-alist-program'."
207 (defcustom gdb-create-source-file-list t
208 "Non-nil means create a list of files from which the executable was built.
209 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
210 line for a long time when starting, possibly because your executable was
211 built from a large number of files. This allows quicker initialization
212 but means that these files are not automatically enabled for debugging,
213 e.g., you won't be able to click in the fringe to set a breakpoint until
214 execution has already stopped there."
219 (defcustom gdb-show-main nil
220 "Non-nil means display source file containing the main routine at startup.
221 Also display the main routine in the disassembly buffer if present."
226 ; Note: This mode requires a separate buffer for inferior IO.
227 (defconst gdb-use-separate-io-buffer t)
229 (defun gdb-force-mode-line-update (status)
230 (let ((buffer gud-comint-buffer))
231 (if (and buffer (buffer-name buffer))
232 (with-current-buffer buffer
233 (setq mode-line-process
235 (process-status (get-buffer-process buffer)) status))
236 ;; Force mode line redisplay soon.
237 (force-mode-line-update)))))
239 (defun gdb-enable-debug (arg)
240 "Toggle logging of transaction between Emacs and Gdb.
241 The log is stored in `gdb-debug-log' as an alist with elements
242 whose cons is send, send-item or recv and whose cdr is the string
243 being transferred. This list may grow up to a size of
244 `gdb-debug-log-max' after which the oldest element (at the end of
245 the list) is deleted every time a new one is added (at the front)."
247 (setq gdb-enable-debug
249 (not gdb-enable-debug)
250 (> (prefix-numeric-value arg) 0)))
251 (message (format "Logging of transaction %sabled"
252 (if gdb-enable-debug "en" "dis"))))
254 (defun gdb-find-watch-expression ()
255 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
256 (varnum (car var)) expr array)
257 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
258 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
259 (component-list (split-string (match-string 2 varnum) "\\." t)))
260 (setq expr (nth 1 var1))
261 (setq varnumlet (car var1))
262 (dolist (component component-list)
263 (setq var2 (assoc varnumlet gdb-var-list))
264 (setq expr (concat expr
265 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
266 (concat "[" component "]")
267 (concat "." component))))
268 (setq varnumlet (concat varnumlet "." component)))
271 (defvar gdb-locals-font-lock-keywords
274 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
275 (1 font-lock-variable-name-face)
276 (3 font-lock-type-face))
278 "Font lock keywords used in `gdb-local-mode'.")
281 (defun gdb (command-line)
282 "Run gdb on program FILE in buffer *gud-FILE*.
283 The directory containing FILE becomes the initial working directory
284 and source-file directory for your debugger.
286 If `gdb-many-windows' is nil (the default value) then gdb just
287 pops up the GUD buffer unless `gdb-show-main' is t. In this case
288 it starts with two windows: one displaying the GUD buffer and the
289 other with the source file with the main routine of the inferior.
291 If `gdb-many-windows' is t, regardless of the value of
292 `gdb-show-main', the layout below will appear unless
293 `gdb-use-separate-io-buffer' is nil when the source buffer
294 occupies the full width of the frame. Keybindings are shown in
297 Watch expressions appear in the speedbar/slowbar.
299 The following commands help control operation :
301 `gdb-many-windows' - Toggle the number of windows gdb uses.
302 `gdb-restore-windows' - To restore the window layout.
304 See Info node `(emacs)GDB Graphical Interface' for a more
305 detailed description of this mode.
308 +----------------------------------------------------------------------+
310 +-----------------------------------+----------------------------------+
311 | GUD buffer (I/O of GDB) | Locals buffer |
315 +-----------------------------------+----------------------------------+
316 | Source buffer | I/O buffer (of debugged program) |
324 +-----------------------------------+----------------------------------+
325 | Stack buffer | Breakpoints buffer |
326 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
327 | | RET gdb-goto-breakpoint |
328 | | D gdb-delete-breakpoint |
329 +-----------------------------------+----------------------------------+"
331 (interactive (list (gud-query-cmdline 'gdb)))
333 (when (and gud-comint-buffer
334 (buffer-name gud-comint-buffer)
335 (get-buffer-process gud-comint-buffer)
336 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
337 (gdb-restore-windows)
339 "Multiple debugging requires restarting in text command mode"))
341 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
342 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
343 (setq comint-input-sender 'gdb-send)
345 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
346 "Set temporary breakpoint at current line.")
348 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
349 "\C-j" "Set execution address to current line.")
351 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
352 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
353 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
354 (gud-def gud-pstar "print* %e" nil
355 "Evaluate C dereferenced pointer expression at point.")
357 (gud-def gud-step "-exec-step %p" "\C-s"
358 "Step one source line with display.")
359 (gud-def gud-stepi "-exec-step-instruction %p" "\C-i"
360 "Step one instruction with display.")
361 (gud-def gud-next "-exec-next %p" "\C-n"
362 "Step one line (skip functions).")
363 (gud-def gud-nexti "nexti %p" nil
364 "Step one instruction (skip functions).")
365 (gud-def gud-cont "-exec-continue" "\C-r"
366 "Continue with display.")
367 (gud-def gud-finish "-exec-finish" "\C-f"
368 "Finish executing current function.")
369 (gud-def gud-run "-exec-run" nil "Runn the program.")
371 (local-set-key "\C-i" 'gud-gdb-complete-command)
372 (setq gdb-first-prompt t)
373 (setq gud-running nil)
375 (run-hooks 'gdb-mode-hook))
378 (gud-def gud-break (if (not (string-equal mode-name "Machine"))
379 (gud-call "break %f:%l" arg)
383 (gud-call "break *%a" arg)))
384 "\C-b" "Set breakpoint at current line or address.")
386 (gud-def gud-remove (if (not (string-equal mode-name "Machine"))
387 (gud-call "clear %f:%l" arg)
391 (gud-call "clear *%a" arg)))
392 "\C-d" "Remove breakpoint at current line or address.")
394 (gud-def gud-until (if (not (string-equal mode-name "Machine"))
395 (gud-call "-exec-until %f:%l" arg)
399 (gud-call "-exec-until *%a" arg)))
400 "\C-u" "Continue to current line or address.")
403 gud-go (gud-call (if gdb-active-process "-exec-continue" "-exec-run") arg)
404 nil "Start or continue execution.")
406 ;; For debugging Emacs only.
410 "pp1 " (if (eq (buffer-local-value
411 'major-mode (window-buffer)) 'speedbar-mode)
412 (gdb-find-watch-expression) "%e")) arg)
413 nil "Print the Emacs s-expression.")
415 (define-key gud-minor-mode-map [left-margin mouse-1]
416 'gdb-mouse-set-clear-breakpoint)
417 (define-key gud-minor-mode-map [left-fringe mouse-1]
418 'gdb-mouse-set-clear-breakpoint)
419 (define-key gud-minor-mode-map [left-margin C-mouse-1]
420 'gdb-mouse-toggle-breakpoint-margin)
421 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
422 'gdb-mouse-toggle-breakpoint-fringe)
424 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
426 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
428 (define-key gud-minor-mode-map [left-margin mouse-3]
430 (define-key gud-minor-mode-map [left-fringe mouse-3]
433 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
435 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
437 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
439 (define-key gud-minor-mode-map [left-margin C-mouse-3]
443 (setq gdb-pc-address (if gdb-show-main "main" nil))
444 (setq gdb-selected-frame nil
447 gdb-pending-triggers nil
448 gdb-output-sink 'user
449 gdb-location-alist nil
450 gdb-source-file-list nil
453 gdb-handler-alist '()
454 gdb-handler-number nil
456 gdb-first-done-or-error t
457 gdb-buffer-fringe-width (car (window-fringes))
459 gdb-source-window nil
460 gdb-inferior-status nil
461 gdb-continuation nil)
463 (setq gdb-buffer-type 'gdbmi)
465 (gdb-force-mode-line-update
466 (propertize "initializing..." 'face font-lock-variable-name-face))
468 (when gdb-use-separate-io-buffer
469 (gdb-get-buffer-create 'gdb-inferior-io)
470 (gdb-clear-inferior-io)
471 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
473 ;; Needs GDB 6.4 onwards
474 (list (concat "-inferior-tty-set "
475 (process-tty-name (get-process "gdb-inferior")) "\n")
477 (if (eq window-system 'w32)
478 (gdb-input (list "-gdb-set new-console off\n" 'ignore)))
479 (gdb-input (list "-gdb-set height 0\n" 'ignore))
480 ;; find source file and compilation directory here
482 ; Needs GDB 6.2 onwards.
483 (list "-file-list-exec-source-files\n" 'gdb-get-source-file-list))
484 (if gdb-create-source-file-list
486 ; Needs GDB 6.0 onwards.
487 (list "-file-list-exec-source-file\n" 'gdb-get-source-file)))
489 (list "-data-list-register-names\n" 'gdb-get-register-names))
491 (list "-gdb-show prompt\n" 'gdb-get-prompt)))
493 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
495 (defun gdb-create-define-alist ()
496 "Create an alist of #define directives for GUD tooltips."
497 (let* ((file (buffer-file-name))
499 (with-output-to-string
500 (with-current-buffer standard-output
503 ;; call-process doesn't work with remote file names.
504 (not (file-remote-p default-directory))
505 (call-process shell-file-name file
506 (list t nil) nil "-c"
507 (concat gdb-cpp-define-alist-program " "
508 gdb-cpp-define-alist-flags))))))
509 (define-list (split-string output "\n" t))
511 (setq gdb-define-alist nil)
512 (dolist (define define-list)
513 (setq name (nth 1 (split-string define "[( ]")))
514 (push (cons name define) gdb-define-alist))))
516 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
517 (defvar tooltip-use-echo-area)
519 (defun gdb-tooltip-print (expr)
521 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
522 (goto-char (point-min))
524 (if (search-forward "=" nil t)
525 (concat expr (buffer-substring (- (point) 2) (point-max)))
527 ;; remove newline for gud-tooltip-echo-area
528 (substring string 0 (- (length string) 1))))
529 (or gud-tooltip-echo-area tooltip-use-echo-area
530 (not (display-graphic-p)))))
532 ;; If expr is a macro for a function don't print because of possible dangerous
533 ;; side-effects. Also printing a function within a tooltip generates an
534 ;; unexpected starting annotation (phase error).
535 (defun gdb-tooltip-print-1 (expr)
536 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
537 (goto-char (point-min))
538 (if (search-forward "expands to: " nil t)
539 (unless (looking-at "\\S-+.*(.*).*")
541 (list (concat "print " expr "\n")
542 `(lambda () (gdb-tooltip-print ,expr))))))))
544 (defun gdb-init-buffer ()
545 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
546 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
547 (when gud-tooltip-mode
548 (make-local-variable 'gdb-define-alist)
549 (gdb-create-define-alist)
550 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
552 (defmacro gdb-if-arrow (arrow-position &rest body)
554 (let ((buffer (marker-buffer ,arrow-position)) (line))
555 (if (equal buffer (window-buffer (posn-window end)))
556 (with-current-buffer buffer
557 (when (or (equal start end)
558 (equal (posn-point start)
559 (marker-position ,arrow-position)))
562 (defun gdb-mouse-until (event)
563 "Continue running until a source line past the current line.
564 The destination source line can be selected either by clicking
565 with mouse-3 on the fringe/margin or dragging the arrow
566 with mouse-1 (default bindings)."
568 (let ((start (event-start event))
569 (end (event-end event)))
570 (gdb-if-arrow gud-overlay-arrow-position
571 (setq line (line-number-at-pos (posn-point end)))
572 (gud-call (concat "until " (number-to-string line))))
573 (gdb-if-arrow gdb-overlay-arrow-position
575 (goto-line (line-number-at-pos (posn-point end)))
577 (gud-call (concat "until *%a"))))))
579 (defun gdb-mouse-jump (event)
580 "Set execution address/line.
581 The destination source line can be selected either by clicking with C-mouse-3
582 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
583 Unlike `gdb-mouse-until' the destination address can be before the current
584 line, and no execution takes place."
586 (let ((start (event-start event))
587 (end (event-end event)))
588 (gdb-if-arrow gud-overlay-arrow-position
589 (setq line (line-number-at-pos (posn-point end)))
591 (gud-call (concat "tbreak " (number-to-string line)))
592 (gud-call (concat "jump " (number-to-string line)))))
593 (gdb-if-arrow gdb-overlay-arrow-position
595 (goto-line (line-number-at-pos (posn-point end)))
598 (gud-call (concat "tbreak *%a"))
599 (gud-call (concat "jump *%a")))))))
601 (defcustom gdb-show-changed-values t
602 "If non-nil change the face of out of scope variables and changed values.
603 Out of scope variables are suppressed with `shadow' face.
604 Changed values are highlighted with the face `font-lock-warning-face'."
609 (defcustom gdb-max-children 40
610 "Maximum number of children before expansion requires confirmation."
615 (defcustom gdb-delete-out-of-scope t
616 "If non-nil delete watch expressions automatically when they go out of scope."
621 (defcustom gdb-speedbar-auto-raise nil
622 "If non-nil raise speedbar every time display of watch expressions is\
628 (defcustom gdb-use-colon-colon-notation nil
629 "If non-nil use FUN::VAR format to display variables in the speedbar."
634 (defun gdb-speedbar-auto-raise (arg)
635 "Toggle automatic raising of the speedbar for watch expressions.
636 With prefix argument ARG, automatically raise speedbar if ARG is
637 positive, otherwise don't automatically raise it."
639 (setq gdb-speedbar-auto-raise
641 (not gdb-speedbar-auto-raise)
642 (> (prefix-numeric-value arg) 0)))
643 (message (format "Auto raising %sabled"
644 (if gdb-speedbar-auto-raise "en" "dis"))))
646 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
647 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
649 (declare-function tooltip-identifier-from-point "tooltip" (point))
651 (defun gud-watch (&optional arg event)
652 "Watch expression at point.
653 With arg, enter name of variable to be watched in the minibuffer."
654 (interactive (list current-prefix-arg last-input-event))
655 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
656 (if (eq minor-mode 'gdbmi)
658 (if event (posn-set-point (event-end event)))
660 (save-selected-window
663 (completing-read "Name of variable: "
664 'gud-gdb-complete-command)
665 (if (and transient-mark-mode mark-active)
666 (buffer-substring (region-beginning) (region-end))
667 (concat (if (eq major-mode 'gdb-registers-mode) "$")
668 (tooltip-identifier-from-point (point)))))))
669 (set-text-properties 0 (length expr) nil expr)
671 (list (concat"-var-create - * " expr "\n")
672 `(lambda () (gdb-var-create-handler ,expr)))))))
673 (message "gud-watch is a no-op in this mode."))))
675 (defconst gdb-var-create-regexp
676 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",\\(?:.*value=\\(\".*\"\\),\\)?.*type=\"\\(.*?\\)\"")
678 (defun gdb-var-create-handler (expr)
679 (goto-char (point-min))
680 (if (re-search-forward gdb-var-create-regexp nil t)
683 (if (and (string-equal gdb-current-language "c")
684 gdb-use-colon-colon-notation gdb-selected-frame)
685 (setq expr (concat gdb-selected-frame "::" expr))
689 (if (match-string 3) (read (match-string 3)))
691 (push var gdb-var-list)
693 (unless (string-equal
694 speedbar-initial-expansion-list-name "GUD")
695 (speedbar-change-initial-expansion-list "GUD"))
698 (concat "-var-evaluate-expression " (car var) "\n")
699 `(lambda () (gdb-var-evaluate-expression-handler
701 (message-box "No symbol \"%s\" in current context." expr)))
703 (defun gdb-speedbar-update ()
704 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
705 (not (member 'gdb-speedbar-timer gdb-pending-triggers)))
706 ;; Dummy command to update speedbar even when idle.
707 (gdb-input (list "-environment-pwd\n" 'gdb-speedbar-timer-fn))
708 ;; Keep gdb-pending-triggers non-nil till end.
709 (push 'gdb-speedbar-timer gdb-pending-triggers)))
711 (defun gdb-speedbar-timer-fn ()
712 (if gdb-speedbar-auto-raise
713 (raise-frame speedbar-frame))
714 (setq gdb-pending-triggers
715 (delq 'gdb-speedbar-timer gdb-pending-triggers))
718 (defun gdb-var-evaluate-expression-handler (varnum changed)
719 (goto-char (point-min))
720 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
721 (let ((var (assoc varnum gdb-var-list)))
723 (if changed (setcar (nthcdr 5 var) 'changed))
724 (setcar (nthcdr 4 var) (read (match-string 1)))))
725 (gdb-speedbar-update))
727 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
728 (defun gdb-var-list-children (varnum)
730 (list (concat "-var-update " varnum "\n") 'ignore))
732 (list (concat "-var-list-children --all-values "
734 `(lambda () (gdb-var-list-children-handler ,varnum)))))
736 (defconst gdb-var-list-children-regexp
737 "child={.*?name=\"\\(.+?\\)\".*?,exp=\"\\(.+?\\)\".*?,\
738 numchild=\"\\(.+?\\)\".*?,value=\\(\".*?\"\\).*?,type=\"\\(.+?\\)\".*?}")
740 (defun gdb-var-list-children-handler (varnum)
741 (goto-char (point-min))
742 (let ((var-list nil))
743 (catch 'child-already-watched
744 (dolist (var gdb-var-list)
745 (if (string-equal varnum (car var))
748 (while (re-search-forward gdb-var-list-children-regexp nil t)
749 (let ((varchild (list (match-string 1)
753 (read (match-string 4))
755 (if (assoc (car varchild) gdb-var-list)
756 (throw 'child-already-watched nil))
757 (push varchild var-list))))
758 (push var var-list)))
759 (setq gdb-var-list (nreverse var-list))))
760 (gdb-speedbar-update))
762 (defun gdb-var-set-format (format)
763 "Set the output format for a variable displayed in the speedbar."
764 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
767 (list (concat "-var-set-format " varnum " " format "\n") 'ignore))
770 (defun gdb-var-delete-1 (varnum)
772 (list (concat "-var-delete " varnum "\n") 'ignore))
773 (setq gdb-var-list (delq var gdb-var-list))
774 (dolist (varchild gdb-var-list)
775 (if (string-match (concat (car var) "\\.") (car varchild))
776 (setq gdb-var-list (delq varchild gdb-var-list)))))
778 (defun gdb-var-delete ()
779 "Delete watch expression at point from the speedbar."
781 (let ((text (speedbar-line-text)))
782 (string-match "\\(\\S-+\\)" text)
783 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
785 (if (string-match "\\." (car var))
786 (message-box "Can only delete a root expression")
787 (gdb-var-delete-1 varnum)))))
789 (defun gdb-var-delete-children (varnum)
790 "Delete children of variable object at point from the speedbar."
792 (list (concat "-var-delete -c " varnum "\n") 'ignore)))
794 (defun gdb-edit-value (text token indent)
795 "Assign a value to a variable displayed in the speedbar."
796 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
797 (varnum (car var)) (value))
798 (setq value (read-string "New value: "))
800 (list (concat "-var-assign " varnum " " value "\n")
801 `(lambda () (gdb-edit-value-handler ,value))))))
803 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
805 (defun gdb-edit-value-handler (value)
806 (goto-char (point-min))
807 (if (re-search-forward gdb-error-regexp nil t)
808 (message-box "Invalid number or expression (%s)" value)))
810 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
811 (defun gdb-var-update ()
812 (if (not (member 'gdb-var-update gdb-pending-triggers))
814 (list "-var-update --all-values *\n" 'gdb-var-update-handler)))
815 (push 'gdb-var-update gdb-pending-triggers))
817 (defconst gdb-var-update-regexp
818 "{.*?name=\"\\(.*?\\)\".*?,\\(?:value=\\(\".*?\"\\),\\)?.*?\
819 in_scope=\"\\(.*?\\)\".*?}")
821 (defun gdb-var-update-handler ()
822 (dolist (var gdb-var-list)
823 (setcar (nthcdr 5 var) nil))
824 (goto-char (point-min))
825 (while (re-search-forward gdb-var-update-regexp nil t)
826 (let* ((varnum (match-string 1))
827 (var (assoc varnum gdb-var-list)))
829 (let ((match (match-string 3)))
830 (cond ((string-equal match "false")
831 (if gdb-delete-out-of-scope
832 (gdb-var-delete-1 varnum)
833 (setcar (nthcdr 5 var) 'out-of-scope)))
834 ((string-equal match "true")
835 (setcar (nthcdr 5 var) 'changed)
836 (setcar (nthcdr 4 var)
837 (read (match-string 2))))
838 ((string-equal match "invalid")
839 (gdb-var-delete-1 varnum)))))))
840 (setq gdb-pending-triggers
841 (delq 'gdb-var-update gdb-pending-triggers))
842 (gdb-speedbar-update))
844 (defun gdb-speedbar-expand-node (text token indent)
845 "Expand the node the user clicked on.
846 TEXT is the text of the button we clicked on, a + or - item.
847 TOKEN is data related to this node.
848 INDENT is the current indentation depth."
849 (cond ((string-match "+" text) ;expand this node
850 (let* ((var (assoc token gdb-var-list))
851 (expr (nth 1 var)) (children (nth 2 var)))
852 (if (or (<= (string-to-number children) gdb-max-children)
854 (format "%s has %s children. Continue? " expr children)))
855 (gdb-var-list-children token))))
856 ((string-match "-" text) ;contract this node
857 (dolist (var gdb-var-list)
858 (if (string-match (concat token "\\.") (car var))
859 (setq gdb-var-list (delq var gdb-var-list))))
860 (gdb-var-delete-children token)
861 (speedbar-change-expand-button-char ?+)
862 (speedbar-delete-subblock indent))
863 (t (error "Ooops... not sure what to do")))
864 (speedbar-center-buffer-smartly))
866 (defun gdb-get-target-string ()
867 (with-current-buffer gud-comint-buffer
874 ;; Each buffer has a TYPE -- a symbol that identifies the function
875 ;; of that particular buffer.
877 ;; The usual gdb interaction buffer is given the type `gdbmi' and
878 ;; is constructed specially.
880 ;; Others are constructed by gdb-get-buffer-create and
881 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
883 (defvar gdb-buffer-rules-assoc '())
885 (defun gdb-get-buffer (key)
886 "Return the gdb buffer tagged with type KEY.
887 The key should be one of the cars in `gdb-buffer-rules-assoc'."
889 (gdb-look-for-tagged-buffer key (buffer-list))))
891 (defun gdb-get-buffer-create (key)
892 "Create a new gdb buffer of the type specified by KEY.
893 The key should be one of the cars in `gdb-buffer-rules-assoc'."
894 (or (gdb-get-buffer key)
895 (let* ((rules (assoc key gdb-buffer-rules-assoc))
896 (name (funcall (gdb-rules-name-maker rules)))
897 (new (get-buffer-create name)))
898 (with-current-buffer new
900 (if (cdr (cdr rules))
901 (setq trigger (funcall (car (cdr (cdr rules))))))
902 (setq gdb-buffer-type key)
903 (set (make-local-variable 'gud-minor-mode)
904 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
905 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
906 (if trigger (funcall trigger)))
909 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
911 (defun gdb-look-for-tagged-buffer (key bufs)
913 (while (and (not retval) bufs)
914 (set-buffer (car bufs))
915 (if (eq gdb-buffer-type key)
916 (setq retval (car bufs)))
917 (setq bufs (cdr bufs)))
920 ;; Used to define all gdb-frame-*-buffer functions except
921 ;; `gdb-frame-separate-io-buffer'
922 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
923 "Define a function NAME which shows gdb BUFFER in a separate frame.
925 DOC is an optional documentation string."
929 (let ((special-display-regexps (append special-display-regexps '(".*")))
930 (special-display-frame-alist gdb-frame-parameters))
931 (display-buffer (gdb-get-buffer-create ,buffer)))))
933 (defmacro def-gdb-display-buffer (name buffer &optional doc)
934 "Define a function NAME which shows gdb BUFFER.
936 DOC is an optional documentation string."
941 (gdb-get-buffer-create ,buffer) t)))
944 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
945 ;; at least one and possible more functions. The functions have these
946 ;; roles in defining a buffer type:
948 ;; NAME - Return a name for this buffer type.
950 ;; The remaining function(s) are optional:
952 ;; MODE - called in a new buffer with no arguments, should establish
953 ;; the proper mode for the buffer.
956 (defun gdb-set-buffer-rules (buffer-type &rest rules)
957 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
959 (setcdr binding rules)
960 (push (cons buffer-type rules)
961 gdb-buffer-rules-assoc))))
963 ;; GUD buffers are an exception to the rules
964 (gdb-set-buffer-rules 'gdbmi 'error)
966 ;; Partial-output buffer : This accumulates output from a command executed on
967 ;; behalf of emacs (rather than the user).
969 (gdb-set-buffer-rules 'gdb-partial-output-buffer
970 'gdb-partial-output-name)
972 (defun gdb-partial-output-name ()
973 (concat " *partial-output-"
974 (gdb-get-target-string)
978 (gdb-set-buffer-rules 'gdb-inferior-io
979 'gdb-inferior-io-name
980 'gdb-inferior-io-mode)
982 (defun gdb-inferior-io-name ()
983 (concat "*input/output of "
984 (gdb-get-target-string)
987 (defun gdb-display-separate-io-buffer ()
988 "Display IO of debugged program in a separate window."
990 (if gdb-use-separate-io-buffer
992 (gdb-get-buffer-create 'gdb-inferior-io) t)))
994 (defconst gdb-frame-parameters
995 '((height . 14) (width . 80)
997 (tool-bar-lines . nil)
998 (menu-bar-lines . nil)
1001 (defun gdb-frame-separate-io-buffer ()
1002 "Display IO of debugged program in a new frame."
1004 (if gdb-use-separate-io-buffer
1005 (let ((special-display-regexps (append special-display-regexps '(".*")))
1006 (special-display-frame-alist gdb-frame-parameters))
1007 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1009 (defvar gdb-inferior-io-mode-map
1010 (let ((map (make-sparse-keymap)))
1011 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1012 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1013 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1014 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1015 (define-key map "\C-d" 'gdb-separate-io-eof)
1018 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1019 "Major mode for gdb inferior-io."
1020 :syntax-table nil :abbrev-table nil
1021 ;; We want to use comint because it has various nifty and familiar
1022 ;; features. We don't need a process, but comint wants one, so create
1024 (make-comint-in-buffer
1025 "gdb-inferior" (current-buffer) "sleep" nil "1000000000"))
1027 (defun gdb-inferior-filter (proc string)
1028 (unless (string-equal string "")
1029 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1030 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1031 (insert-before-markers string)))
1033 (defun gdb-separate-io-interrupt ()
1034 "Interrupt the program being debugged."
1037 (get-buffer-process gud-comint-buffer) comint-ptyp))
1039 (defun gdb-separate-io-quit ()
1040 "Send quit signal to the program being debugged."
1043 (get-buffer-process gud-comint-buffer) comint-ptyp))
1045 (defun gdb-separate-io-stop ()
1046 "Stop the program being debugged."
1049 (get-buffer-process gud-comint-buffer) comint-ptyp))
1051 (defun gdb-separate-io-eof ()
1052 "Send end-of-file to the program being debugged."
1055 (get-buffer-process gud-comint-buffer)))
1057 (defun gdb-clear-inferior-io ()
1058 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1062 (defconst breakpoint-xpm-data
1064 static char *magick[] = {
1065 /* columns rows colors chars-per-pixel */
1081 "XPM data used for breakpoint icon.")
1083 (defconst breakpoint-enabled-pbm-data
1086 0 0 0 0 1 1 1 1 0 0 0 0
1087 0 0 0 1 1 1 1 1 1 0 0 0
1088 0 0 1 1 1 1 1 1 1 1 0 0
1089 0 1 1 1 1 1 1 1 1 1 1 0
1090 0 1 1 1 1 1 1 1 1 1 1 0
1091 0 1 1 1 1 1 1 1 1 1 1 0
1092 0 1 1 1 1 1 1 1 1 1 1 0
1093 0 0 1 1 1 1 1 1 1 1 0 0
1094 0 0 0 1 1 1 1 1 1 0 0 0
1095 0 0 0 0 1 1 1 1 0 0 0 0"
1096 "PBM data used for enabled breakpoint icon.")
1098 (defconst breakpoint-disabled-pbm-data
1110 0 0 0 1 0 1 0 1 0 0"
1111 "PBM data used for disabled breakpoint icon.")
1113 (defvar breakpoint-enabled-icon nil
1114 "Icon for enabled breakpoint in display margin.")
1116 (defvar breakpoint-disabled-icon nil
1117 "Icon for disabled breakpoint in display margin.")
1119 (declare-function define-fringe-bitmap "fringe.c"
1120 (bitmap bits &optional height width align))
1122 (and (display-images-p)
1123 ;; Bitmap for breakpoint in fringe
1124 (define-fringe-bitmap 'breakpoint
1125 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1126 ;; Bitmap for gud-overlay-arrow in fringe
1127 (define-fringe-bitmap 'hollow-right-triangle
1128 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1130 (defface breakpoint-enabled
1134 "Face for enabled breakpoint icon in fringe."
1137 (defface breakpoint-disabled
1138 '((((class color) (min-colors 88)) :foreground "grey70")
1139 ;; Ensure that on low-color displays that we end up something visible.
1140 (((class color) (min-colors 8) (background light))
1141 :foreground "black")
1142 (((class color) (min-colors 8) (background dark))
1143 :foreground "white")
1144 (((type tty) (class mono))
1146 (t :background "gray"))
1147 "Face for disabled breakpoint icon in fringe."
1151 (defun gdb-send (proc string)
1152 "A comint send filter for gdb."
1153 (with-current-buffer gud-comint-buffer
1154 (let ((inhibit-read-only t))
1155 (remove-text-properties (point-min) (point-max) '(face))))
1156 ;; mimic <RET> key to repeat previous command in GDB
1157 (if (not (string-match "^\\s+$" string))
1158 (setq gdb-last-command string)
1159 (if gdb-last-command (setq string gdb-last-command)))
1160 (if gdb-enable-debug
1161 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1162 (if (string-match "^-" string)
1165 (setq gdb-first-done-or-error t)
1166 (process-send-string proc (concat string "\n")))
1168 (if (string-match "\\\\$" string)
1169 (setq gdb-continuation (concat gdb-continuation string "\n"))
1170 (setq gdb-first-done-or-error t)
1171 (process-send-string proc (concat "-interpreter-exec console \""
1172 gdb-continuation string "\"\n"))
1173 (setq gdb-continuation nil))))
1175 (defun gdb-input (item)
1176 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1177 (setq gdb-token-number (1+ gdb-token-number))
1178 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1179 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1180 (process-send-string (get-buffer-process gud-comint-buffer)
1184 (defcustom gud-gdb-command-name "gdb -i=mi"
1185 "Default command to execute an executable under the GDB debugger."
1190 (setq gud-running nil)
1191 (setq gdb-output-sink 'user)
1192 (setq gdb-pending-triggers nil))
1194 (defun gdb-update ()
1195 "Update buffers showing status of debug session."
1196 (when gdb-first-prompt
1197 (gdb-force-mode-line-update
1198 (propertize "initializing..." 'face font-lock-variable-name-face))
1200 (setq gdb-first-prompt nil))
1201 (gdb-get-selected-frame)
1202 (gdb-invalidate-frames)
1203 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1204 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1205 (gdb-invalidate-breakpoints)
1206 (gdb-invalidate-threads)
1207 (gdb-get-changed-registers)
1208 (gdb-invalidate-registers)
1209 (gdb-invalidate-locals)
1210 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1211 (dolist (var gdb-var-list)
1212 (setcar (nthcdr 5 var) nil))
1215 ;; GUD displays the selected GDB frame. This might might not be the current
1216 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1217 ;; visited breakpoint is, use that window.
1218 (defun gdb-display-source-buffer (buffer)
1219 (let* ((last-window (if gud-last-last-frame
1221 (gud-find-file (car gud-last-last-frame)))))
1222 (source-window (or last-window
1223 (if (and gdb-source-window
1224 (window-live-p gdb-source-window))
1225 gdb-source-window))))
1227 (setq gdb-source-window source-window)
1228 (set-window-buffer source-window buffer))
1231 (defun gdb-car< (a b)
1232 (< (car a) (car b)))
1234 (defvar gdbmi-record-list
1235 '((gdb-gdb . "(gdb) \n")
1236 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1237 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1238 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1239 (gdb-console . "~\\(\".*?\"\\)\n")
1240 (gdb-internals . "&\\(\".*?\"\\)\n")
1241 (gdb-stopped . "\\*stopped,?\\(.*?\n\\)")
1242 (gdb-running . "\\*running,\\(.*?\n\\)")
1243 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1244 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")))
1246 (defun gud-gdbmi-marker-filter (string)
1247 "Filter GDB/MI output."
1249 ;; Record transactions if logging is enabled.
1250 (when gdb-enable-debug
1251 (push (cons 'recv string) gdb-debug-log)
1252 (if (and gdb-debug-log-max
1253 (> (length gdb-debug-log) gdb-debug-log-max))
1254 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1256 ;; Recall the left over gud-marker-acc from last time
1257 (setq gud-marker-acc (concat gud-marker-acc string))
1259 ;; Start accumulating output for the GUD buffer
1260 (setq gdb-filter-output "")
1261 (let ((output-record) (output-record-list))
1263 ;; Process all the complete markers in this chunk.
1264 (dolist (gdbmi-record gdbmi-record-list)
1265 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1266 (push (list (match-beginning 0)
1268 (match-string 1 gud-marker-acc)
1269 (match-string 2 gud-marker-acc)
1272 (setq gud-marker-acc
1273 (concat (substring gud-marker-acc 0 (match-beginning 0))
1274 ;; Pad with spaces to preserve position.
1275 (make-string (length (match-string 0 gud-marker-acc)) 32)
1276 (substring gud-marker-acc (match-end 0))))))
1278 (setq output-record-list (sort output-record-list 'gdb-car<))
1280 (dolist (output-record output-record-list)
1281 (let ((record-type (cadr output-record))
1282 (arg1 (nth 2 output-record))
1283 (arg2 (nth 3 output-record)))
1284 (if (eq record-type 'gdb-error)
1285 (gdb-done-or-error arg2 arg1 'error)
1286 (if (eq record-type 'gdb-done)
1287 (gdb-done-or-error arg2 arg1 'done)
1288 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1289 ;; error message on internal stream. Don't print to GUD buffer.
1290 (unless (and (eq record-type 'gdb-internals)
1291 (string-equal (read arg1) "No registers.\n"))
1292 (funcall record-type arg1))))))
1294 (setq gdb-output-sink 'user)
1296 (string-match "^ *" gud-marker-acc)
1297 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1301 (defun gdb-gdb (output-field))
1302 (defun gdb-thread-created (output-field))
1303 (defun gdb-thread-exited (output-field))
1305 (defun gdb-running (output-field)
1306 (setq gdb-inferior-status "running")
1307 (gdb-force-mode-line-update
1308 (propertize gdb-inferior-status 'face font-lock-type-face))
1309 (setq gdb-active-process t)
1310 (setq gud-running t))
1312 (defun gdb-starting (output-field)
1313 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1314 (setq gdb-inferior-status "running")
1315 (gdb-force-mode-line-update
1316 (propertize gdb-inferior-status 'face font-lock-type-face))
1317 (setq gdb-active-process t)
1318 (setq gud-running t))
1320 ;; -break-insert -t didn't give a reason before gdb 6.9
1321 (defconst gdb-stopped-regexp
1322 "\\(reason=\"\\(.*?\\)\"\\)?\\(\\(,exit-code=.*?\\)*\n\\|.*?,file=\".*?\".*?,fullname=\"\\(.*?\\)\".*?,line=\"\\(.*?\\)\".*?\n\\)")
1324 (defun gdb-stopped (output-field)
1325 (setq gud-running nil)
1326 (string-match gdb-stopped-regexp output-field)
1327 (let ((reason (match-string 2 output-field))
1328 (file (match-string 5 output-field)))
1330 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1331 ;;; because synchronous GDB doesn't give these fields with CLI.
1334 ;;; ;; Extract the frame position from the marker.
1335 ;;; gud-last-frame (cons file
1336 ;;; (string-to-number
1337 ;;; (match-string 6 gud-marker-acc)))))
1339 (setq gdb-inferior-status (if reason reason "unknown"))
1340 (gdb-force-mode-line-update
1341 (propertize gdb-inferior-status 'face font-lock-warning-face))
1342 (if (string-equal reason "exited-normally")
1343 (setq gdb-active-process nil)))
1345 (when gdb-first-done-or-error
1346 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name))
1348 (setq gdb-first-done-or-error nil)))
1350 ;; Remove the trimmings from log stream containing debugging messages
1351 ;; being produced by GDB's internals, use warning face and send to GUD
1353 (defun gdb-internals (output-field)
1354 (setq gdb-filter-output
1357 (let ((error-message
1358 (read output-field)))
1360 0 (length error-message)
1361 'face font-lock-warning-face
1365 ;; Remove the trimmings from the console stream and send to GUD buffer
1366 ;; (frontend MI commands should not print to this stream)
1367 (defun gdb-console (output-field)
1368 (setq gdb-filter-output
1371 (read output-field))))
1373 (defun gdb-done-or-error (output-field token-number type)
1374 (if (string-equal token-number "")
1375 ;; Output from command entered by user
1377 (setq gdb-output-sink 'user)
1378 (setq token-number nil)
1379 ;; MI error - send to minibuffer
1380 (when (eq type 'error)
1381 ;; Skip "msg=" from `output-field'
1382 (message (read (substring output-field 4)))
1383 ;; Don't send to the console twice. (If it is a console error
1384 ;; it is also in the console stream.)
1385 (setq output-field nil)))
1386 ;; Output from command from frontend.
1387 (setq gdb-output-sink 'emacs))
1389 (gdb-clear-partial-output)
1390 (when gdb-first-done-or-error
1391 (unless (or token-number gud-running)
1392 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1394 (setq gdb-first-done-or-error nil))
1396 (setq gdb-filter-output
1397 (gdb-concat-output gdb-filter-output output-field))
1401 (with-current-buffer
1402 (gdb-get-buffer-create 'gdb-partial-output-buffer)
1404 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
1405 (setq gdb-handler-alist
1406 (assq-delete-all token-number gdb-handler-alist)))))
1408 (defun gdb-concat-output (so-far new)
1409 (let ((sink gdb-output-sink))
1411 ((eq sink 'user) (concat so-far new))
1413 (gdb-append-to-partial-output new)
1416 (defun gdb-append-to-partial-output (string)
1417 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1418 (goto-char (point-max))
1421 (defun gdb-clear-partial-output ()
1422 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1425 (defun json-partial-output (&optional fix-key)
1426 "Parse gdb-partial-output-buffer with `json-read'.
1428 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurences from
1429 partial output. This is used to get rid of useless keys in lists
1430 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
1431 -break-info are examples of MI commands which issue such
1434 Note that GDB/MI output syntax is different from JSON both
1435 cosmetically and (in some cases) structurally, so correct results
1436 are not guaranteed."
1437 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1438 (goto-char (point-min))
1439 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
1440 (replace-match "" nil nil nil 1))
1441 (goto-char (point-min))
1443 ;; Wrap field names in double quotes and replace equal sign with
1445 ;; TODO: This breaks badly with foo= inside constants
1446 (while (re-search-forward "\\([[:alpha:]-_]+\\)=" nil t)
1447 (replace-match "\"\\1\":" nil nil))
1448 (goto-char (point-max))
1450 (goto-char (point-min))
1451 (let ((json-array-type 'list))
1454 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1455 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1458 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1460 `(defun ,name (&optional ignored)
1461 (if (and ,demand-predicate
1463 gdb-pending-triggers)))
1466 (list ,gdb-command ',output-handler))
1467 (push ',name gdb-pending-triggers)))))
1469 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1470 "Define a handler NAME for TRIGGER acting in BUF-KEY with CUSTOM-DEFUN.
1472 Delete TRIGGER from `gdb-pending-triggers', switch to gdb BUF-KEY
1473 buffer using `gdb-get-buffer', erase it and evalueat
1476 (setq gdb-pending-triggers
1478 gdb-pending-triggers))
1479 (let ((buf (gdb-get-buffer ',buf-key)))
1481 (with-current-buffer buf
1482 (let* ((window (get-buffer-window buf 0))
1483 (start (window-start window))
1484 (p (window-point window))
1485 (buffer-read-only nil))
1487 (set-window-start window start)
1488 (set-window-point window p)
1489 (,custom-defun)))))))
1491 (defmacro def-gdb-auto-updated-buffer (buf-key
1492 trigger-name gdb-command
1493 output-handler-name custom-defun)
1494 "Define a trigger and its handler for buffers of type BUF-KEY.
1496 TRIGGER-NAME trigger is defined to send GDB-COMMAND if BUF-KEY
1499 OUTPUT-HANDLER-NAME handler uses customization of CUSTOM-DEFUN."
1501 (def-gdb-auto-update-trigger ,trigger-name
1502 ;; The demand predicate:
1503 (gdb-get-buffer ',buf-key)
1505 ,output-handler-name)
1506 (def-gdb-auto-update-handler ,output-handler-name
1507 ,trigger-name ,buf-key ,custom-defun)))
1511 ;; Breakpoint buffer : This displays the output of `-break-list'.
1513 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1514 'gdb-breakpoints-buffer-name
1515 'gdb-breakpoints-mode)
1517 (def-gdb-auto-update-trigger gdb-invalidate-breakpoints
1518 (gdb-get-buffer 'gdb-breakpoints-buffer)
1520 gdb-break-list-handler)
1522 (defconst gdb-break-list-regexp
1523 "bkpt={.*?number=\"\\(.*?\\)\".*?,type=\"\\(.*?\\)\".*?,disp=\"\\(.*?\\)\".*?,\
1524 enabled=\"\\(.\\)\".*?,addr=\"\\(.*?\\)\",\\(?:.*?func=\"\\(.*?\\)\".*?,\
1525 file=\"\\(.*?\\)\".*?,fullname=\".*?\".*?,line=\"\\(.*?\\)\",\
1526 \\|\\(?:.*?what=\"\\(.*?\\)\",\\)*\\).*?times=\"\\(.*?\\)\".*?}")
1528 (defun gdb-break-list-handler ()
1529 (setq gdb-pending-triggers (delq 'gdb-invalidate-breakpoints
1530 gdb-pending-triggers))
1531 (let ((breakpoint) (breakpoints-list))
1532 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1533 (goto-char (point-min))
1534 (while (re-search-forward gdb-break-list-regexp nil t)
1535 (let ((breakpoint (list (match-string 1)
1544 (match-string 10))))
1545 (push breakpoint breakpoints-list))))
1546 (let ((buf (gdb-get-buffer 'gdb-breakpoints-buffer)))
1547 (and buf (with-current-buffer buf
1549 (buffer-read-only nil))
1551 (insert "Num Type Disp Enb Hits Addr What\n")
1552 (dolist (breakpoint breakpoints-list)
1555 (nth 0 breakpoint) " "
1556 (nth 1 breakpoint) " "
1557 (nth 2 breakpoint) " "
1558 (propertize (nth 3 breakpoint)
1559 'face (if (eq (string-to-char (nth 3 breakpoint)) ?y)
1560 font-lock-warning-face
1561 font-lock-type-face)) " "
1562 (nth 9 breakpoint) " "
1563 (nth 4 breakpoint) " "
1564 (if (nth 5 breakpoint)
1565 (concat "in " (nth 5 breakpoint) " at " (nth 6 breakpoint) ":" (nth 7 breakpoint) "\n")
1566 (concat (nth 8 breakpoint) "\n")))))
1568 (gdb-break-list-custom))
1570 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1571 (defun gdb-break-list-custom ()
1572 (let ((flag) (bptno))
1573 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1574 (dolist (buffer (buffer-list))
1575 (with-current-buffer buffer
1576 (if (and (eq gud-minor-mode 'gdbmi)
1577 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1578 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1579 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1581 (goto-char (point-min))
1582 (while (< (point) (- (point-max) 1))
1584 (if (looking-at "[^\t].*?breakpoint")
1586 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1587 (setq bptno (match-string 1))
1588 (setq flag (char-after (match-beginning 2)))
1590 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1592 (let ((buffer-read-only nil))
1593 (add-text-properties (match-beginning 1) (match-end 1)
1594 '(face font-lock-function-name-face)))
1595 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1596 (let ((line (match-string 2)) (buffer-read-only nil)
1597 (file (match-string 1)))
1598 (add-text-properties (line-beginning-position)
1600 '(mouse-face highlight
1601 help-echo "mouse-2, RET: visit breakpoint"))
1602 (unless (file-exists-p file)
1603 (setq file (cdr (assoc bptno gdb-location-alist))))
1605 (not (string-equal file "File not found")))
1606 (with-current-buffer
1607 (find-file-noselect file 'nowarn)
1609 ;; Only want one breakpoint icon at each location.
1611 (goto-line (string-to-number line))
1612 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1614 (list (concat "list "
1615 (match-string-no-properties 3) ":1\n")
1618 (list "-file-list-exec-source-file\n"
1619 `(lambda () (gdb-get-location
1620 ,bptno ,line ,flag))))))))))))
1623 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
1625 (defun gdb-get-location (bptno line flag)
1626 "Find the directory containing the relevant source file.
1627 Put in buffer and place breakpoint icon."
1628 (goto-char (point-min))
1629 (catch 'file-not-found
1630 (if (re-search-forward gdb-source-file-regexp nil t)
1631 (delete (cons bptno "File not found") gdb-location-alist)
1632 (push (cons bptno (match-string 1)) gdb-location-alist)
1634 (unless (assoc bptno gdb-location-alist)
1635 (push (cons bptno "File not found") gdb-location-alist)
1636 (message-box "Cannot find source file for breakpoint location.
1637 Add directory to search path for source files using the GDB command, dir."))
1638 (throw 'file-not-found nil))
1639 (with-current-buffer (find-file-noselect (match-string 1))
1641 ;; only want one breakpoint icon at each location
1643 (goto-line (string-to-number line))
1644 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
1646 (add-hook 'find-file-hook 'gdb-find-file-hook)
1648 (defun gdb-find-file-hook ()
1649 "Set up buffer for debugging if file is part of the source code
1650 of the current session."
1651 (if (and (buffer-name gud-comint-buffer)
1652 ;; in case gud or gdb-ui is just loaded
1654 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
1656 (if (member buffer-file-name gdb-source-file-list)
1657 (with-current-buffer (find-buffer-visiting buffer-file-name)
1658 (gdb-init-buffer)))))
1660 (declare-function gud-remove "gdb-ui" t t) ; gud-def
1661 (declare-function gud-break "gdb-ui" t t) ; gud-def
1662 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
1664 (defun gdb-mouse-set-clear-breakpoint (event)
1665 "Set/clear breakpoint in left fringe/margin at mouse click.
1666 If not in a source or disassembly buffer just set point."
1668 (mouse-minibuffer-check event)
1669 (let ((posn (event-end event)))
1670 (with-selected-window (posn-window posn)
1671 (if (or (buffer-file-name) (eq major-mode 'gdb-assembler-mode))
1672 (if (numberp (posn-point posn))
1674 (goto-char (posn-point posn))
1675 (if (or (posn-object posn)
1676 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1680 (posn-set-point posn))))
1682 (defun gdb-mouse-toggle-breakpoint-margin (event)
1683 "Enable/disable breakpoint in left margin with mouse click."
1685 (mouse-minibuffer-check event)
1686 (let ((posn (event-end event)))
1687 (if (numberp (posn-point posn))
1688 (with-selected-window (posn-window posn)
1690 (goto-char (posn-point posn))
1691 (if (posn-object posn)
1693 (let ((bptno (get-text-property
1694 0 'gdb-bptno (car (posn-string posn)))))
1696 (if (get-text-property
1697 0 'gdb-enabled (car (posn-string posn)))
1702 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1703 "Enable/disable breakpoint in left fringe with mouse click."
1705 (mouse-minibuffer-check event)
1706 (let* ((posn (event-end event))
1707 (pos (posn-point posn))
1710 (with-selected-window (posn-window posn)
1712 (set-buffer (window-buffer (selected-window)))
1714 (dolist (overlay (overlays-in pos pos))
1715 (when (overlay-get overlay 'put-break)
1716 (setq obj (overlay-get overlay 'before-string))))
1720 (if (get-text-property 0 'gdb-enabled obj)
1723 (get-text-property 0 'gdb-bptno obj) "\n"))))))))
1725 (defun gdb-breakpoints-buffer-name ()
1726 (with-current-buffer gud-comint-buffer
1727 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1729 (def-gdb-display-buffer
1730 gdb-display-breakpoints-buffer
1731 'gdb-breakpoints-buffer
1732 "Display status of user-settable breakpoints.")
1734 (def-gdb-frame-for-buffer
1735 gdb-frame-breakpoints-buffer
1736 'gdb-breakpoints-buffer
1737 "Display status of user-settable breakpoints in a new frame.")
1739 (defvar gdb-breakpoints-mode-map
1740 (let ((map (make-sparse-keymap))
1741 (menu (make-sparse-keymap "Breakpoints")))
1742 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1743 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1744 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1745 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1746 (suppress-keymap map)
1747 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1748 (define-key map " " 'gdb-toggle-breakpoint)
1749 (define-key map "D" 'gdb-delete-breakpoint)
1750 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1751 (define-key map "q" 'gdb-delete-frame-or-window)
1752 (define-key map "\r" 'gdb-goto-breakpoint)
1753 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1754 (define-key map [follow-link] 'mouse-face)
1757 (defun gdb-delete-frame-or-window ()
1758 "Delete frame if there is only one window. Otherwise delete the window."
1760 (if (one-window-p) (delete-frame)
1763 ;;from make-mode-line-mouse-map
1764 (defun gdb-make-header-line-mouse-map (mouse function) "\
1765 Return a keymap with single entry for mouse key MOUSE on the header line.
1766 MOUSE is defined to run function FUNCTION with no args in the buffer
1767 corresponding to the mode line clicked."
1768 (let ((map (make-sparse-keymap)))
1769 (define-key map (vector 'header-line mouse) function)
1770 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1774 ;; uses "-thread-info". Needs GDB 7.0 onwards.
1777 (defun gdb-jump-to (file line)
1778 (find-file-other-window file)
1781 (define-button-type 'gdb-file-button
1782 'help-echo "Push to jump to source code"
1786 (gdb-jump-to (button-get b 'file)
1787 (button-get b 'line))))
1789 (defun gdb-insert-file-location-button (file line)
1790 "Insert text button which allows jumping to FILE:LINE.
1792 FILE is a full path."
1794 (format "%s:%d" (file-name-nondirectory file) line)
1795 :type 'gdb-file-button
1799 (defun gdb-threads-buffer-name ()
1800 (concat "*threads of " (gdb-get-target-string) "*"))
1802 (def-gdb-display-buffer
1803 gdb-display-threads-buffer
1805 "Display GDB threads.")
1807 (def-gdb-frame-for-buffer
1808 gdb-frame-threads-buffer
1810 "Display GDB threads in a new frame.")
1812 (gdb-set-buffer-rules 'gdb-threads-buffer
1813 'gdb-threads-buffer-name
1816 (def-gdb-auto-updated-buffer gdb-threads-buffer
1817 gdb-invalidate-threads "-thread-info\n"
1818 gdb-thread-list-handler gdb-thread-list-handler-custom)
1821 (defvar gdb-threads-font-lock-keywords
1822 '(("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1823 (" \\(stopped\\) in " (1 font-lock-warning-face))
1824 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
1825 "Font lock keywords used in `gdb-threads-mode'.")
1827 (defvar gdb-threads-mode-map
1829 (make-sparse-keymap))
1831 (defun gdb-threads-mode ()
1832 "Major mode for GDB threads.
1834 \\{gdb-threads-mode-map}"
1835 (kill-all-local-variables)
1836 (setq major-mode 'gdb-threads-mode)
1837 (setq mode-name "Threads")
1838 (use-local-map gdb-threads-mode-map)
1839 (setq buffer-read-only t)
1840 (buffer-disable-undo)
1841 (setq header-line-format gdb-breakpoints-header)
1842 (set (make-local-variable 'font-lock-defaults)
1843 '(gdb-threads-font-lock-keywords))
1844 (run-mode-hooks 'gdb-threads-mode-hook)
1845 'gdb-invalidate-threads)
1847 (defun gdb-thread-list-handler-custom ()
1848 (let* ((res (json-partial-output))
1849 (threads-list (fadr-q "res.threads")))
1850 (dolist (thread threads-list)
1851 (insert (fadr-format "~.id (~.target-id) ~.state in ~.frame.func " thread))
1854 (let ((args (fadr-q "thread.frame.args")))
1856 (insert (fadr-format "~.name=~.value," arg)))
1857 (when args (kill-backward-chars 1)))
1859 (gdb-insert-frame-location (fadr-q "thread.frame"))
1860 (insert (fadr-format " at ~.frame.addr\n" thread)))))
1864 (defun gdb-todo-memory ()
1867 "TODO: Implement memory buffer using\nMI command -data-read-memory"))
1869 ;;; Disassembly view
1871 (defun gdb-disassembly-buffer-name ()
1872 (concat "*disassembly of " (gdb-get-target-string) "*"))
1874 (def-gdb-display-buffer
1875 gdb-display-disassembly-buffer
1876 'gdb-disassembly-buffer
1877 "Display disassembly for current stack frame.")
1879 (def-gdb-frame-for-buffer
1880 gdb-frame-disassembly-buffer
1881 'gdb-disassembly-buffer
1882 "Display disassembly in a new frame.")
1884 (gdb-set-buffer-rules 'gdb-disassembly-buffer
1885 'gdb-disassembly-buffer-name
1886 'gdb-disassembly-mode)
1888 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
1889 (gdb-get-buffer-create 'gdb-disassembly-buffer)
1890 (let ((file (or gdb-selected-file gdb-main-file))
1891 (line (or gdb-selected-line 1)))
1893 (format "-data-disassemble -f %s -l %d -n -1 -- 0\n" file line)
1895 gdb-disassembly-handler)
1897 (def-gdb-auto-update-handler
1898 gdb-disassembly-handler
1899 gdb-invalidate-disassembly
1900 gdb-disassembly-buffer
1901 gdb-disassembly-handler-custom)
1903 (defvar gdb-disassembly-font-lock-keywords
1904 '(;; <__function.name+n>
1905 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1906 (1 font-lock-function-name-face))
1907 ;; 0xNNNNNNNN <__function.name+n>: opcode
1908 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
1909 (4 font-lock-keyword-face))
1910 ;; %register(at least i386)
1911 ("%\\sw+" . font-lock-variable-name-face)
1912 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
1913 (1 font-lock-comment-face)
1914 (2 font-lock-function-name-face))
1915 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
1916 "Font lock keywords used in `gdb-disassembly-mode'.")
1918 (defvar gdb-disassembly-mode-map
1920 (make-sparse-keymap))
1922 (defun gdb-disassembly-mode ()
1923 "Major mode for GDB disassembly information.
1925 \\{gdb-disassembly-mode-map}"
1926 (kill-all-local-variables)
1927 (setq major-mode 'gdb-disassembly-mode)
1928 (setq mode-name "Disassembly")
1929 (use-local-map gdb-disassembly-mode-map)
1930 (setq buffer-read-only t)
1931 (buffer-disable-undo)
1932 (set (make-local-variable 'font-lock-defaults)
1933 '(gdb-disassembly-font-lock-keywords))
1934 (run-mode-hooks 'gdb-disassembly-mode-hook)
1935 'gdb-invalidate-disassembly)
1937 (defun gdb-disassembly-handler-custom ()
1938 (let* ((res (json-partial-output))
1939 (instructions (fadr-member res ".asm_insns")))
1940 (dolist (instr instructions)
1941 (insert (fadr-format "~.address <~.func-name+~.offset>:\t~.inst\n" instr)))))
1944 ;;; Breakpoints view
1945 (defvar gdb-breakpoints-header
1946 `(,(propertize "Breakpoints"
1947 'help-echo "mouse-1: select"
1948 'mouse-face 'mode-line-highlight
1951 (gdb-make-header-line-mouse-map
1953 (lambda (event) (interactive "e")
1954 (save-selected-window
1955 (select-window (posn-window (event-start event)))
1956 (set-window-dedicated-p (selected-window) nil)
1958 (gdb-get-buffer-create 'gdb-breakpoints-buffer))
1959 (set-window-dedicated-p (selected-window) t)))))
1961 ,(propertize "Threads"
1962 'help-echo "mouse-1: select"
1963 'mouse-face 'mode-line-highlight
1966 (gdb-make-header-line-mouse-map
1968 ;; TODO: same code few lines above
1969 (lambda (event) (interactive "e")
1970 (save-selected-window
1971 (select-window (posn-window (event-start event)))
1972 (set-window-dedicated-p (selected-window) nil)
1974 (gdb-get-buffer-create 'gdb-threads-buffer))
1975 (set-window-dedicated-p (selected-window) t)))
1978 (defun gdb-breakpoints-mode ()
1979 "Major mode for gdb breakpoints.
1981 \\{gdb-breakpoints-mode-map}"
1982 (kill-all-local-variables)
1983 (setq major-mode 'gdb-breakpoints-mode)
1984 (setq mode-name "Breakpoints")
1985 (use-local-map gdb-breakpoints-mode-map)
1986 (setq buffer-read-only t)
1987 (buffer-disable-undo)
1988 (setq header-line-format gdb-breakpoints-header)
1989 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1990 'gdb-invalidate-breakpoints)
1992 (defconst gdb-breakpoint-regexp
1993 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1995 (defun gdb-toggle-breakpoint ()
1996 "Enable/disable breakpoint at current line."
1999 (beginning-of-line 1)
2000 (if (looking-at gdb-breakpoint-regexp)
2002 (concat (if (eq ?y (char-after (match-beginning 2)))
2006 (error "Not recognized as break/watchpoint line"))))
2008 (defun gdb-delete-breakpoint ()
2009 "Delete the breakpoint at current line."
2012 (beginning-of-line 1)
2013 (if (looking-at gdb-breakpoint-regexp)
2014 (gud-basic-call (concat "-break-delete " (match-string 1)))
2015 (error "Not recognized as break/watchpoint line"))))
2017 (defun gdb-goto-breakpoint (&optional event)
2018 "Display the breakpoint location specified at current line."
2019 (interactive (list last-input-event))
2020 (if event (posn-set-point (event-end event)))
2021 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
2022 (let ((window (get-buffer-window gud-comint-buffer)))
2023 (if window (save-selected-window (select-window window))))
2025 (beginning-of-line 1)
2026 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
2027 (let ((bptno (match-string 1))
2028 (file (match-string 2))
2029 (line (match-string 3)))
2030 (save-selected-window
2031 (let* ((buffer (find-file-noselect
2032 (if (file-exists-p file) file
2033 (cdr (assoc bptno gdb-location-alist)))))
2034 (window (or (gdb-display-source-buffer buffer)
2035 (display-buffer buffer))))
2036 (setq gdb-source-window window)
2037 (with-current-buffer buffer
2038 (goto-line (string-to-number line))
2039 (set-window-point window (point))))))
2040 (error "Not recognized as break/watchpoint line"))))
2043 ;; Frames buffer. This displays a perpetually correct bactrack trace.
2045 (gdb-set-buffer-rules 'gdb-stack-buffer
2046 'gdb-stack-buffer-name
2049 (def-gdb-auto-update-trigger gdb-invalidate-frames
2050 (gdb-get-buffer 'gdb-stack-buffer)
2051 "-stack-list-frames\n"
2052 gdb-stack-list-frames-handler)
2054 (defun gdb-insert-frame-location (frame)
2055 "Insert \"file:line\" button or library name for FRAME object."
2056 (let ((file (fadr-q "frame.fullname"))
2057 (line (fadr-q "frame.line"))
2058 (from (fadr-q "frame.from")))
2060 ;; Filename with line number
2062 (gdb-insert-file-location-button
2063 file (string-to-number line)))
2065 (from (insert (format " of %s" from))))))
2067 (defun gdb-stack-list-frames-handler ()
2068 (setq gdb-pending-triggers (delq 'gdb-invalidate-frames
2069 gdb-pending-triggers))
2070 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2071 (let* ((res (json-partial-output "frame"))
2072 (stack (fadr-q "res.stack"))
2073 (buf (gdb-get-buffer 'gdb-stack-buffer)))
2075 (with-current-buffer buf
2076 (let ((buffer-read-only nil))
2078 (dolist (frame (nreverse stack))
2079 (insert (fadr-expand "~.level in ~.func" frame))
2080 (gdb-insert-frame-location frame)
2082 (gdb-stack-list-frames-custom)))))))
2084 (defun gdb-stack-list-frames-custom ()
2085 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2087 (let ((buffer-read-only nil))
2088 (goto-char (point-min))
2090 (while (< (point) (point-max))
2091 (add-text-properties (point-at-bol) (1+ (point-at-bol))
2092 '(mouse-face highlight
2093 help-echo "mouse-2, RET: Select frame"))
2095 (when (and (looking-at "^[0-9]+\\s-+\\S-+\\s-+\\(\\S-+\\)")
2096 (equal (match-string 1) gdb-selected-frame))
2097 (if (> (car (window-fringes)) 0)
2099 (or gdb-stack-position
2100 (setq gdb-stack-position (make-marker)))
2101 (set-marker gdb-stack-position (point)))
2102 (let ((bl (point-at-bol)))
2103 (put-text-property bl (+ bl 4)
2104 'face '(:inverse-video t)))))
2105 (forward-line 1))))))
2107 (defun gdb-stack-buffer-name ()
2108 (with-current-buffer gud-comint-buffer
2109 (concat "*stack frames of " (gdb-get-target-string) "*")))
2111 (def-gdb-display-buffer
2112 gdb-display-stack-buffer
2114 "Display backtrace of current stack.")
2116 (def-gdb-frame-for-buffer
2117 gdb-frame-stack-buffer
2119 "Display backtrace of current stack in a new frame.")
2121 (defvar gdb-frames-mode-map
2122 (let ((map (make-sparse-keymap)))
2123 (suppress-keymap map)
2124 (define-key map "q" 'kill-this-buffer)
2125 (define-key map "\r" 'gdb-frames-select)
2126 (define-key map [mouse-2] 'gdb-frames-select)
2127 (define-key map [follow-link] 'mouse-face)
2130 (defvar gdb-frames-font-lock-keywords
2131 '(("in \\([^ ]+\\) of " (1 font-lock-function-name-face)))
2132 "Font lock keywords used in `gdb-frames-mode'.")
2134 (defun gdb-frames-mode ()
2135 "Major mode for gdb call stack.
2137 \\{gdb-frames-mode-map}"
2138 (kill-all-local-variables)
2139 (setq major-mode 'gdb-frames-mode)
2140 (setq mode-name "Frames")
2141 (setq gdb-stack-position nil)
2142 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2143 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2144 (setq buffer-read-only t)
2145 (buffer-disable-undo)
2146 (use-local-map gdb-frames-mode-map)
2147 (set (make-local-variable 'font-lock-defaults)
2148 '(gdb-frames-font-lock-keywords))
2149 (run-mode-hooks 'gdb-frames-mode-hook)
2150 'gdb-invalidate-frames)
2152 (defun gdb-get-frame-number ()
2155 (let* ((pos (re-search-backward "^\\([0-9]+\\)" nil t))
2156 (n (or (and pos (match-string-no-properties 1)) "0")))
2159 (defun gdb-frames-select (&optional event)
2160 "Select the frame and display the relevant source."
2161 (interactive (list last-input-event))
2162 (if event (posn-set-point (event-end event)))
2163 (gud-basic-call (concat "-stack-select-frame " (gdb-get-frame-number))))
2167 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
2168 (gdb-set-buffer-rules 'gdb-locals-buffer
2169 'gdb-locals-buffer-name
2172 (def-gdb-auto-update-trigger gdb-invalidate-locals
2173 (gdb-get-buffer 'gdb-locals-buffer)
2174 "-stack-list-locals --simple-values\n"
2175 gdb-stack-list-locals-handler)
2177 (defconst gdb-stack-list-locals-regexp
2178 (concat "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\""))
2180 (defvar gdb-locals-watch-map
2181 (let ((map (make-sparse-keymap)))
2182 (suppress-keymap map)
2183 (define-key map "\r" 'gud-watch)
2184 (define-key map [mouse-2] 'gud-watch)
2186 "Keymap to create watch expression of a complex data type local variable.")
2188 (defvar gdb-edit-locals-map-1
2189 (let ((map (make-sparse-keymap)))
2190 (suppress-keymap map)
2191 (define-key map "\r" 'gdb-edit-locals-value)
2192 (define-key map [mouse-2] 'gdb-edit-locals-value)
2194 "Keymap to edit value of a simple data type local variable.")
2196 (defun gdb-edit-locals-value (&optional event)
2197 "Assign a value to a variable displayed in the locals buffer."
2198 (interactive (list last-input-event))
2200 (if event (posn-set-point (event-end event)))
2202 (let* ((var (current-word))
2203 (value (read-string (format "New value (%s): " var))))
2205 (concat "-gdb-set variable " var " = " value)))))
2207 ;; Dont display values of arrays or structures.
2208 ;; These can be expanded using gud-watch.
2209 (defun gdb-stack-list-locals-handler nil
2210 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2211 gdb-pending-triggers))
2212 (let (local locals-list)
2213 (goto-char (point-min))
2214 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
2215 (let ((local (list (match-string 1)
2218 (if (looking-at ",value=\\(\".*\"\\)}")
2219 (setcar (nthcdr 2 local) (read (match-string 1))))
2220 (push local locals-list)))
2221 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2222 (and buf (with-current-buffer buf
2223 (let* ((window (get-buffer-window buf 0))
2224 (start (window-start window))
2225 (p (window-point window))
2226 (buffer-read-only nil) (name) (value))
2228 (dolist (local locals-list)
2229 (setq name (car local))
2230 (setq value (nth 2 local))
2232 (string-match "\\0x" value))
2233 (add-text-properties 0 (length name)
2234 `(mouse-face highlight
2235 help-echo "mouse-2: create watch expression"
2236 local-map ,gdb-locals-watch-map)
2238 (add-text-properties 0 (length value)
2239 `(mouse-face highlight
2240 help-echo "mouse-2: edit value"
2241 local-map ,gdb-edit-locals-map-1)
2244 (concat name "\t" (nth 1 local)
2245 "\t" (nth 2 local) "\n")))
2246 (set-window-start window start)
2247 (set-window-point window p)))))))
2249 (defvar gdb-locals-header
2250 `(,(propertize "Locals"
2251 'help-echo "mouse-1: select"
2252 'mouse-face 'mode-line-highlight
2255 (gdb-make-header-line-mouse-map
2257 (lambda (event) (interactive "e")
2258 (save-selected-window
2259 (select-window (posn-window (event-start event)))
2260 (set-window-dedicated-p (selected-window) nil)
2262 (gdb-get-buffer-create 'gdb-locals-buffer))
2263 (set-window-dedicated-p (selected-window) t)))))
2265 ,(propertize "Registers"
2266 'help-echo "mouse-1: select"
2267 'mouse-face 'mode-line-highlight
2270 (gdb-make-header-line-mouse-map
2272 (lambda (event) (interactive "e")
2273 (save-selected-window
2274 (select-window (posn-window (event-start event)))
2275 (set-window-dedicated-p (selected-window) nil)
2277 (gdb-get-buffer-create 'gdb-registers-buffer))
2278 (set-window-dedicated-p (selected-window) t)))))))
2280 (defvar gdb-locals-mode-map
2281 (let ((map (make-sparse-keymap)))
2282 (suppress-keymap map)
2283 (define-key map "q" 'kill-this-buffer)
2286 (defun gdb-locals-mode ()
2287 "Major mode for gdb locals.
2289 \\{gdb-locals-mode-map}"
2290 (kill-all-local-variables)
2291 (setq major-mode 'gdb-locals-mode)
2292 (setq mode-name (concat "Locals:" gdb-selected-frame))
2293 (setq buffer-read-only t)
2294 (buffer-disable-undo)
2295 (setq header-line-format gdb-locals-header)
2296 (use-local-map gdb-locals-mode-map)
2297 (set (make-local-variable 'font-lock-defaults)
2298 '(gdb-locals-font-lock-keywords))
2299 (run-mode-hooks 'gdb-locals-mode-hook)
2300 'gdb-invalidate-locals)
2302 (defun gdb-locals-buffer-name ()
2303 (with-current-buffer gud-comint-buffer
2304 (concat "*locals of " (gdb-get-target-string) "*")))
2306 (def-gdb-display-buffer
2307 gdb-display-locals-buffer
2309 "Display local variables of current stack and their values.")
2311 (def-gdb-frame-for-buffer
2312 gdb-frame-locals-buffer
2314 "Display local variables of current stack and their values in a new frame.")
2317 ;; Registers buffer.
2319 (gdb-set-buffer-rules 'gdb-registers-buffer
2320 'gdb-registers-buffer-name
2321 'gdb-registers-mode)
2323 (def-gdb-auto-update-trigger gdb-invalidate-registers
2324 (gdb-get-buffer 'gdb-registers-buffer)
2325 "-data-list-register-values x\n"
2326 gdb-data-list-register-values-handler)
2328 (defconst gdb-data-list-register-values-regexp
2329 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
2331 (defun gdb-data-list-register-values-handler ()
2332 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers
2333 gdb-pending-triggers))
2334 (goto-char (point-min))
2335 (if (re-search-forward gdb-error-regexp nil t)
2338 (setq match (match-string 1))
2339 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2340 (let ((buffer-read-only nil))
2343 (goto-char (point-min))))))
2344 (let ((register-list (reverse gdb-register-names))
2345 (register nil) (register-string nil) (register-values nil))
2346 (goto-char (point-min))
2347 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
2348 (setq register (pop register-list))
2349 (setq register-string (concat register "\t" (match-string 2) "\n"))
2350 (if (member (match-string 1) gdb-changed-registers)
2351 (put-text-property 0 (length register-string)
2352 'face 'font-lock-warning-face
2354 (setq register-values
2355 (concat register-values register-string)))
2356 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
2357 (with-current-buffer buf
2358 (let ((p (window-point (get-buffer-window buf 0)))
2359 (buffer-read-only nil))
2361 (insert register-values)
2362 (set-window-point (get-buffer-window buf 0) p))))))
2363 (gdb-data-list-register-values-custom))
2365 (defun gdb-data-list-register-values-custom ()
2366 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2368 (let ((buffer-read-only nil)
2370 (goto-char (point-min))
2371 (while (< (point) (point-max))
2372 (setq bl (line-beginning-position))
2373 (when (looking-at "^[^\t]+")
2374 (put-text-property bl (match-end 0)
2375 'face font-lock-variable-name-face))
2376 (forward-line 1))))))
2378 (defvar gdb-registers-mode-map
2379 (let ((map (make-sparse-keymap)))
2380 (suppress-keymap map)
2381 (define-key map "q" 'kill-this-buffer)
2384 (defun gdb-registers-mode ()
2385 "Major mode for gdb registers.
2387 \\{gdb-registers-mode-map}"
2388 (kill-all-local-variables)
2389 (setq major-mode 'gdb-registers-mode)
2390 (setq mode-name "Registers")
2391 (setq header-line-format gdb-locals-header)
2392 (setq buffer-read-only t)
2393 (buffer-disable-undo)
2394 (use-local-map gdb-registers-mode-map)
2395 (run-mode-hooks 'gdb-registers-mode-hook)
2396 'gdb-invalidate-registers)
2398 (defun gdb-registers-buffer-name ()
2399 (with-current-buffer gud-comint-buffer
2400 (concat "*registers of " (gdb-get-target-string) "*")))
2402 (def-gdb-display-buffer
2403 gdb-display-registers-buffer
2404 'gdb-registers-buffer
2405 "Display integer register contents.")
2407 (def-gdb-frame-for-buffer
2408 gdb-frame-registers-buffer
2409 'gdb-registers-buffer
2410 "Display integer register contents in a new frame.")
2412 ;; Needs GDB 6.4 onwards (used to fail with no stack).
2413 (defun gdb-get-changed-registers ()
2414 (if (and (gdb-get-buffer 'gdb-registers-buffer)
2415 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
2419 "-data-list-changed-registers\n"
2420 'gdb-get-changed-registers-handler))
2421 (push 'gdb-get-changed-registers gdb-pending-triggers))))
2423 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
2425 (defun gdb-get-changed-registers-handler ()
2426 (setq gdb-pending-triggers
2427 (delq 'gdb-get-changed-registers gdb-pending-triggers))
2428 (setq gdb-changed-registers nil)
2429 (goto-char (point-min))
2430 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
2431 (push (match-string 1) gdb-changed-registers)))
2433 (defun gdb-get-register-names ()
2434 "Create a list of register names."
2435 (goto-char (point-min))
2436 (setq gdb-register-names nil)
2437 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
2438 (push (match-string 1) gdb-register-names)))
2441 (defun gdb-get-source-file-list ()
2442 "Create list of source files for current GDB session.
2443 If buffers already exist for any of these files, gud-minor-mode
2445 (goto-char (point-min))
2446 (while (re-search-forward gdb-source-file-regexp nil t)
2447 (push (match-string 1) gdb-source-file-list))
2448 (dolist (buffer (buffer-list))
2449 (with-current-buffer buffer
2450 (when (member buffer-file-name gdb-source-file-list)
2451 (gdb-init-buffer))))
2452 (gdb-force-mode-line-update
2453 (propertize "ready" 'face font-lock-variable-name-face)))
2455 (defun gdb-get-selected-frame ()
2456 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2459 (list "-stack-info-frame\n" 'gdb-frame-handler))
2460 (push 'gdb-get-selected-frame
2461 gdb-pending-triggers))))
2463 (defun gdb-frame-handler ()
2464 (setq gdb-pending-triggers
2465 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2466 (let ((frame (fadr-member (json-partial-output) ".frame")))
2468 (setq gdb-frame-number (fadr-q "frame.level"))
2469 (setq gdb-pc-address (fadr-q "frame.addr"))
2470 (setq gdb-selected-frame (fadr-q "frame.func"))
2471 (setq gdb-selected-file (fadr-q "frame.fullname"))
2472 (let ((line (fadr-q "frame.line")))
2473 (setq gdb-selected-line (or (and line (string-to-number line))
2474 nil)) ; don't fail if line is nil
2475 (when line ; obey the current file only if we have line info
2476 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
2477 (gud-display-frame)))
2478 (if (gdb-get-buffer 'gdb-locals-buffer)
2479 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2480 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2481 (if (gdb-get-buffer 'gdb-disassembly-buffer)
2482 (with-current-buffer (gdb-get-buffer 'gdb-disassembly-buffer)
2483 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2484 (if gud-overlay-arrow-position
2485 (let ((buffer (marker-buffer gud-overlay-arrow-position))
2486 (position (marker-position gud-overlay-arrow-position)))
2488 (with-current-buffer buffer
2489 (setq fringe-indicator-alist
2490 (if (string-equal gdb-frame-number "0")
2492 '((overlay-arrow . hollow-right-triangle))))
2493 (setq gud-overlay-arrow-position (make-marker))
2494 (set-marker gud-overlay-arrow-position position)))))
2495 (when gdb-selected-line
2496 (gdb-invalidate-disassembly)))))
2498 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
2500 (defun gdb-get-prompt ()
2501 "Find prompt for GDB session."
2502 (goto-char (point-min))
2503 (setq gdb-prompt-name nil)
2504 (re-search-forward gdb-prompt-name-regexp nil t)
2505 (setq gdb-prompt-name (match-string 1))
2506 ;; Insert first prompt.
2507 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2509 ;;;; Window management
2510 (defun gdb-display-buffer (buf dedicated &optional frame)
2511 (let ((answer (get-buffer-window buf (or frame 0))))
2513 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
2514 (let ((window (get-lru-window)))
2515 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
2517 (let* ((largest (get-largest-window))
2518 (cur-size (window-height largest)))
2519 (setq answer (split-window largest))
2520 (set-window-buffer answer buf)
2521 (set-window-dedicated-p answer dedicated)
2523 (set-window-buffer window buf)
2527 ;;; Shared keymap initialization:
2529 (let ((menu (make-sparse-keymap "GDB-Windows")))
2530 (define-key gud-menu-map [displays]
2531 `(menu-item "GDB-Windows" ,menu
2532 :visible (eq gud-minor-mode 'gdbmi)))
2533 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2534 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2535 ; (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2536 (define-key menu [memory] '("Memory" . gdb-todo-memory))
2537 (define-key menu [disassembly]
2538 '("Disassembly" . gdb-display-disassembly-buffer))
2539 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2540 (define-key menu [inferior]
2541 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2542 :enable gdb-use-separate-io-buffer))
2543 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2544 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2545 (define-key menu [breakpoints]
2546 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2548 (let ((menu (make-sparse-keymap "GDB-Frames")))
2549 (define-key gud-menu-map [frames]
2550 `(menu-item "GDB-Frames" ,menu
2551 :visible (eq gud-minor-mode 'gdbmi)))
2552 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2553 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2554 ; (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2555 (define-key menu [memory] '("Memory" . gdb-todo-memory))
2556 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
2557 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2558 (define-key menu [inferior]
2559 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2560 :enable gdb-use-separate-io-buffer))
2561 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2562 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2563 (define-key menu [breakpoints]
2564 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2566 (let ((menu (make-sparse-keymap "GDB-MI")))
2567 (define-key gud-menu-map [mi]
2568 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi)))
2569 (define-key menu [gdb-customize]
2570 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
2571 :help "Customize Gdb Graphical Mode options."))
2572 (define-key menu [gdb-use-separate-io]
2573 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2574 :help "Toggle separate IO for debugged program."
2575 :button (:toggle . gdb-use-separate-io-buffer)))
2576 (define-key menu [gdb-many-windows]
2577 '(menu-item "Display Other Windows" gdb-many-windows
2578 :help "Toggle display of locals, stack and breakpoint information"
2579 :button (:toggle . gdb-many-windows)))
2580 (define-key menu [gdb-restore-windows]
2581 '(menu-item "Restore Window Layout" gdb-restore-windows
2582 :help "Restore standard layout for debug session.")))
2584 (defun gdb-frame-gdb-buffer ()
2585 "Display GUD buffer in a new frame."
2587 (let ((special-display-regexps (append special-display-regexps '(".*")))
2588 (special-display-frame-alist
2589 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2590 gdb-frame-parameters)))
2591 (same-window-regexps nil))
2592 (display-buffer gud-comint-buffer)))
2594 (defun gdb-display-gdb-buffer ()
2595 "Display GUD buffer."
2597 (let ((same-window-regexps nil))
2598 (select-window (display-buffer gud-comint-buffer nil 0))))
2600 (defun gdb-set-window-buffer (name)
2601 (set-window-buffer (selected-window) (get-buffer name))
2602 (set-window-dedicated-p (selected-window) t))
2604 (defun gdb-setup-windows ()
2605 "Layout the window pattern for `gdb-many-windows'."
2606 (gdb-display-locals-buffer)
2607 (gdb-display-stack-buffer)
2608 (delete-other-windows)
2609 (gdb-display-breakpoints-buffer)
2610 (delete-other-windows)
2612 (pop-to-buffer gud-comint-buffer)
2613 (split-window nil ( / ( * (window-height) 3) 4))
2614 (split-window nil ( / (window-height) 3))
2615 (split-window-horizontally)
2617 (gdb-set-window-buffer (gdb-locals-buffer-name))
2620 (if gud-last-last-frame
2621 (gud-find-file (car gud-last-last-frame))
2623 (gud-find-file gdb-main-file)
2624 ;; Put buffer list in window if we
2625 ;; can't find a source file.
2626 (list-buffers-noselect))))
2627 (setq gdb-source-window (selected-window))
2628 (when gdb-use-separate-io-buffer
2629 (split-window-horizontally)
2631 (gdb-set-window-buffer
2632 (gdb-get-buffer-create 'gdb-inferior-io)))
2634 (gdb-set-window-buffer (gdb-stack-buffer-name))
2635 (split-window-horizontally)
2637 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2640 (defcustom gdb-many-windows nil
2641 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
2642 In this case it starts with two windows: one displaying the GUD
2643 buffer and the other with the source file with the main routine
2644 of the debugged program. Non-nil means display the layout shown for
2650 (defun gdb-many-windows (arg)
2651 "Toggle the number of windows in the basic arrangement.
2652 With arg, display additional buffers iff arg is positive."
2654 (setq gdb-many-windows
2656 (not gdb-many-windows)
2657 (> (prefix-numeric-value arg) 0)))
2658 (message (format "Display of other windows %sabled"
2659 (if gdb-many-windows "en" "dis")))
2660 (if (and gud-comint-buffer
2661 (buffer-name gud-comint-buffer))
2663 (gdb-restore-windows)
2666 (defun gdb-restore-windows ()
2667 "Restore the basic arrangement of windows used by gdb.
2668 This arrangement depends on the value of `gdb-many-windows'."
2670 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2671 (delete-other-windows)
2672 (if gdb-many-windows
2674 (when (or gud-last-last-frame gdb-show-main)
2678 (if gud-last-last-frame
2679 (gud-find-file (car gud-last-last-frame))
2680 (gud-find-file gdb-main-file)))
2681 (setq gdb-source-window (selected-window))
2685 "Exit a debugging session cleanly.
2686 Kills the gdb buffers, and resets variables and the source buffers."
2687 (dolist (buffer (buffer-list))
2688 (unless (eq buffer gud-comint-buffer)
2689 (with-current-buffer buffer
2690 (if (eq gud-minor-mode 'gdbmi)
2691 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
2693 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2694 (setq gud-minor-mode nil)
2695 (kill-local-variable 'tool-bar-map)
2696 (kill-local-variable 'gdb-define-alist))))))
2697 (setq gdb-overlay-arrow-position nil)
2698 (setq overlay-arrow-variable-list
2699 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2700 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2701 (setq gdb-stack-position nil)
2702 (setq overlay-arrow-variable-list
2703 (delq 'gdb-stack-position overlay-arrow-variable-list))
2704 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2705 (setq gud-running nil)
2706 (setq gdb-active-process nil)
2707 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2709 (defun gdb-get-source-file ()
2710 "Find the source file where the program starts and display it with related
2711 buffers, if required."
2712 (goto-char (point-min))
2713 (if (re-search-forward gdb-source-file-regexp nil t)
2714 (setq gdb-main-file (match-string 1)))
2715 (if gdb-many-windows
2717 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2719 (let ((pop-up-windows t))
2720 (display-buffer (gud-find-file gdb-main-file))))))
2723 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2724 "Put string PUTSTRING in front of POS in the current buffer.
2725 PUTSTRING is displayed by putting an overlay into the current buffer with a
2726 `before-string' string that has a `display' property whose value is
2728 (let ((string (make-string 1 ?x))
2729 (buffer (current-buffer)))
2730 (setq putstring (copy-sequence putstring))
2731 (let ((overlay (make-overlay pos pos buffer))
2733 (list (list 'margin 'left-margin) putstring))))
2734 (put-text-property 0 1 'display prop string)
2736 (add-text-properties 0 1 sprops string))
2737 (overlay-put overlay 'put-break t)
2738 (overlay-put overlay 'before-string string))))
2740 ;;from remove-images
2741 (defun gdb-remove-strings (start end &optional buffer)
2742 "Remove strings between START and END in BUFFER.
2743 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2744 BUFFER nil or omitted means use the current buffer."
2746 (setq buffer (current-buffer)))
2747 (dolist (overlay (overlays-in start end))
2748 (when (overlay-get overlay 'put-break)
2749 (delete-overlay overlay))))
2751 (defun gdb-put-breakpoint-icon (enabled bptno)
2752 (let ((start (- (line-beginning-position) 1))
2753 (end (+ (line-end-position) 1))
2754 (putstring (if enabled "B" "b"))
2755 (source-window (get-buffer-window (current-buffer) 0)))
2756 (add-text-properties
2757 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2760 (add-text-properties
2761 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2762 (add-text-properties
2763 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2764 (gdb-remove-breakpoint-icons start end)
2765 (if (display-images-p)
2766 (if (>= (or left-fringe-width
2767 (if source-window (car (window-fringes source-window)))
2768 gdb-buffer-fringe-width) 8)
2771 `(left-fringe breakpoint
2774 'breakpoint-disabled))
2776 'gdb-enabled enabled)
2777 (when (< left-margin-width 2)
2778 (save-current-buffer
2779 (setq left-margin-width 2)
2783 left-margin-width right-margin-width))))
2786 (or breakpoint-enabled-icon
2787 (setq breakpoint-enabled-icon
2788 (find-image `((:type xpm :data
2789 ,breakpoint-xpm-data
2790 :ascent 100 :pointer hand)
2792 ,breakpoint-enabled-pbm-data
2793 :ascent 100 :pointer hand)))))
2794 (or breakpoint-disabled-icon
2795 (setq breakpoint-disabled-icon
2796 (find-image `((:type xpm :data
2797 ,breakpoint-xpm-data
2798 :conversion disabled
2799 :ascent 100 :pointer hand)
2801 ,breakpoint-disabled-pbm-data
2802 :ascent 100 :pointer hand))))))
2806 (when (< left-margin-width 2)
2807 (save-current-buffer
2808 (setq left-margin-width 2)
2809 (let ((window (get-buffer-window (current-buffer) 0)))
2812 window left-margin-width right-margin-width)))))
2814 (propertize putstring
2815 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2818 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2819 (gdb-remove-strings start end)
2820 (if (display-images-p)
2821 (remove-images start end))
2823 (setq left-margin-width 0)
2824 (let ((window (get-buffer-window (current-buffer) 0)))
2827 window left-margin-width right-margin-width)))))
2831 ;;; gdb-mi.el ends here