Update copyright notices for 2013.
[emacs.git] / lisp / progmodes / gdb-mi.el
blobfc3d336cf9912ffc0a4847b868d5148d3e18c16f
1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
6 ;; Maintainer: FSF
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/>.
26 ;;; Credits:
28 ;; This file was written by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It was further developed
30 ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
31 ;; of Code 2009 Project "Emacs GDB/MI migration".
33 ;;; Commentary:
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
42 ;; manual).
44 ;; M-x gdb will start the debugger.
46 ;; This file uses GDB/MI as the primary interface to GDB. It runs gdb with
47 ;; GDB/MI (-interp=mi) and access CLI using "-interpreter-exec console
48 ;; cli-command". This code replaces gdb-ui.el and uses MI tokens instead
49 ;; of queues. Eventually MI should be asynchronous.
51 ;; Windows Platforms:
53 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
54 ;; explicitly in your program if you want timely display of I/O in Emacs.
55 ;; Alternatively you can make the output stream unbuffered, for example, by
56 ;; using a macro:
58 ;; #ifdef UNBUFFERED
59 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
60 ;; #endif
62 ;; and compiling with -DUNBUFFERED while debugging.
64 ;; If you are using Cygwin GDB and find that the source is not being displayed
65 ;; in Emacs when you step through it, possible solutions are to:
67 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
68 ;; (Since 22.1 Emacs builds under Cygwin.)
69 ;; 2) Use MinGW GDB instead.
70 ;; 3) Use cygwin-mount.el
72 ;;; Mac OSX:
74 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of Mac OSX.
76 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
77 ;; as earlier versions do not compile on Mac OSX.
79 ;;; Known Bugs:
81 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
82 ;; line information, e.g., a routine in libc (just a TODO item).
84 ;; TODO:
85 ;; 2) Watch windows to work with threads.
86 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
87 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
89 ;;; Code:
91 (require 'gud)
92 (require 'json)
93 (require 'bindat)
94 (eval-when-compile (require 'cl-lib))
96 (declare-function speedbar-change-initial-expansion-list
97 "speedbar" (new-default))
98 (declare-function speedbar-timer-fn "speedbar" ())
99 (declare-function speedbar-line-text "speedbar" (&optional p))
100 (declare-function speedbar-change-expand-button-char "speedbar" (char))
101 (declare-function speedbar-delete-subblock "speedbar" (indent))
102 (declare-function speedbar-center-buffer-smartly "speedbar" ())
104 (defvar tool-bar-map)
105 (defvar speedbar-initial-expansion-list-name)
106 (defvar speedbar-frame)
108 (defvar gdb-memory-address "main")
109 (defvar gdb-memory-last-address nil
110 "Last successfully accessed memory address.")
111 (defvar gdb-memory-next-page nil
112 "Address of next memory page for program memory buffer.")
113 (defvar gdb-memory-prev-page nil
114 "Address of previous memory page for program memory buffer.")
116 (defvar gdb-thread-number nil
117 "Main current thread.
119 Invalidation triggers use this variable to query GDB for
120 information on the specified thread by wrapping GDB/MI commands
121 in `gdb-current-context-command'.
123 This variable may be updated implicitly by GDB via `gdb-stopped'
124 or explicitly by `gdb-select-thread'.
126 Only `gdb-setq-thread-number' should be used to change this
127 value.")
129 (defvar gdb-frame-number nil
130 "Selected frame level for main current thread.
132 Updated according to the following rules:
134 When a thread is selected or current thread stops, set to \"0\".
136 When current thread goes running (and possibly exits eventually),
137 set to nil.
139 May be manually changed by user with `gdb-select-frame'.")
141 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
143 ;; Used to show overlay arrow in source buffer. All set in
144 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
145 ;; these but rely on buffer-local thread information instead.
146 (defvar gdb-selected-frame nil
147 "Name of selected function for main current thread.")
148 (defvar gdb-selected-file nil
149 "Name of selected file for main current thread.")
150 (defvar gdb-selected-line nil
151 "Number of selected line for main current thread.")
153 (defvar gdb-threads-list nil
154 "Associative list of threads provided by \"-thread-info\" MI command.
156 Keys are thread numbers (in strings) and values are structures as
157 returned from -thread-info by `gdb-json-partial-output'. Updated in
158 `gdb-thread-list-handler-custom'.")
160 (defvar gdb-running-threads-count nil
161 "Number of currently running threads.
163 If nil, no information is available.
165 Updated in `gdb-thread-list-handler-custom'.")
167 (defvar gdb-stopped-threads-count nil
168 "Number of currently stopped threads.
170 See also `gdb-running-threads-count'.")
172 (defvar gdb-breakpoints-list nil
173 "Associative list of breakpoints provided by \"-break-list\" MI command.
175 Keys are breakpoint numbers (in string) and values are structures
176 as returned from \"-break-list\" by `gdb-json-partial-output'
177 \(\"body\" field is used). Updated in
178 `gdb-breakpoints-list-handler-custom'.")
180 (defvar gdb-current-language nil)
181 (defvar gdb-var-list nil
182 "List of variables in watch window.
183 Each element has the form
184 (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
185 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
186 address for root variables.")
187 (defvar gdb-main-file nil "Source file from which program execution begins.")
189 ;; Overlay arrow markers
190 (defvar gdb-stack-position nil)
191 (defvar gdb-thread-position nil)
192 (defvar gdb-disassembly-position nil)
194 (defvar gdb-location-alist nil
195 "Alist of breakpoint numbers and full filenames. Only used for files that
196 Emacs can't find.")
197 (defvar gdb-active-process nil
198 "GUD tooltips display variable values when t, and macro definitions otherwise.")
199 (defvar gdb-error "Non-nil when GDB is reporting an error.")
200 (defvar gdb-macro-info nil
201 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
202 (defvar gdb-register-names nil "List of register names.")
203 (defvar gdb-changed-registers nil
204 "List of changed register numbers (strings).")
205 (defvar gdb-buffer-fringe-width nil)
206 (defvar gdb-last-command nil)
207 (defvar gdb-prompt-name nil)
208 (defvar gdb-token-number 0)
209 (defvar gdb-handler-alist '())
210 (defvar gdb-handler-number nil)
211 (defvar gdb-source-file-list nil
212 "List of source files for the current executable.")
213 (defvar gdb-first-done-or-error t)
214 (defvar gdb-source-window nil)
215 (defvar gdb-inferior-status nil)
216 (defvar gdb-continuation nil)
217 (defvar gdb-supports-non-stop nil)
218 (defvar gdb-filter-output nil
219 "Message to be shown in GUD console.
221 This variable is updated in `gdb-done-or-error' and returned by
222 `gud-gdbmi-marker-filter'.")
224 (defvar gdb-non-stop nil
225 "Indicates whether current GDB session is using non-stop mode.
227 It is initialized to `gdb-non-stop-setting' at the beginning of
228 every GDB session.")
230 (defvar gdb-buffer-type nil
231 "One of the symbols bound in `gdb-buffer-rules'.")
232 (make-variable-buffer-local 'gdb-buffer-type)
234 (defvar gdb-output-sink 'nil
235 "The disposition of the output of the current gdb command.
236 Possible values are these symbols:
238 `user' -- gdb output should be copied to the GUD buffer
239 for the user to see.
241 `emacs' -- output should be collected in the partial-output-buffer
242 for subsequent processing by a command. This is the
243 disposition of output generated by commands that
244 gdb mode sends to gdb on its own behalf.")
246 ;; Pending triggers prevent congestion: Emacs won't send two similar
247 ;; consecutive requests.
249 (defvar gdb-pending-triggers '()
250 "A list of trigger functions which have not yet been handled.
252 Elements are either function names or pairs (buffer . function)")
254 (defmacro gdb-add-pending (item)
255 `(push ,item gdb-pending-triggers))
256 (defmacro gdb-pending-p (item)
257 `(member ,item gdb-pending-triggers))
258 (defmacro gdb-delete-pending (item)
259 `(setq gdb-pending-triggers
260 (delete ,item gdb-pending-triggers)))
262 (defmacro gdb-wait-for-pending (&rest body)
263 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
265 This function checks `gdb-pending-triggers' value every
266 `gdb-wait-for-pending' seconds."
267 (run-with-timer
268 0.5 nil
269 `(lambda ()
270 (if (not gdb-pending-triggers)
271 (progn ,@body)
272 (gdb-wait-for-pending ,@body)))))
274 ;; Publish-subscribe
276 (defmacro gdb-add-subscriber (publisher subscriber)
277 "Register new PUBLISHER's SUBSCRIBER.
279 SUBSCRIBER must be a pair, where cdr is a function of one
280 argument (see `gdb-emit-signal')."
281 `(add-to-list ',publisher ,subscriber t))
283 (defmacro gdb-delete-subscriber (publisher subscriber)
284 "Unregister SUBSCRIBER from PUBLISHER."
285 `(setq ,publisher (delete ,subscriber
286 ,publisher)))
288 (defun gdb-get-subscribers (publisher)
289 publisher)
291 (defun gdb-emit-signal (publisher &optional signal)
292 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
293 (dolist (subscriber (gdb-get-subscribers publisher))
294 (funcall (cdr subscriber) signal)))
296 (defvar gdb-buf-publisher '()
297 "Used to invalidate GDB buffers by emitting a signal in
298 `gdb-update'.
300 Must be a list of pairs with cars being buffers and cdr's being
301 valid signal handlers.")
303 (defgroup gdb nil
304 "GDB graphical interface"
305 :group 'tools
306 :link '(info-link "(emacs)GDB Graphical Interface")
307 :version "23.2")
309 (defgroup gdb-non-stop nil
310 "GDB non-stop debugging settings"
311 :group 'gdb
312 :version "23.2")
314 (defgroup gdb-buffers nil
315 "GDB buffers"
316 :group 'gdb
317 :version "23.2")
319 (defcustom gdb-debug-log-max 128
320 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
321 :group 'gdb
322 :type '(choice (integer :tag "Number of elements")
323 (const :tag "Unlimited" nil))
324 :version "22.1")
326 (defcustom gdb-non-stop-setting t
327 "When in non-stop mode, stopped threads can be examined while
328 other threads continue to execute.
330 GDB session needs to be restarted for this setting to take
331 effect."
332 :type 'boolean
333 :group 'gdb-non-stop
334 :version "23.2")
336 ;; TODO Some commands can't be called with --all (give a notice about
337 ;; it in setting doc)
338 (defcustom gdb-gud-control-all-threads t
339 "When enabled, GUD execution commands affect all threads when
340 in non-stop mode. Otherwise, only current thread is affected."
341 :type 'boolean
342 :group 'gdb-non-stop
343 :version "23.2")
345 (defcustom gdb-switch-reasons t
346 "List of stop reasons which cause Emacs to switch to the thread
347 which caused the stop. When t, switch to stopped thread no matter
348 what the reason was. When nil, never switch to stopped thread
349 automatically.
351 This setting is used in non-stop mode only. In all-stop mode,
352 Emacs always switches to the thread which caused the stop."
353 ;; exited, exited-normally and exited-signaled are not
354 ;; thread-specific stop reasons and therefore are not included in
355 ;; this list
356 :type '(choice
357 (const :tag "All reasons" t)
358 (set :tag "Selection of reasons..."
359 (const :tag "A breakpoint was reached." "breakpoint-hit")
360 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
361 (const :tag "A read watchpoint was triggered."
362 "read-watchpoint-trigger")
363 (const :tag "An access watchpoint was triggered."
364 "access-watchpoint-trigger")
365 (const :tag "Function finished execution." "function-finished")
366 (const :tag "Location reached." "location-reached")
367 (const :tag "Watchpoint has gone out of scope"
368 "watchpoint-scope")
369 (const :tag "End of stepping range reached."
370 "end-stepping-range")
371 (const :tag "Signal received (like interruption)."
372 "signal-received"))
373 (const :tag "None" nil))
374 :group 'gdb-non-stop
375 :version "23.2"
376 :link '(info-link "(gdb)GDB/MI Async Records"))
378 (defcustom gdb-stopped-functions nil
379 "List of functions called whenever GDB stops.
381 Each function takes one argument, a parsed MI response, which
382 contains fields of corresponding MI *stopped async record:
384 ((stopped-threads . \"all\")
385 (thread-id . \"1\")
386 (frame (line . \"38\")
387 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
388 (file . \"server.c\")
389 (args ((value . \"0x804b038\")
390 (name . \"arg\")))
391 (func . \"hello\")
392 (addr . \"0x0804869e\"))
393 (reason . \"end-stepping-range\"))
395 Note that \"reason\" is only present in non-stop debugging mode.
397 `bindat-get-field' may be used to access the fields of response.
399 Each function is called after the new current thread was selected
400 and GDB buffers were updated in `gdb-stopped'."
401 :type '(repeat function)
402 :group 'gdb
403 :version "23.2"
404 :link '(info-link "(gdb)GDB/MI Async Records"))
406 (defcustom gdb-switch-when-another-stopped t
407 "When nil, Emacs won't switch to stopped thread if some other
408 stopped thread is already selected."
409 :type 'boolean
410 :group 'gdb-non-stop
411 :version "23.2")
413 (defcustom gdb-stack-buffer-locations t
414 "Show file information or library names in stack buffers."
415 :type 'boolean
416 :group 'gdb-buffers
417 :version "23.2")
419 (defcustom gdb-stack-buffer-addresses nil
420 "Show frame addresses in stack buffers."
421 :type 'boolean
422 :group 'gdb-buffers
423 :version "23.2")
425 (defcustom gdb-thread-buffer-verbose-names t
426 "Show long thread names in threads buffer."
427 :type 'boolean
428 :group 'gdb-buffers
429 :version "23.2")
431 (defcustom gdb-thread-buffer-arguments t
432 "Show function arguments in threads buffer."
433 :type 'boolean
434 :group 'gdb-buffers
435 :version "23.2")
437 (defcustom gdb-thread-buffer-locations t
438 "Show file information or library names in threads buffer."
439 :type 'boolean
440 :group 'gdb-buffers
441 :version "23.2")
443 (defcustom gdb-thread-buffer-addresses nil
444 "Show addresses for thread frames in threads buffer."
445 :type 'boolean
446 :group 'gdb-buffers
447 :version "23.2")
449 (defcustom gdb-show-threads-by-default nil
450 "Show threads list buffer instead of breakpoints list by
451 default."
452 :type 'boolean
453 :group 'gdb-buffers
454 :version "23.2")
456 (defvar gdb-debug-log nil
457 "List of commands sent to and replies received from GDB.
458 Most recent commands are listed first. This list stores only the last
459 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
461 ;;;###autoload
462 (define-minor-mode gdb-enable-debug
463 "Toggle logging of transaction between Emacs and Gdb.
464 The log is stored in `gdb-debug-log' as an alist with elements
465 whose cons is send, send-item or recv and whose cdr is the string
466 being transferred. This list may grow up to a size of
467 `gdb-debug-log-max' after which the oldest element (at the end of
468 the list) is deleted every time a new one is added (at the front)."
469 :global t
470 :group 'gdb
471 :version "22.1")
473 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
474 "Shell command for generating a list of defined macros in a source file.
475 This list is used to display the #define directive associated
476 with an identifier as a tooltip. It works in a debug session with
477 GDB, when `gud-tooltip-mode' is t.
479 Set `gdb-cpp-define-alist-flags' for any include paths or
480 predefined macros."
481 :type 'string
482 :group 'gdb
483 :version "22.1")
485 (defcustom gdb-cpp-define-alist-flags ""
486 "Preprocessor flags for `gdb-cpp-define-alist-program'."
487 :type 'string
488 :group 'gdb
489 :version "22.1")
491 (defcustom gdb-create-source-file-list t
492 "Non-nil means create a list of files from which the executable was built.
493 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
494 line for a long time when starting, possibly because your executable was
495 built from a large number of files. This allows quicker initialization
496 but means that these files are not automatically enabled for debugging,
497 e.g., you won't be able to click in the fringe to set a breakpoint until
498 execution has already stopped there."
499 :type 'boolean
500 :group 'gdb
501 :version "23.1")
503 (defcustom gdb-show-main nil
504 "Non-nil means display source file containing the main routine at startup.
505 Also display the main routine in the disassembly buffer if present."
506 :type 'boolean
507 :group 'gdb
508 :version "22.1")
510 (defun gdb-force-mode-line-update (status)
511 (let ((buffer gud-comint-buffer))
512 (if (and buffer (buffer-name buffer))
513 (with-current-buffer buffer
514 (setq mode-line-process
515 (format ":%s [%s]"
516 (process-status (get-buffer-process buffer)) status))
517 ;; Force mode line redisplay soon.
518 (force-mode-line-update)))))
520 ;; These two are used for menu and toolbar
521 (defun gdb-control-all-threads ()
522 "Switch to non-stop/A mode."
523 (interactive)
524 (setq gdb-gud-control-all-threads t)
525 ;; Actually forcing the tool-bar to update.
526 (force-mode-line-update)
527 (message "Now in non-stop/A mode."))
529 (defun gdb-control-current-thread ()
530 "Switch to non-stop/T mode."
531 (interactive)
532 (setq gdb-gud-control-all-threads nil)
533 ;; Actually forcing the tool-bar to update.
534 (force-mode-line-update)
535 (message "Now in non-stop/T mode."))
537 (defun gdb-find-watch-expression ()
538 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
539 (varnum (car var)) expr)
540 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
541 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
542 (component-list (split-string (match-string 2 varnum) "\\." t)))
543 (setq expr (nth 1 var1))
544 (setq varnumlet (car var1))
545 (dolist (component component-list)
546 (setq var2 (assoc varnumlet gdb-var-list))
547 (setq expr (concat expr
548 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
549 (concat "[" component "]")
550 (concat "." component))))
551 (setq varnumlet (concat varnumlet "." component)))
552 expr)))
554 ;; noall is used for commands which don't take --all, but only
555 ;; --thread.
556 (defun gdb-gud-context-command (command &optional noall)
557 "When `gdb-non-stop' is t, add --thread option to COMMAND if
558 `gdb-gud-control-all-threads' is nil and --all option otherwise.
559 If NOALL is t, always add --thread option no matter what
560 `gdb-gud-control-all-threads' value is.
562 When `gdb-non-stop' is nil, return COMMAND unchanged."
563 (if gdb-non-stop
564 (if (and gdb-gud-control-all-threads
565 (not noall)
566 gdb-supports-non-stop)
567 (concat command " --all ")
568 (gdb-current-context-command command))
569 command))
571 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
572 "`gud-call' wrapper which adds --thread/--all options between
573 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
575 NOARG must be t when this macro is used outside `gud-def'"
576 `(gud-call
577 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
578 ,(when (not noarg) 'arg)))
580 (defun gdb--check-interpreter (proc string)
581 (unless (zerop (length string))
582 (let ((filter (process-get proc 'gud-normal-filter)))
583 (set-process-filter proc filter)
584 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
585 ;; Apparently we're not running with -i=mi.
586 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
587 (message msg)
588 (setq string (concat (propertize msg 'font-lock-face 'error)
589 "\n" string)))
590 ;; Use the old gud-gbd filter, not because it works, but because it
591 ;; will properly display GDB's answers rather than hanging waiting for
592 ;; answers that aren't coming.
593 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
594 (funcall filter proc string))))
596 (defvar gdb-control-level 0)
598 ;;;###autoload
599 (defun gdb (command-line)
600 "Run gdb on program FILE in buffer *gud-FILE*.
601 The directory containing FILE becomes the initial working directory
602 and source-file directory for your debugger.
604 COMMAND-LINE is the shell command for starting the gdb session.
605 It should be a string consisting of the name of the gdb
606 executable followed by command-line options. The command-line
607 options should include \"-i=mi\" to use gdb's MI text interface.
608 Note that the old \"--annotate\" option is no longer supported.
610 If `gdb-many-windows' is nil (the default value) then gdb just
611 pops up the GUD buffer unless `gdb-show-main' is t. In this case
612 it starts with two windows: one displaying the GUD buffer and the
613 other with the source file with the main routine of the inferior.
615 If `gdb-many-windows' is t, regardless of the value of
616 `gdb-show-main', the layout below will appear. Keybindings are
617 shown in some of the buffers.
619 Watch expressions appear in the speedbar/slowbar.
621 The following commands help control operation :
623 `gdb-many-windows' - Toggle the number of windows gdb uses.
624 `gdb-restore-windows' - To restore the window layout.
626 See Info node `(emacs)GDB Graphical Interface' for a more
627 detailed description of this mode.
630 +----------------------------------------------------------------------+
631 | GDB Toolbar |
632 +-----------------------------------+----------------------------------+
633 | GUD buffer (I/O of GDB) | Locals buffer |
634 | | |
635 | | |
636 | | |
637 +-----------------------------------+----------------------------------+
638 | Source buffer | I/O buffer (of debugged program) |
639 | | (comint-mode) |
640 | | |
641 | | |
642 | | |
643 | | |
644 | | |
645 | | |
646 +-----------------------------------+----------------------------------+
647 | Stack buffer | Breakpoints buffer |
648 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
649 | | RET gdb-goto-breakpoint |
650 | | D gdb-delete-breakpoint |
651 +-----------------------------------+----------------------------------+"
653 (interactive (list (gud-query-cmdline 'gdb)))
655 (when (and gud-comint-buffer
656 (buffer-name gud-comint-buffer)
657 (get-buffer-process gud-comint-buffer)
658 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
659 (gdb-restore-windows)
660 (error
661 "Multiple debugging requires restarting in text command mode"))
663 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
665 ;; Setup a temporary process filter to warn when GDB was not started
666 ;; with -i=mi.
667 (let ((proc (get-buffer-process gud-comint-buffer)))
668 (process-put proc 'gud-normal-filter (process-filter proc))
669 (set-process-filter proc #'gdb--check-interpreter))
671 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
672 (set (make-local-variable 'gdb-control-level) 0)
673 (setq comint-input-sender 'gdb-send)
674 (when (ring-empty-p comint-input-ring) ; cf shell-mode
675 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
676 (if (eq system-type 'ms-dos)
677 "_gdb_history"
678 ".gdb_history"))))
679 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
680 (hsize (getenv "HISTSIZE")))
681 (dolist (file (append '("~/.gdbinit")
682 (unless (string-equal (expand-file-name ".")
683 (expand-file-name "~"))
684 '(".gdbinit"))))
685 (if (file-readable-p (setq file (expand-file-name file)))
686 (with-temp-buffer
687 (insert-file-contents file)
688 ;; TODO? check for "set history save\\( *on\\)?" and do
689 ;; not use history otherwise?
690 (while (re-search-forward
691 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
692 (cond ((string-equal (match-string 1) "filename")
693 (setq hfile (expand-file-name
694 (match-string 2)
695 (file-name-directory file))))
696 ((string-equal (match-string 1) "size")
697 (setq hsize (match-string 2))))))))
698 (and (stringp hsize)
699 (integerp (setq hsize (string-to-number hsize)))
700 (> hsize 0)
701 (set (make-local-variable 'comint-input-ring-size) hsize))
702 (if (stringp hfile)
703 (set (make-local-variable 'comint-input-ring-file-name) hfile))
704 (comint-read-input-ring t)))
705 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
706 "Set temporary breakpoint at current line.")
707 (gud-def gud-jump
708 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
709 "\C-j" "Set execution address to current line.")
711 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
712 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
713 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
714 (gud-def gud-pstar "print* %e" nil
715 "Evaluate C dereferenced pointer expression at point.")
717 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
718 "\C-s"
719 "Step one source line with display.")
720 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
721 "\C-i"
722 "Step one instruction with display.")
723 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
724 "\C-n"
725 "Step one line (skip functions).")
726 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
728 "Step one instruction (skip functions).")
729 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
730 "\C-r"
731 "Continue with display.")
732 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
733 "\C-f"
734 "Finish executing current function.")
735 (gud-def gud-run "-exec-run"
737 "Run the program.")
739 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
740 (gud-call "break %f:%l" arg)
741 (save-excursion
742 (beginning-of-line)
743 (forward-char 2)
744 (gud-call "break *%a" arg)))
745 "\C-b" "Set breakpoint at current line or address.")
747 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
748 (gud-call "clear %f:%l" arg)
749 (save-excursion
750 (beginning-of-line)
751 (forward-char 2)
752 (gud-call "clear *%a" arg)))
753 "\C-d" "Remove breakpoint at current line or address.")
755 ;; -exec-until doesn't support --all yet
756 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
757 (gud-call "-exec-until %f:%l" arg)
758 (save-excursion
759 (beginning-of-line)
760 (forward-char 2)
761 (gud-call "-exec-until *%a" arg)))
762 "\C-u" "Continue to current line or address.")
763 ;; TODO Why arg here?
764 (gud-def
765 gud-go (gud-call (if gdb-active-process
766 (gdb-gud-context-command "-exec-continue")
767 "-exec-run") arg)
768 nil "Start or continue execution.")
770 ;; For debugging Emacs only.
771 (gud-def gud-pp
772 (gud-call
773 (concat
774 "pp " (if (eq (buffer-local-value
775 'major-mode (window-buffer)) 'speedbar-mode)
776 (gdb-find-watch-expression) "%e")) arg)
777 nil "Print the Emacs s-expression.")
779 (define-key gud-minor-mode-map [left-margin mouse-1]
780 'gdb-mouse-set-clear-breakpoint)
781 (define-key gud-minor-mode-map [left-fringe mouse-1]
782 'gdb-mouse-set-clear-breakpoint)
783 (define-key gud-minor-mode-map [left-margin C-mouse-1]
784 'gdb-mouse-toggle-breakpoint-margin)
785 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
786 'gdb-mouse-toggle-breakpoint-fringe)
788 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
789 'gdb-mouse-until)
790 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
791 'gdb-mouse-until)
792 (define-key gud-minor-mode-map [left-margin mouse-3]
793 'gdb-mouse-until)
794 (define-key gud-minor-mode-map [left-fringe mouse-3]
795 'gdb-mouse-until)
797 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
798 'gdb-mouse-jump)
799 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
800 'gdb-mouse-jump)
801 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
802 'gdb-mouse-jump)
803 (define-key gud-minor-mode-map [left-margin C-mouse-3]
804 'gdb-mouse-jump)
806 (set (make-local-variable 'gud-gdb-completion-function)
807 'gud-gdbmi-completions)
809 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
810 nil 'local)
811 (local-set-key "\C-i" 'completion-at-point)
813 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
815 (setq gdb-first-prompt t)
816 (setq gud-running nil)
818 (gdb-update)
820 (run-hooks 'gdb-mode-hook))
822 (defun gdb-init-1 ()
823 ;; (Re-)initialize.
824 (setq gdb-selected-frame nil
825 gdb-frame-number nil
826 gdb-thread-number nil
827 gdb-var-list nil
828 gdb-pending-triggers nil
829 gdb-output-sink 'user
830 gdb-location-alist nil
831 gdb-source-file-list nil
832 gdb-last-command nil
833 gdb-token-number 0
834 gdb-handler-alist '()
835 gdb-handler-number nil
836 gdb-prompt-name nil
837 gdb-first-done-or-error t
838 gdb-buffer-fringe-width (car (window-fringes))
839 gdb-debug-log nil
840 gdb-source-window nil
841 gdb-inferior-status nil
842 gdb-continuation nil
843 gdb-buf-publisher '()
844 gdb-threads-list '()
845 gdb-breakpoints-list '()
846 gdb-register-names '()
847 gdb-non-stop gdb-non-stop-setting)
849 (setq gdb-buffer-type 'gdbmi)
851 (gdb-force-mode-line-update
852 (propertize "initializing..." 'face font-lock-variable-name-face))
854 (gdb-get-buffer-create 'gdb-inferior-io)
855 (gdb-clear-inferior-io)
856 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
858 (when (eq system-type 'windows-nt)
859 ;; Don't create a separate console window for the debuggee.
860 (gdb-input "-gdb-set new-console off" 'ignore)
861 ;; Force GDB to behave as if its input and output stream were
862 ;; connected to a TTY device (since on Windows we use pipes for
863 ;; communicating with GDB).
864 (gdb-input "-gdb-set interactive-mode on" 'ignore))
865 (gdb-input "-gdb-set height 0" 'ignore)
867 (when gdb-non-stop
868 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
870 (gdb-input "-enable-pretty-printing" 'ignore)
872 ;; Find source file and compilation directory here.
873 (if gdb-create-source-file-list
874 ;; Needs GDB 6.2 onwards.
875 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
876 ;; Needs GDB 6.0 onwards.
877 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
878 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
880 (defun gdb-non-stop-handler ()
881 (goto-char (point-min))
882 (if (re-search-forward "No symbol" nil t)
883 (progn
884 (message
885 "This version of GDB doesn't support non-stop mode. Turning it off.")
886 (setq gdb-non-stop nil)
887 (setq gdb-supports-non-stop nil))
888 (setq gdb-supports-non-stop t)
889 (gdb-input "-gdb-set target-async 1" 'ignore)
890 (gdb-input "-list-target-features" 'gdb-check-target-async)))
892 (defun gdb-check-target-async ()
893 (goto-char (point-min))
894 (unless (re-search-forward "async" nil t)
895 (message
896 "Target doesn't support non-stop mode. Turning it off.")
897 (setq gdb-non-stop nil)
898 (gdb-input "-gdb-set non-stop 0" 'ignore)))
900 (defun gdb-delchar-or-quit (arg)
901 "Delete ARG characters or send a quit command to GDB.
902 Send a quit only if point is at the end of the buffer, there is
903 no input, and GDB is waiting for input."
904 (interactive "p")
905 (unless (and (eq (current-buffer) gud-comint-buffer)
906 (eq gud-minor-mode 'gdbmi))
907 (error "Not in a GDB-MI buffer"))
908 (let ((proc (get-buffer-process gud-comint-buffer)))
909 (if (and (eobp) proc (process-live-p proc)
910 (not gud-running)
911 (= (point) (marker-position (process-mark proc))))
912 ;; Sending an EOF does not work with GDB-MI; submit an
913 ;; explicit quit command.
914 (progn
915 (insert "quit")
916 (comint-send-input t t))
917 (delete-char arg))))
919 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
921 (defun gdb-create-define-alist ()
922 "Create an alist of #define directives for GUD tooltips."
923 (let* ((file (buffer-file-name))
924 (output
925 (with-output-to-string
926 (with-current-buffer standard-output
927 (and file
928 (file-exists-p file)
929 ;; call-process doesn't work with remote file names.
930 (not (file-remote-p default-directory))
931 (call-process shell-file-name file
932 (list t nil) nil "-c"
933 (concat gdb-cpp-define-alist-program " "
934 gdb-cpp-define-alist-flags))))))
935 (define-list (split-string output "\n" t))
936 (name))
937 (setq gdb-define-alist nil)
938 (dolist (define define-list)
939 (setq name (nth 1 (split-string define "[( ]")))
940 (push (cons name define) gdb-define-alist))))
942 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
944 (defun gdb-tooltip-print (expr)
945 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
946 (goto-char (point-min))
947 (cond
948 ((re-search-forward ".*value=\\(\".*\"\\)" nil t)
949 (tooltip-show
950 (concat expr " = " (read (match-string 1)))
951 (or gud-tooltip-echo-area
952 (not (display-graphic-p)))))
953 ((re-search-forward "msg=\\(\".+\"\\)$" nil t)
954 (tooltip-show (read (match-string 1))
955 (or gud-tooltip-echo-area
956 (not (display-graphic-p))))))))
958 ;; If expr is a macro for a function don't print because of possible dangerous
959 ;; side-effects. Also printing a function within a tooltip generates an
960 ;; unexpected starting annotation (phase error).
961 (defun gdb-tooltip-print-1 (expr)
962 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
963 (goto-char (point-min))
964 (if (search-forward "expands to: " nil t)
965 (unless (looking-at "\\S-+.*(.*).*")
966 (gdb-input (concat "-data-evaluate-expression \"" expr "\"")
967 `(lambda () (gdb-tooltip-print ,expr)))))))
969 (defun gdb-init-buffer ()
970 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
971 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
972 (when gud-tooltip-mode
973 (make-local-variable 'gdb-define-alist)
974 (gdb-create-define-alist)
975 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
977 (defmacro gdb--if-arrow (arrow-position start-posn end-posn &rest body)
978 (declare (indent 3))
979 (let ((buffer (make-symbol "buffer")))
980 `(if ,arrow-position
981 (let ((,buffer (marker-buffer ,arrow-position)))
982 (if (equal ,buffer (window-buffer (posn-window ,end-posn)))
983 (with-current-buffer ,buffer
984 (when (or (equal ,start-posn ,end-posn)
985 (equal (posn-point ,start-posn)
986 (marker-position ,arrow-position)))
987 ,@body)))))))
989 (defun gdb-mouse-until (event)
990 "Continue running until a source line past the current line.
991 The destination source line can be selected either by clicking
992 with mouse-3 on the fringe/margin or dragging the arrow
993 with mouse-1 (default bindings)."
994 (interactive "e")
995 (let ((start (event-start event))
996 (end (event-end event)))
997 (gdb--if-arrow gud-overlay-arrow-position start end
998 (let ((line (line-number-at-pos (posn-point end))))
999 (gud-call (concat "until " (number-to-string line)))))
1000 (gdb--if-arrow gdb-disassembly-position start end
1001 (save-excursion
1002 (goto-char (point-min))
1003 (forward-line (1- (line-number-at-pos (posn-point end))))
1004 (forward-char 2)
1005 (gud-call (concat "until *%a"))))))
1007 (defun gdb-mouse-jump (event)
1008 "Set execution address/line.
1009 The destination source line can be selected either by clicking with C-mouse-3
1010 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1011 Unlike `gdb-mouse-until' the destination address can be before the current
1012 line, and no execution takes place."
1013 (interactive "e")
1014 (let ((start (event-start event))
1015 (end (event-end event)))
1016 (gdb--if-arrow gud-overlay-arrow-position start end
1017 (let ((line (line-number-at-pos (posn-point end))))
1018 (gud-call (concat "tbreak " (number-to-string line)))
1019 (gud-call (concat "jump " (number-to-string line)))))
1020 (gdb--if-arrow gdb-disassembly-position start end
1021 (save-excursion
1022 (goto-char (point-min))
1023 (forward-line (1- (line-number-at-pos (posn-point end))))
1024 (forward-char 2)
1025 (gud-call (concat "tbreak *%a"))
1026 (gud-call (concat "jump *%a"))))))
1028 (defcustom gdb-show-changed-values t
1029 "If non-nil change the face of out of scope variables and changed values.
1030 Out of scope variables are suppressed with `shadow' face.
1031 Changed values are highlighted with the face `font-lock-warning-face'."
1032 :type 'boolean
1033 :group 'gdb
1034 :version "22.1")
1036 (defcustom gdb-max-children 40
1037 "Maximum number of children before expansion requires confirmation."
1038 :type 'integer
1039 :group 'gdb
1040 :version "22.1")
1042 (defcustom gdb-delete-out-of-scope t
1043 "If non-nil delete watch expressions automatically when they go out of scope."
1044 :type 'boolean
1045 :group 'gdb
1046 :version "22.2")
1048 (define-minor-mode gdb-speedbar-auto-raise
1049 "Minor mode to automatically raise the speedbar for watch expressions.
1050 With prefix argument ARG, automatically raise speedbar if ARG is
1051 positive, otherwise don't automatically raise it."
1052 :global t
1053 :group 'gdb
1054 :version "22.1")
1056 (defcustom gdb-use-colon-colon-notation nil
1057 "If non-nil use FUN::VAR format to display variables in the speedbar."
1058 :type 'boolean
1059 :group 'gdb
1060 :version "22.1")
1062 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1063 (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
1065 (declare-function tooltip-identifier-from-point "tooltip" (point))
1067 (defun gud-watch (&optional arg event)
1068 "Watch expression at point.
1069 With arg, enter name of variable to be watched in the minibuffer."
1070 (interactive (list current-prefix-arg last-input-event))
1071 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1072 (if (eq minor-mode 'gdbmi)
1073 (progn
1074 (if event (posn-set-point (event-end event)))
1075 (require 'tooltip)
1076 (save-selected-window
1077 (let ((expr
1078 (if arg
1079 (completing-read "Name of variable: "
1080 'gud-gdb-complete-command)
1081 (if (and transient-mark-mode mark-active)
1082 (buffer-substring (region-beginning) (region-end))
1083 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1084 (tooltip-identifier-from-point (point)))))))
1085 (set-text-properties 0 (length expr) nil expr)
1086 (gdb-input (concat "-var-create - * " expr "")
1087 `(lambda () (gdb-var-create-handler ,expr))))))
1088 (message "gud-watch is a no-op in this mode."))))
1090 (defun gdb-var-create-handler (expr)
1091 (let* ((result (gdb-json-partial-output)))
1092 (if (not (bindat-get-field result 'msg))
1093 (let ((var
1094 (list (bindat-get-field result 'name)
1095 (if (and (string-equal gdb-current-language "c")
1096 gdb-use-colon-colon-notation gdb-selected-frame)
1097 (setq expr (concat gdb-selected-frame "::" expr))
1098 expr)
1099 (bindat-get-field result 'numchild)
1100 (bindat-get-field result 'type)
1101 (bindat-get-field result 'value)
1103 (bindat-get-field result 'has_more)
1104 gdb-frame-address)))
1105 (push var gdb-var-list)
1106 (speedbar 1)
1107 (unless (string-equal
1108 speedbar-initial-expansion-list-name "GUD")
1109 (speedbar-change-initial-expansion-list "GUD")))
1110 (message-box "No symbol \"%s\" in current context." expr))))
1112 (defun gdb-speedbar-update ()
1113 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1114 (not (gdb-pending-p 'gdb-speedbar-timer)))
1115 ;; Dummy command to update speedbar even when idle.
1116 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn)
1117 ;; Keep gdb-pending-triggers non-nil till end.
1118 (gdb-add-pending 'gdb-speedbar-timer)))
1120 (defun gdb-speedbar-timer-fn ()
1121 (if gdb-speedbar-auto-raise
1122 (raise-frame speedbar-frame))
1123 (gdb-delete-pending 'gdb-speedbar-timer)
1124 (speedbar-timer-fn))
1126 (defun gdb-var-evaluate-expression-handler (varnum changed)
1127 (goto-char (point-min))
1128 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1129 (let ((var (assoc varnum gdb-var-list)))
1130 (when var
1131 (if changed (setcar (nthcdr 5 var) 'changed))
1132 (setcar (nthcdr 4 var) (read (match-string 1)))))
1133 (gdb-speedbar-update))
1135 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1136 (defun gdb-var-list-children (varnum)
1137 (gdb-input (concat "-var-update " varnum) 'ignore)
1138 (gdb-input (concat "-var-list-children --all-values " varnum)
1139 `(lambda () (gdb-var-list-children-handler ,varnum))))
1141 (defun gdb-var-list-children-handler (varnum)
1142 (let* ((var-list nil)
1143 (output (bindat-get-field (gdb-json-partial-output "child")))
1144 (children (bindat-get-field output 'children)))
1145 (catch 'child-already-watched
1146 (dolist (var gdb-var-list)
1147 (if (string-equal varnum (car var))
1148 (progn
1149 ;; With dynamic varobjs numchild may have increased.
1150 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1151 (push var var-list)
1152 (dolist (child children)
1153 (let ((varchild (list (bindat-get-field child 'name)
1154 (bindat-get-field child 'exp)
1155 (bindat-get-field child 'numchild)
1156 (bindat-get-field child 'type)
1157 (bindat-get-field child 'value)
1159 (bindat-get-field child 'has_more))))
1160 (if (assoc (car varchild) gdb-var-list)
1161 (throw 'child-already-watched nil))
1162 (push varchild var-list))))
1163 (push var var-list)))
1164 (setq gdb-var-list (nreverse var-list))))
1165 (gdb-speedbar-update))
1167 (defun gdb-var-set-format (format)
1168 "Set the output format for a variable displayed in the speedbar."
1169 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1170 (varnum (car var)))
1171 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1172 (gdb-var-update)))
1174 (defun gdb-var-delete-1 (var varnum)
1175 (gdb-input (concat "-var-delete " varnum) 'ignore)
1176 (setq gdb-var-list (delq var gdb-var-list))
1177 (dolist (varchild gdb-var-list)
1178 (if (string-match (concat (car var) "\\.") (car varchild))
1179 (setq gdb-var-list (delq varchild gdb-var-list)))))
1181 (defun gdb-var-delete ()
1182 "Delete watch expression at point from the speedbar."
1183 (interactive)
1184 (let ((text (speedbar-line-text)))
1185 (string-match "\\(\\S-+\\)" text)
1186 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1187 (varnum (car var)))
1188 (if (string-match "\\." (car var))
1189 (message-box "Can only delete a root expression")
1190 (gdb-var-delete-1 var varnum)))))
1192 (defun gdb-var-delete-children (varnum)
1193 "Delete children of variable object at point from the speedbar."
1194 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1196 (defun gdb-edit-value (_text _token _indent)
1197 "Assign a value to a variable displayed in the speedbar."
1198 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1199 (varnum (car var))
1200 (value (read-string "New value: ")))
1201 (gdb-input (concat "-var-assign " varnum " " value)
1202 `(lambda () (gdb-edit-value-handler ,value)))))
1204 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1206 (defun gdb-edit-value-handler (value)
1207 (goto-char (point-min))
1208 (if (re-search-forward gdb-error-regexp nil t)
1209 (message-box "Invalid number or expression (%s)" value)))
1211 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1212 (defun gdb-var-update ()
1213 (if (not (gdb-pending-p 'gdb-var-update))
1214 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler))
1215 (gdb-add-pending 'gdb-var-update))
1217 (defun gdb-var-update-handler ()
1218 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1219 (dolist (var gdb-var-list)
1220 (setcar (nthcdr 5 var) nil))
1221 (let ((temp-var-list gdb-var-list))
1222 (dolist (change changelist)
1223 (let* ((varnum (bindat-get-field change 'name))
1224 (var (assoc varnum gdb-var-list))
1225 (new-num (bindat-get-field change 'new_num_children)))
1226 (when var
1227 (let ((scope (bindat-get-field change 'in_scope))
1228 (has-more (bindat-get-field change 'has_more)))
1229 (cond ((string-equal scope "false")
1230 (if gdb-delete-out-of-scope
1231 (gdb-var-delete-1 var varnum)
1232 (setcar (nthcdr 5 var) 'out-of-scope)))
1233 ((string-equal scope "true")
1234 (setcar (nthcdr 6 var) has-more)
1235 (when (and (or (not has-more)
1236 (string-equal has-more "0"))
1237 (not new-num)
1238 (string-equal (nth 2 var) "0"))
1239 (setcar (nthcdr 4 var)
1240 (bindat-get-field change 'value))
1241 (setcar (nthcdr 5 var) 'changed)))
1242 ((string-equal scope "invalid")
1243 (gdb-var-delete-1 var varnum)))))
1244 (let ((var-list nil) var1
1245 (children (bindat-get-field change 'new_children)))
1246 (when new-num
1247 (setq var1 (pop temp-var-list))
1248 (while var1
1249 (if (string-equal varnum (car var1))
1250 (let ((new (string-to-number new-num))
1251 (previous (string-to-number (nth 2 var1))))
1252 (setcar (nthcdr 2 var1) new-num)
1253 (push var1 var-list)
1254 (cond
1255 ((> new previous)
1256 ;; Add new children to list.
1257 (dotimes (dummy previous)
1258 (push (pop temp-var-list) var-list))
1259 (dolist (child children)
1260 (let ((varchild
1261 (list (bindat-get-field child 'name)
1262 (bindat-get-field child 'exp)
1263 (bindat-get-field child 'numchild)
1264 (bindat-get-field child 'type)
1265 (bindat-get-field child 'value)
1266 'changed
1267 (bindat-get-field child 'has_more))))
1268 (push varchild var-list))))
1269 ;; Remove deleted children from list.
1270 ((< new previous)
1271 (dotimes (dummy new)
1272 (push (pop temp-var-list) var-list))
1273 (dotimes (dummy (- previous new))
1274 (pop temp-var-list)))))
1275 (push var1 var-list))
1276 (setq var1 (pop temp-var-list)))
1277 (setq gdb-var-list (nreverse var-list))))))))
1278 (setq gdb-pending-triggers
1279 (delq 'gdb-var-update gdb-pending-triggers))
1280 (gdb-speedbar-update))
1282 (defun gdb-speedbar-expand-node (text token indent)
1283 "Expand the node the user clicked on.
1284 TEXT is the text of the button we clicked on, a + or - item.
1285 TOKEN is data related to this node.
1286 INDENT is the current indentation depth."
1287 (cond ((string-match "+" text) ;expand this node
1288 (let* ((var (assoc token gdb-var-list))
1289 (expr (nth 1 var)) (children (nth 2 var)))
1290 (if (or (<= (string-to-number children) gdb-max-children)
1291 (y-or-n-p
1292 (format "%s has %s children. Continue? " expr children)))
1293 (gdb-var-list-children token))))
1294 ((string-match "-" text) ;contract this node
1295 (dolist (var gdb-var-list)
1296 (if (string-match (concat token "\\.") (car var))
1297 (setq gdb-var-list (delq var gdb-var-list))))
1298 (gdb-var-delete-children token)
1299 (speedbar-change-expand-button-char ?+)
1300 (speedbar-delete-subblock indent))
1301 (t (error "Ooops... not sure what to do")))
1302 (speedbar-center-buffer-smartly))
1304 (defun gdb-get-target-string ()
1305 (with-current-buffer gud-comint-buffer
1306 gud-target-name))
1310 ;; gdb buffers.
1312 ;; Each buffer has a TYPE -- a symbol that identifies the function
1313 ;; of that particular buffer.
1315 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1316 ;; is constructed specially.
1318 ;; Others are constructed by gdb-get-buffer-create and
1319 ;; named according to the rules set forth in the gdb-buffer-rules
1321 (defvar gdb-buffer-rules '())
1323 (defun gdb-rules-name-maker (rules-entry)
1324 (cadr rules-entry))
1325 (defun gdb-rules-buffer-mode (rules-entry)
1326 (nth 2 rules-entry))
1327 (defun gdb-rules-update-trigger (rules-entry)
1328 (nth 3 rules-entry))
1330 (defun gdb-update-buffer-name ()
1331 "Rename current buffer according to name-maker associated with
1332 it in `gdb-buffer-rules'."
1333 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1334 gdb-buffer-rules))))
1335 (when f (rename-buffer (funcall f)))))
1337 (defun gdb-current-buffer-rules ()
1338 "Get `gdb-buffer-rules' entry for current buffer type."
1339 (assoc gdb-buffer-type gdb-buffer-rules))
1341 (defun gdb-current-buffer-thread ()
1342 "Get thread object of current buffer from `gdb-threads-list'.
1344 When current buffer is not bound to any thread, return main
1345 thread."
1346 (cdr (assoc gdb-thread-number gdb-threads-list)))
1348 (defun gdb-current-buffer-frame ()
1349 "Get current stack frame object for thread of current buffer."
1350 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1352 (defun gdb-buffer-type (buffer)
1353 "Get value of `gdb-buffer-type' for BUFFER."
1354 (with-current-buffer buffer
1355 gdb-buffer-type))
1357 (defun gdb-buffer-shows-main-thread-p ()
1358 "Return t if current GDB buffer shows main selected thread and
1359 is not bound to it."
1360 (current-buffer)
1361 (not (local-variable-p 'gdb-thread-number)))
1363 (defun gdb-get-buffer (buffer-type &optional thread)
1364 "Get a specific GDB buffer.
1366 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1367 and `gdb-thread-number' (if provided) must be equal to THREAD."
1368 (catch 'found
1369 (dolist (buffer (buffer-list) nil)
1370 (with-current-buffer buffer
1371 (when (and (eq gdb-buffer-type buffer-type)
1372 (or (not thread)
1373 (equal gdb-thread-number thread)))
1374 (throw 'found buffer))))))
1376 (defun gdb-get-buffer-create (buffer-type &optional thread)
1377 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1378 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1380 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1381 buffer-local variable of the new buffer.
1383 Buffer mode and name are selected according to buffer type.
1385 If buffer has trigger associated with it in `gdb-buffer-rules',
1386 this trigger is subscribed to `gdb-buf-publisher' and called with
1387 'update argument."
1388 (or (gdb-get-buffer buffer-type thread)
1389 (let ((rules (assoc buffer-type gdb-buffer-rules))
1390 (new (generate-new-buffer "limbo")))
1391 (with-current-buffer new
1392 (let ((mode (gdb-rules-buffer-mode rules))
1393 (trigger (gdb-rules-update-trigger rules)))
1394 (when mode (funcall mode))
1395 (setq gdb-buffer-type buffer-type)
1396 (when thread
1397 (set (make-local-variable 'gdb-thread-number) thread))
1398 (set (make-local-variable 'gud-minor-mode)
1399 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1400 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1401 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1402 (when trigger
1403 (gdb-add-subscriber gdb-buf-publisher
1404 (cons (current-buffer)
1405 (gdb-bind-function-to-buffer
1406 trigger (current-buffer))))
1407 (funcall trigger 'start))
1408 (current-buffer))))))
1410 (defun gdb-bind-function-to-buffer (expr buffer)
1411 "Return a function which will evaluate EXPR in BUFFER."
1412 `(lambda (&rest args)
1413 (with-current-buffer ,buffer
1414 (apply ',expr args))))
1416 ;; Used to display windows with thread-bound buffers
1417 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1418 split-horizontal)
1419 `(defun ,name (&optional thread)
1420 ,(when doc doc)
1421 (message thread)
1422 (gdb-preempt-existing-or-display-buffer
1423 (gdb-get-buffer-create ,buffer thread)
1424 ,split-horizontal)))
1426 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1427 ;; at least one and possible more functions. The functions have these
1428 ;; roles in defining a buffer type:
1430 ;; NAME - Return a name for this buffer type.
1432 ;; The remaining function(s) are optional:
1434 ;; MODE - called in a new buffer with no arguments, should establish
1435 ;; the proper mode for the buffer.
1438 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1439 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1440 (if binding
1441 (setcdr binding rules)
1442 (push (cons buffer-type rules)
1443 gdb-buffer-rules))))
1445 (defun gdb-parent-mode ()
1446 "Generic mode to derive all other GDB buffer modes from."
1447 (kill-all-local-variables)
1448 (setq buffer-read-only t)
1449 (buffer-disable-undo)
1450 ;; Delete buffer from gdb-buf-publisher when it's killed
1451 ;; (if it has an associated update trigger)
1452 (add-hook
1453 'kill-buffer-hook
1454 (function
1455 (lambda ()
1456 (let ((trigger (gdb-rules-update-trigger
1457 (gdb-current-buffer-rules))))
1458 (when trigger
1459 (gdb-delete-subscriber
1460 gdb-buf-publisher
1461 ;; This should match gdb-add-subscriber done in
1462 ;; gdb-get-buffer-create
1463 (cons (current-buffer)
1464 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1465 nil t))
1467 ;; Partial-output buffer : This accumulates output from a command executed on
1468 ;; behalf of emacs (rather than the user).
1470 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1471 'gdb-partial-output-name)
1473 (defun gdb-partial-output-name ()
1474 (concat " *partial-output-"
1475 (gdb-get-target-string)
1476 "*"))
1479 (gdb-set-buffer-rules 'gdb-inferior-io
1480 'gdb-inferior-io-name
1481 'gdb-inferior-io-mode)
1483 (defun gdb-inferior-io-name ()
1484 (concat "*input/output of "
1485 (gdb-get-target-string)
1486 "*"))
1488 (defun gdb-display-io-buffer ()
1489 "Display IO of debugged program in a separate window."
1490 (interactive)
1491 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
1493 (defun gdb-inferior-io--init-proc (proc)
1494 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1495 (set-process-filter proc 'gdb-inferior-filter)
1496 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1497 ;; The process can run on a remote host.
1498 (let ((tty (or (process-get proc 'remote-tty)
1499 (process-tty-name proc))))
1500 (unless (or (null tty)
1501 (string= tty ""))
1502 (gdb-input
1503 (concat "-inferior-tty-set " tty) 'ignore))))
1505 (defun gdb-inferior-io-sentinel (proc str)
1506 (when (eq (process-status proc) 'failed)
1507 ;; When the debugged process exits, Emacs gets an EIO error on
1508 ;; read from the pty, and stops listening to it. If the gdb
1509 ;; process is still running, remove the pty, make a new one, and
1510 ;; pass it to gdb.
1511 (let ((gdb-proc (get-buffer-process gud-comint-buffer))
1512 (io-buffer (process-buffer proc)))
1513 (when (and gdb-proc (process-live-p gdb-proc)
1514 (buffer-live-p io-buffer))
1515 ;; `comint-exec' deletes the original process as a side effect.
1516 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1517 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1519 (defcustom gdb-display-buffer-other-frame-action
1520 '((display-buffer-reuse-window display-buffer-pop-up-frame)
1521 (reusable-frames . visible)
1522 (inhibit-same-window . t)
1523 (pop-up-frame-parameters (height . 14)
1524 (width . 80)
1525 (unsplittable . t)
1526 (tool-bar-lines . nil)
1527 (menu-bar-lines . nil)
1528 (minibuffer . nil)))
1529 "`display-buffer' action for displaying GDB utility frames."
1530 :group 'gdb
1531 :type display-buffer--action-custom-type
1532 :risky t
1533 :version "24.3")
1535 (defun gdb-frame-io-buffer ()
1536 "Display IO of debugged program in another frame."
1537 (interactive)
1538 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1539 gdb-display-buffer-other-frame-action))
1541 (defvar gdb-inferior-io-mode-map
1542 (let ((map (make-sparse-keymap)))
1543 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1544 (define-key map "\C-c\C-z" 'gdb-io-stop)
1545 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1546 (define-key map "\C-c\C-d" 'gdb-io-eof)
1547 (define-key map "\C-d" 'gdb-io-eof)
1548 map))
1550 ;; We want to use comint because it has various nifty and familiar features.
1551 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1552 "Major mode for gdb inferior-io."
1553 :syntax-table nil :abbrev-table nil
1554 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1556 (defun gdb-inferior-filter (proc string)
1557 (unless (string-equal string "")
1558 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
1559 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1560 (comint-output-filter proc string)))
1562 (defun gdb-io-interrupt ()
1563 "Interrupt the program being debugged."
1564 (interactive)
1565 (interrupt-process
1566 (get-buffer-process gud-comint-buffer) comint-ptyp))
1568 (defun gdb-io-quit ()
1569 "Send quit signal to the program being debugged."
1570 (interactive)
1571 (quit-process
1572 (get-buffer-process gud-comint-buffer) comint-ptyp))
1574 (defun gdb-io-stop ()
1575 "Stop the program being debugged."
1576 (interactive)
1577 (stop-process
1578 (get-buffer-process gud-comint-buffer) comint-ptyp))
1580 (defun gdb-io-eof ()
1581 "Send end-of-file to the program being debugged."
1582 (interactive)
1583 (process-send-eof
1584 (get-buffer-process gud-comint-buffer)))
1586 (defun gdb-clear-inferior-io ()
1587 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1588 (erase-buffer)))
1591 (defconst breakpoint-xpm-data
1592 "/* XPM */
1593 static char *magick[] = {
1594 /* columns rows colors chars-per-pixel */
1595 \"10 10 2 1\",
1596 \" c red\",
1597 \"+ c None\",
1598 /* pixels */
1599 \"+++ +++\",
1600 \"++ ++\",
1601 \"+ +\",
1602 \" \",
1603 \" \",
1604 \" \",
1605 \" \",
1606 \"+ +\",
1607 \"++ ++\",
1608 \"+++ +++\",
1610 "XPM data used for breakpoint icon.")
1612 (defconst breakpoint-enabled-pbm-data
1614 10 10\",
1615 0 0 0 0 1 1 1 1 0 0 0 0
1616 0 0 0 1 1 1 1 1 1 0 0 0
1617 0 0 1 1 1 1 1 1 1 1 0 0
1618 0 1 1 1 1 1 1 1 1 1 1 0
1619 0 1 1 1 1 1 1 1 1 1 1 0
1620 0 1 1 1 1 1 1 1 1 1 1 0
1621 0 1 1 1 1 1 1 1 1 1 1 0
1622 0 0 1 1 1 1 1 1 1 1 0 0
1623 0 0 0 1 1 1 1 1 1 0 0 0
1624 0 0 0 0 1 1 1 1 0 0 0 0"
1625 "PBM data used for enabled breakpoint icon.")
1627 (defconst breakpoint-disabled-pbm-data
1629 10 10\",
1630 0 0 1 0 1 0 1 0 0 0
1631 0 1 0 1 0 1 0 1 0 0
1632 1 0 1 0 1 0 1 0 1 0
1633 0 1 0 1 0 1 0 1 0 1
1634 1 0 1 0 1 0 1 0 1 0
1635 0 1 0 1 0 1 0 1 0 1
1636 1 0 1 0 1 0 1 0 1 0
1637 0 1 0 1 0 1 0 1 0 1
1638 0 0 1 0 1 0 1 0 1 0
1639 0 0 0 1 0 1 0 1 0 0"
1640 "PBM data used for disabled breakpoint icon.")
1642 (defvar breakpoint-enabled-icon nil
1643 "Icon for enabled breakpoint in display margin.")
1645 (defvar breakpoint-disabled-icon nil
1646 "Icon for disabled breakpoint in display margin.")
1648 (declare-function define-fringe-bitmap "fringe.c"
1649 (bitmap bits &optional height width align))
1651 (and (display-images-p)
1652 ;; Bitmap for breakpoint in fringe
1653 (define-fringe-bitmap 'breakpoint
1654 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1655 ;; Bitmap for gud-overlay-arrow in fringe
1656 (define-fringe-bitmap 'hollow-right-triangle
1657 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1659 (defface breakpoint-enabled
1660 '((t
1661 :foreground "red1"
1662 :weight bold))
1663 "Face for enabled breakpoint icon in fringe."
1664 :group 'gdb)
1666 (defface breakpoint-disabled
1667 '((((class color) (min-colors 88)) :foreground "grey70")
1668 ;; Ensure that on low-color displays that we end up something visible.
1669 (((class color) (min-colors 8) (background light))
1670 :foreground "black")
1671 (((class color) (min-colors 8) (background dark))
1672 :foreground "white")
1673 (((type tty) (class mono))
1674 :inverse-video t)
1675 (t :background "gray"))
1676 "Face for disabled breakpoint icon in fringe."
1677 :group 'gdb)
1680 (defvar gdb-control-commands-regexp
1681 (concat
1682 "^\\("
1683 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1684 "while-stepping\\|stepping\\|ws\\|actions"
1685 "\\)\\([[:blank:]]+.*\\)?$")
1686 "Regexp matching GDB commands that enter a recursive reading loop.
1687 As long as GDB is in the recursive reading loop, it does not expect
1688 commands to be prefixed by \"-interpreter-exec console\".")
1690 (defun gdb-send (proc string)
1691 "A comint send filter for gdb."
1692 (with-current-buffer gud-comint-buffer
1693 (let ((inhibit-read-only t))
1694 (remove-text-properties (point-min) (point-max) '(face))))
1695 ;; mimic <RET> key to repeat previous command in GDB
1696 (if (not (string= "" string))
1697 (setq gdb-last-command string)
1698 (if gdb-last-command (setq string gdb-last-command)))
1699 (if (or (string-match "^-" string)
1700 (> gdb-control-level 0))
1701 ;; Either MI command or we are feeding GDB's recursive reading loop.
1702 (progn
1703 (setq gdb-first-done-or-error t)
1704 (process-send-string proc (concat string "\n"))
1705 (if (and (string-match "^end$" string)
1706 (> gdb-control-level 0))
1707 (setq gdb-control-level (1- gdb-control-level))))
1708 ;; CLI command
1709 (if (string-match "\\\\$" string)
1710 (setq gdb-continuation (concat gdb-continuation string "\n"))
1711 (setq gdb-first-done-or-error t)
1712 (let ((to-send (concat "-interpreter-exec console "
1713 (gdb-mi-quote string)
1714 "\n")))
1715 (if gdb-enable-debug
1716 (push (cons 'mi-send to-send) gdb-debug-log))
1717 (process-send-string proc to-send))
1718 (if (and (string-match "^end$" string)
1719 (> gdb-control-level 0))
1720 (setq gdb-control-level (1- gdb-control-level)))
1721 (setq gdb-continuation nil)))
1722 (if (string-match gdb-control-commands-regexp string)
1723 (setq gdb-control-level (1+ gdb-control-level))))
1725 (defun gdb-mi-quote (string)
1726 "Return STRING quoted properly as an MI argument.
1727 The string is enclosed in double quotes.
1728 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1729 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1730 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1731 (concat "\"" string "\""))
1733 (defun gdb-input (command handler-function)
1734 "Send COMMAND to GDB via the MI interface.
1735 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1736 complete."
1737 (if gdb-enable-debug (push (list 'send-item command handler-function)
1738 gdb-debug-log))
1739 (setq gdb-token-number (1+ gdb-token-number))
1740 (setq command (concat (number-to-string gdb-token-number) command))
1741 (push (cons gdb-token-number handler-function) gdb-handler-alist)
1742 (process-send-string (get-buffer-process gud-comint-buffer)
1743 (concat command "\n")))
1745 ;; NOFRAME is used for gud execution control commands
1746 (defun gdb-current-context-command (command)
1747 "Add --thread to gdb COMMAND when needed."
1748 (if (and gdb-thread-number
1749 gdb-supports-non-stop)
1750 (concat command " --thread " gdb-thread-number)
1751 command))
1753 (defun gdb-current-context-buffer-name (name)
1754 "Add thread information and asterisks to string NAME.
1756 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1757 (concat "*" name
1758 (if (local-variable-p 'gdb-thread-number)
1759 (format " (bound to thread %s)" gdb-thread-number)
1761 "*"))
1763 (defun gdb-current-context-mode-name (mode)
1764 "Add thread information to MODE which is to be used as
1765 `mode-name'."
1766 (concat mode
1767 (if gdb-thread-number
1768 (format " [thread %s]" gdb-thread-number)
1769 "")))
1772 (defcustom gud-gdb-command-name "gdb -i=mi"
1773 "Default command to execute an executable under the GDB debugger."
1774 :type 'string
1775 :group 'gdb)
1777 (defun gdb-resync()
1778 (setq gud-running nil)
1779 (setq gdb-output-sink 'user)
1780 (setq gdb-pending-triggers nil))
1782 (defun gdb-update (&optional no-proc)
1783 "Update buffers showing status of debug session.
1784 If NO-PROC is non-nil, do not try to contact the GDB process."
1785 (when gdb-first-prompt
1786 (gdb-force-mode-line-update
1787 (propertize "initializing..." 'face font-lock-variable-name-face))
1788 (gdb-init-1)
1789 (setq gdb-first-prompt nil))
1791 (unless no-proc
1792 (gdb-get-main-selected-frame))
1794 ;; We may need to update gdb-threads-list so we can use
1795 (gdb-get-buffer-create 'gdb-threads-buffer)
1796 ;; gdb-break-list is maintained in breakpoints handler
1797 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1799 (unless no-proc
1800 (gdb-emit-signal gdb-buf-publisher 'update))
1802 (gdb-get-changed-registers)
1803 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1804 (dolist (var gdb-var-list)
1805 (setcar (nthcdr 5 var) nil))
1806 (gdb-var-update)))
1808 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1809 ;; because we may need to update current gud-running value without
1810 ;; changing current thread (see gdb-running)
1811 (defun gdb-setq-thread-number (number)
1812 "Only this function must be used to change `gdb-thread-number'
1813 value to NUMBER, because `gud-running' and `gdb-frame-number'
1814 need to be updated appropriately when current thread changes."
1815 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1816 (unless (string-equal number "0") (setq gdb-thread-number number))
1817 (setq gdb-frame-number "0")
1818 (gdb-update-gud-running))
1820 (defun gdb-update-gud-running ()
1821 "Set `gud-running' according to the state of current thread.
1823 `gdb-frame-number' is set to 0 if current thread is now stopped.
1825 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1826 cannot be reliably used to determine whether or not execution
1827 control buttons should be shown in menu or toolbar. Use
1828 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1829 instead.
1831 For all-stop mode, thread information is unavailable while target
1832 is running."
1833 (let ((old-value gud-running))
1834 (setq gud-running
1835 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1836 "running"))
1837 ;; Set frame number to "0" when _current_ threads stops.
1838 (when (and (gdb-current-buffer-thread)
1839 (not (eq gud-running old-value)))
1840 (setq gdb-frame-number "0"))))
1842 (defun gdb-show-run-p ()
1843 "Return t if \"Run/continue\" should be shown on the toolbar."
1844 (or (not gdb-active-process)
1845 (and (or
1846 (not gdb-gud-control-all-threads)
1847 (not gdb-non-stop))
1848 (not gud-running))
1849 (and gdb-gud-control-all-threads
1850 (> gdb-stopped-threads-count 0))))
1852 (defun gdb-show-stop-p ()
1853 "Return t if \"Stop\" should be shown on the toolbar."
1854 (or (and (or
1855 (not gdb-gud-control-all-threads)
1856 (not gdb-non-stop))
1857 gud-running)
1858 (and gdb-gud-control-all-threads
1859 (> gdb-running-threads-count 0))))
1861 ;; GUD displays the selected GDB frame. This might might not be the current
1862 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1863 ;; visited breakpoint is, use that window.
1864 (defun gdb-display-source-buffer (buffer)
1865 (let* ((last-window (if gud-last-last-frame
1866 (get-buffer-window
1867 (gud-find-file (car gud-last-last-frame)))))
1868 (source-window (or last-window
1869 (if (and gdb-source-window
1870 (window-live-p gdb-source-window))
1871 gdb-source-window))))
1872 (when source-window
1873 (setq gdb-source-window source-window)
1874 (set-window-buffer source-window buffer))
1875 source-window))
1877 (defun gdb-car< (a b)
1878 (< (car a) (car b)))
1880 (defvar gdbmi-record-list
1881 '((gdb-gdb . "(gdb) \n")
1882 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1883 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1884 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1885 (gdb-console . "~\\(\".*?\"\\)\n")
1886 (gdb-internals . "&\\(\".*?\"\\)\n")
1887 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1888 (gdb-running . "\\*running,\\(.*?\n\\)")
1889 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1890 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1891 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1892 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1893 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1895 (defun gud-gdbmi-marker-filter (string)
1896 "Filter GDB/MI output."
1898 ;; Record transactions if logging is enabled.
1899 (when gdb-enable-debug
1900 (push (cons 'recv string) gdb-debug-log)
1901 (if (and gdb-debug-log-max
1902 (> (length gdb-debug-log) gdb-debug-log-max))
1903 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1905 ;; Recall the left over gud-marker-acc from last time.
1906 (setq gud-marker-acc (concat gud-marker-acc string))
1908 ;; Start accumulating output for the GUD buffer.
1909 (setq gdb-filter-output "")
1910 (let (output-record-list)
1912 ;; Process all the complete markers in this chunk.
1913 (dolist (gdbmi-record gdbmi-record-list)
1914 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1915 (push (list (match-beginning 0)
1916 (car gdbmi-record)
1917 (match-string 1 gud-marker-acc)
1918 (match-string 2 gud-marker-acc)
1919 (match-end 0))
1920 output-record-list)
1921 (setq gud-marker-acc
1922 (concat (substring gud-marker-acc 0 (match-beginning 0))
1923 ;; Pad with spaces to preserve position.
1924 (make-string (length (match-string 0 gud-marker-acc)) 32)
1925 (substring gud-marker-acc (match-end 0))))))
1927 (setq output-record-list (sort output-record-list 'gdb-car<))
1929 (dolist (output-record output-record-list)
1930 (let ((record-type (cadr output-record))
1931 (arg1 (nth 2 output-record))
1932 (arg2 (nth 3 output-record)))
1933 (cond ((eq record-type 'gdb-error)
1934 (gdb-done-or-error arg2 arg1 'error))
1935 ((eq record-type 'gdb-done)
1936 (gdb-done-or-error arg2 arg1 'done))
1937 ;; Suppress "No registers." GDB 6.8 and earlier
1938 ;; duplicates MI error message on internal stream.
1939 ;; Don't print to GUD buffer.
1940 ((not (and (eq record-type 'gdb-internals)
1941 (string-equal (read arg1) "No registers.\n")))
1942 (funcall record-type arg1)))))
1944 (setq gdb-output-sink 'user)
1945 ;; Remove padding.
1946 (string-match "^ *" gud-marker-acc)
1947 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1949 gdb-filter-output))
1951 (defun gdb-gdb (_output-field))
1953 (defun gdb-shell (output-field)
1954 (setq gdb-filter-output
1955 (concat output-field gdb-filter-output)))
1957 (defun gdb-ignored-notification (_output-field))
1959 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1960 (defun gdb-thread-created (_output-field))
1961 (defun gdb-thread-exited (output-field)
1962 "Handle =thread-exited async record: unset `gdb-thread-number'
1963 if current thread exited and update threads list."
1964 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1965 (if (string= gdb-thread-number thread-id)
1966 (gdb-setq-thread-number nil))
1967 ;; When we continue current thread and it quickly exits,
1968 ;; gdb-pending-triggers left after gdb-running disallow us to
1969 ;; properly call -thread-info without --thread option. Thus we
1970 ;; need to use gdb-wait-for-pending.
1971 (gdb-wait-for-pending
1972 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1974 (defun gdb-thread-selected (output-field)
1975 "Handler for =thread-selected MI output record.
1977 Sets `gdb-thread-number' to new id."
1978 (let* ((result (gdb-json-string output-field))
1979 (thread-id (bindat-get-field result 'id)))
1980 (gdb-setq-thread-number thread-id)
1981 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1982 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1983 ;; as usually. Things happen to fast and second call (from
1984 ;; gdb-thread-selected handler) gets cut off by our beloved
1985 ;; gdb-pending-triggers.
1986 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1987 ;; body will get executed when `gdb-pending-triggers` is empty.
1988 (gdb-wait-for-pending
1989 (gdb-update))))
1991 (defun gdb-running (output-field)
1992 (let* ((thread-id
1993 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
1994 ;; We reset gdb-frame-number to nil if current thread has gone
1995 ;; running. This can't be done in gdb-thread-list-handler-custom
1996 ;; because we need correct gdb-frame-number by the time
1997 ;; -thread-info command is sent.
1998 (when (or (string-equal thread-id "all")
1999 (string-equal thread-id gdb-thread-number))
2000 (setq gdb-frame-number nil)))
2001 (setq gdb-inferior-status "running")
2002 (gdb-force-mode-line-update
2003 (propertize gdb-inferior-status 'face font-lock-type-face))
2004 (when (not gdb-non-stop)
2005 (setq gud-running t))
2006 (setq gdb-active-process t)
2007 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2009 (defun gdb-starting (_output-field)
2010 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2011 (setq gdb-inferior-status "running")
2012 (gdb-force-mode-line-update
2013 (propertize gdb-inferior-status 'face font-lock-type-face))
2014 (setq gdb-active-process t)
2015 (setq gud-running t)
2016 ;; GDB doesn't seem to respond to -thread-info before first stop or
2017 ;; thread exit (even in non-stop mode), so this is useless.
2018 ;; Behavior may change in the future.
2019 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2021 ;; -break-insert -t didn't give a reason before gdb 6.9
2023 (defun gdb-stopped (output-field)
2024 "Given the contents of *stopped MI async record, select new
2025 current thread and update GDB buffers."
2026 ;; Reason is available with target-async only
2027 (let* ((result (gdb-json-string output-field))
2028 (reason (bindat-get-field result 'reason))
2029 (thread-id (bindat-get-field result 'thread-id)))
2031 ;; -data-list-register-names needs to be issued for any stopped
2032 ;; thread
2033 (when (not gdb-register-names)
2034 (gdb-input (concat "-data-list-register-names"
2035 (if gdb-supports-non-stop
2036 (concat " --thread " thread-id)))
2037 'gdb-register-names-handler))
2039 ;; Don't set gud-last-frame here as it's currently done in
2040 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2041 ;; with CLI.
2042 ;;(when file
2043 ;; (setq
2044 ;; ;; Extract the frame position from the marker.
2045 ;; gud-last-frame (cons file
2046 ;; (string-to-number
2047 ;; (match-string 6 gud-marker-acc)))))
2049 (setq gdb-inferior-status (or reason "unknown"))
2050 (gdb-force-mode-line-update
2051 (propertize gdb-inferior-status 'face font-lock-warning-face))
2052 (if (string-equal reason "exited-normally")
2053 (setq gdb-active-process nil))
2055 ;; Select new current thread.
2057 ;; Don't switch if we have no reasons selected
2058 (when gdb-switch-reasons
2059 ;; Switch from another stopped thread only if we have
2060 ;; gdb-switch-when-another-stopped:
2061 (when (or gdb-switch-when-another-stopped
2062 (not (string= "stopped"
2063 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2064 ;; Switch if current reason has been selected or we have no
2065 ;; reasons
2066 (if (or (eq gdb-switch-reasons t)
2067 (member reason gdb-switch-reasons))
2068 (when (not (string-equal gdb-thread-number thread-id))
2069 (message (concat "Switched to thread " thread-id))
2070 (gdb-setq-thread-number thread-id))
2071 (message (format "Thread %s stopped" thread-id)))))
2073 ;; Print "(gdb)" to GUD console
2074 (when gdb-first-done-or-error
2075 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2077 ;; In non-stop, we update information as soon as another thread gets
2078 ;; stopped
2079 (when (or gdb-first-done-or-error
2080 gdb-non-stop)
2081 ;; In all-stop this updates gud-running properly as well.
2082 (gdb-update)
2083 (setq gdb-first-done-or-error nil))
2084 (run-hook-with-args 'gdb-stopped-functions result)))
2086 ;; Remove the trimmings from log stream containing debugging messages
2087 ;; being produced by GDB's internals, use warning face and send to GUD
2088 ;; buffer.
2089 (defun gdb-internals (output-field)
2090 (setq gdb-filter-output
2091 (gdb-concat-output
2092 gdb-filter-output
2093 (if (string= output-field "\"\\n\"")
2095 (let ((error-message
2096 (read output-field)))
2097 (put-text-property
2098 0 (length error-message)
2099 'face font-lock-warning-face
2100 error-message)
2101 error-message)))))
2103 ;; Remove the trimmings from the console stream and send to GUD buffer
2104 ;; (frontend MI commands should not print to this stream)
2105 (defun gdb-console (output-field)
2106 (setq gdb-filter-output
2107 (gdb-concat-output gdb-filter-output (read output-field))))
2109 (defun gdb-done-or-error (output-field token-number type)
2110 (if (string-equal token-number "")
2111 ;; Output from command entered by user
2112 (progn
2113 (setq gdb-output-sink 'user)
2114 (setq token-number nil)
2115 ;; MI error - send to minibuffer
2116 (when (eq type 'error)
2117 ;; Skip "msg=" from `output-field'
2118 (message (read (substring output-field 4)))
2119 ;; Don't send to the console twice. (If it is a console error
2120 ;; it is also in the console stream.)
2121 (setq output-field nil)))
2122 ;; Output from command from frontend.
2123 (setq gdb-output-sink 'emacs))
2125 (gdb-clear-partial-output)
2127 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2128 (let* ((proc (get-buffer-process gud-comint-buffer))
2129 (no-proc (or (null proc)
2130 (memq (process-status proc) '(exit signal)))))
2132 (when gdb-first-done-or-error
2133 (unless (or token-number gud-running no-proc)
2134 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2135 (gdb-update no-proc)
2136 (setq gdb-first-done-or-error nil))
2138 (setq gdb-filter-output
2139 (gdb-concat-output gdb-filter-output output-field))
2141 (when token-number
2142 (with-current-buffer
2143 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2144 (funcall
2145 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2146 (setq gdb-handler-alist
2147 (assq-delete-all token-number gdb-handler-alist)))))
2149 (defun gdb-concat-output (so-far new)
2150 (cond
2151 ((eq gdb-output-sink 'user) (concat so-far new))
2152 ((eq gdb-output-sink 'emacs)
2153 (gdb-append-to-partial-output new)
2154 so-far)))
2156 (defun gdb-append-to-partial-output (string)
2157 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2158 (goto-char (point-max))
2159 (insert string)))
2161 (defun gdb-clear-partial-output ()
2162 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2163 (erase-buffer)))
2165 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2166 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2168 Field names are wrapped in double quotes and equal signs are
2169 replaced with semicolons.
2171 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2172 partial output. This is used to get rid of useless keys in lists
2173 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2174 -break-info are examples of MI commands which issue such
2175 responses.
2177 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2178 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2179 -break-info output when it contains breakpoint script field
2180 incompatible with GDB/MI output syntax."
2181 (save-excursion
2182 (goto-char (point-min))
2183 (when fix-key
2184 (save-excursion
2185 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2186 (replace-match "" nil nil nil 1))))
2187 (when fix-list
2188 (save-excursion
2189 ;; Find positions of braces which enclose broken list
2190 (while (re-search-forward (concat fix-list "={\"") nil t)
2191 (let ((p1 (goto-char (- (point) 2)))
2192 (p2 (progn (forward-sexp)
2193 (1- (point)))))
2194 ;; Replace braces with brackets
2195 (save-excursion
2196 (goto-char p1)
2197 (delete-char 1)
2198 (insert "[")
2199 (goto-char p2)
2200 (delete-char 1)
2201 (insert "]"))))))
2202 (goto-char (point-min))
2203 (insert "{")
2204 (while (re-search-forward
2205 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2206 (replace-match "\"\\1\":\\2" nil nil))
2207 (goto-char (point-max))
2208 (insert "}")))
2210 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2211 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2213 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2214 (gdb-jsonify-buffer fix-key fix-list)
2215 (save-excursion
2216 (goto-char (point-min))
2217 (let ((json-array-type 'list))
2218 (json-read))))
2220 (defun gdb-json-string (string &optional fix-key fix-list)
2221 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2223 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2224 (with-temp-buffer
2225 (insert string)
2226 (gdb-json-read-buffer fix-key fix-list)))
2228 (defun gdb-json-partial-output (&optional fix-key fix-list)
2229 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2231 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2232 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2233 (gdb-json-read-buffer fix-key fix-list)))
2235 (defun gdb-line-posns (line)
2236 "Return a pair of LINE beginning and end positions."
2237 (let ((offset (1+ (- line (line-number-at-pos)))))
2238 (cons
2239 (line-beginning-position offset)
2240 (line-end-position offset))))
2242 (defmacro gdb-mark-line (line variable)
2243 "Set VARIABLE marker to point at beginning of LINE.
2245 If current window has no fringes, inverse colors on LINE.
2247 Return position where LINE begins."
2248 `(save-excursion
2249 (let* ((posns (gdb-line-posns ,line))
2250 (start-posn (car posns))
2251 (end-posn (cdr posns)))
2252 (set-marker ,variable (copy-marker start-posn))
2253 (when (not (> (car (window-fringes)) 0))
2254 (put-text-property start-posn end-posn
2255 'font-lock-face '(:inverse-video t)))
2256 start-posn)))
2258 (defun gdb-pad-string (string padding)
2259 (format (concat "%" (number-to-string padding) "s") string))
2261 ;; gdb-table struct is a way to programmatically construct simple
2262 ;; tables. It help to reliably align columns of data in GDB buffers
2263 ;; and provides
2264 (cl-defstruct gdb-table
2265 (column-sizes nil)
2266 (rows nil)
2267 (row-properties nil)
2268 (right-align nil))
2270 (defun gdb-mapcar* (function &rest seqs)
2271 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2272 If there are several SEQS, FUNCTION is called with that many
2273 arguments, and mapping stops as soon as the shortest list runs
2274 out."
2275 (let ((shortest (apply #'min (mapcar #'length seqs))))
2276 (mapcar (lambda (i)
2277 (apply function
2278 (mapcar
2279 (lambda (seq)
2280 (nth i seq))
2281 seqs)))
2282 (number-sequence 0 (1- shortest)))))
2284 (defun gdb-table-add-row (table row &optional properties)
2285 "Add ROW of string to TABLE and recalculate column sizes.
2287 When non-nil, PROPERTIES will be added to the whole row when
2288 calling `gdb-table-string'."
2289 (let ((rows (gdb-table-rows table))
2290 (row-properties (gdb-table-row-properties table))
2291 (column-sizes (gdb-table-column-sizes table))
2292 (right-align (gdb-table-right-align table)))
2293 (when (not column-sizes)
2294 (setf (gdb-table-column-sizes table)
2295 (make-list (length row) 0)))
2296 (setf (gdb-table-rows table)
2297 (append rows (list row)))
2298 (setf (gdb-table-row-properties table)
2299 (append row-properties (list properties)))
2300 (setf (gdb-table-column-sizes table)
2301 (gdb-mapcar* (lambda (x s)
2302 (let ((new-x
2303 (max (abs x) (string-width (or s "")))))
2304 (if right-align new-x (- new-x))))
2305 (gdb-table-column-sizes table)
2306 row))
2307 ;; Avoid trailing whitespace at eol
2308 (if (not (gdb-table-right-align table))
2309 (setcar (last (gdb-table-column-sizes table)) 0))))
2311 (defun gdb-table-string (table &optional sep)
2312 "Return TABLE as a string with columns separated with SEP."
2313 (let ((column-sizes (gdb-table-column-sizes table)))
2314 (mapconcat
2315 'identity
2316 (gdb-mapcar*
2317 (lambda (row properties)
2318 (apply 'propertize
2319 (mapconcat 'identity
2320 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2321 row column-sizes)
2322 sep)
2323 properties))
2324 (gdb-table-rows table)
2325 (gdb-table-row-properties table))
2326 "\n")))
2328 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2329 (defun gdb-get-many-fields (struct &rest fields)
2330 "Return a list of FIELDS values from STRUCT."
2331 (let ((values))
2332 (dolist (field fields)
2333 (push (bindat-get-field struct field) values))
2334 (nreverse values)))
2336 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2337 handler-name
2338 &optional signal-list)
2339 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2340 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2341 buffer with `gdb-bind-function-to-buffer'.
2343 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2344 defined trigger is called with an argument from SIGNAL-LIST. It's
2345 not recommended to define triggers with empty SIGNAL-LIST.
2346 Normally triggers should respond at least to 'update signal.
2348 Normally the trigger defined by this command must be called from
2349 the buffer where HANDLER-NAME must work. This should be done so
2350 that buffer-local thread number may be used in GDB-COMMAND (by
2351 calling `gdb-current-context-command').
2352 `gdb-bind-function-to-buffer' is used to achieve this, see
2353 `gdb-get-buffer-create'.
2355 Triggers defined by this command are meant to be used as a
2356 trigger argument when describing buffer types with
2357 `gdb-set-buffer-rules'."
2358 `(defun ,trigger-name (&optional signal)
2359 (when
2360 (or (not ,signal-list)
2361 (memq signal ,signal-list))
2362 (when (not (gdb-pending-p
2363 (cons (current-buffer) ',trigger-name)))
2364 (gdb-input ,gdb-command
2365 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2366 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2368 ;; Used by disassembly buffer only, the rest use
2369 ;; def-gdb-trigger-and-handler
2370 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2371 &optional nopreserve)
2372 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2374 Handlers are normally called from the buffers they put output in.
2376 Delete ((current-buffer) . TRIGGER-NAME) from
2377 `gdb-pending-triggers', erase current buffer and evaluate
2378 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2380 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2381 `(defun ,handler-name ()
2382 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2383 (let* ((buffer-read-only nil)
2384 (window (get-buffer-window (current-buffer) 0))
2385 (start (window-start window))
2386 (p (window-point window)))
2387 (erase-buffer)
2388 (,custom-defun)
2389 (gdb-update-buffer-name)
2390 ,(when (not nopreserve)
2391 '(set-window-start window start)
2392 '(set-window-point window p)))))
2394 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2395 handler-name custom-defun
2396 &optional signal-list)
2397 "Define trigger and handler.
2399 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2400 `def-gdb-auto-update-trigger'.
2402 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2403 `def-gdb-auto-update-handler'."
2404 `(progn
2405 (def-gdb-auto-update-trigger ,trigger-name
2406 ,gdb-command
2407 ,handler-name ,signal-list)
2408 (def-gdb-auto-update-handler ,handler-name
2409 ,trigger-name ,custom-defun)))
2413 ;; Breakpoint buffer : This displays the output of `-break-list'.
2414 (def-gdb-trigger-and-handler
2415 gdb-invalidate-breakpoints "-break-list"
2416 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2417 '(start update))
2419 (gdb-set-buffer-rules
2420 'gdb-breakpoints-buffer
2421 'gdb-breakpoints-buffer-name
2422 'gdb-breakpoints-mode
2423 'gdb-invalidate-breakpoints)
2425 (defun gdb-breakpoints-list-handler-custom ()
2426 (let ((breakpoints-list (bindat-get-field
2427 (gdb-json-partial-output "bkpt" "script")
2428 'BreakpointTable 'body))
2429 (table (make-gdb-table)))
2430 (setq gdb-breakpoints-list nil)
2431 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2432 (dolist (breakpoint breakpoints-list)
2433 (add-to-list 'gdb-breakpoints-list
2434 (cons (bindat-get-field breakpoint 'number)
2435 breakpoint))
2436 (let ((at (bindat-get-field breakpoint 'at))
2437 (pending (bindat-get-field breakpoint 'pending))
2438 (func (bindat-get-field breakpoint 'func))
2439 (type (bindat-get-field breakpoint 'type)))
2440 (gdb-table-add-row table
2441 (list
2442 (bindat-get-field breakpoint 'number)
2443 (or type "")
2444 (or (bindat-get-field breakpoint 'disp) "")
2445 (let ((flag (bindat-get-field breakpoint 'enabled)))
2446 (if (string-equal flag "y")
2447 (propertize "y" 'font-lock-face font-lock-warning-face)
2448 (propertize "n" 'font-lock-face font-lock-comment-face)))
2449 (bindat-get-field breakpoint 'addr)
2450 (or (bindat-get-field breakpoint 'times) "")
2451 (if (and type (string-match ".*watchpoint" type))
2452 (bindat-get-field breakpoint 'what)
2453 (or pending at
2454 (concat "in "
2455 (propertize (or func "unknown")
2456 'font-lock-face font-lock-function-name-face)
2457 (gdb-frame-location breakpoint)))))
2458 ;; Add clickable properties only for breakpoints with file:line
2459 ;; information
2460 (append (list 'gdb-breakpoint breakpoint)
2461 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2462 mouse-face highlight))))))
2463 (insert (gdb-table-string table " "))
2464 (gdb-place-breakpoints)))
2466 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2467 (defun gdb-place-breakpoints ()
2468 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2469 (dolist (buffer (buffer-list))
2470 (with-current-buffer buffer
2471 (if (and (eq gud-minor-mode 'gdbmi)
2472 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2473 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2474 (dolist (breakpoint gdb-breakpoints-list)
2475 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2476 ; an associative list
2477 (line (bindat-get-field breakpoint 'line)))
2478 (when line
2479 (let ((file (bindat-get-field breakpoint 'fullname))
2480 (flag (bindat-get-field breakpoint 'enabled))
2481 (bptno (bindat-get-field breakpoint 'number)))
2482 (unless (and file (file-exists-p file))
2483 (setq file (cdr (assoc bptno gdb-location-alist))))
2484 (if (or (null file)
2485 (string-equal file "File not found"))
2486 ;; If the full filename is not recorded in the
2487 ;; breakpoint structure or in `gdb-location-alist', use
2488 ;; -file-list-exec-source-file to extract it.
2489 (when (setq file (bindat-get-field breakpoint 'file))
2490 (gdb-input (concat "list " file ":1") 'ignore)
2491 (gdb-input "-file-list-exec-source-file"
2492 `(lambda () (gdb-get-location
2493 ,bptno ,line ,flag))))
2494 (with-current-buffer (find-file-noselect file 'nowarn)
2495 (gdb-init-buffer)
2496 ;; Only want one breakpoint icon at each location.
2497 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2498 (string-to-number line)))))))))
2500 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2502 (defun gdb-get-location (bptno line flag)
2503 "Find the directory containing the relevant source file.
2504 Put in buffer and place breakpoint icon."
2505 (goto-char (point-min))
2506 (catch 'file-not-found
2507 (if (re-search-forward gdb-source-file-regexp nil t)
2508 (delete (cons bptno "File not found") gdb-location-alist)
2509 (push (cons bptno (match-string 1)) gdb-location-alist)
2510 (gdb-resync)
2511 (unless (assoc bptno gdb-location-alist)
2512 (push (cons bptno "File not found") gdb-location-alist)
2513 (message-box "Cannot find source file for breakpoint location.
2514 Add directory to search path for source files using the GDB command, dir."))
2515 (throw 'file-not-found nil))
2516 (with-current-buffer (find-file-noselect (match-string 1))
2517 (gdb-init-buffer)
2518 ;; only want one breakpoint icon at each location
2519 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2521 (add-hook 'find-file-hook 'gdb-find-file-hook)
2523 (defun gdb-find-file-hook ()
2524 "Set up buffer for debugging if file is part of the source code
2525 of the current session."
2526 (if (and (buffer-name gud-comint-buffer)
2527 ;; in case gud or gdb-ui is just loaded
2528 gud-comint-buffer
2529 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2530 'gdbmi))
2531 (if (member buffer-file-name gdb-source-file-list)
2532 (with-current-buffer (find-buffer-visiting buffer-file-name)
2533 (gdb-init-buffer)))))
2535 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2536 (declare-function gud-break "gdb-mi" t t) ; gud-def
2537 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2539 (defun gdb-mouse-set-clear-breakpoint (event)
2540 "Set/clear breakpoint in left fringe/margin at mouse click.
2541 If not in a source or disassembly buffer just set point."
2542 (interactive "e")
2543 (mouse-minibuffer-check event)
2544 (let ((posn (event-end event)))
2545 (with-selected-window (posn-window posn)
2546 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2547 (if (numberp (posn-point posn))
2548 (save-excursion
2549 (goto-char (posn-point posn))
2550 (if (or (posn-object posn)
2551 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2552 'breakpoint))
2553 (gud-remove nil)
2554 (gud-break nil)))))
2555 (posn-set-point posn))))
2557 (defun gdb-mouse-toggle-breakpoint-margin (event)
2558 "Enable/disable breakpoint in left margin with mouse click."
2559 (interactive "e")
2560 (mouse-minibuffer-check event)
2561 (let ((posn (event-end event)))
2562 (if (numberp (posn-point posn))
2563 (with-selected-window (posn-window posn)
2564 (save-excursion
2565 (goto-char (posn-point posn))
2566 (if (posn-object posn)
2567 (gud-basic-call
2568 (let ((bptno (get-text-property
2569 0 'gdb-bptno (car (posn-string posn)))))
2570 (concat
2571 (if (get-text-property
2572 0 'gdb-enabled (car (posn-string posn)))
2573 "-break-disable "
2574 "-break-enable ")
2575 bptno)))))))))
2577 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2578 "Enable/disable breakpoint in left fringe with mouse click."
2579 (interactive "e")
2580 (mouse-minibuffer-check event)
2581 (let* ((posn (event-end event))
2582 (pos (posn-point posn))
2583 obj)
2584 (when (numberp pos)
2585 (with-selected-window (posn-window posn)
2586 (with-current-buffer (window-buffer (selected-window))
2587 (goto-char pos)
2588 (dolist (overlay (overlays-in pos pos))
2589 (when (overlay-get overlay 'put-break)
2590 (setq obj (overlay-get overlay 'before-string))))
2591 (when (stringp obj)
2592 (gud-basic-call
2593 (concat
2594 (if (get-text-property 0 'gdb-enabled obj)
2595 "-break-disable "
2596 "-break-enable ")
2597 (get-text-property 0 'gdb-bptno obj)))))))))
2599 (defun gdb-breakpoints-buffer-name ()
2600 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2602 (defun gdb-display-breakpoints-buffer (&optional thread)
2603 "Display GDB breakpoints."
2604 (interactive)
2605 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
2607 (defun gdb-frame-breakpoints-buffer (&optional thread)
2608 "Display GDB breakpoints in another frame."
2609 (interactive)
2610 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
2611 gdb-display-buffer-other-frame-action))
2613 (defvar gdb-breakpoints-mode-map
2614 (let ((map (make-sparse-keymap))
2615 (menu (make-sparse-keymap "Breakpoints")))
2616 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2617 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2618 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2619 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2620 (suppress-keymap map)
2621 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2622 (define-key map " " 'gdb-toggle-breakpoint)
2623 (define-key map "D" 'gdb-delete-breakpoint)
2624 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2625 (define-key map "q" 'gdb-delete-frame-or-window)
2626 (define-key map "\r" 'gdb-goto-breakpoint)
2627 (define-key map "\t" (lambda ()
2628 (interactive)
2629 (gdb-set-window-buffer
2630 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2631 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2632 (define-key map [follow-link] 'mouse-face)
2633 map))
2635 (defun gdb-delete-frame-or-window ()
2636 "Delete frame if there is only one window. Otherwise delete the window."
2637 (interactive)
2638 (if (one-window-p) (delete-frame)
2639 (delete-window)))
2641 ;;from make-mode-line-mouse-map
2642 (defun gdb-make-header-line-mouse-map (mouse function) "\
2643 Return a keymap with single entry for mouse key MOUSE on the header line.
2644 MOUSE is defined to run function FUNCTION with no args in the buffer
2645 corresponding to the mode line clicked."
2646 (let ((map (make-sparse-keymap)))
2647 (define-key map (vector 'header-line mouse) function)
2648 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2649 map))
2651 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2652 `(propertize ,name
2653 'help-echo ,help-echo
2654 'mouse-face ',mouse-face
2655 'face ',face
2656 'local-map
2657 (gdb-make-header-line-mouse-map
2658 'mouse-1
2659 (lambda (event) (interactive "e")
2660 (save-selected-window
2661 (select-window (posn-window (event-start event)))
2662 (gdb-set-window-buffer
2663 (gdb-get-buffer-create ',buffer) t) )))))
2666 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2667 ;;; Threads view
2669 (defun gdb-threads-buffer-name ()
2670 (concat "*threads of " (gdb-get-target-string) "*"))
2672 (defun gdb-display-threads-buffer (&optional thread)
2673 "Display GDB threads."
2674 (interactive)
2675 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
2677 (defun gdb-frame-threads-buffer (&optional thread)
2678 "Display GDB threads in another frame."
2679 (interactive)
2680 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
2681 gdb-display-buffer-other-frame-action))
2683 (def-gdb-trigger-and-handler
2684 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2685 gdb-thread-list-handler gdb-thread-list-handler-custom
2686 '(start update update-threads))
2688 (gdb-set-buffer-rules
2689 'gdb-threads-buffer
2690 'gdb-threads-buffer-name
2691 'gdb-threads-mode
2692 'gdb-invalidate-threads)
2694 (defvar gdb-threads-font-lock-keywords
2695 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2696 (" \\(stopped\\)" (1 font-lock-warning-face))
2697 (" \\(running\\)" (1 font-lock-string-face))
2698 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2699 "Font lock keywords used in `gdb-threads-mode'.")
2701 (defvar gdb-threads-mode-map
2702 (let ((map (make-sparse-keymap)))
2703 (define-key map "\r" 'gdb-select-thread)
2704 (define-key map "f" 'gdb-display-stack-for-thread)
2705 (define-key map "F" 'gdb-frame-stack-for-thread)
2706 (define-key map "l" 'gdb-display-locals-for-thread)
2707 (define-key map "L" 'gdb-frame-locals-for-thread)
2708 (define-key map "r" 'gdb-display-registers-for-thread)
2709 (define-key map "R" 'gdb-frame-registers-for-thread)
2710 (define-key map "d" 'gdb-display-disassembly-for-thread)
2711 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2712 (define-key map "i" 'gdb-interrupt-thread)
2713 (define-key map "c" 'gdb-continue-thread)
2714 (define-key map "s" 'gdb-step-thread)
2715 (define-key map "\t"
2716 (lambda ()
2717 (interactive)
2718 (gdb-set-window-buffer
2719 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2720 (define-key map [mouse-2] 'gdb-select-thread)
2721 (define-key map [follow-link] 'mouse-face)
2722 map))
2724 (defvar gdb-threads-header
2725 (list
2726 (gdb-propertize-header
2727 "Breakpoints" gdb-breakpoints-buffer
2728 "mouse-1: select" mode-line-highlight mode-line-inactive)
2730 (gdb-propertize-header "Threads" gdb-threads-buffer
2731 nil nil mode-line)))
2733 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2734 "Major mode for GDB threads."
2735 (setq gdb-thread-position (make-marker))
2736 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2737 (setq header-line-format gdb-threads-header)
2738 (set (make-local-variable 'font-lock-defaults)
2739 '(gdb-threads-font-lock-keywords))
2740 'gdb-invalidate-threads)
2742 (defun gdb-thread-list-handler-custom ()
2743 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2744 (table (make-gdb-table))
2745 (marked-line nil))
2746 (setq gdb-threads-list nil)
2747 (setq gdb-running-threads-count 0)
2748 (setq gdb-stopped-threads-count 0)
2749 (set-marker gdb-thread-position nil)
2751 (dolist (thread (reverse threads-list))
2752 (let ((running (equal (bindat-get-field thread 'state) "running")))
2753 (add-to-list 'gdb-threads-list
2754 (cons (bindat-get-field thread 'id)
2755 thread))
2756 (cl-incf (if running
2757 gdb-running-threads-count
2758 gdb-stopped-threads-count))
2760 (gdb-table-add-row table
2761 (list
2762 (bindat-get-field thread 'id)
2763 (concat
2764 (if gdb-thread-buffer-verbose-names
2765 (concat (bindat-get-field thread 'target-id) " ") "")
2766 (bindat-get-field thread 'state)
2767 ;; Include frame information for stopped threads
2768 (if (not running)
2769 (concat
2770 " in " (bindat-get-field thread 'frame 'func)
2771 (if gdb-thread-buffer-arguments
2772 (concat
2773 " ("
2774 (let ((args (bindat-get-field thread 'frame 'args)))
2775 (mapconcat
2776 (lambda (arg)
2777 (apply #'format "%s=%s"
2778 (gdb-get-many-fields arg 'name 'value)))
2779 args ","))
2780 ")")
2782 (if gdb-thread-buffer-locations
2783 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2784 (if gdb-thread-buffer-addresses
2785 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2786 "")))
2787 (list
2788 'gdb-thread thread
2789 'mouse-face 'highlight
2790 'help-echo "mouse-2, RET: select thread")))
2791 (when (string-equal gdb-thread-number
2792 (bindat-get-field thread 'id))
2793 (setq marked-line (length gdb-threads-list))))
2794 (insert (gdb-table-string table " "))
2795 (when marked-line
2796 (gdb-mark-line marked-line gdb-thread-position)))
2797 ;; We update gud-running here because we need to make sure that
2798 ;; gdb-threads-list is up-to-date
2799 (gdb-update-gud-running)
2800 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2802 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2803 "Define a NAME command which will act upon thread on the current line.
2805 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2806 be the value of 'gdb-thread property of the current line. If
2807 'gdb-thread is nil, error is signaled."
2808 `(defun ,name (&optional event)
2809 ,(when doc doc)
2810 (interactive (list last-input-event))
2811 (if event (posn-set-point (event-end event)))
2812 (save-excursion
2813 (beginning-of-line)
2814 (let ((thread (get-text-property (point) 'gdb-thread)))
2815 (if thread
2816 ,custom-defun
2817 (error "Not recognized as thread line"))))))
2819 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2820 &optional doc)
2821 "Define a NAME which will call BUFFER-COMMAND with id of thread
2822 on the current line."
2823 `(def-gdb-thread-buffer-command ,name
2824 (,buffer-command (bindat-get-field thread 'id))
2825 ,doc))
2827 (def-gdb-thread-buffer-command gdb-select-thread
2828 (let ((new-id (bindat-get-field thread 'id)))
2829 (gdb-setq-thread-number new-id)
2830 (gdb-input (concat "-thread-select " new-id) 'ignore)
2831 (gdb-update))
2832 "Select the thread at current line of threads buffer.")
2834 (def-gdb-thread-buffer-simple-command
2835 gdb-display-stack-for-thread
2836 gdb-preemptively-display-stack-buffer
2837 "Display stack buffer for the thread at current line.")
2839 (def-gdb-thread-buffer-simple-command
2840 gdb-display-locals-for-thread
2841 gdb-preemptively-display-locals-buffer
2842 "Display locals buffer for the thread at current line.")
2844 (def-gdb-thread-buffer-simple-command
2845 gdb-display-registers-for-thread
2846 gdb-preemptively-display-registers-buffer
2847 "Display registers buffer for the thread at current line.")
2849 (def-gdb-thread-buffer-simple-command
2850 gdb-display-disassembly-for-thread
2851 gdb-preemptively-display-disassembly-buffer
2852 "Display disassembly buffer for the thread at current line.")
2854 (def-gdb-thread-buffer-simple-command
2855 gdb-frame-stack-for-thread
2856 gdb-frame-stack-buffer
2857 "Display another frame with stack buffer for thread at current line.")
2859 (def-gdb-thread-buffer-simple-command
2860 gdb-frame-locals-for-thread
2861 gdb-frame-locals-buffer
2862 "Display another frame with locals buffer for thread at current line.")
2864 (def-gdb-thread-buffer-simple-command
2865 gdb-frame-registers-for-thread
2866 gdb-frame-registers-buffer
2867 "Display another frame with registers buffer for the thread at current line.")
2869 (def-gdb-thread-buffer-simple-command
2870 gdb-frame-disassembly-for-thread
2871 gdb-frame-disassembly-buffer
2872 "Display another frame with disassembly buffer for the thread at current line.")
2874 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2875 "Define a NAME which will execute GUD-COMMAND with
2876 `gdb-thread-number' locally bound to id of thread on the current
2877 line."
2878 `(def-gdb-thread-buffer-command ,name
2879 (if gdb-non-stop
2880 (let ((gdb-thread-number (bindat-get-field thread 'id))
2881 (gdb-gud-control-all-threads nil))
2882 (call-interactively #',gud-command))
2883 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2884 ,doc))
2886 (def-gdb-thread-buffer-gud-command
2887 gdb-interrupt-thread
2888 gud-stop-subjob
2889 "Interrupt thread at current line.")
2891 (def-gdb-thread-buffer-gud-command
2892 gdb-continue-thread
2893 gud-cont
2894 "Continue thread at current line.")
2896 (def-gdb-thread-buffer-gud-command
2897 gdb-step-thread
2898 gud-step
2899 "Step thread at current line.")
2902 ;;; Memory view
2904 (defcustom gdb-memory-rows 8
2905 "Number of data rows in memory window."
2906 :type 'integer
2907 :group 'gud
2908 :version "23.2")
2910 (defcustom gdb-memory-columns 4
2911 "Number of data columns in memory window."
2912 :type 'integer
2913 :group 'gud
2914 :version "23.2")
2916 (defcustom gdb-memory-format "x"
2917 "Display format of data items in memory window."
2918 :type '(choice (const :tag "Hexadecimal" "x")
2919 (const :tag "Signed decimal" "d")
2920 (const :tag "Unsigned decimal" "u")
2921 (const :tag "Octal" "o")
2922 (const :tag "Binary" "t"))
2923 :group 'gud
2924 :version "22.1")
2926 (defcustom gdb-memory-unit 4
2927 "Unit size of data items in memory window."
2928 :type '(choice (const :tag "Byte" 1)
2929 (const :tag "Halfword" 2)
2930 (const :tag "Word" 4)
2931 (const :tag "Giant word" 8))
2932 :group 'gud
2933 :version "23.2")
2935 (def-gdb-trigger-and-handler
2936 gdb-invalidate-memory
2937 (format "-data-read-memory %s %s %d %d %d"
2938 gdb-memory-address
2939 gdb-memory-format
2940 gdb-memory-unit
2941 gdb-memory-rows
2942 gdb-memory-columns)
2943 gdb-read-memory-handler
2944 gdb-read-memory-custom
2945 '(start update))
2947 (gdb-set-buffer-rules
2948 'gdb-memory-buffer
2949 'gdb-memory-buffer-name
2950 'gdb-memory-mode
2951 'gdb-invalidate-memory)
2953 (defun gdb-memory-column-width (size format)
2954 "Return length of string with memory unit of SIZE in FORMAT.
2956 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2957 in `gdb-memory-format'."
2958 (let ((format-base (cdr (assoc format
2959 '(("x" . 16)
2960 ("d" . 10) ("u" . 10)
2961 ("o" . 8)
2962 ("t" . 2))))))
2963 (if format-base
2964 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2965 (cond ((string-equal format "x")
2966 (+ 2 res)) ; hexadecimal numbers have 0x in front
2967 ((or (string-equal format "d")
2968 (string-equal format "o"))
2969 (1+ res))
2970 (t res)))
2971 (error "Unknown format"))))
2973 (defun gdb-read-memory-custom ()
2974 (let* ((res (gdb-json-partial-output))
2975 (err-msg (bindat-get-field res 'msg)))
2976 (if (not err-msg)
2977 (let ((memory (bindat-get-field res 'memory)))
2978 (setq gdb-memory-address (bindat-get-field res 'addr))
2979 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2980 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2981 (setq gdb-memory-last-address gdb-memory-address)
2982 (dolist (row memory)
2983 (insert (concat (bindat-get-field row 'addr) ":"))
2984 (dolist (column (bindat-get-field row 'data))
2985 (insert (gdb-pad-string column
2986 (+ 2 (gdb-memory-column-width
2987 gdb-memory-unit
2988 gdb-memory-format)))))
2989 (newline)))
2990 ;; Show last page instead of empty buffer when out of bounds
2991 (progn
2992 (let ((gdb-memory-address gdb-memory-last-address))
2993 (gdb-invalidate-memory 'update)
2994 (error err-msg))))))
2996 (defvar gdb-memory-mode-map
2997 (let ((map (make-sparse-keymap)))
2998 (suppress-keymap map t)
2999 (define-key map "q" 'kill-this-buffer)
3000 (define-key map "n" 'gdb-memory-show-next-page)
3001 (define-key map "p" 'gdb-memory-show-previous-page)
3002 (define-key map "a" 'gdb-memory-set-address)
3003 (define-key map "t" 'gdb-memory-format-binary)
3004 (define-key map "o" 'gdb-memory-format-octal)
3005 (define-key map "u" 'gdb-memory-format-unsigned)
3006 (define-key map "d" 'gdb-memory-format-signed)
3007 (define-key map "x" 'gdb-memory-format-hexadecimal)
3008 (define-key map "b" 'gdb-memory-unit-byte)
3009 (define-key map "h" 'gdb-memory-unit-halfword)
3010 (define-key map "w" 'gdb-memory-unit-word)
3011 (define-key map "g" 'gdb-memory-unit-giant)
3012 (define-key map "R" 'gdb-memory-set-rows)
3013 (define-key map "C" 'gdb-memory-set-columns)
3014 map))
3016 (defun gdb-memory-set-address-event (event)
3017 "Handle a click on address field in memory buffer header."
3018 (interactive "e")
3019 (save-selected-window
3020 (select-window (posn-window (event-start event)))
3021 (gdb-memory-set-address)))
3023 ;; Non-event version for use within keymap
3024 (defun gdb-memory-set-address ()
3025 "Set the start memory address."
3026 (interactive)
3027 (let ((arg (read-from-minibuffer "Memory address: ")))
3028 (setq gdb-memory-address arg))
3029 (gdb-invalidate-memory 'update))
3031 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3032 "Define a function NAME which reads new VAR value from minibuffer."
3033 `(defun ,name (event)
3034 ,(when doc doc)
3035 (interactive "e")
3036 (save-selected-window
3037 (select-window (posn-window (event-start event)))
3038 (let* ((arg (read-from-minibuffer ,echo-string))
3039 (count (string-to-number arg)))
3040 (if (<= count 0)
3041 (error "Positive number only")
3042 (customize-set-variable ',variable count)
3043 (gdb-invalidate-memory 'update))))))
3045 (def-gdb-set-positive-number
3046 gdb-memory-set-rows
3047 gdb-memory-rows
3048 "Rows: "
3049 "Set the number of data rows in memory window.")
3051 (def-gdb-set-positive-number
3052 gdb-memory-set-columns
3053 gdb-memory-columns
3054 "Columns: "
3055 "Set the number of data columns in memory window.")
3057 (defmacro def-gdb-memory-format (name format doc)
3058 "Define a function NAME to switch memory buffer to use FORMAT.
3060 DOC is an optional documentation string."
3061 `(defun ,name () ,(when doc doc)
3062 (interactive)
3063 (customize-set-variable 'gdb-memory-format ,format)
3064 (gdb-invalidate-memory 'update)))
3066 (def-gdb-memory-format
3067 gdb-memory-format-binary "t"
3068 "Set the display format to binary.")
3070 (def-gdb-memory-format
3071 gdb-memory-format-octal "o"
3072 "Set the display format to octal.")
3074 (def-gdb-memory-format
3075 gdb-memory-format-unsigned "u"
3076 "Set the display format to unsigned decimal.")
3078 (def-gdb-memory-format
3079 gdb-memory-format-signed "d"
3080 "Set the display format to decimal.")
3082 (def-gdb-memory-format
3083 gdb-memory-format-hexadecimal "x"
3084 "Set the display format to hexadecimal.")
3086 (defvar gdb-memory-format-map
3087 (let ((map (make-sparse-keymap)))
3088 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3089 map)
3090 "Keymap to select format in the header line.")
3092 (defvar gdb-memory-format-menu
3093 (let ((map (make-sparse-keymap "Format")))
3095 (define-key map [binary]
3096 '(menu-item "Binary" gdb-memory-format-binary
3097 :button (:radio . (equal gdb-memory-format "t"))))
3098 (define-key map [octal]
3099 '(menu-item "Octal" gdb-memory-format-octal
3100 :button (:radio . (equal gdb-memory-format "o"))))
3101 (define-key map [unsigned]
3102 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3103 :button (:radio . (equal gdb-memory-format "u"))))
3104 (define-key map [signed]
3105 '(menu-item "Signed Decimal" gdb-memory-format-signed
3106 :button (:radio . (equal gdb-memory-format "d"))))
3107 (define-key map [hexadecimal]
3108 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3109 :button (:radio . (equal gdb-memory-format "x"))))
3110 map)
3111 "Menu of display formats in the header line.")
3113 (defun gdb-memory-format-menu (event)
3114 (interactive "@e")
3115 (x-popup-menu event gdb-memory-format-menu))
3117 (defun gdb-memory-format-menu-1 (event)
3118 (interactive "e")
3119 (save-selected-window
3120 (select-window (posn-window (event-start event)))
3121 (let* ((selection (gdb-memory-format-menu event))
3122 (binding (and selection (lookup-key gdb-memory-format-menu
3123 (vector (car selection))))))
3124 (if binding (call-interactively binding)))))
3126 (defmacro def-gdb-memory-unit (name unit-size doc)
3127 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3129 DOC is an optional documentation string."
3130 `(defun ,name () ,(when doc doc)
3131 (interactive)
3132 (customize-set-variable 'gdb-memory-unit ,unit-size)
3133 (gdb-invalidate-memory 'update)))
3135 (def-gdb-memory-unit gdb-memory-unit-giant 8
3136 "Set the unit size to giant words (eight bytes).")
3138 (def-gdb-memory-unit gdb-memory-unit-word 4
3139 "Set the unit size to words (four bytes).")
3141 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3142 "Set the unit size to halfwords (two bytes).")
3144 (def-gdb-memory-unit gdb-memory-unit-byte 1
3145 "Set the unit size to bytes.")
3147 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3148 "Define a function NAME which show new address in memory buffer.
3150 The defined function switches Memory buffer to show address
3151 stored in ADDRESS-VAR variable.
3153 DOC is an optional documentation string."
3154 `(defun ,name
3155 ,(when doc doc)
3156 (interactive)
3157 (let ((gdb-memory-address ,address-var))
3158 (gdb-invalidate-memory))))
3160 (def-gdb-memory-show-page gdb-memory-show-previous-page
3161 gdb-memory-prev-page)
3163 (def-gdb-memory-show-page gdb-memory-show-next-page
3164 gdb-memory-next-page)
3166 (defvar gdb-memory-unit-map
3167 (let ((map (make-sparse-keymap)))
3168 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3169 map)
3170 "Keymap to select units in the header line.")
3172 (defvar gdb-memory-unit-menu
3173 (let ((map (make-sparse-keymap "Unit")))
3174 (define-key map [giantwords]
3175 '(menu-item "Giant words" gdb-memory-unit-giant
3176 :button (:radio . (equal gdb-memory-unit 8))))
3177 (define-key map [words]
3178 '(menu-item "Words" gdb-memory-unit-word
3179 :button (:radio . (equal gdb-memory-unit 4))))
3180 (define-key map [halfwords]
3181 '(menu-item "Halfwords" gdb-memory-unit-halfword
3182 :button (:radio . (equal gdb-memory-unit 2))))
3183 (define-key map [bytes]
3184 '(menu-item "Bytes" gdb-memory-unit-byte
3185 :button (:radio . (equal gdb-memory-unit 1))))
3186 map)
3187 "Menu of units in the header line.")
3189 (defun gdb-memory-unit-menu (event)
3190 (interactive "@e")
3191 (x-popup-menu event gdb-memory-unit-menu))
3193 (defun gdb-memory-unit-menu-1 (event)
3194 (interactive "e")
3195 (save-selected-window
3196 (select-window (posn-window (event-start event)))
3197 (let* ((selection (gdb-memory-unit-menu event))
3198 (binding (and selection (lookup-key gdb-memory-unit-menu
3199 (vector (car selection))))))
3200 (if binding (call-interactively binding)))))
3202 (defvar gdb-memory-font-lock-keywords
3203 '(;; <__function.name+n>
3204 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3205 (1 font-lock-function-name-face)))
3206 "Font lock keywords used in `gdb-memory-mode'.")
3208 (defvar gdb-memory-header
3209 '(:eval
3210 (concat
3211 "Start address["
3212 (propertize "-"
3213 'face font-lock-warning-face
3214 'help-echo "mouse-1: decrement address"
3215 'mouse-face 'mode-line-highlight
3216 'local-map (gdb-make-header-line-mouse-map
3217 'mouse-1
3218 #'gdb-memory-show-previous-page))
3220 (propertize "+"
3221 'face font-lock-warning-face
3222 'help-echo "mouse-1: increment address"
3223 'mouse-face 'mode-line-highlight
3224 'local-map (gdb-make-header-line-mouse-map
3225 'mouse-1
3226 #'gdb-memory-show-next-page))
3227 "]: "
3228 (propertize gdb-memory-address
3229 'face font-lock-warning-face
3230 'help-echo "mouse-1: set start address"
3231 'mouse-face 'mode-line-highlight
3232 'local-map (gdb-make-header-line-mouse-map
3233 'mouse-1
3234 #'gdb-memory-set-address-event))
3235 " Rows: "
3236 (propertize (number-to-string gdb-memory-rows)
3237 'face font-lock-warning-face
3238 'help-echo "mouse-1: set number of columns"
3239 'mouse-face 'mode-line-highlight
3240 'local-map (gdb-make-header-line-mouse-map
3241 'mouse-1
3242 #'gdb-memory-set-rows))
3243 " Columns: "
3244 (propertize (number-to-string gdb-memory-columns)
3245 'face font-lock-warning-face
3246 'help-echo "mouse-1: set number of columns"
3247 'mouse-face 'mode-line-highlight
3248 'local-map (gdb-make-header-line-mouse-map
3249 'mouse-1
3250 #'gdb-memory-set-columns))
3251 " Display Format: "
3252 (propertize gdb-memory-format
3253 'face font-lock-warning-face
3254 'help-echo "mouse-3: select display format"
3255 'mouse-face 'mode-line-highlight
3256 'local-map gdb-memory-format-map)
3257 " Unit Size: "
3258 (propertize (number-to-string gdb-memory-unit)
3259 'face font-lock-warning-face
3260 'help-echo "mouse-3: select unit size"
3261 'mouse-face 'mode-line-highlight
3262 'local-map gdb-memory-unit-map)))
3263 "Header line used in `gdb-memory-mode'.")
3265 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3266 "Major mode for examining memory."
3267 (setq header-line-format gdb-memory-header)
3268 (set (make-local-variable 'font-lock-defaults)
3269 '(gdb-memory-font-lock-keywords))
3270 'gdb-invalidate-memory)
3272 (defun gdb-memory-buffer-name ()
3273 (concat "*memory of " (gdb-get-target-string) "*"))
3275 (defun gdb-display-memory-buffer (&optional thread)
3276 "Display GDB memory contents."
3277 (interactive)
3278 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3280 (defun gdb-frame-memory-buffer ()
3281 "Display memory contents in another frame."
3282 (interactive)
3283 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3284 gdb-display-buffer-other-frame-action))
3287 ;;; Disassembly view
3289 (defun gdb-disassembly-buffer-name ()
3290 (gdb-current-context-buffer-name
3291 (concat "disassembly of " (gdb-get-target-string))))
3293 (defun gdb-display-disassembly-buffer (&optional thread)
3294 "Display GDB disassembly information."
3295 (interactive)
3296 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3298 (def-gdb-preempt-display-buffer
3299 gdb-preemptively-display-disassembly-buffer
3300 'gdb-disassembly-buffer)
3302 (defun gdb-frame-disassembly-buffer (&optional thread)
3303 "Display GDB disassembly information in another frame."
3304 (interactive)
3305 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3306 gdb-display-buffer-other-frame-action))
3308 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3309 (let* ((frame (gdb-current-buffer-frame))
3310 (file (bindat-get-field frame 'fullname))
3311 (line (bindat-get-field frame 'line)))
3312 (if file
3313 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3314 ;; If we're unable to get a file name / line for $PC, simply
3315 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3316 ;; 150 bytes) instructions.
3317 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3318 gdb-disassembly-handler
3319 ;; We update disassembly only after we have actual frame information
3320 ;; about all threads, so no there's `update' signal in this list
3321 '(start update-disassembly))
3323 (def-gdb-auto-update-handler
3324 gdb-disassembly-handler
3325 gdb-invalidate-disassembly
3326 gdb-disassembly-handler-custom
3329 (gdb-set-buffer-rules
3330 'gdb-disassembly-buffer
3331 'gdb-disassembly-buffer-name
3332 'gdb-disassembly-mode
3333 'gdb-invalidate-disassembly)
3335 (defvar gdb-disassembly-font-lock-keywords
3336 '(;; <__function.name+n>
3337 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3338 (1 font-lock-function-name-face))
3339 ;; 0xNNNNNNNN <__function.name+n>: opcode
3340 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3341 (4 font-lock-keyword-face))
3342 ;; %register(at least i386)
3343 ("%\\sw+" . font-lock-variable-name-face)
3344 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3345 (1 font-lock-comment-face)
3346 (2 font-lock-function-name-face))
3347 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3348 "Font lock keywords used in `gdb-disassembly-mode'.")
3350 (defvar gdb-disassembly-mode-map
3351 ;; TODO
3352 (let ((map (make-sparse-keymap)))
3353 (suppress-keymap map)
3354 (define-key map "q" 'kill-this-buffer)
3355 map))
3357 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3358 "Major mode for GDB disassembly information."
3359 ;; TODO Rename overlay variable for disassembly mode
3360 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3361 (setq fringes-outside-margins t)
3362 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3363 (set (make-local-variable 'font-lock-defaults)
3364 '(gdb-disassembly-font-lock-keywords))
3365 'gdb-invalidate-disassembly)
3367 (defun gdb-disassembly-handler-custom ()
3368 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3369 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3370 (table (make-gdb-table))
3371 (marked-line nil))
3372 (dolist (instr instructions)
3373 (gdb-table-add-row table
3374 (list
3375 (bindat-get-field instr 'address)
3376 (let
3377 ((func-name (bindat-get-field instr 'func-name))
3378 (offset (bindat-get-field instr 'offset)))
3379 (if func-name
3380 (format "<%s+%s>:" func-name offset)
3381 ""))
3382 (bindat-get-field instr 'inst)))
3383 (when (string-equal (bindat-get-field instr 'address)
3384 address)
3385 (progn
3386 (setq marked-line (length (gdb-table-rows table)))
3387 (setq fringe-indicator-alist
3388 (if (string-equal gdb-frame-number "0")
3390 '((overlay-arrow . hollow-right-triangle)))))))
3391 (insert (gdb-table-string table " "))
3392 (gdb-disassembly-place-breakpoints)
3393 ;; Mark current position with overlay arrow and scroll window to
3394 ;; that point
3395 (when marked-line
3396 (let ((window (get-buffer-window (current-buffer) 0)))
3397 (set-window-point window (gdb-mark-line marked-line
3398 gdb-disassembly-position))))
3399 (setq mode-name
3400 (gdb-current-context-mode-name
3401 (concat "Disassembly: "
3402 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3404 (defun gdb-disassembly-place-breakpoints ()
3405 (gdb-remove-breakpoint-icons (point-min) (point-max))
3406 (dolist (breakpoint gdb-breakpoints-list)
3407 (let* ((breakpoint (cdr breakpoint))
3408 (bptno (bindat-get-field breakpoint 'number))
3409 (flag (bindat-get-field breakpoint 'enabled))
3410 (address (bindat-get-field breakpoint 'addr)))
3411 (save-excursion
3412 (goto-char (point-min))
3413 (if (re-search-forward (concat "^" address) nil t)
3414 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3417 (defvar gdb-breakpoints-header
3418 (list
3419 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3420 nil nil mode-line)
3422 (gdb-propertize-header "Threads" gdb-threads-buffer
3423 "mouse-1: select" mode-line-highlight
3424 mode-line-inactive)))
3426 ;;; Breakpoints view
3427 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3428 "Major mode for gdb breakpoints."
3429 (setq header-line-format gdb-breakpoints-header)
3430 'gdb-invalidate-breakpoints)
3432 (defun gdb-toggle-breakpoint ()
3433 "Enable/disable breakpoint at current line of breakpoints buffer."
3434 (interactive)
3435 (save-excursion
3436 (beginning-of-line)
3437 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3438 (if breakpoint
3439 (gud-basic-call
3440 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3441 "-break-disable "
3442 "-break-enable ")
3443 (bindat-get-field breakpoint 'number)))
3444 (error "Not recognized as break/watchpoint line")))))
3446 (defun gdb-delete-breakpoint ()
3447 "Delete the breakpoint at current line of breakpoints buffer."
3448 (interactive)
3449 (save-excursion
3450 (beginning-of-line)
3451 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3452 (if breakpoint
3453 (gud-basic-call (concat "-break-delete "
3454 (bindat-get-field breakpoint 'number)))
3455 (error "Not recognized as break/watchpoint line")))))
3457 (defun gdb-goto-breakpoint (&optional event)
3458 "Go to the location of breakpoint at current line of
3459 breakpoints buffer."
3460 (interactive (list last-input-event))
3461 (if event (posn-set-point (event-end event)))
3462 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3463 (let ((window (get-buffer-window gud-comint-buffer)))
3464 (if window (save-selected-window (select-window window))))
3465 (save-excursion
3466 (beginning-of-line)
3467 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3468 (if breakpoint
3469 (let ((bptno (bindat-get-field breakpoint 'number))
3470 (file (bindat-get-field breakpoint 'fullname))
3471 (line (bindat-get-field breakpoint 'line)))
3472 (save-selected-window
3473 (let* ((buffer (find-file-noselect
3474 (if (file-exists-p file) file
3475 (cdr (assoc bptno gdb-location-alist)))))
3476 (window (or (gdb-display-source-buffer buffer)
3477 (display-buffer buffer))))
3478 (setq gdb-source-window window)
3479 (with-current-buffer buffer
3480 (goto-char (point-min))
3481 (forward-line (1- (string-to-number line)))
3482 (set-window-point window (point))))))
3483 (error "Not recognized as break/watchpoint line")))))
3486 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3488 (def-gdb-trigger-and-handler
3489 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3490 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3491 '(start update))
3493 (gdb-set-buffer-rules
3494 'gdb-stack-buffer
3495 'gdb-stack-buffer-name
3496 'gdb-frames-mode
3497 'gdb-invalidate-frames)
3499 (defun gdb-frame-location (frame)
3500 "Return \" of file:line\" or \" of library\" for structure FRAME.
3502 FRAME must have either \"file\" and \"line\" members or \"from\"
3503 member."
3504 (let ((file (bindat-get-field frame 'file))
3505 (line (bindat-get-field frame 'line))
3506 (from (bindat-get-field frame 'from)))
3507 (let ((res (or (and file line (concat file ":" line))
3508 from)))
3509 (if res (concat " of " res) ""))))
3511 (defun gdb-stack-list-frames-custom ()
3512 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3513 (table (make-gdb-table)))
3514 (set-marker gdb-stack-position nil)
3515 (dolist (frame stack)
3516 (gdb-table-add-row table
3517 (list
3518 (bindat-get-field frame 'level)
3519 "in"
3520 (concat
3521 (bindat-get-field frame 'func)
3522 (if gdb-stack-buffer-locations
3523 (gdb-frame-location frame) "")
3524 (if gdb-stack-buffer-addresses
3525 (concat " at " (bindat-get-field frame 'addr)) "")))
3526 `(mouse-face highlight
3527 help-echo "mouse-2, RET: Select frame"
3528 gdb-frame ,frame)))
3529 (insert (gdb-table-string table " ")))
3530 (when (and gdb-frame-number
3531 (gdb-buffer-shows-main-thread-p))
3532 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3533 gdb-stack-position))
3534 (setq mode-name
3535 (gdb-current-context-mode-name "Frames")))
3537 (defun gdb-stack-buffer-name ()
3538 (gdb-current-context-buffer-name
3539 (concat "stack frames of " (gdb-get-target-string))))
3541 (defun gdb-display-stack-buffer (&optional thread)
3542 "Display GDB backtrace for current stack."
3543 (interactive)
3544 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
3546 (def-gdb-preempt-display-buffer
3547 gdb-preemptively-display-stack-buffer
3548 'gdb-stack-buffer nil t)
3550 (defun gdb-frame-stack-buffer (&optional thread)
3551 "Display GDB backtrace for current stack in another frame."
3552 (interactive)
3553 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
3554 gdb-display-buffer-other-frame-action))
3556 (defvar gdb-frames-mode-map
3557 (let ((map (make-sparse-keymap)))
3558 (suppress-keymap map)
3559 (define-key map "q" 'kill-this-buffer)
3560 (define-key map "\r" 'gdb-select-frame)
3561 (define-key map [mouse-2] 'gdb-select-frame)
3562 (define-key map [follow-link] 'mouse-face)
3563 map))
3565 (defvar gdb-frames-font-lock-keywords
3566 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3567 "Font lock keywords used in `gdb-frames-mode'.")
3569 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3570 "Major mode for gdb call stack."
3571 (setq gdb-stack-position (make-marker))
3572 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3573 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3574 (set (make-local-variable 'font-lock-defaults)
3575 '(gdb-frames-font-lock-keywords))
3576 'gdb-invalidate-frames)
3578 (defun gdb-select-frame (&optional event)
3579 "Select the frame and display the relevant source."
3580 (interactive (list last-input-event))
3581 (if event (posn-set-point (event-end event)))
3582 (let ((frame (get-text-property (point) 'gdb-frame)))
3583 (if frame
3584 (if (gdb-buffer-shows-main-thread-p)
3585 (let ((new-level (bindat-get-field frame 'level)))
3586 (setq gdb-frame-number new-level)
3587 (gdb-input (concat "-stack-select-frame " new-level)
3588 'ignore)
3589 (gdb-update))
3590 (error "Could not select frame for non-current thread"))
3591 (error "Not recognized as frame line"))))
3594 ;; Locals buffer.
3595 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3596 (def-gdb-trigger-and-handler
3597 gdb-invalidate-locals
3598 (concat (gdb-current-context-command "-stack-list-locals")
3599 " --simple-values")
3600 gdb-locals-handler gdb-locals-handler-custom
3601 '(start update))
3603 (gdb-set-buffer-rules
3604 'gdb-locals-buffer
3605 'gdb-locals-buffer-name
3606 'gdb-locals-mode
3607 'gdb-invalidate-locals)
3609 (defvar gdb-locals-watch-map
3610 (let ((map (make-sparse-keymap)))
3611 (suppress-keymap map)
3612 (define-key map "\r" 'gud-watch)
3613 (define-key map [mouse-2] 'gud-watch)
3614 map)
3615 "Keymap to create watch expression of a complex data type local variable.")
3617 (defvar gdb-edit-locals-map-1
3618 (let ((map (make-sparse-keymap)))
3619 (suppress-keymap map)
3620 (define-key map "\r" 'gdb-edit-locals-value)
3621 (define-key map [mouse-2] 'gdb-edit-locals-value)
3622 map)
3623 "Keymap to edit value of a simple data type local variable.")
3625 (defun gdb-edit-locals-value (&optional event)
3626 "Assign a value to a variable displayed in the locals buffer."
3627 (interactive (list last-input-event))
3628 (save-excursion
3629 (if event (posn-set-point (event-end event)))
3630 (beginning-of-line)
3631 (let* ((var (bindat-get-field
3632 (get-text-property (point) 'gdb-local-variable) 'name))
3633 (value (read-string (format "New value (%s): " var))))
3634 (gud-basic-call
3635 (concat "-gdb-set variable " var " = " value)))))
3637 ;; Don't display values of arrays or structures.
3638 ;; These can be expanded using gud-watch.
3639 (defun gdb-locals-handler-custom ()
3640 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3641 (table (make-gdb-table)))
3642 (dolist (local locals-list)
3643 (let ((name (bindat-get-field local 'name))
3644 (value (bindat-get-field local 'value))
3645 (type (bindat-get-field local 'type)))
3646 (if (or (not value)
3647 (string-match "\\0x" value))
3648 (add-text-properties 0 (length name)
3649 `(mouse-face highlight
3650 help-echo "mouse-2: create watch expression"
3651 local-map ,gdb-locals-watch-map)
3652 name)
3653 (add-text-properties 0 (length value)
3654 `(mouse-face highlight
3655 help-echo "mouse-2: edit value"
3656 local-map ,gdb-edit-locals-map-1)
3657 value))
3658 (gdb-table-add-row
3659 table
3660 (list
3661 (propertize type 'font-lock-face font-lock-type-face)
3662 (propertize name 'font-lock-face font-lock-variable-name-face)
3663 value)
3664 `(gdb-local-variable ,local))))
3665 (insert (gdb-table-string table " "))
3666 (setq mode-name
3667 (gdb-current-context-mode-name
3668 (concat "Locals: "
3669 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3671 (defvar gdb-locals-header
3672 (list
3673 (gdb-propertize-header "Locals" gdb-locals-buffer
3674 nil nil mode-line)
3676 (gdb-propertize-header "Registers" gdb-registers-buffer
3677 "mouse-1: select" mode-line-highlight
3678 mode-line-inactive)))
3680 (defvar gdb-locals-mode-map
3681 (let ((map (make-sparse-keymap)))
3682 (suppress-keymap map)
3683 (define-key map "q" 'kill-this-buffer)
3684 (define-key map "\t" (lambda ()
3685 (interactive)
3686 (gdb-set-window-buffer
3687 (gdb-get-buffer-create
3688 'gdb-registers-buffer
3689 gdb-thread-number) t)))
3690 map))
3692 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3693 "Major mode for gdb locals."
3694 (setq header-line-format gdb-locals-header)
3695 'gdb-invalidate-locals)
3697 (defun gdb-locals-buffer-name ()
3698 (gdb-current-context-buffer-name
3699 (concat "locals of " (gdb-get-target-string))))
3701 (defun gdb-display-locals-buffer (&optional thread)
3702 "Display the local variables of current GDB stack."
3703 (interactive)
3704 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
3706 (def-gdb-preempt-display-buffer
3707 gdb-preemptively-display-locals-buffer
3708 'gdb-locals-buffer nil t)
3710 (defun gdb-frame-locals-buffer (&optional thread)
3711 "Display the local variables of the current GDB stack in another frame."
3712 (interactive)
3713 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
3714 gdb-display-buffer-other-frame-action))
3717 ;; Registers buffer.
3719 (def-gdb-trigger-and-handler
3720 gdb-invalidate-registers
3721 (concat (gdb-current-context-command "-data-list-register-values") " x")
3722 gdb-registers-handler
3723 gdb-registers-handler-custom
3724 '(start update))
3726 (gdb-set-buffer-rules
3727 'gdb-registers-buffer
3728 'gdb-registers-buffer-name
3729 'gdb-registers-mode
3730 'gdb-invalidate-registers)
3732 (defun gdb-registers-handler-custom ()
3733 (when gdb-register-names
3734 (let ((register-values
3735 (bindat-get-field (gdb-json-partial-output) 'register-values))
3736 (table (make-gdb-table)))
3737 (dolist (register register-values)
3738 (let* ((register-number (bindat-get-field register 'number))
3739 (value (bindat-get-field register 'value))
3740 (register-name (nth (string-to-number register-number)
3741 gdb-register-names)))
3742 (gdb-table-add-row
3743 table
3744 (list
3745 (propertize register-name
3746 'font-lock-face font-lock-variable-name-face)
3747 (if (member register-number gdb-changed-registers)
3748 (propertize value 'font-lock-face font-lock-warning-face)
3749 value))
3750 `(mouse-face highlight
3751 help-echo "mouse-2: edit value"
3752 gdb-register-name ,register-name))))
3753 (insert (gdb-table-string table " ")))
3754 (setq mode-name
3755 (gdb-current-context-mode-name "Registers"))))
3757 (defun gdb-edit-register-value (&optional event)
3758 "Assign a value to a register displayed in the registers buffer."
3759 (interactive (list last-input-event))
3760 (save-excursion
3761 (if event (posn-set-point (event-end event)))
3762 (beginning-of-line)
3763 (let* ((var (bindat-get-field
3764 (get-text-property (point) 'gdb-register-name)))
3765 (value (read-string (format "New value (%s): " var))))
3766 (gud-basic-call
3767 (concat "-gdb-set variable $" var " = " value)))))
3769 (defvar gdb-registers-mode-map
3770 (let ((map (make-sparse-keymap)))
3771 (suppress-keymap map)
3772 (define-key map "\r" 'gdb-edit-register-value)
3773 (define-key map [mouse-2] 'gdb-edit-register-value)
3774 (define-key map "q" 'kill-this-buffer)
3775 (define-key map "\t" (lambda ()
3776 (interactive)
3777 (gdb-set-window-buffer
3778 (gdb-get-buffer-create
3779 'gdb-locals-buffer
3780 gdb-thread-number) t)))
3781 map))
3783 (defvar gdb-registers-header
3784 (list
3785 (gdb-propertize-header "Locals" gdb-locals-buffer
3786 "mouse-1: select" mode-line-highlight
3787 mode-line-inactive)
3789 (gdb-propertize-header "Registers" gdb-registers-buffer
3790 nil nil mode-line)))
3792 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3793 "Major mode for gdb registers."
3794 (setq header-line-format gdb-registers-header)
3795 'gdb-invalidate-registers)
3797 (defun gdb-registers-buffer-name ()
3798 (gdb-current-context-buffer-name
3799 (concat "registers of " (gdb-get-target-string))))
3801 (defun gdb-display-registers-buffer (&optional thread)
3802 "Display GDB register contents."
3803 (interactive)
3804 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
3806 (def-gdb-preempt-display-buffer
3807 gdb-preemptively-display-registers-buffer
3808 'gdb-registers-buffer nil t)
3810 (defun gdb-frame-registers-buffer (&optional thread)
3811 "Display GDB register contents in another frame."
3812 (interactive)
3813 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
3814 gdb-display-buffer-other-frame-action))
3816 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3817 (defun gdb-get-changed-registers ()
3818 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3819 (not (gdb-pending-p 'gdb-get-changed-registers)))
3820 (gdb-input "-data-list-changed-registers"
3821 'gdb-changed-registers-handler)
3822 (gdb-add-pending 'gdb-get-changed-registers)))
3824 (defun gdb-changed-registers-handler ()
3825 (gdb-delete-pending 'gdb-get-changed-registers)
3826 (setq gdb-changed-registers nil)
3827 (dolist (register-number
3828 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3829 (push register-number gdb-changed-registers)))
3831 (defun gdb-register-names-handler ()
3832 ;; Don't use gdb-pending-triggers because this handler is called
3833 ;; only once (in gdb-init-1)
3834 (setq gdb-register-names nil)
3835 (dolist (register-name
3836 (bindat-get-field (gdb-json-partial-output) 'register-names))
3837 (push register-name gdb-register-names))
3838 (setq gdb-register-names (reverse gdb-register-names)))
3841 (defun gdb-get-source-file-list ()
3842 "Create list of source files for current GDB session.
3843 If buffers already exist for any of these files, gud-minor-mode
3844 is set in them."
3845 (goto-char (point-min))
3846 (while (re-search-forward gdb-source-file-regexp nil t)
3847 (push (match-string 1) gdb-source-file-list))
3848 (dolist (buffer (buffer-list))
3849 (with-current-buffer buffer
3850 (when (member buffer-file-name gdb-source-file-list)
3851 (gdb-init-buffer)))))
3853 (defun gdb-get-main-selected-frame ()
3854 "Trigger for `gdb-frame-handler' which uses main current
3855 thread. Called from `gdb-update'."
3856 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3857 (progn
3858 (gdb-input (gdb-current-context-command "-stack-info-frame")
3859 'gdb-frame-handler)
3860 (gdb-add-pending 'gdb-get-main-selected-frame))))
3862 (defun gdb-frame-handler ()
3863 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3864 overlay arrow in source buffer."
3865 (gdb-delete-pending 'gdb-get-main-selected-frame)
3866 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3867 (when frame
3868 (setq gdb-selected-frame (bindat-get-field frame 'func))
3869 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3870 (setq gdb-frame-number (bindat-get-field frame 'level))
3871 (setq gdb-frame-address (bindat-get-field frame 'addr))
3872 (let ((line (bindat-get-field frame 'line)))
3873 (setq gdb-selected-line (and line (string-to-number line)))
3874 (when (and gdb-selected-file gdb-selected-line)
3875 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3876 (gud-display-frame)))
3877 (if gud-overlay-arrow-position
3878 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3879 (position (marker-position gud-overlay-arrow-position)))
3880 (when buffer
3881 (with-current-buffer buffer
3882 (setq fringe-indicator-alist
3883 (if (string-equal gdb-frame-number "0")
3885 '((overlay-arrow . hollow-right-triangle))))
3886 (setq gud-overlay-arrow-position (make-marker))
3887 (set-marker gud-overlay-arrow-position position))))))))
3889 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3891 (defun gdb-get-prompt ()
3892 "Find prompt for GDB session."
3893 (goto-char (point-min))
3894 (setq gdb-prompt-name nil)
3895 (re-search-forward gdb-prompt-name-regexp nil t)
3896 (setq gdb-prompt-name (match-string 1))
3897 ;; Insert first prompt.
3898 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3900 ;;;; Window management
3901 (defun gdb-display-buffer (buf)
3902 "Show buffer BUF, and make that window dedicated."
3903 (let ((window (display-buffer buf)))
3904 (set-window-dedicated-p window t)
3905 window))
3907 ;; (let ((answer (get-buffer-window buf 0)))
3908 ;; (if answer
3909 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
3910 ;; (let ((window (get-lru-window)))
3911 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3912 ;; 'gdbmi)
3913 ;; (let ((largest (get-largest-window)))
3914 ;; (setq answer (split-window largest))
3915 ;; (set-window-buffer answer buf)
3916 ;; (set-window-dedicated-p answer t)
3917 ;; answer)
3918 ;; (set-window-buffer window buf)
3919 ;; window)))))
3922 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3923 "Find window displaying a buffer with the same
3924 `gdb-buffer-type' as BUF and show BUF there. If no such window
3925 exists, just call `gdb-display-buffer' for BUF. If the window
3926 found is already dedicated, split window according to
3927 SPLIT-HORIZONTAL and show BUF in the new window."
3928 (if buf
3929 (when (not (get-buffer-window buf))
3930 (let* ((buf-type (gdb-buffer-type buf))
3931 (existing-window
3932 (get-window-with-predicate
3933 #'(lambda (w)
3934 (and (eq buf-type
3935 (gdb-buffer-type (window-buffer w)))
3936 (not (window-dedicated-p w)))))))
3937 (if existing-window
3938 (set-window-buffer existing-window buf)
3939 (let ((dedicated-window
3940 (get-window-with-predicate
3941 #'(lambda (w)
3942 (eq buf-type
3943 (gdb-buffer-type (window-buffer w)))))))
3944 (if dedicated-window
3945 (set-window-buffer
3946 (split-window dedicated-window nil split-horizontal) buf)
3947 (gdb-display-buffer buf))))))
3948 (error "Null buffer")))
3950 ;;; Shared keymap initialization:
3952 (let ((menu (make-sparse-keymap "GDB-Windows")))
3953 (define-key gud-menu-map [displays]
3954 `(menu-item "GDB-Windows" ,menu
3955 :visible (eq gud-minor-mode 'gdbmi)))
3956 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3957 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3958 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3959 (define-key menu [disassembly]
3960 '("Disassembly" . gdb-display-disassembly-buffer))
3961 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3962 (define-key menu [inferior]
3963 '("IO" . gdb-display-io-buffer))
3964 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3965 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3966 (define-key menu [breakpoints]
3967 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3969 (let ((menu (make-sparse-keymap "GDB-Frames")))
3970 (define-key gud-menu-map [frames]
3971 `(menu-item "GDB-Frames" ,menu
3972 :visible (eq gud-minor-mode 'gdbmi)))
3973 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3974 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3975 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3976 (define-key menu [disassembly]
3977 '("Disassembly" . gdb-frame-disassembly-buffer))
3978 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3979 (define-key menu [inferior]
3980 '("IO" . gdb-frame-io-buffer))
3981 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3982 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3983 (define-key menu [breakpoints]
3984 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3986 (let ((menu (make-sparse-keymap "GDB-MI")))
3987 (define-key menu [gdb-customize]
3988 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3989 :help "Customize Gdb Graphical Mode options."))
3990 (define-key menu [gdb-many-windows]
3991 '(menu-item "Display Other Windows" gdb-many-windows
3992 :help "Toggle display of locals, stack and breakpoint information"
3993 :button (:toggle . gdb-many-windows)))
3994 (define-key menu [gdb-restore-windows]
3995 '(menu-item "Restore Window Layout" gdb-restore-windows
3996 :help "Restore standard layout for debug session."))
3997 (define-key menu [sep1]
3998 '(menu-item "--"))
3999 (define-key menu [all-threads]
4000 '(menu-item "GUD controls all threads"
4001 (lambda ()
4002 (interactive)
4003 (setq gdb-gud-control-all-threads t))
4004 :help "GUD start/stop commands apply to all threads"
4005 :button (:radio . gdb-gud-control-all-threads)))
4006 (define-key menu [current-thread]
4007 '(menu-item "GUD controls current thread"
4008 (lambda ()
4009 (interactive)
4010 (setq gdb-gud-control-all-threads nil))
4011 :help "GUD start/stop commands apply to current thread only"
4012 :button (:radio . (not gdb-gud-control-all-threads))))
4013 (define-key menu [sep2]
4014 '(menu-item "--"))
4015 (define-key menu [gdb-customize-reasons]
4016 '(menu-item "Customize switching..."
4017 (lambda ()
4018 (interactive)
4019 (customize-option 'gdb-switch-reasons))))
4020 (define-key menu [gdb-switch-when-another-stopped]
4021 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4022 gdb-switch-when-another-stopped
4023 "Automatically switch to stopped thread"
4024 "GDB thread switching %s"
4025 "Switch to stopped thread"))
4026 (define-key gud-menu-map [mi]
4027 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4029 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4030 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4031 ;; tool-bar-local-item-from-menu here because it appends new buttons
4032 ;; to toolbar from right to left while we want our A/T throttle to
4033 ;; show up right before Run button.
4034 (define-key-after gud-tool-bar-map [all-threads]
4035 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4036 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4037 :visible (and (eq gud-minor-mode 'gdbmi)
4038 gdb-non-stop
4039 (not gdb-gud-control-all-threads)))
4040 'run)
4042 (define-key-after gud-tool-bar-map [current-thread]
4043 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4044 :image (find-image '((:type xpm :file "gud/all.xpm")))
4045 :visible (and (eq gud-minor-mode 'gdbmi)
4046 gdb-non-stop
4047 gdb-gud-control-all-threads))
4048 'all-threads)
4050 (defun gdb-frame-gdb-buffer ()
4051 "Display GUD buffer in another frame."
4052 (interactive)
4053 (display-buffer-other-frame gud-comint-buffer))
4055 (defun gdb-display-gdb-buffer ()
4056 "Display GUD buffer."
4057 (interactive)
4058 (pop-to-buffer gud-comint-buffer nil 0))
4060 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4061 "Set buffer of selected window to NAME and dedicate window.
4063 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4064 window is dedicated."
4065 (unless window (setq window (selected-window)))
4066 (when ignore-dedicated
4067 (set-window-dedicated-p window nil))
4068 (set-window-buffer window (get-buffer name))
4069 (set-window-dedicated-p window t))
4071 (defun gdb-setup-windows ()
4072 "Layout the window pattern for `gdb-many-windows'."
4073 (gdb-get-buffer-create 'gdb-locals-buffer)
4074 (gdb-get-buffer-create 'gdb-stack-buffer)
4075 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4076 (set-window-dedicated-p (selected-window) nil)
4077 (switch-to-buffer gud-comint-buffer)
4078 (delete-other-windows)
4079 (let ((win0 (selected-window))
4080 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4081 (win2 (split-window nil ( / (window-height) 3)))
4082 (win3 (split-window-right)))
4083 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4084 (select-window win2)
4085 (set-window-buffer
4086 win2
4087 (if gud-last-last-frame
4088 (gud-find-file (car gud-last-last-frame))
4089 (if gdb-main-file
4090 (gud-find-file gdb-main-file)
4091 ;; Put buffer list in window if we
4092 ;; can't find a source file.
4093 (list-buffers-noselect))))
4094 (setq gdb-source-window (selected-window))
4095 (let ((win4 (split-window-right)))
4096 (gdb-set-window-buffer
4097 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4098 (select-window win1)
4099 (gdb-set-window-buffer (gdb-stack-buffer-name))
4100 (let ((win5 (split-window-right)))
4101 (gdb-set-window-buffer (if gdb-show-threads-by-default
4102 (gdb-threads-buffer-name)
4103 (gdb-breakpoints-buffer-name))
4104 nil win5))
4105 (select-window win0)))
4107 (define-minor-mode gdb-many-windows
4108 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4109 In this case it starts with two windows: one displaying the GUD
4110 buffer and the other with the source file with the main routine
4111 of the debugged program. Non-nil means display the layout shown for
4112 `gdb'."
4113 :global t
4114 :group 'gdb
4115 :version "22.1"
4116 (if (and gud-comint-buffer
4117 (buffer-name gud-comint-buffer))
4118 (ignore-errors
4119 (gdb-restore-windows))))
4121 (defun gdb-restore-windows ()
4122 "Restore the basic arrangement of windows used by gdb.
4123 This arrangement depends on the value of `gdb-many-windows'."
4124 (interactive)
4125 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4126 (delete-other-windows)
4127 (if gdb-many-windows
4128 (gdb-setup-windows)
4129 (when (or gud-last-last-frame gdb-show-main)
4130 (let ((win (split-window)))
4131 (set-window-buffer
4133 (if gud-last-last-frame
4134 (gud-find-file (car gud-last-last-frame))
4135 (gud-find-file gdb-main-file)))
4136 (setq gdb-source-window win)))))
4138 ;; Called from `gud-sentinel' in gud.el:
4139 (defun gdb-reset ()
4140 "Exit a debugging session cleanly.
4141 Kills the gdb buffers, and resets variables and the source buffers."
4142 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4143 ;; process. This pty must be deleted explicitly.
4144 (let ((pty (get-process "gdb-inferior")))
4145 (if pty (delete-process pty)))
4146 ;; Find gdb-mi buffers and kill them.
4147 (dolist (buffer (buffer-list))
4148 (unless (eq buffer gud-comint-buffer)
4149 (with-current-buffer buffer
4150 (if (eq gud-minor-mode 'gdbmi)
4151 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4152 (kill-buffer nil)
4153 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4154 (setq gud-minor-mode nil)
4155 (kill-local-variable 'tool-bar-map)
4156 (kill-local-variable 'gdb-define-alist))))))
4157 (setq gdb-disassembly-position nil)
4158 (setq overlay-arrow-variable-list
4159 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4160 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4161 (setq gdb-stack-position nil)
4162 (setq overlay-arrow-variable-list
4163 (delq 'gdb-stack-position overlay-arrow-variable-list))
4164 (setq gdb-thread-position nil)
4165 (setq overlay-arrow-variable-list
4166 (delq 'gdb-thread-position overlay-arrow-variable-list))
4167 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4168 (setq gud-running nil)
4169 (setq gdb-active-process nil)
4170 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4172 (defun gdb-get-source-file ()
4173 "Find the source file where the program starts and display it with related
4174 buffers, if required."
4175 (goto-char (point-min))
4176 (if (re-search-forward gdb-source-file-regexp nil t)
4177 (setq gdb-main-file (match-string 1)))
4178 (if gdb-many-windows
4179 (gdb-setup-windows)
4180 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4181 (and gdb-show-main
4182 gdb-main-file
4183 (display-buffer (gud-find-file gdb-main-file))))
4184 (gdb-force-mode-line-update
4185 (propertize "ready" 'face font-lock-variable-name-face)))
4187 ;;from put-image
4188 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4189 "Put string PUTSTRING in front of POS in the current buffer.
4190 PUTSTRING is displayed by putting an overlay into the current buffer with a
4191 `before-string' string that has a `display' property whose value is
4192 PUTSTRING."
4193 (let ((string (make-string 1 ?x))
4194 (buffer (current-buffer)))
4195 (setq putstring (copy-sequence putstring))
4196 (let ((overlay (make-overlay pos pos buffer))
4197 (prop (or dprop
4198 (list (list 'margin 'left-margin) putstring))))
4199 (put-text-property 0 1 'display prop string)
4200 (if sprops
4201 (add-text-properties 0 1 sprops string))
4202 (overlay-put overlay 'put-break t)
4203 (overlay-put overlay 'before-string string))))
4205 ;;from remove-images
4206 (defun gdb-remove-strings (start end &optional buffer)
4207 "Remove strings between START and END in BUFFER.
4208 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4209 BUFFER nil or omitted means use the current buffer."
4210 (unless buffer
4211 (setq buffer (current-buffer)))
4212 (dolist (overlay (overlays-in start end))
4213 (when (overlay-get overlay 'put-break)
4214 (delete-overlay overlay))))
4216 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4217 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4218 (start (- (car posns) 1))
4219 (end (+ (cdr posns) 1))
4220 (putstring (if enabled "B" "b"))
4221 (source-window (get-buffer-window (current-buffer) 0)))
4222 (add-text-properties
4223 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4224 putstring)
4225 (if enabled
4226 (add-text-properties
4227 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4228 (add-text-properties
4229 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4230 (gdb-remove-breakpoint-icons start end)
4231 (if (display-images-p)
4232 (if (>= (or left-fringe-width
4233 (if source-window (car (window-fringes source-window)))
4234 gdb-buffer-fringe-width) 8)
4235 (gdb-put-string
4236 nil (1+ start)
4237 `(left-fringe breakpoint
4238 ,(if enabled
4239 'breakpoint-enabled
4240 'breakpoint-disabled))
4241 'gdb-bptno bptno
4242 'gdb-enabled enabled)
4243 (when (< left-margin-width 2)
4244 (save-current-buffer
4245 (setq left-margin-width 2)
4246 (if source-window
4247 (set-window-margins
4248 source-window
4249 left-margin-width right-margin-width))))
4250 (put-image
4251 (if enabled
4252 (or breakpoint-enabled-icon
4253 (setq breakpoint-enabled-icon
4254 (find-image `((:type xpm :data
4255 ,breakpoint-xpm-data
4256 :ascent 100 :pointer hand)
4257 (:type pbm :data
4258 ,breakpoint-enabled-pbm-data
4259 :ascent 100 :pointer hand)))))
4260 (or breakpoint-disabled-icon
4261 (setq breakpoint-disabled-icon
4262 (find-image `((:type xpm :data
4263 ,breakpoint-xpm-data
4264 :conversion disabled
4265 :ascent 100 :pointer hand)
4266 (:type pbm :data
4267 ,breakpoint-disabled-pbm-data
4268 :ascent 100 :pointer hand))))))
4269 (+ start 1)
4270 putstring
4271 'left-margin))
4272 (when (< left-margin-width 2)
4273 (save-current-buffer
4274 (setq left-margin-width 2)
4275 (let ((window (get-buffer-window (current-buffer) 0)))
4276 (if window
4277 (set-window-margins
4278 window left-margin-width right-margin-width)))))
4279 (gdb-put-string
4280 (propertize putstring
4281 'face (if enabled
4282 'breakpoint-enabled 'breakpoint-disabled))
4283 (1+ start)))))
4285 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4286 (gdb-remove-strings start end)
4287 (if (display-images-p)
4288 (remove-images start end))
4289 (when remove-margin
4290 (setq left-margin-width 0)
4291 (let ((window (get-buffer-window (current-buffer) 0)))
4292 (if window
4293 (set-window-margins
4294 window left-margin-width right-margin-width)))))
4297 ;;; Functions for inline completion.
4299 (defvar gud-gdb-fetch-lines-in-progress)
4300 (defvar gud-gdb-fetch-lines-string)
4301 (defvar gud-gdb-fetch-lines-break)
4302 (defvar gud-gdb-fetched-lines)
4304 (defun gud-gdbmi-completions (context command)
4305 "Completion table for GDB/MI commands.
4306 COMMAND is the prefix for which we seek completion.
4307 CONTEXT is the text before COMMAND on the line."
4308 (let ((gud-gdb-fetch-lines-in-progress t)
4309 (gud-gdb-fetch-lines-string nil)
4310 (gud-gdb-fetch-lines-break (length context))
4311 (gud-gdb-fetched-lines nil)
4312 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4313 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4314 complete-list)
4315 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4316 (gdb-input (concat "complete " context command)
4317 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4318 (while gud-gdb-fetch-lines-in-progress
4319 (accept-process-output (get-buffer-process gud-comint-buffer))))
4320 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4322 (defun gud-gdbmi-fetch-lines-filter (string)
4323 "Custom filter function for `gud-gdbmi-completions'."
4324 (setq string (concat gud-gdb-fetch-lines-string
4325 (gud-gdbmi-marker-filter string)))
4326 (while (string-match "\n" string)
4327 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4328 gud-gdb-fetched-lines)
4329 (setq string (substring string (match-end 0))))
4332 (provide 'gdb-mi)
4334 ;;; gdb-mi.el ends here