Spelling fixes.
[emacs.git] / lisp / progmodes / gdb-mi.el
blob53807b8de30cd07601086d1caa99372f16881d65
1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007-2011 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 by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It is currently being 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 works without 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 don 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-version 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 (string-equal gdb-version "7.0+"))
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 "pp1 " (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 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
815 nil 'local)
816 (local-set-key "\C-i" 'completion-at-point)
818 (setq gdb-first-prompt t)
819 (setq gud-running nil)
821 (gdb-update)
823 (run-hooks 'gdb-mode-hook))
825 (defun gdb-init-1 ()
826 ;; (re-)initialise
827 (setq gdb-selected-frame nil
828 gdb-frame-number nil
829 gdb-thread-number nil
830 gdb-var-list nil
831 gdb-pending-triggers nil
832 gdb-output-sink 'user
833 gdb-location-alist nil
834 gdb-source-file-list nil
835 gdb-last-command nil
836 gdb-token-number 0
837 gdb-handler-alist '()
838 gdb-handler-number nil
839 gdb-prompt-name nil
840 gdb-first-done-or-error t
841 gdb-buffer-fringe-width (car (window-fringes))
842 gdb-debug-log nil
843 gdb-source-window nil
844 gdb-inferior-status nil
845 gdb-continuation nil
846 gdb-buf-publisher '()
847 gdb-threads-list '()
848 gdb-breakpoints-list '()
849 gdb-register-names '()
850 gdb-non-stop gdb-non-stop-setting)
852 (setq gdb-buffer-type 'gdbmi)
854 (gdb-force-mode-line-update
855 (propertize "initializing..." 'face font-lock-variable-name-face))
857 (gdb-get-buffer-create 'gdb-inferior-io)
858 (gdb-clear-inferior-io)
859 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
860 (gdb-input
861 ;; Needs GDB 6.4 onwards
862 (list (concat "-inferior-tty-set "
864 ;; The process can run on a remote host.
865 (process-get (get-process "gdb-inferior") 'remote-tty)
866 (process-tty-name (get-process "gdb-inferior"))))
867 'ignore))
868 (if (eq window-system 'w32)
869 (gdb-input (list "-gdb-set new-console off" 'ignore)))
870 (gdb-input (list "-gdb-set height 0" 'ignore))
872 (when gdb-non-stop
873 (gdb-input (list "-gdb-set non-stop 1" 'gdb-non-stop-handler)))
875 ;; find source file and compilation directory here
876 (gdb-input
877 ; Needs GDB 6.2 onwards.
878 (list "-file-list-exec-source-files" 'gdb-get-source-file-list))
879 (if gdb-create-source-file-list
880 (gdb-input
881 ; Needs GDB 6.0 onwards.
882 (list "-file-list-exec-source-file" 'gdb-get-source-file)))
883 (gdb-input
884 (list "-gdb-show prompt" 'gdb-get-prompt)))
886 (defun gdb-non-stop-handler ()
887 (goto-char (point-min))
888 (if (re-search-forward "No symbol" nil t)
889 (progn
890 (message
891 "This version of GDB doesn't support non-stop mode. Turning it off.")
892 (setq gdb-non-stop nil)
893 (setq gdb-version "pre-7.0"))
894 (setq gdb-version "7.0+")
895 (gdb-input (list "-gdb-set target-async 1" 'ignore))
896 (gdb-input (list "-enable-pretty-printing" 'ignore))))
898 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
900 (defun gdb-create-define-alist ()
901 "Create an alist of #define directives for GUD tooltips."
902 (let* ((file (buffer-file-name))
903 (output
904 (with-output-to-string
905 (with-current-buffer standard-output
906 (and file
907 (file-exists-p file)
908 ;; call-process doesn't work with remote file names.
909 (not (file-remote-p default-directory))
910 (call-process shell-file-name file
911 (list t nil) nil "-c"
912 (concat gdb-cpp-define-alist-program " "
913 gdb-cpp-define-alist-flags))))))
914 (define-list (split-string output "\n" t))
915 (name))
916 (setq gdb-define-alist nil)
917 (dolist (define define-list)
918 (setq name (nth 1 (split-string define "[( ]")))
919 (push (cons name define) gdb-define-alist))))
921 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
922 (defvar tooltip-use-echo-area)
924 (defun gdb-tooltip-print (expr)
925 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
926 (goto-char (point-min))
927 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
928 (tooltip-show
929 (concat expr " = " (read (match-string 1)))
930 (or gud-tooltip-echo-area tooltip-use-echo-area
931 (not (display-graphic-p)))))))
933 ;; If expr is a macro for a function don't print because of possible dangerous
934 ;; side-effects. Also printing a function within a tooltip generates an
935 ;; unexpected starting annotation (phase error).
936 (defun gdb-tooltip-print-1 (expr)
937 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
938 (goto-char (point-min))
939 (if (search-forward "expands to: " nil t)
940 (unless (looking-at "\\S-+.*(.*).*")
941 (gdb-input
942 (list (concat "-data-evaluate-expression " expr)
943 `(lambda () (gdb-tooltip-print ,expr))))))))
945 (defun gdb-init-buffer ()
946 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
947 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
948 (when gud-tooltip-mode
949 (make-local-variable 'gdb-define-alist)
950 (gdb-create-define-alist)
951 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
953 (defmacro gdb-if-arrow (arrow-position &rest body)
954 `(if ,arrow-position
955 (let ((buffer (marker-buffer ,arrow-position)) (line))
956 (if (equal buffer (window-buffer (posn-window end)))
957 (with-current-buffer buffer
958 (when (or (equal start end)
959 (equal (posn-point start)
960 (marker-position ,arrow-position)))
961 ,@body))))))
963 (defun gdb-mouse-until (event)
964 "Continue running until a source line past the current line.
965 The destination source line can be selected either by clicking
966 with mouse-3 on the fringe/margin or dragging the arrow
967 with mouse-1 (default bindings)."
968 (interactive "e")
969 (let ((start (event-start event))
970 (end (event-end event)))
971 (gdb-if-arrow gud-overlay-arrow-position
972 (setq line (line-number-at-pos (posn-point end)))
973 (gud-call (concat "until " (number-to-string line))))
974 (gdb-if-arrow gdb-disassembly-position
975 (save-excursion
976 (goto-char (point-min))
977 (forward-line (1- (line-number-at-pos (posn-point end))))
978 (forward-char 2)
979 (gud-call (concat "until *%a"))))))
981 (defun gdb-mouse-jump (event)
982 "Set execution address/line.
983 The destination source line can be selected either by clicking with C-mouse-3
984 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
985 Unlike `gdb-mouse-until' the destination address can be before the current
986 line, and no execution takes place."
987 (interactive "e")
988 (let ((start (event-start event))
989 (end (event-end event)))
990 (gdb-if-arrow gud-overlay-arrow-position
991 (setq line (line-number-at-pos (posn-point end)))
992 (progn
993 (gud-call (concat "tbreak " (number-to-string line)))
994 (gud-call (concat "jump " (number-to-string line)))))
995 (gdb-if-arrow gdb-disassembly-position
996 (save-excursion
997 (goto-char (point-min))
998 (forward-line (1- (line-number-at-pos (posn-point end))))
999 (forward-char 2)
1000 (progn
1001 (gud-call (concat "tbreak *%a"))
1002 (gud-call (concat "jump *%a")))))))
1004 (defcustom gdb-show-changed-values t
1005 "If non-nil change the face of out of scope variables and changed values.
1006 Out of scope variables are suppressed with `shadow' face.
1007 Changed values are highlighted with the face `font-lock-warning-face'."
1008 :type 'boolean
1009 :group 'gdb
1010 :version "22.1")
1012 (defcustom gdb-max-children 40
1013 "Maximum number of children before expansion requires confirmation."
1014 :type 'integer
1015 :group 'gdb
1016 :version "22.1")
1018 (defcustom gdb-delete-out-of-scope t
1019 "If non-nil delete watch expressions automatically when they go out of scope."
1020 :type 'boolean
1021 :group 'gdb
1022 :version "22.2")
1024 (defcustom gdb-speedbar-auto-raise nil
1025 "If non-nil raise speedbar every time display of watch expressions is\
1026 updated."
1027 :type 'boolean
1028 :group 'gdb
1029 :version "22.1")
1031 (defcustom gdb-use-colon-colon-notation nil
1032 "If non-nil use FUN::VAR format to display variables in the speedbar."
1033 :type 'boolean
1034 :group 'gdb
1035 :version "22.1")
1037 (defun gdb-speedbar-auto-raise (arg)
1038 "Toggle automatic raising of the speedbar for watch expressions.
1039 With prefix argument ARG, automatically raise speedbar if ARG is
1040 positive, otherwise don't automatically raise it."
1041 (interactive "P")
1042 (setq gdb-speedbar-auto-raise
1043 (if (null arg)
1044 (not gdb-speedbar-auto-raise)
1045 (> (prefix-numeric-value arg) 0)))
1046 (message (format "Auto raising %sabled"
1047 (if gdb-speedbar-auto-raise "en" "dis"))))
1049 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1050 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
1052 (declare-function tooltip-identifier-from-point "tooltip" (point))
1054 (defun gud-watch (&optional arg event)
1055 "Watch expression at point.
1056 With arg, enter name of variable to be watched in the minibuffer."
1057 (interactive (list current-prefix-arg last-input-event))
1058 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1059 (if (eq minor-mode 'gdbmi)
1060 (progn
1061 (if event (posn-set-point (event-end event)))
1062 (require 'tooltip)
1063 (save-selected-window
1064 (let ((expr
1065 (if arg
1066 (completing-read "Name of variable: "
1067 'gud-gdb-complete-command)
1068 (if (and transient-mark-mode mark-active)
1069 (buffer-substring (region-beginning) (region-end))
1070 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1071 (tooltip-identifier-from-point (point)))))))
1072 (set-text-properties 0 (length expr) nil expr)
1073 (gdb-input
1074 (list (concat"-var-create - * " expr "")
1075 `(lambda () (gdb-var-create-handler ,expr)))))))
1076 (message "gud-watch is a no-op in this mode."))))
1078 (defun gdb-var-create-handler (expr)
1079 (let* ((result (gdb-json-partial-output)))
1080 (if (not (bindat-get-field result 'msg))
1081 (let ((var
1082 (list (bindat-get-field result 'name)
1083 (if (and (string-equal gdb-current-language "c")
1084 gdb-use-colon-colon-notation gdb-selected-frame)
1085 (setq expr (concat gdb-selected-frame "::" expr))
1086 expr)
1087 (bindat-get-field result 'numchild)
1088 (bindat-get-field result 'type)
1089 (bindat-get-field result 'value)
1091 (bindat-get-field result 'has_more)
1092 gdb-frame-address)))
1093 (push var gdb-var-list)
1094 (speedbar 1)
1095 (unless (string-equal
1096 speedbar-initial-expansion-list-name "GUD")
1097 (speedbar-change-initial-expansion-list "GUD")))
1098 (message-box "No symbol \"%s\" in current context." expr))))
1100 (defun gdb-speedbar-update ()
1101 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1102 (not (gdb-pending-p 'gdb-speedbar-timer)))
1103 ;; Dummy command to update speedbar even when idle.
1104 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn))
1105 ;; Keep gdb-pending-triggers non-nil till end.
1106 (gdb-add-pending 'gdb-speedbar-timer)))
1108 (defun gdb-speedbar-timer-fn ()
1109 (if gdb-speedbar-auto-raise
1110 (raise-frame speedbar-frame))
1111 (gdb-delete-pending 'gdb-speedbar-timer)
1112 (speedbar-timer-fn))
1114 (defun gdb-var-evaluate-expression-handler (varnum changed)
1115 (goto-char (point-min))
1116 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1117 (let ((var (assoc varnum gdb-var-list)))
1118 (when var
1119 (if changed (setcar (nthcdr 5 var) 'changed))
1120 (setcar (nthcdr 4 var) (read (match-string 1)))))
1121 (gdb-speedbar-update))
1123 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1124 (defun gdb-var-list-children (varnum)
1125 (gdb-input
1126 (list (concat "-var-update " varnum) 'ignore))
1127 (gdb-input
1128 (list (concat "-var-list-children --all-values "
1129 varnum)
1130 `(lambda () (gdb-var-list-children-handler ,varnum)))))
1132 (defun gdb-var-list-children-handler (varnum)
1133 (let* ((var-list nil)
1134 (output (bindat-get-field (gdb-json-partial-output "child")))
1135 (children (bindat-get-field output 'children)))
1136 (catch 'child-already-watched
1137 (dolist (var gdb-var-list)
1138 (if (string-equal varnum (car var))
1139 (progn
1140 ;; With dynamic varobjs numchild may have increased.
1141 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1142 (push var var-list)
1143 (dolist (child children)
1144 (let ((varchild (list (bindat-get-field child 'name)
1145 (bindat-get-field child 'exp)
1146 (bindat-get-field child 'numchild)
1147 (bindat-get-field child 'type)
1148 (bindat-get-field child 'value)
1150 (bindat-get-field child 'has_more))))
1151 (if (assoc (car varchild) gdb-var-list)
1152 (throw 'child-already-watched nil))
1153 (push varchild var-list))))
1154 (push var var-list)))
1155 (setq gdb-var-list (nreverse var-list))))
1156 (gdb-speedbar-update))
1158 (defun gdb-var-set-format (format)
1159 "Set the output format for a variable displayed in the speedbar."
1160 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1161 (varnum (car var)))
1162 (gdb-input
1163 (list (concat "-var-set-format " varnum " " format) 'ignore))
1164 (gdb-var-update)))
1166 (defun gdb-var-delete-1 (var varnum)
1167 (gdb-input
1168 (list (concat "-var-delete " varnum) 'ignore))
1169 (setq gdb-var-list (delq var gdb-var-list))
1170 (dolist (varchild gdb-var-list)
1171 (if (string-match (concat (car var) "\\.") (car varchild))
1172 (setq gdb-var-list (delq varchild gdb-var-list)))))
1174 (defun gdb-var-delete ()
1175 "Delete watch expression at point from the speedbar."
1176 (interactive)
1177 (let ((text (speedbar-line-text)))
1178 (string-match "\\(\\S-+\\)" text)
1179 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1180 (varnum (car var)))
1181 (if (string-match "\\." (car var))
1182 (message-box "Can only delete a root expression")
1183 (gdb-var-delete-1 var varnum)))))
1185 (defun gdb-var-delete-children (varnum)
1186 "Delete children of variable object at point from the speedbar."
1187 (gdb-input
1188 (list (concat "-var-delete -c " varnum) 'ignore)))
1190 (defun gdb-edit-value (_text _token _indent)
1191 "Assign a value to a variable displayed in the speedbar."
1192 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1193 (varnum (car var)) (value))
1194 (setq value (read-string "New value: "))
1195 (gdb-input
1196 (list (concat "-var-assign " varnum " " value)
1197 `(lambda () (gdb-edit-value-handler ,value))))))
1199 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1201 (defun gdb-edit-value-handler (value)
1202 (goto-char (point-min))
1203 (if (re-search-forward gdb-error-regexp nil t)
1204 (message-box "Invalid number or expression (%s)" value)))
1206 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1207 (defun gdb-var-update ()
1208 (if (not (gdb-pending-p 'gdb-var-update))
1209 (gdb-input
1210 (list "-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 gdb-enable-debug
1676 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1677 (if (string-match "^-" string)
1678 ;; MI command
1679 (progn
1680 (setq gdb-first-done-or-error t)
1681 (process-send-string proc (concat string "\n")))
1682 ;; CLI command
1683 (if (string-match "\\\\$" string)
1684 (setq gdb-continuation (concat gdb-continuation string "\n"))
1685 (setq gdb-first-done-or-error t)
1686 (process-send-string proc (concat "-interpreter-exec console \""
1687 gdb-continuation string "\"\n"))
1688 (setq gdb-continuation nil))))
1690 (defun gdb-input (item)
1691 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1692 (setq gdb-token-number (1+ gdb-token-number))
1693 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1694 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1695 (process-send-string (get-buffer-process gud-comint-buffer)
1696 (concat (car item) "\n")))
1698 ;; NOFRAME is used for gud execution control commands
1699 (defun gdb-current-context-command (command)
1700 "Add --thread to gdb COMMAND when needed."
1701 (if (and gdb-thread-number
1702 (string-equal gdb-version "7.0+"))
1703 (concat command " --thread " gdb-thread-number)
1704 command))
1706 (defun gdb-current-context-buffer-name (name)
1707 "Add thread information and asterisks to string NAME.
1709 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1710 (concat "*" name
1711 (if (local-variable-p 'gdb-thread-number)
1712 (format " (bound to thread %s)" gdb-thread-number)
1714 "*"))
1716 (defun gdb-current-context-mode-name (mode)
1717 "Add thread information to MODE which is to be used as
1718 `mode-name'."
1719 (concat mode
1720 (if gdb-thread-number
1721 (format " [thread %s]" gdb-thread-number)
1722 "")))
1725 (defcustom gud-gdb-command-name "gdb -i=mi"
1726 "Default command to execute an executable under the GDB debugger."
1727 :type 'string
1728 :group 'gdb)
1730 (defun gdb-resync()
1731 (setq gud-running nil)
1732 (setq gdb-output-sink 'user)
1733 (setq gdb-pending-triggers nil))
1735 (defun gdb-update ()
1736 "Update buffers showing status of debug session."
1737 (when gdb-first-prompt
1738 (gdb-force-mode-line-update
1739 (propertize "initializing..." 'face font-lock-variable-name-face))
1740 (gdb-init-1)
1741 (setq gdb-first-prompt nil))
1743 (gdb-get-main-selected-frame)
1744 ;; We may need to update gdb-threads-list so we can use
1745 (gdb-get-buffer-create 'gdb-threads-buffer)
1746 ;; gdb-break-list is maintained in breakpoints handler
1747 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1749 (gdb-emit-signal gdb-buf-publisher 'update)
1751 (gdb-get-changed-registers)
1753 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1754 (dolist (var gdb-var-list)
1755 (setcar (nthcdr 5 var) nil))
1756 (gdb-var-update)))
1758 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1759 ;; because we may need to update current gud-running value without
1760 ;; changing current thread (see gdb-running)
1761 (defun gdb-setq-thread-number (number)
1762 "Only this function must be used to change `gdb-thread-number'
1763 value to NUMBER, because `gud-running' and `gdb-frame-number'
1764 need to be updated appropriately when current thread changes."
1765 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1766 (unless (string-equal number "0") (setq gdb-thread-number number))
1767 (setq gdb-frame-number "0")
1768 (gdb-update-gud-running))
1770 (defun gdb-update-gud-running ()
1771 "Set `gud-running' according to the state of current thread.
1773 `gdb-frame-number' is set to 0 if current thread is now stopped.
1775 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1776 cannot be reliably used to determine whether or not execution
1777 control buttons should be shown in menu or toolbar. Use
1778 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1779 instead.
1781 For all-stop mode, thread information is unavailable while target
1782 is running."
1783 (let ((old-value gud-running))
1784 (setq gud-running
1785 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1786 "running"))
1787 ;; Set frame number to "0" when _current_ threads stops
1788 (when (and (gdb-current-buffer-thread)
1789 (not (eq gud-running old-value)))
1790 (setq gdb-frame-number "0"))))
1792 (defun gdb-show-run-p ()
1793 "Return t if \"Run/continue\" should be shown on the toolbar."
1794 (or (not gdb-active-process)
1795 (and (or
1796 (not gdb-gud-control-all-threads)
1797 (not gdb-non-stop))
1798 (not gud-running))
1799 (and gdb-gud-control-all-threads
1800 (> gdb-stopped-threads-count 0))))
1802 (defun gdb-show-stop-p ()
1803 "Return t if \"Stop\" should be shown on the toolbar."
1804 (or (and (or
1805 (not gdb-gud-control-all-threads)
1806 (not gdb-non-stop))
1807 gud-running)
1808 (and gdb-gud-control-all-threads
1809 (> gdb-running-threads-count 0))))
1811 ;; GUD displays the selected GDB frame. This might might not be the current
1812 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1813 ;; visited breakpoint is, use that window.
1814 (defun gdb-display-source-buffer (buffer)
1815 (let* ((last-window (if gud-last-last-frame
1816 (get-buffer-window
1817 (gud-find-file (car gud-last-last-frame)))))
1818 (source-window (or last-window
1819 (if (and gdb-source-window
1820 (window-live-p gdb-source-window))
1821 gdb-source-window))))
1822 (when source-window
1823 (setq gdb-source-window source-window)
1824 (set-window-buffer source-window buffer))
1825 source-window))
1827 (defun gdb-car< (a b)
1828 (< (car a) (car b)))
1830 (defvar gdbmi-record-list
1831 '((gdb-gdb . "(gdb) \n")
1832 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1833 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1834 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1835 (gdb-console . "~\\(\".*?\"\\)\n")
1836 (gdb-internals . "&\\(\".*?\"\\)\n")
1837 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1838 (gdb-running . "\\*running,\\(.*?\n\\)")
1839 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1840 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1841 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1842 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1843 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1845 (defun gud-gdbmi-marker-filter (string)
1846 "Filter GDB/MI output."
1848 ;; Record transactions if logging is enabled.
1849 (when gdb-enable-debug
1850 (push (cons 'recv string) gdb-debug-log)
1851 (if (and gdb-debug-log-max
1852 (> (length gdb-debug-log) gdb-debug-log-max))
1853 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1855 ;; Recall the left over gud-marker-acc from last time
1856 (setq gud-marker-acc (concat gud-marker-acc string))
1858 ;; Start accumulating output for the GUD buffer
1859 (setq gdb-filter-output "")
1860 (let (output-record-list)
1862 ;; Process all the complete markers in this chunk.
1863 (dolist (gdbmi-record gdbmi-record-list)
1864 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1865 (push (list (match-beginning 0)
1866 (car gdbmi-record)
1867 (match-string 1 gud-marker-acc)
1868 (match-string 2 gud-marker-acc)
1869 (match-end 0))
1870 output-record-list)
1871 (setq gud-marker-acc
1872 (concat (substring gud-marker-acc 0 (match-beginning 0))
1873 ;; Pad with spaces to preserve position.
1874 (make-string (length (match-string 0 gud-marker-acc)) 32)
1875 (substring gud-marker-acc (match-end 0))))))
1877 (setq output-record-list (sort output-record-list 'gdb-car<))
1879 (dolist (output-record output-record-list)
1880 (let ((record-type (cadr output-record))
1881 (arg1 (nth 2 output-record))
1882 (arg2 (nth 3 output-record)))
1883 (if (eq record-type 'gdb-error)
1884 (gdb-done-or-error arg2 arg1 'error)
1885 (if (eq record-type 'gdb-done)
1886 (gdb-done-or-error arg2 arg1 'done)
1887 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1888 ;; error message on internal stream. Don't print to GUD buffer.
1889 (unless (and (eq record-type 'gdb-internals)
1890 (string-equal (read arg1) "No registers.\n"))
1891 (funcall record-type arg1))))))
1893 (setq gdb-output-sink 'user)
1894 ;; Remove padding.
1895 (string-match "^ *" gud-marker-acc)
1896 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1898 gdb-filter-output))
1900 (defun gdb-gdb (_output-field))
1902 (defun gdb-shell (output-field)
1903 (let ((gdb-output-sink gdb-output-sink))
1904 (setq gdb-filter-output
1905 (concat output-field gdb-filter-output))))
1907 (defun gdb-ignored-notification (_output-field))
1909 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1910 (defun gdb-thread-created (_output-field))
1911 (defun gdb-thread-exited (output-field)
1912 "Handle =thread-exited async record: unset `gdb-thread-number'
1913 if current thread exited and update threads list."
1914 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1915 (if (string= gdb-thread-number thread-id)
1916 (gdb-setq-thread-number nil))
1917 ;; When we continue current thread and it quickly exits,
1918 ;; gdb-pending-triggers left after gdb-running disallow us to
1919 ;; properly call -thread-info without --thread option. Thus we
1920 ;; need to use gdb-wait-for-pending.
1921 (gdb-wait-for-pending
1922 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1924 (defun gdb-thread-selected (output-field)
1925 "Handler for =thread-selected MI output record.
1927 Sets `gdb-thread-number' to new id."
1928 (let* ((result (gdb-json-string output-field))
1929 (thread-id (bindat-get-field result 'id)))
1930 (gdb-setq-thread-number thread-id)
1931 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1932 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1933 ;; as usually. Things happen to fast and second call (from
1934 ;; gdb-thread-selected handler) gets cut off by our beloved
1935 ;; gdb-pending-triggers.
1936 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1937 ;; body will get executed when `gdb-pending-triggers` is empty.
1938 (gdb-wait-for-pending
1939 (gdb-update))))
1941 (defun gdb-running (output-field)
1942 (let* ((thread-id
1943 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
1944 ;; We reset gdb-frame-number to nil if current thread has gone
1945 ;; running. This can't be done in gdb-thread-list-handler-custom
1946 ;; because we need correct gdb-frame-number by the time
1947 ;; -thread-info command is sent.
1948 (when (or (string-equal thread-id "all")
1949 (string-equal thread-id gdb-thread-number))
1950 (setq gdb-frame-number nil)))
1951 (setq gdb-inferior-status "running")
1952 (gdb-force-mode-line-update
1953 (propertize gdb-inferior-status 'face font-lock-type-face))
1954 (when (not gdb-non-stop)
1955 (setq gud-running t))
1956 (setq gdb-active-process t)
1957 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1959 (defun gdb-starting (_output-field)
1960 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1961 (setq gdb-inferior-status "running")
1962 (gdb-force-mode-line-update
1963 (propertize gdb-inferior-status 'face font-lock-type-face))
1964 (setq gdb-active-process t)
1965 (setq gud-running t)
1966 ;; GDB doesn't seem to respond to -thread-info before first stop or
1967 ;; thread exit (even in non-stop mode), so this is useless.
1968 ;; Behavior may change in the future.
1969 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1971 ;; -break-insert -t didn't give a reason before gdb 6.9
1973 (defun gdb-stopped (output-field)
1974 "Given the contents of *stopped MI async record, select new
1975 current thread and update GDB buffers."
1976 ;; Reason is available with target-async only
1977 (let* ((result (gdb-json-string output-field))
1978 (reason (bindat-get-field result 'reason))
1979 (thread-id (bindat-get-field result 'thread-id)))
1981 ;; -data-list-register-names needs to be issued for any stopped
1982 ;; thread
1983 (when (not gdb-register-names)
1984 (gdb-input
1985 (list (concat "-data-list-register-names"
1986 (if (string-equal gdb-version "7.0+")
1987 (concat" --thread " thread-id)))
1988 'gdb-register-names-handler)))
1990 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1991 ;;; because synchronous GDB doesn't give these fields with CLI.
1992 ;;; (when file
1993 ;;; (setq
1994 ;;; ;; Extract the frame position from the marker.
1995 ;;; gud-last-frame (cons file
1996 ;;; (string-to-number
1997 ;;; (match-string 6 gud-marker-acc)))))
1999 (setq gdb-inferior-status (or reason "unknown"))
2000 (gdb-force-mode-line-update
2001 (propertize gdb-inferior-status 'face font-lock-warning-face))
2002 (if (string-equal reason "exited-normally")
2003 (setq gdb-active-process nil))
2005 ;; Select new current thread.
2007 ;; Don't switch if we have no reasons selected
2008 (when gdb-switch-reasons
2009 ;; Switch from another stopped thread only if we have
2010 ;; gdb-switch-when-another-stopped:
2011 (when (or gdb-switch-when-another-stopped
2012 (not (string= "stopped"
2013 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2014 ;; Switch if current reason has been selected or we have no
2015 ;; reasons
2016 (if (or (eq gdb-switch-reasons t)
2017 (member reason gdb-switch-reasons))
2018 (when (not (string-equal gdb-thread-number thread-id))
2019 (message (concat "Switched to thread " thread-id))
2020 (gdb-setq-thread-number thread-id))
2021 (message (format "Thread %s stopped" thread-id)))))
2023 ;; Print "(gdb)" to GUD console
2024 (when gdb-first-done-or-error
2025 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2027 ;; In non-stop, we update information as soon as another thread gets
2028 ;; stopped
2029 (when (or gdb-first-done-or-error
2030 gdb-non-stop)
2031 ;; In all-stop this updates gud-running properly as well.
2032 (gdb-update)
2033 (setq gdb-first-done-or-error nil))
2034 (run-hook-with-args 'gdb-stopped-hooks result)))
2036 ;; Remove the trimmings from log stream containing debugging messages
2037 ;; being produced by GDB's internals, use warning face and send to GUD
2038 ;; buffer.
2039 (defun gdb-internals (output-field)
2040 (setq gdb-filter-output
2041 (gdb-concat-output
2042 gdb-filter-output
2043 (let ((error-message
2044 (read output-field)))
2045 (put-text-property
2046 0 (length error-message)
2047 'face font-lock-warning-face
2048 error-message)
2049 error-message))))
2051 ;; Remove the trimmings from the console stream and send to GUD buffer
2052 ;; (frontend MI commands should not print to this stream)
2053 (defun gdb-console (output-field)
2054 (setq gdb-filter-output
2055 (gdb-concat-output
2056 gdb-filter-output
2057 (read output-field))))
2059 (defun gdb-done-or-error (output-field token-number type)
2060 (if (string-equal token-number "")
2061 ;; Output from command entered by user
2062 (progn
2063 (setq gdb-output-sink 'user)
2064 (setq token-number nil)
2065 ;; MI error - send to minibuffer
2066 (when (eq type 'error)
2067 ;; Skip "msg=" from `output-field'
2068 (message (read (substring output-field 4)))
2069 ;; Don't send to the console twice. (If it is a console error
2070 ;; it is also in the console stream.)
2071 (setq output-field nil)))
2072 ;; Output from command from frontend.
2073 (setq gdb-output-sink 'emacs))
2075 (gdb-clear-partial-output)
2076 (when gdb-first-done-or-error
2077 (unless (or token-number gud-running)
2078 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2079 (gdb-update)
2080 (setq gdb-first-done-or-error nil))
2082 (setq gdb-filter-output
2083 (gdb-concat-output gdb-filter-output output-field))
2085 (if token-number
2086 (progn
2087 (with-current-buffer
2088 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2089 (funcall
2090 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2091 (setq gdb-handler-alist
2092 (assq-delete-all token-number gdb-handler-alist)))))
2094 (defun gdb-concat-output (so-far new)
2095 (let ((sink gdb-output-sink))
2096 (cond
2097 ((eq sink 'user) (concat so-far new))
2098 ((eq sink 'emacs)
2099 (gdb-append-to-partial-output new)
2100 so-far))))
2102 (defun gdb-append-to-partial-output (string)
2103 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2104 (goto-char (point-max))
2105 (insert string)))
2107 (defun gdb-clear-partial-output ()
2108 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2109 (erase-buffer)))
2111 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2112 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2114 Field names are wrapped in double quotes and equal signs are
2115 replaced with semicolons.
2117 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2118 partial output. This is used to get rid of useless keys in lists
2119 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2120 -break-info are examples of MI commands which issue such
2121 responses.
2123 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2124 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2125 -break-info output when it contains breakpoint script field
2126 incompatible with GDB/MI output syntax."
2127 (save-excursion
2128 (goto-char (point-min))
2129 (when fix-key
2130 (save-excursion
2131 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2132 (replace-match "" nil nil nil 1))))
2133 (when fix-list
2134 (save-excursion
2135 ;; Find positions of braces which enclose broken list
2136 (while (re-search-forward (concat fix-list "={\"") nil t)
2137 (let ((p1 (goto-char (- (point) 2)))
2138 (p2 (progn (forward-sexp)
2139 (1- (point)))))
2140 ;; Replace braces with brackets
2141 (save-excursion
2142 (goto-char p1)
2143 (delete-char 1)
2144 (insert "[")
2145 (goto-char p2)
2146 (delete-char 1)
2147 (insert "]"))))))
2148 (goto-char (point-min))
2149 (insert "{")
2150 (while (re-search-forward
2151 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2152 (replace-match "\"\\1\":\\2" nil nil))
2153 (goto-char (point-max))
2154 (insert "}")))
2156 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2157 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2159 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2160 (gdb-jsonify-buffer fix-key fix-list)
2161 (save-excursion
2162 (goto-char (point-min))
2163 (let ((json-array-type 'list))
2164 (json-read))))
2166 (defun gdb-json-string (string &optional fix-key fix-list)
2167 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2169 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2170 (with-temp-buffer
2171 (insert string)
2172 (gdb-json-read-buffer fix-key fix-list)))
2174 (defun gdb-json-partial-output (&optional fix-key fix-list)
2175 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2177 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2178 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2179 (gdb-json-read-buffer fix-key fix-list)))
2181 (defun gdb-line-posns (line)
2182 "Return a pair of LINE beginning and end positions."
2183 (let ((offset (1+ (- line (line-number-at-pos)))))
2184 (cons
2185 (line-beginning-position offset)
2186 (line-end-position offset))))
2188 (defmacro gdb-mark-line (line variable)
2189 "Set VARIABLE marker to point at beginning of LINE.
2191 If current window has no fringes, inverse colors on LINE.
2193 Return position where LINE begins."
2194 `(save-excursion
2195 (let* ((posns (gdb-line-posns ,line))
2196 (start-posn (car posns))
2197 (end-posn (cdr posns)))
2198 (set-marker ,variable (copy-marker start-posn))
2199 (when (not (> (car (window-fringes)) 0))
2200 (put-text-property start-posn end-posn
2201 'font-lock-face '(:inverse-video t)))
2202 start-posn)))
2204 (defun gdb-pad-string (string padding)
2205 (format (concat "%" (number-to-string padding) "s") string))
2207 ;; gdb-table struct is a way to programmatically construct simple
2208 ;; tables. It help to reliably align columns of data in GDB buffers
2209 ;; and provides
2210 (defstruct
2211 gdb-table
2212 (column-sizes nil)
2213 (rows nil)
2214 (row-properties nil)
2215 (right-align nil))
2217 (defun gdb-mapcar* (function &rest seqs)
2218 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2219 If there are several SEQS, FUNCTION is called with that many
2220 arguments, and mapping stops as sson as the shortest list runs
2221 out."
2222 (let ((shortest (apply #'min (mapcar #'length seqs))))
2223 (mapcar (lambda (i)
2224 (apply function
2225 (mapcar
2226 (lambda (seq)
2227 (nth i seq))
2228 seqs)))
2229 (number-sequence 0 (1- shortest)))))
2231 (defun gdb-table-add-row (table row &optional properties)
2232 "Add ROW of string to TABLE and recalculate column sizes.
2234 When non-nil, PROPERTIES will be added to the whole row when
2235 calling `gdb-table-string'."
2236 (let ((rows (gdb-table-rows table))
2237 (row-properties (gdb-table-row-properties table))
2238 (column-sizes (gdb-table-column-sizes table))
2239 (right-align (gdb-table-right-align table)))
2240 (when (not column-sizes)
2241 (setf (gdb-table-column-sizes table)
2242 (make-list (length row) 0)))
2243 (setf (gdb-table-rows table)
2244 (append rows (list row)))
2245 (setf (gdb-table-row-properties table)
2246 (append row-properties (list properties)))
2247 (setf (gdb-table-column-sizes table)
2248 (gdb-mapcar* (lambda (x s)
2249 (let ((new-x
2250 (max (abs x) (string-width (or s "")))))
2251 (if right-align new-x (- new-x))))
2252 (gdb-table-column-sizes table)
2253 row))
2254 ;; Avoid trailing whitespace at eol
2255 (if (not (gdb-table-right-align table))
2256 (setcar (last (gdb-table-column-sizes table)) 0))))
2258 (defun gdb-table-string (table &optional sep)
2259 "Return TABLE as a string with columns separated with SEP."
2260 (let ((column-sizes (gdb-table-column-sizes table)))
2261 (mapconcat
2262 'identity
2263 (gdb-mapcar*
2264 (lambda (row properties)
2265 (apply 'propertize
2266 (mapconcat 'identity
2267 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2268 row column-sizes)
2269 sep)
2270 properties))
2271 (gdb-table-rows table)
2272 (gdb-table-row-properties table))
2273 "\n")))
2275 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2276 (defun gdb-get-many-fields (struct &rest fields)
2277 "Return a list of FIELDS values from STRUCT."
2278 (let ((values))
2279 (dolist (field fields values)
2280 (setq values (append values (list (bindat-get-field struct field)))))))
2282 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2283 handler-name
2284 &optional signal-list)
2285 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2286 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2287 buffer with `gdb-bind-function-to-buffer'.
2289 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2290 defined trigger is called with an argument from SIGNAL-LIST. It's
2291 not recommended to define triggers with empty SIGNAL-LIST.
2292 Normally triggers should respond at least to 'update signal.
2294 Normally the trigger defined by this command must be called from
2295 the buffer where HANDLER-NAME must work. This should be done so
2296 that buffer-local thread number may be used in GDB-COMMAND (by
2297 calling `gdb-current-context-command').
2298 `gdb-bind-function-to-buffer' is used to achieve this, see
2299 `gdb-get-buffer-create'.
2301 Triggers defined by this command are meant to be used as a
2302 trigger argument when describing buffer types with
2303 `gdb-set-buffer-rules'."
2304 `(defun ,trigger-name (&optional signal)
2305 (when
2306 (or (not ,signal-list)
2307 (memq signal ,signal-list))
2308 (when (not (gdb-pending-p
2309 (cons (current-buffer) ',trigger-name)))
2310 (gdb-input
2311 (list ,gdb-command
2312 (gdb-bind-function-to-buffer ',handler-name (current-buffer))))
2313 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2315 ;; Used by disassembly buffer only, the rest use
2316 ;; def-gdb-trigger-and-handler
2317 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2318 &optional nopreserve)
2319 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2321 Handlers are normally called from the buffers they put output in.
2323 Delete ((current-buffer) . TRIGGER-NAME) from
2324 `gdb-pending-triggers', erase current buffer and evaluate
2325 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2327 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2328 `(defun ,handler-name ()
2329 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2330 (let* ((buffer-read-only nil)
2331 (window (get-buffer-window (current-buffer) 0))
2332 (start (window-start window))
2333 (p (window-point window)))
2334 (erase-buffer)
2335 (,custom-defun)
2336 (gdb-update-buffer-name)
2337 ,(when (not nopreserve)
2338 '(set-window-start window start)
2339 '(set-window-point window p)))))
2341 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2342 handler-name custom-defun
2343 &optional signal-list)
2344 "Define trigger and handler.
2346 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2347 `def-gdb-auto-update-trigger'.
2349 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2350 `def-gdb-auto-update-handler'."
2351 `(progn
2352 (def-gdb-auto-update-trigger ,trigger-name
2353 ,gdb-command
2354 ,handler-name ,signal-list)
2355 (def-gdb-auto-update-handler ,handler-name
2356 ,trigger-name ,custom-defun)))
2360 ;; Breakpoint buffer : This displays the output of `-break-list'.
2361 (def-gdb-trigger-and-handler
2362 gdb-invalidate-breakpoints "-break-list"
2363 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2364 '(start update))
2366 (gdb-set-buffer-rules
2367 'gdb-breakpoints-buffer
2368 'gdb-breakpoints-buffer-name
2369 'gdb-breakpoints-mode
2370 'gdb-invalidate-breakpoints)
2372 (defun gdb-breakpoints-list-handler-custom ()
2373 (let ((breakpoints-list (bindat-get-field
2374 (gdb-json-partial-output "bkpt" "script")
2375 'BreakpointTable 'body))
2376 (table (make-gdb-table)))
2377 (setq gdb-breakpoints-list nil)
2378 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2379 (dolist (breakpoint breakpoints-list)
2380 (add-to-list 'gdb-breakpoints-list
2381 (cons (bindat-get-field breakpoint 'number)
2382 breakpoint))
2383 (let ((at (bindat-get-field breakpoint 'at))
2384 (pending (bindat-get-field breakpoint 'pending))
2385 (func (bindat-get-field breakpoint 'func))
2386 (type (bindat-get-field breakpoint 'type)))
2387 (gdb-table-add-row table
2388 (list
2389 (bindat-get-field breakpoint 'number)
2390 type
2391 (bindat-get-field breakpoint 'disp)
2392 (let ((flag (bindat-get-field breakpoint 'enabled)))
2393 (if (string-equal flag "y")
2394 (propertize "y" 'font-lock-face font-lock-warning-face)
2395 (propertize "n" 'font-lock-face font-lock-comment-face)))
2396 (bindat-get-field breakpoint 'addr)
2397 (bindat-get-field breakpoint 'times)
2398 (if (string-match ".*watchpoint" type)
2399 (bindat-get-field breakpoint 'what)
2400 (or pending at
2401 (concat "in "
2402 (propertize (or func "unknown")
2403 'font-lock-face font-lock-function-name-face)
2404 (gdb-frame-location breakpoint)))))
2405 ;; Add clickable properties only for breakpoints with file:line
2406 ;; information
2407 (append (list 'gdb-breakpoint breakpoint)
2408 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2409 mouse-face highlight))))))
2410 (insert (gdb-table-string table " "))
2411 (gdb-place-breakpoints)))
2413 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2414 (defun gdb-place-breakpoints ()
2415 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2416 (dolist (buffer (buffer-list))
2417 (with-current-buffer buffer
2418 (if (and (eq gud-minor-mode 'gdbmi)
2419 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2420 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2421 (dolist (breakpoint gdb-breakpoints-list)
2422 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2423 ; an associative list
2424 (line (bindat-get-field breakpoint 'line)))
2425 (when line
2426 (let ((file (bindat-get-field breakpoint 'fullname))
2427 (flag (bindat-get-field breakpoint 'enabled))
2428 (bptno (bindat-get-field breakpoint 'number)))
2429 (unless (file-exists-p file)
2430 (setq file (cdr (assoc bptno gdb-location-alist))))
2431 (if (and file
2432 (not (string-equal file "File not found")))
2433 (with-current-buffer
2434 (find-file-noselect file 'nowarn)
2435 (gdb-init-buffer)
2436 ;; Only want one breakpoint icon at each location.
2437 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2438 (string-to-number line)))
2439 (gdb-input
2440 (list (concat "list " file ":1")
2441 'ignore))
2442 (gdb-input
2443 (list "-file-list-exec-source-file"
2444 `(lambda () (gdb-get-location
2445 ,bptno ,line ,flag))))))))))
2447 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2449 (defun gdb-get-location (bptno line flag)
2450 "Find the directory containing the relevant source file.
2451 Put in buffer and place breakpoint icon."
2452 (goto-char (point-min))
2453 (catch 'file-not-found
2454 (if (re-search-forward gdb-source-file-regexp nil t)
2455 (delete (cons bptno "File not found") gdb-location-alist)
2456 (push (cons bptno (match-string 1)) gdb-location-alist)
2457 (gdb-resync)
2458 (unless (assoc bptno gdb-location-alist)
2459 (push (cons bptno "File not found") gdb-location-alist)
2460 (message-box "Cannot find source file for breakpoint location.
2461 Add directory to search path for source files using the GDB command, dir."))
2462 (throw 'file-not-found nil))
2463 (with-current-buffer (find-file-noselect (match-string 1))
2464 (gdb-init-buffer)
2465 ;; only want one breakpoint icon at each location
2466 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2468 (add-hook 'find-file-hook 'gdb-find-file-hook)
2470 (defun gdb-find-file-hook ()
2471 "Set up buffer for debugging if file is part of the source code
2472 of the current session."
2473 (if (and (buffer-name gud-comint-buffer)
2474 ;; in case gud or gdb-ui is just loaded
2475 gud-comint-buffer
2476 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2477 'gdbmi))
2478 (if (member buffer-file-name gdb-source-file-list)
2479 (with-current-buffer (find-buffer-visiting buffer-file-name)
2480 (gdb-init-buffer)))))
2482 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2483 (declare-function gud-break "gdb-mi" t t) ; gud-def
2484 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2486 (defun gdb-mouse-set-clear-breakpoint (event)
2487 "Set/clear breakpoint in left fringe/margin at mouse click.
2488 If not in a source or disassembly buffer just set point."
2489 (interactive "e")
2490 (mouse-minibuffer-check event)
2491 (let ((posn (event-end event)))
2492 (with-selected-window (posn-window posn)
2493 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2494 (if (numberp (posn-point posn))
2495 (save-excursion
2496 (goto-char (posn-point posn))
2497 (if (or (posn-object posn)
2498 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2499 'breakpoint))
2500 (gud-remove nil)
2501 (gud-break nil)))))
2502 (posn-set-point posn))))
2504 (defun gdb-mouse-toggle-breakpoint-margin (event)
2505 "Enable/disable breakpoint in left margin with mouse click."
2506 (interactive "e")
2507 (mouse-minibuffer-check event)
2508 (let ((posn (event-end event)))
2509 (if (numberp (posn-point posn))
2510 (with-selected-window (posn-window posn)
2511 (save-excursion
2512 (goto-char (posn-point posn))
2513 (if (posn-object posn)
2514 (gud-basic-call
2515 (let ((bptno (get-text-property
2516 0 'gdb-bptno (car (posn-string posn)))))
2517 (concat
2518 (if (get-text-property
2519 0 'gdb-enabled (car (posn-string posn)))
2520 "-break-disable "
2521 "-break-enable ")
2522 bptno)))))))))
2524 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2525 "Enable/disable breakpoint in left fringe with mouse click."
2526 (interactive "e")
2527 (mouse-minibuffer-check event)
2528 (let* ((posn (event-end event))
2529 (pos (posn-point posn))
2530 obj)
2531 (when (numberp pos)
2532 (with-selected-window (posn-window posn)
2533 (with-current-buffer (window-buffer (selected-window))
2534 (goto-char pos)
2535 (dolist (overlay (overlays-in pos pos))
2536 (when (overlay-get overlay 'put-break)
2537 (setq obj (overlay-get overlay 'before-string))))
2538 (when (stringp obj)
2539 (gud-basic-call
2540 (concat
2541 (if (get-text-property 0 'gdb-enabled obj)
2542 "-break-disable "
2543 "-break-enable ")
2544 (get-text-property 0 'gdb-bptno obj)))))))))
2546 (defun gdb-breakpoints-buffer-name ()
2547 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2549 (def-gdb-display-buffer
2550 gdb-display-breakpoints-buffer
2551 'gdb-breakpoints-buffer
2552 "Display status of user-settable breakpoints.")
2554 (def-gdb-frame-for-buffer
2555 gdb-frame-breakpoints-buffer
2556 'gdb-breakpoints-buffer
2557 "Display status of user-settable breakpoints in a new frame.")
2559 (defvar gdb-breakpoints-mode-map
2560 (let ((map (make-sparse-keymap))
2561 (menu (make-sparse-keymap "Breakpoints")))
2562 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2563 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2564 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2565 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2566 (suppress-keymap map)
2567 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2568 (define-key map " " 'gdb-toggle-breakpoint)
2569 (define-key map "D" 'gdb-delete-breakpoint)
2570 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2571 (define-key map "q" 'gdb-delete-frame-or-window)
2572 (define-key map "\r" 'gdb-goto-breakpoint)
2573 (define-key map "\t" (lambda ()
2574 (interactive)
2575 (gdb-set-window-buffer
2576 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2577 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2578 (define-key map [follow-link] 'mouse-face)
2579 map))
2581 (defun gdb-delete-frame-or-window ()
2582 "Delete frame if there is only one window. Otherwise delete the window."
2583 (interactive)
2584 (if (one-window-p) (delete-frame)
2585 (delete-window)))
2587 ;;from make-mode-line-mouse-map
2588 (defun gdb-make-header-line-mouse-map (mouse function) "\
2589 Return a keymap with single entry for mouse key MOUSE on the header line.
2590 MOUSE is defined to run function FUNCTION with no args in the buffer
2591 corresponding to the mode line clicked."
2592 (let ((map (make-sparse-keymap)))
2593 (define-key map (vector 'header-line mouse) function)
2594 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2595 map))
2597 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2598 `(propertize ,name
2599 'help-echo ,help-echo
2600 'mouse-face ',mouse-face
2601 'face ',face
2602 'local-map
2603 (gdb-make-header-line-mouse-map
2604 'mouse-1
2605 (lambda (event) (interactive "e")
2606 (save-selected-window
2607 (select-window (posn-window (event-start event)))
2608 (gdb-set-window-buffer
2609 (gdb-get-buffer-create ',buffer) t) )))))
2612 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2613 ;;; Threads view
2615 (defun gdb-threads-buffer-name ()
2616 (concat "*threads of " (gdb-get-target-string) "*"))
2618 (def-gdb-display-buffer
2619 gdb-display-threads-buffer
2620 'gdb-threads-buffer
2621 "Display GDB threads.")
2623 (def-gdb-frame-for-buffer
2624 gdb-frame-threads-buffer
2625 'gdb-threads-buffer
2626 "Display GDB threads in a new frame.")
2628 (def-gdb-trigger-and-handler
2629 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2630 gdb-thread-list-handler gdb-thread-list-handler-custom
2631 '(start update update-threads))
2633 (gdb-set-buffer-rules
2634 'gdb-threads-buffer
2635 'gdb-threads-buffer-name
2636 'gdb-threads-mode
2637 'gdb-invalidate-threads)
2639 (defvar gdb-threads-font-lock-keywords
2640 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2641 (" \\(stopped\\)" (1 font-lock-warning-face))
2642 (" \\(running\\)" (1 font-lock-string-face))
2643 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2644 "Font lock keywords used in `gdb-threads-mode'.")
2646 (defvar gdb-threads-mode-map
2647 (let ((map (make-sparse-keymap)))
2648 (define-key map "\r" 'gdb-select-thread)
2649 (define-key map "f" 'gdb-display-stack-for-thread)
2650 (define-key map "F" 'gdb-frame-stack-for-thread)
2651 (define-key map "l" 'gdb-display-locals-for-thread)
2652 (define-key map "L" 'gdb-frame-locals-for-thread)
2653 (define-key map "r" 'gdb-display-registers-for-thread)
2654 (define-key map "R" 'gdb-frame-registers-for-thread)
2655 (define-key map "d" 'gdb-display-disassembly-for-thread)
2656 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2657 (define-key map "i" 'gdb-interrupt-thread)
2658 (define-key map "c" 'gdb-continue-thread)
2659 (define-key map "s" 'gdb-step-thread)
2660 (define-key map "\t"
2661 (lambda ()
2662 (interactive)
2663 (gdb-set-window-buffer
2664 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2665 (define-key map [mouse-2] 'gdb-select-thread)
2666 (define-key map [follow-link] 'mouse-face)
2667 map))
2669 (defvar gdb-threads-header
2670 (list
2671 (gdb-propertize-header
2672 "Breakpoints" gdb-breakpoints-buffer
2673 "mouse-1: select" mode-line-highlight mode-line-inactive)
2675 (gdb-propertize-header "Threads" gdb-threads-buffer
2676 nil nil mode-line)))
2678 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2679 "Major mode for GDB threads."
2680 (setq gdb-thread-position (make-marker))
2681 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2682 (setq header-line-format gdb-threads-header)
2683 (set (make-local-variable 'font-lock-defaults)
2684 '(gdb-threads-font-lock-keywords))
2685 'gdb-invalidate-threads)
2687 (defun gdb-thread-list-handler-custom ()
2688 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2689 (table (make-gdb-table))
2690 (marked-line nil))
2691 (setq gdb-threads-list nil)
2692 (setq gdb-running-threads-count 0)
2693 (setq gdb-stopped-threads-count 0)
2694 (set-marker gdb-thread-position nil)
2696 (dolist (thread (reverse threads-list))
2697 (let ((running (equal (bindat-get-field thread 'state) "running")))
2698 (add-to-list 'gdb-threads-list
2699 (cons (bindat-get-field thread 'id)
2700 thread))
2701 (if running
2702 (incf gdb-running-threads-count)
2703 (incf gdb-stopped-threads-count))
2705 (gdb-table-add-row table
2706 (list
2707 (bindat-get-field thread 'id)
2708 (concat
2709 (if gdb-thread-buffer-verbose-names
2710 (concat (bindat-get-field thread 'target-id) " ") "")
2711 (bindat-get-field thread 'state)
2712 ;; Include frame information for stopped threads
2713 (if (not running)
2714 (concat
2715 " in " (bindat-get-field thread 'frame 'func)
2716 (if gdb-thread-buffer-arguments
2717 (concat
2718 " ("
2719 (let ((args (bindat-get-field thread 'frame 'args)))
2720 (mapconcat
2721 (lambda (arg)
2722 (apply #'format "%s=%s"
2723 (gdb-get-many-fields arg 'name 'value)))
2724 args ","))
2725 ")")
2727 (if gdb-thread-buffer-locations
2728 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2729 (if gdb-thread-buffer-addresses
2730 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2731 "")))
2732 (list
2733 'gdb-thread thread
2734 'mouse-face 'highlight
2735 'help-echo "mouse-2, RET: select thread")))
2736 (when (string-equal gdb-thread-number
2737 (bindat-get-field thread 'id))
2738 (setq marked-line (length gdb-threads-list))))
2739 (insert (gdb-table-string table " "))
2740 (when marked-line
2741 (gdb-mark-line marked-line gdb-thread-position)))
2742 ;; We update gud-running here because we need to make sure that
2743 ;; gdb-threads-list is up-to-date
2744 (gdb-update-gud-running)
2745 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2747 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2748 "Define a NAME command which will act upon thread on the current line.
2750 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2751 be the value of 'gdb-thread property of the current line. If
2752 'gdb-thread is nil, error is signaled."
2753 `(defun ,name (&optional event)
2754 ,(when doc doc)
2755 (interactive (list last-input-event))
2756 (if event (posn-set-point (event-end event)))
2757 (save-excursion
2758 (beginning-of-line)
2759 (let ((thread (get-text-property (point) 'gdb-thread)))
2760 (if thread
2761 ,custom-defun
2762 (error "Not recognized as thread line"))))))
2764 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
2765 &optional doc)
2766 "Define a NAME which will call BUFFER-COMMAND with id of thread
2767 on the current line."
2768 `(def-gdb-thread-buffer-command ,name
2769 (,buffer-command (bindat-get-field thread 'id))
2770 ,doc))
2772 (def-gdb-thread-buffer-command gdb-select-thread
2773 (let ((new-id (bindat-get-field thread 'id)))
2774 (gdb-setq-thread-number new-id)
2775 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2776 (gdb-update))
2777 "Select the thread at current line of threads buffer.")
2779 (def-gdb-thread-buffer-simple-command
2780 gdb-display-stack-for-thread
2781 gdb-preemptively-display-stack-buffer
2782 "Display stack buffer for the thread at current line.")
2784 (def-gdb-thread-buffer-simple-command
2785 gdb-display-locals-for-thread
2786 gdb-preemptively-display-locals-buffer
2787 "Display locals buffer for the thread at current line.")
2789 (def-gdb-thread-buffer-simple-command
2790 gdb-display-registers-for-thread
2791 gdb-preemptively-display-registers-buffer
2792 "Display registers buffer for the thread at current line.")
2794 (def-gdb-thread-buffer-simple-command
2795 gdb-display-disassembly-for-thread
2796 gdb-preemptively-display-disassembly-buffer
2797 "Display disassembly buffer for the thread at current line.")
2799 (def-gdb-thread-buffer-simple-command
2800 gdb-frame-stack-for-thread
2801 gdb-frame-stack-buffer
2802 "Display a new frame with stack buffer for the thread at
2803 current line.")
2805 (def-gdb-thread-buffer-simple-command
2806 gdb-frame-locals-for-thread
2807 gdb-frame-locals-buffer
2808 "Display a new frame with locals buffer for the thread at
2809 current line.")
2811 (def-gdb-thread-buffer-simple-command
2812 gdb-frame-registers-for-thread
2813 gdb-frame-registers-buffer
2814 "Display a new frame with registers buffer for the thread at
2815 current line.")
2817 (def-gdb-thread-buffer-simple-command
2818 gdb-frame-disassembly-for-thread
2819 gdb-frame-disassembly-buffer
2820 "Display a new frame with disassembly buffer for the thread at
2821 current line.")
2823 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2824 "Define a NAME which will execute GUD-COMMAND with
2825 `gdb-thread-number' locally bound to id of thread on the current
2826 line."
2827 `(def-gdb-thread-buffer-command ,name
2828 (if gdb-non-stop
2829 (let ((gdb-thread-number (bindat-get-field thread 'id))
2830 (gdb-gud-control-all-threads nil))
2831 (call-interactively #',gud-command))
2832 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2833 ,doc))
2835 (def-gdb-thread-buffer-gud-command
2836 gdb-interrupt-thread
2837 gud-stop-subjob
2838 "Interrupt thread at current line.")
2840 (def-gdb-thread-buffer-gud-command
2841 gdb-continue-thread
2842 gud-cont
2843 "Continue thread at current line.")
2845 (def-gdb-thread-buffer-gud-command
2846 gdb-step-thread
2847 gud-step
2848 "Step thread at current line.")
2851 ;;; Memory view
2853 (defcustom gdb-memory-rows 8
2854 "Number of data rows in memory window."
2855 :type 'integer
2856 :group 'gud
2857 :version "23.2")
2859 (defcustom gdb-memory-columns 4
2860 "Number of data columns in memory window."
2861 :type 'integer
2862 :group 'gud
2863 :version "23.2")
2865 (defcustom gdb-memory-format "x"
2866 "Display format of data items in memory window."
2867 :type '(choice (const :tag "Hexadecimal" "x")
2868 (const :tag "Signed decimal" "d")
2869 (const :tag "Unsigned decimal" "u")
2870 (const :tag "Octal" "o")
2871 (const :tag "Binary" "t"))
2872 :group 'gud
2873 :version "22.1")
2875 (defcustom gdb-memory-unit 4
2876 "Unit size of data items in memory window."
2877 :type '(choice (const :tag "Byte" 1)
2878 (const :tag "Halfword" 2)
2879 (const :tag "Word" 4)
2880 (const :tag "Giant word" 8))
2881 :group 'gud
2882 :version "23.2")
2884 (def-gdb-trigger-and-handler
2885 gdb-invalidate-memory
2886 (format "-data-read-memory %s %s %d %d %d"
2887 gdb-memory-address
2888 gdb-memory-format
2889 gdb-memory-unit
2890 gdb-memory-rows
2891 gdb-memory-columns)
2892 gdb-read-memory-handler
2893 gdb-read-memory-custom
2894 '(start update))
2896 (gdb-set-buffer-rules
2897 'gdb-memory-buffer
2898 'gdb-memory-buffer-name
2899 'gdb-memory-mode
2900 'gdb-invalidate-memory)
2902 (defun gdb-memory-column-width (size format)
2903 "Return length of string with memory unit of SIZE in FORMAT.
2905 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2906 in `gdb-memory-format'."
2907 (let ((format-base (cdr (assoc format
2908 '(("x" . 16)
2909 ("d" . 10) ("u" . 10)
2910 ("o" . 8)
2911 ("t" . 2))))))
2912 (if format-base
2913 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2914 (cond ((string-equal format "x")
2915 (+ 2 res)) ; hexadecimal numbers have 0x in front
2916 ((or (string-equal format "d")
2917 (string-equal format "o"))
2918 (1+ res))
2919 (t res)))
2920 (error "Unknown format"))))
2922 (defun gdb-read-memory-custom ()
2923 (let* ((res (gdb-json-partial-output))
2924 (err-msg (bindat-get-field res 'msg)))
2925 (if (not err-msg)
2926 (let ((memory (bindat-get-field res 'memory)))
2927 (setq gdb-memory-address (bindat-get-field res 'addr))
2928 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2929 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2930 (setq gdb-memory-last-address gdb-memory-address)
2931 (dolist (row memory)
2932 (insert (concat (bindat-get-field row 'addr) ":"))
2933 (dolist (column (bindat-get-field row 'data))
2934 (insert (gdb-pad-string column
2935 (+ 2 (gdb-memory-column-width
2936 gdb-memory-unit
2937 gdb-memory-format)))))
2938 (newline)))
2939 ;; Show last page instead of empty buffer when out of bounds
2940 (progn
2941 (let ((gdb-memory-address gdb-memory-last-address))
2942 (gdb-invalidate-memory 'update)
2943 (error err-msg))))))
2945 (defvar gdb-memory-mode-map
2946 (let ((map (make-sparse-keymap)))
2947 (suppress-keymap map t)
2948 (define-key map "q" 'kill-this-buffer)
2949 (define-key map "n" 'gdb-memory-show-next-page)
2950 (define-key map "p" 'gdb-memory-show-previous-page)
2951 (define-key map "a" 'gdb-memory-set-address)
2952 (define-key map "t" 'gdb-memory-format-binary)
2953 (define-key map "o" 'gdb-memory-format-octal)
2954 (define-key map "u" 'gdb-memory-format-unsigned)
2955 (define-key map "d" 'gdb-memory-format-signed)
2956 (define-key map "x" 'gdb-memory-format-hexadecimal)
2957 (define-key map "b" 'gdb-memory-unit-byte)
2958 (define-key map "h" 'gdb-memory-unit-halfword)
2959 (define-key map "w" 'gdb-memory-unit-word)
2960 (define-key map "g" 'gdb-memory-unit-giant)
2961 (define-key map "R" 'gdb-memory-set-rows)
2962 (define-key map "C" 'gdb-memory-set-columns)
2963 map))
2965 (defun gdb-memory-set-address-event (event)
2966 "Handle a click on address field in memory buffer header."
2967 (interactive "e")
2968 (save-selected-window
2969 (select-window (posn-window (event-start event)))
2970 (gdb-memory-set-address)))
2972 ;; Non-event version for use within keymap
2973 (defun gdb-memory-set-address ()
2974 "Set the start memory address."
2975 (interactive)
2976 (let ((arg (read-from-minibuffer "Memory address: ")))
2977 (setq gdb-memory-address arg))
2978 (gdb-invalidate-memory 'update))
2980 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2981 "Define a function NAME which reads new VAR value from minibuffer."
2982 `(defun ,name (event)
2983 ,(when doc doc)
2984 (interactive "e")
2985 (save-selected-window
2986 (select-window (posn-window (event-start event)))
2987 (let* ((arg (read-from-minibuffer ,echo-string))
2988 (count (string-to-number arg)))
2989 (if (<= count 0)
2990 (error "Positive number only")
2991 (customize-set-variable ',variable count)
2992 (gdb-invalidate-memory 'update))))))
2994 (def-gdb-set-positive-number
2995 gdb-memory-set-rows
2996 gdb-memory-rows
2997 "Rows: "
2998 "Set the number of data rows in memory window.")
3000 (def-gdb-set-positive-number
3001 gdb-memory-set-columns
3002 gdb-memory-columns
3003 "Columns: "
3004 "Set the number of data columns in memory window.")
3006 (defmacro def-gdb-memory-format (name format doc)
3007 "Define a function NAME to switch memory buffer to use FORMAT.
3009 DOC is an optional documentation string."
3010 `(defun ,name () ,(when doc doc)
3011 (interactive)
3012 (customize-set-variable 'gdb-memory-format ,format)
3013 (gdb-invalidate-memory 'update)))
3015 (def-gdb-memory-format
3016 gdb-memory-format-binary "t"
3017 "Set the display format to binary.")
3019 (def-gdb-memory-format
3020 gdb-memory-format-octal "o"
3021 "Set the display format to octal.")
3023 (def-gdb-memory-format
3024 gdb-memory-format-unsigned "u"
3025 "Set the display format to unsigned decimal.")
3027 (def-gdb-memory-format
3028 gdb-memory-format-signed "d"
3029 "Set the display format to decimal.")
3031 (def-gdb-memory-format
3032 gdb-memory-format-hexadecimal "x"
3033 "Set the display format to hexadecimal.")
3035 (defvar gdb-memory-format-map
3036 (let ((map (make-sparse-keymap)))
3037 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3038 map)
3039 "Keymap to select format in the header line.")
3041 (defvar gdb-memory-format-menu
3042 (let ((map (make-sparse-keymap "Format")))
3044 (define-key map [binary]
3045 '(menu-item "Binary" gdb-memory-format-binary
3046 :button (:radio . (equal gdb-memory-format "t"))))
3047 (define-key map [octal]
3048 '(menu-item "Octal" gdb-memory-format-octal
3049 :button (:radio . (equal gdb-memory-format "o"))))
3050 (define-key map [unsigned]
3051 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3052 :button (:radio . (equal gdb-memory-format "u"))))
3053 (define-key map [signed]
3054 '(menu-item "Signed Decimal" gdb-memory-format-signed
3055 :button (:radio . (equal gdb-memory-format "d"))))
3056 (define-key map [hexadecimal]
3057 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3058 :button (:radio . (equal gdb-memory-format "x"))))
3059 map)
3060 "Menu of display formats in the header line.")
3062 (defun gdb-memory-format-menu (event)
3063 (interactive "@e")
3064 (x-popup-menu event gdb-memory-format-menu))
3066 (defun gdb-memory-format-menu-1 (event)
3067 (interactive "e")
3068 (save-selected-window
3069 (select-window (posn-window (event-start event)))
3070 (let* ((selection (gdb-memory-format-menu event))
3071 (binding (and selection (lookup-key gdb-memory-format-menu
3072 (vector (car selection))))))
3073 (if binding (call-interactively binding)))))
3075 (defmacro def-gdb-memory-unit (name unit-size doc)
3076 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3078 DOC is an optional documentation string."
3079 `(defun ,name () ,(when doc doc)
3080 (interactive)
3081 (customize-set-variable 'gdb-memory-unit ,unit-size)
3082 (gdb-invalidate-memory 'update)))
3084 (def-gdb-memory-unit gdb-memory-unit-giant 8
3085 "Set the unit size to giant words (eight bytes).")
3087 (def-gdb-memory-unit gdb-memory-unit-word 4
3088 "Set the unit size to words (four bytes).")
3090 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3091 "Set the unit size to halfwords (two bytes).")
3093 (def-gdb-memory-unit gdb-memory-unit-byte 1
3094 "Set the unit size to bytes.")
3096 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3097 "Define a function NAME which show new address in memory buffer.
3099 The defined function switches Memory buffer to show address
3100 stored in ADDRESS-VAR variable.
3102 DOC is an optional documentation string."
3103 `(defun ,name
3104 ,(when doc doc)
3105 (interactive)
3106 (let ((gdb-memory-address ,address-var))
3107 (gdb-invalidate-memory))))
3109 (def-gdb-memory-show-page gdb-memory-show-previous-page
3110 gdb-memory-prev-page)
3112 (def-gdb-memory-show-page gdb-memory-show-next-page
3113 gdb-memory-next-page)
3115 (defvar gdb-memory-unit-map
3116 (let ((map (make-sparse-keymap)))
3117 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3118 map)
3119 "Keymap to select units in the header line.")
3121 (defvar gdb-memory-unit-menu
3122 (let ((map (make-sparse-keymap "Unit")))
3123 (define-key map [giantwords]
3124 '(menu-item "Giant words" gdb-memory-unit-giant
3125 :button (:radio . (equal gdb-memory-unit 8))))
3126 (define-key map [words]
3127 '(menu-item "Words" gdb-memory-unit-word
3128 :button (:radio . (equal gdb-memory-unit 4))))
3129 (define-key map [halfwords]
3130 '(menu-item "Halfwords" gdb-memory-unit-halfword
3131 :button (:radio . (equal gdb-memory-unit 2))))
3132 (define-key map [bytes]
3133 '(menu-item "Bytes" gdb-memory-unit-byte
3134 :button (:radio . (equal gdb-memory-unit 1))))
3135 map)
3136 "Menu of units in the header line.")
3138 (defun gdb-memory-unit-menu (event)
3139 (interactive "@e")
3140 (x-popup-menu event gdb-memory-unit-menu))
3142 (defun gdb-memory-unit-menu-1 (event)
3143 (interactive "e")
3144 (save-selected-window
3145 (select-window (posn-window (event-start event)))
3146 (let* ((selection (gdb-memory-unit-menu event))
3147 (binding (and selection (lookup-key gdb-memory-unit-menu
3148 (vector (car selection))))))
3149 (if binding (call-interactively binding)))))
3151 (defvar gdb-memory-font-lock-keywords
3152 '(;; <__function.name+n>
3153 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3154 (1 font-lock-function-name-face)))
3155 "Font lock keywords used in `gdb-memory-mode'.")
3157 (defvar gdb-memory-header
3158 '(:eval
3159 (concat
3160 "Start address["
3161 (propertize "-"
3162 'face font-lock-warning-face
3163 'help-echo "mouse-1: decrement address"
3164 'mouse-face 'mode-line-highlight
3165 'local-map (gdb-make-header-line-mouse-map
3166 'mouse-1
3167 #'gdb-memory-show-previous-page))
3169 (propertize "+"
3170 'face font-lock-warning-face
3171 'help-echo "mouse-1: increment address"
3172 'mouse-face 'mode-line-highlight
3173 'local-map (gdb-make-header-line-mouse-map
3174 'mouse-1
3175 #'gdb-memory-show-next-page))
3176 "]: "
3177 (propertize gdb-memory-address
3178 'face font-lock-warning-face
3179 'help-echo "mouse-1: set start address"
3180 'mouse-face 'mode-line-highlight
3181 'local-map (gdb-make-header-line-mouse-map
3182 'mouse-1
3183 #'gdb-memory-set-address-event))
3184 " Rows: "
3185 (propertize (number-to-string gdb-memory-rows)
3186 'face font-lock-warning-face
3187 'help-echo "mouse-1: set number of columns"
3188 'mouse-face 'mode-line-highlight
3189 'local-map (gdb-make-header-line-mouse-map
3190 'mouse-1
3191 #'gdb-memory-set-rows))
3192 " Columns: "
3193 (propertize (number-to-string gdb-memory-columns)
3194 'face font-lock-warning-face
3195 'help-echo "mouse-1: set number of columns"
3196 'mouse-face 'mode-line-highlight
3197 'local-map (gdb-make-header-line-mouse-map
3198 'mouse-1
3199 #'gdb-memory-set-columns))
3200 " Display Format: "
3201 (propertize gdb-memory-format
3202 'face font-lock-warning-face
3203 'help-echo "mouse-3: select display format"
3204 'mouse-face 'mode-line-highlight
3205 'local-map gdb-memory-format-map)
3206 " Unit Size: "
3207 (propertize (number-to-string gdb-memory-unit)
3208 'face font-lock-warning-face
3209 'help-echo "mouse-3: select unit size"
3210 'mouse-face 'mode-line-highlight
3211 'local-map gdb-memory-unit-map)))
3212 "Header line used in `gdb-memory-mode'.")
3214 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3215 "Major mode for examining memory."
3216 (setq header-line-format gdb-memory-header)
3217 (set (make-local-variable 'font-lock-defaults)
3218 '(gdb-memory-font-lock-keywords))
3219 'gdb-invalidate-memory)
3221 (defun gdb-memory-buffer-name ()
3222 (concat "*memory of " (gdb-get-target-string) "*"))
3224 (def-gdb-display-buffer
3225 gdb-display-memory-buffer
3226 'gdb-memory-buffer
3227 "Display memory contents.")
3229 (defun gdb-frame-memory-buffer ()
3230 "Display memory contents in a new frame."
3231 (interactive)
3232 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3233 (special-display-frame-alist
3234 `((left-fringe . 0)
3235 (right-fringe . 0)
3236 (width . 83)
3237 ,@gdb-frame-parameters)))
3238 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3241 ;;; Disassembly view
3243 (defun gdb-disassembly-buffer-name ()
3244 (gdb-current-context-buffer-name
3245 (concat "disassembly of " (gdb-get-target-string))))
3247 (def-gdb-display-buffer
3248 gdb-display-disassembly-buffer
3249 'gdb-disassembly-buffer
3250 "Display disassembly for current stack frame.")
3252 (def-gdb-preempt-display-buffer
3253 gdb-preemptively-display-disassembly-buffer
3254 'gdb-disassembly-buffer)
3256 (def-gdb-frame-for-buffer
3257 gdb-frame-disassembly-buffer
3258 'gdb-disassembly-buffer
3259 "Display disassembly in a new frame.")
3261 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3262 (let* ((frame (gdb-current-buffer-frame))
3263 (file (bindat-get-field frame 'fullname))
3264 (line (bindat-get-field frame 'line)))
3265 (when file
3266 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3267 gdb-disassembly-handler
3268 ;; We update disassembly only after we have actual frame information
3269 ;; about all threads, so no there's `update' signal in this list
3270 '(start update-disassembly))
3272 (def-gdb-auto-update-handler
3273 gdb-disassembly-handler
3274 gdb-invalidate-disassembly
3275 gdb-disassembly-handler-custom
3278 (gdb-set-buffer-rules
3279 'gdb-disassembly-buffer
3280 'gdb-disassembly-buffer-name
3281 'gdb-disassembly-mode
3282 'gdb-invalidate-disassembly)
3284 (defvar gdb-disassembly-font-lock-keywords
3285 '(;; <__function.name+n>
3286 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3287 (1 font-lock-function-name-face))
3288 ;; 0xNNNNNNNN <__function.name+n>: opcode
3289 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3290 (4 font-lock-keyword-face))
3291 ;; %register(at least i386)
3292 ("%\\sw+" . font-lock-variable-name-face)
3293 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3294 (1 font-lock-comment-face)
3295 (2 font-lock-function-name-face))
3296 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3297 "Font lock keywords used in `gdb-disassembly-mode'.")
3299 (defvar gdb-disassembly-mode-map
3300 ;; TODO
3301 (let ((map (make-sparse-keymap)))
3302 (suppress-keymap map)
3303 (define-key map "q" 'kill-this-buffer)
3304 map))
3306 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3307 "Major mode for GDB disassembly information."
3308 ;; TODO Rename overlay variable for disassembly mode
3309 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3310 (setq fringes-outside-margins t)
3311 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3312 (set (make-local-variable 'font-lock-defaults)
3313 '(gdb-disassembly-font-lock-keywords))
3314 'gdb-invalidate-disassembly)
3316 (defun gdb-disassembly-handler-custom ()
3317 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3318 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3319 (table (make-gdb-table))
3320 (marked-line nil))
3321 (dolist (instr instructions)
3322 (gdb-table-add-row table
3323 (list
3324 (bindat-get-field instr 'address)
3325 (apply #'format "<%s+%s>:"
3326 (gdb-get-many-fields instr 'func-name 'offset))
3327 (bindat-get-field instr 'inst)))
3328 (when (string-equal (bindat-get-field instr 'address)
3329 address)
3330 (progn
3331 (setq marked-line (length (gdb-table-rows table)))
3332 (setq fringe-indicator-alist
3333 (if (string-equal gdb-frame-number "0")
3335 '((overlay-arrow . hollow-right-triangle)))))))
3336 (insert (gdb-table-string table " "))
3337 (gdb-disassembly-place-breakpoints)
3338 ;; Mark current position with overlay arrow and scroll window to
3339 ;; that point
3340 (when marked-line
3341 (let ((window (get-buffer-window (current-buffer) 0)))
3342 (set-window-point window (gdb-mark-line marked-line
3343 gdb-disassembly-position))))
3344 (setq mode-name
3345 (gdb-current-context-mode-name
3346 (concat "Disassembly: "
3347 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3349 (defun gdb-disassembly-place-breakpoints ()
3350 (gdb-remove-breakpoint-icons (point-min) (point-max))
3351 (dolist (breakpoint gdb-breakpoints-list)
3352 (let* ((breakpoint (cdr breakpoint))
3353 (bptno (bindat-get-field breakpoint 'number))
3354 (flag (bindat-get-field breakpoint 'enabled))
3355 (address (bindat-get-field breakpoint 'addr)))
3356 (save-excursion
3357 (goto-char (point-min))
3358 (if (re-search-forward (concat "^" address) nil t)
3359 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3362 (defvar gdb-breakpoints-header
3363 (list
3364 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3365 nil nil mode-line)
3367 (gdb-propertize-header "Threads" gdb-threads-buffer
3368 "mouse-1: select" mode-line-highlight
3369 mode-line-inactive)))
3371 ;;; Breakpoints view
3372 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3373 "Major mode for gdb breakpoints."
3374 (setq header-line-format gdb-breakpoints-header)
3375 'gdb-invalidate-breakpoints)
3377 (defun gdb-toggle-breakpoint ()
3378 "Enable/disable breakpoint at current line of breakpoints buffer."
3379 (interactive)
3380 (save-excursion
3381 (beginning-of-line)
3382 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3383 (if breakpoint
3384 (gud-basic-call
3385 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3386 "-break-disable "
3387 "-break-enable ")
3388 (bindat-get-field breakpoint 'number)))
3389 (error "Not recognized as break/watchpoint line")))))
3391 (defun gdb-delete-breakpoint ()
3392 "Delete the breakpoint at current line of breakpoints buffer."
3393 (interactive)
3394 (save-excursion
3395 (beginning-of-line)
3396 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3397 (if breakpoint
3398 (gud-basic-call (concat "-break-delete "
3399 (bindat-get-field breakpoint 'number)))
3400 (error "Not recognized as break/watchpoint line")))))
3402 (defun gdb-goto-breakpoint (&optional event)
3403 "Go to the location of breakpoint at current line of
3404 breakpoints buffer."
3405 (interactive (list last-input-event))
3406 (if event (posn-set-point (event-end event)))
3407 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3408 (let ((window (get-buffer-window gud-comint-buffer)))
3409 (if window (save-selected-window (select-window window))))
3410 (save-excursion
3411 (beginning-of-line)
3412 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3413 (if breakpoint
3414 (let ((bptno (bindat-get-field breakpoint 'number))
3415 (file (bindat-get-field breakpoint 'fullname))
3416 (line (bindat-get-field breakpoint 'line)))
3417 (save-selected-window
3418 (let* ((buffer (find-file-noselect
3419 (if (file-exists-p file) file
3420 (cdr (assoc bptno gdb-location-alist)))))
3421 (window (or (gdb-display-source-buffer buffer)
3422 (display-buffer buffer))))
3423 (setq gdb-source-window window)
3424 (with-current-buffer buffer
3425 (goto-char (point-min))
3426 (forward-line (1- (string-to-number line)))
3427 (set-window-point window (point))))))
3428 (error "Not recognized as break/watchpoint line")))))
3431 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3433 (def-gdb-trigger-and-handler
3434 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3435 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3436 '(start update))
3438 (gdb-set-buffer-rules
3439 'gdb-stack-buffer
3440 'gdb-stack-buffer-name
3441 'gdb-frames-mode
3442 'gdb-invalidate-frames)
3444 (defun gdb-frame-location (frame)
3445 "Return \" of file:line\" or \" of library\" for structure FRAME.
3447 FRAME must have either \"file\" and \"line\" members or \"from\"
3448 member."
3449 (let ((file (bindat-get-field frame 'file))
3450 (line (bindat-get-field frame 'line))
3451 (from (bindat-get-field frame 'from)))
3452 (let ((res (or (and file line (concat file ":" line))
3453 from)))
3454 (if res (concat " of " res) ""))))
3456 (defun gdb-stack-list-frames-custom ()
3457 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3458 (table (make-gdb-table)))
3459 (set-marker gdb-stack-position nil)
3460 (dolist (frame stack)
3461 (gdb-table-add-row table
3462 (list
3463 (bindat-get-field frame 'level)
3464 "in"
3465 (concat
3466 (bindat-get-field frame 'func)
3467 (if gdb-stack-buffer-locations
3468 (gdb-frame-location frame) "")
3469 (if gdb-stack-buffer-addresses
3470 (concat " at " (bindat-get-field frame 'addr)) "")))
3471 `(mouse-face highlight
3472 help-echo "mouse-2, RET: Select frame"
3473 gdb-frame ,frame)))
3474 (insert (gdb-table-string table " ")))
3475 (when (and gdb-frame-number
3476 (gdb-buffer-shows-main-thread-p))
3477 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3478 gdb-stack-position))
3479 (setq mode-name
3480 (gdb-current-context-mode-name "Frames")))
3482 (defun gdb-stack-buffer-name ()
3483 (gdb-current-context-buffer-name
3484 (concat "stack frames of " (gdb-get-target-string))))
3486 (def-gdb-display-buffer
3487 gdb-display-stack-buffer
3488 'gdb-stack-buffer
3489 "Display backtrace of current stack.")
3491 (def-gdb-preempt-display-buffer
3492 gdb-preemptively-display-stack-buffer
3493 'gdb-stack-buffer nil t)
3495 (def-gdb-frame-for-buffer
3496 gdb-frame-stack-buffer
3497 'gdb-stack-buffer
3498 "Display backtrace of current stack in a new frame.")
3500 (defvar gdb-frames-mode-map
3501 (let ((map (make-sparse-keymap)))
3502 (suppress-keymap map)
3503 (define-key map "q" 'kill-this-buffer)
3504 (define-key map "\r" 'gdb-select-frame)
3505 (define-key map [mouse-2] 'gdb-select-frame)
3506 (define-key map [follow-link] 'mouse-face)
3507 map))
3509 (defvar gdb-frames-font-lock-keywords
3510 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3511 "Font lock keywords used in `gdb-frames-mode'.")
3513 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3514 "Major mode for gdb call stack."
3515 (setq gdb-stack-position (make-marker))
3516 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3517 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3518 (set (make-local-variable 'font-lock-defaults)
3519 '(gdb-frames-font-lock-keywords))
3520 'gdb-invalidate-frames)
3522 (defun gdb-select-frame (&optional event)
3523 "Select the frame and display the relevant source."
3524 (interactive (list last-input-event))
3525 (if event (posn-set-point (event-end event)))
3526 (let ((frame (get-text-property (point) 'gdb-frame)))
3527 (if frame
3528 (if (gdb-buffer-shows-main-thread-p)
3529 (let ((new-level (bindat-get-field frame 'level)))
3530 (setq gdb-frame-number new-level)
3531 (gdb-input (list (concat "-stack-select-frame " new-level)
3532 'ignore))
3533 (gdb-update))
3534 (error "Could not select frame for non-current thread"))
3535 (error "Not recognized as frame line"))))
3538 ;; Locals buffer.
3539 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3540 (def-gdb-trigger-and-handler
3541 gdb-invalidate-locals
3542 (concat (gdb-current-context-command "-stack-list-locals")
3543 " --simple-values")
3544 gdb-locals-handler gdb-locals-handler-custom
3545 '(start update))
3547 (gdb-set-buffer-rules
3548 'gdb-locals-buffer
3549 'gdb-locals-buffer-name
3550 'gdb-locals-mode
3551 'gdb-invalidate-locals)
3553 (defvar gdb-locals-watch-map
3554 (let ((map (make-sparse-keymap)))
3555 (suppress-keymap map)
3556 (define-key map "\r" 'gud-watch)
3557 (define-key map [mouse-2] 'gud-watch)
3558 map)
3559 "Keymap to create watch expression of a complex data type local variable.")
3561 (defvar gdb-edit-locals-map-1
3562 (let ((map (make-sparse-keymap)))
3563 (suppress-keymap map)
3564 (define-key map "\r" 'gdb-edit-locals-value)
3565 (define-key map [mouse-2] 'gdb-edit-locals-value)
3566 map)
3567 "Keymap to edit value of a simple data type local variable.")
3569 (defun gdb-edit-locals-value (&optional event)
3570 "Assign a value to a variable displayed in the locals buffer."
3571 (interactive (list last-input-event))
3572 (save-excursion
3573 (if event (posn-set-point (event-end event)))
3574 (beginning-of-line)
3575 (let* ((var (bindat-get-field
3576 (get-text-property (point) 'gdb-local-variable) 'name))
3577 (value (read-string (format "New value (%s): " var))))
3578 (gud-basic-call
3579 (concat "-gdb-set variable " var " = " value)))))
3581 ;; Dont display values of arrays or structures.
3582 ;; These can be expanded using gud-watch.
3583 (defun gdb-locals-handler-custom ()
3584 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3585 (table (make-gdb-table)))
3586 (dolist (local locals-list)
3587 (let ((name (bindat-get-field local 'name))
3588 (value (bindat-get-field local 'value))
3589 (type (bindat-get-field local 'type)))
3590 (if (or (not value)
3591 (string-match "\\0x" value))
3592 (add-text-properties 0 (length name)
3593 `(mouse-face highlight
3594 help-echo "mouse-2: create watch expression"
3595 local-map ,gdb-locals-watch-map)
3596 name)
3597 (add-text-properties 0 (length value)
3598 `(mouse-face highlight
3599 help-echo "mouse-2: edit value"
3600 local-map ,gdb-edit-locals-map-1)
3601 value))
3602 (gdb-table-add-row
3603 table
3604 (list
3605 (propertize type 'font-lock-face font-lock-type-face)
3606 (propertize name 'font-lock-face font-lock-variable-name-face)
3607 value)
3608 `(gdb-local-variable ,local))))
3609 (insert (gdb-table-string table " "))
3610 (setq mode-name
3611 (gdb-current-context-mode-name
3612 (concat "Locals: "
3613 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3615 (defvar gdb-locals-header
3616 (list
3617 (gdb-propertize-header "Locals" gdb-locals-buffer
3618 nil nil mode-line)
3620 (gdb-propertize-header "Registers" gdb-registers-buffer
3621 "mouse-1: select" mode-line-highlight
3622 mode-line-inactive)))
3624 (defvar gdb-locals-mode-map
3625 (let ((map (make-sparse-keymap)))
3626 (suppress-keymap map)
3627 (define-key map "q" 'kill-this-buffer)
3628 (define-key map "\t" (lambda ()
3629 (interactive)
3630 (gdb-set-window-buffer
3631 (gdb-get-buffer-create
3632 'gdb-registers-buffer
3633 gdb-thread-number) t)))
3634 map))
3636 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3637 "Major mode for gdb locals."
3638 (setq header-line-format gdb-locals-header)
3639 'gdb-invalidate-locals)
3641 (defun gdb-locals-buffer-name ()
3642 (gdb-current-context-buffer-name
3643 (concat "locals of " (gdb-get-target-string))))
3645 (def-gdb-display-buffer
3646 gdb-display-locals-buffer
3647 'gdb-locals-buffer
3648 "Display local variables of current stack and their values.")
3650 (def-gdb-preempt-display-buffer
3651 gdb-preemptively-display-locals-buffer
3652 'gdb-locals-buffer nil t)
3654 (def-gdb-frame-for-buffer
3655 gdb-frame-locals-buffer
3656 'gdb-locals-buffer
3657 "Display local variables of current stack and their values in a new frame.")
3660 ;; Registers buffer.
3662 (def-gdb-trigger-and-handler
3663 gdb-invalidate-registers
3664 (concat (gdb-current-context-command "-data-list-register-values") " x")
3665 gdb-registers-handler
3666 gdb-registers-handler-custom
3667 '(start update))
3669 (gdb-set-buffer-rules
3670 'gdb-registers-buffer
3671 'gdb-registers-buffer-name
3672 'gdb-registers-mode
3673 'gdb-invalidate-registers)
3675 (defun gdb-registers-handler-custom ()
3676 (when gdb-register-names
3677 (let ((register-values
3678 (bindat-get-field (gdb-json-partial-output) 'register-values))
3679 (table (make-gdb-table)))
3680 (dolist (register register-values)
3681 (let* ((register-number (bindat-get-field register 'number))
3682 (value (bindat-get-field register 'value))
3683 (register-name (nth (string-to-number register-number)
3684 gdb-register-names)))
3685 (gdb-table-add-row
3686 table
3687 (list
3688 (propertize register-name
3689 'font-lock-face font-lock-variable-name-face)
3690 (if (member register-number gdb-changed-registers)
3691 (propertize value 'font-lock-face font-lock-warning-face)
3692 value))
3693 `(mouse-face highlight
3694 help-echo "mouse-2: edit value"
3695 gdb-register-name ,register-name))))
3696 (insert (gdb-table-string table " ")))
3697 (setq mode-name
3698 (gdb-current-context-mode-name "Registers"))))
3700 (defun gdb-edit-register-value (&optional event)
3701 "Assign a value to a register displayed in the registers buffer."
3702 (interactive (list last-input-event))
3703 (save-excursion
3704 (if event (posn-set-point (event-end event)))
3705 (beginning-of-line)
3706 (let* ((var (bindat-get-field
3707 (get-text-property (point) 'gdb-register-name)))
3708 (value (read-string (format "New value (%s): " var))))
3709 (gud-basic-call
3710 (concat "-gdb-set variable $" var " = " value)))))
3712 (defvar gdb-registers-mode-map
3713 (let ((map (make-sparse-keymap)))
3714 (suppress-keymap map)
3715 (define-key map "\r" 'gdb-edit-register-value)
3716 (define-key map [mouse-2] 'gdb-edit-register-value)
3717 (define-key map "q" 'kill-this-buffer)
3718 (define-key map "\t" (lambda ()
3719 (interactive)
3720 (gdb-set-window-buffer
3721 (gdb-get-buffer-create
3722 'gdb-locals-buffer
3723 gdb-thread-number) t)))
3724 map))
3726 (defvar gdb-registers-header
3727 (list
3728 (gdb-propertize-header "Locals" gdb-locals-buffer
3729 "mouse-1: select" mode-line-highlight
3730 mode-line-inactive)
3732 (gdb-propertize-header "Registers" gdb-registers-buffer
3733 nil nil mode-line)))
3735 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3736 "Major mode for gdb registers."
3737 (setq header-line-format gdb-registers-header)
3738 'gdb-invalidate-registers)
3740 (defun gdb-registers-buffer-name ()
3741 (gdb-current-context-buffer-name
3742 (concat "registers of " (gdb-get-target-string))))
3744 (def-gdb-display-buffer
3745 gdb-display-registers-buffer
3746 'gdb-registers-buffer
3747 "Display integer register contents.")
3749 (def-gdb-preempt-display-buffer
3750 gdb-preemptively-display-registers-buffer
3751 'gdb-registers-buffer nil t)
3753 (def-gdb-frame-for-buffer
3754 gdb-frame-registers-buffer
3755 'gdb-registers-buffer
3756 "Display integer register contents in a new frame.")
3758 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3759 (defun gdb-get-changed-registers ()
3760 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3761 (not (gdb-pending-p 'gdb-get-changed-registers)))
3762 (progn
3763 (gdb-input
3764 (list
3765 "-data-list-changed-registers"
3766 'gdb-changed-registers-handler))
3767 (gdb-add-pending 'gdb-get-changed-registers))))
3769 (defun gdb-changed-registers-handler ()
3770 (gdb-delete-pending 'gdb-get-changed-registers)
3771 (setq gdb-changed-registers nil)
3772 (dolist (register-number
3773 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3774 (push register-number gdb-changed-registers)))
3776 (defun gdb-register-names-handler ()
3777 ;; Don't use gdb-pending-triggers because this handler is called
3778 ;; only once (in gdb-init-1)
3779 (setq gdb-register-names nil)
3780 (dolist (register-name
3781 (bindat-get-field (gdb-json-partial-output) 'register-names))
3782 (push register-name gdb-register-names))
3783 (setq gdb-register-names (reverse gdb-register-names)))
3786 (defun gdb-get-source-file-list ()
3787 "Create list of source files for current GDB session.
3788 If buffers already exist for any of these files, gud-minor-mode
3789 is set in them."
3790 (goto-char (point-min))
3791 (while (re-search-forward gdb-source-file-regexp nil t)
3792 (push (match-string 1) gdb-source-file-list))
3793 (dolist (buffer (buffer-list))
3794 (with-current-buffer buffer
3795 (when (member buffer-file-name gdb-source-file-list)
3796 (gdb-init-buffer))))
3797 (gdb-force-mode-line-update
3798 (propertize "ready" 'face font-lock-variable-name-face)))
3800 (defun gdb-get-main-selected-frame ()
3801 "Trigger for `gdb-frame-handler' which uses main current
3802 thread. Called from `gdb-update'."
3803 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3804 (progn
3805 (gdb-input
3806 (list (gdb-current-context-command "-stack-info-frame")
3807 'gdb-frame-handler))
3808 (gdb-add-pending 'gdb-get-main-selected-frame))))
3810 (defun gdb-frame-handler ()
3811 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3812 overlay arrow in source buffer."
3813 (gdb-delete-pending 'gdb-get-main-selected-frame)
3814 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3815 (when frame
3816 (setq gdb-selected-frame (bindat-get-field frame 'func))
3817 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3818 (setq gdb-frame-number (bindat-get-field frame 'level))
3819 (setq gdb-frame-address (bindat-get-field frame 'addr))
3820 (let ((line (bindat-get-field frame 'line)))
3821 (setq gdb-selected-line (and line (string-to-number line)))
3822 (when (and gdb-selected-file gdb-selected-line)
3823 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3824 (gud-display-frame)))
3825 (if gud-overlay-arrow-position
3826 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3827 (position (marker-position gud-overlay-arrow-position)))
3828 (when buffer
3829 (with-current-buffer buffer
3830 (setq fringe-indicator-alist
3831 (if (string-equal gdb-frame-number "0")
3833 '((overlay-arrow . hollow-right-triangle))))
3834 (setq gud-overlay-arrow-position (make-marker))
3835 (set-marker gud-overlay-arrow-position position))))))))
3837 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3839 (defun gdb-get-prompt ()
3840 "Find prompt for GDB session."
3841 (goto-char (point-min))
3842 (setq gdb-prompt-name nil)
3843 (re-search-forward gdb-prompt-name-regexp nil t)
3844 (setq gdb-prompt-name (match-string 1))
3845 ;; Insert first prompt.
3846 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3848 ;;;; Window management
3849 (defun gdb-display-buffer (buf dedicated &optional frame)
3850 "Show buffer BUF.
3852 If BUF is already displayed in some window, show it, deiconifying
3853 the frame if necessary. Otherwise, find least recently used
3854 window and show BUF there, if the window is not used for GDB
3855 already, in which case that window is splitted first."
3856 (let ((answer (get-buffer-window buf (or frame 0))))
3857 (if answer
3858 (display-buffer buf nil (or frame 0)) ;Deiconify frame if necessary.
3859 (let ((window (get-lru-window)))
3860 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3861 'gdbmi)
3862 (let ((largest (get-largest-window)))
3863 (setq answer (split-window largest))
3864 (set-window-buffer answer buf)
3865 (set-window-dedicated-p answer dedicated)
3866 answer)
3867 (set-window-buffer window buf)
3868 window)))))
3870 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3871 "Find window displaying a buffer with the same
3872 `gdb-buffer-type' as BUF and show BUF there. If no such window
3873 exists, just call `gdb-display-buffer' for BUF. If the window
3874 found is already dedicated, split window according to
3875 SPLIT-HORIZONTAL and show BUF in the new window."
3876 (if buf
3877 (when (not (get-buffer-window buf))
3878 (let* ((buf-type (gdb-buffer-type buf))
3879 (existing-window
3880 (get-window-with-predicate
3881 #'(lambda (w)
3882 (and (eq buf-type
3883 (gdb-buffer-type (window-buffer w)))
3884 (not (window-dedicated-p w)))))))
3885 (if existing-window
3886 (set-window-buffer existing-window buf)
3887 (let ((dedicated-window
3888 (get-window-with-predicate
3889 #'(lambda (w)
3890 (eq buf-type
3891 (gdb-buffer-type (window-buffer w)))))))
3892 (if dedicated-window
3893 (set-window-buffer
3894 (split-window dedicated-window nil split-horizontal) buf)
3895 (gdb-display-buffer buf t))))))
3896 (error "Null buffer")))
3898 ;;; Shared keymap initialization:
3900 (let ((menu (make-sparse-keymap "GDB-Windows")))
3901 (define-key gud-menu-map [displays]
3902 `(menu-item "GDB-Windows" ,menu
3903 :visible (eq gud-minor-mode 'gdbmi)))
3904 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3905 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3906 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3907 (define-key menu [disassembly]
3908 '("Disassembly" . gdb-display-disassembly-buffer))
3909 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3910 (define-key menu [inferior]
3911 '("IO" . gdb-display-io-buffer))
3912 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3913 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3914 (define-key menu [breakpoints]
3915 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3917 (let ((menu (make-sparse-keymap "GDB-Frames")))
3918 (define-key gud-menu-map [frames]
3919 `(menu-item "GDB-Frames" ,menu
3920 :visible (eq gud-minor-mode 'gdbmi)))
3921 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3922 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3923 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3924 (define-key menu [disassembly]
3925 '("Disassembly" . gdb-frame-disassembly-buffer))
3926 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3927 (define-key menu [inferior]
3928 '("IO" . gdb-frame-io-buffer))
3929 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3930 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3931 (define-key menu [breakpoints]
3932 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3934 (let ((menu (make-sparse-keymap "GDB-MI")))
3935 (define-key menu [gdb-customize]
3936 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3937 :help "Customize Gdb Graphical Mode options."))
3938 (define-key menu [gdb-many-windows]
3939 '(menu-item "Display Other Windows" gdb-many-windows
3940 :help "Toggle display of locals, stack and breakpoint information"
3941 :button (:toggle . gdb-many-windows)))
3942 (define-key menu [gdb-restore-windows]
3943 '(menu-item "Restore Window Layout" gdb-restore-windows
3944 :help "Restore standard layout for debug session."))
3945 (define-key menu [sep1]
3946 '(menu-item "--"))
3947 (define-key menu [all-threads]
3948 '(menu-item "GUD controls all threads"
3949 (lambda ()
3950 (interactive)
3951 (setq gdb-gud-control-all-threads t))
3952 :help "GUD start/stop commands apply to all threads"
3953 :button (:radio . gdb-gud-control-all-threads)))
3954 (define-key menu [current-thread]
3955 '(menu-item "GUD controls current thread"
3956 (lambda ()
3957 (interactive)
3958 (setq gdb-gud-control-all-threads nil))
3959 :help "GUD start/stop commands apply to current thread only"
3960 :button (:radio . (not gdb-gud-control-all-threads))))
3961 (define-key menu [sep2]
3962 '(menu-item "--"))
3963 (define-key menu [gdb-customize-reasons]
3964 '(menu-item "Customize switching..."
3965 (lambda ()
3966 (interactive)
3967 (customize-option 'gdb-switch-reasons))))
3968 (define-key menu [gdb-switch-when-another-stopped]
3969 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
3970 gdb-switch-when-another-stopped
3971 "Automatically switch to stopped thread"
3972 "GDB thread switching %s"
3973 "Switch to stopped thread"))
3974 (define-key gud-menu-map [mi]
3975 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3977 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3978 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3979 ;; tool-bar-local-item-from-menu here because it appends new buttons
3980 ;; to toolbar from right to left while we want our A/T throttle to
3981 ;; show up right before Run button.
3982 (define-key-after gud-tool-bar-map [all-threads]
3983 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3984 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3985 :visible (and (eq gud-minor-mode 'gdbmi)
3986 gdb-non-stop
3987 (not gdb-gud-control-all-threads)))
3988 'run)
3990 (define-key-after gud-tool-bar-map [current-thread]
3991 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3992 :image (find-image '((:type xpm :file "gud/all.xpm")))
3993 :visible (and (eq gud-minor-mode 'gdbmi)
3994 gdb-non-stop
3995 gdb-gud-control-all-threads))
3996 'all-threads)
3998 (defun gdb-frame-gdb-buffer ()
3999 "Display GUD buffer in a new frame."
4000 (interactive)
4001 (display-buffer-other-frame gud-comint-buffer))
4003 (defun gdb-display-gdb-buffer ()
4004 "Display GUD buffer."
4005 (interactive)
4006 (pop-to-buffer gud-comint-buffer nil 0))
4008 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4009 "Set buffer of selected window to NAME and dedicate window.
4011 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4012 window is dedicated."
4013 (unless window (setq window (selected-window)))
4014 (when ignore-dedicated
4015 (set-window-dedicated-p window nil))
4016 (set-window-buffer window (get-buffer name))
4017 (set-window-dedicated-p window t))
4019 (defun gdb-setup-windows ()
4020 "Layout the window pattern for `gdb-many-windows'."
4021 (gdb-display-locals-buffer)
4022 (gdb-display-stack-buffer)
4023 (delete-other-windows)
4024 (gdb-display-breakpoints-buffer)
4025 (delete-other-windows)
4026 ;; Don't dedicate.
4027 (switch-to-buffer gud-comint-buffer)
4028 (let ((win0 (selected-window))
4029 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4030 (win2 (split-window nil ( / (window-height) 3)))
4031 (win3 (split-window-right)))
4032 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4033 (select-window win2)
4034 (set-window-buffer
4035 win2
4036 (if gud-last-last-frame
4037 (gud-find-file (car gud-last-last-frame))
4038 (if gdb-main-file
4039 (gud-find-file gdb-main-file)
4040 ;; Put buffer list in window if we
4041 ;; can't find a source file.
4042 (list-buffers-noselect))))
4043 (setq gdb-source-window (selected-window))
4044 (let ((win4 (split-window-right)))
4045 (gdb-set-window-buffer
4046 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4047 (select-window win1)
4048 (gdb-set-window-buffer (gdb-stack-buffer-name))
4049 (let ((win5 (split-window-right)))
4050 (gdb-set-window-buffer (if gdb-show-threads-by-default
4051 (gdb-threads-buffer-name)
4052 (gdb-breakpoints-buffer-name))
4053 nil win5))
4054 (select-window win0)))
4056 (defcustom gdb-many-windows nil
4057 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4058 In this case it starts with two windows: one displaying the GUD
4059 buffer and the other with the source file with the main routine
4060 of the debugged program. Non-nil means display the layout shown for
4061 `gdb'."
4062 :type 'boolean
4063 :group 'gdb
4064 :version "22.1")
4066 (defun gdb-many-windows (arg)
4067 "Toggle the number of windows in the basic arrangement.
4068 With arg, display additional buffers iff arg is positive."
4069 (interactive "P")
4070 (setq gdb-many-windows
4071 (if (null arg)
4072 (not gdb-many-windows)
4073 (> (prefix-numeric-value arg) 0)))
4074 (message (format "Display of other windows %sabled"
4075 (if gdb-many-windows "en" "dis")))
4076 (if (and gud-comint-buffer
4077 (buffer-name gud-comint-buffer))
4078 (condition-case nil
4079 (gdb-restore-windows)
4080 (error nil))))
4082 (defun gdb-restore-windows ()
4083 "Restore the basic arrangement of windows used by gdb.
4084 This arrangement depends on the value of `gdb-many-windows'."
4085 (interactive)
4086 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4087 (delete-other-windows)
4088 (if gdb-many-windows
4089 (gdb-setup-windows)
4090 (when (or gud-last-last-frame gdb-show-main)
4091 (let ((win (split-window)))
4092 (set-window-buffer
4094 (if gud-last-last-frame
4095 (gud-find-file (car gud-last-last-frame))
4096 (gud-find-file gdb-main-file)))
4097 (setq gdb-source-window win)))))
4099 (defun gdb-reset ()
4100 "Exit a debugging session cleanly.
4101 Kills the gdb buffers, and resets variables and the source buffers."
4102 (dolist (buffer (buffer-list))
4103 (unless (eq buffer gud-comint-buffer)
4104 (with-current-buffer buffer
4105 (if (eq gud-minor-mode 'gdbmi)
4106 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4107 (kill-buffer nil)
4108 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4109 (setq gud-minor-mode nil)
4110 (kill-local-variable 'tool-bar-map)
4111 (kill-local-variable 'gdb-define-alist))))))
4112 (setq gdb-disassembly-position nil)
4113 (setq overlay-arrow-variable-list
4114 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4115 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4116 (setq gdb-stack-position nil)
4117 (setq overlay-arrow-variable-list
4118 (delq 'gdb-stack-position overlay-arrow-variable-list))
4119 (setq gdb-thread-position nil)
4120 (setq overlay-arrow-variable-list
4121 (delq 'gdb-thread-position overlay-arrow-variable-list))
4122 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4123 (setq gud-running nil)
4124 (setq gdb-active-process nil)
4125 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4127 (defun gdb-get-source-file ()
4128 "Find the source file where the program starts and display it with related
4129 buffers, if required."
4130 (goto-char (point-min))
4131 (if (re-search-forward gdb-source-file-regexp nil t)
4132 (setq gdb-main-file (match-string 1)))
4133 (if gdb-many-windows
4134 (gdb-setup-windows)
4135 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4136 (if gdb-show-main
4137 (let ((pop-up-windows t))
4138 (display-buffer (gud-find-file gdb-main-file))))))
4140 ;;from put-image
4141 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4142 "Put string PUTSTRING in front of POS in the current buffer.
4143 PUTSTRING is displayed by putting an overlay into the current buffer with a
4144 `before-string' string that has a `display' property whose value is
4145 PUTSTRING."
4146 (let ((string (make-string 1 ?x))
4147 (buffer (current-buffer)))
4148 (setq putstring (copy-sequence putstring))
4149 (let ((overlay (make-overlay pos pos buffer))
4150 (prop (or dprop
4151 (list (list 'margin 'left-margin) putstring))))
4152 (put-text-property 0 1 'display prop string)
4153 (if sprops
4154 (add-text-properties 0 1 sprops string))
4155 (overlay-put overlay 'put-break t)
4156 (overlay-put overlay 'before-string string))))
4158 ;;from remove-images
4159 (defun gdb-remove-strings (start end &optional buffer)
4160 "Remove strings between START and END in BUFFER.
4161 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4162 BUFFER nil or omitted means use the current buffer."
4163 (unless buffer
4164 (setq buffer (current-buffer)))
4165 (dolist (overlay (overlays-in start end))
4166 (when (overlay-get overlay 'put-break)
4167 (delete-overlay overlay))))
4169 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4170 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4171 (start (- (car posns) 1))
4172 (end (+ (cdr posns) 1))
4173 (putstring (if enabled "B" "b"))
4174 (source-window (get-buffer-window (current-buffer) 0)))
4175 (add-text-properties
4176 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4177 putstring)
4178 (if enabled
4179 (add-text-properties
4180 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4181 (add-text-properties
4182 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4183 (gdb-remove-breakpoint-icons start end)
4184 (if (display-images-p)
4185 (if (>= (or left-fringe-width
4186 (if source-window (car (window-fringes source-window)))
4187 gdb-buffer-fringe-width) 8)
4188 (gdb-put-string
4189 nil (1+ start)
4190 `(left-fringe breakpoint
4191 ,(if enabled
4192 'breakpoint-enabled
4193 'breakpoint-disabled))
4194 'gdb-bptno bptno
4195 'gdb-enabled enabled)
4196 (when (< left-margin-width 2)
4197 (save-current-buffer
4198 (setq left-margin-width 2)
4199 (if source-window
4200 (set-window-margins
4201 source-window
4202 left-margin-width right-margin-width))))
4203 (put-image
4204 (if enabled
4205 (or breakpoint-enabled-icon
4206 (setq breakpoint-enabled-icon
4207 (find-image `((:type xpm :data
4208 ,breakpoint-xpm-data
4209 :ascent 100 :pointer hand)
4210 (:type pbm :data
4211 ,breakpoint-enabled-pbm-data
4212 :ascent 100 :pointer hand)))))
4213 (or breakpoint-disabled-icon
4214 (setq breakpoint-disabled-icon
4215 (find-image `((:type xpm :data
4216 ,breakpoint-xpm-data
4217 :conversion disabled
4218 :ascent 100 :pointer hand)
4219 (:type pbm :data
4220 ,breakpoint-disabled-pbm-data
4221 :ascent 100 :pointer hand))))))
4222 (+ start 1)
4223 putstring
4224 'left-margin))
4225 (when (< left-margin-width 2)
4226 (save-current-buffer
4227 (setq left-margin-width 2)
4228 (let ((window (get-buffer-window (current-buffer) 0)))
4229 (if window
4230 (set-window-margins
4231 window left-margin-width right-margin-width)))))
4232 (gdb-put-string
4233 (propertize putstring
4234 'face (if enabled
4235 'breakpoint-enabled 'breakpoint-disabled))
4236 (1+ start)))))
4238 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4239 (gdb-remove-strings start end)
4240 (if (display-images-p)
4241 (remove-images start end))
4242 (when remove-margin
4243 (setq left-margin-width 0)
4244 (let ((window (get-buffer-window (current-buffer) 0)))
4245 (if window
4246 (set-window-margins
4247 window left-margin-width right-margin-width)))))
4249 (provide 'gdb-mi)
4251 ;;; gdb-mi.el ends here