More CL cleanups and reduction of use of cl.el.
[emacs.git] / lisp / progmodes / gdb-mi.el
blob23a34b85194eef63c2b6c3ad8b022d9f0e7737a1
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-lib))
96 (declare-function speedbar-change-initial-expansion-list
97 "speedbar" (new-default))
98 (declare-function speedbar-timer-fn "speedbar" ())
99 (declare-function speedbar-line-text "speedbar" (&optional p))
100 (declare-function speedbar-change-expand-button-char "speedbar" (char))
101 (declare-function speedbar-delete-subblock "speedbar" (indent))
102 (declare-function speedbar-center-buffer-smartly "speedbar" ())
104 (defvar tool-bar-map)
105 (defvar speedbar-initial-expansion-list-name)
106 (defvar speedbar-frame)
108 (defvar gdb-memory-address "main")
109 (defvar gdb-memory-last-address nil
110 "Last successfully accessed memory address.")
111 (defvar gdb-memory-next-page nil
112 "Address of next memory page for program memory buffer.")
113 (defvar gdb-memory-prev-page nil
114 "Address of previous memory page for program memory buffer.")
116 (defvar gdb-thread-number nil
117 "Main current thread.
119 Invalidation triggers use this variable to query GDB for
120 information on the specified thread by wrapping GDB/MI commands
121 in `gdb-current-context-command'.
123 This variable may be updated implicitly by GDB via `gdb-stopped'
124 or explicitly by `gdb-select-thread'.
126 Only `gdb-setq-thread-number' should be used to change this
127 value.")
129 (defvar gdb-frame-number nil
130 "Selected frame level for main current thread.
132 Updated according to the following rules:
134 When a thread is selected or current thread stops, set to \"0\".
136 When current thread goes running (and possibly exits eventually),
137 set to nil.
139 May be manually changed by user with `gdb-select-frame'.")
141 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
143 ;; Used to show overlay arrow in source buffer. All set in
144 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
145 ;; these but rely on buffer-local thread information instead.
146 (defvar gdb-selected-frame nil
147 "Name of selected function for main current thread.")
148 (defvar gdb-selected-file nil
149 "Name of selected file for main current thread.")
150 (defvar gdb-selected-line nil
151 "Number of selected line for main current thread.")
153 (defvar gdb-threads-list nil
154 "Associative list of threads provided by \"-thread-info\" MI command.
156 Keys are thread numbers (in strings) and values are structures as
157 returned from -thread-info by `gdb-json-partial-output'. Updated in
158 `gdb-thread-list-handler-custom'.")
160 (defvar gdb-running-threads-count nil
161 "Number of currently running threads.
163 If nil, no information is available.
165 Updated in `gdb-thread-list-handler-custom'.")
167 (defvar gdb-stopped-threads-count nil
168 "Number of currently stopped threads.
170 See also `gdb-running-threads-count'.")
172 (defvar gdb-breakpoints-list nil
173 "Associative list of breakpoints provided by \"-break-list\" MI command.
175 Keys are breakpoint numbers (in string) and values are structures
176 as returned from \"-break-list\" by `gdb-json-partial-output'
177 \(\"body\" field is used). Updated in
178 `gdb-breakpoints-list-handler-custom'.")
180 (defvar gdb-current-language nil)
181 (defvar gdb-var-list nil
182 "List of variables in watch window.
183 Each element has the form
184 (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
185 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
186 address for root variables.")
187 (defvar gdb-main-file nil "Source file from which program execution begins.")
189 ;; Overlay arrow markers
190 (defvar gdb-stack-position nil)
191 (defvar gdb-thread-position nil)
192 (defvar gdb-disassembly-position nil)
194 (defvar gdb-location-alist nil
195 "Alist of breakpoint numbers and full filenames. Only used for files that
196 Emacs can't find.")
197 (defvar gdb-active-process nil
198 "GUD tooltips display variable values when t, and macro definitions otherwise.")
199 (defvar gdb-error "Non-nil when GDB is reporting an error.")
200 (defvar gdb-macro-info nil
201 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
202 (defvar gdb-register-names nil "List of register names.")
203 (defvar gdb-changed-registers nil
204 "List of changed register numbers (strings).")
205 (defvar gdb-buffer-fringe-width nil)
206 (defvar gdb-last-command nil)
207 (defvar gdb-prompt-name nil)
208 (defvar gdb-token-number 0)
209 (defvar gdb-handler-alist '())
210 (defvar gdb-handler-number nil)
211 (defvar gdb-source-file-list nil
212 "List of source files for the current executable.")
213 (defvar gdb-first-done-or-error t)
214 (defvar gdb-source-window nil)
215 (defvar gdb-inferior-status nil)
216 (defvar gdb-continuation nil)
217 (defvar gdb-supports-non-stop nil)
218 (defvar gdb-filter-output nil
219 "Message to be shown in GUD console.
221 This variable is updated in `gdb-done-or-error' and returned by
222 `gud-gdbmi-marker-filter'.")
224 (defvar gdb-non-stop nil
225 "Indicates whether current GDB session is using non-stop mode.
227 It is initialized to `gdb-non-stop-setting' at the beginning of
228 every GDB session.")
230 (defvar gdb-buffer-type nil
231 "One of the symbols bound in `gdb-buffer-rules'.")
232 (make-variable-buffer-local 'gdb-buffer-type)
234 (defvar gdb-output-sink 'nil
235 "The disposition of the output of the current gdb command.
236 Possible values are these symbols:
238 `user' -- gdb output should be copied to the GUD buffer
239 for the user to see.
241 `emacs' -- output should be collected in the partial-output-buffer
242 for subsequent processing by a command. This is the
243 disposition of output generated by commands that
244 gdb mode sends to gdb on its own behalf.")
246 ;; Pending triggers prevent congestion: Emacs won't send two similar
247 ;; consecutive requests.
249 (defvar gdb-pending-triggers '()
250 "A list of trigger functions which have not yet been handled.
252 Elements are either function names or pairs (buffer . function)")
254 (defmacro gdb-add-pending (item)
255 `(push ,item gdb-pending-triggers))
256 (defmacro gdb-pending-p (item)
257 `(member ,item gdb-pending-triggers))
258 (defmacro gdb-delete-pending (item)
259 `(setq gdb-pending-triggers
260 (delete ,item gdb-pending-triggers)))
262 (defmacro gdb-wait-for-pending (&rest body)
263 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
265 This function checks `gdb-pending-triggers' value every
266 `gdb-wait-for-pending' seconds."
267 (run-with-timer
268 0.5 nil
269 `(lambda ()
270 (if (not gdb-pending-triggers)
271 (progn ,@body)
272 (gdb-wait-for-pending ,@body)))))
274 ;; Publish-subscribe
276 (defmacro gdb-add-subscriber (publisher subscriber)
277 "Register new PUBLISHER's SUBSCRIBER.
279 SUBSCRIBER must be a pair, where cdr is a function of one
280 argument (see `gdb-emit-signal')."
281 `(add-to-list ',publisher ,subscriber t))
283 (defmacro gdb-delete-subscriber (publisher subscriber)
284 "Unregister SUBSCRIBER from PUBLISHER."
285 `(setq ,publisher (delete ,subscriber
286 ,publisher)))
288 (defun gdb-get-subscribers (publisher)
289 publisher)
291 (defun gdb-emit-signal (publisher &optional signal)
292 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
293 (dolist (subscriber (gdb-get-subscribers publisher))
294 (funcall (cdr subscriber) signal)))
296 (defvar gdb-buf-publisher '()
297 "Used to invalidate GDB buffers by emitting a signal in
298 `gdb-update'.
300 Must be a list of pairs with cars being buffers and cdr's being
301 valid signal handlers.")
303 (defgroup gdb nil
304 "GDB graphical interface"
305 :group 'tools
306 :link '(info-link "(emacs)GDB Graphical Interface")
307 :version "23.2")
309 (defgroup gdb-non-stop nil
310 "GDB non-stop debugging settings"
311 :group 'gdb
312 :version "23.2")
314 (defgroup gdb-buffers nil
315 "GDB buffers"
316 :group 'gdb
317 :version "23.2")
319 (defcustom gdb-debug-log-max 128
320 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
321 :group 'gdb
322 :type '(choice (integer :tag "Number of elements")
323 (const :tag "Unlimited" nil))
324 :version "22.1")
326 (defcustom gdb-non-stop-setting t
327 "When in non-stop mode, stopped threads can be examined while
328 other threads continue to execute.
330 GDB session needs to be restarted for this setting to take
331 effect."
332 :type 'boolean
333 :group 'gdb-non-stop
334 :version "23.2")
336 ;; TODO Some commands can't be called with --all (give a notice about
337 ;; it in setting doc)
338 (defcustom gdb-gud-control-all-threads t
339 "When enabled, GUD execution commands affect all threads when
340 in non-stop mode. Otherwise, only current thread is affected."
341 :type 'boolean
342 :group 'gdb-non-stop
343 :version "23.2")
345 (defcustom gdb-switch-reasons t
346 "List of stop reasons which cause Emacs to switch to the thread
347 which caused the stop. When t, switch to stopped thread no matter
348 what the reason was. When nil, never switch to stopped thread
349 automatically.
351 This setting is used in non-stop mode only. In all-stop mode,
352 Emacs always switches to the thread which caused the stop."
353 ;; exited, exited-normally and exited-signaled are not
354 ;; thread-specific stop reasons and therefore are not included in
355 ;; this list
356 :type '(choice
357 (const :tag "All reasons" t)
358 (set :tag "Selection of reasons..."
359 (const :tag "A breakpoint was reached." "breakpoint-hit")
360 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
361 (const :tag "A read watchpoint was triggered."
362 "read-watchpoint-trigger")
363 (const :tag "An access watchpoint was triggered."
364 "access-watchpoint-trigger")
365 (const :tag "Function finished execution." "function-finished")
366 (const :tag "Location reached." "location-reached")
367 (const :tag "Watchpoint has gone out of scope"
368 "watchpoint-scope")
369 (const :tag "End of stepping range reached."
370 "end-stepping-range")
371 (const :tag "Signal received (like interruption)."
372 "signal-received"))
373 (const :tag "None" nil))
374 :group 'gdb-non-stop
375 :version "23.2"
376 :link '(info-link "(gdb)GDB/MI Async Records"))
378 (defcustom gdb-stopped-functions nil
379 "List of functions called whenever GDB stops.
381 Each function takes one argument, a parsed MI response, which
382 contains fields of corresponding MI *stopped async record:
384 ((stopped-threads . \"all\")
385 (thread-id . \"1\")
386 (frame (line . \"38\")
387 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
388 (file . \"server.c\")
389 (args ((value . \"0x804b038\")
390 (name . \"arg\")))
391 (func . \"hello\")
392 (addr . \"0x0804869e\"))
393 (reason . \"end-stepping-range\"))
395 Note that \"reason\" is only present in non-stop debugging mode.
397 `bindat-get-field' may be used to access the fields of response.
399 Each function is called after the new current thread was selected
400 and GDB buffers were updated in `gdb-stopped'."
401 :type '(repeat function)
402 :group 'gdb
403 :version "23.2"
404 :link '(info-link "(gdb)GDB/MI Async Records"))
406 (defcustom gdb-switch-when-another-stopped t
407 "When nil, Emacs won't switch to stopped thread if some other
408 stopped thread is already selected."
409 :type 'boolean
410 :group 'gdb-non-stop
411 :version "23.2")
413 (defcustom gdb-stack-buffer-locations t
414 "Show file information or library names in stack buffers."
415 :type 'boolean
416 :group 'gdb-buffers
417 :version "23.2")
419 (defcustom gdb-stack-buffer-addresses nil
420 "Show frame addresses in stack buffers."
421 :type 'boolean
422 :group 'gdb-buffers
423 :version "23.2")
425 (defcustom gdb-thread-buffer-verbose-names t
426 "Show long thread names in threads buffer."
427 :type 'boolean
428 :group 'gdb-buffers
429 :version "23.2")
431 (defcustom gdb-thread-buffer-arguments t
432 "Show function arguments in threads buffer."
433 :type 'boolean
434 :group 'gdb-buffers
435 :version "23.2")
437 (defcustom gdb-thread-buffer-locations t
438 "Show file information or library names in threads buffer."
439 :type 'boolean
440 :group 'gdb-buffers
441 :version "23.2")
443 (defcustom gdb-thread-buffer-addresses nil
444 "Show addresses for thread frames in threads buffer."
445 :type 'boolean
446 :group 'gdb-buffers
447 :version "23.2")
449 (defcustom gdb-show-threads-by-default nil
450 "Show threads list buffer instead of breakpoints list by
451 default."
452 :type 'boolean
453 :group 'gdb-buffers
454 :version "23.2")
456 (defvar gdb-debug-log nil
457 "List of commands sent to and replies received from GDB.
458 Most recent commands are listed first. This list stores only the last
459 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
461 ;;;###autoload
462 (define-minor-mode gdb-enable-debug
463 "Toggle logging of transaction between Emacs and Gdb.
464 The log is stored in `gdb-debug-log' as an alist with elements
465 whose cons is send, send-item or recv and whose cdr is the string
466 being transferred. This list may grow up to a size of
467 `gdb-debug-log-max' after which the oldest element (at the end of
468 the list) is deleted every time a new one is added (at the front)."
469 :global t
470 :group 'gdb
471 :version "22.1")
473 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
474 "Shell command for generating a list of defined macros in a source file.
475 This list is used to display the #define directive associated
476 with an identifier as a tooltip. It works in a debug session with
477 GDB, when `gud-tooltip-mode' is t.
479 Set `gdb-cpp-define-alist-flags' for any include paths or
480 predefined macros."
481 :type 'string
482 :group 'gdb
483 :version "22.1")
485 (defcustom gdb-cpp-define-alist-flags ""
486 "Preprocessor flags for `gdb-cpp-define-alist-program'."
487 :type 'string
488 :group 'gdb
489 :version "22.1")
491 (defcustom gdb-create-source-file-list t
492 "Non-nil means create a list of files from which the executable was built.
493 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
494 line for a long time when starting, possibly because your executable was
495 built from a large number of files. This allows quicker initialization
496 but means that these files are not automatically enabled for debugging,
497 e.g., you won't be able to click in the fringe to set a breakpoint until
498 execution has already stopped there."
499 :type 'boolean
500 :group 'gdb
501 :version "23.1")
503 (defcustom gdb-show-main nil
504 "Non-nil means display source file containing the main routine at startup.
505 Also display the main routine in the disassembly buffer if present."
506 :type 'boolean
507 :group 'gdb
508 :version "22.1")
510 (defun gdb-force-mode-line-update (status)
511 (let ((buffer gud-comint-buffer))
512 (if (and buffer (buffer-name buffer))
513 (with-current-buffer buffer
514 (setq mode-line-process
515 (format ":%s [%s]"
516 (process-status (get-buffer-process buffer)) status))
517 ;; Force mode line redisplay soon.
518 (force-mode-line-update)))))
520 ;; These two are used for menu and toolbar
521 (defun gdb-control-all-threads ()
522 "Switch to non-stop/A mode."
523 (interactive)
524 (setq gdb-gud-control-all-threads t)
525 ;; Actually forcing the tool-bar to update.
526 (force-mode-line-update)
527 (message "Now in non-stop/A mode."))
529 (defun gdb-control-current-thread ()
530 "Switch to non-stop/T mode."
531 (interactive)
532 (setq gdb-gud-control-all-threads nil)
533 ;; Actually forcing the tool-bar to update.
534 (force-mode-line-update)
535 (message "Now in non-stop/T mode."))
537 (defun gdb-find-watch-expression ()
538 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
539 (varnum (car var)) expr)
540 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
541 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
542 (component-list (split-string (match-string 2 varnum) "\\." t)))
543 (setq expr (nth 1 var1))
544 (setq varnumlet (car var1))
545 (dolist (component component-list)
546 (setq var2 (assoc varnumlet gdb-var-list))
547 (setq expr (concat expr
548 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
549 (concat "[" component "]")
550 (concat "." component))))
551 (setq varnumlet (concat varnumlet "." component)))
552 expr)))
554 ;; noall is used for commands which don't take --all, but only
555 ;; --thread.
556 (defun gdb-gud-context-command (command &optional noall)
557 "When `gdb-non-stop' is t, add --thread option to COMMAND if
558 `gdb-gud-control-all-threads' is nil and --all option otherwise.
559 If NOALL is t, always add --thread option no matter what
560 `gdb-gud-control-all-threads' value is.
562 When `gdb-non-stop' is nil, return COMMAND unchanged."
563 (if gdb-non-stop
564 (if (and gdb-gud-control-all-threads
565 (not noall)
566 gdb-supports-non-stop)
567 (concat command " --all ")
568 (gdb-current-context-command command))
569 command))
571 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
572 "`gud-call' wrapper which adds --thread/--all options between
573 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
575 NOARG must be t when this macro is used outside `gud-def'"
576 `(gud-call
577 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
578 ,(when (not noarg) 'arg)))
580 (defun gdb--check-interpreter (proc string)
581 (unless (zerop (length string))
582 (let ((filter (process-get proc 'gud-normal-filter)))
583 (set-process-filter proc filter)
584 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
585 ;; Apparently we're not running with -i=mi.
586 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
587 (message msg)
588 (setq string (concat (propertize msg 'font-lock-face 'error)
589 "\n" string)))
590 ;; Use the old gud-gbd filter, not because it works, but because it
591 ;; will properly display GDB's answers rather than hanging waiting for
592 ;; answers that aren't coming.
593 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
594 (funcall filter proc string))))
596 (defvar gdb-control-level 0)
598 ;;;###autoload
599 (defun gdb (command-line)
600 "Run gdb on program FILE in buffer *gud-FILE*.
601 The directory containing FILE becomes the initial working directory
602 and source-file directory for your debugger.
604 COMMAND-LINE is the shell command for starting the gdb session.
605 It should be a string consisting of the name of the gdb
606 executable followed by command-line options. The command-line
607 options should include \"-i=mi\" to use gdb's MI text interface.
608 Note that the old \"--annotate\" option is no longer supported.
610 If `gdb-many-windows' is nil (the default value) then gdb just
611 pops up the GUD buffer unless `gdb-show-main' is t. In this case
612 it starts with two windows: one displaying the GUD buffer and the
613 other with the source file with the main routine of the inferior.
615 If `gdb-many-windows' is t, regardless of the value of
616 `gdb-show-main', the layout below will appear. Keybindings are
617 shown in some of the buffers.
619 Watch expressions appear in the speedbar/slowbar.
621 The following commands help control operation :
623 `gdb-many-windows' - Toggle the number of windows gdb uses.
624 `gdb-restore-windows' - To restore the window layout.
626 See Info node `(emacs)GDB Graphical Interface' for a more
627 detailed description of this mode.
630 +----------------------------------------------------------------------+
631 | GDB Toolbar |
632 +-----------------------------------+----------------------------------+
633 | GUD buffer (I/O of GDB) | Locals buffer |
634 | | |
635 | | |
636 | | |
637 +-----------------------------------+----------------------------------+
638 | Source buffer | I/O buffer (of debugged program) |
639 | | (comint-mode) |
640 | | |
641 | | |
642 | | |
643 | | |
644 | | |
645 | | |
646 +-----------------------------------+----------------------------------+
647 | Stack buffer | Breakpoints buffer |
648 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
649 | | RET gdb-goto-breakpoint |
650 | | D gdb-delete-breakpoint |
651 +-----------------------------------+----------------------------------+"
653 (interactive (list (gud-query-cmdline 'gdb)))
655 (when (and gud-comint-buffer
656 (buffer-name gud-comint-buffer)
657 (get-buffer-process gud-comint-buffer)
658 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
659 (gdb-restore-windows)
660 (error
661 "Multiple debugging requires restarting in text command mode"))
663 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
665 ;; Setup a temporary process filter to warn when GDB was not started
666 ;; with -i=mi.
667 (let ((proc (get-buffer-process gud-comint-buffer)))
668 (process-put proc 'gud-normal-filter (process-filter proc))
669 (set-process-filter proc #'gdb--check-interpreter))
671 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
672 (set (make-local-variable 'gdb-control-level) 0)
673 (setq comint-input-sender 'gdb-send)
674 (when (ring-empty-p comint-input-ring) ; cf shell-mode
675 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
676 (if (eq system-type 'ms-dos)
677 "_gdb_history"
678 ".gdb_history"))))
679 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
680 (hsize (getenv "HISTSIZE")))
681 (dolist (file (append '("~/.gdbinit")
682 (unless (string-equal (expand-file-name ".")
683 (expand-file-name "~"))
684 '(".gdbinit"))))
685 (if (file-readable-p (setq file (expand-file-name file)))
686 (with-temp-buffer
687 (insert-file-contents file)
688 ;; TODO? check for "set history save\\( *on\\)?" and do
689 ;; not use history otherwise?
690 (while (re-search-forward
691 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
692 (cond ((string-equal (match-string 1) "filename")
693 (setq hfile (expand-file-name
694 (match-string 2)
695 (file-name-directory file))))
696 ((string-equal (match-string 1) "size")
697 (setq hsize (match-string 2))))))))
698 (and (stringp hsize)
699 (integerp (setq hsize (string-to-number hsize)))
700 (> hsize 0)
701 (set (make-local-variable 'comint-input-ring-size) hsize))
702 (if (stringp hfile)
703 (set (make-local-variable 'comint-input-ring-file-name) hfile))
704 (comint-read-input-ring t)))
705 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
706 "Set temporary breakpoint at current line.")
707 (gud-def gud-jump
708 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
709 "\C-j" "Set execution address to current line.")
711 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
712 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
713 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
714 (gud-def gud-pstar "print* %e" nil
715 "Evaluate C dereferenced pointer expression at point.")
717 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
718 "\C-s"
719 "Step one source line with display.")
720 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
721 "\C-i"
722 "Step one instruction with display.")
723 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
724 "\C-n"
725 "Step one line (skip functions).")
726 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
728 "Step one instruction (skip functions).")
729 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
730 "\C-r"
731 "Continue with display.")
732 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
733 "\C-f"
734 "Finish executing current function.")
735 (gud-def gud-run "-exec-run"
737 "Run the program.")
739 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
740 (gud-call "break %f:%l" arg)
741 (save-excursion
742 (beginning-of-line)
743 (forward-char 2)
744 (gud-call "break *%a" arg)))
745 "\C-b" "Set breakpoint at current line or address.")
747 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
748 (gud-call "clear %f:%l" arg)
749 (save-excursion
750 (beginning-of-line)
751 (forward-char 2)
752 (gud-call "clear *%a" arg)))
753 "\C-d" "Remove breakpoint at current line or address.")
755 ;; -exec-until doesn't support --all yet
756 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
757 (gud-call "-exec-until %f:%l" arg)
758 (save-excursion
759 (beginning-of-line)
760 (forward-char 2)
761 (gud-call "-exec-until *%a" arg)))
762 "\C-u" "Continue to current line or address.")
763 ;; TODO Why arg here?
764 (gud-def
765 gud-go (gud-call (if gdb-active-process
766 (gdb-gud-context-command "-exec-continue")
767 "-exec-run") arg)
768 nil "Start or continue execution.")
770 ;; For debugging Emacs only.
771 (gud-def gud-pp
772 (gud-call
773 (concat
774 "pp " (if (eq (buffer-local-value
775 'major-mode (window-buffer)) 'speedbar-mode)
776 (gdb-find-watch-expression) "%e")) arg)
777 nil "Print the Emacs s-expression.")
779 (define-key gud-minor-mode-map [left-margin mouse-1]
780 'gdb-mouse-set-clear-breakpoint)
781 (define-key gud-minor-mode-map [left-fringe mouse-1]
782 'gdb-mouse-set-clear-breakpoint)
783 (define-key gud-minor-mode-map [left-margin C-mouse-1]
784 'gdb-mouse-toggle-breakpoint-margin)
785 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
786 'gdb-mouse-toggle-breakpoint-fringe)
788 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
789 'gdb-mouse-until)
790 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
791 'gdb-mouse-until)
792 (define-key gud-minor-mode-map [left-margin mouse-3]
793 'gdb-mouse-until)
794 (define-key gud-minor-mode-map [left-fringe mouse-3]
795 'gdb-mouse-until)
797 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
798 'gdb-mouse-jump)
799 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
800 'gdb-mouse-jump)
801 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
802 'gdb-mouse-jump)
803 (define-key gud-minor-mode-map [left-margin C-mouse-3]
804 'gdb-mouse-jump)
806 (set (make-local-variable 'gud-gdb-completion-function)
807 'gud-gdbmi-completions)
809 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
810 nil 'local)
811 (local-set-key "\C-i" 'completion-at-point)
813 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
815 (setq gdb-first-prompt t)
816 (setq gud-running nil)
818 (gdb-update)
820 (run-hooks 'gdb-mode-hook))
822 (defun gdb-init-1 ()
823 ;; (Re-)initialize.
824 (setq gdb-selected-frame nil
825 gdb-frame-number nil
826 gdb-thread-number nil
827 gdb-var-list nil
828 gdb-pending-triggers nil
829 gdb-output-sink 'user
830 gdb-location-alist nil
831 gdb-source-file-list nil
832 gdb-last-command nil
833 gdb-token-number 0
834 gdb-handler-alist '()
835 gdb-handler-number nil
836 gdb-prompt-name nil
837 gdb-first-done-or-error t
838 gdb-buffer-fringe-width (car (window-fringes))
839 gdb-debug-log nil
840 gdb-source-window nil
841 gdb-inferior-status nil
842 gdb-continuation nil
843 gdb-buf-publisher '()
844 gdb-threads-list '()
845 gdb-breakpoints-list '()
846 gdb-register-names '()
847 gdb-non-stop gdb-non-stop-setting)
849 (setq gdb-buffer-type 'gdbmi)
851 (gdb-force-mode-line-update
852 (propertize "initializing..." 'face font-lock-variable-name-face))
854 (gdb-get-buffer-create 'gdb-inferior-io)
855 (gdb-clear-inferior-io)
856 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
858 (when (eq system-type 'windows-nt)
859 ;; Don't create a separate console window for the debuggee.
860 (gdb-input "-gdb-set new-console off" 'ignore)
861 ;; Force GDB to behave as if its input and output stream were
862 ;; connected to a TTY device (since on Windows we use pipes for
863 ;; communicating with GDB).
864 (gdb-input "-gdb-set interactive-mode on" 'ignore))
865 (gdb-input "-gdb-set height 0" 'ignore)
867 (when gdb-non-stop
868 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
870 (gdb-input "-enable-pretty-printing" 'ignore)
872 ;; Find source file and compilation directory here.
873 (if gdb-create-source-file-list
874 ;; Needs GDB 6.2 onwards.
875 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
876 ;; Needs GDB 6.0 onwards.
877 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
878 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
880 (defun gdb-non-stop-handler ()
881 (goto-char (point-min))
882 (if (re-search-forward "No symbol" nil t)
883 (progn
884 (message
885 "This version of GDB doesn't support non-stop mode. Turning it off.")
886 (setq gdb-non-stop nil)
887 (setq gdb-supports-non-stop nil))
888 (setq gdb-supports-non-stop t)
889 (gdb-input "-gdb-set target-async 1" 'ignore)
890 (gdb-input "-list-target-features" 'gdb-check-target-async)))
892 (defun gdb-check-target-async ()
893 (goto-char (point-min))
894 (unless (re-search-forward "async" nil t)
895 (message
896 "Target doesn't support non-stop mode. Turning it off.")
897 (setq gdb-non-stop nil)
898 (gdb-input "-gdb-set non-stop 0" 'ignore)))
900 (defun gdb-delchar-or-quit (arg)
901 "Delete ARG characters or send a quit command to GDB.
902 Send a quit only if point is at the end of the buffer, there is
903 no input, and GDB is waiting for input."
904 (interactive "p")
905 (unless (and (eq (current-buffer) gud-comint-buffer)
906 (eq gud-minor-mode 'gdbmi))
907 (error "Not in a GDB-MI buffer"))
908 (let ((proc (get-buffer-process gud-comint-buffer)))
909 (if (and (eobp) proc (process-live-p proc)
910 (not gud-running)
911 (= (point) (marker-position (process-mark proc))))
912 ;; Sending an EOF does not work with GDB-MI; submit an
913 ;; explicit quit command.
914 (progn
915 (insert "quit")
916 (comint-send-input t t))
917 (delete-char arg))))
919 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
921 (defun gdb-create-define-alist ()
922 "Create an alist of #define directives for GUD tooltips."
923 (let* ((file (buffer-file-name))
924 (output
925 (with-output-to-string
926 (with-current-buffer standard-output
927 (and file
928 (file-exists-p file)
929 ;; call-process doesn't work with remote file names.
930 (not (file-remote-p default-directory))
931 (call-process shell-file-name file
932 (list t nil) nil "-c"
933 (concat gdb-cpp-define-alist-program " "
934 gdb-cpp-define-alist-flags))))))
935 (define-list (split-string output "\n" t))
936 (name))
937 (setq gdb-define-alist nil)
938 (dolist (define define-list)
939 (setq name (nth 1 (split-string define "[( ]")))
940 (push (cons name define) gdb-define-alist))))
942 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
944 (defun gdb-tooltip-print (expr)
945 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
946 (goto-char (point-min))
947 (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 (cl-defstruct gdb-table
2273 (column-sizes nil)
2274 (rows nil)
2275 (row-properties nil)
2276 (right-align nil))
2278 (defun gdb-mapcar* (function &rest seqs)
2279 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2280 If there are several SEQS, FUNCTION is called with that many
2281 arguments, and mapping stops as soon as the shortest list runs
2282 out."
2283 (let ((shortest (apply #'min (mapcar #'length seqs))))
2284 (mapcar (lambda (i)
2285 (apply function
2286 (mapcar
2287 (lambda (seq)
2288 (nth i seq))
2289 seqs)))
2290 (number-sequence 0 (1- shortest)))))
2292 (defun gdb-table-add-row (table row &optional properties)
2293 "Add ROW of string to TABLE and recalculate column sizes.
2295 When non-nil, PROPERTIES will be added to the whole row when
2296 calling `gdb-table-string'."
2297 (let ((rows (gdb-table-rows table))
2298 (row-properties (gdb-table-row-properties table))
2299 (column-sizes (gdb-table-column-sizes table))
2300 (right-align (gdb-table-right-align table)))
2301 (when (not column-sizes)
2302 (setf (gdb-table-column-sizes table)
2303 (make-list (length row) 0)))
2304 (setf (gdb-table-rows table)
2305 (append rows (list row)))
2306 (setf (gdb-table-row-properties table)
2307 (append row-properties (list properties)))
2308 (setf (gdb-table-column-sizes table)
2309 (gdb-mapcar* (lambda (x s)
2310 (let ((new-x
2311 (max (abs x) (string-width (or s "")))))
2312 (if right-align new-x (- new-x))))
2313 (gdb-table-column-sizes table)
2314 row))
2315 ;; Avoid trailing whitespace at eol
2316 (if (not (gdb-table-right-align table))
2317 (setcar (last (gdb-table-column-sizes table)) 0))))
2319 (defun gdb-table-string (table &optional sep)
2320 "Return TABLE as a string with columns separated with SEP."
2321 (let ((column-sizes (gdb-table-column-sizes table)))
2322 (mapconcat
2323 'identity
2324 (gdb-mapcar*
2325 (lambda (row properties)
2326 (apply 'propertize
2327 (mapconcat 'identity
2328 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2329 row column-sizes)
2330 sep)
2331 properties))
2332 (gdb-table-rows table)
2333 (gdb-table-row-properties table))
2334 "\n")))
2336 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2337 (defun gdb-get-many-fields (struct &rest fields)
2338 "Return a list of FIELDS values from STRUCT."
2339 (let ((values))
2340 (dolist (field fields)
2341 (push (bindat-get-field struct field) values))
2342 (nreverse values)))
2344 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2345 handler-name
2346 &optional signal-list)
2347 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2348 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2349 buffer with `gdb-bind-function-to-buffer'.
2351 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2352 defined trigger is called with an argument from SIGNAL-LIST. It's
2353 not recommended to define triggers with empty SIGNAL-LIST.
2354 Normally triggers should respond at least to 'update signal.
2356 Normally the trigger defined by this command must be called from
2357 the buffer where HANDLER-NAME must work. This should be done so
2358 that buffer-local thread number may be used in GDB-COMMAND (by
2359 calling `gdb-current-context-command').
2360 `gdb-bind-function-to-buffer' is used to achieve this, see
2361 `gdb-get-buffer-create'.
2363 Triggers defined by this command are meant to be used as a
2364 trigger argument when describing buffer types with
2365 `gdb-set-buffer-rules'."
2366 `(defun ,trigger-name (&optional signal)
2367 (when
2368 (or (not ,signal-list)
2369 (memq signal ,signal-list))
2370 (when (not (gdb-pending-p
2371 (cons (current-buffer) ',trigger-name)))
2372 (gdb-input ,gdb-command
2373 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2374 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2376 ;; Used by disassembly buffer only, the rest use
2377 ;; def-gdb-trigger-and-handler
2378 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2379 &optional nopreserve)
2380 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2382 Handlers are normally called from the buffers they put output in.
2384 Delete ((current-buffer) . TRIGGER-NAME) from
2385 `gdb-pending-triggers', erase current buffer and evaluate
2386 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2388 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2389 `(defun ,handler-name ()
2390 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2391 (let* ((buffer-read-only nil)
2392 (window (get-buffer-window (current-buffer) 0))
2393 (start (window-start window))
2394 (p (window-point window)))
2395 (erase-buffer)
2396 (,custom-defun)
2397 (gdb-update-buffer-name)
2398 ,(when (not nopreserve)
2399 '(set-window-start window start)
2400 '(set-window-point window p)))))
2402 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2403 handler-name custom-defun
2404 &optional signal-list)
2405 "Define trigger and handler.
2407 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2408 `def-gdb-auto-update-trigger'.
2410 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2411 `def-gdb-auto-update-handler'."
2412 `(progn
2413 (def-gdb-auto-update-trigger ,trigger-name
2414 ,gdb-command
2415 ,handler-name ,signal-list)
2416 (def-gdb-auto-update-handler ,handler-name
2417 ,trigger-name ,custom-defun)))
2421 ;; Breakpoint buffer : This displays the output of `-break-list'.
2422 (def-gdb-trigger-and-handler
2423 gdb-invalidate-breakpoints "-break-list"
2424 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2425 '(start update))
2427 (gdb-set-buffer-rules
2428 'gdb-breakpoints-buffer
2429 'gdb-breakpoints-buffer-name
2430 'gdb-breakpoints-mode
2431 'gdb-invalidate-breakpoints)
2433 (defun gdb-breakpoints-list-handler-custom ()
2434 (let ((breakpoints-list (bindat-get-field
2435 (gdb-json-partial-output "bkpt" "script")
2436 'BreakpointTable 'body))
2437 (table (make-gdb-table)))
2438 (setq gdb-breakpoints-list nil)
2439 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2440 (dolist (breakpoint breakpoints-list)
2441 (add-to-list 'gdb-breakpoints-list
2442 (cons (bindat-get-field breakpoint 'number)
2443 breakpoint))
2444 (let ((at (bindat-get-field breakpoint 'at))
2445 (pending (bindat-get-field breakpoint 'pending))
2446 (func (bindat-get-field breakpoint 'func))
2447 (type (bindat-get-field breakpoint 'type)))
2448 (gdb-table-add-row table
2449 (list
2450 (bindat-get-field breakpoint 'number)
2451 (or type "")
2452 (or (bindat-get-field breakpoint 'disp) "")
2453 (let ((flag (bindat-get-field breakpoint 'enabled)))
2454 (if (string-equal flag "y")
2455 (propertize "y" 'font-lock-face font-lock-warning-face)
2456 (propertize "n" 'font-lock-face font-lock-comment-face)))
2457 (bindat-get-field breakpoint 'addr)
2458 (or (bindat-get-field breakpoint 'times) "")
2459 (if (and type (string-match ".*watchpoint" type))
2460 (bindat-get-field breakpoint 'what)
2461 (or pending at
2462 (concat "in "
2463 (propertize (or func "unknown")
2464 'font-lock-face font-lock-function-name-face)
2465 (gdb-frame-location breakpoint)))))
2466 ;; Add clickable properties only for breakpoints with file:line
2467 ;; information
2468 (append (list 'gdb-breakpoint breakpoint)
2469 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2470 mouse-face highlight))))))
2471 (insert (gdb-table-string table " "))
2472 (gdb-place-breakpoints)))
2474 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2475 (defun gdb-place-breakpoints ()
2476 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2477 (dolist (buffer (buffer-list))
2478 (with-current-buffer buffer
2479 (if (and (eq gud-minor-mode 'gdbmi)
2480 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2481 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2482 (dolist (breakpoint gdb-breakpoints-list)
2483 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2484 ; an associative list
2485 (line (bindat-get-field breakpoint 'line)))
2486 (when line
2487 (let ((file (bindat-get-field breakpoint 'fullname))
2488 (flag (bindat-get-field breakpoint 'enabled))
2489 (bptno (bindat-get-field breakpoint 'number)))
2490 (unless (file-exists-p file)
2491 (setq file (cdr (assoc bptno gdb-location-alist))))
2492 (if (and file
2493 (not (string-equal file "File not found")))
2494 (with-current-buffer
2495 (find-file-noselect file 'nowarn)
2496 (gdb-init-buffer)
2497 ;; Only want one breakpoint icon at each location.
2498 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2499 (string-to-number line)))
2500 (gdb-input (concat "list " file ":1") 'ignore)
2501 (gdb-input "-file-list-exec-source-file"
2502 `(lambda () (gdb-get-location
2503 ,bptno ,line ,flag)))))))))
2505 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2507 (defun gdb-get-location (bptno line flag)
2508 "Find the directory containing the relevant source file.
2509 Put in buffer and place breakpoint icon."
2510 (goto-char (point-min))
2511 (catch 'file-not-found
2512 (if (re-search-forward gdb-source-file-regexp nil t)
2513 (delete (cons bptno "File not found") gdb-location-alist)
2514 (push (cons bptno (match-string 1)) gdb-location-alist)
2515 (gdb-resync)
2516 (unless (assoc bptno gdb-location-alist)
2517 (push (cons bptno "File not found") gdb-location-alist)
2518 (message-box "Cannot find source file for breakpoint location.
2519 Add directory to search path for source files using the GDB command, dir."))
2520 (throw 'file-not-found nil))
2521 (with-current-buffer (find-file-noselect (match-string 1))
2522 (gdb-init-buffer)
2523 ;; only want one breakpoint icon at each location
2524 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2526 (add-hook 'find-file-hook 'gdb-find-file-hook)
2528 (defun gdb-find-file-hook ()
2529 "Set up buffer for debugging if file is part of the source code
2530 of the current session."
2531 (if (and (buffer-name gud-comint-buffer)
2532 ;; in case gud or gdb-ui is just loaded
2533 gud-comint-buffer
2534 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2535 'gdbmi))
2536 (if (member buffer-file-name gdb-source-file-list)
2537 (with-current-buffer (find-buffer-visiting buffer-file-name)
2538 (gdb-init-buffer)))))
2540 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2541 (declare-function gud-break "gdb-mi" t t) ; gud-def
2542 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2544 (defun gdb-mouse-set-clear-breakpoint (event)
2545 "Set/clear breakpoint in left fringe/margin at mouse click.
2546 If not in a source or disassembly buffer just set point."
2547 (interactive "e")
2548 (mouse-minibuffer-check event)
2549 (let ((posn (event-end event)))
2550 (with-selected-window (posn-window posn)
2551 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2552 (if (numberp (posn-point posn))
2553 (save-excursion
2554 (goto-char (posn-point posn))
2555 (if (or (posn-object posn)
2556 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2557 'breakpoint))
2558 (gud-remove nil)
2559 (gud-break nil)))))
2560 (posn-set-point posn))))
2562 (defun gdb-mouse-toggle-breakpoint-margin (event)
2563 "Enable/disable breakpoint in left margin with mouse click."
2564 (interactive "e")
2565 (mouse-minibuffer-check event)
2566 (let ((posn (event-end event)))
2567 (if (numberp (posn-point posn))
2568 (with-selected-window (posn-window posn)
2569 (save-excursion
2570 (goto-char (posn-point posn))
2571 (if (posn-object posn)
2572 (gud-basic-call
2573 (let ((bptno (get-text-property
2574 0 'gdb-bptno (car (posn-string posn)))))
2575 (concat
2576 (if (get-text-property
2577 0 'gdb-enabled (car (posn-string posn)))
2578 "-break-disable "
2579 "-break-enable ")
2580 bptno)))))))))
2582 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2583 "Enable/disable breakpoint in left fringe with mouse click."
2584 (interactive "e")
2585 (mouse-minibuffer-check event)
2586 (let* ((posn (event-end event))
2587 (pos (posn-point posn))
2588 obj)
2589 (when (numberp pos)
2590 (with-selected-window (posn-window posn)
2591 (with-current-buffer (window-buffer (selected-window))
2592 (goto-char pos)
2593 (dolist (overlay (overlays-in pos pos))
2594 (when (overlay-get overlay 'put-break)
2595 (setq obj (overlay-get overlay 'before-string))))
2596 (when (stringp obj)
2597 (gud-basic-call
2598 (concat
2599 (if (get-text-property 0 'gdb-enabled obj)
2600 "-break-disable "
2601 "-break-enable ")
2602 (get-text-property 0 'gdb-bptno obj)))))))))
2604 (defun gdb-breakpoints-buffer-name ()
2605 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2607 (def-gdb-display-buffer
2608 gdb-display-breakpoints-buffer
2609 'gdb-breakpoints-buffer
2610 "Display status of user-settable breakpoints.")
2612 (def-gdb-frame-for-buffer
2613 gdb-frame-breakpoints-buffer
2614 'gdb-breakpoints-buffer
2615 "Display status of user-settable breakpoints in a new frame.")
2617 (defvar gdb-breakpoints-mode-map
2618 (let ((map (make-sparse-keymap))
2619 (menu (make-sparse-keymap "Breakpoints")))
2620 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2621 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2622 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2623 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2624 (suppress-keymap map)
2625 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2626 (define-key map " " 'gdb-toggle-breakpoint)
2627 (define-key map "D" 'gdb-delete-breakpoint)
2628 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2629 (define-key map "q" 'gdb-delete-frame-or-window)
2630 (define-key map "\r" 'gdb-goto-breakpoint)
2631 (define-key map "\t" (lambda ()
2632 (interactive)
2633 (gdb-set-window-buffer
2634 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2635 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2636 (define-key map [follow-link] 'mouse-face)
2637 map))
2639 (defun gdb-delete-frame-or-window ()
2640 "Delete frame if there is only one window. Otherwise delete the window."
2641 (interactive)
2642 (if (one-window-p) (delete-frame)
2643 (delete-window)))
2645 ;;from make-mode-line-mouse-map
2646 (defun gdb-make-header-line-mouse-map (mouse function) "\
2647 Return a keymap with single entry for mouse key MOUSE on the header line.
2648 MOUSE is defined to run function FUNCTION with no args in the buffer
2649 corresponding to the mode line clicked."
2650 (let ((map (make-sparse-keymap)))
2651 (define-key map (vector 'header-line mouse) function)
2652 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2653 map))
2655 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2656 `(propertize ,name
2657 'help-echo ,help-echo
2658 'mouse-face ',mouse-face
2659 'face ',face
2660 'local-map
2661 (gdb-make-header-line-mouse-map
2662 'mouse-1
2663 (lambda (event) (interactive "e")
2664 (save-selected-window
2665 (select-window (posn-window (event-start event)))
2666 (gdb-set-window-buffer
2667 (gdb-get-buffer-create ',buffer) t) )))))
2670 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2671 ;;; Threads view
2673 (defun gdb-threads-buffer-name ()
2674 (concat "*threads of " (gdb-get-target-string) "*"))
2676 (def-gdb-display-buffer
2677 gdb-display-threads-buffer
2678 'gdb-threads-buffer
2679 "Display GDB threads.")
2681 (def-gdb-frame-for-buffer
2682 gdb-frame-threads-buffer
2683 'gdb-threads-buffer
2684 "Display GDB threads in a new frame.")
2686 (def-gdb-trigger-and-handler
2687 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2688 gdb-thread-list-handler gdb-thread-list-handler-custom
2689 '(start update update-threads))
2691 (gdb-set-buffer-rules
2692 'gdb-threads-buffer
2693 'gdb-threads-buffer-name
2694 'gdb-threads-mode
2695 'gdb-invalidate-threads)
2697 (defvar gdb-threads-font-lock-keywords
2698 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2699 (" \\(stopped\\)" (1 font-lock-warning-face))
2700 (" \\(running\\)" (1 font-lock-string-face))
2701 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2702 "Font lock keywords used in `gdb-threads-mode'.")
2704 (defvar gdb-threads-mode-map
2705 (let ((map (make-sparse-keymap)))
2706 (define-key map "\r" 'gdb-select-thread)
2707 (define-key map "f" 'gdb-display-stack-for-thread)
2708 (define-key map "F" 'gdb-frame-stack-for-thread)
2709 (define-key map "l" 'gdb-display-locals-for-thread)
2710 (define-key map "L" 'gdb-frame-locals-for-thread)
2711 (define-key map "r" 'gdb-display-registers-for-thread)
2712 (define-key map "R" 'gdb-frame-registers-for-thread)
2713 (define-key map "d" 'gdb-display-disassembly-for-thread)
2714 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2715 (define-key map "i" 'gdb-interrupt-thread)
2716 (define-key map "c" 'gdb-continue-thread)
2717 (define-key map "s" 'gdb-step-thread)
2718 (define-key map "\t"
2719 (lambda ()
2720 (interactive)
2721 (gdb-set-window-buffer
2722 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2723 (define-key map [mouse-2] 'gdb-select-thread)
2724 (define-key map [follow-link] 'mouse-face)
2725 map))
2727 (defvar gdb-threads-header
2728 (list
2729 (gdb-propertize-header
2730 "Breakpoints" gdb-breakpoints-buffer
2731 "mouse-1: select" mode-line-highlight mode-line-inactive)
2733 (gdb-propertize-header "Threads" gdb-threads-buffer
2734 nil nil mode-line)))
2736 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2737 "Major mode for GDB threads."
2738 (setq gdb-thread-position (make-marker))
2739 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2740 (setq header-line-format gdb-threads-header)
2741 (set (make-local-variable 'font-lock-defaults)
2742 '(gdb-threads-font-lock-keywords))
2743 'gdb-invalidate-threads)
2745 (defun gdb-thread-list-handler-custom ()
2746 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2747 (table (make-gdb-table))
2748 (marked-line nil))
2749 (setq gdb-threads-list nil)
2750 (setq gdb-running-threads-count 0)
2751 (setq gdb-stopped-threads-count 0)
2752 (set-marker gdb-thread-position nil)
2754 (dolist (thread (reverse threads-list))
2755 (let ((running (equal (bindat-get-field thread 'state) "running")))
2756 (add-to-list 'gdb-threads-list
2757 (cons (bindat-get-field thread 'id)
2758 thread))
2759 (cl-incf (if running
2760 gdb-running-threads-count
2761 gdb-stopped-threads-count))
2763 (gdb-table-add-row table
2764 (list
2765 (bindat-get-field thread 'id)
2766 (concat
2767 (if gdb-thread-buffer-verbose-names
2768 (concat (bindat-get-field thread 'target-id) " ") "")
2769 (bindat-get-field thread 'state)
2770 ;; Include frame information for stopped threads
2771 (if (not running)
2772 (concat
2773 " in " (bindat-get-field thread 'frame 'func)
2774 (if gdb-thread-buffer-arguments
2775 (concat
2776 " ("
2777 (let ((args (bindat-get-field thread 'frame 'args)))
2778 (mapconcat
2779 (lambda (arg)
2780 (apply #'format "%s=%s"
2781 (gdb-get-many-fields arg 'name 'value)))
2782 args ","))
2783 ")")
2785 (if gdb-thread-buffer-locations
2786 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2787 (if gdb-thread-buffer-addresses
2788 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2789 "")))
2790 (list
2791 'gdb-thread thread
2792 'mouse-face 'highlight
2793 'help-echo "mouse-2, RET: select thread")))
2794 (when (string-equal gdb-thread-number
2795 (bindat-get-field thread 'id))
2796 (setq marked-line (length gdb-threads-list))))
2797 (insert (gdb-table-string table " "))
2798 (when marked-line
2799 (gdb-mark-line marked-line gdb-thread-position)))
2800 ;; We update gud-running here because we need to make sure that
2801 ;; gdb-threads-list is up-to-date
2802 (gdb-update-gud-running)
2803 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2805 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2806 "Define a NAME command which will act upon thread on the current line.
2808 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2809 be the value of 'gdb-thread property of the current line. If
2810 'gdb-thread is nil, error is signaled."
2811 `(defun ,name (&optional event)
2812 ,(when doc doc)
2813 (interactive (list last-input-event))
2814 (if event (posn-set-point (event-end event)))
2815 (save-excursion
2816 (beginning-of-line)
2817 (let ((thread (get-text-property (point) 'gdb-thread)))
2818 (if thread
2819 ,custom-defun
2820 (error "Not recognized as thread line"))))))
2822 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2823 &optional doc)
2824 "Define a NAME which will call BUFFER-COMMAND with id of thread
2825 on the current line."
2826 `(def-gdb-thread-buffer-command ,name
2827 (,buffer-command (bindat-get-field thread 'id))
2828 ,doc))
2830 (def-gdb-thread-buffer-command gdb-select-thread
2831 (let ((new-id (bindat-get-field thread 'id)))
2832 (gdb-setq-thread-number new-id)
2833 (gdb-input (concat "-thread-select " new-id) 'ignore)
2834 (gdb-update))
2835 "Select the thread at current line of threads buffer.")
2837 (def-gdb-thread-buffer-simple-command
2838 gdb-display-stack-for-thread
2839 gdb-preemptively-display-stack-buffer
2840 "Display stack buffer for the thread at current line.")
2842 (def-gdb-thread-buffer-simple-command
2843 gdb-display-locals-for-thread
2844 gdb-preemptively-display-locals-buffer
2845 "Display locals buffer for the thread at current line.")
2847 (def-gdb-thread-buffer-simple-command
2848 gdb-display-registers-for-thread
2849 gdb-preemptively-display-registers-buffer
2850 "Display registers buffer for the thread at current line.")
2852 (def-gdb-thread-buffer-simple-command
2853 gdb-display-disassembly-for-thread
2854 gdb-preemptively-display-disassembly-buffer
2855 "Display disassembly buffer for the thread at current line.")
2857 (def-gdb-thread-buffer-simple-command
2858 gdb-frame-stack-for-thread
2859 gdb-frame-stack-buffer
2860 "Display a new frame with stack buffer for the thread at
2861 current line.")
2863 (def-gdb-thread-buffer-simple-command
2864 gdb-frame-locals-for-thread
2865 gdb-frame-locals-buffer
2866 "Display a new frame with locals buffer for the thread at
2867 current line.")
2869 (def-gdb-thread-buffer-simple-command
2870 gdb-frame-registers-for-thread
2871 gdb-frame-registers-buffer
2872 "Display a new frame with registers buffer for the thread at
2873 current line.")
2875 (def-gdb-thread-buffer-simple-command
2876 gdb-frame-disassembly-for-thread
2877 gdb-frame-disassembly-buffer
2878 "Display a new frame with disassembly buffer for the thread at
2879 current line.")
2881 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2882 "Define a NAME which will execute GUD-COMMAND with
2883 `gdb-thread-number' locally bound to id of thread on the current
2884 line."
2885 `(def-gdb-thread-buffer-command ,name
2886 (if gdb-non-stop
2887 (let ((gdb-thread-number (bindat-get-field thread 'id))
2888 (gdb-gud-control-all-threads nil))
2889 (call-interactively #',gud-command))
2890 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2891 ,doc))
2893 (def-gdb-thread-buffer-gud-command
2894 gdb-interrupt-thread
2895 gud-stop-subjob
2896 "Interrupt thread at current line.")
2898 (def-gdb-thread-buffer-gud-command
2899 gdb-continue-thread
2900 gud-cont
2901 "Continue thread at current line.")
2903 (def-gdb-thread-buffer-gud-command
2904 gdb-step-thread
2905 gud-step
2906 "Step thread at current line.")
2909 ;;; Memory view
2911 (defcustom gdb-memory-rows 8
2912 "Number of data rows in memory window."
2913 :type 'integer
2914 :group 'gud
2915 :version "23.2")
2917 (defcustom gdb-memory-columns 4
2918 "Number of data columns in memory window."
2919 :type 'integer
2920 :group 'gud
2921 :version "23.2")
2923 (defcustom gdb-memory-format "x"
2924 "Display format of data items in memory window."
2925 :type '(choice (const :tag "Hexadecimal" "x")
2926 (const :tag "Signed decimal" "d")
2927 (const :tag "Unsigned decimal" "u")
2928 (const :tag "Octal" "o")
2929 (const :tag "Binary" "t"))
2930 :group 'gud
2931 :version "22.1")
2933 (defcustom gdb-memory-unit 4
2934 "Unit size of data items in memory window."
2935 :type '(choice (const :tag "Byte" 1)
2936 (const :tag "Halfword" 2)
2937 (const :tag "Word" 4)
2938 (const :tag "Giant word" 8))
2939 :group 'gud
2940 :version "23.2")
2942 (def-gdb-trigger-and-handler
2943 gdb-invalidate-memory
2944 (format "-data-read-memory %s %s %d %d %d"
2945 gdb-memory-address
2946 gdb-memory-format
2947 gdb-memory-unit
2948 gdb-memory-rows
2949 gdb-memory-columns)
2950 gdb-read-memory-handler
2951 gdb-read-memory-custom
2952 '(start update))
2954 (gdb-set-buffer-rules
2955 'gdb-memory-buffer
2956 'gdb-memory-buffer-name
2957 'gdb-memory-mode
2958 'gdb-invalidate-memory)
2960 (defun gdb-memory-column-width (size format)
2961 "Return length of string with memory unit of SIZE in FORMAT.
2963 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2964 in `gdb-memory-format'."
2965 (let ((format-base (cdr (assoc format
2966 '(("x" . 16)
2967 ("d" . 10) ("u" . 10)
2968 ("o" . 8)
2969 ("t" . 2))))))
2970 (if format-base
2971 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2972 (cond ((string-equal format "x")
2973 (+ 2 res)) ; hexadecimal numbers have 0x in front
2974 ((or (string-equal format "d")
2975 (string-equal format "o"))
2976 (1+ res))
2977 (t res)))
2978 (error "Unknown format"))))
2980 (defun gdb-read-memory-custom ()
2981 (let* ((res (gdb-json-partial-output))
2982 (err-msg (bindat-get-field res 'msg)))
2983 (if (not err-msg)
2984 (let ((memory (bindat-get-field res 'memory)))
2985 (setq gdb-memory-address (bindat-get-field res 'addr))
2986 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2987 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2988 (setq gdb-memory-last-address gdb-memory-address)
2989 (dolist (row memory)
2990 (insert (concat (bindat-get-field row 'addr) ":"))
2991 (dolist (column (bindat-get-field row 'data))
2992 (insert (gdb-pad-string column
2993 (+ 2 (gdb-memory-column-width
2994 gdb-memory-unit
2995 gdb-memory-format)))))
2996 (newline)))
2997 ;; Show last page instead of empty buffer when out of bounds
2998 (progn
2999 (let ((gdb-memory-address gdb-memory-last-address))
3000 (gdb-invalidate-memory 'update)
3001 (error err-msg))))))
3003 (defvar gdb-memory-mode-map
3004 (let ((map (make-sparse-keymap)))
3005 (suppress-keymap map t)
3006 (define-key map "q" 'kill-this-buffer)
3007 (define-key map "n" 'gdb-memory-show-next-page)
3008 (define-key map "p" 'gdb-memory-show-previous-page)
3009 (define-key map "a" 'gdb-memory-set-address)
3010 (define-key map "t" 'gdb-memory-format-binary)
3011 (define-key map "o" 'gdb-memory-format-octal)
3012 (define-key map "u" 'gdb-memory-format-unsigned)
3013 (define-key map "d" 'gdb-memory-format-signed)
3014 (define-key map "x" 'gdb-memory-format-hexadecimal)
3015 (define-key map "b" 'gdb-memory-unit-byte)
3016 (define-key map "h" 'gdb-memory-unit-halfword)
3017 (define-key map "w" 'gdb-memory-unit-word)
3018 (define-key map "g" 'gdb-memory-unit-giant)
3019 (define-key map "R" 'gdb-memory-set-rows)
3020 (define-key map "C" 'gdb-memory-set-columns)
3021 map))
3023 (defun gdb-memory-set-address-event (event)
3024 "Handle a click on address field in memory buffer header."
3025 (interactive "e")
3026 (save-selected-window
3027 (select-window (posn-window (event-start event)))
3028 (gdb-memory-set-address)))
3030 ;; Non-event version for use within keymap
3031 (defun gdb-memory-set-address ()
3032 "Set the start memory address."
3033 (interactive)
3034 (let ((arg (read-from-minibuffer "Memory address: ")))
3035 (setq gdb-memory-address arg))
3036 (gdb-invalidate-memory 'update))
3038 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3039 "Define a function NAME which reads new VAR value from minibuffer."
3040 `(defun ,name (event)
3041 ,(when doc doc)
3042 (interactive "e")
3043 (save-selected-window
3044 (select-window (posn-window (event-start event)))
3045 (let* ((arg (read-from-minibuffer ,echo-string))
3046 (count (string-to-number arg)))
3047 (if (<= count 0)
3048 (error "Positive number only")
3049 (customize-set-variable ',variable count)
3050 (gdb-invalidate-memory 'update))))))
3052 (def-gdb-set-positive-number
3053 gdb-memory-set-rows
3054 gdb-memory-rows
3055 "Rows: "
3056 "Set the number of data rows in memory window.")
3058 (def-gdb-set-positive-number
3059 gdb-memory-set-columns
3060 gdb-memory-columns
3061 "Columns: "
3062 "Set the number of data columns in memory window.")
3064 (defmacro def-gdb-memory-format (name format doc)
3065 "Define a function NAME to switch memory buffer to use FORMAT.
3067 DOC is an optional documentation string."
3068 `(defun ,name () ,(when doc doc)
3069 (interactive)
3070 (customize-set-variable 'gdb-memory-format ,format)
3071 (gdb-invalidate-memory 'update)))
3073 (def-gdb-memory-format
3074 gdb-memory-format-binary "t"
3075 "Set the display format to binary.")
3077 (def-gdb-memory-format
3078 gdb-memory-format-octal "o"
3079 "Set the display format to octal.")
3081 (def-gdb-memory-format
3082 gdb-memory-format-unsigned "u"
3083 "Set the display format to unsigned decimal.")
3085 (def-gdb-memory-format
3086 gdb-memory-format-signed "d"
3087 "Set the display format to decimal.")
3089 (def-gdb-memory-format
3090 gdb-memory-format-hexadecimal "x"
3091 "Set the display format to hexadecimal.")
3093 (defvar gdb-memory-format-map
3094 (let ((map (make-sparse-keymap)))
3095 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3096 map)
3097 "Keymap to select format in the header line.")
3099 (defvar gdb-memory-format-menu
3100 (let ((map (make-sparse-keymap "Format")))
3102 (define-key map [binary]
3103 '(menu-item "Binary" gdb-memory-format-binary
3104 :button (:radio . (equal gdb-memory-format "t"))))
3105 (define-key map [octal]
3106 '(menu-item "Octal" gdb-memory-format-octal
3107 :button (:radio . (equal gdb-memory-format "o"))))
3108 (define-key map [unsigned]
3109 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3110 :button (:radio . (equal gdb-memory-format "u"))))
3111 (define-key map [signed]
3112 '(menu-item "Signed Decimal" gdb-memory-format-signed
3113 :button (:radio . (equal gdb-memory-format "d"))))
3114 (define-key map [hexadecimal]
3115 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3116 :button (:radio . (equal gdb-memory-format "x"))))
3117 map)
3118 "Menu of display formats in the header line.")
3120 (defun gdb-memory-format-menu (event)
3121 (interactive "@e")
3122 (x-popup-menu event gdb-memory-format-menu))
3124 (defun gdb-memory-format-menu-1 (event)
3125 (interactive "e")
3126 (save-selected-window
3127 (select-window (posn-window (event-start event)))
3128 (let* ((selection (gdb-memory-format-menu event))
3129 (binding (and selection (lookup-key gdb-memory-format-menu
3130 (vector (car selection))))))
3131 (if binding (call-interactively binding)))))
3133 (defmacro def-gdb-memory-unit (name unit-size doc)
3134 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3136 DOC is an optional documentation string."
3137 `(defun ,name () ,(when doc doc)
3138 (interactive)
3139 (customize-set-variable 'gdb-memory-unit ,unit-size)
3140 (gdb-invalidate-memory 'update)))
3142 (def-gdb-memory-unit gdb-memory-unit-giant 8
3143 "Set the unit size to giant words (eight bytes).")
3145 (def-gdb-memory-unit gdb-memory-unit-word 4
3146 "Set the unit size to words (four bytes).")
3148 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3149 "Set the unit size to halfwords (two bytes).")
3151 (def-gdb-memory-unit gdb-memory-unit-byte 1
3152 "Set the unit size to bytes.")
3154 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3155 "Define a function NAME which show new address in memory buffer.
3157 The defined function switches Memory buffer to show address
3158 stored in ADDRESS-VAR variable.
3160 DOC is an optional documentation string."
3161 `(defun ,name
3162 ,(when doc doc)
3163 (interactive)
3164 (let ((gdb-memory-address ,address-var))
3165 (gdb-invalidate-memory))))
3167 (def-gdb-memory-show-page gdb-memory-show-previous-page
3168 gdb-memory-prev-page)
3170 (def-gdb-memory-show-page gdb-memory-show-next-page
3171 gdb-memory-next-page)
3173 (defvar gdb-memory-unit-map
3174 (let ((map (make-sparse-keymap)))
3175 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3176 map)
3177 "Keymap to select units in the header line.")
3179 (defvar gdb-memory-unit-menu
3180 (let ((map (make-sparse-keymap "Unit")))
3181 (define-key map [giantwords]
3182 '(menu-item "Giant words" gdb-memory-unit-giant
3183 :button (:radio . (equal gdb-memory-unit 8))))
3184 (define-key map [words]
3185 '(menu-item "Words" gdb-memory-unit-word
3186 :button (:radio . (equal gdb-memory-unit 4))))
3187 (define-key map [halfwords]
3188 '(menu-item "Halfwords" gdb-memory-unit-halfword
3189 :button (:radio . (equal gdb-memory-unit 2))))
3190 (define-key map [bytes]
3191 '(menu-item "Bytes" gdb-memory-unit-byte
3192 :button (:radio . (equal gdb-memory-unit 1))))
3193 map)
3194 "Menu of units in the header line.")
3196 (defun gdb-memory-unit-menu (event)
3197 (interactive "@e")
3198 (x-popup-menu event gdb-memory-unit-menu))
3200 (defun gdb-memory-unit-menu-1 (event)
3201 (interactive "e")
3202 (save-selected-window
3203 (select-window (posn-window (event-start event)))
3204 (let* ((selection (gdb-memory-unit-menu event))
3205 (binding (and selection (lookup-key gdb-memory-unit-menu
3206 (vector (car selection))))))
3207 (if binding (call-interactively binding)))))
3209 (defvar gdb-memory-font-lock-keywords
3210 '(;; <__function.name+n>
3211 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3212 (1 font-lock-function-name-face)))
3213 "Font lock keywords used in `gdb-memory-mode'.")
3215 (defvar gdb-memory-header
3216 '(:eval
3217 (concat
3218 "Start address["
3219 (propertize "-"
3220 'face font-lock-warning-face
3221 'help-echo "mouse-1: decrement address"
3222 'mouse-face 'mode-line-highlight
3223 'local-map (gdb-make-header-line-mouse-map
3224 'mouse-1
3225 #'gdb-memory-show-previous-page))
3227 (propertize "+"
3228 'face font-lock-warning-face
3229 'help-echo "mouse-1: increment address"
3230 'mouse-face 'mode-line-highlight
3231 'local-map (gdb-make-header-line-mouse-map
3232 'mouse-1
3233 #'gdb-memory-show-next-page))
3234 "]: "
3235 (propertize gdb-memory-address
3236 'face font-lock-warning-face
3237 'help-echo "mouse-1: set start address"
3238 'mouse-face 'mode-line-highlight
3239 'local-map (gdb-make-header-line-mouse-map
3240 'mouse-1
3241 #'gdb-memory-set-address-event))
3242 " Rows: "
3243 (propertize (number-to-string gdb-memory-rows)
3244 'face font-lock-warning-face
3245 'help-echo "mouse-1: set number of columns"
3246 'mouse-face 'mode-line-highlight
3247 'local-map (gdb-make-header-line-mouse-map
3248 'mouse-1
3249 #'gdb-memory-set-rows))
3250 " Columns: "
3251 (propertize (number-to-string gdb-memory-columns)
3252 'face font-lock-warning-face
3253 'help-echo "mouse-1: set number of columns"
3254 'mouse-face 'mode-line-highlight
3255 'local-map (gdb-make-header-line-mouse-map
3256 'mouse-1
3257 #'gdb-memory-set-columns))
3258 " Display Format: "
3259 (propertize gdb-memory-format
3260 'face font-lock-warning-face
3261 'help-echo "mouse-3: select display format"
3262 'mouse-face 'mode-line-highlight
3263 'local-map gdb-memory-format-map)
3264 " Unit Size: "
3265 (propertize (number-to-string gdb-memory-unit)
3266 'face font-lock-warning-face
3267 'help-echo "mouse-3: select unit size"
3268 'mouse-face 'mode-line-highlight
3269 'local-map gdb-memory-unit-map)))
3270 "Header line used in `gdb-memory-mode'.")
3272 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3273 "Major mode for examining memory."
3274 (setq header-line-format gdb-memory-header)
3275 (set (make-local-variable 'font-lock-defaults)
3276 '(gdb-memory-font-lock-keywords))
3277 'gdb-invalidate-memory)
3279 (defun gdb-memory-buffer-name ()
3280 (concat "*memory of " (gdb-get-target-string) "*"))
3282 (def-gdb-display-buffer
3283 gdb-display-memory-buffer
3284 'gdb-memory-buffer
3285 "Display memory contents.")
3287 (defun gdb-frame-memory-buffer ()
3288 "Display memory contents in a new frame."
3289 (interactive)
3290 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3291 (special-display-frame-alist
3292 `((left-fringe . 0)
3293 (right-fringe . 0)
3294 (width . 83)
3295 ,@gdb-frame-parameters)))
3296 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3299 ;;; Disassembly view
3301 (defun gdb-disassembly-buffer-name ()
3302 (gdb-current-context-buffer-name
3303 (concat "disassembly of " (gdb-get-target-string))))
3305 (def-gdb-display-buffer
3306 gdb-display-disassembly-buffer
3307 'gdb-disassembly-buffer
3308 "Display disassembly for current stack frame.")
3310 (def-gdb-preempt-display-buffer
3311 gdb-preemptively-display-disassembly-buffer
3312 'gdb-disassembly-buffer)
3314 (def-gdb-frame-for-buffer
3315 gdb-frame-disassembly-buffer
3316 'gdb-disassembly-buffer
3317 "Display disassembly in a new frame.")
3319 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3320 (let* ((frame (gdb-current-buffer-frame))
3321 (file (bindat-get-field frame 'fullname))
3322 (line (bindat-get-field frame 'line)))
3323 (if file
3324 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3325 ;; If we're unable to get a file name / line for $PC, simply
3326 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3327 ;; 150 bytes) instructions.
3328 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3329 gdb-disassembly-handler
3330 ;; We update disassembly only after we have actual frame information
3331 ;; about all threads, so no there's `update' signal in this list
3332 '(start update-disassembly))
3334 (def-gdb-auto-update-handler
3335 gdb-disassembly-handler
3336 gdb-invalidate-disassembly
3337 gdb-disassembly-handler-custom
3340 (gdb-set-buffer-rules
3341 'gdb-disassembly-buffer
3342 'gdb-disassembly-buffer-name
3343 'gdb-disassembly-mode
3344 'gdb-invalidate-disassembly)
3346 (defvar gdb-disassembly-font-lock-keywords
3347 '(;; <__function.name+n>
3348 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3349 (1 font-lock-function-name-face))
3350 ;; 0xNNNNNNNN <__function.name+n>: opcode
3351 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3352 (4 font-lock-keyword-face))
3353 ;; %register(at least i386)
3354 ("%\\sw+" . font-lock-variable-name-face)
3355 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3356 (1 font-lock-comment-face)
3357 (2 font-lock-function-name-face))
3358 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3359 "Font lock keywords used in `gdb-disassembly-mode'.")
3361 (defvar gdb-disassembly-mode-map
3362 ;; TODO
3363 (let ((map (make-sparse-keymap)))
3364 (suppress-keymap map)
3365 (define-key map "q" 'kill-this-buffer)
3366 map))
3368 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3369 "Major mode for GDB disassembly information."
3370 ;; TODO Rename overlay variable for disassembly mode
3371 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3372 (setq fringes-outside-margins t)
3373 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3374 (set (make-local-variable 'font-lock-defaults)
3375 '(gdb-disassembly-font-lock-keywords))
3376 'gdb-invalidate-disassembly)
3378 (defun gdb-disassembly-handler-custom ()
3379 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3380 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3381 (table (make-gdb-table))
3382 (marked-line nil))
3383 (dolist (instr instructions)
3384 (gdb-table-add-row table
3385 (list
3386 (bindat-get-field instr 'address)
3387 (let
3388 ((func-name (bindat-get-field instr 'func-name))
3389 (offset (bindat-get-field instr 'offset)))
3390 (if func-name
3391 (format "<%s+%s>:" func-name offset)
3392 ""))
3393 (bindat-get-field instr 'inst)))
3394 (when (string-equal (bindat-get-field instr 'address)
3395 address)
3396 (progn
3397 (setq marked-line (length (gdb-table-rows table)))
3398 (setq fringe-indicator-alist
3399 (if (string-equal gdb-frame-number "0")
3401 '((overlay-arrow . hollow-right-triangle)))))))
3402 (insert (gdb-table-string table " "))
3403 (gdb-disassembly-place-breakpoints)
3404 ;; Mark current position with overlay arrow and scroll window to
3405 ;; that point
3406 (when marked-line
3407 (let ((window (get-buffer-window (current-buffer) 0)))
3408 (set-window-point window (gdb-mark-line marked-line
3409 gdb-disassembly-position))))
3410 (setq mode-name
3411 (gdb-current-context-mode-name
3412 (concat "Disassembly: "
3413 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3415 (defun gdb-disassembly-place-breakpoints ()
3416 (gdb-remove-breakpoint-icons (point-min) (point-max))
3417 (dolist (breakpoint gdb-breakpoints-list)
3418 (let* ((breakpoint (cdr breakpoint))
3419 (bptno (bindat-get-field breakpoint 'number))
3420 (flag (bindat-get-field breakpoint 'enabled))
3421 (address (bindat-get-field breakpoint 'addr)))
3422 (save-excursion
3423 (goto-char (point-min))
3424 (if (re-search-forward (concat "^" address) nil t)
3425 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3428 (defvar gdb-breakpoints-header
3429 (list
3430 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3431 nil nil mode-line)
3433 (gdb-propertize-header "Threads" gdb-threads-buffer
3434 "mouse-1: select" mode-line-highlight
3435 mode-line-inactive)))
3437 ;;; Breakpoints view
3438 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3439 "Major mode for gdb breakpoints."
3440 (setq header-line-format gdb-breakpoints-header)
3441 'gdb-invalidate-breakpoints)
3443 (defun gdb-toggle-breakpoint ()
3444 "Enable/disable breakpoint at current line of breakpoints buffer."
3445 (interactive)
3446 (save-excursion
3447 (beginning-of-line)
3448 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3449 (if breakpoint
3450 (gud-basic-call
3451 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3452 "-break-disable "
3453 "-break-enable ")
3454 (bindat-get-field breakpoint 'number)))
3455 (error "Not recognized as break/watchpoint line")))))
3457 (defun gdb-delete-breakpoint ()
3458 "Delete the breakpoint at current line of breakpoints buffer."
3459 (interactive)
3460 (save-excursion
3461 (beginning-of-line)
3462 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3463 (if breakpoint
3464 (gud-basic-call (concat "-break-delete "
3465 (bindat-get-field breakpoint 'number)))
3466 (error "Not recognized as break/watchpoint line")))))
3468 (defun gdb-goto-breakpoint (&optional event)
3469 "Go to the location of breakpoint at current line of
3470 breakpoints buffer."
3471 (interactive (list last-input-event))
3472 (if event (posn-set-point (event-end event)))
3473 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3474 (let ((window (get-buffer-window gud-comint-buffer)))
3475 (if window (save-selected-window (select-window window))))
3476 (save-excursion
3477 (beginning-of-line)
3478 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3479 (if breakpoint
3480 (let ((bptno (bindat-get-field breakpoint 'number))
3481 (file (bindat-get-field breakpoint 'fullname))
3482 (line (bindat-get-field breakpoint 'line)))
3483 (save-selected-window
3484 (let* ((buffer (find-file-noselect
3485 (if (file-exists-p file) file
3486 (cdr (assoc bptno gdb-location-alist)))))
3487 (window (or (gdb-display-source-buffer buffer)
3488 (display-buffer buffer))))
3489 (setq gdb-source-window window)
3490 (with-current-buffer buffer
3491 (goto-char (point-min))
3492 (forward-line (1- (string-to-number line)))
3493 (set-window-point window (point))))))
3494 (error "Not recognized as break/watchpoint line")))))
3497 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3499 (def-gdb-trigger-and-handler
3500 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3501 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3502 '(start update))
3504 (gdb-set-buffer-rules
3505 'gdb-stack-buffer
3506 'gdb-stack-buffer-name
3507 'gdb-frames-mode
3508 'gdb-invalidate-frames)
3510 (defun gdb-frame-location (frame)
3511 "Return \" of file:line\" or \" of library\" for structure FRAME.
3513 FRAME must have either \"file\" and \"line\" members or \"from\"
3514 member."
3515 (let ((file (bindat-get-field frame 'file))
3516 (line (bindat-get-field frame 'line))
3517 (from (bindat-get-field frame 'from)))
3518 (let ((res (or (and file line (concat file ":" line))
3519 from)))
3520 (if res (concat " of " res) ""))))
3522 (defun gdb-stack-list-frames-custom ()
3523 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3524 (table (make-gdb-table)))
3525 (set-marker gdb-stack-position nil)
3526 (dolist (frame stack)
3527 (gdb-table-add-row table
3528 (list
3529 (bindat-get-field frame 'level)
3530 "in"
3531 (concat
3532 (bindat-get-field frame 'func)
3533 (if gdb-stack-buffer-locations
3534 (gdb-frame-location frame) "")
3535 (if gdb-stack-buffer-addresses
3536 (concat " at " (bindat-get-field frame 'addr)) "")))
3537 `(mouse-face highlight
3538 help-echo "mouse-2, RET: Select frame"
3539 gdb-frame ,frame)))
3540 (insert (gdb-table-string table " ")))
3541 (when (and gdb-frame-number
3542 (gdb-buffer-shows-main-thread-p))
3543 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3544 gdb-stack-position))
3545 (setq mode-name
3546 (gdb-current-context-mode-name "Frames")))
3548 (defun gdb-stack-buffer-name ()
3549 (gdb-current-context-buffer-name
3550 (concat "stack frames of " (gdb-get-target-string))))
3552 (def-gdb-display-buffer
3553 gdb-display-stack-buffer
3554 'gdb-stack-buffer
3555 "Display backtrace of current stack.")
3557 (def-gdb-preempt-display-buffer
3558 gdb-preemptively-display-stack-buffer
3559 'gdb-stack-buffer nil t)
3561 (def-gdb-frame-for-buffer
3562 gdb-frame-stack-buffer
3563 'gdb-stack-buffer
3564 "Display backtrace of current stack in a new frame.")
3566 (defvar gdb-frames-mode-map
3567 (let ((map (make-sparse-keymap)))
3568 (suppress-keymap map)
3569 (define-key map "q" 'kill-this-buffer)
3570 (define-key map "\r" 'gdb-select-frame)
3571 (define-key map [mouse-2] 'gdb-select-frame)
3572 (define-key map [follow-link] 'mouse-face)
3573 map))
3575 (defvar gdb-frames-font-lock-keywords
3576 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3577 "Font lock keywords used in `gdb-frames-mode'.")
3579 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3580 "Major mode for gdb call stack."
3581 (setq gdb-stack-position (make-marker))
3582 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3583 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3584 (set (make-local-variable 'font-lock-defaults)
3585 '(gdb-frames-font-lock-keywords))
3586 'gdb-invalidate-frames)
3588 (defun gdb-select-frame (&optional event)
3589 "Select the frame and display the relevant source."
3590 (interactive (list last-input-event))
3591 (if event (posn-set-point (event-end event)))
3592 (let ((frame (get-text-property (point) 'gdb-frame)))
3593 (if frame
3594 (if (gdb-buffer-shows-main-thread-p)
3595 (let ((new-level (bindat-get-field frame 'level)))
3596 (setq gdb-frame-number new-level)
3597 (gdb-input (concat "-stack-select-frame " new-level)
3598 'ignore)
3599 (gdb-update))
3600 (error "Could not select frame for non-current thread"))
3601 (error "Not recognized as frame line"))))
3604 ;; Locals buffer.
3605 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3606 (def-gdb-trigger-and-handler
3607 gdb-invalidate-locals
3608 (concat (gdb-current-context-command "-stack-list-locals")
3609 " --simple-values")
3610 gdb-locals-handler gdb-locals-handler-custom
3611 '(start update))
3613 (gdb-set-buffer-rules
3614 'gdb-locals-buffer
3615 'gdb-locals-buffer-name
3616 'gdb-locals-mode
3617 'gdb-invalidate-locals)
3619 (defvar gdb-locals-watch-map
3620 (let ((map (make-sparse-keymap)))
3621 (suppress-keymap map)
3622 (define-key map "\r" 'gud-watch)
3623 (define-key map [mouse-2] 'gud-watch)
3624 map)
3625 "Keymap to create watch expression of a complex data type local variable.")
3627 (defvar gdb-edit-locals-map-1
3628 (let ((map (make-sparse-keymap)))
3629 (suppress-keymap map)
3630 (define-key map "\r" 'gdb-edit-locals-value)
3631 (define-key map [mouse-2] 'gdb-edit-locals-value)
3632 map)
3633 "Keymap to edit value of a simple data type local variable.")
3635 (defun gdb-edit-locals-value (&optional event)
3636 "Assign a value to a variable displayed in the locals buffer."
3637 (interactive (list last-input-event))
3638 (save-excursion
3639 (if event (posn-set-point (event-end event)))
3640 (beginning-of-line)
3641 (let* ((var (bindat-get-field
3642 (get-text-property (point) 'gdb-local-variable) 'name))
3643 (value (read-string (format "New value (%s): " var))))
3644 (gud-basic-call
3645 (concat "-gdb-set variable " var " = " value)))))
3647 ;; Don't display values of arrays or structures.
3648 ;; These can be expanded using gud-watch.
3649 (defun gdb-locals-handler-custom ()
3650 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3651 (table (make-gdb-table)))
3652 (dolist (local locals-list)
3653 (let ((name (bindat-get-field local 'name))
3654 (value (bindat-get-field local 'value))
3655 (type (bindat-get-field local 'type)))
3656 (if (or (not value)
3657 (string-match "\\0x" value))
3658 (add-text-properties 0 (length name)
3659 `(mouse-face highlight
3660 help-echo "mouse-2: create watch expression"
3661 local-map ,gdb-locals-watch-map)
3662 name)
3663 (add-text-properties 0 (length value)
3664 `(mouse-face highlight
3665 help-echo "mouse-2: edit value"
3666 local-map ,gdb-edit-locals-map-1)
3667 value))
3668 (gdb-table-add-row
3669 table
3670 (list
3671 (propertize type 'font-lock-face font-lock-type-face)
3672 (propertize name 'font-lock-face font-lock-variable-name-face)
3673 value)
3674 `(gdb-local-variable ,local))))
3675 (insert (gdb-table-string table " "))
3676 (setq mode-name
3677 (gdb-current-context-mode-name
3678 (concat "Locals: "
3679 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3681 (defvar gdb-locals-header
3682 (list
3683 (gdb-propertize-header "Locals" gdb-locals-buffer
3684 nil nil mode-line)
3686 (gdb-propertize-header "Registers" gdb-registers-buffer
3687 "mouse-1: select" mode-line-highlight
3688 mode-line-inactive)))
3690 (defvar gdb-locals-mode-map
3691 (let ((map (make-sparse-keymap)))
3692 (suppress-keymap map)
3693 (define-key map "q" 'kill-this-buffer)
3694 (define-key map "\t" (lambda ()
3695 (interactive)
3696 (gdb-set-window-buffer
3697 (gdb-get-buffer-create
3698 'gdb-registers-buffer
3699 gdb-thread-number) t)))
3700 map))
3702 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3703 "Major mode for gdb locals."
3704 (setq header-line-format gdb-locals-header)
3705 'gdb-invalidate-locals)
3707 (defun gdb-locals-buffer-name ()
3708 (gdb-current-context-buffer-name
3709 (concat "locals of " (gdb-get-target-string))))
3711 (def-gdb-display-buffer
3712 gdb-display-locals-buffer
3713 'gdb-locals-buffer
3714 "Display local variables of current stack and their values.")
3716 (def-gdb-preempt-display-buffer
3717 gdb-preemptively-display-locals-buffer
3718 'gdb-locals-buffer nil t)
3720 (def-gdb-frame-for-buffer
3721 gdb-frame-locals-buffer
3722 'gdb-locals-buffer
3723 "Display local variables of current stack and their values in a new frame.")
3726 ;; Registers buffer.
3728 (def-gdb-trigger-and-handler
3729 gdb-invalidate-registers
3730 (concat (gdb-current-context-command "-data-list-register-values") " x")
3731 gdb-registers-handler
3732 gdb-registers-handler-custom
3733 '(start update))
3735 (gdb-set-buffer-rules
3736 'gdb-registers-buffer
3737 'gdb-registers-buffer-name
3738 'gdb-registers-mode
3739 'gdb-invalidate-registers)
3741 (defun gdb-registers-handler-custom ()
3742 (when gdb-register-names
3743 (let ((register-values
3744 (bindat-get-field (gdb-json-partial-output) 'register-values))
3745 (table (make-gdb-table)))
3746 (dolist (register register-values)
3747 (let* ((register-number (bindat-get-field register 'number))
3748 (value (bindat-get-field register 'value))
3749 (register-name (nth (string-to-number register-number)
3750 gdb-register-names)))
3751 (gdb-table-add-row
3752 table
3753 (list
3754 (propertize register-name
3755 'font-lock-face font-lock-variable-name-face)
3756 (if (member register-number gdb-changed-registers)
3757 (propertize value 'font-lock-face font-lock-warning-face)
3758 value))
3759 `(mouse-face highlight
3760 help-echo "mouse-2: edit value"
3761 gdb-register-name ,register-name))))
3762 (insert (gdb-table-string table " ")))
3763 (setq mode-name
3764 (gdb-current-context-mode-name "Registers"))))
3766 (defun gdb-edit-register-value (&optional event)
3767 "Assign a value to a register displayed in the registers buffer."
3768 (interactive (list last-input-event))
3769 (save-excursion
3770 (if event (posn-set-point (event-end event)))
3771 (beginning-of-line)
3772 (let* ((var (bindat-get-field
3773 (get-text-property (point) 'gdb-register-name)))
3774 (value (read-string (format "New value (%s): " var))))
3775 (gud-basic-call
3776 (concat "-gdb-set variable $" var " = " value)))))
3778 (defvar gdb-registers-mode-map
3779 (let ((map (make-sparse-keymap)))
3780 (suppress-keymap map)
3781 (define-key map "\r" 'gdb-edit-register-value)
3782 (define-key map [mouse-2] 'gdb-edit-register-value)
3783 (define-key map "q" 'kill-this-buffer)
3784 (define-key map "\t" (lambda ()
3785 (interactive)
3786 (gdb-set-window-buffer
3787 (gdb-get-buffer-create
3788 'gdb-locals-buffer
3789 gdb-thread-number) t)))
3790 map))
3792 (defvar gdb-registers-header
3793 (list
3794 (gdb-propertize-header "Locals" gdb-locals-buffer
3795 "mouse-1: select" mode-line-highlight
3796 mode-line-inactive)
3798 (gdb-propertize-header "Registers" gdb-registers-buffer
3799 nil nil mode-line)))
3801 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3802 "Major mode for gdb registers."
3803 (setq header-line-format gdb-registers-header)
3804 'gdb-invalidate-registers)
3806 (defun gdb-registers-buffer-name ()
3807 (gdb-current-context-buffer-name
3808 (concat "registers of " (gdb-get-target-string))))
3810 (def-gdb-display-buffer
3811 gdb-display-registers-buffer
3812 'gdb-registers-buffer
3813 "Display integer register contents.")
3815 (def-gdb-preempt-display-buffer
3816 gdb-preemptively-display-registers-buffer
3817 'gdb-registers-buffer nil t)
3819 (def-gdb-frame-for-buffer
3820 gdb-frame-registers-buffer
3821 'gdb-registers-buffer
3822 "Display integer register contents in a new frame.")
3824 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3825 (defun gdb-get-changed-registers ()
3826 (when (and (gdb-get-buffer 'gdb-registers-buffer)
3827 (not (gdb-pending-p 'gdb-get-changed-registers)))
3828 (gdb-input "-data-list-changed-registers"
3829 'gdb-changed-registers-handler)
3830 (gdb-add-pending 'gdb-get-changed-registers)))
3832 (defun gdb-changed-registers-handler ()
3833 (gdb-delete-pending 'gdb-get-changed-registers)
3834 (setq gdb-changed-registers nil)
3835 (dolist (register-number
3836 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3837 (push register-number gdb-changed-registers)))
3839 (defun gdb-register-names-handler ()
3840 ;; Don't use gdb-pending-triggers because this handler is called
3841 ;; only once (in gdb-init-1)
3842 (setq gdb-register-names nil)
3843 (dolist (register-name
3844 (bindat-get-field (gdb-json-partial-output) 'register-names))
3845 (push register-name gdb-register-names))
3846 (setq gdb-register-names (reverse gdb-register-names)))
3849 (defun gdb-get-source-file-list ()
3850 "Create list of source files for current GDB session.
3851 If buffers already exist for any of these files, gud-minor-mode
3852 is set in them."
3853 (goto-char (point-min))
3854 (while (re-search-forward gdb-source-file-regexp nil t)
3855 (push (match-string 1) gdb-source-file-list))
3856 (dolist (buffer (buffer-list))
3857 (with-current-buffer buffer
3858 (when (member buffer-file-name gdb-source-file-list)
3859 (gdb-init-buffer)))))
3861 (defun gdb-get-main-selected-frame ()
3862 "Trigger for `gdb-frame-handler' which uses main current
3863 thread. Called from `gdb-update'."
3864 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3865 (progn
3866 (gdb-input (gdb-current-context-command "-stack-info-frame")
3867 'gdb-frame-handler)
3868 (gdb-add-pending 'gdb-get-main-selected-frame))))
3870 (defun gdb-frame-handler ()
3871 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3872 overlay arrow in source buffer."
3873 (gdb-delete-pending 'gdb-get-main-selected-frame)
3874 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3875 (when frame
3876 (setq gdb-selected-frame (bindat-get-field frame 'func))
3877 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3878 (setq gdb-frame-number (bindat-get-field frame 'level))
3879 (setq gdb-frame-address (bindat-get-field frame 'addr))
3880 (let ((line (bindat-get-field frame 'line)))
3881 (setq gdb-selected-line (and line (string-to-number line)))
3882 (when (and gdb-selected-file gdb-selected-line)
3883 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3884 (gud-display-frame)))
3885 (if gud-overlay-arrow-position
3886 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3887 (position (marker-position gud-overlay-arrow-position)))
3888 (when buffer
3889 (with-current-buffer buffer
3890 (setq fringe-indicator-alist
3891 (if (string-equal gdb-frame-number "0")
3893 '((overlay-arrow . hollow-right-triangle))))
3894 (setq gud-overlay-arrow-position (make-marker))
3895 (set-marker gud-overlay-arrow-position position))))))))
3897 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3899 (defun gdb-get-prompt ()
3900 "Find prompt for GDB session."
3901 (goto-char (point-min))
3902 (setq gdb-prompt-name nil)
3903 (re-search-forward gdb-prompt-name-regexp nil t)
3904 (setq gdb-prompt-name (match-string 1))
3905 ;; Insert first prompt.
3906 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3908 ;;;; Window management
3909 (defun gdb-display-buffer (buf dedicated &optional frame)
3910 "Show buffer BUF.
3912 If BUF is already displayed in some window, show it, deiconifying
3913 the frame if necessary. Otherwise, find least recently used
3914 window and show BUF there, if the window is not used for GDB
3915 already, in which case that window is split first."
3916 (let ((answer (get-buffer-window buf (or frame 0))))
3917 (if answer
3918 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3919 (let ((window (get-lru-window)))
3920 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3921 'gdbmi)
3922 (let ((largest (get-largest-window)))
3923 (setq answer (split-window largest))
3924 (set-window-buffer answer buf)
3925 (set-window-dedicated-p answer dedicated)
3926 answer)
3927 (set-window-buffer window buf)
3928 window)))))
3930 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3931 "Find window displaying a buffer with the same
3932 `gdb-buffer-type' as BUF and show BUF there. If no such window
3933 exists, just call `gdb-display-buffer' for BUF. If the window
3934 found is already dedicated, split window according to
3935 SPLIT-HORIZONTAL and show BUF in the new window."
3936 (if buf
3937 (when (not (get-buffer-window buf))
3938 (let* ((buf-type (gdb-buffer-type buf))
3939 (existing-window
3940 (get-window-with-predicate
3941 #'(lambda (w)
3942 (and (eq buf-type
3943 (gdb-buffer-type (window-buffer w)))
3944 (not (window-dedicated-p w)))))))
3945 (if existing-window
3946 (set-window-buffer existing-window buf)
3947 (let ((dedicated-window
3948 (get-window-with-predicate
3949 #'(lambda (w)
3950 (eq buf-type
3951 (gdb-buffer-type (window-buffer w)))))))
3952 (if dedicated-window
3953 (set-window-buffer
3954 (split-window dedicated-window nil split-horizontal) buf)
3955 (gdb-display-buffer buf t))))))
3956 (error "Null buffer")))
3958 ;;; Shared keymap initialization:
3960 (let ((menu (make-sparse-keymap "GDB-Windows")))
3961 (define-key gud-menu-map [displays]
3962 `(menu-item "GDB-Windows" ,menu
3963 :visible (eq gud-minor-mode 'gdbmi)))
3964 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3965 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3966 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3967 (define-key menu [disassembly]
3968 '("Disassembly" . gdb-display-disassembly-buffer))
3969 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3970 (define-key menu [inferior]
3971 '("IO" . gdb-display-io-buffer))
3972 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3973 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3974 (define-key menu [breakpoints]
3975 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3977 (let ((menu (make-sparse-keymap "GDB-Frames")))
3978 (define-key gud-menu-map [frames]
3979 `(menu-item "GDB-Frames" ,menu
3980 :visible (eq gud-minor-mode 'gdbmi)))
3981 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3982 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3983 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3984 (define-key menu [disassembly]
3985 '("Disassembly" . gdb-frame-disassembly-buffer))
3986 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3987 (define-key menu [inferior]
3988 '("IO" . gdb-frame-io-buffer))
3989 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3990 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3991 (define-key menu [breakpoints]
3992 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3994 (let ((menu (make-sparse-keymap "GDB-MI")))
3995 (define-key menu [gdb-customize]
3996 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3997 :help "Customize Gdb Graphical Mode options."))
3998 (define-key menu [gdb-many-windows]
3999 '(menu-item "Display Other Windows" gdb-many-windows
4000 :help "Toggle display of locals, stack and breakpoint information"
4001 :button (:toggle . gdb-many-windows)))
4002 (define-key menu [gdb-restore-windows]
4003 '(menu-item "Restore Window Layout" gdb-restore-windows
4004 :help "Restore standard layout for debug session."))
4005 (define-key menu [sep1]
4006 '(menu-item "--"))
4007 (define-key menu [all-threads]
4008 '(menu-item "GUD controls all threads"
4009 (lambda ()
4010 (interactive)
4011 (setq gdb-gud-control-all-threads t))
4012 :help "GUD start/stop commands apply to all threads"
4013 :button (:radio . gdb-gud-control-all-threads)))
4014 (define-key menu [current-thread]
4015 '(menu-item "GUD controls current thread"
4016 (lambda ()
4017 (interactive)
4018 (setq gdb-gud-control-all-threads nil))
4019 :help "GUD start/stop commands apply to current thread only"
4020 :button (:radio . (not gdb-gud-control-all-threads))))
4021 (define-key menu [sep2]
4022 '(menu-item "--"))
4023 (define-key menu [gdb-customize-reasons]
4024 '(menu-item "Customize switching..."
4025 (lambda ()
4026 (interactive)
4027 (customize-option 'gdb-switch-reasons))))
4028 (define-key menu [gdb-switch-when-another-stopped]
4029 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4030 gdb-switch-when-another-stopped
4031 "Automatically switch to stopped thread"
4032 "GDB thread switching %s"
4033 "Switch to stopped thread"))
4034 (define-key gud-menu-map [mi]
4035 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4037 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4038 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4039 ;; tool-bar-local-item-from-menu here because it appends new buttons
4040 ;; to toolbar from right to left while we want our A/T throttle to
4041 ;; show up right before Run button.
4042 (define-key-after gud-tool-bar-map [all-threads]
4043 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4044 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4045 :visible (and (eq gud-minor-mode 'gdbmi)
4046 gdb-non-stop
4047 (not gdb-gud-control-all-threads)))
4048 'run)
4050 (define-key-after gud-tool-bar-map [current-thread]
4051 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4052 :image (find-image '((:type xpm :file "gud/all.xpm")))
4053 :visible (and (eq gud-minor-mode 'gdbmi)
4054 gdb-non-stop
4055 gdb-gud-control-all-threads))
4056 'all-threads)
4058 (defun gdb-frame-gdb-buffer ()
4059 "Display GUD buffer in a new frame."
4060 (interactive)
4061 (display-buffer-other-frame gud-comint-buffer))
4063 (defun gdb-display-gdb-buffer ()
4064 "Display GUD buffer."
4065 (interactive)
4066 (pop-to-buffer gud-comint-buffer nil 0))
4068 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4069 "Set buffer of selected window to NAME and dedicate window.
4071 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4072 window is dedicated."
4073 (unless window (setq window (selected-window)))
4074 (when ignore-dedicated
4075 (set-window-dedicated-p window nil))
4076 (set-window-buffer window (get-buffer name))
4077 (set-window-dedicated-p window t))
4079 (defun gdb-setup-windows ()
4080 "Layout the window pattern for `gdb-many-windows'."
4081 (gdb-display-locals-buffer)
4082 (gdb-display-stack-buffer)
4083 (delete-other-windows)
4084 (gdb-display-breakpoints-buffer)
4085 (delete-other-windows)
4086 ;; Don't dedicate.
4087 (switch-to-buffer gud-comint-buffer)
4088 (let ((win0 (selected-window))
4089 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4090 (win2 (split-window nil ( / (window-height) 3)))
4091 (win3 (split-window-right)))
4092 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4093 (select-window win2)
4094 (set-window-buffer
4095 win2
4096 (if gud-last-last-frame
4097 (gud-find-file (car gud-last-last-frame))
4098 (if gdb-main-file
4099 (gud-find-file gdb-main-file)
4100 ;; Put buffer list in window if we
4101 ;; can't find a source file.
4102 (list-buffers-noselect))))
4103 (setq gdb-source-window (selected-window))
4104 (let ((win4 (split-window-right)))
4105 (gdb-set-window-buffer
4106 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4107 (select-window win1)
4108 (gdb-set-window-buffer (gdb-stack-buffer-name))
4109 (let ((win5 (split-window-right)))
4110 (gdb-set-window-buffer (if gdb-show-threads-by-default
4111 (gdb-threads-buffer-name)
4112 (gdb-breakpoints-buffer-name))
4113 nil win5))
4114 (select-window win0)))
4116 (define-minor-mode gdb-many-windows
4117 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4118 In this case it starts with two windows: one displaying the GUD
4119 buffer and the other with the source file with the main routine
4120 of the debugged program. Non-nil means display the layout shown for
4121 `gdb'."
4122 :global t
4123 :group 'gdb
4124 :version "22.1"
4125 (if (and gud-comint-buffer
4126 (buffer-name gud-comint-buffer))
4127 (ignore-errors
4128 (gdb-restore-windows))))
4130 (defun gdb-restore-windows ()
4131 "Restore the basic arrangement of windows used by gdb.
4132 This arrangement depends on the value of `gdb-many-windows'."
4133 (interactive)
4134 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4135 (delete-other-windows)
4136 (if gdb-many-windows
4137 (gdb-setup-windows)
4138 (when (or gud-last-last-frame gdb-show-main)
4139 (let ((win (split-window)))
4140 (set-window-buffer
4142 (if gud-last-last-frame
4143 (gud-find-file (car gud-last-last-frame))
4144 (gud-find-file gdb-main-file)))
4145 (setq gdb-source-window win)))))
4147 ;; Called from `gud-sentinel' in gud.el:
4148 (defun gdb-reset ()
4149 "Exit a debugging session cleanly.
4150 Kills the gdb buffers, and resets variables and the source buffers."
4151 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4152 ;; process. This pty must be deleted explicitly.
4153 (let ((pty (get-process "gdb-inferior")))
4154 (if pty (delete-process pty)))
4155 ;; Find gdb-mi buffers and kill them.
4156 (dolist (buffer (buffer-list))
4157 (unless (eq buffer gud-comint-buffer)
4158 (with-current-buffer buffer
4159 (if (eq gud-minor-mode 'gdbmi)
4160 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4161 (kill-buffer nil)
4162 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4163 (setq gud-minor-mode nil)
4164 (kill-local-variable 'tool-bar-map)
4165 (kill-local-variable 'gdb-define-alist))))))
4166 (setq gdb-disassembly-position nil)
4167 (setq overlay-arrow-variable-list
4168 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4169 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4170 (setq gdb-stack-position nil)
4171 (setq overlay-arrow-variable-list
4172 (delq 'gdb-stack-position overlay-arrow-variable-list))
4173 (setq gdb-thread-position nil)
4174 (setq overlay-arrow-variable-list
4175 (delq 'gdb-thread-position overlay-arrow-variable-list))
4176 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4177 (setq gud-running nil)
4178 (setq gdb-active-process nil)
4179 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4181 (defun gdb-get-source-file ()
4182 "Find the source file where the program starts and display it with related
4183 buffers, if required."
4184 (goto-char (point-min))
4185 (if (re-search-forward gdb-source-file-regexp nil t)
4186 (setq gdb-main-file (match-string 1)))
4187 (if gdb-many-windows
4188 (gdb-setup-windows)
4189 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4190 (if (and gdb-show-main gdb-main-file)
4191 (let ((pop-up-windows t))
4192 (display-buffer (gud-find-file gdb-main-file)))))
4193 (gdb-force-mode-line-update
4194 (propertize "ready" 'face font-lock-variable-name-face)))
4196 ;;from put-image
4197 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4198 "Put string PUTSTRING in front of POS in the current buffer.
4199 PUTSTRING is displayed by putting an overlay into the current buffer with a
4200 `before-string' string that has a `display' property whose value is
4201 PUTSTRING."
4202 (let ((string (make-string 1 ?x))
4203 (buffer (current-buffer)))
4204 (setq putstring (copy-sequence putstring))
4205 (let ((overlay (make-overlay pos pos buffer))
4206 (prop (or dprop
4207 (list (list 'margin 'left-margin) putstring))))
4208 (put-text-property 0 1 'display prop string)
4209 (if sprops
4210 (add-text-properties 0 1 sprops string))
4211 (overlay-put overlay 'put-break t)
4212 (overlay-put overlay 'before-string string))))
4214 ;;from remove-images
4215 (defun gdb-remove-strings (start end &optional buffer)
4216 "Remove strings between START and END in BUFFER.
4217 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4218 BUFFER nil or omitted means use the current buffer."
4219 (unless buffer
4220 (setq buffer (current-buffer)))
4221 (dolist (overlay (overlays-in start end))
4222 (when (overlay-get overlay 'put-break)
4223 (delete-overlay overlay))))
4225 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4226 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4227 (start (- (car posns) 1))
4228 (end (+ (cdr posns) 1))
4229 (putstring (if enabled "B" "b"))
4230 (source-window (get-buffer-window (current-buffer) 0)))
4231 (add-text-properties
4232 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4233 putstring)
4234 (if enabled
4235 (add-text-properties
4236 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4237 (add-text-properties
4238 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4239 (gdb-remove-breakpoint-icons start end)
4240 (if (display-images-p)
4241 (if (>= (or left-fringe-width
4242 (if source-window (car (window-fringes source-window)))
4243 gdb-buffer-fringe-width) 8)
4244 (gdb-put-string
4245 nil (1+ start)
4246 `(left-fringe breakpoint
4247 ,(if enabled
4248 'breakpoint-enabled
4249 'breakpoint-disabled))
4250 'gdb-bptno bptno
4251 'gdb-enabled enabled)
4252 (when (< left-margin-width 2)
4253 (save-current-buffer
4254 (setq left-margin-width 2)
4255 (if source-window
4256 (set-window-margins
4257 source-window
4258 left-margin-width right-margin-width))))
4259 (put-image
4260 (if enabled
4261 (or breakpoint-enabled-icon
4262 (setq breakpoint-enabled-icon
4263 (find-image `((:type xpm :data
4264 ,breakpoint-xpm-data
4265 :ascent 100 :pointer hand)
4266 (:type pbm :data
4267 ,breakpoint-enabled-pbm-data
4268 :ascent 100 :pointer hand)))))
4269 (or breakpoint-disabled-icon
4270 (setq breakpoint-disabled-icon
4271 (find-image `((:type xpm :data
4272 ,breakpoint-xpm-data
4273 :conversion disabled
4274 :ascent 100 :pointer hand)
4275 (:type pbm :data
4276 ,breakpoint-disabled-pbm-data
4277 :ascent 100 :pointer hand))))))
4278 (+ start 1)
4279 putstring
4280 'left-margin))
4281 (when (< left-margin-width 2)
4282 (save-current-buffer
4283 (setq left-margin-width 2)
4284 (let ((window (get-buffer-window (current-buffer) 0)))
4285 (if window
4286 (set-window-margins
4287 window left-margin-width right-margin-width)))))
4288 (gdb-put-string
4289 (propertize putstring
4290 'face (if enabled
4291 'breakpoint-enabled 'breakpoint-disabled))
4292 (1+ start)))))
4294 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4295 (gdb-remove-strings start end)
4296 (if (display-images-p)
4297 (remove-images start end))
4298 (when remove-margin
4299 (setq left-margin-width 0)
4300 (let ((window (get-buffer-window (current-buffer) 0)))
4301 (if window
4302 (set-window-margins
4303 window left-margin-width right-margin-width)))))
4306 ;;; Functions for inline completion.
4308 (defvar gud-gdb-fetch-lines-in-progress)
4309 (defvar gud-gdb-fetch-lines-string)
4310 (defvar gud-gdb-fetch-lines-break)
4311 (defvar gud-gdb-fetched-lines)
4313 (defun gud-gdbmi-completions (context command)
4314 "Completion table for GDB/MI commands.
4315 COMMAND is the prefix for which we seek completion.
4316 CONTEXT is the text before COMMAND on the line."
4317 (let ((gud-gdb-fetch-lines-in-progress t)
4318 (gud-gdb-fetch-lines-string nil)
4319 (gud-gdb-fetch-lines-break (length context))
4320 (gud-gdb-fetched-lines nil)
4321 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4322 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter)
4323 complete-list)
4324 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4325 (gdb-input (concat "complete " context command)
4326 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4327 (while gud-gdb-fetch-lines-in-progress
4328 (accept-process-output (get-buffer-process gud-comint-buffer))))
4329 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4331 (defun gud-gdbmi-fetch-lines-filter (string)
4332 "Custom filter function for `gud-gdbmi-completions'."
4333 (setq string (concat gud-gdb-fetch-lines-string
4334 (gud-gdbmi-marker-filter string)))
4335 (while (string-match "\n" string)
4336 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4337 gud-gdb-fetched-lines)
4338 (setq string (substring string (match-end 0))))
4341 (provide 'gdb-mi)
4343 ;;; gdb-mi.el ends here