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