1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007-2012 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. Only used for files that
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 gdb-buffer-type nil
231 "One of the symbols bound in `gdb-buffer-rules'.")
232 (make-variable-buffer-local 'gdb-buffer-type
)
234 (defvar gdb-output-sink
'nil
235 "The disposition of the output of the current gdb command.
236 Possible values are these symbols:
238 `user' -- gdb output should be copied to the GUD buffer
241 `emacs' -- output should be collected in the partial-output-buffer
242 for subsequent processing by a command. This is the
243 disposition of output generated by commands that
244 gdb mode sends to gdb on its own behalf.")
246 ;; Pending triggers prevent congestion: Emacs won't send two similar
247 ;; consecutive requests.
249 (defvar gdb-pending-triggers
'()
250 "A list of trigger functions which have not yet been handled.
252 Elements are either function names or pairs (buffer . function)")
254 (defmacro gdb-add-pending
(item)
255 `(push ,item gdb-pending-triggers
))
256 (defmacro gdb-pending-p
(item)
257 `(member ,item gdb-pending-triggers
))
258 (defmacro gdb-delete-pending
(item)
259 `(setq gdb-pending-triggers
260 (delete ,item gdb-pending-triggers
)))
262 (defmacro gdb-wait-for-pending
(&rest body
)
263 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
265 This function checks `gdb-pending-triggers' value every
266 `gdb-wait-for-pending' seconds."
270 (if (not gdb-pending-triggers
)
272 (gdb-wait-for-pending ,@body
)))))
276 (defmacro gdb-add-subscriber
(publisher subscriber
)
277 "Register new PUBLISHER's SUBSCRIBER.
279 SUBSCRIBER must be a pair, where cdr is a function of one
280 argument (see `gdb-emit-signal')."
281 `(add-to-list ',publisher
,subscriber t
))
283 (defmacro gdb-delete-subscriber
(publisher subscriber
)
284 "Unregister SUBSCRIBER from PUBLISHER."
285 `(setq ,publisher
(delete ,subscriber
288 (defun gdb-get-subscribers (publisher)
291 (defun gdb-emit-signal (publisher &optional signal
)
292 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
293 (dolist (subscriber (gdb-get-subscribers publisher
))
294 (funcall (cdr subscriber
) signal
)))
296 (defvar gdb-buf-publisher
'()
297 "Used to invalidate GDB buffers by emitting a signal in
300 Must be a list of pairs with cars being buffers and cdr's being
301 valid signal handlers.")
304 "GDB graphical interface"
306 :link
'(info-link "(emacs)GDB Graphical Interface")
309 (defgroup gdb-non-stop nil
310 "GDB non-stop debugging settings"
314 (defgroup gdb-buffers nil
319 (defcustom gdb-debug-log-max
128
320 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
322 :type
'(choice (integer :tag
"Number of elements")
323 (const :tag
"Unlimited" nil
))
326 (defcustom gdb-non-stop-setting t
327 "When in non-stop mode, stopped threads can be examined while
328 other threads continue to execute.
330 GDB session needs to be restarted for this setting to take
336 ;; TODO Some commands can't be called with --all (give a notice about
337 ;; it in setting doc)
338 (defcustom gdb-gud-control-all-threads t
339 "When enabled, GUD execution commands affect all threads when
340 in non-stop mode. Otherwise, only current thread is affected."
345 (defcustom gdb-switch-reasons t
346 "List of stop reasons which cause Emacs to switch to the thread
347 which caused the stop. When t, switch to stopped thread no matter
348 what the reason was. When nil, never switch to stopped thread
351 This setting is used in non-stop mode only. In all-stop mode,
352 Emacs always switches to the thread which caused the stop."
353 ;; exited, exited-normally and exited-signaled are not
354 ;; thread-specific stop reasons and therefore are not included in
357 (const :tag
"All reasons" t
)
358 (set :tag
"Selection of reasons..."
359 (const :tag
"A breakpoint was reached." "breakpoint-hit")
360 (const :tag
"A watchpoint was triggered." "watchpoint-trigger")
361 (const :tag
"A read watchpoint was triggered."
362 "read-watchpoint-trigger")
363 (const :tag
"An access watchpoint was triggered."
364 "access-watchpoint-trigger")
365 (const :tag
"Function finished execution." "function-finished")
366 (const :tag
"Location reached." "location-reached")
367 (const :tag
"Watchpoint has gone out of scope"
369 (const :tag
"End of stepping range reached."
370 "end-stepping-range")
371 (const :tag
"Signal received (like interruption)."
373 (const :tag
"None" nil
))
376 :link
'(info-link "(gdb)GDB/MI Async Records"))
378 (defcustom gdb-stopped-functions nil
379 "List of functions called whenever GDB stops.
381 Each function takes one argument, a parsed MI response, which
382 contains fields of corresponding MI *stopped async record:
384 ((stopped-threads . \"all\")
386 (frame (line . \"38\")
387 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
388 (file . \"server.c\")
389 (args ((value . \"0x804b038\")
392 (addr . \"0x0804869e\"))
393 (reason . \"end-stepping-range\"))
395 Note that \"reason\" is only present in non-stop debugging mode.
397 `bindat-get-field' may be used to access the fields of response.
399 Each function is called after the new current thread was selected
400 and GDB buffers were updated in `gdb-stopped'."
401 :type
'(repeat function
)
404 :link
'(info-link "(gdb)GDB/MI Async Records"))
406 (defcustom gdb-switch-when-another-stopped t
407 "When nil, Emacs won't switch to stopped thread if some other
408 stopped thread is already selected."
413 (defcustom gdb-stack-buffer-locations t
414 "Show file information or library names in stack buffers."
419 (defcustom gdb-stack-buffer-addresses nil
420 "Show frame addresses in stack buffers."
425 (defcustom gdb-thread-buffer-verbose-names t
426 "Show long thread names in threads buffer."
431 (defcustom gdb-thread-buffer-arguments t
432 "Show function arguments in threads buffer."
437 (defcustom gdb-thread-buffer-locations t
438 "Show file information or library names in threads buffer."
443 (defcustom gdb-thread-buffer-addresses nil
444 "Show addresses for thread frames in threads buffer."
449 (defcustom gdb-show-threads-by-default nil
450 "Show threads list buffer instead of breakpoints list by
456 (defvar gdb-debug-log nil
457 "List of commands sent to and replies received from GDB.
458 Most recent commands are listed first. This list stores only the last
459 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
462 (define-minor-mode gdb-enable-debug
463 "Toggle logging of transaction between Emacs and Gdb.
464 The log is stored in `gdb-debug-log' as an alist with elements
465 whose cons is send, send-item or recv and whose cdr is the string
466 being transferred. This list may grow up to a size of
467 `gdb-debug-log-max' after which the oldest element (at the end of
468 the list) is deleted every time a new one is added (at the front)."
473 (defcustom gdb-cpp-define-alist-program
"gcc -E -dM -"
474 "Shell command for generating a list of defined macros in a source file.
475 This list is used to display the #define directive associated
476 with an identifier as a tooltip. It works in a debug session with
477 GDB, when `gud-tooltip-mode' is t.
479 Set `gdb-cpp-define-alist-flags' for any include paths or
485 (defcustom gdb-cpp-define-alist-flags
""
486 "Preprocessor flags for `gdb-cpp-define-alist-program'."
491 (defcustom gdb-create-source-file-list t
492 "Non-nil means create a list of files from which the executable was built.
493 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
494 line for a long time when starting, possibly because your executable was
495 built from a large number of files. This allows quicker initialization
496 but means that these files are not automatically enabled for debugging,
497 e.g., you won't be able to click in the fringe to set a breakpoint until
498 execution has already stopped there."
503 (defcustom gdb-show-main nil
504 "Non-nil means display source file containing the main routine at startup.
505 Also display the main routine in the disassembly buffer if present."
510 (defun gdb-force-mode-line-update (status)
511 (let ((buffer gud-comint-buffer
))
512 (if (and buffer
(buffer-name buffer
))
513 (with-current-buffer buffer
514 (setq mode-line-process
516 (process-status (get-buffer-process buffer
)) status
))
517 ;; Force mode line redisplay soon.
518 (force-mode-line-update)))))
520 ;; These two are used for menu and toolbar
521 (defun gdb-control-all-threads ()
522 "Switch to non-stop/A mode."
524 (setq gdb-gud-control-all-threads t
)
525 ;; Actually forcing the tool-bar to update.
526 (force-mode-line-update)
527 (message "Now in non-stop/A mode."))
529 (defun gdb-control-current-thread ()
530 "Switch to non-stop/T mode."
532 (setq gdb-gud-control-all-threads nil
)
533 ;; Actually forcing the tool-bar to update.
534 (force-mode-line-update)
535 (message "Now in non-stop/T mode."))
537 (defun gdb-find-watch-expression ()
538 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list
))
539 (varnum (car var
)) expr
)
540 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum
)
541 (let ((var1 (assoc (match-string 1 varnum
) gdb-var-list
)) var2 varnumlet
542 (component-list (split-string (match-string 2 varnum
) "\\." t
)))
543 (setq expr
(nth 1 var1
))
544 (setq varnumlet
(car var1
))
545 (dolist (component component-list
)
546 (setq var2
(assoc varnumlet gdb-var-list
))
547 (setq expr
(concat expr
548 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2
))
549 (concat "[" component
"]")
550 (concat "." component
))))
551 (setq varnumlet
(concat varnumlet
"." component
)))
554 ;; noall is used for commands which don't take --all, but only
556 (defun gdb-gud-context-command (command &optional noall
)
557 "When `gdb-non-stop' is t, add --thread option to COMMAND if
558 `gdb-gud-control-all-threads' is nil and --all option otherwise.
559 If NOALL is t, always add --thread option no matter what
560 `gdb-gud-control-all-threads' value is.
562 When `gdb-non-stop' is nil, return COMMAND unchanged."
564 (if (and gdb-gud-control-all-threads
566 gdb-supports-non-stop
)
567 (concat command
" --all ")
568 (gdb-current-context-command command
))
571 (defmacro gdb-gud-context-call
(cmd1 &optional cmd2 noall noarg
)
572 "`gud-call' wrapper which adds --thread/--all options between
573 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
575 NOARG must be t when this macro is used outside `gud-def'"
577 (concat (gdb-gud-context-command ,cmd1
,noall
) " " ,cmd2
)
578 ,(when (not noarg
) 'arg
)))
580 (defun gdb--check-interpreter (proc string
)
581 (unless (zerop (length string
))
582 (let ((filter (process-get proc
'gud-normal-filter
)))
583 (set-process-filter proc filter
)
584 (unless (memq (aref string
0) '(?^ ?~ ?
@ ?
& ?
* ?
=))
585 ;; Apparently we're not running with -i=mi.
586 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
588 (setq string
(concat (propertize msg
'font-lock-face
'error
)
590 ;; Use the old gud-gbd filter, not because it works, but because it
591 ;; will properly display GDB's answers rather than hanging waiting for
592 ;; answers that aren't coming.
593 (set (make-local-variable 'gud-marker-filter
) #'gud-gdb-marker-filter
))
594 (funcall filter proc string
))))
596 (defvar gdb-control-level
0)
599 (defun gdb (command-line)
600 "Run gdb on program FILE in buffer *gud-FILE*.
601 The directory containing FILE becomes the initial working directory
602 and source-file directory for your debugger.
604 COMMAND-LINE is the shell command for starting the gdb session.
605 It should be a string consisting of the name of the gdb
606 executable followed by command-line options. The command-line
607 options should include \"-i=mi\" to use gdb's MI text interface.
608 Note that the old \"--annotate\" option is no longer supported.
610 If `gdb-many-windows' is nil (the default value) then gdb just
611 pops up the GUD buffer unless `gdb-show-main' is t. In this case
612 it starts with two windows: one displaying the GUD buffer and the
613 other with the source file with the main routine of the inferior.
615 If `gdb-many-windows' is t, regardless of the value of
616 `gdb-show-main', the layout below will appear. Keybindings are
617 shown in some of the buffers.
619 Watch expressions appear in the speedbar/slowbar.
621 The following commands help control operation :
623 `gdb-many-windows' - Toggle the number of windows gdb uses.
624 `gdb-restore-windows' - To restore the window layout.
626 See Info node `(emacs)GDB Graphical Interface' for a more
627 detailed description of this mode.
630 +----------------------------------------------------------------------+
632 +-----------------------------------+----------------------------------+
633 | GUD buffer (I/O of GDB) | Locals buffer |
637 +-----------------------------------+----------------------------------+
638 | Source buffer | I/O buffer (of debugged program) |
646 +-----------------------------------+----------------------------------+
647 | Stack buffer | Breakpoints buffer |
648 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
649 | | RET gdb-goto-breakpoint |
650 | | D gdb-delete-breakpoint |
651 +-----------------------------------+----------------------------------+"
653 (interactive (list (gud-query-cmdline 'gdb
)))
655 (when (and gud-comint-buffer
656 (buffer-name gud-comint-buffer
)
657 (get-buffer-process gud-comint-buffer
)
658 (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
)))
659 (gdb-restore-windows)
661 "Multiple debugging requires restarting in text command mode"))
663 (gud-common-init command-line nil
'gud-gdbmi-marker-filter
)
665 ;; Setup a temporary process filter to warn when GDB was not started
667 (let ((proc (get-buffer-process gud-comint-buffer
)))
668 (process-put proc
'gud-normal-filter
(process-filter proc
))
669 (set-process-filter proc
#'gdb--check-interpreter
))
671 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
672 (set (make-local-variable 'gdb-control-level
) 0)
673 (setq comint-input-sender
'gdb-send
)
674 (when (ring-empty-p comint-input-ring
) ; cf shell-mode
675 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
676 (if (eq system-type
'ms-dos
)
679 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
680 (hsize (getenv "HISTSIZE")))
681 (dolist (file (append '("~/.gdbinit")
682 (unless (string-equal (expand-file-name ".")
683 (expand-file-name "~"))
685 (if (file-readable-p (setq file
(expand-file-name file
)))
687 (insert-file-contents file
)
688 ;; TODO? check for "set history save\\( *on\\)?" and do
689 ;; not use history otherwise?
690 (while (re-search-forward
691 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t
)
692 (cond ((string-equal (match-string 1) "filename")
693 (setq hfile
(expand-file-name
695 (file-name-directory file
))))
696 ((string-equal (match-string 1) "size")
697 (setq hsize
(match-string 2))))))))
699 (integerp (setq hsize
(string-to-number hsize
)))
701 (set (make-local-variable 'comint-input-ring-size
) hsize
))
703 (set (make-local-variable 'comint-input-ring-file-name
) hfile
))
704 (comint-read-input-ring t
)))
705 (gud-def gud-tbreak
"tbreak %f:%l" "\C-t"
706 "Set temporary breakpoint at current line.")
708 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
709 "\C-j" "Set execution address to current line.")
711 (gud-def gud-up
"up %p" "<" "Up N stack frames (numeric arg).")
712 (gud-def gud-down
"down %p" ">" "Down N stack frames (numeric arg).")
713 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
714 (gud-def gud-pstar
"print* %e" nil
715 "Evaluate C dereferenced pointer expression at point.")
717 (gud-def gud-step
(gdb-gud-context-call "-exec-step" "%p" t
)
719 "Step one source line with display.")
720 (gud-def gud-stepi
(gdb-gud-context-call "-exec-step-instruction" "%p" t
)
722 "Step one instruction with display.")
723 (gud-def gud-next
(gdb-gud-context-call "-exec-next" "%p" t
)
725 "Step one line (skip functions).")
726 (gud-def gud-nexti
(gdb-gud-context-call "-exec-next-instruction" "%p" t
)
728 "Step one instruction (skip functions).")
729 (gud-def gud-cont
(gdb-gud-context-call "-exec-continue")
731 "Continue with display.")
732 (gud-def gud-finish
(gdb-gud-context-call "-exec-finish" nil t
)
734 "Finish executing current function.")
735 (gud-def gud-run
"-exec-run"
739 (gud-def gud-break
(if (not (string-match "Disassembly" mode-name
))
740 (gud-call "break %f:%l" arg
)
744 (gud-call "break *%a" arg
)))
745 "\C-b" "Set breakpoint at current line or address.")
747 (gud-def gud-remove
(if (not (string-match "Disassembly" mode-name
))
748 (gud-call "clear %f:%l" arg
)
752 (gud-call "clear *%a" arg
)))
753 "\C-d" "Remove breakpoint at current line or address.")
755 ;; -exec-until doesn't support --all yet
756 (gud-def gud-until
(if (not (string-match "Disassembly" mode-name
))
757 (gud-call "-exec-until %f:%l" arg
)
761 (gud-call "-exec-until *%a" arg
)))
762 "\C-u" "Continue to current line or address.")
763 ;; TODO Why arg here?
765 gud-go
(gud-call (if gdb-active-process
766 (gdb-gud-context-command "-exec-continue")
768 nil
"Start or continue execution.")
770 ;; For debugging Emacs only.
774 "pp " (if (eq (buffer-local-value
775 'major-mode
(window-buffer)) 'speedbar-mode
)
776 (gdb-find-watch-expression) "%e")) arg
)
777 nil
"Print the Emacs s-expression.")
779 (define-key gud-minor-mode-map
[left-margin mouse-1
]
780 'gdb-mouse-set-clear-breakpoint
)
781 (define-key gud-minor-mode-map
[left-fringe mouse-1
]
782 'gdb-mouse-set-clear-breakpoint
)
783 (define-key gud-minor-mode-map
[left-margin C-mouse-1
]
784 'gdb-mouse-toggle-breakpoint-margin
)
785 (define-key gud-minor-mode-map
[left-fringe C-mouse-1
]
786 'gdb-mouse-toggle-breakpoint-fringe
)
788 (define-key gud-minor-mode-map
[left-margin drag-mouse-1
]
790 (define-key gud-minor-mode-map
[left-fringe drag-mouse-1
]
792 (define-key gud-minor-mode-map
[left-margin mouse-3
]
794 (define-key gud-minor-mode-map
[left-fringe mouse-3
]
797 (define-key gud-minor-mode-map
[left-margin C-drag-mouse-1
]
799 (define-key gud-minor-mode-map
[left-fringe C-drag-mouse-1
]
801 (define-key gud-minor-mode-map
[left-fringe C-mouse-3
]
803 (define-key gud-minor-mode-map
[left-margin C-mouse-3
]
806 (set (make-local-variable 'gud-gdb-completion-function
)
807 'gud-gdbmi-completions
)
809 (add-hook 'completion-at-point-functions
#'gud-gdb-completion-at-point
811 (local-set-key "\C-i" 'completion-at-point
)
813 (local-set-key [remap comint-delchar-or-maybe-eof
] 'gdb-delchar-or-quit
)
815 (setq gdb-first-prompt t
)
816 (setq gud-running nil
)
820 (run-hooks 'gdb-mode-hook
))
824 (setq gdb-selected-frame nil
826 gdb-thread-number nil
828 gdb-pending-triggers nil
829 gdb-output-sink
'user
830 gdb-location-alist nil
831 gdb-source-file-list nil
834 gdb-handler-alist
'()
835 gdb-handler-number nil
837 gdb-first-done-or-error t
838 gdb-buffer-fringe-width
(car (window-fringes))
840 gdb-source-window nil
841 gdb-inferior-status nil
843 gdb-buf-publisher
'()
845 gdb-breakpoints-list
'()
846 gdb-register-names
'()
847 gdb-non-stop gdb-non-stop-setting
)
849 (setq gdb-buffer-type
'gdbmi
)
851 (gdb-force-mode-line-update
852 (propertize "initializing..." 'face font-lock-variable-name-face
))
854 (gdb-get-buffer-create 'gdb-inferior-io
)
855 (gdb-clear-inferior-io)
856 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
858 (when (eq system-type
'windows-nt
)
859 ;; Don't create a separate console window for the debuggee.
860 (gdb-input "-gdb-set new-console off" 'ignore
)
861 ;; Force GDB to behave as if its input and output stream were
862 ;; connected to a TTY device (since on Windows we use pipes for
863 ;; communicating with GDB).
864 (gdb-input "-gdb-set interactive-mode on" 'ignore
))
865 (gdb-input "-gdb-set height 0" 'ignore
)
868 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler
))
870 (gdb-input "-enable-pretty-printing" 'ignore
)
872 ;; Find source file and compilation directory here.
873 (if gdb-create-source-file-list
874 ;; Needs GDB 6.2 onwards.
875 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list
))
876 ;; Needs GDB 6.0 onwards.
877 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file
)
878 (gdb-input "-gdb-show prompt" 'gdb-get-prompt
))
880 (defun gdb-non-stop-handler ()
881 (goto-char (point-min))
882 (if (re-search-forward "No symbol" nil t
)
885 "This version of GDB doesn't support non-stop mode. Turning it off.")
886 (setq gdb-non-stop nil
)
887 (setq gdb-supports-non-stop nil
))
888 (setq gdb-supports-non-stop t
)
889 (gdb-input "-gdb-set target-async 1" 'ignore
)
890 (gdb-input "-list-target-features" 'gdb-check-target-async
)))
892 (defun gdb-check-target-async ()
893 (goto-char (point-min))
894 (unless (re-search-forward "async" nil t
)
896 "Target doesn't support non-stop mode. Turning it off.")
897 (setq gdb-non-stop nil
)
898 (gdb-input "-gdb-set non-stop 0" 'ignore
)))
900 (defun gdb-delchar-or-quit (arg)
901 "Delete ARG characters or send a quit command to GDB.
902 Send a quit only if point is at the end of the buffer, there is
903 no input, and GDB is waiting for input."
905 (unless (and (eq (current-buffer) gud-comint-buffer
)
906 (eq gud-minor-mode
'gdbmi
))
907 (error "Not in a GDB-MI buffer"))
908 (let ((proc (get-buffer-process gud-comint-buffer
)))
909 (if (and (eobp) proc
(process-live-p proc
)
911 (= (point) (marker-position (process-mark proc
))))
912 ;; Sending an EOF does not work with GDB-MI; submit an
913 ;; explicit quit command.
916 (comint-send-input t t
))
919 (defvar gdb-define-alist nil
"Alist of #define directives for GUD tooltips.")
921 (defun gdb-create-define-alist ()
922 "Create an alist of #define directives for GUD tooltips."
923 (let* ((file (buffer-file-name))
925 (with-output-to-string
926 (with-current-buffer standard-output
929 ;; call-process doesn't work with remote file names.
930 (not (file-remote-p default-directory
))
931 (call-process shell-file-name file
932 (list t nil
) nil
"-c"
933 (concat gdb-cpp-define-alist-program
" "
934 gdb-cpp-define-alist-flags
))))))
935 (define-list (split-string output
"\n" t
))
937 (setq gdb-define-alist nil
)
938 (dolist (define define-list
)
939 (setq name
(nth 1 (split-string define
"[( ]")))
940 (push (cons name define
) gdb-define-alist
))))
942 (declare-function tooltip-show
"tooltip" (text &optional use-echo-area
))
944 (defun gdb-tooltip-print (expr)
945 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
946 (goto-char (point-min))
947 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t
)
949 (concat expr
" = " (read (match-string 1)))
950 (or gud-tooltip-echo-area
951 (not (display-graphic-p)))))))
953 ;; If expr is a macro for a function don't print because of possible dangerous
954 ;; side-effects. Also printing a function within a tooltip generates an
955 ;; unexpected starting annotation (phase error).
956 (defun gdb-tooltip-print-1 (expr)
957 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
958 (goto-char (point-min))
959 (if (search-forward "expands to: " nil t
)
960 (unless (looking-at "\\S-+.*(.*).*")
961 (gdb-input (concat "-data-evaluate-expression " expr
)
962 `(lambda () (gdb-tooltip-print ,expr
)))))))
964 (defun gdb-init-buffer ()
965 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
966 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
967 (when gud-tooltip-mode
968 (make-local-variable 'gdb-define-alist
)
969 (gdb-create-define-alist)
970 (add-hook 'after-save-hook
'gdb-create-define-alist nil t
)))
972 (defmacro gdb--if-arrow
(arrow-position start-posn end-posn
&rest body
)
974 (let ((buffer (make-symbol "buffer")))
976 (let ((,buffer
(marker-buffer ,arrow-position
)))
977 (if (equal ,buffer
(window-buffer (posn-window ,end-posn
)))
978 (with-current-buffer ,buffer
979 (when (or (equal ,start-posn
,end-posn
)
980 (equal (posn-point ,start-posn
)
981 (marker-position ,arrow-position
)))
984 (defun gdb-mouse-until (event)
985 "Continue running until a source line past the current line.
986 The destination source line can be selected either by clicking
987 with mouse-3 on the fringe/margin or dragging the arrow
988 with mouse-1 (default bindings)."
990 (let ((start (event-start event
))
991 (end (event-end event
)))
992 (gdb--if-arrow gud-overlay-arrow-position start end
993 (let ((line (line-number-at-pos (posn-point end
))))
994 (gud-call (concat "until " (number-to-string line
)))))
995 (gdb--if-arrow gdb-disassembly-position start end
997 (goto-char (point-min))
998 (forward-line (1- (line-number-at-pos (posn-point end
))))
1000 (gud-call (concat "until *%a"))))))
1002 (defun gdb-mouse-jump (event)
1003 "Set execution address/line.
1004 The destination source line can be selected either by clicking with C-mouse-3
1005 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1006 Unlike `gdb-mouse-until' the destination address can be before the current
1007 line, and no execution takes place."
1009 (let ((start (event-start event
))
1010 (end (event-end event
)))
1011 (gdb--if-arrow gud-overlay-arrow-position start end
1012 (let ((line (line-number-at-pos (posn-point end
))))
1013 (gud-call (concat "tbreak " (number-to-string line
)))
1014 (gud-call (concat "jump " (number-to-string line
)))))
1015 (gdb--if-arrow gdb-disassembly-position start end
1017 (goto-char (point-min))
1018 (forward-line (1- (line-number-at-pos (posn-point end
))))
1020 (gud-call (concat "tbreak *%a"))
1021 (gud-call (concat "jump *%a"))))))
1023 (defcustom gdb-show-changed-values t
1024 "If non-nil change the face of out of scope variables and changed values.
1025 Out of scope variables are suppressed with `shadow' face.
1026 Changed values are highlighted with the face `font-lock-warning-face'."
1031 (defcustom gdb-max-children
40
1032 "Maximum number of children before expansion requires confirmation."
1037 (defcustom gdb-delete-out-of-scope t
1038 "If non-nil delete watch expressions automatically when they go out of scope."
1043 (define-minor-mode gdb-speedbar-auto-raise
1044 "Minor mode to automatically raise the speedbar for watch expressions.
1045 With prefix argument ARG, automatically raise speedbar if ARG is
1046 positive, otherwise don't automatically raise it."
1051 (defcustom gdb-use-colon-colon-notation nil
1052 "If non-nil use FUN::VAR format to display variables in the speedbar."
1057 (define-key gud-minor-mode-map
"\C-c\C-w" 'gud-watch
)
1058 (define-key global-map
(vconcat gud-key-prefix
"\C-w") 'gud-watch
)
1060 (declare-function tooltip-identifier-from-point
"tooltip" (point))
1062 (defun gud-watch (&optional arg event
)
1063 "Watch expression at point.
1064 With arg, enter name of variable to be watched in the minibuffer."
1065 (interactive (list current-prefix-arg last-input-event
))
1066 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer
)))
1067 (if (eq minor-mode
'gdbmi
)
1069 (if event
(posn-set-point (event-end event
)))
1071 (save-selected-window
1074 (completing-read "Name of variable: "
1075 'gud-gdb-complete-command
)
1076 (if (and transient-mark-mode mark-active
)
1077 (buffer-substring (region-beginning) (region-end))
1078 (concat (if (derived-mode-p 'gdb-registers-mode
) "$")
1079 (tooltip-identifier-from-point (point)))))))
1080 (set-text-properties 0 (length expr
) nil expr
)
1081 (gdb-input (concat "-var-create - * " expr
"")
1082 `(lambda () (gdb-var-create-handler ,expr
))))))
1083 (message "gud-watch is a no-op in this mode."))))
1085 (defun gdb-var-create-handler (expr)
1086 (let* ((result (gdb-json-partial-output)))
1087 (if (not (bindat-get-field result
'msg
))
1089 (list (bindat-get-field result
'name
)
1090 (if (and (string-equal gdb-current-language
"c")
1091 gdb-use-colon-colon-notation gdb-selected-frame
)
1092 (setq expr
(concat gdb-selected-frame
"::" expr
))
1094 (bindat-get-field result
'numchild
)
1095 (bindat-get-field result
'type
)
1096 (bindat-get-field result
'value
)
1098 (bindat-get-field result
'has_more
)
1099 gdb-frame-address
)))
1100 (push var gdb-var-list
)
1102 (unless (string-equal
1103 speedbar-initial-expansion-list-name
"GUD")
1104 (speedbar-change-initial-expansion-list "GUD")))
1105 (message-box "No symbol \"%s\" in current context." expr
))))
1107 (defun gdb-speedbar-update ()
1108 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
)
1109 (not (gdb-pending-p 'gdb-speedbar-timer
)))
1110 ;; Dummy command to update speedbar even when idle.
1111 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn
)
1112 ;; Keep gdb-pending-triggers non-nil till end.
1113 (gdb-add-pending 'gdb-speedbar-timer
)))
1115 (defun gdb-speedbar-timer-fn ()
1116 (if gdb-speedbar-auto-raise
1117 (raise-frame speedbar-frame
))
1118 (gdb-delete-pending 'gdb-speedbar-timer
)
1119 (speedbar-timer-fn))
1121 (defun gdb-var-evaluate-expression-handler (varnum changed
)
1122 (goto-char (point-min))
1123 (re-search-forward ".*value=\\(\".*\"\\)" nil t
)
1124 (let ((var (assoc varnum gdb-var-list
)))
1126 (if changed
(setcar (nthcdr 5 var
) 'changed
))
1127 (setcar (nthcdr 4 var
) (read (match-string 1)))))
1128 (gdb-speedbar-update))
1130 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1131 (defun gdb-var-list-children (varnum)
1132 (gdb-input (concat "-var-update " varnum
) 'ignore
)
1133 (gdb-input (concat "-var-list-children --all-values " varnum
)
1134 `(lambda () (gdb-var-list-children-handler ,varnum
))))
1136 (defun gdb-var-list-children-handler (varnum)
1137 (let* ((var-list nil
)
1138 (output (bindat-get-field (gdb-json-partial-output "child")))
1139 (children (bindat-get-field output
'children
)))
1140 (catch 'child-already-watched
1141 (dolist (var gdb-var-list
)
1142 (if (string-equal varnum
(car var
))
1144 ;; With dynamic varobjs numchild may have increased.
1145 (setcar (nthcdr 2 var
) (bindat-get-field output
'numchild
))
1147 (dolist (child children
)
1148 (let ((varchild (list (bindat-get-field child
'name
)
1149 (bindat-get-field child
'exp
)
1150 (bindat-get-field child
'numchild
)
1151 (bindat-get-field child
'type
)
1152 (bindat-get-field child
'value
)
1154 (bindat-get-field child
'has_more
))))
1155 (if (assoc (car varchild
) gdb-var-list
)
1156 (throw 'child-already-watched nil
))
1157 (push varchild var-list
))))
1158 (push var var-list
)))
1159 (setq gdb-var-list
(nreverse var-list
))))
1160 (gdb-speedbar-update))
1162 (defun gdb-var-set-format (format)
1163 "Set the output format for a variable displayed in the speedbar."
1164 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1166 (gdb-input (concat "-var-set-format " varnum
" " format
) 'ignore
)
1169 (defun gdb-var-delete-1 (var varnum
)
1170 (gdb-input (concat "-var-delete " varnum
) 'ignore
)
1171 (setq gdb-var-list
(delq var gdb-var-list
))
1172 (dolist (varchild gdb-var-list
)
1173 (if (string-match (concat (car var
) "\\.") (car varchild
))
1174 (setq gdb-var-list
(delq varchild gdb-var-list
)))))
1176 (defun gdb-var-delete ()
1177 "Delete watch expression at point from the speedbar."
1179 (let ((text (speedbar-line-text)))
1180 (string-match "\\(\\S-+\\)" text
)
1181 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1183 (if (string-match "\\." (car var
))
1184 (message-box "Can only delete a root expression")
1185 (gdb-var-delete-1 var varnum
)))))
1187 (defun gdb-var-delete-children (varnum)
1188 "Delete children of variable object at point from the speedbar."
1189 (gdb-input (concat "-var-delete -c " varnum
) 'ignore
))
1191 (defun gdb-edit-value (_text _token _indent
)
1192 "Assign a value to a variable displayed in the speedbar."
1193 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1195 (value (read-string "New value: ")))
1196 (gdb-input (concat "-var-assign " varnum
" " value
)
1197 `(lambda () (gdb-edit-value-handler ,value
)))))
1199 (defconst gdb-error-regexp
"\\^error,msg=\\(\".+\"\\)")
1201 (defun gdb-edit-value-handler (value)
1202 (goto-char (point-min))
1203 (if (re-search-forward gdb-error-regexp nil t
)
1204 (message-box "Invalid number or expression (%s)" value
)))
1206 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1207 (defun gdb-var-update ()
1208 (if (not (gdb-pending-p 'gdb-var-update
))
1209 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler
))
1210 (gdb-add-pending 'gdb-var-update
))
1212 (defun gdb-var-update-handler ()
1213 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist
)))
1214 (dolist (var gdb-var-list
)
1215 (setcar (nthcdr 5 var
) nil
))
1216 (let ((temp-var-list gdb-var-list
))
1217 (dolist (change changelist
)
1218 (let* ((varnum (bindat-get-field change
'name
))
1219 (var (assoc varnum gdb-var-list
))
1220 (new-num (bindat-get-field change
'new_num_children
)))
1222 (let ((scope (bindat-get-field change
'in_scope
))
1223 (has-more (bindat-get-field change
'has_more
)))
1224 (cond ((string-equal scope
"false")
1225 (if gdb-delete-out-of-scope
1226 (gdb-var-delete-1 var varnum
)
1227 (setcar (nthcdr 5 var
) 'out-of-scope
)))
1228 ((string-equal scope
"true")
1229 (setcar (nthcdr 6 var
) has-more
)
1230 (when (and (or (not has-more
)
1231 (string-equal has-more
"0"))
1233 (string-equal (nth 2 var
) "0"))
1234 (setcar (nthcdr 4 var
)
1235 (bindat-get-field change
'value
))
1236 (setcar (nthcdr 5 var
) 'changed
)))
1237 ((string-equal scope
"invalid")
1238 (gdb-var-delete-1 var varnum
)))))
1239 (let ((var-list nil
) var1
1240 (children (bindat-get-field change
'new_children
)))
1242 (setq var1
(pop temp-var-list
))
1244 (if (string-equal varnum
(car var1
))
1245 (let ((new (string-to-number new-num
))
1246 (previous (string-to-number (nth 2 var1
))))
1247 (setcar (nthcdr 2 var1
) new-num
)
1248 (push var1 var-list
)
1251 ;; Add new children to list.
1252 (dotimes (dummy previous
)
1253 (push (pop temp-var-list
) var-list
))
1254 (dolist (child children
)
1256 (list (bindat-get-field child
'name
)
1257 (bindat-get-field child
'exp
)
1258 (bindat-get-field child
'numchild
)
1259 (bindat-get-field child
'type
)
1260 (bindat-get-field child
'value
)
1262 (bindat-get-field child
'has_more
))))
1263 (push varchild var-list
))))
1264 ;; Remove deleted children from list.
1266 (dotimes (dummy new
)
1267 (push (pop temp-var-list
) var-list
))
1268 (dotimes (dummy (- previous new
))
1269 (pop temp-var-list
)))))
1270 (push var1 var-list
))
1271 (setq var1
(pop temp-var-list
)))
1272 (setq gdb-var-list
(nreverse var-list
))))))))
1273 (setq gdb-pending-triggers
1274 (delq 'gdb-var-update gdb-pending-triggers
))
1275 (gdb-speedbar-update))
1277 (defun gdb-speedbar-expand-node (text token indent
)
1278 "Expand the node the user clicked on.
1279 TEXT is the text of the button we clicked on, a + or - item.
1280 TOKEN is data related to this node.
1281 INDENT is the current indentation depth."
1282 (cond ((string-match "+" text
) ;expand this node
1283 (let* ((var (assoc token gdb-var-list
))
1284 (expr (nth 1 var
)) (children (nth 2 var
)))
1285 (if (or (<= (string-to-number children
) gdb-max-children
)
1287 (format "%s has %s children. Continue? " expr children
)))
1288 (gdb-var-list-children token
))))
1289 ((string-match "-" text
) ;contract this node
1290 (dolist (var gdb-var-list
)
1291 (if (string-match (concat token
"\\.") (car var
))
1292 (setq gdb-var-list
(delq var gdb-var-list
))))
1293 (gdb-var-delete-children token
)
1294 (speedbar-change-expand-button-char ?
+)
1295 (speedbar-delete-subblock indent
))
1296 (t (error "Ooops... not sure what to do")))
1297 (speedbar-center-buffer-smartly))
1299 (defun gdb-get-target-string ()
1300 (with-current-buffer gud-comint-buffer
1307 ;; Each buffer has a TYPE -- a symbol that identifies the function
1308 ;; of that particular buffer.
1310 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1311 ;; is constructed specially.
1313 ;; Others are constructed by gdb-get-buffer-create and
1314 ;; named according to the rules set forth in the gdb-buffer-rules
1316 (defvar gdb-buffer-rules
'())
1318 (defun gdb-rules-name-maker (rules-entry)
1320 (defun gdb-rules-buffer-mode (rules-entry)
1321 (nth 2 rules-entry
))
1322 (defun gdb-rules-update-trigger (rules-entry)
1323 (nth 3 rules-entry
))
1325 (defun gdb-update-buffer-name ()
1326 "Rename current buffer according to name-maker associated with
1327 it in `gdb-buffer-rules'."
1328 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1329 gdb-buffer-rules
))))
1330 (when f
(rename-buffer (funcall f
)))))
1332 (defun gdb-current-buffer-rules ()
1333 "Get `gdb-buffer-rules' entry for current buffer type."
1334 (assoc gdb-buffer-type gdb-buffer-rules
))
1336 (defun gdb-current-buffer-thread ()
1337 "Get thread object of current buffer from `gdb-threads-list'.
1339 When current buffer is not bound to any thread, return main
1341 (cdr (assoc gdb-thread-number gdb-threads-list
)))
1343 (defun gdb-current-buffer-frame ()
1344 "Get current stack frame object for thread of current buffer."
1345 (bindat-get-field (gdb-current-buffer-thread) 'frame
))
1347 (defun gdb-buffer-type (buffer)
1348 "Get value of `gdb-buffer-type' for BUFFER."
1349 (with-current-buffer buffer
1352 (defun gdb-buffer-shows-main-thread-p ()
1353 "Return t if current GDB buffer shows main selected thread and
1354 is not bound to it."
1356 (not (local-variable-p 'gdb-thread-number
)))
1358 (defun gdb-get-buffer (buffer-type &optional thread
)
1359 "Get a specific GDB buffer.
1361 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1362 and `gdb-thread-number' (if provided) must be equal to THREAD."
1364 (dolist (buffer (buffer-list) nil
)
1365 (with-current-buffer buffer
1366 (when (and (eq gdb-buffer-type buffer-type
)
1368 (equal gdb-thread-number thread
)))
1369 (throw 'found buffer
))))))
1371 (defun gdb-get-buffer-create (buffer-type &optional thread
)
1372 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1373 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1375 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1376 buffer-local variable of the new buffer.
1378 Buffer mode and name are selected according to buffer type.
1380 If buffer has trigger associated with it in `gdb-buffer-rules',
1381 this trigger is subscribed to `gdb-buf-publisher' and called with
1383 (or (gdb-get-buffer buffer-type thread
)
1384 (let ((rules (assoc buffer-type gdb-buffer-rules
))
1385 (new (generate-new-buffer "limbo")))
1386 (with-current-buffer new
1387 (let ((mode (gdb-rules-buffer-mode rules
))
1388 (trigger (gdb-rules-update-trigger rules
)))
1389 (when mode
(funcall mode
))
1390 (setq gdb-buffer-type buffer-type
)
1392 (set (make-local-variable 'gdb-thread-number
) thread
))
1393 (set (make-local-variable 'gud-minor-mode
)
1394 (buffer-local-value 'gud-minor-mode gud-comint-buffer
))
1395 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1396 (rename-buffer (funcall (gdb-rules-name-maker rules
)))
1398 (gdb-add-subscriber gdb-buf-publisher
1399 (cons (current-buffer)
1400 (gdb-bind-function-to-buffer
1401 trigger
(current-buffer))))
1402 (funcall trigger
'start
))
1403 (current-buffer))))))
1405 (defun gdb-bind-function-to-buffer (expr buffer
)
1406 "Return a function which will evaluate EXPR in BUFFER."
1407 `(lambda (&rest args
)
1408 (with-current-buffer ,buffer
1409 (apply ',expr args
))))
1411 ;; Used to define all gdb-frame-*-buffer functions except
1412 ;; `gdb-frame-io-buffer'
1413 (defmacro def-gdb-frame-for-buffer
(name buffer
&optional doc
)
1414 "Define a function NAME which shows gdb BUFFER in a separate frame.
1416 DOC is an optional documentation string."
1417 `(defun ,name
(&optional thread
)
1420 (let ((special-display-regexps (append special-display-regexps
'(".*")))
1421 (special-display-frame-alist gdb-frame-parameters
))
1422 (display-buffer (gdb-get-buffer-create ,buffer thread
)))))
1424 (defmacro def-gdb-display-buffer
(name buffer
&optional doc
)
1425 "Define a function NAME which shows gdb BUFFER.
1427 DOC is an optional documentation string."
1428 `(defun ,name
(&optional thread
)
1432 (gdb-get-buffer-create ,buffer thread
) t
)))
1434 ;; Used to display windows with thread-bound buffers
1435 (defmacro def-gdb-preempt-display-buffer
(name buffer
&optional doc
1437 `(defun ,name
(&optional thread
)
1440 (gdb-preempt-existing-or-display-buffer
1441 (gdb-get-buffer-create ,buffer thread
)
1442 ,split-horizontal
)))
1444 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1445 ;; at least one and possible more functions. The functions have these
1446 ;; roles in defining a buffer type:
1448 ;; NAME - Return a name for this buffer type.
1450 ;; The remaining function(s) are optional:
1452 ;; MODE - called in a new buffer with no arguments, should establish
1453 ;; the proper mode for the buffer.
1456 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
1457 (let ((binding (assoc buffer-type gdb-buffer-rules
)))
1459 (setcdr binding rules
)
1460 (push (cons buffer-type rules
)
1461 gdb-buffer-rules
))))
1463 (defun gdb-parent-mode ()
1464 "Generic mode to derive all other GDB buffer modes from."
1465 (kill-all-local-variables)
1466 (setq buffer-read-only t
)
1467 (buffer-disable-undo)
1468 ;; Delete buffer from gdb-buf-publisher when it's killed
1469 ;; (if it has an associated update trigger)
1474 (let ((trigger (gdb-rules-update-trigger
1475 (gdb-current-buffer-rules))))
1477 (gdb-delete-subscriber
1479 ;; This should match gdb-add-subscriber done in
1480 ;; gdb-get-buffer-create
1481 (cons (current-buffer)
1482 (gdb-bind-function-to-buffer trigger
(current-buffer))))))))
1485 ;; Partial-output buffer : This accumulates output from a command executed on
1486 ;; behalf of emacs (rather than the user).
1488 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1489 'gdb-partial-output-name
)
1491 (defun gdb-partial-output-name ()
1492 (concat " *partial-output-"
1493 (gdb-get-target-string)
1497 (gdb-set-buffer-rules 'gdb-inferior-io
1498 'gdb-inferior-io-name
1499 'gdb-inferior-io-mode
)
1501 (defun gdb-inferior-io-name ()
1502 (concat "*input/output of "
1503 (gdb-get-target-string)
1506 (defun gdb-display-io-buffer ()
1507 "Display IO of debugged program in a separate window."
1510 (gdb-get-buffer-create 'gdb-inferior-io
) t
))
1512 (defun gdb-inferior-io--init-proc (proc)
1513 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1514 (set-process-filter proc
'gdb-inferior-filter
)
1515 (set-process-sentinel proc
'gdb-inferior-io-sentinel
)
1517 (concat "-inferior-tty-set "
1518 ;; The process can run on a remote host.
1519 (or (process-get proc
'remote-tty
)
1520 (process-tty-name proc
)))
1523 (defun gdb-inferior-io-sentinel (proc str
)
1524 (when (eq (process-status proc
) 'failed
)
1525 ;; When the debugged process exits, Emacs gets an EIO error on
1526 ;; read from the pty, and stops listening to it. If the gdb
1527 ;; process is still running, remove the pty, make a new one, and
1529 (let ((gdb-proc (get-buffer-process gud-comint-buffer
))
1530 (io-buffer (process-buffer proc
)))
1531 (when (and gdb-proc
(process-live-p gdb-proc
)
1532 (buffer-live-p io-buffer
))
1533 ;; `comint-exec' deletes the original process as a side effect.
1534 (comint-exec io-buffer
"gdb-inferior" nil nil nil
)
1535 (gdb-inferior-io--init-proc (get-buffer-process io-buffer
))))))
1537 (defconst gdb-frame-parameters
1538 '((height .
14) (width .
80)
1540 (tool-bar-lines . nil
)
1541 (menu-bar-lines . nil
)
1542 (minibuffer . nil
)))
1544 (defun gdb-frame-io-buffer ()
1545 "Display IO of debugged program in a new frame."
1547 (let ((special-display-regexps (append special-display-regexps
'(".*")))
1548 (special-display-frame-alist gdb-frame-parameters
))
1549 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io
))))
1551 (defvar gdb-inferior-io-mode-map
1552 (let ((map (make-sparse-keymap)))
1553 (define-key map
"\C-c\C-c" 'gdb-io-interrupt
)
1554 (define-key map
"\C-c\C-z" 'gdb-io-stop
)
1555 (define-key map
"\C-c\C-\\" 'gdb-io-quit
)
1556 (define-key map
"\C-c\C-d" 'gdb-io-eof
)
1557 (define-key map
"\C-d" 'gdb-io-eof
)
1560 ;; We want to use comint because it has various nifty and familiar features.
1561 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
1562 "Major mode for gdb inferior-io."
1563 :syntax-table nil
:abbrev-table nil
1564 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil
))
1566 (defun gdb-inferior-filter (proc string
)
1567 (unless (string-equal string
"")
1568 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
) t
))
1569 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1570 (comint-output-filter proc string
)))
1572 (defun gdb-io-interrupt ()
1573 "Interrupt the program being debugged."
1576 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1578 (defun gdb-io-quit ()
1579 "Send quit signal to the program being debugged."
1582 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1584 (defun gdb-io-stop ()
1585 "Stop the program being debugged."
1588 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1590 (defun gdb-io-eof ()
1591 "Send end-of-file to the program being debugged."
1594 (get-buffer-process gud-comint-buffer
)))
1596 (defun gdb-clear-inferior-io ()
1597 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1601 (defconst breakpoint-xpm-data
1603 static char *magick[] = {
1604 /* columns rows colors chars-per-pixel */
1620 "XPM data used for breakpoint icon.")
1622 (defconst breakpoint-enabled-pbm-data
1625 0 0 0 0 1 1 1 1 0 0 0 0
1626 0 0 0 1 1 1 1 1 1 0 0 0
1627 0 0 1 1 1 1 1 1 1 1 0 0
1628 0 1 1 1 1 1 1 1 1 1 1 0
1629 0 1 1 1 1 1 1 1 1 1 1 0
1630 0 1 1 1 1 1 1 1 1 1 1 0
1631 0 1 1 1 1 1 1 1 1 1 1 0
1632 0 0 1 1 1 1 1 1 1 1 0 0
1633 0 0 0 1 1 1 1 1 1 0 0 0
1634 0 0 0 0 1 1 1 1 0 0 0 0"
1635 "PBM data used for enabled breakpoint icon.")
1637 (defconst breakpoint-disabled-pbm-data
1649 0 0 0 1 0 1 0 1 0 0"
1650 "PBM data used for disabled breakpoint icon.")
1652 (defvar breakpoint-enabled-icon nil
1653 "Icon for enabled breakpoint in display margin.")
1655 (defvar breakpoint-disabled-icon nil
1656 "Icon for disabled breakpoint in display margin.")
1658 (declare-function define-fringe-bitmap
"fringe.c"
1659 (bitmap bits
&optional height width align
))
1661 (and (display-images-p)
1662 ;; Bitmap for breakpoint in fringe
1663 (define-fringe-bitmap 'breakpoint
1664 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1665 ;; Bitmap for gud-overlay-arrow in fringe
1666 (define-fringe-bitmap 'hollow-right-triangle
1667 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1669 (defface breakpoint-enabled
1673 "Face for enabled breakpoint icon in fringe."
1676 (defface breakpoint-disabled
1677 '((((class color
) (min-colors 88)) :foreground
"grey70")
1678 ;; Ensure that on low-color displays that we end up something visible.
1679 (((class color
) (min-colors 8) (background light
))
1680 :foreground
"black")
1681 (((class color
) (min-colors 8) (background dark
))
1682 :foreground
"white")
1683 (((type tty
) (class mono
))
1685 (t :background
"gray"))
1686 "Face for disabled breakpoint icon in fringe."
1690 (defvar gdb-control-commands-regexp
1693 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1694 "while-stepping\\|stepping\\|ws\\|actions"
1695 "\\)\\([[:blank:]]+.*\\)?$")
1696 "Regexp matching GDB commands that enter a recursive reading loop.
1697 As long as GDB is in the recursive reading loop, it does not expect
1698 commands to be prefixed by \"-interpreter-exec console\".")
1700 (defun gdb-send (proc string
)
1701 "A comint send filter for gdb."
1702 (with-current-buffer gud-comint-buffer
1703 (let ((inhibit-read-only t
))
1704 (remove-text-properties (point-min) (point-max) '(face))))
1705 ;; mimic <RET> key to repeat previous command in GDB
1706 (if (not (string= "" string
))
1707 (setq gdb-last-command string
)
1708 (if gdb-last-command
(setq string gdb-last-command
)))
1709 (if (or (string-match "^-" string
)
1710 (> gdb-control-level
0))
1711 ;; Either MI command or we are feeding GDB's recursive reading loop.
1713 (setq gdb-first-done-or-error t
)
1714 (process-send-string proc
(concat string
"\n"))
1715 (if (and (string-match "^end$" string
)
1716 (> gdb-control-level
0))
1717 (setq gdb-control-level
(1- gdb-control-level
))))
1719 (if (string-match "\\\\$" string
)
1720 (setq gdb-continuation
(concat gdb-continuation string
"\n"))
1721 (setq gdb-first-done-or-error t
)
1722 (let ((to-send (concat "-interpreter-exec console "
1723 (gdb-mi-quote string
)
1725 (if gdb-enable-debug
1726 (push (cons 'mi-send to-send
) gdb-debug-log
))
1727 (process-send-string proc to-send
))
1728 (if (and (string-match "^end$" string
)
1729 (> gdb-control-level
0))
1730 (setq gdb-control-level
(1- gdb-control-level
)))
1731 (setq gdb-continuation nil
)))
1732 (if (string-match gdb-control-commands-regexp string
)
1733 (setq gdb-control-level
(1+ gdb-control-level
))))
1735 (defun gdb-mi-quote (string)
1736 "Return STRING quoted properly as an MI argument.
1737 The string is enclosed in double quotes.
1738 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1739 (setq string
(replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string
))
1740 (setq string
(replace-regexp-in-string "\n" "\\n" string t t
))
1741 (concat "\"" string
"\""))
1743 (defun gdb-input (command handler-function
)
1744 "Send COMMAND to GDB via the MI interface.
1745 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1747 (if gdb-enable-debug
(push (list 'send-item command handler-function
)
1749 (setq gdb-token-number
(1+ gdb-token-number
))
1750 (setq command
(concat (number-to-string gdb-token-number
) command
))
1751 (push (cons gdb-token-number handler-function
) gdb-handler-alist
)
1752 (process-send-string (get-buffer-process gud-comint-buffer
)
1753 (concat command
"\n")))
1755 ;; NOFRAME is used for gud execution control commands
1756 (defun gdb-current-context-command (command)
1757 "Add --thread to gdb COMMAND when needed."
1758 (if (and gdb-thread-number
1759 gdb-supports-non-stop
)
1760 (concat command
" --thread " gdb-thread-number
)
1763 (defun gdb-current-context-buffer-name (name)
1764 "Add thread information and asterisks to string NAME.
1766 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1768 (if (local-variable-p 'gdb-thread-number
)
1769 (format " (bound to thread %s)" gdb-thread-number
)
1773 (defun gdb-current-context-mode-name (mode)
1774 "Add thread information to MODE which is to be used as
1777 (if gdb-thread-number
1778 (format " [thread %s]" gdb-thread-number
)
1782 (defcustom gud-gdb-command-name
"gdb -i=mi"
1783 "Default command to execute an executable under the GDB debugger."
1788 (setq gud-running nil
)
1789 (setq gdb-output-sink
'user
)
1790 (setq gdb-pending-triggers nil
))
1792 (defun gdb-update (&optional no-proc
)
1793 "Update buffers showing status of debug session.
1794 If NO-PROC is non-nil, do not try to contact the GDB process."
1795 (when gdb-first-prompt
1796 (gdb-force-mode-line-update
1797 (propertize "initializing..." 'face font-lock-variable-name-face
))
1799 (setq gdb-first-prompt nil
))
1802 (gdb-get-main-selected-frame))
1804 ;; We may need to update gdb-threads-list so we can use
1805 (gdb-get-buffer-create 'gdb-threads-buffer
)
1806 ;; gdb-break-list is maintained in breakpoints handler
1807 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1810 (gdb-emit-signal gdb-buf-publisher
'update
))
1812 (gdb-get-changed-registers)
1813 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1814 (dolist (var gdb-var-list
)
1815 (setcar (nthcdr 5 var
) nil
))
1818 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1819 ;; because we may need to update current gud-running value without
1820 ;; changing current thread (see gdb-running)
1821 (defun gdb-setq-thread-number (number)
1822 "Only this function must be used to change `gdb-thread-number'
1823 value to NUMBER, because `gud-running' and `gdb-frame-number'
1824 need to be updated appropriately when current thread changes."
1825 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1826 (unless (string-equal number
"0") (setq gdb-thread-number number
))
1827 (setq gdb-frame-number
"0")
1828 (gdb-update-gud-running))
1830 (defun gdb-update-gud-running ()
1831 "Set `gud-running' according to the state of current thread.
1833 `gdb-frame-number' is set to 0 if current thread is now stopped.
1835 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1836 cannot be reliably used to determine whether or not execution
1837 control buttons should be shown in menu or toolbar. Use
1838 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1841 For all-stop mode, thread information is unavailable while target
1843 (let ((old-value gud-running
))
1845 (string= (bindat-get-field (gdb-current-buffer-thread) 'state
)
1847 ;; Set frame number to "0" when _current_ threads stops.
1848 (when (and (gdb-current-buffer-thread)
1849 (not (eq gud-running old-value
)))
1850 (setq gdb-frame-number
"0"))))
1852 (defun gdb-show-run-p ()
1853 "Return t if \"Run/continue\" should be shown on the toolbar."
1854 (or (not gdb-active-process
)
1856 (not gdb-gud-control-all-threads
)
1859 (and gdb-gud-control-all-threads
1860 (> gdb-stopped-threads-count
0))))
1862 (defun gdb-show-stop-p ()
1863 "Return t if \"Stop\" should be shown on the toolbar."
1865 (not gdb-gud-control-all-threads
)
1868 (and gdb-gud-control-all-threads
1869 (> gdb-running-threads-count
0))))
1871 ;; GUD displays the selected GDB frame. This might might not be the current
1872 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1873 ;; visited breakpoint is, use that window.
1874 (defun gdb-display-source-buffer (buffer)
1875 (let* ((last-window (if gud-last-last-frame
1877 (gud-find-file (car gud-last-last-frame
)))))
1878 (source-window (or last-window
1879 (if (and gdb-source-window
1880 (window-live-p gdb-source-window
))
1881 gdb-source-window
))))
1883 (setq gdb-source-window source-window
)
1884 (set-window-buffer source-window buffer
))
1887 (defun gdb-car< (a b
)
1888 (< (car a
) (car b
)))
1890 (defvar gdbmi-record-list
1891 '((gdb-gdb .
"(gdb) \n")
1892 (gdb-done .
"\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1893 (gdb-starting .
"\\([0-9]*\\)\\^running\n")
1894 (gdb-error .
"\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1895 (gdb-console .
"~\\(\".*?\"\\)\n")
1896 (gdb-internals .
"&\\(\".*?\"\\)\n")
1897 (gdb-stopped .
"\\*stopped,?\\(.*?\\)\n")
1898 (gdb-running .
"\\*running,\\(.*?\n\\)")
1899 (gdb-thread-created .
"=thread-created,\\(.*?\n\\)")
1900 (gdb-thread-selected .
"=thread-selected,\\(.*?\\)\n")
1901 (gdb-thread-exited .
"=thread-exited,\\(.*?\n\\)")
1902 (gdb-ignored-notification .
"=[-[:alpha:]]+,?\\(.*?\\)\n")
1903 (gdb-shell .
"\\(\\(?:^.+\n\\)+\\)")))
1905 (defun gud-gdbmi-marker-filter (string)
1906 "Filter GDB/MI output."
1908 ;; Record transactions if logging is enabled.
1909 (when gdb-enable-debug
1910 (push (cons 'recv string
) gdb-debug-log
)
1911 (if (and gdb-debug-log-max
1912 (> (length gdb-debug-log
) gdb-debug-log-max
))
1913 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
1915 ;; Recall the left over gud-marker-acc from last time.
1916 (setq gud-marker-acc
(concat gud-marker-acc string
))
1918 ;; Start accumulating output for the GUD buffer.
1919 (setq gdb-filter-output
"")
1920 (let (output-record-list)
1922 ;; Process all the complete markers in this chunk.
1923 (dolist (gdbmi-record gdbmi-record-list
)
1924 (while (string-match (cdr gdbmi-record
) gud-marker-acc
)
1925 (push (list (match-beginning 0)
1927 (match-string 1 gud-marker-acc
)
1928 (match-string 2 gud-marker-acc
)
1931 (setq gud-marker-acc
1932 (concat (substring gud-marker-acc
0 (match-beginning 0))
1933 ;; Pad with spaces to preserve position.
1934 (make-string (length (match-string 0 gud-marker-acc
)) 32)
1935 (substring gud-marker-acc
(match-end 0))))))
1937 (setq output-record-list
(sort output-record-list
'gdb-car
<))
1939 (dolist (output-record output-record-list
)
1940 (let ((record-type (cadr output-record
))
1941 (arg1 (nth 2 output-record
))
1942 (arg2 (nth 3 output-record
)))
1943 (cond ((eq record-type
'gdb-error
)
1944 (gdb-done-or-error arg2 arg1
'error
))
1945 ((eq record-type
'gdb-done
)
1946 (gdb-done-or-error arg2 arg1
'done
))
1947 ;; Suppress "No registers." GDB 6.8 and earlier
1948 ;; duplicates MI error message on internal stream.
1949 ;; Don't print to GUD buffer.
1950 ((not (and (eq record-type
'gdb-internals
)
1951 (string-equal (read arg1
) "No registers.\n")))
1952 (funcall record-type arg1
)))))
1954 (setq gdb-output-sink
'user
)
1956 (string-match "^ *" gud-marker-acc
)
1957 (setq gud-marker-acc
(substring gud-marker-acc
(match-end 0)))
1961 (defun gdb-gdb (_output-field))
1963 (defun gdb-shell (output-field)
1964 (setq gdb-filter-output
1965 (concat output-field gdb-filter-output
)))
1967 (defun gdb-ignored-notification (_output-field))
1969 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1970 (defun gdb-thread-created (_output-field))
1971 (defun gdb-thread-exited (output-field)
1972 "Handle =thread-exited async record: unset `gdb-thread-number'
1973 if current thread exited and update threads list."
1974 (let* ((thread-id (bindat-get-field (gdb-json-string output-field
) 'id
)))
1975 (if (string= gdb-thread-number thread-id
)
1976 (gdb-setq-thread-number nil
))
1977 ;; When we continue current thread and it quickly exits,
1978 ;; gdb-pending-triggers left after gdb-running disallow us to
1979 ;; properly call -thread-info without --thread option. Thus we
1980 ;; need to use gdb-wait-for-pending.
1981 (gdb-wait-for-pending
1982 (gdb-emit-signal gdb-buf-publisher
'update-threads
))))
1984 (defun gdb-thread-selected (output-field)
1985 "Handler for =thread-selected MI output record.
1987 Sets `gdb-thread-number' to new id."
1988 (let* ((result (gdb-json-string output-field
))
1989 (thread-id (bindat-get-field result
'id
)))
1990 (gdb-setq-thread-number thread-id
)
1991 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1992 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1993 ;; as usually. Things happen to fast and second call (from
1994 ;; gdb-thread-selected handler) gets cut off by our beloved
1995 ;; gdb-pending-triggers.
1996 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1997 ;; body will get executed when `gdb-pending-triggers` is empty.
1998 (gdb-wait-for-pending
2001 (defun gdb-running (output-field)
2003 (bindat-get-field (gdb-json-string output-field
) 'thread-id
)))
2004 ;; We reset gdb-frame-number to nil if current thread has gone
2005 ;; running. This can't be done in gdb-thread-list-handler-custom
2006 ;; because we need correct gdb-frame-number by the time
2007 ;; -thread-info command is sent.
2008 (when (or (string-equal thread-id
"all")
2009 (string-equal thread-id gdb-thread-number
))
2010 (setq gdb-frame-number nil
)))
2011 (setq gdb-inferior-status
"running")
2012 (gdb-force-mode-line-update
2013 (propertize gdb-inferior-status
'face font-lock-type-face
))
2014 (when (not gdb-non-stop
)
2015 (setq gud-running t
))
2016 (setq gdb-active-process t
)
2017 (gdb-emit-signal gdb-buf-publisher
'update-threads
))
2019 (defun gdb-starting (_output-field)
2020 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2021 (setq gdb-inferior-status
"running")
2022 (gdb-force-mode-line-update
2023 (propertize gdb-inferior-status
'face font-lock-type-face
))
2024 (setq gdb-active-process t
)
2025 (setq gud-running t
)
2026 ;; GDB doesn't seem to respond to -thread-info before first stop or
2027 ;; thread exit (even in non-stop mode), so this is useless.
2028 ;; Behavior may change in the future.
2029 (gdb-emit-signal gdb-buf-publisher
'update-threads
))
2031 ;; -break-insert -t didn't give a reason before gdb 6.9
2033 (defun gdb-stopped (output-field)
2034 "Given the contents of *stopped MI async record, select new
2035 current thread and update GDB buffers."
2036 ;; Reason is available with target-async only
2037 (let* ((result (gdb-json-string output-field
))
2038 (reason (bindat-get-field result
'reason
))
2039 (thread-id (bindat-get-field result
'thread-id
)))
2041 ;; -data-list-register-names needs to be issued for any stopped
2043 (when (not gdb-register-names
)
2044 (gdb-input (concat "-data-list-register-names"
2045 (if gdb-supports-non-stop
2046 (concat " --thread " thread-id
)))
2047 'gdb-register-names-handler
))
2049 ;; Don't set gud-last-frame here as it's currently done in
2050 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2054 ;; ;; Extract the frame position from the marker.
2055 ;; gud-last-frame (cons file
2056 ;; (string-to-number
2057 ;; (match-string 6 gud-marker-acc)))))
2059 (setq gdb-inferior-status
(or reason
"unknown"))
2060 (gdb-force-mode-line-update
2061 (propertize gdb-inferior-status
'face font-lock-warning-face
))
2062 (if (string-equal reason
"exited-normally")
2063 (setq gdb-active-process nil
))
2065 ;; Select new current thread.
2067 ;; Don't switch if we have no reasons selected
2068 (when gdb-switch-reasons
2069 ;; Switch from another stopped thread only if we have
2070 ;; gdb-switch-when-another-stopped:
2071 (when (or gdb-switch-when-another-stopped
2072 (not (string= "stopped"
2073 (bindat-get-field (gdb-current-buffer-thread) 'state
))))
2074 ;; Switch if current reason has been selected or we have no
2076 (if (or (eq gdb-switch-reasons t
)
2077 (member reason gdb-switch-reasons
))
2078 (when (not (string-equal gdb-thread-number thread-id
))
2079 (message (concat "Switched to thread " thread-id
))
2080 (gdb-setq-thread-number thread-id
))
2081 (message (format "Thread %s stopped" thread-id
)))))
2083 ;; Print "(gdb)" to GUD console
2084 (when gdb-first-done-or-error
2085 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2087 ;; In non-stop, we update information as soon as another thread gets
2089 (when (or gdb-first-done-or-error
2091 ;; In all-stop this updates gud-running properly as well.
2093 (setq gdb-first-done-or-error nil
))
2094 (run-hook-with-args 'gdb-stopped-functions result
)))
2096 ;; Remove the trimmings from log stream containing debugging messages
2097 ;; being produced by GDB's internals, use warning face and send to GUD
2099 (defun gdb-internals (output-field)
2100 (setq gdb-filter-output
2103 (let ((error-message
2104 (read output-field
)))
2106 0 (length error-message
)
2107 'face font-lock-warning-face
2111 ;; Remove the trimmings from the console stream and send to GUD buffer
2112 ;; (frontend MI commands should not print to this stream)
2113 (defun gdb-console (output-field)
2114 (setq gdb-filter-output
2115 (gdb-concat-output gdb-filter-output
(read output-field
))))
2117 (defun gdb-done-or-error (output-field token-number type
)
2118 (if (string-equal token-number
"")
2119 ;; Output from command entered by user
2121 (setq gdb-output-sink
'user
)
2122 (setq token-number nil
)
2123 ;; MI error - send to minibuffer
2124 (when (eq type
'error
)
2125 ;; Skip "msg=" from `output-field'
2126 (message (read (substring output-field
4)))
2127 ;; Don't send to the console twice. (If it is a console error
2128 ;; it is also in the console stream.)
2129 (setq output-field nil
)))
2130 ;; Output from command from frontend.
2131 (setq gdb-output-sink
'emacs
))
2133 (gdb-clear-partial-output)
2135 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2136 (let* ((proc (get-buffer-process gud-comint-buffer
))
2137 (no-proc (or (null proc
)
2138 (memq (process-status proc
) '(exit signal
)))))
2140 (when gdb-first-done-or-error
2141 (unless (or token-number gud-running no-proc
)
2142 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2143 (gdb-update no-proc
)
2144 (setq gdb-first-done-or-error nil
))
2146 (setq gdb-filter-output
2147 (gdb-concat-output gdb-filter-output output-field
))
2150 (with-current-buffer
2151 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2153 (cdr (assoc (string-to-number token-number
) gdb-handler-alist
))))
2154 (setq gdb-handler-alist
2155 (assq-delete-all token-number gdb-handler-alist
)))))
2157 (defun gdb-concat-output (so-far new
)
2159 ((eq gdb-output-sink
'user
) (concat so-far new
))
2160 ((eq gdb-output-sink
'emacs
)
2161 (gdb-append-to-partial-output new
)
2164 (defun gdb-append-to-partial-output (string)
2165 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2166 (goto-char (point-max))
2169 (defun gdb-clear-partial-output ()
2170 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2173 (defun gdb-jsonify-buffer (&optional fix-key fix-list
)
2174 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2176 Field names are wrapped in double quotes and equal signs are
2177 replaced with semicolons.
2179 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2180 partial output. This is used to get rid of useless keys in lists
2181 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2182 -break-info are examples of MI commands which issue such
2185 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2186 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2187 -break-info output when it contains breakpoint script field
2188 incompatible with GDB/MI output syntax."
2190 (goto-char (point-min))
2193 (while (re-search-forward (concat "[\\[,]\\(" fix-key
"=\\)") nil t
)
2194 (replace-match "" nil nil nil
1))))
2197 ;; Find positions of braces which enclose broken list
2198 (while (re-search-forward (concat fix-list
"={\"") nil t
)
2199 (let ((p1 (goto-char (- (point) 2)))
2200 (p2 (progn (forward-sexp)
2202 ;; Replace braces with brackets
2210 (goto-char (point-min))
2212 (while (re-search-forward
2213 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t
)
2214 (replace-match "\"\\1\":\\2" nil nil
))
2215 (goto-char (point-max))
2218 (defun gdb-json-read-buffer (&optional fix-key fix-list
)
2219 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2221 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2222 (gdb-jsonify-buffer fix-key fix-list
)
2224 (goto-char (point-min))
2225 (let ((json-array-type 'list
))
2228 (defun gdb-json-string (string &optional fix-key fix-list
)
2229 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2231 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2234 (gdb-json-read-buffer fix-key fix-list
)))
2236 (defun gdb-json-partial-output (&optional fix-key fix-list
)
2237 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2239 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2240 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2241 (gdb-json-read-buffer fix-key fix-list
)))
2243 (defun gdb-line-posns (line)
2244 "Return a pair of LINE beginning and end positions."
2245 (let ((offset (1+ (- line
(line-number-at-pos)))))
2247 (line-beginning-position offset
)
2248 (line-end-position offset
))))
2250 (defmacro gdb-mark-line
(line variable
)
2251 "Set VARIABLE marker to point at beginning of LINE.
2253 If current window has no fringes, inverse colors on LINE.
2255 Return position where LINE begins."
2257 (let* ((posns (gdb-line-posns ,line
))
2258 (start-posn (car posns
))
2259 (end-posn (cdr posns
)))
2260 (set-marker ,variable
(copy-marker start-posn
))
2261 (when (not (> (car (window-fringes)) 0))
2262 (put-text-property start-posn end-posn
2263 'font-lock-face
'(:inverse-video t
)))
2266 (defun gdb-pad-string (string padding
)
2267 (format (concat "%" (number-to-string padding
) "s") string
))
2269 ;; gdb-table struct is a way to programmatically construct simple
2270 ;; tables. It help to reliably align columns of data in GDB buffers
2272 (cl-defstruct gdb-table
2275 (row-properties nil
)
2278 (defun gdb-mapcar* (function &rest seqs
)
2279 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2280 If there are several SEQS, FUNCTION is called with that many
2281 arguments, and mapping stops as soon as the shortest list runs
2283 (let ((shortest (apply #'min
(mapcar #'length seqs
))))
2290 (number-sequence 0 (1- shortest
)))))
2292 (defun gdb-table-add-row (table row
&optional properties
)
2293 "Add ROW of string to TABLE and recalculate column sizes.
2295 When non-nil, PROPERTIES will be added to the whole row when
2296 calling `gdb-table-string'."
2297 (let ((rows (gdb-table-rows table
))
2298 (row-properties (gdb-table-row-properties table
))
2299 (column-sizes (gdb-table-column-sizes table
))
2300 (right-align (gdb-table-right-align table
)))
2301 (when (not column-sizes
)
2302 (setf (gdb-table-column-sizes table
)
2303 (make-list (length row
) 0)))
2304 (setf (gdb-table-rows table
)
2305 (append rows
(list row
)))
2306 (setf (gdb-table-row-properties table
)
2307 (append row-properties
(list properties
)))
2308 (setf (gdb-table-column-sizes table
)
2309 (gdb-mapcar* (lambda (x s
)
2311 (max (abs x
) (string-width (or s
"")))))
2312 (if right-align new-x
(- new-x
))))
2313 (gdb-table-column-sizes table
)
2315 ;; Avoid trailing whitespace at eol
2316 (if (not (gdb-table-right-align table
))
2317 (setcar (last (gdb-table-column-sizes table
)) 0))))
2319 (defun gdb-table-string (table &optional sep
)
2320 "Return TABLE as a string with columns separated with SEP."
2321 (let ((column-sizes (gdb-table-column-sizes table
)))
2325 (lambda (row properties
)
2327 (mapconcat 'identity
2328 (gdb-mapcar* (lambda (s x
) (gdb-pad-string s x
))
2332 (gdb-table-rows table
)
2333 (gdb-table-row-properties table
))
2336 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2337 (defun gdb-get-many-fields (struct &rest fields
)
2338 "Return a list of FIELDS values from STRUCT."
2340 (dolist (field fields
)
2341 (push (bindat-get-field struct field
) values
))
2344 (defmacro def-gdb-auto-update-trigger
(trigger-name gdb-command
2346 &optional signal-list
)
2347 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2348 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2349 buffer with `gdb-bind-function-to-buffer'.
2351 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2352 defined trigger is called with an argument from SIGNAL-LIST. It's
2353 not recommended to define triggers with empty SIGNAL-LIST.
2354 Normally triggers should respond at least to 'update signal.
2356 Normally the trigger defined by this command must be called from
2357 the buffer where HANDLER-NAME must work. This should be done so
2358 that buffer-local thread number may be used in GDB-COMMAND (by
2359 calling `gdb-current-context-command').
2360 `gdb-bind-function-to-buffer' is used to achieve this, see
2361 `gdb-get-buffer-create'.
2363 Triggers defined by this command are meant to be used as a
2364 trigger argument when describing buffer types with
2365 `gdb-set-buffer-rules'."
2366 `(defun ,trigger-name
(&optional signal
)
2368 (or (not ,signal-list
)
2369 (memq signal
,signal-list
))
2370 (when (not (gdb-pending-p
2371 (cons (current-buffer) ',trigger-name
)))
2372 (gdb-input ,gdb-command
2373 (gdb-bind-function-to-buffer ',handler-name
(current-buffer)))
2374 (gdb-add-pending (cons (current-buffer) ',trigger-name
))))))
2376 ;; Used by disassembly buffer only, the rest use
2377 ;; def-gdb-trigger-and-handler
2378 (defmacro def-gdb-auto-update-handler
(handler-name trigger-name custom-defun
2379 &optional nopreserve
)
2380 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2382 Handlers are normally called from the buffers they put output in.
2384 Delete ((current-buffer) . TRIGGER-NAME) from
2385 `gdb-pending-triggers', erase current buffer and evaluate
2386 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2388 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2389 `(defun ,handler-name
()
2390 (gdb-delete-pending (cons (current-buffer) ',trigger-name
))
2391 (let* ((buffer-read-only nil
)
2392 (window (get-buffer-window (current-buffer) 0))
2393 (start (window-start window
))
2394 (p (window-point window
)))
2397 (gdb-update-buffer-name)
2398 ,(when (not nopreserve
)
2399 '(set-window-start window start
)
2400 '(set-window-point window p
)))))
2402 (defmacro def-gdb-trigger-and-handler
(trigger-name gdb-command
2403 handler-name custom-defun
2404 &optional signal-list
)
2405 "Define trigger and handler.
2407 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2408 `def-gdb-auto-update-trigger'.
2410 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2411 `def-gdb-auto-update-handler'."
2413 (def-gdb-auto-update-trigger ,trigger-name
2415 ,handler-name
,signal-list
)
2416 (def-gdb-auto-update-handler ,handler-name
2417 ,trigger-name
,custom-defun
)))
2421 ;; Breakpoint buffer : This displays the output of `-break-list'.
2422 (def-gdb-trigger-and-handler
2423 gdb-invalidate-breakpoints
"-break-list"
2424 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2427 (gdb-set-buffer-rules
2428 'gdb-breakpoints-buffer
2429 'gdb-breakpoints-buffer-name
2430 'gdb-breakpoints-mode
2431 'gdb-invalidate-breakpoints
)
2433 (defun gdb-breakpoints-list-handler-custom ()
2434 (let ((breakpoints-list (bindat-get-field
2435 (gdb-json-partial-output "bkpt" "script")
2436 'BreakpointTable
'body
))
2437 (table (make-gdb-table)))
2438 (setq gdb-breakpoints-list nil
)
2439 (gdb-table-add-row table
'("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2440 (dolist (breakpoint breakpoints-list
)
2441 (add-to-list 'gdb-breakpoints-list
2442 (cons (bindat-get-field breakpoint
'number
)
2444 (let ((at (bindat-get-field breakpoint
'at
))
2445 (pending (bindat-get-field breakpoint
'pending
))
2446 (func (bindat-get-field breakpoint
'func
))
2447 (type (bindat-get-field breakpoint
'type
)))
2448 (gdb-table-add-row table
2450 (bindat-get-field breakpoint
'number
)
2452 (or (bindat-get-field breakpoint
'disp
) "")
2453 (let ((flag (bindat-get-field breakpoint
'enabled
)))
2454 (if (string-equal flag
"y")
2455 (propertize "y" 'font-lock-face font-lock-warning-face
)
2456 (propertize "n" 'font-lock-face font-lock-comment-face
)))
2457 (bindat-get-field breakpoint
'addr
)
2458 (or (bindat-get-field breakpoint
'times
) "")
2459 (if (and type
(string-match ".*watchpoint" type
))
2460 (bindat-get-field breakpoint
'what
)
2463 (propertize (or func
"unknown")
2464 'font-lock-face font-lock-function-name-face
)
2465 (gdb-frame-location breakpoint
)))))
2466 ;; Add clickable properties only for breakpoints with file:line
2468 (append (list 'gdb-breakpoint breakpoint
)
2469 (when func
'(help-echo "mouse-2, RET: visit breakpoint"
2470 mouse-face highlight
))))))
2471 (insert (gdb-table-string table
" "))
2472 (gdb-place-breakpoints)))
2474 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2475 (defun gdb-place-breakpoints ()
2476 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2477 (dolist (buffer (buffer-list))
2478 (with-current-buffer buffer
2479 (if (and (eq gud-minor-mode
'gdbmi
)
2480 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2481 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2482 (dolist (breakpoint gdb-breakpoints-list
)
2483 (let* ((breakpoint (cdr breakpoint
)) ; gdb-breakpoints-list is
2484 ; an associative list
2485 (line (bindat-get-field breakpoint
'line
)))
2487 (let ((file (bindat-get-field breakpoint
'fullname
))
2488 (flag (bindat-get-field breakpoint
'enabled
))
2489 (bptno (bindat-get-field breakpoint
'number
)))
2490 (unless (file-exists-p file
)
2491 (setq file
(cdr (assoc bptno gdb-location-alist
))))
2493 (not (string-equal file
"File not found")))
2494 (with-current-buffer
2495 (find-file-noselect file
'nowarn
)
2497 ;; Only want one breakpoint icon at each location.
2498 (gdb-put-breakpoint-icon (string-equal flag
"y") bptno
2499 (string-to-number line
)))
2500 (gdb-input (concat "list " file
":1") 'ignore
)
2501 (gdb-input "-file-list-exec-source-file"
2502 `(lambda () (gdb-get-location
2503 ,bptno
,line
,flag
)))))))))
2505 (defvar gdb-source-file-regexp
"fullname=\"\\(.*?\\)\"")
2507 (defun gdb-get-location (bptno line flag
)
2508 "Find the directory containing the relevant source file.
2509 Put in buffer and place breakpoint icon."
2510 (goto-char (point-min))
2511 (catch 'file-not-found
2512 (if (re-search-forward gdb-source-file-regexp nil t
)
2513 (delete (cons bptno
"File not found") gdb-location-alist
)
2514 (push (cons bptno
(match-string 1)) gdb-location-alist
)
2516 (unless (assoc bptno gdb-location-alist
)
2517 (push (cons bptno
"File not found") gdb-location-alist
)
2518 (message-box "Cannot find source file for breakpoint location.
2519 Add directory to search path for source files using the GDB command, dir."))
2520 (throw 'file-not-found nil
))
2521 (with-current-buffer (find-file-noselect (match-string 1))
2523 ;; only want one breakpoint icon at each location
2524 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
(string-to-number line
)))))
2526 (add-hook 'find-file-hook
'gdb-find-file-hook
)
2528 (defun gdb-find-file-hook ()
2529 "Set up buffer for debugging if file is part of the source code
2530 of the current session."
2531 (if (and (buffer-name gud-comint-buffer
)
2532 ;; in case gud or gdb-ui is just loaded
2534 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
2536 (if (member buffer-file-name gdb-source-file-list
)
2537 (with-current-buffer (find-buffer-visiting buffer-file-name
)
2538 (gdb-init-buffer)))))
2540 (declare-function gud-remove
"gdb-mi" t t
) ; gud-def
2541 (declare-function gud-break
"gdb-mi" t t
) ; gud-def
2542 (declare-function fringe-bitmaps-at-pos
"fringe.c" (&optional pos window
))
2544 (defun gdb-mouse-set-clear-breakpoint (event)
2545 "Set/clear breakpoint in left fringe/margin at mouse click.
2546 If not in a source or disassembly buffer just set point."
2548 (mouse-minibuffer-check event
)
2549 (let ((posn (event-end event
)))
2550 (with-selected-window (posn-window posn
)
2551 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode
))
2552 (if (numberp (posn-point posn
))
2554 (goto-char (posn-point posn
))
2555 (if (or (posn-object posn
)
2556 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
2560 (posn-set-point posn
))))
2562 (defun gdb-mouse-toggle-breakpoint-margin (event)
2563 "Enable/disable breakpoint in left margin with mouse click."
2565 (mouse-minibuffer-check event
)
2566 (let ((posn (event-end event
)))
2567 (if (numberp (posn-point posn
))
2568 (with-selected-window (posn-window posn
)
2570 (goto-char (posn-point posn
))
2571 (if (posn-object posn
)
2573 (let ((bptno (get-text-property
2574 0 'gdb-bptno
(car (posn-string posn
)))))
2576 (if (get-text-property
2577 0 'gdb-enabled
(car (posn-string posn
)))
2582 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2583 "Enable/disable breakpoint in left fringe with mouse click."
2585 (mouse-minibuffer-check event
)
2586 (let* ((posn (event-end event
))
2587 (pos (posn-point posn
))
2590 (with-selected-window (posn-window posn
)
2591 (with-current-buffer (window-buffer (selected-window))
2593 (dolist (overlay (overlays-in pos pos
))
2594 (when (overlay-get overlay
'put-break
)
2595 (setq obj
(overlay-get overlay
'before-string
))))
2599 (if (get-text-property 0 'gdb-enabled obj
)
2602 (get-text-property 0 'gdb-bptno obj
)))))))))
2604 (defun gdb-breakpoints-buffer-name ()
2605 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2607 (def-gdb-display-buffer
2608 gdb-display-breakpoints-buffer
2609 'gdb-breakpoints-buffer
2610 "Display status of user-settable breakpoints.")
2612 (def-gdb-frame-for-buffer
2613 gdb-frame-breakpoints-buffer
2614 'gdb-breakpoints-buffer
2615 "Display status of user-settable breakpoints in a new frame.")
2617 (defvar gdb-breakpoints-mode-map
2618 (let ((map (make-sparse-keymap))
2619 (menu (make-sparse-keymap "Breakpoints")))
2620 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
2621 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2622 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2623 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2624 (suppress-keymap map)
2625 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2626 (define-key map " " 'gdb-toggle-breakpoint)
2627 (define-key map "D" 'gdb-delete-breakpoint)
2628 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2629 (define-key map "q" 'gdb-delete-frame-or-window)
2630 (define-key map "\r" 'gdb-goto-breakpoint)
2631 (define-key map "\t" (lambda ()
2633 (gdb-set-window-buffer
2634 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2635 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2636 (define-key map [follow-link] 'mouse-face)
2639 (defun gdb-delete-frame-or-window ()
2640 "Delete frame if there is only one window. Otherwise delete the window."
2642 (if (one-window-p) (delete-frame)
2645 ;;from make-mode-line-mouse-map
2646 (defun gdb-make-header-line-mouse-map (mouse function) "\
2647 Return a keymap with single entry for mouse key MOUSE on the header line.
2648 MOUSE is defined to run function FUNCTION with no args in the buffer
2649 corresponding to the mode line clicked."
2650 (let ((map (make-sparse-keymap)))
2651 (define-key map (vector 'header-line mouse) function)
2652 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2655 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2657 'help-echo ,help-echo
2658 'mouse-face ',mouse-face
2661 (gdb-make-header-line-mouse-map
2663 (lambda (event) (interactive "e")
2664 (save-selected-window
2665 (select-window (posn-window (event-start event)))
2666 (gdb-set-window-buffer
2667 (gdb-get-buffer-create ',buffer) t) )))))
2670 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2673 (defun gdb-threads-buffer-name ()
2674 (concat "*threads of " (gdb-get-target-string) "*"))
2676 (def-gdb-display-buffer
2677 gdb-display-threads-buffer
2679 "Display GDB threads.")
2681 (def-gdb-frame-for-buffer
2682 gdb-frame-threads-buffer
2684 "Display GDB threads in a new frame.")
2686 (def-gdb-trigger-and-handler
2687 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2688 gdb-thread-list-handler gdb-thread-list-handler-custom
2689 '(start update update-threads))
2691 (gdb-set-buffer-rules
2693 'gdb-threads-buffer-name
2695 'gdb-invalidate-threads)
2697 (defvar gdb-threads-font-lock-keywords
2698 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2699 (" \\(stopped\\)" (1 font-lock-warning-face))
2700 (" \\(running\\)" (1 font-lock-string-face))
2701 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2702 "Font lock keywords used in `gdb-threads-mode'.")
2704 (defvar gdb-threads-mode-map
2705 (let ((map (make-sparse-keymap)))
2706 (define-key map "\r" 'gdb-select-thread)
2707 (define-key map "f" 'gdb-display-stack-for-thread)
2708 (define-key map "F" 'gdb-frame-stack-for-thread)
2709 (define-key map "l" 'gdb-display-locals-for-thread)
2710 (define-key map "L" 'gdb-frame-locals-for-thread)
2711 (define-key map "r" 'gdb-display-registers-for-thread)
2712 (define-key map "R" 'gdb-frame-registers-for-thread)
2713 (define-key map "d" 'gdb-display-disassembly-for-thread)
2714 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2715 (define-key map "i" 'gdb-interrupt-thread)
2716 (define-key map "c" 'gdb-continue-thread)
2717 (define-key map "s" 'gdb-step-thread)
2718 (define-key map "\t"
2721 (gdb-set-window-buffer
2722 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2723 (define-key map [mouse-2] 'gdb-select-thread)
2724 (define-key map [follow-link] 'mouse-face)
2727 (defvar gdb-threads-header
2729 (gdb-propertize-header
2730 "Breakpoints" gdb-breakpoints-buffer
2731 "mouse-1: select" mode-line-highlight mode-line-inactive)
2733 (gdb-propertize-header "Threads" gdb-threads-buffer
2734 nil nil mode-line)))
2736 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2737 "Major mode for GDB threads."
2738 (setq gdb-thread-position (make-marker))
2739 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2740 (setq header-line-format gdb-threads-header)
2741 (set (make-local-variable 'font-lock-defaults)
2742 '(gdb-threads-font-lock-keywords))
2743 'gdb-invalidate-threads)
2745 (defun gdb-thread-list-handler-custom ()
2746 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2747 (table (make-gdb-table))
2749 (setq gdb-threads-list nil)
2750 (setq gdb-running-threads-count 0)
2751 (setq gdb-stopped-threads-count 0)
2752 (set-marker gdb-thread-position nil)
2754 (dolist (thread (reverse threads-list))
2755 (let ((running (equal (bindat-get-field thread 'state) "running")))
2756 (add-to-list 'gdb-threads-list
2757 (cons (bindat-get-field thread 'id)
2759 (cl-incf (if running
2760 gdb-running-threads-count
2761 gdb-stopped-threads-count))
2763 (gdb-table-add-row table
2765 (bindat-get-field thread 'id)
2767 (if gdb-thread-buffer-verbose-names
2768 (concat (bindat-get-field thread 'target-id) " ") "")
2769 (bindat-get-field thread 'state)
2770 ;; Include frame information for stopped threads
2773 " in " (bindat-get-field thread 'frame 'func)
2774 (if gdb-thread-buffer-arguments
2777 (let ((args (bindat-get-field thread 'frame 'args)))
2780 (apply #'format "%s=%s"
2781 (gdb-get-many-fields arg 'name 'value)))
2785 (if gdb-thread-buffer-locations
2786 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2787 (if gdb-thread-buffer-addresses
2788 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2792 'mouse-face 'highlight
2793 'help-echo "mouse-2, RET: select thread")))
2794 (when (string-equal gdb-thread-number
2795 (bindat-get-field thread 'id))
2796 (setq marked-line (length gdb-threads-list))))
2797 (insert (gdb-table-string table " "))
2799 (gdb-mark-line marked-line gdb-thread-position)))
2800 ;; We update gud-running here because we need to make sure that
2801 ;; gdb-threads-list is up-to-date
2802 (gdb-update-gud-running)
2803 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2805 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2806 "Define a NAME command which will act upon thread on the current line.
2808 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2809 be the value of 'gdb-thread property of the current line. If
2810 'gdb-thread is nil, error is signaled."
2811 `(defun ,name (&optional event)
2813 (interactive (list last-input-event))
2814 (if event (posn-set-point (event-end event)))
2817 (let ((thread (get-text-property (point) 'gdb-thread)))
2820 (error "Not recognized as thread line"))))))
2822 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2824 "Define a NAME which will call BUFFER-COMMAND with id of thread
2825 on the current line."
2826 `(def-gdb-thread-buffer-command ,name
2827 (,buffer-command (bindat-get-field thread 'id))
2830 (def-gdb-thread-buffer-command gdb-select-thread
2831 (let ((new-id (bindat-get-field thread 'id)))
2832 (gdb-setq-thread-number new-id)
2833 (gdb-input (concat "-thread-select " new-id) 'ignore)
2835 "Select the thread at current line of threads buffer.")
2837 (def-gdb-thread-buffer-simple-command
2838 gdb-display-stack-for-thread
2839 gdb-preemptively-display-stack-buffer
2840 "Display stack buffer for the thread at current line.")
2842 (def-gdb-thread-buffer-simple-command
2843 gdb-display-locals-for-thread
2844 gdb-preemptively-display-locals-buffer
2845 "Display locals buffer for the thread at current line.")
2847 (def-gdb-thread-buffer-simple-command
2848 gdb-display-registers-for-thread
2849 gdb-preemptively-display-registers-buffer
2850 "Display registers buffer for the thread at current line.")
2852 (def-gdb-thread-buffer-simple-command
2853 gdb-display-disassembly-for-thread
2854 gdb-preemptively-display-disassembly-buffer
2855 "Display disassembly buffer for the thread at current line.")
2857 (def-gdb-thread-buffer-simple-command
2858 gdb-frame-stack-for-thread
2859 gdb-frame-stack-buffer
2860 "Display a new frame with stack buffer for the thread at
2863 (def-gdb-thread-buffer-simple-command
2864 gdb-frame-locals-for-thread
2865 gdb-frame-locals-buffer
2866 "Display a new frame with locals buffer for the thread at
2869 (def-gdb-thread-buffer-simple-command
2870 gdb-frame-registers-for-thread
2871 gdb-frame-registers-buffer
2872 "Display a new frame with registers buffer for the thread at
2875 (def-gdb-thread-buffer-simple-command
2876 gdb-frame-disassembly-for-thread
2877 gdb-frame-disassembly-buffer
2878 "Display a new frame with disassembly buffer for the thread at
2881 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2882 "Define a NAME which will execute GUD-COMMAND with
2883 `gdb-thread-number' locally bound to id of thread on the current
2885 `(def-gdb-thread-buffer-command ,name
2887 (let ((gdb-thread-number (bindat-get-field thread 'id))
2888 (gdb-gud-control-all-threads nil))
2889 (call-interactively #',gud-command))
2890 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2893 (def-gdb-thread-buffer-gud-command
2894 gdb-interrupt-thread
2896 "Interrupt thread at current line.")
2898 (def-gdb-thread-buffer-gud-command
2901 "Continue thread at current line.")
2903 (def-gdb-thread-buffer-gud-command
2906 "Step thread at current line.")
2911 (defcustom gdb-memory-rows 8
2912 "Number of data rows in memory window."
2917 (defcustom gdb-memory-columns 4
2918 "Number of data columns in memory window."
2923 (defcustom gdb-memory-format "x"
2924 "Display format of data items in memory window."
2925 :type '(choice (const :tag "Hexadecimal" "x")
2926 (const :tag "Signed decimal" "d")
2927 (const :tag "Unsigned decimal" "u")
2928 (const :tag "Octal" "o")
2929 (const :tag "Binary" "t"))
2933 (defcustom gdb-memory-unit 4
2934 "Unit size of data items in memory window."
2935 :type '(choice (const :tag "Byte" 1)
2936 (const :tag "Halfword" 2)
2937 (const :tag "Word" 4)
2938 (const :tag "Giant word" 8))
2942 (def-gdb-trigger-and-handler
2943 gdb-invalidate-memory
2944 (format "-data-read-memory %s %s %d %d %d"
2950 gdb-read-memory-handler
2951 gdb-read-memory-custom
2954 (gdb-set-buffer-rules
2956 'gdb-memory-buffer-name
2958 'gdb-invalidate-memory)
2960 (defun gdb-memory-column-width (size format)
2961 "Return length of string with memory unit of SIZE in FORMAT.
2963 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2964 in `gdb-memory-format'."
2965 (let ((format-base (cdr (assoc format
2967 ("d" . 10) ("u" . 10)
2971 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2972 (cond ((string-equal format "x")
2973 (+ 2 res)) ; hexadecimal numbers have 0x in front
2974 ((or (string-equal format "d")
2975 (string-equal format "o"))
2978 (error "Unknown format"))))
2980 (defun gdb-read-memory-custom ()
2981 (let* ((res (gdb-json-partial-output))
2982 (err-msg (bindat-get-field res 'msg)))
2984 (let ((memory (bindat-get-field res 'memory)))
2985 (setq gdb-memory-address (bindat-get-field res 'addr))
2986 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2987 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2988 (setq gdb-memory-last-address gdb-memory-address)
2989 (dolist (row memory)
2990 (insert (concat (bindat-get-field row 'addr) ":"))
2991 (dolist (column (bindat-get-field row 'data))
2992 (insert (gdb-pad-string column
2993 (+ 2 (gdb-memory-column-width
2995 gdb-memory-format)))))
2997 ;; Show last page instead of empty buffer when out of bounds
2999 (let ((gdb-memory-address gdb-memory-last-address))
3000 (gdb-invalidate-memory 'update)
3001 (error err-msg))))))
3003 (defvar gdb-memory-mode-map
3004 (let ((map (make-sparse-keymap)))
3005 (suppress-keymap map t)
3006 (define-key map "q" 'kill-this-buffer)
3007 (define-key map "n" 'gdb-memory-show-next-page)
3008 (define-key map "p" 'gdb-memory-show-previous-page)
3009 (define-key map "a" 'gdb-memory-set-address)
3010 (define-key map "t" 'gdb-memory-format-binary)
3011 (define-key map "o" 'gdb-memory-format-octal)
3012 (define-key map "u" 'gdb-memory-format-unsigned)
3013 (define-key map "d" 'gdb-memory-format-signed)
3014 (define-key map "x" 'gdb-memory-format-hexadecimal)
3015 (define-key map "b" 'gdb-memory-unit-byte)
3016 (define-key map "h" 'gdb-memory-unit-halfword)
3017 (define-key map "w" 'gdb-memory-unit-word)
3018 (define-key map "g" 'gdb-memory-unit-giant)
3019 (define-key map "R" 'gdb-memory-set-rows)
3020 (define-key map "C" 'gdb-memory-set-columns)
3023 (defun gdb-memory-set-address-event (event)
3024 "Handle a click on address field in memory buffer header."
3026 (save-selected-window
3027 (select-window (posn-window (event-start event)))
3028 (gdb-memory-set-address)))
3030 ;; Non-event version for use within keymap
3031 (defun gdb-memory-set-address ()
3032 "Set the start memory address."
3034 (let ((arg (read-from-minibuffer "Memory address: ")))
3035 (setq gdb-memory-address arg))
3036 (gdb-invalidate-memory 'update))
3038 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3039 "Define a function NAME which reads new VAR value from minibuffer."
3040 `(defun ,name (event)
3043 (save-selected-window
3044 (select-window (posn-window (event-start event)))
3045 (let* ((arg (read-from-minibuffer ,echo-string))
3046 (count (string-to-number arg)))
3048 (error "Positive number only")
3049 (customize-set-variable ',variable count)
3050 (gdb-invalidate-memory 'update))))))
3052 (def-gdb-set-positive-number
3056 "Set the number of data rows in memory window.")
3058 (def-gdb-set-positive-number
3059 gdb-memory-set-columns
3062 "Set the number of data columns in memory window.")
3064 (defmacro def-gdb-memory-format (name format doc)
3065 "Define a function NAME to switch memory buffer to use FORMAT.
3067 DOC is an optional documentation string."
3068 `(defun ,name () ,(when doc doc)
3070 (customize-set-variable 'gdb-memory-format ,format)
3071 (gdb-invalidate-memory 'update)))
3073 (def-gdb-memory-format
3074 gdb-memory-format-binary "t"
3075 "Set the display format to binary.")
3077 (def-gdb-memory-format
3078 gdb-memory-format-octal "o"
3079 "Set the display format to octal.")
3081 (def-gdb-memory-format
3082 gdb-memory-format-unsigned "u"
3083 "Set the display format to unsigned decimal.")
3085 (def-gdb-memory-format
3086 gdb-memory-format-signed "d"
3087 "Set the display format to decimal.")
3089 (def-gdb-memory-format
3090 gdb-memory-format-hexadecimal "x"
3091 "Set the display format to hexadecimal.")
3093 (defvar gdb-memory-format-map
3094 (let ((map (make-sparse-keymap)))
3095 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3097 "Keymap to select format in the header line.")
3099 (defvar gdb-memory-format-menu
3100 (let ((map (make-sparse-keymap "Format")))
3102 (define-key map [binary]
3103 '(menu-item "Binary" gdb-memory-format-binary
3104 :button (:radio . (equal gdb-memory-format "t"))))
3105 (define-key map [octal]
3106 '(menu-item "Octal" gdb-memory-format-octal
3107 :button (:radio . (equal gdb-memory-format "o"))))
3108 (define-key map [unsigned]
3109 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3110 :button (:radio . (equal gdb-memory-format "u"))))
3111 (define-key map [signed]
3112 '(menu-item "Signed Decimal" gdb-memory-format-signed
3113 :button (:radio . (equal gdb-memory-format "d"))))
3114 (define-key map [hexadecimal]
3115 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3116 :button (:radio . (equal gdb-memory-format "x"))))
3118 "Menu of display formats in the header line.")
3120 (defun gdb-memory-format-menu (event)
3122 (x-popup-menu event gdb-memory-format-menu))
3124 (defun gdb-memory-format-menu-1 (event)
3126 (save-selected-window
3127 (select-window (posn-window (event-start event)))
3128 (let* ((selection (gdb-memory-format-menu event))
3129 (binding (and selection (lookup-key gdb-memory-format-menu
3130 (vector (car selection))))))
3131 (if binding (call-interactively binding)))))
3133 (defmacro def-gdb-memory-unit (name unit-size doc)
3134 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3136 DOC is an optional documentation string."
3137 `(defun ,name () ,(when doc doc)
3139 (customize-set-variable 'gdb-memory-unit ,unit-size)
3140 (gdb-invalidate-memory 'update)))
3142 (def-gdb-memory-unit gdb-memory-unit-giant 8
3143 "Set the unit size to giant words (eight bytes).")
3145 (def-gdb-memory-unit gdb-memory-unit-word 4
3146 "Set the unit size to words (four bytes).")
3148 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3149 "Set the unit size to halfwords (two bytes).")
3151 (def-gdb-memory-unit gdb-memory-unit-byte 1
3152 "Set the unit size to bytes.")
3154 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3155 "Define a function NAME which show new address in memory buffer.
3157 The defined function switches Memory buffer to show address
3158 stored in ADDRESS-VAR variable.
3160 DOC is an optional documentation string."
3164 (let ((gdb-memory-address ,address-var))
3165 (gdb-invalidate-memory))))
3167 (def-gdb-memory-show-page gdb-memory-show-previous-page
3168 gdb-memory-prev-page)
3170 (def-gdb-memory-show-page gdb-memory-show-next-page
3171 gdb-memory-next-page)
3173 (defvar gdb-memory-unit-map
3174 (let ((map (make-sparse-keymap)))
3175 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3177 "Keymap to select units in the header line.")
3179 (defvar gdb-memory-unit-menu
3180 (let ((map (make-sparse-keymap "Unit")))
3181 (define-key map [giantwords]
3182 '(menu-item "Giant words" gdb-memory-unit-giant
3183 :button (:radio . (equal gdb-memory-unit 8))))
3184 (define-key map [words]
3185 '(menu-item "Words" gdb-memory-unit-word
3186 :button (:radio . (equal gdb-memory-unit 4))))
3187 (define-key map [halfwords]
3188 '(menu-item "Halfwords" gdb-memory-unit-halfword
3189 :button (:radio . (equal gdb-memory-unit 2))))
3190 (define-key map [bytes]
3191 '(menu-item "Bytes" gdb-memory-unit-byte
3192 :button (:radio . (equal gdb-memory-unit 1))))
3194 "Menu of units in the header line.")
3196 (defun gdb-memory-unit-menu (event)
3198 (x-popup-menu event gdb-memory-unit-menu))
3200 (defun gdb-memory-unit-menu-1 (event)
3202 (save-selected-window
3203 (select-window (posn-window (event-start event)))
3204 (let* ((selection (gdb-memory-unit-menu event))
3205 (binding (and selection (lookup-key gdb-memory-unit-menu
3206 (vector (car selection))))))
3207 (if binding (call-interactively binding)))))
3209 (defvar gdb-memory-font-lock-keywords
3210 '(;; <__function.name+n>
3211 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3212 (1 font-lock-function-name-face)))
3213 "Font lock keywords used in `gdb-memory-mode'.")
3215 (defvar gdb-memory-header
3220 'face font-lock-warning-face
3221 'help-echo "mouse-1: decrement address"
3222 'mouse-face 'mode-line-highlight
3223 'local-map (gdb-make-header-line-mouse-map
3225 #'gdb-memory-show-previous-page))
3228 'face font-lock-warning-face
3229 'help-echo "mouse-1: increment address"
3230 'mouse-face 'mode-line-highlight
3231 'local-map (gdb-make-header-line-mouse-map
3233 #'gdb-memory-show-next-page))
3235 (propertize gdb-memory-address
3236 'face font-lock-warning-face
3237 'help-echo "mouse-1: set start address"
3238 'mouse-face 'mode-line-highlight
3239 'local-map (gdb-make-header-line-mouse-map
3241 #'gdb-memory-set-address-event))
3243 (propertize (number-to-string gdb-memory-rows)
3244 'face font-lock-warning-face
3245 'help-echo "mouse-1: set number of columns"
3246 'mouse-face 'mode-line-highlight
3247 'local-map (gdb-make-header-line-mouse-map
3249 #'gdb-memory-set-rows))
3251 (propertize (number-to-string gdb-memory-columns)
3252 'face font-lock-warning-face
3253 'help-echo "mouse-1: set number of columns"
3254 'mouse-face 'mode-line-highlight
3255 'local-map (gdb-make-header-line-mouse-map
3257 #'gdb-memory-set-columns))
3259 (propertize gdb-memory-format
3260 'face font-lock-warning-face
3261 'help-echo "mouse-3: select display format"
3262 'mouse-face 'mode-line-highlight
3263 'local-map gdb-memory-format-map)
3265 (propertize (number-to-string gdb-memory-unit)
3266 'face font-lock-warning-face
3267 'help-echo "mouse-3: select unit size"
3268 'mouse-face 'mode-line-highlight
3269 'local-map gdb-memory-unit-map)))
3270 "Header line used in `gdb-memory-mode'.")
3272 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3273 "Major mode for examining memory."
3274 (setq header-line-format gdb-memory-header)
3275 (set (make-local-variable 'font-lock-defaults)
3276 '(gdb-memory-font-lock-keywords))
3277 'gdb-invalidate-memory)
3279 (defun gdb-memory-buffer-name ()
3280 (concat "*memory of " (gdb-get-target-string) "*"))
3282 (def-gdb-display-buffer
3283 gdb-display-memory-buffer
3285 "Display memory contents.")
3287 (defun gdb-frame-memory-buffer ()
3288 "Display memory contents in a new frame."
3290 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3291 (special-display-frame-alist
3295 ,@gdb-frame-parameters)))
3296 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3299 ;;; Disassembly view
3301 (defun gdb-disassembly-buffer-name ()
3302 (gdb-current-context-buffer-name
3303 (concat "disassembly of " (gdb-get-target-string))))
3305 (def-gdb-display-buffer
3306 gdb-display-disassembly-buffer
3307 'gdb-disassembly-buffer
3308 "Display disassembly for current stack frame.")
3310 (def-gdb-preempt-display-buffer
3311 gdb-preemptively-display-disassembly-buffer
3312 'gdb-disassembly-buffer)
3314 (def-gdb-frame-for-buffer
3315 gdb-frame-disassembly-buffer
3316 'gdb-disassembly-buffer
3317 "Display disassembly in a new frame.")
3319 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3320 (let* ((frame (gdb-current-buffer-frame))
3321 (file (bindat-get-field frame 'fullname))
3322 (line (bindat-get-field frame 'line)))
3324 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3325 ;; If we're unable to get a file name / line for $PC, simply
3326 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3327 ;; 150 bytes) instructions.
3328 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3329 gdb-disassembly-handler
3330 ;; We update disassembly only after we have actual frame information
3331 ;; about all threads, so no there's `update' signal in this list
3332 '(start update-disassembly))
3334 (def-gdb-auto-update-handler
3335 gdb-disassembly-handler
3336 gdb-invalidate-disassembly
3337 gdb-disassembly-handler-custom
3340 (gdb-set-buffer-rules
3341 'gdb-disassembly-buffer
3342 'gdb-disassembly-buffer-name
3343 'gdb-disassembly-mode
3344 'gdb-invalidate-disassembly)
3346 (defvar gdb-disassembly-font-lock-keywords
3347 '(;; <__function.name+n>
3348 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3349 (1 font-lock-function-name-face))
3350 ;; 0xNNNNNNNN <__function.name+n>: opcode
3351 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3352 (4 font-lock-keyword-face))
3353 ;; %register(at least i386)
3354 ("%\\sw+" . font-lock-variable-name-face)
3355 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3356 (1 font-lock-comment-face)
3357 (2 font-lock-function-name-face))
3358 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3359 "Font lock keywords used in `gdb-disassembly-mode'.")
3361 (defvar gdb-disassembly-mode-map
3363 (let ((map (make-sparse-keymap)))
3364 (suppress-keymap map)
3365 (define-key map "q" 'kill-this-buffer)
3368 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3369 "Major mode for GDB disassembly information."
3370 ;; TODO Rename overlay variable for disassembly mode
3371 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3372 (setq fringes-outside-margins t)
3373 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3374 (set (make-local-variable 'font-lock-defaults)
3375 '(gdb-disassembly-font-lock-keywords))
3376 'gdb-invalidate-disassembly)
3378 (defun gdb-disassembly-handler-custom ()
3379 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3380 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3381 (table (make-gdb-table))
3383 (dolist (instr instructions)
3384 (gdb-table-add-row table
3386 (bindat-get-field instr 'address)
3388 ((func-name (bindat-get-field instr 'func-name))
3389 (offset (bindat-get-field instr 'offset)))
3391 (format "<%s+%s>:" func-name offset)
3393 (bindat-get-field instr 'inst)))
3394 (when (string-equal (bindat-get-field instr 'address)
3397 (setq marked-line (length (gdb-table-rows table)))
3398 (setq fringe-indicator-alist
3399 (if (string-equal gdb-frame-number "0")
3401 '((overlay-arrow . hollow-right-triangle)))))))
3402 (insert (gdb-table-string table " "))
3403 (gdb-disassembly-place-breakpoints)
3404 ;; Mark current position with overlay arrow and scroll window to
3407 (let ((window (get-buffer-window (current-buffer) 0)))
3408 (set-window-point window (gdb-mark-line marked-line
3409 gdb-disassembly-position))))
3411 (gdb-current-context-mode-name
3412 (concat "Disassembly: "
3413 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3415 (defun gdb-disassembly-place-breakpoints ()
3416 (gdb-remove-breakpoint-icons (point-min) (point-max))
3417 (dolist (breakpoint gdb-breakpoints-list)
3418 (let* ((breakpoint (cdr breakpoint))
3419 (bptno (bindat-get-field breakpoint 'number))
3420 (flag (bindat-get-field breakpoint 'enabled))
3421 (address (bindat-get-field breakpoint 'addr)))
3423 (goto-char (point-min))
3424 (if (re-search-forward (concat "^" address) nil t)
3425 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3428 (defvar gdb-breakpoints-header
3430 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3433 (gdb-propertize-header "Threads" gdb-threads-buffer
3434 "mouse-1: select" mode-line-highlight
3435 mode-line-inactive)))
3437 ;;; Breakpoints view
3438 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3439 "Major mode for gdb breakpoints."
3440 (setq header-line-format gdb-breakpoints-header)
3441 'gdb-invalidate-breakpoints)
3443 (defun gdb-toggle-breakpoint ()
3444 "Enable/disable breakpoint at current line of breakpoints buffer."
3448 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3451 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3454 (bindat-get-field breakpoint 'number)))
3455 (error "Not recognized as break/watchpoint line")))))
3457 (defun gdb-delete-breakpoint ()
3458 "Delete the breakpoint at current line of breakpoints buffer."
3462 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3464 (gud-basic-call (concat "-break-delete "
3465 (bindat-get-field breakpoint 'number)))
3466 (error "Not recognized as break/watchpoint line")))))
3468 (defun gdb-goto-breakpoint (&optional event)
3469 "Go to the location of breakpoint at current line of
3470 breakpoints buffer."
3471 (interactive (list last-input-event))
3472 (if event (posn-set-point (event-end event)))
3473 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3474 (let ((window (get-buffer-window gud-comint-buffer)))
3475 (if window (save-selected-window (select-window window))))
3478 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3480 (let ((bptno (bindat-get-field breakpoint 'number))
3481 (file (bindat-get-field breakpoint 'fullname))
3482 (line (bindat-get-field breakpoint 'line)))
3483 (save-selected-window
3484 (let* ((buffer (find-file-noselect
3485 (if (file-exists-p file) file
3486 (cdr (assoc bptno gdb-location-alist)))))
3487 (window (or (gdb-display-source-buffer buffer)
3488 (display-buffer buffer))))
3489 (setq gdb-source-window window)
3490 (with-current-buffer buffer
3491 (goto-char (point-min))
3492 (forward-line (1- (string-to-number line)))
3493 (set-window-point window (point))))))
3494 (error "Not recognized as break/watchpoint line")))))
3497 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3499 (def-gdb-trigger-and-handler
3500 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3501 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3504 (gdb-set-buffer-rules
3506 'gdb-stack-buffer-name
3508 'gdb-invalidate-frames)
3510 (defun gdb-frame-location (frame)
3511 "Return \" of file:line\" or \" of library\" for structure FRAME.
3513 FRAME must have either \"file\" and \"line\" members or \"from\"
3515 (let ((file (bindat-get-field frame 'file))
3516 (line (bindat-get-field frame 'line))
3517 (from (bindat-get-field frame 'from)))
3518 (let ((res (or (and file line (concat file ":" line))
3520 (if res (concat " of " res) ""))))
3522 (defun gdb-stack-list-frames-custom ()
3523 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3524 (table (make-gdb-table)))
3525 (set-marker gdb-stack-position nil)
3526 (dolist (frame stack)
3527 (gdb-table-add-row table
3529 (bindat-get-field frame 'level)
3532 (bindat-get-field frame 'func)
3533 (if gdb-stack-buffer-locations
3534 (gdb-frame-location frame) "")
3535 (if gdb-stack-buffer-addresses
3536 (concat " at " (bindat-get-field frame 'addr)) "")))
3537 `(mouse-face highlight
3538 help-echo "mouse-2, RET: Select frame"
3540 (insert (gdb-table-string table " ")))
3541 (when (and gdb-frame-number
3542 (gdb-buffer-shows-main-thread-p))
3543 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3544 gdb-stack-position))
3546 (gdb-current-context-mode-name "Frames")))
3548 (defun gdb-stack-buffer-name ()
3549 (gdb-current-context-buffer-name
3550 (concat "stack frames of " (gdb-get-target-string))))
3552 (def-gdb-display-buffer
3553 gdb-display-stack-buffer
3555 "Display backtrace of current stack.")
3557 (def-gdb-preempt-display-buffer
3558 gdb-preemptively-display-stack-buffer
3559 'gdb-stack-buffer nil t)
3561 (def-gdb-frame-for-buffer
3562 gdb-frame-stack-buffer
3564 "Display backtrace of current stack in a new frame.")
3566 (defvar gdb-frames-mode-map
3567 (let ((map (make-sparse-keymap)))
3568 (suppress-keymap map)
3569 (define-key map "q" 'kill-this-buffer)
3570 (define-key map "\r" 'gdb-select-frame)
3571 (define-key map [mouse-2] 'gdb-select-frame)
3572 (define-key map [follow-link] 'mouse-face)
3575 (defvar gdb-frames-font-lock-keywords
3576 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3577 "Font lock keywords used in `gdb-frames-mode'.")
3579 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3580 "Major mode for gdb call stack."
3581 (setq gdb-stack-position (make-marker))
3582 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3583 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3584 (set (make-local-variable 'font-lock-defaults)
3585 '(gdb-frames-font-lock-keywords))
3586 'gdb-invalidate-frames)
3588 (defun gdb-select-frame (&optional event)
3589 "Select the frame and display the relevant source."
3590 (interactive (list last-input-event))
3591 (if event (posn-set-point (event-end event)))
3592 (let ((frame (get-text-property (point) 'gdb-frame)))
3594 (if (gdb-buffer-shows-main-thread-p)
3595 (let ((new-level (bindat-get-field frame 'level)))
3596 (setq gdb-frame-number new-level)
3597 (gdb-input (concat "-stack-select-frame " new-level)
3600 (error "Could not select frame for non-current thread"))
3601 (error "Not recognized as frame line"))))
3605 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3606 (def-gdb-trigger-and-handler
3607 gdb-invalidate-locals
3608 (concat (gdb-current-context-command "-stack-list-locals")
3610 gdb-locals-handler gdb-locals-handler-custom
3613 (gdb-set-buffer-rules
3615 'gdb-locals-buffer-name
3617 'gdb-invalidate-locals)
3619 (defvar gdb-locals-watch-map
3620 (let ((map (make-sparse-keymap)))
3621 (suppress-keymap map)
3622 (define-key map "\r" 'gud-watch)
3623 (define-key map [mouse-2] 'gud-watch)
3625 "Keymap to create watch expression of a complex data type local variable.")
3627 (defvar gdb-edit-locals-map-1
3628 (let ((map (make-sparse-keymap)))
3629 (suppress-keymap map)
3630 (define-key map "\r" 'gdb-edit-locals-value)
3631 (define-key map [mouse-2] 'gdb-edit-locals-value)
3633 "Keymap to edit value of a simple data type local variable.")
3635 (defun gdb-edit-locals-value (&optional event)
3636 "Assign a value to a variable displayed in the locals buffer."
3637 (interactive (list last-input-event))
3639 (if event (posn-set-point (event-end event)))
3641 (let* ((var (bindat-get-field
3642 (get-text-property (point) 'gdb-local-variable) 'name))
3643 (value (read-string (format "New value (%s): " var))))
3645 (concat "-gdb-set variable " var " = " value)))))
3647 ;; Don't display values of arrays or structures.
3648 ;; These can be expanded using gud-watch.
3649 (defun gdb-locals-handler-custom ()
3650 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3651 (table (make-gdb-table)))
3652 (dolist (local locals-list)
3653 (let ((name (bindat-get-field local 'name))
3654 (value (bindat-get-field local 'value))
3655 (type (bindat-get-field local 'type)))
3657 (string-match "\\0x" value))
3658 (add-text-properties 0 (length name)
3659 `(mouse-face highlight
3660 help-echo "mouse-2: create watch expression"
3661 local-map ,gdb-locals-watch-map)
3663 (add-text-properties 0 (length value)
3664 `(mouse-face highlight
3665 help-echo "mouse-2: edit value"
3666 local-map ,gdb-edit-locals-map-1)
3671 (propertize type 'font-lock-face font-lock-type-face)
3672 (propertize name 'font-lock-face font-lock-variable-name-face)
3674 `(gdb-local-variable ,local))))
3675 (insert (gdb-table-string table " "))
3677 (gdb-current-context-mode-name
3679 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3681 (defvar gdb-locals-header
3683 (gdb-propertize-header "Locals" gdb-locals-buffer
3686 (gdb-propertize-header "Registers" gdb-registers-buffer
3687 "mouse-1: select" mode-line-highlight
3688 mode-line-inactive)))
3690 (defvar gdb-locals-mode-map
3691 (let ((map (make-sparse-keymap)))
3692 (suppress-keymap map)
3693 (define-key map "q" 'kill-this-buffer)
3694 (define-key map "\t" (lambda ()
3696 (gdb-set-window-buffer
3697 (gdb-get-buffer-create
3698 'gdb-registers-buffer
3699 gdb-thread-number) t)))
3702 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3703 "Major mode for gdb locals."
3704 (setq header-line-format gdb-locals-header)
3705 'gdb-invalidate-locals)
3707 (defun gdb-locals-buffer-name ()
3708 (gdb-current-context-buffer-name
3709 (concat "locals of " (gdb-get-target-string))))
3711 (def-gdb-display-buffer
3712 gdb-display-locals-buffer
3714 "Display local variables of current stack and their values.")
3716 (def-gdb-preempt-display-buffer
3717 gdb-preemptively-display-locals-buffer
3718 'gdb-locals-buffer nil t)
3720 (def-gdb-frame-for-buffer
3721 gdb-frame-locals-buffer
3723 "Display local variables of current stack and their values in a new frame.")
3726 ;; Registers buffer.
3728 (def-gdb-trigger-and-handler
3729 gdb-invalidate-registers
3730 (concat (gdb-current-context-command "-data-list-register-values") " x")
3731 gdb-registers-handler
3732 gdb-registers-handler-custom
3735 (gdb-set-buffer-rules
3736 'gdb-registers-buffer
3737 'gdb-registers-buffer-name
3739 'gdb-invalidate-registers)
3741 (defun gdb-registers-handler-custom ()
3742 (when gdb-register-names
3743 (let ((register-values
3744 (bindat-get-field (gdb-json-partial-output) 'register-values))
3745 (table (make-gdb-table)))
3746 (dolist (register register-values)
3747 (let* ((register-number (bindat-get-field register 'number))
3748 (value (bindat-get-field register 'value))
3749 (register-name (nth (string-to-number register-number)
3750 gdb-register-names)))
3754 (propertize register-name
3755 'font-lock-face font-lock-variable-name-face)
3756 (if (member register-number gdb-changed-registers)
3757 (propertize value 'font-lock-face font-lock-warning-face)
3759 `(mouse-face highlight
3760 help-echo "mouse-2: edit value"
3761 gdb-register-name ,register-name))))
3762 (insert (gdb-table-string table " ")))
3764 (gdb-current-context-mode-name "Registers"))))
3766 (defun gdb-edit-register-value (&optional event)
3767 "Assign a value to a register displayed in the registers buffer."
3768 (interactive (list last-input-event))
3770 (if event (posn-set-point (event-end event)))
3772 (let* ((var (bindat-get-field
3773 (get-text-property (point) 'gdb-register-name)))
3774 (value (read-string (format "New value (%s): " var))))
3776 (concat "-gdb-set variable $" var " = " value)))))
3778 (defvar gdb-registers-mode-map
3779 (let ((map (make-sparse-keymap)))
3780 (suppress-keymap map)
3781 (define-key map "\r" 'gdb-edit-register-value)
3782 (define-key map [mouse-2] 'gdb-edit-register-value)
3783 (define-key map "q" 'kill-this-buffer)
3784 (define-key map "\t" (lambda ()
3786 (gdb-set-window-buffer
3787 (gdb-get-buffer-create
3789 gdb-thread-number) t)))
3792 (defvar gdb-registers-header
3794 (gdb-propertize-header "Locals" gdb-locals-buffer
3795 "mouse-1: select" mode-line-highlight
3798 (gdb-propertize-header "Registers" gdb-registers-buffer
3799 nil nil mode-line)))
3801 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3802 "Major mode for gdb registers."
3803 (setq header-line-format gdb-registers-header)
3804 'gdb-invalidate-registers)
3806 (defun gdb-registers-buffer-name ()
3807 (gdb-current-context-buffer-name
3808 (concat "registers of " (gdb-get-target-string))))
3810 (def-gdb-display-buffer
3811 gdb-display-registers-buffer
3812 'gdb-registers-buffer
3813 "Display integer register contents.")
3815 (def-gdb-preempt-display-buffer
3816 gdb-preemptively-display-registers-buffer
3817 'gdb-registers-buffer nil t)
3819 (def-gdb-frame-for-buffer
3820 gdb-frame-registers-buffer
3821 'gdb-registers-buffer
3822 "Display integer register contents in a new frame.")
3824 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3825 (defun gdb-get-changed-registers ()
3826 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3827 (not (gdb-pending-p 'gdb-get-changed-registers)))
3828 (gdb-input "-data-list-changed-registers"
3829 'gdb-changed-registers-handler)
3830 (gdb-add-pending 'gdb-get-changed-registers)))
3832 (defun gdb-changed-registers-handler ()
3833 (gdb-delete-pending 'gdb-get-changed-registers)
3834 (setq gdb-changed-registers nil)
3835 (dolist (register-number
3836 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3837 (push register-number gdb-changed-registers)))
3839 (defun gdb-register-names-handler ()
3840 ;; Don't use gdb-pending-triggers because this handler is called
3841 ;; only once (in gdb-init-1)
3842 (setq gdb-register-names nil)
3843 (dolist (register-name
3844 (bindat-get-field (gdb-json-partial-output) 'register-names))
3845 (push register-name gdb-register-names))
3846 (setq gdb-register-names (reverse gdb-register-names)))
3849 (defun gdb-get-source-file-list ()
3850 "Create list of source files for current GDB session.
3851 If buffers already exist for any of these files, gud-minor-mode
3853 (goto-char (point-min))
3854 (while (re-search-forward gdb-source-file-regexp nil t)
3855 (push (match-string 1) gdb-source-file-list))
3856 (dolist (buffer (buffer-list))
3857 (with-current-buffer buffer
3858 (when (member buffer-file-name gdb-source-file-list)
3859 (gdb-init-buffer)))))
3861 (defun gdb-get-main-selected-frame ()
3862 "Trigger for `gdb-frame-handler' which uses main current
3863 thread. Called from `gdb-update'."
3864 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3866 (gdb-input (gdb-current-context-command "-stack-info-frame")
3868 (gdb-add-pending 'gdb-get-main-selected-frame))))
3870 (defun gdb-frame-handler ()
3871 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3872 overlay arrow in source buffer."
3873 (gdb-delete-pending 'gdb-get-main-selected-frame)
3874 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3876 (setq gdb-selected-frame (bindat-get-field frame 'func))
3877 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3878 (setq gdb-frame-number (bindat-get-field frame 'level))
3879 (setq gdb-frame-address (bindat-get-field frame 'addr))
3880 (let ((line (bindat-get-field frame 'line)))
3881 (setq gdb-selected-line (and line (string-to-number line)))
3882 (when (and gdb-selected-file gdb-selected-line)
3883 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3884 (gud-display-frame)))
3885 (if gud-overlay-arrow-position
3886 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3887 (position (marker-position gud-overlay-arrow-position)))
3889 (with-current-buffer buffer
3890 (setq fringe-indicator-alist
3891 (if (string-equal gdb-frame-number "0")
3893 '((overlay-arrow . hollow-right-triangle))))
3894 (setq gud-overlay-arrow-position (make-marker))
3895 (set-marker gud-overlay-arrow-position position))))))))
3897 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3899 (defun gdb-get-prompt ()
3900 "Find prompt for GDB session."
3901 (goto-char (point-min))
3902 (setq gdb-prompt-name nil)
3903 (re-search-forward gdb-prompt-name-regexp nil t)
3904 (setq gdb-prompt-name (match-string 1))
3905 ;; Insert first prompt.
3906 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3908 ;;;; Window management
3909 (defun gdb-display-buffer (buf dedicated &optional frame)
3912 If BUF is already displayed in some window, show it, deiconifying
3913 the frame if necessary. Otherwise, find least recently used
3914 window and show BUF there, if the window is not used for GDB
3915 already, in which case that window is split first."
3916 (let ((answer (get-buffer-window buf (or frame 0))))
3918 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3919 (let ((window (get-lru-window)))
3920 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3922 (let ((largest (get-largest-window)))
3923 (setq answer (split-window largest))
3924 (set-window-buffer answer buf)
3925 (set-window-dedicated-p answer dedicated)
3927 (set-window-buffer window buf)
3930 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3931 "Find window displaying a buffer with the same
3932 `gdb-buffer-type' as BUF and show BUF there. If no such window
3933 exists, just call `gdb-display-buffer' for BUF. If the window
3934 found is already dedicated, split window according to
3935 SPLIT-HORIZONTAL and show BUF in the new window."
3937 (when (not (get-buffer-window buf))
3938 (let* ((buf-type (gdb-buffer-type buf))
3940 (get-window-with-predicate
3943 (gdb-buffer-type (window-buffer w)))
3944 (not (window-dedicated-p w)))))))
3946 (set-window-buffer existing-window buf)
3947 (let ((dedicated-window
3948 (get-window-with-predicate
3951 (gdb-buffer-type (window-buffer w)))))))
3952 (if dedicated-window
3954 (split-window dedicated-window nil split-horizontal) buf)
3955 (gdb-display-buffer buf t))))))
3956 (error "Null buffer")))
3958 ;;; Shared keymap initialization:
3960 (let ((menu (make-sparse-keymap "GDB-Windows")))
3961 (define-key gud-menu-map [displays]
3962 `(menu-item "GDB-Windows" ,menu
3963 :visible (eq gud-minor-mode 'gdbmi)))
3964 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3965 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3966 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3967 (define-key menu [disassembly]
3968 '("Disassembly" . gdb-display-disassembly-buffer))
3969 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3970 (define-key menu [inferior]
3971 '("IO" . gdb-display-io-buffer))
3972 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3973 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3974 (define-key menu [breakpoints]
3975 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3977 (let ((menu (make-sparse-keymap "GDB-Frames")))
3978 (define-key gud-menu-map [frames]
3979 `(menu-item "GDB-Frames" ,menu
3980 :visible (eq gud-minor-mode 'gdbmi)))
3981 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3982 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3983 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3984 (define-key menu [disassembly]
3985 '("Disassembly" . gdb-frame-disassembly-buffer))
3986 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3987 (define-key menu [inferior]
3988 '("IO" . gdb-frame-io-buffer))
3989 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3990 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3991 (define-key menu [breakpoints]
3992 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3994 (let ((menu (make-sparse-keymap "GDB-MI")))
3995 (define-key menu [gdb-customize]
3996 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3997 :help "Customize Gdb Graphical Mode options."))
3998 (define-key menu [gdb-many-windows]
3999 '(menu-item "Display Other Windows" gdb-many-windows
4000 :help "Toggle display of locals, stack and breakpoint information"
4001 :button (:toggle . gdb-many-windows)))
4002 (define-key menu [gdb-restore-windows]
4003 '(menu-item "Restore Window Layout" gdb-restore-windows
4004 :help "Restore standard layout for debug session."))
4005 (define-key menu [sep1]
4007 (define-key menu [all-threads]
4008 '(menu-item "GUD controls all threads"
4011 (setq gdb-gud-control-all-threads t))
4012 :help "GUD start/stop commands apply to all threads"
4013 :button (:radio . gdb-gud-control-all-threads)))
4014 (define-key menu [current-thread]
4015 '(menu-item "GUD controls current thread"
4018 (setq gdb-gud-control-all-threads nil))
4019 :help "GUD start/stop commands apply to current thread only"
4020 :button (:radio . (not gdb-gud-control-all-threads))))
4021 (define-key menu [sep2]
4023 (define-key menu [gdb-customize-reasons]
4024 '(menu-item "Customize switching..."
4027 (customize-option 'gdb-switch-reasons))))
4028 (define-key menu [gdb-switch-when-another-stopped]
4029 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4030 gdb-switch-when-another-stopped
4031 "Automatically switch to stopped thread"
4032 "GDB thread switching %s"
4033 "Switch to stopped thread"))
4034 (define-key gud-menu-map [mi]
4035 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4037 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4038 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4039 ;; tool-bar-local-item-from-menu here because it appends new buttons
4040 ;; to toolbar from right to left while we want our A/T throttle to
4041 ;; show up right before Run button.
4042 (define-key-after gud-tool-bar-map [all-threads]
4043 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4044 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4045 :visible (and (eq gud-minor-mode 'gdbmi)
4047 (not gdb-gud-control-all-threads)))
4050 (define-key-after gud-tool-bar-map [current-thread]
4051 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4052 :image (find-image '((:type xpm :file "gud/all.xpm")))
4053 :visible (and (eq gud-minor-mode 'gdbmi)
4055 gdb-gud-control-all-threads))
4058 (defun gdb-frame-gdb-buffer ()
4059 "Display GUD buffer in a new frame."
4061 (display-buffer-other-frame gud-comint-buffer))
4063 (defun gdb-display-gdb-buffer ()
4064 "Display GUD buffer."
4066 (pop-to-buffer gud-comint-buffer nil 0))
4068 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4069 "Set buffer of selected window to NAME and dedicate window.
4071 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4072 window is dedicated."
4073 (unless window (setq window (selected-window)))
4074 (when ignore-dedicated
4075 (set-window-dedicated-p window nil))
4076 (set-window-buffer window (get-buffer name))
4077 (set-window-dedicated-p window t))
4079 (defun gdb-setup-windows ()
4080 "Layout the window pattern for `gdb-many-windows'."
4081 (gdb-display-locals-buffer)
4082 (gdb-display-stack-buffer)
4083 (delete-other-windows)
4084 (gdb-display-breakpoints-buffer)
4085 (delete-other-windows)
4087 (switch-to-buffer gud-comint-buffer)
4088 (let ((win0 (selected-window))
4089 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4090 (win2 (split-window nil ( / (window-height) 3)))
4091 (win3 (split-window-right)))
4092 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4093 (select-window win2)
4096 (if gud-last-last-frame
4097 (gud-find-file (car gud-last-last-frame))
4099 (gud-find-file gdb-main-file)
4100 ;; Put buffer list in window if we
4101 ;; can't find a source file.
4102 (list-buffers-noselect))))
4103 (setq gdb-source-window (selected-window))
4104 (let ((win4 (split-window-right)))
4105 (gdb-set-window-buffer
4106 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4107 (select-window win1)
4108 (gdb-set-window-buffer (gdb-stack-buffer-name))
4109 (let ((win5 (split-window-right)))
4110 (gdb-set-window-buffer (if gdb-show-threads-by-default
4111 (gdb-threads-buffer-name)
4112 (gdb-breakpoints-buffer-name))
4114 (select-window win0)))
4116 (define-minor-mode gdb-many-windows
4117 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4118 In this case it starts with two windows: one displaying the GUD
4119 buffer and the other with the source file with the main routine
4120 of the debugged program. Non-nil means display the layout shown for
4125 (if (and gud-comint-buffer
4126 (buffer-name gud-comint-buffer))
4128 (gdb-restore-windows))))
4130 (defun gdb-restore-windows ()
4131 "Restore the basic arrangement of windows used by gdb.
4132 This arrangement depends on the value of `gdb-many-windows'."
4134 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4135 (delete-other-windows)
4136 (if gdb-many-windows
4138 (when (or gud-last-last-frame gdb-show-main)
4139 (let ((win (split-window)))
4142 (if gud-last-last-frame
4143 (gud-find-file (car gud-last-last-frame))
4144 (gud-find-file gdb-main-file)))
4145 (setq gdb-source-window win)))))
4147 ;; Called from `gud-sentinel' in gud.el:
4149 "Exit a debugging session cleanly.
4150 Kills the gdb buffers, and resets variables and the source buffers."
4151 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4152 ;; process. This pty must be deleted explicitly.
4153 (let ((pty (get-process "gdb-inferior")))
4154 (if pty (delete-process pty)))
4155 ;; Find gdb-mi buffers and kill them.
4156 (dolist (buffer (buffer-list))
4157 (unless (eq buffer gud-comint-buffer)
4158 (with-current-buffer buffer
4159 (if (eq gud-minor-mode 'gdbmi)
4160 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4162 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4163 (setq gud-minor-mode nil)
4164 (kill-local-variable 'tool-bar-map)
4165 (kill-local-variable 'gdb-define-alist))))))
4166 (setq gdb-disassembly-position nil)
4167 (setq overlay-arrow-variable-list
4168 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4169 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4170 (setq gdb-stack-position nil)
4171 (setq overlay-arrow-variable-list
4172 (delq 'gdb-stack-position overlay-arrow-variable-list))
4173 (setq gdb-thread-position nil)
4174 (setq overlay-arrow-variable-list
4175 (delq 'gdb-thread-position overlay-arrow-variable-list))
4176 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4177 (setq gud-running nil)
4178 (setq gdb-active-process nil)
4179 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4181 (defun gdb-get-source-file ()
4182 "Find the source file where the program starts and display it with related
4183 buffers, if required."
4184 (goto-char (point-min))
4185 (if (re-search-forward gdb-source-file-regexp nil t)
4186 (setq gdb-main-file (match-string 1)))
4187 (if gdb-many-windows
4189 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4190 (if (and gdb-show-main gdb-main-file)
4191 (let ((pop-up-windows t))
4192 (display-buffer (gud-find-file gdb-main-file)))))
4193 (gdb-force-mode-line-update
4194 (propertize "ready" 'face font-lock-variable-name-face)))
4197 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4198 "Put string PUTSTRING in front of POS in the current buffer.
4199 PUTSTRING is displayed by putting an overlay into the current buffer with a
4200 `before-string' string that has a `display' property whose value is
4202 (let ((string (make-string 1 ?x))
4203 (buffer (current-buffer)))
4204 (setq putstring (copy-sequence putstring))
4205 (let ((overlay (make-overlay pos pos buffer))
4207 (list (list 'margin 'left-margin) putstring))))
4208 (put-text-property 0 1 'display prop string)
4210 (add-text-properties 0 1 sprops string))
4211 (overlay-put overlay 'put-break t)
4212 (overlay-put overlay 'before-string string))))
4214 ;;from remove-images
4215 (defun gdb-remove-strings (start end &optional buffer)
4216 "Remove strings between START and END in BUFFER.
4217 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4218 BUFFER nil or omitted means use the current buffer."
4220 (setq buffer (current-buffer)))
4221 (dolist (overlay (overlays-in start end))
4222 (when (overlay-get overlay 'put-break)
4223 (delete-overlay overlay))))
4225 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4226 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4227 (start (- (car posns) 1))
4228 (end (+ (cdr posns) 1))
4229 (putstring (if enabled "B" "b"))
4230 (source-window (get-buffer-window (current-buffer) 0)))
4231 (add-text-properties
4232 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4235 (add-text-properties
4236 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4237 (add-text-properties
4238 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4239 (gdb-remove-breakpoint-icons start end)
4240 (if (display-images-p)
4241 (if (>= (or left-fringe-width
4242 (if source-window (car (window-fringes source-window)))
4243 gdb-buffer-fringe-width) 8)
4246 `(left-fringe breakpoint
4249 'breakpoint-disabled))
4251 'gdb-enabled enabled)
4252 (when (< left-margin-width 2)
4253 (save-current-buffer
4254 (setq left-margin-width 2)
4258 left-margin-width right-margin-width))))
4261 (or breakpoint-enabled-icon
4262 (setq breakpoint-enabled-icon
4263 (find-image `((:type xpm :data
4264 ,breakpoint-xpm-data
4265 :ascent 100 :pointer hand)
4267 ,breakpoint-enabled-pbm-data
4268 :ascent 100 :pointer hand)))))
4269 (or breakpoint-disabled-icon
4270 (setq breakpoint-disabled-icon
4271 (find-image `((:type xpm :data
4272 ,breakpoint-xpm-data
4273 :conversion disabled
4274 :ascent 100 :pointer hand)
4276 ,breakpoint-disabled-pbm-data
4277 :ascent 100 :pointer hand))))))
4281 (when (< left-margin-width 2)
4282 (save-current-buffer
4283 (setq left-margin-width 2)
4284 (let ((window (get-buffer-window (current-buffer) 0)))
4287 window left-margin-width right-margin-width)))))
4289 (propertize putstring
4291 'breakpoint-enabled 'breakpoint-disabled))
4294 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4295 (gdb-remove-strings start end)
4296 (if (display-images-p)
4297 (remove-images start end))
4299 (setq left-margin-width 0)
4300 (let ((window (get-buffer-window (current-buffer) 0)))
4303 window left-margin-width right-margin-width)))))
4306 ;;; Functions for inline completion.
4308 (defvar gud-gdb-fetch-lines-in-progress)
4309 (defvar gud-gdb-fetch-lines-string)
4310 (defvar gud-gdb-fetch-lines-break)
4311 (defvar gud-gdb-fetched-lines)
4313 (defun gud-gdbmi-completions (context command)
4314 "Completion table for GDB/MI commands.
4315 COMMAND is the prefix for which we seek completion.
4316 CONTEXT is the text before COMMAND on the line."
4317 (let ((gud-gdb-fetch-lines-in-progress t)
4318 (gud-gdb-fetch-lines-string nil)
4319 (gud-gdb-fetch-lines-break (length context))
4320 (gud-gdb-fetched-lines nil)
4321 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4322 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4324 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4325 (gdb-input (concat "complete " context command)
4326 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4327 (while gud-gdb-fetch-lines-in-progress
4328 (accept-process-output (get-buffer-process gud-comint-buffer))))
4329 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4331 (defun gud-gdbmi-fetch-lines-filter (string)
4332 "Custom filter function for `gud-gdbmi-completions'."
4333 (setq string (concat gud-gdb-fetch-lines-string
4334 (gud-gdbmi-marker-filter string)))
4335 (while (string-match "\n" string)
4336 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4337 gud-gdb-fetched-lines)
4338 (setq string (substring string (match-end 0))))
4343 ;;; gdb-mi.el ends here