1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
7 ;; Keywords: unix, tools
9 ;; This file is part of GNU Emacs.
11 ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file was written by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It was further developed
30 ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
31 ;; of Code 2009 Project "Emacs GDB/MI migration".
35 ;; This mode acts as a graphical user interface to GDB. You can interact with
36 ;; GDB through the GUD buffer in the usual way, but there are also further
37 ;; buffers which control the execution and describe the state of your program.
38 ;; It separates the input/output of your program from that of GDB and displays
39 ;; expressions and their current values in their own buffers. It also uses
40 ;; features of Emacs 21 such as the fringe/display margin for breakpoints, and
41 ;; the toolbar (see the GDB Graphical Interface section in the Emacs info
44 ;; M-x gdb will start the debugger.
46 ;; This file uses GDB/MI as the primary interface to GDB. It runs gdb with
47 ;; GDB/MI (-interp=mi) and access CLI using "-interpreter-exec console
48 ;; cli-command". This code replaces gdb-ui.el and uses MI tokens instead
49 ;; of queues. Eventually MI should be asynchronous.
53 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
54 ;; explicitly in your program if you want timely display of I/O in Emacs.
55 ;; Alternatively you can make the output stream unbuffered, for example, by
59 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
62 ;; and compiling with -DUNBUFFERED while debugging.
64 ;; If you are using Cygwin GDB and find that the source is not being displayed
65 ;; in Emacs when you step through it, possible solutions are to:
67 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
68 ;; (Since 22.1 Emacs builds under Cygwin.)
69 ;; 2) Use MinGW GDB instead.
70 ;; 3) Use cygwin-mount.el
74 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of Mac OSX.
76 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
77 ;; as earlier versions do not compile on Mac OSX.
81 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
82 ;; line information, e.g., a routine in libc (just a TODO item).
85 ;; 2) Watch windows to work with threads.
86 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
87 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
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
)))
984 (if (and (eobp) proc
(process-live-p proc
)
986 (= (point) (marker-position (process-mark proc
))))
987 ;; Sending an EOF does not work with GDB-MI; submit an
988 ;; explicit quit command.
991 (comint-send-input t t
))
994 (defvar gdb-define-alist nil
"Alist of #define directives for GUD tooltips.")
996 (defun gdb-create-define-alist ()
997 "Create an alist of #define directives for GUD tooltips."
998 (let* ((file (buffer-file-name))
1000 (with-output-to-string
1001 (with-current-buffer standard-output
1003 (file-exists-p file
)
1004 ;; call-process doesn't work with remote file names.
1005 (not (file-remote-p default-directory
))
1006 (call-process shell-file-name file
1007 (list t nil
) nil
"-c"
1008 (concat gdb-cpp-define-alist-program
" "
1009 gdb-cpp-define-alist-flags
))))))
1010 (define-list (split-string output
"\n" t
))
1012 (setq gdb-define-alist nil
)
1013 (dolist (define define-list
)
1014 (setq name
(nth 1 (split-string define
"[( ]")))
1015 (push (cons name define
) gdb-define-alist
))))
1017 (declare-function tooltip-show
"tooltip" (text &optional use-echo-area
))
1019 (defun gdb-tooltip-print (expr)
1020 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
1021 (goto-char (point-min))
1023 ((re-search-forward ".*value=\\(\".*\"\\)" nil t
)
1025 (concat expr
" = " (read (match-string 1)))
1026 (or gud-tooltip-echo-area
1027 (not (display-graphic-p)))))
1028 ((re-search-forward "msg=\\(\".+\"\\)$" nil t
)
1029 (tooltip-show (read (match-string 1))
1030 (or gud-tooltip-echo-area
1031 (not (display-graphic-p))))))))
1033 ;; If expr is a macro for a function don't print because of possible dangerous
1034 ;; side-effects. Also printing a function within a tooltip generates an
1035 ;; unexpected starting annotation (phase error).
1036 (defun gdb-tooltip-print-1 (expr)
1037 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
1038 (goto-char (point-min))
1039 (if (search-forward "expands to: " nil t
)
1040 (unless (looking-at "\\S-+.*(.*).*")
1041 (gdb-input (concat "-data-evaluate-expression \"" expr
"\"")
1042 `(lambda () (gdb-tooltip-print ,expr
)))))))
1044 (defun gdb-init-buffer ()
1045 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
1046 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1047 (when gud-tooltip-mode
1048 (make-local-variable 'gdb-define-alist
)
1049 (gdb-create-define-alist)
1050 (add-hook 'after-save-hook
'gdb-create-define-alist nil t
)))
1052 (defmacro gdb--if-arrow
(arrow-position start-posn end-posn
&rest body
)
1053 (declare (indent 3))
1054 (let ((buffer (make-symbol "buffer")))
1055 `(if ,arrow-position
1056 (let ((,buffer
(marker-buffer ,arrow-position
)))
1057 (if (equal ,buffer
(window-buffer (posn-window ,end-posn
)))
1058 (with-current-buffer ,buffer
1059 (when (or (equal ,start-posn
,end-posn
)
1060 (equal (posn-point ,start-posn
)
1061 (marker-position ,arrow-position
)))
1064 (defun gdb-mouse-until (event)
1065 "Continue running until a source line past the current line.
1066 The destination source line can be selected either by clicking
1067 with mouse-3 on the fringe/margin or dragging the arrow
1068 with mouse-1 (default bindings)."
1070 (let ((start (event-start event
))
1071 (end (event-end event
)))
1072 (gdb--if-arrow gud-overlay-arrow-position start end
1073 (let ((line (line-number-at-pos (posn-point end
))))
1074 (gud-call (concat "until " (number-to-string line
)))))
1075 (gdb--if-arrow gdb-disassembly-position start end
1077 (goto-char (point-min))
1078 (forward-line (1- (line-number-at-pos (posn-point end
))))
1080 (gud-call (concat "until *%a"))))))
1082 (defun gdb-mouse-jump (event)
1083 "Set execution address/line.
1084 The destination source line can be selected either by clicking with C-mouse-3
1085 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1086 Unlike `gdb-mouse-until' the destination address can be before the current
1087 line, and no execution takes place."
1089 (let ((start (event-start event
))
1090 (end (event-end event
)))
1091 (gdb--if-arrow gud-overlay-arrow-position start end
1092 (let ((line (line-number-at-pos (posn-point end
))))
1093 (gud-call (concat "tbreak " (number-to-string line
)))
1094 (gud-call (concat "jump " (number-to-string line
)))))
1095 (gdb--if-arrow gdb-disassembly-position start end
1097 (goto-char (point-min))
1098 (forward-line (1- (line-number-at-pos (posn-point end
))))
1100 (gud-call (concat "tbreak *%a"))
1101 (gud-call (concat "jump *%a"))))))
1103 (defcustom gdb-show-changed-values t
1104 "If non-nil change the face of out of scope variables and changed values.
1105 Out of scope variables are suppressed with `shadow' face.
1106 Changed values are highlighted with the face `font-lock-warning-face'."
1111 (defcustom gdb-max-children
40
1112 "Maximum number of children before expansion requires confirmation."
1117 (defcustom gdb-delete-out-of-scope t
1118 "If non-nil delete watch expressions automatically when they go out of scope."
1123 (define-minor-mode gdb-speedbar-auto-raise
1124 "Minor mode to automatically raise the speedbar for watch expressions.
1125 With prefix argument ARG, automatically raise speedbar if ARG is
1126 positive, otherwise don't automatically raise it."
1131 (defcustom gdb-use-colon-colon-notation nil
1132 "If non-nil use FUN::VAR format to display variables in the speedbar."
1137 (define-key gud-minor-mode-map
"\C-c\C-w" 'gud-watch
)
1138 (define-key global-map
(vconcat gud-key-prefix
"\C-w") 'gud-watch
)
1140 (declare-function tooltip-identifier-from-point
"tooltip" (point))
1142 (defun gud-watch (&optional arg event
)
1143 "Watch expression at point.
1144 With arg, enter name of variable to be watched in the minibuffer."
1145 (interactive (list current-prefix-arg last-input-event
))
1146 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer
)))
1147 (if (eq minor-mode
'gdbmi
)
1149 (if event
(posn-set-point (event-end event
)))
1151 (save-selected-window
1154 (completing-read "Name of variable: "
1155 'gud-gdb-complete-command
)
1156 (if (and transient-mark-mode mark-active
)
1157 (buffer-substring (region-beginning) (region-end))
1158 (concat (if (derived-mode-p 'gdb-registers-mode
) "$")
1159 (tooltip-identifier-from-point (point)))))))
1160 (set-text-properties 0 (length expr
) nil expr
)
1161 (gdb-input (concat "-var-create - * " expr
"")
1162 `(lambda () (gdb-var-create-handler ,expr
))))))
1163 (message "gud-watch is a no-op in this mode."))))
1165 (defun gdb-var-create-handler (expr)
1166 (let* ((result (gdb-json-partial-output)))
1167 (if (not (bindat-get-field result
'msg
))
1169 (list (bindat-get-field result
'name
)
1170 (if (and (string-equal gdb-current-language
"c")
1171 gdb-use-colon-colon-notation gdb-selected-frame
)
1172 (setq expr
(concat gdb-selected-frame
"::" expr
))
1174 (bindat-get-field result
'numchild
)
1175 (bindat-get-field result
'type
)
1176 (bindat-get-field result
'value
)
1178 (bindat-get-field result
'has_more
)
1179 gdb-frame-address
)))
1180 (push var gdb-var-list
)
1182 (unless (string-equal
1183 speedbar-initial-expansion-list-name
"GUD")
1184 (speedbar-change-initial-expansion-list "GUD")))
1185 (message-box "No symbol \"%s\" in current context." expr
))))
1187 (defun gdb-speedbar-update ()
1188 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1189 ;; Dummy command to update speedbar even when idle.
1190 (gdb-input "-environment-pwd"
1191 'gdb-speedbar-timer-fn
1192 'gdb-speedbar-update
)))
1194 (defun gdb-speedbar-timer-fn ()
1195 (if gdb-speedbar-auto-raise
1196 (raise-frame speedbar-frame
))
1197 (speedbar-timer-fn))
1199 (defun gdb-var-evaluate-expression-handler (varnum changed
)
1200 (goto-char (point-min))
1201 (re-search-forward ".*value=\\(\".*\"\\)" nil t
)
1202 (let ((var (assoc varnum gdb-var-list
)))
1204 (if changed
(setcar (nthcdr 5 var
) 'changed
))
1205 (setcar (nthcdr 4 var
) (read (match-string 1)))))
1206 (gdb-speedbar-update))
1208 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1209 (defun gdb-var-list-children (varnum)
1210 (gdb-input (concat "-var-update " varnum
) 'ignore
)
1211 (gdb-input (concat "-var-list-children --all-values " varnum
)
1212 `(lambda () (gdb-var-list-children-handler ,varnum
))))
1214 (defun gdb-var-list-children-handler (varnum)
1215 (let* ((var-list nil
)
1216 (output (bindat-get-field (gdb-json-partial-output "child")))
1217 (children (bindat-get-field output
'children
)))
1218 (catch 'child-already-watched
1219 (dolist (var gdb-var-list
)
1220 (if (string-equal varnum
(car var
))
1222 ;; With dynamic varobjs numchild may have increased.
1223 (setcar (nthcdr 2 var
) (bindat-get-field output
'numchild
))
1225 (dolist (child children
)
1226 (let ((varchild (list (bindat-get-field child
'name
)
1227 (bindat-get-field child
'exp
)
1228 (bindat-get-field child
'numchild
)
1229 (bindat-get-field child
'type
)
1230 (bindat-get-field child
'value
)
1232 (bindat-get-field child
'has_more
))))
1233 (if (assoc (car varchild
) gdb-var-list
)
1234 (throw 'child-already-watched nil
))
1235 (push varchild var-list
))))
1236 (push var var-list
)))
1237 (setq gdb-var-list
(nreverse var-list
))))
1238 (gdb-speedbar-update))
1240 (defun gdb-var-set-format (format)
1241 "Set the output format for a variable displayed in the speedbar."
1242 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1244 (gdb-input (concat "-var-set-format " varnum
" " format
) 'ignore
)
1247 (defun gdb-var-delete-1 (var varnum
)
1248 (gdb-input (concat "-var-delete " varnum
) 'ignore
)
1249 (setq gdb-var-list
(delq var gdb-var-list
))
1250 (dolist (varchild gdb-var-list
)
1251 (if (string-match (concat (car var
) "\\.") (car varchild
))
1252 (setq gdb-var-list
(delq varchild gdb-var-list
)))))
1254 (defun gdb-var-delete ()
1255 "Delete watch expression at point from the speedbar."
1257 (let ((text (speedbar-line-text)))
1258 (string-match "\\(\\S-+\\)" text
)
1259 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1261 (if (string-match "\\." (car var
))
1262 (message-box "Can only delete a root expression")
1263 (gdb-var-delete-1 var varnum
)))))
1265 (defun gdb-var-delete-children (varnum)
1266 "Delete children of variable object at point from the speedbar."
1267 (gdb-input (concat "-var-delete -c " varnum
) 'ignore
))
1269 (defun gdb-edit-value (_text _token _indent
)
1270 "Assign a value to a variable displayed in the speedbar."
1271 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1273 (value (read-string "New value: ")))
1274 (gdb-input (concat "-var-assign " varnum
" " value
)
1275 `(lambda () (gdb-edit-value-handler ,value
)))))
1277 (defconst gdb-error-regexp
"\\^error,msg=\\(\".+\"\\)")
1279 (defun gdb-edit-value-handler (value)
1280 (goto-char (point-min))
1281 (if (re-search-forward gdb-error-regexp nil t
)
1282 (message-box "Invalid number or expression (%s)" value
)))
1284 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1285 (defun gdb-var-update ()
1286 (gdb-input "-var-update --all-values *"
1287 'gdb-var-update-handler
1290 (defun gdb-var-update-handler ()
1291 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist
)))
1292 (dolist (var gdb-var-list
)
1293 (setcar (nthcdr 5 var
) nil
))
1294 (let ((temp-var-list gdb-var-list
))
1295 (dolist (change changelist
)
1296 (let* ((varnum (bindat-get-field change
'name
))
1297 (var (assoc varnum gdb-var-list
))
1298 (new-num (bindat-get-field change
'new_num_children
)))
1300 (let ((scope (bindat-get-field change
'in_scope
))
1301 (has-more (bindat-get-field change
'has_more
)))
1302 (cond ((string-equal scope
"false")
1303 (if gdb-delete-out-of-scope
1304 (gdb-var-delete-1 var varnum
)
1305 (setcar (nthcdr 5 var
) 'out-of-scope
)))
1306 ((string-equal scope
"true")
1307 (setcar (nthcdr 6 var
) has-more
)
1308 (when (and (or (not has-more
)
1309 (string-equal has-more
"0"))
1311 (string-equal (nth 2 var
) "0"))
1312 (setcar (nthcdr 4 var
)
1313 (bindat-get-field change
'value
))
1314 (setcar (nthcdr 5 var
) 'changed
)))
1315 ((string-equal scope
"invalid")
1316 (gdb-var-delete-1 var varnum
)))))
1317 (let ((var-list nil
) var1
1318 (children (bindat-get-field change
'new_children
)))
1320 (setq var1
(pop temp-var-list
))
1322 (if (string-equal varnum
(car var1
))
1323 (let ((new (string-to-number new-num
))
1324 (previous (string-to-number (nth 2 var1
))))
1325 (setcar (nthcdr 2 var1
) new-num
)
1326 (push var1 var-list
)
1329 ;; Add new children to list.
1330 (dotimes (_ previous
)
1331 (push (pop temp-var-list
) var-list
))
1332 (dolist (child children
)
1334 (list (bindat-get-field child
'name
)
1335 (bindat-get-field child
'exp
)
1336 (bindat-get-field child
'numchild
)
1337 (bindat-get-field child
'type
)
1338 (bindat-get-field child
'value
)
1340 (bindat-get-field child
'has_more
))))
1341 (push varchild var-list
))))
1342 ;; Remove deleted children from list.
1345 (push (pop temp-var-list
) var-list
))
1346 (dotimes (_ (- previous new
))
1347 (pop temp-var-list
)))))
1348 (push var1 var-list
))
1349 (setq var1
(pop temp-var-list
)))
1350 (setq gdb-var-list
(nreverse var-list
))))))))
1351 (gdb-speedbar-update))
1353 (defun gdb-speedbar-expand-node (text token indent
)
1354 "Expand the node the user clicked on.
1355 TEXT is the text of the button we clicked on, a + or - item.
1356 TOKEN is data related to this node.
1357 INDENT is the current indentation depth."
1358 (cond ((string-match "+" text
) ;expand this node
1359 (let* ((var (assoc token gdb-var-list
))
1360 (expr (nth 1 var
)) (children (nth 2 var
)))
1361 (if (or (<= (string-to-number children
) gdb-max-children
)
1363 (format "%s has %s children. Continue? " expr children
)))
1364 (gdb-var-list-children token
))))
1365 ((string-match "-" text
) ;contract this node
1366 (dolist (var gdb-var-list
)
1367 (if (string-match (concat token
"\\.") (car var
))
1368 (setq gdb-var-list
(delq var gdb-var-list
))))
1369 (gdb-var-delete-children token
)
1370 (speedbar-change-expand-button-char ?
+)
1371 (speedbar-delete-subblock indent
))
1372 (t (error "Ooops... not sure what to do")))
1373 (speedbar-center-buffer-smartly))
1375 (defun gdb-get-target-string ()
1376 (with-current-buffer gud-comint-buffer
1383 ;; Each buffer has a TYPE -- a symbol that identifies the function
1384 ;; of that particular buffer.
1386 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1387 ;; is constructed specially.
1389 ;; Others are constructed by gdb-get-buffer-create and
1390 ;; named according to the rules set forth in the gdb-buffer-rules
1392 (defvar gdb-buffer-rules
'())
1394 (defun gdb-rules-name-maker (rules-entry)
1396 (defun gdb-rules-buffer-mode (rules-entry)
1397 (nth 2 rules-entry
))
1398 (defun gdb-rules-update-trigger (rules-entry)
1399 (nth 3 rules-entry
))
1401 (defun gdb-update-buffer-name ()
1402 "Rename current buffer according to name-maker associated with
1403 it in `gdb-buffer-rules'."
1404 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1405 gdb-buffer-rules
))))
1406 (when f
(rename-buffer (funcall f
)))))
1408 (defun gdb-current-buffer-rules ()
1409 "Get `gdb-buffer-rules' entry for current buffer type."
1410 (assoc gdb-buffer-type gdb-buffer-rules
))
1412 (defun gdb-current-buffer-thread ()
1413 "Get thread object of current buffer from `gdb-threads-list'.
1415 When current buffer is not bound to any thread, return main
1417 (cdr (assoc gdb-thread-number gdb-threads-list
)))
1419 (defun gdb-current-buffer-frame ()
1420 "Get current stack frame object for thread of current buffer."
1421 (bindat-get-field (gdb-current-buffer-thread) 'frame
))
1423 (defun gdb-buffer-type (buffer)
1424 "Get value of `gdb-buffer-type' for BUFFER."
1425 (with-current-buffer buffer
1428 (defun gdb-buffer-shows-main-thread-p ()
1429 "Return t if current GDB buffer shows main selected thread and
1430 is not bound to it."
1432 (not (local-variable-p 'gdb-thread-number
)))
1434 (defun gdb-get-buffer (buffer-type &optional thread
)
1435 "Get a specific GDB buffer.
1437 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1438 and `gdb-thread-number' (if provided) must be equal to THREAD."
1440 (dolist (buffer (buffer-list) nil
)
1441 (with-current-buffer buffer
1442 (when (and (eq gdb-buffer-type buffer-type
)
1444 (equal gdb-thread-number thread
)))
1445 (throw 'found buffer
))))))
1447 (defun gdb-get-buffer-create (buffer-type &optional thread
)
1448 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1449 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1451 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1452 buffer-local variable of the new buffer.
1454 Buffer mode and name are selected according to buffer type.
1456 If buffer has trigger associated with it in `gdb-buffer-rules',
1457 this trigger is subscribed to `gdb-buf-publisher' and called with
1459 (or (gdb-get-buffer buffer-type thread
)
1460 (let ((rules (assoc buffer-type gdb-buffer-rules
))
1461 (new (generate-new-buffer "limbo")))
1462 (with-current-buffer new
1463 (let ((mode (gdb-rules-buffer-mode rules
))
1464 (trigger (gdb-rules-update-trigger rules
)))
1465 (when mode
(funcall mode
))
1466 (setq gdb-buffer-type buffer-type
)
1468 (set (make-local-variable 'gdb-thread-number
) thread
))
1469 (set (make-local-variable 'gud-minor-mode
)
1470 (buffer-local-value 'gud-minor-mode gud-comint-buffer
))
1471 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1472 (rename-buffer (funcall (gdb-rules-name-maker rules
)))
1474 (gdb-add-subscriber gdb-buf-publisher
1475 (cons (current-buffer)
1476 (gdb-bind-function-to-buffer
1477 trigger
(current-buffer))))
1478 (funcall trigger
'start
))
1479 (current-buffer))))))
1481 (defun gdb-bind-function-to-buffer (expr buffer
)
1482 "Return a function which will evaluate EXPR in BUFFER."
1483 `(lambda (&rest args
)
1484 (with-current-buffer ,buffer
1485 (apply ',expr args
))))
1487 ;; Used to display windows with thread-bound buffers
1488 (defmacro def-gdb-preempt-display-buffer
(name buffer
&optional doc
1490 `(defun ,name
(&optional thread
)
1492 (message "%s" thread
)
1493 (gdb-preempt-existing-or-display-buffer
1494 (gdb-get-buffer-create ,buffer thread
)
1495 ,split-horizontal
)))
1497 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1498 ;; at least one and possible more functions. The functions have these
1499 ;; roles in defining a buffer type:
1501 ;; NAME - Return a name for this buffer type.
1503 ;; The remaining function(s) are optional:
1505 ;; MODE - called in a new buffer with no arguments, should establish
1506 ;; the proper mode for the buffer.
1509 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
1510 (let ((binding (assoc buffer-type gdb-buffer-rules
)))
1512 (setcdr binding rules
)
1513 (push (cons buffer-type rules
)
1514 gdb-buffer-rules
))))
1516 (defun gdb-parent-mode ()
1517 "Generic mode to derive all other GDB buffer modes from."
1518 (kill-all-local-variables)
1519 (setq buffer-read-only t
)
1520 (buffer-disable-undo)
1521 ;; Delete buffer from gdb-buf-publisher when it's killed
1522 ;; (if it has an associated update trigger)
1527 (let ((trigger (gdb-rules-update-trigger
1528 (gdb-current-buffer-rules))))
1530 (gdb-delete-subscriber
1532 ;; This should match gdb-add-subscriber done in
1533 ;; gdb-get-buffer-create
1534 (cons (current-buffer)
1535 (gdb-bind-function-to-buffer trigger
(current-buffer))))))))
1538 ;; Partial-output buffer : This accumulates output from a command executed on
1539 ;; behalf of emacs (rather than the user).
1541 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1542 'gdb-partial-output-name
)
1544 (defun gdb-partial-output-name ()
1545 (concat " *partial-output-"
1546 (gdb-get-target-string)
1550 (gdb-set-buffer-rules 'gdb-inferior-io
1551 'gdb-inferior-io-name
1552 'gdb-inferior-io-mode
)
1554 (defun gdb-inferior-io-name ()
1555 (concat "*input/output of "
1556 (gdb-get-target-string)
1559 (defun gdb-display-io-buffer ()
1560 "Display IO of debugged program in a separate window."
1562 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))
1564 (defun gdb-inferior-io--init-proc (proc)
1565 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1566 (set-process-filter proc
'gdb-inferior-filter
)
1567 (set-process-sentinel proc
'gdb-inferior-io-sentinel
)
1568 ;; The process can run on a remote host.
1569 (let ((tty (or (process-get proc
'remote-tty
)
1570 (process-tty-name proc
))))
1571 (unless (or (null tty
)
1574 (concat "-inferior-tty-set " tty
) 'ignore
))))
1576 (defun gdb-inferior-io-sentinel (proc _str
)
1577 (when (eq (process-status proc
) 'failed
)
1578 ;; When the debugged process exits, Emacs gets an EIO error on
1579 ;; read from the pty, and stops listening to it. If the gdb
1580 ;; process is still running, remove the pty, make a new one, and
1582 (let ((gdb-proc (get-buffer-process gud-comint-buffer
))
1583 (io-buffer (process-buffer proc
)))
1584 (when (and gdb-proc
(process-live-p gdb-proc
)
1585 (buffer-live-p io-buffer
))
1586 ;; `comint-exec' deletes the original process as a side effect.
1587 (comint-exec io-buffer
"gdb-inferior" nil nil nil
)
1588 (gdb-inferior-io--init-proc (get-buffer-process io-buffer
))))))
1590 (defcustom gdb-display-buffer-other-frame-action
1591 '((display-buffer-reuse-window display-buffer-pop-up-frame
)
1592 (reusable-frames . visible
)
1593 (inhibit-same-window . t
)
1594 (pop-up-frame-parameters (height .
14)
1597 (tool-bar-lines . nil
)
1598 (menu-bar-lines . nil
)
1599 (minibuffer . nil
)))
1600 "`display-buffer' action for displaying GDB utility frames."
1602 :type display-buffer--action-custom-type
1606 (defun gdb-frame-io-buffer ()
1607 "Display IO of debugged program in another frame."
1609 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1610 gdb-display-buffer-other-frame-action
))
1612 (defvar gdb-inferior-io-mode-map
1613 (let ((map (make-sparse-keymap)))
1614 (define-key map
"\C-c\C-c" 'gdb-io-interrupt
)
1615 (define-key map
"\C-c\C-z" 'gdb-io-stop
)
1616 (define-key map
"\C-c\C-\\" 'gdb-io-quit
)
1617 (define-key map
"\C-c\C-d" 'gdb-io-eof
)
1618 (define-key map
"\C-d" 'gdb-io-eof
)
1621 ;; We want to use comint because it has various nifty and familiar features.
1622 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
1623 "Major mode for gdb inferior-io."
1624 :syntax-table nil
:abbrev-table nil
1625 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil
))
1627 (defun gdb-inferior-filter (proc string
)
1628 (unless (string-equal string
"")
1629 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))
1630 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1631 (comint-output-filter proc string
)))
1633 (defun gdb-io-interrupt ()
1634 "Interrupt the program being debugged."
1637 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1639 (defun gdb-io-quit ()
1640 "Send quit signal to the program being debugged."
1643 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1645 (defun gdb-io-stop ()
1646 "Stop the program being debugged."
1649 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1651 (defun gdb-io-eof ()
1652 "Send end-of-file to the program being debugged."
1655 (get-buffer-process gud-comint-buffer
)))
1657 (defun gdb-clear-inferior-io ()
1658 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1662 (defconst breakpoint-xpm-data
1664 static char *magick[] = {
1665 /* columns rows colors chars-per-pixel */
1681 "XPM data used for breakpoint icon.")
1683 (defconst breakpoint-enabled-pbm-data
1686 0 0 0 0 1 1 1 1 0 0 0 0
1687 0 0 0 1 1 1 1 1 1 0 0 0
1688 0 0 1 1 1 1 1 1 1 1 0 0
1689 0 1 1 1 1 1 1 1 1 1 1 0
1690 0 1 1 1 1 1 1 1 1 1 1 0
1691 0 1 1 1 1 1 1 1 1 1 1 0
1692 0 1 1 1 1 1 1 1 1 1 1 0
1693 0 0 1 1 1 1 1 1 1 1 0 0
1694 0 0 0 1 1 1 1 1 1 0 0 0
1695 0 0 0 0 1 1 1 1 0 0 0 0"
1696 "PBM data used for enabled breakpoint icon.")
1698 (defconst breakpoint-disabled-pbm-data
1710 0 0 0 1 0 1 0 1 0 0"
1711 "PBM data used for disabled breakpoint icon.")
1713 (defvar breakpoint-enabled-icon nil
1714 "Icon for enabled breakpoint in display margin.")
1716 (defvar breakpoint-disabled-icon nil
1717 "Icon for disabled breakpoint in display margin.")
1719 (declare-function define-fringe-bitmap
"fringe.c"
1720 (bitmap bits
&optional height width align
))
1722 (and (display-images-p)
1723 ;; Bitmap for breakpoint in fringe
1724 (define-fringe-bitmap 'breakpoint
1725 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1726 ;; Bitmap for gud-overlay-arrow in fringe
1727 (define-fringe-bitmap 'hollow-right-triangle
1728 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1730 (defface breakpoint-enabled
1734 "Face for enabled breakpoint icon in fringe."
1737 (defface breakpoint-disabled
1738 '((((class color
) (min-colors 88)) :foreground
"grey70")
1739 ;; Ensure that on low-color displays that we end up something visible.
1740 (((class color
) (min-colors 8) (background light
))
1741 :foreground
"black")
1742 (((class color
) (min-colors 8) (background dark
))
1743 :foreground
"white")
1744 (((type tty
) (class mono
))
1746 (t :background
"gray"))
1747 "Face for disabled breakpoint icon in fringe."
1751 (defvar gdb-control-commands-regexp
1754 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1755 "while-stepping\\|stepping\\|ws\\|actions"
1756 "\\)\\([[:blank:]]+.*\\)?$")
1757 "Regexp matching GDB commands that enter a recursive reading loop.
1758 As long as GDB is in the recursive reading loop, it does not expect
1759 commands to be prefixed by \"-interpreter-exec console\".")
1761 (defun gdb-strip-string-backslash (string)
1762 (replace-regexp-in-string "\\\\$" "" string
))
1764 (defun gdb-send (proc string
)
1765 "A comint send filter for gdb."
1766 (with-current-buffer gud-comint-buffer
1767 (let ((inhibit-read-only t
))
1768 (remove-text-properties (point-min) (point-max) '(face))))
1769 ;; mimic <RET> key to repeat previous command in GDB
1770 (if (not (string= "" string
))
1771 (if gdb-continuation
1772 (setq gdb-last-command
(concat gdb-continuation
1773 (gdb-strip-string-backslash string
)
1775 (setq gdb-last-command
(gdb-strip-string-backslash string
)))
1776 (if gdb-last-command
(setq string gdb-last-command
))
1777 (setq gdb-continuation nil
))
1778 (if (and (not gdb-continuation
) (or (string-match "^-" string
)
1779 (> gdb-control-level
0)))
1780 ;; Either MI command or we are feeding GDB's recursive reading loop.
1782 (setq gdb-first-done-or-error t
)
1783 (process-send-string proc
(concat string
"\n"))
1784 (if (and (string-match "^end$" string
)
1785 (> gdb-control-level
0))
1786 (setq gdb-control-level
(1- gdb-control-level
))))
1788 (if (string-match "\\\\$" string
)
1789 (setq gdb-continuation
1790 (concat gdb-continuation
(gdb-strip-string-backslash
1793 (setq gdb-first-done-or-error t
)
1794 (let ((to-send (concat "-interpreter-exec console "
1795 (gdb-mi-quote (concat gdb-continuation string
" "))
1797 (if gdb-enable-debug
1798 (push (cons 'mi-send to-send
) gdb-debug-log
))
1799 (process-send-string proc to-send
))
1800 (if (and (string-match "^end$" string
)
1801 (> gdb-control-level
0))
1802 (setq gdb-control-level
(1- gdb-control-level
)))
1803 (setq gdb-continuation nil
)))
1804 (if (string-match gdb-control-commands-regexp string
)
1805 (setq gdb-control-level
(1+ gdb-control-level
))))
1807 (defun gdb-mi-quote (string)
1808 "Return STRING quoted properly as an MI argument.
1809 The string is enclosed in double quotes.
1810 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1811 (setq string
(replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string
))
1812 (setq string
(replace-regexp-in-string "\n" "\\n" string t t
))
1813 (concat "\"" string
"\""))
1815 (defun gdb-input (command handler-function
&optional trigger-name
)
1816 "Send COMMAND to GDB via the MI interface.
1817 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1818 complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
1819 Emacs is still waiting for a reply from another command previously
1820 sent with the same TRIGGER-NAME."
1821 (when (or (not trigger-name
)
1822 (not (gdb-pending-handler-p trigger-name
)))
1823 (setq gdb-token-number
(1+ gdb-token-number
))
1824 (setq command
(concat (number-to-string gdb-token-number
) command
))
1826 (if gdb-enable-debug
(push (list 'send-item command handler-function
)
1829 (gdb-add-handler gdb-token-number handler-function trigger-name
)
1831 (if gdbmi-debug-mode
(message "gdb-input: %s" command
))
1832 (process-send-string (get-buffer-process gud-comint-buffer
)
1833 (concat command
"\n"))))
1835 ;; NOFRAME is used for gud execution control commands
1836 (defun gdb-current-context-command (command)
1837 "Add --thread to gdb COMMAND when needed."
1838 (if (and gdb-thread-number
1839 gdb-supports-non-stop
)
1840 (concat command
" --thread " gdb-thread-number
)
1843 (defun gdb-current-context-buffer-name (name)
1844 "Add thread information and asterisks to string NAME.
1846 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1848 (if (local-variable-p 'gdb-thread-number
)
1849 (format " (bound to thread %s)" gdb-thread-number
)
1853 (defun gdb-current-context-mode-name (mode)
1854 "Add thread information to MODE which is to be used as `mode-name'."
1856 (if gdb-thread-number
1857 (format " [thread %s]" gdb-thread-number
)
1861 (defcustom gud-gdb-command-name
"gdb -i=mi"
1862 "Default command to execute an executable under the GDB debugger."
1867 (setq gud-running nil
)
1868 (setq gdb-output-sink
'user
)
1869 (gdb-remove-all-pending-triggers))
1871 (defun gdb-update (&optional no-proc
)
1872 "Update buffers showing status of debug session.
1873 If NO-PROC is non-nil, do not try to contact the GDB process."
1874 (when gdb-first-prompt
1875 (gdb-force-mode-line-update
1876 (propertize "initializing..." 'face font-lock-variable-name-face
))
1878 (setq gdb-first-prompt nil
))
1881 (gdb-get-main-selected-frame))
1883 ;; We may need to update gdb-threads-list so we can use
1884 (gdb-get-buffer-create 'gdb-threads-buffer
)
1885 ;; gdb-break-list is maintained in breakpoints handler
1886 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1889 (gdb-emit-signal gdb-buf-publisher
'update
))
1891 (gdb-get-changed-registers)
1892 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1893 (dolist (var gdb-var-list
)
1894 (setcar (nthcdr 5 var
) nil
))
1897 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1898 ;; because we may need to update current gud-running value without
1899 ;; changing current thread (see gdb-running)
1900 (defun gdb-setq-thread-number (number)
1901 "Set `gdb-thread-number' to NUMBER.
1902 Only this function must be used to change `gdb-thread-number'
1903 value to NUMBER, because `gud-running' and `gdb-frame-number'
1904 need to be updated appropriately when current thread changes."
1905 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1906 (unless (string-equal number
"0") (setq gdb-thread-number number
))
1907 (setq gdb-frame-number
"0")
1908 (gdb-update-gud-running))
1910 (defun gdb-update-gud-running ()
1911 "Set `gud-running' according to the state of current thread.
1913 `gdb-frame-number' is set to 0 if current thread is now stopped.
1915 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1916 cannot be reliably used to determine whether or not execution
1917 control buttons should be shown in menu or toolbar. Use
1918 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1921 For all-stop mode, thread information is unavailable while target
1923 (let ((old-value gud-running
))
1925 (string= (bindat-get-field (gdb-current-buffer-thread) 'state
)
1927 ;; Set frame number to "0" when _current_ threads stops.
1928 (when (and (gdb-current-buffer-thread)
1929 (not (eq gud-running old-value
)))
1930 (setq gdb-frame-number
"0"))))
1932 (defun gdb-show-run-p ()
1933 "Return t if \"Run/continue\" should be shown on the toolbar."
1934 (or (not gdb-active-process
)
1936 (not gdb-gud-control-all-threads
)
1939 (and gdb-gud-control-all-threads
1940 (> gdb-stopped-threads-count
0))))
1942 (defun gdb-show-stop-p ()
1943 "Return t if \"Stop\" should be shown on the toolbar."
1945 (not gdb-gud-control-all-threads
)
1948 (and gdb-gud-control-all-threads
1949 (> gdb-running-threads-count
0))))
1951 ;; GUD displays the selected GDB frame. This might might not be the current
1952 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1953 ;; visited breakpoint is, use that window.
1954 (defun gdb-display-source-buffer (buffer)
1955 (let* ((last-window (if gud-last-last-frame
1957 (gud-find-file (car gud-last-last-frame
)))))
1958 (source-window (or last-window
1959 (if (and gdb-source-window
1960 (window-live-p gdb-source-window
))
1961 gdb-source-window
))))
1963 (setq gdb-source-window source-window
)
1964 (set-window-buffer source-window buffer
))
1968 (defun gdbmi-start-with (str offset match
)
1969 "Return non-nil if string STR starts with MATCH, else returns nil.
1970 OFFSET is the position in STR at which the comparison takes place."
1971 (let ((match-length (length match
))
1972 (str-length (- (length str
) offset
)))
1973 (when (>= str-length match-length
)
1974 (string-equal match
(substring str offset
(+ offset match-length
))))))
1976 (defun gdbmi-same-start (str offset match
)
1977 "Return non-nil iff STR and MATCH are equal up to the end of either strings.
1978 OFFSET is the position in STR at which the comparison takes place."
1979 (let* ((str-length (- (length str
) offset
))
1980 (match-length (length match
))
1981 (compare-length (min str-length match-length
)))
1982 (when (> compare-length
0)
1983 (string-equal (substring str offset
(+ offset compare-length
))
1984 (substring match
0 compare-length
)))))
1986 (defun gdbmi-is-number (character)
1987 "Return non-nil iff CHARACTER is a numerical character between 0 and 9."
1988 (and (>= character ?
0)
1992 (defvar-local gdbmi-bnf-state
'gdbmi-bnf-output
1993 "Current GDB/MI output parser state.
1994 The parser is placed in a different state when an incomplete data steam is
1996 This variable will preserve the state required to resume the parsing
1997 when more data arrives.")
1999 (defvar-local gdbmi-bnf-offset
0
2000 "Offset in `gud-marker-acc' at which the parser is reading.
2001 This offset is used to be able to parse the GDB/MI message
2002 in-place, without the need of copying the string in a temporary buffer
2003 or discarding parsed tokens by substringing the message.")
2005 (defun gdbmi-bnf-init ()
2006 "Initialize the GDB/MI message parser."
2007 (setq gdbmi-bnf-state
'gdbmi-bnf-output
)
2008 (setq gdbmi-bnf-offset
0)
2009 (setq gud-marker-acc
""))
2012 (defun gdbmi-bnf-output ()
2013 "Implementation of the following GDB/MI output grammar rule:
2016 ( out-of-band-record )* [ result-record ] gdb-prompt"
2018 (gdbmi-bnf-skip-unrecognized)
2019 (while (gdbmi-bnf-out-of-band-record))
2020 (gdbmi-bnf-result-record)
2021 (gdbmi-bnf-gdb-prompt))
2024 (defun gdbmi-bnf-skip-unrecognized ()
2025 "Skip characters until is encounters the beginning of a valid record.
2026 Used as a protection mechanism in case something goes wrong when parsing
2027 a GDB/MI reply message."
2028 (let ((acc-length (length gud-marker-acc
))
2029 (prefix-offset gdbmi-bnf-offset
)
2030 (prompt "(gdb) \n"))
2032 (while (and (< prefix-offset acc-length
)
2033 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2034 (setq prefix-offset
(1+ prefix-offset
)))
2036 (if (and (< prefix-offset acc-length
)
2037 (not (memq (aref gud-marker-acc prefix-offset
)
2038 '(?^ ?
* ?
+ ?
= ?~ ?
@ ?
&)))
2039 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt
))
2040 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
2042 (let ((unrecognized-str (match-string 0 gud-marker-acc
)))
2043 (setq gdbmi-bnf-offset
(match-end 0))
2044 (if gdbmi-debug-mode
2045 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str
))
2046 (gdb-shell unrecognized-str
)
2050 (defun gdbmi-bnf-gdb-prompt ()
2051 "Implementation of the following GDB/MI output grammar rule:
2058 (let ((prompt "(gdb) \n"))
2059 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt
)
2060 (if gdbmi-debug-mode
(message "gdbmi-bnf-gdb-prompt: %s" prompt
))
2062 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length prompt
)))
2064 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
2065 ;; the end of a GDB reply message.
2069 (defun gdbmi-bnf-result-record ()
2070 "Implementation of the following GDB/MI output grammar rule:
2073 [ token ] '^' result-class ( ',' result )* nl
2076 any sequence of digits."
2078 (gdbmi-bnf-result-and-async-record-impl))
2081 (defun gdbmi-bnf-out-of-band-record ()
2082 "Implementation of the following GDB/MI output grammar rule:
2084 out-of-band-record ==>
2085 async-record | stream-record"
2087 (or (gdbmi-bnf-async-record)
2088 (gdbmi-bnf-stream-record)))
2091 (defun gdbmi-bnf-async-record ()
2092 "Implementation of the following GDB/MI output grammar rules:
2095 exec-async-output | status-async-output | notify-async-output
2097 exec-async-output ==>
2098 [ token ] '*' async-output
2100 status-async-output ==>
2101 [ token ] '+' async-output
2103 notify-async-output ==>
2104 [ token ] '=' async-output
2107 async-class ( ',' result )* nl"
2109 (gdbmi-bnf-result-and-async-record-impl))
2112 (defun gdbmi-bnf-stream-record ()
2113 "Implement the following GDB/MI output grammar rule:
2115 console-stream-output | target-stream-output | log-stream-output
2117 console-stream-output ==>
2120 target-stream-output ==>
2123 log-stream-output ==>
2125 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2126 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset
) '(?~ ?
@ ?
&))
2127 (string-match "\\([~@&]\\)\\(\".*?\"\\)\n" gud-marker-acc
2129 (let ((prefix (match-string 1 gud-marker-acc
))
2130 (c-string (match-string 2 gud-marker-acc
)))
2132 (setq gdbmi-bnf-offset
(match-end 0))
2133 (if gdbmi-debug-mode
(message "gdbmi-bnf-stream-record: %s"
2134 (match-string 0 gud-marker-acc
)))
2136 (cond ((string-equal prefix
"~")
2137 (gdbmi-bnf-console-stream-output c-string
))
2138 ((string-equal prefix
"@")
2139 (gdbmi-bnf-target-stream-output c-string
))
2140 ((string-equal prefix
"&")
2141 (gdbmi-bnf-log-stream-output c-string
)))
2144 (defun gdbmi-bnf-console-stream-output (c-string)
2145 "Handler for the console-stream-output GDB/MI output grammar rule."
2146 (gdb-console c-string
))
2148 (defun gdbmi-bnf-target-stream-output (_c-string)
2149 "Handler for the target-stream-output GDB/MI output grammar rule."
2150 ;; Not currently used.
2153 (defun gdbmi-bnf-log-stream-output (c-string)
2154 "Handler for the log-stream-output GDB/MI output grammar rule."
2155 ;; Suppress "No registers." GDB 6.8 and earlier
2156 ;; duplicates MI error message on internal stream.
2157 ;; Don't print to GUD buffer.
2158 (if (not (string-equal (read c-string
) "No registers.\n"))
2159 (gdb-internals c-string
)))
2162 (defconst gdbmi-bnf-result-state-configs
2163 '(("^" .
(("done" .
(gdb-done . progressive
))
2164 ("error" .
(gdb-error . progressive
))
2165 ("running" .
(gdb-starting . atomic
))))
2166 ("*" .
(("stopped" .
(gdb-stopped . atomic
))
2167 ("running" .
(gdb-running . atomic
))))
2169 ("=" .
(("thread-created" .
(gdb-thread-created . atomic
))
2170 ("thread-selected" .
(gdb-thread-selected . atomic
))
2171 ("thread-existed" .
(gdb-ignored-notification . atomic
))
2172 ('default .
(gdb-ignored-notification . atomic
)))))
2173 "Alist of alists, mapping the type and class of message to a handler function.
2174 Handler functions are all flagged as either `progressive' or `atomic'.
2175 `progressive' handlers are capable of parsing incomplete messages.
2176 They can be called several time with new data chunk as they arrive from GDB.
2177 `progressive' handlers must have an extra argument that is set to a non-nil
2178 value when the message is complete.
2180 Implement the following GDB/MI output grammar rule:
2182 'done' | 'running' | 'connected' | 'error' | 'exit'
2185 'stopped' | others (where others will be added depending on the needs
2186 --this is still in development).")
2188 (defun gdbmi-bnf-result-and-async-record-impl ()
2189 "Common implementation of the result-record and async-record rule.
2190 Both rules share the same syntax. Those records may be very large in size.
2191 For that reason, the \"result\" part of the record is parsed by
2192 `gdbmi-bnf-incomplete-record-result', which will keep
2193 receiving characters as they arrive from GDB until the record is complete."
2194 (let ((acc-length (length gud-marker-acc
))
2195 (prefix-offset gdbmi-bnf-offset
))
2197 (while (and (< prefix-offset acc-length
)
2198 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2199 (setq prefix-offset
(1+ prefix-offset
)))
2201 (if (and (< prefix-offset acc-length
)
2202 (member (aref gud-marker-acc prefix-offset
) '(?
* ?
+ ?
= ?^
))
2203 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2204 gud-marker-acc gdbmi-bnf-offset
))
2206 (let ((token (match-string 1 gud-marker-acc
))
2207 (prefix (match-string 2 gud-marker-acc
))
2208 (class (match-string 3 gud-marker-acc
))
2209 (complete (string-equal (match-string 4 gud-marker-acc
) "\n"))
2213 (setq gdbmi-bnf-offset
(match-end 0))
2214 (if gdbmi-debug-mode
(message "gdbmi-bnf-result-record: %s"
2215 (match-string 0 gud-marker-acc
)))
2218 (cdr (assoc prefix gdbmi-bnf-result-state-configs
)))
2219 (setq class-command
(cdr (assoc class class-alist
)))
2220 (if (null class-command
)
2221 (setq class-command
(cdr (assoc 'default class-alist
))))
2225 (if (equal (cdr class-command
) 'progressive
)
2226 (funcall (car class-command
) token
"" complete
)
2227 (funcall (car class-command
) token
"")))
2228 (setq gdbmi-bnf-state
2230 (gdbmi-bnf-incomplete-record-result token class-command
)))
2231 (funcall gdbmi-bnf-state
))
2234 (defun gdbmi-bnf-incomplete-record-result (token class-command
)
2235 "State of the parser used to progressively parse a result-record or async-record
2236 rule from an incomplete data stream. The parser will stay in this state until
2237 the end of the current result or async record is reached."
2238 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2239 ;; Search the data stream for the end of the current record:
2240 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset
))
2241 (is-progressive (equal (cdr class-command
) 'progressive
))
2242 (is-complete (not (null newline-pos
)))
2245 (when gdbmi-debug-mode
2246 (message "gdbmi-bnf-incomplete-record-result: %s"
2247 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
)))
2249 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2250 ;; be processed by the class-command handler:
2251 (when (or is-complete is-progressive
)
2253 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
))
2255 ;; Move gdbmi-bnf-offset past the end of the chunk.
2256 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length result-str
)))
2258 (setq gdbmi-bnf-offset
(1+ gdbmi-bnf-offset
))))
2260 ;; Update the parsing state before invoking the handler in class-command
2261 ;; to make sure it's not left in an invalid state if the handler was
2262 ;; to generate an error.
2264 (setq gdbmi-bnf-state
'gdbmi-bnf-output
))
2268 (funcall (car class-command
) token result-str is-complete
)
2270 (funcall (car class-command
) token result-str
))))
2273 ;; Incomplete gdb response: abort parsing until we receive more data.
2274 (if gdbmi-debug-mode
(message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2275 (throw 'gdbmi-incomplete-stream nil
))
2280 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2281 ; The handling of those rules is currently done by the handlers registered
2282 ; in gdbmi-bnf-result-state-configs
2285 ; variable "=" value
2291 ; const | tuple | list
2297 ; "{}" | "{" result ( "," result )* "}"
2300 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2303 (defun gud-gdbmi-marker-filter (string)
2304 "Filter GDB/MI output."
2306 ;; Record transactions if logging is enabled.
2307 (when gdb-enable-debug
2308 (push (cons 'recv string
) gdb-debug-log
)
2309 (if (and gdb-debug-log-max
2310 (> (length gdb-debug-log
) gdb-debug-log-max
))
2311 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
2313 ;; Recall the left over gud-marker-acc from last time.
2314 (setq gud-marker-acc
(concat gud-marker-acc string
))
2316 ;; Start accumulating output for the GUD buffer.
2317 (setq gdb-filter-output
"")
2319 (let ((acc-length (length gud-marker-acc
)))
2320 (catch 'gdbmi-incomplete-stream
2321 (while (and (< gdbmi-bnf-offset acc-length
)
2322 (funcall gdbmi-bnf-state
)))))
2324 (when (/= gdbmi-bnf-offset
0)
2325 (setq gud-marker-acc
(substring gud-marker-acc gdbmi-bnf-offset
))
2326 (setq gdbmi-bnf-offset
0))
2328 (when (and gdbmi-debug-mode
(> (length gud-marker-acc
) 0))
2329 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc
))
2333 (defun gdb-gdb (_output-field))
2335 (defun gdb-shell (output-field)
2336 (setq gdb-filter-output
2337 (concat output-field gdb-filter-output
)))
2339 (defun gdb-ignored-notification (_token _output-field
))
2341 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2342 (defun gdb-thread-created (_token _output-field
))
2343 (defun gdb-thread-exited (_token output-field
)
2344 "Handle =thread-exited async record.
2345 Unset `gdb-thread-number' if current thread exited and update threads list."
2346 (let* ((thread-id (bindat-get-field (gdb-json-string output-field
) 'id
)))
2347 (if (string= gdb-thread-number thread-id
)
2348 (gdb-setq-thread-number nil
))
2349 ;; When we continue current thread and it quickly exits,
2350 ;; the pending triggers in gdb-handler-list left after gdb-running
2351 ;; disallow us to properly call -thread-info without --thread option.
2352 ;; Thus we need to use gdb-wait-for-pending.
2353 (gdb-wait-for-pending
2354 (gdb-emit-signal gdb-buf-publisher
'update-threads
))))
2356 (defun gdb-thread-selected (_token output-field
)
2357 "Handler for =thread-selected MI output record.
2359 Sets `gdb-thread-number' to new id."
2360 (let* ((result (gdb-json-string output-field
))
2361 (thread-id (bindat-get-field result
'id
)))
2362 (gdb-setq-thread-number thread-id
)
2363 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
2364 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
2365 ;; as usually. Things happen to fast and second call (from
2366 ;; gdb-thread-selected handler) gets cut off by our beloved
2367 ;; pending triggers.
2368 ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
2369 ;; body will get executed when `gdb-handler-list' if free of
2370 ;; pending triggers.
2371 (gdb-wait-for-pending
2374 (defun gdb-running (_token output-field
)
2376 (bindat-get-field (gdb-json-string output-field
) 'thread-id
)))
2377 ;; We reset gdb-frame-number to nil if current thread has gone
2378 ;; running. This can't be done in gdb-thread-list-handler-custom
2379 ;; because we need correct gdb-frame-number by the time
2380 ;; -thread-info command is sent.
2381 (when (or (string-equal thread-id
"all")
2382 (string-equal thread-id gdb-thread-number
))
2383 (setq gdb-frame-number nil
)))
2384 (setq gdb-inferior-status
"running")
2385 (gdb-force-mode-line-update
2386 (propertize gdb-inferior-status
'face font-lock-type-face
))
2387 (when (not gdb-non-stop
)
2388 (setq gud-running t
))
2389 (setq gdb-active-process t
))
2391 (defun gdb-starting (_output-field _result
)
2392 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2393 (setq gdb-inferior-status
"running")
2394 (gdb-force-mode-line-update
2395 (propertize gdb-inferior-status
'face font-lock-type-face
))
2396 (setq gdb-active-process t
)
2397 (setq gud-running t
))
2399 ;; -break-insert -t didn't give a reason before gdb 6.9
2401 (defun gdb-stopped (_token output-field
)
2402 "Given the contents of *stopped MI async record, select new
2403 current thread and update GDB buffers."
2404 ;; Reason is available with target-async only
2405 (let* ((result (gdb-json-string output-field
))
2406 (reason (bindat-get-field result
'reason
))
2407 (thread-id (bindat-get-field result
'thread-id
)))
2409 ;; -data-list-register-names needs to be issued for any stopped
2411 (when (not gdb-register-names
)
2412 (gdb-input (concat "-data-list-register-names"
2413 (if gdb-supports-non-stop
2414 (concat " --thread " thread-id
)))
2415 'gdb-register-names-handler
))
2417 ;; Don't set gud-last-frame here as it's currently done in
2418 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2422 ;; ;; Extract the frame position from the marker.
2423 ;; gud-last-frame (cons file
2424 ;; (string-to-number
2425 ;; (match-string 6 gud-marker-acc)))))
2427 (setq gdb-inferior-status
(or reason
"unknown"))
2428 (gdb-force-mode-line-update
2429 (propertize gdb-inferior-status
'face font-lock-warning-face
))
2430 (if (string-equal reason
"exited-normally")
2431 (setq gdb-active-process nil
))
2433 ;; Select new current thread.
2435 ;; Don't switch if we have no reasons selected
2436 (when gdb-switch-reasons
2437 ;; Switch from another stopped thread only if we have
2438 ;; gdb-switch-when-another-stopped:
2439 (when (or gdb-switch-when-another-stopped
2440 (not (string= "stopped"
2441 (bindat-get-field (gdb-current-buffer-thread) 'state
))))
2442 ;; Switch if current reason has been selected or we have no
2444 (if (or (eq gdb-switch-reasons t
)
2445 (member reason gdb-switch-reasons
))
2446 (when (not (string-equal gdb-thread-number thread-id
))
2447 (message "Switched to thread %s" thread-id
)
2448 (gdb-setq-thread-number thread-id
))
2449 (message "Thread %s stopped" thread-id
))))
2451 ;; Print "(gdb)" to GUD console
2452 (when gdb-first-done-or-error
2453 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2455 ;; In non-stop, we update information as soon as another thread gets
2457 (when (or gdb-first-done-or-error
2459 ;; In all-stop this updates gud-running properly as well.
2461 (setq gdb-first-done-or-error nil
))
2462 (run-hook-with-args 'gdb-stopped-functions result
)))
2464 ;; Remove the trimmings from log stream containing debugging messages
2465 ;; being produced by GDB's internals, use warning face and send to GUD
2467 (defun gdb-internals (output-field)
2468 (setq gdb-filter-output
2471 (if (string= output-field
"\"\\n\"")
2473 (let ((error-message
2474 (read output-field
)))
2476 0 (length error-message
)
2477 'face font-lock-warning-face
2481 ;; Remove the trimmings from the console stream and send to GUD buffer
2482 ;; (frontend MI commands should not print to this stream)
2483 (defun gdb-console (output-field)
2484 (setq gdb-filter-output
2485 (gdb-concat-output gdb-filter-output
(read output-field
))))
2487 (defun gdb-done (token-number output-field is-complete
)
2488 (gdb-done-or-error token-number
'done output-field is-complete
))
2490 (defun gdb-error (token-number output-field is-complete
)
2491 (gdb-done-or-error token-number
'error output-field is-complete
))
2493 (defun gdb-done-or-error (token-number type output-field is-complete
)
2494 (if (string-equal token-number
"")
2495 ;; Output from command entered by user
2497 (setq gdb-output-sink
'user
)
2498 (setq token-number nil
)
2499 ;; MI error - send to minibuffer
2500 (when (eq type
'error
)
2501 ;; Skip "msg=" from `output-field'
2502 (message "%s" (read (substring output-field
4)))
2503 ;; Don't send to the console twice. (If it is a console error
2504 ;; it is also in the console stream.)
2505 (setq output-field nil
)))
2506 ;; Output from command from frontend.
2507 (setq gdb-output-sink
'emacs
))
2509 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2510 (let* ((proc (get-buffer-process gud-comint-buffer
))
2511 (no-proc (or (null proc
)
2512 (memq (process-status proc
) '(exit signal
)))))
2514 (when (and is-complete gdb-first-done-or-error
)
2515 (unless (or token-number gud-running no-proc
)
2516 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2517 (gdb-update no-proc
)
2518 (setq gdb-first-done-or-error nil
))
2520 (setq gdb-filter-output
2521 (gdb-concat-output gdb-filter-output output-field
))
2523 ;; We are done concatenating to the output sink. Restore it to user sink:
2524 (setq gdb-output-sink
'user
)
2526 (when (and token-number is-complete
)
2527 (with-current-buffer
2528 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2529 (gdb-handle-reply (string-to-number token-number
))))
2532 (gdb-clear-partial-output))))
2534 (defun gdb-concat-output (so-far new
)
2536 ((eq gdb-output-sink
'user
) (concat so-far new
))
2537 ((eq gdb-output-sink
'emacs
)
2538 (gdb-append-to-partial-output new
)
2541 (defun gdb-append-to-partial-output (string)
2542 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2543 (goto-char (point-max))
2546 (defun gdb-clear-partial-output ()
2547 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2550 (defun gdb-jsonify-buffer (&optional fix-key fix-list
)
2551 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2553 Field names are wrapped in double quotes and equal signs are
2554 replaced with semicolons.
2556 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2557 partial output. This is used to get rid of useless keys in lists
2558 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2559 -break-info are examples of MI commands which issue such
2562 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2563 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2564 -break-info output when it contains breakpoint script field
2565 incompatible with GDB/MI output syntax."
2567 (goto-char (point-min))
2570 (while (re-search-forward (concat "[\\[,]\\(" fix-key
"=\\)") nil t
)
2571 (replace-match "" nil nil nil
1))))
2574 ;; Find positions of braces which enclose broken list
2575 (while (re-search-forward (concat fix-list
"={\"") nil t
)
2576 (let ((p1 (goto-char (- (point) 2)))
2577 (p2 (progn (forward-sexp)
2579 ;; Replace braces with brackets
2587 (goto-char (point-min))
2589 (while (re-search-forward
2590 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t
)
2591 (replace-match "\"\\1\":\\2" nil nil
))
2592 (goto-char (point-max))
2595 (defun gdb-json-read-buffer (&optional fix-key fix-list
)
2596 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2598 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2599 (gdb-jsonify-buffer fix-key fix-list
)
2601 (goto-char (point-min))
2602 (let ((json-array-type 'list
))
2605 (defun gdb-json-string (string &optional fix-key fix-list
)
2606 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2608 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2611 (gdb-json-read-buffer fix-key fix-list
)))
2613 (defun gdb-json-partial-output (&optional fix-key fix-list
)
2614 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2616 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2617 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2618 (gdb-json-read-buffer fix-key fix-list
)))
2620 (defun gdb-line-posns (line)
2621 "Return a pair of LINE beginning and end positions."
2622 (let ((offset (1+ (- line
(line-number-at-pos)))))
2624 (line-beginning-position offset
)
2625 (line-end-position offset
))))
2627 (defmacro gdb-mark-line
(line variable
)
2628 "Set VARIABLE marker to point at beginning of LINE.
2630 If current window has no fringes, inverse colors on LINE.
2632 Return position where LINE begins."
2634 (let* ((posns (gdb-line-posns ,line
))
2635 (start-posn (car posns
))
2636 (end-posn (cdr posns
)))
2637 (set-marker ,variable
(copy-marker start-posn
))
2638 (when (not (> (car (window-fringes)) 0))
2639 (put-text-property start-posn end-posn
2640 'font-lock-face
'(:inverse-video t
)))
2643 (defun gdb-pad-string (string padding
)
2644 (format (concat "%" (number-to-string padding
) "s") string
))
2646 ;; gdb-table struct is a way to programmatically construct simple
2647 ;; tables. It help to reliably align columns of data in GDB buffers
2649 (cl-defstruct gdb-table
2652 (row-properties nil
)
2655 (defun gdb-table-add-row (table row
&optional properties
)
2656 "Add ROW of string to TABLE and recalculate column sizes.
2658 When non-nil, PROPERTIES will be added to the whole row when
2659 calling `gdb-table-string'."
2660 (let ((rows (gdb-table-rows table
))
2661 (row-properties (gdb-table-row-properties table
))
2662 (column-sizes (gdb-table-column-sizes table
))
2663 (right-align (gdb-table-right-align table
)))
2664 (when (not column-sizes
)
2665 (setf (gdb-table-column-sizes table
)
2666 (make-list (length row
) 0)))
2667 (setf (gdb-table-rows table
)
2668 (append rows
(list row
)))
2669 (setf (gdb-table-row-properties table
)
2670 (append row-properties
(list properties
)))
2671 (setf (gdb-table-column-sizes table
)
2672 (cl-mapcar (lambda (x s
)
2674 (max (abs x
) (string-width (or s
"")))))
2675 (if right-align new-x
(- new-x
))))
2676 (gdb-table-column-sizes table
)
2678 ;; Avoid trailing whitespace at eol
2679 (if (not (gdb-table-right-align table
))
2680 (setcar (last (gdb-table-column-sizes table
)) 0))))
2682 (defun gdb-table-string (table &optional sep
)
2683 "Return TABLE as a string with columns separated with SEP."
2684 (let ((column-sizes (gdb-table-column-sizes table
)))
2688 (lambda (row properties
)
2690 (mapconcat 'identity
2691 (cl-mapcar (lambda (s x
) (gdb-pad-string s x
))
2695 (gdb-table-rows table
)
2696 (gdb-table-row-properties table
))
2699 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2700 (defun gdb-get-many-fields (struct &rest fields
)
2701 "Return a list of FIELDS values from STRUCT."
2703 (dolist (field fields
)
2704 (push (bindat-get-field struct field
) values
))
2707 (defmacro def-gdb-auto-update-trigger
(trigger-name gdb-command
2709 &optional signal-list
)
2710 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2711 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2712 buffer with `gdb-bind-function-to-buffer'.
2714 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2715 defined trigger is called with an argument from SIGNAL-LIST. It's
2716 not recommended to define triggers with empty SIGNAL-LIST.
2717 Normally triggers should respond at least to 'update signal.
2719 Normally the trigger defined by this command must be called from
2720 the buffer where HANDLER-NAME must work. This should be done so
2721 that buffer-local thread number may be used in GDB-COMMAND (by
2722 calling `gdb-current-context-command').
2723 `gdb-bind-function-to-buffer' is used to achieve this, see
2724 `gdb-get-buffer-create'.
2726 Triggers defined by this command are meant to be used as a
2727 trigger argument when describing buffer types with
2728 `gdb-set-buffer-rules'."
2729 `(defun ,trigger-name
(&optional signal
)
2731 (or (not ,signal-list
)
2732 (memq signal
,signal-list
))
2733 (gdb-input ,gdb-command
2734 (gdb-bind-function-to-buffer ',handler-name
(current-buffer))
2735 (cons (current-buffer) ',trigger-name
)))))
2737 ;; Used by disassembly buffer only, the rest use
2738 ;; def-gdb-trigger-and-handler
2739 (defmacro def-gdb-auto-update-handler
(handler-name custom-defun
2740 &optional nopreserve
)
2741 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2743 Handlers are normally called from the buffers they put output in.
2745 Erase current buffer and evaluate CUSTOM-DEFUN.
2746 Then call `gdb-update-buffer-name'.
2748 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2749 `(defun ,handler-name
()
2750 (let* ((inhibit-read-only t
)
2751 ,@(unless nopreserve
2752 '((window (get-buffer-window (current-buffer) 0))
2753 (start (window-start window
))
2754 (p (window-point window
)))))
2757 (gdb-update-buffer-name)
2758 ,@(when (not nopreserve
)
2759 '((set-window-start window start
)
2760 (set-window-point window p
))))))
2762 (defmacro def-gdb-trigger-and-handler
(trigger-name gdb-command
2763 handler-name custom-defun
2764 &optional signal-list
)
2765 "Define trigger and handler.
2767 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2768 See `def-gdb-auto-update-trigger'.
2770 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2771 See `def-gdb-auto-update-handler'."
2773 (def-gdb-auto-update-trigger ,trigger-name
2775 ,handler-name
,signal-list
)
2776 (def-gdb-auto-update-handler ,handler-name
2781 ;; Breakpoint buffer : This displays the output of `-break-list'.
2782 (def-gdb-trigger-and-handler
2783 gdb-invalidate-breakpoints
"-break-list"
2784 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2787 (gdb-set-buffer-rules
2788 'gdb-breakpoints-buffer
2789 'gdb-breakpoints-buffer-name
2790 'gdb-breakpoints-mode
2791 'gdb-invalidate-breakpoints
)
2793 (defun gdb-breakpoints-list-handler-custom ()
2794 (let ((breakpoints-list (bindat-get-field
2795 (gdb-json-partial-output "bkpt" "script")
2796 'BreakpointTable
'body
))
2797 (table (make-gdb-table)))
2798 (setq gdb-breakpoints-list nil
)
2799 (gdb-table-add-row table
'("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2800 (dolist (breakpoint breakpoints-list
)
2801 (add-to-list 'gdb-breakpoints-list
2802 (cons (bindat-get-field breakpoint
'number
)
2804 (let ((at (bindat-get-field breakpoint
'at
))
2805 (pending (bindat-get-field breakpoint
'pending
))
2806 (func (bindat-get-field breakpoint
'func
))
2807 (type (bindat-get-field breakpoint
'type
)))
2808 (gdb-table-add-row table
2810 (bindat-get-field breakpoint
'number
)
2812 (or (bindat-get-field breakpoint
'disp
) "")
2813 (let ((flag (bindat-get-field breakpoint
'enabled
)))
2814 (if (string-equal flag
"y")
2815 (propertize "y" 'font-lock-face font-lock-warning-face
)
2816 (propertize "n" 'font-lock-face font-lock-comment-face
)))
2817 (bindat-get-field breakpoint
'addr
)
2818 (or (bindat-get-field breakpoint
'times
) "")
2819 (if (and type
(string-match ".*watchpoint" type
))
2820 (bindat-get-field breakpoint
'what
)
2823 (propertize (or func
"unknown")
2824 'font-lock-face font-lock-function-name-face
)
2825 (gdb-frame-location breakpoint
)))))
2826 ;; Add clickable properties only for breakpoints with file:line
2828 (append (list 'gdb-breakpoint breakpoint
)
2829 (when func
'(help-echo "mouse-2, RET: visit breakpoint"
2830 mouse-face highlight
))))))
2831 (insert (gdb-table-string table
" "))
2832 (gdb-place-breakpoints)))
2834 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2835 (defun gdb-place-breakpoints ()
2836 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2837 (dolist (buffer (buffer-list))
2838 (with-current-buffer buffer
2839 (if (and (eq gud-minor-mode
'gdbmi
)
2840 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2841 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2842 (dolist (breakpoint gdb-breakpoints-list
)
2843 (let* ((breakpoint (cdr breakpoint
)) ; gdb-breakpoints-list is
2844 ; an associative list
2845 (line (bindat-get-field breakpoint
'line
)))
2847 (let ((file (bindat-get-field breakpoint
'fullname
))
2848 (flag (bindat-get-field breakpoint
'enabled
))
2849 (bptno (bindat-get-field breakpoint
'number
)))
2850 (unless (and file
(file-exists-p file
))
2851 (setq file
(cdr (assoc bptno gdb-location-alist
))))
2853 (string-equal file
"File not found"))
2854 ;; If the full filename is not recorded in the
2855 ;; breakpoint structure or in `gdb-location-alist', use
2856 ;; -file-list-exec-source-file to extract it.
2857 (when (setq file
(bindat-get-field breakpoint
'file
))
2858 (gdb-input (concat "list " file
":1") 'ignore
)
2859 (gdb-input "-file-list-exec-source-file"
2860 `(lambda () (gdb-get-location
2861 ,bptno
,line
,flag
))))
2862 (with-current-buffer (find-file-noselect file
'nowarn
)
2864 ;; Only want one breakpoint icon at each location.
2865 (gdb-put-breakpoint-icon (string-equal flag
"y") bptno
2866 (string-to-number line
)))))))))
2868 (defvar gdb-source-file-regexp
"fullname=\"\\(.*?\\)\"")
2870 (defun gdb-get-location (bptno line flag
)
2871 "Find the directory containing the relevant source file.
2872 Put in buffer and place breakpoint icon."
2873 (goto-char (point-min))
2874 (catch 'file-not-found
2875 (if (re-search-forward gdb-source-file-regexp nil t
)
2876 (delete (cons bptno
"File not found") gdb-location-alist
)
2877 (push (cons bptno
(match-string 1)) gdb-location-alist
)
2879 (unless (assoc bptno gdb-location-alist
)
2880 (push (cons bptno
"File not found") gdb-location-alist
)
2881 (message-box "Cannot find source file for breakpoint location.
2882 Add directory to search path for source files using the GDB command, dir."))
2883 (throw 'file-not-found nil
))
2884 (with-current-buffer (find-file-noselect (match-string 1))
2886 ;; only want one breakpoint icon at each location
2887 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
(string-to-number line
)))))
2889 (add-hook 'find-file-hook
'gdb-find-file-hook
)
2891 (defun gdb-find-file-hook ()
2892 "Set up buffer for debugging if file is part of the source code
2893 of the current session."
2894 (if (and (buffer-name gud-comint-buffer
)
2895 ;; in case gud or gdb-ui is just loaded
2897 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
2899 (if (member buffer-file-name gdb-source-file-list
)
2900 (with-current-buffer (find-buffer-visiting buffer-file-name
)
2901 (gdb-init-buffer)))))
2903 (declare-function gud-remove
"gdb-mi" t t
) ; gud-def
2904 (declare-function gud-break
"gdb-mi" t t
) ; gud-def
2905 (declare-function fringe-bitmaps-at-pos
"fringe.c" (&optional pos window
))
2907 (defun gdb-mouse-set-clear-breakpoint (event)
2908 "Set/clear breakpoint in left fringe/margin at mouse click.
2909 If not in a source or disassembly buffer just set point."
2911 (mouse-minibuffer-check event
)
2912 (let ((posn (event-end event
)))
2913 (with-selected-window (posn-window posn
)
2914 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode
))
2915 (if (numberp (posn-point posn
))
2917 (goto-char (posn-point posn
))
2918 (if (or (posn-object posn
)
2919 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
2923 (posn-set-point posn
))))
2925 (defun gdb-mouse-toggle-breakpoint-margin (event)
2926 "Enable/disable breakpoint in left margin with mouse click."
2928 (mouse-minibuffer-check event
)
2929 (let ((posn (event-end event
)))
2930 (if (numberp (posn-point posn
))
2931 (with-selected-window (posn-window posn
)
2933 (goto-char (posn-point posn
))
2934 (if (posn-object posn
)
2936 (let ((bptno (get-text-property
2937 0 'gdb-bptno
(car (posn-string posn
)))))
2939 (if (get-text-property
2940 0 'gdb-enabled
(car (posn-string posn
)))
2945 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2946 "Enable/disable breakpoint in left fringe with mouse click."
2948 (mouse-minibuffer-check event
)
2949 (let* ((posn (event-end event
))
2950 (pos (posn-point posn
))
2953 (with-selected-window (posn-window posn
)
2954 (with-current-buffer (window-buffer)
2956 (dolist (overlay (overlays-in pos pos
))
2957 (when (overlay-get overlay
'put-break
)
2958 (setq obj
(overlay-get overlay
'before-string
))))
2962 (if (get-text-property 0 'gdb-enabled obj
)
2965 (get-text-property 0 'gdb-bptno obj
)))))))))
2967 (defun gdb-breakpoints-buffer-name ()
2968 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2970 (defun gdb-display-breakpoints-buffer (&optional thread
)
2971 "Display GDB breakpoints."
2973 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)))
2975 (defun gdb-frame-breakpoints-buffer (&optional thread
)
2976 "Display GDB breakpoints in another frame."
2978 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)
2979 gdb-display-buffer-other-frame-action
))
2981 (defvar gdb-breakpoints-mode-map
2982 (let ((map (make-sparse-keymap))
2983 (menu (make-sparse-keymap "Breakpoints")))
2984 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
2985 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2986 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2987 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2988 (suppress-keymap map)
2989 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2990 (define-key map " " 'gdb-toggle-breakpoint)
2991 (define-key map "D" 'gdb-delete-breakpoint)
2992 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2993 (define-key map "q" 'gdb-delete-frame-or-window)
2994 (define-key map "\r" 'gdb-goto-breakpoint)
2995 (define-key map "\t" (lambda ()
2997 (gdb-set-window-buffer
2998 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2999 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3000 (define-key map [follow-link] 'mouse-face)
3003 (defun gdb-delete-frame-or-window ()
3004 "Delete frame if there is only one window. Otherwise delete the window."
3006 (if (one-window-p) (delete-frame)
3009 ;;from make-mode-line-mouse-map
3010 (defun gdb-make-header-line-mouse-map (mouse function) "\
3011 Return a keymap with single entry for mouse key MOUSE on the header line.
3012 MOUSE is defined to run function FUNCTION with no args in the buffer
3013 corresponding to the mode line clicked."
3014 (let ((map (make-sparse-keymap)))
3015 (define-key map (vector 'header-line mouse) function)
3016 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3019 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3021 'help-echo ,help-echo
3022 'mouse-face ',mouse-face
3025 (gdb-make-header-line-mouse-map
3027 (lambda (event) (interactive "e")
3028 (save-selected-window
3029 (select-window (posn-window (event-start event)))
3030 (gdb-set-window-buffer
3031 (gdb-get-buffer-create ',buffer) t) )))))
3034 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3037 (defun gdb-threads-buffer-name ()
3038 (concat "*threads of " (gdb-get-target-string) "*"))
3040 (defun gdb-display-threads-buffer (&optional thread)
3041 "Display GDB threads."
3043 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3045 (defun gdb-frame-threads-buffer (&optional thread)
3046 "Display GDB threads in another frame."
3048 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3049 gdb-display-buffer-other-frame-action))
3051 (def-gdb-trigger-and-handler
3052 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3053 gdb-thread-list-handler gdb-thread-list-handler-custom
3054 '(start update update-threads))
3056 (gdb-set-buffer-rules
3058 'gdb-threads-buffer-name
3060 'gdb-invalidate-threads)
3062 (defvar gdb-threads-font-lock-keywords
3063 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3064 (" \\(stopped\\)" (1 font-lock-warning-face))
3065 (" \\(running\\)" (1 font-lock-string-face))
3066 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3067 "Font lock keywords used in `gdb-threads-mode'.")
3069 (defvar gdb-threads-mode-map
3070 (let ((map (make-sparse-keymap)))
3071 (define-key map "\r" 'gdb-select-thread)
3072 (define-key map "f" 'gdb-display-stack-for-thread)
3073 (define-key map "F" 'gdb-frame-stack-for-thread)
3074 (define-key map "l" 'gdb-display-locals-for-thread)
3075 (define-key map "L" 'gdb-frame-locals-for-thread)
3076 (define-key map "r" 'gdb-display-registers-for-thread)
3077 (define-key map "R" 'gdb-frame-registers-for-thread)
3078 (define-key map "d" 'gdb-display-disassembly-for-thread)
3079 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3080 (define-key map "i" 'gdb-interrupt-thread)
3081 (define-key map "c" 'gdb-continue-thread)
3082 (define-key map "s" 'gdb-step-thread)
3083 (define-key map "\t"
3086 (gdb-set-window-buffer
3087 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3088 (define-key map [mouse-2] 'gdb-select-thread)
3089 (define-key map [follow-link] 'mouse-face)
3092 (defvar gdb-threads-header
3094 (gdb-propertize-header
3095 "Breakpoints" gdb-breakpoints-buffer
3096 "mouse-1: select" mode-line-highlight mode-line-inactive)
3098 (gdb-propertize-header "Threads" gdb-threads-buffer
3099 nil nil mode-line)))
3101 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3102 "Major mode for GDB threads."
3103 (setq gdb-thread-position (make-marker))
3104 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3105 (setq header-line-format gdb-threads-header)
3106 (set (make-local-variable 'font-lock-defaults)
3107 '(gdb-threads-font-lock-keywords))
3108 'gdb-invalidate-threads)
3110 (defun gdb-thread-list-handler-custom ()
3111 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3112 (table (make-gdb-table))
3114 (setq gdb-threads-list nil)
3115 (setq gdb-running-threads-count 0)
3116 (setq gdb-stopped-threads-count 0)
3117 (set-marker gdb-thread-position nil)
3119 (dolist (thread (reverse threads-list))
3120 (let ((running (equal (bindat-get-field thread 'state) "running")))
3121 (add-to-list 'gdb-threads-list
3122 (cons (bindat-get-field thread 'id)
3124 (cl-incf (if running
3125 gdb-running-threads-count
3126 gdb-stopped-threads-count))
3131 (bindat-get-field thread 'id)
3133 (if gdb-thread-buffer-verbose-names
3134 (concat (bindat-get-field thread 'target-id) " ") "")
3135 (bindat-get-field thread 'state)
3136 ;; Include frame information for stopped threads
3139 " in " (bindat-get-field thread 'frame 'func)
3140 (if gdb-thread-buffer-arguments
3143 (let ((args (bindat-get-field thread 'frame 'args)))
3146 (apply #'format "%s=%s"
3147 (gdb-get-many-fields arg 'name 'value)))
3151 (if gdb-thread-buffer-locations
3152 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3153 (if gdb-thread-buffer-addresses
3154 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3158 'mouse-face 'highlight
3159 'help-echo "mouse-2, RET: select thread")))
3160 (when (string-equal gdb-thread-number
3161 (bindat-get-field thread 'id))
3162 (setq marked-line (length gdb-threads-list))))
3163 (insert (gdb-table-string table " "))
3165 (gdb-mark-line marked-line gdb-thread-position)))
3166 ;; We update gud-running here because we need to make sure that
3167 ;; gdb-threads-list is up-to-date
3168 (gdb-update-gud-running)
3169 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3171 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3172 "Define a NAME command which will act upon thread on the current line.
3174 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3175 be the value of 'gdb-thread property of the current line.
3176 If `gdb-thread' is nil, error is signaled."
3177 `(defun ,name (&optional event)
3179 (interactive (list last-input-event))
3180 (if event (posn-set-point (event-end event)))
3183 (let ((thread (get-text-property (point) 'gdb-thread)))
3186 (error "Not recognized as thread line"))))))
3188 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3190 "Define a NAME which will call BUFFER-COMMAND with id of thread
3191 on the current line."
3192 `(def-gdb-thread-buffer-command ,name
3193 (,buffer-command (bindat-get-field thread 'id))
3196 (def-gdb-thread-buffer-command gdb-select-thread
3197 (let ((new-id (bindat-get-field thread 'id)))
3198 (gdb-setq-thread-number new-id)
3199 (gdb-input (concat "-thread-select " new-id) 'ignore)
3201 "Select the thread at current line of threads buffer.")
3203 (def-gdb-thread-buffer-simple-command
3204 gdb-display-stack-for-thread
3205 gdb-preemptively-display-stack-buffer
3206 "Display stack buffer for the thread at current line.")
3208 (def-gdb-thread-buffer-simple-command
3209 gdb-display-locals-for-thread
3210 gdb-preemptively-display-locals-buffer
3211 "Display locals buffer for the thread at current line.")
3213 (def-gdb-thread-buffer-simple-command
3214 gdb-display-registers-for-thread
3215 gdb-preemptively-display-registers-buffer
3216 "Display registers buffer for the thread at current line.")
3218 (def-gdb-thread-buffer-simple-command
3219 gdb-display-disassembly-for-thread
3220 gdb-preemptively-display-disassembly-buffer
3221 "Display disassembly buffer for the thread at current line.")
3223 (def-gdb-thread-buffer-simple-command
3224 gdb-frame-stack-for-thread
3225 gdb-frame-stack-buffer
3226 "Display another frame with stack buffer for thread at current line.")
3228 (def-gdb-thread-buffer-simple-command
3229 gdb-frame-locals-for-thread
3230 gdb-frame-locals-buffer
3231 "Display another frame with locals buffer for thread at current line.")
3233 (def-gdb-thread-buffer-simple-command
3234 gdb-frame-registers-for-thread
3235 gdb-frame-registers-buffer
3236 "Display another frame with registers buffer for the thread at current line.")
3238 (def-gdb-thread-buffer-simple-command
3239 gdb-frame-disassembly-for-thread
3240 gdb-frame-disassembly-buffer
3241 "Display another frame with disassembly buffer for the thread at current line.")
3243 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3244 "Define a NAME which will execute GUD-COMMAND with
3245 `gdb-thread-number' locally bound to id of thread on the current
3247 `(def-gdb-thread-buffer-command ,name
3249 (let ((gdb-thread-number (bindat-get-field thread 'id))
3250 (gdb-gud-control-all-threads nil))
3251 (call-interactively #',gud-command))
3252 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3255 (def-gdb-thread-buffer-gud-command
3256 gdb-interrupt-thread
3258 "Interrupt thread at current line.")
3260 (def-gdb-thread-buffer-gud-command
3263 "Continue thread at current line.")
3265 (def-gdb-thread-buffer-gud-command
3268 "Step thread at current line.")
3273 (defcustom gdb-memory-rows 8
3274 "Number of data rows in memory window."
3279 (defcustom gdb-memory-columns 4
3280 "Number of data columns in memory window."
3285 (defcustom gdb-memory-format "x"
3286 "Display format of data items in memory window."
3287 :type '(choice (const :tag "Hexadecimal" "x")
3288 (const :tag "Signed decimal" "d")
3289 (const :tag "Unsigned decimal" "u")
3290 (const :tag "Octal" "o")
3291 (const :tag "Binary" "t"))
3295 (defcustom gdb-memory-unit 4
3296 "Unit size of data items in memory window."
3297 :type '(choice (const :tag "Byte" 1)
3298 (const :tag "Halfword" 2)
3299 (const :tag "Word" 4)
3300 (const :tag "Giant word" 8))
3304 (def-gdb-trigger-and-handler
3305 gdb-invalidate-memory
3306 (format "-data-read-memory %s %s %d %d %d"
3312 gdb-read-memory-handler
3313 gdb-read-memory-custom
3316 (gdb-set-buffer-rules
3318 'gdb-memory-buffer-name
3320 'gdb-invalidate-memory)
3322 (defun gdb-memory-column-width (size format)
3323 "Return length of string with memory unit of SIZE in FORMAT.
3325 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3326 in `gdb-memory-format'."
3327 (let ((format-base (cdr (assoc format
3329 ("d" . 10) ("u" . 10)
3333 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3334 (cond ((string-equal format "x")
3335 (+ 2 res)) ; hexadecimal numbers have 0x in front
3336 ((or (string-equal format "d")
3337 (string-equal format "o"))
3340 (error "Unknown format"))))
3342 (defun gdb-read-memory-custom ()
3343 (let* ((res (gdb-json-partial-output))
3344 (err-msg (bindat-get-field res 'msg)))
3346 (let ((memory (bindat-get-field res 'memory)))
3347 (setq gdb-memory-address (bindat-get-field res 'addr))
3348 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3349 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3350 (setq gdb-memory-last-address gdb-memory-address)
3351 (dolist (row memory)
3352 (insert (concat (bindat-get-field row 'addr) ":"))
3353 (dolist (column (bindat-get-field row 'data))
3354 (insert (gdb-pad-string column
3355 (+ 2 (gdb-memory-column-width
3357 gdb-memory-format)))))
3359 ;; Show last page instead of empty buffer when out of bounds
3361 (let ((gdb-memory-address gdb-memory-last-address))
3362 (gdb-invalidate-memory 'update)
3363 (error err-msg))))))
3365 (defvar gdb-memory-mode-map
3366 (let ((map (make-sparse-keymap)))
3367 (suppress-keymap map t)
3368 (define-key map "q" 'kill-this-buffer)
3369 (define-key map "n" 'gdb-memory-show-next-page)
3370 (define-key map "p" 'gdb-memory-show-previous-page)
3371 (define-key map "a" 'gdb-memory-set-address)
3372 (define-key map "t" 'gdb-memory-format-binary)
3373 (define-key map "o" 'gdb-memory-format-octal)
3374 (define-key map "u" 'gdb-memory-format-unsigned)
3375 (define-key map "d" 'gdb-memory-format-signed)
3376 (define-key map "x" 'gdb-memory-format-hexadecimal)
3377 (define-key map "b" 'gdb-memory-unit-byte)
3378 (define-key map "h" 'gdb-memory-unit-halfword)
3379 (define-key map "w" 'gdb-memory-unit-word)
3380 (define-key map "g" 'gdb-memory-unit-giant)
3381 (define-key map "R" 'gdb-memory-set-rows)
3382 (define-key map "C" 'gdb-memory-set-columns)
3385 (defun gdb-memory-set-address-event (event)
3386 "Handle a click on address field in memory buffer header."
3388 (save-selected-window
3389 (select-window (posn-window (event-start event)))
3390 (gdb-memory-set-address)))
3392 ;; Non-event version for use within keymap
3393 (defun gdb-memory-set-address ()
3394 "Set the start memory address."
3396 (let ((arg (read-from-minibuffer "Memory address: ")))
3397 (setq gdb-memory-address arg))
3398 (gdb-invalidate-memory 'update))
3400 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3401 "Define a function NAME which reads new VAR value from minibuffer."
3402 `(defun ,name (event)
3405 (save-selected-window
3406 (select-window (posn-window (event-start event)))
3407 (let* ((arg (read-from-minibuffer ,echo-string))
3408 (count (string-to-number arg)))
3410 (error "Positive number only")
3411 (customize-set-variable ',variable count)
3412 (gdb-invalidate-memory 'update))))))
3414 (def-gdb-set-positive-number
3418 "Set the number of data rows in memory window.")
3420 (def-gdb-set-positive-number
3421 gdb-memory-set-columns
3424 "Set the number of data columns in memory window.")
3426 (defmacro def-gdb-memory-format (name format doc)
3427 "Define a function NAME to switch memory buffer to use FORMAT.
3429 DOC is an optional documentation string."
3430 `(defun ,name () ,(when doc doc)
3432 (customize-set-variable 'gdb-memory-format ,format)
3433 (gdb-invalidate-memory 'update)))
3435 (def-gdb-memory-format
3436 gdb-memory-format-binary "t"
3437 "Set the display format to binary.")
3439 (def-gdb-memory-format
3440 gdb-memory-format-octal "o"
3441 "Set the display format to octal.")
3443 (def-gdb-memory-format
3444 gdb-memory-format-unsigned "u"
3445 "Set the display format to unsigned decimal.")
3447 (def-gdb-memory-format
3448 gdb-memory-format-signed "d"
3449 "Set the display format to decimal.")
3451 (def-gdb-memory-format
3452 gdb-memory-format-hexadecimal "x"
3453 "Set the display format to hexadecimal.")
3455 (defvar gdb-memory-format-map
3456 (let ((map (make-sparse-keymap)))
3457 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3459 "Keymap to select format in the header line.")
3461 (defvar gdb-memory-format-menu
3462 (let ((map (make-sparse-keymap "Format")))
3464 (define-key map [binary]
3465 '(menu-item "Binary" gdb-memory-format-binary
3466 :button (:radio . (equal gdb-memory-format "t"))))
3467 (define-key map [octal]
3468 '(menu-item "Octal" gdb-memory-format-octal
3469 :button (:radio . (equal gdb-memory-format "o"))))
3470 (define-key map [unsigned]
3471 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3472 :button (:radio . (equal gdb-memory-format "u"))))
3473 (define-key map [signed]
3474 '(menu-item "Signed Decimal" gdb-memory-format-signed
3475 :button (:radio . (equal gdb-memory-format "d"))))
3476 (define-key map [hexadecimal]
3477 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3478 :button (:radio . (equal gdb-memory-format "x"))))
3480 "Menu of display formats in the header line.")
3482 (defun gdb-memory-format-menu (event)
3484 (x-popup-menu event gdb-memory-format-menu))
3486 (defun gdb-memory-format-menu-1 (event)
3488 (save-selected-window
3489 (select-window (posn-window (event-start event)))
3490 (let* ((selection (gdb-memory-format-menu event))
3491 (binding (and selection (lookup-key gdb-memory-format-menu
3492 (vector (car selection))))))
3493 (if binding (call-interactively binding)))))
3495 (defmacro def-gdb-memory-unit (name unit-size doc)
3496 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3498 DOC is an optional documentation string."
3499 `(defun ,name () ,(when doc doc)
3501 (customize-set-variable 'gdb-memory-unit ,unit-size)
3502 (gdb-invalidate-memory 'update)))
3504 (def-gdb-memory-unit gdb-memory-unit-giant 8
3505 "Set the unit size to giant words (eight bytes).")
3507 (def-gdb-memory-unit gdb-memory-unit-word 4
3508 "Set the unit size to words (four bytes).")
3510 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3511 "Set the unit size to halfwords (two bytes).")
3513 (def-gdb-memory-unit gdb-memory-unit-byte 1
3514 "Set the unit size to bytes.")
3516 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3517 "Define a function NAME which show new address in memory buffer.
3519 The defined function switches Memory buffer to show address
3520 stored in ADDRESS-VAR variable.
3522 DOC is an optional documentation string."
3526 (let ((gdb-memory-address ,address-var))
3527 (gdb-invalidate-memory))))
3529 (def-gdb-memory-show-page gdb-memory-show-previous-page
3530 gdb-memory-prev-page)
3532 (def-gdb-memory-show-page gdb-memory-show-next-page
3533 gdb-memory-next-page)
3535 (defvar gdb-memory-unit-map
3536 (let ((map (make-sparse-keymap)))
3537 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3539 "Keymap to select units in the header line.")
3541 (defvar gdb-memory-unit-menu
3542 (let ((map (make-sparse-keymap "Unit")))
3543 (define-key map [giantwords]
3544 '(menu-item "Giant words" gdb-memory-unit-giant
3545 :button (:radio . (equal gdb-memory-unit 8))))
3546 (define-key map [words]
3547 '(menu-item "Words" gdb-memory-unit-word
3548 :button (:radio . (equal gdb-memory-unit 4))))
3549 (define-key map [halfwords]
3550 '(menu-item "Halfwords" gdb-memory-unit-halfword
3551 :button (:radio . (equal gdb-memory-unit 2))))
3552 (define-key map [bytes]
3553 '(menu-item "Bytes" gdb-memory-unit-byte
3554 :button (:radio . (equal gdb-memory-unit 1))))
3556 "Menu of units in the header line.")
3558 (defun gdb-memory-unit-menu (event)
3560 (x-popup-menu event gdb-memory-unit-menu))
3562 (defun gdb-memory-unit-menu-1 (event)
3564 (save-selected-window
3565 (select-window (posn-window (event-start event)))
3566 (let* ((selection (gdb-memory-unit-menu event))
3567 (binding (and selection (lookup-key gdb-memory-unit-menu
3568 (vector (car selection))))))
3569 (if binding (call-interactively binding)))))
3571 (defvar gdb-memory-font-lock-keywords
3572 '(;; <__function.name+n>
3573 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3574 (1 font-lock-function-name-face)))
3575 "Font lock keywords used in `gdb-memory-mode'.")
3577 (defvar gdb-memory-header
3582 'face font-lock-warning-face
3583 'help-echo "mouse-1: decrement address"
3584 'mouse-face 'mode-line-highlight
3585 'local-map (gdb-make-header-line-mouse-map
3587 #'gdb-memory-show-previous-page))
3590 'face font-lock-warning-face
3591 'help-echo "mouse-1: increment address"
3592 'mouse-face 'mode-line-highlight
3593 'local-map (gdb-make-header-line-mouse-map
3595 #'gdb-memory-show-next-page))
3597 (propertize gdb-memory-address
3598 'face font-lock-warning-face
3599 'help-echo "mouse-1: set start address"
3600 'mouse-face 'mode-line-highlight
3601 'local-map (gdb-make-header-line-mouse-map
3603 #'gdb-memory-set-address-event))
3605 (propertize (number-to-string gdb-memory-rows)
3606 'face font-lock-warning-face
3607 'help-echo "mouse-1: set number of columns"
3608 'mouse-face 'mode-line-highlight
3609 'local-map (gdb-make-header-line-mouse-map
3611 #'gdb-memory-set-rows))
3613 (propertize (number-to-string gdb-memory-columns)
3614 'face font-lock-warning-face
3615 'help-echo "mouse-1: set number of columns"
3616 'mouse-face 'mode-line-highlight
3617 'local-map (gdb-make-header-line-mouse-map
3619 #'gdb-memory-set-columns))
3621 (propertize gdb-memory-format
3622 'face font-lock-warning-face
3623 'help-echo "mouse-3: select display format"
3624 'mouse-face 'mode-line-highlight
3625 'local-map gdb-memory-format-map)
3627 (propertize (number-to-string gdb-memory-unit)
3628 'face font-lock-warning-face
3629 'help-echo "mouse-3: select unit size"
3630 'mouse-face 'mode-line-highlight
3631 'local-map gdb-memory-unit-map)))
3632 "Header line used in `gdb-memory-mode'.")
3634 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3635 "Major mode for examining memory."
3636 (setq header-line-format gdb-memory-header)
3637 (set (make-local-variable 'font-lock-defaults)
3638 '(gdb-memory-font-lock-keywords))
3639 'gdb-invalidate-memory)
3641 (defun gdb-memory-buffer-name ()
3642 (concat "*memory of " (gdb-get-target-string) "*"))
3644 (defun gdb-display-memory-buffer (&optional thread)
3645 "Display GDB memory contents."
3647 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3649 (defun gdb-frame-memory-buffer ()
3650 "Display memory contents in another frame."
3652 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3653 gdb-display-buffer-other-frame-action))
3656 ;;; Disassembly view
3658 (defun gdb-disassembly-buffer-name ()
3659 (gdb-current-context-buffer-name
3660 (concat "disassembly of " (gdb-get-target-string))))
3662 (defun gdb-display-disassembly-buffer (&optional thread)
3663 "Display GDB disassembly information."
3665 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3667 (def-gdb-preempt-display-buffer
3668 gdb-preemptively-display-disassembly-buffer
3669 'gdb-disassembly-buffer)
3671 (defun gdb-frame-disassembly-buffer (&optional thread)
3672 "Display GDB disassembly information in another frame."
3674 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3675 gdb-display-buffer-other-frame-action))
3677 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3678 (let* ((frame (gdb-current-buffer-frame))
3679 (file (bindat-get-field frame 'fullname))
3680 (line (bindat-get-field frame 'line)))
3682 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3683 ;; If we're unable to get a file name / line for $PC, simply
3684 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3685 ;; 150 bytes) instructions.
3686 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3687 gdb-disassembly-handler
3688 ;; We update disassembly only after we have actual frame information
3689 ;; about all threads, so no there's `update' signal in this list
3690 '(start update-disassembly))
3692 (def-gdb-auto-update-handler
3693 gdb-disassembly-handler
3694 gdb-disassembly-handler-custom
3697 (gdb-set-buffer-rules
3698 'gdb-disassembly-buffer
3699 'gdb-disassembly-buffer-name
3700 'gdb-disassembly-mode
3701 'gdb-invalidate-disassembly)
3703 (defvar gdb-disassembly-font-lock-keywords
3704 '(;; <__function.name+n>
3705 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3706 (1 font-lock-function-name-face))
3707 ;; 0xNNNNNNNN <__function.name+n>: opcode
3708 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3709 (4 font-lock-keyword-face))
3710 ;; %register(at least i386)
3711 ("%\\sw+" . font-lock-variable-name-face)
3712 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3713 (1 font-lock-comment-face)
3714 (2 font-lock-function-name-face))
3715 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3716 "Font lock keywords used in `gdb-disassembly-mode'.")
3718 (defvar gdb-disassembly-mode-map
3720 (let ((map (make-sparse-keymap)))
3721 (suppress-keymap map)
3722 (define-key map "q" 'kill-this-buffer)
3725 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3726 "Major mode for GDB disassembly information."
3727 ;; TODO Rename overlay variable for disassembly mode
3728 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3729 (setq fringes-outside-margins t)
3730 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3731 (set (make-local-variable 'font-lock-defaults)
3732 '(gdb-disassembly-font-lock-keywords))
3733 'gdb-invalidate-disassembly)
3735 (defun gdb-disassembly-handler-custom ()
3736 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3737 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3738 (table (make-gdb-table))
3740 (dolist (instr instructions)
3741 (gdb-table-add-row table
3743 (bindat-get-field instr 'address)
3745 ((func-name (bindat-get-field instr 'func-name))
3746 (offset (bindat-get-field instr 'offset)))
3748 (format "<%s+%s>:" func-name offset)
3750 (bindat-get-field instr 'inst)))
3751 (when (string-equal (bindat-get-field instr 'address)
3754 (setq marked-line (length (gdb-table-rows table)))
3755 (setq fringe-indicator-alist
3756 (if (string-equal gdb-frame-number "0")
3758 '((overlay-arrow . hollow-right-triangle)))))))
3759 (insert (gdb-table-string table " "))
3760 (gdb-disassembly-place-breakpoints)
3761 ;; Mark current position with overlay arrow and scroll window to
3764 (let ((window (get-buffer-window (current-buffer) 0)))
3765 (set-window-point window (gdb-mark-line marked-line
3766 gdb-disassembly-position))))
3768 (gdb-current-context-mode-name
3769 (concat "Disassembly: "
3770 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3772 (defun gdb-disassembly-place-breakpoints ()
3773 (gdb-remove-breakpoint-icons (point-min) (point-max))
3774 (dolist (breakpoint gdb-breakpoints-list)
3775 (let* ((breakpoint (cdr breakpoint))
3776 (bptno (bindat-get-field breakpoint 'number))
3777 (flag (bindat-get-field breakpoint 'enabled))
3778 (address (bindat-get-field breakpoint 'addr)))
3780 (goto-char (point-min))
3781 (if (re-search-forward (concat "^" address) nil t)
3782 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3785 (defvar gdb-breakpoints-header
3787 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3790 (gdb-propertize-header "Threads" gdb-threads-buffer
3791 "mouse-1: select" mode-line-highlight
3792 mode-line-inactive)))
3794 ;;; Breakpoints view
3795 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3796 "Major mode for gdb breakpoints."
3797 (setq header-line-format gdb-breakpoints-header)
3798 'gdb-invalidate-breakpoints)
3800 (defun gdb-toggle-breakpoint ()
3801 "Enable/disable breakpoint at current line of breakpoints buffer."
3805 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3808 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3811 (bindat-get-field breakpoint 'number)))
3812 (error "Not recognized as break/watchpoint line")))))
3814 (defun gdb-delete-breakpoint ()
3815 "Delete the breakpoint at current line of breakpoints buffer."
3819 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3821 (gud-basic-call (concat "-break-delete "
3822 (bindat-get-field breakpoint 'number)))
3823 (error "Not recognized as break/watchpoint line")))))
3825 (defun gdb-goto-breakpoint (&optional event)
3826 "Go to the location of breakpoint at current line of breakpoints buffer."
3827 (interactive (list last-input-event))
3828 (if event (posn-set-point (event-end event)))
3829 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3830 (let ((window (get-buffer-window gud-comint-buffer)))
3831 (if window (save-selected-window (select-window window))))
3834 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3836 (let ((bptno (bindat-get-field breakpoint 'number))
3837 (file (bindat-get-field breakpoint 'fullname))
3838 (line (bindat-get-field breakpoint 'line)))
3839 (save-selected-window
3840 (let* ((buffer (find-file-noselect
3841 (if (file-exists-p file) file
3842 (cdr (assoc bptno gdb-location-alist)))))
3843 (window (or (gdb-display-source-buffer buffer)
3844 (display-buffer buffer))))
3845 (setq gdb-source-window window)
3846 (with-current-buffer buffer
3847 (goto-char (point-min))
3848 (forward-line (1- (string-to-number line)))
3849 (set-window-point window (point))))))
3850 (error "Not recognized as break/watchpoint line")))))
3853 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3855 (def-gdb-trigger-and-handler
3856 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3857 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3860 (gdb-set-buffer-rules
3862 'gdb-stack-buffer-name
3864 'gdb-invalidate-frames)
3866 (defun gdb-frame-location (frame)
3867 "Return \" of file:line\" or \" of library\" for structure FRAME.
3869 FRAME must have either \"file\" and \"line\" members or \"from\"
3871 (let ((file (bindat-get-field frame 'file))
3872 (line (bindat-get-field frame 'line))
3873 (from (bindat-get-field frame 'from)))
3874 (let ((res (or (and file line (concat file ":" line))
3876 (if res (concat " of " res) ""))))
3878 (defun gdb-stack-list-frames-custom ()
3879 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3880 (table (make-gdb-table)))
3881 (set-marker gdb-stack-position nil)
3882 (dolist (frame stack)
3883 (gdb-table-add-row table
3885 (bindat-get-field frame 'level)
3888 (bindat-get-field frame 'func)
3889 (if gdb-stack-buffer-locations
3890 (gdb-frame-location frame) "")
3891 (if gdb-stack-buffer-addresses
3892 (concat " at " (bindat-get-field frame 'addr)) "")))
3893 `(mouse-face highlight
3894 help-echo "mouse-2, RET: Select frame"
3896 (insert (gdb-table-string table " ")))
3897 (when (and gdb-frame-number
3898 (gdb-buffer-shows-main-thread-p))
3899 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3900 gdb-stack-position))
3902 (gdb-current-context-mode-name "Frames")))
3904 (defun gdb-stack-buffer-name ()
3905 (gdb-current-context-buffer-name
3906 (concat "stack frames of " (gdb-get-target-string))))
3908 (defun gdb-display-stack-buffer (&optional thread)
3909 "Display GDB backtrace for current stack."
3911 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
3913 (def-gdb-preempt-display-buffer
3914 gdb-preemptively-display-stack-buffer
3915 'gdb-stack-buffer nil t)
3917 (defun gdb-frame-stack-buffer (&optional thread)
3918 "Display GDB backtrace for current stack in another frame."
3920 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
3921 gdb-display-buffer-other-frame-action))
3923 (defvar gdb-frames-mode-map
3924 (let ((map (make-sparse-keymap)))
3925 (suppress-keymap map)
3926 (define-key map "q" 'kill-this-buffer)
3927 (define-key map "\r" 'gdb-select-frame)
3928 (define-key map [mouse-2] 'gdb-select-frame)
3929 (define-key map [follow-link] 'mouse-face)
3932 (defvar gdb-frames-font-lock-keywords
3933 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3934 "Font lock keywords used in `gdb-frames-mode'.")
3936 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3937 "Major mode for gdb call stack."
3938 (setq gdb-stack-position (make-marker))
3939 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3940 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3941 (set (make-local-variable 'font-lock-defaults)
3942 '(gdb-frames-font-lock-keywords))
3943 'gdb-invalidate-frames)
3945 (defun gdb-select-frame (&optional event)
3946 "Select the frame and display the relevant source."
3947 (interactive (list last-input-event))
3948 (if event (posn-set-point (event-end event)))
3949 (let ((frame (get-text-property (point) 'gdb-frame)))
3951 (if (gdb-buffer-shows-main-thread-p)
3952 (let ((new-level (bindat-get-field frame 'level)))
3953 (setq gdb-frame-number new-level)
3954 (gdb-input (concat "-stack-select-frame " new-level)
3957 (error "Could not select frame for non-current thread"))
3958 (error "Not recognized as frame line"))))
3962 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3963 (def-gdb-trigger-and-handler
3964 gdb-invalidate-locals
3965 (concat (gdb-current-context-command "-stack-list-locals")
3967 gdb-locals-handler gdb-locals-handler-custom
3970 (gdb-set-buffer-rules
3972 'gdb-locals-buffer-name
3974 'gdb-invalidate-locals)
3976 (defvar gdb-locals-watch-map
3977 (let ((map (make-sparse-keymap)))
3978 (suppress-keymap map)
3979 (define-key map "\r" 'gud-watch)
3980 (define-key map [mouse-2] 'gud-watch)
3982 "Keymap to create watch expression of a complex data type local variable.")
3984 (defvar gdb-edit-locals-map-1
3985 (let ((map (make-sparse-keymap)))
3986 (suppress-keymap map)
3987 (define-key map "\r" 'gdb-edit-locals-value)
3988 (define-key map [mouse-2] 'gdb-edit-locals-value)
3990 "Keymap to edit value of a simple data type local variable.")
3992 (defun gdb-edit-locals-value (&optional event)
3993 "Assign a value to a variable displayed in the locals buffer."
3994 (interactive (list last-input-event))
3996 (if event (posn-set-point (event-end event)))
3998 (let* ((var (bindat-get-field
3999 (get-text-property (point) 'gdb-local-variable) 'name))
4000 (value (read-string (format "New value (%s): " var))))
4002 (concat "-gdb-set variable " var " = " value)))))
4004 ;; Don't display values of arrays or structures.
4005 ;; These can be expanded using gud-watch.
4006 (defun gdb-locals-handler-custom ()
4007 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4008 (table (make-gdb-table)))
4009 (dolist (local locals-list)
4010 (let ((name (bindat-get-field local 'name))
4011 (value (bindat-get-field local 'value))
4012 (type (bindat-get-field local 'type)))
4014 (string-match "\\0x" value))
4015 (add-text-properties 0 (length name)
4016 `(mouse-face highlight
4017 help-echo "mouse-2: create watch expression"
4018 local-map ,gdb-locals-watch-map)
4020 (add-text-properties 0 (length value)
4021 `(mouse-face highlight
4022 help-echo "mouse-2: edit value"
4023 local-map ,gdb-edit-locals-map-1)
4028 (propertize type 'font-lock-face font-lock-type-face)
4029 (propertize name 'font-lock-face font-lock-variable-name-face)
4031 `(gdb-local-variable ,local))))
4032 (insert (gdb-table-string table " "))
4034 (gdb-current-context-mode-name
4036 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4038 (defvar gdb-locals-header
4040 (gdb-propertize-header "Locals" gdb-locals-buffer
4043 (gdb-propertize-header "Registers" gdb-registers-buffer
4044 "mouse-1: select" mode-line-highlight
4045 mode-line-inactive)))
4047 (defvar gdb-locals-mode-map
4048 (let ((map (make-sparse-keymap)))
4049 (suppress-keymap map)
4050 (define-key map "q" 'kill-this-buffer)
4051 (define-key map "\t" (lambda ()
4053 (gdb-set-window-buffer
4054 (gdb-get-buffer-create
4055 'gdb-registers-buffer
4056 gdb-thread-number) t)))
4059 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4060 "Major mode for gdb locals."
4061 (setq header-line-format gdb-locals-header)
4062 'gdb-invalidate-locals)
4064 (defun gdb-locals-buffer-name ()
4065 (gdb-current-context-buffer-name
4066 (concat "locals of " (gdb-get-target-string))))
4068 (defun gdb-display-locals-buffer (&optional thread)
4069 "Display the local variables of current GDB stack."
4071 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4073 (def-gdb-preempt-display-buffer
4074 gdb-preemptively-display-locals-buffer
4075 'gdb-locals-buffer nil t)
4077 (defun gdb-frame-locals-buffer (&optional thread)
4078 "Display the local variables of the current GDB stack in another frame."
4080 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4081 gdb-display-buffer-other-frame-action))
4084 ;; Registers buffer.
4086 (def-gdb-trigger-and-handler
4087 gdb-invalidate-registers
4088 (concat (gdb-current-context-command "-data-list-register-values") " x")
4089 gdb-registers-handler
4090 gdb-registers-handler-custom
4093 (gdb-set-buffer-rules
4094 'gdb-registers-buffer
4095 'gdb-registers-buffer-name
4097 'gdb-invalidate-registers)
4099 (defun gdb-registers-handler-custom ()
4100 (when gdb-register-names
4101 (let ((register-values
4102 (bindat-get-field (gdb-json-partial-output) 'register-values))
4103 (table (make-gdb-table)))
4104 (dolist (register register-values)
4105 (let* ((register-number (bindat-get-field register 'number))
4106 (value (bindat-get-field register 'value))
4107 (register-name (nth (string-to-number register-number)
4108 gdb-register-names)))
4112 (propertize register-name
4113 'font-lock-face font-lock-variable-name-face)
4114 (if (member register-number gdb-changed-registers)
4115 (propertize value 'font-lock-face font-lock-warning-face)
4117 `(mouse-face highlight
4118 help-echo "mouse-2: edit value"
4119 gdb-register-name ,register-name))))
4120 (insert (gdb-table-string table " ")))
4122 (gdb-current-context-mode-name "Registers"))))
4124 (defun gdb-edit-register-value (&optional event)
4125 "Assign a value to a register displayed in the registers buffer."
4126 (interactive (list last-input-event))
4128 (if event (posn-set-point (event-end event)))
4130 (let* ((var (bindat-get-field
4131 (get-text-property (point) 'gdb-register-name)))
4132 (value (read-string (format "New value (%s): " var))))
4134 (concat "-gdb-set variable $" var " = " value)))))
4136 (defvar gdb-registers-mode-map
4137 (let ((map (make-sparse-keymap)))
4138 (suppress-keymap map)
4139 (define-key map "\r" 'gdb-edit-register-value)
4140 (define-key map [mouse-2] 'gdb-edit-register-value)
4141 (define-key map "q" 'kill-this-buffer)
4142 (define-key map "\t" (lambda ()
4144 (gdb-set-window-buffer
4145 (gdb-get-buffer-create
4147 gdb-thread-number) t)))
4150 (defvar gdb-registers-header
4152 (gdb-propertize-header "Locals" gdb-locals-buffer
4153 "mouse-1: select" mode-line-highlight
4156 (gdb-propertize-header "Registers" gdb-registers-buffer
4157 nil nil mode-line)))
4159 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4160 "Major mode for gdb registers."
4161 (setq header-line-format gdb-registers-header)
4162 'gdb-invalidate-registers)
4164 (defun gdb-registers-buffer-name ()
4165 (gdb-current-context-buffer-name
4166 (concat "registers of " (gdb-get-target-string))))
4168 (defun gdb-display-registers-buffer (&optional thread)
4169 "Display GDB register contents."
4171 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4173 (def-gdb-preempt-display-buffer
4174 gdb-preemptively-display-registers-buffer
4175 'gdb-registers-buffer nil t)
4177 (defun gdb-frame-registers-buffer (&optional thread)
4178 "Display GDB register contents in another frame."
4180 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4181 gdb-display-buffer-other-frame-action))
4183 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4184 (defun gdb-get-changed-registers ()
4185 (when (gdb-get-buffer 'gdb-registers-buffer)
4186 (gdb-input "-data-list-changed-registers"
4187 'gdb-changed-registers-handler
4188 'gdb-get-changed-registers)))
4190 (defun gdb-changed-registers-handler ()
4191 (setq gdb-changed-registers nil)
4192 (dolist (register-number
4193 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4194 (push register-number gdb-changed-registers)))
4196 (defun gdb-register-names-handler ()
4197 ;; Don't use pending triggers because this handler is called
4198 ;; only once (in gdb-init-1)
4199 (setq gdb-register-names nil)
4200 (dolist (register-name
4201 (bindat-get-field (gdb-json-partial-output) 'register-names))
4202 (push register-name gdb-register-names))
4203 (setq gdb-register-names (reverse gdb-register-names)))
4206 (defun gdb-get-source-file-list ()
4207 "Create list of source files for current GDB session.
4208 If buffers already exist for any of these files, `gud-minor-mode'
4210 (goto-char (point-min))
4211 (while (re-search-forward gdb-source-file-regexp nil t)
4212 (push (match-string 1) gdb-source-file-list))
4213 (dolist (buffer (buffer-list))
4214 (with-current-buffer buffer
4215 (when (member buffer-file-name gdb-source-file-list)
4216 (gdb-init-buffer)))))
4218 (defun gdb-get-main-selected-frame ()
4219 "Trigger for `gdb-frame-handler' which uses main current thread.
4220 Called from `gdb-update'."
4221 (gdb-input (gdb-current-context-command "-stack-info-frame")
4223 'gdb-get-main-selected-frame))
4225 (defun gdb-frame-handler ()
4226 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4227 overlay arrow in source buffer."
4228 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4230 (setq gdb-selected-frame (bindat-get-field frame 'func))
4231 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4232 (setq gdb-frame-number (bindat-get-field frame 'level))
4233 (setq gdb-frame-address (bindat-get-field frame 'addr))
4234 (let ((line (bindat-get-field frame 'line)))
4235 (setq gdb-selected-line (and line (string-to-number line)))
4236 (when (and gdb-selected-file gdb-selected-line)
4237 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4238 (gud-display-frame)))
4239 (if gud-overlay-arrow-position
4240 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4241 (position (marker-position gud-overlay-arrow-position)))
4243 (with-current-buffer buffer
4244 (setq fringe-indicator-alist
4245 (if (string-equal gdb-frame-number "0")
4247 '((overlay-arrow . hollow-right-triangle))))
4248 (setq gud-overlay-arrow-position (make-marker))
4249 (set-marker gud-overlay-arrow-position position))))))))
4251 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
4253 (defun gdb-get-prompt ()
4254 "Find prompt for GDB session."
4255 (goto-char (point-min))
4256 (setq gdb-prompt-name nil)
4257 (re-search-forward gdb-prompt-name-regexp nil t)
4258 (setq gdb-prompt-name (match-string 1))
4259 ;; Insert first prompt.
4260 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4262 ;;;; Window management
4263 (defun gdb-display-buffer (buf)
4264 "Show buffer BUF, and make that window dedicated."
4265 (let ((window (display-buffer buf)))
4266 (set-window-dedicated-p window t)
4269 ;; (let ((answer (get-buffer-window buf 0)))
4271 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4272 ;; (let ((window (get-lru-window)))
4273 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4275 ;; (let ((largest (get-largest-window)))
4276 ;; (setq answer (split-window largest))
4277 ;; (set-window-buffer answer buf)
4278 ;; (set-window-dedicated-p answer t)
4280 ;; (set-window-buffer window buf)
4284 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4285 "Find window displaying a buffer with the same
4286 `gdb-buffer-type' as BUF and show BUF there. If no such window
4287 exists, just call `gdb-display-buffer' for BUF. If the window
4288 found is already dedicated, split window according to
4289 SPLIT-HORIZONTAL and show BUF in the new window."
4291 (when (not (get-buffer-window buf))
4292 (let* ((buf-type (gdb-buffer-type buf))
4294 (get-window-with-predicate
4297 (gdb-buffer-type (window-buffer w)))
4298 (not (window-dedicated-p w)))))))
4300 (set-window-buffer existing-window buf)
4301 (let ((dedicated-window
4302 (get-window-with-predicate
4305 (gdb-buffer-type (window-buffer w)))))))
4306 (if dedicated-window
4308 (split-window dedicated-window nil split-horizontal) buf)
4309 (gdb-display-buffer buf))))))
4310 (error "Null buffer")))
4312 ;;; Shared keymap initialization:
4314 (let ((menu (make-sparse-keymap "GDB-Windows")))
4315 (define-key gud-menu-map [displays]
4316 `(menu-item "GDB-Windows" ,menu
4317 :visible (eq gud-minor-mode 'gdbmi)))
4318 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4319 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4320 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4321 (define-key menu [disassembly]
4322 '("Disassembly" . gdb-display-disassembly-buffer))
4323 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4324 (define-key menu [inferior]
4325 '("IO" . gdb-display-io-buffer))
4326 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4327 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4328 (define-key menu [breakpoints]
4329 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4331 (let ((menu (make-sparse-keymap "GDB-Frames")))
4332 (define-key gud-menu-map [frames]
4333 `(menu-item "GDB-Frames" ,menu
4334 :visible (eq gud-minor-mode 'gdbmi)))
4335 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4336 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4337 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4338 (define-key menu [disassembly]
4339 '("Disassembly" . gdb-frame-disassembly-buffer))
4340 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4341 (define-key menu [inferior]
4342 '("IO" . gdb-frame-io-buffer))
4343 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4344 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4345 (define-key menu [breakpoints]
4346 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4348 (let ((menu (make-sparse-keymap "GDB-MI")))
4349 (define-key menu [gdb-customize]
4350 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4351 :help "Customize Gdb Graphical Mode options."))
4352 (define-key menu [gdb-many-windows]
4353 '(menu-item "Display Other Windows" gdb-many-windows
4354 :help "Toggle display of locals, stack and breakpoint information"
4355 :button (:toggle . gdb-many-windows)))
4356 (define-key menu [gdb-restore-windows]
4357 '(menu-item "Restore Window Layout" gdb-restore-windows
4358 :help "Restore standard layout for debug session."))
4359 (define-key menu [sep1]
4361 (define-key menu [all-threads]
4362 '(menu-item "GUD controls all threads"
4365 (setq gdb-gud-control-all-threads t))
4366 :help "GUD start/stop commands apply to all threads"
4367 :button (:radio . gdb-gud-control-all-threads)))
4368 (define-key menu [current-thread]
4369 '(menu-item "GUD controls current thread"
4372 (setq gdb-gud-control-all-threads nil))
4373 :help "GUD start/stop commands apply to current thread only"
4374 :button (:radio . (not gdb-gud-control-all-threads))))
4375 (define-key menu [sep2]
4377 (define-key menu [gdb-customize-reasons]
4378 '(menu-item "Customize switching..."
4381 (customize-option 'gdb-switch-reasons))))
4382 (define-key menu [gdb-switch-when-another-stopped]
4383 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4384 gdb-switch-when-another-stopped
4385 "Automatically switch to stopped thread"
4386 "GDB thread switching %s"
4387 "Switch to stopped thread"))
4388 (define-key gud-menu-map [mi]
4389 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4391 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4392 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4393 ;; tool-bar-local-item-from-menu here because it appends new buttons
4394 ;; to toolbar from right to left while we want our A/T throttle to
4395 ;; show up right before Run button.
4396 (define-key-after gud-tool-bar-map [all-threads]
4397 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4398 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4399 :visible (and (eq gud-minor-mode 'gdbmi)
4401 (not gdb-gud-control-all-threads)))
4404 (define-key-after gud-tool-bar-map [current-thread]
4405 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4406 :image (find-image '((:type xpm :file "gud/all.xpm")))
4407 :visible (and (eq gud-minor-mode 'gdbmi)
4409 gdb-gud-control-all-threads))
4412 (defun gdb-frame-gdb-buffer ()
4413 "Display GUD buffer in another frame."
4415 (display-buffer-other-frame gud-comint-buffer))
4417 (defun gdb-display-gdb-buffer ()
4418 "Display GUD buffer."
4420 (pop-to-buffer gud-comint-buffer nil 0))
4422 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4423 "Set buffer of selected window to NAME and dedicate window.
4425 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4426 window is dedicated."
4427 (unless window (setq window (selected-window)))
4428 (when ignore-dedicated
4429 (set-window-dedicated-p window nil))
4430 (set-window-buffer window (get-buffer name))
4431 (set-window-dedicated-p window t))
4433 (defun gdb-setup-windows ()
4434 "Layout the window pattern for option `gdb-many-windows'."
4435 (gdb-get-buffer-create 'gdb-locals-buffer)
4436 (gdb-get-buffer-create 'gdb-stack-buffer)
4437 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4438 (set-window-dedicated-p (selected-window) nil)
4439 (switch-to-buffer gud-comint-buffer)
4440 (delete-other-windows)
4441 (let ((win0 (selected-window))
4442 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4443 (win2 (split-window nil ( / (window-height) 3)))
4444 (win3 (split-window-right)))
4445 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4446 (select-window win2)
4449 (if gud-last-last-frame
4450 (gud-find-file (car gud-last-last-frame))
4452 (gud-find-file gdb-main-file)
4453 ;; Put buffer list in window if we
4454 ;; can't find a source file.
4455 (list-buffers-noselect))))
4456 (setq gdb-source-window (selected-window))
4457 (let ((win4 (split-window-right)))
4458 (gdb-set-window-buffer
4459 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4460 (select-window win1)
4461 (gdb-set-window-buffer (gdb-stack-buffer-name))
4462 (let ((win5 (split-window-right)))
4463 (gdb-set-window-buffer (if gdb-show-threads-by-default
4464 (gdb-threads-buffer-name)
4465 (gdb-breakpoints-buffer-name))
4467 (select-window win0)))
4469 (define-minor-mode gdb-many-windows
4470 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4471 In this case it starts with two windows: one displaying the GUD
4472 buffer and the other with the source file with the main routine
4473 of the debugged program. Non-nil means display the layout shown for
4478 (if (and gud-comint-buffer
4479 (buffer-name gud-comint-buffer))
4481 (gdb-restore-windows))))
4483 (defun gdb-restore-windows ()
4484 "Restore the basic arrangement of windows used by gdb.
4485 This arrangement depends on the value of option `gdb-many-windows'."
4487 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4488 (delete-other-windows)
4489 (if gdb-many-windows
4491 (when (or gud-last-last-frame gdb-show-main)
4492 (let ((win (split-window)))
4495 (if gud-last-last-frame
4496 (gud-find-file (car gud-last-last-frame))
4497 (gud-find-file gdb-main-file)))
4498 (setq gdb-source-window win)))))
4500 ;; Called from `gud-sentinel' in gud.el:
4502 "Exit a debugging session cleanly.
4503 Kills the gdb buffers, and resets variables and the source buffers."
4504 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4505 ;; process. This pty must be deleted explicitly.
4506 (let ((pty (get-process "gdb-inferior")))
4507 (if pty (delete-process pty)))
4508 ;; Find gdb-mi buffers and kill them.
4509 (dolist (buffer (buffer-list))
4510 (unless (eq buffer gud-comint-buffer)
4511 (with-current-buffer buffer
4512 (if (eq gud-minor-mode 'gdbmi)
4513 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4515 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4516 (setq gud-minor-mode nil)
4517 (kill-local-variable 'tool-bar-map)
4518 (kill-local-variable 'gdb-define-alist))))))
4519 (setq gdb-disassembly-position nil)
4520 (setq overlay-arrow-variable-list
4521 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4522 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4523 (setq gdb-stack-position nil)
4524 (setq overlay-arrow-variable-list
4525 (delq 'gdb-stack-position overlay-arrow-variable-list))
4526 (setq gdb-thread-position nil)
4527 (setq overlay-arrow-variable-list
4528 (delq 'gdb-thread-position overlay-arrow-variable-list))
4529 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4530 (setq gud-running nil)
4531 (setq gdb-active-process nil)
4532 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4534 (defun gdb-get-source-file ()
4535 "Find the source file where the program starts and display it with related
4536 buffers, if required."
4537 (goto-char (point-min))
4538 (if (re-search-forward gdb-source-file-regexp nil t)
4539 (setq gdb-main-file (match-string 1)))
4540 (if gdb-many-windows
4542 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4545 (display-buffer (gud-find-file gdb-main-file))))
4546 (gdb-force-mode-line-update
4547 (propertize "ready" 'face font-lock-variable-name-face)))
4550 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4551 "Put string PUTSTRING in front of POS in the current buffer.
4552 PUTSTRING is displayed by putting an overlay into the current buffer with a
4553 `before-string' string that has a `display' property whose value is
4555 (let ((string (make-string 1 ?x))
4556 (buffer (current-buffer)))
4557 (setq putstring (copy-sequence putstring))
4558 (let ((overlay (make-overlay pos pos buffer))
4560 (list (list 'margin 'left-margin) putstring))))
4561 (put-text-property 0 1 'display prop string)
4563 (add-text-properties 0 1 sprops string))
4564 (overlay-put overlay 'put-break t)
4565 (overlay-put overlay 'before-string string))))
4567 ;;from remove-images
4568 (defun gdb-remove-strings (start end &optional buffer)
4569 "Remove strings between START and END in BUFFER.
4570 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4571 BUFFER nil or omitted means use the current buffer."
4573 (setq buffer (current-buffer)))
4574 (dolist (overlay (overlays-in start end))
4575 (when (overlay-get overlay 'put-break)
4576 (delete-overlay overlay))))
4578 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4579 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4580 (start (- (car posns) 1))
4581 (end (+ (cdr posns) 1))
4582 (putstring (if enabled "B" "b"))
4583 (source-window (get-buffer-window (current-buffer) 0)))
4584 (add-text-properties
4585 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4588 (add-text-properties
4589 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4590 (add-text-properties
4591 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4592 (gdb-remove-breakpoint-icons start end)
4593 (if (display-images-p)
4594 (if (>= (or left-fringe-width
4595 (if source-window (car (window-fringes source-window)))
4596 gdb-buffer-fringe-width) 8)
4599 `(left-fringe breakpoint
4602 'breakpoint-disabled))
4604 'gdb-enabled enabled)
4605 (when (< left-margin-width 2)
4606 (save-current-buffer
4607 (setq left-margin-width 2)
4611 left-margin-width right-margin-width))))
4614 (or breakpoint-enabled-icon
4615 (setq breakpoint-enabled-icon
4616 (find-image `((:type xpm :data
4617 ,breakpoint-xpm-data
4618 :ascent 100 :pointer hand)
4620 ,breakpoint-enabled-pbm-data
4621 :ascent 100 :pointer hand)))))
4622 (or breakpoint-disabled-icon
4623 (setq breakpoint-disabled-icon
4624 (find-image `((:type xpm :data
4625 ,breakpoint-xpm-data
4626 :conversion disabled
4627 :ascent 100 :pointer hand)
4629 ,breakpoint-disabled-pbm-data
4630 :ascent 100 :pointer hand))))))
4634 (when (< left-margin-width 2)
4635 (save-current-buffer
4636 (setq left-margin-width 2)
4637 (let ((window (get-buffer-window (current-buffer) 0)))
4640 window left-margin-width right-margin-width)))))
4642 (propertize putstring
4644 'breakpoint-enabled 'breakpoint-disabled))
4647 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4648 (gdb-remove-strings start end)
4649 (if (display-images-p)
4650 (remove-images start end))
4652 (setq left-margin-width 0)
4653 (let ((window (get-buffer-window (current-buffer) 0)))
4656 window left-margin-width right-margin-width)))))
4659 ;;; Functions for inline completion.
4661 (defvar gud-gdb-fetch-lines-in-progress)
4662 (defvar gud-gdb-fetch-lines-string)
4663 (defvar gud-gdb-fetch-lines-break)
4664 (defvar gud-gdb-fetched-lines)
4666 (defun gud-gdbmi-completions (context command)
4667 "Completion table for GDB/MI commands.
4668 COMMAND is the prefix for which we seek completion.
4669 CONTEXT is the text before COMMAND on the line."
4670 (let ((gud-gdb-fetch-lines-in-progress t)
4671 (gud-gdb-fetch-lines-string nil)
4672 (gud-gdb-fetch-lines-break (length context))
4673 (gud-gdb-fetched-lines nil)
4674 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4675 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4676 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4677 (gdb-input (concat "complete " context command)
4678 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4679 (while gud-gdb-fetch-lines-in-progress
4680 (accept-process-output (get-buffer-process gud-comint-buffer))))
4681 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4683 (defun gud-gdbmi-fetch-lines-filter (string)
4684 "Custom filter function for `gud-gdbmi-completions'."
4685 (setq string (concat gud-gdb-fetch-lines-string
4686 (gud-gdbmi-marker-filter string)))
4687 (while (string-match "\n" string)
4688 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4689 gud-gdb-fetched-lines)
4690 (setq string (substring string (match-end 0))))
4695 ;;; gdb-mi.el ends here