Ediff: add some missing documentation
[emacs.git] / lisp / progmodes / gdb-mi.el
blob58552759b9582780795ed81b654f98bb47e1a3d6
1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2017 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: unix, tools
9 ;; This file is part of GNU Emacs.
11 ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;;; Credits:
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".
33 ;;; Commentary:
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
42 ;; manual).
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.
51 ;; Windows Platforms:
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
56 ;; using a macro:
58 ;; #ifdef UNBUFFERED
59 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
60 ;; #endif
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
72 ;;; macOS:
74 ;; GDB in Emacs on macOS works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of macOS.
76 ;; This requires GDB version 7.0 or later as earlier versions do not
77 ;; compile on macOS.
79 ;;; Known Bugs:
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).
84 ;; TODO:
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?
89 ;;; Code:
91 (require 'gud)
92 (require 'json)
93 (require 'bindat)
94 (require 'cl-lib)
96 (declare-function speedbar-change-initial-expansion-list
97 "speedbar" (new-default))
98 (declare-function speedbar-timer-fn "speedbar" ())
99 (declare-function speedbar-line-text "speedbar" (&optional p))
100 (declare-function speedbar-change-expand-button-char "speedbar" (char))
101 (declare-function speedbar-delete-subblock "speedbar" (indent))
102 (declare-function speedbar-center-buffer-smartly "speedbar" ())
104 (defvar tool-bar-map)
105 (defvar speedbar-initial-expansion-list-name)
106 (defvar speedbar-frame)
108 (defvar gdb-memory-address "main")
109 (defvar gdb-memory-last-address nil
110 "Last successfully accessed memory address.")
111 (defvar gdb-memory-next-page nil
112 "Address of next memory page for program memory buffer.")
113 (defvar gdb-memory-prev-page nil
114 "Address of previous memory page for program memory buffer.")
116 (defvar gdb-thread-number nil
117 "Main current thread.
119 Invalidation triggers use this variable to query GDB for
120 information on the specified thread by wrapping GDB/MI commands
121 in `gdb-current-context-command'.
123 This variable may be updated implicitly by GDB via `gdb-stopped'
124 or explicitly by `gdb-select-thread'.
126 Only `gdb-setq-thread-number' should be used to change this
127 value.")
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),
137 set to nil.
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
228 every GDB session.")
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
238 for the user to see.
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."
251 :type 'boolean
252 :group 'gud
253 :version "24.4")
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
275 for this handler."
277 (push (make-gdb-handler :token-number token-number
278 :function handler-function
279 :pending-trigger pending-trigger)
280 gdb-handler-list))
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
289 (cl-delete-if
290 (lambda (handler)
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))
296 gdb-handler-list))
298 (setq gdb-handler-list
299 (cl-delete-if
300 (lambda (handler)
301 "Discard any HANDLER with a token number `eq' to TOKEN-NUMBER."
302 (eq (gdb-handler-token-number handler) token-number))
303 gdb-handler-list))))
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)
309 token-number))
310 gdb-handler-list)))
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)
316 pending-trigger))
317 gdb-handler-list))
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'."
344 `(run-with-timer
345 0.5 nil
346 '(lambda ()
347 (if (not (cl-find-if (lambda (handler)
348 (gdb-handler-pending-trigger handler))
349 gdb-handler-list))
350 (progn ,@body)
351 (gdb-wait-for-pending ,@body)))))
353 ;; Publish-subscribe
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
365 ,publisher)))
367 (defun gdb-get-subscribers (publisher)
368 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.")
380 (defgroup gdb nil
381 "GDB graphical interface"
382 :group 'tools
383 :link '(info-link "(emacs)GDB Graphical Interface")
384 :version "23.2")
386 (defgroup gdb-non-stop nil
387 "GDB non-stop debugging settings"
388 :group 'gdb
389 :version "23.2")
391 (defgroup gdb-buffers nil
392 "GDB buffers"
393 :group 'gdb
394 :version "23.2")
396 (defcustom gdb-debug-log-max 128
397 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
398 :group 'gdb
399 :type '(choice (integer :tag "Number of elements")
400 (const :tag "Unlimited" nil))
401 :version "22.1")
403 (defcustom gdb-non-stop-setting (not (eq system-type 'windows-nt))
404 "If non-nil, GDB sessions are expected to support the non-stop mode.
405 When in the non-stop mode, stopped threads can be examined while
406 other threads continue to execute.
408 If this is non-nil, GDB will be sent the \"set non-stop 1\" command,
409 and if that results in an error, the non-stop setting will be
410 turned off automatically.
412 On MS-Windows, this is off by default, because MS-Windows targets
413 don't support the non-stop mode.
415 GDB session needs to be restarted for this setting to take effect."
416 :type 'boolean
417 :group 'gdb-non-stop
418 :version "26.1")
420 ;; TODO Some commands can't be called with --all (give a notice about
421 ;; it in setting doc)
422 (defcustom gdb-gud-control-all-threads t
423 "When non-nil, GUD execution commands affect all threads when
424 in non-stop mode. Otherwise, only current thread is affected."
425 :type 'boolean
426 :group 'gdb-non-stop
427 :version "23.2")
429 (defcustom gdb-switch-reasons t
430 "List of stop reasons for which Emacs should switch thread.
431 When t, switch to stopped thread no matter what the reason was.
432 When nil, never switch to stopped thread automatically.
434 This setting is used in non-stop mode only. In all-stop mode,
435 Emacs always switches to the thread which caused the stop."
436 ;; exited, exited-normally and exited-signaled are not
437 ;; thread-specific stop reasons and therefore are not included in
438 ;; this list
439 :type '(choice
440 (const :tag "All reasons" t)
441 (set :tag "Selection of reasons..."
442 (const :tag "A breakpoint was reached." "breakpoint-hit")
443 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
444 (const :tag "A read watchpoint was triggered."
445 "read-watchpoint-trigger")
446 (const :tag "An access watchpoint was triggered."
447 "access-watchpoint-trigger")
448 (const :tag "Function finished execution." "function-finished")
449 (const :tag "Location reached." "location-reached")
450 (const :tag "Watchpoint has gone out of scope"
451 "watchpoint-scope")
452 (const :tag "End of stepping range reached."
453 "end-stepping-range")
454 (const :tag "Signal received (like interruption)."
455 "signal-received"))
456 (const :tag "None" nil))
457 :group 'gdb-non-stop
458 :version "23.2"
459 :link '(info-link "(gdb)GDB/MI Async Records"))
461 (defcustom gdb-stopped-functions nil
462 "List of functions called whenever GDB stops.
464 Each function takes one argument, a parsed MI response, which
465 contains fields of corresponding MI *stopped async record:
467 ((stopped-threads . \"all\")
468 (thread-id . \"1\")
469 (frame (line . \"38\")
470 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
471 (file . \"server.c\")
472 (args ((value . \"0x804b038\")
473 (name . \"arg\")))
474 (func . \"hello\")
475 (addr . \"0x0804869e\"))
476 (reason . \"end-stepping-range\"))
478 Note that \"reason\" is only present in non-stop debugging mode.
480 `bindat-get-field' may be used to access the fields of response.
482 Each function is called after the new current thread was selected
483 and GDB buffers were updated in `gdb-stopped'."
484 :type '(repeat function)
485 :group 'gdb
486 :version "23.2"
487 :link '(info-link "(gdb)GDB/MI Async Records"))
489 (defcustom gdb-switch-when-another-stopped t
490 "When nil, don't switch to stopped thread if some other
491 stopped thread is already selected."
492 :type 'boolean
493 :group 'gdb-non-stop
494 :version "23.2")
496 (defcustom gdb-stack-buffer-locations t
497 "Show file information or library names in stack buffers."
498 :type 'boolean
499 :group 'gdb-buffers
500 :version "23.2")
502 (defcustom gdb-stack-buffer-addresses nil
503 "Show frame addresses in stack buffers."
504 :type 'boolean
505 :group 'gdb-buffers
506 :version "23.2")
508 (defcustom gdb-thread-buffer-verbose-names t
509 "Show long thread names in threads buffer."
510 :type 'boolean
511 :group 'gdb-buffers
512 :version "23.2")
514 (defcustom gdb-thread-buffer-arguments t
515 "Show function arguments in threads buffer."
516 :type 'boolean
517 :group 'gdb-buffers
518 :version "23.2")
520 (defcustom gdb-thread-buffer-locations t
521 "Show file information or library names in threads buffer."
522 :type 'boolean
523 :group 'gdb-buffers
524 :version "23.2")
526 (defcustom gdb-thread-buffer-addresses nil
527 "Show addresses for thread frames in threads buffer."
528 :type 'boolean
529 :group 'gdb-buffers
530 :version "23.2")
532 (defcustom gdb-show-threads-by-default nil
533 "Show threads list buffer instead of breakpoints list by default."
534 :type 'boolean
535 :group 'gdb-buffers
536 :version "23.2")
538 (defvar gdb-debug-log nil
539 "List of commands sent to and replies received from GDB.
540 Most recent commands are listed first. This list stores only the last
541 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
543 ;;;###autoload
544 (define-minor-mode gdb-enable-debug
545 "Toggle logging of transaction between Emacs and Gdb.
546 The log is stored in `gdb-debug-log' as an alist with elements
547 whose cons is send, send-item or recv and whose cdr is the string
548 being transferred. This list may grow up to a size of
549 `gdb-debug-log-max' after which the oldest element (at the end of
550 the list) is deleted every time a new one is added (at the front)."
551 :global t
552 :group 'gdb
553 :version "22.1")
555 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
556 "Shell command for generating a list of defined macros in a source file.
557 This list is used to display the #define directive associated
558 with an identifier as a tooltip. It works in a debug session with
559 GDB, when `gud-tooltip-mode' is t.
561 Set `gdb-cpp-define-alist-flags' for any include paths or
562 predefined macros."
563 :type 'string
564 :group 'gdb
565 :version "22.1")
567 (defcustom gdb-cpp-define-alist-flags ""
568 "Preprocessor flags for `gdb-cpp-define-alist-program'."
569 :type 'string
570 :group 'gdb
571 :version "22.1")
573 (defcustom gdb-create-source-file-list t
574 "Non-nil means create a list of files from which the executable was built.
575 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
576 line for a long time when starting, possibly because your executable was
577 built from a large number of files. This allows quicker initialization
578 but means that these files are not automatically enabled for debugging,
579 e.g., you won't be able to click in the fringe to set a breakpoint until
580 execution has already stopped there."
581 :type 'boolean
582 :group 'gdb
583 :version "23.1")
585 (defcustom gdb-show-main nil
586 "Non-nil means display source file containing the main routine at startup.
587 Also display the main routine in the disassembly buffer if present."
588 :type 'boolean
589 :group 'gdb
590 :version "22.1")
592 (defvar gdbmi-debug-mode nil
593 "When non-nil, print the messages sent/received from GDB/MI in *Messages*.")
595 (defun gdb-force-mode-line-update (status)
596 (let ((buffer gud-comint-buffer))
597 (if (and buffer (buffer-name buffer))
598 (with-current-buffer buffer
599 (setq mode-line-process
600 (format ":%s [%s]"
601 (process-status (get-buffer-process buffer)) status))
602 ;; Force mode line redisplay soon.
603 (force-mode-line-update)))))
605 ;; These two are used for menu and toolbar
606 (defun gdb-control-all-threads ()
607 "Switch to non-stop/A mode."
608 (interactive)
609 (setq gdb-gud-control-all-threads t)
610 ;; Actually forcing the tool-bar to update.
611 (force-mode-line-update)
612 (message "Now in non-stop/A mode."))
614 (defun gdb-control-current-thread ()
615 "Switch to non-stop/T mode."
616 (interactive)
617 (setq gdb-gud-control-all-threads nil)
618 ;; Actually forcing the tool-bar to update.
619 (force-mode-line-update)
620 (message "Now in non-stop/T mode."))
622 (defun gdb-find-watch-expression ()
623 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
624 (varnum (car var)) expr)
625 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
626 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
627 (component-list (split-string (match-string 2 varnum) "\\." t)))
628 (setq expr (nth 1 var1))
629 (setq varnumlet (car var1))
630 (dolist (component component-list)
631 (setq var2 (assoc varnumlet gdb-var-list))
632 (setq expr (concat expr
633 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
634 (concat "[" component "]")
635 (concat "." component))))
636 (setq varnumlet (concat varnumlet "." component)))
637 expr)))
639 ;; noall is used for commands which don't take --all, but only
640 ;; --thread.
641 (defun gdb-gud-context-command (command &optional noall)
642 "When `gdb-non-stop' is t, add --thread option to COMMAND if
643 `gdb-gud-control-all-threads' is nil and --all option otherwise.
644 If NOALL is t, always add --thread option no matter what
645 `gdb-gud-control-all-threads' value is.
647 When `gdb-non-stop' is nil, return COMMAND unchanged."
648 (if gdb-non-stop
649 (if (and gdb-gud-control-all-threads
650 (not noall)
651 gdb-supports-non-stop)
652 (concat command " --all ")
653 (gdb-current-context-command command))
654 command))
656 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
657 "`gud-call' wrapper which adds --thread/--all options between
658 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
660 NOARG must be t when this macro is used outside `gud-def'"
661 `(gud-call
662 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
663 ,(when (not noarg) 'arg)))
665 (defun gdb--check-interpreter (filter proc string)
666 (unless (zerop (length string))
667 (remove-function (process-filter proc) #'gdb--check-interpreter)
668 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
669 ;; Apparently we're not running with -i=mi.
670 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
671 (message msg)
672 (setq string (concat (propertize msg 'font-lock-face 'error)
673 "\n" string)))
674 ;; Use the old gud-gbd filter, not because it works, but because it
675 ;; will properly display GDB's answers rather than hanging waiting for
676 ;; answers that aren't coming.
677 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
678 (funcall filter proc string)))
680 (defvar gdb-control-level 0)
682 ;;;###autoload
683 (defun gdb (command-line)
684 "Run gdb passing it COMMAND-LINE as arguments.
686 If COMMAND-LINE names a program FILE to debug, gdb will run in
687 a buffer named *gud-FILE*, and the directory containing FILE
688 becomes the initial working directory and source-file directory
689 for your debugger.
690 If COMMAND-LINE requests that gdb attaches to a process PID, gdb
691 will run in *gud-PID*, otherwise it will run in *gud*; in these
692 cases the initial working directory is the default-directory of
693 the buffer in which this command was invoked.
695 COMMAND-LINE should include \"-i=mi\" to use gdb's MI text interface.
696 Note that the old \"--annotate\" option is no longer supported.
698 If option `gdb-many-windows' is nil (the default value) then gdb just
699 pops up the GUD buffer unless `gdb-show-main' is t. In this case
700 it starts with two windows: one displaying the GUD buffer and the
701 other with the source file with the main routine of the inferior.
703 If option `gdb-many-windows' is t, regardless of the value of
704 `gdb-show-main', the layout below will appear. Keybindings are
705 shown in some of the buffers.
707 Watch expressions appear in the speedbar/slowbar.
709 The following commands help control operation :
711 `gdb-many-windows' - Toggle the number of windows gdb uses.
712 `gdb-restore-windows' - To restore the window layout.
714 See Info node `(emacs)GDB Graphical Interface' for a more
715 detailed description of this mode.
718 +----------------------------------------------------------------------+
719 | GDB Toolbar |
720 +-----------------------------------+----------------------------------+
721 | GUD buffer (I/O of GDB) | Locals buffer |
722 | | |
723 | | |
724 | | |
725 +-----------------------------------+----------------------------------+
726 | Source buffer | I/O buffer (of debugged program) |
727 | | (comint-mode) |
728 | | |
729 | | |
730 | | |
731 | | |
732 | | |
733 | | |
734 +-----------------------------------+----------------------------------+
735 | Stack buffer | Breakpoints buffer |
736 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
737 | | RET gdb-goto-breakpoint |
738 | | D gdb-delete-breakpoint |
739 +-----------------------------------+----------------------------------+"
741 (interactive (list (gud-query-cmdline 'gdb)))
743 (when (and gud-comint-buffer
744 (buffer-name gud-comint-buffer)
745 (get-buffer-process gud-comint-buffer)
746 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
747 (gdb-restore-windows)
748 (error
749 "Multiple debugging requires restarting in text command mode"))
751 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
753 ;; Setup a temporary process filter to warn when GDB was not started
754 ;; with -i=mi.
755 (let ((proc (get-buffer-process gud-comint-buffer)))
756 (add-function :around (process-filter proc) #'gdb--check-interpreter))
758 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
759 (set (make-local-variable 'gdb-control-level) 0)
760 (setq comint-input-sender 'gdb-send)
761 (when (ring-empty-p comint-input-ring) ; cf shell-mode
762 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
763 (if (eq system-type 'ms-dos)
764 "_gdb_history"
765 ".gdb_history"))))
766 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
767 (hsize (getenv "HISTSIZE")))
768 (dolist (file (append '("~/.gdbinit")
769 (unless (string-equal (expand-file-name ".")
770 (expand-file-name "~"))
771 '(".gdbinit"))))
772 (if (file-readable-p (setq file (expand-file-name file)))
773 (with-temp-buffer
774 (insert-file-contents file)
775 ;; TODO? check for "set history save\\( *on\\)?" and do
776 ;; not use history otherwise?
777 (while (re-search-forward
778 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
779 (cond ((string-equal (match-string 1) "filename")
780 (setq hfile (expand-file-name
781 (match-string 2)
782 (file-name-directory file))))
783 ((string-equal (match-string 1) "size")
784 (setq hsize (match-string 2))))))))
785 (and (stringp hsize)
786 (integerp (setq hsize (string-to-number hsize)))
787 (> hsize 0)
788 (set (make-local-variable 'comint-input-ring-size) hsize))
789 (if (stringp hfile)
790 (set (make-local-variable 'comint-input-ring-file-name) hfile))
791 (comint-read-input-ring t)))
792 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
793 "Set temporary breakpoint at current line.")
794 (gud-def gud-jump
795 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
796 "\C-j" "Set execution address to current line.")
798 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
799 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
800 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
801 (gud-def gud-pstar "print* %e" nil
802 "Evaluate C dereferenced pointer expression at point.")
804 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
805 "\C-s"
806 "Step one source line with display.")
807 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
808 "\C-i"
809 "Step one instruction with display.")
810 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
811 "\C-n"
812 "Step one line (skip functions).")
813 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
815 "Step one instruction (skip functions).")
816 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
817 "\C-r"
818 "Continue with display.")
819 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
820 "\C-f"
821 "Finish executing current function.")
822 (gud-def gud-run "-exec-run"
824 "Run the program.")
826 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
827 (gud-call "break %f:%l" arg)
828 (save-excursion
829 (beginning-of-line)
830 (forward-char 2)
831 (gud-call "break *%a" arg)))
832 "\C-b" "Set breakpoint at current line or address.")
834 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
835 (gud-call "clear %f:%l" arg)
836 (save-excursion
837 (beginning-of-line)
838 (forward-char 2)
839 (gud-call "clear *%a" arg)))
840 "\C-d" "Remove breakpoint at current line or address.")
842 ;; -exec-until doesn't support --all yet
843 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
844 (gud-call "-exec-until %f:%l" arg)
845 (save-excursion
846 (beginning-of-line)
847 (forward-char 2)
848 (gud-call "-exec-until *%a" arg)))
849 "\C-u" "Continue to current line or address.")
850 ;; TODO Why arg here?
851 (gud-def
852 gud-go (gud-call (if gdb-active-process
853 (gdb-gud-context-command "-exec-continue")
854 "-exec-run") arg)
855 nil "Start or continue execution.")
857 ;; For debugging Emacs only.
858 (gud-def gud-pp
859 (gud-call
860 (concat
861 "pp " (if (eq (buffer-local-value
862 'major-mode (window-buffer)) 'speedbar-mode)
863 (gdb-find-watch-expression) "%e")) arg)
864 nil "Print the Emacs s-expression.")
866 (define-key gud-minor-mode-map [left-margin mouse-1]
867 'gdb-mouse-set-clear-breakpoint)
868 (define-key gud-minor-mode-map [left-fringe mouse-1]
869 'gdb-mouse-set-clear-breakpoint)
870 (define-key gud-minor-mode-map [left-margin C-mouse-1]
871 'gdb-mouse-toggle-breakpoint-margin)
872 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
873 'gdb-mouse-toggle-breakpoint-fringe)
875 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
876 'gdb-mouse-until)
877 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
878 'gdb-mouse-until)
879 (define-key gud-minor-mode-map [left-margin mouse-3]
880 'gdb-mouse-until)
881 (define-key gud-minor-mode-map [left-fringe mouse-3]
882 'gdb-mouse-until)
884 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
885 'gdb-mouse-jump)
886 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
887 'gdb-mouse-jump)
888 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
889 'gdb-mouse-jump)
890 (define-key gud-minor-mode-map [left-margin C-mouse-3]
891 'gdb-mouse-jump)
893 (set (make-local-variable 'gud-gdb-completion-function)
894 'gud-gdbmi-completions)
896 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
897 nil 'local)
898 (local-set-key "\C-i" 'completion-at-point)
900 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
902 (setq gdb-first-prompt t)
903 (setq gud-running nil)
905 (gdb-update)
907 (run-hooks 'gdb-mode-hook))
909 (defun gdb-init-1 ()
910 ;; (Re-)initialize.
911 (setq gdb-selected-frame nil
912 gdb-frame-number nil
913 gdb-thread-number nil
914 gdb-var-list nil
915 gdb-output-sink 'user
916 gdb-location-alist nil
917 gdb-source-file-list nil
918 gdb-last-command nil
919 gdb-token-number 0
920 gdb-handler-list '()
921 gdb-prompt-name nil
922 gdb-first-done-or-error t
923 gdb-buffer-fringe-width (car (window-fringes))
924 gdb-debug-log nil
925 gdb-source-window nil
926 gdb-inferior-status nil
927 gdb-continuation nil
928 gdb-buf-publisher '()
929 gdb-threads-list '()
930 gdb-breakpoints-list '()
931 gdb-register-names '()
932 gdb-non-stop gdb-non-stop-setting)
934 (gdbmi-bnf-init)
936 (setq gdb-buffer-type 'gdbmi)
938 (gdb-force-mode-line-update
939 (propertize "initializing..." 'face font-lock-variable-name-face))
941 (gdb-get-buffer-create 'gdb-inferior-io)
942 (gdb-clear-inferior-io)
943 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
945 (when (eq system-type 'windows-nt)
946 ;; Don't create a separate console window for the debuggee.
947 (gdb-input "-gdb-set new-console off" 'ignore)
948 ;; Force GDB to behave as if its input and output stream were
949 ;; connected to a TTY device (since on Windows we use pipes for
950 ;; communicating with GDB).
951 (gdb-input "-gdb-set interactive-mode on" 'ignore))
952 (gdb-input "-gdb-set height 0" 'ignore)
954 (when gdb-non-stop
955 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
957 (gdb-input "-enable-pretty-printing" 'ignore)
959 ;; Find source file and compilation directory here.
960 (if gdb-create-source-file-list
961 ;; Needs GDB 6.2 onwards.
962 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
963 ;; Needs GDB 6.0 onwards.
964 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
965 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
967 (defun gdb-non-stop-handler ()
968 (goto-char (point-min))
969 (if (re-search-forward "No symbol" nil t)
970 (progn
971 (message
972 "This version of GDB doesn't support non-stop mode. Turning it off.")
973 (setq gdb-non-stop nil)
974 (setq gdb-supports-non-stop nil))
975 (setq gdb-supports-non-stop t)
976 (gdb-input "-gdb-set target-async 1" 'ignore)
977 (gdb-input "-list-target-features" 'gdb-check-target-async)))
979 (defun gdb-check-target-async ()
980 (goto-char (point-min))
981 (unless (re-search-forward "async" nil t)
982 (message
983 "Target doesn't support non-stop mode. Turning it off.")
984 (setq gdb-non-stop nil)
985 (gdb-input "-gdb-set non-stop 0" 'ignore)))
987 (defun gdb-delchar-or-quit (arg)
988 "Delete ARG characters or send a quit command to GDB.
989 Send a quit only if point is at the end of the buffer, there is
990 no input, and GDB is waiting for input."
991 (interactive "p")
992 (unless (and (eq (current-buffer) gud-comint-buffer)
993 (eq gud-minor-mode 'gdbmi))
994 (error "Not in a GDB-MI buffer"))
995 (let ((proc (get-buffer-process gud-comint-buffer)))
996 (if (and (eobp)
997 (process-live-p proc)
998 (not gud-running)
999 (= (point) (marker-position (process-mark proc))))
1000 ;; Sending an EOF does not work with GDB-MI; submit an
1001 ;; explicit quit command.
1002 (progn
1003 (insert "quit")
1004 (comint-send-input t t))
1005 (delete-char arg))))
1007 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
1009 (defun gdb-create-define-alist ()
1010 "Create an alist of #define directives for GUD tooltips."
1011 (let* ((file (buffer-file-name))
1012 (output
1013 (with-output-to-string
1014 (with-current-buffer standard-output
1015 (and file
1016 (file-exists-p file)
1017 ;; call-process doesn't work with remote file names.
1018 (not (file-remote-p default-directory))
1019 (call-process shell-file-name file
1020 (list t nil) nil "-c"
1021 (concat gdb-cpp-define-alist-program " "
1022 gdb-cpp-define-alist-flags))))))
1023 (define-list (split-string output "\n" t))
1024 (name))
1025 (setq gdb-define-alist nil)
1026 (dolist (define define-list)
1027 (setq name (nth 1 (split-string define "[( ]")))
1028 (push (cons name define) gdb-define-alist))))
1030 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
1032 (defconst gdb--string-regexp "\"\\(?:[^\\\"]\\|\\\\.\\)*\"")
1034 (defun gdb-tooltip-print (expr)
1035 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
1036 (goto-char (point-min))
1037 (cond
1038 ((re-search-forward (concat ".*value=\\(" gdb--string-regexp
1039 "\\)")
1040 nil t)
1041 (tooltip-show
1042 (concat expr " = " (read (match-string 1)))
1043 (or gud-tooltip-echo-area
1044 (not (display-graphic-p)))))
1045 ((re-search-forward "msg=\\(\".+\"\\)$" nil t)
1046 (tooltip-show (read (match-string 1))
1047 (or gud-tooltip-echo-area
1048 (not (display-graphic-p))))))))
1050 ;; If expr is a macro for a function don't print because of possible dangerous
1051 ;; side-effects. Also printing a function within a tooltip generates an
1052 ;; unexpected starting annotation (phase error).
1053 (defun gdb-tooltip-print-1 (expr)
1054 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
1055 (goto-char (point-min))
1056 (if (search-forward "expands to: " nil t)
1057 (unless (looking-at "\\S-+.*(.*).*")
1058 (gdb-input (concat "-data-evaluate-expression \"" expr "\"")
1059 `(lambda () (gdb-tooltip-print ,expr)))))))
1061 (defun gdb-init-buffer ()
1062 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
1063 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1064 (when gud-tooltip-mode
1065 (make-local-variable 'gdb-define-alist)
1066 (gdb-create-define-alist)
1067 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
1069 (defmacro gdb--if-arrow (arrow-position start-posn end-posn &rest body)
1070 (declare (indent 3))
1071 (let ((buffer (make-symbol "buffer")))
1072 `(if ,arrow-position
1073 (let ((,buffer (marker-buffer ,arrow-position)))
1074 (if (equal ,buffer (window-buffer (posn-window ,end-posn)))
1075 (with-current-buffer ,buffer
1076 (when (or (equal ,start-posn ,end-posn)
1077 (equal (posn-point ,start-posn)
1078 (marker-position ,arrow-position)))
1079 ,@body)))))))
1081 (defun gdb-mouse-until (event)
1082 "Continue running until a source line past the current line.
1083 The destination source line can be selected either by clicking
1084 with mouse-3 on the fringe/margin or dragging the arrow
1085 with mouse-1 (default bindings)."
1086 (interactive "e")
1087 (let ((start (event-start event))
1088 (end (event-end event)))
1089 (gdb--if-arrow gud-overlay-arrow-position start end
1090 (let ((line (line-number-at-pos (posn-point end))))
1091 (gud-call (concat "until " (number-to-string line)))))
1092 (gdb--if-arrow gdb-disassembly-position start end
1093 (save-excursion
1094 (goto-char (point-min))
1095 (forward-line (1- (line-number-at-pos (posn-point end))))
1096 (forward-char 2)
1097 (gud-call (concat "until *%a"))))))
1099 (defun gdb-mouse-jump (event)
1100 "Set execution address/line.
1101 The destination source line can be selected either by clicking with C-mouse-3
1102 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1103 Unlike `gdb-mouse-until' the destination address can be before the current
1104 line, and no execution takes place."
1105 (interactive "e")
1106 (let ((start (event-start event))
1107 (end (event-end event)))
1108 (gdb--if-arrow gud-overlay-arrow-position start end
1109 (let ((line (line-number-at-pos (posn-point end))))
1110 (gud-call (concat "tbreak " (number-to-string line)))
1111 (gud-call (concat "jump " (number-to-string line)))))
1112 (gdb--if-arrow gdb-disassembly-position start end
1113 (save-excursion
1114 (goto-char (point-min))
1115 (forward-line (1- (line-number-at-pos (posn-point end))))
1116 (forward-char 2)
1117 (gud-call (concat "tbreak *%a"))
1118 (gud-call (concat "jump *%a"))))))
1120 (defcustom gdb-show-changed-values t
1121 "If non-nil change the face of out of scope variables and changed values.
1122 Out of scope variables are suppressed with `shadow' face.
1123 Changed values are highlighted with the face `font-lock-warning-face'."
1124 :type 'boolean
1125 :group 'gdb
1126 :version "22.1")
1128 (defcustom gdb-max-children 40
1129 "Maximum number of children before expansion requires confirmation."
1130 :type 'integer
1131 :group 'gdb
1132 :version "22.1")
1134 (defcustom gdb-delete-out-of-scope t
1135 "If non-nil delete watch expressions automatically when they go out of scope."
1136 :type 'boolean
1137 :group 'gdb
1138 :version "22.2")
1140 (define-minor-mode gdb-speedbar-auto-raise
1141 "Minor mode to automatically raise the speedbar for watch expressions.
1142 With prefix argument ARG, automatically raise speedbar if ARG is
1143 positive, otherwise don't automatically raise it."
1144 :global t
1145 :group 'gdb
1146 :version "22.1")
1148 (defcustom gdb-use-colon-colon-notation nil
1149 "If non-nil use FUN::VAR format to display variables in the speedbar."
1150 :type 'boolean
1151 :group 'gdb
1152 :version "22.1")
1154 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1155 (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
1157 (declare-function tooltip-identifier-from-point "tooltip" (point))
1159 (defun gud-watch (&optional arg event)
1160 "Watch expression at point.
1161 With arg, enter name of variable to be watched in the minibuffer."
1162 (interactive (list current-prefix-arg last-input-event))
1163 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1164 (if (eq minor-mode 'gdbmi)
1165 (progn
1166 (if event (posn-set-point (event-end event)))
1167 (require 'tooltip)
1168 (save-selected-window
1169 (let ((expr
1170 (if arg
1171 (completing-read "Name of variable: "
1172 'gud-gdb-complete-command)
1173 (if (and transient-mark-mode mark-active)
1174 (buffer-substring (region-beginning) (region-end))
1175 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1176 (tooltip-identifier-from-point (point)))))))
1177 (set-text-properties 0 (length expr) nil expr)
1178 (gdb-input (concat "-var-create - * " expr "")
1179 `(lambda () (gdb-var-create-handler ,expr))))))
1180 (message "gud-watch is a no-op in this mode."))))
1182 (defun gdb-var-create-handler (expr)
1183 (let* ((result (gdb-json-partial-output)))
1184 (if (not (bindat-get-field result 'msg))
1185 (let ((var
1186 (list (bindat-get-field result 'name)
1187 (if (and (string-equal gdb-current-language "c")
1188 gdb-use-colon-colon-notation gdb-selected-frame)
1189 (setq expr (concat gdb-selected-frame "::" expr))
1190 expr)
1191 (bindat-get-field result 'numchild)
1192 (bindat-get-field result 'type)
1193 (bindat-get-field result 'value)
1195 (bindat-get-field result 'has_more)
1196 gdb-frame-address)))
1197 (push var gdb-var-list)
1198 (speedbar 1)
1199 (unless (string-equal
1200 speedbar-initial-expansion-list-name "GUD")
1201 (speedbar-change-initial-expansion-list "GUD")))
1202 (message-box "No symbol \"%s\" in current context." expr))))
1204 (defun gdb-speedbar-update ()
1205 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1206 ;; Dummy command to update speedbar even when idle.
1207 (gdb-input "-environment-pwd"
1208 'gdb-speedbar-timer-fn
1209 'gdb-speedbar-update)))
1211 (defun gdb-speedbar-timer-fn ()
1212 (if gdb-speedbar-auto-raise
1213 (raise-frame speedbar-frame))
1214 (speedbar-timer-fn))
1216 (defun gdb-var-evaluate-expression-handler (varnum changed)
1217 (goto-char (point-min))
1218 (re-search-forward (concat ".*value=\\(" gdb--string-regexp "\\)")
1219 nil t)
1220 (let ((var (assoc varnum gdb-var-list)))
1221 (when var
1222 (if changed (setcar (nthcdr 5 var) 'changed))
1223 (setcar (nthcdr 4 var) (read (match-string 1)))))
1224 (gdb-speedbar-update))
1226 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1227 (defun gdb-var-list-children (varnum)
1228 (gdb-input (concat "-var-update " varnum) 'ignore)
1229 (gdb-input (concat "-var-list-children --all-values " varnum)
1230 `(lambda () (gdb-var-list-children-handler ,varnum))))
1232 (defun gdb-var-list-children-handler (varnum)
1233 (let* ((var-list nil)
1234 (output (bindat-get-field (gdb-json-partial-output "child")))
1235 (children (bindat-get-field output 'children)))
1236 (catch 'child-already-watched
1237 (dolist (var gdb-var-list)
1238 (if (string-equal varnum (car var))
1239 (progn
1240 ;; With dynamic varobjs numchild may have increased.
1241 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1242 (push var var-list)
1243 (dolist (child children)
1244 (let ((varchild (list (bindat-get-field child 'name)
1245 (bindat-get-field child 'exp)
1246 (bindat-get-field child 'numchild)
1247 (bindat-get-field child 'type)
1248 (bindat-get-field child 'value)
1250 (bindat-get-field child 'has_more))))
1251 (if (assoc (car varchild) gdb-var-list)
1252 (throw 'child-already-watched nil))
1253 (push varchild var-list))))
1254 (push var var-list)))
1255 (setq gdb-var-list (nreverse var-list))))
1256 (gdb-speedbar-update))
1258 (defun gdb-var-set-format (format)
1259 "Set the output format for a variable displayed in the speedbar."
1260 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1261 (varnum (car var)))
1262 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1263 (gdb-var-update)))
1265 (defun gdb-var-delete-1 (var varnum)
1266 (gdb-input (concat "-var-delete " varnum) 'ignore)
1267 (setq gdb-var-list (delq var gdb-var-list))
1268 (dolist (varchild gdb-var-list)
1269 (if (string-match (concat (car var) "\\.") (car varchild))
1270 (setq gdb-var-list (delq varchild gdb-var-list)))))
1272 (defun gdb-var-delete ()
1273 "Delete watch expression at point from the speedbar."
1274 (interactive)
1275 (let ((text (speedbar-line-text)))
1276 (string-match "\\(\\S-+\\)" text)
1277 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1278 (varnum (car var)))
1279 (if (string-match "\\." (car var))
1280 (message-box "Can only delete a root expression")
1281 (gdb-var-delete-1 var varnum)))))
1283 (defun gdb-var-delete-children (varnum)
1284 "Delete children of variable object at point from the speedbar."
1285 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1287 (defun gdb-edit-value (_text _token _indent)
1288 "Assign a value to a variable displayed in the speedbar."
1289 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1290 (varnum (car var))
1291 (value (read-string "New value: ")))
1292 (gdb-input (concat "-var-assign " varnum " " value)
1293 `(lambda () (gdb-edit-value-handler ,value)))))
1295 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1297 (defun gdb-edit-value-handler (value)
1298 (goto-char (point-min))
1299 (if (re-search-forward gdb-error-regexp nil t)
1300 (message-box "Invalid number or expression (%s)" value)))
1302 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1303 (defun gdb-var-update ()
1304 (gdb-input "-var-update --all-values *"
1305 'gdb-var-update-handler
1306 'gdb-var-update))
1308 (defun gdb-var-update-handler ()
1309 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1310 (dolist (var gdb-var-list)
1311 (setcar (nthcdr 5 var) nil))
1312 (let ((temp-var-list gdb-var-list))
1313 (dolist (change changelist)
1314 (let* ((varnum (bindat-get-field change 'name))
1315 (var (assoc varnum gdb-var-list))
1316 (new-num (bindat-get-field change 'new_num_children)))
1317 (when var
1318 (let ((scope (bindat-get-field change 'in_scope))
1319 (has-more (bindat-get-field change 'has_more)))
1320 (cond ((string-equal scope "false")
1321 (if gdb-delete-out-of-scope
1322 (gdb-var-delete-1 var varnum)
1323 (setcar (nthcdr 5 var) 'out-of-scope)))
1324 ((string-equal scope "true")
1325 (setcar (nthcdr 6 var) has-more)
1326 (when (and (or (not has-more)
1327 (string-equal has-more "0"))
1328 (not new-num)
1329 (string-equal (nth 2 var) "0"))
1330 (setcar (nthcdr 4 var)
1331 (bindat-get-field change 'value))
1332 (setcar (nthcdr 5 var) 'changed)))
1333 ((string-equal scope "invalid")
1334 (gdb-var-delete-1 var varnum)))))
1335 (let ((var-list nil) var1
1336 (children (bindat-get-field change 'new_children)))
1337 (when new-num
1338 (setq var1 (pop temp-var-list))
1339 (while var1
1340 (if (string-equal varnum (car var1))
1341 (let ((new (string-to-number new-num))
1342 (previous (string-to-number (nth 2 var1))))
1343 (setcar (nthcdr 2 var1) new-num)
1344 (push var1 var-list)
1345 (cond
1346 ((> new previous)
1347 ;; Add new children to list.
1348 (dotimes (_ previous)
1349 (push (pop temp-var-list) var-list))
1350 (dolist (child children)
1351 (let ((varchild
1352 (list (bindat-get-field child 'name)
1353 (bindat-get-field child 'exp)
1354 (bindat-get-field child 'numchild)
1355 (bindat-get-field child 'type)
1356 (bindat-get-field child 'value)
1357 'changed
1358 (bindat-get-field child 'has_more))))
1359 (push varchild var-list))))
1360 ;; Remove deleted children from list.
1361 ((< new previous)
1362 (dotimes (_ new)
1363 (push (pop temp-var-list) var-list))
1364 (dotimes (_ (- previous new))
1365 (pop temp-var-list)))))
1366 (push var1 var-list))
1367 (setq var1 (pop temp-var-list)))
1368 (setq gdb-var-list (nreverse var-list))))))))
1369 (gdb-speedbar-update))
1371 (defun gdb-speedbar-expand-node (text token indent)
1372 "Expand the node the user clicked on.
1373 TEXT is the text of the button we clicked on, a + or - item.
1374 TOKEN is data related to this node.
1375 INDENT is the current indentation depth."
1376 (cond ((string-match "+" text) ;expand this node
1377 (let* ((var (assoc token gdb-var-list))
1378 (expr (nth 1 var)) (children (nth 2 var)))
1379 (if (or (<= (string-to-number children) gdb-max-children)
1380 (y-or-n-p
1381 (format "%s has %s children. Continue? " expr children)))
1382 (gdb-var-list-children token))))
1383 ((string-match "-" text) ;contract this node
1384 (dolist (var gdb-var-list)
1385 (if (string-match (concat token "\\.") (car var))
1386 (setq gdb-var-list (delq var gdb-var-list))))
1387 (gdb-var-delete-children token)
1388 (speedbar-change-expand-button-char ?+)
1389 (speedbar-delete-subblock indent))
1390 (t (error "Ooops... not sure what to do")))
1391 (speedbar-center-buffer-smartly))
1393 (defun gdb-get-target-string ()
1394 (with-current-buffer gud-comint-buffer
1395 gud-target-name))
1399 ;; gdb buffers.
1401 ;; Each buffer has a TYPE -- a symbol that identifies the function
1402 ;; of that particular buffer.
1404 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1405 ;; is constructed specially.
1407 ;; Others are constructed by gdb-get-buffer-create and
1408 ;; named according to the rules set forth in the gdb-buffer-rules
1410 (defvar gdb-buffer-rules '())
1412 (defun gdb-rules-name-maker (rules-entry)
1413 (cadr rules-entry))
1414 (defun gdb-rules-buffer-mode (rules-entry)
1415 (nth 2 rules-entry))
1416 (defun gdb-rules-update-trigger (rules-entry)
1417 (nth 3 rules-entry))
1419 (defun gdb-update-buffer-name ()
1420 "Rename current buffer according to name-maker associated with
1421 it in `gdb-buffer-rules'."
1422 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1423 gdb-buffer-rules))))
1424 (when f (rename-buffer (funcall f)))))
1426 (defun gdb-current-buffer-rules ()
1427 "Get `gdb-buffer-rules' entry for current buffer type."
1428 (assoc gdb-buffer-type gdb-buffer-rules))
1430 (defun gdb-current-buffer-thread ()
1431 "Get thread object of current buffer from `gdb-threads-list'.
1433 When current buffer is not bound to any thread, return main
1434 thread."
1435 (cdr (assoc gdb-thread-number gdb-threads-list)))
1437 (defun gdb-current-buffer-frame ()
1438 "Get current stack frame object for thread of current buffer."
1439 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1441 (defun gdb-buffer-type (buffer)
1442 "Get value of `gdb-buffer-type' for BUFFER."
1443 (with-current-buffer buffer
1444 gdb-buffer-type))
1446 (defun gdb-buffer-shows-main-thread-p ()
1447 "Return t if current GDB buffer shows main selected thread and
1448 is not bound to it."
1449 (current-buffer)
1450 (not (local-variable-p 'gdb-thread-number)))
1452 (defun gdb-get-buffer (buffer-type &optional thread)
1453 "Get a specific GDB buffer.
1455 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1456 and `gdb-thread-number' (if provided) must be equal to THREAD."
1457 (catch 'found
1458 (dolist (buffer (buffer-list) nil)
1459 (with-current-buffer buffer
1460 (when (and (eq gdb-buffer-type buffer-type)
1461 (or (not thread)
1462 (equal gdb-thread-number thread)))
1463 (throw 'found buffer))))))
1465 (defun gdb-get-buffer-create (buffer-type &optional thread)
1466 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1467 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1469 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1470 buffer-local variable of the new buffer.
1472 Buffer mode and name are selected according to buffer type.
1474 If buffer has trigger associated with it in `gdb-buffer-rules',
1475 this trigger is subscribed to `gdb-buf-publisher' and called with
1476 'update argument."
1477 (or (gdb-get-buffer buffer-type thread)
1478 (let ((rules (assoc buffer-type gdb-buffer-rules))
1479 (new (generate-new-buffer "limbo")))
1480 (with-current-buffer new
1481 (let ((mode (gdb-rules-buffer-mode rules))
1482 (trigger (gdb-rules-update-trigger rules)))
1483 (when mode (funcall mode))
1484 (setq gdb-buffer-type buffer-type)
1485 (when thread
1486 (set (make-local-variable 'gdb-thread-number) thread))
1487 (set (make-local-variable 'gud-minor-mode)
1488 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1489 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1490 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1491 (when trigger
1492 (gdb-add-subscriber gdb-buf-publisher
1493 (cons (current-buffer)
1494 (gdb-bind-function-to-buffer
1495 trigger (current-buffer))))
1496 (funcall trigger 'start))
1497 (current-buffer))))))
1499 (defun gdb-bind-function-to-buffer (expr buffer)
1500 "Return a function which will evaluate EXPR in BUFFER."
1501 `(lambda (&rest args)
1502 (with-current-buffer ,buffer
1503 (apply ',expr args))))
1505 ;; Used to display windows with thread-bound buffers
1506 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1507 split-horizontal)
1508 `(defun ,name (&optional thread)
1509 ,(when doc doc)
1510 (message "%s" thread)
1511 (gdb-preempt-existing-or-display-buffer
1512 (gdb-get-buffer-create ,buffer thread)
1513 ,split-horizontal)))
1515 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1516 ;; at least one and possible more functions. The functions have these
1517 ;; roles in defining a buffer type:
1519 ;; NAME - Return a name for this buffer type.
1521 ;; The remaining function(s) are optional:
1523 ;; MODE - called in a new buffer with no arguments, should establish
1524 ;; the proper mode for the buffer.
1527 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1528 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1529 (if binding
1530 (setcdr binding rules)
1531 (push (cons buffer-type rules)
1532 gdb-buffer-rules))))
1534 (defun gdb-parent-mode ()
1535 "Generic mode to derive all other GDB buffer modes from."
1536 (kill-all-local-variables)
1537 (setq buffer-read-only t)
1538 (buffer-disable-undo)
1539 ;; Delete buffer from gdb-buf-publisher when it's killed
1540 ;; (if it has an associated update trigger)
1541 (add-hook
1542 'kill-buffer-hook
1543 (function
1544 (lambda ()
1545 (let ((trigger (gdb-rules-update-trigger
1546 (gdb-current-buffer-rules))))
1547 (when trigger
1548 (gdb-delete-subscriber
1549 gdb-buf-publisher
1550 ;; This should match gdb-add-subscriber done in
1551 ;; gdb-get-buffer-create
1552 (cons (current-buffer)
1553 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1554 nil t))
1556 ;; Partial-output buffer : This accumulates output from a command executed on
1557 ;; behalf of emacs (rather than the user).
1559 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1560 'gdb-partial-output-name)
1562 (defun gdb-partial-output-name ()
1563 (concat " *partial-output-"
1564 (gdb-get-target-string)
1565 "*"))
1568 (gdb-set-buffer-rules 'gdb-inferior-io
1569 'gdb-inferior-io-name
1570 'gdb-inferior-io-mode)
1572 (defun gdb-inferior-io-name ()
1573 (concat "*input/output of "
1574 (gdb-get-target-string)
1575 "*"))
1577 (defun gdb-display-io-buffer ()
1578 "Display IO of debugged program in a separate window."
1579 (interactive)
1580 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
1582 (defun gdb-inferior-io--init-proc (proc)
1583 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1584 (set-process-filter proc 'gdb-inferior-filter)
1585 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1586 ;; The process can run on a remote host.
1587 (let ((tty (or (process-get proc 'remote-tty)
1588 (process-tty-name proc))))
1589 (unless (or (null tty)
1590 (string= tty ""))
1591 (gdb-input
1592 (concat "-inferior-tty-set " tty) 'ignore))))
1594 (defun gdb-inferior-io-sentinel (proc _str)
1595 (when (eq (process-status proc) 'failed)
1596 ;; When the debugged process exits, Emacs gets an EIO error on
1597 ;; read from the pty, and stops listening to it. If the gdb
1598 ;; process is still running, remove the pty, make a new one, and
1599 ;; pass it to gdb.
1600 (let ((io-buffer (process-buffer proc)))
1601 (when (and (process-live-p (get-buffer-process gud-comint-buffer))
1602 (buffer-live-p io-buffer))
1603 ;; `comint-exec' deletes the original process as a side effect.
1604 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1605 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1607 (defcustom gdb-display-buffer-other-frame-action
1608 '((display-buffer-reuse-window display-buffer-pop-up-frame)
1609 (reusable-frames . visible)
1610 (inhibit-same-window . t)
1611 (pop-up-frame-parameters (height . 14)
1612 (width . 80)
1613 (unsplittable . t)
1614 (tool-bar-lines . nil)
1615 (menu-bar-lines . nil)
1616 (minibuffer . nil)))
1617 "`display-buffer' action for displaying GDB utility frames."
1618 :group 'gdb
1619 :type display-buffer--action-custom-type
1620 :risky t
1621 :version "24.3")
1623 (defun gdb-frame-io-buffer ()
1624 "Display IO of debugged program in another frame."
1625 (interactive)
1626 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1627 gdb-display-buffer-other-frame-action))
1629 (defvar gdb-inferior-io-mode-map
1630 (let ((map (make-sparse-keymap)))
1631 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1632 (define-key map "\C-c\C-z" 'gdb-io-stop)
1633 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1634 (define-key map "\C-c\C-d" 'gdb-io-eof)
1635 (define-key map "\C-d" 'gdb-io-eof)
1636 map))
1638 ;; We want to use comint because it has various nifty and familiar features.
1639 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1640 "Major mode for gdb inferior-io."
1641 :syntax-table nil :abbrev-table nil
1642 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1644 (defcustom gdb-display-io-nopopup nil
1645 "When non-nil, and the `gdb-inferior-io' buffer is buried, don't pop it up."
1646 :type 'boolean
1647 :group 'gdb
1648 :version "25.1")
1650 (defun gdb-inferior-filter (proc string)
1651 (unless (string-equal string "")
1652 (let (buf)
1653 (unless (and gdb-display-io-nopopup
1654 (setq buf (gdb-get-buffer 'gdb-inferior-io))
1655 (null (get-buffer-window buf)))
1656 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1657 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1658 (comint-output-filter proc string)))
1660 (defun gdb-io-interrupt ()
1661 "Interrupt the program being debugged."
1662 (interactive)
1663 (interrupt-process
1664 (get-buffer-process gud-comint-buffer) comint-ptyp))
1666 (defun gdb-io-quit ()
1667 "Send quit signal to the program being debugged."
1668 (interactive)
1669 (quit-process
1670 (get-buffer-process gud-comint-buffer) comint-ptyp))
1672 (defun gdb-io-stop ()
1673 "Stop the program being debugged."
1674 (interactive)
1675 (stop-process
1676 (get-buffer-process gud-comint-buffer) comint-ptyp))
1678 (defun gdb-io-eof ()
1679 "Send end-of-file to the program being debugged."
1680 (interactive)
1681 (process-send-eof
1682 (get-buffer-process gud-comint-buffer)))
1684 (defun gdb-clear-inferior-io ()
1685 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1686 (erase-buffer)))
1689 (defconst breakpoint-xpm-data
1690 "/* XPM */
1691 static char *magick[] = {
1692 /* columns rows colors chars-per-pixel */
1693 \"10 10 2 1\",
1694 \" c red\",
1695 \"+ c None\",
1696 /* pixels */
1697 \"+++ +++\",
1698 \"++ ++\",
1699 \"+ +\",
1700 \" \",
1701 \" \",
1702 \" \",
1703 \" \",
1704 \"+ +\",
1705 \"++ ++\",
1706 \"+++ +++\",
1708 "XPM data used for breakpoint icon.")
1710 (defconst breakpoint-enabled-pbm-data
1712 10 10\",
1713 0 0 0 0 1 1 1 1 0 0 0 0
1714 0 0 0 1 1 1 1 1 1 0 0 0
1715 0 0 1 1 1 1 1 1 1 1 0 0
1716 0 1 1 1 1 1 1 1 1 1 1 0
1717 0 1 1 1 1 1 1 1 1 1 1 0
1718 0 1 1 1 1 1 1 1 1 1 1 0
1719 0 1 1 1 1 1 1 1 1 1 1 0
1720 0 0 1 1 1 1 1 1 1 1 0 0
1721 0 0 0 1 1 1 1 1 1 0 0 0
1722 0 0 0 0 1 1 1 1 0 0 0 0"
1723 "PBM data used for enabled breakpoint icon.")
1725 (defconst breakpoint-disabled-pbm-data
1727 10 10\",
1728 0 0 1 0 1 0 1 0 0 0
1729 0 1 0 1 0 1 0 1 0 0
1730 1 0 1 0 1 0 1 0 1 0
1731 0 1 0 1 0 1 0 1 0 1
1732 1 0 1 0 1 0 1 0 1 0
1733 0 1 0 1 0 1 0 1 0 1
1734 1 0 1 0 1 0 1 0 1 0
1735 0 1 0 1 0 1 0 1 0 1
1736 0 0 1 0 1 0 1 0 1 0
1737 0 0 0 1 0 1 0 1 0 0"
1738 "PBM data used for disabled breakpoint icon.")
1740 (defvar breakpoint-enabled-icon nil
1741 "Icon for enabled breakpoint in display margin.")
1743 (defvar breakpoint-disabled-icon nil
1744 "Icon for disabled breakpoint in display margin.")
1746 (declare-function define-fringe-bitmap "fringe.c"
1747 (bitmap bits &optional height width align))
1749 (and (display-images-p)
1750 ;; Bitmap for breakpoint in fringe
1751 (define-fringe-bitmap 'breakpoint
1752 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1753 ;; Bitmap for gud-overlay-arrow in fringe
1754 (define-fringe-bitmap 'hollow-right-triangle
1755 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1757 (defface breakpoint-enabled
1758 '((t
1759 :foreground "red1"
1760 :weight bold))
1761 "Face for enabled breakpoint icon in fringe."
1762 :group 'gdb)
1764 (defface breakpoint-disabled
1765 '((((class color) (min-colors 88)) :foreground "grey70")
1766 ;; Ensure that on low-color displays that we end up something visible.
1767 (((class color) (min-colors 8) (background light))
1768 :foreground "black")
1769 (((class color) (min-colors 8) (background dark))
1770 :foreground "white")
1771 (((type tty) (class mono))
1772 :inverse-video t)
1773 (t :background "gray"))
1774 "Face for disabled breakpoint icon in fringe."
1775 :group 'gdb)
1778 (defvar gdb-python-guile-commands-regexp
1779 "python\\|python-interactive\\|pi\\|guile\\|guile-repl\\|gr"
1780 "Regexp that matches Python and Guile commands supported by GDB.")
1782 (defvar gdb-control-commands-regexp
1783 (concat
1784 "^\\("
1785 "commands\\|if\\|while\\|define\\|document\\|"
1786 gdb-python-guile-commands-regexp
1787 "\\|while-stepping\\|stepping\\|ws\\|actions"
1788 "\\)\\([[:blank:]]+\\([^[:blank:]]*\\)\\)?$")
1789 "Regexp matching GDB commands that enter a recursive reading loop.
1790 As long as GDB is in the recursive reading loop, it does not expect
1791 commands to be prefixed by \"-interpreter-exec console\".")
1793 (defun gdb-strip-string-backslash (string)
1794 (replace-regexp-in-string "\\\\$" "" string))
1796 (defun gdb-send (proc string)
1797 "A comint send filter for gdb."
1798 (with-current-buffer gud-comint-buffer
1799 (let ((inhibit-read-only t))
1800 (remove-text-properties (point-min) (point-max) '(face))))
1801 ;; mimic <RET> key to repeat previous command in GDB
1802 (when (= gdb-control-level 0)
1803 (if (not (string= "" string))
1804 (if gdb-continuation
1805 (setq gdb-last-command (concat gdb-continuation
1806 (gdb-strip-string-backslash string)
1807 " "))
1808 (setq gdb-last-command (gdb-strip-string-backslash string)))
1809 (if gdb-last-command (setq string gdb-last-command))
1810 (setq gdb-continuation nil)))
1811 (if (and (not gdb-continuation)
1812 (or (string-match "^-" string)
1813 (> gdb-control-level 0)))
1814 ;; Either MI command or we are feeding GDB's recursive reading loop.
1815 (progn
1816 (setq gdb-first-done-or-error t)
1817 (process-send-string proc (concat string "\n"))
1818 (if (and (string-match
1819 (concat "^\\("
1820 (if (eq system-type 'windows-nt) "\026" "\004")
1821 "\\|,q\\|,quit\\|end\\)$")
1822 string)
1823 (> gdb-control-level 0))
1824 (setq gdb-control-level (1- gdb-control-level))))
1825 ;; CLI command
1826 (if (string-match "\\\\$" string)
1827 (setq gdb-continuation
1828 (concat gdb-continuation (gdb-strip-string-backslash
1829 string)
1830 " "))
1831 (setq gdb-first-done-or-error t)
1832 (let ((to-send (concat "-interpreter-exec console "
1833 (gdb-mi-quote (concat gdb-continuation string " "))
1834 "\n")))
1835 (if gdb-enable-debug
1836 (push (cons 'mi-send to-send) gdb-debug-log))
1837 (process-send-string proc to-send))
1838 (if (and (string-match
1839 (concat "^\\("
1840 (if (eq system-type 'windows-nt) "\026" "\004")
1841 "\\|,q\\|,quit\\|end\\)$")
1842 string)
1843 (> gdb-control-level 0))
1844 (setq gdb-control-level (1- gdb-control-level)))
1845 (setq gdb-continuation nil)))
1846 ;; Python and Guile commands that have an argument don't enter the
1847 ;; recursive reading loop.
1848 (let* ((control-command-p (string-match gdb-control-commands-regexp string))
1849 (command-arg (match-string 3 string))
1850 (python-or-guile-p (string-match gdb-python-guile-commands-regexp
1851 string)))
1852 (if (and control-command-p
1853 (or (not python-or-guile-p)
1854 (null command-arg)
1855 (zerop (length command-arg))))
1856 (setq gdb-control-level (1+ gdb-control-level)))))
1858 (defun gdb-mi-quote (string)
1859 "Return STRING quoted properly as an MI argument.
1860 The string is enclosed in double quotes.
1861 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1862 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1863 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1864 (concat "\"" string "\""))
1866 (defun gdb-input (command handler-function &optional trigger-name)
1867 "Send COMMAND to GDB via the MI interface.
1868 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1869 complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
1870 Emacs is still waiting for a reply from another command previously
1871 sent with the same TRIGGER-NAME."
1872 (when (or (not trigger-name)
1873 (not (gdb-pending-handler-p trigger-name)))
1874 (setq gdb-token-number (1+ gdb-token-number))
1875 (setq command (concat (number-to-string gdb-token-number) command))
1877 (if gdb-enable-debug (push (list 'send-item command handler-function)
1878 gdb-debug-log))
1880 (gdb-add-handler gdb-token-number handler-function trigger-name)
1882 (if gdbmi-debug-mode (message "gdb-input: %s" command))
1883 (process-send-string (get-buffer-process gud-comint-buffer)
1884 (concat command "\n"))))
1886 ;; NOFRAME is used for gud execution control commands
1887 (defun gdb-current-context-command (command)
1888 "Add --thread to gdb COMMAND when needed."
1889 (if (and gdb-thread-number
1890 gdb-supports-non-stop)
1891 (concat command " --thread " gdb-thread-number)
1892 command))
1894 (defun gdb-current-context-buffer-name (name)
1895 "Add thread information and asterisks to string NAME.
1897 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1898 (concat "*" name
1899 (if (local-variable-p 'gdb-thread-number)
1900 (format " (bound to thread %s)" gdb-thread-number)
1902 "*"))
1904 (defun gdb-current-context-mode-name (mode)
1905 "Add thread information to MODE which is to be used as `mode-name'."
1906 (concat mode
1907 (if gdb-thread-number
1908 (format " [thread %s]" gdb-thread-number)
1909 "")))
1912 (defcustom gud-gdb-command-name "gdb -i=mi"
1913 "Default command to execute an executable under the GDB debugger."
1914 :type 'string
1915 :group 'gdb)
1917 (defun gdb-resync()
1918 (setq gud-running nil)
1919 (setq gdb-output-sink 'user)
1920 (gdb-remove-all-pending-triggers))
1922 (defun gdb-update (&optional no-proc)
1923 "Update buffers showing status of debug session.
1924 If NO-PROC is non-nil, do not try to contact the GDB process."
1925 (when gdb-first-prompt
1926 (gdb-force-mode-line-update
1927 (propertize "initializing..." 'face font-lock-variable-name-face))
1928 (gdb-init-1)
1929 (setq gdb-first-prompt nil))
1931 (unless no-proc
1932 (gdb-get-main-selected-frame))
1934 ;; We may need to update gdb-threads-list so we can use
1935 (gdb-get-buffer-create 'gdb-threads-buffer)
1936 ;; gdb-break-list is maintained in breakpoints handler
1937 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1939 (unless no-proc
1940 (gdb-emit-signal gdb-buf-publisher 'update))
1942 (gdb-get-changed-registers)
1943 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1944 (dolist (var gdb-var-list)
1945 (setcar (nthcdr 5 var) nil))
1946 (gdb-var-update)))
1948 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1949 ;; because we may need to update current gud-running value without
1950 ;; changing current thread (see gdb-running)
1951 (defun gdb-setq-thread-number (number)
1952 "Set `gdb-thread-number' to NUMBER.
1953 Only this function must be used to change `gdb-thread-number'
1954 value to NUMBER, because `gud-running' and `gdb-frame-number'
1955 need to be updated appropriately when current thread changes."
1956 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1957 (unless (string-equal number "0") (setq gdb-thread-number number))
1958 (setq gdb-frame-number "0")
1959 (gdb-update-gud-running))
1961 (defun gdb-update-gud-running ()
1962 "Set `gud-running' according to the state of current thread.
1964 `gdb-frame-number' is set to 0 if current thread is now stopped.
1966 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1967 cannot be reliably used to determine whether or not execution
1968 control buttons should be shown in menu or toolbar. Use
1969 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1970 instead.
1972 For all-stop mode, thread information is unavailable while target
1973 is running."
1974 (let ((old-value gud-running))
1975 (setq gud-running
1976 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1977 "running"))
1978 ;; Set frame number to "0" when _current_ threads stops.
1979 (when (and (gdb-current-buffer-thread)
1980 (not (eq gud-running old-value)))
1981 (setq gdb-frame-number "0"))))
1983 (defun gdb-show-run-p ()
1984 "Return t if \"Run/continue\" should be shown on the toolbar."
1985 (or (not gdb-active-process)
1986 (and (or
1987 (not gdb-gud-control-all-threads)
1988 (not gdb-non-stop))
1989 (not gud-running))
1990 (and gdb-gud-control-all-threads
1991 (> gdb-stopped-threads-count 0))))
1993 (defun gdb-show-stop-p ()
1994 "Return t if \"Stop\" should be shown on the toolbar."
1995 (or (and (or
1996 (not gdb-gud-control-all-threads)
1997 (not gdb-non-stop))
1998 gud-running)
1999 (and gdb-gud-control-all-threads
2000 (not (null gdb-running-threads-count))
2001 (> gdb-running-threads-count 0))))
2003 ;; GUD displays the selected GDB frame. This might might not be the current
2004 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
2005 ;; visited breakpoint is, use that window.
2006 (defun gdb-display-source-buffer (buffer)
2007 (let* ((last-window (if gud-last-last-frame
2008 (get-buffer-window
2009 (gud-find-file (car gud-last-last-frame)))))
2010 (source-window (or last-window
2011 (if (and gdb-source-window
2012 (window-live-p gdb-source-window))
2013 gdb-source-window))))
2014 (when source-window
2015 (setq gdb-source-window source-window)
2016 (set-window-buffer source-window buffer))
2017 source-window))
2020 (defun gdbmi-start-with (str offset match)
2021 "Return non-nil if string STR starts with MATCH, else returns nil.
2022 OFFSET is the position in STR at which the comparison takes place."
2023 (let ((match-length (length match))
2024 (str-length (- (length str) offset)))
2025 (when (>= str-length match-length)
2026 (string-equal match (substring str offset (+ offset match-length))))))
2028 (defun gdbmi-same-start (str offset match)
2029 "Return non-nil if STR and MATCH are equal up to the end of either strings.
2030 OFFSET is the position in STR at which the comparison takes place."
2031 (let* ((str-length (- (length str) offset))
2032 (match-length (length match))
2033 (compare-length (min str-length match-length)))
2034 (when (> compare-length 0)
2035 (string-equal (substring str offset (+ offset compare-length))
2036 (substring match 0 compare-length)))))
2038 (defun gdbmi-is-number (character)
2039 "Return non-nil if CHARACTER is a numerical character between 0 and 9."
2040 (and (>= character ?0)
2041 (<= character ?9)))
2044 (defvar-local gdbmi-bnf-state 'gdbmi-bnf-output
2045 "Current GDB/MI output parser state.
2046 The parser is placed in a different state when an incomplete data steam is
2047 received from GDB.
2048 This variable will preserve the state required to resume the parsing
2049 when more data arrives.")
2051 (defvar-local gdbmi-bnf-offset 0
2052 "Offset in `gud-marker-acc' at which the parser is reading.
2053 This offset is used to be able to parse the GDB/MI message
2054 in-place, without the need of copying the string in a temporary buffer
2055 or discarding parsed tokens by substringing the message.")
2057 (defun gdbmi-bnf-init ()
2058 "Initialize the GDB/MI message parser."
2059 (setq gdbmi-bnf-state 'gdbmi-bnf-output)
2060 (setq gdbmi-bnf-offset 0)
2061 (setq gud-marker-acc ""))
2064 (defun gdbmi-bnf-output ()
2065 "Implementation of the following GDB/MI output grammar rule:
2067 output ==>
2068 ( out-of-band-record )* [ result-record ] gdb-prompt"
2070 (gdbmi-bnf-skip-unrecognized)
2071 (while (gdbmi-bnf-out-of-band-record))
2072 (gdbmi-bnf-result-record)
2073 (gdbmi-bnf-gdb-prompt))
2076 (defun gdbmi-bnf-skip-unrecognized ()
2077 "Skip characters until is encounters the beginning of a valid record.
2078 Used as a protection mechanism in case something goes wrong when parsing
2079 a GDB/MI reply message."
2080 (let ((acc-length (length gud-marker-acc))
2081 (prefix-offset gdbmi-bnf-offset)
2082 (prompt "(gdb) \n"))
2084 (while (and (< prefix-offset acc-length)
2085 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
2086 (setq prefix-offset (1+ prefix-offset)))
2088 (if (and (< prefix-offset acc-length)
2089 (not (memq (aref gud-marker-acc prefix-offset)
2090 '(?^ ?* ?+ ?= ?~ ?@ ?&)))
2091 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt))
2092 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
2093 gdbmi-bnf-offset))
2094 (let ((unrecognized-str (match-string 0 gud-marker-acc)))
2095 (setq gdbmi-bnf-offset (match-end 0))
2096 (if gdbmi-debug-mode
2097 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str))
2098 (gdb-shell unrecognized-str)
2099 t))))
2102 (defun gdbmi-bnf-gdb-prompt ()
2103 "Implementation of the following GDB/MI output grammar rule:
2104 gdb-prompt ==>
2105 `(gdb)' nl
2107 nl ==>
2108 CR | CR-LF"
2110 (let ((prompt "(gdb) \n"))
2111 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt)
2112 (if gdbmi-debug-mode (message "gdbmi-bnf-gdb-prompt: %s" prompt))
2113 (gdb-gdb prompt)
2114 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length prompt)))
2116 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
2117 ;; the end of a GDB reply message.
2118 t)))
2121 (defun gdbmi-bnf-result-record ()
2122 "Implementation of the following GDB/MI output grammar rule:
2124 result-record ==>
2125 [ token ] `^' result-class ( `,' result )* nl
2127 token ==>
2128 any sequence of digits."
2130 (gdbmi-bnf-result-and-async-record-impl))
2133 (defun gdbmi-bnf-out-of-band-record ()
2134 "Implementation of the following GDB/MI output grammar rule:
2136 out-of-band-record ==>
2137 async-record | stream-record"
2139 (or (gdbmi-bnf-async-record)
2140 (gdbmi-bnf-stream-record)))
2143 (defun gdbmi-bnf-async-record ()
2144 "Implementation of the following GDB/MI output grammar rules:
2146 async-record ==>
2147 exec-async-output | status-async-output | notify-async-output
2149 exec-async-output ==>
2150 [ token ] `*' async-output
2152 status-async-output ==>
2153 [ token ] `+' async-output
2155 notify-async-output ==>
2156 [ token ] `=' async-output
2158 async-output ==>
2159 async-class ( `,' result )* nl"
2161 (gdbmi-bnf-result-and-async-record-impl))
2164 (defun gdbmi-bnf-stream-record ()
2165 "Implement the following GDB/MI output grammar rule:
2166 stream-record ==>
2167 console-stream-output | target-stream-output | log-stream-output
2169 console-stream-output ==>
2170 `~' c-string
2172 target-stream-output ==>
2173 `@' c-string
2175 log-stream-output ==>
2176 `&' c-string"
2177 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2178 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset) '(?~ ?@ ?&))
2179 (string-match (concat "\\([~@&]\\)\\(" gdb--string-regexp "\\)\n")
2180 gud-marker-acc
2181 gdbmi-bnf-offset))
2182 (let ((prefix (match-string 1 gud-marker-acc))
2183 (c-string (match-string 2 gud-marker-acc)))
2185 (setq gdbmi-bnf-offset (match-end 0))
2186 (if gdbmi-debug-mode (message "gdbmi-bnf-stream-record: %s"
2187 (match-string 0 gud-marker-acc)))
2189 (cond ((string-equal prefix "~")
2190 (gdbmi-bnf-console-stream-output c-string))
2191 ((string-equal prefix "@")
2192 (gdbmi-bnf-target-stream-output c-string))
2193 ((string-equal prefix "&")
2194 (gdbmi-bnf-log-stream-output c-string)))
2195 t))))
2197 (defun gdbmi-bnf-console-stream-output (c-string)
2198 "Handler for the console-stream-output GDB/MI output grammar rule."
2199 (gdb-console c-string)
2200 ;; We've written to the GUD console, so we should print the prompt
2201 ;; after the next result-class or async-class.
2202 (setq gdb-first-done-or-error t))
2204 (defun gdbmi-bnf-target-stream-output (_c-string)
2205 "Handler for the target-stream-output GDB/MI output grammar rule."
2206 ;; Not currently used.
2209 (defun gdbmi-bnf-log-stream-output (c-string)
2210 "Handler for the log-stream-output GDB/MI output grammar rule."
2211 ;; Suppress "No registers." GDB 6.8 and earlier
2212 ;; duplicates MI error message on internal stream.
2213 ;; Don't print to GUD buffer.
2214 (if (not (string-equal (read c-string) "No registers.\n"))
2215 (gdb-internals c-string)))
2218 (defconst gdbmi-bnf-result-state-configs
2219 '(("^" . (("done" . (gdb-done . progressive))
2220 ("error" . (gdb-error . progressive))
2221 ("running" . (gdb-starting . atomic))))
2222 ("*" . (("stopped" . (gdb-stopped . atomic))
2223 ("running" . (gdb-running . atomic))))
2224 ("+" . ())
2225 ("=" . (("thread-created" . (gdb-thread-created . atomic))
2226 ("thread-selected" . (gdb-thread-selected . atomic))
2227 ("thread-existed" . (gdb-ignored-notification . atomic))
2228 ('default . (gdb-ignored-notification . atomic)))))
2229 "Alist of alists, mapping the type and class of message to a handler function.
2230 Handler functions are all flagged as either `progressive' or `atomic'.
2231 `progressive' handlers are capable of parsing incomplete messages.
2232 They can be called several time with new data chunk as they arrive from GDB.
2233 `progressive' handlers must have an extra argument that is set to a non-nil
2234 value when the message is complete.
2236 Implement the following GDB/MI output grammar rule:
2237 result-class ==>
2238 `done' | `running' | `connected' | `error' | `exit'
2240 async-class ==>
2241 `stopped' | others (where others will be added depending on the needs
2242 --this is still in development).")
2244 (defun gdbmi-bnf-result-and-async-record-impl ()
2245 "Common implementation of the result-record and async-record rule.
2246 Both rules share the same syntax. Those records may be very large in size.
2247 For that reason, the \"result\" part of the record is parsed by
2248 `gdbmi-bnf-incomplete-record-result', which will keep
2249 receiving characters as they arrive from GDB until the record is complete."
2250 (let ((acc-length (length gud-marker-acc))
2251 (prefix-offset gdbmi-bnf-offset))
2253 (while (and (< prefix-offset acc-length)
2254 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
2255 (setq prefix-offset (1+ prefix-offset)))
2257 (if (and (< prefix-offset acc-length)
2258 (member (aref gud-marker-acc prefix-offset) '(?* ?+ ?= ?^))
2259 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2260 gud-marker-acc gdbmi-bnf-offset))
2262 (let ((token (match-string 1 gud-marker-acc))
2263 (prefix (match-string 2 gud-marker-acc))
2264 (class (match-string 3 gud-marker-acc))
2265 (complete (string-equal (match-string 4 gud-marker-acc) "\n"))
2266 class-alist
2267 class-command)
2269 (setq gdbmi-bnf-offset (match-end 0))
2270 (if gdbmi-debug-mode (message "gdbmi-bnf-result-record: %s"
2271 (match-string 0 gud-marker-acc)))
2273 (setq class-alist
2274 (cdr (assoc prefix gdbmi-bnf-result-state-configs)))
2275 (setq class-command (cdr (assoc class class-alist)))
2276 (if (null class-command)
2277 (setq class-command (cdr (assoc 'default class-alist))))
2279 (if complete
2280 (if class-command
2281 (if (equal (cdr class-command) 'progressive)
2282 (funcall (car class-command) token "" complete)
2283 (funcall (car class-command) token "")))
2284 (setq gdbmi-bnf-state
2285 (lambda ()
2286 (gdbmi-bnf-incomplete-record-result token class-command)))
2287 (funcall gdbmi-bnf-state))
2288 t))))
2290 (defun gdbmi-bnf-incomplete-record-result (token class-command)
2291 "State of the parser used to progressively parse a result-record or async-record
2292 rule from an incomplete data stream. The parser will stay in this state until
2293 the end of the current result or async record is reached."
2294 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2295 ;; Search the data stream for the end of the current record:
2296 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset))
2297 (is-progressive (equal (cdr class-command) 'progressive))
2298 (is-complete (not (null newline-pos)))
2299 result-str)
2301 (when gdbmi-debug-mode
2302 (message "gdbmi-bnf-incomplete-record-result: %s"
2303 (substring gud-marker-acc gdbmi-bnf-offset newline-pos)))
2305 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2306 ;; be processed by the class-command handler:
2307 (when (or is-complete is-progressive)
2308 (setq result-str
2309 (substring gud-marker-acc gdbmi-bnf-offset newline-pos))
2311 ;; Move gdbmi-bnf-offset past the end of the chunk.
2312 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length result-str)))
2313 (when newline-pos
2314 (setq gdbmi-bnf-offset (1+ gdbmi-bnf-offset))))
2316 ;; Update the parsing state before invoking the handler in class-command
2317 ;; to make sure it's not left in an invalid state if the handler was
2318 ;; to generate an error.
2319 (if is-complete
2320 (setq gdbmi-bnf-state 'gdbmi-bnf-output))
2322 (if class-command
2323 (if is-progressive
2324 (funcall (car class-command) token result-str is-complete)
2325 (if is-complete
2326 (funcall (car class-command) token result-str))))
2328 (unless is-complete
2329 ;; Incomplete gdb response: abort parsing until we receive more data.
2330 (if gdbmi-debug-mode (message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2331 (throw 'gdbmi-incomplete-stream nil))
2333 is-complete)))
2336 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2337 ; The handling of those rules is currently done by the handlers registered
2338 ; in gdbmi-bnf-result-state-configs
2340 ; result ==>
2341 ; variable "=" value
2343 ; variable ==>
2344 ; string
2346 ; value ==>
2347 ; const | tuple | list
2349 ; const ==>
2350 ; c-string
2352 ; tuple ==>
2353 ; "{}" | "{" result ( "," result )* "}"
2355 ; list ==>
2356 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2358 (defcustom gdb-mi-decode-strings nil
2359 "When non-nil, decode octal escapes in GDB output into non-ASCII text.
2361 If the value is a coding-system, use that coding-system to decode
2362 the bytes reconstructed from octal escapes. Any other non-nil value
2363 means to decode using the coding-system set for the GDB process.
2365 Warning: setting this non-nil might mangle strings reported by GDB
2366 that have literal substrings which match the \\nnn octal escape
2367 patterns, where nnn is an octal number between 200 and 377. So
2368 we only recommend to set this variable non-nil if the program you
2369 are debugging really reports non-ASCII text, or some of its source
2370 file names include non-ASCII characters."
2371 :type '(choice
2372 (const :tag "Don't decode" nil)
2373 (const :tag "Decode using default coding-system" t)
2374 (coding-system :tag "Decode using this coding-system"))
2375 :group 'gdb
2376 :version "25.1")
2378 ;; The idea of the following function was suggested
2379 ;; by Kenichi Handa <handa@gnu.org>.
2381 ;; FIXME: This is fragile: it relies on the assumption that all the
2382 ;; non-ASCII strings output by GDB, including names of the source
2383 ;; files, values of string variables in the inferior, etc., are all
2384 ;; encoded in the same encoding. It also assumes that the \nnn
2385 ;; sequences are not split between chunks of output of the GDB process
2386 ;; due to buffering, and arrive together. Finally, if some string
2387 ;; included literal \nnn strings (as opposed to non-ASCII characters
2388 ;; converted by GDB/MI to octal escapes), this decoding will mangle
2389 ;; those strings. When/if GDB acquires the ability to not
2390 ;; escape-protect non-ASCII characters in its MI output, this kludge
2391 ;; should be removed.
2392 (defun gdb-mi-decode (string)
2393 "Decode octal escapes in MI output STRING into multibyte text."
2394 (let ((coding
2395 (if (coding-system-p gdb-mi-decode-strings)
2396 gdb-mi-decode-strings
2397 (with-current-buffer
2398 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2399 buffer-file-coding-system))))
2400 (with-temp-buffer
2401 (set-buffer-multibyte nil)
2402 (prin1 string (current-buffer))
2403 (goto-char (point-min))
2404 ;; prin1 quotes the octal escapes as well, which interferes with
2405 ;; their interpretation by 'read' below. Remove the extra
2406 ;; backslashes to countermand that.
2407 (while (re-search-forward "\\\\\\(\\\\[2-3][0-7][0-7]\\)" nil t)
2408 (replace-match "\\1" nil nil))
2409 (goto-char (point-min))
2410 (decode-coding-string (read (current-buffer)) coding))))
2412 (defun gud-gdbmi-marker-filter (string)
2413 "Filter GDB/MI output."
2415 ;; If required, decode non-ASCII text encoded with octal escapes.
2416 (or (null gdb-mi-decode-strings)
2417 (setq string (gdb-mi-decode string)))
2419 ;; Record transactions if logging is enabled.
2420 (when gdb-enable-debug
2421 (push (cons 'recv string) gdb-debug-log)
2422 (if (and gdb-debug-log-max
2423 (> (length gdb-debug-log) gdb-debug-log-max))
2424 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
2426 ;; Recall the left over gud-marker-acc from last time.
2427 (setq gud-marker-acc (concat gud-marker-acc string))
2429 ;; Start accumulating output for the GUD buffer.
2430 (setq gdb-filter-output "")
2432 (let ((acc-length (length gud-marker-acc)))
2433 (catch 'gdbmi-incomplete-stream
2434 (while (and (< gdbmi-bnf-offset acc-length)
2435 (funcall gdbmi-bnf-state)))))
2437 (when (/= gdbmi-bnf-offset 0)
2438 (setq gud-marker-acc (substring gud-marker-acc gdbmi-bnf-offset))
2439 (setq gdbmi-bnf-offset 0))
2441 (when (and gdbmi-debug-mode (> (length gud-marker-acc) 0))
2442 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc))
2444 gdb-filter-output)
2446 (defun gdb-gdb (_output-field))
2448 (defun gdb-shell (output-field)
2449 (setq gdb-filter-output
2450 (concat output-field gdb-filter-output)))
2452 (defun gdb-ignored-notification (_token _output-field))
2454 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2455 (defun gdb-thread-created (_token _output-field))
2456 (defun gdb-thread-exited (_token output-field)
2457 "Handle =thread-exited async record.
2458 Unset `gdb-thread-number' if current thread exited and update threads list."
2459 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
2460 (if (string= gdb-thread-number thread-id)
2461 (gdb-setq-thread-number nil))
2462 ;; When we continue current thread and it quickly exits,
2463 ;; the pending triggers in gdb-handler-list left after gdb-running
2464 ;; disallow us to properly call -thread-info without --thread option.
2465 ;; Thus we need to use gdb-wait-for-pending.
2466 (gdb-wait-for-pending
2467 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
2469 (defun gdb-thread-selected (_token output-field)
2470 "Handler for =thread-selected MI output record.
2472 Sets `gdb-thread-number' to new id."
2473 (let* ((result (gdb-json-string output-field))
2474 (thread-id (bindat-get-field result 'id)))
2475 (gdb-setq-thread-number thread-id)
2476 ;; Typing `thread N' in GUD buffer makes GDB emit `^done' followed
2477 ;; by `=thread-selected' notification. `^done' causes `gdb-update'
2478 ;; as usually. Things happen too fast and second call (from
2479 ;; gdb-thread-selected handler) gets cut off by our beloved
2480 ;; pending triggers.
2481 ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
2482 ;; body will get executed when `gdb-handler-list' if free of
2483 ;; pending triggers.
2484 (gdb-wait-for-pending
2485 (gdb-update))))
2487 (defun gdb-running (_token output-field)
2488 (let* ((thread-id
2489 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
2490 ;; We reset gdb-frame-number to nil if current thread has gone
2491 ;; running. This can't be done in gdb-thread-list-handler-custom
2492 ;; because we need correct gdb-frame-number by the time
2493 ;; -thread-info command is sent.
2494 (when (or (string-equal thread-id "all")
2495 (string-equal thread-id gdb-thread-number))
2496 (setq gdb-frame-number nil)))
2497 (setq gdb-inferior-status "running")
2498 (gdb-force-mode-line-update
2499 (propertize gdb-inferior-status 'face font-lock-type-face))
2500 (when (not gdb-non-stop)
2501 (setq gud-running t))
2502 (setq gdb-active-process t))
2504 (defun gdb-starting (_output-field _result)
2505 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2506 (setq gdb-inferior-status "running")
2507 (gdb-force-mode-line-update
2508 (propertize gdb-inferior-status 'face font-lock-type-face))
2509 (setq gdb-active-process t)
2510 (setq gud-running t))
2512 ;; -break-insert -t didn't give a reason before gdb 6.9
2514 (defun gdb-stopped (_token output-field)
2515 "Given the contents of *stopped MI async record, select new
2516 current thread and update GDB buffers."
2517 ;; Reason is available with target-async only
2518 (let* ((result (gdb-json-string output-field))
2519 (reason (bindat-get-field result 'reason))
2520 (thread-id (bindat-get-field result 'thread-id))
2521 (retval (bindat-get-field result 'return-value))
2522 (varnum (bindat-get-field result 'gdb-result-var)))
2524 ;; -data-list-register-names needs to be issued for any stopped
2525 ;; thread
2526 (when (not gdb-register-names)
2527 (gdb-input (concat "-data-list-register-names"
2528 (if gdb-supports-non-stop
2529 (concat " --thread " thread-id)))
2530 'gdb-register-names-handler))
2532 ;; Don't set gud-last-frame here as it's currently done in
2533 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2534 ;; with CLI.
2535 ;;(when file
2536 ;; (setq
2537 ;; ;; Extract the frame position from the marker.
2538 ;; gud-last-frame (cons file
2539 ;; (string-to-number
2540 ;; (match-string 6 gud-marker-acc)))))
2542 (setq gdb-inferior-status (or reason "unknown"))
2543 (gdb-force-mode-line-update
2544 (propertize gdb-inferior-status 'face font-lock-warning-face))
2545 (if (string-equal reason "exited-normally")
2546 (setq gdb-active-process nil))
2548 (when (and retval varnum
2549 ;; When the user typed CLI commands, GDB/MI helpfully
2550 ;; includes the "Value returned" response in the "~"
2551 ;; record; here we avoid displaying it twice.
2552 (not (string-match "^Value returned is " gdb-filter-output)))
2553 (setq gdb-filter-output
2554 (concat gdb-filter-output
2555 (format "Value returned is %s = %s\n" varnum retval))))
2557 ;; Select new current thread.
2559 ;; Don't switch if we have no reasons selected
2560 (when gdb-switch-reasons
2561 ;; Switch from another stopped thread only if we have
2562 ;; gdb-switch-when-another-stopped:
2563 (when (or gdb-switch-when-another-stopped
2564 (not (string= "stopped"
2565 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2566 ;; Switch if current reason has been selected or we have no
2567 ;; reasons
2568 (if (or (eq gdb-switch-reasons t)
2569 (member reason gdb-switch-reasons))
2570 (when (not (string-equal gdb-thread-number thread-id))
2571 (message "Switched to thread %s" thread-id)
2572 (gdb-setq-thread-number thread-id))
2573 (message "Thread %s stopped" thread-id))))
2575 ;; Print "(gdb)" to GUD console
2576 (when gdb-first-done-or-error
2577 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2579 ;; In non-stop, we update information as soon as another thread gets
2580 ;; stopped
2581 (when (or gdb-first-done-or-error
2582 gdb-non-stop)
2583 ;; In all-stop this updates gud-running properly as well.
2584 (gdb-update)
2585 (setq gdb-first-done-or-error nil))
2586 (run-hook-with-args 'gdb-stopped-functions result)))
2588 ;; Remove the trimmings from log stream containing debugging messages
2589 ;; being produced by GDB's internals, use warning face and send to GUD
2590 ;; buffer.
2591 (defun gdb-internals (output-field)
2592 (setq gdb-filter-output
2593 (gdb-concat-output
2594 gdb-filter-output
2595 (if (string= output-field "\"\\n\"")
2597 (let ((error-message
2598 (read output-field)))
2599 (put-text-property
2600 0 (length error-message)
2601 'face font-lock-warning-face
2602 error-message)
2603 error-message)))))
2605 ;; Remove the trimmings from the console stream and send to GUD buffer
2606 ;; (frontend MI commands should not print to this stream)
2607 (defun gdb-console (output-field)
2608 (setq gdb-filter-output
2609 (gdb-concat-output gdb-filter-output (read output-field))))
2611 (defun gdb-done (token-number output-field is-complete)
2612 (gdb-done-or-error token-number 'done output-field is-complete))
2614 (defun gdb-error (token-number output-field is-complete)
2615 (gdb-done-or-error token-number 'error output-field is-complete))
2617 (defun gdb-done-or-error (token-number type output-field is-complete)
2618 (if (string-equal token-number "")
2619 ;; Output from command entered by user
2620 (progn
2621 (setq gdb-output-sink 'user)
2622 (setq token-number nil)
2623 ;; MI error - send to minibuffer
2624 (when (eq type 'error)
2625 ;; Skip "msg=" from `output-field'
2626 (message "%s" (read (substring output-field 4)))
2627 ;; Don't send to the console twice. (If it is a console error
2628 ;; it is also in the console stream.)
2629 (setq output-field nil)))
2630 ;; Output from command from frontend.
2631 (setq gdb-output-sink 'emacs))
2633 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2634 (let* ((proc (get-buffer-process gud-comint-buffer))
2635 (no-proc (or (null proc)
2636 (memq (process-status proc) '(exit signal)))))
2638 (when (and is-complete gdb-first-done-or-error)
2639 (unless (or token-number gud-running no-proc)
2640 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2641 (gdb-update no-proc)
2642 (setq gdb-first-done-or-error nil))
2644 (setq gdb-filter-output
2645 (gdb-concat-output gdb-filter-output output-field))
2647 ;; We are done concatenating to the output sink. Restore it to user sink:
2648 (setq gdb-output-sink 'user)
2650 (when (and token-number is-complete)
2651 (with-current-buffer
2652 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2653 (gdb-handle-reply (string-to-number token-number))))
2655 (when is-complete
2656 (gdb-clear-partial-output))))
2658 (defun gdb-concat-output (so-far new)
2659 (cond
2660 ((eq gdb-output-sink 'user) (concat so-far new))
2661 ((eq gdb-output-sink 'emacs)
2662 (gdb-append-to-partial-output new)
2663 so-far)))
2665 (defun gdb-append-to-partial-output (string)
2666 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2667 (goto-char (point-max))
2668 (insert string)))
2670 (defun gdb-clear-partial-output ()
2671 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2672 (erase-buffer)))
2674 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2675 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2677 Field names are wrapped in double quotes and equal signs are
2678 replaced with semicolons.
2680 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2681 partial output. This is used to get rid of useless keys in lists
2682 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2683 -break-info are examples of MI commands which issue such
2684 responses.
2686 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2687 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2688 -break-info output when it contains breakpoint script field
2689 incompatible with GDB/MI output syntax.
2691 If `default-directory' is remote, full file names are adapted accordingly."
2692 (save-excursion
2693 (let ((remote (file-remote-p default-directory)))
2694 (when remote
2695 (goto-char (point-min))
2696 (while (re-search-forward "[\\[,]fullname=\"\\(.+\\)\"" nil t)
2697 (replace-match (concat remote "\\1") nil nil nil 1))))
2698 (goto-char (point-min))
2699 (when fix-key
2700 (save-excursion
2701 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2702 (replace-match "" nil nil nil 1))))
2703 (when fix-list
2704 (save-excursion
2705 ;; Find positions of braces which enclose broken list
2706 (while (re-search-forward (concat fix-list "={\"") nil t)
2707 (let ((p1 (goto-char (- (point) 2)))
2708 (p2 (progn (forward-sexp)
2709 (1- (point)))))
2710 ;; Replace braces with brackets
2711 (save-excursion
2712 (goto-char p1)
2713 (delete-char 1)
2714 (insert "[")
2715 (goto-char p2)
2716 (delete-char 1)
2717 (insert "]"))))))
2718 (goto-char (point-min))
2719 (insert "{")
2720 (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
2721 gdb--string-regexp "\\)")))
2722 (while (re-search-forward re nil t)
2723 (replace-match "\"\\1\":\\2" nil nil)))
2724 (goto-char (point-max))
2725 (insert "}")))
2727 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2728 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2730 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2731 (gdb-jsonify-buffer fix-key fix-list)
2732 (save-excursion
2733 (goto-char (point-min))
2734 (let ((json-array-type 'list))
2735 (json-read))))
2737 (defun gdb-json-string (string &optional fix-key fix-list)
2738 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2740 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2741 (with-temp-buffer
2742 (insert string)
2743 (gdb-json-read-buffer fix-key fix-list)))
2745 (defun gdb-json-partial-output (&optional fix-key fix-list)
2746 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2748 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2749 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2750 (gdb-json-read-buffer fix-key fix-list)))
2752 (defun gdb-line-posns (line)
2753 "Return a pair of LINE beginning and end positions."
2754 (let ((offset (1+ (- line (line-number-at-pos)))))
2755 (cons
2756 (line-beginning-position offset)
2757 (line-end-position offset))))
2759 (defmacro gdb-mark-line (line variable)
2760 "Set VARIABLE marker to point at beginning of LINE.
2762 If current window has no fringes, inverse colors on LINE.
2764 Return position where LINE begins."
2765 `(save-excursion
2766 (let* ((posns (gdb-line-posns ,line))
2767 (start-posn (car posns))
2768 (end-posn (cdr posns)))
2769 (set-marker ,variable (copy-marker start-posn))
2770 (when (not (> (car (window-fringes)) 0))
2771 (put-text-property start-posn end-posn
2772 'font-lock-face '(:inverse-video t)))
2773 start-posn)))
2775 (defun gdb-pad-string (string padding)
2776 (format (concat "%" (number-to-string padding) "s") string))
2778 ;; gdb-table struct is a way to programmatically construct simple
2779 ;; tables. It help to reliably align columns of data in GDB buffers
2780 ;; and provides
2781 (cl-defstruct gdb-table
2782 (column-sizes nil)
2783 (rows nil)
2784 (row-properties nil)
2785 (right-align nil))
2787 (defun gdb-table-add-row (table row &optional properties)
2788 "Add ROW of string to TABLE and recalculate column sizes.
2790 When non-nil, PROPERTIES will be added to the whole row when
2791 calling `gdb-table-string'."
2792 (let ((rows (gdb-table-rows table))
2793 (row-properties (gdb-table-row-properties table))
2794 (column-sizes (gdb-table-column-sizes table))
2795 (right-align (gdb-table-right-align table)))
2796 (when (not column-sizes)
2797 (setf (gdb-table-column-sizes table)
2798 (make-list (length row) 0)))
2799 (setf (gdb-table-rows table)
2800 (append rows (list row)))
2801 (setf (gdb-table-row-properties table)
2802 (append row-properties (list properties)))
2803 (setf (gdb-table-column-sizes table)
2804 (cl-mapcar (lambda (x s)
2805 (let ((new-x
2806 (max (abs x) (string-width (or s "")))))
2807 (if right-align new-x (- new-x))))
2808 (gdb-table-column-sizes table)
2809 row))
2810 ;; Avoid trailing whitespace at eol
2811 (if (not (gdb-table-right-align table))
2812 (setcar (last (gdb-table-column-sizes table)) 0))))
2814 (defun gdb-table-string (table &optional sep)
2815 "Return TABLE as a string with columns separated with SEP."
2816 (let ((column-sizes (gdb-table-column-sizes table)))
2817 (mapconcat
2818 'identity
2819 (cl-mapcar
2820 (lambda (row properties)
2821 (apply 'propertize
2822 (mapconcat 'identity
2823 (cl-mapcar (lambda (s x) (gdb-pad-string s x))
2824 row column-sizes)
2825 sep)
2826 properties))
2827 (gdb-table-rows table)
2828 (gdb-table-row-properties table))
2829 "\n")))
2831 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2832 (defun gdb-get-many-fields (struct &rest fields)
2833 "Return a list of FIELDS values from STRUCT."
2834 (let ((values))
2835 (dolist (field fields)
2836 (push (bindat-get-field struct field) values))
2837 (nreverse values)))
2839 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2840 handler-name
2841 &optional signal-list)
2842 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2843 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2844 buffer with `gdb-bind-function-to-buffer'.
2846 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2847 defined trigger is called with an argument from SIGNAL-LIST. It's
2848 not recommended to define triggers with empty SIGNAL-LIST.
2849 Normally triggers should respond at least to the `update' signal.
2851 Normally the trigger defined by this command must be called from
2852 the buffer where HANDLER-NAME must work. This should be done so
2853 that buffer-local thread number may be used in GDB-COMMAND (by
2854 calling `gdb-current-context-command').
2855 `gdb-bind-function-to-buffer' is used to achieve this, see
2856 `gdb-get-buffer-create'.
2858 Triggers defined by this command are meant to be used as a
2859 trigger argument when describing buffer types with
2860 `gdb-set-buffer-rules'."
2861 `(defun ,trigger-name (&optional signal)
2862 (when
2863 (or (not ,signal-list)
2864 (memq signal ,signal-list))
2865 (gdb-input ,gdb-command
2866 (gdb-bind-function-to-buffer ',handler-name (current-buffer))
2867 (cons (current-buffer) ',trigger-name)))))
2869 ;; Used by disassembly buffer only, the rest use
2870 ;; def-gdb-trigger-and-handler
2871 (defmacro def-gdb-auto-update-handler (handler-name custom-defun
2872 &optional nopreserve)
2873 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2875 Handlers are normally called from the buffers they put output in.
2877 Erase current buffer and evaluate CUSTOM-DEFUN.
2878 Then call `gdb-update-buffer-name'.
2880 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2881 `(defun ,handler-name ()
2882 (let* ((inhibit-read-only t)
2883 ,@(unless nopreserve
2884 '((window (get-buffer-window (current-buffer) 0))
2885 (start (window-start window))
2886 (p (window-point window)))))
2887 (erase-buffer)
2888 (,custom-defun)
2889 (gdb-update-buffer-name)
2890 ,@(when (not nopreserve)
2891 '((set-window-start window start)
2892 (set-window-point window p))))))
2894 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2895 handler-name custom-defun
2896 &optional signal-list)
2897 "Define trigger and handler.
2899 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2900 See `def-gdb-auto-update-trigger'.
2902 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2903 See `def-gdb-auto-update-handler'."
2904 `(progn
2905 (def-gdb-auto-update-trigger ,trigger-name
2906 ,gdb-command
2907 ,handler-name ,signal-list)
2908 (def-gdb-auto-update-handler ,handler-name
2909 ,custom-defun)))
2913 ;; Breakpoint buffer : This displays the output of `-break-list'.
2914 (def-gdb-trigger-and-handler
2915 gdb-invalidate-breakpoints "-break-list"
2916 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2917 '(start update))
2919 (gdb-set-buffer-rules
2920 'gdb-breakpoints-buffer
2921 'gdb-breakpoints-buffer-name
2922 'gdb-breakpoints-mode
2923 'gdb-invalidate-breakpoints)
2925 (defun gdb-breakpoints-list-handler-custom ()
2926 (let ((breakpoints-list (bindat-get-field
2927 (gdb-json-partial-output "bkpt" "script")
2928 'BreakpointTable 'body))
2929 (table (make-gdb-table)))
2930 (setq gdb-breakpoints-list nil)
2931 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2932 (dolist (breakpoint breakpoints-list)
2933 (add-to-list 'gdb-breakpoints-list
2934 (cons (bindat-get-field breakpoint 'number)
2935 breakpoint))
2936 (let ((at (bindat-get-field breakpoint 'at))
2937 (pending (bindat-get-field breakpoint 'pending))
2938 (func (bindat-get-field breakpoint 'func))
2939 (type (bindat-get-field breakpoint 'type)))
2940 (gdb-table-add-row table
2941 (list
2942 (bindat-get-field breakpoint 'number)
2943 (or type "")
2944 (or (bindat-get-field breakpoint 'disp) "")
2945 (let ((flag (bindat-get-field breakpoint 'enabled)))
2946 (if (string-equal flag "y")
2947 (eval-when-compile
2948 (propertize "y" 'font-lock-face
2949 font-lock-warning-face))
2950 (eval-when-compile
2951 (propertize "n" 'font-lock-face
2952 font-lock-comment-face))))
2953 (bindat-get-field breakpoint 'addr)
2954 (or (bindat-get-field breakpoint 'times) "")
2955 (if (and type (string-match ".*watchpoint" type))
2956 (bindat-get-field breakpoint 'what)
2957 (or pending at
2958 (concat "in "
2959 (propertize (or func "unknown")
2960 'font-lock-face font-lock-function-name-face)
2961 (gdb-frame-location breakpoint)))))
2962 ;; Add clickable properties only for breakpoints with file:line
2963 ;; information
2964 (append (list 'gdb-breakpoint breakpoint)
2965 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2966 mouse-face highlight))))))
2967 (insert (gdb-table-string table " "))
2968 (gdb-place-breakpoints)))
2970 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2971 (defun gdb-place-breakpoints ()
2972 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2973 (dolist (buffer (buffer-list))
2974 (with-current-buffer buffer
2975 (if (and (eq gud-minor-mode 'gdbmi)
2976 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2977 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2978 (dolist (breakpoint gdb-breakpoints-list)
2979 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2980 ; an associative list
2981 (line (bindat-get-field breakpoint 'line)))
2982 (when line
2983 (let ((file (bindat-get-field breakpoint 'fullname))
2984 (flag (bindat-get-field breakpoint 'enabled))
2985 (bptno (bindat-get-field breakpoint 'number)))
2986 (unless (and file (file-exists-p file))
2987 (setq file (cdr (assoc bptno gdb-location-alist))))
2988 (if (or (null file)
2989 (string-equal file "File not found"))
2990 ;; If the full filename is not recorded in the
2991 ;; breakpoint structure or in `gdb-location-alist', use
2992 ;; -file-list-exec-source-file to extract it.
2993 (when (setq file (bindat-get-field breakpoint 'file))
2994 (gdb-input (concat "list " file ":1") 'ignore)
2995 (gdb-input "-file-list-exec-source-file"
2996 `(lambda () (gdb-get-location
2997 ,bptno ,line ,flag))))
2998 (with-current-buffer (find-file-noselect file 'nowarn)
2999 (gdb-init-buffer)
3000 ;; Only want one breakpoint icon at each location.
3001 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
3002 (string-to-number line)))))))))
3004 (defconst gdb-source-file-regexp
3005 (concat "fullname=\\(" gdb--string-regexp "\\)"))
3007 (defun gdb-get-location (bptno line flag)
3008 "Find the directory containing the relevant source file.
3009 Put in buffer and place breakpoint icon."
3010 (goto-char (point-min))
3011 (catch 'file-not-found
3012 (if (re-search-forward gdb-source-file-regexp nil t)
3013 (delete (cons bptno "File not found") gdb-location-alist)
3014 ;; FIXME: Why/how do we use (match-string 1) when the search failed?
3015 (push (cons bptno (match-string 1)) gdb-location-alist)
3016 (gdb-resync)
3017 (unless (assoc bptno gdb-location-alist)
3018 (push (cons bptno "File not found") gdb-location-alist)
3019 (message-box "Cannot find source file for breakpoint location.
3020 Add directory to search path for source files using the GDB command, dir."))
3021 (throw 'file-not-found nil))
3022 (with-current-buffer (find-file-noselect (match-string 1))
3023 (gdb-init-buffer)
3024 ;; only want one breakpoint icon at each location
3025 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
3027 (add-hook 'find-file-hook 'gdb-find-file-hook)
3029 (defun gdb-find-file-hook ()
3030 "Set up buffer for debugging if file is part of the source code
3031 of the current session."
3032 (if (and (buffer-name gud-comint-buffer)
3033 ;; in case gud or gdb-ui is just loaded
3034 gud-comint-buffer
3035 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3036 'gdbmi))
3037 (if (member buffer-file-name gdb-source-file-list)
3038 (with-current-buffer (find-buffer-visiting buffer-file-name)
3039 (gdb-init-buffer)))))
3041 (declare-function gud-remove "gdb-mi" t t) ; gud-def
3042 (declare-function gud-break "gdb-mi" t t) ; gud-def
3043 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
3045 (defun gdb-mouse-set-clear-breakpoint (event)
3046 "Set/clear breakpoint in left fringe/margin at mouse click.
3047 If not in a source or disassembly buffer just set point."
3048 (interactive "e")
3049 (mouse-minibuffer-check event)
3050 (let ((posn (event-end event)))
3051 (with-selected-window (posn-window posn)
3052 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
3053 (if (numberp (posn-point posn))
3054 (save-excursion
3055 (goto-char (posn-point posn))
3056 (if (or (posn-object posn)
3057 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
3058 'breakpoint))
3059 (gud-remove nil)
3060 (gud-break nil)))))
3061 (posn-set-point posn))))
3063 (defun gdb-mouse-toggle-breakpoint-margin (event)
3064 "Enable/disable breakpoint in left margin with mouse click."
3065 (interactive "e")
3066 (mouse-minibuffer-check event)
3067 (let ((posn (event-end event)))
3068 (if (numberp (posn-point posn))
3069 (with-selected-window (posn-window posn)
3070 (save-excursion
3071 (goto-char (posn-point posn))
3072 (if (posn-object posn)
3073 (gud-basic-call
3074 (let ((bptno (get-text-property
3075 0 'gdb-bptno (car (posn-string posn)))))
3076 (concat
3077 (if (get-text-property
3078 0 'gdb-enabled (car (posn-string posn)))
3079 "-break-disable "
3080 "-break-enable ")
3081 bptno)))))))))
3083 (defun gdb-mouse-toggle-breakpoint-fringe (event)
3084 "Enable/disable breakpoint in left fringe with mouse click."
3085 (interactive "e")
3086 (mouse-minibuffer-check event)
3087 (let* ((posn (event-end event))
3088 (pos (posn-point posn))
3089 obj)
3090 (when (numberp pos)
3091 (with-selected-window (posn-window posn)
3092 (with-current-buffer (window-buffer)
3093 (goto-char pos)
3094 (dolist (overlay (overlays-in pos pos))
3095 (when (overlay-get overlay 'put-break)
3096 (setq obj (overlay-get overlay 'before-string))))
3097 (when (stringp obj)
3098 (gud-basic-call
3099 (concat
3100 (if (get-text-property 0 'gdb-enabled obj)
3101 "-break-disable "
3102 "-break-enable ")
3103 (get-text-property 0 'gdb-bptno obj)))))))))
3105 (defun gdb-breakpoints-buffer-name ()
3106 (concat "*breakpoints of " (gdb-get-target-string) "*"))
3108 (defun gdb-display-breakpoints-buffer (&optional thread)
3109 "Display GDB breakpoints."
3110 (interactive)
3111 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
3113 (defun gdb-frame-breakpoints-buffer (&optional thread)
3114 "Display GDB breakpoints in another frame."
3115 (interactive)
3116 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
3117 gdb-display-buffer-other-frame-action))
3119 (defvar gdb-breakpoints-mode-map
3120 (let ((map (make-sparse-keymap))
3121 (menu (make-sparse-keymap "Breakpoints")))
3122 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
3123 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
3124 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
3125 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
3126 (suppress-keymap map)
3127 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
3128 (define-key map " " 'gdb-toggle-breakpoint)
3129 (define-key map "D" 'gdb-delete-breakpoint)
3130 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
3131 (define-key map "q" 'gdb-delete-frame-or-window)
3132 (define-key map "\r" 'gdb-goto-breakpoint)
3133 (define-key map "\t" (lambda ()
3134 (interactive)
3135 (gdb-set-window-buffer
3136 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
3137 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3138 (define-key map [follow-link] 'mouse-face)
3139 map))
3141 (defun gdb-delete-frame-or-window ()
3142 "Delete frame if there is only one window. Otherwise delete the window."
3143 (interactive)
3144 (if (one-window-p) (delete-frame)
3145 (delete-window)))
3147 ;;from make-mode-line-mouse-map
3148 (defun gdb-make-header-line-mouse-map (mouse function) "\
3149 Return a keymap with single entry for mouse key MOUSE on the header line.
3150 MOUSE is defined to run function FUNCTION with no args in the buffer
3151 corresponding to the mode line clicked."
3152 (let ((map (make-sparse-keymap)))
3153 (define-key map (vector 'header-line mouse) function)
3154 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3155 map))
3157 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3158 `(propertize ,name
3159 'help-echo ,help-echo
3160 'mouse-face ',mouse-face
3161 'face ',face
3162 'local-map
3163 (gdb-make-header-line-mouse-map
3164 'mouse-1
3165 (lambda (event) (interactive "e")
3166 (save-selected-window
3167 (select-window (posn-window (event-start event)))
3168 (gdb-set-window-buffer
3169 (gdb-get-buffer-create ',buffer) t) )))))
3172 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3173 ;;; Threads view
3175 (defun gdb-threads-buffer-name ()
3176 (concat "*threads of " (gdb-get-target-string) "*"))
3178 (defun gdb-display-threads-buffer (&optional thread)
3179 "Display GDB threads."
3180 (interactive)
3181 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3183 (defun gdb-frame-threads-buffer (&optional thread)
3184 "Display GDB threads in another frame."
3185 (interactive)
3186 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3187 gdb-display-buffer-other-frame-action))
3189 (def-gdb-trigger-and-handler
3190 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3191 gdb-thread-list-handler gdb-thread-list-handler-custom
3192 '(start update update-threads))
3194 (gdb-set-buffer-rules
3195 'gdb-threads-buffer
3196 'gdb-threads-buffer-name
3197 'gdb-threads-mode
3198 'gdb-invalidate-threads)
3200 (defvar gdb-threads-font-lock-keywords
3201 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3202 (" \\(stopped\\)" (1 font-lock-warning-face))
3203 (" \\(running\\)" (1 font-lock-string-face))
3204 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3205 "Font lock keywords used in `gdb-threads-mode'.")
3207 (defvar gdb-threads-mode-map
3208 (let ((map (make-sparse-keymap)))
3209 (define-key map "\r" 'gdb-select-thread)
3210 (define-key map "f" 'gdb-display-stack-for-thread)
3211 (define-key map "F" 'gdb-frame-stack-for-thread)
3212 (define-key map "l" 'gdb-display-locals-for-thread)
3213 (define-key map "L" 'gdb-frame-locals-for-thread)
3214 (define-key map "r" 'gdb-display-registers-for-thread)
3215 (define-key map "R" 'gdb-frame-registers-for-thread)
3216 (define-key map "d" 'gdb-display-disassembly-for-thread)
3217 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3218 (define-key map "i" 'gdb-interrupt-thread)
3219 (define-key map "c" 'gdb-continue-thread)
3220 (define-key map "s" 'gdb-step-thread)
3221 (define-key map "\t"
3222 (lambda ()
3223 (interactive)
3224 (gdb-set-window-buffer
3225 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3226 (define-key map [mouse-2] 'gdb-select-thread)
3227 (define-key map [follow-link] 'mouse-face)
3228 map))
3230 (defvar gdb-threads-header
3231 (list
3232 (gdb-propertize-header
3233 "Breakpoints" gdb-breakpoints-buffer
3234 "mouse-1: select" mode-line-highlight mode-line-inactive)
3236 (gdb-propertize-header "Threads" gdb-threads-buffer
3237 nil nil mode-line)))
3239 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3240 "Major mode for GDB threads."
3241 (setq gdb-thread-position (make-marker))
3242 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3243 (setq header-line-format gdb-threads-header)
3244 (set (make-local-variable 'font-lock-defaults)
3245 '(gdb-threads-font-lock-keywords))
3246 'gdb-invalidate-threads)
3248 (defun gdb-thread-list-handler-custom ()
3249 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3250 (table (make-gdb-table))
3251 (marked-line nil))
3252 (setq gdb-threads-list nil)
3253 (setq gdb-running-threads-count 0)
3254 (setq gdb-stopped-threads-count 0)
3255 (set-marker gdb-thread-position nil)
3257 (dolist (thread (reverse threads-list))
3258 (let ((running (equal (bindat-get-field thread 'state) "running")))
3259 (add-to-list 'gdb-threads-list
3260 (cons (bindat-get-field thread 'id)
3261 thread))
3262 (cl-incf (if running
3263 gdb-running-threads-count
3264 gdb-stopped-threads-count))
3266 (gdb-table-add-row
3267 table
3268 (list
3269 (bindat-get-field thread 'id)
3270 (concat
3271 (if gdb-thread-buffer-verbose-names
3272 (concat (bindat-get-field thread 'target-id) " ") "")
3273 (bindat-get-field thread 'state)
3274 ;; Include frame information for stopped threads
3275 (if (not running)
3276 (concat
3277 " in " (bindat-get-field thread 'frame 'func)
3278 (if gdb-thread-buffer-arguments
3279 (concat
3280 " ("
3281 (let ((args (bindat-get-field thread 'frame 'args)))
3282 (mapconcat
3283 (lambda (arg)
3284 (apply #'format "%s=%s"
3285 (gdb-get-many-fields arg 'name 'value)))
3286 args ","))
3287 ")")
3289 (if gdb-thread-buffer-locations
3290 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3291 (if gdb-thread-buffer-addresses
3292 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3293 "")))
3294 (list
3295 'gdb-thread thread
3296 'mouse-face 'highlight
3297 'help-echo "mouse-2, RET: select thread")))
3298 (when (string-equal gdb-thread-number
3299 (bindat-get-field thread 'id))
3300 (setq marked-line (length gdb-threads-list))))
3301 (insert (gdb-table-string table " "))
3302 (when marked-line
3303 (gdb-mark-line marked-line gdb-thread-position)))
3304 ;; We update gud-running here because we need to make sure that
3305 ;; gdb-threads-list is up-to-date
3306 (gdb-update-gud-running)
3307 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3309 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3310 "Define a NAME command which will act upon thread on the current line.
3312 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3313 be the value of `gdb-thread' property of the current line.
3314 If `gdb-thread' is nil, error is signaled."
3315 `(defun ,name (&optional event)
3316 ,(when doc doc)
3317 (interactive (list last-input-event))
3318 (if event (posn-set-point (event-end event)))
3319 (save-excursion
3320 (beginning-of-line)
3321 (let ((thread (get-text-property (point) 'gdb-thread)))
3322 (if thread
3323 ,custom-defun
3324 (error "Not recognized as thread line"))))))
3326 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3327 &optional doc)
3328 "Define a NAME which will call BUFFER-COMMAND with id of thread
3329 on the current line."
3330 `(def-gdb-thread-buffer-command ,name
3331 (,buffer-command (bindat-get-field thread 'id))
3332 ,doc))
3334 (def-gdb-thread-buffer-command gdb-select-thread
3335 (let ((new-id (bindat-get-field thread 'id)))
3336 (gdb-setq-thread-number new-id)
3337 (gdb-input (concat "-thread-select " new-id) 'ignore)
3338 (gdb-update))
3339 "Select the thread at current line of threads buffer.")
3341 (def-gdb-thread-buffer-simple-command
3342 gdb-display-stack-for-thread
3343 gdb-preemptively-display-stack-buffer
3344 "Display stack buffer for the thread at current line.")
3346 (def-gdb-thread-buffer-simple-command
3347 gdb-display-locals-for-thread
3348 gdb-preemptively-display-locals-buffer
3349 "Display locals buffer for the thread at current line.")
3351 (def-gdb-thread-buffer-simple-command
3352 gdb-display-registers-for-thread
3353 gdb-preemptively-display-registers-buffer
3354 "Display registers buffer for the thread at current line.")
3356 (def-gdb-thread-buffer-simple-command
3357 gdb-display-disassembly-for-thread
3358 gdb-preemptively-display-disassembly-buffer
3359 "Display disassembly buffer for the thread at current line.")
3361 (def-gdb-thread-buffer-simple-command
3362 gdb-frame-stack-for-thread
3363 gdb-frame-stack-buffer
3364 "Display another frame with stack buffer for thread at current line.")
3366 (def-gdb-thread-buffer-simple-command
3367 gdb-frame-locals-for-thread
3368 gdb-frame-locals-buffer
3369 "Display another frame with locals buffer for thread at current line.")
3371 (def-gdb-thread-buffer-simple-command
3372 gdb-frame-registers-for-thread
3373 gdb-frame-registers-buffer
3374 "Display another frame with registers buffer for the thread at current line.")
3376 (def-gdb-thread-buffer-simple-command
3377 gdb-frame-disassembly-for-thread
3378 gdb-frame-disassembly-buffer
3379 "Display another frame with disassembly buffer for the thread at current line.")
3381 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3382 "Define a NAME which will execute GUD-COMMAND with
3383 `gdb-thread-number' locally bound to id of thread on the current
3384 line."
3385 `(def-gdb-thread-buffer-command ,name
3386 (if gdb-non-stop
3387 (let ((gdb-thread-number (bindat-get-field thread 'id))
3388 (gdb-gud-control-all-threads nil))
3389 (call-interactively #',gud-command))
3390 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3391 ,doc))
3393 (def-gdb-thread-buffer-gud-command
3394 gdb-interrupt-thread
3395 gud-stop-subjob
3396 "Interrupt thread at current line.")
3398 ;; Defined opaquely in M-x gdb via gud-def.
3399 (declare-function gud-cont "gdb-mi" (arg) t)
3401 (def-gdb-thread-buffer-gud-command
3402 gdb-continue-thread
3403 gud-cont
3404 "Continue thread at current line.")
3406 (declare-function gud-step "gdb-mi" (arg) t)
3408 (def-gdb-thread-buffer-gud-command
3409 gdb-step-thread
3410 gud-step
3411 "Step thread at current line.")
3414 ;;; Memory view
3416 (defcustom gdb-memory-rows 8
3417 "Number of data rows in memory window."
3418 :type 'integer
3419 :group 'gud
3420 :version "23.2")
3422 (defcustom gdb-memory-columns 4
3423 "Number of data columns in memory window."
3424 :type 'integer
3425 :group 'gud
3426 :version "23.2")
3428 (defcustom gdb-memory-format "x"
3429 "Display format of data items in memory window."
3430 :type '(choice (const :tag "Hexadecimal" "x")
3431 (const :tag "Signed decimal" "d")
3432 (const :tag "Unsigned decimal" "u")
3433 (const :tag "Octal" "o")
3434 (const :tag "Binary" "t"))
3435 :group 'gud
3436 :version "22.1")
3438 (defcustom gdb-memory-unit 4
3439 "Unit size of data items in memory window."
3440 :type '(choice (const :tag "Byte" 1)
3441 (const :tag "Halfword" 2)
3442 (const :tag "Word" 4)
3443 (const :tag "Giant word" 8))
3444 :group 'gud
3445 :version "23.2")
3447 (def-gdb-trigger-and-handler
3448 gdb-invalidate-memory
3449 (format "-data-read-memory %s %s %d %d %d"
3450 gdb-memory-address
3451 gdb-memory-format
3452 gdb-memory-unit
3453 gdb-memory-rows
3454 gdb-memory-columns)
3455 gdb-read-memory-handler
3456 gdb-read-memory-custom
3457 '(start update))
3459 (gdb-set-buffer-rules
3460 'gdb-memory-buffer
3461 'gdb-memory-buffer-name
3462 'gdb-memory-mode
3463 'gdb-invalidate-memory)
3465 (defun gdb-memory-column-width (size format)
3466 "Return length of string with memory unit of SIZE in FORMAT.
3468 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3469 in `gdb-memory-format'."
3470 (let ((format-base (cdr (assoc format
3471 '(("x" . 16)
3472 ("d" . 10) ("u" . 10)
3473 ("o" . 8)
3474 ("t" . 2))))))
3475 (if format-base
3476 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3477 (cond ((string-equal format "x")
3478 (+ 2 res)) ; hexadecimal numbers have 0x in front
3479 ((or (string-equal format "d")
3480 (string-equal format "o"))
3481 (1+ res))
3482 (t res)))
3483 (error "Unknown format"))))
3485 (defun gdb-read-memory-custom ()
3486 (let* ((res (gdb-json-partial-output))
3487 (err-msg (bindat-get-field res 'msg)))
3488 (if (not err-msg)
3489 (let ((memory (bindat-get-field res 'memory)))
3490 (setq gdb-memory-address (bindat-get-field res 'addr))
3491 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3492 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3493 (setq gdb-memory-last-address gdb-memory-address)
3494 (dolist (row memory)
3495 (insert (concat (bindat-get-field row 'addr) ":"))
3496 (dolist (column (bindat-get-field row 'data))
3497 (insert (gdb-pad-string column
3498 (+ 2 (gdb-memory-column-width
3499 gdb-memory-unit
3500 gdb-memory-format)))))
3501 (newline)))
3502 ;; Show last page instead of empty buffer when out of bounds
3503 (progn
3504 (let ((gdb-memory-address gdb-memory-last-address))
3505 (gdb-invalidate-memory 'update)
3506 (error err-msg))))))
3508 (defvar gdb-memory-mode-map
3509 (let ((map (make-sparse-keymap)))
3510 (suppress-keymap map t)
3511 (define-key map "q" 'kill-current-buffer)
3512 (define-key map "n" 'gdb-memory-show-next-page)
3513 (define-key map "p" 'gdb-memory-show-previous-page)
3514 (define-key map "a" 'gdb-memory-set-address)
3515 (define-key map "t" 'gdb-memory-format-binary)
3516 (define-key map "o" 'gdb-memory-format-octal)
3517 (define-key map "u" 'gdb-memory-format-unsigned)
3518 (define-key map "d" 'gdb-memory-format-signed)
3519 (define-key map "x" 'gdb-memory-format-hexadecimal)
3520 (define-key map "b" 'gdb-memory-unit-byte)
3521 (define-key map "h" 'gdb-memory-unit-halfword)
3522 (define-key map "w" 'gdb-memory-unit-word)
3523 (define-key map "g" 'gdb-memory-unit-giant)
3524 (define-key map "R" 'gdb-memory-set-rows)
3525 (define-key map "C" 'gdb-memory-set-columns)
3526 map))
3528 (defun gdb-memory-set-address-event (event)
3529 "Handle a click on address field in memory buffer header."
3530 (interactive "e")
3531 (save-selected-window
3532 (select-window (posn-window (event-start event)))
3533 (gdb-memory-set-address)))
3535 ;; Non-event version for use within keymap
3536 (defun gdb-memory-set-address ()
3537 "Set the start memory address."
3538 (interactive)
3539 (let ((arg (read-from-minibuffer "Memory address: ")))
3540 (setq gdb-memory-address arg))
3541 (gdb-invalidate-memory 'update))
3543 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3544 "Define a function NAME which reads new VAR value from minibuffer."
3545 `(defun ,name (event)
3546 ,(when doc doc)
3547 (interactive "e")
3548 (save-selected-window
3549 (select-window (posn-window (event-start event)))
3550 (let* ((arg (read-from-minibuffer ,echo-string))
3551 (count (string-to-number arg)))
3552 (if (<= count 0)
3553 (error "Positive number only")
3554 (customize-set-variable ',variable count)
3555 (gdb-invalidate-memory 'update))))))
3557 (def-gdb-set-positive-number
3558 gdb-memory-set-rows
3559 gdb-memory-rows
3560 "Rows: "
3561 "Set the number of data rows in memory window.")
3563 (def-gdb-set-positive-number
3564 gdb-memory-set-columns
3565 gdb-memory-columns
3566 "Columns: "
3567 "Set the number of data columns in memory window.")
3569 (defmacro def-gdb-memory-format (name format doc)
3570 "Define a function NAME to switch memory buffer to use FORMAT.
3572 DOC is an optional documentation string."
3573 `(defun ,name () ,(when doc doc)
3574 (interactive)
3575 (customize-set-variable 'gdb-memory-format ,format)
3576 (gdb-invalidate-memory 'update)))
3578 (def-gdb-memory-format
3579 gdb-memory-format-binary "t"
3580 "Set the display format to binary.")
3582 (def-gdb-memory-format
3583 gdb-memory-format-octal "o"
3584 "Set the display format to octal.")
3586 (def-gdb-memory-format
3587 gdb-memory-format-unsigned "u"
3588 "Set the display format to unsigned decimal.")
3590 (def-gdb-memory-format
3591 gdb-memory-format-signed "d"
3592 "Set the display format to decimal.")
3594 (def-gdb-memory-format
3595 gdb-memory-format-hexadecimal "x"
3596 "Set the display format to hexadecimal.")
3598 (defvar gdb-memory-format-map
3599 (let ((map (make-sparse-keymap)))
3600 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3601 map)
3602 "Keymap to select format in the header line.")
3604 (defvar gdb-memory-format-menu
3605 (let ((map (make-sparse-keymap "Format")))
3607 (define-key map [binary]
3608 '(menu-item "Binary" gdb-memory-format-binary
3609 :button (:radio . (equal gdb-memory-format "t"))))
3610 (define-key map [octal]
3611 '(menu-item "Octal" gdb-memory-format-octal
3612 :button (:radio . (equal gdb-memory-format "o"))))
3613 (define-key map [unsigned]
3614 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3615 :button (:radio . (equal gdb-memory-format "u"))))
3616 (define-key map [signed]
3617 '(menu-item "Signed Decimal" gdb-memory-format-signed
3618 :button (:radio . (equal gdb-memory-format "d"))))
3619 (define-key map [hexadecimal]
3620 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3621 :button (:radio . (equal gdb-memory-format "x"))))
3622 map)
3623 "Menu of display formats in the header line.")
3625 (defun gdb-memory-format-menu (event)
3626 (interactive "@e")
3627 (x-popup-menu event gdb-memory-format-menu))
3629 (defun gdb-memory-format-menu-1 (event)
3630 (interactive "e")
3631 (save-selected-window
3632 (select-window (posn-window (event-start event)))
3633 (let* ((selection (gdb-memory-format-menu event))
3634 (binding (and selection (lookup-key gdb-memory-format-menu
3635 (vector (car selection))))))
3636 (if binding (call-interactively binding)))))
3638 (defmacro def-gdb-memory-unit (name unit-size doc)
3639 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3641 DOC is an optional documentation string."
3642 `(defun ,name () ,(when doc doc)
3643 (interactive)
3644 (customize-set-variable 'gdb-memory-unit ,unit-size)
3645 (gdb-invalidate-memory 'update)))
3647 (def-gdb-memory-unit gdb-memory-unit-giant 8
3648 "Set the unit size to giant words (eight bytes).")
3650 (def-gdb-memory-unit gdb-memory-unit-word 4
3651 "Set the unit size to words (four bytes).")
3653 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3654 "Set the unit size to halfwords (two bytes).")
3656 (def-gdb-memory-unit gdb-memory-unit-byte 1
3657 "Set the unit size to bytes.")
3659 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3660 "Define a function NAME which show new address in memory buffer.
3662 The defined function switches Memory buffer to show address
3663 stored in ADDRESS-VAR variable.
3665 DOC is an optional documentation string."
3666 `(defun ,name
3667 ,(when doc doc)
3668 (interactive)
3669 (let ((gdb-memory-address ,address-var))
3670 (gdb-invalidate-memory))))
3672 (def-gdb-memory-show-page gdb-memory-show-previous-page
3673 gdb-memory-prev-page)
3675 (def-gdb-memory-show-page gdb-memory-show-next-page
3676 gdb-memory-next-page)
3678 (defvar gdb-memory-unit-map
3679 (let ((map (make-sparse-keymap)))
3680 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3681 map)
3682 "Keymap to select units in the header line.")
3684 (defvar gdb-memory-unit-menu
3685 (let ((map (make-sparse-keymap "Unit")))
3686 (define-key map [giantwords]
3687 '(menu-item "Giant words" gdb-memory-unit-giant
3688 :button (:radio . (equal gdb-memory-unit 8))))
3689 (define-key map [words]
3690 '(menu-item "Words" gdb-memory-unit-word
3691 :button (:radio . (equal gdb-memory-unit 4))))
3692 (define-key map [halfwords]
3693 '(menu-item "Halfwords" gdb-memory-unit-halfword
3694 :button (:radio . (equal gdb-memory-unit 2))))
3695 (define-key map [bytes]
3696 '(menu-item "Bytes" gdb-memory-unit-byte
3697 :button (:radio . (equal gdb-memory-unit 1))))
3698 map)
3699 "Menu of units in the header line.")
3701 (defun gdb-memory-unit-menu (event)
3702 (interactive "@e")
3703 (x-popup-menu event gdb-memory-unit-menu))
3705 (defun gdb-memory-unit-menu-1 (event)
3706 (interactive "e")
3707 (save-selected-window
3708 (select-window (posn-window (event-start event)))
3709 (let* ((selection (gdb-memory-unit-menu event))
3710 (binding (and selection (lookup-key gdb-memory-unit-menu
3711 (vector (car selection))))))
3712 (if binding (call-interactively binding)))))
3714 (defvar gdb-memory-font-lock-keywords
3715 '(;; <__function.name+n>
3716 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3717 (1 font-lock-function-name-face)))
3718 "Font lock keywords used in `gdb-memory-mode'.")
3720 (defvar gdb-memory-header
3721 '(:eval
3722 (concat
3723 "Start address["
3724 (propertize "-"
3725 'face font-lock-warning-face
3726 'help-echo "mouse-1: decrement address"
3727 'mouse-face 'mode-line-highlight
3728 'local-map (gdb-make-header-line-mouse-map
3729 'mouse-1
3730 #'gdb-memory-show-previous-page))
3732 (propertize "+"
3733 'face font-lock-warning-face
3734 'help-echo "mouse-1: increment address"
3735 'mouse-face 'mode-line-highlight
3736 'local-map (gdb-make-header-line-mouse-map
3737 'mouse-1
3738 #'gdb-memory-show-next-page))
3739 "]: "
3740 (propertize gdb-memory-address
3741 'face font-lock-warning-face
3742 'help-echo "mouse-1: set start address"
3743 'mouse-face 'mode-line-highlight
3744 'local-map (gdb-make-header-line-mouse-map
3745 'mouse-1
3746 #'gdb-memory-set-address-event))
3747 " Rows: "
3748 (propertize (number-to-string gdb-memory-rows)
3749 'face font-lock-warning-face
3750 'help-echo "mouse-1: set number of columns"
3751 'mouse-face 'mode-line-highlight
3752 'local-map (gdb-make-header-line-mouse-map
3753 'mouse-1
3754 #'gdb-memory-set-rows))
3755 " Columns: "
3756 (propertize (number-to-string gdb-memory-columns)
3757 'face font-lock-warning-face
3758 'help-echo "mouse-1: set number of columns"
3759 'mouse-face 'mode-line-highlight
3760 'local-map (gdb-make-header-line-mouse-map
3761 'mouse-1
3762 #'gdb-memory-set-columns))
3763 " Display Format: "
3764 (propertize gdb-memory-format
3765 'face font-lock-warning-face
3766 'help-echo "mouse-3: select display format"
3767 'mouse-face 'mode-line-highlight
3768 'local-map gdb-memory-format-map)
3769 " Unit Size: "
3770 (propertize (number-to-string gdb-memory-unit)
3771 'face font-lock-warning-face
3772 'help-echo "mouse-3: select unit size"
3773 'mouse-face 'mode-line-highlight
3774 'local-map gdb-memory-unit-map)))
3775 "Header line used in `gdb-memory-mode'.")
3777 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3778 "Major mode for examining memory."
3779 (setq header-line-format gdb-memory-header)
3780 (set (make-local-variable 'font-lock-defaults)
3781 '(gdb-memory-font-lock-keywords))
3782 'gdb-invalidate-memory)
3784 (defun gdb-memory-buffer-name ()
3785 (concat "*memory of " (gdb-get-target-string) "*"))
3787 (defun gdb-display-memory-buffer (&optional thread)
3788 "Display GDB memory contents."
3789 (interactive)
3790 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3792 (defun gdb-frame-memory-buffer ()
3793 "Display memory contents in another frame."
3794 (interactive)
3795 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3796 gdb-display-buffer-other-frame-action))
3799 ;;; Disassembly view
3801 (defun gdb-disassembly-buffer-name ()
3802 (gdb-current-context-buffer-name
3803 (concat "disassembly of " (gdb-get-target-string))))
3805 (defun gdb-display-disassembly-buffer (&optional thread)
3806 "Display GDB disassembly information."
3807 (interactive)
3808 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3810 (def-gdb-preempt-display-buffer
3811 gdb-preemptively-display-disassembly-buffer
3812 'gdb-disassembly-buffer)
3814 (defun gdb-frame-disassembly-buffer (&optional thread)
3815 "Display GDB disassembly information in another frame."
3816 (interactive)
3817 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3818 gdb-display-buffer-other-frame-action))
3820 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3821 (let* ((frame (gdb-current-buffer-frame))
3822 (file (bindat-get-field frame 'fullname))
3823 (line (bindat-get-field frame 'line)))
3824 (if file
3825 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3826 ;; If we're unable to get a file name / line for $PC, simply
3827 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3828 ;; 150 bytes) instructions.
3829 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3830 gdb-disassembly-handler
3831 ;; We update disassembly only after we have actual frame information
3832 ;; about all threads, so no there's `update' signal in this list
3833 '(start update-disassembly))
3835 (def-gdb-auto-update-handler
3836 gdb-disassembly-handler
3837 gdb-disassembly-handler-custom
3840 (gdb-set-buffer-rules
3841 'gdb-disassembly-buffer
3842 'gdb-disassembly-buffer-name
3843 'gdb-disassembly-mode
3844 'gdb-invalidate-disassembly)
3846 (defvar gdb-disassembly-font-lock-keywords
3847 '(;; <__function.name+n>
3848 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3849 (1 font-lock-function-name-face))
3850 ;; 0xNNNNNNNN <__function.name+n>: opcode
3851 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3852 (4 font-lock-keyword-face))
3853 ;; %register(at least i386)
3854 ("%\\sw+" . font-lock-variable-name-face)
3855 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3856 (1 font-lock-comment-face)
3857 (2 font-lock-function-name-face))
3858 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3859 "Font lock keywords used in `gdb-disassembly-mode'.")
3861 (defvar gdb-disassembly-mode-map
3862 ;; TODO
3863 (let ((map (make-sparse-keymap)))
3864 (suppress-keymap map)
3865 (define-key map "q" 'kill-current-buffer)
3866 map))
3868 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3869 "Major mode for GDB disassembly information."
3870 ;; TODO Rename overlay variable for disassembly mode
3871 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3872 (setq fringes-outside-margins t)
3873 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3874 (set (make-local-variable 'font-lock-defaults)
3875 '(gdb-disassembly-font-lock-keywords))
3876 'gdb-invalidate-disassembly)
3878 (defun gdb-disassembly-handler-custom ()
3879 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3880 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3881 (table (make-gdb-table))
3882 (marked-line nil))
3883 (dolist (instr instructions)
3884 (gdb-table-add-row table
3885 (list
3886 (bindat-get-field instr 'address)
3887 (let
3888 ((func-name (bindat-get-field instr 'func-name))
3889 (offset (bindat-get-field instr 'offset)))
3890 (if func-name
3891 (format "<%s+%s>:" func-name offset)
3892 ""))
3893 (bindat-get-field instr 'inst)))
3894 (when (string-equal (bindat-get-field instr 'address)
3895 address)
3896 (progn
3897 (setq marked-line (length (gdb-table-rows table)))
3898 (setq fringe-indicator-alist
3899 (if (string-equal gdb-frame-number "0")
3901 '((overlay-arrow . hollow-right-triangle)))))))
3902 (insert (gdb-table-string table " "))
3903 (gdb-disassembly-place-breakpoints)
3904 ;; Mark current position with overlay arrow and scroll window to
3905 ;; that point
3906 (when marked-line
3907 (let ((window (get-buffer-window (current-buffer) 0)))
3908 (set-window-point window (gdb-mark-line marked-line
3909 gdb-disassembly-position))))
3910 (setq mode-name
3911 (gdb-current-context-mode-name
3912 (concat "Disassembly: "
3913 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3915 (defun gdb-disassembly-place-breakpoints ()
3916 (gdb-remove-breakpoint-icons (point-min) (point-max))
3917 (dolist (breakpoint gdb-breakpoints-list)
3918 (let* ((breakpoint (cdr breakpoint))
3919 (bptno (bindat-get-field breakpoint 'number))
3920 (flag (bindat-get-field breakpoint 'enabled))
3921 (address (bindat-get-field breakpoint 'addr)))
3922 (save-excursion
3923 (goto-char (point-min))
3924 (if (re-search-forward (concat "^" address) nil t)
3925 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3928 (defvar gdb-breakpoints-header
3929 (list
3930 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3931 nil nil mode-line)
3933 (gdb-propertize-header "Threads" gdb-threads-buffer
3934 "mouse-1: select" mode-line-highlight
3935 mode-line-inactive)))
3937 ;;; Breakpoints view
3938 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3939 "Major mode for gdb breakpoints."
3940 (setq header-line-format gdb-breakpoints-header)
3941 'gdb-invalidate-breakpoints)
3943 (defun gdb-toggle-breakpoint ()
3944 "Enable/disable breakpoint at current line of breakpoints buffer."
3945 (interactive)
3946 (save-excursion
3947 (beginning-of-line)
3948 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3949 (if breakpoint
3950 (gud-basic-call
3951 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3952 "-break-disable "
3953 "-break-enable ")
3954 (bindat-get-field breakpoint 'number)))
3955 (error "Not recognized as break/watchpoint line")))))
3957 (defun gdb-delete-breakpoint ()
3958 "Delete the breakpoint at current line of breakpoints buffer."
3959 (interactive)
3960 (save-excursion
3961 (beginning-of-line)
3962 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3963 (if breakpoint
3964 (gud-basic-call (concat "-break-delete "
3965 (bindat-get-field breakpoint 'number)))
3966 (error "Not recognized as break/watchpoint line")))))
3968 (defun gdb-goto-breakpoint (&optional event)
3969 "Go to the location of breakpoint at current line of breakpoints buffer."
3970 (interactive (list last-input-event))
3971 (if event (posn-set-point (event-end event)))
3972 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3973 (let ((window (get-buffer-window gud-comint-buffer)))
3974 (if window (save-selected-window (select-window window))))
3975 (save-excursion
3976 (beginning-of-line)
3977 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3978 (if breakpoint
3979 (let ((bptno (bindat-get-field breakpoint 'number))
3980 (file (bindat-get-field breakpoint 'fullname))
3981 (line (bindat-get-field breakpoint 'line)))
3982 (save-selected-window
3983 (let* ((buffer (find-file-noselect
3984 (if (file-exists-p file) file
3985 (cdr (assoc bptno gdb-location-alist)))))
3986 (window (or (gdb-display-source-buffer buffer)
3987 (display-buffer buffer))))
3988 (setq gdb-source-window window)
3989 (with-current-buffer buffer
3990 (goto-char (point-min))
3991 (forward-line (1- (string-to-number line)))
3992 (set-window-point window (point))))))
3993 (error "Not recognized as break/watchpoint line")))))
3996 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3998 (def-gdb-trigger-and-handler
3999 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
4000 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
4001 '(start update))
4003 (gdb-set-buffer-rules
4004 'gdb-stack-buffer
4005 'gdb-stack-buffer-name
4006 'gdb-frames-mode
4007 'gdb-invalidate-frames)
4009 (defun gdb-frame-location (frame)
4010 "Return \" of file:line\" or \" of library\" for structure FRAME.
4012 FRAME must have either \"file\" and \"line\" members or \"from\"
4013 member."
4014 (let ((file (bindat-get-field frame 'file))
4015 (line (bindat-get-field frame 'line))
4016 (from (bindat-get-field frame 'from)))
4017 (let ((res (or (and file line (concat file ":" line))
4018 from)))
4019 (if res (concat " of " res) ""))))
4021 (defun gdb-stack-list-frames-custom ()
4022 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
4023 (table (make-gdb-table)))
4024 (set-marker gdb-stack-position nil)
4025 (dolist (frame stack)
4026 (gdb-table-add-row table
4027 (list
4028 (bindat-get-field frame 'level)
4029 "in"
4030 (concat
4031 (bindat-get-field frame 'func)
4032 (if gdb-stack-buffer-locations
4033 (gdb-frame-location frame) "")
4034 (if gdb-stack-buffer-addresses
4035 (concat " at " (bindat-get-field frame 'addr)) "")))
4036 `(mouse-face highlight
4037 help-echo "mouse-2, RET: Select frame"
4038 gdb-frame ,frame)))
4039 (insert (gdb-table-string table " ")))
4040 (when (and gdb-frame-number
4041 (gdb-buffer-shows-main-thread-p))
4042 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
4043 gdb-stack-position))
4044 (setq mode-name
4045 (gdb-current-context-mode-name "Frames")))
4047 (defun gdb-stack-buffer-name ()
4048 (gdb-current-context-buffer-name
4049 (concat "stack frames of " (gdb-get-target-string))))
4051 (defun gdb-display-stack-buffer (&optional thread)
4052 "Display GDB backtrace for current stack."
4053 (interactive)
4054 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
4056 (def-gdb-preempt-display-buffer
4057 gdb-preemptively-display-stack-buffer
4058 'gdb-stack-buffer nil t)
4060 (defun gdb-frame-stack-buffer (&optional thread)
4061 "Display GDB backtrace for current stack in another frame."
4062 (interactive)
4063 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
4064 gdb-display-buffer-other-frame-action))
4066 (defvar gdb-frames-mode-map
4067 (let ((map (make-sparse-keymap)))
4068 (suppress-keymap map)
4069 (define-key map "q" 'kill-current-buffer)
4070 (define-key map "\r" 'gdb-select-frame)
4071 (define-key map [mouse-2] 'gdb-select-frame)
4072 (define-key map [follow-link] 'mouse-face)
4073 map))
4075 (defvar gdb-frames-font-lock-keywords
4076 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
4077 "Font lock keywords used in `gdb-frames-mode'.")
4079 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
4080 "Major mode for gdb call stack."
4081 (setq gdb-stack-position (make-marker))
4082 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
4083 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
4084 (set (make-local-variable 'font-lock-defaults)
4085 '(gdb-frames-font-lock-keywords))
4086 'gdb-invalidate-frames)
4088 (defun gdb-select-frame (&optional event)
4089 "Select the frame and display the relevant source."
4090 (interactive (list last-input-event))
4091 (if event (posn-set-point (event-end event)))
4092 (let ((frame (get-text-property (point) 'gdb-frame)))
4093 (if frame
4094 (if (gdb-buffer-shows-main-thread-p)
4095 (let ((new-level (bindat-get-field frame 'level)))
4096 (setq gdb-frame-number new-level)
4097 (gdb-input (concat "-stack-select-frame " new-level)
4098 'ignore)
4099 (gdb-update))
4100 (error "Could not select frame for non-current thread"))
4101 (error "Not recognized as frame line"))))
4104 ;; Locals buffer.
4105 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
4106 (def-gdb-trigger-and-handler
4107 gdb-invalidate-locals
4108 (concat (gdb-current-context-command "-stack-list-locals")
4109 " --simple-values")
4110 gdb-locals-handler gdb-locals-handler-custom
4111 '(start update))
4113 (gdb-set-buffer-rules
4114 'gdb-locals-buffer
4115 'gdb-locals-buffer-name
4116 'gdb-locals-mode
4117 'gdb-invalidate-locals)
4119 (defvar gdb-locals-watch-map
4120 (let ((map (make-sparse-keymap)))
4121 (suppress-keymap map)
4122 (define-key map "\r" 'gud-watch)
4123 (define-key map [mouse-2] 'gud-watch)
4124 map)
4125 "Keymap to create watch expression of a complex data type local variable.")
4127 (defvar gdb-edit-locals-map-1
4128 (let ((map (make-sparse-keymap)))
4129 (suppress-keymap map)
4130 (define-key map "\r" 'gdb-edit-locals-value)
4131 (define-key map [mouse-2] 'gdb-edit-locals-value)
4132 map)
4133 "Keymap to edit value of a simple data type local variable.")
4135 (defun gdb-edit-locals-value (&optional event)
4136 "Assign a value to a variable displayed in the locals buffer."
4137 (interactive (list last-input-event))
4138 (save-excursion
4139 (if event (posn-set-point (event-end event)))
4140 (beginning-of-line)
4141 (let* ((var (bindat-get-field
4142 (get-text-property (point) 'gdb-local-variable) 'name))
4143 (value (read-string (format "New value (%s): " var))))
4144 (gud-basic-call
4145 (concat "-gdb-set variable " var " = " value)))))
4147 ;; Don't display values of arrays or structures.
4148 ;; These can be expanded using gud-watch.
4149 (defun gdb-locals-handler-custom ()
4150 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4151 (table (make-gdb-table)))
4152 (dolist (local locals-list)
4153 (let ((name (bindat-get-field local 'name))
4154 (value (bindat-get-field local 'value))
4155 (type (bindat-get-field local 'type)))
4156 (when (not value)
4157 (setq value "<complex data type>"))
4158 (if (or (not value)
4159 (string-match "\\0x" value))
4160 (add-text-properties 0 (length name)
4161 `(mouse-face highlight
4162 help-echo "mouse-2: create watch expression"
4163 local-map ,gdb-locals-watch-map)
4164 name)
4165 (add-text-properties 0 (length value)
4166 `(mouse-face highlight
4167 help-echo "mouse-2: edit value"
4168 local-map ,gdb-edit-locals-map-1)
4169 value))
4170 (gdb-table-add-row
4171 table
4172 (list
4173 (propertize type 'font-lock-face font-lock-type-face)
4174 (propertize name 'font-lock-face font-lock-variable-name-face)
4175 value)
4176 `(gdb-local-variable ,local))))
4177 (insert (gdb-table-string table " "))
4178 (setq mode-name
4179 (gdb-current-context-mode-name
4180 (concat "Locals: "
4181 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4183 (defvar gdb-locals-header
4184 (list
4185 (gdb-propertize-header "Locals" gdb-locals-buffer
4186 nil nil mode-line)
4188 (gdb-propertize-header "Registers" gdb-registers-buffer
4189 "mouse-1: select" mode-line-highlight
4190 mode-line-inactive)))
4192 (defvar gdb-locals-mode-map
4193 (let ((map (make-sparse-keymap)))
4194 (suppress-keymap map)
4195 (define-key map "q" 'kill-current-buffer)
4196 (define-key map "\t" (lambda ()
4197 (interactive)
4198 (gdb-set-window-buffer
4199 (gdb-get-buffer-create
4200 'gdb-registers-buffer
4201 gdb-thread-number) t)))
4202 map))
4204 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4205 "Major mode for gdb locals."
4206 (setq header-line-format gdb-locals-header)
4207 'gdb-invalidate-locals)
4209 (defun gdb-locals-buffer-name ()
4210 (gdb-current-context-buffer-name
4211 (concat "locals of " (gdb-get-target-string))))
4213 (defun gdb-display-locals-buffer (&optional thread)
4214 "Display the local variables of current GDB stack."
4215 (interactive)
4216 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4218 (def-gdb-preempt-display-buffer
4219 gdb-preemptively-display-locals-buffer
4220 'gdb-locals-buffer nil t)
4222 (defun gdb-frame-locals-buffer (&optional thread)
4223 "Display the local variables of the current GDB stack in another frame."
4224 (interactive)
4225 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4226 gdb-display-buffer-other-frame-action))
4229 ;; Registers buffer.
4231 (def-gdb-trigger-and-handler
4232 gdb-invalidate-registers
4233 (concat (gdb-current-context-command "-data-list-register-values") " x")
4234 gdb-registers-handler
4235 gdb-registers-handler-custom
4236 '(start update))
4238 (gdb-set-buffer-rules
4239 'gdb-registers-buffer
4240 'gdb-registers-buffer-name
4241 'gdb-registers-mode
4242 'gdb-invalidate-registers)
4244 (defun gdb-registers-handler-custom ()
4245 (when gdb-register-names
4246 (let ((register-values
4247 (bindat-get-field (gdb-json-partial-output) 'register-values))
4248 (table (make-gdb-table)))
4249 (dolist (register register-values)
4250 (let* ((register-number (bindat-get-field register 'number))
4251 (value (bindat-get-field register 'value))
4252 (register-name (nth (string-to-number register-number)
4253 gdb-register-names)))
4254 (gdb-table-add-row
4255 table
4256 (list
4257 (propertize register-name
4258 'font-lock-face font-lock-variable-name-face)
4259 (if (member register-number gdb-changed-registers)
4260 (propertize value 'font-lock-face font-lock-warning-face)
4261 value))
4262 `(mouse-face highlight
4263 help-echo "mouse-2: edit value"
4264 gdb-register-name ,register-name))))
4265 (insert (gdb-table-string table " ")))
4266 (setq mode-name
4267 (gdb-current-context-mode-name "Registers"))))
4269 (defun gdb-edit-register-value (&optional event)
4270 "Assign a value to a register displayed in the registers buffer."
4271 (interactive (list last-input-event))
4272 (save-excursion
4273 (if event (posn-set-point (event-end event)))
4274 (beginning-of-line)
4275 (let* ((var (bindat-get-field
4276 (get-text-property (point) 'gdb-register-name)))
4277 (value (read-string (format "New value (%s): " var))))
4278 (gud-basic-call
4279 (concat "-gdb-set variable $" var " = " value)))))
4281 (defvar gdb-registers-mode-map
4282 (let ((map (make-sparse-keymap)))
4283 (suppress-keymap map)
4284 (define-key map "\r" 'gdb-edit-register-value)
4285 (define-key map [mouse-2] 'gdb-edit-register-value)
4286 (define-key map "q" 'kill-current-buffer)
4287 (define-key map "\t" (lambda ()
4288 (interactive)
4289 (gdb-set-window-buffer
4290 (gdb-get-buffer-create
4291 'gdb-locals-buffer
4292 gdb-thread-number) t)))
4293 map))
4295 (defvar gdb-registers-header
4296 (list
4297 (gdb-propertize-header "Locals" gdb-locals-buffer
4298 "mouse-1: select" mode-line-highlight
4299 mode-line-inactive)
4301 (gdb-propertize-header "Registers" gdb-registers-buffer
4302 nil nil mode-line)))
4304 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4305 "Major mode for gdb registers."
4306 (setq header-line-format gdb-registers-header)
4307 'gdb-invalidate-registers)
4309 (defun gdb-registers-buffer-name ()
4310 (gdb-current-context-buffer-name
4311 (concat "registers of " (gdb-get-target-string))))
4313 (defun gdb-display-registers-buffer (&optional thread)
4314 "Display GDB register contents."
4315 (interactive)
4316 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4318 (def-gdb-preempt-display-buffer
4319 gdb-preemptively-display-registers-buffer
4320 'gdb-registers-buffer nil t)
4322 (defun gdb-frame-registers-buffer (&optional thread)
4323 "Display GDB register contents in another frame."
4324 (interactive)
4325 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4326 gdb-display-buffer-other-frame-action))
4328 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4329 (defun gdb-get-changed-registers ()
4330 (when (gdb-get-buffer 'gdb-registers-buffer)
4331 (gdb-input "-data-list-changed-registers"
4332 'gdb-changed-registers-handler
4333 'gdb-get-changed-registers)))
4335 (defun gdb-changed-registers-handler ()
4336 (setq gdb-changed-registers nil)
4337 (dolist (register-number
4338 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4339 (push register-number gdb-changed-registers)))
4341 (defun gdb-register-names-handler ()
4342 ;; Don't use pending triggers because this handler is called
4343 ;; only once (in gdb-init-1)
4344 (setq gdb-register-names nil)
4345 (dolist (register-name
4346 (bindat-get-field (gdb-json-partial-output) 'register-names))
4347 (push register-name gdb-register-names))
4348 (setq gdb-register-names (reverse gdb-register-names)))
4351 (defun gdb-get-source-file-list ()
4352 "Create list of source files for current GDB session.
4353 If buffers already exist for any of these files, `gud-minor-mode'
4354 is set in them."
4355 (goto-char (point-min))
4356 (while (re-search-forward gdb-source-file-regexp nil t)
4357 (push (read (match-string 1)) gdb-source-file-list))
4358 (dolist (buffer (buffer-list))
4359 (with-current-buffer buffer
4360 (when (member buffer-file-name gdb-source-file-list)
4361 (gdb-init-buffer)))))
4363 (defun gdb-get-main-selected-frame ()
4364 "Trigger for `gdb-frame-handler' which uses main current thread.
4365 Called from `gdb-update'."
4366 (gdb-input (gdb-current-context-command "-stack-info-frame")
4367 'gdb-frame-handler
4368 'gdb-get-main-selected-frame))
4370 (defun gdb-frame-handler ()
4371 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4372 overlay arrow in source buffer."
4373 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4374 (when frame
4375 (setq gdb-selected-frame (bindat-get-field frame 'func))
4376 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4377 (setq gdb-frame-number (bindat-get-field frame 'level))
4378 (setq gdb-frame-address (bindat-get-field frame 'addr))
4379 (let ((line (bindat-get-field frame 'line)))
4380 (setq gdb-selected-line (and line (string-to-number line)))
4381 (when (and gdb-selected-file gdb-selected-line)
4382 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4383 (gud-display-frame)))
4384 (if gud-overlay-arrow-position
4385 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4386 (position (marker-position gud-overlay-arrow-position)))
4387 (when buffer
4388 (with-current-buffer buffer
4389 (setq fringe-indicator-alist
4390 (if (string-equal gdb-frame-number "0")
4392 '((overlay-arrow . hollow-right-triangle))))
4393 (setq gud-overlay-arrow-position (make-marker))
4394 (set-marker gud-overlay-arrow-position position))))))))
4396 (defconst gdb-prompt-name-regexp
4397 (concat "value=\\(" gdb--string-regexp "\\)"))
4399 (defun gdb-get-prompt ()
4400 "Find prompt for GDB session."
4401 (goto-char (point-min))
4402 (setq gdb-prompt-name nil)
4403 (re-search-forward gdb-prompt-name-regexp nil t)
4404 (setq gdb-prompt-name (read (match-string 1)))
4405 ;; Insert first prompt.
4406 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4408 ;;;; Window management
4409 (defun gdb-display-buffer (buf)
4410 "Show buffer BUF, and make that window dedicated."
4411 (let ((window (display-buffer buf)))
4412 (set-window-dedicated-p window t)
4413 window))
4415 ;; (let ((answer (get-buffer-window buf 0)))
4416 ;; (if answer
4417 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4418 ;; (let ((window (get-lru-window)))
4419 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4420 ;; 'gdbmi)
4421 ;; (let ((largest (get-largest-window)))
4422 ;; (setq answer (split-window largest))
4423 ;; (set-window-buffer answer buf)
4424 ;; (set-window-dedicated-p answer t)
4425 ;; answer)
4426 ;; (set-window-buffer window buf)
4427 ;; window)))))
4430 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4431 "Find window displaying a buffer with the same
4432 `gdb-buffer-type' as BUF and show BUF there. If no such window
4433 exists, just call `gdb-display-buffer' for BUF. If the window
4434 found is already dedicated, split window according to
4435 SPLIT-HORIZONTAL and show BUF in the new window."
4436 (if buf
4437 (when (not (get-buffer-window buf))
4438 (let* ((buf-type (gdb-buffer-type buf))
4439 (existing-window
4440 (get-window-with-predicate
4441 #'(lambda (w)
4442 (and (eq buf-type
4443 (gdb-buffer-type (window-buffer w)))
4444 (not (window-dedicated-p w)))))))
4445 (if existing-window
4446 (set-window-buffer existing-window buf)
4447 (let ((dedicated-window
4448 (get-window-with-predicate
4449 #'(lambda (w)
4450 (eq buf-type
4451 (gdb-buffer-type (window-buffer w)))))))
4452 (if dedicated-window
4453 (set-window-buffer
4454 (split-window dedicated-window nil split-horizontal) buf)
4455 (gdb-display-buffer buf))))))
4456 (error "Null buffer")))
4458 ;;; Shared keymap initialization:
4460 (let ((menu (make-sparse-keymap "GDB-Windows")))
4461 (define-key gud-menu-map [displays]
4462 `(menu-item "GDB-Windows" ,menu
4463 :visible (eq gud-minor-mode 'gdbmi)))
4464 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4465 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4466 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4467 (define-key menu [disassembly]
4468 '("Disassembly" . gdb-display-disassembly-buffer))
4469 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4470 (define-key menu [inferior]
4471 '("IO" . gdb-display-io-buffer))
4472 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4473 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4474 (define-key menu [breakpoints]
4475 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4477 (let ((menu (make-sparse-keymap "GDB-Frames")))
4478 (define-key gud-menu-map [frames]
4479 `(menu-item "GDB-Frames" ,menu
4480 :visible (eq gud-minor-mode 'gdbmi)))
4481 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4482 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4483 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4484 (define-key menu [disassembly]
4485 '("Disassembly" . gdb-frame-disassembly-buffer))
4486 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4487 (define-key menu [inferior]
4488 '("IO" . gdb-frame-io-buffer))
4489 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4490 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4491 (define-key menu [breakpoints]
4492 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4494 (let ((menu (make-sparse-keymap "GDB-MI")))
4495 (define-key menu [gdb-customize]
4496 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4497 :help "Customize Gdb Graphical Mode options."))
4498 (define-key menu [gdb-many-windows]
4499 '(menu-item "Display Other Windows" gdb-many-windows
4500 :help "Toggle display of locals, stack and breakpoint information"
4501 :button (:toggle . gdb-many-windows)))
4502 (define-key menu [gdb-restore-windows]
4503 '(menu-item "Restore Window Layout" gdb-restore-windows
4504 :help "Restore standard layout for debug session."))
4505 (define-key menu [sep1]
4506 '(menu-item "--"))
4507 (define-key menu [all-threads]
4508 '(menu-item "GUD controls all threads"
4509 (lambda ()
4510 (interactive)
4511 (setq gdb-gud-control-all-threads t))
4512 :help "GUD start/stop commands apply to all threads"
4513 :button (:radio . gdb-gud-control-all-threads)))
4514 (define-key menu [current-thread]
4515 '(menu-item "GUD controls current thread"
4516 (lambda ()
4517 (interactive)
4518 (setq gdb-gud-control-all-threads nil))
4519 :help "GUD start/stop commands apply to current thread only"
4520 :button (:radio . (not gdb-gud-control-all-threads))))
4521 (define-key menu [sep2]
4522 '(menu-item "--"))
4523 (define-key menu [gdb-customize-reasons]
4524 '(menu-item "Customize switching..."
4525 (lambda ()
4526 (interactive)
4527 (customize-option 'gdb-switch-reasons))))
4528 (define-key menu [gdb-switch-when-another-stopped]
4529 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4530 gdb-switch-when-another-stopped
4531 "Automatically switch to stopped thread"
4532 "GDB thread switching %s"
4533 "Switch to stopped thread"))
4534 (define-key gud-menu-map [mi]
4535 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4537 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4538 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4539 ;; tool-bar-local-item-from-menu here because it appends new buttons
4540 ;; to toolbar from right to left while we want our A/T throttle to
4541 ;; show up right before Run button.
4542 (define-key-after gud-tool-bar-map [all-threads]
4543 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4544 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4545 :visible (and (eq gud-minor-mode 'gdbmi)
4546 gdb-non-stop
4547 (not gdb-gud-control-all-threads)))
4548 'run)
4550 (define-key-after gud-tool-bar-map [current-thread]
4551 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4552 :image (find-image '((:type xpm :file "gud/all.xpm")))
4553 :visible (and (eq gud-minor-mode 'gdbmi)
4554 gdb-non-stop
4555 gdb-gud-control-all-threads))
4556 'all-threads)
4558 (defun gdb-frame-gdb-buffer ()
4559 "Display GUD buffer in another frame."
4560 (interactive)
4561 (display-buffer-other-frame gud-comint-buffer))
4563 (defun gdb-display-gdb-buffer ()
4564 "Display GUD buffer."
4565 (interactive)
4566 (pop-to-buffer gud-comint-buffer nil 0))
4568 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4569 "Set buffer of selected window to NAME and dedicate window.
4571 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4572 window is dedicated."
4573 (unless window (setq window (selected-window)))
4574 (when ignore-dedicated
4575 (set-window-dedicated-p window nil))
4576 (set-window-buffer window (get-buffer name))
4577 (set-window-dedicated-p window t))
4579 (defun gdb-setup-windows ()
4580 "Layout the window pattern for option `gdb-many-windows'."
4581 (gdb-get-buffer-create 'gdb-locals-buffer)
4582 (gdb-get-buffer-create 'gdb-stack-buffer)
4583 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4584 (set-window-dedicated-p (selected-window) nil)
4585 (switch-to-buffer gud-comint-buffer)
4586 (delete-other-windows)
4587 (let ((win0 (selected-window))
4588 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4589 (win2 (split-window nil ( / (window-height) 3)))
4590 (win3 (split-window-right)))
4591 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4592 (select-window win2)
4593 (set-window-buffer
4594 win2
4595 (if gud-last-last-frame
4596 (gud-find-file (car gud-last-last-frame))
4597 (if gdb-main-file
4598 (gud-find-file gdb-main-file)
4599 ;; Put buffer list in window if we
4600 ;; can't find a source file.
4601 (list-buffers-noselect))))
4602 (setq gdb-source-window (selected-window))
4603 (let ((win4 (split-window-right)))
4604 (gdb-set-window-buffer
4605 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4606 (select-window win1)
4607 (gdb-set-window-buffer (gdb-stack-buffer-name))
4608 (let ((win5 (split-window-right)))
4609 (gdb-set-window-buffer (if gdb-show-threads-by-default
4610 (gdb-threads-buffer-name)
4611 (gdb-breakpoints-buffer-name))
4612 nil win5))
4613 (select-window win0)))
4615 (define-minor-mode gdb-many-windows
4616 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4617 In this case it starts with two windows: one displaying the GUD
4618 buffer and the other with the source file with the main routine
4619 of the debugged program. Non-nil means display the layout shown for
4620 `gdb'."
4621 :global t
4622 :group 'gdb
4623 :version "22.1"
4624 (if (and gud-comint-buffer
4625 (buffer-name gud-comint-buffer))
4626 (ignore-errors
4627 (gdb-restore-windows))))
4629 (defun gdb-restore-windows ()
4630 "Restore the basic arrangement of windows used by gdb.
4631 This arrangement depends on the value of option `gdb-many-windows'."
4632 (interactive)
4633 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4634 (delete-other-windows)
4635 (if gdb-many-windows
4636 (gdb-setup-windows)
4637 (when (or gud-last-last-frame gdb-show-main)
4638 (let ((win (split-window)))
4639 (set-window-buffer
4641 (if gud-last-last-frame
4642 (gud-find-file (car gud-last-last-frame))
4643 (gud-find-file gdb-main-file)))
4644 (setq gdb-source-window win)))))
4646 ;; Called from `gud-sentinel' in gud.el:
4647 (defun gdb-reset ()
4648 "Exit a debugging session cleanly.
4649 Kills the gdb buffers, and resets variables and the source buffers."
4650 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4651 ;; process. This pty must be deleted explicitly.
4652 (let ((pty (get-process "gdb-inferior")))
4653 (if pty (delete-process pty)))
4654 ;; Find gdb-mi buffers and kill them.
4655 (dolist (buffer (buffer-list))
4656 (unless (eq buffer gud-comint-buffer)
4657 (with-current-buffer buffer
4658 (if (eq gud-minor-mode 'gdbmi)
4659 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4660 (kill-buffer nil)
4661 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4662 (setq gud-minor-mode nil)
4663 (kill-local-variable 'tool-bar-map)
4664 (kill-local-variable 'gdb-define-alist))))))
4665 (setq gdb-disassembly-position nil)
4666 (setq overlay-arrow-variable-list
4667 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4668 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4669 (setq gdb-stack-position nil)
4670 (setq overlay-arrow-variable-list
4671 (delq 'gdb-stack-position overlay-arrow-variable-list))
4672 (setq gdb-thread-position nil)
4673 (setq overlay-arrow-variable-list
4674 (delq 'gdb-thread-position overlay-arrow-variable-list))
4675 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4676 (setq gud-running nil)
4677 (setq gdb-active-process nil)
4678 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4680 (defun gdb-get-source-file ()
4681 "Find the source file where the program starts and display it with related
4682 buffers, if required."
4683 (goto-char (point-min))
4684 (if (re-search-forward gdb-source-file-regexp nil t)
4685 (setq gdb-main-file (read (match-string 1))))
4686 (if gdb-many-windows
4687 (gdb-setup-windows)
4688 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4689 (and gdb-show-main
4690 gdb-main-file
4691 (display-buffer (gud-find-file gdb-main-file))))
4692 (gdb-force-mode-line-update
4693 (propertize "ready" 'face font-lock-variable-name-face)))
4695 ;;from put-image
4696 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4697 "Put string PUTSTRING in front of POS in the current buffer.
4698 PUTSTRING is displayed by putting an overlay into the current buffer with a
4699 `before-string' string that has a `display' property whose value is
4700 PUTSTRING."
4701 (let ((string (make-string 1 ?x))
4702 (buffer (current-buffer)))
4703 (setq putstring (copy-sequence putstring))
4704 (let ((overlay (make-overlay pos pos buffer))
4705 (prop (or dprop
4706 (list (list 'margin 'left-margin) putstring))))
4707 (put-text-property 0 1 'display prop string)
4708 (if sprops
4709 (add-text-properties 0 1 sprops string))
4710 (overlay-put overlay 'put-break t)
4711 (overlay-put overlay 'before-string string))))
4713 ;;from remove-images
4714 (defun gdb-remove-strings (start end &optional buffer)
4715 "Remove strings between START and END in BUFFER.
4716 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4717 BUFFER nil or omitted means use the current buffer."
4718 (unless buffer
4719 (setq buffer (current-buffer)))
4720 (dolist (overlay (overlays-in start end))
4721 (when (overlay-get overlay 'put-break)
4722 (delete-overlay overlay))))
4724 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4725 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4726 (start (- (car posns) 1))
4727 (end (+ (cdr posns) 1))
4728 (putstring (if enabled "B" "b"))
4729 (source-window (get-buffer-window (current-buffer) 0)))
4730 (add-text-properties
4731 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4732 putstring)
4733 (if enabled
4734 (add-text-properties
4735 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4736 (add-text-properties
4737 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4738 (gdb-remove-breakpoint-icons start end)
4739 (if (display-images-p)
4740 (if (>= (or left-fringe-width
4741 (if source-window (car (window-fringes source-window)))
4742 gdb-buffer-fringe-width) 8)
4743 (gdb-put-string
4744 nil (1+ start)
4745 `(left-fringe breakpoint
4746 ,(if enabled
4747 'breakpoint-enabled
4748 'breakpoint-disabled))
4749 'gdb-bptno bptno
4750 'gdb-enabled enabled)
4751 (when (< left-margin-width 2)
4752 (save-current-buffer
4753 (setq left-margin-width 2)
4754 (if source-window
4755 (set-window-margins
4756 source-window
4757 left-margin-width right-margin-width))))
4758 (put-image
4759 (if enabled
4760 (or breakpoint-enabled-icon
4761 (setq breakpoint-enabled-icon
4762 (find-image `((:type xpm :data
4763 ,breakpoint-xpm-data
4764 :ascent 100 :pointer hand)
4765 (:type pbm :data
4766 ,breakpoint-enabled-pbm-data
4767 :ascent 100 :pointer hand)))))
4768 (or breakpoint-disabled-icon
4769 (setq breakpoint-disabled-icon
4770 (find-image `((:type xpm :data
4771 ,breakpoint-xpm-data
4772 :conversion disabled
4773 :ascent 100 :pointer hand)
4774 (:type pbm :data
4775 ,breakpoint-disabled-pbm-data
4776 :ascent 100 :pointer hand))))))
4777 (+ start 1)
4778 putstring
4779 'left-margin))
4780 (when (< left-margin-width 2)
4781 (save-current-buffer
4782 (setq left-margin-width 2)
4783 (let ((window (get-buffer-window (current-buffer) 0)))
4784 (if window
4785 (set-window-margins
4786 window left-margin-width right-margin-width)))))
4787 (gdb-put-string
4788 (propertize putstring
4789 'face (if enabled
4790 'breakpoint-enabled 'breakpoint-disabled))
4791 (1+ start)))))
4793 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4794 (gdb-remove-strings start end)
4795 (if (display-images-p)
4796 (remove-images start end))
4797 (when remove-margin
4798 (setq left-margin-width 0)
4799 (let ((window (get-buffer-window (current-buffer) 0)))
4800 (if window
4801 (set-window-margins
4802 window left-margin-width right-margin-width)))))
4805 ;;; Functions for inline completion.
4807 (defvar gud-gdb-fetch-lines-in-progress)
4808 (defvar gud-gdb-fetch-lines-string)
4809 (defvar gud-gdb-fetch-lines-break)
4810 (defvar gud-gdb-fetched-lines)
4812 (defun gud-gdbmi-completions (context command)
4813 "Completion table for GDB/MI commands.
4814 COMMAND is the prefix for which we seek completion.
4815 CONTEXT is the text before COMMAND on the line."
4816 (let ((gud-gdb-fetch-lines-in-progress t)
4817 (gud-gdb-fetch-lines-string nil)
4818 (gud-gdb-fetch-lines-break (length context))
4819 (gud-gdb-fetched-lines nil)
4820 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4821 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4822 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4823 (gdb-input (concat "complete " context command)
4824 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4825 (while gud-gdb-fetch-lines-in-progress
4826 (accept-process-output (get-buffer-process gud-comint-buffer))))
4827 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4829 (defun gud-gdbmi-fetch-lines-filter (string)
4830 "Custom filter function for `gud-gdbmi-completions'."
4831 (setq string (concat gud-gdb-fetch-lines-string
4832 (gud-gdbmi-marker-filter string)))
4833 (while (string-match "\n" string)
4834 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4835 gud-gdb-fetched-lines)
4836 (setq string (substring string (match-end 0))))
4839 (provide 'gdb-mi)
4841 ;;; gdb-mi.el ends here