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