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 (defun gdb-find-watch-expression ()
523 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
524 (varnum (car var)) expr array)
525 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
526 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
527 (component-list (split-string (match-string 2 varnum) "\\." t)))
528 (setq expr (nth 1 var1))
529 (setq varnumlet (car var1))
530 (dolist (component component-list)
531 (setq var2 (assoc varnumlet gdb-var-list))
532 (setq expr (concat expr
533 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
534 (concat "[" component "]")
535 (concat "." component))))
536 (setq varnumlet (concat varnumlet "." component)))
539 ;; noall is used for commands which don't take --all, but only
541 (defun gdb-gud-context-command (command &optional noall)
542 "When `gdb-non-stop' is t, add --thread option to COMMAND if
543 `gdb-gud-control-all-threads' is nil and --all option otherwise.
544 If NOALL is t, always add --thread option no matter what
545 `gdb-gud-control-all-threads' value is.
547 When `gdb-non-stop' is nil, return COMMAND unchanged."
549 (if (and gdb-gud-control-all-threads
551 (concat command " --all ")
552 (gdb-current-context-command command t))
555 ;; TODO Document this. We use noarg when not in gud-def
556 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
559 (gdb-gud-context-command ,cmd1 ,noall)
560 ,cmd2) ,(when (not noarg) 'arg)))
563 (defun gdb (command-line)
564 "Run gdb on program FILE in buffer *gud-FILE*.
565 The directory containing FILE becomes the initial working directory
566 and source-file directory for your debugger.
568 If `gdb-many-windows' is nil (the default value) then gdb just
569 pops up the GUD buffer unless `gdb-show-main' is t. In this case
570 it starts with two windows: one displaying the GUD buffer and the
571 other with the source file with the main routine of the inferior.
573 If `gdb-many-windows' is t, regardless of the value of
574 `gdb-show-main', the layout below will appear. Keybindings are
575 shown in some of the buffers.
577 Watch expressions appear in the speedbar/slowbar.
579 The following commands help control operation :
581 `gdb-many-windows' - Toggle the number of windows gdb uses.
582 `gdb-restore-windows' - To restore the window layout.
584 See Info node `(emacs)GDB Graphical Interface' for a more
585 detailed description of this mode.
588 +----------------------------------------------------------------------+
590 +-----------------------------------+----------------------------------+
591 | GUD buffer (I/O of GDB) | Locals buffer |
595 +-----------------------------------+----------------------------------+
596 | Source buffer | I/O buffer (of debugged program) |
604 +-----------------------------------+----------------------------------+
605 | Stack buffer | Breakpoints buffer |
606 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
607 | | RET gdb-goto-breakpoint |
608 | | D gdb-delete-breakpoint |
609 +-----------------------------------+----------------------------------+"
611 (interactive (list (gud-query-cmdline 'gdb)))
613 (when (and gud-comint-buffer
614 (buffer-name gud-comint-buffer)
615 (get-buffer-process gud-comint-buffer)
616 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
617 (gdb-restore-windows)
619 "Multiple debugging requires restarting in text command mode"))
621 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
622 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
623 (setq comint-input-sender 'gdb-send)
625 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
626 "Set temporary breakpoint at current line.")
628 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
629 "\C-j" "Set execution address to current line.")
631 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
632 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
633 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
634 (gud-def gud-pstar "print* %e" nil
635 "Evaluate C dereferenced pointer expression at point.")
637 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
639 "Step one source line with display.")
640 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
642 "Step one instruction with display.")
643 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
645 "Step one line (skip functions).")
646 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
648 "Step one instruction (skip functions).")
649 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
651 "Continue with display.")
652 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
654 "Finish executing current function.")
655 (gud-def gud-run "-exec-run"
659 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
660 (gud-call "break %f:%l" arg)
664 (gud-call "break *%a" arg)))
665 "\C-b" "Set breakpoint at current line or address.")
667 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
668 (gud-call "clear %f:%l" arg)
672 (gud-call "clear *%a" arg)))
673 "\C-d" "Remove breakpoint at current line or address.")
675 ;; -exec-until doesn't support --all yet
676 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
677 (gud-call "-exec-until %f:%l" arg)
681 (gud-call "-exec-until *%a" arg)))
682 "\C-u" "Continue to current line or address.")
683 ;; TODO Why arg here?
685 gud-go (gud-call (if gdb-active-process
686 (gdb-gud-context-command "-exec-continue")
688 nil "Start or continue execution.")
690 ;; For debugging Emacs only.
694 "pp1 " (if (eq (buffer-local-value
695 'major-mode (window-buffer)) 'speedbar-mode)
696 (gdb-find-watch-expression) "%e")) arg)
697 nil "Print the Emacs s-expression.")
699 (define-key gud-minor-mode-map [left-margin mouse-1]
700 'gdb-mouse-set-clear-breakpoint)
701 (define-key gud-minor-mode-map [left-fringe mouse-1]
702 'gdb-mouse-set-clear-breakpoint)
703 (define-key gud-minor-mode-map [left-margin C-mouse-1]
704 'gdb-mouse-toggle-breakpoint-margin)
705 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
706 'gdb-mouse-toggle-breakpoint-fringe)
708 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
710 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
712 (define-key gud-minor-mode-map [left-margin mouse-3]
714 (define-key gud-minor-mode-map [left-fringe mouse-3]
717 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
719 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
721 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
723 (define-key gud-minor-mode-map [left-margin C-mouse-3]
726 (local-set-key "\C-i" 'gud-gdb-complete-command)
727 (setq gdb-first-prompt t)
728 (setq gud-running nil)
730 (run-hooks 'gdb-mode-hook))
734 (setq gdb-selected-frame nil
736 gdb-thread-number nil
738 gdb-pending-triggers nil
739 gdb-output-sink 'user
740 gdb-location-alist nil
741 gdb-source-file-list nil
744 gdb-handler-alist '()
745 gdb-handler-number nil
747 gdb-first-done-or-error t
748 gdb-buffer-fringe-width (car (window-fringes))
750 gdb-source-window nil
751 gdb-inferior-status nil
753 gdb-buf-publisher '()
755 gdb-breakpoints-list '()
756 gdb-non-stop gdb-non-stop-setting)
758 (setq gdb-buffer-type 'gdbmi)
760 (gdb-force-mode-line-update
761 (propertize "initializing..." 'face font-lock-variable-name-face))
762 (when gdb-use-separate-io-buffer
763 (gdb-get-buffer-create 'gdb-inferior-io)
764 (gdb-clear-inferior-io)
765 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
767 ;; Needs GDB 6.4 onwards
768 (list (concat "-inferior-tty-set "
769 (process-tty-name (get-process "gdb-inferior")))
771 (if (eq window-system 'w32)
772 (gdb-input (list "-gdb-set new-console off" 'ignore)))
773 (gdb-input (list "-gdb-set height 0" 'ignore))
776 (gdb-input (list "-gdb-set non-stop 1" 'ignore))
777 (gdb-input (list "-gdb-set target-async 1" 'ignore)))
779 ;; find source file and compilation directory here
781 ; Needs GDB 6.2 onwards.
782 (list "-file-list-exec-source-files" 'gdb-get-source-file-list))
783 (if gdb-create-source-file-list
785 ; Needs GDB 6.0 onwards.
786 (list "-file-list-exec-source-file" 'gdb-get-source-file)))
788 (list "-gdb-show prompt" 'gdb-get-prompt)))
790 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
792 (defun gdb-create-define-alist ()
793 "Create an alist of #define directives for GUD tooltips."
794 (let* ((file (buffer-file-name))
796 (with-output-to-string
797 (with-current-buffer standard-output
800 ;; call-process doesn't work with remote file names.
801 (not (file-remote-p default-directory))
802 (call-process shell-file-name file
803 (list t nil) nil "-c"
804 (concat gdb-cpp-define-alist-program " "
805 gdb-cpp-define-alist-flags))))))
806 (define-list (split-string output "\n" t))
808 (setq gdb-define-alist nil)
809 (dolist (define define-list)
810 (setq name (nth 1 (split-string define "[( ]")))
811 (push (cons name define) gdb-define-alist))))
813 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
814 (defvar tooltip-use-echo-area)
816 (defun gdb-tooltip-print (expr)
818 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
819 (goto-char (point-min))
821 (if (search-forward "=" nil t)
822 (concat expr (buffer-substring (- (point) 2) (point-max)))
824 ;; remove newline for gud-tooltip-echo-area
825 (substring string 0 (- (length string) 1))))
826 (or gud-tooltip-echo-area tooltip-use-echo-area
827 (not (display-graphic-p)))))
829 ;; If expr is a macro for a function don't print because of possible dangerous
830 ;; side-effects. Also printing a function within a tooltip generates an
831 ;; unexpected starting annotation (phase error).
832 (defun gdb-tooltip-print-1 (expr)
833 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
834 (goto-char (point-min))
835 (if (search-forward "expands to: " nil t)
836 (unless (looking-at "\\S-+.*(.*).*")
838 (list (concat "print " expr)
839 `(lambda () (gdb-tooltip-print ,expr))))))))
841 (defun gdb-init-buffer ()
842 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
843 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
844 (when gud-tooltip-mode
845 (make-local-variable 'gdb-define-alist)
846 (gdb-create-define-alist)
847 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
849 (defmacro gdb-if-arrow (arrow-position &rest body)
851 (let ((buffer (marker-buffer ,arrow-position)) (line))
852 (if (equal buffer (window-buffer (posn-window end)))
853 (with-current-buffer buffer
854 (when (or (equal start end)
855 (equal (posn-point start)
856 (marker-position ,arrow-position)))
859 (defun gdb-mouse-until (event)
860 "Continue running until a source line past the current line.
861 The destination source line can be selected either by clicking
862 with mouse-3 on the fringe/margin or dragging the arrow
863 with mouse-1 (default bindings)."
865 (let ((start (event-start event))
866 (end (event-end event)))
867 (gdb-if-arrow gud-overlay-arrow-position
868 (setq line (line-number-at-pos (posn-point end)))
869 (gud-call (concat "until " (number-to-string line))))
870 (gdb-if-arrow gdb-disassembly-position
872 (goto-line (line-number-at-pos (posn-point end)))
874 (gud-call (concat "until *%a"))))))
876 (defun gdb-mouse-jump (event)
877 "Set execution address/line.
878 The destination source line can be selected either by clicking with C-mouse-3
879 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
880 Unlike `gdb-mouse-until' the destination address can be before the current
881 line, and no execution takes place."
883 (let ((start (event-start event))
884 (end (event-end event)))
885 (gdb-if-arrow gud-overlay-arrow-position
886 (setq line (line-number-at-pos (posn-point end)))
888 (gud-call (concat "tbreak " (number-to-string line)))
889 (gud-call (concat "jump " (number-to-string line)))))
890 (gdb-if-arrow gdb-disassembly-position
892 (goto-line (line-number-at-pos (posn-point end)))
895 (gud-call (concat "tbreak *%a"))
896 (gud-call (concat "jump *%a")))))))
898 (defcustom gdb-show-changed-values t
899 "If non-nil change the face of out of scope variables and changed values.
900 Out of scope variables are suppressed with `shadow' face.
901 Changed values are highlighted with the face `font-lock-warning-face'."
906 (defcustom gdb-max-children 40
907 "Maximum number of children before expansion requires confirmation."
912 (defcustom gdb-delete-out-of-scope t
913 "If non-nil delete watch expressions automatically when they go out of scope."
918 (defcustom gdb-speedbar-auto-raise nil
919 "If non-nil raise speedbar every time display of watch expressions is\
925 (defcustom gdb-use-colon-colon-notation nil
926 "If non-nil use FUN::VAR format to display variables in the speedbar."
931 (defun gdb-speedbar-auto-raise (arg)
932 "Toggle automatic raising of the speedbar for watch expressions.
933 With prefix argument ARG, automatically raise speedbar if ARG is
934 positive, otherwise don't automatically raise it."
936 (setq gdb-speedbar-auto-raise
938 (not gdb-speedbar-auto-raise)
939 (> (prefix-numeric-value arg) 0)))
940 (message (format "Auto raising %sabled"
941 (if gdb-speedbar-auto-raise "en" "dis"))))
943 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
944 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
946 (declare-function tooltip-identifier-from-point "tooltip" (point))
948 (defun gud-watch (&optional arg event)
949 "Watch expression at point.
950 With arg, enter name of variable to be watched in the minibuffer."
951 (interactive (list current-prefix-arg last-input-event))
952 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
953 (if (eq minor-mode 'gdbmi)
955 (if event (posn-set-point (event-end event)))
957 (save-selected-window
960 (completing-read "Name of variable: "
961 'gud-gdb-complete-command)
962 (if (and transient-mark-mode mark-active)
963 (buffer-substring (region-beginning) (region-end))
964 (concat (if (eq major-mode 'gdb-registers-mode) "$")
965 (tooltip-identifier-from-point (point)))))))
966 (set-text-properties 0 (length expr) nil expr)
968 (list (concat"-var-create - * " expr "")
969 `(lambda () (gdb-var-create-handler ,expr)))))))
970 (message "gud-watch is a no-op in this mode."))))
972 (defun gdb-var-create-handler (expr)
973 (let* ((result (gdb-json-partial-output)))
974 (if (not (gdb-get-field result 'msg))
978 (gdb-get-field result 'name)
979 (if (and (string-equal gdb-current-language "c")
980 gdb-use-colon-colon-notation gdb-selected-frame)
981 (setq expr (concat gdb-selected-frame "::" expr))
983 (gdb-get-field result 'numchild)
984 (gdb-get-field result 'type)
985 (gdb-get-field result 'value)
987 (push var gdb-var-list)
989 (unless (string-equal
990 speedbar-initial-expansion-list-name "GUD")
991 (speedbar-change-initial-expansion-list "GUD"))
994 (concat "-var-evaluate-expression " (car var))
995 `(lambda () (gdb-var-evaluate-expression-handler
997 (message-box "No symbol \"%s\" in current context." expr))))
999 (defun gdb-speedbar-update ()
1000 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1001 (not (gdb-pending-p 'gdb-speedbar-timer)))
1002 ;; Dummy command to update speedbar even when idle.
1003 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn))
1004 ;; Keep gdb-pending-triggers non-nil till end.
1005 (gdb-add-pending 'gdb-speedbar-timer)))
1007 (defun gdb-speedbar-timer-fn ()
1008 (if gdb-speedbar-auto-raise
1009 (raise-frame speedbar-frame))
1010 (gdb-delete-pending 'gdb-speedbar-timer)
1011 (speedbar-timer-fn))
1013 (defun gdb-var-evaluate-expression-handler (varnum changed)
1014 (goto-char (point-min))
1015 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1016 (let ((var (assoc varnum gdb-var-list)))
1018 (if changed (setcar (nthcdr 5 var) 'changed))
1019 (setcar (nthcdr 4 var) (read (match-string 1)))))
1020 (gdb-speedbar-update))
1022 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1023 (defun gdb-var-list-children (varnum)
1025 (list (concat "-var-update " varnum) 'ignore))
1027 (list (concat "-var-list-children --all-values "
1029 `(lambda () (gdb-var-list-children-handler ,varnum)))))
1031 (defconst gdb-var-list-children-regexp
1032 "child={.*?name=\"\\(.+?\\)\".*?,exp=\"\\(.+?\\)\".*?,\
1033 numchild=\"\\(.+?\\)\".*?,value=\\(\".*?\"\\).*?,type=\"\\(.+?\\)\".*?}")
1035 (defun gdb-var-list-children-handler (varnum)
1036 (goto-char (point-min))
1037 (let ((var-list nil))
1038 (catch 'child-already-watched
1039 (dolist (var gdb-var-list)
1040 (if (string-equal varnum (car var))
1043 (while (re-search-forward gdb-var-list-children-regexp nil t)
1044 (let ((varchild (list (match-string 1)
1048 (read (match-string 4))
1050 (if (assoc (car varchild) gdb-var-list)
1051 (throw 'child-already-watched nil))
1052 (push varchild var-list))))
1053 (push var var-list)))
1054 (setq gdb-var-list (nreverse var-list))))
1055 (gdb-speedbar-update))
1057 (defun gdb-var-set-format (format)
1058 "Set the output format for a variable displayed in the speedbar."
1059 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1062 (list (concat "-var-set-format " varnum " " format) 'ignore))
1065 (defun gdb-var-delete-1 (varnum)
1067 (list (concat "-var-delete " varnum) 'ignore))
1068 (setq gdb-var-list (delq var gdb-var-list))
1069 (dolist (varchild gdb-var-list)
1070 (if (string-match (concat (car var) "\\.") (car varchild))
1071 (setq gdb-var-list (delq varchild gdb-var-list)))))
1073 (defun gdb-var-delete ()
1074 "Delete watch expression at point from the speedbar."
1076 (let ((text (speedbar-line-text)))
1077 (string-match "\\(\\S-+\\)" text)
1078 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1080 (if (string-match "\\." (car var))
1081 (message-box "Can only delete a root expression")
1082 (gdb-var-delete-1 varnum)))))
1084 (defun gdb-var-delete-children (varnum)
1085 "Delete children of variable object at point from the speedbar."
1087 (list (concat "-var-delete -c " varnum) 'ignore)))
1089 (defun gdb-edit-value (text token indent)
1090 "Assign a value to a variable displayed in the speedbar."
1091 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1092 (varnum (car var)) (value))
1093 (setq value (read-string "New value: "))
1095 (list (concat "-var-assign " varnum " " value)
1096 `(lambda () (gdb-edit-value-handler ,value))))))
1098 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1100 (defun gdb-edit-value-handler (value)
1101 (goto-char (point-min))
1102 (if (re-search-forward gdb-error-regexp nil t)
1103 (message-box "Invalid number or expression (%s)" value)))
1105 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1106 (defun gdb-var-update ()
1107 (if (not (gdb-pending-p 'gdb-var-update))
1109 (list "-var-update --all-values *" 'gdb-var-update-handler)))
1110 (gdb-add-pending 'gdb-var-update))
1112 (defconst gdb-var-update-regexp
1113 "{.*?name=\"\\(.*?\\)\".*?,\\(?:value=\\(\".*?\"\\),\\)?.*?\
1114 in_scope=\"\\(.*?\\)\".*?}")
1116 (defun gdb-var-update-handler ()
1117 (dolist (var gdb-var-list)
1118 (setcar (nthcdr 5 var) nil))
1119 (goto-char (point-min))
1120 (while (re-search-forward gdb-var-update-regexp nil t)
1121 (let* ((varnum (match-string 1))
1122 (var (assoc varnum gdb-var-list)))
1124 (let ((match (match-string 3)))
1125 (cond ((string-equal match "false")
1126 (if gdb-delete-out-of-scope
1127 (gdb-var-delete-1 varnum)
1128 (setcar (nthcdr 5 var) 'out-of-scope)))
1129 ((string-equal match "true")
1130 (setcar (nthcdr 5 var) 'changed)
1131 (setcar (nthcdr 4 var)
1132 (read (match-string 2))))
1133 ((string-equal match "invalid")
1134 (gdb-var-delete-1 varnum)))))))
1135 (gdb-delete-pending 'gdb-var-update)
1136 (gdb-speedbar-update))
1138 (defun gdb-speedbar-expand-node (text token indent)
1139 "Expand the node the user clicked on.
1140 TEXT is the text of the button we clicked on, a + or - item.
1141 TOKEN is data related to this node.
1142 INDENT is the current indentation depth."
1143 (cond ((string-match "+" text) ;expand this node
1144 (let* ((var (assoc token gdb-var-list))
1145 (expr (nth 1 var)) (children (nth 2 var)))
1146 (if (or (<= (string-to-number children) gdb-max-children)
1148 (format "%s has %s children. Continue? " expr children)))
1149 (gdb-var-list-children token))))
1150 ((string-match "-" text) ;contract this node
1151 (dolist (var gdb-var-list)
1152 (if (string-match (concat token "\\.") (car var))
1153 (setq gdb-var-list (delq var gdb-var-list))))
1154 (gdb-var-delete-children token)
1155 (speedbar-change-expand-button-char ?+)
1156 (speedbar-delete-subblock indent))
1157 (t (error "Ooops... not sure what to do")))
1158 (speedbar-center-buffer-smartly))
1160 (defun gdb-get-target-string ()
1161 (with-current-buffer gud-comint-buffer
1168 ;; Each buffer has a TYPE -- a symbol that identifies the function
1169 ;; of that particular buffer.
1171 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1172 ;; is constructed specially.
1174 ;; Others are constructed by gdb-get-buffer-create and
1175 ;; named according to the rules set forth in the gdb-buffer-rules
1177 (defvar gdb-buffer-rules '())
1179 (defun gdb-rules-name-maker (rules-entry)
1181 (defun gdb-rules-buffer-mode (rules-entry)
1182 (nth 2 rules-entry))
1183 (defun gdb-rules-update-trigger (rules-entry)
1184 (nth 3 rules-entry))
1186 (defun gdb-update-buffer-name ()
1187 "Rename current buffer according to name-maker associated with
1188 it in `gdb-buffer-rules'."
1189 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1190 gdb-buffer-rules))))
1191 (when f (rename-buffer (funcall f)))))
1193 (defun gdb-current-buffer-rules ()
1194 "Get `gdb-buffer-rules' entry for current buffer type."
1195 (assoc gdb-buffer-type gdb-buffer-rules))
1197 (defun gdb-current-buffer-thread ()
1198 "Get thread object of current buffer from `gdb-threads-list'.
1200 When current buffer is not bound to any thread, return main
1202 (cdr (assoc gdb-thread-number gdb-threads-list)))
1204 (defun gdb-current-buffer-frame ()
1205 "Get current stack frame object for thread of current buffer."
1206 (gdb-get-field (gdb-current-buffer-thread) 'frame))
1208 (defun gdb-buffer-type (buffer)
1209 "Get value of `gdb-buffer-type' for BUFFER."
1210 (with-current-buffer buffer
1213 (defun gdb-buffer-shows-main-thread-p ()
1214 "Return t if current GDB buffer shows main selected thread and
1215 is not bound to it."
1217 (not (local-variable-p 'gdb-thread-number)))
1219 (defun gdb-get-buffer (buffer-type &optional thread)
1220 "Get a specific GDB buffer.
1222 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1223 and `gdb-thread-number' (if provided) must be equal to THREAD."
1225 (dolist (buffer (buffer-list) nil)
1226 (with-current-buffer buffer
1227 (when (and (eq gdb-buffer-type buffer-type)
1229 (equal gdb-thread-number thread)))
1230 (throw 'found buffer))))))
1232 (defun gdb-get-buffer-create (buffer-type &optional thread)
1233 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1234 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1236 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1237 buffer-local variable of the new buffer.
1239 Buffer mode and name are selected according to buffer type.
1241 If buffer has trigger associated with it in `gdb-buffer-rules',
1242 this trigger is subscribed to `gdb-buf-publisher' and called with
1244 (or (gdb-get-buffer buffer-type thread)
1245 (let ((rules (assoc buffer-type gdb-buffer-rules))
1246 (new (generate-new-buffer "limbo")))
1247 (with-current-buffer new
1248 (let ((mode (gdb-rules-buffer-mode rules))
1249 (trigger (gdb-rules-update-trigger rules)))
1250 (when mode (funcall mode))
1251 (setq gdb-buffer-type buffer-type)
1253 (set (make-local-variable 'gdb-thread-number) thread))
1254 (set (make-local-variable 'gud-minor-mode)
1255 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1256 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1257 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1259 (gdb-add-subscriber gdb-buf-publisher
1260 (cons (current-buffer)
1261 (gdb-bind-function-to-buffer trigger (current-buffer))))
1262 (funcall trigger 'update))
1263 (current-buffer))))))
1265 (defun gdb-bind-function-to-buffer (expr buffer)
1266 "Return a function which will evaluate EXPR in BUFFER."
1267 `(lambda (&rest args)
1268 (with-current-buffer ,buffer
1269 (apply ',expr args))))
1271 ;; Used to define all gdb-frame-*-buffer functions except
1272 ;; `gdb-frame-separate-io-buffer'
1273 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1274 "Define a function NAME which shows gdb BUFFER in a separate frame.
1276 DOC is an optional documentation string."
1277 `(defun ,name (&optional thread)
1280 (let ((special-display-regexps (append special-display-regexps '(".*")))
1281 (special-display-frame-alist gdb-frame-parameters))
1282 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1284 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1285 "Define a function NAME which shows gdb BUFFER.
1287 DOC is an optional documentation string."
1288 `(defun ,name (&optional thread)
1292 (gdb-get-buffer-create ,buffer thread) t)))
1294 ;; Used to display windows with thread-bound buffers
1295 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc split-horizontal)
1296 `(defun ,name (&optional thread)
1299 (gdb-preempt-existing-or-display-buffer
1300 (gdb-get-buffer-create ,buffer thread)
1301 ,split-horizontal)))
1303 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1304 ;; at least one and possible more functions. The functions have these
1305 ;; roles in defining a buffer type:
1307 ;; NAME - Return a name for this buffer type.
1309 ;; The remaining function(s) are optional:
1311 ;; MODE - called in a new buffer with no arguments, should establish
1312 ;; the proper mode for the buffer.
1315 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1316 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1318 (setcdr binding rules)
1319 (push (cons buffer-type rules)
1320 gdb-buffer-rules))))
1322 (defun gdb-parent-mode ()
1323 "Generic mode to derive all other GDB buffer modes from."
1324 (kill-all-local-variables)
1325 (setq buffer-read-only t)
1326 (buffer-disable-undo)
1327 ;; Delete buffer from gdb-buf-publisher when it's killed
1328 ;; (if it has an associated update trigger)
1333 (let ((trigger (gdb-rules-update-trigger
1334 (gdb-current-buffer-rules))))
1336 (gdb-delete-subscriber
1338 ;; This should match gdb-add-subscriber done in
1339 ;; gdb-get-buffer-create
1340 (cons (current-buffer)
1341 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1344 ;; GUD buffers are an exception to the rules
1345 (gdb-set-buffer-rules 'gdbmi 'error)
1347 ;; Partial-output buffer : This accumulates output from a command executed on
1348 ;; behalf of emacs (rather than the user).
1350 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1351 'gdb-partial-output-name)
1353 (defun gdb-partial-output-name ()
1354 (concat " *partial-output-"
1355 (gdb-get-target-string)
1359 (gdb-set-buffer-rules 'gdb-inferior-io
1360 'gdb-inferior-io-name
1361 'gdb-inferior-io-mode)
1363 (defun gdb-inferior-io-name ()
1364 (concat "*input/output of "
1365 (gdb-get-target-string)
1368 (defun gdb-display-separate-io-buffer ()
1369 "Display IO of debugged program in a separate window."
1371 (if gdb-use-separate-io-buffer
1373 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1375 (defconst gdb-frame-parameters
1376 '((height . 14) (width . 80)
1378 (tool-bar-lines . nil)
1379 (menu-bar-lines . nil)
1380 (minibuffer . nil)))
1382 (defun gdb-frame-separate-io-buffer ()
1383 "Display IO of debugged program in a new frame."
1385 (if gdb-use-separate-io-buffer
1386 (let ((special-display-regexps (append special-display-regexps '(".*")))
1387 (special-display-frame-alist gdb-frame-parameters))
1388 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1390 (defvar gdb-inferior-io-mode-map
1391 (let ((map (make-sparse-keymap)))
1392 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1393 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1394 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1395 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1396 (define-key map "\C-d" 'gdb-separate-io-eof)
1399 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1400 "Major mode for gdb inferior-io."
1401 :syntax-table nil :abbrev-table nil
1402 ;; We want to use comint because it has various nifty and familiar
1403 ;; features. We don't need a process, but comint wants one, so create
1405 (make-comint-in-buffer
1406 "gdb-inferior" (current-buffer) "sleep" nil "1000000000"))
1408 (defun gdb-inferior-filter (proc string)
1409 (unless (string-equal string "")
1410 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1411 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1412 (insert-before-markers string)))
1414 (defun gdb-separate-io-interrupt ()
1415 "Interrupt the program being debugged."
1418 (get-buffer-process gud-comint-buffer) comint-ptyp))
1420 (defun gdb-separate-io-quit ()
1421 "Send quit signal to the program being debugged."
1424 (get-buffer-process gud-comint-buffer) comint-ptyp))
1426 (defun gdb-separate-io-stop ()
1427 "Stop the program being debugged."
1430 (get-buffer-process gud-comint-buffer) comint-ptyp))
1432 (defun gdb-separate-io-eof ()
1433 "Send end-of-file to the program being debugged."
1436 (get-buffer-process gud-comint-buffer)))
1438 (defun gdb-clear-inferior-io ()
1439 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1443 (defconst breakpoint-xpm-data
1445 static char *magick[] = {
1446 /* columns rows colors chars-per-pixel */
1462 "XPM data used for breakpoint icon.")
1464 (defconst breakpoint-enabled-pbm-data
1467 0 0 0 0 1 1 1 1 0 0 0 0
1468 0 0 0 1 1 1 1 1 1 0 0 0
1469 0 0 1 1 1 1 1 1 1 1 0 0
1470 0 1 1 1 1 1 1 1 1 1 1 0
1471 0 1 1 1 1 1 1 1 1 1 1 0
1472 0 1 1 1 1 1 1 1 1 1 1 0
1473 0 1 1 1 1 1 1 1 1 1 1 0
1474 0 0 1 1 1 1 1 1 1 1 0 0
1475 0 0 0 1 1 1 1 1 1 0 0 0
1476 0 0 0 0 1 1 1 1 0 0 0 0"
1477 "PBM data used for enabled breakpoint icon.")
1479 (defconst breakpoint-disabled-pbm-data
1491 0 0 0 1 0 1 0 1 0 0"
1492 "PBM data used for disabled breakpoint icon.")
1494 (defvar breakpoint-enabled-icon nil
1495 "Icon for enabled breakpoint in display margin.")
1497 (defvar breakpoint-disabled-icon nil
1498 "Icon for disabled breakpoint in display margin.")
1500 (declare-function define-fringe-bitmap "fringe.c"
1501 (bitmap bits &optional height width align))
1503 (and (display-images-p)
1504 ;; Bitmap for breakpoint in fringe
1505 (define-fringe-bitmap 'breakpoint
1506 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1507 ;; Bitmap for gud-overlay-arrow in fringe
1508 (define-fringe-bitmap 'hollow-right-triangle
1509 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1511 (defface breakpoint-enabled
1515 "Face for enabled breakpoint icon in fringe."
1518 (defface breakpoint-disabled
1519 '((((class color) (min-colors 88)) :foreground "grey70")
1520 ;; Ensure that on low-color displays that we end up something visible.
1521 (((class color) (min-colors 8) (background light))
1522 :foreground "black")
1523 (((class color) (min-colors 8) (background dark))
1524 :foreground "white")
1525 (((type tty) (class mono))
1527 (t :background "gray"))
1528 "Face for disabled breakpoint icon in fringe."
1532 (defun gdb-send (proc string)
1533 "A comint send filter for gdb."
1534 (with-current-buffer gud-comint-buffer
1535 (let ((inhibit-read-only t))
1536 (remove-text-properties (point-min) (point-max) '(face))))
1537 ;; mimic <RET> key to repeat previous command in GDB
1538 (if (not (string= "" string))
1539 (setq gdb-last-command string)
1540 (if gdb-last-command (setq string gdb-last-command)))
1541 (if gdb-enable-debug
1542 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1543 (if (string-match "^-" string)
1546 (setq gdb-first-done-or-error t)
1547 (process-send-string proc (concat string "\n")))
1549 (if (string-match "\\\\$" string)
1550 (setq gdb-continuation (concat gdb-continuation string "\n"))
1551 (setq gdb-first-done-or-error t)
1552 (process-send-string proc (concat "-interpreter-exec console \""
1553 gdb-continuation string "\"\n"))
1554 (setq gdb-continuation nil))))
1556 (defun gdb-input (item)
1557 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1558 (setq gdb-token-number (1+ gdb-token-number))
1559 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1560 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1561 (process-send-string (get-buffer-process gud-comint-buffer)
1562 (concat (car item) "\n")))
1564 ;; NOFRAME is used for gud execution control commands
1565 (defun gdb-current-context-command (command &optional noframe)
1566 "Add --thread and --frame options to gdb COMMAND.
1568 Option values are taken from `gdb-thread-number' and
1569 `gdb-frame-number'. If `gdb-thread-number' is nil, COMMAND is
1570 returned unchanged. If `gdb-frame-number' is nil of NOFRAME is t,
1571 then no --frame option is added."
1572 ;; gdb-frame-number may be nil while gdb-thread-number is non-nil
1573 ;; (when current thread is running)
1574 (if gdb-thread-number
1575 (concat command " --thread " gdb-thread-number
1576 (if (not (or noframe (not gdb-frame-number)))
1577 (concat " --frame " gdb-frame-number) "")
1581 (defun gdb-current-context-buffer-name (name)
1582 "Add thread information and asterisks to string NAME.
1584 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1586 (if (local-variable-p 'gdb-thread-number)
1587 (format " (bound to thread %s)" gdb-thread-number)
1591 (defun gdb-current-context-mode-name (mode)
1592 "Add thread information to MODE which is to be used as
1595 (if gdb-thread-number
1596 (format " [thread %s]" gdb-thread-number)
1600 (defcustom gud-gdb-command-name "gdb -i=mi"
1601 "Default command to execute an executable under the GDB debugger."
1606 (setq gud-running nil)
1607 (setq gdb-output-sink 'user)
1608 (setq gdb-pending-triggers nil))
1610 (defun gdb-update ()
1611 "Update buffers showing status of debug session."
1612 (when gdb-first-prompt
1613 (gdb-force-mode-line-update
1614 (propertize "initializing..." 'face font-lock-variable-name-face))
1616 (setq gdb-first-prompt nil))
1617 ;; We may need to update gdb-threads-list so we can use
1618 (gdb-get-buffer-create 'gdb-threads-buffer)
1619 ;; gdb-break-list is maintained in breakpoints handler
1620 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1622 (gdb-emit-signal gdb-buf-publisher 'update)
1624 (gdb-get-main-selected-frame)
1626 (gdb-get-changed-registers)
1628 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1629 (dolist (var gdb-var-list)
1630 (setcar (nthcdr 5 var) nil))
1633 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1634 ;; because we may need to update current gud-running value without
1635 ;; changing current thread (see gdb-running)
1636 (defun gdb-setq-thread-number (number)
1637 "Only this function must be used to change `gdb-thread-number'
1638 value to NUMBER, because `gud-running' and `gdb-frame-number'
1639 need to be updated appropriately when current thread changes."
1640 (setq gdb-thread-number number)
1641 (setq gdb-frame-number "0")
1642 (gdb-update-gud-running))
1644 (defun gdb-update-gud-running ()
1645 "Set `gud-running' and `gdb-frame-number' according to the state
1648 `gdb-frame-number' is set to nil if new current thread is
1651 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1652 cannot be reliably used to determine whether or not execution
1653 control buttons should be shown in menu or toolbar. Use
1654 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1657 For all-stop mode, thread information is unavailable while target
1659 (let ((old-value gud-running))
1661 (string= (gdb-get-field (gdb-current-buffer-thread) 'state)
1663 ;; We change frame number only if the state of current thread has
1664 ;; changed or there's no current thread.
1665 (when (not (eq gud-running old-value))
1666 (if (or gud-running (not (gdb-current-buffer-thread)))
1667 (setq gdb-frame-number nil)
1668 (setq gdb-frame-number "0")))))
1670 (defun gdb-show-run-p ()
1671 "Return t if \"Run/continue\" should be shown on the toolbar."
1673 (not gdb-gud-control-all-threads)
1676 (and gdb-gud-control-all-threads
1677 (> gdb-stopped-threads-count 0))))
1679 (defun gdb-show-stop-p ()
1680 "Return t if \"Stop\" should be shown on the toolbar."
1682 (not gdb-gud-control-all-threads)
1685 (and gdb-gud-control-all-threads
1686 (> gdb-running-threads-count 0))))
1688 ;; GUD displays the selected GDB frame. This might might not be the current
1689 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1690 ;; visited breakpoint is, use that window.
1691 (defun gdb-display-source-buffer (buffer)
1692 (let* ((last-window (if gud-last-last-frame
1694 (gud-find-file (car gud-last-last-frame)))))
1695 (source-window (or last-window
1696 (if (and gdb-source-window
1697 (window-live-p gdb-source-window))
1698 gdb-source-window))))
1700 (setq gdb-source-window source-window)
1701 (set-window-buffer source-window buffer))
1704 (defun gdb-car< (a b)
1705 (< (car a) (car b)))
1707 (defvar gdbmi-record-list
1708 '((gdb-gdb . "(gdb) \n")
1709 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1710 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1711 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1712 (gdb-console . "~\\(\".*?\"\\)\n")
1713 (gdb-internals . "&\\(\".*?\"\\)\n")
1714 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1715 (gdb-running . "\\*running,\\(.*?\n\\)")
1716 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1717 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1718 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1719 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1720 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1722 (defun gud-gdbmi-marker-filter (string)
1723 "Filter GDB/MI output."
1725 ;; Record transactions if logging is enabled.
1726 (when gdb-enable-debug
1727 (push (cons 'recv string) gdb-debug-log)
1728 (if (and gdb-debug-log-max
1729 (> (length gdb-debug-log) gdb-debug-log-max))
1730 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1732 ;; Recall the left over gud-marker-acc from last time
1733 (setq gud-marker-acc (concat gud-marker-acc string))
1735 ;; Start accumulating output for the GUD buffer
1736 (setq gdb-filter-output "")
1737 (let ((output-record) (output-record-list))
1739 ;; Process all the complete markers in this chunk.
1740 (dolist (gdbmi-record gdbmi-record-list)
1741 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1742 (push (list (match-beginning 0)
1744 (match-string 1 gud-marker-acc)
1745 (match-string 2 gud-marker-acc)
1748 (setq gud-marker-acc
1749 (concat (substring gud-marker-acc 0 (match-beginning 0))
1750 ;; Pad with spaces to preserve position.
1751 (make-string (length (match-string 0 gud-marker-acc)) 32)
1752 (substring gud-marker-acc (match-end 0))))))
1754 (setq output-record-list (sort output-record-list 'gdb-car<))
1756 (dolist (output-record output-record-list)
1757 (let ((record-type (cadr output-record))
1758 (arg1 (nth 2 output-record))
1759 (arg2 (nth 3 output-record)))
1760 (if (eq record-type 'gdb-error)
1761 (gdb-done-or-error arg2 arg1 'error)
1762 (if (eq record-type 'gdb-done)
1763 (gdb-done-or-error arg2 arg1 'done)
1764 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1765 ;; error message on internal stream. Don't print to GUD buffer.
1766 (unless (and (eq record-type 'gdb-internals)
1767 (string-equal (read arg1) "No registers.\n"))
1768 (funcall record-type arg1))))))
1770 (setq gdb-output-sink 'user)
1772 (string-match "^ *" gud-marker-acc)
1773 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1777 (defun gdb-gdb (output-field))
1779 (defun gdb-shell (output-field)
1780 (let ((gdb-output-sink gdb-output-sink))
1781 (setq gdb-filter-output
1782 (concat output-field gdb-filter-output))))
1784 (defun gdb-ignored-notification (output-field))
1786 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1787 (defun gdb-thread-created (output-field))
1788 (defun gdb-thread-exited (output-field)
1789 "Handle =thread-exited async record: unset `gdb-thread-number'
1790 if current thread exited and update threads list."
1791 (let* ((thread-id (gdb-get-field (gdb-json-string output-field) 'id)))
1792 (if (string= gdb-thread-number thread-id)
1793 (gdb-setq-thread-number nil))
1794 ;; When we continue current thread and it quickly exits,
1795 ;; gdb-pending-triggers left after gdb-running disallow us to
1796 ;; properly call -thread-info without --thread option. Thus we
1797 ;; need to use gdb-wait-for-pending.
1798 (gdb-wait-for-pending
1799 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1801 (defun gdb-thread-selected (output-field)
1802 "Handler for =thread-selected MI output record.
1804 Sets `gdb-thread-number' to new id."
1805 (let* ((result (gdb-json-string output-field))
1806 (thread-id (gdb-get-field result 'id)))
1807 (gdb-setq-thread-number thread-id)
1808 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1809 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1810 ;; as usually. Things happen to fast and second call (from
1811 ;; gdb-thread-selected handler) gets cut off by our beloved
1812 ;; gdb-pending-triggers.
1813 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1814 ;; body will get executed when `gdb-pending-triggers` is empty.
1815 (gdb-wait-for-pending
1818 (defun gdb-running (output-field)
1819 (let* ((thread-id (gdb-get-field (gdb-json-string output-field) 'thread-id)))
1820 ;; We reset gdb-frame-number to nil if current thread has gone
1821 ;; running. This can't be done in gdb-thread-list-handler-custom
1822 ;; because we need correct gdb-frame-number by the time
1823 ;; -thread-info command is sent.
1824 (when (or (string-equal thread-id "all")
1825 (string-equal thread-id gdb-thread-number))
1826 (setq gdb-frame-number nil)))
1827 (setq gdb-inferior-status "running")
1828 (gdb-force-mode-line-update
1829 (propertize gdb-inferior-status 'face font-lock-type-face))
1830 (when (not gdb-non-stop)
1831 (setq gud-running t))
1832 (setq gdb-active-process t)
1833 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1835 (defun gdb-starting (output-field)
1836 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1838 (list "-data-list-register-names" 'gdb-register-names-handler))
1839 (setq gdb-inferior-status "running")
1840 (gdb-force-mode-line-update
1841 (propertize gdb-inferior-status 'face font-lock-type-face))
1842 (setq gdb-active-process t)
1843 (setq gud-running t)
1844 ;; GDB doesn't seem to respond to -thread-info before first stop or
1845 ;; thread exit (even in non-stop mode), so this is useless.
1846 ;; Behaviour may change in the future.
1847 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1849 ;; -break-insert -t didn't give a reason before gdb 6.9
1851 (defun gdb-stopped (output-field)
1852 "Given the contents of *stopped MI async record, select new
1853 current thread and update GDB buffers."
1854 ;; Reason is available with target-async only
1855 (let* ((result (gdb-json-string output-field))
1856 (reason (gdb-get-field result 'reason))
1857 (thread-id (gdb-get-field result 'thread-id)))
1859 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1860 ;;; because synchronous GDB doesn't give these fields with CLI.
1863 ;;; ;; Extract the frame position from the marker.
1864 ;;; gud-last-frame (cons file
1865 ;;; (string-to-number
1866 ;;; (match-string 6 gud-marker-acc)))))
1868 (setq gdb-inferior-status (or reason "unknown"))
1869 (gdb-force-mode-line-update
1870 (propertize gdb-inferior-status 'face font-lock-warning-face))
1871 (if (string-equal reason "exited-normally")
1872 (setq gdb-active-process nil))
1874 ;; Select new current thread.
1876 ;; Don't switch if we have no reasons selected
1877 (when gdb-switch-reasons
1878 ;; Switch from another stopped thread only if we have
1879 ;; gdb-switch-when-another-stopped:
1880 (when (or gdb-switch-when-another-stopped
1881 (not (string= "stopped"
1882 (gdb-get-field (gdb-current-buffer-thread) 'state))))
1883 ;; Switch if current reason has been selected or we have no
1885 (if (or (eq gdb-switch-reasons t)
1886 (member reason gdb-switch-reasons))
1888 (gdb-setq-thread-number thread-id)
1889 (message (concat "Switched to thread " thread-id)))
1890 (message (format "Thread %s stopped" thread-id)))))
1892 ;; Print "(gdb)" to GUD console
1893 (when gdb-first-done-or-error
1894 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1896 ;; In non-stop, we update information as soon as another thread gets
1898 (when (or gdb-first-done-or-error
1900 ;; In all-stop this updates gud-running properly as well.
1902 (setq gdb-first-done-or-error nil))
1903 (run-hook-with-args 'gdb-stopped-hooks result)))
1905 ;; Remove the trimmings from log stream containing debugging messages
1906 ;; being produced by GDB's internals, use warning face and send to GUD
1908 (defun gdb-internals (output-field)
1909 (setq gdb-filter-output
1912 (let ((error-message
1913 (read output-field)))
1915 0 (length error-message)
1916 'face font-lock-warning-face
1920 ;; Remove the trimmings from the console stream and send to GUD buffer
1921 ;; (frontend MI commands should not print to this stream)
1922 (defun gdb-console (output-field)
1923 (setq gdb-filter-output
1926 (read output-field))))
1928 (defun gdb-done-or-error (output-field token-number type)
1929 (if (string-equal token-number "")
1930 ;; Output from command entered by user
1932 (setq gdb-output-sink 'user)
1933 (setq token-number nil)
1934 ;; MI error - send to minibuffer
1935 (when (eq type 'error)
1936 ;; Skip "msg=" from `output-field'
1937 (message (read (substring output-field 4)))
1938 ;; Don't send to the console twice. (If it is a console error
1939 ;; it is also in the console stream.)
1940 (setq output-field nil)))
1941 ;; Output from command from frontend.
1942 (setq gdb-output-sink 'emacs))
1944 (gdb-clear-partial-output)
1945 (when gdb-first-done-or-error
1946 (unless (or token-number gud-running)
1947 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1949 (setq gdb-first-done-or-error nil))
1951 (setq gdb-filter-output
1952 (gdb-concat-output gdb-filter-output output-field))
1956 (with-current-buffer
1957 (gdb-get-buffer-create 'gdb-partial-output-buffer)
1959 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
1960 (setq gdb-handler-alist
1961 (assq-delete-all token-number gdb-handler-alist)))))
1963 (defun gdb-concat-output (so-far new)
1964 (let ((sink gdb-output-sink))
1966 ((eq sink 'user) (concat so-far new))
1968 (gdb-append-to-partial-output new)
1971 (defun gdb-append-to-partial-output (string)
1972 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1973 (goto-char (point-max))
1976 (defun gdb-clear-partial-output ()
1977 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1980 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
1981 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
1983 Field names are wrapped in double quotes and equal signs are
1984 replaced with semicolons.
1986 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurences from
1987 partial output. This is used to get rid of useless keys in lists
1988 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
1989 -break-info are examples of MI commands which issue such
1992 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
1993 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
1994 -break-info output when it contains breakpoint script field
1995 incompatible with GDB/MI output syntax."
1997 (goto-char (point-min))
2000 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2001 (replace-match "" nil nil nil 1))))
2005 ;; Find positions of braces which enclose broken list
2006 (while (re-search-forward (concat fix-list "={\"") nil t)
2007 (let ((p1 (goto-char (- (point) 2)))
2008 (p2 (progn (forward-sexp)
2010 ;; Replace braces with brackets
2018 (goto-char (point-min))
2020 ;; TODO: This breaks badly with foo= inside constants
2021 (while (re-search-forward "\\([[:alpha:]-_]+\\)=" nil t)
2022 (replace-match "\"\\1\":" nil nil))
2023 (goto-char (point-max))
2026 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2027 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2029 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2030 (gdb-jsonify-buffer fix-key fix-list)
2032 (goto-char (point-min))
2033 (let ((json-array-type 'list))
2036 (defun gdb-json-string (string &optional fix-key fix-list)
2037 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2039 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2042 (gdb-json-read-buffer fix-key fix-list)))
2044 (defun gdb-json-partial-output (&optional fix-key fix-list)
2045 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2047 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2048 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2049 (gdb-json-read-buffer fix-key fix-list)))
2051 (defmacro gdb-mark-line (line variable)
2052 "Set VARIABLE marker to point at beginning of LINE.
2054 If current window has no fringes, inverse colors on LINE.
2056 Return position where LINE begins."
2058 (let* ((offset (1+ (- ,line (line-number-at-pos))))
2059 (start-posn (line-beginning-position offset))
2060 (end-posn (line-end-position offset)))
2061 (set-marker ,variable (copy-marker start-posn))
2062 (when (not (> (car (window-fringes)) 0))
2063 (put-text-property start-posn end-posn
2064 'font-lock-face '(:inverse-video t)))
2067 (defun gdb-pad-string (string padding)
2068 (format (concat "%" (number-to-string padding) "s") string))
2070 ;; gdb-table struct is a way to programmatically construct simple
2071 ;; tables. It help to reliably align columns of data in GDB buffers
2077 (row-properties nil)
2080 (defun gdb-table-add-row (table row &optional properties)
2081 "Add ROW of string to TABLE and recalculate column sizes.
2083 When non-nil, PROPERTIES will be added to the whole row when
2084 calling `gdb-table-string'."
2085 (let ((rows (gdb-table-rows table))
2086 (row-properties (gdb-table-row-properties table))
2087 (column-sizes (gdb-table-column-sizes table))
2088 (right-align (gdb-table-right-align table)))
2089 (when (not column-sizes)
2090 (setf (gdb-table-column-sizes table)
2091 (make-list (length row) 0)))
2092 (setf (gdb-table-rows table)
2093 (append rows (list row)))
2094 (setf (gdb-table-row-properties table)
2095 (append row-properties (list properties)))
2096 (setf (gdb-table-column-sizes table)
2097 (mapcar* (lambda (x s)
2099 (max (abs x) (string-width (or s "")))))
2100 (if right-align new-x (- new-x))))
2101 (gdb-table-column-sizes table)
2103 ;; Avoid trailing whitespace at eol
2104 (if (not (gdb-table-right-align table))
2105 (setcar (last (gdb-table-column-sizes table)) 0))))
2107 (defun gdb-table-string (table &optional sep)
2108 "Return TABLE as a string with columns separated with SEP."
2109 (let ((column-sizes (gdb-table-column-sizes table))
2114 (lambda (row properties)
2116 (mapconcat 'identity
2117 (mapcar* (lambda (s x) (gdb-pad-string s x))
2121 (gdb-table-rows table)
2122 (gdb-table-row-properties table))
2125 ;; gdb-get-field goes deep, gdb-get-many-fields goes wide
2126 (defalias 'gdb-get-field 'bindat-get-field)
2128 (defun gdb-get-many-fields (struct &rest fields)
2129 "Return a list of FIELDS values from STRUCT."
2131 (dolist (field fields values)
2132 (setq values (append values (list (gdb-get-field struct field)))))))
2134 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2136 &optional signal-list)
2137 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2138 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2139 buffer with `gdb-bind-function-to-buffer'.
2141 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2142 defined trigger is called with an argument from SIGNAL-LIST. It's
2143 not recommended to define triggers with empty SIGNAL-LIST.
2144 Normally triggers should respond at least to 'update signal.
2146 Normally the trigger defined by this command must be called from
2147 the buffer where HANDLER-NAME must work. This should be done so
2148 that buffer-local thread number may be used in GDB-COMMAND (by
2149 calling `gdb-current-context-command').
2150 `gdb-bind-function-to-buffer' is used to achieve this, see
2151 `gdb-get-buffer-create'.
2153 Triggers defined by this command are meant to be used as a
2154 trigger argument when describing buffer types with
2155 `gdb-set-buffer-rules'."
2156 `(defun ,trigger-name (&optional signal)
2158 (or (not ,signal-list)
2159 (memq signal ,signal-list))
2160 (when (not (gdb-pending-p
2161 (cons (current-buffer) ',trigger-name)))
2164 (gdb-bind-function-to-buffer ',handler-name (current-buffer))))
2165 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2167 ;; Used by disassembly buffer only, the rest use
2168 ;; def-gdb-trigger-and-handler
2169 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2170 &optional nopreserve)
2171 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2173 Handlers are normally called from the buffers they put output in.
2175 Delete ((current-buffer) . TRIGGER-NAME) from
2176 `gdb-pending-triggers', erase current buffer and evaluate
2177 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2179 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2180 `(defun ,handler-name ()
2181 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2182 (let* ((buffer-read-only nil)
2183 (window (get-buffer-window (current-buffer) 0))
2184 (start (window-start window))
2185 (p (window-point window)))
2188 (gdb-update-buffer-name)
2189 ,(when (not nopreserve)
2190 '(set-window-start window start)
2191 '(set-window-point window p)))))
2193 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2194 handler-name custom-defun
2195 &optional signal-list)
2196 "Define trigger and handler.
2198 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2199 `def-gdb-auto-update-trigger'.
2201 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2202 `def-gdb-auto-update-handler'."
2204 (def-gdb-auto-update-trigger ,trigger-name
2206 ,handler-name ,signal-list)
2207 (def-gdb-auto-update-handler ,handler-name
2208 ,trigger-name ,custom-defun)))
2212 ;; Breakpoint buffer : This displays the output of `-break-list'.
2213 (def-gdb-trigger-and-handler
2214 gdb-invalidate-breakpoints "-break-list"
2215 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2218 (gdb-set-buffer-rules
2219 'gdb-breakpoints-buffer
2220 'gdb-breakpoints-buffer-name
2221 'gdb-breakpoints-mode
2222 'gdb-invalidate-breakpoints)
2224 (defun gdb-breakpoints-list-handler-custom ()
2225 (let ((breakpoints-list (gdb-get-field
2226 (gdb-json-partial-output "bkpt" "script")
2227 'BreakpointTable 'body))
2228 (table (make-gdb-table)))
2229 (setq gdb-breakpoints-list nil)
2230 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Hits" "Addr" "What"))
2231 (dolist (breakpoint breakpoints-list)
2232 (add-to-list 'gdb-breakpoints-list
2233 (cons (gdb-get-field breakpoint 'number)
2235 (let ((at (gdb-get-field breakpoint 'at))
2236 (pending (gdb-get-field breakpoint 'pending))
2237 (func (gdb-get-field breakpoint 'func)))
2238 (gdb-table-add-row table
2240 (gdb-get-field breakpoint 'number)
2241 (gdb-get-field breakpoint 'type)
2242 (gdb-get-field breakpoint 'disp)
2243 (let ((flag (gdb-get-field breakpoint 'enabled)))
2244 (if (string-equal flag "y")
2245 (propertize "y" 'font-lock-face font-lock-warning-face)
2246 (propertize "n" 'font-lock-face font-lock-comment-face)))
2247 (gdb-get-field breakpoint 'times)
2248 (gdb-get-field breakpoint 'addr)
2251 (propertize func 'font-lock-face font-lock-function-name-face)
2252 (gdb-frame-location breakpoint))))
2253 ;; Add clickable properties only for breakpoints with file:line
2255 (append (list 'gdb-breakpoint breakpoint)
2256 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2257 mouse-face highlight))))))
2258 (insert (gdb-table-string table " "))
2259 (gdb-place-breakpoints)))
2261 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2262 (defun gdb-place-breakpoints ()
2263 (let ((flag) (bptno))
2264 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2265 (dolist (buffer (buffer-list))
2266 (with-current-buffer buffer
2267 (if (and (eq gud-minor-mode 'gdbmi)
2268 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2269 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2270 (dolist (breakpoint gdb-breakpoints-list)
2271 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2272 ; an associative list
2273 (line (gdb-get-field breakpoint 'line)))
2275 (let ((file (gdb-get-field breakpoint 'fullname))
2276 (flag (gdb-get-field breakpoint 'enabled))
2277 (bptno (gdb-get-field breakpoint 'number)))
2278 (unless (file-exists-p file)
2279 (setq file (cdr (assoc bptno gdb-location-alist))))
2281 (not (string-equal file "File not found")))
2282 (with-current-buffer
2283 (find-file-noselect file 'nowarn)
2285 ;; Only want one breakpoint icon at each location.
2287 (goto-line (string-to-number line))
2288 (gdb-put-breakpoint-icon (string-equal flag "y") bptno)))
2290 (list (concat "list " file ":1")
2293 (list "-file-list-exec-source-file"
2294 `(lambda () (gdb-get-location
2295 ,bptno ,line ,flag)))))))))))
2297 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2299 (defun gdb-get-location (bptno line flag)
2300 "Find the directory containing the relevant source file.
2301 Put in buffer and place breakpoint icon."
2302 (goto-char (point-min))
2303 (catch 'file-not-found
2304 (if (re-search-forward gdb-source-file-regexp nil t)
2305 (delete (cons bptno "File not found") gdb-location-alist)
2306 (push (cons bptno (match-string 1)) gdb-location-alist)
2308 (unless (assoc bptno gdb-location-alist)
2309 (push (cons bptno "File not found") gdb-location-alist)
2310 (message-box "Cannot find source file for breakpoint location.
2311 Add directory to search path for source files using the GDB command, dir."))
2312 (throw 'file-not-found nil))
2313 (with-current-buffer (find-file-noselect (match-string 1))
2315 ;; only want one breakpoint icon at each location
2317 (goto-line (string-to-number line))
2318 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2320 (add-hook 'find-file-hook 'gdb-find-file-hook)
2322 (defun gdb-find-file-hook ()
2323 "Set up buffer for debugging if file is part of the source code
2324 of the current session."
2325 (if (and (buffer-name gud-comint-buffer)
2326 ;; in case gud or gdb-ui is just loaded
2328 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2330 (if (member buffer-file-name gdb-source-file-list)
2331 (with-current-buffer (find-buffer-visiting buffer-file-name)
2332 (gdb-init-buffer)))))
2334 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2335 (declare-function gud-break "gdb-mi" t t) ; gud-def
2336 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2338 (defun gdb-mouse-set-clear-breakpoint (event)
2339 "Set/clear breakpoint in left fringe/margin at mouse click.
2340 If not in a source or disassembly buffer just set point."
2342 (mouse-minibuffer-check event)
2343 (let ((posn (event-end event)))
2344 (with-selected-window (posn-window posn)
2345 (if (or (buffer-file-name) (eq major-mode 'gdb-disassembly-mode))
2346 (if (numberp (posn-point posn))
2348 (goto-char (posn-point posn))
2349 (if (or (posn-object posn)
2350 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2354 (posn-set-point posn))))
2356 (defun gdb-mouse-toggle-breakpoint-margin (event)
2357 "Enable/disable breakpoint in left margin with mouse click."
2359 (mouse-minibuffer-check event)
2360 (let ((posn (event-end event)))
2361 (if (numberp (posn-point posn))
2362 (with-selected-window (posn-window posn)
2364 (goto-char (posn-point posn))
2365 (if (posn-object posn)
2367 (let ((bptno (get-text-property
2368 0 'gdb-bptno (car (posn-string posn)))))
2370 (if (get-text-property
2371 0 'gdb-enabled (car (posn-string posn)))
2376 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2377 "Enable/disable breakpoint in left fringe with mouse click."
2379 (mouse-minibuffer-check event)
2380 (let* ((posn (event-end event))
2381 (pos (posn-point posn))
2384 (with-selected-window (posn-window posn)
2386 (set-buffer (window-buffer (selected-window)))
2388 (dolist (overlay (overlays-in pos pos))
2389 (when (overlay-get overlay 'put-break)
2390 (setq obj (overlay-get overlay 'before-string))))
2394 (if (get-text-property 0 'gdb-enabled obj)
2397 (get-text-property 0 'gdb-bptno obj)))))))))
2399 (defun gdb-breakpoints-buffer-name ()
2400 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2402 (def-gdb-display-buffer
2403 gdb-display-breakpoints-buffer
2404 'gdb-breakpoints-buffer
2405 "Display status of user-settable breakpoints.")
2407 (def-gdb-frame-for-buffer
2408 gdb-frame-breakpoints-buffer
2409 'gdb-breakpoints-buffer
2410 "Display status of user-settable breakpoints in a new frame.")
2412 (defvar gdb-breakpoints-mode-map
2413 (let ((map (make-sparse-keymap))
2414 (menu (make-sparse-keymap "Breakpoints")))
2415 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2416 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2417 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2418 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2419 (suppress-keymap map)
2420 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2421 (define-key map " " 'gdb-toggle-breakpoint)
2422 (define-key map "D" 'gdb-delete-breakpoint)
2423 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2424 (define-key map "q" 'gdb-delete-frame-or-window)
2425 (define-key map "\r" 'gdb-goto-breakpoint)
2426 (define-key map "\t" '(lambda ()
2428 (gdb-set-window-buffer
2429 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2430 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2431 (define-key map [follow-link] 'mouse-face)
2434 (defun gdb-delete-frame-or-window ()
2435 "Delete frame if there is only one window. Otherwise delete the window."
2437 (if (one-window-p) (delete-frame)
2440 ;;from make-mode-line-mouse-map
2441 (defun gdb-make-header-line-mouse-map (mouse function) "\
2442 Return a keymap with single entry for mouse key MOUSE on the header line.
2443 MOUSE is defined to run function FUNCTION with no args in the buffer
2444 corresponding to the mode line clicked."
2445 (let ((map (make-sparse-keymap)))
2446 (define-key map (vector 'header-line mouse) function)
2447 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2450 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2452 'help-echo ,help-echo
2453 'mouse-face ',mouse-face
2456 (gdb-make-header-line-mouse-map
2458 (lambda (event) (interactive "e")
2459 (save-selected-window
2460 (select-window (posn-window (event-start event)))
2461 (gdb-set-window-buffer
2462 (gdb-get-buffer-create ',buffer) t) )))))
2465 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2468 (defun gdb-threads-buffer-name ()
2469 (concat "*threads of " (gdb-get-target-string) "*"))
2471 (def-gdb-display-buffer
2472 gdb-display-threads-buffer
2474 "Display GDB threads.")
2476 (def-gdb-frame-for-buffer
2477 gdb-frame-threads-buffer
2479 "Display GDB threads in a new frame.")
2481 (def-gdb-trigger-and-handler
2482 gdb-invalidate-threads (gdb-current-context-command "-thread-info" gud-running)
2483 gdb-thread-list-handler gdb-thread-list-handler-custom
2484 '(update update-threads))
2486 (gdb-set-buffer-rules
2488 'gdb-threads-buffer-name
2490 'gdb-invalidate-threads)
2492 (defvar gdb-threads-font-lock-keywords
2493 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2494 (" \\(stopped\\)" (1 font-lock-warning-face))
2495 (" \\(running\\)" (1 font-lock-string-face))
2496 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2497 "Font lock keywords used in `gdb-threads-mode'.")
2499 (defvar gdb-threads-mode-map
2500 (let ((map (make-sparse-keymap)))
2501 (define-key map "\r" 'gdb-select-thread)
2502 (define-key map "f" 'gdb-display-stack-for-thread)
2503 (define-key map "F" 'gdb-frame-stack-for-thread)
2504 (define-key map "l" 'gdb-display-locals-for-thread)
2505 (define-key map "L" 'gdb-frame-locals-for-thread)
2506 (define-key map "r" 'gdb-display-registers-for-thread)
2507 (define-key map "R" 'gdb-frame-registers-for-thread)
2508 (define-key map "d" 'gdb-display-disassembly-for-thread)
2509 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2510 (define-key map "i" 'gdb-interrupt-thread)
2511 (define-key map "c" 'gdb-continue-thread)
2512 (define-key map "s" 'gdb-step-thread)
2513 (define-key map "\t" '(lambda ()
2515 (gdb-set-window-buffer
2516 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2517 (define-key map [mouse-2] 'gdb-select-thread)
2518 (define-key map [follow-link] 'mouse-face)
2521 (defvar gdb-threads-header
2523 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
2524 "mouse-1: select" mode-line-highlight mode-line-inactive)
2526 (gdb-propertize-header "Threads" gdb-threads-buffer
2527 nil nil mode-line)))
2529 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2530 "Major mode for GDB threads.
2532 \\{gdb-threads-mode-map}"
2533 (setq gdb-thread-position (make-marker))
2534 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2535 (setq header-line-format gdb-threads-header)
2536 (set (make-local-variable 'font-lock-defaults)
2537 '(gdb-threads-font-lock-keywords))
2538 (run-mode-hooks 'gdb-threads-mode-hook)
2539 'gdb-invalidate-threads)
2541 (defun gdb-thread-list-handler-custom ()
2542 (let ((threads-list (gdb-get-field (gdb-json-partial-output) 'threads))
2543 (table (make-gdb-table))
2545 (setq gdb-threads-list nil)
2546 (setq gdb-running-threads-count 0)
2547 (setq gdb-stopped-threads-count 0)
2548 (set-marker gdb-thread-position nil)
2550 (dolist (thread (reverse threads-list))
2551 (let ((running (string-equal (gdb-get-field thread 'state) "running")))
2552 (add-to-list 'gdb-threads-list
2553 (cons (gdb-get-field thread 'id)
2556 (incf gdb-running-threads-count)
2557 (incf gdb-stopped-threads-count))
2559 (gdb-table-add-row table
2561 (gdb-get-field thread 'id)
2563 (if gdb-thread-buffer-verbose-names
2564 (concat (gdb-get-field thread 'target-id) " ") "")
2565 (gdb-get-field thread 'state)
2566 ;; Include frame information for stopped threads
2569 " in " (gdb-get-field thread 'frame 'func)
2570 (if gdb-thread-buffer-arguments
2573 (let ((args (gdb-get-field thread 'frame 'args)))
2576 (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value))))
2580 (if gdb-thread-buffer-locations
2581 (gdb-frame-location (gdb-get-field thread 'frame)) "")
2582 (if gdb-thread-buffer-addresses
2583 (concat " at " (gdb-get-field thread 'frame 'addr)) ""))
2587 'mouse-face 'highlight
2588 'help-echo "mouse-2, RET: select thread")))
2589 (when (string-equal gdb-thread-number
2590 (gdb-get-field thread 'id))
2591 (setq marked-line (length gdb-threads-list))))
2592 (insert (gdb-table-string table " "))
2594 (gdb-mark-line marked-line gdb-thread-position)))
2595 ;; We update gud-running here because we need to make sure that
2596 ;; gdb-threads-list is up-to-date
2597 (gdb-update-gud-running)
2598 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2600 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2601 "Define a NAME command which will act upon thread on the current line.
2603 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2604 be the value of 'gdb-thread property of the current line. If
2605 'gdb-thread is nil, error is signaled."
2606 `(defun ,name (&optional event)
2609 (if event (posn-set-point (event-end event)))
2612 (let ((thread (get-text-property (point) 'gdb-thread)))
2615 (error "Not recognized as thread line"))))))
2617 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command &optional doc)
2618 "Define a NAME which will call BUFFER-COMMAND with id of thread
2619 on the current line."
2620 `(def-gdb-thread-buffer-command ,name
2621 (,buffer-command (gdb-get-field thread 'id))
2624 (def-gdb-thread-buffer-command gdb-select-thread
2625 (let ((new-id (gdb-get-field thread 'id)))
2626 (gdb-setq-thread-number new-id)
2627 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2629 "Select the thread at current line of threads buffer.")
2631 (def-gdb-thread-buffer-simple-command
2632 gdb-display-stack-for-thread
2633 gdb-preemptively-display-stack-buffer
2634 "Display stack buffer for the thread at current line.")
2636 (def-gdb-thread-buffer-simple-command
2637 gdb-display-locals-for-thread
2638 gdb-preemptively-display-locals-buffer
2639 "Display locals buffer for the thread at current line.")
2641 (def-gdb-thread-buffer-simple-command
2642 gdb-display-registers-for-thread
2643 gdb-preemptively-display-registers-buffer
2644 "Display registers buffer for the thread at current line.")
2646 (def-gdb-thread-buffer-simple-command
2647 gdb-display-disassembly-for-thread
2648 gdb-preemptively-display-disassembly-buffer
2649 "Display disassembly buffer for the thread at current line.")
2651 (def-gdb-thread-buffer-simple-command
2652 gdb-frame-stack-for-thread
2653 gdb-frame-stack-buffer
2654 "Display a new frame with stack buffer for the thread at
2657 (def-gdb-thread-buffer-simple-command
2658 gdb-frame-locals-for-thread
2659 gdb-frame-locals-buffer
2660 "Display a new frame with locals buffer for the thread at
2663 (def-gdb-thread-buffer-simple-command
2664 gdb-frame-registers-for-thread
2665 gdb-frame-registers-buffer
2666 "Display a new frame with registers buffer for the thread at
2669 (def-gdb-thread-buffer-simple-command
2670 gdb-frame-disassembly-for-thread
2671 gdb-frame-disassembly-buffer
2672 "Display a new frame with disassembly buffer for the thread at
2675 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2676 "Define a NAME which will execute GUD-COMMAND with
2677 `gdb-thread-number' locally bound to id of thread on the current
2679 `(def-gdb-thread-buffer-command ,name
2681 (let ((gdb-thread-number (gdb-get-field thread 'id))
2682 (gdb-gud-control-all-threads nil))
2683 (call-interactively #',gud-command))
2684 (error "Available in non-stop mode only, customize gdb-non-stop-setting."))
2687 (def-gdb-thread-buffer-gud-command
2688 gdb-interrupt-thread
2690 "Interrupt thread at current line.")
2692 (def-gdb-thread-buffer-gud-command
2695 "Continue thread at current line.")
2697 (def-gdb-thread-buffer-gud-command
2700 "Step thread at current line.")
2705 (defcustom gdb-memory-rows 8
2706 "Number of data rows in memory window."
2711 (defcustom gdb-memory-columns 4
2712 "Number of data columns in memory window."
2717 (defcustom gdb-memory-format "x"
2718 "Display format of data items in memory window."
2719 :type '(choice (const :tag "Hexadecimal" "x")
2720 (const :tag "Signed decimal" "d")
2721 (const :tag "Unsigned decimal" "u")
2722 (const :tag "Octal" "o")
2723 (const :tag "Binary" "t"))
2727 (defcustom gdb-memory-unit 4
2728 "Unit size of data items in memory window."
2729 :type '(choice (const :tag "Byte" 1)
2730 (const :tag "Halfword" 2)
2731 (const :tag "Word" 4)
2732 (const :tag "Giant word" 8))
2736 (def-gdb-trigger-and-handler
2737 gdb-invalidate-memory
2738 (format "-data-read-memory %s %s %d %d %d"
2744 gdb-read-memory-handler
2745 gdb-read-memory-custom
2748 (gdb-set-buffer-rules
2750 'gdb-memory-buffer-name
2752 'gdb-invalidate-memory)
2754 (defun gdb-memory-column-width (size format)
2755 "Return length of string with memory unit of SIZE in FORMAT.
2757 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2758 in `gdb-memory-format'."
2759 (let ((format-base (cdr (assoc format
2761 ("d" . 10) ("u" . 10)
2765 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2766 (cond ((string-equal format "x")
2767 (+ 2 res)) ; hexadecimal numbers have 0x in front
2768 ((or (string-equal format "d")
2769 (string-equal format "o"))
2772 (error "Unknown format"))))
2774 (defun gdb-read-memory-custom ()
2775 (let* ((res (gdb-json-partial-output))
2776 (err-msg (gdb-get-field res 'msg)))
2778 (let ((memory (gdb-get-field res 'memory)))
2779 (setq gdb-memory-address (gdb-get-field res 'addr))
2780 (setq gdb-memory-next-page (gdb-get-field res 'next-page))
2781 (setq gdb-memory-prev-page (gdb-get-field res 'prev-page))
2782 (setq gdb-memory-last-address gdb-memory-address)
2783 (dolist (row memory)
2784 (insert (concat (gdb-get-field row 'addr) ":"))
2785 (dolist (column (gdb-get-field row 'data))
2786 (insert (gdb-pad-string column
2787 (+ 2 (gdb-memory-column-width
2789 gdb-memory-format)))))
2791 ;; Show last page instead of empty buffer when out of bounds
2793 (let ((gdb-memory-address gdb-memory-last-address))
2794 (gdb-invalidate-memory 'update)
2795 (error err-msg))))))
2797 (defvar gdb-memory-mode-map
2798 (let ((map (make-sparse-keymap)))
2799 (suppress-keymap map t)
2800 (define-key map "q" 'kill-this-buffer)
2801 (define-key map "n" 'gdb-memory-show-next-page)
2802 (define-key map "p" 'gdb-memory-show-previous-page)
2803 (define-key map "a" 'gdb-memory-set-address)
2804 (define-key map "t" 'gdb-memory-format-binary)
2805 (define-key map "o" 'gdb-memory-format-octal)
2806 (define-key map "u" 'gdb-memory-format-unsigned)
2807 (define-key map "d" 'gdb-memory-format-signed)
2808 (define-key map "x" 'gdb-memory-format-hexadecimal)
2809 (define-key map "b" 'gdb-memory-unit-byte)
2810 (define-key map "h" 'gdb-memory-unit-halfword)
2811 (define-key map "w" 'gdb-memory-unit-word)
2812 (define-key map "g" 'gdb-memory-unit-giant)
2813 (define-key map "R" 'gdb-memory-set-rows)
2814 (define-key map "C" 'gdb-memory-set-columns)
2817 (defun gdb-memory-set-address-event (event)
2818 "Handle a click on address field in memory buffer header."
2820 (save-selected-window
2821 (select-window (posn-window (event-start event)))
2822 (gdb-memory-set-address)))
2824 ;; Non-event version for use within keymap
2825 (defun gdb-memory-set-address ()
2826 "Set the start memory address."
2828 (let ((arg (read-from-minibuffer "Memory address: ")))
2829 (setq gdb-memory-address arg))
2830 (gdb-invalidate-memory 'update))
2832 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2833 "Define a function NAME which reads new VAR value from minibuffer."
2834 `(defun ,name (event)
2837 (save-selected-window
2838 (select-window (posn-window (event-start event)))
2839 (let* ((arg (read-from-minibuffer ,echo-string))
2840 (count (string-to-number arg)))
2842 (error "Positive number only")
2843 (customize-set-variable ',variable count)
2844 (gdb-invalidate-memory 'update))))))
2846 (def-gdb-set-positive-number
2850 "Set the number of data rows in memory window.")
2852 (def-gdb-set-positive-number
2853 gdb-memory-set-columns
2856 "Set the number of data columns in memory window.")
2858 (defmacro def-gdb-memory-format (name format doc)
2859 "Define a function NAME to switch memory buffer to use FORMAT.
2861 DOC is an optional documentation string."
2862 `(defun ,name () ,(when doc doc)
2864 (customize-set-variable 'gdb-memory-format ,format)
2865 (gdb-invalidate-memory 'update)))
2867 (def-gdb-memory-format
2868 gdb-memory-format-binary "t"
2869 "Set the display format to binary.")
2871 (def-gdb-memory-format
2872 gdb-memory-format-octal "o"
2873 "Set the display format to octal.")
2875 (def-gdb-memory-format
2876 gdb-memory-format-unsigned "u"
2877 "Set the display format to unsigned decimal.")
2879 (def-gdb-memory-format
2880 gdb-memory-format-signed "d"
2881 "Set the display format to decimal.")
2883 (def-gdb-memory-format
2884 gdb-memory-format-hexadecimal "x"
2885 "Set the display format to hexadecimal.")
2887 (defvar gdb-memory-format-map
2888 (let ((map (make-sparse-keymap)))
2889 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2891 "Keymap to select format in the header line.")
2893 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2894 "Menu of display formats in the header line.")
2896 (define-key gdb-memory-format-menu [binary]
2897 '(menu-item "Binary" gdb-memory-format-binary
2898 :button (:radio . (equal gdb-memory-format "t"))))
2899 (define-key gdb-memory-format-menu [octal]
2900 '(menu-item "Octal" gdb-memory-format-octal
2901 :button (:radio . (equal gdb-memory-format "o"))))
2902 (define-key gdb-memory-format-menu [unsigned]
2903 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2904 :button (:radio . (equal gdb-memory-format "u"))))
2905 (define-key gdb-memory-format-menu [signed]
2906 '(menu-item "Signed Decimal" gdb-memory-format-signed
2907 :button (:radio . (equal gdb-memory-format "d"))))
2908 (define-key gdb-memory-format-menu [hexadecimal]
2909 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2910 :button (:radio . (equal gdb-memory-format "x"))))
2912 (defun gdb-memory-format-menu (event)
2914 (x-popup-menu event gdb-memory-format-menu))
2916 (defun gdb-memory-format-menu-1 (event)
2918 (save-selected-window
2919 (select-window (posn-window (event-start event)))
2920 (let* ((selection (gdb-memory-format-menu event))
2921 (binding (and selection (lookup-key gdb-memory-format-menu
2922 (vector (car selection))))))
2923 (if binding (call-interactively binding)))))
2925 (defmacro def-gdb-memory-unit (name unit-size doc)
2926 "Define a function NAME to switch memory unit size to UNIT-SIZE.
2928 DOC is an optional documentation string."
2929 `(defun ,name () ,(when doc doc)
2931 (customize-set-variable 'gdb-memory-unit ,unit-size)
2932 (gdb-invalidate-memory 'update)))
2934 (def-gdb-memory-unit gdb-memory-unit-giant 8
2935 "Set the unit size to giant words (eight bytes).")
2937 (def-gdb-memory-unit gdb-memory-unit-word 4
2938 "Set the unit size to words (four bytes).")
2940 (def-gdb-memory-unit gdb-memory-unit-halfword 2
2941 "Set the unit size to halfwords (two bytes).")
2943 (def-gdb-memory-unit gdb-memory-unit-byte 1
2944 "Set the unit size to bytes.")
2946 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
2947 "Define a function NAME which show new address in memory buffer.
2949 The defined function switches Memory buffer to show address
2950 stored in ADDRESS-VAR variable.
2952 DOC is an optional documentation string."
2956 (let ((gdb-memory-address ,address-var))
2957 (gdb-invalidate-memory))))
2959 (def-gdb-memory-show-page gdb-memory-show-previous-page
2960 gdb-memory-prev-page)
2962 (def-gdb-memory-show-page gdb-memory-show-next-page
2963 gdb-memory-next-page)
2965 (defvar gdb-memory-unit-map
2966 (let ((map (make-sparse-keymap)))
2967 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2969 "Keymap to select units in the header line.")
2971 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2972 "Menu of units in the header line.")
2974 (define-key gdb-memory-unit-menu [giantwords]
2975 '(menu-item "Giant words" gdb-memory-unit-giant
2976 :button (:radio . (equal gdb-memory-unit 8))))
2977 (define-key gdb-memory-unit-menu [words]
2978 '(menu-item "Words" gdb-memory-unit-word
2979 :button (:radio . (equal gdb-memory-unit 4))))
2980 (define-key gdb-memory-unit-menu [halfwords]
2981 '(menu-item "Halfwords" gdb-memory-unit-halfword
2982 :button (:radio . (equal gdb-memory-unit 2))))
2983 (define-key gdb-memory-unit-menu [bytes]
2984 '(menu-item "Bytes" gdb-memory-unit-byte
2985 :button (:radio . (equal gdb-memory-unit 1))))
2987 (defun gdb-memory-unit-menu (event)
2989 (x-popup-menu event gdb-memory-unit-menu))
2991 (defun gdb-memory-unit-menu-1 (event)
2993 (save-selected-window
2994 (select-window (posn-window (event-start event)))
2995 (let* ((selection (gdb-memory-unit-menu event))
2996 (binding (and selection (lookup-key gdb-memory-unit-menu
2997 (vector (car selection))))))
2998 (if binding (call-interactively binding)))))
3000 (defvar gdb-memory-font-lock-keywords
3001 '(;; <__function.name+n>
3002 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
3004 "Font lock keywords used in `gdb-memory-mode'.")
3006 (defvar gdb-memory-header
3011 'face font-lock-warning-face
3012 'help-echo "mouse-1: decrement address"
3013 'mouse-face 'mode-line-highlight
3014 'local-map (gdb-make-header-line-mouse-map
3016 #'gdb-memory-show-previous-page))
3019 'face font-lock-warning-face
3020 'help-echo "mouse-1: increment address"
3021 'mouse-face 'mode-line-highlight
3022 'local-map (gdb-make-header-line-mouse-map
3024 #'gdb-memory-show-next-page))
3026 (propertize gdb-memory-address
3027 'face font-lock-warning-face
3028 'help-echo "mouse-1: set start address"
3029 'mouse-face 'mode-line-highlight
3030 'local-map (gdb-make-header-line-mouse-map
3032 #'gdb-memory-set-address-event))
3034 (propertize (number-to-string gdb-memory-rows)
3035 'face font-lock-warning-face
3036 'help-echo "mouse-1: set number of columns"
3037 'mouse-face 'mode-line-highlight
3038 'local-map (gdb-make-header-line-mouse-map
3040 #'gdb-memory-set-rows))
3042 (propertize (number-to-string gdb-memory-columns)
3043 'face font-lock-warning-face
3044 'help-echo "mouse-1: set number of columns"
3045 'mouse-face 'mode-line-highlight
3046 'local-map (gdb-make-header-line-mouse-map
3048 #'gdb-memory-set-columns))
3050 (propertize gdb-memory-format
3051 'face font-lock-warning-face
3052 'help-echo "mouse-3: select display format"
3053 'mouse-face 'mode-line-highlight
3054 'local-map gdb-memory-format-map)
3056 (propertize (number-to-string gdb-memory-unit)
3057 'face font-lock-warning-face
3058 'help-echo "mouse-3: select unit size"
3059 'mouse-face 'mode-line-highlight
3060 'local-map gdb-memory-unit-map)))
3061 "Header line used in `gdb-memory-mode'.")
3063 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3064 "Major mode for examining memory.
3066 \\{gdb-memory-mode-map}"
3067 (setq header-line-format gdb-memory-header)
3068 (set (make-local-variable 'font-lock-defaults)
3069 '(gdb-memory-font-lock-keywords))
3070 (run-mode-hooks 'gdb-memory-mode-hook)
3071 'gdb-invalidate-memory)
3073 (defun gdb-memory-buffer-name ()
3074 (concat "*memory of " (gdb-get-target-string) "*"))
3076 (def-gdb-display-buffer
3077 gdb-display-memory-buffer
3079 "Display memory contents.")
3081 (defun gdb-frame-memory-buffer ()
3082 "Display memory contents in a new frame."
3084 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3085 (special-display-frame-alist
3089 ,@gdb-frame-parameters)))
3090 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3093 ;;; Disassembly view
3095 (defun gdb-disassembly-buffer-name ()
3096 (gdb-current-context-buffer-name
3097 (concat "disassembly of " (gdb-get-target-string))))
3099 (def-gdb-display-buffer
3100 gdb-display-disassembly-buffer
3101 'gdb-disassembly-buffer
3102 "Display disassembly for current stack frame.")
3104 (def-gdb-preempt-display-buffer
3105 gdb-preemptively-display-disassembly-buffer
3106 'gdb-disassembly-buffer)
3108 (def-gdb-frame-for-buffer
3109 gdb-frame-disassembly-buffer
3110 'gdb-disassembly-buffer
3111 "Display disassembly in a new frame.")
3113 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3114 (let* ((frame (gdb-current-buffer-frame))
3115 (file (gdb-get-field frame 'fullname))
3116 (line (gdb-get-field frame 'line)))
3118 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3119 gdb-disassembly-handler
3120 ;; We update disassembly only after we have actual frame information
3121 ;; about all threads
3122 '(update update-disassembly))
3124 (def-gdb-auto-update-handler
3125 gdb-disassembly-handler
3126 gdb-invalidate-disassembly
3127 gdb-disassembly-handler-custom
3130 (gdb-set-buffer-rules
3131 'gdb-disassembly-buffer
3132 'gdb-disassembly-buffer-name
3133 'gdb-disassembly-mode
3134 'gdb-invalidate-disassembly)
3136 (defvar gdb-disassembly-font-lock-keywords
3137 '(;; <__function.name+n>
3138 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3139 (1 font-lock-function-name-face))
3140 ;; 0xNNNNNNNN <__function.name+n>: opcode
3141 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3142 (4 font-lock-keyword-face))
3143 ;; %register(at least i386)
3144 ("%\\sw+" . font-lock-variable-name-face)
3145 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3146 (1 font-lock-comment-face)
3147 (2 font-lock-function-name-face))
3148 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3149 "Font lock keywords used in `gdb-disassembly-mode'.")
3151 (defvar gdb-disassembly-mode-map
3153 (let ((map (make-sparse-keymap)))
3154 (suppress-keymap map)
3155 (define-key map "q" 'kill-this-buffer)
3158 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3159 "Major mode for GDB disassembly information.
3161 \\{gdb-disassembly-mode-map}"
3162 ;; TODO Rename overlay variable for disassembly mode
3163 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3164 (setq fringes-outside-margins t)
3165 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3166 (set (make-local-variable 'font-lock-defaults)
3167 '(gdb-disassembly-font-lock-keywords))
3168 (run-mode-hooks 'gdb-disassembly-mode-hook)
3169 'gdb-invalidate-disassembly)
3171 (defun gdb-disassembly-handler-custom ()
3172 (let* ((instructions (gdb-get-field (gdb-json-partial-output) 'asm_insns))
3173 (address (gdb-get-field (gdb-current-buffer-frame) 'addr))
3175 (table (make-gdb-table))
3177 (dolist (instr instructions)
3178 (gdb-table-add-row table
3180 (gdb-get-field instr 'address)
3181 (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
3182 (gdb-get-field instr 'inst)))
3183 (when (string-equal (gdb-get-field instr 'address)
3186 (setq marked-line (length (gdb-table-rows table)))
3187 (setq fringe-indicator-alist
3188 (if (string-equal gdb-frame-number "0")
3190 '((overlay-arrow . hollow-right-triangle)))))))
3191 (insert (gdb-table-string table " "))
3192 (gdb-disassembly-place-breakpoints)
3193 ;; Mark current position with overlay arrow and scroll window to
3196 (let ((window (get-buffer-window (current-buffer) 0)))
3197 (set-window-point window (gdb-mark-line marked-line gdb-disassembly-position))))
3199 (gdb-current-context-mode-name
3200 (concat "Disassembly: "
3201 (gdb-get-field (gdb-current-buffer-frame) 'func))))))
3203 (defun gdb-disassembly-place-breakpoints ()
3204 (gdb-remove-breakpoint-icons (point-min) (point-max))
3205 (dolist (breakpoint gdb-breakpoints-list)
3206 (let* ((breakpoint (cdr breakpoint))
3207 (bptno (gdb-get-field breakpoint 'number))
3208 (flag (gdb-get-field breakpoint 'enabled))
3209 (address (gdb-get-field breakpoint 'addr)))
3211 (goto-char (point-min))
3212 (if (re-search-forward (concat "^" address) nil t)
3213 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3216 (defvar gdb-breakpoints-header
3218 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3221 (gdb-propertize-header "Threads" gdb-threads-buffer
3222 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3224 ;;; Breakpoints view
3225 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3226 "Major mode for gdb breakpoints.
3228 \\{gdb-breakpoints-mode-map}"
3229 (setq header-line-format gdb-breakpoints-header)
3230 (run-mode-hooks 'gdb-breakpoints-mode-hook)
3231 'gdb-invalidate-breakpoints)
3233 (defun gdb-toggle-breakpoint ()
3234 "Enable/disable breakpoint at current line of breakpoints buffer."
3238 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3241 (concat (if (string-equal "y" (gdb-get-field breakpoint 'enabled))
3244 (gdb-get-field breakpoint 'number)))
3245 (error "Not recognized as break/watchpoint line")))))
3247 (defun gdb-delete-breakpoint ()
3248 "Delete the breakpoint at current line of breakpoints buffer."
3252 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3254 (gud-basic-call (concat "-break-delete " (gdb-get-field breakpoint 'number)))
3255 (error "Not recognized as break/watchpoint line")))))
3257 (defun gdb-goto-breakpoint (&optional event)
3258 "Go to the location of breakpoint at current line of
3259 breakpoints buffer."
3260 (interactive (list last-input-event))
3261 (if event (posn-set-point (event-end event)))
3262 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3263 (let ((window (get-buffer-window gud-comint-buffer)))
3264 (if window (save-selected-window (select-window window))))
3267 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3269 (let ((bptno (gdb-get-field breakpoint 'number))
3270 (file (gdb-get-field breakpoint 'fullname))
3271 (line (gdb-get-field breakpoint 'line)))
3272 (save-selected-window
3273 (let* ((buffer (find-file-noselect
3274 (if (file-exists-p file) file
3275 (cdr (assoc bptno gdb-location-alist)))))
3276 (window (or (gdb-display-source-buffer buffer)
3277 (display-buffer buffer))))
3278 (setq gdb-source-window window)
3279 (with-current-buffer buffer
3280 (goto-line (string-to-number line))
3281 (set-window-point window (point))))))
3282 (error "Not recognized as break/watchpoint line")))))
3285 ;; Frames buffer. This displays a perpetually correct bactrack trace.
3287 (def-gdb-trigger-and-handler
3288 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3289 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3292 (gdb-set-buffer-rules
3294 'gdb-stack-buffer-name
3296 'gdb-invalidate-frames)
3298 (defun gdb-frame-location (frame)
3299 "Return \" of file:line\" or \" of library\" for structure FRAME.
3301 FRAME must have either \"file\" and \"line\" members or \"from\"
3303 (let ((file (gdb-get-field frame 'file))
3304 (line (gdb-get-field frame 'line))
3305 (from (gdb-get-field frame 'from)))
3306 (let ((res (or (and file line (concat file ":" line))
3308 (if res (concat " of " res) ""))))
3310 (defun gdb-stack-list-frames-custom ()
3311 (let ((stack (gdb-get-field (gdb-json-partial-output "frame") 'stack))
3312 (table (make-gdb-table)))
3313 (set-marker gdb-stack-position nil)
3314 (dolist (frame stack)
3315 (gdb-table-add-row table
3317 (gdb-get-field frame 'level)
3320 (gdb-get-field frame 'func)
3321 (if gdb-stack-buffer-locations
3322 (gdb-frame-location frame) "")
3323 (if gdb-stack-buffer-addresses
3324 (concat " at " (gdb-get-field frame 'addr)) "")))
3325 `(mouse-face highlight
3326 help-echo "mouse-2, RET: Select frame"
3328 (insert (gdb-table-string table " ")))
3329 (when (and gdb-frame-number
3330 (gdb-buffer-shows-main-thread-p))
3331 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3332 gdb-stack-position))
3334 (gdb-current-context-mode-name "Frames")))
3336 (defun gdb-stack-buffer-name ()
3337 (gdb-current-context-buffer-name
3338 (concat "stack frames of " (gdb-get-target-string))))
3340 (def-gdb-display-buffer
3341 gdb-display-stack-buffer
3343 "Display backtrace of current stack.")
3345 (def-gdb-preempt-display-buffer
3346 gdb-preemptively-display-stack-buffer
3347 'gdb-stack-buffer nil t)
3349 (def-gdb-frame-for-buffer
3350 gdb-frame-stack-buffer
3352 "Display backtrace of current stack in a new frame.")
3354 (defvar gdb-frames-mode-map
3355 (let ((map (make-sparse-keymap)))
3356 (suppress-keymap map)
3357 (define-key map "q" 'kill-this-buffer)
3358 (define-key map "\r" 'gdb-select-frame)
3359 (define-key map [mouse-2] 'gdb-select-frame)
3360 (define-key map [follow-link] 'mouse-face)
3363 (defvar gdb-frames-font-lock-keywords
3364 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3365 "Font lock keywords used in `gdb-frames-mode'.")
3367 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3368 "Major mode for gdb call stack.
3370 \\{gdb-frames-mode-map}"
3371 (setq gdb-stack-position (make-marker))
3372 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3373 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3374 (set (make-local-variable 'font-lock-defaults)
3375 '(gdb-frames-font-lock-keywords))
3376 (run-mode-hooks 'gdb-frames-mode-hook)
3377 'gdb-invalidate-frames)
3379 (defun gdb-select-frame (&optional event)
3380 "Select the frame and display the relevant source."
3381 (interactive (list last-input-event))
3382 (if event (posn-set-point (event-end event)))
3383 (let ((frame (get-text-property (point) 'gdb-frame)))
3385 (if (gdb-buffer-shows-main-thread-p)
3386 (let ((new-level (gdb-get-field frame 'level)))
3387 (setq gdb-frame-number new-level)
3388 (gdb-input (list (concat "-stack-select-frame " new-level) 'ignore))
3390 (error "Could not select frame for non-current thread."))
3391 (error "Not recognized as frame line"))))
3395 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3396 (def-gdb-trigger-and-handler
3397 gdb-invalidate-locals
3398 (concat (gdb-current-context-command "-stack-list-locals") " --simple-values")
3399 gdb-locals-handler gdb-locals-handler-custom
3402 (gdb-set-buffer-rules
3404 'gdb-locals-buffer-name
3406 'gdb-invalidate-locals)
3408 (defvar gdb-locals-watch-map
3409 (let ((map (make-sparse-keymap)))
3410 (suppress-keymap map)
3411 (define-key map "\r" 'gud-watch)
3412 (define-key map [mouse-2] 'gud-watch)
3414 "Keymap to create watch expression of a complex data type local variable.")
3416 (defvar gdb-edit-locals-map-1
3417 (let ((map (make-sparse-keymap)))
3418 (suppress-keymap map)
3419 (define-key map "\r" 'gdb-edit-locals-value)
3420 (define-key map [mouse-2] 'gdb-edit-locals-value)
3422 "Keymap to edit value of a simple data type local variable.")
3424 (defun gdb-edit-locals-value (&optional event)
3425 "Assign a value to a variable displayed in the locals buffer."
3426 (interactive (list last-input-event))
3428 (if event (posn-set-point (event-end event)))
3430 (let* ((var (gdb-get-field
3431 (get-text-property (point) 'gdb-local-variable) 'name))
3432 (value (read-string (format "New value (%s): " var))))
3434 (concat "-gdb-set variable " var " = " value)))))
3436 ;; Dont display values of arrays or structures.
3437 ;; These can be expanded using gud-watch.
3438 (defun gdb-locals-handler-custom ()
3439 (let ((locals-list (gdb-get-field (gdb-json-partial-output) 'locals))
3440 (table (make-gdb-table)))
3441 (dolist (local locals-list)
3442 (let ((name (gdb-get-field local 'name))
3443 (value (gdb-get-field local 'value))
3444 (type (gdb-get-field local 'type)))
3446 (string-match "\\0x" value))
3447 (add-text-properties 0 (length name)
3448 `(mouse-face highlight
3449 help-echo "mouse-2: create watch expression"
3450 local-map ,gdb-locals-watch-map)
3452 (add-text-properties 0 (length value)
3453 `(mouse-face highlight
3454 help-echo "mouse-2: edit value"
3455 local-map ,gdb-edit-locals-map-1)
3460 (propertize type 'font-lock-face font-lock-type-face)
3461 (propertize name 'font-lock-face font-lock-variable-name-face)
3463 `(gdb-local-variable ,local))))
3464 (insert (gdb-table-string table " "))
3466 (gdb-current-context-mode-name
3467 (concat "Locals: " (gdb-get-field (gdb-current-buffer-frame) 'func))))))
3469 (defvar gdb-locals-header
3471 (gdb-propertize-header "Locals" gdb-locals-buffer
3474 (gdb-propertize-header "Registers" gdb-registers-buffer
3475 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3477 (defvar gdb-locals-mode-map
3478 (let ((map (make-sparse-keymap)))
3479 (suppress-keymap map)
3480 (define-key map "q" 'kill-this-buffer)
3481 (define-key map "\t" '(lambda ()
3483 (gdb-set-window-buffer
3484 (gdb-get-buffer-create
3485 'gdb-registers-buffer
3486 gdb-thread-number) t)))
3489 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3490 "Major mode for gdb locals.
3492 \\{gdb-locals-mode-map}"
3493 (setq header-line-format gdb-locals-header)
3494 (run-mode-hooks 'gdb-locals-mode-hook)
3495 'gdb-invalidate-locals)
3497 (defun gdb-locals-buffer-name ()
3498 (gdb-current-context-buffer-name
3499 (concat "locals of " (gdb-get-target-string))))
3501 (def-gdb-display-buffer
3502 gdb-display-locals-buffer
3504 "Display local variables of current stack and their values.")
3506 (def-gdb-preempt-display-buffer
3507 gdb-preemptively-display-locals-buffer
3508 'gdb-locals-buffer nil t)
3510 (def-gdb-frame-for-buffer
3511 gdb-frame-locals-buffer
3513 "Display local variables of current stack and their values in a new frame.")
3516 ;; Registers buffer.
3518 (def-gdb-trigger-and-handler
3519 gdb-invalidate-registers
3520 (concat (gdb-current-context-command "-data-list-register-values") " x")
3521 gdb-registers-handler
3522 gdb-registers-handler-custom
3525 (gdb-set-buffer-rules
3526 'gdb-registers-buffer
3527 'gdb-registers-buffer-name
3529 'gdb-invalidate-registers)
3531 (defun gdb-registers-handler-custom ()
3532 (let ((register-values (gdb-get-field (gdb-json-partial-output) 'register-values))
3533 (table (make-gdb-table)))
3534 (dolist (register register-values)
3535 (let* ((register-number (gdb-get-field register 'number))
3536 (value (gdb-get-field register 'value))
3537 (register-name (nth (string-to-number register-number)
3538 gdb-register-names)))
3542 (propertize register-name 'font-lock-face font-lock-variable-name-face)
3543 (if (member register-number gdb-changed-registers)
3544 (propertize value 'font-lock-face font-lock-warning-face)
3546 `(mouse-face highlight
3547 help-echo "mouse-2: edit value"
3548 gdb-register-name ,register-name))))
3549 (insert (gdb-table-string table " "))
3551 (gdb-current-context-mode-name "Registers"))))
3553 (defun gdb-edit-register-value (&optional event)
3554 "Assign a value to a register displayed in the registers buffer."
3555 (interactive (list last-input-event))
3557 (if event (posn-set-point (event-end event)))
3559 (let* ((var (gdb-get-field
3560 (get-text-property (point) 'gdb-register-name)))
3561 (value (read-string (format "New value (%s): " var))))
3563 (concat "-gdb-set variable $" var " = " value)))))
3565 (defvar gdb-registers-mode-map
3566 (let ((map (make-sparse-keymap)))
3567 (suppress-keymap map)
3568 (define-key map "\r" 'gdb-edit-register-value)
3569 (define-key map [mouse-2] 'gdb-edit-register-value)
3570 (define-key map "q" 'kill-this-buffer)
3571 (define-key map "\t" '(lambda ()
3573 (gdb-set-window-buffer
3574 (gdb-get-buffer-create
3576 gdb-thread-number) t)))
3579 (defvar gdb-registers-header
3581 (gdb-propertize-header "Locals" gdb-locals-buffer
3582 "mouse-1: select" mode-line-highlight mode-line-inactive)
3584 (gdb-propertize-header "Registers" gdb-registers-buffer
3585 nil nil mode-line)))
3587 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3588 "Major mode for gdb registers.
3590 \\{gdb-registers-mode-map}"
3591 (setq header-line-format gdb-registers-header)
3592 (run-mode-hooks 'gdb-registers-mode-hook)
3593 'gdb-invalidate-registers)
3595 (defun gdb-registers-buffer-name ()
3596 (gdb-current-context-buffer-name
3597 (concat "registers of " (gdb-get-target-string))))
3599 (def-gdb-display-buffer
3600 gdb-display-registers-buffer
3601 'gdb-registers-buffer
3602 "Display integer register contents.")
3604 (def-gdb-preempt-display-buffer
3605 gdb-preemptively-display-registers-buffer
3606 'gdb-registers-buffer nil t)
3608 (def-gdb-frame-for-buffer
3609 gdb-frame-registers-buffer
3610 'gdb-registers-buffer
3611 "Display integer register contents in a new frame.")
3613 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3614 (defun gdb-get-changed-registers ()
3615 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3616 (not (gdb-pending-p 'gdb-get-changed-registers)))
3620 "-data-list-changed-registers"
3621 'gdb-changed-registers-handler))
3622 (gdb-add-pending 'gdb-get-changed-registers))))
3624 (defun gdb-changed-registers-handler ()
3625 (gdb-delete-pending 'gdb-get-changed-registers)
3626 (setq gdb-changed-registers nil)
3627 (dolist (register-number (gdb-get-field (gdb-json-partial-output) 'changed-registers))
3628 (push register-number gdb-changed-registers)))
3630 (defun gdb-register-names-handler ()
3631 ;; Don't use gdb-pending-triggers because this handler is called
3632 ;; only once (in gdb-init-1)
3633 (setq gdb-register-names nil)
3634 (dolist (register-name (gdb-get-field (gdb-json-partial-output) 'register-names))
3635 (push register-name gdb-register-names))
3636 (setq gdb-register-names (reverse gdb-register-names)))
3639 (defun gdb-get-source-file-list ()
3640 "Create list of source files for current GDB session.
3641 If buffers already exist for any of these files, gud-minor-mode
3643 (goto-char (point-min))
3644 (while (re-search-forward gdb-source-file-regexp nil t)
3645 (push (match-string 1) gdb-source-file-list))
3646 (dolist (buffer (buffer-list))
3647 (with-current-buffer buffer
3648 (when (member buffer-file-name gdb-source-file-list)
3649 (gdb-init-buffer))))
3650 (gdb-force-mode-line-update
3651 (propertize "ready" 'face font-lock-variable-name-face)))
3653 (defun gdb-get-main-selected-frame ()
3654 "Trigger for `gdb-frame-handler' which uses main current
3655 thread. Called from `gdb-update'."
3656 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3659 (list (gdb-current-context-command "-stack-info-frame") 'gdb-frame-handler))
3660 (gdb-add-pending 'gdb-get-main-selected-frame))))
3662 (defun gdb-frame-handler ()
3663 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3664 overlay arrow in source buffer."
3665 (gdb-delete-pending 'gdb-get-main-selected-frame)
3666 (let ((frame (gdb-get-field (gdb-json-partial-output) 'frame)))
3668 (setq gdb-selected-frame (gdb-get-field frame 'func))
3669 (setq gdb-selected-file (gdb-get-field frame 'fullname))
3670 (let ((line (gdb-get-field frame 'line)))
3671 (setq gdb-selected-line (or (and line (string-to-number line))
3672 nil)) ; don't fail if line is nil
3673 (when line ; obey the current file only if we have line info
3674 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3675 (gud-display-frame)))
3676 (if gud-overlay-arrow-position
3677 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3678 (position (marker-position gud-overlay-arrow-position)))
3680 (with-current-buffer buffer
3681 (setq fringe-indicator-alist
3682 (if (string-equal gdb-frame-number "0")
3684 '((overlay-arrow . hollow-right-triangle))))
3685 (setq gud-overlay-arrow-position (make-marker))
3686 (set-marker gud-overlay-arrow-position position))))))))
3688 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3690 (defun gdb-get-prompt ()
3691 "Find prompt for GDB session."
3692 (goto-char (point-min))
3693 (setq gdb-prompt-name nil)
3694 (re-search-forward gdb-prompt-name-regexp nil t)
3695 (setq gdb-prompt-name (match-string 1))
3696 ;; Insert first prompt.
3697 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3699 ;;;; Window management
3700 (defun gdb-display-buffer (buf dedicated &optional frame)
3703 If BUF is already displayed in some window, show it, deiconifying
3704 the frame if necessary. Otherwise, find least recently used
3705 window and show BUF there, if the window is not used for GDB
3706 already, in which case that window is splitted first."
3707 (let ((answer (get-buffer-window buf (or frame 0))))
3709 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3710 (let ((window (get-lru-window)))
3711 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3713 (let* ((largest (get-largest-window))
3714 (cur-size (window-height largest)))
3715 (setq answer (split-window largest))
3716 (set-window-buffer answer buf)
3717 (set-window-dedicated-p answer dedicated)
3719 (set-window-buffer window buf)
3722 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3723 "Find window displaying a buffer with the same
3724 `gdb-buffer-type' as BUF and show BUF there. If no such window
3725 exists, just call `gdb-display-buffer' for BUF. If the window
3726 found is already dedicated, split window according to
3727 SPLIT-HORIZONTAL and show BUF in the new window."
3729 (when (not (get-buffer-window buf))
3730 (let* ((buf-type (gdb-buffer-type buf))
3732 (get-window-with-predicate
3735 (gdb-buffer-type (window-buffer w)))
3736 (not (window-dedicated-p w)))))))
3738 (set-window-buffer existing-window buf)
3739 (let ((dedicated-window
3740 (get-window-with-predicate
3743 (gdb-buffer-type (window-buffer w)))))))
3744 (if dedicated-window
3746 (split-window dedicated-window nil split-horizontal) buf)
3747 (gdb-display-buffer buf t))))))
3748 (error "Null buffer")))
3750 ;;; Shared keymap initialization:
3752 (let ((menu (make-sparse-keymap "GDB-Windows")))
3753 (define-key gud-menu-map [displays]
3754 `(menu-item "GDB-Windows" ,menu
3755 :visible (eq gud-minor-mode 'gdbmi)))
3756 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3757 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3758 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3759 (define-key menu [disassembly]
3760 '("Disassembly" . gdb-display-disassembly-buffer))
3761 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3762 (define-key menu [inferior]
3763 '("Separate IO" . gdb-display-separate-io-buffer))
3764 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3765 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3766 (define-key menu [breakpoints]
3767 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3769 (let ((menu (make-sparse-keymap "GDB-Frames")))
3770 (define-key gud-menu-map [frames]
3771 `(menu-item "GDB-Frames" ,menu
3772 :visible (eq gud-minor-mode 'gdbmi)))
3773 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3774 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3775 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3776 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
3777 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3778 (define-key menu [inferior]
3779 '("Separate IO" . gdb-frame-separate-io-buffer))
3780 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3781 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3782 (define-key menu [breakpoints]
3783 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3785 (let ((menu (make-sparse-keymap "GDB-MI")))
3786 (define-key menu [gdb-customize]
3787 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3788 :help "Customize Gdb Graphical Mode options."))
3789 (define-key menu [gdb-many-windows]
3790 '(menu-item "Display Other Windows" gdb-many-windows
3791 :help "Toggle display of locals, stack and breakpoint information"
3792 :button (:toggle . gdb-many-windows)))
3793 (define-key menu [gdb-restore-windows]
3794 '(menu-item "Restore Window Layout" gdb-restore-windows
3795 :help "Restore standard layout for debug session."))
3796 (define-key menu [sep1]
3798 (define-key menu [all-threads]
3799 '(menu-item "GUD controls all threads"
3802 (setq gdb-gud-control-all-threads t))
3803 :help "GUD start/stop commands apply to all threads"
3804 :button (:radio . gdb-gud-control-all-threads)))
3805 (define-key menu [current-thread]
3806 '(menu-item "GUD controls current thread"
3809 (setq gdb-gud-control-all-threads nil))
3810 :help "GUD start/stop commands apply to current thread only"
3811 :button (:radio . (not gdb-gud-control-all-threads))))
3812 (define-key menu [sep2]
3814 (define-key menu [gdb-customize-reasons]
3815 '(menu-item "Customize switching..."
3818 (customize-option 'gdb-switch-reasons))))
3819 (define-key menu [gdb-switch-when-another-stopped]
3820 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped gdb-switch-when-another-stopped
3821 "Automatically switch to stopped thread"
3822 "GDB thread switching %s"
3823 "Switch to stopped thread"))
3824 (define-key gud-menu-map [mi]
3825 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3827 (defun gdb-frame-gdb-buffer ()
3828 "Display GUD buffer in a new frame."
3830 (let ((special-display-regexps (append special-display-regexps '(".*")))
3831 (special-display-frame-alist
3832 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3833 gdb-frame-parameters)))
3834 (same-window-regexps nil))
3835 (display-buffer gud-comint-buffer)))
3837 (defun gdb-display-gdb-buffer ()
3838 "Display GUD buffer."
3840 (let ((same-window-regexps nil))
3841 (select-window (display-buffer gud-comint-buffer nil 0))))
3843 (defun gdb-set-window-buffer (name &optional ignore-dedicated)
3844 "Set buffer of selected window to NAME and dedicate window.
3846 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
3847 window is dedicated."
3848 (when ignore-dedicated
3849 (set-window-dedicated-p (selected-window) nil))
3850 (set-window-buffer (selected-window) (get-buffer name))
3851 (set-window-dedicated-p (selected-window) t))
3853 (defun gdb-setup-windows ()
3854 "Layout the window pattern for `gdb-many-windows'."
3855 (gdb-display-locals-buffer)
3856 (gdb-display-stack-buffer)
3857 (delete-other-windows)
3858 (gdb-display-breakpoints-buffer)
3859 (delete-other-windows)
3861 (pop-to-buffer gud-comint-buffer)
3862 (split-window nil ( / ( * (window-height) 3) 4))
3863 (split-window nil ( / (window-height) 3))
3864 (split-window-horizontally)
3866 (gdb-set-window-buffer (gdb-locals-buffer-name))
3869 (if gud-last-last-frame
3870 (gud-find-file (car gud-last-last-frame))
3872 (gud-find-file gdb-main-file)
3873 ;; Put buffer list in window if we
3874 ;; can't find a source file.
3875 (list-buffers-noselect))))
3876 (setq gdb-source-window (selected-window))
3877 (when gdb-use-separate-io-buffer
3878 (split-window-horizontally)
3880 (gdb-set-window-buffer
3881 (gdb-get-buffer-create 'gdb-inferior-io)))
3883 (gdb-set-window-buffer (gdb-stack-buffer-name))
3884 (split-window-horizontally)
3886 (gdb-set-window-buffer (if gdb-show-threads-by-default
3887 (gdb-threads-buffer-name)
3888 (gdb-breakpoints-buffer-name)))
3891 (defcustom gdb-many-windows nil
3892 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
3893 In this case it starts with two windows: one displaying the GUD
3894 buffer and the other with the source file with the main routine
3895 of the debugged program. Non-nil means display the layout shown for
3901 (defun gdb-many-windows (arg)
3902 "Toggle the number of windows in the basic arrangement.
3903 With arg, display additional buffers iff arg is positive."
3905 (setq gdb-many-windows
3907 (not gdb-many-windows)
3908 (> (prefix-numeric-value arg) 0)))
3909 (message (format "Display of other windows %sabled"
3910 (if gdb-many-windows "en" "dis")))
3911 (if (and gud-comint-buffer
3912 (buffer-name gud-comint-buffer))
3914 (gdb-restore-windows)
3917 (defun gdb-restore-windows ()
3918 "Restore the basic arrangement of windows used by gdb.
3919 This arrangement depends on the value of `gdb-many-windows'."
3921 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3922 (delete-other-windows)
3923 (if gdb-many-windows
3925 (when (or gud-last-last-frame gdb-show-main)
3929 (if gud-last-last-frame
3930 (gud-find-file (car gud-last-last-frame))
3931 (gud-find-file gdb-main-file)))
3932 (setq gdb-source-window (selected-window))
3936 "Exit a debugging session cleanly.
3937 Kills the gdb buffers, and resets variables and the source buffers."
3938 (dolist (buffer (buffer-list))
3939 (unless (eq buffer gud-comint-buffer)
3940 (with-current-buffer buffer
3941 (if (eq gud-minor-mode 'gdbmi)
3942 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3944 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3945 (setq gud-minor-mode nil)
3946 (kill-local-variable 'tool-bar-map)
3947 (kill-local-variable 'gdb-define-alist))))))
3948 (setq gdb-disassembly-position nil)
3949 (setq overlay-arrow-variable-list
3950 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
3951 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3952 (setq gdb-stack-position nil)
3953 (setq overlay-arrow-variable-list
3954 (delq 'gdb-stack-position overlay-arrow-variable-list))
3955 (setq gdb-thread-position nil)
3956 (setq overlay-arrow-variable-list
3957 (delq 'gdb-thread-position overlay-arrow-variable-list))
3958 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3959 (setq gud-running nil)
3960 (setq gdb-active-process nil)
3961 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3963 (defun gdb-get-source-file ()
3964 "Find the source file where the program starts and display it with related
3965 buffers, if required."
3966 (goto-char (point-min))
3967 (if (re-search-forward gdb-source-file-regexp nil t)
3968 (setq gdb-main-file (match-string 1)))
3969 (if gdb-many-windows
3971 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3973 (let ((pop-up-windows t))
3974 (display-buffer (gud-find-file gdb-main-file))))))
3977 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3978 "Put string PUTSTRING in front of POS in the current buffer.
3979 PUTSTRING is displayed by putting an overlay into the current buffer with a
3980 `before-string' string that has a `display' property whose value is
3982 (let ((string (make-string 1 ?x))
3983 (buffer (current-buffer)))
3984 (setq putstring (copy-sequence putstring))
3985 (let ((overlay (make-overlay pos pos buffer))
3987 (list (list 'margin 'left-margin) putstring))))
3988 (put-text-property 0 1 'display prop string)
3990 (add-text-properties 0 1 sprops string))
3991 (overlay-put overlay 'put-break t)
3992 (overlay-put overlay 'before-string string))))
3994 ;;from remove-images
3995 (defun gdb-remove-strings (start end &optional buffer)
3996 "Remove strings between START and END in BUFFER.
3997 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3998 BUFFER nil or omitted means use the current buffer."
4000 (setq buffer (current-buffer)))
4001 (dolist (overlay (overlays-in start end))
4002 (when (overlay-get overlay 'put-break)
4003 (delete-overlay overlay))))
4005 (defun gdb-put-breakpoint-icon (enabled bptno)
4006 (let ((start (- (line-beginning-position) 1))
4007 (end (+ (line-end-position) 1))
4008 (putstring (if enabled "B" "b"))
4009 (source-window (get-buffer-window (current-buffer) 0)))
4010 (add-text-properties
4011 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4014 (add-text-properties
4015 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4016 (add-text-properties
4017 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4018 (gdb-remove-breakpoint-icons start end)
4019 (if (display-images-p)
4020 (if (>= (or left-fringe-width
4021 (if source-window (car (window-fringes source-window)))
4022 gdb-buffer-fringe-width) 8)
4025 `(left-fringe breakpoint
4028 'breakpoint-disabled))
4030 'gdb-enabled enabled)
4031 (when (< left-margin-width 2)
4032 (save-current-buffer
4033 (setq left-margin-width 2)
4037 left-margin-width right-margin-width))))
4040 (or breakpoint-enabled-icon
4041 (setq breakpoint-enabled-icon
4042 (find-image `((:type xpm :data
4043 ,breakpoint-xpm-data
4044 :ascent 100 :pointer hand)
4046 ,breakpoint-enabled-pbm-data
4047 :ascent 100 :pointer hand)))))
4048 (or breakpoint-disabled-icon
4049 (setq breakpoint-disabled-icon
4050 (find-image `((:type xpm :data
4051 ,breakpoint-xpm-data
4052 :conversion disabled
4053 :ascent 100 :pointer hand)
4055 ,breakpoint-disabled-pbm-data
4056 :ascent 100 :pointer hand))))))
4060 (when (< left-margin-width 2)
4061 (save-current-buffer
4062 (setq left-margin-width 2)
4063 (let ((window (get-buffer-window (current-buffer) 0)))
4066 window left-margin-width right-margin-width)))))
4068 (propertize putstring
4069 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
4072 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4073 (gdb-remove-strings start end)
4074 (if (display-images-p)
4075 (remove-images start end))
4077 (setq left-margin-width 0)
4078 (let ((window (get-buffer-window (current-buffer) 0)))
4081 window left-margin-width right-margin-width)))))
4085 ;; arch-tag: 1b41ea2b-f364-4cec-8f35-e02e4fe01912
4086 ;;; gdb-mi.el ends here