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