1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
7 ;; Keywords: unix, tools
9 ;; This file is part of GNU Emacs.
11 ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file was written by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It is currently being developed
30 ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
31 ;; of Code 2009 Project "Emacs GDB/MI migration".
35 ;; This mode acts as a graphical user interface to GDB. You can interact with
36 ;; GDB through the GUD buffer in the usual way, but there are also further
37 ;; buffers which control the execution and describe the state of your program.
38 ;; It separates the input/output of your program from that of GDB and displays
39 ;; expressions and their current values in their own buffers. It also uses
40 ;; features of Emacs 21 such as the fringe/display margin for breakpoints, and
41 ;; the toolbar (see the GDB Graphical Interface section in the Emacs info
44 ;; M-x gdb will start the debugger.
46 ;; This file uses GDB/MI as the primary interface to GDB. It runs gdb with
47 ;; GDB/MI (-interp=mi) and access CLI using "-interpreter-exec console
48 ;; cli-command". This code works without gdb-ui.el and uses MI tokens instead
49 ;; of queues. Eventually MI should be asynchronous.
53 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
54 ;; explicitly in your program if you want timely display of I/O in Emacs.
55 ;; Alternatively you can make the output stream unbuffered, for example, by
59 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
62 ;; and compiling with -DUNBUFFERED while debugging.
64 ;; If you are using Cygwin GDB and find that the source is not being displayed
65 ;; in Emacs when you step through it, possible solutions are to:
67 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
68 ;; (Since 22.1 Emacs builds under Cygwin.)
69 ;; 2) Use MinGW GDB instead.
70 ;; 3) Use cygwin-mount.el
74 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of Mac OSX.
76 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
77 ;; as earlier versions do not compile on Mac OSX.
81 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
82 ;; line information, e.g., a routine in libc (just a TODO item).
85 ;; 2) Watch windows to work with threads.
86 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
87 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
94 (eval-when-compile (require 'cl))
96 (declare-function speedbar-change-initial-expansion-list
97 "speedbar" (new-default))
98 (declare-function speedbar-timer-fn "speedbar" ())
99 (declare-function speedbar-line-text "speedbar" (&optional p))
100 (declare-function speedbar-change-expand-button-char "speedbar" (char))
101 (declare-function speedbar-delete-subblock "speedbar" (indent))
102 (declare-function speedbar-center-buffer-smartly "speedbar" ())
104 (defvar tool-bar-map)
105 (defvar speedbar-initial-expansion-list-name)
106 (defvar speedbar-frame)
108 (defvar gdb-memory-address "main")
109 (defvar gdb-memory-last-address nil
110 "Last successfully accessed memory address.")
111 (defvar gdb-memory-next-page nil
112 "Address of next memory page for program memory buffer.")
113 (defvar gdb-memory-prev-page nil
114 "Address of previous memory page for program memory buffer.")
116 (defvar gdb-thread-number nil
117 "Main current thread.
119 Invalidation triggers use this variable to query GDB for
120 information on the specified thread by wrapping GDB/MI commands
121 in `gdb-current-context-command'.
123 This variable may be updated implicitly by GDB via `gdb-stopped'
124 or explicitly by `gdb-select-thread'.
126 Only `gdb-setq-thread-number' should be used to change this
129 (defvar gdb-frame-number nil
130 "Selected frame level for main current thread.
132 Updated according to the following rules:
134 When a thread is selected or current thread stops, set to \"0\".
136 When current thread goes running (and possibly exits eventually),
139 May be manually changed by user with `gdb-select-frame'.")
141 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
143 ;; Used to show overlay arrow in source buffer. All set in
144 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
145 ;; these but rely on buffer-local thread information instead.
146 (defvar gdb-selected-frame nil
147 "Name of selected function for main current thread.")
148 (defvar gdb-selected-file nil
149 "Name of selected file for main current thread.")
150 (defvar gdb-selected-line nil
151 "Number of selected line for main current thread.")
153 (defvar gdb-threads-list nil
154 "Associative list of threads provided by \"-thread-info\" MI command.
156 Keys are thread numbers (in strings) and values are structures as
157 returned from -thread-info by `gdb-json-partial-output'. Updated in
158 `gdb-thread-list-handler-custom'.")
160 (defvar gdb-running-threads-count nil
161 "Number of currently running threads.
163 If nil, no information is available.
165 Updated in `gdb-thread-list-handler-custom'.")
167 (defvar gdb-stopped-threads-count nil
168 "Number of currently stopped threads.
170 See also `gdb-running-threads-count'.")
172 (defvar gdb-breakpoints-list nil
173 "Associative list of breakpoints provided by \"-break-list\" MI command.
175 Keys are breakpoint numbers (in string) and values are structures
176 as returned from \"-break-list\" by `gdb-json-partial-output'
177 \(\"body\" field is used). Updated in
178 `gdb-breakpoints-list-handler-custom'.")
180 (defvar gdb-current-language nil)
181 (defvar gdb-var-list nil
182 "List of variables in watch window.
183 Each element has the form
184 (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
185 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
186 address for root variables.")
187 (defvar gdb-main-file nil "Source file from which program execution begins.")
189 ;; Overlay arrow markers
190 (defvar gdb-stack-position nil)
191 (defvar gdb-thread-position nil)
192 (defvar gdb-disassembly-position nil)
194 (defvar gdb-location-alist nil
195 "Alist of breakpoint numbers and full filenames. Only used for files that
197 (defvar gdb-active-process nil
198 "GUD tooltips display variable values when t, and macro definitions otherwise.")
199 (defvar gdb-error "Non-nil when GDB is reporting an error.")
200 (defvar gdb-macro-info nil
201 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
202 (defvar gdb-register-names nil "List of register names.")
203 (defvar gdb-changed-registers nil
204 "List of changed register numbers (strings).")
205 (defvar gdb-buffer-fringe-width nil)
206 (defvar gdb-last-command nil)
207 (defvar gdb-prompt-name nil)
208 (defvar gdb-token-number 0)
209 (defvar gdb-handler-alist '())
210 (defvar gdb-handler-number nil)
211 (defvar gdb-source-file-list nil
212 "List of source files for the current executable.")
213 (defvar gdb-first-done-or-error t)
214 (defvar gdb-source-window nil)
215 (defvar gdb-inferior-status nil)
216 (defvar gdb-continuation nil)
217 (defvar gdb-supports-non-stop nil)
218 (defvar gdb-filter-output nil
219 "Message to be shown in GUD console.
221 This variable is updated in `gdb-done-or-error' and returned by
222 `gud-gdbmi-marker-filter'.")
224 (defvar gdb-non-stop nil
225 "Indicates whether current GDB session is using non-stop mode.
227 It is initialized to `gdb-non-stop-setting' at the beginning of
230 (defvar gdb-buffer-type nil
231 "One of the symbols bound in `gdb-buffer-rules'.")
232 (make-variable-buffer-local 'gdb-buffer-type)
234 (defvar gdb-output-sink 'nil
235 "The disposition of the output of the current gdb command.
236 Possible values are these symbols:
238 `user' -- gdb output should be copied to the GUD buffer
241 `emacs' -- output should be collected in the partial-output-buffer
242 for subsequent processing by a command. This is the
243 disposition of output generated by commands that
244 gdb mode sends to gdb on its own behalf.")
246 ;; Pending triggers prevent congestion: Emacs won't send two similar
247 ;; consecutive requests.
249 (defvar gdb-pending-triggers '()
250 "A list of trigger functions which have not yet been handled.
252 Elements are either function names or pairs (buffer . function)")
254 (defmacro gdb-add-pending (item)
255 `(push ,item gdb-pending-triggers))
256 (defmacro gdb-pending-p (item)
257 `(member ,item gdb-pending-triggers))
258 (defmacro gdb-delete-pending (item)
259 `(setq gdb-pending-triggers
260 (delete ,item gdb-pending-triggers)))
262 (defmacro gdb-wait-for-pending (&rest body)
263 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
265 This function checks `gdb-pending-triggers' value every
266 `gdb-wait-for-pending' seconds."
270 (if (not gdb-pending-triggers)
272 (gdb-wait-for-pending ,@body)))))
276 (defmacro gdb-add-subscriber (publisher subscriber)
277 "Register new PUBLISHER's SUBSCRIBER.
279 SUBSCRIBER must be a pair, where cdr is a function of one
280 argument (see `gdb-emit-signal')."
281 `(add-to-list ',publisher ,subscriber t))
283 (defmacro gdb-delete-subscriber (publisher subscriber)
284 "Unregister SUBSCRIBER from PUBLISHER."
285 `(setq ,publisher (delete ,subscriber
288 (defun gdb-get-subscribers (publisher)
291 (defun gdb-emit-signal (publisher &optional signal)
292 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
293 (dolist (subscriber (gdb-get-subscribers publisher))
294 (funcall (cdr subscriber) signal)))
296 (defvar gdb-buf-publisher '()
297 "Used to invalidate GDB buffers by emitting a signal in
300 Must be a list of pairs with cars being buffers and cdr's being
301 valid signal handlers.")
304 "GDB graphical interface"
306 :link '(info-link "(emacs)GDB Graphical Interface")
309 (defgroup gdb-non-stop nil
310 "GDB non-stop debugging settings"
314 (defgroup gdb-buffers nil
319 (defcustom gdb-debug-log-max 128
320 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
322 :type '(choice (integer :tag "Number of elements")
323 (const :tag "Unlimited" nil))
326 (defcustom gdb-non-stop-setting t
327 "When in non-stop mode, stopped threads can be examined while
328 other threads continue to execute.
330 GDB session needs to be restarted for this setting to take
336 ;; TODO Some commands can't be called with --all (give a notice about
337 ;; it in setting doc)
338 (defcustom gdb-gud-control-all-threads t
339 "When enabled, GUD execution commands affect all threads when
340 in non-stop mode. Otherwise, only current thread is affected."
345 (defcustom gdb-switch-reasons t
346 "List of stop reasons which cause Emacs to switch to the thread
347 which caused the stop. When t, switch to stopped thread no matter
348 what the reason was. When nil, never switch to stopped thread
351 This setting is used in non-stop mode only. In all-stop mode,
352 Emacs always switches to the thread which caused the stop."
353 ;; exited, exited-normally and exited-signaled are not
354 ;; thread-specific stop reasons and therefore are not included in
357 (const :tag "All reasons" t)
358 (set :tag "Selection of reasons..."
359 (const :tag "A breakpoint was reached." "breakpoint-hit")
360 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
361 (const :tag "A read watchpoint was triggered."
362 "read-watchpoint-trigger")
363 (const :tag "An access watchpoint was triggered."
364 "access-watchpoint-trigger")
365 (const :tag "Function finished execution." "function-finished")
366 (const :tag "Location reached." "location-reached")
367 (const :tag "Watchpoint has gone out of scope"
369 (const :tag "End of stepping range reached."
370 "end-stepping-range")
371 (const :tag "Signal received (like interruption)."
373 (const :tag "None" nil))
376 :link '(info-link "(gdb)GDB/MI Async Records"))
378 (defcustom gdb-stopped-hooks nil
379 "This variable holds a list of functions to be called whenever
382 Each function takes one argument, a parsed MI response, which
383 contains fields of corresponding MI *stopped async record:
385 ((stopped-threads . \"all\")
387 (frame (line . \"38\")
388 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
389 (file . \"server.c\")
390 (args ((value . \"0x804b038\")
393 (addr . \"0x0804869e\"))
394 (reason . \"end-stepping-range\"))
396 Note that \"reason\" is only present in non-stop debugging mode.
398 `bindat-get-field' may be used to access the fields of response.
400 Each function is called after the new current thread was selected
401 and GDB buffers were updated in `gdb-stopped'."
402 :type '(repeat function)
405 :link '(info-link "(gdb)GDB/MI Async Records"))
407 (defcustom gdb-switch-when-another-stopped t
408 "When nil, Emacs won't switch to stopped thread if some other
409 stopped thread is already selected."
414 (defcustom gdb-stack-buffer-locations t
415 "Show file information or library names in stack buffers."
420 (defcustom gdb-stack-buffer-addresses nil
421 "Show frame addresses in stack buffers."
426 (defcustom gdb-thread-buffer-verbose-names t
427 "Show long thread names in threads buffer."
432 (defcustom gdb-thread-buffer-arguments t
433 "Show function arguments in threads buffer."
438 (defcustom gdb-thread-buffer-locations t
439 "Show file information or library names in threads buffer."
444 (defcustom gdb-thread-buffer-addresses nil
445 "Show addresses for thread frames in threads buffer."
450 (defcustom gdb-show-threads-by-default nil
451 "Show threads list buffer instead of breakpoints list by
457 (defvar gdb-debug-log nil
458 "List of commands sent to and replies received from GDB.
459 Most recent commands are listed first. This list stores only the last
460 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
463 (defcustom gdb-enable-debug nil
464 "Non-nil means record the process input and output in `gdb-debug-log'."
469 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
470 "Shell command for generating a list of defined macros in a source file.
471 This list is used to display the #define directive associated
472 with an identifier as a tooltip. It works in a debug session with
473 GDB, when `gud-tooltip-mode' is t.
475 Set `gdb-cpp-define-alist-flags' for any include paths or
481 (defcustom gdb-cpp-define-alist-flags ""
482 "Preprocessor flags for `gdb-cpp-define-alist-program'."
487 (defcustom gdb-create-source-file-list t
488 "Non-nil means create a list of files from which the executable was built.
489 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
490 line for a long time when starting, possibly because your executable was
491 built from a large number of files. This allows quicker initialization
492 but means that these files are not automatically enabled for debugging,
493 e.g., you won't be able to click in the fringe to set a breakpoint until
494 execution has already stopped there."
499 (defcustom gdb-show-main nil
500 "Non-nil means display source file containing the main routine at startup.
501 Also display the main routine in the disassembly buffer if present."
506 (defun gdb-force-mode-line-update (status)
507 (let ((buffer gud-comint-buffer))
508 (if (and buffer (buffer-name buffer))
509 (with-current-buffer buffer
510 (setq mode-line-process
512 (process-status (get-buffer-process buffer)) status))
513 ;; Force mode line redisplay soon.
514 (force-mode-line-update)))))
516 (defun gdb-enable-debug (arg)
517 "Toggle logging of transaction between Emacs and Gdb.
518 The log is stored in `gdb-debug-log' as an alist with elements
519 whose cons is send, send-item or recv and whose cdr is the string
520 being transferred. This list may grow up to a size of
521 `gdb-debug-log-max' after which the oldest element (at the end of
522 the list) is deleted every time a new one is added (at the front)."
524 (setq gdb-enable-debug
526 (not gdb-enable-debug)
527 (> (prefix-numeric-value arg) 0)))
528 (message (format "Logging of transaction %sabled"
529 (if gdb-enable-debug "en" "dis"))))
531 ;; These two are used for menu and toolbar
532 (defun gdb-control-all-threads ()
533 "Switch to non-stop/A mode."
535 (setq gdb-gud-control-all-threads t)
536 ;; Actually forcing the tool-bar to update.
537 (force-mode-line-update)
538 (message "Now in non-stop/A mode."))
540 (defun gdb-control-current-thread ()
541 "Switch to non-stop/T mode."
543 (setq gdb-gud-control-all-threads nil)
544 ;; Actually forcing the tool-bar to update.
545 (force-mode-line-update)
546 (message "Now in non-stop/T mode."))
548 (defun gdb-find-watch-expression ()
549 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
550 (varnum (car var)) expr)
551 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
552 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
553 (component-list (split-string (match-string 2 varnum) "\\." t)))
554 (setq expr (nth 1 var1))
555 (setq varnumlet (car var1))
556 (dolist (component component-list)
557 (setq var2 (assoc varnumlet gdb-var-list))
558 (setq expr (concat expr
559 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
560 (concat "[" component "]")
561 (concat "." component))))
562 (setq varnumlet (concat varnumlet "." component)))
565 ;; noall is used for commands which don't take --all, but only
567 (defun gdb-gud-context-command (command &optional noall)
568 "When `gdb-non-stop' is t, add --thread option to COMMAND if
569 `gdb-gud-control-all-threads' is nil and --all option otherwise.
570 If NOALL is t, always add --thread option no matter what
571 `gdb-gud-control-all-threads' value is.
573 When `gdb-non-stop' is nil, return COMMAND unchanged."
575 (if (and gdb-gud-control-all-threads
577 gdb-supports-non-stop)
578 (concat command " --all ")
579 (gdb-current-context-command command))
582 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
583 "`gud-call' wrapper which adds --thread/--all options between
584 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
586 NOARG must be t when this macro is used outside `gud-def'"
588 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
589 ,(when (not noarg) 'arg)))
591 (defun gdb--check-interpreter (proc string)
592 (unless (zerop (length string))
593 (let ((filter (process-get proc 'gud-normal-filter)))
594 (set-process-filter proc filter)
595 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
596 ;; Apparently we're not running with -i=mi.
597 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
599 (setq string (concat (propertize msg 'font-lock-face 'error)
601 ;; Use the old gud-gbd filter, not because it works, but because it
602 ;; will properly display GDB's answers rather than hanging waiting for
603 ;; answers that aren't coming.
604 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
605 (funcall filter proc string))))
608 (defun gdb (command-line)
609 "Run gdb on program FILE in buffer *gud-FILE*.
610 The directory containing FILE becomes the initial working directory
611 and source-file directory for your debugger.
613 COMMAND-LINE is the shell command for starting the gdb session.
614 It should be a string consisting of the name of the gdb
615 executable followed by command-line options. The command-line
616 options should include \"-i=mi\" to use gdb's MI text interface.
617 Note that the old \"--annotate\" option is no longer supported.
619 If `gdb-many-windows' is nil (the default value) then gdb just
620 pops up the GUD buffer unless `gdb-show-main' is t. In this case
621 it starts with two windows: one displaying the GUD buffer and the
622 other with the source file with the main routine of the inferior.
624 If `gdb-many-windows' is t, regardless of the value of
625 `gdb-show-main', the layout below will appear. Keybindings are
626 shown in some of the buffers.
628 Watch expressions appear in the speedbar/slowbar.
630 The following commands help control operation :
632 `gdb-many-windows' - Toggle the number of windows gdb uses.
633 `gdb-restore-windows' - To restore the window layout.
635 See Info node `(emacs)GDB Graphical Interface' for a more
636 detailed description of this mode.
639 +----------------------------------------------------------------------+
641 +-----------------------------------+----------------------------------+
642 | GUD buffer (I/O of GDB) | Locals buffer |
646 +-----------------------------------+----------------------------------+
647 | Source buffer | I/O buffer (of debugged program) |
655 +-----------------------------------+----------------------------------+
656 | Stack buffer | Breakpoints buffer |
657 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
658 | | RET gdb-goto-breakpoint |
659 | | D gdb-delete-breakpoint |
660 +-----------------------------------+----------------------------------+"
662 (interactive (list (gud-query-cmdline 'gdb)))
664 (when (and gud-comint-buffer
665 (buffer-name gud-comint-buffer)
666 (get-buffer-process gud-comint-buffer)
667 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
668 (gdb-restore-windows)
670 "Multiple debugging requires restarting in text command mode"))
672 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
674 ;; Setup a temporary process filter to warn when GDB was not started
676 (let ((proc (get-buffer-process gud-comint-buffer)))
677 (process-put proc 'gud-normal-filter (process-filter proc))
678 (set-process-filter proc #'gdb--check-interpreter))
680 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
681 (setq comint-input-sender 'gdb-send)
682 (when (ring-empty-p comint-input-ring) ; cf shell-mode
683 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
684 (if (eq system-type 'ms-dos)
687 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
688 (hsize (getenv "HISTSIZE")))
689 (dolist (file (append '("~/.gdbinit")
690 (unless (string-equal (expand-file-name ".")
691 (expand-file-name "~"))
693 (if (file-readable-p (setq file (expand-file-name file)))
695 (insert-file-contents file)
696 ;; TODO? check for "set history save\\( *on\\)?" and do
697 ;; not use history otherwise?
698 (while (re-search-forward
699 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
700 (cond ((string-equal (match-string 1) "filename")
701 (setq hfile (expand-file-name
703 (file-name-directory file))))
704 ((string-equal (match-string 1) "size")
705 (setq hsize (match-string 2))))))))
707 (integerp (setq hsize (string-to-number hsize)))
709 (set (make-local-variable 'comint-input-ring-size) hsize))
711 (set (make-local-variable 'comint-input-ring-file-name) hfile))
712 (comint-read-input-ring t)))
713 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
714 "Set temporary breakpoint at current line.")
716 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
717 "\C-j" "Set execution address to current line.")
719 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
720 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
721 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
722 (gud-def gud-pstar "print* %e" nil
723 "Evaluate C dereferenced pointer expression at point.")
725 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
727 "Step one source line with display.")
728 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
730 "Step one instruction with display.")
731 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
733 "Step one line (skip functions).")
734 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
736 "Step one instruction (skip functions).")
737 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
739 "Continue with display.")
740 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
742 "Finish executing current function.")
743 (gud-def gud-run "-exec-run"
747 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
748 (gud-call "break %f:%l" arg)
752 (gud-call "break *%a" arg)))
753 "\C-b" "Set breakpoint at current line or address.")
755 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
756 (gud-call "clear %f:%l" arg)
760 (gud-call "clear *%a" arg)))
761 "\C-d" "Remove breakpoint at current line or address.")
763 ;; -exec-until doesn't support --all yet
764 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
765 (gud-call "-exec-until %f:%l" arg)
769 (gud-call "-exec-until *%a" arg)))
770 "\C-u" "Continue to current line or address.")
771 ;; TODO Why arg here?
773 gud-go (gud-call (if gdb-active-process
774 (gdb-gud-context-command "-exec-continue")
776 nil "Start or continue execution.")
778 ;; For debugging Emacs only.
782 "pp1 " (if (eq (buffer-local-value
783 'major-mode (window-buffer)) 'speedbar-mode)
784 (gdb-find-watch-expression) "%e")) arg)
785 nil "Print the Emacs s-expression.")
787 (define-key gud-minor-mode-map [left-margin mouse-1]
788 'gdb-mouse-set-clear-breakpoint)
789 (define-key gud-minor-mode-map [left-fringe mouse-1]
790 'gdb-mouse-set-clear-breakpoint)
791 (define-key gud-minor-mode-map [left-margin C-mouse-1]
792 'gdb-mouse-toggle-breakpoint-margin)
793 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
794 'gdb-mouse-toggle-breakpoint-fringe)
796 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
798 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
800 (define-key gud-minor-mode-map [left-margin mouse-3]
802 (define-key gud-minor-mode-map [left-fringe mouse-3]
805 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
807 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
809 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
811 (define-key gud-minor-mode-map [left-margin C-mouse-3]
814 (set (make-local-variable 'gud-gdb-completion-function)
815 'gud-gdbmi-completions)
817 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
819 (local-set-key "\C-i" 'completion-at-point)
821 (setq gdb-first-prompt t)
822 (setq gud-running nil)
826 (run-hooks 'gdb-mode-hook))
830 (setq gdb-selected-frame nil
832 gdb-thread-number nil
834 gdb-pending-triggers nil
835 gdb-output-sink 'user
836 gdb-location-alist nil
837 gdb-source-file-list nil
840 gdb-handler-alist '()
841 gdb-handler-number nil
843 gdb-first-done-or-error t
844 gdb-buffer-fringe-width (car (window-fringes))
846 gdb-source-window nil
847 gdb-inferior-status nil
849 gdb-buf-publisher '()
851 gdb-breakpoints-list '()
852 gdb-register-names '()
853 gdb-non-stop gdb-non-stop-setting)
855 (setq gdb-buffer-type 'gdbmi)
857 (gdb-force-mode-line-update
858 (propertize "initializing..." 'face font-lock-variable-name-face))
860 (gdb-get-buffer-create 'gdb-inferior-io)
861 (gdb-clear-inferior-io)
862 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
864 ;; Needs GDB 6.4 onwards
865 (concat "-inferior-tty-set "
867 ;; The process can run on a remote host.
868 (process-get (get-process "gdb-inferior") 'remote-tty)
869 (process-tty-name (get-process "gdb-inferior"))))
871 (if (eq window-system 'w32)
872 (gdb-input "-gdb-set new-console off" 'ignore))
873 (gdb-input "-gdb-set height 0" 'ignore)
876 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
878 (gdb-input "-enable-pretty-printing" 'ignore)
880 ;; find source file and compilation directory here
881 (if gdb-create-source-file-list
882 ;; Needs GDB 6.2 onwards.
883 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
884 ;; Needs GDB 6.0 onwards.
885 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
886 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
888 (defun gdb-non-stop-handler ()
889 (goto-char (point-min))
890 (if (re-search-forward "No symbol" nil t)
893 "This version of GDB doesn't support non-stop mode. Turning it off.")
894 (setq gdb-non-stop nil)
895 (setq gdb-supports-non-stop nil))
896 (setq gdb-supports-non-stop t)
897 (gdb-input "-gdb-set target-async 1" 'ignore)
898 (gdb-input "-list-target-features" 'gdb-check-target-async)))
900 (defun gdb-check-target-async ()
901 (goto-char (point-min))
902 (unless (re-search-forward "async" nil t)
904 "Target doesn't support non-stop mode. Turning it off.")
905 (setq gdb-non-stop nil)
906 (gdb-input "-gdb-set non-stop 0" 'ignore)))
908 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
910 (defun gdb-create-define-alist ()
911 "Create an alist of #define directives for GUD tooltips."
912 (let* ((file (buffer-file-name))
914 (with-output-to-string
915 (with-current-buffer standard-output
918 ;; call-process doesn't work with remote file names.
919 (not (file-remote-p default-directory))
920 (call-process shell-file-name file
921 (list t nil) nil "-c"
922 (concat gdb-cpp-define-alist-program " "
923 gdb-cpp-define-alist-flags))))))
924 (define-list (split-string output "\n" t))
926 (setq gdb-define-alist nil)
927 (dolist (define define-list)
928 (setq name (nth 1 (split-string define "[( ]")))
929 (push (cons name define) gdb-define-alist))))
931 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
932 (defvar tooltip-use-echo-area)
934 (defun gdb-tooltip-print (expr)
935 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
936 (goto-char (point-min))
937 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
939 (concat expr " = " (read (match-string 1)))
940 (or gud-tooltip-echo-area tooltip-use-echo-area
941 (not (display-graphic-p)))))))
943 ;; If expr is a macro for a function don't print because of possible dangerous
944 ;; side-effects. Also printing a function within a tooltip generates an
945 ;; unexpected starting annotation (phase error).
946 (defun gdb-tooltip-print-1 (expr)
947 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
948 (goto-char (point-min))
949 (if (search-forward "expands to: " nil t)
950 (unless (looking-at "\\S-+.*(.*).*")
951 (gdb-input (concat "-data-evaluate-expression " expr)
952 `(lambda () (gdb-tooltip-print ,expr)))))))
954 (defun gdb-init-buffer ()
955 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
956 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
957 (when gud-tooltip-mode
958 (make-local-variable 'gdb-define-alist)
959 (gdb-create-define-alist)
960 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
962 (defmacro gdb-if-arrow (arrow-position &rest body)
964 (let ((buffer (marker-buffer ,arrow-position)) (line))
965 (if (equal buffer (window-buffer (posn-window end)))
966 (with-current-buffer buffer
967 (when (or (equal start end)
968 (equal (posn-point start)
969 (marker-position ,arrow-position)))
972 (defun gdb-mouse-until (event)
973 "Continue running until a source line past the current line.
974 The destination source line can be selected either by clicking
975 with mouse-3 on the fringe/margin or dragging the arrow
976 with mouse-1 (default bindings)."
978 (let ((start (event-start event))
979 (end (event-end event)))
980 (gdb-if-arrow gud-overlay-arrow-position
981 (setq line (line-number-at-pos (posn-point end)))
982 (gud-call (concat "until " (number-to-string line))))
983 (gdb-if-arrow gdb-disassembly-position
985 (goto-char (point-min))
986 (forward-line (1- (line-number-at-pos (posn-point end))))
988 (gud-call (concat "until *%a"))))))
990 (defun gdb-mouse-jump (event)
991 "Set execution address/line.
992 The destination source line can be selected either by clicking with C-mouse-3
993 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
994 Unlike `gdb-mouse-until' the destination address can be before the current
995 line, and no execution takes place."
997 (let ((start (event-start event))
998 (end (event-end event)))
999 (gdb-if-arrow gud-overlay-arrow-position
1000 (setq line (line-number-at-pos (posn-point end)))
1002 (gud-call (concat "tbreak " (number-to-string line)))
1003 (gud-call (concat "jump " (number-to-string line)))))
1004 (gdb-if-arrow gdb-disassembly-position
1006 (goto-char (point-min))
1007 (forward-line (1- (line-number-at-pos (posn-point end))))
1010 (gud-call (concat "tbreak *%a"))
1011 (gud-call (concat "jump *%a")))))))
1013 (defcustom gdb-show-changed-values t
1014 "If non-nil change the face of out of scope variables and changed values.
1015 Out of scope variables are suppressed with `shadow' face.
1016 Changed values are highlighted with the face `font-lock-warning-face'."
1021 (defcustom gdb-max-children 40
1022 "Maximum number of children before expansion requires confirmation."
1027 (defcustom gdb-delete-out-of-scope t
1028 "If non-nil delete watch expressions automatically when they go out of scope."
1033 (defcustom gdb-speedbar-auto-raise nil
1034 "If non-nil raise speedbar every time display of watch expressions is\
1040 (defcustom gdb-use-colon-colon-notation nil
1041 "If non-nil use FUN::VAR format to display variables in the speedbar."
1046 (defun gdb-speedbar-auto-raise (arg)
1047 "Toggle automatic raising of the speedbar for watch expressions.
1048 With prefix argument ARG, automatically raise speedbar if ARG is
1049 positive, otherwise don't automatically raise it."
1051 (setq gdb-speedbar-auto-raise
1053 (not gdb-speedbar-auto-raise)
1054 (> (prefix-numeric-value arg) 0)))
1055 (message (format "Auto raising %sabled"
1056 (if gdb-speedbar-auto-raise "en" "dis"))))
1058 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1059 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
1061 (declare-function tooltip-identifier-from-point "tooltip" (point))
1063 (defun gud-watch (&optional arg event)
1064 "Watch expression at point.
1065 With arg, enter name of variable to be watched in the minibuffer."
1066 (interactive (list current-prefix-arg last-input-event))
1067 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1068 (if (eq minor-mode 'gdbmi)
1070 (if event (posn-set-point (event-end event)))
1072 (save-selected-window
1075 (completing-read "Name of variable: "
1076 'gud-gdb-complete-command)
1077 (if (and transient-mark-mode mark-active)
1078 (buffer-substring (region-beginning) (region-end))
1079 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1080 (tooltip-identifier-from-point (point)))))))
1081 (set-text-properties 0 (length expr) nil expr)
1082 (gdb-input (concat "-var-create - * " expr "")
1083 `(lambda () (gdb-var-create-handler ,expr))))))
1084 (message "gud-watch is a no-op in this mode."))))
1086 (defun gdb-var-create-handler (expr)
1087 (let* ((result (gdb-json-partial-output)))
1088 (if (not (bindat-get-field result 'msg))
1090 (list (bindat-get-field result 'name)
1091 (if (and (string-equal gdb-current-language "c")
1092 gdb-use-colon-colon-notation gdb-selected-frame)
1093 (setq expr (concat gdb-selected-frame "::" expr))
1095 (bindat-get-field result 'numchild)
1096 (bindat-get-field result 'type)
1097 (bindat-get-field result 'value)
1099 (bindat-get-field result 'has_more)
1100 gdb-frame-address)))
1101 (push var gdb-var-list)
1103 (unless (string-equal
1104 speedbar-initial-expansion-list-name "GUD")
1105 (speedbar-change-initial-expansion-list "GUD")))
1106 (message-box "No symbol \"%s\" in current context." expr))))
1108 (defun gdb-speedbar-update ()
1109 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1110 (not (gdb-pending-p 'gdb-speedbar-timer)))
1111 ;; Dummy command to update speedbar even when idle.
1112 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn)
1113 ;; Keep gdb-pending-triggers non-nil till end.
1114 (gdb-add-pending 'gdb-speedbar-timer)))
1116 (defun gdb-speedbar-timer-fn ()
1117 (if gdb-speedbar-auto-raise
1118 (raise-frame speedbar-frame))
1119 (gdb-delete-pending 'gdb-speedbar-timer)
1120 (speedbar-timer-fn))
1122 (defun gdb-var-evaluate-expression-handler (varnum changed)
1123 (goto-char (point-min))
1124 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1125 (let ((var (assoc varnum gdb-var-list)))
1127 (if changed (setcar (nthcdr 5 var) 'changed))
1128 (setcar (nthcdr 4 var) (read (match-string 1)))))
1129 (gdb-speedbar-update))
1131 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1132 (defun gdb-var-list-children (varnum)
1133 (gdb-input (concat "-var-update " varnum) 'ignore)
1134 (gdb-input (concat "-var-list-children --all-values " varnum)
1135 `(lambda () (gdb-var-list-children-handler ,varnum))))
1137 (defun gdb-var-list-children-handler (varnum)
1138 (let* ((var-list nil)
1139 (output (bindat-get-field (gdb-json-partial-output "child")))
1140 (children (bindat-get-field output 'children)))
1141 (catch 'child-already-watched
1142 (dolist (var gdb-var-list)
1143 (if (string-equal varnum (car var))
1145 ;; With dynamic varobjs numchild may have increased.
1146 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1148 (dolist (child children)
1149 (let ((varchild (list (bindat-get-field child 'name)
1150 (bindat-get-field child 'exp)
1151 (bindat-get-field child 'numchild)
1152 (bindat-get-field child 'type)
1153 (bindat-get-field child 'value)
1155 (bindat-get-field child 'has_more))))
1156 (if (assoc (car varchild) gdb-var-list)
1157 (throw 'child-already-watched nil))
1158 (push varchild var-list))))
1159 (push var var-list)))
1160 (setq gdb-var-list (nreverse var-list))))
1161 (gdb-speedbar-update))
1163 (defun gdb-var-set-format (format)
1164 "Set the output format for a variable displayed in the speedbar."
1165 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1167 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1170 (defun gdb-var-delete-1 (var varnum)
1171 (gdb-input (concat "-var-delete " varnum) 'ignore)
1172 (setq gdb-var-list (delq var gdb-var-list))
1173 (dolist (varchild gdb-var-list)
1174 (if (string-match (concat (car var) "\\.") (car varchild))
1175 (setq gdb-var-list (delq varchild gdb-var-list)))))
1177 (defun gdb-var-delete ()
1178 "Delete watch expression at point from the speedbar."
1180 (let ((text (speedbar-line-text)))
1181 (string-match "\\(\\S-+\\)" text)
1182 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1184 (if (string-match "\\." (car var))
1185 (message-box "Can only delete a root expression")
1186 (gdb-var-delete-1 var varnum)))))
1188 (defun gdb-var-delete-children (varnum)
1189 "Delete children of variable object at point from the speedbar."
1190 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1192 (defun gdb-edit-value (_text _token _indent)
1193 "Assign a value to a variable displayed in the speedbar."
1194 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1195 (varnum (car var)) (value))
1196 (setq value (read-string "New value: "))
1197 (gdb-input (concat "-var-assign " varnum " " value)
1198 `(lambda () (gdb-edit-value-handler ,value)))))
1200 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1202 (defun gdb-edit-value-handler (value)
1203 (goto-char (point-min))
1204 (if (re-search-forward gdb-error-regexp nil t)
1205 (message-box "Invalid number or expression (%s)" value)))
1207 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1208 (defun gdb-var-update ()
1209 (if (not (gdb-pending-p 'gdb-var-update))
1210 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler))
1211 (gdb-add-pending 'gdb-var-update))
1213 (defun gdb-var-update-handler ()
1214 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1215 (dolist (var gdb-var-list)
1216 (setcar (nthcdr 5 var) nil))
1217 (let ((temp-var-list gdb-var-list))
1218 (dolist (change changelist)
1219 (let* ((varnum (bindat-get-field change 'name))
1220 (var (assoc varnum gdb-var-list))
1221 (new-num (bindat-get-field change 'new_num_children)))
1223 (let ((scope (bindat-get-field change 'in_scope))
1224 (has-more (bindat-get-field change 'has_more)))
1225 (cond ((string-equal scope "false")
1226 (if gdb-delete-out-of-scope
1227 (gdb-var-delete-1 var varnum)
1228 (setcar (nthcdr 5 var) 'out-of-scope)))
1229 ((string-equal scope "true")
1230 (setcar (nthcdr 6 var) has-more)
1231 (when (and (or (not has-more)
1232 (string-equal has-more "0"))
1234 (string-equal (nth 2 var) "0"))
1235 (setcar (nthcdr 4 var)
1236 (bindat-get-field change 'value))
1237 (setcar (nthcdr 5 var) 'changed)))
1238 ((string-equal scope "invalid")
1239 (gdb-var-delete-1 var varnum)))))
1240 (let ((var-list nil) var1
1241 (children (bindat-get-field change 'new_children)))
1243 (setq var1 (pop temp-var-list))
1245 (if (string-equal varnum (car var1))
1246 (let ((new (string-to-number new-num))
1247 (previous (string-to-number (nth 2 var1))))
1248 (setcar (nthcdr 2 var1) new-num)
1249 (push var1 var-list)
1252 ;; Add new children to list.
1253 (dotimes (dummy previous)
1254 (push (pop temp-var-list) var-list))
1255 (dolist (child children)
1257 (list (bindat-get-field child 'name)
1258 (bindat-get-field child 'exp)
1259 (bindat-get-field child 'numchild)
1260 (bindat-get-field child 'type)
1261 (bindat-get-field child 'value)
1263 (bindat-get-field child 'has_more))))
1264 (push varchild var-list))))
1265 ;; Remove deleted children from list.
1267 (dotimes (dummy new)
1268 (push (pop temp-var-list) var-list))
1269 (dotimes (dummy (- previous new))
1270 (pop temp-var-list)))))
1271 (push var1 var-list))
1272 (setq var1 (pop temp-var-list)))
1273 (setq gdb-var-list (nreverse var-list))))))))
1274 (setq gdb-pending-triggers
1275 (delq 'gdb-var-update gdb-pending-triggers))
1276 (gdb-speedbar-update))
1278 (defun gdb-speedbar-expand-node (text token indent)
1279 "Expand the node the user clicked on.
1280 TEXT is the text of the button we clicked on, a + or - item.
1281 TOKEN is data related to this node.
1282 INDENT is the current indentation depth."
1283 (cond ((string-match "+" text) ;expand this node
1284 (let* ((var (assoc token gdb-var-list))
1285 (expr (nth 1 var)) (children (nth 2 var)))
1286 (if (or (<= (string-to-number children) gdb-max-children)
1288 (format "%s has %s children. Continue? " expr children)))
1289 (gdb-var-list-children token))))
1290 ((string-match "-" text) ;contract this node
1291 (dolist (var gdb-var-list)
1292 (if (string-match (concat token "\\.") (car var))
1293 (setq gdb-var-list (delq var gdb-var-list))))
1294 (gdb-var-delete-children token)
1295 (speedbar-change-expand-button-char ?+)
1296 (speedbar-delete-subblock indent))
1297 (t (error "Ooops... not sure what to do")))
1298 (speedbar-center-buffer-smartly))
1300 (defun gdb-get-target-string ()
1301 (with-current-buffer gud-comint-buffer
1308 ;; Each buffer has a TYPE -- a symbol that identifies the function
1309 ;; of that particular buffer.
1311 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1312 ;; is constructed specially.
1314 ;; Others are constructed by gdb-get-buffer-create and
1315 ;; named according to the rules set forth in the gdb-buffer-rules
1317 (defvar gdb-buffer-rules '())
1319 (defun gdb-rules-name-maker (rules-entry)
1321 (defun gdb-rules-buffer-mode (rules-entry)
1322 (nth 2 rules-entry))
1323 (defun gdb-rules-update-trigger (rules-entry)
1324 (nth 3 rules-entry))
1326 (defun gdb-update-buffer-name ()
1327 "Rename current buffer according to name-maker associated with
1328 it in `gdb-buffer-rules'."
1329 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1330 gdb-buffer-rules))))
1331 (when f (rename-buffer (funcall f)))))
1333 (defun gdb-current-buffer-rules ()
1334 "Get `gdb-buffer-rules' entry for current buffer type."
1335 (assoc gdb-buffer-type gdb-buffer-rules))
1337 (defun gdb-current-buffer-thread ()
1338 "Get thread object of current buffer from `gdb-threads-list'.
1340 When current buffer is not bound to any thread, return main
1342 (cdr (assoc gdb-thread-number gdb-threads-list)))
1344 (defun gdb-current-buffer-frame ()
1345 "Get current stack frame object for thread of current buffer."
1346 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1348 (defun gdb-buffer-type (buffer)
1349 "Get value of `gdb-buffer-type' for BUFFER."
1350 (with-current-buffer buffer
1353 (defun gdb-buffer-shows-main-thread-p ()
1354 "Return t if current GDB buffer shows main selected thread and
1355 is not bound to it."
1357 (not (local-variable-p 'gdb-thread-number)))
1359 (defun gdb-get-buffer (buffer-type &optional thread)
1360 "Get a specific GDB buffer.
1362 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1363 and `gdb-thread-number' (if provided) must be equal to THREAD."
1365 (dolist (buffer (buffer-list) nil)
1366 (with-current-buffer buffer
1367 (when (and (eq gdb-buffer-type buffer-type)
1369 (equal gdb-thread-number thread)))
1370 (throw 'found buffer))))))
1372 (defun gdb-get-buffer-create (buffer-type &optional thread)
1373 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1374 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1376 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1377 buffer-local variable of the new buffer.
1379 Buffer mode and name are selected according to buffer type.
1381 If buffer has trigger associated with it in `gdb-buffer-rules',
1382 this trigger is subscribed to `gdb-buf-publisher' and called with
1384 (or (gdb-get-buffer buffer-type thread)
1385 (let ((rules (assoc buffer-type gdb-buffer-rules))
1386 (new (generate-new-buffer "limbo")))
1387 (with-current-buffer new
1388 (let ((mode (gdb-rules-buffer-mode rules))
1389 (trigger (gdb-rules-update-trigger rules)))
1390 (when mode (funcall mode))
1391 (setq gdb-buffer-type buffer-type)
1393 (set (make-local-variable 'gdb-thread-number) thread))
1394 (set (make-local-variable 'gud-minor-mode)
1395 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1396 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1397 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1399 (gdb-add-subscriber gdb-buf-publisher
1400 (cons (current-buffer)
1401 (gdb-bind-function-to-buffer
1402 trigger (current-buffer))))
1403 (funcall trigger 'start))
1404 (current-buffer))))))
1406 (defun gdb-bind-function-to-buffer (expr buffer)
1407 "Return a function which will evaluate EXPR in BUFFER."
1408 `(lambda (&rest args)
1409 (with-current-buffer ,buffer
1410 (apply ',expr args))))
1412 ;; Used to define all gdb-frame-*-buffer functions except
1413 ;; `gdb-frame-io-buffer'
1414 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1415 "Define a function NAME which shows gdb BUFFER in a separate frame.
1417 DOC is an optional documentation string."
1418 `(defun ,name (&optional thread)
1421 (let ((special-display-regexps (append special-display-regexps '(".*")))
1422 (special-display-frame-alist gdb-frame-parameters))
1423 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1425 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1426 "Define a function NAME which shows gdb BUFFER.
1428 DOC is an optional documentation string."
1429 `(defun ,name (&optional thread)
1433 (gdb-get-buffer-create ,buffer thread) t)))
1435 ;; Used to display windows with thread-bound buffers
1436 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1438 `(defun ,name (&optional thread)
1441 (gdb-preempt-existing-or-display-buffer
1442 (gdb-get-buffer-create ,buffer thread)
1443 ,split-horizontal)))
1445 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1446 ;; at least one and possible more functions. The functions have these
1447 ;; roles in defining a buffer type:
1449 ;; NAME - Return a name for this buffer type.
1451 ;; The remaining function(s) are optional:
1453 ;; MODE - called in a new buffer with no arguments, should establish
1454 ;; the proper mode for the buffer.
1457 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1458 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1460 (setcdr binding rules)
1461 (push (cons buffer-type rules)
1462 gdb-buffer-rules))))
1464 (defun gdb-parent-mode ()
1465 "Generic mode to derive all other GDB buffer modes from."
1466 (kill-all-local-variables)
1467 (setq buffer-read-only t)
1468 (buffer-disable-undo)
1469 ;; Delete buffer from gdb-buf-publisher when it's killed
1470 ;; (if it has an associated update trigger)
1475 (let ((trigger (gdb-rules-update-trigger
1476 (gdb-current-buffer-rules))))
1478 (gdb-delete-subscriber
1480 ;; This should match gdb-add-subscriber done in
1481 ;; gdb-get-buffer-create
1482 (cons (current-buffer)
1483 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1486 ;; Partial-output buffer : This accumulates output from a command executed on
1487 ;; behalf of emacs (rather than the user).
1489 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1490 'gdb-partial-output-name)
1492 (defun gdb-partial-output-name ()
1493 (concat " *partial-output-"
1494 (gdb-get-target-string)
1498 (gdb-set-buffer-rules 'gdb-inferior-io
1499 'gdb-inferior-io-name
1500 'gdb-inferior-io-mode)
1502 (defun gdb-inferior-io-name ()
1503 (concat "*input/output of "
1504 (gdb-get-target-string)
1507 (defun gdb-display-io-buffer ()
1508 "Display IO of debugged program in a separate window."
1511 (gdb-get-buffer-create 'gdb-inferior-io) t))
1513 (defconst gdb-frame-parameters
1514 '((height . 14) (width . 80)
1516 (tool-bar-lines . nil)
1517 (menu-bar-lines . nil)
1518 (minibuffer . nil)))
1520 (defun gdb-frame-io-buffer ()
1521 "Display IO of debugged program in a new frame."
1523 (let ((special-display-regexps (append special-display-regexps '(".*")))
1524 (special-display-frame-alist gdb-frame-parameters))
1525 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1527 (defvar gdb-inferior-io-mode-map
1528 (let ((map (make-sparse-keymap)))
1529 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1530 (define-key map "\C-c\C-z" 'gdb-io-stop)
1531 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1532 (define-key map "\C-c\C-d" 'gdb-io-eof)
1533 (define-key map "\C-d" 'gdb-io-eof)
1536 ;; We want to use comint because it has various nifty and familiar features.
1537 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1538 "Major mode for gdb inferior-io."
1539 :syntax-table nil :abbrev-table nil
1540 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1542 (defun gdb-inferior-filter (proc string)
1543 (unless (string-equal string "")
1544 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1545 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1546 (comint-output-filter proc string)))
1548 (defun gdb-io-interrupt ()
1549 "Interrupt the program being debugged."
1552 (get-buffer-process gud-comint-buffer) comint-ptyp))
1554 (defun gdb-io-quit ()
1555 "Send quit signal to the program being debugged."
1558 (get-buffer-process gud-comint-buffer) comint-ptyp))
1560 (defun gdb-io-stop ()
1561 "Stop the program being debugged."
1564 (get-buffer-process gud-comint-buffer) comint-ptyp))
1566 (defun gdb-io-eof ()
1567 "Send end-of-file to the program being debugged."
1570 (get-buffer-process gud-comint-buffer)))
1572 (defun gdb-clear-inferior-io ()
1573 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1577 (defconst breakpoint-xpm-data
1579 static char *magick[] = {
1580 /* columns rows colors chars-per-pixel */
1596 "XPM data used for breakpoint icon.")
1598 (defconst breakpoint-enabled-pbm-data
1601 0 0 0 0 1 1 1 1 0 0 0 0
1602 0 0 0 1 1 1 1 1 1 0 0 0
1603 0 0 1 1 1 1 1 1 1 1 0 0
1604 0 1 1 1 1 1 1 1 1 1 1 0
1605 0 1 1 1 1 1 1 1 1 1 1 0
1606 0 1 1 1 1 1 1 1 1 1 1 0
1607 0 1 1 1 1 1 1 1 1 1 1 0
1608 0 0 1 1 1 1 1 1 1 1 0 0
1609 0 0 0 1 1 1 1 1 1 0 0 0
1610 0 0 0 0 1 1 1 1 0 0 0 0"
1611 "PBM data used for enabled breakpoint icon.")
1613 (defconst breakpoint-disabled-pbm-data
1625 0 0 0 1 0 1 0 1 0 0"
1626 "PBM data used for disabled breakpoint icon.")
1628 (defvar breakpoint-enabled-icon nil
1629 "Icon for enabled breakpoint in display margin.")
1631 (defvar breakpoint-disabled-icon nil
1632 "Icon for disabled breakpoint in display margin.")
1634 (declare-function define-fringe-bitmap "fringe.c"
1635 (bitmap bits &optional height width align))
1637 (and (display-images-p)
1638 ;; Bitmap for breakpoint in fringe
1639 (define-fringe-bitmap 'breakpoint
1640 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1641 ;; Bitmap for gud-overlay-arrow in fringe
1642 (define-fringe-bitmap 'hollow-right-triangle
1643 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1645 (defface breakpoint-enabled
1649 "Face for enabled breakpoint icon in fringe."
1652 (defface breakpoint-disabled
1653 '((((class color) (min-colors 88)) :foreground "grey70")
1654 ;; Ensure that on low-color displays that we end up something visible.
1655 (((class color) (min-colors 8) (background light))
1656 :foreground "black")
1657 (((class color) (min-colors 8) (background dark))
1658 :foreground "white")
1659 (((type tty) (class mono))
1661 (t :background "gray"))
1662 "Face for disabled breakpoint icon in fringe."
1666 (defun gdb-send (proc string)
1667 "A comint send filter for gdb."
1668 (with-current-buffer gud-comint-buffer
1669 (let ((inhibit-read-only t))
1670 (remove-text-properties (point-min) (point-max) '(face))))
1671 ;; mimic <RET> key to repeat previous command in GDB
1672 (if (not (string= "" string))
1673 (setq gdb-last-command string)
1674 (if gdb-last-command (setq string gdb-last-command)))
1675 (if gdb-enable-debug
1676 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1677 (if (string-match "^-" string)
1680 (setq gdb-first-done-or-error t)
1681 (process-send-string proc (concat string "\n")))
1683 (if (string-match "\\\\$" string)
1684 (setq gdb-continuation (concat gdb-continuation string "\n"))
1685 (setq gdb-first-done-or-error t)
1686 (process-send-string proc (concat "-interpreter-exec console \""
1687 gdb-continuation string "\"\n"))
1688 (setq gdb-continuation nil))))
1690 (defun gdb-input (command handler-function)
1691 "Send COMMAND to GDB via the MI interface.
1692 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1694 (if gdb-enable-debug (push (list 'send-item command handler-function)
1696 (setq gdb-token-number (1+ gdb-token-number))
1697 (setq command (concat (number-to-string gdb-token-number) command))
1698 (push (cons gdb-token-number handler-function) gdb-handler-alist)
1699 (process-send-string (get-buffer-process gud-comint-buffer)
1700 (concat command "\n")))
1702 ;; NOFRAME is used for gud execution control commands
1703 (defun gdb-current-context-command (command)
1704 "Add --thread to gdb COMMAND when needed."
1705 (if (and gdb-thread-number
1706 gdb-supports-non-stop)
1707 (concat command " --thread " gdb-thread-number)
1710 (defun gdb-current-context-buffer-name (name)
1711 "Add thread information and asterisks to string NAME.
1713 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1715 (if (local-variable-p 'gdb-thread-number)
1716 (format " (bound to thread %s)" gdb-thread-number)
1720 (defun gdb-current-context-mode-name (mode)
1721 "Add thread information to MODE which is to be used as
1724 (if gdb-thread-number
1725 (format " [thread %s]" gdb-thread-number)
1729 (defcustom gud-gdb-command-name "gdb -i=mi"
1730 "Default command to execute an executable under the GDB debugger."
1735 (setq gud-running nil)
1736 (setq gdb-output-sink 'user)
1737 (setq gdb-pending-triggers nil))
1739 (defun gdb-update ()
1740 "Update buffers showing status of debug session."
1741 (when gdb-first-prompt
1742 (gdb-force-mode-line-update
1743 (propertize "initializing..." 'face font-lock-variable-name-face))
1745 (setq gdb-first-prompt nil))
1747 (gdb-get-main-selected-frame)
1748 ;; We may need to update gdb-threads-list so we can use
1749 (gdb-get-buffer-create 'gdb-threads-buffer)
1750 ;; gdb-break-list is maintained in breakpoints handler
1751 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1753 (gdb-emit-signal gdb-buf-publisher 'update)
1755 (gdb-get-changed-registers)
1757 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1758 (dolist (var gdb-var-list)
1759 (setcar (nthcdr 5 var) nil))
1762 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1763 ;; because we may need to update current gud-running value without
1764 ;; changing current thread (see gdb-running)
1765 (defun gdb-setq-thread-number (number)
1766 "Only this function must be used to change `gdb-thread-number'
1767 value to NUMBER, because `gud-running' and `gdb-frame-number'
1768 need to be updated appropriately when current thread changes."
1769 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1770 (unless (string-equal number "0") (setq gdb-thread-number number))
1771 (setq gdb-frame-number "0")
1772 (gdb-update-gud-running))
1774 (defun gdb-update-gud-running ()
1775 "Set `gud-running' according to the state of current thread.
1777 `gdb-frame-number' is set to 0 if current thread is now stopped.
1779 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1780 cannot be reliably used to determine whether or not execution
1781 control buttons should be shown in menu or toolbar. Use
1782 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1785 For all-stop mode, thread information is unavailable while target
1787 (let ((old-value gud-running))
1789 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1791 ;; Set frame number to "0" when _current_ threads stops
1792 (when (and (gdb-current-buffer-thread)
1793 (not (eq gud-running old-value)))
1794 (setq gdb-frame-number "0"))))
1796 (defun gdb-show-run-p ()
1797 "Return t if \"Run/continue\" should be shown on the toolbar."
1798 (or (not gdb-active-process)
1800 (not gdb-gud-control-all-threads)
1803 (and gdb-gud-control-all-threads
1804 (> gdb-stopped-threads-count 0))))
1806 (defun gdb-show-stop-p ()
1807 "Return t if \"Stop\" should be shown on the toolbar."
1809 (not gdb-gud-control-all-threads)
1812 (and gdb-gud-control-all-threads
1813 (> gdb-running-threads-count 0))))
1815 ;; GUD displays the selected GDB frame. This might might not be the current
1816 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1817 ;; visited breakpoint is, use that window.
1818 (defun gdb-display-source-buffer (buffer)
1819 (let* ((last-window (if gud-last-last-frame
1821 (gud-find-file (car gud-last-last-frame)))))
1822 (source-window (or last-window
1823 (if (and gdb-source-window
1824 (window-live-p gdb-source-window))
1825 gdb-source-window))))
1827 (setq gdb-source-window source-window)
1828 (set-window-buffer source-window buffer))
1831 (defun gdb-car< (a b)
1832 (< (car a) (car b)))
1834 (defvar gdbmi-record-list
1835 '((gdb-gdb . "(gdb) \n")
1836 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1837 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1838 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1839 (gdb-console . "~\\(\".*?\"\\)\n")
1840 (gdb-internals . "&\\(\".*?\"\\)\n")
1841 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1842 (gdb-running . "\\*running,\\(.*?\n\\)")
1843 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1844 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1845 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1846 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1847 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1849 (defun gud-gdbmi-marker-filter (string)
1850 "Filter GDB/MI output."
1852 ;; Record transactions if logging is enabled.
1853 (when gdb-enable-debug
1854 (push (cons 'recv string) gdb-debug-log)
1855 (if (and gdb-debug-log-max
1856 (> (length gdb-debug-log) gdb-debug-log-max))
1857 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1859 ;; Recall the left over gud-marker-acc from last time
1860 (setq gud-marker-acc (concat gud-marker-acc string))
1862 ;; Start accumulating output for the GUD buffer
1863 (setq gdb-filter-output "")
1864 (let (output-record-list)
1866 ;; Process all the complete markers in this chunk.
1867 (dolist (gdbmi-record gdbmi-record-list)
1868 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1869 (push (list (match-beginning 0)
1871 (match-string 1 gud-marker-acc)
1872 (match-string 2 gud-marker-acc)
1875 (setq gud-marker-acc
1876 (concat (substring gud-marker-acc 0 (match-beginning 0))
1877 ;; Pad with spaces to preserve position.
1878 (make-string (length (match-string 0 gud-marker-acc)) 32)
1879 (substring gud-marker-acc (match-end 0))))))
1881 (setq output-record-list (sort output-record-list 'gdb-car<))
1883 (dolist (output-record output-record-list)
1884 (let ((record-type (cadr output-record))
1885 (arg1 (nth 2 output-record))
1886 (arg2 (nth 3 output-record)))
1887 (cond ((eq record-type 'gdb-error)
1888 (gdb-done-or-error arg2 arg1 'error))
1889 ((eq record-type 'gdb-done)
1890 (gdb-done-or-error arg2 arg1 'done))
1891 ;; Suppress "No registers." GDB 6.8 and earlier
1892 ;; duplicates MI error message on internal stream.
1893 ;; Don't print to GUD buffer.
1894 ((not (and (eq record-type 'gdb-internals)
1895 (string-equal (read arg1) "No registers.\n")))
1896 (funcall record-type arg1)))))
1898 (setq gdb-output-sink 'user)
1900 (string-match "^ *" gud-marker-acc)
1901 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1905 (defun gdb-gdb (_output-field))
1907 (defun gdb-shell (output-field)
1908 (let ((gdb-output-sink gdb-output-sink))
1909 (setq gdb-filter-output
1910 (concat output-field gdb-filter-output))))
1912 (defun gdb-ignored-notification (_output-field))
1914 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1915 (defun gdb-thread-created (_output-field))
1916 (defun gdb-thread-exited (output-field)
1917 "Handle =thread-exited async record: unset `gdb-thread-number'
1918 if current thread exited and update threads list."
1919 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1920 (if (string= gdb-thread-number thread-id)
1921 (gdb-setq-thread-number nil))
1922 ;; When we continue current thread and it quickly exits,
1923 ;; gdb-pending-triggers left after gdb-running disallow us to
1924 ;; properly call -thread-info without --thread option. Thus we
1925 ;; need to use gdb-wait-for-pending.
1926 (gdb-wait-for-pending
1927 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1929 (defun gdb-thread-selected (output-field)
1930 "Handler for =thread-selected MI output record.
1932 Sets `gdb-thread-number' to new id."
1933 (let* ((result (gdb-json-string output-field))
1934 (thread-id (bindat-get-field result 'id)))
1935 (gdb-setq-thread-number thread-id)
1936 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1937 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1938 ;; as usually. Things happen to fast and second call (from
1939 ;; gdb-thread-selected handler) gets cut off by our beloved
1940 ;; gdb-pending-triggers.
1941 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1942 ;; body will get executed when `gdb-pending-triggers` is empty.
1943 (gdb-wait-for-pending
1946 (defun gdb-running (output-field)
1948 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
1949 ;; We reset gdb-frame-number to nil if current thread has gone
1950 ;; running. This can't be done in gdb-thread-list-handler-custom
1951 ;; because we need correct gdb-frame-number by the time
1952 ;; -thread-info command is sent.
1953 (when (or (string-equal thread-id "all")
1954 (string-equal thread-id gdb-thread-number))
1955 (setq gdb-frame-number nil)))
1956 (setq gdb-inferior-status "running")
1957 (gdb-force-mode-line-update
1958 (propertize gdb-inferior-status 'face font-lock-type-face))
1959 (when (not gdb-non-stop)
1960 (setq gud-running t))
1961 (setq gdb-active-process t)
1962 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1964 (defun gdb-starting (_output-field)
1965 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1966 (setq gdb-inferior-status "running")
1967 (gdb-force-mode-line-update
1968 (propertize gdb-inferior-status 'face font-lock-type-face))
1969 (setq gdb-active-process t)
1970 (setq gud-running t)
1971 ;; GDB doesn't seem to respond to -thread-info before first stop or
1972 ;; thread exit (even in non-stop mode), so this is useless.
1973 ;; Behavior may change in the future.
1974 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1976 ;; -break-insert -t didn't give a reason before gdb 6.9
1978 (defun gdb-stopped (output-field)
1979 "Given the contents of *stopped MI async record, select new
1980 current thread and update GDB buffers."
1981 ;; Reason is available with target-async only
1982 (let* ((result (gdb-json-string output-field))
1983 (reason (bindat-get-field result 'reason))
1984 (thread-id (bindat-get-field result 'thread-id)))
1986 ;; -data-list-register-names needs to be issued for any stopped
1988 (when (not gdb-register-names)
1989 (gdb-input (concat "-data-list-register-names"
1990 (if gdb-supports-non-stop
1991 (concat " --thread " thread-id)))
1992 'gdb-register-names-handler))
1994 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1995 ;;; because synchronous GDB doesn't give these fields with CLI.
1998 ;;; ;; Extract the frame position from the marker.
1999 ;;; gud-last-frame (cons file
2000 ;;; (string-to-number
2001 ;;; (match-string 6 gud-marker-acc)))))
2003 (setq gdb-inferior-status (or reason "unknown"))
2004 (gdb-force-mode-line-update
2005 (propertize gdb-inferior-status 'face font-lock-warning-face))
2006 (if (string-equal reason "exited-normally")
2007 (setq gdb-active-process nil))
2009 ;; Select new current thread.
2011 ;; Don't switch if we have no reasons selected
2012 (when gdb-switch-reasons
2013 ;; Switch from another stopped thread only if we have
2014 ;; gdb-switch-when-another-stopped:
2015 (when (or gdb-switch-when-another-stopped
2016 (not (string= "stopped"
2017 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2018 ;; Switch if current reason has been selected or we have no
2020 (if (or (eq gdb-switch-reasons t)
2021 (member reason gdb-switch-reasons))
2022 (when (not (string-equal gdb-thread-number thread-id))
2023 (message (concat "Switched to thread " thread-id))
2024 (gdb-setq-thread-number thread-id))
2025 (message (format "Thread %s stopped" thread-id)))))
2027 ;; Print "(gdb)" to GUD console
2028 (when gdb-first-done-or-error
2029 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2031 ;; In non-stop, we update information as soon as another thread gets
2033 (when (or gdb-first-done-or-error
2035 ;; In all-stop this updates gud-running properly as well.
2037 (setq gdb-first-done-or-error nil))
2038 (run-hook-with-args 'gdb-stopped-hooks result)))
2040 ;; Remove the trimmings from log stream containing debugging messages
2041 ;; being produced by GDB's internals, use warning face and send to GUD
2043 (defun gdb-internals (output-field)
2044 (setq gdb-filter-output
2047 (let ((error-message
2048 (read output-field)))
2050 0 (length error-message)
2051 'face font-lock-warning-face
2055 ;; Remove the trimmings from the console stream and send to GUD buffer
2056 ;; (frontend MI commands should not print to this stream)
2057 (defun gdb-console (output-field)
2058 (setq gdb-filter-output
2059 (gdb-concat-output gdb-filter-output (read output-field))))
2061 (defun gdb-done-or-error (output-field token-number type)
2062 (if (string-equal token-number "")
2063 ;; Output from command entered by user
2065 (setq gdb-output-sink 'user)
2066 (setq token-number nil)
2067 ;; MI error - send to minibuffer
2068 (when (eq type 'error)
2069 ;; Skip "msg=" from `output-field'
2070 (message (read (substring output-field 4)))
2071 ;; Don't send to the console twice. (If it is a console error
2072 ;; it is also in the console stream.)
2073 (setq output-field nil)))
2074 ;; Output from command from frontend.
2075 (setq gdb-output-sink 'emacs))
2077 (gdb-clear-partial-output)
2078 (when gdb-first-done-or-error
2079 (unless (or token-number gud-running)
2080 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2082 (setq gdb-first-done-or-error nil))
2084 (setq gdb-filter-output
2085 (gdb-concat-output gdb-filter-output output-field))
2089 (with-current-buffer
2090 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2092 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2093 (setq gdb-handler-alist
2094 (assq-delete-all token-number gdb-handler-alist)))))
2096 (defun gdb-concat-output (so-far new)
2098 ((eq gdb-output-sink 'user) (concat so-far new))
2099 ((eq gdb-output-sink 'emacs)
2100 (gdb-append-to-partial-output new)
2103 (defun gdb-append-to-partial-output (string)
2104 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2105 (goto-char (point-max))
2108 (defun gdb-clear-partial-output ()
2109 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2112 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2113 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2115 Field names are wrapped in double quotes and equal signs are
2116 replaced with semicolons.
2118 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2119 partial output. This is used to get rid of useless keys in lists
2120 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2121 -break-info are examples of MI commands which issue such
2124 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2125 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2126 -break-info output when it contains breakpoint script field
2127 incompatible with GDB/MI output syntax."
2129 (goto-char (point-min))
2132 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2133 (replace-match "" nil nil nil 1))))
2136 ;; Find positions of braces which enclose broken list
2137 (while (re-search-forward (concat fix-list "={\"") nil t)
2138 (let ((p1 (goto-char (- (point) 2)))
2139 (p2 (progn (forward-sexp)
2141 ;; Replace braces with brackets
2149 (goto-char (point-min))
2151 (while (re-search-forward
2152 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2153 (replace-match "\"\\1\":\\2" nil nil))
2154 (goto-char (point-max))
2157 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2158 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2160 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2161 (gdb-jsonify-buffer fix-key fix-list)
2163 (goto-char (point-min))
2164 (let ((json-array-type 'list))
2167 (defun gdb-json-string (string &optional fix-key fix-list)
2168 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2170 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2173 (gdb-json-read-buffer fix-key fix-list)))
2175 (defun gdb-json-partial-output (&optional fix-key fix-list)
2176 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2178 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2179 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2180 (gdb-json-read-buffer fix-key fix-list)))
2182 (defun gdb-line-posns (line)
2183 "Return a pair of LINE beginning and end positions."
2184 (let ((offset (1+ (- line (line-number-at-pos)))))
2186 (line-beginning-position offset)
2187 (line-end-position offset))))
2189 (defmacro gdb-mark-line (line variable)
2190 "Set VARIABLE marker to point at beginning of LINE.
2192 If current window has no fringes, inverse colors on LINE.
2194 Return position where LINE begins."
2196 (let* ((posns (gdb-line-posns ,line))
2197 (start-posn (car posns))
2198 (end-posn (cdr posns)))
2199 (set-marker ,variable (copy-marker start-posn))
2200 (when (not (> (car (window-fringes)) 0))
2201 (put-text-property start-posn end-posn
2202 'font-lock-face '(:inverse-video t)))
2205 (defun gdb-pad-string (string padding)
2206 (format (concat "%" (number-to-string padding) "s") string))
2208 ;; gdb-table struct is a way to programmatically construct simple
2209 ;; tables. It help to reliably align columns of data in GDB buffers
2215 (row-properties nil)
2218 (defun gdb-mapcar* (function &rest seqs)
2219 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2220 If there are several SEQS, FUNCTION is called with that many
2221 arguments, and mapping stops as soon as the shortest list runs
2223 (let ((shortest (apply #'min (mapcar #'length seqs))))
2230 (number-sequence 0 (1- shortest)))))
2232 (defun gdb-table-add-row (table row &optional properties)
2233 "Add ROW of string to TABLE and recalculate column sizes.
2235 When non-nil, PROPERTIES will be added to the whole row when
2236 calling `gdb-table-string'."
2237 (let ((rows (gdb-table-rows table))
2238 (row-properties (gdb-table-row-properties table))
2239 (column-sizes (gdb-table-column-sizes table))
2240 (right-align (gdb-table-right-align table)))
2241 (when (not column-sizes)
2242 (setf (gdb-table-column-sizes table)
2243 (make-list (length row) 0)))
2244 (setf (gdb-table-rows table)
2245 (append rows (list row)))
2246 (setf (gdb-table-row-properties table)
2247 (append row-properties (list properties)))
2248 (setf (gdb-table-column-sizes table)
2249 (gdb-mapcar* (lambda (x s)
2251 (max (abs x) (string-width (or s "")))))
2252 (if right-align new-x (- new-x))))
2253 (gdb-table-column-sizes table)
2255 ;; Avoid trailing whitespace at eol
2256 (if (not (gdb-table-right-align table))
2257 (setcar (last (gdb-table-column-sizes table)) 0))))
2259 (defun gdb-table-string (table &optional sep)
2260 "Return TABLE as a string with columns separated with SEP."
2261 (let ((column-sizes (gdb-table-column-sizes table)))
2265 (lambda (row properties)
2267 (mapconcat 'identity
2268 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2272 (gdb-table-rows table)
2273 (gdb-table-row-properties table))
2276 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2277 (defun gdb-get-many-fields (struct &rest fields)
2278 "Return a list of FIELDS values from STRUCT."
2280 (dolist (field fields values)
2281 (setq values (append values (list (bindat-get-field struct field)))))))
2283 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2285 &optional signal-list)
2286 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2287 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2288 buffer with `gdb-bind-function-to-buffer'.
2290 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2291 defined trigger is called with an argument from SIGNAL-LIST. It's
2292 not recommended to define triggers with empty SIGNAL-LIST.
2293 Normally triggers should respond at least to 'update signal.
2295 Normally the trigger defined by this command must be called from
2296 the buffer where HANDLER-NAME must work. This should be done so
2297 that buffer-local thread number may be used in GDB-COMMAND (by
2298 calling `gdb-current-context-command').
2299 `gdb-bind-function-to-buffer' is used to achieve this, see
2300 `gdb-get-buffer-create'.
2302 Triggers defined by this command are meant to be used as a
2303 trigger argument when describing buffer types with
2304 `gdb-set-buffer-rules'."
2305 `(defun ,trigger-name (&optional signal)
2307 (or (not ,signal-list)
2308 (memq signal ,signal-list))
2309 (when (not (gdb-pending-p
2310 (cons (current-buffer) ',trigger-name)))
2311 (gdb-input ,gdb-command
2312 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2313 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2315 ;; Used by disassembly buffer only, the rest use
2316 ;; def-gdb-trigger-and-handler
2317 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2318 &optional nopreserve)
2319 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2321 Handlers are normally called from the buffers they put output in.
2323 Delete ((current-buffer) . TRIGGER-NAME) from
2324 `gdb-pending-triggers', erase current buffer and evaluate
2325 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2327 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2328 `(defun ,handler-name ()
2329 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2330 (let* ((buffer-read-only nil)
2331 (window (get-buffer-window (current-buffer) 0))
2332 (start (window-start window))
2333 (p (window-point window)))
2336 (gdb-update-buffer-name)
2337 ,(when (not nopreserve)
2338 '(set-window-start window start)
2339 '(set-window-point window p)))))
2341 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2342 handler-name custom-defun
2343 &optional signal-list)
2344 "Define trigger and handler.
2346 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2347 `def-gdb-auto-update-trigger'.
2349 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2350 `def-gdb-auto-update-handler'."
2352 (def-gdb-auto-update-trigger ,trigger-name
2354 ,handler-name ,signal-list)
2355 (def-gdb-auto-update-handler ,handler-name
2356 ,trigger-name ,custom-defun)))
2360 ;; Breakpoint buffer : This displays the output of `-break-list'.
2361 (def-gdb-trigger-and-handler
2362 gdb-invalidate-breakpoints "-break-list"
2363 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2366 (gdb-set-buffer-rules
2367 'gdb-breakpoints-buffer
2368 'gdb-breakpoints-buffer-name
2369 'gdb-breakpoints-mode
2370 'gdb-invalidate-breakpoints)
2372 (defun gdb-breakpoints-list-handler-custom ()
2373 (let ((breakpoints-list (bindat-get-field
2374 (gdb-json-partial-output "bkpt" "script")
2375 'BreakpointTable 'body))
2376 (table (make-gdb-table)))
2377 (setq gdb-breakpoints-list nil)
2378 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2379 (dolist (breakpoint breakpoints-list)
2380 (add-to-list 'gdb-breakpoints-list
2381 (cons (bindat-get-field breakpoint 'number)
2383 (let ((at (bindat-get-field breakpoint 'at))
2384 (pending (bindat-get-field breakpoint 'pending))
2385 (func (bindat-get-field breakpoint 'func))
2386 (type (bindat-get-field breakpoint 'type)))
2387 (gdb-table-add-row table
2389 (bindat-get-field breakpoint 'number)
2391 (bindat-get-field breakpoint 'disp)
2392 (let ((flag (bindat-get-field breakpoint 'enabled)))
2393 (if (string-equal flag "y")
2394 (propertize "y" 'font-lock-face font-lock-warning-face)
2395 (propertize "n" 'font-lock-face font-lock-comment-face)))
2396 (bindat-get-field breakpoint 'addr)
2397 (bindat-get-field breakpoint 'times)
2398 (if (string-match ".*watchpoint" type)
2399 (bindat-get-field breakpoint 'what)
2402 (propertize (or func "unknown")
2403 'font-lock-face font-lock-function-name-face)
2404 (gdb-frame-location breakpoint)))))
2405 ;; Add clickable properties only for breakpoints with file:line
2407 (append (list 'gdb-breakpoint breakpoint)
2408 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2409 mouse-face highlight))))))
2410 (insert (gdb-table-string table " "))
2411 (gdb-place-breakpoints)))
2413 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2414 (defun gdb-place-breakpoints ()
2415 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2416 (dolist (buffer (buffer-list))
2417 (with-current-buffer buffer
2418 (if (and (eq gud-minor-mode 'gdbmi)
2419 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2420 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2421 (dolist (breakpoint gdb-breakpoints-list)
2422 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2423 ; an associative list
2424 (line (bindat-get-field breakpoint 'line)))
2426 (let ((file (bindat-get-field breakpoint 'fullname))
2427 (flag (bindat-get-field breakpoint 'enabled))
2428 (bptno (bindat-get-field breakpoint 'number)))
2429 (unless (file-exists-p file)
2430 (setq file (cdr (assoc bptno gdb-location-alist))))
2432 (not (string-equal file "File not found")))
2433 (with-current-buffer
2434 (find-file-noselect file 'nowarn)
2436 ;; Only want one breakpoint icon at each location.
2437 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2438 (string-to-number line)))
2439 (gdb-input (concat "list " file ":1") 'ignore)
2440 (gdb-input "-file-list-exec-source-file"
2441 `(lambda () (gdb-get-location
2442 ,bptno ,line ,flag)))))))))
2444 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2446 (defun gdb-get-location (bptno line flag)
2447 "Find the directory containing the relevant source file.
2448 Put in buffer and place breakpoint icon."
2449 (goto-char (point-min))
2450 (catch 'file-not-found
2451 (if (re-search-forward gdb-source-file-regexp nil t)
2452 (delete (cons bptno "File not found") gdb-location-alist)
2453 (push (cons bptno (match-string 1)) gdb-location-alist)
2455 (unless (assoc bptno gdb-location-alist)
2456 (push (cons bptno "File not found") gdb-location-alist)
2457 (message-box "Cannot find source file for breakpoint location.
2458 Add directory to search path for source files using the GDB command, dir."))
2459 (throw 'file-not-found nil))
2460 (with-current-buffer (find-file-noselect (match-string 1))
2462 ;; only want one breakpoint icon at each location
2463 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2465 (add-hook 'find-file-hook 'gdb-find-file-hook)
2467 (defun gdb-find-file-hook ()
2468 "Set up buffer for debugging if file is part of the source code
2469 of the current session."
2470 (if (and (buffer-name gud-comint-buffer)
2471 ;; in case gud or gdb-ui is just loaded
2473 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2475 (if (member buffer-file-name gdb-source-file-list)
2476 (with-current-buffer (find-buffer-visiting buffer-file-name)
2477 (gdb-init-buffer)))))
2479 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2480 (declare-function gud-break "gdb-mi" t t) ; gud-def
2481 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2483 (defun gdb-mouse-set-clear-breakpoint (event)
2484 "Set/clear breakpoint in left fringe/margin at mouse click.
2485 If not in a source or disassembly buffer just set point."
2487 (mouse-minibuffer-check event)
2488 (let ((posn (event-end event)))
2489 (with-selected-window (posn-window posn)
2490 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2491 (if (numberp (posn-point posn))
2493 (goto-char (posn-point posn))
2494 (if (or (posn-object posn)
2495 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2499 (posn-set-point posn))))
2501 (defun gdb-mouse-toggle-breakpoint-margin (event)
2502 "Enable/disable breakpoint in left margin with mouse click."
2504 (mouse-minibuffer-check event)
2505 (let ((posn (event-end event)))
2506 (if (numberp (posn-point posn))
2507 (with-selected-window (posn-window posn)
2509 (goto-char (posn-point posn))
2510 (if (posn-object posn)
2512 (let ((bptno (get-text-property
2513 0 'gdb-bptno (car (posn-string posn)))))
2515 (if (get-text-property
2516 0 'gdb-enabled (car (posn-string posn)))
2521 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2522 "Enable/disable breakpoint in left fringe with mouse click."
2524 (mouse-minibuffer-check event)
2525 (let* ((posn (event-end event))
2526 (pos (posn-point posn))
2529 (with-selected-window (posn-window posn)
2530 (with-current-buffer (window-buffer (selected-window))
2532 (dolist (overlay (overlays-in pos pos))
2533 (when (overlay-get overlay 'put-break)
2534 (setq obj (overlay-get overlay 'before-string))))
2538 (if (get-text-property 0 'gdb-enabled obj)
2541 (get-text-property 0 'gdb-bptno obj)))))))))
2543 (defun gdb-breakpoints-buffer-name ()
2544 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2546 (def-gdb-display-buffer
2547 gdb-display-breakpoints-buffer
2548 'gdb-breakpoints-buffer
2549 "Display status of user-settable breakpoints.")
2551 (def-gdb-frame-for-buffer
2552 gdb-frame-breakpoints-buffer
2553 'gdb-breakpoints-buffer
2554 "Display status of user-settable breakpoints in a new frame.")
2556 (defvar gdb-breakpoints-mode-map
2557 (let ((map (make-sparse-keymap))
2558 (menu (make-sparse-keymap "Breakpoints")))
2559 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2560 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2561 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2562 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2563 (suppress-keymap map)
2564 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2565 (define-key map " " 'gdb-toggle-breakpoint)
2566 (define-key map "D" 'gdb-delete-breakpoint)
2567 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2568 (define-key map "q" 'gdb-delete-frame-or-window)
2569 (define-key map "\r" 'gdb-goto-breakpoint)
2570 (define-key map "\t" (lambda ()
2572 (gdb-set-window-buffer
2573 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2574 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2575 (define-key map [follow-link] 'mouse-face)
2578 (defun gdb-delete-frame-or-window ()
2579 "Delete frame if there is only one window. Otherwise delete the window."
2581 (if (one-window-p) (delete-frame)
2584 ;;from make-mode-line-mouse-map
2585 (defun gdb-make-header-line-mouse-map (mouse function) "\
2586 Return a keymap with single entry for mouse key MOUSE on the header line.
2587 MOUSE is defined to run function FUNCTION with no args in the buffer
2588 corresponding to the mode line clicked."
2589 (let ((map (make-sparse-keymap)))
2590 (define-key map (vector 'header-line mouse) function)
2591 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2594 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2596 'help-echo ,help-echo
2597 'mouse-face ',mouse-face
2600 (gdb-make-header-line-mouse-map
2602 (lambda (event) (interactive "e")
2603 (save-selected-window
2604 (select-window (posn-window (event-start event)))
2605 (gdb-set-window-buffer
2606 (gdb-get-buffer-create ',buffer) t) )))))
2609 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2612 (defun gdb-threads-buffer-name ()
2613 (concat "*threads of " (gdb-get-target-string) "*"))
2615 (def-gdb-display-buffer
2616 gdb-display-threads-buffer
2618 "Display GDB threads.")
2620 (def-gdb-frame-for-buffer
2621 gdb-frame-threads-buffer
2623 "Display GDB threads in a new frame.")
2625 (def-gdb-trigger-and-handler
2626 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2627 gdb-thread-list-handler gdb-thread-list-handler-custom
2628 '(start update update-threads))
2630 (gdb-set-buffer-rules
2632 'gdb-threads-buffer-name
2634 'gdb-invalidate-threads)
2636 (defvar gdb-threads-font-lock-keywords
2637 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2638 (" \\(stopped\\)" (1 font-lock-warning-face))
2639 (" \\(running\\)" (1 font-lock-string-face))
2640 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2641 "Font lock keywords used in `gdb-threads-mode'.")
2643 (defvar gdb-threads-mode-map
2644 (let ((map (make-sparse-keymap)))
2645 (define-key map "\r" 'gdb-select-thread)
2646 (define-key map "f" 'gdb-display-stack-for-thread)
2647 (define-key map "F" 'gdb-frame-stack-for-thread)
2648 (define-key map "l" 'gdb-display-locals-for-thread)
2649 (define-key map "L" 'gdb-frame-locals-for-thread)
2650 (define-key map "r" 'gdb-display-registers-for-thread)
2651 (define-key map "R" 'gdb-frame-registers-for-thread)
2652 (define-key map "d" 'gdb-display-disassembly-for-thread)
2653 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2654 (define-key map "i" 'gdb-interrupt-thread)
2655 (define-key map "c" 'gdb-continue-thread)
2656 (define-key map "s" 'gdb-step-thread)
2657 (define-key map "\t"
2660 (gdb-set-window-buffer
2661 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2662 (define-key map [mouse-2] 'gdb-select-thread)
2663 (define-key map [follow-link] 'mouse-face)
2666 (defvar gdb-threads-header
2668 (gdb-propertize-header
2669 "Breakpoints" gdb-breakpoints-buffer
2670 "mouse-1: select" mode-line-highlight mode-line-inactive)
2672 (gdb-propertize-header "Threads" gdb-threads-buffer
2673 nil nil mode-line)))
2675 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2676 "Major mode for GDB threads."
2677 (setq gdb-thread-position (make-marker))
2678 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2679 (setq header-line-format gdb-threads-header)
2680 (set (make-local-variable 'font-lock-defaults)
2681 '(gdb-threads-font-lock-keywords))
2682 'gdb-invalidate-threads)
2684 (defun gdb-thread-list-handler-custom ()
2685 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2686 (table (make-gdb-table))
2688 (setq gdb-threads-list nil)
2689 (setq gdb-running-threads-count 0)
2690 (setq gdb-stopped-threads-count 0)
2691 (set-marker gdb-thread-position nil)
2693 (dolist (thread (reverse threads-list))
2694 (let ((running (equal (bindat-get-field thread 'state) "running")))
2695 (add-to-list 'gdb-threads-list
2696 (cons (bindat-get-field thread 'id)
2699 (incf gdb-running-threads-count)
2700 (incf gdb-stopped-threads-count))
2702 (gdb-table-add-row table
2704 (bindat-get-field thread 'id)
2706 (if gdb-thread-buffer-verbose-names
2707 (concat (bindat-get-field thread 'target-id) " ") "")
2708 (bindat-get-field thread 'state)
2709 ;; Include frame information for stopped threads
2712 " in " (bindat-get-field thread 'frame 'func)
2713 (if gdb-thread-buffer-arguments
2716 (let ((args (bindat-get-field thread 'frame 'args)))
2719 (apply #'format "%s=%s"
2720 (gdb-get-many-fields arg 'name 'value)))
2724 (if gdb-thread-buffer-locations
2725 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2726 (if gdb-thread-buffer-addresses
2727 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2731 'mouse-face 'highlight
2732 'help-echo "mouse-2, RET: select thread")))
2733 (when (string-equal gdb-thread-number
2734 (bindat-get-field thread 'id))
2735 (setq marked-line (length gdb-threads-list))))
2736 (insert (gdb-table-string table " "))
2738 (gdb-mark-line marked-line gdb-thread-position)))
2739 ;; We update gud-running here because we need to make sure that
2740 ;; gdb-threads-list is up-to-date
2741 (gdb-update-gud-running)
2742 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2744 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2745 "Define a NAME command which will act upon thread on the current line.
2747 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2748 be the value of 'gdb-thread property of the current line. If
2749 'gdb-thread is nil, error is signaled."
2750 `(defun ,name (&optional event)
2752 (interactive (list last-input-event))
2753 (if event (posn-set-point (event-end event)))
2756 (let ((thread (get-text-property (point) 'gdb-thread)))
2759 (error "Not recognized as thread line"))))))
2761 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2763 "Define a NAME which will call BUFFER-COMMAND with id of thread
2764 on the current line."
2765 `(def-gdb-thread-buffer-command ,name
2766 (,buffer-command (bindat-get-field thread 'id))
2769 (def-gdb-thread-buffer-command gdb-select-thread
2770 (let ((new-id (bindat-get-field thread 'id)))
2771 (gdb-setq-thread-number new-id)
2772 (gdb-input (concat "-thread-select " new-id) 'ignore)
2774 "Select the thread at current line of threads buffer.")
2776 (def-gdb-thread-buffer-simple-command
2777 gdb-display-stack-for-thread
2778 gdb-preemptively-display-stack-buffer
2779 "Display stack buffer for the thread at current line.")
2781 (def-gdb-thread-buffer-simple-command
2782 gdb-display-locals-for-thread
2783 gdb-preemptively-display-locals-buffer
2784 "Display locals buffer for the thread at current line.")
2786 (def-gdb-thread-buffer-simple-command
2787 gdb-display-registers-for-thread
2788 gdb-preemptively-display-registers-buffer
2789 "Display registers buffer for the thread at current line.")
2791 (def-gdb-thread-buffer-simple-command
2792 gdb-display-disassembly-for-thread
2793 gdb-preemptively-display-disassembly-buffer
2794 "Display disassembly buffer for the thread at current line.")
2796 (def-gdb-thread-buffer-simple-command
2797 gdb-frame-stack-for-thread
2798 gdb-frame-stack-buffer
2799 "Display a new frame with stack buffer for the thread at
2802 (def-gdb-thread-buffer-simple-command
2803 gdb-frame-locals-for-thread
2804 gdb-frame-locals-buffer
2805 "Display a new frame with locals buffer for the thread at
2808 (def-gdb-thread-buffer-simple-command
2809 gdb-frame-registers-for-thread
2810 gdb-frame-registers-buffer
2811 "Display a new frame with registers buffer for the thread at
2814 (def-gdb-thread-buffer-simple-command
2815 gdb-frame-disassembly-for-thread
2816 gdb-frame-disassembly-buffer
2817 "Display a new frame with disassembly buffer for the thread at
2820 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2821 "Define a NAME which will execute GUD-COMMAND with
2822 `gdb-thread-number' locally bound to id of thread on the current
2824 `(def-gdb-thread-buffer-command ,name
2826 (let ((gdb-thread-number (bindat-get-field thread 'id))
2827 (gdb-gud-control-all-threads nil))
2828 (call-interactively #',gud-command))
2829 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2832 (def-gdb-thread-buffer-gud-command
2833 gdb-interrupt-thread
2835 "Interrupt thread at current line.")
2837 (def-gdb-thread-buffer-gud-command
2840 "Continue thread at current line.")
2842 (def-gdb-thread-buffer-gud-command
2845 "Step thread at current line.")
2850 (defcustom gdb-memory-rows 8
2851 "Number of data rows in memory window."
2856 (defcustom gdb-memory-columns 4
2857 "Number of data columns in memory window."
2862 (defcustom gdb-memory-format "x"
2863 "Display format of data items in memory window."
2864 :type '(choice (const :tag "Hexadecimal" "x")
2865 (const :tag "Signed decimal" "d")
2866 (const :tag "Unsigned decimal" "u")
2867 (const :tag "Octal" "o")
2868 (const :tag "Binary" "t"))
2872 (defcustom gdb-memory-unit 4
2873 "Unit size of data items in memory window."
2874 :type '(choice (const :tag "Byte" 1)
2875 (const :tag "Halfword" 2)
2876 (const :tag "Word" 4)
2877 (const :tag "Giant word" 8))
2881 (def-gdb-trigger-and-handler
2882 gdb-invalidate-memory
2883 (format "-data-read-memory %s %s %d %d %d"
2889 gdb-read-memory-handler
2890 gdb-read-memory-custom
2893 (gdb-set-buffer-rules
2895 'gdb-memory-buffer-name
2897 'gdb-invalidate-memory)
2899 (defun gdb-memory-column-width (size format)
2900 "Return length of string with memory unit of SIZE in FORMAT.
2902 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2903 in `gdb-memory-format'."
2904 (let ((format-base (cdr (assoc format
2906 ("d" . 10) ("u" . 10)
2910 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2911 (cond ((string-equal format "x")
2912 (+ 2 res)) ; hexadecimal numbers have 0x in front
2913 ((or (string-equal format "d")
2914 (string-equal format "o"))
2917 (error "Unknown format"))))
2919 (defun gdb-read-memory-custom ()
2920 (let* ((res (gdb-json-partial-output))
2921 (err-msg (bindat-get-field res 'msg)))
2923 (let ((memory (bindat-get-field res 'memory)))
2924 (setq gdb-memory-address (bindat-get-field res 'addr))
2925 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2926 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2927 (setq gdb-memory-last-address gdb-memory-address)
2928 (dolist (row memory)
2929 (insert (concat (bindat-get-field row 'addr) ":"))
2930 (dolist (column (bindat-get-field row 'data))
2931 (insert (gdb-pad-string column
2932 (+ 2 (gdb-memory-column-width
2934 gdb-memory-format)))))
2936 ;; Show last page instead of empty buffer when out of bounds
2938 (let ((gdb-memory-address gdb-memory-last-address))
2939 (gdb-invalidate-memory 'update)
2940 (error err-msg))))))
2942 (defvar gdb-memory-mode-map
2943 (let ((map (make-sparse-keymap)))
2944 (suppress-keymap map t)
2945 (define-key map "q" 'kill-this-buffer)
2946 (define-key map "n" 'gdb-memory-show-next-page)
2947 (define-key map "p" 'gdb-memory-show-previous-page)
2948 (define-key map "a" 'gdb-memory-set-address)
2949 (define-key map "t" 'gdb-memory-format-binary)
2950 (define-key map "o" 'gdb-memory-format-octal)
2951 (define-key map "u" 'gdb-memory-format-unsigned)
2952 (define-key map "d" 'gdb-memory-format-signed)
2953 (define-key map "x" 'gdb-memory-format-hexadecimal)
2954 (define-key map "b" 'gdb-memory-unit-byte)
2955 (define-key map "h" 'gdb-memory-unit-halfword)
2956 (define-key map "w" 'gdb-memory-unit-word)
2957 (define-key map "g" 'gdb-memory-unit-giant)
2958 (define-key map "R" 'gdb-memory-set-rows)
2959 (define-key map "C" 'gdb-memory-set-columns)
2962 (defun gdb-memory-set-address-event (event)
2963 "Handle a click on address field in memory buffer header."
2965 (save-selected-window
2966 (select-window (posn-window (event-start event)))
2967 (gdb-memory-set-address)))
2969 ;; Non-event version for use within keymap
2970 (defun gdb-memory-set-address ()
2971 "Set the start memory address."
2973 (let ((arg (read-from-minibuffer "Memory address: ")))
2974 (setq gdb-memory-address arg))
2975 (gdb-invalidate-memory 'update))
2977 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2978 "Define a function NAME which reads new VAR value from minibuffer."
2979 `(defun ,name (event)
2982 (save-selected-window
2983 (select-window (posn-window (event-start event)))
2984 (let* ((arg (read-from-minibuffer ,echo-string))
2985 (count (string-to-number arg)))
2987 (error "Positive number only")
2988 (customize-set-variable ',variable count)
2989 (gdb-invalidate-memory 'update))))))
2991 (def-gdb-set-positive-number
2995 "Set the number of data rows in memory window.")
2997 (def-gdb-set-positive-number
2998 gdb-memory-set-columns
3001 "Set the number of data columns in memory window.")
3003 (defmacro def-gdb-memory-format (name format doc)
3004 "Define a function NAME to switch memory buffer to use FORMAT.
3006 DOC is an optional documentation string."
3007 `(defun ,name () ,(when doc doc)
3009 (customize-set-variable 'gdb-memory-format ,format)
3010 (gdb-invalidate-memory 'update)))
3012 (def-gdb-memory-format
3013 gdb-memory-format-binary "t"
3014 "Set the display format to binary.")
3016 (def-gdb-memory-format
3017 gdb-memory-format-octal "o"
3018 "Set the display format to octal.")
3020 (def-gdb-memory-format
3021 gdb-memory-format-unsigned "u"
3022 "Set the display format to unsigned decimal.")
3024 (def-gdb-memory-format
3025 gdb-memory-format-signed "d"
3026 "Set the display format to decimal.")
3028 (def-gdb-memory-format
3029 gdb-memory-format-hexadecimal "x"
3030 "Set the display format to hexadecimal.")
3032 (defvar gdb-memory-format-map
3033 (let ((map (make-sparse-keymap)))
3034 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3036 "Keymap to select format in the header line.")
3038 (defvar gdb-memory-format-menu
3039 (let ((map (make-sparse-keymap "Format")))
3041 (define-key map [binary]
3042 '(menu-item "Binary" gdb-memory-format-binary
3043 :button (:radio . (equal gdb-memory-format "t"))))
3044 (define-key map [octal]
3045 '(menu-item "Octal" gdb-memory-format-octal
3046 :button (:radio . (equal gdb-memory-format "o"))))
3047 (define-key map [unsigned]
3048 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3049 :button (:radio . (equal gdb-memory-format "u"))))
3050 (define-key map [signed]
3051 '(menu-item "Signed Decimal" gdb-memory-format-signed
3052 :button (:radio . (equal gdb-memory-format "d"))))
3053 (define-key map [hexadecimal]
3054 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3055 :button (:radio . (equal gdb-memory-format "x"))))
3057 "Menu of display formats in the header line.")
3059 (defun gdb-memory-format-menu (event)
3061 (x-popup-menu event gdb-memory-format-menu))
3063 (defun gdb-memory-format-menu-1 (event)
3065 (save-selected-window
3066 (select-window (posn-window (event-start event)))
3067 (let* ((selection (gdb-memory-format-menu event))
3068 (binding (and selection (lookup-key gdb-memory-format-menu
3069 (vector (car selection))))))
3070 (if binding (call-interactively binding)))))
3072 (defmacro def-gdb-memory-unit (name unit-size doc)
3073 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3075 DOC is an optional documentation string."
3076 `(defun ,name () ,(when doc doc)
3078 (customize-set-variable 'gdb-memory-unit ,unit-size)
3079 (gdb-invalidate-memory 'update)))
3081 (def-gdb-memory-unit gdb-memory-unit-giant 8
3082 "Set the unit size to giant words (eight bytes).")
3084 (def-gdb-memory-unit gdb-memory-unit-word 4
3085 "Set the unit size to words (four bytes).")
3087 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3088 "Set the unit size to halfwords (two bytes).")
3090 (def-gdb-memory-unit gdb-memory-unit-byte 1
3091 "Set the unit size to bytes.")
3093 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3094 "Define a function NAME which show new address in memory buffer.
3096 The defined function switches Memory buffer to show address
3097 stored in ADDRESS-VAR variable.
3099 DOC is an optional documentation string."
3103 (let ((gdb-memory-address ,address-var))
3104 (gdb-invalidate-memory))))
3106 (def-gdb-memory-show-page gdb-memory-show-previous-page
3107 gdb-memory-prev-page)
3109 (def-gdb-memory-show-page gdb-memory-show-next-page
3110 gdb-memory-next-page)
3112 (defvar gdb-memory-unit-map
3113 (let ((map (make-sparse-keymap)))
3114 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3116 "Keymap to select units in the header line.")
3118 (defvar gdb-memory-unit-menu
3119 (let ((map (make-sparse-keymap "Unit")))
3120 (define-key map [giantwords]
3121 '(menu-item "Giant words" gdb-memory-unit-giant
3122 :button (:radio . (equal gdb-memory-unit 8))))
3123 (define-key map [words]
3124 '(menu-item "Words" gdb-memory-unit-word
3125 :button (:radio . (equal gdb-memory-unit 4))))
3126 (define-key map [halfwords]
3127 '(menu-item "Halfwords" gdb-memory-unit-halfword
3128 :button (:radio . (equal gdb-memory-unit 2))))
3129 (define-key map [bytes]
3130 '(menu-item "Bytes" gdb-memory-unit-byte
3131 :button (:radio . (equal gdb-memory-unit 1))))
3133 "Menu of units in the header line.")
3135 (defun gdb-memory-unit-menu (event)
3137 (x-popup-menu event gdb-memory-unit-menu))
3139 (defun gdb-memory-unit-menu-1 (event)
3141 (save-selected-window
3142 (select-window (posn-window (event-start event)))
3143 (let* ((selection (gdb-memory-unit-menu event))
3144 (binding (and selection (lookup-key gdb-memory-unit-menu
3145 (vector (car selection))))))
3146 (if binding (call-interactively binding)))))
3148 (defvar gdb-memory-font-lock-keywords
3149 '(;; <__function.name+n>
3150 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3151 (1 font-lock-function-name-face)))
3152 "Font lock keywords used in `gdb-memory-mode'.")
3154 (defvar gdb-memory-header
3159 'face font-lock-warning-face
3160 'help-echo "mouse-1: decrement address"
3161 'mouse-face 'mode-line-highlight
3162 'local-map (gdb-make-header-line-mouse-map
3164 #'gdb-memory-show-previous-page))
3167 'face font-lock-warning-face
3168 'help-echo "mouse-1: increment address"
3169 'mouse-face 'mode-line-highlight
3170 'local-map (gdb-make-header-line-mouse-map
3172 #'gdb-memory-show-next-page))
3174 (propertize gdb-memory-address
3175 'face font-lock-warning-face
3176 'help-echo "mouse-1: set start address"
3177 'mouse-face 'mode-line-highlight
3178 'local-map (gdb-make-header-line-mouse-map
3180 #'gdb-memory-set-address-event))
3182 (propertize (number-to-string gdb-memory-rows)
3183 'face font-lock-warning-face
3184 'help-echo "mouse-1: set number of columns"
3185 'mouse-face 'mode-line-highlight
3186 'local-map (gdb-make-header-line-mouse-map
3188 #'gdb-memory-set-rows))
3190 (propertize (number-to-string gdb-memory-columns)
3191 'face font-lock-warning-face
3192 'help-echo "mouse-1: set number of columns"
3193 'mouse-face 'mode-line-highlight
3194 'local-map (gdb-make-header-line-mouse-map
3196 #'gdb-memory-set-columns))
3198 (propertize gdb-memory-format
3199 'face font-lock-warning-face
3200 'help-echo "mouse-3: select display format"
3201 'mouse-face 'mode-line-highlight
3202 'local-map gdb-memory-format-map)
3204 (propertize (number-to-string gdb-memory-unit)
3205 'face font-lock-warning-face
3206 'help-echo "mouse-3: select unit size"
3207 'mouse-face 'mode-line-highlight
3208 'local-map gdb-memory-unit-map)))
3209 "Header line used in `gdb-memory-mode'.")
3211 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3212 "Major mode for examining memory."
3213 (setq header-line-format gdb-memory-header)
3214 (set (make-local-variable 'font-lock-defaults)
3215 '(gdb-memory-font-lock-keywords))
3216 'gdb-invalidate-memory)
3218 (defun gdb-memory-buffer-name ()
3219 (concat "*memory of " (gdb-get-target-string) "*"))
3221 (def-gdb-display-buffer
3222 gdb-display-memory-buffer
3224 "Display memory contents.")
3226 (defun gdb-frame-memory-buffer ()
3227 "Display memory contents in a new frame."
3229 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3230 (special-display-frame-alist
3234 ,@gdb-frame-parameters)))
3235 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3238 ;;; Disassembly view
3240 (defun gdb-disassembly-buffer-name ()
3241 (gdb-current-context-buffer-name
3242 (concat "disassembly of " (gdb-get-target-string))))
3244 (def-gdb-display-buffer
3245 gdb-display-disassembly-buffer
3246 'gdb-disassembly-buffer
3247 "Display disassembly for current stack frame.")
3249 (def-gdb-preempt-display-buffer
3250 gdb-preemptively-display-disassembly-buffer
3251 'gdb-disassembly-buffer)
3253 (def-gdb-frame-for-buffer
3254 gdb-frame-disassembly-buffer
3255 'gdb-disassembly-buffer
3256 "Display disassembly in a new frame.")
3258 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3259 (let* ((frame (gdb-current-buffer-frame))
3260 (file (bindat-get-field frame 'fullname))
3261 (line (bindat-get-field frame 'line)))
3263 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3264 gdb-disassembly-handler
3265 ;; We update disassembly only after we have actual frame information
3266 ;; about all threads, so no there's `update' signal in this list
3267 '(start update-disassembly))
3269 (def-gdb-auto-update-handler
3270 gdb-disassembly-handler
3271 gdb-invalidate-disassembly
3272 gdb-disassembly-handler-custom
3275 (gdb-set-buffer-rules
3276 'gdb-disassembly-buffer
3277 'gdb-disassembly-buffer-name
3278 'gdb-disassembly-mode
3279 'gdb-invalidate-disassembly)
3281 (defvar gdb-disassembly-font-lock-keywords
3282 '(;; <__function.name+n>
3283 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3284 (1 font-lock-function-name-face))
3285 ;; 0xNNNNNNNN <__function.name+n>: opcode
3286 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3287 (4 font-lock-keyword-face))
3288 ;; %register(at least i386)
3289 ("%\\sw+" . font-lock-variable-name-face)
3290 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3291 (1 font-lock-comment-face)
3292 (2 font-lock-function-name-face))
3293 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3294 "Font lock keywords used in `gdb-disassembly-mode'.")
3296 (defvar gdb-disassembly-mode-map
3298 (let ((map (make-sparse-keymap)))
3299 (suppress-keymap map)
3300 (define-key map "q" 'kill-this-buffer)
3303 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3304 "Major mode for GDB disassembly information."
3305 ;; TODO Rename overlay variable for disassembly mode
3306 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3307 (setq fringes-outside-margins t)
3308 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3309 (set (make-local-variable 'font-lock-defaults)
3310 '(gdb-disassembly-font-lock-keywords))
3311 'gdb-invalidate-disassembly)
3313 (defun gdb-disassembly-handler-custom ()
3314 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3315 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3316 (table (make-gdb-table))
3318 (dolist (instr instructions)
3319 (gdb-table-add-row table
3321 (bindat-get-field instr 'address)
3322 (apply #'format "<%s+%s>:"
3323 (gdb-get-many-fields instr 'func-name 'offset))
3324 (bindat-get-field instr 'inst)))
3325 (when (string-equal (bindat-get-field instr 'address)
3328 (setq marked-line (length (gdb-table-rows table)))
3329 (setq fringe-indicator-alist
3330 (if (string-equal gdb-frame-number "0")
3332 '((overlay-arrow . hollow-right-triangle)))))))
3333 (insert (gdb-table-string table " "))
3334 (gdb-disassembly-place-breakpoints)
3335 ;; Mark current position with overlay arrow and scroll window to
3338 (let ((window (get-buffer-window (current-buffer) 0)))
3339 (set-window-point window (gdb-mark-line marked-line
3340 gdb-disassembly-position))))
3342 (gdb-current-context-mode-name
3343 (concat "Disassembly: "
3344 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3346 (defun gdb-disassembly-place-breakpoints ()
3347 (gdb-remove-breakpoint-icons (point-min) (point-max))
3348 (dolist (breakpoint gdb-breakpoints-list)
3349 (let* ((breakpoint (cdr breakpoint))
3350 (bptno (bindat-get-field breakpoint 'number))
3351 (flag (bindat-get-field breakpoint 'enabled))
3352 (address (bindat-get-field breakpoint 'addr)))
3354 (goto-char (point-min))
3355 (if (re-search-forward (concat "^" address) nil t)
3356 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3359 (defvar gdb-breakpoints-header
3361 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3364 (gdb-propertize-header "Threads" gdb-threads-buffer
3365 "mouse-1: select" mode-line-highlight
3366 mode-line-inactive)))
3368 ;;; Breakpoints view
3369 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3370 "Major mode for gdb breakpoints."
3371 (setq header-line-format gdb-breakpoints-header)
3372 'gdb-invalidate-breakpoints)
3374 (defun gdb-toggle-breakpoint ()
3375 "Enable/disable breakpoint at current line of breakpoints buffer."
3379 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3382 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3385 (bindat-get-field breakpoint 'number)))
3386 (error "Not recognized as break/watchpoint line")))))
3388 (defun gdb-delete-breakpoint ()
3389 "Delete the breakpoint at current line of breakpoints buffer."
3393 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3395 (gud-basic-call (concat "-break-delete "
3396 (bindat-get-field breakpoint 'number)))
3397 (error "Not recognized as break/watchpoint line")))))
3399 (defun gdb-goto-breakpoint (&optional event)
3400 "Go to the location of breakpoint at current line of
3401 breakpoints buffer."
3402 (interactive (list last-input-event))
3403 (if event (posn-set-point (event-end event)))
3404 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3405 (let ((window (get-buffer-window gud-comint-buffer)))
3406 (if window (save-selected-window (select-window window))))
3409 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3411 (let ((bptno (bindat-get-field breakpoint 'number))
3412 (file (bindat-get-field breakpoint 'fullname))
3413 (line (bindat-get-field breakpoint 'line)))
3414 (save-selected-window
3415 (let* ((buffer (find-file-noselect
3416 (if (file-exists-p file) file
3417 (cdr (assoc bptno gdb-location-alist)))))
3418 (window (or (gdb-display-source-buffer buffer)
3419 (display-buffer buffer))))
3420 (setq gdb-source-window window)
3421 (with-current-buffer buffer
3422 (goto-char (point-min))
3423 (forward-line (1- (string-to-number line)))
3424 (set-window-point window (point))))))
3425 (error "Not recognized as break/watchpoint line")))))
3428 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3430 (def-gdb-trigger-and-handler
3431 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3432 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3435 (gdb-set-buffer-rules
3437 'gdb-stack-buffer-name
3439 'gdb-invalidate-frames)
3441 (defun gdb-frame-location (frame)
3442 "Return \" of file:line\" or \" of library\" for structure FRAME.
3444 FRAME must have either \"file\" and \"line\" members or \"from\"
3446 (let ((file (bindat-get-field frame 'file))
3447 (line (bindat-get-field frame 'line))
3448 (from (bindat-get-field frame 'from)))
3449 (let ((res (or (and file line (concat file ":" line))
3451 (if res (concat " of " res) ""))))
3453 (defun gdb-stack-list-frames-custom ()
3454 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3455 (table (make-gdb-table)))
3456 (set-marker gdb-stack-position nil)
3457 (dolist (frame stack)
3458 (gdb-table-add-row table
3460 (bindat-get-field frame 'level)
3463 (bindat-get-field frame 'func)
3464 (if gdb-stack-buffer-locations
3465 (gdb-frame-location frame) "")
3466 (if gdb-stack-buffer-addresses
3467 (concat " at " (bindat-get-field frame 'addr)) "")))
3468 `(mouse-face highlight
3469 help-echo "mouse-2, RET: Select frame"
3471 (insert (gdb-table-string table " ")))
3472 (when (and gdb-frame-number
3473 (gdb-buffer-shows-main-thread-p))
3474 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3475 gdb-stack-position))
3477 (gdb-current-context-mode-name "Frames")))
3479 (defun gdb-stack-buffer-name ()
3480 (gdb-current-context-buffer-name
3481 (concat "stack frames of " (gdb-get-target-string))))
3483 (def-gdb-display-buffer
3484 gdb-display-stack-buffer
3486 "Display backtrace of current stack.")
3488 (def-gdb-preempt-display-buffer
3489 gdb-preemptively-display-stack-buffer
3490 'gdb-stack-buffer nil t)
3492 (def-gdb-frame-for-buffer
3493 gdb-frame-stack-buffer
3495 "Display backtrace of current stack in a new frame.")
3497 (defvar gdb-frames-mode-map
3498 (let ((map (make-sparse-keymap)))
3499 (suppress-keymap map)
3500 (define-key map "q" 'kill-this-buffer)
3501 (define-key map "\r" 'gdb-select-frame)
3502 (define-key map [mouse-2] 'gdb-select-frame)
3503 (define-key map [follow-link] 'mouse-face)
3506 (defvar gdb-frames-font-lock-keywords
3507 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3508 "Font lock keywords used in `gdb-frames-mode'.")
3510 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3511 "Major mode for gdb call stack."
3512 (setq gdb-stack-position (make-marker))
3513 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3514 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3515 (set (make-local-variable 'font-lock-defaults)
3516 '(gdb-frames-font-lock-keywords))
3517 'gdb-invalidate-frames)
3519 (defun gdb-select-frame (&optional event)
3520 "Select the frame and display the relevant source."
3521 (interactive (list last-input-event))
3522 (if event (posn-set-point (event-end event)))
3523 (let ((frame (get-text-property (point) 'gdb-frame)))
3525 (if (gdb-buffer-shows-main-thread-p)
3526 (let ((new-level (bindat-get-field frame 'level)))
3527 (setq gdb-frame-number new-level)
3528 (gdb-input (concat "-stack-select-frame " new-level)
3531 (error "Could not select frame for non-current thread"))
3532 (error "Not recognized as frame line"))))
3536 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3537 (def-gdb-trigger-and-handler
3538 gdb-invalidate-locals
3539 (concat (gdb-current-context-command "-stack-list-locals")
3541 gdb-locals-handler gdb-locals-handler-custom
3544 (gdb-set-buffer-rules
3546 'gdb-locals-buffer-name
3548 'gdb-invalidate-locals)
3550 (defvar gdb-locals-watch-map
3551 (let ((map (make-sparse-keymap)))
3552 (suppress-keymap map)
3553 (define-key map "\r" 'gud-watch)
3554 (define-key map [mouse-2] 'gud-watch)
3556 "Keymap to create watch expression of a complex data type local variable.")
3558 (defvar gdb-edit-locals-map-1
3559 (let ((map (make-sparse-keymap)))
3560 (suppress-keymap map)
3561 (define-key map "\r" 'gdb-edit-locals-value)
3562 (define-key map [mouse-2] 'gdb-edit-locals-value)
3564 "Keymap to edit value of a simple data type local variable.")
3566 (defun gdb-edit-locals-value (&optional event)
3567 "Assign a value to a variable displayed in the locals buffer."
3568 (interactive (list last-input-event))
3570 (if event (posn-set-point (event-end event)))
3572 (let* ((var (bindat-get-field
3573 (get-text-property (point) 'gdb-local-variable) 'name))
3574 (value (read-string (format "New value (%s): " var))))
3576 (concat "-gdb-set variable " var " = " value)))))
3578 ;; Don't display values of arrays or structures.
3579 ;; These can be expanded using gud-watch.
3580 (defun gdb-locals-handler-custom ()
3581 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3582 (table (make-gdb-table)))
3583 (dolist (local locals-list)
3584 (let ((name (bindat-get-field local 'name))
3585 (value (bindat-get-field local 'value))
3586 (type (bindat-get-field local 'type)))
3588 (string-match "\\0x" value))
3589 (add-text-properties 0 (length name)
3590 `(mouse-face highlight
3591 help-echo "mouse-2: create watch expression"
3592 local-map ,gdb-locals-watch-map)
3594 (add-text-properties 0 (length value)
3595 `(mouse-face highlight
3596 help-echo "mouse-2: edit value"
3597 local-map ,gdb-edit-locals-map-1)
3602 (propertize type 'font-lock-face font-lock-type-face)
3603 (propertize name 'font-lock-face font-lock-variable-name-face)
3605 `(gdb-local-variable ,local))))
3606 (insert (gdb-table-string table " "))
3608 (gdb-current-context-mode-name
3610 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3612 (defvar gdb-locals-header
3614 (gdb-propertize-header "Locals" gdb-locals-buffer
3617 (gdb-propertize-header "Registers" gdb-registers-buffer
3618 "mouse-1: select" mode-line-highlight
3619 mode-line-inactive)))
3621 (defvar gdb-locals-mode-map
3622 (let ((map (make-sparse-keymap)))
3623 (suppress-keymap map)
3624 (define-key map "q" 'kill-this-buffer)
3625 (define-key map "\t" (lambda ()
3627 (gdb-set-window-buffer
3628 (gdb-get-buffer-create
3629 'gdb-registers-buffer
3630 gdb-thread-number) t)))
3633 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3634 "Major mode for gdb locals."
3635 (setq header-line-format gdb-locals-header)
3636 'gdb-invalidate-locals)
3638 (defun gdb-locals-buffer-name ()
3639 (gdb-current-context-buffer-name
3640 (concat "locals of " (gdb-get-target-string))))
3642 (def-gdb-display-buffer
3643 gdb-display-locals-buffer
3645 "Display local variables of current stack and their values.")
3647 (def-gdb-preempt-display-buffer
3648 gdb-preemptively-display-locals-buffer
3649 'gdb-locals-buffer nil t)
3651 (def-gdb-frame-for-buffer
3652 gdb-frame-locals-buffer
3654 "Display local variables of current stack and their values in a new frame.")
3657 ;; Registers buffer.
3659 (def-gdb-trigger-and-handler
3660 gdb-invalidate-registers
3661 (concat (gdb-current-context-command "-data-list-register-values") " x")
3662 gdb-registers-handler
3663 gdb-registers-handler-custom
3666 (gdb-set-buffer-rules
3667 'gdb-registers-buffer
3668 'gdb-registers-buffer-name
3670 'gdb-invalidate-registers)
3672 (defun gdb-registers-handler-custom ()
3673 (when gdb-register-names
3674 (let ((register-values
3675 (bindat-get-field (gdb-json-partial-output) 'register-values))
3676 (table (make-gdb-table)))
3677 (dolist (register register-values)
3678 (let* ((register-number (bindat-get-field register 'number))
3679 (value (bindat-get-field register 'value))
3680 (register-name (nth (string-to-number register-number)
3681 gdb-register-names)))
3685 (propertize register-name
3686 'font-lock-face font-lock-variable-name-face)
3687 (if (member register-number gdb-changed-registers)
3688 (propertize value 'font-lock-face font-lock-warning-face)
3690 `(mouse-face highlight
3691 help-echo "mouse-2: edit value"
3692 gdb-register-name ,register-name))))
3693 (insert (gdb-table-string table " ")))
3695 (gdb-current-context-mode-name "Registers"))))
3697 (defun gdb-edit-register-value (&optional event)
3698 "Assign a value to a register displayed in the registers buffer."
3699 (interactive (list last-input-event))
3701 (if event (posn-set-point (event-end event)))
3703 (let* ((var (bindat-get-field
3704 (get-text-property (point) 'gdb-register-name)))
3705 (value (read-string (format "New value (%s): " var))))
3707 (concat "-gdb-set variable $" var " = " value)))))
3709 (defvar gdb-registers-mode-map
3710 (let ((map (make-sparse-keymap)))
3711 (suppress-keymap map)
3712 (define-key map "\r" 'gdb-edit-register-value)
3713 (define-key map [mouse-2] 'gdb-edit-register-value)
3714 (define-key map "q" 'kill-this-buffer)
3715 (define-key map "\t" (lambda ()
3717 (gdb-set-window-buffer
3718 (gdb-get-buffer-create
3720 gdb-thread-number) t)))
3723 (defvar gdb-registers-header
3725 (gdb-propertize-header "Locals" gdb-locals-buffer
3726 "mouse-1: select" mode-line-highlight
3729 (gdb-propertize-header "Registers" gdb-registers-buffer
3730 nil nil mode-line)))
3732 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3733 "Major mode for gdb registers."
3734 (setq header-line-format gdb-registers-header)
3735 'gdb-invalidate-registers)
3737 (defun gdb-registers-buffer-name ()
3738 (gdb-current-context-buffer-name
3739 (concat "registers of " (gdb-get-target-string))))
3741 (def-gdb-display-buffer
3742 gdb-display-registers-buffer
3743 'gdb-registers-buffer
3744 "Display integer register contents.")
3746 (def-gdb-preempt-display-buffer
3747 gdb-preemptively-display-registers-buffer
3748 'gdb-registers-buffer nil t)
3750 (def-gdb-frame-for-buffer
3751 gdb-frame-registers-buffer
3752 'gdb-registers-buffer
3753 "Display integer register contents in a new frame.")
3755 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3756 (defun gdb-get-changed-registers ()
3757 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3758 (not (gdb-pending-p 'gdb-get-changed-registers)))
3759 (gdb-input "-data-list-changed-registers"
3760 'gdb-changed-registers-handler)
3761 (gdb-add-pending 'gdb-get-changed-registers)))
3763 (defun gdb-changed-registers-handler ()
3764 (gdb-delete-pending 'gdb-get-changed-registers)
3765 (setq gdb-changed-registers nil)
3766 (dolist (register-number
3767 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3768 (push register-number gdb-changed-registers)))
3770 (defun gdb-register-names-handler ()
3771 ;; Don't use gdb-pending-triggers because this handler is called
3772 ;; only once (in gdb-init-1)
3773 (setq gdb-register-names nil)
3774 (dolist (register-name
3775 (bindat-get-field (gdb-json-partial-output) 'register-names))
3776 (push register-name gdb-register-names))
3777 (setq gdb-register-names (reverse gdb-register-names)))
3780 (defun gdb-get-source-file-list ()
3781 "Create list of source files for current GDB session.
3782 If buffers already exist for any of these files, gud-minor-mode
3784 (goto-char (point-min))
3785 (while (re-search-forward gdb-source-file-regexp nil t)
3786 (push (match-string 1) gdb-source-file-list))
3787 (dolist (buffer (buffer-list))
3788 (with-current-buffer buffer
3789 (when (member buffer-file-name gdb-source-file-list)
3790 (gdb-init-buffer)))))
3792 (defun gdb-get-main-selected-frame ()
3793 "Trigger for `gdb-frame-handler' which uses main current
3794 thread. Called from `gdb-update'."
3795 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3797 (gdb-input (gdb-current-context-command "-stack-info-frame")
3799 (gdb-add-pending 'gdb-get-main-selected-frame))))
3801 (defun gdb-frame-handler ()
3802 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3803 overlay arrow in source buffer."
3804 (gdb-delete-pending 'gdb-get-main-selected-frame)
3805 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3807 (setq gdb-selected-frame (bindat-get-field frame 'func))
3808 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3809 (setq gdb-frame-number (bindat-get-field frame 'level))
3810 (setq gdb-frame-address (bindat-get-field frame 'addr))
3811 (let ((line (bindat-get-field frame 'line)))
3812 (setq gdb-selected-line (and line (string-to-number line)))
3813 (when (and gdb-selected-file gdb-selected-line)
3814 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3815 (gud-display-frame)))
3816 (if gud-overlay-arrow-position
3817 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3818 (position (marker-position gud-overlay-arrow-position)))
3820 (with-current-buffer buffer
3821 (setq fringe-indicator-alist
3822 (if (string-equal gdb-frame-number "0")
3824 '((overlay-arrow . hollow-right-triangle))))
3825 (setq gud-overlay-arrow-position (make-marker))
3826 (set-marker gud-overlay-arrow-position position))))))))
3828 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3830 (defun gdb-get-prompt ()
3831 "Find prompt for GDB session."
3832 (goto-char (point-min))
3833 (setq gdb-prompt-name nil)
3834 (re-search-forward gdb-prompt-name-regexp nil t)
3835 (setq gdb-prompt-name (match-string 1))
3836 ;; Insert first prompt.
3837 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3839 ;;;; Window management
3840 (defun gdb-display-buffer (buf dedicated &optional frame)
3843 If BUF is already displayed in some window, show it, deiconifying
3844 the frame if necessary. Otherwise, find least recently used
3845 window and show BUF there, if the window is not used for GDB
3846 already, in which case that window is split first."
3847 (let ((answer (get-buffer-window buf (or frame 0))))
3849 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3850 (let ((window (get-lru-window)))
3851 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3853 (let ((largest (get-largest-window)))
3854 (setq answer (split-window largest))
3855 (set-window-buffer answer buf)
3856 (set-window-dedicated-p answer dedicated)
3858 (set-window-buffer window buf)
3861 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3862 "Find window displaying a buffer with the same
3863 `gdb-buffer-type' as BUF and show BUF there. If no such window
3864 exists, just call `gdb-display-buffer' for BUF. If the window
3865 found is already dedicated, split window according to
3866 SPLIT-HORIZONTAL and show BUF in the new window."
3868 (when (not (get-buffer-window buf))
3869 (let* ((buf-type (gdb-buffer-type buf))
3871 (get-window-with-predicate
3874 (gdb-buffer-type (window-buffer w)))
3875 (not (window-dedicated-p w)))))))
3877 (set-window-buffer existing-window buf)
3878 (let ((dedicated-window
3879 (get-window-with-predicate
3882 (gdb-buffer-type (window-buffer w)))))))
3883 (if dedicated-window
3885 (split-window dedicated-window nil split-horizontal) buf)
3886 (gdb-display-buffer buf t))))))
3887 (error "Null buffer")))
3889 ;;; Shared keymap initialization:
3891 (let ((menu (make-sparse-keymap "GDB-Windows")))
3892 (define-key gud-menu-map [displays]
3893 `(menu-item "GDB-Windows" ,menu
3894 :visible (eq gud-minor-mode 'gdbmi)))
3895 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3896 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3897 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3898 (define-key menu [disassembly]
3899 '("Disassembly" . gdb-display-disassembly-buffer))
3900 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3901 (define-key menu [inferior]
3902 '("IO" . gdb-display-io-buffer))
3903 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3904 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3905 (define-key menu [breakpoints]
3906 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3908 (let ((menu (make-sparse-keymap "GDB-Frames")))
3909 (define-key gud-menu-map [frames]
3910 `(menu-item "GDB-Frames" ,menu
3911 :visible (eq gud-minor-mode 'gdbmi)))
3912 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3913 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3914 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3915 (define-key menu [disassembly]
3916 '("Disassembly" . gdb-frame-disassembly-buffer))
3917 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3918 (define-key menu [inferior]
3919 '("IO" . gdb-frame-io-buffer))
3920 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3921 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3922 (define-key menu [breakpoints]
3923 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3925 (let ((menu (make-sparse-keymap "GDB-MI")))
3926 (define-key menu [gdb-customize]
3927 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3928 :help "Customize Gdb Graphical Mode options."))
3929 (define-key menu [gdb-many-windows]
3930 '(menu-item "Display Other Windows" gdb-many-windows
3931 :help "Toggle display of locals, stack and breakpoint information"
3932 :button (:toggle . gdb-many-windows)))
3933 (define-key menu [gdb-restore-windows]
3934 '(menu-item "Restore Window Layout" gdb-restore-windows
3935 :help "Restore standard layout for debug session."))
3936 (define-key menu [sep1]
3938 (define-key menu [all-threads]
3939 '(menu-item "GUD controls all threads"
3942 (setq gdb-gud-control-all-threads t))
3943 :help "GUD start/stop commands apply to all threads"
3944 :button (:radio . gdb-gud-control-all-threads)))
3945 (define-key menu [current-thread]
3946 '(menu-item "GUD controls current thread"
3949 (setq gdb-gud-control-all-threads nil))
3950 :help "GUD start/stop commands apply to current thread only"
3951 :button (:radio . (not gdb-gud-control-all-threads))))
3952 (define-key menu [sep2]
3954 (define-key menu [gdb-customize-reasons]
3955 '(menu-item "Customize switching..."
3958 (customize-option 'gdb-switch-reasons))))
3959 (define-key menu [gdb-switch-when-another-stopped]
3960 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
3961 gdb-switch-when-another-stopped
3962 "Automatically switch to stopped thread"
3963 "GDB thread switching %s"
3964 "Switch to stopped thread"))
3965 (define-key gud-menu-map [mi]
3966 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3968 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3969 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3970 ;; tool-bar-local-item-from-menu here because it appends new buttons
3971 ;; to toolbar from right to left while we want our A/T throttle to
3972 ;; show up right before Run button.
3973 (define-key-after gud-tool-bar-map [all-threads]
3974 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3975 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3976 :visible (and (eq gud-minor-mode 'gdbmi)
3978 (not gdb-gud-control-all-threads)))
3981 (define-key-after gud-tool-bar-map [current-thread]
3982 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3983 :image (find-image '((:type xpm :file "gud/all.xpm")))
3984 :visible (and (eq gud-minor-mode 'gdbmi)
3986 gdb-gud-control-all-threads))
3989 (defun gdb-frame-gdb-buffer ()
3990 "Display GUD buffer in a new frame."
3992 (display-buffer-other-frame gud-comint-buffer))
3994 (defun gdb-display-gdb-buffer ()
3995 "Display GUD buffer."
3997 (pop-to-buffer gud-comint-buffer nil 0))
3999 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4000 "Set buffer of selected window to NAME and dedicate window.
4002 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4003 window is dedicated."
4004 (unless window (setq window (selected-window)))
4005 (when ignore-dedicated
4006 (set-window-dedicated-p window nil))
4007 (set-window-buffer window (get-buffer name))
4008 (set-window-dedicated-p window t))
4010 (defun gdb-setup-windows ()
4011 "Layout the window pattern for `gdb-many-windows'."
4012 (gdb-display-locals-buffer)
4013 (gdb-display-stack-buffer)
4014 (delete-other-windows)
4015 (gdb-display-breakpoints-buffer)
4016 (delete-other-windows)
4018 (switch-to-buffer gud-comint-buffer)
4019 (let ((win0 (selected-window))
4020 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4021 (win2 (split-window nil ( / (window-height) 3)))
4022 (win3 (split-window-right)))
4023 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4024 (select-window win2)
4027 (if gud-last-last-frame
4028 (gud-find-file (car gud-last-last-frame))
4030 (gud-find-file gdb-main-file)
4031 ;; Put buffer list in window if we
4032 ;; can't find a source file.
4033 (list-buffers-noselect))))
4034 (setq gdb-source-window (selected-window))
4035 (let ((win4 (split-window-right)))
4036 (gdb-set-window-buffer
4037 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4038 (select-window win1)
4039 (gdb-set-window-buffer (gdb-stack-buffer-name))
4040 (let ((win5 (split-window-right)))
4041 (gdb-set-window-buffer (if gdb-show-threads-by-default
4042 (gdb-threads-buffer-name)
4043 (gdb-breakpoints-buffer-name))
4045 (select-window win0)))
4047 (defcustom gdb-many-windows nil
4048 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4049 In this case it starts with two windows: one displaying the GUD
4050 buffer and the other with the source file with the main routine
4051 of the debugged program. Non-nil means display the layout shown for
4057 (defun gdb-many-windows (arg)
4058 "Toggle the number of windows in the basic arrangement.
4059 With arg, display additional buffers iff arg is positive."
4061 (setq gdb-many-windows
4063 (not gdb-many-windows)
4064 (> (prefix-numeric-value arg) 0)))
4065 (message (format "Display of other windows %sabled"
4066 (if gdb-many-windows "en" "dis")))
4067 (if (and gud-comint-buffer
4068 (buffer-name gud-comint-buffer))
4070 (gdb-restore-windows)
4073 (defun gdb-restore-windows ()
4074 "Restore the basic arrangement of windows used by gdb.
4075 This arrangement depends on the value of `gdb-many-windows'."
4077 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4078 (delete-other-windows)
4079 (if gdb-many-windows
4081 (when (or gud-last-last-frame gdb-show-main)
4082 (let ((win (split-window)))
4085 (if gud-last-last-frame
4086 (gud-find-file (car gud-last-last-frame))
4087 (gud-find-file gdb-main-file)))
4088 (setq gdb-source-window win)))))
4091 "Exit a debugging session cleanly.
4092 Kills the gdb buffers, and resets variables and the source buffers."
4093 (dolist (buffer (buffer-list))
4094 (unless (eq buffer gud-comint-buffer)
4095 (with-current-buffer buffer
4096 (if (eq gud-minor-mode 'gdbmi)
4097 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4099 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4100 (setq gud-minor-mode nil)
4101 (kill-local-variable 'tool-bar-map)
4102 (kill-local-variable 'gdb-define-alist))))))
4103 (setq gdb-disassembly-position nil)
4104 (setq overlay-arrow-variable-list
4105 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4106 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4107 (setq gdb-stack-position nil)
4108 (setq overlay-arrow-variable-list
4109 (delq 'gdb-stack-position overlay-arrow-variable-list))
4110 (setq gdb-thread-position nil)
4111 (setq overlay-arrow-variable-list
4112 (delq 'gdb-thread-position overlay-arrow-variable-list))
4113 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4114 (setq gud-running nil)
4115 (setq gdb-active-process nil)
4116 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4118 (defun gdb-get-source-file ()
4119 "Find the source file where the program starts and display it with related
4120 buffers, if required."
4121 (goto-char (point-min))
4122 (if (re-search-forward gdb-source-file-regexp nil t)
4123 (setq gdb-main-file (match-string 1)))
4124 (if gdb-many-windows
4126 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4127 (if (and gdb-show-main gdb-main-file)
4128 (let ((pop-up-windows t))
4129 (display-buffer (gud-find-file gdb-main-file)))))
4130 (gdb-force-mode-line-update
4131 (propertize "ready" 'face font-lock-variable-name-face)))
4134 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4135 "Put string PUTSTRING in front of POS in the current buffer.
4136 PUTSTRING is displayed by putting an overlay into the current buffer with a
4137 `before-string' string that has a `display' property whose value is
4139 (let ((string (make-string 1 ?x))
4140 (buffer (current-buffer)))
4141 (setq putstring (copy-sequence putstring))
4142 (let ((overlay (make-overlay pos pos buffer))
4144 (list (list 'margin 'left-margin) putstring))))
4145 (put-text-property 0 1 'display prop string)
4147 (add-text-properties 0 1 sprops string))
4148 (overlay-put overlay 'put-break t)
4149 (overlay-put overlay 'before-string string))))
4151 ;;from remove-images
4152 (defun gdb-remove-strings (start end &optional buffer)
4153 "Remove strings between START and END in BUFFER.
4154 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4155 BUFFER nil or omitted means use the current buffer."
4157 (setq buffer (current-buffer)))
4158 (dolist (overlay (overlays-in start end))
4159 (when (overlay-get overlay 'put-break)
4160 (delete-overlay overlay))))
4162 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4163 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4164 (start (- (car posns) 1))
4165 (end (+ (cdr posns) 1))
4166 (putstring (if enabled "B" "b"))
4167 (source-window (get-buffer-window (current-buffer) 0)))
4168 (add-text-properties
4169 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4172 (add-text-properties
4173 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4174 (add-text-properties
4175 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4176 (gdb-remove-breakpoint-icons start end)
4177 (if (display-images-p)
4178 (if (>= (or left-fringe-width
4179 (if source-window (car (window-fringes source-window)))
4180 gdb-buffer-fringe-width) 8)
4183 `(left-fringe breakpoint
4186 'breakpoint-disabled))
4188 'gdb-enabled enabled)
4189 (when (< left-margin-width 2)
4190 (save-current-buffer
4191 (setq left-margin-width 2)
4195 left-margin-width right-margin-width))))
4198 (or breakpoint-enabled-icon
4199 (setq breakpoint-enabled-icon
4200 (find-image `((:type xpm :data
4201 ,breakpoint-xpm-data
4202 :ascent 100 :pointer hand)
4204 ,breakpoint-enabled-pbm-data
4205 :ascent 100 :pointer hand)))))
4206 (or breakpoint-disabled-icon
4207 (setq breakpoint-disabled-icon
4208 (find-image `((:type xpm :data
4209 ,breakpoint-xpm-data
4210 :conversion disabled
4211 :ascent 100 :pointer hand)
4213 ,breakpoint-disabled-pbm-data
4214 :ascent 100 :pointer hand))))))
4218 (when (< left-margin-width 2)
4219 (save-current-buffer
4220 (setq left-margin-width 2)
4221 (let ((window (get-buffer-window (current-buffer) 0)))
4224 window left-margin-width right-margin-width)))))
4226 (propertize putstring
4228 'breakpoint-enabled 'breakpoint-disabled))
4231 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4232 (gdb-remove-strings start end)
4233 (if (display-images-p)
4234 (remove-images start end))
4236 (setq left-margin-width 0)
4237 (let ((window (get-buffer-window (current-buffer) 0)))
4240 window left-margin-width right-margin-width)))))
4243 ;;; Functions for inline completion.
4245 (defvar gud-gdb-fetch-lines-in-progress)
4246 (defvar gud-gdb-fetch-lines-string)
4247 (defvar gud-gdb-fetch-lines-break)
4248 (defvar gud-gdb-fetched-lines)
4250 (defun gud-gdbmi-completions (context command)
4251 "Completion table for GDB/MI commands.
4252 COMMAND is the prefix for which we seek completion.
4253 CONTEXT is the text before COMMAND on the line."
4254 (let ((gud-gdb-fetch-lines-in-progress t)
4255 (gud-gdb-fetch-lines-string nil)
4256 (gud-gdb-fetch-lines-break (length context))
4257 (gud-gdb-fetched-lines nil)
4258 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4259 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4261 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4262 (gdb-input (concat "complete " context command)
4263 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4264 (while gud-gdb-fetch-lines-in-progress
4265 (accept-process-output (get-buffer-process gud-comint-buffer))))
4266 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4268 (defun gud-gdbmi-fetch-lines-filter (string)
4269 "Custom filter function for `gud-gdbmi-completions'."
4270 (setq string (concat gud-gdb-fetch-lines-string
4271 (gud-gdbmi-marker-filter string)))
4272 (while (string-match "\n" string)
4273 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4274 gud-gdb-fetched-lines)
4275 (setq string (substring string (match-end 0))))
4280 ;;; gdb-mi.el ends here