file-attribute-collect: New defun
[emacs.git] / lisp / progmodes / gdb-mi.el
blob4adcfe7ff16c9d5e4cf15018e767715d5b4a55a3
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."
2665 (save-excursion
2666 (goto-char (point-min))
2667 (when fix-key
2668 (save-excursion
2669 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2670 (replace-match "" nil nil nil 1))))
2671 (when fix-list
2672 (save-excursion
2673 ;; Find positions of braces which enclose broken list
2674 (while (re-search-forward (concat fix-list "={\"") nil t)
2675 (let ((p1 (goto-char (- (point) 2)))
2676 (p2 (progn (forward-sexp)
2677 (1- (point)))))
2678 ;; Replace braces with brackets
2679 (save-excursion
2680 (goto-char p1)
2681 (delete-char 1)
2682 (insert "[")
2683 (goto-char p2)
2684 (delete-char 1)
2685 (insert "]"))))))
2686 (goto-char (point-min))
2687 (insert "{")
2688 (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
2689 gdb--string-regexp "\\)")))
2690 (while (re-search-forward re nil t)
2691 (replace-match "\"\\1\":\\2" nil nil)))
2692 (goto-char (point-max))
2693 (insert "}")))
2695 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2696 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2698 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2699 (gdb-jsonify-buffer fix-key fix-list)
2700 (save-excursion
2701 (goto-char (point-min))
2702 (let ((json-array-type 'list))
2703 (json-read))))
2705 (defun gdb-json-string (string &optional fix-key fix-list)
2706 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2708 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2709 (with-temp-buffer
2710 (insert string)
2711 (gdb-json-read-buffer fix-key fix-list)))
2713 (defun gdb-json-partial-output (&optional fix-key fix-list)
2714 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2716 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2717 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2718 (gdb-json-read-buffer fix-key fix-list)))
2720 (defun gdb-line-posns (line)
2721 "Return a pair of LINE beginning and end positions."
2722 (let ((offset (1+ (- line (line-number-at-pos)))))
2723 (cons
2724 (line-beginning-position offset)
2725 (line-end-position offset))))
2727 (defmacro gdb-mark-line (line variable)
2728 "Set VARIABLE marker to point at beginning of LINE.
2730 If current window has no fringes, inverse colors on LINE.
2732 Return position where LINE begins."
2733 `(save-excursion
2734 (let* ((posns (gdb-line-posns ,line))
2735 (start-posn (car posns))
2736 (end-posn (cdr posns)))
2737 (set-marker ,variable (copy-marker start-posn))
2738 (when (not (> (car (window-fringes)) 0))
2739 (put-text-property start-posn end-posn
2740 'font-lock-face '(:inverse-video t)))
2741 start-posn)))
2743 (defun gdb-pad-string (string padding)
2744 (format (concat "%" (number-to-string padding) "s") string))
2746 ;; gdb-table struct is a way to programmatically construct simple
2747 ;; tables. It help to reliably align columns of data in GDB buffers
2748 ;; and provides
2749 (cl-defstruct gdb-table
2750 (column-sizes nil)
2751 (rows nil)
2752 (row-properties nil)
2753 (right-align nil))
2755 (defun gdb-table-add-row (table row &optional properties)
2756 "Add ROW of string to TABLE and recalculate column sizes.
2758 When non-nil, PROPERTIES will be added to the whole row when
2759 calling `gdb-table-string'."
2760 (let ((rows (gdb-table-rows table))
2761 (row-properties (gdb-table-row-properties table))
2762 (column-sizes (gdb-table-column-sizes table))
2763 (right-align (gdb-table-right-align table)))
2764 (when (not column-sizes)
2765 (setf (gdb-table-column-sizes table)
2766 (make-list (length row) 0)))
2767 (setf (gdb-table-rows table)
2768 (append rows (list row)))
2769 (setf (gdb-table-row-properties table)
2770 (append row-properties (list properties)))
2771 (setf (gdb-table-column-sizes table)
2772 (cl-mapcar (lambda (x s)
2773 (let ((new-x
2774 (max (abs x) (string-width (or s "")))))
2775 (if right-align new-x (- new-x))))
2776 (gdb-table-column-sizes table)
2777 row))
2778 ;; Avoid trailing whitespace at eol
2779 (if (not (gdb-table-right-align table))
2780 (setcar (last (gdb-table-column-sizes table)) 0))))
2782 (defun gdb-table-string (table &optional sep)
2783 "Return TABLE as a string with columns separated with SEP."
2784 (let ((column-sizes (gdb-table-column-sizes table)))
2785 (mapconcat
2786 'identity
2787 (cl-mapcar
2788 (lambda (row properties)
2789 (apply 'propertize
2790 (mapconcat 'identity
2791 (cl-mapcar (lambda (s x) (gdb-pad-string s x))
2792 row column-sizes)
2793 sep)
2794 properties))
2795 (gdb-table-rows table)
2796 (gdb-table-row-properties table))
2797 "\n")))
2799 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2800 (defun gdb-get-many-fields (struct &rest fields)
2801 "Return a list of FIELDS values from STRUCT."
2802 (let ((values))
2803 (dolist (field fields)
2804 (push (bindat-get-field struct field) values))
2805 (nreverse values)))
2807 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2808 handler-name
2809 &optional signal-list)
2810 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2811 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2812 buffer with `gdb-bind-function-to-buffer'.
2814 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2815 defined trigger is called with an argument from SIGNAL-LIST. It's
2816 not recommended to define triggers with empty SIGNAL-LIST.
2817 Normally triggers should respond at least to the `update' signal.
2819 Normally the trigger defined by this command must be called from
2820 the buffer where HANDLER-NAME must work. This should be done so
2821 that buffer-local thread number may be used in GDB-COMMAND (by
2822 calling `gdb-current-context-command').
2823 `gdb-bind-function-to-buffer' is used to achieve this, see
2824 `gdb-get-buffer-create'.
2826 Triggers defined by this command are meant to be used as a
2827 trigger argument when describing buffer types with
2828 `gdb-set-buffer-rules'."
2829 `(defun ,trigger-name (&optional signal)
2830 (when
2831 (or (not ,signal-list)
2832 (memq signal ,signal-list))
2833 (gdb-input ,gdb-command
2834 (gdb-bind-function-to-buffer ',handler-name (current-buffer))
2835 (cons (current-buffer) ',trigger-name)))))
2837 ;; Used by disassembly buffer only, the rest use
2838 ;; def-gdb-trigger-and-handler
2839 (defmacro def-gdb-auto-update-handler (handler-name custom-defun
2840 &optional nopreserve)
2841 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2843 Handlers are normally called from the buffers they put output in.
2845 Erase current buffer and evaluate CUSTOM-DEFUN.
2846 Then call `gdb-update-buffer-name'.
2848 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2849 `(defun ,handler-name ()
2850 (let* ((inhibit-read-only t)
2851 ,@(unless nopreserve
2852 '((window (get-buffer-window (current-buffer) 0))
2853 (start (window-start window))
2854 (p (window-point window)))))
2855 (erase-buffer)
2856 (,custom-defun)
2857 (gdb-update-buffer-name)
2858 ,@(when (not nopreserve)
2859 '((set-window-start window start)
2860 (set-window-point window p))))))
2862 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2863 handler-name custom-defun
2864 &optional signal-list)
2865 "Define trigger and handler.
2867 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2868 See `def-gdb-auto-update-trigger'.
2870 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2871 See `def-gdb-auto-update-handler'."
2872 `(progn
2873 (def-gdb-auto-update-trigger ,trigger-name
2874 ,gdb-command
2875 ,handler-name ,signal-list)
2876 (def-gdb-auto-update-handler ,handler-name
2877 ,custom-defun)))
2881 ;; Breakpoint buffer : This displays the output of `-break-list'.
2882 (def-gdb-trigger-and-handler
2883 gdb-invalidate-breakpoints "-break-list"
2884 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2885 '(start update))
2887 (gdb-set-buffer-rules
2888 'gdb-breakpoints-buffer
2889 'gdb-breakpoints-buffer-name
2890 'gdb-breakpoints-mode
2891 'gdb-invalidate-breakpoints)
2893 (defun gdb-breakpoints-list-handler-custom ()
2894 (let ((breakpoints-list (bindat-get-field
2895 (gdb-json-partial-output "bkpt" "script")
2896 'BreakpointTable 'body))
2897 (table (make-gdb-table)))
2898 (setq gdb-breakpoints-list nil)
2899 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2900 (dolist (breakpoint breakpoints-list)
2901 (add-to-list 'gdb-breakpoints-list
2902 (cons (bindat-get-field breakpoint 'number)
2903 breakpoint))
2904 (let ((at (bindat-get-field breakpoint 'at))
2905 (pending (bindat-get-field breakpoint 'pending))
2906 (func (bindat-get-field breakpoint 'func))
2907 (type (bindat-get-field breakpoint 'type)))
2908 (gdb-table-add-row table
2909 (list
2910 (bindat-get-field breakpoint 'number)
2911 (or type "")
2912 (or (bindat-get-field breakpoint 'disp) "")
2913 (let ((flag (bindat-get-field breakpoint 'enabled)))
2914 (if (string-equal flag "y")
2915 (eval-when-compile
2916 (propertize "y" 'font-lock-face
2917 font-lock-warning-face))
2918 (eval-when-compile
2919 (propertize "n" 'font-lock-face
2920 font-lock-comment-face))))
2921 (bindat-get-field breakpoint 'addr)
2922 (or (bindat-get-field breakpoint 'times) "")
2923 (if (and type (string-match ".*watchpoint" type))
2924 (bindat-get-field breakpoint 'what)
2925 (or pending at
2926 (concat "in "
2927 (propertize (or func "unknown")
2928 'font-lock-face font-lock-function-name-face)
2929 (gdb-frame-location breakpoint)))))
2930 ;; Add clickable properties only for breakpoints with file:line
2931 ;; information
2932 (append (list 'gdb-breakpoint breakpoint)
2933 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2934 mouse-face highlight))))))
2935 (insert (gdb-table-string table " "))
2936 (gdb-place-breakpoints)))
2938 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2939 (defun gdb-place-breakpoints ()
2940 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2941 (dolist (buffer (buffer-list))
2942 (with-current-buffer buffer
2943 (if (and (eq gud-minor-mode 'gdbmi)
2944 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2945 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2946 (dolist (breakpoint gdb-breakpoints-list)
2947 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2948 ; an associative list
2949 (line (bindat-get-field breakpoint 'line)))
2950 (when line
2951 (let ((file (bindat-get-field breakpoint 'fullname))
2952 (flag (bindat-get-field breakpoint 'enabled))
2953 (bptno (bindat-get-field breakpoint 'number)))
2954 (unless (and file (file-exists-p file))
2955 (setq file (cdr (assoc bptno gdb-location-alist))))
2956 (if (or (null file)
2957 (string-equal file "File not found"))
2958 ;; If the full filename is not recorded in the
2959 ;; breakpoint structure or in `gdb-location-alist', use
2960 ;; -file-list-exec-source-file to extract it.
2961 (when (setq file (bindat-get-field breakpoint 'file))
2962 (gdb-input (concat "list " file ":1") 'ignore)
2963 (gdb-input "-file-list-exec-source-file"
2964 `(lambda () (gdb-get-location
2965 ,bptno ,line ,flag))))
2966 (with-current-buffer (find-file-noselect file 'nowarn)
2967 (gdb-init-buffer)
2968 ;; Only want one breakpoint icon at each location.
2969 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2970 (string-to-number line)))))))))
2972 (defconst gdb-source-file-regexp
2973 (concat "fullname=\\(" gdb--string-regexp "\\)"))
2975 (defun gdb-get-location (bptno line flag)
2976 "Find the directory containing the relevant source file.
2977 Put in buffer and place breakpoint icon."
2978 (goto-char (point-min))
2979 (catch 'file-not-found
2980 (if (re-search-forward gdb-source-file-regexp nil t)
2981 (delete (cons bptno "File not found") gdb-location-alist)
2982 ;; FIXME: Why/how do we use (match-string 1) when the search failed?
2983 (push (cons bptno (match-string 1)) gdb-location-alist)
2984 (gdb-resync)
2985 (unless (assoc bptno gdb-location-alist)
2986 (push (cons bptno "File not found") gdb-location-alist)
2987 (message-box "Cannot find source file for breakpoint location.
2988 Add directory to search path for source files using the GDB command, dir."))
2989 (throw 'file-not-found nil))
2990 (with-current-buffer (find-file-noselect (match-string 1))
2991 (gdb-init-buffer)
2992 ;; only want one breakpoint icon at each location
2993 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2995 (add-hook 'find-file-hook 'gdb-find-file-hook)
2997 (defun gdb-find-file-hook ()
2998 "Set up buffer for debugging if file is part of the source code
2999 of the current session."
3000 (if (and (buffer-name gud-comint-buffer)
3001 ;; in case gud or gdb-ui is just loaded
3002 gud-comint-buffer
3003 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3004 'gdbmi))
3005 (if (member buffer-file-name gdb-source-file-list)
3006 (with-current-buffer (find-buffer-visiting buffer-file-name)
3007 (gdb-init-buffer)))))
3009 (declare-function gud-remove "gdb-mi" t t) ; gud-def
3010 (declare-function gud-break "gdb-mi" t t) ; gud-def
3011 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
3013 (defun gdb-mouse-set-clear-breakpoint (event)
3014 "Set/clear breakpoint in left fringe/margin at mouse click.
3015 If not in a source or disassembly buffer just set point."
3016 (interactive "e")
3017 (mouse-minibuffer-check event)
3018 (let ((posn (event-end event)))
3019 (with-selected-window (posn-window posn)
3020 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
3021 (if (numberp (posn-point posn))
3022 (save-excursion
3023 (goto-char (posn-point posn))
3024 (if (or (posn-object posn)
3025 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
3026 'breakpoint))
3027 (gud-remove nil)
3028 (gud-break nil)))))
3029 (posn-set-point posn))))
3031 (defun gdb-mouse-toggle-breakpoint-margin (event)
3032 "Enable/disable breakpoint in left margin with mouse click."
3033 (interactive "e")
3034 (mouse-minibuffer-check event)
3035 (let ((posn (event-end event)))
3036 (if (numberp (posn-point posn))
3037 (with-selected-window (posn-window posn)
3038 (save-excursion
3039 (goto-char (posn-point posn))
3040 (if (posn-object posn)
3041 (gud-basic-call
3042 (let ((bptno (get-text-property
3043 0 'gdb-bptno (car (posn-string posn)))))
3044 (concat
3045 (if (get-text-property
3046 0 'gdb-enabled (car (posn-string posn)))
3047 "-break-disable "
3048 "-break-enable ")
3049 bptno)))))))))
3051 (defun gdb-mouse-toggle-breakpoint-fringe (event)
3052 "Enable/disable breakpoint in left fringe with mouse click."
3053 (interactive "e")
3054 (mouse-minibuffer-check event)
3055 (let* ((posn (event-end event))
3056 (pos (posn-point posn))
3057 obj)
3058 (when (numberp pos)
3059 (with-selected-window (posn-window posn)
3060 (with-current-buffer (window-buffer)
3061 (goto-char pos)
3062 (dolist (overlay (overlays-in pos pos))
3063 (when (overlay-get overlay 'put-break)
3064 (setq obj (overlay-get overlay 'before-string))))
3065 (when (stringp obj)
3066 (gud-basic-call
3067 (concat
3068 (if (get-text-property 0 'gdb-enabled obj)
3069 "-break-disable "
3070 "-break-enable ")
3071 (get-text-property 0 'gdb-bptno obj)))))))))
3073 (defun gdb-breakpoints-buffer-name ()
3074 (concat "*breakpoints of " (gdb-get-target-string) "*"))
3076 (defun gdb-display-breakpoints-buffer (&optional thread)
3077 "Display GDB breakpoints."
3078 (interactive)
3079 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
3081 (defun gdb-frame-breakpoints-buffer (&optional thread)
3082 "Display GDB breakpoints in another frame."
3083 (interactive)
3084 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
3085 gdb-display-buffer-other-frame-action))
3087 (defvar gdb-breakpoints-mode-map
3088 (let ((map (make-sparse-keymap))
3089 (menu (make-sparse-keymap "Breakpoints")))
3090 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
3091 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
3092 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
3093 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
3094 (suppress-keymap map)
3095 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
3096 (define-key map " " 'gdb-toggle-breakpoint)
3097 (define-key map "D" 'gdb-delete-breakpoint)
3098 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
3099 (define-key map "q" 'gdb-delete-frame-or-window)
3100 (define-key map "\r" 'gdb-goto-breakpoint)
3101 (define-key map "\t" (lambda ()
3102 (interactive)
3103 (gdb-set-window-buffer
3104 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
3105 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3106 (define-key map [follow-link] 'mouse-face)
3107 map))
3109 (defun gdb-delete-frame-or-window ()
3110 "Delete frame if there is only one window. Otherwise delete the window."
3111 (interactive)
3112 (if (one-window-p) (delete-frame)
3113 (delete-window)))
3115 ;;from make-mode-line-mouse-map
3116 (defun gdb-make-header-line-mouse-map (mouse function) "\
3117 Return a keymap with single entry for mouse key MOUSE on the header line.
3118 MOUSE is defined to run function FUNCTION with no args in the buffer
3119 corresponding to the mode line clicked."
3120 (let ((map (make-sparse-keymap)))
3121 (define-key map (vector 'header-line mouse) function)
3122 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3123 map))
3125 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3126 `(propertize ,name
3127 'help-echo ,help-echo
3128 'mouse-face ',mouse-face
3129 'face ',face
3130 'local-map
3131 (gdb-make-header-line-mouse-map
3132 'mouse-1
3133 (lambda (event) (interactive "e")
3134 (save-selected-window
3135 (select-window (posn-window (event-start event)))
3136 (gdb-set-window-buffer
3137 (gdb-get-buffer-create ',buffer) t) )))))
3140 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3141 ;;; Threads view
3143 (defun gdb-threads-buffer-name ()
3144 (concat "*threads of " (gdb-get-target-string) "*"))
3146 (defun gdb-display-threads-buffer (&optional thread)
3147 "Display GDB threads."
3148 (interactive)
3149 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3151 (defun gdb-frame-threads-buffer (&optional thread)
3152 "Display GDB threads in another frame."
3153 (interactive)
3154 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3155 gdb-display-buffer-other-frame-action))
3157 (def-gdb-trigger-and-handler
3158 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3159 gdb-thread-list-handler gdb-thread-list-handler-custom
3160 '(start update update-threads))
3162 (gdb-set-buffer-rules
3163 'gdb-threads-buffer
3164 'gdb-threads-buffer-name
3165 'gdb-threads-mode
3166 'gdb-invalidate-threads)
3168 (defvar gdb-threads-font-lock-keywords
3169 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3170 (" \\(stopped\\)" (1 font-lock-warning-face))
3171 (" \\(running\\)" (1 font-lock-string-face))
3172 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3173 "Font lock keywords used in `gdb-threads-mode'.")
3175 (defvar gdb-threads-mode-map
3176 (let ((map (make-sparse-keymap)))
3177 (define-key map "\r" 'gdb-select-thread)
3178 (define-key map "f" 'gdb-display-stack-for-thread)
3179 (define-key map "F" 'gdb-frame-stack-for-thread)
3180 (define-key map "l" 'gdb-display-locals-for-thread)
3181 (define-key map "L" 'gdb-frame-locals-for-thread)
3182 (define-key map "r" 'gdb-display-registers-for-thread)
3183 (define-key map "R" 'gdb-frame-registers-for-thread)
3184 (define-key map "d" 'gdb-display-disassembly-for-thread)
3185 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3186 (define-key map "i" 'gdb-interrupt-thread)
3187 (define-key map "c" 'gdb-continue-thread)
3188 (define-key map "s" 'gdb-step-thread)
3189 (define-key map "\t"
3190 (lambda ()
3191 (interactive)
3192 (gdb-set-window-buffer
3193 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3194 (define-key map [mouse-2] 'gdb-select-thread)
3195 (define-key map [follow-link] 'mouse-face)
3196 map))
3198 (defvar gdb-threads-header
3199 (list
3200 (gdb-propertize-header
3201 "Breakpoints" gdb-breakpoints-buffer
3202 "mouse-1: select" mode-line-highlight mode-line-inactive)
3204 (gdb-propertize-header "Threads" gdb-threads-buffer
3205 nil nil mode-line)))
3207 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3208 "Major mode for GDB threads."
3209 (setq gdb-thread-position (make-marker))
3210 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3211 (setq header-line-format gdb-threads-header)
3212 (set (make-local-variable 'font-lock-defaults)
3213 '(gdb-threads-font-lock-keywords))
3214 'gdb-invalidate-threads)
3216 (defun gdb-thread-list-handler-custom ()
3217 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3218 (table (make-gdb-table))
3219 (marked-line nil))
3220 (setq gdb-threads-list nil)
3221 (setq gdb-running-threads-count 0)
3222 (setq gdb-stopped-threads-count 0)
3223 (set-marker gdb-thread-position nil)
3225 (dolist (thread (reverse threads-list))
3226 (let ((running (equal (bindat-get-field thread 'state) "running")))
3227 (add-to-list 'gdb-threads-list
3228 (cons (bindat-get-field thread 'id)
3229 thread))
3230 (cl-incf (if running
3231 gdb-running-threads-count
3232 gdb-stopped-threads-count))
3234 (gdb-table-add-row
3235 table
3236 (list
3237 (bindat-get-field thread 'id)
3238 (concat
3239 (if gdb-thread-buffer-verbose-names
3240 (concat (bindat-get-field thread 'target-id) " ") "")
3241 (bindat-get-field thread 'state)
3242 ;; Include frame information for stopped threads
3243 (if (not running)
3244 (concat
3245 " in " (bindat-get-field thread 'frame 'func)
3246 (if gdb-thread-buffer-arguments
3247 (concat
3248 " ("
3249 (let ((args (bindat-get-field thread 'frame 'args)))
3250 (mapconcat
3251 (lambda (arg)
3252 (apply #'format "%s=%s"
3253 (gdb-get-many-fields arg 'name 'value)))
3254 args ","))
3255 ")")
3257 (if gdb-thread-buffer-locations
3258 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3259 (if gdb-thread-buffer-addresses
3260 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3261 "")))
3262 (list
3263 'gdb-thread thread
3264 'mouse-face 'highlight
3265 'help-echo "mouse-2, RET: select thread")))
3266 (when (string-equal gdb-thread-number
3267 (bindat-get-field thread 'id))
3268 (setq marked-line (length gdb-threads-list))))
3269 (insert (gdb-table-string table " "))
3270 (when marked-line
3271 (gdb-mark-line marked-line gdb-thread-position)))
3272 ;; We update gud-running here because we need to make sure that
3273 ;; gdb-threads-list is up-to-date
3274 (gdb-update-gud-running)
3275 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3277 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3278 "Define a NAME command which will act upon thread on the current line.
3280 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3281 be the value of `gdb-thread' property of the current line.
3282 If `gdb-thread' is nil, error is signaled."
3283 `(defun ,name (&optional event)
3284 ,(when doc doc)
3285 (interactive (list last-input-event))
3286 (if event (posn-set-point (event-end event)))
3287 (save-excursion
3288 (beginning-of-line)
3289 (let ((thread (get-text-property (point) 'gdb-thread)))
3290 (if thread
3291 ,custom-defun
3292 (error "Not recognized as thread line"))))))
3294 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3295 &optional doc)
3296 "Define a NAME which will call BUFFER-COMMAND with id of thread
3297 on the current line."
3298 `(def-gdb-thread-buffer-command ,name
3299 (,buffer-command (bindat-get-field thread 'id))
3300 ,doc))
3302 (def-gdb-thread-buffer-command gdb-select-thread
3303 (let ((new-id (bindat-get-field thread 'id)))
3304 (gdb-setq-thread-number new-id)
3305 (gdb-input (concat "-thread-select " new-id) 'ignore)
3306 (gdb-update))
3307 "Select the thread at current line of threads buffer.")
3309 (def-gdb-thread-buffer-simple-command
3310 gdb-display-stack-for-thread
3311 gdb-preemptively-display-stack-buffer
3312 "Display stack buffer for the thread at current line.")
3314 (def-gdb-thread-buffer-simple-command
3315 gdb-display-locals-for-thread
3316 gdb-preemptively-display-locals-buffer
3317 "Display locals buffer for the thread at current line.")
3319 (def-gdb-thread-buffer-simple-command
3320 gdb-display-registers-for-thread
3321 gdb-preemptively-display-registers-buffer
3322 "Display registers buffer for the thread at current line.")
3324 (def-gdb-thread-buffer-simple-command
3325 gdb-display-disassembly-for-thread
3326 gdb-preemptively-display-disassembly-buffer
3327 "Display disassembly buffer for the thread at current line.")
3329 (def-gdb-thread-buffer-simple-command
3330 gdb-frame-stack-for-thread
3331 gdb-frame-stack-buffer
3332 "Display another frame with stack buffer for thread at current line.")
3334 (def-gdb-thread-buffer-simple-command
3335 gdb-frame-locals-for-thread
3336 gdb-frame-locals-buffer
3337 "Display another frame with locals buffer for thread at current line.")
3339 (def-gdb-thread-buffer-simple-command
3340 gdb-frame-registers-for-thread
3341 gdb-frame-registers-buffer
3342 "Display another frame with registers buffer for the thread at current line.")
3344 (def-gdb-thread-buffer-simple-command
3345 gdb-frame-disassembly-for-thread
3346 gdb-frame-disassembly-buffer
3347 "Display another frame with disassembly buffer for the thread at current line.")
3349 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3350 "Define a NAME which will execute GUD-COMMAND with
3351 `gdb-thread-number' locally bound to id of thread on the current
3352 line."
3353 `(def-gdb-thread-buffer-command ,name
3354 (if gdb-non-stop
3355 (let ((gdb-thread-number (bindat-get-field thread 'id))
3356 (gdb-gud-control-all-threads nil))
3357 (call-interactively #',gud-command))
3358 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3359 ,doc))
3361 (def-gdb-thread-buffer-gud-command
3362 gdb-interrupt-thread
3363 gud-stop-subjob
3364 "Interrupt thread at current line.")
3366 ;; Defined opaquely in M-x gdb via gud-def.
3367 (declare-function gud-cont "gdb-mi" (arg) t)
3369 (def-gdb-thread-buffer-gud-command
3370 gdb-continue-thread
3371 gud-cont
3372 "Continue thread at current line.")
3374 (declare-function gud-step "gdb-mi" (arg) t)
3376 (def-gdb-thread-buffer-gud-command
3377 gdb-step-thread
3378 gud-step
3379 "Step thread at current line.")
3382 ;;; Memory view
3384 (defcustom gdb-memory-rows 8
3385 "Number of data rows in memory window."
3386 :type 'integer
3387 :group 'gud
3388 :version "23.2")
3390 (defcustom gdb-memory-columns 4
3391 "Number of data columns in memory window."
3392 :type 'integer
3393 :group 'gud
3394 :version "23.2")
3396 (defcustom gdb-memory-format "x"
3397 "Display format of data items in memory window."
3398 :type '(choice (const :tag "Hexadecimal" "x")
3399 (const :tag "Signed decimal" "d")
3400 (const :tag "Unsigned decimal" "u")
3401 (const :tag "Octal" "o")
3402 (const :tag "Binary" "t"))
3403 :group 'gud
3404 :version "22.1")
3406 (defcustom gdb-memory-unit 4
3407 "Unit size of data items in memory window."
3408 :type '(choice (const :tag "Byte" 1)
3409 (const :tag "Halfword" 2)
3410 (const :tag "Word" 4)
3411 (const :tag "Giant word" 8))
3412 :group 'gud
3413 :version "23.2")
3415 (def-gdb-trigger-and-handler
3416 gdb-invalidate-memory
3417 (format "-data-read-memory %s %s %d %d %d"
3418 gdb-memory-address
3419 gdb-memory-format
3420 gdb-memory-unit
3421 gdb-memory-rows
3422 gdb-memory-columns)
3423 gdb-read-memory-handler
3424 gdb-read-memory-custom
3425 '(start update))
3427 (gdb-set-buffer-rules
3428 'gdb-memory-buffer
3429 'gdb-memory-buffer-name
3430 'gdb-memory-mode
3431 'gdb-invalidate-memory)
3433 (defun gdb-memory-column-width (size format)
3434 "Return length of string with memory unit of SIZE in FORMAT.
3436 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3437 in `gdb-memory-format'."
3438 (let ((format-base (cdr (assoc format
3439 '(("x" . 16)
3440 ("d" . 10) ("u" . 10)
3441 ("o" . 8)
3442 ("t" . 2))))))
3443 (if format-base
3444 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3445 (cond ((string-equal format "x")
3446 (+ 2 res)) ; hexadecimal numbers have 0x in front
3447 ((or (string-equal format "d")
3448 (string-equal format "o"))
3449 (1+ res))
3450 (t res)))
3451 (error "Unknown format"))))
3453 (defun gdb-read-memory-custom ()
3454 (let* ((res (gdb-json-partial-output))
3455 (err-msg (bindat-get-field res 'msg)))
3456 (if (not err-msg)
3457 (let ((memory (bindat-get-field res 'memory)))
3458 (setq gdb-memory-address (bindat-get-field res 'addr))
3459 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3460 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3461 (setq gdb-memory-last-address gdb-memory-address)
3462 (dolist (row memory)
3463 (insert (concat (bindat-get-field row 'addr) ":"))
3464 (dolist (column (bindat-get-field row 'data))
3465 (insert (gdb-pad-string column
3466 (+ 2 (gdb-memory-column-width
3467 gdb-memory-unit
3468 gdb-memory-format)))))
3469 (newline)))
3470 ;; Show last page instead of empty buffer when out of bounds
3471 (progn
3472 (let ((gdb-memory-address gdb-memory-last-address))
3473 (gdb-invalidate-memory 'update)
3474 (error err-msg))))))
3476 (defvar gdb-memory-mode-map
3477 (let ((map (make-sparse-keymap)))
3478 (suppress-keymap map t)
3479 (define-key map "q" 'kill-this-buffer)
3480 (define-key map "n" 'gdb-memory-show-next-page)
3481 (define-key map "p" 'gdb-memory-show-previous-page)
3482 (define-key map "a" 'gdb-memory-set-address)
3483 (define-key map "t" 'gdb-memory-format-binary)
3484 (define-key map "o" 'gdb-memory-format-octal)
3485 (define-key map "u" 'gdb-memory-format-unsigned)
3486 (define-key map "d" 'gdb-memory-format-signed)
3487 (define-key map "x" 'gdb-memory-format-hexadecimal)
3488 (define-key map "b" 'gdb-memory-unit-byte)
3489 (define-key map "h" 'gdb-memory-unit-halfword)
3490 (define-key map "w" 'gdb-memory-unit-word)
3491 (define-key map "g" 'gdb-memory-unit-giant)
3492 (define-key map "R" 'gdb-memory-set-rows)
3493 (define-key map "C" 'gdb-memory-set-columns)
3494 map))
3496 (defun gdb-memory-set-address-event (event)
3497 "Handle a click on address field in memory buffer header."
3498 (interactive "e")
3499 (save-selected-window
3500 (select-window (posn-window (event-start event)))
3501 (gdb-memory-set-address)))
3503 ;; Non-event version for use within keymap
3504 (defun gdb-memory-set-address ()
3505 "Set the start memory address."
3506 (interactive)
3507 (let ((arg (read-from-minibuffer "Memory address: ")))
3508 (setq gdb-memory-address arg))
3509 (gdb-invalidate-memory 'update))
3511 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3512 "Define a function NAME which reads new VAR value from minibuffer."
3513 `(defun ,name (event)
3514 ,(when doc doc)
3515 (interactive "e")
3516 (save-selected-window
3517 (select-window (posn-window (event-start event)))
3518 (let* ((arg (read-from-minibuffer ,echo-string))
3519 (count (string-to-number arg)))
3520 (if (<= count 0)
3521 (error "Positive number only")
3522 (customize-set-variable ',variable count)
3523 (gdb-invalidate-memory 'update))))))
3525 (def-gdb-set-positive-number
3526 gdb-memory-set-rows
3527 gdb-memory-rows
3528 "Rows: "
3529 "Set the number of data rows in memory window.")
3531 (def-gdb-set-positive-number
3532 gdb-memory-set-columns
3533 gdb-memory-columns
3534 "Columns: "
3535 "Set the number of data columns in memory window.")
3537 (defmacro def-gdb-memory-format (name format doc)
3538 "Define a function NAME to switch memory buffer to use FORMAT.
3540 DOC is an optional documentation string."
3541 `(defun ,name () ,(when doc doc)
3542 (interactive)
3543 (customize-set-variable 'gdb-memory-format ,format)
3544 (gdb-invalidate-memory 'update)))
3546 (def-gdb-memory-format
3547 gdb-memory-format-binary "t"
3548 "Set the display format to binary.")
3550 (def-gdb-memory-format
3551 gdb-memory-format-octal "o"
3552 "Set the display format to octal.")
3554 (def-gdb-memory-format
3555 gdb-memory-format-unsigned "u"
3556 "Set the display format to unsigned decimal.")
3558 (def-gdb-memory-format
3559 gdb-memory-format-signed "d"
3560 "Set the display format to decimal.")
3562 (def-gdb-memory-format
3563 gdb-memory-format-hexadecimal "x"
3564 "Set the display format to hexadecimal.")
3566 (defvar gdb-memory-format-map
3567 (let ((map (make-sparse-keymap)))
3568 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3569 map)
3570 "Keymap to select format in the header line.")
3572 (defvar gdb-memory-format-menu
3573 (let ((map (make-sparse-keymap "Format")))
3575 (define-key map [binary]
3576 '(menu-item "Binary" gdb-memory-format-binary
3577 :button (:radio . (equal gdb-memory-format "t"))))
3578 (define-key map [octal]
3579 '(menu-item "Octal" gdb-memory-format-octal
3580 :button (:radio . (equal gdb-memory-format "o"))))
3581 (define-key map [unsigned]
3582 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3583 :button (:radio . (equal gdb-memory-format "u"))))
3584 (define-key map [signed]
3585 '(menu-item "Signed Decimal" gdb-memory-format-signed
3586 :button (:radio . (equal gdb-memory-format "d"))))
3587 (define-key map [hexadecimal]
3588 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3589 :button (:radio . (equal gdb-memory-format "x"))))
3590 map)
3591 "Menu of display formats in the header line.")
3593 (defun gdb-memory-format-menu (event)
3594 (interactive "@e")
3595 (x-popup-menu event gdb-memory-format-menu))
3597 (defun gdb-memory-format-menu-1 (event)
3598 (interactive "e")
3599 (save-selected-window
3600 (select-window (posn-window (event-start event)))
3601 (let* ((selection (gdb-memory-format-menu event))
3602 (binding (and selection (lookup-key gdb-memory-format-menu
3603 (vector (car selection))))))
3604 (if binding (call-interactively binding)))))
3606 (defmacro def-gdb-memory-unit (name unit-size doc)
3607 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3609 DOC is an optional documentation string."
3610 `(defun ,name () ,(when doc doc)
3611 (interactive)
3612 (customize-set-variable 'gdb-memory-unit ,unit-size)
3613 (gdb-invalidate-memory 'update)))
3615 (def-gdb-memory-unit gdb-memory-unit-giant 8
3616 "Set the unit size to giant words (eight bytes).")
3618 (def-gdb-memory-unit gdb-memory-unit-word 4
3619 "Set the unit size to words (four bytes).")
3621 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3622 "Set the unit size to halfwords (two bytes).")
3624 (def-gdb-memory-unit gdb-memory-unit-byte 1
3625 "Set the unit size to bytes.")
3627 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3628 "Define a function NAME which show new address in memory buffer.
3630 The defined function switches Memory buffer to show address
3631 stored in ADDRESS-VAR variable.
3633 DOC is an optional documentation string."
3634 `(defun ,name
3635 ,(when doc doc)
3636 (interactive)
3637 (let ((gdb-memory-address ,address-var))
3638 (gdb-invalidate-memory))))
3640 (def-gdb-memory-show-page gdb-memory-show-previous-page
3641 gdb-memory-prev-page)
3643 (def-gdb-memory-show-page gdb-memory-show-next-page
3644 gdb-memory-next-page)
3646 (defvar gdb-memory-unit-map
3647 (let ((map (make-sparse-keymap)))
3648 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3649 map)
3650 "Keymap to select units in the header line.")
3652 (defvar gdb-memory-unit-menu
3653 (let ((map (make-sparse-keymap "Unit")))
3654 (define-key map [giantwords]
3655 '(menu-item "Giant words" gdb-memory-unit-giant
3656 :button (:radio . (equal gdb-memory-unit 8))))
3657 (define-key map [words]
3658 '(menu-item "Words" gdb-memory-unit-word
3659 :button (:radio . (equal gdb-memory-unit 4))))
3660 (define-key map [halfwords]
3661 '(menu-item "Halfwords" gdb-memory-unit-halfword
3662 :button (:radio . (equal gdb-memory-unit 2))))
3663 (define-key map [bytes]
3664 '(menu-item "Bytes" gdb-memory-unit-byte
3665 :button (:radio . (equal gdb-memory-unit 1))))
3666 map)
3667 "Menu of units in the header line.")
3669 (defun gdb-memory-unit-menu (event)
3670 (interactive "@e")
3671 (x-popup-menu event gdb-memory-unit-menu))
3673 (defun gdb-memory-unit-menu-1 (event)
3674 (interactive "e")
3675 (save-selected-window
3676 (select-window (posn-window (event-start event)))
3677 (let* ((selection (gdb-memory-unit-menu event))
3678 (binding (and selection (lookup-key gdb-memory-unit-menu
3679 (vector (car selection))))))
3680 (if binding (call-interactively binding)))))
3682 (defvar gdb-memory-font-lock-keywords
3683 '(;; <__function.name+n>
3684 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3685 (1 font-lock-function-name-face)))
3686 "Font lock keywords used in `gdb-memory-mode'.")
3688 (defvar gdb-memory-header
3689 '(:eval
3690 (concat
3691 "Start address["
3692 (propertize "-"
3693 'face font-lock-warning-face
3694 'help-echo "mouse-1: decrement address"
3695 'mouse-face 'mode-line-highlight
3696 'local-map (gdb-make-header-line-mouse-map
3697 'mouse-1
3698 #'gdb-memory-show-previous-page))
3700 (propertize "+"
3701 'face font-lock-warning-face
3702 'help-echo "mouse-1: increment address"
3703 'mouse-face 'mode-line-highlight
3704 'local-map (gdb-make-header-line-mouse-map
3705 'mouse-1
3706 #'gdb-memory-show-next-page))
3707 "]: "
3708 (propertize gdb-memory-address
3709 'face font-lock-warning-face
3710 'help-echo "mouse-1: set start address"
3711 'mouse-face 'mode-line-highlight
3712 'local-map (gdb-make-header-line-mouse-map
3713 'mouse-1
3714 #'gdb-memory-set-address-event))
3715 " Rows: "
3716 (propertize (number-to-string gdb-memory-rows)
3717 'face font-lock-warning-face
3718 'help-echo "mouse-1: set number of columns"
3719 'mouse-face 'mode-line-highlight
3720 'local-map (gdb-make-header-line-mouse-map
3721 'mouse-1
3722 #'gdb-memory-set-rows))
3723 " Columns: "
3724 (propertize (number-to-string gdb-memory-columns)
3725 'face font-lock-warning-face
3726 'help-echo "mouse-1: set number of columns"
3727 'mouse-face 'mode-line-highlight
3728 'local-map (gdb-make-header-line-mouse-map
3729 'mouse-1
3730 #'gdb-memory-set-columns))
3731 " Display Format: "
3732 (propertize gdb-memory-format
3733 'face font-lock-warning-face
3734 'help-echo "mouse-3: select display format"
3735 'mouse-face 'mode-line-highlight
3736 'local-map gdb-memory-format-map)
3737 " Unit Size: "
3738 (propertize (number-to-string gdb-memory-unit)
3739 'face font-lock-warning-face
3740 'help-echo "mouse-3: select unit size"
3741 'mouse-face 'mode-line-highlight
3742 'local-map gdb-memory-unit-map)))
3743 "Header line used in `gdb-memory-mode'.")
3745 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3746 "Major mode for examining memory."
3747 (setq header-line-format gdb-memory-header)
3748 (set (make-local-variable 'font-lock-defaults)
3749 '(gdb-memory-font-lock-keywords))
3750 'gdb-invalidate-memory)
3752 (defun gdb-memory-buffer-name ()
3753 (concat "*memory of " (gdb-get-target-string) "*"))
3755 (defun gdb-display-memory-buffer (&optional thread)
3756 "Display GDB memory contents."
3757 (interactive)
3758 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3760 (defun gdb-frame-memory-buffer ()
3761 "Display memory contents in another frame."
3762 (interactive)
3763 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3764 gdb-display-buffer-other-frame-action))
3767 ;;; Disassembly view
3769 (defun gdb-disassembly-buffer-name ()
3770 (gdb-current-context-buffer-name
3771 (concat "disassembly of " (gdb-get-target-string))))
3773 (defun gdb-display-disassembly-buffer (&optional thread)
3774 "Display GDB disassembly information."
3775 (interactive)
3776 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3778 (def-gdb-preempt-display-buffer
3779 gdb-preemptively-display-disassembly-buffer
3780 'gdb-disassembly-buffer)
3782 (defun gdb-frame-disassembly-buffer (&optional thread)
3783 "Display GDB disassembly information in another frame."
3784 (interactive)
3785 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3786 gdb-display-buffer-other-frame-action))
3788 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3789 (let* ((frame (gdb-current-buffer-frame))
3790 (file (bindat-get-field frame 'fullname))
3791 (line (bindat-get-field frame 'line)))
3792 (if file
3793 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3794 ;; If we're unable to get a file name / line for $PC, simply
3795 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3796 ;; 150 bytes) instructions.
3797 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3798 gdb-disassembly-handler
3799 ;; We update disassembly only after we have actual frame information
3800 ;; about all threads, so no there's `update' signal in this list
3801 '(start update-disassembly))
3803 (def-gdb-auto-update-handler
3804 gdb-disassembly-handler
3805 gdb-disassembly-handler-custom
3808 (gdb-set-buffer-rules
3809 'gdb-disassembly-buffer
3810 'gdb-disassembly-buffer-name
3811 'gdb-disassembly-mode
3812 'gdb-invalidate-disassembly)
3814 (defvar gdb-disassembly-font-lock-keywords
3815 '(;; <__function.name+n>
3816 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3817 (1 font-lock-function-name-face))
3818 ;; 0xNNNNNNNN <__function.name+n>: opcode
3819 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3820 (4 font-lock-keyword-face))
3821 ;; %register(at least i386)
3822 ("%\\sw+" . font-lock-variable-name-face)
3823 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3824 (1 font-lock-comment-face)
3825 (2 font-lock-function-name-face))
3826 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3827 "Font lock keywords used in `gdb-disassembly-mode'.")
3829 (defvar gdb-disassembly-mode-map
3830 ;; TODO
3831 (let ((map (make-sparse-keymap)))
3832 (suppress-keymap map)
3833 (define-key map "q" 'kill-this-buffer)
3834 map))
3836 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3837 "Major mode for GDB disassembly information."
3838 ;; TODO Rename overlay variable for disassembly mode
3839 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3840 (setq fringes-outside-margins t)
3841 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3842 (set (make-local-variable 'font-lock-defaults)
3843 '(gdb-disassembly-font-lock-keywords))
3844 'gdb-invalidate-disassembly)
3846 (defun gdb-disassembly-handler-custom ()
3847 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3848 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3849 (table (make-gdb-table))
3850 (marked-line nil))
3851 (dolist (instr instructions)
3852 (gdb-table-add-row table
3853 (list
3854 (bindat-get-field instr 'address)
3855 (let
3856 ((func-name (bindat-get-field instr 'func-name))
3857 (offset (bindat-get-field instr 'offset)))
3858 (if func-name
3859 (format "<%s+%s>:" func-name offset)
3860 ""))
3861 (bindat-get-field instr 'inst)))
3862 (when (string-equal (bindat-get-field instr 'address)
3863 address)
3864 (progn
3865 (setq marked-line (length (gdb-table-rows table)))
3866 (setq fringe-indicator-alist
3867 (if (string-equal gdb-frame-number "0")
3869 '((overlay-arrow . hollow-right-triangle)))))))
3870 (insert (gdb-table-string table " "))
3871 (gdb-disassembly-place-breakpoints)
3872 ;; Mark current position with overlay arrow and scroll window to
3873 ;; that point
3874 (when marked-line
3875 (let ((window (get-buffer-window (current-buffer) 0)))
3876 (set-window-point window (gdb-mark-line marked-line
3877 gdb-disassembly-position))))
3878 (setq mode-name
3879 (gdb-current-context-mode-name
3880 (concat "Disassembly: "
3881 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3883 (defun gdb-disassembly-place-breakpoints ()
3884 (gdb-remove-breakpoint-icons (point-min) (point-max))
3885 (dolist (breakpoint gdb-breakpoints-list)
3886 (let* ((breakpoint (cdr breakpoint))
3887 (bptno (bindat-get-field breakpoint 'number))
3888 (flag (bindat-get-field breakpoint 'enabled))
3889 (address (bindat-get-field breakpoint 'addr)))
3890 (save-excursion
3891 (goto-char (point-min))
3892 (if (re-search-forward (concat "^" address) nil t)
3893 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3896 (defvar gdb-breakpoints-header
3897 (list
3898 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3899 nil nil mode-line)
3901 (gdb-propertize-header "Threads" gdb-threads-buffer
3902 "mouse-1: select" mode-line-highlight
3903 mode-line-inactive)))
3905 ;;; Breakpoints view
3906 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3907 "Major mode for gdb breakpoints."
3908 (setq header-line-format gdb-breakpoints-header)
3909 'gdb-invalidate-breakpoints)
3911 (defun gdb-toggle-breakpoint ()
3912 "Enable/disable breakpoint at current line of breakpoints buffer."
3913 (interactive)
3914 (save-excursion
3915 (beginning-of-line)
3916 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3917 (if breakpoint
3918 (gud-basic-call
3919 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3920 "-break-disable "
3921 "-break-enable ")
3922 (bindat-get-field breakpoint 'number)))
3923 (error "Not recognized as break/watchpoint line")))))
3925 (defun gdb-delete-breakpoint ()
3926 "Delete the breakpoint at current line of breakpoints buffer."
3927 (interactive)
3928 (save-excursion
3929 (beginning-of-line)
3930 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3931 (if breakpoint
3932 (gud-basic-call (concat "-break-delete "
3933 (bindat-get-field breakpoint 'number)))
3934 (error "Not recognized as break/watchpoint line")))))
3936 (defun gdb-goto-breakpoint (&optional event)
3937 "Go to the location of breakpoint at current line of breakpoints buffer."
3938 (interactive (list last-input-event))
3939 (if event (posn-set-point (event-end event)))
3940 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3941 (let ((window (get-buffer-window gud-comint-buffer)))
3942 (if window (save-selected-window (select-window window))))
3943 (save-excursion
3944 (beginning-of-line)
3945 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3946 (if breakpoint
3947 (let ((bptno (bindat-get-field breakpoint 'number))
3948 (file (bindat-get-field breakpoint 'fullname))
3949 (line (bindat-get-field breakpoint 'line)))
3950 (save-selected-window
3951 (let* ((buffer (find-file-noselect
3952 (if (file-exists-p file) file
3953 (cdr (assoc bptno gdb-location-alist)))))
3954 (window (or (gdb-display-source-buffer buffer)
3955 (display-buffer buffer))))
3956 (setq gdb-source-window window)
3957 (with-current-buffer buffer
3958 (goto-char (point-min))
3959 (forward-line (1- (string-to-number line)))
3960 (set-window-point window (point))))))
3961 (error "Not recognized as break/watchpoint line")))))
3964 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3966 (def-gdb-trigger-and-handler
3967 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3968 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3969 '(start update))
3971 (gdb-set-buffer-rules
3972 'gdb-stack-buffer
3973 'gdb-stack-buffer-name
3974 'gdb-frames-mode
3975 'gdb-invalidate-frames)
3977 (defun gdb-frame-location (frame)
3978 "Return \" of file:line\" or \" of library\" for structure FRAME.
3980 FRAME must have either \"file\" and \"line\" members or \"from\"
3981 member."
3982 (let ((file (bindat-get-field frame 'file))
3983 (line (bindat-get-field frame 'line))
3984 (from (bindat-get-field frame 'from)))
3985 (let ((res (or (and file line (concat file ":" line))
3986 from)))
3987 (if res (concat " of " res) ""))))
3989 (defun gdb-stack-list-frames-custom ()
3990 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3991 (table (make-gdb-table)))
3992 (set-marker gdb-stack-position nil)
3993 (dolist (frame stack)
3994 (gdb-table-add-row table
3995 (list
3996 (bindat-get-field frame 'level)
3997 "in"
3998 (concat
3999 (bindat-get-field frame 'func)
4000 (if gdb-stack-buffer-locations
4001 (gdb-frame-location frame) "")
4002 (if gdb-stack-buffer-addresses
4003 (concat " at " (bindat-get-field frame 'addr)) "")))
4004 `(mouse-face highlight
4005 help-echo "mouse-2, RET: Select frame"
4006 gdb-frame ,frame)))
4007 (insert (gdb-table-string table " ")))
4008 (when (and gdb-frame-number
4009 (gdb-buffer-shows-main-thread-p))
4010 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
4011 gdb-stack-position))
4012 (setq mode-name
4013 (gdb-current-context-mode-name "Frames")))
4015 (defun gdb-stack-buffer-name ()
4016 (gdb-current-context-buffer-name
4017 (concat "stack frames of " (gdb-get-target-string))))
4019 (defun gdb-display-stack-buffer (&optional thread)
4020 "Display GDB backtrace for current stack."
4021 (interactive)
4022 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
4024 (def-gdb-preempt-display-buffer
4025 gdb-preemptively-display-stack-buffer
4026 'gdb-stack-buffer nil t)
4028 (defun gdb-frame-stack-buffer (&optional thread)
4029 "Display GDB backtrace for current stack in another frame."
4030 (interactive)
4031 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
4032 gdb-display-buffer-other-frame-action))
4034 (defvar gdb-frames-mode-map
4035 (let ((map (make-sparse-keymap)))
4036 (suppress-keymap map)
4037 (define-key map "q" 'kill-this-buffer)
4038 (define-key map "\r" 'gdb-select-frame)
4039 (define-key map [mouse-2] 'gdb-select-frame)
4040 (define-key map [follow-link] 'mouse-face)
4041 map))
4043 (defvar gdb-frames-font-lock-keywords
4044 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
4045 "Font lock keywords used in `gdb-frames-mode'.")
4047 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
4048 "Major mode for gdb call stack."
4049 (setq gdb-stack-position (make-marker))
4050 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
4051 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
4052 (set (make-local-variable 'font-lock-defaults)
4053 '(gdb-frames-font-lock-keywords))
4054 'gdb-invalidate-frames)
4056 (defun gdb-select-frame (&optional event)
4057 "Select the frame and display the relevant source."
4058 (interactive (list last-input-event))
4059 (if event (posn-set-point (event-end event)))
4060 (let ((frame (get-text-property (point) 'gdb-frame)))
4061 (if frame
4062 (if (gdb-buffer-shows-main-thread-p)
4063 (let ((new-level (bindat-get-field frame 'level)))
4064 (setq gdb-frame-number new-level)
4065 (gdb-input (concat "-stack-select-frame " new-level)
4066 'ignore)
4067 (gdb-update))
4068 (error "Could not select frame for non-current thread"))
4069 (error "Not recognized as frame line"))))
4072 ;; Locals buffer.
4073 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
4074 (def-gdb-trigger-and-handler
4075 gdb-invalidate-locals
4076 (concat (gdb-current-context-command "-stack-list-locals")
4077 " --simple-values")
4078 gdb-locals-handler gdb-locals-handler-custom
4079 '(start update))
4081 (gdb-set-buffer-rules
4082 'gdb-locals-buffer
4083 'gdb-locals-buffer-name
4084 'gdb-locals-mode
4085 'gdb-invalidate-locals)
4087 (defvar gdb-locals-watch-map
4088 (let ((map (make-sparse-keymap)))
4089 (suppress-keymap map)
4090 (define-key map "\r" 'gud-watch)
4091 (define-key map [mouse-2] 'gud-watch)
4092 map)
4093 "Keymap to create watch expression of a complex data type local variable.")
4095 (defvar gdb-edit-locals-map-1
4096 (let ((map (make-sparse-keymap)))
4097 (suppress-keymap map)
4098 (define-key map "\r" 'gdb-edit-locals-value)
4099 (define-key map [mouse-2] 'gdb-edit-locals-value)
4100 map)
4101 "Keymap to edit value of a simple data type local variable.")
4103 (defun gdb-edit-locals-value (&optional event)
4104 "Assign a value to a variable displayed in the locals buffer."
4105 (interactive (list last-input-event))
4106 (save-excursion
4107 (if event (posn-set-point (event-end event)))
4108 (beginning-of-line)
4109 (let* ((var (bindat-get-field
4110 (get-text-property (point) 'gdb-local-variable) 'name))
4111 (value (read-string (format "New value (%s): " var))))
4112 (gud-basic-call
4113 (concat "-gdb-set variable " var " = " value)))))
4115 ;; Don't display values of arrays or structures.
4116 ;; These can be expanded using gud-watch.
4117 (defun gdb-locals-handler-custom ()
4118 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4119 (table (make-gdb-table)))
4120 (dolist (local locals-list)
4121 (let ((name (bindat-get-field local 'name))
4122 (value (bindat-get-field local 'value))
4123 (type (bindat-get-field local 'type)))
4124 (when (not value)
4125 (setq value "<complex data type>"))
4126 (if (or (not value)
4127 (string-match "\\0x" value))
4128 (add-text-properties 0 (length name)
4129 `(mouse-face highlight
4130 help-echo "mouse-2: create watch expression"
4131 local-map ,gdb-locals-watch-map)
4132 name)
4133 (add-text-properties 0 (length value)
4134 `(mouse-face highlight
4135 help-echo "mouse-2: edit value"
4136 local-map ,gdb-edit-locals-map-1)
4137 value))
4138 (gdb-table-add-row
4139 table
4140 (list
4141 (propertize type 'font-lock-face font-lock-type-face)
4142 (propertize name 'font-lock-face font-lock-variable-name-face)
4143 value)
4144 `(gdb-local-variable ,local))))
4145 (insert (gdb-table-string table " "))
4146 (setq mode-name
4147 (gdb-current-context-mode-name
4148 (concat "Locals: "
4149 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4151 (defvar gdb-locals-header
4152 (list
4153 (gdb-propertize-header "Locals" gdb-locals-buffer
4154 nil nil mode-line)
4156 (gdb-propertize-header "Registers" gdb-registers-buffer
4157 "mouse-1: select" mode-line-highlight
4158 mode-line-inactive)))
4160 (defvar gdb-locals-mode-map
4161 (let ((map (make-sparse-keymap)))
4162 (suppress-keymap map)
4163 (define-key map "q" 'kill-this-buffer)
4164 (define-key map "\t" (lambda ()
4165 (interactive)
4166 (gdb-set-window-buffer
4167 (gdb-get-buffer-create
4168 'gdb-registers-buffer
4169 gdb-thread-number) t)))
4170 map))
4172 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4173 "Major mode for gdb locals."
4174 (setq header-line-format gdb-locals-header)
4175 'gdb-invalidate-locals)
4177 (defun gdb-locals-buffer-name ()
4178 (gdb-current-context-buffer-name
4179 (concat "locals of " (gdb-get-target-string))))
4181 (defun gdb-display-locals-buffer (&optional thread)
4182 "Display the local variables of current GDB stack."
4183 (interactive)
4184 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4186 (def-gdb-preempt-display-buffer
4187 gdb-preemptively-display-locals-buffer
4188 'gdb-locals-buffer nil t)
4190 (defun gdb-frame-locals-buffer (&optional thread)
4191 "Display the local variables of the current GDB stack in another frame."
4192 (interactive)
4193 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4194 gdb-display-buffer-other-frame-action))
4197 ;; Registers buffer.
4199 (def-gdb-trigger-and-handler
4200 gdb-invalidate-registers
4201 (concat (gdb-current-context-command "-data-list-register-values") " x")
4202 gdb-registers-handler
4203 gdb-registers-handler-custom
4204 '(start update))
4206 (gdb-set-buffer-rules
4207 'gdb-registers-buffer
4208 'gdb-registers-buffer-name
4209 'gdb-registers-mode
4210 'gdb-invalidate-registers)
4212 (defun gdb-registers-handler-custom ()
4213 (when gdb-register-names
4214 (let ((register-values
4215 (bindat-get-field (gdb-json-partial-output) 'register-values))
4216 (table (make-gdb-table)))
4217 (dolist (register register-values)
4218 (let* ((register-number (bindat-get-field register 'number))
4219 (value (bindat-get-field register 'value))
4220 (register-name (nth (string-to-number register-number)
4221 gdb-register-names)))
4222 (gdb-table-add-row
4223 table
4224 (list
4225 (propertize register-name
4226 'font-lock-face font-lock-variable-name-face)
4227 (if (member register-number gdb-changed-registers)
4228 (propertize value 'font-lock-face font-lock-warning-face)
4229 value))
4230 `(mouse-face highlight
4231 help-echo "mouse-2: edit value"
4232 gdb-register-name ,register-name))))
4233 (insert (gdb-table-string table " ")))
4234 (setq mode-name
4235 (gdb-current-context-mode-name "Registers"))))
4237 (defun gdb-edit-register-value (&optional event)
4238 "Assign a value to a register displayed in the registers buffer."
4239 (interactive (list last-input-event))
4240 (save-excursion
4241 (if event (posn-set-point (event-end event)))
4242 (beginning-of-line)
4243 (let* ((var (bindat-get-field
4244 (get-text-property (point) 'gdb-register-name)))
4245 (value (read-string (format "New value (%s): " var))))
4246 (gud-basic-call
4247 (concat "-gdb-set variable $" var " = " value)))))
4249 (defvar gdb-registers-mode-map
4250 (let ((map (make-sparse-keymap)))
4251 (suppress-keymap map)
4252 (define-key map "\r" 'gdb-edit-register-value)
4253 (define-key map [mouse-2] 'gdb-edit-register-value)
4254 (define-key map "q" 'kill-this-buffer)
4255 (define-key map "\t" (lambda ()
4256 (interactive)
4257 (gdb-set-window-buffer
4258 (gdb-get-buffer-create
4259 'gdb-locals-buffer
4260 gdb-thread-number) t)))
4261 map))
4263 (defvar gdb-registers-header
4264 (list
4265 (gdb-propertize-header "Locals" gdb-locals-buffer
4266 "mouse-1: select" mode-line-highlight
4267 mode-line-inactive)
4269 (gdb-propertize-header "Registers" gdb-registers-buffer
4270 nil nil mode-line)))
4272 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4273 "Major mode for gdb registers."
4274 (setq header-line-format gdb-registers-header)
4275 'gdb-invalidate-registers)
4277 (defun gdb-registers-buffer-name ()
4278 (gdb-current-context-buffer-name
4279 (concat "registers of " (gdb-get-target-string))))
4281 (defun gdb-display-registers-buffer (&optional thread)
4282 "Display GDB register contents."
4283 (interactive)
4284 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4286 (def-gdb-preempt-display-buffer
4287 gdb-preemptively-display-registers-buffer
4288 'gdb-registers-buffer nil t)
4290 (defun gdb-frame-registers-buffer (&optional thread)
4291 "Display GDB register contents in another frame."
4292 (interactive)
4293 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4294 gdb-display-buffer-other-frame-action))
4296 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4297 (defun gdb-get-changed-registers ()
4298 (when (gdb-get-buffer 'gdb-registers-buffer)
4299 (gdb-input "-data-list-changed-registers"
4300 'gdb-changed-registers-handler
4301 'gdb-get-changed-registers)))
4303 (defun gdb-changed-registers-handler ()
4304 (setq gdb-changed-registers nil)
4305 (dolist (register-number
4306 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4307 (push register-number gdb-changed-registers)))
4309 (defun gdb-register-names-handler ()
4310 ;; Don't use pending triggers because this handler is called
4311 ;; only once (in gdb-init-1)
4312 (setq gdb-register-names nil)
4313 (dolist (register-name
4314 (bindat-get-field (gdb-json-partial-output) 'register-names))
4315 (push register-name gdb-register-names))
4316 (setq gdb-register-names (reverse gdb-register-names)))
4319 (defun gdb-get-source-file-list ()
4320 "Create list of source files for current GDB session.
4321 If buffers already exist for any of these files, `gud-minor-mode'
4322 is set in them."
4323 (goto-char (point-min))
4324 (while (re-search-forward gdb-source-file-regexp nil t)
4325 (push (read (match-string 1)) gdb-source-file-list))
4326 (dolist (buffer (buffer-list))
4327 (with-current-buffer buffer
4328 (when (member buffer-file-name gdb-source-file-list)
4329 (gdb-init-buffer)))))
4331 (defun gdb-get-main-selected-frame ()
4332 "Trigger for `gdb-frame-handler' which uses main current thread.
4333 Called from `gdb-update'."
4334 (gdb-input (gdb-current-context-command "-stack-info-frame")
4335 'gdb-frame-handler
4336 'gdb-get-main-selected-frame))
4338 (defun gdb-frame-handler ()
4339 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4340 overlay arrow in source buffer."
4341 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4342 (when frame
4343 (setq gdb-selected-frame (bindat-get-field frame 'func))
4344 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4345 (setq gdb-frame-number (bindat-get-field frame 'level))
4346 (setq gdb-frame-address (bindat-get-field frame 'addr))
4347 (let ((line (bindat-get-field frame 'line)))
4348 (setq gdb-selected-line (and line (string-to-number line)))
4349 (when (and gdb-selected-file gdb-selected-line)
4350 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4351 (gud-display-frame)))
4352 (if gud-overlay-arrow-position
4353 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4354 (position (marker-position gud-overlay-arrow-position)))
4355 (when buffer
4356 (with-current-buffer buffer
4357 (setq fringe-indicator-alist
4358 (if (string-equal gdb-frame-number "0")
4360 '((overlay-arrow . hollow-right-triangle))))
4361 (setq gud-overlay-arrow-position (make-marker))
4362 (set-marker gud-overlay-arrow-position position))))))))
4364 (defconst gdb-prompt-name-regexp
4365 (concat "value=\\(" gdb--string-regexp "\\)"))
4367 (defun gdb-get-prompt ()
4368 "Find prompt for GDB session."
4369 (goto-char (point-min))
4370 (setq gdb-prompt-name nil)
4371 (re-search-forward gdb-prompt-name-regexp nil t)
4372 (setq gdb-prompt-name (read (match-string 1)))
4373 ;; Insert first prompt.
4374 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4376 ;;;; Window management
4377 (defun gdb-display-buffer (buf)
4378 "Show buffer BUF, and make that window dedicated."
4379 (let ((window (display-buffer buf)))
4380 (set-window-dedicated-p window t)
4381 window))
4383 ;; (let ((answer (get-buffer-window buf 0)))
4384 ;; (if answer
4385 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4386 ;; (let ((window (get-lru-window)))
4387 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4388 ;; 'gdbmi)
4389 ;; (let ((largest (get-largest-window)))
4390 ;; (setq answer (split-window largest))
4391 ;; (set-window-buffer answer buf)
4392 ;; (set-window-dedicated-p answer t)
4393 ;; answer)
4394 ;; (set-window-buffer window buf)
4395 ;; window)))))
4398 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4399 "Find window displaying a buffer with the same
4400 `gdb-buffer-type' as BUF and show BUF there. If no such window
4401 exists, just call `gdb-display-buffer' for BUF. If the window
4402 found is already dedicated, split window according to
4403 SPLIT-HORIZONTAL and show BUF in the new window."
4404 (if buf
4405 (when (not (get-buffer-window buf))
4406 (let* ((buf-type (gdb-buffer-type buf))
4407 (existing-window
4408 (get-window-with-predicate
4409 #'(lambda (w)
4410 (and (eq buf-type
4411 (gdb-buffer-type (window-buffer w)))
4412 (not (window-dedicated-p w)))))))
4413 (if existing-window
4414 (set-window-buffer existing-window buf)
4415 (let ((dedicated-window
4416 (get-window-with-predicate
4417 #'(lambda (w)
4418 (eq buf-type
4419 (gdb-buffer-type (window-buffer w)))))))
4420 (if dedicated-window
4421 (set-window-buffer
4422 (split-window dedicated-window nil split-horizontal) buf)
4423 (gdb-display-buffer buf))))))
4424 (error "Null buffer")))
4426 ;;; Shared keymap initialization:
4428 (let ((menu (make-sparse-keymap "GDB-Windows")))
4429 (define-key gud-menu-map [displays]
4430 `(menu-item "GDB-Windows" ,menu
4431 :visible (eq gud-minor-mode 'gdbmi)))
4432 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4433 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4434 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4435 (define-key menu [disassembly]
4436 '("Disassembly" . gdb-display-disassembly-buffer))
4437 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4438 (define-key menu [inferior]
4439 '("IO" . gdb-display-io-buffer))
4440 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4441 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4442 (define-key menu [breakpoints]
4443 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4445 (let ((menu (make-sparse-keymap "GDB-Frames")))
4446 (define-key gud-menu-map [frames]
4447 `(menu-item "GDB-Frames" ,menu
4448 :visible (eq gud-minor-mode 'gdbmi)))
4449 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4450 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4451 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4452 (define-key menu [disassembly]
4453 '("Disassembly" . gdb-frame-disassembly-buffer))
4454 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4455 (define-key menu [inferior]
4456 '("IO" . gdb-frame-io-buffer))
4457 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4458 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4459 (define-key menu [breakpoints]
4460 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4462 (let ((menu (make-sparse-keymap "GDB-MI")))
4463 (define-key menu [gdb-customize]
4464 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4465 :help "Customize Gdb Graphical Mode options."))
4466 (define-key menu [gdb-many-windows]
4467 '(menu-item "Display Other Windows" gdb-many-windows
4468 :help "Toggle display of locals, stack and breakpoint information"
4469 :button (:toggle . gdb-many-windows)))
4470 (define-key menu [gdb-restore-windows]
4471 '(menu-item "Restore Window Layout" gdb-restore-windows
4472 :help "Restore standard layout for debug session."))
4473 (define-key menu [sep1]
4474 '(menu-item "--"))
4475 (define-key menu [all-threads]
4476 '(menu-item "GUD controls all threads"
4477 (lambda ()
4478 (interactive)
4479 (setq gdb-gud-control-all-threads t))
4480 :help "GUD start/stop commands apply to all threads"
4481 :button (:radio . gdb-gud-control-all-threads)))
4482 (define-key menu [current-thread]
4483 '(menu-item "GUD controls current thread"
4484 (lambda ()
4485 (interactive)
4486 (setq gdb-gud-control-all-threads nil))
4487 :help "GUD start/stop commands apply to current thread only"
4488 :button (:radio . (not gdb-gud-control-all-threads))))
4489 (define-key menu [sep2]
4490 '(menu-item "--"))
4491 (define-key menu [gdb-customize-reasons]
4492 '(menu-item "Customize switching..."
4493 (lambda ()
4494 (interactive)
4495 (customize-option 'gdb-switch-reasons))))
4496 (define-key menu [gdb-switch-when-another-stopped]
4497 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4498 gdb-switch-when-another-stopped
4499 "Automatically switch to stopped thread"
4500 "GDB thread switching %s"
4501 "Switch to stopped thread"))
4502 (define-key gud-menu-map [mi]
4503 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4505 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4506 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4507 ;; tool-bar-local-item-from-menu here because it appends new buttons
4508 ;; to toolbar from right to left while we want our A/T throttle to
4509 ;; show up right before Run button.
4510 (define-key-after gud-tool-bar-map [all-threads]
4511 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4512 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4513 :visible (and (eq gud-minor-mode 'gdbmi)
4514 gdb-non-stop
4515 (not gdb-gud-control-all-threads)))
4516 'run)
4518 (define-key-after gud-tool-bar-map [current-thread]
4519 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4520 :image (find-image '((:type xpm :file "gud/all.xpm")))
4521 :visible (and (eq gud-minor-mode 'gdbmi)
4522 gdb-non-stop
4523 gdb-gud-control-all-threads))
4524 'all-threads)
4526 (defun gdb-frame-gdb-buffer ()
4527 "Display GUD buffer in another frame."
4528 (interactive)
4529 (display-buffer-other-frame gud-comint-buffer))
4531 (defun gdb-display-gdb-buffer ()
4532 "Display GUD buffer."
4533 (interactive)
4534 (pop-to-buffer gud-comint-buffer nil 0))
4536 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4537 "Set buffer of selected window to NAME and dedicate window.
4539 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4540 window is dedicated."
4541 (unless window (setq window (selected-window)))
4542 (when ignore-dedicated
4543 (set-window-dedicated-p window nil))
4544 (set-window-buffer window (get-buffer name))
4545 (set-window-dedicated-p window t))
4547 (defun gdb-setup-windows ()
4548 "Layout the window pattern for option `gdb-many-windows'."
4549 (gdb-get-buffer-create 'gdb-locals-buffer)
4550 (gdb-get-buffer-create 'gdb-stack-buffer)
4551 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4552 (set-window-dedicated-p (selected-window) nil)
4553 (switch-to-buffer gud-comint-buffer)
4554 (delete-other-windows)
4555 (let ((win0 (selected-window))
4556 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4557 (win2 (split-window nil ( / (window-height) 3)))
4558 (win3 (split-window-right)))
4559 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4560 (select-window win2)
4561 (set-window-buffer
4562 win2
4563 (if gud-last-last-frame
4564 (gud-find-file (car gud-last-last-frame))
4565 (if gdb-main-file
4566 (gud-find-file gdb-main-file)
4567 ;; Put buffer list in window if we
4568 ;; can't find a source file.
4569 (list-buffers-noselect))))
4570 (setq gdb-source-window (selected-window))
4571 (let ((win4 (split-window-right)))
4572 (gdb-set-window-buffer
4573 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4574 (select-window win1)
4575 (gdb-set-window-buffer (gdb-stack-buffer-name))
4576 (let ((win5 (split-window-right)))
4577 (gdb-set-window-buffer (if gdb-show-threads-by-default
4578 (gdb-threads-buffer-name)
4579 (gdb-breakpoints-buffer-name))
4580 nil win5))
4581 (select-window win0)))
4583 (define-minor-mode gdb-many-windows
4584 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4585 In this case it starts with two windows: one displaying the GUD
4586 buffer and the other with the source file with the main routine
4587 of the debugged program. Non-nil means display the layout shown for
4588 `gdb'."
4589 :global t
4590 :group 'gdb
4591 :version "22.1"
4592 (if (and gud-comint-buffer
4593 (buffer-name gud-comint-buffer))
4594 (ignore-errors
4595 (gdb-restore-windows))))
4597 (defun gdb-restore-windows ()
4598 "Restore the basic arrangement of windows used by gdb.
4599 This arrangement depends on the value of option `gdb-many-windows'."
4600 (interactive)
4601 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4602 (delete-other-windows)
4603 (if gdb-many-windows
4604 (gdb-setup-windows)
4605 (when (or gud-last-last-frame gdb-show-main)
4606 (let ((win (split-window)))
4607 (set-window-buffer
4609 (if gud-last-last-frame
4610 (gud-find-file (car gud-last-last-frame))
4611 (gud-find-file gdb-main-file)))
4612 (setq gdb-source-window win)))))
4614 ;; Called from `gud-sentinel' in gud.el:
4615 (defun gdb-reset ()
4616 "Exit a debugging session cleanly.
4617 Kills the gdb buffers, and resets variables and the source buffers."
4618 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4619 ;; process. This pty must be deleted explicitly.
4620 (let ((pty (get-process "gdb-inferior")))
4621 (if pty (delete-process pty)))
4622 ;; Find gdb-mi buffers and kill them.
4623 (dolist (buffer (buffer-list))
4624 (unless (eq buffer gud-comint-buffer)
4625 (with-current-buffer buffer
4626 (if (eq gud-minor-mode 'gdbmi)
4627 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4628 (kill-buffer nil)
4629 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4630 (setq gud-minor-mode nil)
4631 (kill-local-variable 'tool-bar-map)
4632 (kill-local-variable 'gdb-define-alist))))))
4633 (setq gdb-disassembly-position nil)
4634 (setq overlay-arrow-variable-list
4635 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4636 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4637 (setq gdb-stack-position nil)
4638 (setq overlay-arrow-variable-list
4639 (delq 'gdb-stack-position overlay-arrow-variable-list))
4640 (setq gdb-thread-position nil)
4641 (setq overlay-arrow-variable-list
4642 (delq 'gdb-thread-position overlay-arrow-variable-list))
4643 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4644 (setq gud-running nil)
4645 (setq gdb-active-process nil)
4646 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4648 (defun gdb-get-source-file ()
4649 "Find the source file where the program starts and display it with related
4650 buffers, if required."
4651 (goto-char (point-min))
4652 (if (re-search-forward gdb-source-file-regexp nil t)
4653 (setq gdb-main-file (read (match-string 1))))
4654 (if gdb-many-windows
4655 (gdb-setup-windows)
4656 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4657 (and gdb-show-main
4658 gdb-main-file
4659 (display-buffer (gud-find-file gdb-main-file))))
4660 (gdb-force-mode-line-update
4661 (propertize "ready" 'face font-lock-variable-name-face)))
4663 ;;from put-image
4664 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4665 "Put string PUTSTRING in front of POS in the current buffer.
4666 PUTSTRING is displayed by putting an overlay into the current buffer with a
4667 `before-string' string that has a `display' property whose value is
4668 PUTSTRING."
4669 (let ((string (make-string 1 ?x))
4670 (buffer (current-buffer)))
4671 (setq putstring (copy-sequence putstring))
4672 (let ((overlay (make-overlay pos pos buffer))
4673 (prop (or dprop
4674 (list (list 'margin 'left-margin) putstring))))
4675 (put-text-property 0 1 'display prop string)
4676 (if sprops
4677 (add-text-properties 0 1 sprops string))
4678 (overlay-put overlay 'put-break t)
4679 (overlay-put overlay 'before-string string))))
4681 ;;from remove-images
4682 (defun gdb-remove-strings (start end &optional buffer)
4683 "Remove strings between START and END in BUFFER.
4684 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4685 BUFFER nil or omitted means use the current buffer."
4686 (unless buffer
4687 (setq buffer (current-buffer)))
4688 (dolist (overlay (overlays-in start end))
4689 (when (overlay-get overlay 'put-break)
4690 (delete-overlay overlay))))
4692 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4693 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4694 (start (- (car posns) 1))
4695 (end (+ (cdr posns) 1))
4696 (putstring (if enabled "B" "b"))
4697 (source-window (get-buffer-window (current-buffer) 0)))
4698 (add-text-properties
4699 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4700 putstring)
4701 (if enabled
4702 (add-text-properties
4703 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4704 (add-text-properties
4705 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4706 (gdb-remove-breakpoint-icons start end)
4707 (if (display-images-p)
4708 (if (>= (or left-fringe-width
4709 (if source-window (car (window-fringes source-window)))
4710 gdb-buffer-fringe-width) 8)
4711 (gdb-put-string
4712 nil (1+ start)
4713 `(left-fringe breakpoint
4714 ,(if enabled
4715 'breakpoint-enabled
4716 'breakpoint-disabled))
4717 'gdb-bptno bptno
4718 'gdb-enabled enabled)
4719 (when (< left-margin-width 2)
4720 (save-current-buffer
4721 (setq left-margin-width 2)
4722 (if source-window
4723 (set-window-margins
4724 source-window
4725 left-margin-width right-margin-width))))
4726 (put-image
4727 (if enabled
4728 (or breakpoint-enabled-icon
4729 (setq breakpoint-enabled-icon
4730 (find-image `((:type xpm :data
4731 ,breakpoint-xpm-data
4732 :ascent 100 :pointer hand)
4733 (:type pbm :data
4734 ,breakpoint-enabled-pbm-data
4735 :ascent 100 :pointer hand)))))
4736 (or breakpoint-disabled-icon
4737 (setq breakpoint-disabled-icon
4738 (find-image `((:type xpm :data
4739 ,breakpoint-xpm-data
4740 :conversion disabled
4741 :ascent 100 :pointer hand)
4742 (:type pbm :data
4743 ,breakpoint-disabled-pbm-data
4744 :ascent 100 :pointer hand))))))
4745 (+ start 1)
4746 putstring
4747 'left-margin))
4748 (when (< left-margin-width 2)
4749 (save-current-buffer
4750 (setq left-margin-width 2)
4751 (let ((window (get-buffer-window (current-buffer) 0)))
4752 (if window
4753 (set-window-margins
4754 window left-margin-width right-margin-width)))))
4755 (gdb-put-string
4756 (propertize putstring
4757 'face (if enabled
4758 'breakpoint-enabled 'breakpoint-disabled))
4759 (1+ start)))))
4761 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4762 (gdb-remove-strings start end)
4763 (if (display-images-p)
4764 (remove-images start end))
4765 (when remove-margin
4766 (setq left-margin-width 0)
4767 (let ((window (get-buffer-window (current-buffer) 0)))
4768 (if window
4769 (set-window-margins
4770 window left-margin-width right-margin-width)))))
4773 ;;; Functions for inline completion.
4775 (defvar gud-gdb-fetch-lines-in-progress)
4776 (defvar gud-gdb-fetch-lines-string)
4777 (defvar gud-gdb-fetch-lines-break)
4778 (defvar gud-gdb-fetched-lines)
4780 (defun gud-gdbmi-completions (context command)
4781 "Completion table for GDB/MI commands.
4782 COMMAND is the prefix for which we seek completion.
4783 CONTEXT is the text before COMMAND on the line."
4784 (let ((gud-gdb-fetch-lines-in-progress t)
4785 (gud-gdb-fetch-lines-string nil)
4786 (gud-gdb-fetch-lines-break (length context))
4787 (gud-gdb-fetched-lines nil)
4788 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4789 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4790 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4791 (gdb-input (concat "complete " context command)
4792 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4793 (while gud-gdb-fetch-lines-in-progress
4794 (accept-process-output (get-buffer-process gud-comint-buffer))))
4795 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4797 (defun gud-gdbmi-fetch-lines-filter (string)
4798 "Custom filter function for `gud-gdbmi-completions'."
4799 (setq string (concat gud-gdb-fetch-lines-string
4800 (gud-gdbmi-marker-filter string)))
4801 (while (string-match "\n" string)
4802 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4803 gud-gdb-fetched-lines)
4804 (setq string (substring string (match-end 0))))
4807 (provide 'gdb-mi)
4809 ;;; gdb-mi.el ends here