* net/gnutls.el (gnutls-min-prime-bits): Improve docstring.
[emacs.git] / lisp / progmodes / gdb-mi.el
blobb19c828d1710226d4da6cd258e52c1898995c865
1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007-2012 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))
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 (defcustom gdb-enable-debug nil
463 "Non-nil means record the process input and output in `gdb-debug-log'."
464 :type 'boolean
465 :group 'gdb
466 :version "22.1")
468 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
469 "Shell command for generating a list of defined macros in a source file.
470 This list is used to display the #define directive associated
471 with an identifier as a tooltip. It works in a debug session with
472 GDB, when `gud-tooltip-mode' is t.
474 Set `gdb-cpp-define-alist-flags' for any include paths or
475 predefined macros."
476 :type 'string
477 :group 'gdb
478 :version "22.1")
480 (defcustom gdb-cpp-define-alist-flags ""
481 "Preprocessor flags for `gdb-cpp-define-alist-program'."
482 :type 'string
483 :group 'gdb
484 :version "22.1")
486 (defcustom gdb-create-source-file-list t
487 "Non-nil means create a list of files from which the executable was built.
488 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
489 line for a long time when starting, possibly because your executable was
490 built from a large number of files. This allows quicker initialization
491 but means that these files are not automatically enabled for debugging,
492 e.g., you won't be able to click in the fringe to set a breakpoint until
493 execution has already stopped there."
494 :type 'boolean
495 :group 'gdb
496 :version "23.1")
498 (defcustom gdb-show-main nil
499 "Non-nil means display source file containing the main routine at startup.
500 Also display the main routine in the disassembly buffer if present."
501 :type 'boolean
502 :group 'gdb
503 :version "22.1")
505 (defun gdb-force-mode-line-update (status)
506 (let ((buffer gud-comint-buffer))
507 (if (and buffer (buffer-name buffer))
508 (with-current-buffer buffer
509 (setq mode-line-process
510 (format ":%s [%s]"
511 (process-status (get-buffer-process buffer)) status))
512 ;; Force mode line redisplay soon.
513 (force-mode-line-update)))))
515 (defun gdb-enable-debug (arg)
516 "Toggle logging of transaction between Emacs and Gdb.
517 The log is stored in `gdb-debug-log' as an alist with elements
518 whose cons is send, send-item or recv and whose cdr is the string
519 being transferred. This list may grow up to a size of
520 `gdb-debug-log-max' after which the oldest element (at the end of
521 the list) is deleted every time a new one is added (at the front)."
522 (interactive "P")
523 (setq gdb-enable-debug
524 (if (null arg)
525 (not gdb-enable-debug)
526 (> (prefix-numeric-value arg) 0)))
527 (message (format "Logging of transaction %sabled"
528 (if gdb-enable-debug "en" "dis"))))
530 ;; These two are used for menu and toolbar
531 (defun gdb-control-all-threads ()
532 "Switch to non-stop/A mode."
533 (interactive)
534 (setq gdb-gud-control-all-threads t)
535 ;; Actually forcing the tool-bar to update.
536 (force-mode-line-update)
537 (message "Now in non-stop/A mode."))
539 (defun gdb-control-current-thread ()
540 "Switch to non-stop/T mode."
541 (interactive)
542 (setq gdb-gud-control-all-threads nil)
543 ;; Actually forcing the tool-bar to update.
544 (force-mode-line-update)
545 (message "Now in non-stop/T mode."))
547 (defun gdb-find-watch-expression ()
548 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
549 (varnum (car var)) expr)
550 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
551 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
552 (component-list (split-string (match-string 2 varnum) "\\." t)))
553 (setq expr (nth 1 var1))
554 (setq varnumlet (car var1))
555 (dolist (component component-list)
556 (setq var2 (assoc varnumlet gdb-var-list))
557 (setq expr (concat expr
558 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
559 (concat "[" component "]")
560 (concat "." component))))
561 (setq varnumlet (concat varnumlet "." component)))
562 expr)))
564 ;; noall is used for commands which don't take --all, but only
565 ;; --thread.
566 (defun gdb-gud-context-command (command &optional noall)
567 "When `gdb-non-stop' is t, add --thread option to COMMAND if
568 `gdb-gud-control-all-threads' is nil and --all option otherwise.
569 If NOALL is t, always add --thread option no matter what
570 `gdb-gud-control-all-threads' value is.
572 When `gdb-non-stop' is nil, return COMMAND unchanged."
573 (if gdb-non-stop
574 (if (and gdb-gud-control-all-threads
575 (not noall)
576 gdb-supports-non-stop)
577 (concat command " --all ")
578 (gdb-current-context-command command))
579 command))
581 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
582 "`gud-call' wrapper which adds --thread/--all options between
583 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
585 NOARG must be t when this macro is used outside `gud-def'"
586 `(gud-call
587 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
588 ,(when (not noarg) 'arg)))
590 (defun gdb--check-interpreter (proc string)
591 (unless (zerop (length string))
592 (let ((filter (process-get proc 'gud-normal-filter)))
593 (set-process-filter proc filter)
594 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
595 ;; Apparently we're not running with -i=mi.
596 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
597 (message msg)
598 (setq string (concat (propertize msg 'font-lock-face 'error)
599 "\n" string)))
600 ;; Use the old gud-gbd filter, not because it works, but because it
601 ;; will properly display GDB's answers rather than hanging waiting for
602 ;; answers that aren't coming.
603 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
604 (funcall filter proc string))))
606 (defvar gdb-control-level 0)
608 ;;;###autoload
609 (defun gdb (command-line)
610 "Run gdb on program FILE in buffer *gud-FILE*.
611 The directory containing FILE becomes the initial working directory
612 and source-file directory for your debugger.
614 COMMAND-LINE is the shell command for starting the gdb session.
615 It should be a string consisting of the name of the gdb
616 executable followed by command-line options. The command-line
617 options should include \"-i=mi\" to use gdb's MI text interface.
618 Note that the old \"--annotate\" option is no longer supported.
620 If `gdb-many-windows' is nil (the default value) then gdb just
621 pops up the GUD buffer unless `gdb-show-main' is t. In this case
622 it starts with two windows: one displaying the GUD buffer and the
623 other with the source file with the main routine of the inferior.
625 If `gdb-many-windows' is t, regardless of the value of
626 `gdb-show-main', the layout below will appear. Keybindings are
627 shown in some of the buffers.
629 Watch expressions appear in the speedbar/slowbar.
631 The following commands help control operation :
633 `gdb-many-windows' - Toggle the number of windows gdb uses.
634 `gdb-restore-windows' - To restore the window layout.
636 See Info node `(emacs)GDB Graphical Interface' for a more
637 detailed description of this mode.
640 +----------------------------------------------------------------------+
641 | GDB Toolbar |
642 +-----------------------------------+----------------------------------+
643 | GUD buffer (I/O of GDB) | Locals buffer |
644 | | |
645 | | |
646 | | |
647 +-----------------------------------+----------------------------------+
648 | Source buffer | I/O buffer (of debugged program) |
649 | | (comint-mode) |
650 | | |
651 | | |
652 | | |
653 | | |
654 | | |
655 | | |
656 +-----------------------------------+----------------------------------+
657 | Stack buffer | Breakpoints buffer |
658 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
659 | | RET gdb-goto-breakpoint |
660 | | D gdb-delete-breakpoint |
661 +-----------------------------------+----------------------------------+"
663 (interactive (list (gud-query-cmdline 'gdb)))
665 (when (and gud-comint-buffer
666 (buffer-name gud-comint-buffer)
667 (get-buffer-process gud-comint-buffer)
668 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
669 (gdb-restore-windows)
670 (error
671 "Multiple debugging requires restarting in text command mode"))
673 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
675 ;; Setup a temporary process filter to warn when GDB was not started
676 ;; with -i=mi.
677 (let ((proc (get-buffer-process gud-comint-buffer)))
678 (process-put proc 'gud-normal-filter (process-filter proc))
679 (set-process-filter proc #'gdb--check-interpreter))
681 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
682 (set (make-local-variable 'gdb-control-level) 0)
683 (setq comint-input-sender 'gdb-send)
684 (when (ring-empty-p comint-input-ring) ; cf shell-mode
685 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
686 (if (eq system-type 'ms-dos)
687 "_gdb_history"
688 ".gdb_history"))))
689 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
690 (hsize (getenv "HISTSIZE")))
691 (dolist (file (append '("~/.gdbinit")
692 (unless (string-equal (expand-file-name ".")
693 (expand-file-name "~"))
694 '(".gdbinit"))))
695 (if (file-readable-p (setq file (expand-file-name file)))
696 (with-temp-buffer
697 (insert-file-contents file)
698 ;; TODO? check for "set history save\\( *on\\)?" and do
699 ;; not use history otherwise?
700 (while (re-search-forward
701 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
702 (cond ((string-equal (match-string 1) "filename")
703 (setq hfile (expand-file-name
704 (match-string 2)
705 (file-name-directory file))))
706 ((string-equal (match-string 1) "size")
707 (setq hsize (match-string 2))))))))
708 (and (stringp hsize)
709 (integerp (setq hsize (string-to-number hsize)))
710 (> hsize 0)
711 (set (make-local-variable 'comint-input-ring-size) hsize))
712 (if (stringp hfile)
713 (set (make-local-variable 'comint-input-ring-file-name) hfile))
714 (comint-read-input-ring t)))
715 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
716 "Set temporary breakpoint at current line.")
717 (gud-def gud-jump
718 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
719 "\C-j" "Set execution address to current line.")
721 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
722 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
723 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
724 (gud-def gud-pstar "print* %e" nil
725 "Evaluate C dereferenced pointer expression at point.")
727 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
728 "\C-s"
729 "Step one source line with display.")
730 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
731 "\C-i"
732 "Step one instruction with display.")
733 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
734 "\C-n"
735 "Step one line (skip functions).")
736 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
738 "Step one instruction (skip functions).")
739 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
740 "\C-r"
741 "Continue with display.")
742 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
743 "\C-f"
744 "Finish executing current function.")
745 (gud-def gud-run "-exec-run"
747 "Run the program.")
749 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
750 (gud-call "break %f:%l" arg)
751 (save-excursion
752 (beginning-of-line)
753 (forward-char 2)
754 (gud-call "break *%a" arg)))
755 "\C-b" "Set breakpoint at current line or address.")
757 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
758 (gud-call "clear %f:%l" arg)
759 (save-excursion
760 (beginning-of-line)
761 (forward-char 2)
762 (gud-call "clear *%a" arg)))
763 "\C-d" "Remove breakpoint at current line or address.")
765 ;; -exec-until doesn't support --all yet
766 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
767 (gud-call "-exec-until %f:%l" arg)
768 (save-excursion
769 (beginning-of-line)
770 (forward-char 2)
771 (gud-call "-exec-until *%a" arg)))
772 "\C-u" "Continue to current line or address.")
773 ;; TODO Why arg here?
774 (gud-def
775 gud-go (gud-call (if gdb-active-process
776 (gdb-gud-context-command "-exec-continue")
777 "-exec-run") arg)
778 nil "Start or continue execution.")
780 ;; For debugging Emacs only.
781 (gud-def gud-pp
782 (gud-call
783 (concat
784 "pp " (if (eq (buffer-local-value
785 'major-mode (window-buffer)) 'speedbar-mode)
786 (gdb-find-watch-expression) "%e")) arg)
787 nil "Print the Emacs s-expression.")
789 (define-key gud-minor-mode-map [left-margin mouse-1]
790 'gdb-mouse-set-clear-breakpoint)
791 (define-key gud-minor-mode-map [left-fringe mouse-1]
792 'gdb-mouse-set-clear-breakpoint)
793 (define-key gud-minor-mode-map [left-margin C-mouse-1]
794 'gdb-mouse-toggle-breakpoint-margin)
795 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
796 'gdb-mouse-toggle-breakpoint-fringe)
798 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
799 'gdb-mouse-until)
800 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
801 'gdb-mouse-until)
802 (define-key gud-minor-mode-map [left-margin mouse-3]
803 'gdb-mouse-until)
804 (define-key gud-minor-mode-map [left-fringe mouse-3]
805 'gdb-mouse-until)
807 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
808 'gdb-mouse-jump)
809 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
810 'gdb-mouse-jump)
811 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
812 'gdb-mouse-jump)
813 (define-key gud-minor-mode-map [left-margin C-mouse-3]
814 'gdb-mouse-jump)
816 (set (make-local-variable 'gud-gdb-completion-function)
817 'gud-gdbmi-completions)
819 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
820 nil 'local)
821 (local-set-key "\C-i" 'completion-at-point)
823 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
825 (setq gdb-first-prompt t)
826 (setq gud-running nil)
828 (gdb-update)
830 (run-hooks 'gdb-mode-hook))
832 (defun gdb-init-1 ()
833 ;; (re-)initialize
834 (setq gdb-selected-frame nil
835 gdb-frame-number nil
836 gdb-thread-number nil
837 gdb-var-list nil
838 gdb-pending-triggers nil
839 gdb-output-sink 'user
840 gdb-location-alist nil
841 gdb-source-file-list nil
842 gdb-last-command nil
843 gdb-token-number 0
844 gdb-handler-alist '()
845 gdb-handler-number nil
846 gdb-prompt-name nil
847 gdb-first-done-or-error t
848 gdb-buffer-fringe-width (car (window-fringes))
849 gdb-debug-log nil
850 gdb-source-window nil
851 gdb-inferior-status nil
852 gdb-continuation nil
853 gdb-buf-publisher '()
854 gdb-threads-list '()
855 gdb-breakpoints-list '()
856 gdb-register-names '()
857 gdb-non-stop gdb-non-stop-setting)
859 (setq gdb-buffer-type 'gdbmi)
861 (gdb-force-mode-line-update
862 (propertize "initializing..." 'face font-lock-variable-name-face))
864 (gdb-get-buffer-create 'gdb-inferior-io)
865 (gdb-clear-inferior-io)
866 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
868 (if (eq window-system 'w32)
869 (gdb-input "-gdb-set new-console off" 'ignore))
870 (gdb-input "-gdb-set height 0" 'ignore)
872 (when gdb-non-stop
873 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
875 (gdb-input "-enable-pretty-printing" 'ignore)
877 ;; find source file and compilation directory here
878 (if gdb-create-source-file-list
879 ;; Needs GDB 6.2 onwards.
880 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
881 ;; Needs GDB 6.0 onwards.
882 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
883 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
885 (defun gdb-non-stop-handler ()
886 (goto-char (point-min))
887 (if (re-search-forward "No symbol" nil t)
888 (progn
889 (message
890 "This version of GDB doesn't support non-stop mode. Turning it off.")
891 (setq gdb-non-stop nil)
892 (setq gdb-supports-non-stop nil))
893 (setq gdb-supports-non-stop t)
894 (gdb-input "-gdb-set target-async 1" 'ignore)
895 (gdb-input "-list-target-features" 'gdb-check-target-async)))
897 (defun gdb-check-target-async ()
898 (goto-char (point-min))
899 (unless (re-search-forward "async" nil t)
900 (message
901 "Target doesn't support non-stop mode. Turning it off.")
902 (setq gdb-non-stop nil)
903 (gdb-input "-gdb-set non-stop 0" 'ignore)))
905 (defun gdb-delchar-or-quit (arg)
906 "Delete ARG characters or send a quit command to GDB.
907 Send a quit only if point is at the end of the buffer, there is
908 no input, and GDB is waiting for input."
909 (interactive "p")
910 (unless (and (eq (current-buffer) gud-comint-buffer)
911 (eq gud-minor-mode 'gdbmi))
912 (error "Not in a GDB-MI buffer"))
913 (let ((proc (get-buffer-process gud-comint-buffer)))
914 (if (and (eobp) proc (process-live-p proc)
915 (not gud-running)
916 (= (point) (marker-position (process-mark proc))))
917 ;; Sending an EOF does not work with GDB-MI; submit an
918 ;; explicit quit command.
919 (progn
920 (insert "quit")
921 (comint-send-input t t))
922 (delete-char arg))))
924 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
926 (defun gdb-create-define-alist ()
927 "Create an alist of #define directives for GUD tooltips."
928 (let* ((file (buffer-file-name))
929 (output
930 (with-output-to-string
931 (with-current-buffer standard-output
932 (and file
933 (file-exists-p file)
934 ;; call-process doesn't work with remote file names.
935 (not (file-remote-p default-directory))
936 (call-process shell-file-name file
937 (list t nil) nil "-c"
938 (concat gdb-cpp-define-alist-program " "
939 gdb-cpp-define-alist-flags))))))
940 (define-list (split-string output "\n" t))
941 (name))
942 (setq gdb-define-alist nil)
943 (dolist (define define-list)
944 (setq name (nth 1 (split-string define "[( ]")))
945 (push (cons name define) gdb-define-alist))))
947 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
949 (defun gdb-tooltip-print (expr)
950 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
951 (goto-char (point-min))
952 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
953 (tooltip-show
954 (concat expr " = " (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 &rest body)
978 `(if ,arrow-position
979 (let ((buffer (marker-buffer ,arrow-position)) (line))
980 (if (equal buffer (window-buffer (posn-window end)))
981 (with-current-buffer buffer
982 (when (or (equal start end)
983 (equal (posn-point start)
984 (marker-position ,arrow-position)))
985 ,@body))))))
987 (defun gdb-mouse-until (event)
988 "Continue running until a source line past the current line.
989 The destination source line can be selected either by clicking
990 with mouse-3 on the fringe/margin or dragging the arrow
991 with mouse-1 (default bindings)."
992 (interactive "e")
993 (let ((start (event-start event))
994 (end (event-end event)))
995 (gdb-if-arrow gud-overlay-arrow-position
996 (setq line (line-number-at-pos (posn-point end)))
997 (gud-call (concat "until " (number-to-string line))))
998 (gdb-if-arrow gdb-disassembly-position
999 (save-excursion
1000 (goto-char (point-min))
1001 (forward-line (1- (line-number-at-pos (posn-point end))))
1002 (forward-char 2)
1003 (gud-call (concat "until *%a"))))))
1005 (defun gdb-mouse-jump (event)
1006 "Set execution address/line.
1007 The destination source line can be selected either by clicking with C-mouse-3
1008 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1009 Unlike `gdb-mouse-until' the destination address can be before the current
1010 line, and no execution takes place."
1011 (interactive "e")
1012 (let ((start (event-start event))
1013 (end (event-end event)))
1014 (gdb-if-arrow gud-overlay-arrow-position
1015 (setq line (line-number-at-pos (posn-point end)))
1016 (progn
1017 (gud-call (concat "tbreak " (number-to-string line)))
1018 (gud-call (concat "jump " (number-to-string line)))))
1019 (gdb-if-arrow gdb-disassembly-position
1020 (save-excursion
1021 (goto-char (point-min))
1022 (forward-line (1- (line-number-at-pos (posn-point end))))
1023 (forward-char 2)
1024 (progn
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 (defcustom gdb-speedbar-auto-raise nil
1049 "If non-nil raise speedbar every time display of watch expressions is\
1050 updated."
1051 :type 'boolean
1052 :group 'gdb
1053 :version "22.1")
1055 (defcustom gdb-use-colon-colon-notation nil
1056 "If non-nil use FUN::VAR format to display variables in the speedbar."
1057 :type 'boolean
1058 :group 'gdb
1059 :version "22.1")
1061 (defun gdb-speedbar-auto-raise (arg)
1062 "Toggle automatic raising of the speedbar for watch expressions.
1063 With prefix argument ARG, automatically raise speedbar if ARG is
1064 positive, otherwise don't automatically raise it."
1065 (interactive "P")
1066 (setq gdb-speedbar-auto-raise
1067 (if (null arg)
1068 (not gdb-speedbar-auto-raise)
1069 (> (prefix-numeric-value arg) 0)))
1070 (message (format "Auto raising %sabled"
1071 (if gdb-speedbar-auto-raise "en" "dis"))))
1073 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1074 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
1076 (declare-function tooltip-identifier-from-point "tooltip" (point))
1078 (defun gud-watch (&optional arg event)
1079 "Watch expression at point.
1080 With arg, enter name of variable to be watched in the minibuffer."
1081 (interactive (list current-prefix-arg last-input-event))
1082 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1083 (if (eq minor-mode 'gdbmi)
1084 (progn
1085 (if event (posn-set-point (event-end event)))
1086 (require 'tooltip)
1087 (save-selected-window
1088 (let ((expr
1089 (if arg
1090 (completing-read "Name of variable: "
1091 'gud-gdb-complete-command)
1092 (if (and transient-mark-mode mark-active)
1093 (buffer-substring (region-beginning) (region-end))
1094 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1095 (tooltip-identifier-from-point (point)))))))
1096 (set-text-properties 0 (length expr) nil expr)
1097 (gdb-input (concat "-var-create - * " expr "")
1098 `(lambda () (gdb-var-create-handler ,expr))))))
1099 (message "gud-watch is a no-op in this mode."))))
1101 (defun gdb-var-create-handler (expr)
1102 (let* ((result (gdb-json-partial-output)))
1103 (if (not (bindat-get-field result 'msg))
1104 (let ((var
1105 (list (bindat-get-field result 'name)
1106 (if (and (string-equal gdb-current-language "c")
1107 gdb-use-colon-colon-notation gdb-selected-frame)
1108 (setq expr (concat gdb-selected-frame "::" expr))
1109 expr)
1110 (bindat-get-field result 'numchild)
1111 (bindat-get-field result 'type)
1112 (bindat-get-field result 'value)
1114 (bindat-get-field result 'has_more)
1115 gdb-frame-address)))
1116 (push var gdb-var-list)
1117 (speedbar 1)
1118 (unless (string-equal
1119 speedbar-initial-expansion-list-name "GUD")
1120 (speedbar-change-initial-expansion-list "GUD")))
1121 (message-box "No symbol \"%s\" in current context." expr))))
1123 (defun gdb-speedbar-update ()
1124 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1125 (not (gdb-pending-p 'gdb-speedbar-timer)))
1126 ;; Dummy command to update speedbar even when idle.
1127 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn)
1128 ;; Keep gdb-pending-triggers non-nil till end.
1129 (gdb-add-pending 'gdb-speedbar-timer)))
1131 (defun gdb-speedbar-timer-fn ()
1132 (if gdb-speedbar-auto-raise
1133 (raise-frame speedbar-frame))
1134 (gdb-delete-pending 'gdb-speedbar-timer)
1135 (speedbar-timer-fn))
1137 (defun gdb-var-evaluate-expression-handler (varnum changed)
1138 (goto-char (point-min))
1139 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1140 (let ((var (assoc varnum gdb-var-list)))
1141 (when var
1142 (if changed (setcar (nthcdr 5 var) 'changed))
1143 (setcar (nthcdr 4 var) (read (match-string 1)))))
1144 (gdb-speedbar-update))
1146 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1147 (defun gdb-var-list-children (varnum)
1148 (gdb-input (concat "-var-update " varnum) 'ignore)
1149 (gdb-input (concat "-var-list-children --all-values " varnum)
1150 `(lambda () (gdb-var-list-children-handler ,varnum))))
1152 (defun gdb-var-list-children-handler (varnum)
1153 (let* ((var-list nil)
1154 (output (bindat-get-field (gdb-json-partial-output "child")))
1155 (children (bindat-get-field output 'children)))
1156 (catch 'child-already-watched
1157 (dolist (var gdb-var-list)
1158 (if (string-equal varnum (car var))
1159 (progn
1160 ;; With dynamic varobjs numchild may have increased.
1161 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1162 (push var var-list)
1163 (dolist (child children)
1164 (let ((varchild (list (bindat-get-field child 'name)
1165 (bindat-get-field child 'exp)
1166 (bindat-get-field child 'numchild)
1167 (bindat-get-field child 'type)
1168 (bindat-get-field child 'value)
1170 (bindat-get-field child 'has_more))))
1171 (if (assoc (car varchild) gdb-var-list)
1172 (throw 'child-already-watched nil))
1173 (push varchild var-list))))
1174 (push var var-list)))
1175 (setq gdb-var-list (nreverse var-list))))
1176 (gdb-speedbar-update))
1178 (defun gdb-var-set-format (format)
1179 "Set the output format for a variable displayed in the speedbar."
1180 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1181 (varnum (car var)))
1182 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1183 (gdb-var-update)))
1185 (defun gdb-var-delete-1 (var varnum)
1186 (gdb-input (concat "-var-delete " varnum) 'ignore)
1187 (setq gdb-var-list (delq var gdb-var-list))
1188 (dolist (varchild gdb-var-list)
1189 (if (string-match (concat (car var) "\\.") (car varchild))
1190 (setq gdb-var-list (delq varchild gdb-var-list)))))
1192 (defun gdb-var-delete ()
1193 "Delete watch expression at point from the speedbar."
1194 (interactive)
1195 (let ((text (speedbar-line-text)))
1196 (string-match "\\(\\S-+\\)" text)
1197 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1198 (varnum (car var)))
1199 (if (string-match "\\." (car var))
1200 (message-box "Can only delete a root expression")
1201 (gdb-var-delete-1 var varnum)))))
1203 (defun gdb-var-delete-children (varnum)
1204 "Delete children of variable object at point from the speedbar."
1205 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1207 (defun gdb-edit-value (_text _token _indent)
1208 "Assign a value to a variable displayed in the speedbar."
1209 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1210 (varnum (car var)) (value))
1211 (setq value (read-string "New value: "))
1212 (gdb-input (concat "-var-assign " varnum " " value)
1213 `(lambda () (gdb-edit-value-handler ,value)))))
1215 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1217 (defun gdb-edit-value-handler (value)
1218 (goto-char (point-min))
1219 (if (re-search-forward gdb-error-regexp nil t)
1220 (message-box "Invalid number or expression (%s)" value)))
1222 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1223 (defun gdb-var-update ()
1224 (if (not (gdb-pending-p 'gdb-var-update))
1225 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler))
1226 (gdb-add-pending 'gdb-var-update))
1228 (defun gdb-var-update-handler ()
1229 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1230 (dolist (var gdb-var-list)
1231 (setcar (nthcdr 5 var) nil))
1232 (let ((temp-var-list gdb-var-list))
1233 (dolist (change changelist)
1234 (let* ((varnum (bindat-get-field change 'name))
1235 (var (assoc varnum gdb-var-list))
1236 (new-num (bindat-get-field change 'new_num_children)))
1237 (when var
1238 (let ((scope (bindat-get-field change 'in_scope))
1239 (has-more (bindat-get-field change 'has_more)))
1240 (cond ((string-equal scope "false")
1241 (if gdb-delete-out-of-scope
1242 (gdb-var-delete-1 var varnum)
1243 (setcar (nthcdr 5 var) 'out-of-scope)))
1244 ((string-equal scope "true")
1245 (setcar (nthcdr 6 var) has-more)
1246 (when (and (or (not has-more)
1247 (string-equal has-more "0"))
1248 (not new-num)
1249 (string-equal (nth 2 var) "0"))
1250 (setcar (nthcdr 4 var)
1251 (bindat-get-field change 'value))
1252 (setcar (nthcdr 5 var) 'changed)))
1253 ((string-equal scope "invalid")
1254 (gdb-var-delete-1 var varnum)))))
1255 (let ((var-list nil) var1
1256 (children (bindat-get-field change 'new_children)))
1257 (when new-num
1258 (setq var1 (pop temp-var-list))
1259 (while var1
1260 (if (string-equal varnum (car var1))
1261 (let ((new (string-to-number new-num))
1262 (previous (string-to-number (nth 2 var1))))
1263 (setcar (nthcdr 2 var1) new-num)
1264 (push var1 var-list)
1265 (cond
1266 ((> new previous)
1267 ;; Add new children to list.
1268 (dotimes (dummy previous)
1269 (push (pop temp-var-list) var-list))
1270 (dolist (child children)
1271 (let ((varchild
1272 (list (bindat-get-field child 'name)
1273 (bindat-get-field child 'exp)
1274 (bindat-get-field child 'numchild)
1275 (bindat-get-field child 'type)
1276 (bindat-get-field child 'value)
1277 'changed
1278 (bindat-get-field child 'has_more))))
1279 (push varchild var-list))))
1280 ;; Remove deleted children from list.
1281 ((< new previous)
1282 (dotimes (dummy new)
1283 (push (pop temp-var-list) var-list))
1284 (dotimes (dummy (- previous new))
1285 (pop temp-var-list)))))
1286 (push var1 var-list))
1287 (setq var1 (pop temp-var-list)))
1288 (setq gdb-var-list (nreverse var-list))))))))
1289 (setq gdb-pending-triggers
1290 (delq 'gdb-var-update gdb-pending-triggers))
1291 (gdb-speedbar-update))
1293 (defun gdb-speedbar-expand-node (text token indent)
1294 "Expand the node the user clicked on.
1295 TEXT is the text of the button we clicked on, a + or - item.
1296 TOKEN is data related to this node.
1297 INDENT is the current indentation depth."
1298 (cond ((string-match "+" text) ;expand this node
1299 (let* ((var (assoc token gdb-var-list))
1300 (expr (nth 1 var)) (children (nth 2 var)))
1301 (if (or (<= (string-to-number children) gdb-max-children)
1302 (y-or-n-p
1303 (format "%s has %s children. Continue? " expr children)))
1304 (gdb-var-list-children token))))
1305 ((string-match "-" text) ;contract this node
1306 (dolist (var gdb-var-list)
1307 (if (string-match (concat token "\\.") (car var))
1308 (setq gdb-var-list (delq var gdb-var-list))))
1309 (gdb-var-delete-children token)
1310 (speedbar-change-expand-button-char ?+)
1311 (speedbar-delete-subblock indent))
1312 (t (error "Ooops... not sure what to do")))
1313 (speedbar-center-buffer-smartly))
1315 (defun gdb-get-target-string ()
1316 (with-current-buffer gud-comint-buffer
1317 gud-target-name))
1321 ;; gdb buffers.
1323 ;; Each buffer has a TYPE -- a symbol that identifies the function
1324 ;; of that particular buffer.
1326 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1327 ;; is constructed specially.
1329 ;; Others are constructed by gdb-get-buffer-create and
1330 ;; named according to the rules set forth in the gdb-buffer-rules
1332 (defvar gdb-buffer-rules '())
1334 (defun gdb-rules-name-maker (rules-entry)
1335 (cadr rules-entry))
1336 (defun gdb-rules-buffer-mode (rules-entry)
1337 (nth 2 rules-entry))
1338 (defun gdb-rules-update-trigger (rules-entry)
1339 (nth 3 rules-entry))
1341 (defun gdb-update-buffer-name ()
1342 "Rename current buffer according to name-maker associated with
1343 it in `gdb-buffer-rules'."
1344 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1345 gdb-buffer-rules))))
1346 (when f (rename-buffer (funcall f)))))
1348 (defun gdb-current-buffer-rules ()
1349 "Get `gdb-buffer-rules' entry for current buffer type."
1350 (assoc gdb-buffer-type gdb-buffer-rules))
1352 (defun gdb-current-buffer-thread ()
1353 "Get thread object of current buffer from `gdb-threads-list'.
1355 When current buffer is not bound to any thread, return main
1356 thread."
1357 (cdr (assoc gdb-thread-number gdb-threads-list)))
1359 (defun gdb-current-buffer-frame ()
1360 "Get current stack frame object for thread of current buffer."
1361 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1363 (defun gdb-buffer-type (buffer)
1364 "Get value of `gdb-buffer-type' for BUFFER."
1365 (with-current-buffer buffer
1366 gdb-buffer-type))
1368 (defun gdb-buffer-shows-main-thread-p ()
1369 "Return t if current GDB buffer shows main selected thread and
1370 is not bound to it."
1371 (current-buffer)
1372 (not (local-variable-p 'gdb-thread-number)))
1374 (defun gdb-get-buffer (buffer-type &optional thread)
1375 "Get a specific GDB buffer.
1377 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1378 and `gdb-thread-number' (if provided) must be equal to THREAD."
1379 (catch 'found
1380 (dolist (buffer (buffer-list) nil)
1381 (with-current-buffer buffer
1382 (when (and (eq gdb-buffer-type buffer-type)
1383 (or (not thread)
1384 (equal gdb-thread-number thread)))
1385 (throw 'found buffer))))))
1387 (defun gdb-get-buffer-create (buffer-type &optional thread)
1388 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1389 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1391 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1392 buffer-local variable of the new buffer.
1394 Buffer mode and name are selected according to buffer type.
1396 If buffer has trigger associated with it in `gdb-buffer-rules',
1397 this trigger is subscribed to `gdb-buf-publisher' and called with
1398 'update argument."
1399 (or (gdb-get-buffer buffer-type thread)
1400 (let ((rules (assoc buffer-type gdb-buffer-rules))
1401 (new (generate-new-buffer "limbo")))
1402 (with-current-buffer new
1403 (let ((mode (gdb-rules-buffer-mode rules))
1404 (trigger (gdb-rules-update-trigger rules)))
1405 (when mode (funcall mode))
1406 (setq gdb-buffer-type buffer-type)
1407 (when thread
1408 (set (make-local-variable 'gdb-thread-number) thread))
1409 (set (make-local-variable 'gud-minor-mode)
1410 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1411 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1412 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1413 (when trigger
1414 (gdb-add-subscriber gdb-buf-publisher
1415 (cons (current-buffer)
1416 (gdb-bind-function-to-buffer
1417 trigger (current-buffer))))
1418 (funcall trigger 'start))
1419 (current-buffer))))))
1421 (defun gdb-bind-function-to-buffer (expr buffer)
1422 "Return a function which will evaluate EXPR in BUFFER."
1423 `(lambda (&rest args)
1424 (with-current-buffer ,buffer
1425 (apply ',expr args))))
1427 ;; Used to define all gdb-frame-*-buffer functions except
1428 ;; `gdb-frame-io-buffer'
1429 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1430 "Define a function NAME which shows gdb BUFFER in a separate frame.
1432 DOC is an optional documentation string."
1433 `(defun ,name (&optional thread)
1434 ,(when doc doc)
1435 (interactive)
1436 (let ((special-display-regexps (append special-display-regexps '(".*")))
1437 (special-display-frame-alist gdb-frame-parameters))
1438 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1440 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1441 "Define a function NAME which shows gdb BUFFER.
1443 DOC is an optional documentation string."
1444 `(defun ,name (&optional thread)
1445 ,(when doc doc)
1446 (interactive)
1447 (gdb-display-buffer
1448 (gdb-get-buffer-create ,buffer thread) t)))
1450 ;; Used to display windows with thread-bound buffers
1451 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1452 split-horizontal)
1453 `(defun ,name (&optional thread)
1454 ,(when doc doc)
1455 (message thread)
1456 (gdb-preempt-existing-or-display-buffer
1457 (gdb-get-buffer-create ,buffer thread)
1458 ,split-horizontal)))
1460 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1461 ;; at least one and possible more functions. The functions have these
1462 ;; roles in defining a buffer type:
1464 ;; NAME - Return a name for this buffer type.
1466 ;; The remaining function(s) are optional:
1468 ;; MODE - called in a new buffer with no arguments, should establish
1469 ;; the proper mode for the buffer.
1472 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1473 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1474 (if binding
1475 (setcdr binding rules)
1476 (push (cons buffer-type rules)
1477 gdb-buffer-rules))))
1479 (defun gdb-parent-mode ()
1480 "Generic mode to derive all other GDB buffer modes from."
1481 (kill-all-local-variables)
1482 (setq buffer-read-only t)
1483 (buffer-disable-undo)
1484 ;; Delete buffer from gdb-buf-publisher when it's killed
1485 ;; (if it has an associated update trigger)
1486 (add-hook
1487 'kill-buffer-hook
1488 (function
1489 (lambda ()
1490 (let ((trigger (gdb-rules-update-trigger
1491 (gdb-current-buffer-rules))))
1492 (when trigger
1493 (gdb-delete-subscriber
1494 gdb-buf-publisher
1495 ;; This should match gdb-add-subscriber done in
1496 ;; gdb-get-buffer-create
1497 (cons (current-buffer)
1498 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1499 nil t))
1501 ;; Partial-output buffer : This accumulates output from a command executed on
1502 ;; behalf of emacs (rather than the user).
1504 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1505 'gdb-partial-output-name)
1507 (defun gdb-partial-output-name ()
1508 (concat " *partial-output-"
1509 (gdb-get-target-string)
1510 "*"))
1513 (gdb-set-buffer-rules 'gdb-inferior-io
1514 'gdb-inferior-io-name
1515 'gdb-inferior-io-mode)
1517 (defun gdb-inferior-io-name ()
1518 (concat "*input/output of "
1519 (gdb-get-target-string)
1520 "*"))
1522 (defun gdb-display-io-buffer ()
1523 "Display IO of debugged program in a separate window."
1524 (interactive)
1525 (gdb-display-buffer
1526 (gdb-get-buffer-create 'gdb-inferior-io) t))
1528 (defun gdb-inferior-io--init-proc (proc)
1529 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1530 (set-process-filter proc 'gdb-inferior-filter)
1531 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1532 (gdb-input
1533 (concat "-inferior-tty-set "
1534 ;; The process can run on a remote host.
1535 (or (process-get proc 'remote-tty)
1536 (process-tty-name proc)))
1537 'ignore))
1539 (defun gdb-inferior-io-sentinel (proc str)
1540 (when (eq (process-status proc) 'failed)
1541 ;; When the debugged process exits, Emacs gets an EIO error on
1542 ;; read from the pty, and stops listening to it. If the gdb
1543 ;; process is still running, remove the pty, make a new one, and
1544 ;; pass it to gdb.
1545 (let ((gdb-proc (get-buffer-process gud-comint-buffer))
1546 (io-buffer (process-buffer proc)))
1547 (when (and gdb-proc (process-live-p gdb-proc)
1548 (buffer-live-p io-buffer))
1549 ;; `comint-exec' deletes the original process as a side effect.
1550 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1551 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1553 (defconst gdb-frame-parameters
1554 '((height . 14) (width . 80)
1555 (unsplittable . t)
1556 (tool-bar-lines . nil)
1557 (menu-bar-lines . nil)
1558 (minibuffer . nil)))
1560 (defun gdb-frame-io-buffer ()
1561 "Display IO of debugged program in a new frame."
1562 (interactive)
1563 (let ((special-display-regexps (append special-display-regexps '(".*")))
1564 (special-display-frame-alist gdb-frame-parameters))
1565 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1567 (defvar gdb-inferior-io-mode-map
1568 (let ((map (make-sparse-keymap)))
1569 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1570 (define-key map "\C-c\C-z" 'gdb-io-stop)
1571 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1572 (define-key map "\C-c\C-d" 'gdb-io-eof)
1573 (define-key map "\C-d" 'gdb-io-eof)
1574 map))
1576 ;; We want to use comint because it has various nifty and familiar features.
1577 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1578 "Major mode for gdb inferior-io."
1579 :syntax-table nil :abbrev-table nil
1580 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1582 (defun gdb-inferior-filter (proc string)
1583 (unless (string-equal string "")
1584 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1585 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1586 (comint-output-filter proc string)))
1588 (defun gdb-io-interrupt ()
1589 "Interrupt the program being debugged."
1590 (interactive)
1591 (interrupt-process
1592 (get-buffer-process gud-comint-buffer) comint-ptyp))
1594 (defun gdb-io-quit ()
1595 "Send quit signal to the program being debugged."
1596 (interactive)
1597 (quit-process
1598 (get-buffer-process gud-comint-buffer) comint-ptyp))
1600 (defun gdb-io-stop ()
1601 "Stop the program being debugged."
1602 (interactive)
1603 (stop-process
1604 (get-buffer-process gud-comint-buffer) comint-ptyp))
1606 (defun gdb-io-eof ()
1607 "Send end-of-file to the program being debugged."
1608 (interactive)
1609 (process-send-eof
1610 (get-buffer-process gud-comint-buffer)))
1612 (defun gdb-clear-inferior-io ()
1613 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1614 (erase-buffer)))
1617 (defconst breakpoint-xpm-data
1618 "/* XPM */
1619 static char *magick[] = {
1620 /* columns rows colors chars-per-pixel */
1621 \"10 10 2 1\",
1622 \" c red\",
1623 \"+ c None\",
1624 /* pixels */
1625 \"+++ +++\",
1626 \"++ ++\",
1627 \"+ +\",
1628 \" \",
1629 \" \",
1630 \" \",
1631 \" \",
1632 \"+ +\",
1633 \"++ ++\",
1634 \"+++ +++\",
1636 "XPM data used for breakpoint icon.")
1638 (defconst breakpoint-enabled-pbm-data
1640 10 10\",
1641 0 0 0 0 1 1 1 1 0 0 0 0
1642 0 0 0 1 1 1 1 1 1 0 0 0
1643 0 0 1 1 1 1 1 1 1 1 0 0
1644 0 1 1 1 1 1 1 1 1 1 1 0
1645 0 1 1 1 1 1 1 1 1 1 1 0
1646 0 1 1 1 1 1 1 1 1 1 1 0
1647 0 1 1 1 1 1 1 1 1 1 1 0
1648 0 0 1 1 1 1 1 1 1 1 0 0
1649 0 0 0 1 1 1 1 1 1 0 0 0
1650 0 0 0 0 1 1 1 1 0 0 0 0"
1651 "PBM data used for enabled breakpoint icon.")
1653 (defconst breakpoint-disabled-pbm-data
1655 10 10\",
1656 0 0 1 0 1 0 1 0 0 0
1657 0 1 0 1 0 1 0 1 0 0
1658 1 0 1 0 1 0 1 0 1 0
1659 0 1 0 1 0 1 0 1 0 1
1660 1 0 1 0 1 0 1 0 1 0
1661 0 1 0 1 0 1 0 1 0 1
1662 1 0 1 0 1 0 1 0 1 0
1663 0 1 0 1 0 1 0 1 0 1
1664 0 0 1 0 1 0 1 0 1 0
1665 0 0 0 1 0 1 0 1 0 0"
1666 "PBM data used for disabled breakpoint icon.")
1668 (defvar breakpoint-enabled-icon nil
1669 "Icon for enabled breakpoint in display margin.")
1671 (defvar breakpoint-disabled-icon nil
1672 "Icon for disabled breakpoint in display margin.")
1674 (declare-function define-fringe-bitmap "fringe.c"
1675 (bitmap bits &optional height width align))
1677 (and (display-images-p)
1678 ;; Bitmap for breakpoint in fringe
1679 (define-fringe-bitmap 'breakpoint
1680 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1681 ;; Bitmap for gud-overlay-arrow in fringe
1682 (define-fringe-bitmap 'hollow-right-triangle
1683 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1685 (defface breakpoint-enabled
1686 '((t
1687 :foreground "red1"
1688 :weight bold))
1689 "Face for enabled breakpoint icon in fringe."
1690 :group 'gdb)
1692 (defface breakpoint-disabled
1693 '((((class color) (min-colors 88)) :foreground "grey70")
1694 ;; Ensure that on low-color displays that we end up something visible.
1695 (((class color) (min-colors 8) (background light))
1696 :foreground "black")
1697 (((class color) (min-colors 8) (background dark))
1698 :foreground "white")
1699 (((type tty) (class mono))
1700 :inverse-video t)
1701 (t :background "gray"))
1702 "Face for disabled breakpoint icon in fringe."
1703 :group 'gdb)
1706 (defvar gdb-control-commands-regexp
1707 (concat
1708 "^\\("
1709 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1710 "while-stepping\\|stepping\\|ws\\|actions"
1711 "\\)\\([[:blank:]]+.*\\)?$")
1712 "Regexp matching GDB commands that enter a recursive reading loop.
1713 As long as GDB is in the recursive reading loop, it does not expect
1714 commands to be prefixed by \"-interpreter-exec console\".")
1716 (defun gdb-send (proc string)
1717 "A comint send filter for gdb."
1718 (with-current-buffer gud-comint-buffer
1719 (let ((inhibit-read-only t))
1720 (remove-text-properties (point-min) (point-max) '(face))))
1721 ;; mimic <RET> key to repeat previous command in GDB
1722 (if (not (string= "" string))
1723 (setq gdb-last-command string)
1724 (if gdb-last-command (setq string gdb-last-command)))
1725 (if (or (string-match "^-" string)
1726 (> gdb-control-level 0))
1727 ;; Either MI command or we are feeding GDB's recursive reading loop.
1728 (progn
1729 (setq gdb-first-done-or-error t)
1730 (process-send-string proc (concat string "\n"))
1731 (if (and (string-match "^end$" string)
1732 (> gdb-control-level 0))
1733 (setq gdb-control-level (1- gdb-control-level))))
1734 ;; CLI command
1735 (if (string-match "\\\\$" string)
1736 (setq gdb-continuation (concat gdb-continuation string "\n"))
1737 (setq gdb-first-done-or-error t)
1738 (let ((to-send (concat "-interpreter-exec console "
1739 (gdb-mi-quote string)
1740 "\n")))
1741 (if gdb-enable-debug
1742 (push (cons 'mi-send to-send) gdb-debug-log))
1743 (process-send-string proc to-send))
1744 (if (and (string-match "^end$" string)
1745 (> gdb-control-level 0))
1746 (setq gdb-control-level (1- gdb-control-level)))
1747 (setq gdb-continuation nil)))
1748 (if (string-match gdb-control-commands-regexp string)
1749 (setq gdb-control-level (1+ gdb-control-level))))
1751 (defun gdb-mi-quote (string)
1752 "Return STRING quoted properly as an MI argument.
1753 The string is enclosed in double quotes.
1754 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1755 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1756 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1757 (concat "\"" string "\""))
1759 (defun gdb-input (command handler-function)
1760 "Send COMMAND to GDB via the MI interface.
1761 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1762 complete."
1763 (if gdb-enable-debug (push (list 'send-item command handler-function)
1764 gdb-debug-log))
1765 (setq gdb-token-number (1+ gdb-token-number))
1766 (setq command (concat (number-to-string gdb-token-number) command))
1767 (push (cons gdb-token-number handler-function) gdb-handler-alist)
1768 (process-send-string (get-buffer-process gud-comint-buffer)
1769 (concat command "\n")))
1771 ;; NOFRAME is used for gud execution control commands
1772 (defun gdb-current-context-command (command)
1773 "Add --thread to gdb COMMAND when needed."
1774 (if (and gdb-thread-number
1775 gdb-supports-non-stop)
1776 (concat command " --thread " gdb-thread-number)
1777 command))
1779 (defun gdb-current-context-buffer-name (name)
1780 "Add thread information and asterisks to string NAME.
1782 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1783 (concat "*" name
1784 (if (local-variable-p 'gdb-thread-number)
1785 (format " (bound to thread %s)" gdb-thread-number)
1787 "*"))
1789 (defun gdb-current-context-mode-name (mode)
1790 "Add thread information to MODE which is to be used as
1791 `mode-name'."
1792 (concat mode
1793 (if gdb-thread-number
1794 (format " [thread %s]" gdb-thread-number)
1795 "")))
1798 (defcustom gud-gdb-command-name "gdb -i=mi"
1799 "Default command to execute an executable under the GDB debugger."
1800 :type 'string
1801 :group 'gdb)
1803 (defun gdb-resync()
1804 (setq gud-running nil)
1805 (setq gdb-output-sink 'user)
1806 (setq gdb-pending-triggers nil))
1808 (defun gdb-update (&optional no-proc)
1809 "Update buffers showing status of debug session.
1810 If NO-PROC is non-nil, do not try to contact the GDB process."
1811 (when gdb-first-prompt
1812 (gdb-force-mode-line-update
1813 (propertize "initializing..." 'face font-lock-variable-name-face))
1814 (gdb-init-1)
1815 (setq gdb-first-prompt nil))
1817 (unless no-proc
1818 (gdb-get-main-selected-frame))
1820 ;; We may need to update gdb-threads-list so we can use
1821 (gdb-get-buffer-create 'gdb-threads-buffer)
1822 ;; gdb-break-list is maintained in breakpoints handler
1823 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1825 (unless no-proc
1826 (gdb-emit-signal gdb-buf-publisher 'update))
1828 (gdb-get-changed-registers)
1829 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1830 (dolist (var gdb-var-list)
1831 (setcar (nthcdr 5 var) nil))
1832 (gdb-var-update)))
1834 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1835 ;; because we may need to update current gud-running value without
1836 ;; changing current thread (see gdb-running)
1837 (defun gdb-setq-thread-number (number)
1838 "Only this function must be used to change `gdb-thread-number'
1839 value to NUMBER, because `gud-running' and `gdb-frame-number'
1840 need to be updated appropriately when current thread changes."
1841 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1842 (unless (string-equal number "0") (setq gdb-thread-number number))
1843 (setq gdb-frame-number "0")
1844 (gdb-update-gud-running))
1846 (defun gdb-update-gud-running ()
1847 "Set `gud-running' according to the state of current thread.
1849 `gdb-frame-number' is set to 0 if current thread is now stopped.
1851 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1852 cannot be reliably used to determine whether or not execution
1853 control buttons should be shown in menu or toolbar. Use
1854 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1855 instead.
1857 For all-stop mode, thread information is unavailable while target
1858 is running."
1859 (let ((old-value gud-running))
1860 (setq gud-running
1861 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1862 "running"))
1863 ;; Set frame number to "0" when _current_ threads stops
1864 (when (and (gdb-current-buffer-thread)
1865 (not (eq gud-running old-value)))
1866 (setq gdb-frame-number "0"))))
1868 (defun gdb-show-run-p ()
1869 "Return t if \"Run/continue\" should be shown on the toolbar."
1870 (or (not gdb-active-process)
1871 (and (or
1872 (not gdb-gud-control-all-threads)
1873 (not gdb-non-stop))
1874 (not gud-running))
1875 (and gdb-gud-control-all-threads
1876 (> gdb-stopped-threads-count 0))))
1878 (defun gdb-show-stop-p ()
1879 "Return t if \"Stop\" should be shown on the toolbar."
1880 (or (and (or
1881 (not gdb-gud-control-all-threads)
1882 (not gdb-non-stop))
1883 gud-running)
1884 (and gdb-gud-control-all-threads
1885 (> gdb-running-threads-count 0))))
1887 ;; GUD displays the selected GDB frame. This might might not be the current
1888 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1889 ;; visited breakpoint is, use that window.
1890 (defun gdb-display-source-buffer (buffer)
1891 (let* ((last-window (if gud-last-last-frame
1892 (get-buffer-window
1893 (gud-find-file (car gud-last-last-frame)))))
1894 (source-window (or last-window
1895 (if (and gdb-source-window
1896 (window-live-p gdb-source-window))
1897 gdb-source-window))))
1898 (when source-window
1899 (setq gdb-source-window source-window)
1900 (set-window-buffer source-window buffer))
1901 source-window))
1903 (defun gdb-car< (a b)
1904 (< (car a) (car b)))
1906 (defvar gdbmi-record-list
1907 '((gdb-gdb . "(gdb) \n")
1908 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1909 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1910 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1911 (gdb-console . "~\\(\".*?\"\\)\n")
1912 (gdb-internals . "&\\(\".*?\"\\)\n")
1913 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1914 (gdb-running . "\\*running,\\(.*?\n\\)")
1915 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1916 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1917 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1918 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1919 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1921 (defun gud-gdbmi-marker-filter (string)
1922 "Filter GDB/MI output."
1924 ;; Record transactions if logging is enabled.
1925 (when gdb-enable-debug
1926 (push (cons 'recv string) gdb-debug-log)
1927 (if (and gdb-debug-log-max
1928 (> (length gdb-debug-log) gdb-debug-log-max))
1929 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1931 ;; Recall the left over gud-marker-acc from last time
1932 (setq gud-marker-acc (concat gud-marker-acc string))
1934 ;; Start accumulating output for the GUD buffer
1935 (setq gdb-filter-output "")
1936 (let (output-record-list)
1938 ;; Process all the complete markers in this chunk.
1939 (dolist (gdbmi-record gdbmi-record-list)
1940 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1941 (push (list (match-beginning 0)
1942 (car gdbmi-record)
1943 (match-string 1 gud-marker-acc)
1944 (match-string 2 gud-marker-acc)
1945 (match-end 0))
1946 output-record-list)
1947 (setq gud-marker-acc
1948 (concat (substring gud-marker-acc 0 (match-beginning 0))
1949 ;; Pad with spaces to preserve position.
1950 (make-string (length (match-string 0 gud-marker-acc)) 32)
1951 (substring gud-marker-acc (match-end 0))))))
1953 (setq output-record-list (sort output-record-list 'gdb-car<))
1955 (dolist (output-record output-record-list)
1956 (let ((record-type (cadr output-record))
1957 (arg1 (nth 2 output-record))
1958 (arg2 (nth 3 output-record)))
1959 (cond ((eq record-type 'gdb-error)
1960 (gdb-done-or-error arg2 arg1 'error))
1961 ((eq record-type 'gdb-done)
1962 (gdb-done-or-error arg2 arg1 'done))
1963 ;; Suppress "No registers." GDB 6.8 and earlier
1964 ;; duplicates MI error message on internal stream.
1965 ;; Don't print to GUD buffer.
1966 ((not (and (eq record-type 'gdb-internals)
1967 (string-equal (read arg1) "No registers.\n")))
1968 (funcall record-type arg1)))))
1970 (setq gdb-output-sink 'user)
1971 ;; Remove padding.
1972 (string-match "^ *" gud-marker-acc)
1973 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1975 gdb-filter-output))
1977 (defun gdb-gdb (_output-field))
1979 (defun gdb-shell (output-field)
1980 (let ((gdb-output-sink gdb-output-sink))
1981 (setq gdb-filter-output
1982 (concat output-field gdb-filter-output))))
1984 (defun gdb-ignored-notification (_output-field))
1986 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1987 (defun gdb-thread-created (_output-field))
1988 (defun gdb-thread-exited (output-field)
1989 "Handle =thread-exited async record: unset `gdb-thread-number'
1990 if current thread exited and update threads list."
1991 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1992 (if (string= gdb-thread-number thread-id)
1993 (gdb-setq-thread-number nil))
1994 ;; When we continue current thread and it quickly exits,
1995 ;; gdb-pending-triggers left after gdb-running disallow us to
1996 ;; properly call -thread-info without --thread option. Thus we
1997 ;; need to use gdb-wait-for-pending.
1998 (gdb-wait-for-pending
1999 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
2001 (defun gdb-thread-selected (output-field)
2002 "Handler for =thread-selected MI output record.
2004 Sets `gdb-thread-number' to new id."
2005 (let* ((result (gdb-json-string output-field))
2006 (thread-id (bindat-get-field result 'id)))
2007 (gdb-setq-thread-number thread-id)
2008 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
2009 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
2010 ;; as usually. Things happen to fast and second call (from
2011 ;; gdb-thread-selected handler) gets cut off by our beloved
2012 ;; gdb-pending-triggers.
2013 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
2014 ;; body will get executed when `gdb-pending-triggers` is empty.
2015 (gdb-wait-for-pending
2016 (gdb-update))))
2018 (defun gdb-running (output-field)
2019 (let* ((thread-id
2020 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
2021 ;; We reset gdb-frame-number to nil if current thread has gone
2022 ;; running. This can't be done in gdb-thread-list-handler-custom
2023 ;; because we need correct gdb-frame-number by the time
2024 ;; -thread-info command is sent.
2025 (when (or (string-equal thread-id "all")
2026 (string-equal thread-id gdb-thread-number))
2027 (setq gdb-frame-number nil)))
2028 (setq gdb-inferior-status "running")
2029 (gdb-force-mode-line-update
2030 (propertize gdb-inferior-status 'face font-lock-type-face))
2031 (when (not gdb-non-stop)
2032 (setq gud-running t))
2033 (setq gdb-active-process t)
2034 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2036 (defun gdb-starting (_output-field)
2037 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2038 (setq gdb-inferior-status "running")
2039 (gdb-force-mode-line-update
2040 (propertize gdb-inferior-status 'face font-lock-type-face))
2041 (setq gdb-active-process t)
2042 (setq gud-running t)
2043 ;; GDB doesn't seem to respond to -thread-info before first stop or
2044 ;; thread exit (even in non-stop mode), so this is useless.
2045 ;; Behavior may change in the future.
2046 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2048 ;; -break-insert -t didn't give a reason before gdb 6.9
2050 (defun gdb-stopped (output-field)
2051 "Given the contents of *stopped MI async record, select new
2052 current thread and update GDB buffers."
2053 ;; Reason is available with target-async only
2054 (let* ((result (gdb-json-string output-field))
2055 (reason (bindat-get-field result 'reason))
2056 (thread-id (bindat-get-field result 'thread-id)))
2058 ;; -data-list-register-names needs to be issued for any stopped
2059 ;; thread
2060 (when (not gdb-register-names)
2061 (gdb-input (concat "-data-list-register-names"
2062 (if gdb-supports-non-stop
2063 (concat " --thread " thread-id)))
2064 'gdb-register-names-handler))
2066 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
2067 ;;; because synchronous GDB doesn't give these fields with CLI.
2068 ;;; (when file
2069 ;;; (setq
2070 ;;; ;; Extract the frame position from the marker.
2071 ;;; gud-last-frame (cons file
2072 ;;; (string-to-number
2073 ;;; (match-string 6 gud-marker-acc)))))
2075 (setq gdb-inferior-status (or reason "unknown"))
2076 (gdb-force-mode-line-update
2077 (propertize gdb-inferior-status 'face font-lock-warning-face))
2078 (if (string-equal reason "exited-normally")
2079 (setq gdb-active-process nil))
2081 ;; Select new current thread.
2083 ;; Don't switch if we have no reasons selected
2084 (when gdb-switch-reasons
2085 ;; Switch from another stopped thread only if we have
2086 ;; gdb-switch-when-another-stopped:
2087 (when (or gdb-switch-when-another-stopped
2088 (not (string= "stopped"
2089 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2090 ;; Switch if current reason has been selected or we have no
2091 ;; reasons
2092 (if (or (eq gdb-switch-reasons t)
2093 (member reason gdb-switch-reasons))
2094 (when (not (string-equal gdb-thread-number thread-id))
2095 (message (concat "Switched to thread " thread-id))
2096 (gdb-setq-thread-number thread-id))
2097 (message (format "Thread %s stopped" thread-id)))))
2099 ;; Print "(gdb)" to GUD console
2100 (when gdb-first-done-or-error
2101 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2103 ;; In non-stop, we update information as soon as another thread gets
2104 ;; stopped
2105 (when (or gdb-first-done-or-error
2106 gdb-non-stop)
2107 ;; In all-stop this updates gud-running properly as well.
2108 (gdb-update)
2109 (setq gdb-first-done-or-error nil))
2110 (run-hook-with-args 'gdb-stopped-functions result)))
2112 ;; Remove the trimmings from log stream containing debugging messages
2113 ;; being produced by GDB's internals, use warning face and send to GUD
2114 ;; buffer.
2115 (defun gdb-internals (output-field)
2116 (setq gdb-filter-output
2117 (gdb-concat-output
2118 gdb-filter-output
2119 (let ((error-message
2120 (read output-field)))
2121 (put-text-property
2122 0 (length error-message)
2123 'face font-lock-warning-face
2124 error-message)
2125 error-message))))
2127 ;; Remove the trimmings from the console stream and send to GUD buffer
2128 ;; (frontend MI commands should not print to this stream)
2129 (defun gdb-console (output-field)
2130 (setq gdb-filter-output
2131 (gdb-concat-output gdb-filter-output (read output-field))))
2133 (defun gdb-done-or-error (output-field token-number type)
2134 (if (string-equal token-number "")
2135 ;; Output from command entered by user
2136 (progn
2137 (setq gdb-output-sink 'user)
2138 (setq token-number nil)
2139 ;; MI error - send to minibuffer
2140 (when (eq type 'error)
2141 ;; Skip "msg=" from `output-field'
2142 (message (read (substring output-field 4)))
2143 ;; Don't send to the console twice. (If it is a console error
2144 ;; it is also in the console stream.)
2145 (setq output-field nil)))
2146 ;; Output from command from frontend.
2147 (setq gdb-output-sink 'emacs))
2149 (gdb-clear-partial-output)
2151 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2152 (let* ((proc (get-buffer-process gud-comint-buffer))
2153 (no-proc (or (null proc)
2154 (memq (process-status proc) '(exit signal)))))
2156 (when gdb-first-done-or-error
2157 (unless (or token-number gud-running no-proc)
2158 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2159 (gdb-update no-proc)
2160 (setq gdb-first-done-or-error nil))
2162 (setq gdb-filter-output
2163 (gdb-concat-output gdb-filter-output output-field))
2165 (when token-number
2166 (with-current-buffer
2167 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2168 (funcall
2169 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2170 (setq gdb-handler-alist
2171 (assq-delete-all token-number gdb-handler-alist)))))
2173 (defun gdb-concat-output (so-far new)
2174 (cond
2175 ((eq gdb-output-sink 'user) (concat so-far new))
2176 ((eq gdb-output-sink 'emacs)
2177 (gdb-append-to-partial-output new)
2178 so-far)))
2180 (defun gdb-append-to-partial-output (string)
2181 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2182 (goto-char (point-max))
2183 (insert string)))
2185 (defun gdb-clear-partial-output ()
2186 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2187 (erase-buffer)))
2189 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2190 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2192 Field names are wrapped in double quotes and equal signs are
2193 replaced with semicolons.
2195 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2196 partial output. This is used to get rid of useless keys in lists
2197 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2198 -break-info are examples of MI commands which issue such
2199 responses.
2201 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2202 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2203 -break-info output when it contains breakpoint script field
2204 incompatible with GDB/MI output syntax."
2205 (save-excursion
2206 (goto-char (point-min))
2207 (when fix-key
2208 (save-excursion
2209 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2210 (replace-match "" nil nil nil 1))))
2211 (when fix-list
2212 (save-excursion
2213 ;; Find positions of braces which enclose broken list
2214 (while (re-search-forward (concat fix-list "={\"") nil t)
2215 (let ((p1 (goto-char (- (point) 2)))
2216 (p2 (progn (forward-sexp)
2217 (1- (point)))))
2218 ;; Replace braces with brackets
2219 (save-excursion
2220 (goto-char p1)
2221 (delete-char 1)
2222 (insert "[")
2223 (goto-char p2)
2224 (delete-char 1)
2225 (insert "]"))))))
2226 (goto-char (point-min))
2227 (insert "{")
2228 (while (re-search-forward
2229 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2230 (replace-match "\"\\1\":\\2" nil nil))
2231 (goto-char (point-max))
2232 (insert "}")))
2234 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2235 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2237 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2238 (gdb-jsonify-buffer fix-key fix-list)
2239 (save-excursion
2240 (goto-char (point-min))
2241 (let ((json-array-type 'list))
2242 (json-read))))
2244 (defun gdb-json-string (string &optional fix-key fix-list)
2245 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2247 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2248 (with-temp-buffer
2249 (insert string)
2250 (gdb-json-read-buffer fix-key fix-list)))
2252 (defun gdb-json-partial-output (&optional fix-key fix-list)
2253 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2255 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2256 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2257 (gdb-json-read-buffer fix-key fix-list)))
2259 (defun gdb-line-posns (line)
2260 "Return a pair of LINE beginning and end positions."
2261 (let ((offset (1+ (- line (line-number-at-pos)))))
2262 (cons
2263 (line-beginning-position offset)
2264 (line-end-position offset))))
2266 (defmacro gdb-mark-line (line variable)
2267 "Set VARIABLE marker to point at beginning of LINE.
2269 If current window has no fringes, inverse colors on LINE.
2271 Return position where LINE begins."
2272 `(save-excursion
2273 (let* ((posns (gdb-line-posns ,line))
2274 (start-posn (car posns))
2275 (end-posn (cdr posns)))
2276 (set-marker ,variable (copy-marker start-posn))
2277 (when (not (> (car (window-fringes)) 0))
2278 (put-text-property start-posn end-posn
2279 'font-lock-face '(:inverse-video t)))
2280 start-posn)))
2282 (defun gdb-pad-string (string padding)
2283 (format (concat "%" (number-to-string padding) "s") string))
2285 ;; gdb-table struct is a way to programmatically construct simple
2286 ;; tables. It help to reliably align columns of data in GDB buffers
2287 ;; and provides
2288 (defstruct
2289 gdb-table
2290 (column-sizes nil)
2291 (rows nil)
2292 (row-properties nil)
2293 (right-align nil))
2295 (defun gdb-mapcar* (function &rest seqs)
2296 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2297 If there are several SEQS, FUNCTION is called with that many
2298 arguments, and mapping stops as soon as the shortest list runs
2299 out."
2300 (let ((shortest (apply #'min (mapcar #'length seqs))))
2301 (mapcar (lambda (i)
2302 (apply function
2303 (mapcar
2304 (lambda (seq)
2305 (nth i seq))
2306 seqs)))
2307 (number-sequence 0 (1- shortest)))))
2309 (defun gdb-table-add-row (table row &optional properties)
2310 "Add ROW of string to TABLE and recalculate column sizes.
2312 When non-nil, PROPERTIES will be added to the whole row when
2313 calling `gdb-table-string'."
2314 (let ((rows (gdb-table-rows table))
2315 (row-properties (gdb-table-row-properties table))
2316 (column-sizes (gdb-table-column-sizes table))
2317 (right-align (gdb-table-right-align table)))
2318 (when (not column-sizes)
2319 (setf (gdb-table-column-sizes table)
2320 (make-list (length row) 0)))
2321 (setf (gdb-table-rows table)
2322 (append rows (list row)))
2323 (setf (gdb-table-row-properties table)
2324 (append row-properties (list properties)))
2325 (setf (gdb-table-column-sizes table)
2326 (gdb-mapcar* (lambda (x s)
2327 (let ((new-x
2328 (max (abs x) (string-width (or s "")))))
2329 (if right-align new-x (- new-x))))
2330 (gdb-table-column-sizes table)
2331 row))
2332 ;; Avoid trailing whitespace at eol
2333 (if (not (gdb-table-right-align table))
2334 (setcar (last (gdb-table-column-sizes table)) 0))))
2336 (defun gdb-table-string (table &optional sep)
2337 "Return TABLE as a string with columns separated with SEP."
2338 (let ((column-sizes (gdb-table-column-sizes table)))
2339 (mapconcat
2340 'identity
2341 (gdb-mapcar*
2342 (lambda (row properties)
2343 (apply 'propertize
2344 (mapconcat 'identity
2345 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2346 row column-sizes)
2347 sep)
2348 properties))
2349 (gdb-table-rows table)
2350 (gdb-table-row-properties table))
2351 "\n")))
2353 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2354 (defun gdb-get-many-fields (struct &rest fields)
2355 "Return a list of FIELDS values from STRUCT."
2356 (let ((values))
2357 (dolist (field fields values)
2358 (setq values (append values (list (bindat-get-field struct field)))))))
2360 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2361 handler-name
2362 &optional signal-list)
2363 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2364 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2365 buffer with `gdb-bind-function-to-buffer'.
2367 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2368 defined trigger is called with an argument from SIGNAL-LIST. It's
2369 not recommended to define triggers with empty SIGNAL-LIST.
2370 Normally triggers should respond at least to 'update signal.
2372 Normally the trigger defined by this command must be called from
2373 the buffer where HANDLER-NAME must work. This should be done so
2374 that buffer-local thread number may be used in GDB-COMMAND (by
2375 calling `gdb-current-context-command').
2376 `gdb-bind-function-to-buffer' is used to achieve this, see
2377 `gdb-get-buffer-create'.
2379 Triggers defined by this command are meant to be used as a
2380 trigger argument when describing buffer types with
2381 `gdb-set-buffer-rules'."
2382 `(defun ,trigger-name (&optional signal)
2383 (when
2384 (or (not ,signal-list)
2385 (memq signal ,signal-list))
2386 (when (not (gdb-pending-p
2387 (cons (current-buffer) ',trigger-name)))
2388 (gdb-input ,gdb-command
2389 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2390 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2392 ;; Used by disassembly buffer only, the rest use
2393 ;; def-gdb-trigger-and-handler
2394 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2395 &optional nopreserve)
2396 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2398 Handlers are normally called from the buffers they put output in.
2400 Delete ((current-buffer) . TRIGGER-NAME) from
2401 `gdb-pending-triggers', erase current buffer and evaluate
2402 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2404 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2405 `(defun ,handler-name ()
2406 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2407 (let* ((buffer-read-only nil)
2408 (window (get-buffer-window (current-buffer) 0))
2409 (start (window-start window))
2410 (p (window-point window)))
2411 (erase-buffer)
2412 (,custom-defun)
2413 (gdb-update-buffer-name)
2414 ,(when (not nopreserve)
2415 '(set-window-start window start)
2416 '(set-window-point window p)))))
2418 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2419 handler-name custom-defun
2420 &optional signal-list)
2421 "Define trigger and handler.
2423 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2424 `def-gdb-auto-update-trigger'.
2426 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2427 `def-gdb-auto-update-handler'."
2428 `(progn
2429 (def-gdb-auto-update-trigger ,trigger-name
2430 ,gdb-command
2431 ,handler-name ,signal-list)
2432 (def-gdb-auto-update-handler ,handler-name
2433 ,trigger-name ,custom-defun)))
2437 ;; Breakpoint buffer : This displays the output of `-break-list'.
2438 (def-gdb-trigger-and-handler
2439 gdb-invalidate-breakpoints "-break-list"
2440 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2441 '(start update))
2443 (gdb-set-buffer-rules
2444 'gdb-breakpoints-buffer
2445 'gdb-breakpoints-buffer-name
2446 'gdb-breakpoints-mode
2447 'gdb-invalidate-breakpoints)
2449 (defun gdb-breakpoints-list-handler-custom ()
2450 (let ((breakpoints-list (bindat-get-field
2451 (gdb-json-partial-output "bkpt" "script")
2452 'BreakpointTable 'body))
2453 (table (make-gdb-table)))
2454 (setq gdb-breakpoints-list nil)
2455 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2456 (dolist (breakpoint breakpoints-list)
2457 (add-to-list 'gdb-breakpoints-list
2458 (cons (bindat-get-field breakpoint 'number)
2459 breakpoint))
2460 (let ((at (bindat-get-field breakpoint 'at))
2461 (pending (bindat-get-field breakpoint 'pending))
2462 (func (bindat-get-field breakpoint 'func))
2463 (type (bindat-get-field breakpoint 'type)))
2464 (gdb-table-add-row table
2465 (list
2466 (bindat-get-field breakpoint 'number)
2467 (or type "")
2468 (or (bindat-get-field breakpoint 'disp) "")
2469 (let ((flag (bindat-get-field breakpoint 'enabled)))
2470 (if (string-equal flag "y")
2471 (propertize "y" 'font-lock-face font-lock-warning-face)
2472 (propertize "n" 'font-lock-face font-lock-comment-face)))
2473 (bindat-get-field breakpoint 'addr)
2474 (or (bindat-get-field breakpoint 'times) "")
2475 (if (and type (string-match ".*watchpoint" type))
2476 (bindat-get-field breakpoint 'what)
2477 (or pending at
2478 (concat "in "
2479 (propertize (or func "unknown")
2480 'font-lock-face font-lock-function-name-face)
2481 (gdb-frame-location breakpoint)))))
2482 ;; Add clickable properties only for breakpoints with file:line
2483 ;; information
2484 (append (list 'gdb-breakpoint breakpoint)
2485 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2486 mouse-face highlight))))))
2487 (insert (gdb-table-string table " "))
2488 (gdb-place-breakpoints)))
2490 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2491 (defun gdb-place-breakpoints ()
2492 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2493 (dolist (buffer (buffer-list))
2494 (with-current-buffer buffer
2495 (if (and (eq gud-minor-mode 'gdbmi)
2496 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2497 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2498 (dolist (breakpoint gdb-breakpoints-list)
2499 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2500 ; an associative list
2501 (line (bindat-get-field breakpoint 'line)))
2502 (when line
2503 (let ((file (bindat-get-field breakpoint 'fullname))
2504 (flag (bindat-get-field breakpoint 'enabled))
2505 (bptno (bindat-get-field breakpoint 'number)))
2506 (unless (file-exists-p file)
2507 (setq file (cdr (assoc bptno gdb-location-alist))))
2508 (if (and file
2509 (not (string-equal file "File not found")))
2510 (with-current-buffer
2511 (find-file-noselect file 'nowarn)
2512 (gdb-init-buffer)
2513 ;; Only want one breakpoint icon at each location.
2514 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2515 (string-to-number line)))
2516 (gdb-input (concat "list " file ":1") 'ignore)
2517 (gdb-input "-file-list-exec-source-file"
2518 `(lambda () (gdb-get-location
2519 ,bptno ,line ,flag)))))))))
2521 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2523 (defun gdb-get-location (bptno line flag)
2524 "Find the directory containing the relevant source file.
2525 Put in buffer and place breakpoint icon."
2526 (goto-char (point-min))
2527 (catch 'file-not-found
2528 (if (re-search-forward gdb-source-file-regexp nil t)
2529 (delete (cons bptno "File not found") gdb-location-alist)
2530 (push (cons bptno (match-string 1)) gdb-location-alist)
2531 (gdb-resync)
2532 (unless (assoc bptno gdb-location-alist)
2533 (push (cons bptno "File not found") gdb-location-alist)
2534 (message-box "Cannot find source file for breakpoint location.
2535 Add directory to search path for source files using the GDB command, dir."))
2536 (throw 'file-not-found nil))
2537 (with-current-buffer (find-file-noselect (match-string 1))
2538 (gdb-init-buffer)
2539 ;; only want one breakpoint icon at each location
2540 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2542 (add-hook 'find-file-hook 'gdb-find-file-hook)
2544 (defun gdb-find-file-hook ()
2545 "Set up buffer for debugging if file is part of the source code
2546 of the current session."
2547 (if (and (buffer-name gud-comint-buffer)
2548 ;; in case gud or gdb-ui is just loaded
2549 gud-comint-buffer
2550 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2551 'gdbmi))
2552 (if (member buffer-file-name gdb-source-file-list)
2553 (with-current-buffer (find-buffer-visiting buffer-file-name)
2554 (gdb-init-buffer)))))
2556 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2557 (declare-function gud-break "gdb-mi" t t) ; gud-def
2558 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2560 (defun gdb-mouse-set-clear-breakpoint (event)
2561 "Set/clear breakpoint in left fringe/margin at mouse click.
2562 If not in a source or disassembly buffer just set point."
2563 (interactive "e")
2564 (mouse-minibuffer-check event)
2565 (let ((posn (event-end event)))
2566 (with-selected-window (posn-window posn)
2567 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2568 (if (numberp (posn-point posn))
2569 (save-excursion
2570 (goto-char (posn-point posn))
2571 (if (or (posn-object posn)
2572 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2573 'breakpoint))
2574 (gud-remove nil)
2575 (gud-break nil)))))
2576 (posn-set-point posn))))
2578 (defun gdb-mouse-toggle-breakpoint-margin (event)
2579 "Enable/disable breakpoint in left margin with mouse click."
2580 (interactive "e")
2581 (mouse-minibuffer-check event)
2582 (let ((posn (event-end event)))
2583 (if (numberp (posn-point posn))
2584 (with-selected-window (posn-window posn)
2585 (save-excursion
2586 (goto-char (posn-point posn))
2587 (if (posn-object posn)
2588 (gud-basic-call
2589 (let ((bptno (get-text-property
2590 0 'gdb-bptno (car (posn-string posn)))))
2591 (concat
2592 (if (get-text-property
2593 0 'gdb-enabled (car (posn-string posn)))
2594 "-break-disable "
2595 "-break-enable ")
2596 bptno)))))))))
2598 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2599 "Enable/disable breakpoint in left fringe with mouse click."
2600 (interactive "e")
2601 (mouse-minibuffer-check event)
2602 (let* ((posn (event-end event))
2603 (pos (posn-point posn))
2604 obj)
2605 (when (numberp pos)
2606 (with-selected-window (posn-window posn)
2607 (with-current-buffer (window-buffer (selected-window))
2608 (goto-char pos)
2609 (dolist (overlay (overlays-in pos pos))
2610 (when (overlay-get overlay 'put-break)
2611 (setq obj (overlay-get overlay 'before-string))))
2612 (when (stringp obj)
2613 (gud-basic-call
2614 (concat
2615 (if (get-text-property 0 'gdb-enabled obj)
2616 "-break-disable "
2617 "-break-enable ")
2618 (get-text-property 0 'gdb-bptno obj)))))))))
2620 (defun gdb-breakpoints-buffer-name ()
2621 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2623 (def-gdb-display-buffer
2624 gdb-display-breakpoints-buffer
2625 'gdb-breakpoints-buffer
2626 "Display status of user-settable breakpoints.")
2628 (def-gdb-frame-for-buffer
2629 gdb-frame-breakpoints-buffer
2630 'gdb-breakpoints-buffer
2631 "Display status of user-settable breakpoints in a new frame.")
2633 (defvar gdb-breakpoints-mode-map
2634 (let ((map (make-sparse-keymap))
2635 (menu (make-sparse-keymap "Breakpoints")))
2636 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2637 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2638 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2639 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2640 (suppress-keymap map)
2641 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2642 (define-key map " " 'gdb-toggle-breakpoint)
2643 (define-key map "D" 'gdb-delete-breakpoint)
2644 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2645 (define-key map "q" 'gdb-delete-frame-or-window)
2646 (define-key map "\r" 'gdb-goto-breakpoint)
2647 (define-key map "\t" (lambda ()
2648 (interactive)
2649 (gdb-set-window-buffer
2650 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2651 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2652 (define-key map [follow-link] 'mouse-face)
2653 map))
2655 (defun gdb-delete-frame-or-window ()
2656 "Delete frame if there is only one window. Otherwise delete the window."
2657 (interactive)
2658 (if (one-window-p) (delete-frame)
2659 (delete-window)))
2661 ;;from make-mode-line-mouse-map
2662 (defun gdb-make-header-line-mouse-map (mouse function) "\
2663 Return a keymap with single entry for mouse key MOUSE on the header line.
2664 MOUSE is defined to run function FUNCTION with no args in the buffer
2665 corresponding to the mode line clicked."
2666 (let ((map (make-sparse-keymap)))
2667 (define-key map (vector 'header-line mouse) function)
2668 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2669 map))
2671 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2672 `(propertize ,name
2673 'help-echo ,help-echo
2674 'mouse-face ',mouse-face
2675 'face ',face
2676 'local-map
2677 (gdb-make-header-line-mouse-map
2678 'mouse-1
2679 (lambda (event) (interactive "e")
2680 (save-selected-window
2681 (select-window (posn-window (event-start event)))
2682 (gdb-set-window-buffer
2683 (gdb-get-buffer-create ',buffer) t) )))))
2686 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2687 ;;; Threads view
2689 (defun gdb-threads-buffer-name ()
2690 (concat "*threads of " (gdb-get-target-string) "*"))
2692 (def-gdb-display-buffer
2693 gdb-display-threads-buffer
2694 'gdb-threads-buffer
2695 "Display GDB threads.")
2697 (def-gdb-frame-for-buffer
2698 gdb-frame-threads-buffer
2699 'gdb-threads-buffer
2700 "Display GDB threads in a new frame.")
2702 (def-gdb-trigger-and-handler
2703 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2704 gdb-thread-list-handler gdb-thread-list-handler-custom
2705 '(start update update-threads))
2707 (gdb-set-buffer-rules
2708 'gdb-threads-buffer
2709 'gdb-threads-buffer-name
2710 'gdb-threads-mode
2711 'gdb-invalidate-threads)
2713 (defvar gdb-threads-font-lock-keywords
2714 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2715 (" \\(stopped\\)" (1 font-lock-warning-face))
2716 (" \\(running\\)" (1 font-lock-string-face))
2717 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2718 "Font lock keywords used in `gdb-threads-mode'.")
2720 (defvar gdb-threads-mode-map
2721 (let ((map (make-sparse-keymap)))
2722 (define-key map "\r" 'gdb-select-thread)
2723 (define-key map "f" 'gdb-display-stack-for-thread)
2724 (define-key map "F" 'gdb-frame-stack-for-thread)
2725 (define-key map "l" 'gdb-display-locals-for-thread)
2726 (define-key map "L" 'gdb-frame-locals-for-thread)
2727 (define-key map "r" 'gdb-display-registers-for-thread)
2728 (define-key map "R" 'gdb-frame-registers-for-thread)
2729 (define-key map "d" 'gdb-display-disassembly-for-thread)
2730 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2731 (define-key map "i" 'gdb-interrupt-thread)
2732 (define-key map "c" 'gdb-continue-thread)
2733 (define-key map "s" 'gdb-step-thread)
2734 (define-key map "\t"
2735 (lambda ()
2736 (interactive)
2737 (gdb-set-window-buffer
2738 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2739 (define-key map [mouse-2] 'gdb-select-thread)
2740 (define-key map [follow-link] 'mouse-face)
2741 map))
2743 (defvar gdb-threads-header
2744 (list
2745 (gdb-propertize-header
2746 "Breakpoints" gdb-breakpoints-buffer
2747 "mouse-1: select" mode-line-highlight mode-line-inactive)
2749 (gdb-propertize-header "Threads" gdb-threads-buffer
2750 nil nil mode-line)))
2752 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2753 "Major mode for GDB threads."
2754 (setq gdb-thread-position (make-marker))
2755 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2756 (setq header-line-format gdb-threads-header)
2757 (set (make-local-variable 'font-lock-defaults)
2758 '(gdb-threads-font-lock-keywords))
2759 'gdb-invalidate-threads)
2761 (defun gdb-thread-list-handler-custom ()
2762 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2763 (table (make-gdb-table))
2764 (marked-line nil))
2765 (setq gdb-threads-list nil)
2766 (setq gdb-running-threads-count 0)
2767 (setq gdb-stopped-threads-count 0)
2768 (set-marker gdb-thread-position nil)
2770 (dolist (thread (reverse threads-list))
2771 (let ((running (equal (bindat-get-field thread 'state) "running")))
2772 (add-to-list 'gdb-threads-list
2773 (cons (bindat-get-field thread 'id)
2774 thread))
2775 (if running
2776 (incf gdb-running-threads-count)
2777 (incf gdb-stopped-threads-count))
2779 (gdb-table-add-row table
2780 (list
2781 (bindat-get-field thread 'id)
2782 (concat
2783 (if gdb-thread-buffer-verbose-names
2784 (concat (bindat-get-field thread 'target-id) " ") "")
2785 (bindat-get-field thread 'state)
2786 ;; Include frame information for stopped threads
2787 (if (not running)
2788 (concat
2789 " in " (bindat-get-field thread 'frame 'func)
2790 (if gdb-thread-buffer-arguments
2791 (concat
2792 " ("
2793 (let ((args (bindat-get-field thread 'frame 'args)))
2794 (mapconcat
2795 (lambda (arg)
2796 (apply #'format "%s=%s"
2797 (gdb-get-many-fields arg 'name 'value)))
2798 args ","))
2799 ")")
2801 (if gdb-thread-buffer-locations
2802 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2803 (if gdb-thread-buffer-addresses
2804 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2805 "")))
2806 (list
2807 'gdb-thread thread
2808 'mouse-face 'highlight
2809 'help-echo "mouse-2, RET: select thread")))
2810 (when (string-equal gdb-thread-number
2811 (bindat-get-field thread 'id))
2812 (setq marked-line (length gdb-threads-list))))
2813 (insert (gdb-table-string table " "))
2814 (when marked-line
2815 (gdb-mark-line marked-line gdb-thread-position)))
2816 ;; We update gud-running here because we need to make sure that
2817 ;; gdb-threads-list is up-to-date
2818 (gdb-update-gud-running)
2819 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2821 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2822 "Define a NAME command which will act upon thread on the current line.
2824 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2825 be the value of 'gdb-thread property of the current line. If
2826 'gdb-thread is nil, error is signaled."
2827 `(defun ,name (&optional event)
2828 ,(when doc doc)
2829 (interactive (list last-input-event))
2830 (if event (posn-set-point (event-end event)))
2831 (save-excursion
2832 (beginning-of-line)
2833 (let ((thread (get-text-property (point) 'gdb-thread)))
2834 (if thread
2835 ,custom-defun
2836 (error "Not recognized as thread line"))))))
2838 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2839 &optional doc)
2840 "Define a NAME which will call BUFFER-COMMAND with id of thread
2841 on the current line."
2842 `(def-gdb-thread-buffer-command ,name
2843 (,buffer-command (bindat-get-field thread 'id))
2844 ,doc))
2846 (def-gdb-thread-buffer-command gdb-select-thread
2847 (let ((new-id (bindat-get-field thread 'id)))
2848 (gdb-setq-thread-number new-id)
2849 (gdb-input (concat "-thread-select " new-id) 'ignore)
2850 (gdb-update))
2851 "Select the thread at current line of threads buffer.")
2853 (def-gdb-thread-buffer-simple-command
2854 gdb-display-stack-for-thread
2855 gdb-preemptively-display-stack-buffer
2856 "Display stack buffer for the thread at current line.")
2858 (def-gdb-thread-buffer-simple-command
2859 gdb-display-locals-for-thread
2860 gdb-preemptively-display-locals-buffer
2861 "Display locals buffer for the thread at current line.")
2863 (def-gdb-thread-buffer-simple-command
2864 gdb-display-registers-for-thread
2865 gdb-preemptively-display-registers-buffer
2866 "Display registers buffer for the thread at current line.")
2868 (def-gdb-thread-buffer-simple-command
2869 gdb-display-disassembly-for-thread
2870 gdb-preemptively-display-disassembly-buffer
2871 "Display disassembly buffer for the thread at current line.")
2873 (def-gdb-thread-buffer-simple-command
2874 gdb-frame-stack-for-thread
2875 gdb-frame-stack-buffer
2876 "Display a new frame with stack buffer for the thread at
2877 current line.")
2879 (def-gdb-thread-buffer-simple-command
2880 gdb-frame-locals-for-thread
2881 gdb-frame-locals-buffer
2882 "Display a new frame with locals buffer for the thread at
2883 current line.")
2885 (def-gdb-thread-buffer-simple-command
2886 gdb-frame-registers-for-thread
2887 gdb-frame-registers-buffer
2888 "Display a new frame with registers buffer for the thread at
2889 current line.")
2891 (def-gdb-thread-buffer-simple-command
2892 gdb-frame-disassembly-for-thread
2893 gdb-frame-disassembly-buffer
2894 "Display a new frame with disassembly buffer for the thread at
2895 current line.")
2897 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2898 "Define a NAME which will execute GUD-COMMAND with
2899 `gdb-thread-number' locally bound to id of thread on the current
2900 line."
2901 `(def-gdb-thread-buffer-command ,name
2902 (if gdb-non-stop
2903 (let ((gdb-thread-number (bindat-get-field thread 'id))
2904 (gdb-gud-control-all-threads nil))
2905 (call-interactively #',gud-command))
2906 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2907 ,doc))
2909 (def-gdb-thread-buffer-gud-command
2910 gdb-interrupt-thread
2911 gud-stop-subjob
2912 "Interrupt thread at current line.")
2914 (def-gdb-thread-buffer-gud-command
2915 gdb-continue-thread
2916 gud-cont
2917 "Continue thread at current line.")
2919 (def-gdb-thread-buffer-gud-command
2920 gdb-step-thread
2921 gud-step
2922 "Step thread at current line.")
2925 ;;; Memory view
2927 (defcustom gdb-memory-rows 8
2928 "Number of data rows in memory window."
2929 :type 'integer
2930 :group 'gud
2931 :version "23.2")
2933 (defcustom gdb-memory-columns 4
2934 "Number of data columns in memory window."
2935 :type 'integer
2936 :group 'gud
2937 :version "23.2")
2939 (defcustom gdb-memory-format "x"
2940 "Display format of data items in memory window."
2941 :type '(choice (const :tag "Hexadecimal" "x")
2942 (const :tag "Signed decimal" "d")
2943 (const :tag "Unsigned decimal" "u")
2944 (const :tag "Octal" "o")
2945 (const :tag "Binary" "t"))
2946 :group 'gud
2947 :version "22.1")
2949 (defcustom gdb-memory-unit 4
2950 "Unit size of data items in memory window."
2951 :type '(choice (const :tag "Byte" 1)
2952 (const :tag "Halfword" 2)
2953 (const :tag "Word" 4)
2954 (const :tag "Giant word" 8))
2955 :group 'gud
2956 :version "23.2")
2958 (def-gdb-trigger-and-handler
2959 gdb-invalidate-memory
2960 (format "-data-read-memory %s %s %d %d %d"
2961 gdb-memory-address
2962 gdb-memory-format
2963 gdb-memory-unit
2964 gdb-memory-rows
2965 gdb-memory-columns)
2966 gdb-read-memory-handler
2967 gdb-read-memory-custom
2968 '(start update))
2970 (gdb-set-buffer-rules
2971 'gdb-memory-buffer
2972 'gdb-memory-buffer-name
2973 'gdb-memory-mode
2974 'gdb-invalidate-memory)
2976 (defun gdb-memory-column-width (size format)
2977 "Return length of string with memory unit of SIZE in FORMAT.
2979 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2980 in `gdb-memory-format'."
2981 (let ((format-base (cdr (assoc format
2982 '(("x" . 16)
2983 ("d" . 10) ("u" . 10)
2984 ("o" . 8)
2985 ("t" . 2))))))
2986 (if format-base
2987 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2988 (cond ((string-equal format "x")
2989 (+ 2 res)) ; hexadecimal numbers have 0x in front
2990 ((or (string-equal format "d")
2991 (string-equal format "o"))
2992 (1+ res))
2993 (t res)))
2994 (error "Unknown format"))))
2996 (defun gdb-read-memory-custom ()
2997 (let* ((res (gdb-json-partial-output))
2998 (err-msg (bindat-get-field res 'msg)))
2999 (if (not err-msg)
3000 (let ((memory (bindat-get-field res 'memory)))
3001 (setq gdb-memory-address (bindat-get-field res 'addr))
3002 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3003 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3004 (setq gdb-memory-last-address gdb-memory-address)
3005 (dolist (row memory)
3006 (insert (concat (bindat-get-field row 'addr) ":"))
3007 (dolist (column (bindat-get-field row 'data))
3008 (insert (gdb-pad-string column
3009 (+ 2 (gdb-memory-column-width
3010 gdb-memory-unit
3011 gdb-memory-format)))))
3012 (newline)))
3013 ;; Show last page instead of empty buffer when out of bounds
3014 (progn
3015 (let ((gdb-memory-address gdb-memory-last-address))
3016 (gdb-invalidate-memory 'update)
3017 (error err-msg))))))
3019 (defvar gdb-memory-mode-map
3020 (let ((map (make-sparse-keymap)))
3021 (suppress-keymap map t)
3022 (define-key map "q" 'kill-this-buffer)
3023 (define-key map "n" 'gdb-memory-show-next-page)
3024 (define-key map "p" 'gdb-memory-show-previous-page)
3025 (define-key map "a" 'gdb-memory-set-address)
3026 (define-key map "t" 'gdb-memory-format-binary)
3027 (define-key map "o" 'gdb-memory-format-octal)
3028 (define-key map "u" 'gdb-memory-format-unsigned)
3029 (define-key map "d" 'gdb-memory-format-signed)
3030 (define-key map "x" 'gdb-memory-format-hexadecimal)
3031 (define-key map "b" 'gdb-memory-unit-byte)
3032 (define-key map "h" 'gdb-memory-unit-halfword)
3033 (define-key map "w" 'gdb-memory-unit-word)
3034 (define-key map "g" 'gdb-memory-unit-giant)
3035 (define-key map "R" 'gdb-memory-set-rows)
3036 (define-key map "C" 'gdb-memory-set-columns)
3037 map))
3039 (defun gdb-memory-set-address-event (event)
3040 "Handle a click on address field in memory buffer header."
3041 (interactive "e")
3042 (save-selected-window
3043 (select-window (posn-window (event-start event)))
3044 (gdb-memory-set-address)))
3046 ;; Non-event version for use within keymap
3047 (defun gdb-memory-set-address ()
3048 "Set the start memory address."
3049 (interactive)
3050 (let ((arg (read-from-minibuffer "Memory address: ")))
3051 (setq gdb-memory-address arg))
3052 (gdb-invalidate-memory 'update))
3054 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3055 "Define a function NAME which reads new VAR value from minibuffer."
3056 `(defun ,name (event)
3057 ,(when doc doc)
3058 (interactive "e")
3059 (save-selected-window
3060 (select-window (posn-window (event-start event)))
3061 (let* ((arg (read-from-minibuffer ,echo-string))
3062 (count (string-to-number arg)))
3063 (if (<= count 0)
3064 (error "Positive number only")
3065 (customize-set-variable ',variable count)
3066 (gdb-invalidate-memory 'update))))))
3068 (def-gdb-set-positive-number
3069 gdb-memory-set-rows
3070 gdb-memory-rows
3071 "Rows: "
3072 "Set the number of data rows in memory window.")
3074 (def-gdb-set-positive-number
3075 gdb-memory-set-columns
3076 gdb-memory-columns
3077 "Columns: "
3078 "Set the number of data columns in memory window.")
3080 (defmacro def-gdb-memory-format (name format doc)
3081 "Define a function NAME to switch memory buffer to use FORMAT.
3083 DOC is an optional documentation string."
3084 `(defun ,name () ,(when doc doc)
3085 (interactive)
3086 (customize-set-variable 'gdb-memory-format ,format)
3087 (gdb-invalidate-memory 'update)))
3089 (def-gdb-memory-format
3090 gdb-memory-format-binary "t"
3091 "Set the display format to binary.")
3093 (def-gdb-memory-format
3094 gdb-memory-format-octal "o"
3095 "Set the display format to octal.")
3097 (def-gdb-memory-format
3098 gdb-memory-format-unsigned "u"
3099 "Set the display format to unsigned decimal.")
3101 (def-gdb-memory-format
3102 gdb-memory-format-signed "d"
3103 "Set the display format to decimal.")
3105 (def-gdb-memory-format
3106 gdb-memory-format-hexadecimal "x"
3107 "Set the display format to hexadecimal.")
3109 (defvar gdb-memory-format-map
3110 (let ((map (make-sparse-keymap)))
3111 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3112 map)
3113 "Keymap to select format in the header line.")
3115 (defvar gdb-memory-format-menu
3116 (let ((map (make-sparse-keymap "Format")))
3118 (define-key map [binary]
3119 '(menu-item "Binary" gdb-memory-format-binary
3120 :button (:radio . (equal gdb-memory-format "t"))))
3121 (define-key map [octal]
3122 '(menu-item "Octal" gdb-memory-format-octal
3123 :button (:radio . (equal gdb-memory-format "o"))))
3124 (define-key map [unsigned]
3125 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3126 :button (:radio . (equal gdb-memory-format "u"))))
3127 (define-key map [signed]
3128 '(menu-item "Signed Decimal" gdb-memory-format-signed
3129 :button (:radio . (equal gdb-memory-format "d"))))
3130 (define-key map [hexadecimal]
3131 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3132 :button (:radio . (equal gdb-memory-format "x"))))
3133 map)
3134 "Menu of display formats in the header line.")
3136 (defun gdb-memory-format-menu (event)
3137 (interactive "@e")
3138 (x-popup-menu event gdb-memory-format-menu))
3140 (defun gdb-memory-format-menu-1 (event)
3141 (interactive "e")
3142 (save-selected-window
3143 (select-window (posn-window (event-start event)))
3144 (let* ((selection (gdb-memory-format-menu event))
3145 (binding (and selection (lookup-key gdb-memory-format-menu
3146 (vector (car selection))))))
3147 (if binding (call-interactively binding)))))
3149 (defmacro def-gdb-memory-unit (name unit-size doc)
3150 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3152 DOC is an optional documentation string."
3153 `(defun ,name () ,(when doc doc)
3154 (interactive)
3155 (customize-set-variable 'gdb-memory-unit ,unit-size)
3156 (gdb-invalidate-memory 'update)))
3158 (def-gdb-memory-unit gdb-memory-unit-giant 8
3159 "Set the unit size to giant words (eight bytes).")
3161 (def-gdb-memory-unit gdb-memory-unit-word 4
3162 "Set the unit size to words (four bytes).")
3164 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3165 "Set the unit size to halfwords (two bytes).")
3167 (def-gdb-memory-unit gdb-memory-unit-byte 1
3168 "Set the unit size to bytes.")
3170 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3171 "Define a function NAME which show new address in memory buffer.
3173 The defined function switches Memory buffer to show address
3174 stored in ADDRESS-VAR variable.
3176 DOC is an optional documentation string."
3177 `(defun ,name
3178 ,(when doc doc)
3179 (interactive)
3180 (let ((gdb-memory-address ,address-var))
3181 (gdb-invalidate-memory))))
3183 (def-gdb-memory-show-page gdb-memory-show-previous-page
3184 gdb-memory-prev-page)
3186 (def-gdb-memory-show-page gdb-memory-show-next-page
3187 gdb-memory-next-page)
3189 (defvar gdb-memory-unit-map
3190 (let ((map (make-sparse-keymap)))
3191 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3192 map)
3193 "Keymap to select units in the header line.")
3195 (defvar gdb-memory-unit-menu
3196 (let ((map (make-sparse-keymap "Unit")))
3197 (define-key map [giantwords]
3198 '(menu-item "Giant words" gdb-memory-unit-giant
3199 :button (:radio . (equal gdb-memory-unit 8))))
3200 (define-key map [words]
3201 '(menu-item "Words" gdb-memory-unit-word
3202 :button (:radio . (equal gdb-memory-unit 4))))
3203 (define-key map [halfwords]
3204 '(menu-item "Halfwords" gdb-memory-unit-halfword
3205 :button (:radio . (equal gdb-memory-unit 2))))
3206 (define-key map [bytes]
3207 '(menu-item "Bytes" gdb-memory-unit-byte
3208 :button (:radio . (equal gdb-memory-unit 1))))
3209 map)
3210 "Menu of units in the header line.")
3212 (defun gdb-memory-unit-menu (event)
3213 (interactive "@e")
3214 (x-popup-menu event gdb-memory-unit-menu))
3216 (defun gdb-memory-unit-menu-1 (event)
3217 (interactive "e")
3218 (save-selected-window
3219 (select-window (posn-window (event-start event)))
3220 (let* ((selection (gdb-memory-unit-menu event))
3221 (binding (and selection (lookup-key gdb-memory-unit-menu
3222 (vector (car selection))))))
3223 (if binding (call-interactively binding)))))
3225 (defvar gdb-memory-font-lock-keywords
3226 '(;; <__function.name+n>
3227 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3228 (1 font-lock-function-name-face)))
3229 "Font lock keywords used in `gdb-memory-mode'.")
3231 (defvar gdb-memory-header
3232 '(:eval
3233 (concat
3234 "Start address["
3235 (propertize "-"
3236 'face font-lock-warning-face
3237 'help-echo "mouse-1: decrement address"
3238 'mouse-face 'mode-line-highlight
3239 'local-map (gdb-make-header-line-mouse-map
3240 'mouse-1
3241 #'gdb-memory-show-previous-page))
3243 (propertize "+"
3244 'face font-lock-warning-face
3245 'help-echo "mouse-1: increment address"
3246 'mouse-face 'mode-line-highlight
3247 'local-map (gdb-make-header-line-mouse-map
3248 'mouse-1
3249 #'gdb-memory-show-next-page))
3250 "]: "
3251 (propertize gdb-memory-address
3252 'face font-lock-warning-face
3253 'help-echo "mouse-1: set start address"
3254 'mouse-face 'mode-line-highlight
3255 'local-map (gdb-make-header-line-mouse-map
3256 'mouse-1
3257 #'gdb-memory-set-address-event))
3258 " Rows: "
3259 (propertize (number-to-string gdb-memory-rows)
3260 'face font-lock-warning-face
3261 'help-echo "mouse-1: set number of columns"
3262 'mouse-face 'mode-line-highlight
3263 'local-map (gdb-make-header-line-mouse-map
3264 'mouse-1
3265 #'gdb-memory-set-rows))
3266 " Columns: "
3267 (propertize (number-to-string gdb-memory-columns)
3268 'face font-lock-warning-face
3269 'help-echo "mouse-1: set number of columns"
3270 'mouse-face 'mode-line-highlight
3271 'local-map (gdb-make-header-line-mouse-map
3272 'mouse-1
3273 #'gdb-memory-set-columns))
3274 " Display Format: "
3275 (propertize gdb-memory-format
3276 'face font-lock-warning-face
3277 'help-echo "mouse-3: select display format"
3278 'mouse-face 'mode-line-highlight
3279 'local-map gdb-memory-format-map)
3280 " Unit Size: "
3281 (propertize (number-to-string gdb-memory-unit)
3282 'face font-lock-warning-face
3283 'help-echo "mouse-3: select unit size"
3284 'mouse-face 'mode-line-highlight
3285 'local-map gdb-memory-unit-map)))
3286 "Header line used in `gdb-memory-mode'.")
3288 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3289 "Major mode for examining memory."
3290 (setq header-line-format gdb-memory-header)
3291 (set (make-local-variable 'font-lock-defaults)
3292 '(gdb-memory-font-lock-keywords))
3293 'gdb-invalidate-memory)
3295 (defun gdb-memory-buffer-name ()
3296 (concat "*memory of " (gdb-get-target-string) "*"))
3298 (def-gdb-display-buffer
3299 gdb-display-memory-buffer
3300 'gdb-memory-buffer
3301 "Display memory contents.")
3303 (defun gdb-frame-memory-buffer ()
3304 "Display memory contents in a new frame."
3305 (interactive)
3306 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3307 (special-display-frame-alist
3308 `((left-fringe . 0)
3309 (right-fringe . 0)
3310 (width . 83)
3311 ,@gdb-frame-parameters)))
3312 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3315 ;;; Disassembly view
3317 (defun gdb-disassembly-buffer-name ()
3318 (gdb-current-context-buffer-name
3319 (concat "disassembly of " (gdb-get-target-string))))
3321 (def-gdb-display-buffer
3322 gdb-display-disassembly-buffer
3323 'gdb-disassembly-buffer
3324 "Display disassembly for current stack frame.")
3326 (def-gdb-preempt-display-buffer
3327 gdb-preemptively-display-disassembly-buffer
3328 'gdb-disassembly-buffer)
3330 (def-gdb-frame-for-buffer
3331 gdb-frame-disassembly-buffer
3332 'gdb-disassembly-buffer
3333 "Display disassembly in a new frame.")
3335 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3336 (let* ((frame (gdb-current-buffer-frame))
3337 (file (bindat-get-field frame 'fullname))
3338 (line (bindat-get-field frame 'line)))
3339 (if file
3340 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3341 ;; If we're unable to get a file name / line for $PC, simply
3342 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3343 ;; 150 bytes) instructions.
3344 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3345 gdb-disassembly-handler
3346 ;; We update disassembly only after we have actual frame information
3347 ;; about all threads, so no there's `update' signal in this list
3348 '(start update-disassembly))
3350 (def-gdb-auto-update-handler
3351 gdb-disassembly-handler
3352 gdb-invalidate-disassembly
3353 gdb-disassembly-handler-custom
3356 (gdb-set-buffer-rules
3357 'gdb-disassembly-buffer
3358 'gdb-disassembly-buffer-name
3359 'gdb-disassembly-mode
3360 'gdb-invalidate-disassembly)
3362 (defvar gdb-disassembly-font-lock-keywords
3363 '(;; <__function.name+n>
3364 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3365 (1 font-lock-function-name-face))
3366 ;; 0xNNNNNNNN <__function.name+n>: opcode
3367 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3368 (4 font-lock-keyword-face))
3369 ;; %register(at least i386)
3370 ("%\\sw+" . font-lock-variable-name-face)
3371 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3372 (1 font-lock-comment-face)
3373 (2 font-lock-function-name-face))
3374 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3375 "Font lock keywords used in `gdb-disassembly-mode'.")
3377 (defvar gdb-disassembly-mode-map
3378 ;; TODO
3379 (let ((map (make-sparse-keymap)))
3380 (suppress-keymap map)
3381 (define-key map "q" 'kill-this-buffer)
3382 map))
3384 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3385 "Major mode for GDB disassembly information."
3386 ;; TODO Rename overlay variable for disassembly mode
3387 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3388 (setq fringes-outside-margins t)
3389 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3390 (set (make-local-variable 'font-lock-defaults)
3391 '(gdb-disassembly-font-lock-keywords))
3392 'gdb-invalidate-disassembly)
3394 (defun gdb-disassembly-handler-custom ()
3395 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3396 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3397 (table (make-gdb-table))
3398 (marked-line nil))
3399 (dolist (instr instructions)
3400 (gdb-table-add-row table
3401 (list
3402 (bindat-get-field instr 'address)
3403 (let
3404 ((func-name (bindat-get-field instr 'func-name))
3405 (offset (bindat-get-field instr 'offset)))
3406 (if func-name
3407 (format "<%s+%s>:" func-name offset)
3408 ""))
3409 (bindat-get-field instr 'inst)))
3410 (when (string-equal (bindat-get-field instr 'address)
3411 address)
3412 (progn
3413 (setq marked-line (length (gdb-table-rows table)))
3414 (setq fringe-indicator-alist
3415 (if (string-equal gdb-frame-number "0")
3417 '((overlay-arrow . hollow-right-triangle)))))))
3418 (insert (gdb-table-string table " "))
3419 (gdb-disassembly-place-breakpoints)
3420 ;; Mark current position with overlay arrow and scroll window to
3421 ;; that point
3422 (when marked-line
3423 (let ((window (get-buffer-window (current-buffer) 0)))
3424 (set-window-point window (gdb-mark-line marked-line
3425 gdb-disassembly-position))))
3426 (setq mode-name
3427 (gdb-current-context-mode-name
3428 (concat "Disassembly: "
3429 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3431 (defun gdb-disassembly-place-breakpoints ()
3432 (gdb-remove-breakpoint-icons (point-min) (point-max))
3433 (dolist (breakpoint gdb-breakpoints-list)
3434 (let* ((breakpoint (cdr breakpoint))
3435 (bptno (bindat-get-field breakpoint 'number))
3436 (flag (bindat-get-field breakpoint 'enabled))
3437 (address (bindat-get-field breakpoint 'addr)))
3438 (save-excursion
3439 (goto-char (point-min))
3440 (if (re-search-forward (concat "^" address) nil t)
3441 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3444 (defvar gdb-breakpoints-header
3445 (list
3446 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3447 nil nil mode-line)
3449 (gdb-propertize-header "Threads" gdb-threads-buffer
3450 "mouse-1: select" mode-line-highlight
3451 mode-line-inactive)))
3453 ;;; Breakpoints view
3454 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3455 "Major mode for gdb breakpoints."
3456 (setq header-line-format gdb-breakpoints-header)
3457 'gdb-invalidate-breakpoints)
3459 (defun gdb-toggle-breakpoint ()
3460 "Enable/disable breakpoint at current line of breakpoints buffer."
3461 (interactive)
3462 (save-excursion
3463 (beginning-of-line)
3464 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3465 (if breakpoint
3466 (gud-basic-call
3467 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3468 "-break-disable "
3469 "-break-enable ")
3470 (bindat-get-field breakpoint 'number)))
3471 (error "Not recognized as break/watchpoint line")))))
3473 (defun gdb-delete-breakpoint ()
3474 "Delete the breakpoint at current line of breakpoints buffer."
3475 (interactive)
3476 (save-excursion
3477 (beginning-of-line)
3478 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3479 (if breakpoint
3480 (gud-basic-call (concat "-break-delete "
3481 (bindat-get-field breakpoint 'number)))
3482 (error "Not recognized as break/watchpoint line")))))
3484 (defun gdb-goto-breakpoint (&optional event)
3485 "Go to the location of breakpoint at current line of
3486 breakpoints buffer."
3487 (interactive (list last-input-event))
3488 (if event (posn-set-point (event-end event)))
3489 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3490 (let ((window (get-buffer-window gud-comint-buffer)))
3491 (if window (save-selected-window (select-window window))))
3492 (save-excursion
3493 (beginning-of-line)
3494 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3495 (if breakpoint
3496 (let ((bptno (bindat-get-field breakpoint 'number))
3497 (file (bindat-get-field breakpoint 'fullname))
3498 (line (bindat-get-field breakpoint 'line)))
3499 (save-selected-window
3500 (let* ((buffer (find-file-noselect
3501 (if (file-exists-p file) file
3502 (cdr (assoc bptno gdb-location-alist)))))
3503 (window (or (gdb-display-source-buffer buffer)
3504 (display-buffer buffer))))
3505 (setq gdb-source-window window)
3506 (with-current-buffer buffer
3507 (goto-char (point-min))
3508 (forward-line (1- (string-to-number line)))
3509 (set-window-point window (point))))))
3510 (error "Not recognized as break/watchpoint line")))))
3513 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3515 (def-gdb-trigger-and-handler
3516 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3517 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3518 '(start update))
3520 (gdb-set-buffer-rules
3521 'gdb-stack-buffer
3522 'gdb-stack-buffer-name
3523 'gdb-frames-mode
3524 'gdb-invalidate-frames)
3526 (defun gdb-frame-location (frame)
3527 "Return \" of file:line\" or \" of library\" for structure FRAME.
3529 FRAME must have either \"file\" and \"line\" members or \"from\"
3530 member."
3531 (let ((file (bindat-get-field frame 'file))
3532 (line (bindat-get-field frame 'line))
3533 (from (bindat-get-field frame 'from)))
3534 (let ((res (or (and file line (concat file ":" line))
3535 from)))
3536 (if res (concat " of " res) ""))))
3538 (defun gdb-stack-list-frames-custom ()
3539 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3540 (table (make-gdb-table)))
3541 (set-marker gdb-stack-position nil)
3542 (dolist (frame stack)
3543 (gdb-table-add-row table
3544 (list
3545 (bindat-get-field frame 'level)
3546 "in"
3547 (concat
3548 (bindat-get-field frame 'func)
3549 (if gdb-stack-buffer-locations
3550 (gdb-frame-location frame) "")
3551 (if gdb-stack-buffer-addresses
3552 (concat " at " (bindat-get-field frame 'addr)) "")))
3553 `(mouse-face highlight
3554 help-echo "mouse-2, RET: Select frame"
3555 gdb-frame ,frame)))
3556 (insert (gdb-table-string table " ")))
3557 (when (and gdb-frame-number
3558 (gdb-buffer-shows-main-thread-p))
3559 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3560 gdb-stack-position))
3561 (setq mode-name
3562 (gdb-current-context-mode-name "Frames")))
3564 (defun gdb-stack-buffer-name ()
3565 (gdb-current-context-buffer-name
3566 (concat "stack frames of " (gdb-get-target-string))))
3568 (def-gdb-display-buffer
3569 gdb-display-stack-buffer
3570 'gdb-stack-buffer
3571 "Display backtrace of current stack.")
3573 (def-gdb-preempt-display-buffer
3574 gdb-preemptively-display-stack-buffer
3575 'gdb-stack-buffer nil t)
3577 (def-gdb-frame-for-buffer
3578 gdb-frame-stack-buffer
3579 'gdb-stack-buffer
3580 "Display backtrace of current stack in a new frame.")
3582 (defvar gdb-frames-mode-map
3583 (let ((map (make-sparse-keymap)))
3584 (suppress-keymap map)
3585 (define-key map "q" 'kill-this-buffer)
3586 (define-key map "\r" 'gdb-select-frame)
3587 (define-key map [mouse-2] 'gdb-select-frame)
3588 (define-key map [follow-link] 'mouse-face)
3589 map))
3591 (defvar gdb-frames-font-lock-keywords
3592 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3593 "Font lock keywords used in `gdb-frames-mode'.")
3595 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3596 "Major mode for gdb call stack."
3597 (setq gdb-stack-position (make-marker))
3598 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3599 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3600 (set (make-local-variable 'font-lock-defaults)
3601 '(gdb-frames-font-lock-keywords))
3602 'gdb-invalidate-frames)
3604 (defun gdb-select-frame (&optional event)
3605 "Select the frame and display the relevant source."
3606 (interactive (list last-input-event))
3607 (if event (posn-set-point (event-end event)))
3608 (let ((frame (get-text-property (point) 'gdb-frame)))
3609 (if frame
3610 (if (gdb-buffer-shows-main-thread-p)
3611 (let ((new-level (bindat-get-field frame 'level)))
3612 (setq gdb-frame-number new-level)
3613 (gdb-input (concat "-stack-select-frame " new-level)
3614 'ignore)
3615 (gdb-update))
3616 (error "Could not select frame for non-current thread"))
3617 (error "Not recognized as frame line"))))
3620 ;; Locals buffer.
3621 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3622 (def-gdb-trigger-and-handler
3623 gdb-invalidate-locals
3624 (concat (gdb-current-context-command "-stack-list-locals")
3625 " --simple-values")
3626 gdb-locals-handler gdb-locals-handler-custom
3627 '(start update))
3629 (gdb-set-buffer-rules
3630 'gdb-locals-buffer
3631 'gdb-locals-buffer-name
3632 'gdb-locals-mode
3633 'gdb-invalidate-locals)
3635 (defvar gdb-locals-watch-map
3636 (let ((map (make-sparse-keymap)))
3637 (suppress-keymap map)
3638 (define-key map "\r" 'gud-watch)
3639 (define-key map [mouse-2] 'gud-watch)
3640 map)
3641 "Keymap to create watch expression of a complex data type local variable.")
3643 (defvar gdb-edit-locals-map-1
3644 (let ((map (make-sparse-keymap)))
3645 (suppress-keymap map)
3646 (define-key map "\r" 'gdb-edit-locals-value)
3647 (define-key map [mouse-2] 'gdb-edit-locals-value)
3648 map)
3649 "Keymap to edit value of a simple data type local variable.")
3651 (defun gdb-edit-locals-value (&optional event)
3652 "Assign a value to a variable displayed in the locals buffer."
3653 (interactive (list last-input-event))
3654 (save-excursion
3655 (if event (posn-set-point (event-end event)))
3656 (beginning-of-line)
3657 (let* ((var (bindat-get-field
3658 (get-text-property (point) 'gdb-local-variable) 'name))
3659 (value (read-string (format "New value (%s): " var))))
3660 (gud-basic-call
3661 (concat "-gdb-set variable " var " = " value)))))
3663 ;; Don't display values of arrays or structures.
3664 ;; These can be expanded using gud-watch.
3665 (defun gdb-locals-handler-custom ()
3666 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3667 (table (make-gdb-table)))
3668 (dolist (local locals-list)
3669 (let ((name (bindat-get-field local 'name))
3670 (value (bindat-get-field local 'value))
3671 (type (bindat-get-field local 'type)))
3672 (if (or (not value)
3673 (string-match "\\0x" value))
3674 (add-text-properties 0 (length name)
3675 `(mouse-face highlight
3676 help-echo "mouse-2: create watch expression"
3677 local-map ,gdb-locals-watch-map)
3678 name)
3679 (add-text-properties 0 (length value)
3680 `(mouse-face highlight
3681 help-echo "mouse-2: edit value"
3682 local-map ,gdb-edit-locals-map-1)
3683 value))
3684 (gdb-table-add-row
3685 table
3686 (list
3687 (propertize type 'font-lock-face font-lock-type-face)
3688 (propertize name 'font-lock-face font-lock-variable-name-face)
3689 value)
3690 `(gdb-local-variable ,local))))
3691 (insert (gdb-table-string table " "))
3692 (setq mode-name
3693 (gdb-current-context-mode-name
3694 (concat "Locals: "
3695 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3697 (defvar gdb-locals-header
3698 (list
3699 (gdb-propertize-header "Locals" gdb-locals-buffer
3700 nil nil mode-line)
3702 (gdb-propertize-header "Registers" gdb-registers-buffer
3703 "mouse-1: select" mode-line-highlight
3704 mode-line-inactive)))
3706 (defvar gdb-locals-mode-map
3707 (let ((map (make-sparse-keymap)))
3708 (suppress-keymap map)
3709 (define-key map "q" 'kill-this-buffer)
3710 (define-key map "\t" (lambda ()
3711 (interactive)
3712 (gdb-set-window-buffer
3713 (gdb-get-buffer-create
3714 'gdb-registers-buffer
3715 gdb-thread-number) t)))
3716 map))
3718 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3719 "Major mode for gdb locals."
3720 (setq header-line-format gdb-locals-header)
3721 'gdb-invalidate-locals)
3723 (defun gdb-locals-buffer-name ()
3724 (gdb-current-context-buffer-name
3725 (concat "locals of " (gdb-get-target-string))))
3727 (def-gdb-display-buffer
3728 gdb-display-locals-buffer
3729 'gdb-locals-buffer
3730 "Display local variables of current stack and their values.")
3732 (def-gdb-preempt-display-buffer
3733 gdb-preemptively-display-locals-buffer
3734 'gdb-locals-buffer nil t)
3736 (def-gdb-frame-for-buffer
3737 gdb-frame-locals-buffer
3738 'gdb-locals-buffer
3739 "Display local variables of current stack and their values in a new frame.")
3742 ;; Registers buffer.
3744 (def-gdb-trigger-and-handler
3745 gdb-invalidate-registers
3746 (concat (gdb-current-context-command "-data-list-register-values") " x")
3747 gdb-registers-handler
3748 gdb-registers-handler-custom
3749 '(start update))
3751 (gdb-set-buffer-rules
3752 'gdb-registers-buffer
3753 'gdb-registers-buffer-name
3754 'gdb-registers-mode
3755 'gdb-invalidate-registers)
3757 (defun gdb-registers-handler-custom ()
3758 (when gdb-register-names
3759 (let ((register-values
3760 (bindat-get-field (gdb-json-partial-output) 'register-values))
3761 (table (make-gdb-table)))
3762 (dolist (register register-values)
3763 (let* ((register-number (bindat-get-field register 'number))
3764 (value (bindat-get-field register 'value))
3765 (register-name (nth (string-to-number register-number)
3766 gdb-register-names)))
3767 (gdb-table-add-row
3768 table
3769 (list
3770 (propertize register-name
3771 'font-lock-face font-lock-variable-name-face)
3772 (if (member register-number gdb-changed-registers)
3773 (propertize value 'font-lock-face font-lock-warning-face)
3774 value))
3775 `(mouse-face highlight
3776 help-echo "mouse-2: edit value"
3777 gdb-register-name ,register-name))))
3778 (insert (gdb-table-string table " ")))
3779 (setq mode-name
3780 (gdb-current-context-mode-name "Registers"))))
3782 (defun gdb-edit-register-value (&optional event)
3783 "Assign a value to a register displayed in the registers buffer."
3784 (interactive (list last-input-event))
3785 (save-excursion
3786 (if event (posn-set-point (event-end event)))
3787 (beginning-of-line)
3788 (let* ((var (bindat-get-field
3789 (get-text-property (point) 'gdb-register-name)))
3790 (value (read-string (format "New value (%s): " var))))
3791 (gud-basic-call
3792 (concat "-gdb-set variable $" var " = " value)))))
3794 (defvar gdb-registers-mode-map
3795 (let ((map (make-sparse-keymap)))
3796 (suppress-keymap map)
3797 (define-key map "\r" 'gdb-edit-register-value)
3798 (define-key map [mouse-2] 'gdb-edit-register-value)
3799 (define-key map "q" 'kill-this-buffer)
3800 (define-key map "\t" (lambda ()
3801 (interactive)
3802 (gdb-set-window-buffer
3803 (gdb-get-buffer-create
3804 'gdb-locals-buffer
3805 gdb-thread-number) t)))
3806 map))
3808 (defvar gdb-registers-header
3809 (list
3810 (gdb-propertize-header "Locals" gdb-locals-buffer
3811 "mouse-1: select" mode-line-highlight
3812 mode-line-inactive)
3814 (gdb-propertize-header "Registers" gdb-registers-buffer
3815 nil nil mode-line)))
3817 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3818 "Major mode for gdb registers."
3819 (setq header-line-format gdb-registers-header)
3820 'gdb-invalidate-registers)
3822 (defun gdb-registers-buffer-name ()
3823 (gdb-current-context-buffer-name
3824 (concat "registers of " (gdb-get-target-string))))
3826 (def-gdb-display-buffer
3827 gdb-display-registers-buffer
3828 'gdb-registers-buffer
3829 "Display integer register contents.")
3831 (def-gdb-preempt-display-buffer
3832 gdb-preemptively-display-registers-buffer
3833 'gdb-registers-buffer nil t)
3835 (def-gdb-frame-for-buffer
3836 gdb-frame-registers-buffer
3837 'gdb-registers-buffer
3838 "Display integer register contents in a new frame.")
3840 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3841 (defun gdb-get-changed-registers ()
3842 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3843 (not (gdb-pending-p 'gdb-get-changed-registers)))
3844 (gdb-input "-data-list-changed-registers"
3845 'gdb-changed-registers-handler)
3846 (gdb-add-pending 'gdb-get-changed-registers)))
3848 (defun gdb-changed-registers-handler ()
3849 (gdb-delete-pending 'gdb-get-changed-registers)
3850 (setq gdb-changed-registers nil)
3851 (dolist (register-number
3852 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3853 (push register-number gdb-changed-registers)))
3855 (defun gdb-register-names-handler ()
3856 ;; Don't use gdb-pending-triggers because this handler is called
3857 ;; only once (in gdb-init-1)
3858 (setq gdb-register-names nil)
3859 (dolist (register-name
3860 (bindat-get-field (gdb-json-partial-output) 'register-names))
3861 (push register-name gdb-register-names))
3862 (setq gdb-register-names (reverse gdb-register-names)))
3865 (defun gdb-get-source-file-list ()
3866 "Create list of source files for current GDB session.
3867 If buffers already exist for any of these files, gud-minor-mode
3868 is set in them."
3869 (goto-char (point-min))
3870 (while (re-search-forward gdb-source-file-regexp nil t)
3871 (push (match-string 1) gdb-source-file-list))
3872 (dolist (buffer (buffer-list))
3873 (with-current-buffer buffer
3874 (when (member buffer-file-name gdb-source-file-list)
3875 (gdb-init-buffer)))))
3877 (defun gdb-get-main-selected-frame ()
3878 "Trigger for `gdb-frame-handler' which uses main current
3879 thread. Called from `gdb-update'."
3880 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3881 (progn
3882 (gdb-input (gdb-current-context-command "-stack-info-frame")
3883 'gdb-frame-handler)
3884 (gdb-add-pending 'gdb-get-main-selected-frame))))
3886 (defun gdb-frame-handler ()
3887 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3888 overlay arrow in source buffer."
3889 (gdb-delete-pending 'gdb-get-main-selected-frame)
3890 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3891 (when frame
3892 (setq gdb-selected-frame (bindat-get-field frame 'func))
3893 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3894 (setq gdb-frame-number (bindat-get-field frame 'level))
3895 (setq gdb-frame-address (bindat-get-field frame 'addr))
3896 (let ((line (bindat-get-field frame 'line)))
3897 (setq gdb-selected-line (and line (string-to-number line)))
3898 (when (and gdb-selected-file gdb-selected-line)
3899 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3900 (gud-display-frame)))
3901 (if gud-overlay-arrow-position
3902 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3903 (position (marker-position gud-overlay-arrow-position)))
3904 (when buffer
3905 (with-current-buffer buffer
3906 (setq fringe-indicator-alist
3907 (if (string-equal gdb-frame-number "0")
3909 '((overlay-arrow . hollow-right-triangle))))
3910 (setq gud-overlay-arrow-position (make-marker))
3911 (set-marker gud-overlay-arrow-position position))))))))
3913 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3915 (defun gdb-get-prompt ()
3916 "Find prompt for GDB session."
3917 (goto-char (point-min))
3918 (setq gdb-prompt-name nil)
3919 (re-search-forward gdb-prompt-name-regexp nil t)
3920 (setq gdb-prompt-name (match-string 1))
3921 ;; Insert first prompt.
3922 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3924 ;;;; Window management
3925 (defun gdb-display-buffer (buf dedicated &optional frame)
3926 "Show buffer BUF.
3928 If BUF is already displayed in some window, show it, deiconifying
3929 the frame if necessary. Otherwise, find least recently used
3930 window and show BUF there, if the window is not used for GDB
3931 already, in which case that window is split first."
3932 (let ((answer (get-buffer-window buf (or frame 0))))
3933 (if answer
3934 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3935 (let ((window (get-lru-window)))
3936 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3937 'gdbmi)
3938 (let ((largest (get-largest-window)))
3939 (setq answer (split-window largest))
3940 (set-window-buffer answer buf)
3941 (set-window-dedicated-p answer dedicated)
3942 answer)
3943 (set-window-buffer window buf)
3944 window)))))
3946 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3947 "Find window displaying a buffer with the same
3948 `gdb-buffer-type' as BUF and show BUF there. If no such window
3949 exists, just call `gdb-display-buffer' for BUF. If the window
3950 found is already dedicated, split window according to
3951 SPLIT-HORIZONTAL and show BUF in the new window."
3952 (if buf
3953 (when (not (get-buffer-window buf))
3954 (let* ((buf-type (gdb-buffer-type buf))
3955 (existing-window
3956 (get-window-with-predicate
3957 #'(lambda (w)
3958 (and (eq buf-type
3959 (gdb-buffer-type (window-buffer w)))
3960 (not (window-dedicated-p w)))))))
3961 (if existing-window
3962 (set-window-buffer existing-window buf)
3963 (let ((dedicated-window
3964 (get-window-with-predicate
3965 #'(lambda (w)
3966 (eq buf-type
3967 (gdb-buffer-type (window-buffer w)))))))
3968 (if dedicated-window
3969 (set-window-buffer
3970 (split-window dedicated-window nil split-horizontal) buf)
3971 (gdb-display-buffer buf t))))))
3972 (error "Null buffer")))
3974 ;;; Shared keymap initialization:
3976 (let ((menu (make-sparse-keymap "GDB-Windows")))
3977 (define-key gud-menu-map [displays]
3978 `(menu-item "GDB-Windows" ,menu
3979 :visible (eq gud-minor-mode 'gdbmi)))
3980 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3981 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3982 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3983 (define-key menu [disassembly]
3984 '("Disassembly" . gdb-display-disassembly-buffer))
3985 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3986 (define-key menu [inferior]
3987 '("IO" . gdb-display-io-buffer))
3988 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3989 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3990 (define-key menu [breakpoints]
3991 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3993 (let ((menu (make-sparse-keymap "GDB-Frames")))
3994 (define-key gud-menu-map [frames]
3995 `(menu-item "GDB-Frames" ,menu
3996 :visible (eq gud-minor-mode 'gdbmi)))
3997 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3998 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3999 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4000 (define-key menu [disassembly]
4001 '("Disassembly" . gdb-frame-disassembly-buffer))
4002 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4003 (define-key menu [inferior]
4004 '("IO" . gdb-frame-io-buffer))
4005 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4006 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4007 (define-key menu [breakpoints]
4008 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4010 (let ((menu (make-sparse-keymap "GDB-MI")))
4011 (define-key menu [gdb-customize]
4012 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4013 :help "Customize Gdb Graphical Mode options."))
4014 (define-key menu [gdb-many-windows]
4015 '(menu-item "Display Other Windows" gdb-many-windows
4016 :help "Toggle display of locals, stack and breakpoint information"
4017 :button (:toggle . gdb-many-windows)))
4018 (define-key menu [gdb-restore-windows]
4019 '(menu-item "Restore Window Layout" gdb-restore-windows
4020 :help "Restore standard layout for debug session."))
4021 (define-key menu [sep1]
4022 '(menu-item "--"))
4023 (define-key menu [all-threads]
4024 '(menu-item "GUD controls all threads"
4025 (lambda ()
4026 (interactive)
4027 (setq gdb-gud-control-all-threads t))
4028 :help "GUD start/stop commands apply to all threads"
4029 :button (:radio . gdb-gud-control-all-threads)))
4030 (define-key menu [current-thread]
4031 '(menu-item "GUD controls current thread"
4032 (lambda ()
4033 (interactive)
4034 (setq gdb-gud-control-all-threads nil))
4035 :help "GUD start/stop commands apply to current thread only"
4036 :button (:radio . (not gdb-gud-control-all-threads))))
4037 (define-key menu [sep2]
4038 '(menu-item "--"))
4039 (define-key menu [gdb-customize-reasons]
4040 '(menu-item "Customize switching..."
4041 (lambda ()
4042 (interactive)
4043 (customize-option 'gdb-switch-reasons))))
4044 (define-key menu [gdb-switch-when-another-stopped]
4045 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4046 gdb-switch-when-another-stopped
4047 "Automatically switch to stopped thread"
4048 "GDB thread switching %s"
4049 "Switch to stopped thread"))
4050 (define-key gud-menu-map [mi]
4051 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4053 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4054 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4055 ;; tool-bar-local-item-from-menu here because it appends new buttons
4056 ;; to toolbar from right to left while we want our A/T throttle to
4057 ;; show up right before Run button.
4058 (define-key-after gud-tool-bar-map [all-threads]
4059 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4060 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4061 :visible (and (eq gud-minor-mode 'gdbmi)
4062 gdb-non-stop
4063 (not gdb-gud-control-all-threads)))
4064 'run)
4066 (define-key-after gud-tool-bar-map [current-thread]
4067 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4068 :image (find-image '((:type xpm :file "gud/all.xpm")))
4069 :visible (and (eq gud-minor-mode 'gdbmi)
4070 gdb-non-stop
4071 gdb-gud-control-all-threads))
4072 'all-threads)
4074 (defun gdb-frame-gdb-buffer ()
4075 "Display GUD buffer in a new frame."
4076 (interactive)
4077 (display-buffer-other-frame gud-comint-buffer))
4079 (defun gdb-display-gdb-buffer ()
4080 "Display GUD buffer."
4081 (interactive)
4082 (pop-to-buffer gud-comint-buffer nil 0))
4084 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4085 "Set buffer of selected window to NAME and dedicate window.
4087 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4088 window is dedicated."
4089 (unless window (setq window (selected-window)))
4090 (when ignore-dedicated
4091 (set-window-dedicated-p window nil))
4092 (set-window-buffer window (get-buffer name))
4093 (set-window-dedicated-p window t))
4095 (defun gdb-setup-windows ()
4096 "Layout the window pattern for `gdb-many-windows'."
4097 (gdb-display-locals-buffer)
4098 (gdb-display-stack-buffer)
4099 (delete-other-windows)
4100 (gdb-display-breakpoints-buffer)
4101 (delete-other-windows)
4102 ;; Don't dedicate.
4103 (switch-to-buffer gud-comint-buffer)
4104 (let ((win0 (selected-window))
4105 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4106 (win2 (split-window nil ( / (window-height) 3)))
4107 (win3 (split-window-right)))
4108 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4109 (select-window win2)
4110 (set-window-buffer
4111 win2
4112 (if gud-last-last-frame
4113 (gud-find-file (car gud-last-last-frame))
4114 (if gdb-main-file
4115 (gud-find-file gdb-main-file)
4116 ;; Put buffer list in window if we
4117 ;; can't find a source file.
4118 (list-buffers-noselect))))
4119 (setq gdb-source-window (selected-window))
4120 (let ((win4 (split-window-right)))
4121 (gdb-set-window-buffer
4122 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4123 (select-window win1)
4124 (gdb-set-window-buffer (gdb-stack-buffer-name))
4125 (let ((win5 (split-window-right)))
4126 (gdb-set-window-buffer (if gdb-show-threads-by-default
4127 (gdb-threads-buffer-name)
4128 (gdb-breakpoints-buffer-name))
4129 nil win5))
4130 (select-window win0)))
4132 (defcustom gdb-many-windows nil
4133 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4134 In this case it starts with two windows: one displaying the GUD
4135 buffer and the other with the source file with the main routine
4136 of the debugged program. Non-nil means display the layout shown for
4137 `gdb'."
4138 :type 'boolean
4139 :group 'gdb
4140 :version "22.1")
4142 (defun gdb-many-windows (arg)
4143 "Toggle the number of windows in the basic arrangement.
4144 With arg, display additional buffers iff arg is positive."
4145 (interactive "P")
4146 (setq gdb-many-windows
4147 (if (null arg)
4148 (not gdb-many-windows)
4149 (> (prefix-numeric-value arg) 0)))
4150 (message (format "Display of other windows %sabled"
4151 (if gdb-many-windows "en" "dis")))
4152 (if (and gud-comint-buffer
4153 (buffer-name gud-comint-buffer))
4154 (condition-case nil
4155 (gdb-restore-windows)
4156 (error nil))))
4158 (defun gdb-restore-windows ()
4159 "Restore the basic arrangement of windows used by gdb.
4160 This arrangement depends on the value of `gdb-many-windows'."
4161 (interactive)
4162 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4163 (delete-other-windows)
4164 (if gdb-many-windows
4165 (gdb-setup-windows)
4166 (when (or gud-last-last-frame gdb-show-main)
4167 (let ((win (split-window)))
4168 (set-window-buffer
4170 (if gud-last-last-frame
4171 (gud-find-file (car gud-last-last-frame))
4172 (gud-find-file gdb-main-file)))
4173 (setq gdb-source-window win)))))
4175 ;; Called from `gud-sentinel' in gud.el:
4176 (defun gdb-reset ()
4177 "Exit a debugging session cleanly.
4178 Kills the gdb buffers, and resets variables and the source buffers."
4179 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4180 ;; process. This pty must be deleted explicitly.
4181 (let ((pty (get-process "gdb-inferior")))
4182 (if pty (delete-process pty)))
4183 ;; Find gdb-mi buffers and kill them.
4184 (dolist (buffer (buffer-list))
4185 (unless (eq buffer gud-comint-buffer)
4186 (with-current-buffer buffer
4187 (if (eq gud-minor-mode 'gdbmi)
4188 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4189 (kill-buffer nil)
4190 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4191 (setq gud-minor-mode nil)
4192 (kill-local-variable 'tool-bar-map)
4193 (kill-local-variable 'gdb-define-alist))))))
4194 (setq gdb-disassembly-position nil)
4195 (setq overlay-arrow-variable-list
4196 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4197 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4198 (setq gdb-stack-position nil)
4199 (setq overlay-arrow-variable-list
4200 (delq 'gdb-stack-position overlay-arrow-variable-list))
4201 (setq gdb-thread-position nil)
4202 (setq overlay-arrow-variable-list
4203 (delq 'gdb-thread-position overlay-arrow-variable-list))
4204 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4205 (setq gud-running nil)
4206 (setq gdb-active-process nil)
4207 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4209 (defun gdb-get-source-file ()
4210 "Find the source file where the program starts and display it with related
4211 buffers, if required."
4212 (goto-char (point-min))
4213 (if (re-search-forward gdb-source-file-regexp nil t)
4214 (setq gdb-main-file (match-string 1)))
4215 (if gdb-many-windows
4216 (gdb-setup-windows)
4217 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4218 (if (and gdb-show-main gdb-main-file)
4219 (let ((pop-up-windows t))
4220 (display-buffer (gud-find-file gdb-main-file)))))
4221 (gdb-force-mode-line-update
4222 (propertize "ready" 'face font-lock-variable-name-face)))
4224 ;;from put-image
4225 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4226 "Put string PUTSTRING in front of POS in the current buffer.
4227 PUTSTRING is displayed by putting an overlay into the current buffer with a
4228 `before-string' string that has a `display' property whose value is
4229 PUTSTRING."
4230 (let ((string (make-string 1 ?x))
4231 (buffer (current-buffer)))
4232 (setq putstring (copy-sequence putstring))
4233 (let ((overlay (make-overlay pos pos buffer))
4234 (prop (or dprop
4235 (list (list 'margin 'left-margin) putstring))))
4236 (put-text-property 0 1 'display prop string)
4237 (if sprops
4238 (add-text-properties 0 1 sprops string))
4239 (overlay-put overlay 'put-break t)
4240 (overlay-put overlay 'before-string string))))
4242 ;;from remove-images
4243 (defun gdb-remove-strings (start end &optional buffer)
4244 "Remove strings between START and END in BUFFER.
4245 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4246 BUFFER nil or omitted means use the current buffer."
4247 (unless buffer
4248 (setq buffer (current-buffer)))
4249 (dolist (overlay (overlays-in start end))
4250 (when (overlay-get overlay 'put-break)
4251 (delete-overlay overlay))))
4253 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4254 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4255 (start (- (car posns) 1))
4256 (end (+ (cdr posns) 1))
4257 (putstring (if enabled "B" "b"))
4258 (source-window (get-buffer-window (current-buffer) 0)))
4259 (add-text-properties
4260 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4261 putstring)
4262 (if enabled
4263 (add-text-properties
4264 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4265 (add-text-properties
4266 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4267 (gdb-remove-breakpoint-icons start end)
4268 (if (display-images-p)
4269 (if (>= (or left-fringe-width
4270 (if source-window (car (window-fringes source-window)))
4271 gdb-buffer-fringe-width) 8)
4272 (gdb-put-string
4273 nil (1+ start)
4274 `(left-fringe breakpoint
4275 ,(if enabled
4276 'breakpoint-enabled
4277 'breakpoint-disabled))
4278 'gdb-bptno bptno
4279 'gdb-enabled enabled)
4280 (when (< left-margin-width 2)
4281 (save-current-buffer
4282 (setq left-margin-width 2)
4283 (if source-window
4284 (set-window-margins
4285 source-window
4286 left-margin-width right-margin-width))))
4287 (put-image
4288 (if enabled
4289 (or breakpoint-enabled-icon
4290 (setq breakpoint-enabled-icon
4291 (find-image `((:type xpm :data
4292 ,breakpoint-xpm-data
4293 :ascent 100 :pointer hand)
4294 (:type pbm :data
4295 ,breakpoint-enabled-pbm-data
4296 :ascent 100 :pointer hand)))))
4297 (or breakpoint-disabled-icon
4298 (setq breakpoint-disabled-icon
4299 (find-image `((:type xpm :data
4300 ,breakpoint-xpm-data
4301 :conversion disabled
4302 :ascent 100 :pointer hand)
4303 (:type pbm :data
4304 ,breakpoint-disabled-pbm-data
4305 :ascent 100 :pointer hand))))))
4306 (+ start 1)
4307 putstring
4308 'left-margin))
4309 (when (< left-margin-width 2)
4310 (save-current-buffer
4311 (setq left-margin-width 2)
4312 (let ((window (get-buffer-window (current-buffer) 0)))
4313 (if window
4314 (set-window-margins
4315 window left-margin-width right-margin-width)))))
4316 (gdb-put-string
4317 (propertize putstring
4318 'face (if enabled
4319 'breakpoint-enabled 'breakpoint-disabled))
4320 (1+ start)))))
4322 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4323 (gdb-remove-strings start end)
4324 (if (display-images-p)
4325 (remove-images start end))
4326 (when remove-margin
4327 (setq left-margin-width 0)
4328 (let ((window (get-buffer-window (current-buffer) 0)))
4329 (if window
4330 (set-window-margins
4331 window left-margin-width right-margin-width)))))
4334 ;;; Functions for inline completion.
4336 (defvar gud-gdb-fetch-lines-in-progress)
4337 (defvar gud-gdb-fetch-lines-string)
4338 (defvar gud-gdb-fetch-lines-break)
4339 (defvar gud-gdb-fetched-lines)
4341 (defun gud-gdbmi-completions (context command)
4342 "Completion table for GDB/MI commands.
4343 COMMAND is the prefix for which we seek completion.
4344 CONTEXT is the text before COMMAND on the line."
4345 (let ((gud-gdb-fetch-lines-in-progress t)
4346 (gud-gdb-fetch-lines-string nil)
4347 (gud-gdb-fetch-lines-break (length context))
4348 (gud-gdb-fetched-lines nil)
4349 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4350 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4351 complete-list)
4352 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4353 (gdb-input (concat "complete " context command)
4354 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4355 (while gud-gdb-fetch-lines-in-progress
4356 (accept-process-output (get-buffer-process gud-comint-buffer))))
4357 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4359 (defun gud-gdbmi-fetch-lines-filter (string)
4360 "Custom filter function for `gud-gdbmi-completions'."
4361 (setq string (concat gud-gdb-fetch-lines-string
4362 (gud-gdbmi-marker-filter string)))
4363 (while (string-match "\n" string)
4364 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4365 gud-gdb-fetched-lines)
4366 (setq string (substring string (match-end 0))))
4369 (provide 'gdb-mi)
4371 ;;; gdb-mi.el ends here