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 Reset whenever current thread changes.")
139 ;; Used to show overlay arrow in source buffer. All set in
140 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
141 ;; these but rely on buffer-local thread information instead.
142 (defvar gdb-selected-frame nil
143 "Name of selected function for main current thread.")
144 (defvar gdb-selected-file nil
145 "Name of selected file for main current thread.")
146 (defvar gdb-selected-line nil
147 "Number of selected line for main current thread.")
149 (defvar gdb-threads-list nil
150 "Associative list of threads provided by \"-thread-info\" MI command.
152 Keys are thread numbers (in strings) and values are structures as
153 returned from -thread-info by `gdb-json-partial-output'. Updated in
154 `gdb-thread-list-handler-custom'.")
156 (defvar gdb-running-threads-count nil
157 "Number of currently running threads.
159 Nil means that no information is available.
161 Updated in `gdb-thread-list-handler-custom'.")
163 (defvar gdb-stopped-threads-count nil
164 "Number of currently stopped threads.
166 See also `gdb-running-threads-count'.")
168 (defvar gdb-breakpoints-list nil
169 "Associative list of breakpoints provided by \"-break-list\" MI command.
171 Keys are breakpoint numbers (in string) and values are structures
172 as returned from \"-break-list\" by `gdb-json-partial-output'
173 \(\"body\" field is used). Updated in
174 `gdb-breakpoints-list-handler-custom'.")
176 (defvar gdb-current-language nil)
177 (defvar gdb-var-list nil
178 "List of variables in watch window.
179 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS) where
180 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
181 (defvar gdb-main-file nil "Source file from which program execution begins.")
183 ;; Overlay arrow markers
184 (defvar gdb-stack-position nil)
185 (defvar gdb-thread-position nil)
186 (defvar gdb-disassembly-position nil)
188 (defvar gdb-location-alist nil
189 "Alist of breakpoint numbers and full filenames. Only used for files that
191 (defvar gdb-active-process nil
192 "GUD tooltips display variable values when t, and macro definitions otherwise.")
193 (defvar gdb-error "Non-nil when GDB is reporting an error.")
194 (defvar gdb-macro-info nil
195 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
196 (defvar gdb-register-names nil "List of register names.")
197 (defvar gdb-changed-registers nil
198 "List of changed register numbers (strings).")
199 (defvar gdb-buffer-fringe-width nil)
200 (defvar gdb-last-command nil)
201 (defvar gdb-prompt-name nil)
202 (defvar gdb-token-number 0)
203 (defvar gdb-handler-alist '())
204 (defvar gdb-handler-number nil)
205 (defvar gdb-source-file-list nil
206 "List of source files for the current executable.")
207 (defvar gdb-first-done-or-error t)
208 (defvar gdb-source-window nil)
209 (defvar gdb-inferior-status nil)
210 (defvar gdb-continuation nil)
211 (defvar gdb-filter-output nil
212 "Message to be shown in GUD console.
214 This variable is updated in `gdb-done-or-error' and returned by
215 `gud-gdbmi-marker-filter'.")
217 (defvar gdb-non-stop nil
218 "Indicates whether current GDB session is using non-stop mode.
220 It is initialized to `gdb-non-stop-setting' at the beginning of
223 (defvar gdb-buffer-type nil
224 "One of the symbols bound in `gdb-buffer-rules'.")
225 (make-variable-buffer-local 'gdb-buffer-type)
227 (defvar gdb-output-sink 'nil
228 "The disposition of the output of the current gdb command.
229 Possible values are these symbols:
231 `user' -- gdb output should be copied to the GUD buffer
234 `emacs' -- output should be collected in the partial-output-buffer
235 for subsequent processing by a command. This is the
236 disposition of output generated by commands that
237 gdb mode sends to gdb on its own behalf.")
239 ;; Pending triggers prevent congestion: Emacs won't send two similar
240 ;; consecutive requests.
242 (defvar gdb-pending-triggers '()
243 "A list of trigger functions which have not yet been handled.
245 Elements are either function names or pairs (buffer . function)")
247 (defmacro gdb-add-pending (item)
248 `(push ,item gdb-pending-triggers))
249 (defmacro gdb-pending-p (item)
250 `(member ,item gdb-pending-triggers))
251 (defmacro gdb-delete-pending (item)
252 `(setq gdb-pending-triggers
253 (delete ,item gdb-pending-triggers)))
255 (defmacro gdb-wait-for-pending (&rest body)
256 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
258 This function checks `gdb-pending-triggers' value every
259 `gdb-wait-for-pending' seconds."
263 (if (not gdb-pending-triggers)
265 (gdb-wait-for-pending ,@body)))))
269 (defmacro gdb-add-subscriber (publisher subscriber)
270 "Register new PUBLISHER's SUBSCRIBER.
272 SUBSCRIBER must be a pair, where cdr is a function of one
273 argument (see `gdb-emit-signal')."
274 `(add-to-list ',publisher ,subscriber t))
276 (defmacro gdb-delete-subscriber (publisher subscriber)
277 "Unregister SUBSCRIBER from PUBLISHER."
278 `(setq ,publisher (delete ,subscriber
281 (defun gdb-get-subscribers (publisher)
284 (defun gdb-emit-signal (publisher &optional signal)
285 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
286 (dolist (subscriber (gdb-get-subscribers publisher))
287 (funcall (cdr subscriber) signal)))
289 (defvar gdb-buf-publisher '()
290 "Used to invalidate GDB buffers by emitting a signal in
293 Must be a list of pairs with cars being buffers and cdr's being
294 valid signal handlers.")
297 "GDB graphical interface"
299 :link '(info-link "(emacs)GDB Graphical Interface")
302 (defgroup gdb-non-stop nil
303 "GDB non-stop debugging settings"
307 (defgroup gdb-buffers nil
312 (defcustom gdb-debug-log-max 128
313 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
315 :type '(choice (integer :tag "Number of elements")
316 (const :tag "Unlimited" nil))
319 (defcustom gdb-non-stop-setting t
320 "When in non-stop mode, stopped threads can be examined while
321 other threads continue to execute.
323 GDB session needs to be restarted for this setting to take
329 ;; TODO Some commands can't be called with --all (give a notice about
330 ;; it in setting doc)
331 (defcustom gdb-gud-control-all-threads t
332 "When enabled, GUD execution commands affect all threads when
333 in non-stop mode. Otherwise, only current thread is affected."
338 (defcustom gdb-switch-reasons t
339 "List of stop reasons which cause Emacs to switch to the thread
340 which caused the stop. When t, switch to stopped thread no matter
341 what the reason was. When nil, never switch to stopped thread
344 This setting is used in non-stop mode only. In all-stop mode,
345 Emacs always switches to the thread which caused the stop."
346 ;; exited, exited-normally and exited-signalled are not
347 ;; thread-specific stop reasons and therefore are not included in
350 (const :tag "All reasons" t)
351 (set :tag "Selection of reasons..."
352 (const :tag "A breakpoint was reached." "breakpoint-hit")
353 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
354 (const :tag "A read watchpoint was triggered." "read-watchpoint-trigger")
355 (const :tag "An access watchpoint was triggered." "access-watchpoint-trigger")
356 (const :tag "Function finished execution." "function-finished")
357 (const :tag "Location reached." "location-reached")
358 (const :tag "Watchpoint has gone out of scope" "watchpoint-scope")
359 (const :tag "End of stepping range reached." "end-stepping-range")
360 (const :tag "Signal received (like interruption)." "signal-received"))
361 (const :tag "None" nil))
364 :link '(info-link "(gdb)GDB/MI Async Records"))
366 (defcustom gdb-stopped-hooks nil
367 "This variable holds a list of functions to be called whenever
370 Each function takes one argument, a parsed MI response, which
371 contains fields of corresponding MI *stopped async record:
373 ((stopped-threads . \"all\")
375 (frame (line . \"38\")
376 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
377 (file . \"server.c\")
378 (args ((value . \"0x804b038\")
381 (addr . \"0x0804869e\"))
382 (reason . \"end-stepping-range\"))
384 Note that \"reason\" is only present in non-stop debugging mode.
386 `gdb-get-field' may be used to access the fields of response.
388 Each function is called after the new current thread was selected
389 and GDB buffers were updated in `gdb-stopped'."
390 :type '(repeat function)
393 :link '(info-link "(gdb)GDB/MI Async Records"))
395 (defcustom gdb-switch-when-another-stopped t
396 "When nil, Emacs won't switch to stopped thread if some other
397 stopped thread is already selected."
402 (defcustom gdb-stack-buffer-locations t
403 "Show file information or library names in stack buffers."
408 (defcustom gdb-stack-buffer-addresses nil
409 "Show frame addresses in stack buffers."
414 (defcustom gdb-thread-buffer-verbose-names t
415 "Show long thread names in threads buffer."
420 (defcustom gdb-thread-buffer-arguments t
421 "Show function arguments in threads buffer."
426 (defcustom gdb-thread-buffer-locations t
427 "Show file information or library names in threads buffer."
432 (defcustom gdb-thread-buffer-addresses nil
433 "Show addresses for thread frames in threads buffer."
438 (defcustom gdb-show-threads-by-default nil
439 "Show threads list buffer instead of breakpoints list by
445 (defvar gdb-debug-log nil
446 "List of commands sent to and replies received from GDB.
447 Most recent commands are listed first. This list stores only the last
448 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
451 (defcustom gdb-enable-debug nil
452 "Non-nil means record the process input and output in `gdb-debug-log'."
457 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
458 "Shell command for generating a list of defined macros in a source file.
459 This list is used to display the #define directive associated
460 with an identifier as a tooltip. It works in a debug session with
461 GDB, when `gud-tooltip-mode' is t.
463 Set `gdb-cpp-define-alist-flags' for any include paths or
469 (defcustom gdb-cpp-define-alist-flags ""
470 "Preprocessor flags for `gdb-cpp-define-alist-program'."
475 (defcustom gdb-create-source-file-list t
476 "Non-nil means create a list of files from which the executable was built.
477 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
478 line for a long time when starting, possibly because your executable was
479 built from a large number of files. This allows quicker initialization
480 but means that these files are not automatically enabled for debugging,
481 e.g., you won't be able to click in the fringe to set a breakpoint until
482 execution has already stopped there."
487 (defcustom gdb-show-main nil
488 "Non-nil means display source file containing the main routine at startup.
489 Also display the main routine in the disassembly buffer if present."
494 ; Note: This mode requires a separate buffer for inferior IO.
495 (defconst gdb-use-separate-io-buffer t)
497 (defun gdb-force-mode-line-update (status)
498 (let ((buffer gud-comint-buffer))
499 (if (and buffer (buffer-name buffer))
500 (with-current-buffer buffer
501 (setq mode-line-process
503 (process-status (get-buffer-process buffer)) status))
504 ;; Force mode line redisplay soon.
505 (force-mode-line-update)))))
507 (defun gdb-enable-debug (arg)
508 "Toggle logging of transaction between Emacs and Gdb.
509 The log is stored in `gdb-debug-log' as an alist with elements
510 whose cons is send, send-item or recv and whose cdr is the string
511 being transferred. This list may grow up to a size of
512 `gdb-debug-log-max' after which the oldest element (at the end of
513 the list) is deleted every time a new one is added (at the front)."
515 (setq gdb-enable-debug
517 (not gdb-enable-debug)
518 (> (prefix-numeric-value arg) 0)))
519 (message (format "Logging of transaction %sabled"
520 (if gdb-enable-debug "en" "dis"))))
522 ;; These two are used for menu and toolbar
523 (defun gdb-control-all-threads ()
524 "Switch to non-stop/A mode."
526 (setq gdb-gud-control-all-threads t)
527 (message "Now in non-stop/A mode."))
529 (defun gdb-control-current-thread ()
530 "Switch to non-stop/T mode."
532 (setq gdb-gud-control-all-threads nil)
533 (message "Now in non-stop/T mode."))
535 (defun gdb-find-watch-expression ()
536 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
537 (varnum (car var)) expr array)
538 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
539 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
540 (component-list (split-string (match-string 2 varnum) "\\." t)))
541 (setq expr (nth 1 var1))
542 (setq varnumlet (car var1))
543 (dolist (component component-list)
544 (setq var2 (assoc varnumlet gdb-var-list))
545 (setq expr (concat expr
546 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
547 (concat "[" component "]")
548 (concat "." component))))
549 (setq varnumlet (concat varnumlet "." component)))
552 ;; noall is used for commands which don't take --all, but only
554 (defun gdb-gud-context-command (command &optional noall)
555 "When `gdb-non-stop' is t, add --thread option to COMMAND if
556 `gdb-gud-control-all-threads' is nil and --all option otherwise.
557 If NOALL is t, always add --thread option no matter what
558 `gdb-gud-control-all-threads' value is.
560 When `gdb-non-stop' is nil, return COMMAND unchanged."
562 (if (and gdb-gud-control-all-threads
564 (concat command " --all ")
565 (gdb-current-context-command command t))
568 ;; TODO Document this. We use noarg when not in gud-def
569 (defun gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
572 (gdb-gud-context-command cmd1 noall)
573 cmd2) (when (not noarg) 'arg)))
576 (defun gdb (command-line)
577 "Run gdb on program FILE in buffer *gud-FILE*.
578 The directory containing FILE becomes the initial working directory
579 and source-file directory for your debugger.
581 If `gdb-many-windows' is nil (the default value) then gdb just
582 pops up the GUD buffer unless `gdb-show-main' is t. In this case
583 it starts with two windows: one displaying the GUD buffer and the
584 other with the source file with the main routine of the inferior.
586 If `gdb-many-windows' is t, regardless of the value of
587 `gdb-show-main', the layout below will appear. Keybindings are
588 shown in some of the buffers.
590 Watch expressions appear in the speedbar/slowbar.
592 The following commands help control operation :
594 `gdb-many-windows' - Toggle the number of windows gdb uses.
595 `gdb-restore-windows' - To restore the window layout.
597 See Info node `(emacs)GDB Graphical Interface' for a more
598 detailed description of this mode.
601 +----------------------------------------------------------------------+
603 +-----------------------------------+----------------------------------+
604 | GUD buffer (I/O of GDB) | Locals buffer |
608 +-----------------------------------+----------------------------------+
609 | Source buffer | I/O buffer (of debugged program) |
617 +-----------------------------------+----------------------------------+
618 | Stack buffer | Breakpoints buffer |
619 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
620 | | RET gdb-goto-breakpoint |
621 | | D gdb-delete-breakpoint |
622 +-----------------------------------+----------------------------------+"
624 (interactive (list (gud-query-cmdline 'gdb)))
626 (when (and gud-comint-buffer
627 (buffer-name gud-comint-buffer)
628 (get-buffer-process gud-comint-buffer)
629 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
630 (gdb-restore-windows)
632 "Multiple debugging requires restarting in text command mode"))
634 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
635 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
636 (setq comint-input-sender 'gdb-send)
638 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
639 "Set temporary breakpoint at current line.")
641 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
642 "\C-j" "Set execution address to current line.")
644 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
645 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
646 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
647 (gud-def gud-pstar "print* %e" nil
648 "Evaluate C dereferenced pointer expression at point.")
650 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
652 "Step one source line with display.")
653 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
655 "Step one instruction with display.")
656 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
658 "Step one line (skip functions).")
659 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
661 "Step one instruction (skip functions).")
662 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
664 "Continue with display.")
665 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
667 "Finish executing current function.")
668 (gud-def gud-run "-exec-run"
672 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
673 (gud-call "break %f:%l" arg)
677 (gud-call "break *%a" arg)))
678 "\C-b" "Set breakpoint at current line or address.")
680 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
681 (gud-call "clear %f:%l" arg)
685 (gud-call "clear *%a" arg)))
686 "\C-d" "Remove breakpoint at current line or address.")
688 ;; -exec-until doesn't support --all yet
689 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
690 (gud-call "-exec-until %f:%l" arg)
694 (gud-call "-exec-until *%a" arg)))
695 "\C-u" "Continue to current line or address.")
696 ;; TODO Why arg here?
698 gud-go (gud-call (if gdb-active-process
699 (gdb-gud-context-command "-exec-continue")
701 nil "Start or continue execution.")
703 ;; For debugging Emacs only.
707 "pp1 " (if (eq (buffer-local-value
708 'major-mode (window-buffer)) 'speedbar-mode)
709 (gdb-find-watch-expression) "%e")) arg)
710 nil "Print the Emacs s-expression.")
712 (define-key gud-minor-mode-map [left-margin mouse-1]
713 'gdb-mouse-set-clear-breakpoint)
714 (define-key gud-minor-mode-map [left-fringe mouse-1]
715 'gdb-mouse-set-clear-breakpoint)
716 (define-key gud-minor-mode-map [left-margin C-mouse-1]
717 'gdb-mouse-toggle-breakpoint-margin)
718 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
719 'gdb-mouse-toggle-breakpoint-fringe)
721 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
723 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
725 (define-key gud-minor-mode-map [left-margin mouse-3]
727 (define-key gud-minor-mode-map [left-fringe mouse-3]
730 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
732 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
734 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
736 (define-key gud-minor-mode-map [left-margin C-mouse-3]
739 (local-set-key "\C-i" 'gud-gdb-complete-command)
740 (setq gdb-first-prompt t)
741 (setq gud-running nil)
743 (run-hooks 'gdb-mode-hook))
747 (setq gdb-selected-frame nil
749 gdb-thread-number nil
751 gdb-pending-triggers nil
752 gdb-output-sink 'user
753 gdb-location-alist nil
754 gdb-source-file-list nil
757 gdb-handler-alist '()
758 gdb-handler-number nil
760 gdb-first-done-or-error t
761 gdb-buffer-fringe-width (car (window-fringes))
763 gdb-source-window nil
764 gdb-inferior-status nil
766 gdb-buf-publisher '()
768 gdb-breakpoints-list '()
769 gdb-non-stop gdb-non-stop-setting)
771 (setq gdb-buffer-type 'gdbmi)
773 (gdb-force-mode-line-update
774 (propertize "initializing..." 'face font-lock-variable-name-face))
775 (when gdb-use-separate-io-buffer
776 (gdb-get-buffer-create 'gdb-inferior-io)
777 (gdb-clear-inferior-io)
778 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
780 ;; Needs GDB 6.4 onwards
781 (list (concat "-inferior-tty-set "
782 (process-tty-name (get-process "gdb-inferior")))
784 (if (eq window-system 'w32)
785 (gdb-input (list "-gdb-set new-console off" 'ignore)))
786 (gdb-input (list "-gdb-set height 0" 'ignore))
789 (gdb-input (list "-gdb-set non-stop 1" 'ignore))
790 (gdb-input (list "-gdb-set target-async 1" 'ignore)))
792 ;; find source file and compilation directory here
794 ; Needs GDB 6.2 onwards.
795 (list "-file-list-exec-source-files" 'gdb-get-source-file-list))
796 (if gdb-create-source-file-list
798 ; Needs GDB 6.0 onwards.
799 (list "-file-list-exec-source-file" 'gdb-get-source-file)))
801 (list "-gdb-show prompt" 'gdb-get-prompt)))
803 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
805 (defun gdb-create-define-alist ()
806 "Create an alist of #define directives for GUD tooltips."
807 (let* ((file (buffer-file-name))
809 (with-output-to-string
810 (with-current-buffer standard-output
813 ;; call-process doesn't work with remote file names.
814 (not (file-remote-p default-directory))
815 (call-process shell-file-name file
816 (list t nil) nil "-c"
817 (concat gdb-cpp-define-alist-program " "
818 gdb-cpp-define-alist-flags))))))
819 (define-list (split-string output "\n" t))
821 (setq gdb-define-alist nil)
822 (dolist (define define-list)
823 (setq name (nth 1 (split-string define "[( ]")))
824 (push (cons name define) gdb-define-alist))))
826 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
827 (defvar tooltip-use-echo-area)
829 (defun gdb-tooltip-print (expr)
831 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
832 (goto-char (point-min))
834 (if (search-forward "=" nil t)
835 (concat expr (buffer-substring (- (point) 2) (point-max)))
837 ;; remove newline for gud-tooltip-echo-area
838 (substring string 0 (- (length string) 1))))
839 (or gud-tooltip-echo-area tooltip-use-echo-area
840 (not (display-graphic-p)))))
842 ;; If expr is a macro for a function don't print because of possible dangerous
843 ;; side-effects. Also printing a function within a tooltip generates an
844 ;; unexpected starting annotation (phase error).
845 (defun gdb-tooltip-print-1 (expr)
846 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
847 (goto-char (point-min))
848 (if (search-forward "expands to: " nil t)
849 (unless (looking-at "\\S-+.*(.*).*")
851 (list (concat "print " expr)
852 `(lambda () (gdb-tooltip-print ,expr))))))))
854 (defun gdb-init-buffer ()
855 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
856 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
857 (when gud-tooltip-mode
858 (make-local-variable 'gdb-define-alist)
859 (gdb-create-define-alist)
860 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
862 (defmacro gdb-if-arrow (arrow-position &rest body)
864 (let ((buffer (marker-buffer ,arrow-position)) (line))
865 (if (equal buffer (window-buffer (posn-window end)))
866 (with-current-buffer buffer
867 (when (or (equal start end)
868 (equal (posn-point start)
869 (marker-position ,arrow-position)))
872 (defun gdb-mouse-until (event)
873 "Continue running until a source line past the current line.
874 The destination source line can be selected either by clicking
875 with mouse-3 on the fringe/margin or dragging the arrow
876 with mouse-1 (default bindings)."
878 (let ((start (event-start event))
879 (end (event-end event)))
880 (gdb-if-arrow gud-overlay-arrow-position
881 (setq line (line-number-at-pos (posn-point end)))
882 (gud-call (concat "until " (number-to-string line))))
883 (gdb-if-arrow gdb-disassembly-position
885 (goto-line (line-number-at-pos (posn-point end)))
887 (gud-call (concat "until *%a"))))))
889 (defun gdb-mouse-jump (event)
890 "Set execution address/line.
891 The destination source line can be selected either by clicking with C-mouse-3
892 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
893 Unlike `gdb-mouse-until' the destination address can be before the current
894 line, and no execution takes place."
896 (let ((start (event-start event))
897 (end (event-end event)))
898 (gdb-if-arrow gud-overlay-arrow-position
899 (setq line (line-number-at-pos (posn-point end)))
901 (gud-call (concat "tbreak " (number-to-string line)))
902 (gud-call (concat "jump " (number-to-string line)))))
903 (gdb-if-arrow gdb-disassembly-position
905 (goto-line (line-number-at-pos (posn-point end)))
908 (gud-call (concat "tbreak *%a"))
909 (gud-call (concat "jump *%a")))))))
911 (defcustom gdb-show-changed-values t
912 "If non-nil change the face of out of scope variables and changed values.
913 Out of scope variables are suppressed with `shadow' face.
914 Changed values are highlighted with the face `font-lock-warning-face'."
919 (defcustom gdb-max-children 40
920 "Maximum number of children before expansion requires confirmation."
925 (defcustom gdb-delete-out-of-scope t
926 "If non-nil delete watch expressions automatically when they go out of scope."
931 (defcustom gdb-speedbar-auto-raise nil
932 "If non-nil raise speedbar every time display of watch expressions is\
938 (defcustom gdb-use-colon-colon-notation nil
939 "If non-nil use FUN::VAR format to display variables in the speedbar."
944 (defun gdb-speedbar-auto-raise (arg)
945 "Toggle automatic raising of the speedbar for watch expressions.
946 With prefix argument ARG, automatically raise speedbar if ARG is
947 positive, otherwise don't automatically raise it."
949 (setq gdb-speedbar-auto-raise
951 (not gdb-speedbar-auto-raise)
952 (> (prefix-numeric-value arg) 0)))
953 (message (format "Auto raising %sabled"
954 (if gdb-speedbar-auto-raise "en" "dis"))))
956 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
957 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
959 (declare-function tooltip-identifier-from-point "tooltip" (point))
961 (defun gud-watch (&optional arg event)
962 "Watch expression at point.
963 With arg, enter name of variable to be watched in the minibuffer."
964 (interactive (list current-prefix-arg last-input-event))
965 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
966 (if (eq minor-mode 'gdbmi)
968 (if event (posn-set-point (event-end event)))
970 (save-selected-window
973 (completing-read "Name of variable: "
974 'gud-gdb-complete-command)
975 (if (and transient-mark-mode mark-active)
976 (buffer-substring (region-beginning) (region-end))
977 (concat (if (eq major-mode 'gdb-registers-mode) "$")
978 (tooltip-identifier-from-point (point)))))))
979 (set-text-properties 0 (length expr) nil expr)
981 (list (concat"-var-create - * " expr "")
982 `(lambda () (gdb-var-create-handler ,expr)))))))
983 (message "gud-watch is a no-op in this mode."))))
985 (defun gdb-var-create-handler (expr)
986 (let* ((result (gdb-json-partial-output)))
987 (if (not (gdb-get-field result 'msg))
991 (gdb-get-field result 'name)
992 (if (and (string-equal gdb-current-language "c")
993 gdb-use-colon-colon-notation gdb-selected-frame)
994 (setq expr (concat gdb-selected-frame "::" expr))
996 (gdb-get-field result 'numchild)
997 (gdb-get-field result 'type)
998 (gdb-get-field result 'value)
1000 (push var gdb-var-list)
1002 (unless (string-equal
1003 speedbar-initial-expansion-list-name "GUD")
1004 (speedbar-change-initial-expansion-list "GUD"))
1007 (concat "-var-evaluate-expression " (car var))
1008 `(lambda () (gdb-var-evaluate-expression-handler
1010 (message-box "No symbol \"%s\" in current context." expr))))
1012 (defun gdb-speedbar-update ()
1013 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1014 (not (gdb-pending-p 'gdb-speedbar-timer)))
1015 ;; Dummy command to update speedbar even when idle.
1016 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn))
1017 ;; Keep gdb-pending-triggers non-nil till end.
1018 (gdb-add-pending 'gdb-speedbar-timer)))
1020 (defun gdb-speedbar-timer-fn ()
1021 (if gdb-speedbar-auto-raise
1022 (raise-frame speedbar-frame))
1023 (gdb-delete-pending 'gdb-speedbar-timer)
1024 (speedbar-timer-fn))
1026 (defun gdb-var-evaluate-expression-handler (varnum changed)
1027 (goto-char (point-min))
1028 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1029 (let ((var (assoc varnum gdb-var-list)))
1031 (if changed (setcar (nthcdr 5 var) 'changed))
1032 (setcar (nthcdr 4 var) (read (match-string 1)))))
1033 (gdb-speedbar-update))
1035 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1036 (defun gdb-var-list-children (varnum)
1038 (list (concat "-var-update " varnum) 'ignore))
1040 (list (concat "-var-list-children --all-values "
1042 `(lambda () (gdb-var-list-children-handler ,varnum)))))
1044 (defconst gdb-var-list-children-regexp
1045 "child={.*?name=\"\\(.+?\\)\".*?,exp=\"\\(.+?\\)\".*?,\
1046 numchild=\"\\(.+?\\)\".*?,value=\\(\".*?\"\\).*?,type=\"\\(.+?\\)\".*?}")
1048 (defun gdb-var-list-children-handler (varnum)
1049 (goto-char (point-min))
1050 (let ((var-list nil))
1051 (catch 'child-already-watched
1052 (dolist (var gdb-var-list)
1053 (if (string-equal varnum (car var))
1056 (while (re-search-forward gdb-var-list-children-regexp nil t)
1057 (let ((varchild (list (match-string 1)
1061 (read (match-string 4))
1063 (if (assoc (car varchild) gdb-var-list)
1064 (throw 'child-already-watched nil))
1065 (push varchild var-list))))
1066 (push var var-list)))
1067 (setq gdb-var-list (nreverse var-list))))
1068 (gdb-speedbar-update))
1070 (defun gdb-var-set-format (format)
1071 "Set the output format for a variable displayed in the speedbar."
1072 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1075 (list (concat "-var-set-format " varnum " " format) 'ignore))
1078 (defun gdb-var-delete-1 (varnum)
1080 (list (concat "-var-delete " varnum) 'ignore))
1081 (setq gdb-var-list (delq var gdb-var-list))
1082 (dolist (varchild gdb-var-list)
1083 (if (string-match (concat (car var) "\\.") (car varchild))
1084 (setq gdb-var-list (delq varchild gdb-var-list)))))
1086 (defun gdb-var-delete ()
1087 "Delete watch expression at point from the speedbar."
1089 (let ((text (speedbar-line-text)))
1090 (string-match "\\(\\S-+\\)" text)
1091 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1093 (if (string-match "\\." (car var))
1094 (message-box "Can only delete a root expression")
1095 (gdb-var-delete-1 varnum)))))
1097 (defun gdb-var-delete-children (varnum)
1098 "Delete children of variable object at point from the speedbar."
1100 (list (concat "-var-delete -c " varnum) 'ignore)))
1102 (defun gdb-edit-value (text token indent)
1103 "Assign a value to a variable displayed in the speedbar."
1104 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1105 (varnum (car var)) (value))
1106 (setq value (read-string "New value: "))
1108 (list (concat "-var-assign " varnum " " value)
1109 `(lambda () (gdb-edit-value-handler ,value))))))
1111 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1113 (defun gdb-edit-value-handler (value)
1114 (goto-char (point-min))
1115 (if (re-search-forward gdb-error-regexp nil t)
1116 (message-box "Invalid number or expression (%s)" value)))
1118 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1119 (defun gdb-var-update ()
1120 (if (not (gdb-pending-p 'gdb-var-update))
1122 (list "-var-update --all-values *" 'gdb-var-update-handler)))
1123 (gdb-add-pending 'gdb-var-update))
1125 (defconst gdb-var-update-regexp
1126 "{.*?name=\"\\(.*?\\)\".*?,\\(?:value=\\(\".*?\"\\),\\)?.*?\
1127 in_scope=\"\\(.*?\\)\".*?}")
1129 (defun gdb-var-update-handler ()
1130 (dolist (var gdb-var-list)
1131 (setcar (nthcdr 5 var) nil))
1132 (goto-char (point-min))
1133 (while (re-search-forward gdb-var-update-regexp nil t)
1134 (let* ((varnum (match-string 1))
1135 (var (assoc varnum gdb-var-list)))
1137 (let ((match (match-string 3)))
1138 (cond ((string-equal match "false")
1139 (if gdb-delete-out-of-scope
1140 (gdb-var-delete-1 varnum)
1141 (setcar (nthcdr 5 var) 'out-of-scope)))
1142 ((string-equal match "true")
1143 (setcar (nthcdr 5 var) 'changed)
1144 (setcar (nthcdr 4 var)
1145 (read (match-string 2))))
1146 ((string-equal match "invalid")
1147 (gdb-var-delete-1 varnum)))))))
1148 (gdb-delete-pending 'gdb-var-update)
1149 (gdb-speedbar-update))
1151 (defun gdb-speedbar-expand-node (text token indent)
1152 "Expand the node the user clicked on.
1153 TEXT is the text of the button we clicked on, a + or - item.
1154 TOKEN is data related to this node.
1155 INDENT is the current indentation depth."
1156 (cond ((string-match "+" text) ;expand this node
1157 (let* ((var (assoc token gdb-var-list))
1158 (expr (nth 1 var)) (children (nth 2 var)))
1159 (if (or (<= (string-to-number children) gdb-max-children)
1161 (format "%s has %s children. Continue? " expr children)))
1162 (gdb-var-list-children token))))
1163 ((string-match "-" text) ;contract this node
1164 (dolist (var gdb-var-list)
1165 (if (string-match (concat token "\\.") (car var))
1166 (setq gdb-var-list (delq var gdb-var-list))))
1167 (gdb-var-delete-children token)
1168 (speedbar-change-expand-button-char ?+)
1169 (speedbar-delete-subblock indent))
1170 (t (error "Ooops... not sure what to do")))
1171 (speedbar-center-buffer-smartly))
1173 (defun gdb-get-target-string ()
1174 (with-current-buffer gud-comint-buffer
1181 ;; Each buffer has a TYPE -- a symbol that identifies the function
1182 ;; of that particular buffer.
1184 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1185 ;; is constructed specially.
1187 ;; Others are constructed by gdb-get-buffer-create and
1188 ;; named according to the rules set forth in the gdb-buffer-rules
1190 (defvar gdb-buffer-rules '())
1192 (defun gdb-rules-name-maker (rules-entry)
1194 (defun gdb-rules-buffer-mode (rules-entry)
1195 (nth 2 rules-entry))
1196 (defun gdb-rules-update-trigger (rules-entry)
1197 (nth 3 rules-entry))
1199 (defun gdb-update-buffer-name ()
1200 "Rename current buffer according to name-maker associated with
1201 it in `gdb-buffer-rules'."
1202 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1203 gdb-buffer-rules))))
1204 (when f (rename-buffer (funcall f)))))
1206 (defun gdb-current-buffer-rules ()
1207 "Get `gdb-buffer-rules' entry for current buffer type."
1208 (assoc gdb-buffer-type gdb-buffer-rules))
1210 (defun gdb-current-buffer-thread ()
1211 "Get thread object of current buffer from `gdb-threads-list'.
1213 When current buffer is not bound to any thread, return main
1215 (cdr (assoc gdb-thread-number gdb-threads-list)))
1217 (defun gdb-current-buffer-frame ()
1218 "Get current stack frame object for thread of current buffer."
1219 (gdb-get-field (gdb-current-buffer-thread) 'frame))
1221 (defun gdb-buffer-type (buffer)
1222 "Get value of `gdb-buffer-type' for BUFFER."
1223 (with-current-buffer buffer
1226 (defun gdb-buffer-shows-main-thread-p ()
1227 "Return t if current GDB buffer shows main selected thread and
1228 is not bound to it."
1230 (not (local-variable-p 'gdb-thread-number)))
1232 (defun gdb-get-buffer (buffer-type &optional thread)
1233 "Get a specific GDB buffer.
1235 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1236 and `gdb-thread-number' (if provided) must be equal to THREAD."
1238 (dolist (buffer (buffer-list) nil)
1239 (with-current-buffer buffer
1240 (when (and (eq gdb-buffer-type buffer-type)
1242 (equal gdb-thread-number thread)))
1243 (throw 'found buffer))))))
1245 (defun gdb-get-buffer-create (buffer-type &optional thread)
1246 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1247 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1249 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1250 buffer-local variable of the new buffer.
1252 Buffer mode and name are selected according to buffer type.
1254 If buffer has trigger associated with it in `gdb-buffer-rules',
1255 this trigger is subscribed to `gdb-buf-publisher' and called with
1257 (or (gdb-get-buffer buffer-type thread)
1258 (let ((rules (assoc buffer-type gdb-buffer-rules))
1259 (new (generate-new-buffer "limbo")))
1260 (with-current-buffer new
1261 (let ((mode (gdb-rules-buffer-mode rules))
1262 (trigger (gdb-rules-update-trigger rules)))
1263 (when mode (funcall mode))
1264 (setq gdb-buffer-type buffer-type)
1266 (set (make-local-variable 'gdb-thread-number) thread))
1267 (set (make-local-variable 'gud-minor-mode)
1268 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1269 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1270 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1272 (gdb-add-subscriber gdb-buf-publisher
1273 (cons (current-buffer)
1274 (gdb-bind-function-to-buffer trigger (current-buffer))))
1275 (funcall trigger 'update))
1276 (current-buffer))))))
1278 (defun gdb-bind-function-to-buffer (expr buffer)
1279 "Return a function which will evaluate EXPR in BUFFER."
1280 `(lambda (&rest args)
1281 (with-current-buffer ,buffer
1282 (apply ',expr args))))
1284 ;; Used to define all gdb-frame-*-buffer functions except
1285 ;; `gdb-frame-separate-io-buffer'
1286 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1287 "Define a function NAME which shows gdb BUFFER in a separate frame.
1289 DOC is an optional documentation string."
1290 `(defun ,name (&optional thread)
1293 (let ((special-display-regexps (append special-display-regexps '(".*")))
1294 (special-display-frame-alist gdb-frame-parameters))
1295 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1297 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1298 "Define a function NAME which shows gdb BUFFER.
1300 DOC is an optional documentation string."
1301 `(defun ,name (&optional thread)
1305 (gdb-get-buffer-create ,buffer thread) t)))
1307 ;; Used to display windows with thread-bound buffers
1308 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc split-horizontal)
1309 `(defun ,name (&optional thread)
1312 (gdb-preempt-existing-or-display-buffer
1313 (gdb-get-buffer-create ,buffer thread)
1314 ,split-horizontal)))
1316 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1317 ;; at least one and possible more functions. The functions have these
1318 ;; roles in defining a buffer type:
1320 ;; NAME - Return a name for this buffer type.
1322 ;; The remaining function(s) are optional:
1324 ;; MODE - called in a new buffer with no arguments, should establish
1325 ;; the proper mode for the buffer.
1328 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1329 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1331 (setcdr binding rules)
1332 (push (cons buffer-type rules)
1333 gdb-buffer-rules))))
1335 (defun gdb-parent-mode ()
1336 "Generic mode to derive all other GDB buffer modes from."
1337 (kill-all-local-variables)
1338 (setq buffer-read-only t)
1339 (buffer-disable-undo)
1340 ;; Delete buffer from gdb-buf-publisher when it's killed
1341 ;; (if it has an associated update trigger)
1346 (let ((trigger (gdb-rules-update-trigger
1347 (gdb-current-buffer-rules))))
1349 (gdb-delete-subscriber
1351 ;; This should match gdb-add-subscriber done in
1352 ;; gdb-get-buffer-create
1353 (cons (current-buffer)
1354 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1357 ;; GUD buffers are an exception to the rules
1358 (gdb-set-buffer-rules 'gdbmi 'error)
1360 ;; Partial-output buffer : This accumulates output from a command executed on
1361 ;; behalf of emacs (rather than the user).
1363 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1364 'gdb-partial-output-name)
1366 (defun gdb-partial-output-name ()
1367 (concat " *partial-output-"
1368 (gdb-get-target-string)
1372 (gdb-set-buffer-rules 'gdb-inferior-io
1373 'gdb-inferior-io-name
1374 'gdb-inferior-io-mode)
1376 (defun gdb-inferior-io-name ()
1377 (concat "*input/output of "
1378 (gdb-get-target-string)
1381 (defun gdb-display-separate-io-buffer ()
1382 "Display IO of debugged program in a separate window."
1384 (if gdb-use-separate-io-buffer
1386 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1388 (defconst gdb-frame-parameters
1389 '((height . 14) (width . 80)
1391 (tool-bar-lines . nil)
1392 (menu-bar-lines . nil)
1393 (minibuffer . nil)))
1395 (defun gdb-frame-separate-io-buffer ()
1396 "Display IO of debugged program in a new frame."
1398 (if gdb-use-separate-io-buffer
1399 (let ((special-display-regexps (append special-display-regexps '(".*")))
1400 (special-display-frame-alist gdb-frame-parameters))
1401 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1403 (defvar gdb-inferior-io-mode-map
1404 (let ((map (make-sparse-keymap)))
1405 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1406 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1407 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1408 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1409 (define-key map "\C-d" 'gdb-separate-io-eof)
1412 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1413 "Major mode for gdb inferior-io."
1414 :syntax-table nil :abbrev-table nil
1415 ;; We want to use comint because it has various nifty and familiar
1416 ;; features. We don't need a process, but comint wants one, so create
1418 (make-comint-in-buffer
1419 "gdb-inferior" (current-buffer) "sleep" nil "1000000000"))
1421 (defun gdb-inferior-filter (proc string)
1422 (unless (string-equal string "")
1423 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1424 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1425 (insert-before-markers string)))
1427 (defun gdb-separate-io-interrupt ()
1428 "Interrupt the program being debugged."
1431 (get-buffer-process gud-comint-buffer) comint-ptyp))
1433 (defun gdb-separate-io-quit ()
1434 "Send quit signal to the program being debugged."
1437 (get-buffer-process gud-comint-buffer) comint-ptyp))
1439 (defun gdb-separate-io-stop ()
1440 "Stop the program being debugged."
1443 (get-buffer-process gud-comint-buffer) comint-ptyp))
1445 (defun gdb-separate-io-eof ()
1446 "Send end-of-file to the program being debugged."
1449 (get-buffer-process gud-comint-buffer)))
1451 (defun gdb-clear-inferior-io ()
1452 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1456 (defconst breakpoint-xpm-data
1458 static char *magick[] = {
1459 /* columns rows colors chars-per-pixel */
1475 "XPM data used for breakpoint icon.")
1477 (defconst breakpoint-enabled-pbm-data
1480 0 0 0 0 1 1 1 1 0 0 0 0
1481 0 0 0 1 1 1 1 1 1 0 0 0
1482 0 0 1 1 1 1 1 1 1 1 0 0
1483 0 1 1 1 1 1 1 1 1 1 1 0
1484 0 1 1 1 1 1 1 1 1 1 1 0
1485 0 1 1 1 1 1 1 1 1 1 1 0
1486 0 1 1 1 1 1 1 1 1 1 1 0
1487 0 0 1 1 1 1 1 1 1 1 0 0
1488 0 0 0 1 1 1 1 1 1 0 0 0
1489 0 0 0 0 1 1 1 1 0 0 0 0"
1490 "PBM data used for enabled breakpoint icon.")
1492 (defconst breakpoint-disabled-pbm-data
1504 0 0 0 1 0 1 0 1 0 0"
1505 "PBM data used for disabled breakpoint icon.")
1507 (defvar breakpoint-enabled-icon nil
1508 "Icon for enabled breakpoint in display margin.")
1510 (defvar breakpoint-disabled-icon nil
1511 "Icon for disabled breakpoint in display margin.")
1513 (declare-function define-fringe-bitmap "fringe.c"
1514 (bitmap bits &optional height width align))
1516 (and (display-images-p)
1517 ;; Bitmap for breakpoint in fringe
1518 (define-fringe-bitmap 'breakpoint
1519 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1520 ;; Bitmap for gud-overlay-arrow in fringe
1521 (define-fringe-bitmap 'hollow-right-triangle
1522 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1524 (defface breakpoint-enabled
1528 "Face for enabled breakpoint icon in fringe."
1531 (defface breakpoint-disabled
1532 '((((class color) (min-colors 88)) :foreground "grey70")
1533 ;; Ensure that on low-color displays that we end up something visible.
1534 (((class color) (min-colors 8) (background light))
1535 :foreground "black")
1536 (((class color) (min-colors 8) (background dark))
1537 :foreground "white")
1538 (((type tty) (class mono))
1540 (t :background "gray"))
1541 "Face for disabled breakpoint icon in fringe."
1545 (defun gdb-send (proc string)
1546 "A comint send filter for gdb."
1547 (with-current-buffer gud-comint-buffer
1548 (let ((inhibit-read-only t))
1549 (remove-text-properties (point-min) (point-max) '(face))))
1550 ;; mimic <RET> key to repeat previous command in GDB
1551 (if (not (string= "" string))
1552 (setq gdb-last-command string)
1553 (if gdb-last-command (setq string gdb-last-command)))
1554 (if gdb-enable-debug
1555 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1556 (if (string-match "^-" string)
1559 (setq gdb-first-done-or-error t)
1560 (process-send-string proc (concat string "\n")))
1562 (if (string-match "\\\\$" string)
1563 (setq gdb-continuation (concat gdb-continuation string "\n"))
1564 (setq gdb-first-done-or-error t)
1565 (process-send-string proc (concat "-interpreter-exec console \""
1566 gdb-continuation string "\"\n"))
1567 (setq gdb-continuation nil))))
1569 (defun gdb-input (item)
1570 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1571 (setq gdb-token-number (1+ gdb-token-number))
1572 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1573 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1574 (process-send-string (get-buffer-process gud-comint-buffer)
1575 (concat (car item) "\n")))
1577 ;; NOFRAME is used for gud execution control commands
1578 (defun gdb-current-context-command (command &optional noframe)
1579 "Add --thread and --frame options to gdb COMMAND.
1581 Option values are taken from `gdb-thread-number' and
1582 `gdb-frame-number'. If `gdb-thread-number' is nil, COMMAND is
1583 returned unchanged. If `gdb-frame-number' is nil of NOFRAME is t,
1584 then no --frame option is added."
1585 ;; gdb-frame-number may be nil while gdb-thread-number is non-nil
1586 ;; (when current thread is running)
1587 (if gdb-thread-number
1588 (concat command " --thread " gdb-thread-number
1589 (if (not (or noframe (not gdb-frame-number)))
1590 (concat " --frame " gdb-frame-number) "")
1594 (defun gdb-current-context-buffer-name (name)
1595 "Add thread information and asterisks to string NAME.
1597 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1599 (if (local-variable-p 'gdb-thread-number)
1600 (format " (bound to thread %s)" gdb-thread-number)
1604 (defun gdb-current-context-mode-name (mode)
1605 "Add thread information to MODE which is to be used as
1608 (if gdb-thread-number
1609 (format " [thread %s]" gdb-thread-number)
1613 (defcustom gud-gdb-command-name "gdb -i=mi"
1614 "Default command to execute an executable under the GDB debugger."
1619 (setq gud-running nil)
1620 (setq gdb-output-sink 'user)
1621 (setq gdb-pending-triggers nil))
1623 (defun gdb-update ()
1624 "Update buffers showing status of debug session."
1625 (when gdb-first-prompt
1626 (gdb-force-mode-line-update
1627 (propertize "initializing..." 'face font-lock-variable-name-face))
1629 (setq gdb-first-prompt nil))
1630 ;; We may need to update gdb-threads-list so we can use
1631 (gdb-get-buffer-create 'gdb-threads-buffer)
1632 ;; gdb-break-list is maintained in breakpoints handler
1633 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1635 (gdb-emit-signal gdb-buf-publisher 'update)
1637 (gdb-get-main-selected-frame)
1639 (gdb-get-changed-registers)
1641 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1642 (dolist (var gdb-var-list)
1643 (setcar (nthcdr 5 var) nil))
1646 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1647 ;; because we may need to update current gud-running value without
1648 ;; changing current thread (see gdb-running)
1649 (defun gdb-setq-thread-number (number)
1650 "Only this function must be used to change `gdb-thread-number'
1651 value to NUMBER, because `gud-running' and `gdb-frame-number'
1652 need to be updated appropriately when current thread changes."
1653 (setq gdb-thread-number number)
1654 (setq gdb-frame-number "0")
1655 (gdb-update-gud-running))
1657 (defun gdb-update-gud-running ()
1658 "Set `gud-running' and `gdb-frame-number' according to the state
1661 `gdb-frame-number' is set to nil if new current thread is
1664 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1665 cannot be reliably used to determine whether or not execution
1666 control buttons should be shown in menu or toolbar. Use
1667 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1670 For all-stop mode, thread information is unavailable while target
1672 (let ((old-value gud-running))
1674 (string= (gdb-get-field (gdb-current-buffer-thread) 'state)
1676 ;; We change frame number only if the state of current thread has
1677 ;; changed or there's no current thread.
1678 (when (not (eq gud-running old-value))
1679 (if (or gud-running (not (gdb-current-buffer-thread)))
1680 (setq gdb-frame-number nil)
1681 (setq gdb-frame-number "0")))))
1683 (defun gdb-show-run-p ()
1684 "Return t if \"Run/continue\" should be shown on the toolbar."
1685 (or (not gdb-active-process)
1687 (not gdb-gud-control-all-threads)
1690 (and gdb-gud-control-all-threads
1691 (> gdb-stopped-threads-count 0))))
1693 (defun gdb-show-stop-p ()
1694 "Return t if \"Stop\" should be shown on the toolbar."
1696 (not gdb-gud-control-all-threads)
1699 (and gdb-gud-control-all-threads
1700 (> gdb-running-threads-count 0))))
1702 ;; GUD displays the selected GDB frame. This might might not be the current
1703 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1704 ;; visited breakpoint is, use that window.
1705 (defun gdb-display-source-buffer (buffer)
1706 (let* ((last-window (if gud-last-last-frame
1708 (gud-find-file (car gud-last-last-frame)))))
1709 (source-window (or last-window
1710 (if (and gdb-source-window
1711 (window-live-p gdb-source-window))
1712 gdb-source-window))))
1714 (setq gdb-source-window source-window)
1715 (set-window-buffer source-window buffer))
1718 (defun gdb-car< (a b)
1719 (< (car a) (car b)))
1721 (defvar gdbmi-record-list
1722 '((gdb-gdb . "(gdb) \n")
1723 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1724 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1725 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1726 (gdb-console . "~\\(\".*?\"\\)\n")
1727 (gdb-internals . "&\\(\".*?\"\\)\n")
1728 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1729 (gdb-running . "\\*running,\\(.*?\n\\)")
1730 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1731 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1732 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1733 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1734 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1736 (defun gud-gdbmi-marker-filter (string)
1737 "Filter GDB/MI output."
1739 ;; Record transactions if logging is enabled.
1740 (when gdb-enable-debug
1741 (push (cons 'recv string) gdb-debug-log)
1742 (if (and gdb-debug-log-max
1743 (> (length gdb-debug-log) gdb-debug-log-max))
1744 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1746 ;; Recall the left over gud-marker-acc from last time
1747 (setq gud-marker-acc (concat gud-marker-acc string))
1749 ;; Start accumulating output for the GUD buffer
1750 (setq gdb-filter-output "")
1751 (let ((output-record) (output-record-list))
1753 ;; Process all the complete markers in this chunk.
1754 (dolist (gdbmi-record gdbmi-record-list)
1755 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1756 (push (list (match-beginning 0)
1758 (match-string 1 gud-marker-acc)
1759 (match-string 2 gud-marker-acc)
1762 (setq gud-marker-acc
1763 (concat (substring gud-marker-acc 0 (match-beginning 0))
1764 ;; Pad with spaces to preserve position.
1765 (make-string (length (match-string 0 gud-marker-acc)) 32)
1766 (substring gud-marker-acc (match-end 0))))))
1768 (setq output-record-list (sort output-record-list 'gdb-car<))
1770 (dolist (output-record output-record-list)
1771 (let ((record-type (cadr output-record))
1772 (arg1 (nth 2 output-record))
1773 (arg2 (nth 3 output-record)))
1774 (if (eq record-type 'gdb-error)
1775 (gdb-done-or-error arg2 arg1 'error)
1776 (if (eq record-type 'gdb-done)
1777 (gdb-done-or-error arg2 arg1 'done)
1778 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1779 ;; error message on internal stream. Don't print to GUD buffer.
1780 (unless (and (eq record-type 'gdb-internals)
1781 (string-equal (read arg1) "No registers.\n"))
1782 (funcall record-type arg1))))))
1784 (setq gdb-output-sink 'user)
1786 (string-match "^ *" gud-marker-acc)
1787 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1791 (defun gdb-gdb (output-field))
1793 (defun gdb-shell (output-field)
1794 (let ((gdb-output-sink gdb-output-sink))
1795 (setq gdb-filter-output
1796 (concat output-field gdb-filter-output))))
1798 (defun gdb-ignored-notification (output-field))
1800 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1801 (defun gdb-thread-created (output-field))
1802 (defun gdb-thread-exited (output-field)
1803 "Handle =thread-exited async record: unset `gdb-thread-number'
1804 if current thread exited and update threads list."
1805 (let* ((thread-id (gdb-get-field (gdb-json-string output-field) 'id)))
1806 (if (string= gdb-thread-number thread-id)
1807 (gdb-setq-thread-number nil))
1808 ;; When we continue current thread and it quickly exits,
1809 ;; gdb-pending-triggers left after gdb-running disallow us to
1810 ;; properly call -thread-info without --thread option. Thus we
1811 ;; need to use gdb-wait-for-pending.
1812 (gdb-wait-for-pending
1813 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1815 (defun gdb-thread-selected (output-field)
1816 "Handler for =thread-selected MI output record.
1818 Sets `gdb-thread-number' to new id."
1819 (let* ((result (gdb-json-string output-field))
1820 (thread-id (gdb-get-field result 'id)))
1821 (gdb-setq-thread-number thread-id)
1822 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1823 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1824 ;; as usually. Things happen to fast and second call (from
1825 ;; gdb-thread-selected handler) gets cut off by our beloved
1826 ;; gdb-pending-triggers.
1827 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1828 ;; body will get executed when `gdb-pending-triggers` is empty.
1829 (gdb-wait-for-pending
1832 (defun gdb-running (output-field)
1833 (let* ((thread-id (gdb-get-field (gdb-json-string output-field) 'thread-id)))
1834 ;; We reset gdb-frame-number to nil if current thread has gone
1835 ;; running. This can't be done in gdb-thread-list-handler-custom
1836 ;; because we need correct gdb-frame-number by the time
1837 ;; -thread-info command is sent.
1838 (when (or (string-equal thread-id "all")
1839 (string-equal thread-id gdb-thread-number))
1840 (setq gdb-frame-number nil)))
1841 (setq gdb-inferior-status "running")
1842 (gdb-force-mode-line-update
1843 (propertize gdb-inferior-status 'face font-lock-type-face))
1844 (when (not gdb-non-stop)
1845 (setq gud-running t))
1846 (setq gdb-active-process t)
1847 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1849 (defun gdb-starting (output-field)
1850 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1852 (list "-data-list-register-names" 'gdb-register-names-handler))
1853 (setq gdb-inferior-status "running")
1854 (gdb-force-mode-line-update
1855 (propertize gdb-inferior-status 'face font-lock-type-face))
1856 (setq gdb-active-process t)
1857 (setq gud-running t)
1858 ;; GDB doesn't seem to respond to -thread-info before first stop or
1859 ;; thread exit (even in non-stop mode), so this is useless.
1860 ;; Behaviour may change in the future.
1861 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1863 ;; -break-insert -t didn't give a reason before gdb 6.9
1865 (defun gdb-stopped (output-field)
1866 "Given the contents of *stopped MI async record, select new
1867 current thread and update GDB buffers."
1868 ;; Reason is available with target-async only
1869 (let* ((result (gdb-json-string output-field))
1870 (reason (gdb-get-field result 'reason))
1871 (thread-id (gdb-get-field result 'thread-id)))
1873 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1874 ;;; because synchronous GDB doesn't give these fields with CLI.
1877 ;;; ;; Extract the frame position from the marker.
1878 ;;; gud-last-frame (cons file
1879 ;;; (string-to-number
1880 ;;; (match-string 6 gud-marker-acc)))))
1882 (setq gdb-inferior-status (or reason "unknown"))
1883 (gdb-force-mode-line-update
1884 (propertize gdb-inferior-status 'face font-lock-warning-face))
1885 (if (string-equal reason "exited-normally")
1886 (setq gdb-active-process nil))
1888 ;; Select new current thread.
1890 ;; Don't switch if we have no reasons selected
1891 (when gdb-switch-reasons
1892 ;; Switch from another stopped thread only if we have
1893 ;; gdb-switch-when-another-stopped:
1894 (when (or gdb-switch-when-another-stopped
1895 (not (string= "stopped"
1896 (gdb-get-field (gdb-current-buffer-thread) 'state))))
1897 ;; Switch if current reason has been selected or we have no
1899 (if (or (eq gdb-switch-reasons t)
1900 (member reason gdb-switch-reasons))
1902 (gdb-setq-thread-number thread-id)
1903 (message (concat "Switched to thread " thread-id)))
1904 (message (format "Thread %s stopped" thread-id)))))
1906 ;; Print "(gdb)" to GUD console
1907 (when gdb-first-done-or-error
1908 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1910 ;; In non-stop, we update information as soon as another thread gets
1912 (when (or gdb-first-done-or-error
1914 ;; In all-stop this updates gud-running properly as well.
1916 (setq gdb-first-done-or-error nil))
1917 (run-hook-with-args 'gdb-stopped-hooks result)))
1919 ;; Remove the trimmings from log stream containing debugging messages
1920 ;; being produced by GDB's internals, use warning face and send to GUD
1922 (defun gdb-internals (output-field)
1923 (setq gdb-filter-output
1926 (let ((error-message
1927 (read output-field)))
1929 0 (length error-message)
1930 'face font-lock-warning-face
1934 ;; Remove the trimmings from the console stream and send to GUD buffer
1935 ;; (frontend MI commands should not print to this stream)
1936 (defun gdb-console (output-field)
1937 (setq gdb-filter-output
1940 (read output-field))))
1942 (defun gdb-done-or-error (output-field token-number type)
1943 (if (string-equal token-number "")
1944 ;; Output from command entered by user
1946 (setq gdb-output-sink 'user)
1947 (setq token-number nil)
1948 ;; MI error - send to minibuffer
1949 (when (eq type 'error)
1950 ;; Skip "msg=" from `output-field'
1951 (message (read (substring output-field 4)))
1952 ;; Don't send to the console twice. (If it is a console error
1953 ;; it is also in the console stream.)
1954 (setq output-field nil)))
1955 ;; Output from command from frontend.
1956 (setq gdb-output-sink 'emacs))
1958 (gdb-clear-partial-output)
1959 (when gdb-first-done-or-error
1960 (unless (or token-number gud-running)
1961 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1963 (setq gdb-first-done-or-error nil))
1965 (setq gdb-filter-output
1966 (gdb-concat-output gdb-filter-output output-field))
1970 (with-current-buffer
1971 (gdb-get-buffer-create 'gdb-partial-output-buffer)
1973 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
1974 (setq gdb-handler-alist
1975 (assq-delete-all token-number gdb-handler-alist)))))
1977 (defun gdb-concat-output (so-far new)
1978 (let ((sink gdb-output-sink))
1980 ((eq sink 'user) (concat so-far new))
1982 (gdb-append-to-partial-output new)
1985 (defun gdb-append-to-partial-output (string)
1986 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1987 (goto-char (point-max))
1990 (defun gdb-clear-partial-output ()
1991 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1994 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
1995 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
1997 Field names are wrapped in double quotes and equal signs are
1998 replaced with semicolons.
2000 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurences from
2001 partial output. This is used to get rid of useless keys in lists
2002 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2003 -break-info are examples of MI commands which issue such
2006 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2007 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2008 -break-info output when it contains breakpoint script field
2009 incompatible with GDB/MI output syntax."
2011 (goto-char (point-min))
2014 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2015 (replace-match "" nil nil nil 1))))
2019 ;; Find positions of braces which enclose broken list
2020 (while (re-search-forward (concat fix-list "={\"") nil t)
2021 (let ((p1 (goto-char (- (point) 2)))
2022 (p2 (progn (forward-sexp)
2024 ;; Replace braces with brackets
2032 (goto-char (point-min))
2034 ;; TODO: This breaks badly with foo= inside constants
2035 (while (re-search-forward "\\([[:alpha:]-_]+\\)=" nil t)
2036 (replace-match "\"\\1\":" nil nil))
2037 (goto-char (point-max))
2040 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2041 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2043 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2044 (gdb-jsonify-buffer fix-key fix-list)
2046 (goto-char (point-min))
2047 (let ((json-array-type 'list))
2050 (defun gdb-json-string (string &optional fix-key fix-list)
2051 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2053 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2056 (gdb-json-read-buffer fix-key fix-list)))
2058 (defun gdb-json-partial-output (&optional fix-key fix-list)
2059 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2061 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2062 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2063 (gdb-json-read-buffer fix-key fix-list)))
2065 (defmacro gdb-mark-line (line variable)
2066 "Set VARIABLE marker to point at beginning of LINE.
2068 If current window has no fringes, inverse colors on LINE.
2070 Return position where LINE begins."
2072 (let* ((offset (1+ (- ,line (line-number-at-pos))))
2073 (start-posn (line-beginning-position offset))
2074 (end-posn (line-end-position offset)))
2075 (set-marker ,variable (copy-marker start-posn))
2076 (when (not (> (car (window-fringes)) 0))
2077 (put-text-property start-posn end-posn
2078 'font-lock-face '(:inverse-video t)))
2081 (defun gdb-pad-string (string padding)
2082 (format (concat "%" (number-to-string padding) "s") string))
2084 ;; gdb-table struct is a way to programmatically construct simple
2085 ;; tables. It help to reliably align columns of data in GDB buffers
2091 (row-properties nil)
2094 (defun gdb-table-add-row (table row &optional properties)
2095 "Add ROW of string to TABLE and recalculate column sizes.
2097 When non-nil, PROPERTIES will be added to the whole row when
2098 calling `gdb-table-string'."
2099 (let ((rows (gdb-table-rows table))
2100 (row-properties (gdb-table-row-properties table))
2101 (column-sizes (gdb-table-column-sizes table))
2102 (right-align (gdb-table-right-align table)))
2103 (when (not column-sizes)
2104 (setf (gdb-table-column-sizes table)
2105 (make-list (length row) 0)))
2106 (setf (gdb-table-rows table)
2107 (append rows (list row)))
2108 (setf (gdb-table-row-properties table)
2109 (append row-properties (list properties)))
2110 (setf (gdb-table-column-sizes table)
2111 (mapcar* (lambda (x s)
2113 (max (abs x) (string-width (or s "")))))
2114 (if right-align new-x (- new-x))))
2115 (gdb-table-column-sizes table)
2117 ;; Avoid trailing whitespace at eol
2118 (if (not (gdb-table-right-align table))
2119 (setcar (last (gdb-table-column-sizes table)) 0))))
2121 (defun gdb-table-string (table &optional sep)
2122 "Return TABLE as a string with columns separated with SEP."
2123 (let ((column-sizes (gdb-table-column-sizes table))
2128 (lambda (row properties)
2130 (mapconcat 'identity
2131 (mapcar* (lambda (s x) (gdb-pad-string s x))
2135 (gdb-table-rows table)
2136 (gdb-table-row-properties table))
2139 ;; gdb-get-field goes deep, gdb-get-many-fields goes wide
2140 (defalias 'gdb-get-field 'bindat-get-field)
2142 (defun gdb-get-many-fields (struct &rest fields)
2143 "Return a list of FIELDS values from STRUCT."
2145 (dolist (field fields values)
2146 (setq values (append values (list (gdb-get-field struct field)))))))
2148 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2150 &optional signal-list)
2151 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2152 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2153 buffer with `gdb-bind-function-to-buffer'.
2155 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2156 defined trigger is called with an argument from SIGNAL-LIST. It's
2157 not recommended to define triggers with empty SIGNAL-LIST.
2158 Normally triggers should respond at least to 'update signal.
2160 Normally the trigger defined by this command must be called from
2161 the buffer where HANDLER-NAME must work. This should be done so
2162 that buffer-local thread number may be used in GDB-COMMAND (by
2163 calling `gdb-current-context-command').
2164 `gdb-bind-function-to-buffer' is used to achieve this, see
2165 `gdb-get-buffer-create'.
2167 Triggers defined by this command are meant to be used as a
2168 trigger argument when describing buffer types with
2169 `gdb-set-buffer-rules'."
2170 `(defun ,trigger-name (&optional signal)
2172 (or (not ,signal-list)
2173 (memq signal ,signal-list))
2174 (when (not (gdb-pending-p
2175 (cons (current-buffer) ',trigger-name)))
2178 (gdb-bind-function-to-buffer ',handler-name (current-buffer))))
2179 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2181 ;; Used by disassembly buffer only, the rest use
2182 ;; def-gdb-trigger-and-handler
2183 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2184 &optional nopreserve)
2185 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2187 Handlers are normally called from the buffers they put output in.
2189 Delete ((current-buffer) . TRIGGER-NAME) from
2190 `gdb-pending-triggers', erase current buffer and evaluate
2191 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2193 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2194 `(defun ,handler-name ()
2195 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2196 (let* ((buffer-read-only nil)
2197 (window (get-buffer-window (current-buffer) 0))
2198 (start (window-start window))
2199 (p (window-point window)))
2202 (gdb-update-buffer-name)
2203 ,(when (not nopreserve)
2204 '(set-window-start window start)
2205 '(set-window-point window p)))))
2207 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2208 handler-name custom-defun
2209 &optional signal-list)
2210 "Define trigger and handler.
2212 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2213 `def-gdb-auto-update-trigger'.
2215 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2216 `def-gdb-auto-update-handler'."
2218 (def-gdb-auto-update-trigger ,trigger-name
2220 ,handler-name ,signal-list)
2221 (def-gdb-auto-update-handler ,handler-name
2222 ,trigger-name ,custom-defun)))
2226 ;; Breakpoint buffer : This displays the output of `-break-list'.
2227 (def-gdb-trigger-and-handler
2228 gdb-invalidate-breakpoints "-break-list"
2229 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2232 (gdb-set-buffer-rules
2233 'gdb-breakpoints-buffer
2234 'gdb-breakpoints-buffer-name
2235 'gdb-breakpoints-mode
2236 'gdb-invalidate-breakpoints)
2238 (defun gdb-breakpoints-list-handler-custom ()
2239 (let ((breakpoints-list (gdb-get-field
2240 (gdb-json-partial-output "bkpt" "script")
2241 'BreakpointTable 'body))
2242 (table (make-gdb-table)))
2243 (setq gdb-breakpoints-list nil)
2244 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Hits" "Addr" "What"))
2245 (dolist (breakpoint breakpoints-list)
2246 (add-to-list 'gdb-breakpoints-list
2247 (cons (gdb-get-field breakpoint 'number)
2249 (let ((at (gdb-get-field breakpoint 'at))
2250 (pending (gdb-get-field breakpoint 'pending))
2251 (func (gdb-get-field breakpoint 'func)))
2252 (gdb-table-add-row table
2254 (gdb-get-field breakpoint 'number)
2255 (gdb-get-field breakpoint 'type)
2256 (gdb-get-field breakpoint 'disp)
2257 (let ((flag (gdb-get-field breakpoint 'enabled)))
2258 (if (string-equal flag "y")
2259 (propertize "y" 'font-lock-face font-lock-warning-face)
2260 (propertize "n" 'font-lock-face font-lock-comment-face)))
2261 (gdb-get-field breakpoint 'times)
2262 (gdb-get-field breakpoint 'addr)
2265 (propertize func 'font-lock-face font-lock-function-name-face)
2266 (gdb-frame-location breakpoint))))
2267 ;; Add clickable properties only for breakpoints with file:line
2269 (append (list 'gdb-breakpoint breakpoint)
2270 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2271 mouse-face highlight))))))
2272 (insert (gdb-table-string table " "))
2273 (gdb-place-breakpoints)))
2275 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2276 (defun gdb-place-breakpoints ()
2277 (let ((flag) (bptno))
2278 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2279 (dolist (buffer (buffer-list))
2280 (with-current-buffer buffer
2281 (if (and (eq gud-minor-mode 'gdbmi)
2282 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2283 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2284 (dolist (breakpoint gdb-breakpoints-list)
2285 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2286 ; an associative list
2287 (line (gdb-get-field breakpoint 'line)))
2289 (let ((file (gdb-get-field breakpoint 'fullname))
2290 (flag (gdb-get-field breakpoint 'enabled))
2291 (bptno (gdb-get-field breakpoint 'number)))
2292 (unless (file-exists-p file)
2293 (setq file (cdr (assoc bptno gdb-location-alist))))
2295 (not (string-equal file "File not found")))
2296 (with-current-buffer
2297 (find-file-noselect file 'nowarn)
2299 ;; Only want one breakpoint icon at each location.
2301 (goto-line (string-to-number line))
2302 (gdb-put-breakpoint-icon (string-equal flag "y") bptno)))
2304 (list (concat "list " file ":1")
2307 (list "-file-list-exec-source-file"
2308 `(lambda () (gdb-get-location
2309 ,bptno ,line ,flag)))))))))))
2311 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2313 (defun gdb-get-location (bptno line flag)
2314 "Find the directory containing the relevant source file.
2315 Put in buffer and place breakpoint icon."
2316 (goto-char (point-min))
2317 (catch 'file-not-found
2318 (if (re-search-forward gdb-source-file-regexp nil t)
2319 (delete (cons bptno "File not found") gdb-location-alist)
2320 (push (cons bptno (match-string 1)) gdb-location-alist)
2322 (unless (assoc bptno gdb-location-alist)
2323 (push (cons bptno "File not found") gdb-location-alist)
2324 (message-box "Cannot find source file for breakpoint location.
2325 Add directory to search path for source files using the GDB command, dir."))
2326 (throw 'file-not-found nil))
2327 (with-current-buffer (find-file-noselect (match-string 1))
2329 ;; only want one breakpoint icon at each location
2331 (goto-line (string-to-number line))
2332 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2334 (add-hook 'find-file-hook 'gdb-find-file-hook)
2336 (defun gdb-find-file-hook ()
2337 "Set up buffer for debugging if file is part of the source code
2338 of the current session."
2339 (if (and (buffer-name gud-comint-buffer)
2340 ;; in case gud or gdb-ui is just loaded
2342 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2344 (if (member buffer-file-name gdb-source-file-list)
2345 (with-current-buffer (find-buffer-visiting buffer-file-name)
2346 (gdb-init-buffer)))))
2348 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2349 (declare-function gud-break "gdb-mi" t t) ; gud-def
2350 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2352 (defun gdb-mouse-set-clear-breakpoint (event)
2353 "Set/clear breakpoint in left fringe/margin at mouse click.
2354 If not in a source or disassembly buffer just set point."
2356 (mouse-minibuffer-check event)
2357 (let ((posn (event-end event)))
2358 (with-selected-window (posn-window posn)
2359 (if (or (buffer-file-name) (eq major-mode 'gdb-disassembly-mode))
2360 (if (numberp (posn-point posn))
2362 (goto-char (posn-point posn))
2363 (if (or (posn-object posn)
2364 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2368 (posn-set-point posn))))
2370 (defun gdb-mouse-toggle-breakpoint-margin (event)
2371 "Enable/disable breakpoint in left margin with mouse click."
2373 (mouse-minibuffer-check event)
2374 (let ((posn (event-end event)))
2375 (if (numberp (posn-point posn))
2376 (with-selected-window (posn-window posn)
2378 (goto-char (posn-point posn))
2379 (if (posn-object posn)
2381 (let ((bptno (get-text-property
2382 0 'gdb-bptno (car (posn-string posn)))))
2384 (if (get-text-property
2385 0 'gdb-enabled (car (posn-string posn)))
2390 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2391 "Enable/disable breakpoint in left fringe with mouse click."
2393 (mouse-minibuffer-check event)
2394 (let* ((posn (event-end event))
2395 (pos (posn-point posn))
2398 (with-selected-window (posn-window posn)
2400 (set-buffer (window-buffer (selected-window)))
2402 (dolist (overlay (overlays-in pos pos))
2403 (when (overlay-get overlay 'put-break)
2404 (setq obj (overlay-get overlay 'before-string))))
2408 (if (get-text-property 0 'gdb-enabled obj)
2411 (get-text-property 0 'gdb-bptno obj)))))))))
2413 (defun gdb-breakpoints-buffer-name ()
2414 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2416 (def-gdb-display-buffer
2417 gdb-display-breakpoints-buffer
2418 'gdb-breakpoints-buffer
2419 "Display status of user-settable breakpoints.")
2421 (def-gdb-frame-for-buffer
2422 gdb-frame-breakpoints-buffer
2423 'gdb-breakpoints-buffer
2424 "Display status of user-settable breakpoints in a new frame.")
2426 (defvar gdb-breakpoints-mode-map
2427 (let ((map (make-sparse-keymap))
2428 (menu (make-sparse-keymap "Breakpoints")))
2429 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2430 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2431 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2432 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2433 (suppress-keymap map)
2434 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2435 (define-key map " " 'gdb-toggle-breakpoint)
2436 (define-key map "D" 'gdb-delete-breakpoint)
2437 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2438 (define-key map "q" 'gdb-delete-frame-or-window)
2439 (define-key map "\r" 'gdb-goto-breakpoint)
2440 (define-key map "\t" '(lambda ()
2442 (gdb-set-window-buffer
2443 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2444 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2445 (define-key map [follow-link] 'mouse-face)
2448 (defun gdb-delete-frame-or-window ()
2449 "Delete frame if there is only one window. Otherwise delete the window."
2451 (if (one-window-p) (delete-frame)
2454 ;;from make-mode-line-mouse-map
2455 (defun gdb-make-header-line-mouse-map (mouse function) "\
2456 Return a keymap with single entry for mouse key MOUSE on the header line.
2457 MOUSE is defined to run function FUNCTION with no args in the buffer
2458 corresponding to the mode line clicked."
2459 (let ((map (make-sparse-keymap)))
2460 (define-key map (vector 'header-line mouse) function)
2461 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2464 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2466 'help-echo ,help-echo
2467 'mouse-face ',mouse-face
2470 (gdb-make-header-line-mouse-map
2472 (lambda (event) (interactive "e")
2473 (save-selected-window
2474 (select-window (posn-window (event-start event)))
2475 (gdb-set-window-buffer
2476 (gdb-get-buffer-create ',buffer) t) )))))
2479 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2482 (defun gdb-threads-buffer-name ()
2483 (concat "*threads of " (gdb-get-target-string) "*"))
2485 (def-gdb-display-buffer
2486 gdb-display-threads-buffer
2488 "Display GDB threads.")
2490 (def-gdb-frame-for-buffer
2491 gdb-frame-threads-buffer
2493 "Display GDB threads in a new frame.")
2495 (def-gdb-trigger-and-handler
2496 gdb-invalidate-threads (gdb-current-context-command "-thread-info" gud-running)
2497 gdb-thread-list-handler gdb-thread-list-handler-custom
2498 '(update update-threads))
2500 (gdb-set-buffer-rules
2502 'gdb-threads-buffer-name
2504 'gdb-invalidate-threads)
2506 (defvar gdb-threads-font-lock-keywords
2507 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2508 (" \\(stopped\\)" (1 font-lock-warning-face))
2509 (" \\(running\\)" (1 font-lock-string-face))
2510 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2511 "Font lock keywords used in `gdb-threads-mode'.")
2513 (defvar gdb-threads-mode-map
2514 (let ((map (make-sparse-keymap)))
2515 (define-key map "\r" 'gdb-select-thread)
2516 (define-key map "f" 'gdb-display-stack-for-thread)
2517 (define-key map "F" 'gdb-frame-stack-for-thread)
2518 (define-key map "l" 'gdb-display-locals-for-thread)
2519 (define-key map "L" 'gdb-frame-locals-for-thread)
2520 (define-key map "r" 'gdb-display-registers-for-thread)
2521 (define-key map "R" 'gdb-frame-registers-for-thread)
2522 (define-key map "d" 'gdb-display-disassembly-for-thread)
2523 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2524 (define-key map "i" 'gdb-interrupt-thread)
2525 (define-key map "c" 'gdb-continue-thread)
2526 (define-key map "s" 'gdb-step-thread)
2527 (define-key map "\t" '(lambda ()
2529 (gdb-set-window-buffer
2530 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2531 (define-key map [mouse-2] 'gdb-select-thread)
2532 (define-key map [follow-link] 'mouse-face)
2535 (defvar gdb-threads-header
2537 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
2538 "mouse-1: select" mode-line-highlight mode-line-inactive)
2540 (gdb-propertize-header "Threads" gdb-threads-buffer
2541 nil nil mode-line)))
2543 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2544 "Major mode for GDB threads.
2546 \\{gdb-threads-mode-map}"
2547 (setq gdb-thread-position (make-marker))
2548 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2549 (setq header-line-format gdb-threads-header)
2550 (set (make-local-variable 'font-lock-defaults)
2551 '(gdb-threads-font-lock-keywords))
2552 (run-mode-hooks 'gdb-threads-mode-hook)
2553 'gdb-invalidate-threads)
2555 (defun gdb-thread-list-handler-custom ()
2556 (let ((threads-list (gdb-get-field (gdb-json-partial-output) 'threads))
2557 (table (make-gdb-table))
2559 (setq gdb-threads-list nil)
2560 (setq gdb-running-threads-count 0)
2561 (setq gdb-stopped-threads-count 0)
2562 (set-marker gdb-thread-position nil)
2564 (dolist (thread (reverse threads-list))
2565 (let ((running (string-equal (gdb-get-field thread 'state) "running")))
2566 (add-to-list 'gdb-threads-list
2567 (cons (gdb-get-field thread 'id)
2570 (incf gdb-running-threads-count)
2571 (incf gdb-stopped-threads-count))
2573 (gdb-table-add-row table
2575 (gdb-get-field thread 'id)
2577 (if gdb-thread-buffer-verbose-names
2578 (concat (gdb-get-field thread 'target-id) " ") "")
2579 (gdb-get-field thread 'state)
2580 ;; Include frame information for stopped threads
2583 " in " (gdb-get-field thread 'frame 'func)
2584 (if gdb-thread-buffer-arguments
2587 (let ((args (gdb-get-field thread 'frame 'args)))
2590 (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value))))
2594 (if gdb-thread-buffer-locations
2595 (gdb-frame-location (gdb-get-field thread 'frame)) "")
2596 (if gdb-thread-buffer-addresses
2597 (concat " at " (gdb-get-field thread 'frame 'addr)) ""))
2601 'mouse-face 'highlight
2602 'help-echo "mouse-2, RET: select thread")))
2603 (when (string-equal gdb-thread-number
2604 (gdb-get-field thread 'id))
2605 (setq marked-line (length gdb-threads-list))))
2606 (insert (gdb-table-string table " "))
2608 (gdb-mark-line marked-line gdb-thread-position)))
2609 ;; We update gud-running here because we need to make sure that
2610 ;; gdb-threads-list is up-to-date
2611 (gdb-update-gud-running)
2612 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2614 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2615 "Define a NAME command which will act upon thread on the current line.
2617 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2618 be the value of 'gdb-thread property of the current line. If
2619 'gdb-thread is nil, error is signaled."
2620 `(defun ,name (&optional event)
2623 (if event (posn-set-point (event-end event)))
2626 (let ((thread (get-text-property (point) 'gdb-thread)))
2629 (error "Not recognized as thread line"))))))
2631 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command &optional doc)
2632 "Define a NAME which will call BUFFER-COMMAND with id of thread
2633 on the current line."
2634 `(def-gdb-thread-buffer-command ,name
2635 (,buffer-command (gdb-get-field thread 'id))
2638 (def-gdb-thread-buffer-command gdb-select-thread
2639 (let ((new-id (gdb-get-field thread 'id)))
2640 (gdb-setq-thread-number new-id)
2641 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2643 "Select the thread at current line of threads buffer.")
2645 (def-gdb-thread-buffer-simple-command
2646 gdb-display-stack-for-thread
2647 gdb-preemptively-display-stack-buffer
2648 "Display stack buffer for the thread at current line.")
2650 (def-gdb-thread-buffer-simple-command
2651 gdb-display-locals-for-thread
2652 gdb-preemptively-display-locals-buffer
2653 "Display locals buffer for the thread at current line.")
2655 (def-gdb-thread-buffer-simple-command
2656 gdb-display-registers-for-thread
2657 gdb-preemptively-display-registers-buffer
2658 "Display registers buffer for the thread at current line.")
2660 (def-gdb-thread-buffer-simple-command
2661 gdb-display-disassembly-for-thread
2662 gdb-preemptively-display-disassembly-buffer
2663 "Display disassembly buffer for the thread at current line.")
2665 (def-gdb-thread-buffer-simple-command
2666 gdb-frame-stack-for-thread
2667 gdb-frame-stack-buffer
2668 "Display a new frame with stack buffer for the thread at
2671 (def-gdb-thread-buffer-simple-command
2672 gdb-frame-locals-for-thread
2673 gdb-frame-locals-buffer
2674 "Display a new frame with locals buffer for the thread at
2677 (def-gdb-thread-buffer-simple-command
2678 gdb-frame-registers-for-thread
2679 gdb-frame-registers-buffer
2680 "Display a new frame with registers buffer for the thread at
2683 (def-gdb-thread-buffer-simple-command
2684 gdb-frame-disassembly-for-thread
2685 gdb-frame-disassembly-buffer
2686 "Display a new frame with disassembly buffer for the thread at
2689 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2690 "Define a NAME which will execute GUD-COMMAND with
2691 `gdb-thread-number' locally bound to id of thread on the current
2693 `(def-gdb-thread-buffer-command ,name
2695 (let ((gdb-thread-number (gdb-get-field thread 'id))
2696 (gdb-gud-control-all-threads nil))
2697 (call-interactively #',gud-command))
2698 (error "Available in non-stop mode only, customize gdb-non-stop-setting."))
2701 (def-gdb-thread-buffer-gud-command
2702 gdb-interrupt-thread
2704 "Interrupt thread at current line.")
2706 (def-gdb-thread-buffer-gud-command
2709 "Continue thread at current line.")
2711 (def-gdb-thread-buffer-gud-command
2714 "Step thread at current line.")
2719 (defcustom gdb-memory-rows 8
2720 "Number of data rows in memory window."
2725 (defcustom gdb-memory-columns 4
2726 "Number of data columns in memory window."
2731 (defcustom gdb-memory-format "x"
2732 "Display format of data items in memory window."
2733 :type '(choice (const :tag "Hexadecimal" "x")
2734 (const :tag "Signed decimal" "d")
2735 (const :tag "Unsigned decimal" "u")
2736 (const :tag "Octal" "o")
2737 (const :tag "Binary" "t"))
2741 (defcustom gdb-memory-unit 4
2742 "Unit size of data items in memory window."
2743 :type '(choice (const :tag "Byte" 1)
2744 (const :tag "Halfword" 2)
2745 (const :tag "Word" 4)
2746 (const :tag "Giant word" 8))
2750 (def-gdb-trigger-and-handler
2751 gdb-invalidate-memory
2752 (format "-data-read-memory %s %s %d %d %d"
2758 gdb-read-memory-handler
2759 gdb-read-memory-custom
2762 (gdb-set-buffer-rules
2764 'gdb-memory-buffer-name
2766 'gdb-invalidate-memory)
2768 (defun gdb-memory-column-width (size format)
2769 "Return length of string with memory unit of SIZE in FORMAT.
2771 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2772 in `gdb-memory-format'."
2773 (let ((format-base (cdr (assoc format
2775 ("d" . 10) ("u" . 10)
2779 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2780 (cond ((string-equal format "x")
2781 (+ 2 res)) ; hexadecimal numbers have 0x in front
2782 ((or (string-equal format "d")
2783 (string-equal format "o"))
2786 (error "Unknown format"))))
2788 (defun gdb-read-memory-custom ()
2789 (let* ((res (gdb-json-partial-output))
2790 (err-msg (gdb-get-field res 'msg)))
2792 (let ((memory (gdb-get-field res 'memory)))
2793 (setq gdb-memory-address (gdb-get-field res 'addr))
2794 (setq gdb-memory-next-page (gdb-get-field res 'next-page))
2795 (setq gdb-memory-prev-page (gdb-get-field res 'prev-page))
2796 (setq gdb-memory-last-address gdb-memory-address)
2797 (dolist (row memory)
2798 (insert (concat (gdb-get-field row 'addr) ":"))
2799 (dolist (column (gdb-get-field row 'data))
2800 (insert (gdb-pad-string column
2801 (+ 2 (gdb-memory-column-width
2803 gdb-memory-format)))))
2805 ;; Show last page instead of empty buffer when out of bounds
2807 (let ((gdb-memory-address gdb-memory-last-address))
2808 (gdb-invalidate-memory 'update)
2809 (error err-msg))))))
2811 (defvar gdb-memory-mode-map
2812 (let ((map (make-sparse-keymap)))
2813 (suppress-keymap map t)
2814 (define-key map "q" 'kill-this-buffer)
2815 (define-key map "n" 'gdb-memory-show-next-page)
2816 (define-key map "p" 'gdb-memory-show-previous-page)
2817 (define-key map "a" 'gdb-memory-set-address)
2818 (define-key map "t" 'gdb-memory-format-binary)
2819 (define-key map "o" 'gdb-memory-format-octal)
2820 (define-key map "u" 'gdb-memory-format-unsigned)
2821 (define-key map "d" 'gdb-memory-format-signed)
2822 (define-key map "x" 'gdb-memory-format-hexadecimal)
2823 (define-key map "b" 'gdb-memory-unit-byte)
2824 (define-key map "h" 'gdb-memory-unit-halfword)
2825 (define-key map "w" 'gdb-memory-unit-word)
2826 (define-key map "g" 'gdb-memory-unit-giant)
2827 (define-key map "R" 'gdb-memory-set-rows)
2828 (define-key map "C" 'gdb-memory-set-columns)
2831 (defun gdb-memory-set-address-event (event)
2832 "Handle a click on address field in memory buffer header."
2834 (save-selected-window
2835 (select-window (posn-window (event-start event)))
2836 (gdb-memory-set-address)))
2838 ;; Non-event version for use within keymap
2839 (defun gdb-memory-set-address ()
2840 "Set the start memory address."
2842 (let ((arg (read-from-minibuffer "Memory address: ")))
2843 (setq gdb-memory-address arg))
2844 (gdb-invalidate-memory 'update))
2846 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2847 "Define a function NAME which reads new VAR value from minibuffer."
2848 `(defun ,name (event)
2851 (save-selected-window
2852 (select-window (posn-window (event-start event)))
2853 (let* ((arg (read-from-minibuffer ,echo-string))
2854 (count (string-to-number arg)))
2856 (error "Positive number only")
2857 (customize-set-variable ',variable count)
2858 (gdb-invalidate-memory 'update))))))
2860 (def-gdb-set-positive-number
2864 "Set the number of data rows in memory window.")
2866 (def-gdb-set-positive-number
2867 gdb-memory-set-columns
2870 "Set the number of data columns in memory window.")
2872 (defmacro def-gdb-memory-format (name format doc)
2873 "Define a function NAME to switch memory buffer to use FORMAT.
2875 DOC is an optional documentation string."
2876 `(defun ,name () ,(when doc doc)
2878 (customize-set-variable 'gdb-memory-format ,format)
2879 (gdb-invalidate-memory 'update)))
2881 (def-gdb-memory-format
2882 gdb-memory-format-binary "t"
2883 "Set the display format to binary.")
2885 (def-gdb-memory-format
2886 gdb-memory-format-octal "o"
2887 "Set the display format to octal.")
2889 (def-gdb-memory-format
2890 gdb-memory-format-unsigned "u"
2891 "Set the display format to unsigned decimal.")
2893 (def-gdb-memory-format
2894 gdb-memory-format-signed "d"
2895 "Set the display format to decimal.")
2897 (def-gdb-memory-format
2898 gdb-memory-format-hexadecimal "x"
2899 "Set the display format to hexadecimal.")
2901 (defvar gdb-memory-format-map
2902 (let ((map (make-sparse-keymap)))
2903 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2905 "Keymap to select format in the header line.")
2907 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2908 "Menu of display formats in the header line.")
2910 (define-key gdb-memory-format-menu [binary]
2911 '(menu-item "Binary" gdb-memory-format-binary
2912 :button (:radio . (equal gdb-memory-format "t"))))
2913 (define-key gdb-memory-format-menu [octal]
2914 '(menu-item "Octal" gdb-memory-format-octal
2915 :button (:radio . (equal gdb-memory-format "o"))))
2916 (define-key gdb-memory-format-menu [unsigned]
2917 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2918 :button (:radio . (equal gdb-memory-format "u"))))
2919 (define-key gdb-memory-format-menu [signed]
2920 '(menu-item "Signed Decimal" gdb-memory-format-signed
2921 :button (:radio . (equal gdb-memory-format "d"))))
2922 (define-key gdb-memory-format-menu [hexadecimal]
2923 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2924 :button (:radio . (equal gdb-memory-format "x"))))
2926 (defun gdb-memory-format-menu (event)
2928 (x-popup-menu event gdb-memory-format-menu))
2930 (defun gdb-memory-format-menu-1 (event)
2932 (save-selected-window
2933 (select-window (posn-window (event-start event)))
2934 (let* ((selection (gdb-memory-format-menu event))
2935 (binding (and selection (lookup-key gdb-memory-format-menu
2936 (vector (car selection))))))
2937 (if binding (call-interactively binding)))))
2939 (defmacro def-gdb-memory-unit (name unit-size doc)
2940 "Define a function NAME to switch memory unit size to UNIT-SIZE.
2942 DOC is an optional documentation string."
2943 `(defun ,name () ,(when doc doc)
2945 (customize-set-variable 'gdb-memory-unit ,unit-size)
2946 (gdb-invalidate-memory 'update)))
2948 (def-gdb-memory-unit gdb-memory-unit-giant 8
2949 "Set the unit size to giant words (eight bytes).")
2951 (def-gdb-memory-unit gdb-memory-unit-word 4
2952 "Set the unit size to words (four bytes).")
2954 (def-gdb-memory-unit gdb-memory-unit-halfword 2
2955 "Set the unit size to halfwords (two bytes).")
2957 (def-gdb-memory-unit gdb-memory-unit-byte 1
2958 "Set the unit size to bytes.")
2960 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
2961 "Define a function NAME which show new address in memory buffer.
2963 The defined function switches Memory buffer to show address
2964 stored in ADDRESS-VAR variable.
2966 DOC is an optional documentation string."
2970 (let ((gdb-memory-address ,address-var))
2971 (gdb-invalidate-memory))))
2973 (def-gdb-memory-show-page gdb-memory-show-previous-page
2974 gdb-memory-prev-page)
2976 (def-gdb-memory-show-page gdb-memory-show-next-page
2977 gdb-memory-next-page)
2979 (defvar gdb-memory-unit-map
2980 (let ((map (make-sparse-keymap)))
2981 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2983 "Keymap to select units in the header line.")
2985 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2986 "Menu of units in the header line.")
2988 (define-key gdb-memory-unit-menu [giantwords]
2989 '(menu-item "Giant words" gdb-memory-unit-giant
2990 :button (:radio . (equal gdb-memory-unit 8))))
2991 (define-key gdb-memory-unit-menu [words]
2992 '(menu-item "Words" gdb-memory-unit-word
2993 :button (:radio . (equal gdb-memory-unit 4))))
2994 (define-key gdb-memory-unit-menu [halfwords]
2995 '(menu-item "Halfwords" gdb-memory-unit-halfword
2996 :button (:radio . (equal gdb-memory-unit 2))))
2997 (define-key gdb-memory-unit-menu [bytes]
2998 '(menu-item "Bytes" gdb-memory-unit-byte
2999 :button (:radio . (equal gdb-memory-unit 1))))
3001 (defun gdb-memory-unit-menu (event)
3003 (x-popup-menu event gdb-memory-unit-menu))
3005 (defun gdb-memory-unit-menu-1 (event)
3007 (save-selected-window
3008 (select-window (posn-window (event-start event)))
3009 (let* ((selection (gdb-memory-unit-menu event))
3010 (binding (and selection (lookup-key gdb-memory-unit-menu
3011 (vector (car selection))))))
3012 (if binding (call-interactively binding)))))
3014 (defvar gdb-memory-font-lock-keywords
3015 '(;; <__function.name+n>
3016 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
3018 "Font lock keywords used in `gdb-memory-mode'.")
3020 (defvar gdb-memory-header
3025 'face font-lock-warning-face
3026 'help-echo "mouse-1: decrement address"
3027 'mouse-face 'mode-line-highlight
3028 'local-map (gdb-make-header-line-mouse-map
3030 #'gdb-memory-show-previous-page))
3033 'face font-lock-warning-face
3034 'help-echo "mouse-1: increment address"
3035 'mouse-face 'mode-line-highlight
3036 'local-map (gdb-make-header-line-mouse-map
3038 #'gdb-memory-show-next-page))
3040 (propertize gdb-memory-address
3041 'face font-lock-warning-face
3042 'help-echo "mouse-1: set start address"
3043 'mouse-face 'mode-line-highlight
3044 'local-map (gdb-make-header-line-mouse-map
3046 #'gdb-memory-set-address-event))
3048 (propertize (number-to-string gdb-memory-rows)
3049 'face font-lock-warning-face
3050 'help-echo "mouse-1: set number of columns"
3051 'mouse-face 'mode-line-highlight
3052 'local-map (gdb-make-header-line-mouse-map
3054 #'gdb-memory-set-rows))
3056 (propertize (number-to-string gdb-memory-columns)
3057 'face font-lock-warning-face
3058 'help-echo "mouse-1: set number of columns"
3059 'mouse-face 'mode-line-highlight
3060 'local-map (gdb-make-header-line-mouse-map
3062 #'gdb-memory-set-columns))
3064 (propertize gdb-memory-format
3065 'face font-lock-warning-face
3066 'help-echo "mouse-3: select display format"
3067 'mouse-face 'mode-line-highlight
3068 'local-map gdb-memory-format-map)
3070 (propertize (number-to-string gdb-memory-unit)
3071 'face font-lock-warning-face
3072 'help-echo "mouse-3: select unit size"
3073 'mouse-face 'mode-line-highlight
3074 'local-map gdb-memory-unit-map)))
3075 "Header line used in `gdb-memory-mode'.")
3077 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3078 "Major mode for examining memory.
3080 \\{gdb-memory-mode-map}"
3081 (setq header-line-format gdb-memory-header)
3082 (set (make-local-variable 'font-lock-defaults)
3083 '(gdb-memory-font-lock-keywords))
3084 (run-mode-hooks 'gdb-memory-mode-hook)
3085 'gdb-invalidate-memory)
3087 (defun gdb-memory-buffer-name ()
3088 (concat "*memory of " (gdb-get-target-string) "*"))
3090 (def-gdb-display-buffer
3091 gdb-display-memory-buffer
3093 "Display memory contents.")
3095 (defun gdb-frame-memory-buffer ()
3096 "Display memory contents in a new frame."
3098 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3099 (special-display-frame-alist
3103 ,@gdb-frame-parameters)))
3104 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3107 ;;; Disassembly view
3109 (defun gdb-disassembly-buffer-name ()
3110 (gdb-current-context-buffer-name
3111 (concat "disassembly of " (gdb-get-target-string))))
3113 (def-gdb-display-buffer
3114 gdb-display-disassembly-buffer
3115 'gdb-disassembly-buffer
3116 "Display disassembly for current stack frame.")
3118 (def-gdb-preempt-display-buffer
3119 gdb-preemptively-display-disassembly-buffer
3120 'gdb-disassembly-buffer)
3122 (def-gdb-frame-for-buffer
3123 gdb-frame-disassembly-buffer
3124 'gdb-disassembly-buffer
3125 "Display disassembly in a new frame.")
3127 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3128 (let* ((frame (gdb-current-buffer-frame))
3129 (file (gdb-get-field frame 'fullname))
3130 (line (gdb-get-field frame 'line)))
3132 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3133 gdb-disassembly-handler
3134 ;; We update disassembly only after we have actual frame information
3135 ;; about all threads
3136 '(update update-disassembly))
3138 (def-gdb-auto-update-handler
3139 gdb-disassembly-handler
3140 gdb-invalidate-disassembly
3141 gdb-disassembly-handler-custom
3144 (gdb-set-buffer-rules
3145 'gdb-disassembly-buffer
3146 'gdb-disassembly-buffer-name
3147 'gdb-disassembly-mode
3148 'gdb-invalidate-disassembly)
3150 (defvar gdb-disassembly-font-lock-keywords
3151 '(;; <__function.name+n>
3152 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3153 (1 font-lock-function-name-face))
3154 ;; 0xNNNNNNNN <__function.name+n>: opcode
3155 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3156 (4 font-lock-keyword-face))
3157 ;; %register(at least i386)
3158 ("%\\sw+" . font-lock-variable-name-face)
3159 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3160 (1 font-lock-comment-face)
3161 (2 font-lock-function-name-face))
3162 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3163 "Font lock keywords used in `gdb-disassembly-mode'.")
3165 (defvar gdb-disassembly-mode-map
3167 (let ((map (make-sparse-keymap)))
3168 (suppress-keymap map)
3169 (define-key map "q" 'kill-this-buffer)
3172 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3173 "Major mode for GDB disassembly information.
3175 \\{gdb-disassembly-mode-map}"
3176 ;; TODO Rename overlay variable for disassembly mode
3177 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3178 (setq fringes-outside-margins t)
3179 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3180 (set (make-local-variable 'font-lock-defaults)
3181 '(gdb-disassembly-font-lock-keywords))
3182 (run-mode-hooks 'gdb-disassembly-mode-hook)
3183 'gdb-invalidate-disassembly)
3185 (defun gdb-disassembly-handler-custom ()
3186 (let* ((instructions (gdb-get-field (gdb-json-partial-output) 'asm_insns))
3187 (address (gdb-get-field (gdb-current-buffer-frame) 'addr))
3189 (table (make-gdb-table))
3191 (dolist (instr instructions)
3192 (gdb-table-add-row table
3194 (gdb-get-field instr 'address)
3195 (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
3196 (gdb-get-field instr 'inst)))
3197 (when (string-equal (gdb-get-field instr 'address)
3200 (setq marked-line (length (gdb-table-rows table)))
3201 (setq fringe-indicator-alist
3202 (if (string-equal gdb-frame-number "0")
3204 '((overlay-arrow . hollow-right-triangle)))))))
3205 (insert (gdb-table-string table " "))
3206 (gdb-disassembly-place-breakpoints)
3207 ;; Mark current position with overlay arrow and scroll window to
3210 (let ((window (get-buffer-window (current-buffer) 0)))
3211 (set-window-point window (gdb-mark-line marked-line gdb-disassembly-position))))
3213 (gdb-current-context-mode-name
3214 (concat "Disassembly: "
3215 (gdb-get-field (gdb-current-buffer-frame) 'func))))))
3217 (defun gdb-disassembly-place-breakpoints ()
3218 (gdb-remove-breakpoint-icons (point-min) (point-max))
3219 (dolist (breakpoint gdb-breakpoints-list)
3220 (let* ((breakpoint (cdr breakpoint))
3221 (bptno (gdb-get-field breakpoint 'number))
3222 (flag (gdb-get-field breakpoint 'enabled))
3223 (address (gdb-get-field breakpoint 'addr)))
3225 (goto-char (point-min))
3226 (if (re-search-forward (concat "^" address) nil t)
3227 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3230 (defvar gdb-breakpoints-header
3232 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3235 (gdb-propertize-header "Threads" gdb-threads-buffer
3236 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3238 ;;; Breakpoints view
3239 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3240 "Major mode for gdb breakpoints.
3242 \\{gdb-breakpoints-mode-map}"
3243 (setq header-line-format gdb-breakpoints-header)
3244 (run-mode-hooks 'gdb-breakpoints-mode-hook)
3245 'gdb-invalidate-breakpoints)
3247 (defun gdb-toggle-breakpoint ()
3248 "Enable/disable breakpoint at current line of breakpoints buffer."
3252 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3255 (concat (if (string-equal "y" (gdb-get-field breakpoint 'enabled))
3258 (gdb-get-field breakpoint 'number)))
3259 (error "Not recognized as break/watchpoint line")))))
3261 (defun gdb-delete-breakpoint ()
3262 "Delete the breakpoint at current line of breakpoints buffer."
3266 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3268 (gud-basic-call (concat "-break-delete " (gdb-get-field breakpoint 'number)))
3269 (error "Not recognized as break/watchpoint line")))))
3271 (defun gdb-goto-breakpoint (&optional event)
3272 "Go to the location of breakpoint at current line of
3273 breakpoints buffer."
3274 (interactive (list last-input-event))
3275 (if event (posn-set-point (event-end event)))
3276 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3277 (let ((window (get-buffer-window gud-comint-buffer)))
3278 (if window (save-selected-window (select-window window))))
3281 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3283 (let ((bptno (gdb-get-field breakpoint 'number))
3284 (file (gdb-get-field breakpoint 'fullname))
3285 (line (gdb-get-field breakpoint 'line)))
3286 (save-selected-window
3287 (let* ((buffer (find-file-noselect
3288 (if (file-exists-p file) file
3289 (cdr (assoc bptno gdb-location-alist)))))
3290 (window (or (gdb-display-source-buffer buffer)
3291 (display-buffer buffer))))
3292 (setq gdb-source-window window)
3293 (with-current-buffer buffer
3294 (goto-line (string-to-number line))
3295 (set-window-point window (point))))))
3296 (error "Not recognized as break/watchpoint line")))))
3299 ;; Frames buffer. This displays a perpetually correct bactrack trace.
3301 (def-gdb-trigger-and-handler
3302 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3303 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3306 (gdb-set-buffer-rules
3308 'gdb-stack-buffer-name
3310 'gdb-invalidate-frames)
3312 (defun gdb-frame-location (frame)
3313 "Return \" of file:line\" or \" of library\" for structure FRAME.
3315 FRAME must have either \"file\" and \"line\" members or \"from\"
3317 (let ((file (gdb-get-field frame 'file))
3318 (line (gdb-get-field frame 'line))
3319 (from (gdb-get-field frame 'from)))
3320 (let ((res (or (and file line (concat file ":" line))
3322 (if res (concat " of " res) ""))))
3324 (defun gdb-stack-list-frames-custom ()
3325 (let ((stack (gdb-get-field (gdb-json-partial-output "frame") 'stack))
3326 (table (make-gdb-table)))
3327 (set-marker gdb-stack-position nil)
3328 (dolist (frame stack)
3329 (gdb-table-add-row table
3331 (gdb-get-field frame 'level)
3334 (gdb-get-field frame 'func)
3335 (if gdb-stack-buffer-locations
3336 (gdb-frame-location frame) "")
3337 (if gdb-stack-buffer-addresses
3338 (concat " at " (gdb-get-field frame 'addr)) "")))
3339 `(mouse-face highlight
3340 help-echo "mouse-2, RET: Select frame"
3342 (insert (gdb-table-string table " ")))
3343 (when (and gdb-frame-number
3344 (gdb-buffer-shows-main-thread-p))
3345 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3346 gdb-stack-position))
3348 (gdb-current-context-mode-name "Frames")))
3350 (defun gdb-stack-buffer-name ()
3351 (gdb-current-context-buffer-name
3352 (concat "stack frames of " (gdb-get-target-string))))
3354 (def-gdb-display-buffer
3355 gdb-display-stack-buffer
3357 "Display backtrace of current stack.")
3359 (def-gdb-preempt-display-buffer
3360 gdb-preemptively-display-stack-buffer
3361 'gdb-stack-buffer nil t)
3363 (def-gdb-frame-for-buffer
3364 gdb-frame-stack-buffer
3366 "Display backtrace of current stack in a new frame.")
3368 (defvar gdb-frames-mode-map
3369 (let ((map (make-sparse-keymap)))
3370 (suppress-keymap map)
3371 (define-key map "q" 'kill-this-buffer)
3372 (define-key map "\r" 'gdb-select-frame)
3373 (define-key map [mouse-2] 'gdb-select-frame)
3374 (define-key map [follow-link] 'mouse-face)
3377 (defvar gdb-frames-font-lock-keywords
3378 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3379 "Font lock keywords used in `gdb-frames-mode'.")
3381 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3382 "Major mode for gdb call stack.
3384 \\{gdb-frames-mode-map}"
3385 (setq gdb-stack-position (make-marker))
3386 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3387 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3388 (set (make-local-variable 'font-lock-defaults)
3389 '(gdb-frames-font-lock-keywords))
3390 (run-mode-hooks 'gdb-frames-mode-hook)
3391 'gdb-invalidate-frames)
3393 (defun gdb-select-frame (&optional event)
3394 "Select the frame and display the relevant source."
3395 (interactive (list last-input-event))
3396 (if event (posn-set-point (event-end event)))
3397 (let ((frame (get-text-property (point) 'gdb-frame)))
3399 (if (gdb-buffer-shows-main-thread-p)
3400 (let ((new-level (gdb-get-field frame 'level)))
3401 (setq gdb-frame-number new-level)
3402 (gdb-input (list (concat "-stack-select-frame " new-level) 'ignore))
3404 (error "Could not select frame for non-current thread."))
3405 (error "Not recognized as frame line"))))
3409 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3410 (def-gdb-trigger-and-handler
3411 gdb-invalidate-locals
3412 (concat (gdb-current-context-command "-stack-list-locals") " --simple-values")
3413 gdb-locals-handler gdb-locals-handler-custom
3416 (gdb-set-buffer-rules
3418 'gdb-locals-buffer-name
3420 'gdb-invalidate-locals)
3422 (defvar gdb-locals-watch-map
3423 (let ((map (make-sparse-keymap)))
3424 (suppress-keymap map)
3425 (define-key map "\r" 'gud-watch)
3426 (define-key map [mouse-2] 'gud-watch)
3428 "Keymap to create watch expression of a complex data type local variable.")
3430 (defvar gdb-edit-locals-map-1
3431 (let ((map (make-sparse-keymap)))
3432 (suppress-keymap map)
3433 (define-key map "\r" 'gdb-edit-locals-value)
3434 (define-key map [mouse-2] 'gdb-edit-locals-value)
3436 "Keymap to edit value of a simple data type local variable.")
3438 (defun gdb-edit-locals-value (&optional event)
3439 "Assign a value to a variable displayed in the locals buffer."
3440 (interactive (list last-input-event))
3442 (if event (posn-set-point (event-end event)))
3444 (let* ((var (gdb-get-field
3445 (get-text-property (point) 'gdb-local-variable) 'name))
3446 (value (read-string (format "New value (%s): " var))))
3448 (concat "-gdb-set variable " var " = " value)))))
3450 ;; Dont display values of arrays or structures.
3451 ;; These can be expanded using gud-watch.
3452 (defun gdb-locals-handler-custom ()
3453 (let ((locals-list (gdb-get-field (gdb-json-partial-output) 'locals))
3454 (table (make-gdb-table)))
3455 (dolist (local locals-list)
3456 (let ((name (gdb-get-field local 'name))
3457 (value (gdb-get-field local 'value))
3458 (type (gdb-get-field local 'type)))
3460 (string-match "\\0x" value))
3461 (add-text-properties 0 (length name)
3462 `(mouse-face highlight
3463 help-echo "mouse-2: create watch expression"
3464 local-map ,gdb-locals-watch-map)
3466 (add-text-properties 0 (length value)
3467 `(mouse-face highlight
3468 help-echo "mouse-2: edit value"
3469 local-map ,gdb-edit-locals-map-1)
3474 (propertize type 'font-lock-face font-lock-type-face)
3475 (propertize name 'font-lock-face font-lock-variable-name-face)
3477 `(gdb-local-variable ,local))))
3478 (insert (gdb-table-string table " "))
3480 (gdb-current-context-mode-name
3481 (concat "Locals: " (gdb-get-field (gdb-current-buffer-frame) 'func))))))
3483 (defvar gdb-locals-header
3485 (gdb-propertize-header "Locals" gdb-locals-buffer
3488 (gdb-propertize-header "Registers" gdb-registers-buffer
3489 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3491 (defvar gdb-locals-mode-map
3492 (let ((map (make-sparse-keymap)))
3493 (suppress-keymap map)
3494 (define-key map "q" 'kill-this-buffer)
3495 (define-key map "\t" '(lambda ()
3497 (gdb-set-window-buffer
3498 (gdb-get-buffer-create
3499 'gdb-registers-buffer
3500 gdb-thread-number) t)))
3503 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3504 "Major mode for gdb locals.
3506 \\{gdb-locals-mode-map}"
3507 (setq header-line-format gdb-locals-header)
3508 (run-mode-hooks 'gdb-locals-mode-hook)
3509 'gdb-invalidate-locals)
3511 (defun gdb-locals-buffer-name ()
3512 (gdb-current-context-buffer-name
3513 (concat "locals of " (gdb-get-target-string))))
3515 (def-gdb-display-buffer
3516 gdb-display-locals-buffer
3518 "Display local variables of current stack and their values.")
3520 (def-gdb-preempt-display-buffer
3521 gdb-preemptively-display-locals-buffer
3522 'gdb-locals-buffer nil t)
3524 (def-gdb-frame-for-buffer
3525 gdb-frame-locals-buffer
3527 "Display local variables of current stack and their values in a new frame.")
3530 ;; Registers buffer.
3532 (def-gdb-trigger-and-handler
3533 gdb-invalidate-registers
3534 (concat (gdb-current-context-command "-data-list-register-values") " x")
3535 gdb-registers-handler
3536 gdb-registers-handler-custom
3539 (gdb-set-buffer-rules
3540 'gdb-registers-buffer
3541 'gdb-registers-buffer-name
3543 'gdb-invalidate-registers)
3545 (defun gdb-registers-handler-custom ()
3546 (let ((register-values (gdb-get-field (gdb-json-partial-output) 'register-values))
3547 (table (make-gdb-table)))
3548 (dolist (register register-values)
3549 (let* ((register-number (gdb-get-field register 'number))
3550 (value (gdb-get-field register 'value))
3551 (register-name (nth (string-to-number register-number)
3552 gdb-register-names)))
3557 (propertize register-name 'font-lock-face font-lock-variable-name-face)
3558 (if (member register-number gdb-changed-registers)
3559 (propertize value 'font-lock-face font-lock-warning-face)
3561 `(mouse-face highlight
3562 help-echo "mouse-2: edit value"
3563 gdb-register-name ,register-name)))))
3564 (insert (gdb-table-string table " "))
3566 (gdb-current-context-mode-name "Registers"))))
3568 (defun gdb-edit-register-value (&optional event)
3569 "Assign a value to a register displayed in the registers buffer."
3570 (interactive (list last-input-event))
3572 (if event (posn-set-point (event-end event)))
3574 (let* ((var (gdb-get-field
3575 (get-text-property (point) 'gdb-register-name)))
3576 (value (read-string (format "New value (%s): " var))))
3578 (concat "-gdb-set variable $" var " = " value)))))
3580 (defvar gdb-registers-mode-map
3581 (let ((map (make-sparse-keymap)))
3582 (suppress-keymap map)
3583 (define-key map "\r" 'gdb-edit-register-value)
3584 (define-key map [mouse-2] 'gdb-edit-register-value)
3585 (define-key map "q" 'kill-this-buffer)
3586 (define-key map "\t" '(lambda ()
3588 (gdb-set-window-buffer
3589 (gdb-get-buffer-create
3591 gdb-thread-number) t)))
3594 (defvar gdb-registers-header
3596 (gdb-propertize-header "Locals" gdb-locals-buffer
3597 "mouse-1: select" mode-line-highlight mode-line-inactive)
3599 (gdb-propertize-header "Registers" gdb-registers-buffer
3600 nil nil mode-line)))
3602 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3603 "Major mode for gdb registers.
3605 \\{gdb-registers-mode-map}"
3606 (setq header-line-format gdb-registers-header)
3607 (run-mode-hooks 'gdb-registers-mode-hook)
3608 'gdb-invalidate-registers)
3610 (defun gdb-registers-buffer-name ()
3611 (gdb-current-context-buffer-name
3612 (concat "registers of " (gdb-get-target-string))))
3614 (def-gdb-display-buffer
3615 gdb-display-registers-buffer
3616 'gdb-registers-buffer
3617 "Display integer register contents.")
3619 (def-gdb-preempt-display-buffer
3620 gdb-preemptively-display-registers-buffer
3621 'gdb-registers-buffer nil t)
3623 (def-gdb-frame-for-buffer
3624 gdb-frame-registers-buffer
3625 'gdb-registers-buffer
3626 "Display integer register contents in a new frame.")
3628 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3629 (defun gdb-get-changed-registers ()
3630 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3631 (not (gdb-pending-p 'gdb-get-changed-registers)))
3635 "-data-list-changed-registers"
3636 'gdb-changed-registers-handler))
3637 (gdb-add-pending 'gdb-get-changed-registers))))
3639 (defun gdb-changed-registers-handler ()
3640 (gdb-delete-pending 'gdb-get-changed-registers)
3641 (setq gdb-changed-registers nil)
3642 (dolist (register-number (gdb-get-field (gdb-json-partial-output) 'changed-registers))
3643 (push register-number gdb-changed-registers)))
3645 (defun gdb-register-names-handler ()
3646 ;; Don't use gdb-pending-triggers because this handler is called
3647 ;; only once (in gdb-init-1)
3648 (setq gdb-register-names nil)
3649 (dolist (register-name (gdb-get-field (gdb-json-partial-output) 'register-names))
3650 (push register-name gdb-register-names))
3651 (setq gdb-register-names (reverse gdb-register-names)))
3654 (defun gdb-get-source-file-list ()
3655 "Create list of source files for current GDB session.
3656 If buffers already exist for any of these files, gud-minor-mode
3658 (goto-char (point-min))
3659 (while (re-search-forward gdb-source-file-regexp nil t)
3660 (push (match-string 1) gdb-source-file-list))
3661 (dolist (buffer (buffer-list))
3662 (with-current-buffer buffer
3663 (when (member buffer-file-name gdb-source-file-list)
3664 (gdb-init-buffer))))
3665 (gdb-force-mode-line-update
3666 (propertize "ready" 'face font-lock-variable-name-face)))
3668 (defun gdb-get-main-selected-frame ()
3669 "Trigger for `gdb-frame-handler' which uses main current
3670 thread. Called from `gdb-update'."
3671 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3674 (list (gdb-current-context-command "-stack-info-frame") 'gdb-frame-handler))
3675 (gdb-add-pending 'gdb-get-main-selected-frame))))
3677 (defun gdb-frame-handler ()
3678 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3679 overlay arrow in source buffer."
3680 (gdb-delete-pending 'gdb-get-main-selected-frame)
3681 (let ((frame (gdb-get-field (gdb-json-partial-output) 'frame)))
3683 (setq gdb-selected-frame (gdb-get-field frame 'func))
3684 (setq gdb-selected-file (gdb-get-field frame 'fullname))
3685 (let ((line (gdb-get-field frame 'line)))
3686 (setq gdb-selected-line (or (and line (string-to-number line))
3687 nil)) ; don't fail if line is nil
3688 (when line ; obey the current file only if we have line info
3689 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3690 (gud-display-frame)))
3691 (if gud-overlay-arrow-position
3692 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3693 (position (marker-position gud-overlay-arrow-position)))
3695 (with-current-buffer buffer
3696 (setq fringe-indicator-alist
3697 (if (string-equal gdb-frame-number "0")
3699 '((overlay-arrow . hollow-right-triangle))))
3700 (setq gud-overlay-arrow-position (make-marker))
3701 (set-marker gud-overlay-arrow-position position))))))))
3703 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3705 (defun gdb-get-prompt ()
3706 "Find prompt for GDB session."
3707 (goto-char (point-min))
3708 (setq gdb-prompt-name nil)
3709 (re-search-forward gdb-prompt-name-regexp nil t)
3710 (setq gdb-prompt-name (match-string 1))
3711 ;; Insert first prompt.
3712 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3714 ;;;; Window management
3715 (defun gdb-display-buffer (buf dedicated &optional frame)
3718 If BUF is already displayed in some window, show it, deiconifying
3719 the frame if necessary. Otherwise, find least recently used
3720 window and show BUF there, if the window is not used for GDB
3721 already, in which case that window is splitted first."
3722 (let ((answer (get-buffer-window buf (or frame 0))))
3724 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3725 (let ((window (get-lru-window)))
3726 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3728 (let* ((largest (get-largest-window))
3729 (cur-size (window-height largest)))
3730 (setq answer (split-window largest))
3731 (set-window-buffer answer buf)
3732 (set-window-dedicated-p answer dedicated)
3734 (set-window-buffer window buf)
3737 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3738 "Find window displaying a buffer with the same
3739 `gdb-buffer-type' as BUF and show BUF there. If no such window
3740 exists, just call `gdb-display-buffer' for BUF. If the window
3741 found is already dedicated, split window according to
3742 SPLIT-HORIZONTAL and show BUF in the new window."
3744 (when (not (get-buffer-window buf))
3745 (let* ((buf-type (gdb-buffer-type buf))
3747 (get-window-with-predicate
3750 (gdb-buffer-type (window-buffer w)))
3751 (not (window-dedicated-p w)))))))
3753 (set-window-buffer existing-window buf)
3754 (let ((dedicated-window
3755 (get-window-with-predicate
3758 (gdb-buffer-type (window-buffer w)))))))
3759 (if dedicated-window
3761 (split-window dedicated-window nil split-horizontal) buf)
3762 (gdb-display-buffer buf t))))))
3763 (error "Null buffer")))
3765 ;;; Shared keymap initialization:
3767 (let ((menu (make-sparse-keymap "GDB-Windows")))
3768 (define-key gud-menu-map [displays]
3769 `(menu-item "GDB-Windows" ,menu
3770 :visible (eq gud-minor-mode 'gdbmi)))
3771 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3772 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3773 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3774 (define-key menu [disassembly]
3775 '("Disassembly" . gdb-display-disassembly-buffer))
3776 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3777 (define-key menu [inferior]
3778 '("Separate IO" . gdb-display-separate-io-buffer))
3779 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3780 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3781 (define-key menu [breakpoints]
3782 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3784 (let ((menu (make-sparse-keymap "GDB-Frames")))
3785 (define-key gud-menu-map [frames]
3786 `(menu-item "GDB-Frames" ,menu
3787 :visible (eq gud-minor-mode 'gdbmi)))
3788 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3789 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3790 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3791 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
3792 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3793 (define-key menu [inferior]
3794 '("Separate IO" . gdb-frame-separate-io-buffer))
3795 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3796 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3797 (define-key menu [breakpoints]
3798 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3800 (let ((menu (make-sparse-keymap "GDB-MI")))
3801 (define-key menu [gdb-customize]
3802 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3803 :help "Customize Gdb Graphical Mode options."))
3804 (define-key menu [gdb-many-windows]
3805 '(menu-item "Display Other Windows" gdb-many-windows
3806 :help "Toggle display of locals, stack and breakpoint information"
3807 :button (:toggle . gdb-many-windows)))
3808 (define-key menu [gdb-restore-windows]
3809 '(menu-item "Restore Window Layout" gdb-restore-windows
3810 :help "Restore standard layout for debug session."))
3811 (define-key menu [sep1]
3813 (define-key menu [all-threads]
3814 '(menu-item "GUD controls all threads"
3817 (setq gdb-gud-control-all-threads t))
3818 :help "GUD start/stop commands apply to all threads"
3819 :button (:radio . gdb-gud-control-all-threads)))
3820 (define-key menu [current-thread]
3821 '(menu-item "GUD controls current thread"
3824 (setq gdb-gud-control-all-threads nil))
3825 :help "GUD start/stop commands apply to current thread only"
3826 :button (:radio . (not gdb-gud-control-all-threads))))
3827 (define-key menu [sep2]
3829 (define-key menu [gdb-customize-reasons]
3830 '(menu-item "Customize switching..."
3833 (customize-option 'gdb-switch-reasons))))
3834 (define-key menu [gdb-switch-when-another-stopped]
3835 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped gdb-switch-when-another-stopped
3836 "Automatically switch to stopped thread"
3837 "GDB thread switching %s"
3838 "Switch to stopped thread"))
3839 (define-key gud-menu-map [mi]
3840 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3842 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3843 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3844 ;; tool-bar-local-item-from-menu here because it appends new buttons
3845 ;; to toolbar from right to left while we want our A/T throttle to
3846 ;; show up right before Run button.
3847 (define-key-after gud-tool-bar-map [all-threads]
3848 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3849 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3850 :visible (and (eq gud-minor-mode 'gdbmi)
3852 (not gdb-gud-control-all-threads)))
3855 (define-key-after gud-tool-bar-map [current-thread]
3856 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3857 :image (find-image '((:type xpm :file "gud/all.xpm")))
3858 :visible (and (eq gud-minor-mode 'gdbmi)
3860 gdb-gud-control-all-threads))
3863 (defun gdb-frame-gdb-buffer ()
3864 "Display GUD buffer in a new frame."
3866 (let ((special-display-regexps (append special-display-regexps '(".*")))
3867 (special-display-frame-alist
3868 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3869 gdb-frame-parameters)))
3870 (same-window-regexps nil))
3871 (display-buffer gud-comint-buffer)))
3873 (defun gdb-display-gdb-buffer ()
3874 "Display GUD buffer."
3876 (let ((same-window-regexps nil))
3877 (select-window (display-buffer gud-comint-buffer nil 0))))
3879 (defun gdb-set-window-buffer (name &optional ignore-dedicated)
3880 "Set buffer of selected window to NAME and dedicate window.
3882 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
3883 window is dedicated."
3884 (when ignore-dedicated
3885 (set-window-dedicated-p (selected-window) nil))
3886 (set-window-buffer (selected-window) (get-buffer name))
3887 (set-window-dedicated-p (selected-window) t))
3889 (defun gdb-setup-windows ()
3890 "Layout the window pattern for `gdb-many-windows'."
3891 (gdb-display-locals-buffer)
3892 (gdb-display-stack-buffer)
3893 (delete-other-windows)
3894 (gdb-display-breakpoints-buffer)
3895 (delete-other-windows)
3897 (pop-to-buffer gud-comint-buffer)
3898 (split-window nil ( / ( * (window-height) 3) 4))
3899 (split-window nil ( / (window-height) 3))
3900 (split-window-horizontally)
3902 (gdb-set-window-buffer (gdb-locals-buffer-name))
3905 (if gud-last-last-frame
3906 (gud-find-file (car gud-last-last-frame))
3908 (gud-find-file gdb-main-file)
3909 ;; Put buffer list in window if we
3910 ;; can't find a source file.
3911 (list-buffers-noselect))))
3912 (setq gdb-source-window (selected-window))
3913 (when gdb-use-separate-io-buffer
3914 (split-window-horizontally)
3916 (gdb-set-window-buffer
3917 (gdb-get-buffer-create 'gdb-inferior-io)))
3919 (gdb-set-window-buffer (gdb-stack-buffer-name))
3920 (split-window-horizontally)
3922 (gdb-set-window-buffer (if gdb-show-threads-by-default
3923 (gdb-threads-buffer-name)
3924 (gdb-breakpoints-buffer-name)))
3927 (defcustom gdb-many-windows nil
3928 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
3929 In this case it starts with two windows: one displaying the GUD
3930 buffer and the other with the source file with the main routine
3931 of the debugged program. Non-nil means display the layout shown for
3937 (defun gdb-many-windows (arg)
3938 "Toggle the number of windows in the basic arrangement.
3939 With arg, display additional buffers iff arg is positive."
3941 (setq gdb-many-windows
3943 (not gdb-many-windows)
3944 (> (prefix-numeric-value arg) 0)))
3945 (message (format "Display of other windows %sabled"
3946 (if gdb-many-windows "en" "dis")))
3947 (if (and gud-comint-buffer
3948 (buffer-name gud-comint-buffer))
3950 (gdb-restore-windows)
3953 (defun gdb-restore-windows ()
3954 "Restore the basic arrangement of windows used by gdb.
3955 This arrangement depends on the value of `gdb-many-windows'."
3957 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3958 (delete-other-windows)
3959 (if gdb-many-windows
3961 (when (or gud-last-last-frame gdb-show-main)
3965 (if gud-last-last-frame
3966 (gud-find-file (car gud-last-last-frame))
3967 (gud-find-file gdb-main-file)))
3968 (setq gdb-source-window (selected-window))
3972 "Exit a debugging session cleanly.
3973 Kills the gdb buffers, and resets variables and the source buffers."
3974 (dolist (buffer (buffer-list))
3975 (unless (eq buffer gud-comint-buffer)
3976 (with-current-buffer buffer
3977 (if (eq gud-minor-mode 'gdbmi)
3978 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3980 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3981 (setq gud-minor-mode nil)
3982 (kill-local-variable 'tool-bar-map)
3983 (kill-local-variable 'gdb-define-alist))))))
3984 (setq gdb-disassembly-position nil)
3985 (setq overlay-arrow-variable-list
3986 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
3987 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3988 (setq gdb-stack-position nil)
3989 (setq overlay-arrow-variable-list
3990 (delq 'gdb-stack-position overlay-arrow-variable-list))
3991 (setq gdb-thread-position nil)
3992 (setq overlay-arrow-variable-list
3993 (delq 'gdb-thread-position overlay-arrow-variable-list))
3994 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3995 (setq gud-running nil)
3996 (setq gdb-active-process nil)
3997 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3999 (defun gdb-get-source-file ()
4000 "Find the source file where the program starts and display it with related
4001 buffers, if required."
4002 (goto-char (point-min))
4003 (if (re-search-forward gdb-source-file-regexp nil t)
4004 (setq gdb-main-file (match-string 1)))
4005 (if gdb-many-windows
4007 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4009 (let ((pop-up-windows t))
4010 (display-buffer (gud-find-file gdb-main-file))))))
4013 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4014 "Put string PUTSTRING in front of POS in the current buffer.
4015 PUTSTRING is displayed by putting an overlay into the current buffer with a
4016 `before-string' string that has a `display' property whose value is
4018 (let ((string (make-string 1 ?x))
4019 (buffer (current-buffer)))
4020 (setq putstring (copy-sequence putstring))
4021 (let ((overlay (make-overlay pos pos buffer))
4023 (list (list 'margin 'left-margin) putstring))))
4024 (put-text-property 0 1 'display prop string)
4026 (add-text-properties 0 1 sprops string))
4027 (overlay-put overlay 'put-break t)
4028 (overlay-put overlay 'before-string string))))
4030 ;;from remove-images
4031 (defun gdb-remove-strings (start end &optional buffer)
4032 "Remove strings between START and END in BUFFER.
4033 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4034 BUFFER nil or omitted means use the current buffer."
4036 (setq buffer (current-buffer)))
4037 (dolist (overlay (overlays-in start end))
4038 (when (overlay-get overlay 'put-break)
4039 (delete-overlay overlay))))
4041 (defun gdb-put-breakpoint-icon (enabled bptno)
4042 (let ((start (- (line-beginning-position) 1))
4043 (end (+ (line-end-position) 1))
4044 (putstring (if enabled "B" "b"))
4045 (source-window (get-buffer-window (current-buffer) 0)))
4046 (add-text-properties
4047 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4050 (add-text-properties
4051 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4052 (add-text-properties
4053 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4054 (gdb-remove-breakpoint-icons start end)
4055 (if (display-images-p)
4056 (if (>= (or left-fringe-width
4057 (if source-window (car (window-fringes source-window)))
4058 gdb-buffer-fringe-width) 8)
4061 `(left-fringe breakpoint
4064 'breakpoint-disabled))
4066 'gdb-enabled enabled)
4067 (when (< left-margin-width 2)
4068 (save-current-buffer
4069 (setq left-margin-width 2)
4073 left-margin-width right-margin-width))))
4076 (or breakpoint-enabled-icon
4077 (setq breakpoint-enabled-icon
4078 (find-image `((:type xpm :data
4079 ,breakpoint-xpm-data
4080 :ascent 100 :pointer hand)
4082 ,breakpoint-enabled-pbm-data
4083 :ascent 100 :pointer hand)))))
4084 (or breakpoint-disabled-icon
4085 (setq breakpoint-disabled-icon
4086 (find-image `((:type xpm :data
4087 ,breakpoint-xpm-data
4088 :conversion disabled
4089 :ascent 100 :pointer hand)
4091 ,breakpoint-disabled-pbm-data
4092 :ascent 100 :pointer hand))))))
4096 (when (< left-margin-width 2)
4097 (save-current-buffer
4098 (setq left-margin-width 2)
4099 (let ((window (get-buffer-window (current-buffer) 0)))
4102 window left-margin-width right-margin-width)))))
4104 (propertize putstring
4105 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
4108 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4109 (gdb-remove-strings start end)
4110 (if (display-images-p)
4111 (remove-images start end))
4113 (setq left-margin-width 0)
4114 (let ((window (get-buffer-window (current-buffer) 0)))
4117 window left-margin-width right-margin-width)))))
4121 ;; arch-tag: 1b41ea2b-f364-4cec-8f35-e02e4fe01912
4122 ;;; gdb-mi.el ends here