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