1 ;;; gdb-mi.el --- User Interface for running GDB
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Nick Roberts <nickrob@gnu.org>
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/>.
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".
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
44 ;; M-x gdb will start the debugger.
46 ;; This file uses GDB/MI as the primary interface to GDB. It is still under
47 ;; development and is part of a process to migrate Emacs from annotations (as
48 ;; used in gdb-ui.el) to GDB/MI. It runs gdb with GDB/MI (-interp=mi) and
49 ;; access CLI using "-interpreter-exec console cli-command". This code works
50 ;; without gdb-ui.el and uses MI tokens instead of queues. Eventually MI
51 ;; should be asynchronous.
53 ;; This mode will PARTLY WORK WITH RECENT GDB RELEASES (status in modeline
54 ;; doesn't update properly when execution commands are issued from GUD buffer)
55 ;; and WORKS BEST when GDB runs asynchronously: maint set linux-async on.
57 ;; You need development version of GDB 7.0 for the thread buffer to work.
59 ;; This file replaces gdb-ui.el and is for development with GDB. Use the
60 ;; release branch of Emacs 22 for the latest version of gdb-ui.el.
64 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
65 ;; explicitly in your program if you want timely display of I/O in Emacs.
66 ;; Alternatively you can make the output stream unbuffered, for example, by
70 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
73 ;; and compiling with -DUNBUFFERED while debugging.
75 ;; If you are using Cygwin GDB and find that the source is not being displayed
76 ;; in Emacs when you step through it, possible solutions are to:
78 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
79 ;; (Since 22.1 Emacs builds under Cygwin.)
80 ;; 2) Use MinGW GDB instead.
81 ;; 3) Use cygwin-mount.el
85 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
86 ;; some changes to the version that they include as part of Mac OSX.
87 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
88 ;; as earlier versions don not compile on Mac OSX.
92 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
93 ;; line information, e.g., a routine in libc (just a TODO item).
96 ;; 2) Watch windows to work with threads.
97 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
98 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
106 (defvar tool-bar-map)
107 (defvar speedbar-initial-expansion-list-name)
108 (defvar speedbar-frame)
110 (defvar gdb-pc-address nil "Initialization for Assembler buffer.
111 Set to \"main\" at start if `gdb-show-main' is t.")
112 (defvar gdb-memory-address "main")
113 (defvar gdb-memory-last-address nil
114 "Last successfully accessed memory address.")
115 (defvar gdb-memory-next-page nil
116 "Address of next memory page for program memory buffer.")
117 (defvar gdb-memory-prev-page nil
118 "Address of previous memory page for program memory buffer.")
120 (defvar gdb-frame-number "0")
121 (defvar gdb-thread-number "1"
122 "Main current thread.
124 Invalidation triggers use this variable to query GDB for
125 information on the specified thread.
127 This variable may be updated implicitly by GDB via
128 `gdb-thread-list-handler-custom' or explicitly by
129 `gdb-select-thread'.")
131 (defvar gdb-selected-frame nil)
132 (defvar gdb-selected-file nil)
133 (defvar gdb-selected-line nil)
134 (defvar gdb-current-language nil)
135 (defvar gdb-var-list nil
136 "List of variables in watch window.
137 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS) where
138 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
139 (defvar gdb-main-file nil "Source file from which program execution begins.")
140 (defvar gdb-overlay-arrow-position nil)
141 (defvar gdb-stack-position nil)
142 (defvar gdb-breakpoints-list nil
143 "List of breakpoints.
145 `gdb-get-field' is used to access breakpoints data stored in this
146 variable. Each element contains the same fields as \"body\"
147 member of \"-break-info\".")
148 (defvar gdb-location-alist nil
149 "Alist of breakpoint numbers and full filenames. Only used for files that
151 (defvar gdb-active-process nil
152 "GUD tooltips display variable values when t, and macro definitions otherwise.")
153 (defvar gdb-error "Non-nil when GDB is reporting an error.")
154 (defvar gdb-macro-info nil
155 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
156 (defvar gdb-register-names nil "List of register names.")
157 (defvar gdb-changed-registers nil
158 "List of changed register numbers (strings).")
159 (defvar gdb-buffer-fringe-width nil)
160 (defvar gdb-last-command nil)
161 (defvar gdb-prompt-name nil)
162 (defvar gdb-token-number 0)
163 (defvar gdb-handler-alist '())
164 (defvar gdb-handler-number nil)
165 (defvar gdb-source-file-list nil
166 "List of source files for the current executable.")
167 (defvar gdb-first-done-or-error t)
168 (defvar gdb-source-window nil)
169 (defvar gdb-inferior-status nil)
170 (defvar gdb-continuation nil)
171 (defvar gdb-filter-output nil
172 "Message to be shown in GUD console.
174 This variable is updated in `gdb-done-or-error' and returned by
175 `gud-gdbmi-marker-filter'.")
177 (defvar gdb-buffer-type nil
178 "One of the symbols bound in `gdb-buffer-rules'.")
179 (make-variable-buffer-local 'gdb-buffer-type)
181 (defvar gdb-output-sink 'nil
182 "The disposition of the output of the current gdb command.
183 Possible values are these symbols:
185 `user' -- gdb output should be copied to the GUD buffer
188 `emacs' -- output should be collected in the partial-output-buffer
189 for subsequent processing by a command. This is the
190 disposition of output generated by commands that
191 gdb mode sends to gdb on its own behalf.")
193 (defvar gdb-pending-triggers '()
194 "A list of trigger functions which have not yet been handled.
196 Elements are either function names or pairs (buffer . function)")
198 (defmacro gdb-add-pending (item)
199 `(push ,item gdb-pending-triggers))
200 (defmacro gdb-pending-p (item)
201 `(member ,item gdb-pending-triggers))
202 (defmacro gdb-delete-pending (item)
203 `(setq gdb-pending-triggers
204 (delete ,item gdb-pending-triggers)))
206 (defcustom gdb-debug-log-max 128
207 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
209 :type '(choice (integer :tag "Number of elements")
210 (const :tag "Unlimited" nil))
213 (defvar gdb-debug-log nil
214 "List of commands sent to and replies received from GDB.
215 Most recent commands are listed first. This list stores only the last
216 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
219 (defcustom gdb-enable-debug nil
220 "Non-nil means record the process input and output in `gdb-debug-log'."
225 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
226 "Shell command for generating a list of defined macros in a source file.
227 This list is used to display the #define directive associated
228 with an identifier as a tooltip. It works in a debug session with
229 GDB, when `gud-tooltip-mode' is t.
231 Set `gdb-cpp-define-alist-flags' for any include paths or
237 (defcustom gdb-cpp-define-alist-flags ""
238 "Preprocessor flags for `gdb-cpp-define-alist-program'."
243 (defcustom gdb-create-source-file-list t
244 "Non-nil means create a list of files from which the executable was built.
245 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
246 line for a long time when starting, possibly because your executable was
247 built from a large number of files. This allows quicker initialization
248 but means that these files are not automatically enabled for debugging,
249 e.g., you won't be able to click in the fringe to set a breakpoint until
250 execution has already stopped there."
255 (defcustom gdb-show-main nil
256 "Non-nil means display source file containing the main routine at startup.
257 Also display the main routine in the disassembly buffer if present."
262 ; Note: This mode requires a separate buffer for inferior IO.
263 (defconst gdb-use-separate-io-buffer t)
265 (defun gdb-force-mode-line-update (status)
266 (let ((buffer gud-comint-buffer))
267 (if (and buffer (buffer-name buffer))
268 (with-current-buffer buffer
269 (setq mode-line-process
271 (process-status (get-buffer-process buffer)) status))
272 ;; Force mode line redisplay soon.
273 (force-mode-line-update)))))
275 (defun gdb-enable-debug (arg)
276 "Toggle logging of transaction between Emacs and Gdb.
277 The log is stored in `gdb-debug-log' as an alist with elements
278 whose cons is send, send-item or recv and whose cdr is the string
279 being transferred. This list may grow up to a size of
280 `gdb-debug-log-max' after which the oldest element (at the end of
281 the list) is deleted every time a new one is added (at the front)."
283 (setq gdb-enable-debug
285 (not gdb-enable-debug)
286 (> (prefix-numeric-value arg) 0)))
287 (message (format "Logging of transaction %sabled"
288 (if gdb-enable-debug "en" "dis"))))
290 (defun gdb-find-watch-expression ()
291 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
292 (varnum (car var)) expr array)
293 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
294 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
295 (component-list (split-string (match-string 2 varnum) "\\." t)))
296 (setq expr (nth 1 var1))
297 (setq varnumlet (car var1))
298 (dolist (component component-list)
299 (setq var2 (assoc varnumlet gdb-var-list))
300 (setq expr (concat expr
301 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
302 (concat "[" component "]")
303 (concat "." component))))
304 (setq varnumlet (concat varnumlet "." component)))
307 (defvar gdb-locals-font-lock-keywords
310 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
311 (1 font-lock-variable-name-face)
312 (3 font-lock-type-face))
314 "Font lock keywords used in `gdb-local-mode'.")
317 (defun gdb (command-line)
318 "Run gdb on program FILE in buffer *gud-FILE*.
319 The directory containing FILE becomes the initial working directory
320 and source-file directory for your debugger.
322 If `gdb-many-windows' is nil (the default value) then gdb just
323 pops up the GUD buffer unless `gdb-show-main' is t. In this case
324 it starts with two windows: one displaying the GUD buffer and the
325 other with the source file with the main routine of the inferior.
327 If `gdb-many-windows' is t, regardless of the value of
328 `gdb-show-main', the layout below will appear. Keybindings are
329 shown in some of the buffers.
331 Watch expressions appear in the speedbar/slowbar.
333 The following commands help control operation :
335 `gdb-many-windows' - Toggle the number of windows gdb uses.
336 `gdb-restore-windows' - To restore the window layout.
338 See Info node `(emacs)GDB Graphical Interface' for a more
339 detailed description of this mode.
342 +----------------------------------------------------------------------+
344 +-----------------------------------+----------------------------------+
345 | GUD buffer (I/O of GDB) | Locals buffer |
349 +-----------------------------------+----------------------------------+
350 | Source buffer | I/O buffer (of debugged program) |
358 +-----------------------------------+----------------------------------+
359 | Stack buffer | Breakpoints buffer |
360 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
361 | | RET gdb-goto-breakpoint |
362 | | D gdb-delete-breakpoint |
363 +-----------------------------------+----------------------------------+"
365 (interactive (list (gud-query-cmdline 'gdb)))
367 (when (and gud-comint-buffer
368 (buffer-name gud-comint-buffer)
369 (get-buffer-process gud-comint-buffer)
370 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
371 (gdb-restore-windows)
373 "Multiple debugging requires restarting in text command mode"))
375 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
376 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
377 (setq comint-input-sender 'gdb-send)
379 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
380 "Set temporary breakpoint at current line.")
382 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
383 "\C-j" "Set execution address to current line.")
385 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
386 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
387 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
388 (gud-def gud-pstar "print* %e" nil
389 "Evaluate C dereferenced pointer expression at point.")
391 (gud-def gud-step "-exec-step %p" "\C-s"
392 "Step one source line with display.")
393 (gud-def gud-stepi "-exec-step-instruction %p" "\C-i"
394 "Step one instruction with display.")
395 (gud-def gud-next "-exec-next %p" "\C-n"
396 "Step one line (skip functions).")
397 (gud-def gud-nexti "nexti %p" nil
398 "Step one instruction (skip functions).")
399 (gud-def gud-cont "-exec-continue" "\C-r"
400 "Continue with display.")
401 (gud-def gud-finish "-exec-finish" "\C-f"
402 "Finish executing current function.")
403 (gud-def gud-run "-exec-run" nil "Runn the program.")
405 (local-set-key "\C-i" 'gud-gdb-complete-command)
406 (setq gdb-first-prompt t)
407 (setq gud-running nil)
409 (run-hooks 'gdb-mode-hook))
412 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
413 (gud-call "break %f:%l" arg)
417 (gud-call "break *%a" arg)))
418 "\C-b" "Set breakpoint at current line or address.")
420 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
421 (gud-call "clear %f:%l" arg)
425 (gud-call "clear *%a" arg)))
426 "\C-d" "Remove breakpoint at current line or address.")
428 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
429 (gud-call "-exec-until %f:%l" arg)
433 (gud-call "-exec-until *%a" arg)))
434 "\C-u" "Continue to current line or address.")
437 gud-go (gud-call (if gdb-active-process "-exec-continue" "-exec-run") arg)
438 nil "Start or continue execution.")
440 ;; For debugging Emacs only.
444 "pp1 " (if (eq (buffer-local-value
445 'major-mode (window-buffer)) 'speedbar-mode)
446 (gdb-find-watch-expression) "%e")) arg)
447 nil "Print the Emacs s-expression.")
449 (define-key gud-minor-mode-map [left-margin mouse-1]
450 'gdb-mouse-set-clear-breakpoint)
451 (define-key gud-minor-mode-map [left-fringe mouse-1]
452 'gdb-mouse-set-clear-breakpoint)
453 (define-key gud-minor-mode-map [left-margin C-mouse-1]
454 'gdb-mouse-toggle-breakpoint-margin)
455 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
456 'gdb-mouse-toggle-breakpoint-fringe)
458 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
460 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
462 (define-key gud-minor-mode-map [left-margin mouse-3]
464 (define-key gud-minor-mode-map [left-fringe mouse-3]
467 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
469 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
471 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
473 (define-key gud-minor-mode-map [left-margin C-mouse-3]
477 (setq gdb-pc-address (if gdb-show-main "main" nil))
478 (setq gdb-selected-frame nil
481 gdb-pending-triggers nil
482 gdb-output-sink 'user
483 gdb-location-alist nil
484 gdb-source-file-list nil
487 gdb-handler-alist '()
488 gdb-handler-number nil
490 gdb-first-done-or-error t
491 gdb-buffer-fringe-width (car (window-fringes))
493 gdb-source-window nil
494 gdb-inferior-status nil
495 gdb-continuation nil)
497 (setq gdb-buffer-type 'gdbmi)
499 (gdb-force-mode-line-update
500 (propertize "initializing..." 'face font-lock-variable-name-face))
501 (setq gdb-buf-publisher '())
502 (when gdb-use-separate-io-buffer
503 (gdb-get-buffer-create 'gdb-inferior-io)
504 (gdb-clear-inferior-io)
505 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
507 ;; Needs GDB 6.4 onwards
508 (list (concat "-inferior-tty-set "
509 (process-tty-name (get-process "gdb-inferior")))
511 (if (eq window-system 'w32)
512 (gdb-input (list "-gdb-set new-console off" 'ignore)))
513 (gdb-input (list "-gdb-set height 0" 'ignore))
514 ;; find source file and compilation directory here
516 ; Needs GDB 6.2 onwards.
517 (list "-file-list-exec-source-files" 'gdb-get-source-file-list))
518 (if gdb-create-source-file-list
520 ; Needs GDB 6.0 onwards.
521 (list "-file-list-exec-source-file" 'gdb-get-source-file)))
523 (list "-gdb-show prompt" 'gdb-get-prompt)))
525 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
527 (defun gdb-create-define-alist ()
528 "Create an alist of #define directives for GUD tooltips."
529 (let* ((file (buffer-file-name))
531 (with-output-to-string
532 (with-current-buffer standard-output
535 ;; call-process doesn't work with remote file names.
536 (not (file-remote-p default-directory))
537 (call-process shell-file-name file
538 (list t nil) nil "-c"
539 (concat gdb-cpp-define-alist-program " "
540 gdb-cpp-define-alist-flags))))))
541 (define-list (split-string output "\n" t))
543 (setq gdb-define-alist nil)
544 (dolist (define define-list)
545 (setq name (nth 1 (split-string define "[( ]")))
546 (push (cons name define) gdb-define-alist))))
548 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
549 (defvar tooltip-use-echo-area)
551 (defun gdb-tooltip-print (expr)
553 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
554 (goto-char (point-min))
556 (if (search-forward "=" nil t)
557 (concat expr (buffer-substring (- (point) 2) (point-max)))
559 ;; remove newline for gud-tooltip-echo-area
560 (substring string 0 (- (length string) 1))))
561 (or gud-tooltip-echo-area tooltip-use-echo-area
562 (not (display-graphic-p)))))
564 ;; If expr is a macro for a function don't print because of possible dangerous
565 ;; side-effects. Also printing a function within a tooltip generates an
566 ;; unexpected starting annotation (phase error).
567 (defun gdb-tooltip-print-1 (expr)
568 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
569 (goto-char (point-min))
570 (if (search-forward "expands to: " nil t)
571 (unless (looking-at "\\S-+.*(.*).*")
573 (list (concat "print " expr)
574 `(lambda () (gdb-tooltip-print ,expr))))))))
576 (defun gdb-init-buffer ()
577 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
578 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
579 (when gud-tooltip-mode
580 (make-local-variable 'gdb-define-alist)
581 (gdb-create-define-alist)
582 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
584 (defmacro gdb-if-arrow (arrow-position &rest body)
586 (let ((buffer (marker-buffer ,arrow-position)) (line))
587 (if (equal buffer (window-buffer (posn-window end)))
588 (with-current-buffer buffer
589 (when (or (equal start end)
590 (equal (posn-point start)
591 (marker-position ,arrow-position)))
594 (defun gdb-mouse-until (event)
595 "Continue running until a source line past the current line.
596 The destination source line can be selected either by clicking
597 with mouse-3 on the fringe/margin or dragging the arrow
598 with mouse-1 (default bindings)."
600 (let ((start (event-start event))
601 (end (event-end event)))
602 (gdb-if-arrow gud-overlay-arrow-position
603 (setq line (line-number-at-pos (posn-point end)))
604 (gud-call (concat "until " (number-to-string line))))
605 (gdb-if-arrow gdb-overlay-arrow-position
607 (goto-line (line-number-at-pos (posn-point end)))
609 (gud-call (concat "until *%a"))))))
611 (defun gdb-mouse-jump (event)
612 "Set execution address/line.
613 The destination source line can be selected either by clicking with C-mouse-3
614 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
615 Unlike `gdb-mouse-until' the destination address can be before the current
616 line, and no execution takes place."
618 (let ((start (event-start event))
619 (end (event-end event)))
620 (gdb-if-arrow gud-overlay-arrow-position
621 (setq line (line-number-at-pos (posn-point end)))
623 (gud-call (concat "tbreak " (number-to-string line)))
624 (gud-call (concat "jump " (number-to-string line)))))
625 (gdb-if-arrow gdb-overlay-arrow-position
627 (goto-line (line-number-at-pos (posn-point end)))
630 (gud-call (concat "tbreak *%a"))
631 (gud-call (concat "jump *%a")))))))
633 (defcustom gdb-show-changed-values t
634 "If non-nil change the face of out of scope variables and changed values.
635 Out of scope variables are suppressed with `shadow' face.
636 Changed values are highlighted with the face `font-lock-warning-face'."
641 (defcustom gdb-max-children 40
642 "Maximum number of children before expansion requires confirmation."
647 (defcustom gdb-delete-out-of-scope t
648 "If non-nil delete watch expressions automatically when they go out of scope."
653 (defcustom gdb-speedbar-auto-raise nil
654 "If non-nil raise speedbar every time display of watch expressions is\
660 (defcustom gdb-use-colon-colon-notation nil
661 "If non-nil use FUN::VAR format to display variables in the speedbar."
666 (defun gdb-speedbar-auto-raise (arg)
667 "Toggle automatic raising of the speedbar for watch expressions.
668 With prefix argument ARG, automatically raise speedbar if ARG is
669 positive, otherwise don't automatically raise it."
671 (setq gdb-speedbar-auto-raise
673 (not gdb-speedbar-auto-raise)
674 (> (prefix-numeric-value arg) 0)))
675 (message (format "Auto raising %sabled"
676 (if gdb-speedbar-auto-raise "en" "dis"))))
678 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
679 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
681 (declare-function tooltip-identifier-from-point "tooltip" (point))
683 (defun gud-watch (&optional arg event)
684 "Watch expression at point.
685 With arg, enter name of variable to be watched in the minibuffer."
686 (interactive (list current-prefix-arg last-input-event))
687 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
688 (if (eq minor-mode 'gdbmi)
690 (if event (posn-set-point (event-end event)))
692 (save-selected-window
695 (completing-read "Name of variable: "
696 'gud-gdb-complete-command)
697 (if (and transient-mark-mode mark-active)
698 (buffer-substring (region-beginning) (region-end))
699 (concat (if (eq major-mode 'gdb-registers-mode) "$")
700 (tooltip-identifier-from-point (point)))))))
701 (set-text-properties 0 (length expr) nil expr)
703 (list (concat"-var-create - * " expr "")
704 `(lambda () (gdb-var-create-handler ,expr)))))))
705 (message "gud-watch is a no-op in this mode."))))
707 (defconst gdb-var-create-regexp
708 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",\\(?:.*value=\\(\".*\"\\),\\)?.*type=\"\\(.*?\\)\"")
710 (defun gdb-var-create-handler (expr)
711 (goto-char (point-min))
712 (if (re-search-forward gdb-var-create-regexp nil t)
715 (if (and (string-equal gdb-current-language "c")
716 gdb-use-colon-colon-notation gdb-selected-frame)
717 (setq expr (concat gdb-selected-frame "::" expr))
721 (if (match-string 3) (read (match-string 3)))
723 (push var gdb-var-list)
725 (unless (string-equal
726 speedbar-initial-expansion-list-name "GUD")
727 (speedbar-change-initial-expansion-list "GUD"))
730 (concat "-var-evaluate-expression " (car var))
731 `(lambda () (gdb-var-evaluate-expression-handler
733 (message-box "No symbol \"%s\" in current context." expr)))
735 (defun gdb-speedbar-update ()
736 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
737 (not (gdb-pending-p 'gdb-speedbar-timer)))
738 ;; Dummy command to update speedbar even when idle.
739 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn))
740 ;; Keep gdb-pending-triggers non-nil till end.
741 (gdb-add-pending 'gdb-speedbar-timer)))
743 (defun gdb-speedbar-timer-fn ()
744 (if gdb-speedbar-auto-raise
745 (raise-frame speedbar-frame))
746 (gdb-delete-pending 'gdb-speedbar-timer)
749 (defun gdb-var-evaluate-expression-handler (varnum changed)
750 (goto-char (point-min))
751 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
752 (let ((var (assoc varnum gdb-var-list)))
754 (if changed (setcar (nthcdr 5 var) 'changed))
755 (setcar (nthcdr 4 var) (read (match-string 1)))))
756 (gdb-speedbar-update))
758 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
759 (defun gdb-var-list-children (varnum)
761 (list (concat "-var-update " varnum) 'ignore))
763 (list (concat "-var-list-children --all-values "
765 `(lambda () (gdb-var-list-children-handler ,varnum)))))
767 (defconst gdb-var-list-children-regexp
768 "child={.*?name=\"\\(.+?\\)\".*?,exp=\"\\(.+?\\)\".*?,\
769 numchild=\"\\(.+?\\)\".*?,value=\\(\".*?\"\\).*?,type=\"\\(.+?\\)\".*?}")
771 (defun gdb-var-list-children-handler (varnum)
772 (goto-char (point-min))
773 (let ((var-list nil))
774 (catch 'child-already-watched
775 (dolist (var gdb-var-list)
776 (if (string-equal varnum (car var))
779 (while (re-search-forward gdb-var-list-children-regexp nil t)
780 (let ((varchild (list (match-string 1)
784 (read (match-string 4))
786 (if (assoc (car varchild) gdb-var-list)
787 (throw 'child-already-watched nil))
788 (push varchild var-list))))
789 (push var var-list)))
790 (setq gdb-var-list (nreverse var-list))))
791 (gdb-speedbar-update))
793 (defun gdb-var-set-format (format)
794 "Set the output format for a variable displayed in the speedbar."
795 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
798 (list (concat "-var-set-format " varnum " " format) 'ignore))
801 (defun gdb-var-delete-1 (varnum)
803 (list (concat "-var-delete " varnum) 'ignore))
804 (setq gdb-var-list (delq var gdb-var-list))
805 (dolist (varchild gdb-var-list)
806 (if (string-match (concat (car var) "\\.") (car varchild))
807 (setq gdb-var-list (delq varchild gdb-var-list)))))
809 (defun gdb-var-delete ()
810 "Delete watch expression at point from the speedbar."
812 (let ((text (speedbar-line-text)))
813 (string-match "\\(\\S-+\\)" text)
814 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
816 (if (string-match "\\." (car var))
817 (message-box "Can only delete a root expression")
818 (gdb-var-delete-1 varnum)))))
820 (defun gdb-var-delete-children (varnum)
821 "Delete children of variable object at point from the speedbar."
823 (list (concat "-var-delete -c " varnum) 'ignore)))
825 (defun gdb-edit-value (text token indent)
826 "Assign a value to a variable displayed in the speedbar."
827 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
828 (varnum (car var)) (value))
829 (setq value (read-string "New value: "))
831 (list (concat "-var-assign " varnum " " value)
832 `(lambda () (gdb-edit-value-handler ,value))))))
834 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
836 (defun gdb-edit-value-handler (value)
837 (goto-char (point-min))
838 (if (re-search-forward gdb-error-regexp nil t)
839 (message-box "Invalid number or expression (%s)" value)))
841 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
842 (defun gdb-var-update ()
843 (if (not (gdb-pending-p 'gdb-var-update))
845 (list "-var-update --all-values *" 'gdb-var-update-handler)))
846 (gdb-add-pending 'gdb-var-update))
848 (defconst gdb-var-update-regexp
849 "{.*?name=\"\\(.*?\\)\".*?,\\(?:value=\\(\".*?\"\\),\\)?.*?\
850 in_scope=\"\\(.*?\\)\".*?}")
852 (defun gdb-var-update-handler ()
853 (dolist (var gdb-var-list)
854 (setcar (nthcdr 5 var) nil))
855 (goto-char (point-min))
856 (while (re-search-forward gdb-var-update-regexp nil t)
857 (let* ((varnum (match-string 1))
858 (var (assoc varnum gdb-var-list)))
860 (let ((match (match-string 3)))
861 (cond ((string-equal match "false")
862 (if gdb-delete-out-of-scope
863 (gdb-var-delete-1 varnum)
864 (setcar (nthcdr 5 var) 'out-of-scope)))
865 ((string-equal match "true")
866 (setcar (nthcdr 5 var) 'changed)
867 (setcar (nthcdr 4 var)
868 (read (match-string 2))))
869 ((string-equal match "invalid")
870 (gdb-var-delete-1 varnum)))))))
871 (gdb-delete-pending 'gdb-var-update)
872 (gdb-speedbar-update))
874 (defun gdb-speedbar-expand-node (text token indent)
875 "Expand the node the user clicked on.
876 TEXT is the text of the button we clicked on, a + or - item.
877 TOKEN is data related to this node.
878 INDENT is the current indentation depth."
879 (cond ((string-match "+" text) ;expand this node
880 (let* ((var (assoc token gdb-var-list))
881 (expr (nth 1 var)) (children (nth 2 var)))
882 (if (or (<= (string-to-number children) gdb-max-children)
884 (format "%s has %s children. Continue? " expr children)))
885 (gdb-var-list-children token))))
886 ((string-match "-" text) ;contract this node
887 (dolist (var gdb-var-list)
888 (if (string-match (concat token "\\.") (car var))
889 (setq gdb-var-list (delq var gdb-var-list))))
890 (gdb-var-delete-children token)
891 (speedbar-change-expand-button-char ?+)
892 (speedbar-delete-subblock indent))
893 (t (error "Ooops... not sure what to do")))
894 (speedbar-center-buffer-smartly))
896 (defun gdb-get-target-string ()
897 (with-current-buffer gud-comint-buffer
904 ;; Each buffer has a TYPE -- a symbol that identifies the function
905 ;; of that particular buffer.
907 ;; The usual gdb interaction buffer is given the type `gdbmi' and
908 ;; is constructed specially.
910 ;; Others are constructed by gdb-get-buffer-create and
911 ;; named according to the rules set forth in the gdb-buffer-rules
913 (defvar gdb-buffer-rules '())
914 (defalias 'gdb-rules-name-maker 'second)
915 (defalias 'gdb-rules-buffer-mode 'third)
916 (defalias 'gdb-rules-update-trigger 'fourth)
918 (defun gdb-update-buffer-name ()
919 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
921 (when f (rename-buffer (funcall f)))))
923 (defun gdb-get-buffer (key &optional thread)
924 "Get a specific GDB buffer.
926 In that buffer, `gdb-buffer-type' must be equal to KEY and
927 `gdb-thread-number' (if provided) must be equal to THREAD.
929 When THREAD is nil, global `gdb-thread-number' value is used."
930 (when (not thread) (setq thread gdb-thread-number))
932 (dolist (buffer (buffer-list) nil)
933 (with-current-buffer buffer
934 (when (and (eq gdb-buffer-type key)
935 (equal gdb-thread-number thread))
936 (throw 'found buffer))))))
938 (defun gdb-get-buffer-create (key &optional thread)
939 "Create a new GDB buffer of the type specified by KEY.
940 The key should be one of the cars in `gdb-buffer-rules'.
942 If THREAD is non-nil, it is assigned to `gdb-thread-number'
943 buffer-local variable of the new buffer.
945 If buffer's mode returns a symbol, it's used to register "
946 (or (gdb-get-buffer key thread)
947 (let ((rules (assoc key gdb-buffer-rules))
948 (new (generate-new-buffer "limbo")))
949 (with-current-buffer new
950 (let ((mode (gdb-rules-buffer-mode rules))
951 (trigger (gdb-rules-update-trigger rules)))
952 (when mode (funcall mode))
953 (setq gdb-buffer-type key)
955 (set (make-local-variable 'gdb-thread-number) thread))
956 (set (make-local-variable 'gud-minor-mode)
957 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
958 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
959 (rename-buffer (funcall (gdb-rules-name-maker rules)))
961 (gdb-add-subscriber gdb-buf-publisher
962 (cons (current-buffer)
963 (gdb-bind-function-to-buffer trigger (current-buffer))))
965 (current-buffer))))))
967 (defun gdb-bind-function-to-buffer (expr buffer)
968 "Return a function which will evaluate EXPR in BUFFER."
969 `(lambda (&rest args)
970 (with-current-buffer ,buffer
971 (apply ',expr args))))
973 ;; Used to define all gdb-frame-*-buffer functions except
974 ;; `gdb-frame-separate-io-buffer'
975 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
976 "Define a function NAME which shows gdb BUFFER in a separate frame.
978 DOC is an optional documentation string."
979 `(defun ,name (&optional thread)
982 (let ((special-display-regexps (append special-display-regexps '(".*")))
983 (special-display-frame-alist gdb-frame-parameters))
984 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
986 (defmacro def-gdb-display-buffer (name buffer &optional doc)
987 "Define a function NAME which shows gdb BUFFER.
989 DOC is an optional documentation string."
990 `(defun ,name (&optional thread)
994 (gdb-get-buffer-create ,buffer thread) t)))
996 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
997 ;; at least one and possible more functions. The functions have these
998 ;; roles in defining a buffer type:
1000 ;; NAME - Return a name for this buffer type.
1002 ;; The remaining function(s) are optional:
1004 ;; MODE - called in a new buffer with no arguments, should establish
1005 ;; the proper mode for the buffer.
1008 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1009 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1011 (setcdr binding rules)
1012 (push (cons buffer-type rules)
1013 gdb-buffer-rules))))
1015 ;; GUD buffers are an exception to the rules
1016 (gdb-set-buffer-rules 'gdbmi 'error)
1018 ;; Partial-output buffer : This accumulates output from a command executed on
1019 ;; behalf of emacs (rather than the user).
1021 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1022 'gdb-partial-output-name)
1024 (defun gdb-partial-output-name ()
1025 (concat " *partial-output-"
1026 (gdb-get-target-string)
1030 (gdb-set-buffer-rules 'gdb-inferior-io
1031 'gdb-inferior-io-name
1032 'gdb-inferior-io-mode)
1034 (defun gdb-inferior-io-name ()
1035 (concat "*input/output of "
1036 (gdb-get-target-string)
1039 (defun gdb-display-separate-io-buffer ()
1040 "Display IO of debugged program in a separate window."
1042 (if gdb-use-separate-io-buffer
1044 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1046 (defconst gdb-frame-parameters
1047 '((height . 14) (width . 80)
1049 (tool-bar-lines . nil)
1050 (menu-bar-lines . nil)
1051 (minibuffer . nil)))
1053 (defun gdb-frame-separate-io-buffer ()
1054 "Display IO of debugged program in a new frame."
1056 (if gdb-use-separate-io-buffer
1057 (let ((special-display-regexps (append special-display-regexps '(".*")))
1058 (special-display-frame-alist gdb-frame-parameters))
1059 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1061 (defvar gdb-inferior-io-mode-map
1062 (let ((map (make-sparse-keymap)))
1063 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1064 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1065 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1066 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1067 (define-key map "\C-d" 'gdb-separate-io-eof)
1070 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1071 "Major mode for gdb inferior-io."
1072 :syntax-table nil :abbrev-table nil
1073 ;; We want to use comint because it has various nifty and familiar
1074 ;; features. We don't need a process, but comint wants one, so create
1076 (make-comint-in-buffer
1077 "gdb-inferior" (current-buffer) "sleep" nil "1000000000"))
1079 (defun gdb-inferior-filter (proc string)
1080 (unless (string-equal string "")
1081 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1082 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1083 (insert-before-markers string)))
1085 (defun gdb-separate-io-interrupt ()
1086 "Interrupt the program being debugged."
1089 (get-buffer-process gud-comint-buffer) comint-ptyp))
1091 (defun gdb-separate-io-quit ()
1092 "Send quit signal to the program being debugged."
1095 (get-buffer-process gud-comint-buffer) comint-ptyp))
1097 (defun gdb-separate-io-stop ()
1098 "Stop the program being debugged."
1101 (get-buffer-process gud-comint-buffer) comint-ptyp))
1103 (defun gdb-separate-io-eof ()
1104 "Send end-of-file to the program being debugged."
1107 (get-buffer-process gud-comint-buffer)))
1109 (defun gdb-clear-inferior-io ()
1110 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1114 (defconst breakpoint-xpm-data
1116 static char *magick[] = {
1117 /* columns rows colors chars-per-pixel */
1133 "XPM data used for breakpoint icon.")
1135 (defconst breakpoint-enabled-pbm-data
1138 0 0 0 0 1 1 1 1 0 0 0 0
1139 0 0 0 1 1 1 1 1 1 0 0 0
1140 0 0 1 1 1 1 1 1 1 1 0 0
1141 0 1 1 1 1 1 1 1 1 1 1 0
1142 0 1 1 1 1 1 1 1 1 1 1 0
1143 0 1 1 1 1 1 1 1 1 1 1 0
1144 0 1 1 1 1 1 1 1 1 1 1 0
1145 0 0 1 1 1 1 1 1 1 1 0 0
1146 0 0 0 1 1 1 1 1 1 0 0 0
1147 0 0 0 0 1 1 1 1 0 0 0 0"
1148 "PBM data used for enabled breakpoint icon.")
1150 (defconst breakpoint-disabled-pbm-data
1162 0 0 0 1 0 1 0 1 0 0"
1163 "PBM data used for disabled breakpoint icon.")
1165 (defvar breakpoint-enabled-icon nil
1166 "Icon for enabled breakpoint in display margin.")
1168 (defvar breakpoint-disabled-icon nil
1169 "Icon for disabled breakpoint in display margin.")
1171 (declare-function define-fringe-bitmap "fringe.c"
1172 (bitmap bits &optional height width align))
1174 (and (display-images-p)
1175 ;; Bitmap for breakpoint in fringe
1176 (define-fringe-bitmap 'breakpoint
1177 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1178 ;; Bitmap for gud-overlay-arrow in fringe
1179 (define-fringe-bitmap 'hollow-right-triangle
1180 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1182 (defface breakpoint-enabled
1186 "Face for enabled breakpoint icon in fringe."
1189 (defface breakpoint-disabled
1190 '((((class color) (min-colors 88)) :foreground "grey70")
1191 ;; Ensure that on low-color displays that we end up something visible.
1192 (((class color) (min-colors 8) (background light))
1193 :foreground "black")
1194 (((class color) (min-colors 8) (background dark))
1195 :foreground "white")
1196 (((type tty) (class mono))
1198 (t :background "gray"))
1199 "Face for disabled breakpoint icon in fringe."
1203 (defun gdb-send (proc string)
1204 "A comint send filter for gdb."
1205 (with-current-buffer gud-comint-buffer
1206 (let ((inhibit-read-only t))
1207 (remove-text-properties (point-min) (point-max) '(face))))
1208 ;; mimic <RET> key to repeat previous command in GDB
1209 (if (not (string-match "^\\s+$" string))
1210 (setq gdb-last-command string)
1211 (if gdb-last-command (setq string gdb-last-command)))
1212 (if gdb-enable-debug
1213 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1214 (if (string-match "^-" string)
1217 (setq gdb-first-done-or-error t)
1218 (process-send-string proc (concat string "\n")))
1220 (if (string-match "\\\\$" string)
1221 (setq gdb-continuation (concat gdb-continuation string "\n"))
1222 (setq gdb-first-done-or-error t)
1223 (process-send-string proc (concat "-interpreter-exec console \""
1224 gdb-continuation string "\"\n"))
1225 (setq gdb-continuation nil))))
1227 (defun gdb-input (item)
1228 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1229 (setq gdb-token-number (1+ gdb-token-number))
1230 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1231 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1232 (process-send-string (get-buffer-process gud-comint-buffer)
1233 (concat (car item) "\n")))
1235 (defun gdb-current-context-command (command)
1236 "Add --thread option to gdb COMMAND.
1238 Option value is taken from `gdb-thread-number'."
1239 (concat command " --thread " gdb-thread-number))
1241 (defun gdb-current-context-buffer-name (name)
1242 "Add thread information and asterisks to string NAME."
1244 (if (local-variable-p 'gdb-thread-number)
1245 " (bound to thread "
1246 " (current thread ")
1247 gdb-thread-number ")*"))
1250 (defcustom gud-gdb-command-name "gdb -i=mi"
1251 "Default command to execute an executable under the GDB debugger."
1256 (setq gud-running nil)
1257 (setq gdb-output-sink 'user)
1258 (setq gdb-pending-triggers nil))
1260 ;; Publish-subscribe
1262 (defmacro gdb-add-subscriber (publisher subscriber)
1263 "Register new PUBLISHER's SUBSCRIBER.
1265 SUBSCRIBER must be a pair, where cdr is a function of one
1266 argument (see `gdb-emit-signal')."
1267 `(add-to-list ',publisher ,subscriber))
1269 (defun gdb-get-subscribers (publisher)
1272 (defun gdb-emit-signal (publisher &optional signal)
1273 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
1274 (dolist (subscriber (gdb-get-subscribers publisher))
1275 (funcall (cdr subscriber) signal)))
1277 (defvar gdb-buf-publisher '()
1278 "Used to invalidate GDB buffers by emitting a signal in
1281 Must be a list of pairs with cars being buffers and cdr's being
1282 valid signal handlers.")
1284 (defun gdb-update ()
1285 "Update buffers showing status of debug session."
1286 (when gdb-first-prompt
1287 (gdb-force-mode-line-update
1288 (propertize "initializing..." 'face font-lock-variable-name-face))
1290 (setq gdb-first-prompt nil))
1291 ;; We may need to update gdb-thread-number, so we call threads buffer
1292 (gdb-get-buffer-create 'gdb-threads-buffer)
1293 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1294 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1296 (gdb-emit-signal gdb-buf-publisher 'update)
1297 (gdb-get-selected-frame)
1298 (gdb-get-changed-registers)
1300 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1301 (dolist (var gdb-var-list)
1302 (setcar (nthcdr 5 var) nil))
1305 ;; GUD displays the selected GDB frame. This might might not be the current
1306 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1307 ;; visited breakpoint is, use that window.
1308 (defun gdb-display-source-buffer (buffer)
1309 (let* ((last-window (if gud-last-last-frame
1311 (gud-find-file (car gud-last-last-frame)))))
1312 (source-window (or last-window
1313 (if (and gdb-source-window
1314 (window-live-p gdb-source-window))
1315 gdb-source-window))))
1317 (setq gdb-source-window source-window)
1318 (set-window-buffer source-window buffer))
1321 (defun gdb-car< (a b)
1322 (< (car a) (car b)))
1324 (defvar gdbmi-record-list
1325 '((gdb-gdb . "(gdb) \n")
1326 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1327 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1328 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1329 (gdb-console . "~\\(\".*?\"\\)\n")
1330 (gdb-internals . "&\\(\".*?\"\\)\n")
1331 (gdb-stopped . "\\*stopped,?\\(.*?\n\\)")
1332 (gdb-running . "\\*running,\\(.*?\n\\)")
1333 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1334 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")))
1336 (defun gud-gdbmi-marker-filter (string)
1337 "Filter GDB/MI output."
1339 ;; Record transactions if logging is enabled.
1340 (when gdb-enable-debug
1341 (push (cons 'recv string) gdb-debug-log)
1342 (if (and gdb-debug-log-max
1343 (> (length gdb-debug-log) gdb-debug-log-max))
1344 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1346 ;; Recall the left over gud-marker-acc from last time
1347 (setq gud-marker-acc (concat gud-marker-acc string))
1349 ;; Start accumulating output for the GUD buffer
1350 (setq gdb-filter-output "")
1351 (let ((output-record) (output-record-list))
1353 ;; Process all the complete markers in this chunk.
1354 (dolist (gdbmi-record gdbmi-record-list)
1355 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1356 (push (list (match-beginning 0)
1358 (match-string 1 gud-marker-acc)
1359 (match-string 2 gud-marker-acc)
1362 (setq gud-marker-acc
1363 (concat (substring gud-marker-acc 0 (match-beginning 0))
1364 ;; Pad with spaces to preserve position.
1365 (make-string (length (match-string 0 gud-marker-acc)) 32)
1366 (substring gud-marker-acc (match-end 0))))))
1368 (setq output-record-list (sort output-record-list 'gdb-car<))
1370 (dolist (output-record output-record-list)
1371 (let ((record-type (cadr output-record))
1372 (arg1 (nth 2 output-record))
1373 (arg2 (nth 3 output-record)))
1374 (if (eq record-type 'gdb-error)
1375 (gdb-done-or-error arg2 arg1 'error)
1376 (if (eq record-type 'gdb-done)
1377 (gdb-done-or-error arg2 arg1 'done)
1378 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1379 ;; error message on internal stream. Don't print to GUD buffer.
1380 (unless (and (eq record-type 'gdb-internals)
1381 (string-equal (read arg1) "No registers.\n"))
1382 (funcall record-type arg1))))))
1384 (setq gdb-output-sink 'user)
1386 (string-match "^ *" gud-marker-acc)
1387 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1391 (defun gdb-gdb (output-field))
1392 (defun gdb-thread-created (output-field))
1393 (defun gdb-thread-exited (output-field))
1395 (defun gdb-running (output-field)
1396 (setq gdb-inferior-status "running")
1397 (gdb-force-mode-line-update
1398 (propertize gdb-inferior-status 'face font-lock-type-face))
1399 (setq gdb-active-process t)
1400 (setq gud-running t))
1402 (defun gdb-starting (output-field)
1403 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1405 (list "-data-list-register-names" 'gdb-get-register-names))
1406 (setq gdb-inferior-status "running")
1407 (gdb-force-mode-line-update
1408 (propertize gdb-inferior-status 'face font-lock-type-face))
1409 (setq gdb-active-process t)
1410 (setq gud-running t))
1412 ;; -break-insert -t didn't give a reason before gdb 6.9
1413 (defconst gdb-stopped-regexp
1414 "\\(reason=\"\\(.*?\\)\"\\)?\\(\\(,exit-code=.*?\\)*\n\\|.*?,file=\".*?\".*?,fullname=\"\\(.*?\\)\".*?,line=\"\\(.*?\\)\".*?\n\\)")
1416 (defun gdb-stopped (output-field)
1417 (setq gud-running nil)
1418 (string-match gdb-stopped-regexp output-field)
1419 (let ((reason (match-string 2 output-field))
1420 (file (match-string 5 output-field)))
1422 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1423 ;;; because synchronous GDB doesn't give these fields with CLI.
1426 ;;; ;; Extract the frame position from the marker.
1427 ;;; gud-last-frame (cons file
1428 ;;; (string-to-number
1429 ;;; (match-string 6 gud-marker-acc)))))
1431 (setq gdb-inferior-status (if reason reason "unknown"))
1432 (gdb-force-mode-line-update
1433 (propertize gdb-inferior-status 'face font-lock-warning-face))
1434 (if (string-equal reason "exited-normally")
1435 (setq gdb-active-process nil)))
1437 (when gdb-first-done-or-error
1438 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name))
1440 (setq gdb-first-done-or-error nil)))
1442 ;; Remove the trimmings from log stream containing debugging messages
1443 ;; being produced by GDB's internals, use warning face and send to GUD
1445 (defun gdb-internals (output-field)
1446 (setq gdb-filter-output
1449 (let ((error-message
1450 (read output-field)))
1452 0 (length error-message)
1453 'face font-lock-warning-face
1457 ;; Remove the trimmings from the console stream and send to GUD buffer
1458 ;; (frontend MI commands should not print to this stream)
1459 (defun gdb-console (output-field)
1460 (setq gdb-filter-output
1463 (read output-field))))
1465 (defun gdb-done-or-error (output-field token-number type)
1466 (if (string-equal token-number "")
1467 ;; Output from command entered by user
1469 (setq gdb-output-sink 'user)
1470 (setq token-number nil)
1471 ;; MI error - send to minibuffer
1472 (when (eq type 'error)
1473 ;; Skip "msg=" from `output-field'
1474 (message (read (substring output-field 4)))
1475 ;; Don't send to the console twice. (If it is a console error
1476 ;; it is also in the console stream.)
1477 (setq output-field nil)))
1478 ;; Output from command from frontend.
1479 (setq gdb-output-sink 'emacs))
1481 (gdb-clear-partial-output)
1482 (when gdb-first-done-or-error
1483 (unless (or token-number gud-running)
1484 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1486 (setq gdb-first-done-or-error nil))
1488 (setq gdb-filter-output
1489 (gdb-concat-output gdb-filter-output output-field))
1493 (with-current-buffer
1494 (gdb-get-buffer-create 'gdb-partial-output-buffer)
1496 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
1497 (setq gdb-handler-alist
1498 (assq-delete-all token-number gdb-handler-alist)))))
1500 (defun gdb-concat-output (so-far new)
1501 (let ((sink gdb-output-sink))
1503 ((eq sink 'user) (concat so-far new))
1505 (gdb-append-to-partial-output new)
1508 (defun gdb-append-to-partial-output (string)
1509 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1510 (goto-char (point-max))
1513 (defun gdb-clear-partial-output ()
1514 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1517 (defun json-partial-output (&optional fix-key fix-list)
1518 "Parse gdb-partial-output-buffer with `json-read'.
1520 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurences from
1521 partial output. This is used to get rid of useless keys in lists
1522 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
1523 -break-info are examples of MI commands which issue such
1526 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
1527 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
1528 -break-info output when it contains breakpoint script field
1529 incompatible with GDB/MI output syntax.
1531 Note that GDB/MI output syntax is different from JSON both
1532 cosmetically and (in some cases) structurally, so correct results
1533 are not guaranteed."
1534 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1535 (goto-char (point-min))
1538 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
1539 (replace-match "" nil nil nil 1))))
1542 ;; Find positions of brackets which enclose broken list
1543 (while (re-search-forward (concat fix-list "={\"") nil t)
1544 (let ((p1 (goto-char (- (point) 2)))
1545 (p2 (progn (forward-sexp)
1547 ;; Replace braces with brackets
1555 (goto-char (point-min))
1557 ;; Wrap field names in double quotes and replace equal sign with
1559 ;; TODO: This breaks badly with foo= inside constants
1560 (while (re-search-forward "\\([[:alpha:]-_]+\\)=" nil t)
1561 (replace-match "\"\\1\":" nil nil))
1562 (goto-char (point-max))
1564 (goto-char (point-min))
1565 (let ((json-array-type 'list))
1568 (defun gdb-pad-string (string padding)
1569 (format (concat "%" (number-to-string padding) "s") string))
1571 (defalias 'gdb-get-field 'bindat-get-field)
1573 (defun gdb-get-many-fields (struct &rest fields)
1574 "Return a list of FIELDS values from STRUCT."
1576 (dolist (field fields values)
1577 (setq values (append values (list (gdb-get-field struct field)))))))
1579 ;; NAME is the function name.
1580 ;; GDB-COMMAND is a string of such. HANDLER-NAME is the function bound to the
1581 ;; current input and buffer which recieved the trigger signal.
1582 ;; Trigger must be bound to buffer via gdb-bind-function-to-buffer before use!
1583 ;; See how it's done in gdb-get-buffer-create.
1585 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
1587 `(defun ,trigger-name (&optional signal)
1588 (if (not (gdb-pending-p
1589 (cons (current-buffer) ',trigger-name)))
1593 (gdb-bind-function-to-buffer ',handler-name (current-buffer))))
1594 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
1596 ;; Used by disassembly buffer only, the rest use
1597 ;; def-gdb-trigger-and-handler
1598 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun)
1599 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
1601 Delete ((current-buffer) . TRIGGER) from `gdb-pending-triggers',
1602 erase current buffer and evaluate CUSTOM-DEFUN."
1603 `(defun ,handler-name ()
1604 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
1605 (let* ((buffer-read-only nil))
1608 (gdb-update-buffer-name))))
1610 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
1611 handler-name custom-defun)
1612 "Define trigger and handler.
1614 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
1616 HANDLER-NAME handler uses customization of CUSTOM-DEFUN."
1618 (def-gdb-auto-update-trigger ,trigger-name
1621 (def-gdb-auto-update-handler ,handler-name
1622 ,trigger-name ,custom-defun)))
1626 ;; Breakpoint buffer : This displays the output of `-break-list'.
1627 (def-gdb-trigger-and-handler
1628 gdb-invalidate-breakpoints "-break-list"
1629 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom)
1631 (gdb-set-buffer-rules
1632 'gdb-breakpoints-buffer
1633 'gdb-breakpoints-buffer-name
1634 'gdb-breakpoints-mode
1635 'gdb-invalidate-breakpoints)
1637 (defun gdb-breakpoints-list-handler-custom ()
1638 (let ((breakpoints-list (gdb-get-field
1639 (json-partial-output "bkpt" "script")
1640 'BreakpointTable 'body)))
1641 (setq gdb-breakpoints-list breakpoints-list)
1642 (insert "Num\tType\t\tDisp\tEnb\tHits\tAddr What\n")
1643 (dolist (breakpoint breakpoints-list)
1646 (gdb-get-field breakpoint 'number) "\t"
1647 (gdb-get-field breakpoint 'type) "\t"
1648 (gdb-get-field breakpoint 'disp) "\t"
1649 (let ((flag (gdb-get-field breakpoint 'enabled)))
1650 (if (string-equal flag "y")
1651 (propertize "y" 'face font-lock-warning-face)
1652 (propertize "n" 'face font-lock-type-face))) "\t"
1653 (gdb-get-field breakpoint 'times) "\t"
1654 (gdb-get-field breakpoint 'addr)))
1655 (let ((at (gdb-get-field breakpoint 'at)))
1660 (propertize (gdb-get-field breakpoint 'func)
1661 'face font-lock-function-name-face)))
1662 (gdb-insert-frame-location breakpoint)
1663 (add-text-properties (line-beginning-position)
1665 '(mouse-face highlight
1666 help-echo "mouse-2, RET: visit breakpoint"))))
1667 (at (insert (concat " " at)))
1668 (t (insert (gdb-get-field breakpoint 'original-location)))))
1669 (add-text-properties (line-beginning-position)
1671 `(gdb-breakpoint ,breakpoint))
1673 (gdb-place-breakpoints)))
1675 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1676 (defun gdb-place-breakpoints ()
1677 (let ((flag) (bptno))
1678 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1679 (dolist (buffer (buffer-list))
1680 (with-current-buffer buffer
1681 (if (and (eq gud-minor-mode 'gdbmi)
1682 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1683 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1684 (dolist (breakpoint gdb-breakpoints-list)
1685 (let ((line (gdb-get-field breakpoint 'line)))
1687 (let ((file (gdb-get-field breakpoint 'fullname))
1688 (flag (gdb-get-field breakpoint 'enabled))
1689 (bptno (gdb-get-field breakpoint 'number)))
1690 (unless (file-exists-p file)
1691 (setq file (cdr (assoc bptno gdb-location-alist))))
1693 (not (string-equal file "File not found")))
1694 (with-current-buffer
1695 (find-file-noselect file 'nowarn)
1697 ;; Only want one breakpoint icon at each location.
1699 (goto-line (string-to-number line))
1700 (gdb-put-breakpoint-icon (string-equal flag "y") bptno)))
1702 (list (concat "list " file ":1")
1705 (list "-file-list-exec-source-file"
1706 `(lambda () (gdb-get-location
1707 ,bptno ,line ,flag)))))))))))
1709 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
1711 (defun gdb-get-location (bptno line flag)
1712 "Find the directory containing the relevant source file.
1713 Put in buffer and place breakpoint icon."
1714 (goto-char (point-min))
1715 (catch 'file-not-found
1716 (if (re-search-forward gdb-source-file-regexp nil t)
1717 (delete (cons bptno "File not found") gdb-location-alist)
1718 (push (cons bptno (match-string 1)) gdb-location-alist)
1720 (unless (assoc bptno gdb-location-alist)
1721 (push (cons bptno "File not found") gdb-location-alist)
1722 (message-box "Cannot find source file for breakpoint location.
1723 Add directory to search path for source files using the GDB command, dir."))
1724 (throw 'file-not-found nil))
1725 (with-current-buffer (find-file-noselect (match-string 1))
1727 ;; only want one breakpoint icon at each location
1729 (goto-line (string-to-number line))
1730 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
1732 (add-hook 'find-file-hook 'gdb-find-file-hook)
1734 (defun gdb-find-file-hook ()
1735 "Set up buffer for debugging if file is part of the source code
1736 of the current session."
1737 (if (and (buffer-name gud-comint-buffer)
1738 ;; in case gud or gdb-ui is just loaded
1740 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
1742 (if (member buffer-file-name gdb-source-file-list)
1743 (with-current-buffer (find-buffer-visiting buffer-file-name)
1744 (gdb-init-buffer)))))
1746 (declare-function gud-remove "gdb-mi" t t) ; gud-def
1747 (declare-function gud-break "gdb-mi" t t) ; gud-def
1748 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
1750 (defun gdb-mouse-set-clear-breakpoint (event)
1751 "Set/clear breakpoint in left fringe/margin at mouse click.
1752 If not in a source or disassembly buffer just set point."
1754 (mouse-minibuffer-check event)
1755 (let ((posn (event-end event)))
1756 (with-selected-window (posn-window posn)
1757 (if (or (buffer-file-name) (eq major-mode 'gdb-disassembly-mode))
1758 (if (numberp (posn-point posn))
1760 (goto-char (posn-point posn))
1761 (if (or (posn-object posn)
1762 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1766 (posn-set-point posn))))
1768 (defun gdb-mouse-toggle-breakpoint-margin (event)
1769 "Enable/disable breakpoint in left margin with mouse click."
1771 (mouse-minibuffer-check event)
1772 (let ((posn (event-end event)))
1773 (if (numberp (posn-point posn))
1774 (with-selected-window (posn-window posn)
1776 (goto-char (posn-point posn))
1777 (if (posn-object posn)
1779 (let ((bptno (get-text-property
1780 0 'gdb-bptno (car (posn-string posn)))))
1782 (if (get-text-property
1783 0 'gdb-enabled (car (posn-string posn)))
1788 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1789 "Enable/disable breakpoint in left fringe with mouse click."
1791 (mouse-minibuffer-check event)
1792 (let* ((posn (event-end event))
1793 (pos (posn-point posn))
1796 (with-selected-window (posn-window posn)
1798 (set-buffer (window-buffer (selected-window)))
1800 (dolist (overlay (overlays-in pos pos))
1801 (when (overlay-get overlay 'put-break)
1802 (setq obj (overlay-get overlay 'before-string))))
1806 (if (get-text-property 0 'gdb-enabled obj)
1809 (get-text-property 0 'gdb-bptno obj)))))))))
1811 (defun gdb-breakpoints-buffer-name ()
1812 (concat "*breakpoints of " (gdb-get-target-string) "*"))
1814 (def-gdb-display-buffer
1815 gdb-display-breakpoints-buffer
1816 'gdb-breakpoints-buffer
1817 "Display status of user-settable breakpoints.")
1819 (def-gdb-frame-for-buffer
1820 gdb-frame-breakpoints-buffer
1821 'gdb-breakpoints-buffer
1822 "Display status of user-settable breakpoints in a new frame.")
1824 (defvar gdb-breakpoints-mode-map
1825 (let ((map (make-sparse-keymap))
1826 (menu (make-sparse-keymap "Breakpoints")))
1827 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1828 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1829 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1830 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1831 (suppress-keymap map)
1832 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1833 (define-key map " " 'gdb-toggle-breakpoint)
1834 (define-key map "D" 'gdb-delete-breakpoint)
1835 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1836 (define-key map "q" 'gdb-delete-frame-or-window)
1837 (define-key map "\r" 'gdb-goto-breakpoint)
1838 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1839 (define-key map [follow-link] 'mouse-face)
1842 (defun gdb-delete-frame-or-window ()
1843 "Delete frame if there is only one window. Otherwise delete the window."
1845 (if (one-window-p) (delete-frame)
1848 ;;from make-mode-line-mouse-map
1849 (defun gdb-make-header-line-mouse-map (mouse function) "\
1850 Return a keymap with single entry for mouse key MOUSE on the header line.
1851 MOUSE is defined to run function FUNCTION with no args in the buffer
1852 corresponding to the mode line clicked."
1853 (let ((map (make-sparse-keymap)))
1854 (define-key map (vector 'header-line mouse) function)
1855 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1858 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
1860 'help-echo ,help-echo
1861 'mouse-face ',mouse-face
1864 (gdb-make-header-line-mouse-map
1866 (lambda (event) (interactive "e")
1867 (save-selected-window
1868 (select-window (posn-window (event-start event)))
1869 (set-window-dedicated-p (selected-window) nil)
1871 (gdb-get-buffer-create ',buffer))
1872 (setq header-line-format(gdb-set-header ',buffer))
1873 (set-window-dedicated-p (selected-window) t))))))
1875 (defun gdb-set-header (buffer)
1876 (cond ((eq buffer 'gdb-locals-buffer)
1878 (gdb-propertize-header "Locals" gdb-locals-buffer
1881 (gdb-propertize-header "Registers" gdb-registers-buffer
1882 "mouse-1: select" mode-line-highlight mode-line-inactive)))
1883 ((eq buffer 'gdb-registers-buffer)
1885 (gdb-propertize-header "Locals" gdb-locals-buffer
1886 "mouse-1: select" mode-line-highlight mode-line-inactive)
1888 (gdb-propertize-header "Registers" gdb-registers-buffer
1889 nil nil mode-line)))
1890 ((eq buffer 'gdb-breakpoints-buffer)
1892 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
1895 (gdb-propertize-header "Threads" gdb-threads-buffer
1896 "mouse-1: select" mode-line-highlight mode-line-inactive)))
1897 ((eq buffer 'gdb-threads-buffer)
1899 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
1900 "mouse-1: select" mode-line-highlight mode-line-inactive)
1902 (gdb-propertize-header "Threads" gdb-threads-buffer
1903 nil nil mode-line)))))
1906 ;; uses "-thread-info". Needs GDB 7.0 onwards.
1909 (defun gdb-jump-to (file line)
1910 (find-file-other-window file)
1913 (define-button-type 'gdb-file-button
1914 'help-echo "Push to jump to source code"
1918 (gdb-jump-to (button-get b 'file)
1919 (button-get b 'line))))
1921 (defun gdb-insert-file-location-button (file line)
1922 "Insert text button which allows jumping to FILE:LINE.
1924 FILE is a full path."
1926 (format "%s:%d" (file-name-nondirectory file) line)
1927 :type 'gdb-file-button
1931 (defun gdb-threads-buffer-name ()
1932 (concat "*threads of " (gdb-get-target-string) "*"))
1934 (def-gdb-display-buffer
1935 gdb-display-threads-buffer
1937 "Display GDB threads.")
1939 (def-gdb-frame-for-buffer
1940 gdb-frame-threads-buffer
1942 "Display GDB threads in a new frame.")
1944 (def-gdb-trigger-and-handler
1945 gdb-invalidate-threads "-thread-info"
1946 gdb-thread-list-handler gdb-thread-list-handler-custom)
1948 (gdb-set-buffer-rules
1950 'gdb-threads-buffer-name
1952 'gdb-invalidate-threads)
1954 (defvar gdb-threads-font-lock-keywords
1955 '(("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1956 (" \\(stopped\\) in " (1 font-lock-warning-face))
1957 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
1958 "Font lock keywords used in `gdb-threads-mode'.")
1960 (defvar gdb-threads-mode-map
1961 (let ((map (make-sparse-keymap)))
1962 (define-key map " " 'gdb-select-thread)
1963 (define-key map "s" 'gdb-display-stack-for-thread)
1964 (define-key map "S" 'gdb-frame-stack-for-thread)
1965 (define-key map "l" 'gdb-display-locals-for-thread)
1966 (define-key map "L" 'gdb-frame-locals-for-thread)
1967 (define-key map "r" 'gdb-display-registers-for-thread)
1968 (define-key map "R" 'gdb-frame-registers-for-thread)
1971 (defvar gdb-breakpoints-header
1973 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
1976 (gdb-propertize-header "Threads" gdb-threads-buffer
1977 "mouse-1: select" mode-line-highlight mode-line-inactive)))
1979 (defun gdb-threads-mode ()
1980 "Major mode for GDB threads.
1982 \\{gdb-threads-mode-map}"
1983 (kill-all-local-variables)
1984 (setq major-mode 'gdb-threads-mode)
1985 (setq mode-name "Threads")
1986 (use-local-map gdb-threads-mode-map)
1987 (setq buffer-read-only t)
1988 (buffer-disable-undo)
1989 (setq gdb-thread-position (make-marker))
1990 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
1991 (setq header-line-format gdb-breakpoints-header)
1992 (set (make-local-variable 'font-lock-defaults)
1993 '(gdb-threads-font-lock-keywords))
1994 (run-mode-hooks 'gdb-threads-mode-hook)
1995 'gdb-invalidate-threads)
1997 (defun gdb-thread-list-handler-custom ()
1998 (let* ((res (json-partial-output))
1999 (threads-list (gdb-get-field res 'threads))
2000 (current-thread (gdb-get-field res 'current-thread-id)))
2001 (when (and current-thread
2002 (not (string-equal current-thread gdb-thread-number)))
2003 ;; Implicitly switch thread (in case previous one dies)
2004 (message (concat "GDB switched to another thread: " current-thread))
2005 (setq gdb-thread-number current-thread))
2006 (set-marker gdb-thread-position nil)
2007 (dolist (thread threads-list)
2008 (insert (apply 'format `("%s (%s) %s in %s "
2009 ,@(gdb-get-many-fields thread 'id 'target-id 'state)
2010 ,(gdb-get-field thread 'frame 'func))))
2013 (let ((args (gdb-get-field thread 'frame 'args)))
2015 (insert (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value)))))
2016 (when args (kill-backward-chars 1)))
2018 (gdb-insert-frame-location (gdb-get-field thread 'frame))
2019 (insert (format " at %s" (gdb-get-field thread 'frame 'addr)))
2020 (add-text-properties (line-beginning-position)
2022 `(gdb-thread ,thread))
2023 (when (string-equal gdb-thread-number
2024 (gdb-get-field thread 'id))
2025 (set-marker gdb-thread-position (line-beginning-position)))
2028 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2029 "Define a NAME command which will act upon thread on the current line.
2031 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2032 be the value of 'gdb-thread propery of the current line. If
2033 'gdb-thread is nil, error is signaled."
2039 (let ((thread (get-text-property (point) 'gdb-thread)))
2042 (error "Not recognized as thread line"))))))
2044 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command &optional doc)
2045 "Define a NAME which will call BUFFER-COMMAND with id of thread
2046 on the current line."
2047 `(def-gdb-thread-buffer-command ,name
2048 (,buffer-command (gdb-get-field thread 'id))
2051 (def-gdb-thread-buffer-command gdb-select-thread
2052 (if (string-equal (gdb-get-field thread 'state) "running")
2053 (error "Cannot select running thread")
2054 (let ((new-id (gdb-get-field thread 'id)))
2055 (setq gdb-thread-number new-id)
2056 (gud-basic-call (concat "-thread-select " new-id))))
2057 "Select the thread at current line of threads buffer.")
2059 (def-gdb-thread-simple-buffer-command
2060 gdb-display-stack-for-thread
2061 gdb-display-stack-buffer
2062 "Display stack buffer for the thread at current line.")
2064 (def-gdb-thread-simple-buffer-command
2065 gdb-display-locals-for-thread
2066 gdb-display-locals-buffer
2067 "Display locals buffer for the thread at current line.")
2069 (def-gdb-thread-simple-buffer-command
2070 gdb-display-registers-for-thread
2071 gdb-display-registers-buffer
2072 "Display registers buffer for the thread at current line.")
2074 (def-gdb-thread-simple-buffer-command
2075 gdb-frame-stack-for-thread
2076 gdb-frame-stack-buffer
2077 "Display a new frame with stack buffer for the thread at
2080 (def-gdb-thread-simple-buffer-command
2081 gdb-frame-locals-for-thread
2082 gdb-frame-locals-buffer
2083 "Display a new frame with locals buffer for the thread at
2086 (def-gdb-thread-simple-buffer-command
2087 gdb-frame-registers-for-thread
2088 gdb-frame-registers-buffer
2089 "Display a new frame with registers buffer for the thread at
2095 (defcustom gdb-memory-rows 8
2096 "Number of data rows in memory window."
2101 (defcustom gdb-memory-columns 4
2102 "Number of data columns in memory window."
2107 (defcustom gdb-memory-format "x"
2108 "Display format of data items in memory window."
2109 :type '(choice (const :tag "Hexadecimal" "x")
2110 (const :tag "Signed decimal" "d")
2111 (const :tag "Unsigned decimal" "u")
2112 (const :tag "Octal" "o")
2113 (const :tag "Binary" "t"))
2117 (defcustom gdb-memory-unit 4
2118 "Unit size of data items in memory window."
2119 :type '(choice (const :tag "Byte" 1)
2120 (const :tag "Halfword" 2)
2121 (const :tag "Word" 4)
2122 (const :tag "Giant word" 8))
2126 (def-gdb-trigger-and-handler
2127 gdb-invalidate-memory
2128 (format "-data-read-memory %s %s %d %d %d"
2134 gdb-read-memory-handler
2135 gdb-read-memory-custom)
2137 (gdb-set-buffer-rules
2139 'gdb-memory-buffer-name
2141 'gdb-invalidate-memory)
2143 (defun gdb-memory-column-width (size format)
2144 "Return length of string with memory unit of SIZE in FORMAT.
2146 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2147 in `gdb-memory-format'."
2148 (let ((format-base (cdr (assoc format
2150 ("d" . 10) ("u" . 10)
2154 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2155 (cond ((string-equal format "x")
2156 (+ 2 res)) ; hexadecimal numbers have 0x in front
2157 ((or (string-equal format "d")
2158 (string-equal format "o"))
2161 (error "Unknown format"))))
2163 (defun gdb-read-memory-custom ()
2164 (let* ((res (json-partial-output))
2165 (err-msg (gdb-get-field res 'msg)))
2167 (let ((memory (gdb-get-field res 'memory)))
2168 (setq gdb-memory-address (gdb-get-field res 'addr))
2169 (setq gdb-memory-next-page (gdb-get-field res 'next-page))
2170 (setq gdb-memory-prev-page (gdb-get-field res 'prev-page))
2171 (setq gdb-memory-last-address gdb-memory-address)
2172 (dolist (row memory)
2173 (insert (concat (gdb-get-field row 'addr) ":"))
2174 (dolist (column (gdb-get-field row 'data))
2175 (insert (gdb-pad-string column
2176 (+ 2 (gdb-memory-column-width
2178 gdb-memory-format)))))
2180 ;; Show last page instead of empty buffer when out of bounds
2182 (let ((gdb-memory-address gdb-memory-last-address))
2183 (gdb-invalidate-memory)
2184 (error err-msg))))))
2186 (defvar gdb-memory-mode-map
2187 (let ((map (make-sparse-keymap)))
2188 (suppress-keymap map t)
2189 (define-key map "q" 'kill-this-buffer)
2190 (define-key map "n" 'gdb-memory-show-next-page)
2191 (define-key map "p" 'gdb-memory-show-previous-page)
2192 (define-key map "a" 'gdb-memory-set-address)
2193 (define-key map "t" 'gdb-memory-format-binary)
2194 (define-key map "o" 'gdb-memory-format-octal)
2195 (define-key map "u" 'gdb-memory-format-unsigned)
2196 (define-key map "d" 'gdb-memory-format-signed)
2197 (define-key map "x" 'gdb-memory-format-hexadecimal)
2198 (define-key map "b" 'gdb-memory-unit-byte)
2199 (define-key map "h" 'gdb-memory-unit-halfword)
2200 (define-key map "w" 'gdb-memory-unit-word)
2201 (define-key map "g" 'gdb-memory-unit-giant)
2202 (define-key map "R" 'gdb-memory-set-rows)
2203 (define-key map "C" 'gdb-memory-set-columns)
2206 (defun gdb-memory-set-address-event (event)
2207 "Handle a click on address field in memory buffer header."
2209 (save-selected-window
2210 (select-window (posn-window (event-start event)))
2211 (gdb-memory-set-address)))
2213 ;; Non-event version for use within keymap
2214 (defun gdb-memory-set-address ()
2215 "Set the start memory address."
2217 (let ((arg (read-from-minibuffer "Memory address: ")))
2218 (setq gdb-memory-address arg))
2219 (gdb-invalidate-memory))
2221 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2222 "Define a function NAME which reads new VAR value from minibuffer."
2223 `(defun ,name (event)
2226 (save-selected-window
2227 (select-window (posn-window (event-start event)))
2228 (let* ((arg (read-from-minibuffer ,echo-string))
2229 (count (string-to-number arg)))
2231 (error "Positive number only")
2232 (customize-set-variable ',variable count)
2233 (gdb-invalidate-memory))))))
2235 (def-gdb-set-positive-number
2239 "Set the number of data rows in memory window.")
2241 (def-gdb-set-positive-number
2242 gdb-memory-set-columns
2245 "Set the number of data columns in memory window.")
2247 (defmacro def-gdb-memory-format (name format doc)
2248 "Define a function NAME to switch memory buffer to use FORMAT.
2250 DOC is an optional documentation string."
2251 `(defun ,name () ,(when doc doc)
2253 (customize-set-variable 'gdb-memory-format ,format)
2254 (gdb-invalidate-memory)))
2256 (def-gdb-memory-format
2257 gdb-memory-format-binary "t"
2258 "Set the display format to binary.")
2260 (def-gdb-memory-format
2261 gdb-memory-format-octal "o"
2262 "Set the display format to octal.")
2264 (def-gdb-memory-format
2265 gdb-memory-format-unsigned "u"
2266 "Set the display format to unsigned decimal.")
2268 (def-gdb-memory-format
2269 gdb-memory-format-signed "d"
2270 "Set the display format to decimal.")
2272 (def-gdb-memory-format
2273 gdb-memory-format-hexadecimal "x"
2274 "Set the display format to hexadecimal.")
2276 (defvar gdb-memory-format-map
2277 (let ((map (make-sparse-keymap)))
2278 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2280 "Keymap to select format in the header line.")
2282 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2283 "Menu of display formats in the header line.")
2285 (define-key gdb-memory-format-menu [binary]
2286 '(menu-item "Binary" gdb-memory-format-binary
2287 :button (:radio . (equal gdb-memory-format "t"))))
2288 (define-key gdb-memory-format-menu [octal]
2289 '(menu-item "Octal" gdb-memory-format-octal
2290 :button (:radio . (equal gdb-memory-format "o"))))
2291 (define-key gdb-memory-format-menu [unsigned]
2292 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2293 :button (:radio . (equal gdb-memory-format "u"))))
2294 (define-key gdb-memory-format-menu [signed]
2295 '(menu-item "Signed Decimal" gdb-memory-format-signed
2296 :button (:radio . (equal gdb-memory-format "d"))))
2297 (define-key gdb-memory-format-menu [hexadecimal]
2298 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2299 :button (:radio . (equal gdb-memory-format "x"))))
2301 (defun gdb-memory-format-menu (event)
2303 (x-popup-menu event gdb-memory-format-menu))
2305 (defun gdb-memory-format-menu-1 (event)
2307 (save-selected-window
2308 (select-window (posn-window (event-start event)))
2309 (let* ((selection (gdb-memory-format-menu event))
2310 (binding (and selection (lookup-key gdb-memory-format-menu
2311 (vector (car selection))))))
2312 (if binding (call-interactively binding)))))
2314 (defmacro def-gdb-memory-unit (name unit-size doc)
2315 "Define a function NAME to switch memory unit size to UNIT-SIZE.
2317 DOC is an optional documentation string."
2318 `(defun ,name () ,(when doc doc)
2320 (customize-set-variable 'gdb-memory-unit ,unit-size)
2321 (gdb-invalidate-memory)))
2323 (def-gdb-memory-unit gdb-memory-unit-giant 8
2324 "Set the unit size to giant words (eight bytes).")
2326 (def-gdb-memory-unit gdb-memory-unit-word 4
2327 "Set the unit size to words (four bytes).")
2329 (def-gdb-memory-unit gdb-memory-unit-halfword 2
2330 "Set the unit size to halfwords (two bytes).")
2332 (def-gdb-memory-unit gdb-memory-unit-byte 1
2333 "Set the unit size to bytes.")
2335 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
2336 "Define a function NAME which show new address in memory buffer.
2338 The defined function switches Memory buffer to show address
2339 stored in ADDRESS-VAR variable.
2341 DOC is an optional documentation string."
2345 (let ((gdb-memory-address ,address-var))
2346 (gdb-invalidate-memory))))
2348 (def-gdb-memory-show-page gdb-memory-show-previous-page
2349 gdb-memory-prev-page)
2351 (def-gdb-memory-show-page gdb-memory-show-next-page
2352 gdb-memory-next-page)
2354 (defvar gdb-memory-unit-map
2355 (let ((map (make-sparse-keymap)))
2356 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2358 "Keymap to select units in the header line.")
2360 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2361 "Menu of units in the header line.")
2363 (define-key gdb-memory-unit-menu [giantwords]
2364 '(menu-item "Giant words" gdb-memory-unit-giant
2365 :button (:radio . (equal gdb-memory-unit 8))))
2366 (define-key gdb-memory-unit-menu [words]
2367 '(menu-item "Words" gdb-memory-unit-word
2368 :button (:radio . (equal gdb-memory-unit 4))))
2369 (define-key gdb-memory-unit-menu [halfwords]
2370 '(menu-item "Halfwords" gdb-memory-unit-halfword
2371 :button (:radio . (equal gdb-memory-unit 2))))
2372 (define-key gdb-memory-unit-menu [bytes]
2373 '(menu-item "Bytes" gdb-memory-unit-byte
2374 :button (:radio . (equal gdb-memory-unit 1))))
2376 (defun gdb-memory-unit-menu (event)
2378 (x-popup-menu event gdb-memory-unit-menu))
2380 (defun gdb-memory-unit-menu-1 (event)
2382 (save-selected-window
2383 (select-window (posn-window (event-start event)))
2384 (let* ((selection (gdb-memory-unit-menu event))
2385 (binding (and selection (lookup-key gdb-memory-unit-menu
2386 (vector (car selection))))))
2387 (if binding (call-interactively binding)))))
2389 (defvar gdb-memory-font-lock-keywords
2390 '(;; <__function.name+n>
2391 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2393 "Font lock keywords used in `gdb-memory-mode'.")
2395 (defvar gdb-memory-header
2400 'face font-lock-warning-face
2401 'help-echo "mouse-1: decrement address"
2402 'mouse-face 'mode-line-highlight
2403 'local-map (gdb-make-header-line-mouse-map
2405 #'gdb-memory-show-previous-page))
2408 'face font-lock-warning-face
2409 'help-echo "mouse-1: increment address"
2410 'mouse-face 'mode-line-highlight
2411 'local-map (gdb-make-header-line-mouse-map
2413 #'gdb-memory-show-next-page))
2415 (propertize gdb-memory-address
2416 'face font-lock-warning-face
2417 'help-echo "mouse-1: set start address"
2418 'mouse-face 'mode-line-highlight
2419 'local-map (gdb-make-header-line-mouse-map
2421 #'gdb-memory-set-address-event))
2423 (propertize (number-to-string gdb-memory-rows)
2424 'face font-lock-warning-face
2425 'help-echo "mouse-1: set number of columns"
2426 'mouse-face 'mode-line-highlight
2427 'local-map (gdb-make-header-line-mouse-map
2429 #'gdb-memory-set-rows))
2431 (propertize (number-to-string gdb-memory-columns)
2432 'face font-lock-warning-face
2433 'help-echo "mouse-1: set number of columns"
2434 'mouse-face 'mode-line-highlight
2435 'local-map (gdb-make-header-line-mouse-map
2437 #'gdb-memory-set-columns))
2439 (propertize gdb-memory-format
2440 'face font-lock-warning-face
2441 'help-echo "mouse-3: select display format"
2442 'mouse-face 'mode-line-highlight
2443 'local-map gdb-memory-format-map)
2445 (propertize (number-to-string gdb-memory-unit)
2446 'face font-lock-warning-face
2447 'help-echo "mouse-3: select unit size"
2448 'mouse-face 'mode-line-highlight
2449 'local-map gdb-memory-unit-map)))
2450 "Header line used in `gdb-memory-mode'.")
2452 (defun gdb-memory-mode ()
2453 "Major mode for examining memory.
2455 \\{gdb-memory-mode-map}"
2456 (kill-all-local-variables)
2457 (setq major-mode 'gdb-memory-mode)
2458 (setq mode-name "Memory")
2459 (use-local-map gdb-memory-mode-map)
2460 (setq buffer-read-only t)
2461 (setq header-line-format gdb-memory-header)
2462 (set (make-local-variable 'font-lock-defaults)
2463 '(gdb-memory-font-lock-keywords))
2464 (run-mode-hooks 'gdb-memory-mode-hook)
2465 'gdb-invalidate-memory)
2467 (defun gdb-memory-buffer-name ()
2468 (concat "*memory of " (gdb-get-target-string) "*"))
2470 (def-gdb-display-buffer
2471 gdb-display-memory-buffer
2473 "Display memory contents.")
2475 (defun gdb-frame-memory-buffer ()
2476 "Display memory contents in a new frame."
2478 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2479 (special-display-frame-alist
2483 ,@gdb-frame-parameters)))
2484 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2487 ;;; Disassembly view
2489 (defun gdb-disassembly-buffer-name ()
2490 (concat "*disassembly of " (gdb-get-target-string) "*"))
2492 (def-gdb-display-buffer
2493 gdb-display-disassembly-buffer
2494 'gdb-disassembly-buffer
2495 "Display disassembly for current stack frame.")
2497 (def-gdb-frame-for-buffer
2498 gdb-frame-disassembly-buffer
2499 'gdb-disassembly-buffer
2500 "Display disassembly in a new frame.")
2502 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
2503 (let ((file (or gdb-selected-file gdb-main-file))
2504 (line (or gdb-selected-line 1)))
2505 (if (not file) (error "Disassembly invalidated with no file selected.")
2506 (format "-data-disassemble -f %s -l %d -n -1 -- 0" file line)))
2507 gdb-disassembly-handler)
2509 (def-gdb-auto-update-handler
2510 gdb-disassembly-handler
2511 gdb-invalidate-disassembly
2512 gdb-disassembly-handler-custom)
2514 (gdb-set-buffer-rules
2515 'gdb-disassembly-buffer
2516 'gdb-disassembly-buffer-name
2517 'gdb-disassembly-mode
2518 'gdb-invalidate-disassembly)
2520 (defvar gdb-disassembly-font-lock-keywords
2521 '(;; <__function.name+n>
2522 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2523 (1 font-lock-function-name-face))
2524 ;; 0xNNNNNNNN <__function.name+n>: opcode
2525 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2526 (4 font-lock-keyword-face))
2527 ;; %register(at least i386)
2528 ("%\\sw+" . font-lock-variable-name-face)
2529 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2530 (1 font-lock-comment-face)
2531 (2 font-lock-function-name-face))
2532 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2533 "Font lock keywords used in `gdb-disassembly-mode'.")
2535 (defvar gdb-disassembly-mode-map
2537 (let ((map (make-sparse-keymap)))
2538 (suppress-keymap map)
2539 (define-key map "q" 'kill-this-buffer)
2542 (defun gdb-disassembly-mode ()
2543 "Major mode for GDB disassembly information.
2545 \\{gdb-disassembly-mode-map}"
2546 (kill-all-local-variables)
2547 (setq major-mode 'gdb-disassembly-mode)
2548 (setq mode-name "Disassembly")
2549 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2550 (setq fringes-outside-margins t)
2551 (setq gdb-overlay-arrow-position (make-marker))
2552 (use-local-map gdb-disassembly-mode-map)
2553 (setq buffer-read-only t)
2554 (buffer-disable-undo)
2555 (set (make-local-variable 'font-lock-defaults)
2556 '(gdb-disassembly-font-lock-keywords))
2557 (run-mode-hooks 'gdb-disassembly-mode-hook)
2558 'gdb-invalidate-disassembly)
2560 (defun gdb-disassembly-handler-custom ()
2561 (let* ((res (json-partial-output))
2562 (instructions (gdb-get-field res 'asm_insns))
2564 (let* ((last-instr (car (last instructions)))
2565 (column-padding (+ 2 (string-width
2568 ,@(gdb-get-many-fields last-instr 'func-name 'offset)))))))
2569 (dolist (instr instructions)
2570 ;; Put overlay arrow
2571 (when (string-equal (gdb-get-field instr 'address)
2575 (setq fringe-indicator-alist
2576 (if (string-equal gdb-frame-number "0")
2578 '((overlay-arrow . hollow-right-triangle))))
2579 (set-marker gdb-overlay-arrow-position (point))))
2582 (gdb-get-field instr 'address)
2584 (gdb-pad-string (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
2586 (gdb-get-field instr 'inst)
2588 (gdb-disassembly-place-breakpoints)
2589 (let ((window (get-buffer-window (current-buffer) 0)))
2590 (set-window-point window pos)))))
2592 (defun gdb-disassembly-place-breakpoints ()
2593 (gdb-remove-breakpoint-icons (point-min) (point-max))
2594 (dolist (breakpoint gdb-breakpoints-list)
2595 (let ((bptno (gdb-get-field breakpoint 'number))
2596 (flag (gdb-get-field breakpoint 'enabled))
2597 (address (gdb-get-field breakpoint 'addr)))
2599 (goto-char (point-min))
2600 (if (re-search-forward (concat "^" address) nil t)
2601 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
2604 ;;; Breakpoints view
2605 (defun gdb-breakpoints-mode ()
2606 "Major mode for gdb breakpoints.
2608 \\{gdb-breakpoints-mode-map}"
2609 (kill-all-local-variables)
2610 (setq major-mode 'gdb-breakpoints-mode)
2611 (setq mode-name "Breakpoints")
2612 (use-local-map gdb-breakpoints-mode-map)
2613 (setq buffer-read-only t)
2614 (buffer-disable-undo)
2615 (setq header-line-format gdb-breakpoints-header)
2616 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2617 'gdb-invalidate-breakpoints)
2619 (defun gdb-toggle-breakpoint ()
2620 "Enable/disable breakpoint at current line of breakpoints buffer."
2624 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
2627 (concat (if (string-equal "y" (gdb-get-field breakpoint 'enabled))
2630 (gdb-get-field breakpoint 'number)))
2631 (error "Not recognized as break/watchpoint line")))))
2633 (defun gdb-delete-breakpoint ()
2634 "Delete the breakpoint at current line of breakpoints buffer."
2638 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
2640 (gud-basic-call (concat "-break-delete " (gdb-get-field breakpoint 'number)))
2641 (error "Not recognized as break/watchpoint line")))))
2643 (defun gdb-goto-breakpoint (&optional event)
2644 "Go to the location of breakpoint at current line of
2645 breakpoints buffer."
2646 (interactive (list last-input-event))
2647 (if event (posn-set-point (event-end event)))
2648 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
2649 (let ((window (get-buffer-window gud-comint-buffer)))
2650 (if window (save-selected-window (select-window window))))
2653 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
2655 (let ((bptno (gdb-get-field breakpoint 'number))
2656 (file (gdb-get-field breakpoint 'fullname))
2657 (line (gdb-get-field breakpoint 'line)))
2658 (save-selected-window
2659 (let* ((buffer (find-file-noselect
2660 (if (file-exists-p file) file
2661 (cdr (assoc bptno gdb-location-alist)))))
2662 (window (or (gdb-display-source-buffer buffer)
2663 (display-buffer buffer))))
2664 (setq gdb-source-window window)
2665 (with-current-buffer buffer
2666 (goto-line (string-to-number line))
2667 (set-window-point window (point))))))
2668 (error "Not recognized as break/watchpoint line")))))
2671 ;; Frames buffer. This displays a perpetually correct bactrack trace.
2673 (def-gdb-trigger-and-handler
2674 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
2675 gdb-stack-list-frames-handler gdb-stack-list-frames-custom)
2677 (gdb-set-buffer-rules
2679 'gdb-stack-buffer-name
2681 'gdb-invalidate-frames)
2683 (defun gdb-insert-frame-location (frame)
2684 "Insert \"of file:line\" button or library name for structure FRAME.
2686 FRAME must have either \"file\" and \"line\" members or \"from\"
2688 (let ((file (gdb-get-field frame 'fullname))
2689 (line (gdb-get-field frame 'line))
2690 (from (gdb-get-field frame 'from)))
2692 ;; Filename with line number
2694 (gdb-insert-file-location-button
2695 file (string-to-number line)))
2697 (from (insert (format " of %s" from))))))
2699 (defun gdb-stack-list-frames-custom ()
2700 (let* ((res (json-partial-output "frame"))
2701 (stack (gdb-get-field res 'stack)))
2702 (dolist (frame (nreverse stack))
2703 (insert (apply 'format `("%s in %s" ,@(gdb-get-many-fields frame 'level 'func))))
2704 (gdb-insert-frame-location frame)
2707 (goto-char (point-min))
2709 (while (< (point) (point-max))
2710 (add-text-properties (point-at-bol) (1+ (point-at-bol))
2711 '(mouse-face highlight
2712 help-echo "mouse-2, RET: Select frame"))
2714 (when (and (looking-at "^[0-9]+\\s-+\\S-+\\s-+\\(\\S-+\\)")
2715 (equal (match-string 1) gdb-selected-frame))
2716 (if (> (car (window-fringes)) 0)
2718 (or gdb-stack-position
2719 (setq gdb-stack-position (make-marker)))
2720 (set-marker gdb-stack-position (point)))
2721 (let ((bl (point-at-bol)))
2722 (put-text-property bl (+ bl 4)
2723 'face '(:inverse-video t)))))
2724 (forward-line 1)))))
2726 (defun gdb-stack-buffer-name ()
2727 (gdb-current-context-buffer-name
2728 (concat "stack frames of " (gdb-get-target-string))))
2730 (def-gdb-display-buffer
2731 gdb-display-stack-buffer
2733 "Display backtrace of current stack.")
2735 (def-gdb-frame-for-buffer
2736 gdb-frame-stack-buffer
2738 "Display backtrace of current stack in a new frame.")
2740 (defvar gdb-frames-mode-map
2741 (let ((map (make-sparse-keymap)))
2742 (suppress-keymap map)
2743 (define-key map "q" 'kill-this-buffer)
2744 (define-key map "\r" 'gdb-frames-select)
2745 (define-key map [mouse-2] 'gdb-frames-select)
2746 (define-key map [follow-link] 'mouse-face)
2749 (defvar gdb-frames-font-lock-keywords
2750 '(("in \\([^ ]+\\) of " (1 font-lock-function-name-face)))
2751 "Font lock keywords used in `gdb-frames-mode'.")
2753 (defun gdb-frames-mode ()
2754 "Major mode for gdb call stack.
2756 \\{gdb-frames-mode-map}"
2757 (kill-all-local-variables)
2758 (setq major-mode 'gdb-frames-mode)
2759 (setq mode-name "Frames")
2760 (setq gdb-stack-position nil)
2761 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2762 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2763 (setq buffer-read-only t)
2764 (buffer-disable-undo)
2765 (use-local-map gdb-frames-mode-map)
2766 (set (make-local-variable 'font-lock-defaults)
2767 '(gdb-frames-font-lock-keywords))
2768 (run-mode-hooks 'gdb-frames-mode-hook)
2769 'gdb-invalidate-frames)
2771 (defun gdb-get-frame-number ()
2774 (let* ((pos (re-search-backward "^\\([0-9]+\\)" nil t))
2775 (n (or (and pos (match-string-no-properties 1)) "0")))
2778 (defun gdb-frames-select (&optional event)
2779 "Select the frame and display the relevant source."
2780 (interactive (list last-input-event))
2781 (if event (posn-set-point (event-end event)))
2782 (gud-basic-call (concat "-stack-select-frame " (gdb-get-frame-number))))
2786 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
2787 (def-gdb-trigger-and-handler
2788 gdb-invalidate-locals
2789 (concat (gdb-current-context-command "-stack-list-locals") " --simple-values")
2790 gdb-locals-handler gdb-locals-handler-custom)
2792 (gdb-set-buffer-rules
2794 'gdb-locals-buffer-name
2796 'gdb-invalidate-locals)
2798 (defvar gdb-locals-watch-map
2799 (let ((map (make-sparse-keymap)))
2800 (suppress-keymap map)
2801 (define-key map "\r" 'gud-watch)
2802 (define-key map [mouse-2] 'gud-watch)
2804 "Keymap to create watch expression of a complex data type local variable.")
2806 (defvar gdb-edit-locals-map-1
2807 (let ((map (make-sparse-keymap)))
2808 (suppress-keymap map)
2809 (define-key map "\r" 'gdb-edit-locals-value)
2810 (define-key map [mouse-2] 'gdb-edit-locals-value)
2812 "Keymap to edit value of a simple data type local variable.")
2814 (defun gdb-edit-locals-value (&optional event)
2815 "Assign a value to a variable displayed in the locals buffer."
2816 (interactive (list last-input-event))
2818 (if event (posn-set-point (event-end event)))
2820 (let* ((var (current-word))
2821 (value (read-string (format "New value (%s): " var))))
2823 (concat "-gdb-set variable " var " = " value)))))
2825 ;; Dont display values of arrays or structures.
2826 ;; These can be expanded using gud-watch.
2827 (defun gdb-locals-handler-custom ()
2828 (let ((locals-list (gdb-get-field (json-partial-output) 'locals)))
2829 (dolist (local locals-list)
2830 (let ((name (gdb-get-field local 'name))
2831 (value (gdb-get-field local 'value))
2832 (type (gdb-get-field local 'type)))
2834 (string-match "\\0x" value))
2835 (add-text-properties 0 (length name)
2836 `(mouse-face highlight
2837 help-echo "mouse-2: create watch expression"
2838 local-map ,gdb-locals-watch-map)
2840 (add-text-properties 0 (length value)
2841 `(mouse-face highlight
2842 help-echo "mouse-2: edit value"
2843 local-map ,gdb-edit-locals-map-1)
2846 (concat name "\t" type
2847 "\t" value "\n"))))))
2849 (defvar gdb-locals-header
2851 (gdb-propertize-header "Locals" gdb-locals-buffer
2854 (gdb-propertize-header "Registers" gdb-registers-buffer
2855 "mouse-1: select" mode-line-highlight mode-line-inactive)))
2857 (defvar gdb-locals-mode-map
2858 (let ((map (make-sparse-keymap)))
2859 (suppress-keymap map)
2860 (define-key map "q" 'kill-this-buffer)
2863 (defun gdb-locals-mode ()
2864 "Major mode for gdb locals.
2866 \\{gdb-locals-mode-map}"
2867 (kill-all-local-variables)
2868 (setq major-mode 'gdb-locals-mode)
2869 (setq mode-name (concat "Locals:" gdb-selected-frame))
2870 (setq buffer-read-only t)
2871 (buffer-disable-undo)
2872 (setq header-line-format gdb-locals-header)
2873 (use-local-map gdb-locals-mode-map)
2874 (set (make-local-variable 'font-lock-defaults)
2875 '(gdb-locals-font-lock-keywords))
2876 (run-mode-hooks 'gdb-locals-mode-hook)
2877 'gdb-invalidate-locals)
2879 (defun gdb-locals-buffer-name ()
2880 (gdb-current-context-buffer-name
2881 (concat "locals of " (gdb-get-target-string))))
2883 (def-gdb-display-buffer
2884 gdb-display-locals-buffer
2886 "Display local variables of current stack and their values.")
2888 (def-gdb-frame-for-buffer
2889 gdb-frame-locals-buffer
2891 "Display local variables of current stack and their values in a new frame.")
2894 ;; Registers buffer.
2896 (def-gdb-trigger-and-handler
2897 gdb-invalidate-registers
2898 (concat (gdb-current-context-command "-data-list-register-values") " x")
2899 gdb-registers-handler
2900 gdb-registers-handler-custom)
2902 (gdb-set-buffer-rules
2903 'gdb-registers-buffer
2904 'gdb-registers-buffer-name
2906 'gdb-invalidate-registers)
2908 (defun gdb-registers-handler-custom ()
2909 (let ((register-values (gdb-get-field (json-partial-output) 'register-values))
2910 (register-names-list (reverse gdb-register-names)))
2911 (dolist (register register-values)
2912 (let* ((register-number (gdb-get-field register 'number))
2913 (value (gdb-get-field register 'value))
2914 (register-name (nth (string-to-number register-number)
2915 register-names-list)))
2918 (propertize register-name 'face font-lock-variable-name-face)
2920 (if (member register-number gdb-changed-registers)
2921 (propertize value 'face font-lock-warning-face)
2925 (defvar gdb-registers-mode-map
2926 (let ((map (make-sparse-keymap)))
2927 (suppress-keymap map)
2928 (define-key map "q" 'kill-this-buffer)
2931 (defun gdb-registers-mode ()
2932 "Major mode for gdb registers.
2934 \\{gdb-registers-mode-map}"
2935 (kill-all-local-variables)
2936 (setq major-mode 'gdb-registers-mode)
2937 (setq mode-name "Registers")
2938 (setq header-line-format gdb-locals-header)
2939 (setq buffer-read-only t)
2940 (buffer-disable-undo)
2941 (use-local-map gdb-registers-mode-map)
2942 (run-mode-hooks 'gdb-registers-mode-hook)
2943 'gdb-invalidate-registers)
2945 (defun gdb-registers-buffer-name ()
2946 (gdb-current-context-buffer-name
2947 (concat "registers of " (gdb-get-target-string))))
2949 (def-gdb-display-buffer
2950 gdb-display-registers-buffer
2951 'gdb-registers-buffer
2952 "Display integer register contents.")
2954 (def-gdb-frame-for-buffer
2955 gdb-frame-registers-buffer
2956 'gdb-registers-buffer
2957 "Display integer register contents in a new frame.")
2959 ;; Needs GDB 6.4 onwards (used to fail with no stack).
2960 (defun gdb-get-changed-registers ()
2961 (if (and (gdb-get-buffer 'gdb-registers-buffer)
2962 (not (gdb-pending-p 'gdb-get-changed-registers)))
2966 "-data-list-changed-registers"
2967 'gdb-changed-registers-handler))
2968 (gdb-add-pending 'gdb-get-changed-registers))))
2970 (defun gdb-changed-registers-handler ()
2971 (gdb-delete-pending 'gdb-get-changed-registers)
2972 (setq gdb-changed-registers nil)
2973 (dolist (register-number (gdb-get-field (json-partial-output) 'changed-registers))
2974 (push register-number gdb-changed-registers)))
2976 (defun gdb-register-names-handler ()
2977 ;; Don't use gdb-pending-triggers because this handler is called
2978 ;; only once (in gdb-init-1)
2979 (setq gdb-register-names nil)
2980 (dolist (register-name (gdb-get-field (json-partial-output) 'register-names))
2981 (push register-name gdb-register-names))
2982 (setq gdb-register-names (reverse gdb-register-names)))
2985 (defun gdb-get-source-file-list ()
2986 "Create list of source files for current GDB session.
2987 If buffers already exist for any of these files, gud-minor-mode
2989 (goto-char (point-min))
2990 (while (re-search-forward gdb-source-file-regexp nil t)
2991 (push (match-string 1) gdb-source-file-list))
2992 (dolist (buffer (buffer-list))
2993 (with-current-buffer buffer
2994 (when (member buffer-file-name gdb-source-file-list)
2995 (gdb-init-buffer))))
2996 (gdb-force-mode-line-update
2997 (propertize "ready" 'face font-lock-variable-name-face)))
2999 (defun gdb-get-selected-frame ()
3000 (if (not (gdb-pending-p 'gdb-get-selected-frame))
3003 (list (gdb-current-context-command "-stack-info-frame") 'gdb-frame-handler))
3004 (push 'gdb-get-selected-frame
3005 gdb-pending-triggers))))
3007 (defun gdb-frame-handler ()
3008 (gdb-delete-pending 'gdb-get-selected-frame)
3009 (let ((frame (gdb-get-field (json-partial-output) 'frame)))
3011 (setq gdb-frame-number (gdb-get-field frame 'level))
3012 (setq gdb-pc-address (gdb-get-field frame 'addr))
3013 (setq gdb-selected-frame (gdb-get-field frame 'func))
3014 (setq gdb-selected-file (gdb-get-field frame 'fullname))
3015 (let ((line (gdb-get-field frame 'line)))
3016 (setq gdb-selected-line (or (and line (string-to-number line))
3017 nil)) ; don't fail if line is nil
3018 (when line ; obey the current file only if we have line info
3019 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3020 (gud-display-frame)))
3021 (if (gdb-get-buffer 'gdb-locals-buffer)
3022 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3023 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3024 (if (gdb-get-buffer 'gdb-disassembly-buffer)
3025 (with-current-buffer (gdb-get-buffer 'gdb-disassembly-buffer)
3026 (setq mode-name (concat "Disassembly:" gdb-selected-frame))))
3027 (if gud-overlay-arrow-position
3028 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3029 (position (marker-position gud-overlay-arrow-position)))
3031 (with-current-buffer buffer
3032 (setq fringe-indicator-alist
3033 (if (string-equal gdb-frame-number "0")
3035 '((overlay-arrow . hollow-right-triangle))))
3036 (setq gud-overlay-arrow-position (make-marker))
3037 (set-marker gud-overlay-arrow-position position)))))
3038 (when gdb-selected-line
3039 (gdb-invalidate-disassembly)))))
3041 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3043 (defun gdb-get-prompt ()
3044 "Find prompt for GDB session."
3045 (goto-char (point-min))
3046 (setq gdb-prompt-name nil)
3047 (re-search-forward gdb-prompt-name-regexp nil t)
3048 (setq gdb-prompt-name (match-string 1))
3049 ;; Insert first prompt.
3050 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3052 ;;;; Window management
3053 (defun gdb-display-buffer (buf dedicated &optional frame)
3054 (let ((answer (get-buffer-window buf (or frame 0))))
3056 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3057 (let ((window (get-lru-window)))
3058 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3060 (let* ((largest (get-largest-window))
3061 (cur-size (window-height largest)))
3062 (setq answer (split-window largest))
3063 (set-window-buffer answer buf)
3064 (set-window-dedicated-p answer dedicated)
3066 (set-window-buffer window buf)
3070 ;;; Shared keymap initialization:
3072 (let ((menu (make-sparse-keymap "GDB-Windows")))
3073 (define-key gud-menu-map [displays]
3074 `(menu-item "GDB-Windows" ,menu
3075 :visible (eq gud-minor-mode 'gdbmi)))
3076 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3077 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3078 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3079 (define-key menu [disassembly]
3080 '("Disassembly" . gdb-display-disassembly-buffer))
3081 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3082 (define-key menu [inferior]
3083 '("Separate IO" . gdb-display-separate-io-buffer))
3084 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3085 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3086 (define-key menu [breakpoints]
3087 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3089 (let ((menu (make-sparse-keymap "GDB-Frames")))
3090 (define-key gud-menu-map [frames]
3091 `(menu-item "GDB-Frames" ,menu
3092 :visible (eq gud-minor-mode 'gdbmi)))
3093 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3094 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3095 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3096 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
3097 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3098 (define-key menu [inferior]
3099 '("Separate IO" . gdb-frame-separate-io-buffer))
3100 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3101 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3102 (define-key menu [breakpoints]
3103 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3105 (let ((menu (make-sparse-keymap "GDB-MI")))
3106 (define-key gud-menu-map [mi]
3107 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi)))
3108 (define-key menu [gdb-customize]
3109 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3110 :help "Customize Gdb Graphical Mode options."))
3111 (define-key menu [gdb-many-windows]
3112 '(menu-item "Display Other Windows" gdb-many-windows
3113 :help "Toggle display of locals, stack and breakpoint information"
3114 :button (:toggle . gdb-many-windows)))
3115 (define-key menu [gdb-restore-windows]
3116 '(menu-item "Restore Window Layout" gdb-restore-windows
3117 :help "Restore standard layout for debug session.")))
3119 (defun gdb-frame-gdb-buffer ()
3120 "Display GUD buffer in a new frame."
3122 (let ((special-display-regexps (append special-display-regexps '(".*")))
3123 (special-display-frame-alist
3124 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3125 gdb-frame-parameters)))
3126 (same-window-regexps nil))
3127 (display-buffer gud-comint-buffer)))
3129 (defun gdb-display-gdb-buffer ()
3130 "Display GUD buffer."
3132 (let ((same-window-regexps nil))
3133 (select-window (display-buffer gud-comint-buffer nil 0))))
3135 (defun gdb-set-window-buffer (name)
3136 (set-window-buffer (selected-window) (get-buffer name))
3137 (set-window-dedicated-p (selected-window) t))
3139 (defun gdb-setup-windows ()
3140 "Layout the window pattern for `gdb-many-windows'."
3141 (gdb-display-locals-buffer)
3142 (gdb-display-stack-buffer)
3143 (delete-other-windows)
3144 (gdb-display-breakpoints-buffer)
3145 (delete-other-windows)
3147 (pop-to-buffer gud-comint-buffer)
3148 (split-window nil ( / ( * (window-height) 3) 4))
3149 (split-window nil ( / (window-height) 3))
3150 (split-window-horizontally)
3152 (gdb-set-window-buffer (gdb-locals-buffer-name))
3155 (if gud-last-last-frame
3156 (gud-find-file (car gud-last-last-frame))
3158 (gud-find-file gdb-main-file)
3159 ;; Put buffer list in window if we
3160 ;; can't find a source file.
3161 (list-buffers-noselect))))
3162 (setq gdb-source-window (selected-window))
3163 (when gdb-use-separate-io-buffer
3164 (split-window-horizontally)
3166 (gdb-set-window-buffer
3167 (gdb-get-buffer-create 'gdb-inferior-io)))
3169 (gdb-set-window-buffer (gdb-stack-buffer-name))
3170 (split-window-horizontally)
3172 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3175 (defcustom gdb-many-windows nil
3176 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
3177 In this case it starts with two windows: one displaying the GUD
3178 buffer and the other with the source file with the main routine
3179 of the debugged program. Non-nil means display the layout shown for
3185 (defun gdb-many-windows (arg)
3186 "Toggle the number of windows in the basic arrangement.
3187 With arg, display additional buffers iff arg is positive."
3189 (setq gdb-many-windows
3191 (not gdb-many-windows)
3192 (> (prefix-numeric-value arg) 0)))
3193 (message (format "Display of other windows %sabled"
3194 (if gdb-many-windows "en" "dis")))
3195 (if (and gud-comint-buffer
3196 (buffer-name gud-comint-buffer))
3198 (gdb-restore-windows)
3201 (defun gdb-restore-windows ()
3202 "Restore the basic arrangement of windows used by gdb.
3203 This arrangement depends on the value of `gdb-many-windows'."
3205 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3206 (delete-other-windows)
3207 (if gdb-many-windows
3209 (when (or gud-last-last-frame gdb-show-main)
3213 (if gud-last-last-frame
3214 (gud-find-file (car gud-last-last-frame))
3215 (gud-find-file gdb-main-file)))
3216 (setq gdb-source-window (selected-window))
3220 "Exit a debugging session cleanly.
3221 Kills the gdb buffers, and resets variables and the source buffers."
3222 (dolist (buffer (buffer-list))
3223 (unless (eq buffer gud-comint-buffer)
3224 (with-current-buffer buffer
3225 (if (eq gud-minor-mode 'gdbmi)
3226 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3228 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3229 (setq gud-minor-mode nil)
3230 (kill-local-variable 'tool-bar-map)
3231 (kill-local-variable 'gdb-define-alist))))))
3232 (setq gdb-overlay-arrow-position nil)
3233 (setq overlay-arrow-variable-list
3234 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3235 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3236 (setq gdb-stack-position nil)
3237 (setq overlay-arrow-variable-list
3238 (delq 'gdb-stack-position overlay-arrow-variable-list))
3239 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3240 (setq gud-running nil)
3241 (setq gdb-active-process nil)
3242 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3244 (defun gdb-get-source-file ()
3245 "Find the source file where the program starts and display it with related
3246 buffers, if required."
3247 (goto-char (point-min))
3248 (if (re-search-forward gdb-source-file-regexp nil t)
3249 (setq gdb-main-file (match-string 1)))
3250 (if gdb-many-windows
3252 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3254 (let ((pop-up-windows t))
3255 (display-buffer (gud-find-file gdb-main-file))))))
3258 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3259 "Put string PUTSTRING in front of POS in the current buffer.
3260 PUTSTRING is displayed by putting an overlay into the current buffer with a
3261 `before-string' string that has a `display' property whose value is
3263 (let ((string (make-string 1 ?x))
3264 (buffer (current-buffer)))
3265 (setq putstring (copy-sequence putstring))
3266 (let ((overlay (make-overlay pos pos buffer))
3268 (list (list 'margin 'left-margin) putstring))))
3269 (put-text-property 0 1 'display prop string)
3271 (add-text-properties 0 1 sprops string))
3272 (overlay-put overlay 'put-break t)
3273 (overlay-put overlay 'before-string string))))
3275 ;;from remove-images
3276 (defun gdb-remove-strings (start end &optional buffer)
3277 "Remove strings between START and END in BUFFER.
3278 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3279 BUFFER nil or omitted means use the current buffer."
3281 (setq buffer (current-buffer)))
3282 (dolist (overlay (overlays-in start end))
3283 (when (overlay-get overlay 'put-break)
3284 (delete-overlay overlay))))
3286 (defun gdb-put-breakpoint-icon (enabled bptno)
3287 (let ((start (- (line-beginning-position) 1))
3288 (end (+ (line-end-position) 1))
3289 (putstring (if enabled "B" "b"))
3290 (source-window (get-buffer-window (current-buffer) 0)))
3291 (add-text-properties
3292 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3295 (add-text-properties
3296 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3297 (add-text-properties
3298 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3299 (gdb-remove-breakpoint-icons start end)
3300 (if (display-images-p)
3301 (if (>= (or left-fringe-width
3302 (if source-window (car (window-fringes source-window)))
3303 gdb-buffer-fringe-width) 8)
3306 `(left-fringe breakpoint
3309 'breakpoint-disabled))
3311 'gdb-enabled enabled)
3312 (when (< left-margin-width 2)
3313 (save-current-buffer
3314 (setq left-margin-width 2)
3318 left-margin-width right-margin-width))))
3321 (or breakpoint-enabled-icon
3322 (setq breakpoint-enabled-icon
3323 (find-image `((:type xpm :data
3324 ,breakpoint-xpm-data
3325 :ascent 100 :pointer hand)
3327 ,breakpoint-enabled-pbm-data
3328 :ascent 100 :pointer hand)))))
3329 (or breakpoint-disabled-icon
3330 (setq breakpoint-disabled-icon
3331 (find-image `((:type xpm :data
3332 ,breakpoint-xpm-data
3333 :conversion disabled
3334 :ascent 100 :pointer hand)
3336 ,breakpoint-disabled-pbm-data
3337 :ascent 100 :pointer hand))))))
3341 (when (< left-margin-width 2)
3342 (save-current-buffer
3343 (setq left-margin-width 2)
3344 (let ((window (get-buffer-window (current-buffer) 0)))
3347 window left-margin-width right-margin-width)))))
3349 (propertize putstring
3350 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3353 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3354 (gdb-remove-strings start end)
3355 (if (display-images-p)
3356 (remove-images start end))
3358 (setq left-margin-width 0)
3359 (let ((window (get-buffer-window (current-buffer) 0)))
3362 window left-margin-width right-margin-width)))))
3366 ;; arch-tag: 1b41ea2b-f364-4cec-8f35-e02e4fe01912
3367 ;;; gdb-mi.el ends here