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