1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007-2011 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 by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It is currently being 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 is still under
47 ;; development and is part of a process to migrate Emacs from annotations (as
48 ;; used in gdb-ui.el) to GDB/MI. It runs gdb with GDB/MI (-interp=mi) and
49 ;; access CLI using "-interpreter-exec console cli-command". This code works
50 ;; without gdb-ui.el and uses MI tokens instead of queues. Eventually MI
51 ;; should be asynchronous.
53 ;; This mode will PARTLY WORK WITH RECENT GDB RELEASES (status in modeline
54 ;; doesn't update properly when execution commands are issued from GUD buffer)
55 ;; and WORKS BEST when GDB runs asynchronously: maint set linux-async on.
57 ;; You need development version of GDB 7.0 for the thread buffer to work.
59 ;; This file replaces gdb-ui.el and is for development with GDB. Use the
60 ;; release branch of Emacs 22 for the latest version of gdb-ui.el.
64 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
65 ;; explicitly in your program if you want timely display of I/O in Emacs.
66 ;; Alternatively you can make the output stream unbuffered, for example, by
70 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
73 ;; and compiling with -DUNBUFFERED while debugging.
75 ;; If you are using Cygwin GDB and find that the source is not being displayed
76 ;; in Emacs when you step through it, possible solutions are to:
78 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
79 ;; (Since 22.1 Emacs builds under Cygwin.)
80 ;; 2) Use MinGW GDB instead.
81 ;; 3) Use cygwin-mount.el
85 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
86 ;; some changes to the version that they include as part of Mac OSX.
87 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
88 ;; as earlier versions don not compile on Mac OSX.
92 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
93 ;; line information, e.g., a routine in libc (just a TODO item).
96 ;; 2) Watch windows to work with threads.
97 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
98 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
105 (eval-when-compile (require 'cl
))
107 (declare-function speedbar-change-initial-expansion-list
108 "speedbar" (new-default))
109 (declare-function speedbar-timer-fn
"speedbar" ())
110 (declare-function speedbar-line-text
"speedbar" (&optional p
))
111 (declare-function speedbar-change-expand-button-char
"speedbar" (char))
112 (declare-function speedbar-delete-subblock
"speedbar" (indent))
113 (declare-function speedbar-center-buffer-smartly
"speedbar" ())
115 (defvar tool-bar-map
)
116 (defvar speedbar-initial-expansion-list-name
)
117 (defvar speedbar-frame
)
119 (defvar gdb-memory-address
"main")
120 (defvar gdb-memory-last-address nil
121 "Last successfully accessed memory address.")
122 (defvar gdb-memory-next-page nil
123 "Address of next memory page for program memory buffer.")
124 (defvar gdb-memory-prev-page nil
125 "Address of previous memory page for program memory buffer.")
127 (defvar gdb-thread-number nil
128 "Main current thread.
130 Invalidation triggers use this variable to query GDB for
131 information on the specified thread by wrapping GDB/MI commands
132 in `gdb-current-context-command'.
134 This variable may be updated implicitly by GDB via `gdb-stopped'
135 or explicitly by `gdb-select-thread'.
137 Only `gdb-setq-thread-number' should be used to change this
140 (defvar gdb-frame-number nil
141 "Selected frame level for main current thread.
143 Updated according to the following rules:
145 When a thread is selected or current thread stops, set to \"0\".
147 When current thread goes running (and possibly exits eventually),
150 May be manually changed by user with `gdb-select-frame'.")
152 (defvar gdb-frame-address nil
"Identity of frame for watch expression.")
154 ;; Used to show overlay arrow in source buffer. All set in
155 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
156 ;; these but rely on buffer-local thread information instead.
157 (defvar gdb-selected-frame nil
158 "Name of selected function for main current thread.")
159 (defvar gdb-selected-file nil
160 "Name of selected file for main current thread.")
161 (defvar gdb-selected-line nil
162 "Number of selected line for main current thread.")
164 (defvar gdb-threads-list nil
165 "Associative list of threads provided by \"-thread-info\" MI command.
167 Keys are thread numbers (in strings) and values are structures as
168 returned from -thread-info by `gdb-json-partial-output'. Updated in
169 `gdb-thread-list-handler-custom'.")
171 (defvar gdb-running-threads-count nil
172 "Number of currently running threads.
174 If nil, no information is available.
176 Updated in `gdb-thread-list-handler-custom'.")
178 (defvar gdb-stopped-threads-count nil
179 "Number of currently stopped threads.
181 See also `gdb-running-threads-count'.")
183 (defvar gdb-breakpoints-list nil
184 "Associative list of breakpoints provided by \"-break-list\" MI command.
186 Keys are breakpoint numbers (in string) and values are structures
187 as returned from \"-break-list\" by `gdb-json-partial-output'
188 \(\"body\" field is used). Updated in
189 `gdb-breakpoints-list-handler-custom'.")
191 (defvar gdb-current-language nil
)
192 (defvar gdb-var-list nil
193 "List of variables in watch window.
194 Each element has the form
195 (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
196 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
197 address for root variables.")
198 (defvar gdb-main-file nil
"Source file from which program execution begins.")
200 ;; Overlay arrow markers
201 (defvar gdb-stack-position nil
)
202 (defvar gdb-thread-position nil
)
203 (defvar gdb-disassembly-position nil
)
205 (defvar gdb-location-alist nil
206 "Alist of breakpoint numbers and full filenames. Only used for files that
208 (defvar gdb-active-process nil
209 "GUD tooltips display variable values when t, and macro definitions otherwise.")
210 (defvar gdb-error
"Non-nil when GDB is reporting an error.")
211 (defvar gdb-macro-info nil
212 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
213 (defvar gdb-register-names nil
"List of register names.")
214 (defvar gdb-changed-registers nil
215 "List of changed register numbers (strings).")
216 (defvar gdb-buffer-fringe-width nil
)
217 (defvar gdb-last-command nil
)
218 (defvar gdb-prompt-name nil
)
219 (defvar gdb-token-number
0)
220 (defvar gdb-handler-alist
'())
221 (defvar gdb-handler-number nil
)
222 (defvar gdb-source-file-list nil
223 "List of source files for the current executable.")
224 (defvar gdb-first-done-or-error t
)
225 (defvar gdb-source-window nil
)
226 (defvar gdb-inferior-status nil
)
227 (defvar gdb-continuation nil
)
228 (defvar gdb-version nil
)
229 (defvar gdb-filter-output nil
230 "Message to be shown in GUD console.
232 This variable is updated in `gdb-done-or-error' and returned by
233 `gud-gdbmi-marker-filter'.")
235 (defvar gdb-non-stop nil
236 "Indicates whether current GDB session is using non-stop mode.
238 It is initialized to `gdb-non-stop-setting' at the beginning of
241 (defvar gdb-buffer-type nil
242 "One of the symbols bound in `gdb-buffer-rules'.")
243 (make-variable-buffer-local 'gdb-buffer-type
)
245 (defvar gdb-output-sink
'nil
246 "The disposition of the output of the current gdb command.
247 Possible values are these symbols:
249 `user' -- gdb output should be copied to the GUD buffer
252 `emacs' -- output should be collected in the partial-output-buffer
253 for subsequent processing by a command. This is the
254 disposition of output generated by commands that
255 gdb mode sends to gdb on its own behalf.")
257 ;; Pending triggers prevent congestion: Emacs won't send two similar
258 ;; consecutive requests.
260 (defvar gdb-pending-triggers
'()
261 "A list of trigger functions which have not yet been handled.
263 Elements are either function names or pairs (buffer . function)")
265 (defmacro gdb-add-pending
(item)
266 `(push ,item gdb-pending-triggers
))
267 (defmacro gdb-pending-p
(item)
268 `(member ,item gdb-pending-triggers
))
269 (defmacro gdb-delete-pending
(item)
270 `(setq gdb-pending-triggers
271 (delete ,item gdb-pending-triggers
)))
273 (defmacro gdb-wait-for-pending
(&rest body
)
274 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
276 This function checks `gdb-pending-triggers' value every
277 `gdb-wait-for-pending' seconds."
281 (if (not gdb-pending-triggers
)
283 (gdb-wait-for-pending ,@body
)))))
287 (defmacro gdb-add-subscriber
(publisher subscriber
)
288 "Register new PUBLISHER's SUBSCRIBER.
290 SUBSCRIBER must be a pair, where cdr is a function of one
291 argument (see `gdb-emit-signal')."
292 `(add-to-list ',publisher
,subscriber t
))
294 (defmacro gdb-delete-subscriber
(publisher subscriber
)
295 "Unregister SUBSCRIBER from PUBLISHER."
296 `(setq ,publisher
(delete ,subscriber
299 (defun gdb-get-subscribers (publisher)
302 (defun gdb-emit-signal (publisher &optional signal
)
303 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
304 (dolist (subscriber (gdb-get-subscribers publisher
))
305 (funcall (cdr subscriber
) signal
)))
307 (defvar gdb-buf-publisher
'()
308 "Used to invalidate GDB buffers by emitting a signal in
311 Must be a list of pairs with cars being buffers and cdr's being
312 valid signal handlers.")
315 "GDB graphical interface"
317 :link
'(info-link "(emacs)GDB Graphical Interface")
320 (defgroup gdb-non-stop nil
321 "GDB non-stop debugging settings"
325 (defgroup gdb-buffers nil
330 (defcustom gdb-debug-log-max
128
331 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
333 :type
'(choice (integer :tag
"Number of elements")
334 (const :tag
"Unlimited" nil
))
337 (defcustom gdb-non-stop-setting t
338 "When in non-stop mode, stopped threads can be examined while
339 other threads continue to execute.
341 GDB session needs to be restarted for this setting to take
347 ;; TODO Some commands can't be called with --all (give a notice about
348 ;; it in setting doc)
349 (defcustom gdb-gud-control-all-threads t
350 "When enabled, GUD execution commands affect all threads when
351 in non-stop mode. Otherwise, only current thread is affected."
356 (defcustom gdb-switch-reasons t
357 "List of stop reasons which cause Emacs to switch to the thread
358 which caused the stop. When t, switch to stopped thread no matter
359 what the reason was. When nil, never switch to stopped thread
362 This setting is used in non-stop mode only. In all-stop mode,
363 Emacs always switches to the thread which caused the stop."
364 ;; exited, exited-normally and exited-signalled are not
365 ;; thread-specific stop reasons and therefore are not included in
368 (const :tag
"All reasons" t
)
369 (set :tag
"Selection of reasons..."
370 (const :tag
"A breakpoint was reached." "breakpoint-hit")
371 (const :tag
"A watchpoint was triggered." "watchpoint-trigger")
372 (const :tag
"A read watchpoint was triggered."
373 "read-watchpoint-trigger")
374 (const :tag
"An access watchpoint was triggered."
375 "access-watchpoint-trigger")
376 (const :tag
"Function finished execution." "function-finished")
377 (const :tag
"Location reached." "location-reached")
378 (const :tag
"Watchpoint has gone out of scope"
380 (const :tag
"End of stepping range reached."
381 "end-stepping-range")
382 (const :tag
"Signal received (like interruption)."
384 (const :tag
"None" nil
))
387 :link
'(info-link "(gdb)GDB/MI Async Records"))
389 (defcustom gdb-stopped-hooks nil
390 "This variable holds a list of functions to be called whenever
393 Each function takes one argument, a parsed MI response, which
394 contains fields of corresponding MI *stopped async record:
396 ((stopped-threads . \"all\")
398 (frame (line . \"38\")
399 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
400 (file . \"server.c\")
401 (args ((value . \"0x804b038\")
404 (addr . \"0x0804869e\"))
405 (reason . \"end-stepping-range\"))
407 Note that \"reason\" is only present in non-stop debugging mode.
409 `bindat-get-field' may be used to access the fields of response.
411 Each function is called after the new current thread was selected
412 and GDB buffers were updated in `gdb-stopped'."
413 :type
'(repeat function
)
416 :link
'(info-link "(gdb)GDB/MI Async Records"))
418 (defcustom gdb-switch-when-another-stopped t
419 "When nil, Emacs won't switch to stopped thread if some other
420 stopped thread is already selected."
425 (defcustom gdb-stack-buffer-locations t
426 "Show file information or library names in stack buffers."
431 (defcustom gdb-stack-buffer-addresses nil
432 "Show frame addresses in stack buffers."
437 (defcustom gdb-thread-buffer-verbose-names t
438 "Show long thread names in threads buffer."
443 (defcustom gdb-thread-buffer-arguments t
444 "Show function arguments in threads buffer."
449 (defcustom gdb-thread-buffer-locations t
450 "Show file information or library names in threads buffer."
455 (defcustom gdb-thread-buffer-addresses nil
456 "Show addresses for thread frames in threads buffer."
461 (defcustom gdb-show-threads-by-default nil
462 "Show threads list buffer instead of breakpoints list by
468 (defvar gdb-debug-log nil
469 "List of commands sent to and replies received from GDB.
470 Most recent commands are listed first. This list stores only the last
471 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
474 (defcustom gdb-enable-debug nil
475 "Non-nil means record the process input and output in `gdb-debug-log'."
480 (defcustom gdb-cpp-define-alist-program
"gcc -E -dM -"
481 "Shell command for generating a list of defined macros in a source file.
482 This list is used to display the #define directive associated
483 with an identifier as a tooltip. It works in a debug session with
484 GDB, when `gud-tooltip-mode' is t.
486 Set `gdb-cpp-define-alist-flags' for any include paths or
492 (defcustom gdb-cpp-define-alist-flags
""
493 "Preprocessor flags for `gdb-cpp-define-alist-program'."
498 (defcustom gdb-create-source-file-list t
499 "Non-nil means create a list of files from which the executable was built.
500 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
501 line for a long time when starting, possibly because your executable was
502 built from a large number of files. This allows quicker initialization
503 but means that these files are not automatically enabled for debugging,
504 e.g., you won't be able to click in the fringe to set a breakpoint until
505 execution has already stopped there."
510 (defcustom gdb-show-main nil
511 "Non-nil means display source file containing the main routine at startup.
512 Also display the main routine in the disassembly buffer if present."
517 (defun gdb-force-mode-line-update (status)
518 (let ((buffer gud-comint-buffer
))
519 (if (and buffer
(buffer-name buffer
))
520 (with-current-buffer buffer
521 (setq mode-line-process
523 (process-status (get-buffer-process buffer
)) status
))
524 ;; Force mode line redisplay soon.
525 (force-mode-line-update)))))
527 (defun gdb-enable-debug (arg)
528 "Toggle logging of transaction between Emacs and Gdb.
529 The log is stored in `gdb-debug-log' as an alist with elements
530 whose cons is send, send-item or recv and whose cdr is the string
531 being transferred. This list may grow up to a size of
532 `gdb-debug-log-max' after which the oldest element (at the end of
533 the list) is deleted every time a new one is added (at the front)."
535 (setq gdb-enable-debug
537 (not gdb-enable-debug
)
538 (> (prefix-numeric-value arg
) 0)))
539 (message (format "Logging of transaction %sabled"
540 (if gdb-enable-debug
"en" "dis"))))
542 ;; These two are used for menu and toolbar
543 (defun gdb-control-all-threads ()
544 "Switch to non-stop/A mode."
546 (setq gdb-gud-control-all-threads t
)
547 ;; Actually forcing the tool-bar to update.
548 (force-mode-line-update)
549 (message "Now in non-stop/A mode."))
551 (defun gdb-control-current-thread ()
552 "Switch to non-stop/T mode."
554 (setq gdb-gud-control-all-threads nil
)
555 ;; Actually forcing the tool-bar to update.
556 (force-mode-line-update)
557 (message "Now in non-stop/T mode."))
559 (defun gdb-find-watch-expression ()
560 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list
))
561 (varnum (car var
)) expr
)
562 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum
)
563 (let ((var1 (assoc (match-string 1 varnum
) gdb-var-list
)) var2 varnumlet
564 (component-list (split-string (match-string 2 varnum
) "\\." t
)))
565 (setq expr
(nth 1 var1
))
566 (setq varnumlet
(car var1
))
567 (dolist (component component-list
)
568 (setq var2
(assoc varnumlet gdb-var-list
))
569 (setq expr
(concat expr
570 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2
))
571 (concat "[" component
"]")
572 (concat "." component
))))
573 (setq varnumlet
(concat varnumlet
"." component
)))
576 ;; noall is used for commands which don't take --all, but only
578 (defun gdb-gud-context-command (command &optional noall
)
579 "When `gdb-non-stop' is t, add --thread option to COMMAND if
580 `gdb-gud-control-all-threads' is nil and --all option otherwise.
581 If NOALL is t, always add --thread option no matter what
582 `gdb-gud-control-all-threads' value is.
584 When `gdb-non-stop' is nil, return COMMAND unchanged."
586 (if (and gdb-gud-control-all-threads
588 (string-equal gdb-version
"7.0+"))
589 (concat command
" --all ")
590 (gdb-current-context-command command
))
593 (defmacro gdb-gud-context-call
(cmd1 &optional cmd2 noall noarg
)
594 "`gud-call' wrapper which adds --thread/--all options between
595 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
597 NOARG must be t when this macro is used outside `gud-def'"
599 (concat (gdb-gud-context-command ,cmd1
,noall
) " " ,cmd2
)
600 ,(when (not noarg
) 'arg
)))
603 (defun gdb (command-line)
604 "Run gdb on program FILE in buffer *gud-FILE*.
605 The directory containing FILE becomes the initial working directory
606 and source-file directory for your debugger.
608 COMMAND-LINE is the shell command for starting the gdb session.
609 It should be a string consisting of the name of the gdb
610 executable followed by command-line options. The command-line
611 options should include \"-i=mi\" to use gdb's MI text interface.
612 Note that the old \"--annotate\" option is no longer supported.
614 If `gdb-many-windows' is nil (the default value) then gdb just
615 pops up the GUD buffer unless `gdb-show-main' is t. In this case
616 it starts with two windows: one displaying the GUD buffer and the
617 other with the source file with the main routine of the inferior.
619 If `gdb-many-windows' is t, regardless of the value of
620 `gdb-show-main', the layout below will appear. Keybindings are
621 shown in some of the buffers.
623 Watch expressions appear in the speedbar/slowbar.
625 The following commands help control operation :
627 `gdb-many-windows' - Toggle the number of windows gdb uses.
628 `gdb-restore-windows' - To restore the window layout.
630 See Info node `(emacs)GDB Graphical Interface' for a more
631 detailed description of this mode.
634 +----------------------------------------------------------------------+
636 +-----------------------------------+----------------------------------+
637 | GUD buffer (I/O of GDB) | Locals buffer |
641 +-----------------------------------+----------------------------------+
642 | Source buffer | I/O buffer (of debugged program) |
650 +-----------------------------------+----------------------------------+
651 | Stack buffer | Breakpoints buffer |
652 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
653 | | RET gdb-goto-breakpoint |
654 | | D gdb-delete-breakpoint |
655 +-----------------------------------+----------------------------------+"
657 (interactive (list (gud-query-cmdline 'gdb
)))
659 (when (and gud-comint-buffer
660 (buffer-name gud-comint-buffer
)
661 (get-buffer-process gud-comint-buffer
)
662 (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
)))
663 (gdb-restore-windows)
665 "Multiple debugging requires restarting in text command mode"))
667 (gud-common-init command-line nil
'gud-gdbmi-marker-filter
)
668 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
669 (setq comint-input-sender
'gdb-send
)
670 (when (ring-empty-p comint-input-ring
) ; cf shell-mode
671 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
672 (if (eq system-type
'ms-dos
)
675 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
676 (hsize (getenv "HISTSIZE")))
677 (dolist (file (append '("~/.gdbinit")
678 (unless (string-equal (expand-file-name ".")
679 (expand-file-name "~"))
681 (if (file-readable-p (setq file
(expand-file-name file
)))
683 (insert-file-contents file
)
684 ;; TODO? check for "set history save\\( *on\\)?" and do
685 ;; not use history otherwise?
686 (while (re-search-forward
687 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t
)
688 (cond ((string-equal (match-string 1) "filename")
689 (setq hfile
(expand-file-name
691 (file-name-directory file
))))
692 ((string-equal (match-string 1) "size")
693 (setq hsize
(match-string 2))))))))
695 (integerp (setq hsize
(string-to-number hsize
)))
697 (set (make-local-variable 'comint-input-ring-size
) hsize
))
699 (set (make-local-variable 'comint-input-ring-file-name
) hfile
))
700 (comint-read-input-ring t
)))
701 (gud-def gud-tbreak
"tbreak %f:%l" "\C-t"
702 "Set temporary breakpoint at current line.")
704 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
705 "\C-j" "Set execution address to current line.")
707 (gud-def gud-up
"up %p" "<" "Up N stack frames (numeric arg).")
708 (gud-def gud-down
"down %p" ">" "Down N stack frames (numeric arg).")
709 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
710 (gud-def gud-pstar
"print* %e" nil
711 "Evaluate C dereferenced pointer expression at point.")
713 (gud-def gud-step
(gdb-gud-context-call "-exec-step" "%p" t
)
715 "Step one source line with display.")
716 (gud-def gud-stepi
(gdb-gud-context-call "-exec-step-instruction" "%p" t
)
718 "Step one instruction with display.")
719 (gud-def gud-next
(gdb-gud-context-call "-exec-next" "%p" t
)
721 "Step one line (skip functions).")
722 (gud-def gud-nexti
(gdb-gud-context-call "-exec-next-instruction" "%p" t
)
724 "Step one instruction (skip functions).")
725 (gud-def gud-cont
(gdb-gud-context-call "-exec-continue")
727 "Continue with display.")
728 (gud-def gud-finish
(gdb-gud-context-call "-exec-finish" nil t
)
730 "Finish executing current function.")
731 (gud-def gud-run
"-exec-run"
735 (gud-def gud-break
(if (not (string-match "Disassembly" mode-name
))
736 (gud-call "break %f:%l" arg
)
740 (gud-call "break *%a" arg
)))
741 "\C-b" "Set breakpoint at current line or address.")
743 (gud-def gud-remove
(if (not (string-match "Disassembly" mode-name
))
744 (gud-call "clear %f:%l" arg
)
748 (gud-call "clear *%a" arg
)))
749 "\C-d" "Remove breakpoint at current line or address.")
751 ;; -exec-until doesn't support --all yet
752 (gud-def gud-until
(if (not (string-match "Disassembly" mode-name
))
753 (gud-call "-exec-until %f:%l" arg
)
757 (gud-call "-exec-until *%a" arg
)))
758 "\C-u" "Continue to current line or address.")
759 ;; TODO Why arg here?
761 gud-go
(gud-call (if gdb-active-process
762 (gdb-gud-context-command "-exec-continue")
764 nil
"Start or continue execution.")
766 ;; For debugging Emacs only.
770 "pp1 " (if (eq (buffer-local-value
771 'major-mode
(window-buffer)) 'speedbar-mode
)
772 (gdb-find-watch-expression) "%e")) arg
)
773 nil
"Print the Emacs s-expression.")
775 (define-key gud-minor-mode-map
[left-margin mouse-1
]
776 'gdb-mouse-set-clear-breakpoint
)
777 (define-key gud-minor-mode-map
[left-fringe mouse-1
]
778 'gdb-mouse-set-clear-breakpoint
)
779 (define-key gud-minor-mode-map
[left-margin C-mouse-1
]
780 'gdb-mouse-toggle-breakpoint-margin
)
781 (define-key gud-minor-mode-map
[left-fringe C-mouse-1
]
782 'gdb-mouse-toggle-breakpoint-fringe
)
784 (define-key gud-minor-mode-map
[left-margin drag-mouse-1
]
786 (define-key gud-minor-mode-map
[left-fringe drag-mouse-1
]
788 (define-key gud-minor-mode-map
[left-margin mouse-3
]
790 (define-key gud-minor-mode-map
[left-fringe mouse-3
]
793 (define-key gud-minor-mode-map
[left-margin C-drag-mouse-1
]
795 (define-key gud-minor-mode-map
[left-fringe C-drag-mouse-1
]
797 (define-key gud-minor-mode-map
[left-fringe C-mouse-3
]
799 (define-key gud-minor-mode-map
[left-margin C-mouse-3
]
802 (add-hook 'completion-at-point-functions
#'gud-gdb-completion-at-point
804 (local-set-key "\C-i" 'completion-at-point
)
806 (setq gdb-first-prompt t
)
807 (setq gud-running nil
)
811 (run-hooks 'gdb-mode-hook
))
815 (setq gdb-selected-frame nil
817 gdb-thread-number nil
819 gdb-pending-triggers nil
820 gdb-output-sink
'user
821 gdb-location-alist nil
822 gdb-source-file-list nil
825 gdb-handler-alist
'()
826 gdb-handler-number nil
828 gdb-first-done-or-error t
829 gdb-buffer-fringe-width
(car (window-fringes))
831 gdb-source-window nil
832 gdb-inferior-status nil
834 gdb-buf-publisher
'()
836 gdb-breakpoints-list
'()
837 gdb-register-names
'()
838 gdb-non-stop gdb-non-stop-setting
)
840 (setq gdb-buffer-type
'gdbmi
)
842 (gdb-force-mode-line-update
843 (propertize "initializing..." 'face font-lock-variable-name-face
))
845 (gdb-get-buffer-create 'gdb-inferior-io
)
846 (gdb-clear-inferior-io)
847 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter
)
849 ;; Needs GDB 6.4 onwards
850 (list (concat "-inferior-tty-set "
852 ;; The process can run on a remote host.
853 (process-get (get-process "gdb-inferior") 'remote-tty
)
854 (process-tty-name (get-process "gdb-inferior"))))
856 (if (eq window-system
'w32
)
857 (gdb-input (list "-gdb-set new-console off" 'ignore
)))
858 (gdb-input (list "-gdb-set height 0" 'ignore
))
861 (gdb-input (list "-gdb-set non-stop 1" 'gdb-non-stop-handler
)))
863 ;; find source file and compilation directory here
865 ; Needs GDB 6.2 onwards.
866 (list "-file-list-exec-source-files" 'gdb-get-source-file-list
))
867 (if gdb-create-source-file-list
869 ; Needs GDB 6.0 onwards.
870 (list "-file-list-exec-source-file" 'gdb-get-source-file
)))
872 (list "-gdb-show prompt" 'gdb-get-prompt
)))
874 (defun gdb-non-stop-handler ()
875 (goto-char (point-min))
876 (if (re-search-forward "No symbol" nil t
)
879 "This version of GDB doesn't support non-stop mode. Turning it off.")
880 (setq gdb-non-stop nil
)
881 (setq gdb-version
"pre-7.0"))
882 (setq gdb-version
"7.0+")
883 (gdb-input (list "-gdb-set target-async 1" 'ignore
))
884 (gdb-input (list "-enable-pretty-printing" 'ignore
))))
886 (defvar gdb-define-alist nil
"Alist of #define directives for GUD tooltips.")
888 (defun gdb-create-define-alist ()
889 "Create an alist of #define directives for GUD tooltips."
890 (let* ((file (buffer-file-name))
892 (with-output-to-string
893 (with-current-buffer standard-output
896 ;; call-process doesn't work with remote file names.
897 (not (file-remote-p default-directory
))
898 (call-process shell-file-name file
899 (list t nil
) nil
"-c"
900 (concat gdb-cpp-define-alist-program
" "
901 gdb-cpp-define-alist-flags
))))))
902 (define-list (split-string output
"\n" t
))
904 (setq gdb-define-alist nil
)
905 (dolist (define define-list
)
906 (setq name
(nth 1 (split-string define
"[( ]")))
907 (push (cons name define
) gdb-define-alist
))))
909 (declare-function tooltip-show
"tooltip" (text &optional use-echo-area
))
910 (defvar tooltip-use-echo-area
)
912 (defun gdb-tooltip-print (expr)
913 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
914 (goto-char (point-min))
915 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t
)
917 (concat expr
" = " (read (match-string 1)))
918 (or gud-tooltip-echo-area tooltip-use-echo-area
919 (not (display-graphic-p)))))))
921 ;; If expr is a macro for a function don't print because of possible dangerous
922 ;; side-effects. Also printing a function within a tooltip generates an
923 ;; unexpected starting annotation (phase error).
924 (defun gdb-tooltip-print-1 (expr)
925 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
926 (goto-char (point-min))
927 (if (search-forward "expands to: " nil t
)
928 (unless (looking-at "\\S-+.*(.*).*")
930 (list (concat "-data-evaluate-expression " expr
)
931 `(lambda () (gdb-tooltip-print ,expr
))))))))
933 (defun gdb-init-buffer ()
934 (set (make-local-variable 'gud-minor-mode
) 'gdbmi
)
935 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
936 (when gud-tooltip-mode
937 (make-local-variable 'gdb-define-alist
)
938 (gdb-create-define-alist)
939 (add-hook 'after-save-hook
'gdb-create-define-alist nil t
)))
941 (defmacro gdb-if-arrow
(arrow-position &rest body
)
943 (let ((buffer (marker-buffer ,arrow-position
)) (line))
944 (if (equal buffer
(window-buffer (posn-window end
)))
945 (with-current-buffer buffer
946 (when (or (equal start end
)
947 (equal (posn-point start
)
948 (marker-position ,arrow-position
)))
951 (defun gdb-mouse-until (event)
952 "Continue running until a source line past the current line.
953 The destination source line can be selected either by clicking
954 with mouse-3 on the fringe/margin or dragging the arrow
955 with mouse-1 (default bindings)."
957 (let ((start (event-start event
))
958 (end (event-end event
)))
959 (gdb-if-arrow gud-overlay-arrow-position
960 (setq line
(line-number-at-pos (posn-point end
)))
961 (gud-call (concat "until " (number-to-string line
))))
962 (gdb-if-arrow gdb-disassembly-position
964 (goto-char (point-min))
965 (forward-line (1- (line-number-at-pos (posn-point end
))))
967 (gud-call (concat "until *%a"))))))
969 (defun gdb-mouse-jump (event)
970 "Set execution address/line.
971 The destination source line can be selected either by clicking with C-mouse-3
972 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
973 Unlike `gdb-mouse-until' the destination address can be before the current
974 line, and no execution takes place."
976 (let ((start (event-start event
))
977 (end (event-end event
)))
978 (gdb-if-arrow gud-overlay-arrow-position
979 (setq line
(line-number-at-pos (posn-point end
)))
981 (gud-call (concat "tbreak " (number-to-string line
)))
982 (gud-call (concat "jump " (number-to-string line
)))))
983 (gdb-if-arrow gdb-disassembly-position
985 (goto-char (point-min))
986 (forward-line (1- (line-number-at-pos (posn-point end
))))
989 (gud-call (concat "tbreak *%a"))
990 (gud-call (concat "jump *%a")))))))
992 (defcustom gdb-show-changed-values t
993 "If non-nil change the face of out of scope variables and changed values.
994 Out of scope variables are suppressed with `shadow' face.
995 Changed values are highlighted with the face `font-lock-warning-face'."
1000 (defcustom gdb-max-children
40
1001 "Maximum number of children before expansion requires confirmation."
1006 (defcustom gdb-delete-out-of-scope t
1007 "If non-nil delete watch expressions automatically when they go out of scope."
1012 (defcustom gdb-speedbar-auto-raise nil
1013 "If non-nil raise speedbar every time display of watch expressions is\
1019 (defcustom gdb-use-colon-colon-notation nil
1020 "If non-nil use FUN::VAR format to display variables in the speedbar."
1025 (defun gdb-speedbar-auto-raise (arg)
1026 "Toggle automatic raising of the speedbar for watch expressions.
1027 With prefix argument ARG, automatically raise speedbar if ARG is
1028 positive, otherwise don't automatically raise it."
1030 (setq gdb-speedbar-auto-raise
1032 (not gdb-speedbar-auto-raise
)
1033 (> (prefix-numeric-value arg
) 0)))
1034 (message (format "Auto raising %sabled"
1035 (if gdb-speedbar-auto-raise
"en" "dis"))))
1037 (define-key gud-minor-mode-map
"\C-c\C-w" 'gud-watch
)
1038 (define-key global-map
(concat gud-key-prefix
"\C-w") 'gud-watch
)
1040 (declare-function tooltip-identifier-from-point
"tooltip" (point))
1042 (defun gud-watch (&optional arg event
)
1043 "Watch expression at point.
1044 With arg, enter name of variable to be watched in the minibuffer."
1045 (interactive (list current-prefix-arg last-input-event
))
1046 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer
)))
1047 (if (eq minor-mode
'gdbmi
)
1049 (if event
(posn-set-point (event-end event
)))
1051 (save-selected-window
1054 (completing-read "Name of variable: "
1055 'gud-gdb-complete-command
)
1056 (if (and transient-mark-mode mark-active
)
1057 (buffer-substring (region-beginning) (region-end))
1058 (concat (if (derived-mode-p 'gdb-registers-mode
) "$")
1059 (tooltip-identifier-from-point (point)))))))
1060 (set-text-properties 0 (length expr
) nil expr
)
1062 (list (concat"-var-create - * " expr
"")
1063 `(lambda () (gdb-var-create-handler ,expr
)))))))
1064 (message "gud-watch is a no-op in this mode."))))
1066 (defun gdb-var-create-handler (expr)
1067 (let* ((result (gdb-json-partial-output)))
1068 (if (not (bindat-get-field result
'msg
))
1070 (list (bindat-get-field result
'name
)
1071 (if (and (string-equal gdb-current-language
"c")
1072 gdb-use-colon-colon-notation gdb-selected-frame
)
1073 (setq expr
(concat gdb-selected-frame
"::" expr
))
1075 (bindat-get-field result
'numchild
)
1076 (bindat-get-field result
'type
)
1077 (bindat-get-field result
'value
)
1079 (bindat-get-field result
'has_more
)
1080 gdb-frame-address
)))
1081 (push var gdb-var-list
)
1083 (unless (string-equal
1084 speedbar-initial-expansion-list-name
"GUD")
1085 (speedbar-change-initial-expansion-list "GUD")))
1086 (message-box "No symbol \"%s\" in current context." expr
))))
1088 (defun gdb-speedbar-update ()
1089 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
)
1090 (not (gdb-pending-p 'gdb-speedbar-timer
)))
1091 ;; Dummy command to update speedbar even when idle.
1092 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn
))
1093 ;; Keep gdb-pending-triggers non-nil till end.
1094 (gdb-add-pending 'gdb-speedbar-timer
)))
1096 (defun gdb-speedbar-timer-fn ()
1097 (if gdb-speedbar-auto-raise
1098 (raise-frame speedbar-frame
))
1099 (gdb-delete-pending 'gdb-speedbar-timer
)
1100 (speedbar-timer-fn))
1102 (defun gdb-var-evaluate-expression-handler (varnum changed
)
1103 (goto-char (point-min))
1104 (re-search-forward ".*value=\\(\".*\"\\)" nil t
)
1105 (let ((var (assoc varnum gdb-var-list
)))
1107 (if changed
(setcar (nthcdr 5 var
) 'changed
))
1108 (setcar (nthcdr 4 var
) (read (match-string 1)))))
1109 (gdb-speedbar-update))
1111 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1112 (defun gdb-var-list-children (varnum)
1114 (list (concat "-var-update " varnum
) 'ignore
))
1116 (list (concat "-var-list-children --all-values "
1118 `(lambda () (gdb-var-list-children-handler ,varnum
)))))
1120 (defun gdb-var-list-children-handler (varnum)
1121 (let* ((var-list nil
)
1122 (output (bindat-get-field (gdb-json-partial-output "child")))
1123 (children (bindat-get-field output
'children
)))
1124 (catch 'child-already-watched
1125 (dolist (var gdb-var-list
)
1126 (if (string-equal varnum
(car var
))
1128 ;; With dynamic varobjs numchild may have increased.
1129 (setcar (nthcdr 2 var
) (bindat-get-field output
'numchild
))
1131 (dolist (child children
)
1132 (let ((varchild (list (bindat-get-field child
'name
)
1133 (bindat-get-field child
'exp
)
1134 (bindat-get-field child
'numchild
)
1135 (bindat-get-field child
'type
)
1136 (bindat-get-field child
'value
)
1138 (bindat-get-field child
'has_more
))))
1139 (if (assoc (car varchild
) gdb-var-list
)
1140 (throw 'child-already-watched nil
))
1141 (push varchild var-list
))))
1142 (push var var-list
)))
1143 (setq gdb-var-list
(nreverse var-list
))))
1144 (gdb-speedbar-update))
1146 (defun gdb-var-set-format (format)
1147 "Set the output format for a variable displayed in the speedbar."
1148 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1151 (list (concat "-var-set-format " varnum
" " format
) 'ignore
))
1154 (defun gdb-var-delete-1 (var varnum
)
1156 (list (concat "-var-delete " varnum
) 'ignore
))
1157 (setq gdb-var-list
(delq var gdb-var-list
))
1158 (dolist (varchild gdb-var-list
)
1159 (if (string-match (concat (car var
) "\\.") (car varchild
))
1160 (setq gdb-var-list
(delq varchild gdb-var-list
)))))
1162 (defun gdb-var-delete ()
1163 "Delete watch expression at point from the speedbar."
1165 (let ((text (speedbar-line-text)))
1166 (string-match "\\(\\S-+\\)" text
)
1167 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1169 (if (string-match "\\." (car var
))
1170 (message-box "Can only delete a root expression")
1171 (gdb-var-delete-1 var varnum
)))))
1173 (defun gdb-var-delete-children (varnum)
1174 "Delete children of variable object at point from the speedbar."
1176 (list (concat "-var-delete -c " varnum
) 'ignore
)))
1178 (defun gdb-edit-value (_text _token _indent
)
1179 "Assign a value to a variable displayed in the speedbar."
1180 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
1181 (varnum (car var
)) (value))
1182 (setq value
(read-string "New value: "))
1184 (list (concat "-var-assign " varnum
" " value
)
1185 `(lambda () (gdb-edit-value-handler ,value
))))))
1187 (defconst gdb-error-regexp
"\\^error,msg=\\(\".+\"\\)")
1189 (defun gdb-edit-value-handler (value)
1190 (goto-char (point-min))
1191 (if (re-search-forward gdb-error-regexp nil t
)
1192 (message-box "Invalid number or expression (%s)" value
)))
1194 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1195 (defun gdb-var-update ()
1196 (if (not (gdb-pending-p 'gdb-var-update
))
1198 (list "-var-update --all-values *" 'gdb-var-update-handler
)))
1199 (gdb-add-pending 'gdb-var-update
))
1201 (defun gdb-var-update-handler ()
1202 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist
)))
1203 (dolist (var gdb-var-list
)
1204 (setcar (nthcdr 5 var
) nil
))
1205 (let ((temp-var-list gdb-var-list
))
1206 (dolist (change changelist
)
1207 (let* ((varnum (bindat-get-field change
'name
))
1208 (var (assoc varnum gdb-var-list
))
1209 (new-num (bindat-get-field change
'new_num_children
)))
1211 (let ((scope (bindat-get-field change
'in_scope
))
1212 (has-more (bindat-get-field change
'has_more
)))
1213 (cond ((string-equal scope
"false")
1214 (if gdb-delete-out-of-scope
1215 (gdb-var-delete-1 var varnum
)
1216 (setcar (nthcdr 5 var
) 'out-of-scope
)))
1217 ((string-equal scope
"true")
1218 (setcar (nthcdr 6 var
) has-more
)
1219 (when (and (or (not has-more
)
1220 (string-equal has-more
"0"))
1222 (string-equal (nth 2 var
) "0"))
1223 (setcar (nthcdr 4 var
)
1224 (bindat-get-field change
'value
))
1225 (setcar (nthcdr 5 var
) 'changed
)))
1226 ((string-equal scope
"invalid")
1227 (gdb-var-delete-1 var varnum
)))))
1228 (let ((var-list nil
) var1
1229 (children (bindat-get-field change
'new_children
)))
1231 (setq var1
(pop temp-var-list
))
1233 (if (string-equal varnum
(car var1
))
1234 (let ((new (string-to-number new-num
))
1235 (previous (string-to-number (nth 2 var1
))))
1236 (setcar (nthcdr 2 var1
) new-num
)
1237 (push var1 var-list
)
1240 ;; Add new children to list.
1241 (dotimes (dummy previous
)
1242 (push (pop temp-var-list
) var-list
))
1243 (dolist (child children
)
1245 (list (bindat-get-field child
'name
)
1246 (bindat-get-field child
'exp
)
1247 (bindat-get-field child
'numchild
)
1248 (bindat-get-field child
'type
)
1249 (bindat-get-field child
'value
)
1251 (bindat-get-field child
'has_more
))))
1252 (push varchild var-list
))))
1253 ;; Remove deleted children from list.
1255 (dotimes (dummy new
)
1256 (push (pop temp-var-list
) var-list
))
1257 (dotimes (dummy (- previous new
))
1258 (pop temp-var-list
)))))
1259 (push var1 var-list
))
1260 (setq var1
(pop temp-var-list
)))
1261 (setq gdb-var-list
(nreverse var-list
))))))))
1262 (setq gdb-pending-triggers
1263 (delq 'gdb-var-update gdb-pending-triggers
))
1264 (gdb-speedbar-update))
1266 (defun gdb-speedbar-expand-node (text token indent
)
1267 "Expand the node the user clicked on.
1268 TEXT is the text of the button we clicked on, a + or - item.
1269 TOKEN is data related to this node.
1270 INDENT is the current indentation depth."
1271 (cond ((string-match "+" text
) ;expand this node
1272 (let* ((var (assoc token gdb-var-list
))
1273 (expr (nth 1 var
)) (children (nth 2 var
)))
1274 (if (or (<= (string-to-number children
) gdb-max-children
)
1276 (format "%s has %s children. Continue? " expr children
)))
1277 (gdb-var-list-children token
))))
1278 ((string-match "-" text
) ;contract this node
1279 (dolist (var gdb-var-list
)
1280 (if (string-match (concat token
"\\.") (car var
))
1281 (setq gdb-var-list
(delq var gdb-var-list
))))
1282 (gdb-var-delete-children token
)
1283 (speedbar-change-expand-button-char ?
+)
1284 (speedbar-delete-subblock indent
))
1285 (t (error "Ooops... not sure what to do")))
1286 (speedbar-center-buffer-smartly))
1288 (defun gdb-get-target-string ()
1289 (with-current-buffer gud-comint-buffer
1296 ;; Each buffer has a TYPE -- a symbol that identifies the function
1297 ;; of that particular buffer.
1299 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1300 ;; is constructed specially.
1302 ;; Others are constructed by gdb-get-buffer-create and
1303 ;; named according to the rules set forth in the gdb-buffer-rules
1305 (defvar gdb-buffer-rules
'())
1307 (defun gdb-rules-name-maker (rules-entry)
1309 (defun gdb-rules-buffer-mode (rules-entry)
1310 (nth 2 rules-entry
))
1311 (defun gdb-rules-update-trigger (rules-entry)
1312 (nth 3 rules-entry
))
1314 (defun gdb-update-buffer-name ()
1315 "Rename current buffer according to name-maker associated with
1316 it in `gdb-buffer-rules'."
1317 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1318 gdb-buffer-rules
))))
1319 (when f
(rename-buffer (funcall f
)))))
1321 (defun gdb-current-buffer-rules ()
1322 "Get `gdb-buffer-rules' entry for current buffer type."
1323 (assoc gdb-buffer-type gdb-buffer-rules
))
1325 (defun gdb-current-buffer-thread ()
1326 "Get thread object of current buffer from `gdb-threads-list'.
1328 When current buffer is not bound to any thread, return main
1330 (cdr (assoc gdb-thread-number gdb-threads-list
)))
1332 (defun gdb-current-buffer-frame ()
1333 "Get current stack frame object for thread of current buffer."
1334 (bindat-get-field (gdb-current-buffer-thread) 'frame
))
1336 (defun gdb-buffer-type (buffer)
1337 "Get value of `gdb-buffer-type' for BUFFER."
1338 (with-current-buffer buffer
1341 (defun gdb-buffer-shows-main-thread-p ()
1342 "Return t if current GDB buffer shows main selected thread and
1343 is not bound to it."
1345 (not (local-variable-p 'gdb-thread-number
)))
1347 (defun gdb-get-buffer (buffer-type &optional thread
)
1348 "Get a specific GDB buffer.
1350 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1351 and `gdb-thread-number' (if provided) must be equal to THREAD."
1353 (dolist (buffer (buffer-list) nil
)
1354 (with-current-buffer buffer
1355 (when (and (eq gdb-buffer-type buffer-type
)
1357 (equal gdb-thread-number thread
)))
1358 (throw 'found buffer
))))))
1360 (defun gdb-get-buffer-create (buffer-type &optional thread
)
1361 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1362 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1364 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1365 buffer-local variable of the new buffer.
1367 Buffer mode and name are selected according to buffer type.
1369 If buffer has trigger associated with it in `gdb-buffer-rules',
1370 this trigger is subscribed to `gdb-buf-publisher' and called with
1372 (or (gdb-get-buffer buffer-type thread
)
1373 (let ((rules (assoc buffer-type gdb-buffer-rules
))
1374 (new (generate-new-buffer "limbo")))
1375 (with-current-buffer new
1376 (let ((mode (gdb-rules-buffer-mode rules
))
1377 (trigger (gdb-rules-update-trigger rules
)))
1378 (when mode
(funcall mode
))
1379 (setq gdb-buffer-type buffer-type
)
1381 (set (make-local-variable 'gdb-thread-number
) thread
))
1382 (set (make-local-variable 'gud-minor-mode
)
1383 (buffer-local-value 'gud-minor-mode gud-comint-buffer
))
1384 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1385 (rename-buffer (funcall (gdb-rules-name-maker rules
)))
1387 (gdb-add-subscriber gdb-buf-publisher
1388 (cons (current-buffer)
1389 (gdb-bind-function-to-buffer
1390 trigger
(current-buffer))))
1391 (funcall trigger
'start
))
1392 (current-buffer))))))
1394 (defun gdb-bind-function-to-buffer (expr buffer
)
1395 "Return a function which will evaluate EXPR in BUFFER."
1396 `(lambda (&rest args
)
1397 (with-current-buffer ,buffer
1398 (apply ',expr args
))))
1400 ;; Used to define all gdb-frame-*-buffer functions except
1401 ;; `gdb-frame-io-buffer'
1402 (defmacro def-gdb-frame-for-buffer
(name buffer
&optional doc
)
1403 "Define a function NAME which shows gdb BUFFER in a separate frame.
1405 DOC is an optional documentation string."
1406 `(defun ,name
(&optional thread
)
1409 (let ((special-display-regexps (append special-display-regexps
'(".*")))
1410 (special-display-frame-alist gdb-frame-parameters
))
1411 (display-buffer (gdb-get-buffer-create ,buffer thread
)))))
1413 (defmacro def-gdb-display-buffer
(name buffer
&optional doc
)
1414 "Define a function NAME which shows gdb BUFFER.
1416 DOC is an optional documentation string."
1417 `(defun ,name
(&optional thread
)
1421 (gdb-get-buffer-create ,buffer thread
) t
)))
1423 ;; Used to display windows with thread-bound buffers
1424 (defmacro def-gdb-preempt-display-buffer
(name buffer
&optional doc
1426 `(defun ,name
(&optional thread
)
1429 (gdb-preempt-existing-or-display-buffer
1430 (gdb-get-buffer-create ,buffer thread
)
1431 ,split-horizontal
)))
1433 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1434 ;; at least one and possible more functions. The functions have these
1435 ;; roles in defining a buffer type:
1437 ;; NAME - Return a name for this buffer type.
1439 ;; The remaining function(s) are optional:
1441 ;; MODE - called in a new buffer with no arguments, should establish
1442 ;; the proper mode for the buffer.
1445 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
1446 (let ((binding (assoc buffer-type gdb-buffer-rules
)))
1448 (setcdr binding rules
)
1449 (push (cons buffer-type rules
)
1450 gdb-buffer-rules
))))
1452 (defun gdb-parent-mode ()
1453 "Generic mode to derive all other GDB buffer modes from."
1454 (kill-all-local-variables)
1455 (setq buffer-read-only t
)
1456 (buffer-disable-undo)
1457 ;; Delete buffer from gdb-buf-publisher when it's killed
1458 ;; (if it has an associated update trigger)
1463 (let ((trigger (gdb-rules-update-trigger
1464 (gdb-current-buffer-rules))))
1466 (gdb-delete-subscriber
1468 ;; This should match gdb-add-subscriber done in
1469 ;; gdb-get-buffer-create
1470 (cons (current-buffer)
1471 (gdb-bind-function-to-buffer trigger
(current-buffer))))))))
1474 ;; Partial-output buffer : This accumulates output from a command executed on
1475 ;; behalf of emacs (rather than the user).
1477 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1478 'gdb-partial-output-name
)
1480 (defun gdb-partial-output-name ()
1481 (concat " *partial-output-"
1482 (gdb-get-target-string)
1486 (gdb-set-buffer-rules 'gdb-inferior-io
1487 'gdb-inferior-io-name
1488 'gdb-inferior-io-mode
)
1490 (defun gdb-inferior-io-name ()
1491 (concat "*input/output of "
1492 (gdb-get-target-string)
1495 (defun gdb-display-io-buffer ()
1496 "Display IO of debugged program in a separate window."
1499 (gdb-get-buffer-create 'gdb-inferior-io
) t
))
1501 (defconst gdb-frame-parameters
1502 '((height .
14) (width .
80)
1504 (tool-bar-lines . nil
)
1505 (menu-bar-lines . nil
)
1506 (minibuffer . nil
)))
1508 (defun gdb-frame-io-buffer ()
1509 "Display IO of debugged program in a new frame."
1511 (let ((special-display-regexps (append special-display-regexps
'(".*")))
1512 (special-display-frame-alist gdb-frame-parameters
))
1513 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io
))))
1515 (defvar gdb-inferior-io-mode-map
1516 (let ((map (make-sparse-keymap)))
1517 (define-key map
"\C-c\C-c" 'gdb-io-interrupt
)
1518 (define-key map
"\C-c\C-z" 'gdb-io-stop
)
1519 (define-key map
"\C-c\C-\\" 'gdb-io-quit
)
1520 (define-key map
"\C-c\C-d" 'gdb-io-eof
)
1521 (define-key map
"\C-d" 'gdb-io-eof
)
1524 ;; We want to use comint because it has various nifty and familiar features.
1525 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
1526 "Major mode for gdb inferior-io."
1527 :syntax-table nil
:abbrev-table nil
1528 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil
))
1530 (defun gdb-inferior-filter (proc string
)
1531 (unless (string-equal string
"")
1532 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
) t
))
1533 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1534 (comint-output-filter proc string
)))
1536 (defun gdb-io-interrupt ()
1537 "Interrupt the program being debugged."
1540 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1542 (defun gdb-io-quit ()
1543 "Send quit signal to the program being debugged."
1546 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1548 (defun gdb-io-stop ()
1549 "Stop the program being debugged."
1552 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1554 (defun gdb-io-eof ()
1555 "Send end-of-file to the program being debugged."
1558 (get-buffer-process gud-comint-buffer
)))
1560 (defun gdb-clear-inferior-io ()
1561 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1565 (defconst breakpoint-xpm-data
1567 static char *magick[] = {
1568 /* columns rows colors chars-per-pixel */
1584 "XPM data used for breakpoint icon.")
1586 (defconst breakpoint-enabled-pbm-data
1589 0 0 0 0 1 1 1 1 0 0 0 0
1590 0 0 0 1 1 1 1 1 1 0 0 0
1591 0 0 1 1 1 1 1 1 1 1 0 0
1592 0 1 1 1 1 1 1 1 1 1 1 0
1593 0 1 1 1 1 1 1 1 1 1 1 0
1594 0 1 1 1 1 1 1 1 1 1 1 0
1595 0 1 1 1 1 1 1 1 1 1 1 0
1596 0 0 1 1 1 1 1 1 1 1 0 0
1597 0 0 0 1 1 1 1 1 1 0 0 0
1598 0 0 0 0 1 1 1 1 0 0 0 0"
1599 "PBM data used for enabled breakpoint icon.")
1601 (defconst breakpoint-disabled-pbm-data
1613 0 0 0 1 0 1 0 1 0 0"
1614 "PBM data used for disabled breakpoint icon.")
1616 (defvar breakpoint-enabled-icon nil
1617 "Icon for enabled breakpoint in display margin.")
1619 (defvar breakpoint-disabled-icon nil
1620 "Icon for disabled breakpoint in display margin.")
1622 (declare-function define-fringe-bitmap
"fringe.c"
1623 (bitmap bits
&optional height width align
))
1625 (and (display-images-p)
1626 ;; Bitmap for breakpoint in fringe
1627 (define-fringe-bitmap 'breakpoint
1628 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1629 ;; Bitmap for gud-overlay-arrow in fringe
1630 (define-fringe-bitmap 'hollow-right-triangle
1631 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1633 (defface breakpoint-enabled
1637 "Face for enabled breakpoint icon in fringe."
1640 (defface breakpoint-disabled
1641 '((((class color
) (min-colors 88)) :foreground
"grey70")
1642 ;; Ensure that on low-color displays that we end up something visible.
1643 (((class color
) (min-colors 8) (background light
))
1644 :foreground
"black")
1645 (((class color
) (min-colors 8) (background dark
))
1646 :foreground
"white")
1647 (((type tty
) (class mono
))
1649 (t :background
"gray"))
1650 "Face for disabled breakpoint icon in fringe."
1654 (defun gdb-send (proc string
)
1655 "A comint send filter for gdb."
1656 (with-current-buffer gud-comint-buffer
1657 (let ((inhibit-read-only t
))
1658 (remove-text-properties (point-min) (point-max) '(face))))
1659 ;; mimic <RET> key to repeat previous command in GDB
1660 (if (not (string= "" string
))
1661 (setq gdb-last-command string
)
1662 (if gdb-last-command
(setq string gdb-last-command
)))
1663 (if gdb-enable-debug
1664 (push (cons 'mi-send
(concat string
"\n")) gdb-debug-log
))
1665 (if (string-match "^-" string
)
1668 (setq gdb-first-done-or-error t
)
1669 (process-send-string proc
(concat string
"\n")))
1671 (if (string-match "\\\\$" string
)
1672 (setq gdb-continuation
(concat gdb-continuation string
"\n"))
1673 (setq gdb-first-done-or-error t
)
1674 (process-send-string proc
(concat "-interpreter-exec console \""
1675 gdb-continuation string
"\"\n"))
1676 (setq gdb-continuation nil
))))
1678 (defun gdb-input (item)
1679 (if gdb-enable-debug
(push (cons 'send-item item
) gdb-debug-log
))
1680 (setq gdb-token-number
(1+ gdb-token-number
))
1681 (setcar item
(concat (number-to-string gdb-token-number
) (car item
)))
1682 (push (cons gdb-token-number
(car (cdr item
))) gdb-handler-alist
)
1683 (process-send-string (get-buffer-process gud-comint-buffer
)
1684 (concat (car item
) "\n")))
1686 ;; NOFRAME is used for gud execution control commands
1687 (defun gdb-current-context-command (command)
1688 "Add --thread to gdb COMMAND when needed."
1689 (if (and gdb-thread-number
1690 (string-equal gdb-version
"7.0+"))
1691 (concat command
" --thread " gdb-thread-number
)
1694 (defun gdb-current-context-buffer-name (name)
1695 "Add thread information and asterisks to string NAME.
1697 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1699 (if (local-variable-p 'gdb-thread-number
)
1700 (format " (bound to thread %s)" gdb-thread-number
)
1704 (defun gdb-current-context-mode-name (mode)
1705 "Add thread information to MODE which is to be used as
1708 (if gdb-thread-number
1709 (format " [thread %s]" gdb-thread-number
)
1713 (defcustom gud-gdb-command-name
"gdb -i=mi"
1714 "Default command to execute an executable under the GDB debugger."
1719 (setq gud-running nil
)
1720 (setq gdb-output-sink
'user
)
1721 (setq gdb-pending-triggers nil
))
1723 (defun gdb-update ()
1724 "Update buffers showing status of debug session."
1725 (when gdb-first-prompt
1726 (gdb-force-mode-line-update
1727 (propertize "initializing..." 'face font-lock-variable-name-face
))
1729 (setq gdb-first-prompt nil
))
1731 (gdb-get-main-selected-frame)
1732 ;; We may need to update gdb-threads-list so we can use
1733 (gdb-get-buffer-create 'gdb-threads-buffer
)
1734 ;; gdb-break-list is maintained in breakpoints handler
1735 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1737 (gdb-emit-signal gdb-buf-publisher
'update
)
1739 (gdb-get-changed-registers)
1741 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1742 (dolist (var gdb-var-list
)
1743 (setcar (nthcdr 5 var
) nil
))
1746 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1747 ;; because we may need to update current gud-running value without
1748 ;; changing current thread (see gdb-running)
1749 (defun gdb-setq-thread-number (number)
1750 "Only this function must be used to change `gdb-thread-number'
1751 value to NUMBER, because `gud-running' and `gdb-frame-number'
1752 need to be updated appropriately when current thread changes."
1753 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1754 (unless (string-equal number
"0") (setq gdb-thread-number number
))
1755 (setq gdb-frame-number
"0")
1756 (gdb-update-gud-running))
1758 (defun gdb-update-gud-running ()
1759 "Set `gud-running' according to the state of current thread.
1761 `gdb-frame-number' is set to 0 if current thread is now stopped.
1763 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1764 cannot be reliably used to determine whether or not execution
1765 control buttons should be shown in menu or toolbar. Use
1766 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1769 For all-stop mode, thread information is unavailable while target
1771 (let ((old-value gud-running
))
1773 (string= (bindat-get-field (gdb-current-buffer-thread) 'state
)
1775 ;; Set frame number to "0" when _current_ threads stops
1776 (when (and (gdb-current-buffer-thread)
1777 (not (eq gud-running old-value
)))
1778 (setq gdb-frame-number
"0"))))
1780 (defun gdb-show-run-p ()
1781 "Return t if \"Run/continue\" should be shown on the toolbar."
1782 (or (not gdb-active-process
)
1784 (not gdb-gud-control-all-threads
)
1787 (and gdb-gud-control-all-threads
1788 (> gdb-stopped-threads-count
0))))
1790 (defun gdb-show-stop-p ()
1791 "Return t if \"Stop\" should be shown on the toolbar."
1793 (not gdb-gud-control-all-threads
)
1796 (and gdb-gud-control-all-threads
1797 (> gdb-running-threads-count
0))))
1799 ;; GUD displays the selected GDB frame. This might might not be the current
1800 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1801 ;; visited breakpoint is, use that window.
1802 (defun gdb-display-source-buffer (buffer)
1803 (let* ((last-window (if gud-last-last-frame
1805 (gud-find-file (car gud-last-last-frame
)))))
1806 (source-window (or last-window
1807 (if (and gdb-source-window
1808 (window-live-p gdb-source-window
))
1809 gdb-source-window
))))
1811 (setq gdb-source-window source-window
)
1812 (set-window-buffer source-window buffer
))
1815 (defun gdb-car< (a b
)
1816 (< (car a
) (car b
)))
1818 (defvar gdbmi-record-list
1819 '((gdb-gdb .
"(gdb) \n")
1820 (gdb-done .
"\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1821 (gdb-starting .
"\\([0-9]*\\)\\^running\n")
1822 (gdb-error .
"\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1823 (gdb-console .
"~\\(\".*?\"\\)\n")
1824 (gdb-internals .
"&\\(\".*?\"\\)\n")
1825 (gdb-stopped .
"\\*stopped,?\\(.*?\\)\n")
1826 (gdb-running .
"\\*running,\\(.*?\n\\)")
1827 (gdb-thread-created .
"=thread-created,\\(.*?\n\\)")
1828 (gdb-thread-selected .
"=thread-selected,\\(.*?\\)\n")
1829 (gdb-thread-exited .
"=thread-exited,\\(.*?\n\\)")
1830 (gdb-ignored-notification .
"=[-[:alpha:]]+,?\\(.*?\\)\n")
1831 (gdb-shell .
"\\(\\(?:^.+\n\\)+\\)")))
1833 (defun gud-gdbmi-marker-filter (string)
1834 "Filter GDB/MI output."
1836 ;; Record transactions if logging is enabled.
1837 (when gdb-enable-debug
1838 (push (cons 'recv string
) gdb-debug-log
)
1839 (if (and gdb-debug-log-max
1840 (> (length gdb-debug-log
) gdb-debug-log-max
))
1841 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
1843 ;; Recall the left over gud-marker-acc from last time
1844 (setq gud-marker-acc
(concat gud-marker-acc string
))
1846 ;; Start accumulating output for the GUD buffer
1847 (setq gdb-filter-output
"")
1848 (let (output-record-list)
1850 ;; Process all the complete markers in this chunk.
1851 (dolist (gdbmi-record gdbmi-record-list
)
1852 (while (string-match (cdr gdbmi-record
) gud-marker-acc
)
1853 (push (list (match-beginning 0)
1855 (match-string 1 gud-marker-acc
)
1856 (match-string 2 gud-marker-acc
)
1859 (setq gud-marker-acc
1860 (concat (substring gud-marker-acc
0 (match-beginning 0))
1861 ;; Pad with spaces to preserve position.
1862 (make-string (length (match-string 0 gud-marker-acc
)) 32)
1863 (substring gud-marker-acc
(match-end 0))))))
1865 (setq output-record-list
(sort output-record-list
'gdb-car
<))
1867 (dolist (output-record output-record-list
)
1868 (let ((record-type (cadr output-record
))
1869 (arg1 (nth 2 output-record
))
1870 (arg2 (nth 3 output-record
)))
1871 (if (eq record-type
'gdb-error
)
1872 (gdb-done-or-error arg2 arg1
'error
)
1873 (if (eq record-type
'gdb-done
)
1874 (gdb-done-or-error arg2 arg1
'done
)
1875 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1876 ;; error message on internal stream. Don't print to GUD buffer.
1877 (unless (and (eq record-type
'gdb-internals
)
1878 (string-equal (read arg1
) "No registers.\n"))
1879 (funcall record-type arg1
))))))
1881 (setq gdb-output-sink
'user
)
1883 (string-match "^ *" gud-marker-acc
)
1884 (setq gud-marker-acc
(substring gud-marker-acc
(match-end 0)))
1888 (defun gdb-gdb (_output-field))
1890 (defun gdb-shell (output-field)
1891 (let ((gdb-output-sink gdb-output-sink
))
1892 (setq gdb-filter-output
1893 (concat output-field gdb-filter-output
))))
1895 (defun gdb-ignored-notification (_output-field))
1897 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1898 (defun gdb-thread-created (_output-field))
1899 (defun gdb-thread-exited (output-field)
1900 "Handle =thread-exited async record: unset `gdb-thread-number'
1901 if current thread exited and update threads list."
1902 (let* ((thread-id (bindat-get-field (gdb-json-string output-field
) 'id
)))
1903 (if (string= gdb-thread-number thread-id
)
1904 (gdb-setq-thread-number nil
))
1905 ;; When we continue current thread and it quickly exits,
1906 ;; gdb-pending-triggers left after gdb-running disallow us to
1907 ;; properly call -thread-info without --thread option. Thus we
1908 ;; need to use gdb-wait-for-pending.
1909 (gdb-wait-for-pending
1910 (gdb-emit-signal gdb-buf-publisher
'update-threads
))))
1912 (defun gdb-thread-selected (output-field)
1913 "Handler for =thread-selected MI output record.
1915 Sets `gdb-thread-number' to new id."
1916 (let* ((result (gdb-json-string output-field
))
1917 (thread-id (bindat-get-field result
'id
)))
1918 (gdb-setq-thread-number thread-id
)
1919 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1920 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1921 ;; as usually. Things happen to fast and second call (from
1922 ;; gdb-thread-selected handler) gets cut off by our beloved
1923 ;; gdb-pending-triggers.
1924 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1925 ;; body will get executed when `gdb-pending-triggers` is empty.
1926 (gdb-wait-for-pending
1929 (defun gdb-running (output-field)
1931 (bindat-get-field (gdb-json-string output-field
) 'thread-id
)))
1932 ;; We reset gdb-frame-number to nil if current thread has gone
1933 ;; running. This can't be done in gdb-thread-list-handler-custom
1934 ;; because we need correct gdb-frame-number by the time
1935 ;; -thread-info command is sent.
1936 (when (or (string-equal thread-id
"all")
1937 (string-equal thread-id gdb-thread-number
))
1938 (setq gdb-frame-number nil
)))
1939 (setq gdb-inferior-status
"running")
1940 (gdb-force-mode-line-update
1941 (propertize gdb-inferior-status
'face font-lock-type-face
))
1942 (when (not gdb-non-stop
)
1943 (setq gud-running t
))
1944 (setq gdb-active-process t
)
1945 (gdb-emit-signal gdb-buf-publisher
'update-threads
))
1947 (defun gdb-starting (_output-field)
1948 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1949 (setq gdb-inferior-status
"running")
1950 (gdb-force-mode-line-update
1951 (propertize gdb-inferior-status
'face font-lock-type-face
))
1952 (setq gdb-active-process t
)
1953 (setq gud-running t
)
1954 ;; GDB doesn't seem to respond to -thread-info before first stop or
1955 ;; thread exit (even in non-stop mode), so this is useless.
1956 ;; Behaviour may change in the future.
1957 (gdb-emit-signal gdb-buf-publisher
'update-threads
))
1959 ;; -break-insert -t didn't give a reason before gdb 6.9
1961 (defun gdb-stopped (output-field)
1962 "Given the contents of *stopped MI async record, select new
1963 current thread and update GDB buffers."
1964 ;; Reason is available with target-async only
1965 (let* ((result (gdb-json-string output-field
))
1966 (reason (bindat-get-field result
'reason
))
1967 (thread-id (bindat-get-field result
'thread-id
)))
1969 ;; -data-list-register-names needs to be issued for any stopped
1971 (when (not gdb-register-names
)
1973 (list (concat "-data-list-register-names"
1974 (if (string-equal gdb-version
"7.0+")
1975 (concat" --thread " thread-id
)))
1976 'gdb-register-names-handler
)))
1978 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1979 ;;; because synchronous GDB doesn't give these fields with CLI.
1982 ;;; ;; Extract the frame position from the marker.
1983 ;;; gud-last-frame (cons file
1984 ;;; (string-to-number
1985 ;;; (match-string 6 gud-marker-acc)))))
1987 (setq gdb-inferior-status
(or reason
"unknown"))
1988 (gdb-force-mode-line-update
1989 (propertize gdb-inferior-status
'face font-lock-warning-face
))
1990 (if (string-equal reason
"exited-normally")
1991 (setq gdb-active-process nil
))
1993 ;; Select new current thread.
1995 ;; Don't switch if we have no reasons selected
1996 (when gdb-switch-reasons
1997 ;; Switch from another stopped thread only if we have
1998 ;; gdb-switch-when-another-stopped:
1999 (when (or gdb-switch-when-another-stopped
2000 (not (string= "stopped"
2001 (bindat-get-field (gdb-current-buffer-thread) 'state
))))
2002 ;; Switch if current reason has been selected or we have no
2004 (if (or (eq gdb-switch-reasons t
)
2005 (member reason gdb-switch-reasons
))
2006 (when (not (string-equal gdb-thread-number thread-id
))
2007 (message (concat "Switched to thread " thread-id
))
2008 (gdb-setq-thread-number thread-id
))
2009 (message (format "Thread %s stopped" thread-id
)))))
2011 ;; Print "(gdb)" to GUD console
2012 (when gdb-first-done-or-error
2013 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2015 ;; In non-stop, we update information as soon as another thread gets
2017 (when (or gdb-first-done-or-error
2019 ;; In all-stop this updates gud-running properly as well.
2021 (setq gdb-first-done-or-error nil
))
2022 (run-hook-with-args 'gdb-stopped-hooks result
)))
2024 ;; Remove the trimmings from log stream containing debugging messages
2025 ;; being produced by GDB's internals, use warning face and send to GUD
2027 (defun gdb-internals (output-field)
2028 (setq gdb-filter-output
2031 (let ((error-message
2032 (read output-field
)))
2034 0 (length error-message
)
2035 'face font-lock-warning-face
2039 ;; Remove the trimmings from the console stream and send to GUD buffer
2040 ;; (frontend MI commands should not print to this stream)
2041 (defun gdb-console (output-field)
2042 (setq gdb-filter-output
2045 (read output-field
))))
2047 (defun gdb-done-or-error (output-field token-number type
)
2048 (if (string-equal token-number
"")
2049 ;; Output from command entered by user
2051 (setq gdb-output-sink
'user
)
2052 (setq token-number nil
)
2053 ;; MI error - send to minibuffer
2054 (when (eq type
'error
)
2055 ;; Skip "msg=" from `output-field'
2056 (message (read (substring output-field
4)))
2057 ;; Don't send to the console twice. (If it is a console error
2058 ;; it is also in the console stream.)
2059 (setq output-field nil
)))
2060 ;; Output from command from frontend.
2061 (setq gdb-output-sink
'emacs
))
2063 (gdb-clear-partial-output)
2064 (when gdb-first-done-or-error
2065 (unless (or token-number gud-running
)
2066 (setq gdb-filter-output
(concat gdb-filter-output gdb-prompt-name
)))
2068 (setq gdb-first-done-or-error nil
))
2070 (setq gdb-filter-output
2071 (gdb-concat-output gdb-filter-output output-field
))
2075 (with-current-buffer
2076 (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2078 (cdr (assoc (string-to-number token-number
) gdb-handler-alist
))))
2079 (setq gdb-handler-alist
2080 (assq-delete-all token-number gdb-handler-alist
)))))
2082 (defun gdb-concat-output (so-far new
)
2083 (let ((sink gdb-output-sink
))
2085 ((eq sink
'user
) (concat so-far new
))
2087 (gdb-append-to-partial-output new
)
2090 (defun gdb-append-to-partial-output (string)
2091 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2092 (goto-char (point-max))
2095 (defun gdb-clear-partial-output ()
2096 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2099 (defun gdb-jsonify-buffer (&optional fix-key fix-list
)
2100 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2102 Field names are wrapped in double quotes and equal signs are
2103 replaced with semicolons.
2105 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2106 partial output. This is used to get rid of useless keys in lists
2107 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2108 -break-info are examples of MI commands which issue such
2111 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2112 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2113 -break-info output when it contains breakpoint script field
2114 incompatible with GDB/MI output syntax."
2116 (goto-char (point-min))
2119 (while (re-search-forward (concat "[\\[,]\\(" fix-key
"=\\)") nil t
)
2120 (replace-match "" nil nil nil
1))))
2123 ;; Find positions of braces which enclose broken list
2124 (while (re-search-forward (concat fix-list
"={\"") nil t
)
2125 (let ((p1 (goto-char (- (point) 2)))
2126 (p2 (progn (forward-sexp)
2128 ;; Replace braces with brackets
2136 (goto-char (point-min))
2138 (while (re-search-forward
2139 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t
)
2140 (replace-match "\"\\1\":\\2" nil nil
))
2141 (goto-char (point-max))
2144 (defun gdb-json-read-buffer (&optional fix-key fix-list
)
2145 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2147 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2148 (gdb-jsonify-buffer fix-key fix-list
)
2150 (goto-char (point-min))
2151 (let ((json-array-type 'list
))
2154 (defun gdb-json-string (string &optional fix-key fix-list
)
2155 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2157 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2160 (gdb-json-read-buffer fix-key fix-list
)))
2162 (defun gdb-json-partial-output (&optional fix-key fix-list
)
2163 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2165 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2166 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
2167 (gdb-json-read-buffer fix-key fix-list
)))
2169 (defun gdb-line-posns (line)
2170 "Return a pair of LINE beginning and end positions."
2171 (let ((offset (1+ (- line
(line-number-at-pos)))))
2173 (line-beginning-position offset
)
2174 (line-end-position offset
))))
2176 (defmacro gdb-mark-line
(line variable
)
2177 "Set VARIABLE marker to point at beginning of LINE.
2179 If current window has no fringes, inverse colors on LINE.
2181 Return position where LINE begins."
2183 (let* ((posns (gdb-line-posns ,line
))
2184 (start-posn (car posns
))
2185 (end-posn (cdr posns
)))
2186 (set-marker ,variable
(copy-marker start-posn
))
2187 (when (not (> (car (window-fringes)) 0))
2188 (put-text-property start-posn end-posn
2189 'font-lock-face
'(:inverse-video t
)))
2192 (defun gdb-pad-string (string padding
)
2193 (format (concat "%" (number-to-string padding
) "s") string
))
2195 ;; gdb-table struct is a way to programmatically construct simple
2196 ;; tables. It help to reliably align columns of data in GDB buffers
2202 (row-properties nil
)
2205 (defun gdb-mapcar* (function &rest seqs
)
2206 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2207 If there are several SEQS, FUNCTION is called with that many
2208 arugments, and mapping stops as sson as the shortest list runs
2210 (let ((shortest (apply #'min
(mapcar #'length seqs
))))
2217 (number-sequence 0 (1- shortest
)))))
2219 (defun gdb-table-add-row (table row
&optional properties
)
2220 "Add ROW of string to TABLE and recalculate column sizes.
2222 When non-nil, PROPERTIES will be added to the whole row when
2223 calling `gdb-table-string'."
2224 (let ((rows (gdb-table-rows table
))
2225 (row-properties (gdb-table-row-properties table
))
2226 (column-sizes (gdb-table-column-sizes table
))
2227 (right-align (gdb-table-right-align table
)))
2228 (when (not column-sizes
)
2229 (setf (gdb-table-column-sizes table
)
2230 (make-list (length row
) 0)))
2231 (setf (gdb-table-rows table
)
2232 (append rows
(list row
)))
2233 (setf (gdb-table-row-properties table
)
2234 (append row-properties
(list properties
)))
2235 (setf (gdb-table-column-sizes table
)
2236 (gdb-mapcar* (lambda (x s
)
2238 (max (abs x
) (string-width (or s
"")))))
2239 (if right-align new-x
(- new-x
))))
2240 (gdb-table-column-sizes table
)
2242 ;; Avoid trailing whitespace at eol
2243 (if (not (gdb-table-right-align table
))
2244 (setcar (last (gdb-table-column-sizes table
)) 0))))
2246 (defun gdb-table-string (table &optional sep
)
2247 "Return TABLE as a string with columns separated with SEP."
2248 (let ((column-sizes (gdb-table-column-sizes table
)))
2252 (lambda (row properties
)
2254 (mapconcat 'identity
2255 (gdb-mapcar* (lambda (s x
) (gdb-pad-string s x
))
2259 (gdb-table-rows table
)
2260 (gdb-table-row-properties table
))
2263 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2264 (defun gdb-get-many-fields (struct &rest fields
)
2265 "Return a list of FIELDS values from STRUCT."
2267 (dolist (field fields values
)
2268 (setq values
(append values
(list (bindat-get-field struct field
)))))))
2270 (defmacro def-gdb-auto-update-trigger
(trigger-name gdb-command
2272 &optional signal-list
)
2273 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2274 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2275 buffer with `gdb-bind-function-to-buffer'.
2277 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2278 defined trigger is called with an argument from SIGNAL-LIST. It's
2279 not recommended to define triggers with empty SIGNAL-LIST.
2280 Normally triggers should respond at least to 'update signal.
2282 Normally the trigger defined by this command must be called from
2283 the buffer where HANDLER-NAME must work. This should be done so
2284 that buffer-local thread number may be used in GDB-COMMAND (by
2285 calling `gdb-current-context-command').
2286 `gdb-bind-function-to-buffer' is used to achieve this, see
2287 `gdb-get-buffer-create'.
2289 Triggers defined by this command are meant to be used as a
2290 trigger argument when describing buffer types with
2291 `gdb-set-buffer-rules'."
2292 `(defun ,trigger-name
(&optional signal
)
2294 (or (not ,signal-list
)
2295 (memq signal
,signal-list
))
2296 (when (not (gdb-pending-p
2297 (cons (current-buffer) ',trigger-name
)))
2300 (gdb-bind-function-to-buffer ',handler-name
(current-buffer))))
2301 (gdb-add-pending (cons (current-buffer) ',trigger-name
))))))
2303 ;; Used by disassembly buffer only, the rest use
2304 ;; def-gdb-trigger-and-handler
2305 (defmacro def-gdb-auto-update-handler
(handler-name trigger-name custom-defun
2306 &optional nopreserve
)
2307 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2309 Handlers are normally called from the buffers they put output in.
2311 Delete ((current-buffer) . TRIGGER-NAME) from
2312 `gdb-pending-triggers', erase current buffer and evaluate
2313 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2315 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2316 `(defun ,handler-name
()
2317 (gdb-delete-pending (cons (current-buffer) ',trigger-name
))
2318 (let* ((buffer-read-only nil
)
2319 (window (get-buffer-window (current-buffer) 0))
2320 (start (window-start window
))
2321 (p (window-point window
)))
2324 (gdb-update-buffer-name)
2325 ,(when (not nopreserve
)
2326 '(set-window-start window start
)
2327 '(set-window-point window p
)))))
2329 (defmacro def-gdb-trigger-and-handler
(trigger-name gdb-command
2330 handler-name custom-defun
2331 &optional signal-list
)
2332 "Define trigger and handler.
2334 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2335 `def-gdb-auto-update-trigger'.
2337 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2338 `def-gdb-auto-update-handler'."
2340 (def-gdb-auto-update-trigger ,trigger-name
2342 ,handler-name
,signal-list
)
2343 (def-gdb-auto-update-handler ,handler-name
2344 ,trigger-name
,custom-defun
)))
2348 ;; Breakpoint buffer : This displays the output of `-break-list'.
2349 (def-gdb-trigger-and-handler
2350 gdb-invalidate-breakpoints
"-break-list"
2351 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2354 (gdb-set-buffer-rules
2355 'gdb-breakpoints-buffer
2356 'gdb-breakpoints-buffer-name
2357 'gdb-breakpoints-mode
2358 'gdb-invalidate-breakpoints
)
2360 (defun gdb-breakpoints-list-handler-custom ()
2361 (let ((breakpoints-list (bindat-get-field
2362 (gdb-json-partial-output "bkpt" "script")
2363 'BreakpointTable
'body
))
2364 (table (make-gdb-table)))
2365 (setq gdb-breakpoints-list nil
)
2366 (gdb-table-add-row table
'("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2367 (dolist (breakpoint breakpoints-list
)
2368 (add-to-list 'gdb-breakpoints-list
2369 (cons (bindat-get-field breakpoint
'number
)
2371 (let ((at (bindat-get-field breakpoint
'at
))
2372 (pending (bindat-get-field breakpoint
'pending
))
2373 (func (bindat-get-field breakpoint
'func
))
2374 (type (bindat-get-field breakpoint
'type
)))
2375 (gdb-table-add-row table
2377 (bindat-get-field breakpoint
'number
)
2379 (bindat-get-field breakpoint
'disp
)
2380 (let ((flag (bindat-get-field breakpoint
'enabled
)))
2381 (if (string-equal flag
"y")
2382 (propertize "y" 'font-lock-face font-lock-warning-face
)
2383 (propertize "n" 'font-lock-face font-lock-comment-face
)))
2384 (bindat-get-field breakpoint
'addr
)
2385 (bindat-get-field breakpoint
'times
)
2386 (if (string-match ".*watchpoint" type
)
2387 (bindat-get-field breakpoint
'what
)
2390 (propertize (or func
"unknown")
2391 'font-lock-face font-lock-function-name-face
)
2392 (gdb-frame-location breakpoint
)))))
2393 ;; Add clickable properties only for breakpoints with file:line
2395 (append (list 'gdb-breakpoint breakpoint
)
2396 (when func
'(help-echo "mouse-2, RET: visit breakpoint"
2397 mouse-face highlight
))))))
2398 (insert (gdb-table-string table
" "))
2399 (gdb-place-breakpoints)))
2401 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2402 (defun gdb-place-breakpoints ()
2403 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2404 (dolist (buffer (buffer-list))
2405 (with-current-buffer buffer
2406 (if (and (eq gud-minor-mode
'gdbmi
)
2407 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2408 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2409 (dolist (breakpoint gdb-breakpoints-list
)
2410 (let* ((breakpoint (cdr breakpoint
)) ; gdb-breakpoints-list is
2411 ; an associative list
2412 (line (bindat-get-field breakpoint
'line
)))
2414 (let ((file (bindat-get-field breakpoint
'fullname
))
2415 (flag (bindat-get-field breakpoint
'enabled
))
2416 (bptno (bindat-get-field breakpoint
'number
)))
2417 (unless (file-exists-p file
)
2418 (setq file
(cdr (assoc bptno gdb-location-alist
))))
2420 (not (string-equal file
"File not found")))
2421 (with-current-buffer
2422 (find-file-noselect file
'nowarn
)
2424 ;; Only want one breakpoint icon at each location.
2425 (gdb-put-breakpoint-icon (string-equal flag
"y") bptno
2426 (string-to-number line
)))
2428 (list (concat "list " file
":1")
2431 (list "-file-list-exec-source-file"
2432 `(lambda () (gdb-get-location
2433 ,bptno
,line
,flag
))))))))))
2435 (defvar gdb-source-file-regexp
"fullname=\"\\(.*?\\)\"")
2437 (defun gdb-get-location (bptno line flag
)
2438 "Find the directory containing the relevant source file.
2439 Put in buffer and place breakpoint icon."
2440 (goto-char (point-min))
2441 (catch 'file-not-found
2442 (if (re-search-forward gdb-source-file-regexp nil t
)
2443 (delete (cons bptno
"File not found") gdb-location-alist
)
2444 (push (cons bptno
(match-string 1)) gdb-location-alist
)
2446 (unless (assoc bptno gdb-location-alist
)
2447 (push (cons bptno
"File not found") gdb-location-alist
)
2448 (message-box "Cannot find source file for breakpoint location.
2449 Add directory to search path for source files using the GDB command, dir."))
2450 (throw 'file-not-found nil
))
2451 (with-current-buffer (find-file-noselect (match-string 1))
2453 ;; only want one breakpoint icon at each location
2454 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
(string-to-number line
)))))
2456 (add-hook 'find-file-hook
'gdb-find-file-hook
)
2458 (defun gdb-find-file-hook ()
2459 "Set up buffer for debugging if file is part of the source code
2460 of the current session."
2461 (if (and (buffer-name gud-comint-buffer
)
2462 ;; in case gud or gdb-ui is just loaded
2464 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
2466 (if (member buffer-file-name gdb-source-file-list
)
2467 (with-current-buffer (find-buffer-visiting buffer-file-name
)
2468 (gdb-init-buffer)))))
2470 (declare-function gud-remove
"gdb-mi" t t
) ; gud-def
2471 (declare-function gud-break
"gdb-mi" t t
) ; gud-def
2472 (declare-function fringe-bitmaps-at-pos
"fringe.c" (&optional pos window
))
2474 (defun gdb-mouse-set-clear-breakpoint (event)
2475 "Set/clear breakpoint in left fringe/margin at mouse click.
2476 If not in a source or disassembly buffer just set point."
2478 (mouse-minibuffer-check event
)
2479 (let ((posn (event-end event
)))
2480 (with-selected-window (posn-window posn
)
2481 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode
))
2482 (if (numberp (posn-point posn
))
2484 (goto-char (posn-point posn
))
2485 (if (or (posn-object posn
)
2486 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
2490 (posn-set-point posn
))))
2492 (defun gdb-mouse-toggle-breakpoint-margin (event)
2493 "Enable/disable breakpoint in left margin with mouse click."
2495 (mouse-minibuffer-check event
)
2496 (let ((posn (event-end event
)))
2497 (if (numberp (posn-point posn
))
2498 (with-selected-window (posn-window posn
)
2500 (goto-char (posn-point posn
))
2501 (if (posn-object posn
)
2503 (let ((bptno (get-text-property
2504 0 'gdb-bptno
(car (posn-string posn
)))))
2506 (if (get-text-property
2507 0 'gdb-enabled
(car (posn-string posn
)))
2512 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2513 "Enable/disable breakpoint in left fringe with mouse click."
2515 (mouse-minibuffer-check event
)
2516 (let* ((posn (event-end event
))
2517 (pos (posn-point posn
))
2520 (with-selected-window (posn-window posn
)
2521 (with-current-buffer (window-buffer (selected-window))
2523 (dolist (overlay (overlays-in pos pos
))
2524 (when (overlay-get overlay
'put-break
)
2525 (setq obj
(overlay-get overlay
'before-string
))))
2529 (if (get-text-property 0 'gdb-enabled obj
)
2532 (get-text-property 0 'gdb-bptno obj
)))))))))
2534 (defun gdb-breakpoints-buffer-name ()
2535 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2537 (def-gdb-display-buffer
2538 gdb-display-breakpoints-buffer
2539 'gdb-breakpoints-buffer
2540 "Display status of user-settable breakpoints.")
2542 (def-gdb-frame-for-buffer
2543 gdb-frame-breakpoints-buffer
2544 'gdb-breakpoints-buffer
2545 "Display status of user-settable breakpoints in a new frame.")
2547 (defvar gdb-breakpoints-mode-map
2548 (let ((map (make-sparse-keymap))
2549 (menu (make-sparse-keymap "Breakpoints")))
2550 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
2551 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2552 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2553 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2554 (suppress-keymap map)
2555 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2556 (define-key map " " 'gdb-toggle-breakpoint)
2557 (define-key map "D" 'gdb-delete-breakpoint)
2558 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2559 (define-key map "q" 'gdb-delete-frame-or-window)
2560 (define-key map "\r" 'gdb-goto-breakpoint)
2561 (define-key map "\t" (lambda ()
2563 (gdb-set-window-buffer
2564 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2565 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2566 (define-key map [follow-link] 'mouse-face)
2569 (defun gdb-delete-frame-or-window ()
2570 "Delete frame if there is only one window. Otherwise delete the window."
2572 (if (one-window-p) (delete-frame)
2575 ;;from make-mode-line-mouse-map
2576 (defun gdb-make-header-line-mouse-map (mouse function) "\
2577 Return a keymap with single entry for mouse key MOUSE on the header line.
2578 MOUSE is defined to run function FUNCTION with no args in the buffer
2579 corresponding to the mode line clicked."
2580 (let ((map (make-sparse-keymap)))
2581 (define-key map (vector 'header-line mouse) function)
2582 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2585 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2587 'help-echo ,help-echo
2588 'mouse-face ',mouse-face
2591 (gdb-make-header-line-mouse-map
2593 (lambda (event) (interactive "e")
2594 (save-selected-window
2595 (select-window (posn-window (event-start event)))
2596 (gdb-set-window-buffer
2597 (gdb-get-buffer-create ',buffer) t) )))))
2600 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2603 (defun gdb-threads-buffer-name ()
2604 (concat "*threads of " (gdb-get-target-string) "*"))
2606 (def-gdb-display-buffer
2607 gdb-display-threads-buffer
2609 "Display GDB threads.")
2611 (def-gdb-frame-for-buffer
2612 gdb-frame-threads-buffer
2614 "Display GDB threads in a new frame.")
2616 (def-gdb-trigger-and-handler
2617 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2618 gdb-thread-list-handler gdb-thread-list-handler-custom
2619 '(start update update-threads))
2621 (gdb-set-buffer-rules
2623 'gdb-threads-buffer-name
2625 'gdb-invalidate-threads)
2627 (defvar gdb-threads-font-lock-keywords
2628 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2629 (" \\(stopped\\)" (1 font-lock-warning-face))
2630 (" \\(running\\)" (1 font-lock-string-face))
2631 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2632 "Font lock keywords used in `gdb-threads-mode'.")
2634 (defvar gdb-threads-mode-map
2635 (let ((map (make-sparse-keymap)))
2636 (define-key map "\r" 'gdb-select-thread)
2637 (define-key map "f" 'gdb-display-stack-for-thread)
2638 (define-key map "F" 'gdb-frame-stack-for-thread)
2639 (define-key map "l" 'gdb-display-locals-for-thread)
2640 (define-key map "L" 'gdb-frame-locals-for-thread)
2641 (define-key map "r" 'gdb-display-registers-for-thread)
2642 (define-key map "R" 'gdb-frame-registers-for-thread)
2643 (define-key map "d" 'gdb-display-disassembly-for-thread)
2644 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2645 (define-key map "i" 'gdb-interrupt-thread)
2646 (define-key map "c" 'gdb-continue-thread)
2647 (define-key map "s" 'gdb-step-thread)
2648 (define-key map "\t"
2651 (gdb-set-window-buffer
2652 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2653 (define-key map [mouse-2] 'gdb-select-thread)
2654 (define-key map [follow-link] 'mouse-face)
2657 (defvar gdb-threads-header
2659 (gdb-propertize-header
2660 "Breakpoints" gdb-breakpoints-buffer
2661 "mouse-1: select" mode-line-highlight mode-line-inactive)
2663 (gdb-propertize-header "Threads" gdb-threads-buffer
2664 nil nil mode-line)))
2666 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2667 "Major mode for GDB threads."
2668 (setq gdb-thread-position (make-marker))
2669 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2670 (setq header-line-format gdb-threads-header)
2671 (set (make-local-variable 'font-lock-defaults)
2672 '(gdb-threads-font-lock-keywords))
2673 'gdb-invalidate-threads)
2675 (defun gdb-thread-list-handler-custom ()
2676 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2677 (table (make-gdb-table))
2679 (setq gdb-threads-list nil)
2680 (setq gdb-running-threads-count 0)
2681 (setq gdb-stopped-threads-count 0)
2682 (set-marker gdb-thread-position nil)
2684 (dolist (thread (reverse threads-list))
2685 (let ((running (equal (bindat-get-field thread 'state) "running")))
2686 (add-to-list 'gdb-threads-list
2687 (cons (bindat-get-field thread 'id)
2690 (incf gdb-running-threads-count)
2691 (incf gdb-stopped-threads-count))
2693 (gdb-table-add-row table
2695 (bindat-get-field thread 'id)
2697 (if gdb-thread-buffer-verbose-names
2698 (concat (bindat-get-field thread 'target-id) " ") "")
2699 (bindat-get-field thread 'state)
2700 ;; Include frame information for stopped threads
2703 " in " (bindat-get-field thread 'frame 'func)
2704 (if gdb-thread-buffer-arguments
2707 (let ((args (bindat-get-field thread 'frame 'args)))
2710 (apply #'format "%s=%s"
2711 (gdb-get-many-fields arg 'name 'value)))
2715 (if gdb-thread-buffer-locations
2716 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2717 (if gdb-thread-buffer-addresses
2718 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2722 'mouse-face 'highlight
2723 'help-echo "mouse-2, RET: select thread")))
2724 (when (string-equal gdb-thread-number
2725 (bindat-get-field thread 'id))
2726 (setq marked-line (length gdb-threads-list))))
2727 (insert (gdb-table-string table " "))
2729 (gdb-mark-line marked-line gdb-thread-position)))
2730 ;; We update gud-running here because we need to make sure that
2731 ;; gdb-threads-list is up-to-date
2732 (gdb-update-gud-running)
2733 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2735 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2736 "Define a NAME command which will act upon thread on the current line.
2738 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2739 be the value of 'gdb-thread property of the current line. If
2740 'gdb-thread is nil, error is signaled."
2741 `(defun ,name (&optional event)
2743 (interactive (list last-input-event))
2744 (if event (posn-set-point (event-end event)))
2747 (let ((thread (get-text-property (point) 'gdb-thread)))
2750 (error "Not recognized as thread line"))))))
2752 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2754 "Define a NAME which will call BUFFER-COMMAND with id of thread
2755 on the current line."
2756 `(def-gdb-thread-buffer-command ,name
2757 (,buffer-command (bindat-get-field thread 'id))
2760 (def-gdb-thread-buffer-command gdb-select-thread
2761 (let ((new-id (bindat-get-field thread 'id)))
2762 (gdb-setq-thread-number new-id)
2763 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2765 "Select the thread at current line of threads buffer.")
2767 (def-gdb-thread-buffer-simple-command
2768 gdb-display-stack-for-thread
2769 gdb-preemptively-display-stack-buffer
2770 "Display stack buffer for the thread at current line.")
2772 (def-gdb-thread-buffer-simple-command
2773 gdb-display-locals-for-thread
2774 gdb-preemptively-display-locals-buffer
2775 "Display locals buffer for the thread at current line.")
2777 (def-gdb-thread-buffer-simple-command
2778 gdb-display-registers-for-thread
2779 gdb-preemptively-display-registers-buffer
2780 "Display registers buffer for the thread at current line.")
2782 (def-gdb-thread-buffer-simple-command
2783 gdb-display-disassembly-for-thread
2784 gdb-preemptively-display-disassembly-buffer
2785 "Display disassembly buffer for the thread at current line.")
2787 (def-gdb-thread-buffer-simple-command
2788 gdb-frame-stack-for-thread
2789 gdb-frame-stack-buffer
2790 "Display a new frame with stack buffer for the thread at
2793 (def-gdb-thread-buffer-simple-command
2794 gdb-frame-locals-for-thread
2795 gdb-frame-locals-buffer
2796 "Display a new frame with locals buffer for the thread at
2799 (def-gdb-thread-buffer-simple-command
2800 gdb-frame-registers-for-thread
2801 gdb-frame-registers-buffer
2802 "Display a new frame with registers buffer for the thread at
2805 (def-gdb-thread-buffer-simple-command
2806 gdb-frame-disassembly-for-thread
2807 gdb-frame-disassembly-buffer
2808 "Display a new frame with disassembly buffer for the thread at
2811 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2812 "Define a NAME which will execute GUD-COMMAND with
2813 `gdb-thread-number' locally bound to id of thread on the current
2815 `(def-gdb-thread-buffer-command ,name
2817 (let ((gdb-thread-number (bindat-get-field thread 'id))
2818 (gdb-gud-control-all-threads nil))
2819 (call-interactively #',gud-command))
2820 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2823 (def-gdb-thread-buffer-gud-command
2824 gdb-interrupt-thread
2826 "Interrupt thread at current line.")
2828 (def-gdb-thread-buffer-gud-command
2831 "Continue thread at current line.")
2833 (def-gdb-thread-buffer-gud-command
2836 "Step thread at current line.")
2841 (defcustom gdb-memory-rows 8
2842 "Number of data rows in memory window."
2847 (defcustom gdb-memory-columns 4
2848 "Number of data columns in memory window."
2853 (defcustom gdb-memory-format "x"
2854 "Display format of data items in memory window."
2855 :type '(choice (const :tag "Hexadecimal" "x")
2856 (const :tag "Signed decimal" "d")
2857 (const :tag "Unsigned decimal" "u")
2858 (const :tag "Octal" "o")
2859 (const :tag "Binary" "t"))
2863 (defcustom gdb-memory-unit 4
2864 "Unit size of data items in memory window."
2865 :type '(choice (const :tag "Byte" 1)
2866 (const :tag "Halfword" 2)
2867 (const :tag "Word" 4)
2868 (const :tag "Giant word" 8))
2872 (def-gdb-trigger-and-handler
2873 gdb-invalidate-memory
2874 (format "-data-read-memory %s %s %d %d %d"
2880 gdb-read-memory-handler
2881 gdb-read-memory-custom
2884 (gdb-set-buffer-rules
2886 'gdb-memory-buffer-name
2888 'gdb-invalidate-memory)
2890 (defun gdb-memory-column-width (size format)
2891 "Return length of string with memory unit of SIZE in FORMAT.
2893 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2894 in `gdb-memory-format'."
2895 (let ((format-base (cdr (assoc format
2897 ("d" . 10) ("u" . 10)
2901 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2902 (cond ((string-equal format "x")
2903 (+ 2 res)) ; hexadecimal numbers have 0x in front
2904 ((or (string-equal format "d")
2905 (string-equal format "o"))
2908 (error "Unknown format"))))
2910 (defun gdb-read-memory-custom ()
2911 (let* ((res (gdb-json-partial-output))
2912 (err-msg (bindat-get-field res 'msg)))
2914 (let ((memory (bindat-get-field res 'memory)))
2915 (setq gdb-memory-address (bindat-get-field res 'addr))
2916 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2917 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2918 (setq gdb-memory-last-address gdb-memory-address)
2919 (dolist (row memory)
2920 (insert (concat (bindat-get-field row 'addr) ":"))
2921 (dolist (column (bindat-get-field row 'data))
2922 (insert (gdb-pad-string column
2923 (+ 2 (gdb-memory-column-width
2925 gdb-memory-format)))))
2927 ;; Show last page instead of empty buffer when out of bounds
2929 (let ((gdb-memory-address gdb-memory-last-address))
2930 (gdb-invalidate-memory 'update)
2931 (error err-msg))))))
2933 (defvar gdb-memory-mode-map
2934 (let ((map (make-sparse-keymap)))
2935 (suppress-keymap map t)
2936 (define-key map "q" 'kill-this-buffer)
2937 (define-key map "n" 'gdb-memory-show-next-page)
2938 (define-key map "p" 'gdb-memory-show-previous-page)
2939 (define-key map "a" 'gdb-memory-set-address)
2940 (define-key map "t" 'gdb-memory-format-binary)
2941 (define-key map "o" 'gdb-memory-format-octal)
2942 (define-key map "u" 'gdb-memory-format-unsigned)
2943 (define-key map "d" 'gdb-memory-format-signed)
2944 (define-key map "x" 'gdb-memory-format-hexadecimal)
2945 (define-key map "b" 'gdb-memory-unit-byte)
2946 (define-key map "h" 'gdb-memory-unit-halfword)
2947 (define-key map "w" 'gdb-memory-unit-word)
2948 (define-key map "g" 'gdb-memory-unit-giant)
2949 (define-key map "R" 'gdb-memory-set-rows)
2950 (define-key map "C" 'gdb-memory-set-columns)
2953 (defun gdb-memory-set-address-event (event)
2954 "Handle a click on address field in memory buffer header."
2956 (save-selected-window
2957 (select-window (posn-window (event-start event)))
2958 (gdb-memory-set-address)))
2960 ;; Non-event version for use within keymap
2961 (defun gdb-memory-set-address ()
2962 "Set the start memory address."
2964 (let ((arg (read-from-minibuffer "Memory address: ")))
2965 (setq gdb-memory-address arg))
2966 (gdb-invalidate-memory 'update))
2968 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2969 "Define a function NAME which reads new VAR value from minibuffer."
2970 `(defun ,name (event)
2973 (save-selected-window
2974 (select-window (posn-window (event-start event)))
2975 (let* ((arg (read-from-minibuffer ,echo-string))
2976 (count (string-to-number arg)))
2978 (error "Positive number only")
2979 (customize-set-variable ',variable count)
2980 (gdb-invalidate-memory 'update))))))
2982 (def-gdb-set-positive-number
2986 "Set the number of data rows in memory window.")
2988 (def-gdb-set-positive-number
2989 gdb-memory-set-columns
2992 "Set the number of data columns in memory window.")
2994 (defmacro def-gdb-memory-format (name format doc)
2995 "Define a function NAME to switch memory buffer to use FORMAT.
2997 DOC is an optional documentation string."
2998 `(defun ,name () ,(when doc doc)
3000 (customize-set-variable 'gdb-memory-format ,format)
3001 (gdb-invalidate-memory 'update)))
3003 (def-gdb-memory-format
3004 gdb-memory-format-binary "t"
3005 "Set the display format to binary.")
3007 (def-gdb-memory-format
3008 gdb-memory-format-octal "o"
3009 "Set the display format to octal.")
3011 (def-gdb-memory-format
3012 gdb-memory-format-unsigned "u"
3013 "Set the display format to unsigned decimal.")
3015 (def-gdb-memory-format
3016 gdb-memory-format-signed "d"
3017 "Set the display format to decimal.")
3019 (def-gdb-memory-format
3020 gdb-memory-format-hexadecimal "x"
3021 "Set the display format to hexadecimal.")
3023 (defvar gdb-memory-format-map
3024 (let ((map (make-sparse-keymap)))
3025 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3027 "Keymap to select format in the header line.")
3029 (defvar gdb-memory-format-menu
3030 (let ((map (make-sparse-keymap "Format")))
3032 (define-key map [binary]
3033 '(menu-item "Binary" gdb-memory-format-binary
3034 :button (:radio . (equal gdb-memory-format "t"))))
3035 (define-key map [octal]
3036 '(menu-item "Octal" gdb-memory-format-octal
3037 :button (:radio . (equal gdb-memory-format "o"))))
3038 (define-key map [unsigned]
3039 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3040 :button (:radio . (equal gdb-memory-format "u"))))
3041 (define-key map [signed]
3042 '(menu-item "Signed Decimal" gdb-memory-format-signed
3043 :button (:radio . (equal gdb-memory-format "d"))))
3044 (define-key map [hexadecimal]
3045 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3046 :button (:radio . (equal gdb-memory-format "x"))))
3048 "Menu of display formats in the header line.")
3050 (defun gdb-memory-format-menu (event)
3052 (x-popup-menu event gdb-memory-format-menu))
3054 (defun gdb-memory-format-menu-1 (event)
3056 (save-selected-window
3057 (select-window (posn-window (event-start event)))
3058 (let* ((selection (gdb-memory-format-menu event))
3059 (binding (and selection (lookup-key gdb-memory-format-menu
3060 (vector (car selection))))))
3061 (if binding (call-interactively binding)))))
3063 (defmacro def-gdb-memory-unit (name unit-size doc)
3064 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3066 DOC is an optional documentation string."
3067 `(defun ,name () ,(when doc doc)
3069 (customize-set-variable 'gdb-memory-unit ,unit-size)
3070 (gdb-invalidate-memory 'update)))
3072 (def-gdb-memory-unit gdb-memory-unit-giant 8
3073 "Set the unit size to giant words (eight bytes).")
3075 (def-gdb-memory-unit gdb-memory-unit-word 4
3076 "Set the unit size to words (four bytes).")
3078 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3079 "Set the unit size to halfwords (two bytes).")
3081 (def-gdb-memory-unit gdb-memory-unit-byte 1
3082 "Set the unit size to bytes.")
3084 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3085 "Define a function NAME which show new address in memory buffer.
3087 The defined function switches Memory buffer to show address
3088 stored in ADDRESS-VAR variable.
3090 DOC is an optional documentation string."
3094 (let ((gdb-memory-address ,address-var))
3095 (gdb-invalidate-memory))))
3097 (def-gdb-memory-show-page gdb-memory-show-previous-page
3098 gdb-memory-prev-page)
3100 (def-gdb-memory-show-page gdb-memory-show-next-page
3101 gdb-memory-next-page)
3103 (defvar gdb-memory-unit-map
3104 (let ((map (make-sparse-keymap)))
3105 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3107 "Keymap to select units in the header line.")
3109 (defvar gdb-memory-unit-menu
3110 (let ((map (make-sparse-keymap "Unit")))
3111 (define-key map [giantwords]
3112 '(menu-item "Giant words" gdb-memory-unit-giant
3113 :button (:radio . (equal gdb-memory-unit 8))))
3114 (define-key map [words]
3115 '(menu-item "Words" gdb-memory-unit-word
3116 :button (:radio . (equal gdb-memory-unit 4))))
3117 (define-key map [halfwords]
3118 '(menu-item "Halfwords" gdb-memory-unit-halfword
3119 :button (:radio . (equal gdb-memory-unit 2))))
3120 (define-key map [bytes]
3121 '(menu-item "Bytes" gdb-memory-unit-byte
3122 :button (:radio . (equal gdb-memory-unit 1))))
3124 "Menu of units in the header line.")
3126 (defun gdb-memory-unit-menu (event)
3128 (x-popup-menu event gdb-memory-unit-menu))
3130 (defun gdb-memory-unit-menu-1 (event)
3132 (save-selected-window
3133 (select-window (posn-window (event-start event)))
3134 (let* ((selection (gdb-memory-unit-menu event))
3135 (binding (and selection (lookup-key gdb-memory-unit-menu
3136 (vector (car selection))))))
3137 (if binding (call-interactively binding)))))
3139 (defvar gdb-memory-font-lock-keywords
3140 '(;; <__function.name+n>
3141 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3142 (1 font-lock-function-name-face)))
3143 "Font lock keywords used in `gdb-memory-mode'.")
3145 (defvar gdb-memory-header
3150 'face font-lock-warning-face
3151 'help-echo "mouse-1: decrement address"
3152 'mouse-face 'mode-line-highlight
3153 'local-map (gdb-make-header-line-mouse-map
3155 #'gdb-memory-show-previous-page))
3158 'face font-lock-warning-face
3159 'help-echo "mouse-1: increment address"
3160 'mouse-face 'mode-line-highlight
3161 'local-map (gdb-make-header-line-mouse-map
3163 #'gdb-memory-show-next-page))
3165 (propertize gdb-memory-address
3166 'face font-lock-warning-face
3167 'help-echo "mouse-1: set start address"
3168 'mouse-face 'mode-line-highlight
3169 'local-map (gdb-make-header-line-mouse-map
3171 #'gdb-memory-set-address-event))
3173 (propertize (number-to-string gdb-memory-rows)
3174 'face font-lock-warning-face
3175 'help-echo "mouse-1: set number of columns"
3176 'mouse-face 'mode-line-highlight
3177 'local-map (gdb-make-header-line-mouse-map
3179 #'gdb-memory-set-rows))
3181 (propertize (number-to-string gdb-memory-columns)
3182 'face font-lock-warning-face
3183 'help-echo "mouse-1: set number of columns"
3184 'mouse-face 'mode-line-highlight
3185 'local-map (gdb-make-header-line-mouse-map
3187 #'gdb-memory-set-columns))
3189 (propertize gdb-memory-format
3190 'face font-lock-warning-face
3191 'help-echo "mouse-3: select display format"
3192 'mouse-face 'mode-line-highlight
3193 'local-map gdb-memory-format-map)
3195 (propertize (number-to-string gdb-memory-unit)
3196 'face font-lock-warning-face
3197 'help-echo "mouse-3: select unit size"
3198 'mouse-face 'mode-line-highlight
3199 'local-map gdb-memory-unit-map)))
3200 "Header line used in `gdb-memory-mode'.")
3202 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3203 "Major mode for examining memory."
3204 (setq header-line-format gdb-memory-header)
3205 (set (make-local-variable 'font-lock-defaults)
3206 '(gdb-memory-font-lock-keywords))
3207 'gdb-invalidate-memory)
3209 (defun gdb-memory-buffer-name ()
3210 (concat "*memory of " (gdb-get-target-string) "*"))
3212 (def-gdb-display-buffer
3213 gdb-display-memory-buffer
3215 "Display memory contents.")
3217 (defun gdb-frame-memory-buffer ()
3218 "Display memory contents in a new frame."
3220 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3221 (special-display-frame-alist
3225 ,@gdb-frame-parameters)))
3226 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3229 ;;; Disassembly view
3231 (defun gdb-disassembly-buffer-name ()
3232 (gdb-current-context-buffer-name
3233 (concat "disassembly of " (gdb-get-target-string))))
3235 (def-gdb-display-buffer
3236 gdb-display-disassembly-buffer
3237 'gdb-disassembly-buffer
3238 "Display disassembly for current stack frame.")
3240 (def-gdb-preempt-display-buffer
3241 gdb-preemptively-display-disassembly-buffer
3242 'gdb-disassembly-buffer)
3244 (def-gdb-frame-for-buffer
3245 gdb-frame-disassembly-buffer
3246 'gdb-disassembly-buffer
3247 "Display disassembly in a new frame.")
3249 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3250 (let* ((frame (gdb-current-buffer-frame))
3251 (file (bindat-get-field frame 'fullname))
3252 (line (bindat-get-field frame 'line)))
3254 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3255 gdb-disassembly-handler
3256 ;; We update disassembly only after we have actual frame information
3257 ;; about all threads, so no there's `update' signal in this list
3258 '(start update-disassembly))
3260 (def-gdb-auto-update-handler
3261 gdb-disassembly-handler
3262 gdb-invalidate-disassembly
3263 gdb-disassembly-handler-custom
3266 (gdb-set-buffer-rules
3267 'gdb-disassembly-buffer
3268 'gdb-disassembly-buffer-name
3269 'gdb-disassembly-mode
3270 'gdb-invalidate-disassembly)
3272 (defvar gdb-disassembly-font-lock-keywords
3273 '(;; <__function.name+n>
3274 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3275 (1 font-lock-function-name-face))
3276 ;; 0xNNNNNNNN <__function.name+n>: opcode
3277 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3278 (4 font-lock-keyword-face))
3279 ;; %register(at least i386)
3280 ("%\\sw+" . font-lock-variable-name-face)
3281 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3282 (1 font-lock-comment-face)
3283 (2 font-lock-function-name-face))
3284 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3285 "Font lock keywords used in `gdb-disassembly-mode'.")
3287 (defvar gdb-disassembly-mode-map
3289 (let ((map (make-sparse-keymap)))
3290 (suppress-keymap map)
3291 (define-key map "q" 'kill-this-buffer)
3294 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3295 "Major mode for GDB disassembly information."
3296 ;; TODO Rename overlay variable for disassembly mode
3297 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3298 (setq fringes-outside-margins t)
3299 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3300 (set (make-local-variable 'font-lock-defaults)
3301 '(gdb-disassembly-font-lock-keywords))
3302 'gdb-invalidate-disassembly)
3304 (defun gdb-disassembly-handler-custom ()
3305 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3306 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3307 (table (make-gdb-table))
3309 (dolist (instr instructions)
3310 (gdb-table-add-row table
3312 (bindat-get-field instr 'address)
3313 (apply #'format "<%s+%s>:"
3314 (gdb-get-many-fields instr 'func-name 'offset))
3315 (bindat-get-field instr 'inst)))
3316 (when (string-equal (bindat-get-field instr 'address)
3319 (setq marked-line (length (gdb-table-rows table)))
3320 (setq fringe-indicator-alist
3321 (if (string-equal gdb-frame-number "0")
3323 '((overlay-arrow . hollow-right-triangle)))))))
3324 (insert (gdb-table-string table " "))
3325 (gdb-disassembly-place-breakpoints)
3326 ;; Mark current position with overlay arrow and scroll window to
3329 (let ((window (get-buffer-window (current-buffer) 0)))
3330 (set-window-point window (gdb-mark-line marked-line
3331 gdb-disassembly-position))))
3333 (gdb-current-context-mode-name
3334 (concat "Disassembly: "
3335 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3337 (defun gdb-disassembly-place-breakpoints ()
3338 (gdb-remove-breakpoint-icons (point-min) (point-max))
3339 (dolist (breakpoint gdb-breakpoints-list)
3340 (let* ((breakpoint (cdr breakpoint))
3341 (bptno (bindat-get-field breakpoint 'number))
3342 (flag (bindat-get-field breakpoint 'enabled))
3343 (address (bindat-get-field breakpoint 'addr)))
3345 (goto-char (point-min))
3346 (if (re-search-forward (concat "^" address) nil t)
3347 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3350 (defvar gdb-breakpoints-header
3352 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3355 (gdb-propertize-header "Threads" gdb-threads-buffer
3356 "mouse-1: select" mode-line-highlight
3357 mode-line-inactive)))
3359 ;;; Breakpoints view
3360 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3361 "Major mode for gdb breakpoints."
3362 (setq header-line-format gdb-breakpoints-header)
3363 'gdb-invalidate-breakpoints)
3365 (defun gdb-toggle-breakpoint ()
3366 "Enable/disable breakpoint at current line of breakpoints buffer."
3370 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3373 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3376 (bindat-get-field breakpoint 'number)))
3377 (error "Not recognized as break/watchpoint line")))))
3379 (defun gdb-delete-breakpoint ()
3380 "Delete the breakpoint at current line of breakpoints buffer."
3384 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3386 (gud-basic-call (concat "-break-delete "
3387 (bindat-get-field breakpoint 'number)))
3388 (error "Not recognized as break/watchpoint line")))))
3390 (defun gdb-goto-breakpoint (&optional event)
3391 "Go to the location of breakpoint at current line of
3392 breakpoints buffer."
3393 (interactive (list last-input-event))
3394 (if event (posn-set-point (event-end event)))
3395 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3396 (let ((window (get-buffer-window gud-comint-buffer)))
3397 (if window (save-selected-window (select-window window))))
3400 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3402 (let ((bptno (bindat-get-field breakpoint 'number))
3403 (file (bindat-get-field breakpoint 'fullname))
3404 (line (bindat-get-field breakpoint 'line)))
3405 (save-selected-window
3406 (let* ((buffer (find-file-noselect
3407 (if (file-exists-p file) file
3408 (cdr (assoc bptno gdb-location-alist)))))
3409 (window (or (gdb-display-source-buffer buffer)
3410 (display-buffer buffer))))
3411 (setq gdb-source-window window)
3412 (with-current-buffer buffer
3413 (goto-char (point-min))
3414 (forward-line (1- (string-to-number line)))
3415 (set-window-point window (point))))))
3416 (error "Not recognized as break/watchpoint line")))))
3419 ;; Frames buffer. This displays a perpetually correct bactrack trace.
3421 (def-gdb-trigger-and-handler
3422 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3423 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3426 (gdb-set-buffer-rules
3428 'gdb-stack-buffer-name
3430 'gdb-invalidate-frames)
3432 (defun gdb-frame-location (frame)
3433 "Return \" of file:line\" or \" of library\" for structure FRAME.
3435 FRAME must have either \"file\" and \"line\" members or \"from\"
3437 (let ((file (bindat-get-field frame 'file))
3438 (line (bindat-get-field frame 'line))
3439 (from (bindat-get-field frame 'from)))
3440 (let ((res (or (and file line (concat file ":" line))
3442 (if res (concat " of " res) ""))))
3444 (defun gdb-stack-list-frames-custom ()
3445 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3446 (table (make-gdb-table)))
3447 (set-marker gdb-stack-position nil)
3448 (dolist (frame stack)
3449 (gdb-table-add-row table
3451 (bindat-get-field frame 'level)
3454 (bindat-get-field frame 'func)
3455 (if gdb-stack-buffer-locations
3456 (gdb-frame-location frame) "")
3457 (if gdb-stack-buffer-addresses
3458 (concat " at " (bindat-get-field frame 'addr)) "")))
3459 `(mouse-face highlight
3460 help-echo "mouse-2, RET: Select frame"
3462 (insert (gdb-table-string table " ")))
3463 (when (and gdb-frame-number
3464 (gdb-buffer-shows-main-thread-p))
3465 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3466 gdb-stack-position))
3468 (gdb-current-context-mode-name "Frames")))
3470 (defun gdb-stack-buffer-name ()
3471 (gdb-current-context-buffer-name
3472 (concat "stack frames of " (gdb-get-target-string))))
3474 (def-gdb-display-buffer
3475 gdb-display-stack-buffer
3477 "Display backtrace of current stack.")
3479 (def-gdb-preempt-display-buffer
3480 gdb-preemptively-display-stack-buffer
3481 'gdb-stack-buffer nil t)
3483 (def-gdb-frame-for-buffer
3484 gdb-frame-stack-buffer
3486 "Display backtrace of current stack in a new frame.")
3488 (defvar gdb-frames-mode-map
3489 (let ((map (make-sparse-keymap)))
3490 (suppress-keymap map)
3491 (define-key map "q" 'kill-this-buffer)
3492 (define-key map "\r" 'gdb-select-frame)
3493 (define-key map [mouse-2] 'gdb-select-frame)
3494 (define-key map [follow-link] 'mouse-face)
3497 (defvar gdb-frames-font-lock-keywords
3498 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3499 "Font lock keywords used in `gdb-frames-mode'.")
3501 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3502 "Major mode for gdb call stack."
3503 (setq gdb-stack-position (make-marker))
3504 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3505 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3506 (set (make-local-variable 'font-lock-defaults)
3507 '(gdb-frames-font-lock-keywords))
3508 'gdb-invalidate-frames)
3510 (defun gdb-select-frame (&optional event)
3511 "Select the frame and display the relevant source."
3512 (interactive (list last-input-event))
3513 (if event (posn-set-point (event-end event)))
3514 (let ((frame (get-text-property (point) 'gdb-frame)))
3516 (if (gdb-buffer-shows-main-thread-p)
3517 (let ((new-level (bindat-get-field frame 'level)))
3518 (setq gdb-frame-number new-level)
3519 (gdb-input (list (concat "-stack-select-frame " new-level)
3522 (error "Could not select frame for non-current thread"))
3523 (error "Not recognized as frame line"))))
3527 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3528 (def-gdb-trigger-and-handler
3529 gdb-invalidate-locals
3530 (concat (gdb-current-context-command "-stack-list-locals")
3532 gdb-locals-handler gdb-locals-handler-custom
3535 (gdb-set-buffer-rules
3537 'gdb-locals-buffer-name
3539 'gdb-invalidate-locals)
3541 (defvar gdb-locals-watch-map
3542 (let ((map (make-sparse-keymap)))
3543 (suppress-keymap map)
3544 (define-key map "\r" 'gud-watch)
3545 (define-key map [mouse-2] 'gud-watch)
3547 "Keymap to create watch expression of a complex data type local variable.")
3549 (defvar gdb-edit-locals-map-1
3550 (let ((map (make-sparse-keymap)))
3551 (suppress-keymap map)
3552 (define-key map "\r" 'gdb-edit-locals-value)
3553 (define-key map [mouse-2] 'gdb-edit-locals-value)
3555 "Keymap to edit value of a simple data type local variable.")
3557 (defun gdb-edit-locals-value (&optional event)
3558 "Assign a value to a variable displayed in the locals buffer."
3559 (interactive (list last-input-event))
3561 (if event (posn-set-point (event-end event)))
3563 (let* ((var (bindat-get-field
3564 (get-text-property (point) 'gdb-local-variable) 'name))
3565 (value (read-string (format "New value (%s): " var))))
3567 (concat "-gdb-set variable " var " = " value)))))
3569 ;; Dont display values of arrays or structures.
3570 ;; These can be expanded using gud-watch.
3571 (defun gdb-locals-handler-custom ()
3572 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3573 (table (make-gdb-table)))
3574 (dolist (local locals-list)
3575 (let ((name (bindat-get-field local 'name))
3576 (value (bindat-get-field local 'value))
3577 (type (bindat-get-field local 'type)))
3579 (string-match "\\0x" value))
3580 (add-text-properties 0 (length name)
3581 `(mouse-face highlight
3582 help-echo "mouse-2: create watch expression"
3583 local-map ,gdb-locals-watch-map)
3585 (add-text-properties 0 (length value)
3586 `(mouse-face highlight
3587 help-echo "mouse-2: edit value"
3588 local-map ,gdb-edit-locals-map-1)
3593 (propertize type 'font-lock-face font-lock-type-face)
3594 (propertize name 'font-lock-face font-lock-variable-name-face)
3596 `(gdb-local-variable ,local))))
3597 (insert (gdb-table-string table " "))
3599 (gdb-current-context-mode-name
3601 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3603 (defvar gdb-locals-header
3605 (gdb-propertize-header "Locals" gdb-locals-buffer
3608 (gdb-propertize-header "Registers" gdb-registers-buffer
3609 "mouse-1: select" mode-line-highlight
3610 mode-line-inactive)))
3612 (defvar gdb-locals-mode-map
3613 (let ((map (make-sparse-keymap)))
3614 (suppress-keymap map)
3615 (define-key map "q" 'kill-this-buffer)
3616 (define-key map "\t" (lambda ()
3618 (gdb-set-window-buffer
3619 (gdb-get-buffer-create
3620 'gdb-registers-buffer
3621 gdb-thread-number) t)))
3624 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3625 "Major mode for gdb locals."
3626 (setq header-line-format gdb-locals-header)
3627 'gdb-invalidate-locals)
3629 (defun gdb-locals-buffer-name ()
3630 (gdb-current-context-buffer-name
3631 (concat "locals of " (gdb-get-target-string))))
3633 (def-gdb-display-buffer
3634 gdb-display-locals-buffer
3636 "Display local variables of current stack and their values.")
3638 (def-gdb-preempt-display-buffer
3639 gdb-preemptively-display-locals-buffer
3640 'gdb-locals-buffer nil t)
3642 (def-gdb-frame-for-buffer
3643 gdb-frame-locals-buffer
3645 "Display local variables of current stack and their values in a new frame.")
3648 ;; Registers buffer.
3650 (def-gdb-trigger-and-handler
3651 gdb-invalidate-registers
3652 (concat (gdb-current-context-command "-data-list-register-values") " x")
3653 gdb-registers-handler
3654 gdb-registers-handler-custom
3657 (gdb-set-buffer-rules
3658 'gdb-registers-buffer
3659 'gdb-registers-buffer-name
3661 'gdb-invalidate-registers)
3663 (defun gdb-registers-handler-custom ()
3664 (when gdb-register-names
3665 (let ((register-values
3666 (bindat-get-field (gdb-json-partial-output) 'register-values))
3667 (table (make-gdb-table)))
3668 (dolist (register register-values)
3669 (let* ((register-number (bindat-get-field register 'number))
3670 (value (bindat-get-field register 'value))
3671 (register-name (nth (string-to-number register-number)
3672 gdb-register-names)))
3676 (propertize register-name
3677 'font-lock-face font-lock-variable-name-face)
3678 (if (member register-number gdb-changed-registers)
3679 (propertize value 'font-lock-face font-lock-warning-face)
3681 `(mouse-face highlight
3682 help-echo "mouse-2: edit value"
3683 gdb-register-name ,register-name))))
3684 (insert (gdb-table-string table " ")))
3686 (gdb-current-context-mode-name "Registers"))))
3688 (defun gdb-edit-register-value (&optional event)
3689 "Assign a value to a register displayed in the registers buffer."
3690 (interactive (list last-input-event))
3692 (if event (posn-set-point (event-end event)))
3694 (let* ((var (bindat-get-field
3695 (get-text-property (point) 'gdb-register-name)))
3696 (value (read-string (format "New value (%s): " var))))
3698 (concat "-gdb-set variable $" var " = " value)))))
3700 (defvar gdb-registers-mode-map
3701 (let ((map (make-sparse-keymap)))
3702 (suppress-keymap map)
3703 (define-key map "\r" 'gdb-edit-register-value)
3704 (define-key map [mouse-2] 'gdb-edit-register-value)
3705 (define-key map "q" 'kill-this-buffer)
3706 (define-key map "\t" (lambda ()
3708 (gdb-set-window-buffer
3709 (gdb-get-buffer-create
3711 gdb-thread-number) t)))
3714 (defvar gdb-registers-header
3716 (gdb-propertize-header "Locals" gdb-locals-buffer
3717 "mouse-1: select" mode-line-highlight
3720 (gdb-propertize-header "Registers" gdb-registers-buffer
3721 nil nil mode-line)))
3723 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3724 "Major mode for gdb registers."
3725 (setq header-line-format gdb-registers-header)
3726 'gdb-invalidate-registers)
3728 (defun gdb-registers-buffer-name ()
3729 (gdb-current-context-buffer-name
3730 (concat "registers of " (gdb-get-target-string))))
3732 (def-gdb-display-buffer
3733 gdb-display-registers-buffer
3734 'gdb-registers-buffer
3735 "Display integer register contents.")
3737 (def-gdb-preempt-display-buffer
3738 gdb-preemptively-display-registers-buffer
3739 'gdb-registers-buffer nil t)
3741 (def-gdb-frame-for-buffer
3742 gdb-frame-registers-buffer
3743 'gdb-registers-buffer
3744 "Display integer register contents in a new frame.")
3746 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3747 (defun gdb-get-changed-registers ()
3748 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3749 (not (gdb-pending-p 'gdb-get-changed-registers)))
3753 "-data-list-changed-registers"
3754 'gdb-changed-registers-handler))
3755 (gdb-add-pending 'gdb-get-changed-registers))))
3757 (defun gdb-changed-registers-handler ()
3758 (gdb-delete-pending 'gdb-get-changed-registers)
3759 (setq gdb-changed-registers nil)
3760 (dolist (register-number
3761 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3762 (push register-number gdb-changed-registers)))
3764 (defun gdb-register-names-handler ()
3765 ;; Don't use gdb-pending-triggers because this handler is called
3766 ;; only once (in gdb-init-1)
3767 (setq gdb-register-names nil)
3768 (dolist (register-name
3769 (bindat-get-field (gdb-json-partial-output) 'register-names))
3770 (push register-name gdb-register-names))
3771 (setq gdb-register-names (reverse gdb-register-names)))
3774 (defun gdb-get-source-file-list ()
3775 "Create list of source files for current GDB session.
3776 If buffers already exist for any of these files, gud-minor-mode
3778 (goto-char (point-min))
3779 (while (re-search-forward gdb-source-file-regexp nil t)
3780 (push (match-string 1) gdb-source-file-list))
3781 (dolist (buffer (buffer-list))
3782 (with-current-buffer buffer
3783 (when (member buffer-file-name gdb-source-file-list)
3784 (gdb-init-buffer))))
3785 (gdb-force-mode-line-update
3786 (propertize "ready" 'face font-lock-variable-name-face)))
3788 (defun gdb-get-main-selected-frame ()
3789 "Trigger for `gdb-frame-handler' which uses main current
3790 thread. Called from `gdb-update'."
3791 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3794 (list (gdb-current-context-command "-stack-info-frame")
3795 'gdb-frame-handler))
3796 (gdb-add-pending 'gdb-get-main-selected-frame))))
3798 (defun gdb-frame-handler ()
3799 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3800 overlay arrow in source buffer."
3801 (gdb-delete-pending 'gdb-get-main-selected-frame)
3802 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3804 (setq gdb-selected-frame (bindat-get-field frame 'func))
3805 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3806 (setq gdb-frame-number (bindat-get-field frame 'level))
3807 (setq gdb-frame-address (bindat-get-field frame 'addr))
3808 (let ((line (bindat-get-field frame 'line)))
3809 (setq gdb-selected-line (and line (string-to-number line)))
3810 (when (and gdb-selected-file gdb-selected-line)
3811 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3812 (gud-display-frame)))
3813 (if gud-overlay-arrow-position
3814 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3815 (position (marker-position gud-overlay-arrow-position)))
3817 (with-current-buffer buffer
3818 (setq fringe-indicator-alist
3819 (if (string-equal gdb-frame-number "0")
3821 '((overlay-arrow . hollow-right-triangle))))
3822 (setq gud-overlay-arrow-position (make-marker))
3823 (set-marker gud-overlay-arrow-position position))))))))
3825 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3827 (defun gdb-get-prompt ()
3828 "Find prompt for GDB session."
3829 (goto-char (point-min))
3830 (setq gdb-prompt-name nil)
3831 (re-search-forward gdb-prompt-name-regexp nil t)
3832 (setq gdb-prompt-name (match-string 1))
3833 ;; Insert first prompt.
3834 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3836 ;;;; Window management
3837 (defun gdb-display-buffer (buf dedicated &optional frame)
3840 If BUF is already displayed in some window, show it, deiconifying
3841 the frame if necessary. Otherwise, find least recently used
3842 window and show BUF there, if the window is not used for GDB
3843 already, in which case that window is splitted first."
3844 (let ((answer (get-buffer-window buf (or frame 0))))
3846 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3847 (let ((window (get-lru-window)))
3848 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3850 (let ((largest (get-largest-window)))
3851 (setq answer (split-window largest))
3852 (set-window-buffer answer buf)
3853 (set-window-dedicated-p answer dedicated)
3855 (set-window-buffer window buf)
3858 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3859 "Find window displaying a buffer with the same
3860 `gdb-buffer-type' as BUF and show BUF there. If no such window
3861 exists, just call `gdb-display-buffer' for BUF. If the window
3862 found is already dedicated, split window according to
3863 SPLIT-HORIZONTAL and show BUF in the new window."
3865 (when (not (get-buffer-window buf))
3866 (let* ((buf-type (gdb-buffer-type buf))
3868 (get-window-with-predicate
3871 (gdb-buffer-type (window-buffer w)))
3872 (not (window-dedicated-p w)))))))
3874 (set-window-buffer existing-window buf)
3875 (let ((dedicated-window
3876 (get-window-with-predicate
3879 (gdb-buffer-type (window-buffer w)))))))
3880 (if dedicated-window
3882 (split-window dedicated-window nil split-horizontal) buf)
3883 (gdb-display-buffer buf t))))))
3884 (error "Null buffer")))
3886 ;;; Shared keymap initialization:
3888 (let ((menu (make-sparse-keymap "GDB-Windows")))
3889 (define-key gud-menu-map [displays]
3890 `(menu-item "GDB-Windows" ,menu
3891 :visible (eq gud-minor-mode 'gdbmi)))
3892 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3893 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3894 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3895 (define-key menu [disassembly]
3896 '("Disassembly" . gdb-display-disassembly-buffer))
3897 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3898 (define-key menu [inferior]
3899 '("IO" . gdb-display-io-buffer))
3900 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3901 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3902 (define-key menu [breakpoints]
3903 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3905 (let ((menu (make-sparse-keymap "GDB-Frames")))
3906 (define-key gud-menu-map [frames]
3907 `(menu-item "GDB-Frames" ,menu
3908 :visible (eq gud-minor-mode 'gdbmi)))
3909 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3910 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3911 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3912 (define-key menu [disassembly]
3913 '("Disassembly" . gdb-frame-disassembly-buffer))
3914 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3915 (define-key menu [inferior]
3916 '("IO" . gdb-frame-io-buffer))
3917 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3918 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3919 (define-key menu [breakpoints]
3920 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3922 (let ((menu (make-sparse-keymap "GDB-MI")))
3923 (define-key menu [gdb-customize]
3924 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3925 :help "Customize Gdb Graphical Mode options."))
3926 (define-key menu [gdb-many-windows]
3927 '(menu-item "Display Other Windows" gdb-many-windows
3928 :help "Toggle display of locals, stack and breakpoint information"
3929 :button (:toggle . gdb-many-windows)))
3930 (define-key menu [gdb-restore-windows]
3931 '(menu-item "Restore Window Layout" gdb-restore-windows
3932 :help "Restore standard layout for debug session."))
3933 (define-key menu [sep1]
3935 (define-key menu [all-threads]
3936 '(menu-item "GUD controls all threads"
3939 (setq gdb-gud-control-all-threads t))
3940 :help "GUD start/stop commands apply to all threads"
3941 :button (:radio . gdb-gud-control-all-threads)))
3942 (define-key menu [current-thread]
3943 '(menu-item "GUD controls current thread"
3946 (setq gdb-gud-control-all-threads nil))
3947 :help "GUD start/stop commands apply to current thread only"
3948 :button (:radio . (not gdb-gud-control-all-threads))))
3949 (define-key menu [sep2]
3951 (define-key menu [gdb-customize-reasons]
3952 '(menu-item "Customize switching..."
3955 (customize-option 'gdb-switch-reasons))))
3956 (define-key menu [gdb-switch-when-another-stopped]
3957 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
3958 gdb-switch-when-another-stopped
3959 "Automatically switch to stopped thread"
3960 "GDB thread switching %s"
3961 "Switch to stopped thread"))
3962 (define-key gud-menu-map [mi]
3963 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3965 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3966 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3967 ;; tool-bar-local-item-from-menu here because it appends new buttons
3968 ;; to toolbar from right to left while we want our A/T throttle to
3969 ;; show up right before Run button.
3970 (define-key-after gud-tool-bar-map [all-threads]
3971 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3972 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3973 :visible (and (eq gud-minor-mode 'gdbmi)
3975 (not gdb-gud-control-all-threads)))
3978 (define-key-after gud-tool-bar-map [current-thread]
3979 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3980 :image (find-image '((:type xpm :file "gud/all.xpm")))
3981 :visible (and (eq gud-minor-mode 'gdbmi)
3983 gdb-gud-control-all-threads))
3986 (defun gdb-frame-gdb-buffer ()
3987 "Display GUD buffer in a new frame."
3989 (display-buffer-other-frame gud-comint-buffer))
3991 (defun gdb-display-gdb-buffer ()
3992 "Display GUD buffer."
3994 (pop-to-buffer gud-comint-buffer nil 0))
3996 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
3997 "Set buffer of selected window to NAME and dedicate window.
3999 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4000 window is dedicated."
4001 (unless window (setq window (selected-window)))
4002 (when ignore-dedicated
4003 (set-window-dedicated-p window nil))
4004 (set-window-buffer window (get-buffer name))
4005 (set-window-dedicated-p window t))
4007 (defun gdb-setup-windows ()
4008 "Layout the window pattern for `gdb-many-windows'."
4009 (gdb-display-locals-buffer)
4010 (gdb-display-stack-buffer)
4011 (delete-other-windows)
4012 (gdb-display-breakpoints-buffer)
4013 (delete-other-windows)
4015 (switch-to-buffer gud-comint-buffer)
4016 (let ((win0 (selected-window))
4017 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4018 (win2 (split-window nil ( / (window-height) 3)))
4019 (win3 (split-window-horizontally)))
4020 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4021 (select-window win2)
4024 (if gud-last-last-frame
4025 (gud-find-file (car gud-last-last-frame))
4027 (gud-find-file gdb-main-file)
4028 ;; Put buffer list in window if we
4029 ;; can't find a source file.
4030 (list-buffers-noselect))))
4031 (setq gdb-source-window (selected-window))
4032 (let ((win4 (split-window-horizontally)))
4033 (gdb-set-window-buffer
4034 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4035 (select-window win1)
4036 (gdb-set-window-buffer (gdb-stack-buffer-name))
4037 (let ((win5 (split-window-horizontally)))
4038 (gdb-set-window-buffer (if gdb-show-threads-by-default
4039 (gdb-threads-buffer-name)
4040 (gdb-breakpoints-buffer-name))
4042 (select-window win0)))
4044 (defcustom gdb-many-windows nil
4045 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4046 In this case it starts with two windows: one displaying the GUD
4047 buffer and the other with the source file with the main routine
4048 of the debugged program. Non-nil means display the layout shown for
4054 (defun gdb-many-windows (arg)
4055 "Toggle the number of windows in the basic arrangement.
4056 With arg, display additional buffers iff arg is positive."
4058 (setq gdb-many-windows
4060 (not gdb-many-windows)
4061 (> (prefix-numeric-value arg) 0)))
4062 (message (format "Display of other windows %sabled"
4063 (if gdb-many-windows "en" "dis")))
4064 (if (and gud-comint-buffer
4065 (buffer-name gud-comint-buffer))
4067 (gdb-restore-windows)
4070 (defun gdb-restore-windows ()
4071 "Restore the basic arrangement of windows used by gdb.
4072 This arrangement depends on the value of `gdb-many-windows'."
4074 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4075 (delete-other-windows)
4076 (if gdb-many-windows
4078 (when (or gud-last-last-frame gdb-show-main)
4079 (let ((win (split-window)))
4082 (if gud-last-last-frame
4083 (gud-find-file (car gud-last-last-frame))
4084 (gud-find-file gdb-main-file)))
4085 (setq gdb-source-window win)))))
4088 "Exit a debugging session cleanly.
4089 Kills the gdb buffers, and resets variables and the source buffers."
4090 (dolist (buffer (buffer-list))
4091 (unless (eq buffer gud-comint-buffer)
4092 (with-current-buffer buffer
4093 (if (eq gud-minor-mode 'gdbmi)
4094 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4096 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4097 (setq gud-minor-mode nil)
4098 (kill-local-variable 'tool-bar-map)
4099 (kill-local-variable 'gdb-define-alist))))))
4100 (setq gdb-disassembly-position nil)
4101 (setq overlay-arrow-variable-list
4102 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4103 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4104 (setq gdb-stack-position nil)
4105 (setq overlay-arrow-variable-list
4106 (delq 'gdb-stack-position overlay-arrow-variable-list))
4107 (setq gdb-thread-position nil)
4108 (setq overlay-arrow-variable-list
4109 (delq 'gdb-thread-position overlay-arrow-variable-list))
4110 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4111 (setq gud-running nil)
4112 (setq gdb-active-process nil)
4113 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4115 (defun gdb-get-source-file ()
4116 "Find the source file where the program starts and display it with related
4117 buffers, if required."
4118 (goto-char (point-min))
4119 (if (re-search-forward gdb-source-file-regexp nil t)
4120 (setq gdb-main-file (match-string 1)))
4121 (if gdb-many-windows
4123 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4125 (let ((pop-up-windows t))
4126 (display-buffer (gud-find-file gdb-main-file))))))
4129 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4130 "Put string PUTSTRING in front of POS in the current buffer.
4131 PUTSTRING is displayed by putting an overlay into the current buffer with a
4132 `before-string' string that has a `display' property whose value is
4134 (let ((string (make-string 1 ?x))
4135 (buffer (current-buffer)))
4136 (setq putstring (copy-sequence putstring))
4137 (let ((overlay (make-overlay pos pos buffer))
4139 (list (list 'margin 'left-margin) putstring))))
4140 (put-text-property 0 1 'display prop string)
4142 (add-text-properties 0 1 sprops string))
4143 (overlay-put overlay 'put-break t)
4144 (overlay-put overlay 'before-string string))))
4146 ;;from remove-images
4147 (defun gdb-remove-strings (start end &optional buffer)
4148 "Remove strings between START and END in BUFFER.
4149 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4150 BUFFER nil or omitted means use the current buffer."
4152 (setq buffer (current-buffer)))
4153 (dolist (overlay (overlays-in start end))
4154 (when (overlay-get overlay 'put-break)
4155 (delete-overlay overlay))))
4157 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4158 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4159 (start (- (car posns) 1))
4160 (end (+ (cdr posns) 1))
4161 (putstring (if enabled "B" "b"))
4162 (source-window (get-buffer-window (current-buffer) 0)))
4163 (add-text-properties
4164 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4167 (add-text-properties
4168 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4169 (add-text-properties
4170 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4171 (gdb-remove-breakpoint-icons start end)
4172 (if (display-images-p)
4173 (if (>= (or left-fringe-width
4174 (if source-window (car (window-fringes source-window)))
4175 gdb-buffer-fringe-width) 8)
4178 `(left-fringe breakpoint
4181 'breakpoint-disabled))
4183 'gdb-enabled enabled)
4184 (when (< left-margin-width 2)
4185 (save-current-buffer
4186 (setq left-margin-width 2)
4190 left-margin-width right-margin-width))))
4193 (or breakpoint-enabled-icon
4194 (setq breakpoint-enabled-icon
4195 (find-image `((:type xpm :data
4196 ,breakpoint-xpm-data
4197 :ascent 100 :pointer hand)
4199 ,breakpoint-enabled-pbm-data
4200 :ascent 100 :pointer hand)))))
4201 (or breakpoint-disabled-icon
4202 (setq breakpoint-disabled-icon
4203 (find-image `((:type xpm :data
4204 ,breakpoint-xpm-data
4205 :conversion disabled
4206 :ascent 100 :pointer hand)
4208 ,breakpoint-disabled-pbm-data
4209 :ascent 100 :pointer hand))))))
4213 (when (< left-margin-width 2)
4214 (save-current-buffer
4215 (setq left-margin-width 2)
4216 (let ((window (get-buffer-window (current-buffer) 0)))
4219 window left-margin-width right-margin-width)))))
4221 (propertize putstring
4223 'breakpoint-enabled 'breakpoint-disabled))
4226 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4227 (gdb-remove-strings start end)
4228 (if (display-images-p)
4229 (remove-images start end))
4231 (setq left-margin-width 0)
4232 (let ((window (get-buffer-window (current-buffer) 0)))
4235 window left-margin-width right-margin-width)))))
4239 ;;; gdb-mi.el ends here