1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
8 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This mode acts as a graphical user interface to GDB. You can interact with
30 ;; GDB through the GUD buffer in the usual way, but there are also further
31 ;; buffers which control the execution and describe the state of your program.
32 ;; It separates the input/output of your program from that of GDB, if
33 ;; required, and watches expressions in the speedbar. It also uses features of
34 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
35 ;; (see the GDB Graphical Interface section in the Emacs info manual).
37 ;; By default, M-x gdb will start the debugger.
39 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
40 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
41 ;; You don't need to know about annotations to use this mode as a debugger,
42 ;; but if you are interested developing the mode itself, see the Annotations
43 ;; section in the GDB info manual.
45 ;; GDB developers plan to make the annotation interface obsolete. A new
46 ;; interface called GDB/MI (machine interface) has been designed to replace it.
47 ;; Some GDB/MI commands are used in this file through the CLI command
48 ;; 'interpreter mi <mi-command>'. To help with the process of fully migrating
49 ;; Emacs from annotations to GDB/MI, there is an experimental package called
50 ;; gdb-mi in the Emacs Lisp Package Archive ("http://tromey.com/elpa/"). It
51 ;; comprises of modified gud.el and a file called gdb-mi.el which replaces
52 ;; gdb-ui.el. When installed, this overrides the current files and invoking
53 ;; M-x gdb will use GDB/MI directly (starts with "gdb -i=mi"). When deleted
54 ;; ('d' followed by 'x' in Package Menu mode), the files are deleted and old
55 ;; functionality restored. This provides a convenient way to review the
56 ;; current status/contribute to its improvement. For someone who just wants to
57 ;; use GDB, however, the current mode in Emacs 22 is a much better option.
58 ;; There is also a file, also called gdb-mi.el, a version of which is included
59 ;; the GDB distribution. This will probably only work with versions
60 ;; distributed with GDB 6.5 or later. Unlike the version in ELPA it works on
61 ;; top of gdb-ui.el and you can only start it with M-x gdbmi.
63 ;; This mode SHOULD WORK WITH GDB 5.0 or later but you will NEED AT LEAST
64 ;; GDB 6.0 to use watch expressions. It works best with GDB 6.4 or later
65 ;; where watch expressions will update more quickly.
67 ;;; Windows Platforms:
69 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
70 ;; explicitly in your program if you want timely display of I/O in Emacs.
71 ;; Alternatively you can make the output stream unbuffered, for example, by
75 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
78 ;; and compiling with -DUNBUFFERED while debugging.
82 ;; 1) Cannot handle multiple debug sessions.
83 ;; 2) If you wish to call procedures from your program in GDB
84 ;; e.g "call myproc ()", "p mysquare (5)" then use level 2 annotations
85 ;; "gdb --annotate=2 myprog" to keep source buffer/selected frame fixed.
86 ;; 3) After detaching from a process, clicking on the "GO" icon on toolbar
87 ;; (gud-go) sends "continue" to GDB (should be "run").
91 ;; 1) Use MI command -data-read-memory for memory window.
92 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
93 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
100 (defvar speedbar-initial-expansion-list-name
)
102 (defvar gdb-pc-address nil
"Initialization for Assembler buffer.
103 Set to \"main\" at start if gdb-show-main is t.")
104 (defvar gdb-frame-address nil
"Identity of frame for watch expression.")
105 (defvar gdb-previous-frame-address nil
)
106 (defvar gdb-memory-address
"main")
107 (defvar gdb-previous-frame nil
)
108 (defvar gdb-selected-frame nil
)
109 (defvar gdb-frame-number nil
)
110 (defvar gdb-current-language nil
)
111 (defvar gdb-var-list nil
112 "List of variables in watch window.
113 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS FP)
114 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
115 address for root variables.")
116 (defvar gdb-main-file nil
"Source file from which program execution begins.")
117 (defvar gud-old-arrow nil
)
118 (defvar gdb-overlay-arrow-position nil
)
119 (defvar gdb-stack-position nil
)
120 (defvar gdb-server-prefix nil
)
121 (defvar gdb-flush-pending-output nil
)
122 (defvar gdb-location-alist nil
123 "Alist of breakpoint numbers and full filenames. Only used for files that
125 (defvar gdb-active-process nil
126 "GUD tooltips display variable values when t, and macro definitions otherwise.")
127 (defvar gdb-error
"Non-nil when GDB is reporting an error.")
128 (defvar gdb-macro-info nil
129 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
130 (defvar gdb-buffer-fringe-width nil
)
131 (defvar gdb-signalled nil
)
132 (defvar gdb-source-window nil
)
133 (defvar gdb-inferior-status nil
)
134 (defvar gdb-continuation nil
)
135 (defvar gdb-look-up-stack nil
)
136 (defvar gdb-frame-begin nil
137 "Non-nil when GDB generates frame-begin annotation.")
138 (defvar gdb-printing t
)
139 (defvar gdb-parent-bptno-enabled nil
)
141 (defvar gdb-buffer-type nil
142 "One of the symbols bound in `gdb-buffer-rules'.")
143 (make-variable-buffer-local 'gdb-buffer-type
)
145 (defvar gdb-input-queue
()
146 "A list of gdb command objects.")
148 (defvar gdb-prompting nil
149 "True when gdb is idle with no pending input.")
151 (defvar gdb-output-sink
'user
152 "The disposition of the output of the current gdb command.
153 Possible values are these symbols:
155 `user' -- gdb output should be copied to the GUD buffer
158 `inferior' -- gdb output should be copied to the inferior-io buffer.
160 `pre-emacs' -- output should be ignored util the post-prompt
161 annotation is received. Then the output-sink
163 `emacs' -- output should be collected in the partial-output-buffer
164 for subsequent processing by a command. This is the
165 disposition of output generated by commands that
166 gdb mode sends to gdb on its own behalf.
167 `post-emacs' -- ignore output until the prompt annotation is
168 received, then go to USER disposition.
170 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
171 \(`user' and `emacs').")
173 (defvar gdb-current-item nil
174 "The most recent command item sent to gdb.")
176 (defvar gdb-pending-triggers
'()
177 "A list of trigger functions that have run later than their output
180 (defvar gdb-first-post-prompt nil
)
181 (defvar gdb-version nil
)
182 (defvar gdb-locals-font-lock-keywords nil
)
183 (defvar gdb-source-file-list nil
184 "List of source files for the current executable")
185 (defconst gdb-error-regexp
"\\^error,msg=\"\\(.+\\)\"")
187 (defvar gdb-locals-font-lock-keywords-1
189 ;; var = (struct struct_tag) value
190 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
191 (1 font-lock-variable-name-face
)
192 (3 font-lock-keyword-face
)
193 (4 font-lock-type-face
))
194 ;; var = (type) value
195 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
196 (1 font-lock-variable-name-face
)
197 (3 font-lock-type-face
))
199 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
200 (1 font-lock-variable-name-face
))
202 "Font lock keywords used in `gdb-local-mode'.")
204 (defvar gdb-locals-font-lock-keywords-2
207 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
208 (1 font-lock-variable-name-face
)
209 (3 font-lock-type-face
))
211 "Font lock keywords used in `gdb-local-mode'.")
213 ;; Variables for GDB 6.4+
214 (defvar gdb-register-names nil
"List of register names.")
215 (defvar gdb-changed-registers nil
216 "List of changed register numbers (strings).")
219 (defun gdb (command-line)
220 "Run gdb on program FILE in buffer *gud-FILE*.
221 The directory containing FILE becomes the initial working
222 directory and source-file directory for your debugger.
224 If `gdb-many-windows' is nil (the default value) then gdb just
225 pops up the GUD buffer unless `gdb-show-main' is t. In this case
226 it starts with two windows: one displaying the GUD buffer and the
227 other with the source file with the main routine of the inferior.
229 If `gdb-many-windows' is t, regardless of the value of
230 `gdb-show-main', the layout below will appear unless
231 `gdb-use-separate-io-buffer' is nil when the source buffer
232 occupies the full width of the frame. Keybindings are shown in
235 Watch expressions appear in the speedbar/slowbar.
237 The following commands help control operation :
239 `gdb-many-windows' - Toggle the number of windows gdb uses.
240 `gdb-restore-windows' - To restore the window layout.
242 See Info node `(emacs)GDB Graphical Interface' for a more
243 detailed description of this mode.
246 +----------------------------------------------------------------------+
248 +-----------------------------------+----------------------------------+
249 | GUD buffer (I/O of GDB) | Locals buffer |
253 +-----------------------------------+----------------------------------+
254 | Source buffer | I/O buffer (of debugged program) |
262 +-----------------------------------+----------------------------------+
263 | Stack buffer | Breakpoints buffer |
264 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
265 | | RET gdb-goto-breakpoint |
266 | | D gdb-delete-breakpoint |
267 +-----------------------------------+----------------------------------+
269 To run GDB in text command mode, replace the GDB \"--annotate=3\"
270 option with \"--fullname\" either in the minibuffer for the
271 current Emacs session, or the custom variable
272 `gud-gdb-command-name' for all future sessions. You need to use
273 text command mode to debug multiple programs within one Emacs
275 (interactive (list (gud-query-cmdline 'gdb
)))
277 (when (and gud-comint-buffer
278 (buffer-name gud-comint-buffer
)
279 (get-buffer-process gud-comint-buffer
)
280 (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
)))
281 (gdb-restore-windows)
283 "Multiple debugging requires restarting in text command mode"))
285 (gud-common-init command-line nil
'gud-gdba-marker-filter
)
286 (set (make-local-variable 'gud-minor-mode
) 'gdba
)
288 (gud-def gud-break
"break %f:%l" "\C-b" "Set breakpoint at current line.")
289 (gud-def gud-tbreak
"tbreak %f:%l" "\C-t"
290 "Set temporary breakpoint at current line.")
291 (gud-def gud-remove
"clear %f:%l" "\C-d" "Remove breakpoint at current line")
292 (gud-def gud-step
"step %p" "\C-s" "Step one source line with display.")
293 (gud-def gud-stepi
"stepi %p" "\C-i" "Step one instruction with display.")
294 (gud-def gud-next
"next %p" "\C-n" "Step one line (skip functions).")
295 (gud-def gud-nexti
"nexti %p" nil
"Step one instruction (skip functions).")
296 (gud-def gud-cont
"cont" "\C-r" "Continue with display.")
297 (gud-def gud-finish
"finish" "\C-f" "Finish executing current function.")
299 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
300 "\C-j" "Set execution address to current line.")
302 (gud-def gud-up
"up %p" "<" "Up N stack frames (numeric arg).")
303 (gud-def gud-down
"down %p" ">" "Down N stack frames (numeric arg).")
304 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
305 (gud-def gud-pstar
"print* %e" nil
306 "Evaluate C dereferenced pointer expression at point.")
308 ;; For debugging Emacs only.
309 (gud-def gud-pv
"pv1 %e" "\C-v" "Print the value of the lisp variable.")
311 (gud-def gud-until
"until %l" "\C-u" "Continue to current line.")
312 (gud-def gud-run
"run" nil
"Run the program.")
314 (local-set-key "\C-i" 'gud-gdb-complete-command
)
315 (setq comint-prompt-regexp
"^(.*gdb[+]?) *")
316 (setq paragraph-start comint-prompt-regexp
)
317 (setq gdb-first-prompt t
)
318 (setq gud-running nil
)
320 (setq gud-filter-pending-text nil
)
321 (run-hooks 'gdb-mode-hook
))
323 (defcustom gdb-debug-log-max
128
324 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
326 :type
'(choice (integer :tag
"Number of elements")
327 (const :tag
"Unlimited" nil
))
330 (defvar gdb-debug-log nil
331 "List of commands sent to and replies received from GDB. Most
332 recent commands are listed first. This list stores only the last
333 'gdb-debug-log-max' values. This variable is used to debug
337 (defcustom gdb-enable-debug nil
338 "Non-nil means record the process input and output in `gdb-debug-log'."
343 (defcustom gdb-cpp-define-alist-program
"gcc -E -dM -"
344 "Shell command for generating a list of defined macros in a source file.
345 This list is used to display the #define directive associated
346 with an identifier as a tooltip. It works in a debug session with
347 GDB, when gud-tooltip-mode is t.
349 Set `gdb-cpp-define-alist-flags' for any include paths or
355 (defcustom gdb-cpp-define-alist-flags
""
356 "Preprocessor flags for `gdb-cpp-define-alist-program'."
361 (defcustom gdb-show-main nil
362 "Non-nil means display source file containing the main routine at startup.
363 Also display the main routine in the disassembly buffer if present."
368 (defcustom gdb-many-windows nil
369 "If nil, just pop up the GUD buffer unless `gdb-show-main' is t.
370 In this case start with two windows: one displaying the GUD
371 buffer and the other with the source file with the main routine
372 of the debugged program. Non-nil means display the layout shown
378 (defcustom gdb-use-separate-io-buffer nil
379 "Non-nil means display output from the debugged program in a separate buffer."
384 (defun gdb-force-mode-line-update (status)
385 (let ((buffer gud-comint-buffer
))
386 (if (and buffer
(buffer-name buffer
))
387 (with-current-buffer buffer
388 (setq mode-line-process
390 (process-status (get-buffer-process buffer
)) status
))
391 ;; Force mode line redisplay soon.
392 (force-mode-line-update)))))
394 (defun gdb-many-windows (arg)
395 "Toggle the number of windows in the basic arrangement.
396 With prefix argument ARG, display additional buffers if ARG is positive,
397 otherwise use a single window."
399 (setq gdb-many-windows
401 (not gdb-many-windows
)
402 (> (prefix-numeric-value arg
) 0)))
403 (message (format "Display of other windows %sabled"
404 (if gdb-many-windows
"en" "dis")))
405 (if (and gud-comint-buffer
406 (buffer-name gud-comint-buffer
))
408 (gdb-restore-windows)
411 (defun gdb-use-separate-io-buffer (arg)
412 "Toggle separate IO for debugged program.
413 With prefix argument ARG, use separate IO if ARG is positive,
416 (setq gdb-use-separate-io-buffer
418 (not gdb-use-separate-io-buffer
)
419 (> (prefix-numeric-value arg
) 0)))
420 (message (format "Separate IO %sabled"
421 (if gdb-use-separate-io-buffer
"en" "dis")))
422 (if (and gud-comint-buffer
423 (buffer-name gud-comint-buffer
))
425 (if gdb-use-separate-io-buffer
426 (if gdb-many-windows
(gdb-restore-windows))
427 (kill-buffer (gdb-inferior-io-name)))
430 (defvar gdb-define-alist nil
"Alist of #define directives for GUD tooltips.")
432 (defun gdb-create-define-alist ()
433 "Create an alist of #define directives for GUD tooltips."
434 (let* ((file (buffer-file-name))
436 (with-output-to-string
437 (with-current-buffer standard-output
438 (and file
(file-exists-p file
)
439 (call-process shell-file-name file
440 (list t nil
) nil
"-c"
441 (concat gdb-cpp-define-alist-program
" "
442 gdb-cpp-define-alist-flags
))))))
443 (define-list (split-string output
"\n" t
)) (name))
444 (setq gdb-define-alist nil
)
445 (dolist (define define-list
)
446 (setq name
(nth 1 (split-string define
"[( ]")))
447 (push (cons name define
) gdb-define-alist
))))
449 (defun gdb-tooltip-print (expr)
451 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
452 (goto-char (point-min))
454 (if (search-forward "=" nil t
)
455 (concat expr
(buffer-substring (- (point) 2) (point-max)))
457 ;; remove newline for gud-tooltip-echo-area
458 (substring string
0 (- (length string
) 1))))
459 (or gud-tooltip-echo-area tooltip-use-echo-area
)))
461 ;; If expr is a macro for a function don't print because of possible dangerous
462 ;; side-effects. Also printing a function within a tooltip generates an
463 ;; unexpected starting annotation (phase error).
464 (defun gdb-tooltip-print-1 (expr)
465 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
466 (goto-char (point-min))
467 (if (search-forward "expands to: " nil t
)
468 (unless (looking-at "\\S-+.*(.*).*")
470 (list (concat gdb-server-prefix
"print " expr
"\n")
471 `(lambda () (gdb-tooltip-print ,expr
))))))))
473 (defconst gdb-source-file-regexp
"\\(.+?\\), \\|\\([^, \n].*$\\)")
475 (defun gdb-set-gud-minor-mode-existing-buffers ()
476 "Create list of source files for current GDB session."
477 (goto-char (point-min))
478 (when (search-forward "read in on demand:" nil t
)
479 (while (re-search-forward gdb-source-file-regexp nil t
)
480 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
481 gdb-source-file-list
))
482 (dolist (buffer (buffer-list))
483 (with-current-buffer buffer
484 (when (and buffer-file-name
485 (member (file-name-nondirectory buffer-file-name
)
486 gdb-source-file-list
))
487 (set (make-local-variable 'gud-minor-mode
) 'gdba
)
488 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
489 (when gud-tooltip-mode
490 (make-local-variable 'gdb-define-alist
)
491 (gdb-create-define-alist)
492 (add-hook 'after-save-hook
'gdb-create-define-alist nil t
))))))
493 (gdb-force-mode-line-update
494 (propertize "ready" 'face font-lock-variable-name-face
)))
496 (defun gdb-find-watch-expression ()
497 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list
))
498 (varnum (car var
)) expr array
)
499 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum
)
500 (let ((var1 (assoc (match-string 1 varnum
) gdb-var-list
)) var2 varnumlet
501 (component-list (split-string (match-string 2 varnum
) "\\." t
)))
502 (setq expr
(nth 1 var1
))
503 (setq varnumlet
(car var1
))
504 (dolist (component component-list
)
505 (setq var2
(assoc varnumlet gdb-var-list
))
506 (setq expr
(concat expr
507 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2
))
508 (concat "[" component
"]")
509 (concat "." component
))))
510 (setq varnumlet
(concat varnumlet
"." component
)))
514 (gud-def gud-break
(if (not (string-match "Machine" mode-name
))
515 (gud-call "break %f:%l" arg
)
519 (gud-call "break *%a" arg
)))
520 "\C-b" "Set breakpoint at current line or address.")
522 (gud-def gud-remove
(if (not (string-match "Machine" mode-name
))
523 (gud-call "clear %f:%l" arg
)
527 (gud-call "clear *%a" arg
)))
528 "\C-d" "Remove breakpoint at current line or address.")
530 (gud-def gud-until
(if (not (string-match "Machine" mode-name
))
531 (gud-call "until %f:%l" arg
)
535 (gud-call "until *%a" arg
)))
536 "\C-u" "Continue to current line or address.")
538 (gud-def gud-go
(gud-call (if gdb-active-process
"continue" "run") arg
)
539 nil
"Start or continue execution.")
541 ;; For debugging Emacs only.
545 "pp1 " (if (eq (buffer-local-value
546 'major-mode
(window-buffer)) 'speedbar-mode
)
547 (gdb-find-watch-expression) "%e")) arg
)
548 nil
"Print the emacs s-expression.")
550 (define-key gud-minor-mode-map
[left-margin mouse-1
]
551 'gdb-mouse-set-clear-breakpoint
)
552 (define-key gud-minor-mode-map
[left-fringe mouse-1
]
553 'gdb-mouse-set-clear-breakpoint
)
554 (define-key gud-minor-mode-map
[left-margin C-mouse-1
]
555 'gdb-mouse-toggle-breakpoint-margin
)
556 (define-key gud-minor-mode-map
[left-fringe C-mouse-1
]
557 'gdb-mouse-toggle-breakpoint-fringe
)
559 (define-key gud-minor-mode-map
[left-margin drag-mouse-1
]
561 (define-key gud-minor-mode-map
[left-fringe drag-mouse-1
]
563 (define-key gud-minor-mode-map
[left-margin mouse-3
]
565 (define-key gud-minor-mode-map
[left-fringe mouse-3
]
568 (define-key gud-minor-mode-map
[left-margin C-drag-mouse-1
]
570 (define-key gud-minor-mode-map
[left-fringe C-drag-mouse-1
]
572 (define-key gud-minor-mode-map
[left-fringe C-mouse-3
]
574 (define-key gud-minor-mode-map
[left-margin C-mouse-3
]
577 (setq comint-input-sender
'gdb-send
)
580 (setq gdb-pc-address
(if gdb-show-main
"main" nil
))
581 (setq gdb-previous-frame-address nil
582 gdb-memory-address
"main"
583 gdb-previous-frame nil
584 gdb-selected-frame nil
585 gdb-current-language nil
589 gdb-first-post-prompt t
593 gdb-pending-triggers nil
594 gdb-output-sink
'user
595 gdb-server-prefix
"server "
596 gdb-flush-pending-output nil
597 gdb-location-alist nil
598 gdb-source-file-list nil
601 gdb-buffer-fringe-width
(car (window-fringes))
604 gdb-source-window nil
605 gdb-inferior-status nil
607 gdb-look-up-stack nil
612 (setq gdb-buffer-type
'gdba
)
614 (if gdb-use-separate-io-buffer
(gdb-clear-inferior-io))
616 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
617 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
621 (if (eq window-system
'w32
)
622 (gdb-enqueue-input (list "set new-console off\n" 'ignore
)))
623 (gdb-enqueue-input (list "set height 0\n" 'ignore
))
624 (gdb-enqueue-input (list "set width 0\n" 'ignore
))
626 (if (string-equal gdb-version
"pre-6.4")
628 (gdb-enqueue-input (list (concat gdb-server-prefix
"info sources\n")
629 'gdb-set-gud-minor-mode-existing-buffers
))
630 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1
))
632 (list "server interpreter mi -data-list-register-names\n"
633 'gdb-get-register-names
))
634 ; Needs GDB 6.2 onwards.
636 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
637 'gdb-set-gud-minor-mode-existing-buffers-1
))
638 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2
))
640 ;; Find source file and compilation directory here.
641 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
642 (gdb-enqueue-input (list "server list\n" 'ignore
))
643 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info
))
645 (run-hooks 'gdb-mode-hook
))
647 (defun gdb-get-version ()
648 (goto-char (point-min))
649 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t
)
650 (setq gdb-version
"pre-6.4")
651 (setq gdb-version
"6.4+"))
654 (defmacro gdb-if-arrow
(arrow-position &rest body
)
656 (let ((buffer (marker-buffer ,arrow-position
)) (line))
657 (if (equal buffer
(window-buffer (posn-window end
)))
658 (with-current-buffer buffer
659 (when (or (equal start end
)
660 (equal (posn-point start
)
661 (marker-position ,arrow-position
)))
664 (defun gdb-mouse-until (event)
665 "Continue running until a source line past the current line.
666 The destination source line can be selected either by clicking
667 with mouse-3 on the fringe/margin or dragging the arrow
668 with mouse-1 (default bindings)."
670 (let ((start (event-start event
))
671 (end (event-end event
)))
672 (gdb-if-arrow gud-overlay-arrow-position
673 (setq line
(line-number-at-pos (posn-point end
)))
674 (gud-call (concat "until " (number-to-string line
))))
675 (gdb-if-arrow gdb-overlay-arrow-position
677 (goto-line (line-number-at-pos (posn-point end
)))
679 (gud-call (concat "until *%a"))))))
681 (defun gdb-mouse-jump (event)
682 "Set execution address/line.
683 The destination source line can be selected either by clicking with C-mouse-3
684 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
685 Unlike gdb-mouse-until the destination address can be before the current
686 line, and no execution takes place."
688 (let ((start (event-start event
))
689 (end (event-end event
)))
690 (gdb-if-arrow gud-overlay-arrow-position
691 (setq line
(line-number-at-pos (posn-point end
)))
693 (gud-call (concat "tbreak " (number-to-string line
)))
694 (gud-call (concat "jump " (number-to-string line
)))))
695 (gdb-if-arrow gdb-overlay-arrow-position
697 (goto-line (line-number-at-pos (posn-point end
)))
700 (gud-call (concat "tbreak *%a"))
701 (gud-call (concat "jump *%a")))))))
703 (defcustom gdb-speedbar-auto-raise nil
704 "If non-nil raise speedbar every time display of watch expressions is\
710 (defun gdb-speedbar-auto-raise (arg)
711 "Toggle automatic raising of the speedbar for watch expressions.
712 With prefix argument ARG, automatically raise speedbar if ARG is
713 positive, otherwise don't automatically raise it."
715 (setq gdb-speedbar-auto-raise
717 (not gdb-speedbar-auto-raise
)
718 (> (prefix-numeric-value arg
) 0)))
719 (message (format "Auto raising %sabled"
720 (if gdb-speedbar-auto-raise
"en" "dis"))))
722 (defcustom gdb-use-colon-colon-notation nil
723 "If non-nil use FUN::VAR format to display variables in the speedbar."
728 (define-key gud-minor-mode-map
"\C-c\C-w" 'gud-watch
)
729 (define-key global-map
(concat gud-key-prefix
"\C-w") 'gud-watch
)
731 (defun gud-watch (&optional arg event
)
732 "Watch expression at point.
733 With arg, enter name of variable to be watched in the minibuffer."
734 (interactive (list current-prefix-arg last-input-event
))
735 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer
)))
736 (if (memq minor-mode
'(gdbmi gdba
))
738 (if event
(posn-set-point (event-end event
)))
740 (save-selected-window
743 (completing-read "Name of variable: "
744 'gud-gdb-complete-command
)
745 (if (and transient-mark-mode mark-active
)
746 (buffer-substring (region-beginning) (region-end))
747 (tooltip-identifier-from-point (point))))))
749 (set-text-properties 0 (length expr
) nil expr
)
752 (if (eq minor-mode
'gdba
)
754 "server interpreter mi \"-var-create - * " expr
"\"\n")
755 (concat"-var-create - * " expr
"\n"))
756 `(lambda () (gdb-var-create-handler ,expr
)))))))
757 (message "gud-watch is a no-op in this mode."))))
759 (defconst gdb-var-create-regexp
760 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",\\(?:.*value=\\(\".*\"\\),\\)?.*type=\"\\(.*?\\)\"")
762 (defun gdb-var-create-handler (expr)
763 (goto-char (point-min))
764 (if (re-search-forward gdb-var-create-regexp nil t
)
767 (if (and (string-equal gdb-current-language
"c")
768 gdb-use-colon-colon-notation gdb-selected-frame
)
769 (setq expr
(concat gdb-selected-frame
"::" expr
))
773 (if (match-string 3) (read (match-string 3)))
774 nil gdb-frame-address
)))
775 (push var gdb-var-list
)
776 (unless (string-equal
777 speedbar-initial-expansion-list-name
"GUD")
778 (speedbar-change-initial-expansion-list "GUD"))
782 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
784 (concat "server interpreter mi \"0-var-evaluate-expression "
786 (concat "0-var-evaluate-expression " (car var
) "\n"))
787 `(lambda () (gdb-var-evaluate-expression-handler
789 (if (search-forward "Undefined command" nil t
)
790 (message-box "Watching expressions requires GDB 6.0 onwards")
791 (message-box "No symbol \"%s\" in current context." expr
))))
793 (defun gdb-speedbar-update ()
794 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
)
795 (not (member 'gdb-speedbar-timer gdb-pending-triggers
)))
796 ;; Dummy command to update speedbar even when idle.
797 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn
))
798 ;; Keep gdb-pending-triggers non-nil till end.
799 (push 'gdb-speedbar-timer gdb-pending-triggers
)))
801 (defun gdb-speedbar-timer-fn ()
802 (setq gdb-pending-triggers
803 (delq 'gdb-speedbar-timer gdb-pending-triggers
))
806 (defun gdb-var-evaluate-expression-handler (varnum changed
)
807 (goto-char (point-min))
808 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t
)
809 (setq gdb-pending-triggers
810 (delq (string-to-number (match-string 1)) gdb-pending-triggers
))
811 (let ((var (assoc varnum gdb-var-list
)))
813 (if changed
(setcar (nthcdr 5 var
) 'changed
))
814 (setcar (nthcdr 4 var
) (read (match-string 2)))))
815 (gdb-speedbar-update))
817 (defun gdb-var-list-children (varnum)
819 (list (concat "server interpreter mi \"-var-list-children " varnum
"\"\n")
820 `(lambda () (gdb-var-list-children-handler ,varnum
)))))
822 (defconst gdb-var-list-children-regexp
823 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
824 numchild=\"\\(.*?\\)\"\\(}\\|,.*?\\(type=\"\\(.*?\\)\"\\)?.*?}\\)")
826 (defun gdb-var-list-children-handler (varnum)
827 (goto-char (point-min))
828 (let ((var-list nil
))
829 (catch 'child-already-watched
830 (dolist (var gdb-var-list
)
831 (if (string-equal varnum
(car var
))
834 (while (re-search-forward gdb-var-list-children-regexp nil t
)
835 (let ((varchild (list (match-string 1)
840 (if (assoc (car varchild
) gdb-var-list
)
841 (throw 'child-already-watched nil
))
842 (push varchild var-list
)
846 "server interpreter mi \"0-var-evaluate-expression "
847 (car varchild
) "\"\n")
848 `(lambda () (gdb-var-evaluate-expression-handler
849 ,(car varchild
) nil
)))))))
850 (push var var-list
)))
851 (setq gdb-var-list
(nreverse var-list
)))))
853 (defun gdb-var-update ()
854 (when (not (member 'gdb-var-update gdb-pending-triggers
))
856 (list "server interpreter mi \"-var-update *\"\n"
857 'gdb-var-update-handler
))
858 (push 'gdb-var-update gdb-pending-triggers
)))
860 (defconst gdb-var-update-regexp
861 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
862 type_changed=\".*?\".*?}")
864 (defun gdb-var-update-handler ()
865 (dolist (var gdb-var-list
)
866 (setcar (nthcdr 5 var
) nil
))
867 (goto-char (point-min))
869 (while (re-search-forward gdb-var-update-regexp nil t
)
870 (let ((varnum (match-string 1)))
871 (if (string-equal (match-string 2) "false")
872 (let ((var (assoc varnum gdb-var-list
)))
873 (if var
(setcar (nthcdr 5 var
) 'out-of-scope
)))
875 (push n gdb-pending-triggers
)
878 (concat "server interpreter mi \"" (number-to-string n
)
879 "-var-evaluate-expression " varnum
"\"\n")
880 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t
))))))))
881 (setq gdb-pending-triggers
882 (delq 'gdb-var-update gdb-pending-triggers
)))
884 (defun gdb-var-set-format (format)
885 "Set the output format for a variable displayed in the speedbar."
886 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
890 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
) 'gdba
)
891 (concat "server interpreter mi \"-var-set-format "
892 varnum
" " format
"\"\n")
893 (concat "-var-set-format " varnum
" " format
"\n"))
894 `(lambda () (gdb-var-set-format-handler ,varnum
))))))
896 (defconst gdb-var-set-format-regexp
897 "format=\"\\(.*?\\)\",.*value=\"\\(.*?\\)\"")
899 (defun gdb-var-set-format-handler (varnum)
900 (goto-char (point-min))
901 (if (re-search-forward gdb-var-set-format-regexp nil t
)
902 (let ((var (assoc varnum gdb-var-list
)))
903 (setcar (nthcdr 4 var
) (match-string 2))
904 (gdb-var-update-1))))
906 (defun gdb-var-delete-1 (varnum)
909 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
) 'gdba
)
910 (concat "server interpreter mi \"-var-delete " varnum
"\"\n")
911 (concat "-var-delete " varnum
"\n"))
913 (setq gdb-var-list
(delq var gdb-var-list
))
914 (dolist (varchild gdb-var-list
)
915 (if (string-match (concat (car var
) "\\.") (car varchild
))
916 (setq gdb-var-list
(delq varchild gdb-var-list
)))))
918 (defun gdb-var-delete ()
919 "Delete watch expression at point from the speedbar."
921 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer
)
923 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
925 (if (string-match "\\." (car var
))
926 (message-box "Can only delete a root expression")
927 (gdb-var-delete-1 varnum
)))))
929 (defun gdb-var-delete-children (varnum)
930 "Delete children of variable object at point from the speedbar."
933 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
) 'gdba
)
934 (concat "server interpreter mi \"-var-delete -c " varnum
"\"\n")
935 (concat "-var-delete -c " varnum
"\n")) 'ignore
)))
937 (defun gdb-edit-value (text token indent
)
938 "Assign a value to a variable displayed in the speedbar."
939 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
940 (varnum (car var
)) (value))
941 (setq value
(read-string "New value: "))
944 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
) 'gdba
)
945 (concat "server interpreter mi \"-var-assign "
946 varnum
" " value
"\"\n")
947 (concat "-var-assign " varnum
" " value
"\n"))
948 `(lambda () (gdb-edit-value-handler ,value
))))))
950 (defun gdb-edit-value-handler (value)
951 (goto-char (point-min))
952 (if (re-search-forward gdb-error-regexp nil t
)
953 (message-box "Invalid number or expression (%s)" value
)))
955 (defcustom gdb-show-changed-values t
956 "If non-nil change the face of out of scope variables and changed values.
957 Out of scope variables are suppressed with `shadow' face.
958 Changed values are highlighted with the face `font-lock-warning-face'."
963 (defcustom gdb-max-children
40
964 "Maximum number of children before expansion requires confirmation."
969 (defcustom gdb-delete-out-of-scope t
970 "If non-nil delete watch expressions automatically when they go out of scope."
975 (defun gdb-speedbar-expand-node (text token indent
)
976 "Expand the node the user clicked on.
977 TEXT is the text of the button we clicked on, a + or - item.
978 TOKEN is data related to this node.
979 INDENT is the current indentation depth."
980 (if (and gud-comint-buffer
(buffer-name gud-comint-buffer
))
982 (cond ((string-match "+" text
) ;expand this node
983 (let* ((var (assoc token gdb-var-list
))
984 (expr (nth 1 var
)) (children (nth 2 var
)))
985 (if (or (<= (string-to-number children
) gdb-max-children
)
988 "%s has %s children. Continue? " expr children
)))
989 (if (and (eq (buffer-local-value
990 'gud-minor-mode gud-comint-buffer
) 'gdba
)
991 (string-equal gdb-version
"pre-6.4"))
992 (gdb-var-list-children token
)
993 (gdb-var-list-children-1 token
)))))
994 ((string-match "-" text
) ;contract this node
995 (dolist (var gdb-var-list
)
996 (if (string-match (concat token
"\\.") (car var
))
997 (setq gdb-var-list
(delq var gdb-var-list
))))
998 (gdb-var-delete-children token
)
999 (speedbar-change-expand-button-char ?
+)
1000 (speedbar-delete-subblock indent
))
1001 (t (error "Ooops... not sure what to do")))
1002 (speedbar-center-buffer-smartly))
1003 (message-box "GUD session has been killed")))
1005 (defun gdb-get-target-string ()
1006 (with-current-buffer gud-comint-buffer
1013 ;; Each buffer has a TYPE -- a symbol that identifies the function
1014 ;; of that particular buffer.
1016 ;; The usual gdb interaction buffer is given the type `gdba' and
1017 ;; is constructed specially.
1019 ;; Others are constructed by gdb-get-buffer-create and
1020 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
1022 (defvar gdb-buffer-rules-assoc
'())
1024 (defun gdb-get-buffer (key)
1025 "Return the gdb buffer tagged with type KEY.
1026 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1028 (gdb-look-for-tagged-buffer key
(buffer-list))))
1030 (defun gdb-get-buffer-create (key)
1031 "Create a new gdb buffer of the type specified by KEY.
1032 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1033 (or (gdb-get-buffer key
)
1034 (let* ((rules (assoc key gdb-buffer-rules-assoc
))
1035 (name (funcall (gdb-rules-name-maker rules
)))
1036 (new (get-buffer-create name
)))
1037 (with-current-buffer new
1039 (if (cdr (cdr rules
))
1040 (setq trigger
(funcall (car (cdr (cdr rules
))))))
1041 (setq gdb-buffer-type key
)
1042 (set (make-local-variable 'gud-minor-mode
)
1043 (buffer-local-value 'gud-minor-mode gud-comint-buffer
))
1044 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
1045 (if trigger
(funcall trigger
)))
1048 (defun gdb-rules-name-maker (rules) (car (cdr rules
)))
1050 (defun gdb-look-for-tagged-buffer (key bufs
)
1052 (while (and (not retval
) bufs
)
1053 (set-buffer (car bufs
))
1054 (if (eq gdb-buffer-type key
)
1055 (setq retval
(car bufs
)))
1056 (setq bufs
(cdr bufs
)))
1060 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1061 ;; at least one and possible more functions. The functions have these
1062 ;; roles in defining a buffer type:
1064 ;; NAME - Return a name for this buffer type.
1066 ;; The remaining function(s) are optional:
1068 ;; MODE - called in a new buffer with no arguments, should establish
1069 ;; the proper mode for the buffer.
1072 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
1073 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc
)))
1075 (setcdr binding rules
)
1076 (push (cons buffer-type rules
)
1077 gdb-buffer-rules-assoc
))))
1079 ;; GUD buffers are an exception to the rules
1080 (gdb-set-buffer-rules 'gdba
'error
)
1082 ;; Partial-output buffer : This accumulates output from a command executed on
1083 ;; behalf of emacs (rather than the user).
1085 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1086 'gdb-partial-output-name
)
1088 (defun gdb-partial-output-name ()
1089 (concat " *partial-output-"
1090 (gdb-get-target-string)
1094 (gdb-set-buffer-rules 'gdb-inferior-io
1095 'gdb-inferior-io-name
1096 'gdb-inferior-io-mode
)
1098 (defun gdb-inferior-io-name ()
1099 (concat "*input/output of "
1100 (gdb-get-target-string)
1103 (defun gdb-display-separate-io-buffer ()
1104 "Display IO of debugged program in a separate window."
1106 (if gdb-use-separate-io-buffer
1108 (gdb-get-buffer-create 'gdb-inferior-io
) t
)))
1110 (defconst gdb-frame-parameters
1111 '((height .
14) (width .
80)
1113 (tool-bar-lines . nil
)
1114 (menu-bar-lines . nil
)
1115 (minibuffer . nil
)))
1117 (defun gdb-frame-separate-io-buffer ()
1118 "Display IO of debugged program in a new frame."
1120 (if gdb-use-separate-io-buffer
1121 (let ((special-display-regexps (append special-display-regexps
'(".*")))
1122 (special-display-frame-alist gdb-frame-parameters
))
1123 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io
)))))
1125 (defvar gdb-inferior-io-mode-map
1126 (let ((map (make-sparse-keymap)))
1127 (define-key map
"\C-c\C-c" 'gdb-separate-io-interrupt
)
1128 (define-key map
"\C-c\C-z" 'gdb-separate-io-stop
)
1129 (define-key map
"\C-c\C-\\" 'gdb-separate-io-quit
)
1130 (define-key map
"\C-c\C-d" 'gdb-separate-io-eof
)
1131 (define-key map
"\C-d" 'gdb-separate-io-eof
)
1134 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
1135 "Major mode for gdb inferior-io."
1136 :syntax-table nil
:abbrev-table nil
1137 ;; We want to use comint because it has various nifty and familiar
1138 ;; features. We don't need a process, but comint wants one, so create
1140 (make-comint-in-buffer
1141 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1142 (current-buffer) "hexl")
1143 (setq comint-input-sender
'gdb-inferior-io-sender
))
1145 (defun gdb-inferior-io-sender (proc string
)
1146 ;; PROC is the pseudo-process created to satisfy comint.
1147 (with-current-buffer (process-buffer proc
)
1148 (setq proc
(get-buffer-process gud-comint-buffer
))
1149 (process-send-string proc string
)
1150 (process-send-string proc
"\n")))
1152 (defun gdb-separate-io-interrupt ()
1153 "Interrupt the program being debugged."
1156 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1158 (defun gdb-separate-io-quit ()
1159 "Send quit signal to the program being debugged."
1162 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1164 (defun gdb-separate-io-stop ()
1165 "Stop the program being debugged."
1168 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
1170 (defun gdb-separate-io-eof ()
1171 "Send end-of-file to the program being debugged."
1174 (get-buffer-process gud-comint-buffer
)))
1177 ;; gdb communications
1180 ;; INPUT: things sent to gdb
1182 ;; The queues are lists. Each element is either a string (indicating user or
1183 ;; user-like input) or a list of the form:
1185 ;; (INPUT-STRING HANDLER-FN)
1187 ;; The handler function will be called from the partial-output buffer when the
1188 ;; command completes. This is the way to write commands which invoke gdb
1189 ;; commands autonomously.
1191 ;; These lists are consumed tail first.
1194 (defun gdb-send (proc string
)
1195 "A comint send filter for gdb.
1196 This filter may simply queue input for a later time."
1198 (with-current-buffer gud-comint-buffer
1199 (let ((inhibit-read-only t
))
1200 (remove-text-properties (point-min) (point-max) '(face))))
1203 (let ((item (concat string
"\n")))
1204 (if gdb-enable-debug
(push (cons 'send item
) gdb-debug-log
))
1205 (process-send-string proc item
)))
1206 (if (string-match "\\\\\\'" string
)
1207 (setq gdb-continuation
(concat gdb-continuation string
"\n"))
1208 (let ((item (concat gdb-continuation string
1209 (if (not comint-input-sender-no-newline
) "\n"))))
1210 (gdb-enqueue-input item
)
1211 (setq gdb-continuation nil
))))))
1213 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1214 ;; is a query, or other non-top-level prompt.
1216 (defun gdb-enqueue-input (item)
1217 (if (not gud-running
)
1220 (gdb-send-item item
)
1221 (setq gdb-prompting nil
))
1222 (push item gdb-input-queue
))))
1224 (defun gdb-dequeue-input ()
1225 (let ((queue gdb-input-queue
))
1227 (let ((last (car (last queue
))))
1228 (unless (nbutlast queue
) (setq gdb-input-queue
'()))
1230 ;; This should be nil here anyway but set it just to make sure.
1231 (setq gdb-pending-triggers nil
))))
1233 (defun gdb-send-item (item)
1234 (setq gdb-flush-pending-output nil
)
1235 (if gdb-enable-debug
(push (cons 'send-item item
) gdb-debug-log
))
1236 (setq gdb-current-item item
)
1237 (let ((process (get-buffer-process gud-comint-buffer
)))
1238 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer
) 'gdba
)
1241 (setq gdb-output-sink
'user
)
1242 (process-send-string process item
))
1244 (gdb-clear-partial-output)
1245 (setq gdb-output-sink
'pre-emacs
)
1246 (process-send-string process
1248 ;; case: eq gud-minor-mode 'gdbmi
1249 (gdb-clear-partial-output)
1250 (setq gdb-output-sink
'emacs
)
1251 (process-send-string process
(car item
)))))
1254 ;; output -- things gdb prints to emacs
1256 ;; GDB output is a stream interrupted by annotations.
1257 ;; Annotations can be recognized by their beginning
1258 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1260 ;; The tag is a string obeying symbol syntax.
1262 ;; The optional part `<opt>' can be either the empty string
1263 ;; or a space followed by more data relating to the annotation.
1264 ;; For example, the SOURCE annotation is followed by a filename,
1265 ;; line number and various useless goo. This data must not include
1269 (defcustom gud-gdb-command-name
"gdb --annotate=3"
1270 "Default command to execute an executable under the GDB debugger.
1271 The option \"--annotate=3\" must be included in this value if you
1272 want the GDB Graphical Interface."
1277 (defvar gdb-annotation-rules
1278 '(("pre-prompt" gdb-pre-prompt
)
1279 ("prompt" gdb-prompt
)
1280 ("commands" gdb-subprompt
)
1281 ("overload-choice" gdb-subprompt
)
1282 ("query" gdb-subprompt
)
1283 ;; Need this prompt for GDB 6.1
1284 ("nquery" gdb-subprompt
)
1285 ("prompt-for-continue" gdb-subprompt
)
1286 ("post-prompt" gdb-post-prompt
)
1287 ("source" gdb-source
)
1288 ("starting" gdb-starting
)
1289 ("exited" gdb-exited
)
1290 ("signalled" gdb-signalled
)
1291 ("signal" gdb-signal
)
1292 ("breakpoint" gdb-stopping
)
1293 ("watchpoint" gdb-stopping
)
1294 ("frame-begin" gdb-frame-begin
)
1295 ("stopped" gdb-stopped
)
1296 ("error-begin" gdb-error
)
1298 ) "An assoc mapping annotation tags to functions which process them.")
1301 (setq gdb-flush-pending-output t
)
1302 (setq gud-running nil
)
1303 (gdb-force-mode-line-update
1304 (propertize "stopped"'face font-lock-warning-face
))
1305 (setq gdb-output-sink
'user
)
1306 (setq gdb-input-queue nil
)
1307 (setq gdb-pending-triggers nil
)
1308 (setq gdb-prompting t
))
1310 (defconst gdb-source-spec-regexp
1311 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1313 ;; Do not use this except as an annotation handler.
1314 (defun gdb-source (args)
1315 (string-match gdb-source-spec-regexp args
)
1316 ;; Extract the frame position from the marker.
1317 (setq gud-last-frame
1319 (match-string 1 args
)
1320 (string-to-number (match-string 2 args
))))
1321 (setq gdb-pc-address
(match-string 3 args
))
1322 ;; cover for auto-display output which comes *before*
1323 ;; stopped annotation
1324 (if (eq gdb-output-sink
'inferior
) (setq gdb-output-sink
'user
)))
1326 (defun gdb-pre-prompt (ignored)
1327 "An annotation handler for `pre-prompt'.
1328 This terminates the collection of output from a previous command if that
1329 happens to be in effect."
1330 (setq gdb-error nil
)
1331 (let ((sink gdb-output-sink
))
1335 (setq gdb-output-sink
'post-emacs
))
1338 (error "Phase error in gdb-pre-prompt (got %s)" sink
)))))
1340 (defun gdb-prompt (ignored)
1341 "An annotation handler for `prompt'.
1342 This sends the next command (if any) to gdb."
1343 (when gdb-first-prompt
1344 (gdb-force-mode-line-update
1345 (propertize "initializing..." 'face font-lock-variable-name-face
))
1347 (setq gdb-first-prompt nil
))
1348 (let ((sink gdb-output-sink
))
1351 ((eq sink
'post-emacs
)
1352 (setq gdb-output-sink
'user
)
1354 (car (cdr gdb-current-item
))))
1355 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
1356 (funcall handler
))))
1359 (error "Phase error in gdb-prompt (got %s)" sink
))))
1360 (let ((input (gdb-dequeue-input)))
1362 (gdb-send-item input
)
1364 (setq gdb-prompting t
)
1365 (gud-display-frame)))))
1367 (defun gdb-subprompt (ignored)
1368 "An annotation handler for non-top-level prompts."
1369 (setq gdb-prompting t
))
1371 (defun gdb-starting (ignored)
1372 "An annotation handler for `starting'.
1373 This says that I/O for the subprocess is now the program being debugged,
1375 (setq gdb-active-process t
)
1376 (setq gdb-printing t
)
1377 (let ((sink gdb-output-sink
))
1381 (setq gud-running t
)
1382 (setq gdb-inferior-status
"running")
1383 (setq gdb-signalled nil
)
1384 (gdb-force-mode-line-update
1385 (propertize gdb-inferior-status
'face font-lock-type-face
))
1386 (gdb-remove-text-properties)
1387 (setq gud-old-arrow gud-overlay-arrow-position
)
1388 (setq gud-overlay-arrow-position nil
)
1389 (setq gdb-overlay-arrow-position nil
)
1390 (setq gdb-stack-position nil
)
1391 (if gdb-use-separate-io-buffer
1392 (setq gdb-output-sink
'inferior
))))
1395 (error "Unexpected `starting' annotation")))))
1397 (defun gdb-signal (ignored)
1398 (setq gdb-inferior-status
"signal")
1399 (gdb-force-mode-line-update
1400 (propertize gdb-inferior-status
'face font-lock-warning-face
))
1401 (gdb-stopping ignored
))
1403 (defun gdb-stopping (ignored)
1404 "An annotation handler for `breakpoint' and other annotations.
1405 They say that I/O for the subprocess is now GDB, not the program
1407 (if gdb-use-separate-io-buffer
1408 (let ((sink gdb-output-sink
))
1410 ((eq sink
'inferior
)
1411 (setq gdb-output-sink
'user
))
1414 (error "Unexpected stopping annotation"))))))
1416 (defun gdb-exited (ignored)
1417 "An annotation handler for `exited' and `signalled'.
1418 They say that I/O for the subprocess is now GDB, not the program
1419 being debugged and that the program is no longer running. This
1420 function is used to change the focus of GUD tooltips to #define
1422 (setq gdb-active-process nil
)
1423 (setq gud-overlay-arrow-position nil
)
1424 (setq gdb-overlay-arrow-position nil
)
1425 (setq gdb-stack-position nil
)
1426 (setq gud-old-arrow nil
)
1427 (setq gdb-inferior-status
"exited")
1428 (gdb-force-mode-line-update
1429 (propertize gdb-inferior-status
'face font-lock-warning-face
))
1430 (gdb-stopping ignored
))
1432 (defun gdb-signalled (ignored)
1433 (setq gdb-signalled t
))
1435 (defun gdb-frame-begin (ignored)
1436 (setq gdb-frame-begin t
)
1437 (setq gdb-printing nil
)
1438 (let ((sink gdb-output-sink
))
1440 ((eq sink
'inferior
)
1441 (setq gdb-output-sink
'user
))
1443 ((eq sink
'emacs
) t
)
1446 (error "Unexpected frame-begin annotation (%S)" sink
)))))
1448 (defcustom gdb-same-frame focus-follows-mouse
1449 "Non-nil means pop up GUD buffer in same frame."
1454 (defcustom gdb-find-source-frame nil
1455 "Non-nil means try to find a source frame further up stack e.g after signal."
1460 (defun gdb-find-source-frame (arg)
1461 "Toggle trying to find a source frame further up stack.
1462 With prefix argument ARG, look for a source frame further up
1463 stack if ARG is positive, otherwise don't look further up."
1465 (setq gdb-find-source-frame
1467 (not gdb-find-source-frame
)
1468 (> (prefix-numeric-value arg
) 0)))
1469 (message (format "Looking for source frame %sabled"
1470 (if gdb-find-source-frame
"en" "dis"))))
1472 (defun gdb-stopped (ignored)
1473 "An annotation handler for `stopped'.
1474 It is just like `gdb-stopping', except that if we already set the output
1475 sink to `user' in `gdb-stopping', that is fine."
1476 (setq gud-running nil
)
1477 (unless (or gud-overlay-arrow-position gud-last-frame
)
1478 (if (and gdb-frame-begin gdb-printing
)
1479 (setq gud-overlay-arrow-position gud-old-arrow
)
1480 ;;Pop up GUD buffer to display current frame when it doesn't have source
1481 ;;information i.e if not compiled with -g as with libc routines generally.
1483 (gdb-display-gdb-buffer)
1484 (gdb-frame-gdb-buffer))
1485 (if gdb-find-source-frame
1486 ;;Try to find source further up stack e.g after signal.
1487 (setq gdb-look-up-stack
1488 (if (gdb-get-buffer 'gdb-stack-buffer
)
1491 (gdb-get-buffer-create 'gdb-stack-buffer
)
1492 (gdb-invalidate-frames)
1494 (unless (member gdb-inferior-status
'("exited" "signal"))
1495 (setq gdb-active-process t
) ;Just for attaching case.
1496 (setq gdb-inferior-status
"stopped")
1497 (gdb-force-mode-line-update
1498 (propertize gdb-inferior-status
'face font-lock-warning-face
)))
1499 (let ((sink gdb-output-sink
))
1501 ((eq sink
'inferior
)
1502 (setq gdb-output-sink
'user
))
1506 (error "Unexpected stopped annotation"))))
1507 (if gdb-signalled
(gdb-exited ignored
)))
1509 (defun gdb-error (ignored)
1510 (setq gdb-error
(not gdb-error
)))
1512 (defun gdb-post-prompt (ignored)
1513 "An annotation handler for `post-prompt'.
1514 This begins the collection of output from the current command if that
1515 happens to be appropriate."
1516 ;; Don't add to queue if there outstanding items or gdb-version is not known
1518 (unless (or gdb-pending-triggers gdb-first-post-prompt
)
1519 (gdb-get-selected-frame)
1520 (gdb-invalidate-frames)
1521 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1522 (gdb-get-buffer-create 'gdb-breakpoints-buffer
)
1523 (gdb-invalidate-breakpoints)
1524 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1525 ;; so gdb-pc-address is updated.
1526 ;; (gdb-invalidate-assembler)
1528 (if (string-equal gdb-version
"pre-6.4")
1529 (gdb-invalidate-registers)
1530 (gdb-get-changed-registers)
1531 (gdb-invalidate-registers-1))
1533 (gdb-invalidate-memory)
1534 (if (string-equal gdb-version
"pre-6.4")
1535 (gdb-invalidate-locals)
1536 (gdb-invalidate-locals-1))
1538 (gdb-invalidate-threads)
1539 (unless (eq system-type
'darwin
) ;Breaks on Darwin's GDB-5.3.
1540 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1541 ;; Only needed/used with speedbar/watch expressions.
1542 (when (and (boundp 'speedbar-frame
) (frame-live-p speedbar-frame
))
1543 (if (string-equal gdb-version
"pre-6.4")
1545 (gdb-var-update-1)))))
1546 (setq gdb-first-post-prompt nil
)
1547 (let ((sink gdb-output-sink
))
1550 ((eq sink
'pre-emacs
)
1551 (setq gdb-output-sink
'emacs
))
1554 (error "Phase error in gdb-post-prompt (got %s)" sink
)))))
1556 (defconst gdb-buffer-list
1557 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer
))
1559 (defun gdb-remove-text-properties ()
1560 (dolist (buffertype gdb-buffer-list
)
1561 (let ((buffer (gdb-get-buffer buffertype
)))
1563 (with-current-buffer buffer
1564 (let ((inhibit-read-only t
))
1565 (remove-text-properties
1566 (point-min) (point-max) '(mouse-face nil help-echo nil
))))))))
1568 ;; GUD displays the selected GDB frame. This might might not be the current
1569 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1570 ;; visited breakpoint is, use that window.
1571 (defun gdb-display-source-buffer (buffer)
1572 (let* ((last-window (if gud-last-last-frame
1574 (gud-find-file (car gud-last-last-frame
)))))
1575 (source-window (or last-window
1576 (if (and gdb-source-window
1577 (window-live-p gdb-source-window
))
1578 gdb-source-window
))))
1580 (setq gdb-source-window source-window
)
1581 (set-window-buffer source-window buffer
))
1584 ;; Derived from gud-gdb-marker-regexp
1585 (defvar gdb-fullname-regexp
1586 (concat "\\(.:?[^" ":" "\n]*\\)" ":" "\\([0-9]*\\)" ":" ".*"))
1588 (defun gud-gdba-marker-filter (string)
1589 "A gud marker filter for gdb. Handle a burst of output from GDB."
1590 (if gdb-flush-pending-output
1592 (when gdb-enable-debug
1593 (push (cons 'recv string
) gdb-debug-log
)
1594 (if (and gdb-debug-log-max
1595 (> (length gdb-debug-log
) gdb-debug-log-max
))
1596 (setcdr (nthcdr (1- gdb-debug-log-max
) gdb-debug-log
) nil
)))
1597 ;; Recall the left over gud-marker-acc from last time.
1598 (setq gud-marker-acc
(concat gud-marker-acc string
))
1599 ;; Start accumulating output for the GUD buffer.
1602 ;; Process all the complete markers in this chunk.
1603 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc
)
1604 (let ((annotation (match-string 1 gud-marker-acc
))
1605 (before (substring gud-marker-acc
0 (match-beginning 0)))
1606 (after (substring gud-marker-acc
(match-end 0))))
1608 ;; Parse the tag from the annotation, and maybe its arguments.
1609 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation
)
1610 (let* ((annotation-type (match-string 1 annotation
))
1611 (annotation-arguments (match-string 2 annotation
))
1612 (annotation-rule (assoc annotation-type
1613 gdb-annotation-rules
))
1614 (fullname (string-match gdb-fullname-regexp annotation-type
)))
1616 ;; Stuff prior to the match is just ordinary output.
1617 ;; It is either concatenated to OUTPUT or directed
1620 (gdb-concat-output output
1621 (concat before
(if fullname
"\n"))))
1623 ;; Take that stuff off the gud-marker-acc.
1624 (setq gud-marker-acc after
)
1626 ;; Call the handler for this annotation.
1628 (funcall (car (cdr annotation-rule
))
1629 annotation-arguments
)
1631 ;; Switch to gud-gdb-marker-filter if appropriate.
1634 ;; Extract the frame position from the marker.
1635 (setq gud-last-frame
(cons (match-string 1 annotation
)
1637 (match-string 2 annotation
))))
1639 (set (make-local-variable 'gud-minor-mode
) 'gdb
)
1640 (set (make-local-variable 'gud-marker-filter
)
1641 'gud-gdb-marker-filter
)))
1643 ;; Else the annotation is not recognized. Ignore it silently,
1644 ;; so that GDB can add new annotations without causing
1648 ;; Does the remaining text end in a partial line?
1649 ;; If it does, then keep part of the gud-marker-acc until we get more.
1650 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1653 ;; Everything before the potential marker start can be output.
1655 (gdb-concat-output output
1656 (substring gud-marker-acc
0
1657 (match-beginning 0))))
1659 ;; Everything after, we save, to combine with later input.
1660 (setq gud-marker-acc
(substring gud-marker-acc
1661 (match-beginning 0))))
1663 ;; In case we know the gud-marker-acc contains no partial annotations:
1665 (setq output
(gdb-concat-output output gud-marker-acc
))
1666 (setq gud-marker-acc
"")))
1669 (defun gdb-concat-output (so-far new
)
1671 (put-text-property 0 (length new
) 'face font-lock-warning-face new
))
1672 (let ((sink gdb-output-sink
))
1674 ((eq sink
'user
) (concat so-far new
))
1675 ((or (eq sink
'pre-emacs
) (eq sink
'post-emacs
)) so-far
)
1677 (gdb-append-to-partial-output new
)
1679 ((eq sink
'inferior
)
1680 (gdb-append-to-inferior-io new
)
1684 (error "Bogon output sink %S" sink
)))))
1686 (defun gdb-append-to-partial-output (string)
1687 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
1688 (goto-char (point-max))
1691 (defun gdb-clear-partial-output ()
1692 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer
)
1695 (defun gdb-append-to-inferior-io (string)
1696 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1697 (goto-char (point-max))
1698 (insert-before-markers string
))
1699 (if (not (string-equal string
""))
1700 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io
) t
)))
1702 (defun gdb-clear-inferior-io ()
1703 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io
)
1707 ;; One trick is to have a command who's output is always available in a buffer
1708 ;; of it's own, and is always up to date. We build several buffers of this
1711 ;; There are two aspects to this: gdb has to tell us when the output for that
1712 ;; command might have changed, and we have to be able to run the command
1713 ;; behind the user's back.
1715 ;; The output phasing associated with the variable gdb-output-sink
1716 ;; help us to run commands behind the user's back.
1718 ;; Below is the code for specificly managing buffers of output from one
1722 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1723 ;; It adds an input for the command we are tracking. It should be the
1724 ;; annotation rule binding of whatever gdb sends to tell us this command
1725 ;; might have changed it's output.
1727 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1728 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1729 ;; input in the input queue (see comment about ``gdb communications'' above).
1731 (defmacro def-gdb-auto-update-trigger
(name demand-predicate gdb-command
1733 `(defun ,name
(&optional ignored
)
1734 (if (and ,demand-predicate
1736 gdb-pending-triggers
)))
1739 (list ,gdb-command
',output-handler
))
1740 (push ',name gdb-pending-triggers
)))))
1742 (defmacro def-gdb-auto-update-handler
(name trigger buf-key custom-defun
)
1744 (setq gdb-pending-triggers
1746 gdb-pending-triggers
))
1747 (let ((buf (gdb-get-buffer ',buf-key
)))
1749 (with-current-buffer buf
1750 (let* ((window (get-buffer-window buf
0))
1751 (start (window-start window
))
1752 (p (window-point window
))
1753 (buffer-read-only nil
))
1755 (insert-buffer-substring (gdb-get-buffer-create
1756 'gdb-partial-output-buffer
))
1757 (set-window-start window start
)
1758 (set-window-point window p
)))))
1759 ;; put customisation here
1762 (defmacro def-gdb-auto-updated-buffer
(buffer-key
1763 trigger-name gdb-command
1764 output-handler-name custom-defun
)
1766 (def-gdb-auto-update-trigger ,trigger-name
1767 ;; The demand predicate:
1768 (gdb-get-buffer ',buffer-key
)
1770 ,output-handler-name
)
1771 (def-gdb-auto-update-handler ,output-handler-name
1772 ,trigger-name
,buffer-key
,custom-defun
)))
1776 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1778 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1779 'gdb-breakpoints-buffer-name
1780 'gdb-breakpoints-mode
)
1782 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1783 ;; This defines the auto update rule for buffers of type
1784 ;; `gdb-breakpoints-buffer'.
1786 ;; It defines a function to serve as the annotation handler that
1787 ;; handles the `foo-invalidated' message. That function is called:
1788 gdb-invalidate-breakpoints
1790 ;; To update the buffer, this command is sent to gdb.
1791 "server info breakpoints\n"
1793 ;; This also defines a function to be the handler for the output
1794 ;; from the command above. That function will copy the output into
1795 ;; the appropriately typed buffer. That function will be called:
1796 gdb-info-breakpoints-handler
1797 ;; buffer specific functions
1798 gdb-info-breakpoints-custom
)
1800 (defconst breakpoint-xpm-data
1802 static char *magick[] = {
1803 /* columns rows colors chars-per-pixel */
1819 "XPM data used for breakpoint icon.")
1821 (defconst breakpoint-enabled-pbm-data
1824 0 0 0 0 1 1 1 1 0 0 0 0
1825 0 0 0 1 1 1 1 1 1 0 0 0
1826 0 0 1 1 1 1 1 1 1 1 0 0
1827 0 1 1 1 1 1 1 1 1 1 1 0
1828 0 1 1 1 1 1 1 1 1 1 1 0
1829 0 1 1 1 1 1 1 1 1 1 1 0
1830 0 1 1 1 1 1 1 1 1 1 1 0
1831 0 0 1 1 1 1 1 1 1 1 0 0
1832 0 0 0 1 1 1 1 1 1 0 0 0
1833 0 0 0 0 1 1 1 1 0 0 0 0"
1834 "PBM data used for enabled breakpoint icon.")
1836 (defconst breakpoint-disabled-pbm-data
1848 0 0 0 1 0 1 0 1 0 0"
1849 "PBM data used for disabled breakpoint icon.")
1851 (defvar breakpoint-enabled-icon nil
1852 "Icon for enabled breakpoint in display margin.")
1854 (defvar breakpoint-disabled-icon nil
1855 "Icon for disabled breakpoint in display margin.")
1857 (and (display-images-p)
1858 ;; Bitmap for breakpoint in fringe
1859 (define-fringe-bitmap 'breakpoint
1860 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1861 ;; Bitmap for gud-overlay-arrow in fringe
1862 (define-fringe-bitmap 'hollow-right-triangle
1863 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1865 (defface breakpoint-enabled
1869 "Face for enabled breakpoint icon in fringe."
1872 (defface breakpoint-disabled
1873 '((((class color
) (min-colors 88)) :foreground
"grey70")
1874 ;; Ensure that on low-color displays that we end up something visible.
1875 (((class color
) (min-colors 8) (background light
))
1876 :foreground
"black")
1877 (((class color
) (min-colors 8) (background dark
))
1878 :foreground
"white")
1879 (((type tty
) (class mono
))
1881 (t :background
"gray"))
1882 "Face for disabled breakpoint icon in fringe."
1885 (defconst gdb-breakpoint-regexp
1886 "\\(?:\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\|\\([0-9]+\\.[0-9]+\\)\\)\\s-+\\(.\\)\\s-+")
1888 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1889 (defun gdb-info-breakpoints-custom ()
1890 (let ((flag) (bptno))
1891 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1892 (dolist (buffer (buffer-list))
1893 (with-current-buffer buffer
1894 (if (and (memq gud-minor-mode
'(gdba gdbmi
))
1895 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1896 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1897 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer
)
1899 (let ((buffer-read-only nil
))
1900 (goto-char (point-min))
1901 (while (< (point) (- (point-max) 1))
1903 (if (looking-at gdb-breakpoint-regexp
)
1905 (setq bptno
(or (match-string 1) (match-string 2)))
1906 (setq flag
(char-after (match-beginning 3)))
1907 (if (match-string 1)
1908 (setq gdb-parent-bptno-enabled
(eq flag ?y
)))
1909 (add-text-properties
1910 (match-beginning 3) (match-end 3)
1912 '(face font-lock-warning-face
)
1913 '(face font-lock-type-face
)))
1915 (el (line-end-position)))
1916 (if (re-search-forward " in \\(.*\\) at\\s-+" el t
)
1918 (add-text-properties
1919 (match-beginning 1) (match-end 1)
1920 '(face font-lock-function-name-face
))
1921 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1922 (let ((line (match-string 2))
1923 (file (match-string 1)))
1924 (add-text-properties bl el
1925 '(mouse-face highlight
1926 help-echo
"mouse-2, RET: visit breakpoint"))
1927 (unless (file-exists-p file
)
1928 (setq file
(cdr (assoc bptno gdb-location-alist
))))
1930 (not (string-equal file
"File not found")))
1931 (with-current-buffer
1932 (find-file-noselect file
'nowarn
)
1933 (set (make-local-variable 'gud-minor-mode
)
1935 (set (make-local-variable 'tool-bar-map
)
1937 ;; Only want one breakpoint icon at each
1940 (goto-line (string-to-number line
))
1941 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
)))
1944 (concat gdb-server-prefix
"list "
1945 (match-string-no-properties 1) ":1\n")
1948 (list (concat gdb-server-prefix
"info source\n")
1949 `(lambda () (gdb-get-location
1950 ,bptno
,line
,flag
)))))))
1951 (if (re-search-forward
1952 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1954 (add-text-properties
1955 (match-beginning 1) (match-end 1)
1956 '(face font-lock-function-name-face
))
1958 (re-search-backward "\\s-\\(\\S-*\\)"
1960 (add-text-properties
1961 (match-beginning 1) (match-end 1)
1962 '(face font-lock-variable-name-face
)))))))
1964 (if (gdb-get-buffer 'gdb-assembler-buffer
) (gdb-assembler-custom)))
1966 (defun gdb-mouse-set-clear-breakpoint (event)
1967 "Set/clear breakpoint in left fringe/margin with mouse click."
1969 (mouse-minibuffer-check event
)
1970 (let ((posn (event-end event
)))
1971 (if (numberp (posn-point posn
))
1972 (with-selected-window (posn-window posn
)
1974 (goto-char (posn-point posn
))
1975 (if (or (posn-object posn
)
1976 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
1979 (gud-break nil
)))))))
1981 (defun gdb-mouse-toggle-breakpoint-margin (event)
1982 "Enable/disable breakpoint in left margin with mouse click."
1984 (mouse-minibuffer-check event
)
1985 (let ((posn (event-end event
)))
1986 (if (numberp (posn-point posn
))
1987 (with-selected-window (posn-window posn
)
1989 (goto-char (posn-point posn
))
1990 (if (posn-object posn
)
1991 (let* ((bptno (get-text-property
1992 0 'gdb-bptno
(car (posn-string posn
)))))
1993 (string-match "\\([0-9+]\\)*" bptno
)
1996 (concat gdb-server-prefix
1997 (if (get-text-property
1998 0 'gdb-enabled
(car (posn-string posn
)))
2001 (match-string 1 bptno
) "\n")
2004 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2005 "Enable/disable breakpoint in left fringe with mouse click."
2007 (mouse-minibuffer-check event
)
2008 (let* ((posn (event-end event
))
2009 (pos (posn-point posn
))
2012 (with-selected-window (posn-window posn
)
2014 (set-buffer (window-buffer (selected-window)))
2016 (dolist (overlay (overlays-in pos pos
))
2017 (when (overlay-get overlay
'put-break
)
2018 (setq obj
(overlay-get overlay
'before-string
))))
2020 (let* ((bptno (get-text-property 0 'gdb-bptno obj
)))
2021 (string-match "\\([0-9+]\\)*" bptno
)
2024 (concat gdb-server-prefix
2025 (if (get-text-property 0 'gdb-enabled obj
)
2028 (match-string 1 bptno
) "\n")
2031 (defun gdb-breakpoints-buffer-name ()
2032 (with-current-buffer gud-comint-buffer
2033 (concat "*breakpoints of " (gdb-get-target-string) "*")))
2035 (defun gdb-display-breakpoints-buffer ()
2036 "Display status of user-settable breakpoints."
2039 (gdb-get-buffer-create 'gdb-breakpoints-buffer
) t
))
2041 (defun gdb-frame-breakpoints-buffer ()
2042 "Display status of user-settable breakpoints in a new frame."
2044 (let ((special-display-regexps (append special-display-regexps
'(".*")))
2045 (special-display-frame-alist gdb-frame-parameters
))
2046 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer
))))
2048 (defvar gdb-breakpoints-mode-map
2049 (let ((map (make-sparse-keymap))
2050 (menu (make-sparse-keymap "Breakpoints")))
2051 (define-key menu
[quit] '("Quit" . gdb-delete-frame-or-window))
2052 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2053 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2054 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2055 (suppress-keymap map)
2056 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2057 (define-key map " " 'gdb-toggle-breakpoint)
2058 (define-key map "D" 'gdb-delete-breakpoint)
2059 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2060 (define-key map "q" 'gdb-delete-frame-or-window)
2061 (define-key map "\r" 'gdb-goto-breakpoint)
2062 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2063 (define-key map [follow-link] 'mouse-face)
2066 (defun gdb-delete-frame-or-window ()
2067 "Delete frame if there is only one window. Otherwise delete the window."
2069 (if (one-window-p) (delete-frame)
2072 (defun gdb-breakpoints-mode ()
2073 "Major mode for gdb breakpoints.
2075 \\{gdb-breakpoints-mode-map}"
2076 (kill-all-local-variables)
2077 (setq major-mode 'gdb-breakpoints-mode)
2078 (setq mode-name "Breakpoints")
2079 (use-local-map gdb-breakpoints-mode-map)
2080 (setq buffer-read-only t)
2081 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2082 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2083 'gdb-invalidate-breakpoints
2084 'gdbmi-invalidate-breakpoints))
2086 (defun gdb-toggle-breakpoint ()
2087 "Enable/disable breakpoint at current line."
2090 (beginning-of-line 1)
2091 (if (looking-at gdb-breakpoint-regexp)
2094 (concat gdb-server-prefix
2095 (if (eq ?y (char-after (match-beginning 3)))
2098 (or (match-string 1) (match-string 2)) "\n") 'ignore))
2099 (error "Not recognized as break/watchpoint line"))))
2101 (defun gdb-delete-breakpoint ()
2102 "Delete the breakpoint at current line."
2105 (beginning-of-line 1)
2106 (if (looking-at gdb-breakpoint-regexp)
2107 (if (match-string 1)
2110 (concat gdb-server-prefix "delete " (match-string 1) "\n")
2112 (message-box "This breakpoint cannot be deleted on its own."))
2113 (error "Not recognized as break/watchpoint line"))))
2115 (defun gdb-goto-breakpoint (&optional event)
2116 "Display the breakpoint location specified at current line."
2117 (interactive (list last-input-event))
2118 (if event (posn-set-point (event-end event)))
2120 (beginning-of-line 1)
2121 (if (looking-at "\\([0-9]+\\.?[0-9]*\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
2122 (let ((bptno (match-string 1))
2123 (file (match-string 2))
2124 (line (match-string 3)))
2125 (save-selected-window
2126 (let* ((buffer (find-file-noselect
2127 (if (file-exists-p file) file
2128 (cdr (assoc bptno gdb-location-alist)))))
2129 (window (or (gdb-display-source-buffer buffer)
2130 (display-buffer buffer))))
2131 (setq gdb-source-window window)
2132 (with-current-buffer buffer
2133 (goto-line (string-to-number line))
2134 (set-window-point window (point))))))
2135 (error "No location specified."))))
2138 ;; Frames buffer. This displays a perpetually correct bactracktrace
2139 ;; (from the command `where').
2141 ;; Alas, if your stack is deep, it is costly.
2143 (defcustom gdb-max-frames 40
2144 "Maximum number of frames displayed in call stack."
2149 (gdb-set-buffer-rules 'gdb-stack-buffer
2150 'gdb-stack-buffer-name
2153 (def-gdb-auto-updated-buffer gdb-stack-buffer
2154 gdb-invalidate-frames
2155 (concat "server info stack " (number-to-string gdb-max-frames) "\n")
2156 gdb-info-stack-handler
2157 gdb-info-stack-custom)
2159 (defun gdb-info-stack-custom ()
2160 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2163 (unless (eq gdb-look-up-stack 'delete)
2164 (let ((buffer-read-only nil)
2166 (goto-char (point-min))
2167 (while (< (point) (point-max))
2168 (setq bl (line-beginning-position)
2169 el (line-end-position))
2170 (when (looking-at "#")
2171 (add-text-properties bl el
2172 '(mouse-face highlight
2173 help-echo "mouse-2, RET: Select frame")))
2175 (when (looking-at "^#\\([0-9]+\\)")
2176 (when (string-equal (match-string 1) gdb-frame-number)
2177 (if (> (car (window-fringes)) 0)
2179 (or gdb-stack-position
2180 (setq gdb-stack-position (make-marker)))
2181 (set-marker gdb-stack-position (point))
2182 (setq move-to gdb-stack-position))
2183 (put-text-property bl (+ bl 4)
2184 'face '(:inverse-video t))
2186 (when (re-search-forward
2188 (if (string-equal (match-string 1) "0") "" " in ")
2189 "\\([^ ]+\\) (") el t)
2190 (put-text-property (match-beginning 1) (match-end 1)
2191 'face font-lock-function-name-face)
2192 (setq bl (match-end 0))
2193 (while (re-search-forward "<\\([^>]+\\)>" el t)
2194 (put-text-property (match-beginning 1) (match-end 1)
2195 'face font-lock-function-name-face))
2197 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2198 (put-text-property (match-beginning 1) (match-end 1)
2199 'face font-lock-variable-name-face))))
2202 (when (looking-at "(More stack frames follow...)")
2203 (add-text-properties (match-beginning 0) (match-end 0)
2204 '(mouse-face highlight
2207 "mouse-2, RET: customize gdb-max-frames to see more frames")))))
2208 (when gdb-look-up-stack
2209 (goto-char (point-min))
2210 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2211 (let ((start (line-beginning-position))
2212 (file (match-string 1))
2213 (line (match-string 2)))
2214 (re-search-backward "^#*\\([0-9]+\\)" start t)
2216 (list (concat gdb-server-prefix "frame "
2217 (match-string 1) "\n") 'gdb-set-hollow))
2219 (list (concat gdb-server-prefix "frame 0\n") 'ignore))))))
2221 (let ((window (get-buffer-window (current-buffer) 0)))
2223 (with-selected-window window
2225 (unless (pos-visible-in-window-p)
2226 (recenter '(center)))))))))
2227 (if (eq gdb-look-up-stack 'delete)
2228 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2229 (setq gdb-look-up-stack nil))
2231 (defun gdb-set-hollow ()
2232 (if gud-last-last-frame
2233 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2234 (setq fringe-indicator-alist
2235 '((overlay-arrow . hollow-right-triangle))))))
2237 (defun gdb-stack-buffer-name ()
2238 (with-current-buffer gud-comint-buffer
2239 (concat "*stack frames of " (gdb-get-target-string) "*")))
2241 (defun gdb-display-stack-buffer ()
2242 "Display backtrace of current stack."
2245 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2247 (defun gdb-frame-stack-buffer ()
2248 "Display backtrace of current stack in a new frame."
2250 (let ((special-display-regexps (append special-display-regexps '(".*")))
2251 (special-display-frame-alist gdb-frame-parameters))
2252 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2254 (defvar gdb-frames-mode-map
2255 (let ((map (make-sparse-keymap)))
2256 (suppress-keymap map)
2257 (define-key map "q" 'kill-this-buffer)
2258 (define-key map "\r" 'gdb-frames-select)
2259 (define-key map [mouse-2] 'gdb-frames-select)
2260 (define-key map [follow-link] 'mouse-face)
2263 (defun gdb-frames-mode ()
2264 "Major mode for gdb call stack.
2266 \\{gdb-frames-mode-map}"
2267 (kill-all-local-variables)
2268 (setq major-mode 'gdb-frames-mode)
2269 (setq mode-name "Frames")
2270 (setq gdb-stack-position nil)
2271 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2272 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2273 (setq buffer-read-only t)
2274 (use-local-map gdb-frames-mode-map)
2275 (run-mode-hooks 'gdb-frames-mode-hook)
2276 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2277 'gdb-invalidate-frames
2278 'gdbmi-invalidate-frames))
2280 (defun gdb-get-frame-number ()
2283 (let* ((start (line-beginning-position))
2284 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2285 (n (or (and pos (match-string 1)) "0")))
2288 (defun gdb-frames-select (&optional event)
2289 "Select the frame and display the relevant source."
2290 (interactive (list last-input-event))
2291 (if event (posn-set-point (event-end event)))
2292 (if (get-text-property (point) 'gdb-max-frames)
2294 (message-box "After setting gdb-max-frames, you need to enter\n\
2295 another GDB command e.g pwd, to see new frames")
2296 (customize-variable-other-window 'gdb-max-frames))
2298 (list (concat gdb-server-prefix "frame "
2299 (gdb-get-frame-number) "\n") 'ignore))))
2302 ;; Threads buffer. This displays a selectable thread list.
2304 (gdb-set-buffer-rules 'gdb-threads-buffer
2305 'gdb-threads-buffer-name
2308 (def-gdb-auto-updated-buffer gdb-threads-buffer
2309 gdb-invalidate-threads
2310 (concat gdb-server-prefix "info threads\n")
2311 gdb-info-threads-handler
2312 gdb-info-threads-custom)
2314 (defun gdb-info-threads-custom ()
2315 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2316 (let ((buffer-read-only nil))
2318 (goto-char (point-min))
2319 (while (< (point) (point-max))
2320 (unless (looking-at "No ")
2321 (add-text-properties (line-beginning-position) (line-end-position)
2322 '(mouse-face highlight
2323 help-echo "mouse-2, RET: select thread")))
2324 (forward-line 1))))))
2326 (defun gdb-threads-buffer-name ()
2327 (with-current-buffer gud-comint-buffer
2328 (concat "*threads of " (gdb-get-target-string) "*")))
2330 (defun gdb-display-threads-buffer ()
2331 "Display IDs of currently known threads."
2334 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2336 (defun gdb-frame-threads-buffer ()
2337 "Display IDs of currently known threads in a new frame."
2339 (let ((special-display-regexps (append special-display-regexps '(".*")))
2340 (special-display-frame-alist gdb-frame-parameters))
2341 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2343 (defvar gdb-threads-mode-map
2344 (let ((map (make-sparse-keymap)))
2345 (suppress-keymap map)
2346 (define-key map "q" 'kill-this-buffer)
2347 (define-key map "\r" 'gdb-threads-select)
2348 (define-key map [mouse-2] 'gdb-threads-select)
2349 (define-key map [follow-link] 'mouse-face)
2352 (defvar gdb-threads-font-lock-keywords
2353 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2354 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2355 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2356 "Font lock keywords used in `gdb-threads-mode'.")
2358 (defun gdb-threads-mode ()
2359 "Major mode for gdb threads.
2361 \\{gdb-threads-mode-map}"
2362 (kill-all-local-variables)
2363 (setq major-mode 'gdb-threads-mode)
2364 (setq mode-name "Threads")
2365 (setq buffer-read-only t)
2366 (use-local-map gdb-threads-mode-map)
2367 (set (make-local-variable 'font-lock-defaults)
2368 '(gdb-threads-font-lock-keywords))
2369 (run-mode-hooks 'gdb-threads-mode-hook)
2370 'gdb-invalidate-threads)
2372 (defun gdb-get-thread-number ()
2374 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2375 (match-string-no-properties 1)))
2377 (defun gdb-threads-select (&optional event)
2378 "Select the thread and display the relevant source."
2379 (interactive (list last-input-event))
2380 (if event (posn-set-point (event-end event)))
2382 (list (concat gdb-server-prefix "thread "
2383 (gdb-get-thread-number) "\n") 'ignore))
2384 (gud-display-frame))
2387 ;; Registers buffer.
2389 (defcustom gdb-all-registers nil
2390 "Non-nil means include floating-point registers."
2395 (gdb-set-buffer-rules 'gdb-registers-buffer
2396 'gdb-registers-buffer-name
2397 'gdb-registers-mode)
2399 (def-gdb-auto-updated-buffer gdb-registers-buffer
2400 gdb-invalidate-registers
2402 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2403 gdb-info-registers-handler
2404 gdb-info-registers-custom)
2406 (defun gdb-info-registers-custom ()
2407 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2409 (let ((buffer-read-only nil)
2411 (goto-char (point-min))
2412 (while (< (point) (point-max))
2413 (setq start (line-beginning-position))
2414 (setq end (line-end-position))
2415 (when (looking-at "^[^ ]+")
2416 (unless (string-equal (match-string 0) "The")
2417 (put-text-property start (match-end 0)
2418 'face font-lock-variable-name-face)
2419 (add-text-properties start end
2420 '(help-echo "mouse-2: edit value"
2421 mouse-face highlight))))
2422 (forward-line 1))))))
2424 (defun gdb-edit-register-value (&optional event)
2425 (interactive (list last-input-event))
2427 (if event (posn-set-point (event-end event)))
2429 (let* ((register (current-word))
2430 (value (read-string (format "New value (%s): " register))))
2432 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2435 (defvar gdb-registers-mode-map
2436 (let ((map (make-sparse-keymap)))
2437 (suppress-keymap map)
2438 (define-key map "\r" 'gdb-edit-register-value)
2439 (define-key map [mouse-2] 'gdb-edit-register-value)
2440 (define-key map " " 'gdb-all-registers)
2441 (define-key map "q" 'kill-this-buffer)
2444 (defun gdb-registers-mode ()
2445 "Major mode for gdb registers.
2447 \\{gdb-registers-mode-map}"
2448 (kill-all-local-variables)
2449 (setq major-mode 'gdb-registers-mode)
2450 (setq mode-name "Registers")
2451 (setq buffer-read-only t)
2452 (use-local-map gdb-registers-mode-map)
2453 (run-mode-hooks 'gdb-registers-mode-hook)
2454 (if (string-equal gdb-version "pre-6.4")
2456 (if gdb-all-registers (setq mode-name "Registers:All"))
2457 'gdb-invalidate-registers)
2458 'gdb-invalidate-registers-1))
2460 (defun gdb-registers-buffer-name ()
2461 (with-current-buffer gud-comint-buffer
2462 (concat "*registers of " (gdb-get-target-string) "*")))
2464 (defun gdb-display-registers-buffer ()
2465 "Display integer register contents."
2468 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2470 (defun gdb-frame-registers-buffer ()
2471 "Display integer register contents in a new frame."
2473 (let ((special-display-regexps (append special-display-regexps '(".*")))
2474 (special-display-frame-alist gdb-frame-parameters))
2475 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2477 (defun gdb-all-registers ()
2478 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2480 (when (string-equal gdb-version "pre-6.4")
2481 (if gdb-all-registers
2483 (setq gdb-all-registers nil)
2484 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2485 (setq mode-name "Registers")))
2486 (setq gdb-all-registers t)
2487 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2488 (setq mode-name "Registers:All")))
2489 (message (format "Display of floating-point registers %sabled"
2490 (if gdb-all-registers "en" "dis")))
2491 (gdb-invalidate-registers)))
2496 (defcustom gdb-memory-repeat-count 32
2497 "Number of data items in memory window."
2502 (defcustom gdb-memory-format "x"
2503 "Display format of data items in memory window."
2504 :type '(choice (const :tag "Hexadecimal" "x")
2505 (const :tag "Signed decimal" "d")
2506 (const :tag "Unsigned decimal" "u")
2507 (const :tag "Octal" "o")
2508 (const :tag "Binary" "t"))
2512 (defcustom gdb-memory-unit "w"
2513 "Unit size of data items in memory window."
2514 :type '(choice (const :tag "Byte" "b")
2515 (const :tag "Halfword" "h")
2516 (const :tag "Word" "w")
2517 (const :tag "Giant word" "g"))
2521 (gdb-set-buffer-rules 'gdb-memory-buffer
2522 'gdb-memory-buffer-name
2525 (def-gdb-auto-updated-buffer gdb-memory-buffer
2526 gdb-invalidate-memory
2527 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2528 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2529 gdb-read-memory-handler
2530 gdb-read-memory-custom)
2532 (defun gdb-read-memory-custom ()
2534 (goto-char (point-min))
2535 (if (looking-at "0x[[:xdigit:]]+")
2536 (setq gdb-memory-address (match-string 0)))))
2538 (defvar gdb-memory-mode-map
2539 (let ((map (make-sparse-keymap)))
2540 (suppress-keymap map)
2541 (define-key map "q" 'kill-this-buffer)
2544 (defun gdb-memory-set-address (event)
2545 "Set the start memory address."
2547 (save-selected-window
2548 (select-window (posn-window (event-start event)))
2549 (let ((arg (read-from-minibuffer "Memory address: ")))
2550 (setq gdb-memory-address arg))
2551 (gdb-invalidate-memory)))
2553 (defun gdb-memory-set-repeat-count (event)
2554 "Set the number of data items in memory window."
2556 (save-selected-window
2557 (select-window (posn-window (event-start event)))
2558 (let* ((arg (read-from-minibuffer "Repeat count: "))
2559 (count (string-to-number arg)))
2561 (error "Positive numbers only")
2562 (customize-set-variable 'gdb-memory-repeat-count count)
2563 (gdb-invalidate-memory)))))
2565 (defun gdb-memory-format-binary ()
2566 "Set the display format to binary."
2568 (customize-set-variable 'gdb-memory-format "t")
2569 (gdb-invalidate-memory))
2571 (defun gdb-memory-format-octal ()
2572 "Set the display format to octal."
2574 (customize-set-variable 'gdb-memory-format "o")
2575 (gdb-invalidate-memory))
2577 (defun gdb-memory-format-unsigned ()
2578 "Set the display format to unsigned decimal."
2580 (customize-set-variable 'gdb-memory-format "u")
2581 (gdb-invalidate-memory))
2583 (defun gdb-memory-format-signed ()
2584 "Set the display format to decimal."
2586 (customize-set-variable 'gdb-memory-format "d")
2587 (gdb-invalidate-memory))
2589 (defun gdb-memory-format-hexadecimal ()
2590 "Set the display format to hexadecimal."
2592 (customize-set-variable 'gdb-memory-format "x")
2593 (gdb-invalidate-memory))
2595 (defvar gdb-memory-format-map
2596 (let ((map (make-sparse-keymap)))
2597 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2599 "Keymap to select format in the header line.")
2601 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2602 "Menu of display formats in the header line.")
2604 (define-key gdb-memory-format-menu [binary]
2605 '(menu-item "Binary" gdb-memory-format-binary
2606 :button (:radio . (equal gdb-memory-format "t"))))
2607 (define-key gdb-memory-format-menu [octal]
2608 '(menu-item "Octal" gdb-memory-format-octal
2609 :button (:radio . (equal gdb-memory-format "o"))))
2610 (define-key gdb-memory-format-menu [unsigned]
2611 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2612 :button (:radio . (equal gdb-memory-format "u"))))
2613 (define-key gdb-memory-format-menu [signed]
2614 '(menu-item "Signed Decimal" gdb-memory-format-signed
2615 :button (:radio . (equal gdb-memory-format "d"))))
2616 (define-key gdb-memory-format-menu [hexadecimal]
2617 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2618 :button (:radio . (equal gdb-memory-format "x"))))
2620 (defun gdb-memory-format-menu (event)
2622 (x-popup-menu event gdb-memory-format-menu))
2624 (defun gdb-memory-format-menu-1 (event)
2626 (save-selected-window
2627 (select-window (posn-window (event-start event)))
2628 (let* ((selection (gdb-memory-format-menu event))
2629 (binding (and selection (lookup-key gdb-memory-format-menu
2630 (vector (car selection))))))
2631 (if binding (call-interactively binding)))))
2633 (defun gdb-memory-unit-giant ()
2634 "Set the unit size to giant words (eight bytes)."
2636 (customize-set-variable 'gdb-memory-unit "g")
2637 (gdb-invalidate-memory))
2639 (defun gdb-memory-unit-word ()
2640 "Set the unit size to words (four bytes)."
2642 (customize-set-variable 'gdb-memory-unit "w")
2643 (gdb-invalidate-memory))
2645 (defun gdb-memory-unit-halfword ()
2646 "Set the unit size to halfwords (two bytes)."
2648 (customize-set-variable 'gdb-memory-unit "h")
2649 (gdb-invalidate-memory))
2651 (defun gdb-memory-unit-byte ()
2652 "Set the unit size to bytes."
2654 (customize-set-variable 'gdb-memory-unit "b")
2655 (gdb-invalidate-memory))
2657 (defvar gdb-memory-unit-map
2658 (let ((map (make-sparse-keymap)))
2659 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2661 "Keymap to select units in the header line.")
2663 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2664 "Menu of units in the header line.")
2666 (define-key gdb-memory-unit-menu [giantwords]
2667 '(menu-item "Giant words" gdb-memory-unit-giant
2668 :button (:radio . (equal gdb-memory-unit "g"))))
2669 (define-key gdb-memory-unit-menu [words]
2670 '(menu-item "Words" gdb-memory-unit-word
2671 :button (:radio . (equal gdb-memory-unit "w"))))
2672 (define-key gdb-memory-unit-menu [halfwords]
2673 '(menu-item "Halfwords" gdb-memory-unit-halfword
2674 :button (:radio . (equal gdb-memory-unit "h"))))
2675 (define-key gdb-memory-unit-menu [bytes]
2676 '(menu-item "Bytes" gdb-memory-unit-byte
2677 :button (:radio . (equal gdb-memory-unit "b"))))
2679 (defun gdb-memory-unit-menu (event)
2681 (x-popup-menu event gdb-memory-unit-menu))
2683 (defun gdb-memory-unit-menu-1 (event)
2685 (save-selected-window
2686 (select-window (posn-window (event-start event)))
2687 (let* ((selection (gdb-memory-unit-menu event))
2688 (binding (and selection (lookup-key gdb-memory-unit-menu
2689 (vector (car selection))))))
2690 (if binding (call-interactively binding)))))
2692 ;;from make-mode-line-mouse-map
2693 (defun gdb-make-header-line-mouse-map (mouse function) "\
2694 Return a keymap with single entry for mouse key MOUSE on the header line.
2695 MOUSE is defined to run function FUNCTION with no args in the buffer
2696 corresponding to the mode line clicked."
2697 (let ((map (make-sparse-keymap)))
2698 (define-key map (vector 'header-line mouse) function)
2699 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2702 (defvar gdb-memory-font-lock-keywords
2703 '(;; <__function.name+n>
2704 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2706 "Font lock keywords used in `gdb-memory-mode'.")
2708 (defun gdb-memory-mode ()
2709 "Major mode for examining memory.
2711 \\{gdb-memory-mode-map}"
2712 (kill-all-local-variables)
2713 (setq major-mode 'gdb-memory-mode)
2714 (setq mode-name "Memory")
2715 (setq buffer-read-only t)
2716 (use-local-map gdb-memory-mode-map)
2717 (setq header-line-format
2723 'face font-lock-warning-face
2724 'help-echo "mouse-1: decrement address"
2725 'mouse-face 'mode-line-highlight
2727 (gdb-make-header-line-mouse-map
2729 (lambda () (interactive)
2730 (let ((gdb-memory-address
2731 ;; Let GDB do the arithmetic.
2733 gdb-memory-address " - "
2735 (* gdb-memory-repeat-count
2736 (cond ((string= gdb-memory-unit "b") 1)
2737 ((string= gdb-memory-unit "h") 2)
2738 ((string= gdb-memory-unit "w") 4)
2739 ((string= gdb-memory-unit "g") 8)))))))
2740 (gdb-invalidate-memory)))))
2743 'face font-lock-warning-face
2744 'help-echo "mouse-1: increment address"
2745 'mouse-face 'mode-line-highlight
2746 'local-map (gdb-make-header-line-mouse-map
2748 (lambda () (interactive)
2749 (let ((gdb-memory-address nil))
2750 (gdb-invalidate-memory)))))
2752 (propertize gdb-memory-address
2753 'face font-lock-warning-face
2754 'help-echo "mouse-1: set memory address"
2755 'mouse-face 'mode-line-highlight
2756 'local-map (gdb-make-header-line-mouse-map
2758 #'gdb-memory-set-address))
2760 (propertize (number-to-string gdb-memory-repeat-count)
2761 'face font-lock-warning-face
2762 'help-echo "mouse-1: set repeat count"
2763 'mouse-face 'mode-line-highlight
2764 'local-map (gdb-make-header-line-mouse-map
2766 #'gdb-memory-set-repeat-count))
2768 (propertize gdb-memory-format
2769 'face font-lock-warning-face
2770 'help-echo "mouse-3: select display format"
2771 'mouse-face 'mode-line-highlight
2772 'local-map gdb-memory-format-map)
2774 (propertize gdb-memory-unit
2775 'face font-lock-warning-face
2776 'help-echo "mouse-3: select unit size"
2777 'mouse-face 'mode-line-highlight
2778 'local-map gdb-memory-unit-map))))
2779 (set (make-local-variable 'font-lock-defaults)
2780 '(gdb-memory-font-lock-keywords))
2781 (run-mode-hooks 'gdb-memory-mode-hook)
2782 'gdb-invalidate-memory)
2784 (defun gdb-memory-buffer-name ()
2785 (with-current-buffer gud-comint-buffer
2786 (concat "*memory of " (gdb-get-target-string) "*")))
2788 (defun gdb-display-memory-buffer ()
2789 "Display memory contents."
2792 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2794 (defun gdb-frame-memory-buffer ()
2795 "Display memory contents in a new frame."
2797 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2798 (special-display-frame-alist
2799 (cons '(left-fringe . 0)
2800 (cons '(right-fringe . 0)
2801 (cons '(width . 83) gdb-frame-parameters)))))
2802 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2807 (gdb-set-buffer-rules 'gdb-locals-buffer
2808 'gdb-locals-buffer-name
2811 (def-gdb-auto-update-trigger gdb-invalidate-locals
2812 (gdb-get-buffer 'gdb-locals-buffer)
2813 "server info locals\n"
2814 gdb-info-locals-handler)
2816 (defvar gdb-locals-watch-map
2817 (let ((map (make-sparse-keymap)))
2818 (suppress-keymap map)
2819 (define-key map "\r" (lambda () (interactive)
2822 (define-key map [mouse-2] (lambda (event) (interactive "e")
2823 (mouse-set-point event)
2827 "Keymap to create watch expression of a complex data type local variable.")
2829 (defconst gdb-struct-string
2830 (concat (propertize "[struct/union]"
2831 'mouse-face 'highlight
2832 'help-echo "mouse-2: create watch expression"
2833 'local-map gdb-locals-watch-map) "\n"))
2835 (defconst gdb-array-string
2836 (concat " " (propertize "[array]"
2837 'mouse-face 'highlight
2838 'help-echo "mouse-2: create watch expression"
2839 'local-map gdb-locals-watch-map) "\n"))
2841 ;; Abbreviate for arrays and structures.
2842 ;; These can be expanded using gud-display.
2843 (defun gdb-info-locals-handler ()
2844 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2845 gdb-pending-triggers))
2846 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2847 (with-current-buffer buf
2848 (goto-char (point-min))
2849 (while (re-search-forward "^[ }].*\n" nil t)
2850 (replace-match "" nil nil))
2851 (goto-char (point-min))
2852 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2853 (replace-match gdb-struct-string nil nil))
2854 (goto-char (point-min))
2855 (while (re-search-forward "\\s-*{.*\n" nil t)
2856 (replace-match gdb-array-string nil nil))))
2857 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2859 (with-current-buffer buf
2860 (let* ((window (get-buffer-window buf 0))
2861 (start (window-start window))
2862 (p (window-point window))
2863 (buffer-read-only nil))
2865 (insert-buffer-substring (gdb-get-buffer-create
2866 'gdb-partial-output-buffer))
2867 (set-window-start window start)
2868 (set-window-point window p))
2870 (run-hooks 'gdb-info-locals-hook))
2872 (defvar gdb-locals-mode-map
2873 (let ((map (make-sparse-keymap)))
2874 (suppress-keymap map)
2875 (define-key map "q" 'kill-this-buffer)
2878 (defun gdb-locals-mode ()
2879 "Major mode for gdb locals.
2881 \\{gdb-locals-mode-map}"
2882 (kill-all-local-variables)
2883 (setq major-mode 'gdb-locals-mode)
2884 (setq mode-name (concat "Locals:" gdb-selected-frame))
2885 (setq buffer-read-only t)
2886 (use-local-map gdb-locals-mode-map)
2887 (set (make-local-variable 'font-lock-defaults)
2888 '(gdb-locals-font-lock-keywords))
2889 (run-mode-hooks 'gdb-locals-mode-hook)
2890 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2891 (string-equal gdb-version "pre-6.4"))
2892 'gdb-invalidate-locals
2893 'gdb-invalidate-locals-1))
2895 (defun gdb-locals-buffer-name ()
2896 (with-current-buffer gud-comint-buffer
2897 (concat "*locals of " (gdb-get-target-string) "*")))
2899 (defun gdb-display-locals-buffer ()
2900 "Display local variables of current stack and their values."
2903 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2905 (defun gdb-frame-locals-buffer ()
2906 "Display local variables of current stack and their values in a new frame."
2908 (let ((special-display-regexps (append special-display-regexps '(".*")))
2909 (special-display-frame-alist gdb-frame-parameters))
2910 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2913 ;;;; Window management
2914 (defun gdb-display-buffer (buf dedicated &optional size)
2915 (let ((answer (get-buffer-window buf 0))
2918 (display-buffer buf nil 0) ;Deiconify the frame if necessary.
2919 ;; The buffer is not yet displayed.
2920 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2921 (let ((window (get-lru-window)))
2923 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2924 ,gdb-source-window))))
2926 (set-window-buffer window buf)
2927 (setq answer window))
2928 (setq must-split t)))
2930 (let* ((largest (get-largest-window))
2931 (cur-size (window-height largest))
2932 (new-size (and size (< size cur-size) (- cur-size size))))
2933 (setq answer (split-window largest new-size))
2934 (set-window-buffer answer buf)
2935 (set-window-dedicated-p answer dedicated)))
2939 ;;; Shared keymap initialization:
2941 (let ((menu (make-sparse-keymap "GDB-Windows")))
2942 (define-key gud-menu-map [displays]
2943 `(menu-item "GDB-Windows" ,menu
2944 :visible (memq gud-minor-mode '(gdbmi gdba))))
2945 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2946 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2947 (define-key menu [inferior]
2948 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2949 :enable gdb-use-separate-io-buffer))
2950 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2951 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2952 (define-key menu [disassembly]
2953 '("Disassembly" . gdb-display-assembler-buffer))
2954 (define-key menu [breakpoints]
2955 '("Breakpoints" . gdb-display-breakpoints-buffer))
2956 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2957 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2959 (let ((menu (make-sparse-keymap "GDB-Frames")))
2960 (define-key gud-menu-map [frames]
2961 `(menu-item "GDB-Frames" ,menu
2962 :visible (memq gud-minor-mode '(gdbmi gdba))))
2963 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2964 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2965 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2966 (define-key menu [inferior]
2967 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2968 :enable gdb-use-separate-io-buffer))
2969 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2970 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2971 (define-key menu [breakpoints]
2972 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2973 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2974 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2976 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2977 (define-key gud-menu-map [ui]
2978 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2979 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2980 (define-key menu [gdb-find-source-frame]
2981 '(menu-item "Look For Source Frame" gdb-find-source-frame
2982 :visible (eq gud-minor-mode 'gdba)
2983 :help "Toggle look for source frame."
2984 :button (:toggle . gdb-find-source-frame)))
2985 (define-key menu [gdb-use-separate-io]
2986 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2987 :visible (eq gud-minor-mode 'gdba)
2988 :help "Toggle separate IO for debugged program."
2989 :button (:toggle . gdb-use-separate-io-buffer)))
2990 (define-key menu [gdb-many-windows]
2991 '(menu-item "Display Other Windows" gdb-many-windows
2992 :help "Toggle display of locals, stack and breakpoint information"
2993 :button (:toggle . gdb-many-windows)))
2994 (define-key menu [gdb-restore-windows]
2995 '(menu-item "Restore Window Layout" gdb-restore-windows
2996 :help "Restore standard layout for debug session.")))
2998 (defun gdb-frame-gdb-buffer ()
2999 "Display GUD buffer in a new frame."
3001 (let ((special-display-regexps (append special-display-regexps '(".*")))
3002 (special-display-frame-alist
3003 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3004 gdb-frame-parameters)))
3005 (same-window-regexps nil))
3006 (display-buffer gud-comint-buffer)))
3008 (defun gdb-display-gdb-buffer ()
3009 "Display GUD buffer."
3011 (let ((same-window-regexps nil))
3012 (pop-to-buffer gud-comint-buffer)))
3014 (defun gdb-set-window-buffer (name)
3015 (set-window-buffer (selected-window) (get-buffer name))
3016 (set-window-dedicated-p (selected-window) t))
3018 (defun gdb-setup-windows ()
3019 "Layout the window pattern for `gdb-many-windows'."
3020 (gdb-display-locals-buffer)
3021 (gdb-display-stack-buffer)
3022 (delete-other-windows)
3023 (gdb-display-breakpoints-buffer)
3024 (delete-other-windows)
3026 (pop-to-buffer gud-comint-buffer)
3027 (split-window nil ( / ( * (window-height) 3) 4))
3028 (split-window nil ( / (window-height) 3))
3029 (split-window-horizontally)
3031 (gdb-set-window-buffer (gdb-locals-buffer-name))
3034 (if gud-last-last-frame
3035 (gud-find-file (car gud-last-last-frame))
3037 (gud-find-file gdb-main-file)
3038 ;; Put buffer list in window if we
3039 ;; can't find a source file.
3040 (list-buffers-noselect))))
3041 (setq gdb-source-window (selected-window))
3042 (when gdb-use-separate-io-buffer
3043 (split-window-horizontally)
3045 (gdb-set-window-buffer
3046 (gdb-get-buffer-create 'gdb-inferior-io)))
3048 (gdb-set-window-buffer (gdb-stack-buffer-name))
3049 (split-window-horizontally)
3051 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3054 (defun gdb-restore-windows ()
3055 "Restore the basic arrangement of windows used by gdba.
3056 This arrangement depends on the value of `gdb-many-windows'."
3058 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3059 (delete-other-windows)
3060 (if gdb-many-windows
3062 (when (or gud-last-last-frame gdb-show-main)
3066 (if gud-last-last-frame
3067 (gud-find-file (car gud-last-last-frame))
3068 (gud-find-file gdb-main-file)))
3069 (setq gdb-source-window (selected-window))
3073 "Exit a debugging session cleanly.
3074 Kills the gdb buffers, and resets variables and the source buffers."
3075 (dolist (buffer (buffer-list))
3076 (unless (eq buffer gud-comint-buffer)
3077 (with-current-buffer buffer
3078 (if (memq gud-minor-mode '(gdbmi gdba))
3079 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3081 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3082 (setq gud-minor-mode nil)
3083 (kill-local-variable 'tool-bar-map)
3084 (kill-local-variable 'gdb-define-alist))))))
3085 (setq gdb-overlay-arrow-position nil)
3086 (setq overlay-arrow-variable-list
3087 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3088 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3089 (setq gdb-stack-position nil)
3090 (setq overlay-arrow-variable-list
3091 (delq 'gdb-stack-position overlay-arrow-variable-list))
3092 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3093 (setq gud-running nil)
3094 (setq gdb-active-process nil)
3095 (setq gdb-var-list nil)
3096 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3098 (defun gdb-source-info ()
3099 "Find the source file where the program starts and displays it with related
3101 (goto-char (point-min))
3102 (if (and (search-forward "Located in " nil t)
3103 (looking-at "\\S-+"))
3104 (setq gdb-main-file (match-string 0)))
3105 (goto-char (point-min))
3106 (if (search-forward "Includes preprocessor macro info." nil t)
3107 (setq gdb-macro-info t))
3108 (if gdb-many-windows
3110 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3112 (let ((pop-up-windows t))
3113 (display-buffer (gud-find-file gdb-main-file)))))
3116 (defun gdb-get-location (bptno line flag)
3117 "Find the directory containing the relevant source file.
3118 Put in buffer and place breakpoint icon."
3119 (goto-char (point-min))
3120 (catch 'file-not-found
3121 (if (search-forward "Located in " nil t)
3122 (when (looking-at "\\S-+")
3123 (delete (cons bptno "File not found") gdb-location-alist)
3124 (push (cons bptno (match-string 0)) gdb-location-alist))
3126 (unless (assoc bptno gdb-location-alist)
3127 (push (cons bptno "File not found") gdb-location-alist)
3128 (message-box "Cannot find source file for breakpoint location.\n\
3129 Add directory to search path for source files using the GDB command, dir."))
3130 (throw 'file-not-found nil))
3131 (with-current-buffer
3132 (find-file-noselect (match-string 0))
3133 (save-current-buffer
3134 (set (make-local-variable 'gud-minor-mode) 'gdba)
3135 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
3136 ;; only want one breakpoint icon at each location
3138 (goto-line (string-to-number line))
3139 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
3141 (add-hook 'find-file-hook 'gdb-find-file-hook)
3143 (defun gdb-find-file-hook ()
3144 "Set up buffer for debugging if file is part of the source code
3145 of the current session."
3146 (if (and (buffer-name gud-comint-buffer)
3147 ;; in case gud or gdb-ui is just loaded
3149 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3151 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
3152 (if (member (if (string-equal gdb-version "pre-6.4")
3153 (file-name-nondirectory buffer-file-name)
3155 gdb-source-file-list)
3156 (with-current-buffer (find-buffer-visiting buffer-file-name)
3157 (set (make-local-variable 'gud-minor-mode)
3158 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3159 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3162 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3163 "Put string PUTSTRING in front of POS in the current buffer.
3164 PUTSTRING is displayed by putting an overlay into the current buffer with a
3165 `before-string' string that has a `display' property whose value is
3167 (let ((string (make-string 1 ?x))
3168 (buffer (current-buffer)))
3169 (setq putstring (copy-sequence putstring))
3170 (let ((overlay (make-overlay pos pos buffer))
3172 (list (list 'margin 'left-margin) putstring))))
3173 (put-text-property 0 1 'display prop string)
3175 (add-text-properties 0 1 sprops string))
3176 (overlay-put overlay 'put-break t)
3177 (overlay-put overlay 'before-string string))))
3179 ;;from remove-images
3180 (defun gdb-remove-strings (start end &optional buffer)
3181 "Remove strings between START and END in BUFFER.
3182 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3183 BUFFER nil or omitted means use the current buffer."
3185 (setq buffer (current-buffer)))
3186 (dolist (overlay (overlays-in start end))
3187 (when (overlay-get overlay 'put-break)
3188 (delete-overlay overlay))))
3190 (defun gdb-put-breakpoint-icon (enabled bptno)
3191 (if (string-match "[0-9+]+\\." bptno)
3192 (setq enabled gdb-parent-bptno-enabled))
3193 (let ((start (- (line-beginning-position) 1))
3194 (end (+ (line-end-position) 1))
3195 (putstring (if enabled "B" "b"))
3196 (source-window (get-buffer-window (current-buffer) 0)))
3197 (add-text-properties
3198 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3201 (add-text-properties
3202 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3203 (add-text-properties
3204 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3205 (gdb-remove-breakpoint-icons start end)
3206 (if (display-images-p)
3207 (if (>= (or left-fringe-width
3208 (if source-window (car (window-fringes source-window)))
3209 gdb-buffer-fringe-width) 8)
3212 `(left-fringe breakpoint
3215 'breakpoint-disabled))
3217 'gdb-enabled enabled)
3218 (when (< left-margin-width 2)
3219 (save-current-buffer
3220 (setq left-margin-width 2)
3224 left-margin-width right-margin-width))))
3227 (or breakpoint-enabled-icon
3228 (setq breakpoint-enabled-icon
3229 (find-image `((:type xpm :data
3230 ,breakpoint-xpm-data
3231 :ascent 100 :pointer hand)
3233 ,breakpoint-enabled-pbm-data
3234 :ascent 100 :pointer hand)))))
3235 (or breakpoint-disabled-icon
3236 (setq breakpoint-disabled-icon
3237 (find-image `((:type xpm :data
3238 ,breakpoint-xpm-data
3239 :conversion disabled
3240 :ascent 100 :pointer hand)
3242 ,breakpoint-disabled-pbm-data
3243 :ascent 100 :pointer hand))))))
3247 (when (< left-margin-width 2)
3248 (save-current-buffer
3249 (setq left-margin-width 2)
3250 (let ((window (get-buffer-window (current-buffer) 0)))
3253 window left-margin-width right-margin-width)))))
3255 (propertize putstring
3256 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3259 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3260 (gdb-remove-strings start end)
3261 (if (display-images-p)
3262 (remove-images start end))
3264 (setq left-margin-width 0)
3265 (let ((window (get-buffer-window (current-buffer) 0)))
3268 window left-margin-width right-margin-width)))))
3272 ;; Assembler buffer.
3274 (gdb-set-buffer-rules 'gdb-assembler-buffer
3275 'gdb-assembler-buffer-name
3276 'gdb-assembler-mode)
3278 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3279 ;; window-start but keep the overlay arrow/current line visible.
3280 (defun gdb-assembler-handler ()
3281 (setq gdb-pending-triggers
3282 (delq 'gdb-invalidate-assembler
3283 gdb-pending-triggers))
3284 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3286 (with-current-buffer buf
3287 (let* ((window (get-buffer-window buf 0))
3288 (p (window-point window))
3289 (buffer-read-only nil))
3291 (insert-buffer-substring (gdb-get-buffer-create
3292 'gdb-partial-output-buffer))
3293 (set-window-point window p)))))
3294 ;; put customisation here
3295 (gdb-assembler-custom))
3297 (defun gdb-assembler-custom ()
3298 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3299 (pos 1) (address) (flag) (bptno))
3300 (with-current-buffer buffer
3302 (if (not (equal gdb-pc-address "main"))
3304 (goto-char (point-min))
3305 (if (and gdb-pc-address
3306 (search-forward gdb-pc-address nil t))
3310 (setq fringe-indicator-alist
3311 (if (string-equal gdb-frame-number "0")
3313 '((overlay-arrow . hollow-right-triangle))))
3314 (or gdb-overlay-arrow-position
3315 (setq gdb-overlay-arrow-position (make-marker)))
3316 (set-marker gdb-overlay-arrow-position (point))))))
3317 ;; remove all breakpoint-icons in assembler buffer before updating.
3318 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3319 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3320 (goto-char (point-min))
3321 (while (< (point) (- (point-max) 1))
3324 "\\([0-9]+\\.?[0-9]*\\).*?\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3325 (setq bptno (match-string 1))
3326 (setq flag (char-after (match-beginning 2)))
3327 (setq address (match-string 3))
3328 (with-current-buffer buffer
3330 (goto-char (point-min))
3331 (if (search-forward address nil t)
3332 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
3333 (if (not (equal gdb-pc-address "main"))
3334 (with-current-buffer buffer
3335 (set-window-point (get-buffer-window buffer 0) pos)))))
3337 (defvar gdb-assembler-mode-map
3338 (let ((map (make-sparse-keymap)))
3339 (suppress-keymap map)
3340 (define-key map "q" 'kill-this-buffer)
3343 (defvar gdb-assembler-font-lock-keywords
3344 '(;; <__function.name+n>
3345 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3346 (1 font-lock-function-name-face))
3347 ;; 0xNNNNNNNN <__function.name+n>: opcode
3348 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3349 (4 font-lock-keyword-face))
3350 ;; %register(at least i386)
3351 ("%\\sw+" . font-lock-variable-name-face)
3352 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3353 (1 font-lock-comment-face)
3354 (2 font-lock-function-name-face))
3355 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3356 "Font lock keywords used in `gdb-assembler-mode'.")
3358 (defun gdb-assembler-mode ()
3359 "Major mode for viewing code assembler.
3361 \\{gdb-assembler-mode-map}"
3362 (kill-all-local-variables)
3363 (setq major-mode 'gdb-assembler-mode)
3364 (setq mode-name (concat "Machine:" gdb-selected-frame))
3365 (setq gdb-overlay-arrow-position nil)
3366 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3367 (setq fringes-outside-margins t)
3368 (setq buffer-read-only t)
3369 (use-local-map gdb-assembler-mode-map)
3370 (gdb-invalidate-assembler)
3371 (set (make-local-variable 'font-lock-defaults)
3372 '(gdb-assembler-font-lock-keywords))
3373 (run-mode-hooks 'gdb-assembler-mode-hook)
3374 'gdb-invalidate-assembler)
3376 (defun gdb-assembler-buffer-name ()
3377 (with-current-buffer gud-comint-buffer
3378 (concat "*disassembly of " (gdb-get-target-string) "*")))
3380 (defun gdb-display-assembler-buffer ()
3381 "Display disassembly view."
3383 (setq gdb-previous-frame nil)
3385 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3387 (defun gdb-frame-assembler-buffer ()
3388 "Display disassembly view in a new frame."
3390 (setq gdb-previous-frame nil)
3391 (let ((special-display-regexps (append special-display-regexps '(".*")))
3392 (special-display-frame-alist gdb-frame-parameters))
3393 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3395 ;; modified because if gdb-pc-address has changed value a new command
3396 ;; must be enqueued to update the buffer with the new output
3397 (defun gdb-invalidate-assembler (&optional ignored)
3398 (if (gdb-get-buffer 'gdb-assembler-buffer)
3400 (unless (and gdb-selected-frame
3401 (string-equal gdb-selected-frame gdb-previous-frame))
3402 (if (or (not (member 'gdb-invalidate-assembler
3403 gdb-pending-triggers))
3404 (not (string-equal gdb-pc-address
3405 gdb-previous-frame-address)))
3407 ;; take previous disassemble command, if any, off the queue
3408 (with-current-buffer gud-comint-buffer
3409 (let ((queue gdb-input-queue))
3410 (dolist (item queue)
3411 (if (equal (cdr item) '(gdb-assembler-handler))
3412 (setq gdb-input-queue
3413 (delete item gdb-input-queue))))))
3416 (concat gdb-server-prefix "disassemble "
3417 (if (member gdb-pc-address '(nil "main")) nil "0x")
3418 gdb-pc-address "\n")
3419 'gdb-assembler-handler))
3420 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3421 (setq gdb-previous-frame-address gdb-pc-address)
3422 (setq gdb-previous-frame gdb-selected-frame)))))))
3424 (defun gdb-get-selected-frame ()
3425 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3428 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3429 (push 'gdb-get-selected-frame
3430 gdb-pending-triggers))))
3432 (defun gdb-frame-handler ()
3433 (setq gdb-pending-triggers
3434 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3435 (goto-char (point-min))
3436 (when (re-search-forward
3437 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3438 (setq gdb-frame-number (match-string 1))
3439 (setq gdb-frame-address (match-string 2)))
3440 (goto-char (point-min))
3441 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3442 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3444 (setq gdb-selected-frame (match-string 2))
3445 (if (gdb-get-buffer 'gdb-locals-buffer)
3446 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3447 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3448 (if (gdb-get-buffer 'gdb-assembler-buffer)
3449 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3450 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3451 (setq gdb-pc-address (match-string 1))
3452 (if (and (match-string 3) gud-overlay-arrow-position)
3453 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3454 (position (marker-position gud-overlay-arrow-position)))
3456 (string-equal (file-name-nondirectory
3457 (buffer-file-name buffer))
3458 (file-name-nondirectory (match-string 3))))
3459 (with-current-buffer buffer
3460 (setq fringe-indicator-alist
3461 (if (string-equal gdb-frame-number "0")
3463 '((overlay-arrow . hollow-right-triangle))))
3464 (set-marker gud-overlay-arrow-position position))))))
3465 (goto-char (point-min))
3466 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3467 (setq gdb-current-language (match-string 1)))
3468 (gdb-invalidate-assembler))
3471 ;; Code specific to GDB 6.4
3472 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3474 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3475 "Create list of source files for current GDB session.
3476 If buffers already exist for any of these files, gud-minor-mode
3478 (goto-char (point-min))
3479 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3480 (push (match-string 1) gdb-source-file-list))
3481 (dolist (buffer (buffer-list))
3482 (with-current-buffer buffer
3483 (when (member buffer-file-name gdb-source-file-list)
3484 (set (make-local-variable 'gud-minor-mode)
3485 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3486 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3487 (when gud-tooltip-mode
3488 (make-local-variable 'gdb-define-alist)
3489 (gdb-create-define-alist)
3490 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3491 (gdb-force-mode-line-update
3492 (propertize "ready" 'face font-lock-variable-name-face)))
3494 ; Uses "-var-list-children --all-values". Needs GDB 6.4 onwards.
3495 (defun gdb-var-list-children-1 (varnum)
3498 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3499 (concat "server interpreter mi \"-var-list-children --all-values \\\""
3501 (concat "-var-list-children --all-values \"" varnum "\"\n"))
3502 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3504 (defconst gdb-var-list-children-regexp-1
3505 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3506 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3507 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3509 (defun gdb-var-list-children-handler-1 (varnum)
3510 (goto-char (point-min))
3511 (let ((var-list nil))
3512 (catch 'child-already-watched
3513 (dolist (var gdb-var-list)
3514 (if (string-equal varnum (car var))
3517 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3518 (let ((varchild (list (match-string 1)
3522 (read (match-string 4))
3524 (if (assoc (car varchild) gdb-var-list)
3525 (throw 'child-already-watched nil))
3526 (push varchild var-list))))
3527 (push var var-list)))
3528 (setq gdb-var-list (nreverse var-list))))
3529 (gdb-speedbar-update))
3531 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3532 (defun gdb-var-update-1 ()
3533 (if (not (member 'gdb-var-update gdb-pending-triggers))
3537 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3538 "server interpreter mi \"-var-update --all-values *\"\n"
3539 "-var-update --all-values *\n")
3540 'gdb-var-update-handler-1))
3541 (push 'gdb-var-update gdb-pending-triggers))))
3543 (defconst gdb-var-update-regexp-1
3544 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3545 in_scope=\"\\(.*?\\)\".*?}")
3547 (defun gdb-var-update-handler-1 ()
3548 (dolist (var gdb-var-list)
3549 (setcar (nthcdr 5 var) nil))
3550 (goto-char (point-min))
3551 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3552 (let* ((varnum (match-string 1))
3553 (var (assoc varnum gdb-var-list)))
3555 (let ((match (match-string 3)))
3556 (cond ((string-equal match "false")
3557 (if gdb-delete-out-of-scope
3558 (gdb-var-delete-1 varnum)
3559 (setcar (nthcdr 5 var) 'out-of-scope)))
3560 ((string-equal match "true")
3561 (setcar (nthcdr 5 var) 'changed)
3562 (setcar (nthcdr 4 var)
3563 (read (match-string 2))))
3564 ((string-equal match "invalid")
3565 (gdb-var-delete-1 varnum)))))))
3566 (setq gdb-pending-triggers
3567 (delq 'gdb-var-update gdb-pending-triggers))
3568 (gdb-speedbar-update))
3570 ;; Registers buffer.
3572 (gdb-set-buffer-rules 'gdb-registers-buffer
3573 'gdb-registers-buffer-name
3574 'gdb-registers-mode)
3576 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3577 (gdb-get-buffer 'gdb-registers-buffer)
3578 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3579 "server interpreter mi \"-data-list-register-values x\"\n"
3580 "-data-list-register-values x\n")
3581 gdb-data-list-register-values-handler)
3583 (defconst gdb-data-list-register-values-regexp
3584 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3586 (defun gdb-data-list-register-values-handler ()
3587 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3588 gdb-pending-triggers))
3589 (goto-char (point-min))
3590 (if (re-search-forward gdb-error-regexp nil t)
3591 (let ((err (match-string 1)))
3592 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3593 (let ((buffer-read-only nil))
3595 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3597 (goto-char (point-min)))))
3598 (let ((register-list (reverse gdb-register-names))
3599 (register nil) (register-string nil) (register-values nil))
3600 (goto-char (point-min))
3601 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3602 (setq register (pop register-list))
3603 (setq register-string (concat register "\t" (match-string 2) "\n"))
3604 (if (member (match-string 1) gdb-changed-registers)
3605 (put-text-property 0 (length register-string)
3606 'face 'font-lock-warning-face
3608 (setq register-values
3609 (concat register-values register-string)))
3610 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3611 (with-current-buffer buf
3612 (let* ((window (get-buffer-window buf 0))
3613 (start (window-start window))
3614 (p (window-point window))
3615 (buffer-read-only nil))
3617 (insert register-values)
3618 (set-window-start window start)
3619 (set-window-point window p))))))
3620 (gdb-data-list-register-values-custom))
3622 (defun gdb-data-list-register-values-custom ()
3623 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3625 (let ((buffer-read-only nil)
3627 (goto-char (point-min))
3628 (while (< (point) (point-max))
3629 (setq start (line-beginning-position))
3630 (setq end (line-end-position))
3631 (when (looking-at "^[^\t]+")
3632 (unless (string-equal (match-string 0) "No registers.")
3633 (put-text-property start (match-end 0)
3634 'face font-lock-variable-name-face)
3635 (add-text-properties start end
3636 '(help-echo "mouse-2: edit value"
3637 mouse-face highlight))))
3638 (forward-line 1))))))
3640 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3641 (defun gdb-get-changed-registers ()
3642 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3643 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3647 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3648 "server interpreter mi -data-list-changed-registers\n"
3649 "-data-list-changed-registers\n")
3650 'gdb-get-changed-registers-handler))
3651 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3653 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3655 (defun gdb-get-changed-registers-handler ()
3656 (setq gdb-pending-triggers
3657 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3658 (setq gdb-changed-registers nil)
3659 (goto-char (point-min))
3660 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3661 (push (match-string 1) gdb-changed-registers)))
3666 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3667 (gdb-set-buffer-rules 'gdb-locals-buffer
3668 'gdb-locals-buffer-name
3671 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3672 (gdb-get-buffer 'gdb-locals-buffer)
3673 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3674 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3675 "-stack-list-locals --simple-values\n")
3676 gdb-stack-list-locals-handler)
3678 (defconst gdb-stack-list-locals-regexp
3679 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3681 (defvar gdb-locals-watch-map-1
3682 (let ((map (make-sparse-keymap)))
3683 (suppress-keymap map)
3684 (define-key map "\r" 'gud-watch)
3685 (define-key map [mouse-2] 'gud-watch)
3687 "Keymap to create watch expression of a complex data type local variable.")
3689 (defvar gdb-edit-locals-map-1
3690 (let ((map (make-sparse-keymap)))
3691 (suppress-keymap map)
3692 (define-key map "\r" 'gdb-edit-locals-value)
3693 (define-key map [mouse-2] 'gdb-edit-locals-value)
3695 "Keymap to edit value of a simple data type local variable.")
3697 (defun gdb-edit-locals-value (&optional event)
3698 "Assign a value to a variable displayed in the locals buffer."
3699 (interactive (list last-input-event))
3701 (if event (posn-set-point (event-end event)))
3703 (let* ((var (current-word))
3704 (value (read-string (format "New value (%s): " var))))
3706 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3709 ;; Dont display values of arrays or structures.
3710 ;; These can be expanded using gud-watch.
3711 (defun gdb-stack-list-locals-handler ()
3712 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3713 gdb-pending-triggers))
3714 (goto-char (point-min))
3715 (if (re-search-forward gdb-error-regexp nil t)
3716 (let ((err (match-string 1)))
3717 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3718 (let ((buffer-read-only nil))
3721 (goto-char (point-min)))))
3722 (let (local locals-list)
3723 (goto-char (point-min))
3724 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3725 (let ((local (list (match-string 1)
3728 (if (looking-at ",value=\\(\".*\"\\).*?}")
3729 (setcar (nthcdr 2 local) (read (match-string 1))))
3730 (push local locals-list)))
3731 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3732 (and buf (with-current-buffer buf
3733 (let* ((window (get-buffer-window buf 0))
3734 (start (window-start window))
3735 (p (window-point window))
3736 (buffer-read-only nil) (name) (value))
3738 (dolist (local locals-list)
3739 (setq name (car local))
3740 (setq value (nth 2 local))
3742 (string-match "^\\0x" value))
3743 (add-text-properties 0 (length name)
3744 `(mouse-face highlight
3745 help-echo "mouse-2: create watch expression"
3746 local-map ,gdb-locals-watch-map-1)
3748 (add-text-properties 0 (length value)
3749 `(mouse-face highlight
3750 help-echo "mouse-2: edit value"
3751 local-map ,gdb-edit-locals-map-1)
3754 (concat name "\t" (nth 1 local)
3756 (set-window-start window start)
3757 (set-window-point window p))))))))
3759 (defun gdb-get-register-names ()
3760 "Create a list of register names."
3761 (goto-char (point-min))
3762 (setq gdb-register-names nil)
3763 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3764 (push (match-string 1) gdb-register-names)))
3768 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3769 ;;; gdb-ui.el ends here