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-lib))
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 (define-minor-mode gdb-enable-debug
463 "Toggle logging of transaction between Emacs and Gdb.
464 The log is stored in `gdb-debug-log' as an alist with elements
465 whose cons is send, send-item or recv and whose cdr is the string
466 being transferred. This list may grow up to a size of
467 `gdb-debug-log-max' after which the oldest element (at the end of
468 the list) is deleted every time a new one is added (at the front)."
473 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
474 "Shell command for generating a list of defined macros in a source file.
475 This list is used to display the #define directive associated
476 with an identifier as a tooltip. It works in a debug session with
477 GDB, when `gud-tooltip-mode' is t.
479 Set `gdb-cpp-define-alist-flags' for any include paths or
485 (defcustom gdb-cpp-define-alist-flags ""
486 "Preprocessor flags for `gdb-cpp-define-alist-program'."
491 (defcustom gdb-create-source-file-list t
492 "Non-nil means create a list of files from which the executable was built.
493 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
494 line for a long time when starting, possibly because your executable was
495 built from a large number of files. This allows quicker initialization
496 but means that these files are not automatically enabled for debugging,
497 e.g., you won't be able to click in the fringe to set a breakpoint until
498 execution has already stopped there."
503 (defcustom gdb-show-main nil
504 "Non-nil means display source file containing the main routine at startup.
505 Also display the main routine in the disassembly buffer if present."
510 (defun gdb-force-mode-line-update (status)
511 (let ((buffer gud-comint-buffer))
512 (if (and buffer (buffer-name buffer))
513 (with-current-buffer buffer
514 (setq mode-line-process
516 (process-status (get-buffer-process buffer)) status))
517 ;; Force mode line redisplay soon.
518 (force-mode-line-update)))))
520 ;; These two are used for menu and toolbar
521 (defun gdb-control-all-threads ()
522 "Switch to non-stop/A mode."
524 (setq gdb-gud-control-all-threads t)
525 ;; Actually forcing the tool-bar to update.
526 (force-mode-line-update)
527 (message "Now in non-stop/A mode."))
529 (defun gdb-control-current-thread ()
530 "Switch to non-stop/T mode."
532 (setq gdb-gud-control-all-threads nil)
533 ;; Actually forcing the tool-bar to update.
534 (force-mode-line-update)
535 (message "Now in non-stop/T mode."))
537 (defun gdb-find-watch-expression ()
538 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
539 (varnum (car var)) expr)
540 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
541 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
542 (component-list (split-string (match-string 2 varnum) "\\." t)))
543 (setq expr (nth 1 var1))
544 (setq varnumlet (car var1))
545 (dolist (component component-list)
546 (setq var2 (assoc varnumlet gdb-var-list))
547 (setq expr (concat expr
548 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
549 (concat "[" component "]")
550 (concat "." component))))
551 (setq varnumlet (concat varnumlet "." component)))
554 ;; noall is used for commands which don't take --all, but only
556 (defun gdb-gud-context-command (command &optional noall)
557 "When `gdb-non-stop' is t, add --thread option to COMMAND if
558 `gdb-gud-control-all-threads' is nil and --all option otherwise.
559 If NOALL is t, always add --thread option no matter what
560 `gdb-gud-control-all-threads' value is.
562 When `gdb-non-stop' is nil, return COMMAND unchanged."
564 (if (and gdb-gud-control-all-threads
566 gdb-supports-non-stop)
567 (concat command " --all ")
568 (gdb-current-context-command command))
571 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
572 "`gud-call' wrapper which adds --thread/--all options between
573 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
575 NOARG must be t when this macro is used outside `gud-def'"
577 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
578 ,(when (not noarg) 'arg)))
580 (defun gdb--check-interpreter (proc string)
581 (unless (zerop (length string))
582 (let ((filter (process-get proc 'gud-normal-filter)))
583 (set-process-filter proc filter)
584 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
585 ;; Apparently we're not running with -i=mi.
586 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
588 (setq string (concat (propertize msg 'font-lock-face 'error)
590 ;; Use the old gud-gbd filter, not because it works, but because it
591 ;; will properly display GDB's answers rather than hanging waiting for
592 ;; answers that aren't coming.
593 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
594 (funcall filter proc string))))
596 (defvar gdb-control-level 0)
599 (defun gdb (command-line)
600 "Run gdb on program FILE in buffer *gud-FILE*.
601 The directory containing FILE becomes the initial working directory
602 and source-file directory for your debugger.
604 COMMAND-LINE is the shell command for starting the gdb session.
605 It should be a string consisting of the name of the gdb
606 executable followed by command-line options. The command-line
607 options should include \"-i=mi\" to use gdb's MI text interface.
608 Note that the old \"--annotate\" option is no longer supported.
610 If `gdb-many-windows' is nil (the default value) then gdb just
611 pops up the GUD buffer unless `gdb-show-main' is t. In this case
612 it starts with two windows: one displaying the GUD buffer and the
613 other with the source file with the main routine of the inferior.
615 If `gdb-many-windows' is t, regardless of the value of
616 `gdb-show-main', the layout below will appear. Keybindings are
617 shown in some of the buffers.
619 Watch expressions appear in the speedbar/slowbar.
621 The following commands help control operation :
623 `gdb-many-windows' - Toggle the number of windows gdb uses.
624 `gdb-restore-windows' - To restore the window layout.
626 See Info node `(emacs)GDB Graphical Interface' for a more
627 detailed description of this mode.
630 +----------------------------------------------------------------------+
632 +-----------------------------------+----------------------------------+
633 | GUD buffer (I/O of GDB) | Locals buffer |
637 +-----------------------------------+----------------------------------+
638 | Source buffer | I/O buffer (of debugged program) |
646 +-----------------------------------+----------------------------------+
647 | Stack buffer | Breakpoints buffer |
648 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
649 | | RET gdb-goto-breakpoint |
650 | | D gdb-delete-breakpoint |
651 +-----------------------------------+----------------------------------+"
653 (interactive (list (gud-query-cmdline 'gdb)))
655 (when (and gud-comint-buffer
656 (buffer-name gud-comint-buffer)
657 (get-buffer-process gud-comint-buffer)
658 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
659 (gdb-restore-windows)
661 "Multiple debugging requires restarting in text command mode"))
663 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
665 ;; Setup a temporary process filter to warn when GDB was not started
667 (let ((proc (get-buffer-process gud-comint-buffer)))
668 (process-put proc 'gud-normal-filter (process-filter proc))
669 (set-process-filter proc #'gdb--check-interpreter))
671 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
672 (set (make-local-variable 'gdb-control-level) 0)
673 (setq comint-input-sender 'gdb-send)
674 (when (ring-empty-p comint-input-ring) ; cf shell-mode
675 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
676 (if (eq system-type 'ms-dos)
679 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
680 (hsize (getenv "HISTSIZE")))
681 (dolist (file (append '("~/.gdbinit")
682 (unless (string-equal (expand-file-name ".")
683 (expand-file-name "~"))
685 (if (file-readable-p (setq file (expand-file-name file)))
687 (insert-file-contents file)
688 ;; TODO? check for "set history save\\( *on\\)?" and do
689 ;; not use history otherwise?
690 (while (re-search-forward
691 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
692 (cond ((string-equal (match-string 1) "filename")
693 (setq hfile (expand-file-name
695 (file-name-directory file))))
696 ((string-equal (match-string 1) "size")
697 (setq hsize (match-string 2))))))))
699 (integerp (setq hsize (string-to-number hsize)))
701 (set (make-local-variable 'comint-input-ring-size) hsize))
703 (set (make-local-variable 'comint-input-ring-file-name) hfile))
704 (comint-read-input-ring t)))
705 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
706 "Set temporary breakpoint at current line.")
708 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
709 "\C-j" "Set execution address to current line.")
711 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
712 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
713 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
714 (gud-def gud-pstar "print* %e" nil
715 "Evaluate C dereferenced pointer expression at point.")
717 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
719 "Step one source line with display.")
720 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
722 "Step one instruction with display.")
723 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
725 "Step one line (skip functions).")
726 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
728 "Step one instruction (skip functions).")
729 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
731 "Continue with display.")
732 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
734 "Finish executing current function.")
735 (gud-def gud-run "-exec-run"
739 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
740 (gud-call "break %f:%l" arg)
744 (gud-call "break *%a" arg)))
745 "\C-b" "Set breakpoint at current line or address.")
747 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
748 (gud-call "clear %f:%l" arg)
752 (gud-call "clear *%a" arg)))
753 "\C-d" "Remove breakpoint at current line or address.")
755 ;; -exec-until doesn't support --all yet
756 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
757 (gud-call "-exec-until %f:%l" arg)
761 (gud-call "-exec-until *%a" arg)))
762 "\C-u" "Continue to current line or address.")
763 ;; TODO Why arg here?
765 gud-go (gud-call (if gdb-active-process
766 (gdb-gud-context-command "-exec-continue")
768 nil "Start or continue execution.")
770 ;; For debugging Emacs only.
774 "pp " (if (eq (buffer-local-value
775 'major-mode (window-buffer)) 'speedbar-mode)
776 (gdb-find-watch-expression) "%e")) arg)
777 nil "Print the Emacs s-expression.")
779 (define-key gud-minor-mode-map [left-margin mouse-1]
780 'gdb-mouse-set-clear-breakpoint)
781 (define-key gud-minor-mode-map [left-fringe mouse-1]
782 'gdb-mouse-set-clear-breakpoint)
783 (define-key gud-minor-mode-map [left-margin C-mouse-1]
784 'gdb-mouse-toggle-breakpoint-margin)
785 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
786 'gdb-mouse-toggle-breakpoint-fringe)
788 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
790 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
792 (define-key gud-minor-mode-map [left-margin mouse-3]
794 (define-key gud-minor-mode-map [left-fringe mouse-3]
797 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
799 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
801 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
803 (define-key gud-minor-mode-map [left-margin C-mouse-3]
806 (set (make-local-variable 'gud-gdb-completion-function)
807 'gud-gdbmi-completions)
809 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
811 (local-set-key "\C-i" 'completion-at-point)
813 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
815 (setq gdb-first-prompt t)
816 (setq gud-running nil)
820 (run-hooks 'gdb-mode-hook))
824 (setq gdb-selected-frame nil
826 gdb-thread-number nil
828 gdb-pending-triggers nil
829 gdb-output-sink 'user
830 gdb-location-alist nil
831 gdb-source-file-list nil
834 gdb-handler-alist '()
835 gdb-handler-number nil
837 gdb-first-done-or-error t
838 gdb-buffer-fringe-width (car (window-fringes))
840 gdb-source-window nil
841 gdb-inferior-status nil
843 gdb-buf-publisher '()
845 gdb-breakpoints-list '()
846 gdb-register-names '()
847 gdb-non-stop gdb-non-stop-setting)
849 (setq gdb-buffer-type 'gdbmi)
851 (gdb-force-mode-line-update
852 (propertize "initializing..." 'face font-lock-variable-name-face))
854 (gdb-get-buffer-create 'gdb-inferior-io)
855 (gdb-clear-inferior-io)
856 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
858 (when (eq system-type 'windows-nt)
859 ;; Don't create a separate console window for the debuggee.
860 (gdb-input "-gdb-set new-console off" 'ignore)
861 ;; Force GDB to behave as if its input and output stream were
862 ;; connected to a TTY device (since on Windows we use pipes for
863 ;; communicating with GDB).
864 (gdb-input "-gdb-set interactive-mode on" 'ignore))
865 (gdb-input "-gdb-set height 0" 'ignore)
868 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
870 (gdb-input "-enable-pretty-printing" 'ignore)
872 ;; Find source file and compilation directory here.
873 (if gdb-create-source-file-list
874 ;; Needs GDB 6.2 onwards.
875 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
876 ;; Needs GDB 6.0 onwards.
877 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
878 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
880 (defun gdb-non-stop-handler ()
881 (goto-char (point-min))
882 (if (re-search-forward "No symbol" nil t)
885 "This version of GDB doesn't support non-stop mode. Turning it off.")
886 (setq gdb-non-stop nil)
887 (setq gdb-supports-non-stop nil))
888 (setq gdb-supports-non-stop t)
889 (gdb-input "-gdb-set target-async 1" 'ignore)
890 (gdb-input "-list-target-features" 'gdb-check-target-async)))
892 (defun gdb-check-target-async ()
893 (goto-char (point-min))
894 (unless (re-search-forward "async" nil t)
896 "Target doesn't support non-stop mode. Turning it off.")
897 (setq gdb-non-stop nil)
898 (gdb-input "-gdb-set non-stop 0" 'ignore)))
900 (defun gdb-delchar-or-quit (arg)
901 "Delete ARG characters or send a quit command to GDB.
902 Send a quit only if point is at the end of the buffer, there is
903 no input, and GDB is waiting for input."
905 (unless (and (eq (current-buffer) gud-comint-buffer)
906 (eq gud-minor-mode 'gdbmi))
907 (error "Not in a GDB-MI buffer"))
908 (let ((proc (get-buffer-process gud-comint-buffer)))
909 (if (and (eobp) proc (process-live-p proc)
911 (= (point) (marker-position (process-mark proc))))
912 ;; Sending an EOF does not work with GDB-MI; submit an
913 ;; explicit quit command.
916 (comint-send-input t t))
919 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
921 (defun gdb-create-define-alist ()
922 "Create an alist of #define directives for GUD tooltips."
923 (let* ((file (buffer-file-name))
925 (with-output-to-string
926 (with-current-buffer standard-output
929 ;; call-process doesn't work with remote file names.
930 (not (file-remote-p default-directory))
931 (call-process shell-file-name file
932 (list t nil) nil "-c"
933 (concat gdb-cpp-define-alist-program " "
934 gdb-cpp-define-alist-flags))))))
935 (define-list (split-string output "\n" t))
937 (setq gdb-define-alist nil)
938 (dolist (define define-list)
939 (setq name (nth 1 (split-string define "[( ]")))
940 (push (cons name define) gdb-define-alist))))
942 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
944 (defun gdb-tooltip-print (expr)
945 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
946 (goto-char (point-min))
947 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
949 (concat expr " = " (read (match-string 1)))
950 (or gud-tooltip-echo-area
951 (not (display-graphic-p)))))))
953 ;; If expr is a macro for a function don't print because of possible dangerous
954 ;; side-effects. Also printing a function within a tooltip generates an
955 ;; unexpected starting annotation (phase error).
956 (defun gdb-tooltip-print-1 (expr)
957 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
958 (goto-char (point-min))
959 (if (search-forward "expands to: " nil t)
960 (unless (looking-at "\\S-+.*(.*).*")
961 (gdb-input (concat "-data-evaluate-expression " expr)
962 `(lambda () (gdb-tooltip-print ,expr)))))))
964 (defun gdb-init-buffer ()
965 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
966 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
967 (when gud-tooltip-mode
968 (make-local-variable 'gdb-define-alist)
969 (gdb-create-define-alist)
970 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
972 (defmacro gdb--if-arrow (arrow-position start-posn end-posn &rest body)
974 (let ((buffer (make-symbol "buffer")))
976 (let ((,buffer (marker-buffer ,arrow-position)))
977 (if (equal ,buffer (window-buffer (posn-window ,end-posn)))
978 (with-current-buffer ,buffer
979 (when (or (equal ,start-posn ,end-posn)
980 (equal (posn-point ,start-posn)
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 start end
993 (let ((line (line-number-at-pos (posn-point end))))
994 (gud-call (concat "until " (number-to-string line)))))
995 (gdb--if-arrow gdb-disassembly-position start end
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 start end
1012 (let ((line (line-number-at-pos (posn-point end))))
1013 (gud-call (concat "tbreak " (number-to-string line)))
1014 (gud-call (concat "jump " (number-to-string line)))))
1015 (gdb--if-arrow gdb-disassembly-position start end
1017 (goto-char (point-min))
1018 (forward-line (1- (line-number-at-pos (posn-point end))))
1020 (gud-call (concat "tbreak *%a"))
1021 (gud-call (concat "jump *%a"))))))
1023 (defcustom gdb-show-changed-values t
1024 "If non-nil change the face of out of scope variables and changed values.
1025 Out of scope variables are suppressed with `shadow' face.
1026 Changed values are highlighted with the face `font-lock-warning-face'."
1031 (defcustom gdb-max-children 40
1032 "Maximum number of children before expansion requires confirmation."
1037 (defcustom gdb-delete-out-of-scope t
1038 "If non-nil delete watch expressions automatically when they go out of scope."
1043 (define-minor-mode gdb-speedbar-auto-raise
1044 "Minor mode to automatically raise the speedbar for watch expressions.
1045 With prefix argument ARG, automatically raise speedbar if ARG is
1046 positive, otherwise don't automatically raise it."
1051 (defcustom gdb-use-colon-colon-notation nil
1052 "If non-nil use FUN::VAR format to display variables in the speedbar."
1057 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1058 (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
1060 (declare-function tooltip-identifier-from-point "tooltip" (point))
1062 (defun gud-watch (&optional arg event)
1063 "Watch expression at point.
1064 With arg, enter name of variable to be watched in the minibuffer."
1065 (interactive (list current-prefix-arg last-input-event))
1066 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1067 (if (eq minor-mode 'gdbmi)
1069 (if event (posn-set-point (event-end event)))
1071 (save-selected-window
1074 (completing-read "Name of variable: "
1075 'gud-gdb-complete-command)
1076 (if (and transient-mark-mode mark-active)
1077 (buffer-substring (region-beginning) (region-end))
1078 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1079 (tooltip-identifier-from-point (point)))))))
1080 (set-text-properties 0 (length expr) nil expr)
1081 (gdb-input (concat "-var-create - * " expr "")
1082 `(lambda () (gdb-var-create-handler ,expr))))))
1083 (message "gud-watch is a no-op in this mode."))))
1085 (defun gdb-var-create-handler (expr)
1086 (let* ((result (gdb-json-partial-output)))
1087 (if (not (bindat-get-field result 'msg))
1089 (list (bindat-get-field result 'name)
1090 (if (and (string-equal gdb-current-language "c")
1091 gdb-use-colon-colon-notation gdb-selected-frame)
1092 (setq expr (concat gdb-selected-frame "::" expr))
1094 (bindat-get-field result 'numchild)
1095 (bindat-get-field result 'type)
1096 (bindat-get-field result 'value)
1098 (bindat-get-field result 'has_more)
1099 gdb-frame-address)))
1100 (push var gdb-var-list)
1102 (unless (string-equal
1103 speedbar-initial-expansion-list-name "GUD")
1104 (speedbar-change-initial-expansion-list "GUD")))
1105 (message-box "No symbol \"%s\" in current context." expr))))
1107 (defun gdb-speedbar-update ()
1108 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1109 (not (gdb-pending-p 'gdb-speedbar-timer)))
1110 ;; Dummy command to update speedbar even when idle.
1111 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn)
1112 ;; Keep gdb-pending-triggers non-nil till end.
1113 (gdb-add-pending 'gdb-speedbar-timer)))
1115 (defun gdb-speedbar-timer-fn ()
1116 (if gdb-speedbar-auto-raise
1117 (raise-frame speedbar-frame))
1118 (gdb-delete-pending 'gdb-speedbar-timer)
1119 (speedbar-timer-fn))
1121 (defun gdb-var-evaluate-expression-handler (varnum changed)
1122 (goto-char (point-min))
1123 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1124 (let ((var (assoc varnum gdb-var-list)))
1126 (if changed (setcar (nthcdr 5 var) 'changed))
1127 (setcar (nthcdr 4 var) (read (match-string 1)))))
1128 (gdb-speedbar-update))
1130 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1131 (defun gdb-var-list-children (varnum)
1132 (gdb-input (concat "-var-update " varnum) 'ignore)
1133 (gdb-input (concat "-var-list-children --all-values " varnum)
1134 `(lambda () (gdb-var-list-children-handler ,varnum))))
1136 (defun gdb-var-list-children-handler (varnum)
1137 (let* ((var-list nil)
1138 (output (bindat-get-field (gdb-json-partial-output "child")))
1139 (children (bindat-get-field output 'children)))
1140 (catch 'child-already-watched
1141 (dolist (var gdb-var-list)
1142 (if (string-equal varnum (car var))
1144 ;; With dynamic varobjs numchild may have increased.
1145 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1147 (dolist (child children)
1148 (let ((varchild (list (bindat-get-field child 'name)
1149 (bindat-get-field child 'exp)
1150 (bindat-get-field child 'numchild)
1151 (bindat-get-field child 'type)
1152 (bindat-get-field child 'value)
1154 (bindat-get-field child 'has_more))))
1155 (if (assoc (car varchild) gdb-var-list)
1156 (throw 'child-already-watched nil))
1157 (push varchild var-list))))
1158 (push var var-list)))
1159 (setq gdb-var-list (nreverse var-list))))
1160 (gdb-speedbar-update))
1162 (defun gdb-var-set-format (format)
1163 "Set the output format for a variable displayed in the speedbar."
1164 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1166 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1169 (defun gdb-var-delete-1 (var varnum)
1170 (gdb-input (concat "-var-delete " varnum) 'ignore)
1171 (setq gdb-var-list (delq var gdb-var-list))
1172 (dolist (varchild gdb-var-list)
1173 (if (string-match (concat (car var) "\\.") (car varchild))
1174 (setq gdb-var-list (delq varchild gdb-var-list)))))
1176 (defun gdb-var-delete ()
1177 "Delete watch expression at point from the speedbar."
1179 (let ((text (speedbar-line-text)))
1180 (string-match "\\(\\S-+\\)" text)
1181 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1183 (if (string-match "\\." (car var))
1184 (message-box "Can only delete a root expression")
1185 (gdb-var-delete-1 var varnum)))))
1187 (defun gdb-var-delete-children (varnum)
1188 "Delete children of variable object at point from the speedbar."
1189 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1191 (defun gdb-edit-value (_text _token _indent)
1192 "Assign a value to a variable displayed in the speedbar."
1193 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1195 (value (read-string "New value: ")))
1196 (gdb-input (concat "-var-assign " varnum " " value)
1197 `(lambda () (gdb-edit-value-handler ,value)))))
1199 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1201 (defun gdb-edit-value-handler (value)
1202 (goto-char (point-min))
1203 (if (re-search-forward gdb-error-regexp nil t)
1204 (message-box "Invalid number or expression (%s)" value)))
1206 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1207 (defun gdb-var-update ()
1208 (if (not (gdb-pending-p 'gdb-var-update))
1209 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler))
1210 (gdb-add-pending 'gdb-var-update))
1212 (defun gdb-var-update-handler ()
1213 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1214 (dolist (var gdb-var-list)
1215 (setcar (nthcdr 5 var) nil))
1216 (let ((temp-var-list gdb-var-list))
1217 (dolist (change changelist)
1218 (let* ((varnum (bindat-get-field change 'name))
1219 (var (assoc varnum gdb-var-list))
1220 (new-num (bindat-get-field change 'new_num_children)))
1222 (let ((scope (bindat-get-field change 'in_scope))
1223 (has-more (bindat-get-field change 'has_more)))
1224 (cond ((string-equal scope "false")
1225 (if gdb-delete-out-of-scope
1226 (gdb-var-delete-1 var varnum)
1227 (setcar (nthcdr 5 var) 'out-of-scope)))
1228 ((string-equal scope "true")
1229 (setcar (nthcdr 6 var) has-more)
1230 (when (and (or (not has-more)
1231 (string-equal has-more "0"))
1233 (string-equal (nth 2 var) "0"))
1234 (setcar (nthcdr 4 var)
1235 (bindat-get-field change 'value))
1236 (setcar (nthcdr 5 var) 'changed)))
1237 ((string-equal scope "invalid")
1238 (gdb-var-delete-1 var varnum)))))
1239 (let ((var-list nil) var1
1240 (children (bindat-get-field change 'new_children)))
1242 (setq var1 (pop temp-var-list))
1244 (if (string-equal varnum (car var1))
1245 (let ((new (string-to-number new-num))
1246 (previous (string-to-number (nth 2 var1))))
1247 (setcar (nthcdr 2 var1) new-num)
1248 (push var1 var-list)
1251 ;; Add new children to list.
1252 (dotimes (dummy previous)
1253 (push (pop temp-var-list) var-list))
1254 (dolist (child children)
1256 (list (bindat-get-field child 'name)
1257 (bindat-get-field child 'exp)
1258 (bindat-get-field child 'numchild)
1259 (bindat-get-field child 'type)
1260 (bindat-get-field child 'value)
1262 (bindat-get-field child 'has_more))))
1263 (push varchild var-list))))
1264 ;; Remove deleted children from list.
1266 (dotimes (dummy new)
1267 (push (pop temp-var-list) var-list))
1268 (dotimes (dummy (- previous new))
1269 (pop temp-var-list)))))
1270 (push var1 var-list))
1271 (setq var1 (pop temp-var-list)))
1272 (setq gdb-var-list (nreverse var-list))))))))
1273 (setq gdb-pending-triggers
1274 (delq 'gdb-var-update gdb-pending-triggers))
1275 (gdb-speedbar-update))
1277 (defun gdb-speedbar-expand-node (text token indent)
1278 "Expand the node the user clicked on.
1279 TEXT is the text of the button we clicked on, a + or - item.
1280 TOKEN is data related to this node.
1281 INDENT is the current indentation depth."
1282 (cond ((string-match "+" text) ;expand this node
1283 (let* ((var (assoc token gdb-var-list))
1284 (expr (nth 1 var)) (children (nth 2 var)))
1285 (if (or (<= (string-to-number children) gdb-max-children)
1287 (format "%s has %s children. Continue? " expr children)))
1288 (gdb-var-list-children token))))
1289 ((string-match "-" text) ;contract this node
1290 (dolist (var gdb-var-list)
1291 (if (string-match (concat token "\\.") (car var))
1292 (setq gdb-var-list (delq var gdb-var-list))))
1293 (gdb-var-delete-children token)
1294 (speedbar-change-expand-button-char ?+)
1295 (speedbar-delete-subblock indent))
1296 (t (error "Ooops... not sure what to do")))
1297 (speedbar-center-buffer-smartly))
1299 (defun gdb-get-target-string ()
1300 (with-current-buffer gud-comint-buffer
1307 ;; Each buffer has a TYPE -- a symbol that identifies the function
1308 ;; of that particular buffer.
1310 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1311 ;; is constructed specially.
1313 ;; Others are constructed by gdb-get-buffer-create and
1314 ;; named according to the rules set forth in the gdb-buffer-rules
1316 (defvar gdb-buffer-rules '())
1318 (defun gdb-rules-name-maker (rules-entry)
1320 (defun gdb-rules-buffer-mode (rules-entry)
1321 (nth 2 rules-entry))
1322 (defun gdb-rules-update-trigger (rules-entry)
1323 (nth 3 rules-entry))
1325 (defun gdb-update-buffer-name ()
1326 "Rename current buffer according to name-maker associated with
1327 it in `gdb-buffer-rules'."
1328 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1329 gdb-buffer-rules))))
1330 (when f (rename-buffer (funcall f)))))
1332 (defun gdb-current-buffer-rules ()
1333 "Get `gdb-buffer-rules' entry for current buffer type."
1334 (assoc gdb-buffer-type gdb-buffer-rules))
1336 (defun gdb-current-buffer-thread ()
1337 "Get thread object of current buffer from `gdb-threads-list'.
1339 When current buffer is not bound to any thread, return main
1341 (cdr (assoc gdb-thread-number gdb-threads-list)))
1343 (defun gdb-current-buffer-frame ()
1344 "Get current stack frame object for thread of current buffer."
1345 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1347 (defun gdb-buffer-type (buffer)
1348 "Get value of `gdb-buffer-type' for BUFFER."
1349 (with-current-buffer buffer
1352 (defun gdb-buffer-shows-main-thread-p ()
1353 "Return t if current GDB buffer shows main selected thread and
1354 is not bound to it."
1356 (not (local-variable-p 'gdb-thread-number)))
1358 (defun gdb-get-buffer (buffer-type &optional thread)
1359 "Get a specific GDB buffer.
1361 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1362 and `gdb-thread-number' (if provided) must be equal to THREAD."
1364 (dolist (buffer (buffer-list) nil)
1365 (with-current-buffer buffer
1366 (when (and (eq gdb-buffer-type buffer-type)
1368 (equal gdb-thread-number thread)))
1369 (throw 'found buffer))))))
1371 (defun gdb-get-buffer-create (buffer-type &optional thread)
1372 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1373 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1375 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1376 buffer-local variable of the new buffer.
1378 Buffer mode and name are selected according to buffer type.
1380 If buffer has trigger associated with it in `gdb-buffer-rules',
1381 this trigger is subscribed to `gdb-buf-publisher' and called with
1383 (or (gdb-get-buffer buffer-type thread)
1384 (let ((rules (assoc buffer-type gdb-buffer-rules))
1385 (new (generate-new-buffer "limbo")))
1386 (with-current-buffer new
1387 (let ((mode (gdb-rules-buffer-mode rules))
1388 (trigger (gdb-rules-update-trigger rules)))
1389 (when mode (funcall mode))
1390 (setq gdb-buffer-type buffer-type)
1392 (set (make-local-variable 'gdb-thread-number) thread))
1393 (set (make-local-variable 'gud-minor-mode)
1394 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1395 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1396 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1398 (gdb-add-subscriber gdb-buf-publisher
1399 (cons (current-buffer)
1400 (gdb-bind-function-to-buffer
1401 trigger (current-buffer))))
1402 (funcall trigger 'start))
1403 (current-buffer))))))
1405 (defun gdb-bind-function-to-buffer (expr buffer)
1406 "Return a function which will evaluate EXPR in BUFFER."
1407 `(lambda (&rest args)
1408 (with-current-buffer ,buffer
1409 (apply ',expr args))))
1411 ;; Used to define all gdb-frame-*-buffer functions except
1412 ;; `gdb-frame-io-buffer'
1413 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1414 "Define a function NAME which shows gdb BUFFER in a separate frame.
1416 DOC is an optional documentation string."
1417 `(defun ,name (&optional thread)
1420 (let ((special-display-regexps (append special-display-regexps '(".*")))
1421 (special-display-frame-alist gdb-frame-parameters))
1422 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1424 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1425 "Define a function NAME which shows gdb BUFFER.
1427 DOC is an optional documentation string."
1428 `(defun ,name (&optional thread)
1432 (gdb-get-buffer-create ,buffer thread) t)))
1434 ;; Used to display windows with thread-bound buffers
1435 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1437 `(defun ,name (&optional thread)
1440 (gdb-preempt-existing-or-display-buffer
1441 (gdb-get-buffer-create ,buffer thread)
1442 ,split-horizontal)))
1444 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1445 ;; at least one and possible more functions. The functions have these
1446 ;; roles in defining a buffer type:
1448 ;; NAME - Return a name for this buffer type.
1450 ;; The remaining function(s) are optional:
1452 ;; MODE - called in a new buffer with no arguments, should establish
1453 ;; the proper mode for the buffer.
1456 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1457 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1459 (setcdr binding rules)
1460 (push (cons buffer-type rules)
1461 gdb-buffer-rules))))
1463 (defun gdb-parent-mode ()
1464 "Generic mode to derive all other GDB buffer modes from."
1465 (kill-all-local-variables)
1466 (setq buffer-read-only t)
1467 (buffer-disable-undo)
1468 ;; Delete buffer from gdb-buf-publisher when it's killed
1469 ;; (if it has an associated update trigger)
1474 (let ((trigger (gdb-rules-update-trigger
1475 (gdb-current-buffer-rules))))
1477 (gdb-delete-subscriber
1479 ;; This should match gdb-add-subscriber done in
1480 ;; gdb-get-buffer-create
1481 (cons (current-buffer)
1482 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1485 ;; Partial-output buffer : This accumulates output from a command executed on
1486 ;; behalf of emacs (rather than the user).
1488 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1489 'gdb-partial-output-name)
1491 (defun gdb-partial-output-name ()
1492 (concat " *partial-output-"
1493 (gdb-get-target-string)
1497 (gdb-set-buffer-rules 'gdb-inferior-io
1498 'gdb-inferior-io-name
1499 'gdb-inferior-io-mode)
1501 (defun gdb-inferior-io-name ()
1502 (concat "*input/output of "
1503 (gdb-get-target-string)
1506 (defun gdb-display-io-buffer ()
1507 "Display IO of debugged program in a separate window."
1510 (gdb-get-buffer-create 'gdb-inferior-io) t))
1512 (defun gdb-inferior-io--init-proc (proc)
1513 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1514 (set-process-filter proc 'gdb-inferior-filter)
1515 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1517 (concat "-inferior-tty-set "
1518 ;; The process can run on a remote host.
1519 (or (process-get proc 'remote-tty)
1520 (process-tty-name proc)))
1523 (defun gdb-inferior-io-sentinel (proc str)
1524 (when (eq (process-status proc) 'failed)
1525 ;; When the debugged process exits, Emacs gets an EIO error on
1526 ;; read from the pty, and stops listening to it. If the gdb
1527 ;; process is still running, remove the pty, make a new one, and
1529 (let ((gdb-proc (get-buffer-process gud-comint-buffer))
1530 (io-buffer (process-buffer proc)))
1531 (when (and gdb-proc (process-live-p gdb-proc)
1532 (buffer-live-p io-buffer))
1533 ;; `comint-exec' deletes the original process as a side effect.
1534 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1535 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1537 (defconst gdb-frame-parameters
1538 '((height . 14) (width . 80)
1540 (tool-bar-lines . nil)
1541 (menu-bar-lines . nil)
1542 (minibuffer . nil)))
1544 (defun gdb-frame-io-buffer ()
1545 "Display IO of debugged program in a new frame."
1547 (let ((special-display-regexps (append special-display-regexps '(".*")))
1548 (special-display-frame-alist gdb-frame-parameters))
1549 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1551 (defvar gdb-inferior-io-mode-map
1552 (let ((map (make-sparse-keymap)))
1553 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1554 (define-key map "\C-c\C-z" 'gdb-io-stop)
1555 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1556 (define-key map "\C-c\C-d" 'gdb-io-eof)
1557 (define-key map "\C-d" 'gdb-io-eof)
1560 ;; We want to use comint because it has various nifty and familiar features.
1561 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1562 "Major mode for gdb inferior-io."
1563 :syntax-table nil :abbrev-table nil
1564 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1566 (defun gdb-inferior-filter (proc string)
1567 (unless (string-equal string "")
1568 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1569 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1570 (comint-output-filter proc string)))
1572 (defun gdb-io-interrupt ()
1573 "Interrupt the program being debugged."
1576 (get-buffer-process gud-comint-buffer) comint-ptyp))
1578 (defun gdb-io-quit ()
1579 "Send quit signal to the program being debugged."
1582 (get-buffer-process gud-comint-buffer) comint-ptyp))
1584 (defun gdb-io-stop ()
1585 "Stop the program being debugged."
1588 (get-buffer-process gud-comint-buffer) comint-ptyp))
1590 (defun gdb-io-eof ()
1591 "Send end-of-file to the program being debugged."
1594 (get-buffer-process gud-comint-buffer)))
1596 (defun gdb-clear-inferior-io ()
1597 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1601 (defconst breakpoint-xpm-data
1603 static char *magick[] = {
1604 /* columns rows colors chars-per-pixel */
1620 "XPM data used for breakpoint icon.")
1622 (defconst breakpoint-enabled-pbm-data
1625 0 0 0 0 1 1 1 1 0 0 0 0
1626 0 0 0 1 1 1 1 1 1 0 0 0
1627 0 0 1 1 1 1 1 1 1 1 0 0
1628 0 1 1 1 1 1 1 1 1 1 1 0
1629 0 1 1 1 1 1 1 1 1 1 1 0
1630 0 1 1 1 1 1 1 1 1 1 1 0
1631 0 1 1 1 1 1 1 1 1 1 1 0
1632 0 0 1 1 1 1 1 1 1 1 0 0
1633 0 0 0 1 1 1 1 1 1 0 0 0
1634 0 0 0 0 1 1 1 1 0 0 0 0"
1635 "PBM data used for enabled breakpoint icon.")
1637 (defconst breakpoint-disabled-pbm-data
1649 0 0 0 1 0 1 0 1 0 0"
1650 "PBM data used for disabled breakpoint icon.")
1652 (defvar breakpoint-enabled-icon nil
1653 "Icon for enabled breakpoint in display margin.")
1655 (defvar breakpoint-disabled-icon nil
1656 "Icon for disabled breakpoint in display margin.")
1658 (declare-function define-fringe-bitmap "fringe.c"
1659 (bitmap bits &optional height width align))
1661 (and (display-images-p)
1662 ;; Bitmap for breakpoint in fringe
1663 (define-fringe-bitmap 'breakpoint
1664 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1665 ;; Bitmap for gud-overlay-arrow in fringe
1666 (define-fringe-bitmap 'hollow-right-triangle
1667 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1669 (defface breakpoint-enabled
1673 "Face for enabled breakpoint icon in fringe."
1676 (defface breakpoint-disabled
1677 '((((class color) (min-colors 88)) :foreground "grey70")
1678 ;; Ensure that on low-color displays that we end up something visible.
1679 (((class color) (min-colors 8) (background light))
1680 :foreground "black")
1681 (((class color) (min-colors 8) (background dark))
1682 :foreground "white")
1683 (((type tty) (class mono))
1685 (t :background "gray"))
1686 "Face for disabled breakpoint icon in fringe."
1690 (defvar gdb-control-commands-regexp
1693 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1694 "while-stepping\\|stepping\\|ws\\|actions"
1695 "\\)\\([[:blank:]]+.*\\)?$")
1696 "Regexp matching GDB commands that enter a recursive reading loop.
1697 As long as GDB is in the recursive reading loop, it does not expect
1698 commands to be prefixed by \"-interpreter-exec console\".")
1700 (defun gdb-send (proc string)
1701 "A comint send filter for gdb."
1702 (with-current-buffer gud-comint-buffer
1703 (let ((inhibit-read-only t))
1704 (remove-text-properties (point-min) (point-max) '(face))))
1705 ;; mimic <RET> key to repeat previous command in GDB
1706 (if (not (string= "" string))
1707 (setq gdb-last-command string)
1708 (if gdb-last-command (setq string gdb-last-command)))
1709 (if (or (string-match "^-" string)
1710 (> gdb-control-level 0))
1711 ;; Either MI command or we are feeding GDB's recursive reading loop.
1713 (setq gdb-first-done-or-error t)
1714 (process-send-string proc (concat string "\n"))
1715 (if (and (string-match "^end$" string)
1716 (> gdb-control-level 0))
1717 (setq gdb-control-level (1- gdb-control-level))))
1719 (if (string-match "\\\\$" string)
1720 (setq gdb-continuation (concat gdb-continuation string "\n"))
1721 (setq gdb-first-done-or-error t)
1722 (let ((to-send (concat "-interpreter-exec console "
1723 (gdb-mi-quote string)
1725 (if gdb-enable-debug
1726 (push (cons 'mi-send to-send) gdb-debug-log))
1727 (process-send-string proc to-send))
1728 (if (and (string-match "^end$" string)
1729 (> gdb-control-level 0))
1730 (setq gdb-control-level (1- gdb-control-level)))
1731 (setq gdb-continuation nil)))
1732 (if (string-match gdb-control-commands-regexp string)
1733 (setq gdb-control-level (1+ gdb-control-level))))
1735 (defun gdb-mi-quote (string)
1736 "Return STRING quoted properly as an MI argument.
1737 The string is enclosed in double quotes.
1738 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1739 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1740 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1741 (concat "\"" string "\""))
1743 (defun gdb-input (command handler-function)
1744 "Send COMMAND to GDB via the MI interface.
1745 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1747 (if gdb-enable-debug (push (list 'send-item command handler-function)
1749 (setq gdb-token-number (1+ gdb-token-number))
1750 (setq command (concat (number-to-string gdb-token-number) command))
1751 (push (cons gdb-token-number handler-function) gdb-handler-alist)
1752 (process-send-string (get-buffer-process gud-comint-buffer)
1753 (concat command "\n")))
1755 ;; NOFRAME is used for gud execution control commands
1756 (defun gdb-current-context-command (command)
1757 "Add --thread to gdb COMMAND when needed."
1758 (if (and gdb-thread-number
1759 gdb-supports-non-stop)
1760 (concat command " --thread " gdb-thread-number)
1763 (defun gdb-current-context-buffer-name (name)
1764 "Add thread information and asterisks to string NAME.
1766 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1768 (if (local-variable-p 'gdb-thread-number)
1769 (format " (bound to thread %s)" gdb-thread-number)
1773 (defun gdb-current-context-mode-name (mode)
1774 "Add thread information to MODE which is to be used as
1777 (if gdb-thread-number
1778 (format " [thread %s]" gdb-thread-number)
1782 (defcustom gud-gdb-command-name "gdb -i=mi"
1783 "Default command to execute an executable under the GDB debugger."
1788 (setq gud-running nil)
1789 (setq gdb-output-sink 'user)
1790 (setq gdb-pending-triggers nil))
1792 (defun gdb-update (&optional no-proc)
1793 "Update buffers showing status of debug session.
1794 If NO-PROC is non-nil, do not try to contact the GDB process."
1795 (when gdb-first-prompt
1796 (gdb-force-mode-line-update
1797 (propertize "initializing..." 'face font-lock-variable-name-face))
1799 (setq gdb-first-prompt nil))
1802 (gdb-get-main-selected-frame))
1804 ;; We may need to update gdb-threads-list so we can use
1805 (gdb-get-buffer-create 'gdb-threads-buffer)
1806 ;; gdb-break-list is maintained in breakpoints handler
1807 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1810 (gdb-emit-signal gdb-buf-publisher 'update))
1812 (gdb-get-changed-registers)
1813 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1814 (dolist (var gdb-var-list)
1815 (setcar (nthcdr 5 var) nil))
1818 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1819 ;; because we may need to update current gud-running value without
1820 ;; changing current thread (see gdb-running)
1821 (defun gdb-setq-thread-number (number)
1822 "Only this function must be used to change `gdb-thread-number'
1823 value to NUMBER, because `gud-running' and `gdb-frame-number'
1824 need to be updated appropriately when current thread changes."
1825 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1826 (unless (string-equal number "0") (setq gdb-thread-number number))
1827 (setq gdb-frame-number "0")
1828 (gdb-update-gud-running))
1830 (defun gdb-update-gud-running ()
1831 "Set `gud-running' according to the state of current thread.
1833 `gdb-frame-number' is set to 0 if current thread is now stopped.
1835 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1836 cannot be reliably used to determine whether or not execution
1837 control buttons should be shown in menu or toolbar. Use
1838 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1841 For all-stop mode, thread information is unavailable while target
1843 (let ((old-value gud-running))
1845 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1847 ;; Set frame number to "0" when _current_ threads stops.
1848 (when (and (gdb-current-buffer-thread)
1849 (not (eq gud-running old-value)))
1850 (setq gdb-frame-number "0"))))
1852 (defun gdb-show-run-p ()
1853 "Return t if \"Run/continue\" should be shown on the toolbar."
1854 (or (not gdb-active-process)
1856 (not gdb-gud-control-all-threads)
1859 (and gdb-gud-control-all-threads
1860 (> gdb-stopped-threads-count 0))))
1862 (defun gdb-show-stop-p ()
1863 "Return t if \"Stop\" should be shown on the toolbar."
1865 (not gdb-gud-control-all-threads)
1868 (and gdb-gud-control-all-threads
1869 (> gdb-running-threads-count 0))))
1871 ;; GUD displays the selected GDB frame. This might might not be the current
1872 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1873 ;; visited breakpoint is, use that window.
1874 (defun gdb-display-source-buffer (buffer)
1875 (let* ((last-window (if gud-last-last-frame
1877 (gud-find-file (car gud-last-last-frame)))))
1878 (source-window (or last-window
1879 (if (and gdb-source-window
1880 (window-live-p gdb-source-window))
1881 gdb-source-window))))
1883 (setq gdb-source-window source-window)
1884 (set-window-buffer source-window buffer))
1887 (defun gdb-car< (a b)
1888 (< (car a) (car b)))
1890 (defvar gdbmi-record-list
1891 '((gdb-gdb . "(gdb) \n")
1892 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1893 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1894 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1895 (gdb-console . "~\\(\".*?\"\\)\n")
1896 (gdb-internals . "&\\(\".*?\"\\)\n")
1897 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1898 (gdb-running . "\\*running,\\(.*?\n\\)")
1899 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1900 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1901 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1902 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1903 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1905 (defun gud-gdbmi-marker-filter (string)
1906 "Filter GDB/MI output."
1908 ;; Record transactions if logging is enabled.
1909 (when gdb-enable-debug
1910 (push (cons 'recv string) gdb-debug-log)
1911 (if (and gdb-debug-log-max
1912 (> (length gdb-debug-log) gdb-debug-log-max))
1913 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1915 ;; Recall the left over gud-marker-acc from last time.
1916 (setq gud-marker-acc (concat gud-marker-acc string))
1918 ;; Start accumulating output for the GUD buffer.
1919 (setq gdb-filter-output "")
1920 (let (output-record-list)
1922 ;; Process all the complete markers in this chunk.
1923 (dolist (gdbmi-record gdbmi-record-list)
1924 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1925 (push (list (match-beginning 0)
1927 (match-string 1 gud-marker-acc)
1928 (match-string 2 gud-marker-acc)
1931 (setq gud-marker-acc
1932 (concat (substring gud-marker-acc 0 (match-beginning 0))
1933 ;; Pad with spaces to preserve position.
1934 (make-string (length (match-string 0 gud-marker-acc)) 32)
1935 (substring gud-marker-acc (match-end 0))))))
1937 (setq output-record-list (sort output-record-list 'gdb-car<))
1939 (dolist (output-record output-record-list)
1940 (let ((record-type (cadr output-record))
1941 (arg1 (nth 2 output-record))
1942 (arg2 (nth 3 output-record)))
1943 (cond ((eq record-type 'gdb-error)
1944 (gdb-done-or-error arg2 arg1 'error))
1945 ((eq record-type 'gdb-done)
1946 (gdb-done-or-error arg2 arg1 'done))
1947 ;; Suppress "No registers." GDB 6.8 and earlier
1948 ;; duplicates MI error message on internal stream.
1949 ;; Don't print to GUD buffer.
1950 ((not (and (eq record-type 'gdb-internals)
1951 (string-equal (read arg1) "No registers.\n")))
1952 (funcall record-type arg1)))))
1954 (setq gdb-output-sink 'user)
1956 (string-match "^ *" gud-marker-acc)
1957 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1961 (defun gdb-gdb (_output-field))
1963 (defun gdb-shell (output-field)
1964 (setq gdb-filter-output
1965 (concat output-field gdb-filter-output)))
1967 (defun gdb-ignored-notification (_output-field))
1969 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1970 (defun gdb-thread-created (_output-field))
1971 (defun gdb-thread-exited (output-field)
1972 "Handle =thread-exited async record: unset `gdb-thread-number'
1973 if current thread exited and update threads list."
1974 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1975 (if (string= gdb-thread-number thread-id)
1976 (gdb-setq-thread-number nil))
1977 ;; When we continue current thread and it quickly exits,
1978 ;; gdb-pending-triggers left after gdb-running disallow us to
1979 ;; properly call -thread-info without --thread option. Thus we
1980 ;; need to use gdb-wait-for-pending.
1981 (gdb-wait-for-pending
1982 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1984 (defun gdb-thread-selected (output-field)
1985 "Handler for =thread-selected MI output record.
1987 Sets `gdb-thread-number' to new id."
1988 (let* ((result (gdb-json-string output-field))
1989 (thread-id (bindat-get-field result 'id)))
1990 (gdb-setq-thread-number thread-id)
1991 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1992 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1993 ;; as usually. Things happen to fast and second call (from
1994 ;; gdb-thread-selected handler) gets cut off by our beloved
1995 ;; gdb-pending-triggers.
1996 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1997 ;; body will get executed when `gdb-pending-triggers` is empty.
1998 (gdb-wait-for-pending
2001 (defun gdb-running (output-field)
2003 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
2004 ;; We reset gdb-frame-number to nil if current thread has gone
2005 ;; running. This can't be done in gdb-thread-list-handler-custom
2006 ;; because we need correct gdb-frame-number by the time
2007 ;; -thread-info command is sent.
2008 (when (or (string-equal thread-id "all")
2009 (string-equal thread-id gdb-thread-number))
2010 (setq gdb-frame-number nil)))
2011 (setq gdb-inferior-status "running")
2012 (gdb-force-mode-line-update
2013 (propertize gdb-inferior-status 'face font-lock-type-face))
2014 (when (not gdb-non-stop)
2015 (setq gud-running t))
2016 (setq gdb-active-process t)
2017 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2019 (defun gdb-starting (_output-field)
2020 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2021 (setq gdb-inferior-status "running")
2022 (gdb-force-mode-line-update
2023 (propertize gdb-inferior-status 'face font-lock-type-face))
2024 (setq gdb-active-process t)
2025 (setq gud-running t)
2026 ;; GDB doesn't seem to respond to -thread-info before first stop or
2027 ;; thread exit (even in non-stop mode), so this is useless.
2028 ;; Behavior may change in the future.
2029 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2031 ;; -break-insert -t didn't give a reason before gdb 6.9
2033 (defun gdb-stopped (output-field)
2034 "Given the contents of *stopped MI async record, select new
2035 current thread and update GDB buffers."
2036 ;; Reason is available with target-async only
2037 (let* ((result (gdb-json-string output-field))
2038 (reason (bindat-get-field result 'reason))
2039 (thread-id (bindat-get-field result 'thread-id)))
2041 ;; -data-list-register-names needs to be issued for any stopped
2043 (when (not gdb-register-names)
2044 (gdb-input (concat "-data-list-register-names"
2045 (if gdb-supports-non-stop
2046 (concat " --thread " thread-id)))
2047 'gdb-register-names-handler))
2049 ;; Don't set gud-last-frame here as it's currently done in
2050 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2054 ;; ;; Extract the frame position from the marker.
2055 ;; gud-last-frame (cons file
2056 ;; (string-to-number
2057 ;; (match-string 6 gud-marker-acc)))))
2059 (setq gdb-inferior-status (or reason "unknown"))
2060 (gdb-force-mode-line-update
2061 (propertize gdb-inferior-status 'face font-lock-warning-face))
2062 (if (string-equal reason "exited-normally")
2063 (setq gdb-active-process nil))
2065 ;; Select new current thread.
2067 ;; Don't switch if we have no reasons selected
2068 (when gdb-switch-reasons
2069 ;; Switch from another stopped thread only if we have
2070 ;; gdb-switch-when-another-stopped:
2071 (when (or gdb-switch-when-another-stopped
2072 (not (string= "stopped"
2073 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2074 ;; Switch if current reason has been selected or we have no
2076 (if (or (eq gdb-switch-reasons t)
2077 (member reason gdb-switch-reasons))
2078 (when (not (string-equal gdb-thread-number thread-id))
2079 (message (concat "Switched to thread " thread-id))
2080 (gdb-setq-thread-number thread-id))
2081 (message (format "Thread %s stopped" thread-id)))))
2083 ;; Print "(gdb)" to GUD console
2084 (when gdb-first-done-or-error
2085 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2087 ;; In non-stop, we update information as soon as another thread gets
2089 (when (or gdb-first-done-or-error
2091 ;; In all-stop this updates gud-running properly as well.
2093 (setq gdb-first-done-or-error nil))
2094 (run-hook-with-args 'gdb-stopped-functions result)))
2096 ;; Remove the trimmings from log stream containing debugging messages
2097 ;; being produced by GDB's internals, use warning face and send to GUD
2099 (defun gdb-internals (output-field)
2100 (setq gdb-filter-output
2103 (let ((error-message
2104 (read output-field)))
2106 0 (length error-message)
2107 'face font-lock-warning-face
2111 ;; Remove the trimmings from the console stream and send to GUD buffer
2112 ;; (frontend MI commands should not print to this stream)
2113 (defun gdb-console (output-field)
2114 (setq gdb-filter-output
2115 (gdb-concat-output gdb-filter-output (read output-field))))
2117 (defun gdb-done-or-error (output-field token-number type)
2118 (if (string-equal token-number "")
2119 ;; Output from command entered by user
2121 (setq gdb-output-sink 'user)
2122 (setq token-number nil)
2123 ;; MI error - send to minibuffer
2124 (when (eq type 'error)
2125 ;; Skip "msg=" from `output-field'
2126 (message (read (substring output-field 4)))
2127 ;; Don't send to the console twice. (If it is a console error
2128 ;; it is also in the console stream.)
2129 (setq output-field nil)))
2130 ;; Output from command from frontend.
2131 (setq gdb-output-sink 'emacs))
2133 (gdb-clear-partial-output)
2135 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2136 (let* ((proc (get-buffer-process gud-comint-buffer))
2137 (no-proc (or (null proc)
2138 (memq (process-status proc) '(exit signal)))))
2140 (when gdb-first-done-or-error
2141 (unless (or token-number gud-running no-proc)
2142 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2143 (gdb-update no-proc)
2144 (setq gdb-first-done-or-error nil))
2146 (setq gdb-filter-output
2147 (gdb-concat-output gdb-filter-output output-field))
2150 (with-current-buffer
2151 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2153 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2154 (setq gdb-handler-alist
2155 (assq-delete-all token-number gdb-handler-alist)))))
2157 (defun gdb-concat-output (so-far new)
2159 ((eq gdb-output-sink 'user) (concat so-far new))
2160 ((eq gdb-output-sink 'emacs)
2161 (gdb-append-to-partial-output new)
2164 (defun gdb-append-to-partial-output (string)
2165 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2166 (goto-char (point-max))
2169 (defun gdb-clear-partial-output ()
2170 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2173 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2174 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2176 Field names are wrapped in double quotes and equal signs are
2177 replaced with semicolons.
2179 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2180 partial output. This is used to get rid of useless keys in lists
2181 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2182 -break-info are examples of MI commands which issue such
2185 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2186 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2187 -break-info output when it contains breakpoint script field
2188 incompatible with GDB/MI output syntax."
2190 (goto-char (point-min))
2193 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2194 (replace-match "" nil nil nil 1))))
2197 ;; Find positions of braces which enclose broken list
2198 (while (re-search-forward (concat fix-list "={\"") nil t)
2199 (let ((p1 (goto-char (- (point) 2)))
2200 (p2 (progn (forward-sexp)
2202 ;; Replace braces with brackets
2210 (goto-char (point-min))
2212 (while (re-search-forward
2213 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2214 (replace-match "\"\\1\":\\2" nil nil))
2215 (goto-char (point-max))
2218 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2219 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2221 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2222 (gdb-jsonify-buffer fix-key fix-list)
2224 (goto-char (point-min))
2225 (let ((json-array-type 'list))
2228 (defun gdb-json-string (string &optional fix-key fix-list)
2229 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2231 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2234 (gdb-json-read-buffer fix-key fix-list)))
2236 (defun gdb-json-partial-output (&optional fix-key fix-list)
2237 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2239 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2240 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2241 (gdb-json-read-buffer fix-key fix-list)))
2243 (defun gdb-line-posns (line)
2244 "Return a pair of LINE beginning and end positions."
2245 (let ((offset (1+ (- line (line-number-at-pos)))))
2247 (line-beginning-position offset)
2248 (line-end-position offset))))
2250 (defmacro gdb-mark-line (line variable)
2251 "Set VARIABLE marker to point at beginning of LINE.
2253 If current window has no fringes, inverse colors on LINE.
2255 Return position where LINE begins."
2257 (let* ((posns (gdb-line-posns ,line))
2258 (start-posn (car posns))
2259 (end-posn (cdr posns)))
2260 (set-marker ,variable (copy-marker start-posn))
2261 (when (not (> (car (window-fringes)) 0))
2262 (put-text-property start-posn end-posn
2263 'font-lock-face '(:inverse-video t)))
2266 (defun gdb-pad-string (string padding)
2267 (format (concat "%" (number-to-string padding) "s") string))
2269 ;; gdb-table struct is a way to programmatically construct simple
2270 ;; tables. It help to reliably align columns of data in GDB buffers
2272 (cl-defstruct gdb-table
2275 (row-properties nil)
2278 (defun gdb-mapcar* (function &rest seqs)
2279 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2280 If there are several SEQS, FUNCTION is called with that many
2281 arguments, and mapping stops as soon as the shortest list runs
2283 (let ((shortest (apply #'min (mapcar #'length seqs))))
2290 (number-sequence 0 (1- shortest)))))
2292 (defun gdb-table-add-row (table row &optional properties)
2293 "Add ROW of string to TABLE and recalculate column sizes.
2295 When non-nil, PROPERTIES will be added to the whole row when
2296 calling `gdb-table-string'."
2297 (let ((rows (gdb-table-rows table))
2298 (row-properties (gdb-table-row-properties table))
2299 (column-sizes (gdb-table-column-sizes table))
2300 (right-align (gdb-table-right-align table)))
2301 (when (not column-sizes)
2302 (setf (gdb-table-column-sizes table)
2303 (make-list (length row) 0)))
2304 (setf (gdb-table-rows table)
2305 (append rows (list row)))
2306 (setf (gdb-table-row-properties table)
2307 (append row-properties (list properties)))
2308 (setf (gdb-table-column-sizes table)
2309 (gdb-mapcar* (lambda (x s)
2311 (max (abs x) (string-width (or s "")))))
2312 (if right-align new-x (- new-x))))
2313 (gdb-table-column-sizes table)
2315 ;; Avoid trailing whitespace at eol
2316 (if (not (gdb-table-right-align table))
2317 (setcar (last (gdb-table-column-sizes table)) 0))))
2319 (defun gdb-table-string (table &optional sep)
2320 "Return TABLE as a string with columns separated with SEP."
2321 (let ((column-sizes (gdb-table-column-sizes table)))
2325 (lambda (row properties)
2327 (mapconcat 'identity
2328 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2332 (gdb-table-rows table)
2333 (gdb-table-row-properties table))
2336 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2337 (defun gdb-get-many-fields (struct &rest fields)
2338 "Return a list of FIELDS values from STRUCT."
2340 (dolist (field fields)
2341 (push (bindat-get-field struct field) values))
2344 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2346 &optional signal-list)
2347 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2348 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2349 buffer with `gdb-bind-function-to-buffer'.
2351 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2352 defined trigger is called with an argument from SIGNAL-LIST. It's
2353 not recommended to define triggers with empty SIGNAL-LIST.
2354 Normally triggers should respond at least to 'update signal.
2356 Normally the trigger defined by this command must be called from
2357 the buffer where HANDLER-NAME must work. This should be done so
2358 that buffer-local thread number may be used in GDB-COMMAND (by
2359 calling `gdb-current-context-command').
2360 `gdb-bind-function-to-buffer' is used to achieve this, see
2361 `gdb-get-buffer-create'.
2363 Triggers defined by this command are meant to be used as a
2364 trigger argument when describing buffer types with
2365 `gdb-set-buffer-rules'."
2366 `(defun ,trigger-name (&optional signal)
2368 (or (not ,signal-list)
2369 (memq signal ,signal-list))
2370 (when (not (gdb-pending-p
2371 (cons (current-buffer) ',trigger-name)))
2372 (gdb-input ,gdb-command
2373 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2374 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2376 ;; Used by disassembly buffer only, the rest use
2377 ;; def-gdb-trigger-and-handler
2378 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2379 &optional nopreserve)
2380 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2382 Handlers are normally called from the buffers they put output in.
2384 Delete ((current-buffer) . TRIGGER-NAME) from
2385 `gdb-pending-triggers', erase current buffer and evaluate
2386 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2388 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2389 `(defun ,handler-name ()
2390 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2391 (let* ((buffer-read-only nil)
2392 (window (get-buffer-window (current-buffer) 0))
2393 (start (window-start window))
2394 (p (window-point window)))
2397 (gdb-update-buffer-name)
2398 ,(when (not nopreserve)
2399 '(set-window-start window start)
2400 '(set-window-point window p)))))
2402 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2403 handler-name custom-defun
2404 &optional signal-list)
2405 "Define trigger and handler.
2407 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2408 `def-gdb-auto-update-trigger'.
2410 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2411 `def-gdb-auto-update-handler'."
2413 (def-gdb-auto-update-trigger ,trigger-name
2415 ,handler-name ,signal-list)
2416 (def-gdb-auto-update-handler ,handler-name
2417 ,trigger-name ,custom-defun)))
2421 ;; Breakpoint buffer : This displays the output of `-break-list'.
2422 (def-gdb-trigger-and-handler
2423 gdb-invalidate-breakpoints "-break-list"
2424 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2427 (gdb-set-buffer-rules
2428 'gdb-breakpoints-buffer
2429 'gdb-breakpoints-buffer-name
2430 'gdb-breakpoints-mode
2431 'gdb-invalidate-breakpoints)
2433 (defun gdb-breakpoints-list-handler-custom ()
2434 (let ((breakpoints-list (bindat-get-field
2435 (gdb-json-partial-output "bkpt" "script")
2436 'BreakpointTable 'body))
2437 (table (make-gdb-table)))
2438 (setq gdb-breakpoints-list nil)
2439 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2440 (dolist (breakpoint breakpoints-list)
2441 (add-to-list 'gdb-breakpoints-list
2442 (cons (bindat-get-field breakpoint 'number)
2444 (let ((at (bindat-get-field breakpoint 'at))
2445 (pending (bindat-get-field breakpoint 'pending))
2446 (func (bindat-get-field breakpoint 'func))
2447 (type (bindat-get-field breakpoint 'type)))
2448 (gdb-table-add-row table
2450 (bindat-get-field breakpoint 'number)
2452 (or (bindat-get-field breakpoint 'disp) "")
2453 (let ((flag (bindat-get-field breakpoint 'enabled)))
2454 (if (string-equal flag "y")
2455 (propertize "y" 'font-lock-face font-lock-warning-face)
2456 (propertize "n" 'font-lock-face font-lock-comment-face)))
2457 (bindat-get-field breakpoint 'addr)
2458 (or (bindat-get-field breakpoint 'times) "")
2459 (if (and type (string-match ".*watchpoint" type))
2460 (bindat-get-field breakpoint 'what)
2463 (propertize (or func "unknown")
2464 'font-lock-face font-lock-function-name-face)
2465 (gdb-frame-location breakpoint)))))
2466 ;; Add clickable properties only for breakpoints with file:line
2468 (append (list 'gdb-breakpoint breakpoint)
2469 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2470 mouse-face highlight))))))
2471 (insert (gdb-table-string table " "))
2472 (gdb-place-breakpoints)))
2474 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2475 (defun gdb-place-breakpoints ()
2476 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2477 (dolist (buffer (buffer-list))
2478 (with-current-buffer buffer
2479 (if (and (eq gud-minor-mode 'gdbmi)
2480 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2481 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2482 (dolist (breakpoint gdb-breakpoints-list)
2483 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2484 ; an associative list
2485 (line (bindat-get-field breakpoint 'line)))
2487 (let ((file (bindat-get-field breakpoint 'fullname))
2488 (flag (bindat-get-field breakpoint 'enabled))
2489 (bptno (bindat-get-field breakpoint 'number)))
2490 (unless (and file (file-exists-p file))
2491 (setq file (cdr (assoc bptno gdb-location-alist))))
2493 (string-equal file "File not found"))
2494 ;; If the full filename is not recorded in the
2495 ;; breakpoint structure or in `gdb-location-alist', use
2496 ;; -file-list-exec-source-file to extract it.
2497 (when (setq file (bindat-get-field breakpoint 'file))
2498 (gdb-input (concat "list " file ":1") 'ignore)
2499 (gdb-input "-file-list-exec-source-file"
2500 `(lambda () (gdb-get-location
2501 ,bptno ,line ,flag))))
2502 (with-current-buffer (find-file-noselect file 'nowarn)
2504 ;; Only want one breakpoint icon at each location.
2505 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2506 (string-to-number line)))))))))
2508 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2510 (defun gdb-get-location (bptno line flag)
2511 "Find the directory containing the relevant source file.
2512 Put in buffer and place breakpoint icon."
2513 (goto-char (point-min))
2514 (catch 'file-not-found
2515 (if (re-search-forward gdb-source-file-regexp nil t)
2516 (delete (cons bptno "File not found") gdb-location-alist)
2517 (push (cons bptno (match-string 1)) gdb-location-alist)
2519 (unless (assoc bptno gdb-location-alist)
2520 (push (cons bptno "File not found") gdb-location-alist)
2521 (message-box "Cannot find source file for breakpoint location.
2522 Add directory to search path for source files using the GDB command, dir."))
2523 (throw 'file-not-found nil))
2524 (with-current-buffer (find-file-noselect (match-string 1))
2526 ;; only want one breakpoint icon at each location
2527 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2529 (add-hook 'find-file-hook 'gdb-find-file-hook)
2531 (defun gdb-find-file-hook ()
2532 "Set up buffer for debugging if file is part of the source code
2533 of the current session."
2534 (if (and (buffer-name gud-comint-buffer)
2535 ;; in case gud or gdb-ui is just loaded
2537 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2539 (if (member buffer-file-name gdb-source-file-list)
2540 (with-current-buffer (find-buffer-visiting buffer-file-name)
2541 (gdb-init-buffer)))))
2543 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2544 (declare-function gud-break "gdb-mi" t t) ; gud-def
2545 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2547 (defun gdb-mouse-set-clear-breakpoint (event)
2548 "Set/clear breakpoint in left fringe/margin at mouse click.
2549 If not in a source or disassembly buffer just set point."
2551 (mouse-minibuffer-check event)
2552 (let ((posn (event-end event)))
2553 (with-selected-window (posn-window posn)
2554 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2555 (if (numberp (posn-point posn))
2557 (goto-char (posn-point posn))
2558 (if (or (posn-object posn)
2559 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2563 (posn-set-point posn))))
2565 (defun gdb-mouse-toggle-breakpoint-margin (event)
2566 "Enable/disable breakpoint in left margin with mouse click."
2568 (mouse-minibuffer-check event)
2569 (let ((posn (event-end event)))
2570 (if (numberp (posn-point posn))
2571 (with-selected-window (posn-window posn)
2573 (goto-char (posn-point posn))
2574 (if (posn-object posn)
2576 (let ((bptno (get-text-property
2577 0 'gdb-bptno (car (posn-string posn)))))
2579 (if (get-text-property
2580 0 'gdb-enabled (car (posn-string posn)))
2585 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2586 "Enable/disable breakpoint in left fringe with mouse click."
2588 (mouse-minibuffer-check event)
2589 (let* ((posn (event-end event))
2590 (pos (posn-point posn))
2593 (with-selected-window (posn-window posn)
2594 (with-current-buffer (window-buffer (selected-window))
2596 (dolist (overlay (overlays-in pos pos))
2597 (when (overlay-get overlay 'put-break)
2598 (setq obj (overlay-get overlay 'before-string))))
2602 (if (get-text-property 0 'gdb-enabled obj)
2605 (get-text-property 0 'gdb-bptno obj)))))))))
2607 (defun gdb-breakpoints-buffer-name ()
2608 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2610 (def-gdb-display-buffer
2611 gdb-display-breakpoints-buffer
2612 'gdb-breakpoints-buffer
2613 "Display status of user-settable breakpoints.")
2615 (def-gdb-frame-for-buffer
2616 gdb-frame-breakpoints-buffer
2617 'gdb-breakpoints-buffer
2618 "Display status of user-settable breakpoints in a new frame.")
2620 (defvar gdb-breakpoints-mode-map
2621 (let ((map (make-sparse-keymap))
2622 (menu (make-sparse-keymap "Breakpoints")))
2623 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2624 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2625 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2626 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2627 (suppress-keymap map)
2628 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2629 (define-key map " " 'gdb-toggle-breakpoint)
2630 (define-key map "D" 'gdb-delete-breakpoint)
2631 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2632 (define-key map "q" 'gdb-delete-frame-or-window)
2633 (define-key map "\r" 'gdb-goto-breakpoint)
2634 (define-key map "\t" (lambda ()
2636 (gdb-set-window-buffer
2637 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2638 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2639 (define-key map [follow-link] 'mouse-face)
2642 (defun gdb-delete-frame-or-window ()
2643 "Delete frame if there is only one window. Otherwise delete the window."
2645 (if (one-window-p) (delete-frame)
2648 ;;from make-mode-line-mouse-map
2649 (defun gdb-make-header-line-mouse-map (mouse function) "\
2650 Return a keymap with single entry for mouse key MOUSE on the header line.
2651 MOUSE is defined to run function FUNCTION with no args in the buffer
2652 corresponding to the mode line clicked."
2653 (let ((map (make-sparse-keymap)))
2654 (define-key map (vector 'header-line mouse) function)
2655 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2658 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2660 'help-echo ,help-echo
2661 'mouse-face ',mouse-face
2664 (gdb-make-header-line-mouse-map
2666 (lambda (event) (interactive "e")
2667 (save-selected-window
2668 (select-window (posn-window (event-start event)))
2669 (gdb-set-window-buffer
2670 (gdb-get-buffer-create ',buffer) t) )))))
2673 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2676 (defun gdb-threads-buffer-name ()
2677 (concat "*threads of " (gdb-get-target-string) "*"))
2679 (def-gdb-display-buffer
2680 gdb-display-threads-buffer
2682 "Display GDB threads.")
2684 (def-gdb-frame-for-buffer
2685 gdb-frame-threads-buffer
2687 "Display GDB threads in a new frame.")
2689 (def-gdb-trigger-and-handler
2690 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2691 gdb-thread-list-handler gdb-thread-list-handler-custom
2692 '(start update update-threads))
2694 (gdb-set-buffer-rules
2696 'gdb-threads-buffer-name
2698 'gdb-invalidate-threads)
2700 (defvar gdb-threads-font-lock-keywords
2701 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2702 (" \\(stopped\\)" (1 font-lock-warning-face))
2703 (" \\(running\\)" (1 font-lock-string-face))
2704 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2705 "Font lock keywords used in `gdb-threads-mode'.")
2707 (defvar gdb-threads-mode-map
2708 (let ((map (make-sparse-keymap)))
2709 (define-key map "\r" 'gdb-select-thread)
2710 (define-key map "f" 'gdb-display-stack-for-thread)
2711 (define-key map "F" 'gdb-frame-stack-for-thread)
2712 (define-key map "l" 'gdb-display-locals-for-thread)
2713 (define-key map "L" 'gdb-frame-locals-for-thread)
2714 (define-key map "r" 'gdb-display-registers-for-thread)
2715 (define-key map "R" 'gdb-frame-registers-for-thread)
2716 (define-key map "d" 'gdb-display-disassembly-for-thread)
2717 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2718 (define-key map "i" 'gdb-interrupt-thread)
2719 (define-key map "c" 'gdb-continue-thread)
2720 (define-key map "s" 'gdb-step-thread)
2721 (define-key map "\t"
2724 (gdb-set-window-buffer
2725 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2726 (define-key map [mouse-2] 'gdb-select-thread)
2727 (define-key map [follow-link] 'mouse-face)
2730 (defvar gdb-threads-header
2732 (gdb-propertize-header
2733 "Breakpoints" gdb-breakpoints-buffer
2734 "mouse-1: select" mode-line-highlight mode-line-inactive)
2736 (gdb-propertize-header "Threads" gdb-threads-buffer
2737 nil nil mode-line)))
2739 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2740 "Major mode for GDB threads."
2741 (setq gdb-thread-position (make-marker))
2742 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2743 (setq header-line-format gdb-threads-header)
2744 (set (make-local-variable 'font-lock-defaults)
2745 '(gdb-threads-font-lock-keywords))
2746 'gdb-invalidate-threads)
2748 (defun gdb-thread-list-handler-custom ()
2749 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2750 (table (make-gdb-table))
2752 (setq gdb-threads-list nil)
2753 (setq gdb-running-threads-count 0)
2754 (setq gdb-stopped-threads-count 0)
2755 (set-marker gdb-thread-position nil)
2757 (dolist (thread (reverse threads-list))
2758 (let ((running (equal (bindat-get-field thread 'state) "running")))
2759 (add-to-list 'gdb-threads-list
2760 (cons (bindat-get-field thread 'id)
2762 (cl-incf (if running
2763 gdb-running-threads-count
2764 gdb-stopped-threads-count))
2766 (gdb-table-add-row table
2768 (bindat-get-field thread 'id)
2770 (if gdb-thread-buffer-verbose-names
2771 (concat (bindat-get-field thread 'target-id) " ") "")
2772 (bindat-get-field thread 'state)
2773 ;; Include frame information for stopped threads
2776 " in " (bindat-get-field thread 'frame 'func)
2777 (if gdb-thread-buffer-arguments
2780 (let ((args (bindat-get-field thread 'frame 'args)))
2783 (apply #'format "%s=%s"
2784 (gdb-get-many-fields arg 'name 'value)))
2788 (if gdb-thread-buffer-locations
2789 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2790 (if gdb-thread-buffer-addresses
2791 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2795 'mouse-face 'highlight
2796 'help-echo "mouse-2, RET: select thread")))
2797 (when (string-equal gdb-thread-number
2798 (bindat-get-field thread 'id))
2799 (setq marked-line (length gdb-threads-list))))
2800 (insert (gdb-table-string table " "))
2802 (gdb-mark-line marked-line gdb-thread-position)))
2803 ;; We update gud-running here because we need to make sure that
2804 ;; gdb-threads-list is up-to-date
2805 (gdb-update-gud-running)
2806 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2808 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2809 "Define a NAME command which will act upon thread on the current line.
2811 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2812 be the value of 'gdb-thread property of the current line. If
2813 'gdb-thread is nil, error is signaled."
2814 `(defun ,name (&optional event)
2816 (interactive (list last-input-event))
2817 (if event (posn-set-point (event-end event)))
2820 (let ((thread (get-text-property (point) 'gdb-thread)))
2823 (error "Not recognized as thread line"))))))
2825 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2827 "Define a NAME which will call BUFFER-COMMAND with id of thread
2828 on the current line."
2829 `(def-gdb-thread-buffer-command ,name
2830 (,buffer-command (bindat-get-field thread 'id))
2833 (def-gdb-thread-buffer-command gdb-select-thread
2834 (let ((new-id (bindat-get-field thread 'id)))
2835 (gdb-setq-thread-number new-id)
2836 (gdb-input (concat "-thread-select " new-id) 'ignore)
2838 "Select the thread at current line of threads buffer.")
2840 (def-gdb-thread-buffer-simple-command
2841 gdb-display-stack-for-thread
2842 gdb-preemptively-display-stack-buffer
2843 "Display stack buffer for the thread at current line.")
2845 (def-gdb-thread-buffer-simple-command
2846 gdb-display-locals-for-thread
2847 gdb-preemptively-display-locals-buffer
2848 "Display locals buffer for the thread at current line.")
2850 (def-gdb-thread-buffer-simple-command
2851 gdb-display-registers-for-thread
2852 gdb-preemptively-display-registers-buffer
2853 "Display registers buffer for the thread at current line.")
2855 (def-gdb-thread-buffer-simple-command
2856 gdb-display-disassembly-for-thread
2857 gdb-preemptively-display-disassembly-buffer
2858 "Display disassembly buffer for the thread at current line.")
2860 (def-gdb-thread-buffer-simple-command
2861 gdb-frame-stack-for-thread
2862 gdb-frame-stack-buffer
2863 "Display a new frame with stack buffer for the thread at
2866 (def-gdb-thread-buffer-simple-command
2867 gdb-frame-locals-for-thread
2868 gdb-frame-locals-buffer
2869 "Display a new frame with locals buffer for the thread at
2872 (def-gdb-thread-buffer-simple-command
2873 gdb-frame-registers-for-thread
2874 gdb-frame-registers-buffer
2875 "Display a new frame with registers buffer for the thread at
2878 (def-gdb-thread-buffer-simple-command
2879 gdb-frame-disassembly-for-thread
2880 gdb-frame-disassembly-buffer
2881 "Display a new frame with disassembly buffer for the thread at
2884 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2885 "Define a NAME which will execute GUD-COMMAND with
2886 `gdb-thread-number' locally bound to id of thread on the current
2888 `(def-gdb-thread-buffer-command ,name
2890 (let ((gdb-thread-number (bindat-get-field thread 'id))
2891 (gdb-gud-control-all-threads nil))
2892 (call-interactively #',gud-command))
2893 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2896 (def-gdb-thread-buffer-gud-command
2897 gdb-interrupt-thread
2899 "Interrupt thread at current line.")
2901 (def-gdb-thread-buffer-gud-command
2904 "Continue thread at current line.")
2906 (def-gdb-thread-buffer-gud-command
2909 "Step thread at current line.")
2914 (defcustom gdb-memory-rows 8
2915 "Number of data rows in memory window."
2920 (defcustom gdb-memory-columns 4
2921 "Number of data columns in memory window."
2926 (defcustom gdb-memory-format "x"
2927 "Display format of data items in memory window."
2928 :type '(choice (const :tag "Hexadecimal" "x")
2929 (const :tag "Signed decimal" "d")
2930 (const :tag "Unsigned decimal" "u")
2931 (const :tag "Octal" "o")
2932 (const :tag "Binary" "t"))
2936 (defcustom gdb-memory-unit 4
2937 "Unit size of data items in memory window."
2938 :type '(choice (const :tag "Byte" 1)
2939 (const :tag "Halfword" 2)
2940 (const :tag "Word" 4)
2941 (const :tag "Giant word" 8))
2945 (def-gdb-trigger-and-handler
2946 gdb-invalidate-memory
2947 (format "-data-read-memory %s %s %d %d %d"
2953 gdb-read-memory-handler
2954 gdb-read-memory-custom
2957 (gdb-set-buffer-rules
2959 'gdb-memory-buffer-name
2961 'gdb-invalidate-memory)
2963 (defun gdb-memory-column-width (size format)
2964 "Return length of string with memory unit of SIZE in FORMAT.
2966 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2967 in `gdb-memory-format'."
2968 (let ((format-base (cdr (assoc format
2970 ("d" . 10) ("u" . 10)
2974 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2975 (cond ((string-equal format "x")
2976 (+ 2 res)) ; hexadecimal numbers have 0x in front
2977 ((or (string-equal format "d")
2978 (string-equal format "o"))
2981 (error "Unknown format"))))
2983 (defun gdb-read-memory-custom ()
2984 (let* ((res (gdb-json-partial-output))
2985 (err-msg (bindat-get-field res 'msg)))
2987 (let ((memory (bindat-get-field res 'memory)))
2988 (setq gdb-memory-address (bindat-get-field res 'addr))
2989 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2990 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2991 (setq gdb-memory-last-address gdb-memory-address)
2992 (dolist (row memory)
2993 (insert (concat (bindat-get-field row 'addr) ":"))
2994 (dolist (column (bindat-get-field row 'data))
2995 (insert (gdb-pad-string column
2996 (+ 2 (gdb-memory-column-width
2998 gdb-memory-format)))))
3000 ;; Show last page instead of empty buffer when out of bounds
3002 (let ((gdb-memory-address gdb-memory-last-address))
3003 (gdb-invalidate-memory 'update)
3004 (error err-msg))))))
3006 (defvar gdb-memory-mode-map
3007 (let ((map (make-sparse-keymap)))
3008 (suppress-keymap map t)
3009 (define-key map "q" 'kill-this-buffer)
3010 (define-key map "n" 'gdb-memory-show-next-page)
3011 (define-key map "p" 'gdb-memory-show-previous-page)
3012 (define-key map "a" 'gdb-memory-set-address)
3013 (define-key map "t" 'gdb-memory-format-binary)
3014 (define-key map "o" 'gdb-memory-format-octal)
3015 (define-key map "u" 'gdb-memory-format-unsigned)
3016 (define-key map "d" 'gdb-memory-format-signed)
3017 (define-key map "x" 'gdb-memory-format-hexadecimal)
3018 (define-key map "b" 'gdb-memory-unit-byte)
3019 (define-key map "h" 'gdb-memory-unit-halfword)
3020 (define-key map "w" 'gdb-memory-unit-word)
3021 (define-key map "g" 'gdb-memory-unit-giant)
3022 (define-key map "R" 'gdb-memory-set-rows)
3023 (define-key map "C" 'gdb-memory-set-columns)
3026 (defun gdb-memory-set-address-event (event)
3027 "Handle a click on address field in memory buffer header."
3029 (save-selected-window
3030 (select-window (posn-window (event-start event)))
3031 (gdb-memory-set-address)))
3033 ;; Non-event version for use within keymap
3034 (defun gdb-memory-set-address ()
3035 "Set the start memory address."
3037 (let ((arg (read-from-minibuffer "Memory address: ")))
3038 (setq gdb-memory-address arg))
3039 (gdb-invalidate-memory 'update))
3041 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3042 "Define a function NAME which reads new VAR value from minibuffer."
3043 `(defun ,name (event)
3046 (save-selected-window
3047 (select-window (posn-window (event-start event)))
3048 (let* ((arg (read-from-minibuffer ,echo-string))
3049 (count (string-to-number arg)))
3051 (error "Positive number only")
3052 (customize-set-variable ',variable count)
3053 (gdb-invalidate-memory 'update))))))
3055 (def-gdb-set-positive-number
3059 "Set the number of data rows in memory window.")
3061 (def-gdb-set-positive-number
3062 gdb-memory-set-columns
3065 "Set the number of data columns in memory window.")
3067 (defmacro def-gdb-memory-format (name format doc)
3068 "Define a function NAME to switch memory buffer to use FORMAT.
3070 DOC is an optional documentation string."
3071 `(defun ,name () ,(when doc doc)
3073 (customize-set-variable 'gdb-memory-format ,format)
3074 (gdb-invalidate-memory 'update)))
3076 (def-gdb-memory-format
3077 gdb-memory-format-binary "t"
3078 "Set the display format to binary.")
3080 (def-gdb-memory-format
3081 gdb-memory-format-octal "o"
3082 "Set the display format to octal.")
3084 (def-gdb-memory-format
3085 gdb-memory-format-unsigned "u"
3086 "Set the display format to unsigned decimal.")
3088 (def-gdb-memory-format
3089 gdb-memory-format-signed "d"
3090 "Set the display format to decimal.")
3092 (def-gdb-memory-format
3093 gdb-memory-format-hexadecimal "x"
3094 "Set the display format to hexadecimal.")
3096 (defvar gdb-memory-format-map
3097 (let ((map (make-sparse-keymap)))
3098 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3100 "Keymap to select format in the header line.")
3102 (defvar gdb-memory-format-menu
3103 (let ((map (make-sparse-keymap "Format")))
3105 (define-key map [binary]
3106 '(menu-item "Binary" gdb-memory-format-binary
3107 :button (:radio . (equal gdb-memory-format "t"))))
3108 (define-key map [octal]
3109 '(menu-item "Octal" gdb-memory-format-octal
3110 :button (:radio . (equal gdb-memory-format "o"))))
3111 (define-key map [unsigned]
3112 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3113 :button (:radio . (equal gdb-memory-format "u"))))
3114 (define-key map [signed]
3115 '(menu-item "Signed Decimal" gdb-memory-format-signed
3116 :button (:radio . (equal gdb-memory-format "d"))))
3117 (define-key map [hexadecimal]
3118 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3119 :button (:radio . (equal gdb-memory-format "x"))))
3121 "Menu of display formats in the header line.")
3123 (defun gdb-memory-format-menu (event)
3125 (x-popup-menu event gdb-memory-format-menu))
3127 (defun gdb-memory-format-menu-1 (event)
3129 (save-selected-window
3130 (select-window (posn-window (event-start event)))
3131 (let* ((selection (gdb-memory-format-menu event))
3132 (binding (and selection (lookup-key gdb-memory-format-menu
3133 (vector (car selection))))))
3134 (if binding (call-interactively binding)))))
3136 (defmacro def-gdb-memory-unit (name unit-size doc)
3137 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3139 DOC is an optional documentation string."
3140 `(defun ,name () ,(when doc doc)
3142 (customize-set-variable 'gdb-memory-unit ,unit-size)
3143 (gdb-invalidate-memory 'update)))
3145 (def-gdb-memory-unit gdb-memory-unit-giant 8
3146 "Set the unit size to giant words (eight bytes).")
3148 (def-gdb-memory-unit gdb-memory-unit-word 4
3149 "Set the unit size to words (four bytes).")
3151 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3152 "Set the unit size to halfwords (two bytes).")
3154 (def-gdb-memory-unit gdb-memory-unit-byte 1
3155 "Set the unit size to bytes.")
3157 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3158 "Define a function NAME which show new address in memory buffer.
3160 The defined function switches Memory buffer to show address
3161 stored in ADDRESS-VAR variable.
3163 DOC is an optional documentation string."
3167 (let ((gdb-memory-address ,address-var))
3168 (gdb-invalidate-memory))))
3170 (def-gdb-memory-show-page gdb-memory-show-previous-page
3171 gdb-memory-prev-page)
3173 (def-gdb-memory-show-page gdb-memory-show-next-page
3174 gdb-memory-next-page)
3176 (defvar gdb-memory-unit-map
3177 (let ((map (make-sparse-keymap)))
3178 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3180 "Keymap to select units in the header line.")
3182 (defvar gdb-memory-unit-menu
3183 (let ((map (make-sparse-keymap "Unit")))
3184 (define-key map [giantwords]
3185 '(menu-item "Giant words" gdb-memory-unit-giant
3186 :button (:radio . (equal gdb-memory-unit 8))))
3187 (define-key map [words]
3188 '(menu-item "Words" gdb-memory-unit-word
3189 :button (:radio . (equal gdb-memory-unit 4))))
3190 (define-key map [halfwords]
3191 '(menu-item "Halfwords" gdb-memory-unit-halfword
3192 :button (:radio . (equal gdb-memory-unit 2))))
3193 (define-key map [bytes]
3194 '(menu-item "Bytes" gdb-memory-unit-byte
3195 :button (:radio . (equal gdb-memory-unit 1))))
3197 "Menu of units in the header line.")
3199 (defun gdb-memory-unit-menu (event)
3201 (x-popup-menu event gdb-memory-unit-menu))
3203 (defun gdb-memory-unit-menu-1 (event)
3205 (save-selected-window
3206 (select-window (posn-window (event-start event)))
3207 (let* ((selection (gdb-memory-unit-menu event))
3208 (binding (and selection (lookup-key gdb-memory-unit-menu
3209 (vector (car selection))))))
3210 (if binding (call-interactively binding)))))
3212 (defvar gdb-memory-font-lock-keywords
3213 '(;; <__function.name+n>
3214 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3215 (1 font-lock-function-name-face)))
3216 "Font lock keywords used in `gdb-memory-mode'.")
3218 (defvar gdb-memory-header
3223 'face font-lock-warning-face
3224 'help-echo "mouse-1: decrement address"
3225 'mouse-face 'mode-line-highlight
3226 'local-map (gdb-make-header-line-mouse-map
3228 #'gdb-memory-show-previous-page))
3231 'face font-lock-warning-face
3232 'help-echo "mouse-1: increment address"
3233 'mouse-face 'mode-line-highlight
3234 'local-map (gdb-make-header-line-mouse-map
3236 #'gdb-memory-show-next-page))
3238 (propertize gdb-memory-address
3239 'face font-lock-warning-face
3240 'help-echo "mouse-1: set start address"
3241 'mouse-face 'mode-line-highlight
3242 'local-map (gdb-make-header-line-mouse-map
3244 #'gdb-memory-set-address-event))
3246 (propertize (number-to-string gdb-memory-rows)
3247 'face font-lock-warning-face
3248 'help-echo "mouse-1: set number of columns"
3249 'mouse-face 'mode-line-highlight
3250 'local-map (gdb-make-header-line-mouse-map
3252 #'gdb-memory-set-rows))
3254 (propertize (number-to-string gdb-memory-columns)
3255 'face font-lock-warning-face
3256 'help-echo "mouse-1: set number of columns"
3257 'mouse-face 'mode-line-highlight
3258 'local-map (gdb-make-header-line-mouse-map
3260 #'gdb-memory-set-columns))
3262 (propertize gdb-memory-format
3263 'face font-lock-warning-face
3264 'help-echo "mouse-3: select display format"
3265 'mouse-face 'mode-line-highlight
3266 'local-map gdb-memory-format-map)
3268 (propertize (number-to-string gdb-memory-unit)
3269 'face font-lock-warning-face
3270 'help-echo "mouse-3: select unit size"
3271 'mouse-face 'mode-line-highlight
3272 'local-map gdb-memory-unit-map)))
3273 "Header line used in `gdb-memory-mode'.")
3275 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3276 "Major mode for examining memory."
3277 (setq header-line-format gdb-memory-header)
3278 (set (make-local-variable 'font-lock-defaults)
3279 '(gdb-memory-font-lock-keywords))
3280 'gdb-invalidate-memory)
3282 (defun gdb-memory-buffer-name ()
3283 (concat "*memory of " (gdb-get-target-string) "*"))
3285 (def-gdb-display-buffer
3286 gdb-display-memory-buffer
3288 "Display memory contents.")
3290 (defun gdb-frame-memory-buffer ()
3291 "Display memory contents in a new frame."
3293 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3294 (special-display-frame-alist
3298 ,@gdb-frame-parameters)))
3299 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3302 ;;; Disassembly view
3304 (defun gdb-disassembly-buffer-name ()
3305 (gdb-current-context-buffer-name
3306 (concat "disassembly of " (gdb-get-target-string))))
3308 (def-gdb-display-buffer
3309 gdb-display-disassembly-buffer
3310 'gdb-disassembly-buffer
3311 "Display disassembly for current stack frame.")
3313 (def-gdb-preempt-display-buffer
3314 gdb-preemptively-display-disassembly-buffer
3315 'gdb-disassembly-buffer)
3317 (def-gdb-frame-for-buffer
3318 gdb-frame-disassembly-buffer
3319 'gdb-disassembly-buffer
3320 "Display disassembly in a new frame.")
3322 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3323 (let* ((frame (gdb-current-buffer-frame))
3324 (file (bindat-get-field frame 'fullname))
3325 (line (bindat-get-field frame 'line)))
3327 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3328 ;; If we're unable to get a file name / line for $PC, simply
3329 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3330 ;; 150 bytes) instructions.
3331 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3332 gdb-disassembly-handler
3333 ;; We update disassembly only after we have actual frame information
3334 ;; about all threads, so no there's `update' signal in this list
3335 '(start update-disassembly))
3337 (def-gdb-auto-update-handler
3338 gdb-disassembly-handler
3339 gdb-invalidate-disassembly
3340 gdb-disassembly-handler-custom
3343 (gdb-set-buffer-rules
3344 'gdb-disassembly-buffer
3345 'gdb-disassembly-buffer-name
3346 'gdb-disassembly-mode
3347 'gdb-invalidate-disassembly)
3349 (defvar gdb-disassembly-font-lock-keywords
3350 '(;; <__function.name+n>
3351 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3352 (1 font-lock-function-name-face))
3353 ;; 0xNNNNNNNN <__function.name+n>: opcode
3354 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3355 (4 font-lock-keyword-face))
3356 ;; %register(at least i386)
3357 ("%\\sw+" . font-lock-variable-name-face)
3358 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3359 (1 font-lock-comment-face)
3360 (2 font-lock-function-name-face))
3361 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3362 "Font lock keywords used in `gdb-disassembly-mode'.")
3364 (defvar gdb-disassembly-mode-map
3366 (let ((map (make-sparse-keymap)))
3367 (suppress-keymap map)
3368 (define-key map "q" 'kill-this-buffer)
3371 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3372 "Major mode for GDB disassembly information."
3373 ;; TODO Rename overlay variable for disassembly mode
3374 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3375 (setq fringes-outside-margins t)
3376 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3377 (set (make-local-variable 'font-lock-defaults)
3378 '(gdb-disassembly-font-lock-keywords))
3379 'gdb-invalidate-disassembly)
3381 (defun gdb-disassembly-handler-custom ()
3382 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3383 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3384 (table (make-gdb-table))
3386 (dolist (instr instructions)
3387 (gdb-table-add-row table
3389 (bindat-get-field instr 'address)
3391 ((func-name (bindat-get-field instr 'func-name))
3392 (offset (bindat-get-field instr 'offset)))
3394 (format "<%s+%s>:" func-name offset)
3396 (bindat-get-field instr 'inst)))
3397 (when (string-equal (bindat-get-field instr 'address)
3400 (setq marked-line (length (gdb-table-rows table)))
3401 (setq fringe-indicator-alist
3402 (if (string-equal gdb-frame-number "0")
3404 '((overlay-arrow . hollow-right-triangle)))))))
3405 (insert (gdb-table-string table " "))
3406 (gdb-disassembly-place-breakpoints)
3407 ;; Mark current position with overlay arrow and scroll window to
3410 (let ((window (get-buffer-window (current-buffer) 0)))
3411 (set-window-point window (gdb-mark-line marked-line
3412 gdb-disassembly-position))))
3414 (gdb-current-context-mode-name
3415 (concat "Disassembly: "
3416 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3418 (defun gdb-disassembly-place-breakpoints ()
3419 (gdb-remove-breakpoint-icons (point-min) (point-max))
3420 (dolist (breakpoint gdb-breakpoints-list)
3421 (let* ((breakpoint (cdr breakpoint))
3422 (bptno (bindat-get-field breakpoint 'number))
3423 (flag (bindat-get-field breakpoint 'enabled))
3424 (address (bindat-get-field breakpoint 'addr)))
3426 (goto-char (point-min))
3427 (if (re-search-forward (concat "^" address) nil t)
3428 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3431 (defvar gdb-breakpoints-header
3433 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3436 (gdb-propertize-header "Threads" gdb-threads-buffer
3437 "mouse-1: select" mode-line-highlight
3438 mode-line-inactive)))
3440 ;;; Breakpoints view
3441 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3442 "Major mode for gdb breakpoints."
3443 (setq header-line-format gdb-breakpoints-header)
3444 'gdb-invalidate-breakpoints)
3446 (defun gdb-toggle-breakpoint ()
3447 "Enable/disable breakpoint at current line of breakpoints buffer."
3451 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3454 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3457 (bindat-get-field breakpoint 'number)))
3458 (error "Not recognized as break/watchpoint line")))))
3460 (defun gdb-delete-breakpoint ()
3461 "Delete the breakpoint at current line of breakpoints buffer."
3465 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3467 (gud-basic-call (concat "-break-delete "
3468 (bindat-get-field breakpoint 'number)))
3469 (error "Not recognized as break/watchpoint line")))))
3471 (defun gdb-goto-breakpoint (&optional event)
3472 "Go to the location of breakpoint at current line of
3473 breakpoints buffer."
3474 (interactive (list last-input-event))
3475 (if event (posn-set-point (event-end event)))
3476 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3477 (let ((window (get-buffer-window gud-comint-buffer)))
3478 (if window (save-selected-window (select-window window))))
3481 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3483 (let ((bptno (bindat-get-field breakpoint 'number))
3484 (file (bindat-get-field breakpoint 'fullname))
3485 (line (bindat-get-field breakpoint 'line)))
3486 (save-selected-window
3487 (let* ((buffer (find-file-noselect
3488 (if (file-exists-p file) file
3489 (cdr (assoc bptno gdb-location-alist)))))
3490 (window (or (gdb-display-source-buffer buffer)
3491 (display-buffer buffer))))
3492 (setq gdb-source-window window)
3493 (with-current-buffer buffer
3494 (goto-char (point-min))
3495 (forward-line (1- (string-to-number line)))
3496 (set-window-point window (point))))))
3497 (error "Not recognized as break/watchpoint line")))))
3500 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3502 (def-gdb-trigger-and-handler
3503 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3504 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3507 (gdb-set-buffer-rules
3509 'gdb-stack-buffer-name
3511 'gdb-invalidate-frames)
3513 (defun gdb-frame-location (frame)
3514 "Return \" of file:line\" or \" of library\" for structure FRAME.
3516 FRAME must have either \"file\" and \"line\" members or \"from\"
3518 (let ((file (bindat-get-field frame 'file))
3519 (line (bindat-get-field frame 'line))
3520 (from (bindat-get-field frame 'from)))
3521 (let ((res (or (and file line (concat file ":" line))
3523 (if res (concat " of " res) ""))))
3525 (defun gdb-stack-list-frames-custom ()
3526 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3527 (table (make-gdb-table)))
3528 (set-marker gdb-stack-position nil)
3529 (dolist (frame stack)
3530 (gdb-table-add-row table
3532 (bindat-get-field frame 'level)
3535 (bindat-get-field frame 'func)
3536 (if gdb-stack-buffer-locations
3537 (gdb-frame-location frame) "")
3538 (if gdb-stack-buffer-addresses
3539 (concat " at " (bindat-get-field frame 'addr)) "")))
3540 `(mouse-face highlight
3541 help-echo "mouse-2, RET: Select frame"
3543 (insert (gdb-table-string table " ")))
3544 (when (and gdb-frame-number
3545 (gdb-buffer-shows-main-thread-p))
3546 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3547 gdb-stack-position))
3549 (gdb-current-context-mode-name "Frames")))
3551 (defun gdb-stack-buffer-name ()
3552 (gdb-current-context-buffer-name
3553 (concat "stack frames of " (gdb-get-target-string))))
3555 (def-gdb-display-buffer
3556 gdb-display-stack-buffer
3558 "Display backtrace of current stack.")
3560 (def-gdb-preempt-display-buffer
3561 gdb-preemptively-display-stack-buffer
3562 'gdb-stack-buffer nil t)
3564 (def-gdb-frame-for-buffer
3565 gdb-frame-stack-buffer
3567 "Display backtrace of current stack in a new frame.")
3569 (defvar gdb-frames-mode-map
3570 (let ((map (make-sparse-keymap)))
3571 (suppress-keymap map)
3572 (define-key map "q" 'kill-this-buffer)
3573 (define-key map "\r" 'gdb-select-frame)
3574 (define-key map [mouse-2] 'gdb-select-frame)
3575 (define-key map [follow-link] 'mouse-face)
3578 (defvar gdb-frames-font-lock-keywords
3579 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3580 "Font lock keywords used in `gdb-frames-mode'.")
3582 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3583 "Major mode for gdb call stack."
3584 (setq gdb-stack-position (make-marker))
3585 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3586 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3587 (set (make-local-variable 'font-lock-defaults)
3588 '(gdb-frames-font-lock-keywords))
3589 'gdb-invalidate-frames)
3591 (defun gdb-select-frame (&optional event)
3592 "Select the frame and display the relevant source."
3593 (interactive (list last-input-event))
3594 (if event (posn-set-point (event-end event)))
3595 (let ((frame (get-text-property (point) 'gdb-frame)))
3597 (if (gdb-buffer-shows-main-thread-p)
3598 (let ((new-level (bindat-get-field frame 'level)))
3599 (setq gdb-frame-number new-level)
3600 (gdb-input (concat "-stack-select-frame " new-level)
3603 (error "Could not select frame for non-current thread"))
3604 (error "Not recognized as frame line"))))
3608 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3609 (def-gdb-trigger-and-handler
3610 gdb-invalidate-locals
3611 (concat (gdb-current-context-command "-stack-list-locals")
3613 gdb-locals-handler gdb-locals-handler-custom
3616 (gdb-set-buffer-rules
3618 'gdb-locals-buffer-name
3620 'gdb-invalidate-locals)
3622 (defvar gdb-locals-watch-map
3623 (let ((map (make-sparse-keymap)))
3624 (suppress-keymap map)
3625 (define-key map "\r" 'gud-watch)
3626 (define-key map [mouse-2] 'gud-watch)
3628 "Keymap to create watch expression of a complex data type local variable.")
3630 (defvar gdb-edit-locals-map-1
3631 (let ((map (make-sparse-keymap)))
3632 (suppress-keymap map)
3633 (define-key map "\r" 'gdb-edit-locals-value)
3634 (define-key map [mouse-2] 'gdb-edit-locals-value)
3636 "Keymap to edit value of a simple data type local variable.")
3638 (defun gdb-edit-locals-value (&optional event)
3639 "Assign a value to a variable displayed in the locals buffer."
3640 (interactive (list last-input-event))
3642 (if event (posn-set-point (event-end event)))
3644 (let* ((var (bindat-get-field
3645 (get-text-property (point) 'gdb-local-variable) 'name))
3646 (value (read-string (format "New value (%s): " var))))
3648 (concat "-gdb-set variable " var " = " value)))))
3650 ;; Don't display values of arrays or structures.
3651 ;; These can be expanded using gud-watch.
3652 (defun gdb-locals-handler-custom ()
3653 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3654 (table (make-gdb-table)))
3655 (dolist (local locals-list)
3656 (let ((name (bindat-get-field local 'name))
3657 (value (bindat-get-field local 'value))
3658 (type (bindat-get-field local 'type)))
3660 (string-match "\\0x" value))
3661 (add-text-properties 0 (length name)
3662 `(mouse-face highlight
3663 help-echo "mouse-2: create watch expression"
3664 local-map ,gdb-locals-watch-map)
3666 (add-text-properties 0 (length value)
3667 `(mouse-face highlight
3668 help-echo "mouse-2: edit value"
3669 local-map ,gdb-edit-locals-map-1)
3674 (propertize type 'font-lock-face font-lock-type-face)
3675 (propertize name 'font-lock-face font-lock-variable-name-face)
3677 `(gdb-local-variable ,local))))
3678 (insert (gdb-table-string table " "))
3680 (gdb-current-context-mode-name
3682 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3684 (defvar gdb-locals-header
3686 (gdb-propertize-header "Locals" gdb-locals-buffer
3689 (gdb-propertize-header "Registers" gdb-registers-buffer
3690 "mouse-1: select" mode-line-highlight
3691 mode-line-inactive)))
3693 (defvar gdb-locals-mode-map
3694 (let ((map (make-sparse-keymap)))
3695 (suppress-keymap map)
3696 (define-key map "q" 'kill-this-buffer)
3697 (define-key map "\t" (lambda ()
3699 (gdb-set-window-buffer
3700 (gdb-get-buffer-create
3701 'gdb-registers-buffer
3702 gdb-thread-number) t)))
3705 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3706 "Major mode for gdb locals."
3707 (setq header-line-format gdb-locals-header)
3708 'gdb-invalidate-locals)
3710 (defun gdb-locals-buffer-name ()
3711 (gdb-current-context-buffer-name
3712 (concat "locals of " (gdb-get-target-string))))
3714 (def-gdb-display-buffer
3715 gdb-display-locals-buffer
3717 "Display local variables of current stack and their values.")
3719 (def-gdb-preempt-display-buffer
3720 gdb-preemptively-display-locals-buffer
3721 'gdb-locals-buffer nil t)
3723 (def-gdb-frame-for-buffer
3724 gdb-frame-locals-buffer
3726 "Display local variables of current stack and their values in a new frame.")
3729 ;; Registers buffer.
3731 (def-gdb-trigger-and-handler
3732 gdb-invalidate-registers
3733 (concat (gdb-current-context-command "-data-list-register-values") " x")
3734 gdb-registers-handler
3735 gdb-registers-handler-custom
3738 (gdb-set-buffer-rules
3739 'gdb-registers-buffer
3740 'gdb-registers-buffer-name
3742 'gdb-invalidate-registers)
3744 (defun gdb-registers-handler-custom ()
3745 (when gdb-register-names
3746 (let ((register-values
3747 (bindat-get-field (gdb-json-partial-output) 'register-values))
3748 (table (make-gdb-table)))
3749 (dolist (register register-values)
3750 (let* ((register-number (bindat-get-field register 'number))
3751 (value (bindat-get-field register 'value))
3752 (register-name (nth (string-to-number register-number)
3753 gdb-register-names)))
3757 (propertize register-name
3758 'font-lock-face font-lock-variable-name-face)
3759 (if (member register-number gdb-changed-registers)
3760 (propertize value 'font-lock-face font-lock-warning-face)
3762 `(mouse-face highlight
3763 help-echo "mouse-2: edit value"
3764 gdb-register-name ,register-name))))
3765 (insert (gdb-table-string table " ")))
3767 (gdb-current-context-mode-name "Registers"))))
3769 (defun gdb-edit-register-value (&optional event)
3770 "Assign a value to a register displayed in the registers buffer."
3771 (interactive (list last-input-event))
3773 (if event (posn-set-point (event-end event)))
3775 (let* ((var (bindat-get-field
3776 (get-text-property (point) 'gdb-register-name)))
3777 (value (read-string (format "New value (%s): " var))))
3779 (concat "-gdb-set variable $" var " = " value)))))
3781 (defvar gdb-registers-mode-map
3782 (let ((map (make-sparse-keymap)))
3783 (suppress-keymap map)
3784 (define-key map "\r" 'gdb-edit-register-value)
3785 (define-key map [mouse-2] 'gdb-edit-register-value)
3786 (define-key map "q" 'kill-this-buffer)
3787 (define-key map "\t" (lambda ()
3789 (gdb-set-window-buffer
3790 (gdb-get-buffer-create
3792 gdb-thread-number) t)))
3795 (defvar gdb-registers-header
3797 (gdb-propertize-header "Locals" gdb-locals-buffer
3798 "mouse-1: select" mode-line-highlight
3801 (gdb-propertize-header "Registers" gdb-registers-buffer
3802 nil nil mode-line)))
3804 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3805 "Major mode for gdb registers."
3806 (setq header-line-format gdb-registers-header)
3807 'gdb-invalidate-registers)
3809 (defun gdb-registers-buffer-name ()
3810 (gdb-current-context-buffer-name
3811 (concat "registers of " (gdb-get-target-string))))
3813 (def-gdb-display-buffer
3814 gdb-display-registers-buffer
3815 'gdb-registers-buffer
3816 "Display integer register contents.")
3818 (def-gdb-preempt-display-buffer
3819 gdb-preemptively-display-registers-buffer
3820 'gdb-registers-buffer nil t)
3822 (def-gdb-frame-for-buffer
3823 gdb-frame-registers-buffer
3824 'gdb-registers-buffer
3825 "Display integer register contents in a new frame.")
3827 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3828 (defun gdb-get-changed-registers ()
3829 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3830 (not (gdb-pending-p 'gdb-get-changed-registers)))
3831 (gdb-input "-data-list-changed-registers"
3832 'gdb-changed-registers-handler)
3833 (gdb-add-pending 'gdb-get-changed-registers)))
3835 (defun gdb-changed-registers-handler ()
3836 (gdb-delete-pending 'gdb-get-changed-registers)
3837 (setq gdb-changed-registers nil)
3838 (dolist (register-number
3839 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3840 (push register-number gdb-changed-registers)))
3842 (defun gdb-register-names-handler ()
3843 ;; Don't use gdb-pending-triggers because this handler is called
3844 ;; only once (in gdb-init-1)
3845 (setq gdb-register-names nil)
3846 (dolist (register-name
3847 (bindat-get-field (gdb-json-partial-output) 'register-names))
3848 (push register-name gdb-register-names))
3849 (setq gdb-register-names (reverse gdb-register-names)))
3852 (defun gdb-get-source-file-list ()
3853 "Create list of source files for current GDB session.
3854 If buffers already exist for any of these files, gud-minor-mode
3856 (goto-char (point-min))
3857 (while (re-search-forward gdb-source-file-regexp nil t)
3858 (push (match-string 1) gdb-source-file-list))
3859 (dolist (buffer (buffer-list))
3860 (with-current-buffer buffer
3861 (when (member buffer-file-name gdb-source-file-list)
3862 (gdb-init-buffer)))))
3864 (defun gdb-get-main-selected-frame ()
3865 "Trigger for `gdb-frame-handler' which uses main current
3866 thread. Called from `gdb-update'."
3867 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3869 (gdb-input (gdb-current-context-command "-stack-info-frame")
3871 (gdb-add-pending 'gdb-get-main-selected-frame))))
3873 (defun gdb-frame-handler ()
3874 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3875 overlay arrow in source buffer."
3876 (gdb-delete-pending 'gdb-get-main-selected-frame)
3877 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3879 (setq gdb-selected-frame (bindat-get-field frame 'func))
3880 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3881 (setq gdb-frame-number (bindat-get-field frame 'level))
3882 (setq gdb-frame-address (bindat-get-field frame 'addr))
3883 (let ((line (bindat-get-field frame 'line)))
3884 (setq gdb-selected-line (and line (string-to-number line)))
3885 (when (and gdb-selected-file gdb-selected-line)
3886 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3887 (gud-display-frame)))
3888 (if gud-overlay-arrow-position
3889 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3890 (position (marker-position gud-overlay-arrow-position)))
3892 (with-current-buffer buffer
3893 (setq fringe-indicator-alist
3894 (if (string-equal gdb-frame-number "0")
3896 '((overlay-arrow . hollow-right-triangle))))
3897 (setq gud-overlay-arrow-position (make-marker))
3898 (set-marker gud-overlay-arrow-position position))))))))
3900 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3902 (defun gdb-get-prompt ()
3903 "Find prompt for GDB session."
3904 (goto-char (point-min))
3905 (setq gdb-prompt-name nil)
3906 (re-search-forward gdb-prompt-name-regexp nil t)
3907 (setq gdb-prompt-name (match-string 1))
3908 ;; Insert first prompt.
3909 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3911 ;;;; Window management
3912 (defun gdb-display-buffer (buf dedicated &optional frame)
3915 If BUF is already displayed in some window, show it, deiconifying
3916 the frame if necessary. Otherwise, find least recently used
3917 window and show BUF there, if the window is not used for GDB
3918 already, in which case that window is split first."
3919 (let ((answer (get-buffer-window buf (or frame 0))))
3921 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3922 (let ((window (get-lru-window)))
3923 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3925 (let ((largest (get-largest-window)))
3926 (setq answer (split-window largest))
3927 (set-window-buffer answer buf)
3928 (set-window-dedicated-p answer dedicated)
3930 (set-window-buffer window buf)
3933 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3934 "Find window displaying a buffer with the same
3935 `gdb-buffer-type' as BUF and show BUF there. If no such window
3936 exists, just call `gdb-display-buffer' for BUF. If the window
3937 found is already dedicated, split window according to
3938 SPLIT-HORIZONTAL and show BUF in the new window."
3940 (when (not (get-buffer-window buf))
3941 (let* ((buf-type (gdb-buffer-type buf))
3943 (get-window-with-predicate
3946 (gdb-buffer-type (window-buffer w)))
3947 (not (window-dedicated-p w)))))))
3949 (set-window-buffer existing-window buf)
3950 (let ((dedicated-window
3951 (get-window-with-predicate
3954 (gdb-buffer-type (window-buffer w)))))))
3955 (if dedicated-window
3957 (split-window dedicated-window nil split-horizontal) buf)
3958 (gdb-display-buffer buf t))))))
3959 (error "Null buffer")))
3961 ;;; Shared keymap initialization:
3963 (let ((menu (make-sparse-keymap "GDB-Windows")))
3964 (define-key gud-menu-map [displays]
3965 `(menu-item "GDB-Windows" ,menu
3966 :visible (eq gud-minor-mode 'gdbmi)))
3967 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3968 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3969 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3970 (define-key menu [disassembly]
3971 '("Disassembly" . gdb-display-disassembly-buffer))
3972 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3973 (define-key menu [inferior]
3974 '("IO" . gdb-display-io-buffer))
3975 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3976 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3977 (define-key menu [breakpoints]
3978 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3980 (let ((menu (make-sparse-keymap "GDB-Frames")))
3981 (define-key gud-menu-map [frames]
3982 `(menu-item "GDB-Frames" ,menu
3983 :visible (eq gud-minor-mode 'gdbmi)))
3984 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3985 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3986 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3987 (define-key menu [disassembly]
3988 '("Disassembly" . gdb-frame-disassembly-buffer))
3989 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3990 (define-key menu [inferior]
3991 '("IO" . gdb-frame-io-buffer))
3992 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3993 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3994 (define-key menu [breakpoints]
3995 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3997 (let ((menu (make-sparse-keymap "GDB-MI")))
3998 (define-key menu [gdb-customize]
3999 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4000 :help "Customize Gdb Graphical Mode options."))
4001 (define-key menu [gdb-many-windows]
4002 '(menu-item "Display Other Windows" gdb-many-windows
4003 :help "Toggle display of locals, stack and breakpoint information"
4004 :button (:toggle . gdb-many-windows)))
4005 (define-key menu [gdb-restore-windows]
4006 '(menu-item "Restore Window Layout" gdb-restore-windows
4007 :help "Restore standard layout for debug session."))
4008 (define-key menu [sep1]
4010 (define-key menu [all-threads]
4011 '(menu-item "GUD controls all threads"
4014 (setq gdb-gud-control-all-threads t))
4015 :help "GUD start/stop commands apply to all threads"
4016 :button (:radio . gdb-gud-control-all-threads)))
4017 (define-key menu [current-thread]
4018 '(menu-item "GUD controls current thread"
4021 (setq gdb-gud-control-all-threads nil))
4022 :help "GUD start/stop commands apply to current thread only"
4023 :button (:radio . (not gdb-gud-control-all-threads))))
4024 (define-key menu [sep2]
4026 (define-key menu [gdb-customize-reasons]
4027 '(menu-item "Customize switching..."
4030 (customize-option 'gdb-switch-reasons))))
4031 (define-key menu [gdb-switch-when-another-stopped]
4032 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4033 gdb-switch-when-another-stopped
4034 "Automatically switch to stopped thread"
4035 "GDB thread switching %s"
4036 "Switch to stopped thread"))
4037 (define-key gud-menu-map [mi]
4038 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4040 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4041 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4042 ;; tool-bar-local-item-from-menu here because it appends new buttons
4043 ;; to toolbar from right to left while we want our A/T throttle to
4044 ;; show up right before Run button.
4045 (define-key-after gud-tool-bar-map [all-threads]
4046 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4047 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4048 :visible (and (eq gud-minor-mode 'gdbmi)
4050 (not gdb-gud-control-all-threads)))
4053 (define-key-after gud-tool-bar-map [current-thread]
4054 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4055 :image (find-image '((:type xpm :file "gud/all.xpm")))
4056 :visible (and (eq gud-minor-mode 'gdbmi)
4058 gdb-gud-control-all-threads))
4061 (defun gdb-frame-gdb-buffer ()
4062 "Display GUD buffer in a new frame."
4064 (display-buffer-other-frame gud-comint-buffer))
4066 (defun gdb-display-gdb-buffer ()
4067 "Display GUD buffer."
4069 (pop-to-buffer gud-comint-buffer nil 0))
4071 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4072 "Set buffer of selected window to NAME and dedicate window.
4074 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4075 window is dedicated."
4076 (unless window (setq window (selected-window)))
4077 (when ignore-dedicated
4078 (set-window-dedicated-p window nil))
4079 (set-window-buffer window (get-buffer name))
4080 (set-window-dedicated-p window t))
4082 (defun gdb-setup-windows ()
4083 "Layout the window pattern for `gdb-many-windows'."
4084 (gdb-display-locals-buffer)
4085 (gdb-display-stack-buffer)
4086 (delete-other-windows)
4087 (gdb-display-breakpoints-buffer)
4088 (delete-other-windows)
4090 (switch-to-buffer gud-comint-buffer)
4091 (let ((win0 (selected-window))
4092 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4093 (win2 (split-window nil ( / (window-height) 3)))
4094 (win3 (split-window-right)))
4095 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4096 (select-window win2)
4099 (if gud-last-last-frame
4100 (gud-find-file (car gud-last-last-frame))
4102 (gud-find-file gdb-main-file)
4103 ;; Put buffer list in window if we
4104 ;; can't find a source file.
4105 (list-buffers-noselect))))
4106 (setq gdb-source-window (selected-window))
4107 (let ((win4 (split-window-right)))
4108 (gdb-set-window-buffer
4109 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4110 (select-window win1)
4111 (gdb-set-window-buffer (gdb-stack-buffer-name))
4112 (let ((win5 (split-window-right)))
4113 (gdb-set-window-buffer (if gdb-show-threads-by-default
4114 (gdb-threads-buffer-name)
4115 (gdb-breakpoints-buffer-name))
4117 (select-window win0)))
4119 (define-minor-mode gdb-many-windows
4120 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4121 In this case it starts with two windows: one displaying the GUD
4122 buffer and the other with the source file with the main routine
4123 of the debugged program. Non-nil means display the layout shown for
4128 (if (and gud-comint-buffer
4129 (buffer-name gud-comint-buffer))
4131 (gdb-restore-windows))))
4133 (defun gdb-restore-windows ()
4134 "Restore the basic arrangement of windows used by gdb.
4135 This arrangement depends on the value of `gdb-many-windows'."
4137 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4138 (delete-other-windows)
4139 (if gdb-many-windows
4141 (when (or gud-last-last-frame gdb-show-main)
4142 (let ((win (split-window)))
4145 (if gud-last-last-frame
4146 (gud-find-file (car gud-last-last-frame))
4147 (gud-find-file gdb-main-file)))
4148 (setq gdb-source-window win)))))
4150 ;; Called from `gud-sentinel' in gud.el:
4152 "Exit a debugging session cleanly.
4153 Kills the gdb buffers, and resets variables and the source buffers."
4154 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4155 ;; process. This pty must be deleted explicitly.
4156 (let ((pty (get-process "gdb-inferior")))
4157 (if pty (delete-process pty)))
4158 ;; Find gdb-mi buffers and kill them.
4159 (dolist (buffer (buffer-list))
4160 (unless (eq buffer gud-comint-buffer)
4161 (with-current-buffer buffer
4162 (if (eq gud-minor-mode 'gdbmi)
4163 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4165 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4166 (setq gud-minor-mode nil)
4167 (kill-local-variable 'tool-bar-map)
4168 (kill-local-variable 'gdb-define-alist))))))
4169 (setq gdb-disassembly-position nil)
4170 (setq overlay-arrow-variable-list
4171 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4172 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4173 (setq gdb-stack-position nil)
4174 (setq overlay-arrow-variable-list
4175 (delq 'gdb-stack-position overlay-arrow-variable-list))
4176 (setq gdb-thread-position nil)
4177 (setq overlay-arrow-variable-list
4178 (delq 'gdb-thread-position overlay-arrow-variable-list))
4179 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4180 (setq gud-running nil)
4181 (setq gdb-active-process nil)
4182 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4184 (defun gdb-get-source-file ()
4185 "Find the source file where the program starts and display it with related
4186 buffers, if required."
4187 (goto-char (point-min))
4188 (if (re-search-forward gdb-source-file-regexp nil t)
4189 (setq gdb-main-file (match-string 1)))
4190 (if gdb-many-windows
4192 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4193 (if (and gdb-show-main gdb-main-file)
4194 (let ((pop-up-windows t))
4195 (display-buffer (gud-find-file gdb-main-file)))))
4196 (gdb-force-mode-line-update
4197 (propertize "ready" 'face font-lock-variable-name-face)))
4200 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4201 "Put string PUTSTRING in front of POS in the current buffer.
4202 PUTSTRING is displayed by putting an overlay into the current buffer with a
4203 `before-string' string that has a `display' property whose value is
4205 (let ((string (make-string 1 ?x))
4206 (buffer (current-buffer)))
4207 (setq putstring (copy-sequence putstring))
4208 (let ((overlay (make-overlay pos pos buffer))
4210 (list (list 'margin 'left-margin) putstring))))
4211 (put-text-property 0 1 'display prop string)
4213 (add-text-properties 0 1 sprops string))
4214 (overlay-put overlay 'put-break t)
4215 (overlay-put overlay 'before-string string))))
4217 ;;from remove-images
4218 (defun gdb-remove-strings (start end &optional buffer)
4219 "Remove strings between START and END in BUFFER.
4220 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4221 BUFFER nil or omitted means use the current buffer."
4223 (setq buffer (current-buffer)))
4224 (dolist (overlay (overlays-in start end))
4225 (when (overlay-get overlay 'put-break)
4226 (delete-overlay overlay))))
4228 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4229 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4230 (start (- (car posns) 1))
4231 (end (+ (cdr posns) 1))
4232 (putstring (if enabled "B" "b"))
4233 (source-window (get-buffer-window (current-buffer) 0)))
4234 (add-text-properties
4235 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4238 (add-text-properties
4239 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4240 (add-text-properties
4241 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4242 (gdb-remove-breakpoint-icons start end)
4243 (if (display-images-p)
4244 (if (>= (or left-fringe-width
4245 (if source-window (car (window-fringes source-window)))
4246 gdb-buffer-fringe-width) 8)
4249 `(left-fringe breakpoint
4252 'breakpoint-disabled))
4254 'gdb-enabled enabled)
4255 (when (< left-margin-width 2)
4256 (save-current-buffer
4257 (setq left-margin-width 2)
4261 left-margin-width right-margin-width))))
4264 (or breakpoint-enabled-icon
4265 (setq breakpoint-enabled-icon
4266 (find-image `((:type xpm :data
4267 ,breakpoint-xpm-data
4268 :ascent 100 :pointer hand)
4270 ,breakpoint-enabled-pbm-data
4271 :ascent 100 :pointer hand)))))
4272 (or breakpoint-disabled-icon
4273 (setq breakpoint-disabled-icon
4274 (find-image `((:type xpm :data
4275 ,breakpoint-xpm-data
4276 :conversion disabled
4277 :ascent 100 :pointer hand)
4279 ,breakpoint-disabled-pbm-data
4280 :ascent 100 :pointer hand))))))
4284 (when (< left-margin-width 2)
4285 (save-current-buffer
4286 (setq left-margin-width 2)
4287 (let ((window (get-buffer-window (current-buffer) 0)))
4290 window left-margin-width right-margin-width)))))
4292 (propertize putstring
4294 'breakpoint-enabled 'breakpoint-disabled))
4297 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4298 (gdb-remove-strings start end)
4299 (if (display-images-p)
4300 (remove-images start end))
4302 (setq left-margin-width 0)
4303 (let ((window (get-buffer-window (current-buffer) 0)))
4306 window left-margin-width right-margin-width)))))
4309 ;;; Functions for inline completion.
4311 (defvar gud-gdb-fetch-lines-in-progress)
4312 (defvar gud-gdb-fetch-lines-string)
4313 (defvar gud-gdb-fetch-lines-break)
4314 (defvar gud-gdb-fetched-lines)
4316 (defun gud-gdbmi-completions (context command)
4317 "Completion table for GDB/MI commands.
4318 COMMAND is the prefix for which we seek completion.
4319 CONTEXT is the text before COMMAND on the line."
4320 (let ((gud-gdb-fetch-lines-in-progress t)
4321 (gud-gdb-fetch-lines-string nil)
4322 (gud-gdb-fetch-lines-break (length context))
4323 (gud-gdb-fetched-lines nil)
4324 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4325 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4327 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4328 (gdb-input (concat "complete " context command)
4329 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4330 (while gud-gdb-fetch-lines-in-progress
4331 (accept-process-output (get-buffer-process gud-comint-buffer))))
4332 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4334 (defun gud-gdbmi-fetch-lines-filter (string)
4335 "Custom filter function for `gud-gdbmi-completions'."
4336 (setq string (concat gud-gdb-fetch-lines-string
4337 (gud-gdbmi-marker-filter string)))
4338 (while (string-match "\n" string)
4339 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4340 gud-gdb-fetched-lines)
4341 (setq string (substring string (match-end 0))))
4346 ;;; gdb-mi.el ends here