1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2017 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
6 ;; Maintainer: emacs-devel@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 macOS works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of macOS.
76 ;; This requires GDB version 7.0 or later as earlier versions do not
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?
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.
196 Only used for files that Emacs can't find.")
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-list
'()
210 "List of gdb-handler keeping track of all pending GDB commands.")
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-local gdb-buffer-type nil
231 "One of the symbols bound in `gdb-buffer-rules'.")
233 (defvar gdb-output-sink
'nil
234 "The disposition of the output of the current gdb command.
235 Possible values are these symbols:
237 `user' -- gdb output should be copied to the GUD buffer
240 `emacs' -- output should be collected in the partial-output-buffer
241 for subsequent processing by a command. This is the
242 disposition of output generated by commands that
243 gdb mode sends to gdb on its own behalf.")
245 (defcustom gdb-discard-unordered-replies t
246 "Non-nil means discard any out-of-order GDB replies.
247 This protects against lost GDB replies, assuming that GDB always
248 replies in the same order as Emacs sends commands. When receiving a
249 reply with a given token-number, assume any pending messages with a
250 lower token-number are out-of-order."
255 (cl-defstruct gdb-handler
256 "Data required to handle the reply of a command sent to GDB."
257 ;; Prefix of the command sent to GDB. The GDB reply for this command
258 ;; will be prefixed with this same TOKEN-NUMBER
259 (token-number nil
:read-only t
)
260 ;; Callback to invoke when the reply is received from GDB
261 (function nil
:read-only t
)
262 ;; PENDING-TRIGGER is used to prevent congestion: Emacs won't send
263 ;; two requests with the same PENDING-TRIGGER until a reply is received
264 ;; for the first one."
265 (pending-trigger nil
))
267 (defun gdb-add-handler (token-number handler-function
&optional pending-trigger
)
268 "Insert a new GDB command handler in `gdb-handler-list'.
269 Handlers are used to keep track of the commands sent to GDB
270 and to handle the replies received.
271 Upon reception of a reply prefixed with TOKEN-NUMBER,
272 invoke the callback HANDLER-FUNCTION.
273 If PENDING-TRIGGER is specified, no new GDB commands will be
274 sent with this same PENDING-TRIGGER until a reply is received
277 (push (make-gdb-handler :token-number token-number
278 :function handler-function
279 :pending-trigger pending-trigger
)
282 (defun gdb-delete-handler (token-number)
283 "Remove the handler TOKEN-NUMBER from `gdb-handler-list'.
284 Additionally, if `gdb-discard-unordered-replies' is non-nil,
285 discard all handlers having a token number less than TOKEN-NUMBER."
286 (if gdb-discard-unordered-replies
288 (setq gdb-handler-list
291 "Discard any HANDLER with a token number `<=' than TOKEN-NUMBER."
292 (when (< (gdb-handler-token-number handler
) token-number
)
293 (message "WARNING! Discarding GDB handler with token #%d\n"
294 (gdb-handler-token-number handler
)))
295 (<= (gdb-handler-token-number handler
) token-number
))
298 (setq gdb-handler-list
301 "Discard any HANDLER with a token number `eq' to TOKEN-NUMBER."
302 (eq (gdb-handler-token-number handler
) token-number
))
305 (defun gdb-get-handler-function (token-number)
306 "Return the function callback registered with the handler TOKEN-NUMBER."
307 (gdb-handler-function
308 (cl-find-if (lambda (handler) (eq (gdb-handler-token-number handler
)
313 (defun gdb-pending-handler-p (pending-trigger)
314 "Return non-nil if a command handler is pending with trigger PENDING-TRIGGER."
315 (cl-find-if (lambda (handler) (eq (gdb-handler-pending-trigger handler
)
320 (defun gdb-handle-reply (token-number)
321 "Handle the GDB reply TOKEN-NUMBER.
322 This invokes the handler registered with this token number
323 in `gdb-handler-list' and clears all pending handlers invalidated
324 by the reception of this reply."
325 (let ((handler-function (gdb-get-handler-function token-number
)))
326 (when handler-function
327 (funcall handler-function
)
328 (gdb-delete-handler token-number
))))
330 (defun gdb-remove-all-pending-triggers ()
331 "Remove all pending triggers from gdb-handler-list.
332 The handlers are left in gdb-handler-list so that replies received
333 from GDB could still be handled. However, removing the pending triggers
334 allows Emacs to send new commands even if replies of previous commands
335 were not yet received."
336 (dolist (handler gdb-handler-list
)
337 (setf (gdb-handler-pending-trigger handler
) nil
)))
339 (defmacro gdb-wait-for-pending
(&rest body
)
340 "Wait for all pending GDB commands to finish and evaluate BODY.
342 This function checks every 0.5 seconds if there are any pending
343 triggers in `gdb-handler-list'."
347 (if (not (cl-find-if (lambda (handler)
348 (gdb-handler-pending-trigger handler
))
351 (gdb-wait-for-pending ,@body
)))))
355 (defmacro gdb-add-subscriber
(publisher subscriber
)
356 "Register new PUBLISHER's SUBSCRIBER.
358 SUBSCRIBER must be a pair, where cdr is a function of one
359 argument (see `gdb-emit-signal')."
360 `(add-to-list ',publisher
,subscriber t
))
362 (defmacro gdb-delete-subscriber
(publisher subscriber
)
363 "Unregister SUBSCRIBER from PUBLISHER."
364 `(setq ,publisher
(delete ,subscriber
367 (defun gdb-get-subscribers (publisher)
370 (defun gdb-emit-signal (publisher &optional signal
)
371 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
372 (dolist (subscriber (gdb-get-subscribers publisher
))
373 (funcall (cdr subscriber
) signal
)))
375 (defvar gdb-buf-publisher
'()
376 "Used to invalidate GDB buffers by emitting a signal in `gdb-update'.
377 Must be a list of pairs with cars being buffers and cdr's being
378 valid signal handlers.")
381 "GDB graphical interface"
383 :link
'(info-link "(emacs)GDB Graphical Interface")
386 (defgroup gdb-non-stop nil
387 "GDB non-stop debugging settings"
391 (defgroup gdb-buffers nil
396 (defcustom gdb-debug-log-max
128
397 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
399 :type
'(choice (integer :tag
"Number of elements")
400 (const :tag
"Unlimited" nil
))
403 (defcustom gdb-non-stop-setting t
404 "When in non-stop mode, stopped threads can be examined while
405 other threads continue to execute.
407 GDB session needs to be restarted for this setting to take effect."
412 ;; TODO Some commands can't be called with --all (give a notice about
413 ;; it in setting doc)
414 (defcustom gdb-gud-control-all-threads t
415 "When non-nil, GUD execution commands affect all threads when
416 in non-stop mode. Otherwise, only current thread is affected."
421 (defcustom gdb-switch-reasons t
422 "List of stop reasons for which Emacs should switch thread.
423 When t, switch to stopped thread no matter what the reason was.
424 When nil, never switch to stopped thread automatically.
426 This setting is used in non-stop mode only. In all-stop mode,
427 Emacs always switches to the thread which caused the stop."
428 ;; exited, exited-normally and exited-signaled are not
429 ;; thread-specific stop reasons and therefore are not included in
432 (const :tag
"All reasons" t
)
433 (set :tag
"Selection of reasons..."
434 (const :tag
"A breakpoint was reached." "breakpoint-hit")
435 (const :tag
"A watchpoint was triggered." "watchpoint-trigger")
436 (const :tag
"A read watchpoint was triggered."
437 "read-watchpoint-trigger")
438 (const :tag
"An access watchpoint was triggered."
439 "access-watchpoint-trigger")
440 (const :tag
"Function finished execution." "function-finished")
441 (const :tag
"Location reached." "location-reached")
442 (const :tag
"Watchpoint has gone out of scope"
444 (const :tag
"End of stepping range reached."
445 "end-stepping-range")
446 (const :tag
"Signal received (like interruption)."
448 (const :tag
"None" nil
))
451 :link
'(info-link "(gdb)GDB/MI Async Records"))
453 (defcustom gdb-stopped-functions nil
454 "List of functions called whenever GDB stops.
456 Each function takes one argument, a parsed MI response, which
457 contains fields of corresponding MI *stopped async record:
459 ((stopped-threads . \"all\")
461 (frame (line . \"38\")
462 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
463 (file . \"server.c\")
464 (args ((value . \"0x804b038\")
467 (addr . \"0x0804869e\"))
468 (reason . \"end-stepping-range\"))
470 Note that \"reason\" is only present in non-stop debugging mode.
472 `bindat-get-field' may be used to access the fields of response.
474 Each function is called after the new current thread was selected
475 and GDB buffers were updated in `gdb-stopped'."
476 :type
'(repeat function
)
479 :link
'(info-link "(gdb)GDB/MI Async Records"))
481 (defcustom gdb-switch-when-another-stopped t
482 "When nil, don't switch to stopped thread if some other
483 stopped thread is already selected."
488 (defcustom gdb-stack-buffer-locations t
489 "Show file information or library names in stack buffers."
494 (defcustom gdb-stack-buffer-addresses nil
495 "Show frame addresses in stack buffers."
500 (defcustom gdb-thread-buffer-verbose-names t
501 "Show long thread names in threads buffer."
506 (defcustom gdb-thread-buffer-arguments t
507 "Show function arguments in threads buffer."
512 (defcustom gdb-thread-buffer-locations t
513 "Show file information or library names in threads buffer."
518 (defcustom gdb-thread-buffer-addresses nil
519 "Show addresses for thread frames in threads buffer."
524 (defcustom gdb-show-threads-by-default nil
525 "Show threads list buffer instead of breakpoints list by default."
530 (defvar gdb-debug-log nil
531 "List of commands sent to and replies received from GDB.
532 Most recent commands are listed first. This list stores only the last
533 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
536 (define-minor-mode gdb-enable-debug
537 "Toggle logging of transaction between Emacs and Gdb.
538 The log is stored in `gdb-debug-log' as an alist with elements
539 whose cons is send, send-item or recv and whose cdr is the string
540 being transferred. This list may grow up to a size of
541 `gdb-debug-log-max' after which the oldest element (at the end of
542 the list) is deleted every time a new one is added (at the front)."
547 (defcustom gdb-cpp-define-alist-program
"gcc -E -dM -"
548 "Shell command for generating a list of defined macros in a source file.
549 This list is used to display the #define directive associated
550 with an identifier as a tooltip. It works in a debug session with
551 GDB, when `gud-tooltip-mode' is t.
553 Set `gdb-cpp-define-alist-flags' for any include paths or
559 (defcustom gdb-cpp-define-alist-flags
""
560 "Preprocessor flags for `gdb-cpp-define-alist-program'."
565 (defcustom gdb-create-source-file-list t
566 "Non-nil means create a list of files from which the executable was built.
567 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
568 line for a long time when starting, possibly because your executable was
569 built from a large number of files. This allows quicker initialization
570 but means that these files are not automatically enabled for debugging,
571 e.g., you won't be able to click in the fringe to set a breakpoint until
572 execution has already stopped there."
577 (defcustom gdb-show-main nil
578 "Non-nil means display source file containing the main routine at startup.
579 Also display the main routine in the disassembly buffer if present."
584 (defvar gdbmi-debug-mode nil
585 "When non-nil, print the messages sent/received from GDB/MI in *Messages*.")
587 (defun gdb-force-mode-line-update (status)
588 (let ((buffer gud-comint-buffer
))
589 (if (and buffer
(buffer-name buffer
))
590 (with-current-buffer buffer
591 (setq mode-line-process
593 (process-status (get-buffer-process buffer
)) status
))
594 ;; Force mode line redisplay soon.
595 (force-mode-line-update)))))
597 ;; These two are used for menu and toolbar
598 (defun gdb-control-all-threads ()
599 "Switch to non-stop/A mode."
601 (setq gdb-gud-control-all-threads t
)
602 ;; Actually forcing the tool-bar to update.
603 (force-mode-line-update)
604 (message "Now in non-stop/A mode."))
606 (defun gdb-control-current-thread ()
607 "Switch to non-stop/T mode."
609 (setq gdb-gud-control-all-threads nil
)
610 ;; Actually forcing the tool-bar to update.
611 (force-mode-line-update)
612 (message "Now in non-stop/T mode."))
614 (defun gdb-find-watch-expression ()
615 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list
))
616 (varnum (car var
)) expr
)
617 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum
)
618 (let ((var1 (assoc (match-string 1 varnum
) gdb-var-list
)) var2 varnumlet
619 (component-list (split-string (match-string 2 varnum
) "\\." t
)))
620 (setq expr
(nth 1 var1
))
621 (setq varnumlet
(car var1
))
622 (dolist (component component-list
)
623 (setq var2
(assoc varnumlet gdb-var-list
))
624 (setq expr
(concat expr
625 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2
))
626 (concat "[" component
"]")
627 (concat "." component
))))
628 (setq varnumlet
(concat varnumlet
"." component
)))
631 ;; noall is used for commands which don't take --all, but only
633 (defun gdb-gud-context-command (command &optional noall
)
634 "When `gdb-non-stop' is t, add --thread option to COMMAND if
635 `gdb-gud-control-all-threads' is nil and --all option otherwise.
636 If NOALL is t, always add --thread option no matter what
637 `gdb-gud-control-all-threads' value is.
639 When `gdb-non-stop' is nil, return COMMAND unchanged."
641 (if (and gdb-gud-control-all-threads
643 gdb-supports-non-stop
)
644 (concat command
" --all ")
645 (gdb-current-context-command command
))
648 (defmacro gdb-gud-context-call
(cmd1 &optional cmd2 noall noarg
)
649 "`gud-call' wrapper which adds --thread/--all options between
650 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
652 NOARG must be t when this macro is used outside `gud-def'"
654 (concat (gdb-gud-context-command ,cmd1
,noall
) " " ,cmd2
)
655 ,(when (not noarg
) 'arg
)))
657 (defun gdb--check-interpreter (filter proc string
)
658 (unless (zerop (length string
))
659 (remove-function (process-filter proc
) #'gdb--check-interpreter
)
660 (unless (memq (aref string
0) '(?^ ?~ ?
@ ?
& ?
* ?
=))
661 ;; Apparently we're not running with -i=mi.
662 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
664 (setq string
(concat (propertize msg
'font-lock-face
'error
)
666 ;; Use the old gud-gbd filter, not because it works, but because it
667 ;; will properly display GDB's answers rather than hanging waiting for
668 ;; answers that aren't coming.
669 (set (make-local-variable 'gud-marker-filter
) #'gud-gdb-marker-filter
))
670 (funcall filter proc string
)))
672 (defvar gdb-control-level
0)
675 (defun gdb (command-line)
676 "Run gdb passing it COMMAND-LINE as arguments.
678 If COMMAND-LINE names a program FILE to debug, gdb will run in
679 a buffer named *gud-FILE*, and the directory containing FILE
680 becomes the initial working directory and source-file directory
682 If COMMAND-LINE requests that gdb attaches to a process PID, gdb
683 will run in *gud-PID*, otherwise it will run in *gud*; in these
684 cases the initial working directory is the default-directory of
685 the buffer in which this command was invoked.
687 COMMAND-LINE should include \"-i=mi\" to use gdb's MI text interface.
688 Note that the old \"--annotate\" option is no longer supported.
690 If option `gdb-many-windows' is nil (the default value) then gdb just
691 pops up the GUD buffer unless `gdb-show-main' is t. In this case
692 it starts with two windows: one displaying the GUD buffer and the
693 other with the source file with the main routine of the inferior.
695 If option `gdb-many-windows' is t, regardless of the value of
696 `gdb-show-main', the layout below will appear. Keybindings are
697 shown in some of the buffers.
699 Watch expressions appear in the speedbar/slowbar.
701 The following commands help control operation :
703 `gdb-many-windows' - Toggle the number of windows gdb uses.
704 `gdb-restore-windows' - To restore the window layout.
706 See Info node `(emacs)GDB Graphical Interface' for a more
707 detailed description of this mode.
710 +----------------------------------------------------------------------+
712 +-----------------------------------+----------------------------------+
713 | GUD buffer (I/O of GDB) | Locals buffer |
717 +-----------------------------------+----------------------------------+
718 | Source buffer | I/O buffer (of debugged program) |
726 +-----------------------------------+----------------------------------+
727 | Stack buffer | Breakpoints buffer |
728 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
729 | | RET gdb-goto-breakpoint |
730 | | D gdb-delete-breakpoint |
731 +-----------------------------------+----------------------------------+"
733 (interactive (list (gud-query-cmdline 'gdb
)))
735 (when (and gud-comint-buffer
736 (buffer-name gud-comint-buffer
)
737 (get-buffer-process gud-comint-buffer
)
738 (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
)))
739 (gdb-restore-windows)
741 "Multiple debugging requires restarting in text command mode"))
743 (gud-common-init command-line nil
'gud-gdbmi-marker-filter
)
745 ;; Setup a temporary process filter to warn when GDB was not started
747 (let ((proc (get-buffer-process gud-comint-buffer
)))
748 (add-function :around
(process-filter proc
) #'gdb--check-interpreter
))
750 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
751 (set (make-local-variable 'gdb-control-level
) 0)
752 (setq comint-input-sender
'gdb-send
)
753 (when (ring-empty-p comint-input-ring
) ; cf shell-mode
754 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
755 (if (eq system-type
'ms-dos
)
758 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
759 (hsize (getenv "HISTSIZE")))
760 (dolist (file (append '("~/.gdbinit")
761 (unless (string-equal (expand-file-name ".")
762 (expand-file-name "~"))
764 (if (file-readable-p (setq file
(expand-file-name file
)))
766 (insert-file-contents file
)
767 ;; TODO? check for "set history save\\( *on\\)?" and do
768 ;; not use history otherwise?
769 (while (re-search-forward
770 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t
)
771 (cond ((string-equal (match-string 1) "filename")
772 (setq hfile
(expand-file-name
774 (file-name-directory file
))))
775 ((string-equal (match-string 1) "size")
776 (setq hsize
(match-string 2))))))))
778 (integerp (setq hsize
(string-to-number hsize
)))
780 (set (make-local-variable 'comint-input-ring-size
) hsize
))
782 (set (make-local-variable 'comint-input-ring-file-name
) hfile
))
783 (comint-read-input-ring t
)))
784 (gud-def gud-tbreak
"tbreak %f:%l" "\C-t"
785 "Set temporary breakpoint at current line.")
787 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
788 "\C-j" "Set execution address to current line.")
790 (gud-def gud-up
"up %p" "<" "Up N stack frames (numeric arg).")
791 (gud-def gud-down
"down %p" ">" "Down N stack frames (numeric arg).")
792 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
793 (gud-def gud-pstar
"print* %e" nil
794 "Evaluate C dereferenced pointer expression at point.")
796 (gud-def gud-step
(gdb-gud-context-call "-exec-step" "%p" t
)
798 "Step one source line with display.")
799 (gud-def gud-stepi
(gdb-gud-context-call "-exec-step-instruction" "%p" t
)
801 "Step one instruction with display.")
802 (gud-def gud-next
(gdb-gud-context-call "-exec-next" "%p" t
)
804 "Step one line (skip functions).")
805 (gud-def gud-nexti
(gdb-gud-context-call "-exec-next-instruction" "%p" t
)
807 "Step one instruction (skip functions).")
808 (gud-def gud-cont
(gdb-gud-context-call "-exec-continue")
810 "Continue with display.")
811 (gud-def gud-finish
(gdb-gud-context-call "-exec-finish" nil t
)
813 "Finish executing current function.")
814 (gud-def gud-run
"-exec-run"
818 (gud-def gud-break
(if (not (string-match "Disassembly" mode-name
))
819 (gud-call "break %f:%l" arg
)
823 (gud-call "break *%a" arg
)))
824 "\C-b" "Set breakpoint at current line or address.")
826 (gud-def gud-remove
(if (not (string-match "Disassembly" mode-name
))
827 (gud-call "clear %f:%l" arg
)
831 (gud-call "clear *%a" arg
)))
832 "\C-d" "Remove breakpoint at current line or address.")
834 ;; -exec-until doesn't support --all yet
835 (gud-def gud-until
(if (not (string-match "Disassembly" mode-name
))
836 (gud-call "-exec-until %f:%l" arg
)
840 (gud-call "-exec-until *%a" arg
)))
841 "\C-u" "Continue to current line or address.")
842 ;; TODO Why arg here?
844 gud-go
(gud-call (if gdb-active-process
845 (gdb-gud-context-command "-exec-continue")
847 nil
"Start or continue execution.")
849 ;; For debugging Emacs only.
853 "pp " (if (eq (buffer-local-value
854 'major-mode
(window-buffer)) 'speedbar-mode
)
855 (gdb-find-watch-expression) "%e")) arg
)
856 nil
"Print the Emacs s-expression.")
858 (define-key gud-minor-mode-map
[left-margin mouse-1
]
859 'gdb-mouse-set-clear-breakpoint
)
860 (define-key gud-minor-mode-map
[left-fringe mouse-1
]
861 'gdb-mouse-set-clear-breakpoint
)
862 (define-key gud-minor-mode-map
[left-margin C-mouse-1
]
863 'gdb-mouse-toggle-breakpoint-margin
)
864 (define-key gud-minor-mode-map
[left-fringe C-mouse-1
]
865 'gdb-mouse-toggle-breakpoint-fringe
)
867 (define-key gud-minor-mode-map
[left-margin drag-mouse-1
]
869 (define-key gud-minor-mode-map
[left-fringe drag-mouse-1
]
871 (define-key gud-minor-mode-map
[left-margin mouse-3
]
873 (define-key gud-minor-mode-map
[left-fringe mouse-3
]
876 (define-key gud-minor-mode-map
[left-margin C-drag-mouse-1
]
878 (define-key gud-minor-mode-map
[left-fringe C-drag-mouse-1
]
880 (define-key gud-minor-mode-map
[left-fringe C-mouse-3
]
882 (define-key gud-minor-mode-map
[left-margin C-mouse-3
]
885 (set (make-local-variable 'gud-gdb-completion-function
)
886 'gud-gdbmi-completions
)
888 (add-hook 'completion-at-point-functions
#'gud-gdb-completion-at-point
890 (local-set-key "\C-i" 'completion-at-point
)
892 (local-set-key [remap comint-delchar-or-maybe-eof
] 'gdb-delchar-or-quit
)
894 (setq gdb-first-prompt t
)
895 (setq gud-running nil
)
899 (run-hooks 'gdb-mode-hook
))
903 (setq gdb-selected-frame nil
905 gdb-thread-number nil
907 gdb-output-sink
'user
908 gdb-location-alist nil
909 gdb-source-file-list nil
914 gdb-first-done-or-error t
915 gdb-buffer-fringe-width
(car (window-fringes))
917 gdb-source-window nil
918 gdb-inferior-status nil
920 gdb-buf-publisher
'()
922 gdb-breakpoints-list
'()
923 gdb-register-names
'()
924 gdb-non-stop gdb-non-stop-setting
)
928 (setq gdb-buffer-type
'gdbmi
)
930 (gdb-force-mode-line-update
931 (propertize "initializing..." 'face font-lock-variable-name-face
))
933 (gdb-get-buffer-create 'gdb-inferior-io
)
934 (gdb-clear-inferior-io)
935 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
937 (when (eq system-type
'windows-nt
)
938 ;; Don't create a separate console window for the debuggee.
939 (gdb-input "-gdb-set new-console off" 'ignore
)
940 ;; Force GDB to behave as if its input and output stream were
941 ;; connected to a TTY device (since on Windows we use pipes for
942 ;; communicating with GDB).
943 (gdb-input "-gdb-set interactive-mode on" 'ignore
))
944 (gdb-input "-gdb-set height 0" 'ignore
)
947 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler
))
949 (gdb-input "-enable-pretty-printing" 'ignore
)
951 ;; Find source file and compilation directory here.
952 (if gdb-create-source-file-list
953 ;; Needs GDB 6.2 onwards.
954 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list
))
955 ;; Needs GDB 6.0 onwards.
956 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file
)
957 (gdb-input "-gdb-show prompt" 'gdb-get-prompt
))
959 (defun gdb-non-stop-handler ()
960 (goto-char (point-min))
961 (if (re-search-forward "No symbol" nil t
)
964 "This version of GDB doesn't support non-stop mode. Turning it off.")
965 (setq gdb-non-stop nil
)
966 (setq gdb-supports-non-stop nil
))
967 (setq gdb-supports-non-stop t
)
968 (gdb-input "-gdb-set target-async 1" 'ignore
)
969 (gdb-input "-list-target-features" 'gdb-check-target-async
)))
971 (defun gdb-check-target-async ()
972 (goto-char (point-min))
973 (unless (re-search-forward "async" nil t
)
975 "Target doesn't support non-stop mode. Turning it off.")
976 (setq gdb-non-stop nil
)
977 (gdb-input "-gdb-set non-stop 0" 'ignore
)))
979 (defun gdb-delchar-or-quit (arg)
980 "Delete ARG characters or send a quit command to GDB.
981 Send a quit only if point is at the end of the buffer, there is
982 no input, and GDB is waiting for input."
984 (unless (and (eq (current-buffer) gud-comint-buffer
)
985 (eq gud-minor-mode
'gdbmi
))
986 (error "Not in a GDB-MI buffer"))
987 (let ((proc (get-buffer-process gud-comint-buffer
)))
989 (process-live-p proc
)
991 (= (point) (marker-position (process-mark proc
))))
992 ;; Sending an EOF does not work with GDB-MI; submit an
993 ;; explicit quit command.
996 (comint-send-input t t
))
999 (defvar gdb-define-alist nil
"Alist of #define directives for GUD tooltips.")
1001 (defun gdb-create-define-alist ()
1002 "Create an alist of #define directives for GUD tooltips."
1003 (let* ((file (buffer-file-name))
1005 (with-output-to-string
1006 (with-current-buffer standard-output
1008 (file-exists-p file
)
1009 ;; call-process doesn't work with remote file names.
1010 (not (file-remote-p default-directory
))
1011 (call-process shell-file-name file
1012 (list t nil
) nil
"-c"
1013 (concat gdb-cpp-define-alist-program
" "
1014 gdb-cpp-define-alist-flags
))))))
1015 (define-list (split-string output
"\n" t
))
1017 (setq gdb-define-alist nil
)
1018 (dolist (define define-list
)
1019 (setq name
(nth 1 (split-string define
"[( ]")))
1020 (push (cons name define
) gdb-define-alist
))))
1022 (declare-function tooltip-show
"tooltip" (text &optional use-echo-area
))
1024 (defconst gdb--string-regexp
"\"\\(?:[^\\\"]\\|\\\\.\\)*\"")
1026 (defun gdb-tooltip-print (expr)
1027 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
1028 (goto-char (point-min))
1030 ((re-search-forward (concat ".*value=\\(" gdb--string-regexp
1034 (concat expr
" = " (read (match-string 1)))
1035 (or gud-tooltip-echo-area
1036 (not (display-graphic-p)))))
1037 ((re-search-forward "msg=\\(\".+\"\\)$" nil t
)
1038 (tooltip-show (read (match-string 1))
1039 (or gud-tooltip-echo-area
1040 (not (display-graphic-p))))))))
1042 ;; If expr is a macro for a function don't print because of possible dangerous
1043 ;; side-effects. Also printing a function within a tooltip generates an
1044 ;; unexpected starting annotation (phase error).
1045 (defun gdb-tooltip-print-1 (expr)
1046 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
1047 (goto-char (point-min))
1048 (if (search-forward "expands to: " nil t
)
1049 (unless (looking-at "\\S-+.*(.*).*")
1050 (gdb-input (concat "-data-evaluate-expression \"" expr
"\"")
1051 `(lambda () (gdb-tooltip-print ,expr
)))))))
1053 (defun gdb-init-buffer ()
1054 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
1055 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1056 (when gud-tooltip-mode
1057 (make-local-variable 'gdb-define-alist
)
1058 (gdb-create-define-alist)
1059 (add-hook 'after-save-hook
'gdb-create-define-alist nil t
)))
1061 (defmacro gdb--if-arrow
(arrow-position start-posn end-posn
&rest body
)
1062 (declare (indent 3))
1063 (let ((buffer (make-symbol "buffer")))
1064 `(if ,arrow-position
1065 (let ((,buffer
(marker-buffer ,arrow-position
)))
1066 (if (equal ,buffer
(window-buffer (posn-window ,end-posn
)))
1067 (with-current-buffer ,buffer
1068 (when (or (equal ,start-posn
,end-posn
)
1069 (equal (posn-point ,start-posn
)
1070 (marker-position ,arrow-position
)))
1073 (defun gdb-mouse-until (event)
1074 "Continue running until a source line past the current line.
1075 The destination source line can be selected either by clicking
1076 with mouse-3 on the fringe/margin or dragging the arrow
1077 with mouse-1 (default bindings)."
1079 (let ((start (event-start event
))
1080 (end (event-end event
)))
1081 (gdb--if-arrow gud-overlay-arrow-position start end
1082 (let ((line (line-number-at-pos (posn-point end
))))
1083 (gud-call (concat "until " (number-to-string line
)))))
1084 (gdb--if-arrow gdb-disassembly-position start end
1086 (goto-char (point-min))
1087 (forward-line (1- (line-number-at-pos (posn-point end
))))
1089 (gud-call (concat "until *%a"))))))
1091 (defun gdb-mouse-jump (event)
1092 "Set execution address/line.
1093 The destination source line can be selected either by clicking with C-mouse-3
1094 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1095 Unlike `gdb-mouse-until' the destination address can be before the current
1096 line, and no execution takes place."
1098 (let ((start (event-start event
))
1099 (end (event-end event
)))
1100 (gdb--if-arrow gud-overlay-arrow-position start end
1101 (let ((line (line-number-at-pos (posn-point end
))))
1102 (gud-call (concat "tbreak " (number-to-string line
)))
1103 (gud-call (concat "jump " (number-to-string line
)))))
1104 (gdb--if-arrow gdb-disassembly-position start end
1106 (goto-char (point-min))
1107 (forward-line (1- (line-number-at-pos (posn-point end
))))
1109 (gud-call (concat "tbreak *%a"))
1110 (gud-call (concat "jump *%a"))))))
1112 (defcustom gdb-show-changed-values t
1113 "If non-nil change the face of out of scope variables and changed values.
1114 Out of scope variables are suppressed with `shadow' face.
1115 Changed values are highlighted with the face `font-lock-warning-face'."
1120 (defcustom gdb-max-children
40
1121 "Maximum number of children before expansion requires confirmation."
1126 (defcustom gdb-delete-out-of-scope t
1127 "If non-nil delete watch expressions automatically when they go out of scope."
1132 (define-minor-mode gdb-speedbar-auto-raise
1133 "Minor mode to automatically raise the speedbar for watch expressions.
1134 With prefix argument ARG, automatically raise speedbar if ARG is
1135 positive, otherwise don't automatically raise it."
1140 (defcustom gdb-use-colon-colon-notation nil
1141 "If non-nil use FUN::VAR format to display variables in the speedbar."
1146 (define-key gud-minor-mode-map
"\C-c\C-w" 'gud-watch
)
1147 (define-key global-map
(vconcat gud-key-prefix
"\C-w") 'gud-watch
)
1149 (declare-function tooltip-identifier-from-point
"tooltip" (point))
1151 (defun gud-watch (&optional arg event
)
1152 "Watch expression at point.
1153 With arg, enter name of variable to be watched in the minibuffer."
1154 (interactive (list current-prefix-arg last-input-event
))
1155 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer
)))
1156 (if (eq minor-mode
'gdbmi
)
1158 (if event
(posn-set-point (event-end event
)))
1160 (save-selected-window
1163 (completing-read "Name of variable: "
1164 'gud-gdb-complete-command
)
1165 (if (and transient-mark-mode mark-active
)
1166 (buffer-substring (region-beginning) (region-end))
1167 (concat (if (derived-mode-p 'gdb-registers-mode
) "$")
1168 (tooltip-identifier-from-point (point)))))))
1169 (set-text-properties 0 (length expr
) nil expr
)
1170 (gdb-input (concat "-var-create - * " expr
"")
1171 `(lambda () (gdb-var-create-handler ,expr
))))))
1172 (message "gud-watch is a no-op in this mode."))))
1174 (defun gdb-var-create-handler (expr)
1175 (let* ((result (gdb-json-partial-output)))
1176 (if (not (bindat-get-field result
'msg
))
1178 (list (bindat-get-field result
'name
)
1179 (if (and (string-equal gdb-current-language
"c")
1180 gdb-use-colon-colon-notation gdb-selected-frame
)
1181 (setq expr
(concat gdb-selected-frame
"::" expr
))
1183 (bindat-get-field result
'numchild
)
1184 (bindat-get-field result
'type
)
1185 (bindat-get-field result
'value
)
1187 (bindat-get-field result
'has_more
)
1188 gdb-frame-address
)))
1189 (push var gdb-var-list
)
1191 (unless (string-equal
1192 speedbar-initial-expansion-list-name
"GUD")
1193 (speedbar-change-initial-expansion-list "GUD")))
1194 (message-box "No symbol \"%s\" in current context." expr
))))
1196 (defun gdb-speedbar-update ()
1197 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1198 ;; Dummy command to update speedbar even when idle.
1199 (gdb-input "-environment-pwd"
1200 'gdb-speedbar-timer-fn
1201 'gdb-speedbar-update
)))
1203 (defun gdb-speedbar-timer-fn ()
1204 (if gdb-speedbar-auto-raise
1205 (raise-frame speedbar-frame
))
1206 (speedbar-timer-fn))
1208 (defun gdb-var-evaluate-expression-handler (varnum changed
)
1209 (goto-char (point-min))
1210 (re-search-forward (concat ".*value=\\(" gdb--string-regexp
"\\)")
1212 (let ((var (assoc varnum gdb-var-list
)))
1214 (if changed
(setcar (nthcdr 5 var
) 'changed
))
1215 (setcar (nthcdr 4 var
) (read (match-string 1)))))
1216 (gdb-speedbar-update))
1218 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1219 (defun gdb-var-list-children (varnum)
1220 (gdb-input (concat "-var-update " varnum
) 'ignore
)
1221 (gdb-input (concat "-var-list-children --all-values " varnum
)
1222 `(lambda () (gdb-var-list-children-handler ,varnum
))))
1224 (defun gdb-var-list-children-handler (varnum)
1225 (let* ((var-list nil
)
1226 (output (bindat-get-field (gdb-json-partial-output "child")))
1227 (children (bindat-get-field output
'children
)))
1228 (catch 'child-already-watched
1229 (dolist (var gdb-var-list
)
1230 (if (string-equal varnum
(car var
))
1232 ;; With dynamic varobjs numchild may have increased.
1233 (setcar (nthcdr 2 var
) (bindat-get-field output
'numchild
))
1235 (dolist (child children
)
1236 (let ((varchild (list (bindat-get-field child
'name
)
1237 (bindat-get-field child
'exp
)
1238 (bindat-get-field child
'numchild
)
1239 (bindat-get-field child
'type
)
1240 (bindat-get-field child
'value
)
1242 (bindat-get-field child
'has_more
))))
1243 (if (assoc (car varchild
) gdb-var-list
)
1244 (throw 'child-already-watched nil
))
1245 (push varchild var-list
))))
1246 (push var var-list
)))
1247 (setq gdb-var-list
(nreverse var-list
))))
1248 (gdb-speedbar-update))
1250 (defun gdb-var-set-format (format)
1251 "Set the output format for a variable displayed in the speedbar."
1252 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1254 (gdb-input (concat "-var-set-format " varnum
" " format
) 'ignore
)
1257 (defun gdb-var-delete-1 (var varnum
)
1258 (gdb-input (concat "-var-delete " varnum
) 'ignore
)
1259 (setq gdb-var-list
(delq var gdb-var-list
))
1260 (dolist (varchild gdb-var-list
)
1261 (if (string-match (concat (car var
) "\\.") (car varchild
))
1262 (setq gdb-var-list
(delq varchild gdb-var-list
)))))
1264 (defun gdb-var-delete ()
1265 "Delete watch expression at point from the speedbar."
1267 (let ((text (speedbar-line-text)))
1268 (string-match "\\(\\S-+\\)" text
)
1269 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1271 (if (string-match "\\." (car var
))
1272 (message-box "Can only delete a root expression")
1273 (gdb-var-delete-1 var varnum
)))))
1275 (defun gdb-var-delete-children (varnum)
1276 "Delete children of variable object at point from the speedbar."
1277 (gdb-input (concat "-var-delete -c " varnum
) 'ignore
))
1279 (defun gdb-edit-value (_text _token _indent
)
1280 "Assign a value to a variable displayed in the speedbar."
1281 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1283 (value (read-string "New value: ")))
1284 (gdb-input (concat "-var-assign " varnum
" " value
)
1285 `(lambda () (gdb-edit-value-handler ,value
)))))
1287 (defconst gdb-error-regexp
"\\^error,msg=\\(\".+\"\\)")
1289 (defun gdb-edit-value-handler (value)
1290 (goto-char (point-min))
1291 (if (re-search-forward gdb-error-regexp nil t
)
1292 (message-box "Invalid number or expression (%s)" value
)))
1294 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1295 (defun gdb-var-update ()
1296 (gdb-input "-var-update --all-values *"
1297 'gdb-var-update-handler
1300 (defun gdb-var-update-handler ()
1301 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist
)))
1302 (dolist (var gdb-var-list
)
1303 (setcar (nthcdr 5 var
) nil
))
1304 (let ((temp-var-list gdb-var-list
))
1305 (dolist (change changelist
)
1306 (let* ((varnum (bindat-get-field change
'name
))
1307 (var (assoc varnum gdb-var-list
))
1308 (new-num (bindat-get-field change
'new_num_children
)))
1310 (let ((scope (bindat-get-field change
'in_scope
))
1311 (has-more (bindat-get-field change
'has_more
)))
1312 (cond ((string-equal scope
"false")
1313 (if gdb-delete-out-of-scope
1314 (gdb-var-delete-1 var varnum
)
1315 (setcar (nthcdr 5 var
) 'out-of-scope
)))
1316 ((string-equal scope
"true")
1317 (setcar (nthcdr 6 var
) has-more
)
1318 (when (and (or (not has-more
)
1319 (string-equal has-more
"0"))
1321 (string-equal (nth 2 var
) "0"))
1322 (setcar (nthcdr 4 var
)
1323 (bindat-get-field change
'value
))
1324 (setcar (nthcdr 5 var
) 'changed
)))
1325 ((string-equal scope
"invalid")
1326 (gdb-var-delete-1 var varnum
)))))
1327 (let ((var-list nil
) var1
1328 (children (bindat-get-field change
'new_children
)))
1330 (setq var1
(pop temp-var-list
))
1332 (if (string-equal varnum
(car var1
))
1333 (let ((new (string-to-number new-num
))
1334 (previous (string-to-number (nth 2 var1
))))
1335 (setcar (nthcdr 2 var1
) new-num
)
1336 (push var1 var-list
)
1339 ;; Add new children to list.
1340 (dotimes (_ previous
)
1341 (push (pop temp-var-list
) var-list
))
1342 (dolist (child children
)
1344 (list (bindat-get-field child
'name
)
1345 (bindat-get-field child
'exp
)
1346 (bindat-get-field child
'numchild
)
1347 (bindat-get-field child
'type
)
1348 (bindat-get-field child
'value
)
1350 (bindat-get-field child
'has_more
))))
1351 (push varchild var-list
))))
1352 ;; Remove deleted children from list.
1355 (push (pop temp-var-list
) var-list
))
1356 (dotimes (_ (- previous new
))
1357 (pop temp-var-list
)))))
1358 (push var1 var-list
))
1359 (setq var1
(pop temp-var-list
)))
1360 (setq gdb-var-list
(nreverse var-list
))))))))
1361 (gdb-speedbar-update))
1363 (defun gdb-speedbar-expand-node (text token indent
)
1364 "Expand the node the user clicked on.
1365 TEXT is the text of the button we clicked on, a + or - item.
1366 TOKEN is data related to this node.
1367 INDENT is the current indentation depth."
1368 (cond ((string-match "+" text
) ;expand this node
1369 (let* ((var (assoc token gdb-var-list
))
1370 (expr (nth 1 var
)) (children (nth 2 var
)))
1371 (if (or (<= (string-to-number children
) gdb-max-children
)
1373 (format "%s has %s children. Continue? " expr children
)))
1374 (gdb-var-list-children token
))))
1375 ((string-match "-" text
) ;contract this node
1376 (dolist (var gdb-var-list
)
1377 (if (string-match (concat token
"\\.") (car var
))
1378 (setq gdb-var-list
(delq var gdb-var-list
))))
1379 (gdb-var-delete-children token
)
1380 (speedbar-change-expand-button-char ?
+)
1381 (speedbar-delete-subblock indent
))
1382 (t (error "Ooops... not sure what to do")))
1383 (speedbar-center-buffer-smartly))
1385 (defun gdb-get-target-string ()
1386 (with-current-buffer gud-comint-buffer
1393 ;; Each buffer has a TYPE -- a symbol that identifies the function
1394 ;; of that particular buffer.
1396 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1397 ;; is constructed specially.
1399 ;; Others are constructed by gdb-get-buffer-create and
1400 ;; named according to the rules set forth in the gdb-buffer-rules
1402 (defvar gdb-buffer-rules
'())
1404 (defun gdb-rules-name-maker (rules-entry)
1406 (defun gdb-rules-buffer-mode (rules-entry)
1407 (nth 2 rules-entry
))
1408 (defun gdb-rules-update-trigger (rules-entry)
1409 (nth 3 rules-entry
))
1411 (defun gdb-update-buffer-name ()
1412 "Rename current buffer according to name-maker associated with
1413 it in `gdb-buffer-rules'."
1414 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1415 gdb-buffer-rules
))))
1416 (when f
(rename-buffer (funcall f
)))))
1418 (defun gdb-current-buffer-rules ()
1419 "Get `gdb-buffer-rules' entry for current buffer type."
1420 (assoc gdb-buffer-type gdb-buffer-rules
))
1422 (defun gdb-current-buffer-thread ()
1423 "Get thread object of current buffer from `gdb-threads-list'.
1425 When current buffer is not bound to any thread, return main
1427 (cdr (assoc gdb-thread-number gdb-threads-list
)))
1429 (defun gdb-current-buffer-frame ()
1430 "Get current stack frame object for thread of current buffer."
1431 (bindat-get-field (gdb-current-buffer-thread) 'frame
))
1433 (defun gdb-buffer-type (buffer)
1434 "Get value of `gdb-buffer-type' for BUFFER."
1435 (with-current-buffer buffer
1438 (defun gdb-buffer-shows-main-thread-p ()
1439 "Return t if current GDB buffer shows main selected thread and
1440 is not bound to it."
1442 (not (local-variable-p 'gdb-thread-number
)))
1444 (defun gdb-get-buffer (buffer-type &optional thread
)
1445 "Get a specific GDB buffer.
1447 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1448 and `gdb-thread-number' (if provided) must be equal to THREAD."
1450 (dolist (buffer (buffer-list) nil
)
1451 (with-current-buffer buffer
1452 (when (and (eq gdb-buffer-type buffer-type
)
1454 (equal gdb-thread-number thread
)))
1455 (throw 'found buffer
))))))
1457 (defun gdb-get-buffer-create (buffer-type &optional thread
)
1458 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1459 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1461 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1462 buffer-local variable of the new buffer.
1464 Buffer mode and name are selected according to buffer type.
1466 If buffer has trigger associated with it in `gdb-buffer-rules',
1467 this trigger is subscribed to `gdb-buf-publisher' and called with
1469 (or (gdb-get-buffer buffer-type thread
)
1470 (let ((rules (assoc buffer-type gdb-buffer-rules
))
1471 (new (generate-new-buffer "limbo")))
1472 (with-current-buffer new
1473 (let ((mode (gdb-rules-buffer-mode rules
))
1474 (trigger (gdb-rules-update-trigger rules
)))
1475 (when mode
(funcall mode
))
1476 (setq gdb-buffer-type buffer-type
)
1478 (set (make-local-variable 'gdb-thread-number
) thread
))
1479 (set (make-local-variable 'gud-minor-mode
)
1480 (buffer-local-value 'gud-minor-mode gud-comint-buffer
))
1481 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1482 (rename-buffer (funcall (gdb-rules-name-maker rules
)))
1484 (gdb-add-subscriber gdb-buf-publisher
1485 (cons (current-buffer)
1486 (gdb-bind-function-to-buffer
1487 trigger
(current-buffer))))
1488 (funcall trigger
'start
))
1489 (current-buffer))))))
1491 (defun gdb-bind-function-to-buffer (expr buffer
)
1492 "Return a function which will evaluate EXPR in BUFFER."
1493 `(lambda (&rest args
)
1494 (with-current-buffer ,buffer
1495 (apply ',expr args
))))
1497 ;; Used to display windows with thread-bound buffers
1498 (defmacro def-gdb-preempt-display-buffer
(name buffer
&optional doc
1500 `(defun ,name
(&optional thread
)
1502 (message "%s" thread
)
1503 (gdb-preempt-existing-or-display-buffer
1504 (gdb-get-buffer-create ,buffer thread
)
1505 ,split-horizontal
)))
1507 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1508 ;; at least one and possible more functions. The functions have these
1509 ;; roles in defining a buffer type:
1511 ;; NAME - Return a name for this buffer type.
1513 ;; The remaining function(s) are optional:
1515 ;; MODE - called in a new buffer with no arguments, should establish
1516 ;; the proper mode for the buffer.
1519 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
1520 (let ((binding (assoc buffer-type gdb-buffer-rules
)))
1522 (setcdr binding rules
)
1523 (push (cons buffer-type rules
)
1524 gdb-buffer-rules
))))
1526 (defun gdb-parent-mode ()
1527 "Generic mode to derive all other GDB buffer modes from."
1528 (kill-all-local-variables)
1529 (setq buffer-read-only t
)
1530 (buffer-disable-undo)
1531 ;; Delete buffer from gdb-buf-publisher when it's killed
1532 ;; (if it has an associated update trigger)
1537 (let ((trigger (gdb-rules-update-trigger
1538 (gdb-current-buffer-rules))))
1540 (gdb-delete-subscriber
1542 ;; This should match gdb-add-subscriber done in
1543 ;; gdb-get-buffer-create
1544 (cons (current-buffer)
1545 (gdb-bind-function-to-buffer trigger
(current-buffer))))))))
1548 ;; Partial-output buffer : This accumulates output from a command executed on
1549 ;; behalf of emacs (rather than the user).
1551 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1552 'gdb-partial-output-name
)
1554 (defun gdb-partial-output-name ()
1555 (concat " *partial-output-"
1556 (gdb-get-target-string)
1560 (gdb-set-buffer-rules 'gdb-inferior-io
1561 'gdb-inferior-io-name
1562 'gdb-inferior-io-mode
)
1564 (defun gdb-inferior-io-name ()
1565 (concat "*input/output of "
1566 (gdb-get-target-string)
1569 (defun gdb-display-io-buffer ()
1570 "Display IO of debugged program in a separate window."
1572 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))
1574 (defun gdb-inferior-io--init-proc (proc)
1575 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1576 (set-process-filter proc
'gdb-inferior-filter
)
1577 (set-process-sentinel proc
'gdb-inferior-io-sentinel
)
1578 ;; The process can run on a remote host.
1579 (let ((tty (or (process-get proc
'remote-tty
)
1580 (process-tty-name proc
))))
1581 (unless (or (null tty
)
1584 (concat "-inferior-tty-set " tty
) 'ignore
))))
1586 (defun gdb-inferior-io-sentinel (proc _str
)
1587 (when (eq (process-status proc
) 'failed
)
1588 ;; When the debugged process exits, Emacs gets an EIO error on
1589 ;; read from the pty, and stops listening to it. If the gdb
1590 ;; process is still running, remove the pty, make a new one, and
1592 (let ((io-buffer (process-buffer proc
)))
1593 (when (and (process-live-p (get-buffer-process gud-comint-buffer
))
1594 (buffer-live-p io-buffer
))
1595 ;; `comint-exec' deletes the original process as a side effect.
1596 (comint-exec io-buffer
"gdb-inferior" nil nil nil
)
1597 (gdb-inferior-io--init-proc (get-buffer-process io-buffer
))))))
1599 (defcustom gdb-display-buffer-other-frame-action
1600 '((display-buffer-reuse-window display-buffer-pop-up-frame
)
1601 (reusable-frames . visible
)
1602 (inhibit-same-window . t
)
1603 (pop-up-frame-parameters (height .
14)
1606 (tool-bar-lines . nil
)
1607 (menu-bar-lines . nil
)
1608 (minibuffer . nil
)))
1609 "`display-buffer' action for displaying GDB utility frames."
1611 :type display-buffer--action-custom-type
1615 (defun gdb-frame-io-buffer ()
1616 "Display IO of debugged program in another frame."
1618 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1619 gdb-display-buffer-other-frame-action
))
1621 (defvar gdb-inferior-io-mode-map
1622 (let ((map (make-sparse-keymap)))
1623 (define-key map
"\C-c\C-c" 'gdb-io-interrupt
)
1624 (define-key map
"\C-c\C-z" 'gdb-io-stop
)
1625 (define-key map
"\C-c\C-\\" 'gdb-io-quit
)
1626 (define-key map
"\C-c\C-d" 'gdb-io-eof
)
1627 (define-key map
"\C-d" 'gdb-io-eof
)
1630 ;; We want to use comint because it has various nifty and familiar features.
1631 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
1632 "Major mode for gdb inferior-io."
1633 :syntax-table nil
:abbrev-table nil
1634 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil
))
1636 (defcustom gdb-display-io-nopopup nil
1637 "When non-nil, and the `gdb-inferior-io' buffer is buried, don't pop it up."
1642 (defun gdb-inferior-filter (proc string
)
1643 (unless (string-equal string
"")
1645 (unless (and gdb-display-io-nopopup
1646 (setq buf
(gdb-get-buffer 'gdb-inferior-io
))
1647 (null (get-buffer-window buf
)))
1648 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))))
1649 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1650 (comint-output-filter proc string
)))
1652 (defun gdb-io-interrupt ()
1653 "Interrupt the program being debugged."
1656 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1658 (defun gdb-io-quit ()
1659 "Send quit signal to the program being debugged."
1662 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1664 (defun gdb-io-stop ()
1665 "Stop the program being debugged."
1668 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1670 (defun gdb-io-eof ()
1671 "Send end-of-file to the program being debugged."
1674 (get-buffer-process gud-comint-buffer
)))
1676 (defun gdb-clear-inferior-io ()
1677 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1681 (defconst breakpoint-xpm-data
1683 static char *magick[] = {
1684 /* columns rows colors chars-per-pixel */
1700 "XPM data used for breakpoint icon.")
1702 (defconst breakpoint-enabled-pbm-data
1705 0 0 0 0 1 1 1 1 0 0 0 0
1706 0 0 0 1 1 1 1 1 1 0 0 0
1707 0 0 1 1 1 1 1 1 1 1 0 0
1708 0 1 1 1 1 1 1 1 1 1 1 0
1709 0 1 1 1 1 1 1 1 1 1 1 0
1710 0 1 1 1 1 1 1 1 1 1 1 0
1711 0 1 1 1 1 1 1 1 1 1 1 0
1712 0 0 1 1 1 1 1 1 1 1 0 0
1713 0 0 0 1 1 1 1 1 1 0 0 0
1714 0 0 0 0 1 1 1 1 0 0 0 0"
1715 "PBM data used for enabled breakpoint icon.")
1717 (defconst breakpoint-disabled-pbm-data
1729 0 0 0 1 0 1 0 1 0 0"
1730 "PBM data used for disabled breakpoint icon.")
1732 (defvar breakpoint-enabled-icon nil
1733 "Icon for enabled breakpoint in display margin.")
1735 (defvar breakpoint-disabled-icon nil
1736 "Icon for disabled breakpoint in display margin.")
1738 (declare-function define-fringe-bitmap
"fringe.c"
1739 (bitmap bits
&optional height width align
))
1741 (and (display-images-p)
1742 ;; Bitmap for breakpoint in fringe
1743 (define-fringe-bitmap 'breakpoint
1744 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1745 ;; Bitmap for gud-overlay-arrow in fringe
1746 (define-fringe-bitmap 'hollow-right-triangle
1747 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1749 (defface breakpoint-enabled
1753 "Face for enabled breakpoint icon in fringe."
1756 (defface breakpoint-disabled
1757 '((((class color
) (min-colors 88)) :foreground
"grey70")
1758 ;; Ensure that on low-color displays that we end up something visible.
1759 (((class color
) (min-colors 8) (background light
))
1760 :foreground
"black")
1761 (((class color
) (min-colors 8) (background dark
))
1762 :foreground
"white")
1763 (((type tty
) (class mono
))
1765 (t :background
"gray"))
1766 "Face for disabled breakpoint icon in fringe."
1770 (defvar gdb-python-guile-commands-regexp
1771 "python\\|python-interactive\\|pi\\|guile\\|guile-repl\\|gr"
1772 "Regexp that matches Python and Guile commands supported by GDB.")
1774 (defvar gdb-control-commands-regexp
1777 "commands\\|if\\|while\\|define\\|document\\|"
1778 gdb-python-guile-commands-regexp
1779 "\\|while-stepping\\|stepping\\|ws\\|actions"
1780 "\\)\\([[:blank:]]+\\([^[:blank:]]*\\)\\)?$")
1781 "Regexp matching GDB commands that enter a recursive reading loop.
1782 As long as GDB is in the recursive reading loop, it does not expect
1783 commands to be prefixed by \"-interpreter-exec console\".")
1785 (defun gdb-strip-string-backslash (string)
1786 (replace-regexp-in-string "\\\\$" "" string
))
1788 (defun gdb-send (proc string
)
1789 "A comint send filter for gdb."
1790 (with-current-buffer gud-comint-buffer
1791 (let ((inhibit-read-only t
))
1792 (remove-text-properties (point-min) (point-max) '(face))))
1793 ;; mimic <RET> key to repeat previous command in GDB
1794 (when (= gdb-control-level
0)
1795 (if (not (string= "" string
))
1796 (if gdb-continuation
1797 (setq gdb-last-command
(concat gdb-continuation
1798 (gdb-strip-string-backslash string
)
1800 (setq gdb-last-command
(gdb-strip-string-backslash string
)))
1801 (if gdb-last-command
(setq string gdb-last-command
))
1802 (setq gdb-continuation nil
)))
1803 (if (and (not gdb-continuation
)
1804 (or (string-match "^-" string
)
1805 (> gdb-control-level
0)))
1806 ;; Either MI command or we are feeding GDB's recursive reading loop.
1808 (setq gdb-first-done-or-error t
)
1809 (process-send-string proc
(concat string
"\n"))
1810 (if (and (string-match
1812 (if (eq system-type
'windows-nt
) "\026" "\004")
1813 "\\|,q\\|,quit\\|end\\)$")
1815 (> gdb-control-level
0))
1816 (setq gdb-control-level
(1- gdb-control-level
))))
1818 (if (string-match "\\\\$" string
)
1819 (setq gdb-continuation
1820 (concat gdb-continuation
(gdb-strip-string-backslash
1823 (setq gdb-first-done-or-error t
)
1824 (let ((to-send (concat "-interpreter-exec console "
1825 (gdb-mi-quote (concat gdb-continuation string
" "))
1827 (if gdb-enable-debug
1828 (push (cons 'mi-send to-send
) gdb-debug-log
))
1829 (process-send-string proc to-send
))
1830 (if (and (string-match
1832 (if (eq system-type
'windows-nt
) "\026" "\004")
1833 "\\|,q\\|,quit\\|end\\)$")
1835 (> gdb-control-level
0))
1836 (setq gdb-control-level
(1- gdb-control-level
)))
1837 (setq gdb-continuation nil
)))
1838 ;; Python and Guile commands that have an argument don't enter the
1839 ;; recursive reading loop.
1840 (let* ((control-command-p (string-match gdb-control-commands-regexp string
))
1841 (command-arg (match-string 3 string
))
1842 (python-or-guile-p (string-match gdb-python-guile-commands-regexp
1844 (if (and control-command-p
1845 (or (not python-or-guile-p
)
1847 (zerop (length command-arg
))))
1848 (setq gdb-control-level
(1+ gdb-control-level
)))))
1850 (defun gdb-mi-quote (string)
1851 "Return STRING quoted properly as an MI argument.
1852 The string is enclosed in double quotes.
1853 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1854 (setq string
(replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string
))
1855 (setq string
(replace-regexp-in-string "\n" "\\n" string t t
))
1856 (concat "\"" string
"\""))
1858 (defun gdb-input (command handler-function
&optional trigger-name
)
1859 "Send COMMAND to GDB via the MI interface.
1860 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1861 complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
1862 Emacs is still waiting for a reply from another command previously
1863 sent with the same TRIGGER-NAME."
1864 (when (or (not trigger-name
)
1865 (not (gdb-pending-handler-p trigger-name
)))
1866 (setq gdb-token-number
(1+ gdb-token-number
))
1867 (setq command
(concat (number-to-string gdb-token-number
) command
))
1869 (if gdb-enable-debug
(push (list 'send-item command handler-function
)
1872 (gdb-add-handler gdb-token-number handler-function trigger-name
)
1874 (if gdbmi-debug-mode
(message "gdb-input: %s" command
))
1875 (process-send-string (get-buffer-process gud-comint-buffer
)
1876 (concat command
"\n"))))
1878 ;; NOFRAME is used for gud execution control commands
1879 (defun gdb-current-context-command (command)
1880 "Add --thread to gdb COMMAND when needed."
1881 (if (and gdb-thread-number
1882 gdb-supports-non-stop
)
1883 (concat command
" --thread " gdb-thread-number
)
1886 (defun gdb-current-context-buffer-name (name)
1887 "Add thread information and asterisks to string NAME.
1889 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1891 (if (local-variable-p 'gdb-thread-number
)
1892 (format " (bound to thread %s)" gdb-thread-number
)
1896 (defun gdb-current-context-mode-name (mode)
1897 "Add thread information to MODE which is to be used as `mode-name'."
1899 (if gdb-thread-number
1900 (format " [thread %s]" gdb-thread-number
)
1904 (defcustom gud-gdb-command-name
"gdb -i=mi"
1905 "Default command to execute an executable under the GDB debugger."
1910 (setq gud-running nil
)
1911 (setq gdb-output-sink
'user
)
1912 (gdb-remove-all-pending-triggers))
1914 (defun gdb-update (&optional no-proc
)
1915 "Update buffers showing status of debug session.
1916 If NO-PROC is non-nil, do not try to contact the GDB process."
1917 (when gdb-first-prompt
1918 (gdb-force-mode-line-update
1919 (propertize "initializing..." 'face font-lock-variable-name-face
))
1921 (setq gdb-first-prompt nil
))
1924 (gdb-get-main-selected-frame))
1926 ;; We may need to update gdb-threads-list so we can use
1927 (gdb-get-buffer-create 'gdb-threads-buffer
)
1928 ;; gdb-break-list is maintained in breakpoints handler
1929 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1932 (gdb-emit-signal gdb-buf-publisher
'update
))
1934 (gdb-get-changed-registers)
1935 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1936 (dolist (var gdb-var-list
)
1937 (setcar (nthcdr 5 var
) nil
))
1940 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1941 ;; because we may need to update current gud-running value without
1942 ;; changing current thread (see gdb-running)
1943 (defun gdb-setq-thread-number (number)
1944 "Set `gdb-thread-number' to NUMBER.
1945 Only this function must be used to change `gdb-thread-number'
1946 value to NUMBER, because `gud-running' and `gdb-frame-number'
1947 need to be updated appropriately when current thread changes."
1948 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1949 (unless (string-equal number
"0") (setq gdb-thread-number number
))
1950 (setq gdb-frame-number
"0")
1951 (gdb-update-gud-running))
1953 (defun gdb-update-gud-running ()
1954 "Set `gud-running' according to the state of current thread.
1956 `gdb-frame-number' is set to 0 if current thread is now stopped.
1958 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1959 cannot be reliably used to determine whether or not execution
1960 control buttons should be shown in menu or toolbar. Use
1961 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1964 For all-stop mode, thread information is unavailable while target
1966 (let ((old-value gud-running
))
1968 (string= (bindat-get-field (gdb-current-buffer-thread) 'state
)
1970 ;; Set frame number to "0" when _current_ threads stops.
1971 (when (and (gdb-current-buffer-thread)
1972 (not (eq gud-running old-value
)))
1973 (setq gdb-frame-number
"0"))))
1975 (defun gdb-show-run-p ()
1976 "Return t if \"Run/continue\" should be shown on the toolbar."
1977 (or (not gdb-active-process
)
1979 (not gdb-gud-control-all-threads
)
1982 (and gdb-gud-control-all-threads
1983 (> gdb-stopped-threads-count
0))))
1985 (defun gdb-show-stop-p ()
1986 "Return t if \"Stop\" should be shown on the toolbar."
1988 (not gdb-gud-control-all-threads
)
1991 (and gdb-gud-control-all-threads
1992 (not (null gdb-running-threads-count
))
1993 (> gdb-running-threads-count
0))))
1995 ;; GUD displays the selected GDB frame. This might might not be the current
1996 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1997 ;; visited breakpoint is, use that window.
1998 (defun gdb-display-source-buffer (buffer)
1999 (let* ((last-window (if gud-last-last-frame
2001 (gud-find-file (car gud-last-last-frame
)))))
2002 (source-window (or last-window
2003 (if (and gdb-source-window
2004 (window-live-p gdb-source-window
))
2005 gdb-source-window
))))
2007 (setq gdb-source-window source-window
)
2008 (set-window-buffer source-window buffer
))
2012 (defun gdbmi-start-with (str offset match
)
2013 "Return non-nil if string STR starts with MATCH, else returns nil.
2014 OFFSET is the position in STR at which the comparison takes place."
2015 (let ((match-length (length match
))
2016 (str-length (- (length str
) offset
)))
2017 (when (>= str-length match-length
)
2018 (string-equal match
(substring str offset
(+ offset match-length
))))))
2020 (defun gdbmi-same-start (str offset match
)
2021 "Return non-nil if STR and MATCH are equal up to the end of either strings.
2022 OFFSET is the position in STR at which the comparison takes place."
2023 (let* ((str-length (- (length str
) offset
))
2024 (match-length (length match
))
2025 (compare-length (min str-length match-length
)))
2026 (when (> compare-length
0)
2027 (string-equal (substring str offset
(+ offset compare-length
))
2028 (substring match
0 compare-length
)))))
2030 (defun gdbmi-is-number (character)
2031 "Return non-nil if CHARACTER is a numerical character between 0 and 9."
2032 (and (>= character ?
0)
2036 (defvar-local gdbmi-bnf-state
'gdbmi-bnf-output
2037 "Current GDB/MI output parser state.
2038 The parser is placed in a different state when an incomplete data steam is
2040 This variable will preserve the state required to resume the parsing
2041 when more data arrives.")
2043 (defvar-local gdbmi-bnf-offset
0
2044 "Offset in `gud-marker-acc' at which the parser is reading.
2045 This offset is used to be able to parse the GDB/MI message
2046 in-place, without the need of copying the string in a temporary buffer
2047 or discarding parsed tokens by substringing the message.")
2049 (defun gdbmi-bnf-init ()
2050 "Initialize the GDB/MI message parser."
2051 (setq gdbmi-bnf-state
'gdbmi-bnf-output
)
2052 (setq gdbmi-bnf-offset
0)
2053 (setq gud-marker-acc
""))
2056 (defun gdbmi-bnf-output ()
2057 "Implementation of the following GDB/MI output grammar rule:
2060 ( out-of-band-record )* [ result-record ] gdb-prompt"
2062 (gdbmi-bnf-skip-unrecognized)
2063 (while (gdbmi-bnf-out-of-band-record))
2064 (gdbmi-bnf-result-record)
2065 (gdbmi-bnf-gdb-prompt))
2068 (defun gdbmi-bnf-skip-unrecognized ()
2069 "Skip characters until is encounters the beginning of a valid record.
2070 Used as a protection mechanism in case something goes wrong when parsing
2071 a GDB/MI reply message."
2072 (let ((acc-length (length gud-marker-acc
))
2073 (prefix-offset gdbmi-bnf-offset
)
2074 (prompt "(gdb) \n"))
2076 (while (and (< prefix-offset acc-length
)
2077 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2078 (setq prefix-offset
(1+ prefix-offset
)))
2080 (if (and (< prefix-offset acc-length
)
2081 (not (memq (aref gud-marker-acc prefix-offset
)
2082 '(?^ ?
* ?
+ ?
= ?~ ?
@ ?
&)))
2083 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt
))
2084 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
2086 (let ((unrecognized-str (match-string 0 gud-marker-acc
)))
2087 (setq gdbmi-bnf-offset
(match-end 0))
2088 (if gdbmi-debug-mode
2089 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str
))
2090 (gdb-shell unrecognized-str
)
2094 (defun gdbmi-bnf-gdb-prompt ()
2095 "Implementation of the following GDB/MI output grammar rule:
2102 (let ((prompt "(gdb) \n"))
2103 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt
)
2104 (if gdbmi-debug-mode
(message "gdbmi-bnf-gdb-prompt: %s" prompt
))
2106 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length prompt
)))
2108 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
2109 ;; the end of a GDB reply message.
2113 (defun gdbmi-bnf-result-record ()
2114 "Implementation of the following GDB/MI output grammar rule:
2117 [ token ] `^' result-class ( `,' result )* nl
2120 any sequence of digits."
2122 (gdbmi-bnf-result-and-async-record-impl))
2125 (defun gdbmi-bnf-out-of-band-record ()
2126 "Implementation of the following GDB/MI output grammar rule:
2128 out-of-band-record ==>
2129 async-record | stream-record"
2131 (or (gdbmi-bnf-async-record)
2132 (gdbmi-bnf-stream-record)))
2135 (defun gdbmi-bnf-async-record ()
2136 "Implementation of the following GDB/MI output grammar rules:
2139 exec-async-output | status-async-output | notify-async-output
2141 exec-async-output ==>
2142 [ token ] `*' async-output
2144 status-async-output ==>
2145 [ token ] `+' async-output
2147 notify-async-output ==>
2148 [ token ] `=' async-output
2151 async-class ( `,' result )* nl"
2153 (gdbmi-bnf-result-and-async-record-impl))
2156 (defun gdbmi-bnf-stream-record ()
2157 "Implement the following GDB/MI output grammar rule:
2159 console-stream-output | target-stream-output | log-stream-output
2161 console-stream-output ==>
2164 target-stream-output ==>
2167 log-stream-output ==>
2169 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2170 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset
) '(?~ ?
@ ?
&))
2171 (string-match (concat "\\([~@&]\\)\\(" gdb--string-regexp
"\\)\n")
2174 (let ((prefix (match-string 1 gud-marker-acc
))
2175 (c-string (match-string 2 gud-marker-acc
)))
2177 (setq gdbmi-bnf-offset
(match-end 0))
2178 (if gdbmi-debug-mode
(message "gdbmi-bnf-stream-record: %s"
2179 (match-string 0 gud-marker-acc
)))
2181 (cond ((string-equal prefix
"~")
2182 (gdbmi-bnf-console-stream-output c-string
))
2183 ((string-equal prefix
"@")
2184 (gdbmi-bnf-target-stream-output c-string
))
2185 ((string-equal prefix
"&")
2186 (gdbmi-bnf-log-stream-output c-string
)))
2189 (defun gdbmi-bnf-console-stream-output (c-string)
2190 "Handler for the console-stream-output GDB/MI output grammar rule."
2191 (gdb-console c-string
))
2193 (defun gdbmi-bnf-target-stream-output (_c-string)
2194 "Handler for the target-stream-output GDB/MI output grammar rule."
2195 ;; Not currently used.
2198 (defun gdbmi-bnf-log-stream-output (c-string)
2199 "Handler for the log-stream-output GDB/MI output grammar rule."
2200 ;; Suppress "No registers." GDB 6.8 and earlier
2201 ;; duplicates MI error message on internal stream.
2202 ;; Don't print to GUD buffer.
2203 (if (not (string-equal (read c-string
) "No registers.\n"))
2204 (gdb-internals c-string
)))
2207 (defconst gdbmi-bnf-result-state-configs
2208 '(("^" .
(("done" .
(gdb-done . progressive
))
2209 ("error" .
(gdb-error . progressive
))
2210 ("running" .
(gdb-starting . atomic
))))
2211 ("*" .
(("stopped" .
(gdb-stopped . atomic
))
2212 ("running" .
(gdb-running . atomic
))))
2214 ("=" .
(("thread-created" .
(gdb-thread-created . atomic
))
2215 ("thread-selected" .
(gdb-thread-selected . atomic
))
2216 ("thread-existed" .
(gdb-ignored-notification . atomic
))
2217 ('default .
(gdb-ignored-notification . atomic
)))))
2218 "Alist of alists, mapping the type and class of message to a handler function.
2219 Handler functions are all flagged as either `progressive' or `atomic'.
2220 `progressive' handlers are capable of parsing incomplete messages.
2221 They can be called several time with new data chunk as they arrive from GDB.
2222 `progressive' handlers must have an extra argument that is set to a non-nil
2223 value when the message is complete.
2225 Implement the following GDB/MI output grammar rule:
2227 `done' | `running' | `connected' | `error' | `exit'
2230 `stopped' | others (where others will be added depending on the needs
2231 --this is still in development).")
2233 (defun gdbmi-bnf-result-and-async-record-impl ()
2234 "Common implementation of the result-record and async-record rule.
2235 Both rules share the same syntax. Those records may be very large in size.
2236 For that reason, the \"result\" part of the record is parsed by
2237 `gdbmi-bnf-incomplete-record-result', which will keep
2238 receiving characters as they arrive from GDB until the record is complete."
2239 (let ((acc-length (length gud-marker-acc
))
2240 (prefix-offset gdbmi-bnf-offset
))
2242 (while (and (< prefix-offset acc-length
)
2243 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2244 (setq prefix-offset
(1+ prefix-offset
)))
2246 (if (and (< prefix-offset acc-length
)
2247 (member (aref gud-marker-acc prefix-offset
) '(?
* ?
+ ?
= ?^
))
2248 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2249 gud-marker-acc gdbmi-bnf-offset
))
2251 (let ((token (match-string 1 gud-marker-acc
))
2252 (prefix (match-string 2 gud-marker-acc
))
2253 (class (match-string 3 gud-marker-acc
))
2254 (complete (string-equal (match-string 4 gud-marker-acc
) "\n"))
2258 (setq gdbmi-bnf-offset
(match-end 0))
2259 (if gdbmi-debug-mode
(message "gdbmi-bnf-result-record: %s"
2260 (match-string 0 gud-marker-acc
)))
2263 (cdr (assoc prefix gdbmi-bnf-result-state-configs
)))
2264 (setq class-command
(cdr (assoc class class-alist
)))
2265 (if (null class-command
)
2266 (setq class-command
(cdr (assoc 'default class-alist
))))
2270 (if (equal (cdr class-command
) 'progressive
)
2271 (funcall (car class-command
) token
"" complete
)
2272 (funcall (car class-command
) token
"")))
2273 (setq gdbmi-bnf-state
2275 (gdbmi-bnf-incomplete-record-result token class-command
)))
2276 (funcall gdbmi-bnf-state
))
2279 (defun gdbmi-bnf-incomplete-record-result (token class-command
)
2280 "State of the parser used to progressively parse a result-record or async-record
2281 rule from an incomplete data stream. The parser will stay in this state until
2282 the end of the current result or async record is reached."
2283 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2284 ;; Search the data stream for the end of the current record:
2285 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset
))
2286 (is-progressive (equal (cdr class-command
) 'progressive
))
2287 (is-complete (not (null newline-pos
)))
2290 (when gdbmi-debug-mode
2291 (message "gdbmi-bnf-incomplete-record-result: %s"
2292 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
)))
2294 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2295 ;; be processed by the class-command handler:
2296 (when (or is-complete is-progressive
)
2298 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
))
2300 ;; Move gdbmi-bnf-offset past the end of the chunk.
2301 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length result-str
)))
2303 (setq gdbmi-bnf-offset
(1+ gdbmi-bnf-offset
))))
2305 ;; Update the parsing state before invoking the handler in class-command
2306 ;; to make sure it's not left in an invalid state if the handler was
2307 ;; to generate an error.
2309 (setq gdbmi-bnf-state
'gdbmi-bnf-output
))
2313 (funcall (car class-command
) token result-str is-complete
)
2315 (funcall (car class-command
) token result-str
))))
2318 ;; Incomplete gdb response: abort parsing until we receive more data.
2319 (if gdbmi-debug-mode
(message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2320 (throw 'gdbmi-incomplete-stream nil
))
2325 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2326 ; The handling of those rules is currently done by the handlers registered
2327 ; in gdbmi-bnf-result-state-configs
2330 ; variable "=" value
2336 ; const | tuple | list
2342 ; "{}" | "{" result ( "," result )* "}"
2345 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2347 (defcustom gdb-mi-decode-strings nil
2348 "When non-nil, decode octal escapes in GDB output into non-ASCII text.
2350 If the value is a coding-system, use that coding-system to decode
2351 the bytes reconstructed from octal escapes. Any other non-nil value
2352 means to decode using the coding-system set for the GDB process.
2354 Warning: setting this non-nil might mangle strings reported by GDB
2355 that have literal substrings which match the \\nnn octal escape
2356 patterns, where nnn is an octal number between 200 and 377. So
2357 we only recommend to set this variable non-nil if the program you
2358 are debugging really reports non-ASCII text, or some of its source
2359 file names include non-ASCII characters."
2361 (const :tag
"Don't decode" nil
)
2362 (const :tag
"Decode using default coding-system" t
)
2363 (coding-system :tag
"Decode using this coding-system"))
2367 ;; The idea of the following function was suggested
2368 ;; by Kenichi Handa <handa@gnu.org>.
2370 ;; FIXME: This is fragile: it relies on the assumption that all the
2371 ;; non-ASCII strings output by GDB, including names of the source
2372 ;; files, values of string variables in the inferior, etc., are all
2373 ;; encoded in the same encoding. It also assumes that the \nnn
2374 ;; sequences are not split between chunks of output of the GDB process
2375 ;; due to buffering, and arrive together. Finally, if some string
2376 ;; included literal \nnn strings (as opposed to non-ASCII characters
2377 ;; converted by by GDB/MI to octal escapes), this decoding will mangle
2378 ;; those strings. When/if GDB acquires the ability to not
2379 ;; escape-protect non-ASCII characters in its MI output, this kludge
2380 ;; should be removed.
2381 (defun gdb-mi-decode (string)
2382 "Decode octal escapes in MI output STRING into multibyte text."
2384 (if (coding-system-p gdb-mi-decode-strings
)
2385 gdb-mi-decode-strings
2386 (with-current-buffer
2387 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2388 buffer-file-coding-system
))))
2390 (set-buffer-multibyte nil
)
2391 (prin1 string
(current-buffer))
2392 (goto-char (point-min))
2393 ;; prin1 quotes the octal escapes as well, which interferes with
2394 ;; their interpretation by 'read' below. Remove the extra
2395 ;; backslashes to countermand that.
2396 (while (re-search-forward "\\\\\\(\\\\[2-3][0-7][0-7]\\)" nil t
)
2397 (replace-match "\\1" nil nil
))
2398 (goto-char (point-min))
2399 (decode-coding-string (read (current-buffer)) coding
))))
2401 (defun gud-gdbmi-marker-filter (string)
2402 "Filter GDB/MI output."
2404 ;; If required, decode non-ASCII text encoded with octal escapes.
2405 (or (null gdb-mi-decode-strings
)
2406 (setq string
(gdb-mi-decode string
)))
2408 ;; Record transactions if logging is enabled.
2409 (when gdb-enable-debug
2410 (push (cons 'recv string
) gdb-debug-log
)
2411 (if (and gdb-debug-log-max
2412 (> (length gdb-debug-log
) gdb-debug-log-max
))
2413 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
2415 ;; Recall the left over gud-marker-acc from last time.
2416 (setq gud-marker-acc
(concat gud-marker-acc string
))
2418 ;; Start accumulating output for the GUD buffer.
2419 (setq gdb-filter-output
"")
2421 (let ((acc-length (length gud-marker-acc
)))
2422 (catch 'gdbmi-incomplete-stream
2423 (while (and (< gdbmi-bnf-offset acc-length
)
2424 (funcall gdbmi-bnf-state
)))))
2426 (when (/= gdbmi-bnf-offset
0)
2427 (setq gud-marker-acc
(substring gud-marker-acc gdbmi-bnf-offset
))
2428 (setq gdbmi-bnf-offset
0))
2430 (when (and gdbmi-debug-mode
(> (length gud-marker-acc
) 0))
2431 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc
))
2435 (defun gdb-gdb (_output-field))
2437 (defun gdb-shell (output-field)
2438 (setq gdb-filter-output
2439 (concat output-field gdb-filter-output
)))
2441 (defun gdb-ignored-notification (_token _output-field
))
2443 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2444 (defun gdb-thread-created (_token _output-field
))
2445 (defun gdb-thread-exited (_token output-field
)
2446 "Handle =thread-exited async record.
2447 Unset `gdb-thread-number' if current thread exited and update threads list."
2448 (let* ((thread-id (bindat-get-field (gdb-json-string output-field
) 'id
)))
2449 (if (string= gdb-thread-number thread-id
)
2450 (gdb-setq-thread-number nil
))
2451 ;; When we continue current thread and it quickly exits,
2452 ;; the pending triggers in gdb-handler-list left after gdb-running
2453 ;; disallow us to properly call -thread-info without --thread option.
2454 ;; Thus we need to use gdb-wait-for-pending.
2455 (gdb-wait-for-pending
2456 (gdb-emit-signal gdb-buf-publisher
'update-threads
))))
2458 (defun gdb-thread-selected (_token output-field
)
2459 "Handler for =thread-selected MI output record.
2461 Sets `gdb-thread-number' to new id."
2462 (let* ((result (gdb-json-string output-field
))
2463 (thread-id (bindat-get-field result
'id
)))
2464 (gdb-setq-thread-number thread-id
)
2465 ;; Typing `thread N' in GUD buffer makes GDB emit `^done' followed
2466 ;; by `=thread-selected' notification. `^done' causes `gdb-update'
2467 ;; as usually. Things happen too fast and second call (from
2468 ;; gdb-thread-selected handler) gets cut off by our beloved
2469 ;; pending triggers.
2470 ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
2471 ;; body will get executed when `gdb-handler-list' if free of
2472 ;; pending triggers.
2473 (gdb-wait-for-pending
2476 (defun gdb-running (_token output-field
)
2478 (bindat-get-field (gdb-json-string output-field
) 'thread-id
)))
2479 ;; We reset gdb-frame-number to nil if current thread has gone
2480 ;; running. This can't be done in gdb-thread-list-handler-custom
2481 ;; because we need correct gdb-frame-number by the time
2482 ;; -thread-info command is sent.
2483 (when (or (string-equal thread-id
"all")
2484 (string-equal thread-id gdb-thread-number
))
2485 (setq gdb-frame-number nil
)))
2486 (setq gdb-inferior-status
"running")
2487 (gdb-force-mode-line-update
2488 (propertize gdb-inferior-status
'face font-lock-type-face
))
2489 (when (not gdb-non-stop
)
2490 (setq gud-running t
))
2491 (setq gdb-active-process t
))
2493 (defun gdb-starting (_output-field _result
)
2494 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2495 (setq gdb-inferior-status
"running")
2496 (gdb-force-mode-line-update
2497 (propertize gdb-inferior-status
'face font-lock-type-face
))
2498 (setq gdb-active-process t
)
2499 (setq gud-running t
))
2501 ;; -break-insert -t didn't give a reason before gdb 6.9
2503 (defun gdb-stopped (_token output-field
)
2504 "Given the contents of *stopped MI async record, select new
2505 current thread and update GDB buffers."
2506 ;; Reason is available with target-async only
2507 (let* ((result (gdb-json-string output-field
))
2508 (reason (bindat-get-field result
'reason
))
2509 (thread-id (bindat-get-field result
'thread-id
))
2510 (retval (bindat-get-field result
'return-value
))
2511 (varnum (bindat-get-field result
'gdb-result-var
)))
2513 ;; -data-list-register-names needs to be issued for any stopped
2515 (when (not gdb-register-names
)
2516 (gdb-input (concat "-data-list-register-names"
2517 (if gdb-supports-non-stop
2518 (concat " --thread " thread-id
)))
2519 'gdb-register-names-handler
))
2521 ;; Don't set gud-last-frame here as it's currently done in
2522 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2526 ;; ;; Extract the frame position from the marker.
2527 ;; gud-last-frame (cons file
2528 ;; (string-to-number
2529 ;; (match-string 6 gud-marker-acc)))))
2531 (setq gdb-inferior-status
(or reason
"unknown"))
2532 (gdb-force-mode-line-update
2533 (propertize gdb-inferior-status
'face font-lock-warning-face
))
2534 (if (string-equal reason
"exited-normally")
2535 (setq gdb-active-process nil
))
2537 (when (and retval varnum
2538 ;; When the user typed CLI commands, GDB/MI helpfully
2539 ;; includes the "Value returned" response in the "~"
2540 ;; record; here we avoid displaying it twice.
2541 (not (string-match "^Value returned is " gdb-filter-output
)))
2542 (setq gdb-filter-output
2543 (concat gdb-filter-output
2544 (format "Value returned is %s = %s\n" varnum retval
))))
2546 ;; Select new current thread.
2548 ;; Don't switch if we have no reasons selected
2549 (when gdb-switch-reasons
2550 ;; Switch from another stopped thread only if we have
2551 ;; gdb-switch-when-another-stopped:
2552 (when (or gdb-switch-when-another-stopped
2553 (not (string= "stopped"
2554 (bindat-get-field (gdb-current-buffer-thread) 'state
))))
2555 ;; Switch if current reason has been selected or we have no
2557 (if (or (eq gdb-switch-reasons t
)
2558 (member reason gdb-switch-reasons
))
2559 (when (not (string-equal gdb-thread-number thread-id
))
2560 (message "Switched to thread %s" thread-id
)
2561 (gdb-setq-thread-number thread-id
))
2562 (message "Thread %s stopped" thread-id
))))
2564 ;; Print "(gdb)" to GUD console
2565 (when gdb-first-done-or-error
2566 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2568 ;; In non-stop, we update information as soon as another thread gets
2570 (when (or gdb-first-done-or-error
2572 ;; In all-stop this updates gud-running properly as well.
2574 (setq gdb-first-done-or-error nil
))
2575 (run-hook-with-args 'gdb-stopped-functions result
)))
2577 ;; Remove the trimmings from log stream containing debugging messages
2578 ;; being produced by GDB's internals, use warning face and send to GUD
2580 (defun gdb-internals (output-field)
2581 (setq gdb-filter-output
2584 (if (string= output-field
"\"\\n\"")
2586 (let ((error-message
2587 (read output-field
)))
2589 0 (length error-message
)
2590 'face font-lock-warning-face
2594 ;; Remove the trimmings from the console stream and send to GUD buffer
2595 ;; (frontend MI commands should not print to this stream)
2596 (defun gdb-console (output-field)
2597 (setq gdb-filter-output
2598 (gdb-concat-output gdb-filter-output
(read output-field
))))
2600 (defun gdb-done (token-number output-field is-complete
)
2601 (gdb-done-or-error token-number
'done output-field is-complete
))
2603 (defun gdb-error (token-number output-field is-complete
)
2604 (gdb-done-or-error token-number
'error output-field is-complete
))
2606 (defun gdb-done-or-error (token-number type output-field is-complete
)
2607 (if (string-equal token-number
"")
2608 ;; Output from command entered by user
2610 (setq gdb-output-sink
'user
)
2611 (setq token-number nil
)
2612 ;; MI error - send to minibuffer
2613 (when (eq type
'error
)
2614 ;; Skip "msg=" from `output-field'
2615 (message "%s" (read (substring output-field
4)))
2616 ;; Don't send to the console twice. (If it is a console error
2617 ;; it is also in the console stream.)
2618 (setq output-field nil
)))
2619 ;; Output from command from frontend.
2620 (setq gdb-output-sink
'emacs
))
2622 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2623 (let* ((proc (get-buffer-process gud-comint-buffer
))
2624 (no-proc (or (null proc
)
2625 (memq (process-status proc
) '(exit signal
)))))
2627 (when (and is-complete gdb-first-done-or-error
)
2628 (unless (or token-number gud-running no-proc
)
2629 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2630 (gdb-update no-proc
)
2631 (setq gdb-first-done-or-error nil
))
2633 (setq gdb-filter-output
2634 (gdb-concat-output gdb-filter-output output-field
))
2636 ;; We are done concatenating to the output sink. Restore it to user sink:
2637 (setq gdb-output-sink
'user
)
2639 (when (and token-number is-complete
)
2640 (with-current-buffer
2641 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2642 (gdb-handle-reply (string-to-number token-number
))))
2645 (gdb-clear-partial-output))))
2647 (defun gdb-concat-output (so-far new
)
2649 ((eq gdb-output-sink
'user
) (concat so-far new
))
2650 ((eq gdb-output-sink
'emacs
)
2651 (gdb-append-to-partial-output new
)
2654 (defun gdb-append-to-partial-output (string)
2655 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2656 (goto-char (point-max))
2659 (defun gdb-clear-partial-output ()
2660 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2663 (defun gdb-jsonify-buffer (&optional fix-key fix-list
)
2664 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2666 Field names are wrapped in double quotes and equal signs are
2667 replaced with semicolons.
2669 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2670 partial output. This is used to get rid of useless keys in lists
2671 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2672 -break-info are examples of MI commands which issue such
2675 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2676 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2677 -break-info output when it contains breakpoint script field
2678 incompatible with GDB/MI output syntax.
2680 If `default-directory' is remote, full file names are adapted accordingly."
2682 (let ((remote (file-remote-p default-directory
)))
2684 (goto-char (point-min))
2685 (while (re-search-forward "[\\[,]fullname=\"\\(.+\\)\"" nil t
)
2686 (replace-match (concat remote
"\\1") nil nil nil
1))))
2687 (goto-char (point-min))
2690 (while (re-search-forward (concat "[\\[,]\\(" fix-key
"=\\)") nil t
)
2691 (replace-match "" nil nil nil
1))))
2694 ;; Find positions of braces which enclose broken list
2695 (while (re-search-forward (concat fix-list
"={\"") nil t
)
2696 (let ((p1 (goto-char (- (point) 2)))
2697 (p2 (progn (forward-sexp)
2699 ;; Replace braces with brackets
2707 (goto-char (point-min))
2709 (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
2710 gdb--string-regexp
"\\)")))
2711 (while (re-search-forward re nil t
)
2712 (replace-match "\"\\1\":\\2" nil nil
)))
2713 (goto-char (point-max))
2716 (defun gdb-json-read-buffer (&optional fix-key fix-list
)
2717 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2719 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2720 (gdb-jsonify-buffer fix-key fix-list
)
2722 (goto-char (point-min))
2723 (let ((json-array-type 'list
))
2726 (defun gdb-json-string (string &optional fix-key fix-list
)
2727 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2729 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2732 (gdb-json-read-buffer fix-key fix-list
)))
2734 (defun gdb-json-partial-output (&optional fix-key fix-list
)
2735 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2737 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2738 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2739 (gdb-json-read-buffer fix-key fix-list
)))
2741 (defun gdb-line-posns (line)
2742 "Return a pair of LINE beginning and end positions."
2743 (let ((offset (1+ (- line
(line-number-at-pos)))))
2745 (line-beginning-position offset
)
2746 (line-end-position offset
))))
2748 (defmacro gdb-mark-line
(line variable
)
2749 "Set VARIABLE marker to point at beginning of LINE.
2751 If current window has no fringes, inverse colors on LINE.
2753 Return position where LINE begins."
2755 (let* ((posns (gdb-line-posns ,line
))
2756 (start-posn (car posns
))
2757 (end-posn (cdr posns
)))
2758 (set-marker ,variable
(copy-marker start-posn
))
2759 (when (not (> (car (window-fringes)) 0))
2760 (put-text-property start-posn end-posn
2761 'font-lock-face
'(:inverse-video t
)))
2764 (defun gdb-pad-string (string padding
)
2765 (format (concat "%" (number-to-string padding
) "s") string
))
2767 ;; gdb-table struct is a way to programmatically construct simple
2768 ;; tables. It help to reliably align columns of data in GDB buffers
2770 (cl-defstruct gdb-table
2773 (row-properties nil
)
2776 (defun gdb-table-add-row (table row
&optional properties
)
2777 "Add ROW of string to TABLE and recalculate column sizes.
2779 When non-nil, PROPERTIES will be added to the whole row when
2780 calling `gdb-table-string'."
2781 (let ((rows (gdb-table-rows table
))
2782 (row-properties (gdb-table-row-properties table
))
2783 (column-sizes (gdb-table-column-sizes table
))
2784 (right-align (gdb-table-right-align table
)))
2785 (when (not column-sizes
)
2786 (setf (gdb-table-column-sizes table
)
2787 (make-list (length row
) 0)))
2788 (setf (gdb-table-rows table
)
2789 (append rows
(list row
)))
2790 (setf (gdb-table-row-properties table
)
2791 (append row-properties
(list properties
)))
2792 (setf (gdb-table-column-sizes table
)
2793 (cl-mapcar (lambda (x s
)
2795 (max (abs x
) (string-width (or s
"")))))
2796 (if right-align new-x
(- new-x
))))
2797 (gdb-table-column-sizes table
)
2799 ;; Avoid trailing whitespace at eol
2800 (if (not (gdb-table-right-align table
))
2801 (setcar (last (gdb-table-column-sizes table
)) 0))))
2803 (defun gdb-table-string (table &optional sep
)
2804 "Return TABLE as a string with columns separated with SEP."
2805 (let ((column-sizes (gdb-table-column-sizes table
)))
2809 (lambda (row properties
)
2811 (mapconcat 'identity
2812 (cl-mapcar (lambda (s x
) (gdb-pad-string s x
))
2816 (gdb-table-rows table
)
2817 (gdb-table-row-properties table
))
2820 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2821 (defun gdb-get-many-fields (struct &rest fields
)
2822 "Return a list of FIELDS values from STRUCT."
2824 (dolist (field fields
)
2825 (push (bindat-get-field struct field
) values
))
2828 (defmacro def-gdb-auto-update-trigger
(trigger-name gdb-command
2830 &optional signal-list
)
2831 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2832 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2833 buffer with `gdb-bind-function-to-buffer'.
2835 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2836 defined trigger is called with an argument from SIGNAL-LIST. It's
2837 not recommended to define triggers with empty SIGNAL-LIST.
2838 Normally triggers should respond at least to the `update' signal.
2840 Normally the trigger defined by this command must be called from
2841 the buffer where HANDLER-NAME must work. This should be done so
2842 that buffer-local thread number may be used in GDB-COMMAND (by
2843 calling `gdb-current-context-command').
2844 `gdb-bind-function-to-buffer' is used to achieve this, see
2845 `gdb-get-buffer-create'.
2847 Triggers defined by this command are meant to be used as a
2848 trigger argument when describing buffer types with
2849 `gdb-set-buffer-rules'."
2850 `(defun ,trigger-name
(&optional signal
)
2852 (or (not ,signal-list
)
2853 (memq signal
,signal-list
))
2854 (gdb-input ,gdb-command
2855 (gdb-bind-function-to-buffer ',handler-name
(current-buffer))
2856 (cons (current-buffer) ',trigger-name
)))))
2858 ;; Used by disassembly buffer only, the rest use
2859 ;; def-gdb-trigger-and-handler
2860 (defmacro def-gdb-auto-update-handler
(handler-name custom-defun
2861 &optional nopreserve
)
2862 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2864 Handlers are normally called from the buffers they put output in.
2866 Erase current buffer and evaluate CUSTOM-DEFUN.
2867 Then call `gdb-update-buffer-name'.
2869 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2870 `(defun ,handler-name
()
2871 (let* ((inhibit-read-only t
)
2872 ,@(unless nopreserve
2873 '((window (get-buffer-window (current-buffer) 0))
2874 (start (window-start window
))
2875 (p (window-point window
)))))
2878 (gdb-update-buffer-name)
2879 ,@(when (not nopreserve
)
2880 '((set-window-start window start
)
2881 (set-window-point window p
))))))
2883 (defmacro def-gdb-trigger-and-handler
(trigger-name gdb-command
2884 handler-name custom-defun
2885 &optional signal-list
)
2886 "Define trigger and handler.
2888 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2889 See `def-gdb-auto-update-trigger'.
2891 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2892 See `def-gdb-auto-update-handler'."
2894 (def-gdb-auto-update-trigger ,trigger-name
2896 ,handler-name
,signal-list
)
2897 (def-gdb-auto-update-handler ,handler-name
2902 ;; Breakpoint buffer : This displays the output of `-break-list'.
2903 (def-gdb-trigger-and-handler
2904 gdb-invalidate-breakpoints
"-break-list"
2905 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2908 (gdb-set-buffer-rules
2909 'gdb-breakpoints-buffer
2910 'gdb-breakpoints-buffer-name
2911 'gdb-breakpoints-mode
2912 'gdb-invalidate-breakpoints
)
2914 (defun gdb-breakpoints-list-handler-custom ()
2915 (let ((breakpoints-list (bindat-get-field
2916 (gdb-json-partial-output "bkpt" "script")
2917 'BreakpointTable
'body
))
2918 (table (make-gdb-table)))
2919 (setq gdb-breakpoints-list nil
)
2920 (gdb-table-add-row table
'("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2921 (dolist (breakpoint breakpoints-list
)
2922 (add-to-list 'gdb-breakpoints-list
2923 (cons (bindat-get-field breakpoint
'number
)
2925 (let ((at (bindat-get-field breakpoint
'at
))
2926 (pending (bindat-get-field breakpoint
'pending
))
2927 (func (bindat-get-field breakpoint
'func
))
2928 (type (bindat-get-field breakpoint
'type
)))
2929 (gdb-table-add-row table
2931 (bindat-get-field breakpoint
'number
)
2933 (or (bindat-get-field breakpoint
'disp
) "")
2934 (let ((flag (bindat-get-field breakpoint
'enabled
)))
2935 (if (string-equal flag
"y")
2937 (propertize "y" 'font-lock-face
2938 font-lock-warning-face
))
2940 (propertize "n" 'font-lock-face
2941 font-lock-comment-face
))))
2942 (bindat-get-field breakpoint
'addr
)
2943 (or (bindat-get-field breakpoint
'times
) "")
2944 (if (and type
(string-match ".*watchpoint" type
))
2945 (bindat-get-field breakpoint
'what
)
2948 (propertize (or func
"unknown")
2949 'font-lock-face font-lock-function-name-face
)
2950 (gdb-frame-location breakpoint
)))))
2951 ;; Add clickable properties only for breakpoints with file:line
2953 (append (list 'gdb-breakpoint breakpoint
)
2954 (when func
'(help-echo "mouse-2, RET: visit breakpoint"
2955 mouse-face highlight
))))))
2956 (insert (gdb-table-string table
" "))
2957 (gdb-place-breakpoints)))
2959 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2960 (defun gdb-place-breakpoints ()
2961 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2962 (dolist (buffer (buffer-list))
2963 (with-current-buffer buffer
2964 (if (and (eq gud-minor-mode
'gdbmi
)
2965 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2966 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2967 (dolist (breakpoint gdb-breakpoints-list
)
2968 (let* ((breakpoint (cdr breakpoint
)) ; gdb-breakpoints-list is
2969 ; an associative list
2970 (line (bindat-get-field breakpoint
'line
)))
2972 (let ((file (bindat-get-field breakpoint
'fullname
))
2973 (flag (bindat-get-field breakpoint
'enabled
))
2974 (bptno (bindat-get-field breakpoint
'number
)))
2975 (unless (and file
(file-exists-p file
))
2976 (setq file
(cdr (assoc bptno gdb-location-alist
))))
2978 (string-equal file
"File not found"))
2979 ;; If the full filename is not recorded in the
2980 ;; breakpoint structure or in `gdb-location-alist', use
2981 ;; -file-list-exec-source-file to extract it.
2982 (when (setq file
(bindat-get-field breakpoint
'file
))
2983 (gdb-input (concat "list " file
":1") 'ignore
)
2984 (gdb-input "-file-list-exec-source-file"
2985 `(lambda () (gdb-get-location
2986 ,bptno
,line
,flag
))))
2987 (with-current-buffer (find-file-noselect file
'nowarn
)
2989 ;; Only want one breakpoint icon at each location.
2990 (gdb-put-breakpoint-icon (string-equal flag
"y") bptno
2991 (string-to-number line
)))))))))
2993 (defconst gdb-source-file-regexp
2994 (concat "fullname=\\(" gdb--string-regexp
"\\)"))
2996 (defun gdb-get-location (bptno line flag
)
2997 "Find the directory containing the relevant source file.
2998 Put in buffer and place breakpoint icon."
2999 (goto-char (point-min))
3000 (catch 'file-not-found
3001 (if (re-search-forward gdb-source-file-regexp nil t
)
3002 (delete (cons bptno
"File not found") gdb-location-alist
)
3003 ;; FIXME: Why/how do we use (match-string 1) when the search failed?
3004 (push (cons bptno
(match-string 1)) gdb-location-alist
)
3006 (unless (assoc bptno gdb-location-alist
)
3007 (push (cons bptno
"File not found") gdb-location-alist
)
3008 (message-box "Cannot find source file for breakpoint location.
3009 Add directory to search path for source files using the GDB command, dir."))
3010 (throw 'file-not-found nil
))
3011 (with-current-buffer (find-file-noselect (match-string 1))
3013 ;; only want one breakpoint icon at each location
3014 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
(string-to-number line
)))))
3016 (add-hook 'find-file-hook
'gdb-find-file-hook
)
3018 (defun gdb-find-file-hook ()
3019 "Set up buffer for debugging if file is part of the source code
3020 of the current session."
3021 (if (and (buffer-name gud-comint-buffer
)
3022 ;; in case gud or gdb-ui is just loaded
3024 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
3026 (if (member buffer-file-name gdb-source-file-list
)
3027 (with-current-buffer (find-buffer-visiting buffer-file-name
)
3028 (gdb-init-buffer)))))
3030 (declare-function gud-remove
"gdb-mi" t t
) ; gud-def
3031 (declare-function gud-break
"gdb-mi" t t
) ; gud-def
3032 (declare-function fringe-bitmaps-at-pos
"fringe.c" (&optional pos window
))
3034 (defun gdb-mouse-set-clear-breakpoint (event)
3035 "Set/clear breakpoint in left fringe/margin at mouse click.
3036 If not in a source or disassembly buffer just set point."
3038 (mouse-minibuffer-check event
)
3039 (let ((posn (event-end event
)))
3040 (with-selected-window (posn-window posn
)
3041 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode
))
3042 (if (numberp (posn-point posn
))
3044 (goto-char (posn-point posn
))
3045 (if (or (posn-object posn
)
3046 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
3050 (posn-set-point posn
))))
3052 (defun gdb-mouse-toggle-breakpoint-margin (event)
3053 "Enable/disable breakpoint in left margin with mouse click."
3055 (mouse-minibuffer-check event
)
3056 (let ((posn (event-end event
)))
3057 (if (numberp (posn-point posn
))
3058 (with-selected-window (posn-window posn
)
3060 (goto-char (posn-point posn
))
3061 (if (posn-object posn
)
3063 (let ((bptno (get-text-property
3064 0 'gdb-bptno
(car (posn-string posn
)))))
3066 (if (get-text-property
3067 0 'gdb-enabled
(car (posn-string posn
)))
3072 (defun gdb-mouse-toggle-breakpoint-fringe (event)
3073 "Enable/disable breakpoint in left fringe with mouse click."
3075 (mouse-minibuffer-check event
)
3076 (let* ((posn (event-end event
))
3077 (pos (posn-point posn
))
3080 (with-selected-window (posn-window posn
)
3081 (with-current-buffer (window-buffer)
3083 (dolist (overlay (overlays-in pos pos
))
3084 (when (overlay-get overlay
'put-break
)
3085 (setq obj
(overlay-get overlay
'before-string
))))
3089 (if (get-text-property 0 'gdb-enabled obj
)
3092 (get-text-property 0 'gdb-bptno obj
)))))))))
3094 (defun gdb-breakpoints-buffer-name ()
3095 (concat "*breakpoints of " (gdb-get-target-string) "*"))
3097 (defun gdb-display-breakpoints-buffer (&optional thread
)
3098 "Display GDB breakpoints."
3100 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)))
3102 (defun gdb-frame-breakpoints-buffer (&optional thread
)
3103 "Display GDB breakpoints in another frame."
3105 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)
3106 gdb-display-buffer-other-frame-action
))
3108 (defvar gdb-breakpoints-mode-map
3109 (let ((map (make-sparse-keymap))
3110 (menu (make-sparse-keymap "Breakpoints")))
3111 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
3112 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
3113 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
3114 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
3115 (suppress-keymap map)
3116 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
3117 (define-key map " " 'gdb-toggle-breakpoint)
3118 (define-key map "D" 'gdb-delete-breakpoint)
3119 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
3120 (define-key map "q" 'gdb-delete-frame-or-window)
3121 (define-key map "\r" 'gdb-goto-breakpoint)
3122 (define-key map "\t" (lambda ()
3124 (gdb-set-window-buffer
3125 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
3126 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3127 (define-key map [follow-link] 'mouse-face)
3130 (defun gdb-delete-frame-or-window ()
3131 "Delete frame if there is only one window. Otherwise delete the window."
3133 (if (one-window-p) (delete-frame)
3136 ;;from make-mode-line-mouse-map
3137 (defun gdb-make-header-line-mouse-map (mouse function) "\
3138 Return a keymap with single entry for mouse key MOUSE on the header line.
3139 MOUSE is defined to run function FUNCTION with no args in the buffer
3140 corresponding to the mode line clicked."
3141 (let ((map (make-sparse-keymap)))
3142 (define-key map (vector 'header-line mouse) function)
3143 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3146 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3148 'help-echo ,help-echo
3149 'mouse-face ',mouse-face
3152 (gdb-make-header-line-mouse-map
3154 (lambda (event) (interactive "e")
3155 (save-selected-window
3156 (select-window (posn-window (event-start event)))
3157 (gdb-set-window-buffer
3158 (gdb-get-buffer-create ',buffer) t) )))))
3161 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3164 (defun gdb-threads-buffer-name ()
3165 (concat "*threads of " (gdb-get-target-string) "*"))
3167 (defun gdb-display-threads-buffer (&optional thread)
3168 "Display GDB threads."
3170 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3172 (defun gdb-frame-threads-buffer (&optional thread)
3173 "Display GDB threads in another frame."
3175 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3176 gdb-display-buffer-other-frame-action))
3178 (def-gdb-trigger-and-handler
3179 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3180 gdb-thread-list-handler gdb-thread-list-handler-custom
3181 '(start update update-threads))
3183 (gdb-set-buffer-rules
3185 'gdb-threads-buffer-name
3187 'gdb-invalidate-threads)
3189 (defvar gdb-threads-font-lock-keywords
3190 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3191 (" \\(stopped\\)" (1 font-lock-warning-face))
3192 (" \\(running\\)" (1 font-lock-string-face))
3193 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3194 "Font lock keywords used in `gdb-threads-mode'.")
3196 (defvar gdb-threads-mode-map
3197 (let ((map (make-sparse-keymap)))
3198 (define-key map "\r" 'gdb-select-thread)
3199 (define-key map "f" 'gdb-display-stack-for-thread)
3200 (define-key map "F" 'gdb-frame-stack-for-thread)
3201 (define-key map "l" 'gdb-display-locals-for-thread)
3202 (define-key map "L" 'gdb-frame-locals-for-thread)
3203 (define-key map "r" 'gdb-display-registers-for-thread)
3204 (define-key map "R" 'gdb-frame-registers-for-thread)
3205 (define-key map "d" 'gdb-display-disassembly-for-thread)
3206 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3207 (define-key map "i" 'gdb-interrupt-thread)
3208 (define-key map "c" 'gdb-continue-thread)
3209 (define-key map "s" 'gdb-step-thread)
3210 (define-key map "\t"
3213 (gdb-set-window-buffer
3214 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3215 (define-key map [mouse-2] 'gdb-select-thread)
3216 (define-key map [follow-link] 'mouse-face)
3219 (defvar gdb-threads-header
3221 (gdb-propertize-header
3222 "Breakpoints" gdb-breakpoints-buffer
3223 "mouse-1: select" mode-line-highlight mode-line-inactive)
3225 (gdb-propertize-header "Threads" gdb-threads-buffer
3226 nil nil mode-line)))
3228 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3229 "Major mode for GDB threads."
3230 (setq gdb-thread-position (make-marker))
3231 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3232 (setq header-line-format gdb-threads-header)
3233 (set (make-local-variable 'font-lock-defaults)
3234 '(gdb-threads-font-lock-keywords))
3235 'gdb-invalidate-threads)
3237 (defun gdb-thread-list-handler-custom ()
3238 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3239 (table (make-gdb-table))
3241 (setq gdb-threads-list nil)
3242 (setq gdb-running-threads-count 0)
3243 (setq gdb-stopped-threads-count 0)
3244 (set-marker gdb-thread-position nil)
3246 (dolist (thread (reverse threads-list))
3247 (let ((running (equal (bindat-get-field thread 'state) "running")))
3248 (add-to-list 'gdb-threads-list
3249 (cons (bindat-get-field thread 'id)
3251 (cl-incf (if running
3252 gdb-running-threads-count
3253 gdb-stopped-threads-count))
3258 (bindat-get-field thread 'id)
3260 (if gdb-thread-buffer-verbose-names
3261 (concat (bindat-get-field thread 'target-id) " ") "")
3262 (bindat-get-field thread 'state)
3263 ;; Include frame information for stopped threads
3266 " in " (bindat-get-field thread 'frame 'func)
3267 (if gdb-thread-buffer-arguments
3270 (let ((args (bindat-get-field thread 'frame 'args)))
3273 (apply #'format "%s=%s"
3274 (gdb-get-many-fields arg 'name 'value)))
3278 (if gdb-thread-buffer-locations
3279 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3280 (if gdb-thread-buffer-addresses
3281 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3285 'mouse-face 'highlight
3286 'help-echo "mouse-2, RET: select thread")))
3287 (when (string-equal gdb-thread-number
3288 (bindat-get-field thread 'id))
3289 (setq marked-line (length gdb-threads-list))))
3290 (insert (gdb-table-string table " "))
3292 (gdb-mark-line marked-line gdb-thread-position)))
3293 ;; We update gud-running here because we need to make sure that
3294 ;; gdb-threads-list is up-to-date
3295 (gdb-update-gud-running)
3296 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3298 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3299 "Define a NAME command which will act upon thread on the current line.
3301 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3302 be the value of `gdb-thread' property of the current line.
3303 If `gdb-thread' is nil, error is signaled."
3304 `(defun ,name (&optional event)
3306 (interactive (list last-input-event))
3307 (if event (posn-set-point (event-end event)))
3310 (let ((thread (get-text-property (point) 'gdb-thread)))
3313 (error "Not recognized as thread line"))))))
3315 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3317 "Define a NAME which will call BUFFER-COMMAND with id of thread
3318 on the current line."
3319 `(def-gdb-thread-buffer-command ,name
3320 (,buffer-command (bindat-get-field thread 'id))
3323 (def-gdb-thread-buffer-command gdb-select-thread
3324 (let ((new-id (bindat-get-field thread 'id)))
3325 (gdb-setq-thread-number new-id)
3326 (gdb-input (concat "-thread-select " new-id) 'ignore)
3328 "Select the thread at current line of threads buffer.")
3330 (def-gdb-thread-buffer-simple-command
3331 gdb-display-stack-for-thread
3332 gdb-preemptively-display-stack-buffer
3333 "Display stack buffer for the thread at current line.")
3335 (def-gdb-thread-buffer-simple-command
3336 gdb-display-locals-for-thread
3337 gdb-preemptively-display-locals-buffer
3338 "Display locals buffer for the thread at current line.")
3340 (def-gdb-thread-buffer-simple-command
3341 gdb-display-registers-for-thread
3342 gdb-preemptively-display-registers-buffer
3343 "Display registers buffer for the thread at current line.")
3345 (def-gdb-thread-buffer-simple-command
3346 gdb-display-disassembly-for-thread
3347 gdb-preemptively-display-disassembly-buffer
3348 "Display disassembly buffer for the thread at current line.")
3350 (def-gdb-thread-buffer-simple-command
3351 gdb-frame-stack-for-thread
3352 gdb-frame-stack-buffer
3353 "Display another frame with stack buffer for thread at current line.")
3355 (def-gdb-thread-buffer-simple-command
3356 gdb-frame-locals-for-thread
3357 gdb-frame-locals-buffer
3358 "Display another frame with locals buffer for thread at current line.")
3360 (def-gdb-thread-buffer-simple-command
3361 gdb-frame-registers-for-thread
3362 gdb-frame-registers-buffer
3363 "Display another frame with registers buffer for the thread at current line.")
3365 (def-gdb-thread-buffer-simple-command
3366 gdb-frame-disassembly-for-thread
3367 gdb-frame-disassembly-buffer
3368 "Display another frame with disassembly buffer for the thread at current line.")
3370 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3371 "Define a NAME which will execute GUD-COMMAND with
3372 `gdb-thread-number' locally bound to id of thread on the current
3374 `(def-gdb-thread-buffer-command ,name
3376 (let ((gdb-thread-number (bindat-get-field thread 'id))
3377 (gdb-gud-control-all-threads nil))
3378 (call-interactively #',gud-command))
3379 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3382 (def-gdb-thread-buffer-gud-command
3383 gdb-interrupt-thread
3385 "Interrupt thread at current line.")
3387 ;; Defined opaquely in M-x gdb via gud-def.
3388 (declare-function gud-cont "gdb-mi" (arg) t)
3390 (def-gdb-thread-buffer-gud-command
3393 "Continue thread at current line.")
3395 (declare-function gud-step "gdb-mi" (arg) t)
3397 (def-gdb-thread-buffer-gud-command
3400 "Step thread at current line.")
3405 (defcustom gdb-memory-rows 8
3406 "Number of data rows in memory window."
3411 (defcustom gdb-memory-columns 4
3412 "Number of data columns in memory window."
3417 (defcustom gdb-memory-format "x"
3418 "Display format of data items in memory window."
3419 :type '(choice (const :tag "Hexadecimal" "x")
3420 (const :tag "Signed decimal" "d")
3421 (const :tag "Unsigned decimal" "u")
3422 (const :tag "Octal" "o")
3423 (const :tag "Binary" "t"))
3427 (defcustom gdb-memory-unit 4
3428 "Unit size of data items in memory window."
3429 :type '(choice (const :tag "Byte" 1)
3430 (const :tag "Halfword" 2)
3431 (const :tag "Word" 4)
3432 (const :tag "Giant word" 8))
3436 (def-gdb-trigger-and-handler
3437 gdb-invalidate-memory
3438 (format "-data-read-memory %s %s %d %d %d"
3444 gdb-read-memory-handler
3445 gdb-read-memory-custom
3448 (gdb-set-buffer-rules
3450 'gdb-memory-buffer-name
3452 'gdb-invalidate-memory)
3454 (defun gdb-memory-column-width (size format)
3455 "Return length of string with memory unit of SIZE in FORMAT.
3457 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3458 in `gdb-memory-format'."
3459 (let ((format-base (cdr (assoc format
3461 ("d" . 10) ("u" . 10)
3465 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3466 (cond ((string-equal format "x")
3467 (+ 2 res)) ; hexadecimal numbers have 0x in front
3468 ((or (string-equal format "d")
3469 (string-equal format "o"))
3472 (error "Unknown format"))))
3474 (defun gdb-read-memory-custom ()
3475 (let* ((res (gdb-json-partial-output))
3476 (err-msg (bindat-get-field res 'msg)))
3478 (let ((memory (bindat-get-field res 'memory)))
3479 (setq gdb-memory-address (bindat-get-field res 'addr))
3480 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3481 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3482 (setq gdb-memory-last-address gdb-memory-address)
3483 (dolist (row memory)
3484 (insert (concat (bindat-get-field row 'addr) ":"))
3485 (dolist (column (bindat-get-field row 'data))
3486 (insert (gdb-pad-string column
3487 (+ 2 (gdb-memory-column-width
3489 gdb-memory-format)))))
3491 ;; Show last page instead of empty buffer when out of bounds
3493 (let ((gdb-memory-address gdb-memory-last-address))
3494 (gdb-invalidate-memory 'update)
3495 (error err-msg))))))
3497 (defvar gdb-memory-mode-map
3498 (let ((map (make-sparse-keymap)))
3499 (suppress-keymap map t)
3500 (define-key map "q" 'kill-current-buffer)
3501 (define-key map "n" 'gdb-memory-show-next-page)
3502 (define-key map "p" 'gdb-memory-show-previous-page)
3503 (define-key map "a" 'gdb-memory-set-address)
3504 (define-key map "t" 'gdb-memory-format-binary)
3505 (define-key map "o" 'gdb-memory-format-octal)
3506 (define-key map "u" 'gdb-memory-format-unsigned)
3507 (define-key map "d" 'gdb-memory-format-signed)
3508 (define-key map "x" 'gdb-memory-format-hexadecimal)
3509 (define-key map "b" 'gdb-memory-unit-byte)
3510 (define-key map "h" 'gdb-memory-unit-halfword)
3511 (define-key map "w" 'gdb-memory-unit-word)
3512 (define-key map "g" 'gdb-memory-unit-giant)
3513 (define-key map "R" 'gdb-memory-set-rows)
3514 (define-key map "C" 'gdb-memory-set-columns)
3517 (defun gdb-memory-set-address-event (event)
3518 "Handle a click on address field in memory buffer header."
3520 (save-selected-window
3521 (select-window (posn-window (event-start event)))
3522 (gdb-memory-set-address)))
3524 ;; Non-event version for use within keymap
3525 (defun gdb-memory-set-address ()
3526 "Set the start memory address."
3528 (let ((arg (read-from-minibuffer "Memory address: ")))
3529 (setq gdb-memory-address arg))
3530 (gdb-invalidate-memory 'update))
3532 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3533 "Define a function NAME which reads new VAR value from minibuffer."
3534 `(defun ,name (event)
3537 (save-selected-window
3538 (select-window (posn-window (event-start event)))
3539 (let* ((arg (read-from-minibuffer ,echo-string))
3540 (count (string-to-number arg)))
3542 (error "Positive number only")
3543 (customize-set-variable ',variable count)
3544 (gdb-invalidate-memory 'update))))))
3546 (def-gdb-set-positive-number
3550 "Set the number of data rows in memory window.")
3552 (def-gdb-set-positive-number
3553 gdb-memory-set-columns
3556 "Set the number of data columns in memory window.")
3558 (defmacro def-gdb-memory-format (name format doc)
3559 "Define a function NAME to switch memory buffer to use FORMAT.
3561 DOC is an optional documentation string."
3562 `(defun ,name () ,(when doc doc)
3564 (customize-set-variable 'gdb-memory-format ,format)
3565 (gdb-invalidate-memory 'update)))
3567 (def-gdb-memory-format
3568 gdb-memory-format-binary "t"
3569 "Set the display format to binary.")
3571 (def-gdb-memory-format
3572 gdb-memory-format-octal "o"
3573 "Set the display format to octal.")
3575 (def-gdb-memory-format
3576 gdb-memory-format-unsigned "u"
3577 "Set the display format to unsigned decimal.")
3579 (def-gdb-memory-format
3580 gdb-memory-format-signed "d"
3581 "Set the display format to decimal.")
3583 (def-gdb-memory-format
3584 gdb-memory-format-hexadecimal "x"
3585 "Set the display format to hexadecimal.")
3587 (defvar gdb-memory-format-map
3588 (let ((map (make-sparse-keymap)))
3589 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3591 "Keymap to select format in the header line.")
3593 (defvar gdb-memory-format-menu
3594 (let ((map (make-sparse-keymap "Format")))
3596 (define-key map [binary]
3597 '(menu-item "Binary" gdb-memory-format-binary
3598 :button (:radio . (equal gdb-memory-format "t"))))
3599 (define-key map [octal]
3600 '(menu-item "Octal" gdb-memory-format-octal
3601 :button (:radio . (equal gdb-memory-format "o"))))
3602 (define-key map [unsigned]
3603 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3604 :button (:radio . (equal gdb-memory-format "u"))))
3605 (define-key map [signed]
3606 '(menu-item "Signed Decimal" gdb-memory-format-signed
3607 :button (:radio . (equal gdb-memory-format "d"))))
3608 (define-key map [hexadecimal]
3609 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3610 :button (:radio . (equal gdb-memory-format "x"))))
3612 "Menu of display formats in the header line.")
3614 (defun gdb-memory-format-menu (event)
3616 (x-popup-menu event gdb-memory-format-menu))
3618 (defun gdb-memory-format-menu-1 (event)
3620 (save-selected-window
3621 (select-window (posn-window (event-start event)))
3622 (let* ((selection (gdb-memory-format-menu event))
3623 (binding (and selection (lookup-key gdb-memory-format-menu
3624 (vector (car selection))))))
3625 (if binding (call-interactively binding)))))
3627 (defmacro def-gdb-memory-unit (name unit-size doc)
3628 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3630 DOC is an optional documentation string."
3631 `(defun ,name () ,(when doc doc)
3633 (customize-set-variable 'gdb-memory-unit ,unit-size)
3634 (gdb-invalidate-memory 'update)))
3636 (def-gdb-memory-unit gdb-memory-unit-giant 8
3637 "Set the unit size to giant words (eight bytes).")
3639 (def-gdb-memory-unit gdb-memory-unit-word 4
3640 "Set the unit size to words (four bytes).")
3642 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3643 "Set the unit size to halfwords (two bytes).")
3645 (def-gdb-memory-unit gdb-memory-unit-byte 1
3646 "Set the unit size to bytes.")
3648 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3649 "Define a function NAME which show new address in memory buffer.
3651 The defined function switches Memory buffer to show address
3652 stored in ADDRESS-VAR variable.
3654 DOC is an optional documentation string."
3658 (let ((gdb-memory-address ,address-var))
3659 (gdb-invalidate-memory))))
3661 (def-gdb-memory-show-page gdb-memory-show-previous-page
3662 gdb-memory-prev-page)
3664 (def-gdb-memory-show-page gdb-memory-show-next-page
3665 gdb-memory-next-page)
3667 (defvar gdb-memory-unit-map
3668 (let ((map (make-sparse-keymap)))
3669 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3671 "Keymap to select units in the header line.")
3673 (defvar gdb-memory-unit-menu
3674 (let ((map (make-sparse-keymap "Unit")))
3675 (define-key map [giantwords]
3676 '(menu-item "Giant words" gdb-memory-unit-giant
3677 :button (:radio . (equal gdb-memory-unit 8))))
3678 (define-key map [words]
3679 '(menu-item "Words" gdb-memory-unit-word
3680 :button (:radio . (equal gdb-memory-unit 4))))
3681 (define-key map [halfwords]
3682 '(menu-item "Halfwords" gdb-memory-unit-halfword
3683 :button (:radio . (equal gdb-memory-unit 2))))
3684 (define-key map [bytes]
3685 '(menu-item "Bytes" gdb-memory-unit-byte
3686 :button (:radio . (equal gdb-memory-unit 1))))
3688 "Menu of units in the header line.")
3690 (defun gdb-memory-unit-menu (event)
3692 (x-popup-menu event gdb-memory-unit-menu))
3694 (defun gdb-memory-unit-menu-1 (event)
3696 (save-selected-window
3697 (select-window (posn-window (event-start event)))
3698 (let* ((selection (gdb-memory-unit-menu event))
3699 (binding (and selection (lookup-key gdb-memory-unit-menu
3700 (vector (car selection))))))
3701 (if binding (call-interactively binding)))))
3703 (defvar gdb-memory-font-lock-keywords
3704 '(;; <__function.name+n>
3705 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3706 (1 font-lock-function-name-face)))
3707 "Font lock keywords used in `gdb-memory-mode'.")
3709 (defvar gdb-memory-header
3714 'face font-lock-warning-face
3715 'help-echo "mouse-1: decrement address"
3716 'mouse-face 'mode-line-highlight
3717 'local-map (gdb-make-header-line-mouse-map
3719 #'gdb-memory-show-previous-page))
3722 'face font-lock-warning-face
3723 'help-echo "mouse-1: increment address"
3724 'mouse-face 'mode-line-highlight
3725 'local-map (gdb-make-header-line-mouse-map
3727 #'gdb-memory-show-next-page))
3729 (propertize gdb-memory-address
3730 'face font-lock-warning-face
3731 'help-echo "mouse-1: set start address"
3732 'mouse-face 'mode-line-highlight
3733 'local-map (gdb-make-header-line-mouse-map
3735 #'gdb-memory-set-address-event))
3737 (propertize (number-to-string gdb-memory-rows)
3738 'face font-lock-warning-face
3739 'help-echo "mouse-1: set number of columns"
3740 'mouse-face 'mode-line-highlight
3741 'local-map (gdb-make-header-line-mouse-map
3743 #'gdb-memory-set-rows))
3745 (propertize (number-to-string gdb-memory-columns)
3746 'face font-lock-warning-face
3747 'help-echo "mouse-1: set number of columns"
3748 'mouse-face 'mode-line-highlight
3749 'local-map (gdb-make-header-line-mouse-map
3751 #'gdb-memory-set-columns))
3753 (propertize gdb-memory-format
3754 'face font-lock-warning-face
3755 'help-echo "mouse-3: select display format"
3756 'mouse-face 'mode-line-highlight
3757 'local-map gdb-memory-format-map)
3759 (propertize (number-to-string gdb-memory-unit)
3760 'face font-lock-warning-face
3761 'help-echo "mouse-3: select unit size"
3762 'mouse-face 'mode-line-highlight
3763 'local-map gdb-memory-unit-map)))
3764 "Header line used in `gdb-memory-mode'.")
3766 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3767 "Major mode for examining memory."
3768 (setq header-line-format gdb-memory-header)
3769 (set (make-local-variable 'font-lock-defaults)
3770 '(gdb-memory-font-lock-keywords))
3771 'gdb-invalidate-memory)
3773 (defun gdb-memory-buffer-name ()
3774 (concat "*memory of " (gdb-get-target-string) "*"))
3776 (defun gdb-display-memory-buffer (&optional thread)
3777 "Display GDB memory contents."
3779 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3781 (defun gdb-frame-memory-buffer ()
3782 "Display memory contents in another frame."
3784 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3785 gdb-display-buffer-other-frame-action))
3788 ;;; Disassembly view
3790 (defun gdb-disassembly-buffer-name ()
3791 (gdb-current-context-buffer-name
3792 (concat "disassembly of " (gdb-get-target-string))))
3794 (defun gdb-display-disassembly-buffer (&optional thread)
3795 "Display GDB disassembly information."
3797 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3799 (def-gdb-preempt-display-buffer
3800 gdb-preemptively-display-disassembly-buffer
3801 'gdb-disassembly-buffer)
3803 (defun gdb-frame-disassembly-buffer (&optional thread)
3804 "Display GDB disassembly information in another frame."
3806 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3807 gdb-display-buffer-other-frame-action))
3809 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3810 (let* ((frame (gdb-current-buffer-frame))
3811 (file (bindat-get-field frame 'fullname))
3812 (line (bindat-get-field frame 'line)))
3814 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3815 ;; If we're unable to get a file name / line for $PC, simply
3816 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3817 ;; 150 bytes) instructions.
3818 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3819 gdb-disassembly-handler
3820 ;; We update disassembly only after we have actual frame information
3821 ;; about all threads, so no there's `update' signal in this list
3822 '(start update-disassembly))
3824 (def-gdb-auto-update-handler
3825 gdb-disassembly-handler
3826 gdb-disassembly-handler-custom
3829 (gdb-set-buffer-rules
3830 'gdb-disassembly-buffer
3831 'gdb-disassembly-buffer-name
3832 'gdb-disassembly-mode
3833 'gdb-invalidate-disassembly)
3835 (defvar gdb-disassembly-font-lock-keywords
3836 '(;; <__function.name+n>
3837 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3838 (1 font-lock-function-name-face))
3839 ;; 0xNNNNNNNN <__function.name+n>: opcode
3840 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3841 (4 font-lock-keyword-face))
3842 ;; %register(at least i386)
3843 ("%\\sw+" . font-lock-variable-name-face)
3844 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3845 (1 font-lock-comment-face)
3846 (2 font-lock-function-name-face))
3847 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3848 "Font lock keywords used in `gdb-disassembly-mode'.")
3850 (defvar gdb-disassembly-mode-map
3852 (let ((map (make-sparse-keymap)))
3853 (suppress-keymap map)
3854 (define-key map "q" 'kill-current-buffer)
3857 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3858 "Major mode for GDB disassembly information."
3859 ;; TODO Rename overlay variable for disassembly mode
3860 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3861 (setq fringes-outside-margins t)
3862 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3863 (set (make-local-variable 'font-lock-defaults)
3864 '(gdb-disassembly-font-lock-keywords))
3865 'gdb-invalidate-disassembly)
3867 (defun gdb-disassembly-handler-custom ()
3868 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3869 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3870 (table (make-gdb-table))
3872 (dolist (instr instructions)
3873 (gdb-table-add-row table
3875 (bindat-get-field instr 'address)
3877 ((func-name (bindat-get-field instr 'func-name))
3878 (offset (bindat-get-field instr 'offset)))
3880 (format "<%s+%s>:" func-name offset)
3882 (bindat-get-field instr 'inst)))
3883 (when (string-equal (bindat-get-field instr 'address)
3886 (setq marked-line (length (gdb-table-rows table)))
3887 (setq fringe-indicator-alist
3888 (if (string-equal gdb-frame-number "0")
3890 '((overlay-arrow . hollow-right-triangle)))))))
3891 (insert (gdb-table-string table " "))
3892 (gdb-disassembly-place-breakpoints)
3893 ;; Mark current position with overlay arrow and scroll window to
3896 (let ((window (get-buffer-window (current-buffer) 0)))
3897 (set-window-point window (gdb-mark-line marked-line
3898 gdb-disassembly-position))))
3900 (gdb-current-context-mode-name
3901 (concat "Disassembly: "
3902 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3904 (defun gdb-disassembly-place-breakpoints ()
3905 (gdb-remove-breakpoint-icons (point-min) (point-max))
3906 (dolist (breakpoint gdb-breakpoints-list)
3907 (let* ((breakpoint (cdr breakpoint))
3908 (bptno (bindat-get-field breakpoint 'number))
3909 (flag (bindat-get-field breakpoint 'enabled))
3910 (address (bindat-get-field breakpoint 'addr)))
3912 (goto-char (point-min))
3913 (if (re-search-forward (concat "^" address) nil t)
3914 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3917 (defvar gdb-breakpoints-header
3919 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3922 (gdb-propertize-header "Threads" gdb-threads-buffer
3923 "mouse-1: select" mode-line-highlight
3924 mode-line-inactive)))
3926 ;;; Breakpoints view
3927 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3928 "Major mode for gdb breakpoints."
3929 (setq header-line-format gdb-breakpoints-header)
3930 'gdb-invalidate-breakpoints)
3932 (defun gdb-toggle-breakpoint ()
3933 "Enable/disable breakpoint at current line of breakpoints buffer."
3937 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3940 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3943 (bindat-get-field breakpoint 'number)))
3944 (error "Not recognized as break/watchpoint line")))))
3946 (defun gdb-delete-breakpoint ()
3947 "Delete the breakpoint at current line of breakpoints buffer."
3951 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3953 (gud-basic-call (concat "-break-delete "
3954 (bindat-get-field breakpoint 'number)))
3955 (error "Not recognized as break/watchpoint line")))))
3957 (defun gdb-goto-breakpoint (&optional event)
3958 "Go to the location of breakpoint at current line of breakpoints buffer."
3959 (interactive (list last-input-event))
3960 (if event (posn-set-point (event-end event)))
3961 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3962 (let ((window (get-buffer-window gud-comint-buffer)))
3963 (if window (save-selected-window (select-window window))))
3966 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3968 (let ((bptno (bindat-get-field breakpoint 'number))
3969 (file (bindat-get-field breakpoint 'fullname))
3970 (line (bindat-get-field breakpoint 'line)))
3971 (save-selected-window
3972 (let* ((buffer (find-file-noselect
3973 (if (file-exists-p file) file
3974 (cdr (assoc bptno gdb-location-alist)))))
3975 (window (or (gdb-display-source-buffer buffer)
3976 (display-buffer buffer))))
3977 (setq gdb-source-window window)
3978 (with-current-buffer buffer
3979 (goto-char (point-min))
3980 (forward-line (1- (string-to-number line)))
3981 (set-window-point window (point))))))
3982 (error "Not recognized as break/watchpoint line")))))
3985 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3987 (def-gdb-trigger-and-handler
3988 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3989 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3992 (gdb-set-buffer-rules
3994 'gdb-stack-buffer-name
3996 'gdb-invalidate-frames)
3998 (defun gdb-frame-location (frame)
3999 "Return \" of file:line\" or \" of library\" for structure FRAME.
4001 FRAME must have either \"file\" and \"line\" members or \"from\"
4003 (let ((file (bindat-get-field frame 'file))
4004 (line (bindat-get-field frame 'line))
4005 (from (bindat-get-field frame 'from)))
4006 (let ((res (or (and file line (concat file ":" line))
4008 (if res (concat " of " res) ""))))
4010 (defun gdb-stack-list-frames-custom ()
4011 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
4012 (table (make-gdb-table)))
4013 (set-marker gdb-stack-position nil)
4014 (dolist (frame stack)
4015 (gdb-table-add-row table
4017 (bindat-get-field frame 'level)
4020 (bindat-get-field frame 'func)
4021 (if gdb-stack-buffer-locations
4022 (gdb-frame-location frame) "")
4023 (if gdb-stack-buffer-addresses
4024 (concat " at " (bindat-get-field frame 'addr)) "")))
4025 `(mouse-face highlight
4026 help-echo "mouse-2, RET: Select frame"
4028 (insert (gdb-table-string table " ")))
4029 (when (and gdb-frame-number
4030 (gdb-buffer-shows-main-thread-p))
4031 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
4032 gdb-stack-position))
4034 (gdb-current-context-mode-name "Frames")))
4036 (defun gdb-stack-buffer-name ()
4037 (gdb-current-context-buffer-name
4038 (concat "stack frames of " (gdb-get-target-string))))
4040 (defun gdb-display-stack-buffer (&optional thread)
4041 "Display GDB backtrace for current stack."
4043 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
4045 (def-gdb-preempt-display-buffer
4046 gdb-preemptively-display-stack-buffer
4047 'gdb-stack-buffer nil t)
4049 (defun gdb-frame-stack-buffer (&optional thread)
4050 "Display GDB backtrace for current stack in another frame."
4052 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
4053 gdb-display-buffer-other-frame-action))
4055 (defvar gdb-frames-mode-map
4056 (let ((map (make-sparse-keymap)))
4057 (suppress-keymap map)
4058 (define-key map "q" 'kill-current-buffer)
4059 (define-key map "\r" 'gdb-select-frame)
4060 (define-key map [mouse-2] 'gdb-select-frame)
4061 (define-key map [follow-link] 'mouse-face)
4064 (defvar gdb-frames-font-lock-keywords
4065 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
4066 "Font lock keywords used in `gdb-frames-mode'.")
4068 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
4069 "Major mode for gdb call stack."
4070 (setq gdb-stack-position (make-marker))
4071 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
4072 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
4073 (set (make-local-variable 'font-lock-defaults)
4074 '(gdb-frames-font-lock-keywords))
4075 'gdb-invalidate-frames)
4077 (defun gdb-select-frame (&optional event)
4078 "Select the frame and display the relevant source."
4079 (interactive (list last-input-event))
4080 (if event (posn-set-point (event-end event)))
4081 (let ((frame (get-text-property (point) 'gdb-frame)))
4083 (if (gdb-buffer-shows-main-thread-p)
4084 (let ((new-level (bindat-get-field frame 'level)))
4085 (setq gdb-frame-number new-level)
4086 (gdb-input (concat "-stack-select-frame " new-level)
4089 (error "Could not select frame for non-current thread"))
4090 (error "Not recognized as frame line"))))
4094 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
4095 (def-gdb-trigger-and-handler
4096 gdb-invalidate-locals
4097 (concat (gdb-current-context-command "-stack-list-locals")
4099 gdb-locals-handler gdb-locals-handler-custom
4102 (gdb-set-buffer-rules
4104 'gdb-locals-buffer-name
4106 'gdb-invalidate-locals)
4108 (defvar gdb-locals-watch-map
4109 (let ((map (make-sparse-keymap)))
4110 (suppress-keymap map)
4111 (define-key map "\r" 'gud-watch)
4112 (define-key map [mouse-2] 'gud-watch)
4114 "Keymap to create watch expression of a complex data type local variable.")
4116 (defvar gdb-edit-locals-map-1
4117 (let ((map (make-sparse-keymap)))
4118 (suppress-keymap map)
4119 (define-key map "\r" 'gdb-edit-locals-value)
4120 (define-key map [mouse-2] 'gdb-edit-locals-value)
4122 "Keymap to edit value of a simple data type local variable.")
4124 (defun gdb-edit-locals-value (&optional event)
4125 "Assign a value to a variable displayed in the locals buffer."
4126 (interactive (list last-input-event))
4128 (if event (posn-set-point (event-end event)))
4130 (let* ((var (bindat-get-field
4131 (get-text-property (point) 'gdb-local-variable) 'name))
4132 (value (read-string (format "New value (%s): " var))))
4134 (concat "-gdb-set variable " var " = " value)))))
4136 ;; Don't display values of arrays or structures.
4137 ;; These can be expanded using gud-watch.
4138 (defun gdb-locals-handler-custom ()
4139 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4140 (table (make-gdb-table)))
4141 (dolist (local locals-list)
4142 (let ((name (bindat-get-field local 'name))
4143 (value (bindat-get-field local 'value))
4144 (type (bindat-get-field local 'type)))
4146 (setq value "<complex data type>"))
4148 (string-match "\\0x" value))
4149 (add-text-properties 0 (length name)
4150 `(mouse-face highlight
4151 help-echo "mouse-2: create watch expression"
4152 local-map ,gdb-locals-watch-map)
4154 (add-text-properties 0 (length value)
4155 `(mouse-face highlight
4156 help-echo "mouse-2: edit value"
4157 local-map ,gdb-edit-locals-map-1)
4162 (propertize type 'font-lock-face font-lock-type-face)
4163 (propertize name 'font-lock-face font-lock-variable-name-face)
4165 `(gdb-local-variable ,local))))
4166 (insert (gdb-table-string table " "))
4168 (gdb-current-context-mode-name
4170 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4172 (defvar gdb-locals-header
4174 (gdb-propertize-header "Locals" gdb-locals-buffer
4177 (gdb-propertize-header "Registers" gdb-registers-buffer
4178 "mouse-1: select" mode-line-highlight
4179 mode-line-inactive)))
4181 (defvar gdb-locals-mode-map
4182 (let ((map (make-sparse-keymap)))
4183 (suppress-keymap map)
4184 (define-key map "q" 'kill-current-buffer)
4185 (define-key map "\t" (lambda ()
4187 (gdb-set-window-buffer
4188 (gdb-get-buffer-create
4189 'gdb-registers-buffer
4190 gdb-thread-number) t)))
4193 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4194 "Major mode for gdb locals."
4195 (setq header-line-format gdb-locals-header)
4196 'gdb-invalidate-locals)
4198 (defun gdb-locals-buffer-name ()
4199 (gdb-current-context-buffer-name
4200 (concat "locals of " (gdb-get-target-string))))
4202 (defun gdb-display-locals-buffer (&optional thread)
4203 "Display the local variables of current GDB stack."
4205 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4207 (def-gdb-preempt-display-buffer
4208 gdb-preemptively-display-locals-buffer
4209 'gdb-locals-buffer nil t)
4211 (defun gdb-frame-locals-buffer (&optional thread)
4212 "Display the local variables of the current GDB stack in another frame."
4214 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4215 gdb-display-buffer-other-frame-action))
4218 ;; Registers buffer.
4220 (def-gdb-trigger-and-handler
4221 gdb-invalidate-registers
4222 (concat (gdb-current-context-command "-data-list-register-values") " x")
4223 gdb-registers-handler
4224 gdb-registers-handler-custom
4227 (gdb-set-buffer-rules
4228 'gdb-registers-buffer
4229 'gdb-registers-buffer-name
4231 'gdb-invalidate-registers)
4233 (defun gdb-registers-handler-custom ()
4234 (when gdb-register-names
4235 (let ((register-values
4236 (bindat-get-field (gdb-json-partial-output) 'register-values))
4237 (table (make-gdb-table)))
4238 (dolist (register register-values)
4239 (let* ((register-number (bindat-get-field register 'number))
4240 (value (bindat-get-field register 'value))
4241 (register-name (nth (string-to-number register-number)
4242 gdb-register-names)))
4246 (propertize register-name
4247 'font-lock-face font-lock-variable-name-face)
4248 (if (member register-number gdb-changed-registers)
4249 (propertize value 'font-lock-face font-lock-warning-face)
4251 `(mouse-face highlight
4252 help-echo "mouse-2: edit value"
4253 gdb-register-name ,register-name))))
4254 (insert (gdb-table-string table " ")))
4256 (gdb-current-context-mode-name "Registers"))))
4258 (defun gdb-edit-register-value (&optional event)
4259 "Assign a value to a register displayed in the registers buffer."
4260 (interactive (list last-input-event))
4262 (if event (posn-set-point (event-end event)))
4264 (let* ((var (bindat-get-field
4265 (get-text-property (point) 'gdb-register-name)))
4266 (value (read-string (format "New value (%s): " var))))
4268 (concat "-gdb-set variable $" var " = " value)))))
4270 (defvar gdb-registers-mode-map
4271 (let ((map (make-sparse-keymap)))
4272 (suppress-keymap map)
4273 (define-key map "\r" 'gdb-edit-register-value)
4274 (define-key map [mouse-2] 'gdb-edit-register-value)
4275 (define-key map "q" 'kill-current-buffer)
4276 (define-key map "\t" (lambda ()
4278 (gdb-set-window-buffer
4279 (gdb-get-buffer-create
4281 gdb-thread-number) t)))
4284 (defvar gdb-registers-header
4286 (gdb-propertize-header "Locals" gdb-locals-buffer
4287 "mouse-1: select" mode-line-highlight
4290 (gdb-propertize-header "Registers" gdb-registers-buffer
4291 nil nil mode-line)))
4293 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4294 "Major mode for gdb registers."
4295 (setq header-line-format gdb-registers-header)
4296 'gdb-invalidate-registers)
4298 (defun gdb-registers-buffer-name ()
4299 (gdb-current-context-buffer-name
4300 (concat "registers of " (gdb-get-target-string))))
4302 (defun gdb-display-registers-buffer (&optional thread)
4303 "Display GDB register contents."
4305 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4307 (def-gdb-preempt-display-buffer
4308 gdb-preemptively-display-registers-buffer
4309 'gdb-registers-buffer nil t)
4311 (defun gdb-frame-registers-buffer (&optional thread)
4312 "Display GDB register contents in another frame."
4314 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4315 gdb-display-buffer-other-frame-action))
4317 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4318 (defun gdb-get-changed-registers ()
4319 (when (gdb-get-buffer 'gdb-registers-buffer)
4320 (gdb-input "-data-list-changed-registers"
4321 'gdb-changed-registers-handler
4322 'gdb-get-changed-registers)))
4324 (defun gdb-changed-registers-handler ()
4325 (setq gdb-changed-registers nil)
4326 (dolist (register-number
4327 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4328 (push register-number gdb-changed-registers)))
4330 (defun gdb-register-names-handler ()
4331 ;; Don't use pending triggers because this handler is called
4332 ;; only once (in gdb-init-1)
4333 (setq gdb-register-names nil)
4334 (dolist (register-name
4335 (bindat-get-field (gdb-json-partial-output) 'register-names))
4336 (push register-name gdb-register-names))
4337 (setq gdb-register-names (reverse gdb-register-names)))
4340 (defun gdb-get-source-file-list ()
4341 "Create list of source files for current GDB session.
4342 If buffers already exist for any of these files, `gud-minor-mode'
4344 (goto-char (point-min))
4345 (while (re-search-forward gdb-source-file-regexp nil t)
4346 (push (read (match-string 1)) gdb-source-file-list))
4347 (dolist (buffer (buffer-list))
4348 (with-current-buffer buffer
4349 (when (member buffer-file-name gdb-source-file-list)
4350 (gdb-init-buffer)))))
4352 (defun gdb-get-main-selected-frame ()
4353 "Trigger for `gdb-frame-handler' which uses main current thread.
4354 Called from `gdb-update'."
4355 (gdb-input (gdb-current-context-command "-stack-info-frame")
4357 'gdb-get-main-selected-frame))
4359 (defun gdb-frame-handler ()
4360 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4361 overlay arrow in source buffer."
4362 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4364 (setq gdb-selected-frame (bindat-get-field frame 'func))
4365 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4366 (setq gdb-frame-number (bindat-get-field frame 'level))
4367 (setq gdb-frame-address (bindat-get-field frame 'addr))
4368 (let ((line (bindat-get-field frame 'line)))
4369 (setq gdb-selected-line (and line (string-to-number line)))
4370 (when (and gdb-selected-file gdb-selected-line)
4371 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4372 (gud-display-frame)))
4373 (if gud-overlay-arrow-position
4374 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4375 (position (marker-position gud-overlay-arrow-position)))
4377 (with-current-buffer buffer
4378 (setq fringe-indicator-alist
4379 (if (string-equal gdb-frame-number "0")
4381 '((overlay-arrow . hollow-right-triangle))))
4382 (setq gud-overlay-arrow-position (make-marker))
4383 (set-marker gud-overlay-arrow-position position))))))))
4385 (defconst gdb-prompt-name-regexp
4386 (concat "value=\\(" gdb--string-regexp "\\)"))
4388 (defun gdb-get-prompt ()
4389 "Find prompt for GDB session."
4390 (goto-char (point-min))
4391 (setq gdb-prompt-name nil)
4392 (re-search-forward gdb-prompt-name-regexp nil t)
4393 (setq gdb-prompt-name (read (match-string 1)))
4394 ;; Insert first prompt.
4395 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4397 ;;;; Window management
4398 (defun gdb-display-buffer (buf)
4399 "Show buffer BUF, and make that window dedicated."
4400 (let ((window (display-buffer buf)))
4401 (set-window-dedicated-p window t)
4404 ;; (let ((answer (get-buffer-window buf 0)))
4406 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4407 ;; (let ((window (get-lru-window)))
4408 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4410 ;; (let ((largest (get-largest-window)))
4411 ;; (setq answer (split-window largest))
4412 ;; (set-window-buffer answer buf)
4413 ;; (set-window-dedicated-p answer t)
4415 ;; (set-window-buffer window buf)
4419 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4420 "Find window displaying a buffer with the same
4421 `gdb-buffer-type' as BUF and show BUF there. If no such window
4422 exists, just call `gdb-display-buffer' for BUF. If the window
4423 found is already dedicated, split window according to
4424 SPLIT-HORIZONTAL and show BUF in the new window."
4426 (when (not (get-buffer-window buf))
4427 (let* ((buf-type (gdb-buffer-type buf))
4429 (get-window-with-predicate
4432 (gdb-buffer-type (window-buffer w)))
4433 (not (window-dedicated-p w)))))))
4435 (set-window-buffer existing-window buf)
4436 (let ((dedicated-window
4437 (get-window-with-predicate
4440 (gdb-buffer-type (window-buffer w)))))))
4441 (if dedicated-window
4443 (split-window dedicated-window nil split-horizontal) buf)
4444 (gdb-display-buffer buf))))))
4445 (error "Null buffer")))
4447 ;;; Shared keymap initialization:
4449 (let ((menu (make-sparse-keymap "GDB-Windows")))
4450 (define-key gud-menu-map [displays]
4451 `(menu-item "GDB-Windows" ,menu
4452 :visible (eq gud-minor-mode 'gdbmi)))
4453 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4454 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4455 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4456 (define-key menu [disassembly]
4457 '("Disassembly" . gdb-display-disassembly-buffer))
4458 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4459 (define-key menu [inferior]
4460 '("IO" . gdb-display-io-buffer))
4461 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4462 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4463 (define-key menu [breakpoints]
4464 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4466 (let ((menu (make-sparse-keymap "GDB-Frames")))
4467 (define-key gud-menu-map [frames]
4468 `(menu-item "GDB-Frames" ,menu
4469 :visible (eq gud-minor-mode 'gdbmi)))
4470 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4471 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4472 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4473 (define-key menu [disassembly]
4474 '("Disassembly" . gdb-frame-disassembly-buffer))
4475 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4476 (define-key menu [inferior]
4477 '("IO" . gdb-frame-io-buffer))
4478 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4479 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4480 (define-key menu [breakpoints]
4481 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4483 (let ((menu (make-sparse-keymap "GDB-MI")))
4484 (define-key menu [gdb-customize]
4485 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4486 :help "Customize Gdb Graphical Mode options."))
4487 (define-key menu [gdb-many-windows]
4488 '(menu-item "Display Other Windows" gdb-many-windows
4489 :help "Toggle display of locals, stack and breakpoint information"
4490 :button (:toggle . gdb-many-windows)))
4491 (define-key menu [gdb-restore-windows]
4492 '(menu-item "Restore Window Layout" gdb-restore-windows
4493 :help "Restore standard layout for debug session."))
4494 (define-key menu [sep1]
4496 (define-key menu [all-threads]
4497 '(menu-item "GUD controls all threads"
4500 (setq gdb-gud-control-all-threads t))
4501 :help "GUD start/stop commands apply to all threads"
4502 :button (:radio . gdb-gud-control-all-threads)))
4503 (define-key menu [current-thread]
4504 '(menu-item "GUD controls current thread"
4507 (setq gdb-gud-control-all-threads nil))
4508 :help "GUD start/stop commands apply to current thread only"
4509 :button (:radio . (not gdb-gud-control-all-threads))))
4510 (define-key menu [sep2]
4512 (define-key menu [gdb-customize-reasons]
4513 '(menu-item "Customize switching..."
4516 (customize-option 'gdb-switch-reasons))))
4517 (define-key menu [gdb-switch-when-another-stopped]
4518 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4519 gdb-switch-when-another-stopped
4520 "Automatically switch to stopped thread"
4521 "GDB thread switching %s"
4522 "Switch to stopped thread"))
4523 (define-key gud-menu-map [mi]
4524 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4526 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4527 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4528 ;; tool-bar-local-item-from-menu here because it appends new buttons
4529 ;; to toolbar from right to left while we want our A/T throttle to
4530 ;; show up right before Run button.
4531 (define-key-after gud-tool-bar-map [all-threads]
4532 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4533 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4534 :visible (and (eq gud-minor-mode 'gdbmi)
4536 (not gdb-gud-control-all-threads)))
4539 (define-key-after gud-tool-bar-map [current-thread]
4540 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4541 :image (find-image '((:type xpm :file "gud/all.xpm")))
4542 :visible (and (eq gud-minor-mode 'gdbmi)
4544 gdb-gud-control-all-threads))
4547 (defun gdb-frame-gdb-buffer ()
4548 "Display GUD buffer in another frame."
4550 (display-buffer-other-frame gud-comint-buffer))
4552 (defun gdb-display-gdb-buffer ()
4553 "Display GUD buffer."
4555 (pop-to-buffer gud-comint-buffer nil 0))
4557 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4558 "Set buffer of selected window to NAME and dedicate window.
4560 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4561 window is dedicated."
4562 (unless window (setq window (selected-window)))
4563 (when ignore-dedicated
4564 (set-window-dedicated-p window nil))
4565 (set-window-buffer window (get-buffer name))
4566 (set-window-dedicated-p window t))
4568 (defun gdb-setup-windows ()
4569 "Layout the window pattern for option `gdb-many-windows'."
4570 (gdb-get-buffer-create 'gdb-locals-buffer)
4571 (gdb-get-buffer-create 'gdb-stack-buffer)
4572 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4573 (set-window-dedicated-p (selected-window) nil)
4574 (switch-to-buffer gud-comint-buffer)
4575 (delete-other-windows)
4576 (let ((win0 (selected-window))
4577 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4578 (win2 (split-window nil ( / (window-height) 3)))
4579 (win3 (split-window-right)))
4580 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4581 (select-window win2)
4584 (if gud-last-last-frame
4585 (gud-find-file (car gud-last-last-frame))
4587 (gud-find-file gdb-main-file)
4588 ;; Put buffer list in window if we
4589 ;; can't find a source file.
4590 (list-buffers-noselect))))
4591 (setq gdb-source-window (selected-window))
4592 (let ((win4 (split-window-right)))
4593 (gdb-set-window-buffer
4594 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4595 (select-window win1)
4596 (gdb-set-window-buffer (gdb-stack-buffer-name))
4597 (let ((win5 (split-window-right)))
4598 (gdb-set-window-buffer (if gdb-show-threads-by-default
4599 (gdb-threads-buffer-name)
4600 (gdb-breakpoints-buffer-name))
4602 (select-window win0)))
4604 (define-minor-mode gdb-many-windows
4605 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4606 In this case it starts with two windows: one displaying the GUD
4607 buffer and the other with the source file with the main routine
4608 of the debugged program. Non-nil means display the layout shown for
4613 (if (and gud-comint-buffer
4614 (buffer-name gud-comint-buffer))
4616 (gdb-restore-windows))))
4618 (defun gdb-restore-windows ()
4619 "Restore the basic arrangement of windows used by gdb.
4620 This arrangement depends on the value of option `gdb-many-windows'."
4622 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4623 (delete-other-windows)
4624 (if gdb-many-windows
4626 (when (or gud-last-last-frame gdb-show-main)
4627 (let ((win (split-window)))
4630 (if gud-last-last-frame
4631 (gud-find-file (car gud-last-last-frame))
4632 (gud-find-file gdb-main-file)))
4633 (setq gdb-source-window win)))))
4635 ;; Called from `gud-sentinel' in gud.el:
4637 "Exit a debugging session cleanly.
4638 Kills the gdb buffers, and resets variables and the source buffers."
4639 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4640 ;; process. This pty must be deleted explicitly.
4641 (let ((pty (get-process "gdb-inferior")))
4642 (if pty (delete-process pty)))
4643 ;; Find gdb-mi buffers and kill them.
4644 (dolist (buffer (buffer-list))
4645 (unless (eq buffer gud-comint-buffer)
4646 (with-current-buffer buffer
4647 (if (eq gud-minor-mode 'gdbmi)
4648 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4650 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4651 (setq gud-minor-mode nil)
4652 (kill-local-variable 'tool-bar-map)
4653 (kill-local-variable 'gdb-define-alist))))))
4654 (setq gdb-disassembly-position nil)
4655 (setq overlay-arrow-variable-list
4656 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4657 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4658 (setq gdb-stack-position nil)
4659 (setq overlay-arrow-variable-list
4660 (delq 'gdb-stack-position overlay-arrow-variable-list))
4661 (setq gdb-thread-position nil)
4662 (setq overlay-arrow-variable-list
4663 (delq 'gdb-thread-position overlay-arrow-variable-list))
4664 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4665 (setq gud-running nil)
4666 (setq gdb-active-process nil)
4667 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4669 (defun gdb-get-source-file ()
4670 "Find the source file where the program starts and display it with related
4671 buffers, if required."
4672 (goto-char (point-min))
4673 (if (re-search-forward gdb-source-file-regexp nil t)
4674 (setq gdb-main-file (read (match-string 1))))
4675 (if gdb-many-windows
4677 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4680 (display-buffer (gud-find-file gdb-main-file))))
4681 (gdb-force-mode-line-update
4682 (propertize "ready" 'face font-lock-variable-name-face)))
4685 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4686 "Put string PUTSTRING in front of POS in the current buffer.
4687 PUTSTRING is displayed by putting an overlay into the current buffer with a
4688 `before-string' string that has a `display' property whose value is
4690 (let ((string (make-string 1 ?x))
4691 (buffer (current-buffer)))
4692 (setq putstring (copy-sequence putstring))
4693 (let ((overlay (make-overlay pos pos buffer))
4695 (list (list 'margin 'left-margin) putstring))))
4696 (put-text-property 0 1 'display prop string)
4698 (add-text-properties 0 1 sprops string))
4699 (overlay-put overlay 'put-break t)
4700 (overlay-put overlay 'before-string string))))
4702 ;;from remove-images
4703 (defun gdb-remove-strings (start end &optional buffer)
4704 "Remove strings between START and END in BUFFER.
4705 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4706 BUFFER nil or omitted means use the current buffer."
4708 (setq buffer (current-buffer)))
4709 (dolist (overlay (overlays-in start end))
4710 (when (overlay-get overlay 'put-break)
4711 (delete-overlay overlay))))
4713 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4714 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4715 (start (- (car posns) 1))
4716 (end (+ (cdr posns) 1))
4717 (putstring (if enabled "B" "b"))
4718 (source-window (get-buffer-window (current-buffer) 0)))
4719 (add-text-properties
4720 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4723 (add-text-properties
4724 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4725 (add-text-properties
4726 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4727 (gdb-remove-breakpoint-icons start end)
4728 (if (display-images-p)
4729 (if (>= (or left-fringe-width
4730 (if source-window (car (window-fringes source-window)))
4731 gdb-buffer-fringe-width) 8)
4734 `(left-fringe breakpoint
4737 'breakpoint-disabled))
4739 'gdb-enabled enabled)
4740 (when (< left-margin-width 2)
4741 (save-current-buffer
4742 (setq left-margin-width 2)
4746 left-margin-width right-margin-width))))
4749 (or breakpoint-enabled-icon
4750 (setq breakpoint-enabled-icon
4751 (find-image `((:type xpm :data
4752 ,breakpoint-xpm-data
4753 :ascent 100 :pointer hand)
4755 ,breakpoint-enabled-pbm-data
4756 :ascent 100 :pointer hand)))))
4757 (or breakpoint-disabled-icon
4758 (setq breakpoint-disabled-icon
4759 (find-image `((:type xpm :data
4760 ,breakpoint-xpm-data
4761 :conversion disabled
4762 :ascent 100 :pointer hand)
4764 ,breakpoint-disabled-pbm-data
4765 :ascent 100 :pointer hand))))))
4769 (when (< left-margin-width 2)
4770 (save-current-buffer
4771 (setq left-margin-width 2)
4772 (let ((window (get-buffer-window (current-buffer) 0)))
4775 window left-margin-width right-margin-width)))))
4777 (propertize putstring
4779 'breakpoint-enabled 'breakpoint-disabled))
4782 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4783 (gdb-remove-strings start end)
4784 (if (display-images-p)
4785 (remove-images start end))
4787 (setq left-margin-width 0)
4788 (let ((window (get-buffer-window (current-buffer) 0)))
4791 window left-margin-width right-margin-width)))))
4794 ;;; Functions for inline completion.
4796 (defvar gud-gdb-fetch-lines-in-progress)
4797 (defvar gud-gdb-fetch-lines-string)
4798 (defvar gud-gdb-fetch-lines-break)
4799 (defvar gud-gdb-fetched-lines)
4801 (defun gud-gdbmi-completions (context command)
4802 "Completion table for GDB/MI commands.
4803 COMMAND is the prefix for which we seek completion.
4804 CONTEXT is the text before COMMAND on the line."
4805 (let ((gud-gdb-fetch-lines-in-progress t)
4806 (gud-gdb-fetch-lines-string nil)
4807 (gud-gdb-fetch-lines-break (length context))
4808 (gud-gdb-fetched-lines nil)
4809 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4810 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4811 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4812 (gdb-input (concat "complete " context command)
4813 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4814 (while gud-gdb-fetch-lines-in-progress
4815 (accept-process-output (get-buffer-process gud-comint-buffer))))
4816 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4818 (defun gud-gdbmi-fetch-lines-filter (string)
4819 "Custom filter function for `gud-gdbmi-completions'."
4820 (setq string (concat gud-gdb-fetch-lines-string
4821 (gud-gdbmi-marker-filter string)))
4822 (while (string-match "\n" string)
4823 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4824 gud-gdb-fetched-lines)
4825 (setq string (substring string (match-end 0))))
4830 ;;; gdb-mi.el ends here