1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
7 ;; Keywords: unix, tools
9 ;; This file is part of GNU Emacs.
11 ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file was written by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It was further developed
30 ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
31 ;; of Code 2009 Project "Emacs GDB/MI migration".
35 ;; This mode acts as a graphical user interface to GDB. You can interact with
36 ;; GDB through the GUD buffer in the usual way, but there are also further
37 ;; buffers which control the execution and describe the state of your program.
38 ;; It separates the input/output of your program from that of GDB and displays
39 ;; expressions and their current values in their own buffers. It also uses
40 ;; features of Emacs 21 such as the fringe/display margin for breakpoints, and
41 ;; the toolbar (see the GDB Graphical Interface section in the Emacs info
44 ;; M-x gdb will start the debugger.
46 ;; This file uses GDB/MI as the primary interface to GDB. It runs gdb with
47 ;; GDB/MI (-interp=mi) and access CLI using "-interpreter-exec console
48 ;; cli-command". This code replaces gdb-ui.el and uses MI tokens instead
49 ;; of queues. Eventually MI should be asynchronous.
53 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
54 ;; explicitly in your program if you want timely display of I/O in Emacs.
55 ;; Alternatively you can make the output stream unbuffered, for example, by
59 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
62 ;; and compiling with -DUNBUFFERED while debugging.
64 ;; If you are using Cygwin GDB and find that the source is not being displayed
65 ;; in Emacs when you step through it, possible solutions are to:
67 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
68 ;; (Since 22.1 Emacs builds under Cygwin.)
69 ;; 2) Use MinGW GDB instead.
70 ;; 3) Use cygwin-mount.el
74 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of Mac OSX.
76 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
77 ;; as earlier versions do not compile on Mac OSX.
81 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
82 ;; line information, e.g., a routine in libc (just a TODO item).
85 ;; 2) Watch windows to work with threads.
86 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
87 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
94 (eval-when-compile (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
129 (defvar gdb-frame-number nil
130 "Selected frame level for main current thread.
132 Updated according to the following rules:
134 When a thread is selected or current thread stops, set to \"0\".
136 When current thread goes running (and possibly exits eventually),
139 May be manually changed by user with `gdb-select-frame'.")
141 (defvar gdb-frame-address nil
"Identity of frame for watch expression.")
143 ;; Used to show overlay arrow in source buffer. All set in
144 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
145 ;; these but rely on buffer-local thread information instead.
146 (defvar gdb-selected-frame nil
147 "Name of selected function for main current thread.")
148 (defvar gdb-selected-file nil
149 "Name of selected file for main current thread.")
150 (defvar gdb-selected-line nil
151 "Number of selected line for main current thread.")
153 (defvar gdb-threads-list nil
154 "Associative list of threads provided by \"-thread-info\" MI command.
156 Keys are thread numbers (in strings) and values are structures as
157 returned from -thread-info by `gdb-json-partial-output'. Updated in
158 `gdb-thread-list-handler-custom'.")
160 (defvar gdb-running-threads-count nil
161 "Number of currently running threads.
163 If nil, no information is available.
165 Updated in `gdb-thread-list-handler-custom'.")
167 (defvar gdb-stopped-threads-count nil
168 "Number of currently stopped threads.
170 See also `gdb-running-threads-count'.")
172 (defvar gdb-breakpoints-list nil
173 "Associative list of breakpoints provided by \"-break-list\" MI command.
175 Keys are breakpoint numbers (in string) and values are structures
176 as returned from \"-break-list\" by `gdb-json-partial-output'
177 \(\"body\" field is used). Updated in
178 `gdb-breakpoints-list-handler-custom'.")
180 (defvar gdb-current-language nil
)
181 (defvar gdb-var-list nil
182 "List of variables in watch window.
183 Each element has the form
184 (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
185 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
186 address for root variables.")
187 (defvar gdb-main-file nil
"Source file from which program execution begins.")
189 ;; Overlay arrow markers
190 (defvar gdb-stack-position nil
)
191 (defvar gdb-thread-position nil
)
192 (defvar gdb-disassembly-position nil
)
194 (defvar gdb-location-alist nil
195 "Alist of breakpoint numbers and full filenames.
196 Only used for files that Emacs can't find.")
197 (defvar gdb-active-process nil
198 "GUD tooltips display variable values when t, and macro definitions otherwise.")
199 (defvar gdb-error
"Non-nil when GDB is reporting an error.")
200 (defvar gdb-macro-info nil
201 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
202 (defvar gdb-register-names nil
"List of register names.")
203 (defvar gdb-changed-registers nil
204 "List of changed register numbers (strings).")
205 (defvar gdb-buffer-fringe-width nil
)
206 (defvar gdb-last-command nil
)
207 (defvar gdb-prompt-name nil
)
208 (defvar gdb-token-number
0)
209 (defvar gdb-handler-alist
'())
210 (defvar gdb-handler-number nil
)
211 (defvar gdb-source-file-list nil
212 "List of source files for the current executable.")
213 (defvar gdb-first-done-or-error t
)
214 (defvar gdb-source-window nil
)
215 (defvar gdb-inferior-status nil
)
216 (defvar gdb-continuation nil
)
217 (defvar gdb-supports-non-stop nil
)
218 (defvar gdb-filter-output nil
219 "Message to be shown in GUD console.
221 This variable is updated in `gdb-done-or-error' and returned by
222 `gud-gdbmi-marker-filter'.")
224 (defvar gdb-non-stop nil
225 "Indicates whether current GDB session is using non-stop mode.
227 It is initialized to `gdb-non-stop-setting' at the beginning of
230 (defvar-local gdb-buffer-type nil
231 "One of the symbols bound in `gdb-buffer-rules'.")
233 (defvar gdb-output-sink
'nil
234 "The disposition of the output of the current gdb command.
235 Possible values are these symbols:
237 `user' -- gdb output should be copied to the GUD buffer
240 `emacs' -- output should be collected in the partial-output-buffer
241 for subsequent processing by a command. This is the
242 disposition of output generated by commands that
243 gdb mode sends to gdb on its own behalf.")
245 ;; Pending triggers prevent congestion: Emacs won't send two similar
246 ;; consecutive requests.
248 (defvar gdb-pending-triggers
'()
249 "A list of trigger functions which have not yet been handled.
251 Elements are either function names or pairs (buffer . function)")
253 (defmacro gdb-add-pending
(item)
254 `(push ,item gdb-pending-triggers
))
255 (defmacro gdb-pending-p
(item)
256 `(member ,item gdb-pending-triggers
))
257 (defmacro gdb-delete-pending
(item)
258 `(setq gdb-pending-triggers
259 (delete ,item gdb-pending-triggers
)))
261 (defmacro gdb-wait-for-pending
(&rest body
)
262 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
264 This function checks `gdb-pending-triggers' value every
265 `gdb-wait-for-pending' seconds."
269 (if (not gdb-pending-triggers
)
271 (gdb-wait-for-pending ,@body
)))))
275 (defmacro gdb-add-subscriber
(publisher subscriber
)
276 "Register new PUBLISHER's SUBSCRIBER.
278 SUBSCRIBER must be a pair, where cdr is a function of one
279 argument (see `gdb-emit-signal')."
280 `(add-to-list ',publisher
,subscriber t
))
282 (defmacro gdb-delete-subscriber
(publisher subscriber
)
283 "Unregister SUBSCRIBER from PUBLISHER."
284 `(setq ,publisher
(delete ,subscriber
287 (defun gdb-get-subscribers (publisher)
290 (defun gdb-emit-signal (publisher &optional signal
)
291 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
292 (dolist (subscriber (gdb-get-subscribers publisher
))
293 (funcall (cdr subscriber
) signal
)))
295 (defvar gdb-buf-publisher
'()
296 "Used to invalidate GDB buffers by emitting a signal in `gdb-update'.
297 Must be a list of pairs with cars being buffers and cdr's being
298 valid signal handlers.")
301 "GDB graphical interface"
303 :link
'(info-link "(emacs)GDB Graphical Interface")
306 (defgroup gdb-non-stop nil
307 "GDB non-stop debugging settings"
311 (defgroup gdb-buffers nil
316 (defcustom gdb-debug-log-max
128
317 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
319 :type
'(choice (integer :tag
"Number of elements")
320 (const :tag
"Unlimited" nil
))
323 (defcustom gdb-non-stop-setting t
324 "When in non-stop mode, stopped threads can be examined while
325 other threads continue to execute.
327 GDB session needs to be restarted for this setting to take effect."
332 ;; TODO Some commands can't be called with --all (give a notice about
333 ;; it in setting doc)
334 (defcustom gdb-gud-control-all-threads t
335 "When non-nil, GUD execution commands affect all threads when
336 in non-stop mode. Otherwise, only current thread is affected."
341 (defcustom gdb-switch-reasons t
342 "List of stop reasons for which Emacs should switch thread.
343 When t, switch to stopped thread no matter what the reason was.
344 When nil, never switch to stopped thread automatically.
346 This setting is used in non-stop mode only. In all-stop mode,
347 Emacs always switches to the thread which caused the stop."
348 ;; exited, exited-normally and exited-signaled are not
349 ;; thread-specific stop reasons and therefore are not included in
352 (const :tag
"All reasons" t
)
353 (set :tag
"Selection of reasons..."
354 (const :tag
"A breakpoint was reached." "breakpoint-hit")
355 (const :tag
"A watchpoint was triggered." "watchpoint-trigger")
356 (const :tag
"A read watchpoint was triggered."
357 "read-watchpoint-trigger")
358 (const :tag
"An access watchpoint was triggered."
359 "access-watchpoint-trigger")
360 (const :tag
"Function finished execution." "function-finished")
361 (const :tag
"Location reached." "location-reached")
362 (const :tag
"Watchpoint has gone out of scope"
364 (const :tag
"End of stepping range reached."
365 "end-stepping-range")
366 (const :tag
"Signal received (like interruption)."
368 (const :tag
"None" nil
))
371 :link
'(info-link "(gdb)GDB/MI Async Records"))
373 (defcustom gdb-stopped-functions nil
374 "List of functions called whenever GDB stops.
376 Each function takes one argument, a parsed MI response, which
377 contains fields of corresponding MI *stopped async record:
379 ((stopped-threads . \"all\")
381 (frame (line . \"38\")
382 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
383 (file . \"server.c\")
384 (args ((value . \"0x804b038\")
387 (addr . \"0x0804869e\"))
388 (reason . \"end-stepping-range\"))
390 Note that \"reason\" is only present in non-stop debugging mode.
392 `bindat-get-field' may be used to access the fields of response.
394 Each function is called after the new current thread was selected
395 and GDB buffers were updated in `gdb-stopped'."
396 :type
'(repeat function
)
399 :link
'(info-link "(gdb)GDB/MI Async Records"))
401 (defcustom gdb-switch-when-another-stopped t
402 "When nil, don't switch to stopped thread if some other
403 stopped thread is already selected."
408 (defcustom gdb-stack-buffer-locations t
409 "Show file information or library names in stack buffers."
414 (defcustom gdb-stack-buffer-addresses nil
415 "Show frame addresses in stack buffers."
420 (defcustom gdb-thread-buffer-verbose-names t
421 "Show long thread names in threads buffer."
426 (defcustom gdb-thread-buffer-arguments t
427 "Show function arguments in threads buffer."
432 (defcustom gdb-thread-buffer-locations t
433 "Show file information or library names in threads buffer."
438 (defcustom gdb-thread-buffer-addresses nil
439 "Show addresses for thread frames in threads buffer."
444 (defcustom gdb-show-threads-by-default nil
445 "Show threads list buffer instead of breakpoints list by default."
450 (defvar gdb-debug-log nil
451 "List of commands sent to and replies received from GDB.
452 Most recent commands are listed first. This list stores only the last
453 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
456 (define-minor-mode gdb-enable-debug
457 "Toggle logging of transaction between Emacs and Gdb.
458 The log is stored in `gdb-debug-log' as an alist with elements
459 whose cons is send, send-item or recv and whose cdr is the string
460 being transferred. This list may grow up to a size of
461 `gdb-debug-log-max' after which the oldest element (at the end of
462 the list) is deleted every time a new one is added (at the front)."
467 (defcustom gdb-cpp-define-alist-program
"gcc -E -dM -"
468 "Shell command for generating a list of defined macros in a source file.
469 This list is used to display the #define directive associated
470 with an identifier as a tooltip. It works in a debug session with
471 GDB, when `gud-tooltip-mode' is t.
473 Set `gdb-cpp-define-alist-flags' for any include paths or
479 (defcustom gdb-cpp-define-alist-flags
""
480 "Preprocessor flags for `gdb-cpp-define-alist-program'."
485 (defcustom gdb-create-source-file-list t
486 "Non-nil means create a list of files from which the executable was built.
487 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
488 line for a long time when starting, possibly because your executable was
489 built from a large number of files. This allows quicker initialization
490 but means that these files are not automatically enabled for debugging,
491 e.g., you won't be able to click in the fringe to set a breakpoint until
492 execution has already stopped there."
497 (defcustom gdb-show-main nil
498 "Non-nil means display source file containing the main routine at startup.
499 Also display the main routine in the disassembly buffer if present."
504 (defvar gdbmi-debug-mode nil
505 "When non-nil, print the messages sent/received from GDB/MI in *Messages*.")
507 (defun gdb-force-mode-line-update (status)
508 (let ((buffer gud-comint-buffer
))
509 (if (and buffer
(buffer-name buffer
))
510 (with-current-buffer buffer
511 (setq mode-line-process
513 (process-status (get-buffer-process buffer
)) status
))
514 ;; Force mode line redisplay soon.
515 (force-mode-line-update)))))
517 ;; These two are used for menu and toolbar
518 (defun gdb-control-all-threads ()
519 "Switch to non-stop/A mode."
521 (setq gdb-gud-control-all-threads t
)
522 ;; Actually forcing the tool-bar to update.
523 (force-mode-line-update)
524 (message "Now in non-stop/A mode."))
526 (defun gdb-control-current-thread ()
527 "Switch to non-stop/T mode."
529 (setq gdb-gud-control-all-threads nil
)
530 ;; Actually forcing the tool-bar to update.
531 (force-mode-line-update)
532 (message "Now in non-stop/T mode."))
534 (defun gdb-find-watch-expression ()
535 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list
))
536 (varnum (car var
)) expr
)
537 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum
)
538 (let ((var1 (assoc (match-string 1 varnum
) gdb-var-list
)) var2 varnumlet
539 (component-list (split-string (match-string 2 varnum
) "\\." t
)))
540 (setq expr
(nth 1 var1
))
541 (setq varnumlet
(car var1
))
542 (dolist (component component-list
)
543 (setq var2
(assoc varnumlet gdb-var-list
))
544 (setq expr
(concat expr
545 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2
))
546 (concat "[" component
"]")
547 (concat "." component
))))
548 (setq varnumlet
(concat varnumlet
"." component
)))
551 ;; noall is used for commands which don't take --all, but only
553 (defun gdb-gud-context-command (command &optional noall
)
554 "When `gdb-non-stop' is t, add --thread option to COMMAND if
555 `gdb-gud-control-all-threads' is nil and --all option otherwise.
556 If NOALL is t, always add --thread option no matter what
557 `gdb-gud-control-all-threads' value is.
559 When `gdb-non-stop' is nil, return COMMAND unchanged."
561 (if (and gdb-gud-control-all-threads
563 gdb-supports-non-stop
)
564 (concat command
" --all ")
565 (gdb-current-context-command command
))
568 (defmacro gdb-gud-context-call
(cmd1 &optional cmd2 noall noarg
)
569 "`gud-call' wrapper which adds --thread/--all options between
570 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
572 NOARG must be t when this macro is used outside `gud-def'"
574 (concat (gdb-gud-context-command ,cmd1
,noall
) " " ,cmd2
)
575 ,(when (not noarg
) 'arg
)))
577 (defun gdb--check-interpreter (proc string
)
578 (unless (zerop (length string
))
579 (let ((filter (process-get proc
'gud-normal-filter
)))
580 (set-process-filter proc filter
)
581 (unless (memq (aref string
0) '(?^ ?~ ?
@ ?
& ?
* ?
=))
582 ;; Apparently we're not running with -i=mi.
583 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
585 (setq string
(concat (propertize msg
'font-lock-face
'error
)
587 ;; Use the old gud-gbd filter, not because it works, but because it
588 ;; will properly display GDB's answers rather than hanging waiting for
589 ;; answers that aren't coming.
590 (set (make-local-variable 'gud-marker-filter
) #'gud-gdb-marker-filter
))
591 (funcall filter proc string
))))
593 (defvar gdb-control-level
0)
596 (defun gdb (command-line)
597 "Run gdb on program FILE in buffer *gud-FILE*.
598 The directory containing FILE becomes the initial working directory
599 and source-file directory for your debugger.
601 COMMAND-LINE is the shell command for starting the gdb session.
602 It should be a string consisting of the name of the gdb
603 executable followed by command line options. The command line
604 options should include \"-i=mi\" to use gdb's MI text interface.
605 Note that the old \"--annotate\" option is no longer supported.
607 If option `gdb-many-windows' is nil (the default value) then gdb just
608 pops up the GUD buffer unless `gdb-show-main' is t. In this case
609 it starts with two windows: one displaying the GUD buffer and the
610 other with the source file with the main routine of the inferior.
612 If option `gdb-many-windows' is t, regardless of the value of
613 `gdb-show-main', the layout below will appear. Keybindings are
614 shown in some of the buffers.
616 Watch expressions appear in the speedbar/slowbar.
618 The following commands help control operation :
620 `gdb-many-windows' - Toggle the number of windows gdb uses.
621 `gdb-restore-windows' - To restore the window layout.
623 See Info node `(emacs)GDB Graphical Interface' for a more
624 detailed description of this mode.
627 +----------------------------------------------------------------------+
629 +-----------------------------------+----------------------------------+
630 | GUD buffer (I/O of GDB) | Locals buffer |
634 +-----------------------------------+----------------------------------+
635 | Source buffer | I/O buffer (of debugged program) |
643 +-----------------------------------+----------------------------------+
644 | Stack buffer | Breakpoints buffer |
645 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
646 | | RET gdb-goto-breakpoint |
647 | | D gdb-delete-breakpoint |
648 +-----------------------------------+----------------------------------+"
650 (interactive (list (gud-query-cmdline 'gdb
)))
652 (when (and gud-comint-buffer
653 (buffer-name gud-comint-buffer
)
654 (get-buffer-process gud-comint-buffer
)
655 (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
)))
656 (gdb-restore-windows)
658 "Multiple debugging requires restarting in text command mode"))
660 (gud-common-init command-line nil
'gud-gdbmi-marker-filter
)
662 ;; Setup a temporary process filter to warn when GDB was not started
664 (let ((proc (get-buffer-process gud-comint-buffer
)))
665 (process-put proc
'gud-normal-filter
(process-filter proc
))
666 (set-process-filter proc
#'gdb--check-interpreter
))
668 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
669 (set (make-local-variable 'gdb-control-level
) 0)
670 (setq comint-input-sender
'gdb-send
)
671 (when (ring-empty-p comint-input-ring
) ; cf shell-mode
672 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
673 (if (eq system-type
'ms-dos
)
676 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
677 (hsize (getenv "HISTSIZE")))
678 (dolist (file (append '("~/.gdbinit")
679 (unless (string-equal (expand-file-name ".")
680 (expand-file-name "~"))
682 (if (file-readable-p (setq file
(expand-file-name file
)))
684 (insert-file-contents file
)
685 ;; TODO? check for "set history save\\( *on\\)?" and do
686 ;; not use history otherwise?
687 (while (re-search-forward
688 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t
)
689 (cond ((string-equal (match-string 1) "filename")
690 (setq hfile
(expand-file-name
692 (file-name-directory file
))))
693 ((string-equal (match-string 1) "size")
694 (setq hsize
(match-string 2))))))))
696 (integerp (setq hsize
(string-to-number hsize
)))
698 (set (make-local-variable 'comint-input-ring-size
) hsize
))
700 (set (make-local-variable 'comint-input-ring-file-name
) hfile
))
701 (comint-read-input-ring t
)))
702 (gud-def gud-tbreak
"tbreak %f:%l" "\C-t"
703 "Set temporary breakpoint at current line.")
705 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
706 "\C-j" "Set execution address to current line.")
708 (gud-def gud-up
"up %p" "<" "Up N stack frames (numeric arg).")
709 (gud-def gud-down
"down %p" ">" "Down N stack frames (numeric arg).")
710 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
711 (gud-def gud-pstar
"print* %e" nil
712 "Evaluate C dereferenced pointer expression at point.")
714 (gud-def gud-step
(gdb-gud-context-call "-exec-step" "%p" t
)
716 "Step one source line with display.")
717 (gud-def gud-stepi
(gdb-gud-context-call "-exec-step-instruction" "%p" t
)
719 "Step one instruction with display.")
720 (gud-def gud-next
(gdb-gud-context-call "-exec-next" "%p" t
)
722 "Step one line (skip functions).")
723 (gud-def gud-nexti
(gdb-gud-context-call "-exec-next-instruction" "%p" t
)
725 "Step one instruction (skip functions).")
726 (gud-def gud-cont
(gdb-gud-context-call "-exec-continue")
728 "Continue with display.")
729 (gud-def gud-finish
(gdb-gud-context-call "-exec-finish" nil t
)
731 "Finish executing current function.")
732 (gud-def gud-run
"-exec-run"
736 (gud-def gud-break
(if (not (string-match "Disassembly" mode-name
))
737 (gud-call "break %f:%l" arg
)
741 (gud-call "break *%a" arg
)))
742 "\C-b" "Set breakpoint at current line or address.")
744 (gud-def gud-remove
(if (not (string-match "Disassembly" mode-name
))
745 (gud-call "clear %f:%l" arg
)
749 (gud-call "clear *%a" arg
)))
750 "\C-d" "Remove breakpoint at current line or address.")
752 ;; -exec-until doesn't support --all yet
753 (gud-def gud-until
(if (not (string-match "Disassembly" mode-name
))
754 (gud-call "-exec-until %f:%l" arg
)
758 (gud-call "-exec-until *%a" arg
)))
759 "\C-u" "Continue to current line or address.")
760 ;; TODO Why arg here?
762 gud-go
(gud-call (if gdb-active-process
763 (gdb-gud-context-command "-exec-continue")
765 nil
"Start or continue execution.")
767 ;; For debugging Emacs only.
771 "pp " (if (eq (buffer-local-value
772 'major-mode
(window-buffer)) 'speedbar-mode
)
773 (gdb-find-watch-expression) "%e")) arg
)
774 nil
"Print the Emacs s-expression.")
776 (define-key gud-minor-mode-map
[left-margin mouse-1
]
777 'gdb-mouse-set-clear-breakpoint
)
778 (define-key gud-minor-mode-map
[left-fringe mouse-1
]
779 'gdb-mouse-set-clear-breakpoint
)
780 (define-key gud-minor-mode-map
[left-margin C-mouse-1
]
781 'gdb-mouse-toggle-breakpoint-margin
)
782 (define-key gud-minor-mode-map
[left-fringe C-mouse-1
]
783 'gdb-mouse-toggle-breakpoint-fringe
)
785 (define-key gud-minor-mode-map
[left-margin drag-mouse-1
]
787 (define-key gud-minor-mode-map
[left-fringe drag-mouse-1
]
789 (define-key gud-minor-mode-map
[left-margin mouse-3
]
791 (define-key gud-minor-mode-map
[left-fringe mouse-3
]
794 (define-key gud-minor-mode-map
[left-margin C-drag-mouse-1
]
796 (define-key gud-minor-mode-map
[left-fringe C-drag-mouse-1
]
798 (define-key gud-minor-mode-map
[left-fringe C-mouse-3
]
800 (define-key gud-minor-mode-map
[left-margin C-mouse-3
]
803 (set (make-local-variable 'gud-gdb-completion-function
)
804 'gud-gdbmi-completions
)
806 (add-hook 'completion-at-point-functions
#'gud-gdb-completion-at-point
808 (local-set-key "\C-i" 'completion-at-point
)
810 (local-set-key [remap comint-delchar-or-maybe-eof
] 'gdb-delchar-or-quit
)
812 (setq gdb-first-prompt t
)
813 (setq gud-running nil
)
817 (run-hooks 'gdb-mode-hook
))
821 (setq gdb-selected-frame nil
823 gdb-thread-number nil
825 gdb-pending-triggers nil
826 gdb-output-sink
'user
827 gdb-location-alist nil
828 gdb-source-file-list nil
831 gdb-handler-alist
'()
832 gdb-handler-number nil
834 gdb-first-done-or-error t
835 gdb-buffer-fringe-width
(car (window-fringes))
837 gdb-source-window nil
838 gdb-inferior-status nil
840 gdb-buf-publisher
'()
842 gdb-breakpoints-list
'()
843 gdb-register-names
'()
844 gdb-non-stop gdb-non-stop-setting
)
848 (setq gdb-buffer-type
'gdbmi
)
850 (gdb-force-mode-line-update
851 (propertize "initializing..." 'face font-lock-variable-name-face
))
853 (gdb-get-buffer-create 'gdb-inferior-io
)
854 (gdb-clear-inferior-io)
855 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
857 (when (eq system-type
'windows-nt
)
858 ;; Don't create a separate console window for the debuggee.
859 (gdb-input "-gdb-set new-console off" 'ignore
)
860 ;; Force GDB to behave as if its input and output stream were
861 ;; connected to a TTY device (since on Windows we use pipes for
862 ;; communicating with GDB).
863 (gdb-input "-gdb-set interactive-mode on" 'ignore
))
864 (gdb-input "-gdb-set height 0" 'ignore
)
867 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler
))
869 (gdb-input "-enable-pretty-printing" 'ignore
)
871 ;; Find source file and compilation directory here.
872 (if gdb-create-source-file-list
873 ;; Needs GDB 6.2 onwards.
874 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list
))
875 ;; Needs GDB 6.0 onwards.
876 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file
)
877 (gdb-input "-gdb-show prompt" 'gdb-get-prompt
))
879 (defun gdb-non-stop-handler ()
880 (goto-char (point-min))
881 (if (re-search-forward "No symbol" nil t
)
884 "This version of GDB doesn't support non-stop mode. Turning it off.")
885 (setq gdb-non-stop nil
)
886 (setq gdb-supports-non-stop nil
))
887 (setq gdb-supports-non-stop t
)
888 (gdb-input "-gdb-set target-async 1" 'ignore
)
889 (gdb-input "-list-target-features" 'gdb-check-target-async
)))
891 (defun gdb-check-target-async ()
892 (goto-char (point-min))
893 (unless (re-search-forward "async" nil t
)
895 "Target doesn't support non-stop mode. Turning it off.")
896 (setq gdb-non-stop nil
)
897 (gdb-input "-gdb-set non-stop 0" 'ignore
)))
899 (defun gdb-delchar-or-quit (arg)
900 "Delete ARG characters or send a quit command to GDB.
901 Send a quit only if point is at the end of the buffer, there is
902 no input, and GDB is waiting for input."
904 (unless (and (eq (current-buffer) gud-comint-buffer
)
905 (eq gud-minor-mode
'gdbmi
))
906 (error "Not in a GDB-MI buffer"))
907 (let ((proc (get-buffer-process gud-comint-buffer
)))
908 (if (and (eobp) proc
(process-live-p proc
)
910 (= (point) (marker-position (process-mark proc
))))
911 ;; Sending an EOF does not work with GDB-MI; submit an
912 ;; explicit quit command.
915 (comint-send-input t t
))
918 (defvar gdb-define-alist nil
"Alist of #define directives for GUD tooltips.")
920 (defun gdb-create-define-alist ()
921 "Create an alist of #define directives for GUD tooltips."
922 (let* ((file (buffer-file-name))
924 (with-output-to-string
925 (with-current-buffer standard-output
928 ;; call-process doesn't work with remote file names.
929 (not (file-remote-p default-directory
))
930 (call-process shell-file-name file
931 (list t nil
) nil
"-c"
932 (concat gdb-cpp-define-alist-program
" "
933 gdb-cpp-define-alist-flags
))))))
934 (define-list (split-string output
"\n" t
))
936 (setq gdb-define-alist nil
)
937 (dolist (define define-list
)
938 (setq name
(nth 1 (split-string define
"[( ]")))
939 (push (cons name define
) gdb-define-alist
))))
941 (declare-function tooltip-show
"tooltip" (text &optional use-echo-area
))
943 (defun gdb-tooltip-print (expr)
944 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
945 (goto-char (point-min))
947 ((re-search-forward ".*value=\\(\".*\"\\)" nil t
)
949 (concat expr
" = " (read (match-string 1)))
950 (or gud-tooltip-echo-area
951 (not (display-graphic-p)))))
952 ((re-search-forward "msg=\\(\".+\"\\)$" nil t
)
953 (tooltip-show (read (match-string 1))
954 (or gud-tooltip-echo-area
955 (not (display-graphic-p))))))))
957 ;; If expr is a macro for a function don't print because of possible dangerous
958 ;; side-effects. Also printing a function within a tooltip generates an
959 ;; unexpected starting annotation (phase error).
960 (defun gdb-tooltip-print-1 (expr)
961 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
962 (goto-char (point-min))
963 (if (search-forward "expands to: " nil t
)
964 (unless (looking-at "\\S-+.*(.*).*")
965 (gdb-input (concat "-data-evaluate-expression \"" expr
"\"")
966 `(lambda () (gdb-tooltip-print ,expr
)))))))
968 (defun gdb-init-buffer ()
969 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
970 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
971 (when gud-tooltip-mode
972 (make-local-variable 'gdb-define-alist
)
973 (gdb-create-define-alist)
974 (add-hook 'after-save-hook
'gdb-create-define-alist nil t
)))
976 (defmacro gdb--if-arrow
(arrow-position start-posn end-posn
&rest body
)
978 (let ((buffer (make-symbol "buffer")))
980 (let ((,buffer
(marker-buffer ,arrow-position
)))
981 (if (equal ,buffer
(window-buffer (posn-window ,end-posn
)))
982 (with-current-buffer ,buffer
983 (when (or (equal ,start-posn
,end-posn
)
984 (equal (posn-point ,start-posn
)
985 (marker-position ,arrow-position
)))
988 (defun gdb-mouse-until (event)
989 "Continue running until a source line past the current line.
990 The destination source line can be selected either by clicking
991 with mouse-3 on the fringe/margin or dragging the arrow
992 with mouse-1 (default bindings)."
994 (let ((start (event-start event
))
995 (end (event-end event
)))
996 (gdb--if-arrow gud-overlay-arrow-position start end
997 (let ((line (line-number-at-pos (posn-point end
))))
998 (gud-call (concat "until " (number-to-string line
)))))
999 (gdb--if-arrow gdb-disassembly-position start end
1001 (goto-char (point-min))
1002 (forward-line (1- (line-number-at-pos (posn-point end
))))
1004 (gud-call (concat "until *%a"))))))
1006 (defun gdb-mouse-jump (event)
1007 "Set execution address/line.
1008 The destination source line can be selected either by clicking with C-mouse-3
1009 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1010 Unlike `gdb-mouse-until' the destination address can be before the current
1011 line, and no execution takes place."
1013 (let ((start (event-start event
))
1014 (end (event-end event
)))
1015 (gdb--if-arrow gud-overlay-arrow-position start end
1016 (let ((line (line-number-at-pos (posn-point end
))))
1017 (gud-call (concat "tbreak " (number-to-string line
)))
1018 (gud-call (concat "jump " (number-to-string line
)))))
1019 (gdb--if-arrow gdb-disassembly-position start end
1021 (goto-char (point-min))
1022 (forward-line (1- (line-number-at-pos (posn-point end
))))
1024 (gud-call (concat "tbreak *%a"))
1025 (gud-call (concat "jump *%a"))))))
1027 (defcustom gdb-show-changed-values t
1028 "If non-nil change the face of out of scope variables and changed values.
1029 Out of scope variables are suppressed with `shadow' face.
1030 Changed values are highlighted with the face `font-lock-warning-face'."
1035 (defcustom gdb-max-children
40
1036 "Maximum number of children before expansion requires confirmation."
1041 (defcustom gdb-delete-out-of-scope t
1042 "If non-nil delete watch expressions automatically when they go out of scope."
1047 (define-minor-mode gdb-speedbar-auto-raise
1048 "Minor mode to automatically raise the speedbar for watch expressions.
1049 With prefix argument ARG, automatically raise speedbar if ARG is
1050 positive, otherwise don't automatically raise it."
1055 (defcustom gdb-use-colon-colon-notation nil
1056 "If non-nil use FUN::VAR format to display variables in the speedbar."
1061 (define-key gud-minor-mode-map
"\C-c\C-w" 'gud-watch
)
1062 (define-key global-map
(vconcat gud-key-prefix
"\C-w") 'gud-watch
)
1064 (declare-function tooltip-identifier-from-point
"tooltip" (point))
1066 (defun gud-watch (&optional arg event
)
1067 "Watch expression at point.
1068 With arg, enter name of variable to be watched in the minibuffer."
1069 (interactive (list current-prefix-arg last-input-event
))
1070 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer
)))
1071 (if (eq minor-mode
'gdbmi
)
1073 (if event
(posn-set-point (event-end event
)))
1075 (save-selected-window
1078 (completing-read "Name of variable: "
1079 'gud-gdb-complete-command
)
1080 (if (and transient-mark-mode mark-active
)
1081 (buffer-substring (region-beginning) (region-end))
1082 (concat (if (derived-mode-p 'gdb-registers-mode
) "$")
1083 (tooltip-identifier-from-point (point)))))))
1084 (set-text-properties 0 (length expr
) nil expr
)
1085 (gdb-input (concat "-var-create - * " expr
"")
1086 `(lambda () (gdb-var-create-handler ,expr
))))))
1087 (message "gud-watch is a no-op in this mode."))))
1089 (defun gdb-var-create-handler (expr)
1090 (let* ((result (gdb-json-partial-output)))
1091 (if (not (bindat-get-field result
'msg
))
1093 (list (bindat-get-field result
'name
)
1094 (if (and (string-equal gdb-current-language
"c")
1095 gdb-use-colon-colon-notation gdb-selected-frame
)
1096 (setq expr
(concat gdb-selected-frame
"::" expr
))
1098 (bindat-get-field result
'numchild
)
1099 (bindat-get-field result
'type
)
1100 (bindat-get-field result
'value
)
1102 (bindat-get-field result
'has_more
)
1103 gdb-frame-address
)))
1104 (push var gdb-var-list
)
1106 (unless (string-equal
1107 speedbar-initial-expansion-list-name
"GUD")
1108 (speedbar-change-initial-expansion-list "GUD")))
1109 (message-box "No symbol \"%s\" in current context." expr
))))
1111 (defun gdb-speedbar-update ()
1112 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
)
1113 (not (gdb-pending-p 'gdb-speedbar-timer
)))
1114 ;; Dummy command to update speedbar even when idle.
1115 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn
)
1116 ;; Keep gdb-pending-triggers non-nil till end.
1117 (gdb-add-pending 'gdb-speedbar-timer
)))
1119 (defun gdb-speedbar-timer-fn ()
1120 (if gdb-speedbar-auto-raise
1121 (raise-frame speedbar-frame
))
1122 (gdb-delete-pending 'gdb-speedbar-timer
)
1123 (speedbar-timer-fn))
1125 (defun gdb-var-evaluate-expression-handler (varnum changed
)
1126 (goto-char (point-min))
1127 (re-search-forward ".*value=\\(\".*\"\\)" nil t
)
1128 (let ((var (assoc varnum gdb-var-list
)))
1130 (if changed
(setcar (nthcdr 5 var
) 'changed
))
1131 (setcar (nthcdr 4 var
) (read (match-string 1)))))
1132 (gdb-speedbar-update))
1134 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1135 (defun gdb-var-list-children (varnum)
1136 (gdb-input (concat "-var-update " varnum
) 'ignore
)
1137 (gdb-input (concat "-var-list-children --all-values " varnum
)
1138 `(lambda () (gdb-var-list-children-handler ,varnum
))))
1140 (defun gdb-var-list-children-handler (varnum)
1141 (let* ((var-list nil
)
1142 (output (bindat-get-field (gdb-json-partial-output "child")))
1143 (children (bindat-get-field output
'children
)))
1144 (catch 'child-already-watched
1145 (dolist (var gdb-var-list
)
1146 (if (string-equal varnum
(car var
))
1148 ;; With dynamic varobjs numchild may have increased.
1149 (setcar (nthcdr 2 var
) (bindat-get-field output
'numchild
))
1151 (dolist (child children
)
1152 (let ((varchild (list (bindat-get-field child
'name
)
1153 (bindat-get-field child
'exp
)
1154 (bindat-get-field child
'numchild
)
1155 (bindat-get-field child
'type
)
1156 (bindat-get-field child
'value
)
1158 (bindat-get-field child
'has_more
))))
1159 (if (assoc (car varchild
) gdb-var-list
)
1160 (throw 'child-already-watched nil
))
1161 (push varchild var-list
))))
1162 (push var var-list
)))
1163 (setq gdb-var-list
(nreverse var-list
))))
1164 (gdb-speedbar-update))
1166 (defun gdb-var-set-format (format)
1167 "Set the output format for a variable displayed in the speedbar."
1168 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1170 (gdb-input (concat "-var-set-format " varnum
" " format
) 'ignore
)
1173 (defun gdb-var-delete-1 (var varnum
)
1174 (gdb-input (concat "-var-delete " varnum
) 'ignore
)
1175 (setq gdb-var-list
(delq var gdb-var-list
))
1176 (dolist (varchild gdb-var-list
)
1177 (if (string-match (concat (car var
) "\\.") (car varchild
))
1178 (setq gdb-var-list
(delq varchild gdb-var-list
)))))
1180 (defun gdb-var-delete ()
1181 "Delete watch expression at point from the speedbar."
1183 (let ((text (speedbar-line-text)))
1184 (string-match "\\(\\S-+\\)" text
)
1185 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1187 (if (string-match "\\." (car var
))
1188 (message-box "Can only delete a root expression")
1189 (gdb-var-delete-1 var varnum
)))))
1191 (defun gdb-var-delete-children (varnum)
1192 "Delete children of variable object at point from the speedbar."
1193 (gdb-input (concat "-var-delete -c " varnum
) 'ignore
))
1195 (defun gdb-edit-value (_text _token _indent
)
1196 "Assign a value to a variable displayed in the speedbar."
1197 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1199 (value (read-string "New value: ")))
1200 (gdb-input (concat "-var-assign " varnum
" " value
)
1201 `(lambda () (gdb-edit-value-handler ,value
)))))
1203 (defconst gdb-error-regexp
"\\^error,msg=\\(\".+\"\\)")
1205 (defun gdb-edit-value-handler (value)
1206 (goto-char (point-min))
1207 (if (re-search-forward gdb-error-regexp nil t
)
1208 (message-box "Invalid number or expression (%s)" value
)))
1210 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1211 (defun gdb-var-update ()
1212 (if (not (gdb-pending-p 'gdb-var-update
))
1213 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler
))
1214 (gdb-add-pending 'gdb-var-update
))
1216 (defun gdb-var-update-handler ()
1217 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist
)))
1218 (dolist (var gdb-var-list
)
1219 (setcar (nthcdr 5 var
) nil
))
1220 (let ((temp-var-list gdb-var-list
))
1221 (dolist (change changelist
)
1222 (let* ((varnum (bindat-get-field change
'name
))
1223 (var (assoc varnum gdb-var-list
))
1224 (new-num (bindat-get-field change
'new_num_children
)))
1226 (let ((scope (bindat-get-field change
'in_scope
))
1227 (has-more (bindat-get-field change
'has_more
)))
1228 (cond ((string-equal scope
"false")
1229 (if gdb-delete-out-of-scope
1230 (gdb-var-delete-1 var varnum
)
1231 (setcar (nthcdr 5 var
) 'out-of-scope
)))
1232 ((string-equal scope
"true")
1233 (setcar (nthcdr 6 var
) has-more
)
1234 (when (and (or (not has-more
)
1235 (string-equal has-more
"0"))
1237 (string-equal (nth 2 var
) "0"))
1238 (setcar (nthcdr 4 var
)
1239 (bindat-get-field change
'value
))
1240 (setcar (nthcdr 5 var
) 'changed
)))
1241 ((string-equal scope
"invalid")
1242 (gdb-var-delete-1 var varnum
)))))
1243 (let ((var-list nil
) var1
1244 (children (bindat-get-field change
'new_children
)))
1246 (setq var1
(pop temp-var-list
))
1248 (if (string-equal varnum
(car var1
))
1249 (let ((new (string-to-number new-num
))
1250 (previous (string-to-number (nth 2 var1
))))
1251 (setcar (nthcdr 2 var1
) new-num
)
1252 (push var1 var-list
)
1255 ;; Add new children to list.
1256 (dotimes (_ previous
)
1257 (push (pop temp-var-list
) var-list
))
1258 (dolist (child children
)
1260 (list (bindat-get-field child
'name
)
1261 (bindat-get-field child
'exp
)
1262 (bindat-get-field child
'numchild
)
1263 (bindat-get-field child
'type
)
1264 (bindat-get-field child
'value
)
1266 (bindat-get-field child
'has_more
))))
1267 (push varchild var-list
))))
1268 ;; Remove deleted children from list.
1271 (push (pop temp-var-list
) var-list
))
1272 (dotimes (_ (- previous new
))
1273 (pop temp-var-list
)))))
1274 (push var1 var-list
))
1275 (setq var1
(pop temp-var-list
)))
1276 (setq gdb-var-list
(nreverse var-list
))))))))
1277 (setq gdb-pending-triggers
1278 (delq 'gdb-var-update gdb-pending-triggers
))
1279 (gdb-speedbar-update))
1281 (defun gdb-speedbar-expand-node (text token indent
)
1282 "Expand the node the user clicked on.
1283 TEXT is the text of the button we clicked on, a + or - item.
1284 TOKEN is data related to this node.
1285 INDENT is the current indentation depth."
1286 (cond ((string-match "+" text
) ;expand this node
1287 (let* ((var (assoc token gdb-var-list
))
1288 (expr (nth 1 var
)) (children (nth 2 var
)))
1289 (if (or (<= (string-to-number children
) gdb-max-children
)
1291 (format "%s has %s children. Continue? " expr children
)))
1292 (gdb-var-list-children token
))))
1293 ((string-match "-" text
) ;contract this node
1294 (dolist (var gdb-var-list
)
1295 (if (string-match (concat token
"\\.") (car var
))
1296 (setq gdb-var-list
(delq var gdb-var-list
))))
1297 (gdb-var-delete-children token
)
1298 (speedbar-change-expand-button-char ?
+)
1299 (speedbar-delete-subblock indent
))
1300 (t (error "Ooops... not sure what to do")))
1301 (speedbar-center-buffer-smartly))
1303 (defun gdb-get-target-string ()
1304 (with-current-buffer gud-comint-buffer
1311 ;; Each buffer has a TYPE -- a symbol that identifies the function
1312 ;; of that particular buffer.
1314 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1315 ;; is constructed specially.
1317 ;; Others are constructed by gdb-get-buffer-create and
1318 ;; named according to the rules set forth in the gdb-buffer-rules
1320 (defvar gdb-buffer-rules
'())
1322 (defun gdb-rules-name-maker (rules-entry)
1324 (defun gdb-rules-buffer-mode (rules-entry)
1325 (nth 2 rules-entry
))
1326 (defun gdb-rules-update-trigger (rules-entry)
1327 (nth 3 rules-entry
))
1329 (defun gdb-update-buffer-name ()
1330 "Rename current buffer according to name-maker associated with
1331 it in `gdb-buffer-rules'."
1332 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1333 gdb-buffer-rules
))))
1334 (when f
(rename-buffer (funcall f
)))))
1336 (defun gdb-current-buffer-rules ()
1337 "Get `gdb-buffer-rules' entry for current buffer type."
1338 (assoc gdb-buffer-type gdb-buffer-rules
))
1340 (defun gdb-current-buffer-thread ()
1341 "Get thread object of current buffer from `gdb-threads-list'.
1343 When current buffer is not bound to any thread, return main
1345 (cdr (assoc gdb-thread-number gdb-threads-list
)))
1347 (defun gdb-current-buffer-frame ()
1348 "Get current stack frame object for thread of current buffer."
1349 (bindat-get-field (gdb-current-buffer-thread) 'frame
))
1351 (defun gdb-buffer-type (buffer)
1352 "Get value of `gdb-buffer-type' for BUFFER."
1353 (with-current-buffer buffer
1356 (defun gdb-buffer-shows-main-thread-p ()
1357 "Return t if current GDB buffer shows main selected thread and
1358 is not bound to it."
1360 (not (local-variable-p 'gdb-thread-number
)))
1362 (defun gdb-get-buffer (buffer-type &optional thread
)
1363 "Get a specific GDB buffer.
1365 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1366 and `gdb-thread-number' (if provided) must be equal to THREAD."
1368 (dolist (buffer (buffer-list) nil
)
1369 (with-current-buffer buffer
1370 (when (and (eq gdb-buffer-type buffer-type
)
1372 (equal gdb-thread-number thread
)))
1373 (throw 'found buffer
))))))
1375 (defun gdb-get-buffer-create (buffer-type &optional thread
)
1376 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1377 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1379 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1380 buffer-local variable of the new buffer.
1382 Buffer mode and name are selected according to buffer type.
1384 If buffer has trigger associated with it in `gdb-buffer-rules',
1385 this trigger is subscribed to `gdb-buf-publisher' and called with
1387 (or (gdb-get-buffer buffer-type thread
)
1388 (let ((rules (assoc buffer-type gdb-buffer-rules
))
1389 (new (generate-new-buffer "limbo")))
1390 (with-current-buffer new
1391 (let ((mode (gdb-rules-buffer-mode rules
))
1392 (trigger (gdb-rules-update-trigger rules
)))
1393 (when mode
(funcall mode
))
1394 (setq gdb-buffer-type buffer-type
)
1396 (set (make-local-variable 'gdb-thread-number
) thread
))
1397 (set (make-local-variable 'gud-minor-mode
)
1398 (buffer-local-value 'gud-minor-mode gud-comint-buffer
))
1399 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1400 (rename-buffer (funcall (gdb-rules-name-maker rules
)))
1402 (gdb-add-subscriber gdb-buf-publisher
1403 (cons (current-buffer)
1404 (gdb-bind-function-to-buffer
1405 trigger
(current-buffer))))
1406 (funcall trigger
'start
))
1407 (current-buffer))))))
1409 (defun gdb-bind-function-to-buffer (expr buffer
)
1410 "Return a function which will evaluate EXPR in BUFFER."
1411 `(lambda (&rest args
)
1412 (with-current-buffer ,buffer
1413 (apply ',expr args
))))
1415 ;; Used to display windows with thread-bound buffers
1416 (defmacro def-gdb-preempt-display-buffer
(name buffer
&optional doc
1418 `(defun ,name
(&optional thread
)
1421 (gdb-preempt-existing-or-display-buffer
1422 (gdb-get-buffer-create ,buffer thread
)
1423 ,split-horizontal
)))
1425 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1426 ;; at least one and possible more functions. The functions have these
1427 ;; roles in defining a buffer type:
1429 ;; NAME - Return a name for this buffer type.
1431 ;; The remaining function(s) are optional:
1433 ;; MODE - called in a new buffer with no arguments, should establish
1434 ;; the proper mode for the buffer.
1437 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
1438 (let ((binding (assoc buffer-type gdb-buffer-rules
)))
1440 (setcdr binding rules
)
1441 (push (cons buffer-type rules
)
1442 gdb-buffer-rules
))))
1444 (defun gdb-parent-mode ()
1445 "Generic mode to derive all other GDB buffer modes from."
1446 (kill-all-local-variables)
1447 (setq buffer-read-only t
)
1448 (buffer-disable-undo)
1449 ;; Delete buffer from gdb-buf-publisher when it's killed
1450 ;; (if it has an associated update trigger)
1455 (let ((trigger (gdb-rules-update-trigger
1456 (gdb-current-buffer-rules))))
1458 (gdb-delete-subscriber
1460 ;; This should match gdb-add-subscriber done in
1461 ;; gdb-get-buffer-create
1462 (cons (current-buffer)
1463 (gdb-bind-function-to-buffer trigger
(current-buffer))))))))
1466 ;; Partial-output buffer : This accumulates output from a command executed on
1467 ;; behalf of emacs (rather than the user).
1469 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1470 'gdb-partial-output-name
)
1472 (defun gdb-partial-output-name ()
1473 (concat " *partial-output-"
1474 (gdb-get-target-string)
1478 (gdb-set-buffer-rules 'gdb-inferior-io
1479 'gdb-inferior-io-name
1480 'gdb-inferior-io-mode
)
1482 (defun gdb-inferior-io-name ()
1483 (concat "*input/output of "
1484 (gdb-get-target-string)
1487 (defun gdb-display-io-buffer ()
1488 "Display IO of debugged program in a separate window."
1490 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))
1492 (defun gdb-inferior-io--init-proc (proc)
1493 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1494 (set-process-filter proc
'gdb-inferior-filter
)
1495 (set-process-sentinel proc
'gdb-inferior-io-sentinel
)
1496 ;; The process can run on a remote host.
1497 (let ((tty (or (process-get proc
'remote-tty
)
1498 (process-tty-name proc
))))
1499 (unless (or (null tty
)
1502 (concat "-inferior-tty-set " tty
) 'ignore
))))
1504 (defun gdb-inferior-io-sentinel (proc _str
)
1505 (when (eq (process-status proc
) 'failed
)
1506 ;; When the debugged process exits, Emacs gets an EIO error on
1507 ;; read from the pty, and stops listening to it. If the gdb
1508 ;; process is still running, remove the pty, make a new one, and
1510 (let ((gdb-proc (get-buffer-process gud-comint-buffer
))
1511 (io-buffer (process-buffer proc
)))
1512 (when (and gdb-proc
(process-live-p gdb-proc
)
1513 (buffer-live-p io-buffer
))
1514 ;; `comint-exec' deletes the original process as a side effect.
1515 (comint-exec io-buffer
"gdb-inferior" nil nil nil
)
1516 (gdb-inferior-io--init-proc (get-buffer-process io-buffer
))))))
1518 (defcustom gdb-display-buffer-other-frame-action
1519 '((display-buffer-reuse-window display-buffer-pop-up-frame
)
1520 (reusable-frames . visible
)
1521 (inhibit-same-window . t
)
1522 (pop-up-frame-parameters (height .
14)
1525 (tool-bar-lines . nil
)
1526 (menu-bar-lines . nil
)
1527 (minibuffer . nil
)))
1528 "`display-buffer' action for displaying GDB utility frames."
1530 :type display-buffer--action-custom-type
1534 (defun gdb-frame-io-buffer ()
1535 "Display IO of debugged program in another frame."
1537 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1538 gdb-display-buffer-other-frame-action
))
1540 (defvar gdb-inferior-io-mode-map
1541 (let ((map (make-sparse-keymap)))
1542 (define-key map
"\C-c\C-c" 'gdb-io-interrupt
)
1543 (define-key map
"\C-c\C-z" 'gdb-io-stop
)
1544 (define-key map
"\C-c\C-\\" 'gdb-io-quit
)
1545 (define-key map
"\C-c\C-d" 'gdb-io-eof
)
1546 (define-key map
"\C-d" 'gdb-io-eof
)
1549 ;; We want to use comint because it has various nifty and familiar features.
1550 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
1551 "Major mode for gdb inferior-io."
1552 :syntax-table nil
:abbrev-table nil
1553 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil
))
1555 (defun gdb-inferior-filter (proc string
)
1556 (unless (string-equal string
"")
1557 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))
1558 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1559 (comint-output-filter proc string
)))
1561 (defun gdb-io-interrupt ()
1562 "Interrupt the program being debugged."
1565 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1567 (defun gdb-io-quit ()
1568 "Send quit signal to the program being debugged."
1571 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1573 (defun gdb-io-stop ()
1574 "Stop the program being debugged."
1577 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1579 (defun gdb-io-eof ()
1580 "Send end-of-file to the program being debugged."
1583 (get-buffer-process gud-comint-buffer
)))
1585 (defun gdb-clear-inferior-io ()
1586 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1590 (defconst breakpoint-xpm-data
1592 static char *magick[] = {
1593 /* columns rows colors chars-per-pixel */
1609 "XPM data used for breakpoint icon.")
1611 (defconst breakpoint-enabled-pbm-data
1614 0 0 0 0 1 1 1 1 0 0 0 0
1615 0 0 0 1 1 1 1 1 1 0 0 0
1616 0 0 1 1 1 1 1 1 1 1 0 0
1617 0 1 1 1 1 1 1 1 1 1 1 0
1618 0 1 1 1 1 1 1 1 1 1 1 0
1619 0 1 1 1 1 1 1 1 1 1 1 0
1620 0 1 1 1 1 1 1 1 1 1 1 0
1621 0 0 1 1 1 1 1 1 1 1 0 0
1622 0 0 0 1 1 1 1 1 1 0 0 0
1623 0 0 0 0 1 1 1 1 0 0 0 0"
1624 "PBM data used for enabled breakpoint icon.")
1626 (defconst breakpoint-disabled-pbm-data
1638 0 0 0 1 0 1 0 1 0 0"
1639 "PBM data used for disabled breakpoint icon.")
1641 (defvar breakpoint-enabled-icon nil
1642 "Icon for enabled breakpoint in display margin.")
1644 (defvar breakpoint-disabled-icon nil
1645 "Icon for disabled breakpoint in display margin.")
1647 (declare-function define-fringe-bitmap
"fringe.c"
1648 (bitmap bits
&optional height width align
))
1650 (and (display-images-p)
1651 ;; Bitmap for breakpoint in fringe
1652 (define-fringe-bitmap 'breakpoint
1653 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1654 ;; Bitmap for gud-overlay-arrow in fringe
1655 (define-fringe-bitmap 'hollow-right-triangle
1656 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1658 (defface breakpoint-enabled
1662 "Face for enabled breakpoint icon in fringe."
1665 (defface breakpoint-disabled
1666 '((((class color
) (min-colors 88)) :foreground
"grey70")
1667 ;; Ensure that on low-color displays that we end up something visible.
1668 (((class color
) (min-colors 8) (background light
))
1669 :foreground
"black")
1670 (((class color
) (min-colors 8) (background dark
))
1671 :foreground
"white")
1672 (((type tty
) (class mono
))
1674 (t :background
"gray"))
1675 "Face for disabled breakpoint icon in fringe."
1679 (defvar gdb-control-commands-regexp
1682 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1683 "while-stepping\\|stepping\\|ws\\|actions"
1684 "\\)\\([[:blank:]]+.*\\)?$")
1685 "Regexp matching GDB commands that enter a recursive reading loop.
1686 As long as GDB is in the recursive reading loop, it does not expect
1687 commands to be prefixed by \"-interpreter-exec console\".")
1689 (defun gdb-send (proc string
)
1690 "A comint send filter for gdb."
1691 (with-current-buffer gud-comint-buffer
1692 (let ((inhibit-read-only t
))
1693 (remove-text-properties (point-min) (point-max) '(face))))
1694 ;; mimic <RET> key to repeat previous command in GDB
1695 (if (not (string= "" string
))
1696 (setq gdb-last-command string
)
1697 (if gdb-last-command
(setq string gdb-last-command
)))
1698 (if (or (string-match "^-" string
)
1699 (> gdb-control-level
0))
1700 ;; Either MI command or we are feeding GDB's recursive reading loop.
1702 (setq gdb-first-done-or-error t
)
1703 (process-send-string proc
(concat string
"\n"))
1704 (if (and (string-match "^end$" string
)
1705 (> gdb-control-level
0))
1706 (setq gdb-control-level
(1- gdb-control-level
))))
1708 (if (string-match "\\\\$" string
)
1709 (setq gdb-continuation
(concat gdb-continuation string
"\n"))
1710 (setq gdb-first-done-or-error t
)
1711 (let ((to-send (concat "-interpreter-exec console "
1712 (gdb-mi-quote string
)
1714 (if gdb-enable-debug
1715 (push (cons 'mi-send to-send
) gdb-debug-log
))
1716 (process-send-string proc to-send
))
1717 (if (and (string-match "^end$" string
)
1718 (> gdb-control-level
0))
1719 (setq gdb-control-level
(1- gdb-control-level
)))
1720 (setq gdb-continuation nil
)))
1721 (if (string-match gdb-control-commands-regexp string
)
1722 (setq gdb-control-level
(1+ gdb-control-level
))))
1724 (defun gdb-mi-quote (string)
1725 "Return STRING quoted properly as an MI argument.
1726 The string is enclosed in double quotes.
1727 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1728 (setq string
(replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string
))
1729 (setq string
(replace-regexp-in-string "\n" "\\n" string t t
))
1730 (concat "\"" string
"\""))
1732 (defun gdb-input (command handler-function
)
1733 "Send COMMAND to GDB via the MI interface.
1734 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1736 (if gdb-enable-debug
(push (list 'send-item command handler-function
)
1738 (setq gdb-token-number
(1+ gdb-token-number
))
1739 (setq command
(concat (number-to-string gdb-token-number
) command
))
1740 (push (cons gdb-token-number handler-function
) gdb-handler-alist
)
1741 (if gdbmi-debug-mode
(message "gdb-input: %s" command
))
1742 (process-send-string (get-buffer-process gud-comint-buffer
)
1743 (concat command
"\n")))
1745 ;; NOFRAME is used for gud execution control commands
1746 (defun gdb-current-context-command (command)
1747 "Add --thread to gdb COMMAND when needed."
1748 (if (and gdb-thread-number
1749 gdb-supports-non-stop
)
1750 (concat command
" --thread " gdb-thread-number
)
1753 (defun gdb-current-context-buffer-name (name)
1754 "Add thread information and asterisks to string NAME.
1756 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1758 (if (local-variable-p 'gdb-thread-number
)
1759 (format " (bound to thread %s)" gdb-thread-number
)
1763 (defun gdb-current-context-mode-name (mode)
1764 "Add thread information to MODE which is to be used as `mode-name'."
1766 (if gdb-thread-number
1767 (format " [thread %s]" gdb-thread-number
)
1771 (defcustom gud-gdb-command-name
"gdb -i=mi"
1772 "Default command to execute an executable under the GDB debugger."
1777 (setq gud-running nil
)
1778 (setq gdb-output-sink
'user
)
1779 (setq gdb-pending-triggers nil
))
1781 (defun gdb-update (&optional no-proc
)
1782 "Update buffers showing status of debug session.
1783 If NO-PROC is non-nil, do not try to contact the GDB process."
1784 (when gdb-first-prompt
1785 (gdb-force-mode-line-update
1786 (propertize "initializing..." 'face font-lock-variable-name-face
))
1788 (setq gdb-first-prompt nil
))
1791 (gdb-get-main-selected-frame))
1793 ;; We may need to update gdb-threads-list so we can use
1794 (gdb-get-buffer-create 'gdb-threads-buffer
)
1795 ;; gdb-break-list is maintained in breakpoints handler
1796 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1799 (gdb-emit-signal gdb-buf-publisher
'update
))
1801 (gdb-get-changed-registers)
1802 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1803 (dolist (var gdb-var-list
)
1804 (setcar (nthcdr 5 var
) nil
))
1807 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1808 ;; because we may need to update current gud-running value without
1809 ;; changing current thread (see gdb-running)
1810 (defun gdb-setq-thread-number (number)
1811 "Set `gdb-thread-number' to NUMBER.
1812 Only this function must be used to change `gdb-thread-number'
1813 value to NUMBER, because `gud-running' and `gdb-frame-number'
1814 need to be updated appropriately when current thread changes."
1815 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1816 (unless (string-equal number
"0") (setq gdb-thread-number number
))
1817 (setq gdb-frame-number
"0")
1818 (gdb-update-gud-running))
1820 (defun gdb-update-gud-running ()
1821 "Set `gud-running' according to the state of current thread.
1823 `gdb-frame-number' is set to 0 if current thread is now stopped.
1825 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1826 cannot be reliably used to determine whether or not execution
1827 control buttons should be shown in menu or toolbar. Use
1828 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1831 For all-stop mode, thread information is unavailable while target
1833 (let ((old-value gud-running
))
1835 (string= (bindat-get-field (gdb-current-buffer-thread) 'state
)
1837 ;; Set frame number to "0" when _current_ threads stops.
1838 (when (and (gdb-current-buffer-thread)
1839 (not (eq gud-running old-value
)))
1840 (setq gdb-frame-number
"0"))))
1842 (defun gdb-show-run-p ()
1843 "Return t if \"Run/continue\" should be shown on the toolbar."
1844 (or (not gdb-active-process
)
1846 (not gdb-gud-control-all-threads
)
1849 (and gdb-gud-control-all-threads
1850 (> gdb-stopped-threads-count
0))))
1852 (defun gdb-show-stop-p ()
1853 "Return t if \"Stop\" should be shown on the toolbar."
1855 (not gdb-gud-control-all-threads
)
1858 (and gdb-gud-control-all-threads
1859 (> gdb-running-threads-count
0))))
1861 ;; GUD displays the selected GDB frame. This might might not be the current
1862 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1863 ;; visited breakpoint is, use that window.
1864 (defun gdb-display-source-buffer (buffer)
1865 (let* ((last-window (if gud-last-last-frame
1867 (gud-find-file (car gud-last-last-frame
)))))
1868 (source-window (or last-window
1869 (if (and gdb-source-window
1870 (window-live-p gdb-source-window
))
1871 gdb-source-window
))))
1873 (setq gdb-source-window source-window
)
1874 (set-window-buffer source-window buffer
))
1878 (defun gdbmi-start-with (str offset match
)
1879 "Return non-nil if string STR starts with MATCH, else returns nil.
1880 OFFSET is the position in STR at which the comparison takes place."
1881 (let ((match-length (length match
))
1882 (str-length (- (length str
) offset
)))
1883 (when (>= str-length match-length
)
1884 (string-equal match
(substring str offset
(+ offset match-length
))))))
1886 (defun gdbmi-same-start (str offset match
)
1887 "Return non-nil iff STR and MATCH are equal up to the end of either strings.
1888 OFFSET is the position in STR at which the comparison takes place."
1889 (let* ((str-length (- (length str
) offset
))
1890 (match-length (length match
))
1891 (compare-length (min str-length match-length
)))
1892 (when (> compare-length
0)
1893 (string-equal (substring str offset
(+ offset compare-length
))
1894 (substring match
0 compare-length
)))))
1896 (defun gdbmi-is-number (character)
1897 "Return non-nil iff CHARACTER is a numerical character between 0 and 9."
1898 (and (>= character ?
0)
1902 (defvar-local gdbmi-bnf-state
'gdbmi-bnf-output
1903 "Current GDB/MI output parser state.
1904 The parser is placed in a different state when an incomplete data steam is
1906 This variable will preserve the state required to resume the parsing
1907 when more data arrives.")
1909 (defvar-local gdbmi-bnf-offset
0
1910 "Offset in `gud-marker-acc' at which the parser is reading.
1911 This offset is used to be able to parse the GDB/MI message
1912 in-place, without the need of copying the string in a temporary buffer
1913 or discarding parsed tokens by substringing the message.")
1915 (defun gdbmi-bnf-init ()
1916 "Initialize the GDB/MI message parser."
1917 (setq gdbmi-bnf-state
'gdbmi-bnf-output
)
1918 (setq gdbmi-bnf-offset
0)
1919 (setq gud-marker-acc
""))
1922 (defun gdbmi-bnf-output ()
1923 "Implementation of the following GDB/MI output grammar rule:
1926 ( out-of-band-record )* [ result-record ] gdb-prompt"
1928 (gdbmi-bnf-skip-unrecognized)
1929 (while (gdbmi-bnf-out-of-band-record))
1930 (gdbmi-bnf-result-record)
1931 (gdbmi-bnf-gdb-prompt))
1934 (defun gdbmi-bnf-skip-unrecognized ()
1935 "Skip characters until is encounters the beginning of a valid record.
1936 Used as a protection mechanism in case something goes wrong when parsing
1937 a GDB/MI reply message."
1938 (let ((acc-length (length gud-marker-acc
))
1939 (prefix-offset gdbmi-bnf-offset
)
1940 (prompt "(gdb) \n"))
1942 (while (and (< prefix-offset acc-length
)
1943 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
1944 (setq prefix-offset
(1+ prefix-offset
)))
1946 (if (and (< prefix-offset acc-length
)
1947 (not (memq (aref gud-marker-acc prefix-offset
)
1948 '(?^ ?
* ?
+ ?
= ?~ ?
@ ?
&)))
1949 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt
))
1950 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
1952 (let ((unrecognized-str (match-string 0 gud-marker-acc
)))
1953 (setq gdbmi-bnf-offset
(match-end 0))
1954 (if gdbmi-debug-mode
1955 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str
))
1956 (gdb-shell unrecognized-str
)
1960 (defun gdbmi-bnf-gdb-prompt ()
1961 "Implementation of the following GDB/MI output grammar rule:
1968 (let ((prompt "(gdb) \n"))
1969 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt
)
1970 (if gdbmi-debug-mode
(message "gdbmi-bnf-gdb-prompt: %s" prompt
))
1972 (setq gdbmi-bnf-offset
(+ gdbmi-bnf-offset
(length prompt
)))
1974 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
1975 ;; the end of a GDB reply message.
1979 (defun gdbmi-bnf-result-record ()
1980 "Implementation of the following GDB/MI output grammar rule:
1983 [ token ] '^' result-class ( ',' result )* nl
1986 any sequence of digits."
1988 (gdbmi-bnf-result-and-async-record-impl))
1991 (defun gdbmi-bnf-out-of-band-record ()
1992 "Implementation of the following GDB/MI output grammar rule:
1994 out-of-band-record ==>
1995 async-record | stream-record"
1997 (or (gdbmi-bnf-async-record)
1998 (gdbmi-bnf-stream-record)))
2001 (defun gdbmi-bnf-async-record ()
2002 "Implementation of the following GDB/MI output grammar rules:
2005 exec-async-output | status-async-output | notify-async-output
2007 exec-async-output ==>
2008 [ token ] '*' async-output
2010 status-async-output ==>
2011 [ token ] '+' async-output
2013 notify-async-output ==>
2014 [ token ] '=' async-output
2017 async-class ( ',' result )* nl"
2019 (gdbmi-bnf-result-and-async-record-impl))
2022 (defun gdbmi-bnf-stream-record ()
2023 "Implement the following GDB/MI output grammar rule:
2025 console-stream-output | target-stream-output | log-stream-output
2027 console-stream-output ==>
2030 target-stream-output ==>
2033 log-stream-output ==>
2035 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2036 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset
) '(?~ ?
@ ?
&))
2037 (string-match "\\([~@&]\\)\\(\".*?\"\\)\n" gud-marker-acc
2039 (let ((prefix (match-string 1 gud-marker-acc
))
2040 (c-string (match-string 2 gud-marker-acc
)))
2042 (setq gdbmi-bnf-offset
(match-end 0))
2043 (if gdbmi-debug-mode
(message "gdbmi-bnf-stream-record: %s"
2044 (match-string 0 gud-marker-acc
)))
2046 (cond ((string-equal prefix
"~")
2047 (gdbmi-bnf-console-stream-output c-string
))
2048 ((string-equal prefix
"@")
2049 (gdbmi-bnf-target-stream-output c-string
))
2050 ((string-equal prefix
"&")
2051 (gdbmi-bnf-log-stream-output c-string
)))
2054 (defun gdbmi-bnf-console-stream-output (c-string)
2055 "Handler for the console-stream-output GDB/MI output grammar rule."
2056 (gdb-console c-string
))
2058 (defun gdbmi-bnf-target-stream-output (_c-string)
2059 "Handler for the target-stream-output GDB/MI output grammar rule."
2060 ;; Not currently used.
2063 (defun gdbmi-bnf-log-stream-output (c-string)
2064 "Handler for the log-stream-output GDB/MI output grammar rule."
2065 ;; Suppress "No registers." GDB 6.8 and earlier
2066 ;; duplicates MI error message on internal stream.
2067 ;; Don't print to GUD buffer.
2068 (if (not (string-equal (read c-string
) "No registers.\n"))
2069 (gdb-internals c-string
)))
2072 (defconst gdbmi-bnf-result-state-configs
2073 '(("^" .
(("done" .
(gdb-done . progressive
))
2074 ("error" .
(gdb-error . progressive
))
2075 ("running" .
(gdb-starting . atomic
))))
2076 ("*" .
(("stopped" .
(gdb-stopped . atomic
))
2077 ("running" .
(gdb-running . atomic
))))
2079 ("=" .
(("thread-created" .
(gdb-thread-created . atomic
))
2080 ("thread-selected" .
(gdb-thread-selected . atomic
))
2081 ("thread-existed" .
(gdb-ignored-notification . atomic
))
2082 ('default .
(gdb-ignored-notification . atomic
)))))
2083 "Alist of alists, mapping the type and class of message to a handler function.
2084 Handler functions are all flagged as either `progressive' or `atomic'.
2085 `progressive' handlers are capable of parsing incomplete messages.
2086 They can be called several time with new data chunk as they arrive from GDB.
2087 `progressive' handlers must have an extra argument that is set to a non-nil
2088 value when the message is complete.
2090 Implement the following GDB/MI output grammar rule:
2092 'done' | 'running' | 'connected' | 'error' | 'exit'
2095 'stopped' | others (where others will be added depending on the needs
2096 --this is still in development).")
2098 (defun gdbmi-bnf-result-and-async-record-impl ()
2099 "Common implementation of the result-record and async-record rule.
2100 Both rules share the same syntax. Those records may be very large in size.
2101 For that reason, the \"result\" part of the record is parsed by
2102 `gdbmi-bnf-incomplete-record-result', which will keep
2103 receiving characters as they arrive from GDB until the record is complete."
2104 (let ((acc-length (length gud-marker-acc
))
2105 (prefix-offset gdbmi-bnf-offset
))
2107 (while (and (< prefix-offset acc-length
)
2108 (gdbmi-is-number (aref gud-marker-acc prefix-offset
)))
2109 (setq prefix-offset
(1+ prefix-offset
)))
2111 (if (and (< prefix-offset acc-length
)
2112 (member (aref gud-marker-acc prefix-offset
) '(?
* ?
+ ?
= ?^
))
2113 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2114 gud-marker-acc gdbmi-bnf-offset
))
2116 (let ((token (match-string 1 gud-marker-acc
))
2117 (prefix (match-string 2 gud-marker-acc
))
2118 (class (match-string 3 gud-marker-acc
))
2119 (complete (string-equal (match-string 4 gud-marker-acc
) "\n"))
2123 (setq gdbmi-bnf-offset
(match-end 0))
2124 (if gdbmi-debug-mode
(message "gdbmi-bnf-result-record: %s"
2125 (match-string 0 gud-marker-acc
)))
2128 (cdr (assoc prefix gdbmi-bnf-result-state-configs
)))
2129 (setq class-command
(cdr (assoc class class-alist
)))
2130 (if (null class-command
)
2131 (setq class-command
(cdr (assoc 'default class-alist
))))
2135 (if (equal (cdr class-command
) 'progressive
)
2136 (funcall (car class-command
) token
"" complete
)
2137 (funcall (car class-command
) token
"")))
2138 (setq gdbmi-bnf-state
2140 (gdbmi-bnf-incomplete-record-result token class-command
)))
2141 (funcall gdbmi-bnf-state
))
2144 (defun gdbmi-bnf-incomplete-record-result (token class-command
)
2145 "State of the parser used to progressively parse a result-record or async-record
2146 rule from an incomplete data stream. The parser will stay in this state until
2147 the end of the current result or async record is reached."
2148 (when (< gdbmi-bnf-offset
(length gud-marker-acc
))
2149 ;; Search the data stream for the end of the current record:
2150 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset
))
2151 (is-progressive (equal (cdr class-command
) 'progressive
))
2152 (is-complete (not (null newline-pos
)))
2155 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2156 ;; be processed by the class-command handler:
2157 (when (or is-complete is-progressive
)
2159 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
))
2160 (setq gdbmi-bnf-offset
(+ 1 newline-pos
)))
2162 (if gdbmi-debug-mode
2163 (message "gdbmi-bnf-incomplete-record-result: %s"
2164 (substring gud-marker-acc gdbmi-bnf-offset newline-pos
)))
2166 ;; Update the parsing state before invoking the handler in class-command
2167 ;; to make sure it's not left in an invalid state if the handler was
2168 ;; to generate an error.
2170 (setq gdbmi-bnf-state
'gdbmi-bnf-output
))
2174 (funcall (car class-command
) token result-str is-complete
)
2176 (funcall (car class-command
) token result-str
))))
2179 ;; Incomplete gdb response: abort parsing until we receive more data.
2180 (if gdbmi-debug-mode
(message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2181 (throw 'gdbmi-incomplete-stream nil
))
2186 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2187 ; The handling of those rules is currently done by the handlers registered
2188 ; in gdbmi-bnf-result-state-configs
2191 ; variable "=" value
2197 ; const | tuple | list
2203 ; "{}" | "{" result ( "," result )* "}"
2206 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2209 (defun gud-gdbmi-marker-filter (string)
2210 "Filter GDB/MI output."
2212 ;; Record transactions if logging is enabled.
2213 (when gdb-enable-debug
2214 (push (cons 'recv string
) gdb-debug-log
)
2215 (if (and gdb-debug-log-max
2216 (> (length gdb-debug-log
) gdb-debug-log-max
))
2217 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
2219 ;; Recall the left over gud-marker-acc from last time.
2220 (setq gud-marker-acc
(concat gud-marker-acc string
))
2222 ;; Start accumulating output for the GUD buffer.
2223 (setq gdb-filter-output
"")
2225 (let ((acc-length (length gud-marker-acc
)))
2226 (catch 'gdbmi-incomplete-stream
2227 (while (and (< gdbmi-bnf-offset acc-length
)
2228 (funcall gdbmi-bnf-state
)))))
2230 (when (/= gdbmi-bnf-offset
0)
2231 (setq gud-marker-acc
(substring gud-marker-acc gdbmi-bnf-offset
))
2232 (setq gdbmi-bnf-offset
0))
2234 (when (and gdbmi-debug-mode
(> (length gud-marker-acc
) 0))
2235 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc
))
2239 (defun gdb-gdb (_output-field))
2241 (defun gdb-shell (output-field)
2242 (setq gdb-filter-output
2243 (concat output-field gdb-filter-output
)))
2245 (defun gdb-ignored-notification (_token _output-field
))
2247 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2248 (defun gdb-thread-created (_token _output-field
))
2249 (defun gdb-thread-exited (_token output-field
)
2250 "Handle =thread-exited async record.
2251 Unset `gdb-thread-number' if current thread exited and update threads list."
2252 (let* ((thread-id (bindat-get-field (gdb-json-string output-field
) 'id
)))
2253 (if (string= gdb-thread-number thread-id
)
2254 (gdb-setq-thread-number nil
))
2255 ;; When we continue current thread and it quickly exits,
2256 ;; gdb-pending-triggers left after gdb-running disallow us to
2257 ;; properly call -thread-info without --thread option. Thus we
2258 ;; need to use gdb-wait-for-pending.
2259 (gdb-wait-for-pending
2260 (gdb-emit-signal gdb-buf-publisher
'update-threads
))))
2262 (defun gdb-thread-selected (_token output-field
)
2263 "Handler for =thread-selected MI output record.
2265 Sets `gdb-thread-number' to new id."
2266 (let* ((result (gdb-json-string output-field
))
2267 (thread-id (bindat-get-field result
'id
)))
2268 (gdb-setq-thread-number thread-id
)
2269 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
2270 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
2271 ;; as usually. Things happen to fast and second call (from
2272 ;; gdb-thread-selected handler) gets cut off by our beloved
2273 ;; gdb-pending-triggers.
2274 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
2275 ;; body will get executed when `gdb-pending-triggers` is empty.
2276 (gdb-wait-for-pending
2279 (defun gdb-running (_token output-field
)
2281 (bindat-get-field (gdb-json-string output-field
) 'thread-id
)))
2282 ;; We reset gdb-frame-number to nil if current thread has gone
2283 ;; running. This can't be done in gdb-thread-list-handler-custom
2284 ;; because we need correct gdb-frame-number by the time
2285 ;; -thread-info command is sent.
2286 (when (or (string-equal thread-id
"all")
2287 (string-equal thread-id gdb-thread-number
))
2288 (setq gdb-frame-number nil
)))
2289 (setq gdb-inferior-status
"running")
2290 (gdb-force-mode-line-update
2291 (propertize gdb-inferior-status
'face font-lock-type-face
))
2292 (when (not gdb-non-stop
)
2293 (setq gud-running t
))
2294 (setq gdb-active-process t
)
2295 (gdb-emit-signal gdb-buf-publisher
'update-threads
))
2297 (defun gdb-starting (_output-field _result
)
2298 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2299 (setq gdb-inferior-status
"running")
2300 (gdb-force-mode-line-update
2301 (propertize gdb-inferior-status
'face font-lock-type-face
))
2302 (setq gdb-active-process t
)
2303 (setq gud-running t
)
2304 ;; GDB doesn't seem to respond to -thread-info before first stop or
2305 ;; thread exit (even in non-stop mode), so this is useless.
2306 ;; Behavior may change in the future.
2307 (gdb-emit-signal gdb-buf-publisher
'update-threads
))
2309 ;; -break-insert -t didn't give a reason before gdb 6.9
2311 (defun gdb-stopped (_token output-field
)
2312 "Given the contents of *stopped MI async record, select new
2313 current thread and update GDB buffers."
2314 ;; Reason is available with target-async only
2315 (let* ((result (gdb-json-string output-field
))
2316 (reason (bindat-get-field result
'reason
))
2317 (thread-id (bindat-get-field result
'thread-id
)))
2319 ;; -data-list-register-names needs to be issued for any stopped
2321 (when (not gdb-register-names
)
2322 (gdb-input (concat "-data-list-register-names"
2323 (if gdb-supports-non-stop
2324 (concat " --thread " thread-id
)))
2325 'gdb-register-names-handler
))
2327 ;; Don't set gud-last-frame here as it's currently done in
2328 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2332 ;; ;; Extract the frame position from the marker.
2333 ;; gud-last-frame (cons file
2334 ;; (string-to-number
2335 ;; (match-string 6 gud-marker-acc)))))
2337 (setq gdb-inferior-status
(or reason
"unknown"))
2338 (gdb-force-mode-line-update
2339 (propertize gdb-inferior-status
'face font-lock-warning-face
))
2340 (if (string-equal reason
"exited-normally")
2341 (setq gdb-active-process nil
))
2343 ;; Select new current thread.
2345 ;; Don't switch if we have no reasons selected
2346 (when gdb-switch-reasons
2347 ;; Switch from another stopped thread only if we have
2348 ;; gdb-switch-when-another-stopped:
2349 (when (or gdb-switch-when-another-stopped
2350 (not (string= "stopped"
2351 (bindat-get-field (gdb-current-buffer-thread) 'state
))))
2352 ;; Switch if current reason has been selected or we have no
2354 (if (or (eq gdb-switch-reasons t
)
2355 (member reason gdb-switch-reasons
))
2356 (when (not (string-equal gdb-thread-number thread-id
))
2357 (message (concat "Switched to thread " thread-id
))
2358 (gdb-setq-thread-number thread-id
))
2359 (message (format "Thread %s stopped" thread-id
)))))
2361 ;; Print "(gdb)" to GUD console
2362 (when gdb-first-done-or-error
2363 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2365 ;; In non-stop, we update information as soon as another thread gets
2367 (when (or gdb-first-done-or-error
2369 ;; In all-stop this updates gud-running properly as well.
2371 (setq gdb-first-done-or-error nil
))
2372 (run-hook-with-args 'gdb-stopped-functions result
)))
2374 ;; Remove the trimmings from log stream containing debugging messages
2375 ;; being produced by GDB's internals, use warning face and send to GUD
2377 (defun gdb-internals (output-field)
2378 (setq gdb-filter-output
2381 (if (string= output-field
"\"\\n\"")
2383 (let ((error-message
2384 (read output-field
)))
2386 0 (length error-message
)
2387 'face font-lock-warning-face
2391 ;; Remove the trimmings from the console stream and send to GUD buffer
2392 ;; (frontend MI commands should not print to this stream)
2393 (defun gdb-console (output-field)
2394 (setq gdb-filter-output
2395 (gdb-concat-output gdb-filter-output
(read output-field
))))
2397 (defun gdb-done (token-number output-field is-complete
)
2398 (gdb-done-or-error token-number
'done output-field is-complete
))
2400 (defun gdb-error (token-number output-field is-complete
)
2401 (gdb-done-or-error token-number
'error output-field is-complete
))
2403 (defun gdb-done-or-error (token-number type output-field is-complete
)
2404 (if (string-equal token-number
"")
2405 ;; Output from command entered by user
2407 (setq gdb-output-sink
'user
)
2408 (setq token-number nil
)
2409 ;; MI error - send to minibuffer
2410 (when (eq type
'error
)
2411 ;; Skip "msg=" from `output-field'
2412 (message (read (substring output-field
4)))
2413 ;; Don't send to the console twice. (If it is a console error
2414 ;; it is also in the console stream.)
2415 (setq output-field nil
)))
2416 ;; Output from command from frontend.
2417 (setq gdb-output-sink
'emacs
))
2419 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2420 (let* ((proc (get-buffer-process gud-comint-buffer
))
2421 (no-proc (or (null proc
)
2422 (memq (process-status proc
) '(exit signal
)))))
2424 (when (and is-complete gdb-first-done-or-error
)
2425 (unless (or token-number gud-running no-proc
)
2426 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2427 (gdb-update no-proc
)
2428 (setq gdb-first-done-or-error nil
))
2430 (setq gdb-filter-output
2431 (gdb-concat-output gdb-filter-output output-field
))
2433 ;; We are done concatenating to the output sink. Restore it to user sink:
2434 (setq gdb-output-sink
'user
)
2436 (when (and token-number is-complete
)
2437 (with-current-buffer
2438 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2440 (cdr (assoc (string-to-number token-number
) gdb-handler-alist
))))
2441 (setq gdb-handler-alist
2442 (assq-delete-all token-number gdb-handler-alist
)))
2445 (gdb-clear-partial-output))))
2447 (defun gdb-concat-output (so-far new
)
2449 ((eq gdb-output-sink
'user
) (concat so-far new
))
2450 ((eq gdb-output-sink
'emacs
)
2451 (gdb-append-to-partial-output new
)
2454 (defun gdb-append-to-partial-output (string)
2455 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2456 (goto-char (point-max))
2459 (defun gdb-clear-partial-output ()
2460 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2463 (defun gdb-jsonify-buffer (&optional fix-key fix-list
)
2464 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2466 Field names are wrapped in double quotes and equal signs are
2467 replaced with semicolons.
2469 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2470 partial output. This is used to get rid of useless keys in lists
2471 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2472 -break-info are examples of MI commands which issue such
2475 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2476 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2477 -break-info output when it contains breakpoint script field
2478 incompatible with GDB/MI output syntax."
2480 (goto-char (point-min))
2483 (while (re-search-forward (concat "[\\[,]\\(" fix-key
"=\\)") nil t
)
2484 (replace-match "" nil nil nil
1))))
2487 ;; Find positions of braces which enclose broken list
2488 (while (re-search-forward (concat fix-list
"={\"") nil t
)
2489 (let ((p1 (goto-char (- (point) 2)))
2490 (p2 (progn (forward-sexp)
2492 ;; Replace braces with brackets
2500 (goto-char (point-min))
2502 (while (re-search-forward
2503 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t
)
2504 (replace-match "\"\\1\":\\2" nil nil
))
2505 (goto-char (point-max))
2508 (defun gdb-json-read-buffer (&optional fix-key fix-list
)
2509 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2511 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2512 (gdb-jsonify-buffer fix-key fix-list
)
2514 (goto-char (point-min))
2515 (let ((json-array-type 'list
))
2518 (defun gdb-json-string (string &optional fix-key fix-list
)
2519 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2521 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2524 (gdb-json-read-buffer fix-key fix-list
)))
2526 (defun gdb-json-partial-output (&optional fix-key fix-list
)
2527 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2529 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2530 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2531 (gdb-json-read-buffer fix-key fix-list
)))
2533 (defun gdb-line-posns (line)
2534 "Return a pair of LINE beginning and end positions."
2535 (let ((offset (1+ (- line
(line-number-at-pos)))))
2537 (line-beginning-position offset
)
2538 (line-end-position offset
))))
2540 (defmacro gdb-mark-line
(line variable
)
2541 "Set VARIABLE marker to point at beginning of LINE.
2543 If current window has no fringes, inverse colors on LINE.
2545 Return position where LINE begins."
2547 (let* ((posns (gdb-line-posns ,line
))
2548 (start-posn (car posns
))
2549 (end-posn (cdr posns
)))
2550 (set-marker ,variable
(copy-marker start-posn
))
2551 (when (not (> (car (window-fringes)) 0))
2552 (put-text-property start-posn end-posn
2553 'font-lock-face
'(:inverse-video t
)))
2556 (defun gdb-pad-string (string padding
)
2557 (format (concat "%" (number-to-string padding
) "s") string
))
2559 ;; gdb-table struct is a way to programmatically construct simple
2560 ;; tables. It help to reliably align columns of data in GDB buffers
2562 (cl-defstruct gdb-table
2565 (row-properties nil
)
2568 (defun gdb-mapcar* (function &rest seqs
)
2569 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2570 If there are several SEQS, FUNCTION is called with that many
2571 arguments, and mapping stops as soon as the shortest list runs
2573 (let ((shortest (apply #'min
(mapcar #'length seqs
))))
2580 (number-sequence 0 (1- shortest
)))))
2582 (defun gdb-table-add-row (table row
&optional properties
)
2583 "Add ROW of string to TABLE and recalculate column sizes.
2585 When non-nil, PROPERTIES will be added to the whole row when
2586 calling `gdb-table-string'."
2587 (let ((rows (gdb-table-rows table
))
2588 (row-properties (gdb-table-row-properties table
))
2589 (column-sizes (gdb-table-column-sizes table
))
2590 (right-align (gdb-table-right-align table
)))
2591 (when (not column-sizes
)
2592 (setf (gdb-table-column-sizes table
)
2593 (make-list (length row
) 0)))
2594 (setf (gdb-table-rows table
)
2595 (append rows
(list row
)))
2596 (setf (gdb-table-row-properties table
)
2597 (append row-properties
(list properties
)))
2598 (setf (gdb-table-column-sizes table
)
2599 (gdb-mapcar* (lambda (x s
)
2601 (max (abs x
) (string-width (or s
"")))))
2602 (if right-align new-x
(- new-x
))))
2603 (gdb-table-column-sizes table
)
2605 ;; Avoid trailing whitespace at eol
2606 (if (not (gdb-table-right-align table
))
2607 (setcar (last (gdb-table-column-sizes table
)) 0))))
2609 (defun gdb-table-string (table &optional sep
)
2610 "Return TABLE as a string with columns separated with SEP."
2611 (let ((column-sizes (gdb-table-column-sizes table
)))
2615 (lambda (row properties
)
2617 (mapconcat 'identity
2618 (gdb-mapcar* (lambda (s x
) (gdb-pad-string s x
))
2622 (gdb-table-rows table
)
2623 (gdb-table-row-properties table
))
2626 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2627 (defun gdb-get-many-fields (struct &rest fields
)
2628 "Return a list of FIELDS values from STRUCT."
2630 (dolist (field fields
)
2631 (push (bindat-get-field struct field
) values
))
2634 (defmacro def-gdb-auto-update-trigger
(trigger-name gdb-command
2636 &optional signal-list
)
2637 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2638 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2639 buffer with `gdb-bind-function-to-buffer'.
2641 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2642 defined trigger is called with an argument from SIGNAL-LIST. It's
2643 not recommended to define triggers with empty SIGNAL-LIST.
2644 Normally triggers should respond at least to 'update signal.
2646 Normally the trigger defined by this command must be called from
2647 the buffer where HANDLER-NAME must work. This should be done so
2648 that buffer-local thread number may be used in GDB-COMMAND (by
2649 calling `gdb-current-context-command').
2650 `gdb-bind-function-to-buffer' is used to achieve this, see
2651 `gdb-get-buffer-create'.
2653 Triggers defined by this command are meant to be used as a
2654 trigger argument when describing buffer types with
2655 `gdb-set-buffer-rules'."
2656 `(defun ,trigger-name
(&optional signal
)
2658 (or (not ,signal-list
)
2659 (memq signal
,signal-list
))
2660 (when (not (gdb-pending-p
2661 (cons (current-buffer) ',trigger-name
)))
2662 (gdb-input ,gdb-command
2663 (gdb-bind-function-to-buffer ',handler-name
(current-buffer)))
2664 (gdb-add-pending (cons (current-buffer) ',trigger-name
))))))
2666 ;; Used by disassembly buffer only, the rest use
2667 ;; def-gdb-trigger-and-handler
2668 (defmacro def-gdb-auto-update-handler
(handler-name trigger-name custom-defun
2669 &optional nopreserve
)
2670 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2672 Handlers are normally called from the buffers they put output in.
2674 Delete ((current-buffer) . TRIGGER-NAME) from
2675 `gdb-pending-triggers', erase current buffer and evaluate
2676 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2678 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2679 `(defun ,handler-name
()
2680 (gdb-delete-pending (cons (current-buffer) ',trigger-name
))
2681 (let* ((inhibit-read-only t
)
2682 ,@(unless nopreserve
2683 '((window (get-buffer-window (current-buffer) 0))
2684 (start (window-start window
))
2685 (p (window-point window
)))))
2688 (gdb-update-buffer-name)
2689 ,@(when (not nopreserve
)
2690 '((set-window-start window start
)
2691 (set-window-point window p
))))))
2693 (defmacro def-gdb-trigger-and-handler
(trigger-name gdb-command
2694 handler-name custom-defun
2695 &optional signal-list
)
2696 "Define trigger and handler.
2698 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2699 See `def-gdb-auto-update-trigger'.
2701 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2702 See `def-gdb-auto-update-handler'."
2704 (def-gdb-auto-update-trigger ,trigger-name
2706 ,handler-name
,signal-list
)
2707 (def-gdb-auto-update-handler ,handler-name
2708 ,trigger-name
,custom-defun
)))
2712 ;; Breakpoint buffer : This displays the output of `-break-list'.
2713 (def-gdb-trigger-and-handler
2714 gdb-invalidate-breakpoints
"-break-list"
2715 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2718 (gdb-set-buffer-rules
2719 'gdb-breakpoints-buffer
2720 'gdb-breakpoints-buffer-name
2721 'gdb-breakpoints-mode
2722 'gdb-invalidate-breakpoints
)
2724 (defun gdb-breakpoints-list-handler-custom ()
2725 (let ((breakpoints-list (bindat-get-field
2726 (gdb-json-partial-output "bkpt" "script")
2727 'BreakpointTable
'body
))
2728 (table (make-gdb-table)))
2729 (setq gdb-breakpoints-list nil
)
2730 (gdb-table-add-row table
'("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2731 (dolist (breakpoint breakpoints-list
)
2732 (add-to-list 'gdb-breakpoints-list
2733 (cons (bindat-get-field breakpoint
'number
)
2735 (let ((at (bindat-get-field breakpoint
'at
))
2736 (pending (bindat-get-field breakpoint
'pending
))
2737 (func (bindat-get-field breakpoint
'func
))
2738 (type (bindat-get-field breakpoint
'type
)))
2739 (gdb-table-add-row table
2741 (bindat-get-field breakpoint
'number
)
2743 (or (bindat-get-field breakpoint
'disp
) "")
2744 (let ((flag (bindat-get-field breakpoint
'enabled
)))
2745 (if (string-equal flag
"y")
2746 (propertize "y" 'font-lock-face font-lock-warning-face
)
2747 (propertize "n" 'font-lock-face font-lock-comment-face
)))
2748 (bindat-get-field breakpoint
'addr
)
2749 (or (bindat-get-field breakpoint
'times
) "")
2750 (if (and type
(string-match ".*watchpoint" type
))
2751 (bindat-get-field breakpoint
'what
)
2754 (propertize (or func
"unknown")
2755 'font-lock-face font-lock-function-name-face
)
2756 (gdb-frame-location breakpoint
)))))
2757 ;; Add clickable properties only for breakpoints with file:line
2759 (append (list 'gdb-breakpoint breakpoint
)
2760 (when func
'(help-echo "mouse-2, RET: visit breakpoint"
2761 mouse-face highlight
))))))
2762 (insert (gdb-table-string table
" "))
2763 (gdb-place-breakpoints)))
2765 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2766 (defun gdb-place-breakpoints ()
2767 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2768 (dolist (buffer (buffer-list))
2769 (with-current-buffer buffer
2770 (if (and (eq gud-minor-mode
'gdbmi
)
2771 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2772 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2773 (dolist (breakpoint gdb-breakpoints-list
)
2774 (let* ((breakpoint (cdr breakpoint
)) ; gdb-breakpoints-list is
2775 ; an associative list
2776 (line (bindat-get-field breakpoint
'line
)))
2778 (let ((file (bindat-get-field breakpoint
'fullname
))
2779 (flag (bindat-get-field breakpoint
'enabled
))
2780 (bptno (bindat-get-field breakpoint
'number
)))
2781 (unless (and file
(file-exists-p file
))
2782 (setq file
(cdr (assoc bptno gdb-location-alist
))))
2784 (string-equal file
"File not found"))
2785 ;; If the full filename is not recorded in the
2786 ;; breakpoint structure or in `gdb-location-alist', use
2787 ;; -file-list-exec-source-file to extract it.
2788 (when (setq file
(bindat-get-field breakpoint
'file
))
2789 (gdb-input (concat "list " file
":1") 'ignore
)
2790 (gdb-input "-file-list-exec-source-file"
2791 `(lambda () (gdb-get-location
2792 ,bptno
,line
,flag
))))
2793 (with-current-buffer (find-file-noselect file
'nowarn
)
2795 ;; Only want one breakpoint icon at each location.
2796 (gdb-put-breakpoint-icon (string-equal flag
"y") bptno
2797 (string-to-number line
)))))))))
2799 (defvar gdb-source-file-regexp
"fullname=\"\\(.*?\\)\"")
2801 (defun gdb-get-location (bptno line flag
)
2802 "Find the directory containing the relevant source file.
2803 Put in buffer and place breakpoint icon."
2804 (goto-char (point-min))
2805 (catch 'file-not-found
2806 (if (re-search-forward gdb-source-file-regexp nil t
)
2807 (delete (cons bptno
"File not found") gdb-location-alist
)
2808 (push (cons bptno
(match-string 1)) gdb-location-alist
)
2810 (unless (assoc bptno gdb-location-alist
)
2811 (push (cons bptno
"File not found") gdb-location-alist
)
2812 (message-box "Cannot find source file for breakpoint location.
2813 Add directory to search path for source files using the GDB command, dir."))
2814 (throw 'file-not-found nil
))
2815 (with-current-buffer (find-file-noselect (match-string 1))
2817 ;; only want one breakpoint icon at each location
2818 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
(string-to-number line
)))))
2820 (add-hook 'find-file-hook
'gdb-find-file-hook
)
2822 (defun gdb-find-file-hook ()
2823 "Set up buffer for debugging if file is part of the source code
2824 of the current session."
2825 (if (and (buffer-name gud-comint-buffer
)
2826 ;; in case gud or gdb-ui is just loaded
2828 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
2830 (if (member buffer-file-name gdb-source-file-list
)
2831 (with-current-buffer (find-buffer-visiting buffer-file-name
)
2832 (gdb-init-buffer)))))
2834 (declare-function gud-remove
"gdb-mi" t t
) ; gud-def
2835 (declare-function gud-break
"gdb-mi" t t
) ; gud-def
2836 (declare-function fringe-bitmaps-at-pos
"fringe.c" (&optional pos window
))
2838 (defun gdb-mouse-set-clear-breakpoint (event)
2839 "Set/clear breakpoint in left fringe/margin at mouse click.
2840 If not in a source or disassembly buffer just set point."
2842 (mouse-minibuffer-check event
)
2843 (let ((posn (event-end event
)))
2844 (with-selected-window (posn-window posn
)
2845 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode
))
2846 (if (numberp (posn-point posn
))
2848 (goto-char (posn-point posn
))
2849 (if (or (posn-object posn
)
2850 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
2854 (posn-set-point posn
))))
2856 (defun gdb-mouse-toggle-breakpoint-margin (event)
2857 "Enable/disable breakpoint in left margin with mouse click."
2859 (mouse-minibuffer-check event
)
2860 (let ((posn (event-end event
)))
2861 (if (numberp (posn-point posn
))
2862 (with-selected-window (posn-window posn
)
2864 (goto-char (posn-point posn
))
2865 (if (posn-object posn
)
2867 (let ((bptno (get-text-property
2868 0 'gdb-bptno
(car (posn-string posn
)))))
2870 (if (get-text-property
2871 0 'gdb-enabled
(car (posn-string posn
)))
2876 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2877 "Enable/disable breakpoint in left fringe with mouse click."
2879 (mouse-minibuffer-check event
)
2880 (let* ((posn (event-end event
))
2881 (pos (posn-point posn
))
2884 (with-selected-window (posn-window posn
)
2885 (with-current-buffer (window-buffer (selected-window))
2887 (dolist (overlay (overlays-in pos pos
))
2888 (when (overlay-get overlay
'put-break
)
2889 (setq obj
(overlay-get overlay
'before-string
))))
2893 (if (get-text-property 0 'gdb-enabled obj
)
2896 (get-text-property 0 'gdb-bptno obj
)))))))))
2898 (defun gdb-breakpoints-buffer-name ()
2899 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2901 (defun gdb-display-breakpoints-buffer (&optional thread
)
2902 "Display GDB breakpoints."
2904 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)))
2906 (defun gdb-frame-breakpoints-buffer (&optional thread
)
2907 "Display GDB breakpoints in another frame."
2909 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread
)
2910 gdb-display-buffer-other-frame-action
))
2912 (defvar gdb-breakpoints-mode-map
2913 (let ((map (make-sparse-keymap))
2914 (menu (make-sparse-keymap "Breakpoints")))
2915 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
2916 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2917 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2918 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2919 (suppress-keymap map)
2920 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2921 (define-key map " " 'gdb-toggle-breakpoint)
2922 (define-key map "D" 'gdb-delete-breakpoint)
2923 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2924 (define-key map "q" 'gdb-delete-frame-or-window)
2925 (define-key map "\r" 'gdb-goto-breakpoint)
2926 (define-key map "\t" (lambda ()
2928 (gdb-set-window-buffer
2929 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2930 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2931 (define-key map [follow-link] 'mouse-face)
2934 (defun gdb-delete-frame-or-window ()
2935 "Delete frame if there is only one window. Otherwise delete the window."
2937 (if (one-window-p) (delete-frame)
2940 ;;from make-mode-line-mouse-map
2941 (defun gdb-make-header-line-mouse-map (mouse function) "\
2942 Return a keymap with single entry for mouse key MOUSE on the header line.
2943 MOUSE is defined to run function FUNCTION with no args in the buffer
2944 corresponding to the mode line clicked."
2945 (let ((map (make-sparse-keymap)))
2946 (define-key map (vector 'header-line mouse) function)
2947 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2950 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2952 'help-echo ,help-echo
2953 'mouse-face ',mouse-face
2956 (gdb-make-header-line-mouse-map
2958 (lambda (event) (interactive "e")
2959 (save-selected-window
2960 (select-window (posn-window (event-start event)))
2961 (gdb-set-window-buffer
2962 (gdb-get-buffer-create ',buffer) t) )))))
2965 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2968 (defun gdb-threads-buffer-name ()
2969 (concat "*threads of " (gdb-get-target-string) "*"))
2971 (defun gdb-display-threads-buffer (&optional thread)
2972 "Display GDB threads."
2974 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
2976 (defun gdb-frame-threads-buffer (&optional thread)
2977 "Display GDB threads in another frame."
2979 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
2980 gdb-display-buffer-other-frame-action))
2982 (def-gdb-trigger-and-handler
2983 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2984 gdb-thread-list-handler gdb-thread-list-handler-custom
2985 '(start update update-threads))
2987 (gdb-set-buffer-rules
2989 'gdb-threads-buffer-name
2991 'gdb-invalidate-threads)
2993 (defvar gdb-threads-font-lock-keywords
2994 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2995 (" \\(stopped\\)" (1 font-lock-warning-face))
2996 (" \\(running\\)" (1 font-lock-string-face))
2997 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2998 "Font lock keywords used in `gdb-threads-mode'.")
3000 (defvar gdb-threads-mode-map
3001 (let ((map (make-sparse-keymap)))
3002 (define-key map "\r" 'gdb-select-thread)
3003 (define-key map "f" 'gdb-display-stack-for-thread)
3004 (define-key map "F" 'gdb-frame-stack-for-thread)
3005 (define-key map "l" 'gdb-display-locals-for-thread)
3006 (define-key map "L" 'gdb-frame-locals-for-thread)
3007 (define-key map "r" 'gdb-display-registers-for-thread)
3008 (define-key map "R" 'gdb-frame-registers-for-thread)
3009 (define-key map "d" 'gdb-display-disassembly-for-thread)
3010 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3011 (define-key map "i" 'gdb-interrupt-thread)
3012 (define-key map "c" 'gdb-continue-thread)
3013 (define-key map "s" 'gdb-step-thread)
3014 (define-key map "\t"
3017 (gdb-set-window-buffer
3018 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3019 (define-key map [mouse-2] 'gdb-select-thread)
3020 (define-key map [follow-link] 'mouse-face)
3023 (defvar gdb-threads-header
3025 (gdb-propertize-header
3026 "Breakpoints" gdb-breakpoints-buffer
3027 "mouse-1: select" mode-line-highlight mode-line-inactive)
3029 (gdb-propertize-header "Threads" gdb-threads-buffer
3030 nil nil mode-line)))
3032 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3033 "Major mode for GDB threads."
3034 (setq gdb-thread-position (make-marker))
3035 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3036 (setq header-line-format gdb-threads-header)
3037 (set (make-local-variable 'font-lock-defaults)
3038 '(gdb-threads-font-lock-keywords))
3039 'gdb-invalidate-threads)
3041 (defun gdb-thread-list-handler-custom ()
3042 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3043 (table (make-gdb-table))
3045 (setq gdb-threads-list nil)
3046 (setq gdb-running-threads-count 0)
3047 (setq gdb-stopped-threads-count 0)
3048 (set-marker gdb-thread-position nil)
3050 (dolist (thread (reverse threads-list))
3051 (let ((running (equal (bindat-get-field thread 'state) "running")))
3052 (add-to-list 'gdb-threads-list
3053 (cons (bindat-get-field thread 'id)
3055 (cl-incf (if running
3056 gdb-running-threads-count
3057 gdb-stopped-threads-count))
3062 (bindat-get-field thread 'id)
3064 (if gdb-thread-buffer-verbose-names
3065 (concat (bindat-get-field thread 'target-id) " ") "")
3066 (bindat-get-field thread 'state)
3067 ;; Include frame information for stopped threads
3070 " in " (bindat-get-field thread 'frame 'func)
3071 (if gdb-thread-buffer-arguments
3074 (let ((args (bindat-get-field thread 'frame 'args)))
3077 (apply #'format "%s=%s"
3078 (gdb-get-many-fields arg 'name 'value)))
3082 (if gdb-thread-buffer-locations
3083 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3084 (if gdb-thread-buffer-addresses
3085 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3089 'mouse-face 'highlight
3090 'help-echo "mouse-2, RET: select thread")))
3091 (when (string-equal gdb-thread-number
3092 (bindat-get-field thread 'id))
3093 (setq marked-line (length gdb-threads-list))))
3094 (insert (gdb-table-string table " "))
3096 (gdb-mark-line marked-line gdb-thread-position)))
3097 ;; We update gud-running here because we need to make sure that
3098 ;; gdb-threads-list is up-to-date
3099 (gdb-update-gud-running)
3100 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3102 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3103 "Define a NAME command which will act upon thread on the current line.
3105 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3106 be the value of 'gdb-thread property of the current line.
3107 If `gdb-thread' is nil, error is signaled."
3108 `(defun ,name (&optional event)
3110 (interactive (list last-input-event))
3111 (if event (posn-set-point (event-end event)))
3114 (let ((thread (get-text-property (point) 'gdb-thread)))
3117 (error "Not recognized as thread line"))))))
3119 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3121 "Define a NAME which will call BUFFER-COMMAND with id of thread
3122 on the current line."
3123 `(def-gdb-thread-buffer-command ,name
3124 (,buffer-command (bindat-get-field thread 'id))
3127 (def-gdb-thread-buffer-command gdb-select-thread
3128 (let ((new-id (bindat-get-field thread 'id)))
3129 (gdb-setq-thread-number new-id)
3130 (gdb-input (concat "-thread-select " new-id) 'ignore)
3132 "Select the thread at current line of threads buffer.")
3134 (def-gdb-thread-buffer-simple-command
3135 gdb-display-stack-for-thread
3136 gdb-preemptively-display-stack-buffer
3137 "Display stack buffer for the thread at current line.")
3139 (def-gdb-thread-buffer-simple-command
3140 gdb-display-locals-for-thread
3141 gdb-preemptively-display-locals-buffer
3142 "Display locals buffer for the thread at current line.")
3144 (def-gdb-thread-buffer-simple-command
3145 gdb-display-registers-for-thread
3146 gdb-preemptively-display-registers-buffer
3147 "Display registers buffer for the thread at current line.")
3149 (def-gdb-thread-buffer-simple-command
3150 gdb-display-disassembly-for-thread
3151 gdb-preemptively-display-disassembly-buffer
3152 "Display disassembly buffer for the thread at current line.")
3154 (def-gdb-thread-buffer-simple-command
3155 gdb-frame-stack-for-thread
3156 gdb-frame-stack-buffer
3157 "Display another frame with stack buffer for thread at current line.")
3159 (def-gdb-thread-buffer-simple-command
3160 gdb-frame-locals-for-thread
3161 gdb-frame-locals-buffer
3162 "Display another frame with locals buffer for thread at current line.")
3164 (def-gdb-thread-buffer-simple-command
3165 gdb-frame-registers-for-thread
3166 gdb-frame-registers-buffer
3167 "Display another frame with registers buffer for the thread at current line.")
3169 (def-gdb-thread-buffer-simple-command
3170 gdb-frame-disassembly-for-thread
3171 gdb-frame-disassembly-buffer
3172 "Display another frame with disassembly buffer for the thread at current line.")
3174 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3175 "Define a NAME which will execute GUD-COMMAND with
3176 `gdb-thread-number' locally bound to id of thread on the current
3178 `(def-gdb-thread-buffer-command ,name
3180 (let ((gdb-thread-number (bindat-get-field thread 'id))
3181 (gdb-gud-control-all-threads nil))
3182 (call-interactively #',gud-command))
3183 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3186 (def-gdb-thread-buffer-gud-command
3187 gdb-interrupt-thread
3189 "Interrupt thread at current line.")
3191 (def-gdb-thread-buffer-gud-command
3194 "Continue thread at current line.")
3196 (def-gdb-thread-buffer-gud-command
3199 "Step thread at current line.")
3204 (defcustom gdb-memory-rows 8
3205 "Number of data rows in memory window."
3210 (defcustom gdb-memory-columns 4
3211 "Number of data columns in memory window."
3216 (defcustom gdb-memory-format "x"
3217 "Display format of data items in memory window."
3218 :type '(choice (const :tag "Hexadecimal" "x")
3219 (const :tag "Signed decimal" "d")
3220 (const :tag "Unsigned decimal" "u")
3221 (const :tag "Octal" "o")
3222 (const :tag "Binary" "t"))
3226 (defcustom gdb-memory-unit 4
3227 "Unit size of data items in memory window."
3228 :type '(choice (const :tag "Byte" 1)
3229 (const :tag "Halfword" 2)
3230 (const :tag "Word" 4)
3231 (const :tag "Giant word" 8))
3235 (def-gdb-trigger-and-handler
3236 gdb-invalidate-memory
3237 (format "-data-read-memory %s %s %d %d %d"
3243 gdb-read-memory-handler
3244 gdb-read-memory-custom
3247 (gdb-set-buffer-rules
3249 'gdb-memory-buffer-name
3251 'gdb-invalidate-memory)
3253 (defun gdb-memory-column-width (size format)
3254 "Return length of string with memory unit of SIZE in FORMAT.
3256 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3257 in `gdb-memory-format'."
3258 (let ((format-base (cdr (assoc format
3260 ("d" . 10) ("u" . 10)
3264 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3265 (cond ((string-equal format "x")
3266 (+ 2 res)) ; hexadecimal numbers have 0x in front
3267 ((or (string-equal format "d")
3268 (string-equal format "o"))
3271 (error "Unknown format"))))
3273 (defun gdb-read-memory-custom ()
3274 (let* ((res (gdb-json-partial-output))
3275 (err-msg (bindat-get-field res 'msg)))
3277 (let ((memory (bindat-get-field res 'memory)))
3278 (setq gdb-memory-address (bindat-get-field res 'addr))
3279 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3280 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3281 (setq gdb-memory-last-address gdb-memory-address)
3282 (dolist (row memory)
3283 (insert (concat (bindat-get-field row 'addr) ":"))
3284 (dolist (column (bindat-get-field row 'data))
3285 (insert (gdb-pad-string column
3286 (+ 2 (gdb-memory-column-width
3288 gdb-memory-format)))))
3290 ;; Show last page instead of empty buffer when out of bounds
3292 (let ((gdb-memory-address gdb-memory-last-address))
3293 (gdb-invalidate-memory 'update)
3294 (error err-msg))))))
3296 (defvar gdb-memory-mode-map
3297 (let ((map (make-sparse-keymap)))
3298 (suppress-keymap map t)
3299 (define-key map "q" 'kill-this-buffer)
3300 (define-key map "n" 'gdb-memory-show-next-page)
3301 (define-key map "p" 'gdb-memory-show-previous-page)
3302 (define-key map "a" 'gdb-memory-set-address)
3303 (define-key map "t" 'gdb-memory-format-binary)
3304 (define-key map "o" 'gdb-memory-format-octal)
3305 (define-key map "u" 'gdb-memory-format-unsigned)
3306 (define-key map "d" 'gdb-memory-format-signed)
3307 (define-key map "x" 'gdb-memory-format-hexadecimal)
3308 (define-key map "b" 'gdb-memory-unit-byte)
3309 (define-key map "h" 'gdb-memory-unit-halfword)
3310 (define-key map "w" 'gdb-memory-unit-word)
3311 (define-key map "g" 'gdb-memory-unit-giant)
3312 (define-key map "R" 'gdb-memory-set-rows)
3313 (define-key map "C" 'gdb-memory-set-columns)
3316 (defun gdb-memory-set-address-event (event)
3317 "Handle a click on address field in memory buffer header."
3319 (save-selected-window
3320 (select-window (posn-window (event-start event)))
3321 (gdb-memory-set-address)))
3323 ;; Non-event version for use within keymap
3324 (defun gdb-memory-set-address ()
3325 "Set the start memory address."
3327 (let ((arg (read-from-minibuffer "Memory address: ")))
3328 (setq gdb-memory-address arg))
3329 (gdb-invalidate-memory 'update))
3331 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3332 "Define a function NAME which reads new VAR value from minibuffer."
3333 `(defun ,name (event)
3336 (save-selected-window
3337 (select-window (posn-window (event-start event)))
3338 (let* ((arg (read-from-minibuffer ,echo-string))
3339 (count (string-to-number arg)))
3341 (error "Positive number only")
3342 (customize-set-variable ',variable count)
3343 (gdb-invalidate-memory 'update))))))
3345 (def-gdb-set-positive-number
3349 "Set the number of data rows in memory window.")
3351 (def-gdb-set-positive-number
3352 gdb-memory-set-columns
3355 "Set the number of data columns in memory window.")
3357 (defmacro def-gdb-memory-format (name format doc)
3358 "Define a function NAME to switch memory buffer to use FORMAT.
3360 DOC is an optional documentation string."
3361 `(defun ,name () ,(when doc doc)
3363 (customize-set-variable 'gdb-memory-format ,format)
3364 (gdb-invalidate-memory 'update)))
3366 (def-gdb-memory-format
3367 gdb-memory-format-binary "t"
3368 "Set the display format to binary.")
3370 (def-gdb-memory-format
3371 gdb-memory-format-octal "o"
3372 "Set the display format to octal.")
3374 (def-gdb-memory-format
3375 gdb-memory-format-unsigned "u"
3376 "Set the display format to unsigned decimal.")
3378 (def-gdb-memory-format
3379 gdb-memory-format-signed "d"
3380 "Set the display format to decimal.")
3382 (def-gdb-memory-format
3383 gdb-memory-format-hexadecimal "x"
3384 "Set the display format to hexadecimal.")
3386 (defvar gdb-memory-format-map
3387 (let ((map (make-sparse-keymap)))
3388 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3390 "Keymap to select format in the header line.")
3392 (defvar gdb-memory-format-menu
3393 (let ((map (make-sparse-keymap "Format")))
3395 (define-key map [binary]
3396 '(menu-item "Binary" gdb-memory-format-binary
3397 :button (:radio . (equal gdb-memory-format "t"))))
3398 (define-key map [octal]
3399 '(menu-item "Octal" gdb-memory-format-octal
3400 :button (:radio . (equal gdb-memory-format "o"))))
3401 (define-key map [unsigned]
3402 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3403 :button (:radio . (equal gdb-memory-format "u"))))
3404 (define-key map [signed]
3405 '(menu-item "Signed Decimal" gdb-memory-format-signed
3406 :button (:radio . (equal gdb-memory-format "d"))))
3407 (define-key map [hexadecimal]
3408 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3409 :button (:radio . (equal gdb-memory-format "x"))))
3411 "Menu of display formats in the header line.")
3413 (defun gdb-memory-format-menu (event)
3415 (x-popup-menu event gdb-memory-format-menu))
3417 (defun gdb-memory-format-menu-1 (event)
3419 (save-selected-window
3420 (select-window (posn-window (event-start event)))
3421 (let* ((selection (gdb-memory-format-menu event))
3422 (binding (and selection (lookup-key gdb-memory-format-menu
3423 (vector (car selection))))))
3424 (if binding (call-interactively binding)))))
3426 (defmacro def-gdb-memory-unit (name unit-size doc)
3427 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3429 DOC is an optional documentation string."
3430 `(defun ,name () ,(when doc doc)
3432 (customize-set-variable 'gdb-memory-unit ,unit-size)
3433 (gdb-invalidate-memory 'update)))
3435 (def-gdb-memory-unit gdb-memory-unit-giant 8
3436 "Set the unit size to giant words (eight bytes).")
3438 (def-gdb-memory-unit gdb-memory-unit-word 4
3439 "Set the unit size to words (four bytes).")
3441 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3442 "Set the unit size to halfwords (two bytes).")
3444 (def-gdb-memory-unit gdb-memory-unit-byte 1
3445 "Set the unit size to bytes.")
3447 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3448 "Define a function NAME which show new address in memory buffer.
3450 The defined function switches Memory buffer to show address
3451 stored in ADDRESS-VAR variable.
3453 DOC is an optional documentation string."
3457 (let ((gdb-memory-address ,address-var))
3458 (gdb-invalidate-memory))))
3460 (def-gdb-memory-show-page gdb-memory-show-previous-page
3461 gdb-memory-prev-page)
3463 (def-gdb-memory-show-page gdb-memory-show-next-page
3464 gdb-memory-next-page)
3466 (defvar gdb-memory-unit-map
3467 (let ((map (make-sparse-keymap)))
3468 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3470 "Keymap to select units in the header line.")
3472 (defvar gdb-memory-unit-menu
3473 (let ((map (make-sparse-keymap "Unit")))
3474 (define-key map [giantwords]
3475 '(menu-item "Giant words" gdb-memory-unit-giant
3476 :button (:radio . (equal gdb-memory-unit 8))))
3477 (define-key map [words]
3478 '(menu-item "Words" gdb-memory-unit-word
3479 :button (:radio . (equal gdb-memory-unit 4))))
3480 (define-key map [halfwords]
3481 '(menu-item "Halfwords" gdb-memory-unit-halfword
3482 :button (:radio . (equal gdb-memory-unit 2))))
3483 (define-key map [bytes]
3484 '(menu-item "Bytes" gdb-memory-unit-byte
3485 :button (:radio . (equal gdb-memory-unit 1))))
3487 "Menu of units in the header line.")
3489 (defun gdb-memory-unit-menu (event)
3491 (x-popup-menu event gdb-memory-unit-menu))
3493 (defun gdb-memory-unit-menu-1 (event)
3495 (save-selected-window
3496 (select-window (posn-window (event-start event)))
3497 (let* ((selection (gdb-memory-unit-menu event))
3498 (binding (and selection (lookup-key gdb-memory-unit-menu
3499 (vector (car selection))))))
3500 (if binding (call-interactively binding)))))
3502 (defvar gdb-memory-font-lock-keywords
3503 '(;; <__function.name+n>
3504 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3505 (1 font-lock-function-name-face)))
3506 "Font lock keywords used in `gdb-memory-mode'.")
3508 (defvar gdb-memory-header
3513 'face font-lock-warning-face
3514 'help-echo "mouse-1: decrement address"
3515 'mouse-face 'mode-line-highlight
3516 'local-map (gdb-make-header-line-mouse-map
3518 #'gdb-memory-show-previous-page))
3521 'face font-lock-warning-face
3522 'help-echo "mouse-1: increment address"
3523 'mouse-face 'mode-line-highlight
3524 'local-map (gdb-make-header-line-mouse-map
3526 #'gdb-memory-show-next-page))
3528 (propertize gdb-memory-address
3529 'face font-lock-warning-face
3530 'help-echo "mouse-1: set start address"
3531 'mouse-face 'mode-line-highlight
3532 'local-map (gdb-make-header-line-mouse-map
3534 #'gdb-memory-set-address-event))
3536 (propertize (number-to-string gdb-memory-rows)
3537 'face font-lock-warning-face
3538 'help-echo "mouse-1: set number of columns"
3539 'mouse-face 'mode-line-highlight
3540 'local-map (gdb-make-header-line-mouse-map
3542 #'gdb-memory-set-rows))
3544 (propertize (number-to-string gdb-memory-columns)
3545 'face font-lock-warning-face
3546 'help-echo "mouse-1: set number of columns"
3547 'mouse-face 'mode-line-highlight
3548 'local-map (gdb-make-header-line-mouse-map
3550 #'gdb-memory-set-columns))
3552 (propertize gdb-memory-format
3553 'face font-lock-warning-face
3554 'help-echo "mouse-3: select display format"
3555 'mouse-face 'mode-line-highlight
3556 'local-map gdb-memory-format-map)
3558 (propertize (number-to-string gdb-memory-unit)
3559 'face font-lock-warning-face
3560 'help-echo "mouse-3: select unit size"
3561 'mouse-face 'mode-line-highlight
3562 'local-map gdb-memory-unit-map)))
3563 "Header line used in `gdb-memory-mode'.")
3565 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3566 "Major mode for examining memory."
3567 (setq header-line-format gdb-memory-header)
3568 (set (make-local-variable 'font-lock-defaults)
3569 '(gdb-memory-font-lock-keywords))
3570 'gdb-invalidate-memory)
3572 (defun gdb-memory-buffer-name ()
3573 (concat "*memory of " (gdb-get-target-string) "*"))
3575 (defun gdb-display-memory-buffer (&optional thread)
3576 "Display GDB memory contents."
3578 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3580 (defun gdb-frame-memory-buffer ()
3581 "Display memory contents in another frame."
3583 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3584 gdb-display-buffer-other-frame-action))
3587 ;;; Disassembly view
3589 (defun gdb-disassembly-buffer-name ()
3590 (gdb-current-context-buffer-name
3591 (concat "disassembly of " (gdb-get-target-string))))
3593 (defun gdb-display-disassembly-buffer (&optional thread)
3594 "Display GDB disassembly information."
3596 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3598 (def-gdb-preempt-display-buffer
3599 gdb-preemptively-display-disassembly-buffer
3600 'gdb-disassembly-buffer)
3602 (defun gdb-frame-disassembly-buffer (&optional thread)
3603 "Display GDB disassembly information in another frame."
3605 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3606 gdb-display-buffer-other-frame-action))
3608 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3609 (let* ((frame (gdb-current-buffer-frame))
3610 (file (bindat-get-field frame 'fullname))
3611 (line (bindat-get-field frame 'line)))
3613 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3614 ;; If we're unable to get a file name / line for $PC, simply
3615 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3616 ;; 150 bytes) instructions.
3617 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3618 gdb-disassembly-handler
3619 ;; We update disassembly only after we have actual frame information
3620 ;; about all threads, so no there's `update' signal in this list
3621 '(start update-disassembly))
3623 (def-gdb-auto-update-handler
3624 gdb-disassembly-handler
3625 gdb-invalidate-disassembly
3626 gdb-disassembly-handler-custom
3629 (gdb-set-buffer-rules
3630 'gdb-disassembly-buffer
3631 'gdb-disassembly-buffer-name
3632 'gdb-disassembly-mode
3633 'gdb-invalidate-disassembly)
3635 (defvar gdb-disassembly-font-lock-keywords
3636 '(;; <__function.name+n>
3637 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3638 (1 font-lock-function-name-face))
3639 ;; 0xNNNNNNNN <__function.name+n>: opcode
3640 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3641 (4 font-lock-keyword-face))
3642 ;; %register(at least i386)
3643 ("%\\sw+" . font-lock-variable-name-face)
3644 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3645 (1 font-lock-comment-face)
3646 (2 font-lock-function-name-face))
3647 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3648 "Font lock keywords used in `gdb-disassembly-mode'.")
3650 (defvar gdb-disassembly-mode-map
3652 (let ((map (make-sparse-keymap)))
3653 (suppress-keymap map)
3654 (define-key map "q" 'kill-this-buffer)
3657 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3658 "Major mode for GDB disassembly information."
3659 ;; TODO Rename overlay variable for disassembly mode
3660 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3661 (setq fringes-outside-margins t)
3662 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3663 (set (make-local-variable 'font-lock-defaults)
3664 '(gdb-disassembly-font-lock-keywords))
3665 'gdb-invalidate-disassembly)
3667 (defun gdb-disassembly-handler-custom ()
3668 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3669 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3670 (table (make-gdb-table))
3672 (dolist (instr instructions)
3673 (gdb-table-add-row table
3675 (bindat-get-field instr 'address)
3677 ((func-name (bindat-get-field instr 'func-name))
3678 (offset (bindat-get-field instr 'offset)))
3680 (format "<%s+%s>:" func-name offset)
3682 (bindat-get-field instr 'inst)))
3683 (when (string-equal (bindat-get-field instr 'address)
3686 (setq marked-line (length (gdb-table-rows table)))
3687 (setq fringe-indicator-alist
3688 (if (string-equal gdb-frame-number "0")
3690 '((overlay-arrow . hollow-right-triangle)))))))
3691 (insert (gdb-table-string table " "))
3692 (gdb-disassembly-place-breakpoints)
3693 ;; Mark current position with overlay arrow and scroll window to
3696 (let ((window (get-buffer-window (current-buffer) 0)))
3697 (set-window-point window (gdb-mark-line marked-line
3698 gdb-disassembly-position))))
3700 (gdb-current-context-mode-name
3701 (concat "Disassembly: "
3702 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3704 (defun gdb-disassembly-place-breakpoints ()
3705 (gdb-remove-breakpoint-icons (point-min) (point-max))
3706 (dolist (breakpoint gdb-breakpoints-list)
3707 (let* ((breakpoint (cdr breakpoint))
3708 (bptno (bindat-get-field breakpoint 'number))
3709 (flag (bindat-get-field breakpoint 'enabled))
3710 (address (bindat-get-field breakpoint 'addr)))
3712 (goto-char (point-min))
3713 (if (re-search-forward (concat "^" address) nil t)
3714 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3717 (defvar gdb-breakpoints-header
3719 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3722 (gdb-propertize-header "Threads" gdb-threads-buffer
3723 "mouse-1: select" mode-line-highlight
3724 mode-line-inactive)))
3726 ;;; Breakpoints view
3727 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3728 "Major mode for gdb breakpoints."
3729 (setq header-line-format gdb-breakpoints-header)
3730 'gdb-invalidate-breakpoints)
3732 (defun gdb-toggle-breakpoint ()
3733 "Enable/disable breakpoint at current line of breakpoints buffer."
3737 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3740 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3743 (bindat-get-field breakpoint 'number)))
3744 (error "Not recognized as break/watchpoint line")))))
3746 (defun gdb-delete-breakpoint ()
3747 "Delete the breakpoint at current line of breakpoints buffer."
3751 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3753 (gud-basic-call (concat "-break-delete "
3754 (bindat-get-field breakpoint 'number)))
3755 (error "Not recognized as break/watchpoint line")))))
3757 (defun gdb-goto-breakpoint (&optional event)
3758 "Go to the location of breakpoint at current line of breakpoints buffer."
3759 (interactive (list last-input-event))
3760 (if event (posn-set-point (event-end event)))
3761 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3762 (let ((window (get-buffer-window gud-comint-buffer)))
3763 (if window (save-selected-window (select-window window))))
3766 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3768 (let ((bptno (bindat-get-field breakpoint 'number))
3769 (file (bindat-get-field breakpoint 'fullname))
3770 (line (bindat-get-field breakpoint 'line)))
3771 (save-selected-window
3772 (let* ((buffer (find-file-noselect
3773 (if (file-exists-p file) file
3774 (cdr (assoc bptno gdb-location-alist)))))
3775 (window (or (gdb-display-source-buffer buffer)
3776 (display-buffer buffer))))
3777 (setq gdb-source-window window)
3778 (with-current-buffer buffer
3779 (goto-char (point-min))
3780 (forward-line (1- (string-to-number line)))
3781 (set-window-point window (point))))))
3782 (error "Not recognized as break/watchpoint line")))))
3785 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3787 (def-gdb-trigger-and-handler
3788 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3789 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3792 (gdb-set-buffer-rules
3794 'gdb-stack-buffer-name
3796 'gdb-invalidate-frames)
3798 (defun gdb-frame-location (frame)
3799 "Return \" of file:line\" or \" of library\" for structure FRAME.
3801 FRAME must have either \"file\" and \"line\" members or \"from\"
3803 (let ((file (bindat-get-field frame 'file))
3804 (line (bindat-get-field frame 'line))
3805 (from (bindat-get-field frame 'from)))
3806 (let ((res (or (and file line (concat file ":" line))
3808 (if res (concat " of " res) ""))))
3810 (defun gdb-stack-list-frames-custom ()
3811 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3812 (table (make-gdb-table)))
3813 (set-marker gdb-stack-position nil)
3814 (dolist (frame stack)
3815 (gdb-table-add-row table
3817 (bindat-get-field frame 'level)
3820 (bindat-get-field frame 'func)
3821 (if gdb-stack-buffer-locations
3822 (gdb-frame-location frame) "")
3823 (if gdb-stack-buffer-addresses
3824 (concat " at " (bindat-get-field frame 'addr)) "")))
3825 `(mouse-face highlight
3826 help-echo "mouse-2, RET: Select frame"
3828 (insert (gdb-table-string table " ")))
3829 (when (and gdb-frame-number
3830 (gdb-buffer-shows-main-thread-p))
3831 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3832 gdb-stack-position))
3834 (gdb-current-context-mode-name "Frames")))
3836 (defun gdb-stack-buffer-name ()
3837 (gdb-current-context-buffer-name
3838 (concat "stack frames of " (gdb-get-target-string))))
3840 (defun gdb-display-stack-buffer (&optional thread)
3841 "Display GDB backtrace for current stack."
3843 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
3845 (def-gdb-preempt-display-buffer
3846 gdb-preemptively-display-stack-buffer
3847 'gdb-stack-buffer nil t)
3849 (defun gdb-frame-stack-buffer (&optional thread)
3850 "Display GDB backtrace for current stack in another frame."
3852 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
3853 gdb-display-buffer-other-frame-action))
3855 (defvar gdb-frames-mode-map
3856 (let ((map (make-sparse-keymap)))
3857 (suppress-keymap map)
3858 (define-key map "q" 'kill-this-buffer)
3859 (define-key map "\r" 'gdb-select-frame)
3860 (define-key map [mouse-2] 'gdb-select-frame)
3861 (define-key map [follow-link] 'mouse-face)
3864 (defvar gdb-frames-font-lock-keywords
3865 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3866 "Font lock keywords used in `gdb-frames-mode'.")
3868 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3869 "Major mode for gdb call stack."
3870 (setq gdb-stack-position (make-marker))
3871 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3872 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3873 (set (make-local-variable 'font-lock-defaults)
3874 '(gdb-frames-font-lock-keywords))
3875 'gdb-invalidate-frames)
3877 (defun gdb-select-frame (&optional event)
3878 "Select the frame and display the relevant source."
3879 (interactive (list last-input-event))
3880 (if event (posn-set-point (event-end event)))
3881 (let ((frame (get-text-property (point) 'gdb-frame)))
3883 (if (gdb-buffer-shows-main-thread-p)
3884 (let ((new-level (bindat-get-field frame 'level)))
3885 (setq gdb-frame-number new-level)
3886 (gdb-input (concat "-stack-select-frame " new-level)
3889 (error "Could not select frame for non-current thread"))
3890 (error "Not recognized as frame line"))))
3894 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3895 (def-gdb-trigger-and-handler
3896 gdb-invalidate-locals
3897 (concat (gdb-current-context-command "-stack-list-locals")
3899 gdb-locals-handler gdb-locals-handler-custom
3902 (gdb-set-buffer-rules
3904 'gdb-locals-buffer-name
3906 'gdb-invalidate-locals)
3908 (defvar gdb-locals-watch-map
3909 (let ((map (make-sparse-keymap)))
3910 (suppress-keymap map)
3911 (define-key map "\r" 'gud-watch)
3912 (define-key map [mouse-2] 'gud-watch)
3914 "Keymap to create watch expression of a complex data type local variable.")
3916 (defvar gdb-edit-locals-map-1
3917 (let ((map (make-sparse-keymap)))
3918 (suppress-keymap map)
3919 (define-key map "\r" 'gdb-edit-locals-value)
3920 (define-key map [mouse-2] 'gdb-edit-locals-value)
3922 "Keymap to edit value of a simple data type local variable.")
3924 (defun gdb-edit-locals-value (&optional event)
3925 "Assign a value to a variable displayed in the locals buffer."
3926 (interactive (list last-input-event))
3928 (if event (posn-set-point (event-end event)))
3930 (let* ((var (bindat-get-field
3931 (get-text-property (point) 'gdb-local-variable) 'name))
3932 (value (read-string (format "New value (%s): " var))))
3934 (concat "-gdb-set variable " var " = " value)))))
3936 ;; Don't display values of arrays or structures.
3937 ;; These can be expanded using gud-watch.
3938 (defun gdb-locals-handler-custom ()
3939 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3940 (table (make-gdb-table)))
3941 (dolist (local locals-list)
3942 (let ((name (bindat-get-field local 'name))
3943 (value (bindat-get-field local 'value))
3944 (type (bindat-get-field local 'type)))
3946 (string-match "\\0x" value))
3947 (add-text-properties 0 (length name)
3948 `(mouse-face highlight
3949 help-echo "mouse-2: create watch expression"
3950 local-map ,gdb-locals-watch-map)
3952 (add-text-properties 0 (length value)
3953 `(mouse-face highlight
3954 help-echo "mouse-2: edit value"
3955 local-map ,gdb-edit-locals-map-1)
3960 (propertize type 'font-lock-face font-lock-type-face)
3961 (propertize name 'font-lock-face font-lock-variable-name-face)
3963 `(gdb-local-variable ,local))))
3964 (insert (gdb-table-string table " "))
3966 (gdb-current-context-mode-name
3968 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3970 (defvar gdb-locals-header
3972 (gdb-propertize-header "Locals" gdb-locals-buffer
3975 (gdb-propertize-header "Registers" gdb-registers-buffer
3976 "mouse-1: select" mode-line-highlight
3977 mode-line-inactive)))
3979 (defvar gdb-locals-mode-map
3980 (let ((map (make-sparse-keymap)))
3981 (suppress-keymap map)
3982 (define-key map "q" 'kill-this-buffer)
3983 (define-key map "\t" (lambda ()
3985 (gdb-set-window-buffer
3986 (gdb-get-buffer-create
3987 'gdb-registers-buffer
3988 gdb-thread-number) t)))
3991 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3992 "Major mode for gdb locals."
3993 (setq header-line-format gdb-locals-header)
3994 'gdb-invalidate-locals)
3996 (defun gdb-locals-buffer-name ()
3997 (gdb-current-context-buffer-name
3998 (concat "locals of " (gdb-get-target-string))))
4000 (defun gdb-display-locals-buffer (&optional thread)
4001 "Display the local variables of current GDB stack."
4003 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4005 (def-gdb-preempt-display-buffer
4006 gdb-preemptively-display-locals-buffer
4007 'gdb-locals-buffer nil t)
4009 (defun gdb-frame-locals-buffer (&optional thread)
4010 "Display the local variables of the current GDB stack in another frame."
4012 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4013 gdb-display-buffer-other-frame-action))
4016 ;; Registers buffer.
4018 (def-gdb-trigger-and-handler
4019 gdb-invalidate-registers
4020 (concat (gdb-current-context-command "-data-list-register-values") " x")
4021 gdb-registers-handler
4022 gdb-registers-handler-custom
4025 (gdb-set-buffer-rules
4026 'gdb-registers-buffer
4027 'gdb-registers-buffer-name
4029 'gdb-invalidate-registers)
4031 (defun gdb-registers-handler-custom ()
4032 (when gdb-register-names
4033 (let ((register-values
4034 (bindat-get-field (gdb-json-partial-output) 'register-values))
4035 (table (make-gdb-table)))
4036 (dolist (register register-values)
4037 (let* ((register-number (bindat-get-field register 'number))
4038 (value (bindat-get-field register 'value))
4039 (register-name (nth (string-to-number register-number)
4040 gdb-register-names)))
4044 (propertize register-name
4045 'font-lock-face font-lock-variable-name-face)
4046 (if (member register-number gdb-changed-registers)
4047 (propertize value 'font-lock-face font-lock-warning-face)
4049 `(mouse-face highlight
4050 help-echo "mouse-2: edit value"
4051 gdb-register-name ,register-name))))
4052 (insert (gdb-table-string table " ")))
4054 (gdb-current-context-mode-name "Registers"))))
4056 (defun gdb-edit-register-value (&optional event)
4057 "Assign a value to a register displayed in the registers buffer."
4058 (interactive (list last-input-event))
4060 (if event (posn-set-point (event-end event)))
4062 (let* ((var (bindat-get-field
4063 (get-text-property (point) 'gdb-register-name)))
4064 (value (read-string (format "New value (%s): " var))))
4066 (concat "-gdb-set variable $" var " = " value)))))
4068 (defvar gdb-registers-mode-map
4069 (let ((map (make-sparse-keymap)))
4070 (suppress-keymap map)
4071 (define-key map "\r" 'gdb-edit-register-value)
4072 (define-key map [mouse-2] 'gdb-edit-register-value)
4073 (define-key map "q" 'kill-this-buffer)
4074 (define-key map "\t" (lambda ()
4076 (gdb-set-window-buffer
4077 (gdb-get-buffer-create
4079 gdb-thread-number) t)))
4082 (defvar gdb-registers-header
4084 (gdb-propertize-header "Locals" gdb-locals-buffer
4085 "mouse-1: select" mode-line-highlight
4088 (gdb-propertize-header "Registers" gdb-registers-buffer
4089 nil nil mode-line)))
4091 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4092 "Major mode for gdb registers."
4093 (setq header-line-format gdb-registers-header)
4094 'gdb-invalidate-registers)
4096 (defun gdb-registers-buffer-name ()
4097 (gdb-current-context-buffer-name
4098 (concat "registers of " (gdb-get-target-string))))
4100 (defun gdb-display-registers-buffer (&optional thread)
4101 "Display GDB register contents."
4103 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4105 (def-gdb-preempt-display-buffer
4106 gdb-preemptively-display-registers-buffer
4107 'gdb-registers-buffer nil t)
4109 (defun gdb-frame-registers-buffer (&optional thread)
4110 "Display GDB register contents in another frame."
4112 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4113 gdb-display-buffer-other-frame-action))
4115 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4116 (defun gdb-get-changed-registers ()
4117 (when (and (gdb-get-buffer 'gdb-registers-buffer)
4118 (not (gdb-pending-p 'gdb-get-changed-registers)))
4119 (gdb-input "-data-list-changed-registers"
4120 'gdb-changed-registers-handler)
4121 (gdb-add-pending 'gdb-get-changed-registers)))
4123 (defun gdb-changed-registers-handler ()
4124 (gdb-delete-pending 'gdb-get-changed-registers)
4125 (setq gdb-changed-registers nil)
4126 (dolist (register-number
4127 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4128 (push register-number gdb-changed-registers)))
4130 (defun gdb-register-names-handler ()
4131 ;; Don't use gdb-pending-triggers because this handler is called
4132 ;; only once (in gdb-init-1)
4133 (setq gdb-register-names nil)
4134 (dolist (register-name
4135 (bindat-get-field (gdb-json-partial-output) 'register-names))
4136 (push register-name gdb-register-names))
4137 (setq gdb-register-names (reverse gdb-register-names)))
4140 (defun gdb-get-source-file-list ()
4141 "Create list of source files for current GDB session.
4142 If buffers already exist for any of these files, `gud-minor-mode'
4144 (goto-char (point-min))
4145 (while (re-search-forward gdb-source-file-regexp nil t)
4146 (push (match-string 1) gdb-source-file-list))
4147 (dolist (buffer (buffer-list))
4148 (with-current-buffer buffer
4149 (when (member buffer-file-name gdb-source-file-list)
4150 (gdb-init-buffer)))))
4152 (defun gdb-get-main-selected-frame ()
4153 "Trigger for `gdb-frame-handler' which uses main current thread.
4154 Called from `gdb-update'."
4155 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
4157 (gdb-input (gdb-current-context-command "-stack-info-frame")
4159 (gdb-add-pending 'gdb-get-main-selected-frame))))
4161 (defun gdb-frame-handler ()
4162 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4163 overlay arrow in source buffer."
4164 (gdb-delete-pending 'gdb-get-main-selected-frame)
4165 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4167 (setq gdb-selected-frame (bindat-get-field frame 'func))
4168 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4169 (setq gdb-frame-number (bindat-get-field frame 'level))
4170 (setq gdb-frame-address (bindat-get-field frame 'addr))
4171 (let ((line (bindat-get-field frame 'line)))
4172 (setq gdb-selected-line (and line (string-to-number line)))
4173 (when (and gdb-selected-file gdb-selected-line)
4174 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4175 (gud-display-frame)))
4176 (if gud-overlay-arrow-position
4177 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4178 (position (marker-position gud-overlay-arrow-position)))
4180 (with-current-buffer buffer
4181 (setq fringe-indicator-alist
4182 (if (string-equal gdb-frame-number "0")
4184 '((overlay-arrow . hollow-right-triangle))))
4185 (setq gud-overlay-arrow-position (make-marker))
4186 (set-marker gud-overlay-arrow-position position))))))))
4188 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
4190 (defun gdb-get-prompt ()
4191 "Find prompt for GDB session."
4192 (goto-char (point-min))
4193 (setq gdb-prompt-name nil)
4194 (re-search-forward gdb-prompt-name-regexp nil t)
4195 (setq gdb-prompt-name (match-string 1))
4196 ;; Insert first prompt.
4197 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4199 ;;;; Window management
4200 (defun gdb-display-buffer (buf)
4201 "Show buffer BUF, and make that window dedicated."
4202 (let ((window (display-buffer buf)))
4203 (set-window-dedicated-p window t)
4206 ;; (let ((answer (get-buffer-window buf 0)))
4208 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4209 ;; (let ((window (get-lru-window)))
4210 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4212 ;; (let ((largest (get-largest-window)))
4213 ;; (setq answer (split-window largest))
4214 ;; (set-window-buffer answer buf)
4215 ;; (set-window-dedicated-p answer t)
4217 ;; (set-window-buffer window buf)
4221 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4222 "Find window displaying a buffer with the same
4223 `gdb-buffer-type' as BUF and show BUF there. If no such window
4224 exists, just call `gdb-display-buffer' for BUF. If the window
4225 found is already dedicated, split window according to
4226 SPLIT-HORIZONTAL and show BUF in the new window."
4228 (when (not (get-buffer-window buf))
4229 (let* ((buf-type (gdb-buffer-type buf))
4231 (get-window-with-predicate
4234 (gdb-buffer-type (window-buffer w)))
4235 (not (window-dedicated-p w)))))))
4237 (set-window-buffer existing-window buf)
4238 (let ((dedicated-window
4239 (get-window-with-predicate
4242 (gdb-buffer-type (window-buffer w)))))))
4243 (if dedicated-window
4245 (split-window dedicated-window nil split-horizontal) buf)
4246 (gdb-display-buffer buf))))))
4247 (error "Null buffer")))
4249 ;;; Shared keymap initialization:
4251 (let ((menu (make-sparse-keymap "GDB-Windows")))
4252 (define-key gud-menu-map [displays]
4253 `(menu-item "GDB-Windows" ,menu
4254 :visible (eq gud-minor-mode 'gdbmi)))
4255 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4256 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4257 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4258 (define-key menu [disassembly]
4259 '("Disassembly" . gdb-display-disassembly-buffer))
4260 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4261 (define-key menu [inferior]
4262 '("IO" . gdb-display-io-buffer))
4263 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4264 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4265 (define-key menu [breakpoints]
4266 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4268 (let ((menu (make-sparse-keymap "GDB-Frames")))
4269 (define-key gud-menu-map [frames]
4270 `(menu-item "GDB-Frames" ,menu
4271 :visible (eq gud-minor-mode 'gdbmi)))
4272 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4273 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4274 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4275 (define-key menu [disassembly]
4276 '("Disassembly" . gdb-frame-disassembly-buffer))
4277 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4278 (define-key menu [inferior]
4279 '("IO" . gdb-frame-io-buffer))
4280 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4281 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4282 (define-key menu [breakpoints]
4283 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4285 (let ((menu (make-sparse-keymap "GDB-MI")))
4286 (define-key menu [gdb-customize]
4287 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4288 :help "Customize Gdb Graphical Mode options."))
4289 (define-key menu [gdb-many-windows]
4290 '(menu-item "Display Other Windows" gdb-many-windows
4291 :help "Toggle display of locals, stack and breakpoint information"
4292 :button (:toggle . gdb-many-windows)))
4293 (define-key menu [gdb-restore-windows]
4294 '(menu-item "Restore Window Layout" gdb-restore-windows
4295 :help "Restore standard layout for debug session."))
4296 (define-key menu [sep1]
4298 (define-key menu [all-threads]
4299 '(menu-item "GUD controls all threads"
4302 (setq gdb-gud-control-all-threads t))
4303 :help "GUD start/stop commands apply to all threads"
4304 :button (:radio . gdb-gud-control-all-threads)))
4305 (define-key menu [current-thread]
4306 '(menu-item "GUD controls current thread"
4309 (setq gdb-gud-control-all-threads nil))
4310 :help "GUD start/stop commands apply to current thread only"
4311 :button (:radio . (not gdb-gud-control-all-threads))))
4312 (define-key menu [sep2]
4314 (define-key menu [gdb-customize-reasons]
4315 '(menu-item "Customize switching..."
4318 (customize-option 'gdb-switch-reasons))))
4319 (define-key menu [gdb-switch-when-another-stopped]
4320 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4321 gdb-switch-when-another-stopped
4322 "Automatically switch to stopped thread"
4323 "GDB thread switching %s"
4324 "Switch to stopped thread"))
4325 (define-key gud-menu-map [mi]
4326 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4328 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4329 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4330 ;; tool-bar-local-item-from-menu here because it appends new buttons
4331 ;; to toolbar from right to left while we want our A/T throttle to
4332 ;; show up right before Run button.
4333 (define-key-after gud-tool-bar-map [all-threads]
4334 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4335 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4336 :visible (and (eq gud-minor-mode 'gdbmi)
4338 (not gdb-gud-control-all-threads)))
4341 (define-key-after gud-tool-bar-map [current-thread]
4342 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4343 :image (find-image '((:type xpm :file "gud/all.xpm")))
4344 :visible (and (eq gud-minor-mode 'gdbmi)
4346 gdb-gud-control-all-threads))
4349 (defun gdb-frame-gdb-buffer ()
4350 "Display GUD buffer in another frame."
4352 (display-buffer-other-frame gud-comint-buffer))
4354 (defun gdb-display-gdb-buffer ()
4355 "Display GUD buffer."
4357 (pop-to-buffer gud-comint-buffer nil 0))
4359 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4360 "Set buffer of selected window to NAME and dedicate window.
4362 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4363 window is dedicated."
4364 (unless window (setq window (selected-window)))
4365 (when ignore-dedicated
4366 (set-window-dedicated-p window nil))
4367 (set-window-buffer window (get-buffer name))
4368 (set-window-dedicated-p window t))
4370 (defun gdb-setup-windows ()
4371 "Layout the window pattern for option `gdb-many-windows'."
4372 (gdb-get-buffer-create 'gdb-locals-buffer)
4373 (gdb-get-buffer-create 'gdb-stack-buffer)
4374 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4375 (set-window-dedicated-p (selected-window) nil)
4376 (switch-to-buffer gud-comint-buffer)
4377 (delete-other-windows)
4378 (let ((win0 (selected-window))
4379 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4380 (win2 (split-window nil ( / (window-height) 3)))
4381 (win3 (split-window-right)))
4382 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4383 (select-window win2)
4386 (if gud-last-last-frame
4387 (gud-find-file (car gud-last-last-frame))
4389 (gud-find-file gdb-main-file)
4390 ;; Put buffer list in window if we
4391 ;; can't find a source file.
4392 (list-buffers-noselect))))
4393 (setq gdb-source-window (selected-window))
4394 (let ((win4 (split-window-right)))
4395 (gdb-set-window-buffer
4396 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4397 (select-window win1)
4398 (gdb-set-window-buffer (gdb-stack-buffer-name))
4399 (let ((win5 (split-window-right)))
4400 (gdb-set-window-buffer (if gdb-show-threads-by-default
4401 (gdb-threads-buffer-name)
4402 (gdb-breakpoints-buffer-name))
4404 (select-window win0)))
4406 (define-minor-mode gdb-many-windows
4407 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4408 In this case it starts with two windows: one displaying the GUD
4409 buffer and the other with the source file with the main routine
4410 of the debugged program. Non-nil means display the layout shown for
4415 (if (and gud-comint-buffer
4416 (buffer-name gud-comint-buffer))
4418 (gdb-restore-windows))))
4420 (defun gdb-restore-windows ()
4421 "Restore the basic arrangement of windows used by gdb.
4422 This arrangement depends on the value of option `gdb-many-windows'."
4424 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4425 (delete-other-windows)
4426 (if gdb-many-windows
4428 (when (or gud-last-last-frame gdb-show-main)
4429 (let ((win (split-window)))
4432 (if gud-last-last-frame
4433 (gud-find-file (car gud-last-last-frame))
4434 (gud-find-file gdb-main-file)))
4435 (setq gdb-source-window win)))))
4437 ;; Called from `gud-sentinel' in gud.el:
4439 "Exit a debugging session cleanly.
4440 Kills the gdb buffers, and resets variables and the source buffers."
4441 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4442 ;; process. This pty must be deleted explicitly.
4443 (let ((pty (get-process "gdb-inferior")))
4444 (if pty (delete-process pty)))
4445 ;; Find gdb-mi buffers and kill them.
4446 (dolist (buffer (buffer-list))
4447 (unless (eq buffer gud-comint-buffer)
4448 (with-current-buffer buffer
4449 (if (eq gud-minor-mode 'gdbmi)
4450 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4452 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4453 (setq gud-minor-mode nil)
4454 (kill-local-variable 'tool-bar-map)
4455 (kill-local-variable 'gdb-define-alist))))))
4456 (setq gdb-disassembly-position nil)
4457 (setq overlay-arrow-variable-list
4458 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4459 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4460 (setq gdb-stack-position nil)
4461 (setq overlay-arrow-variable-list
4462 (delq 'gdb-stack-position overlay-arrow-variable-list))
4463 (setq gdb-thread-position nil)
4464 (setq overlay-arrow-variable-list
4465 (delq 'gdb-thread-position overlay-arrow-variable-list))
4466 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4467 (setq gud-running nil)
4468 (setq gdb-active-process nil)
4469 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4471 (defun gdb-get-source-file ()
4472 "Find the source file where the program starts and display it with related
4473 buffers, if required."
4474 (goto-char (point-min))
4475 (if (re-search-forward gdb-source-file-regexp nil t)
4476 (setq gdb-main-file (match-string 1)))
4477 (if gdb-many-windows
4479 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4482 (display-buffer (gud-find-file gdb-main-file))))
4483 (gdb-force-mode-line-update
4484 (propertize "ready" 'face font-lock-variable-name-face)))
4487 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4488 "Put string PUTSTRING in front of POS in the current buffer.
4489 PUTSTRING is displayed by putting an overlay into the current buffer with a
4490 `before-string' string that has a `display' property whose value is
4492 (let ((string (make-string 1 ?x))
4493 (buffer (current-buffer)))
4494 (setq putstring (copy-sequence putstring))
4495 (let ((overlay (make-overlay pos pos buffer))
4497 (list (list 'margin 'left-margin) putstring))))
4498 (put-text-property 0 1 'display prop string)
4500 (add-text-properties 0 1 sprops string))
4501 (overlay-put overlay 'put-break t)
4502 (overlay-put overlay 'before-string string))))
4504 ;;from remove-images
4505 (defun gdb-remove-strings (start end &optional buffer)
4506 "Remove strings between START and END in BUFFER.
4507 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4508 BUFFER nil or omitted means use the current buffer."
4510 (setq buffer (current-buffer)))
4511 (dolist (overlay (overlays-in start end))
4512 (when (overlay-get overlay 'put-break)
4513 (delete-overlay overlay))))
4515 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4516 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4517 (start (- (car posns) 1))
4518 (end (+ (cdr posns) 1))
4519 (putstring (if enabled "B" "b"))
4520 (source-window (get-buffer-window (current-buffer) 0)))
4521 (add-text-properties
4522 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4525 (add-text-properties
4526 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4527 (add-text-properties
4528 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4529 (gdb-remove-breakpoint-icons start end)
4530 (if (display-images-p)
4531 (if (>= (or left-fringe-width
4532 (if source-window (car (window-fringes source-window)))
4533 gdb-buffer-fringe-width) 8)
4536 `(left-fringe breakpoint
4539 'breakpoint-disabled))
4541 'gdb-enabled enabled)
4542 (when (< left-margin-width 2)
4543 (save-current-buffer
4544 (setq left-margin-width 2)
4548 left-margin-width right-margin-width))))
4551 (or breakpoint-enabled-icon
4552 (setq breakpoint-enabled-icon
4553 (find-image `((:type xpm :data
4554 ,breakpoint-xpm-data
4555 :ascent 100 :pointer hand)
4557 ,breakpoint-enabled-pbm-data
4558 :ascent 100 :pointer hand)))))
4559 (or breakpoint-disabled-icon
4560 (setq breakpoint-disabled-icon
4561 (find-image `((:type xpm :data
4562 ,breakpoint-xpm-data
4563 :conversion disabled
4564 :ascent 100 :pointer hand)
4566 ,breakpoint-disabled-pbm-data
4567 :ascent 100 :pointer hand))))))
4571 (when (< left-margin-width 2)
4572 (save-current-buffer
4573 (setq left-margin-width 2)
4574 (let ((window (get-buffer-window (current-buffer) 0)))
4577 window left-margin-width right-margin-width)))))
4579 (propertize putstring
4581 'breakpoint-enabled 'breakpoint-disabled))
4584 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4585 (gdb-remove-strings start end)
4586 (if (display-images-p)
4587 (remove-images start end))
4589 (setq left-margin-width 0)
4590 (let ((window (get-buffer-window (current-buffer) 0)))
4593 window left-margin-width right-margin-width)))))
4596 ;;; Functions for inline completion.
4598 (defvar gud-gdb-fetch-lines-in-progress)
4599 (defvar gud-gdb-fetch-lines-string)
4600 (defvar gud-gdb-fetch-lines-break)
4601 (defvar gud-gdb-fetched-lines)
4603 (defun gud-gdbmi-completions (context command)
4604 "Completion table for GDB/MI commands.
4605 COMMAND is the prefix for which we seek completion.
4606 CONTEXT is the text before COMMAND on the line."
4607 (let ((gud-gdb-fetch-lines-in-progress t)
4608 (gud-gdb-fetch-lines-string nil)
4609 (gud-gdb-fetch-lines-break (length context))
4610 (gud-gdb-fetched-lines nil)
4611 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4612 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4613 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4614 (gdb-input (concat "complete " context command)
4615 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4616 (while gud-gdb-fetch-lines-in-progress
4617 (accept-process-output (get-buffer-process gud-comint-buffer))))
4618 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4620 (defun gud-gdbmi-fetch-lines-filter (string)
4621 "Custom filter function for `gud-gdbmi-completions'."
4622 (setq string (concat gud-gdb-fetch-lines-string
4623 (gud-gdbmi-marker-filter string)))
4624 (while (string-match "\n" string)
4625 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4626 gud-gdb-fetched-lines)
4627 (setq string (substring string (match-end 0))))
4632 ;;; gdb-mi.el ends here