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