1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2015 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\\|python\\|"
1770 "while-stepping\\|stepping\\|ws\\|actions"
1771 "\\)\\([[:blank:]]+.*\\)?$")
1772 "Regexp matching GDB commands that enter a recursive reading loop.
1773 As long as GDB is in the recursive reading loop, it does not expect
1774 commands to be prefixed by \"-interpreter-exec console\".")
1776 (defun gdb-strip-string-backslash (string)
1777 (replace-regexp-in-string "\\\\$" "" string
))
1779 (defun gdb-send (proc string
)
1780 "A comint send filter for gdb."
1781 (with-current-buffer gud-comint-buffer
1782 (let ((inhibit-read-only t
))
1783 (remove-text-properties (point-min) (point-max) '(face))))
1784 ;; mimic <RET> key to repeat previous command in GDB
1785 (if (not (string= "" string
))
1786 (if gdb-continuation
1787 (setq gdb-last-command
(concat gdb-continuation
1788 (gdb-strip-string-backslash string
)
1790 (setq gdb-last-command
(gdb-strip-string-backslash string
)))
1791 (if gdb-last-command
(setq string gdb-last-command
))
1792 (setq gdb-continuation nil
))
1793 (if (and (not gdb-continuation
) (or (string-match "^-" string
)
1794 (> gdb-control-level
0)))
1795 ;; Either MI command or we are feeding GDB's recursive reading loop.
1797 (setq gdb-first-done-or-error t
)
1798 (process-send-string proc
(concat string
"\n"))
1799 (if (and (string-match "^end$" string
)
1800 (> gdb-control-level
0))
1801 (setq gdb-control-level
(1- gdb-control-level
))))
1803 (if (string-match "\\\\$" string
)
1804 (setq gdb-continuation
1805 (concat gdb-continuation
(gdb-strip-string-backslash
1808 (setq gdb-first-done-or-error t
)
1809 (let ((to-send (concat "-interpreter-exec console "
1810 (gdb-mi-quote (concat gdb-continuation string
" "))
1812 (if gdb-enable-debug
1813 (push (cons 'mi-send to-send
) gdb-debug-log
))
1814 (process-send-string proc to-send
))
1815 (if (and (string-match "^end$" string
)
1816 (> gdb-control-level
0))
1817 (setq gdb-control-level
(1- gdb-control-level
)))
1818 (setq gdb-continuation nil
)))
1819 (if (string-match gdb-control-commands-regexp string
)
1820 (setq gdb-control-level
(1+ gdb-control-level
))))
1822 (defun gdb-mi-quote (string)
1823 "Return STRING quoted properly as an MI argument.
1824 The string is enclosed in double quotes.
1825 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1826 (setq string
(replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string
))
1827 (setq string
(replace-regexp-in-string "\n" "\\n" string t t
))
1828 (concat "\"" string
"\""))
1830 (defun gdb-input (command handler-function
&optional trigger-name
)
1831 "Send COMMAND to GDB via the MI interface.
1832 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1833 complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
1834 Emacs is still waiting for a reply from another command previously
1835 sent with the same TRIGGER-NAME."
1836 (when (or (not trigger-name
)
1837 (not (gdb-pending-handler-p trigger-name
)))
1838 (setq gdb-token-number
(1+ gdb-token-number
))
1839 (setq command
(concat (number-to-string gdb-token-number
) command
))
1841 (if gdb-enable-debug
(push (list 'send-item command handler-function
)
1844 (gdb-add-handler gdb-token-number handler-function trigger-name
)
1846 (if gdbmi-debug-mode
(message "gdb-input: %s" command
))
1847 (process-send-string (get-buffer-process gud-comint-buffer
)
1848 (concat command
"\n"))))
1850 ;; NOFRAME is used for gud execution control commands
1851 (defun gdb-current-context-command (command)
1852 "Add --thread to gdb COMMAND when needed."
1853 (if (and gdb-thread-number
1854 gdb-supports-non-stop
)
1855 (concat command
" --thread " gdb-thread-number
)
1858 (defun gdb-current-context-buffer-name (name)
1859 "Add thread information and asterisks to string NAME.
1861 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1863 (if (local-variable-p 'gdb-thread-number
)
1864 (format " (bound to thread %s)" gdb-thread-number
)
1868 (defun gdb-current-context-mode-name (mode)
1869 "Add thread information to MODE which is to be used as `mode-name'."
1871 (if gdb-thread-number
1872 (format " [thread %s]" gdb-thread-number
)
1876 (defcustom gud-gdb-command-name
"gdb -i=mi"
1877 "Default command to execute an executable under the GDB debugger."
1882 (setq gud-running nil
)
1883 (setq gdb-output-sink
'user
)
1884 (gdb-remove-all-pending-triggers))
1886 (defun gdb-update (&optional no-proc
)
1887 "Update buffers showing status of debug session.
1888 If NO-PROC is non-nil, do not try to contact the GDB process."
1889 (when gdb-first-prompt
1890 (gdb-force-mode-line-update
1891 (propertize "initializing..." 'face font-lock-variable-name-face
))
1893 (setq gdb-first-prompt nil
))
1896 (gdb-get-main-selected-frame))
1898 ;; We may need to update gdb-threads-list so we can use
1899 (gdb-get-buffer-create 'gdb-threads-buffer
)
1900 ;; gdb-break-list is maintained in breakpoints handler
1901 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1904 (gdb-emit-signal gdb-buf-publisher
'update
))
1906 (gdb-get-changed-registers)
1907 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1908 (dolist (var gdb-var-list
)
1909 (setcar (nthcdr 5 var
) nil
))
1912 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1913 ;; because we may need to update current gud-running value without
1914 ;; changing current thread (see gdb-running)
1915 (defun gdb-setq-thread-number (number)
1916 "Set `gdb-thread-number' to NUMBER.
1917 Only this function must be used to change `gdb-thread-number'
1918 value to NUMBER, because `gud-running' and `gdb-frame-number'
1919 need to be updated appropriately when current thread changes."
1920 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1921 (unless (string-equal number
"0") (setq gdb-thread-number number
))
1922 (setq gdb-frame-number
"0")
1923 (gdb-update-gud-running))
1925 (defun gdb-update-gud-running ()
1926 "Set `gud-running' according to the state of current thread.
1928 `gdb-frame-number' is set to 0 if current thread is now stopped.
1930 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1931 cannot be reliably used to determine whether or not execution
1932 control buttons should be shown in menu or toolbar. Use
1933 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1936 For all-stop mode, thread information is unavailable while target
1938 (let ((old-value gud-running
))
1940 (string= (bindat-get-field (gdb-current-buffer-thread) 'state
)
1942 ;; Set frame number to "0" when _current_ threads stops.
1943 (when (and (gdb-current-buffer-thread)
1944 (not (eq gud-running old-value
)))
1945 (setq gdb-frame-number
"0"))))
1947 (defun gdb-show-run-p ()
1948 "Return t if \"Run/continue\" should be shown on the toolbar."
1949 (or (not gdb-active-process
)
1951 (not gdb-gud-control-all-threads
)
1954 (and gdb-gud-control-all-threads
1955 (> gdb-stopped-threads-count
0))))
1957 (defun gdb-show-stop-p ()
1958 "Return t if \"Stop\" should be shown on the toolbar."
1960 (not gdb-gud-control-all-threads
)
1963 (and gdb-gud-control-all-threads
1964 (> gdb-running-threads-count
0))))
1966 ;; GUD displays the selected GDB frame. This might might not be the current
1967 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1968 ;; visited breakpoint is, use that window.
1969 (defun gdb-display-source-buffer (buffer)
1970 (let* ((last-window (if gud-last-last-frame
1972 (gud-find-file (car gud-last-last-frame
)))))
1973 (source-window (or last-window
1974 (if (and gdb-source-window
1975 (window-live-p gdb-source-window
))
1976 gdb-source-window
))))
1978 (setq gdb-source-window source-window
)
1979 (set-window-buffer source-window buffer
))
1983 (defun gdbmi-start-with (str offset match
)
1984 "Return non-nil if string STR starts with MATCH, else returns nil.
1985 OFFSET is the position in STR at which the comparison takes place."
1986 (let ((match-length (length match
))
1987 (str-length (- (length str
) offset
)))
1988 (when (>= str-length match-length
)
1989 (string-equal match
(substring str offset
(+ offset match-length
))))))
1991 (defun gdbmi-same-start (str offset match
)
1992 "Return non-nil if STR and MATCH are equal up to the end of either strings.
1993 OFFSET is the position in STR at which the comparison takes place."
1994 (let* ((str-length (- (length str
) offset
))
1995 (match-length (length match
))
1996 (compare-length (min str-length match-length
)))
1997 (when (> compare-length
0)
1998 (string-equal (substring str offset
(+ offset compare-length
))
1999 (substring match
0 compare-length
)))))
2001 (defun gdbmi-is-number (character)
2002 "Return non-nil if CHARACTER is a numerical character between 0 and 9."
2003 (and (>= character ?
0)
2007 (defvar-local gdbmi-bnf-state
'gdbmi-bnf-output
2008 "Current GDB/MI output parser state.
2009 The parser is placed in a different state when an incomplete data steam is
2011 This variable will preserve the state required to resume the parsing
2012 when more data arrives.")
2014 (defvar-local gdbmi-bnf-offset
0
2015 "Offset in `gud-marker-acc' at which the parser is reading.
2016 This offset is used to be able to parse the GDB/MI message
2017 in-place, without the need of copying the string in a temporary buffer
2018 or discarding parsed tokens by substringing the message.")
2020 (defun gdbmi-bnf-init ()
2021 "Initialize the GDB/MI message parser."
2022 (setq gdbmi-bnf-state
'gdbmi-bnf-output
)
2023 (setq gdbmi-bnf-offset
0)
2024 (setq gud-marker-acc
""))
2027 (defun gdbmi-bnf-output ()
2028 "Implementation of the following GDB/MI output grammar rule:
2031 ( out-of-band-record )* [ result-record ] gdb-prompt"
2033 (gdbmi-bnf-skip-unrecognized)
2034 (while (gdbmi-bnf-out-of-band-record))
2035 (gdbmi-bnf-result-record)
2036 (gdbmi-bnf-gdb-prompt))
2039 (defun gdbmi-bnf-skip-unrecognized ()
2040 "Skip characters until is encounters the beginning of a valid record.
2041 Used as a protection mechanism in case something goes wrong when parsing
2042 a GDB/MI reply message."
2043 (let ((acc-length (length gud-marker-acc
))
2044 (prefix-offset gdbmi-bnf-offset
)
2045 (prompt "(gdb) \n"))
2047 (while (and (< prefix-offset acc-length
)
2048 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2049 (setq prefix-offset
(1+ prefix-offset
)))
2051 (if (and (< prefix-offset acc-length
)
2052 (not (memq (aref gud-marker-acc prefix-offset
)
2053 '(?^ ?
* ?
+ ?
= ?~ ?
@ ?
&)))
2054 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt
))
2055 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
2057 (let ((unrecognized-str (match-string 0 gud-marker-acc
)))
2058 (setq gdbmi-bnf-offset
(match-end 0))
2059 (if gdbmi-debug-mode
2060 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str
))
2061 (gdb-shell unrecognized-str
)
2065 (defun gdbmi-bnf-gdb-prompt ()
2066 "Implementation of the following GDB/MI output grammar rule:
2073 (let ((prompt "(gdb) \n"))
2074 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt
)
2075 (if gdbmi-debug-mode
(message "gdbmi-bnf-gdb-prompt: %s" prompt
))
2077 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length prompt
)))
2079 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
2080 ;; the end of a GDB reply message.
2084 (defun gdbmi-bnf-result-record ()
2085 "Implementation of the following GDB/MI output grammar rule:
2088 [ token ] `^' result-class ( `,' result )* nl
2091 any sequence of digits."
2093 (gdbmi-bnf-result-and-async-record-impl))
2096 (defun gdbmi-bnf-out-of-band-record ()
2097 "Implementation of the following GDB/MI output grammar rule:
2099 out-of-band-record ==>
2100 async-record | stream-record"
2102 (or (gdbmi-bnf-async-record)
2103 (gdbmi-bnf-stream-record)))
2106 (defun gdbmi-bnf-async-record ()
2107 "Implementation of the following GDB/MI output grammar rules:
2110 exec-async-output | status-async-output | notify-async-output
2112 exec-async-output ==>
2113 [ token ] `*' async-output
2115 status-async-output ==>
2116 [ token ] `+' async-output
2118 notify-async-output ==>
2119 [ token ] `=' async-output
2122 async-class ( `,' result )* nl"
2124 (gdbmi-bnf-result-and-async-record-impl))
2127 (defun gdbmi-bnf-stream-record ()
2128 "Implement the following GDB/MI output grammar rule:
2130 console-stream-output | target-stream-output | log-stream-output
2132 console-stream-output ==>
2135 target-stream-output ==>
2138 log-stream-output ==>
2140 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2141 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset
) '(?~ ?
@ ?
&))
2142 (string-match (concat "\\([~@&]\\)\\(" gdb--string-regexp
"\\)\n")
2145 (let ((prefix (match-string 1 gud-marker-acc
))
2146 (c-string (match-string 2 gud-marker-acc
)))
2148 (setq gdbmi-bnf-offset
(match-end 0))
2149 (if gdbmi-debug-mode
(message "gdbmi-bnf-stream-record: %s"
2150 (match-string 0 gud-marker-acc
)))
2152 (cond ((string-equal prefix
"~")
2153 (gdbmi-bnf-console-stream-output c-string
))
2154 ((string-equal prefix
"@")
2155 (gdbmi-bnf-target-stream-output c-string
))
2156 ((string-equal prefix
"&")
2157 (gdbmi-bnf-log-stream-output c-string
)))
2160 (defun gdbmi-bnf-console-stream-output (c-string)
2161 "Handler for the console-stream-output GDB/MI output grammar rule."
2162 (gdb-console c-string
))
2164 (defun gdbmi-bnf-target-stream-output (_c-string)
2165 "Handler for the target-stream-output GDB/MI output grammar rule."
2166 ;; Not currently used.
2169 (defun gdbmi-bnf-log-stream-output (c-string)
2170 "Handler for the log-stream-output GDB/MI output grammar rule."
2171 ;; Suppress "No registers." GDB 6.8 and earlier
2172 ;; duplicates MI error message on internal stream.
2173 ;; Don't print to GUD buffer.
2174 (if (not (string-equal (read c-string
) "No registers.\n"))
2175 (gdb-internals c-string
)))
2178 (defconst gdbmi-bnf-result-state-configs
2179 '(("^" .
(("done" .
(gdb-done . progressive
))
2180 ("error" .
(gdb-error . progressive
))
2181 ("running" .
(gdb-starting . atomic
))))
2182 ("*" .
(("stopped" .
(gdb-stopped . atomic
))
2183 ("running" .
(gdb-running . atomic
))))
2185 ("=" .
(("thread-created" .
(gdb-thread-created . atomic
))
2186 ("thread-selected" .
(gdb-thread-selected . atomic
))
2187 ("thread-existed" .
(gdb-ignored-notification . atomic
))
2188 ('default .
(gdb-ignored-notification . atomic
)))))
2189 "Alist of alists, mapping the type and class of message to a handler function.
2190 Handler functions are all flagged as either `progressive' or `atomic'.
2191 `progressive' handlers are capable of parsing incomplete messages.
2192 They can be called several time with new data chunk as they arrive from GDB.
2193 `progressive' handlers must have an extra argument that is set to a non-nil
2194 value when the message is complete.
2196 Implement the following GDB/MI output grammar rule:
2198 `done' | `running' | `connected' | `error' | `exit'
2201 `stopped' | others (where others will be added depending on the needs
2202 --this is still in development).")
2204 (defun gdbmi-bnf-result-and-async-record-impl ()
2205 "Common implementation of the result-record and async-record rule.
2206 Both rules share the same syntax. Those records may be very large in size.
2207 For that reason, the \"result\" part of the record is parsed by
2208 `gdbmi-bnf-incomplete-record-result', which will keep
2209 receiving characters as they arrive from GDB until the record is complete."
2210 (let ((acc-length (length gud-marker-acc
))
2211 (prefix-offset gdbmi-bnf-offset
))
2213 (while (and (< prefix-offset acc-length
)
2214 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2215 (setq prefix-offset
(1+ prefix-offset
)))
2217 (if (and (< prefix-offset acc-length
)
2218 (member (aref gud-marker-acc prefix-offset
) '(?
* ?
+ ?
= ?^
))
2219 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2220 gud-marker-acc gdbmi-bnf-offset
))
2222 (let ((token (match-string 1 gud-marker-acc
))
2223 (prefix (match-string 2 gud-marker-acc
))
2224 (class (match-string 3 gud-marker-acc
))
2225 (complete (string-equal (match-string 4 gud-marker-acc
) "\n"))
2229 (setq gdbmi-bnf-offset
(match-end 0))
2230 (if gdbmi-debug-mode
(message "gdbmi-bnf-result-record: %s"
2231 (match-string 0 gud-marker-acc
)))
2234 (cdr (assoc prefix gdbmi-bnf-result-state-configs
)))
2235 (setq class-command
(cdr (assoc class class-alist
)))
2236 (if (null class-command
)
2237 (setq class-command
(cdr (assoc 'default class-alist
))))
2241 (if (equal (cdr class-command
) 'progressive
)
2242 (funcall (car class-command
) token
"" complete
)
2243 (funcall (car class-command
) token
"")))
2244 (setq gdbmi-bnf-state
2246 (gdbmi-bnf-incomplete-record-result token class-command
)))
2247 (funcall gdbmi-bnf-state
))
2250 (defun gdbmi-bnf-incomplete-record-result (token class-command
)
2251 "State of the parser used to progressively parse a result-record or async-record
2252 rule from an incomplete data stream. The parser will stay in this state until
2253 the end of the current result or async record is reached."
2254 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2255 ;; Search the data stream for the end of the current record:
2256 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset
))
2257 (is-progressive (equal (cdr class-command
) 'progressive
))
2258 (is-complete (not (null newline-pos
)))
2261 (when gdbmi-debug-mode
2262 (message "gdbmi-bnf-incomplete-record-result: %s"
2263 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
)))
2265 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2266 ;; be processed by the class-command handler:
2267 (when (or is-complete is-progressive
)
2269 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
))
2271 ;; Move gdbmi-bnf-offset past the end of the chunk.
2272 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length result-str
)))
2274 (setq gdbmi-bnf-offset
(1+ gdbmi-bnf-offset
))))
2276 ;; Update the parsing state before invoking the handler in class-command
2277 ;; to make sure it's not left in an invalid state if the handler was
2278 ;; to generate an error.
2280 (setq gdbmi-bnf-state
'gdbmi-bnf-output
))
2284 (funcall (car class-command
) token result-str is-complete
)
2286 (funcall (car class-command
) token result-str
))))
2289 ;; Incomplete gdb response: abort parsing until we receive more data.
2290 (if gdbmi-debug-mode
(message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2291 (throw 'gdbmi-incomplete-stream nil
))
2296 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2297 ; The handling of those rules is currently done by the handlers registered
2298 ; in gdbmi-bnf-result-state-configs
2301 ; variable "=" value
2307 ; const | tuple | list
2313 ; "{}" | "{" result ( "," result )* "}"
2316 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2319 (defun gud-gdbmi-marker-filter (string)
2320 "Filter GDB/MI output."
2322 ;; Record transactions if logging is enabled.
2323 (when gdb-enable-debug
2324 (push (cons 'recv string
) gdb-debug-log
)
2325 (if (and gdb-debug-log-max
2326 (> (length gdb-debug-log
) gdb-debug-log-max
))
2327 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
2329 ;; Recall the left over gud-marker-acc from last time.
2330 (setq gud-marker-acc
(concat gud-marker-acc string
))
2332 ;; Start accumulating output for the GUD buffer.
2333 (setq gdb-filter-output
"")
2335 (let ((acc-length (length gud-marker-acc
)))
2336 (catch 'gdbmi-incomplete-stream
2337 (while (and (< gdbmi-bnf-offset acc-length
)
2338 (funcall gdbmi-bnf-state
)))))
2340 (when (/= gdbmi-bnf-offset
0)
2341 (setq gud-marker-acc
(substring gud-marker-acc gdbmi-bnf-offset
))
2342 (setq gdbmi-bnf-offset
0))
2344 (when (and gdbmi-debug-mode
(> (length gud-marker-acc
) 0))
2345 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc
))
2349 (defun gdb-gdb (_output-field))
2351 (defun gdb-shell (output-field)
2352 (setq gdb-filter-output
2353 (concat output-field gdb-filter-output
)))
2355 (defun gdb-ignored-notification (_token _output-field
))
2357 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2358 (defun gdb-thread-created (_token _output-field
))
2359 (defun gdb-thread-exited (_token output-field
)
2360 "Handle =thread-exited async record.
2361 Unset `gdb-thread-number' if current thread exited and update threads list."
2362 (let* ((thread-id (bindat-get-field (gdb-json-string output-field
) 'id
)))
2363 (if (string= gdb-thread-number thread-id
)
2364 (gdb-setq-thread-number nil
))
2365 ;; When we continue current thread and it quickly exits,
2366 ;; the pending triggers in gdb-handler-list left after gdb-running
2367 ;; disallow us to properly call -thread-info without --thread option.
2368 ;; Thus we need to use gdb-wait-for-pending.
2369 (gdb-wait-for-pending
2370 (gdb-emit-signal gdb-buf-publisher
'update-threads
))))
2372 (defun gdb-thread-selected (_token output-field
)
2373 "Handler for =thread-selected MI output record.
2375 Sets `gdb-thread-number' to new id."
2376 (let* ((result (gdb-json-string output-field
))
2377 (thread-id (bindat-get-field result
'id
)))
2378 (gdb-setq-thread-number thread-id
)
2379 ;; Typing `thread N' in GUD buffer makes GDB emit `^done' followed
2380 ;; by `=thread-selected' notification. `^done' causes `gdb-update'
2381 ;; as usually. Things happen too fast and second call (from
2382 ;; gdb-thread-selected handler) gets cut off by our beloved
2383 ;; pending triggers.
2384 ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
2385 ;; body will get executed when `gdb-handler-list' if free of
2386 ;; pending triggers.
2387 (gdb-wait-for-pending
2390 (defun gdb-running (_token output-field
)
2392 (bindat-get-field (gdb-json-string output-field
) 'thread-id
)))
2393 ;; We reset gdb-frame-number to nil if current thread has gone
2394 ;; running. This can't be done in gdb-thread-list-handler-custom
2395 ;; because we need correct gdb-frame-number by the time
2396 ;; -thread-info command is sent.
2397 (when (or (string-equal thread-id
"all")
2398 (string-equal thread-id gdb-thread-number
))
2399 (setq gdb-frame-number nil
)))
2400 (setq gdb-inferior-status
"running")
2401 (gdb-force-mode-line-update
2402 (propertize gdb-inferior-status
'face font-lock-type-face
))
2403 (when (not gdb-non-stop
)
2404 (setq gud-running t
))
2405 (setq gdb-active-process t
))
2407 (defun gdb-starting (_output-field _result
)
2408 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2409 (setq gdb-inferior-status
"running")
2410 (gdb-force-mode-line-update
2411 (propertize gdb-inferior-status
'face font-lock-type-face
))
2412 (setq gdb-active-process t
)
2413 (setq gud-running t
))
2415 ;; -break-insert -t didn't give a reason before gdb 6.9
2417 (defun gdb-stopped (_token output-field
)
2418 "Given the contents of *stopped MI async record, select new
2419 current thread and update GDB buffers."
2420 ;; Reason is available with target-async only
2421 (let* ((result (gdb-json-string output-field
))
2422 (reason (bindat-get-field result
'reason
))
2423 (thread-id (bindat-get-field result
'thread-id
)))
2425 ;; -data-list-register-names needs to be issued for any stopped
2427 (when (not gdb-register-names
)
2428 (gdb-input (concat "-data-list-register-names"
2429 (if gdb-supports-non-stop
2430 (concat " --thread " thread-id
)))
2431 'gdb-register-names-handler
))
2433 ;; Don't set gud-last-frame here as it's currently done in
2434 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2438 ;; ;; Extract the frame position from the marker.
2439 ;; gud-last-frame (cons file
2440 ;; (string-to-number
2441 ;; (match-string 6 gud-marker-acc)))))
2443 (setq gdb-inferior-status
(or reason
"unknown"))
2444 (gdb-force-mode-line-update
2445 (propertize gdb-inferior-status
'face font-lock-warning-face
))
2446 (if (string-equal reason
"exited-normally")
2447 (setq gdb-active-process nil
))
2449 ;; Select new current thread.
2451 ;; Don't switch if we have no reasons selected
2452 (when gdb-switch-reasons
2453 ;; Switch from another stopped thread only if we have
2454 ;; gdb-switch-when-another-stopped:
2455 (when (or gdb-switch-when-another-stopped
2456 (not (string= "stopped"
2457 (bindat-get-field (gdb-current-buffer-thread) 'state
))))
2458 ;; Switch if current reason has been selected or we have no
2460 (if (or (eq gdb-switch-reasons t
)
2461 (member reason gdb-switch-reasons
))
2462 (when (not (string-equal gdb-thread-number thread-id
))
2463 (message "Switched to thread %s" thread-id
)
2464 (gdb-setq-thread-number thread-id
))
2465 (message "Thread %s stopped" thread-id
))))
2467 ;; Print "(gdb)" to GUD console
2468 (when gdb-first-done-or-error
2469 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2471 ;; In non-stop, we update information as soon as another thread gets
2473 (when (or gdb-first-done-or-error
2475 ;; In all-stop this updates gud-running properly as well.
2477 (setq gdb-first-done-or-error nil
))
2478 (run-hook-with-args 'gdb-stopped-functions result
)))
2480 ;; Remove the trimmings from log stream containing debugging messages
2481 ;; being produced by GDB's internals, use warning face and send to GUD
2483 (defun gdb-internals (output-field)
2484 (setq gdb-filter-output
2487 (if (string= output-field
"\"\\n\"")
2489 (let ((error-message
2490 (read output-field
)))
2492 0 (length error-message
)
2493 'face font-lock-warning-face
2497 ;; Remove the trimmings from the console stream and send to GUD buffer
2498 ;; (frontend MI commands should not print to this stream)
2499 (defun gdb-console (output-field)
2500 (setq gdb-filter-output
2501 (gdb-concat-output gdb-filter-output
(read output-field
))))
2503 (defun gdb-done (token-number output-field is-complete
)
2504 (gdb-done-or-error token-number
'done output-field is-complete
))
2506 (defun gdb-error (token-number output-field is-complete
)
2507 (gdb-done-or-error token-number
'error output-field is-complete
))
2509 (defun gdb-done-or-error (token-number type output-field is-complete
)
2510 (if (string-equal token-number
"")
2511 ;; Output from command entered by user
2513 (setq gdb-output-sink
'user
)
2514 (setq token-number nil
)
2515 ;; MI error - send to minibuffer
2516 (when (eq type
'error
)
2517 ;; Skip "msg=" from `output-field'
2518 (message "%s" (read (substring output-field
4)))
2519 ;; Don't send to the console twice. (If it is a console error
2520 ;; it is also in the console stream.)
2521 (setq output-field nil
)))
2522 ;; Output from command from frontend.
2523 (setq gdb-output-sink
'emacs
))
2525 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2526 (let* ((proc (get-buffer-process gud-comint-buffer
))
2527 (no-proc (or (null proc
)
2528 (memq (process-status proc
) '(exit signal
)))))
2530 (when (and is-complete gdb-first-done-or-error
)
2531 (unless (or token-number gud-running no-proc
)
2532 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2533 (gdb-update no-proc
)
2534 (setq gdb-first-done-or-error nil
))
2536 (setq gdb-filter-output
2537 (gdb-concat-output gdb-filter-output output-field
))
2539 ;; We are done concatenating to the output sink. Restore it to user sink:
2540 (setq gdb-output-sink
'user
)
2542 (when (and token-number is-complete
)
2543 (with-current-buffer
2544 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2545 (gdb-handle-reply (string-to-number token-number
))))
2548 (gdb-clear-partial-output))))
2550 (defun gdb-concat-output (so-far new
)
2552 ((eq gdb-output-sink
'user
) (concat so-far new
))
2553 ((eq gdb-output-sink
'emacs
)
2554 (gdb-append-to-partial-output new
)
2557 (defun gdb-append-to-partial-output (string)
2558 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2559 (goto-char (point-max))
2562 (defun gdb-clear-partial-output ()
2563 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2566 (defun gdb-jsonify-buffer (&optional fix-key fix-list
)
2567 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2569 Field names are wrapped in double quotes and equal signs are
2570 replaced with semicolons.
2572 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2573 partial output. This is used to get rid of useless keys in lists
2574 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2575 -break-info are examples of MI commands which issue such
2578 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2579 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2580 -break-info output when it contains breakpoint script field
2581 incompatible with GDB/MI output syntax."
2583 (goto-char (point-min))
2586 (while (re-search-forward (concat "[\\[,]\\(" fix-key
"=\\)") nil t
)
2587 (replace-match "" nil nil nil
1))))
2590 ;; Find positions of braces which enclose broken list
2591 (while (re-search-forward (concat fix-list
"={\"") nil t
)
2592 (let ((p1 (goto-char (- (point) 2)))
2593 (p2 (progn (forward-sexp)
2595 ;; Replace braces with brackets
2603 (goto-char (point-min))
2605 (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
2606 gdb--string-regexp
"\\)")))
2607 (while (re-search-forward re nil t
)
2608 (replace-match "\"\\1\":\\2" nil nil
)))
2609 (goto-char (point-max))
2612 (defun gdb-json-read-buffer (&optional fix-key fix-list
)
2613 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2615 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2616 (gdb-jsonify-buffer fix-key fix-list
)
2618 (goto-char (point-min))
2619 (let ((json-array-type 'list
))
2622 (defun gdb-json-string (string &optional fix-key fix-list
)
2623 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2625 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2628 (gdb-json-read-buffer fix-key fix-list
)))
2630 (defun gdb-json-partial-output (&optional fix-key fix-list
)
2631 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2633 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2634 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2635 (gdb-json-read-buffer fix-key fix-list
)))
2637 (defun gdb-line-posns (line)
2638 "Return a pair of LINE beginning and end positions."
2639 (let ((offset (1+ (- line
(line-number-at-pos)))))
2641 (line-beginning-position offset
)
2642 (line-end-position offset
))))
2644 (defmacro gdb-mark-line
(line variable
)
2645 "Set VARIABLE marker to point at beginning of LINE.
2647 If current window has no fringes, inverse colors on LINE.
2649 Return position where LINE begins."
2651 (let* ((posns (gdb-line-posns ,line
))
2652 (start-posn (car posns
))
2653 (end-posn (cdr posns
)))
2654 (set-marker ,variable
(copy-marker start-posn
))
2655 (when (not (> (car (window-fringes)) 0))
2656 (put-text-property start-posn end-posn
2657 'font-lock-face
'(:inverse-video t
)))
2660 (defun gdb-pad-string (string padding
)
2661 (format (concat "%" (number-to-string padding
) "s") string
))
2663 ;; gdb-table struct is a way to programmatically construct simple
2664 ;; tables. It help to reliably align columns of data in GDB buffers
2666 (cl-defstruct gdb-table
2669 (row-properties nil
)
2672 (defun gdb-table-add-row (table row
&optional properties
)
2673 "Add ROW of string to TABLE and recalculate column sizes.
2675 When non-nil, PROPERTIES will be added to the whole row when
2676 calling `gdb-table-string'."
2677 (let ((rows (gdb-table-rows table
))
2678 (row-properties (gdb-table-row-properties table
))
2679 (column-sizes (gdb-table-column-sizes table
))
2680 (right-align (gdb-table-right-align table
)))
2681 (when (not column-sizes
)
2682 (setf (gdb-table-column-sizes table
)
2683 (make-list (length row
) 0)))
2684 (setf (gdb-table-rows table
)
2685 (append rows
(list row
)))
2686 (setf (gdb-table-row-properties table
)
2687 (append row-properties
(list properties
)))
2688 (setf (gdb-table-column-sizes table
)
2689 (cl-mapcar (lambda (x s
)
2691 (max (abs x
) (string-width (or s
"")))))
2692 (if right-align new-x
(- new-x
))))
2693 (gdb-table-column-sizes table
)
2695 ;; Avoid trailing whitespace at eol
2696 (if (not (gdb-table-right-align table
))
2697 (setcar (last (gdb-table-column-sizes table
)) 0))))
2699 (defun gdb-table-string (table &optional sep
)
2700 "Return TABLE as a string with columns separated with SEP."
2701 (let ((column-sizes (gdb-table-column-sizes table
)))
2705 (lambda (row properties
)
2707 (mapconcat 'identity
2708 (cl-mapcar (lambda (s x
) (gdb-pad-string s x
))
2712 (gdb-table-rows table
)
2713 (gdb-table-row-properties table
))
2716 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2717 (defun gdb-get-many-fields (struct &rest fields
)
2718 "Return a list of FIELDS values from STRUCT."
2720 (dolist (field fields
)
2721 (push (bindat-get-field struct field
) values
))
2724 (defmacro def-gdb-auto-update-trigger
(trigger-name gdb-command
2726 &optional signal-list
)
2727 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2728 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2729 buffer with `gdb-bind-function-to-buffer'.
2731 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2732 defined trigger is called with an argument from SIGNAL-LIST. It's
2733 not recommended to define triggers with empty SIGNAL-LIST.
2734 Normally triggers should respond at least to 'update signal.
2736 Normally the trigger defined by this command must be called from
2737 the buffer where HANDLER-NAME must work. This should be done so
2738 that buffer-local thread number may be used in GDB-COMMAND (by
2739 calling `gdb-current-context-command').
2740 `gdb-bind-function-to-buffer' is used to achieve this, see
2741 `gdb-get-buffer-create'.
2743 Triggers defined by this command are meant to be used as a
2744 trigger argument when describing buffer types with
2745 `gdb-set-buffer-rules'."
2746 `(defun ,trigger-name
(&optional signal
)
2748 (or (not ,signal-list
)
2749 (memq signal
,signal-list
))
2750 (gdb-input ,gdb-command
2751 (gdb-bind-function-to-buffer ',handler-name
(current-buffer))
2752 (cons (current-buffer) ',trigger-name
)))))
2754 ;; Used by disassembly buffer only, the rest use
2755 ;; def-gdb-trigger-and-handler
2756 (defmacro def-gdb-auto-update-handler
(handler-name custom-defun
2757 &optional nopreserve
)
2758 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2760 Handlers are normally called from the buffers they put output in.
2762 Erase current buffer and evaluate CUSTOM-DEFUN.
2763 Then call `gdb-update-buffer-name'.
2765 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2766 `(defun ,handler-name
()
2767 (let* ((inhibit-read-only t
)
2768 ,@(unless nopreserve
2769 '((window (get-buffer-window (current-buffer) 0))
2770 (start (window-start window
))
2771 (p (window-point window
)))))
2774 (gdb-update-buffer-name)
2775 ,@(when (not nopreserve
)
2776 '((set-window-start window start
)
2777 (set-window-point window p
))))))
2779 (defmacro def-gdb-trigger-and-handler
(trigger-name gdb-command
2780 handler-name custom-defun
2781 &optional signal-list
)
2782 "Define trigger and handler.
2784 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2785 See `def-gdb-auto-update-trigger'.
2787 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2788 See `def-gdb-auto-update-handler'."
2790 (def-gdb-auto-update-trigger ,trigger-name
2792 ,handler-name
,signal-list
)
2793 (def-gdb-auto-update-handler ,handler-name
2798 ;; Breakpoint buffer : This displays the output of `-break-list'.
2799 (def-gdb-trigger-and-handler
2800 gdb-invalidate-breakpoints
"-break-list"
2801 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2804 (gdb-set-buffer-rules
2805 'gdb-breakpoints-buffer
2806 'gdb-breakpoints-buffer-name
2807 'gdb-breakpoints-mode
2808 'gdb-invalidate-breakpoints
)
2810 (defun gdb-breakpoints-list-handler-custom ()
2811 (let ((breakpoints-list (bindat-get-field
2812 (gdb-json-partial-output "bkpt" "script")
2813 'BreakpointTable
'body
))
2814 (table (make-gdb-table)))
2815 (setq gdb-breakpoints-list nil
)
2816 (gdb-table-add-row table
'("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2817 (dolist (breakpoint breakpoints-list
)
2818 (add-to-list 'gdb-breakpoints-list
2819 (cons (bindat-get-field breakpoint
'number
)
2821 (let ((at (bindat-get-field breakpoint
'at
))
2822 (pending (bindat-get-field breakpoint
'pending
))
2823 (func (bindat-get-field breakpoint
'func
))
2824 (type (bindat-get-field breakpoint
'type
)))
2825 (gdb-table-add-row table
2827 (bindat-get-field breakpoint
'number
)
2829 (or (bindat-get-field breakpoint
'disp
) "")
2830 (let ((flag (bindat-get-field breakpoint
'enabled
)))
2831 (if (string-equal flag
"y")
2833 (propertize "y" 'font-lock-face
2834 font-lock-warning-face
))
2836 (propertize "n" 'font-lock-face
2837 font-lock-comment-face
))))
2838 (bindat-get-field breakpoint
'addr
)
2839 (or (bindat-get-field breakpoint
'times
) "")
2840 (if (and type
(string-match ".*watchpoint" type
))
2841 (bindat-get-field breakpoint
'what
)
2844 (propertize (or func
"unknown")
2845 'font-lock-face font-lock-function-name-face
)
2846 (gdb-frame-location breakpoint
)))))
2847 ;; Add clickable properties only for breakpoints with file:line
2849 (append (list 'gdb-breakpoint breakpoint
)
2850 (when func
'(help-echo "mouse-2, RET: visit breakpoint"
2851 mouse-face highlight
))))))
2852 (insert (gdb-table-string table
" "))
2853 (gdb-place-breakpoints)))
2855 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2856 (defun gdb-place-breakpoints ()
2857 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2858 (dolist (buffer (buffer-list))
2859 (with-current-buffer buffer
2860 (if (and (eq gud-minor-mode
'gdbmi
)
2861 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2862 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2863 (dolist (breakpoint gdb-breakpoints-list
)
2864 (let* ((breakpoint (cdr breakpoint
)) ; gdb-breakpoints-list is
2865 ; an associative list
2866 (line (bindat-get-field breakpoint
'line
)))
2868 (let ((file (bindat-get-field breakpoint
'fullname
))
2869 (flag (bindat-get-field breakpoint
'enabled
))
2870 (bptno (bindat-get-field breakpoint
'number
)))
2871 (unless (and file
(file-exists-p file
))
2872 (setq file
(cdr (assoc bptno gdb-location-alist
))))
2874 (string-equal file
"File not found"))
2875 ;; If the full filename is not recorded in the
2876 ;; breakpoint structure or in `gdb-location-alist', use
2877 ;; -file-list-exec-source-file to extract it.
2878 (when (setq file
(bindat-get-field breakpoint
'file
))
2879 (gdb-input (concat "list " file
":1") 'ignore
)
2880 (gdb-input "-file-list-exec-source-file"
2881 `(lambda () (gdb-get-location
2882 ,bptno
,line
,flag
))))
2883 (with-current-buffer (find-file-noselect file
'nowarn
)
2885 ;; Only want one breakpoint icon at each location.
2886 (gdb-put-breakpoint-icon (string-equal flag
"y") bptno
2887 (string-to-number line
)))))))))
2889 (defconst gdb-source-file-regexp
2890 (concat "fullname=\\(" gdb--string-regexp
"\\)"))
2892 (defun gdb-get-location (bptno line flag
)
2893 "Find the directory containing the relevant source file.
2894 Put in buffer and place breakpoint icon."
2895 (goto-char (point-min))
2896 (catch 'file-not-found
2897 (if (re-search-forward gdb-source-file-regexp nil t
)
2898 (delete (cons bptno
"File not found") gdb-location-alist
)
2899 ;; FIXME: Why/how do we use (match-string 1) when the search failed?
2900 (push (cons bptno
(match-string 1)) gdb-location-alist
)
2902 (unless (assoc bptno gdb-location-alist
)
2903 (push (cons bptno
"File not found") gdb-location-alist
)
2904 (message-box "Cannot find source file for breakpoint location.
2905 Add directory to search path for source files using the GDB command, dir."))
2906 (throw 'file-not-found nil
))
2907 (with-current-buffer (find-file-noselect (match-string 1))
2909 ;; only want one breakpoint icon at each location
2910 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
(string-to-number line
)))))
2912 (add-hook 'find-file-hook
'gdb-find-file-hook
)
2914 (defun gdb-find-file-hook ()
2915 "Set up buffer for debugging if file is part of the source code
2916 of the current session."
2917 (if (and (buffer-name gud-comint-buffer
)
2918 ;; in case gud or gdb-ui is just loaded
2920 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
2922 (if (member buffer-file-name gdb-source-file-list
)
2923 (with-current-buffer (find-buffer-visiting buffer-file-name
)
2924 (gdb-init-buffer)))))
2926 (declare-function gud-remove
"gdb-mi" t t
) ; gud-def
2927 (declare-function gud-break
"gdb-mi" t t
) ; gud-def
2928 (declare-function fringe-bitmaps-at-pos
"fringe.c" (&optional pos window
))
2930 (defun gdb-mouse-set-clear-breakpoint (event)
2931 "Set/clear breakpoint in left fringe/margin at mouse click.
2932 If not in a source or disassembly buffer just set point."
2934 (mouse-minibuffer-check event
)
2935 (let ((posn (event-end event
)))
2936 (with-selected-window (posn-window posn
)
2937 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode
))
2938 (if (numberp (posn-point posn
))
2940 (goto-char (posn-point posn
))
2941 (if (or (posn-object posn
)
2942 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
2946 (posn-set-point posn
))))
2948 (defun gdb-mouse-toggle-breakpoint-margin (event)
2949 "Enable/disable breakpoint in left margin with mouse click."
2951 (mouse-minibuffer-check event
)
2952 (let ((posn (event-end event
)))
2953 (if (numberp (posn-point posn
))
2954 (with-selected-window (posn-window posn
)
2956 (goto-char (posn-point posn
))
2957 (if (posn-object posn
)
2959 (let ((bptno (get-text-property
2960 0 'gdb-bptno
(car (posn-string posn
)))))
2962 (if (get-text-property
2963 0 'gdb-enabled
(car (posn-string posn
)))
2968 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2969 "Enable/disable breakpoint in left fringe with mouse click."
2971 (mouse-minibuffer-check event
)
2972 (let* ((posn (event-end event
))
2973 (pos (posn-point posn
))
2976 (with-selected-window (posn-window posn
)
2977 (with-current-buffer (window-buffer)
2979 (dolist (overlay (overlays-in pos pos
))
2980 (when (overlay-get overlay
'put-break
)
2981 (setq obj
(overlay-get overlay
'before-string
))))
2985 (if (get-text-property 0 'gdb-enabled obj
)
2988 (get-text-property 0 'gdb-bptno obj
)))))))))
2990 (defun gdb-breakpoints-buffer-name ()
2991 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2993 (defun gdb-display-breakpoints-buffer (&optional thread
)
2994 "Display GDB breakpoints."
2996 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)))
2998 (defun gdb-frame-breakpoints-buffer (&optional thread
)
2999 "Display GDB breakpoints in another frame."
3001 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)
3002 gdb-display-buffer-other-frame-action
))
3004 (defvar gdb-breakpoints-mode-map
3005 (let ((map (make-sparse-keymap))
3006 (menu (make-sparse-keymap "Breakpoints")))
3007 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
3008 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
3009 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
3010 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
3011 (suppress-keymap map)
3012 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
3013 (define-key map " " 'gdb-toggle-breakpoint)
3014 (define-key map "D" 'gdb-delete-breakpoint)
3015 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
3016 (define-key map "q" 'gdb-delete-frame-or-window)
3017 (define-key map "\r" 'gdb-goto-breakpoint)
3018 (define-key map "\t" (lambda ()
3020 (gdb-set-window-buffer
3021 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
3022 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3023 (define-key map [follow-link] 'mouse-face)
3026 (defun gdb-delete-frame-or-window ()
3027 "Delete frame if there is only one window. Otherwise delete the window."
3029 (if (one-window-p) (delete-frame)
3032 ;;from make-mode-line-mouse-map
3033 (defun gdb-make-header-line-mouse-map (mouse function) "\
3034 Return a keymap with single entry for mouse key MOUSE on the header line.
3035 MOUSE is defined to run function FUNCTION with no args in the buffer
3036 corresponding to the mode line clicked."
3037 (let ((map (make-sparse-keymap)))
3038 (define-key map (vector 'header-line mouse) function)
3039 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3042 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3044 'help-echo ,help-echo
3045 'mouse-face ',mouse-face
3048 (gdb-make-header-line-mouse-map
3050 (lambda (event) (interactive "e")
3051 (save-selected-window
3052 (select-window (posn-window (event-start event)))
3053 (gdb-set-window-buffer
3054 (gdb-get-buffer-create ',buffer) t) )))))
3057 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3060 (defun gdb-threads-buffer-name ()
3061 (concat "*threads of " (gdb-get-target-string) "*"))
3063 (defun gdb-display-threads-buffer (&optional thread)
3064 "Display GDB threads."
3066 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3068 (defun gdb-frame-threads-buffer (&optional thread)
3069 "Display GDB threads in another frame."
3071 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3072 gdb-display-buffer-other-frame-action))
3074 (def-gdb-trigger-and-handler
3075 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3076 gdb-thread-list-handler gdb-thread-list-handler-custom
3077 '(start update update-threads))
3079 (gdb-set-buffer-rules
3081 'gdb-threads-buffer-name
3083 'gdb-invalidate-threads)
3085 (defvar gdb-threads-font-lock-keywords
3086 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3087 (" \\(stopped\\)" (1 font-lock-warning-face))
3088 (" \\(running\\)" (1 font-lock-string-face))
3089 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3090 "Font lock keywords used in `gdb-threads-mode'.")
3092 (defvar gdb-threads-mode-map
3093 (let ((map (make-sparse-keymap)))
3094 (define-key map "\r" 'gdb-select-thread)
3095 (define-key map "f" 'gdb-display-stack-for-thread)
3096 (define-key map "F" 'gdb-frame-stack-for-thread)
3097 (define-key map "l" 'gdb-display-locals-for-thread)
3098 (define-key map "L" 'gdb-frame-locals-for-thread)
3099 (define-key map "r" 'gdb-display-registers-for-thread)
3100 (define-key map "R" 'gdb-frame-registers-for-thread)
3101 (define-key map "d" 'gdb-display-disassembly-for-thread)
3102 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3103 (define-key map "i" 'gdb-interrupt-thread)
3104 (define-key map "c" 'gdb-continue-thread)
3105 (define-key map "s" 'gdb-step-thread)
3106 (define-key map "\t"
3109 (gdb-set-window-buffer
3110 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3111 (define-key map [mouse-2] 'gdb-select-thread)
3112 (define-key map [follow-link] 'mouse-face)
3115 (defvar gdb-threads-header
3117 (gdb-propertize-header
3118 "Breakpoints" gdb-breakpoints-buffer
3119 "mouse-1: select" mode-line-highlight mode-line-inactive)
3121 (gdb-propertize-header "Threads" gdb-threads-buffer
3122 nil nil mode-line)))
3124 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3125 "Major mode for GDB threads."
3126 (setq gdb-thread-position (make-marker))
3127 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3128 (setq header-line-format gdb-threads-header)
3129 (set (make-local-variable 'font-lock-defaults)
3130 '(gdb-threads-font-lock-keywords))
3131 'gdb-invalidate-threads)
3133 (defun gdb-thread-list-handler-custom ()
3134 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3135 (table (make-gdb-table))
3137 (setq gdb-threads-list nil)
3138 (setq gdb-running-threads-count 0)
3139 (setq gdb-stopped-threads-count 0)
3140 (set-marker gdb-thread-position nil)
3142 (dolist (thread (reverse threads-list))
3143 (let ((running (equal (bindat-get-field thread 'state) "running")))
3144 (add-to-list 'gdb-threads-list
3145 (cons (bindat-get-field thread 'id)
3147 (cl-incf (if running
3148 gdb-running-threads-count
3149 gdb-stopped-threads-count))
3154 (bindat-get-field thread 'id)
3156 (if gdb-thread-buffer-verbose-names
3157 (concat (bindat-get-field thread 'target-id) " ") "")
3158 (bindat-get-field thread 'state)
3159 ;; Include frame information for stopped threads
3162 " in " (bindat-get-field thread 'frame 'func)
3163 (if gdb-thread-buffer-arguments
3166 (let ((args (bindat-get-field thread 'frame 'args)))
3169 (apply #'format "%s=%s"
3170 (gdb-get-many-fields arg 'name 'value)))
3174 (if gdb-thread-buffer-locations
3175 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3176 (if gdb-thread-buffer-addresses
3177 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3181 'mouse-face 'highlight
3182 'help-echo "mouse-2, RET: select thread")))
3183 (when (string-equal gdb-thread-number
3184 (bindat-get-field thread 'id))
3185 (setq marked-line (length gdb-threads-list))))
3186 (insert (gdb-table-string table " "))
3188 (gdb-mark-line marked-line gdb-thread-position)))
3189 ;; We update gud-running here because we need to make sure that
3190 ;; gdb-threads-list is up-to-date
3191 (gdb-update-gud-running)
3192 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3194 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3195 "Define a NAME command which will act upon thread on the current line.
3197 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3198 be the value of 'gdb-thread property of the current line.
3199 If `gdb-thread' is nil, error is signaled."
3200 `(defun ,name (&optional event)
3202 (interactive (list last-input-event))
3203 (if event (posn-set-point (event-end event)))
3206 (let ((thread (get-text-property (point) 'gdb-thread)))
3209 (error "Not recognized as thread line"))))))
3211 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3213 "Define a NAME which will call BUFFER-COMMAND with id of thread
3214 on the current line."
3215 `(def-gdb-thread-buffer-command ,name
3216 (,buffer-command (bindat-get-field thread 'id))
3219 (def-gdb-thread-buffer-command gdb-select-thread
3220 (let ((new-id (bindat-get-field thread 'id)))
3221 (gdb-setq-thread-number new-id)
3222 (gdb-input (concat "-thread-select " new-id) 'ignore)
3224 "Select the thread at current line of threads buffer.")
3226 (def-gdb-thread-buffer-simple-command
3227 gdb-display-stack-for-thread
3228 gdb-preemptively-display-stack-buffer
3229 "Display stack buffer for the thread at current line.")
3231 (def-gdb-thread-buffer-simple-command
3232 gdb-display-locals-for-thread
3233 gdb-preemptively-display-locals-buffer
3234 "Display locals buffer for the thread at current line.")
3236 (def-gdb-thread-buffer-simple-command
3237 gdb-display-registers-for-thread
3238 gdb-preemptively-display-registers-buffer
3239 "Display registers buffer for the thread at current line.")
3241 (def-gdb-thread-buffer-simple-command
3242 gdb-display-disassembly-for-thread
3243 gdb-preemptively-display-disassembly-buffer
3244 "Display disassembly buffer for the thread at current line.")
3246 (def-gdb-thread-buffer-simple-command
3247 gdb-frame-stack-for-thread
3248 gdb-frame-stack-buffer
3249 "Display another frame with stack buffer for thread at current line.")
3251 (def-gdb-thread-buffer-simple-command
3252 gdb-frame-locals-for-thread
3253 gdb-frame-locals-buffer
3254 "Display another frame with locals buffer for thread at current line.")
3256 (def-gdb-thread-buffer-simple-command
3257 gdb-frame-registers-for-thread
3258 gdb-frame-registers-buffer
3259 "Display another frame with registers buffer for the thread at current line.")
3261 (def-gdb-thread-buffer-simple-command
3262 gdb-frame-disassembly-for-thread
3263 gdb-frame-disassembly-buffer
3264 "Display another frame with disassembly buffer for the thread at current line.")
3266 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3267 "Define a NAME which will execute GUD-COMMAND with
3268 `gdb-thread-number' locally bound to id of thread on the current
3270 `(def-gdb-thread-buffer-command ,name
3272 (let ((gdb-thread-number (bindat-get-field thread 'id))
3273 (gdb-gud-control-all-threads nil))
3274 (call-interactively #',gud-command))
3275 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3278 (def-gdb-thread-buffer-gud-command
3279 gdb-interrupt-thread
3281 "Interrupt thread at current line.")
3283 ;; Defined opaquely in M-x gdb via gud-def.
3284 (declare-function gud-cont "gdb-mi" (arg) t)
3286 (def-gdb-thread-buffer-gud-command
3289 "Continue thread at current line.")
3291 (declare-function gud-step "gdb-mi" (arg) t)
3293 (def-gdb-thread-buffer-gud-command
3296 "Step thread at current line.")
3301 (defcustom gdb-memory-rows 8
3302 "Number of data rows in memory window."
3307 (defcustom gdb-memory-columns 4
3308 "Number of data columns in memory window."
3313 (defcustom gdb-memory-format "x"
3314 "Display format of data items in memory window."
3315 :type '(choice (const :tag "Hexadecimal" "x")
3316 (const :tag "Signed decimal" "d")
3317 (const :tag "Unsigned decimal" "u")
3318 (const :tag "Octal" "o")
3319 (const :tag "Binary" "t"))
3323 (defcustom gdb-memory-unit 4
3324 "Unit size of data items in memory window."
3325 :type '(choice (const :tag "Byte" 1)
3326 (const :tag "Halfword" 2)
3327 (const :tag "Word" 4)
3328 (const :tag "Giant word" 8))
3332 (def-gdb-trigger-and-handler
3333 gdb-invalidate-memory
3334 (format "-data-read-memory %s %s %d %d %d"
3340 gdb-read-memory-handler
3341 gdb-read-memory-custom
3344 (gdb-set-buffer-rules
3346 'gdb-memory-buffer-name
3348 'gdb-invalidate-memory)
3350 (defun gdb-memory-column-width (size format)
3351 "Return length of string with memory unit of SIZE in FORMAT.
3353 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3354 in `gdb-memory-format'."
3355 (let ((format-base (cdr (assoc format
3357 ("d" . 10) ("u" . 10)
3361 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3362 (cond ((string-equal format "x")
3363 (+ 2 res)) ; hexadecimal numbers have 0x in front
3364 ((or (string-equal format "d")
3365 (string-equal format "o"))
3368 (error "Unknown format"))))
3370 (defun gdb-read-memory-custom ()
3371 (let* ((res (gdb-json-partial-output))
3372 (err-msg (bindat-get-field res 'msg)))
3374 (let ((memory (bindat-get-field res 'memory)))
3375 (setq gdb-memory-address (bindat-get-field res 'addr))
3376 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3377 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3378 (setq gdb-memory-last-address gdb-memory-address)
3379 (dolist (row memory)
3380 (insert (concat (bindat-get-field row 'addr) ":"))
3381 (dolist (column (bindat-get-field row 'data))
3382 (insert (gdb-pad-string column
3383 (+ 2 (gdb-memory-column-width
3385 gdb-memory-format)))))
3387 ;; Show last page instead of empty buffer when out of bounds
3389 (let ((gdb-memory-address gdb-memory-last-address))
3390 (gdb-invalidate-memory 'update)
3391 (error err-msg))))))
3393 (defvar gdb-memory-mode-map
3394 (let ((map (make-sparse-keymap)))
3395 (suppress-keymap map t)
3396 (define-key map "q" 'kill-this-buffer)
3397 (define-key map "n" 'gdb-memory-show-next-page)
3398 (define-key map "p" 'gdb-memory-show-previous-page)
3399 (define-key map "a" 'gdb-memory-set-address)
3400 (define-key map "t" 'gdb-memory-format-binary)
3401 (define-key map "o" 'gdb-memory-format-octal)
3402 (define-key map "u" 'gdb-memory-format-unsigned)
3403 (define-key map "d" 'gdb-memory-format-signed)
3404 (define-key map "x" 'gdb-memory-format-hexadecimal)
3405 (define-key map "b" 'gdb-memory-unit-byte)
3406 (define-key map "h" 'gdb-memory-unit-halfword)
3407 (define-key map "w" 'gdb-memory-unit-word)
3408 (define-key map "g" 'gdb-memory-unit-giant)
3409 (define-key map "R" 'gdb-memory-set-rows)
3410 (define-key map "C" 'gdb-memory-set-columns)
3413 (defun gdb-memory-set-address-event (event)
3414 "Handle a click on address field in memory buffer header."
3416 (save-selected-window
3417 (select-window (posn-window (event-start event)))
3418 (gdb-memory-set-address)))
3420 ;; Non-event version for use within keymap
3421 (defun gdb-memory-set-address ()
3422 "Set the start memory address."
3424 (let ((arg (read-from-minibuffer "Memory address: ")))
3425 (setq gdb-memory-address arg))
3426 (gdb-invalidate-memory 'update))
3428 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3429 "Define a function NAME which reads new VAR value from minibuffer."
3430 `(defun ,name (event)
3433 (save-selected-window
3434 (select-window (posn-window (event-start event)))
3435 (let* ((arg (read-from-minibuffer ,echo-string))
3436 (count (string-to-number arg)))
3438 (error "Positive number only")
3439 (customize-set-variable ',variable count)
3440 (gdb-invalidate-memory 'update))))))
3442 (def-gdb-set-positive-number
3446 "Set the number of data rows in memory window.")
3448 (def-gdb-set-positive-number
3449 gdb-memory-set-columns
3452 "Set the number of data columns in memory window.")
3454 (defmacro def-gdb-memory-format (name format doc)
3455 "Define a function NAME to switch memory buffer to use FORMAT.
3457 DOC is an optional documentation string."
3458 `(defun ,name () ,(when doc doc)
3460 (customize-set-variable 'gdb-memory-format ,format)
3461 (gdb-invalidate-memory 'update)))
3463 (def-gdb-memory-format
3464 gdb-memory-format-binary "t"
3465 "Set the display format to binary.")
3467 (def-gdb-memory-format
3468 gdb-memory-format-octal "o"
3469 "Set the display format to octal.")
3471 (def-gdb-memory-format
3472 gdb-memory-format-unsigned "u"
3473 "Set the display format to unsigned decimal.")
3475 (def-gdb-memory-format
3476 gdb-memory-format-signed "d"
3477 "Set the display format to decimal.")
3479 (def-gdb-memory-format
3480 gdb-memory-format-hexadecimal "x"
3481 "Set the display format to hexadecimal.")
3483 (defvar gdb-memory-format-map
3484 (let ((map (make-sparse-keymap)))
3485 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3487 "Keymap to select format in the header line.")
3489 (defvar gdb-memory-format-menu
3490 (let ((map (make-sparse-keymap "Format")))
3492 (define-key map [binary]
3493 '(menu-item "Binary" gdb-memory-format-binary
3494 :button (:radio . (equal gdb-memory-format "t"))))
3495 (define-key map [octal]
3496 '(menu-item "Octal" gdb-memory-format-octal
3497 :button (:radio . (equal gdb-memory-format "o"))))
3498 (define-key map [unsigned]
3499 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3500 :button (:radio . (equal gdb-memory-format "u"))))
3501 (define-key map [signed]
3502 '(menu-item "Signed Decimal" gdb-memory-format-signed
3503 :button (:radio . (equal gdb-memory-format "d"))))
3504 (define-key map [hexadecimal]
3505 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3506 :button (:radio . (equal gdb-memory-format "x"))))
3508 "Menu of display formats in the header line.")
3510 (defun gdb-memory-format-menu (event)
3512 (x-popup-menu event gdb-memory-format-menu))
3514 (defun gdb-memory-format-menu-1 (event)
3516 (save-selected-window
3517 (select-window (posn-window (event-start event)))
3518 (let* ((selection (gdb-memory-format-menu event))
3519 (binding (and selection (lookup-key gdb-memory-format-menu
3520 (vector (car selection))))))
3521 (if binding (call-interactively binding)))))
3523 (defmacro def-gdb-memory-unit (name unit-size doc)
3524 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3526 DOC is an optional documentation string."
3527 `(defun ,name () ,(when doc doc)
3529 (customize-set-variable 'gdb-memory-unit ,unit-size)
3530 (gdb-invalidate-memory 'update)))
3532 (def-gdb-memory-unit gdb-memory-unit-giant 8
3533 "Set the unit size to giant words (eight bytes).")
3535 (def-gdb-memory-unit gdb-memory-unit-word 4
3536 "Set the unit size to words (four bytes).")
3538 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3539 "Set the unit size to halfwords (two bytes).")
3541 (def-gdb-memory-unit gdb-memory-unit-byte 1
3542 "Set the unit size to bytes.")
3544 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3545 "Define a function NAME which show new address in memory buffer.
3547 The defined function switches Memory buffer to show address
3548 stored in ADDRESS-VAR variable.
3550 DOC is an optional documentation string."
3554 (let ((gdb-memory-address ,address-var))
3555 (gdb-invalidate-memory))))
3557 (def-gdb-memory-show-page gdb-memory-show-previous-page
3558 gdb-memory-prev-page)
3560 (def-gdb-memory-show-page gdb-memory-show-next-page
3561 gdb-memory-next-page)
3563 (defvar gdb-memory-unit-map
3564 (let ((map (make-sparse-keymap)))
3565 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3567 "Keymap to select units in the header line.")
3569 (defvar gdb-memory-unit-menu
3570 (let ((map (make-sparse-keymap "Unit")))
3571 (define-key map [giantwords]
3572 '(menu-item "Giant words" gdb-memory-unit-giant
3573 :button (:radio . (equal gdb-memory-unit 8))))
3574 (define-key map [words]
3575 '(menu-item "Words" gdb-memory-unit-word
3576 :button (:radio . (equal gdb-memory-unit 4))))
3577 (define-key map [halfwords]
3578 '(menu-item "Halfwords" gdb-memory-unit-halfword
3579 :button (:radio . (equal gdb-memory-unit 2))))
3580 (define-key map [bytes]
3581 '(menu-item "Bytes" gdb-memory-unit-byte
3582 :button (:radio . (equal gdb-memory-unit 1))))
3584 "Menu of units in the header line.")
3586 (defun gdb-memory-unit-menu (event)
3588 (x-popup-menu event gdb-memory-unit-menu))
3590 (defun gdb-memory-unit-menu-1 (event)
3592 (save-selected-window
3593 (select-window (posn-window (event-start event)))
3594 (let* ((selection (gdb-memory-unit-menu event))
3595 (binding (and selection (lookup-key gdb-memory-unit-menu
3596 (vector (car selection))))))
3597 (if binding (call-interactively binding)))))
3599 (defvar gdb-memory-font-lock-keywords
3600 '(;; <__function.name+n>
3601 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3602 (1 font-lock-function-name-face)))
3603 "Font lock keywords used in `gdb-memory-mode'.")
3605 (defvar gdb-memory-header
3610 'face font-lock-warning-face
3611 'help-echo "mouse-1: decrement address"
3612 'mouse-face 'mode-line-highlight
3613 'local-map (gdb-make-header-line-mouse-map
3615 #'gdb-memory-show-previous-page))
3618 'face font-lock-warning-face
3619 'help-echo "mouse-1: increment address"
3620 'mouse-face 'mode-line-highlight
3621 'local-map (gdb-make-header-line-mouse-map
3623 #'gdb-memory-show-next-page))
3625 (propertize gdb-memory-address
3626 'face font-lock-warning-face
3627 'help-echo "mouse-1: set start address"
3628 'mouse-face 'mode-line-highlight
3629 'local-map (gdb-make-header-line-mouse-map
3631 #'gdb-memory-set-address-event))
3633 (propertize (number-to-string gdb-memory-rows)
3634 'face font-lock-warning-face
3635 'help-echo "mouse-1: set number of columns"
3636 'mouse-face 'mode-line-highlight
3637 'local-map (gdb-make-header-line-mouse-map
3639 #'gdb-memory-set-rows))
3641 (propertize (number-to-string gdb-memory-columns)
3642 'face font-lock-warning-face
3643 'help-echo "mouse-1: set number of columns"
3644 'mouse-face 'mode-line-highlight
3645 'local-map (gdb-make-header-line-mouse-map
3647 #'gdb-memory-set-columns))
3649 (propertize gdb-memory-format
3650 'face font-lock-warning-face
3651 'help-echo "mouse-3: select display format"
3652 'mouse-face 'mode-line-highlight
3653 'local-map gdb-memory-format-map)
3655 (propertize (number-to-string gdb-memory-unit)
3656 'face font-lock-warning-face
3657 'help-echo "mouse-3: select unit size"
3658 'mouse-face 'mode-line-highlight
3659 'local-map gdb-memory-unit-map)))
3660 "Header line used in `gdb-memory-mode'.")
3662 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3663 "Major mode for examining memory."
3664 (setq header-line-format gdb-memory-header)
3665 (set (make-local-variable 'font-lock-defaults)
3666 '(gdb-memory-font-lock-keywords))
3667 'gdb-invalidate-memory)
3669 (defun gdb-memory-buffer-name ()
3670 (concat "*memory of " (gdb-get-target-string) "*"))
3672 (defun gdb-display-memory-buffer (&optional thread)
3673 "Display GDB memory contents."
3675 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3677 (defun gdb-frame-memory-buffer ()
3678 "Display memory contents in another frame."
3680 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3681 gdb-display-buffer-other-frame-action))
3684 ;;; Disassembly view
3686 (defun gdb-disassembly-buffer-name ()
3687 (gdb-current-context-buffer-name
3688 (concat "disassembly of " (gdb-get-target-string))))
3690 (defun gdb-display-disassembly-buffer (&optional thread)
3691 "Display GDB disassembly information."
3693 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3695 (def-gdb-preempt-display-buffer
3696 gdb-preemptively-display-disassembly-buffer
3697 'gdb-disassembly-buffer)
3699 (defun gdb-frame-disassembly-buffer (&optional thread)
3700 "Display GDB disassembly information in another frame."
3702 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3703 gdb-display-buffer-other-frame-action))
3705 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3706 (let* ((frame (gdb-current-buffer-frame))
3707 (file (bindat-get-field frame 'fullname))
3708 (line (bindat-get-field frame 'line)))
3710 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3711 ;; If we're unable to get a file name / line for $PC, simply
3712 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3713 ;; 150 bytes) instructions.
3714 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3715 gdb-disassembly-handler
3716 ;; We update disassembly only after we have actual frame information
3717 ;; about all threads, so no there's `update' signal in this list
3718 '(start update-disassembly))
3720 (def-gdb-auto-update-handler
3721 gdb-disassembly-handler
3722 gdb-disassembly-handler-custom
3725 (gdb-set-buffer-rules
3726 'gdb-disassembly-buffer
3727 'gdb-disassembly-buffer-name
3728 'gdb-disassembly-mode
3729 'gdb-invalidate-disassembly)
3731 (defvar gdb-disassembly-font-lock-keywords
3732 '(;; <__function.name+n>
3733 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3734 (1 font-lock-function-name-face))
3735 ;; 0xNNNNNNNN <__function.name+n>: opcode
3736 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3737 (4 font-lock-keyword-face))
3738 ;; %register(at least i386)
3739 ("%\\sw+" . font-lock-variable-name-face)
3740 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3741 (1 font-lock-comment-face)
3742 (2 font-lock-function-name-face))
3743 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3744 "Font lock keywords used in `gdb-disassembly-mode'.")
3746 (defvar gdb-disassembly-mode-map
3748 (let ((map (make-sparse-keymap)))
3749 (suppress-keymap map)
3750 (define-key map "q" 'kill-this-buffer)
3753 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3754 "Major mode for GDB disassembly information."
3755 ;; TODO Rename overlay variable for disassembly mode
3756 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3757 (setq fringes-outside-margins t)
3758 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3759 (set (make-local-variable 'font-lock-defaults)
3760 '(gdb-disassembly-font-lock-keywords))
3761 'gdb-invalidate-disassembly)
3763 (defun gdb-disassembly-handler-custom ()
3764 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3765 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3766 (table (make-gdb-table))
3768 (dolist (instr instructions)
3769 (gdb-table-add-row table
3771 (bindat-get-field instr 'address)
3773 ((func-name (bindat-get-field instr 'func-name))
3774 (offset (bindat-get-field instr 'offset)))
3776 (format "<%s+%s>:" func-name offset)
3778 (bindat-get-field instr 'inst)))
3779 (when (string-equal (bindat-get-field instr 'address)
3782 (setq marked-line (length (gdb-table-rows table)))
3783 (setq fringe-indicator-alist
3784 (if (string-equal gdb-frame-number "0")
3786 '((overlay-arrow . hollow-right-triangle)))))))
3787 (insert (gdb-table-string table " "))
3788 (gdb-disassembly-place-breakpoints)
3789 ;; Mark current position with overlay arrow and scroll window to
3792 (let ((window (get-buffer-window (current-buffer) 0)))
3793 (set-window-point window (gdb-mark-line marked-line
3794 gdb-disassembly-position))))
3796 (gdb-current-context-mode-name
3797 (concat "Disassembly: "
3798 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3800 (defun gdb-disassembly-place-breakpoints ()
3801 (gdb-remove-breakpoint-icons (point-min) (point-max))
3802 (dolist (breakpoint gdb-breakpoints-list)
3803 (let* ((breakpoint (cdr breakpoint))
3804 (bptno (bindat-get-field breakpoint 'number))
3805 (flag (bindat-get-field breakpoint 'enabled))
3806 (address (bindat-get-field breakpoint 'addr)))
3808 (goto-char (point-min))
3809 (if (re-search-forward (concat "^" address) nil t)
3810 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3813 (defvar gdb-breakpoints-header
3815 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3818 (gdb-propertize-header "Threads" gdb-threads-buffer
3819 "mouse-1: select" mode-line-highlight
3820 mode-line-inactive)))
3822 ;;; Breakpoints view
3823 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3824 "Major mode for gdb breakpoints."
3825 (setq header-line-format gdb-breakpoints-header)
3826 'gdb-invalidate-breakpoints)
3828 (defun gdb-toggle-breakpoint ()
3829 "Enable/disable breakpoint at current line of breakpoints buffer."
3833 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3836 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3839 (bindat-get-field breakpoint 'number)))
3840 (error "Not recognized as break/watchpoint line")))))
3842 (defun gdb-delete-breakpoint ()
3843 "Delete the breakpoint at current line of breakpoints buffer."
3847 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3849 (gud-basic-call (concat "-break-delete "
3850 (bindat-get-field breakpoint 'number)))
3851 (error "Not recognized as break/watchpoint line")))))
3853 (defun gdb-goto-breakpoint (&optional event)
3854 "Go to the location of breakpoint at current line of breakpoints buffer."
3855 (interactive (list last-input-event))
3856 (if event (posn-set-point (event-end event)))
3857 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3858 (let ((window (get-buffer-window gud-comint-buffer)))
3859 (if window (save-selected-window (select-window window))))
3862 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3864 (let ((bptno (bindat-get-field breakpoint 'number))
3865 (file (bindat-get-field breakpoint 'fullname))
3866 (line (bindat-get-field breakpoint 'line)))
3867 (save-selected-window
3868 (let* ((buffer (find-file-noselect
3869 (if (file-exists-p file) file
3870 (cdr (assoc bptno gdb-location-alist)))))
3871 (window (or (gdb-display-source-buffer buffer)
3872 (display-buffer buffer))))
3873 (setq gdb-source-window window)
3874 (with-current-buffer buffer
3875 (goto-char (point-min))
3876 (forward-line (1- (string-to-number line)))
3877 (set-window-point window (point))))))
3878 (error "Not recognized as break/watchpoint line")))))
3881 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3883 (def-gdb-trigger-and-handler
3884 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3885 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3888 (gdb-set-buffer-rules
3890 'gdb-stack-buffer-name
3892 'gdb-invalidate-frames)
3894 (defun gdb-frame-location (frame)
3895 "Return \" of file:line\" or \" of library\" for structure FRAME.
3897 FRAME must have either \"file\" and \"line\" members or \"from\"
3899 (let ((file (bindat-get-field frame 'file))
3900 (line (bindat-get-field frame 'line))
3901 (from (bindat-get-field frame 'from)))
3902 (let ((res (or (and file line (concat file ":" line))
3904 (if res (concat " of " res) ""))))
3906 (defun gdb-stack-list-frames-custom ()
3907 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3908 (table (make-gdb-table)))
3909 (set-marker gdb-stack-position nil)
3910 (dolist (frame stack)
3911 (gdb-table-add-row table
3913 (bindat-get-field frame 'level)
3916 (bindat-get-field frame 'func)
3917 (if gdb-stack-buffer-locations
3918 (gdb-frame-location frame) "")
3919 (if gdb-stack-buffer-addresses
3920 (concat " at " (bindat-get-field frame 'addr)) "")))
3921 `(mouse-face highlight
3922 help-echo "mouse-2, RET: Select frame"
3924 (insert (gdb-table-string table " ")))
3925 (when (and gdb-frame-number
3926 (gdb-buffer-shows-main-thread-p))
3927 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3928 gdb-stack-position))
3930 (gdb-current-context-mode-name "Frames")))
3932 (defun gdb-stack-buffer-name ()
3933 (gdb-current-context-buffer-name
3934 (concat "stack frames of " (gdb-get-target-string))))
3936 (defun gdb-display-stack-buffer (&optional thread)
3937 "Display GDB backtrace for current stack."
3939 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
3941 (def-gdb-preempt-display-buffer
3942 gdb-preemptively-display-stack-buffer
3943 'gdb-stack-buffer nil t)
3945 (defun gdb-frame-stack-buffer (&optional thread)
3946 "Display GDB backtrace for current stack in another frame."
3948 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
3949 gdb-display-buffer-other-frame-action))
3951 (defvar gdb-frames-mode-map
3952 (let ((map (make-sparse-keymap)))
3953 (suppress-keymap map)
3954 (define-key map "q" 'kill-this-buffer)
3955 (define-key map "\r" 'gdb-select-frame)
3956 (define-key map [mouse-2] 'gdb-select-frame)
3957 (define-key map [follow-link] 'mouse-face)
3960 (defvar gdb-frames-font-lock-keywords
3961 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3962 "Font lock keywords used in `gdb-frames-mode'.")
3964 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3965 "Major mode for gdb call stack."
3966 (setq gdb-stack-position (make-marker))
3967 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3968 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3969 (set (make-local-variable 'font-lock-defaults)
3970 '(gdb-frames-font-lock-keywords))
3971 'gdb-invalidate-frames)
3973 (defun gdb-select-frame (&optional event)
3974 "Select the frame and display the relevant source."
3975 (interactive (list last-input-event))
3976 (if event (posn-set-point (event-end event)))
3977 (let ((frame (get-text-property (point) 'gdb-frame)))
3979 (if (gdb-buffer-shows-main-thread-p)
3980 (let ((new-level (bindat-get-field frame 'level)))
3981 (setq gdb-frame-number new-level)
3982 (gdb-input (concat "-stack-select-frame " new-level)
3985 (error "Could not select frame for non-current thread"))
3986 (error "Not recognized as frame line"))))
3990 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3991 (def-gdb-trigger-and-handler
3992 gdb-invalidate-locals
3993 (concat (gdb-current-context-command "-stack-list-locals")
3995 gdb-locals-handler gdb-locals-handler-custom
3998 (gdb-set-buffer-rules
4000 'gdb-locals-buffer-name
4002 'gdb-invalidate-locals)
4004 (defvar gdb-locals-watch-map
4005 (let ((map (make-sparse-keymap)))
4006 (suppress-keymap map)
4007 (define-key map "\r" 'gud-watch)
4008 (define-key map [mouse-2] 'gud-watch)
4010 "Keymap to create watch expression of a complex data type local variable.")
4012 (defvar gdb-edit-locals-map-1
4013 (let ((map (make-sparse-keymap)))
4014 (suppress-keymap map)
4015 (define-key map "\r" 'gdb-edit-locals-value)
4016 (define-key map [mouse-2] 'gdb-edit-locals-value)
4018 "Keymap to edit value of a simple data type local variable.")
4020 (defun gdb-edit-locals-value (&optional event)
4021 "Assign a value to a variable displayed in the locals buffer."
4022 (interactive (list last-input-event))
4024 (if event (posn-set-point (event-end event)))
4026 (let* ((var (bindat-get-field
4027 (get-text-property (point) 'gdb-local-variable) 'name))
4028 (value (read-string (format "New value (%s): " var))))
4030 (concat "-gdb-set variable " var " = " value)))))
4032 ;; Don't display values of arrays or structures.
4033 ;; These can be expanded using gud-watch.
4034 (defun gdb-locals-handler-custom ()
4035 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4036 (table (make-gdb-table)))
4037 (dolist (local locals-list)
4038 (let ((name (bindat-get-field local 'name))
4039 (value (bindat-get-field local 'value))
4040 (type (bindat-get-field local 'type)))
4042 (setq value "<complex data type>"))
4044 (string-match "\\0x" value))
4045 (add-text-properties 0 (length name)
4046 `(mouse-face highlight
4047 help-echo "mouse-2: create watch expression"
4048 local-map ,gdb-locals-watch-map)
4050 (add-text-properties 0 (length value)
4051 `(mouse-face highlight
4052 help-echo "mouse-2: edit value"
4053 local-map ,gdb-edit-locals-map-1)
4058 (propertize type 'font-lock-face font-lock-type-face)
4059 (propertize name 'font-lock-face font-lock-variable-name-face)
4061 `(gdb-local-variable ,local))))
4062 (insert (gdb-table-string table " "))
4064 (gdb-current-context-mode-name
4066 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4068 (defvar gdb-locals-header
4070 (gdb-propertize-header "Locals" gdb-locals-buffer
4073 (gdb-propertize-header "Registers" gdb-registers-buffer
4074 "mouse-1: select" mode-line-highlight
4075 mode-line-inactive)))
4077 (defvar gdb-locals-mode-map
4078 (let ((map (make-sparse-keymap)))
4079 (suppress-keymap map)
4080 (define-key map "q" 'kill-this-buffer)
4081 (define-key map "\t" (lambda ()
4083 (gdb-set-window-buffer
4084 (gdb-get-buffer-create
4085 'gdb-registers-buffer
4086 gdb-thread-number) t)))
4089 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4090 "Major mode for gdb locals."
4091 (setq header-line-format gdb-locals-header)
4092 'gdb-invalidate-locals)
4094 (defun gdb-locals-buffer-name ()
4095 (gdb-current-context-buffer-name
4096 (concat "locals of " (gdb-get-target-string))))
4098 (defun gdb-display-locals-buffer (&optional thread)
4099 "Display the local variables of current GDB stack."
4101 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4103 (def-gdb-preempt-display-buffer
4104 gdb-preemptively-display-locals-buffer
4105 'gdb-locals-buffer nil t)
4107 (defun gdb-frame-locals-buffer (&optional thread)
4108 "Display the local variables of the current GDB stack in another frame."
4110 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4111 gdb-display-buffer-other-frame-action))
4114 ;; Registers buffer.
4116 (def-gdb-trigger-and-handler
4117 gdb-invalidate-registers
4118 (concat (gdb-current-context-command "-data-list-register-values") " x")
4119 gdb-registers-handler
4120 gdb-registers-handler-custom
4123 (gdb-set-buffer-rules
4124 'gdb-registers-buffer
4125 'gdb-registers-buffer-name
4127 'gdb-invalidate-registers)
4129 (defun gdb-registers-handler-custom ()
4130 (when gdb-register-names
4131 (let ((register-values
4132 (bindat-get-field (gdb-json-partial-output) 'register-values))
4133 (table (make-gdb-table)))
4134 (dolist (register register-values)
4135 (let* ((register-number (bindat-get-field register 'number))
4136 (value (bindat-get-field register 'value))
4137 (register-name (nth (string-to-number register-number)
4138 gdb-register-names)))
4142 (propertize register-name
4143 'font-lock-face font-lock-variable-name-face)
4144 (if (member register-number gdb-changed-registers)
4145 (propertize value 'font-lock-face font-lock-warning-face)
4147 `(mouse-face highlight
4148 help-echo "mouse-2: edit value"
4149 gdb-register-name ,register-name))))
4150 (insert (gdb-table-string table " ")))
4152 (gdb-current-context-mode-name "Registers"))))
4154 (defun gdb-edit-register-value (&optional event)
4155 "Assign a value to a register displayed in the registers buffer."
4156 (interactive (list last-input-event))
4158 (if event (posn-set-point (event-end event)))
4160 (let* ((var (bindat-get-field
4161 (get-text-property (point) 'gdb-register-name)))
4162 (value (read-string (format "New value (%s): " var))))
4164 (concat "-gdb-set variable $" var " = " value)))))
4166 (defvar gdb-registers-mode-map
4167 (let ((map (make-sparse-keymap)))
4168 (suppress-keymap map)
4169 (define-key map "\r" 'gdb-edit-register-value)
4170 (define-key map [mouse-2] 'gdb-edit-register-value)
4171 (define-key map "q" 'kill-this-buffer)
4172 (define-key map "\t" (lambda ()
4174 (gdb-set-window-buffer
4175 (gdb-get-buffer-create
4177 gdb-thread-number) t)))
4180 (defvar gdb-registers-header
4182 (gdb-propertize-header "Locals" gdb-locals-buffer
4183 "mouse-1: select" mode-line-highlight
4186 (gdb-propertize-header "Registers" gdb-registers-buffer
4187 nil nil mode-line)))
4189 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4190 "Major mode for gdb registers."
4191 (setq header-line-format gdb-registers-header)
4192 'gdb-invalidate-registers)
4194 (defun gdb-registers-buffer-name ()
4195 (gdb-current-context-buffer-name
4196 (concat "registers of " (gdb-get-target-string))))
4198 (defun gdb-display-registers-buffer (&optional thread)
4199 "Display GDB register contents."
4201 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4203 (def-gdb-preempt-display-buffer
4204 gdb-preemptively-display-registers-buffer
4205 'gdb-registers-buffer nil t)
4207 (defun gdb-frame-registers-buffer (&optional thread)
4208 "Display GDB register contents in another frame."
4210 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4211 gdb-display-buffer-other-frame-action))
4213 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4214 (defun gdb-get-changed-registers ()
4215 (when (gdb-get-buffer 'gdb-registers-buffer)
4216 (gdb-input "-data-list-changed-registers"
4217 'gdb-changed-registers-handler
4218 'gdb-get-changed-registers)))
4220 (defun gdb-changed-registers-handler ()
4221 (setq gdb-changed-registers nil)
4222 (dolist (register-number
4223 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4224 (push register-number gdb-changed-registers)))
4226 (defun gdb-register-names-handler ()
4227 ;; Don't use pending triggers because this handler is called
4228 ;; only once (in gdb-init-1)
4229 (setq gdb-register-names nil)
4230 (dolist (register-name
4231 (bindat-get-field (gdb-json-partial-output) 'register-names))
4232 (push register-name gdb-register-names))
4233 (setq gdb-register-names (reverse gdb-register-names)))
4236 (defun gdb-get-source-file-list ()
4237 "Create list of source files for current GDB session.
4238 If buffers already exist for any of these files, `gud-minor-mode'
4240 (goto-char (point-min))
4241 (while (re-search-forward gdb-source-file-regexp nil t)
4242 (push (read (match-string 1)) gdb-source-file-list))
4243 (dolist (buffer (buffer-list))
4244 (with-current-buffer buffer
4245 (when (member buffer-file-name gdb-source-file-list)
4246 (gdb-init-buffer)))))
4248 (defun gdb-get-main-selected-frame ()
4249 "Trigger for `gdb-frame-handler' which uses main current thread.
4250 Called from `gdb-update'."
4251 (gdb-input (gdb-current-context-command "-stack-info-frame")
4253 'gdb-get-main-selected-frame))
4255 (defun gdb-frame-handler ()
4256 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4257 overlay arrow in source buffer."
4258 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4260 (setq gdb-selected-frame (bindat-get-field frame 'func))
4261 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4262 (setq gdb-frame-number (bindat-get-field frame 'level))
4263 (setq gdb-frame-address (bindat-get-field frame 'addr))
4264 (let ((line (bindat-get-field frame 'line)))
4265 (setq gdb-selected-line (and line (string-to-number line)))
4266 (when (and gdb-selected-file gdb-selected-line)
4267 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4268 (gud-display-frame)))
4269 (if gud-overlay-arrow-position
4270 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4271 (position (marker-position gud-overlay-arrow-position)))
4273 (with-current-buffer buffer
4274 (setq fringe-indicator-alist
4275 (if (string-equal gdb-frame-number "0")
4277 '((overlay-arrow . hollow-right-triangle))))
4278 (setq gud-overlay-arrow-position (make-marker))
4279 (set-marker gud-overlay-arrow-position position))))))))
4281 (defconst gdb-prompt-name-regexp
4282 (concat "value=\\(" gdb--string-regexp "\\)"))
4284 (defun gdb-get-prompt ()
4285 "Find prompt for GDB session."
4286 (goto-char (point-min))
4287 (setq gdb-prompt-name nil)
4288 (re-search-forward gdb-prompt-name-regexp nil t)
4289 (setq gdb-prompt-name (read (match-string 1)))
4290 ;; Insert first prompt.
4291 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4293 ;;;; Window management
4294 (defun gdb-display-buffer (buf)
4295 "Show buffer BUF, and make that window dedicated."
4296 (let ((window (display-buffer buf)))
4297 (set-window-dedicated-p window t)
4300 ;; (let ((answer (get-buffer-window buf 0)))
4302 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4303 ;; (let ((window (get-lru-window)))
4304 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4306 ;; (let ((largest (get-largest-window)))
4307 ;; (setq answer (split-window largest))
4308 ;; (set-window-buffer answer buf)
4309 ;; (set-window-dedicated-p answer t)
4311 ;; (set-window-buffer window buf)
4315 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4316 "Find window displaying a buffer with the same
4317 `gdb-buffer-type' as BUF and show BUF there. If no such window
4318 exists, just call `gdb-display-buffer' for BUF. If the window
4319 found is already dedicated, split window according to
4320 SPLIT-HORIZONTAL and show BUF in the new window."
4322 (when (not (get-buffer-window buf))
4323 (let* ((buf-type (gdb-buffer-type buf))
4325 (get-window-with-predicate
4328 (gdb-buffer-type (window-buffer w)))
4329 (not (window-dedicated-p w)))))))
4331 (set-window-buffer existing-window buf)
4332 (let ((dedicated-window
4333 (get-window-with-predicate
4336 (gdb-buffer-type (window-buffer w)))))))
4337 (if dedicated-window
4339 (split-window dedicated-window nil split-horizontal) buf)
4340 (gdb-display-buffer buf))))))
4341 (error "Null buffer")))
4343 ;;; Shared keymap initialization:
4345 (let ((menu (make-sparse-keymap "GDB-Windows")))
4346 (define-key gud-menu-map [displays]
4347 `(menu-item "GDB-Windows" ,menu
4348 :visible (eq gud-minor-mode 'gdbmi)))
4349 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4350 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4351 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4352 (define-key menu [disassembly]
4353 '("Disassembly" . gdb-display-disassembly-buffer))
4354 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4355 (define-key menu [inferior]
4356 '("IO" . gdb-display-io-buffer))
4357 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4358 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4359 (define-key menu [breakpoints]
4360 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4362 (let ((menu (make-sparse-keymap "GDB-Frames")))
4363 (define-key gud-menu-map [frames]
4364 `(menu-item "GDB-Frames" ,menu
4365 :visible (eq gud-minor-mode 'gdbmi)))
4366 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4367 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4368 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4369 (define-key menu [disassembly]
4370 '("Disassembly" . gdb-frame-disassembly-buffer))
4371 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4372 (define-key menu [inferior]
4373 '("IO" . gdb-frame-io-buffer))
4374 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4375 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4376 (define-key menu [breakpoints]
4377 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4379 (let ((menu (make-sparse-keymap "GDB-MI")))
4380 (define-key menu [gdb-customize]
4381 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4382 :help "Customize Gdb Graphical Mode options."))
4383 (define-key menu [gdb-many-windows]
4384 '(menu-item "Display Other Windows" gdb-many-windows
4385 :help "Toggle display of locals, stack and breakpoint information"
4386 :button (:toggle . gdb-many-windows)))
4387 (define-key menu [gdb-restore-windows]
4388 '(menu-item "Restore Window Layout" gdb-restore-windows
4389 :help "Restore standard layout for debug session."))
4390 (define-key menu [sep1]
4392 (define-key menu [all-threads]
4393 '(menu-item "GUD controls all threads"
4396 (setq gdb-gud-control-all-threads t))
4397 :help "GUD start/stop commands apply to all threads"
4398 :button (:radio . gdb-gud-control-all-threads)))
4399 (define-key menu [current-thread]
4400 '(menu-item "GUD controls current thread"
4403 (setq gdb-gud-control-all-threads nil))
4404 :help "GUD start/stop commands apply to current thread only"
4405 :button (:radio . (not gdb-gud-control-all-threads))))
4406 (define-key menu [sep2]
4408 (define-key menu [gdb-customize-reasons]
4409 '(menu-item "Customize switching..."
4412 (customize-option 'gdb-switch-reasons))))
4413 (define-key menu [gdb-switch-when-another-stopped]
4414 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4415 gdb-switch-when-another-stopped
4416 "Automatically switch to stopped thread"
4417 "GDB thread switching %s"
4418 "Switch to stopped thread"))
4419 (define-key gud-menu-map [mi]
4420 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4422 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4423 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4424 ;; tool-bar-local-item-from-menu here because it appends new buttons
4425 ;; to toolbar from right to left while we want our A/T throttle to
4426 ;; show up right before Run button.
4427 (define-key-after gud-tool-bar-map [all-threads]
4428 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4429 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4430 :visible (and (eq gud-minor-mode 'gdbmi)
4432 (not gdb-gud-control-all-threads)))
4435 (define-key-after gud-tool-bar-map [current-thread]
4436 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4437 :image (find-image '((:type xpm :file "gud/all.xpm")))
4438 :visible (and (eq gud-minor-mode 'gdbmi)
4440 gdb-gud-control-all-threads))
4443 (defun gdb-frame-gdb-buffer ()
4444 "Display GUD buffer in another frame."
4446 (display-buffer-other-frame gud-comint-buffer))
4448 (defun gdb-display-gdb-buffer ()
4449 "Display GUD buffer."
4451 (pop-to-buffer gud-comint-buffer nil 0))
4453 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4454 "Set buffer of selected window to NAME and dedicate window.
4456 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4457 window is dedicated."
4458 (unless window (setq window (selected-window)))
4459 (when ignore-dedicated
4460 (set-window-dedicated-p window nil))
4461 (set-window-buffer window (get-buffer name))
4462 (set-window-dedicated-p window t))
4464 (defun gdb-setup-windows ()
4465 "Layout the window pattern for option `gdb-many-windows'."
4466 (gdb-get-buffer-create 'gdb-locals-buffer)
4467 (gdb-get-buffer-create 'gdb-stack-buffer)
4468 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4469 (set-window-dedicated-p (selected-window) nil)
4470 (switch-to-buffer gud-comint-buffer)
4471 (delete-other-windows)
4472 (let ((win0 (selected-window))
4473 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4474 (win2 (split-window nil ( / (window-height) 3)))
4475 (win3 (split-window-right)))
4476 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4477 (select-window win2)
4480 (if gud-last-last-frame
4481 (gud-find-file (car gud-last-last-frame))
4483 (gud-find-file gdb-main-file)
4484 ;; Put buffer list in window if we
4485 ;; can't find a source file.
4486 (list-buffers-noselect))))
4487 (setq gdb-source-window (selected-window))
4488 (let ((win4 (split-window-right)))
4489 (gdb-set-window-buffer
4490 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4491 (select-window win1)
4492 (gdb-set-window-buffer (gdb-stack-buffer-name))
4493 (let ((win5 (split-window-right)))
4494 (gdb-set-window-buffer (if gdb-show-threads-by-default
4495 (gdb-threads-buffer-name)
4496 (gdb-breakpoints-buffer-name))
4498 (select-window win0)))
4500 (define-minor-mode gdb-many-windows
4501 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4502 In this case it starts with two windows: one displaying the GUD
4503 buffer and the other with the source file with the main routine
4504 of the debugged program. Non-nil means display the layout shown for
4509 (if (and gud-comint-buffer
4510 (buffer-name gud-comint-buffer))
4512 (gdb-restore-windows))))
4514 (defun gdb-restore-windows ()
4515 "Restore the basic arrangement of windows used by gdb.
4516 This arrangement depends on the value of option `gdb-many-windows'."
4518 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4519 (delete-other-windows)
4520 (if gdb-many-windows
4522 (when (or gud-last-last-frame gdb-show-main)
4523 (let ((win (split-window)))
4526 (if gud-last-last-frame
4527 (gud-find-file (car gud-last-last-frame))
4528 (gud-find-file gdb-main-file)))
4529 (setq gdb-source-window win)))))
4531 ;; Called from `gud-sentinel' in gud.el:
4533 "Exit a debugging session cleanly.
4534 Kills the gdb buffers, and resets variables and the source buffers."
4535 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4536 ;; process. This pty must be deleted explicitly.
4537 (let ((pty (get-process "gdb-inferior")))
4538 (if pty (delete-process pty)))
4539 ;; Find gdb-mi buffers and kill them.
4540 (dolist (buffer (buffer-list))
4541 (unless (eq buffer gud-comint-buffer)
4542 (with-current-buffer buffer
4543 (if (eq gud-minor-mode 'gdbmi)
4544 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4546 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4547 (setq gud-minor-mode nil)
4548 (kill-local-variable 'tool-bar-map)
4549 (kill-local-variable 'gdb-define-alist))))))
4550 (setq gdb-disassembly-position nil)
4551 (setq overlay-arrow-variable-list
4552 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4553 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4554 (setq gdb-stack-position nil)
4555 (setq overlay-arrow-variable-list
4556 (delq 'gdb-stack-position overlay-arrow-variable-list))
4557 (setq gdb-thread-position nil)
4558 (setq overlay-arrow-variable-list
4559 (delq 'gdb-thread-position overlay-arrow-variable-list))
4560 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4561 (setq gud-running nil)
4562 (setq gdb-active-process nil)
4563 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4565 (defun gdb-get-source-file ()
4566 "Find the source file where the program starts and display it with related
4567 buffers, if required."
4568 (goto-char (point-min))
4569 (if (re-search-forward gdb-source-file-regexp nil t)
4570 (setq gdb-main-file (read (match-string 1))))
4571 (if gdb-many-windows
4573 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4576 (display-buffer (gud-find-file gdb-main-file))))
4577 (gdb-force-mode-line-update
4578 (propertize "ready" 'face font-lock-variable-name-face)))
4581 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4582 "Put string PUTSTRING in front of POS in the current buffer.
4583 PUTSTRING is displayed by putting an overlay into the current buffer with a
4584 `before-string' string that has a `display' property whose value is
4586 (let ((string (make-string 1 ?x))
4587 (buffer (current-buffer)))
4588 (setq putstring (copy-sequence putstring))
4589 (let ((overlay (make-overlay pos pos buffer))
4591 (list (list 'margin 'left-margin) putstring))))
4592 (put-text-property 0 1 'display prop string)
4594 (add-text-properties 0 1 sprops string))
4595 (overlay-put overlay 'put-break t)
4596 (overlay-put overlay 'before-string string))))
4598 ;;from remove-images
4599 (defun gdb-remove-strings (start end &optional buffer)
4600 "Remove strings between START and END in BUFFER.
4601 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4602 BUFFER nil or omitted means use the current buffer."
4604 (setq buffer (current-buffer)))
4605 (dolist (overlay (overlays-in start end))
4606 (when (overlay-get overlay 'put-break)
4607 (delete-overlay overlay))))
4609 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4610 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4611 (start (- (car posns) 1))
4612 (end (+ (cdr posns) 1))
4613 (putstring (if enabled "B" "b"))
4614 (source-window (get-buffer-window (current-buffer) 0)))
4615 (add-text-properties
4616 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4619 (add-text-properties
4620 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4621 (add-text-properties
4622 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4623 (gdb-remove-breakpoint-icons start end)
4624 (if (display-images-p)
4625 (if (>= (or left-fringe-width
4626 (if source-window (car (window-fringes source-window)))
4627 gdb-buffer-fringe-width) 8)
4630 `(left-fringe breakpoint
4633 'breakpoint-disabled))
4635 'gdb-enabled enabled)
4636 (when (< left-margin-width 2)
4637 (save-current-buffer
4638 (setq left-margin-width 2)
4642 left-margin-width right-margin-width))))
4645 (or breakpoint-enabled-icon
4646 (setq breakpoint-enabled-icon
4647 (find-image `((:type xpm :data
4648 ,breakpoint-xpm-data
4649 :ascent 100 :pointer hand)
4651 ,breakpoint-enabled-pbm-data
4652 :ascent 100 :pointer hand)))))
4653 (or breakpoint-disabled-icon
4654 (setq breakpoint-disabled-icon
4655 (find-image `((:type xpm :data
4656 ,breakpoint-xpm-data
4657 :conversion disabled
4658 :ascent 100 :pointer hand)
4660 ,breakpoint-disabled-pbm-data
4661 :ascent 100 :pointer hand))))))
4665 (when (< left-margin-width 2)
4666 (save-current-buffer
4667 (setq left-margin-width 2)
4668 (let ((window (get-buffer-window (current-buffer) 0)))
4671 window left-margin-width right-margin-width)))))
4673 (propertize putstring
4675 'breakpoint-enabled 'breakpoint-disabled))
4678 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4679 (gdb-remove-strings start end)
4680 (if (display-images-p)
4681 (remove-images start end))
4683 (setq left-margin-width 0)
4684 (let ((window (get-buffer-window (current-buffer) 0)))
4687 window left-margin-width right-margin-width)))))
4690 ;;; Functions for inline completion.
4692 (defvar gud-gdb-fetch-lines-in-progress)
4693 (defvar gud-gdb-fetch-lines-string)
4694 (defvar gud-gdb-fetch-lines-break)
4695 (defvar gud-gdb-fetched-lines)
4697 (defun gud-gdbmi-completions (context command)
4698 "Completion table for GDB/MI commands.
4699 COMMAND is the prefix for which we seek completion.
4700 CONTEXT is the text before COMMAND on the line."
4701 (let ((gud-gdb-fetch-lines-in-progress t)
4702 (gud-gdb-fetch-lines-string nil)
4703 (gud-gdb-fetch-lines-break (length context))
4704 (gud-gdb-fetched-lines nil)
4705 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4706 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4707 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4708 (gdb-input (concat "complete " context command)
4709 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4710 (while gud-gdb-fetch-lines-in-progress
4711 (accept-process-output (get-buffer-process gud-comint-buffer))))
4712 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4714 (defun gud-gdbmi-fetch-lines-filter (string)
4715 "Custom filter function for `gud-gdbmi-completions'."
4716 (setq string (concat gud-gdb-fetch-lines-string
4717 (gud-gdbmi-marker-filter string)))
4718 (while (string-match "\n" string)
4719 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4720 gud-gdb-fetched-lines)
4721 (setq string (substring string (match-end 0))))
4726 ;;; gdb-mi.el ends here