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