Handle remote file names in gdb-mi.el
[emacs.git] / lisp / progmodes / gdb-mi.el
blob8047e208ea57fdb2786d7fa7b421dec9ac7b97d5
1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2016 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: unix, tools
9 ;; This file is part of GNU Emacs.
11 ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 passing it COMMAND-LINE as arguments.
678 If COMMAND-LINE names a program FILE to debug, gdb will run in
679 a buffer named *gud-FILE*, and the directory containing FILE
680 becomes the initial working directory and source-file directory
681 for your debugger.
682 If COMMAND-LINE requests that gdb attaches to a process PID, gdb
683 will run in *gud-PID*, otherwise it will run in *gud*; in these
684 cases the initial working directory is the default-directory of
685 the buffer in which this command was invoked.
687 COMMAND-LINE should include \"-i=mi\" to use gdb's MI text interface.
688 Note that the old \"--annotate\" option is no longer supported.
690 If option `gdb-many-windows' is nil (the default value) then gdb just
691 pops up the GUD buffer unless `gdb-show-main' is t. In this case
692 it starts with two windows: one displaying the GUD buffer and the
693 other with the source file with the main routine of the inferior.
695 If option `gdb-many-windows' is t, regardless of the value of
696 `gdb-show-main', the layout below will appear. Keybindings are
697 shown in some of the buffers.
699 Watch expressions appear in the speedbar/slowbar.
701 The following commands help control operation :
703 `gdb-many-windows' - Toggle the number of windows gdb uses.
704 `gdb-restore-windows' - To restore the window layout.
706 See Info node `(emacs)GDB Graphical Interface' for a more
707 detailed description of this mode.
710 +----------------------------------------------------------------------+
711 | GDB Toolbar |
712 +-----------------------------------+----------------------------------+
713 | GUD buffer (I/O of GDB) | Locals buffer |
714 | | |
715 | | |
716 | | |
717 +-----------------------------------+----------------------------------+
718 | Source buffer | I/O buffer (of debugged program) |
719 | | (comint-mode) |
720 | | |
721 | | |
722 | | |
723 | | |
724 | | |
725 | | |
726 +-----------------------------------+----------------------------------+
727 | Stack buffer | Breakpoints buffer |
728 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
729 | | RET gdb-goto-breakpoint |
730 | | D gdb-delete-breakpoint |
731 +-----------------------------------+----------------------------------+"
733 (interactive (list (gud-query-cmdline 'gdb)))
735 (when (and gud-comint-buffer
736 (buffer-name gud-comint-buffer)
737 (get-buffer-process gud-comint-buffer)
738 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
739 (gdb-restore-windows)
740 (error
741 "Multiple debugging requires restarting in text command mode"))
743 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
745 ;; Setup a temporary process filter to warn when GDB was not started
746 ;; with -i=mi.
747 (let ((proc (get-buffer-process gud-comint-buffer)))
748 (add-function :around (process-filter proc) #'gdb--check-interpreter))
750 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
751 (set (make-local-variable 'gdb-control-level) 0)
752 (setq comint-input-sender 'gdb-send)
753 (when (ring-empty-p comint-input-ring) ; cf shell-mode
754 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
755 (if (eq system-type 'ms-dos)
756 "_gdb_history"
757 ".gdb_history"))))
758 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
759 (hsize (getenv "HISTSIZE")))
760 (dolist (file (append '("~/.gdbinit")
761 (unless (string-equal (expand-file-name ".")
762 (expand-file-name "~"))
763 '(".gdbinit"))))
764 (if (file-readable-p (setq file (expand-file-name file)))
765 (with-temp-buffer
766 (insert-file-contents file)
767 ;; TODO? check for "set history save\\( *on\\)?" and do
768 ;; not use history otherwise?
769 (while (re-search-forward
770 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
771 (cond ((string-equal (match-string 1) "filename")
772 (setq hfile (expand-file-name
773 (match-string 2)
774 (file-name-directory file))))
775 ((string-equal (match-string 1) "size")
776 (setq hsize (match-string 2))))))))
777 (and (stringp hsize)
778 (integerp (setq hsize (string-to-number hsize)))
779 (> hsize 0)
780 (set (make-local-variable 'comint-input-ring-size) hsize))
781 (if (stringp hfile)
782 (set (make-local-variable 'comint-input-ring-file-name) hfile))
783 (comint-read-input-ring t)))
784 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
785 "Set temporary breakpoint at current line.")
786 (gud-def gud-jump
787 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
788 "\C-j" "Set execution address to current line.")
790 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
791 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
792 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
793 (gud-def gud-pstar "print* %e" nil
794 "Evaluate C dereferenced pointer expression at point.")
796 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
797 "\C-s"
798 "Step one source line with display.")
799 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
800 "\C-i"
801 "Step one instruction with display.")
802 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
803 "\C-n"
804 "Step one line (skip functions).")
805 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
807 "Step one instruction (skip functions).")
808 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
809 "\C-r"
810 "Continue with display.")
811 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
812 "\C-f"
813 "Finish executing current function.")
814 (gud-def gud-run "-exec-run"
816 "Run the program.")
818 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
819 (gud-call "break %f:%l" arg)
820 (save-excursion
821 (beginning-of-line)
822 (forward-char 2)
823 (gud-call "break *%a" arg)))
824 "\C-b" "Set breakpoint at current line or address.")
826 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
827 (gud-call "clear %f:%l" arg)
828 (save-excursion
829 (beginning-of-line)
830 (forward-char 2)
831 (gud-call "clear *%a" arg)))
832 "\C-d" "Remove breakpoint at current line or address.")
834 ;; -exec-until doesn't support --all yet
835 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
836 (gud-call "-exec-until %f:%l" arg)
837 (save-excursion
838 (beginning-of-line)
839 (forward-char 2)
840 (gud-call "-exec-until *%a" arg)))
841 "\C-u" "Continue to current line or address.")
842 ;; TODO Why arg here?
843 (gud-def
844 gud-go (gud-call (if gdb-active-process
845 (gdb-gud-context-command "-exec-continue")
846 "-exec-run") arg)
847 nil "Start or continue execution.")
849 ;; For debugging Emacs only.
850 (gud-def gud-pp
851 (gud-call
852 (concat
853 "pp " (if (eq (buffer-local-value
854 'major-mode (window-buffer)) 'speedbar-mode)
855 (gdb-find-watch-expression) "%e")) arg)
856 nil "Print the Emacs s-expression.")
858 (define-key gud-minor-mode-map [left-margin mouse-1]
859 'gdb-mouse-set-clear-breakpoint)
860 (define-key gud-minor-mode-map [left-fringe mouse-1]
861 'gdb-mouse-set-clear-breakpoint)
862 (define-key gud-minor-mode-map [left-margin C-mouse-1]
863 'gdb-mouse-toggle-breakpoint-margin)
864 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
865 'gdb-mouse-toggle-breakpoint-fringe)
867 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
868 'gdb-mouse-until)
869 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
870 'gdb-mouse-until)
871 (define-key gud-minor-mode-map [left-margin mouse-3]
872 'gdb-mouse-until)
873 (define-key gud-minor-mode-map [left-fringe mouse-3]
874 'gdb-mouse-until)
876 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
877 'gdb-mouse-jump)
878 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
879 'gdb-mouse-jump)
880 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
881 'gdb-mouse-jump)
882 (define-key gud-minor-mode-map [left-margin C-mouse-3]
883 'gdb-mouse-jump)
885 (set (make-local-variable 'gud-gdb-completion-function)
886 'gud-gdbmi-completions)
888 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
889 nil 'local)
890 (local-set-key "\C-i" 'completion-at-point)
892 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
894 (setq gdb-first-prompt t)
895 (setq gud-running nil)
897 (gdb-update)
899 (run-hooks 'gdb-mode-hook))
901 (defun gdb-init-1 ()
902 ;; (Re-)initialize.
903 (setq gdb-selected-frame nil
904 gdb-frame-number nil
905 gdb-thread-number nil
906 gdb-var-list nil
907 gdb-output-sink 'user
908 gdb-location-alist nil
909 gdb-source-file-list nil
910 gdb-last-command nil
911 gdb-token-number 0
912 gdb-handler-list '()
913 gdb-prompt-name nil
914 gdb-first-done-or-error t
915 gdb-buffer-fringe-width (car (window-fringes))
916 gdb-debug-log nil
917 gdb-source-window nil
918 gdb-inferior-status nil
919 gdb-continuation nil
920 gdb-buf-publisher '()
921 gdb-threads-list '()
922 gdb-breakpoints-list '()
923 gdb-register-names '()
924 gdb-non-stop gdb-non-stop-setting)
926 (gdbmi-bnf-init)
928 (setq gdb-buffer-type 'gdbmi)
930 (gdb-force-mode-line-update
931 (propertize "initializing..." 'face font-lock-variable-name-face))
933 (gdb-get-buffer-create 'gdb-inferior-io)
934 (gdb-clear-inferior-io)
935 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
937 (when (eq system-type 'windows-nt)
938 ;; Don't create a separate console window for the debuggee.
939 (gdb-input "-gdb-set new-console off" 'ignore)
940 ;; Force GDB to behave as if its input and output stream were
941 ;; connected to a TTY device (since on Windows we use pipes for
942 ;; communicating with GDB).
943 (gdb-input "-gdb-set interactive-mode on" 'ignore))
944 (gdb-input "-gdb-set height 0" 'ignore)
946 (when gdb-non-stop
947 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
949 (gdb-input "-enable-pretty-printing" 'ignore)
951 ;; Find source file and compilation directory here.
952 (if gdb-create-source-file-list
953 ;; Needs GDB 6.2 onwards.
954 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
955 ;; Needs GDB 6.0 onwards.
956 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
957 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
959 (defun gdb-non-stop-handler ()
960 (goto-char (point-min))
961 (if (re-search-forward "No symbol" nil t)
962 (progn
963 (message
964 "This version of GDB doesn't support non-stop mode. Turning it off.")
965 (setq gdb-non-stop nil)
966 (setq gdb-supports-non-stop nil))
967 (setq gdb-supports-non-stop t)
968 (gdb-input "-gdb-set target-async 1" 'ignore)
969 (gdb-input "-list-target-features" 'gdb-check-target-async)))
971 (defun gdb-check-target-async ()
972 (goto-char (point-min))
973 (unless (re-search-forward "async" nil t)
974 (message
975 "Target doesn't support non-stop mode. Turning it off.")
976 (setq gdb-non-stop nil)
977 (gdb-input "-gdb-set non-stop 0" 'ignore)))
979 (defun gdb-delchar-or-quit (arg)
980 "Delete ARG characters or send a quit command to GDB.
981 Send a quit only if point is at the end of the buffer, there is
982 no input, and GDB is waiting for input."
983 (interactive "p")
984 (unless (and (eq (current-buffer) gud-comint-buffer)
985 (eq gud-minor-mode 'gdbmi))
986 (error "Not in a GDB-MI buffer"))
987 (let ((proc (get-buffer-process gud-comint-buffer)))
988 (if (and (eobp)
989 (process-live-p proc)
990 (not gud-running)
991 (= (point) (marker-position (process-mark proc))))
992 ;; Sending an EOF does not work with GDB-MI; submit an
993 ;; explicit quit command.
994 (progn
995 (insert "quit")
996 (comint-send-input t t))
997 (delete-char arg))))
999 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
1001 (defun gdb-create-define-alist ()
1002 "Create an alist of #define directives for GUD tooltips."
1003 (let* ((file (buffer-file-name))
1004 (output
1005 (with-output-to-string
1006 (with-current-buffer standard-output
1007 (and file
1008 (file-exists-p file)
1009 ;; call-process doesn't work with remote file names.
1010 (not (file-remote-p default-directory))
1011 (call-process shell-file-name file
1012 (list t nil) nil "-c"
1013 (concat gdb-cpp-define-alist-program " "
1014 gdb-cpp-define-alist-flags))))))
1015 (define-list (split-string output "\n" t))
1016 (name))
1017 (setq gdb-define-alist nil)
1018 (dolist (define define-list)
1019 (setq name (nth 1 (split-string define "[( ]")))
1020 (push (cons name define) gdb-define-alist))))
1022 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
1024 (defconst gdb--string-regexp "\"\\(?:[^\\\"]\\|\\\\.\\)*\"")
1026 (defun gdb-tooltip-print (expr)
1027 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
1028 (goto-char (point-min))
1029 (cond
1030 ((re-search-forward (concat ".*value=\\(" gdb--string-regexp
1031 "\\)")
1032 nil t)
1033 (tooltip-show
1034 (concat expr " = " (read (match-string 1)))
1035 (or gud-tooltip-echo-area
1036 (not (display-graphic-p)))))
1037 ((re-search-forward "msg=\\(\".+\"\\)$" nil t)
1038 (tooltip-show (read (match-string 1))
1039 (or gud-tooltip-echo-area
1040 (not (display-graphic-p))))))))
1042 ;; If expr is a macro for a function don't print because of possible dangerous
1043 ;; side-effects. Also printing a function within a tooltip generates an
1044 ;; unexpected starting annotation (phase error).
1045 (defun gdb-tooltip-print-1 (expr)
1046 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
1047 (goto-char (point-min))
1048 (if (search-forward "expands to: " nil t)
1049 (unless (looking-at "\\S-+.*(.*).*")
1050 (gdb-input (concat "-data-evaluate-expression \"" expr "\"")
1051 `(lambda () (gdb-tooltip-print ,expr)))))))
1053 (defun gdb-init-buffer ()
1054 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
1055 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1056 (when gud-tooltip-mode
1057 (make-local-variable 'gdb-define-alist)
1058 (gdb-create-define-alist)
1059 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
1061 (defmacro gdb--if-arrow (arrow-position start-posn end-posn &rest body)
1062 (declare (indent 3))
1063 (let ((buffer (make-symbol "buffer")))
1064 `(if ,arrow-position
1065 (let ((,buffer (marker-buffer ,arrow-position)))
1066 (if (equal ,buffer (window-buffer (posn-window ,end-posn)))
1067 (with-current-buffer ,buffer
1068 (when (or (equal ,start-posn ,end-posn)
1069 (equal (posn-point ,start-posn)
1070 (marker-position ,arrow-position)))
1071 ,@body)))))))
1073 (defun gdb-mouse-until (event)
1074 "Continue running until a source line past the current line.
1075 The destination source line can be selected either by clicking
1076 with mouse-3 on the fringe/margin or dragging the arrow
1077 with mouse-1 (default bindings)."
1078 (interactive "e")
1079 (let ((start (event-start event))
1080 (end (event-end event)))
1081 (gdb--if-arrow gud-overlay-arrow-position start end
1082 (let ((line (line-number-at-pos (posn-point end))))
1083 (gud-call (concat "until " (number-to-string line)))))
1084 (gdb--if-arrow gdb-disassembly-position start end
1085 (save-excursion
1086 (goto-char (point-min))
1087 (forward-line (1- (line-number-at-pos (posn-point end))))
1088 (forward-char 2)
1089 (gud-call (concat "until *%a"))))))
1091 (defun gdb-mouse-jump (event)
1092 "Set execution address/line.
1093 The destination source line can be selected either by clicking with C-mouse-3
1094 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1095 Unlike `gdb-mouse-until' the destination address can be before the current
1096 line, and no execution takes place."
1097 (interactive "e")
1098 (let ((start (event-start event))
1099 (end (event-end event)))
1100 (gdb--if-arrow gud-overlay-arrow-position start end
1101 (let ((line (line-number-at-pos (posn-point end))))
1102 (gud-call (concat "tbreak " (number-to-string line)))
1103 (gud-call (concat "jump " (number-to-string line)))))
1104 (gdb--if-arrow gdb-disassembly-position start end
1105 (save-excursion
1106 (goto-char (point-min))
1107 (forward-line (1- (line-number-at-pos (posn-point end))))
1108 (forward-char 2)
1109 (gud-call (concat "tbreak *%a"))
1110 (gud-call (concat "jump *%a"))))))
1112 (defcustom gdb-show-changed-values t
1113 "If non-nil change the face of out of scope variables and changed values.
1114 Out of scope variables are suppressed with `shadow' face.
1115 Changed values are highlighted with the face `font-lock-warning-face'."
1116 :type 'boolean
1117 :group 'gdb
1118 :version "22.1")
1120 (defcustom gdb-max-children 40
1121 "Maximum number of children before expansion requires confirmation."
1122 :type 'integer
1123 :group 'gdb
1124 :version "22.1")
1126 (defcustom gdb-delete-out-of-scope t
1127 "If non-nil delete watch expressions automatically when they go out of scope."
1128 :type 'boolean
1129 :group 'gdb
1130 :version "22.2")
1132 (define-minor-mode gdb-speedbar-auto-raise
1133 "Minor mode to automatically raise the speedbar for watch expressions.
1134 With prefix argument ARG, automatically raise speedbar if ARG is
1135 positive, otherwise don't automatically raise it."
1136 :global t
1137 :group 'gdb
1138 :version "22.1")
1140 (defcustom gdb-use-colon-colon-notation nil
1141 "If non-nil use FUN::VAR format to display variables in the speedbar."
1142 :type 'boolean
1143 :group 'gdb
1144 :version "22.1")
1146 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1147 (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
1149 (declare-function tooltip-identifier-from-point "tooltip" (point))
1151 (defun gud-watch (&optional arg event)
1152 "Watch expression at point.
1153 With arg, enter name of variable to be watched in the minibuffer."
1154 (interactive (list current-prefix-arg last-input-event))
1155 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1156 (if (eq minor-mode 'gdbmi)
1157 (progn
1158 (if event (posn-set-point (event-end event)))
1159 (require 'tooltip)
1160 (save-selected-window
1161 (let ((expr
1162 (if arg
1163 (completing-read "Name of variable: "
1164 'gud-gdb-complete-command)
1165 (if (and transient-mark-mode mark-active)
1166 (buffer-substring (region-beginning) (region-end))
1167 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1168 (tooltip-identifier-from-point (point)))))))
1169 (set-text-properties 0 (length expr) nil expr)
1170 (gdb-input (concat "-var-create - * " expr "")
1171 `(lambda () (gdb-var-create-handler ,expr))))))
1172 (message "gud-watch is a no-op in this mode."))))
1174 (defun gdb-var-create-handler (expr)
1175 (let* ((result (gdb-json-partial-output)))
1176 (if (not (bindat-get-field result 'msg))
1177 (let ((var
1178 (list (bindat-get-field result 'name)
1179 (if (and (string-equal gdb-current-language "c")
1180 gdb-use-colon-colon-notation gdb-selected-frame)
1181 (setq expr (concat gdb-selected-frame "::" expr))
1182 expr)
1183 (bindat-get-field result 'numchild)
1184 (bindat-get-field result 'type)
1185 (bindat-get-field result 'value)
1187 (bindat-get-field result 'has_more)
1188 gdb-frame-address)))
1189 (push var gdb-var-list)
1190 (speedbar 1)
1191 (unless (string-equal
1192 speedbar-initial-expansion-list-name "GUD")
1193 (speedbar-change-initial-expansion-list "GUD")))
1194 (message-box "No symbol \"%s\" in current context." expr))))
1196 (defun gdb-speedbar-update ()
1197 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1198 ;; Dummy command to update speedbar even when idle.
1199 (gdb-input "-environment-pwd"
1200 'gdb-speedbar-timer-fn
1201 'gdb-speedbar-update)))
1203 (defun gdb-speedbar-timer-fn ()
1204 (if gdb-speedbar-auto-raise
1205 (raise-frame speedbar-frame))
1206 (speedbar-timer-fn))
1208 (defun gdb-var-evaluate-expression-handler (varnum changed)
1209 (goto-char (point-min))
1210 (re-search-forward (concat ".*value=\\(" gdb--string-regexp "\\)")
1211 nil t)
1212 (let ((var (assoc varnum gdb-var-list)))
1213 (when var
1214 (if changed (setcar (nthcdr 5 var) 'changed))
1215 (setcar (nthcdr 4 var) (read (match-string 1)))))
1216 (gdb-speedbar-update))
1218 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1219 (defun gdb-var-list-children (varnum)
1220 (gdb-input (concat "-var-update " varnum) 'ignore)
1221 (gdb-input (concat "-var-list-children --all-values " varnum)
1222 `(lambda () (gdb-var-list-children-handler ,varnum))))
1224 (defun gdb-var-list-children-handler (varnum)
1225 (let* ((var-list nil)
1226 (output (bindat-get-field (gdb-json-partial-output "child")))
1227 (children (bindat-get-field output 'children)))
1228 (catch 'child-already-watched
1229 (dolist (var gdb-var-list)
1230 (if (string-equal varnum (car var))
1231 (progn
1232 ;; With dynamic varobjs numchild may have increased.
1233 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1234 (push var var-list)
1235 (dolist (child children)
1236 (let ((varchild (list (bindat-get-field child 'name)
1237 (bindat-get-field child 'exp)
1238 (bindat-get-field child 'numchild)
1239 (bindat-get-field child 'type)
1240 (bindat-get-field child 'value)
1242 (bindat-get-field child 'has_more))))
1243 (if (assoc (car varchild) gdb-var-list)
1244 (throw 'child-already-watched nil))
1245 (push varchild var-list))))
1246 (push var var-list)))
1247 (setq gdb-var-list (nreverse var-list))))
1248 (gdb-speedbar-update))
1250 (defun gdb-var-set-format (format)
1251 "Set the output format for a variable displayed in the speedbar."
1252 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1253 (varnum (car var)))
1254 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1255 (gdb-var-update)))
1257 (defun gdb-var-delete-1 (var varnum)
1258 (gdb-input (concat "-var-delete " varnum) 'ignore)
1259 (setq gdb-var-list (delq var gdb-var-list))
1260 (dolist (varchild gdb-var-list)
1261 (if (string-match (concat (car var) "\\.") (car varchild))
1262 (setq gdb-var-list (delq varchild gdb-var-list)))))
1264 (defun gdb-var-delete ()
1265 "Delete watch expression at point from the speedbar."
1266 (interactive)
1267 (let ((text (speedbar-line-text)))
1268 (string-match "\\(\\S-+\\)" text)
1269 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1270 (varnum (car var)))
1271 (if (string-match "\\." (car var))
1272 (message-box "Can only delete a root expression")
1273 (gdb-var-delete-1 var varnum)))))
1275 (defun gdb-var-delete-children (varnum)
1276 "Delete children of variable object at point from the speedbar."
1277 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1279 (defun gdb-edit-value (_text _token _indent)
1280 "Assign a value to a variable displayed in the speedbar."
1281 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1282 (varnum (car var))
1283 (value (read-string "New value: ")))
1284 (gdb-input (concat "-var-assign " varnum " " value)
1285 `(lambda () (gdb-edit-value-handler ,value)))))
1287 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1289 (defun gdb-edit-value-handler (value)
1290 (goto-char (point-min))
1291 (if (re-search-forward gdb-error-regexp nil t)
1292 (message-box "Invalid number or expression (%s)" value)))
1294 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1295 (defun gdb-var-update ()
1296 (gdb-input "-var-update --all-values *"
1297 'gdb-var-update-handler
1298 'gdb-var-update))
1300 (defun gdb-var-update-handler ()
1301 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1302 (dolist (var gdb-var-list)
1303 (setcar (nthcdr 5 var) nil))
1304 (let ((temp-var-list gdb-var-list))
1305 (dolist (change changelist)
1306 (let* ((varnum (bindat-get-field change 'name))
1307 (var (assoc varnum gdb-var-list))
1308 (new-num (bindat-get-field change 'new_num_children)))
1309 (when var
1310 (let ((scope (bindat-get-field change 'in_scope))
1311 (has-more (bindat-get-field change 'has_more)))
1312 (cond ((string-equal scope "false")
1313 (if gdb-delete-out-of-scope
1314 (gdb-var-delete-1 var varnum)
1315 (setcar (nthcdr 5 var) 'out-of-scope)))
1316 ((string-equal scope "true")
1317 (setcar (nthcdr 6 var) has-more)
1318 (when (and (or (not has-more)
1319 (string-equal has-more "0"))
1320 (not new-num)
1321 (string-equal (nth 2 var) "0"))
1322 (setcar (nthcdr 4 var)
1323 (bindat-get-field change 'value))
1324 (setcar (nthcdr 5 var) 'changed)))
1325 ((string-equal scope "invalid")
1326 (gdb-var-delete-1 var varnum)))))
1327 (let ((var-list nil) var1
1328 (children (bindat-get-field change 'new_children)))
1329 (when new-num
1330 (setq var1 (pop temp-var-list))
1331 (while var1
1332 (if (string-equal varnum (car var1))
1333 (let ((new (string-to-number new-num))
1334 (previous (string-to-number (nth 2 var1))))
1335 (setcar (nthcdr 2 var1) new-num)
1336 (push var1 var-list)
1337 (cond
1338 ((> new previous)
1339 ;; Add new children to list.
1340 (dotimes (_ previous)
1341 (push (pop temp-var-list) var-list))
1342 (dolist (child children)
1343 (let ((varchild
1344 (list (bindat-get-field child 'name)
1345 (bindat-get-field child 'exp)
1346 (bindat-get-field child 'numchild)
1347 (bindat-get-field child 'type)
1348 (bindat-get-field child 'value)
1349 'changed
1350 (bindat-get-field child 'has_more))))
1351 (push varchild var-list))))
1352 ;; Remove deleted children from list.
1353 ((< new previous)
1354 (dotimes (_ new)
1355 (push (pop temp-var-list) var-list))
1356 (dotimes (_ (- previous new))
1357 (pop temp-var-list)))))
1358 (push var1 var-list))
1359 (setq var1 (pop temp-var-list)))
1360 (setq gdb-var-list (nreverse var-list))))))))
1361 (gdb-speedbar-update))
1363 (defun gdb-speedbar-expand-node (text token indent)
1364 "Expand the node the user clicked on.
1365 TEXT is the text of the button we clicked on, a + or - item.
1366 TOKEN is data related to this node.
1367 INDENT is the current indentation depth."
1368 (cond ((string-match "+" text) ;expand this node
1369 (let* ((var (assoc token gdb-var-list))
1370 (expr (nth 1 var)) (children (nth 2 var)))
1371 (if (or (<= (string-to-number children) gdb-max-children)
1372 (y-or-n-p
1373 (format "%s has %s children. Continue? " expr children)))
1374 (gdb-var-list-children token))))
1375 ((string-match "-" text) ;contract this node
1376 (dolist (var gdb-var-list)
1377 (if (string-match (concat token "\\.") (car var))
1378 (setq gdb-var-list (delq var gdb-var-list))))
1379 (gdb-var-delete-children token)
1380 (speedbar-change-expand-button-char ?+)
1381 (speedbar-delete-subblock indent))
1382 (t (error "Ooops... not sure what to do")))
1383 (speedbar-center-buffer-smartly))
1385 (defun gdb-get-target-string ()
1386 (with-current-buffer gud-comint-buffer
1387 gud-target-name))
1391 ;; gdb buffers.
1393 ;; Each buffer has a TYPE -- a symbol that identifies the function
1394 ;; of that particular buffer.
1396 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1397 ;; is constructed specially.
1399 ;; Others are constructed by gdb-get-buffer-create and
1400 ;; named according to the rules set forth in the gdb-buffer-rules
1402 (defvar gdb-buffer-rules '())
1404 (defun gdb-rules-name-maker (rules-entry)
1405 (cadr rules-entry))
1406 (defun gdb-rules-buffer-mode (rules-entry)
1407 (nth 2 rules-entry))
1408 (defun gdb-rules-update-trigger (rules-entry)
1409 (nth 3 rules-entry))
1411 (defun gdb-update-buffer-name ()
1412 "Rename current buffer according to name-maker associated with
1413 it in `gdb-buffer-rules'."
1414 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1415 gdb-buffer-rules))))
1416 (when f (rename-buffer (funcall f)))))
1418 (defun gdb-current-buffer-rules ()
1419 "Get `gdb-buffer-rules' entry for current buffer type."
1420 (assoc gdb-buffer-type gdb-buffer-rules))
1422 (defun gdb-current-buffer-thread ()
1423 "Get thread object of current buffer from `gdb-threads-list'.
1425 When current buffer is not bound to any thread, return main
1426 thread."
1427 (cdr (assoc gdb-thread-number gdb-threads-list)))
1429 (defun gdb-current-buffer-frame ()
1430 "Get current stack frame object for thread of current buffer."
1431 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1433 (defun gdb-buffer-type (buffer)
1434 "Get value of `gdb-buffer-type' for BUFFER."
1435 (with-current-buffer buffer
1436 gdb-buffer-type))
1438 (defun gdb-buffer-shows-main-thread-p ()
1439 "Return t if current GDB buffer shows main selected thread and
1440 is not bound to it."
1441 (current-buffer)
1442 (not (local-variable-p 'gdb-thread-number)))
1444 (defun gdb-get-buffer (buffer-type &optional thread)
1445 "Get a specific GDB buffer.
1447 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1448 and `gdb-thread-number' (if provided) must be equal to THREAD."
1449 (catch 'found
1450 (dolist (buffer (buffer-list) nil)
1451 (with-current-buffer buffer
1452 (when (and (eq gdb-buffer-type buffer-type)
1453 (or (not thread)
1454 (equal gdb-thread-number thread)))
1455 (throw 'found buffer))))))
1457 (defun gdb-get-buffer-create (buffer-type &optional thread)
1458 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1459 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1461 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1462 buffer-local variable of the new buffer.
1464 Buffer mode and name are selected according to buffer type.
1466 If buffer has trigger associated with it in `gdb-buffer-rules',
1467 this trigger is subscribed to `gdb-buf-publisher' and called with
1468 'update argument."
1469 (or (gdb-get-buffer buffer-type thread)
1470 (let ((rules (assoc buffer-type gdb-buffer-rules))
1471 (new (generate-new-buffer "limbo")))
1472 (with-current-buffer new
1473 (let ((mode (gdb-rules-buffer-mode rules))
1474 (trigger (gdb-rules-update-trigger rules)))
1475 (when mode (funcall mode))
1476 (setq gdb-buffer-type buffer-type)
1477 (when thread
1478 (set (make-local-variable 'gdb-thread-number) thread))
1479 (set (make-local-variable 'gud-minor-mode)
1480 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1481 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1482 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1483 (when trigger
1484 (gdb-add-subscriber gdb-buf-publisher
1485 (cons (current-buffer)
1486 (gdb-bind-function-to-buffer
1487 trigger (current-buffer))))
1488 (funcall trigger 'start))
1489 (current-buffer))))))
1491 (defun gdb-bind-function-to-buffer (expr buffer)
1492 "Return a function which will evaluate EXPR in BUFFER."
1493 `(lambda (&rest args)
1494 (with-current-buffer ,buffer
1495 (apply ',expr args))))
1497 ;; Used to display windows with thread-bound buffers
1498 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1499 split-horizontal)
1500 `(defun ,name (&optional thread)
1501 ,(when doc doc)
1502 (message "%s" thread)
1503 (gdb-preempt-existing-or-display-buffer
1504 (gdb-get-buffer-create ,buffer thread)
1505 ,split-horizontal)))
1507 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1508 ;; at least one and possible more functions. The functions have these
1509 ;; roles in defining a buffer type:
1511 ;; NAME - Return a name for this buffer type.
1513 ;; The remaining function(s) are optional:
1515 ;; MODE - called in a new buffer with no arguments, should establish
1516 ;; the proper mode for the buffer.
1519 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1520 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1521 (if binding
1522 (setcdr binding rules)
1523 (push (cons buffer-type rules)
1524 gdb-buffer-rules))))
1526 (defun gdb-parent-mode ()
1527 "Generic mode to derive all other GDB buffer modes from."
1528 (kill-all-local-variables)
1529 (setq buffer-read-only t)
1530 (buffer-disable-undo)
1531 ;; Delete buffer from gdb-buf-publisher when it's killed
1532 ;; (if it has an associated update trigger)
1533 (add-hook
1534 'kill-buffer-hook
1535 (function
1536 (lambda ()
1537 (let ((trigger (gdb-rules-update-trigger
1538 (gdb-current-buffer-rules))))
1539 (when trigger
1540 (gdb-delete-subscriber
1541 gdb-buf-publisher
1542 ;; This should match gdb-add-subscriber done in
1543 ;; gdb-get-buffer-create
1544 (cons (current-buffer)
1545 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1546 nil t))
1548 ;; Partial-output buffer : This accumulates output from a command executed on
1549 ;; behalf of emacs (rather than the user).
1551 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1552 'gdb-partial-output-name)
1554 (defun gdb-partial-output-name ()
1555 (concat " *partial-output-"
1556 (gdb-get-target-string)
1557 "*"))
1560 (gdb-set-buffer-rules 'gdb-inferior-io
1561 'gdb-inferior-io-name
1562 'gdb-inferior-io-mode)
1564 (defun gdb-inferior-io-name ()
1565 (concat "*input/output of "
1566 (gdb-get-target-string)
1567 "*"))
1569 (defun gdb-display-io-buffer ()
1570 "Display IO of debugged program in a separate window."
1571 (interactive)
1572 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
1574 (defun gdb-inferior-io--init-proc (proc)
1575 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1576 (set-process-filter proc 'gdb-inferior-filter)
1577 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1578 ;; The process can run on a remote host.
1579 (let ((tty (or (process-get proc 'remote-tty)
1580 (process-tty-name proc))))
1581 (unless (or (null tty)
1582 (string= tty ""))
1583 (gdb-input
1584 (concat "-inferior-tty-set " tty) 'ignore))))
1586 (defun gdb-inferior-io-sentinel (proc _str)
1587 (when (eq (process-status proc) 'failed)
1588 ;; When the debugged process exits, Emacs gets an EIO error on
1589 ;; read from the pty, and stops listening to it. If the gdb
1590 ;; process is still running, remove the pty, make a new one, and
1591 ;; pass it to gdb.
1592 (let ((io-buffer (process-buffer proc)))
1593 (when (and (process-live-p (get-buffer-process gud-comint-buffer))
1594 (buffer-live-p io-buffer))
1595 ;; `comint-exec' deletes the original process as a side effect.
1596 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1597 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1599 (defcustom gdb-display-buffer-other-frame-action
1600 '((display-buffer-reuse-window display-buffer-pop-up-frame)
1601 (reusable-frames . visible)
1602 (inhibit-same-window . t)
1603 (pop-up-frame-parameters (height . 14)
1604 (width . 80)
1605 (unsplittable . t)
1606 (tool-bar-lines . nil)
1607 (menu-bar-lines . nil)
1608 (minibuffer . nil)))
1609 "`display-buffer' action for displaying GDB utility frames."
1610 :group 'gdb
1611 :type display-buffer--action-custom-type
1612 :risky t
1613 :version "24.3")
1615 (defun gdb-frame-io-buffer ()
1616 "Display IO of debugged program in another frame."
1617 (interactive)
1618 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1619 gdb-display-buffer-other-frame-action))
1621 (defvar gdb-inferior-io-mode-map
1622 (let ((map (make-sparse-keymap)))
1623 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1624 (define-key map "\C-c\C-z" 'gdb-io-stop)
1625 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1626 (define-key map "\C-c\C-d" 'gdb-io-eof)
1627 (define-key map "\C-d" 'gdb-io-eof)
1628 map))
1630 ;; We want to use comint because it has various nifty and familiar features.
1631 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1632 "Major mode for gdb inferior-io."
1633 :syntax-table nil :abbrev-table nil
1634 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1636 (defcustom gdb-display-io-nopopup nil
1637 "When non-nil, and the `gdb-inferior-io' buffer is buried, don't pop it up."
1638 :type 'boolean
1639 :group 'gdb
1640 :version "25.1")
1642 (defun gdb-inferior-filter (proc string)
1643 (unless (string-equal string "")
1644 (let (buf)
1645 (unless (and gdb-display-io-nopopup
1646 (setq buf (gdb-get-buffer 'gdb-inferior-io))
1647 (null (get-buffer-window buf)))
1648 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1649 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1650 (comint-output-filter proc string)))
1652 (defun gdb-io-interrupt ()
1653 "Interrupt the program being debugged."
1654 (interactive)
1655 (interrupt-process
1656 (get-buffer-process gud-comint-buffer) comint-ptyp))
1658 (defun gdb-io-quit ()
1659 "Send quit signal to the program being debugged."
1660 (interactive)
1661 (quit-process
1662 (get-buffer-process gud-comint-buffer) comint-ptyp))
1664 (defun gdb-io-stop ()
1665 "Stop the program being debugged."
1666 (interactive)
1667 (stop-process
1668 (get-buffer-process gud-comint-buffer) comint-ptyp))
1670 (defun gdb-io-eof ()
1671 "Send end-of-file to the program being debugged."
1672 (interactive)
1673 (process-send-eof
1674 (get-buffer-process gud-comint-buffer)))
1676 (defun gdb-clear-inferior-io ()
1677 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1678 (erase-buffer)))
1681 (defconst breakpoint-xpm-data
1682 "/* XPM */
1683 static char *magick[] = {
1684 /* columns rows colors chars-per-pixel */
1685 \"10 10 2 1\",
1686 \" c red\",
1687 \"+ c None\",
1688 /* pixels */
1689 \"+++ +++\",
1690 \"++ ++\",
1691 \"+ +\",
1692 \" \",
1693 \" \",
1694 \" \",
1695 \" \",
1696 \"+ +\",
1697 \"++ ++\",
1698 \"+++ +++\",
1700 "XPM data used for breakpoint icon.")
1702 (defconst breakpoint-enabled-pbm-data
1704 10 10\",
1705 0 0 0 0 1 1 1 1 0 0 0 0
1706 0 0 0 1 1 1 1 1 1 0 0 0
1707 0 0 1 1 1 1 1 1 1 1 0 0
1708 0 1 1 1 1 1 1 1 1 1 1 0
1709 0 1 1 1 1 1 1 1 1 1 1 0
1710 0 1 1 1 1 1 1 1 1 1 1 0
1711 0 1 1 1 1 1 1 1 1 1 1 0
1712 0 0 1 1 1 1 1 1 1 1 0 0
1713 0 0 0 1 1 1 1 1 1 0 0 0
1714 0 0 0 0 1 1 1 1 0 0 0 0"
1715 "PBM data used for enabled breakpoint icon.")
1717 (defconst breakpoint-disabled-pbm-data
1719 10 10\",
1720 0 0 1 0 1 0 1 0 0 0
1721 0 1 0 1 0 1 0 1 0 0
1722 1 0 1 0 1 0 1 0 1 0
1723 0 1 0 1 0 1 0 1 0 1
1724 1 0 1 0 1 0 1 0 1 0
1725 0 1 0 1 0 1 0 1 0 1
1726 1 0 1 0 1 0 1 0 1 0
1727 0 1 0 1 0 1 0 1 0 1
1728 0 0 1 0 1 0 1 0 1 0
1729 0 0 0 1 0 1 0 1 0 0"
1730 "PBM data used for disabled breakpoint icon.")
1732 (defvar breakpoint-enabled-icon nil
1733 "Icon for enabled breakpoint in display margin.")
1735 (defvar breakpoint-disabled-icon nil
1736 "Icon for disabled breakpoint in display margin.")
1738 (declare-function define-fringe-bitmap "fringe.c"
1739 (bitmap bits &optional height width align))
1741 (and (display-images-p)
1742 ;; Bitmap for breakpoint in fringe
1743 (define-fringe-bitmap 'breakpoint
1744 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1745 ;; Bitmap for gud-overlay-arrow in fringe
1746 (define-fringe-bitmap 'hollow-right-triangle
1747 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1749 (defface breakpoint-enabled
1750 '((t
1751 :foreground "red1"
1752 :weight bold))
1753 "Face for enabled breakpoint icon in fringe."
1754 :group 'gdb)
1756 (defface breakpoint-disabled
1757 '((((class color) (min-colors 88)) :foreground "grey70")
1758 ;; Ensure that on low-color displays that we end up something visible.
1759 (((class color) (min-colors 8) (background light))
1760 :foreground "black")
1761 (((class color) (min-colors 8) (background dark))
1762 :foreground "white")
1763 (((type tty) (class mono))
1764 :inverse-video t)
1765 (t :background "gray"))
1766 "Face for disabled breakpoint icon in fringe."
1767 :group 'gdb)
1770 (defvar gdb-control-commands-regexp
1771 (concat
1772 "^\\("
1773 "commands\\|if\\|while\\|define\\|document\\|"
1774 "python\\|python-interactive\\|pi\\|guile\\|guile-repl\\|gr\\|"
1775 "while-stepping\\|stepping\\|ws\\|actions"
1776 "\\)\\([[:blank:]]+.*\\)?$")
1777 "Regexp matching GDB commands that enter a recursive reading loop.
1778 As long as GDB is in the recursive reading loop, it does not expect
1779 commands to be prefixed by \"-interpreter-exec console\".")
1781 (defun gdb-strip-string-backslash (string)
1782 (replace-regexp-in-string "\\\\$" "" string))
1784 (defun gdb-send (proc string)
1785 "A comint send filter for gdb."
1786 (with-current-buffer gud-comint-buffer
1787 (let ((inhibit-read-only t))
1788 (remove-text-properties (point-min) (point-max) '(face))))
1789 ;; mimic <RET> key to repeat previous command in GDB
1790 (when (= gdb-control-level 0)
1791 (if (not (string= "" string))
1792 (if gdb-continuation
1793 (setq gdb-last-command (concat gdb-continuation
1794 (gdb-strip-string-backslash string)
1795 " "))
1796 (setq gdb-last-command (gdb-strip-string-backslash string)))
1797 (if gdb-last-command (setq string gdb-last-command))
1798 (setq gdb-continuation nil)))
1799 (if (and (not gdb-continuation)
1800 (or (string-match "^-" string)
1801 (> gdb-control-level 0)))
1802 ;; Either MI command or we are feeding GDB's recursive reading loop.
1803 (progn
1804 (setq gdb-first-done-or-error t)
1805 (process-send-string proc (concat string "\n"))
1806 (if (and (string-match
1807 (concat "^\\("
1808 (if (eq system-type 'windows-nt) "\026" "\004")
1809 "\\|,q\\|,quit\\|end\\)$")
1810 string)
1811 (> gdb-control-level 0))
1812 (setq gdb-control-level (1- gdb-control-level))))
1813 ;; CLI command
1814 (if (string-match "\\\\$" string)
1815 (setq gdb-continuation
1816 (concat gdb-continuation (gdb-strip-string-backslash
1817 string)
1818 " "))
1819 (setq gdb-first-done-or-error t)
1820 (let ((to-send (concat "-interpreter-exec console "
1821 (gdb-mi-quote (concat gdb-continuation string " "))
1822 "\n")))
1823 (if gdb-enable-debug
1824 (push (cons 'mi-send to-send) gdb-debug-log))
1825 (process-send-string proc to-send))
1826 (if (and (string-match
1827 (concat "^\\("
1828 (if (eq system-type 'windows-nt) "\026" "\004")
1829 "\\|,q\\|,quit\\|end\\)$")
1830 string)
1831 (> gdb-control-level 0))
1832 (setq gdb-control-level (1- gdb-control-level)))
1833 (setq gdb-continuation nil)))
1834 (if (string-match gdb-control-commands-regexp string)
1835 (setq gdb-control-level (1+ gdb-control-level))))
1837 (defun gdb-mi-quote (string)
1838 "Return STRING quoted properly as an MI argument.
1839 The string is enclosed in double quotes.
1840 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1841 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1842 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1843 (concat "\"" string "\""))
1845 (defun gdb-input (command handler-function &optional trigger-name)
1846 "Send COMMAND to GDB via the MI interface.
1847 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1848 complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
1849 Emacs is still waiting for a reply from another command previously
1850 sent with the same TRIGGER-NAME."
1851 (when (or (not trigger-name)
1852 (not (gdb-pending-handler-p trigger-name)))
1853 (setq gdb-token-number (1+ gdb-token-number))
1854 (setq command (concat (number-to-string gdb-token-number) command))
1856 (if gdb-enable-debug (push (list 'send-item command handler-function)
1857 gdb-debug-log))
1859 (gdb-add-handler gdb-token-number handler-function trigger-name)
1861 (if gdbmi-debug-mode (message "gdb-input: %s" command))
1862 (process-send-string (get-buffer-process gud-comint-buffer)
1863 (concat command "\n"))))
1865 ;; NOFRAME is used for gud execution control commands
1866 (defun gdb-current-context-command (command)
1867 "Add --thread to gdb COMMAND when needed."
1868 (if (and gdb-thread-number
1869 gdb-supports-non-stop)
1870 (concat command " --thread " gdb-thread-number)
1871 command))
1873 (defun gdb-current-context-buffer-name (name)
1874 "Add thread information and asterisks to string NAME.
1876 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1877 (concat "*" name
1878 (if (local-variable-p 'gdb-thread-number)
1879 (format " (bound to thread %s)" gdb-thread-number)
1881 "*"))
1883 (defun gdb-current-context-mode-name (mode)
1884 "Add thread information to MODE which is to be used as `mode-name'."
1885 (concat mode
1886 (if gdb-thread-number
1887 (format " [thread %s]" gdb-thread-number)
1888 "")))
1891 (defcustom gud-gdb-command-name "gdb -i=mi"
1892 "Default command to execute an executable under the GDB debugger."
1893 :type 'string
1894 :group 'gdb)
1896 (defun gdb-resync()
1897 (setq gud-running nil)
1898 (setq gdb-output-sink 'user)
1899 (gdb-remove-all-pending-triggers))
1901 (defun gdb-update (&optional no-proc)
1902 "Update buffers showing status of debug session.
1903 If NO-PROC is non-nil, do not try to contact the GDB process."
1904 (when gdb-first-prompt
1905 (gdb-force-mode-line-update
1906 (propertize "initializing..." 'face font-lock-variable-name-face))
1907 (gdb-init-1)
1908 (setq gdb-first-prompt nil))
1910 (unless no-proc
1911 (gdb-get-main-selected-frame))
1913 ;; We may need to update gdb-threads-list so we can use
1914 (gdb-get-buffer-create 'gdb-threads-buffer)
1915 ;; gdb-break-list is maintained in breakpoints handler
1916 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1918 (unless no-proc
1919 (gdb-emit-signal gdb-buf-publisher 'update))
1921 (gdb-get-changed-registers)
1922 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1923 (dolist (var gdb-var-list)
1924 (setcar (nthcdr 5 var) nil))
1925 (gdb-var-update)))
1927 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1928 ;; because we may need to update current gud-running value without
1929 ;; changing current thread (see gdb-running)
1930 (defun gdb-setq-thread-number (number)
1931 "Set `gdb-thread-number' to NUMBER.
1932 Only this function must be used to change `gdb-thread-number'
1933 value to NUMBER, because `gud-running' and `gdb-frame-number'
1934 need to be updated appropriately when current thread changes."
1935 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1936 (unless (string-equal number "0") (setq gdb-thread-number number))
1937 (setq gdb-frame-number "0")
1938 (gdb-update-gud-running))
1940 (defun gdb-update-gud-running ()
1941 "Set `gud-running' according to the state of current thread.
1943 `gdb-frame-number' is set to 0 if current thread is now stopped.
1945 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1946 cannot be reliably used to determine whether or not execution
1947 control buttons should be shown in menu or toolbar. Use
1948 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1949 instead.
1951 For all-stop mode, thread information is unavailable while target
1952 is running."
1953 (let ((old-value gud-running))
1954 (setq gud-running
1955 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1956 "running"))
1957 ;; Set frame number to "0" when _current_ threads stops.
1958 (when (and (gdb-current-buffer-thread)
1959 (not (eq gud-running old-value)))
1960 (setq gdb-frame-number "0"))))
1962 (defun gdb-show-run-p ()
1963 "Return t if \"Run/continue\" should be shown on the toolbar."
1964 (or (not gdb-active-process)
1965 (and (or
1966 (not gdb-gud-control-all-threads)
1967 (not gdb-non-stop))
1968 (not gud-running))
1969 (and gdb-gud-control-all-threads
1970 (> gdb-stopped-threads-count 0))))
1972 (defun gdb-show-stop-p ()
1973 "Return t if \"Stop\" should be shown on the toolbar."
1974 (or (and (or
1975 (not gdb-gud-control-all-threads)
1976 (not gdb-non-stop))
1977 gud-running)
1978 (and gdb-gud-control-all-threads
1979 (> gdb-running-threads-count 0))))
1981 ;; GUD displays the selected GDB frame. This might might not be the current
1982 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1983 ;; visited breakpoint is, use that window.
1984 (defun gdb-display-source-buffer (buffer)
1985 (let* ((last-window (if gud-last-last-frame
1986 (get-buffer-window
1987 (gud-find-file (car gud-last-last-frame)))))
1988 (source-window (or last-window
1989 (if (and gdb-source-window
1990 (window-live-p gdb-source-window))
1991 gdb-source-window))))
1992 (when source-window
1993 (setq gdb-source-window source-window)
1994 (set-window-buffer source-window buffer))
1995 source-window))
1998 (defun gdbmi-start-with (str offset match)
1999 "Return non-nil if string STR starts with MATCH, else returns nil.
2000 OFFSET is the position in STR at which the comparison takes place."
2001 (let ((match-length (length match))
2002 (str-length (- (length str) offset)))
2003 (when (>= str-length match-length)
2004 (string-equal match (substring str offset (+ offset match-length))))))
2006 (defun gdbmi-same-start (str offset match)
2007 "Return non-nil if STR and MATCH are equal up to the end of either strings.
2008 OFFSET is the position in STR at which the comparison takes place."
2009 (let* ((str-length (- (length str) offset))
2010 (match-length (length match))
2011 (compare-length (min str-length match-length)))
2012 (when (> compare-length 0)
2013 (string-equal (substring str offset (+ offset compare-length))
2014 (substring match 0 compare-length)))))
2016 (defun gdbmi-is-number (character)
2017 "Return non-nil if CHARACTER is a numerical character between 0 and 9."
2018 (and (>= character ?0)
2019 (<= character ?9)))
2022 (defvar-local gdbmi-bnf-state 'gdbmi-bnf-output
2023 "Current GDB/MI output parser state.
2024 The parser is placed in a different state when an incomplete data steam is
2025 received from GDB.
2026 This variable will preserve the state required to resume the parsing
2027 when more data arrives.")
2029 (defvar-local gdbmi-bnf-offset 0
2030 "Offset in `gud-marker-acc' at which the parser is reading.
2031 This offset is used to be able to parse the GDB/MI message
2032 in-place, without the need of copying the string in a temporary buffer
2033 or discarding parsed tokens by substringing the message.")
2035 (defun gdbmi-bnf-init ()
2036 "Initialize the GDB/MI message parser."
2037 (setq gdbmi-bnf-state 'gdbmi-bnf-output)
2038 (setq gdbmi-bnf-offset 0)
2039 (setq gud-marker-acc ""))
2042 (defun gdbmi-bnf-output ()
2043 "Implementation of the following GDB/MI output grammar rule:
2045 output ==>
2046 ( out-of-band-record )* [ result-record ] gdb-prompt"
2048 (gdbmi-bnf-skip-unrecognized)
2049 (while (gdbmi-bnf-out-of-band-record))
2050 (gdbmi-bnf-result-record)
2051 (gdbmi-bnf-gdb-prompt))
2054 (defun gdbmi-bnf-skip-unrecognized ()
2055 "Skip characters until is encounters the beginning of a valid record.
2056 Used as a protection mechanism in case something goes wrong when parsing
2057 a GDB/MI reply message."
2058 (let ((acc-length (length gud-marker-acc))
2059 (prefix-offset gdbmi-bnf-offset)
2060 (prompt "(gdb) \n"))
2062 (while (and (< prefix-offset acc-length)
2063 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
2064 (setq prefix-offset (1+ prefix-offset)))
2066 (if (and (< prefix-offset acc-length)
2067 (not (memq (aref gud-marker-acc prefix-offset)
2068 '(?^ ?* ?+ ?= ?~ ?@ ?&)))
2069 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt))
2070 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
2071 gdbmi-bnf-offset))
2072 (let ((unrecognized-str (match-string 0 gud-marker-acc)))
2073 (setq gdbmi-bnf-offset (match-end 0))
2074 (if gdbmi-debug-mode
2075 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str))
2076 (gdb-shell unrecognized-str)
2077 t))))
2080 (defun gdbmi-bnf-gdb-prompt ()
2081 "Implementation of the following GDB/MI output grammar rule:
2082 gdb-prompt ==>
2083 `(gdb)' nl
2085 nl ==>
2086 CR | CR-LF"
2088 (let ((prompt "(gdb) \n"))
2089 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt)
2090 (if gdbmi-debug-mode (message "gdbmi-bnf-gdb-prompt: %s" prompt))
2091 (gdb-gdb prompt)
2092 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length prompt)))
2094 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
2095 ;; the end of a GDB reply message.
2096 t)))
2099 (defun gdbmi-bnf-result-record ()
2100 "Implementation of the following GDB/MI output grammar rule:
2102 result-record ==>
2103 [ token ] `^' result-class ( `,' result )* nl
2105 token ==>
2106 any sequence of digits."
2108 (gdbmi-bnf-result-and-async-record-impl))
2111 (defun gdbmi-bnf-out-of-band-record ()
2112 "Implementation of the following GDB/MI output grammar rule:
2114 out-of-band-record ==>
2115 async-record | stream-record"
2117 (or (gdbmi-bnf-async-record)
2118 (gdbmi-bnf-stream-record)))
2121 (defun gdbmi-bnf-async-record ()
2122 "Implementation of the following GDB/MI output grammar rules:
2124 async-record ==>
2125 exec-async-output | status-async-output | notify-async-output
2127 exec-async-output ==>
2128 [ token ] `*' async-output
2130 status-async-output ==>
2131 [ token ] `+' async-output
2133 notify-async-output ==>
2134 [ token ] `=' async-output
2136 async-output ==>
2137 async-class ( `,' result )* nl"
2139 (gdbmi-bnf-result-and-async-record-impl))
2142 (defun gdbmi-bnf-stream-record ()
2143 "Implement the following GDB/MI output grammar rule:
2144 stream-record ==>
2145 console-stream-output | target-stream-output | log-stream-output
2147 console-stream-output ==>
2148 `~' c-string
2150 target-stream-output ==>
2151 `@' c-string
2153 log-stream-output ==>
2154 `&' c-string"
2155 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2156 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset) '(?~ ?@ ?&))
2157 (string-match (concat "\\([~@&]\\)\\(" gdb--string-regexp "\\)\n")
2158 gud-marker-acc
2159 gdbmi-bnf-offset))
2160 (let ((prefix (match-string 1 gud-marker-acc))
2161 (c-string (match-string 2 gud-marker-acc)))
2163 (setq gdbmi-bnf-offset (match-end 0))
2164 (if gdbmi-debug-mode (message "gdbmi-bnf-stream-record: %s"
2165 (match-string 0 gud-marker-acc)))
2167 (cond ((string-equal prefix "~")
2168 (gdbmi-bnf-console-stream-output c-string))
2169 ((string-equal prefix "@")
2170 (gdbmi-bnf-target-stream-output c-string))
2171 ((string-equal prefix "&")
2172 (gdbmi-bnf-log-stream-output c-string)))
2173 t))))
2175 (defun gdbmi-bnf-console-stream-output (c-string)
2176 "Handler for the console-stream-output GDB/MI output grammar rule."
2177 (gdb-console c-string))
2179 (defun gdbmi-bnf-target-stream-output (_c-string)
2180 "Handler for the target-stream-output GDB/MI output grammar rule."
2181 ;; Not currently used.
2184 (defun gdbmi-bnf-log-stream-output (c-string)
2185 "Handler for the log-stream-output GDB/MI output grammar rule."
2186 ;; Suppress "No registers." GDB 6.8 and earlier
2187 ;; duplicates MI error message on internal stream.
2188 ;; Don't print to GUD buffer.
2189 (if (not (string-equal (read c-string) "No registers.\n"))
2190 (gdb-internals c-string)))
2193 (defconst gdbmi-bnf-result-state-configs
2194 '(("^" . (("done" . (gdb-done . progressive))
2195 ("error" . (gdb-error . progressive))
2196 ("running" . (gdb-starting . atomic))))
2197 ("*" . (("stopped" . (gdb-stopped . atomic))
2198 ("running" . (gdb-running . atomic))))
2199 ("+" . ())
2200 ("=" . (("thread-created" . (gdb-thread-created . atomic))
2201 ("thread-selected" . (gdb-thread-selected . atomic))
2202 ("thread-existed" . (gdb-ignored-notification . atomic))
2203 ('default . (gdb-ignored-notification . atomic)))))
2204 "Alist of alists, mapping the type and class of message to a handler function.
2205 Handler functions are all flagged as either `progressive' or `atomic'.
2206 `progressive' handlers are capable of parsing incomplete messages.
2207 They can be called several time with new data chunk as they arrive from GDB.
2208 `progressive' handlers must have an extra argument that is set to a non-nil
2209 value when the message is complete.
2211 Implement the following GDB/MI output grammar rule:
2212 result-class ==>
2213 `done' | `running' | `connected' | `error' | `exit'
2215 async-class ==>
2216 `stopped' | others (where others will be added depending on the needs
2217 --this is still in development).")
2219 (defun gdbmi-bnf-result-and-async-record-impl ()
2220 "Common implementation of the result-record and async-record rule.
2221 Both rules share the same syntax. Those records may be very large in size.
2222 For that reason, the \"result\" part of the record is parsed by
2223 `gdbmi-bnf-incomplete-record-result', which will keep
2224 receiving characters as they arrive from GDB until the record is complete."
2225 (let ((acc-length (length gud-marker-acc))
2226 (prefix-offset gdbmi-bnf-offset))
2228 (while (and (< prefix-offset acc-length)
2229 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
2230 (setq prefix-offset (1+ prefix-offset)))
2232 (if (and (< prefix-offset acc-length)
2233 (member (aref gud-marker-acc prefix-offset) '(?* ?+ ?= ?^))
2234 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2235 gud-marker-acc gdbmi-bnf-offset))
2237 (let ((token (match-string 1 gud-marker-acc))
2238 (prefix (match-string 2 gud-marker-acc))
2239 (class (match-string 3 gud-marker-acc))
2240 (complete (string-equal (match-string 4 gud-marker-acc) "\n"))
2241 class-alist
2242 class-command)
2244 (setq gdbmi-bnf-offset (match-end 0))
2245 (if gdbmi-debug-mode (message "gdbmi-bnf-result-record: %s"
2246 (match-string 0 gud-marker-acc)))
2248 (setq class-alist
2249 (cdr (assoc prefix gdbmi-bnf-result-state-configs)))
2250 (setq class-command (cdr (assoc class class-alist)))
2251 (if (null class-command)
2252 (setq class-command (cdr (assoc 'default class-alist))))
2254 (if complete
2255 (if class-command
2256 (if (equal (cdr class-command) 'progressive)
2257 (funcall (car class-command) token "" complete)
2258 (funcall (car class-command) token "")))
2259 (setq gdbmi-bnf-state
2260 (lambda ()
2261 (gdbmi-bnf-incomplete-record-result token class-command)))
2262 (funcall gdbmi-bnf-state))
2263 t))))
2265 (defun gdbmi-bnf-incomplete-record-result (token class-command)
2266 "State of the parser used to progressively parse a result-record or async-record
2267 rule from an incomplete data stream. The parser will stay in this state until
2268 the end of the current result or async record is reached."
2269 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2270 ;; Search the data stream for the end of the current record:
2271 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset))
2272 (is-progressive (equal (cdr class-command) 'progressive))
2273 (is-complete (not (null newline-pos)))
2274 result-str)
2276 (when gdbmi-debug-mode
2277 (message "gdbmi-bnf-incomplete-record-result: %s"
2278 (substring gud-marker-acc gdbmi-bnf-offset newline-pos)))
2280 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2281 ;; be processed by the class-command handler:
2282 (when (or is-complete is-progressive)
2283 (setq result-str
2284 (substring gud-marker-acc gdbmi-bnf-offset newline-pos))
2286 ;; Move gdbmi-bnf-offset past the end of the chunk.
2287 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length result-str)))
2288 (when newline-pos
2289 (setq gdbmi-bnf-offset (1+ gdbmi-bnf-offset))))
2291 ;; Update the parsing state before invoking the handler in class-command
2292 ;; to make sure it's not left in an invalid state if the handler was
2293 ;; to generate an error.
2294 (if is-complete
2295 (setq gdbmi-bnf-state 'gdbmi-bnf-output))
2297 (if class-command
2298 (if is-progressive
2299 (funcall (car class-command) token result-str is-complete)
2300 (if is-complete
2301 (funcall (car class-command) token result-str))))
2303 (unless is-complete
2304 ;; Incomplete gdb response: abort parsing until we receive more data.
2305 (if gdbmi-debug-mode (message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2306 (throw 'gdbmi-incomplete-stream nil))
2308 is-complete)))
2311 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2312 ; The handling of those rules is currently done by the handlers registered
2313 ; in gdbmi-bnf-result-state-configs
2315 ; result ==>
2316 ; variable "=" value
2318 ; variable ==>
2319 ; string
2321 ; value ==>
2322 ; const | tuple | list
2324 ; const ==>
2325 ; c-string
2327 ; tuple ==>
2328 ; "{}" | "{" result ( "," result )* "}"
2330 ; list ==>
2331 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2333 (defcustom gdb-mi-decode-strings nil
2334 "When non-nil, decode octal escapes in GDB output into non-ASCII text.
2336 If the value is a coding-system, use that coding-system to decode
2337 the bytes reconstructed from octal escapes. Any other non-nil value
2338 means to decode using the coding-system set for the GDB process.
2340 Warning: setting this non-nil might mangle strings reported by GDB
2341 that have literal substrings which match the \\nnn octal escape
2342 patterns, where nnn is an octal number between 200 and 377. So
2343 we only recommend to set this variable non-nil if the program you
2344 are debugging really reports non-ASCII text, or some of its source
2345 file names include non-ASCII characters."
2346 :type '(choice
2347 (const :tag "Don't decode" nil)
2348 (const :tag "Decode using default coding-system" t)
2349 (coding-system :tag "Decode using this coding-system"))
2350 :group 'gdb
2351 :version "25.1")
2353 ;; The idea of the following function was suggested
2354 ;; by Kenichi Handa <handa@gnu.org>.
2356 ;; FIXME: This is fragile: it relies on the assumption that all the
2357 ;; non-ASCII strings output by GDB, including names of the source
2358 ;; files, values of string variables in the inferior, etc., are all
2359 ;; encoded in the same encoding. It also assumes that the \nnn
2360 ;; sequences are not split between chunks of output of the GDB process
2361 ;; due to buffering, and arrive together. Finally, if some string
2362 ;; included literal \nnn strings (as opposed to non-ASCII characters
2363 ;; converted by by GDB/MI to octal escapes), this decoding will mangle
2364 ;; those strings. When/if GDB acquires the ability to not
2365 ;; escape-protect non-ASCII characters in its MI output, this kludge
2366 ;; should be removed.
2367 (defun gdb-mi-decode (string)
2368 "Decode octal escapes in MI output STRING into multibyte text."
2369 (let ((coding
2370 (if (coding-system-p gdb-mi-decode-strings)
2371 gdb-mi-decode-strings
2372 (with-current-buffer
2373 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2374 buffer-file-coding-system))))
2375 (with-temp-buffer
2376 (set-buffer-multibyte nil)
2377 (prin1 string (current-buffer))
2378 (goto-char (point-min))
2379 ;; prin1 quotes the octal escapes as well, which interferes with
2380 ;; their interpretation by 'read' below. Remove the extra
2381 ;; backslashes to countermand that.
2382 (while (re-search-forward "\\\\\\(\\\\[2-3][0-7][0-7]\\)" nil t)
2383 (replace-match "\\1" nil nil))
2384 (goto-char (point-min))
2385 (decode-coding-string (read (current-buffer)) coding))))
2387 (defun gud-gdbmi-marker-filter (string)
2388 "Filter GDB/MI output."
2390 ;; If required, decode non-ASCII text encoded with octal escapes.
2391 (or (null gdb-mi-decode-strings)
2392 (setq string (gdb-mi-decode string)))
2394 ;; Record transactions if logging is enabled.
2395 (when gdb-enable-debug
2396 (push (cons 'recv string) gdb-debug-log)
2397 (if (and gdb-debug-log-max
2398 (> (length gdb-debug-log) gdb-debug-log-max))
2399 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
2401 ;; Recall the left over gud-marker-acc from last time.
2402 (setq gud-marker-acc (concat gud-marker-acc string))
2404 ;; Start accumulating output for the GUD buffer.
2405 (setq gdb-filter-output "")
2407 (let ((acc-length (length gud-marker-acc)))
2408 (catch 'gdbmi-incomplete-stream
2409 (while (and (< gdbmi-bnf-offset acc-length)
2410 (funcall gdbmi-bnf-state)))))
2412 (when (/= gdbmi-bnf-offset 0)
2413 (setq gud-marker-acc (substring gud-marker-acc gdbmi-bnf-offset))
2414 (setq gdbmi-bnf-offset 0))
2416 (when (and gdbmi-debug-mode (> (length gud-marker-acc) 0))
2417 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc))
2419 gdb-filter-output)
2421 (defun gdb-gdb (_output-field))
2423 (defun gdb-shell (output-field)
2424 (setq gdb-filter-output
2425 (concat output-field gdb-filter-output)))
2427 (defun gdb-ignored-notification (_token _output-field))
2429 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2430 (defun gdb-thread-created (_token _output-field))
2431 (defun gdb-thread-exited (_token output-field)
2432 "Handle =thread-exited async record.
2433 Unset `gdb-thread-number' if current thread exited and update threads list."
2434 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
2435 (if (string= gdb-thread-number thread-id)
2436 (gdb-setq-thread-number nil))
2437 ;; When we continue current thread and it quickly exits,
2438 ;; the pending triggers in gdb-handler-list left after gdb-running
2439 ;; disallow us to properly call -thread-info without --thread option.
2440 ;; Thus we need to use gdb-wait-for-pending.
2441 (gdb-wait-for-pending
2442 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
2444 (defun gdb-thread-selected (_token output-field)
2445 "Handler for =thread-selected MI output record.
2447 Sets `gdb-thread-number' to new id."
2448 (let* ((result (gdb-json-string output-field))
2449 (thread-id (bindat-get-field result 'id)))
2450 (gdb-setq-thread-number thread-id)
2451 ;; Typing `thread N' in GUD buffer makes GDB emit `^done' followed
2452 ;; by `=thread-selected' notification. `^done' causes `gdb-update'
2453 ;; as usually. Things happen too fast and second call (from
2454 ;; gdb-thread-selected handler) gets cut off by our beloved
2455 ;; pending triggers.
2456 ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
2457 ;; body will get executed when `gdb-handler-list' if free of
2458 ;; pending triggers.
2459 (gdb-wait-for-pending
2460 (gdb-update))))
2462 (defun gdb-running (_token output-field)
2463 (let* ((thread-id
2464 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
2465 ;; We reset gdb-frame-number to nil if current thread has gone
2466 ;; running. This can't be done in gdb-thread-list-handler-custom
2467 ;; because we need correct gdb-frame-number by the time
2468 ;; -thread-info command is sent.
2469 (when (or (string-equal thread-id "all")
2470 (string-equal thread-id gdb-thread-number))
2471 (setq gdb-frame-number nil)))
2472 (setq gdb-inferior-status "running")
2473 (gdb-force-mode-line-update
2474 (propertize gdb-inferior-status 'face font-lock-type-face))
2475 (when (not gdb-non-stop)
2476 (setq gud-running t))
2477 (setq gdb-active-process t))
2479 (defun gdb-starting (_output-field _result)
2480 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2481 (setq gdb-inferior-status "running")
2482 (gdb-force-mode-line-update
2483 (propertize gdb-inferior-status 'face font-lock-type-face))
2484 (setq gdb-active-process t)
2485 (setq gud-running t))
2487 ;; -break-insert -t didn't give a reason before gdb 6.9
2489 (defun gdb-stopped (_token output-field)
2490 "Given the contents of *stopped MI async record, select new
2491 current thread and update GDB buffers."
2492 ;; Reason is available with target-async only
2493 (let* ((result (gdb-json-string output-field))
2494 (reason (bindat-get-field result 'reason))
2495 (thread-id (bindat-get-field result 'thread-id))
2496 (retval (bindat-get-field result 'return-value))
2497 (varnum (bindat-get-field result 'gdb-result-var)))
2499 ;; -data-list-register-names needs to be issued for any stopped
2500 ;; thread
2501 (when (not gdb-register-names)
2502 (gdb-input (concat "-data-list-register-names"
2503 (if gdb-supports-non-stop
2504 (concat " --thread " thread-id)))
2505 'gdb-register-names-handler))
2507 ;; Don't set gud-last-frame here as it's currently done in
2508 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2509 ;; with CLI.
2510 ;;(when file
2511 ;; (setq
2512 ;; ;; Extract the frame position from the marker.
2513 ;; gud-last-frame (cons file
2514 ;; (string-to-number
2515 ;; (match-string 6 gud-marker-acc)))))
2517 (setq gdb-inferior-status (or reason "unknown"))
2518 (gdb-force-mode-line-update
2519 (propertize gdb-inferior-status 'face font-lock-warning-face))
2520 (if (string-equal reason "exited-normally")
2521 (setq gdb-active-process nil))
2523 (when (and retval varnum
2524 ;; When the user typed CLI commands, GDB/MI helpfully
2525 ;; includes the "Value returned" response in the "~"
2526 ;; record; here we avoid displaying it twice.
2527 (not (string-match "^Value returned is " gdb-filter-output)))
2528 (setq gdb-filter-output
2529 (concat gdb-filter-output
2530 (format "Value returned is %s = %s\n" varnum retval))))
2532 ;; Select new current thread.
2534 ;; Don't switch if we have no reasons selected
2535 (when gdb-switch-reasons
2536 ;; Switch from another stopped thread only if we have
2537 ;; gdb-switch-when-another-stopped:
2538 (when (or gdb-switch-when-another-stopped
2539 (not (string= "stopped"
2540 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2541 ;; Switch if current reason has been selected or we have no
2542 ;; reasons
2543 (if (or (eq gdb-switch-reasons t)
2544 (member reason gdb-switch-reasons))
2545 (when (not (string-equal gdb-thread-number thread-id))
2546 (message "Switched to thread %s" thread-id)
2547 (gdb-setq-thread-number thread-id))
2548 (message "Thread %s stopped" thread-id))))
2550 ;; Print "(gdb)" to GUD console
2551 (when gdb-first-done-or-error
2552 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2554 ;; In non-stop, we update information as soon as another thread gets
2555 ;; stopped
2556 (when (or gdb-first-done-or-error
2557 gdb-non-stop)
2558 ;; In all-stop this updates gud-running properly as well.
2559 (gdb-update)
2560 (setq gdb-first-done-or-error nil))
2561 (run-hook-with-args 'gdb-stopped-functions result)))
2563 ;; Remove the trimmings from log stream containing debugging messages
2564 ;; being produced by GDB's internals, use warning face and send to GUD
2565 ;; buffer.
2566 (defun gdb-internals (output-field)
2567 (setq gdb-filter-output
2568 (gdb-concat-output
2569 gdb-filter-output
2570 (if (string= output-field "\"\\n\"")
2572 (let ((error-message
2573 (read output-field)))
2574 (put-text-property
2575 0 (length error-message)
2576 'face font-lock-warning-face
2577 error-message)
2578 error-message)))))
2580 ;; Remove the trimmings from the console stream and send to GUD buffer
2581 ;; (frontend MI commands should not print to this stream)
2582 (defun gdb-console (output-field)
2583 (setq gdb-filter-output
2584 (gdb-concat-output gdb-filter-output (read output-field))))
2586 (defun gdb-done (token-number output-field is-complete)
2587 (gdb-done-or-error token-number 'done output-field is-complete))
2589 (defun gdb-error (token-number output-field is-complete)
2590 (gdb-done-or-error token-number 'error output-field is-complete))
2592 (defun gdb-done-or-error (token-number type output-field is-complete)
2593 (if (string-equal token-number "")
2594 ;; Output from command entered by user
2595 (progn
2596 (setq gdb-output-sink 'user)
2597 (setq token-number nil)
2598 ;; MI error - send to minibuffer
2599 (when (eq type 'error)
2600 ;; Skip "msg=" from `output-field'
2601 (message "%s" (read (substring output-field 4)))
2602 ;; Don't send to the console twice. (If it is a console error
2603 ;; it is also in the console stream.)
2604 (setq output-field nil)))
2605 ;; Output from command from frontend.
2606 (setq gdb-output-sink 'emacs))
2608 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2609 (let* ((proc (get-buffer-process gud-comint-buffer))
2610 (no-proc (or (null proc)
2611 (memq (process-status proc) '(exit signal)))))
2613 (when (and is-complete gdb-first-done-or-error)
2614 (unless (or token-number gud-running no-proc)
2615 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2616 (gdb-update no-proc)
2617 (setq gdb-first-done-or-error nil))
2619 (setq gdb-filter-output
2620 (gdb-concat-output gdb-filter-output output-field))
2622 ;; We are done concatenating to the output sink. Restore it to user sink:
2623 (setq gdb-output-sink 'user)
2625 (when (and token-number is-complete)
2626 (with-current-buffer
2627 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2628 (gdb-handle-reply (string-to-number token-number))))
2630 (when is-complete
2631 (gdb-clear-partial-output))))
2633 (defun gdb-concat-output (so-far new)
2634 (cond
2635 ((eq gdb-output-sink 'user) (concat so-far new))
2636 ((eq gdb-output-sink 'emacs)
2637 (gdb-append-to-partial-output new)
2638 so-far)))
2640 (defun gdb-append-to-partial-output (string)
2641 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2642 (goto-char (point-max))
2643 (insert string)))
2645 (defun gdb-clear-partial-output ()
2646 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2647 (erase-buffer)))
2649 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2650 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2652 Field names are wrapped in double quotes and equal signs are
2653 replaced with semicolons.
2655 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2656 partial output. This is used to get rid of useless keys in lists
2657 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2658 -break-info are examples of MI commands which issue such
2659 responses.
2661 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2662 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2663 -break-info output when it contains breakpoint script field
2664 incompatible with GDB/MI output syntax.
2666 If `default-directory' is remote, full file names are adapted accordingly."
2667 (save-excursion
2668 (let ((remote (file-remote-p default-directory)))
2669 (when remote
2670 (goto-char (point-min))
2671 (while (re-search-forward "[\\[,]fullname=\"\\(.+\\)\"" nil t)
2672 (replace-match (concat remote "\\1") nil nil nil 1))))
2673 (goto-char (point-min))
2674 (when fix-key
2675 (save-excursion
2676 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2677 (replace-match "" nil nil nil 1))))
2678 (when fix-list
2679 (save-excursion
2680 ;; Find positions of braces which enclose broken list
2681 (while (re-search-forward (concat fix-list "={\"") nil t)
2682 (let ((p1 (goto-char (- (point) 2)))
2683 (p2 (progn (forward-sexp)
2684 (1- (point)))))
2685 ;; Replace braces with brackets
2686 (save-excursion
2687 (goto-char p1)
2688 (delete-char 1)
2689 (insert "[")
2690 (goto-char p2)
2691 (delete-char 1)
2692 (insert "]"))))))
2693 (goto-char (point-min))
2694 (insert "{")
2695 (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
2696 gdb--string-regexp "\\)")))
2697 (while (re-search-forward re nil t)
2698 (replace-match "\"\\1\":\\2" nil nil)))
2699 (goto-char (point-max))
2700 (insert "}")))
2702 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2703 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2705 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2706 (gdb-jsonify-buffer fix-key fix-list)
2707 (save-excursion
2708 (goto-char (point-min))
2709 (let ((json-array-type 'list))
2710 (json-read))))
2712 (defun gdb-json-string (string &optional fix-key fix-list)
2713 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2715 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2716 (with-temp-buffer
2717 (insert string)
2718 (gdb-json-read-buffer fix-key fix-list)))
2720 (defun gdb-json-partial-output (&optional fix-key fix-list)
2721 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2723 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2724 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2725 (gdb-json-read-buffer fix-key fix-list)))
2727 (defun gdb-line-posns (line)
2728 "Return a pair of LINE beginning and end positions."
2729 (let ((offset (1+ (- line (line-number-at-pos)))))
2730 (cons
2731 (line-beginning-position offset)
2732 (line-end-position offset))))
2734 (defmacro gdb-mark-line (line variable)
2735 "Set VARIABLE marker to point at beginning of LINE.
2737 If current window has no fringes, inverse colors on LINE.
2739 Return position where LINE begins."
2740 `(save-excursion
2741 (let* ((posns (gdb-line-posns ,line))
2742 (start-posn (car posns))
2743 (end-posn (cdr posns)))
2744 (set-marker ,variable (copy-marker start-posn))
2745 (when (not (> (car (window-fringes)) 0))
2746 (put-text-property start-posn end-posn
2747 'font-lock-face '(:inverse-video t)))
2748 start-posn)))
2750 (defun gdb-pad-string (string padding)
2751 (format (concat "%" (number-to-string padding) "s") string))
2753 ;; gdb-table struct is a way to programmatically construct simple
2754 ;; tables. It help to reliably align columns of data in GDB buffers
2755 ;; and provides
2756 (cl-defstruct gdb-table
2757 (column-sizes nil)
2758 (rows nil)
2759 (row-properties nil)
2760 (right-align nil))
2762 (defun gdb-table-add-row (table row &optional properties)
2763 "Add ROW of string to TABLE and recalculate column sizes.
2765 When non-nil, PROPERTIES will be added to the whole row when
2766 calling `gdb-table-string'."
2767 (let ((rows (gdb-table-rows table))
2768 (row-properties (gdb-table-row-properties table))
2769 (column-sizes (gdb-table-column-sizes table))
2770 (right-align (gdb-table-right-align table)))
2771 (when (not column-sizes)
2772 (setf (gdb-table-column-sizes table)
2773 (make-list (length row) 0)))
2774 (setf (gdb-table-rows table)
2775 (append rows (list row)))
2776 (setf (gdb-table-row-properties table)
2777 (append row-properties (list properties)))
2778 (setf (gdb-table-column-sizes table)
2779 (cl-mapcar (lambda (x s)
2780 (let ((new-x
2781 (max (abs x) (string-width (or s "")))))
2782 (if right-align new-x (- new-x))))
2783 (gdb-table-column-sizes table)
2784 row))
2785 ;; Avoid trailing whitespace at eol
2786 (if (not (gdb-table-right-align table))
2787 (setcar (last (gdb-table-column-sizes table)) 0))))
2789 (defun gdb-table-string (table &optional sep)
2790 "Return TABLE as a string with columns separated with SEP."
2791 (let ((column-sizes (gdb-table-column-sizes table)))
2792 (mapconcat
2793 'identity
2794 (cl-mapcar
2795 (lambda (row properties)
2796 (apply 'propertize
2797 (mapconcat 'identity
2798 (cl-mapcar (lambda (s x) (gdb-pad-string s x))
2799 row column-sizes)
2800 sep)
2801 properties))
2802 (gdb-table-rows table)
2803 (gdb-table-row-properties table))
2804 "\n")))
2806 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2807 (defun gdb-get-many-fields (struct &rest fields)
2808 "Return a list of FIELDS values from STRUCT."
2809 (let ((values))
2810 (dolist (field fields)
2811 (push (bindat-get-field struct field) values))
2812 (nreverse values)))
2814 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2815 handler-name
2816 &optional signal-list)
2817 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2818 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2819 buffer with `gdb-bind-function-to-buffer'.
2821 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2822 defined trigger is called with an argument from SIGNAL-LIST. It's
2823 not recommended to define triggers with empty SIGNAL-LIST.
2824 Normally triggers should respond at least to the `update' signal.
2826 Normally the trigger defined by this command must be called from
2827 the buffer where HANDLER-NAME must work. This should be done so
2828 that buffer-local thread number may be used in GDB-COMMAND (by
2829 calling `gdb-current-context-command').
2830 `gdb-bind-function-to-buffer' is used to achieve this, see
2831 `gdb-get-buffer-create'.
2833 Triggers defined by this command are meant to be used as a
2834 trigger argument when describing buffer types with
2835 `gdb-set-buffer-rules'."
2836 `(defun ,trigger-name (&optional signal)
2837 (when
2838 (or (not ,signal-list)
2839 (memq signal ,signal-list))
2840 (gdb-input ,gdb-command
2841 (gdb-bind-function-to-buffer ',handler-name (current-buffer))
2842 (cons (current-buffer) ',trigger-name)))))
2844 ;; Used by disassembly buffer only, the rest use
2845 ;; def-gdb-trigger-and-handler
2846 (defmacro def-gdb-auto-update-handler (handler-name custom-defun
2847 &optional nopreserve)
2848 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2850 Handlers are normally called from the buffers they put output in.
2852 Erase current buffer and evaluate CUSTOM-DEFUN.
2853 Then call `gdb-update-buffer-name'.
2855 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2856 `(defun ,handler-name ()
2857 (let* ((inhibit-read-only t)
2858 ,@(unless nopreserve
2859 '((window (get-buffer-window (current-buffer) 0))
2860 (start (window-start window))
2861 (p (window-point window)))))
2862 (erase-buffer)
2863 (,custom-defun)
2864 (gdb-update-buffer-name)
2865 ,@(when (not nopreserve)
2866 '((set-window-start window start)
2867 (set-window-point window p))))))
2869 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2870 handler-name custom-defun
2871 &optional signal-list)
2872 "Define trigger and handler.
2874 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2875 See `def-gdb-auto-update-trigger'.
2877 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2878 See `def-gdb-auto-update-handler'."
2879 `(progn
2880 (def-gdb-auto-update-trigger ,trigger-name
2881 ,gdb-command
2882 ,handler-name ,signal-list)
2883 (def-gdb-auto-update-handler ,handler-name
2884 ,custom-defun)))
2888 ;; Breakpoint buffer : This displays the output of `-break-list'.
2889 (def-gdb-trigger-and-handler
2890 gdb-invalidate-breakpoints "-break-list"
2891 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2892 '(start update))
2894 (gdb-set-buffer-rules
2895 'gdb-breakpoints-buffer
2896 'gdb-breakpoints-buffer-name
2897 'gdb-breakpoints-mode
2898 'gdb-invalidate-breakpoints)
2900 (defun gdb-breakpoints-list-handler-custom ()
2901 (let ((breakpoints-list (bindat-get-field
2902 (gdb-json-partial-output "bkpt" "script")
2903 'BreakpointTable 'body))
2904 (table (make-gdb-table)))
2905 (setq gdb-breakpoints-list nil)
2906 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2907 (dolist (breakpoint breakpoints-list)
2908 (add-to-list 'gdb-breakpoints-list
2909 (cons (bindat-get-field breakpoint 'number)
2910 breakpoint))
2911 (let ((at (bindat-get-field breakpoint 'at))
2912 (pending (bindat-get-field breakpoint 'pending))
2913 (func (bindat-get-field breakpoint 'func))
2914 (type (bindat-get-field breakpoint 'type)))
2915 (gdb-table-add-row table
2916 (list
2917 (bindat-get-field breakpoint 'number)
2918 (or type "")
2919 (or (bindat-get-field breakpoint 'disp) "")
2920 (let ((flag (bindat-get-field breakpoint 'enabled)))
2921 (if (string-equal flag "y")
2922 (eval-when-compile
2923 (propertize "y" 'font-lock-face
2924 font-lock-warning-face))
2925 (eval-when-compile
2926 (propertize "n" 'font-lock-face
2927 font-lock-comment-face))))
2928 (bindat-get-field breakpoint 'addr)
2929 (or (bindat-get-field breakpoint 'times) "")
2930 (if (and type (string-match ".*watchpoint" type))
2931 (bindat-get-field breakpoint 'what)
2932 (or pending at
2933 (concat "in "
2934 (propertize (or func "unknown")
2935 'font-lock-face font-lock-function-name-face)
2936 (gdb-frame-location breakpoint)))))
2937 ;; Add clickable properties only for breakpoints with file:line
2938 ;; information
2939 (append (list 'gdb-breakpoint breakpoint)
2940 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2941 mouse-face highlight))))))
2942 (insert (gdb-table-string table " "))
2943 (gdb-place-breakpoints)))
2945 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2946 (defun gdb-place-breakpoints ()
2947 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2948 (dolist (buffer (buffer-list))
2949 (with-current-buffer buffer
2950 (if (and (eq gud-minor-mode 'gdbmi)
2951 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2952 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2953 (dolist (breakpoint gdb-breakpoints-list)
2954 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2955 ; an associative list
2956 (line (bindat-get-field breakpoint 'line)))
2957 (when line
2958 (let ((file (bindat-get-field breakpoint 'fullname))
2959 (flag (bindat-get-field breakpoint 'enabled))
2960 (bptno (bindat-get-field breakpoint 'number)))
2961 (unless (and file (file-exists-p file))
2962 (setq file (cdr (assoc bptno gdb-location-alist))))
2963 (if (or (null file)
2964 (string-equal file "File not found"))
2965 ;; If the full filename is not recorded in the
2966 ;; breakpoint structure or in `gdb-location-alist', use
2967 ;; -file-list-exec-source-file to extract it.
2968 (when (setq file (bindat-get-field breakpoint 'file))
2969 (gdb-input (concat "list " file ":1") 'ignore)
2970 (gdb-input "-file-list-exec-source-file"
2971 `(lambda () (gdb-get-location
2972 ,bptno ,line ,flag))))
2973 (with-current-buffer (find-file-noselect file 'nowarn)
2974 (gdb-init-buffer)
2975 ;; Only want one breakpoint icon at each location.
2976 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2977 (string-to-number line)))))))))
2979 (defconst gdb-source-file-regexp
2980 (concat "fullname=\\(" gdb--string-regexp "\\)"))
2982 (defun gdb-get-location (bptno line flag)
2983 "Find the directory containing the relevant source file.
2984 Put in buffer and place breakpoint icon."
2985 (goto-char (point-min))
2986 (catch 'file-not-found
2987 (if (re-search-forward gdb-source-file-regexp nil t)
2988 (delete (cons bptno "File not found") gdb-location-alist)
2989 ;; FIXME: Why/how do we use (match-string 1) when the search failed?
2990 (push (cons bptno (match-string 1)) gdb-location-alist)
2991 (gdb-resync)
2992 (unless (assoc bptno gdb-location-alist)
2993 (push (cons bptno "File not found") gdb-location-alist)
2994 (message-box "Cannot find source file for breakpoint location.
2995 Add directory to search path for source files using the GDB command, dir."))
2996 (throw 'file-not-found nil))
2997 (with-current-buffer (find-file-noselect (match-string 1))
2998 (gdb-init-buffer)
2999 ;; only want one breakpoint icon at each location
3000 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
3002 (add-hook 'find-file-hook 'gdb-find-file-hook)
3004 (defun gdb-find-file-hook ()
3005 "Set up buffer for debugging if file is part of the source code
3006 of the current session."
3007 (if (and (buffer-name gud-comint-buffer)
3008 ;; in case gud or gdb-ui is just loaded
3009 gud-comint-buffer
3010 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3011 'gdbmi))
3012 (if (member buffer-file-name gdb-source-file-list)
3013 (with-current-buffer (find-buffer-visiting buffer-file-name)
3014 (gdb-init-buffer)))))
3016 (declare-function gud-remove "gdb-mi" t t) ; gud-def
3017 (declare-function gud-break "gdb-mi" t t) ; gud-def
3018 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
3020 (defun gdb-mouse-set-clear-breakpoint (event)
3021 "Set/clear breakpoint in left fringe/margin at mouse click.
3022 If not in a source or disassembly buffer just set point."
3023 (interactive "e")
3024 (mouse-minibuffer-check event)
3025 (let ((posn (event-end event)))
3026 (with-selected-window (posn-window posn)
3027 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
3028 (if (numberp (posn-point posn))
3029 (save-excursion
3030 (goto-char (posn-point posn))
3031 (if (or (posn-object posn)
3032 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
3033 'breakpoint))
3034 (gud-remove nil)
3035 (gud-break nil)))))
3036 (posn-set-point posn))))
3038 (defun gdb-mouse-toggle-breakpoint-margin (event)
3039 "Enable/disable breakpoint in left margin with mouse click."
3040 (interactive "e")
3041 (mouse-minibuffer-check event)
3042 (let ((posn (event-end event)))
3043 (if (numberp (posn-point posn))
3044 (with-selected-window (posn-window posn)
3045 (save-excursion
3046 (goto-char (posn-point posn))
3047 (if (posn-object posn)
3048 (gud-basic-call
3049 (let ((bptno (get-text-property
3050 0 'gdb-bptno (car (posn-string posn)))))
3051 (concat
3052 (if (get-text-property
3053 0 'gdb-enabled (car (posn-string posn)))
3054 "-break-disable "
3055 "-break-enable ")
3056 bptno)))))))))
3058 (defun gdb-mouse-toggle-breakpoint-fringe (event)
3059 "Enable/disable breakpoint in left fringe with mouse click."
3060 (interactive "e")
3061 (mouse-minibuffer-check event)
3062 (let* ((posn (event-end event))
3063 (pos (posn-point posn))
3064 obj)
3065 (when (numberp pos)
3066 (with-selected-window (posn-window posn)
3067 (with-current-buffer (window-buffer)
3068 (goto-char pos)
3069 (dolist (overlay (overlays-in pos pos))
3070 (when (overlay-get overlay 'put-break)
3071 (setq obj (overlay-get overlay 'before-string))))
3072 (when (stringp obj)
3073 (gud-basic-call
3074 (concat
3075 (if (get-text-property 0 'gdb-enabled obj)
3076 "-break-disable "
3077 "-break-enable ")
3078 (get-text-property 0 'gdb-bptno obj)))))))))
3080 (defun gdb-breakpoints-buffer-name ()
3081 (concat "*breakpoints of " (gdb-get-target-string) "*"))
3083 (defun gdb-display-breakpoints-buffer (&optional thread)
3084 "Display GDB breakpoints."
3085 (interactive)
3086 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
3088 (defun gdb-frame-breakpoints-buffer (&optional thread)
3089 "Display GDB breakpoints in another frame."
3090 (interactive)
3091 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
3092 gdb-display-buffer-other-frame-action))
3094 (defvar gdb-breakpoints-mode-map
3095 (let ((map (make-sparse-keymap))
3096 (menu (make-sparse-keymap "Breakpoints")))
3097 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
3098 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
3099 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
3100 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
3101 (suppress-keymap map)
3102 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
3103 (define-key map " " 'gdb-toggle-breakpoint)
3104 (define-key map "D" 'gdb-delete-breakpoint)
3105 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
3106 (define-key map "q" 'gdb-delete-frame-or-window)
3107 (define-key map "\r" 'gdb-goto-breakpoint)
3108 (define-key map "\t" (lambda ()
3109 (interactive)
3110 (gdb-set-window-buffer
3111 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
3112 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3113 (define-key map [follow-link] 'mouse-face)
3114 map))
3116 (defun gdb-delete-frame-or-window ()
3117 "Delete frame if there is only one window. Otherwise delete the window."
3118 (interactive)
3119 (if (one-window-p) (delete-frame)
3120 (delete-window)))
3122 ;;from make-mode-line-mouse-map
3123 (defun gdb-make-header-line-mouse-map (mouse function) "\
3124 Return a keymap with single entry for mouse key MOUSE on the header line.
3125 MOUSE is defined to run function FUNCTION with no args in the buffer
3126 corresponding to the mode line clicked."
3127 (let ((map (make-sparse-keymap)))
3128 (define-key map (vector 'header-line mouse) function)
3129 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3130 map))
3132 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3133 `(propertize ,name
3134 'help-echo ,help-echo
3135 'mouse-face ',mouse-face
3136 'face ',face
3137 'local-map
3138 (gdb-make-header-line-mouse-map
3139 'mouse-1
3140 (lambda (event) (interactive "e")
3141 (save-selected-window
3142 (select-window (posn-window (event-start event)))
3143 (gdb-set-window-buffer
3144 (gdb-get-buffer-create ',buffer) t) )))))
3147 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3148 ;;; Threads view
3150 (defun gdb-threads-buffer-name ()
3151 (concat "*threads of " (gdb-get-target-string) "*"))
3153 (defun gdb-display-threads-buffer (&optional thread)
3154 "Display GDB threads."
3155 (interactive)
3156 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3158 (defun gdb-frame-threads-buffer (&optional thread)
3159 "Display GDB threads in another frame."
3160 (interactive)
3161 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3162 gdb-display-buffer-other-frame-action))
3164 (def-gdb-trigger-and-handler
3165 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3166 gdb-thread-list-handler gdb-thread-list-handler-custom
3167 '(start update update-threads))
3169 (gdb-set-buffer-rules
3170 'gdb-threads-buffer
3171 'gdb-threads-buffer-name
3172 'gdb-threads-mode
3173 'gdb-invalidate-threads)
3175 (defvar gdb-threads-font-lock-keywords
3176 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3177 (" \\(stopped\\)" (1 font-lock-warning-face))
3178 (" \\(running\\)" (1 font-lock-string-face))
3179 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3180 "Font lock keywords used in `gdb-threads-mode'.")
3182 (defvar gdb-threads-mode-map
3183 (let ((map (make-sparse-keymap)))
3184 (define-key map "\r" 'gdb-select-thread)
3185 (define-key map "f" 'gdb-display-stack-for-thread)
3186 (define-key map "F" 'gdb-frame-stack-for-thread)
3187 (define-key map "l" 'gdb-display-locals-for-thread)
3188 (define-key map "L" 'gdb-frame-locals-for-thread)
3189 (define-key map "r" 'gdb-display-registers-for-thread)
3190 (define-key map "R" 'gdb-frame-registers-for-thread)
3191 (define-key map "d" 'gdb-display-disassembly-for-thread)
3192 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3193 (define-key map "i" 'gdb-interrupt-thread)
3194 (define-key map "c" 'gdb-continue-thread)
3195 (define-key map "s" 'gdb-step-thread)
3196 (define-key map "\t"
3197 (lambda ()
3198 (interactive)
3199 (gdb-set-window-buffer
3200 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3201 (define-key map [mouse-2] 'gdb-select-thread)
3202 (define-key map [follow-link] 'mouse-face)
3203 map))
3205 (defvar gdb-threads-header
3206 (list
3207 (gdb-propertize-header
3208 "Breakpoints" gdb-breakpoints-buffer
3209 "mouse-1: select" mode-line-highlight mode-line-inactive)
3211 (gdb-propertize-header "Threads" gdb-threads-buffer
3212 nil nil mode-line)))
3214 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3215 "Major mode for GDB threads."
3216 (setq gdb-thread-position (make-marker))
3217 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3218 (setq header-line-format gdb-threads-header)
3219 (set (make-local-variable 'font-lock-defaults)
3220 '(gdb-threads-font-lock-keywords))
3221 'gdb-invalidate-threads)
3223 (defun gdb-thread-list-handler-custom ()
3224 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3225 (table (make-gdb-table))
3226 (marked-line nil))
3227 (setq gdb-threads-list nil)
3228 (setq gdb-running-threads-count 0)
3229 (setq gdb-stopped-threads-count 0)
3230 (set-marker gdb-thread-position nil)
3232 (dolist (thread (reverse threads-list))
3233 (let ((running (equal (bindat-get-field thread 'state) "running")))
3234 (add-to-list 'gdb-threads-list
3235 (cons (bindat-get-field thread 'id)
3236 thread))
3237 (cl-incf (if running
3238 gdb-running-threads-count
3239 gdb-stopped-threads-count))
3241 (gdb-table-add-row
3242 table
3243 (list
3244 (bindat-get-field thread 'id)
3245 (concat
3246 (if gdb-thread-buffer-verbose-names
3247 (concat (bindat-get-field thread 'target-id) " ") "")
3248 (bindat-get-field thread 'state)
3249 ;; Include frame information for stopped threads
3250 (if (not running)
3251 (concat
3252 " in " (bindat-get-field thread 'frame 'func)
3253 (if gdb-thread-buffer-arguments
3254 (concat
3255 " ("
3256 (let ((args (bindat-get-field thread 'frame 'args)))
3257 (mapconcat
3258 (lambda (arg)
3259 (apply #'format "%s=%s"
3260 (gdb-get-many-fields arg 'name 'value)))
3261 args ","))
3262 ")")
3264 (if gdb-thread-buffer-locations
3265 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3266 (if gdb-thread-buffer-addresses
3267 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3268 "")))
3269 (list
3270 'gdb-thread thread
3271 'mouse-face 'highlight
3272 'help-echo "mouse-2, RET: select thread")))
3273 (when (string-equal gdb-thread-number
3274 (bindat-get-field thread 'id))
3275 (setq marked-line (length gdb-threads-list))))
3276 (insert (gdb-table-string table " "))
3277 (when marked-line
3278 (gdb-mark-line marked-line gdb-thread-position)))
3279 ;; We update gud-running here because we need to make sure that
3280 ;; gdb-threads-list is up-to-date
3281 (gdb-update-gud-running)
3282 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3284 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3285 "Define a NAME command which will act upon thread on the current line.
3287 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3288 be the value of `gdb-thread' property of the current line.
3289 If `gdb-thread' is nil, error is signaled."
3290 `(defun ,name (&optional event)
3291 ,(when doc doc)
3292 (interactive (list last-input-event))
3293 (if event (posn-set-point (event-end event)))
3294 (save-excursion
3295 (beginning-of-line)
3296 (let ((thread (get-text-property (point) 'gdb-thread)))
3297 (if thread
3298 ,custom-defun
3299 (error "Not recognized as thread line"))))))
3301 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3302 &optional doc)
3303 "Define a NAME which will call BUFFER-COMMAND with id of thread
3304 on the current line."
3305 `(def-gdb-thread-buffer-command ,name
3306 (,buffer-command (bindat-get-field thread 'id))
3307 ,doc))
3309 (def-gdb-thread-buffer-command gdb-select-thread
3310 (let ((new-id (bindat-get-field thread 'id)))
3311 (gdb-setq-thread-number new-id)
3312 (gdb-input (concat "-thread-select " new-id) 'ignore)
3313 (gdb-update))
3314 "Select the thread at current line of threads buffer.")
3316 (def-gdb-thread-buffer-simple-command
3317 gdb-display-stack-for-thread
3318 gdb-preemptively-display-stack-buffer
3319 "Display stack buffer for the thread at current line.")
3321 (def-gdb-thread-buffer-simple-command
3322 gdb-display-locals-for-thread
3323 gdb-preemptively-display-locals-buffer
3324 "Display locals buffer for the thread at current line.")
3326 (def-gdb-thread-buffer-simple-command
3327 gdb-display-registers-for-thread
3328 gdb-preemptively-display-registers-buffer
3329 "Display registers buffer for the thread at current line.")
3331 (def-gdb-thread-buffer-simple-command
3332 gdb-display-disassembly-for-thread
3333 gdb-preemptively-display-disassembly-buffer
3334 "Display disassembly buffer for the thread at current line.")
3336 (def-gdb-thread-buffer-simple-command
3337 gdb-frame-stack-for-thread
3338 gdb-frame-stack-buffer
3339 "Display another frame with stack buffer for thread at current line.")
3341 (def-gdb-thread-buffer-simple-command
3342 gdb-frame-locals-for-thread
3343 gdb-frame-locals-buffer
3344 "Display another frame with locals buffer for thread at current line.")
3346 (def-gdb-thread-buffer-simple-command
3347 gdb-frame-registers-for-thread
3348 gdb-frame-registers-buffer
3349 "Display another frame with registers buffer for the thread at current line.")
3351 (def-gdb-thread-buffer-simple-command
3352 gdb-frame-disassembly-for-thread
3353 gdb-frame-disassembly-buffer
3354 "Display another frame with disassembly buffer for the thread at current line.")
3356 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3357 "Define a NAME which will execute GUD-COMMAND with
3358 `gdb-thread-number' locally bound to id of thread on the current
3359 line."
3360 `(def-gdb-thread-buffer-command ,name
3361 (if gdb-non-stop
3362 (let ((gdb-thread-number (bindat-get-field thread 'id))
3363 (gdb-gud-control-all-threads nil))
3364 (call-interactively #',gud-command))
3365 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3366 ,doc))
3368 (def-gdb-thread-buffer-gud-command
3369 gdb-interrupt-thread
3370 gud-stop-subjob
3371 "Interrupt thread at current line.")
3373 ;; Defined opaquely in M-x gdb via gud-def.
3374 (declare-function gud-cont "gdb-mi" (arg) t)
3376 (def-gdb-thread-buffer-gud-command
3377 gdb-continue-thread
3378 gud-cont
3379 "Continue thread at current line.")
3381 (declare-function gud-step "gdb-mi" (arg) t)
3383 (def-gdb-thread-buffer-gud-command
3384 gdb-step-thread
3385 gud-step
3386 "Step thread at current line.")
3389 ;;; Memory view
3391 (defcustom gdb-memory-rows 8
3392 "Number of data rows in memory window."
3393 :type 'integer
3394 :group 'gud
3395 :version "23.2")
3397 (defcustom gdb-memory-columns 4
3398 "Number of data columns in memory window."
3399 :type 'integer
3400 :group 'gud
3401 :version "23.2")
3403 (defcustom gdb-memory-format "x"
3404 "Display format of data items in memory window."
3405 :type '(choice (const :tag "Hexadecimal" "x")
3406 (const :tag "Signed decimal" "d")
3407 (const :tag "Unsigned decimal" "u")
3408 (const :tag "Octal" "o")
3409 (const :tag "Binary" "t"))
3410 :group 'gud
3411 :version "22.1")
3413 (defcustom gdb-memory-unit 4
3414 "Unit size of data items in memory window."
3415 :type '(choice (const :tag "Byte" 1)
3416 (const :tag "Halfword" 2)
3417 (const :tag "Word" 4)
3418 (const :tag "Giant word" 8))
3419 :group 'gud
3420 :version "23.2")
3422 (def-gdb-trigger-and-handler
3423 gdb-invalidate-memory
3424 (format "-data-read-memory %s %s %d %d %d"
3425 gdb-memory-address
3426 gdb-memory-format
3427 gdb-memory-unit
3428 gdb-memory-rows
3429 gdb-memory-columns)
3430 gdb-read-memory-handler
3431 gdb-read-memory-custom
3432 '(start update))
3434 (gdb-set-buffer-rules
3435 'gdb-memory-buffer
3436 'gdb-memory-buffer-name
3437 'gdb-memory-mode
3438 'gdb-invalidate-memory)
3440 (defun gdb-memory-column-width (size format)
3441 "Return length of string with memory unit of SIZE in FORMAT.
3443 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3444 in `gdb-memory-format'."
3445 (let ((format-base (cdr (assoc format
3446 '(("x" . 16)
3447 ("d" . 10) ("u" . 10)
3448 ("o" . 8)
3449 ("t" . 2))))))
3450 (if format-base
3451 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3452 (cond ((string-equal format "x")
3453 (+ 2 res)) ; hexadecimal numbers have 0x in front
3454 ((or (string-equal format "d")
3455 (string-equal format "o"))
3456 (1+ res))
3457 (t res)))
3458 (error "Unknown format"))))
3460 (defun gdb-read-memory-custom ()
3461 (let* ((res (gdb-json-partial-output))
3462 (err-msg (bindat-get-field res 'msg)))
3463 (if (not err-msg)
3464 (let ((memory (bindat-get-field res 'memory)))
3465 (setq gdb-memory-address (bindat-get-field res 'addr))
3466 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3467 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3468 (setq gdb-memory-last-address gdb-memory-address)
3469 (dolist (row memory)
3470 (insert (concat (bindat-get-field row 'addr) ":"))
3471 (dolist (column (bindat-get-field row 'data))
3472 (insert (gdb-pad-string column
3473 (+ 2 (gdb-memory-column-width
3474 gdb-memory-unit
3475 gdb-memory-format)))))
3476 (newline)))
3477 ;; Show last page instead of empty buffer when out of bounds
3478 (progn
3479 (let ((gdb-memory-address gdb-memory-last-address))
3480 (gdb-invalidate-memory 'update)
3481 (error err-msg))))))
3483 (defvar gdb-memory-mode-map
3484 (let ((map (make-sparse-keymap)))
3485 (suppress-keymap map t)
3486 (define-key map "q" 'kill-this-buffer)
3487 (define-key map "n" 'gdb-memory-show-next-page)
3488 (define-key map "p" 'gdb-memory-show-previous-page)
3489 (define-key map "a" 'gdb-memory-set-address)
3490 (define-key map "t" 'gdb-memory-format-binary)
3491 (define-key map "o" 'gdb-memory-format-octal)
3492 (define-key map "u" 'gdb-memory-format-unsigned)
3493 (define-key map "d" 'gdb-memory-format-signed)
3494 (define-key map "x" 'gdb-memory-format-hexadecimal)
3495 (define-key map "b" 'gdb-memory-unit-byte)
3496 (define-key map "h" 'gdb-memory-unit-halfword)
3497 (define-key map "w" 'gdb-memory-unit-word)
3498 (define-key map "g" 'gdb-memory-unit-giant)
3499 (define-key map "R" 'gdb-memory-set-rows)
3500 (define-key map "C" 'gdb-memory-set-columns)
3501 map))
3503 (defun gdb-memory-set-address-event (event)
3504 "Handle a click on address field in memory buffer header."
3505 (interactive "e")
3506 (save-selected-window
3507 (select-window (posn-window (event-start event)))
3508 (gdb-memory-set-address)))
3510 ;; Non-event version for use within keymap
3511 (defun gdb-memory-set-address ()
3512 "Set the start memory address."
3513 (interactive)
3514 (let ((arg (read-from-minibuffer "Memory address: ")))
3515 (setq gdb-memory-address arg))
3516 (gdb-invalidate-memory 'update))
3518 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3519 "Define a function NAME which reads new VAR value from minibuffer."
3520 `(defun ,name (event)
3521 ,(when doc doc)
3522 (interactive "e")
3523 (save-selected-window
3524 (select-window (posn-window (event-start event)))
3525 (let* ((arg (read-from-minibuffer ,echo-string))
3526 (count (string-to-number arg)))
3527 (if (<= count 0)
3528 (error "Positive number only")
3529 (customize-set-variable ',variable count)
3530 (gdb-invalidate-memory 'update))))))
3532 (def-gdb-set-positive-number
3533 gdb-memory-set-rows
3534 gdb-memory-rows
3535 "Rows: "
3536 "Set the number of data rows in memory window.")
3538 (def-gdb-set-positive-number
3539 gdb-memory-set-columns
3540 gdb-memory-columns
3541 "Columns: "
3542 "Set the number of data columns in memory window.")
3544 (defmacro def-gdb-memory-format (name format doc)
3545 "Define a function NAME to switch memory buffer to use FORMAT.
3547 DOC is an optional documentation string."
3548 `(defun ,name () ,(when doc doc)
3549 (interactive)
3550 (customize-set-variable 'gdb-memory-format ,format)
3551 (gdb-invalidate-memory 'update)))
3553 (def-gdb-memory-format
3554 gdb-memory-format-binary "t"
3555 "Set the display format to binary.")
3557 (def-gdb-memory-format
3558 gdb-memory-format-octal "o"
3559 "Set the display format to octal.")
3561 (def-gdb-memory-format
3562 gdb-memory-format-unsigned "u"
3563 "Set the display format to unsigned decimal.")
3565 (def-gdb-memory-format
3566 gdb-memory-format-signed "d"
3567 "Set the display format to decimal.")
3569 (def-gdb-memory-format
3570 gdb-memory-format-hexadecimal "x"
3571 "Set the display format to hexadecimal.")
3573 (defvar gdb-memory-format-map
3574 (let ((map (make-sparse-keymap)))
3575 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3576 map)
3577 "Keymap to select format in the header line.")
3579 (defvar gdb-memory-format-menu
3580 (let ((map (make-sparse-keymap "Format")))
3582 (define-key map [binary]
3583 '(menu-item "Binary" gdb-memory-format-binary
3584 :button (:radio . (equal gdb-memory-format "t"))))
3585 (define-key map [octal]
3586 '(menu-item "Octal" gdb-memory-format-octal
3587 :button (:radio . (equal gdb-memory-format "o"))))
3588 (define-key map [unsigned]
3589 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3590 :button (:radio . (equal gdb-memory-format "u"))))
3591 (define-key map [signed]
3592 '(menu-item "Signed Decimal" gdb-memory-format-signed
3593 :button (:radio . (equal gdb-memory-format "d"))))
3594 (define-key map [hexadecimal]
3595 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3596 :button (:radio . (equal gdb-memory-format "x"))))
3597 map)
3598 "Menu of display formats in the header line.")
3600 (defun gdb-memory-format-menu (event)
3601 (interactive "@e")
3602 (x-popup-menu event gdb-memory-format-menu))
3604 (defun gdb-memory-format-menu-1 (event)
3605 (interactive "e")
3606 (save-selected-window
3607 (select-window (posn-window (event-start event)))
3608 (let* ((selection (gdb-memory-format-menu event))
3609 (binding (and selection (lookup-key gdb-memory-format-menu
3610 (vector (car selection))))))
3611 (if binding (call-interactively binding)))))
3613 (defmacro def-gdb-memory-unit (name unit-size doc)
3614 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3616 DOC is an optional documentation string."
3617 `(defun ,name () ,(when doc doc)
3618 (interactive)
3619 (customize-set-variable 'gdb-memory-unit ,unit-size)
3620 (gdb-invalidate-memory 'update)))
3622 (def-gdb-memory-unit gdb-memory-unit-giant 8
3623 "Set the unit size to giant words (eight bytes).")
3625 (def-gdb-memory-unit gdb-memory-unit-word 4
3626 "Set the unit size to words (four bytes).")
3628 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3629 "Set the unit size to halfwords (two bytes).")
3631 (def-gdb-memory-unit gdb-memory-unit-byte 1
3632 "Set the unit size to bytes.")
3634 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3635 "Define a function NAME which show new address in memory buffer.
3637 The defined function switches Memory buffer to show address
3638 stored in ADDRESS-VAR variable.
3640 DOC is an optional documentation string."
3641 `(defun ,name
3642 ,(when doc doc)
3643 (interactive)
3644 (let ((gdb-memory-address ,address-var))
3645 (gdb-invalidate-memory))))
3647 (def-gdb-memory-show-page gdb-memory-show-previous-page
3648 gdb-memory-prev-page)
3650 (def-gdb-memory-show-page gdb-memory-show-next-page
3651 gdb-memory-next-page)
3653 (defvar gdb-memory-unit-map
3654 (let ((map (make-sparse-keymap)))
3655 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3656 map)
3657 "Keymap to select units in the header line.")
3659 (defvar gdb-memory-unit-menu
3660 (let ((map (make-sparse-keymap "Unit")))
3661 (define-key map [giantwords]
3662 '(menu-item "Giant words" gdb-memory-unit-giant
3663 :button (:radio . (equal gdb-memory-unit 8))))
3664 (define-key map [words]
3665 '(menu-item "Words" gdb-memory-unit-word
3666 :button (:radio . (equal gdb-memory-unit 4))))
3667 (define-key map [halfwords]
3668 '(menu-item "Halfwords" gdb-memory-unit-halfword
3669 :button (:radio . (equal gdb-memory-unit 2))))
3670 (define-key map [bytes]
3671 '(menu-item "Bytes" gdb-memory-unit-byte
3672 :button (:radio . (equal gdb-memory-unit 1))))
3673 map)
3674 "Menu of units in the header line.")
3676 (defun gdb-memory-unit-menu (event)
3677 (interactive "@e")
3678 (x-popup-menu event gdb-memory-unit-menu))
3680 (defun gdb-memory-unit-menu-1 (event)
3681 (interactive "e")
3682 (save-selected-window
3683 (select-window (posn-window (event-start event)))
3684 (let* ((selection (gdb-memory-unit-menu event))
3685 (binding (and selection (lookup-key gdb-memory-unit-menu
3686 (vector (car selection))))))
3687 (if binding (call-interactively binding)))))
3689 (defvar gdb-memory-font-lock-keywords
3690 '(;; <__function.name+n>
3691 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3692 (1 font-lock-function-name-face)))
3693 "Font lock keywords used in `gdb-memory-mode'.")
3695 (defvar gdb-memory-header
3696 '(:eval
3697 (concat
3698 "Start address["
3699 (propertize "-"
3700 'face font-lock-warning-face
3701 'help-echo "mouse-1: decrement address"
3702 'mouse-face 'mode-line-highlight
3703 'local-map (gdb-make-header-line-mouse-map
3704 'mouse-1
3705 #'gdb-memory-show-previous-page))
3707 (propertize "+"
3708 'face font-lock-warning-face
3709 'help-echo "mouse-1: increment address"
3710 'mouse-face 'mode-line-highlight
3711 'local-map (gdb-make-header-line-mouse-map
3712 'mouse-1
3713 #'gdb-memory-show-next-page))
3714 "]: "
3715 (propertize gdb-memory-address
3716 'face font-lock-warning-face
3717 'help-echo "mouse-1: set start address"
3718 'mouse-face 'mode-line-highlight
3719 'local-map (gdb-make-header-line-mouse-map
3720 'mouse-1
3721 #'gdb-memory-set-address-event))
3722 " Rows: "
3723 (propertize (number-to-string gdb-memory-rows)
3724 'face font-lock-warning-face
3725 'help-echo "mouse-1: set number of columns"
3726 'mouse-face 'mode-line-highlight
3727 'local-map (gdb-make-header-line-mouse-map
3728 'mouse-1
3729 #'gdb-memory-set-rows))
3730 " Columns: "
3731 (propertize (number-to-string gdb-memory-columns)
3732 'face font-lock-warning-face
3733 'help-echo "mouse-1: set number of columns"
3734 'mouse-face 'mode-line-highlight
3735 'local-map (gdb-make-header-line-mouse-map
3736 'mouse-1
3737 #'gdb-memory-set-columns))
3738 " Display Format: "
3739 (propertize gdb-memory-format
3740 'face font-lock-warning-face
3741 'help-echo "mouse-3: select display format"
3742 'mouse-face 'mode-line-highlight
3743 'local-map gdb-memory-format-map)
3744 " Unit Size: "
3745 (propertize (number-to-string gdb-memory-unit)
3746 'face font-lock-warning-face
3747 'help-echo "mouse-3: select unit size"
3748 'mouse-face 'mode-line-highlight
3749 'local-map gdb-memory-unit-map)))
3750 "Header line used in `gdb-memory-mode'.")
3752 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3753 "Major mode for examining memory."
3754 (setq header-line-format gdb-memory-header)
3755 (set (make-local-variable 'font-lock-defaults)
3756 '(gdb-memory-font-lock-keywords))
3757 'gdb-invalidate-memory)
3759 (defun gdb-memory-buffer-name ()
3760 (concat "*memory of " (gdb-get-target-string) "*"))
3762 (defun gdb-display-memory-buffer (&optional thread)
3763 "Display GDB memory contents."
3764 (interactive)
3765 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3767 (defun gdb-frame-memory-buffer ()
3768 "Display memory contents in another frame."
3769 (interactive)
3770 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3771 gdb-display-buffer-other-frame-action))
3774 ;;; Disassembly view
3776 (defun gdb-disassembly-buffer-name ()
3777 (gdb-current-context-buffer-name
3778 (concat "disassembly of " (gdb-get-target-string))))
3780 (defun gdb-display-disassembly-buffer (&optional thread)
3781 "Display GDB disassembly information."
3782 (interactive)
3783 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3785 (def-gdb-preempt-display-buffer
3786 gdb-preemptively-display-disassembly-buffer
3787 'gdb-disassembly-buffer)
3789 (defun gdb-frame-disassembly-buffer (&optional thread)
3790 "Display GDB disassembly information in another frame."
3791 (interactive)
3792 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3793 gdb-display-buffer-other-frame-action))
3795 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3796 (let* ((frame (gdb-current-buffer-frame))
3797 (file (bindat-get-field frame 'fullname))
3798 (line (bindat-get-field frame 'line)))
3799 (if file
3800 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3801 ;; If we're unable to get a file name / line for $PC, simply
3802 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3803 ;; 150 bytes) instructions.
3804 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3805 gdb-disassembly-handler
3806 ;; We update disassembly only after we have actual frame information
3807 ;; about all threads, so no there's `update' signal in this list
3808 '(start update-disassembly))
3810 (def-gdb-auto-update-handler
3811 gdb-disassembly-handler
3812 gdb-disassembly-handler-custom
3815 (gdb-set-buffer-rules
3816 'gdb-disassembly-buffer
3817 'gdb-disassembly-buffer-name
3818 'gdb-disassembly-mode
3819 'gdb-invalidate-disassembly)
3821 (defvar gdb-disassembly-font-lock-keywords
3822 '(;; <__function.name+n>
3823 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3824 (1 font-lock-function-name-face))
3825 ;; 0xNNNNNNNN <__function.name+n>: opcode
3826 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3827 (4 font-lock-keyword-face))
3828 ;; %register(at least i386)
3829 ("%\\sw+" . font-lock-variable-name-face)
3830 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3831 (1 font-lock-comment-face)
3832 (2 font-lock-function-name-face))
3833 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3834 "Font lock keywords used in `gdb-disassembly-mode'.")
3836 (defvar gdb-disassembly-mode-map
3837 ;; TODO
3838 (let ((map (make-sparse-keymap)))
3839 (suppress-keymap map)
3840 (define-key map "q" 'kill-this-buffer)
3841 map))
3843 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3844 "Major mode for GDB disassembly information."
3845 ;; TODO Rename overlay variable for disassembly mode
3846 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3847 (setq fringes-outside-margins t)
3848 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3849 (set (make-local-variable 'font-lock-defaults)
3850 '(gdb-disassembly-font-lock-keywords))
3851 'gdb-invalidate-disassembly)
3853 (defun gdb-disassembly-handler-custom ()
3854 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3855 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3856 (table (make-gdb-table))
3857 (marked-line nil))
3858 (dolist (instr instructions)
3859 (gdb-table-add-row table
3860 (list
3861 (bindat-get-field instr 'address)
3862 (let
3863 ((func-name (bindat-get-field instr 'func-name))
3864 (offset (bindat-get-field instr 'offset)))
3865 (if func-name
3866 (format "<%s+%s>:" func-name offset)
3867 ""))
3868 (bindat-get-field instr 'inst)))
3869 (when (string-equal (bindat-get-field instr 'address)
3870 address)
3871 (progn
3872 (setq marked-line (length (gdb-table-rows table)))
3873 (setq fringe-indicator-alist
3874 (if (string-equal gdb-frame-number "0")
3876 '((overlay-arrow . hollow-right-triangle)))))))
3877 (insert (gdb-table-string table " "))
3878 (gdb-disassembly-place-breakpoints)
3879 ;; Mark current position with overlay arrow and scroll window to
3880 ;; that point
3881 (when marked-line
3882 (let ((window (get-buffer-window (current-buffer) 0)))
3883 (set-window-point window (gdb-mark-line marked-line
3884 gdb-disassembly-position))))
3885 (setq mode-name
3886 (gdb-current-context-mode-name
3887 (concat "Disassembly: "
3888 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3890 (defun gdb-disassembly-place-breakpoints ()
3891 (gdb-remove-breakpoint-icons (point-min) (point-max))
3892 (dolist (breakpoint gdb-breakpoints-list)
3893 (let* ((breakpoint (cdr breakpoint))
3894 (bptno (bindat-get-field breakpoint 'number))
3895 (flag (bindat-get-field breakpoint 'enabled))
3896 (address (bindat-get-field breakpoint 'addr)))
3897 (save-excursion
3898 (goto-char (point-min))
3899 (if (re-search-forward (concat "^" address) nil t)
3900 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3903 (defvar gdb-breakpoints-header
3904 (list
3905 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3906 nil nil mode-line)
3908 (gdb-propertize-header "Threads" gdb-threads-buffer
3909 "mouse-1: select" mode-line-highlight
3910 mode-line-inactive)))
3912 ;;; Breakpoints view
3913 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3914 "Major mode for gdb breakpoints."
3915 (setq header-line-format gdb-breakpoints-header)
3916 'gdb-invalidate-breakpoints)
3918 (defun gdb-toggle-breakpoint ()
3919 "Enable/disable breakpoint at current line of breakpoints buffer."
3920 (interactive)
3921 (save-excursion
3922 (beginning-of-line)
3923 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3924 (if breakpoint
3925 (gud-basic-call
3926 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3927 "-break-disable "
3928 "-break-enable ")
3929 (bindat-get-field breakpoint 'number)))
3930 (error "Not recognized as break/watchpoint line")))))
3932 (defun gdb-delete-breakpoint ()
3933 "Delete the breakpoint at current line of breakpoints buffer."
3934 (interactive)
3935 (save-excursion
3936 (beginning-of-line)
3937 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3938 (if breakpoint
3939 (gud-basic-call (concat "-break-delete "
3940 (bindat-get-field breakpoint 'number)))
3941 (error "Not recognized as break/watchpoint line")))))
3943 (defun gdb-goto-breakpoint (&optional event)
3944 "Go to the location of breakpoint at current line of breakpoints buffer."
3945 (interactive (list last-input-event))
3946 (if event (posn-set-point (event-end event)))
3947 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3948 (let ((window (get-buffer-window gud-comint-buffer)))
3949 (if window (save-selected-window (select-window window))))
3950 (save-excursion
3951 (beginning-of-line)
3952 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3953 (if breakpoint
3954 (let ((bptno (bindat-get-field breakpoint 'number))
3955 (file (bindat-get-field breakpoint 'fullname))
3956 (line (bindat-get-field breakpoint 'line)))
3957 (save-selected-window
3958 (let* ((buffer (find-file-noselect
3959 (if (file-exists-p file) file
3960 (cdr (assoc bptno gdb-location-alist)))))
3961 (window (or (gdb-display-source-buffer buffer)
3962 (display-buffer buffer))))
3963 (setq gdb-source-window window)
3964 (with-current-buffer buffer
3965 (goto-char (point-min))
3966 (forward-line (1- (string-to-number line)))
3967 (set-window-point window (point))))))
3968 (error "Not recognized as break/watchpoint line")))))
3971 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3973 (def-gdb-trigger-and-handler
3974 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3975 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3976 '(start update))
3978 (gdb-set-buffer-rules
3979 'gdb-stack-buffer
3980 'gdb-stack-buffer-name
3981 'gdb-frames-mode
3982 'gdb-invalidate-frames)
3984 (defun gdb-frame-location (frame)
3985 "Return \" of file:line\" or \" of library\" for structure FRAME.
3987 FRAME must have either \"file\" and \"line\" members or \"from\"
3988 member."
3989 (let ((file (bindat-get-field frame 'file))
3990 (line (bindat-get-field frame 'line))
3991 (from (bindat-get-field frame 'from)))
3992 (let ((res (or (and file line (concat file ":" line))
3993 from)))
3994 (if res (concat " of " res) ""))))
3996 (defun gdb-stack-list-frames-custom ()
3997 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3998 (table (make-gdb-table)))
3999 (set-marker gdb-stack-position nil)
4000 (dolist (frame stack)
4001 (gdb-table-add-row table
4002 (list
4003 (bindat-get-field frame 'level)
4004 "in"
4005 (concat
4006 (bindat-get-field frame 'func)
4007 (if gdb-stack-buffer-locations
4008 (gdb-frame-location frame) "")
4009 (if gdb-stack-buffer-addresses
4010 (concat " at " (bindat-get-field frame 'addr)) "")))
4011 `(mouse-face highlight
4012 help-echo "mouse-2, RET: Select frame"
4013 gdb-frame ,frame)))
4014 (insert (gdb-table-string table " ")))
4015 (when (and gdb-frame-number
4016 (gdb-buffer-shows-main-thread-p))
4017 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
4018 gdb-stack-position))
4019 (setq mode-name
4020 (gdb-current-context-mode-name "Frames")))
4022 (defun gdb-stack-buffer-name ()
4023 (gdb-current-context-buffer-name
4024 (concat "stack frames of " (gdb-get-target-string))))
4026 (defun gdb-display-stack-buffer (&optional thread)
4027 "Display GDB backtrace for current stack."
4028 (interactive)
4029 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
4031 (def-gdb-preempt-display-buffer
4032 gdb-preemptively-display-stack-buffer
4033 'gdb-stack-buffer nil t)
4035 (defun gdb-frame-stack-buffer (&optional thread)
4036 "Display GDB backtrace for current stack in another frame."
4037 (interactive)
4038 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
4039 gdb-display-buffer-other-frame-action))
4041 (defvar gdb-frames-mode-map
4042 (let ((map (make-sparse-keymap)))
4043 (suppress-keymap map)
4044 (define-key map "q" 'kill-this-buffer)
4045 (define-key map "\r" 'gdb-select-frame)
4046 (define-key map [mouse-2] 'gdb-select-frame)
4047 (define-key map [follow-link] 'mouse-face)
4048 map))
4050 (defvar gdb-frames-font-lock-keywords
4051 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
4052 "Font lock keywords used in `gdb-frames-mode'.")
4054 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
4055 "Major mode for gdb call stack."
4056 (setq gdb-stack-position (make-marker))
4057 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
4058 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
4059 (set (make-local-variable 'font-lock-defaults)
4060 '(gdb-frames-font-lock-keywords))
4061 'gdb-invalidate-frames)
4063 (defun gdb-select-frame (&optional event)
4064 "Select the frame and display the relevant source."
4065 (interactive (list last-input-event))
4066 (if event (posn-set-point (event-end event)))
4067 (let ((frame (get-text-property (point) 'gdb-frame)))
4068 (if frame
4069 (if (gdb-buffer-shows-main-thread-p)
4070 (let ((new-level (bindat-get-field frame 'level)))
4071 (setq gdb-frame-number new-level)
4072 (gdb-input (concat "-stack-select-frame " new-level)
4073 'ignore)
4074 (gdb-update))
4075 (error "Could not select frame for non-current thread"))
4076 (error "Not recognized as frame line"))))
4079 ;; Locals buffer.
4080 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
4081 (def-gdb-trigger-and-handler
4082 gdb-invalidate-locals
4083 (concat (gdb-current-context-command "-stack-list-locals")
4084 " --simple-values")
4085 gdb-locals-handler gdb-locals-handler-custom
4086 '(start update))
4088 (gdb-set-buffer-rules
4089 'gdb-locals-buffer
4090 'gdb-locals-buffer-name
4091 'gdb-locals-mode
4092 'gdb-invalidate-locals)
4094 (defvar gdb-locals-watch-map
4095 (let ((map (make-sparse-keymap)))
4096 (suppress-keymap map)
4097 (define-key map "\r" 'gud-watch)
4098 (define-key map [mouse-2] 'gud-watch)
4099 map)
4100 "Keymap to create watch expression of a complex data type local variable.")
4102 (defvar gdb-edit-locals-map-1
4103 (let ((map (make-sparse-keymap)))
4104 (suppress-keymap map)
4105 (define-key map "\r" 'gdb-edit-locals-value)
4106 (define-key map [mouse-2] 'gdb-edit-locals-value)
4107 map)
4108 "Keymap to edit value of a simple data type local variable.")
4110 (defun gdb-edit-locals-value (&optional event)
4111 "Assign a value to a variable displayed in the locals buffer."
4112 (interactive (list last-input-event))
4113 (save-excursion
4114 (if event (posn-set-point (event-end event)))
4115 (beginning-of-line)
4116 (let* ((var (bindat-get-field
4117 (get-text-property (point) 'gdb-local-variable) 'name))
4118 (value (read-string (format "New value (%s): " var))))
4119 (gud-basic-call
4120 (concat "-gdb-set variable " var " = " value)))))
4122 ;; Don't display values of arrays or structures.
4123 ;; These can be expanded using gud-watch.
4124 (defun gdb-locals-handler-custom ()
4125 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4126 (table (make-gdb-table)))
4127 (dolist (local locals-list)
4128 (let ((name (bindat-get-field local 'name))
4129 (value (bindat-get-field local 'value))
4130 (type (bindat-get-field local 'type)))
4131 (when (not value)
4132 (setq value "<complex data type>"))
4133 (if (or (not value)
4134 (string-match "\\0x" value))
4135 (add-text-properties 0 (length name)
4136 `(mouse-face highlight
4137 help-echo "mouse-2: create watch expression"
4138 local-map ,gdb-locals-watch-map)
4139 name)
4140 (add-text-properties 0 (length value)
4141 `(mouse-face highlight
4142 help-echo "mouse-2: edit value"
4143 local-map ,gdb-edit-locals-map-1)
4144 value))
4145 (gdb-table-add-row
4146 table
4147 (list
4148 (propertize type 'font-lock-face font-lock-type-face)
4149 (propertize name 'font-lock-face font-lock-variable-name-face)
4150 value)
4151 `(gdb-local-variable ,local))))
4152 (insert (gdb-table-string table " "))
4153 (setq mode-name
4154 (gdb-current-context-mode-name
4155 (concat "Locals: "
4156 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4158 (defvar gdb-locals-header
4159 (list
4160 (gdb-propertize-header "Locals" gdb-locals-buffer
4161 nil nil mode-line)
4163 (gdb-propertize-header "Registers" gdb-registers-buffer
4164 "mouse-1: select" mode-line-highlight
4165 mode-line-inactive)))
4167 (defvar gdb-locals-mode-map
4168 (let ((map (make-sparse-keymap)))
4169 (suppress-keymap map)
4170 (define-key map "q" 'kill-this-buffer)
4171 (define-key map "\t" (lambda ()
4172 (interactive)
4173 (gdb-set-window-buffer
4174 (gdb-get-buffer-create
4175 'gdb-registers-buffer
4176 gdb-thread-number) t)))
4177 map))
4179 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4180 "Major mode for gdb locals."
4181 (setq header-line-format gdb-locals-header)
4182 'gdb-invalidate-locals)
4184 (defun gdb-locals-buffer-name ()
4185 (gdb-current-context-buffer-name
4186 (concat "locals of " (gdb-get-target-string))))
4188 (defun gdb-display-locals-buffer (&optional thread)
4189 "Display the local variables of current GDB stack."
4190 (interactive)
4191 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4193 (def-gdb-preempt-display-buffer
4194 gdb-preemptively-display-locals-buffer
4195 'gdb-locals-buffer nil t)
4197 (defun gdb-frame-locals-buffer (&optional thread)
4198 "Display the local variables of the current GDB stack in another frame."
4199 (interactive)
4200 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4201 gdb-display-buffer-other-frame-action))
4204 ;; Registers buffer.
4206 (def-gdb-trigger-and-handler
4207 gdb-invalidate-registers
4208 (concat (gdb-current-context-command "-data-list-register-values") " x")
4209 gdb-registers-handler
4210 gdb-registers-handler-custom
4211 '(start update))
4213 (gdb-set-buffer-rules
4214 'gdb-registers-buffer
4215 'gdb-registers-buffer-name
4216 'gdb-registers-mode
4217 'gdb-invalidate-registers)
4219 (defun gdb-registers-handler-custom ()
4220 (when gdb-register-names
4221 (let ((register-values
4222 (bindat-get-field (gdb-json-partial-output) 'register-values))
4223 (table (make-gdb-table)))
4224 (dolist (register register-values)
4225 (let* ((register-number (bindat-get-field register 'number))
4226 (value (bindat-get-field register 'value))
4227 (register-name (nth (string-to-number register-number)
4228 gdb-register-names)))
4229 (gdb-table-add-row
4230 table
4231 (list
4232 (propertize register-name
4233 'font-lock-face font-lock-variable-name-face)
4234 (if (member register-number gdb-changed-registers)
4235 (propertize value 'font-lock-face font-lock-warning-face)
4236 value))
4237 `(mouse-face highlight
4238 help-echo "mouse-2: edit value"
4239 gdb-register-name ,register-name))))
4240 (insert (gdb-table-string table " ")))
4241 (setq mode-name
4242 (gdb-current-context-mode-name "Registers"))))
4244 (defun gdb-edit-register-value (&optional event)
4245 "Assign a value to a register displayed in the registers buffer."
4246 (interactive (list last-input-event))
4247 (save-excursion
4248 (if event (posn-set-point (event-end event)))
4249 (beginning-of-line)
4250 (let* ((var (bindat-get-field
4251 (get-text-property (point) 'gdb-register-name)))
4252 (value (read-string (format "New value (%s): " var))))
4253 (gud-basic-call
4254 (concat "-gdb-set variable $" var " = " value)))))
4256 (defvar gdb-registers-mode-map
4257 (let ((map (make-sparse-keymap)))
4258 (suppress-keymap map)
4259 (define-key map "\r" 'gdb-edit-register-value)
4260 (define-key map [mouse-2] 'gdb-edit-register-value)
4261 (define-key map "q" 'kill-this-buffer)
4262 (define-key map "\t" (lambda ()
4263 (interactive)
4264 (gdb-set-window-buffer
4265 (gdb-get-buffer-create
4266 'gdb-locals-buffer
4267 gdb-thread-number) t)))
4268 map))
4270 (defvar gdb-registers-header
4271 (list
4272 (gdb-propertize-header "Locals" gdb-locals-buffer
4273 "mouse-1: select" mode-line-highlight
4274 mode-line-inactive)
4276 (gdb-propertize-header "Registers" gdb-registers-buffer
4277 nil nil mode-line)))
4279 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4280 "Major mode for gdb registers."
4281 (setq header-line-format gdb-registers-header)
4282 'gdb-invalidate-registers)
4284 (defun gdb-registers-buffer-name ()
4285 (gdb-current-context-buffer-name
4286 (concat "registers of " (gdb-get-target-string))))
4288 (defun gdb-display-registers-buffer (&optional thread)
4289 "Display GDB register contents."
4290 (interactive)
4291 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4293 (def-gdb-preempt-display-buffer
4294 gdb-preemptively-display-registers-buffer
4295 'gdb-registers-buffer nil t)
4297 (defun gdb-frame-registers-buffer (&optional thread)
4298 "Display GDB register contents in another frame."
4299 (interactive)
4300 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4301 gdb-display-buffer-other-frame-action))
4303 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4304 (defun gdb-get-changed-registers ()
4305 (when (gdb-get-buffer 'gdb-registers-buffer)
4306 (gdb-input "-data-list-changed-registers"
4307 'gdb-changed-registers-handler
4308 'gdb-get-changed-registers)))
4310 (defun gdb-changed-registers-handler ()
4311 (setq gdb-changed-registers nil)
4312 (dolist (register-number
4313 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4314 (push register-number gdb-changed-registers)))
4316 (defun gdb-register-names-handler ()
4317 ;; Don't use pending triggers because this handler is called
4318 ;; only once (in gdb-init-1)
4319 (setq gdb-register-names nil)
4320 (dolist (register-name
4321 (bindat-get-field (gdb-json-partial-output) 'register-names))
4322 (push register-name gdb-register-names))
4323 (setq gdb-register-names (reverse gdb-register-names)))
4326 (defun gdb-get-source-file-list ()
4327 "Create list of source files for current GDB session.
4328 If buffers already exist for any of these files, `gud-minor-mode'
4329 is set in them."
4330 (goto-char (point-min))
4331 (while (re-search-forward gdb-source-file-regexp nil t)
4332 (push (read (match-string 1)) gdb-source-file-list))
4333 (dolist (buffer (buffer-list))
4334 (with-current-buffer buffer
4335 (when (member buffer-file-name gdb-source-file-list)
4336 (gdb-init-buffer)))))
4338 (defun gdb-get-main-selected-frame ()
4339 "Trigger for `gdb-frame-handler' which uses main current thread.
4340 Called from `gdb-update'."
4341 (gdb-input (gdb-current-context-command "-stack-info-frame")
4342 'gdb-frame-handler
4343 'gdb-get-main-selected-frame))
4345 (defun gdb-frame-handler ()
4346 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4347 overlay arrow in source buffer."
4348 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4349 (when frame
4350 (setq gdb-selected-frame (bindat-get-field frame 'func))
4351 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4352 (setq gdb-frame-number (bindat-get-field frame 'level))
4353 (setq gdb-frame-address (bindat-get-field frame 'addr))
4354 (let ((line (bindat-get-field frame 'line)))
4355 (setq gdb-selected-line (and line (string-to-number line)))
4356 (when (and gdb-selected-file gdb-selected-line)
4357 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4358 (gud-display-frame)))
4359 (if gud-overlay-arrow-position
4360 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4361 (position (marker-position gud-overlay-arrow-position)))
4362 (when buffer
4363 (with-current-buffer buffer
4364 (setq fringe-indicator-alist
4365 (if (string-equal gdb-frame-number "0")
4367 '((overlay-arrow . hollow-right-triangle))))
4368 (setq gud-overlay-arrow-position (make-marker))
4369 (set-marker gud-overlay-arrow-position position))))))))
4371 (defconst gdb-prompt-name-regexp
4372 (concat "value=\\(" gdb--string-regexp "\\)"))
4374 (defun gdb-get-prompt ()
4375 "Find prompt for GDB session."
4376 (goto-char (point-min))
4377 (setq gdb-prompt-name nil)
4378 (re-search-forward gdb-prompt-name-regexp nil t)
4379 (setq gdb-prompt-name (read (match-string 1)))
4380 ;; Insert first prompt.
4381 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4383 ;;;; Window management
4384 (defun gdb-display-buffer (buf)
4385 "Show buffer BUF, and make that window dedicated."
4386 (let ((window (display-buffer buf)))
4387 (set-window-dedicated-p window t)
4388 window))
4390 ;; (let ((answer (get-buffer-window buf 0)))
4391 ;; (if answer
4392 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4393 ;; (let ((window (get-lru-window)))
4394 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4395 ;; 'gdbmi)
4396 ;; (let ((largest (get-largest-window)))
4397 ;; (setq answer (split-window largest))
4398 ;; (set-window-buffer answer buf)
4399 ;; (set-window-dedicated-p answer t)
4400 ;; answer)
4401 ;; (set-window-buffer window buf)
4402 ;; window)))))
4405 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4406 "Find window displaying a buffer with the same
4407 `gdb-buffer-type' as BUF and show BUF there. If no such window
4408 exists, just call `gdb-display-buffer' for BUF. If the window
4409 found is already dedicated, split window according to
4410 SPLIT-HORIZONTAL and show BUF in the new window."
4411 (if buf
4412 (when (not (get-buffer-window buf))
4413 (let* ((buf-type (gdb-buffer-type buf))
4414 (existing-window
4415 (get-window-with-predicate
4416 #'(lambda (w)
4417 (and (eq buf-type
4418 (gdb-buffer-type (window-buffer w)))
4419 (not (window-dedicated-p w)))))))
4420 (if existing-window
4421 (set-window-buffer existing-window buf)
4422 (let ((dedicated-window
4423 (get-window-with-predicate
4424 #'(lambda (w)
4425 (eq buf-type
4426 (gdb-buffer-type (window-buffer w)))))))
4427 (if dedicated-window
4428 (set-window-buffer
4429 (split-window dedicated-window nil split-horizontal) buf)
4430 (gdb-display-buffer buf))))))
4431 (error "Null buffer")))
4433 ;;; Shared keymap initialization:
4435 (let ((menu (make-sparse-keymap "GDB-Windows")))
4436 (define-key gud-menu-map [displays]
4437 `(menu-item "GDB-Windows" ,menu
4438 :visible (eq gud-minor-mode 'gdbmi)))
4439 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4440 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4441 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4442 (define-key menu [disassembly]
4443 '("Disassembly" . gdb-display-disassembly-buffer))
4444 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4445 (define-key menu [inferior]
4446 '("IO" . gdb-display-io-buffer))
4447 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4448 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4449 (define-key menu [breakpoints]
4450 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4452 (let ((menu (make-sparse-keymap "GDB-Frames")))
4453 (define-key gud-menu-map [frames]
4454 `(menu-item "GDB-Frames" ,menu
4455 :visible (eq gud-minor-mode 'gdbmi)))
4456 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4457 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4458 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4459 (define-key menu [disassembly]
4460 '("Disassembly" . gdb-frame-disassembly-buffer))
4461 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4462 (define-key menu [inferior]
4463 '("IO" . gdb-frame-io-buffer))
4464 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4465 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4466 (define-key menu [breakpoints]
4467 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4469 (let ((menu (make-sparse-keymap "GDB-MI")))
4470 (define-key menu [gdb-customize]
4471 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4472 :help "Customize Gdb Graphical Mode options."))
4473 (define-key menu [gdb-many-windows]
4474 '(menu-item "Display Other Windows" gdb-many-windows
4475 :help "Toggle display of locals, stack and breakpoint information"
4476 :button (:toggle . gdb-many-windows)))
4477 (define-key menu [gdb-restore-windows]
4478 '(menu-item "Restore Window Layout" gdb-restore-windows
4479 :help "Restore standard layout for debug session."))
4480 (define-key menu [sep1]
4481 '(menu-item "--"))
4482 (define-key menu [all-threads]
4483 '(menu-item "GUD controls all threads"
4484 (lambda ()
4485 (interactive)
4486 (setq gdb-gud-control-all-threads t))
4487 :help "GUD start/stop commands apply to all threads"
4488 :button (:radio . gdb-gud-control-all-threads)))
4489 (define-key menu [current-thread]
4490 '(menu-item "GUD controls current thread"
4491 (lambda ()
4492 (interactive)
4493 (setq gdb-gud-control-all-threads nil))
4494 :help "GUD start/stop commands apply to current thread only"
4495 :button (:radio . (not gdb-gud-control-all-threads))))
4496 (define-key menu [sep2]
4497 '(menu-item "--"))
4498 (define-key menu [gdb-customize-reasons]
4499 '(menu-item "Customize switching..."
4500 (lambda ()
4501 (interactive)
4502 (customize-option 'gdb-switch-reasons))))
4503 (define-key menu [gdb-switch-when-another-stopped]
4504 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4505 gdb-switch-when-another-stopped
4506 "Automatically switch to stopped thread"
4507 "GDB thread switching %s"
4508 "Switch to stopped thread"))
4509 (define-key gud-menu-map [mi]
4510 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4512 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4513 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4514 ;; tool-bar-local-item-from-menu here because it appends new buttons
4515 ;; to toolbar from right to left while we want our A/T throttle to
4516 ;; show up right before Run button.
4517 (define-key-after gud-tool-bar-map [all-threads]
4518 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4519 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4520 :visible (and (eq gud-minor-mode 'gdbmi)
4521 gdb-non-stop
4522 (not gdb-gud-control-all-threads)))
4523 'run)
4525 (define-key-after gud-tool-bar-map [current-thread]
4526 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4527 :image (find-image '((:type xpm :file "gud/all.xpm")))
4528 :visible (and (eq gud-minor-mode 'gdbmi)
4529 gdb-non-stop
4530 gdb-gud-control-all-threads))
4531 'all-threads)
4533 (defun gdb-frame-gdb-buffer ()
4534 "Display GUD buffer in another frame."
4535 (interactive)
4536 (display-buffer-other-frame gud-comint-buffer))
4538 (defun gdb-display-gdb-buffer ()
4539 "Display GUD buffer."
4540 (interactive)
4541 (pop-to-buffer gud-comint-buffer nil 0))
4543 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4544 "Set buffer of selected window to NAME and dedicate window.
4546 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4547 window is dedicated."
4548 (unless window (setq window (selected-window)))
4549 (when ignore-dedicated
4550 (set-window-dedicated-p window nil))
4551 (set-window-buffer window (get-buffer name))
4552 (set-window-dedicated-p window t))
4554 (defun gdb-setup-windows ()
4555 "Layout the window pattern for option `gdb-many-windows'."
4556 (gdb-get-buffer-create 'gdb-locals-buffer)
4557 (gdb-get-buffer-create 'gdb-stack-buffer)
4558 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4559 (set-window-dedicated-p (selected-window) nil)
4560 (switch-to-buffer gud-comint-buffer)
4561 (delete-other-windows)
4562 (let ((win0 (selected-window))
4563 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4564 (win2 (split-window nil ( / (window-height) 3)))
4565 (win3 (split-window-right)))
4566 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4567 (select-window win2)
4568 (set-window-buffer
4569 win2
4570 (if gud-last-last-frame
4571 (gud-find-file (car gud-last-last-frame))
4572 (if gdb-main-file
4573 (gud-find-file gdb-main-file)
4574 ;; Put buffer list in window if we
4575 ;; can't find a source file.
4576 (list-buffers-noselect))))
4577 (setq gdb-source-window (selected-window))
4578 (let ((win4 (split-window-right)))
4579 (gdb-set-window-buffer
4580 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4581 (select-window win1)
4582 (gdb-set-window-buffer (gdb-stack-buffer-name))
4583 (let ((win5 (split-window-right)))
4584 (gdb-set-window-buffer (if gdb-show-threads-by-default
4585 (gdb-threads-buffer-name)
4586 (gdb-breakpoints-buffer-name))
4587 nil win5))
4588 (select-window win0)))
4590 (define-minor-mode gdb-many-windows
4591 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4592 In this case it starts with two windows: one displaying the GUD
4593 buffer and the other with the source file with the main routine
4594 of the debugged program. Non-nil means display the layout shown for
4595 `gdb'."
4596 :global t
4597 :group 'gdb
4598 :version "22.1"
4599 (if (and gud-comint-buffer
4600 (buffer-name gud-comint-buffer))
4601 (ignore-errors
4602 (gdb-restore-windows))))
4604 (defun gdb-restore-windows ()
4605 "Restore the basic arrangement of windows used by gdb.
4606 This arrangement depends on the value of option `gdb-many-windows'."
4607 (interactive)
4608 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4609 (delete-other-windows)
4610 (if gdb-many-windows
4611 (gdb-setup-windows)
4612 (when (or gud-last-last-frame gdb-show-main)
4613 (let ((win (split-window)))
4614 (set-window-buffer
4616 (if gud-last-last-frame
4617 (gud-find-file (car gud-last-last-frame))
4618 (gud-find-file gdb-main-file)))
4619 (setq gdb-source-window win)))))
4621 ;; Called from `gud-sentinel' in gud.el:
4622 (defun gdb-reset ()
4623 "Exit a debugging session cleanly.
4624 Kills the gdb buffers, and resets variables and the source buffers."
4625 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4626 ;; process. This pty must be deleted explicitly.
4627 (let ((pty (get-process "gdb-inferior")))
4628 (if pty (delete-process pty)))
4629 ;; Find gdb-mi buffers and kill them.
4630 (dolist (buffer (buffer-list))
4631 (unless (eq buffer gud-comint-buffer)
4632 (with-current-buffer buffer
4633 (if (eq gud-minor-mode 'gdbmi)
4634 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4635 (kill-buffer nil)
4636 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4637 (setq gud-minor-mode nil)
4638 (kill-local-variable 'tool-bar-map)
4639 (kill-local-variable 'gdb-define-alist))))))
4640 (setq gdb-disassembly-position nil)
4641 (setq overlay-arrow-variable-list
4642 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4643 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4644 (setq gdb-stack-position nil)
4645 (setq overlay-arrow-variable-list
4646 (delq 'gdb-stack-position overlay-arrow-variable-list))
4647 (setq gdb-thread-position nil)
4648 (setq overlay-arrow-variable-list
4649 (delq 'gdb-thread-position overlay-arrow-variable-list))
4650 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4651 (setq gud-running nil)
4652 (setq gdb-active-process nil)
4653 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4655 (defun gdb-get-source-file ()
4656 "Find the source file where the program starts and display it with related
4657 buffers, if required."
4658 (goto-char (point-min))
4659 (if (re-search-forward gdb-source-file-regexp nil t)
4660 (setq gdb-main-file (read (match-string 1))))
4661 (if gdb-many-windows
4662 (gdb-setup-windows)
4663 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4664 (and gdb-show-main
4665 gdb-main-file
4666 (display-buffer (gud-find-file gdb-main-file))))
4667 (gdb-force-mode-line-update
4668 (propertize "ready" 'face font-lock-variable-name-face)))
4670 ;;from put-image
4671 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4672 "Put string PUTSTRING in front of POS in the current buffer.
4673 PUTSTRING is displayed by putting an overlay into the current buffer with a
4674 `before-string' string that has a `display' property whose value is
4675 PUTSTRING."
4676 (let ((string (make-string 1 ?x))
4677 (buffer (current-buffer)))
4678 (setq putstring (copy-sequence putstring))
4679 (let ((overlay (make-overlay pos pos buffer))
4680 (prop (or dprop
4681 (list (list 'margin 'left-margin) putstring))))
4682 (put-text-property 0 1 'display prop string)
4683 (if sprops
4684 (add-text-properties 0 1 sprops string))
4685 (overlay-put overlay 'put-break t)
4686 (overlay-put overlay 'before-string string))))
4688 ;;from remove-images
4689 (defun gdb-remove-strings (start end &optional buffer)
4690 "Remove strings between START and END in BUFFER.
4691 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4692 BUFFER nil or omitted means use the current buffer."
4693 (unless buffer
4694 (setq buffer (current-buffer)))
4695 (dolist (overlay (overlays-in start end))
4696 (when (overlay-get overlay 'put-break)
4697 (delete-overlay overlay))))
4699 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4700 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4701 (start (- (car posns) 1))
4702 (end (+ (cdr posns) 1))
4703 (putstring (if enabled "B" "b"))
4704 (source-window (get-buffer-window (current-buffer) 0)))
4705 (add-text-properties
4706 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4707 putstring)
4708 (if enabled
4709 (add-text-properties
4710 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4711 (add-text-properties
4712 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4713 (gdb-remove-breakpoint-icons start end)
4714 (if (display-images-p)
4715 (if (>= (or left-fringe-width
4716 (if source-window (car (window-fringes source-window)))
4717 gdb-buffer-fringe-width) 8)
4718 (gdb-put-string
4719 nil (1+ start)
4720 `(left-fringe breakpoint
4721 ,(if enabled
4722 'breakpoint-enabled
4723 'breakpoint-disabled))
4724 'gdb-bptno bptno
4725 'gdb-enabled enabled)
4726 (when (< left-margin-width 2)
4727 (save-current-buffer
4728 (setq left-margin-width 2)
4729 (if source-window
4730 (set-window-margins
4731 source-window
4732 left-margin-width right-margin-width))))
4733 (put-image
4734 (if enabled
4735 (or breakpoint-enabled-icon
4736 (setq breakpoint-enabled-icon
4737 (find-image `((:type xpm :data
4738 ,breakpoint-xpm-data
4739 :ascent 100 :pointer hand)
4740 (:type pbm :data
4741 ,breakpoint-enabled-pbm-data
4742 :ascent 100 :pointer hand)))))
4743 (or breakpoint-disabled-icon
4744 (setq breakpoint-disabled-icon
4745 (find-image `((:type xpm :data
4746 ,breakpoint-xpm-data
4747 :conversion disabled
4748 :ascent 100 :pointer hand)
4749 (:type pbm :data
4750 ,breakpoint-disabled-pbm-data
4751 :ascent 100 :pointer hand))))))
4752 (+ start 1)
4753 putstring
4754 'left-margin))
4755 (when (< left-margin-width 2)
4756 (save-current-buffer
4757 (setq left-margin-width 2)
4758 (let ((window (get-buffer-window (current-buffer) 0)))
4759 (if window
4760 (set-window-margins
4761 window left-margin-width right-margin-width)))))
4762 (gdb-put-string
4763 (propertize putstring
4764 'face (if enabled
4765 'breakpoint-enabled 'breakpoint-disabled))
4766 (1+ start)))))
4768 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4769 (gdb-remove-strings start end)
4770 (if (display-images-p)
4771 (remove-images start end))
4772 (when remove-margin
4773 (setq left-margin-width 0)
4774 (let ((window (get-buffer-window (current-buffer) 0)))
4775 (if window
4776 (set-window-margins
4777 window left-margin-width right-margin-width)))))
4780 ;;; Functions for inline completion.
4782 (defvar gud-gdb-fetch-lines-in-progress)
4783 (defvar gud-gdb-fetch-lines-string)
4784 (defvar gud-gdb-fetch-lines-break)
4785 (defvar gud-gdb-fetched-lines)
4787 (defun gud-gdbmi-completions (context command)
4788 "Completion table for GDB/MI commands.
4789 COMMAND is the prefix for which we seek completion.
4790 CONTEXT is the text before COMMAND on the line."
4791 (let ((gud-gdb-fetch-lines-in-progress t)
4792 (gud-gdb-fetch-lines-string nil)
4793 (gud-gdb-fetch-lines-break (length context))
4794 (gud-gdb-fetched-lines nil)
4795 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4796 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4797 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4798 (gdb-input (concat "complete " context command)
4799 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4800 (while gud-gdb-fetch-lines-in-progress
4801 (accept-process-output (get-buffer-process gud-comint-buffer))))
4802 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4804 (defun gud-gdbmi-fetch-lines-filter (string)
4805 "Custom filter function for `gud-gdbmi-completions'."
4806 (setq string (concat gud-gdb-fetch-lines-string
4807 (gud-gdbmi-marker-filter string)))
4808 (while (string-match "\n" string)
4809 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4810 gud-gdb-fetched-lines)
4811 (setq string (substring string (match-end 0))))
4814 (provide 'gdb-mi)
4816 ;;; gdb-mi.el ends here