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