1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007, 2008, 2009 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?
109 (defvar tool-bar-map)
110 (defvar speedbar-initial-expansion-list-name)
111 (defvar speedbar-frame)
113 (defvar gdb-memory-address "main")
114 (defvar gdb-memory-last-address nil
115 "Last successfully accessed memory address.")
116 (defvar gdb-memory-next-page nil
117 "Address of next memory page for program memory buffer.")
118 (defvar gdb-memory-prev-page nil
119 "Address of previous memory page for program memory buffer.")
121 (defvar gdb-thread-number nil
122 "Main current thread.
124 Invalidation triggers use this variable to query GDB for
125 information on the specified thread by wrapping GDB/MI commands
126 in `gdb-current-context-command'.
128 This variable may be updated implicitly by GDB via `gdb-stopped'
129 or explicitly by `gdb-select-thread'.
131 Only `gdb-setq-thread-number' should be used to change this
134 (defvar gdb-frame-number nil
135 "Selected frame level for main current thread.
137 Updated according to the following rules:
139 When a thread is selected or current thread stops, set to \"0\".
141 When current thread goes running (and possibly exits eventually),
144 May be manually changed by user with `gdb-select-frame'.")
146 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
148 ;; Used to show overlay arrow in source buffer. All set in
149 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
150 ;; these but rely on buffer-local thread information instead.
151 (defvar gdb-selected-frame nil
152 "Name of selected function for main current thread.")
153 (defvar gdb-selected-file nil
154 "Name of selected file for main current thread.")
155 (defvar gdb-selected-line nil
156 "Number of selected line for main current thread.")
158 (defvar gdb-threads-list nil
159 "Associative list of threads provided by \"-thread-info\" MI command.
161 Keys are thread numbers (in strings) and values are structures as
162 returned from -thread-info by `gdb-json-partial-output'. Updated in
163 `gdb-thread-list-handler-custom'.")
165 (defvar gdb-running-threads-count nil
166 "Number of currently running threads.
168 Nil means that no information is available.
170 Updated in `gdb-thread-list-handler-custom'.")
172 (defvar gdb-stopped-threads-count nil
173 "Number of currently stopped threads.
175 See also `gdb-running-threads-count'.")
177 (defvar gdb-breakpoints-list nil
178 "Associative list of breakpoints provided by \"-break-list\" MI command.
180 Keys are breakpoint numbers (in string) and values are structures
181 as returned from \"-break-list\" by `gdb-json-partial-output'
182 \(\"body\" field is used). Updated in
183 `gdb-breakpoints-list-handler-custom'.")
185 (defvar gdb-current-language nil)
186 (defvar gdb-var-list nil
187 "List of variables in watch window.
188 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
189 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
190 address for root variables.")
191 (defvar gdb-main-file nil "Source file from which program execution begins.")
193 ;; Overlay arrow markers
194 (defvar gdb-stack-position nil)
195 (defvar gdb-thread-position nil)
196 (defvar gdb-disassembly-position nil)
198 (defvar gdb-location-alist nil
199 "Alist of breakpoint numbers and full filenames. Only used for files that
201 (defvar gdb-active-process nil
202 "GUD tooltips display variable values when t, and macro definitions otherwise.")
203 (defvar gdb-error "Non-nil when GDB is reporting an error.")
204 (defvar gdb-macro-info nil
205 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
206 (defvar gdb-register-names nil "List of register names.")
207 (defvar gdb-changed-registers nil
208 "List of changed register numbers (strings).")
209 (defvar gdb-buffer-fringe-width nil)
210 (defvar gdb-last-command nil)
211 (defvar gdb-prompt-name nil)
212 (defvar gdb-token-number 0)
213 (defvar gdb-handler-alist '())
214 (defvar gdb-handler-number nil)
215 (defvar gdb-source-file-list nil
216 "List of source files for the current executable.")
217 (defvar gdb-first-done-or-error t)
218 (defvar gdb-source-window nil)
219 (defvar gdb-inferior-status nil)
220 (defvar gdb-continuation nil)
221 (defvar gdb-filter-output nil
222 "Message to be shown in GUD console.
224 This variable is updated in `gdb-done-or-error' and returned by
225 `gud-gdbmi-marker-filter'.")
227 (defvar gdb-non-stop nil
228 "Indicates whether current GDB session is using non-stop mode.
230 It is initialized to `gdb-non-stop-setting' at the beginning of
233 (defvar gdb-buffer-type nil
234 "One of the symbols bound in `gdb-buffer-rules'.")
235 (make-variable-buffer-local 'gdb-buffer-type)
237 (defvar gdb-output-sink 'nil
238 "The disposition of the output of the current gdb command.
239 Possible values are these symbols:
241 `user' -- gdb output should be copied to the GUD buffer
244 `emacs' -- output should be collected in the partial-output-buffer
245 for subsequent processing by a command. This is the
246 disposition of output generated by commands that
247 gdb mode sends to gdb on its own behalf.")
249 ;; Pending triggers prevent congestion: Emacs won't send two similar
250 ;; consecutive requests.
252 (defvar gdb-pending-triggers '()
253 "A list of trigger functions which have not yet been handled.
255 Elements are either function names or pairs (buffer . function)")
257 (defmacro gdb-add-pending (item)
258 `(push ,item gdb-pending-triggers))
259 (defmacro gdb-pending-p (item)
260 `(member ,item gdb-pending-triggers))
261 (defmacro gdb-delete-pending (item)
262 `(setq gdb-pending-triggers
263 (delete ,item gdb-pending-triggers)))
265 (defmacro gdb-wait-for-pending (&rest body)
266 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
268 This function checks `gdb-pending-triggers' value every
269 `gdb-wait-for-pending' seconds."
273 (if (not gdb-pending-triggers)
275 (gdb-wait-for-pending ,@body)))))
279 (defmacro gdb-add-subscriber (publisher subscriber)
280 "Register new PUBLISHER's SUBSCRIBER.
282 SUBSCRIBER must be a pair, where cdr is a function of one
283 argument (see `gdb-emit-signal')."
284 `(add-to-list ',publisher ,subscriber t))
286 (defmacro gdb-delete-subscriber (publisher subscriber)
287 "Unregister SUBSCRIBER from PUBLISHER."
288 `(setq ,publisher (delete ,subscriber
291 (defun gdb-get-subscribers (publisher)
294 (defun gdb-emit-signal (publisher &optional signal)
295 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
296 (dolist (subscriber (gdb-get-subscribers publisher))
297 (funcall (cdr subscriber) signal)))
299 (defvar gdb-buf-publisher '()
300 "Used to invalidate GDB buffers by emitting a signal in
303 Must be a list of pairs with cars being buffers and cdr's being
304 valid signal handlers.")
307 "GDB graphical interface"
309 :link '(info-link "(emacs)GDB Graphical Interface")
312 (defgroup gdb-non-stop nil
313 "GDB non-stop debugging settings"
317 (defgroup gdb-buffers nil
322 (defcustom gdb-debug-log-max 128
323 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
325 :type '(choice (integer :tag "Number of elements")
326 (const :tag "Unlimited" nil))
329 (defcustom gdb-non-stop-setting t
330 "When in non-stop mode, stopped threads can be examined while
331 other threads continue to execute.
333 GDB session needs to be restarted for this setting to take
339 ;; TODO Some commands can't be called with --all (give a notice about
340 ;; it in setting doc)
341 (defcustom gdb-gud-control-all-threads t
342 "When enabled, GUD execution commands affect all threads when
343 in non-stop mode. Otherwise, only current thread is affected."
348 (defcustom gdb-switch-reasons t
349 "List of stop reasons which cause Emacs to switch to the thread
350 which caused the stop. When t, switch to stopped thread no matter
351 what the reason was. When nil, never switch to stopped thread
354 This setting is used in non-stop mode only. In all-stop mode,
355 Emacs always switches to the thread which caused the stop."
356 ;; exited, exited-normally and exited-signalled are not
357 ;; thread-specific stop reasons and therefore are not included in
360 (const :tag "All reasons" t)
361 (set :tag "Selection of reasons..."
362 (const :tag "A breakpoint was reached." "breakpoint-hit")
363 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
364 (const :tag "A read watchpoint was triggered." "read-watchpoint-trigger")
365 (const :tag "An access watchpoint was triggered." "access-watchpoint-trigger")
366 (const :tag "Function finished execution." "function-finished")
367 (const :tag "Location reached." "location-reached")
368 (const :tag "Watchpoint has gone out of scope" "watchpoint-scope")
369 (const :tag "End of stepping range reached." "end-stepping-range")
370 (const :tag "Signal received (like interruption)." "signal-received"))
371 (const :tag "None" nil))
374 :link '(info-link "(gdb)GDB/MI Async Records"))
376 (defcustom gdb-stopped-hooks nil
377 "This variable holds a list of functions to be called whenever
380 Each function takes one argument, a parsed MI response, which
381 contains fields of corresponding MI *stopped async record:
383 ((stopped-threads . \"all\")
385 (frame (line . \"38\")
386 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
387 (file . \"server.c\")
388 (args ((value . \"0x804b038\")
391 (addr . \"0x0804869e\"))
392 (reason . \"end-stepping-range\"))
394 Note that \"reason\" is only present in non-stop debugging mode.
396 `bindat-get-field' may be used to access the fields of response.
398 Each function is called after the new current thread was selected
399 and GDB buffers were updated in `gdb-stopped'."
400 :type '(repeat function)
403 :link '(info-link "(gdb)GDB/MI Async Records"))
405 (defcustom gdb-switch-when-another-stopped t
406 "When nil, Emacs won't switch to stopped thread if some other
407 stopped thread is already selected."
412 (defcustom gdb-stack-buffer-locations t
413 "Show file information or library names in stack buffers."
418 (defcustom gdb-stack-buffer-addresses nil
419 "Show frame addresses in stack buffers."
424 (defcustom gdb-thread-buffer-verbose-names t
425 "Show long thread names in threads buffer."
430 (defcustom gdb-thread-buffer-arguments t
431 "Show function arguments in threads buffer."
436 (defcustom gdb-thread-buffer-locations t
437 "Show file information or library names in threads buffer."
442 (defcustom gdb-thread-buffer-addresses nil
443 "Show addresses for thread frames in threads buffer."
448 (defcustom gdb-show-threads-by-default nil
449 "Show threads list buffer instead of breakpoints list by
455 (defvar gdb-debug-log nil
456 "List of commands sent to and replies received from GDB.
457 Most recent commands are listed first. This list stores only the last
458 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
461 (defcustom gdb-enable-debug nil
462 "Non-nil means record the process input and output in `gdb-debug-log'."
467 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
468 "Shell command for generating a list of defined macros in a source file.
469 This list is used to display the #define directive associated
470 with an identifier as a tooltip. It works in a debug session with
471 GDB, when `gud-tooltip-mode' is t.
473 Set `gdb-cpp-define-alist-flags' for any include paths or
479 (defcustom gdb-cpp-define-alist-flags ""
480 "Preprocessor flags for `gdb-cpp-define-alist-program'."
485 (defcustom gdb-create-source-file-list t
486 "Non-nil means create a list of files from which the executable was built.
487 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
488 line for a long time when starting, possibly because your executable was
489 built from a large number of files. This allows quicker initialization
490 but means that these files are not automatically enabled for debugging,
491 e.g., you won't be able to click in the fringe to set a breakpoint until
492 execution has already stopped there."
497 (defcustom gdb-show-main nil
498 "Non-nil means display source file containing the main routine at startup.
499 Also display the main routine in the disassembly buffer if present."
504 (defun gdb-force-mode-line-update (status)
505 (let ((buffer gud-comint-buffer))
506 (if (and buffer (buffer-name buffer))
507 (with-current-buffer buffer
508 (setq mode-line-process
510 (process-status (get-buffer-process buffer)) status))
511 ;; Force mode line redisplay soon.
512 (force-mode-line-update)))))
514 (defun gdb-enable-debug (arg)
515 "Toggle logging of transaction between Emacs and Gdb.
516 The log is stored in `gdb-debug-log' as an alist with elements
517 whose cons is send, send-item or recv and whose cdr is the string
518 being transferred. This list may grow up to a size of
519 `gdb-debug-log-max' after which the oldest element (at the end of
520 the list) is deleted every time a new one is added (at the front)."
522 (setq gdb-enable-debug
524 (not gdb-enable-debug)
525 (> (prefix-numeric-value arg) 0)))
526 (message (format "Logging of transaction %sabled"
527 (if gdb-enable-debug "en" "dis"))))
529 ;; These two are used for menu and toolbar
530 (defun gdb-control-all-threads ()
531 "Switch to non-stop/A mode."
533 (setq gdb-gud-control-all-threads t)
534 ;; Actually forcing the tool-bar to update.
535 (force-mode-line-update)
536 (message "Now in non-stop/A mode."))
538 (defun gdb-control-current-thread ()
539 "Switch to non-stop/T mode."
541 (setq gdb-gud-control-all-threads nil)
542 ;; Actually forcing the tool-bar to update.
543 (force-mode-line-update)
544 (message "Now in non-stop/T mode."))
546 (defun gdb-find-watch-expression ()
547 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
548 (varnum (car var)) expr array)
549 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
550 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
551 (component-list (split-string (match-string 2 varnum) "\\." t)))
552 (setq expr (nth 1 var1))
553 (setq varnumlet (car var1))
554 (dolist (component component-list)
555 (setq var2 (assoc varnumlet gdb-var-list))
556 (setq expr (concat expr
557 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
558 (concat "[" component "]")
559 (concat "." component))))
560 (setq varnumlet (concat varnumlet "." component)))
563 ;; noall is used for commands which don't take --all, but only
565 (defun gdb-gud-context-command (command &optional noall)
566 "When `gdb-non-stop' is t, add --thread option to COMMAND if
567 `gdb-gud-control-all-threads' is nil and --all option otherwise.
568 If NOALL is t, always add --thread option no matter what
569 `gdb-gud-control-all-threads' value is.
571 When `gdb-non-stop' is nil, return COMMAND unchanged."
573 (if (and gdb-gud-control-all-threads
575 (concat command " --all ")
576 (gdb-current-context-command command t))
579 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
580 "`gud-call' wrapper which adds --thread/--all options between
581 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
583 NOARG must be t when this macro is used outside `gud-def'"
585 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
586 ,(when (not noarg) 'arg)))
589 (defun gdb (command-line)
590 "Run gdb on program FILE in buffer *gud-FILE*.
591 The directory containing FILE becomes the initial working directory
592 and source-file directory for your debugger.
594 If `gdb-many-windows' is nil (the default value) then gdb just
595 pops up the GUD buffer unless `gdb-show-main' is t. In this case
596 it starts with two windows: one displaying the GUD buffer and the
597 other with the source file with the main routine of the inferior.
599 If `gdb-many-windows' is t, regardless of the value of
600 `gdb-show-main', the layout below will appear. Keybindings are
601 shown in some of the buffers.
603 Watch expressions appear in the speedbar/slowbar.
605 The following commands help control operation :
607 `gdb-many-windows' - Toggle the number of windows gdb uses.
608 `gdb-restore-windows' - To restore the window layout.
610 See Info node `(emacs)GDB Graphical Interface' for a more
611 detailed description of this mode.
614 +----------------------------------------------------------------------+
616 +-----------------------------------+----------------------------------+
617 | GUD buffer (I/O of GDB) | Locals buffer |
621 +-----------------------------------+----------------------------------+
622 | Source buffer | I/O buffer (of debugged program) |
630 +-----------------------------------+----------------------------------+
631 | Stack buffer | Breakpoints buffer |
632 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
633 | | RET gdb-goto-breakpoint |
634 | | D gdb-delete-breakpoint |
635 +-----------------------------------+----------------------------------+"
637 (interactive (list (gud-query-cmdline 'gdb)))
639 (when (and gud-comint-buffer
640 (buffer-name gud-comint-buffer)
641 (get-buffer-process gud-comint-buffer)
642 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
643 (gdb-restore-windows)
645 "Multiple debugging requires restarting in text command mode"))
647 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
648 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
649 (setq comint-input-sender 'gdb-send)
651 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
652 "Set temporary breakpoint at current line.")
654 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
655 "\C-j" "Set execution address to current line.")
657 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
658 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
659 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
660 (gud-def gud-pstar "print* %e" nil
661 "Evaluate C dereferenced pointer expression at point.")
663 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
665 "Step one source line with display.")
666 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
668 "Step one instruction with display.")
669 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
671 "Step one line (skip functions).")
672 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
674 "Step one instruction (skip functions).")
675 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
677 "Continue with display.")
678 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
680 "Finish executing current function.")
681 (gud-def gud-run "-exec-run"
685 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
686 (gud-call "break %f:%l" arg)
690 (gud-call "break *%a" arg)))
691 "\C-b" "Set breakpoint at current line or address.")
693 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
694 (gud-call "clear %f:%l" arg)
698 (gud-call "clear *%a" arg)))
699 "\C-d" "Remove breakpoint at current line or address.")
701 ;; -exec-until doesn't support --all yet
702 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
703 (gud-call "-exec-until %f:%l" arg)
707 (gud-call "-exec-until *%a" arg)))
708 "\C-u" "Continue to current line or address.")
709 ;; TODO Why arg here?
711 gud-go (gud-call (if gdb-active-process
712 (gdb-gud-context-command "-exec-continue")
714 nil "Start or continue execution.")
716 ;; For debugging Emacs only.
720 "pp1 " (if (eq (buffer-local-value
721 'major-mode (window-buffer)) 'speedbar-mode)
722 (gdb-find-watch-expression) "%e")) arg)
723 nil "Print the Emacs s-expression.")
725 (define-key gud-minor-mode-map [left-margin mouse-1]
726 'gdb-mouse-set-clear-breakpoint)
727 (define-key gud-minor-mode-map [left-fringe mouse-1]
728 'gdb-mouse-set-clear-breakpoint)
729 (define-key gud-minor-mode-map [left-margin C-mouse-1]
730 'gdb-mouse-toggle-breakpoint-margin)
731 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
732 'gdb-mouse-toggle-breakpoint-fringe)
734 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
736 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
738 (define-key gud-minor-mode-map [left-margin mouse-3]
740 (define-key gud-minor-mode-map [left-fringe mouse-3]
743 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
745 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
747 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
749 (define-key gud-minor-mode-map [left-margin C-mouse-3]
752 (local-set-key "\C-i" 'gud-gdb-complete-command)
753 (setq gdb-first-prompt t)
754 (setq gud-running nil)
758 (run-hooks 'gdb-mode-hook))
762 (setq gdb-selected-frame nil
764 gdb-thread-number nil
766 gdb-pending-triggers nil
767 gdb-output-sink 'user
768 gdb-location-alist nil
769 gdb-source-file-list nil
772 gdb-handler-alist '()
773 gdb-handler-number nil
775 gdb-first-done-or-error t
776 gdb-buffer-fringe-width (car (window-fringes))
778 gdb-source-window nil
779 gdb-inferior-status nil
781 gdb-buf-publisher '()
783 gdb-breakpoints-list '()
784 gdb-register-names '()
785 gdb-non-stop gdb-non-stop-setting)
787 (setq gdb-buffer-type 'gdbmi)
789 (gdb-force-mode-line-update
790 (propertize "initializing..." 'face font-lock-variable-name-face))
792 (gdb-get-buffer-create 'gdb-inferior-io)
793 (gdb-clear-inferior-io)
794 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
796 ;; Needs GDB 6.4 onwards
797 (list (concat "-inferior-tty-set "
798 (process-tty-name (get-process "gdb-inferior")))
800 (if (eq window-system 'w32)
801 (gdb-input (list "-gdb-set new-console off" 'ignore)))
802 (gdb-input (list "-gdb-set height 0" 'ignore))
805 (gdb-input (list "-gdb-set non-stop 1" 'gdb-non-stop-handler)))
807 ;; find source file and compilation directory here
809 ; Needs GDB 6.2 onwards.
810 (list "-file-list-exec-source-files" 'gdb-get-source-file-list))
811 (if gdb-create-source-file-list
813 ; Needs GDB 6.0 onwards.
814 (list "-file-list-exec-source-file" 'gdb-get-source-file)))
816 (list "-gdb-show prompt" 'gdb-get-prompt)))
818 (defun gdb-non-stop-handler ()
819 (goto-char (point-min))
820 (if (re-search-forward "No symbol" nil t)
822 (message "This version of GDB doesn't support non-stop mode. Turning it off.")
823 (setq gdb-non-stop nil))
824 (gdb-input (list "-gdb-set target-async 1" 'ignore))
825 (gdb-input (list "-enable-pretty-printing" 'ignore))))
827 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
829 (defun gdb-create-define-alist ()
830 "Create an alist of #define directives for GUD tooltips."
831 (let* ((file (buffer-file-name))
833 (with-output-to-string
834 (with-current-buffer standard-output
837 ;; call-process doesn't work with remote file names.
838 (not (file-remote-p default-directory))
839 (call-process shell-file-name file
840 (list t nil) nil "-c"
841 (concat gdb-cpp-define-alist-program " "
842 gdb-cpp-define-alist-flags))))))
843 (define-list (split-string output "\n" t))
845 (setq gdb-define-alist nil)
846 (dolist (define define-list)
847 (setq name (nth 1 (split-string define "[( ]")))
848 (push (cons name define) gdb-define-alist))))
850 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
851 (defvar tooltip-use-echo-area)
853 (defun gdb-tooltip-print (expr)
854 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
855 (goto-char (point-min))
856 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
858 (concat expr " = " (read (match-string 1)))
859 (or gud-tooltip-echo-area tooltip-use-echo-area
860 (not (display-graphic-p)))))))
862 ;; If expr is a macro for a function don't print because of possible dangerous
863 ;; side-effects. Also printing a function within a tooltip generates an
864 ;; unexpected starting annotation (phase error).
865 (defun gdb-tooltip-print-1 (expr)
866 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
867 (goto-char (point-min))
868 (if (search-forward "expands to: " nil t)
869 (unless (looking-at "\\S-+.*(.*).*")
871 (list (concat "-data-evaluate-expression " expr)
872 `(lambda () (gdb-tooltip-print ,expr))))))))
874 (defun gdb-init-buffer ()
875 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
876 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
877 (when gud-tooltip-mode
878 (make-local-variable 'gdb-define-alist)
879 (gdb-create-define-alist)
880 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
882 (defmacro gdb-if-arrow (arrow-position &rest body)
884 (let ((buffer (marker-buffer ,arrow-position)) (line))
885 (if (equal buffer (window-buffer (posn-window end)))
886 (with-current-buffer buffer
887 (when (or (equal start end)
888 (equal (posn-point start)
889 (marker-position ,arrow-position)))
892 (defun gdb-mouse-until (event)
893 "Continue running until a source line past the current line.
894 The destination source line can be selected either by clicking
895 with mouse-3 on the fringe/margin or dragging the arrow
896 with mouse-1 (default bindings)."
898 (let ((start (event-start event))
899 (end (event-end event)))
900 (gdb-if-arrow gud-overlay-arrow-position
901 (setq line (line-number-at-pos (posn-point end)))
902 (gud-call (concat "until " (number-to-string line))))
903 (gdb-if-arrow gdb-disassembly-position
905 (goto-char (point-min))
906 (forward-line (1- (line-number-at-pos (posn-point end))))
908 (gud-call (concat "until *%a"))))))
910 (defun gdb-mouse-jump (event)
911 "Set execution address/line.
912 The destination source line can be selected either by clicking with C-mouse-3
913 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
914 Unlike `gdb-mouse-until' the destination address can be before the current
915 line, and no execution takes place."
917 (let ((start (event-start event))
918 (end (event-end event)))
919 (gdb-if-arrow gud-overlay-arrow-position
920 (setq line (line-number-at-pos (posn-point end)))
922 (gud-call (concat "tbreak " (number-to-string line)))
923 (gud-call (concat "jump " (number-to-string line)))))
924 (gdb-if-arrow gdb-disassembly-position
926 (goto-char (point-min))
927 (forward-line (1- (line-number-at-pos (posn-point end))))
930 (gud-call (concat "tbreak *%a"))
931 (gud-call (concat "jump *%a")))))))
933 (defcustom gdb-show-changed-values t
934 "If non-nil change the face of out of scope variables and changed values.
935 Out of scope variables are suppressed with `shadow' face.
936 Changed values are highlighted with the face `font-lock-warning-face'."
941 (defcustom gdb-max-children 40
942 "Maximum number of children before expansion requires confirmation."
947 (defcustom gdb-delete-out-of-scope t
948 "If non-nil delete watch expressions automatically when they go out of scope."
953 (defcustom gdb-speedbar-auto-raise nil
954 "If non-nil raise speedbar every time display of watch expressions is\
960 (defcustom gdb-use-colon-colon-notation nil
961 "If non-nil use FUN::VAR format to display variables in the speedbar."
966 (defun gdb-speedbar-auto-raise (arg)
967 "Toggle automatic raising of the speedbar for watch expressions.
968 With prefix argument ARG, automatically raise speedbar if ARG is
969 positive, otherwise don't automatically raise it."
971 (setq gdb-speedbar-auto-raise
973 (not gdb-speedbar-auto-raise)
974 (> (prefix-numeric-value arg) 0)))
975 (message (format "Auto raising %sabled"
976 (if gdb-speedbar-auto-raise "en" "dis"))))
978 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
979 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
981 (declare-function tooltip-identifier-from-point "tooltip" (point))
983 (defun gud-watch (&optional arg event)
984 "Watch expression at point.
985 With arg, enter name of variable to be watched in the minibuffer."
986 (interactive (list current-prefix-arg last-input-event))
987 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
988 (if (eq minor-mode 'gdbmi)
990 (if event (posn-set-point (event-end event)))
992 (save-selected-window
995 (completing-read "Name of variable: "
996 'gud-gdb-complete-command)
997 (if (and transient-mark-mode mark-active)
998 (buffer-substring (region-beginning) (region-end))
999 (concat (if (eq major-mode 'gdb-registers-mode) "$")
1000 (tooltip-identifier-from-point (point)))))))
1001 (set-text-properties 0 (length expr) nil expr)
1003 (list (concat"-var-create - * " expr "")
1004 `(lambda () (gdb-var-create-handler ,expr)))))))
1005 (message "gud-watch is a no-op in this mode."))))
1007 (defun gdb-var-create-handler (expr)
1008 (let* ((result (gdb-json-partial-output)))
1009 (if (not (bindat-get-field result 'msg))
1011 (list (bindat-get-field result 'name)
1012 (if (and (string-equal gdb-current-language "c")
1013 gdb-use-colon-colon-notation gdb-selected-frame)
1014 (setq expr (concat gdb-selected-frame "::" expr))
1016 (bindat-get-field result 'numchild)
1017 (bindat-get-field result 'type)
1018 (bindat-get-field result 'value)
1020 (bindat-get-field result 'has_more)
1021 gdb-frame-address)))
1022 (push var gdb-var-list)
1024 (unless (string-equal
1025 speedbar-initial-expansion-list-name "GUD")
1026 (speedbar-change-initial-expansion-list "GUD")))
1027 (message-box "No symbol \"%s\" in current context." expr))))
1029 (defun gdb-speedbar-update ()
1030 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1031 (not (gdb-pending-p 'gdb-speedbar-timer)))
1032 ;; Dummy command to update speedbar even when idle.
1033 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn))
1034 ;; Keep gdb-pending-triggers non-nil till end.
1035 (gdb-add-pending 'gdb-speedbar-timer)))
1037 (defun gdb-speedbar-timer-fn ()
1038 (if gdb-speedbar-auto-raise
1039 (raise-frame speedbar-frame))
1040 (gdb-delete-pending 'gdb-speedbar-timer)
1041 (speedbar-timer-fn))
1043 (defun gdb-var-evaluate-expression-handler (varnum changed)
1044 (goto-char (point-min))
1045 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1046 (let ((var (assoc varnum gdb-var-list)))
1048 (if changed (setcar (nthcdr 5 var) 'changed))
1049 (setcar (nthcdr 4 var) (read (match-string 1)))))
1050 (gdb-speedbar-update))
1052 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1053 (defun gdb-var-list-children (varnum)
1055 (list (concat "-var-update " varnum) 'ignore))
1057 (list (concat "-var-list-children --all-values "
1059 `(lambda () (gdb-var-list-children-handler ,varnum)))))
1061 (defun gdb-var-list-children-handler (varnum)
1062 (let* ((var-list nil)
1063 (output (bindat-get-field (gdb-json-partial-output "child")))
1064 (children (bindat-get-field output 'children)))
1065 (catch 'child-already-watched
1066 (dolist (var gdb-var-list)
1067 (if (string-equal varnum (car var))
1069 ;; With dynamic varobjs numchild may have increased.
1070 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1072 (dolist (child children)
1073 (let ((varchild (list (bindat-get-field child 'name)
1074 (bindat-get-field child 'exp)
1075 (bindat-get-field child 'numchild)
1076 (bindat-get-field child 'type)
1077 (bindat-get-field child 'value)
1079 (bindat-get-field child 'has_more))))
1080 (if (assoc (car varchild) gdb-var-list)
1081 (throw 'child-already-watched nil))
1082 (push varchild var-list))))
1083 (push var var-list)))
1084 (setq gdb-var-list (nreverse var-list))))
1085 (gdb-speedbar-update))
1087 (defun gdb-var-set-format (format)
1088 "Set the output format for a variable displayed in the speedbar."
1089 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1092 (list (concat "-var-set-format " varnum " " format) 'ignore))
1095 (defun gdb-var-delete-1 (var varnum)
1097 (list (concat "-var-delete " varnum) 'ignore))
1098 (setq gdb-var-list (delq var gdb-var-list))
1099 (dolist (varchild gdb-var-list)
1100 (if (string-match (concat (car var) "\\.") (car varchild))
1101 (setq gdb-var-list (delq varchild gdb-var-list)))))
1103 (defun gdb-var-delete ()
1104 "Delete watch expression at point from the speedbar."
1106 (let ((text (speedbar-line-text)))
1107 (string-match "\\(\\S-+\\)" text)
1108 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1110 (if (string-match "\\." (car var))
1111 (message-box "Can only delete a root expression")
1112 (gdb-var-delete-1 var varnum)))))
1114 (defun gdb-var-delete-children (varnum)
1115 "Delete children of variable object at point from the speedbar."
1117 (list (concat "-var-delete -c " varnum) 'ignore)))
1119 (defun gdb-edit-value (text token indent)
1120 "Assign a value to a variable displayed in the speedbar."
1121 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1122 (varnum (car var)) (value))
1123 (setq value (read-string "New value: "))
1125 (list (concat "-var-assign " varnum " " value)
1126 `(lambda () (gdb-edit-value-handler ,value))))))
1128 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1130 (defun gdb-edit-value-handler (value)
1131 (goto-char (point-min))
1132 (if (re-search-forward gdb-error-regexp nil t)
1133 (message-box "Invalid number or expression (%s)" value)))
1135 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1136 (defun gdb-var-update ()
1137 (if (not (gdb-pending-p 'gdb-var-update))
1139 (list "-var-update --all-values *" 'gdb-var-update-handler)))
1140 (gdb-add-pending 'gdb-var-update))
1142 (defun gdb-var-update-handler ()
1143 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1144 (dolist (var gdb-var-list)
1145 (setcar (nthcdr 5 var) nil))
1146 (let ((temp-var-list gdb-var-list))
1147 (dolist (change changelist)
1148 (let* ((varnum (bindat-get-field change 'name))
1149 (var (assoc varnum gdb-var-list))
1150 (new-num (bindat-get-field change 'new_num_children)))
1152 (let ((scope (bindat-get-field change 'in_scope)))
1153 (cond ((string-equal scope "false")
1154 (if gdb-delete-out-of-scope
1155 (gdb-var-delete-1 var varnum)
1156 (setcar (nthcdr 5 var) 'out-of-scope)))
1157 ((string-equal scope "true")
1158 (setcar (nthcdr 6 var)
1159 (bindat-get-field change 'has_more))
1160 (when (and (string-equal (nth 6 var) "0")
1162 (string-equal (nth 2 var) "0"))
1163 (setcar (nthcdr 4 var)
1164 (bindat-get-field change 'value))
1165 (setcar (nthcdr 5 var) 'changed)))
1166 ((string-equal scope "invalid")
1167 (gdb-var-delete-1 var varnum)))))
1168 (let ((var-list nil) var1
1169 (children (bindat-get-field change 'new_children)))
1172 (setq var1 (pop temp-var-list))
1174 (if (string-equal varnum (car var1))
1175 (let ((new (string-to-number new-num))
1176 (previous (string-to-number (nth 2 var1))))
1177 (setcar (nthcdr 2 var1) new-num)
1178 (push var1 var-list)
1179 (cond ((> new previous)
1180 ;; Add new children to list.
1181 (dotimes (dummy previous)
1182 (push (pop temp-var-list) var-list))
1183 (dolist (child children)
1185 (list (bindat-get-field child 'name)
1186 (bindat-get-field child 'exp)
1187 (bindat-get-field child 'numchild)
1188 (bindat-get-field child 'type)
1189 (bindat-get-field child 'value)
1191 (bindat-get-field child 'has_more))))
1192 (push varchild var-list))))
1193 ;; Remove deleted children from list.
1195 (dotimes (dummy new)
1196 (push (pop temp-var-list) var-list))
1197 (dotimes (dummy (- previous new))
1198 (pop temp-var-list)))))
1199 (push var1 var-list))
1200 (setq var1 (pop temp-var-list)))
1201 (setq gdb-var-list (nreverse var-list)))))))))
1202 (setq gdb-pending-triggers
1203 (delq 'gdb-var-update gdb-pending-triggers))
1204 (gdb-speedbar-update))
1206 (defun gdb-speedbar-expand-node (text token indent)
1207 "Expand the node the user clicked on.
1208 TEXT is the text of the button we clicked on, a + or - item.
1209 TOKEN is data related to this node.
1210 INDENT is the current indentation depth."
1211 (cond ((string-match "+" text) ;expand this node
1212 (let* ((var (assoc token gdb-var-list))
1213 (expr (nth 1 var)) (children (nth 2 var)))
1214 (if (or (<= (string-to-number children) gdb-max-children)
1216 (format "%s has %s children. Continue? " expr children)))
1217 (gdb-var-list-children token))))
1218 ((string-match "-" text) ;contract this node
1219 (dolist (var gdb-var-list)
1220 (if (string-match (concat token "\\.") (car var))
1221 (setq gdb-var-list (delq var gdb-var-list))))
1222 (gdb-var-delete-children token)
1223 (speedbar-change-expand-button-char ?+)
1224 (speedbar-delete-subblock indent))
1225 (t (error "Ooops... not sure what to do")))
1226 (speedbar-center-buffer-smartly))
1228 (defun gdb-get-target-string ()
1229 (with-current-buffer gud-comint-buffer
1236 ;; Each buffer has a TYPE -- a symbol that identifies the function
1237 ;; of that particular buffer.
1239 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1240 ;; is constructed specially.
1242 ;; Others are constructed by gdb-get-buffer-create and
1243 ;; named according to the rules set forth in the gdb-buffer-rules
1245 (defvar gdb-buffer-rules '())
1247 (defun gdb-rules-name-maker (rules-entry)
1249 (defun gdb-rules-buffer-mode (rules-entry)
1250 (nth 2 rules-entry))
1251 (defun gdb-rules-update-trigger (rules-entry)
1252 (nth 3 rules-entry))
1254 (defun gdb-update-buffer-name ()
1255 "Rename current buffer according to name-maker associated with
1256 it in `gdb-buffer-rules'."
1257 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1258 gdb-buffer-rules))))
1259 (when f (rename-buffer (funcall f)))))
1261 (defun gdb-current-buffer-rules ()
1262 "Get `gdb-buffer-rules' entry for current buffer type."
1263 (assoc gdb-buffer-type gdb-buffer-rules))
1265 (defun gdb-current-buffer-thread ()
1266 "Get thread object of current buffer from `gdb-threads-list'.
1268 When current buffer is not bound to any thread, return main
1270 (cdr (assoc gdb-thread-number gdb-threads-list)))
1272 (defun gdb-current-buffer-frame ()
1273 "Get current stack frame object for thread of current buffer."
1274 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1276 (defun gdb-buffer-type (buffer)
1277 "Get value of `gdb-buffer-type' for BUFFER."
1278 (with-current-buffer buffer
1281 (defun gdb-buffer-shows-main-thread-p ()
1282 "Return t if current GDB buffer shows main selected thread and
1283 is not bound to it."
1285 (not (local-variable-p 'gdb-thread-number)))
1287 (defun gdb-get-buffer (buffer-type &optional thread)
1288 "Get a specific GDB buffer.
1290 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1291 and `gdb-thread-number' (if provided) must be equal to THREAD."
1293 (dolist (buffer (buffer-list) nil)
1294 (with-current-buffer buffer
1295 (when (and (eq gdb-buffer-type buffer-type)
1297 (equal gdb-thread-number thread)))
1298 (throw 'found buffer))))))
1300 (defun gdb-get-buffer-create (buffer-type &optional thread)
1301 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1302 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1304 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1305 buffer-local variable of the new buffer.
1307 Buffer mode and name are selected according to buffer type.
1309 If buffer has trigger associated with it in `gdb-buffer-rules',
1310 this trigger is subscribed to `gdb-buf-publisher' and called with
1312 (or (gdb-get-buffer buffer-type thread)
1313 (let ((rules (assoc buffer-type gdb-buffer-rules))
1314 (new (generate-new-buffer "limbo")))
1315 (with-current-buffer new
1316 (let ((mode (gdb-rules-buffer-mode rules))
1317 (trigger (gdb-rules-update-trigger rules)))
1318 (when mode (funcall mode))
1319 (setq gdb-buffer-type buffer-type)
1321 (set (make-local-variable 'gdb-thread-number) thread))
1322 (set (make-local-variable 'gud-minor-mode)
1323 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1324 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1325 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1327 (gdb-add-subscriber gdb-buf-publisher
1328 (cons (current-buffer)
1329 (gdb-bind-function-to-buffer trigger (current-buffer))))
1330 (funcall trigger 'start))
1331 (current-buffer))))))
1333 (defun gdb-bind-function-to-buffer (expr buffer)
1334 "Return a function which will evaluate EXPR in BUFFER."
1335 `(lambda (&rest args)
1336 (with-current-buffer ,buffer
1337 (apply ',expr args))))
1339 ;; Used to define all gdb-frame-*-buffer functions except
1340 ;; `gdb-frame-io-buffer'
1341 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1342 "Define a function NAME which shows gdb BUFFER in a separate frame.
1344 DOC is an optional documentation string."
1345 `(defun ,name (&optional thread)
1348 (let ((special-display-regexps (append special-display-regexps '(".*")))
1349 (special-display-frame-alist gdb-frame-parameters))
1350 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1352 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1353 "Define a function NAME which shows gdb BUFFER.
1355 DOC is an optional documentation string."
1356 `(defun ,name (&optional thread)
1360 (gdb-get-buffer-create ,buffer thread) t)))
1362 ;; Used to display windows with thread-bound buffers
1363 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1365 `(defun ,name (&optional thread)
1368 (gdb-preempt-existing-or-display-buffer
1369 (gdb-get-buffer-create ,buffer thread)
1370 ,split-horizontal)))
1372 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1373 ;; at least one and possible more functions. The functions have these
1374 ;; roles in defining a buffer type:
1376 ;; NAME - Return a name for this buffer type.
1378 ;; The remaining function(s) are optional:
1380 ;; MODE - called in a new buffer with no arguments, should establish
1381 ;; the proper mode for the buffer.
1384 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1385 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1387 (setcdr binding rules)
1388 (push (cons buffer-type rules)
1389 gdb-buffer-rules))))
1391 (defun gdb-parent-mode ()
1392 "Generic mode to derive all other GDB buffer modes from."
1393 (kill-all-local-variables)
1394 (setq buffer-read-only t)
1395 (buffer-disable-undo)
1396 ;; Delete buffer from gdb-buf-publisher when it's killed
1397 ;; (if it has an associated update trigger)
1402 (let ((trigger (gdb-rules-update-trigger
1403 (gdb-current-buffer-rules))))
1405 (gdb-delete-subscriber
1407 ;; This should match gdb-add-subscriber done in
1408 ;; gdb-get-buffer-create
1409 (cons (current-buffer)
1410 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1413 ;; Partial-output buffer : This accumulates output from a command executed on
1414 ;; behalf of emacs (rather than the user).
1416 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1417 'gdb-partial-output-name)
1419 (defun gdb-partial-output-name ()
1420 (concat " *partial-output-"
1421 (gdb-get-target-string)
1425 (gdb-set-buffer-rules 'gdb-inferior-io
1426 'gdb-inferior-io-name
1427 'gdb-inferior-io-mode)
1429 (defun gdb-inferior-io-name ()
1430 (concat "*input/output of "
1431 (gdb-get-target-string)
1434 (defun gdb-display-io-buffer ()
1435 "Display IO of debugged program in a separate window."
1438 (gdb-get-buffer-create 'gdb-inferior-io) t))
1440 (defconst gdb-frame-parameters
1441 '((height . 14) (width . 80)
1443 (tool-bar-lines . nil)
1444 (menu-bar-lines . nil)
1445 (minibuffer . nil)))
1447 (defun gdb-frame-io-buffer ()
1448 "Display IO of debugged program in a new frame."
1450 (let ((special-display-regexps (append special-display-regexps '(".*")))
1451 (special-display-frame-alist gdb-frame-parameters))
1452 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1454 (defvar gdb-inferior-io-mode-map
1455 (let ((map (make-sparse-keymap)))
1456 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1457 (define-key map "\C-c\C-z" 'gdb-io-stop)
1458 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1459 (define-key map "\C-c\C-d" 'gdb-io-eof)
1460 (define-key map "\C-d" 'gdb-io-eof)
1463 ;; We want to use comint because it has various nifty and familiar features.
1464 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1465 "Major mode for gdb inferior-io.
1467 The following commands are available:
1468 \\{gdb-inferior-io-mode-map}"
1470 :syntax-table nil :abbrev-table nil
1472 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1474 (defun gdb-inferior-filter (proc string)
1475 (unless (string-equal string "")
1476 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1477 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1478 (comint-output-filter proc string)))
1480 (defun gdb-io-interrupt ()
1481 "Interrupt the program being debugged."
1484 (get-buffer-process gud-comint-buffer) comint-ptyp))
1486 (defun gdb-io-quit ()
1487 "Send quit signal to the program being debugged."
1490 (get-buffer-process gud-comint-buffer) comint-ptyp))
1492 (defun gdb-io-stop ()
1493 "Stop the program being debugged."
1496 (get-buffer-process gud-comint-buffer) comint-ptyp))
1498 (defun gdb-io-eof ()
1499 "Send end-of-file to the program being debugged."
1502 (get-buffer-process gud-comint-buffer)))
1504 (defun gdb-clear-inferior-io ()
1505 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1509 (defconst breakpoint-xpm-data
1511 static char *magick[] = {
1512 /* columns rows colors chars-per-pixel */
1528 "XPM data used for breakpoint icon.")
1530 (defconst breakpoint-enabled-pbm-data
1533 0 0 0 0 1 1 1 1 0 0 0 0
1534 0 0 0 1 1 1 1 1 1 0 0 0
1535 0 0 1 1 1 1 1 1 1 1 0 0
1536 0 1 1 1 1 1 1 1 1 1 1 0
1537 0 1 1 1 1 1 1 1 1 1 1 0
1538 0 1 1 1 1 1 1 1 1 1 1 0
1539 0 1 1 1 1 1 1 1 1 1 1 0
1540 0 0 1 1 1 1 1 1 1 1 0 0
1541 0 0 0 1 1 1 1 1 1 0 0 0
1542 0 0 0 0 1 1 1 1 0 0 0 0"
1543 "PBM data used for enabled breakpoint icon.")
1545 (defconst breakpoint-disabled-pbm-data
1557 0 0 0 1 0 1 0 1 0 0"
1558 "PBM data used for disabled breakpoint icon.")
1560 (defvar breakpoint-enabled-icon nil
1561 "Icon for enabled breakpoint in display margin.")
1563 (defvar breakpoint-disabled-icon nil
1564 "Icon for disabled breakpoint in display margin.")
1566 (declare-function define-fringe-bitmap "fringe.c"
1567 (bitmap bits &optional height width align))
1569 (and (display-images-p)
1570 ;; Bitmap for breakpoint in fringe
1571 (define-fringe-bitmap 'breakpoint
1572 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1573 ;; Bitmap for gud-overlay-arrow in fringe
1574 (define-fringe-bitmap 'hollow-right-triangle
1575 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1577 (defface breakpoint-enabled
1581 "Face for enabled breakpoint icon in fringe."
1584 (defface breakpoint-disabled
1585 '((((class color) (min-colors 88)) :foreground "grey70")
1586 ;; Ensure that on low-color displays that we end up something visible.
1587 (((class color) (min-colors 8) (background light))
1588 :foreground "black")
1589 (((class color) (min-colors 8) (background dark))
1590 :foreground "white")
1591 (((type tty) (class mono))
1593 (t :background "gray"))
1594 "Face for disabled breakpoint icon in fringe."
1598 (defun gdb-send (proc string)
1599 "A comint send filter for gdb."
1600 (with-current-buffer gud-comint-buffer
1601 (let ((inhibit-read-only t))
1602 (remove-text-properties (point-min) (point-max) '(face))))
1603 ;; mimic <RET> key to repeat previous command in GDB
1604 (if (not (string= "" string))
1605 (setq gdb-last-command string)
1606 (if gdb-last-command (setq string gdb-last-command)))
1607 (if gdb-enable-debug
1608 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1609 (if (string-match "^-" string)
1612 (setq gdb-first-done-or-error t)
1613 (process-send-string proc (concat string "\n")))
1615 (if (string-match "\\\\$" string)
1616 (setq gdb-continuation (concat gdb-continuation string "\n"))
1617 (setq gdb-first-done-or-error t)
1618 (process-send-string proc (concat "-interpreter-exec console \""
1619 gdb-continuation string "\"\n"))
1620 (setq gdb-continuation nil))))
1622 (defun gdb-input (item)
1623 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1624 (setq gdb-token-number (1+ gdb-token-number))
1625 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1626 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1627 (process-send-string (get-buffer-process gud-comint-buffer)
1628 (concat (car item) "\n")))
1630 ;; NOFRAME is used for gud execution control commands
1631 (defun gdb-current-context-command (command &optional noframe)
1632 "Add --thread and --frame options to gdb COMMAND.
1634 Option values are taken from `gdb-thread-number' and
1635 `gdb-frame-number'. If `gdb-thread-number' is nil, COMMAND is
1636 returned unchanged. If `gdb-frame-number' is nil of NOFRAME is t,
1637 then no --frame option is added."
1638 ;; gdb-frame-number may be nil while gdb-thread-number is non-nil
1639 ;; (when current thread is running)
1640 (if gdb-thread-number
1641 (concat command " --thread " gdb-thread-number)
1644 (defun gdb-current-context-buffer-name (name)
1645 "Add thread information and asterisks to string NAME.
1647 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1649 (if (local-variable-p 'gdb-thread-number)
1650 (format " (bound to thread %s)" gdb-thread-number)
1654 (defun gdb-current-context-mode-name (mode)
1655 "Add thread information to MODE which is to be used as
1658 (if gdb-thread-number
1659 (format " [thread %s]" gdb-thread-number)
1663 (defcustom gud-gdb-command-name "gdb -i=mi"
1664 "Default command to execute an executable under the GDB debugger."
1669 (setq gud-running nil)
1670 (setq gdb-output-sink 'user)
1671 (setq gdb-pending-triggers nil))
1673 (defun gdb-update ()
1674 "Update buffers showing status of debug session."
1675 (when gdb-first-prompt
1676 (gdb-force-mode-line-update
1677 (propertize "initializing..." 'face font-lock-variable-name-face))
1679 (setq gdb-first-prompt nil))
1681 (gdb-get-main-selected-frame)
1682 ;; We may need to update gdb-threads-list so we can use
1683 (gdb-get-buffer-create 'gdb-threads-buffer)
1684 ;; gdb-break-list is maintained in breakpoints handler
1685 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1687 (gdb-emit-signal gdb-buf-publisher 'update)
1689 (gdb-get-changed-registers)
1691 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1692 (dolist (var gdb-var-list)
1693 (setcar (nthcdr 5 var) nil))
1696 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1697 ;; because we may need to update current gud-running value without
1698 ;; changing current thread (see gdb-running)
1699 (defun gdb-setq-thread-number (number)
1700 "Only this function must be used to change `gdb-thread-number'
1701 value to NUMBER, because `gud-running' and `gdb-frame-number'
1702 need to be updated appropriately when current thread changes."
1703 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1704 (unless (string-equal number "0") (setq gdb-thread-number number))
1705 (setq gdb-frame-number "0")
1706 (gdb-update-gud-running))
1708 (defun gdb-update-gud-running ()
1709 "Set `gud-running' according to the state of current thread.
1711 `gdb-frame-number' is set to 0 if current thread is now stopped.
1713 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1714 cannot be reliably used to determine whether or not execution
1715 control buttons should be shown in menu or toolbar. Use
1716 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1719 For all-stop mode, thread information is unavailable while target
1721 (let ((old-value gud-running))
1723 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1725 ;; Set frame number to "0" when _current_ threads stops
1726 (when (and (gdb-current-buffer-thread)
1727 (not (eq gud-running old-value)))
1728 (setq gdb-frame-number "0"))))
1730 (defun gdb-show-run-p ()
1731 "Return t if \"Run/continue\" should be shown on the toolbar."
1732 (or (not gdb-active-process)
1734 (not gdb-gud-control-all-threads)
1737 (and gdb-gud-control-all-threads
1738 (> gdb-stopped-threads-count 0))))
1740 (defun gdb-show-stop-p ()
1741 "Return t if \"Stop\" should be shown on the toolbar."
1743 (not gdb-gud-control-all-threads)
1746 (and gdb-gud-control-all-threads
1747 (> gdb-running-threads-count 0))))
1749 ;; GUD displays the selected GDB frame. This might might not be the current
1750 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1751 ;; visited breakpoint is, use that window.
1752 (defun gdb-display-source-buffer (buffer)
1753 (let* ((last-window (if gud-last-last-frame
1755 (gud-find-file (car gud-last-last-frame)))))
1756 (source-window (or last-window
1757 (if (and gdb-source-window
1758 (window-live-p gdb-source-window))
1759 gdb-source-window))))
1761 (setq gdb-source-window source-window)
1762 (set-window-buffer source-window buffer))
1765 (defun gdb-car< (a b)
1766 (< (car a) (car b)))
1768 (defvar gdbmi-record-list
1769 '((gdb-gdb . "(gdb) \n")
1770 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1771 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1772 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1773 (gdb-console . "~\\(\".*?\"\\)\n")
1774 (gdb-internals . "&\\(\".*?\"\\)\n")
1775 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1776 (gdb-running . "\\*running,\\(.*?\n\\)")
1777 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1778 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1779 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1780 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1781 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1783 (defun gud-gdbmi-marker-filter (string)
1784 "Filter GDB/MI output."
1786 ;; Record transactions if logging is enabled.
1787 (when gdb-enable-debug
1788 (push (cons 'recv string) gdb-debug-log)
1789 (if (and gdb-debug-log-max
1790 (> (length gdb-debug-log) gdb-debug-log-max))
1791 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1793 ;; Recall the left over gud-marker-acc from last time
1794 (setq gud-marker-acc (concat gud-marker-acc string))
1796 ;; Start accumulating output for the GUD buffer
1797 (setq gdb-filter-output "")
1798 (let ((output-record) (output-record-list))
1800 ;; Process all the complete markers in this chunk.
1801 (dolist (gdbmi-record gdbmi-record-list)
1802 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1803 (push (list (match-beginning 0)
1805 (match-string 1 gud-marker-acc)
1806 (match-string 2 gud-marker-acc)
1809 (setq gud-marker-acc
1810 (concat (substring gud-marker-acc 0 (match-beginning 0))
1811 ;; Pad with spaces to preserve position.
1812 (make-string (length (match-string 0 gud-marker-acc)) 32)
1813 (substring gud-marker-acc (match-end 0))))))
1815 (setq output-record-list (sort output-record-list 'gdb-car<))
1817 (dolist (output-record output-record-list)
1818 (let ((record-type (cadr output-record))
1819 (arg1 (nth 2 output-record))
1820 (arg2 (nth 3 output-record)))
1821 (if (eq record-type 'gdb-error)
1822 (gdb-done-or-error arg2 arg1 'error)
1823 (if (eq record-type 'gdb-done)
1824 (gdb-done-or-error arg2 arg1 'done)
1825 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1826 ;; error message on internal stream. Don't print to GUD buffer.
1827 (unless (and (eq record-type 'gdb-internals)
1828 (string-equal (read arg1) "No registers.\n"))
1829 (funcall record-type arg1))))))
1831 (setq gdb-output-sink 'user)
1833 (string-match "^ *" gud-marker-acc)
1834 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1838 (defun gdb-gdb (output-field))
1840 (defun gdb-shell (output-field)
1841 (let ((gdb-output-sink gdb-output-sink))
1842 (setq gdb-filter-output
1843 (concat output-field gdb-filter-output))))
1845 (defun gdb-ignored-notification (output-field))
1847 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1848 (defun gdb-thread-created (output-field))
1849 (defun gdb-thread-exited (output-field)
1850 "Handle =thread-exited async record: unset `gdb-thread-number'
1851 if current thread exited and update threads list."
1852 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1853 (if (string= gdb-thread-number thread-id)
1854 (gdb-setq-thread-number nil))
1855 ;; When we continue current thread and it quickly exits,
1856 ;; gdb-pending-triggers left after gdb-running disallow us to
1857 ;; properly call -thread-info without --thread option. Thus we
1858 ;; need to use gdb-wait-for-pending.
1859 (gdb-wait-for-pending
1860 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1862 (defun gdb-thread-selected (output-field)
1863 "Handler for =thread-selected MI output record.
1865 Sets `gdb-thread-number' to new id."
1866 (let* ((result (gdb-json-string output-field))
1867 (thread-id (bindat-get-field result 'id)))
1868 (gdb-setq-thread-number thread-id)
1869 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1870 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1871 ;; as usually. Things happen to fast and second call (from
1872 ;; gdb-thread-selected handler) gets cut off by our beloved
1873 ;; gdb-pending-triggers.
1874 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1875 ;; body will get executed when `gdb-pending-triggers` is empty.
1876 (gdb-wait-for-pending
1879 (defun gdb-running (output-field)
1880 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'thread-id)))
1881 ;; We reset gdb-frame-number to nil if current thread has gone
1882 ;; running. This can't be done in gdb-thread-list-handler-custom
1883 ;; because we need correct gdb-frame-number by the time
1884 ;; -thread-info command is sent.
1885 (when (or (string-equal thread-id "all")
1886 (string-equal thread-id gdb-thread-number))
1887 (setq gdb-frame-number nil)))
1888 (setq gdb-inferior-status "running")
1889 (gdb-force-mode-line-update
1890 (propertize gdb-inferior-status 'face font-lock-type-face))
1891 (when (not gdb-non-stop)
1892 (setq gud-running t))
1893 (setq gdb-active-process t)
1894 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1896 (defun gdb-starting (output-field)
1897 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1898 (setq gdb-inferior-status "running")
1899 (gdb-force-mode-line-update
1900 (propertize gdb-inferior-status 'face font-lock-type-face))
1901 (setq gdb-active-process t)
1902 (setq gud-running t)
1903 ;; GDB doesn't seem to respond to -thread-info before first stop or
1904 ;; thread exit (even in non-stop mode), so this is useless.
1905 ;; Behaviour may change in the future.
1906 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1908 ;; -break-insert -t didn't give a reason before gdb 6.9
1910 (defun gdb-stopped (output-field)
1911 "Given the contents of *stopped MI async record, select new
1912 current thread and update GDB buffers."
1913 ;; Reason is available with target-async only
1914 (let* ((result (gdb-json-string output-field))
1915 (reason (bindat-get-field result 'reason))
1916 (thread-id (bindat-get-field result 'thread-id)))
1918 ;; -data-list-register-names needs to be issued for any stopped
1920 (when (not gdb-register-names)
1922 (list (concat "-data-list-register-names --thread " thread-id)
1923 'gdb-register-names-handler)))
1925 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1926 ;;; because synchronous GDB doesn't give these fields with CLI.
1929 ;;; ;; Extract the frame position from the marker.
1930 ;;; gud-last-frame (cons file
1931 ;;; (string-to-number
1932 ;;; (match-string 6 gud-marker-acc)))))
1934 (setq gdb-inferior-status (or reason "unknown"))
1935 (gdb-force-mode-line-update
1936 (propertize gdb-inferior-status 'face font-lock-warning-face))
1937 (if (string-equal reason "exited-normally")
1938 (setq gdb-active-process nil))
1940 ;; Select new current thread.
1942 ;; Don't switch if we have no reasons selected
1943 (when gdb-switch-reasons
1944 ;; Switch from another stopped thread only if we have
1945 ;; gdb-switch-when-another-stopped:
1946 (when (or gdb-switch-when-another-stopped
1947 (not (string= "stopped"
1948 (bindat-get-field (gdb-current-buffer-thread) 'state))))
1949 ;; Switch if current reason has been selected or we have no
1951 (if (or (eq gdb-switch-reasons t)
1952 (member reason gdb-switch-reasons))
1953 (when (not (string-equal gdb-thread-number thread-id))
1954 (message (concat "Switched to thread " thread-id))
1955 (gdb-setq-thread-number thread-id))
1956 (message (format "Thread %s stopped" thread-id)))))
1958 ;; Print "(gdb)" to GUD console
1959 (when gdb-first-done-or-error
1960 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1962 ;; In non-stop, we update information as soon as another thread gets
1964 (when (or gdb-first-done-or-error
1966 ;; In all-stop this updates gud-running properly as well.
1968 (setq gdb-first-done-or-error nil))
1969 (run-hook-with-args 'gdb-stopped-hooks result)))
1971 ;; Remove the trimmings from log stream containing debugging messages
1972 ;; being produced by GDB's internals, use warning face and send to GUD
1974 (defun gdb-internals (output-field)
1975 (setq gdb-filter-output
1978 (let ((error-message
1979 (read output-field)))
1981 0 (length error-message)
1982 'face font-lock-warning-face
1986 ;; Remove the trimmings from the console stream and send to GUD buffer
1987 ;; (frontend MI commands should not print to this stream)
1988 (defun gdb-console (output-field)
1989 (setq gdb-filter-output
1992 (read output-field))))
1994 (defun gdb-done-or-error (output-field token-number type)
1995 (if (string-equal token-number "")
1996 ;; Output from command entered by user
1998 (setq gdb-output-sink 'user)
1999 (setq token-number nil)
2000 ;; MI error - send to minibuffer
2001 (when (eq type 'error)
2002 ;; Skip "msg=" from `output-field'
2003 (message (read (substring output-field 4)))
2004 ;; Don't send to the console twice. (If it is a console error
2005 ;; it is also in the console stream.)
2006 (setq output-field nil)))
2007 ;; Output from command from frontend.
2008 (setq gdb-output-sink 'emacs))
2010 (gdb-clear-partial-output)
2011 (when gdb-first-done-or-error
2012 (unless (or token-number gud-running)
2013 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2015 (setq gdb-first-done-or-error nil))
2017 (setq gdb-filter-output
2018 (gdb-concat-output gdb-filter-output output-field))
2022 (with-current-buffer
2023 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2025 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2026 (setq gdb-handler-alist
2027 (assq-delete-all token-number gdb-handler-alist)))))
2029 (defun gdb-concat-output (so-far new)
2030 (let ((sink gdb-output-sink))
2032 ((eq sink 'user) (concat so-far new))
2034 (gdb-append-to-partial-output new)
2037 (defun gdb-append-to-partial-output (string)
2038 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2039 (goto-char (point-max))
2042 (defun gdb-clear-partial-output ()
2043 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2046 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2047 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2049 Field names are wrapped in double quotes and equal signs are
2050 replaced with semicolons.
2052 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurences from
2053 partial output. This is used to get rid of useless keys in lists
2054 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2055 -break-info are examples of MI commands which issue such
2058 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2059 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2060 -break-info output when it contains breakpoint script field
2061 incompatible with GDB/MI output syntax."
2063 (goto-char (point-min))
2066 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2067 (replace-match "" nil nil nil 1))))
2071 ;; Find positions of braces which enclose broken list
2072 (while (re-search-forward (concat fix-list "={\"") nil t)
2073 (let ((p1 (goto-char (- (point) 2)))
2074 (p2 (progn (forward-sexp)
2076 ;; Replace braces with brackets
2084 (goto-char (point-min))
2086 ;; TODO: This breaks badly with foo= inside constants
2087 (while (re-search-forward "\\([[:alpha:]-_]+\\)=" nil t)
2088 (replace-match "\"\\1\":" nil nil))
2089 (goto-char (point-max))
2092 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2093 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2095 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2096 (gdb-jsonify-buffer fix-key fix-list)
2098 (goto-char (point-min))
2099 (let ((json-array-type 'list))
2102 (defun gdb-json-string (string &optional fix-key fix-list)
2103 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2105 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2108 (gdb-json-read-buffer fix-key fix-list)))
2110 (defun gdb-json-partial-output (&optional fix-key fix-list)
2111 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2113 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2114 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2115 (gdb-json-read-buffer fix-key fix-list)))
2117 (defun gdb-line-posns (line)
2118 "Return a pair of LINE beginning and end positions."
2119 (let ((offset (1+ (- line (line-number-at-pos)))))
2121 (line-beginning-position offset)
2122 (line-end-position offset))))
2124 (defmacro gdb-mark-line (line variable)
2125 "Set VARIABLE marker to point at beginning of LINE.
2127 If current window has no fringes, inverse colors on LINE.
2129 Return position where LINE begins."
2131 (let* ((posns (gdb-line-posns ,line))
2132 (start-posn (car posns))
2133 (end-posn (cdr posns)))
2134 (set-marker ,variable (copy-marker start-posn))
2135 (when (not (> (car (window-fringes)) 0))
2136 (put-text-property start-posn end-posn
2137 'font-lock-face '(:inverse-video t)))
2140 (defun gdb-pad-string (string padding)
2141 (format (concat "%" (number-to-string padding) "s") string))
2143 ;; gdb-table struct is a way to programmatically construct simple
2144 ;; tables. It help to reliably align columns of data in GDB buffers
2150 (row-properties nil)
2153 (defun gdb-mapcar* (function &rest seqs)
2154 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2155 If there are several SEQS, FUNCTION is called with that many
2156 arugments, and mapping stops as sson as the shortest list runs
2158 (let ((shortest (apply #'min (mapcar #'length seqs))))
2165 (number-sequence 0 (1- shortest)))))
2167 (defun gdb-table-add-row (table row &optional properties)
2168 "Add ROW of string to TABLE and recalculate column sizes.
2170 When non-nil, PROPERTIES will be added to the whole row when
2171 calling `gdb-table-string'."
2172 (let ((rows (gdb-table-rows table))
2173 (row-properties (gdb-table-row-properties table))
2174 (column-sizes (gdb-table-column-sizes table))
2175 (right-align (gdb-table-right-align table)))
2176 (when (not column-sizes)
2177 (setf (gdb-table-column-sizes table)
2178 (make-list (length row) 0)))
2179 (setf (gdb-table-rows table)
2180 (append rows (list row)))
2181 (setf (gdb-table-row-properties table)
2182 (append row-properties (list properties)))
2183 (setf (gdb-table-column-sizes table)
2184 (gdb-mapcar* (lambda (x s)
2186 (max (abs x) (string-width (or s "")))))
2187 (if right-align new-x (- new-x))))
2188 (gdb-table-column-sizes table)
2190 ;; Avoid trailing whitespace at eol
2191 (if (not (gdb-table-right-align table))
2192 (setcar (last (gdb-table-column-sizes table)) 0))))
2194 (defun gdb-table-string (table &optional sep)
2195 "Return TABLE as a string with columns separated with SEP."
2196 (let ((column-sizes (gdb-table-column-sizes table))
2201 (lambda (row properties)
2203 (mapconcat 'identity
2204 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2208 (gdb-table-rows table)
2209 (gdb-table-row-properties table))
2212 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2213 (defun gdb-get-many-fields (struct &rest fields)
2214 "Return a list of FIELDS values from STRUCT."
2216 (dolist (field fields values)
2217 (setq values (append values (list (bindat-get-field struct field)))))))
2219 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2221 &optional signal-list)
2222 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2223 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2224 buffer with `gdb-bind-function-to-buffer'.
2226 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2227 defined trigger is called with an argument from SIGNAL-LIST. It's
2228 not recommended to define triggers with empty SIGNAL-LIST.
2229 Normally triggers should respond at least to 'update signal.
2231 Normally the trigger defined by this command must be called from
2232 the buffer where HANDLER-NAME must work. This should be done so
2233 that buffer-local thread number may be used in GDB-COMMAND (by
2234 calling `gdb-current-context-command').
2235 `gdb-bind-function-to-buffer' is used to achieve this, see
2236 `gdb-get-buffer-create'.
2238 Triggers defined by this command are meant to be used as a
2239 trigger argument when describing buffer types with
2240 `gdb-set-buffer-rules'."
2241 `(defun ,trigger-name (&optional signal)
2243 (or (not ,signal-list)
2244 (memq signal ,signal-list))
2245 (when (not (gdb-pending-p
2246 (cons (current-buffer) ',trigger-name)))
2249 (gdb-bind-function-to-buffer ',handler-name (current-buffer))))
2250 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2252 ;; Used by disassembly buffer only, the rest use
2253 ;; def-gdb-trigger-and-handler
2254 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2255 &optional nopreserve)
2256 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2258 Handlers are normally called from the buffers they put output in.
2260 Delete ((current-buffer) . TRIGGER-NAME) from
2261 `gdb-pending-triggers', erase current buffer and evaluate
2262 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2264 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2265 `(defun ,handler-name ()
2266 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2267 (let* ((buffer-read-only nil)
2268 (window (get-buffer-window (current-buffer) 0))
2269 (start (window-start window))
2270 (p (window-point window)))
2273 (gdb-update-buffer-name)
2274 ,(when (not nopreserve)
2275 '(set-window-start window start)
2276 '(set-window-point window p)))))
2278 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2279 handler-name custom-defun
2280 &optional signal-list)
2281 "Define trigger and handler.
2283 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2284 `def-gdb-auto-update-trigger'.
2286 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2287 `def-gdb-auto-update-handler'."
2289 (def-gdb-auto-update-trigger ,trigger-name
2291 ,handler-name ,signal-list)
2292 (def-gdb-auto-update-handler ,handler-name
2293 ,trigger-name ,custom-defun)))
2297 ;; Breakpoint buffer : This displays the output of `-break-list'.
2298 (def-gdb-trigger-and-handler
2299 gdb-invalidate-breakpoints "-break-list"
2300 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2303 (gdb-set-buffer-rules
2304 'gdb-breakpoints-buffer
2305 'gdb-breakpoints-buffer-name
2306 'gdb-breakpoints-mode
2307 'gdb-invalidate-breakpoints)
2309 (defun gdb-breakpoints-list-handler-custom ()
2310 (let ((breakpoints-list (bindat-get-field
2311 (gdb-json-partial-output "bkpt" "script")
2312 'BreakpointTable 'body))
2313 (table (make-gdb-table)))
2314 (setq gdb-breakpoints-list nil)
2315 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2316 (dolist (breakpoint breakpoints-list)
2317 (add-to-list 'gdb-breakpoints-list
2318 (cons (bindat-get-field breakpoint 'number)
2320 (let ((at (bindat-get-field breakpoint 'at))
2321 (pending (bindat-get-field breakpoint 'pending))
2322 (func (bindat-get-field breakpoint 'func))
2323 (type (bindat-get-field breakpoint 'type)))
2324 (gdb-table-add-row table
2326 (bindat-get-field breakpoint 'number)
2328 (bindat-get-field breakpoint 'disp)
2329 (let ((flag (bindat-get-field breakpoint 'enabled)))
2330 (if (string-equal flag "y")
2331 (propertize "y" 'font-lock-face font-lock-warning-face)
2332 (propertize "n" 'font-lock-face font-lock-comment-face)))
2333 (bindat-get-field breakpoint 'addr)
2334 (bindat-get-field breakpoint 'times)
2335 (if (string-match ".*watchpoint" type)
2336 (bindat-get-field breakpoint 'what)
2339 (propertize func 'font-lock-face font-lock-function-name-face)
2340 (gdb-frame-location breakpoint)))))
2341 ;; Add clickable properties only for breakpoints with file:line
2343 (append (list 'gdb-breakpoint breakpoint)
2344 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2345 mouse-face highlight))))))
2346 (insert (gdb-table-string table " "))
2347 (gdb-place-breakpoints)))
2349 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2350 (defun gdb-place-breakpoints ()
2351 (let ((flag) (bptno))
2352 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2353 (dolist (buffer (buffer-list))
2354 (with-current-buffer buffer
2355 (if (and (eq gud-minor-mode 'gdbmi)
2356 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2357 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2358 (dolist (breakpoint gdb-breakpoints-list)
2359 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2360 ; an associative list
2361 (line (bindat-get-field breakpoint 'line)))
2363 (let ((file (bindat-get-field breakpoint 'fullname))
2364 (flag (bindat-get-field breakpoint 'enabled))
2365 (bptno (bindat-get-field breakpoint 'number)))
2366 (unless (file-exists-p file)
2367 (setq file (cdr (assoc bptno gdb-location-alist))))
2369 (not (string-equal file "File not found")))
2370 (with-current-buffer
2371 (find-file-noselect file 'nowarn)
2373 ;; Only want one breakpoint icon at each location.
2374 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2375 (string-to-number line)))
2377 (list (concat "list " file ":1")
2380 (list "-file-list-exec-source-file"
2381 `(lambda () (gdb-get-location
2382 ,bptno ,line ,flag)))))))))))
2384 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2386 (defun gdb-get-location (bptno line flag)
2387 "Find the directory containing the relevant source file.
2388 Put in buffer and place breakpoint icon."
2389 (goto-char (point-min))
2390 (catch 'file-not-found
2391 (if (re-search-forward gdb-source-file-regexp nil t)
2392 (delete (cons bptno "File not found") gdb-location-alist)
2393 (push (cons bptno (match-string 1)) gdb-location-alist)
2395 (unless (assoc bptno gdb-location-alist)
2396 (push (cons bptno "File not found") gdb-location-alist)
2397 (message-box "Cannot find source file for breakpoint location.
2398 Add directory to search path for source files using the GDB command, dir."))
2399 (throw 'file-not-found nil))
2400 (with-current-buffer (find-file-noselect (match-string 1))
2402 ;; only want one breakpoint icon at each location
2403 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2405 (add-hook 'find-file-hook 'gdb-find-file-hook)
2407 (defun gdb-find-file-hook ()
2408 "Set up buffer for debugging if file is part of the source code
2409 of the current session."
2410 (if (and (buffer-name gud-comint-buffer)
2411 ;; in case gud or gdb-ui is just loaded
2413 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2415 (if (member buffer-file-name gdb-source-file-list)
2416 (with-current-buffer (find-buffer-visiting buffer-file-name)
2417 (gdb-init-buffer)))))
2419 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2420 (declare-function gud-break "gdb-mi" t t) ; gud-def
2421 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2423 (defun gdb-mouse-set-clear-breakpoint (event)
2424 "Set/clear breakpoint in left fringe/margin at mouse click.
2425 If not in a source or disassembly buffer just set point."
2427 (mouse-minibuffer-check event)
2428 (let ((posn (event-end event)))
2429 (with-selected-window (posn-window posn)
2430 (if (or (buffer-file-name) (eq major-mode 'gdb-disassembly-mode))
2431 (if (numberp (posn-point posn))
2433 (goto-char (posn-point posn))
2434 (if (or (posn-object posn)
2435 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2439 (posn-set-point posn))))
2441 (defun gdb-mouse-toggle-breakpoint-margin (event)
2442 "Enable/disable breakpoint in left margin with mouse click."
2444 (mouse-minibuffer-check event)
2445 (let ((posn (event-end event)))
2446 (if (numberp (posn-point posn))
2447 (with-selected-window (posn-window posn)
2449 (goto-char (posn-point posn))
2450 (if (posn-object posn)
2452 (let ((bptno (get-text-property
2453 0 'gdb-bptno (car (posn-string posn)))))
2455 (if (get-text-property
2456 0 'gdb-enabled (car (posn-string posn)))
2461 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2462 "Enable/disable breakpoint in left fringe with mouse click."
2464 (mouse-minibuffer-check event)
2465 (let* ((posn (event-end event))
2466 (pos (posn-point posn))
2469 (with-selected-window (posn-window posn)
2471 (set-buffer (window-buffer (selected-window)))
2473 (dolist (overlay (overlays-in pos pos))
2474 (when (overlay-get overlay 'put-break)
2475 (setq obj (overlay-get overlay 'before-string))))
2479 (if (get-text-property 0 'gdb-enabled obj)
2482 (get-text-property 0 'gdb-bptno obj)))))))))
2484 (defun gdb-breakpoints-buffer-name ()
2485 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2487 (def-gdb-display-buffer
2488 gdb-display-breakpoints-buffer
2489 'gdb-breakpoints-buffer
2490 "Display status of user-settable breakpoints.")
2492 (def-gdb-frame-for-buffer
2493 gdb-frame-breakpoints-buffer
2494 'gdb-breakpoints-buffer
2495 "Display status of user-settable breakpoints in a new frame.")
2497 (defvar gdb-breakpoints-mode-map
2498 (let ((map (make-sparse-keymap))
2499 (menu (make-sparse-keymap "Breakpoints")))
2500 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2501 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2502 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2503 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2504 (suppress-keymap map)
2505 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2506 (define-key map " " 'gdb-toggle-breakpoint)
2507 (define-key map "D" 'gdb-delete-breakpoint)
2508 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2509 (define-key map "q" 'gdb-delete-frame-or-window)
2510 (define-key map "\r" 'gdb-goto-breakpoint)
2511 (define-key map "\t" '(lambda ()
2513 (gdb-set-window-buffer
2514 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2515 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2516 (define-key map [follow-link] 'mouse-face)
2519 (defun gdb-delete-frame-or-window ()
2520 "Delete frame if there is only one window. Otherwise delete the window."
2522 (if (one-window-p) (delete-frame)
2525 ;;from make-mode-line-mouse-map
2526 (defun gdb-make-header-line-mouse-map (mouse function) "\
2527 Return a keymap with single entry for mouse key MOUSE on the header line.
2528 MOUSE is defined to run function FUNCTION with no args in the buffer
2529 corresponding to the mode line clicked."
2530 (let ((map (make-sparse-keymap)))
2531 (define-key map (vector 'header-line mouse) function)
2532 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2535 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2537 'help-echo ,help-echo
2538 'mouse-face ',mouse-face
2541 (gdb-make-header-line-mouse-map
2543 (lambda (event) (interactive "e")
2544 (save-selected-window
2545 (select-window (posn-window (event-start event)))
2546 (gdb-set-window-buffer
2547 (gdb-get-buffer-create ',buffer) t) )))))
2550 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2553 (defun gdb-threads-buffer-name ()
2554 (concat "*threads of " (gdb-get-target-string) "*"))
2556 (def-gdb-display-buffer
2557 gdb-display-threads-buffer
2559 "Display GDB threads.")
2561 (def-gdb-frame-for-buffer
2562 gdb-frame-threads-buffer
2564 "Display GDB threads in a new frame.")
2566 (def-gdb-trigger-and-handler
2567 gdb-invalidate-threads (gdb-current-context-command "-thread-info" gud-running)
2568 gdb-thread-list-handler gdb-thread-list-handler-custom
2569 '(start update update-threads))
2571 (gdb-set-buffer-rules
2573 'gdb-threads-buffer-name
2575 'gdb-invalidate-threads)
2577 (defvar gdb-threads-font-lock-keywords
2578 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2579 (" \\(stopped\\)" (1 font-lock-warning-face))
2580 (" \\(running\\)" (1 font-lock-string-face))
2581 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2582 "Font lock keywords used in `gdb-threads-mode'.")
2584 (defvar gdb-threads-mode-map
2585 (let ((map (make-sparse-keymap)))
2586 (define-key map "\r" 'gdb-select-thread)
2587 (define-key map "f" 'gdb-display-stack-for-thread)
2588 (define-key map "F" 'gdb-frame-stack-for-thread)
2589 (define-key map "l" 'gdb-display-locals-for-thread)
2590 (define-key map "L" 'gdb-frame-locals-for-thread)
2591 (define-key map "r" 'gdb-display-registers-for-thread)
2592 (define-key map "R" 'gdb-frame-registers-for-thread)
2593 (define-key map "d" 'gdb-display-disassembly-for-thread)
2594 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2595 (define-key map "i" 'gdb-interrupt-thread)
2596 (define-key map "c" 'gdb-continue-thread)
2597 (define-key map "s" 'gdb-step-thread)
2598 (define-key map "\t" '(lambda ()
2600 (gdb-set-window-buffer
2601 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2602 (define-key map [mouse-2] 'gdb-select-thread)
2603 (define-key map [follow-link] 'mouse-face)
2606 (defvar gdb-threads-header
2608 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
2609 "mouse-1: select" mode-line-highlight mode-line-inactive)
2611 (gdb-propertize-header "Threads" gdb-threads-buffer
2612 nil nil mode-line)))
2614 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2615 "Major mode for GDB threads.
2617 \\{gdb-threads-mode-map}"
2618 (setq gdb-thread-position (make-marker))
2619 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2620 (setq header-line-format gdb-threads-header)
2621 (set (make-local-variable 'font-lock-defaults)
2622 '(gdb-threads-font-lock-keywords))
2623 (run-mode-hooks 'gdb-threads-mode-hook)
2624 'gdb-invalidate-threads)
2626 (defun gdb-thread-list-handler-custom ()
2627 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2628 (table (make-gdb-table))
2630 (setq gdb-threads-list nil)
2631 (setq gdb-running-threads-count 0)
2632 (setq gdb-stopped-threads-count 0)
2633 (set-marker gdb-thread-position nil)
2635 (dolist (thread (reverse threads-list))
2636 (let ((running (string-equal (bindat-get-field thread 'state) "running")))
2637 (add-to-list 'gdb-threads-list
2638 (cons (bindat-get-field thread 'id)
2641 (incf gdb-running-threads-count)
2642 (incf gdb-stopped-threads-count))
2644 (gdb-table-add-row table
2646 (bindat-get-field thread 'id)
2648 (if gdb-thread-buffer-verbose-names
2649 (concat (bindat-get-field thread 'target-id) " ") "")
2650 (bindat-get-field thread 'state)
2651 ;; Include frame information for stopped threads
2654 " in " (bindat-get-field thread 'frame 'func)
2655 (if gdb-thread-buffer-arguments
2658 (let ((args (bindat-get-field thread 'frame 'args)))
2661 (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value))))
2665 (if gdb-thread-buffer-locations
2666 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2667 (if gdb-thread-buffer-addresses
2668 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2672 'mouse-face 'highlight
2673 'help-echo "mouse-2, RET: select thread")))
2674 (when (string-equal gdb-thread-number
2675 (bindat-get-field thread 'id))
2676 (setq marked-line (length gdb-threads-list))))
2677 (insert (gdb-table-string table " "))
2679 (gdb-mark-line marked-line gdb-thread-position)))
2680 ;; We update gud-running here because we need to make sure that
2681 ;; gdb-threads-list is up-to-date
2682 (gdb-update-gud-running)
2683 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2685 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2686 "Define a NAME command which will act upon thread on the current line.
2688 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2689 be the value of 'gdb-thread property of the current line. If
2690 'gdb-thread is nil, error is signaled."
2691 `(defun ,name (&optional event)
2693 (interactive (list last-input-event))
2694 (if event (posn-set-point (event-end event)))
2697 (let ((thread (get-text-property (point) 'gdb-thread)))
2700 (error "Not recognized as thread line"))))))
2702 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command &optional doc)
2703 "Define a NAME which will call BUFFER-COMMAND with id of thread
2704 on the current line."
2705 `(def-gdb-thread-buffer-command ,name
2706 (,buffer-command (bindat-get-field thread 'id))
2709 (def-gdb-thread-buffer-command gdb-select-thread
2710 (let ((new-id (bindat-get-field thread 'id)))
2711 (gdb-setq-thread-number new-id)
2712 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2714 "Select the thread at current line of threads buffer.")
2716 (def-gdb-thread-buffer-simple-command
2717 gdb-display-stack-for-thread
2718 gdb-preemptively-display-stack-buffer
2719 "Display stack buffer for the thread at current line.")
2721 (def-gdb-thread-buffer-simple-command
2722 gdb-display-locals-for-thread
2723 gdb-preemptively-display-locals-buffer
2724 "Display locals buffer for the thread at current line.")
2726 (def-gdb-thread-buffer-simple-command
2727 gdb-display-registers-for-thread
2728 gdb-preemptively-display-registers-buffer
2729 "Display registers buffer for the thread at current line.")
2731 (def-gdb-thread-buffer-simple-command
2732 gdb-display-disassembly-for-thread
2733 gdb-preemptively-display-disassembly-buffer
2734 "Display disassembly buffer for the thread at current line.")
2736 (def-gdb-thread-buffer-simple-command
2737 gdb-frame-stack-for-thread
2738 gdb-frame-stack-buffer
2739 "Display a new frame with stack buffer for the thread at
2742 (def-gdb-thread-buffer-simple-command
2743 gdb-frame-locals-for-thread
2744 gdb-frame-locals-buffer
2745 "Display a new frame with locals buffer for the thread at
2748 (def-gdb-thread-buffer-simple-command
2749 gdb-frame-registers-for-thread
2750 gdb-frame-registers-buffer
2751 "Display a new frame with registers buffer for the thread at
2754 (def-gdb-thread-buffer-simple-command
2755 gdb-frame-disassembly-for-thread
2756 gdb-frame-disassembly-buffer
2757 "Display a new frame with disassembly buffer for the thread at
2760 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2761 "Define a NAME which will execute GUD-COMMAND with
2762 `gdb-thread-number' locally bound to id of thread on the current
2764 `(def-gdb-thread-buffer-command ,name
2766 (let ((gdb-thread-number (bindat-get-field thread 'id))
2767 (gdb-gud-control-all-threads nil))
2768 (call-interactively #',gud-command))
2769 (error "Available in non-stop mode only, customize gdb-non-stop-setting."))
2772 (def-gdb-thread-buffer-gud-command
2773 gdb-interrupt-thread
2775 "Interrupt thread at current line.")
2777 (def-gdb-thread-buffer-gud-command
2780 "Continue thread at current line.")
2782 (def-gdb-thread-buffer-gud-command
2785 "Step thread at current line.")
2790 (defcustom gdb-memory-rows 8
2791 "Number of data rows in memory window."
2796 (defcustom gdb-memory-columns 4
2797 "Number of data columns in memory window."
2802 (defcustom gdb-memory-format "x"
2803 "Display format of data items in memory window."
2804 :type '(choice (const :tag "Hexadecimal" "x")
2805 (const :tag "Signed decimal" "d")
2806 (const :tag "Unsigned decimal" "u")
2807 (const :tag "Octal" "o")
2808 (const :tag "Binary" "t"))
2812 (defcustom gdb-memory-unit 4
2813 "Unit size of data items in memory window."
2814 :type '(choice (const :tag "Byte" 1)
2815 (const :tag "Halfword" 2)
2816 (const :tag "Word" 4)
2817 (const :tag "Giant word" 8))
2821 (def-gdb-trigger-and-handler
2822 gdb-invalidate-memory
2823 (format "-data-read-memory %s %s %d %d %d"
2829 gdb-read-memory-handler
2830 gdb-read-memory-custom
2833 (gdb-set-buffer-rules
2835 'gdb-memory-buffer-name
2837 'gdb-invalidate-memory)
2839 (defun gdb-memory-column-width (size format)
2840 "Return length of string with memory unit of SIZE in FORMAT.
2842 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2843 in `gdb-memory-format'."
2844 (let ((format-base (cdr (assoc format
2846 ("d" . 10) ("u" . 10)
2850 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2851 (cond ((string-equal format "x")
2852 (+ 2 res)) ; hexadecimal numbers have 0x in front
2853 ((or (string-equal format "d")
2854 (string-equal format "o"))
2857 (error "Unknown format"))))
2859 (defun gdb-read-memory-custom ()
2860 (let* ((res (gdb-json-partial-output))
2861 (err-msg (bindat-get-field res 'msg)))
2863 (let ((memory (bindat-get-field res 'memory)))
2864 (setq gdb-memory-address (bindat-get-field res 'addr))
2865 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2866 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2867 (setq gdb-memory-last-address gdb-memory-address)
2868 (dolist (row memory)
2869 (insert (concat (bindat-get-field row 'addr) ":"))
2870 (dolist (column (bindat-get-field row 'data))
2871 (insert (gdb-pad-string column
2872 (+ 2 (gdb-memory-column-width
2874 gdb-memory-format)))))
2876 ;; Show last page instead of empty buffer when out of bounds
2878 (let ((gdb-memory-address gdb-memory-last-address))
2879 (gdb-invalidate-memory 'update)
2880 (error err-msg))))))
2882 (defvar gdb-memory-mode-map
2883 (let ((map (make-sparse-keymap)))
2884 (suppress-keymap map t)
2885 (define-key map "q" 'kill-this-buffer)
2886 (define-key map "n" 'gdb-memory-show-next-page)
2887 (define-key map "p" 'gdb-memory-show-previous-page)
2888 (define-key map "a" 'gdb-memory-set-address)
2889 (define-key map "t" 'gdb-memory-format-binary)
2890 (define-key map "o" 'gdb-memory-format-octal)
2891 (define-key map "u" 'gdb-memory-format-unsigned)
2892 (define-key map "d" 'gdb-memory-format-signed)
2893 (define-key map "x" 'gdb-memory-format-hexadecimal)
2894 (define-key map "b" 'gdb-memory-unit-byte)
2895 (define-key map "h" 'gdb-memory-unit-halfword)
2896 (define-key map "w" 'gdb-memory-unit-word)
2897 (define-key map "g" 'gdb-memory-unit-giant)
2898 (define-key map "R" 'gdb-memory-set-rows)
2899 (define-key map "C" 'gdb-memory-set-columns)
2902 (defun gdb-memory-set-address-event (event)
2903 "Handle a click on address field in memory buffer header."
2905 (save-selected-window
2906 (select-window (posn-window (event-start event)))
2907 (gdb-memory-set-address)))
2909 ;; Non-event version for use within keymap
2910 (defun gdb-memory-set-address ()
2911 "Set the start memory address."
2913 (let ((arg (read-from-minibuffer "Memory address: ")))
2914 (setq gdb-memory-address arg))
2915 (gdb-invalidate-memory 'update))
2917 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2918 "Define a function NAME which reads new VAR value from minibuffer."
2919 `(defun ,name (event)
2922 (save-selected-window
2923 (select-window (posn-window (event-start event)))
2924 (let* ((arg (read-from-minibuffer ,echo-string))
2925 (count (string-to-number arg)))
2927 (error "Positive number only")
2928 (customize-set-variable ',variable count)
2929 (gdb-invalidate-memory 'update))))))
2931 (def-gdb-set-positive-number
2935 "Set the number of data rows in memory window.")
2937 (def-gdb-set-positive-number
2938 gdb-memory-set-columns
2941 "Set the number of data columns in memory window.")
2943 (defmacro def-gdb-memory-format (name format doc)
2944 "Define a function NAME to switch memory buffer to use FORMAT.
2946 DOC is an optional documentation string."
2947 `(defun ,name () ,(when doc doc)
2949 (customize-set-variable 'gdb-memory-format ,format)
2950 (gdb-invalidate-memory 'update)))
2952 (def-gdb-memory-format
2953 gdb-memory-format-binary "t"
2954 "Set the display format to binary.")
2956 (def-gdb-memory-format
2957 gdb-memory-format-octal "o"
2958 "Set the display format to octal.")
2960 (def-gdb-memory-format
2961 gdb-memory-format-unsigned "u"
2962 "Set the display format to unsigned decimal.")
2964 (def-gdb-memory-format
2965 gdb-memory-format-signed "d"
2966 "Set the display format to decimal.")
2968 (def-gdb-memory-format
2969 gdb-memory-format-hexadecimal "x"
2970 "Set the display format to hexadecimal.")
2972 (defvar gdb-memory-format-map
2973 (let ((map (make-sparse-keymap)))
2974 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2976 "Keymap to select format in the header line.")
2978 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2979 "Menu of display formats in the header line.")
2981 (define-key gdb-memory-format-menu [binary]
2982 '(menu-item "Binary" gdb-memory-format-binary
2983 :button (:radio . (equal gdb-memory-format "t"))))
2984 (define-key gdb-memory-format-menu [octal]
2985 '(menu-item "Octal" gdb-memory-format-octal
2986 :button (:radio . (equal gdb-memory-format "o"))))
2987 (define-key gdb-memory-format-menu [unsigned]
2988 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2989 :button (:radio . (equal gdb-memory-format "u"))))
2990 (define-key gdb-memory-format-menu [signed]
2991 '(menu-item "Signed Decimal" gdb-memory-format-signed
2992 :button (:radio . (equal gdb-memory-format "d"))))
2993 (define-key gdb-memory-format-menu [hexadecimal]
2994 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2995 :button (:radio . (equal gdb-memory-format "x"))))
2997 (defun gdb-memory-format-menu (event)
2999 (x-popup-menu event gdb-memory-format-menu))
3001 (defun gdb-memory-format-menu-1 (event)
3003 (save-selected-window
3004 (select-window (posn-window (event-start event)))
3005 (let* ((selection (gdb-memory-format-menu event))
3006 (binding (and selection (lookup-key gdb-memory-format-menu
3007 (vector (car selection))))))
3008 (if binding (call-interactively binding)))))
3010 (defmacro def-gdb-memory-unit (name unit-size doc)
3011 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3013 DOC is an optional documentation string."
3014 `(defun ,name () ,(when doc doc)
3016 (customize-set-variable 'gdb-memory-unit ,unit-size)
3017 (gdb-invalidate-memory 'update)))
3019 (def-gdb-memory-unit gdb-memory-unit-giant 8
3020 "Set the unit size to giant words (eight bytes).")
3022 (def-gdb-memory-unit gdb-memory-unit-word 4
3023 "Set the unit size to words (four bytes).")
3025 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3026 "Set the unit size to halfwords (two bytes).")
3028 (def-gdb-memory-unit gdb-memory-unit-byte 1
3029 "Set the unit size to bytes.")
3031 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3032 "Define a function NAME which show new address in memory buffer.
3034 The defined function switches Memory buffer to show address
3035 stored in ADDRESS-VAR variable.
3037 DOC is an optional documentation string."
3041 (let ((gdb-memory-address ,address-var))
3042 (gdb-invalidate-memory))))
3044 (def-gdb-memory-show-page gdb-memory-show-previous-page
3045 gdb-memory-prev-page)
3047 (def-gdb-memory-show-page gdb-memory-show-next-page
3048 gdb-memory-next-page)
3050 (defvar gdb-memory-unit-map
3051 (let ((map (make-sparse-keymap)))
3052 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3054 "Keymap to select units in the header line.")
3056 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
3057 "Menu of units in the header line.")
3059 (define-key gdb-memory-unit-menu [giantwords]
3060 '(menu-item "Giant words" gdb-memory-unit-giant
3061 :button (:radio . (equal gdb-memory-unit 8))))
3062 (define-key gdb-memory-unit-menu [words]
3063 '(menu-item "Words" gdb-memory-unit-word
3064 :button (:radio . (equal gdb-memory-unit 4))))
3065 (define-key gdb-memory-unit-menu [halfwords]
3066 '(menu-item "Halfwords" gdb-memory-unit-halfword
3067 :button (:radio . (equal gdb-memory-unit 2))))
3068 (define-key gdb-memory-unit-menu [bytes]
3069 '(menu-item "Bytes" gdb-memory-unit-byte
3070 :button (:radio . (equal gdb-memory-unit 1))))
3072 (defun gdb-memory-unit-menu (event)
3074 (x-popup-menu event gdb-memory-unit-menu))
3076 (defun gdb-memory-unit-menu-1 (event)
3078 (save-selected-window
3079 (select-window (posn-window (event-start event)))
3080 (let* ((selection (gdb-memory-unit-menu event))
3081 (binding (and selection (lookup-key gdb-memory-unit-menu
3082 (vector (car selection))))))
3083 (if binding (call-interactively binding)))))
3085 (defvar gdb-memory-font-lock-keywords
3086 '(;; <__function.name+n>
3087 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
3089 "Font lock keywords used in `gdb-memory-mode'.")
3091 (defvar gdb-memory-header
3096 'face font-lock-warning-face
3097 'help-echo "mouse-1: decrement address"
3098 'mouse-face 'mode-line-highlight
3099 'local-map (gdb-make-header-line-mouse-map
3101 #'gdb-memory-show-previous-page))
3104 'face font-lock-warning-face
3105 'help-echo "mouse-1: increment address"
3106 'mouse-face 'mode-line-highlight
3107 'local-map (gdb-make-header-line-mouse-map
3109 #'gdb-memory-show-next-page))
3111 (propertize gdb-memory-address
3112 'face font-lock-warning-face
3113 'help-echo "mouse-1: set start address"
3114 'mouse-face 'mode-line-highlight
3115 'local-map (gdb-make-header-line-mouse-map
3117 #'gdb-memory-set-address-event))
3119 (propertize (number-to-string gdb-memory-rows)
3120 'face font-lock-warning-face
3121 'help-echo "mouse-1: set number of columns"
3122 'mouse-face 'mode-line-highlight
3123 'local-map (gdb-make-header-line-mouse-map
3125 #'gdb-memory-set-rows))
3127 (propertize (number-to-string gdb-memory-columns)
3128 'face font-lock-warning-face
3129 'help-echo "mouse-1: set number of columns"
3130 'mouse-face 'mode-line-highlight
3131 'local-map (gdb-make-header-line-mouse-map
3133 #'gdb-memory-set-columns))
3135 (propertize gdb-memory-format
3136 'face font-lock-warning-face
3137 'help-echo "mouse-3: select display format"
3138 'mouse-face 'mode-line-highlight
3139 'local-map gdb-memory-format-map)
3141 (propertize (number-to-string gdb-memory-unit)
3142 'face font-lock-warning-face
3143 'help-echo "mouse-3: select unit size"
3144 'mouse-face 'mode-line-highlight
3145 'local-map gdb-memory-unit-map)))
3146 "Header line used in `gdb-memory-mode'.")
3148 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3149 "Major mode for examining memory.
3151 \\{gdb-memory-mode-map}"
3152 (setq header-line-format gdb-memory-header)
3153 (set (make-local-variable 'font-lock-defaults)
3154 '(gdb-memory-font-lock-keywords))
3155 (run-mode-hooks 'gdb-memory-mode-hook)
3156 'gdb-invalidate-memory)
3158 (defun gdb-memory-buffer-name ()
3159 (concat "*memory of " (gdb-get-target-string) "*"))
3161 (def-gdb-display-buffer
3162 gdb-display-memory-buffer
3164 "Display memory contents.")
3166 (defun gdb-frame-memory-buffer ()
3167 "Display memory contents in a new frame."
3169 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3170 (special-display-frame-alist
3174 ,@gdb-frame-parameters)))
3175 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3178 ;;; Disassembly view
3180 (defun gdb-disassembly-buffer-name ()
3181 (gdb-current-context-buffer-name
3182 (concat "disassembly of " (gdb-get-target-string))))
3184 (def-gdb-display-buffer
3185 gdb-display-disassembly-buffer
3186 'gdb-disassembly-buffer
3187 "Display disassembly for current stack frame.")
3189 (def-gdb-preempt-display-buffer
3190 gdb-preemptively-display-disassembly-buffer
3191 'gdb-disassembly-buffer)
3193 (def-gdb-frame-for-buffer
3194 gdb-frame-disassembly-buffer
3195 'gdb-disassembly-buffer
3196 "Display disassembly in a new frame.")
3198 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3199 (let* ((frame (gdb-current-buffer-frame))
3200 (file (bindat-get-field frame 'fullname))
3201 (line (bindat-get-field frame 'line)))
3203 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3204 gdb-disassembly-handler
3205 ;; We update disassembly only after we have actual frame information
3206 ;; about all threads, so no there's `update' signal in this list
3207 '(start update-disassembly))
3209 (def-gdb-auto-update-handler
3210 gdb-disassembly-handler
3211 gdb-invalidate-disassembly
3212 gdb-disassembly-handler-custom
3215 (gdb-set-buffer-rules
3216 'gdb-disassembly-buffer
3217 'gdb-disassembly-buffer-name
3218 'gdb-disassembly-mode
3219 'gdb-invalidate-disassembly)
3221 (defvar gdb-disassembly-font-lock-keywords
3222 '(;; <__function.name+n>
3223 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3224 (1 font-lock-function-name-face))
3225 ;; 0xNNNNNNNN <__function.name+n>: opcode
3226 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3227 (4 font-lock-keyword-face))
3228 ;; %register(at least i386)
3229 ("%\\sw+" . font-lock-variable-name-face)
3230 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3231 (1 font-lock-comment-face)
3232 (2 font-lock-function-name-face))
3233 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3234 "Font lock keywords used in `gdb-disassembly-mode'.")
3236 (defvar gdb-disassembly-mode-map
3238 (let ((map (make-sparse-keymap)))
3239 (suppress-keymap map)
3240 (define-key map "q" 'kill-this-buffer)
3243 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3244 "Major mode for GDB disassembly information.
3246 \\{gdb-disassembly-mode-map}"
3247 ;; TODO Rename overlay variable for disassembly mode
3248 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3249 (setq fringes-outside-margins t)
3250 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3251 (set (make-local-variable 'font-lock-defaults)
3252 '(gdb-disassembly-font-lock-keywords))
3253 (run-mode-hooks 'gdb-disassembly-mode-hook)
3254 'gdb-invalidate-disassembly)
3256 (defun gdb-disassembly-handler-custom ()
3257 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3258 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3260 (table (make-gdb-table))
3262 (dolist (instr instructions)
3263 (gdb-table-add-row table
3265 (bindat-get-field instr 'address)
3266 (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
3267 (bindat-get-field instr 'inst)))
3268 (when (string-equal (bindat-get-field instr 'address)
3271 (setq marked-line (length (gdb-table-rows table)))
3272 (setq fringe-indicator-alist
3273 (if (string-equal gdb-frame-number "0")
3275 '((overlay-arrow . hollow-right-triangle)))))))
3276 (insert (gdb-table-string table " "))
3277 (gdb-disassembly-place-breakpoints)
3278 ;; Mark current position with overlay arrow and scroll window to
3281 (let ((window (get-buffer-window (current-buffer) 0)))
3282 (set-window-point window (gdb-mark-line marked-line gdb-disassembly-position))))
3284 (gdb-current-context-mode-name
3285 (concat "Disassembly: "
3286 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3288 (defun gdb-disassembly-place-breakpoints ()
3289 (gdb-remove-breakpoint-icons (point-min) (point-max))
3290 (dolist (breakpoint gdb-breakpoints-list)
3291 (let* ((breakpoint (cdr breakpoint))
3292 (bptno (bindat-get-field breakpoint 'number))
3293 (flag (bindat-get-field breakpoint 'enabled))
3294 (address (bindat-get-field breakpoint 'addr)))
3296 (goto-char (point-min))
3297 (if (re-search-forward (concat "^" address) nil t)
3298 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3301 (defvar gdb-breakpoints-header
3303 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3306 (gdb-propertize-header "Threads" gdb-threads-buffer
3307 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3309 ;;; Breakpoints view
3310 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3311 "Major mode for gdb breakpoints.
3313 \\{gdb-breakpoints-mode-map}"
3314 (setq header-line-format gdb-breakpoints-header)
3315 (run-mode-hooks 'gdb-breakpoints-mode-hook)
3316 'gdb-invalidate-breakpoints)
3318 (defun gdb-toggle-breakpoint ()
3319 "Enable/disable breakpoint at current line of breakpoints buffer."
3323 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3326 (concat (if (string-equal "y" (bindat-get-field breakpoint 'enabled))
3329 (bindat-get-field breakpoint 'number)))
3330 (error "Not recognized as break/watchpoint line")))))
3332 (defun gdb-delete-breakpoint ()
3333 "Delete the breakpoint at current line of breakpoints buffer."
3337 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3339 (gud-basic-call (concat "-break-delete " (bindat-get-field breakpoint 'number)))
3340 (error "Not recognized as break/watchpoint line")))))
3342 (defun gdb-goto-breakpoint (&optional event)
3343 "Go to the location of breakpoint at current line of
3344 breakpoints buffer."
3345 (interactive (list last-input-event))
3346 (if event (posn-set-point (event-end event)))
3347 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3348 (let ((window (get-buffer-window gud-comint-buffer)))
3349 (if window (save-selected-window (select-window window))))
3352 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3354 (let ((bptno (bindat-get-field breakpoint 'number))
3355 (file (bindat-get-field breakpoint 'fullname))
3356 (line (bindat-get-field breakpoint 'line)))
3357 (save-selected-window
3358 (let* ((buffer (find-file-noselect
3359 (if (file-exists-p file) file
3360 (cdr (assoc bptno gdb-location-alist)))))
3361 (window (or (gdb-display-source-buffer buffer)
3362 (display-buffer buffer))))
3363 (setq gdb-source-window window)
3364 (with-current-buffer buffer
3365 (goto-char (point-min))
3366 (forward-line (1- (string-to-number line)))
3367 (set-window-point window (point))))))
3368 (error "Not recognized as break/watchpoint line")))))
3371 ;; Frames buffer. This displays a perpetually correct bactrack trace.
3373 (def-gdb-trigger-and-handler
3374 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3375 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3378 (gdb-set-buffer-rules
3380 'gdb-stack-buffer-name
3382 'gdb-invalidate-frames)
3384 (defun gdb-frame-location (frame)
3385 "Return \" of file:line\" or \" of library\" for structure FRAME.
3387 FRAME must have either \"file\" and \"line\" members or \"from\"
3389 (let ((file (bindat-get-field frame 'file))
3390 (line (bindat-get-field frame 'line))
3391 (from (bindat-get-field frame 'from)))
3392 (let ((res (or (and file line (concat file ":" line))
3394 (if res (concat " of " res) ""))))
3396 (defun gdb-stack-list-frames-custom ()
3397 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3398 (table (make-gdb-table)))
3399 (set-marker gdb-stack-position nil)
3400 (dolist (frame stack)
3401 (gdb-table-add-row table
3403 (bindat-get-field frame 'level)
3406 (bindat-get-field frame 'func)
3407 (if gdb-stack-buffer-locations
3408 (gdb-frame-location frame) "")
3409 (if gdb-stack-buffer-addresses
3410 (concat " at " (bindat-get-field frame 'addr)) "")))
3411 `(mouse-face highlight
3412 help-echo "mouse-2, RET: Select frame"
3414 (insert (gdb-table-string table " ")))
3415 (when (and gdb-frame-number
3416 (gdb-buffer-shows-main-thread-p))
3417 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3418 gdb-stack-position))
3420 (gdb-current-context-mode-name "Frames")))
3422 (defun gdb-stack-buffer-name ()
3423 (gdb-current-context-buffer-name
3424 (concat "stack frames of " (gdb-get-target-string))))
3426 (def-gdb-display-buffer
3427 gdb-display-stack-buffer
3429 "Display backtrace of current stack.")
3431 (def-gdb-preempt-display-buffer
3432 gdb-preemptively-display-stack-buffer
3433 'gdb-stack-buffer nil t)
3435 (def-gdb-frame-for-buffer
3436 gdb-frame-stack-buffer
3438 "Display backtrace of current stack in a new frame.")
3440 (defvar gdb-frames-mode-map
3441 (let ((map (make-sparse-keymap)))
3442 (suppress-keymap map)
3443 (define-key map "q" 'kill-this-buffer)
3444 (define-key map "\r" 'gdb-select-frame)
3445 (define-key map [mouse-2] 'gdb-select-frame)
3446 (define-key map [follow-link] 'mouse-face)
3449 (defvar gdb-frames-font-lock-keywords
3450 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3451 "Font lock keywords used in `gdb-frames-mode'.")
3453 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3454 "Major mode for gdb call stack.
3456 \\{gdb-frames-mode-map}"
3457 (setq gdb-stack-position (make-marker))
3458 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3459 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3460 (set (make-local-variable 'font-lock-defaults)
3461 '(gdb-frames-font-lock-keywords))
3462 (run-mode-hooks 'gdb-frames-mode-hook)
3463 'gdb-invalidate-frames)
3465 (defun gdb-select-frame (&optional event)
3466 "Select the frame and display the relevant source."
3467 (interactive (list last-input-event))
3468 (if event (posn-set-point (event-end event)))
3469 (let ((frame (get-text-property (point) 'gdb-frame)))
3471 (if (gdb-buffer-shows-main-thread-p)
3472 (let ((new-level (bindat-get-field frame 'level)))
3473 (setq gdb-frame-number new-level)
3474 (gdb-input (list (concat "-stack-select-frame " new-level) 'ignore))
3476 (error "Could not select frame for non-current thread."))
3477 (error "Not recognized as frame line"))))
3481 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3482 (def-gdb-trigger-and-handler
3483 gdb-invalidate-locals
3484 (concat (gdb-current-context-command "-stack-list-locals") " --simple-values")
3485 gdb-locals-handler gdb-locals-handler-custom
3488 (gdb-set-buffer-rules
3490 'gdb-locals-buffer-name
3492 'gdb-invalidate-locals)
3494 (defvar gdb-locals-watch-map
3495 (let ((map (make-sparse-keymap)))
3496 (suppress-keymap map)
3497 (define-key map "\r" 'gud-watch)
3498 (define-key map [mouse-2] 'gud-watch)
3500 "Keymap to create watch expression of a complex data type local variable.")
3502 (defvar gdb-edit-locals-map-1
3503 (let ((map (make-sparse-keymap)))
3504 (suppress-keymap map)
3505 (define-key map "\r" 'gdb-edit-locals-value)
3506 (define-key map [mouse-2] 'gdb-edit-locals-value)
3508 "Keymap to edit value of a simple data type local variable.")
3510 (defun gdb-edit-locals-value (&optional event)
3511 "Assign a value to a variable displayed in the locals buffer."
3512 (interactive (list last-input-event))
3514 (if event (posn-set-point (event-end event)))
3516 (let* ((var (bindat-get-field
3517 (get-text-property (point) 'gdb-local-variable) 'name))
3518 (value (read-string (format "New value (%s): " var))))
3520 (concat "-gdb-set variable " var " = " value)))))
3522 ;; Dont display values of arrays or structures.
3523 ;; These can be expanded using gud-watch.
3524 (defun gdb-locals-handler-custom ()
3525 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3526 (table (make-gdb-table)))
3527 (dolist (local locals-list)
3528 (let ((name (bindat-get-field local 'name))
3529 (value (bindat-get-field local 'value))
3530 (type (bindat-get-field local 'type)))
3532 (string-match "\\0x" value))
3533 (add-text-properties 0 (length name)
3534 `(mouse-face highlight
3535 help-echo "mouse-2: create watch expression"
3536 local-map ,gdb-locals-watch-map)
3538 (add-text-properties 0 (length value)
3539 `(mouse-face highlight
3540 help-echo "mouse-2: edit value"
3541 local-map ,gdb-edit-locals-map-1)
3546 (propertize type 'font-lock-face font-lock-type-face)
3547 (propertize name 'font-lock-face font-lock-variable-name-face)
3549 `(gdb-local-variable ,local))))
3550 (insert (gdb-table-string table " "))
3552 (gdb-current-context-mode-name
3553 (concat "Locals: " (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3555 (defvar gdb-locals-header
3557 (gdb-propertize-header "Locals" gdb-locals-buffer
3560 (gdb-propertize-header "Registers" gdb-registers-buffer
3561 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3563 (defvar gdb-locals-mode-map
3564 (let ((map (make-sparse-keymap)))
3565 (suppress-keymap map)
3566 (define-key map "q" 'kill-this-buffer)
3567 (define-key map "\t" '(lambda ()
3569 (gdb-set-window-buffer
3570 (gdb-get-buffer-create
3571 'gdb-registers-buffer
3572 gdb-thread-number) t)))
3575 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3576 "Major mode for gdb locals.
3578 \\{gdb-locals-mode-map}"
3579 (setq header-line-format gdb-locals-header)
3580 (run-mode-hooks 'gdb-locals-mode-hook)
3581 'gdb-invalidate-locals)
3583 (defun gdb-locals-buffer-name ()
3584 (gdb-current-context-buffer-name
3585 (concat "locals of " (gdb-get-target-string))))
3587 (def-gdb-display-buffer
3588 gdb-display-locals-buffer
3590 "Display local variables of current stack and their values.")
3592 (def-gdb-preempt-display-buffer
3593 gdb-preemptively-display-locals-buffer
3594 'gdb-locals-buffer nil t)
3596 (def-gdb-frame-for-buffer
3597 gdb-frame-locals-buffer
3599 "Display local variables of current stack and their values in a new frame.")
3602 ;; Registers buffer.
3604 (def-gdb-trigger-and-handler
3605 gdb-invalidate-registers
3606 (concat (gdb-current-context-command "-data-list-register-values") " x")
3607 gdb-registers-handler
3608 gdb-registers-handler-custom
3611 (gdb-set-buffer-rules
3612 'gdb-registers-buffer
3613 'gdb-registers-buffer-name
3615 'gdb-invalidate-registers)
3617 (defun gdb-registers-handler-custom ()
3618 (when gdb-register-names
3619 (let ((register-values (bindat-get-field (gdb-json-partial-output) 'register-values))
3620 (table (make-gdb-table)))
3621 (dolist (register register-values)
3622 (let* ((register-number (bindat-get-field register 'number))
3623 (value (bindat-get-field register 'value))
3624 (register-name (nth (string-to-number register-number)
3625 gdb-register-names)))
3629 (propertize register-name 'font-lock-face font-lock-variable-name-face)
3630 (if (member register-number gdb-changed-registers)
3631 (propertize value 'font-lock-face font-lock-warning-face)
3633 `(mouse-face highlight
3634 help-echo "mouse-2: edit value"
3635 gdb-register-name ,register-name))))
3636 (insert (gdb-table-string table " ")))
3638 (gdb-current-context-mode-name "Registers"))))
3640 (defun gdb-edit-register-value (&optional event)
3641 "Assign a value to a register displayed in the registers buffer."
3642 (interactive (list last-input-event))
3644 (if event (posn-set-point (event-end event)))
3646 (let* ((var (bindat-get-field
3647 (get-text-property (point) 'gdb-register-name)))
3648 (value (read-string (format "New value (%s): " var))))
3650 (concat "-gdb-set variable $" var " = " value)))))
3652 (defvar gdb-registers-mode-map
3653 (let ((map (make-sparse-keymap)))
3654 (suppress-keymap map)
3655 (define-key map "\r" 'gdb-edit-register-value)
3656 (define-key map [mouse-2] 'gdb-edit-register-value)
3657 (define-key map "q" 'kill-this-buffer)
3658 (define-key map "\t" '(lambda ()
3660 (gdb-set-window-buffer
3661 (gdb-get-buffer-create
3663 gdb-thread-number) t)))
3666 (defvar gdb-registers-header
3668 (gdb-propertize-header "Locals" gdb-locals-buffer
3669 "mouse-1: select" mode-line-highlight mode-line-inactive)
3671 (gdb-propertize-header "Registers" gdb-registers-buffer
3672 nil nil mode-line)))
3674 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3675 "Major mode for gdb registers.
3677 \\{gdb-registers-mode-map}"
3678 (setq header-line-format gdb-registers-header)
3679 (run-mode-hooks 'gdb-registers-mode-hook)
3680 'gdb-invalidate-registers)
3682 (defun gdb-registers-buffer-name ()
3683 (gdb-current-context-buffer-name
3684 (concat "registers of " (gdb-get-target-string))))
3686 (def-gdb-display-buffer
3687 gdb-display-registers-buffer
3688 'gdb-registers-buffer
3689 "Display integer register contents.")
3691 (def-gdb-preempt-display-buffer
3692 gdb-preemptively-display-registers-buffer
3693 'gdb-registers-buffer nil t)
3695 (def-gdb-frame-for-buffer
3696 gdb-frame-registers-buffer
3697 'gdb-registers-buffer
3698 "Display integer register contents in a new frame.")
3700 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3701 (defun gdb-get-changed-registers ()
3702 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3703 (not (gdb-pending-p 'gdb-get-changed-registers)))
3707 "-data-list-changed-registers"
3708 'gdb-changed-registers-handler))
3709 (gdb-add-pending 'gdb-get-changed-registers))))
3711 (defun gdb-changed-registers-handler ()
3712 (gdb-delete-pending 'gdb-get-changed-registers)
3713 (setq gdb-changed-registers nil)
3714 (dolist (register-number (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3715 (push register-number gdb-changed-registers)))
3717 (defun gdb-register-names-handler ()
3718 ;; Don't use gdb-pending-triggers because this handler is called
3719 ;; only once (in gdb-init-1)
3720 (setq gdb-register-names nil)
3721 (dolist (register-name (bindat-get-field (gdb-json-partial-output) 'register-names))
3722 (push register-name gdb-register-names))
3723 (setq gdb-register-names (reverse gdb-register-names)))
3726 (defun gdb-get-source-file-list ()
3727 "Create list of source files for current GDB session.
3728 If buffers already exist for any of these files, gud-minor-mode
3730 (goto-char (point-min))
3731 (while (re-search-forward gdb-source-file-regexp nil t)
3732 (push (match-string 1) gdb-source-file-list))
3733 (dolist (buffer (buffer-list))
3734 (with-current-buffer buffer
3735 (when (member buffer-file-name gdb-source-file-list)
3736 (gdb-init-buffer))))
3737 (gdb-force-mode-line-update
3738 (propertize "ready" 'face font-lock-variable-name-face)))
3740 (defun gdb-get-main-selected-frame ()
3741 "Trigger for `gdb-frame-handler' which uses main current
3742 thread. Called from `gdb-update'."
3743 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3746 (list (gdb-current-context-command "-stack-info-frame") 'gdb-frame-handler))
3747 (gdb-add-pending 'gdb-get-main-selected-frame))))
3749 (defun gdb-frame-handler ()
3750 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3751 overlay arrow in source buffer."
3752 (gdb-delete-pending 'gdb-get-main-selected-frame)
3753 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3755 (setq gdb-selected-frame (bindat-get-field frame 'func))
3756 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3757 (setq gdb-frame-number (bindat-get-field frame 'level))
3758 (setq gdb-frame-address (bindat-get-field frame 'addr))
3759 (let ((line (bindat-get-field frame 'line)))
3760 (setq gdb-selected-line (or (and line (string-to-number line))
3761 nil)) ; don't fail if line is nil
3762 (when line ; obey the current file only if we have line info
3763 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3764 (gud-display-frame)))
3765 (if gud-overlay-arrow-position
3766 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3767 (position (marker-position gud-overlay-arrow-position)))
3769 (with-current-buffer buffer
3770 (setq fringe-indicator-alist
3771 (if (string-equal gdb-frame-number "0")
3773 '((overlay-arrow . hollow-right-triangle))))
3774 (setq gud-overlay-arrow-position (make-marker))
3775 (set-marker gud-overlay-arrow-position position))))))))
3777 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3779 (defun gdb-get-prompt ()
3780 "Find prompt for GDB session."
3781 (goto-char (point-min))
3782 (setq gdb-prompt-name nil)
3783 (re-search-forward gdb-prompt-name-regexp nil t)
3784 (setq gdb-prompt-name (match-string 1))
3785 ;; Insert first prompt.
3786 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3788 ;;;; Window management
3789 (defun gdb-display-buffer (buf dedicated &optional frame)
3792 If BUF is already displayed in some window, show it, deiconifying
3793 the frame if necessary. Otherwise, find least recently used
3794 window and show BUF there, if the window is not used for GDB
3795 already, in which case that window is splitted first."
3796 (let ((answer (get-buffer-window buf (or frame 0))))
3798 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3799 (let ((window (get-lru-window)))
3800 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3802 (let* ((largest (get-largest-window))
3803 (cur-size (window-height largest)))
3804 (setq answer (split-window largest))
3805 (set-window-buffer answer buf)
3806 (set-window-dedicated-p answer dedicated)
3808 (set-window-buffer window buf)
3811 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3812 "Find window displaying a buffer with the same
3813 `gdb-buffer-type' as BUF and show BUF there. If no such window
3814 exists, just call `gdb-display-buffer' for BUF. If the window
3815 found is already dedicated, split window according to
3816 SPLIT-HORIZONTAL and show BUF in the new window."
3818 (when (not (get-buffer-window buf))
3819 (let* ((buf-type (gdb-buffer-type buf))
3821 (get-window-with-predicate
3824 (gdb-buffer-type (window-buffer w)))
3825 (not (window-dedicated-p w)))))))
3827 (set-window-buffer existing-window buf)
3828 (let ((dedicated-window
3829 (get-window-with-predicate
3832 (gdb-buffer-type (window-buffer w)))))))
3833 (if dedicated-window
3835 (split-window dedicated-window nil split-horizontal) buf)
3836 (gdb-display-buffer buf t))))))
3837 (error "Null buffer")))
3839 ;;; Shared keymap initialization:
3841 (let ((menu (make-sparse-keymap "GDB-Windows")))
3842 (define-key gud-menu-map [displays]
3843 `(menu-item "GDB-Windows" ,menu
3844 :visible (eq gud-minor-mode 'gdbmi)))
3845 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3846 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3847 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3848 (define-key menu [disassembly]
3849 '("Disassembly" . gdb-display-disassembly-buffer))
3850 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3851 (define-key menu [inferior]
3852 '("IO" . gdb-display-io-buffer))
3853 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3854 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3855 (define-key menu [breakpoints]
3856 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3858 (let ((menu (make-sparse-keymap "GDB-Frames")))
3859 (define-key gud-menu-map [frames]
3860 `(menu-item "GDB-Frames" ,menu
3861 :visible (eq gud-minor-mode 'gdbmi)))
3862 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3863 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3864 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3865 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
3866 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3867 (define-key menu [inferior]
3868 '("IO" . gdb-frame-io-buffer))
3869 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3870 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3871 (define-key menu [breakpoints]
3872 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3874 (let ((menu (make-sparse-keymap "GDB-MI")))
3875 (define-key menu [gdb-customize]
3876 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3877 :help "Customize Gdb Graphical Mode options."))
3878 (define-key menu [gdb-many-windows]
3879 '(menu-item "Display Other Windows" gdb-many-windows
3880 :help "Toggle display of locals, stack and breakpoint information"
3881 :button (:toggle . gdb-many-windows)))
3882 (define-key menu [gdb-restore-windows]
3883 '(menu-item "Restore Window Layout" gdb-restore-windows
3884 :help "Restore standard layout for debug session."))
3885 (define-key menu [sep1]
3887 (define-key menu [all-threads]
3888 '(menu-item "GUD controls all threads"
3891 (setq gdb-gud-control-all-threads t))
3892 :help "GUD start/stop commands apply to all threads"
3893 :button (:radio . gdb-gud-control-all-threads)))
3894 (define-key menu [current-thread]
3895 '(menu-item "GUD controls current thread"
3898 (setq gdb-gud-control-all-threads nil))
3899 :help "GUD start/stop commands apply to current thread only"
3900 :button (:radio . (not gdb-gud-control-all-threads))))
3901 (define-key menu [sep2]
3903 (define-key menu [gdb-customize-reasons]
3904 '(menu-item "Customize switching..."
3907 (customize-option 'gdb-switch-reasons))))
3908 (define-key menu [gdb-switch-when-another-stopped]
3909 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped gdb-switch-when-another-stopped
3910 "Automatically switch to stopped thread"
3911 "GDB thread switching %s"
3912 "Switch to stopped thread"))
3913 (define-key gud-menu-map [mi]
3914 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3916 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3917 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3918 ;; tool-bar-local-item-from-menu here because it appends new buttons
3919 ;; to toolbar from right to left while we want our A/T throttle to
3920 ;; show up right before Run button.
3921 (define-key-after gud-tool-bar-map [all-threads]
3922 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3923 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3924 :visible (and (eq gud-minor-mode 'gdbmi)
3926 (not gdb-gud-control-all-threads)))
3929 (define-key-after gud-tool-bar-map [current-thread]
3930 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3931 :image (find-image '((:type xpm :file "gud/all.xpm")))
3932 :visible (and (eq gud-minor-mode 'gdbmi)
3934 gdb-gud-control-all-threads))
3937 (defun gdb-frame-gdb-buffer ()
3938 "Display GUD buffer in a new frame."
3940 (let ((special-display-regexps (append special-display-regexps '(".*")))
3941 (special-display-frame-alist
3942 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3943 gdb-frame-parameters)))
3944 (same-window-regexps nil))
3945 (display-buffer gud-comint-buffer)))
3947 (defun gdb-display-gdb-buffer ()
3948 "Display GUD buffer."
3950 (let ((same-window-regexps nil))
3951 (select-window (display-buffer gud-comint-buffer nil 0))))
3953 (defun gdb-set-window-buffer (name &optional ignore-dedicated)
3954 "Set buffer of selected window to NAME and dedicate window.
3956 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
3957 window is dedicated."
3958 (when ignore-dedicated
3959 (set-window-dedicated-p (selected-window) nil))
3960 (set-window-buffer (selected-window) (get-buffer name))
3961 (set-window-dedicated-p (selected-window) t))
3963 (defun gdb-setup-windows ()
3964 "Layout the window pattern for `gdb-many-windows'."
3965 (gdb-display-locals-buffer)
3966 (gdb-display-stack-buffer)
3967 (delete-other-windows)
3968 (gdb-display-breakpoints-buffer)
3969 (delete-other-windows)
3971 (pop-to-buffer gud-comint-buffer)
3972 (split-window nil ( / ( * (window-height) 3) 4))
3973 (split-window nil ( / (window-height) 3))
3974 (split-window-horizontally)
3976 (gdb-set-window-buffer (gdb-locals-buffer-name))
3979 (if gud-last-last-frame
3980 (gud-find-file (car gud-last-last-frame))
3982 (gud-find-file gdb-main-file)
3983 ;; Put buffer list in window if we
3984 ;; can't find a source file.
3985 (list-buffers-noselect))))
3986 (setq gdb-source-window (selected-window))
3987 (split-window-horizontally)
3989 (gdb-set-window-buffer
3990 (gdb-get-buffer-create 'gdb-inferior-io))
3992 (gdb-set-window-buffer (gdb-stack-buffer-name))
3993 (split-window-horizontally)
3995 (gdb-set-window-buffer (if gdb-show-threads-by-default
3996 (gdb-threads-buffer-name)
3997 (gdb-breakpoints-buffer-name)))
4000 (defcustom gdb-many-windows nil
4001 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4002 In this case it starts with two windows: one displaying the GUD
4003 buffer and the other with the source file with the main routine
4004 of the debugged program. Non-nil means display the layout shown for
4010 (defun gdb-many-windows (arg)
4011 "Toggle the number of windows in the basic arrangement.
4012 With arg, display additional buffers iff arg is positive."
4014 (setq gdb-many-windows
4016 (not gdb-many-windows)
4017 (> (prefix-numeric-value arg) 0)))
4018 (message (format "Display of other windows %sabled"
4019 (if gdb-many-windows "en" "dis")))
4020 (if (and gud-comint-buffer
4021 (buffer-name gud-comint-buffer))
4023 (gdb-restore-windows)
4026 (defun gdb-restore-windows ()
4027 "Restore the basic arrangement of windows used by gdb.
4028 This arrangement depends on the value of `gdb-many-windows'."
4030 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
4031 (delete-other-windows)
4032 (if gdb-many-windows
4034 (when (or gud-last-last-frame gdb-show-main)
4038 (if gud-last-last-frame
4039 (gud-find-file (car gud-last-last-frame))
4040 (gud-find-file gdb-main-file)))
4041 (setq gdb-source-window (selected-window))
4045 "Exit a debugging session cleanly.
4046 Kills the gdb buffers, and resets variables and the source buffers."
4047 (dolist (buffer (buffer-list))
4048 (unless (eq buffer gud-comint-buffer)
4049 (with-current-buffer buffer
4050 (if (eq gud-minor-mode 'gdbmi)
4051 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4053 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4054 (setq gud-minor-mode nil)
4055 (kill-local-variable 'tool-bar-map)
4056 (kill-local-variable 'gdb-define-alist))))))
4057 (setq gdb-disassembly-position nil)
4058 (setq overlay-arrow-variable-list
4059 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4060 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4061 (setq gdb-stack-position nil)
4062 (setq overlay-arrow-variable-list
4063 (delq 'gdb-stack-position overlay-arrow-variable-list))
4064 (setq gdb-thread-position nil)
4065 (setq overlay-arrow-variable-list
4066 (delq 'gdb-thread-position overlay-arrow-variable-list))
4067 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4068 (setq gud-running nil)
4069 (setq gdb-active-process nil)
4070 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4072 (defun gdb-get-source-file ()
4073 "Find the source file where the program starts and display it with related
4074 buffers, if required."
4075 (goto-char (point-min))
4076 (if (re-search-forward gdb-source-file-regexp nil t)
4077 (setq gdb-main-file (match-string 1)))
4078 (if gdb-many-windows
4080 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4082 (let ((pop-up-windows t))
4083 (display-buffer (gud-find-file gdb-main-file))))))
4086 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4087 "Put string PUTSTRING in front of POS in the current buffer.
4088 PUTSTRING is displayed by putting an overlay into the current buffer with a
4089 `before-string' string that has a `display' property whose value is
4091 (let ((string (make-string 1 ?x))
4092 (buffer (current-buffer)))
4093 (setq putstring (copy-sequence putstring))
4094 (let ((overlay (make-overlay pos pos buffer))
4096 (list (list 'margin 'left-margin) putstring))))
4097 (put-text-property 0 1 'display prop string)
4099 (add-text-properties 0 1 sprops string))
4100 (overlay-put overlay 'put-break t)
4101 (overlay-put overlay 'before-string string))))
4103 ;;from remove-images
4104 (defun gdb-remove-strings (start end &optional buffer)
4105 "Remove strings between START and END in BUFFER.
4106 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4107 BUFFER nil or omitted means use the current buffer."
4109 (setq buffer (current-buffer)))
4110 (dolist (overlay (overlays-in start end))
4111 (when (overlay-get overlay 'put-break)
4112 (delete-overlay overlay))))
4114 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4115 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4116 (start (- (car posns) 1))
4117 (end (+ (cdr posns) 1))
4118 (putstring (if enabled "B" "b"))
4119 (source-window (get-buffer-window (current-buffer) 0)))
4120 (add-text-properties
4121 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4124 (add-text-properties
4125 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4126 (add-text-properties
4127 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4128 (gdb-remove-breakpoint-icons start end)
4129 (if (display-images-p)
4130 (if (>= (or left-fringe-width
4131 (if source-window (car (window-fringes source-window)))
4132 gdb-buffer-fringe-width) 8)
4135 `(left-fringe breakpoint
4138 'breakpoint-disabled))
4140 'gdb-enabled enabled)
4141 (when (< left-margin-width 2)
4142 (save-current-buffer
4143 (setq left-margin-width 2)
4147 left-margin-width right-margin-width))))
4150 (or breakpoint-enabled-icon
4151 (setq breakpoint-enabled-icon
4152 (find-image `((:type xpm :data
4153 ,breakpoint-xpm-data
4154 :ascent 100 :pointer hand)
4156 ,breakpoint-enabled-pbm-data
4157 :ascent 100 :pointer hand)))))
4158 (or breakpoint-disabled-icon
4159 (setq breakpoint-disabled-icon
4160 (find-image `((:type xpm :data
4161 ,breakpoint-xpm-data
4162 :conversion disabled
4163 :ascent 100 :pointer hand)
4165 ,breakpoint-disabled-pbm-data
4166 :ascent 100 :pointer hand))))))
4170 (when (< left-margin-width 2)
4171 (save-current-buffer
4172 (setq left-margin-width 2)
4173 (let ((window (get-buffer-window (current-buffer) 0)))
4176 window left-margin-width right-margin-width)))))
4178 (propertize putstring
4179 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
4182 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4183 (gdb-remove-strings start end)
4184 (if (display-images-p)
4185 (remove-images start end))
4187 (setq left-margin-width 0)
4188 (let ((window (get-buffer-window (current-buffer) 0)))
4191 window left-margin-width right-margin-width)))))
4195 ;; arch-tag: 1b41ea2b-f364-4cec-8f35-e02e4fe01912
4196 ;;; gdb-mi.el ends here