1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007-2012 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 was further 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 replaces 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-functions nil
379 "List of functions called whenever GDB stops.
381 Each function takes one argument, a parsed MI response, which
382 contains fields of corresponding MI *stopped async record:
384 ((stopped-threads . \"all\")
386 (frame (line . \"38\")
387 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
388 (file . \"server.c\")
389 (args ((value . \"0x804b038\")
392 (addr . \"0x0804869e\"))
393 (reason . \"end-stepping-range\"))
395 Note that \"reason\" is only present in non-stop debugging mode.
397 `bindat-get-field' may be used to access the fields of response.
399 Each function is called after the new current thread was selected
400 and GDB buffers were updated in `gdb-stopped'."
401 :type '(repeat function)
404 :link '(info-link "(gdb)GDB/MI Async Records"))
406 (defcustom gdb-switch-when-another-stopped t
407 "When nil, Emacs won't switch to stopped thread if some other
408 stopped thread is already selected."
413 (defcustom gdb-stack-buffer-locations t
414 "Show file information or library names in stack buffers."
419 (defcustom gdb-stack-buffer-addresses nil
420 "Show frame addresses in stack buffers."
425 (defcustom gdb-thread-buffer-verbose-names t
426 "Show long thread names in threads buffer."
431 (defcustom gdb-thread-buffer-arguments t
432 "Show function arguments in threads buffer."
437 (defcustom gdb-thread-buffer-locations t
438 "Show file information or library names in threads buffer."
443 (defcustom gdb-thread-buffer-addresses nil
444 "Show addresses for thread frames in threads buffer."
449 (defcustom gdb-show-threads-by-default nil
450 "Show threads list buffer instead of breakpoints list by
456 (defvar gdb-debug-log nil
457 "List of commands sent to and replies received from GDB.
458 Most recent commands are listed first. This list stores only the last
459 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
462 (defcustom gdb-enable-debug nil
463 "Non-nil means record the process input and output in `gdb-debug-log'."
468 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
469 "Shell command for generating a list of defined macros in a source file.
470 This list is used to display the #define directive associated
471 with an identifier as a tooltip. It works in a debug session with
472 GDB, when `gud-tooltip-mode' is t.
474 Set `gdb-cpp-define-alist-flags' for any include paths or
480 (defcustom gdb-cpp-define-alist-flags ""
481 "Preprocessor flags for `gdb-cpp-define-alist-program'."
486 (defcustom gdb-create-source-file-list t
487 "Non-nil means create a list of files from which the executable was built.
488 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
489 line for a long time when starting, possibly because your executable was
490 built from a large number of files. This allows quicker initialization
491 but means that these files are not automatically enabled for debugging,
492 e.g., you won't be able to click in the fringe to set a breakpoint until
493 execution has already stopped there."
498 (defcustom gdb-show-main nil
499 "Non-nil means display source file containing the main routine at startup.
500 Also display the main routine in the disassembly buffer if present."
505 (defun gdb-force-mode-line-update (status)
506 (let ((buffer gud-comint-buffer))
507 (if (and buffer (buffer-name buffer))
508 (with-current-buffer buffer
509 (setq mode-line-process
511 (process-status (get-buffer-process buffer)) status))
512 ;; Force mode line redisplay soon.
513 (force-mode-line-update)))))
515 (defun gdb-enable-debug (arg)
516 "Toggle logging of transaction between Emacs and Gdb.
517 The log is stored in `gdb-debug-log' as an alist with elements
518 whose cons is send, send-item or recv and whose cdr is the string
519 being transferred. This list may grow up to a size of
520 `gdb-debug-log-max' after which the oldest element (at the end of
521 the list) is deleted every time a new one is added (at the front)."
523 (setq gdb-enable-debug
525 (not gdb-enable-debug)
526 (> (prefix-numeric-value arg) 0)))
527 (message (format "Logging of transaction %sabled"
528 (if gdb-enable-debug "en" "dis"))))
530 ;; These two are used for menu and toolbar
531 (defun gdb-control-all-threads ()
532 "Switch to non-stop/A mode."
534 (setq gdb-gud-control-all-threads t)
535 ;; Actually forcing the tool-bar to update.
536 (force-mode-line-update)
537 (message "Now in non-stop/A mode."))
539 (defun gdb-control-current-thread ()
540 "Switch to non-stop/T mode."
542 (setq gdb-gud-control-all-threads nil)
543 ;; Actually forcing the tool-bar to update.
544 (force-mode-line-update)
545 (message "Now in non-stop/T mode."))
547 (defun gdb-find-watch-expression ()
548 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
549 (varnum (car var)) expr)
550 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
551 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
552 (component-list (split-string (match-string 2 varnum) "\\." t)))
553 (setq expr (nth 1 var1))
554 (setq varnumlet (car var1))
555 (dolist (component component-list)
556 (setq var2 (assoc varnumlet gdb-var-list))
557 (setq expr (concat expr
558 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
559 (concat "[" component "]")
560 (concat "." component))))
561 (setq varnumlet (concat varnumlet "." component)))
564 ;; noall is used for commands which don't take --all, but only
566 (defun gdb-gud-context-command (command &optional noall)
567 "When `gdb-non-stop' is t, add --thread option to COMMAND if
568 `gdb-gud-control-all-threads' is nil and --all option otherwise.
569 If NOALL is t, always add --thread option no matter what
570 `gdb-gud-control-all-threads' value is.
572 When `gdb-non-stop' is nil, return COMMAND unchanged."
574 (if (and gdb-gud-control-all-threads
576 gdb-supports-non-stop)
577 (concat command " --all ")
578 (gdb-current-context-command command))
581 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
582 "`gud-call' wrapper which adds --thread/--all options between
583 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
585 NOARG must be t when this macro is used outside `gud-def'"
587 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
588 ,(when (not noarg) 'arg)))
590 (defun gdb--check-interpreter (proc string)
591 (unless (zerop (length string))
592 (let ((filter (process-get proc 'gud-normal-filter)))
593 (set-process-filter proc filter)
594 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
595 ;; Apparently we're not running with -i=mi.
596 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
598 (setq string (concat (propertize msg 'font-lock-face 'error)
600 ;; Use the old gud-gbd filter, not because it works, but because it
601 ;; will properly display GDB's answers rather than hanging waiting for
602 ;; answers that aren't coming.
603 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
604 (funcall filter proc string))))
607 (defun gdb (command-line)
608 "Run gdb on program FILE in buffer *gud-FILE*.
609 The directory containing FILE becomes the initial working directory
610 and source-file directory for your debugger.
612 COMMAND-LINE is the shell command for starting the gdb session.
613 It should be a string consisting of the name of the gdb
614 executable followed by command-line options. The command-line
615 options should include \"-i=mi\" to use gdb's MI text interface.
616 Note that the old \"--annotate\" option is no longer supported.
618 If `gdb-many-windows' is nil (the default value) then gdb just
619 pops up the GUD buffer unless `gdb-show-main' is t. In this case
620 it starts with two windows: one displaying the GUD buffer and the
621 other with the source file with the main routine of the inferior.
623 If `gdb-many-windows' is t, regardless of the value of
624 `gdb-show-main', the layout below will appear. Keybindings are
625 shown in some of the buffers.
627 Watch expressions appear in the speedbar/slowbar.
629 The following commands help control operation :
631 `gdb-many-windows' - Toggle the number of windows gdb uses.
632 `gdb-restore-windows' - To restore the window layout.
634 See Info node `(emacs)GDB Graphical Interface' for a more
635 detailed description of this mode.
638 +----------------------------------------------------------------------+
640 +-----------------------------------+----------------------------------+
641 | GUD buffer (I/O of GDB) | Locals buffer |
645 +-----------------------------------+----------------------------------+
646 | Source buffer | I/O buffer (of debugged program) |
654 +-----------------------------------+----------------------------------+
655 | Stack buffer | Breakpoints buffer |
656 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
657 | | RET gdb-goto-breakpoint |
658 | | D gdb-delete-breakpoint |
659 +-----------------------------------+----------------------------------+"
661 (interactive (list (gud-query-cmdline 'gdb)))
663 (when (and gud-comint-buffer
664 (buffer-name gud-comint-buffer)
665 (get-buffer-process gud-comint-buffer)
666 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
667 (gdb-restore-windows)
669 "Multiple debugging requires restarting in text command mode"))
671 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
673 ;; Setup a temporary process filter to warn when GDB was not started
675 (let ((proc (get-buffer-process gud-comint-buffer)))
676 (process-put proc 'gud-normal-filter (process-filter proc))
677 (set-process-filter proc #'gdb--check-interpreter))
679 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
680 (setq comint-input-sender 'gdb-send)
681 (when (ring-empty-p comint-input-ring) ; cf shell-mode
682 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
683 (if (eq system-type 'ms-dos)
686 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
687 (hsize (getenv "HISTSIZE")))
688 (dolist (file (append '("~/.gdbinit")
689 (unless (string-equal (expand-file-name ".")
690 (expand-file-name "~"))
692 (if (file-readable-p (setq file (expand-file-name file)))
694 (insert-file-contents file)
695 ;; TODO? check for "set history save\\( *on\\)?" and do
696 ;; not use history otherwise?
697 (while (re-search-forward
698 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
699 (cond ((string-equal (match-string 1) "filename")
700 (setq hfile (expand-file-name
702 (file-name-directory file))))
703 ((string-equal (match-string 1) "size")
704 (setq hsize (match-string 2))))))))
706 (integerp (setq hsize (string-to-number hsize)))
708 (set (make-local-variable 'comint-input-ring-size) hsize))
710 (set (make-local-variable 'comint-input-ring-file-name) hfile))
711 (comint-read-input-ring t)))
712 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
713 "Set temporary breakpoint at current line.")
715 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
716 "\C-j" "Set execution address to current line.")
718 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
719 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
720 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
721 (gud-def gud-pstar "print* %e" nil
722 "Evaluate C dereferenced pointer expression at point.")
724 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
726 "Step one source line with display.")
727 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
729 "Step one instruction with display.")
730 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
732 "Step one line (skip functions).")
733 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
735 "Step one instruction (skip functions).")
736 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
738 "Continue with display.")
739 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
741 "Finish executing current function.")
742 (gud-def gud-run "-exec-run"
746 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
747 (gud-call "break %f:%l" arg)
751 (gud-call "break *%a" arg)))
752 "\C-b" "Set breakpoint at current line or address.")
754 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
755 (gud-call "clear %f:%l" arg)
759 (gud-call "clear *%a" arg)))
760 "\C-d" "Remove breakpoint at current line or address.")
762 ;; -exec-until doesn't support --all yet
763 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
764 (gud-call "-exec-until %f:%l" arg)
768 (gud-call "-exec-until *%a" arg)))
769 "\C-u" "Continue to current line or address.")
770 ;; TODO Why arg here?
772 gud-go (gud-call (if gdb-active-process
773 (gdb-gud-context-command "-exec-continue")
775 nil "Start or continue execution.")
777 ;; For debugging Emacs only.
781 "pp " (if (eq (buffer-local-value
782 'major-mode (window-buffer)) 'speedbar-mode)
783 (gdb-find-watch-expression) "%e")) arg)
784 nil "Print the Emacs s-expression.")
786 (define-key gud-minor-mode-map [left-margin mouse-1]
787 'gdb-mouse-set-clear-breakpoint)
788 (define-key gud-minor-mode-map [left-fringe mouse-1]
789 'gdb-mouse-set-clear-breakpoint)
790 (define-key gud-minor-mode-map [left-margin C-mouse-1]
791 'gdb-mouse-toggle-breakpoint-margin)
792 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
793 'gdb-mouse-toggle-breakpoint-fringe)
795 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
797 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
799 (define-key gud-minor-mode-map [left-margin mouse-3]
801 (define-key gud-minor-mode-map [left-fringe mouse-3]
804 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
806 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
808 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
810 (define-key gud-minor-mode-map [left-margin C-mouse-3]
813 (set (make-local-variable 'gud-gdb-completion-function)
814 'gud-gdbmi-completions)
816 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
818 (local-set-key "\C-i" 'completion-at-point)
820 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
822 (setq gdb-first-prompt t)
823 (setq gud-running nil)
827 (run-hooks 'gdb-mode-hook))
831 (setq gdb-selected-frame nil
833 gdb-thread-number nil
835 gdb-pending-triggers nil
836 gdb-output-sink 'user
837 gdb-location-alist nil
838 gdb-source-file-list nil
841 gdb-handler-alist '()
842 gdb-handler-number nil
844 gdb-first-done-or-error t
845 gdb-buffer-fringe-width (car (window-fringes))
847 gdb-source-window nil
848 gdb-inferior-status nil
850 gdb-buf-publisher '()
852 gdb-breakpoints-list '()
853 gdb-register-names '()
854 gdb-non-stop gdb-non-stop-setting)
856 (setq gdb-buffer-type 'gdbmi)
858 (gdb-force-mode-line-update
859 (propertize "initializing..." 'face font-lock-variable-name-face))
861 (gdb-get-buffer-create 'gdb-inferior-io)
862 (gdb-clear-inferior-io)
863 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
865 (if (eq window-system 'w32)
866 (gdb-input "-gdb-set new-console off" 'ignore))
867 (gdb-input "-gdb-set height 0" 'ignore)
870 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
872 (gdb-input "-enable-pretty-printing" 'ignore)
874 ;; find source file and compilation directory here
875 (if gdb-create-source-file-list
876 ;; Needs GDB 6.2 onwards.
877 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
878 ;; Needs GDB 6.0 onwards.
879 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
880 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
882 (defun gdb-non-stop-handler ()
883 (goto-char (point-min))
884 (if (re-search-forward "No symbol" nil t)
887 "This version of GDB doesn't support non-stop mode. Turning it off.")
888 (setq gdb-non-stop nil)
889 (setq gdb-supports-non-stop nil))
890 (setq gdb-supports-non-stop t)
891 (gdb-input "-gdb-set target-async 1" 'ignore)
892 (gdb-input "-list-target-features" 'gdb-check-target-async)))
894 (defun gdb-check-target-async ()
895 (goto-char (point-min))
896 (unless (re-search-forward "async" nil t)
898 "Target doesn't support non-stop mode. Turning it off.")
899 (setq gdb-non-stop nil)
900 (gdb-input "-gdb-set non-stop 0" 'ignore)))
902 (defun gdb-delchar-or-quit (arg)
903 "Delete ARG characters or send a quit command to GDB.
904 Send a quit only if point is at the end of the buffer, there is
905 no input, and GDB is waiting for input."
907 (unless (and (eq (current-buffer) gud-comint-buffer)
908 (eq gud-minor-mode 'gdbmi))
909 (error "Not in a GDB-MI buffer"))
910 (let ((proc (get-buffer-process gud-comint-buffer)))
911 (if (and (eobp) proc (process-live-p proc)
913 (= (point) (marker-position (process-mark proc))))
914 ;; Sending an EOF does not work with GDB-MI; submit an
915 ;; explicit quit command.
918 (comint-send-input t t))
921 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
923 (defun gdb-create-define-alist ()
924 "Create an alist of #define directives for GUD tooltips."
925 (let* ((file (buffer-file-name))
927 (with-output-to-string
928 (with-current-buffer standard-output
931 ;; call-process doesn't work with remote file names.
932 (not (file-remote-p default-directory))
933 (call-process shell-file-name file
934 (list t nil) nil "-c"
935 (concat gdb-cpp-define-alist-program " "
936 gdb-cpp-define-alist-flags))))))
937 (define-list (split-string output "\n" t))
939 (setq gdb-define-alist nil)
940 (dolist (define define-list)
941 (setq name (nth 1 (split-string define "[( ]")))
942 (push (cons name define) gdb-define-alist))))
944 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
946 (defun gdb-tooltip-print (expr)
947 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
948 (goto-char (point-min))
949 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
951 (concat expr " = " (read (match-string 1)))
952 (or gud-tooltip-echo-area
953 (not (display-graphic-p)))))))
955 ;; If expr is a macro for a function don't print because of possible dangerous
956 ;; side-effects. Also printing a function within a tooltip generates an
957 ;; unexpected starting annotation (phase error).
958 (defun gdb-tooltip-print-1 (expr)
959 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
960 (goto-char (point-min))
961 (if (search-forward "expands to: " nil t)
962 (unless (looking-at "\\S-+.*(.*).*")
963 (gdb-input (concat "-data-evaluate-expression " expr)
964 `(lambda () (gdb-tooltip-print ,expr)))))))
966 (defun gdb-init-buffer ()
967 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
968 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
969 (when gud-tooltip-mode
970 (make-local-variable 'gdb-define-alist)
971 (gdb-create-define-alist)
972 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
974 (defmacro gdb-if-arrow (arrow-position &rest body)
976 (let ((buffer (marker-buffer ,arrow-position)) (line))
977 (if (equal buffer (window-buffer (posn-window end)))
978 (with-current-buffer buffer
979 (when (or (equal start end)
980 (equal (posn-point start)
981 (marker-position ,arrow-position)))
984 (defun gdb-mouse-until (event)
985 "Continue running until a source line past the current line.
986 The destination source line can be selected either by clicking
987 with mouse-3 on the fringe/margin or dragging the arrow
988 with mouse-1 (default bindings)."
990 (let ((start (event-start event))
991 (end (event-end event)))
992 (gdb-if-arrow gud-overlay-arrow-position
993 (setq line (line-number-at-pos (posn-point end)))
994 (gud-call (concat "until " (number-to-string line))))
995 (gdb-if-arrow gdb-disassembly-position
997 (goto-char (point-min))
998 (forward-line (1- (line-number-at-pos (posn-point end))))
1000 (gud-call (concat "until *%a"))))))
1002 (defun gdb-mouse-jump (event)
1003 "Set execution address/line.
1004 The destination source line can be selected either by clicking with C-mouse-3
1005 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1006 Unlike `gdb-mouse-until' the destination address can be before the current
1007 line, and no execution takes place."
1009 (let ((start (event-start event))
1010 (end (event-end event)))
1011 (gdb-if-arrow gud-overlay-arrow-position
1012 (setq line (line-number-at-pos (posn-point end)))
1014 (gud-call (concat "tbreak " (number-to-string line)))
1015 (gud-call (concat "jump " (number-to-string line)))))
1016 (gdb-if-arrow gdb-disassembly-position
1018 (goto-char (point-min))
1019 (forward-line (1- (line-number-at-pos (posn-point end))))
1022 (gud-call (concat "tbreak *%a"))
1023 (gud-call (concat "jump *%a")))))))
1025 (defcustom gdb-show-changed-values t
1026 "If non-nil change the face of out of scope variables and changed values.
1027 Out of scope variables are suppressed with `shadow' face.
1028 Changed values are highlighted with the face `font-lock-warning-face'."
1033 (defcustom gdb-max-children 40
1034 "Maximum number of children before expansion requires confirmation."
1039 (defcustom gdb-delete-out-of-scope t
1040 "If non-nil delete watch expressions automatically when they go out of scope."
1045 (defcustom gdb-speedbar-auto-raise nil
1046 "If non-nil raise speedbar every time display of watch expressions is\
1052 (defcustom gdb-use-colon-colon-notation nil
1053 "If non-nil use FUN::VAR format to display variables in the speedbar."
1058 (defun gdb-speedbar-auto-raise (arg)
1059 "Toggle automatic raising of the speedbar for watch expressions.
1060 With prefix argument ARG, automatically raise speedbar if ARG is
1061 positive, otherwise don't automatically raise it."
1063 (setq gdb-speedbar-auto-raise
1065 (not gdb-speedbar-auto-raise)
1066 (> (prefix-numeric-value arg) 0)))
1067 (message (format "Auto raising %sabled"
1068 (if gdb-speedbar-auto-raise "en" "dis"))))
1070 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1071 (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
1073 (declare-function tooltip-identifier-from-point "tooltip" (point))
1075 (defun gud-watch (&optional arg event)
1076 "Watch expression at point.
1077 With arg, enter name of variable to be watched in the minibuffer."
1078 (interactive (list current-prefix-arg last-input-event))
1079 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1080 (if (eq minor-mode 'gdbmi)
1082 (if event (posn-set-point (event-end event)))
1084 (save-selected-window
1087 (completing-read "Name of variable: "
1088 'gud-gdb-complete-command)
1089 (if (and transient-mark-mode mark-active)
1090 (buffer-substring (region-beginning) (region-end))
1091 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1092 (tooltip-identifier-from-point (point)))))))
1093 (set-text-properties 0 (length expr) nil expr)
1094 (gdb-input (concat "-var-create - * " expr "")
1095 `(lambda () (gdb-var-create-handler ,expr))))))
1096 (message "gud-watch is a no-op in this mode."))))
1098 (defun gdb-var-create-handler (expr)
1099 (let* ((result (gdb-json-partial-output)))
1100 (if (not (bindat-get-field result 'msg))
1102 (list (bindat-get-field result 'name)
1103 (if (and (string-equal gdb-current-language "c")
1104 gdb-use-colon-colon-notation gdb-selected-frame)
1105 (setq expr (concat gdb-selected-frame "::" expr))
1107 (bindat-get-field result 'numchild)
1108 (bindat-get-field result 'type)
1109 (bindat-get-field result 'value)
1111 (bindat-get-field result 'has_more)
1112 gdb-frame-address)))
1113 (push var gdb-var-list)
1115 (unless (string-equal
1116 speedbar-initial-expansion-list-name "GUD")
1117 (speedbar-change-initial-expansion-list "GUD")))
1118 (message-box "No symbol \"%s\" in current context." expr))))
1120 (defun gdb-speedbar-update ()
1121 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1122 (not (gdb-pending-p 'gdb-speedbar-timer)))
1123 ;; Dummy command to update speedbar even when idle.
1124 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn)
1125 ;; Keep gdb-pending-triggers non-nil till end.
1126 (gdb-add-pending 'gdb-speedbar-timer)))
1128 (defun gdb-speedbar-timer-fn ()
1129 (if gdb-speedbar-auto-raise
1130 (raise-frame speedbar-frame))
1131 (gdb-delete-pending 'gdb-speedbar-timer)
1132 (speedbar-timer-fn))
1134 (defun gdb-var-evaluate-expression-handler (varnum changed)
1135 (goto-char (point-min))
1136 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1137 (let ((var (assoc varnum gdb-var-list)))
1139 (if changed (setcar (nthcdr 5 var) 'changed))
1140 (setcar (nthcdr 4 var) (read (match-string 1)))))
1141 (gdb-speedbar-update))
1143 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1144 (defun gdb-var-list-children (varnum)
1145 (gdb-input (concat "-var-update " varnum) 'ignore)
1146 (gdb-input (concat "-var-list-children --all-values " varnum)
1147 `(lambda () (gdb-var-list-children-handler ,varnum))))
1149 (defun gdb-var-list-children-handler (varnum)
1150 (let* ((var-list nil)
1151 (output (bindat-get-field (gdb-json-partial-output "child")))
1152 (children (bindat-get-field output 'children)))
1153 (catch 'child-already-watched
1154 (dolist (var gdb-var-list)
1155 (if (string-equal varnum (car var))
1157 ;; With dynamic varobjs numchild may have increased.
1158 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1160 (dolist (child children)
1161 (let ((varchild (list (bindat-get-field child 'name)
1162 (bindat-get-field child 'exp)
1163 (bindat-get-field child 'numchild)
1164 (bindat-get-field child 'type)
1165 (bindat-get-field child 'value)
1167 (bindat-get-field child 'has_more))))
1168 (if (assoc (car varchild) gdb-var-list)
1169 (throw 'child-already-watched nil))
1170 (push varchild var-list))))
1171 (push var var-list)))
1172 (setq gdb-var-list (nreverse var-list))))
1173 (gdb-speedbar-update))
1175 (defun gdb-var-set-format (format)
1176 "Set the output format for a variable displayed in the speedbar."
1177 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1179 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1182 (defun gdb-var-delete-1 (var varnum)
1183 (gdb-input (concat "-var-delete " varnum) 'ignore)
1184 (setq gdb-var-list (delq var gdb-var-list))
1185 (dolist (varchild gdb-var-list)
1186 (if (string-match (concat (car var) "\\.") (car varchild))
1187 (setq gdb-var-list (delq varchild gdb-var-list)))))
1189 (defun gdb-var-delete ()
1190 "Delete watch expression at point from the speedbar."
1192 (let ((text (speedbar-line-text)))
1193 (string-match "\\(\\S-+\\)" text)
1194 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1196 (if (string-match "\\." (car var))
1197 (message-box "Can only delete a root expression")
1198 (gdb-var-delete-1 var varnum)))))
1200 (defun gdb-var-delete-children (varnum)
1201 "Delete children of variable object at point from the speedbar."
1202 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1204 (defun gdb-edit-value (_text _token _indent)
1205 "Assign a value to a variable displayed in the speedbar."
1206 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1207 (varnum (car var)) (value))
1208 (setq value (read-string "New value: "))
1209 (gdb-input (concat "-var-assign " varnum " " value)
1210 `(lambda () (gdb-edit-value-handler ,value)))))
1212 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1214 (defun gdb-edit-value-handler (value)
1215 (goto-char (point-min))
1216 (if (re-search-forward gdb-error-regexp nil t)
1217 (message-box "Invalid number or expression (%s)" value)))
1219 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1220 (defun gdb-var-update ()
1221 (if (not (gdb-pending-p 'gdb-var-update))
1222 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler))
1223 (gdb-add-pending 'gdb-var-update))
1225 (defun gdb-var-update-handler ()
1226 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1227 (dolist (var gdb-var-list)
1228 (setcar (nthcdr 5 var) nil))
1229 (let ((temp-var-list gdb-var-list))
1230 (dolist (change changelist)
1231 (let* ((varnum (bindat-get-field change 'name))
1232 (var (assoc varnum gdb-var-list))
1233 (new-num (bindat-get-field change 'new_num_children)))
1235 (let ((scope (bindat-get-field change 'in_scope))
1236 (has-more (bindat-get-field change 'has_more)))
1237 (cond ((string-equal scope "false")
1238 (if gdb-delete-out-of-scope
1239 (gdb-var-delete-1 var varnum)
1240 (setcar (nthcdr 5 var) 'out-of-scope)))
1241 ((string-equal scope "true")
1242 (setcar (nthcdr 6 var) has-more)
1243 (when (and (or (not has-more)
1244 (string-equal has-more "0"))
1246 (string-equal (nth 2 var) "0"))
1247 (setcar (nthcdr 4 var)
1248 (bindat-get-field change 'value))
1249 (setcar (nthcdr 5 var) 'changed)))
1250 ((string-equal scope "invalid")
1251 (gdb-var-delete-1 var varnum)))))
1252 (let ((var-list nil) var1
1253 (children (bindat-get-field change 'new_children)))
1255 (setq var1 (pop temp-var-list))
1257 (if (string-equal varnum (car var1))
1258 (let ((new (string-to-number new-num))
1259 (previous (string-to-number (nth 2 var1))))
1260 (setcar (nthcdr 2 var1) new-num)
1261 (push var1 var-list)
1264 ;; Add new children to list.
1265 (dotimes (dummy previous)
1266 (push (pop temp-var-list) var-list))
1267 (dolist (child children)
1269 (list (bindat-get-field child 'name)
1270 (bindat-get-field child 'exp)
1271 (bindat-get-field child 'numchild)
1272 (bindat-get-field child 'type)
1273 (bindat-get-field child 'value)
1275 (bindat-get-field child 'has_more))))
1276 (push varchild var-list))))
1277 ;; Remove deleted children from list.
1279 (dotimes (dummy new)
1280 (push (pop temp-var-list) var-list))
1281 (dotimes (dummy (- previous new))
1282 (pop temp-var-list)))))
1283 (push var1 var-list))
1284 (setq var1 (pop temp-var-list)))
1285 (setq gdb-var-list (nreverse var-list))))))))
1286 (setq gdb-pending-triggers
1287 (delq 'gdb-var-update gdb-pending-triggers))
1288 (gdb-speedbar-update))
1290 (defun gdb-speedbar-expand-node (text token indent)
1291 "Expand the node the user clicked on.
1292 TEXT is the text of the button we clicked on, a + or - item.
1293 TOKEN is data related to this node.
1294 INDENT is the current indentation depth."
1295 (cond ((string-match "+" text) ;expand this node
1296 (let* ((var (assoc token gdb-var-list))
1297 (expr (nth 1 var)) (children (nth 2 var)))
1298 (if (or (<= (string-to-number children) gdb-max-children)
1300 (format "%s has %s children. Continue? " expr children)))
1301 (gdb-var-list-children token))))
1302 ((string-match "-" text) ;contract this node
1303 (dolist (var gdb-var-list)
1304 (if (string-match (concat token "\\.") (car var))
1305 (setq gdb-var-list (delq var gdb-var-list))))
1306 (gdb-var-delete-children token)
1307 (speedbar-change-expand-button-char ?+)
1308 (speedbar-delete-subblock indent))
1309 (t (error "Ooops... not sure what to do")))
1310 (speedbar-center-buffer-smartly))
1312 (defun gdb-get-target-string ()
1313 (with-current-buffer gud-comint-buffer
1320 ;; Each buffer has a TYPE -- a symbol that identifies the function
1321 ;; of that particular buffer.
1323 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1324 ;; is constructed specially.
1326 ;; Others are constructed by gdb-get-buffer-create and
1327 ;; named according to the rules set forth in the gdb-buffer-rules
1329 (defvar gdb-buffer-rules '())
1331 (defun gdb-rules-name-maker (rules-entry)
1333 (defun gdb-rules-buffer-mode (rules-entry)
1334 (nth 2 rules-entry))
1335 (defun gdb-rules-update-trigger (rules-entry)
1336 (nth 3 rules-entry))
1338 (defun gdb-update-buffer-name ()
1339 "Rename current buffer according to name-maker associated with
1340 it in `gdb-buffer-rules'."
1341 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1342 gdb-buffer-rules))))
1343 (when f (rename-buffer (funcall f)))))
1345 (defun gdb-current-buffer-rules ()
1346 "Get `gdb-buffer-rules' entry for current buffer type."
1347 (assoc gdb-buffer-type gdb-buffer-rules))
1349 (defun gdb-current-buffer-thread ()
1350 "Get thread object of current buffer from `gdb-threads-list'.
1352 When current buffer is not bound to any thread, return main
1354 (cdr (assoc gdb-thread-number gdb-threads-list)))
1356 (defun gdb-current-buffer-frame ()
1357 "Get current stack frame object for thread of current buffer."
1358 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1360 (defun gdb-buffer-type (buffer)
1361 "Get value of `gdb-buffer-type' for BUFFER."
1362 (with-current-buffer buffer
1365 (defun gdb-buffer-shows-main-thread-p ()
1366 "Return t if current GDB buffer shows main selected thread and
1367 is not bound to it."
1369 (not (local-variable-p 'gdb-thread-number)))
1371 (defun gdb-get-buffer (buffer-type &optional thread)
1372 "Get a specific GDB buffer.
1374 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1375 and `gdb-thread-number' (if provided) must be equal to THREAD."
1377 (dolist (buffer (buffer-list) nil)
1378 (with-current-buffer buffer
1379 (when (and (eq gdb-buffer-type buffer-type)
1381 (equal gdb-thread-number thread)))
1382 (throw 'found buffer))))))
1384 (defun gdb-get-buffer-create (buffer-type &optional thread)
1385 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1386 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1388 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1389 buffer-local variable of the new buffer.
1391 Buffer mode and name are selected according to buffer type.
1393 If buffer has trigger associated with it in `gdb-buffer-rules',
1394 this trigger is subscribed to `gdb-buf-publisher' and called with
1396 (or (gdb-get-buffer buffer-type thread)
1397 (let ((rules (assoc buffer-type gdb-buffer-rules))
1398 (new (generate-new-buffer "limbo")))
1399 (with-current-buffer new
1400 (let ((mode (gdb-rules-buffer-mode rules))
1401 (trigger (gdb-rules-update-trigger rules)))
1402 (when mode (funcall mode))
1403 (setq gdb-buffer-type buffer-type)
1405 (set (make-local-variable 'gdb-thread-number) thread))
1406 (set (make-local-variable 'gud-minor-mode)
1407 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1408 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1409 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1411 (gdb-add-subscriber gdb-buf-publisher
1412 (cons (current-buffer)
1413 (gdb-bind-function-to-buffer
1414 trigger (current-buffer))))
1415 (funcall trigger 'start))
1416 (current-buffer))))))
1418 (defun gdb-bind-function-to-buffer (expr buffer)
1419 "Return a function which will evaluate EXPR in BUFFER."
1420 `(lambda (&rest args)
1421 (with-current-buffer ,buffer
1422 (apply ',expr args))))
1424 ;; Used to define all gdb-frame-*-buffer functions except
1425 ;; `gdb-frame-io-buffer'
1426 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1427 "Define a function NAME which shows gdb BUFFER in a separate frame.
1429 DOC is an optional documentation string."
1430 `(defun ,name (&optional thread)
1433 (let ((special-display-regexps (append special-display-regexps '(".*")))
1434 (special-display-frame-alist gdb-frame-parameters))
1435 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1437 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1438 "Define a function NAME which shows gdb BUFFER.
1440 DOC is an optional documentation string."
1441 `(defun ,name (&optional thread)
1445 (gdb-get-buffer-create ,buffer thread) t)))
1447 ;; Used to display windows with thread-bound buffers
1448 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1450 `(defun ,name (&optional thread)
1453 (gdb-preempt-existing-or-display-buffer
1454 (gdb-get-buffer-create ,buffer thread)
1455 ,split-horizontal)))
1457 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1458 ;; at least one and possible more functions. The functions have these
1459 ;; roles in defining a buffer type:
1461 ;; NAME - Return a name for this buffer type.
1463 ;; The remaining function(s) are optional:
1465 ;; MODE - called in a new buffer with no arguments, should establish
1466 ;; the proper mode for the buffer.
1469 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1470 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1472 (setcdr binding rules)
1473 (push (cons buffer-type rules)
1474 gdb-buffer-rules))))
1476 (defun gdb-parent-mode ()
1477 "Generic mode to derive all other GDB buffer modes from."
1478 (kill-all-local-variables)
1479 (setq buffer-read-only t)
1480 (buffer-disable-undo)
1481 ;; Delete buffer from gdb-buf-publisher when it's killed
1482 ;; (if it has an associated update trigger)
1487 (let ((trigger (gdb-rules-update-trigger
1488 (gdb-current-buffer-rules))))
1490 (gdb-delete-subscriber
1492 ;; This should match gdb-add-subscriber done in
1493 ;; gdb-get-buffer-create
1494 (cons (current-buffer)
1495 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1498 ;; Partial-output buffer : This accumulates output from a command executed on
1499 ;; behalf of emacs (rather than the user).
1501 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1502 'gdb-partial-output-name)
1504 (defun gdb-partial-output-name ()
1505 (concat " *partial-output-"
1506 (gdb-get-target-string)
1510 (gdb-set-buffer-rules 'gdb-inferior-io
1511 'gdb-inferior-io-name
1512 'gdb-inferior-io-mode)
1514 (defun gdb-inferior-io-name ()
1515 (concat "*input/output of "
1516 (gdb-get-target-string)
1519 (defun gdb-display-io-buffer ()
1520 "Display IO of debugged program in a separate window."
1523 (gdb-get-buffer-create 'gdb-inferior-io) t))
1525 (defun gdb-inferior-io--init-proc (proc)
1526 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1527 (set-process-filter proc 'gdb-inferior-filter)
1528 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1530 (concat "-inferior-tty-set "
1531 ;; The process can run on a remote host.
1532 (or (process-get proc 'remote-tty)
1533 (process-tty-name proc)))
1536 (defun gdb-inferior-io-sentinel (proc str)
1537 (when (eq (process-status proc) 'failed)
1538 ;; When the debugged process exits, Emacs gets an EIO error on
1539 ;; read from the pty, and stops listening to it. If the gdb
1540 ;; process is still running, remove the pty, make a new one, and
1542 (let ((gdb-proc (get-buffer-process gud-comint-buffer))
1543 (io-buffer (process-buffer proc)))
1544 (when (and gdb-proc (process-live-p gdb-proc)
1545 (buffer-live-p io-buffer))
1546 ;; `comint-exec' deletes the original process as a side effect.
1547 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1548 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1550 (defconst gdb-frame-parameters
1551 '((height . 14) (width . 80)
1553 (tool-bar-lines . nil)
1554 (menu-bar-lines . nil)
1555 (minibuffer . nil)))
1557 (defun gdb-frame-io-buffer ()
1558 "Display IO of debugged program in a new frame."
1560 (let ((special-display-regexps (append special-display-regexps '(".*")))
1561 (special-display-frame-alist gdb-frame-parameters))
1562 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1564 (defvar gdb-inferior-io-mode-map
1565 (let ((map (make-sparse-keymap)))
1566 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1567 (define-key map "\C-c\C-z" 'gdb-io-stop)
1568 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1569 (define-key map "\C-c\C-d" 'gdb-io-eof)
1570 (define-key map "\C-d" 'gdb-io-eof)
1573 ;; We want to use comint because it has various nifty and familiar features.
1574 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1575 "Major mode for gdb inferior-io."
1576 :syntax-table nil :abbrev-table nil
1577 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1579 (defun gdb-inferior-filter (proc string)
1580 (unless (string-equal string "")
1581 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1582 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1583 (comint-output-filter proc string)))
1585 (defun gdb-io-interrupt ()
1586 "Interrupt the program being debugged."
1589 (get-buffer-process gud-comint-buffer) comint-ptyp))
1591 (defun gdb-io-quit ()
1592 "Send quit signal to the program being debugged."
1595 (get-buffer-process gud-comint-buffer) comint-ptyp))
1597 (defun gdb-io-stop ()
1598 "Stop the program being debugged."
1601 (get-buffer-process gud-comint-buffer) comint-ptyp))
1603 (defun gdb-io-eof ()
1604 "Send end-of-file to the program being debugged."
1607 (get-buffer-process gud-comint-buffer)))
1609 (defun gdb-clear-inferior-io ()
1610 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1614 (defconst breakpoint-xpm-data
1616 static char *magick[] = {
1617 /* columns rows colors chars-per-pixel */
1633 "XPM data used for breakpoint icon.")
1635 (defconst breakpoint-enabled-pbm-data
1638 0 0 0 0 1 1 1 1 0 0 0 0
1639 0 0 0 1 1 1 1 1 1 0 0 0
1640 0 0 1 1 1 1 1 1 1 1 0 0
1641 0 1 1 1 1 1 1 1 1 1 1 0
1642 0 1 1 1 1 1 1 1 1 1 1 0
1643 0 1 1 1 1 1 1 1 1 1 1 0
1644 0 1 1 1 1 1 1 1 1 1 1 0
1645 0 0 1 1 1 1 1 1 1 1 0 0
1646 0 0 0 1 1 1 1 1 1 0 0 0
1647 0 0 0 0 1 1 1 1 0 0 0 0"
1648 "PBM data used for enabled breakpoint icon.")
1650 (defconst breakpoint-disabled-pbm-data
1662 0 0 0 1 0 1 0 1 0 0"
1663 "PBM data used for disabled breakpoint icon.")
1665 (defvar breakpoint-enabled-icon nil
1666 "Icon for enabled breakpoint in display margin.")
1668 (defvar breakpoint-disabled-icon nil
1669 "Icon for disabled breakpoint in display margin.")
1671 (declare-function define-fringe-bitmap "fringe.c"
1672 (bitmap bits &optional height width align))
1674 (and (display-images-p)
1675 ;; Bitmap for breakpoint in fringe
1676 (define-fringe-bitmap 'breakpoint
1677 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1678 ;; Bitmap for gud-overlay-arrow in fringe
1679 (define-fringe-bitmap 'hollow-right-triangle
1680 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1682 (defface breakpoint-enabled
1686 "Face for enabled breakpoint icon in fringe."
1689 (defface breakpoint-disabled
1690 '((((class color) (min-colors 88)) :foreground "grey70")
1691 ;; Ensure that on low-color displays that we end up something visible.
1692 (((class color) (min-colors 8) (background light))
1693 :foreground "black")
1694 (((class color) (min-colors 8) (background dark))
1695 :foreground "white")
1696 (((type tty) (class mono))
1698 (t :background "gray"))
1699 "Face for disabled breakpoint icon in fringe."
1703 (defun gdb-send (proc string)
1704 "A comint send filter for gdb."
1705 (with-current-buffer gud-comint-buffer
1706 (let ((inhibit-read-only t))
1707 (remove-text-properties (point-min) (point-max) '(face))))
1708 ;; mimic <RET> key to repeat previous command in GDB
1709 (if (not (string= "" string))
1710 (setq gdb-last-command string)
1711 (if gdb-last-command (setq string gdb-last-command)))
1712 (if (string-match "^-" string)
1715 (setq gdb-first-done-or-error t)
1716 (process-send-string proc (concat string "\n")))
1718 (if (string-match "\\\\$" string)
1719 (setq gdb-continuation (concat gdb-continuation string "\n"))
1720 (setq gdb-first-done-or-error t)
1721 (let ((to-send (concat "-interpreter-exec console "
1722 (gdb-mi-quote string)
1724 (if gdb-enable-debug
1725 (push (cons 'mi-send to-send) gdb-debug-log))
1726 (process-send-string proc to-send))
1727 (setq gdb-continuation nil))))
1729 (defun gdb-mi-quote (string)
1730 "Return STRING quoted properly as an MI argument.
1731 The string is enclosed in double quotes.
1732 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1733 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1734 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1735 (concat "\"" string "\""))
1737 (defun gdb-input (command handler-function)
1738 "Send COMMAND to GDB via the MI interface.
1739 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1741 (if gdb-enable-debug (push (list 'send-item command handler-function)
1743 (setq gdb-token-number (1+ gdb-token-number))
1744 (setq command (concat (number-to-string gdb-token-number) command))
1745 (push (cons gdb-token-number handler-function) gdb-handler-alist)
1746 (process-send-string (get-buffer-process gud-comint-buffer)
1747 (concat command "\n")))
1749 ;; NOFRAME is used for gud execution control commands
1750 (defun gdb-current-context-command (command)
1751 "Add --thread to gdb COMMAND when needed."
1752 (if (and gdb-thread-number
1753 gdb-supports-non-stop)
1754 (concat command " --thread " gdb-thread-number)
1757 (defun gdb-current-context-buffer-name (name)
1758 "Add thread information and asterisks to string NAME.
1760 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1762 (if (local-variable-p 'gdb-thread-number)
1763 (format " (bound to thread %s)" gdb-thread-number)
1767 (defun gdb-current-context-mode-name (mode)
1768 "Add thread information to MODE which is to be used as
1771 (if gdb-thread-number
1772 (format " [thread %s]" gdb-thread-number)
1776 (defcustom gud-gdb-command-name "gdb -i=mi"
1777 "Default command to execute an executable under the GDB debugger."
1782 (setq gud-running nil)
1783 (setq gdb-output-sink 'user)
1784 (setq gdb-pending-triggers nil))
1786 (defun gdb-update (&optional no-proc)
1787 "Update buffers showing status of debug session.
1788 If NO-PROC is non-nil, do not try to contact the GDB process."
1789 (when gdb-first-prompt
1790 (gdb-force-mode-line-update
1791 (propertize "initializing..." 'face font-lock-variable-name-face))
1793 (setq gdb-first-prompt nil))
1796 (gdb-get-main-selected-frame))
1798 ;; We may need to update gdb-threads-list so we can use
1799 (gdb-get-buffer-create 'gdb-threads-buffer)
1800 ;; gdb-break-list is maintained in breakpoints handler
1801 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1804 (gdb-emit-signal gdb-buf-publisher 'update))
1806 (gdb-get-changed-registers)
1807 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1808 (dolist (var gdb-var-list)
1809 (setcar (nthcdr 5 var) nil))
1812 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1813 ;; because we may need to update current gud-running value without
1814 ;; changing current thread (see gdb-running)
1815 (defun gdb-setq-thread-number (number)
1816 "Only this function must be used to change `gdb-thread-number'
1817 value to NUMBER, because `gud-running' and `gdb-frame-number'
1818 need to be updated appropriately when current thread changes."
1819 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1820 (unless (string-equal number "0") (setq gdb-thread-number number))
1821 (setq gdb-frame-number "0")
1822 (gdb-update-gud-running))
1824 (defun gdb-update-gud-running ()
1825 "Set `gud-running' according to the state of current thread.
1827 `gdb-frame-number' is set to 0 if current thread is now stopped.
1829 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1830 cannot be reliably used to determine whether or not execution
1831 control buttons should be shown in menu or toolbar. Use
1832 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1835 For all-stop mode, thread information is unavailable while target
1837 (let ((old-value gud-running))
1839 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1841 ;; Set frame number to "0" when _current_ threads stops
1842 (when (and (gdb-current-buffer-thread)
1843 (not (eq gud-running old-value)))
1844 (setq gdb-frame-number "0"))))
1846 (defun gdb-show-run-p ()
1847 "Return t if \"Run/continue\" should be shown on the toolbar."
1848 (or (not gdb-active-process)
1850 (not gdb-gud-control-all-threads)
1853 (and gdb-gud-control-all-threads
1854 (> gdb-stopped-threads-count 0))))
1856 (defun gdb-show-stop-p ()
1857 "Return t if \"Stop\" should be shown on the toolbar."
1859 (not gdb-gud-control-all-threads)
1862 (and gdb-gud-control-all-threads
1863 (> gdb-running-threads-count 0))))
1865 ;; GUD displays the selected GDB frame. This might might not be the current
1866 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1867 ;; visited breakpoint is, use that window.
1868 (defun gdb-display-source-buffer (buffer)
1869 (let* ((last-window (if gud-last-last-frame
1871 (gud-find-file (car gud-last-last-frame)))))
1872 (source-window (or last-window
1873 (if (and gdb-source-window
1874 (window-live-p gdb-source-window))
1875 gdb-source-window))))
1877 (setq gdb-source-window source-window)
1878 (set-window-buffer source-window buffer))
1881 (defun gdb-car< (a b)
1882 (< (car a) (car b)))
1884 (defvar gdbmi-record-list
1885 '((gdb-gdb . "(gdb) \n")
1886 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1887 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1888 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1889 (gdb-console . "~\\(\".*?\"\\)\n")
1890 (gdb-internals . "&\\(\".*?\"\\)\n")
1891 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1892 (gdb-running . "\\*running,\\(.*?\n\\)")
1893 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1894 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1895 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1896 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1897 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1899 (defun gud-gdbmi-marker-filter (string)
1900 "Filter GDB/MI output."
1902 ;; Record transactions if logging is enabled.
1903 (when gdb-enable-debug
1904 (push (cons 'recv string) gdb-debug-log)
1905 (if (and gdb-debug-log-max
1906 (> (length gdb-debug-log) gdb-debug-log-max))
1907 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1909 ;; Recall the left over gud-marker-acc from last time
1910 (setq gud-marker-acc (concat gud-marker-acc string))
1912 ;; Start accumulating output for the GUD buffer
1913 (setq gdb-filter-output "")
1914 (let (output-record-list)
1916 ;; Process all the complete markers in this chunk.
1917 (dolist (gdbmi-record gdbmi-record-list)
1918 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1919 (push (list (match-beginning 0)
1921 (match-string 1 gud-marker-acc)
1922 (match-string 2 gud-marker-acc)
1925 (setq gud-marker-acc
1926 (concat (substring gud-marker-acc 0 (match-beginning 0))
1927 ;; Pad with spaces to preserve position.
1928 (make-string (length (match-string 0 gud-marker-acc)) 32)
1929 (substring gud-marker-acc (match-end 0))))))
1931 (setq output-record-list (sort output-record-list 'gdb-car<))
1933 (dolist (output-record output-record-list)
1934 (let ((record-type (cadr output-record))
1935 (arg1 (nth 2 output-record))
1936 (arg2 (nth 3 output-record)))
1937 (cond ((eq record-type 'gdb-error)
1938 (gdb-done-or-error arg2 arg1 'error))
1939 ((eq record-type 'gdb-done)
1940 (gdb-done-or-error arg2 arg1 'done))
1941 ;; Suppress "No registers." GDB 6.8 and earlier
1942 ;; duplicates MI error message on internal stream.
1943 ;; Don't print to GUD buffer.
1944 ((not (and (eq record-type 'gdb-internals)
1945 (string-equal (read arg1) "No registers.\n")))
1946 (funcall record-type arg1)))))
1948 (setq gdb-output-sink 'user)
1950 (string-match "^ *" gud-marker-acc)
1951 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1955 (defun gdb-gdb (_output-field))
1957 (defun gdb-shell (output-field)
1958 (let ((gdb-output-sink gdb-output-sink))
1959 (setq gdb-filter-output
1960 (concat output-field gdb-filter-output))))
1962 (defun gdb-ignored-notification (_output-field))
1964 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1965 (defun gdb-thread-created (_output-field))
1966 (defun gdb-thread-exited (output-field)
1967 "Handle =thread-exited async record: unset `gdb-thread-number'
1968 if current thread exited and update threads list."
1969 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1970 (if (string= gdb-thread-number thread-id)
1971 (gdb-setq-thread-number nil))
1972 ;; When we continue current thread and it quickly exits,
1973 ;; gdb-pending-triggers left after gdb-running disallow us to
1974 ;; properly call -thread-info without --thread option. Thus we
1975 ;; need to use gdb-wait-for-pending.
1976 (gdb-wait-for-pending
1977 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1979 (defun gdb-thread-selected (output-field)
1980 "Handler for =thread-selected MI output record.
1982 Sets `gdb-thread-number' to new id."
1983 (let* ((result (gdb-json-string output-field))
1984 (thread-id (bindat-get-field result 'id)))
1985 (gdb-setq-thread-number thread-id)
1986 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1987 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1988 ;; as usually. Things happen to fast and second call (from
1989 ;; gdb-thread-selected handler) gets cut off by our beloved
1990 ;; gdb-pending-triggers.
1991 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1992 ;; body will get executed when `gdb-pending-triggers` is empty.
1993 (gdb-wait-for-pending
1996 (defun gdb-running (output-field)
1998 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
1999 ;; We reset gdb-frame-number to nil if current thread has gone
2000 ;; running. This can't be done in gdb-thread-list-handler-custom
2001 ;; because we need correct gdb-frame-number by the time
2002 ;; -thread-info command is sent.
2003 (when (or (string-equal thread-id "all")
2004 (string-equal thread-id gdb-thread-number))
2005 (setq gdb-frame-number nil)))
2006 (setq gdb-inferior-status "running")
2007 (gdb-force-mode-line-update
2008 (propertize gdb-inferior-status 'face font-lock-type-face))
2009 (when (not gdb-non-stop)
2010 (setq gud-running t))
2011 (setq gdb-active-process t)
2012 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2014 (defun gdb-starting (_output-field)
2015 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2016 (setq gdb-inferior-status "running")
2017 (gdb-force-mode-line-update
2018 (propertize gdb-inferior-status 'face font-lock-type-face))
2019 (setq gdb-active-process t)
2020 (setq gud-running t)
2021 ;; GDB doesn't seem to respond to -thread-info before first stop or
2022 ;; thread exit (even in non-stop mode), so this is useless.
2023 ;; Behavior may change in the future.
2024 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2026 ;; -break-insert -t didn't give a reason before gdb 6.9
2028 (defun gdb-stopped (output-field)
2029 "Given the contents of *stopped MI async record, select new
2030 current thread and update GDB buffers."
2031 ;; Reason is available with target-async only
2032 (let* ((result (gdb-json-string output-field))
2033 (reason (bindat-get-field result 'reason))
2034 (thread-id (bindat-get-field result 'thread-id)))
2036 ;; -data-list-register-names needs to be issued for any stopped
2038 (when (not gdb-register-names)
2039 (gdb-input (concat "-data-list-register-names"
2040 (if gdb-supports-non-stop
2041 (concat " --thread " thread-id)))
2042 'gdb-register-names-handler))
2044 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
2045 ;;; because synchronous GDB doesn't give these fields with CLI.
2048 ;;; ;; Extract the frame position from the marker.
2049 ;;; gud-last-frame (cons file
2050 ;;; (string-to-number
2051 ;;; (match-string 6 gud-marker-acc)))))
2053 (setq gdb-inferior-status (or reason "unknown"))
2054 (gdb-force-mode-line-update
2055 (propertize gdb-inferior-status 'face font-lock-warning-face))
2056 (if (string-equal reason "exited-normally")
2057 (setq gdb-active-process nil))
2059 ;; Select new current thread.
2061 ;; Don't switch if we have no reasons selected
2062 (when gdb-switch-reasons
2063 ;; Switch from another stopped thread only if we have
2064 ;; gdb-switch-when-another-stopped:
2065 (when (or gdb-switch-when-another-stopped
2066 (not (string= "stopped"
2067 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2068 ;; Switch if current reason has been selected or we have no
2070 (if (or (eq gdb-switch-reasons t)
2071 (member reason gdb-switch-reasons))
2072 (when (not (string-equal gdb-thread-number thread-id))
2073 (message (concat "Switched to thread " thread-id))
2074 (gdb-setq-thread-number thread-id))
2075 (message (format "Thread %s stopped" thread-id)))))
2077 ;; Print "(gdb)" to GUD console
2078 (when gdb-first-done-or-error
2079 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2081 ;; In non-stop, we update information as soon as another thread gets
2083 (when (or gdb-first-done-or-error
2085 ;; In all-stop this updates gud-running properly as well.
2087 (setq gdb-first-done-or-error nil))
2088 (run-hook-with-args 'gdb-stopped-functions result)))
2090 ;; Remove the trimmings from log stream containing debugging messages
2091 ;; being produced by GDB's internals, use warning face and send to GUD
2093 (defun gdb-internals (output-field)
2094 (setq gdb-filter-output
2097 (let ((error-message
2098 (read output-field)))
2100 0 (length error-message)
2101 'face font-lock-warning-face
2105 ;; Remove the trimmings from the console stream and send to GUD buffer
2106 ;; (frontend MI commands should not print to this stream)
2107 (defun gdb-console (output-field)
2108 (setq gdb-filter-output
2109 (gdb-concat-output gdb-filter-output (read output-field))))
2111 (defun gdb-done-or-error (output-field token-number type)
2112 (if (string-equal token-number "")
2113 ;; Output from command entered by user
2115 (setq gdb-output-sink 'user)
2116 (setq token-number nil)
2117 ;; MI error - send to minibuffer
2118 (when (eq type 'error)
2119 ;; Skip "msg=" from `output-field'
2120 (message (read (substring output-field 4)))
2121 ;; Don't send to the console twice. (If it is a console error
2122 ;; it is also in the console stream.)
2123 (setq output-field nil)))
2124 ;; Output from command from frontend.
2125 (setq gdb-output-sink 'emacs))
2127 (gdb-clear-partial-output)
2129 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2130 (let* ((proc (get-buffer-process gud-comint-buffer))
2131 (no-proc (or (null proc)
2132 (memq (process-status proc) '(exit signal)))))
2134 (when gdb-first-done-or-error
2135 (unless (or token-number gud-running no-proc)
2136 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2137 (gdb-update no-proc)
2138 (setq gdb-first-done-or-error nil))
2140 (setq gdb-filter-output
2141 (gdb-concat-output gdb-filter-output output-field))
2144 (with-current-buffer
2145 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2147 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2148 (setq gdb-handler-alist
2149 (assq-delete-all token-number gdb-handler-alist)))))
2151 (defun gdb-concat-output (so-far new)
2153 ((eq gdb-output-sink 'user) (concat so-far new))
2154 ((eq gdb-output-sink 'emacs)
2155 (gdb-append-to-partial-output new)
2158 (defun gdb-append-to-partial-output (string)
2159 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2160 (goto-char (point-max))
2163 (defun gdb-clear-partial-output ()
2164 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2167 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2168 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2170 Field names are wrapped in double quotes and equal signs are
2171 replaced with semicolons.
2173 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2174 partial output. This is used to get rid of useless keys in lists
2175 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2176 -break-info are examples of MI commands which issue such
2179 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2180 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2181 -break-info output when it contains breakpoint script field
2182 incompatible with GDB/MI output syntax."
2184 (goto-char (point-min))
2187 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2188 (replace-match "" nil nil nil 1))))
2191 ;; Find positions of braces which enclose broken list
2192 (while (re-search-forward (concat fix-list "={\"") nil t)
2193 (let ((p1 (goto-char (- (point) 2)))
2194 (p2 (progn (forward-sexp)
2196 ;; Replace braces with brackets
2204 (goto-char (point-min))
2206 (while (re-search-forward
2207 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2208 (replace-match "\"\\1\":\\2" nil nil))
2209 (goto-char (point-max))
2212 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2213 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2215 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2216 (gdb-jsonify-buffer fix-key fix-list)
2218 (goto-char (point-min))
2219 (let ((json-array-type 'list))
2222 (defun gdb-json-string (string &optional fix-key fix-list)
2223 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2225 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2228 (gdb-json-read-buffer fix-key fix-list)))
2230 (defun gdb-json-partial-output (&optional fix-key fix-list)
2231 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2233 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2234 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2235 (gdb-json-read-buffer fix-key fix-list)))
2237 (defun gdb-line-posns (line)
2238 "Return a pair of LINE beginning and end positions."
2239 (let ((offset (1+ (- line (line-number-at-pos)))))
2241 (line-beginning-position offset)
2242 (line-end-position offset))))
2244 (defmacro gdb-mark-line (line variable)
2245 "Set VARIABLE marker to point at beginning of LINE.
2247 If current window has no fringes, inverse colors on LINE.
2249 Return position where LINE begins."
2251 (let* ((posns (gdb-line-posns ,line))
2252 (start-posn (car posns))
2253 (end-posn (cdr posns)))
2254 (set-marker ,variable (copy-marker start-posn))
2255 (when (not (> (car (window-fringes)) 0))
2256 (put-text-property start-posn end-posn
2257 'font-lock-face '(:inverse-video t)))
2260 (defun gdb-pad-string (string padding)
2261 (format (concat "%" (number-to-string padding) "s") string))
2263 ;; gdb-table struct is a way to programmatically construct simple
2264 ;; tables. It help to reliably align columns of data in GDB buffers
2270 (row-properties nil)
2273 (defun gdb-mapcar* (function &rest seqs)
2274 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2275 If there are several SEQS, FUNCTION is called with that many
2276 arguments, and mapping stops as soon as the shortest list runs
2278 (let ((shortest (apply #'min (mapcar #'length seqs))))
2285 (number-sequence 0 (1- shortest)))))
2287 (defun gdb-table-add-row (table row &optional properties)
2288 "Add ROW of string to TABLE and recalculate column sizes.
2290 When non-nil, PROPERTIES will be added to the whole row when
2291 calling `gdb-table-string'."
2292 (let ((rows (gdb-table-rows table))
2293 (row-properties (gdb-table-row-properties table))
2294 (column-sizes (gdb-table-column-sizes table))
2295 (right-align (gdb-table-right-align table)))
2296 (when (not column-sizes)
2297 (setf (gdb-table-column-sizes table)
2298 (make-list (length row) 0)))
2299 (setf (gdb-table-rows table)
2300 (append rows (list row)))
2301 (setf (gdb-table-row-properties table)
2302 (append row-properties (list properties)))
2303 (setf (gdb-table-column-sizes table)
2304 (gdb-mapcar* (lambda (x s)
2306 (max (abs x) (string-width (or s "")))))
2307 (if right-align new-x (- new-x))))
2308 (gdb-table-column-sizes table)
2310 ;; Avoid trailing whitespace at eol
2311 (if (not (gdb-table-right-align table))
2312 (setcar (last (gdb-table-column-sizes table)) 0))))
2314 (defun gdb-table-string (table &optional sep)
2315 "Return TABLE as a string with columns separated with SEP."
2316 (let ((column-sizes (gdb-table-column-sizes table)))
2320 (lambda (row properties)
2322 (mapconcat 'identity
2323 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2327 (gdb-table-rows table)
2328 (gdb-table-row-properties table))
2331 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2332 (defun gdb-get-many-fields (struct &rest fields)
2333 "Return a list of FIELDS values from STRUCT."
2335 (dolist (field fields values)
2336 (setq values (append values (list (bindat-get-field struct field)))))))
2338 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2340 &optional signal-list)
2341 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2342 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2343 buffer with `gdb-bind-function-to-buffer'.
2345 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2346 defined trigger is called with an argument from SIGNAL-LIST. It's
2347 not recommended to define triggers with empty SIGNAL-LIST.
2348 Normally triggers should respond at least to 'update signal.
2350 Normally the trigger defined by this command must be called from
2351 the buffer where HANDLER-NAME must work. This should be done so
2352 that buffer-local thread number may be used in GDB-COMMAND (by
2353 calling `gdb-current-context-command').
2354 `gdb-bind-function-to-buffer' is used to achieve this, see
2355 `gdb-get-buffer-create'.
2357 Triggers defined by this command are meant to be used as a
2358 trigger argument when describing buffer types with
2359 `gdb-set-buffer-rules'."
2360 `(defun ,trigger-name (&optional signal)
2362 (or (not ,signal-list)
2363 (memq signal ,signal-list))
2364 (when (not (gdb-pending-p
2365 (cons (current-buffer) ',trigger-name)))
2366 (gdb-input ,gdb-command
2367 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2368 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2370 ;; Used by disassembly buffer only, the rest use
2371 ;; def-gdb-trigger-and-handler
2372 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2373 &optional nopreserve)
2374 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2376 Handlers are normally called from the buffers they put output in.
2378 Delete ((current-buffer) . TRIGGER-NAME) from
2379 `gdb-pending-triggers', erase current buffer and evaluate
2380 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2382 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2383 `(defun ,handler-name ()
2384 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2385 (let* ((buffer-read-only nil)
2386 (window (get-buffer-window (current-buffer) 0))
2387 (start (window-start window))
2388 (p (window-point window)))
2391 (gdb-update-buffer-name)
2392 ,(when (not nopreserve)
2393 '(set-window-start window start)
2394 '(set-window-point window p)))))
2396 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2397 handler-name custom-defun
2398 &optional signal-list)
2399 "Define trigger and handler.
2401 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2402 `def-gdb-auto-update-trigger'.
2404 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2405 `def-gdb-auto-update-handler'."
2407 (def-gdb-auto-update-trigger ,trigger-name
2409 ,handler-name ,signal-list)
2410 (def-gdb-auto-update-handler ,handler-name
2411 ,trigger-name ,custom-defun)))
2415 ;; Breakpoint buffer : This displays the output of `-break-list'.
2416 (def-gdb-trigger-and-handler
2417 gdb-invalidate-breakpoints "-break-list"
2418 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2421 (gdb-set-buffer-rules
2422 'gdb-breakpoints-buffer
2423 'gdb-breakpoints-buffer-name
2424 'gdb-breakpoints-mode
2425 'gdb-invalidate-breakpoints)
2427 (defun gdb-breakpoints-list-handler-custom ()
2428 (let ((breakpoints-list (bindat-get-field
2429 (gdb-json-partial-output "bkpt" "script")
2430 'BreakpointTable 'body))
2431 (table (make-gdb-table)))
2432 (setq gdb-breakpoints-list nil)
2433 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2434 (dolist (breakpoint breakpoints-list)
2435 (add-to-list 'gdb-breakpoints-list
2436 (cons (bindat-get-field breakpoint 'number)
2438 (let ((at (bindat-get-field breakpoint 'at))
2439 (pending (bindat-get-field breakpoint 'pending))
2440 (func (bindat-get-field breakpoint 'func))
2441 (type (bindat-get-field breakpoint 'type)))
2442 (gdb-table-add-row table
2444 (bindat-get-field breakpoint 'number)
2446 (or (bindat-get-field breakpoint 'disp) "")
2447 (let ((flag (bindat-get-field breakpoint 'enabled)))
2448 (if (string-equal flag "y")
2449 (propertize "y" 'font-lock-face font-lock-warning-face)
2450 (propertize "n" 'font-lock-face font-lock-comment-face)))
2451 (bindat-get-field breakpoint 'addr)
2452 (or (bindat-get-field breakpoint 'times) "")
2453 (if (and type (string-match ".*watchpoint" type))
2454 (bindat-get-field breakpoint 'what)
2457 (propertize (or func "unknown")
2458 'font-lock-face font-lock-function-name-face)
2459 (gdb-frame-location breakpoint)))))
2460 ;; Add clickable properties only for breakpoints with file:line
2462 (append (list 'gdb-breakpoint breakpoint)
2463 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2464 mouse-face highlight))))))
2465 (insert (gdb-table-string table " "))
2466 (gdb-place-breakpoints)))
2468 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2469 (defun gdb-place-breakpoints ()
2470 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2471 (dolist (buffer (buffer-list))
2472 (with-current-buffer buffer
2473 (if (and (eq gud-minor-mode 'gdbmi)
2474 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2475 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2476 (dolist (breakpoint gdb-breakpoints-list)
2477 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2478 ; an associative list
2479 (line (bindat-get-field breakpoint 'line)))
2481 (let ((file (bindat-get-field breakpoint 'fullname))
2482 (flag (bindat-get-field breakpoint 'enabled))
2483 (bptno (bindat-get-field breakpoint 'number)))
2484 (unless (file-exists-p file)
2485 (setq file (cdr (assoc bptno gdb-location-alist))))
2487 (not (string-equal file "File not found")))
2488 (with-current-buffer
2489 (find-file-noselect file 'nowarn)
2491 ;; Only want one breakpoint icon at each location.
2492 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2493 (string-to-number line)))
2494 (gdb-input (concat "list " file ":1") 'ignore)
2495 (gdb-input "-file-list-exec-source-file"
2496 `(lambda () (gdb-get-location
2497 ,bptno ,line ,flag)))))))))
2499 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2501 (defun gdb-get-location (bptno line flag)
2502 "Find the directory containing the relevant source file.
2503 Put in buffer and place breakpoint icon."
2504 (goto-char (point-min))
2505 (catch 'file-not-found
2506 (if (re-search-forward gdb-source-file-regexp nil t)
2507 (delete (cons bptno "File not found") gdb-location-alist)
2508 (push (cons bptno (match-string 1)) gdb-location-alist)
2510 (unless (assoc bptno gdb-location-alist)
2511 (push (cons bptno "File not found") gdb-location-alist)
2512 (message-box "Cannot find source file for breakpoint location.
2513 Add directory to search path for source files using the GDB command, dir."))
2514 (throw 'file-not-found nil))
2515 (with-current-buffer (find-file-noselect (match-string 1))
2517 ;; only want one breakpoint icon at each location
2518 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2520 (add-hook 'find-file-hook 'gdb-find-file-hook)
2522 (defun gdb-find-file-hook ()
2523 "Set up buffer for debugging if file is part of the source code
2524 of the current session."
2525 (if (and (buffer-name gud-comint-buffer)
2526 ;; in case gud or gdb-ui is just loaded
2528 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2530 (if (member buffer-file-name gdb-source-file-list)
2531 (with-current-buffer (find-buffer-visiting buffer-file-name)
2532 (gdb-init-buffer)))))
2534 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2535 (declare-function gud-break "gdb-mi" t t) ; gud-def
2536 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2538 (defun gdb-mouse-set-clear-breakpoint (event)
2539 "Set/clear breakpoint in left fringe/margin at mouse click.
2540 If not in a source or disassembly buffer just set point."
2542 (mouse-minibuffer-check event)
2543 (let ((posn (event-end event)))
2544 (with-selected-window (posn-window posn)
2545 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2546 (if (numberp (posn-point posn))
2548 (goto-char (posn-point posn))
2549 (if (or (posn-object posn)
2550 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2554 (posn-set-point posn))))
2556 (defun gdb-mouse-toggle-breakpoint-margin (event)
2557 "Enable/disable breakpoint in left margin with mouse click."
2559 (mouse-minibuffer-check event)
2560 (let ((posn (event-end event)))
2561 (if (numberp (posn-point posn))
2562 (with-selected-window (posn-window posn)
2564 (goto-char (posn-point posn))
2565 (if (posn-object posn)
2567 (let ((bptno (get-text-property
2568 0 'gdb-bptno (car (posn-string posn)))))
2570 (if (get-text-property
2571 0 'gdb-enabled (car (posn-string posn)))
2576 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2577 "Enable/disable breakpoint in left fringe with mouse click."
2579 (mouse-minibuffer-check event)
2580 (let* ((posn (event-end event))
2581 (pos (posn-point posn))
2584 (with-selected-window (posn-window posn)
2585 (with-current-buffer (window-buffer (selected-window))
2587 (dolist (overlay (overlays-in pos pos))
2588 (when (overlay-get overlay 'put-break)
2589 (setq obj (overlay-get overlay 'before-string))))
2593 (if (get-text-property 0 'gdb-enabled obj)
2596 (get-text-property 0 'gdb-bptno obj)))))))))
2598 (defun gdb-breakpoints-buffer-name ()
2599 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2601 (def-gdb-display-buffer
2602 gdb-display-breakpoints-buffer
2603 'gdb-breakpoints-buffer
2604 "Display status of user-settable breakpoints.")
2606 (def-gdb-frame-for-buffer
2607 gdb-frame-breakpoints-buffer
2608 'gdb-breakpoints-buffer
2609 "Display status of user-settable breakpoints in a new frame.")
2611 (defvar gdb-breakpoints-mode-map
2612 (let ((map (make-sparse-keymap))
2613 (menu (make-sparse-keymap "Breakpoints")))
2614 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2615 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2616 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2617 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2618 (suppress-keymap map)
2619 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2620 (define-key map " " 'gdb-toggle-breakpoint)
2621 (define-key map "D" 'gdb-delete-breakpoint)
2622 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2623 (define-key map "q" 'gdb-delete-frame-or-window)
2624 (define-key map "\r" 'gdb-goto-breakpoint)
2625 (define-key map "\t" (lambda ()
2627 (gdb-set-window-buffer
2628 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2629 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2630 (define-key map [follow-link] 'mouse-face)
2633 (defun gdb-delete-frame-or-window ()
2634 "Delete frame if there is only one window. Otherwise delete the window."
2636 (if (one-window-p) (delete-frame)
2639 ;;from make-mode-line-mouse-map
2640 (defun gdb-make-header-line-mouse-map (mouse function) "\
2641 Return a keymap with single entry for mouse key MOUSE on the header line.
2642 MOUSE is defined to run function FUNCTION with no args in the buffer
2643 corresponding to the mode line clicked."
2644 (let ((map (make-sparse-keymap)))
2645 (define-key map (vector 'header-line mouse) function)
2646 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2649 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2651 'help-echo ,help-echo
2652 'mouse-face ',mouse-face
2655 (gdb-make-header-line-mouse-map
2657 (lambda (event) (interactive "e")
2658 (save-selected-window
2659 (select-window (posn-window (event-start event)))
2660 (gdb-set-window-buffer
2661 (gdb-get-buffer-create ',buffer) t) )))))
2664 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2667 (defun gdb-threads-buffer-name ()
2668 (concat "*threads of " (gdb-get-target-string) "*"))
2670 (def-gdb-display-buffer
2671 gdb-display-threads-buffer
2673 "Display GDB threads.")
2675 (def-gdb-frame-for-buffer
2676 gdb-frame-threads-buffer
2678 "Display GDB threads in a new frame.")
2680 (def-gdb-trigger-and-handler
2681 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2682 gdb-thread-list-handler gdb-thread-list-handler-custom
2683 '(start update update-threads))
2685 (gdb-set-buffer-rules
2687 'gdb-threads-buffer-name
2689 'gdb-invalidate-threads)
2691 (defvar gdb-threads-font-lock-keywords
2692 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2693 (" \\(stopped\\)" (1 font-lock-warning-face))
2694 (" \\(running\\)" (1 font-lock-string-face))
2695 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2696 "Font lock keywords used in `gdb-threads-mode'.")
2698 (defvar gdb-threads-mode-map
2699 (let ((map (make-sparse-keymap)))
2700 (define-key map "\r" 'gdb-select-thread)
2701 (define-key map "f" 'gdb-display-stack-for-thread)
2702 (define-key map "F" 'gdb-frame-stack-for-thread)
2703 (define-key map "l" 'gdb-display-locals-for-thread)
2704 (define-key map "L" 'gdb-frame-locals-for-thread)
2705 (define-key map "r" 'gdb-display-registers-for-thread)
2706 (define-key map "R" 'gdb-frame-registers-for-thread)
2707 (define-key map "d" 'gdb-display-disassembly-for-thread)
2708 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2709 (define-key map "i" 'gdb-interrupt-thread)
2710 (define-key map "c" 'gdb-continue-thread)
2711 (define-key map "s" 'gdb-step-thread)
2712 (define-key map "\t"
2715 (gdb-set-window-buffer
2716 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2717 (define-key map [mouse-2] 'gdb-select-thread)
2718 (define-key map [follow-link] 'mouse-face)
2721 (defvar gdb-threads-header
2723 (gdb-propertize-header
2724 "Breakpoints" gdb-breakpoints-buffer
2725 "mouse-1: select" mode-line-highlight mode-line-inactive)
2727 (gdb-propertize-header "Threads" gdb-threads-buffer
2728 nil nil mode-line)))
2730 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2731 "Major mode for GDB threads."
2732 (setq gdb-thread-position (make-marker))
2733 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2734 (setq header-line-format gdb-threads-header)
2735 (set (make-local-variable 'font-lock-defaults)
2736 '(gdb-threads-font-lock-keywords))
2737 'gdb-invalidate-threads)
2739 (defun gdb-thread-list-handler-custom ()
2740 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2741 (table (make-gdb-table))
2743 (setq gdb-threads-list nil)
2744 (setq gdb-running-threads-count 0)
2745 (setq gdb-stopped-threads-count 0)
2746 (set-marker gdb-thread-position nil)
2748 (dolist (thread (reverse threads-list))
2749 (let ((running (equal (bindat-get-field thread 'state) "running")))
2750 (add-to-list 'gdb-threads-list
2751 (cons (bindat-get-field thread 'id)
2754 (incf gdb-running-threads-count)
2755 (incf gdb-stopped-threads-count))
2757 (gdb-table-add-row table
2759 (bindat-get-field thread 'id)
2761 (if gdb-thread-buffer-verbose-names
2762 (concat (bindat-get-field thread 'target-id) " ") "")
2763 (bindat-get-field thread 'state)
2764 ;; Include frame information for stopped threads
2767 " in " (bindat-get-field thread 'frame 'func)
2768 (if gdb-thread-buffer-arguments
2771 (let ((args (bindat-get-field thread 'frame 'args)))
2774 (apply #'format "%s=%s"
2775 (gdb-get-many-fields arg 'name 'value)))
2779 (if gdb-thread-buffer-locations
2780 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2781 (if gdb-thread-buffer-addresses
2782 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2786 'mouse-face 'highlight
2787 'help-echo "mouse-2, RET: select thread")))
2788 (when (string-equal gdb-thread-number
2789 (bindat-get-field thread 'id))
2790 (setq marked-line (length gdb-threads-list))))
2791 (insert (gdb-table-string table " "))
2793 (gdb-mark-line marked-line gdb-thread-position)))
2794 ;; We update gud-running here because we need to make sure that
2795 ;; gdb-threads-list is up-to-date
2796 (gdb-update-gud-running)
2797 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2799 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2800 "Define a NAME command which will act upon thread on the current line.
2802 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2803 be the value of 'gdb-thread property of the current line. If
2804 'gdb-thread is nil, error is signaled."
2805 `(defun ,name (&optional event)
2807 (interactive (list last-input-event))
2808 (if event (posn-set-point (event-end event)))
2811 (let ((thread (get-text-property (point) 'gdb-thread)))
2814 (error "Not recognized as thread line"))))))
2816 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2818 "Define a NAME which will call BUFFER-COMMAND with id of thread
2819 on the current line."
2820 `(def-gdb-thread-buffer-command ,name
2821 (,buffer-command (bindat-get-field thread 'id))
2824 (def-gdb-thread-buffer-command gdb-select-thread
2825 (let ((new-id (bindat-get-field thread 'id)))
2826 (gdb-setq-thread-number new-id)
2827 (gdb-input (concat "-thread-select " new-id) 'ignore)
2829 "Select the thread at current line of threads buffer.")
2831 (def-gdb-thread-buffer-simple-command
2832 gdb-display-stack-for-thread
2833 gdb-preemptively-display-stack-buffer
2834 "Display stack buffer for the thread at current line.")
2836 (def-gdb-thread-buffer-simple-command
2837 gdb-display-locals-for-thread
2838 gdb-preemptively-display-locals-buffer
2839 "Display locals buffer for the thread at current line.")
2841 (def-gdb-thread-buffer-simple-command
2842 gdb-display-registers-for-thread
2843 gdb-preemptively-display-registers-buffer
2844 "Display registers buffer for the thread at current line.")
2846 (def-gdb-thread-buffer-simple-command
2847 gdb-display-disassembly-for-thread
2848 gdb-preemptively-display-disassembly-buffer
2849 "Display disassembly buffer for the thread at current line.")
2851 (def-gdb-thread-buffer-simple-command
2852 gdb-frame-stack-for-thread
2853 gdb-frame-stack-buffer
2854 "Display a new frame with stack buffer for the thread at
2857 (def-gdb-thread-buffer-simple-command
2858 gdb-frame-locals-for-thread
2859 gdb-frame-locals-buffer
2860 "Display a new frame with locals buffer for the thread at
2863 (def-gdb-thread-buffer-simple-command
2864 gdb-frame-registers-for-thread
2865 gdb-frame-registers-buffer
2866 "Display a new frame with registers buffer for the thread at
2869 (def-gdb-thread-buffer-simple-command
2870 gdb-frame-disassembly-for-thread
2871 gdb-frame-disassembly-buffer
2872 "Display a new frame with disassembly buffer for the thread at
2875 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2876 "Define a NAME which will execute GUD-COMMAND with
2877 `gdb-thread-number' locally bound to id of thread on the current
2879 `(def-gdb-thread-buffer-command ,name
2881 (let ((gdb-thread-number (bindat-get-field thread 'id))
2882 (gdb-gud-control-all-threads nil))
2883 (call-interactively #',gud-command))
2884 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2887 (def-gdb-thread-buffer-gud-command
2888 gdb-interrupt-thread
2890 "Interrupt thread at current line.")
2892 (def-gdb-thread-buffer-gud-command
2895 "Continue thread at current line.")
2897 (def-gdb-thread-buffer-gud-command
2900 "Step thread at current line.")
2905 (defcustom gdb-memory-rows 8
2906 "Number of data rows in memory window."
2911 (defcustom gdb-memory-columns 4
2912 "Number of data columns in memory window."
2917 (defcustom gdb-memory-format "x"
2918 "Display format of data items in memory window."
2919 :type '(choice (const :tag "Hexadecimal" "x")
2920 (const :tag "Signed decimal" "d")
2921 (const :tag "Unsigned decimal" "u")
2922 (const :tag "Octal" "o")
2923 (const :tag "Binary" "t"))
2927 (defcustom gdb-memory-unit 4
2928 "Unit size of data items in memory window."
2929 :type '(choice (const :tag "Byte" 1)
2930 (const :tag "Halfword" 2)
2931 (const :tag "Word" 4)
2932 (const :tag "Giant word" 8))
2936 (def-gdb-trigger-and-handler
2937 gdb-invalidate-memory
2938 (format "-data-read-memory %s %s %d %d %d"
2944 gdb-read-memory-handler
2945 gdb-read-memory-custom
2948 (gdb-set-buffer-rules
2950 'gdb-memory-buffer-name
2952 'gdb-invalidate-memory)
2954 (defun gdb-memory-column-width (size format)
2955 "Return length of string with memory unit of SIZE in FORMAT.
2957 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2958 in `gdb-memory-format'."
2959 (let ((format-base (cdr (assoc format
2961 ("d" . 10) ("u" . 10)
2965 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2966 (cond ((string-equal format "x")
2967 (+ 2 res)) ; hexadecimal numbers have 0x in front
2968 ((or (string-equal format "d")
2969 (string-equal format "o"))
2972 (error "Unknown format"))))
2974 (defun gdb-read-memory-custom ()
2975 (let* ((res (gdb-json-partial-output))
2976 (err-msg (bindat-get-field res 'msg)))
2978 (let ((memory (bindat-get-field res 'memory)))
2979 (setq gdb-memory-address (bindat-get-field res 'addr))
2980 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2981 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2982 (setq gdb-memory-last-address gdb-memory-address)
2983 (dolist (row memory)
2984 (insert (concat (bindat-get-field row 'addr) ":"))
2985 (dolist (column (bindat-get-field row 'data))
2986 (insert (gdb-pad-string column
2987 (+ 2 (gdb-memory-column-width
2989 gdb-memory-format)))))
2991 ;; Show last page instead of empty buffer when out of bounds
2993 (let ((gdb-memory-address gdb-memory-last-address))
2994 (gdb-invalidate-memory 'update)
2995 (error err-msg))))))
2997 (defvar gdb-memory-mode-map
2998 (let ((map (make-sparse-keymap)))
2999 (suppress-keymap map t)
3000 (define-key map "q" 'kill-this-buffer)
3001 (define-key map "n" 'gdb-memory-show-next-page)
3002 (define-key map "p" 'gdb-memory-show-previous-page)
3003 (define-key map "a" 'gdb-memory-set-address)
3004 (define-key map "t" 'gdb-memory-format-binary)
3005 (define-key map "o" 'gdb-memory-format-octal)
3006 (define-key map "u" 'gdb-memory-format-unsigned)
3007 (define-key map "d" 'gdb-memory-format-signed)
3008 (define-key map "x" 'gdb-memory-format-hexadecimal)
3009 (define-key map "b" 'gdb-memory-unit-byte)
3010 (define-key map "h" 'gdb-memory-unit-halfword)
3011 (define-key map "w" 'gdb-memory-unit-word)
3012 (define-key map "g" 'gdb-memory-unit-giant)
3013 (define-key map "R" 'gdb-memory-set-rows)
3014 (define-key map "C" 'gdb-memory-set-columns)
3017 (defun gdb-memory-set-address-event (event)
3018 "Handle a click on address field in memory buffer header."
3020 (save-selected-window
3021 (select-window (posn-window (event-start event)))
3022 (gdb-memory-set-address)))
3024 ;; Non-event version for use within keymap
3025 (defun gdb-memory-set-address ()
3026 "Set the start memory address."
3028 (let ((arg (read-from-minibuffer "Memory address: ")))
3029 (setq gdb-memory-address arg))
3030 (gdb-invalidate-memory 'update))
3032 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3033 "Define a function NAME which reads new VAR value from minibuffer."
3034 `(defun ,name (event)
3037 (save-selected-window
3038 (select-window (posn-window (event-start event)))
3039 (let* ((arg (read-from-minibuffer ,echo-string))
3040 (count (string-to-number arg)))
3042 (error "Positive number only")
3043 (customize-set-variable ',variable count)
3044 (gdb-invalidate-memory 'update))))))
3046 (def-gdb-set-positive-number
3050 "Set the number of data rows in memory window.")
3052 (def-gdb-set-positive-number
3053 gdb-memory-set-columns
3056 "Set the number of data columns in memory window.")
3058 (defmacro def-gdb-memory-format (name format doc)
3059 "Define a function NAME to switch memory buffer to use FORMAT.
3061 DOC is an optional documentation string."
3062 `(defun ,name () ,(when doc doc)
3064 (customize-set-variable 'gdb-memory-format ,format)
3065 (gdb-invalidate-memory 'update)))
3067 (def-gdb-memory-format
3068 gdb-memory-format-binary "t"
3069 "Set the display format to binary.")
3071 (def-gdb-memory-format
3072 gdb-memory-format-octal "o"
3073 "Set the display format to octal.")
3075 (def-gdb-memory-format
3076 gdb-memory-format-unsigned "u"
3077 "Set the display format to unsigned decimal.")
3079 (def-gdb-memory-format
3080 gdb-memory-format-signed "d"
3081 "Set the display format to decimal.")
3083 (def-gdb-memory-format
3084 gdb-memory-format-hexadecimal "x"
3085 "Set the display format to hexadecimal.")
3087 (defvar gdb-memory-format-map
3088 (let ((map (make-sparse-keymap)))
3089 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3091 "Keymap to select format in the header line.")
3093 (defvar gdb-memory-format-menu
3094 (let ((map (make-sparse-keymap "Format")))
3096 (define-key map [binary]
3097 '(menu-item "Binary" gdb-memory-format-binary
3098 :button (:radio . (equal gdb-memory-format "t"))))
3099 (define-key map [octal]
3100 '(menu-item "Octal" gdb-memory-format-octal
3101 :button (:radio . (equal gdb-memory-format "o"))))
3102 (define-key map [unsigned]
3103 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3104 :button (:radio . (equal gdb-memory-format "u"))))
3105 (define-key map [signed]
3106 '(menu-item "Signed Decimal" gdb-memory-format-signed
3107 :button (:radio . (equal gdb-memory-format "d"))))
3108 (define-key map [hexadecimal]
3109 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3110 :button (:radio . (equal gdb-memory-format "x"))))
3112 "Menu of display formats in the header line.")
3114 (defun gdb-memory-format-menu (event)
3116 (x-popup-menu event gdb-memory-format-menu))
3118 (defun gdb-memory-format-menu-1 (event)
3120 (save-selected-window
3121 (select-window (posn-window (event-start event)))
3122 (let* ((selection (gdb-memory-format-menu event))
3123 (binding (and selection (lookup-key gdb-memory-format-menu
3124 (vector (car selection))))))
3125 (if binding (call-interactively binding)))))
3127 (defmacro def-gdb-memory-unit (name unit-size doc)
3128 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3130 DOC is an optional documentation string."
3131 `(defun ,name () ,(when doc doc)
3133 (customize-set-variable 'gdb-memory-unit ,unit-size)
3134 (gdb-invalidate-memory 'update)))
3136 (def-gdb-memory-unit gdb-memory-unit-giant 8
3137 "Set the unit size to giant words (eight bytes).")
3139 (def-gdb-memory-unit gdb-memory-unit-word 4
3140 "Set the unit size to words (four bytes).")
3142 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3143 "Set the unit size to halfwords (two bytes).")
3145 (def-gdb-memory-unit gdb-memory-unit-byte 1
3146 "Set the unit size to bytes.")
3148 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3149 "Define a function NAME which show new address in memory buffer.
3151 The defined function switches Memory buffer to show address
3152 stored in ADDRESS-VAR variable.
3154 DOC is an optional documentation string."
3158 (let ((gdb-memory-address ,address-var))
3159 (gdb-invalidate-memory))))
3161 (def-gdb-memory-show-page gdb-memory-show-previous-page
3162 gdb-memory-prev-page)
3164 (def-gdb-memory-show-page gdb-memory-show-next-page
3165 gdb-memory-next-page)
3167 (defvar gdb-memory-unit-map
3168 (let ((map (make-sparse-keymap)))
3169 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3171 "Keymap to select units in the header line.")
3173 (defvar gdb-memory-unit-menu
3174 (let ((map (make-sparse-keymap "Unit")))
3175 (define-key map [giantwords]
3176 '(menu-item "Giant words" gdb-memory-unit-giant
3177 :button (:radio . (equal gdb-memory-unit 8))))
3178 (define-key map [words]
3179 '(menu-item "Words" gdb-memory-unit-word
3180 :button (:radio . (equal gdb-memory-unit 4))))
3181 (define-key map [halfwords]
3182 '(menu-item "Halfwords" gdb-memory-unit-halfword
3183 :button (:radio . (equal gdb-memory-unit 2))))
3184 (define-key map [bytes]
3185 '(menu-item "Bytes" gdb-memory-unit-byte
3186 :button (:radio . (equal gdb-memory-unit 1))))
3188 "Menu of units in the header line.")
3190 (defun gdb-memory-unit-menu (event)
3192 (x-popup-menu event gdb-memory-unit-menu))
3194 (defun gdb-memory-unit-menu-1 (event)
3196 (save-selected-window
3197 (select-window (posn-window (event-start event)))
3198 (let* ((selection (gdb-memory-unit-menu event))
3199 (binding (and selection (lookup-key gdb-memory-unit-menu
3200 (vector (car selection))))))
3201 (if binding (call-interactively binding)))))
3203 (defvar gdb-memory-font-lock-keywords
3204 '(;; <__function.name+n>
3205 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3206 (1 font-lock-function-name-face)))
3207 "Font lock keywords used in `gdb-memory-mode'.")
3209 (defvar gdb-memory-header
3214 'face font-lock-warning-face
3215 'help-echo "mouse-1: decrement address"
3216 'mouse-face 'mode-line-highlight
3217 'local-map (gdb-make-header-line-mouse-map
3219 #'gdb-memory-show-previous-page))
3222 'face font-lock-warning-face
3223 'help-echo "mouse-1: increment address"
3224 'mouse-face 'mode-line-highlight
3225 'local-map (gdb-make-header-line-mouse-map
3227 #'gdb-memory-show-next-page))
3229 (propertize gdb-memory-address
3230 'face font-lock-warning-face
3231 'help-echo "mouse-1: set start address"
3232 'mouse-face 'mode-line-highlight
3233 'local-map (gdb-make-header-line-mouse-map
3235 #'gdb-memory-set-address-event))
3237 (propertize (number-to-string gdb-memory-rows)
3238 'face font-lock-warning-face
3239 'help-echo "mouse-1: set number of columns"
3240 'mouse-face 'mode-line-highlight
3241 'local-map (gdb-make-header-line-mouse-map
3243 #'gdb-memory-set-rows))
3245 (propertize (number-to-string gdb-memory-columns)
3246 'face font-lock-warning-face
3247 'help-echo "mouse-1: set number of columns"
3248 'mouse-face 'mode-line-highlight
3249 'local-map (gdb-make-header-line-mouse-map
3251 #'gdb-memory-set-columns))
3253 (propertize gdb-memory-format
3254 'face font-lock-warning-face
3255 'help-echo "mouse-3: select display format"
3256 'mouse-face 'mode-line-highlight
3257 'local-map gdb-memory-format-map)
3259 (propertize (number-to-string gdb-memory-unit)
3260 'face font-lock-warning-face
3261 'help-echo "mouse-3: select unit size"
3262 'mouse-face 'mode-line-highlight
3263 'local-map gdb-memory-unit-map)))
3264 "Header line used in `gdb-memory-mode'.")
3266 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3267 "Major mode for examining memory."
3268 (setq header-line-format gdb-memory-header)
3269 (set (make-local-variable 'font-lock-defaults)
3270 '(gdb-memory-font-lock-keywords))
3271 'gdb-invalidate-memory)
3273 (defun gdb-memory-buffer-name ()
3274 (concat "*memory of " (gdb-get-target-string) "*"))
3276 (def-gdb-display-buffer
3277 gdb-display-memory-buffer
3279 "Display memory contents.")
3281 (defun gdb-frame-memory-buffer ()
3282 "Display memory contents in a new frame."
3284 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3285 (special-display-frame-alist
3289 ,@gdb-frame-parameters)))
3290 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3293 ;;; Disassembly view
3295 (defun gdb-disassembly-buffer-name ()
3296 (gdb-current-context-buffer-name
3297 (concat "disassembly of " (gdb-get-target-string))))
3299 (def-gdb-display-buffer
3300 gdb-display-disassembly-buffer
3301 'gdb-disassembly-buffer
3302 "Display disassembly for current stack frame.")
3304 (def-gdb-preempt-display-buffer
3305 gdb-preemptively-display-disassembly-buffer
3306 'gdb-disassembly-buffer)
3308 (def-gdb-frame-for-buffer
3309 gdb-frame-disassembly-buffer
3310 'gdb-disassembly-buffer
3311 "Display disassembly in a new frame.")
3313 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3314 (let* ((frame (gdb-current-buffer-frame))
3315 (file (bindat-get-field frame 'fullname))
3316 (line (bindat-get-field frame 'line)))
3318 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3319 ;; If we're unable to get a file name / line for $PC, simply
3320 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3321 ;; 150 bytes) instructions.
3322 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3323 gdb-disassembly-handler
3324 ;; We update disassembly only after we have actual frame information
3325 ;; about all threads, so no there's `update' signal in this list
3326 '(start update-disassembly))
3328 (def-gdb-auto-update-handler
3329 gdb-disassembly-handler
3330 gdb-invalidate-disassembly
3331 gdb-disassembly-handler-custom
3334 (gdb-set-buffer-rules
3335 'gdb-disassembly-buffer
3336 'gdb-disassembly-buffer-name
3337 'gdb-disassembly-mode
3338 'gdb-invalidate-disassembly)
3340 (defvar gdb-disassembly-font-lock-keywords
3341 '(;; <__function.name+n>
3342 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3343 (1 font-lock-function-name-face))
3344 ;; 0xNNNNNNNN <__function.name+n>: opcode
3345 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3346 (4 font-lock-keyword-face))
3347 ;; %register(at least i386)
3348 ("%\\sw+" . font-lock-variable-name-face)
3349 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3350 (1 font-lock-comment-face)
3351 (2 font-lock-function-name-face))
3352 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3353 "Font lock keywords used in `gdb-disassembly-mode'.")
3355 (defvar gdb-disassembly-mode-map
3357 (let ((map (make-sparse-keymap)))
3358 (suppress-keymap map)
3359 (define-key map "q" 'kill-this-buffer)
3362 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3363 "Major mode for GDB disassembly information."
3364 ;; TODO Rename overlay variable for disassembly mode
3365 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3366 (setq fringes-outside-margins t)
3367 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3368 (set (make-local-variable 'font-lock-defaults)
3369 '(gdb-disassembly-font-lock-keywords))
3370 'gdb-invalidate-disassembly)
3372 (defun gdb-disassembly-handler-custom ()
3373 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3374 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3375 (table (make-gdb-table))
3377 (dolist (instr instructions)
3378 (gdb-table-add-row table
3380 (bindat-get-field instr 'address)
3382 ((func-name (bindat-get-field instr 'func-name))
3383 (offset (bindat-get-field instr 'offset)))
3385 (format "<%s+%s>:" func-name offset)
3387 (bindat-get-field instr 'inst)))
3388 (when (string-equal (bindat-get-field instr 'address)
3391 (setq marked-line (length (gdb-table-rows table)))
3392 (setq fringe-indicator-alist
3393 (if (string-equal gdb-frame-number "0")
3395 '((overlay-arrow . hollow-right-triangle)))))))
3396 (insert (gdb-table-string table " "))
3397 (gdb-disassembly-place-breakpoints)
3398 ;; Mark current position with overlay arrow and scroll window to
3401 (let ((window (get-buffer-window (current-buffer) 0)))
3402 (set-window-point window (gdb-mark-line marked-line
3403 gdb-disassembly-position))))
3405 (gdb-current-context-mode-name
3406 (concat "Disassembly: "
3407 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3409 (defun gdb-disassembly-place-breakpoints ()
3410 (gdb-remove-breakpoint-icons (point-min) (point-max))
3411 (dolist (breakpoint gdb-breakpoints-list)
3412 (let* ((breakpoint (cdr breakpoint))
3413 (bptno (bindat-get-field breakpoint 'number))
3414 (flag (bindat-get-field breakpoint 'enabled))
3415 (address (bindat-get-field breakpoint 'addr)))
3417 (goto-char (point-min))
3418 (if (re-search-forward (concat "^" address) nil t)
3419 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3422 (defvar gdb-breakpoints-header
3424 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3427 (gdb-propertize-header "Threads" gdb-threads-buffer
3428 "mouse-1: select" mode-line-highlight
3429 mode-line-inactive)))
3431 ;;; Breakpoints view
3432 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3433 "Major mode for gdb breakpoints."
3434 (setq header-line-format gdb-breakpoints-header)
3435 'gdb-invalidate-breakpoints)
3437 (defun gdb-toggle-breakpoint ()
3438 "Enable/disable breakpoint at current line of breakpoints buffer."
3442 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3445 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3448 (bindat-get-field breakpoint 'number)))
3449 (error "Not recognized as break/watchpoint line")))))
3451 (defun gdb-delete-breakpoint ()
3452 "Delete the breakpoint at current line of breakpoints buffer."
3456 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3458 (gud-basic-call (concat "-break-delete "
3459 (bindat-get-field breakpoint 'number)))
3460 (error "Not recognized as break/watchpoint line")))))
3462 (defun gdb-goto-breakpoint (&optional event)
3463 "Go to the location of breakpoint at current line of
3464 breakpoints buffer."
3465 (interactive (list last-input-event))
3466 (if event (posn-set-point (event-end event)))
3467 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3468 (let ((window (get-buffer-window gud-comint-buffer)))
3469 (if window (save-selected-window (select-window window))))
3472 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3474 (let ((bptno (bindat-get-field breakpoint 'number))
3475 (file (bindat-get-field breakpoint 'fullname))
3476 (line (bindat-get-field breakpoint 'line)))
3477 (save-selected-window
3478 (let* ((buffer (find-file-noselect
3479 (if (file-exists-p file) file
3480 (cdr (assoc bptno gdb-location-alist)))))
3481 (window (or (gdb-display-source-buffer buffer)
3482 (display-buffer buffer))))
3483 (setq gdb-source-window window)
3484 (with-current-buffer buffer
3485 (goto-char (point-min))
3486 (forward-line (1- (string-to-number line)))
3487 (set-window-point window (point))))))
3488 (error "Not recognized as break/watchpoint line")))))
3491 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3493 (def-gdb-trigger-and-handler
3494 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3495 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3498 (gdb-set-buffer-rules
3500 'gdb-stack-buffer-name
3502 'gdb-invalidate-frames)
3504 (defun gdb-frame-location (frame)
3505 "Return \" of file:line\" or \" of library\" for structure FRAME.
3507 FRAME must have either \"file\" and \"line\" members or \"from\"
3509 (let ((file (bindat-get-field frame 'file))
3510 (line (bindat-get-field frame 'line))
3511 (from (bindat-get-field frame 'from)))
3512 (let ((res (or (and file line (concat file ":" line))
3514 (if res (concat " of " res) ""))))
3516 (defun gdb-stack-list-frames-custom ()
3517 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3518 (table (make-gdb-table)))
3519 (set-marker gdb-stack-position nil)
3520 (dolist (frame stack)
3521 (gdb-table-add-row table
3523 (bindat-get-field frame 'level)
3526 (bindat-get-field frame 'func)
3527 (if gdb-stack-buffer-locations
3528 (gdb-frame-location frame) "")
3529 (if gdb-stack-buffer-addresses
3530 (concat " at " (bindat-get-field frame 'addr)) "")))
3531 `(mouse-face highlight
3532 help-echo "mouse-2, RET: Select frame"
3534 (insert (gdb-table-string table " ")))
3535 (when (and gdb-frame-number
3536 (gdb-buffer-shows-main-thread-p))
3537 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3538 gdb-stack-position))
3540 (gdb-current-context-mode-name "Frames")))
3542 (defun gdb-stack-buffer-name ()
3543 (gdb-current-context-buffer-name
3544 (concat "stack frames of " (gdb-get-target-string))))
3546 (def-gdb-display-buffer
3547 gdb-display-stack-buffer
3549 "Display backtrace of current stack.")
3551 (def-gdb-preempt-display-buffer
3552 gdb-preemptively-display-stack-buffer
3553 'gdb-stack-buffer nil t)
3555 (def-gdb-frame-for-buffer
3556 gdb-frame-stack-buffer
3558 "Display backtrace of current stack in a new frame.")
3560 (defvar gdb-frames-mode-map
3561 (let ((map (make-sparse-keymap)))
3562 (suppress-keymap map)
3563 (define-key map "q" 'kill-this-buffer)
3564 (define-key map "\r" 'gdb-select-frame)
3565 (define-key map [mouse-2] 'gdb-select-frame)
3566 (define-key map [follow-link] 'mouse-face)
3569 (defvar gdb-frames-font-lock-keywords
3570 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3571 "Font lock keywords used in `gdb-frames-mode'.")
3573 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3574 "Major mode for gdb call stack."
3575 (setq gdb-stack-position (make-marker))
3576 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3577 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3578 (set (make-local-variable 'font-lock-defaults)
3579 '(gdb-frames-font-lock-keywords))
3580 'gdb-invalidate-frames)
3582 (defun gdb-select-frame (&optional event)
3583 "Select the frame and display the relevant source."
3584 (interactive (list last-input-event))
3585 (if event (posn-set-point (event-end event)))
3586 (let ((frame (get-text-property (point) 'gdb-frame)))
3588 (if (gdb-buffer-shows-main-thread-p)
3589 (let ((new-level (bindat-get-field frame 'level)))
3590 (setq gdb-frame-number new-level)
3591 (gdb-input (concat "-stack-select-frame " new-level)
3594 (error "Could not select frame for non-current thread"))
3595 (error "Not recognized as frame line"))))
3599 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3600 (def-gdb-trigger-and-handler
3601 gdb-invalidate-locals
3602 (concat (gdb-current-context-command "-stack-list-locals")
3604 gdb-locals-handler gdb-locals-handler-custom
3607 (gdb-set-buffer-rules
3609 'gdb-locals-buffer-name
3611 'gdb-invalidate-locals)
3613 (defvar gdb-locals-watch-map
3614 (let ((map (make-sparse-keymap)))
3615 (suppress-keymap map)
3616 (define-key map "\r" 'gud-watch)
3617 (define-key map [mouse-2] 'gud-watch)
3619 "Keymap to create watch expression of a complex data type local variable.")
3621 (defvar gdb-edit-locals-map-1
3622 (let ((map (make-sparse-keymap)))
3623 (suppress-keymap map)
3624 (define-key map "\r" 'gdb-edit-locals-value)
3625 (define-key map [mouse-2] 'gdb-edit-locals-value)
3627 "Keymap to edit value of a simple data type local variable.")
3629 (defun gdb-edit-locals-value (&optional event)
3630 "Assign a value to a variable displayed in the locals buffer."
3631 (interactive (list last-input-event))
3633 (if event (posn-set-point (event-end event)))
3635 (let* ((var (bindat-get-field
3636 (get-text-property (point) 'gdb-local-variable) 'name))
3637 (value (read-string (format "New value (%s): " var))))
3639 (concat "-gdb-set variable " var " = " value)))))
3641 ;; Don't display values of arrays or structures.
3642 ;; These can be expanded using gud-watch.
3643 (defun gdb-locals-handler-custom ()
3644 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3645 (table (make-gdb-table)))
3646 (dolist (local locals-list)
3647 (let ((name (bindat-get-field local 'name))
3648 (value (bindat-get-field local 'value))
3649 (type (bindat-get-field local 'type)))
3651 (string-match "\\0x" value))
3652 (add-text-properties 0 (length name)
3653 `(mouse-face highlight
3654 help-echo "mouse-2: create watch expression"
3655 local-map ,gdb-locals-watch-map)
3657 (add-text-properties 0 (length value)
3658 `(mouse-face highlight
3659 help-echo "mouse-2: edit value"
3660 local-map ,gdb-edit-locals-map-1)
3665 (propertize type 'font-lock-face font-lock-type-face)
3666 (propertize name 'font-lock-face font-lock-variable-name-face)
3668 `(gdb-local-variable ,local))))
3669 (insert (gdb-table-string table " "))
3671 (gdb-current-context-mode-name
3673 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3675 (defvar gdb-locals-header
3677 (gdb-propertize-header "Locals" gdb-locals-buffer
3680 (gdb-propertize-header "Registers" gdb-registers-buffer
3681 "mouse-1: select" mode-line-highlight
3682 mode-line-inactive)))
3684 (defvar gdb-locals-mode-map
3685 (let ((map (make-sparse-keymap)))
3686 (suppress-keymap map)
3687 (define-key map "q" 'kill-this-buffer)
3688 (define-key map "\t" (lambda ()
3690 (gdb-set-window-buffer
3691 (gdb-get-buffer-create
3692 'gdb-registers-buffer
3693 gdb-thread-number) t)))
3696 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3697 "Major mode for gdb locals."
3698 (setq header-line-format gdb-locals-header)
3699 'gdb-invalidate-locals)
3701 (defun gdb-locals-buffer-name ()
3702 (gdb-current-context-buffer-name
3703 (concat "locals of " (gdb-get-target-string))))
3705 (def-gdb-display-buffer
3706 gdb-display-locals-buffer
3708 "Display local variables of current stack and their values.")
3710 (def-gdb-preempt-display-buffer
3711 gdb-preemptively-display-locals-buffer
3712 'gdb-locals-buffer nil t)
3714 (def-gdb-frame-for-buffer
3715 gdb-frame-locals-buffer
3717 "Display local variables of current stack and their values in a new frame.")
3720 ;; Registers buffer.
3722 (def-gdb-trigger-and-handler
3723 gdb-invalidate-registers
3724 (concat (gdb-current-context-command "-data-list-register-values") " x")
3725 gdb-registers-handler
3726 gdb-registers-handler-custom
3729 (gdb-set-buffer-rules
3730 'gdb-registers-buffer
3731 'gdb-registers-buffer-name
3733 'gdb-invalidate-registers)
3735 (defun gdb-registers-handler-custom ()
3736 (when gdb-register-names
3737 (let ((register-values
3738 (bindat-get-field (gdb-json-partial-output) 'register-values))
3739 (table (make-gdb-table)))
3740 (dolist (register register-values)
3741 (let* ((register-number (bindat-get-field register 'number))
3742 (value (bindat-get-field register 'value))
3743 (register-name (nth (string-to-number register-number)
3744 gdb-register-names)))
3748 (propertize register-name
3749 'font-lock-face font-lock-variable-name-face)
3750 (if (member register-number gdb-changed-registers)
3751 (propertize value 'font-lock-face font-lock-warning-face)
3753 `(mouse-face highlight
3754 help-echo "mouse-2: edit value"
3755 gdb-register-name ,register-name))))
3756 (insert (gdb-table-string table " ")))
3758 (gdb-current-context-mode-name "Registers"))))
3760 (defun gdb-edit-register-value (&optional event)
3761 "Assign a value to a register displayed in the registers buffer."
3762 (interactive (list last-input-event))
3764 (if event (posn-set-point (event-end event)))
3766 (let* ((var (bindat-get-field
3767 (get-text-property (point) 'gdb-register-name)))
3768 (value (read-string (format "New value (%s): " var))))
3770 (concat "-gdb-set variable $" var " = " value)))))
3772 (defvar gdb-registers-mode-map
3773 (let ((map (make-sparse-keymap)))
3774 (suppress-keymap map)
3775 (define-key map "\r" 'gdb-edit-register-value)
3776 (define-key map [mouse-2] 'gdb-edit-register-value)
3777 (define-key map "q" 'kill-this-buffer)
3778 (define-key map "\t" (lambda ()
3780 (gdb-set-window-buffer
3781 (gdb-get-buffer-create
3783 gdb-thread-number) t)))
3786 (defvar gdb-registers-header
3788 (gdb-propertize-header "Locals" gdb-locals-buffer
3789 "mouse-1: select" mode-line-highlight
3792 (gdb-propertize-header "Registers" gdb-registers-buffer
3793 nil nil mode-line)))
3795 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3796 "Major mode for gdb registers."
3797 (setq header-line-format gdb-registers-header)
3798 'gdb-invalidate-registers)
3800 (defun gdb-registers-buffer-name ()
3801 (gdb-current-context-buffer-name
3802 (concat "registers of " (gdb-get-target-string))))
3804 (def-gdb-display-buffer
3805 gdb-display-registers-buffer
3806 'gdb-registers-buffer
3807 "Display integer register contents.")
3809 (def-gdb-preempt-display-buffer
3810 gdb-preemptively-display-registers-buffer
3811 'gdb-registers-buffer nil t)
3813 (def-gdb-frame-for-buffer
3814 gdb-frame-registers-buffer
3815 'gdb-registers-buffer
3816 "Display integer register contents in a new frame.")
3818 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3819 (defun gdb-get-changed-registers ()
3820 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3821 (not (gdb-pending-p 'gdb-get-changed-registers)))
3822 (gdb-input "-data-list-changed-registers"
3823 'gdb-changed-registers-handler)
3824 (gdb-add-pending 'gdb-get-changed-registers)))
3826 (defun gdb-changed-registers-handler ()
3827 (gdb-delete-pending 'gdb-get-changed-registers)
3828 (setq gdb-changed-registers nil)
3829 (dolist (register-number
3830 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3831 (push register-number gdb-changed-registers)))
3833 (defun gdb-register-names-handler ()
3834 ;; Don't use gdb-pending-triggers because this handler is called
3835 ;; only once (in gdb-init-1)
3836 (setq gdb-register-names nil)
3837 (dolist (register-name
3838 (bindat-get-field (gdb-json-partial-output) 'register-names))
3839 (push register-name gdb-register-names))
3840 (setq gdb-register-names (reverse gdb-register-names)))
3843 (defun gdb-get-source-file-list ()
3844 "Create list of source files for current GDB session.
3845 If buffers already exist for any of these files, gud-minor-mode
3847 (goto-char (point-min))
3848 (while (re-search-forward gdb-source-file-regexp nil t)
3849 (push (match-string 1) gdb-source-file-list))
3850 (dolist (buffer (buffer-list))
3851 (with-current-buffer buffer
3852 (when (member buffer-file-name gdb-source-file-list)
3853 (gdb-init-buffer)))))
3855 (defun gdb-get-main-selected-frame ()
3856 "Trigger for `gdb-frame-handler' which uses main current
3857 thread. Called from `gdb-update'."
3858 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3860 (gdb-input (gdb-current-context-command "-stack-info-frame")
3862 (gdb-add-pending 'gdb-get-main-selected-frame))))
3864 (defun gdb-frame-handler ()
3865 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3866 overlay arrow in source buffer."
3867 (gdb-delete-pending 'gdb-get-main-selected-frame)
3868 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3870 (setq gdb-selected-frame (bindat-get-field frame 'func))
3871 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3872 (setq gdb-frame-number (bindat-get-field frame 'level))
3873 (setq gdb-frame-address (bindat-get-field frame 'addr))
3874 (let ((line (bindat-get-field frame 'line)))
3875 (setq gdb-selected-line (and line (string-to-number line)))
3876 (when (and gdb-selected-file gdb-selected-line)
3877 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3878 (gud-display-frame)))
3879 (if gud-overlay-arrow-position
3880 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3881 (position (marker-position gud-overlay-arrow-position)))
3883 (with-current-buffer buffer
3884 (setq fringe-indicator-alist
3885 (if (string-equal gdb-frame-number "0")
3887 '((overlay-arrow . hollow-right-triangle))))
3888 (setq gud-overlay-arrow-position (make-marker))
3889 (set-marker gud-overlay-arrow-position position))))))))
3891 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3893 (defun gdb-get-prompt ()
3894 "Find prompt for GDB session."
3895 (goto-char (point-min))
3896 (setq gdb-prompt-name nil)
3897 (re-search-forward gdb-prompt-name-regexp nil t)
3898 (setq gdb-prompt-name (match-string 1))
3899 ;; Insert first prompt.
3900 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3902 ;;;; Window management
3903 (defun gdb-display-buffer (buf dedicated &optional frame)
3906 If BUF is already displayed in some window, show it, deiconifying
3907 the frame if necessary. Otherwise, find least recently used
3908 window and show BUF there, if the window is not used for GDB
3909 already, in which case that window is split first."
3910 (let ((answer (get-buffer-window buf (or frame 0))))
3912 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3913 (let ((window (get-lru-window)))
3914 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3916 (let ((largest (get-largest-window)))
3917 (setq answer (split-window largest))
3918 (set-window-buffer answer buf)
3919 (set-window-dedicated-p answer dedicated)
3921 (set-window-buffer window buf)
3924 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3925 "Find window displaying a buffer with the same
3926 `gdb-buffer-type' as BUF and show BUF there. If no such window
3927 exists, just call `gdb-display-buffer' for BUF. If the window
3928 found is already dedicated, split window according to
3929 SPLIT-HORIZONTAL and show BUF in the new window."
3931 (when (not (get-buffer-window buf))
3932 (let* ((buf-type (gdb-buffer-type buf))
3934 (get-window-with-predicate
3937 (gdb-buffer-type (window-buffer w)))
3938 (not (window-dedicated-p w)))))))
3940 (set-window-buffer existing-window buf)
3941 (let ((dedicated-window
3942 (get-window-with-predicate
3945 (gdb-buffer-type (window-buffer w)))))))
3946 (if dedicated-window
3948 (split-window dedicated-window nil split-horizontal) buf)
3949 (gdb-display-buffer buf t))))))
3950 (error "Null buffer")))
3952 ;;; Shared keymap initialization:
3954 (let ((menu (make-sparse-keymap "GDB-Windows")))
3955 (define-key gud-menu-map [displays]
3956 `(menu-item "GDB-Windows" ,menu
3957 :visible (eq gud-minor-mode 'gdbmi)))
3958 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3959 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3960 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3961 (define-key menu [disassembly]
3962 '("Disassembly" . gdb-display-disassembly-buffer))
3963 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3964 (define-key menu [inferior]
3965 '("IO" . gdb-display-io-buffer))
3966 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3967 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3968 (define-key menu [breakpoints]
3969 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3971 (let ((menu (make-sparse-keymap "GDB-Frames")))
3972 (define-key gud-menu-map [frames]
3973 `(menu-item "GDB-Frames" ,menu
3974 :visible (eq gud-minor-mode 'gdbmi)))
3975 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3976 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3977 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3978 (define-key menu [disassembly]
3979 '("Disassembly" . gdb-frame-disassembly-buffer))
3980 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3981 (define-key menu [inferior]
3982 '("IO" . gdb-frame-io-buffer))
3983 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3984 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3985 (define-key menu [breakpoints]
3986 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3988 (let ((menu (make-sparse-keymap "GDB-MI")))
3989 (define-key menu [gdb-customize]
3990 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3991 :help "Customize Gdb Graphical Mode options."))
3992 (define-key menu [gdb-many-windows]
3993 '(menu-item "Display Other Windows" gdb-many-windows
3994 :help "Toggle display of locals, stack and breakpoint information"
3995 :button (:toggle . gdb-many-windows)))
3996 (define-key menu [gdb-restore-windows]
3997 '(menu-item "Restore Window Layout" gdb-restore-windows
3998 :help "Restore standard layout for debug session."))
3999 (define-key menu [sep1]
4001 (define-key menu [all-threads]
4002 '(menu-item "GUD controls all threads"
4005 (setq gdb-gud-control-all-threads t))
4006 :help "GUD start/stop commands apply to all threads"
4007 :button (:radio . gdb-gud-control-all-threads)))
4008 (define-key menu [current-thread]
4009 '(menu-item "GUD controls current thread"
4012 (setq gdb-gud-control-all-threads nil))
4013 :help "GUD start/stop commands apply to current thread only"
4014 :button (:radio . (not gdb-gud-control-all-threads))))
4015 (define-key menu [sep2]
4017 (define-key menu [gdb-customize-reasons]
4018 '(menu-item "Customize switching..."
4021 (customize-option 'gdb-switch-reasons))))
4022 (define-key menu [gdb-switch-when-another-stopped]
4023 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4024 gdb-switch-when-another-stopped
4025 "Automatically switch to stopped thread"
4026 "GDB thread switching %s"
4027 "Switch to stopped thread"))
4028 (define-key gud-menu-map [mi]
4029 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4031 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4032 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4033 ;; tool-bar-local-item-from-menu here because it appends new buttons
4034 ;; to toolbar from right to left while we want our A/T throttle to
4035 ;; show up right before Run button.
4036 (define-key-after gud-tool-bar-map [all-threads]
4037 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4038 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4039 :visible (and (eq gud-minor-mode 'gdbmi)
4041 (not gdb-gud-control-all-threads)))
4044 (define-key-after gud-tool-bar-map [current-thread]
4045 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4046 :image (find-image '((:type xpm :file "gud/all.xpm")))
4047 :visible (and (eq gud-minor-mode 'gdbmi)
4049 gdb-gud-control-all-threads))
4052 (defun gdb-frame-gdb-buffer ()
4053 "Display GUD buffer in a new frame."
4055 (display-buffer-other-frame gud-comint-buffer))
4057 (defun gdb-display-gdb-buffer ()
4058 "Display GUD buffer."
4060 (pop-to-buffer gud-comint-buffer nil 0))
4062 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4063 "Set buffer of selected window to NAME and dedicate window.
4065 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4066 window is dedicated."
4067 (unless window (setq window (selected-window)))
4068 (when ignore-dedicated
4069 (set-window-dedicated-p window nil))
4070 (set-window-buffer window (get-buffer name))
4071 (set-window-dedicated-p window t))
4073 (defun gdb-setup-windows ()
4074 "Layout the window pattern for `gdb-many-windows'."
4075 (gdb-display-locals-buffer)
4076 (gdb-display-stack-buffer)
4077 (delete-other-windows)
4078 (gdb-display-breakpoints-buffer)
4079 (delete-other-windows)
4081 (switch-to-buffer gud-comint-buffer)
4082 (let ((win0 (selected-window))
4083 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4084 (win2 (split-window nil ( / (window-height) 3)))
4085 (win3 (split-window-right)))
4086 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4087 (select-window win2)
4090 (if gud-last-last-frame
4091 (gud-find-file (car gud-last-last-frame))
4093 (gud-find-file gdb-main-file)
4094 ;; Put buffer list in window if we
4095 ;; can't find a source file.
4096 (list-buffers-noselect))))
4097 (setq gdb-source-window (selected-window))
4098 (let ((win4 (split-window-right)))
4099 (gdb-set-window-buffer
4100 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4101 (select-window win1)
4102 (gdb-set-window-buffer (gdb-stack-buffer-name))
4103 (let ((win5 (split-window-right)))
4104 (gdb-set-window-buffer (if gdb-show-threads-by-default
4105 (gdb-threads-buffer-name)
4106 (gdb-breakpoints-buffer-name))
4108 (select-window win0)))
4110 (defcustom gdb-many-windows nil
4111 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4112 In this case it starts with two windows: one displaying the GUD
4113 buffer and the other with the source file with the main routine
4114 of the debugged program. Non-nil means display the layout shown for
4120 (defun gdb-many-windows (arg)
4121 "Toggle the number of windows in the basic arrangement.
4122 With arg, display additional buffers iff arg is positive."
4124 (setq gdb-many-windows
4126 (not gdb-many-windows)
4127 (> (prefix-numeric-value arg) 0)))
4128 (message (format "Display of other windows %sabled"
4129 (if gdb-many-windows "en" "dis")))
4130 (if (and gud-comint-buffer
4131 (buffer-name gud-comint-buffer))
4133 (gdb-restore-windows)
4136 (defun gdb-restore-windows ()
4137 "Restore the basic arrangement of windows used by gdb.
4138 This arrangement depends on the value of `gdb-many-windows'."
4140 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4141 (delete-other-windows)
4142 (if gdb-many-windows
4144 (when (or gud-last-last-frame gdb-show-main)
4145 (let ((win (split-window)))
4148 (if gud-last-last-frame
4149 (gud-find-file (car gud-last-last-frame))
4150 (gud-find-file gdb-main-file)))
4151 (setq gdb-source-window win)))))
4153 ;; Called from `gud-sentinel' in gud.el:
4155 "Exit a debugging session cleanly.
4156 Kills the gdb buffers, and resets variables and the source buffers."
4157 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4158 ;; process. This pty must be deleted explicitly.
4159 (let ((pty (get-process "gdb-inferior")))
4160 (if pty (delete-process pty)))
4161 ;; Find gdb-mi buffers and kill them.
4162 (dolist (buffer (buffer-list))
4163 (unless (eq buffer gud-comint-buffer)
4164 (with-current-buffer buffer
4165 (if (eq gud-minor-mode 'gdbmi)
4166 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4168 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4169 (setq gud-minor-mode nil)
4170 (kill-local-variable 'tool-bar-map)
4171 (kill-local-variable 'gdb-define-alist))))))
4172 (setq gdb-disassembly-position nil)
4173 (setq overlay-arrow-variable-list
4174 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4175 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4176 (setq gdb-stack-position nil)
4177 (setq overlay-arrow-variable-list
4178 (delq 'gdb-stack-position overlay-arrow-variable-list))
4179 (setq gdb-thread-position nil)
4180 (setq overlay-arrow-variable-list
4181 (delq 'gdb-thread-position overlay-arrow-variable-list))
4182 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4183 (setq gud-running nil)
4184 (setq gdb-active-process nil)
4185 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4187 (defun gdb-get-source-file ()
4188 "Find the source file where the program starts and display it with related
4189 buffers, if required."
4190 (goto-char (point-min))
4191 (if (re-search-forward gdb-source-file-regexp nil t)
4192 (setq gdb-main-file (match-string 1)))
4193 (if gdb-many-windows
4195 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4196 (if (and gdb-show-main gdb-main-file)
4197 (let ((pop-up-windows t))
4198 (display-buffer (gud-find-file gdb-main-file)))))
4199 (gdb-force-mode-line-update
4200 (propertize "ready" 'face font-lock-variable-name-face)))
4203 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4204 "Put string PUTSTRING in front of POS in the current buffer.
4205 PUTSTRING is displayed by putting an overlay into the current buffer with a
4206 `before-string' string that has a `display' property whose value is
4208 (let ((string (make-string 1 ?x))
4209 (buffer (current-buffer)))
4210 (setq putstring (copy-sequence putstring))
4211 (let ((overlay (make-overlay pos pos buffer))
4213 (list (list 'margin 'left-margin) putstring))))
4214 (put-text-property 0 1 'display prop string)
4216 (add-text-properties 0 1 sprops string))
4217 (overlay-put overlay 'put-break t)
4218 (overlay-put overlay 'before-string string))))
4220 ;;from remove-images
4221 (defun gdb-remove-strings (start end &optional buffer)
4222 "Remove strings between START and END in BUFFER.
4223 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4224 BUFFER nil or omitted means use the current buffer."
4226 (setq buffer (current-buffer)))
4227 (dolist (overlay (overlays-in start end))
4228 (when (overlay-get overlay 'put-break)
4229 (delete-overlay overlay))))
4231 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4232 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4233 (start (- (car posns) 1))
4234 (end (+ (cdr posns) 1))
4235 (putstring (if enabled "B" "b"))
4236 (source-window (get-buffer-window (current-buffer) 0)))
4237 (add-text-properties
4238 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4241 (add-text-properties
4242 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4243 (add-text-properties
4244 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4245 (gdb-remove-breakpoint-icons start end)
4246 (if (display-images-p)
4247 (if (>= (or left-fringe-width
4248 (if source-window (car (window-fringes source-window)))
4249 gdb-buffer-fringe-width) 8)
4252 `(left-fringe breakpoint
4255 'breakpoint-disabled))
4257 'gdb-enabled enabled)
4258 (when (< left-margin-width 2)
4259 (save-current-buffer
4260 (setq left-margin-width 2)
4264 left-margin-width right-margin-width))))
4267 (or breakpoint-enabled-icon
4268 (setq breakpoint-enabled-icon
4269 (find-image `((:type xpm :data
4270 ,breakpoint-xpm-data
4271 :ascent 100 :pointer hand)
4273 ,breakpoint-enabled-pbm-data
4274 :ascent 100 :pointer hand)))))
4275 (or breakpoint-disabled-icon
4276 (setq breakpoint-disabled-icon
4277 (find-image `((:type xpm :data
4278 ,breakpoint-xpm-data
4279 :conversion disabled
4280 :ascent 100 :pointer hand)
4282 ,breakpoint-disabled-pbm-data
4283 :ascent 100 :pointer hand))))))
4287 (when (< left-margin-width 2)
4288 (save-current-buffer
4289 (setq left-margin-width 2)
4290 (let ((window (get-buffer-window (current-buffer) 0)))
4293 window left-margin-width right-margin-width)))))
4295 (propertize putstring
4297 'breakpoint-enabled 'breakpoint-disabled))
4300 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4301 (gdb-remove-strings start end)
4302 (if (display-images-p)
4303 (remove-images start end))
4305 (setq left-margin-width 0)
4306 (let ((window (get-buffer-window (current-buffer) 0)))
4309 window left-margin-width right-margin-width)))))
4312 ;;; Functions for inline completion.
4314 (defvar gud-gdb-fetch-lines-in-progress)
4315 (defvar gud-gdb-fetch-lines-string)
4316 (defvar gud-gdb-fetch-lines-break)
4317 (defvar gud-gdb-fetched-lines)
4319 (defun gud-gdbmi-completions (context command)
4320 "Completion table for GDB/MI commands.
4321 COMMAND is the prefix for which we seek completion.
4322 CONTEXT is the text before COMMAND on the line."
4323 (let ((gud-gdb-fetch-lines-in-progress t)
4324 (gud-gdb-fetch-lines-string nil)
4325 (gud-gdb-fetch-lines-break (length context))
4326 (gud-gdb-fetched-lines nil)
4327 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4328 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4330 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4331 (gdb-input (concat "complete " context command)
4332 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4333 (while gud-gdb-fetch-lines-in-progress
4334 (accept-process-output (get-buffer-process gud-comint-buffer))))
4335 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4337 (defun gud-gdbmi-fetch-lines-filter (string)
4338 "Custom filter function for `gud-gdbmi-completions'."
4339 (setq string (concat gud-gdb-fetch-lines-string
4340 (gud-gdbmi-marker-filter string)))
4341 (while (string-match "\n" string)
4342 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4343 gud-gdb-fetched-lines)
4344 (setq string (substring string (match-end 0))))
4349 ;;; gdb-mi.el ends here