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