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