Document reserved keys
[emacs.git] / lisp / progmodes / gud.el
blob15b428bb68b99a9aeaaba771bbe0b81607bb4781
1 ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers -*- lexical-binding:t -*-
3 ;; Copyright (C) 1992-1996, 1998, 2000-2018 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: unix, tools
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 of the License, or
15 ;; (at your option) any later version.
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. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>.
28 ;; It was later rewritten by rms. Some ideas were due to Masanobu. Grand
29 ;; Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com> Barry
30 ;; Warsaw <bwarsaw@cen.com> hacked the mode to use comint.el. Shane Hartman
31 ;; <shane@spr.com> added support for xdb (HPUX debugger). Rick Sladkey
32 ;; <jrs@world.std.com> wrote the GDB command completion code. Dave Love
33 ;; <d.love@dl.ac.uk> added the IRIX kluge, re-implemented the Mips-ish variant
34 ;; and added a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX
35 ;; kluge with the gud-xdb-directories hack producing gud-dbx-directories.
36 ;; Derek L. Davies <ddavies@world.std.com> added support for jdb (Java
37 ;; debugger.) Jan Nieuwenhuizen added support for the Guile REPL (Guile
38 ;; debugger).
40 ;;; Code:
42 (require 'comint)
44 (defvar gdb-active-process)
45 (defvar gdb-define-alist)
46 (defvar gdb-macro-info)
47 (defvar gdb-show-changed-values)
48 (defvar gdb-source-window)
49 (defvar gdb-var-list)
50 (defvar hl-line-mode)
51 (defvar hl-line-sticky-flag)
54 ;; ======================================================================
55 ;; GUD commands must be visible in C buffers visited by GUD
57 (defgroup gud nil
58 "The \"Grand Unified Debugger\" interface.
59 Supported debuggers include gdb, sdb, dbx, xdb, perldb,
60 pdb (Python), and jdb."
61 :group 'processes
62 :group 'tools)
65 (defcustom gud-key-prefix "\C-x\C-a"
66 "Prefix of all GUD commands valid in C buffers."
67 :type 'key-sequence
68 :group 'gud)
70 (global-set-key (vconcat gud-key-prefix "\C-l") 'gud-refresh)
71 ;; (define-key ctl-x-map " " 'gud-break); backward compatibility hack
73 (defvar gud-marker-filter nil)
74 (put 'gud-marker-filter 'permanent-local t)
75 (defvar gud-find-file nil)
76 (put 'gud-find-file 'permanent-local t)
78 (defun gud-marker-filter (&rest args)
79 (apply gud-marker-filter args))
81 (defvar gud-minor-mode nil)
82 (put 'gud-minor-mode 'permanent-local t)
84 (defvar gud-comint-buffer nil)
86 (defvar gud-keep-buffer nil)
88 (defun gud-symbol (sym &optional soft minor-mode)
89 "Return the symbol used for SYM in MINOR-MODE.
90 MINOR-MODE defaults to `gud-minor-mode'.
91 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
92 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
93 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
94 (funcall (if soft 'intern-soft 'intern)
95 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
97 (defun gud-val (sym &optional minor-mode)
98 "Return the value of `gud-symbol' SYM. Default to nil."
99 (let ((sym (gud-symbol sym t minor-mode)))
100 (if (boundp sym) (symbol-value sym))))
102 (defvar gud-running nil
103 "Non-nil if debugged program is running.
104 Used to gray out relevant toolbar icons.")
106 (defvar gud-target-name "--unknown--"
107 "The apparent name of the program being debugged in a gud buffer.")
109 ;; Use existing Info buffer, if possible.
110 (defun gud-goto-info ()
111 "Go to relevant Emacs info node."
112 (interactive)
113 (if (eq gud-minor-mode 'gdbmi)
114 (info-other-window "(emacs)GDB Graphical Interface")
115 (info-other-window "(emacs)Debuggers")))
117 (defun gud-tool-bar-item-visible-no-fringe ()
118 (not (or (eq (buffer-local-value 'major-mode (window-buffer)) 'speedbar-mode)
119 (eq (buffer-local-value 'major-mode (window-buffer)) 'gdb-memory-mode)
120 (and (eq gud-minor-mode 'gdbmi)
121 (> (car (window-fringes)) 0)))))
123 (declare-function gdb-gud-context-command "gdb-mi.el")
125 (defun gud-stop-subjob ()
126 (interactive)
127 (with-current-buffer gud-comint-buffer
128 (cond ((string-equal gud-target-name "emacs")
129 (comint-stop-subjob))
130 ((eq gud-minor-mode 'jdb)
131 (gud-call "suspend"))
132 ((eq gud-minor-mode 'gdbmi)
133 (gud-call (gdb-gud-context-command "-exec-interrupt")))
135 (comint-interrupt-subjob)))))
137 (easy-mmode-defmap gud-menu-map
138 '(([help] "Info (debugger)" . gud-goto-info)
139 ([tooltips] menu-item "Show GUD tooltips" gud-tooltip-mode
140 :enable (and (not emacs-basic-display)
141 (display-graphic-p)
142 (fboundp 'x-show-tip))
143 :visible (memq gud-minor-mode
144 '(gdbmi guiler dbx sdb xdb pdb))
145 :button (:toggle . gud-tooltip-mode))
146 ([refresh] "Refresh" . gud-refresh)
147 ([run] menu-item "Run" gud-run
148 :enable (not gud-running)
149 :visible (or (memq gud-minor-mode '(gdb dbx jdb))
150 (and (eq gud-minor-mode 'gdbmi)
151 (or (not (gdb-show-run-p))
152 (bound-and-true-p
153 gdb-active-process)))))
154 ([go] menu-item (if (bound-and-true-p gdb-active-process)
155 "Continue" "Run") gud-go
156 :visible (and (eq gud-minor-mode 'gdbmi)
157 (gdb-show-run-p)))
158 ([stop] menu-item "Stop" gud-stop-subjob
159 :visible (or (not (memq gud-minor-mode '(gdbmi pdb)))
160 (and (eq gud-minor-mode 'gdbmi)
161 (gdb-show-stop-p))))
162 ([until] menu-item "Continue to selection" gud-until
163 :enable (not gud-running)
164 :visible (and (memq gud-minor-mode '(gdbmi gdb perldb))
165 (gud-tool-bar-item-visible-no-fringe)))
166 ([remove] menu-item "Remove Breakpoint" gud-remove
167 :enable (not gud-running)
168 :visible (gud-tool-bar-item-visible-no-fringe))
169 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
170 :enable (not gud-running)
171 :visible (memq gud-minor-mode
172 '(gdbmi gdb sdb xdb)))
173 ([break] menu-item "Set Breakpoint" gud-break
174 :enable (not gud-running)
175 :visible (gud-tool-bar-item-visible-no-fringe))
176 ([up] menu-item "Up Stack" gud-up
177 :enable (not gud-running)
178 :visible (memq gud-minor-mode
179 '(gdbmi gdb guiler dbx xdb jdb pdb)))
180 ([down] menu-item "Down Stack" gud-down
181 :enable (not gud-running)
182 :visible (memq gud-minor-mode
183 '(gdbmi gdb guiler dbx xdb jdb pdb)))
184 ([pp] menu-item "Print S-expression" gud-pp
185 :enable (and (not gud-running)
186 (bound-and-true-p gdb-active-process))
187 :visible (and (string-equal
188 (buffer-local-value
189 'gud-target-name gud-comint-buffer) "emacs")
190 (eq gud-minor-mode 'gdbmi)))
191 ([print*] menu-item (if (eq gud-minor-mode 'jdb)
192 "Dump object"
193 "Print Dereference") gud-pstar
194 :enable (not gud-running)
195 :visible (memq gud-minor-mode '(gdbmi gdb jdb)))
196 ([print] menu-item "Print Expression" gud-print
197 :enable (not gud-running))
198 ([watch] menu-item "Watch Expression" gud-watch
199 :enable (not gud-running)
200 :visible (eq gud-minor-mode 'gdbmi))
201 ([finish] menu-item "Finish Function" gud-finish
202 :enable (not gud-running)
203 :visible (memq gud-minor-mode
204 '(gdbmi gdb guiler xdb jdb pdb)))
205 ([stepi] menu-item "Step Instruction" gud-stepi
206 :enable (not gud-running)
207 :visible (memq gud-minor-mode '(gdbmi gdb dbx)))
208 ([nexti] menu-item "Next Instruction" gud-nexti
209 :enable (not gud-running)
210 :visible (memq gud-minor-mode '(gdbmi gdb dbx)))
211 ([step] menu-item "Step Line" gud-step
212 :enable (not gud-running))
213 ([next] menu-item "Next Line" gud-next
214 :enable (not gud-running))
215 ([cont] menu-item "Continue" gud-cont
216 :enable (not gud-running)
217 :visible (not (eq gud-minor-mode 'gdbmi))))
218 "Menu for `gud-mode'."
219 :name "Gud")
221 (easy-mmode-defmap gud-minor-mode-map
222 (append
223 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
224 ;; Get tool bar like functionality from the menu bar on a text only
225 ;; terminal.
226 (unless window-system
227 `(([menu-bar down]
228 . (,(propertize "down" 'face 'font-lock-doc-face) . gud-down))
229 ([menu-bar up]
230 . (,(propertize "up" 'face 'font-lock-doc-face) . gud-up))
231 ([menu-bar finish]
232 . (,(propertize "finish" 'face 'font-lock-doc-face) . gud-finish))
233 ([menu-bar step]
234 . (,(propertize "step" 'face 'font-lock-doc-face) . gud-step))
235 ([menu-bar next]
236 . (,(propertize "next" 'face 'font-lock-doc-face) . gud-next))
237 ([menu-bar until] menu-item
238 ,(propertize "until" 'face 'font-lock-doc-face) gud-until
239 :visible (memq gud-minor-mode '(gdbmi gdb perldb)))
240 ([menu-bar cont] menu-item
241 ,(propertize "cont" 'face 'font-lock-doc-face) gud-cont
242 :visible (not (eq gud-minor-mode 'gdbmi)))
243 ([menu-bar run] menu-item
244 ,(propertize "run" 'face 'font-lock-doc-face) gud-run
245 :visible (memq gud-minor-mode '(gdbmi gdb dbx jdb)))
246 ([menu-bar go] menu-item
247 ,(propertize " go " 'face 'font-lock-doc-face) gud-go
248 :visible (and (eq gud-minor-mode 'gdbmi)
249 (gdb-show-run-p)))
250 ([menu-bar stop] menu-item
251 ,(propertize "stop" 'face 'font-lock-doc-face) gud-stop-subjob
252 :visible (or (and (eq gud-minor-mode 'gdbmi)
253 (gdb-show-stop-p))
254 (not (eq gud-minor-mode 'gdbmi))))
255 ([menu-bar print]
256 . (,(propertize "print" 'face 'font-lock-doc-face) . gud-print))
257 ([menu-bar tools] . undefined)
258 ([menu-bar buffer] . undefined)
259 ([menu-bar options] . undefined)
260 ([menu-bar edit] . undefined)
261 ([menu-bar file] . undefined))))
262 "Map used in visited files.")
264 (setf (alist-get 'gud-minor-mode minor-mode-map-alist)
265 gud-minor-mode-map)
267 (defvar gud-mode-map
268 ;; Will inherit from comint-mode via define-derived-mode.
269 (make-sparse-keymap)
270 "`gud-mode' keymap.")
272 (defvar gud-tool-bar-map
273 (let ((map (make-sparse-keymap)))
274 (dolist (x '((gud-break . "gud/break")
275 (gud-remove . "gud/remove")
276 (gud-print . "gud/print")
277 (gud-pstar . "gud/pstar")
278 (gud-pp . "gud/pp")
279 (gud-watch . "gud/watch")
280 (gud-run . "gud/run")
281 (gud-go . "gud/go")
282 (gud-stop-subjob . "gud/stop")
283 (gud-cont . "gud/cont")
284 (gud-until . "gud/until")
285 (gud-next . "gud/next")
286 (gud-step . "gud/step")
287 (gud-finish . "gud/finish")
288 (gud-nexti . "gud/nexti")
289 (gud-stepi . "gud/stepi")
290 (gud-up . "gud/up")
291 (gud-down . "gud/down")
292 (gud-goto-info . "info"))
293 map)
294 (tool-bar-local-item-from-menu
295 (car x) (cdr x) map gud-minor-mode-map))))
297 (defun gud-file-name (f)
298 "Transform a relative file name to an absolute file name.
299 Uses `gud-<MINOR-MODE>-directories' to find the source files."
300 ;; When `default-directory' is a remote file name, prepend its
301 ;; remote part to f, which is the local file name. Fortunately,
302 ;; `file-remote-p' returns exactly this remote file name part (or
303 ;; nil otherwise).
304 (setq f (concat (or (file-remote-p default-directory) "") f))
305 (if (file-exists-p f) (expand-file-name f)
306 (let ((directories (gud-val 'directories))
307 (result nil))
308 (while directories
309 (let ((path (expand-file-name f (car directories))))
310 (if (file-exists-p path)
311 (setq result path
312 directories nil)))
313 (setq directories (cdr directories)))
314 result)))
316 (declare-function gdb-create-define-alist "gdb-mi" ())
318 (defun gud-find-file (file)
319 ;; Don't get confused by double slashes in the name that comes from GDB.
320 (while (string-match "//+" file)
321 (setq file (replace-match "/" t t file)))
322 (let ((minor-mode gud-minor-mode)
323 (buf (funcall (or gud-find-file 'gud-file-name) file)))
324 (when (stringp buf)
325 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
326 (when buf
327 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
328 (with-current-buffer buf
329 (setq-local gud-minor-mode minor-mode)
330 (if (boundp 'tool-bar-map) ; not --without-x
331 (setq-local tool-bar-map gud-tool-bar-map))
332 (when (and gud-tooltip-mode
333 (eq gud-minor-mode 'gdbmi))
334 (make-local-variable 'gdb-define-alist)
335 (unless gdb-define-alist (gdb-create-define-alist))
336 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))
337 (make-local-variable 'gud-keep-buffer))
338 buf)))
340 ;; ======================================================================
341 ;; command definition
343 ;; This macro is used below to define some basic debugger interface commands.
344 ;; Of course you may use `gud-def' with any other debugger command, including
345 ;; user defined ones.
347 ;; A macro call like (gud-def FUNC CMD KEY DOC) expands to a form
348 ;; which defines FUNC to send the command CMD to the debugger, gives
349 ;; it the docstring DOC, and binds that function to KEY in the GUD
350 ;; major mode. The function is also bound in the global keymap with the
351 ;; GUD prefix.
353 (defmacro gud-def (func cmd key &optional doc)
354 "Define FUNC to be a command sending CMD and bound to KEY, with
355 optional doc string DOC. Certain %-escapes in the string arguments
356 are interpreted specially if present. These are:
358 %f -- Name (without directory) of current source file.
359 %F -- Name (without directory or extension) of current source file.
360 %d -- Directory of current source file.
361 %l -- Number of current source line.
362 %e -- Text of the C lvalue or function-call expression surrounding point.
363 %a -- Text of the hexadecimal address surrounding point.
364 %p -- Prefix argument to the command (if any) as a number.
365 %c -- Fully qualified class name derived from the expression
366 surrounding point (jdb only).
368 The `current' source file is the file of the current buffer (if
369 we're in a C file) or the source file current at the last break or
370 step (if we're in the GUD buffer).
371 The `current' line is that of the current buffer (if we're in a
372 source file) or the source line number at the last break or step (if
373 we're in the GUD buffer)."
374 `(progn
375 (defalias ',func (lambda (arg)
376 ,@(if doc (list doc))
377 (interactive "p")
378 (if (not gud-running)
379 ,(if (stringp cmd)
380 `(gud-call ,cmd arg)
381 cmd))))
382 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
383 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
385 ;; Where gud-display-frame should put the debugging arrow; a cons of
386 ;; (filename . line-number). This is set by the marker-filter, which scans
387 ;; the debugger's output for indications of the current program counter.
388 (defvar gud-last-frame nil)
390 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
391 ;; the last frame, even if it's been called before and gud-last-frame has
392 ;; been set to nil.
393 (defvar gud-last-last-frame nil)
395 ;; All debugger-specific information is collected here.
396 ;; Here's how it works, in case you ever need to add a debugger to the mode.
398 ;; Each entry must define the following at startup:
400 ;;<name>
401 ;; comint-prompt-regexp
402 ;; gud-<name>-massage-args
403 ;; gud-<name>-marker-filter
404 ;; gud-<name>-find-file
406 ;; The job of the massage-args method is to modify the given list of
407 ;; debugger arguments before running the debugger.
409 ;; The job of the marker-filter method is to detect file/line markers in
410 ;; strings and set the global gud-last-frame to indicate what display
411 ;; action (if any) should be triggered by the marker. Note that only
412 ;; whatever the method *returns* is displayed in the buffer; thus, you
413 ;; can filter the debugger's output, interpreting some and passing on
414 ;; the rest.
416 ;; The job of the find-file method is to visit and return the buffer indicated
417 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
418 ;; something else.
420 ;; ======================================================================
421 ;; speedbar support functions and variables.
422 (eval-when-compile (require 'dframe)) ; for dframe-with-attached-buffer
424 (defvar gud-last-speedbar-stackframe nil
425 "Description of the currently displayed GUD stack.
426 The value t means that there is no stack, and we are in display-file mode.")
428 (defvar gud-speedbar-key-map nil
429 "Keymap used when in the buffers display mode.")
431 ;; At runtime, will be pulled in as a require of speedbar.
432 (declare-function dframe-message "dframe" (fmt &rest args))
434 (defun gud-speedbar-item-info ()
435 "Display the data type of the watch expression element."
436 (let ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list)))
437 (if (nth 7 var)
438 (dframe-message "%s: %s" (nth 7 var) (nth 3 var))
439 (dframe-message "%s" (nth 3 var)))))
441 (declare-function speedbar-make-specialized-keymap "speedbar" ())
442 (declare-function speedbar-add-expansion-list "speedbar" (new-list))
443 (defvar speedbar-mode-functions-list)
445 (defun gud-install-speedbar-variables ()
446 "Install those variables used by speedbar to enhance gud/gdb."
447 (unless gud-speedbar-key-map
448 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
449 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
450 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
451 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
452 (define-key gud-speedbar-key-map " " 'speedbar-toggle-line-expansion)
453 (define-key gud-speedbar-key-map "D" 'gdb-var-delete)
454 (define-key gud-speedbar-key-map "p" 'gud-pp))
456 (speedbar-add-expansion-list '("GUD" gud-speedbar-menu-items
457 gud-speedbar-key-map
458 gud-expansion-speedbar-buttons))
460 (add-to-list
461 'speedbar-mode-functions-list
462 '("GUD" (speedbar-item-info . gud-speedbar-item-info)
463 (speedbar-line-directory . ignore))))
465 (defvar gud-speedbar-menu-items
466 '(["Jump to stack frame" speedbar-edit-line
467 :visible (not (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
468 'gdbmi))]
469 ["Edit value" speedbar-edit-line
470 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
471 'gdbmi)]
472 ["Delete expression" gdb-var-delete
473 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
474 'gdbmi)]
475 ["Auto raise frame" gdb-speedbar-auto-raise
476 :style toggle :selected gdb-speedbar-auto-raise
477 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
478 'gdbmi)]
479 ("Output Format"
480 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
481 'gdbmi)
482 ["Binary" (gdb-var-set-format "binary") t]
483 ["Natural" (gdb-var-set-format "natural") t]
484 ["Hexadecimal" (gdb-var-set-format "hexadecimal") t]))
485 "Additional menu items to add to the speedbar frame.")
487 ;; Make sure our special speedbar mode is loaded
488 (if (featurep 'speedbar)
489 (gud-install-speedbar-variables)
490 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
492 (defun gud-expansion-speedbar-buttons (_directory _zero)
493 "Wrapper for call to `speedbar-add-expansion-list'.
494 DIRECTORY and ZERO are not used, but are required by the caller."
495 (gud-speedbar-buttons gud-comint-buffer))
497 (declare-function speedbar-make-tag-line "speedbar"
498 (type char func data tag tfunc tdata tface depth))
499 (declare-function speedbar-remove-localized-speedbar-support "speedbar"
500 (buffer))
501 (declare-function speedbar-insert-button "speedbar"
502 (text face mouse function &optional token prevline))
504 (defun gud-speedbar-buttons (buffer)
505 "Create a speedbar display based on the current state of GUD.
506 If the GUD BUFFER is not running a supported debugger, then turn
507 off the specialized speedbar mode. BUFFER is not used, but is
508 required by the caller."
509 (when (and gud-comint-buffer
510 ;; gud-comint-buffer might be killed
511 (buffer-name gud-comint-buffer))
512 (let* ((minor-mode (with-current-buffer buffer gud-minor-mode))
513 (window (get-buffer-window (current-buffer) 0))
514 (start (window-start window))
515 (p (window-point window)))
516 (cond
517 ((eq minor-mode 'gdbmi)
518 (erase-buffer)
519 (insert "Watch Expressions:\n")
520 (let ((var-list gdb-var-list) parent)
521 (while var-list
522 (let* (char (depth 0) (start 0) (var (car var-list))
523 (varnum (car var)) (expr (nth 1 var))
524 (type (if (nth 3 var) (nth 3 var) " "))
525 (value (nth 4 var)) (status (nth 5 var))
526 (has-more (nth 6 var)))
527 (put-text-property
528 0 (length expr) 'face font-lock-variable-name-face expr)
529 (put-text-property
530 0 (length type) 'face font-lock-type-face type)
531 (while (string-match "\\." varnum start)
532 (setq depth (1+ depth)
533 start (1+ (match-beginning 0))))
534 (if (eq depth 0) (setq parent nil))
535 (if (and (or (not has-more) (string-equal has-more "0"))
536 (or (equal (nth 2 var) "0")
537 (and (equal (nth 2 var) "1")
538 (string-match "char \\*$" type)) ))
539 (speedbar-make-tag-line
540 'bracket ?? nil nil
541 (concat expr "\t" value)
542 (if (or parent (eq status 'out-of-scope))
543 nil 'gdb-edit-value)
545 (if gdb-show-changed-values
546 (or parent (pcase status
547 (`changed 'font-lock-warning-face)
548 (`out-of-scope 'shadow)
549 (_ t)))
551 depth)
552 (if (eq status 'out-of-scope) (setq parent 'shadow))
553 (if (and (nth 1 var-list)
554 (string-match (concat varnum "\\.")
555 (car (nth 1 var-list))))
556 (setq char ?-)
557 (setq char ?+))
558 (if (string-match "\\*$\\|\\*&$" type)
559 (speedbar-make-tag-line
560 'bracket char
561 'gdb-speedbar-expand-node varnum
562 (concat expr "\t" type "\t" value)
563 (if (or parent (eq status 'out-of-scope))
564 nil 'gdb-edit-value)
566 (if gdb-show-changed-values
567 (or parent (pcase status
568 (`changed 'font-lock-warning-face)
569 (`out-of-scope 'shadow)
570 (_ t)))
572 depth)
573 (speedbar-make-tag-line
574 'bracket char
575 'gdb-speedbar-expand-node varnum
576 (concat expr "\t" type)
577 nil nil
578 (if (and (or parent status) gdb-show-changed-values)
579 'shadow t)
580 depth))))
581 (setq var-list (cdr var-list)))))
582 (t (unless (and (save-excursion
583 (goto-char (point-min))
584 (looking-at "Current Stack:"))
585 (equal gud-last-last-frame gud-last-speedbar-stackframe))
586 (let ((gud-frame-list
587 (cond ((eq minor-mode 'gdb)
588 (gud-gdb-get-stackframe buffer))
589 ;; Add more debuggers here!
590 (t (speedbar-remove-localized-speedbar-support buffer)
591 nil))))
592 (erase-buffer)
593 (if (not gud-frame-list)
594 (insert "No Stack frames\n")
595 (insert "Current Stack:\n"))
596 (dolist (frame gud-frame-list)
597 (insert (nth 1 frame) ":\n")
598 (if (= (length frame) 2)
599 (progn
600 (speedbar-insert-button (car frame)
601 'speedbar-directory-face
602 nil nil nil t))
603 (speedbar-insert-button
604 (car frame)
605 'speedbar-file-face
606 'speedbar-highlight-face
607 (cond ((memq minor-mode '(gdbmi gdb))
608 'gud-gdb-goto-stackframe)
609 (t (error "Should never be here")))
610 frame t))))
611 (setq gud-last-speedbar-stackframe gud-last-last-frame))))
612 (set-window-start window start)
613 (set-window-point window p))))
616 ;; ======================================================================
617 ;; gdb functions
619 ;; History of argument lists passed to gdb.
620 (defvar gud-gdb-history nil)
622 (defcustom gud-gud-gdb-command-name "gdb --fullname"
623 "Default command to run an executable under GDB in text command mode.
624 The option \"--fullname\" must be included in this value."
625 :type 'string
626 :group 'gud)
628 (defvar gud-gdb-marker-regexp
629 ;; This used to use path-separator instead of ":";
630 ;; however, we found that on both Windows 32 and MSDOS
631 ;; a colon is correct here.
632 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
633 "\\([0-9]*\\)" ":" ".*\n"))
635 ;; There's no guarantee that Emacs will hand the filter the entire
636 ;; marker at once; it could be broken up across several strings. We
637 ;; might even receive a big chunk with several markers in it. If we
638 ;; receive a chunk of text which looks like it might contain the
639 ;; beginning of a marker, we save it here between calls to the
640 ;; filter.
641 (defvar gud-marker-acc "")
642 (make-variable-buffer-local 'gud-marker-acc)
644 (defun gud-gdb-marker-filter (string)
645 (setq gud-marker-acc (concat gud-marker-acc string))
646 (let ((output ""))
648 ;; Process all the complete markers in this chunk.
649 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
650 (setq
652 ;; Extract the frame position from the marker.
653 gud-last-frame (cons (match-string 1 gud-marker-acc)
654 (string-to-number (match-string 2 gud-marker-acc)))
656 ;; Append any text before the marker to the output we're going
657 ;; to return - we don't include the marker in this text.
658 output (concat output
659 (substring gud-marker-acc 0 (match-beginning 0)))
661 ;; Set the accumulator to the remaining text.
662 gud-marker-acc (substring gud-marker-acc (match-end 0))))
664 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
665 (setq
666 ;; Append any text before the marker to the output we're going
667 ;; to return - we don't include the marker in this text.
668 output (concat output
669 (substring gud-marker-acc 0 (match-beginning 0)))
671 ;; Set the accumulator to the remaining text.
673 gud-marker-acc (substring gud-marker-acc (match-end 0))))
675 ;; Does the remaining text look like it might end with the
676 ;; beginning of another marker? If it does, then keep it in
677 ;; gud-marker-acc until we receive the rest of it. Since we
678 ;; know the full marker regexp above failed, it's pretty simple to
679 ;; test for marker starts.
680 (if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
681 (progn
682 ;; Everything before the potential marker start can be output.
683 (setq output (concat output (substring gud-marker-acc
684 0 (match-beginning 0))))
686 ;; Everything after, we save, to combine with later input.
687 (setq gud-marker-acc
688 (substring gud-marker-acc (match-beginning 0))))
690 (setq output (concat output gud-marker-acc)
691 gud-marker-acc ""))
693 output))
695 (easy-mmode-defmap gud-minibuffer-local-map
696 '(("\C-i" . comint-dynamic-complete-filename))
697 "Keymap for minibuffer prompting of gud startup command."
698 :inherit minibuffer-local-map)
700 (defun gud-query-cmdline (minor-mode &optional init)
701 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
702 (cmd-name (gud-val 'command-name minor-mode)))
703 (unless (boundp hist-sym) (set hist-sym nil))
704 (read-from-minibuffer
705 (format "Run %s (like this): " minor-mode)
706 (or (car-safe (symbol-value hist-sym))
707 (concat (or cmd-name (symbol-name minor-mode))
709 (or init
710 (let ((file nil))
711 (dolist (f (directory-files default-directory) file)
712 (if (and (file-executable-p f)
713 (not (file-directory-p f))
714 (or (not file)
715 (file-newer-than-file-p f file)))
716 (setq file f)))))))
717 gud-minibuffer-local-map nil
718 hist-sym)))
720 (defvar gdb-first-prompt t)
722 (defvar gud-filter-pending-text nil
723 "Non-nil means this is text that has been saved for later in `gud-filter'.")
725 ;; One of the nice features of GDB is its impressive support for
726 ;; context-sensitive command completion. We preserve that feature
727 ;; in the GUD buffer by using a GDB command designed just for Emacs.
729 (defvar gud-gdb-completion-function nil
730 "Completion function for GDB commands.
731 It receives two arguments: COMMAND, the prefix for which we seek
732 completion; and CONTEXT, the text before COMMAND on the line.
733 It should return a list of completion strings.")
735 ;; If in gdb mode, gdb-mi is loaded.
736 (declare-function gdb-restore-windows "gdb-mi" ())
738 ;; The old gdb command (text command mode). The new one is in gdb-mi.el.
739 ;;;###autoload
740 (defun gud-gdb (command-line)
741 "Run gdb passing it COMMAND-LINE as arguments.
742 If COMMAND-LINE names a program FILE to debug, gdb will run in
743 a buffer named *gud-FILE*, and the directory containing FILE
744 becomes the initial working directory and source-file directory
745 for your debugger.
746 If COMMAND-LINE requests that gdb attaches to a process PID, gdb
747 will run in *gud-PID*, otherwise it will run in *gud*; in these
748 cases the initial working directory is the default-directory of
749 the buffer in which this command was invoked."
750 (interactive (list (gud-query-cmdline 'gud-gdb)))
752 (when (and gud-comint-buffer
753 (buffer-name gud-comint-buffer)
754 (get-buffer-process gud-comint-buffer)
755 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdbmi)))
756 (gdb-restore-windows)
757 (error
758 "Multiple debugging requires restarting in text command mode"))
760 (gud-common-init command-line nil 'gud-gdb-marker-filter)
761 (set (make-local-variable 'gud-minor-mode) 'gdb)
763 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
764 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
765 "Set temporary breakpoint at current line.")
766 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
767 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
768 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
769 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
770 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
771 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
772 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
773 (gud-def gud-jump
774 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
775 "\C-j" "Set execution address to current line.")
777 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
778 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
779 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
780 (gud-def gud-pstar "print* %e" nil
781 "Evaluate C dereferenced pointer expression at point.")
783 ;; For debugging Emacs only.
784 (gud-def gud-pv "pv %e" "\C-v" "Print the value of the lisp variable.")
786 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
787 (gud-def gud-run "run" nil "Run the program.")
789 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
790 nil 'local)
791 (set (make-local-variable 'gud-gdb-completion-function) 'gud-gdb-completions)
793 (local-set-key "\C-i" 'completion-at-point)
794 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
795 (setq paragraph-start comint-prompt-regexp)
796 (setq gdb-first-prompt t)
797 (setq gud-running nil)
798 (setq gud-filter-pending-text nil)
799 (run-hooks 'gud-gdb-mode-hook))
801 ;; The completion process filter indicates when it is finished.
802 (defvar gud-gdb-fetch-lines-in-progress)
804 ;; Since output may arrive in fragments we accumulate partials strings here.
805 (defvar gud-gdb-fetch-lines-string)
807 ;; We need to know how much of the completion to chop off.
808 (defvar gud-gdb-fetch-lines-break)
810 ;; The completion list is constructed by the process filter.
811 (defvar gud-gdb-fetched-lines)
813 (defun gud-gdb-completions (context command)
814 "Completion table for GDB commands.
815 COMMAND is the prefix for which we seek completion.
816 CONTEXT is the text before COMMAND on the line."
817 (let* ((complete-list
818 (gud-gdb-run-command-fetch-lines (concat "complete " context command)
819 (current-buffer)
820 ;; From string-match above.
821 (length context))))
822 ;; Protect against old versions of GDB.
823 (and complete-list
824 (string-match "^Undefined command: \"complete\"" (car complete-list))
825 (error "This version of GDB doesn't support the `complete' command"))
826 (gud-gdb-completions-1 complete-list)))
828 ;; This function is also used by `gud-gdbmi-completions'.
829 (defun gud-gdb-completions-1 (complete-list)
830 ;; Sort the list like readline.
831 (setq complete-list (sort complete-list (function string-lessp)))
832 ;; Remove duplicates.
833 (let ((first complete-list)
834 (second (cdr complete-list)))
835 (while second
836 (if (string-equal (car first) (car second))
837 (setcdr first (setq second (cdr second)))
838 (setq first second
839 second (cdr second)))))
840 ;; Add a trailing single quote if there is a unique completion
841 ;; and it contains an odd number of unquoted single quotes.
842 (and (= (length complete-list) 1)
843 (let ((str (car complete-list))
844 (pos 0)
845 (count 0))
846 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
847 (setq count (1+ count)
848 pos (match-end 0)))
849 (and (= (mod count 2) 1)
850 (setq complete-list (list (concat str "'"))))))
851 complete-list)
853 (defun gud-gdb-completion-at-point ()
854 "Return the data to complete the GDB command before point."
855 (let ((end (point))
856 (start
857 (save-excursion
858 (skip-chars-backward "^ " (comint-line-beginning-position))
859 (point))))
860 ;; FIXME: `gud-gdb-run-command-fetch-lines' has some nasty side-effects on
861 ;; the buffer (via `gud-delete-prompt-marker'): it removes the prompt and
862 ;; then re-adds it later, thus messing up markers and overlays along the
863 ;; way (bug#18282).
864 ;; We use an "insert-before" marker for `start', since it's typically right
865 ;; after the prompt, which works around the problem, but is a hack (and
866 ;; comes with other downsides, e.g. if completion adds text at `start').
867 (list (copy-marker start t) end
868 (completion-table-dynamic
869 (apply-partially gud-gdb-completion-function
870 (buffer-substring (comint-line-beginning-position)
871 start))))))
873 ;; (defun gud-gdb-complete-command ()
874 ;; "Perform completion on the GDB command preceding point.
875 ;; This is implemented using the GDB `complete' command which isn't
876 ;; available with older versions of GDB."
877 ;; (interactive)
878 ;; (apply #'completion-in-region (gud-gdb-completion-at-point)))
880 ;; The completion process filter is installed temporarily to slurp the
881 ;; output of GDB up to the next prompt and build the completion list.
882 (defun gud-gdb-fetch-lines-filter (string)
883 "Filter used to read the list of lines output by a command.
884 STRING is the output to filter.
885 It is passed through `gud-gdb-marker-filter' before we look at it."
886 (setq string (gud-gdb-marker-filter string))
887 (setq string (concat gud-gdb-fetch-lines-string string))
888 (while (string-match "\n" string)
889 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
890 gud-gdb-fetched-lines)
891 (setq string (substring string (match-end 0))))
892 (if (string-match comint-prompt-regexp string)
893 (progn
894 (setq gud-gdb-fetch-lines-in-progress nil)
895 string)
896 (progn
897 (setq gud-gdb-fetch-lines-string string)
898 "")))
900 ;; gdb speedbar functions
902 ;; Part of the macro expansion of dframe-with-attached-buffer.
903 ;; At runtime, will be pulled in as a require of speedbar.
904 (declare-function dframe-select-attached-frame "dframe" (&optional frame))
905 (declare-function dframe-maybee-jump-to-attached-frame "dframe" ())
907 (defun gud-gdb-goto-stackframe (_text token _indent)
908 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
909 (dframe-with-attached-buffer
910 (gud-basic-call (concat "server frame " (nth 1 token)))
911 (sit-for 1)))
913 (defvar gud-gdb-fetched-stack-frame nil
914 "Stack frames we are fetching from GDB.")
916 (defun gud-gdb-get-stackframe (buffer)
917 "Extract the current stack frame out of the GUD GDB BUFFER."
918 (let ((newlst nil)
919 (fetched-stack-frame-list
920 (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
921 (if (and (car fetched-stack-frame-list)
922 (string-match "No stack" (car fetched-stack-frame-list)))
923 ;; Go into some other mode???
925 (dolist (e fetched-stack-frame-list)
926 (let ((name nil) (num nil))
927 (if (not (or
928 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
929 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
930 (if (not (string-match
931 "at \\([-0-9a-zA-Z_/.]+\\):\\([0-9]+\\)$" e))
933 (setcar newlst
934 (list (nth 0 (car newlst))
935 (nth 1 (car newlst))
936 (match-string 1 e)
937 (match-string 2 e))))
938 (setq num (match-string 1 e)
939 name (match-string 2 e))
940 (setq newlst
941 (cons
942 (if (string-match
943 "at \\([-0-9a-zA-Z_/.]+\\):\\([0-9]+\\)$" e)
944 (list name num (match-string 1 e)
945 (match-string 2 e))
946 (list name num))
947 newlst)))))
948 (nreverse newlst))))
950 ;(defun gud-gdb-selected-frame-info (buffer)
951 ; "Learn GDB information for the currently selected stack frame in BUFFER."
954 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
955 "Run COMMAND, and return the list of lines it outputs.
956 BUFFER is the current buffer which may be the GUD buffer in which to run.
957 SKIP is the number of chars to skip on each line, it defaults to 0."
958 (with-current-buffer gud-comint-buffer
959 (unless (and (eq gud-comint-buffer buffer)
960 (save-excursion
961 (goto-char (point-max))
962 (forward-line 0)
963 (not (looking-at comint-prompt-regexp))))
964 (let ((gud-gdb-fetch-lines-in-progress t)
965 (gud-gdb-fetched-lines nil)
966 (gud-gdb-fetch-lines-string nil)
967 (gud-gdb-fetch-lines-break (or skip 0))
968 (gud-marker-filter #'gud-gdb-fetch-lines-filter))
969 ;; Issue the command to GDB.
970 (gud-basic-call command)
971 ;; Slurp the output.
972 (while gud-gdb-fetch-lines-in-progress
973 (accept-process-output (get-buffer-process gud-comint-buffer)))
974 (nreverse gud-gdb-fetched-lines)))))
977 ;; ======================================================================
978 ;; sdb functions
980 ;; History of argument lists passed to sdb.
981 (defvar gud-sdb-history nil)
983 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
984 "If nil, we're on a System V Release 4 and don't need the tags hack.")
986 (defvar gud-sdb-lastfile nil)
988 (defun gud-sdb-marker-filter (string)
989 (setq gud-marker-acc
990 (if gud-marker-acc (concat gud-marker-acc string) string))
991 (let (start)
992 ;; Process all complete markers in this chunk
993 (while
994 (cond
995 ;; System V Release 3.2 uses this format
996 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
997 gud-marker-acc start)
998 (setq gud-last-frame
999 (cons (match-string 3 gud-marker-acc)
1000 (string-to-number (match-string 4 gud-marker-acc)))))
1001 ;; System V Release 4.0 quite often clumps two lines together
1002 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
1003 gud-marker-acc start)
1004 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
1005 (setq gud-last-frame
1006 (cons gud-sdb-lastfile
1007 (string-to-number (match-string 3 gud-marker-acc)))))
1008 ;; System V Release 4.0
1009 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
1010 gud-marker-acc start)
1011 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
1012 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
1013 gud-marker-acc start))
1014 (setq gud-last-frame
1015 (cons gud-sdb-lastfile
1016 (string-to-number (match-string 1 gud-marker-acc)))))
1018 (setq gud-sdb-lastfile nil)))
1019 (setq start (match-end 0)))
1021 ;; Search for the last incomplete line in this chunk
1022 (while (string-match "\n" gud-marker-acc start)
1023 (setq start (match-end 0)))
1025 ;; If we have an incomplete line, store it in gud-marker-acc.
1026 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
1027 string)
1029 (defun gud-sdb-find-file (f)
1030 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
1032 ;;;###autoload
1033 (defun sdb (command-line)
1034 "Run sdb on program FILE in buffer *gud-FILE*.
1035 The directory containing FILE becomes the initial working directory
1036 and source-file directory for your debugger."
1037 (interactive (list (gud-query-cmdline 'sdb)))
1039 (if gud-sdb-needs-tags (require 'etags))
1040 (if (and gud-sdb-needs-tags
1041 (not (and (boundp 'tags-file-name)
1042 (stringp tags-file-name)
1043 (file-exists-p tags-file-name))))
1044 (error "The sdb support requires a valid tags table to work"))
1046 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
1047 (set (make-local-variable 'gud-minor-mode) 'sdb)
1049 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
1050 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
1051 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
1052 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
1053 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
1054 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1055 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1056 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
1058 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
1059 (setq paragraph-start comint-prompt-regexp)
1060 (run-hooks 'sdb-mode-hook)
1063 ;; ======================================================================
1064 ;; dbx functions
1066 ;; History of argument lists passed to dbx.
1067 (defvar gud-dbx-history nil)
1069 (defcustom gud-dbx-directories nil
1070 "A list of directories that dbx should search for source code.
1071 If nil, only source files in the program directory
1072 will be known to dbx.
1074 The file names should be absolute, or relative to the directory
1075 containing the executable being debugged."
1076 :type '(choice (const :tag "Current Directory" nil)
1077 (repeat :value ("")
1078 directory))
1079 :group 'gud)
1081 (defun gud-dbx-massage-args (_file args)
1082 (nconc (let ((directories gud-dbx-directories)
1083 (result nil))
1084 (while directories
1085 (setq result (cons (car directories) (cons "-I" result)))
1086 (setq directories (cdr directories)))
1087 (nreverse result))
1088 args))
1090 (defun gud-dbx-marker-filter (string)
1091 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
1093 (let (start)
1094 ;; Process all complete markers in this chunk.
1095 (while (or (string-match
1096 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1097 gud-marker-acc start)
1098 (string-match
1099 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1100 gud-marker-acc start))
1101 (setq gud-last-frame
1102 (cons (match-string 2 gud-marker-acc)
1103 (string-to-number (match-string 1 gud-marker-acc)))
1104 start (match-end 0)))
1106 ;; Search for the last incomplete line in this chunk
1107 (while (string-match "\n" gud-marker-acc start)
1108 (setq start (match-end 0)))
1110 ;; If the incomplete line APPEARS to begin with another marker, keep it
1111 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1112 ;; unnecessary concat during the next call.
1113 (setq gud-marker-acc
1114 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
1115 (substring gud-marker-acc (match-beginning 0))
1116 nil)))
1117 string)
1119 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
1120 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
1121 (defvar gud-mips-p
1122 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
1123 ;; We haven't tested gud on this system:
1124 (string-match "^mips-[^-]*-riscos" system-configuration)
1125 ;; It's documented on OSF/1.3
1126 (string-match "^mips-[^-]*-osf1" system-configuration)
1127 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
1128 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
1130 (defvar gud-dbx-command-name
1131 (concat "dbx" (if gud-mips-p " -emacs")))
1133 ;; This is just like the gdb one except for the regexps since we need to cope
1134 ;; with an optional breakpoint number in [] before the ^Z^Z
1135 (defun gud-mipsdbx-marker-filter (string)
1136 (setq gud-marker-acc (concat gud-marker-acc string))
1137 (let ((output ""))
1139 ;; Process all the complete markers in this chunk.
1140 (while (string-match
1141 ;; This is like th gdb marker but with an optional
1142 ;; leading break point number like `[1] '
1143 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1144 gud-marker-acc)
1145 (setq
1147 ;; Extract the frame position from the marker.
1148 gud-last-frame
1149 (cons (match-string 1 gud-marker-acc)
1150 (string-to-number (match-string 2 gud-marker-acc)))
1152 ;; Append any text before the marker to the output we're going
1153 ;; to return - we don't include the marker in this text.
1154 output (concat output
1155 (substring gud-marker-acc 0 (match-beginning 0)))
1157 ;; Set the accumulator to the remaining text.
1158 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1160 ;; Does the remaining text look like it might end with the
1161 ;; beginning of another marker? If it does, then keep it in
1162 ;; gud-marker-acc until we receive the rest of it. Since we
1163 ;; know the full marker regexp above failed, it's pretty simple to
1164 ;; test for marker starts.
1165 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
1166 (progn
1167 ;; Everything before the potential marker start can be output.
1168 (setq output (concat output (substring gud-marker-acc
1169 0 (match-beginning 0))))
1171 ;; Everything after, we save, to combine with later input.
1172 (setq gud-marker-acc
1173 (substring gud-marker-acc (match-beginning 0))))
1175 (setq output (concat output gud-marker-acc)
1176 gud-marker-acc ""))
1178 output))
1180 ;; The dbx in IRIX is a pain. It doesn't print the file name when
1181 ;; stopping at a breakpoint (but you do get it from the `up' and
1182 ;; `down' commands...). The only way to extract the information seems
1183 ;; to be with a `file' command, although the current line number is
1184 ;; available in $curline. Thus we have to look for output which
1185 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
1186 ;; to output the information we want with a combination of the
1187 ;; `printf' and `file' commands as a pseudo marker which we can
1188 ;; recognize next time through the marker-filter. This would be like
1189 ;; the gdb marker but you can't get the file name without a newline...
1190 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
1191 ;; number rather than a line number etc. Maybe this could be made to
1192 ;; work by listing all the breakpoints and picking the one(s) with the
1193 ;; correct line number, but life's too short.
1194 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
1196 (defvar gud-irix-p nil
1197 "Non-nil to assume the interface appropriate for IRIX dbx.
1198 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
1199 a better solution in 6.1 upwards.")
1200 (defvar gud-dbx-use-stopformat-p nil
1201 "Non-nil to use the dbx feature present at least from Irix 6.1
1202 whereby $stopformat=1 produces an output format compatible with
1203 `gud-dbx-marker-filter'.")
1204 ;; [Irix dbx seemed to be a moving target. The dbx output changed
1205 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
1206 ;; the output from `up' is no longer spotted by gud (and it's probably
1207 ;; not distinctive enough to try to match it -- use C-<, C->
1208 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
1209 ;; `long long'(why?!), so the printf stuff needed changing. The line
1210 ;; number was cast to `long' as a compromise between the new `long
1211 ;; long' and the original `int'. This was reported not to work in 6.2,
1212 ;; so it's changed back to int -- don't make your sources too long.
1213 ;; From Irix6.1 (but not 6.0?) dbx supported an undocumented feature
1214 ;; whereby `set $stopformat=1' reportedly produces output compatible
1215 ;; with `gud-dbx-marker-filter', which we prefer.
1217 ;; The process filter is also somewhat
1218 ;; unreliable, sometimes not spotting the markers; I don't know
1219 ;; whether there's anything that can be done about that.]
1221 ;; this filter is influenced by the xdb one rather than the gdb one
1222 (defun gud-irixdbx-marker-filter (string)
1223 (let (result (case-fold-search nil))
1224 (if (or (string-match comint-prompt-regexp string)
1225 (string-match ".*\012" string))
1226 (setq result (concat gud-marker-acc string)
1227 gud-marker-acc "")
1228 (setq gud-marker-acc (concat gud-marker-acc string)))
1229 (if result
1230 (cond
1231 ;; look for breakpoint or signal indication e.g.:
1232 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
1233 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
1234 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
1235 ((string-match
1236 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
1237 result)
1238 ;; prod dbx into printing out the line number and file
1239 ;; name in a form we can grok as below
1240 (process-send-string (get-buffer-process gud-comint-buffer)
1241 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1242 ;; look for result of, say, "up" e.g.:
1243 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
1244 ;; (this will also catch one of the lines printed by "where")
1245 ((string-match
1246 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
1247 result)
1248 (let ((file (match-string 1 result)))
1249 (if (file-exists-p file)
1250 (setq gud-last-frame
1251 (cons (match-string 1 result)
1252 (string-to-number (match-string 2 result))))))
1253 result)
1254 ((string-match ; kluged-up marker as above
1255 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
1256 (let ((file (gud-file-name (match-string 2 result))))
1257 (if (and file (file-exists-p file))
1258 (setq gud-last-frame
1259 (cons file
1260 (string-to-number (match-string 1 result))))))
1261 (setq result (substring result 0 (match-beginning 0))))))
1262 (or result "")))
1264 ;; There are a couple of differences between DG's dbx output and normal
1265 ;; dbx output which make it nontrivial to integrate this into the
1266 ;; standard dbx-marker-filter (mainly, there are a different number of
1267 ;; backreferences). The markers look like:
1269 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
1271 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
1272 ;; number), and
1274 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
1276 ;; from signals and
1278 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
1280 ;; from up/down/where.
1282 (defun gud-dguxdbx-marker-filter (string)
1283 (setq gud-marker-acc (if gud-marker-acc
1284 (concat gud-marker-acc string)
1285 string))
1286 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
1287 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
1288 start)
1289 ;; Process all complete markers in this chunk.
1290 (while (string-match re gud-marker-acc start)
1291 (setq gud-last-frame
1292 (cons (match-string 4 gud-marker-acc)
1293 (string-to-number (match-string 3 gud-marker-acc)))
1294 start (match-end 0)))
1296 ;; Search for the last incomplete line in this chunk
1297 (while (string-match "\n" gud-marker-acc start)
1298 (setq start (match-end 0)))
1300 ;; If the incomplete line APPEARS to begin with another marker, keep it
1301 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1302 ;; unnecessary concat during the next call.
1303 (setq gud-marker-acc
1304 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
1305 (substring gud-marker-acc (match-beginning 0))
1306 nil)))
1307 string)
1309 ;;;###autoload
1310 (defun dbx (command-line)
1311 "Run dbx on program FILE in buffer *gud-FILE*.
1312 The directory containing FILE becomes the initial working directory
1313 and source-file directory for your debugger."
1314 (interactive (list (gud-query-cmdline 'dbx)))
1316 (cond
1317 (gud-mips-p
1318 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1319 (gud-irix-p
1320 (gud-common-init command-line 'gud-dbx-massage-args
1321 'gud-irixdbx-marker-filter))
1323 (gud-common-init command-line 'gud-dbx-massage-args
1324 'gud-dbx-marker-filter)))
1326 (set (make-local-variable 'gud-minor-mode) 'dbx)
1328 (cond
1329 (gud-mips-p
1330 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1331 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1332 (gud-def gud-break "stop at \"%f\":%l"
1333 "\C-b" "Set breakpoint at current line.")
1334 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1335 (gud-irix-p
1336 (gud-def gud-break "stop at \"%d%f\":%l"
1337 "\C-b" "Set breakpoint at current line.")
1338 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1339 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1340 "<" "Up (numeric arg) stack frames.")
1341 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1342 ">" "Down (numeric arg) stack frames.")
1343 ;; Make dbx give out the source location info that we need.
1344 (process-send-string (get-buffer-process gud-comint-buffer)
1345 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1347 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1348 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1349 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1350 "\C-b" "Set breakpoint at current line.")
1351 (if gud-dbx-use-stopformat-p
1352 (process-send-string (get-buffer-process gud-comint-buffer)
1353 "set $stopformat=1\n"))))
1355 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1356 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1357 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1358 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1359 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
1360 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1361 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1362 (gud-def gud-run "run" nil "Run the program.")
1364 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1365 (setq paragraph-start comint-prompt-regexp)
1366 (run-hooks 'dbx-mode-hook)
1369 ;; ======================================================================
1370 ;; xdb (HP PA-RISC debugger) functions
1372 ;; History of argument lists passed to xdb.
1373 (defvar gud-xdb-history nil)
1375 (defcustom gud-xdb-directories nil
1376 "A list of directories that xdb should search for source code.
1377 If nil, only source files in the program directory
1378 will be known to xdb.
1380 The file names should be absolute, or relative to the directory
1381 containing the executable being debugged."
1382 :type '(choice (const :tag "Current Directory" nil)
1383 (repeat :value ("")
1384 directory))
1385 :group 'gud)
1387 (defun gud-xdb-massage-args (_file args)
1388 (nconc (let ((directories gud-xdb-directories)
1389 (result nil))
1390 (while directories
1391 (setq result (cons (car directories) (cons "-d" result)))
1392 (setq directories (cdr directories)))
1393 (nreverse result))
1394 args))
1396 ;; xdb does not print the lines all at once, so we have to accumulate them
1397 (defun gud-xdb-marker-filter (string)
1398 (let (result)
1399 (if (or (string-match comint-prompt-regexp string)
1400 (string-match ".*\012" string))
1401 (setq result (concat gud-marker-acc string)
1402 gud-marker-acc "")
1403 (setq gud-marker-acc (concat gud-marker-acc string)))
1404 (if result
1405 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1406 result)
1407 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1408 result))
1409 (let ((line (string-to-number (match-string 2 result)))
1410 (file (gud-file-name (match-string 1 result))))
1411 (if file
1412 (setq gud-last-frame (cons file line))))))
1413 (or result "")))
1415 ;;;###autoload
1416 (defun xdb (command-line)
1417 "Run xdb on program FILE in buffer *gud-FILE*.
1418 The directory containing FILE becomes the initial working directory
1419 and source-file directory for your debugger.
1421 You can set the variable `gud-xdb-directories' to a list of program source
1422 directories if your program contains sources from more than one directory."
1423 (interactive (list (gud-query-cmdline 'xdb)))
1425 (gud-common-init command-line 'gud-xdb-massage-args
1426 'gud-xdb-marker-filter)
1427 (set (make-local-variable 'gud-minor-mode) 'xdb)
1429 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1430 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1431 "Set temporary breakpoint at current line.")
1432 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1433 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1434 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1435 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1436 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1437 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1438 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1439 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1441 (setq comint-prompt-regexp "^>")
1442 (setq paragraph-start comint-prompt-regexp)
1443 (run-hooks 'xdb-mode-hook))
1445 ;; ======================================================================
1446 ;; perldb functions
1448 ;; History of argument lists passed to perldb.
1449 (defvar gud-perldb-history nil)
1451 (defun gud-perldb-massage-args (_file args)
1452 "Convert a command line as would be typed normally to run perldb
1453 into one that invokes an Emacs-enabled debugging session.
1454 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1455 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1456 ;; arguments ?
1457 (let* ((new-args nil)
1458 (seen-e nil)
1459 (shift (lambda () (push (pop args) new-args))))
1461 ;; Pass all switches and -e scripts through.
1462 (while (and args
1463 (string-match "^-" (car args))
1464 (not (equal "-" (car args)))
1465 (not (equal "--" (car args))))
1466 (when (equal "-e" (car args))
1467 ;; -e goes with the next arg, so shift one extra.
1468 (or (funcall shift)
1469 ;; -e as the last arg is an error in Perl.
1470 (error "No code specified for -e"))
1471 (setq seen-e t))
1472 (funcall shift))
1474 (unless seen-e
1475 (if (or (not args)
1476 (string-match "^-" (car args)))
1477 (error "Can't use stdin as the script to debug"))
1478 ;; This is the program name.
1479 (funcall shift))
1481 ;; If -e specified, make sure there is a -- so -emacs is not taken
1482 ;; as -e macs.
1483 (if (and args (equal "--" (car args)))
1484 (funcall shift)
1485 (and seen-e (push "--" new-args)))
1487 (push "-emacs" new-args)
1488 (while args
1489 (funcall shift))
1491 (nreverse new-args)))
1493 ;; There's no guarantee that Emacs will hand the filter the entire
1494 ;; marker at once; it could be broken up across several strings. We
1495 ;; might even receive a big chunk with several markers in it. If we
1496 ;; receive a chunk of text which looks like it might contain the
1497 ;; beginning of a marker, we save it here between calls to the
1498 ;; filter.
1499 (defun gud-perldb-marker-filter (string)
1500 (setq gud-marker-acc (concat gud-marker-acc string))
1501 (let ((output ""))
1503 ;; Process all the complete markers in this chunk.
1505 ;; Here I match the string coming out of perldb.
1506 ;; The strings can look like any of
1508 ;; "\032\032/tmp/tst.pl:6:0\n"
1509 ;; "\032\032(eval 5)[/tmp/tst.pl:6]:3:0\n"
1510 ;; "\032\032(eval 17)[Basic/Core/Core.pm.PL (i.e. PDL::Core.pm):2931]:1:0\n"
1512 ;; From those I want the filename and the line number. First I look for
1513 ;; the eval case. If that doesn't match, I look for the "normal" case.
1514 (while
1515 (string-match
1516 (eval-when-compile
1517 (let ((file-re "\\(?:[a-zA-Z]:\\)?[^:\n]*"))
1518 (concat "\032\032\\(?:"
1519 (concat
1520 "(eval [0-9]+)\\["
1521 "\\(" file-re "\\)" ; Filename.
1522 "\\(?: (i\\.e\\. [^)]*)\\)?"
1523 ":\\([0-9]*\\)\\]") ; Line number.
1524 "\\|"
1525 (concat
1526 "\\(?1:" file-re "\\)" ; Filename.
1527 ":\\(?2:[0-9]*\\)") ; Line number.
1528 "\\):.*\n")))
1529 gud-marker-acc)
1530 (setq
1532 ;; Extract the frame position from the marker.
1533 gud-last-frame
1534 (cons (match-string 1 gud-marker-acc)
1535 (string-to-number (match-string 2 gud-marker-acc)))
1537 ;; Append any text before the marker to the output we're going
1538 ;; to return - we don't include the marker in this text.
1539 output (concat output
1540 (substring gud-marker-acc 0 (match-beginning 0)))
1542 ;; Set the accumulator to the remaining text.
1543 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1545 ;; Does the remaining text look like it might end with the
1546 ;; beginning of another marker? If it does, then keep it in
1547 ;; gud-marker-acc until we receive the rest of it. Since we
1548 ;; know the full marker regexp above failed, it's pretty simple to
1549 ;; test for marker starts.
1550 (if (string-match "\032.*\\'" gud-marker-acc)
1551 (progn
1552 ;; Everything before the potential marker start can be output.
1553 (setq output (concat output (substring gud-marker-acc
1554 0 (match-beginning 0))))
1556 ;; Everything after, we save, to combine with later input.
1557 (setq gud-marker-acc
1558 (substring gud-marker-acc (match-beginning 0))))
1560 (setq output (concat output gud-marker-acc)
1561 gud-marker-acc ""))
1563 output))
1565 (defcustom gud-perldb-command-name "perl -d"
1566 "Default command to execute a Perl script under debugger."
1567 :type 'string
1568 :group 'gud)
1570 ;;;###autoload
1571 (defun perldb (command-line)
1572 "Run perldb on program FILE in buffer *gud-FILE*.
1573 The directory containing FILE becomes the initial working directory
1574 and source-file directory for your debugger."
1575 (interactive
1576 (list (gud-query-cmdline 'perldb
1577 (concat (or (buffer-file-name) "-e 0") " "))))
1579 (gud-common-init command-line 'gud-perldb-massage-args
1580 'gud-perldb-marker-filter)
1581 (set (make-local-variable 'gud-minor-mode) 'perldb)
1583 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1584 (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
1585 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1586 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1587 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1588 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1589 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1590 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1591 (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
1592 (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
1595 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1596 (setq paragraph-start comint-prompt-regexp)
1597 (run-hooks 'perldb-mode-hook))
1599 ;; ======================================================================
1600 ;; pdb (Python debugger) functions
1602 ;; History of argument lists passed to pdb.
1603 (defvar gud-pdb-history nil)
1605 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1606 ;; Either file or function name may be omitted: "> <string>(0)?()"
1607 (defvar gud-pdb-marker-regexp
1608 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\|<module>\\)()\\(->[^\n\r]*\\)?[\n\r]")
1610 (defvar gud-pdb-marker-regexp-file-group 1)
1611 (defvar gud-pdb-marker-regexp-line-group 2)
1612 (defvar gud-pdb-marker-regexp-fnname-group 3)
1614 (defvar gud-pdb-marker-regexp-start "^> ")
1616 ;; There's no guarantee that Emacs will hand the filter the entire
1617 ;; marker at once; it could be broken up across several strings. We
1618 ;; might even receive a big chunk with several markers in it. If we
1619 ;; receive a chunk of text which looks like it might contain the
1620 ;; beginning of a marker, we save it here between calls to the
1621 ;; filter.
1622 (defun gud-pdb-marker-filter (string)
1623 (setq gud-marker-acc (concat gud-marker-acc string))
1624 (let ((output ""))
1626 ;; Process all the complete markers in this chunk.
1627 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1628 (setq
1630 ;; Extract the frame position from the marker.
1631 gud-last-frame
1632 (let ((file (match-string gud-pdb-marker-regexp-file-group
1633 gud-marker-acc))
1634 (line (string-to-number
1635 (match-string gud-pdb-marker-regexp-line-group
1636 gud-marker-acc))))
1637 (if (string-equal file "<string>")
1638 gud-last-frame
1639 (cons file line)))
1641 ;; Output everything instead of the below
1642 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1643 ;; ;; Append any text before the marker to the output we're going
1644 ;; ;; to return - we don't include the marker in this text.
1645 ;; output (concat output
1646 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1648 ;; Set the accumulator to the remaining text.
1649 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1651 ;; Does the remaining text look like it might end with the
1652 ;; beginning of another marker? If it does, then keep it in
1653 ;; gud-marker-acc until we receive the rest of it. Since we
1654 ;; know the full marker regexp above failed, it's pretty simple to
1655 ;; test for marker starts.
1656 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1657 (progn
1658 ;; Everything before the potential marker start can be output.
1659 (setq output (concat output (substring gud-marker-acc
1660 0 (match-beginning 0))))
1662 ;; Everything after, we save, to combine with later input.
1663 (setq gud-marker-acc
1664 (substring gud-marker-acc (match-beginning 0))))
1666 (setq output (concat output gud-marker-acc)
1667 gud-marker-acc ""))
1669 output))
1671 (defcustom gud-pdb-command-name "pdb"
1672 "File name for executing the Python debugger.
1673 This should be an executable on your path, or an absolute file name."
1674 :type 'string
1675 :group 'gud)
1677 ;;;###autoload
1678 (defun pdb (command-line)
1679 "Run pdb on program FILE in buffer `*gud-FILE*'.
1680 The directory containing FILE becomes the initial working directory
1681 and source-file directory for your debugger."
1682 (interactive
1683 (list (gud-query-cmdline 'pdb)))
1685 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1686 (set (make-local-variable 'gud-minor-mode) 'pdb)
1688 (gud-def gud-break "break %d%f:%l" "\C-b" "Set breakpoint at current line.")
1689 (gud-def gud-remove "clear %d%f:%l" "\C-d" "Remove breakpoint at current line")
1690 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1691 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1692 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1693 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1694 (gud-def gud-up "up" "<" "Up one stack frame.")
1695 (gud-def gud-down "down" ">" "Down one stack frame.")
1696 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1697 ;; Is this right?
1698 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1700 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1701 (setq comint-prompt-regexp "^(Pdb) *")
1702 (setq paragraph-start comint-prompt-regexp)
1703 (run-hooks 'pdb-mode-hook))
1705 ;; ======================================================================
1706 ;; Guile REPL (guiler) functions
1708 ;; History of argument lists passed to guiler.
1709 (defvar gud-guiler-history nil)
1711 (defvar gud-guiler-lastfile nil)
1713 (defun gud-guiler-marker-filter (string)
1714 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
1716 (let ((start 0))
1717 (while
1718 (cond
1719 ((string-match "^In \\(.*\\):" gud-marker-acc start)
1720 (setq gud-guiler-lastfile (match-string 1 gud-marker-acc)))
1721 ((string-match "^\\([^:\n]+\\):\\([0-9]+\\):\\([0-9]+\\):[^\n]*"
1722 gud-marker-acc start)
1723 (setq gud-guiler-lastfile (match-string 1 gud-marker-acc))
1724 (setq gud-last-frame
1725 (cons gud-guiler-lastfile
1726 (string-to-number (match-string 2 gud-marker-acc)))))
1727 ((string-match "^[ ]*\\([0-9]+\\):\\([0-9]+\\) [^\n]*"
1728 gud-marker-acc start)
1729 (if gud-guiler-lastfile
1730 (setq gud-last-frame
1731 (cons gud-guiler-lastfile
1732 (string-to-number (match-string 1 gud-marker-acc))))))
1733 ((string-match comint-prompt-regexp gud-marker-acc start) t)
1734 ((string= (substring gud-marker-acc start) "") nil)
1735 (t nil))
1736 (setq start (match-end 0)))
1738 ;; Search for the last incomplete line in this chunk
1739 (while (string-match "\n" gud-marker-acc start)
1740 (setq start (match-end 0)))
1742 ;; If we have an incomplete line, store it in gud-marker-acc.
1743 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
1744 string)
1747 (defcustom gud-guiler-command-name "guile"
1748 "File name for executing the Guile debugger.
1749 This should be an executable on your path, or an absolute file name."
1750 :version "25.1"
1751 :type 'string
1752 :group 'gud)
1754 ;;;###autoload
1755 (defun guiler (command-line)
1756 "Run guiler on program FILE in buffer `*gud-FILE*'.
1757 The directory containing FILE becomes the initial working directory
1758 and source-file directory for your debugger."
1759 (interactive
1760 (list (gud-query-cmdline 'guiler)))
1762 (gud-common-init command-line nil 'gud-guiler-marker-filter)
1763 (setq-local gud-minor-mode 'guiler)
1765 ;; FIXME: absolute file-names are not grokked yet by Guile's ,break-at-source
1766 ;; and relative file names only when relative to %load-path.
1767 ;; (gud-def gud-break ",break-at-source %d%f %l" "\C-b" "Set breakpoint at current line.")
1768 (gud-def gud-break ",break-at-source %f %l" "\C-b" "Set breakpoint at current line.")
1769 ;; FIXME: remove breakpoint with file-line not yet supported by Guile
1770 ;; (gud-def gud-remove ",delete ---> %d%f:%l" "\C-d" "Remove breakpoint at current line")
1771 (gud-def gud-step ",step" "\C-s" "Step one source line with display.")
1772 (gud-def gud-next ",next" "\C-n" "Step one line (skip functions).")
1773 ;; (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1774 (gud-def gud-finish ",finish" "\C-f" "Finish executing current function.")
1775 (gud-def gud-up ",up" "<" "Up one stack frame.")
1776 (gud-def gud-down ",down" ">" "Down one stack frame.")
1777 (gud-def gud-print "%e" "\C-p" "Evaluate Guile expression at point.")
1779 (setq comint-prompt-regexp "^scheme@([^>]+> ")
1780 (setq paragraph-start comint-prompt-regexp)
1781 (run-hooks 'guiler-mode-hook))
1783 ;; ======================================================================
1785 ;; JDB support.
1787 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1788 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1790 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1791 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1792 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1794 ;; INVOCATION NOTES:
1796 ;; You invoke jdb-mode with:
1798 ;; M-x jdb <enter>
1800 ;; It responds with:
1802 ;; Run jdb (like this): jdb
1804 ;; type any jdb switches followed by the name of the class you'd like to debug.
1805 ;; Supply a fully qualified classname (these don't have the ".class" extension)
1806 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1807 ;; See the known problems section below for restrictions when specifying jdb
1808 ;; command line switches (search forward for '-classpath').
1810 ;; You should see something like the following:
1812 ;; Current directory is ~/src/java/hello/
1813 ;; Initializing jdb...
1814 ;; 0xed2f6628:class(hello)
1815 ;; >
1817 ;; To set an initial breakpoint try:
1819 ;; > stop in hello.main
1820 ;; Breakpoint set in hello.main
1821 ;; >
1823 ;; To execute the program type:
1825 ;; > run
1826 ;; run hello
1828 ;; Breakpoint hit: running ...
1829 ;; hello.main (hello:12)
1831 ;; Type M-n to step over the current line and M-s to step into it. That,
1832 ;; along with the JDB 'help' command should get you started. The 'quit'
1833 ;; JDB command will get out of the debugger. There is some truly
1834 ;; pathetic JDB documentation available at:
1836 ;; http://java.sun.com/products/jdk/1.1/debugging/
1838 ;; KNOWN PROBLEMS AND FIXME's:
1840 ;; Not sure what happens with inner classes ... haven't tried them.
1842 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1844 ;; You must not put whitespace between "-classpath" and the path to
1845 ;; search for java classes even though it is required when invoking jdb
1846 ;; from the command line. See gud-jdb-massage-args for details.
1847 ;; The same applies for "-sourcepath".
1849 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1850 ;; refer to the documentation of `gud-jdb-use-classpath' and
1851 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1852 ;; on using the classpath for locating java source files.
1854 ;; If any of the source files in the directories listed in
1855 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1856 ;; every file ending in ".java" in these directories parses without error.
1858 ;; All the .java files in the directories in gud-jdb-directories are
1859 ;; syntactically analyzed each time gud jdb is invoked. It would be
1860 ;; nice to keep as much information as possible between runs. It would
1861 ;; be really nice to analyze the files only as necessary (when the
1862 ;; source needs to be displayed.) I'm not sure to what extent the former
1863 ;; can be accomplished and I'm not sure the latter can be done at all
1864 ;; since I don't know of any general way to tell which .class files are
1865 ;; defined by which .java file without analyzing all the .java files.
1866 ;; If anyone knows why JavaSoft didn't put the source file names in
1867 ;; debuggable .class files please clue me in so I find something else
1868 ;; to be spiteful and bitter about.
1870 ;; ======================================================================
1871 ;; gud jdb variables and functions
1873 (defcustom gud-jdb-command-name "jdb"
1874 "Command that executes the Java debugger."
1875 :type 'string
1876 :group 'gud)
1878 (defcustom gud-jdb-use-classpath t
1879 "If non-nil, search for Java source files in classpath directories.
1880 The list of directories to search is the value of `gud-jdb-classpath'.
1881 The file pathname is obtained by converting the fully qualified
1882 class information output by jdb to a relative pathname and appending
1883 it to `gud-jdb-classpath' element by element until a match is found.
1885 This method has a significant jdb startup time reduction advantage
1886 since it does not require the scanning of all `gud-jdb-directories'
1887 and parsing all Java files for class information.
1889 Set to nil to use `gud-jdb-directories' to scan java sources for
1890 class information on jdb startup (original method)."
1891 :type 'boolean
1892 :group 'gud)
1894 (defvar gud-jdb-classpath nil
1895 "Java/jdb classpath directories list.
1896 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1897 list automatically using the following methods in sequence
1898 \(with subsequent successful steps overriding the results of previous
1899 steps):
1901 1) Read the CLASSPATH environment variable,
1902 2) Read any \"-classpath\" argument used to run jdb,
1903 or detected in jdb output (e.g. if jdb is run by a script
1904 that echoes the actual jdb command before starting jdb),
1905 3) Send a \"classpath\" command to jdb and scan jdb output for
1906 classpath information if jdb is invoked with an \"-attach\" (to
1907 an already running VM) argument (This case typically does not
1908 have a \"-classpath\" command line argument - that is provided
1909 to the VM when it is started).
1911 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1912 those debuggers do not support the classpath command. Use 1) or 2).")
1914 (defvar gud-jdb-sourcepath nil
1915 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1916 This list is prepended to `gud-jdb-classpath' to form the complete
1917 list of directories searched for source files.")
1919 (defvar gud-marker-acc-max-length 4000
1920 "Maximum number of debugger output characters to keep.
1921 This variable limits the size of `gud-marker-acc' which holds
1922 the most recent debugger output history while searching for
1923 source file information.")
1925 (defvar gud-jdb-history nil
1926 "History of argument lists passed to jdb.")
1929 ;; List of Java source file directories.
1930 (defvar gud-jdb-directories (list ".")
1931 "A list of directories that gud jdb should search for source code.
1932 The file names should be absolute, or relative to the current
1933 directory.
1935 The set of .java files residing in the directories listed are
1936 syntactically analyzed to determine the classes they define and the
1937 packages in which these classes belong. In this way gud jdb maps the
1938 package-qualified class names output by the jdb debugger to the source
1939 file from which the class originated. This allows gud mode to keep
1940 the source code display in sync with the debugging session.")
1942 (defvar gud-jdb-source-files nil
1943 "List of the java source files for this debugging session.")
1945 ;; Association list of fully qualified class names (package + class name)
1946 ;; and their source files.
1947 (defvar gud-jdb-class-source-alist nil
1948 "Association list of fully qualified class names and source files.")
1950 ;; This is used to hold a source file during analysis.
1951 (defvar gud-jdb-analysis-buffer nil)
1953 (defvar gud-jdb-classpath-string nil
1954 "Holds temporary classpath values.")
1956 (defun gud-jdb-build-source-files-list (path extn)
1957 "Return a list of java source files (absolute paths).
1958 PATH gives the directories in which to search for files with
1959 extension EXTN. Normally EXTN is given as the regular expression
1960 \"\\.java$\" ."
1961 (mapcan (lambda (d)
1962 (when (file-directory-p d)
1963 (directory-files d t extn nil)))
1964 path))
1966 ;; Move point past whitespace.
1967 (defun gud-jdb-skip-whitespace ()
1968 (skip-chars-forward " \n\r\t\014"))
1970 ;; Move point past a "// <eol>" type of comment.
1971 (defun gud-jdb-skip-single-line-comment ()
1972 (end-of-line))
1974 ;; Move point past a "/* */" or "/** */" type of comment.
1975 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1976 (forward-char 2)
1977 (catch 'break
1978 (while (not (eobp))
1979 (if (eq (following-char) ?*)
1980 (progn
1981 (forward-char)
1982 (if (not (eobp))
1983 (if (eq (following-char) ?/)
1984 (progn
1985 (forward-char)
1986 (throw 'break nil)))))
1987 (forward-char)))))
1989 ;; Move point past any number of consecutive whitespace chars and/or comments.
1990 (defun gud-jdb-skip-whitespace-and-comments ()
1991 (gud-jdb-skip-whitespace)
1992 (catch 'done
1993 (while t
1994 (cond
1995 ((looking-at "//")
1996 (gud-jdb-skip-single-line-comment)
1997 (gud-jdb-skip-whitespace))
1998 ((looking-at "/\\*")
1999 (gud-jdb-skip-traditional-or-documentation-comment)
2000 (gud-jdb-skip-whitespace))
2001 (t (throw 'done nil))))))
2003 ;; Move point past things that are id-like. The intent is to skip regular
2004 ;; id's, such as class or interface names as well as package and interface
2005 ;; names.
2006 (defun gud-jdb-skip-id-ish-thing ()
2007 (skip-chars-forward "^ /\n\r\t\014,;{"))
2009 ;; Move point past a string literal.
2010 (defun gud-jdb-skip-string-literal ()
2011 (forward-char)
2012 (while (not (cond
2013 ((eq (following-char) ?\\)
2014 (forward-char))
2015 ((eq (following-char) ?\042))))
2016 (forward-char))
2017 (forward-char))
2019 ;; Move point past a character literal.
2020 (defun gud-jdb-skip-character-literal ()
2021 (forward-char)
2022 (while
2023 (progn
2024 (if (eq (following-char) ?\\)
2025 (forward-char 2))
2026 (not (eq (following-char) ?\')))
2027 (forward-char))
2028 (forward-char))
2030 ;; Move point past the following block. There may be (legal) cruft before
2031 ;; the block's opening brace. There must be a block or it's the end of life
2032 ;; in petticoat junction.
2033 (defun gud-jdb-skip-block ()
2035 ;; Find the beginning of the block.
2036 (while
2037 (not (eq (following-char) ?{))
2039 ;; Skip any constructs that can harbor literal block delimiter
2040 ;; characters and/or the delimiters for the constructs themselves.
2041 (cond
2042 ((looking-at "//")
2043 (gud-jdb-skip-single-line-comment))
2044 ((looking-at "/\\*")
2045 (gud-jdb-skip-traditional-or-documentation-comment))
2046 ((eq (following-char) ?\042)
2047 (gud-jdb-skip-string-literal))
2048 ((eq (following-char) ?\')
2049 (gud-jdb-skip-character-literal))
2050 (t (forward-char))))
2052 ;; Now at the beginning of the block.
2053 (forward-char)
2055 ;; Skip over the body of the block as well as the final brace.
2056 (let ((open-level 1))
2057 (while (not (eq open-level 0))
2058 (cond
2059 ((looking-at "//")
2060 (gud-jdb-skip-single-line-comment))
2061 ((looking-at "/\\*")
2062 (gud-jdb-skip-traditional-or-documentation-comment))
2063 ((eq (following-char) ?\042)
2064 (gud-jdb-skip-string-literal))
2065 ((eq (following-char) ?\')
2066 (gud-jdb-skip-character-literal))
2067 ((eq (following-char) ?{)
2068 (setq open-level (+ open-level 1))
2069 (forward-char))
2070 ((eq (following-char) ?})
2071 (setq open-level (- open-level 1))
2072 (forward-char))
2073 (t (forward-char))))))
2075 ;; Find the package and class definitions in Java source file FILE. Assumes
2076 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
2077 ;; to hold the source during analysis.
2078 (defun gud-jdb-analyze-source (buf file)
2079 (let ((l nil))
2080 (set-buffer buf)
2081 (insert-file-contents file nil nil nil t)
2082 (goto-char 0)
2083 (catch 'abort
2084 (let ((p ""))
2085 (while (progn
2086 (gud-jdb-skip-whitespace)
2087 (not (eobp)))
2088 (cond
2090 ;; Any number of semi's following a block is legal. Move point
2091 ;; past them. Note that comments and whitespace may be
2092 ;; interspersed as well.
2093 ((eq (following-char) ?\073)
2094 (forward-char))
2096 ;; Move point past a single line comment.
2097 ((looking-at "//")
2098 (gud-jdb-skip-single-line-comment))
2100 ;; Move point past a traditional or documentation comment.
2101 ((looking-at "/\\*")
2102 (gud-jdb-skip-traditional-or-documentation-comment))
2104 ;; Move point past a package statement, but save the PackageName.
2105 ((looking-at "package")
2106 (forward-char 7)
2107 (gud-jdb-skip-whitespace-and-comments)
2108 (let ((s (point)))
2109 (gud-jdb-skip-id-ish-thing)
2110 (setq p (concat (buffer-substring s (point)) "."))
2111 (gud-jdb-skip-whitespace-and-comments)
2112 (if (eq (following-char) ?\073)
2113 (forward-char))))
2115 ;; Move point past an import statement.
2116 ((looking-at "import")
2117 (forward-char 6)
2118 (gud-jdb-skip-whitespace-and-comments)
2119 (gud-jdb-skip-id-ish-thing)
2120 (gud-jdb-skip-whitespace-and-comments)
2121 (if (eq (following-char) ?\073)
2122 (forward-char)))
2124 ;; Move point past the various kinds of ClassModifiers.
2125 ((looking-at "public")
2126 (forward-char 6))
2127 ((looking-at "abstract")
2128 (forward-char 8))
2129 ((looking-at "final")
2130 (forward-char 5))
2132 ;; Move point past a ClassDeclaration, but save the class
2133 ;; Identifier.
2134 ((looking-at "class")
2135 (forward-char 5)
2136 (gud-jdb-skip-whitespace-and-comments)
2137 (let ((s (point)))
2138 (gud-jdb-skip-id-ish-thing)
2139 (setq
2140 l (nconc l (list (concat p (buffer-substring s (point)))))))
2141 (gud-jdb-skip-block))
2143 ;; Move point past an interface statement.
2144 ((looking-at "interface")
2145 (forward-char 9)
2146 (gud-jdb-skip-block))
2148 ;; Anything else means the input is invalid.
2150 (message "Error parsing file %s." file)
2151 (throw 'abort nil))))))
2154 (defun gud-jdb-build-class-source-alist-for-file (file)
2155 (mapcar
2156 (lambda (c)
2157 (cons c file))
2158 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
2160 ;; Return an alist of fully qualified classes and the source files
2161 ;; holding their definitions. SOURCES holds a list of all the source
2162 ;; files to examine.
2163 (defun gud-jdb-build-class-source-alist (sources)
2164 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
2165 (prog1
2166 (apply
2167 'nconc
2168 (mapcar
2169 'gud-jdb-build-class-source-alist-for-file
2170 sources))
2171 (kill-buffer gud-jdb-analysis-buffer)
2172 (setq gud-jdb-analysis-buffer nil)))
2174 ;; Change what was given in the minibuffer to something that can be used to
2175 ;; invoke the debugger.
2176 (defun gud-jdb-massage-args (_file args)
2177 ;; The jdb executable must have whitespace between "-classpath" and
2178 ;; its value while gud-common-init expects all switch values to
2179 ;; follow the switch keyword without intervening whitespace. We
2180 ;; require that when the user enters the "-classpath" switch in the
2181 ;; EMACS minibuffer that they do so without the intervening
2182 ;; whitespace. This function adds it back (it's called after
2183 ;; gud-common-init). There are more switches like this (for
2184 ;; instance "-host" and "-password") but I don't care about them
2185 ;; yet.
2186 (if args
2187 (let (massaged-args user-error)
2189 (while (and args (not user-error))
2190 (cond
2191 ((setq user-error (string-match "-classpath$" (car args))))
2192 ((setq user-error (string-match "-sourcepath$" (car args))))
2193 ((string-match "-classpath\\(.+\\)" (car args))
2194 (setq massaged-args
2195 (append massaged-args
2196 (list "-classpath"
2197 (setq gud-jdb-classpath-string
2198 (match-string 1 (car args)))))))
2199 ((string-match "-sourcepath\\(.+\\)" (car args))
2200 (setq massaged-args
2201 (append massaged-args
2202 (list "-sourcepath"
2203 (setq gud-jdb-sourcepath
2204 (match-string 1 (car args)))))))
2205 (t (setq massaged-args (append massaged-args (list (car args))))))
2206 (setq args (cdr args)))
2208 ;; By this point the current directory is all screwed up. Maybe we
2209 ;; could fix things and re-invoke gud-common-init, but for now I think
2210 ;; issuing the error is good enough.
2211 (if user-error
2212 (progn
2213 (kill-buffer (current-buffer))
2214 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
2215 massaged-args)))
2217 ;; Search for an association with P, a fully qualified class name, in
2218 ;; gud-jdb-class-source-alist. The association gives the fully
2219 ;; qualified file name of the source file which produced the class.
2220 (defun gud-jdb-find-source-file (p)
2221 (cdr (assoc p gud-jdb-class-source-alist)))
2223 ;; Note: Reset to this value every time a prompt is seen
2224 (defvar gud-jdb-lowest-stack-level 999)
2226 (defun gud-jdb-find-source-using-classpath (p)
2227 "Find source file corresponding to fully qualified class P.
2228 Convert P from jdb's output, converted to a pathname
2229 relative to a classpath directory."
2230 (save-match-data
2231 (let
2232 (;; Replace dots with slashes and append ".java" to generate file
2233 ;; name relative to classpath
2234 (filename
2235 (concat
2236 (mapconcat 'identity
2237 (split-string
2238 ;; Eliminate any subclass references in the class
2239 ;; name string. These start with a "$"
2240 (if (string-match "$.*" p)
2241 (replace-match "" t t p) p)
2242 "\\.") "/")
2243 ".java"))
2244 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2245 found-file)
2246 (while (and cplist
2247 (not (setq found-file
2248 (file-readable-p
2249 (concat (car cplist) "/" filename)))))
2250 (setq cplist (cdr cplist)))
2251 (if found-file (concat (car cplist) "/" filename)))))
2253 (defun gud-jdb-find-source (_string)
2254 "Alias for function used to locate source files.
2255 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
2256 during jdb initialization depending on the value of
2257 `gud-jdb-use-classpath'."
2258 nil)
2260 (defun gud-jdb-parse-classpath-string (string)
2261 "Parse the classpath list and convert each item to an absolute pathname."
2262 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
2263 (replace-match "" nil nil s) s))
2264 (mapcar 'file-truename
2265 (split-string
2266 string
2267 (concat "[ \t\n\r,\"" path-separator "]+")))))
2269 ;; See commentary for other debugger's marker filters - there you will find
2270 ;; important notes about STRING.
2271 (defun gud-jdb-marker-filter (string)
2273 ;; Build up the accumulator.
2274 (setq gud-marker-acc
2275 (if gud-marker-acc
2276 (concat gud-marker-acc string)
2277 string))
2279 ;; Look for classpath information until gud-jdb-classpath-string is found
2280 ;; (interactive, multiple settings of classpath from jdb
2281 ;; not supported/followed)
2282 (if (and gud-jdb-use-classpath
2283 (not gud-jdb-classpath-string)
2284 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
2285 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
2286 (setq gud-jdb-classpath
2287 (gud-jdb-parse-classpath-string
2288 (setq gud-jdb-classpath-string
2289 (match-string 1 gud-marker-acc)))))
2291 ;; We process STRING from left to right. Each time through the
2292 ;; following loop we process at most one marker. After we've found a
2293 ;; marker, delete gud-marker-acc up to and including the match
2294 (let (file-found)
2295 ;; Process each complete marker in the input.
2296 (while
2298 ;; Do we see a marker?
2299 (string-match
2300 ;; jdb puts out a string of the following form when it
2301 ;; hits a breakpoint:
2303 ;; <fully-qualified-class><method> (<class>:<line-number>)
2305 ;; <fully-qualified-class>'s are composed of Java ID's
2306 ;; separated by periods. <method> and <class> are
2307 ;; also Java ID's. <method> begins with a period and
2308 ;; may contain less-than and greater-than (constructors,
2309 ;; for instance, are called <init> in the symbol table.)
2310 ;; Java ID's begin with a letter followed by letters
2311 ;; and/or digits. The set of letters includes underscore
2312 ;; and dollar sign.
2314 ;; The first group matches <fully-qualified-class>,
2315 ;; the second group matches <class> and the third group
2316 ;; matches <line-number>. We don't care about using
2317 ;; <method> so we don't "group" it.
2319 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
2320 ;; ID's only.
2322 ;; The ".," in the last square-bracket are necessary because
2323 ;; of Sun's total disrespect for backwards compatibility in
2324 ;; reported line numbers from jdb - starting in 1.4.0 they
2325 ;; print line numbers using LOCALE, inserting a comma or a
2326 ;; period at the thousands positions (how ingenious!).
2328 "\\(\\[[0-9]+] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
2329 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9.,]+\\)"
2330 gud-marker-acc)
2332 ;; A good marker is one that:
2333 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
2334 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
2335 ;; since the last prompt
2336 ;; Figure out the line on which to position the debugging arrow.
2337 ;; Return the info as a cons of the form:
2339 ;; (<file-name> . <line-number>) .
2340 (if (if (match-beginning 1)
2341 (let (n)
2342 (setq n (string-to-number (substring
2343 gud-marker-acc
2344 (1+ (match-beginning 1))
2345 (- (match-end 1) 2))))
2346 (if (< n gud-jdb-lowest-stack-level)
2347 (progn (setq gud-jdb-lowest-stack-level n) t)))
2349 (if (setq file-found
2350 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
2351 (setq gud-last-frame
2352 (cons file-found
2353 (string-to-number
2354 (let
2355 ((numstr (match-string 4 gud-marker-acc)))
2356 (if (string-match "[.,]" numstr)
2357 (replace-match "" nil nil numstr)
2358 numstr)))))
2359 (message "Could not find source file.")))
2361 ;; Set the accumulator to the remaining text.
2362 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
2364 (if (string-match comint-prompt-regexp gud-marker-acc)
2365 (setq gud-jdb-lowest-stack-level 999)))
2367 ;; Do not allow gud-marker-acc to grow without bound. If the source
2368 ;; file information is not within the last 3/4
2369 ;; gud-marker-acc-max-length characters, well,...
2370 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
2371 (setq gud-marker-acc
2372 (substring gud-marker-acc
2373 (- (/ (* gud-marker-acc-max-length 3) 4)))))
2375 ;; We don't filter any debugger output so just return what we were given.
2376 string)
2378 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
2380 ;;;###autoload
2381 (defun jdb (command-line)
2382 "Run jdb with command line COMMAND-LINE in a buffer.
2383 The buffer is named \"*gud*\" if no initial class is given or
2384 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
2385 switch is given, omit all whitespace between it and its value.
2387 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
2388 information on how jdb accesses source files. Alternatively (if
2389 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
2390 original source file access method.
2392 For general information about commands available to control jdb from
2393 gud, see `gud-mode'."
2394 (interactive
2395 (list (gud-query-cmdline 'jdb)))
2396 (setq gud-jdb-classpath nil)
2397 (setq gud-jdb-sourcepath nil)
2399 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
2400 ;; if CLASSPATH is set.
2401 (setq gud-jdb-classpath-string (or (getenv "CLASSPATH") "."))
2402 (if gud-jdb-classpath-string
2403 (setq gud-jdb-classpath
2404 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2405 (setq gud-jdb-classpath-string nil) ; prepare for next
2407 (gud-common-init command-line 'gud-jdb-massage-args
2408 'gud-jdb-marker-filter)
2409 (set (make-local-variable 'gud-minor-mode) 'jdb)
2411 ;; If a -classpath option was provided, set gud-jdb-classpath
2412 (if gud-jdb-classpath-string
2413 (setq gud-jdb-classpath
2414 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2415 (setq gud-jdb-classpath-string nil) ; prepare for next
2416 ;; If a -sourcepath option was provided, parse it
2417 (if gud-jdb-sourcepath
2418 (setq gud-jdb-sourcepath
2419 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2421 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2422 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2423 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2424 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2425 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2426 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2427 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2428 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2429 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2430 (gud-def gud-print "print %e" "\C-p" "Print value of expression at point.")
2431 (gud-def gud-pstar "dump %e" nil "Print all object information at point.")
2433 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2434 (setq paragraph-start comint-prompt-regexp)
2435 (run-hooks 'jdb-mode-hook)
2437 (if gud-jdb-use-classpath
2438 ;; Get the classpath information from the debugger
2439 (progn
2440 (if (string-match "-attach" command-line)
2441 (gud-call "classpath"))
2442 (fset 'gud-jdb-find-source
2443 'gud-jdb-find-source-using-classpath))
2445 ;; Else create and bind the class/source association list as well
2446 ;; as the source file list.
2447 (setq gud-jdb-class-source-alist
2448 (gud-jdb-build-class-source-alist
2449 (setq gud-jdb-source-files
2450 (gud-jdb-build-source-files-list gud-jdb-directories
2451 "\\.java$"))))
2452 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2455 ;; End of debugger-specific information
2459 ;; When we send a command to the debugger via gud-call, it's annoying
2460 ;; to see the command and the new prompt inserted into the debugger's
2461 ;; buffer; we have other ways of knowing the command has completed.
2463 ;; If the buffer looks like this:
2464 ;; --------------------
2465 ;; (gdb) set args foo bar
2466 ;; (gdb) -!-
2467 ;; --------------------
2468 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2469 ;; source file to set a breakpoint, we want the buffer to end up like
2470 ;; this:
2471 ;; --------------------
2472 ;; (gdb) set args foo bar
2473 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2474 ;; (gdb) -!-
2475 ;; --------------------
2476 ;; Essentially, the old prompt is deleted, and the command's output
2477 ;; and the new prompt take its place.
2479 ;; Not echoing the command is easy enough; you send it directly using
2480 ;; process-send-string, and it never enters the buffer. However,
2481 ;; getting rid of the old prompt is trickier; you don't want to do it
2482 ;; when you send the command, since that will result in an annoying
2483 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2484 ;; waits for a response from the debugger, and the new prompt is
2485 ;; inserted. Instead, we'll wait until we actually get some output
2486 ;; from the subprocess before we delete the prompt. If the command
2487 ;; produced no output other than a new prompt, that prompt will most
2488 ;; likely be in the first chunk of output received, so we will delete
2489 ;; the prompt and then replace it with an identical one. If the
2490 ;; command produces output, the prompt is moving anyway, so the
2491 ;; flicker won't be annoying.
2493 ;; So - when we want to delete the prompt upon receipt of the next
2494 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2495 ;; the start of the prompt; the process filter will notice this, and
2496 ;; delete all text between it and the process output marker. If
2497 ;; gud-delete-prompt-marker points nowhere, we leave the current
2498 ;; prompt alone.
2499 (defvar gud-delete-prompt-marker nil)
2502 (put 'gud-mode 'mode-class 'special)
2504 (define-derived-mode gud-mode comint-mode "Debugger"
2505 "Major mode for interacting with an inferior debugger process.
2507 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2508 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2509 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2510 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2512 After startup, the following commands are available in both the GUD
2513 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2514 or step operation:
2516 \\[gud-break] sets a breakpoint at the current file and line. In the
2517 GUD buffer, the current file and line are those of the last breakpoint or
2518 step. In a source buffer, they are the buffer's file and current line.
2520 \\[gud-remove] removes breakpoints on the current file and line.
2522 \\[gud-refresh] displays in the source window the last line referred to
2523 in the gud buffer.
2525 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2526 step-one-line (not entering function calls), and step-one-instruction
2527 and then update the source window with the current file and position.
2528 \\[gud-cont] continues execution.
2530 \\[gud-print] tries to find the largest C lvalue or function-call expression
2531 around point, and sends it to the debugger for value display.
2533 The above commands are common to all supported debuggers except xdb which
2534 does not support stepping instructions.
2536 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2537 except that the breakpoint is temporary; that is, it is removed when
2538 execution stops on it.
2540 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2541 frame. \\[gud-down] drops back down through one.
2543 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2544 the current function and stops.
2546 All the keystrokes above are accessible in the GUD buffer
2547 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2549 All pre-defined functions for which the concept make sense repeat
2550 themselves the appropriate number of times if you give a prefix
2551 argument.
2553 You may use the `gud-def' macro in the initialization hook to define other
2554 commands.
2556 Other commands for interacting with the debugger process are inherited from
2557 comint mode, which see."
2558 (setq mode-line-process '(":%s"))
2559 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2560 (set (make-local-variable 'gud-last-frame) nil)
2561 (if (boundp 'tool-bar-map) ; not --without-x
2562 (setq-local tool-bar-map gud-tool-bar-map))
2563 (make-local-variable 'comint-prompt-regexp)
2564 ;; Don't put repeated commands in command history many times.
2565 (set (make-local-variable 'comint-input-ignoredups) t)
2566 (make-local-variable 'paragraph-start)
2567 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker))
2568 (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook nil t))
2570 (defcustom gud-chdir-before-run t
2571 "Non-nil if GUD should `cd' to the debugged executable."
2572 :group 'gud
2573 :type 'boolean)
2575 ;; Perform initializations common to all debuggers.
2576 ;; The first arg is the specified command line,
2577 ;; which starts with the program to debug.
2578 ;; The other three args specify the values to use
2579 ;; for local variables in the debugger buffer.
2580 (defun gud-common-init (command-line massage-args marker-filter
2581 &optional find-file)
2582 (let* ((words (split-string-and-unquote command-line))
2583 (program (car words))
2584 (dir default-directory)
2585 ;; Extract the file name from WORDS
2586 ;; and put t in its place.
2587 ;; Later on we will put the modified file name arg back there.
2588 (file-word (let ((w (cdr words)))
2589 (while (and w (= ?- (aref (car w) 0)))
2590 (setq w (cdr w)))
2591 (and w
2592 (prog1 (car w)
2593 (setcar w t)))))
2594 (file-subst
2595 (and file-word (substitute-in-file-name file-word)))
2596 (args (cdr words))
2597 ;; If a directory was specified, expand the file name.
2598 ;; Otherwise, don't expand it, so GDB can use the PATH.
2599 ;; A file name without directory is literally valid
2600 ;; only if the file exists in ., and in that case,
2601 ;; omitting the expansion here has no visible effect.
2602 (file (and file-word
2603 (if (file-name-directory file-subst)
2604 (expand-file-name file-subst)
2605 file-subst)))
2606 (filepart (and file-word (concat "-" (file-name-nondirectory file))))
2607 (existing-buffer (get-buffer (concat "*gud" filepart "*"))))
2608 (switch-to-buffer (concat "*gud" filepart "*"))
2609 (when (and existing-buffer (get-buffer-process existing-buffer))
2610 (error "This program is already being debugged"))
2611 ;; Set the dir, in case the buffer already existed with a different dir.
2612 (setq default-directory dir)
2613 ;; Set default-directory to the file's directory.
2614 (and file-word
2615 gud-chdir-before-run
2616 ;; Don't set default-directory if no directory was specified.
2617 ;; In that case, either the file is found in the current directory,
2618 ;; in which case this setq is a no-op,
2619 ;; or it is found by searching PATH,
2620 ;; in which case we don't know what directory it was found in.
2621 (file-name-directory file)
2622 (setq default-directory (file-name-directory file)))
2623 (or (bolp) (newline))
2624 (insert "Current directory is " default-directory "\n")
2625 ;; Put the substituted and expanded file name back in its place.
2626 (let ((w args))
2627 (while (and w (not (eq (car w) t)))
2628 (setq w (cdr w)))
2629 ;; Tramp has already been loaded if we are here.
2630 (if w (setcar w (setq file (file-local-name file)))))
2631 (apply 'make-comint (concat "gud" filepart) program nil
2632 (if massage-args (funcall massage-args file args) args))
2633 ;; Since comint clobbered the mode, we don't set it until now.
2634 (gud-mode)
2635 (set (make-local-variable 'gud-target-name)
2636 (and file-word (file-name-nondirectory file))))
2637 (set (make-local-variable 'gud-marker-filter) marker-filter)
2638 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2639 (setq gud-last-last-frame nil)
2641 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2642 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2643 (gud-set-buffer))
2645 (defun gud-set-buffer ()
2646 (when (derived-mode-p 'gud-mode)
2647 (setq gud-comint-buffer (current-buffer))))
2649 (defvar gud-filter-defer-flag nil
2650 "Non-nil means don't process anything from the debugger right now.
2651 It is saved for when this flag is not set.")
2653 ;; These functions are responsible for inserting output from your debugger
2654 ;; into the buffer. The hard work is done by the method that is
2655 ;; the value of gud-marker-filter.
2657 (defun gud-filter (proc string)
2658 ;; Here's where the actual buffer insertion is done
2659 (let (output process-window)
2660 (if (buffer-name (process-buffer proc))
2661 (if gud-filter-defer-flag
2662 ;; If we can't process any text now,
2663 ;; save it for later.
2664 (setq gud-filter-pending-text
2665 (concat (or gud-filter-pending-text "") string))
2667 ;; If we have to ask a question during the processing,
2668 ;; defer any additional text that comes from the debugger
2669 ;; during that time.
2670 (let ((gud-filter-defer-flag t))
2671 ;; Process now any text we previously saved up.
2672 (if gud-filter-pending-text
2673 (setq string (concat gud-filter-pending-text string)
2674 gud-filter-pending-text nil))
2676 (with-current-buffer (process-buffer proc)
2677 ;; If we have been so requested, delete the debugger prompt.
2678 (save-restriction
2679 (widen)
2680 (if (marker-buffer gud-delete-prompt-marker)
2681 (let ((inhibit-read-only t))
2682 (delete-region (process-mark proc)
2683 gud-delete-prompt-marker)
2684 (comint-update-fence)
2685 (set-marker gud-delete-prompt-marker nil)))
2686 ;; Save the process output, checking for source file markers.
2687 (setq output (gud-marker-filter string))
2688 ;; Check for a filename-and-line number.
2689 ;; Don't display the specified file
2690 ;; unless (1) point is at or after the position where output appears
2691 ;; and (2) this buffer is on the screen.
2692 (setq process-window
2693 (and gud-last-frame
2694 (>= (point) (process-mark proc))
2695 (get-buffer-window (current-buffer)))))
2697 ;; Let the comint filter do the actual insertion.
2698 ;; That lets us inherit various comint features.
2699 (comint-output-filter proc output))
2701 ;; Put the arrow on the source line.
2702 ;; This must be outside of the save-excursion
2703 ;; in case the source file is our current buffer.
2704 (if process-window
2705 (with-selected-window process-window
2706 (gud-display-frame))
2707 ;; We have to be in the proper buffer, (process-buffer proc),
2708 ;; but not in a save-excursion, because that would restore point.
2709 (with-current-buffer (process-buffer proc)
2710 (gud-display-frame))))
2712 ;; If we deferred text that arrived during this processing,
2713 ;; handle it now.
2714 (if gud-filter-pending-text
2715 (gud-filter proc ""))))))
2717 (defvar gud-minor-mode-type nil)
2718 (defvar gud-overlay-arrow-position nil)
2719 (add-to-list 'overlay-arrow-variable-list 'gud-overlay-arrow-position)
2721 (declare-function gdb-reset "gdb-mi" ())
2722 (declare-function speedbar-change-initial-expansion-list "speedbar" (new))
2723 (defvar speedbar-previously-used-expansion-list-name)
2725 (defun gud-sentinel (proc msg)
2726 (cond ((null (buffer-name (process-buffer proc)))
2727 ;; buffer killed
2728 ;; Stop displaying an arrow in a source file.
2729 (setq gud-overlay-arrow-position nil)
2730 (set-process-buffer proc nil)
2731 (if (and (boundp 'speedbar-initial-expansion-list-name)
2732 (string-equal speedbar-initial-expansion-list-name "GUD"))
2733 (speedbar-change-initial-expansion-list
2734 speedbar-previously-used-expansion-list-name))
2735 (if (eq gud-minor-mode-type 'gdbmi)
2736 (gdb-reset)
2737 (gud-reset)))
2738 ((memq (process-status proc) '(signal exit))
2739 ;; Stop displaying an arrow in a source file.
2740 (setq gud-overlay-arrow-position nil)
2741 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2742 'gdbmi)
2743 (gdb-reset)
2744 (gud-reset))
2745 (let* ((obuf (current-buffer)))
2746 ;; save-excursion isn't the right thing if
2747 ;; process-buffer is current-buffer
2748 (unwind-protect
2749 (progn
2750 ;; Write something in the GUD buffer and hack its mode line,
2751 (set-buffer (process-buffer proc))
2752 ;; Fix the mode line.
2753 (setq mode-line-process
2754 (concat ":"
2755 (symbol-name (process-status proc))))
2756 (force-mode-line-update)
2757 (if (eobp)
2758 (insert ?\n mode-name " " msg)
2759 (save-excursion
2760 (goto-char (point-max))
2761 (insert ?\n mode-name " " msg)))
2762 ;; If buffer and mode line will show that the process
2763 ;; is dead, we can delete it now. Otherwise it
2764 ;; will stay around until M-x list-processes.
2765 (delete-process proc))
2766 ;; Restore old buffer, but don't restore old point
2767 ;; if obuf is the gud buffer.
2768 (set-buffer obuf))))))
2770 (defun gud-kill-buffer-hook ()
2771 (setq gud-minor-mode-type gud-minor-mode)
2772 (condition-case nil
2773 (progn
2774 (kill-process (get-buffer-process (current-buffer)))
2775 (delete-process (get-process "gdb-inferior")))
2776 (error nil)))
2778 (defun gud-reset ()
2779 (dolist (buffer (buffer-list))
2780 (unless (eq buffer gud-comint-buffer)
2781 (with-current-buffer buffer
2782 (when gud-minor-mode
2783 (setq gud-minor-mode nil)
2784 (kill-local-variable 'tool-bar-map))))))
2786 (defun gud-display-frame ()
2787 "Find and obey the last filename-and-line marker from the debugger.
2788 Obeying it means displaying in another window the specified file and line."
2789 (interactive)
2790 (when gud-last-frame
2791 (gud-set-buffer)
2792 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2793 (setq gud-last-last-frame gud-last-frame
2794 gud-last-frame nil)))
2796 (declare-function global-hl-line-highlight "hl-line" ())
2797 (declare-function hl-line-highlight "hl-line" ())
2798 (declare-function gdb-display-source-buffer "gdb-mi" (buffer))
2800 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2801 ;; and that its line LINE is visible.
2802 ;; Put the overlay-arrow on the line LINE in that buffer.
2803 ;; Most of the trickiness in here comes from wanting to preserve the current
2804 ;; region-restriction if that's possible. We use an explicit display-buffer
2805 ;; to get around the fact that this is called inside a save-excursion.
2807 (defun gud-display-line (true-file line)
2808 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2809 (buffer
2810 (with-current-buffer gud-comint-buffer
2811 (gud-find-file true-file)))
2812 (window (and buffer
2813 (or (get-buffer-window buffer)
2814 (display-buffer buffer '(nil (inhibit-same-window . t))))))
2815 (pos))
2816 (when buffer
2817 (with-current-buffer buffer
2818 (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
2819 (if (yes-or-no-p
2820 (format "File %s changed on disk. Reread from disk? "
2821 (buffer-name)))
2822 (revert-buffer t t)
2823 (setq gud-keep-buffer t)))
2824 (save-restriction
2825 (widen)
2826 (goto-char (point-min))
2827 (forward-line (1- line))
2828 (setq pos (point))
2829 (or gud-overlay-arrow-position
2830 (setq gud-overlay-arrow-position (make-marker)))
2831 (set-marker gud-overlay-arrow-position (point) (current-buffer))
2832 ;; If they turned on hl-line, move the hl-line highlight to
2833 ;; the arrow's line.
2834 (when (featurep 'hl-line)
2835 (cond
2836 (global-hl-line-mode
2837 (global-hl-line-highlight))
2838 ((and hl-line-mode hl-line-sticky-flag)
2839 (hl-line-highlight)))))
2840 (cond ((or (< pos (point-min)) (> pos (point-max)))
2841 (widen)
2842 (goto-char pos))))
2843 (when window
2844 (set-window-point window gud-overlay-arrow-position)
2845 (if (eq gud-minor-mode 'gdbmi)
2846 (setq gdb-source-window window))))))
2848 ;; The gud-call function must do the right thing whether its invoking
2849 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2850 ;; or a C buffer. In the former case, we want to supply data from
2851 ;; gud-last-frame. Here's how we do it:
2853 (defun gud-format-command (str arg)
2854 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2855 (frame (or gud-last-frame gud-last-last-frame))
2856 (buffer-file-name-localized
2857 (and (buffer-file-name)
2858 (file-local-name (buffer-file-name))))
2859 result)
2860 (while (and str
2861 (let ((case-fold-search nil))
2862 (string-match "\\([^%]*\\)%\\([adefFlpc]\\)" str)))
2863 (let ((key (string-to-char (match-string 2 str)))
2864 subst)
2865 (cond
2866 ((eq key ?f)
2867 (setq subst (file-name-nondirectory (if insource
2868 buffer-file-name-localized
2869 (car frame)))))
2870 ((eq key ?F)
2871 (setq subst (file-name-base (if insource
2872 buffer-file-name-localized
2873 (car frame)))))
2874 ((eq key ?d)
2875 (setq subst (file-name-directory (if insource
2876 buffer-file-name-localized
2877 (car frame)))))
2878 ((eq key ?l)
2879 (setq subst (int-to-string
2880 (if insource
2881 (save-restriction
2882 (widen)
2883 (+ (count-lines (point-min) (point))
2884 (if (bolp) 1 0)))
2885 (cdr frame)))))
2886 ((eq key ?e)
2887 (setq subst (gud-find-expr)))
2888 ((eq key ?a)
2889 (setq subst (gud-read-address)))
2890 ((eq key ?c)
2891 (setq subst
2892 (gud-find-class
2893 (if insource
2894 (buffer-file-name)
2895 (car frame))
2896 (if insource
2897 (save-restriction
2898 (widen)
2899 (+ (count-lines (point-min) (point))
2900 (if (bolp) 1 0)))
2901 (cdr frame)))))
2902 ((eq key ?p)
2903 (setq subst (if arg (int-to-string arg)))))
2904 (setq result (concat result (match-string 1 str) subst)))
2905 (setq str (substring str (match-end 2))))
2906 ;; There might be text left in STR when the loop ends.
2907 (concat result str)))
2909 (defun gud-read-address ()
2910 "Return a string containing the core-address found in the buffer at point."
2911 (save-match-data
2912 (save-excursion
2913 (let ((pt (point)) found begin)
2914 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2915 (cond
2916 (found (forward-char 2)
2917 (buffer-substring found
2918 (progn (re-search-forward "[^0-9a-f]")
2919 (forward-char -1)
2920 (point))))
2921 (t (setq begin (progn (re-search-backward "[^0-9]")
2922 (forward-char 1)
2923 (point)))
2924 (forward-char 1)
2925 (re-search-forward "[^0-9]")
2926 (forward-char -1)
2927 (buffer-substring begin (point))))))))
2929 (defun gud-call (fmt &optional arg)
2930 (let ((msg (gud-format-command fmt arg)))
2931 (message "Command: %s" msg)
2932 (sit-for 0)
2933 (gud-basic-call msg)))
2935 (defun gud-basic-call (command)
2936 "Invoke the debugger COMMAND displaying source in other window."
2937 (interactive)
2938 (gud-set-buffer)
2939 (let ((proc (get-buffer-process gud-comint-buffer)))
2940 (or proc (error "Current buffer has no process"))
2941 ;; Arrange for the current prompt to get deleted.
2942 (with-current-buffer gud-comint-buffer
2943 (save-excursion
2944 (save-restriction
2945 (widen)
2946 (if (marker-position gud-delete-prompt-marker)
2947 ;; We get here when printing an expression.
2948 (goto-char gud-delete-prompt-marker)
2949 (goto-char (process-mark proc))
2950 (forward-line 0))
2951 (if (looking-at comint-prompt-regexp)
2952 (set-marker gud-delete-prompt-marker (point)))
2953 (if (eq gud-minor-mode 'gdbmi)
2954 (apply comint-input-sender (list proc command))
2955 (process-send-string proc (concat command "\n"))))))))
2957 (defun gud-refresh (&optional arg)
2958 "Fix up a possibly garbled display, and redraw the arrow."
2959 (interactive "P")
2960 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2961 (gud-display-frame)
2962 (recenter arg))
2964 ;; Code for parsing expressions out of C or Fortran code. The single entry
2965 ;; point is gud-find-expr, which tries to return an lvalue expression from
2966 ;; around point.
2968 (defvar gud-find-expr-function 'gud-find-c-expr)
2970 (defun gud-find-expr (&rest args)
2971 (let ((expr (if (and transient-mark-mode mark-active)
2972 (buffer-substring (region-beginning) (region-end))
2973 (apply gud-find-expr-function args))))
2974 (save-match-data
2975 (if (string-match "\n" expr)
2976 (error "Expression must not include a newline"))
2977 (with-current-buffer gud-comint-buffer
2978 (save-excursion
2979 (goto-char (process-mark (get-buffer-process gud-comint-buffer)))
2980 (forward-line 0)
2981 (when (looking-at comint-prompt-regexp)
2982 (set-marker gud-delete-prompt-marker (point))
2983 (set-marker-insertion-type gud-delete-prompt-marker t))
2984 (unless (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2985 'jdb)
2986 (insert (concat expr " = "))))))
2987 expr))
2989 ;; The next eight functions are hacked from gdbsrc.el by
2990 ;; Debby Ayers <ayers@asc.slb.com>,
2991 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2993 (defun gud-find-c-expr ()
2994 "Returns the expr that surrounds point."
2995 (interactive)
2996 (save-excursion
2997 (let ((p (point))
2998 (expr (gud-innermost-expr))
2999 (test-expr (gud-prev-expr)))
3000 (while (and test-expr (gud-expr-compound test-expr expr))
3001 (let ((prev-expr expr))
3002 (setq expr (cons (car test-expr) (cdr expr)))
3003 (goto-char (car expr))
3004 (setq test-expr (gud-prev-expr))
3005 ;; If we just pasted on the condition of an if or while,
3006 ;; throw it away again.
3007 (if (member (buffer-substring (car test-expr) (cdr test-expr))
3008 '("if" "while" "for"))
3009 (setq test-expr nil
3010 expr prev-expr))))
3011 (goto-char p)
3012 (setq test-expr (gud-next-expr))
3013 (while (gud-expr-compound expr test-expr)
3014 (setq expr (cons (car expr) (cdr test-expr)))
3015 (setq test-expr (gud-next-expr)))
3016 (buffer-substring (car expr) (cdr expr)))))
3018 (defun gud-innermost-expr ()
3019 "Returns the smallest expr that point is in; move point to beginning of it.
3020 The expr is represented as a cons cell, where the car specifies the point in
3021 the current buffer that marks the beginning of the expr and the cdr specifies
3022 the character after the end of the expr."
3023 (let ((p (point)) begin end)
3024 (gud-backward-sexp)
3025 (setq begin (point))
3026 (gud-forward-sexp)
3027 (setq end (point))
3028 (if (>= p end)
3029 (progn
3030 (setq begin p)
3031 (goto-char p)
3032 (gud-forward-sexp)
3033 (setq end (point)))
3035 (goto-char begin)
3036 (cons begin end)))
3038 (defun gud-backward-sexp ()
3039 "Version of `backward-sexp' that catches errors."
3040 (condition-case nil
3041 (backward-sexp)
3042 (error t)))
3044 (defun gud-forward-sexp ()
3045 "Version of `forward-sexp' that catches errors."
3046 (condition-case nil
3047 (forward-sexp)
3048 (error t)))
3050 (defun gud-prev-expr ()
3051 "Returns the previous expr, point is set to beginning of that expr.
3052 The expr is represented as a cons cell, where the car specifies the point in
3053 the current buffer that marks the beginning of the expr and the cdr specifies
3054 the character after the end of the expr"
3055 (let ((begin) (end))
3056 (gud-backward-sexp)
3057 (setq begin (point))
3058 (gud-forward-sexp)
3059 (setq end (point))
3060 (goto-char begin)
3061 (cons begin end)))
3063 (defun gud-next-expr ()
3064 "Returns the following expr, point is set to beginning of that expr.
3065 The expr is represented as a cons cell, where the car specifies the point in
3066 the current buffer that marks the beginning of the expr and the cdr specifies
3067 the character after the end of the expr."
3068 (let ((begin) (end))
3069 (gud-forward-sexp)
3070 (gud-forward-sexp)
3071 (setq end (point))
3072 (gud-backward-sexp)
3073 (setq begin (point))
3074 (cons begin end)))
3076 (defun gud-expr-compound-sep (span-start span-end)
3077 "Scan from SPAN-START to SPAN-END for punctuation characters.
3078 If `->' is found, return `?.'. If `.' is found, return `?.'.
3079 If any other punctuation is found, return `??'.
3080 If no punctuation is found, return `?\\s'."
3081 (let ((result ?\s)
3082 (syntax))
3083 (while (< span-start span-end)
3084 (setq syntax (char-syntax (char-after span-start)))
3085 (cond
3086 ((= syntax ?\s) t)
3087 ((= syntax ?.) (setq syntax (char-after span-start))
3088 (cond
3089 ((= syntax ?.) (setq result ?.))
3090 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
3091 (setq result ?.)
3092 (setq span-start (+ span-start 1)))
3093 (t (setq span-start span-end)
3094 (setq result ??)))))
3095 (setq span-start (+ span-start 1)))
3096 result))
3098 (defun gud-expr-compound (first second)
3099 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
3100 The two exprs are represented as a cons cells, where the car
3101 specifies the point in the current buffer that marks the beginning of the
3102 expr and the cdr specifies the character after the end of the expr.
3103 Link exprs of the form:
3104 Expr -> Expr
3105 Expr . Expr
3106 Expr (Expr)
3107 Expr [Expr]
3108 (Expr) Expr
3109 [Expr] Expr"
3110 (let ((span-start (cdr first))
3111 (span-end (car second))
3112 (syntax))
3113 (setq syntax (gud-expr-compound-sep span-start span-end))
3114 (cond
3115 ((= (car first) (car second)) nil)
3116 ((= (cdr first) (cdr second)) nil)
3117 ((= syntax ?.) t)
3118 ((= syntax ?\s)
3119 (setq span-start (char-after (- span-start 1)))
3120 (setq span-end (char-after span-end))
3121 (cond
3122 ((= span-start ?\)) t)
3123 ((= span-start ?\]) t)
3124 ((= span-end ?\() t)
3125 ((= span-end ?\[) t)
3126 (t nil)))
3127 (t nil))))
3130 (declare-function c-langelem-sym "cc-defs" (langelem))
3131 (declare-function c-langelem-pos "cc-defs" (langelem))
3133 (defun gud-find-class (f _line)
3134 "Find fully qualified class in file F at line LINE.
3135 This function uses the `gud-jdb-classpath' (and optional
3136 `gud-jdb-sourcepath') list(s) to derive a file
3137 pathname relative to its classpath directory. The values in
3138 `gud-jdb-classpath' are assumed to have been converted to absolute
3139 pathname standards using file-truename.
3140 If F is visited by a buffer and its mode is CC-mode(Java),
3141 syntactic information of LINE is used to find the enclosing (nested)
3142 class string which is appended to the top level
3143 class of the file (using s to separate nested class ids)."
3144 ;; Convert f to a standard representation and remove suffix
3145 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
3146 (save-match-data
3147 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
3148 (fbuffer (get-file-buffer f))
3149 class-found
3150 ;; Syntax-symbol returns the symbol of the *first* element
3151 ;; in the syntactical analysis result list, syntax-point
3152 ;; returns the buffer position of same
3153 (syntax-symbol (lambda (x) (c-langelem-sym (car x))))
3154 (syntax-point (lambda (x) (c-langelem-pos (car x)))))
3155 (setq f (file-name-sans-extension (file-truename f)))
3156 ;; Search through classpath list for an entry that is
3157 ;; contained in f
3158 (while (and cplist (not class-found))
3159 (if (string-match (car cplist) f)
3160 (setq class-found
3161 (mapconcat 'identity
3162 (split-string
3163 (substring f (+ (match-end 0) 1))
3164 "/") ".")))
3165 (setq cplist (cdr cplist)))
3166 ;; if f is visited by a java(cc-mode) buffer, walk up the
3167 ;; syntactic information chain and collect any 'inclass
3168 ;; symbols until 'topmost-intro is reached to find out if
3169 ;; point is within a nested class
3170 ;; FIXME: Yuck!!! cc-mode should provide a function instead.
3171 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
3172 (with-current-buffer fbuffer
3173 (let ((nclass) (syntax))
3174 ;; While the c-syntactic information does not start
3175 ;; with the 'topmost-intro symbol, there may be
3176 ;; nested classes...
3177 (while (not (eq 'topmost-intro
3178 (funcall syntax-symbol (c-guess-basic-syntax))))
3179 ;; Check if the current position c-syntactic
3180 ;; analysis has 'inclass
3181 (setq syntax (c-guess-basic-syntax))
3182 (while
3183 (and (not (eq 'inclass (funcall syntax-symbol syntax)))
3184 (cdr syntax))
3185 (setq syntax (cdr syntax)))
3186 (if (eq 'inclass (funcall syntax-symbol syntax))
3187 (progn
3188 (goto-char (funcall syntax-point syntax))
3189 ;; Now we're at the beginning of a class
3190 ;; definition. Find class name
3191 (looking-at
3192 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
3193 (setq nclass
3194 (append (list (match-string-no-properties 1))
3195 nclass)))
3196 (setq syntax (c-guess-basic-syntax))
3197 (while (and (not (funcall syntax-point syntax)) (cdr syntax))
3198 (setq syntax (cdr syntax)))
3199 (goto-char (funcall syntax-point syntax))
3201 (string-match (concat (car nclass) "$") class-found)
3202 (setq class-found
3203 (replace-match (mapconcat 'identity nclass "$")
3204 t t class-found)))))
3205 (if (not class-found)
3206 (message "gud-find-class: class for file %s not found!" f))
3207 class-found))
3208 ;; Not using classpath - try class/source association list
3209 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
3210 (if class-found
3211 (car class-found)
3212 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
3213 nil))))
3216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3217 ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3220 (defvar gdb-script-mode-syntax-table
3221 (let ((st (make-syntax-table)))
3222 (modify-syntax-entry ?' "\"" st)
3223 (modify-syntax-entry ?# "<" st)
3224 (modify-syntax-entry ?\n ">" st)
3225 st))
3227 (defvar gdb-script-font-lock-keywords
3228 '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
3229 ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
3230 ("^\\s-*\\(\\w\\(\\w\\|\\s_\\)*\\)" (1 font-lock-keyword-face))))
3232 (defconst gdb-script-syntax-propertize-function
3233 (syntax-propertize-rules
3234 ("^document\\s-.*\\(\n\\)" (1 "< b"))
3235 ("^end\\(\\>\\)"
3236 (1 (ignore
3237 (when (and (> (match-beginning 0) (point-min))
3238 (eq 1 (nth 7 (save-excursion
3239 (syntax-ppss (1- (match-beginning 0)))))))
3240 ;; We change the \n in front, which is more difficult, but results
3241 ;; in better highlighting. If the doc is empty, the single \n is
3242 ;; both the beginning and the end of the docstring, which can't be
3243 ;; expressed in syntax-tables. Instead, we place the "> b" after
3244 ;; placing the "< b", so the start marker is overwritten by the
3245 ;; termination marker and in the end Emacs simply considers that
3246 ;; there's no docstring at all, which is fine.
3247 (put-text-property (1- (match-beginning 0)) (match-beginning 0)
3248 'syntax-table (eval-when-compile
3249 (string-to-syntax "> b")))
3250 ;; Make sure that rehighlighting the previous line won't erase our
3251 ;; syntax-table property and that modifying `end' will.
3252 (put-text-property (1- (match-beginning 0)) (match-end 0)
3253 'syntax-multiline t)))))))
3255 (defun gdb-script-font-lock-syntactic-face (state)
3256 (cond
3257 ((nth 3 state) font-lock-string-face)
3258 ((nth 7 state) font-lock-doc-face)
3259 (t font-lock-comment-face)))
3261 (defvar gdb-script-basic-indent 2)
3263 (defun gdb-script-skip-to-head ()
3264 "We're just in front of an `end' and we need to go to its head."
3265 (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\|commands\\)\\>" nil 'move)
3266 (match-end 2))
3267 (gdb-script-skip-to-head)))
3269 (defun gdb-script-calculate-indentation ()
3270 (cond
3271 ((looking-at "end\\>")
3272 (gdb-script-skip-to-head)
3273 (current-indentation))
3274 ((looking-at "else\\>")
3275 (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
3276 (match-end 2))
3277 (gdb-script-skip-to-head))
3278 (current-indentation))
3280 (forward-comment (- (point-max)))
3281 (forward-line 0)
3282 (skip-chars-forward " \t")
3283 (+ (current-indentation)
3284 (if (looking-at "\\(if\\|while\\|define\\|else\\|commands\\)\\>")
3285 gdb-script-basic-indent 0)))))
3287 (defun gdb-script-indent-line ()
3288 "Indent current line of GDB script."
3289 (interactive)
3290 (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
3291 (save-excursion
3292 (forward-line 0)
3293 (skip-chars-forward " \t")
3294 (not (looking-at "end\\>"))))
3295 'noindent
3296 (let* ((savep (point))
3297 (indent (condition-case nil
3298 (save-excursion
3299 (forward-line 0)
3300 (skip-chars-forward " \t")
3301 (if (>= (point) savep) (setq savep nil))
3302 (max (gdb-script-calculate-indentation) 0))
3303 (error 0))))
3304 (if savep
3305 (save-excursion (indent-line-to indent))
3306 (indent-line-to indent)))))
3308 ;; Derived from cfengine.el.
3309 (defun gdb-script-beginning-of-defun ()
3310 "`beginning-of-defun' function for Gdb script mode.
3311 Treats actions as defuns."
3312 (unless (<= (current-column) (current-indentation))
3313 (end-of-line))
3314 (if (re-search-backward "^define \\|^document " nil t)
3315 (beginning-of-line)
3316 (goto-char (point-min)))
3319 ;; Derived from cfengine.el.
3320 (defun gdb-script-end-of-defun ()
3321 "`end-of-defun' function for Gdb script mode.
3322 Treats actions as defuns."
3323 (end-of-line)
3324 (if (re-search-forward "^end" nil t)
3325 (beginning-of-line)
3326 (goto-char (point-max)))
3329 ;;;###autoload
3330 (define-derived-mode gdb-script-mode prog-mode "GDB-Script"
3331 "Major mode for editing GDB scripts."
3332 (set (make-local-variable 'comment-start) "#")
3333 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
3334 (set (make-local-variable 'outline-regexp) "[ \t]")
3335 (set (make-local-variable 'imenu-generic-expression)
3336 '((nil "^define[ \t]+\\(\\w+\\)" 1)))
3337 (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
3338 (set (make-local-variable 'beginning-of-defun-function)
3339 #'gdb-script-beginning-of-defun)
3340 (set (make-local-variable 'end-of-defun-function)
3341 #'gdb-script-end-of-defun)
3342 (set (make-local-variable 'font-lock-defaults)
3343 '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
3344 (font-lock-syntactic-face-function
3345 . gdb-script-font-lock-syntactic-face)))
3346 ;; Recognize docstrings.
3347 (set (make-local-variable 'syntax-propertize-function)
3348 gdb-script-syntax-propertize-function)
3349 (add-hook 'syntax-propertize-extend-region-functions
3350 #'syntax-propertize-multiline 'append 'local))
3353 ;;; tooltips for GUD
3355 ;;; Customizable settings
3357 (defvar tooltip-mode)
3359 ;;;###autoload
3360 (define-minor-mode gud-tooltip-mode
3361 "Toggle the display of GUD tooltips.
3362 With a prefix argument ARG, enable the feature if ARG is
3363 positive, and disable it otherwise. If called from Lisp, enable
3364 it if ARG is omitted or nil."
3365 :global t
3366 :group 'gud
3367 :group 'tooltip
3368 (require 'tooltip)
3369 (if gud-tooltip-mode
3370 (progn
3371 (add-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3372 (add-hook 'pre-command-hook 'tooltip-hide)
3373 (add-hook 'tooltip-functions 'gud-tooltip-tips)
3374 (define-key global-map [mouse-movement] 'gud-tooltip-mouse-motion))
3375 (unless tooltip-mode (remove-hook 'pre-command-hook 'tooltip-hide)
3376 (remove-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3377 (remove-hook 'tooltip-functions 'gud-tooltip-tips)
3378 (define-key global-map [mouse-movement] 'ignore)))
3379 (gud-tooltip-activate-mouse-motions-if-enabled)
3380 (if (and gud-comint-buffer
3381 (buffer-name gud-comint-buffer); gud-comint-buffer might be killed
3382 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3383 'gdbmi))
3384 (if gud-tooltip-mode
3385 (progn
3386 (dolist (buffer (buffer-list))
3387 (unless (eq buffer gud-comint-buffer)
3388 (with-current-buffer buffer
3389 (when (and (eq gud-minor-mode 'gdbmi)
3390 (not (string-match "\\`\\*.+\\*\\'"
3391 (buffer-name))))
3392 (make-local-variable 'gdb-define-alist)
3393 (gdb-create-define-alist)
3394 (add-hook 'after-save-hook
3395 'gdb-create-define-alist nil t))))))
3396 (kill-local-variable 'gdb-define-alist)
3397 (remove-hook 'after-save-hook 'gdb-create-define-alist t))))
3399 (define-obsolete-variable-alias 'tooltip-gud-modes
3400 'gud-tooltip-modes "22.1")
3402 (defcustom gud-tooltip-modes '(gud-mode c-mode c++-mode fortran-mode
3403 python-mode)
3404 "List of modes for which to enable GUD tooltips."
3405 :type '(repeat (symbol :tag "Major mode"))
3406 :group 'gud
3407 :group 'tooltip)
3409 (define-obsolete-variable-alias 'tooltip-gud-display
3410 'gud-tooltip-display "22.1")
3412 (defcustom gud-tooltip-display
3413 '((eq (tooltip-event-buffer gud-tooltip-event)
3414 (marker-buffer gud-overlay-arrow-position)))
3415 "List of forms determining where GUD tooltips are displayed.
3417 Forms in the list are combined with AND. The default is to display
3418 only tooltips in the buffer containing the overlay arrow."
3419 :type 'sexp
3420 :risky t
3421 :group 'gud
3422 :group 'tooltip)
3424 (defcustom gud-tooltip-echo-area nil
3425 "Use the echo area instead of frames for GUD tooltips."
3426 :type 'boolean
3427 :group 'gud
3428 :group 'tooltip)
3430 (make-obsolete-variable 'gud-tooltip-echo-area
3431 "disable Tooltip mode instead" "24.4" 'set)
3433 ;;; Reacting on mouse movements
3435 (defun gud-tooltip-change-major-mode ()
3436 "Function added to `change-major-mode-hook' when tooltip mode is on."
3437 (add-hook 'post-command-hook 'gud-tooltip-activate-mouse-motions-if-enabled))
3439 (defun gud-tooltip-activate-mouse-motions-if-enabled ()
3440 "Reconsider for all buffers whether mouse motion events are desired."
3441 (remove-hook 'post-command-hook
3442 'gud-tooltip-activate-mouse-motions-if-enabled)
3443 (dolist (buffer (buffer-list))
3444 (with-current-buffer buffer
3445 (if (and gud-tooltip-mode
3446 (memq major-mode gud-tooltip-modes))
3447 (gud-tooltip-activate-mouse-motions t)
3448 (gud-tooltip-activate-mouse-motions nil)))))
3450 (defvar gud-tooltip-mouse-motions-active nil
3451 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
3453 ;; We don't set track-mouse globally because this is a big redisplay
3454 ;; problem in buffers having a pre-command-hook or such installed,
3455 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
3456 ;; set-buffer prevents redisplay optimizations, so every mouse motion
3457 ;; would be accompanied by a full redisplay.
3459 (defun gud-tooltip-activate-mouse-motions (activatep)
3460 "Activate/deactivate mouse motion events for the current buffer.
3461 ACTIVATEP non-nil means activate mouse motion events."
3462 (if activatep
3463 (progn
3464 (set (make-local-variable 'gud-tooltip-mouse-motions-active) t)
3465 (set (make-local-variable 'track-mouse) t))
3466 (when gud-tooltip-mouse-motions-active
3467 (kill-local-variable 'gud-tooltip-mouse-motions-active)
3468 (kill-local-variable 'track-mouse))))
3470 (defvar tooltip-last-mouse-motion-event)
3471 (declare-function tooltip-hide "tooltip" (&optional ignored-arg))
3472 (declare-function tooltip-start-delayed-tip "tooltip" ())
3474 (defun gud-tooltip-mouse-motion (event)
3475 "Command handler for mouse movement events in `global-map'."
3476 (interactive "e")
3477 (tooltip-hide)
3478 (when (car (mouse-pixel-position))
3479 (setq tooltip-last-mouse-motion-event (copy-sequence event))
3480 (tooltip-start-delayed-tip)))
3482 ;;; Tips for `gud'
3484 (defvar gud-tooltip-dereference nil
3485 "Non-nil means print expressions with a `*' in front of them.
3486 For C this would dereference a pointer expression.")
3488 (defvar gud-tooltip-event nil
3489 "The mouse movement event that led to a tooltip display.
3490 This event can be examined by forms in `gud-tooltip-display'.")
3492 (defun gud-tooltip-dereference (&optional arg)
3493 "Toggle whether tooltips should show `* expr' or `expr'.
3494 With arg, dereference expr if ARG is positive, otherwise do not dereference."
3495 (interactive "P")
3496 (setq gud-tooltip-dereference
3497 (if (null arg)
3498 (not gud-tooltip-dereference)
3499 (> (prefix-numeric-value arg) 0)))
3500 (message "Dereferencing is now %s."
3501 (if gud-tooltip-dereference "on" "off")))
3503 (define-obsolete-function-alias 'tooltip-gud-toggle-dereference
3504 'gud-tooltip-dereference "22.1")
3505 (defvar tooltip-use-echo-area)
3506 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
3507 (declare-function tooltip-strip-prompt "tooltip" (process output))
3509 ; This will only display data that comes in one chunk.
3510 ; Larger arrays (say 400 elements) are displayed in
3511 ; the tooltip incompletely and spill over into the gud buffer.
3512 ; Switching the process-filter creates timing problems and
3513 ; it may be difficult to do better. Using GDB/MI as in
3514 ; gdb-mi.el gets around this problem.
3515 (defun gud-tooltip-process-output (process output)
3516 "Process debugger output and show it in a tooltip window."
3517 (remove-function (process-filter process) #'gud-tooltip-process-output)
3518 (tooltip-show (tooltip-strip-prompt process output)
3519 (or gud-tooltip-echo-area tooltip-use-echo-area
3520 (not tooltip-mode))))
3522 (defun gud-tooltip-print-command (expr)
3523 "Return a suitable command to print the expression EXPR."
3524 (pcase gud-minor-mode
3525 (`gdbmi (concat "-data-evaluate-expression \"" expr "\""))
3526 (`guiler expr)
3527 (`dbx (concat "print " expr))
3528 ((or `xdb `pdb) (concat "p " expr))
3529 (`sdb (concat expr "/"))))
3531 (declare-function gdb-input "gdb-mi" (command handler &optional trigger))
3532 (declare-function tooltip-expr-to-print "tooltip" (event))
3533 (declare-function tooltip-event-buffer "tooltip" (event))
3535 (defun gud-tooltip-tips (event)
3536 "Show tip for identifier or selection under the mouse.
3537 The mouse must either point at an identifier or inside a selected
3538 region for the tip window to be shown. If `gud-tooltip-dereference' is t,
3539 add a `*' in front of the printed expression. In the case of a C program
3540 controlled by GDB, show the associated #define directives when program is
3541 not executing.
3543 This function must return nil if it doesn't handle EVENT."
3544 (let (process)
3545 (when (and (eventp event)
3546 gud-tooltip-mode
3547 gud-comint-buffer
3548 (buffer-name gud-comint-buffer); might be killed
3549 (setq process (get-buffer-process gud-comint-buffer))
3550 (posn-point (event-end event))
3551 (or (and (eq gud-minor-mode 'gdbmi) (not gdb-active-process))
3552 (progn (setq gud-tooltip-event event)
3553 (eval (cons 'and gud-tooltip-display)))))
3554 (let ((expr (tooltip-expr-to-print event)))
3555 (when expr
3556 (if (and (eq gud-minor-mode 'gdbmi)
3557 (not gdb-active-process))
3558 (progn
3559 (with-current-buffer (tooltip-event-buffer event)
3560 (let ((define-elt (assoc expr gdb-define-alist)))
3561 (unless (null define-elt)
3562 (tooltip-show
3563 (cdr define-elt)
3564 (or gud-tooltip-echo-area tooltip-use-echo-area
3565 (not tooltip-mode)))
3566 expr))))
3567 (when gud-tooltip-dereference
3568 (setq expr (concat "*" expr)))
3569 (let ((cmd (gud-tooltip-print-command expr)))
3570 (when (and gud-tooltip-mode (eq gud-minor-mode 'gdb))
3571 (gud-tooltip-mode -1)
3572 ;; The blank before the newline is for MS-Windows,
3573 ;; whose emulation of message box removes newlines and
3574 ;; displays a single long line.
3575 (message-box "Using GUD tooltips in this mode is unsafe \n\
3576 so they have been disabled."))
3577 (unless (null cmd) ; CMD can be nil if unknown debugger
3578 (if (eq gud-minor-mode 'gdbmi)
3579 (if gdb-macro-info
3580 (gdb-input
3581 (concat
3582 "server macro expand " expr "\n")
3583 `(lambda () (gdb-tooltip-print-1 ,expr)))
3584 (gdb-input
3585 (concat cmd "\n")
3586 `(lambda () (gdb-tooltip-print ,expr))))
3587 (add-function :override (process-filter process)
3588 #'gud-tooltip-process-output)
3589 (gud-basic-call cmd))
3590 expr))))))))
3592 (provide 'gud)
3594 ;;; gud.el ends here