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