Merge changes from emacs-24 branch
[emacs.git] / lisp / progmodes / gdb-mi.el
blob0cc5de90573e1cf17af70e338c095eddcd100a8b
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 (when (eq system-type 'windows-nt)
869 ;; Don't create a separate console window for the debuggee.
870 (gdb-input "-gdb-set new-console off" 'ignore)
871 ;; Force GDB to behave as if its input and output stream were
872 ;; connected to a TTY device (since on Windows we use pipes for
873 ;; communicating with GDB).
874 (gdb-input "-gdb-set interactive-mode on" 'ignore))
875 (gdb-input "-gdb-set height 0" 'ignore)
877 (when gdb-non-stop
878 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
880 (gdb-input "-enable-pretty-printing" 'ignore)
882 ;; find source file and compilation directory here
883 (if gdb-create-source-file-list
884 ;; Needs GDB 6.2 onwards.
885 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
886 ;; Needs GDB 6.0 onwards.
887 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
888 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
890 (defun gdb-non-stop-handler ()
891 (goto-char (point-min))
892 (if (re-search-forward "No symbol" nil t)
893 (progn
894 (message
895 "This version of GDB doesn't support non-stop mode. Turning it off.")
896 (setq gdb-non-stop nil)
897 (setq gdb-supports-non-stop nil))
898 (setq gdb-supports-non-stop t)
899 (gdb-input "-gdb-set target-async 1" 'ignore)
900 (gdb-input "-list-target-features" 'gdb-check-target-async)))
902 (defun gdb-check-target-async ()
903 (goto-char (point-min))
904 (unless (re-search-forward "async" nil t)
905 (message
906 "Target doesn't support non-stop mode. Turning it off.")
907 (setq gdb-non-stop nil)
908 (gdb-input "-gdb-set non-stop 0" 'ignore)))
910 (defun gdb-delchar-or-quit (arg)
911 "Delete ARG characters or send a quit command to GDB.
912 Send a quit only if point is at the end of the buffer, there is
913 no input, and GDB is waiting for input."
914 (interactive "p")
915 (unless (and (eq (current-buffer) gud-comint-buffer)
916 (eq gud-minor-mode 'gdbmi))
917 (error "Not in a GDB-MI buffer"))
918 (let ((proc (get-buffer-process gud-comint-buffer)))
919 (if (and (eobp) proc (process-live-p proc)
920 (not gud-running)
921 (= (point) (marker-position (process-mark proc))))
922 ;; Sending an EOF does not work with GDB-MI; submit an
923 ;; explicit quit command.
924 (progn
925 (insert "quit")
926 (comint-send-input t t))
927 (delete-char arg))))
929 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
931 (defun gdb-create-define-alist ()
932 "Create an alist of #define directives for GUD tooltips."
933 (let* ((file (buffer-file-name))
934 (output
935 (with-output-to-string
936 (with-current-buffer standard-output
937 (and file
938 (file-exists-p file)
939 ;; call-process doesn't work with remote file names.
940 (not (file-remote-p default-directory))
941 (call-process shell-file-name file
942 (list t nil) nil "-c"
943 (concat gdb-cpp-define-alist-program " "
944 gdb-cpp-define-alist-flags))))))
945 (define-list (split-string output "\n" t))
946 (name))
947 (setq gdb-define-alist nil)
948 (dolist (define define-list)
949 (setq name (nth 1 (split-string define "[( ]")))
950 (push (cons name define) gdb-define-alist))))
952 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
954 (defun gdb-tooltip-print (expr)
955 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
956 (goto-char (point-min))
957 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
958 (tooltip-show
959 (concat expr " = " (read (match-string 1)))
960 (or gud-tooltip-echo-area
961 (not (display-graphic-p)))))))
963 ;; If expr is a macro for a function don't print because of possible dangerous
964 ;; side-effects. Also printing a function within a tooltip generates an
965 ;; unexpected starting annotation (phase error).
966 (defun gdb-tooltip-print-1 (expr)
967 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
968 (goto-char (point-min))
969 (if (search-forward "expands to: " nil t)
970 (unless (looking-at "\\S-+.*(.*).*")
971 (gdb-input (concat "-data-evaluate-expression " expr)
972 `(lambda () (gdb-tooltip-print ,expr)))))))
974 (defun gdb-init-buffer ()
975 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
976 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
977 (when gud-tooltip-mode
978 (make-local-variable 'gdb-define-alist)
979 (gdb-create-define-alist)
980 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
982 (defmacro gdb-if-arrow (arrow-position &rest body)
983 `(if ,arrow-position
984 (let ((buffer (marker-buffer ,arrow-position)) (line))
985 (if (equal buffer (window-buffer (posn-window end)))
986 (with-current-buffer buffer
987 (when (or (equal start end)
988 (equal (posn-point start)
989 (marker-position ,arrow-position)))
990 ,@body))))))
992 (defun gdb-mouse-until (event)
993 "Continue running until a source line past the current line.
994 The destination source line can be selected either by clicking
995 with mouse-3 on the fringe/margin or dragging the arrow
996 with mouse-1 (default bindings)."
997 (interactive "e")
998 (let ((start (event-start event))
999 (end (event-end event)))
1000 (gdb-if-arrow gud-overlay-arrow-position
1001 (setq line (line-number-at-pos (posn-point end)))
1002 (gud-call (concat "until " (number-to-string line))))
1003 (gdb-if-arrow gdb-disassembly-position
1004 (save-excursion
1005 (goto-char (point-min))
1006 (forward-line (1- (line-number-at-pos (posn-point end))))
1007 (forward-char 2)
1008 (gud-call (concat "until *%a"))))))
1010 (defun gdb-mouse-jump (event)
1011 "Set execution address/line.
1012 The destination source line can be selected either by clicking with C-mouse-3
1013 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1014 Unlike `gdb-mouse-until' the destination address can be before the current
1015 line, and no execution takes place."
1016 (interactive "e")
1017 (let ((start (event-start event))
1018 (end (event-end event)))
1019 (gdb-if-arrow gud-overlay-arrow-position
1020 (setq line (line-number-at-pos (posn-point end)))
1021 (progn
1022 (gud-call (concat "tbreak " (number-to-string line)))
1023 (gud-call (concat "jump " (number-to-string line)))))
1024 (gdb-if-arrow gdb-disassembly-position
1025 (save-excursion
1026 (goto-char (point-min))
1027 (forward-line (1- (line-number-at-pos (posn-point end))))
1028 (forward-char 2)
1029 (progn
1030 (gud-call (concat "tbreak *%a"))
1031 (gud-call (concat "jump *%a")))))))
1033 (defcustom gdb-show-changed-values t
1034 "If non-nil change the face of out of scope variables and changed values.
1035 Out of scope variables are suppressed with `shadow' face.
1036 Changed values are highlighted with the face `font-lock-warning-face'."
1037 :type 'boolean
1038 :group 'gdb
1039 :version "22.1")
1041 (defcustom gdb-max-children 40
1042 "Maximum number of children before expansion requires confirmation."
1043 :type 'integer
1044 :group 'gdb
1045 :version "22.1")
1047 (defcustom gdb-delete-out-of-scope t
1048 "If non-nil delete watch expressions automatically when they go out of scope."
1049 :type 'boolean
1050 :group 'gdb
1051 :version "22.2")
1053 (defcustom gdb-speedbar-auto-raise nil
1054 "If non-nil raise speedbar every time display of watch expressions is\
1055 updated."
1056 :type 'boolean
1057 :group 'gdb
1058 :version "22.1")
1060 (defcustom gdb-use-colon-colon-notation nil
1061 "If non-nil use FUN::VAR format to display variables in the speedbar."
1062 :type 'boolean
1063 :group 'gdb
1064 :version "22.1")
1066 (defun gdb-speedbar-auto-raise (arg)
1067 "Toggle automatic raising of the speedbar for watch expressions.
1068 With prefix argument ARG, automatically raise speedbar if ARG is
1069 positive, otherwise don't automatically raise it."
1070 (interactive "P")
1071 (setq gdb-speedbar-auto-raise
1072 (if (null arg)
1073 (not gdb-speedbar-auto-raise)
1074 (> (prefix-numeric-value arg) 0)))
1075 (message (format "Auto raising %sabled"
1076 (if gdb-speedbar-auto-raise "en" "dis"))))
1078 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1079 (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
1081 (declare-function tooltip-identifier-from-point "tooltip" (point))
1083 (defun gud-watch (&optional arg event)
1084 "Watch expression at point.
1085 With arg, enter name of variable to be watched in the minibuffer."
1086 (interactive (list current-prefix-arg last-input-event))
1087 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1088 (if (eq minor-mode 'gdbmi)
1089 (progn
1090 (if event (posn-set-point (event-end event)))
1091 (require 'tooltip)
1092 (save-selected-window
1093 (let ((expr
1094 (if arg
1095 (completing-read "Name of variable: "
1096 'gud-gdb-complete-command)
1097 (if (and transient-mark-mode mark-active)
1098 (buffer-substring (region-beginning) (region-end))
1099 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1100 (tooltip-identifier-from-point (point)))))))
1101 (set-text-properties 0 (length expr) nil expr)
1102 (gdb-input (concat "-var-create - * " expr "")
1103 `(lambda () (gdb-var-create-handler ,expr))))))
1104 (message "gud-watch is a no-op in this mode."))))
1106 (defun gdb-var-create-handler (expr)
1107 (let* ((result (gdb-json-partial-output)))
1108 (if (not (bindat-get-field result 'msg))
1109 (let ((var
1110 (list (bindat-get-field result 'name)
1111 (if (and (string-equal gdb-current-language "c")
1112 gdb-use-colon-colon-notation gdb-selected-frame)
1113 (setq expr (concat gdb-selected-frame "::" expr))
1114 expr)
1115 (bindat-get-field result 'numchild)
1116 (bindat-get-field result 'type)
1117 (bindat-get-field result 'value)
1119 (bindat-get-field result 'has_more)
1120 gdb-frame-address)))
1121 (push var gdb-var-list)
1122 (speedbar 1)
1123 (unless (string-equal
1124 speedbar-initial-expansion-list-name "GUD")
1125 (speedbar-change-initial-expansion-list "GUD")))
1126 (message-box "No symbol \"%s\" in current context." expr))))
1128 (defun gdb-speedbar-update ()
1129 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1130 (not (gdb-pending-p 'gdb-speedbar-timer)))
1131 ;; Dummy command to update speedbar even when idle.
1132 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn)
1133 ;; Keep gdb-pending-triggers non-nil till end.
1134 (gdb-add-pending 'gdb-speedbar-timer)))
1136 (defun gdb-speedbar-timer-fn ()
1137 (if gdb-speedbar-auto-raise
1138 (raise-frame speedbar-frame))
1139 (gdb-delete-pending 'gdb-speedbar-timer)
1140 (speedbar-timer-fn))
1142 (defun gdb-var-evaluate-expression-handler (varnum changed)
1143 (goto-char (point-min))
1144 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1145 (let ((var (assoc varnum gdb-var-list)))
1146 (when var
1147 (if changed (setcar (nthcdr 5 var) 'changed))
1148 (setcar (nthcdr 4 var) (read (match-string 1)))))
1149 (gdb-speedbar-update))
1151 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1152 (defun gdb-var-list-children (varnum)
1153 (gdb-input (concat "-var-update " varnum) 'ignore)
1154 (gdb-input (concat "-var-list-children --all-values " varnum)
1155 `(lambda () (gdb-var-list-children-handler ,varnum))))
1157 (defun gdb-var-list-children-handler (varnum)
1158 (let* ((var-list nil)
1159 (output (bindat-get-field (gdb-json-partial-output "child")))
1160 (children (bindat-get-field output 'children)))
1161 (catch 'child-already-watched
1162 (dolist (var gdb-var-list)
1163 (if (string-equal varnum (car var))
1164 (progn
1165 ;; With dynamic varobjs numchild may have increased.
1166 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1167 (push var var-list)
1168 (dolist (child children)
1169 (let ((varchild (list (bindat-get-field child 'name)
1170 (bindat-get-field child 'exp)
1171 (bindat-get-field child 'numchild)
1172 (bindat-get-field child 'type)
1173 (bindat-get-field child 'value)
1175 (bindat-get-field child 'has_more))))
1176 (if (assoc (car varchild) gdb-var-list)
1177 (throw 'child-already-watched nil))
1178 (push varchild var-list))))
1179 (push var var-list)))
1180 (setq gdb-var-list (nreverse var-list))))
1181 (gdb-speedbar-update))
1183 (defun gdb-var-set-format (format)
1184 "Set the output format for a variable displayed in the speedbar."
1185 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1186 (varnum (car var)))
1187 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1188 (gdb-var-update)))
1190 (defun gdb-var-delete-1 (var varnum)
1191 (gdb-input (concat "-var-delete " varnum) 'ignore)
1192 (setq gdb-var-list (delq var gdb-var-list))
1193 (dolist (varchild gdb-var-list)
1194 (if (string-match (concat (car var) "\\.") (car varchild))
1195 (setq gdb-var-list (delq varchild gdb-var-list)))))
1197 (defun gdb-var-delete ()
1198 "Delete watch expression at point from the speedbar."
1199 (interactive)
1200 (let ((text (speedbar-line-text)))
1201 (string-match "\\(\\S-+\\)" text)
1202 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1203 (varnum (car var)))
1204 (if (string-match "\\." (car var))
1205 (message-box "Can only delete a root expression")
1206 (gdb-var-delete-1 var varnum)))))
1208 (defun gdb-var-delete-children (varnum)
1209 "Delete children of variable object at point from the speedbar."
1210 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1212 (defun gdb-edit-value (_text _token _indent)
1213 "Assign a value to a variable displayed in the speedbar."
1214 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1215 (varnum (car var)) (value))
1216 (setq value (read-string "New value: "))
1217 (gdb-input (concat "-var-assign " varnum " " value)
1218 `(lambda () (gdb-edit-value-handler ,value)))))
1220 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1222 (defun gdb-edit-value-handler (value)
1223 (goto-char (point-min))
1224 (if (re-search-forward gdb-error-regexp nil t)
1225 (message-box "Invalid number or expression (%s)" value)))
1227 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1228 (defun gdb-var-update ()
1229 (if (not (gdb-pending-p 'gdb-var-update))
1230 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler))
1231 (gdb-add-pending 'gdb-var-update))
1233 (defun gdb-var-update-handler ()
1234 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1235 (dolist (var gdb-var-list)
1236 (setcar (nthcdr 5 var) nil))
1237 (let ((temp-var-list gdb-var-list))
1238 (dolist (change changelist)
1239 (let* ((varnum (bindat-get-field change 'name))
1240 (var (assoc varnum gdb-var-list))
1241 (new-num (bindat-get-field change 'new_num_children)))
1242 (when var
1243 (let ((scope (bindat-get-field change 'in_scope))
1244 (has-more (bindat-get-field change 'has_more)))
1245 (cond ((string-equal scope "false")
1246 (if gdb-delete-out-of-scope
1247 (gdb-var-delete-1 var varnum)
1248 (setcar (nthcdr 5 var) 'out-of-scope)))
1249 ((string-equal scope "true")
1250 (setcar (nthcdr 6 var) has-more)
1251 (when (and (or (not has-more)
1252 (string-equal has-more "0"))
1253 (not new-num)
1254 (string-equal (nth 2 var) "0"))
1255 (setcar (nthcdr 4 var)
1256 (bindat-get-field change 'value))
1257 (setcar (nthcdr 5 var) 'changed)))
1258 ((string-equal scope "invalid")
1259 (gdb-var-delete-1 var varnum)))))
1260 (let ((var-list nil) var1
1261 (children (bindat-get-field change 'new_children)))
1262 (when new-num
1263 (setq var1 (pop temp-var-list))
1264 (while var1
1265 (if (string-equal varnum (car var1))
1266 (let ((new (string-to-number new-num))
1267 (previous (string-to-number (nth 2 var1))))
1268 (setcar (nthcdr 2 var1) new-num)
1269 (push var1 var-list)
1270 (cond
1271 ((> new previous)
1272 ;; Add new children to list.
1273 (dotimes (dummy previous)
1274 (push (pop temp-var-list) var-list))
1275 (dolist (child children)
1276 (let ((varchild
1277 (list (bindat-get-field child 'name)
1278 (bindat-get-field child 'exp)
1279 (bindat-get-field child 'numchild)
1280 (bindat-get-field child 'type)
1281 (bindat-get-field child 'value)
1282 'changed
1283 (bindat-get-field child 'has_more))))
1284 (push varchild var-list))))
1285 ;; Remove deleted children from list.
1286 ((< new previous)
1287 (dotimes (dummy new)
1288 (push (pop temp-var-list) var-list))
1289 (dotimes (dummy (- previous new))
1290 (pop temp-var-list)))))
1291 (push var1 var-list))
1292 (setq var1 (pop temp-var-list)))
1293 (setq gdb-var-list (nreverse var-list))))))))
1294 (setq gdb-pending-triggers
1295 (delq 'gdb-var-update gdb-pending-triggers))
1296 (gdb-speedbar-update))
1298 (defun gdb-speedbar-expand-node (text token indent)
1299 "Expand the node the user clicked on.
1300 TEXT is the text of the button we clicked on, a + or - item.
1301 TOKEN is data related to this node.
1302 INDENT is the current indentation depth."
1303 (cond ((string-match "+" text) ;expand this node
1304 (let* ((var (assoc token gdb-var-list))
1305 (expr (nth 1 var)) (children (nth 2 var)))
1306 (if (or (<= (string-to-number children) gdb-max-children)
1307 (y-or-n-p
1308 (format "%s has %s children. Continue? " expr children)))
1309 (gdb-var-list-children token))))
1310 ((string-match "-" text) ;contract this node
1311 (dolist (var gdb-var-list)
1312 (if (string-match (concat token "\\.") (car var))
1313 (setq gdb-var-list (delq var gdb-var-list))))
1314 (gdb-var-delete-children token)
1315 (speedbar-change-expand-button-char ?+)
1316 (speedbar-delete-subblock indent))
1317 (t (error "Ooops... not sure what to do")))
1318 (speedbar-center-buffer-smartly))
1320 (defun gdb-get-target-string ()
1321 (with-current-buffer gud-comint-buffer
1322 gud-target-name))
1326 ;; gdb buffers.
1328 ;; Each buffer has a TYPE -- a symbol that identifies the function
1329 ;; of that particular buffer.
1331 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1332 ;; is constructed specially.
1334 ;; Others are constructed by gdb-get-buffer-create and
1335 ;; named according to the rules set forth in the gdb-buffer-rules
1337 (defvar gdb-buffer-rules '())
1339 (defun gdb-rules-name-maker (rules-entry)
1340 (cadr rules-entry))
1341 (defun gdb-rules-buffer-mode (rules-entry)
1342 (nth 2 rules-entry))
1343 (defun gdb-rules-update-trigger (rules-entry)
1344 (nth 3 rules-entry))
1346 (defun gdb-update-buffer-name ()
1347 "Rename current buffer according to name-maker associated with
1348 it in `gdb-buffer-rules'."
1349 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1350 gdb-buffer-rules))))
1351 (when f (rename-buffer (funcall f)))))
1353 (defun gdb-current-buffer-rules ()
1354 "Get `gdb-buffer-rules' entry for current buffer type."
1355 (assoc gdb-buffer-type gdb-buffer-rules))
1357 (defun gdb-current-buffer-thread ()
1358 "Get thread object of current buffer from `gdb-threads-list'.
1360 When current buffer is not bound to any thread, return main
1361 thread."
1362 (cdr (assoc gdb-thread-number gdb-threads-list)))
1364 (defun gdb-current-buffer-frame ()
1365 "Get current stack frame object for thread of current buffer."
1366 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1368 (defun gdb-buffer-type (buffer)
1369 "Get value of `gdb-buffer-type' for BUFFER."
1370 (with-current-buffer buffer
1371 gdb-buffer-type))
1373 (defun gdb-buffer-shows-main-thread-p ()
1374 "Return t if current GDB buffer shows main selected thread and
1375 is not bound to it."
1376 (current-buffer)
1377 (not (local-variable-p 'gdb-thread-number)))
1379 (defun gdb-get-buffer (buffer-type &optional thread)
1380 "Get a specific GDB buffer.
1382 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1383 and `gdb-thread-number' (if provided) must be equal to THREAD."
1384 (catch 'found
1385 (dolist (buffer (buffer-list) nil)
1386 (with-current-buffer buffer
1387 (when (and (eq gdb-buffer-type buffer-type)
1388 (or (not thread)
1389 (equal gdb-thread-number thread)))
1390 (throw 'found buffer))))))
1392 (defun gdb-get-buffer-create (buffer-type &optional thread)
1393 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1394 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1396 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1397 buffer-local variable of the new buffer.
1399 Buffer mode and name are selected according to buffer type.
1401 If buffer has trigger associated with it in `gdb-buffer-rules',
1402 this trigger is subscribed to `gdb-buf-publisher' and called with
1403 'update argument."
1404 (or (gdb-get-buffer buffer-type thread)
1405 (let ((rules (assoc buffer-type gdb-buffer-rules))
1406 (new (generate-new-buffer "limbo")))
1407 (with-current-buffer new
1408 (let ((mode (gdb-rules-buffer-mode rules))
1409 (trigger (gdb-rules-update-trigger rules)))
1410 (when mode (funcall mode))
1411 (setq gdb-buffer-type buffer-type)
1412 (when thread
1413 (set (make-local-variable 'gdb-thread-number) thread))
1414 (set (make-local-variable 'gud-minor-mode)
1415 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1416 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1417 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1418 (when trigger
1419 (gdb-add-subscriber gdb-buf-publisher
1420 (cons (current-buffer)
1421 (gdb-bind-function-to-buffer
1422 trigger (current-buffer))))
1423 (funcall trigger 'start))
1424 (current-buffer))))))
1426 (defun gdb-bind-function-to-buffer (expr buffer)
1427 "Return a function which will evaluate EXPR in BUFFER."
1428 `(lambda (&rest args)
1429 (with-current-buffer ,buffer
1430 (apply ',expr args))))
1432 ;; Used to define all gdb-frame-*-buffer functions except
1433 ;; `gdb-frame-io-buffer'
1434 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1435 "Define a function NAME which shows gdb BUFFER in a separate frame.
1437 DOC is an optional documentation string."
1438 `(defun ,name (&optional thread)
1439 ,(when doc doc)
1440 (interactive)
1441 (let ((special-display-regexps (append special-display-regexps '(".*")))
1442 (special-display-frame-alist gdb-frame-parameters))
1443 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1445 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1446 "Define a function NAME which shows gdb BUFFER.
1448 DOC is an optional documentation string."
1449 `(defun ,name (&optional thread)
1450 ,(when doc doc)
1451 (interactive)
1452 (gdb-display-buffer
1453 (gdb-get-buffer-create ,buffer thread) t)))
1455 ;; Used to display windows with thread-bound buffers
1456 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1457 split-horizontal)
1458 `(defun ,name (&optional thread)
1459 ,(when doc doc)
1460 (message thread)
1461 (gdb-preempt-existing-or-display-buffer
1462 (gdb-get-buffer-create ,buffer thread)
1463 ,split-horizontal)))
1465 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1466 ;; at least one and possible more functions. The functions have these
1467 ;; roles in defining a buffer type:
1469 ;; NAME - Return a name for this buffer type.
1471 ;; The remaining function(s) are optional:
1473 ;; MODE - called in a new buffer with no arguments, should establish
1474 ;; the proper mode for the buffer.
1477 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1478 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1479 (if binding
1480 (setcdr binding rules)
1481 (push (cons buffer-type rules)
1482 gdb-buffer-rules))))
1484 (defun gdb-parent-mode ()
1485 "Generic mode to derive all other GDB buffer modes from."
1486 (kill-all-local-variables)
1487 (setq buffer-read-only t)
1488 (buffer-disable-undo)
1489 ;; Delete buffer from gdb-buf-publisher when it's killed
1490 ;; (if it has an associated update trigger)
1491 (add-hook
1492 'kill-buffer-hook
1493 (function
1494 (lambda ()
1495 (let ((trigger (gdb-rules-update-trigger
1496 (gdb-current-buffer-rules))))
1497 (when trigger
1498 (gdb-delete-subscriber
1499 gdb-buf-publisher
1500 ;; This should match gdb-add-subscriber done in
1501 ;; gdb-get-buffer-create
1502 (cons (current-buffer)
1503 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1504 nil t))
1506 ;; Partial-output buffer : This accumulates output from a command executed on
1507 ;; behalf of emacs (rather than the user).
1509 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1510 'gdb-partial-output-name)
1512 (defun gdb-partial-output-name ()
1513 (concat " *partial-output-"
1514 (gdb-get-target-string)
1515 "*"))
1518 (gdb-set-buffer-rules 'gdb-inferior-io
1519 'gdb-inferior-io-name
1520 'gdb-inferior-io-mode)
1522 (defun gdb-inferior-io-name ()
1523 (concat "*input/output of "
1524 (gdb-get-target-string)
1525 "*"))
1527 (defun gdb-display-io-buffer ()
1528 "Display IO of debugged program in a separate window."
1529 (interactive)
1530 (gdb-display-buffer
1531 (gdb-get-buffer-create 'gdb-inferior-io) t))
1533 (defun gdb-inferior-io--init-proc (proc)
1534 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1535 (set-process-filter proc 'gdb-inferior-filter)
1536 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1537 (gdb-input
1538 (concat "-inferior-tty-set "
1539 ;; The process can run on a remote host.
1540 (or (process-get proc 'remote-tty)
1541 (process-tty-name proc)))
1542 'ignore))
1544 (defun gdb-inferior-io-sentinel (proc str)
1545 (when (eq (process-status proc) 'failed)
1546 ;; When the debugged process exits, Emacs gets an EIO error on
1547 ;; read from the pty, and stops listening to it. If the gdb
1548 ;; process is still running, remove the pty, make a new one, and
1549 ;; pass it to gdb.
1550 (let ((gdb-proc (get-buffer-process gud-comint-buffer))
1551 (io-buffer (process-buffer proc)))
1552 (when (and gdb-proc (process-live-p gdb-proc)
1553 (buffer-live-p io-buffer))
1554 ;; `comint-exec' deletes the original process as a side effect.
1555 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1556 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1558 (defconst gdb-frame-parameters
1559 '((height . 14) (width . 80)
1560 (unsplittable . t)
1561 (tool-bar-lines . nil)
1562 (menu-bar-lines . nil)
1563 (minibuffer . nil)))
1565 (defun gdb-frame-io-buffer ()
1566 "Display IO of debugged program in a new frame."
1567 (interactive)
1568 (let ((special-display-regexps (append special-display-regexps '(".*")))
1569 (special-display-frame-alist gdb-frame-parameters))
1570 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1572 (defvar gdb-inferior-io-mode-map
1573 (let ((map (make-sparse-keymap)))
1574 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1575 (define-key map "\C-c\C-z" 'gdb-io-stop)
1576 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1577 (define-key map "\C-c\C-d" 'gdb-io-eof)
1578 (define-key map "\C-d" 'gdb-io-eof)
1579 map))
1581 ;; We want to use comint because it has various nifty and familiar features.
1582 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1583 "Major mode for gdb inferior-io."
1584 :syntax-table nil :abbrev-table nil
1585 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1587 (defun gdb-inferior-filter (proc string)
1588 (unless (string-equal string "")
1589 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1590 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1591 (comint-output-filter proc string)))
1593 (defun gdb-io-interrupt ()
1594 "Interrupt the program being debugged."
1595 (interactive)
1596 (interrupt-process
1597 (get-buffer-process gud-comint-buffer) comint-ptyp))
1599 (defun gdb-io-quit ()
1600 "Send quit signal to the program being debugged."
1601 (interactive)
1602 (quit-process
1603 (get-buffer-process gud-comint-buffer) comint-ptyp))
1605 (defun gdb-io-stop ()
1606 "Stop the program being debugged."
1607 (interactive)
1608 (stop-process
1609 (get-buffer-process gud-comint-buffer) comint-ptyp))
1611 (defun gdb-io-eof ()
1612 "Send end-of-file to the program being debugged."
1613 (interactive)
1614 (process-send-eof
1615 (get-buffer-process gud-comint-buffer)))
1617 (defun gdb-clear-inferior-io ()
1618 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1619 (erase-buffer)))
1622 (defconst breakpoint-xpm-data
1623 "/* XPM */
1624 static char *magick[] = {
1625 /* columns rows colors chars-per-pixel */
1626 \"10 10 2 1\",
1627 \" c red\",
1628 \"+ c None\",
1629 /* pixels */
1630 \"+++ +++\",
1631 \"++ ++\",
1632 \"+ +\",
1633 \" \",
1634 \" \",
1635 \" \",
1636 \" \",
1637 \"+ +\",
1638 \"++ ++\",
1639 \"+++ +++\",
1641 "XPM data used for breakpoint icon.")
1643 (defconst breakpoint-enabled-pbm-data
1645 10 10\",
1646 0 0 0 0 1 1 1 1 0 0 0 0
1647 0 0 0 1 1 1 1 1 1 0 0 0
1648 0 0 1 1 1 1 1 1 1 1 0 0
1649 0 1 1 1 1 1 1 1 1 1 1 0
1650 0 1 1 1 1 1 1 1 1 1 1 0
1651 0 1 1 1 1 1 1 1 1 1 1 0
1652 0 1 1 1 1 1 1 1 1 1 1 0
1653 0 0 1 1 1 1 1 1 1 1 0 0
1654 0 0 0 1 1 1 1 1 1 0 0 0
1655 0 0 0 0 1 1 1 1 0 0 0 0"
1656 "PBM data used for enabled breakpoint icon.")
1658 (defconst breakpoint-disabled-pbm-data
1660 10 10\",
1661 0 0 1 0 1 0 1 0 0 0
1662 0 1 0 1 0 1 0 1 0 0
1663 1 0 1 0 1 0 1 0 1 0
1664 0 1 0 1 0 1 0 1 0 1
1665 1 0 1 0 1 0 1 0 1 0
1666 0 1 0 1 0 1 0 1 0 1
1667 1 0 1 0 1 0 1 0 1 0
1668 0 1 0 1 0 1 0 1 0 1
1669 0 0 1 0 1 0 1 0 1 0
1670 0 0 0 1 0 1 0 1 0 0"
1671 "PBM data used for disabled breakpoint icon.")
1673 (defvar breakpoint-enabled-icon nil
1674 "Icon for enabled breakpoint in display margin.")
1676 (defvar breakpoint-disabled-icon nil
1677 "Icon for disabled breakpoint in display margin.")
1679 (declare-function define-fringe-bitmap "fringe.c"
1680 (bitmap bits &optional height width align))
1682 (and (display-images-p)
1683 ;; Bitmap for breakpoint in fringe
1684 (define-fringe-bitmap 'breakpoint
1685 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1686 ;; Bitmap for gud-overlay-arrow in fringe
1687 (define-fringe-bitmap 'hollow-right-triangle
1688 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1690 (defface breakpoint-enabled
1691 '((t
1692 :foreground "red1"
1693 :weight bold))
1694 "Face for enabled breakpoint icon in fringe."
1695 :group 'gdb)
1697 (defface breakpoint-disabled
1698 '((((class color) (min-colors 88)) :foreground "grey70")
1699 ;; Ensure that on low-color displays that we end up something visible.
1700 (((class color) (min-colors 8) (background light))
1701 :foreground "black")
1702 (((class color) (min-colors 8) (background dark))
1703 :foreground "white")
1704 (((type tty) (class mono))
1705 :inverse-video t)
1706 (t :background "gray"))
1707 "Face for disabled breakpoint icon in fringe."
1708 :group 'gdb)
1711 (defvar gdb-control-commands-regexp
1712 (concat
1713 "^\\("
1714 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1715 "while-stepping\\|stepping\\|ws\\|actions"
1716 "\\)\\([[:blank:]]+.*\\)?$")
1717 "Regexp matching GDB commands that enter a recursive reading loop.
1718 As long as GDB is in the recursive reading loop, it does not expect
1719 commands to be prefixed by \"-interpreter-exec console\".")
1721 (defun gdb-send (proc string)
1722 "A comint send filter for gdb."
1723 (with-current-buffer gud-comint-buffer
1724 (let ((inhibit-read-only t))
1725 (remove-text-properties (point-min) (point-max) '(face))))
1726 ;; mimic <RET> key to repeat previous command in GDB
1727 (if (not (string= "" string))
1728 (setq gdb-last-command string)
1729 (if gdb-last-command (setq string gdb-last-command)))
1730 (if (or (string-match "^-" string)
1731 (> gdb-control-level 0))
1732 ;; Either MI command or we are feeding GDB's recursive reading loop.
1733 (progn
1734 (setq gdb-first-done-or-error t)
1735 (process-send-string proc (concat string "\n"))
1736 (if (and (string-match "^end$" string)
1737 (> gdb-control-level 0))
1738 (setq gdb-control-level (1- gdb-control-level))))
1739 ;; CLI command
1740 (if (string-match "\\\\$" string)
1741 (setq gdb-continuation (concat gdb-continuation string "\n"))
1742 (setq gdb-first-done-or-error t)
1743 (let ((to-send (concat "-interpreter-exec console "
1744 (gdb-mi-quote string)
1745 "\n")))
1746 (if gdb-enable-debug
1747 (push (cons 'mi-send to-send) gdb-debug-log))
1748 (process-send-string proc to-send))
1749 (if (and (string-match "^end$" string)
1750 (> gdb-control-level 0))
1751 (setq gdb-control-level (1- gdb-control-level)))
1752 (setq gdb-continuation nil)))
1753 (if (string-match gdb-control-commands-regexp string)
1754 (setq gdb-control-level (1+ gdb-control-level))))
1756 (defun gdb-mi-quote (string)
1757 "Return STRING quoted properly as an MI argument.
1758 The string is enclosed in double quotes.
1759 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1760 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1761 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1762 (concat "\"" string "\""))
1764 (defun gdb-input (command handler-function)
1765 "Send COMMAND to GDB via the MI interface.
1766 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1767 complete."
1768 (if gdb-enable-debug (push (list 'send-item command handler-function)
1769 gdb-debug-log))
1770 (setq gdb-token-number (1+ gdb-token-number))
1771 (setq command (concat (number-to-string gdb-token-number) command))
1772 (push (cons gdb-token-number handler-function) gdb-handler-alist)
1773 (process-send-string (get-buffer-process gud-comint-buffer)
1774 (concat command "\n")))
1776 ;; NOFRAME is used for gud execution control commands
1777 (defun gdb-current-context-command (command)
1778 "Add --thread to gdb COMMAND when needed."
1779 (if (and gdb-thread-number
1780 gdb-supports-non-stop)
1781 (concat command " --thread " gdb-thread-number)
1782 command))
1784 (defun gdb-current-context-buffer-name (name)
1785 "Add thread information and asterisks to string NAME.
1787 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1788 (concat "*" name
1789 (if (local-variable-p 'gdb-thread-number)
1790 (format " (bound to thread %s)" gdb-thread-number)
1792 "*"))
1794 (defun gdb-current-context-mode-name (mode)
1795 "Add thread information to MODE which is to be used as
1796 `mode-name'."
1797 (concat mode
1798 (if gdb-thread-number
1799 (format " [thread %s]" gdb-thread-number)
1800 "")))
1803 (defcustom gud-gdb-command-name "gdb -i=mi"
1804 "Default command to execute an executable under the GDB debugger."
1805 :type 'string
1806 :group 'gdb)
1808 (defun gdb-resync()
1809 (setq gud-running nil)
1810 (setq gdb-output-sink 'user)
1811 (setq gdb-pending-triggers nil))
1813 (defun gdb-update (&optional no-proc)
1814 "Update buffers showing status of debug session.
1815 If NO-PROC is non-nil, do not try to contact the GDB process."
1816 (when gdb-first-prompt
1817 (gdb-force-mode-line-update
1818 (propertize "initializing..." 'face font-lock-variable-name-face))
1819 (gdb-init-1)
1820 (setq gdb-first-prompt nil))
1822 (unless no-proc
1823 (gdb-get-main-selected-frame))
1825 ;; We may need to update gdb-threads-list so we can use
1826 (gdb-get-buffer-create 'gdb-threads-buffer)
1827 ;; gdb-break-list is maintained in breakpoints handler
1828 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1830 (unless no-proc
1831 (gdb-emit-signal gdb-buf-publisher 'update))
1833 (gdb-get-changed-registers)
1834 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1835 (dolist (var gdb-var-list)
1836 (setcar (nthcdr 5 var) nil))
1837 (gdb-var-update)))
1839 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1840 ;; because we may need to update current gud-running value without
1841 ;; changing current thread (see gdb-running)
1842 (defun gdb-setq-thread-number (number)
1843 "Only this function must be used to change `gdb-thread-number'
1844 value to NUMBER, because `gud-running' and `gdb-frame-number'
1845 need to be updated appropriately when current thread changes."
1846 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1847 (unless (string-equal number "0") (setq gdb-thread-number number))
1848 (setq gdb-frame-number "0")
1849 (gdb-update-gud-running))
1851 (defun gdb-update-gud-running ()
1852 "Set `gud-running' according to the state of current thread.
1854 `gdb-frame-number' is set to 0 if current thread is now stopped.
1856 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1857 cannot be reliably used to determine whether or not execution
1858 control buttons should be shown in menu or toolbar. Use
1859 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1860 instead.
1862 For all-stop mode, thread information is unavailable while target
1863 is running."
1864 (let ((old-value gud-running))
1865 (setq gud-running
1866 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1867 "running"))
1868 ;; Set frame number to "0" when _current_ threads stops
1869 (when (and (gdb-current-buffer-thread)
1870 (not (eq gud-running old-value)))
1871 (setq gdb-frame-number "0"))))
1873 (defun gdb-show-run-p ()
1874 "Return t if \"Run/continue\" should be shown on the toolbar."
1875 (or (not gdb-active-process)
1876 (and (or
1877 (not gdb-gud-control-all-threads)
1878 (not gdb-non-stop))
1879 (not gud-running))
1880 (and gdb-gud-control-all-threads
1881 (> gdb-stopped-threads-count 0))))
1883 (defun gdb-show-stop-p ()
1884 "Return t if \"Stop\" should be shown on the toolbar."
1885 (or (and (or
1886 (not gdb-gud-control-all-threads)
1887 (not gdb-non-stop))
1888 gud-running)
1889 (and gdb-gud-control-all-threads
1890 (> gdb-running-threads-count 0))))
1892 ;; GUD displays the selected GDB frame. This might might not be the current
1893 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1894 ;; visited breakpoint is, use that window.
1895 (defun gdb-display-source-buffer (buffer)
1896 (let* ((last-window (if gud-last-last-frame
1897 (get-buffer-window
1898 (gud-find-file (car gud-last-last-frame)))))
1899 (source-window (or last-window
1900 (if (and gdb-source-window
1901 (window-live-p gdb-source-window))
1902 gdb-source-window))))
1903 (when source-window
1904 (setq gdb-source-window source-window)
1905 (set-window-buffer source-window buffer))
1906 source-window))
1908 (defun gdb-car< (a b)
1909 (< (car a) (car b)))
1911 (defvar gdbmi-record-list
1912 '((gdb-gdb . "(gdb) \n")
1913 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1914 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1915 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1916 (gdb-console . "~\\(\".*?\"\\)\n")
1917 (gdb-internals . "&\\(\".*?\"\\)\n")
1918 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1919 (gdb-running . "\\*running,\\(.*?\n\\)")
1920 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1921 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1922 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1923 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1924 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1926 (defun gud-gdbmi-marker-filter (string)
1927 "Filter GDB/MI output."
1929 ;; Record transactions if logging is enabled.
1930 (when gdb-enable-debug
1931 (push (cons 'recv string) gdb-debug-log)
1932 (if (and gdb-debug-log-max
1933 (> (length gdb-debug-log) gdb-debug-log-max))
1934 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1936 ;; Recall the left over gud-marker-acc from last time
1937 (setq gud-marker-acc (concat gud-marker-acc string))
1939 ;; Start accumulating output for the GUD buffer
1940 (setq gdb-filter-output "")
1941 (let (output-record-list)
1943 ;; Process all the complete markers in this chunk.
1944 (dolist (gdbmi-record gdbmi-record-list)
1945 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1946 (push (list (match-beginning 0)
1947 (car gdbmi-record)
1948 (match-string 1 gud-marker-acc)
1949 (match-string 2 gud-marker-acc)
1950 (match-end 0))
1951 output-record-list)
1952 (setq gud-marker-acc
1953 (concat (substring gud-marker-acc 0 (match-beginning 0))
1954 ;; Pad with spaces to preserve position.
1955 (make-string (length (match-string 0 gud-marker-acc)) 32)
1956 (substring gud-marker-acc (match-end 0))))))
1958 (setq output-record-list (sort output-record-list 'gdb-car<))
1960 (dolist (output-record output-record-list)
1961 (let ((record-type (cadr output-record))
1962 (arg1 (nth 2 output-record))
1963 (arg2 (nth 3 output-record)))
1964 (cond ((eq record-type 'gdb-error)
1965 (gdb-done-or-error arg2 arg1 'error))
1966 ((eq record-type 'gdb-done)
1967 (gdb-done-or-error arg2 arg1 'done))
1968 ;; Suppress "No registers." GDB 6.8 and earlier
1969 ;; duplicates MI error message on internal stream.
1970 ;; Don't print to GUD buffer.
1971 ((not (and (eq record-type 'gdb-internals)
1972 (string-equal (read arg1) "No registers.\n")))
1973 (funcall record-type arg1)))))
1975 (setq gdb-output-sink 'user)
1976 ;; Remove padding.
1977 (string-match "^ *" gud-marker-acc)
1978 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1980 gdb-filter-output))
1982 (defun gdb-gdb (_output-field))
1984 (defun gdb-shell (output-field)
1985 (let ((gdb-output-sink gdb-output-sink))
1986 (setq gdb-filter-output
1987 (concat output-field gdb-filter-output))))
1989 (defun gdb-ignored-notification (_output-field))
1991 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1992 (defun gdb-thread-created (_output-field))
1993 (defun gdb-thread-exited (output-field)
1994 "Handle =thread-exited async record: unset `gdb-thread-number'
1995 if current thread exited and update threads list."
1996 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1997 (if (string= gdb-thread-number thread-id)
1998 (gdb-setq-thread-number nil))
1999 ;; When we continue current thread and it quickly exits,
2000 ;; gdb-pending-triggers left after gdb-running disallow us to
2001 ;; properly call -thread-info without --thread option. Thus we
2002 ;; need to use gdb-wait-for-pending.
2003 (gdb-wait-for-pending
2004 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
2006 (defun gdb-thread-selected (output-field)
2007 "Handler for =thread-selected MI output record.
2009 Sets `gdb-thread-number' to new id."
2010 (let* ((result (gdb-json-string output-field))
2011 (thread-id (bindat-get-field result 'id)))
2012 (gdb-setq-thread-number thread-id)
2013 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
2014 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
2015 ;; as usually. Things happen to fast and second call (from
2016 ;; gdb-thread-selected handler) gets cut off by our beloved
2017 ;; gdb-pending-triggers.
2018 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
2019 ;; body will get executed when `gdb-pending-triggers` is empty.
2020 (gdb-wait-for-pending
2021 (gdb-update))))
2023 (defun gdb-running (output-field)
2024 (let* ((thread-id
2025 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
2026 ;; We reset gdb-frame-number to nil if current thread has gone
2027 ;; running. This can't be done in gdb-thread-list-handler-custom
2028 ;; because we need correct gdb-frame-number by the time
2029 ;; -thread-info command is sent.
2030 (when (or (string-equal thread-id "all")
2031 (string-equal thread-id gdb-thread-number))
2032 (setq gdb-frame-number nil)))
2033 (setq gdb-inferior-status "running")
2034 (gdb-force-mode-line-update
2035 (propertize gdb-inferior-status 'face font-lock-type-face))
2036 (when (not gdb-non-stop)
2037 (setq gud-running t))
2038 (setq gdb-active-process t)
2039 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2041 (defun gdb-starting (_output-field)
2042 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2043 (setq gdb-inferior-status "running")
2044 (gdb-force-mode-line-update
2045 (propertize gdb-inferior-status 'face font-lock-type-face))
2046 (setq gdb-active-process t)
2047 (setq gud-running t)
2048 ;; GDB doesn't seem to respond to -thread-info before first stop or
2049 ;; thread exit (even in non-stop mode), so this is useless.
2050 ;; Behavior may change in the future.
2051 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2053 ;; -break-insert -t didn't give a reason before gdb 6.9
2055 (defun gdb-stopped (output-field)
2056 "Given the contents of *stopped MI async record, select new
2057 current thread and update GDB buffers."
2058 ;; Reason is available with target-async only
2059 (let* ((result (gdb-json-string output-field))
2060 (reason (bindat-get-field result 'reason))
2061 (thread-id (bindat-get-field result 'thread-id)))
2063 ;; -data-list-register-names needs to be issued for any stopped
2064 ;; thread
2065 (when (not gdb-register-names)
2066 (gdb-input (concat "-data-list-register-names"
2067 (if gdb-supports-non-stop
2068 (concat " --thread " thread-id)))
2069 'gdb-register-names-handler))
2071 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
2072 ;;; because synchronous GDB doesn't give these fields with CLI.
2073 ;;; (when file
2074 ;;; (setq
2075 ;;; ;; Extract the frame position from the marker.
2076 ;;; gud-last-frame (cons file
2077 ;;; (string-to-number
2078 ;;; (match-string 6 gud-marker-acc)))))
2080 (setq gdb-inferior-status (or reason "unknown"))
2081 (gdb-force-mode-line-update
2082 (propertize gdb-inferior-status 'face font-lock-warning-face))
2083 (if (string-equal reason "exited-normally")
2084 (setq gdb-active-process nil))
2086 ;; Select new current thread.
2088 ;; Don't switch if we have no reasons selected
2089 (when gdb-switch-reasons
2090 ;; Switch from another stopped thread only if we have
2091 ;; gdb-switch-when-another-stopped:
2092 (when (or gdb-switch-when-another-stopped
2093 (not (string= "stopped"
2094 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2095 ;; Switch if current reason has been selected or we have no
2096 ;; reasons
2097 (if (or (eq gdb-switch-reasons t)
2098 (member reason gdb-switch-reasons))
2099 (when (not (string-equal gdb-thread-number thread-id))
2100 (message (concat "Switched to thread " thread-id))
2101 (gdb-setq-thread-number thread-id))
2102 (message (format "Thread %s stopped" thread-id)))))
2104 ;; Print "(gdb)" to GUD console
2105 (when gdb-first-done-or-error
2106 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2108 ;; In non-stop, we update information as soon as another thread gets
2109 ;; stopped
2110 (when (or gdb-first-done-or-error
2111 gdb-non-stop)
2112 ;; In all-stop this updates gud-running properly as well.
2113 (gdb-update)
2114 (setq gdb-first-done-or-error nil))
2115 (run-hook-with-args 'gdb-stopped-functions result)))
2117 ;; Remove the trimmings from log stream containing debugging messages
2118 ;; being produced by GDB's internals, use warning face and send to GUD
2119 ;; buffer.
2120 (defun gdb-internals (output-field)
2121 (setq gdb-filter-output
2122 (gdb-concat-output
2123 gdb-filter-output
2124 (let ((error-message
2125 (read output-field)))
2126 (put-text-property
2127 0 (length error-message)
2128 'face font-lock-warning-face
2129 error-message)
2130 error-message))))
2132 ;; Remove the trimmings from the console stream and send to GUD buffer
2133 ;; (frontend MI commands should not print to this stream)
2134 (defun gdb-console (output-field)
2135 (setq gdb-filter-output
2136 (gdb-concat-output gdb-filter-output (read output-field))))
2138 (defun gdb-done-or-error (output-field token-number type)
2139 (if (string-equal token-number "")
2140 ;; Output from command entered by user
2141 (progn
2142 (setq gdb-output-sink 'user)
2143 (setq token-number nil)
2144 ;; MI error - send to minibuffer
2145 (when (eq type 'error)
2146 ;; Skip "msg=" from `output-field'
2147 (message (read (substring output-field 4)))
2148 ;; Don't send to the console twice. (If it is a console error
2149 ;; it is also in the console stream.)
2150 (setq output-field nil)))
2151 ;; Output from command from frontend.
2152 (setq gdb-output-sink 'emacs))
2154 (gdb-clear-partial-output)
2156 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2157 (let* ((proc (get-buffer-process gud-comint-buffer))
2158 (no-proc (or (null proc)
2159 (memq (process-status proc) '(exit signal)))))
2161 (when gdb-first-done-or-error
2162 (unless (or token-number gud-running no-proc)
2163 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2164 (gdb-update no-proc)
2165 (setq gdb-first-done-or-error nil))
2167 (setq gdb-filter-output
2168 (gdb-concat-output gdb-filter-output output-field))
2170 (when token-number
2171 (with-current-buffer
2172 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2173 (funcall
2174 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2175 (setq gdb-handler-alist
2176 (assq-delete-all token-number gdb-handler-alist)))))
2178 (defun gdb-concat-output (so-far new)
2179 (cond
2180 ((eq gdb-output-sink 'user) (concat so-far new))
2181 ((eq gdb-output-sink 'emacs)
2182 (gdb-append-to-partial-output new)
2183 so-far)))
2185 (defun gdb-append-to-partial-output (string)
2186 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2187 (goto-char (point-max))
2188 (insert string)))
2190 (defun gdb-clear-partial-output ()
2191 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2192 (erase-buffer)))
2194 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2195 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2197 Field names are wrapped in double quotes and equal signs are
2198 replaced with semicolons.
2200 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2201 partial output. This is used to get rid of useless keys in lists
2202 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2203 -break-info are examples of MI commands which issue such
2204 responses.
2206 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2207 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2208 -break-info output when it contains breakpoint script field
2209 incompatible with GDB/MI output syntax."
2210 (save-excursion
2211 (goto-char (point-min))
2212 (when fix-key
2213 (save-excursion
2214 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2215 (replace-match "" nil nil nil 1))))
2216 (when fix-list
2217 (save-excursion
2218 ;; Find positions of braces which enclose broken list
2219 (while (re-search-forward (concat fix-list "={\"") nil t)
2220 (let ((p1 (goto-char (- (point) 2)))
2221 (p2 (progn (forward-sexp)
2222 (1- (point)))))
2223 ;; Replace braces with brackets
2224 (save-excursion
2225 (goto-char p1)
2226 (delete-char 1)
2227 (insert "[")
2228 (goto-char p2)
2229 (delete-char 1)
2230 (insert "]"))))))
2231 (goto-char (point-min))
2232 (insert "{")
2233 (while (re-search-forward
2234 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2235 (replace-match "\"\\1\":\\2" nil nil))
2236 (goto-char (point-max))
2237 (insert "}")))
2239 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2240 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2242 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2243 (gdb-jsonify-buffer fix-key fix-list)
2244 (save-excursion
2245 (goto-char (point-min))
2246 (let ((json-array-type 'list))
2247 (json-read))))
2249 (defun gdb-json-string (string &optional fix-key fix-list)
2250 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2252 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2253 (with-temp-buffer
2254 (insert string)
2255 (gdb-json-read-buffer fix-key fix-list)))
2257 (defun gdb-json-partial-output (&optional fix-key fix-list)
2258 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2260 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2261 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2262 (gdb-json-read-buffer fix-key fix-list)))
2264 (defun gdb-line-posns (line)
2265 "Return a pair of LINE beginning and end positions."
2266 (let ((offset (1+ (- line (line-number-at-pos)))))
2267 (cons
2268 (line-beginning-position offset)
2269 (line-end-position offset))))
2271 (defmacro gdb-mark-line (line variable)
2272 "Set VARIABLE marker to point at beginning of LINE.
2274 If current window has no fringes, inverse colors on LINE.
2276 Return position where LINE begins."
2277 `(save-excursion
2278 (let* ((posns (gdb-line-posns ,line))
2279 (start-posn (car posns))
2280 (end-posn (cdr posns)))
2281 (set-marker ,variable (copy-marker start-posn))
2282 (when (not (> (car (window-fringes)) 0))
2283 (put-text-property start-posn end-posn
2284 'font-lock-face '(:inverse-video t)))
2285 start-posn)))
2287 (defun gdb-pad-string (string padding)
2288 (format (concat "%" (number-to-string padding) "s") string))
2290 ;; gdb-table struct is a way to programmatically construct simple
2291 ;; tables. It help to reliably align columns of data in GDB buffers
2292 ;; and provides
2293 (defstruct
2294 gdb-table
2295 (column-sizes nil)
2296 (rows nil)
2297 (row-properties nil)
2298 (right-align nil))
2300 (defun gdb-mapcar* (function &rest seqs)
2301 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2302 If there are several SEQS, FUNCTION is called with that many
2303 arguments, and mapping stops as soon as the shortest list runs
2304 out."
2305 (let ((shortest (apply #'min (mapcar #'length seqs))))
2306 (mapcar (lambda (i)
2307 (apply function
2308 (mapcar
2309 (lambda (seq)
2310 (nth i seq))
2311 seqs)))
2312 (number-sequence 0 (1- shortest)))))
2314 (defun gdb-table-add-row (table row &optional properties)
2315 "Add ROW of string to TABLE and recalculate column sizes.
2317 When non-nil, PROPERTIES will be added to the whole row when
2318 calling `gdb-table-string'."
2319 (let ((rows (gdb-table-rows table))
2320 (row-properties (gdb-table-row-properties table))
2321 (column-sizes (gdb-table-column-sizes table))
2322 (right-align (gdb-table-right-align table)))
2323 (when (not column-sizes)
2324 (setf (gdb-table-column-sizes table)
2325 (make-list (length row) 0)))
2326 (setf (gdb-table-rows table)
2327 (append rows (list row)))
2328 (setf (gdb-table-row-properties table)
2329 (append row-properties (list properties)))
2330 (setf (gdb-table-column-sizes table)
2331 (gdb-mapcar* (lambda (x s)
2332 (let ((new-x
2333 (max (abs x) (string-width (or s "")))))
2334 (if right-align new-x (- new-x))))
2335 (gdb-table-column-sizes table)
2336 row))
2337 ;; Avoid trailing whitespace at eol
2338 (if (not (gdb-table-right-align table))
2339 (setcar (last (gdb-table-column-sizes table)) 0))))
2341 (defun gdb-table-string (table &optional sep)
2342 "Return TABLE as a string with columns separated with SEP."
2343 (let ((column-sizes (gdb-table-column-sizes table)))
2344 (mapconcat
2345 'identity
2346 (gdb-mapcar*
2347 (lambda (row properties)
2348 (apply 'propertize
2349 (mapconcat 'identity
2350 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2351 row column-sizes)
2352 sep)
2353 properties))
2354 (gdb-table-rows table)
2355 (gdb-table-row-properties table))
2356 "\n")))
2358 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2359 (defun gdb-get-many-fields (struct &rest fields)
2360 "Return a list of FIELDS values from STRUCT."
2361 (let ((values))
2362 (dolist (field fields values)
2363 (setq values (append values (list (bindat-get-field struct field)))))))
2365 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2366 handler-name
2367 &optional signal-list)
2368 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2369 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2370 buffer with `gdb-bind-function-to-buffer'.
2372 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2373 defined trigger is called with an argument from SIGNAL-LIST. It's
2374 not recommended to define triggers with empty SIGNAL-LIST.
2375 Normally triggers should respond at least to 'update signal.
2377 Normally the trigger defined by this command must be called from
2378 the buffer where HANDLER-NAME must work. This should be done so
2379 that buffer-local thread number may be used in GDB-COMMAND (by
2380 calling `gdb-current-context-command').
2381 `gdb-bind-function-to-buffer' is used to achieve this, see
2382 `gdb-get-buffer-create'.
2384 Triggers defined by this command are meant to be used as a
2385 trigger argument when describing buffer types with
2386 `gdb-set-buffer-rules'."
2387 `(defun ,trigger-name (&optional signal)
2388 (when
2389 (or (not ,signal-list)
2390 (memq signal ,signal-list))
2391 (when (not (gdb-pending-p
2392 (cons (current-buffer) ',trigger-name)))
2393 (gdb-input ,gdb-command
2394 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2395 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2397 ;; Used by disassembly buffer only, the rest use
2398 ;; def-gdb-trigger-and-handler
2399 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2400 &optional nopreserve)
2401 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2403 Handlers are normally called from the buffers they put output in.
2405 Delete ((current-buffer) . TRIGGER-NAME) from
2406 `gdb-pending-triggers', erase current buffer and evaluate
2407 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2409 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2410 `(defun ,handler-name ()
2411 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2412 (let* ((buffer-read-only nil)
2413 (window (get-buffer-window (current-buffer) 0))
2414 (start (window-start window))
2415 (p (window-point window)))
2416 (erase-buffer)
2417 (,custom-defun)
2418 (gdb-update-buffer-name)
2419 ,(when (not nopreserve)
2420 '(set-window-start window start)
2421 '(set-window-point window p)))))
2423 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2424 handler-name custom-defun
2425 &optional signal-list)
2426 "Define trigger and handler.
2428 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2429 `def-gdb-auto-update-trigger'.
2431 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2432 `def-gdb-auto-update-handler'."
2433 `(progn
2434 (def-gdb-auto-update-trigger ,trigger-name
2435 ,gdb-command
2436 ,handler-name ,signal-list)
2437 (def-gdb-auto-update-handler ,handler-name
2438 ,trigger-name ,custom-defun)))
2442 ;; Breakpoint buffer : This displays the output of `-break-list'.
2443 (def-gdb-trigger-and-handler
2444 gdb-invalidate-breakpoints "-break-list"
2445 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2446 '(start update))
2448 (gdb-set-buffer-rules
2449 'gdb-breakpoints-buffer
2450 'gdb-breakpoints-buffer-name
2451 'gdb-breakpoints-mode
2452 'gdb-invalidate-breakpoints)
2454 (defun gdb-breakpoints-list-handler-custom ()
2455 (let ((breakpoints-list (bindat-get-field
2456 (gdb-json-partial-output "bkpt" "script")
2457 'BreakpointTable 'body))
2458 (table (make-gdb-table)))
2459 (setq gdb-breakpoints-list nil)
2460 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2461 (dolist (breakpoint breakpoints-list)
2462 (add-to-list 'gdb-breakpoints-list
2463 (cons (bindat-get-field breakpoint 'number)
2464 breakpoint))
2465 (let ((at (bindat-get-field breakpoint 'at))
2466 (pending (bindat-get-field breakpoint 'pending))
2467 (func (bindat-get-field breakpoint 'func))
2468 (type (bindat-get-field breakpoint 'type)))
2469 (gdb-table-add-row table
2470 (list
2471 (bindat-get-field breakpoint 'number)
2472 (or type "")
2473 (or (bindat-get-field breakpoint 'disp) "")
2474 (let ((flag (bindat-get-field breakpoint 'enabled)))
2475 (if (string-equal flag "y")
2476 (propertize "y" 'font-lock-face font-lock-warning-face)
2477 (propertize "n" 'font-lock-face font-lock-comment-face)))
2478 (bindat-get-field breakpoint 'addr)
2479 (or (bindat-get-field breakpoint 'times) "")
2480 (if (and type (string-match ".*watchpoint" type))
2481 (bindat-get-field breakpoint 'what)
2482 (or pending at
2483 (concat "in "
2484 (propertize (or func "unknown")
2485 'font-lock-face font-lock-function-name-face)
2486 (gdb-frame-location breakpoint)))))
2487 ;; Add clickable properties only for breakpoints with file:line
2488 ;; information
2489 (append (list 'gdb-breakpoint breakpoint)
2490 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2491 mouse-face highlight))))))
2492 (insert (gdb-table-string table " "))
2493 (gdb-place-breakpoints)))
2495 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2496 (defun gdb-place-breakpoints ()
2497 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2498 (dolist (buffer (buffer-list))
2499 (with-current-buffer buffer
2500 (if (and (eq gud-minor-mode 'gdbmi)
2501 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2502 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2503 (dolist (breakpoint gdb-breakpoints-list)
2504 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2505 ; an associative list
2506 (line (bindat-get-field breakpoint 'line)))
2507 (when line
2508 (let ((file (bindat-get-field breakpoint 'fullname))
2509 (flag (bindat-get-field breakpoint 'enabled))
2510 (bptno (bindat-get-field breakpoint 'number)))
2511 (unless (file-exists-p file)
2512 (setq file (cdr (assoc bptno gdb-location-alist))))
2513 (if (and file
2514 (not (string-equal file "File not found")))
2515 (with-current-buffer
2516 (find-file-noselect file 'nowarn)
2517 (gdb-init-buffer)
2518 ;; Only want one breakpoint icon at each location.
2519 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2520 (string-to-number line)))
2521 (gdb-input (concat "list " file ":1") 'ignore)
2522 (gdb-input "-file-list-exec-source-file"
2523 `(lambda () (gdb-get-location
2524 ,bptno ,line ,flag)))))))))
2526 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2528 (defun gdb-get-location (bptno line flag)
2529 "Find the directory containing the relevant source file.
2530 Put in buffer and place breakpoint icon."
2531 (goto-char (point-min))
2532 (catch 'file-not-found
2533 (if (re-search-forward gdb-source-file-regexp nil t)
2534 (delete (cons bptno "File not found") gdb-location-alist)
2535 (push (cons bptno (match-string 1)) gdb-location-alist)
2536 (gdb-resync)
2537 (unless (assoc bptno gdb-location-alist)
2538 (push (cons bptno "File not found") gdb-location-alist)
2539 (message-box "Cannot find source file for breakpoint location.
2540 Add directory to search path for source files using the GDB command, dir."))
2541 (throw 'file-not-found nil))
2542 (with-current-buffer (find-file-noselect (match-string 1))
2543 (gdb-init-buffer)
2544 ;; only want one breakpoint icon at each location
2545 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2547 (add-hook 'find-file-hook 'gdb-find-file-hook)
2549 (defun gdb-find-file-hook ()
2550 "Set up buffer for debugging if file is part of the source code
2551 of the current session."
2552 (if (and (buffer-name gud-comint-buffer)
2553 ;; in case gud or gdb-ui is just loaded
2554 gud-comint-buffer
2555 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2556 'gdbmi))
2557 (if (member buffer-file-name gdb-source-file-list)
2558 (with-current-buffer (find-buffer-visiting buffer-file-name)
2559 (gdb-init-buffer)))))
2561 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2562 (declare-function gud-break "gdb-mi" t t) ; gud-def
2563 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2565 (defun gdb-mouse-set-clear-breakpoint (event)
2566 "Set/clear breakpoint in left fringe/margin at mouse click.
2567 If not in a source or disassembly buffer just set point."
2568 (interactive "e")
2569 (mouse-minibuffer-check event)
2570 (let ((posn (event-end event)))
2571 (with-selected-window (posn-window posn)
2572 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2573 (if (numberp (posn-point posn))
2574 (save-excursion
2575 (goto-char (posn-point posn))
2576 (if (or (posn-object posn)
2577 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2578 'breakpoint))
2579 (gud-remove nil)
2580 (gud-break nil)))))
2581 (posn-set-point posn))))
2583 (defun gdb-mouse-toggle-breakpoint-margin (event)
2584 "Enable/disable breakpoint in left margin with mouse click."
2585 (interactive "e")
2586 (mouse-minibuffer-check event)
2587 (let ((posn (event-end event)))
2588 (if (numberp (posn-point posn))
2589 (with-selected-window (posn-window posn)
2590 (save-excursion
2591 (goto-char (posn-point posn))
2592 (if (posn-object posn)
2593 (gud-basic-call
2594 (let ((bptno (get-text-property
2595 0 'gdb-bptno (car (posn-string posn)))))
2596 (concat
2597 (if (get-text-property
2598 0 'gdb-enabled (car (posn-string posn)))
2599 "-break-disable "
2600 "-break-enable ")
2601 bptno)))))))))
2603 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2604 "Enable/disable breakpoint in left fringe with mouse click."
2605 (interactive "e")
2606 (mouse-minibuffer-check event)
2607 (let* ((posn (event-end event))
2608 (pos (posn-point posn))
2609 obj)
2610 (when (numberp pos)
2611 (with-selected-window (posn-window posn)
2612 (with-current-buffer (window-buffer (selected-window))
2613 (goto-char pos)
2614 (dolist (overlay (overlays-in pos pos))
2615 (when (overlay-get overlay 'put-break)
2616 (setq obj (overlay-get overlay 'before-string))))
2617 (when (stringp obj)
2618 (gud-basic-call
2619 (concat
2620 (if (get-text-property 0 'gdb-enabled obj)
2621 "-break-disable "
2622 "-break-enable ")
2623 (get-text-property 0 'gdb-bptno obj)))))))))
2625 (defun gdb-breakpoints-buffer-name ()
2626 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2628 (def-gdb-display-buffer
2629 gdb-display-breakpoints-buffer
2630 'gdb-breakpoints-buffer
2631 "Display status of user-settable breakpoints.")
2633 (def-gdb-frame-for-buffer
2634 gdb-frame-breakpoints-buffer
2635 'gdb-breakpoints-buffer
2636 "Display status of user-settable breakpoints in a new frame.")
2638 (defvar gdb-breakpoints-mode-map
2639 (let ((map (make-sparse-keymap))
2640 (menu (make-sparse-keymap "Breakpoints")))
2641 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2642 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2643 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2644 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2645 (suppress-keymap map)
2646 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2647 (define-key map " " 'gdb-toggle-breakpoint)
2648 (define-key map "D" 'gdb-delete-breakpoint)
2649 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2650 (define-key map "q" 'gdb-delete-frame-or-window)
2651 (define-key map "\r" 'gdb-goto-breakpoint)
2652 (define-key map "\t" (lambda ()
2653 (interactive)
2654 (gdb-set-window-buffer
2655 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2656 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2657 (define-key map [follow-link] 'mouse-face)
2658 map))
2660 (defun gdb-delete-frame-or-window ()
2661 "Delete frame if there is only one window. Otherwise delete the window."
2662 (interactive)
2663 (if (one-window-p) (delete-frame)
2664 (delete-window)))
2666 ;;from make-mode-line-mouse-map
2667 (defun gdb-make-header-line-mouse-map (mouse function) "\
2668 Return a keymap with single entry for mouse key MOUSE on the header line.
2669 MOUSE is defined to run function FUNCTION with no args in the buffer
2670 corresponding to the mode line clicked."
2671 (let ((map (make-sparse-keymap)))
2672 (define-key map (vector 'header-line mouse) function)
2673 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2674 map))
2676 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2677 `(propertize ,name
2678 'help-echo ,help-echo
2679 'mouse-face ',mouse-face
2680 'face ',face
2681 'local-map
2682 (gdb-make-header-line-mouse-map
2683 'mouse-1
2684 (lambda (event) (interactive "e")
2685 (save-selected-window
2686 (select-window (posn-window (event-start event)))
2687 (gdb-set-window-buffer
2688 (gdb-get-buffer-create ',buffer) t) )))))
2691 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2692 ;;; Threads view
2694 (defun gdb-threads-buffer-name ()
2695 (concat "*threads of " (gdb-get-target-string) "*"))
2697 (def-gdb-display-buffer
2698 gdb-display-threads-buffer
2699 'gdb-threads-buffer
2700 "Display GDB threads.")
2702 (def-gdb-frame-for-buffer
2703 gdb-frame-threads-buffer
2704 'gdb-threads-buffer
2705 "Display GDB threads in a new frame.")
2707 (def-gdb-trigger-and-handler
2708 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2709 gdb-thread-list-handler gdb-thread-list-handler-custom
2710 '(start update update-threads))
2712 (gdb-set-buffer-rules
2713 'gdb-threads-buffer
2714 'gdb-threads-buffer-name
2715 'gdb-threads-mode
2716 'gdb-invalidate-threads)
2718 (defvar gdb-threads-font-lock-keywords
2719 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2720 (" \\(stopped\\)" (1 font-lock-warning-face))
2721 (" \\(running\\)" (1 font-lock-string-face))
2722 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2723 "Font lock keywords used in `gdb-threads-mode'.")
2725 (defvar gdb-threads-mode-map
2726 (let ((map (make-sparse-keymap)))
2727 (define-key map "\r" 'gdb-select-thread)
2728 (define-key map "f" 'gdb-display-stack-for-thread)
2729 (define-key map "F" 'gdb-frame-stack-for-thread)
2730 (define-key map "l" 'gdb-display-locals-for-thread)
2731 (define-key map "L" 'gdb-frame-locals-for-thread)
2732 (define-key map "r" 'gdb-display-registers-for-thread)
2733 (define-key map "R" 'gdb-frame-registers-for-thread)
2734 (define-key map "d" 'gdb-display-disassembly-for-thread)
2735 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2736 (define-key map "i" 'gdb-interrupt-thread)
2737 (define-key map "c" 'gdb-continue-thread)
2738 (define-key map "s" 'gdb-step-thread)
2739 (define-key map "\t"
2740 (lambda ()
2741 (interactive)
2742 (gdb-set-window-buffer
2743 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2744 (define-key map [mouse-2] 'gdb-select-thread)
2745 (define-key map [follow-link] 'mouse-face)
2746 map))
2748 (defvar gdb-threads-header
2749 (list
2750 (gdb-propertize-header
2751 "Breakpoints" gdb-breakpoints-buffer
2752 "mouse-1: select" mode-line-highlight mode-line-inactive)
2754 (gdb-propertize-header "Threads" gdb-threads-buffer
2755 nil nil mode-line)))
2757 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2758 "Major mode for GDB threads."
2759 (setq gdb-thread-position (make-marker))
2760 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2761 (setq header-line-format gdb-threads-header)
2762 (set (make-local-variable 'font-lock-defaults)
2763 '(gdb-threads-font-lock-keywords))
2764 'gdb-invalidate-threads)
2766 (defun gdb-thread-list-handler-custom ()
2767 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2768 (table (make-gdb-table))
2769 (marked-line nil))
2770 (setq gdb-threads-list nil)
2771 (setq gdb-running-threads-count 0)
2772 (setq gdb-stopped-threads-count 0)
2773 (set-marker gdb-thread-position nil)
2775 (dolist (thread (reverse threads-list))
2776 (let ((running (equal (bindat-get-field thread 'state) "running")))
2777 (add-to-list 'gdb-threads-list
2778 (cons (bindat-get-field thread 'id)
2779 thread))
2780 (if running
2781 (incf gdb-running-threads-count)
2782 (incf gdb-stopped-threads-count))
2784 (gdb-table-add-row table
2785 (list
2786 (bindat-get-field thread 'id)
2787 (concat
2788 (if gdb-thread-buffer-verbose-names
2789 (concat (bindat-get-field thread 'target-id) " ") "")
2790 (bindat-get-field thread 'state)
2791 ;; Include frame information for stopped threads
2792 (if (not running)
2793 (concat
2794 " in " (bindat-get-field thread 'frame 'func)
2795 (if gdb-thread-buffer-arguments
2796 (concat
2797 " ("
2798 (let ((args (bindat-get-field thread 'frame 'args)))
2799 (mapconcat
2800 (lambda (arg)
2801 (apply #'format "%s=%s"
2802 (gdb-get-many-fields arg 'name 'value)))
2803 args ","))
2804 ")")
2806 (if gdb-thread-buffer-locations
2807 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2808 (if gdb-thread-buffer-addresses
2809 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2810 "")))
2811 (list
2812 'gdb-thread thread
2813 'mouse-face 'highlight
2814 'help-echo "mouse-2, RET: select thread")))
2815 (when (string-equal gdb-thread-number
2816 (bindat-get-field thread 'id))
2817 (setq marked-line (length gdb-threads-list))))
2818 (insert (gdb-table-string table " "))
2819 (when marked-line
2820 (gdb-mark-line marked-line gdb-thread-position)))
2821 ;; We update gud-running here because we need to make sure that
2822 ;; gdb-threads-list is up-to-date
2823 (gdb-update-gud-running)
2824 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2826 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2827 "Define a NAME command which will act upon thread on the current line.
2829 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2830 be the value of 'gdb-thread property of the current line. If
2831 'gdb-thread is nil, error is signaled."
2832 `(defun ,name (&optional event)
2833 ,(when doc doc)
2834 (interactive (list last-input-event))
2835 (if event (posn-set-point (event-end event)))
2836 (save-excursion
2837 (beginning-of-line)
2838 (let ((thread (get-text-property (point) 'gdb-thread)))
2839 (if thread
2840 ,custom-defun
2841 (error "Not recognized as thread line"))))))
2843 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2844 &optional doc)
2845 "Define a NAME which will call BUFFER-COMMAND with id of thread
2846 on the current line."
2847 `(def-gdb-thread-buffer-command ,name
2848 (,buffer-command (bindat-get-field thread 'id))
2849 ,doc))
2851 (def-gdb-thread-buffer-command gdb-select-thread
2852 (let ((new-id (bindat-get-field thread 'id)))
2853 (gdb-setq-thread-number new-id)
2854 (gdb-input (concat "-thread-select " new-id) 'ignore)
2855 (gdb-update))
2856 "Select the thread at current line of threads buffer.")
2858 (def-gdb-thread-buffer-simple-command
2859 gdb-display-stack-for-thread
2860 gdb-preemptively-display-stack-buffer
2861 "Display stack buffer for the thread at current line.")
2863 (def-gdb-thread-buffer-simple-command
2864 gdb-display-locals-for-thread
2865 gdb-preemptively-display-locals-buffer
2866 "Display locals buffer for the thread at current line.")
2868 (def-gdb-thread-buffer-simple-command
2869 gdb-display-registers-for-thread
2870 gdb-preemptively-display-registers-buffer
2871 "Display registers buffer for the thread at current line.")
2873 (def-gdb-thread-buffer-simple-command
2874 gdb-display-disassembly-for-thread
2875 gdb-preemptively-display-disassembly-buffer
2876 "Display disassembly buffer for the thread at current line.")
2878 (def-gdb-thread-buffer-simple-command
2879 gdb-frame-stack-for-thread
2880 gdb-frame-stack-buffer
2881 "Display a new frame with stack buffer for the thread at
2882 current line.")
2884 (def-gdb-thread-buffer-simple-command
2885 gdb-frame-locals-for-thread
2886 gdb-frame-locals-buffer
2887 "Display a new frame with locals buffer for the thread at
2888 current line.")
2890 (def-gdb-thread-buffer-simple-command
2891 gdb-frame-registers-for-thread
2892 gdb-frame-registers-buffer
2893 "Display a new frame with registers buffer for the thread at
2894 current line.")
2896 (def-gdb-thread-buffer-simple-command
2897 gdb-frame-disassembly-for-thread
2898 gdb-frame-disassembly-buffer
2899 "Display a new frame with disassembly buffer for the thread at
2900 current line.")
2902 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2903 "Define a NAME which will execute GUD-COMMAND with
2904 `gdb-thread-number' locally bound to id of thread on the current
2905 line."
2906 `(def-gdb-thread-buffer-command ,name
2907 (if gdb-non-stop
2908 (let ((gdb-thread-number (bindat-get-field thread 'id))
2909 (gdb-gud-control-all-threads nil))
2910 (call-interactively #',gud-command))
2911 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2912 ,doc))
2914 (def-gdb-thread-buffer-gud-command
2915 gdb-interrupt-thread
2916 gud-stop-subjob
2917 "Interrupt thread at current line.")
2919 (def-gdb-thread-buffer-gud-command
2920 gdb-continue-thread
2921 gud-cont
2922 "Continue thread at current line.")
2924 (def-gdb-thread-buffer-gud-command
2925 gdb-step-thread
2926 gud-step
2927 "Step thread at current line.")
2930 ;;; Memory view
2932 (defcustom gdb-memory-rows 8
2933 "Number of data rows in memory window."
2934 :type 'integer
2935 :group 'gud
2936 :version "23.2")
2938 (defcustom gdb-memory-columns 4
2939 "Number of data columns in memory window."
2940 :type 'integer
2941 :group 'gud
2942 :version "23.2")
2944 (defcustom gdb-memory-format "x"
2945 "Display format of data items in memory window."
2946 :type '(choice (const :tag "Hexadecimal" "x")
2947 (const :tag "Signed decimal" "d")
2948 (const :tag "Unsigned decimal" "u")
2949 (const :tag "Octal" "o")
2950 (const :tag "Binary" "t"))
2951 :group 'gud
2952 :version "22.1")
2954 (defcustom gdb-memory-unit 4
2955 "Unit size of data items in memory window."
2956 :type '(choice (const :tag "Byte" 1)
2957 (const :tag "Halfword" 2)
2958 (const :tag "Word" 4)
2959 (const :tag "Giant word" 8))
2960 :group 'gud
2961 :version "23.2")
2963 (def-gdb-trigger-and-handler
2964 gdb-invalidate-memory
2965 (format "-data-read-memory %s %s %d %d %d"
2966 gdb-memory-address
2967 gdb-memory-format
2968 gdb-memory-unit
2969 gdb-memory-rows
2970 gdb-memory-columns)
2971 gdb-read-memory-handler
2972 gdb-read-memory-custom
2973 '(start update))
2975 (gdb-set-buffer-rules
2976 'gdb-memory-buffer
2977 'gdb-memory-buffer-name
2978 'gdb-memory-mode
2979 'gdb-invalidate-memory)
2981 (defun gdb-memory-column-width (size format)
2982 "Return length of string with memory unit of SIZE in FORMAT.
2984 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2985 in `gdb-memory-format'."
2986 (let ((format-base (cdr (assoc format
2987 '(("x" . 16)
2988 ("d" . 10) ("u" . 10)
2989 ("o" . 8)
2990 ("t" . 2))))))
2991 (if format-base
2992 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2993 (cond ((string-equal format "x")
2994 (+ 2 res)) ; hexadecimal numbers have 0x in front
2995 ((or (string-equal format "d")
2996 (string-equal format "o"))
2997 (1+ res))
2998 (t res)))
2999 (error "Unknown format"))))
3001 (defun gdb-read-memory-custom ()
3002 (let* ((res (gdb-json-partial-output))
3003 (err-msg (bindat-get-field res 'msg)))
3004 (if (not err-msg)
3005 (let ((memory (bindat-get-field res 'memory)))
3006 (setq gdb-memory-address (bindat-get-field res 'addr))
3007 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3008 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3009 (setq gdb-memory-last-address gdb-memory-address)
3010 (dolist (row memory)
3011 (insert (concat (bindat-get-field row 'addr) ":"))
3012 (dolist (column (bindat-get-field row 'data))
3013 (insert (gdb-pad-string column
3014 (+ 2 (gdb-memory-column-width
3015 gdb-memory-unit
3016 gdb-memory-format)))))
3017 (newline)))
3018 ;; Show last page instead of empty buffer when out of bounds
3019 (progn
3020 (let ((gdb-memory-address gdb-memory-last-address))
3021 (gdb-invalidate-memory 'update)
3022 (error err-msg))))))
3024 (defvar gdb-memory-mode-map
3025 (let ((map (make-sparse-keymap)))
3026 (suppress-keymap map t)
3027 (define-key map "q" 'kill-this-buffer)
3028 (define-key map "n" 'gdb-memory-show-next-page)
3029 (define-key map "p" 'gdb-memory-show-previous-page)
3030 (define-key map "a" 'gdb-memory-set-address)
3031 (define-key map "t" 'gdb-memory-format-binary)
3032 (define-key map "o" 'gdb-memory-format-octal)
3033 (define-key map "u" 'gdb-memory-format-unsigned)
3034 (define-key map "d" 'gdb-memory-format-signed)
3035 (define-key map "x" 'gdb-memory-format-hexadecimal)
3036 (define-key map "b" 'gdb-memory-unit-byte)
3037 (define-key map "h" 'gdb-memory-unit-halfword)
3038 (define-key map "w" 'gdb-memory-unit-word)
3039 (define-key map "g" 'gdb-memory-unit-giant)
3040 (define-key map "R" 'gdb-memory-set-rows)
3041 (define-key map "C" 'gdb-memory-set-columns)
3042 map))
3044 (defun gdb-memory-set-address-event (event)
3045 "Handle a click on address field in memory buffer header."
3046 (interactive "e")
3047 (save-selected-window
3048 (select-window (posn-window (event-start event)))
3049 (gdb-memory-set-address)))
3051 ;; Non-event version for use within keymap
3052 (defun gdb-memory-set-address ()
3053 "Set the start memory address."
3054 (interactive)
3055 (let ((arg (read-from-minibuffer "Memory address: ")))
3056 (setq gdb-memory-address arg))
3057 (gdb-invalidate-memory 'update))
3059 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3060 "Define a function NAME which reads new VAR value from minibuffer."
3061 `(defun ,name (event)
3062 ,(when doc doc)
3063 (interactive "e")
3064 (save-selected-window
3065 (select-window (posn-window (event-start event)))
3066 (let* ((arg (read-from-minibuffer ,echo-string))
3067 (count (string-to-number arg)))
3068 (if (<= count 0)
3069 (error "Positive number only")
3070 (customize-set-variable ',variable count)
3071 (gdb-invalidate-memory 'update))))))
3073 (def-gdb-set-positive-number
3074 gdb-memory-set-rows
3075 gdb-memory-rows
3076 "Rows: "
3077 "Set the number of data rows in memory window.")
3079 (def-gdb-set-positive-number
3080 gdb-memory-set-columns
3081 gdb-memory-columns
3082 "Columns: "
3083 "Set the number of data columns in memory window.")
3085 (defmacro def-gdb-memory-format (name format doc)
3086 "Define a function NAME to switch memory buffer to use FORMAT.
3088 DOC is an optional documentation string."
3089 `(defun ,name () ,(when doc doc)
3090 (interactive)
3091 (customize-set-variable 'gdb-memory-format ,format)
3092 (gdb-invalidate-memory 'update)))
3094 (def-gdb-memory-format
3095 gdb-memory-format-binary "t"
3096 "Set the display format to binary.")
3098 (def-gdb-memory-format
3099 gdb-memory-format-octal "o"
3100 "Set the display format to octal.")
3102 (def-gdb-memory-format
3103 gdb-memory-format-unsigned "u"
3104 "Set the display format to unsigned decimal.")
3106 (def-gdb-memory-format
3107 gdb-memory-format-signed "d"
3108 "Set the display format to decimal.")
3110 (def-gdb-memory-format
3111 gdb-memory-format-hexadecimal "x"
3112 "Set the display format to hexadecimal.")
3114 (defvar gdb-memory-format-map
3115 (let ((map (make-sparse-keymap)))
3116 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3117 map)
3118 "Keymap to select format in the header line.")
3120 (defvar gdb-memory-format-menu
3121 (let ((map (make-sparse-keymap "Format")))
3123 (define-key map [binary]
3124 '(menu-item "Binary" gdb-memory-format-binary
3125 :button (:radio . (equal gdb-memory-format "t"))))
3126 (define-key map [octal]
3127 '(menu-item "Octal" gdb-memory-format-octal
3128 :button (:radio . (equal gdb-memory-format "o"))))
3129 (define-key map [unsigned]
3130 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3131 :button (:radio . (equal gdb-memory-format "u"))))
3132 (define-key map [signed]
3133 '(menu-item "Signed Decimal" gdb-memory-format-signed
3134 :button (:radio . (equal gdb-memory-format "d"))))
3135 (define-key map [hexadecimal]
3136 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3137 :button (:radio . (equal gdb-memory-format "x"))))
3138 map)
3139 "Menu of display formats in the header line.")
3141 (defun gdb-memory-format-menu (event)
3142 (interactive "@e")
3143 (x-popup-menu event gdb-memory-format-menu))
3145 (defun gdb-memory-format-menu-1 (event)
3146 (interactive "e")
3147 (save-selected-window
3148 (select-window (posn-window (event-start event)))
3149 (let* ((selection (gdb-memory-format-menu event))
3150 (binding (and selection (lookup-key gdb-memory-format-menu
3151 (vector (car selection))))))
3152 (if binding (call-interactively binding)))))
3154 (defmacro def-gdb-memory-unit (name unit-size doc)
3155 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3157 DOC is an optional documentation string."
3158 `(defun ,name () ,(when doc doc)
3159 (interactive)
3160 (customize-set-variable 'gdb-memory-unit ,unit-size)
3161 (gdb-invalidate-memory 'update)))
3163 (def-gdb-memory-unit gdb-memory-unit-giant 8
3164 "Set the unit size to giant words (eight bytes).")
3166 (def-gdb-memory-unit gdb-memory-unit-word 4
3167 "Set the unit size to words (four bytes).")
3169 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3170 "Set the unit size to halfwords (two bytes).")
3172 (def-gdb-memory-unit gdb-memory-unit-byte 1
3173 "Set the unit size to bytes.")
3175 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3176 "Define a function NAME which show new address in memory buffer.
3178 The defined function switches Memory buffer to show address
3179 stored in ADDRESS-VAR variable.
3181 DOC is an optional documentation string."
3182 `(defun ,name
3183 ,(when doc doc)
3184 (interactive)
3185 (let ((gdb-memory-address ,address-var))
3186 (gdb-invalidate-memory))))
3188 (def-gdb-memory-show-page gdb-memory-show-previous-page
3189 gdb-memory-prev-page)
3191 (def-gdb-memory-show-page gdb-memory-show-next-page
3192 gdb-memory-next-page)
3194 (defvar gdb-memory-unit-map
3195 (let ((map (make-sparse-keymap)))
3196 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3197 map)
3198 "Keymap to select units in the header line.")
3200 (defvar gdb-memory-unit-menu
3201 (let ((map (make-sparse-keymap "Unit")))
3202 (define-key map [giantwords]
3203 '(menu-item "Giant words" gdb-memory-unit-giant
3204 :button (:radio . (equal gdb-memory-unit 8))))
3205 (define-key map [words]
3206 '(menu-item "Words" gdb-memory-unit-word
3207 :button (:radio . (equal gdb-memory-unit 4))))
3208 (define-key map [halfwords]
3209 '(menu-item "Halfwords" gdb-memory-unit-halfword
3210 :button (:radio . (equal gdb-memory-unit 2))))
3211 (define-key map [bytes]
3212 '(menu-item "Bytes" gdb-memory-unit-byte
3213 :button (:radio . (equal gdb-memory-unit 1))))
3214 map)
3215 "Menu of units in the header line.")
3217 (defun gdb-memory-unit-menu (event)
3218 (interactive "@e")
3219 (x-popup-menu event gdb-memory-unit-menu))
3221 (defun gdb-memory-unit-menu-1 (event)
3222 (interactive "e")
3223 (save-selected-window
3224 (select-window (posn-window (event-start event)))
3225 (let* ((selection (gdb-memory-unit-menu event))
3226 (binding (and selection (lookup-key gdb-memory-unit-menu
3227 (vector (car selection))))))
3228 (if binding (call-interactively binding)))))
3230 (defvar gdb-memory-font-lock-keywords
3231 '(;; <__function.name+n>
3232 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3233 (1 font-lock-function-name-face)))
3234 "Font lock keywords used in `gdb-memory-mode'.")
3236 (defvar gdb-memory-header
3237 '(:eval
3238 (concat
3239 "Start address["
3240 (propertize "-"
3241 'face font-lock-warning-face
3242 'help-echo "mouse-1: decrement address"
3243 'mouse-face 'mode-line-highlight
3244 'local-map (gdb-make-header-line-mouse-map
3245 'mouse-1
3246 #'gdb-memory-show-previous-page))
3248 (propertize "+"
3249 'face font-lock-warning-face
3250 'help-echo "mouse-1: increment address"
3251 'mouse-face 'mode-line-highlight
3252 'local-map (gdb-make-header-line-mouse-map
3253 'mouse-1
3254 #'gdb-memory-show-next-page))
3255 "]: "
3256 (propertize gdb-memory-address
3257 'face font-lock-warning-face
3258 'help-echo "mouse-1: set start address"
3259 'mouse-face 'mode-line-highlight
3260 'local-map (gdb-make-header-line-mouse-map
3261 'mouse-1
3262 #'gdb-memory-set-address-event))
3263 " Rows: "
3264 (propertize (number-to-string gdb-memory-rows)
3265 'face font-lock-warning-face
3266 'help-echo "mouse-1: set number of columns"
3267 'mouse-face 'mode-line-highlight
3268 'local-map (gdb-make-header-line-mouse-map
3269 'mouse-1
3270 #'gdb-memory-set-rows))
3271 " Columns: "
3272 (propertize (number-to-string gdb-memory-columns)
3273 'face font-lock-warning-face
3274 'help-echo "mouse-1: set number of columns"
3275 'mouse-face 'mode-line-highlight
3276 'local-map (gdb-make-header-line-mouse-map
3277 'mouse-1
3278 #'gdb-memory-set-columns))
3279 " Display Format: "
3280 (propertize gdb-memory-format
3281 'face font-lock-warning-face
3282 'help-echo "mouse-3: select display format"
3283 'mouse-face 'mode-line-highlight
3284 'local-map gdb-memory-format-map)
3285 " Unit Size: "
3286 (propertize (number-to-string gdb-memory-unit)
3287 'face font-lock-warning-face
3288 'help-echo "mouse-3: select unit size"
3289 'mouse-face 'mode-line-highlight
3290 'local-map gdb-memory-unit-map)))
3291 "Header line used in `gdb-memory-mode'.")
3293 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3294 "Major mode for examining memory."
3295 (setq header-line-format gdb-memory-header)
3296 (set (make-local-variable 'font-lock-defaults)
3297 '(gdb-memory-font-lock-keywords))
3298 'gdb-invalidate-memory)
3300 (defun gdb-memory-buffer-name ()
3301 (concat "*memory of " (gdb-get-target-string) "*"))
3303 (def-gdb-display-buffer
3304 gdb-display-memory-buffer
3305 'gdb-memory-buffer
3306 "Display memory contents.")
3308 (defun gdb-frame-memory-buffer ()
3309 "Display memory contents in a new frame."
3310 (interactive)
3311 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3312 (special-display-frame-alist
3313 `((left-fringe . 0)
3314 (right-fringe . 0)
3315 (width . 83)
3316 ,@gdb-frame-parameters)))
3317 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3320 ;;; Disassembly view
3322 (defun gdb-disassembly-buffer-name ()
3323 (gdb-current-context-buffer-name
3324 (concat "disassembly of " (gdb-get-target-string))))
3326 (def-gdb-display-buffer
3327 gdb-display-disassembly-buffer
3328 'gdb-disassembly-buffer
3329 "Display disassembly for current stack frame.")
3331 (def-gdb-preempt-display-buffer
3332 gdb-preemptively-display-disassembly-buffer
3333 'gdb-disassembly-buffer)
3335 (def-gdb-frame-for-buffer
3336 gdb-frame-disassembly-buffer
3337 'gdb-disassembly-buffer
3338 "Display disassembly in a new frame.")
3340 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3341 (let* ((frame (gdb-current-buffer-frame))
3342 (file (bindat-get-field frame 'fullname))
3343 (line (bindat-get-field frame 'line)))
3344 (if file
3345 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3346 ;; If we're unable to get a file name / line for $PC, simply
3347 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3348 ;; 150 bytes) instructions.
3349 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3350 gdb-disassembly-handler
3351 ;; We update disassembly only after we have actual frame information
3352 ;; about all threads, so no there's `update' signal in this list
3353 '(start update-disassembly))
3355 (def-gdb-auto-update-handler
3356 gdb-disassembly-handler
3357 gdb-invalidate-disassembly
3358 gdb-disassembly-handler-custom
3361 (gdb-set-buffer-rules
3362 'gdb-disassembly-buffer
3363 'gdb-disassembly-buffer-name
3364 'gdb-disassembly-mode
3365 'gdb-invalidate-disassembly)
3367 (defvar gdb-disassembly-font-lock-keywords
3368 '(;; <__function.name+n>
3369 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3370 (1 font-lock-function-name-face))
3371 ;; 0xNNNNNNNN <__function.name+n>: opcode
3372 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3373 (4 font-lock-keyword-face))
3374 ;; %register(at least i386)
3375 ("%\\sw+" . font-lock-variable-name-face)
3376 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3377 (1 font-lock-comment-face)
3378 (2 font-lock-function-name-face))
3379 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3380 "Font lock keywords used in `gdb-disassembly-mode'.")
3382 (defvar gdb-disassembly-mode-map
3383 ;; TODO
3384 (let ((map (make-sparse-keymap)))
3385 (suppress-keymap map)
3386 (define-key map "q" 'kill-this-buffer)
3387 map))
3389 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3390 "Major mode for GDB disassembly information."
3391 ;; TODO Rename overlay variable for disassembly mode
3392 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3393 (setq fringes-outside-margins t)
3394 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3395 (set (make-local-variable 'font-lock-defaults)
3396 '(gdb-disassembly-font-lock-keywords))
3397 'gdb-invalidate-disassembly)
3399 (defun gdb-disassembly-handler-custom ()
3400 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3401 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3402 (table (make-gdb-table))
3403 (marked-line nil))
3404 (dolist (instr instructions)
3405 (gdb-table-add-row table
3406 (list
3407 (bindat-get-field instr 'address)
3408 (let
3409 ((func-name (bindat-get-field instr 'func-name))
3410 (offset (bindat-get-field instr 'offset)))
3411 (if func-name
3412 (format "<%s+%s>:" func-name offset)
3413 ""))
3414 (bindat-get-field instr 'inst)))
3415 (when (string-equal (bindat-get-field instr 'address)
3416 address)
3417 (progn
3418 (setq marked-line (length (gdb-table-rows table)))
3419 (setq fringe-indicator-alist
3420 (if (string-equal gdb-frame-number "0")
3422 '((overlay-arrow . hollow-right-triangle)))))))
3423 (insert (gdb-table-string table " "))
3424 (gdb-disassembly-place-breakpoints)
3425 ;; Mark current position with overlay arrow and scroll window to
3426 ;; that point
3427 (when marked-line
3428 (let ((window (get-buffer-window (current-buffer) 0)))
3429 (set-window-point window (gdb-mark-line marked-line
3430 gdb-disassembly-position))))
3431 (setq mode-name
3432 (gdb-current-context-mode-name
3433 (concat "Disassembly: "
3434 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3436 (defun gdb-disassembly-place-breakpoints ()
3437 (gdb-remove-breakpoint-icons (point-min) (point-max))
3438 (dolist (breakpoint gdb-breakpoints-list)
3439 (let* ((breakpoint (cdr breakpoint))
3440 (bptno (bindat-get-field breakpoint 'number))
3441 (flag (bindat-get-field breakpoint 'enabled))
3442 (address (bindat-get-field breakpoint 'addr)))
3443 (save-excursion
3444 (goto-char (point-min))
3445 (if (re-search-forward (concat "^" address) nil t)
3446 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3449 (defvar gdb-breakpoints-header
3450 (list
3451 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3452 nil nil mode-line)
3454 (gdb-propertize-header "Threads" gdb-threads-buffer
3455 "mouse-1: select" mode-line-highlight
3456 mode-line-inactive)))
3458 ;;; Breakpoints view
3459 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3460 "Major mode for gdb breakpoints."
3461 (setq header-line-format gdb-breakpoints-header)
3462 'gdb-invalidate-breakpoints)
3464 (defun gdb-toggle-breakpoint ()
3465 "Enable/disable breakpoint at current line of breakpoints buffer."
3466 (interactive)
3467 (save-excursion
3468 (beginning-of-line)
3469 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3470 (if breakpoint
3471 (gud-basic-call
3472 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3473 "-break-disable "
3474 "-break-enable ")
3475 (bindat-get-field breakpoint 'number)))
3476 (error "Not recognized as break/watchpoint line")))))
3478 (defun gdb-delete-breakpoint ()
3479 "Delete the breakpoint at current line of breakpoints buffer."
3480 (interactive)
3481 (save-excursion
3482 (beginning-of-line)
3483 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3484 (if breakpoint
3485 (gud-basic-call (concat "-break-delete "
3486 (bindat-get-field breakpoint 'number)))
3487 (error "Not recognized as break/watchpoint line")))))
3489 (defun gdb-goto-breakpoint (&optional event)
3490 "Go to the location of breakpoint at current line of
3491 breakpoints buffer."
3492 (interactive (list last-input-event))
3493 (if event (posn-set-point (event-end event)))
3494 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3495 (let ((window (get-buffer-window gud-comint-buffer)))
3496 (if window (save-selected-window (select-window window))))
3497 (save-excursion
3498 (beginning-of-line)
3499 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3500 (if breakpoint
3501 (let ((bptno (bindat-get-field breakpoint 'number))
3502 (file (bindat-get-field breakpoint 'fullname))
3503 (line (bindat-get-field breakpoint 'line)))
3504 (save-selected-window
3505 (let* ((buffer (find-file-noselect
3506 (if (file-exists-p file) file
3507 (cdr (assoc bptno gdb-location-alist)))))
3508 (window (or (gdb-display-source-buffer buffer)
3509 (display-buffer buffer))))
3510 (setq gdb-source-window window)
3511 (with-current-buffer buffer
3512 (goto-char (point-min))
3513 (forward-line (1- (string-to-number line)))
3514 (set-window-point window (point))))))
3515 (error "Not recognized as break/watchpoint line")))))
3518 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3520 (def-gdb-trigger-and-handler
3521 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3522 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3523 '(start update))
3525 (gdb-set-buffer-rules
3526 'gdb-stack-buffer
3527 'gdb-stack-buffer-name
3528 'gdb-frames-mode
3529 'gdb-invalidate-frames)
3531 (defun gdb-frame-location (frame)
3532 "Return \" of file:line\" or \" of library\" for structure FRAME.
3534 FRAME must have either \"file\" and \"line\" members or \"from\"
3535 member."
3536 (let ((file (bindat-get-field frame 'file))
3537 (line (bindat-get-field frame 'line))
3538 (from (bindat-get-field frame 'from)))
3539 (let ((res (or (and file line (concat file ":" line))
3540 from)))
3541 (if res (concat " of " res) ""))))
3543 (defun gdb-stack-list-frames-custom ()
3544 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3545 (table (make-gdb-table)))
3546 (set-marker gdb-stack-position nil)
3547 (dolist (frame stack)
3548 (gdb-table-add-row table
3549 (list
3550 (bindat-get-field frame 'level)
3551 "in"
3552 (concat
3553 (bindat-get-field frame 'func)
3554 (if gdb-stack-buffer-locations
3555 (gdb-frame-location frame) "")
3556 (if gdb-stack-buffer-addresses
3557 (concat " at " (bindat-get-field frame 'addr)) "")))
3558 `(mouse-face highlight
3559 help-echo "mouse-2, RET: Select frame"
3560 gdb-frame ,frame)))
3561 (insert (gdb-table-string table " ")))
3562 (when (and gdb-frame-number
3563 (gdb-buffer-shows-main-thread-p))
3564 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3565 gdb-stack-position))
3566 (setq mode-name
3567 (gdb-current-context-mode-name "Frames")))
3569 (defun gdb-stack-buffer-name ()
3570 (gdb-current-context-buffer-name
3571 (concat "stack frames of " (gdb-get-target-string))))
3573 (def-gdb-display-buffer
3574 gdb-display-stack-buffer
3575 'gdb-stack-buffer
3576 "Display backtrace of current stack.")
3578 (def-gdb-preempt-display-buffer
3579 gdb-preemptively-display-stack-buffer
3580 'gdb-stack-buffer nil t)
3582 (def-gdb-frame-for-buffer
3583 gdb-frame-stack-buffer
3584 'gdb-stack-buffer
3585 "Display backtrace of current stack in a new frame.")
3587 (defvar gdb-frames-mode-map
3588 (let ((map (make-sparse-keymap)))
3589 (suppress-keymap map)
3590 (define-key map "q" 'kill-this-buffer)
3591 (define-key map "\r" 'gdb-select-frame)
3592 (define-key map [mouse-2] 'gdb-select-frame)
3593 (define-key map [follow-link] 'mouse-face)
3594 map))
3596 (defvar gdb-frames-font-lock-keywords
3597 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3598 "Font lock keywords used in `gdb-frames-mode'.")
3600 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3601 "Major mode for gdb call stack."
3602 (setq gdb-stack-position (make-marker))
3603 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3604 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3605 (set (make-local-variable 'font-lock-defaults)
3606 '(gdb-frames-font-lock-keywords))
3607 'gdb-invalidate-frames)
3609 (defun gdb-select-frame (&optional event)
3610 "Select the frame and display the relevant source."
3611 (interactive (list last-input-event))
3612 (if event (posn-set-point (event-end event)))
3613 (let ((frame (get-text-property (point) 'gdb-frame)))
3614 (if frame
3615 (if (gdb-buffer-shows-main-thread-p)
3616 (let ((new-level (bindat-get-field frame 'level)))
3617 (setq gdb-frame-number new-level)
3618 (gdb-input (concat "-stack-select-frame " new-level)
3619 'ignore)
3620 (gdb-update))
3621 (error "Could not select frame for non-current thread"))
3622 (error "Not recognized as frame line"))))
3625 ;; Locals buffer.
3626 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3627 (def-gdb-trigger-and-handler
3628 gdb-invalidate-locals
3629 (concat (gdb-current-context-command "-stack-list-locals")
3630 " --simple-values")
3631 gdb-locals-handler gdb-locals-handler-custom
3632 '(start update))
3634 (gdb-set-buffer-rules
3635 'gdb-locals-buffer
3636 'gdb-locals-buffer-name
3637 'gdb-locals-mode
3638 'gdb-invalidate-locals)
3640 (defvar gdb-locals-watch-map
3641 (let ((map (make-sparse-keymap)))
3642 (suppress-keymap map)
3643 (define-key map "\r" 'gud-watch)
3644 (define-key map [mouse-2] 'gud-watch)
3645 map)
3646 "Keymap to create watch expression of a complex data type local variable.")
3648 (defvar gdb-edit-locals-map-1
3649 (let ((map (make-sparse-keymap)))
3650 (suppress-keymap map)
3651 (define-key map "\r" 'gdb-edit-locals-value)
3652 (define-key map [mouse-2] 'gdb-edit-locals-value)
3653 map)
3654 "Keymap to edit value of a simple data type local variable.")
3656 (defun gdb-edit-locals-value (&optional event)
3657 "Assign a value to a variable displayed in the locals buffer."
3658 (interactive (list last-input-event))
3659 (save-excursion
3660 (if event (posn-set-point (event-end event)))
3661 (beginning-of-line)
3662 (let* ((var (bindat-get-field
3663 (get-text-property (point) 'gdb-local-variable) 'name))
3664 (value (read-string (format "New value (%s): " var))))
3665 (gud-basic-call
3666 (concat "-gdb-set variable " var " = " value)))))
3668 ;; Don't display values of arrays or structures.
3669 ;; These can be expanded using gud-watch.
3670 (defun gdb-locals-handler-custom ()
3671 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3672 (table (make-gdb-table)))
3673 (dolist (local locals-list)
3674 (let ((name (bindat-get-field local 'name))
3675 (value (bindat-get-field local 'value))
3676 (type (bindat-get-field local 'type)))
3677 (if (or (not value)
3678 (string-match "\\0x" value))
3679 (add-text-properties 0 (length name)
3680 `(mouse-face highlight
3681 help-echo "mouse-2: create watch expression"
3682 local-map ,gdb-locals-watch-map)
3683 name)
3684 (add-text-properties 0 (length value)
3685 `(mouse-face highlight
3686 help-echo "mouse-2: edit value"
3687 local-map ,gdb-edit-locals-map-1)
3688 value))
3689 (gdb-table-add-row
3690 table
3691 (list
3692 (propertize type 'font-lock-face font-lock-type-face)
3693 (propertize name 'font-lock-face font-lock-variable-name-face)
3694 value)
3695 `(gdb-local-variable ,local))))
3696 (insert (gdb-table-string table " "))
3697 (setq mode-name
3698 (gdb-current-context-mode-name
3699 (concat "Locals: "
3700 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3702 (defvar gdb-locals-header
3703 (list
3704 (gdb-propertize-header "Locals" gdb-locals-buffer
3705 nil nil mode-line)
3707 (gdb-propertize-header "Registers" gdb-registers-buffer
3708 "mouse-1: select" mode-line-highlight
3709 mode-line-inactive)))
3711 (defvar gdb-locals-mode-map
3712 (let ((map (make-sparse-keymap)))
3713 (suppress-keymap map)
3714 (define-key map "q" 'kill-this-buffer)
3715 (define-key map "\t" (lambda ()
3716 (interactive)
3717 (gdb-set-window-buffer
3718 (gdb-get-buffer-create
3719 'gdb-registers-buffer
3720 gdb-thread-number) t)))
3721 map))
3723 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3724 "Major mode for gdb locals."
3725 (setq header-line-format gdb-locals-header)
3726 'gdb-invalidate-locals)
3728 (defun gdb-locals-buffer-name ()
3729 (gdb-current-context-buffer-name
3730 (concat "locals of " (gdb-get-target-string))))
3732 (def-gdb-display-buffer
3733 gdb-display-locals-buffer
3734 'gdb-locals-buffer
3735 "Display local variables of current stack and their values.")
3737 (def-gdb-preempt-display-buffer
3738 gdb-preemptively-display-locals-buffer
3739 'gdb-locals-buffer nil t)
3741 (def-gdb-frame-for-buffer
3742 gdb-frame-locals-buffer
3743 'gdb-locals-buffer
3744 "Display local variables of current stack and their values in a new frame.")
3747 ;; Registers buffer.
3749 (def-gdb-trigger-and-handler
3750 gdb-invalidate-registers
3751 (concat (gdb-current-context-command "-data-list-register-values") " x")
3752 gdb-registers-handler
3753 gdb-registers-handler-custom
3754 '(start update))
3756 (gdb-set-buffer-rules
3757 'gdb-registers-buffer
3758 'gdb-registers-buffer-name
3759 'gdb-registers-mode
3760 'gdb-invalidate-registers)
3762 (defun gdb-registers-handler-custom ()
3763 (when gdb-register-names
3764 (let ((register-values
3765 (bindat-get-field (gdb-json-partial-output) 'register-values))
3766 (table (make-gdb-table)))
3767 (dolist (register register-values)
3768 (let* ((register-number (bindat-get-field register 'number))
3769 (value (bindat-get-field register 'value))
3770 (register-name (nth (string-to-number register-number)
3771 gdb-register-names)))
3772 (gdb-table-add-row
3773 table
3774 (list
3775 (propertize register-name
3776 'font-lock-face font-lock-variable-name-face)
3777 (if (member register-number gdb-changed-registers)
3778 (propertize value 'font-lock-face font-lock-warning-face)
3779 value))
3780 `(mouse-face highlight
3781 help-echo "mouse-2: edit value"
3782 gdb-register-name ,register-name))))
3783 (insert (gdb-table-string table " ")))
3784 (setq mode-name
3785 (gdb-current-context-mode-name "Registers"))))
3787 (defun gdb-edit-register-value (&optional event)
3788 "Assign a value to a register displayed in the registers buffer."
3789 (interactive (list last-input-event))
3790 (save-excursion
3791 (if event (posn-set-point (event-end event)))
3792 (beginning-of-line)
3793 (let* ((var (bindat-get-field
3794 (get-text-property (point) 'gdb-register-name)))
3795 (value (read-string (format "New value (%s): " var))))
3796 (gud-basic-call
3797 (concat "-gdb-set variable $" var " = " value)))))
3799 (defvar gdb-registers-mode-map
3800 (let ((map (make-sparse-keymap)))
3801 (suppress-keymap map)
3802 (define-key map "\r" 'gdb-edit-register-value)
3803 (define-key map [mouse-2] 'gdb-edit-register-value)
3804 (define-key map "q" 'kill-this-buffer)
3805 (define-key map "\t" (lambda ()
3806 (interactive)
3807 (gdb-set-window-buffer
3808 (gdb-get-buffer-create
3809 'gdb-locals-buffer
3810 gdb-thread-number) t)))
3811 map))
3813 (defvar gdb-registers-header
3814 (list
3815 (gdb-propertize-header "Locals" gdb-locals-buffer
3816 "mouse-1: select" mode-line-highlight
3817 mode-line-inactive)
3819 (gdb-propertize-header "Registers" gdb-registers-buffer
3820 nil nil mode-line)))
3822 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3823 "Major mode for gdb registers."
3824 (setq header-line-format gdb-registers-header)
3825 'gdb-invalidate-registers)
3827 (defun gdb-registers-buffer-name ()
3828 (gdb-current-context-buffer-name
3829 (concat "registers of " (gdb-get-target-string))))
3831 (def-gdb-display-buffer
3832 gdb-display-registers-buffer
3833 'gdb-registers-buffer
3834 "Display integer register contents.")
3836 (def-gdb-preempt-display-buffer
3837 gdb-preemptively-display-registers-buffer
3838 'gdb-registers-buffer nil t)
3840 (def-gdb-frame-for-buffer
3841 gdb-frame-registers-buffer
3842 'gdb-registers-buffer
3843 "Display integer register contents in a new frame.")
3845 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3846 (defun gdb-get-changed-registers ()
3847 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3848 (not (gdb-pending-p 'gdb-get-changed-registers)))
3849 (gdb-input "-data-list-changed-registers"
3850 'gdb-changed-registers-handler)
3851 (gdb-add-pending 'gdb-get-changed-registers)))
3853 (defun gdb-changed-registers-handler ()
3854 (gdb-delete-pending 'gdb-get-changed-registers)
3855 (setq gdb-changed-registers nil)
3856 (dolist (register-number
3857 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3858 (push register-number gdb-changed-registers)))
3860 (defun gdb-register-names-handler ()
3861 ;; Don't use gdb-pending-triggers because this handler is called
3862 ;; only once (in gdb-init-1)
3863 (setq gdb-register-names nil)
3864 (dolist (register-name
3865 (bindat-get-field (gdb-json-partial-output) 'register-names))
3866 (push register-name gdb-register-names))
3867 (setq gdb-register-names (reverse gdb-register-names)))
3870 (defun gdb-get-source-file-list ()
3871 "Create list of source files for current GDB session.
3872 If buffers already exist for any of these files, gud-minor-mode
3873 is set in them."
3874 (goto-char (point-min))
3875 (while (re-search-forward gdb-source-file-regexp nil t)
3876 (push (match-string 1) gdb-source-file-list))
3877 (dolist (buffer (buffer-list))
3878 (with-current-buffer buffer
3879 (when (member buffer-file-name gdb-source-file-list)
3880 (gdb-init-buffer)))))
3882 (defun gdb-get-main-selected-frame ()
3883 "Trigger for `gdb-frame-handler' which uses main current
3884 thread. Called from `gdb-update'."
3885 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3886 (progn
3887 (gdb-input (gdb-current-context-command "-stack-info-frame")
3888 'gdb-frame-handler)
3889 (gdb-add-pending 'gdb-get-main-selected-frame))))
3891 (defun gdb-frame-handler ()
3892 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3893 overlay arrow in source buffer."
3894 (gdb-delete-pending 'gdb-get-main-selected-frame)
3895 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3896 (when frame
3897 (setq gdb-selected-frame (bindat-get-field frame 'func))
3898 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3899 (setq gdb-frame-number (bindat-get-field frame 'level))
3900 (setq gdb-frame-address (bindat-get-field frame 'addr))
3901 (let ((line (bindat-get-field frame 'line)))
3902 (setq gdb-selected-line (and line (string-to-number line)))
3903 (when (and gdb-selected-file gdb-selected-line)
3904 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3905 (gud-display-frame)))
3906 (if gud-overlay-arrow-position
3907 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3908 (position (marker-position gud-overlay-arrow-position)))
3909 (when buffer
3910 (with-current-buffer buffer
3911 (setq fringe-indicator-alist
3912 (if (string-equal gdb-frame-number "0")
3914 '((overlay-arrow . hollow-right-triangle))))
3915 (setq gud-overlay-arrow-position (make-marker))
3916 (set-marker gud-overlay-arrow-position position))))))))
3918 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3920 (defun gdb-get-prompt ()
3921 "Find prompt for GDB session."
3922 (goto-char (point-min))
3923 (setq gdb-prompt-name nil)
3924 (re-search-forward gdb-prompt-name-regexp nil t)
3925 (setq gdb-prompt-name (match-string 1))
3926 ;; Insert first prompt.
3927 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3929 ;;;; Window management
3930 (defun gdb-display-buffer (buf dedicated &optional frame)
3931 "Show buffer BUF.
3933 If BUF is already displayed in some window, show it, deiconifying
3934 the frame if necessary. Otherwise, find least recently used
3935 window and show BUF there, if the window is not used for GDB
3936 already, in which case that window is split first."
3937 (let ((answer (get-buffer-window buf (or frame 0))))
3938 (if answer
3939 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3940 (let ((window (get-lru-window)))
3941 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3942 'gdbmi)
3943 (let ((largest (get-largest-window)))
3944 (setq answer (split-window largest))
3945 (set-window-buffer answer buf)
3946 (set-window-dedicated-p answer dedicated)
3947 answer)
3948 (set-window-buffer window buf)
3949 window)))))
3951 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3952 "Find window displaying a buffer with the same
3953 `gdb-buffer-type' as BUF and show BUF there. If no such window
3954 exists, just call `gdb-display-buffer' for BUF. If the window
3955 found is already dedicated, split window according to
3956 SPLIT-HORIZONTAL and show BUF in the new window."
3957 (if buf
3958 (when (not (get-buffer-window buf))
3959 (let* ((buf-type (gdb-buffer-type buf))
3960 (existing-window
3961 (get-window-with-predicate
3962 #'(lambda (w)
3963 (and (eq buf-type
3964 (gdb-buffer-type (window-buffer w)))
3965 (not (window-dedicated-p w)))))))
3966 (if existing-window
3967 (set-window-buffer existing-window buf)
3968 (let ((dedicated-window
3969 (get-window-with-predicate
3970 #'(lambda (w)
3971 (eq buf-type
3972 (gdb-buffer-type (window-buffer w)))))))
3973 (if dedicated-window
3974 (set-window-buffer
3975 (split-window dedicated-window nil split-horizontal) buf)
3976 (gdb-display-buffer buf t))))))
3977 (error "Null buffer")))
3979 ;;; Shared keymap initialization:
3981 (let ((menu (make-sparse-keymap "GDB-Windows")))
3982 (define-key gud-menu-map [displays]
3983 `(menu-item "GDB-Windows" ,menu
3984 :visible (eq gud-minor-mode 'gdbmi)))
3985 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3986 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3987 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3988 (define-key menu [disassembly]
3989 '("Disassembly" . gdb-display-disassembly-buffer))
3990 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3991 (define-key menu [inferior]
3992 '("IO" . gdb-display-io-buffer))
3993 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3994 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3995 (define-key menu [breakpoints]
3996 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3998 (let ((menu (make-sparse-keymap "GDB-Frames")))
3999 (define-key gud-menu-map [frames]
4000 `(menu-item "GDB-Frames" ,menu
4001 :visible (eq gud-minor-mode 'gdbmi)))
4002 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4003 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4004 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4005 (define-key menu [disassembly]
4006 '("Disassembly" . gdb-frame-disassembly-buffer))
4007 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4008 (define-key menu [inferior]
4009 '("IO" . gdb-frame-io-buffer))
4010 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4011 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4012 (define-key menu [breakpoints]
4013 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4015 (let ((menu (make-sparse-keymap "GDB-MI")))
4016 (define-key menu [gdb-customize]
4017 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4018 :help "Customize Gdb Graphical Mode options."))
4019 (define-key menu [gdb-many-windows]
4020 '(menu-item "Display Other Windows" gdb-many-windows
4021 :help "Toggle display of locals, stack and breakpoint information"
4022 :button (:toggle . gdb-many-windows)))
4023 (define-key menu [gdb-restore-windows]
4024 '(menu-item "Restore Window Layout" gdb-restore-windows
4025 :help "Restore standard layout for debug session."))
4026 (define-key menu [sep1]
4027 '(menu-item "--"))
4028 (define-key menu [all-threads]
4029 '(menu-item "GUD controls all threads"
4030 (lambda ()
4031 (interactive)
4032 (setq gdb-gud-control-all-threads t))
4033 :help "GUD start/stop commands apply to all threads"
4034 :button (:radio . gdb-gud-control-all-threads)))
4035 (define-key menu [current-thread]
4036 '(menu-item "GUD controls current thread"
4037 (lambda ()
4038 (interactive)
4039 (setq gdb-gud-control-all-threads nil))
4040 :help "GUD start/stop commands apply to current thread only"
4041 :button (:radio . (not gdb-gud-control-all-threads))))
4042 (define-key menu [sep2]
4043 '(menu-item "--"))
4044 (define-key menu [gdb-customize-reasons]
4045 '(menu-item "Customize switching..."
4046 (lambda ()
4047 (interactive)
4048 (customize-option 'gdb-switch-reasons))))
4049 (define-key menu [gdb-switch-when-another-stopped]
4050 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4051 gdb-switch-when-another-stopped
4052 "Automatically switch to stopped thread"
4053 "GDB thread switching %s"
4054 "Switch to stopped thread"))
4055 (define-key gud-menu-map [mi]
4056 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4058 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4059 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4060 ;; tool-bar-local-item-from-menu here because it appends new buttons
4061 ;; to toolbar from right to left while we want our A/T throttle to
4062 ;; show up right before Run button.
4063 (define-key-after gud-tool-bar-map [all-threads]
4064 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4065 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4066 :visible (and (eq gud-minor-mode 'gdbmi)
4067 gdb-non-stop
4068 (not gdb-gud-control-all-threads)))
4069 'run)
4071 (define-key-after gud-tool-bar-map [current-thread]
4072 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4073 :image (find-image '((:type xpm :file "gud/all.xpm")))
4074 :visible (and (eq gud-minor-mode 'gdbmi)
4075 gdb-non-stop
4076 gdb-gud-control-all-threads))
4077 'all-threads)
4079 (defun gdb-frame-gdb-buffer ()
4080 "Display GUD buffer in a new frame."
4081 (interactive)
4082 (display-buffer-other-frame gud-comint-buffer))
4084 (defun gdb-display-gdb-buffer ()
4085 "Display GUD buffer."
4086 (interactive)
4087 (pop-to-buffer gud-comint-buffer nil 0))
4089 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4090 "Set buffer of selected window to NAME and dedicate window.
4092 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4093 window is dedicated."
4094 (unless window (setq window (selected-window)))
4095 (when ignore-dedicated
4096 (set-window-dedicated-p window nil))
4097 (set-window-buffer window (get-buffer name))
4098 (set-window-dedicated-p window t))
4100 (defun gdb-setup-windows ()
4101 "Layout the window pattern for `gdb-many-windows'."
4102 (gdb-display-locals-buffer)
4103 (gdb-display-stack-buffer)
4104 (delete-other-windows)
4105 (gdb-display-breakpoints-buffer)
4106 (delete-other-windows)
4107 ;; Don't dedicate.
4108 (switch-to-buffer gud-comint-buffer)
4109 (let ((win0 (selected-window))
4110 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4111 (win2 (split-window nil ( / (window-height) 3)))
4112 (win3 (split-window-right)))
4113 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4114 (select-window win2)
4115 (set-window-buffer
4116 win2
4117 (if gud-last-last-frame
4118 (gud-find-file (car gud-last-last-frame))
4119 (if gdb-main-file
4120 (gud-find-file gdb-main-file)
4121 ;; Put buffer list in window if we
4122 ;; can't find a source file.
4123 (list-buffers-noselect))))
4124 (setq gdb-source-window (selected-window))
4125 (let ((win4 (split-window-right)))
4126 (gdb-set-window-buffer
4127 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4128 (select-window win1)
4129 (gdb-set-window-buffer (gdb-stack-buffer-name))
4130 (let ((win5 (split-window-right)))
4131 (gdb-set-window-buffer (if gdb-show-threads-by-default
4132 (gdb-threads-buffer-name)
4133 (gdb-breakpoints-buffer-name))
4134 nil win5))
4135 (select-window win0)))
4137 (defcustom gdb-many-windows nil
4138 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4139 In this case it starts with two windows: one displaying the GUD
4140 buffer and the other with the source file with the main routine
4141 of the debugged program. Non-nil means display the layout shown for
4142 `gdb'."
4143 :type 'boolean
4144 :group 'gdb
4145 :version "22.1")
4147 (defun gdb-many-windows (arg)
4148 "Toggle the number of windows in the basic arrangement.
4149 With arg, display additional buffers iff arg is positive."
4150 (interactive "P")
4151 (setq gdb-many-windows
4152 (if (null arg)
4153 (not gdb-many-windows)
4154 (> (prefix-numeric-value arg) 0)))
4155 (message (format "Display of other windows %sabled"
4156 (if gdb-many-windows "en" "dis")))
4157 (if (and gud-comint-buffer
4158 (buffer-name gud-comint-buffer))
4159 (condition-case nil
4160 (gdb-restore-windows)
4161 (error nil))))
4163 (defun gdb-restore-windows ()
4164 "Restore the basic arrangement of windows used by gdb.
4165 This arrangement depends on the value of `gdb-many-windows'."
4166 (interactive)
4167 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4168 (delete-other-windows)
4169 (if gdb-many-windows
4170 (gdb-setup-windows)
4171 (when (or gud-last-last-frame gdb-show-main)
4172 (let ((win (split-window)))
4173 (set-window-buffer
4175 (if gud-last-last-frame
4176 (gud-find-file (car gud-last-last-frame))
4177 (gud-find-file gdb-main-file)))
4178 (setq gdb-source-window win)))))
4180 ;; Called from `gud-sentinel' in gud.el:
4181 (defun gdb-reset ()
4182 "Exit a debugging session cleanly.
4183 Kills the gdb buffers, and resets variables and the source buffers."
4184 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4185 ;; process. This pty must be deleted explicitly.
4186 (let ((pty (get-process "gdb-inferior")))
4187 (if pty (delete-process pty)))
4188 ;; Find gdb-mi buffers and kill them.
4189 (dolist (buffer (buffer-list))
4190 (unless (eq buffer gud-comint-buffer)
4191 (with-current-buffer buffer
4192 (if (eq gud-minor-mode 'gdbmi)
4193 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4194 (kill-buffer nil)
4195 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4196 (setq gud-minor-mode nil)
4197 (kill-local-variable 'tool-bar-map)
4198 (kill-local-variable 'gdb-define-alist))))))
4199 (setq gdb-disassembly-position nil)
4200 (setq overlay-arrow-variable-list
4201 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4202 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4203 (setq gdb-stack-position nil)
4204 (setq overlay-arrow-variable-list
4205 (delq 'gdb-stack-position overlay-arrow-variable-list))
4206 (setq gdb-thread-position nil)
4207 (setq overlay-arrow-variable-list
4208 (delq 'gdb-thread-position overlay-arrow-variable-list))
4209 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4210 (setq gud-running nil)
4211 (setq gdb-active-process nil)
4212 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4214 (defun gdb-get-source-file ()
4215 "Find the source file where the program starts and display it with related
4216 buffers, if required."
4217 (goto-char (point-min))
4218 (if (re-search-forward gdb-source-file-regexp nil t)
4219 (setq gdb-main-file (match-string 1)))
4220 (if gdb-many-windows
4221 (gdb-setup-windows)
4222 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4223 (if (and gdb-show-main gdb-main-file)
4224 (let ((pop-up-windows t))
4225 (display-buffer (gud-find-file gdb-main-file)))))
4226 (gdb-force-mode-line-update
4227 (propertize "ready" 'face font-lock-variable-name-face)))
4229 ;;from put-image
4230 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4231 "Put string PUTSTRING in front of POS in the current buffer.
4232 PUTSTRING is displayed by putting an overlay into the current buffer with a
4233 `before-string' string that has a `display' property whose value is
4234 PUTSTRING."
4235 (let ((string (make-string 1 ?x))
4236 (buffer (current-buffer)))
4237 (setq putstring (copy-sequence putstring))
4238 (let ((overlay (make-overlay pos pos buffer))
4239 (prop (or dprop
4240 (list (list 'margin 'left-margin) putstring))))
4241 (put-text-property 0 1 'display prop string)
4242 (if sprops
4243 (add-text-properties 0 1 sprops string))
4244 (overlay-put overlay 'put-break t)
4245 (overlay-put overlay 'before-string string))))
4247 ;;from remove-images
4248 (defun gdb-remove-strings (start end &optional buffer)
4249 "Remove strings between START and END in BUFFER.
4250 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4251 BUFFER nil or omitted means use the current buffer."
4252 (unless buffer
4253 (setq buffer (current-buffer)))
4254 (dolist (overlay (overlays-in start end))
4255 (when (overlay-get overlay 'put-break)
4256 (delete-overlay overlay))))
4258 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4259 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4260 (start (- (car posns) 1))
4261 (end (+ (cdr posns) 1))
4262 (putstring (if enabled "B" "b"))
4263 (source-window (get-buffer-window (current-buffer) 0)))
4264 (add-text-properties
4265 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4266 putstring)
4267 (if enabled
4268 (add-text-properties
4269 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4270 (add-text-properties
4271 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4272 (gdb-remove-breakpoint-icons start end)
4273 (if (display-images-p)
4274 (if (>= (or left-fringe-width
4275 (if source-window (car (window-fringes source-window)))
4276 gdb-buffer-fringe-width) 8)
4277 (gdb-put-string
4278 nil (1+ start)
4279 `(left-fringe breakpoint
4280 ,(if enabled
4281 'breakpoint-enabled
4282 'breakpoint-disabled))
4283 'gdb-bptno bptno
4284 'gdb-enabled enabled)
4285 (when (< left-margin-width 2)
4286 (save-current-buffer
4287 (setq left-margin-width 2)
4288 (if source-window
4289 (set-window-margins
4290 source-window
4291 left-margin-width right-margin-width))))
4292 (put-image
4293 (if enabled
4294 (or breakpoint-enabled-icon
4295 (setq breakpoint-enabled-icon
4296 (find-image `((:type xpm :data
4297 ,breakpoint-xpm-data
4298 :ascent 100 :pointer hand)
4299 (:type pbm :data
4300 ,breakpoint-enabled-pbm-data
4301 :ascent 100 :pointer hand)))))
4302 (or breakpoint-disabled-icon
4303 (setq breakpoint-disabled-icon
4304 (find-image `((:type xpm :data
4305 ,breakpoint-xpm-data
4306 :conversion disabled
4307 :ascent 100 :pointer hand)
4308 (:type pbm :data
4309 ,breakpoint-disabled-pbm-data
4310 :ascent 100 :pointer hand))))))
4311 (+ start 1)
4312 putstring
4313 'left-margin))
4314 (when (< left-margin-width 2)
4315 (save-current-buffer
4316 (setq left-margin-width 2)
4317 (let ((window (get-buffer-window (current-buffer) 0)))
4318 (if window
4319 (set-window-margins
4320 window left-margin-width right-margin-width)))))
4321 (gdb-put-string
4322 (propertize putstring
4323 'face (if enabled
4324 'breakpoint-enabled 'breakpoint-disabled))
4325 (1+ start)))))
4327 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4328 (gdb-remove-strings start end)
4329 (if (display-images-p)
4330 (remove-images start end))
4331 (when remove-margin
4332 (setq left-margin-width 0)
4333 (let ((window (get-buffer-window (current-buffer) 0)))
4334 (if window
4335 (set-window-margins
4336 window left-margin-width right-margin-width)))))
4339 ;;; Functions for inline completion.
4341 (defvar gud-gdb-fetch-lines-in-progress)
4342 (defvar gud-gdb-fetch-lines-string)
4343 (defvar gud-gdb-fetch-lines-break)
4344 (defvar gud-gdb-fetched-lines)
4346 (defun gud-gdbmi-completions (context command)
4347 "Completion table for GDB/MI commands.
4348 COMMAND is the prefix for which we seek completion.
4349 CONTEXT is the text before COMMAND on the line."
4350 (let ((gud-gdb-fetch-lines-in-progress t)
4351 (gud-gdb-fetch-lines-string nil)
4352 (gud-gdb-fetch-lines-break (length context))
4353 (gud-gdb-fetched-lines nil)
4354 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4355 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4356 complete-list)
4357 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4358 (gdb-input (concat "complete " context command)
4359 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4360 (while gud-gdb-fetch-lines-in-progress
4361 (accept-process-output (get-buffer-process gud-comint-buffer))))
4362 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4364 (defun gud-gdbmi-fetch-lines-filter (string)
4365 "Custom filter function for `gud-gdbmi-completions'."
4366 (setq string (concat gud-gdb-fetch-lines-string
4367 (gud-gdbmi-marker-filter string)))
4368 (while (string-match "\n" string)
4369 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4370 gud-gdb-fetched-lines)
4371 (setq string (substring string (match-end 0))))
4374 (provide 'gdb-mi)
4376 ;;; gdb-mi.el ends here