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