1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2016 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 Mac OSX works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of Mac OSX.
76 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
77 ;; as earlier versions do not compile on Mac OSX.
81 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
82 ;; line information, e.g., a routine in libc (just a TODO item).
85 ;; 2) Watch windows to work with threads.
86 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
87 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
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 on program FILE in buffer *gud-FILE*.
677 The directory containing FILE becomes the initial working directory
678 and source-file directory for your debugger.
680 COMMAND-LINE is the shell command for starting the gdb session.
681 It should be a string consisting of the name of the gdb
682 executable followed by command line options. The command line
683 options should include \"-i=mi\" to use gdb's MI text interface.
684 Note that the old \"--annotate\" option is no longer supported.
686 If option `gdb-many-windows' is nil (the default value) then gdb just
687 pops up the GUD buffer unless `gdb-show-main' is t. In this case
688 it starts with two windows: one displaying the GUD buffer and the
689 other with the source file with the main routine of the inferior.
691 If option `gdb-many-windows' is t, regardless of the value of
692 `gdb-show-main', the layout below will appear. Keybindings are
693 shown in some of the buffers.
695 Watch expressions appear in the speedbar/slowbar.
697 The following commands help control operation :
699 `gdb-many-windows' - Toggle the number of windows gdb uses.
700 `gdb-restore-windows' - To restore the window layout.
702 See Info node `(emacs)GDB Graphical Interface' for a more
703 detailed description of this mode.
706 +----------------------------------------------------------------------+
708 +-----------------------------------+----------------------------------+
709 | GUD buffer (I/O of GDB) | Locals buffer |
713 +-----------------------------------+----------------------------------+
714 | Source buffer | I/O buffer (of debugged program) |
722 +-----------------------------------+----------------------------------+
723 | Stack buffer | Breakpoints buffer |
724 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
725 | | RET gdb-goto-breakpoint |
726 | | D gdb-delete-breakpoint |
727 +-----------------------------------+----------------------------------+"
729 (interactive (list (gud-query-cmdline 'gdb
)))
731 (when (and gud-comint-buffer
732 (buffer-name gud-comint-buffer
)
733 (get-buffer-process gud-comint-buffer
)
734 (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
)))
735 (gdb-restore-windows)
737 "Multiple debugging requires restarting in text command mode"))
739 (gud-common-init command-line nil
'gud-gdbmi-marker-filter
)
741 ;; Setup a temporary process filter to warn when GDB was not started
743 (let ((proc (get-buffer-process gud-comint-buffer
)))
744 (add-function :around
(process-filter proc
) #'gdb--check-interpreter
))
746 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
747 (set (make-local-variable 'gdb-control-level
) 0)
748 (setq comint-input-sender
'gdb-send
)
749 (when (ring-empty-p comint-input-ring
) ; cf shell-mode
750 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
751 (if (eq system-type
'ms-dos
)
754 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
755 (hsize (getenv "HISTSIZE")))
756 (dolist (file (append '("~/.gdbinit")
757 (unless (string-equal (expand-file-name ".")
758 (expand-file-name "~"))
760 (if (file-readable-p (setq file
(expand-file-name file
)))
762 (insert-file-contents file
)
763 ;; TODO? check for "set history save\\( *on\\)?" and do
764 ;; not use history otherwise?
765 (while (re-search-forward
766 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t
)
767 (cond ((string-equal (match-string 1) "filename")
768 (setq hfile
(expand-file-name
770 (file-name-directory file
))))
771 ((string-equal (match-string 1) "size")
772 (setq hsize
(match-string 2))))))))
774 (integerp (setq hsize
(string-to-number hsize
)))
776 (set (make-local-variable 'comint-input-ring-size
) hsize
))
778 (set (make-local-variable 'comint-input-ring-file-name
) hfile
))
779 (comint-read-input-ring t
)))
780 (gud-def gud-tbreak
"tbreak %f:%l" "\C-t"
781 "Set temporary breakpoint at current line.")
783 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
784 "\C-j" "Set execution address to current line.")
786 (gud-def gud-up
"up %p" "<" "Up N stack frames (numeric arg).")
787 (gud-def gud-down
"down %p" ">" "Down N stack frames (numeric arg).")
788 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
789 (gud-def gud-pstar
"print* %e" nil
790 "Evaluate C dereferenced pointer expression at point.")
792 (gud-def gud-step
(gdb-gud-context-call "-exec-step" "%p" t
)
794 "Step one source line with display.")
795 (gud-def gud-stepi
(gdb-gud-context-call "-exec-step-instruction" "%p" t
)
797 "Step one instruction with display.")
798 (gud-def gud-next
(gdb-gud-context-call "-exec-next" "%p" t
)
800 "Step one line (skip functions).")
801 (gud-def gud-nexti
(gdb-gud-context-call "-exec-next-instruction" "%p" t
)
803 "Step one instruction (skip functions).")
804 (gud-def gud-cont
(gdb-gud-context-call "-exec-continue")
806 "Continue with display.")
807 (gud-def gud-finish
(gdb-gud-context-call "-exec-finish" nil t
)
809 "Finish executing current function.")
810 (gud-def gud-run
"-exec-run"
814 (gud-def gud-break
(if (not (string-match "Disassembly" mode-name
))
815 (gud-call "break %f:%l" arg
)
819 (gud-call "break *%a" arg
)))
820 "\C-b" "Set breakpoint at current line or address.")
822 (gud-def gud-remove
(if (not (string-match "Disassembly" mode-name
))
823 (gud-call "clear %f:%l" arg
)
827 (gud-call "clear *%a" arg
)))
828 "\C-d" "Remove breakpoint at current line or address.")
830 ;; -exec-until doesn't support --all yet
831 (gud-def gud-until
(if (not (string-match "Disassembly" mode-name
))
832 (gud-call "-exec-until %f:%l" arg
)
836 (gud-call "-exec-until *%a" arg
)))
837 "\C-u" "Continue to current line or address.")
838 ;; TODO Why arg here?
840 gud-go
(gud-call (if gdb-active-process
841 (gdb-gud-context-command "-exec-continue")
843 nil
"Start or continue execution.")
845 ;; For debugging Emacs only.
849 "pp " (if (eq (buffer-local-value
850 'major-mode
(window-buffer)) 'speedbar-mode
)
851 (gdb-find-watch-expression) "%e")) arg
)
852 nil
"Print the Emacs s-expression.")
854 (define-key gud-minor-mode-map
[left-margin mouse-1
]
855 'gdb-mouse-set-clear-breakpoint
)
856 (define-key gud-minor-mode-map
[left-fringe mouse-1
]
857 'gdb-mouse-set-clear-breakpoint
)
858 (define-key gud-minor-mode-map
[left-margin C-mouse-1
]
859 'gdb-mouse-toggle-breakpoint-margin
)
860 (define-key gud-minor-mode-map
[left-fringe C-mouse-1
]
861 'gdb-mouse-toggle-breakpoint-fringe
)
863 (define-key gud-minor-mode-map
[left-margin drag-mouse-1
]
865 (define-key gud-minor-mode-map
[left-fringe drag-mouse-1
]
867 (define-key gud-minor-mode-map
[left-margin mouse-3
]
869 (define-key gud-minor-mode-map
[left-fringe mouse-3
]
872 (define-key gud-minor-mode-map
[left-margin C-drag-mouse-1
]
874 (define-key gud-minor-mode-map
[left-fringe C-drag-mouse-1
]
876 (define-key gud-minor-mode-map
[left-fringe C-mouse-3
]
878 (define-key gud-minor-mode-map
[left-margin C-mouse-3
]
881 (set (make-local-variable 'gud-gdb-completion-function
)
882 'gud-gdbmi-completions
)
884 (add-hook 'completion-at-point-functions
#'gud-gdb-completion-at-point
886 (local-set-key "\C-i" 'completion-at-point
)
888 (local-set-key [remap comint-delchar-or-maybe-eof
] 'gdb-delchar-or-quit
)
890 (setq gdb-first-prompt t
)
891 (setq gud-running nil
)
895 (run-hooks 'gdb-mode-hook
))
899 (setq gdb-selected-frame nil
901 gdb-thread-number nil
903 gdb-output-sink
'user
904 gdb-location-alist nil
905 gdb-source-file-list nil
910 gdb-first-done-or-error t
911 gdb-buffer-fringe-width
(car (window-fringes))
913 gdb-source-window nil
914 gdb-inferior-status nil
916 gdb-buf-publisher
'()
918 gdb-breakpoints-list
'()
919 gdb-register-names
'()
920 gdb-non-stop gdb-non-stop-setting
)
924 (setq gdb-buffer-type
'gdbmi
)
926 (gdb-force-mode-line-update
927 (propertize "initializing..." 'face font-lock-variable-name-face
))
929 (gdb-get-buffer-create 'gdb-inferior-io
)
930 (gdb-clear-inferior-io)
931 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
933 (when (eq system-type
'windows-nt
)
934 ;; Don't create a separate console window for the debuggee.
935 (gdb-input "-gdb-set new-console off" 'ignore
)
936 ;; Force GDB to behave as if its input and output stream were
937 ;; connected to a TTY device (since on Windows we use pipes for
938 ;; communicating with GDB).
939 (gdb-input "-gdb-set interactive-mode on" 'ignore
))
940 (gdb-input "-gdb-set height 0" 'ignore
)
943 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler
))
945 (gdb-input "-enable-pretty-printing" 'ignore
)
947 ;; Find source file and compilation directory here.
948 (if gdb-create-source-file-list
949 ;; Needs GDB 6.2 onwards.
950 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list
))
951 ;; Needs GDB 6.0 onwards.
952 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file
)
953 (gdb-input "-gdb-show prompt" 'gdb-get-prompt
))
955 (defun gdb-non-stop-handler ()
956 (goto-char (point-min))
957 (if (re-search-forward "No symbol" nil t
)
960 "This version of GDB doesn't support non-stop mode. Turning it off.")
961 (setq gdb-non-stop nil
)
962 (setq gdb-supports-non-stop nil
))
963 (setq gdb-supports-non-stop t
)
964 (gdb-input "-gdb-set target-async 1" 'ignore
)
965 (gdb-input "-list-target-features" 'gdb-check-target-async
)))
967 (defun gdb-check-target-async ()
968 (goto-char (point-min))
969 (unless (re-search-forward "async" nil t
)
971 "Target doesn't support non-stop mode. Turning it off.")
972 (setq gdb-non-stop nil
)
973 (gdb-input "-gdb-set non-stop 0" 'ignore
)))
975 (defun gdb-delchar-or-quit (arg)
976 "Delete ARG characters or send a quit command to GDB.
977 Send a quit only if point is at the end of the buffer, there is
978 no input, and GDB is waiting for input."
980 (unless (and (eq (current-buffer) gud-comint-buffer
)
981 (eq gud-minor-mode
'gdbmi
))
982 (error "Not in a GDB-MI buffer"))
983 (let ((proc (get-buffer-process gud-comint-buffer
)))
985 (process-live-p proc
)
987 (= (point) (marker-position (process-mark proc
))))
988 ;; Sending an EOF does not work with GDB-MI; submit an
989 ;; explicit quit command.
992 (comint-send-input t t
))
995 (defvar gdb-define-alist nil
"Alist of #define directives for GUD tooltips.")
997 (defun gdb-create-define-alist ()
998 "Create an alist of #define directives for GUD tooltips."
999 (let* ((file (buffer-file-name))
1001 (with-output-to-string
1002 (with-current-buffer standard-output
1004 (file-exists-p file
)
1005 ;; call-process doesn't work with remote file names.
1006 (not (file-remote-p default-directory
))
1007 (call-process shell-file-name file
1008 (list t nil
) nil
"-c"
1009 (concat gdb-cpp-define-alist-program
" "
1010 gdb-cpp-define-alist-flags
))))))
1011 (define-list (split-string output
"\n" t
))
1013 (setq gdb-define-alist nil
)
1014 (dolist (define define-list
)
1015 (setq name
(nth 1 (split-string define
"[( ]")))
1016 (push (cons name define
) gdb-define-alist
))))
1018 (declare-function tooltip-show
"tooltip" (text &optional use-echo-area
))
1020 (defconst gdb--string-regexp
"\"\\(?:[^\\\"]\\|\\\\.\\)*\"")
1022 (defun gdb-tooltip-print (expr)
1023 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
1024 (goto-char (point-min))
1026 ((re-search-forward (concat ".*value=\\(" gdb--string-regexp
1030 (concat expr
" = " (read (match-string 1)))
1031 (or gud-tooltip-echo-area
1032 (not (display-graphic-p)))))
1033 ((re-search-forward "msg=\\(\".+\"\\)$" nil t
)
1034 (tooltip-show (read (match-string 1))
1035 (or gud-tooltip-echo-area
1036 (not (display-graphic-p))))))))
1038 ;; If expr is a macro for a function don't print because of possible dangerous
1039 ;; side-effects. Also printing a function within a tooltip generates an
1040 ;; unexpected starting annotation (phase error).
1041 (defun gdb-tooltip-print-1 (expr)
1042 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
1043 (goto-char (point-min))
1044 (if (search-forward "expands to: " nil t
)
1045 (unless (looking-at "\\S-+.*(.*).*")
1046 (gdb-input (concat "-data-evaluate-expression \"" expr
"\"")
1047 `(lambda () (gdb-tooltip-print ,expr
)))))))
1049 (defun gdb-init-buffer ()
1050 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
1051 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1052 (when gud-tooltip-mode
1053 (make-local-variable 'gdb-define-alist
)
1054 (gdb-create-define-alist)
1055 (add-hook 'after-save-hook
'gdb-create-define-alist nil t
)))
1057 (defmacro gdb--if-arrow
(arrow-position start-posn end-posn
&rest body
)
1058 (declare (indent 3))
1059 (let ((buffer (make-symbol "buffer")))
1060 `(if ,arrow-position
1061 (let ((,buffer
(marker-buffer ,arrow-position
)))
1062 (if (equal ,buffer
(window-buffer (posn-window ,end-posn
)))
1063 (with-current-buffer ,buffer
1064 (when (or (equal ,start-posn
,end-posn
)
1065 (equal (posn-point ,start-posn
)
1066 (marker-position ,arrow-position
)))
1069 (defun gdb-mouse-until (event)
1070 "Continue running until a source line past the current line.
1071 The destination source line can be selected either by clicking
1072 with mouse-3 on the fringe/margin or dragging the arrow
1073 with mouse-1 (default bindings)."
1075 (let ((start (event-start event
))
1076 (end (event-end event
)))
1077 (gdb--if-arrow gud-overlay-arrow-position start end
1078 (let ((line (line-number-at-pos (posn-point end
))))
1079 (gud-call (concat "until " (number-to-string line
)))))
1080 (gdb--if-arrow gdb-disassembly-position start end
1082 (goto-char (point-min))
1083 (forward-line (1- (line-number-at-pos (posn-point end
))))
1085 (gud-call (concat "until *%a"))))))
1087 (defun gdb-mouse-jump (event)
1088 "Set execution address/line.
1089 The destination source line can be selected either by clicking with C-mouse-3
1090 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1091 Unlike `gdb-mouse-until' the destination address can be before the current
1092 line, and no execution takes place."
1094 (let ((start (event-start event
))
1095 (end (event-end event
)))
1096 (gdb--if-arrow gud-overlay-arrow-position start end
1097 (let ((line (line-number-at-pos (posn-point end
))))
1098 (gud-call (concat "tbreak " (number-to-string line
)))
1099 (gud-call (concat "jump " (number-to-string line
)))))
1100 (gdb--if-arrow gdb-disassembly-position start end
1102 (goto-char (point-min))
1103 (forward-line (1- (line-number-at-pos (posn-point end
))))
1105 (gud-call (concat "tbreak *%a"))
1106 (gud-call (concat "jump *%a"))))))
1108 (defcustom gdb-show-changed-values t
1109 "If non-nil change the face of out of scope variables and changed values.
1110 Out of scope variables are suppressed with `shadow' face.
1111 Changed values are highlighted with the face `font-lock-warning-face'."
1116 (defcustom gdb-max-children
40
1117 "Maximum number of children before expansion requires confirmation."
1122 (defcustom gdb-delete-out-of-scope t
1123 "If non-nil delete watch expressions automatically when they go out of scope."
1128 (define-minor-mode gdb-speedbar-auto-raise
1129 "Minor mode to automatically raise the speedbar for watch expressions.
1130 With prefix argument ARG, automatically raise speedbar if ARG is
1131 positive, otherwise don't automatically raise it."
1136 (defcustom gdb-use-colon-colon-notation nil
1137 "If non-nil use FUN::VAR format to display variables in the speedbar."
1142 (define-key gud-minor-mode-map
"\C-c\C-w" 'gud-watch
)
1143 (define-key global-map
(vconcat gud-key-prefix
"\C-w") 'gud-watch
)
1145 (declare-function tooltip-identifier-from-point
"tooltip" (point))
1147 (defun gud-watch (&optional arg event
)
1148 "Watch expression at point.
1149 With arg, enter name of variable to be watched in the minibuffer."
1150 (interactive (list current-prefix-arg last-input-event
))
1151 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer
)))
1152 (if (eq minor-mode
'gdbmi
)
1154 (if event
(posn-set-point (event-end event
)))
1156 (save-selected-window
1159 (completing-read "Name of variable: "
1160 'gud-gdb-complete-command
)
1161 (if (and transient-mark-mode mark-active
)
1162 (buffer-substring (region-beginning) (region-end))
1163 (concat (if (derived-mode-p 'gdb-registers-mode
) "$")
1164 (tooltip-identifier-from-point (point)))))))
1165 (set-text-properties 0 (length expr
) nil expr
)
1166 (gdb-input (concat "-var-create - * " expr
"")
1167 `(lambda () (gdb-var-create-handler ,expr
))))))
1168 (message "gud-watch is a no-op in this mode."))))
1170 (defun gdb-var-create-handler (expr)
1171 (let* ((result (gdb-json-partial-output)))
1172 (if (not (bindat-get-field result
'msg
))
1174 (list (bindat-get-field result
'name
)
1175 (if (and (string-equal gdb-current-language
"c")
1176 gdb-use-colon-colon-notation gdb-selected-frame
)
1177 (setq expr
(concat gdb-selected-frame
"::" expr
))
1179 (bindat-get-field result
'numchild
)
1180 (bindat-get-field result
'type
)
1181 (bindat-get-field result
'value
)
1183 (bindat-get-field result
'has_more
)
1184 gdb-frame-address
)))
1185 (push var gdb-var-list
)
1187 (unless (string-equal
1188 speedbar-initial-expansion-list-name
"GUD")
1189 (speedbar-change-initial-expansion-list "GUD")))
1190 (message-box "No symbol \"%s\" in current context." expr
))))
1192 (defun gdb-speedbar-update ()
1193 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1194 ;; Dummy command to update speedbar even when idle.
1195 (gdb-input "-environment-pwd"
1196 'gdb-speedbar-timer-fn
1197 'gdb-speedbar-update
)))
1199 (defun gdb-speedbar-timer-fn ()
1200 (if gdb-speedbar-auto-raise
1201 (raise-frame speedbar-frame
))
1202 (speedbar-timer-fn))
1204 (defun gdb-var-evaluate-expression-handler (varnum changed
)
1205 (goto-char (point-min))
1206 (re-search-forward (concat ".*value=\\(" gdb--string-regexp
"\\)")
1208 (let ((var (assoc varnum gdb-var-list
)))
1210 (if changed
(setcar (nthcdr 5 var
) 'changed
))
1211 (setcar (nthcdr 4 var
) (read (match-string 1)))))
1212 (gdb-speedbar-update))
1214 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1215 (defun gdb-var-list-children (varnum)
1216 (gdb-input (concat "-var-update " varnum
) 'ignore
)
1217 (gdb-input (concat "-var-list-children --all-values " varnum
)
1218 `(lambda () (gdb-var-list-children-handler ,varnum
))))
1220 (defun gdb-var-list-children-handler (varnum)
1221 (let* ((var-list nil
)
1222 (output (bindat-get-field (gdb-json-partial-output "child")))
1223 (children (bindat-get-field output
'children
)))
1224 (catch 'child-already-watched
1225 (dolist (var gdb-var-list
)
1226 (if (string-equal varnum
(car var
))
1228 ;; With dynamic varobjs numchild may have increased.
1229 (setcar (nthcdr 2 var
) (bindat-get-field output
'numchild
))
1231 (dolist (child children
)
1232 (let ((varchild (list (bindat-get-field child
'name
)
1233 (bindat-get-field child
'exp
)
1234 (bindat-get-field child
'numchild
)
1235 (bindat-get-field child
'type
)
1236 (bindat-get-field child
'value
)
1238 (bindat-get-field child
'has_more
))))
1239 (if (assoc (car varchild
) gdb-var-list
)
1240 (throw 'child-already-watched nil
))
1241 (push varchild var-list
))))
1242 (push var var-list
)))
1243 (setq gdb-var-list
(nreverse var-list
))))
1244 (gdb-speedbar-update))
1246 (defun gdb-var-set-format (format)
1247 "Set the output format for a variable displayed in the speedbar."
1248 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1250 (gdb-input (concat "-var-set-format " varnum
" " format
) 'ignore
)
1253 (defun gdb-var-delete-1 (var varnum
)
1254 (gdb-input (concat "-var-delete " varnum
) 'ignore
)
1255 (setq gdb-var-list
(delq var gdb-var-list
))
1256 (dolist (varchild gdb-var-list
)
1257 (if (string-match (concat (car var
) "\\.") (car varchild
))
1258 (setq gdb-var-list
(delq varchild gdb-var-list
)))))
1260 (defun gdb-var-delete ()
1261 "Delete watch expression at point from the speedbar."
1263 (let ((text (speedbar-line-text)))
1264 (string-match "\\(\\S-+\\)" text
)
1265 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1267 (if (string-match "\\." (car var
))
1268 (message-box "Can only delete a root expression")
1269 (gdb-var-delete-1 var varnum
)))))
1271 (defun gdb-var-delete-children (varnum)
1272 "Delete children of variable object at point from the speedbar."
1273 (gdb-input (concat "-var-delete -c " varnum
) 'ignore
))
1275 (defun gdb-edit-value (_text _token _indent
)
1276 "Assign a value to a variable displayed in the speedbar."
1277 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1279 (value (read-string "New value: ")))
1280 (gdb-input (concat "-var-assign " varnum
" " value
)
1281 `(lambda () (gdb-edit-value-handler ,value
)))))
1283 (defconst gdb-error-regexp
"\\^error,msg=\\(\".+\"\\)")
1285 (defun gdb-edit-value-handler (value)
1286 (goto-char (point-min))
1287 (if (re-search-forward gdb-error-regexp nil t
)
1288 (message-box "Invalid number or expression (%s)" value
)))
1290 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1291 (defun gdb-var-update ()
1292 (gdb-input "-var-update --all-values *"
1293 'gdb-var-update-handler
1296 (defun gdb-var-update-handler ()
1297 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist
)))
1298 (dolist (var gdb-var-list
)
1299 (setcar (nthcdr 5 var
) nil
))
1300 (let ((temp-var-list gdb-var-list
))
1301 (dolist (change changelist
)
1302 (let* ((varnum (bindat-get-field change
'name
))
1303 (var (assoc varnum gdb-var-list
))
1304 (new-num (bindat-get-field change
'new_num_children
)))
1306 (let ((scope (bindat-get-field change
'in_scope
))
1307 (has-more (bindat-get-field change
'has_more
)))
1308 (cond ((string-equal scope
"false")
1309 (if gdb-delete-out-of-scope
1310 (gdb-var-delete-1 var varnum
)
1311 (setcar (nthcdr 5 var
) 'out-of-scope
)))
1312 ((string-equal scope
"true")
1313 (setcar (nthcdr 6 var
) has-more
)
1314 (when (and (or (not has-more
)
1315 (string-equal has-more
"0"))
1317 (string-equal (nth 2 var
) "0"))
1318 (setcar (nthcdr 4 var
)
1319 (bindat-get-field change
'value
))
1320 (setcar (nthcdr 5 var
) 'changed
)))
1321 ((string-equal scope
"invalid")
1322 (gdb-var-delete-1 var varnum
)))))
1323 (let ((var-list nil
) var1
1324 (children (bindat-get-field change
'new_children
)))
1326 (setq var1
(pop temp-var-list
))
1328 (if (string-equal varnum
(car var1
))
1329 (let ((new (string-to-number new-num
))
1330 (previous (string-to-number (nth 2 var1
))))
1331 (setcar (nthcdr 2 var1
) new-num
)
1332 (push var1 var-list
)
1335 ;; Add new children to list.
1336 (dotimes (_ previous
)
1337 (push (pop temp-var-list
) var-list
))
1338 (dolist (child children
)
1340 (list (bindat-get-field child
'name
)
1341 (bindat-get-field child
'exp
)
1342 (bindat-get-field child
'numchild
)
1343 (bindat-get-field child
'type
)
1344 (bindat-get-field child
'value
)
1346 (bindat-get-field child
'has_more
))))
1347 (push varchild var-list
))))
1348 ;; Remove deleted children from list.
1351 (push (pop temp-var-list
) var-list
))
1352 (dotimes (_ (- previous new
))
1353 (pop temp-var-list
)))))
1354 (push var1 var-list
))
1355 (setq var1
(pop temp-var-list
)))
1356 (setq gdb-var-list
(nreverse var-list
))))))))
1357 (gdb-speedbar-update))
1359 (defun gdb-speedbar-expand-node (text token indent
)
1360 "Expand the node the user clicked on.
1361 TEXT is the text of the button we clicked on, a + or - item.
1362 TOKEN is data related to this node.
1363 INDENT is the current indentation depth."
1364 (cond ((string-match "+" text
) ;expand this node
1365 (let* ((var (assoc token gdb-var-list
))
1366 (expr (nth 1 var
)) (children (nth 2 var
)))
1367 (if (or (<= (string-to-number children
) gdb-max-children
)
1369 (format "%s has %s children. Continue? " expr children
)))
1370 (gdb-var-list-children token
))))
1371 ((string-match "-" text
) ;contract this node
1372 (dolist (var gdb-var-list
)
1373 (if (string-match (concat token
"\\.") (car var
))
1374 (setq gdb-var-list
(delq var gdb-var-list
))))
1375 (gdb-var-delete-children token
)
1376 (speedbar-change-expand-button-char ?
+)
1377 (speedbar-delete-subblock indent
))
1378 (t (error "Ooops... not sure what to do")))
1379 (speedbar-center-buffer-smartly))
1381 (defun gdb-get-target-string ()
1382 (with-current-buffer gud-comint-buffer
1389 ;; Each buffer has a TYPE -- a symbol that identifies the function
1390 ;; of that particular buffer.
1392 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1393 ;; is constructed specially.
1395 ;; Others are constructed by gdb-get-buffer-create and
1396 ;; named according to the rules set forth in the gdb-buffer-rules
1398 (defvar gdb-buffer-rules
'())
1400 (defun gdb-rules-name-maker (rules-entry)
1402 (defun gdb-rules-buffer-mode (rules-entry)
1403 (nth 2 rules-entry
))
1404 (defun gdb-rules-update-trigger (rules-entry)
1405 (nth 3 rules-entry
))
1407 (defun gdb-update-buffer-name ()
1408 "Rename current buffer according to name-maker associated with
1409 it in `gdb-buffer-rules'."
1410 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1411 gdb-buffer-rules
))))
1412 (when f
(rename-buffer (funcall f
)))))
1414 (defun gdb-current-buffer-rules ()
1415 "Get `gdb-buffer-rules' entry for current buffer type."
1416 (assoc gdb-buffer-type gdb-buffer-rules
))
1418 (defun gdb-current-buffer-thread ()
1419 "Get thread object of current buffer from `gdb-threads-list'.
1421 When current buffer is not bound to any thread, return main
1423 (cdr (assoc gdb-thread-number gdb-threads-list
)))
1425 (defun gdb-current-buffer-frame ()
1426 "Get current stack frame object for thread of current buffer."
1427 (bindat-get-field (gdb-current-buffer-thread) 'frame
))
1429 (defun gdb-buffer-type (buffer)
1430 "Get value of `gdb-buffer-type' for BUFFER."
1431 (with-current-buffer buffer
1434 (defun gdb-buffer-shows-main-thread-p ()
1435 "Return t if current GDB buffer shows main selected thread and
1436 is not bound to it."
1438 (not (local-variable-p 'gdb-thread-number
)))
1440 (defun gdb-get-buffer (buffer-type &optional thread
)
1441 "Get a specific GDB buffer.
1443 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1444 and `gdb-thread-number' (if provided) must be equal to THREAD."
1446 (dolist (buffer (buffer-list) nil
)
1447 (with-current-buffer buffer
1448 (when (and (eq gdb-buffer-type buffer-type
)
1450 (equal gdb-thread-number thread
)))
1451 (throw 'found buffer
))))))
1453 (defun gdb-get-buffer-create (buffer-type &optional thread
)
1454 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1455 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1457 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1458 buffer-local variable of the new buffer.
1460 Buffer mode and name are selected according to buffer type.
1462 If buffer has trigger associated with it in `gdb-buffer-rules',
1463 this trigger is subscribed to `gdb-buf-publisher' and called with
1465 (or (gdb-get-buffer buffer-type thread
)
1466 (let ((rules (assoc buffer-type gdb-buffer-rules
))
1467 (new (generate-new-buffer "limbo")))
1468 (with-current-buffer new
1469 (let ((mode (gdb-rules-buffer-mode rules
))
1470 (trigger (gdb-rules-update-trigger rules
)))
1471 (when mode
(funcall mode
))
1472 (setq gdb-buffer-type buffer-type
)
1474 (set (make-local-variable 'gdb-thread-number
) thread
))
1475 (set (make-local-variable 'gud-minor-mode
)
1476 (buffer-local-value 'gud-minor-mode gud-comint-buffer
))
1477 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1478 (rename-buffer (funcall (gdb-rules-name-maker rules
)))
1480 (gdb-add-subscriber gdb-buf-publisher
1481 (cons (current-buffer)
1482 (gdb-bind-function-to-buffer
1483 trigger
(current-buffer))))
1484 (funcall trigger
'start
))
1485 (current-buffer))))))
1487 (defun gdb-bind-function-to-buffer (expr buffer
)
1488 "Return a function which will evaluate EXPR in BUFFER."
1489 `(lambda (&rest args
)
1490 (with-current-buffer ,buffer
1491 (apply ',expr args
))))
1493 ;; Used to display windows with thread-bound buffers
1494 (defmacro def-gdb-preempt-display-buffer
(name buffer
&optional doc
1496 `(defun ,name
(&optional thread
)
1498 (message "%s" thread
)
1499 (gdb-preempt-existing-or-display-buffer
1500 (gdb-get-buffer-create ,buffer thread
)
1501 ,split-horizontal
)))
1503 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1504 ;; at least one and possible more functions. The functions have these
1505 ;; roles in defining a buffer type:
1507 ;; NAME - Return a name for this buffer type.
1509 ;; The remaining function(s) are optional:
1511 ;; MODE - called in a new buffer with no arguments, should establish
1512 ;; the proper mode for the buffer.
1515 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
1516 (let ((binding (assoc buffer-type gdb-buffer-rules
)))
1518 (setcdr binding rules
)
1519 (push (cons buffer-type rules
)
1520 gdb-buffer-rules
))))
1522 (defun gdb-parent-mode ()
1523 "Generic mode to derive all other GDB buffer modes from."
1524 (kill-all-local-variables)
1525 (setq buffer-read-only t
)
1526 (buffer-disable-undo)
1527 ;; Delete buffer from gdb-buf-publisher when it's killed
1528 ;; (if it has an associated update trigger)
1533 (let ((trigger (gdb-rules-update-trigger
1534 (gdb-current-buffer-rules))))
1536 (gdb-delete-subscriber
1538 ;; This should match gdb-add-subscriber done in
1539 ;; gdb-get-buffer-create
1540 (cons (current-buffer)
1541 (gdb-bind-function-to-buffer trigger
(current-buffer))))))))
1544 ;; Partial-output buffer : This accumulates output from a command executed on
1545 ;; behalf of emacs (rather than the user).
1547 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1548 'gdb-partial-output-name
)
1550 (defun gdb-partial-output-name ()
1551 (concat " *partial-output-"
1552 (gdb-get-target-string)
1556 (gdb-set-buffer-rules 'gdb-inferior-io
1557 'gdb-inferior-io-name
1558 'gdb-inferior-io-mode
)
1560 (defun gdb-inferior-io-name ()
1561 (concat "*input/output of "
1562 (gdb-get-target-string)
1565 (defun gdb-display-io-buffer ()
1566 "Display IO of debugged program in a separate window."
1568 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))
1570 (defun gdb-inferior-io--init-proc (proc)
1571 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1572 (set-process-filter proc
'gdb-inferior-filter
)
1573 (set-process-sentinel proc
'gdb-inferior-io-sentinel
)
1574 ;; The process can run on a remote host.
1575 (let ((tty (or (process-get proc
'remote-tty
)
1576 (process-tty-name proc
))))
1577 (unless (or (null tty
)
1580 (concat "-inferior-tty-set " tty
) 'ignore
))))
1582 (defun gdb-inferior-io-sentinel (proc _str
)
1583 (when (eq (process-status proc
) 'failed
)
1584 ;; When the debugged process exits, Emacs gets an EIO error on
1585 ;; read from the pty, and stops listening to it. If the gdb
1586 ;; process is still running, remove the pty, make a new one, and
1588 (let ((io-buffer (process-buffer proc
)))
1589 (when (and (process-live-p (get-buffer-process gud-comint-buffer
))
1590 (buffer-live-p io-buffer
))
1591 ;; `comint-exec' deletes the original process as a side effect.
1592 (comint-exec io-buffer
"gdb-inferior" nil nil nil
)
1593 (gdb-inferior-io--init-proc (get-buffer-process io-buffer
))))))
1595 (defcustom gdb-display-buffer-other-frame-action
1596 '((display-buffer-reuse-window display-buffer-pop-up-frame
)
1597 (reusable-frames . visible
)
1598 (inhibit-same-window . t
)
1599 (pop-up-frame-parameters (height .
14)
1602 (tool-bar-lines . nil
)
1603 (menu-bar-lines . nil
)
1604 (minibuffer . nil
)))
1605 "`display-buffer' action for displaying GDB utility frames."
1607 :type display-buffer--action-custom-type
1611 (defun gdb-frame-io-buffer ()
1612 "Display IO of debugged program in another frame."
1614 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1615 gdb-display-buffer-other-frame-action
))
1617 (defvar gdb-inferior-io-mode-map
1618 (let ((map (make-sparse-keymap)))
1619 (define-key map
"\C-c\C-c" 'gdb-io-interrupt
)
1620 (define-key map
"\C-c\C-z" 'gdb-io-stop
)
1621 (define-key map
"\C-c\C-\\" 'gdb-io-quit
)
1622 (define-key map
"\C-c\C-d" 'gdb-io-eof
)
1623 (define-key map
"\C-d" 'gdb-io-eof
)
1626 ;; We want to use comint because it has various nifty and familiar features.
1627 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
1628 "Major mode for gdb inferior-io."
1629 :syntax-table nil
:abbrev-table nil
1630 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil
))
1632 (defcustom gdb-display-io-nopopup nil
1633 "When non-nil, and the `gdb-inferior-io' buffer is buried, don't pop it up."
1638 (defun gdb-inferior-filter (proc string
)
1639 (unless (string-equal string
"")
1641 (unless (and gdb-display-io-nopopup
1642 (setq buf
(gdb-get-buffer 'gdb-inferior-io
))
1643 (null (get-buffer-window buf
)))
1644 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))))
1645 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1646 (comint-output-filter proc string
)))
1648 (defun gdb-io-interrupt ()
1649 "Interrupt the program being debugged."
1652 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1654 (defun gdb-io-quit ()
1655 "Send quit signal to the program being debugged."
1658 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1660 (defun gdb-io-stop ()
1661 "Stop the program being debugged."
1664 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1666 (defun gdb-io-eof ()
1667 "Send end-of-file to the program being debugged."
1670 (get-buffer-process gud-comint-buffer
)))
1672 (defun gdb-clear-inferior-io ()
1673 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1677 (defconst breakpoint-xpm-data
1679 static char *magick[] = {
1680 /* columns rows colors chars-per-pixel */
1696 "XPM data used for breakpoint icon.")
1698 (defconst breakpoint-enabled-pbm-data
1701 0 0 0 0 1 1 1 1 0 0 0 0
1702 0 0 0 1 1 1 1 1 1 0 0 0
1703 0 0 1 1 1 1 1 1 1 1 0 0
1704 0 1 1 1 1 1 1 1 1 1 1 0
1705 0 1 1 1 1 1 1 1 1 1 1 0
1706 0 1 1 1 1 1 1 1 1 1 1 0
1707 0 1 1 1 1 1 1 1 1 1 1 0
1708 0 0 1 1 1 1 1 1 1 1 0 0
1709 0 0 0 1 1 1 1 1 1 0 0 0
1710 0 0 0 0 1 1 1 1 0 0 0 0"
1711 "PBM data used for enabled breakpoint icon.")
1713 (defconst breakpoint-disabled-pbm-data
1725 0 0 0 1 0 1 0 1 0 0"
1726 "PBM data used for disabled breakpoint icon.")
1728 (defvar breakpoint-enabled-icon nil
1729 "Icon for enabled breakpoint in display margin.")
1731 (defvar breakpoint-disabled-icon nil
1732 "Icon for disabled breakpoint in display margin.")
1734 (declare-function define-fringe-bitmap
"fringe.c"
1735 (bitmap bits
&optional height width align
))
1737 (and (display-images-p)
1738 ;; Bitmap for breakpoint in fringe
1739 (define-fringe-bitmap 'breakpoint
1740 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1741 ;; Bitmap for gud-overlay-arrow in fringe
1742 (define-fringe-bitmap 'hollow-right-triangle
1743 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1745 (defface breakpoint-enabled
1749 "Face for enabled breakpoint icon in fringe."
1752 (defface breakpoint-disabled
1753 '((((class color
) (min-colors 88)) :foreground
"grey70")
1754 ;; Ensure that on low-color displays that we end up something visible.
1755 (((class color
) (min-colors 8) (background light
))
1756 :foreground
"black")
1757 (((class color
) (min-colors 8) (background dark
))
1758 :foreground
"white")
1759 (((type tty
) (class mono
))
1761 (t :background
"gray"))
1762 "Face for disabled breakpoint icon in fringe."
1766 (defvar gdb-control-commands-regexp
1769 "commands\\|if\\|while\\|define\\|document\\|"
1770 "python\\|python-interactive\\|pi\\|guile\\|guile-repl\\|gr\\|"
1771 "while-stepping\\|stepping\\|ws\\|actions"
1772 "\\)\\([[:blank:]]+.*\\)?$")
1773 "Regexp matching GDB commands that enter a recursive reading loop.
1774 As long as GDB is in the recursive reading loop, it does not expect
1775 commands to be prefixed by \"-interpreter-exec console\".")
1777 (defun gdb-strip-string-backslash (string)
1778 (replace-regexp-in-string "\\\\$" "" string
))
1780 (defun gdb-send (proc string
)
1781 "A comint send filter for gdb."
1782 (with-current-buffer gud-comint-buffer
1783 (let ((inhibit-read-only t
))
1784 (remove-text-properties (point-min) (point-max) '(face))))
1785 ;; mimic <RET> key to repeat previous command in GDB
1786 (when (= gdb-control-level
0)
1787 (if (not (string= "" string
))
1788 (if gdb-continuation
1789 (setq gdb-last-command
(concat gdb-continuation
1790 (gdb-strip-string-backslash string
)
1792 (setq gdb-last-command
(gdb-strip-string-backslash string
)))
1793 (if gdb-last-command
(setq string gdb-last-command
))
1794 (setq gdb-continuation nil
)))
1795 (if (and (not gdb-continuation
)
1796 (or (string-match "^-" string
)
1797 (> gdb-control-level
0)))
1798 ;; Either MI command or we are feeding GDB's recursive reading loop.
1800 (setq gdb-first-done-or-error t
)
1801 (process-send-string proc
(concat string
"\n"))
1802 (if (and (string-match
1804 (if (eq system-type
'windows-nt
) "\026" "\004")
1805 "\\|,q\\|,quit\\|end\\)$")
1807 (> gdb-control-level
0))
1808 (setq gdb-control-level
(1- gdb-control-level
))))
1810 (if (string-match "\\\\$" string
)
1811 (setq gdb-continuation
1812 (concat gdb-continuation
(gdb-strip-string-backslash
1815 (setq gdb-first-done-or-error t
)
1816 (let ((to-send (concat "-interpreter-exec console "
1817 (gdb-mi-quote (concat gdb-continuation string
" "))
1819 (if gdb-enable-debug
1820 (push (cons 'mi-send to-send
) gdb-debug-log
))
1821 (process-send-string proc to-send
))
1822 (if (and (string-match
1824 (if (eq system-type
'windows-nt
) "\026" "\004")
1825 "\\|,q\\|,quit\\|end\\)$")
1827 (> gdb-control-level
0))
1828 (setq gdb-control-level
(1- gdb-control-level
)))
1829 (setq gdb-continuation nil
)))
1830 (if (string-match gdb-control-commands-regexp string
)
1831 (setq gdb-control-level
(1+ gdb-control-level
))))
1833 (defun gdb-mi-quote (string)
1834 "Return STRING quoted properly as an MI argument.
1835 The string is enclosed in double quotes.
1836 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1837 (setq string
(replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string
))
1838 (setq string
(replace-regexp-in-string "\n" "\\n" string t t
))
1839 (concat "\"" string
"\""))
1841 (defun gdb-input (command handler-function
&optional trigger-name
)
1842 "Send COMMAND to GDB via the MI interface.
1843 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1844 complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
1845 Emacs is still waiting for a reply from another command previously
1846 sent with the same TRIGGER-NAME."
1847 (when (or (not trigger-name
)
1848 (not (gdb-pending-handler-p trigger-name
)))
1849 (setq gdb-token-number
(1+ gdb-token-number
))
1850 (setq command
(concat (number-to-string gdb-token-number
) command
))
1852 (if gdb-enable-debug
(push (list 'send-item command handler-function
)
1855 (gdb-add-handler gdb-token-number handler-function trigger-name
)
1857 (if gdbmi-debug-mode
(message "gdb-input: %s" command
))
1858 (process-send-string (get-buffer-process gud-comint-buffer
)
1859 (concat command
"\n"))))
1861 ;; NOFRAME is used for gud execution control commands
1862 (defun gdb-current-context-command (command)
1863 "Add --thread to gdb COMMAND when needed."
1864 (if (and gdb-thread-number
1865 gdb-supports-non-stop
)
1866 (concat command
" --thread " gdb-thread-number
)
1869 (defun gdb-current-context-buffer-name (name)
1870 "Add thread information and asterisks to string NAME.
1872 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1874 (if (local-variable-p 'gdb-thread-number
)
1875 (format " (bound to thread %s)" gdb-thread-number
)
1879 (defun gdb-current-context-mode-name (mode)
1880 "Add thread information to MODE which is to be used as `mode-name'."
1882 (if gdb-thread-number
1883 (format " [thread %s]" gdb-thread-number
)
1887 (defcustom gud-gdb-command-name
"gdb -i=mi"
1888 "Default command to execute an executable under the GDB debugger."
1893 (setq gud-running nil
)
1894 (setq gdb-output-sink
'user
)
1895 (gdb-remove-all-pending-triggers))
1897 (defun gdb-update (&optional no-proc
)
1898 "Update buffers showing status of debug session.
1899 If NO-PROC is non-nil, do not try to contact the GDB process."
1900 (when gdb-first-prompt
1901 (gdb-force-mode-line-update
1902 (propertize "initializing..." 'face font-lock-variable-name-face
))
1904 (setq gdb-first-prompt nil
))
1907 (gdb-get-main-selected-frame))
1909 ;; We may need to update gdb-threads-list so we can use
1910 (gdb-get-buffer-create 'gdb-threads-buffer
)
1911 ;; gdb-break-list is maintained in breakpoints handler
1912 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1915 (gdb-emit-signal gdb-buf-publisher
'update
))
1917 (gdb-get-changed-registers)
1918 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1919 (dolist (var gdb-var-list
)
1920 (setcar (nthcdr 5 var
) nil
))
1923 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1924 ;; because we may need to update current gud-running value without
1925 ;; changing current thread (see gdb-running)
1926 (defun gdb-setq-thread-number (number)
1927 "Set `gdb-thread-number' to NUMBER.
1928 Only this function must be used to change `gdb-thread-number'
1929 value to NUMBER, because `gud-running' and `gdb-frame-number'
1930 need to be updated appropriately when current thread changes."
1931 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1932 (unless (string-equal number
"0") (setq gdb-thread-number number
))
1933 (setq gdb-frame-number
"0")
1934 (gdb-update-gud-running))
1936 (defun gdb-update-gud-running ()
1937 "Set `gud-running' according to the state of current thread.
1939 `gdb-frame-number' is set to 0 if current thread is now stopped.
1941 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1942 cannot be reliably used to determine whether or not execution
1943 control buttons should be shown in menu or toolbar. Use
1944 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1947 For all-stop mode, thread information is unavailable while target
1949 (let ((old-value gud-running
))
1951 (string= (bindat-get-field (gdb-current-buffer-thread) 'state
)
1953 ;; Set frame number to "0" when _current_ threads stops.
1954 (when (and (gdb-current-buffer-thread)
1955 (not (eq gud-running old-value
)))
1956 (setq gdb-frame-number
"0"))))
1958 (defun gdb-show-run-p ()
1959 "Return t if \"Run/continue\" should be shown on the toolbar."
1960 (or (not gdb-active-process
)
1962 (not gdb-gud-control-all-threads
)
1965 (and gdb-gud-control-all-threads
1966 (> gdb-stopped-threads-count
0))))
1968 (defun gdb-show-stop-p ()
1969 "Return t if \"Stop\" should be shown on the toolbar."
1971 (not gdb-gud-control-all-threads
)
1974 (and gdb-gud-control-all-threads
1975 (> gdb-running-threads-count
0))))
1977 ;; GUD displays the selected GDB frame. This might might not be the current
1978 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1979 ;; visited breakpoint is, use that window.
1980 (defun gdb-display-source-buffer (buffer)
1981 (let* ((last-window (if gud-last-last-frame
1983 (gud-find-file (car gud-last-last-frame
)))))
1984 (source-window (or last-window
1985 (if (and gdb-source-window
1986 (window-live-p gdb-source-window
))
1987 gdb-source-window
))))
1989 (setq gdb-source-window source-window
)
1990 (set-window-buffer source-window buffer
))
1994 (defun gdbmi-start-with (str offset match
)
1995 "Return non-nil if string STR starts with MATCH, else returns nil.
1996 OFFSET is the position in STR at which the comparison takes place."
1997 (let ((match-length (length match
))
1998 (str-length (- (length str
) offset
)))
1999 (when (>= str-length match-length
)
2000 (string-equal match
(substring str offset
(+ offset match-length
))))))
2002 (defun gdbmi-same-start (str offset match
)
2003 "Return non-nil if STR and MATCH are equal up to the end of either strings.
2004 OFFSET is the position in STR at which the comparison takes place."
2005 (let* ((str-length (- (length str
) offset
))
2006 (match-length (length match
))
2007 (compare-length (min str-length match-length
)))
2008 (when (> compare-length
0)
2009 (string-equal (substring str offset
(+ offset compare-length
))
2010 (substring match
0 compare-length
)))))
2012 (defun gdbmi-is-number (character)
2013 "Return non-nil if CHARACTER is a numerical character between 0 and 9."
2014 (and (>= character ?
0)
2018 (defvar-local gdbmi-bnf-state
'gdbmi-bnf-output
2019 "Current GDB/MI output parser state.
2020 The parser is placed in a different state when an incomplete data steam is
2022 This variable will preserve the state required to resume the parsing
2023 when more data arrives.")
2025 (defvar-local gdbmi-bnf-offset
0
2026 "Offset in `gud-marker-acc' at which the parser is reading.
2027 This offset is used to be able to parse the GDB/MI message
2028 in-place, without the need of copying the string in a temporary buffer
2029 or discarding parsed tokens by substringing the message.")
2031 (defun gdbmi-bnf-init ()
2032 "Initialize the GDB/MI message parser."
2033 (setq gdbmi-bnf-state
'gdbmi-bnf-output
)
2034 (setq gdbmi-bnf-offset
0)
2035 (setq gud-marker-acc
""))
2038 (defun gdbmi-bnf-output ()
2039 "Implementation of the following GDB/MI output grammar rule:
2042 ( out-of-band-record )* [ result-record ] gdb-prompt"
2044 (gdbmi-bnf-skip-unrecognized)
2045 (while (gdbmi-bnf-out-of-band-record))
2046 (gdbmi-bnf-result-record)
2047 (gdbmi-bnf-gdb-prompt))
2050 (defun gdbmi-bnf-skip-unrecognized ()
2051 "Skip characters until is encounters the beginning of a valid record.
2052 Used as a protection mechanism in case something goes wrong when parsing
2053 a GDB/MI reply message."
2054 (let ((acc-length (length gud-marker-acc
))
2055 (prefix-offset gdbmi-bnf-offset
)
2056 (prompt "(gdb) \n"))
2058 (while (and (< prefix-offset acc-length
)
2059 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2060 (setq prefix-offset
(1+ prefix-offset
)))
2062 (if (and (< prefix-offset acc-length
)
2063 (not (memq (aref gud-marker-acc prefix-offset
)
2064 '(?^ ?
* ?
+ ?
= ?~ ?
@ ?
&)))
2065 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt
))
2066 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
2068 (let ((unrecognized-str (match-string 0 gud-marker-acc
)))
2069 (setq gdbmi-bnf-offset
(match-end 0))
2070 (if gdbmi-debug-mode
2071 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str
))
2072 (gdb-shell unrecognized-str
)
2076 (defun gdbmi-bnf-gdb-prompt ()
2077 "Implementation of the following GDB/MI output grammar rule:
2084 (let ((prompt "(gdb) \n"))
2085 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt
)
2086 (if gdbmi-debug-mode
(message "gdbmi-bnf-gdb-prompt: %s" prompt
))
2088 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length prompt
)))
2090 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
2091 ;; the end of a GDB reply message.
2095 (defun gdbmi-bnf-result-record ()
2096 "Implementation of the following GDB/MI output grammar rule:
2099 [ token ] `^' result-class ( `,' result )* nl
2102 any sequence of digits."
2104 (gdbmi-bnf-result-and-async-record-impl))
2107 (defun gdbmi-bnf-out-of-band-record ()
2108 "Implementation of the following GDB/MI output grammar rule:
2110 out-of-band-record ==>
2111 async-record | stream-record"
2113 (or (gdbmi-bnf-async-record)
2114 (gdbmi-bnf-stream-record)))
2117 (defun gdbmi-bnf-async-record ()
2118 "Implementation of the following GDB/MI output grammar rules:
2121 exec-async-output | status-async-output | notify-async-output
2123 exec-async-output ==>
2124 [ token ] `*' async-output
2126 status-async-output ==>
2127 [ token ] `+' async-output
2129 notify-async-output ==>
2130 [ token ] `=' async-output
2133 async-class ( `,' result )* nl"
2135 (gdbmi-bnf-result-and-async-record-impl))
2138 (defun gdbmi-bnf-stream-record ()
2139 "Implement the following GDB/MI output grammar rule:
2141 console-stream-output | target-stream-output | log-stream-output
2143 console-stream-output ==>
2146 target-stream-output ==>
2149 log-stream-output ==>
2151 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2152 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset
) '(?~ ?
@ ?
&))
2153 (string-match (concat "\\([~@&]\\)\\(" gdb--string-regexp
"\\)\n")
2156 (let ((prefix (match-string 1 gud-marker-acc
))
2157 (c-string (match-string 2 gud-marker-acc
)))
2159 (setq gdbmi-bnf-offset
(match-end 0))
2160 (if gdbmi-debug-mode
(message "gdbmi-bnf-stream-record: %s"
2161 (match-string 0 gud-marker-acc
)))
2163 (cond ((string-equal prefix
"~")
2164 (gdbmi-bnf-console-stream-output c-string
))
2165 ((string-equal prefix
"@")
2166 (gdbmi-bnf-target-stream-output c-string
))
2167 ((string-equal prefix
"&")
2168 (gdbmi-bnf-log-stream-output c-string
)))
2171 (defun gdbmi-bnf-console-stream-output (c-string)
2172 "Handler for the console-stream-output GDB/MI output grammar rule."
2173 (gdb-console c-string
))
2175 (defun gdbmi-bnf-target-stream-output (_c-string)
2176 "Handler for the target-stream-output GDB/MI output grammar rule."
2177 ;; Not currently used.
2180 (defun gdbmi-bnf-log-stream-output (c-string)
2181 "Handler for the log-stream-output GDB/MI output grammar rule."
2182 ;; Suppress "No registers." GDB 6.8 and earlier
2183 ;; duplicates MI error message on internal stream.
2184 ;; Don't print to GUD buffer.
2185 (if (not (string-equal (read c-string
) "No registers.\n"))
2186 (gdb-internals c-string
)))
2189 (defconst gdbmi-bnf-result-state-configs
2190 '(("^" .
(("done" .
(gdb-done . progressive
))
2191 ("error" .
(gdb-error . progressive
))
2192 ("running" .
(gdb-starting . atomic
))))
2193 ("*" .
(("stopped" .
(gdb-stopped . atomic
))
2194 ("running" .
(gdb-running . atomic
))))
2196 ("=" .
(("thread-created" .
(gdb-thread-created . atomic
))
2197 ("thread-selected" .
(gdb-thread-selected . atomic
))
2198 ("thread-existed" .
(gdb-ignored-notification . atomic
))
2199 ('default .
(gdb-ignored-notification . atomic
)))))
2200 "Alist of alists, mapping the type and class of message to a handler function.
2201 Handler functions are all flagged as either `progressive' or `atomic'.
2202 `progressive' handlers are capable of parsing incomplete messages.
2203 They can be called several time with new data chunk as they arrive from GDB.
2204 `progressive' handlers must have an extra argument that is set to a non-nil
2205 value when the message is complete.
2207 Implement the following GDB/MI output grammar rule:
2209 `done' | `running' | `connected' | `error' | `exit'
2212 `stopped' | others (where others will be added depending on the needs
2213 --this is still in development).")
2215 (defun gdbmi-bnf-result-and-async-record-impl ()
2216 "Common implementation of the result-record and async-record rule.
2217 Both rules share the same syntax. Those records may be very large in size.
2218 For that reason, the \"result\" part of the record is parsed by
2219 `gdbmi-bnf-incomplete-record-result', which will keep
2220 receiving characters as they arrive from GDB until the record is complete."
2221 (let ((acc-length (length gud-marker-acc
))
2222 (prefix-offset gdbmi-bnf-offset
))
2224 (while (and (< prefix-offset acc-length
)
2225 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2226 (setq prefix-offset
(1+ prefix-offset
)))
2228 (if (and (< prefix-offset acc-length
)
2229 (member (aref gud-marker-acc prefix-offset
) '(?
* ?
+ ?
= ?^
))
2230 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2231 gud-marker-acc gdbmi-bnf-offset
))
2233 (let ((token (match-string 1 gud-marker-acc
))
2234 (prefix (match-string 2 gud-marker-acc
))
2235 (class (match-string 3 gud-marker-acc
))
2236 (complete (string-equal (match-string 4 gud-marker-acc
) "\n"))
2240 (setq gdbmi-bnf-offset
(match-end 0))
2241 (if gdbmi-debug-mode
(message "gdbmi-bnf-result-record: %s"
2242 (match-string 0 gud-marker-acc
)))
2245 (cdr (assoc prefix gdbmi-bnf-result-state-configs
)))
2246 (setq class-command
(cdr (assoc class class-alist
)))
2247 (if (null class-command
)
2248 (setq class-command
(cdr (assoc 'default class-alist
))))
2252 (if (equal (cdr class-command
) 'progressive
)
2253 (funcall (car class-command
) token
"" complete
)
2254 (funcall (car class-command
) token
"")))
2255 (setq gdbmi-bnf-state
2257 (gdbmi-bnf-incomplete-record-result token class-command
)))
2258 (funcall gdbmi-bnf-state
))
2261 (defun gdbmi-bnf-incomplete-record-result (token class-command
)
2262 "State of the parser used to progressively parse a result-record or async-record
2263 rule from an incomplete data stream. The parser will stay in this state until
2264 the end of the current result or async record is reached."
2265 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2266 ;; Search the data stream for the end of the current record:
2267 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset
))
2268 (is-progressive (equal (cdr class-command
) 'progressive
))
2269 (is-complete (not (null newline-pos
)))
2272 (when gdbmi-debug-mode
2273 (message "gdbmi-bnf-incomplete-record-result: %s"
2274 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
)))
2276 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2277 ;; be processed by the class-command handler:
2278 (when (or is-complete is-progressive
)
2280 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
))
2282 ;; Move gdbmi-bnf-offset past the end of the chunk.
2283 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length result-str
)))
2285 (setq gdbmi-bnf-offset
(1+ gdbmi-bnf-offset
))))
2287 ;; Update the parsing state before invoking the handler in class-command
2288 ;; to make sure it's not left in an invalid state if the handler was
2289 ;; to generate an error.
2291 (setq gdbmi-bnf-state
'gdbmi-bnf-output
))
2295 (funcall (car class-command
) token result-str is-complete
)
2297 (funcall (car class-command
) token result-str
))))
2300 ;; Incomplete gdb response: abort parsing until we receive more data.
2301 (if gdbmi-debug-mode
(message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2302 (throw 'gdbmi-incomplete-stream nil
))
2307 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2308 ; The handling of those rules is currently done by the handlers registered
2309 ; in gdbmi-bnf-result-state-configs
2312 ; variable "=" value
2318 ; const | tuple | list
2324 ; "{}" | "{" result ( "," result )* "}"
2327 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2329 (defcustom gdb-mi-decode-strings nil
2330 "When non-nil, decode octal escapes in GDB output into non-ASCII text.
2332 If the value is a coding-system, use that coding-system to decode
2333 the bytes reconstructed from octal escapes. Any other non-nil value
2334 means to decode using the coding-system set for the GDB process.
2336 Warning: setting this non-nil might mangle strings reported by GDB
2337 that have literal substrings which match the \\nnn octal escape
2338 patterns, where nnn is an octal number between 200 and 377. So
2339 we only recommend to set this variable non-nil if the program you
2340 are debugging really reports non-ASCII text, or some of its source
2341 file names include non-ASCII characters."
2343 (const :tag
"Don't decode" nil
)
2344 (const :tag
"Decode using default coding-system" t
)
2345 (coding-system :tag
"Decode using this coding-system"))
2349 ;; The idea of the following function was suggested
2350 ;; by Kenichi Handa <handa@gnu.org>.
2352 ;; FIXME: This is fragile: it relies on the assumption that all the
2353 ;; non-ASCII strings output by GDB, including names of the source
2354 ;; files, values of string variables in the inferior, etc., are all
2355 ;; encoded in the same encoding. It also assumes that the \nnn
2356 ;; sequences are not split between chunks of output of the GDB process
2357 ;; due to buffering, and arrive together. Finally, if some string
2358 ;; included literal \nnn strings (as opposed to non-ASCII characters
2359 ;; converted by by GDB/MI to octal escapes), this decoding will mangle
2360 ;; those strings. When/if GDB acquires the ability to not
2361 ;; escape-protect non-ASCII characters in its MI output, this kludge
2362 ;; should be removed.
2363 (defun gdb-mi-decode (string)
2364 "Decode octal escapes in MI output STRING into multibyte text."
2366 (if (coding-system-p gdb-mi-decode-strings
)
2367 gdb-mi-decode-strings
2368 (with-current-buffer
2369 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2370 buffer-file-coding-system
))))
2372 (set-buffer-multibyte nil
)
2373 (prin1 string
(current-buffer))
2374 (goto-char (point-min))
2375 ;; prin1 quotes the octal escapes as well, which interferes with
2376 ;; their interpretation by 'read' below. Remove the extra
2377 ;; backslashes to countermand that.
2378 (while (re-search-forward "\\\\\\(\\\\[2-3][0-7][0-7]\\)" nil t
)
2379 (replace-match "\\1" nil nil
))
2380 (goto-char (point-min))
2381 (decode-coding-string (read (current-buffer)) coding
))))
2383 (defun gud-gdbmi-marker-filter (string)
2384 "Filter GDB/MI output."
2386 ;; If required, decode non-ASCII text encoded with octal escapes.
2387 (or (null gdb-mi-decode-strings
)
2388 (setq string
(gdb-mi-decode string
)))
2390 ;; Record transactions if logging is enabled.
2391 (when gdb-enable-debug
2392 (push (cons 'recv string
) gdb-debug-log
)
2393 (if (and gdb-debug-log-max
2394 (> (length gdb-debug-log
) gdb-debug-log-max
))
2395 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
2397 ;; Recall the left over gud-marker-acc from last time.
2398 (setq gud-marker-acc
(concat gud-marker-acc string
))
2400 ;; Start accumulating output for the GUD buffer.
2401 (setq gdb-filter-output
"")
2403 (let ((acc-length (length gud-marker-acc
)))
2404 (catch 'gdbmi-incomplete-stream
2405 (while (and (< gdbmi-bnf-offset acc-length
)
2406 (funcall gdbmi-bnf-state
)))))
2408 (when (/= gdbmi-bnf-offset
0)
2409 (setq gud-marker-acc
(substring gud-marker-acc gdbmi-bnf-offset
))
2410 (setq gdbmi-bnf-offset
0))
2412 (when (and gdbmi-debug-mode
(> (length gud-marker-acc
) 0))
2413 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc
))
2417 (defun gdb-gdb (_output-field))
2419 (defun gdb-shell (output-field)
2420 (setq gdb-filter-output
2421 (concat output-field gdb-filter-output
)))
2423 (defun gdb-ignored-notification (_token _output-field
))
2425 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2426 (defun gdb-thread-created (_token _output-field
))
2427 (defun gdb-thread-exited (_token output-field
)
2428 "Handle =thread-exited async record.
2429 Unset `gdb-thread-number' if current thread exited and update threads list."
2430 (let* ((thread-id (bindat-get-field (gdb-json-string output-field
) 'id
)))
2431 (if (string= gdb-thread-number thread-id
)
2432 (gdb-setq-thread-number nil
))
2433 ;; When we continue current thread and it quickly exits,
2434 ;; the pending triggers in gdb-handler-list left after gdb-running
2435 ;; disallow us to properly call -thread-info without --thread option.
2436 ;; Thus we need to use gdb-wait-for-pending.
2437 (gdb-wait-for-pending
2438 (gdb-emit-signal gdb-buf-publisher
'update-threads
))))
2440 (defun gdb-thread-selected (_token output-field
)
2441 "Handler for =thread-selected MI output record.
2443 Sets `gdb-thread-number' to new id."
2444 (let* ((result (gdb-json-string output-field
))
2445 (thread-id (bindat-get-field result
'id
)))
2446 (gdb-setq-thread-number thread-id
)
2447 ;; Typing `thread N' in GUD buffer makes GDB emit `^done' followed
2448 ;; by `=thread-selected' notification. `^done' causes `gdb-update'
2449 ;; as usually. Things happen too fast and second call (from
2450 ;; gdb-thread-selected handler) gets cut off by our beloved
2451 ;; pending triggers.
2452 ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
2453 ;; body will get executed when `gdb-handler-list' if free of
2454 ;; pending triggers.
2455 (gdb-wait-for-pending
2458 (defun gdb-running (_token output-field
)
2460 (bindat-get-field (gdb-json-string output-field
) 'thread-id
)))
2461 ;; We reset gdb-frame-number to nil if current thread has gone
2462 ;; running. This can't be done in gdb-thread-list-handler-custom
2463 ;; because we need correct gdb-frame-number by the time
2464 ;; -thread-info command is sent.
2465 (when (or (string-equal thread-id
"all")
2466 (string-equal thread-id gdb-thread-number
))
2467 (setq gdb-frame-number nil
)))
2468 (setq gdb-inferior-status
"running")
2469 (gdb-force-mode-line-update
2470 (propertize gdb-inferior-status
'face font-lock-type-face
))
2471 (when (not gdb-non-stop
)
2472 (setq gud-running t
))
2473 (setq gdb-active-process t
))
2475 (defun gdb-starting (_output-field _result
)
2476 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2477 (setq gdb-inferior-status
"running")
2478 (gdb-force-mode-line-update
2479 (propertize gdb-inferior-status
'face font-lock-type-face
))
2480 (setq gdb-active-process t
)
2481 (setq gud-running t
))
2483 ;; -break-insert -t didn't give a reason before gdb 6.9
2485 (defun gdb-stopped (_token output-field
)
2486 "Given the contents of *stopped MI async record, select new
2487 current thread and update GDB buffers."
2488 ;; Reason is available with target-async only
2489 (let* ((result (gdb-json-string output-field
))
2490 (reason (bindat-get-field result
'reason
))
2491 (thread-id (bindat-get-field result
'thread-id
))
2492 (retval (bindat-get-field result
'return-value
))
2493 (varnum (bindat-get-field result
'gdb-result-var
)))
2495 ;; -data-list-register-names needs to be issued for any stopped
2497 (when (not gdb-register-names
)
2498 (gdb-input (concat "-data-list-register-names"
2499 (if gdb-supports-non-stop
2500 (concat " --thread " thread-id
)))
2501 'gdb-register-names-handler
))
2503 ;; Don't set gud-last-frame here as it's currently done in
2504 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2508 ;; ;; Extract the frame position from the marker.
2509 ;; gud-last-frame (cons file
2510 ;; (string-to-number
2511 ;; (match-string 6 gud-marker-acc)))))
2513 (setq gdb-inferior-status
(or reason
"unknown"))
2514 (gdb-force-mode-line-update
2515 (propertize gdb-inferior-status
'face font-lock-warning-face
))
2516 (if (string-equal reason
"exited-normally")
2517 (setq gdb-active-process nil
))
2519 (when (and retval varnum
2520 ;; When the user typed CLI commands, GDB/MI helpfully
2521 ;; includes the "Value returned" response in the "~"
2522 ;; record; here we avoid displaying it twice.
2523 (not (string-match "^Value returned is " gdb-filter-output
)))
2524 (setq gdb-filter-output
2525 (concat gdb-filter-output
2526 (format "Value returned is %s = %s\n" varnum retval
))))
2528 ;; Select new current thread.
2530 ;; Don't switch if we have no reasons selected
2531 (when gdb-switch-reasons
2532 ;; Switch from another stopped thread only if we have
2533 ;; gdb-switch-when-another-stopped:
2534 (when (or gdb-switch-when-another-stopped
2535 (not (string= "stopped"
2536 (bindat-get-field (gdb-current-buffer-thread) 'state
))))
2537 ;; Switch if current reason has been selected or we have no
2539 (if (or (eq gdb-switch-reasons t
)
2540 (member reason gdb-switch-reasons
))
2541 (when (not (string-equal gdb-thread-number thread-id
))
2542 (message "Switched to thread %s" thread-id
)
2543 (gdb-setq-thread-number thread-id
))
2544 (message "Thread %s stopped" thread-id
))))
2546 ;; Print "(gdb)" to GUD console
2547 (when gdb-first-done-or-error
2548 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2550 ;; In non-stop, we update information as soon as another thread gets
2552 (when (or gdb-first-done-or-error
2554 ;; In all-stop this updates gud-running properly as well.
2556 (setq gdb-first-done-or-error nil
))
2557 (run-hook-with-args 'gdb-stopped-functions result
)))
2559 ;; Remove the trimmings from log stream containing debugging messages
2560 ;; being produced by GDB's internals, use warning face and send to GUD
2562 (defun gdb-internals (output-field)
2563 (setq gdb-filter-output
2566 (if (string= output-field
"\"\\n\"")
2568 (let ((error-message
2569 (read output-field
)))
2571 0 (length error-message
)
2572 'face font-lock-warning-face
2576 ;; Remove the trimmings from the console stream and send to GUD buffer
2577 ;; (frontend MI commands should not print to this stream)
2578 (defun gdb-console (output-field)
2579 (setq gdb-filter-output
2580 (gdb-concat-output gdb-filter-output
(read output-field
))))
2582 (defun gdb-done (token-number output-field is-complete
)
2583 (gdb-done-or-error token-number
'done output-field is-complete
))
2585 (defun gdb-error (token-number output-field is-complete
)
2586 (gdb-done-or-error token-number
'error output-field is-complete
))
2588 (defun gdb-done-or-error (token-number type output-field is-complete
)
2589 (if (string-equal token-number
"")
2590 ;; Output from command entered by user
2592 (setq gdb-output-sink
'user
)
2593 (setq token-number nil
)
2594 ;; MI error - send to minibuffer
2595 (when (eq type
'error
)
2596 ;; Skip "msg=" from `output-field'
2597 (message "%s" (read (substring output-field
4)))
2598 ;; Don't send to the console twice. (If it is a console error
2599 ;; it is also in the console stream.)
2600 (setq output-field nil
)))
2601 ;; Output from command from frontend.
2602 (setq gdb-output-sink
'emacs
))
2604 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2605 (let* ((proc (get-buffer-process gud-comint-buffer
))
2606 (no-proc (or (null proc
)
2607 (memq (process-status proc
) '(exit signal
)))))
2609 (when (and is-complete gdb-first-done-or-error
)
2610 (unless (or token-number gud-running no-proc
)
2611 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2612 (gdb-update no-proc
)
2613 (setq gdb-first-done-or-error nil
))
2615 (setq gdb-filter-output
2616 (gdb-concat-output gdb-filter-output output-field
))
2618 ;; We are done concatenating to the output sink. Restore it to user sink:
2619 (setq gdb-output-sink
'user
)
2621 (when (and token-number is-complete
)
2622 (with-current-buffer
2623 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2624 (gdb-handle-reply (string-to-number token-number
))))
2627 (gdb-clear-partial-output))))
2629 (defun gdb-concat-output (so-far new
)
2631 ((eq gdb-output-sink
'user
) (concat so-far new
))
2632 ((eq gdb-output-sink
'emacs
)
2633 (gdb-append-to-partial-output new
)
2636 (defun gdb-append-to-partial-output (string)
2637 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2638 (goto-char (point-max))
2641 (defun gdb-clear-partial-output ()
2642 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2645 (defun gdb-jsonify-buffer (&optional fix-key fix-list
)
2646 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2648 Field names are wrapped in double quotes and equal signs are
2649 replaced with semicolons.
2651 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2652 partial output. This is used to get rid of useless keys in lists
2653 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2654 -break-info are examples of MI commands which issue such
2657 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2658 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2659 -break-info output when it contains breakpoint script field
2660 incompatible with GDB/MI output syntax."
2662 (goto-char (point-min))
2665 (while (re-search-forward (concat "[\\[,]\\(" fix-key
"=\\)") nil t
)
2666 (replace-match "" nil nil nil
1))))
2669 ;; Find positions of braces which enclose broken list
2670 (while (re-search-forward (concat fix-list
"={\"") nil t
)
2671 (let ((p1 (goto-char (- (point) 2)))
2672 (p2 (progn (forward-sexp)
2674 ;; Replace braces with brackets
2682 (goto-char (point-min))
2684 (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
2685 gdb--string-regexp
"\\)")))
2686 (while (re-search-forward re nil t
)
2687 (replace-match "\"\\1\":\\2" nil nil
)))
2688 (goto-char (point-max))
2691 (defun gdb-json-read-buffer (&optional fix-key fix-list
)
2692 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2694 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2695 (gdb-jsonify-buffer fix-key fix-list
)
2697 (goto-char (point-min))
2698 (let ((json-array-type 'list
))
2701 (defun gdb-json-string (string &optional fix-key fix-list
)
2702 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2704 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2707 (gdb-json-read-buffer fix-key fix-list
)))
2709 (defun gdb-json-partial-output (&optional fix-key fix-list
)
2710 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2712 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2713 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2714 (gdb-json-read-buffer fix-key fix-list
)))
2716 (defun gdb-line-posns (line)
2717 "Return a pair of LINE beginning and end positions."
2718 (let ((offset (1+ (- line
(line-number-at-pos)))))
2720 (line-beginning-position offset
)
2721 (line-end-position offset
))))
2723 (defmacro gdb-mark-line
(line variable
)
2724 "Set VARIABLE marker to point at beginning of LINE.
2726 If current window has no fringes, inverse colors on LINE.
2728 Return position where LINE begins."
2730 (let* ((posns (gdb-line-posns ,line
))
2731 (start-posn (car posns
))
2732 (end-posn (cdr posns
)))
2733 (set-marker ,variable
(copy-marker start-posn
))
2734 (when (not (> (car (window-fringes)) 0))
2735 (put-text-property start-posn end-posn
2736 'font-lock-face
'(:inverse-video t
)))
2739 (defun gdb-pad-string (string padding
)
2740 (format (concat "%" (number-to-string padding
) "s") string
))
2742 ;; gdb-table struct is a way to programmatically construct simple
2743 ;; tables. It help to reliably align columns of data in GDB buffers
2745 (cl-defstruct gdb-table
2748 (row-properties nil
)
2751 (defun gdb-table-add-row (table row
&optional properties
)
2752 "Add ROW of string to TABLE and recalculate column sizes.
2754 When non-nil, PROPERTIES will be added to the whole row when
2755 calling `gdb-table-string'."
2756 (let ((rows (gdb-table-rows table
))
2757 (row-properties (gdb-table-row-properties table
))
2758 (column-sizes (gdb-table-column-sizes table
))
2759 (right-align (gdb-table-right-align table
)))
2760 (when (not column-sizes
)
2761 (setf (gdb-table-column-sizes table
)
2762 (make-list (length row
) 0)))
2763 (setf (gdb-table-rows table
)
2764 (append rows
(list row
)))
2765 (setf (gdb-table-row-properties table
)
2766 (append row-properties
(list properties
)))
2767 (setf (gdb-table-column-sizes table
)
2768 (cl-mapcar (lambda (x s
)
2770 (max (abs x
) (string-width (or s
"")))))
2771 (if right-align new-x
(- new-x
))))
2772 (gdb-table-column-sizes table
)
2774 ;; Avoid trailing whitespace at eol
2775 (if (not (gdb-table-right-align table
))
2776 (setcar (last (gdb-table-column-sizes table
)) 0))))
2778 (defun gdb-table-string (table &optional sep
)
2779 "Return TABLE as a string with columns separated with SEP."
2780 (let ((column-sizes (gdb-table-column-sizes table
)))
2784 (lambda (row properties
)
2786 (mapconcat 'identity
2787 (cl-mapcar (lambda (s x
) (gdb-pad-string s x
))
2791 (gdb-table-rows table
)
2792 (gdb-table-row-properties table
))
2795 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2796 (defun gdb-get-many-fields (struct &rest fields
)
2797 "Return a list of FIELDS values from STRUCT."
2799 (dolist (field fields
)
2800 (push (bindat-get-field struct field
) values
))
2803 (defmacro def-gdb-auto-update-trigger
(trigger-name gdb-command
2805 &optional signal-list
)
2806 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2807 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2808 buffer with `gdb-bind-function-to-buffer'.
2810 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2811 defined trigger is called with an argument from SIGNAL-LIST. It's
2812 not recommended to define triggers with empty SIGNAL-LIST.
2813 Normally triggers should respond at least to the `update' signal.
2815 Normally the trigger defined by this command must be called from
2816 the buffer where HANDLER-NAME must work. This should be done so
2817 that buffer-local thread number may be used in GDB-COMMAND (by
2818 calling `gdb-current-context-command').
2819 `gdb-bind-function-to-buffer' is used to achieve this, see
2820 `gdb-get-buffer-create'.
2822 Triggers defined by this command are meant to be used as a
2823 trigger argument when describing buffer types with
2824 `gdb-set-buffer-rules'."
2825 `(defun ,trigger-name
(&optional signal
)
2827 (or (not ,signal-list
)
2828 (memq signal
,signal-list
))
2829 (gdb-input ,gdb-command
2830 (gdb-bind-function-to-buffer ',handler-name
(current-buffer))
2831 (cons (current-buffer) ',trigger-name
)))))
2833 ;; Used by disassembly buffer only, the rest use
2834 ;; def-gdb-trigger-and-handler
2835 (defmacro def-gdb-auto-update-handler
(handler-name custom-defun
2836 &optional nopreserve
)
2837 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2839 Handlers are normally called from the buffers they put output in.
2841 Erase current buffer and evaluate CUSTOM-DEFUN.
2842 Then call `gdb-update-buffer-name'.
2844 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2845 `(defun ,handler-name
()
2846 (let* ((inhibit-read-only t
)
2847 ,@(unless nopreserve
2848 '((window (get-buffer-window (current-buffer) 0))
2849 (start (window-start window
))
2850 (p (window-point window
)))))
2853 (gdb-update-buffer-name)
2854 ,@(when (not nopreserve
)
2855 '((set-window-start window start
)
2856 (set-window-point window p
))))))
2858 (defmacro def-gdb-trigger-and-handler
(trigger-name gdb-command
2859 handler-name custom-defun
2860 &optional signal-list
)
2861 "Define trigger and handler.
2863 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2864 See `def-gdb-auto-update-trigger'.
2866 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2867 See `def-gdb-auto-update-handler'."
2869 (def-gdb-auto-update-trigger ,trigger-name
2871 ,handler-name
,signal-list
)
2872 (def-gdb-auto-update-handler ,handler-name
2877 ;; Breakpoint buffer : This displays the output of `-break-list'.
2878 (def-gdb-trigger-and-handler
2879 gdb-invalidate-breakpoints
"-break-list"
2880 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2883 (gdb-set-buffer-rules
2884 'gdb-breakpoints-buffer
2885 'gdb-breakpoints-buffer-name
2886 'gdb-breakpoints-mode
2887 'gdb-invalidate-breakpoints
)
2889 (defun gdb-breakpoints-list-handler-custom ()
2890 (let ((breakpoints-list (bindat-get-field
2891 (gdb-json-partial-output "bkpt" "script")
2892 'BreakpointTable
'body
))
2893 (table (make-gdb-table)))
2894 (setq gdb-breakpoints-list nil
)
2895 (gdb-table-add-row table
'("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2896 (dolist (breakpoint breakpoints-list
)
2897 (add-to-list 'gdb-breakpoints-list
2898 (cons (bindat-get-field breakpoint
'number
)
2900 (let ((at (bindat-get-field breakpoint
'at
))
2901 (pending (bindat-get-field breakpoint
'pending
))
2902 (func (bindat-get-field breakpoint
'func
))
2903 (type (bindat-get-field breakpoint
'type
)))
2904 (gdb-table-add-row table
2906 (bindat-get-field breakpoint
'number
)
2908 (or (bindat-get-field breakpoint
'disp
) "")
2909 (let ((flag (bindat-get-field breakpoint
'enabled
)))
2910 (if (string-equal flag
"y")
2912 (propertize "y" 'font-lock-face
2913 font-lock-warning-face
))
2915 (propertize "n" 'font-lock-face
2916 font-lock-comment-face
))))
2917 (bindat-get-field breakpoint
'addr
)
2918 (or (bindat-get-field breakpoint
'times
) "")
2919 (if (and type
(string-match ".*watchpoint" type
))
2920 (bindat-get-field breakpoint
'what
)
2923 (propertize (or func
"unknown")
2924 'font-lock-face font-lock-function-name-face
)
2925 (gdb-frame-location breakpoint
)))))
2926 ;; Add clickable properties only for breakpoints with file:line
2928 (append (list 'gdb-breakpoint breakpoint
)
2929 (when func
'(help-echo "mouse-2, RET: visit breakpoint"
2930 mouse-face highlight
))))))
2931 (insert (gdb-table-string table
" "))
2932 (gdb-place-breakpoints)))
2934 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2935 (defun gdb-place-breakpoints ()
2936 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2937 (dolist (buffer (buffer-list))
2938 (with-current-buffer buffer
2939 (if (and (eq gud-minor-mode
'gdbmi
)
2940 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2941 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2942 (dolist (breakpoint gdb-breakpoints-list
)
2943 (let* ((breakpoint (cdr breakpoint
)) ; gdb-breakpoints-list is
2944 ; an associative list
2945 (line (bindat-get-field breakpoint
'line
)))
2947 (let ((file (bindat-get-field breakpoint
'fullname
))
2948 (flag (bindat-get-field breakpoint
'enabled
))
2949 (bptno (bindat-get-field breakpoint
'number
)))
2950 (unless (and file
(file-exists-p file
))
2951 (setq file
(cdr (assoc bptno gdb-location-alist
))))
2953 (string-equal file
"File not found"))
2954 ;; If the full filename is not recorded in the
2955 ;; breakpoint structure or in `gdb-location-alist', use
2956 ;; -file-list-exec-source-file to extract it.
2957 (when (setq file
(bindat-get-field breakpoint
'file
))
2958 (gdb-input (concat "list " file
":1") 'ignore
)
2959 (gdb-input "-file-list-exec-source-file"
2960 `(lambda () (gdb-get-location
2961 ,bptno
,line
,flag
))))
2962 (with-current-buffer (find-file-noselect file
'nowarn
)
2964 ;; Only want one breakpoint icon at each location.
2965 (gdb-put-breakpoint-icon (string-equal flag
"y") bptno
2966 (string-to-number line
)))))))))
2968 (defconst gdb-source-file-regexp
2969 (concat "fullname=\\(" gdb--string-regexp
"\\)"))
2971 (defun gdb-get-location (bptno line flag
)
2972 "Find the directory containing the relevant source file.
2973 Put in buffer and place breakpoint icon."
2974 (goto-char (point-min))
2975 (catch 'file-not-found
2976 (if (re-search-forward gdb-source-file-regexp nil t
)
2977 (delete (cons bptno
"File not found") gdb-location-alist
)
2978 ;; FIXME: Why/how do we use (match-string 1) when the search failed?
2979 (push (cons bptno
(match-string 1)) gdb-location-alist
)
2981 (unless (assoc bptno gdb-location-alist
)
2982 (push (cons bptno
"File not found") gdb-location-alist
)
2983 (message-box "Cannot find source file for breakpoint location.
2984 Add directory to search path for source files using the GDB command, dir."))
2985 (throw 'file-not-found nil
))
2986 (with-current-buffer (find-file-noselect (match-string 1))
2988 ;; only want one breakpoint icon at each location
2989 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
(string-to-number line
)))))
2991 (add-hook 'find-file-hook
'gdb-find-file-hook
)
2993 (defun gdb-find-file-hook ()
2994 "Set up buffer for debugging if file is part of the source code
2995 of the current session."
2996 (if (and (buffer-name gud-comint-buffer
)
2997 ;; in case gud or gdb-ui is just loaded
2999 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
3001 (if (member buffer-file-name gdb-source-file-list
)
3002 (with-current-buffer (find-buffer-visiting buffer-file-name
)
3003 (gdb-init-buffer)))))
3005 (declare-function gud-remove
"gdb-mi" t t
) ; gud-def
3006 (declare-function gud-break
"gdb-mi" t t
) ; gud-def
3007 (declare-function fringe-bitmaps-at-pos
"fringe.c" (&optional pos window
))
3009 (defun gdb-mouse-set-clear-breakpoint (event)
3010 "Set/clear breakpoint in left fringe/margin at mouse click.
3011 If not in a source or disassembly buffer just set point."
3013 (mouse-minibuffer-check event
)
3014 (let ((posn (event-end event
)))
3015 (with-selected-window (posn-window posn
)
3016 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode
))
3017 (if (numberp (posn-point posn
))
3019 (goto-char (posn-point posn
))
3020 (if (or (posn-object posn
)
3021 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
3025 (posn-set-point posn
))))
3027 (defun gdb-mouse-toggle-breakpoint-margin (event)
3028 "Enable/disable breakpoint in left margin with mouse click."
3030 (mouse-minibuffer-check event
)
3031 (let ((posn (event-end event
)))
3032 (if (numberp (posn-point posn
))
3033 (with-selected-window (posn-window posn
)
3035 (goto-char (posn-point posn
))
3036 (if (posn-object posn
)
3038 (let ((bptno (get-text-property
3039 0 'gdb-bptno
(car (posn-string posn
)))))
3041 (if (get-text-property
3042 0 'gdb-enabled
(car (posn-string posn
)))
3047 (defun gdb-mouse-toggle-breakpoint-fringe (event)
3048 "Enable/disable breakpoint in left fringe with mouse click."
3050 (mouse-minibuffer-check event
)
3051 (let* ((posn (event-end event
))
3052 (pos (posn-point posn
))
3055 (with-selected-window (posn-window posn
)
3056 (with-current-buffer (window-buffer)
3058 (dolist (overlay (overlays-in pos pos
))
3059 (when (overlay-get overlay
'put-break
)
3060 (setq obj
(overlay-get overlay
'before-string
))))
3064 (if (get-text-property 0 'gdb-enabled obj
)
3067 (get-text-property 0 'gdb-bptno obj
)))))))))
3069 (defun gdb-breakpoints-buffer-name ()
3070 (concat "*breakpoints of " (gdb-get-target-string) "*"))
3072 (defun gdb-display-breakpoints-buffer (&optional thread
)
3073 "Display GDB breakpoints."
3075 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)))
3077 (defun gdb-frame-breakpoints-buffer (&optional thread
)
3078 "Display GDB breakpoints in another frame."
3080 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)
3081 gdb-display-buffer-other-frame-action
))
3083 (defvar gdb-breakpoints-mode-map
3084 (let ((map (make-sparse-keymap))
3085 (menu (make-sparse-keymap "Breakpoints")))
3086 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
3087 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
3088 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
3089 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
3090 (suppress-keymap map)
3091 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
3092 (define-key map " " 'gdb-toggle-breakpoint)
3093 (define-key map "D" 'gdb-delete-breakpoint)
3094 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
3095 (define-key map "q" 'gdb-delete-frame-or-window)
3096 (define-key map "\r" 'gdb-goto-breakpoint)
3097 (define-key map "\t" (lambda ()
3099 (gdb-set-window-buffer
3100 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
3101 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3102 (define-key map [follow-link] 'mouse-face)
3105 (defun gdb-delete-frame-or-window ()
3106 "Delete frame if there is only one window. Otherwise delete the window."
3108 (if (one-window-p) (delete-frame)
3111 ;;from make-mode-line-mouse-map
3112 (defun gdb-make-header-line-mouse-map (mouse function) "\
3113 Return a keymap with single entry for mouse key MOUSE on the header line.
3114 MOUSE is defined to run function FUNCTION with no args in the buffer
3115 corresponding to the mode line clicked."
3116 (let ((map (make-sparse-keymap)))
3117 (define-key map (vector 'header-line mouse) function)
3118 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3121 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3123 'help-echo ,help-echo
3124 'mouse-face ',mouse-face
3127 (gdb-make-header-line-mouse-map
3129 (lambda (event) (interactive "e")
3130 (save-selected-window
3131 (select-window (posn-window (event-start event)))
3132 (gdb-set-window-buffer
3133 (gdb-get-buffer-create ',buffer) t) )))))
3136 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3139 (defun gdb-threads-buffer-name ()
3140 (concat "*threads of " (gdb-get-target-string) "*"))
3142 (defun gdb-display-threads-buffer (&optional thread)
3143 "Display GDB threads."
3145 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3147 (defun gdb-frame-threads-buffer (&optional thread)
3148 "Display GDB threads in another frame."
3150 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3151 gdb-display-buffer-other-frame-action))
3153 (def-gdb-trigger-and-handler
3154 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3155 gdb-thread-list-handler gdb-thread-list-handler-custom
3156 '(start update update-threads))
3158 (gdb-set-buffer-rules
3160 'gdb-threads-buffer-name
3162 'gdb-invalidate-threads)
3164 (defvar gdb-threads-font-lock-keywords
3165 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3166 (" \\(stopped\\)" (1 font-lock-warning-face))
3167 (" \\(running\\)" (1 font-lock-string-face))
3168 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3169 "Font lock keywords used in `gdb-threads-mode'.")
3171 (defvar gdb-threads-mode-map
3172 (let ((map (make-sparse-keymap)))
3173 (define-key map "\r" 'gdb-select-thread)
3174 (define-key map "f" 'gdb-display-stack-for-thread)
3175 (define-key map "F" 'gdb-frame-stack-for-thread)
3176 (define-key map "l" 'gdb-display-locals-for-thread)
3177 (define-key map "L" 'gdb-frame-locals-for-thread)
3178 (define-key map "r" 'gdb-display-registers-for-thread)
3179 (define-key map "R" 'gdb-frame-registers-for-thread)
3180 (define-key map "d" 'gdb-display-disassembly-for-thread)
3181 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3182 (define-key map "i" 'gdb-interrupt-thread)
3183 (define-key map "c" 'gdb-continue-thread)
3184 (define-key map "s" 'gdb-step-thread)
3185 (define-key map "\t"
3188 (gdb-set-window-buffer
3189 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3190 (define-key map [mouse-2] 'gdb-select-thread)
3191 (define-key map [follow-link] 'mouse-face)
3194 (defvar gdb-threads-header
3196 (gdb-propertize-header
3197 "Breakpoints" gdb-breakpoints-buffer
3198 "mouse-1: select" mode-line-highlight mode-line-inactive)
3200 (gdb-propertize-header "Threads" gdb-threads-buffer
3201 nil nil mode-line)))
3203 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3204 "Major mode for GDB threads."
3205 (setq gdb-thread-position (make-marker))
3206 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3207 (setq header-line-format gdb-threads-header)
3208 (set (make-local-variable 'font-lock-defaults)
3209 '(gdb-threads-font-lock-keywords))
3210 'gdb-invalidate-threads)
3212 (defun gdb-thread-list-handler-custom ()
3213 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3214 (table (make-gdb-table))
3216 (setq gdb-threads-list nil)
3217 (setq gdb-running-threads-count 0)
3218 (setq gdb-stopped-threads-count 0)
3219 (set-marker gdb-thread-position nil)
3221 (dolist (thread (reverse threads-list))
3222 (let ((running (equal (bindat-get-field thread 'state) "running")))
3223 (add-to-list 'gdb-threads-list
3224 (cons (bindat-get-field thread 'id)
3226 (cl-incf (if running
3227 gdb-running-threads-count
3228 gdb-stopped-threads-count))
3233 (bindat-get-field thread 'id)
3235 (if gdb-thread-buffer-verbose-names
3236 (concat (bindat-get-field thread 'target-id) " ") "")
3237 (bindat-get-field thread 'state)
3238 ;; Include frame information for stopped threads
3241 " in " (bindat-get-field thread 'frame 'func)
3242 (if gdb-thread-buffer-arguments
3245 (let ((args (bindat-get-field thread 'frame 'args)))
3248 (apply #'format "%s=%s"
3249 (gdb-get-many-fields arg 'name 'value)))
3253 (if gdb-thread-buffer-locations
3254 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3255 (if gdb-thread-buffer-addresses
3256 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3260 'mouse-face 'highlight
3261 'help-echo "mouse-2, RET: select thread")))
3262 (when (string-equal gdb-thread-number
3263 (bindat-get-field thread 'id))
3264 (setq marked-line (length gdb-threads-list))))
3265 (insert (gdb-table-string table " "))
3267 (gdb-mark-line marked-line gdb-thread-position)))
3268 ;; We update gud-running here because we need to make sure that
3269 ;; gdb-threads-list is up-to-date
3270 (gdb-update-gud-running)
3271 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3273 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3274 "Define a NAME command which will act upon thread on the current line.
3276 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3277 be the value of `gdb-thread' property of the current line.
3278 If `gdb-thread' is nil, error is signaled."
3279 `(defun ,name (&optional event)
3281 (interactive (list last-input-event))
3282 (if event (posn-set-point (event-end event)))
3285 (let ((thread (get-text-property (point) 'gdb-thread)))
3288 (error "Not recognized as thread line"))))))
3290 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3292 "Define a NAME which will call BUFFER-COMMAND with id of thread
3293 on the current line."
3294 `(def-gdb-thread-buffer-command ,name
3295 (,buffer-command (bindat-get-field thread 'id))
3298 (def-gdb-thread-buffer-command gdb-select-thread
3299 (let ((new-id (bindat-get-field thread 'id)))
3300 (gdb-setq-thread-number new-id)
3301 (gdb-input (concat "-thread-select " new-id) 'ignore)
3303 "Select the thread at current line of threads buffer.")
3305 (def-gdb-thread-buffer-simple-command
3306 gdb-display-stack-for-thread
3307 gdb-preemptively-display-stack-buffer
3308 "Display stack buffer for the thread at current line.")
3310 (def-gdb-thread-buffer-simple-command
3311 gdb-display-locals-for-thread
3312 gdb-preemptively-display-locals-buffer
3313 "Display locals buffer for the thread at current line.")
3315 (def-gdb-thread-buffer-simple-command
3316 gdb-display-registers-for-thread
3317 gdb-preemptively-display-registers-buffer
3318 "Display registers buffer for the thread at current line.")
3320 (def-gdb-thread-buffer-simple-command
3321 gdb-display-disassembly-for-thread
3322 gdb-preemptively-display-disassembly-buffer
3323 "Display disassembly buffer for the thread at current line.")
3325 (def-gdb-thread-buffer-simple-command
3326 gdb-frame-stack-for-thread
3327 gdb-frame-stack-buffer
3328 "Display another frame with stack buffer for thread at current line.")
3330 (def-gdb-thread-buffer-simple-command
3331 gdb-frame-locals-for-thread
3332 gdb-frame-locals-buffer
3333 "Display another frame with locals buffer for thread at current line.")
3335 (def-gdb-thread-buffer-simple-command
3336 gdb-frame-registers-for-thread
3337 gdb-frame-registers-buffer
3338 "Display another frame with registers buffer for the thread at current line.")
3340 (def-gdb-thread-buffer-simple-command
3341 gdb-frame-disassembly-for-thread
3342 gdb-frame-disassembly-buffer
3343 "Display another frame with disassembly buffer for the thread at current line.")
3345 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3346 "Define a NAME which will execute GUD-COMMAND with
3347 `gdb-thread-number' locally bound to id of thread on the current
3349 `(def-gdb-thread-buffer-command ,name
3351 (let ((gdb-thread-number (bindat-get-field thread 'id))
3352 (gdb-gud-control-all-threads nil))
3353 (call-interactively #',gud-command))
3354 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3357 (def-gdb-thread-buffer-gud-command
3358 gdb-interrupt-thread
3360 "Interrupt thread at current line.")
3362 ;; Defined opaquely in M-x gdb via gud-def.
3363 (declare-function gud-cont "gdb-mi" (arg) t)
3365 (def-gdb-thread-buffer-gud-command
3368 "Continue thread at current line.")
3370 (declare-function gud-step "gdb-mi" (arg) t)
3372 (def-gdb-thread-buffer-gud-command
3375 "Step thread at current line.")
3380 (defcustom gdb-memory-rows 8
3381 "Number of data rows in memory window."
3386 (defcustom gdb-memory-columns 4
3387 "Number of data columns in memory window."
3392 (defcustom gdb-memory-format "x"
3393 "Display format of data items in memory window."
3394 :type '(choice (const :tag "Hexadecimal" "x")
3395 (const :tag "Signed decimal" "d")
3396 (const :tag "Unsigned decimal" "u")
3397 (const :tag "Octal" "o")
3398 (const :tag "Binary" "t"))
3402 (defcustom gdb-memory-unit 4
3403 "Unit size of data items in memory window."
3404 :type '(choice (const :tag "Byte" 1)
3405 (const :tag "Halfword" 2)
3406 (const :tag "Word" 4)
3407 (const :tag "Giant word" 8))
3411 (def-gdb-trigger-and-handler
3412 gdb-invalidate-memory
3413 (format "-data-read-memory %s %s %d %d %d"
3419 gdb-read-memory-handler
3420 gdb-read-memory-custom
3423 (gdb-set-buffer-rules
3425 'gdb-memory-buffer-name
3427 'gdb-invalidate-memory)
3429 (defun gdb-memory-column-width (size format)
3430 "Return length of string with memory unit of SIZE in FORMAT.
3432 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3433 in `gdb-memory-format'."
3434 (let ((format-base (cdr (assoc format
3436 ("d" . 10) ("u" . 10)
3440 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3441 (cond ((string-equal format "x")
3442 (+ 2 res)) ; hexadecimal numbers have 0x in front
3443 ((or (string-equal format "d")
3444 (string-equal format "o"))
3447 (error "Unknown format"))))
3449 (defun gdb-read-memory-custom ()
3450 (let* ((res (gdb-json-partial-output))
3451 (err-msg (bindat-get-field res 'msg)))
3453 (let ((memory (bindat-get-field res 'memory)))
3454 (setq gdb-memory-address (bindat-get-field res 'addr))
3455 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3456 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3457 (setq gdb-memory-last-address gdb-memory-address)
3458 (dolist (row memory)
3459 (insert (concat (bindat-get-field row 'addr) ":"))
3460 (dolist (column (bindat-get-field row 'data))
3461 (insert (gdb-pad-string column
3462 (+ 2 (gdb-memory-column-width
3464 gdb-memory-format)))))
3466 ;; Show last page instead of empty buffer when out of bounds
3468 (let ((gdb-memory-address gdb-memory-last-address))
3469 (gdb-invalidate-memory 'update)
3470 (error err-msg))))))
3472 (defvar gdb-memory-mode-map
3473 (let ((map (make-sparse-keymap)))
3474 (suppress-keymap map t)
3475 (define-key map "q" 'kill-this-buffer)
3476 (define-key map "n" 'gdb-memory-show-next-page)
3477 (define-key map "p" 'gdb-memory-show-previous-page)
3478 (define-key map "a" 'gdb-memory-set-address)
3479 (define-key map "t" 'gdb-memory-format-binary)
3480 (define-key map "o" 'gdb-memory-format-octal)
3481 (define-key map "u" 'gdb-memory-format-unsigned)
3482 (define-key map "d" 'gdb-memory-format-signed)
3483 (define-key map "x" 'gdb-memory-format-hexadecimal)
3484 (define-key map "b" 'gdb-memory-unit-byte)
3485 (define-key map "h" 'gdb-memory-unit-halfword)
3486 (define-key map "w" 'gdb-memory-unit-word)
3487 (define-key map "g" 'gdb-memory-unit-giant)
3488 (define-key map "R" 'gdb-memory-set-rows)
3489 (define-key map "C" 'gdb-memory-set-columns)
3492 (defun gdb-memory-set-address-event (event)
3493 "Handle a click on address field in memory buffer header."
3495 (save-selected-window
3496 (select-window (posn-window (event-start event)))
3497 (gdb-memory-set-address)))
3499 ;; Non-event version for use within keymap
3500 (defun gdb-memory-set-address ()
3501 "Set the start memory address."
3503 (let ((arg (read-from-minibuffer "Memory address: ")))
3504 (setq gdb-memory-address arg))
3505 (gdb-invalidate-memory 'update))
3507 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3508 "Define a function NAME which reads new VAR value from minibuffer."
3509 `(defun ,name (event)
3512 (save-selected-window
3513 (select-window (posn-window (event-start event)))
3514 (let* ((arg (read-from-minibuffer ,echo-string))
3515 (count (string-to-number arg)))
3517 (error "Positive number only")
3518 (customize-set-variable ',variable count)
3519 (gdb-invalidate-memory 'update))))))
3521 (def-gdb-set-positive-number
3525 "Set the number of data rows in memory window.")
3527 (def-gdb-set-positive-number
3528 gdb-memory-set-columns
3531 "Set the number of data columns in memory window.")
3533 (defmacro def-gdb-memory-format (name format doc)
3534 "Define a function NAME to switch memory buffer to use FORMAT.
3536 DOC is an optional documentation string."
3537 `(defun ,name () ,(when doc doc)
3539 (customize-set-variable 'gdb-memory-format ,format)
3540 (gdb-invalidate-memory 'update)))
3542 (def-gdb-memory-format
3543 gdb-memory-format-binary "t"
3544 "Set the display format to binary.")
3546 (def-gdb-memory-format
3547 gdb-memory-format-octal "o"
3548 "Set the display format to octal.")
3550 (def-gdb-memory-format
3551 gdb-memory-format-unsigned "u"
3552 "Set the display format to unsigned decimal.")
3554 (def-gdb-memory-format
3555 gdb-memory-format-signed "d"
3556 "Set the display format to decimal.")
3558 (def-gdb-memory-format
3559 gdb-memory-format-hexadecimal "x"
3560 "Set the display format to hexadecimal.")
3562 (defvar gdb-memory-format-map
3563 (let ((map (make-sparse-keymap)))
3564 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3566 "Keymap to select format in the header line.")
3568 (defvar gdb-memory-format-menu
3569 (let ((map (make-sparse-keymap "Format")))
3571 (define-key map [binary]
3572 '(menu-item "Binary" gdb-memory-format-binary
3573 :button (:radio . (equal gdb-memory-format "t"))))
3574 (define-key map [octal]
3575 '(menu-item "Octal" gdb-memory-format-octal
3576 :button (:radio . (equal gdb-memory-format "o"))))
3577 (define-key map [unsigned]
3578 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3579 :button (:radio . (equal gdb-memory-format "u"))))
3580 (define-key map [signed]
3581 '(menu-item "Signed Decimal" gdb-memory-format-signed
3582 :button (:radio . (equal gdb-memory-format "d"))))
3583 (define-key map [hexadecimal]
3584 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3585 :button (:radio . (equal gdb-memory-format "x"))))
3587 "Menu of display formats in the header line.")
3589 (defun gdb-memory-format-menu (event)
3591 (x-popup-menu event gdb-memory-format-menu))
3593 (defun gdb-memory-format-menu-1 (event)
3595 (save-selected-window
3596 (select-window (posn-window (event-start event)))
3597 (let* ((selection (gdb-memory-format-menu event))
3598 (binding (and selection (lookup-key gdb-memory-format-menu
3599 (vector (car selection))))))
3600 (if binding (call-interactively binding)))))
3602 (defmacro def-gdb-memory-unit (name unit-size doc)
3603 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3605 DOC is an optional documentation string."
3606 `(defun ,name () ,(when doc doc)
3608 (customize-set-variable 'gdb-memory-unit ,unit-size)
3609 (gdb-invalidate-memory 'update)))
3611 (def-gdb-memory-unit gdb-memory-unit-giant 8
3612 "Set the unit size to giant words (eight bytes).")
3614 (def-gdb-memory-unit gdb-memory-unit-word 4
3615 "Set the unit size to words (four bytes).")
3617 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3618 "Set the unit size to halfwords (two bytes).")
3620 (def-gdb-memory-unit gdb-memory-unit-byte 1
3621 "Set the unit size to bytes.")
3623 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3624 "Define a function NAME which show new address in memory buffer.
3626 The defined function switches Memory buffer to show address
3627 stored in ADDRESS-VAR variable.
3629 DOC is an optional documentation string."
3633 (let ((gdb-memory-address ,address-var))
3634 (gdb-invalidate-memory))))
3636 (def-gdb-memory-show-page gdb-memory-show-previous-page
3637 gdb-memory-prev-page)
3639 (def-gdb-memory-show-page gdb-memory-show-next-page
3640 gdb-memory-next-page)
3642 (defvar gdb-memory-unit-map
3643 (let ((map (make-sparse-keymap)))
3644 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3646 "Keymap to select units in the header line.")
3648 (defvar gdb-memory-unit-menu
3649 (let ((map (make-sparse-keymap "Unit")))
3650 (define-key map [giantwords]
3651 '(menu-item "Giant words" gdb-memory-unit-giant
3652 :button (:radio . (equal gdb-memory-unit 8))))
3653 (define-key map [words]
3654 '(menu-item "Words" gdb-memory-unit-word
3655 :button (:radio . (equal gdb-memory-unit 4))))
3656 (define-key map [halfwords]
3657 '(menu-item "Halfwords" gdb-memory-unit-halfword
3658 :button (:radio . (equal gdb-memory-unit 2))))
3659 (define-key map [bytes]
3660 '(menu-item "Bytes" gdb-memory-unit-byte
3661 :button (:radio . (equal gdb-memory-unit 1))))
3663 "Menu of units in the header line.")
3665 (defun gdb-memory-unit-menu (event)
3667 (x-popup-menu event gdb-memory-unit-menu))
3669 (defun gdb-memory-unit-menu-1 (event)
3671 (save-selected-window
3672 (select-window (posn-window (event-start event)))
3673 (let* ((selection (gdb-memory-unit-menu event))
3674 (binding (and selection (lookup-key gdb-memory-unit-menu
3675 (vector (car selection))))))
3676 (if binding (call-interactively binding)))))
3678 (defvar gdb-memory-font-lock-keywords
3679 '(;; <__function.name+n>
3680 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3681 (1 font-lock-function-name-face)))
3682 "Font lock keywords used in `gdb-memory-mode'.")
3684 (defvar gdb-memory-header
3689 'face font-lock-warning-face
3690 'help-echo "mouse-1: decrement address"
3691 'mouse-face 'mode-line-highlight
3692 'local-map (gdb-make-header-line-mouse-map
3694 #'gdb-memory-show-previous-page))
3697 'face font-lock-warning-face
3698 'help-echo "mouse-1: increment address"
3699 'mouse-face 'mode-line-highlight
3700 'local-map (gdb-make-header-line-mouse-map
3702 #'gdb-memory-show-next-page))
3704 (propertize gdb-memory-address
3705 'face font-lock-warning-face
3706 'help-echo "mouse-1: set start address"
3707 'mouse-face 'mode-line-highlight
3708 'local-map (gdb-make-header-line-mouse-map
3710 #'gdb-memory-set-address-event))
3712 (propertize (number-to-string gdb-memory-rows)
3713 'face font-lock-warning-face
3714 'help-echo "mouse-1: set number of columns"
3715 'mouse-face 'mode-line-highlight
3716 'local-map (gdb-make-header-line-mouse-map
3718 #'gdb-memory-set-rows))
3720 (propertize (number-to-string gdb-memory-columns)
3721 'face font-lock-warning-face
3722 'help-echo "mouse-1: set number of columns"
3723 'mouse-face 'mode-line-highlight
3724 'local-map (gdb-make-header-line-mouse-map
3726 #'gdb-memory-set-columns))
3728 (propertize gdb-memory-format
3729 'face font-lock-warning-face
3730 'help-echo "mouse-3: select display format"
3731 'mouse-face 'mode-line-highlight
3732 'local-map gdb-memory-format-map)
3734 (propertize (number-to-string gdb-memory-unit)
3735 'face font-lock-warning-face
3736 'help-echo "mouse-3: select unit size"
3737 'mouse-face 'mode-line-highlight
3738 'local-map gdb-memory-unit-map)))
3739 "Header line used in `gdb-memory-mode'.")
3741 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3742 "Major mode for examining memory."
3743 (setq header-line-format gdb-memory-header)
3744 (set (make-local-variable 'font-lock-defaults)
3745 '(gdb-memory-font-lock-keywords))
3746 'gdb-invalidate-memory)
3748 (defun gdb-memory-buffer-name ()
3749 (concat "*memory of " (gdb-get-target-string) "*"))
3751 (defun gdb-display-memory-buffer (&optional thread)
3752 "Display GDB memory contents."
3754 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3756 (defun gdb-frame-memory-buffer ()
3757 "Display memory contents in another frame."
3759 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3760 gdb-display-buffer-other-frame-action))
3763 ;;; Disassembly view
3765 (defun gdb-disassembly-buffer-name ()
3766 (gdb-current-context-buffer-name
3767 (concat "disassembly of " (gdb-get-target-string))))
3769 (defun gdb-display-disassembly-buffer (&optional thread)
3770 "Display GDB disassembly information."
3772 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3774 (def-gdb-preempt-display-buffer
3775 gdb-preemptively-display-disassembly-buffer
3776 'gdb-disassembly-buffer)
3778 (defun gdb-frame-disassembly-buffer (&optional thread)
3779 "Display GDB disassembly information in another frame."
3781 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3782 gdb-display-buffer-other-frame-action))
3784 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3785 (let* ((frame (gdb-current-buffer-frame))
3786 (file (bindat-get-field frame 'fullname))
3787 (line (bindat-get-field frame 'line)))
3789 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3790 ;; If we're unable to get a file name / line for $PC, simply
3791 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3792 ;; 150 bytes) instructions.
3793 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3794 gdb-disassembly-handler
3795 ;; We update disassembly only after we have actual frame information
3796 ;; about all threads, so no there's `update' signal in this list
3797 '(start update-disassembly))
3799 (def-gdb-auto-update-handler
3800 gdb-disassembly-handler
3801 gdb-disassembly-handler-custom
3804 (gdb-set-buffer-rules
3805 'gdb-disassembly-buffer
3806 'gdb-disassembly-buffer-name
3807 'gdb-disassembly-mode
3808 'gdb-invalidate-disassembly)
3810 (defvar gdb-disassembly-font-lock-keywords
3811 '(;; <__function.name+n>
3812 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3813 (1 font-lock-function-name-face))
3814 ;; 0xNNNNNNNN <__function.name+n>: opcode
3815 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3816 (4 font-lock-keyword-face))
3817 ;; %register(at least i386)
3818 ("%\\sw+" . font-lock-variable-name-face)
3819 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3820 (1 font-lock-comment-face)
3821 (2 font-lock-function-name-face))
3822 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3823 "Font lock keywords used in `gdb-disassembly-mode'.")
3825 (defvar gdb-disassembly-mode-map
3827 (let ((map (make-sparse-keymap)))
3828 (suppress-keymap map)
3829 (define-key map "q" 'kill-this-buffer)
3832 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3833 "Major mode for GDB disassembly information."
3834 ;; TODO Rename overlay variable for disassembly mode
3835 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3836 (setq fringes-outside-margins t)
3837 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3838 (set (make-local-variable 'font-lock-defaults)
3839 '(gdb-disassembly-font-lock-keywords))
3840 'gdb-invalidate-disassembly)
3842 (defun gdb-disassembly-handler-custom ()
3843 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3844 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3845 (table (make-gdb-table))
3847 (dolist (instr instructions)
3848 (gdb-table-add-row table
3850 (bindat-get-field instr 'address)
3852 ((func-name (bindat-get-field instr 'func-name))
3853 (offset (bindat-get-field instr 'offset)))
3855 (format "<%s+%s>:" func-name offset)
3857 (bindat-get-field instr 'inst)))
3858 (when (string-equal (bindat-get-field instr 'address)
3861 (setq marked-line (length (gdb-table-rows table)))
3862 (setq fringe-indicator-alist
3863 (if (string-equal gdb-frame-number "0")
3865 '((overlay-arrow . hollow-right-triangle)))))))
3866 (insert (gdb-table-string table " "))
3867 (gdb-disassembly-place-breakpoints)
3868 ;; Mark current position with overlay arrow and scroll window to
3871 (let ((window (get-buffer-window (current-buffer) 0)))
3872 (set-window-point window (gdb-mark-line marked-line
3873 gdb-disassembly-position))))
3875 (gdb-current-context-mode-name
3876 (concat "Disassembly: "
3877 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3879 (defun gdb-disassembly-place-breakpoints ()
3880 (gdb-remove-breakpoint-icons (point-min) (point-max))
3881 (dolist (breakpoint gdb-breakpoints-list)
3882 (let* ((breakpoint (cdr breakpoint))
3883 (bptno (bindat-get-field breakpoint 'number))
3884 (flag (bindat-get-field breakpoint 'enabled))
3885 (address (bindat-get-field breakpoint 'addr)))
3887 (goto-char (point-min))
3888 (if (re-search-forward (concat "^" address) nil t)
3889 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3892 (defvar gdb-breakpoints-header
3894 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3897 (gdb-propertize-header "Threads" gdb-threads-buffer
3898 "mouse-1: select" mode-line-highlight
3899 mode-line-inactive)))
3901 ;;; Breakpoints view
3902 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3903 "Major mode for gdb breakpoints."
3904 (setq header-line-format gdb-breakpoints-header)
3905 'gdb-invalidate-breakpoints)
3907 (defun gdb-toggle-breakpoint ()
3908 "Enable/disable breakpoint at current line of breakpoints buffer."
3912 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3915 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3918 (bindat-get-field breakpoint 'number)))
3919 (error "Not recognized as break/watchpoint line")))))
3921 (defun gdb-delete-breakpoint ()
3922 "Delete the breakpoint at current line of breakpoints buffer."
3926 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3928 (gud-basic-call (concat "-break-delete "
3929 (bindat-get-field breakpoint 'number)))
3930 (error "Not recognized as break/watchpoint line")))))
3932 (defun gdb-goto-breakpoint (&optional event)
3933 "Go to the location of breakpoint at current line of breakpoints buffer."
3934 (interactive (list last-input-event))
3935 (if event (posn-set-point (event-end event)))
3936 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3937 (let ((window (get-buffer-window gud-comint-buffer)))
3938 (if window (save-selected-window (select-window window))))
3941 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3943 (let ((bptno (bindat-get-field breakpoint 'number))
3944 (file (bindat-get-field breakpoint 'fullname))
3945 (line (bindat-get-field breakpoint 'line)))
3946 (save-selected-window
3947 (let* ((buffer (find-file-noselect
3948 (if (file-exists-p file) file
3949 (cdr (assoc bptno gdb-location-alist)))))
3950 (window (or (gdb-display-source-buffer buffer)
3951 (display-buffer buffer))))
3952 (setq gdb-source-window window)
3953 (with-current-buffer buffer
3954 (goto-char (point-min))
3955 (forward-line (1- (string-to-number line)))
3956 (set-window-point window (point))))))
3957 (error "Not recognized as break/watchpoint line")))))
3960 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3962 (def-gdb-trigger-and-handler
3963 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3964 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3967 (gdb-set-buffer-rules
3969 'gdb-stack-buffer-name
3971 'gdb-invalidate-frames)
3973 (defun gdb-frame-location (frame)
3974 "Return \" of file:line\" or \" of library\" for structure FRAME.
3976 FRAME must have either \"file\" and \"line\" members or \"from\"
3978 (let ((file (bindat-get-field frame 'file))
3979 (line (bindat-get-field frame 'line))
3980 (from (bindat-get-field frame 'from)))
3981 (let ((res (or (and file line (concat file ":" line))
3983 (if res (concat " of " res) ""))))
3985 (defun gdb-stack-list-frames-custom ()
3986 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3987 (table (make-gdb-table)))
3988 (set-marker gdb-stack-position nil)
3989 (dolist (frame stack)
3990 (gdb-table-add-row table
3992 (bindat-get-field frame 'level)
3995 (bindat-get-field frame 'func)
3996 (if gdb-stack-buffer-locations
3997 (gdb-frame-location frame) "")
3998 (if gdb-stack-buffer-addresses
3999 (concat " at " (bindat-get-field frame 'addr)) "")))
4000 `(mouse-face highlight
4001 help-echo "mouse-2, RET: Select frame"
4003 (insert (gdb-table-string table " ")))
4004 (when (and gdb-frame-number
4005 (gdb-buffer-shows-main-thread-p))
4006 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
4007 gdb-stack-position))
4009 (gdb-current-context-mode-name "Frames")))
4011 (defun gdb-stack-buffer-name ()
4012 (gdb-current-context-buffer-name
4013 (concat "stack frames of " (gdb-get-target-string))))
4015 (defun gdb-display-stack-buffer (&optional thread)
4016 "Display GDB backtrace for current stack."
4018 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
4020 (def-gdb-preempt-display-buffer
4021 gdb-preemptively-display-stack-buffer
4022 'gdb-stack-buffer nil t)
4024 (defun gdb-frame-stack-buffer (&optional thread)
4025 "Display GDB backtrace for current stack in another frame."
4027 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
4028 gdb-display-buffer-other-frame-action))
4030 (defvar gdb-frames-mode-map
4031 (let ((map (make-sparse-keymap)))
4032 (suppress-keymap map)
4033 (define-key map "q" 'kill-this-buffer)
4034 (define-key map "\r" 'gdb-select-frame)
4035 (define-key map [mouse-2] 'gdb-select-frame)
4036 (define-key map [follow-link] 'mouse-face)
4039 (defvar gdb-frames-font-lock-keywords
4040 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
4041 "Font lock keywords used in `gdb-frames-mode'.")
4043 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
4044 "Major mode for gdb call stack."
4045 (setq gdb-stack-position (make-marker))
4046 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
4047 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
4048 (set (make-local-variable 'font-lock-defaults)
4049 '(gdb-frames-font-lock-keywords))
4050 'gdb-invalidate-frames)
4052 (defun gdb-select-frame (&optional event)
4053 "Select the frame and display the relevant source."
4054 (interactive (list last-input-event))
4055 (if event (posn-set-point (event-end event)))
4056 (let ((frame (get-text-property (point) 'gdb-frame)))
4058 (if (gdb-buffer-shows-main-thread-p)
4059 (let ((new-level (bindat-get-field frame 'level)))
4060 (setq gdb-frame-number new-level)
4061 (gdb-input (concat "-stack-select-frame " new-level)
4064 (error "Could not select frame for non-current thread"))
4065 (error "Not recognized as frame line"))))
4069 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
4070 (def-gdb-trigger-and-handler
4071 gdb-invalidate-locals
4072 (concat (gdb-current-context-command "-stack-list-locals")
4074 gdb-locals-handler gdb-locals-handler-custom
4077 (gdb-set-buffer-rules
4079 'gdb-locals-buffer-name
4081 'gdb-invalidate-locals)
4083 (defvar gdb-locals-watch-map
4084 (let ((map (make-sparse-keymap)))
4085 (suppress-keymap map)
4086 (define-key map "\r" 'gud-watch)
4087 (define-key map [mouse-2] 'gud-watch)
4089 "Keymap to create watch expression of a complex data type local variable.")
4091 (defvar gdb-edit-locals-map-1
4092 (let ((map (make-sparse-keymap)))
4093 (suppress-keymap map)
4094 (define-key map "\r" 'gdb-edit-locals-value)
4095 (define-key map [mouse-2] 'gdb-edit-locals-value)
4097 "Keymap to edit value of a simple data type local variable.")
4099 (defun gdb-edit-locals-value (&optional event)
4100 "Assign a value to a variable displayed in the locals buffer."
4101 (interactive (list last-input-event))
4103 (if event (posn-set-point (event-end event)))
4105 (let* ((var (bindat-get-field
4106 (get-text-property (point) 'gdb-local-variable) 'name))
4107 (value (read-string (format "New value (%s): " var))))
4109 (concat "-gdb-set variable " var " = " value)))))
4111 ;; Don't display values of arrays or structures.
4112 ;; These can be expanded using gud-watch.
4113 (defun gdb-locals-handler-custom ()
4114 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4115 (table (make-gdb-table)))
4116 (dolist (local locals-list)
4117 (let ((name (bindat-get-field local 'name))
4118 (value (bindat-get-field local 'value))
4119 (type (bindat-get-field local 'type)))
4121 (setq value "<complex data type>"))
4123 (string-match "\\0x" value))
4124 (add-text-properties 0 (length name)
4125 `(mouse-face highlight
4126 help-echo "mouse-2: create watch expression"
4127 local-map ,gdb-locals-watch-map)
4129 (add-text-properties 0 (length value)
4130 `(mouse-face highlight
4131 help-echo "mouse-2: edit value"
4132 local-map ,gdb-edit-locals-map-1)
4137 (propertize type 'font-lock-face font-lock-type-face)
4138 (propertize name 'font-lock-face font-lock-variable-name-face)
4140 `(gdb-local-variable ,local))))
4141 (insert (gdb-table-string table " "))
4143 (gdb-current-context-mode-name
4145 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4147 (defvar gdb-locals-header
4149 (gdb-propertize-header "Locals" gdb-locals-buffer
4152 (gdb-propertize-header "Registers" gdb-registers-buffer
4153 "mouse-1: select" mode-line-highlight
4154 mode-line-inactive)))
4156 (defvar gdb-locals-mode-map
4157 (let ((map (make-sparse-keymap)))
4158 (suppress-keymap map)
4159 (define-key map "q" 'kill-this-buffer)
4160 (define-key map "\t" (lambda ()
4162 (gdb-set-window-buffer
4163 (gdb-get-buffer-create
4164 'gdb-registers-buffer
4165 gdb-thread-number) t)))
4168 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4169 "Major mode for gdb locals."
4170 (setq header-line-format gdb-locals-header)
4171 'gdb-invalidate-locals)
4173 (defun gdb-locals-buffer-name ()
4174 (gdb-current-context-buffer-name
4175 (concat "locals of " (gdb-get-target-string))))
4177 (defun gdb-display-locals-buffer (&optional thread)
4178 "Display the local variables of current GDB stack."
4180 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4182 (def-gdb-preempt-display-buffer
4183 gdb-preemptively-display-locals-buffer
4184 'gdb-locals-buffer nil t)
4186 (defun gdb-frame-locals-buffer (&optional thread)
4187 "Display the local variables of the current GDB stack in another frame."
4189 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4190 gdb-display-buffer-other-frame-action))
4193 ;; Registers buffer.
4195 (def-gdb-trigger-and-handler
4196 gdb-invalidate-registers
4197 (concat (gdb-current-context-command "-data-list-register-values") " x")
4198 gdb-registers-handler
4199 gdb-registers-handler-custom
4202 (gdb-set-buffer-rules
4203 'gdb-registers-buffer
4204 'gdb-registers-buffer-name
4206 'gdb-invalidate-registers)
4208 (defun gdb-registers-handler-custom ()
4209 (when gdb-register-names
4210 (let ((register-values
4211 (bindat-get-field (gdb-json-partial-output) 'register-values))
4212 (table (make-gdb-table)))
4213 (dolist (register register-values)
4214 (let* ((register-number (bindat-get-field register 'number))
4215 (value (bindat-get-field register 'value))
4216 (register-name (nth (string-to-number register-number)
4217 gdb-register-names)))
4221 (propertize register-name
4222 'font-lock-face font-lock-variable-name-face)
4223 (if (member register-number gdb-changed-registers)
4224 (propertize value 'font-lock-face font-lock-warning-face)
4226 `(mouse-face highlight
4227 help-echo "mouse-2: edit value"
4228 gdb-register-name ,register-name))))
4229 (insert (gdb-table-string table " ")))
4231 (gdb-current-context-mode-name "Registers"))))
4233 (defun gdb-edit-register-value (&optional event)
4234 "Assign a value to a register displayed in the registers buffer."
4235 (interactive (list last-input-event))
4237 (if event (posn-set-point (event-end event)))
4239 (let* ((var (bindat-get-field
4240 (get-text-property (point) 'gdb-register-name)))
4241 (value (read-string (format "New value (%s): " var))))
4243 (concat "-gdb-set variable $" var " = " value)))))
4245 (defvar gdb-registers-mode-map
4246 (let ((map (make-sparse-keymap)))
4247 (suppress-keymap map)
4248 (define-key map "\r" 'gdb-edit-register-value)
4249 (define-key map [mouse-2] 'gdb-edit-register-value)
4250 (define-key map "q" 'kill-this-buffer)
4251 (define-key map "\t" (lambda ()
4253 (gdb-set-window-buffer
4254 (gdb-get-buffer-create
4256 gdb-thread-number) t)))
4259 (defvar gdb-registers-header
4261 (gdb-propertize-header "Locals" gdb-locals-buffer
4262 "mouse-1: select" mode-line-highlight
4265 (gdb-propertize-header "Registers" gdb-registers-buffer
4266 nil nil mode-line)))
4268 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4269 "Major mode for gdb registers."
4270 (setq header-line-format gdb-registers-header)
4271 'gdb-invalidate-registers)
4273 (defun gdb-registers-buffer-name ()
4274 (gdb-current-context-buffer-name
4275 (concat "registers of " (gdb-get-target-string))))
4277 (defun gdb-display-registers-buffer (&optional thread)
4278 "Display GDB register contents."
4280 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4282 (def-gdb-preempt-display-buffer
4283 gdb-preemptively-display-registers-buffer
4284 'gdb-registers-buffer nil t)
4286 (defun gdb-frame-registers-buffer (&optional thread)
4287 "Display GDB register contents in another frame."
4289 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4290 gdb-display-buffer-other-frame-action))
4292 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4293 (defun gdb-get-changed-registers ()
4294 (when (gdb-get-buffer 'gdb-registers-buffer)
4295 (gdb-input "-data-list-changed-registers"
4296 'gdb-changed-registers-handler
4297 'gdb-get-changed-registers)))
4299 (defun gdb-changed-registers-handler ()
4300 (setq gdb-changed-registers nil)
4301 (dolist (register-number
4302 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4303 (push register-number gdb-changed-registers)))
4305 (defun gdb-register-names-handler ()
4306 ;; Don't use pending triggers because this handler is called
4307 ;; only once (in gdb-init-1)
4308 (setq gdb-register-names nil)
4309 (dolist (register-name
4310 (bindat-get-field (gdb-json-partial-output) 'register-names))
4311 (push register-name gdb-register-names))
4312 (setq gdb-register-names (reverse gdb-register-names)))
4315 (defun gdb-get-source-file-list ()
4316 "Create list of source files for current GDB session.
4317 If buffers already exist for any of these files, `gud-minor-mode'
4319 (goto-char (point-min))
4320 (while (re-search-forward gdb-source-file-regexp nil t)
4321 (push (read (match-string 1)) gdb-source-file-list))
4322 (dolist (buffer (buffer-list))
4323 (with-current-buffer buffer
4324 (when (member buffer-file-name gdb-source-file-list)
4325 (gdb-init-buffer)))))
4327 (defun gdb-get-main-selected-frame ()
4328 "Trigger for `gdb-frame-handler' which uses main current thread.
4329 Called from `gdb-update'."
4330 (gdb-input (gdb-current-context-command "-stack-info-frame")
4332 'gdb-get-main-selected-frame))
4334 (defun gdb-frame-handler ()
4335 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4336 overlay arrow in source buffer."
4337 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4339 (setq gdb-selected-frame (bindat-get-field frame 'func))
4340 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4341 (setq gdb-frame-number (bindat-get-field frame 'level))
4342 (setq gdb-frame-address (bindat-get-field frame 'addr))
4343 (let ((line (bindat-get-field frame 'line)))
4344 (setq gdb-selected-line (and line (string-to-number line)))
4345 (when (and gdb-selected-file gdb-selected-line)
4346 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4347 (gud-display-frame)))
4348 (if gud-overlay-arrow-position
4349 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4350 (position (marker-position gud-overlay-arrow-position)))
4352 (with-current-buffer buffer
4353 (setq fringe-indicator-alist
4354 (if (string-equal gdb-frame-number "0")
4356 '((overlay-arrow . hollow-right-triangle))))
4357 (setq gud-overlay-arrow-position (make-marker))
4358 (set-marker gud-overlay-arrow-position position))))))))
4360 (defconst gdb-prompt-name-regexp
4361 (concat "value=\\(" gdb--string-regexp "\\)"))
4363 (defun gdb-get-prompt ()
4364 "Find prompt for GDB session."
4365 (goto-char (point-min))
4366 (setq gdb-prompt-name nil)
4367 (re-search-forward gdb-prompt-name-regexp nil t)
4368 (setq gdb-prompt-name (read (match-string 1)))
4369 ;; Insert first prompt.
4370 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4372 ;;;; Window management
4373 (defun gdb-display-buffer (buf)
4374 "Show buffer BUF, and make that window dedicated."
4375 (let ((window (display-buffer buf)))
4376 (set-window-dedicated-p window t)
4379 ;; (let ((answer (get-buffer-window buf 0)))
4381 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4382 ;; (let ((window (get-lru-window)))
4383 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4385 ;; (let ((largest (get-largest-window)))
4386 ;; (setq answer (split-window largest))
4387 ;; (set-window-buffer answer buf)
4388 ;; (set-window-dedicated-p answer t)
4390 ;; (set-window-buffer window buf)
4394 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4395 "Find window displaying a buffer with the same
4396 `gdb-buffer-type' as BUF and show BUF there. If no such window
4397 exists, just call `gdb-display-buffer' for BUF. If the window
4398 found is already dedicated, split window according to
4399 SPLIT-HORIZONTAL and show BUF in the new window."
4401 (when (not (get-buffer-window buf))
4402 (let* ((buf-type (gdb-buffer-type buf))
4404 (get-window-with-predicate
4407 (gdb-buffer-type (window-buffer w)))
4408 (not (window-dedicated-p w)))))))
4410 (set-window-buffer existing-window buf)
4411 (let ((dedicated-window
4412 (get-window-with-predicate
4415 (gdb-buffer-type (window-buffer w)))))))
4416 (if dedicated-window
4418 (split-window dedicated-window nil split-horizontal) buf)
4419 (gdb-display-buffer buf))))))
4420 (error "Null buffer")))
4422 ;;; Shared keymap initialization:
4424 (let ((menu (make-sparse-keymap "GDB-Windows")))
4425 (define-key gud-menu-map [displays]
4426 `(menu-item "GDB-Windows" ,menu
4427 :visible (eq gud-minor-mode 'gdbmi)))
4428 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4429 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4430 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4431 (define-key menu [disassembly]
4432 '("Disassembly" . gdb-display-disassembly-buffer))
4433 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4434 (define-key menu [inferior]
4435 '("IO" . gdb-display-io-buffer))
4436 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4437 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4438 (define-key menu [breakpoints]
4439 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4441 (let ((menu (make-sparse-keymap "GDB-Frames")))
4442 (define-key gud-menu-map [frames]
4443 `(menu-item "GDB-Frames" ,menu
4444 :visible (eq gud-minor-mode 'gdbmi)))
4445 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4446 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4447 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4448 (define-key menu [disassembly]
4449 '("Disassembly" . gdb-frame-disassembly-buffer))
4450 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4451 (define-key menu [inferior]
4452 '("IO" . gdb-frame-io-buffer))
4453 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4454 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4455 (define-key menu [breakpoints]
4456 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4458 (let ((menu (make-sparse-keymap "GDB-MI")))
4459 (define-key menu [gdb-customize]
4460 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4461 :help "Customize Gdb Graphical Mode options."))
4462 (define-key menu [gdb-many-windows]
4463 '(menu-item "Display Other Windows" gdb-many-windows
4464 :help "Toggle display of locals, stack and breakpoint information"
4465 :button (:toggle . gdb-many-windows)))
4466 (define-key menu [gdb-restore-windows]
4467 '(menu-item "Restore Window Layout" gdb-restore-windows
4468 :help "Restore standard layout for debug session."))
4469 (define-key menu [sep1]
4471 (define-key menu [all-threads]
4472 '(menu-item "GUD controls all threads"
4475 (setq gdb-gud-control-all-threads t))
4476 :help "GUD start/stop commands apply to all threads"
4477 :button (:radio . gdb-gud-control-all-threads)))
4478 (define-key menu [current-thread]
4479 '(menu-item "GUD controls current thread"
4482 (setq gdb-gud-control-all-threads nil))
4483 :help "GUD start/stop commands apply to current thread only"
4484 :button (:radio . (not gdb-gud-control-all-threads))))
4485 (define-key menu [sep2]
4487 (define-key menu [gdb-customize-reasons]
4488 '(menu-item "Customize switching..."
4491 (customize-option 'gdb-switch-reasons))))
4492 (define-key menu [gdb-switch-when-another-stopped]
4493 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4494 gdb-switch-when-another-stopped
4495 "Automatically switch to stopped thread"
4496 "GDB thread switching %s"
4497 "Switch to stopped thread"))
4498 (define-key gud-menu-map [mi]
4499 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4501 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4502 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4503 ;; tool-bar-local-item-from-menu here because it appends new buttons
4504 ;; to toolbar from right to left while we want our A/T throttle to
4505 ;; show up right before Run button.
4506 (define-key-after gud-tool-bar-map [all-threads]
4507 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4508 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4509 :visible (and (eq gud-minor-mode 'gdbmi)
4511 (not gdb-gud-control-all-threads)))
4514 (define-key-after gud-tool-bar-map [current-thread]
4515 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4516 :image (find-image '((:type xpm :file "gud/all.xpm")))
4517 :visible (and (eq gud-minor-mode 'gdbmi)
4519 gdb-gud-control-all-threads))
4522 (defun gdb-frame-gdb-buffer ()
4523 "Display GUD buffer in another frame."
4525 (display-buffer-other-frame gud-comint-buffer))
4527 (defun gdb-display-gdb-buffer ()
4528 "Display GUD buffer."
4530 (pop-to-buffer gud-comint-buffer nil 0))
4532 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4533 "Set buffer of selected window to NAME and dedicate window.
4535 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4536 window is dedicated."
4537 (unless window (setq window (selected-window)))
4538 (when ignore-dedicated
4539 (set-window-dedicated-p window nil))
4540 (set-window-buffer window (get-buffer name))
4541 (set-window-dedicated-p window t))
4543 (defun gdb-setup-windows ()
4544 "Layout the window pattern for option `gdb-many-windows'."
4545 (gdb-get-buffer-create 'gdb-locals-buffer)
4546 (gdb-get-buffer-create 'gdb-stack-buffer)
4547 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4548 (set-window-dedicated-p (selected-window) nil)
4549 (switch-to-buffer gud-comint-buffer)
4550 (delete-other-windows)
4551 (let ((win0 (selected-window))
4552 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4553 (win2 (split-window nil ( / (window-height) 3)))
4554 (win3 (split-window-right)))
4555 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4556 (select-window win2)
4559 (if gud-last-last-frame
4560 (gud-find-file (car gud-last-last-frame))
4562 (gud-find-file gdb-main-file)
4563 ;; Put buffer list in window if we
4564 ;; can't find a source file.
4565 (list-buffers-noselect))))
4566 (setq gdb-source-window (selected-window))
4567 (let ((win4 (split-window-right)))
4568 (gdb-set-window-buffer
4569 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4570 (select-window win1)
4571 (gdb-set-window-buffer (gdb-stack-buffer-name))
4572 (let ((win5 (split-window-right)))
4573 (gdb-set-window-buffer (if gdb-show-threads-by-default
4574 (gdb-threads-buffer-name)
4575 (gdb-breakpoints-buffer-name))
4577 (select-window win0)))
4579 (define-minor-mode gdb-many-windows
4580 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4581 In this case it starts with two windows: one displaying the GUD
4582 buffer and the other with the source file with the main routine
4583 of the debugged program. Non-nil means display the layout shown for
4588 (if (and gud-comint-buffer
4589 (buffer-name gud-comint-buffer))
4591 (gdb-restore-windows))))
4593 (defun gdb-restore-windows ()
4594 "Restore the basic arrangement of windows used by gdb.
4595 This arrangement depends on the value of option `gdb-many-windows'."
4597 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4598 (delete-other-windows)
4599 (if gdb-many-windows
4601 (when (or gud-last-last-frame gdb-show-main)
4602 (let ((win (split-window)))
4605 (if gud-last-last-frame
4606 (gud-find-file (car gud-last-last-frame))
4607 (gud-find-file gdb-main-file)))
4608 (setq gdb-source-window win)))))
4610 ;; Called from `gud-sentinel' in gud.el:
4612 "Exit a debugging session cleanly.
4613 Kills the gdb buffers, and resets variables and the source buffers."
4614 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4615 ;; process. This pty must be deleted explicitly.
4616 (let ((pty (get-process "gdb-inferior")))
4617 (if pty (delete-process pty)))
4618 ;; Find gdb-mi buffers and kill them.
4619 (dolist (buffer (buffer-list))
4620 (unless (eq buffer gud-comint-buffer)
4621 (with-current-buffer buffer
4622 (if (eq gud-minor-mode 'gdbmi)
4623 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4625 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4626 (setq gud-minor-mode nil)
4627 (kill-local-variable 'tool-bar-map)
4628 (kill-local-variable 'gdb-define-alist))))))
4629 (setq gdb-disassembly-position nil)
4630 (setq overlay-arrow-variable-list
4631 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4632 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4633 (setq gdb-stack-position nil)
4634 (setq overlay-arrow-variable-list
4635 (delq 'gdb-stack-position overlay-arrow-variable-list))
4636 (setq gdb-thread-position nil)
4637 (setq overlay-arrow-variable-list
4638 (delq 'gdb-thread-position overlay-arrow-variable-list))
4639 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4640 (setq gud-running nil)
4641 (setq gdb-active-process nil)
4642 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4644 (defun gdb-get-source-file ()
4645 "Find the source file where the program starts and display it with related
4646 buffers, if required."
4647 (goto-char (point-min))
4648 (if (re-search-forward gdb-source-file-regexp nil t)
4649 (setq gdb-main-file (read (match-string 1))))
4650 (if gdb-many-windows
4652 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4655 (display-buffer (gud-find-file gdb-main-file))))
4656 (gdb-force-mode-line-update
4657 (propertize "ready" 'face font-lock-variable-name-face)))
4660 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4661 "Put string PUTSTRING in front of POS in the current buffer.
4662 PUTSTRING is displayed by putting an overlay into the current buffer with a
4663 `before-string' string that has a `display' property whose value is
4665 (let ((string (make-string 1 ?x))
4666 (buffer (current-buffer)))
4667 (setq putstring (copy-sequence putstring))
4668 (let ((overlay (make-overlay pos pos buffer))
4670 (list (list 'margin 'left-margin) putstring))))
4671 (put-text-property 0 1 'display prop string)
4673 (add-text-properties 0 1 sprops string))
4674 (overlay-put overlay 'put-break t)
4675 (overlay-put overlay 'before-string string))))
4677 ;;from remove-images
4678 (defun gdb-remove-strings (start end &optional buffer)
4679 "Remove strings between START and END in BUFFER.
4680 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4681 BUFFER nil or omitted means use the current buffer."
4683 (setq buffer (current-buffer)))
4684 (dolist (overlay (overlays-in start end))
4685 (when (overlay-get overlay 'put-break)
4686 (delete-overlay overlay))))
4688 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4689 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4690 (start (- (car posns) 1))
4691 (end (+ (cdr posns) 1))
4692 (putstring (if enabled "B" "b"))
4693 (source-window (get-buffer-window (current-buffer) 0)))
4694 (add-text-properties
4695 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4698 (add-text-properties
4699 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4700 (add-text-properties
4701 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4702 (gdb-remove-breakpoint-icons start end)
4703 (if (display-images-p)
4704 (if (>= (or left-fringe-width
4705 (if source-window (car (window-fringes source-window)))
4706 gdb-buffer-fringe-width) 8)
4709 `(left-fringe breakpoint
4712 'breakpoint-disabled))
4714 'gdb-enabled enabled)
4715 (when (< left-margin-width 2)
4716 (save-current-buffer
4717 (setq left-margin-width 2)
4721 left-margin-width right-margin-width))))
4724 (or breakpoint-enabled-icon
4725 (setq breakpoint-enabled-icon
4726 (find-image `((:type xpm :data
4727 ,breakpoint-xpm-data
4728 :ascent 100 :pointer hand)
4730 ,breakpoint-enabled-pbm-data
4731 :ascent 100 :pointer hand)))))
4732 (or breakpoint-disabled-icon
4733 (setq breakpoint-disabled-icon
4734 (find-image `((:type xpm :data
4735 ,breakpoint-xpm-data
4736 :conversion disabled
4737 :ascent 100 :pointer hand)
4739 ,breakpoint-disabled-pbm-data
4740 :ascent 100 :pointer hand))))))
4744 (when (< left-margin-width 2)
4745 (save-current-buffer
4746 (setq left-margin-width 2)
4747 (let ((window (get-buffer-window (current-buffer) 0)))
4750 window left-margin-width right-margin-width)))))
4752 (propertize putstring
4754 'breakpoint-enabled 'breakpoint-disabled))
4757 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4758 (gdb-remove-strings start end)
4759 (if (display-images-p)
4760 (remove-images start end))
4762 (setq left-margin-width 0)
4763 (let ((window (get-buffer-window (current-buffer) 0)))
4766 window left-margin-width right-margin-width)))))
4769 ;;; Functions for inline completion.
4771 (defvar gud-gdb-fetch-lines-in-progress)
4772 (defvar gud-gdb-fetch-lines-string)
4773 (defvar gud-gdb-fetch-lines-break)
4774 (defvar gud-gdb-fetched-lines)
4776 (defun gud-gdbmi-completions (context command)
4777 "Completion table for GDB/MI commands.
4778 COMMAND is the prefix for which we seek completion.
4779 CONTEXT is the text before COMMAND on the line."
4780 (let ((gud-gdb-fetch-lines-in-progress t)
4781 (gud-gdb-fetch-lines-string nil)
4782 (gud-gdb-fetch-lines-break (length context))
4783 (gud-gdb-fetched-lines nil)
4784 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4785 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4786 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4787 (gdb-input (concat "complete " context command)
4788 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4789 (while gud-gdb-fetch-lines-in-progress
4790 (accept-process-output (get-buffer-process gud-comint-buffer))))
4791 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4793 (defun gud-gdbmi-fetch-lines-filter (string)
4794 "Custom filter function for `gud-gdbmi-completions'."
4795 (setq string (concat gud-gdb-fetch-lines-string
4796 (gud-gdbmi-marker-filter string)))
4797 (while (string-match "\n" string)
4798 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4799 gud-gdb-fetched-lines)
4800 (setq string (substring string (match-end 0))))
4805 ;;; gdb-mi.el ends here