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 gdb-def-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 gdb-def-display-buffer (name buffer &optional doc)
934 "Define a function NAME which shows gdb BUFFER.
936 DOC is an optional documentation string."
940 (gdb-get-buffer-create ,name) t)))
943 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
944 ;; at least one and possible more functions. The functions have these
945 ;; roles in defining a buffer type:
947 ;; NAME - Return a name for this buffer type.
949 ;; The remaining function(s) are optional:
951 ;; MODE - called in a new buffer with no arguments, should establish
952 ;; the proper mode for the buffer.
955 (defun gdb-set-buffer-rules (buffer-type &rest rules)
956 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
958 (setcdr binding rules)
959 (push (cons buffer-type rules)
960 gdb-buffer-rules-assoc))))
962 ;; GUD buffers are an exception to the rules
963 (gdb-set-buffer-rules 'gdbmi 'error)
965 ;; Partial-output buffer : This accumulates output from a command executed on
966 ;; behalf of emacs (rather than the user).
968 (gdb-set-buffer-rules 'gdb-partial-output-buffer
969 'gdb-partial-output-name)
971 (defun gdb-partial-output-name ()
972 (concat " *partial-output-"
973 (gdb-get-target-string)
977 (gdb-set-buffer-rules 'gdb-inferior-io
978 'gdb-inferior-io-name
979 'gdb-inferior-io-mode)
981 (defun gdb-inferior-io-name ()
982 (concat "*input/output of "
983 (gdb-get-target-string)
986 (defun gdb-display-separate-io-buffer ()
987 "Display IO of debugged program in a separate window."
989 (if gdb-use-separate-io-buffer
991 (gdb-get-buffer-create 'gdb-inferior-io) t)))
993 (defconst gdb-frame-parameters
994 '((height . 14) (width . 80)
996 (tool-bar-lines . nil)
997 (menu-bar-lines . nil)
1000 (defun gdb-frame-separate-io-buffer ()
1001 "Display IO of debugged program in a new frame."
1003 (if gdb-use-separate-io-buffer
1004 (let ((special-display-regexps (append special-display-regexps '(".*")))
1005 (special-display-frame-alist gdb-frame-parameters))
1006 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1008 (defvar gdb-inferior-io-mode-map
1009 (let ((map (make-sparse-keymap)))
1010 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1011 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1012 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1013 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1014 (define-key map "\C-d" 'gdb-separate-io-eof)
1017 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1018 "Major mode for gdb inferior-io."
1019 :syntax-table nil :abbrev-table nil
1020 ;; We want to use comint because it has various nifty and familiar
1021 ;; features. We don't need a process, but comint wants one, so create
1023 (make-comint-in-buffer
1024 "gdb-inferior" (current-buffer) "sleep" nil "1000000000"))
1026 (defun gdb-inferior-filter (proc string)
1027 (unless (string-equal string "")
1028 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1029 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1030 (insert-before-markers string)))
1032 (defun gdb-separate-io-interrupt ()
1033 "Interrupt the program being debugged."
1036 (get-buffer-process gud-comint-buffer) comint-ptyp))
1038 (defun gdb-separate-io-quit ()
1039 "Send quit signal to the program being debugged."
1042 (get-buffer-process gud-comint-buffer) comint-ptyp))
1044 (defun gdb-separate-io-stop ()
1045 "Stop the program being debugged."
1048 (get-buffer-process gud-comint-buffer) comint-ptyp))
1050 (defun gdb-separate-io-eof ()
1051 "Send end-of-file to the program being debugged."
1054 (get-buffer-process gud-comint-buffer)))
1056 (defun gdb-clear-inferior-io ()
1057 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1061 (defconst breakpoint-xpm-data
1063 static char *magick[] = {
1064 /* columns rows colors chars-per-pixel */
1080 "XPM data used for breakpoint icon.")
1082 (defconst breakpoint-enabled-pbm-data
1085 0 0 0 0 1 1 1 1 0 0 0 0
1086 0 0 0 1 1 1 1 1 1 0 0 0
1087 0 0 1 1 1 1 1 1 1 1 0 0
1088 0 1 1 1 1 1 1 1 1 1 1 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 0 1 1 1 1 1 1 1 1 0 0
1093 0 0 0 1 1 1 1 1 1 0 0 0
1094 0 0 0 0 1 1 1 1 0 0 0 0"
1095 "PBM data used for enabled breakpoint icon.")
1097 (defconst breakpoint-disabled-pbm-data
1109 0 0 0 1 0 1 0 1 0 0"
1110 "PBM data used for disabled breakpoint icon.")
1112 (defvar breakpoint-enabled-icon nil
1113 "Icon for enabled breakpoint in display margin.")
1115 (defvar breakpoint-disabled-icon nil
1116 "Icon for disabled breakpoint in display margin.")
1118 (declare-function define-fringe-bitmap "fringe.c"
1119 (bitmap bits &optional height width align))
1121 (and (display-images-p)
1122 ;; Bitmap for breakpoint in fringe
1123 (define-fringe-bitmap 'breakpoint
1124 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1125 ;; Bitmap for gud-overlay-arrow in fringe
1126 (define-fringe-bitmap 'hollow-right-triangle
1127 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1129 (defface breakpoint-enabled
1133 "Face for enabled breakpoint icon in fringe."
1136 (defface breakpoint-disabled
1137 '((((class color) (min-colors 88)) :foreground "grey70")
1138 ;; Ensure that on low-color displays that we end up something visible.
1139 (((class color) (min-colors 8) (background light))
1140 :foreground "black")
1141 (((class color) (min-colors 8) (background dark))
1142 :foreground "white")
1143 (((type tty) (class mono))
1145 (t :background "gray"))
1146 "Face for disabled breakpoint icon in fringe."
1150 (defun gdb-send (proc string)
1151 "A comint send filter for gdb."
1152 (with-current-buffer gud-comint-buffer
1153 (let ((inhibit-read-only t))
1154 (remove-text-properties (point-min) (point-max) '(face))))
1155 ;; mimic <RET> key to repeat previous command in GDB
1156 (if (not (string-match "^\\s+$" string))
1157 (setq gdb-last-command string)
1158 (if gdb-last-command (setq string gdb-last-command)))
1159 (if gdb-enable-debug
1160 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1161 (if (string-match "^-" string)
1164 (setq gdb-first-done-or-error t)
1165 (process-send-string proc (concat string "\n")))
1167 (if (string-match "\\\\$" string)
1168 (setq gdb-continuation (concat gdb-continuation string "\n"))
1169 (setq gdb-first-done-or-error t)
1170 (process-send-string proc (concat "-interpreter-exec console \""
1171 gdb-continuation string "\"\n"))
1172 (setq gdb-continuation nil))))
1174 (defun gdb-input (item)
1175 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1176 (setq gdb-token-number (1+ gdb-token-number))
1177 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1178 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1179 (process-send-string (get-buffer-process gud-comint-buffer)
1183 (defcustom gud-gdb-command-name "gdb -i=mi"
1184 "Default command to execute an executable under the GDB debugger."
1189 (setq gud-running nil)
1190 (setq gdb-output-sink 'user)
1191 (setq gdb-pending-triggers nil))
1193 (defun gdb-update ()
1194 "Update buffers showing status of debug session."
1195 (when gdb-first-prompt
1196 (gdb-force-mode-line-update
1197 (propertize "initializing..." 'face font-lock-variable-name-face))
1199 (setq gdb-first-prompt nil))
1200 (gdb-get-selected-frame)
1201 (gdb-invalidate-frames)
1202 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1203 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1204 (gdb-invalidate-breakpoints)
1205 (gdb-invalidate-threads)
1206 (gdb-get-changed-registers)
1207 (gdb-invalidate-registers)
1208 (gdb-invalidate-locals)
1209 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1210 (dolist (var gdb-var-list)
1211 (setcar (nthcdr 5 var) nil))
1214 ;; GUD displays the selected GDB frame. This might might not be the current
1215 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1216 ;; visited breakpoint is, use that window.
1217 (defun gdb-display-source-buffer (buffer)
1218 (let* ((last-window (if gud-last-last-frame
1220 (gud-find-file (car gud-last-last-frame)))))
1221 (source-window (or last-window
1222 (if (and gdb-source-window
1223 (window-live-p gdb-source-window))
1224 gdb-source-window))))
1226 (setq gdb-source-window source-window)
1227 (set-window-buffer source-window buffer))
1230 (defun gdb-car< (a b)
1231 (< (car a) (car b)))
1233 (defvar gdbmi-record-list
1234 '((gdb-gdb . "(gdb) \n")
1235 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1236 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1237 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1238 (gdb-console . "~\\(\".*?\"\\)\n")
1239 (gdb-internals . "&\\(\".*?\"\\)\n")
1240 (gdb-stopped . "\\*stopped,?\\(.*?\n\\)")
1241 (gdb-running . "\\*running,\\(.*?\n\\)")
1242 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1243 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")))
1245 (defun gud-gdbmi-marker-filter (string)
1246 "Filter GDB/MI output."
1248 ;; Record transactions if logging is enabled.
1249 (when gdb-enable-debug
1250 (push (cons 'recv string) gdb-debug-log)
1251 (if (and gdb-debug-log-max
1252 (> (length gdb-debug-log) gdb-debug-log-max))
1253 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1255 ;; Recall the left over gud-marker-acc from last time
1256 (setq gud-marker-acc (concat gud-marker-acc string))
1258 ;; Start accumulating output for the GUD buffer
1259 (setq gdb-filter-output "")
1260 (let ((output-record) (output-record-list))
1262 ;; Process all the complete markers in this chunk.
1263 (dolist (gdbmi-record gdbmi-record-list)
1264 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1265 (push (list (match-beginning 0)
1267 (match-string 1 gud-marker-acc)
1268 (match-string 2 gud-marker-acc)
1271 (setq gud-marker-acc
1272 (concat (substring gud-marker-acc 0 (match-beginning 0))
1273 ;; Pad with spaces to preserve position.
1274 (make-string (length (match-string 0 gud-marker-acc)) 32)
1275 (substring gud-marker-acc (match-end 0))))))
1277 (setq output-record-list (sort output-record-list 'gdb-car<))
1279 (dolist (output-record output-record-list)
1280 (let ((record-type (cadr output-record))
1281 (arg1 (caddr output-record))
1282 (arg2 (cadddr output-record)))
1283 (if (eq record-type 'gdb-error)
1284 (gdb-done-or-error arg2 arg1 'error)
1285 (if (eq record-type 'gdb-done)
1286 (gdb-done-or-error arg2 arg1 'done)
1287 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1288 ;; error message on internal stream. Don't print to GUD buffer.
1289 (unless (and (eq record-type 'gdb-internals)
1290 (string-equal (read arg1) "No registers.\n"))
1291 (funcall record-type arg1))))))
1293 (setq gdb-output-sink 'user)
1295 (string-match "^ *" gud-marker-acc)
1296 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1300 (defun gdb-gdb (output-field))
1301 (defun gdb-thread-created (output-field))
1302 (defun gdb-thread-exited (output-field))
1304 (defun gdb-running (output-field)
1305 (setq gdb-inferior-status "running")
1306 (gdb-force-mode-line-update
1307 (propertize gdb-inferior-status 'face font-lock-type-face))
1308 (setq gdb-active-process t)
1309 (setq gud-running t))
1311 (defun gdb-starting (output-field)
1312 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1313 (setq gdb-inferior-status "running")
1314 (gdb-force-mode-line-update
1315 (propertize gdb-inferior-status 'face font-lock-type-face))
1316 (setq gdb-active-process t)
1317 (setq gud-running t))
1319 ;; -break-insert -t didn't give a reason before gdb 6.9
1320 (defconst gdb-stopped-regexp
1321 "\\(reason=\"\\(.*?\\)\"\\)?\\(\\(,exit-code=.*?\\)*\n\\|.*?,file=\".*?\".*?,fullname=\"\\(.*?\\)\".*?,line=\"\\(.*?\\)\".*?\n\\)")
1323 (defun gdb-stopped (output-field)
1324 (setq gud-running nil)
1325 (string-match gdb-stopped-regexp output-field)
1326 (let ((reason (match-string 2 output-field))
1327 (file (match-string 5 output-field)))
1329 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1330 ;;; because synchronous GDB doesn't give these fields with CLI.
1333 ;;; ;; Extract the frame position from the marker.
1334 ;;; gud-last-frame (cons file
1335 ;;; (string-to-number
1336 ;;; (match-string 6 gud-marker-acc)))))
1338 (setq gdb-inferior-status (if reason reason "unknown"))
1339 (gdb-force-mode-line-update
1340 (propertize gdb-inferior-status 'face font-lock-warning-face))
1341 (if (string-equal reason "exited-normally")
1342 (setq gdb-active-process nil)))
1344 (when gdb-first-done-or-error
1345 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name))
1347 (setq gdb-first-done-or-error nil)))
1349 ;; Remove the trimmings from log stream containing debugging messages
1350 ;; being produced by GDB's internals, use warning face and send to GUD
1352 (defun gdb-internals (output-field)
1353 (setq gdb-filter-output
1356 (let ((error-message
1357 (read output-field)))
1359 0 (length error-message)
1360 'face font-lock-warning-face
1364 ;; Remove the trimmings from the console stream and send to GUD buffer
1365 ;; (frontend MI commands should not print to this stream)
1366 (defun gdb-console (output-field)
1367 (setq gdb-filter-output
1370 (read output-field))))
1372 (defun gdb-done-or-error (output-field token-number type)
1373 (if (string-equal token-number "")
1374 ;; Output from command entered by user
1376 (setq gdb-output-sink 'user)
1377 (setq token-number nil)
1378 ;; MI error - send to minibuffer
1379 (when (eq type 'error)
1380 ;; Skip "msg=" from `output-field'
1381 (message (read (substring output-field 4)))
1382 ;; Don't send to the console twice. (If it is a console error
1383 ;; it is also in the console stream.)
1384 (setq output-field nil)))
1385 ;; Output from command from frontend.
1386 (setq gdb-output-sink 'emacs))
1388 (gdb-clear-partial-output)
1389 (when gdb-first-done-or-error
1390 (unless (or token-number gud-running)
1391 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1393 (setq gdb-first-done-or-error nil))
1395 (setq gdb-filter-output
1396 (gdb-concat-output gdb-filter-output output-field))
1400 (with-current-buffer
1401 (gdb-get-buffer-create 'gdb-partial-output-buffer)
1403 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
1404 (setq gdb-handler-alist
1405 (assq-delete-all token-number gdb-handler-alist)))))
1407 (defun gdb-concat-output (so-far new)
1408 (let ((sink gdb-output-sink))
1410 ((eq sink 'user) (concat so-far new))
1412 (gdb-append-to-partial-output new)
1415 (defun gdb-append-to-partial-output (string)
1416 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1417 (goto-char (point-max))
1420 (defun gdb-clear-partial-output ()
1421 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1424 (defun json-partial-output (&optional fix-key)
1425 "Parse gdb-partial-output-buffer with `json-read'.
1427 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurences from
1428 partial output. This is used to get rid of useless keys in lists
1429 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
1430 -break-info are examples of MI commands which issue such
1433 Note that GDB/MI output syntax is different from JSON both
1434 cosmetically and (in some cases) structurally, so correct results
1435 are not guaranteed."
1436 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1437 (goto-char (point-min))
1438 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
1439 (replace-match "" nil nil nil 1))
1440 (goto-char (point-min))
1442 ;; Wrap field names in double quotes and replace equal sign with
1444 ;; TODO: This breaks badly with foo= inside constants
1445 (while (re-search-forward "\\([[:alpha:]-_]+\\)=" nil t)
1446 (replace-match "\"\\1\":" nil nil))
1447 (goto-char (point-max))
1449 (goto-char (point-min))
1450 (let ((json-array-type 'list))
1453 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1454 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1457 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1459 `(defun ,name (&optional ignored)
1460 (if (and ,demand-predicate
1462 gdb-pending-triggers)))
1465 (list ,gdb-command ',output-handler))
1466 (push ',name gdb-pending-triggers)))))
1468 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1470 (setq gdb-pending-triggers
1472 gdb-pending-triggers))
1473 (let ((buf (gdb-get-buffer ',buf-key)))
1475 (with-current-buffer buf
1476 (let* ((window (get-buffer-window buf 0))
1477 (start (window-start window))
1478 (p (window-point window))
1479 (buffer-read-only nil))
1481 (insert-buffer-substring (gdb-get-buffer-create
1482 'gdb-partial-output-buffer))
1483 (set-window-start window start)
1484 (set-window-point window p)))))
1485 ;; put customisation here
1489 ;; Breakpoint buffer : This displays the output of `-break-list'.
1491 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1492 'gdb-breakpoints-buffer-name
1493 'gdb-breakpoints-mode)
1495 (def-gdb-auto-update-trigger gdb-invalidate-breakpoints
1496 (gdb-get-buffer 'gdb-breakpoints-buffer)
1498 gdb-break-list-handler)
1500 (defconst gdb-break-list-regexp
1501 "bkpt={.*?number=\"\\(.*?\\)\".*?,type=\"\\(.*?\\)\".*?,disp=\"\\(.*?\\)\".*?,\
1502 enabled=\"\\(.\\)\".*?,addr=\"\\(.*?\\)\",\\(?:.*?func=\"\\(.*?\\)\".*?,\
1503 file=\"\\(.*?\\)\".*?,fullname=\".*?\".*?,line=\"\\(.*?\\)\",\
1504 \\|\\(?:.*?what=\"\\(.*?\\)\",\\)*\\).*?times=\"\\(.*?\\)\".*?}")
1506 (defun gdb-break-list-handler ()
1507 (setq gdb-pending-triggers (delq 'gdb-invalidate-breakpoints
1508 gdb-pending-triggers))
1509 (let ((breakpoint) (breakpoints-list))
1510 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1511 (goto-char (point-min))
1512 (while (re-search-forward gdb-break-list-regexp nil t)
1513 (let ((breakpoint (list (match-string 1)
1522 (match-string 10))))
1523 (push breakpoint breakpoints-list))))
1524 (let ((buf (gdb-get-buffer 'gdb-breakpoints-buffer)))
1525 (and buf (with-current-buffer buf
1527 (buffer-read-only nil))
1529 (insert "Num Type Disp Enb Hits Addr What\n")
1530 (dolist (breakpoint breakpoints-list)
1533 (nth 0 breakpoint) " "
1534 (nth 1 breakpoint) " "
1535 (nth 2 breakpoint) " "
1536 (propertize (nth 3 breakpoint)
1537 'face (if (eq (string-to-char (nth 3 breakpoint)) ?y)
1538 font-lock-warning-face
1539 font-lock-type-face)) " "
1540 (nth 9 breakpoint) " "
1541 (nth 4 breakpoint) " "
1542 (if (nth 5 breakpoint)
1543 (concat "in " (nth 5 breakpoint) " at " (nth 6 breakpoint) ":" (nth 7 breakpoint) "\n")
1544 (concat (nth 8 breakpoint) "\n")))))
1546 (gdb-break-list-custom))
1548 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1549 (defun gdb-break-list-custom ()
1550 (let ((flag) (bptno))
1551 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1552 (dolist (buffer (buffer-list))
1553 (with-current-buffer buffer
1554 (if (and (eq gud-minor-mode 'gdbmi)
1555 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1556 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1557 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1559 (goto-char (point-min))
1560 (while (< (point) (- (point-max) 1))
1562 (if (looking-at "[^\t].*?breakpoint")
1564 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1565 (setq bptno (match-string 1))
1566 (setq flag (char-after (match-beginning 2)))
1568 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1570 (let ((buffer-read-only nil))
1571 (add-text-properties (match-beginning 1) (match-end 1)
1572 '(face font-lock-function-name-face)))
1573 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1574 (let ((line (match-string 2)) (buffer-read-only nil)
1575 (file (match-string 1)))
1576 (add-text-properties (line-beginning-position)
1578 '(mouse-face highlight
1579 help-echo "mouse-2, RET: visit breakpoint"))
1580 (unless (file-exists-p file)
1581 (setq file (cdr (assoc bptno gdb-location-alist))))
1583 (not (string-equal file "File not found")))
1584 (with-current-buffer
1585 (find-file-noselect file 'nowarn)
1587 ;; Only want one breakpoint icon at each location.
1589 (goto-line (string-to-number line))
1590 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1592 (list (concat "list "
1593 (match-string-no-properties 3) ":1\n")
1596 (list "-file-list-exec-source-file\n"
1597 `(lambda () (gdb-get-location
1598 ,bptno ,line ,flag))))))))))))
1601 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
1603 (defun gdb-get-location (bptno line flag)
1604 "Find the directory containing the relevant source file.
1605 Put in buffer and place breakpoint icon."
1606 (goto-char (point-min))
1607 (catch 'file-not-found
1608 (if (re-search-forward gdb-source-file-regexp nil t)
1609 (delete (cons bptno "File not found") gdb-location-alist)
1610 (push (cons bptno (match-string 1)) gdb-location-alist)
1612 (unless (assoc bptno gdb-location-alist)
1613 (push (cons bptno "File not found") gdb-location-alist)
1614 (message-box "Cannot find source file for breakpoint location.
1615 Add directory to search path for source files using the GDB command, dir."))
1616 (throw 'file-not-found nil))
1617 (with-current-buffer (find-file-noselect (match-string 1))
1619 ;; only want one breakpoint icon at each location
1621 (goto-line (string-to-number line))
1622 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
1624 (add-hook 'find-file-hook 'gdb-find-file-hook)
1626 (defun gdb-find-file-hook ()
1627 "Set up buffer for debugging if file is part of the source code
1628 of the current session."
1629 (if (and (buffer-name gud-comint-buffer)
1630 ;; in case gud or gdb-ui is just loaded
1632 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
1634 (if (member buffer-file-name gdb-source-file-list)
1635 (with-current-buffer (find-buffer-visiting buffer-file-name)
1636 (gdb-init-buffer)))))
1638 (declare-function gud-remove "gdb-ui" t t) ; gud-def
1639 (declare-function gud-break "gdb-ui" t t) ; gud-def
1640 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
1642 (defun gdb-mouse-set-clear-breakpoint (event)
1643 "Set/clear breakpoint in left fringe/margin at mouse click.
1644 If not in a source or disassembly buffer just set point."
1646 (mouse-minibuffer-check event)
1647 (let ((posn (event-end event)))
1648 (with-selected-window (posn-window posn)
1649 (if (or (buffer-file-name) (eq major-mode 'gdb-assembler-mode))
1650 (if (numberp (posn-point posn))
1652 (goto-char (posn-point posn))
1653 (if (or (posn-object posn)
1654 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1658 (posn-set-point posn))))
1660 (defun gdb-mouse-toggle-breakpoint-margin (event)
1661 "Enable/disable breakpoint in left margin with mouse click."
1663 (mouse-minibuffer-check event)
1664 (let ((posn (event-end event)))
1665 (if (numberp (posn-point posn))
1666 (with-selected-window (posn-window posn)
1668 (goto-char (posn-point posn))
1669 (if (posn-object posn)
1671 (let ((bptno (get-text-property
1672 0 'gdb-bptno (car (posn-string posn)))))
1674 (if (get-text-property
1675 0 'gdb-enabled (car (posn-string posn)))
1680 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1681 "Enable/disable breakpoint in left fringe with mouse click."
1683 (mouse-minibuffer-check event)
1684 (let* ((posn (event-end event))
1685 (pos (posn-point posn))
1688 (with-selected-window (posn-window posn)
1690 (set-buffer (window-buffer (selected-window)))
1692 (dolist (overlay (overlays-in pos pos))
1693 (when (overlay-get overlay 'put-break)
1694 (setq obj (overlay-get overlay 'before-string))))
1698 (if (get-text-property 0 'gdb-enabled obj)
1701 (get-text-property 0 'gdb-bptno obj) "\n"))))))))
1703 (defun gdb-breakpoints-buffer-name ()
1704 (with-current-buffer gud-comint-buffer
1705 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1707 (gdb-def-display-buffer
1708 gdb-display-breakpoints-buffer
1709 'gdb-breakpoints-buffer
1710 "Display status of user-settable breakpoints.")
1712 (gdb-def-frame-for-buffer
1713 gdb-frame-breakpoints-buffer
1714 'gdb-breakpoints-buffer
1715 "Display status of user-settable breakpoints in a new frame.")
1717 (defvar gdb-breakpoints-mode-map
1718 (let ((map (make-sparse-keymap))
1719 (menu (make-sparse-keymap "Breakpoints")))
1720 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1721 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1722 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1723 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1724 (suppress-keymap map)
1725 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1726 (define-key map " " 'gdb-toggle-breakpoint)
1727 (define-key map "D" 'gdb-delete-breakpoint)
1728 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1729 (define-key map "q" 'gdb-delete-frame-or-window)
1730 (define-key map "\r" 'gdb-goto-breakpoint)
1731 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1732 (define-key map [follow-link] 'mouse-face)
1735 (defun gdb-delete-frame-or-window ()
1736 "Delete frame if there is only one window. Otherwise delete the window."
1738 (if (one-window-p) (delete-frame)
1741 ;;from make-mode-line-mouse-map
1742 (defun gdb-make-header-line-mouse-map (mouse function) "\
1743 Return a keymap with single entry for mouse key MOUSE on the header line.
1744 MOUSE is defined to run function FUNCTION with no args in the buffer
1745 corresponding to the mode line clicked."
1746 (let ((map (make-sparse-keymap)))
1747 (define-key map (vector 'header-line mouse) function)
1748 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1752 ;; uses "-thread-info". Needs GDB 7.0 onwards.
1755 (defun gdb-jump-to (file line)
1756 (find-file-other-window file)
1759 (define-button-type 'gdb-file-button
1760 'help-echo "Push to jump to source code"
1764 (gdb-jump-to (button-get b 'file)
1765 (button-get b 'line))))
1767 (defun gdb-insert-file-location-button (file line)
1768 "Insert text button which allows jumping to FILE:LINE.
1770 FILE is a full path."
1772 (format "%s:%d" (file-name-nondirectory file) line)
1773 :type 'gdb-file-button
1777 (defun gdb-threads-buffer-name ()
1778 (concat "*threads of " (gdb-get-target-string) "*"))
1780 (gdb-def-display-buffer
1781 gdb-display-threads-buffer
1783 "Display GDB threads.")
1785 (gdb-def-frame-for-buffer
1786 gdb-frame-threads-buffer
1788 "Display GDB threads in a new frame.")
1790 (gdb-set-buffer-rules 'gdb-threads-buffer
1791 'gdb-threads-buffer-name
1794 (def-gdb-auto-update-trigger gdb-invalidate-threads
1795 (gdb-get-buffer-create 'gdb-threads-buffer)
1797 gdb-thread-list-handler)
1799 (defvar gdb-threads-font-lock-keywords
1800 '(("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1801 (" \\(stopped\\) in " (1 font-lock-warning-face))
1802 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
1803 "Font lock keywords used in `gdb-threads-mode'.")
1805 (defun gdb-threads-mode ()
1806 "Major mode for GDB threads.
1808 \\{gdb-threads-mode-map}"
1809 (kill-all-local-variables)
1810 (setq major-mode 'gdb-threads-mode)
1811 (setq mode-name "Threads")
1812 (use-local-map gdb-threads-mode-map)
1813 (setq buffer-read-only t)
1814 (buffer-disable-undo)
1815 (setq header-line-format gdb-breakpoints-header)
1816 (set (make-local-variable 'font-lock-defaults)
1817 '(gdb-threads-font-lock-keywords))
1818 (run-mode-hooks 'gdb-threads-mode-hook)
1819 'gdb-invalidate-threads)
1821 (defvar gdb-threads-mode-map
1823 (make-sparse-keymap))
1825 (defun gdb-thread-list-handler ()
1826 (setq gdb-pending-triggers (delq 'gdb-invalidate-threads
1827 gdb-pending-triggers))
1828 (let* ((res (json-partial-output))
1829 (threads-list (fadr-q "res.threads"))
1830 (buf (gdb-get-buffer 'gdb-threads-buffer)))
1832 (with-current-buffer buf
1833 (let ((buffer-read-only nil))
1835 (dolist (thread threads-list)
1836 (insert (fadr-format "~.id (~.target-id) ~.state in ~.frame.func " thread))
1839 (let ((args (fadr-q "thread.frame.args")))
1841 (insert (fadr-format "~.name=~.value," arg)))
1842 (when args (kill-backward-chars 1)))
1844 (insert-frame-location (fadr-q "thread.frame"))
1845 (insert (fadr-format " at ~.frame.addr\n" thread))))))))
1849 (defun gdb-todo-memory ()
1852 "TODO: Implement memory buffer using\nMI command -data-read-memory"))
1854 ;;; Disassembly view
1856 (defun gdb-disassembly-buffer-name ()
1857 (concat "*disassembly of " (gdb-get-target-string) "*"))
1859 (gdb-def-display-buffer
1860 gdb-display-disassembly-buffer
1861 'gdb-disassembly-buffer
1862 "Display disassembly for current stack frame.")
1864 (gdb-def-frame-for-buffer
1865 gdb-frame-disassembly-buffer
1866 'gdb-disassembly-buffer
1867 "Display disassembly in a new frame.")
1869 (gdb-set-buffer-rules 'gdb-disassembly-buffer
1870 'gdb-disassembly-buffer-name
1871 'gdb-disassembly-mode)
1873 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
1874 (gdb-get-buffer-create 'gdb-disassembly-buffer)
1875 (let ((file (or gdb-selected-file gdb-main-file))
1876 (line (or gdb-selected-line 1)))
1878 (format "-data-disassemble -f %s -l %d -n -1 -- 0\n" file line)
1880 gdb-disassembly-handler)
1882 (defvar gdb-disassembly-font-lock-keywords
1883 '(;; <__function.name+n>
1884 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1885 (1 font-lock-function-name-face))
1886 ;; 0xNNNNNNNN <__function.name+n>: opcode
1887 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
1888 (4 font-lock-keyword-face))
1889 ;; %register(at least i386)
1890 ("%\\sw+" . font-lock-variable-name-face)
1891 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
1892 (1 font-lock-comment-face)
1893 (2 font-lock-function-name-face))
1894 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
1895 "Font lock keywords used in `gdb-disassembly-mode'.")
1897 (defvar gdb-disassembly-mode-map
1899 (make-sparse-keymap))
1901 (defun gdb-disassembly-mode ()
1902 "Major mode for GDB disassembly information.
1904 \\{gdb-disassembly-mode-map}"
1905 (kill-all-local-variables)
1906 (setq major-mode 'gdb-disassembly-mode)
1907 (setq mode-name "Disassembly")
1908 (use-local-map gdb-disassembly-mode-map)
1909 (setq buffer-read-only t)
1910 (buffer-disable-undo)
1911 (set (make-local-variable 'font-lock-defaults)
1912 '(gdb-disassembly-font-lock-keywords))
1913 (run-mode-hooks 'gdb-disassembly-mode-hook)
1914 'gdb-invalidate-disassembly)
1916 (defun gdb-disassembly-handler ()
1917 (setq gdb-pending-triggers (delq 'gdb-invalidate-disassembly
1918 gdb-pending-triggers))
1919 (let* ((res (json-partial-output))
1920 (instructions (fadr-member res ".asm_insns"))
1921 (buf (gdb-get-buffer 'gdb-disassembly-buffer)))
1923 (with-current-buffer buf
1924 (let ((buffer-read-only nil))
1926 (dolist (instr instructions)
1927 (insert (fadr-format "~.address <~.func-name+~.offset>:\t~.inst\n" instr))))))))
1930 ;;; Breakpoints view
1932 (defvar gdb-breakpoints-header
1933 `(,(propertize "Breakpoints"
1934 'help-echo "mouse-1: select"
1935 'mouse-face 'mode-line-highlight
1938 (gdb-make-header-line-mouse-map
1940 (lambda (event) (interactive "e")
1941 (save-selected-window
1942 (select-window (posn-window (event-start event)))
1943 (set-window-dedicated-p (selected-window) nil)
1945 (gdb-get-buffer-create 'gdb-breakpoints-buffer))
1946 (set-window-dedicated-p (selected-window) t)))))
1948 ,(propertize "Threads"
1949 'help-echo "mouse-1: select"
1950 'mouse-face 'mode-line-highlight
1953 (gdb-make-header-line-mouse-map
1955 ;; TODO: same code few lines above
1956 (lambda (event) (interactive "e")
1957 (save-selected-window
1958 (select-window (posn-window (event-start event)))
1959 (set-window-dedicated-p (selected-window) nil)
1961 (gdb-get-buffer-create 'gdb-threads-buffer))
1962 (set-window-dedicated-p (selected-window) t)))
1965 (defun gdb-breakpoints-mode ()
1966 "Major mode for gdb breakpoints.
1968 \\{gdb-breakpoints-mode-map}"
1969 (kill-all-local-variables)
1970 (setq major-mode 'gdb-breakpoints-mode)
1971 (setq mode-name "Breakpoints")
1972 (use-local-map gdb-breakpoints-mode-map)
1973 (setq buffer-read-only t)
1974 (buffer-disable-undo)
1975 (setq header-line-format gdb-breakpoints-header)
1976 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1977 'gdb-invalidate-breakpoints)
1979 (defconst gdb-breakpoint-regexp
1980 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1982 (defun gdb-toggle-breakpoint ()
1983 "Enable/disable breakpoint at current line."
1986 (beginning-of-line 1)
1987 (if (looking-at gdb-breakpoint-regexp)
1989 (concat (if (eq ?y (char-after (match-beginning 2)))
1993 (error "Not recognized as break/watchpoint line"))))
1995 (defun gdb-delete-breakpoint ()
1996 "Delete the breakpoint at current line."
1999 (beginning-of-line 1)
2000 (if (looking-at gdb-breakpoint-regexp)
2001 (gud-basic-call (concat "-break-delete " (match-string 1)))
2002 (error "Not recognized as break/watchpoint line"))))
2004 (defun gdb-goto-breakpoint (&optional event)
2005 "Display the breakpoint location specified at current line."
2006 (interactive (list last-input-event))
2007 (if event (posn-set-point (event-end event)))
2008 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
2009 (let ((window (get-buffer-window gud-comint-buffer)))
2010 (if window (save-selected-window (select-window window))))
2012 (beginning-of-line 1)
2013 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
2014 (let ((bptno (match-string 1))
2015 (file (match-string 2))
2016 (line (match-string 3)))
2017 (save-selected-window
2018 (let* ((buffer (find-file-noselect
2019 (if (file-exists-p file) file
2020 (cdr (assoc bptno gdb-location-alist)))))
2021 (window (or (gdb-display-source-buffer buffer)
2022 (display-buffer buffer))))
2023 (setq gdb-source-window window)
2024 (with-current-buffer buffer
2025 (goto-line (string-to-number line))
2026 (set-window-point window (point))))))
2027 (error "Not recognized as break/watchpoint line"))))
2030 ;; Frames buffer. This displays a perpetually correct bactrack trace.
2032 (gdb-set-buffer-rules 'gdb-stack-buffer
2033 'gdb-stack-buffer-name
2036 (def-gdb-auto-update-trigger gdb-invalidate-frames
2037 (gdb-get-buffer 'gdb-stack-buffer)
2038 "-stack-list-frames\n"
2039 gdb-stack-list-frames-handler)
2041 (defun insert-frame-location (frame)
2042 "Insert \"file:line\" button or library name for FRAME object."
2043 (let ((file (fadr-q "frame.fullname"))
2044 (line (fadr-q "frame.line"))
2045 (from (fadr-q "frame.from")))
2047 ;; Filename with line number
2049 (gdb-insert-file-location-button
2050 file (string-to-number line)))
2052 (from (insert (format " of %s" from))))))
2054 (defun gdb-stack-list-frames-handler ()
2055 (setq gdb-pending-triggers (delq 'gdb-invalidate-frames
2056 gdb-pending-triggers))
2057 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2058 (let* ((res (json-partial-output "frame"))
2059 (stack (fadr-q "res.stack"))
2060 (buf (gdb-get-buffer 'gdb-stack-buffer)))
2062 (with-current-buffer buf
2063 (let ((buffer-read-only nil))
2065 (dolist (frame (nreverse stack))
2066 (insert (fadr-expand "~.level in ~.func" frame))
2067 (insert-frame-location frame)
2069 (gdb-stack-list-frames-custom)))))))
2071 (defun gdb-stack-list-frames-custom ()
2072 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2074 (let ((buffer-read-only nil))
2075 (goto-char (point-min))
2077 (while (< (point) (point-max))
2078 (add-text-properties (point-at-bol) (1+ (point-at-bol))
2079 '(mouse-face highlight
2080 help-echo "mouse-2, RET: Select frame"))
2082 (when (and (looking-at "^[0-9]+\\s-+\\S-+\\s-+\\(\\S-+\\)")
2083 (equal (match-string 1) gdb-selected-frame))
2084 (if (> (car (window-fringes)) 0)
2086 (or gdb-stack-position
2087 (setq gdb-stack-position (make-marker)))
2088 (set-marker gdb-stack-position (point)))
2089 (let ((bl (point-at-bol)))
2090 (put-text-property bl (+ bl 4)
2091 'face '(:inverse-video t)))))
2092 (forward-line 1))))))
2094 (defun gdb-stack-buffer-name ()
2095 (with-current-buffer gud-comint-buffer
2096 (concat "*stack frames of " (gdb-get-target-string) "*")))
2098 (gdb-def-display-buffer
2099 gdb-display-stack-buffer
2101 "Display backtrace of current stack.")
2103 (gdb-def-frame-for-buffer
2104 gdb-frame-stack-buffer
2106 "Display backtrace of current stack in a new frame.")
2108 (defvar gdb-frames-mode-map
2109 (let ((map (make-sparse-keymap)))
2110 (suppress-keymap map)
2111 (define-key map "q" 'kill-this-buffer)
2112 (define-key map "\r" 'gdb-frames-select)
2113 (define-key map [mouse-2] 'gdb-frames-select)
2114 (define-key map [follow-link] 'mouse-face)
2117 (defvar gdb-frames-font-lock-keywords
2118 '(("in \\([^ ]+\\) of " (1 font-lock-function-name-face)))
2119 "Font lock keywords used in `gdb-frames-mode'.")
2121 (defun gdb-frames-mode ()
2122 "Major mode for gdb call stack.
2124 \\{gdb-frames-mode-map}"
2125 (kill-all-local-variables)
2126 (setq major-mode 'gdb-frames-mode)
2127 (setq mode-name "Frames")
2128 (setq gdb-stack-position nil)
2129 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2130 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2131 (setq buffer-read-only t)
2132 (buffer-disable-undo)
2133 (use-local-map gdb-frames-mode-map)
2134 (set (make-local-variable 'font-lock-defaults)
2135 '(gdb-frames-font-lock-keywords))
2136 (run-mode-hooks 'gdb-frames-mode-hook)
2137 'gdb-invalidate-frames)
2139 (defun gdb-get-frame-number ()
2142 (let* ((pos (re-search-backward "^\\([0-9]+\\)" nil t))
2143 (n (or (and pos (match-string-no-properties 1)) "0")))
2146 (defun gdb-frames-select (&optional event)
2147 "Select the frame and display the relevant source."
2148 (interactive (list last-input-event))
2149 (if event (posn-set-point (event-end event)))
2150 (gud-basic-call (concat "-stack-select-frame " (gdb-get-frame-number))))
2154 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
2155 (gdb-set-buffer-rules 'gdb-locals-buffer
2156 'gdb-locals-buffer-name
2159 (def-gdb-auto-update-trigger gdb-invalidate-locals
2160 (gdb-get-buffer 'gdb-locals-buffer)
2161 "-stack-list-locals --simple-values\n"
2162 gdb-stack-list-locals-handler)
2164 (defconst gdb-stack-list-locals-regexp
2165 (concat "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\""))
2167 (defvar gdb-locals-watch-map
2168 (let ((map (make-sparse-keymap)))
2169 (suppress-keymap map)
2170 (define-key map "\r" 'gud-watch)
2171 (define-key map [mouse-2] 'gud-watch)
2173 "Keymap to create watch expression of a complex data type local variable.")
2175 (defvar gdb-edit-locals-map-1
2176 (let ((map (make-sparse-keymap)))
2177 (suppress-keymap map)
2178 (define-key map "\r" 'gdb-edit-locals-value)
2179 (define-key map [mouse-2] 'gdb-edit-locals-value)
2181 "Keymap to edit value of a simple data type local variable.")
2183 (defun gdb-edit-locals-value (&optional event)
2184 "Assign a value to a variable displayed in the locals buffer."
2185 (interactive (list last-input-event))
2187 (if event (posn-set-point (event-end event)))
2189 (let* ((var (current-word))
2190 (value (read-string (format "New value (%s): " var))))
2192 (concat "-gdb-set variable " var " = " value)))))
2194 ;; Dont display values of arrays or structures.
2195 ;; These can be expanded using gud-watch.
2196 (defun gdb-stack-list-locals-handler nil
2197 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2198 gdb-pending-triggers))
2199 (let (local locals-list)
2200 (goto-char (point-min))
2201 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
2202 (let ((local (list (match-string 1)
2205 (if (looking-at ",value=\\(\".*\"\\)}")
2206 (setcar (nthcdr 2 local) (read (match-string 1))))
2207 (push local locals-list)))
2208 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2209 (and buf (with-current-buffer buf
2210 (let* ((window (get-buffer-window buf 0))
2211 (start (window-start window))
2212 (p (window-point window))
2213 (buffer-read-only nil) (name) (value))
2215 (dolist (local locals-list)
2216 (setq name (car local))
2217 (setq value (nth 2 local))
2219 (string-match "\\0x" value))
2220 (add-text-properties 0 (length name)
2221 `(mouse-face highlight
2222 help-echo "mouse-2: create watch expression"
2223 local-map ,gdb-locals-watch-map)
2225 (add-text-properties 0 (length value)
2226 `(mouse-face highlight
2227 help-echo "mouse-2: edit value"
2228 local-map ,gdb-edit-locals-map-1)
2231 (concat name "\t" (nth 1 local)
2232 "\t" (nth 2 local) "\n")))
2233 (set-window-start window start)
2234 (set-window-point window p)))))))
2236 (defvar gdb-locals-header
2237 `(,(propertize "Locals"
2238 'help-echo "mouse-1: select"
2239 'mouse-face 'mode-line-highlight
2242 (gdb-make-header-line-mouse-map
2244 (lambda (event) (interactive "e")
2245 (save-selected-window
2246 (select-window (posn-window (event-start event)))
2247 (set-window-dedicated-p (selected-window) nil)
2249 (gdb-get-buffer-create 'gdb-locals-buffer))
2250 (set-window-dedicated-p (selected-window) t)))))
2252 ,(propertize "Registers"
2253 'help-echo "mouse-1: select"
2254 'mouse-face 'mode-line-highlight
2257 (gdb-make-header-line-mouse-map
2259 (lambda (event) (interactive "e")
2260 (save-selected-window
2261 (select-window (posn-window (event-start event)))
2262 (set-window-dedicated-p (selected-window) nil)
2264 (gdb-get-buffer-create 'gdb-registers-buffer))
2265 (set-window-dedicated-p (selected-window) t)))))))
2267 (defvar gdb-locals-mode-map
2268 (let ((map (make-sparse-keymap)))
2269 (suppress-keymap map)
2270 (define-key map "q" 'kill-this-buffer)
2273 (defun gdb-locals-mode ()
2274 "Major mode for gdb locals.
2276 \\{gdb-locals-mode-map}"
2277 (kill-all-local-variables)
2278 (setq major-mode 'gdb-locals-mode)
2279 (setq mode-name (concat "Locals:" gdb-selected-frame))
2280 (setq buffer-read-only t)
2281 (buffer-disable-undo)
2282 (setq header-line-format gdb-locals-header)
2283 (use-local-map gdb-locals-mode-map)
2284 (set (make-local-variable 'font-lock-defaults)
2285 '(gdb-locals-font-lock-keywords))
2286 (run-mode-hooks 'gdb-locals-mode-hook)
2287 'gdb-invalidate-locals)
2289 (defun gdb-locals-buffer-name ()
2290 (with-current-buffer gud-comint-buffer
2291 (concat "*locals of " (gdb-get-target-string) "*")))
2293 (gdb-def-display-buffer
2294 gdb-display-local-buffer
2296 "Display local variables of current stack and their values.")
2298 (gdb-def-frame-for-buffer
2299 gdb-frame-locals-buffer
2301 "Display local variables of current stack and their values in a new frame.")
2304 ;; Registers buffer.
2306 (gdb-set-buffer-rules 'gdb-registers-buffer
2307 'gdb-registers-buffer-name
2308 'gdb-registers-mode)
2310 (def-gdb-auto-update-trigger gdb-invalidate-registers
2311 (gdb-get-buffer 'gdb-registers-buffer)
2312 "-data-list-register-values x\n"
2313 gdb-data-list-register-values-handler)
2315 (defconst gdb-data-list-register-values-regexp
2316 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
2318 (defun gdb-data-list-register-values-handler ()
2319 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers
2320 gdb-pending-triggers))
2321 (goto-char (point-min))
2322 (if (re-search-forward gdb-error-regexp nil t)
2325 (setq match (match-string 1))
2326 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2327 (let ((buffer-read-only nil))
2330 (goto-char (point-min))))))
2331 (let ((register-list (reverse gdb-register-names))
2332 (register nil) (register-string nil) (register-values nil))
2333 (goto-char (point-min))
2334 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
2335 (setq register (pop register-list))
2336 (setq register-string (concat register "\t" (match-string 2) "\n"))
2337 (if (member (match-string 1) gdb-changed-registers)
2338 (put-text-property 0 (length register-string)
2339 'face 'font-lock-warning-face
2341 (setq register-values
2342 (concat register-values register-string)))
2343 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
2344 (with-current-buffer buf
2345 (let ((p (window-point (get-buffer-window buf 0)))
2346 (buffer-read-only nil))
2348 (insert register-values)
2349 (set-window-point (get-buffer-window buf 0) p))))))
2350 (gdb-data-list-register-values-custom))
2352 (defun gdb-data-list-register-values-custom ()
2353 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2355 (let ((buffer-read-only nil)
2357 (goto-char (point-min))
2358 (while (< (point) (point-max))
2359 (setq bl (line-beginning-position))
2360 (when (looking-at "^[^\t]+")
2361 (put-text-property bl (match-end 0)
2362 'face font-lock-variable-name-face))
2363 (forward-line 1))))))
2365 (defvar gdb-registers-mode-map
2366 (let ((map (make-sparse-keymap)))
2367 (suppress-keymap map)
2368 (define-key map "q" 'kill-this-buffer)
2371 (defun gdb-registers-mode ()
2372 "Major mode for gdb registers.
2374 \\{gdb-registers-mode-map}"
2375 (kill-all-local-variables)
2376 (setq major-mode 'gdb-registers-mode)
2377 (setq mode-name "Registers")
2378 (setq header-line-format gdb-locals-header)
2379 (setq buffer-read-only t)
2380 (buffer-disable-undo)
2381 (use-local-map gdb-registers-mode-map)
2382 (run-mode-hooks 'gdb-registers-mode-hook)
2383 'gdb-invalidate-registers)
2385 (defun gdb-registers-buffer-name ()
2386 (with-current-buffer gud-comint-buffer
2387 (concat "*registers of " (gdb-get-target-string) "*")))
2389 (gdb-def-display-buffer
2390 gdb-display-registers-buffer
2391 'gdb-registers-buffer
2392 "Display integer register contents.")
2394 (gdb-def-frame-for-buffer
2395 gdb-frame-registers-buffer
2396 'gdb-registers-buffer
2397 "Display integer register contents in a new frame.")
2399 ;; Needs GDB 6.4 onwards (used to fail with no stack).
2400 (defun gdb-get-changed-registers ()
2401 (if (and (gdb-get-buffer 'gdb-registers-buffer)
2402 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
2406 "-data-list-changed-registers\n"
2407 'gdb-get-changed-registers-handler))
2408 (push 'gdb-get-changed-registers gdb-pending-triggers))))
2410 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
2412 (defun gdb-get-changed-registers-handler ()
2413 (setq gdb-pending-triggers
2414 (delq 'gdb-get-changed-registers gdb-pending-triggers))
2415 (setq gdb-changed-registers nil)
2416 (goto-char (point-min))
2417 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
2418 (push (match-string 1) gdb-changed-registers)))
2420 (defun gdb-get-register-names ()
2421 "Create a list of register names."
2422 (goto-char (point-min))
2423 (setq gdb-register-names nil)
2424 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
2425 (push (match-string 1) gdb-register-names)))
2428 (defun gdb-get-source-file-list ()
2429 "Create list of source files for current GDB session.
2430 If buffers already exist for any of these files, gud-minor-mode
2432 (goto-char (point-min))
2433 (while (re-search-forward gdb-source-file-regexp nil t)
2434 (push (match-string 1) gdb-source-file-list))
2435 (dolist (buffer (buffer-list))
2436 (with-current-buffer buffer
2437 (when (member buffer-file-name gdb-source-file-list)
2438 (gdb-init-buffer))))
2439 (gdb-force-mode-line-update
2440 (propertize "ready" 'face font-lock-variable-name-face)))
2442 (defun gdb-get-selected-frame ()
2443 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2446 (list "-stack-info-frame\n" 'gdb-frame-handler))
2447 (push 'gdb-get-selected-frame
2448 gdb-pending-triggers))))
2450 (defun gdb-frame-handler ()
2451 (setq gdb-pending-triggers
2452 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2453 (let ((frame (fadr-member (json-partial-output) ".frame")))
2455 (setq gdb-frame-number (fadr-q "frame.level"))
2456 (setq gdb-pc-address (fadr-q "frame.addr"))
2457 (setq gdb-selected-frame (fadr-q "frame.func"))
2458 (setq gdb-selected-file (fadr-q "frame.fullname"))
2459 (let ((line (fadr-q "frame.line")))
2460 (setq gdb-selected-line (or (and line (string-to-number line))
2461 nil))) ; don't fail if line is nil
2462 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
2464 (if (gdb-get-buffer 'gdb-locals-buffer)
2465 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2466 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2467 (if (gdb-get-buffer 'gdb-disassembly-buffer)
2468 (with-current-buffer (gdb-get-buffer 'gdb-disassembly-buffer)
2469 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2470 (if gud-overlay-arrow-position
2471 (let ((buffer (marker-buffer gud-overlay-arrow-position))
2472 (position (marker-position gud-overlay-arrow-position)))
2474 (with-current-buffer buffer
2475 (setq fringe-indicator-alist
2476 (if (string-equal gdb-frame-number "0")
2478 '((overlay-arrow . hollow-right-triangle))))
2479 (setq gud-overlay-arrow-position (make-marker))
2480 (set-marker gud-overlay-arrow-position position)))))
2481 (gdb-invalidate-disassembly))))
2483 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
2485 (defun gdb-get-prompt ()
2486 "Find prompt for GDB session."
2487 (goto-char (point-min))
2488 (setq gdb-prompt-name nil)
2489 (re-search-forward gdb-prompt-name-regexp nil t)
2490 (setq gdb-prompt-name (match-string 1))
2491 ;; Insert first prompt.
2492 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2494 ;;;; Window management
2495 (defun gdb-display-buffer (buf dedicated &optional frame)
2496 (let ((answer (get-buffer-window buf (or frame 0))))
2498 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
2499 (let ((window (get-lru-window)))
2500 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
2502 (let* ((largest (get-largest-window))
2503 (cur-size (window-height largest)))
2504 (setq answer (split-window largest))
2505 (set-window-buffer answer buf)
2506 (set-window-dedicated-p answer dedicated)
2508 (set-window-buffer window buf)
2512 ;;; Shared keymap initialization:
2514 (let ((menu (make-sparse-keymap "GDB-Windows")))
2515 (define-key gud-menu-map [displays]
2516 `(menu-item "GDB-Windows" ,menu
2517 :visible (eq gud-minor-mode 'gdbmi)))
2518 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2519 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2520 ; (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2521 (define-key menu [memory] '("Memory" . gdb-todo-memory))
2522 (define-key menu [disassembly]
2523 '("Disassembly" . gdb-display-assembler-buffer))
2524 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2525 (define-key menu [inferior]
2526 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2527 :enable gdb-use-separate-io-buffer))
2528 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2529 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2530 (define-key menu [breakpoints]
2531 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2533 (let ((menu (make-sparse-keymap "GDB-Frames")))
2534 (define-key gud-menu-map [frames]
2535 `(menu-item "GDB-Frames" ,menu
2536 :visible (eq gud-minor-mode 'gdbmi)))
2537 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2538 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2539 ; (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2540 (define-key menu [memory] '("Memory" . gdb-todo-memory))
2541 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2542 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2543 (define-key menu [inferior]
2544 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2545 :enable gdb-use-separate-io-buffer))
2546 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2547 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2548 (define-key menu [breakpoints]
2549 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2551 (let ((menu (make-sparse-keymap "GDB-MI")))
2552 (define-key gud-menu-map [mi]
2553 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi)))
2554 (define-key menu [gdb-customize]
2555 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
2556 :help "Customize Gdb Graphical Mode options."))
2557 (define-key menu [gdb-use-separate-io]
2558 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2559 :help "Toggle separate IO for debugged program."
2560 :button (:toggle . gdb-use-separate-io-buffer)))
2561 (define-key menu [gdb-many-windows]
2562 '(menu-item "Display Other Windows" gdb-many-windows
2563 :help "Toggle display of locals, stack and breakpoint information"
2564 :button (:toggle . gdb-many-windows)))
2565 (define-key menu [gdb-restore-windows]
2566 '(menu-item "Restore Window Layout" gdb-restore-windows
2567 :help "Restore standard layout for debug session.")))
2569 (defun gdb-frame-gdb-buffer ()
2570 "Display GUD buffer in a new frame."
2572 (let ((special-display-regexps (append special-display-regexps '(".*")))
2573 (special-display-frame-alist
2574 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2575 gdb-frame-parameters)))
2576 (same-window-regexps nil))
2577 (display-buffer gud-comint-buffer)))
2579 (defun gdb-display-gdb-buffer ()
2580 "Display GUD buffer."
2582 (let ((same-window-regexps nil))
2583 (select-window (display-buffer gud-comint-buffer nil 0))))
2585 (defun gdb-set-window-buffer (name)
2586 (set-window-buffer (selected-window) (get-buffer name))
2587 (set-window-dedicated-p (selected-window) t))
2589 (defun gdb-setup-windows ()
2590 "Layout the window pattern for `gdb-many-windows'."
2591 (gdb-display-locals-buffer)
2592 (gdb-display-stack-buffer)
2593 (delete-other-windows)
2594 (gdb-display-breakpoints-buffer)
2595 (delete-other-windows)
2597 (pop-to-buffer gud-comint-buffer)
2598 (split-window nil ( / ( * (window-height) 3) 4))
2599 (split-window nil ( / (window-height) 3))
2600 (split-window-horizontally)
2602 (gdb-set-window-buffer (gdb-locals-buffer-name))
2605 (if gud-last-last-frame
2606 (gud-find-file (car gud-last-last-frame))
2608 (gud-find-file gdb-main-file)
2609 ;; Put buffer list in window if we
2610 ;; can't find a source file.
2611 (list-buffers-noselect))))
2612 (setq gdb-source-window (selected-window))
2613 (when gdb-use-separate-io-buffer
2614 (split-window-horizontally)
2616 (gdb-set-window-buffer
2617 (gdb-get-buffer-create 'gdb-inferior-io)))
2619 (gdb-set-window-buffer (gdb-stack-buffer-name))
2620 (split-window-horizontally)
2622 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2625 (defcustom gdb-many-windows nil
2626 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
2627 In this case it starts with two windows: one displaying the GUD
2628 buffer and the other with the source file with the main routine
2629 of the debugged program. Non-nil means display the layout shown for
2635 (defun gdb-many-windows (arg)
2636 "Toggle the number of windows in the basic arrangement.
2637 With arg, display additional buffers iff arg is positive."
2639 (setq gdb-many-windows
2641 (not gdb-many-windows)
2642 (> (prefix-numeric-value arg) 0)))
2643 (message (format "Display of other windows %sabled"
2644 (if gdb-many-windows "en" "dis")))
2645 (if (and gud-comint-buffer
2646 (buffer-name gud-comint-buffer))
2648 (gdb-restore-windows)
2651 (defun gdb-restore-windows ()
2652 "Restore the basic arrangement of windows used by gdb.
2653 This arrangement depends on the value of `gdb-many-windows'."
2655 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2656 (delete-other-windows)
2657 (if gdb-many-windows
2659 (when (or gud-last-last-frame gdb-show-main)
2663 (if gud-last-last-frame
2664 (gud-find-file (car gud-last-last-frame))
2665 (gud-find-file gdb-main-file)))
2666 (setq gdb-source-window (selected-window))
2670 "Exit a debugging session cleanly.
2671 Kills the gdb buffers, and resets variables and the source buffers."
2672 (dolist (buffer (buffer-list))
2673 (unless (eq buffer gud-comint-buffer)
2674 (with-current-buffer buffer
2675 (if (eq gud-minor-mode 'gdbmi)
2676 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
2678 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2679 (setq gud-minor-mode nil)
2680 (kill-local-variable 'tool-bar-map)
2681 (kill-local-variable 'gdb-define-alist))))))
2682 (setq gdb-overlay-arrow-position nil)
2683 (setq overlay-arrow-variable-list
2684 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2685 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2686 (setq gdb-stack-position nil)
2687 (setq overlay-arrow-variable-list
2688 (delq 'gdb-stack-position overlay-arrow-variable-list))
2689 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2690 (setq gud-running nil)
2691 (setq gdb-active-process nil)
2692 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2694 (defun gdb-get-source-file ()
2695 "Find the source file where the program starts and display it with related
2696 buffers, if required."
2697 (goto-char (point-min))
2698 (if (re-search-forward gdb-source-file-regexp nil t)
2699 (setq gdb-main-file (match-string 1)))
2700 (if gdb-many-windows
2702 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2704 (let ((pop-up-windows t))
2705 (display-buffer (gud-find-file gdb-main-file))))))
2708 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2709 "Put string PUTSTRING in front of POS in the current buffer.
2710 PUTSTRING is displayed by putting an overlay into the current buffer with a
2711 `before-string' string that has a `display' property whose value is
2713 (let ((string (make-string 1 ?x))
2714 (buffer (current-buffer)))
2715 (setq putstring (copy-sequence putstring))
2716 (let ((overlay (make-overlay pos pos buffer))
2718 (list (list 'margin 'left-margin) putstring))))
2719 (put-text-property 0 1 'display prop string)
2721 (add-text-properties 0 1 sprops string))
2722 (overlay-put overlay 'put-break t)
2723 (overlay-put overlay 'before-string string))))
2725 ;;from remove-images
2726 (defun gdb-remove-strings (start end &optional buffer)
2727 "Remove strings between START and END in BUFFER.
2728 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2729 BUFFER nil or omitted means use the current buffer."
2731 (setq buffer (current-buffer)))
2732 (dolist (overlay (overlays-in start end))
2733 (when (overlay-get overlay 'put-break)
2734 (delete-overlay overlay))))
2736 (defun gdb-put-breakpoint-icon (enabled bptno)
2737 (let ((start (- (line-beginning-position) 1))
2738 (end (+ (line-end-position) 1))
2739 (putstring (if enabled "B" "b"))
2740 (source-window (get-buffer-window (current-buffer) 0)))
2741 (add-text-properties
2742 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2745 (add-text-properties
2746 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2747 (add-text-properties
2748 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2749 (gdb-remove-breakpoint-icons start end)
2750 (if (display-images-p)
2751 (if (>= (or left-fringe-width
2752 (if source-window (car (window-fringes source-window)))
2753 gdb-buffer-fringe-width) 8)
2756 `(left-fringe breakpoint
2759 'breakpoint-disabled))
2761 'gdb-enabled enabled)
2762 (when (< left-margin-width 2)
2763 (save-current-buffer
2764 (setq left-margin-width 2)
2768 left-margin-width right-margin-width))))
2771 (or breakpoint-enabled-icon
2772 (setq breakpoint-enabled-icon
2773 (find-image `((:type xpm :data
2774 ,breakpoint-xpm-data
2775 :ascent 100 :pointer hand)
2777 ,breakpoint-enabled-pbm-data
2778 :ascent 100 :pointer hand)))))
2779 (or breakpoint-disabled-icon
2780 (setq breakpoint-disabled-icon
2781 (find-image `((:type xpm :data
2782 ,breakpoint-xpm-data
2783 :conversion disabled
2784 :ascent 100 :pointer hand)
2786 ,breakpoint-disabled-pbm-data
2787 :ascent 100 :pointer hand))))))
2791 (when (< left-margin-width 2)
2792 (save-current-buffer
2793 (setq left-margin-width 2)
2794 (let ((window (get-buffer-window (current-buffer) 0)))
2797 window left-margin-width right-margin-width)))))
2799 (propertize putstring
2800 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2803 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2804 (gdb-remove-strings start end)
2805 (if (display-images-p)
2806 (remove-images start end))
2808 (setq left-margin-width 0)
2809 (let ((window (get-buffer-window (current-buffer) 0)))
2812 window left-margin-width right-margin-width)))))
2816 ;;; gdb-mi.el ends here