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