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