f98509c2c10413ced29f233be255b5d2fb23f581
[bpt/guile.git] / ice-9 / debugger.scm
1 ;;;; Guile Debugger
2
3 ;;; Copyright (C) 1999, 2001 Free Software Foundation, Inc.
4 ;;;
5 ;;; This program is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU General Public License as
7 ;;; published by the Free Software Foundation; either version 2, or
8 ;;; (at your option) any later version.
9 ;;;
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU General Public License
16 ;;; along with this software; see the file COPYING. If not, write to
17 ;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;; Boston, MA 02111-1307 USA
19 ;;;
20 ;;; As a special exception, the Free Software Foundation gives permission
21 ;;; for additional uses of the text contained in its release of GUILE.
22 ;;;
23 ;;; The exception is that, if you link the GUILE library with other files
24 ;;; to produce an executable, this does not by itself cause the
25 ;;; resulting executable to be covered by the GNU General Public License.
26 ;;; Your use of that executable is in no way restricted on account of
27 ;;; linking the GUILE library code into it.
28 ;;;
29 ;;; This exception does not however invalidate any other reasons why
30 ;;; the executable file might be covered by the GNU General Public License.
31 ;;;
32 ;;; This exception applies only to the code released by the
33 ;;; Free Software Foundation under the name GUILE. If you copy
34 ;;; code from other Free Software Foundation releases into a copy of
35 ;;; GUILE, as the General Public License permits, the exception does
36 ;;; not apply to the code that you add in this way. To avoid misleading
37 ;;; anyone as to the status of such modified files, you must delete
38 ;;; this exception notice from them.
39 ;;;
40 ;;; If you write modifications of your own for GUILE, it is your choice
41 ;;; whether to permit this exception to apply to your modifications.
42 ;;; If you do not wish that, delete this exception notice.
43
44 (define-module (ice-9 debugger)
45 :use-module (ice-9 debug)
46 :use-module (ice-9 format)
47 :no-backtrace
48 )
49
50 (if (memq 'readline *features*)
51 (define-module (ice-9 debugger)
52 :use-module (ice-9 readline)))
53
54 \f
55 (define debugger-prompt "debug> ")
56
57 (define-public (debug)
58 (let ((stack (fluid-ref the-last-stack)))
59 (if stack
60 (let ((state (make-state stack 0)))
61 (display "This is the Guile debugger; type \"help\" for help.")
62 (newline)
63 (display "There are ")
64 (write (stack-length stack))
65 (display " frames on the stack.")
66 (newline)
67 (newline)
68 (write-state-short state)
69 (read-and-dispatch-commands state (current-input-port)))
70 (display "Nothing to debug.\n"))))
71
72 (define (debugger-handler key . args)
73 (case key
74 ((exit-debugger) #f)
75 ((signal)
76 ;; Restore stack
77 (fluid-set! the-last-stack (fluid-ref before-signal-stack))
78 (apply display-error #f (current-error-port) args))
79 (else
80 (display "Internal debugger error:\n")
81 (save-stack debugger-handler)
82 (apply throw key args)))
83 (throw 'exit-debugger)) ;Pop the stack
84
85 (define (read-and-dispatch-commands state port)
86 (catch 'exit-debugger
87 (lambda ()
88 (lazy-catch #t
89 (lambda ()
90 (with-fluids ((last-command #f))
91 (let loop ((state state))
92 (loop (read-and-dispatch-command state port)))))
93 debugger-handler))
94 (lambda args
95 *unspecified*)))
96
97 (define (read-and-dispatch-command state port)
98 (if (using-readline?)
99 (set-readline-prompt! debugger-prompt)
100 (display debugger-prompt))
101 (force-output) ;This should not be necessary...
102 (let ((token (read-token port)))
103 (cond ((eof-object? token)
104 (throw 'exit-debugger))
105 ((not token)
106 (discard-rest-of-line port)
107 (catch-user-errors port (lambda () (run-last-command state))))
108 (else
109 (or (catch-user-errors port
110 (lambda ()
111 (dispatch-command token command-table state port)))
112 state)))))
113
114 (define (run-last-command state)
115 (let ((procedure (fluid-ref last-command)))
116 (if procedure
117 (procedure state)
118 state)))
119
120 (define (catch-user-errors port thunk)
121 (catch 'debugger-user-error
122 thunk
123 (lambda (key . objects)
124 (apply user-warning objects)
125 (discard-rest-of-line port)
126 #f)))
127
128 (define last-command (make-fluid))
129
130 (define (user-warning . objects)
131 (for-each (lambda (object)
132 (display object))
133 objects)
134 (newline))
135
136 (define (user-error . objects)
137 (apply throw 'debugger-user-error objects))
138 \f
139 ;;;; Command dispatch
140
141 (define (dispatch-command string table state port)
142 (let ((value (command-table-value table string)))
143 (if value
144 (dispatch-command/value value state port)
145 (user-error "Unknown command: " string))))
146
147 (define (dispatch-command/value value state port)
148 (cond ((command? value)
149 (dispatch-command/command value state port))
150 ((command-table? value)
151 (dispatch-command/table value state port))
152 ((list? value)
153 (dispatch-command/name value state port))
154 (else
155 (error "Unrecognized command-table value: " value))))
156
157 (define (dispatch-command/command command state port)
158 (let ((procedure (command-procedure command))
159 (arguments ((command-parser command) port)))
160 (let ((procedure (lambda (state) (apply procedure state arguments))))
161 (warn-about-extra-args port)
162 (fluid-set! last-command procedure)
163 (procedure state))))
164
165 (define (warn-about-extra-args port)
166 ;; **** modify this to show the arguments.
167 (let ((char (skip-whitespace port)))
168 (cond ((eof-object? char) #f)
169 ((char=? #\newline char) (read-char port))
170 (else
171 (user-warning "Extra arguments at end of line: "
172 (read-rest-of-line port))))))
173
174 (define (dispatch-command/table table state port)
175 (let ((token (read-token port)))
176 (if (or (eof-object? token)
177 (not token))
178 (user-error "Command name too short.")
179 (dispatch-command token table state port))))
180
181 (define (dispatch-command/name name state port)
182 (let ((value (lookup-command name)))
183 (cond ((not value)
184 (apply user-error "Unknown command name: " name))
185 ((command-table? value)
186 (apply user-error "Partial command name: " name))
187 (else
188 (dispatch-command/value value state port)))))
189 \f
190 ;;;; Command definition
191
192 (define (define-command name argument-template documentation procedure)
193 (let ((name (canonicalize-command-name name)))
194 (add-command name
195 (make-command name
196 (argument-template->parser argument-template)
197 documentation
198 procedure)
199 command-table)
200 name))
201
202 (define (define-command-alias name1 name2)
203 (let ((name1 (canonicalize-command-name name1)))
204 (add-command name1 (canonicalize-command-name name2) command-table)
205 name1))
206 \f
207 (define (argument-template->parser template)
208 ;; Deliberately handles only cases that occur in "commands.scm".
209 (cond ((eq? 'tokens template)
210 (lambda (port)
211 (let loop ((tokens '()))
212 (let ((token (read-token port)))
213 (if (or (eof-object? token)
214 (not token))
215 (list (reverse! tokens))
216 (loop (cons token tokens)))))))
217 ((null? template)
218 (lambda (port)
219 '()))
220 ((and (pair? template)
221 (null? (cdr template))
222 (eq? 'object (car template)))
223 (lambda (port)
224 (list (read port))))
225 ((and (pair? template)
226 (equal? ''optional (car template))
227 (pair? (cdr template))
228 (null? (cddr template)))
229 (case (cadr template)
230 ((token)
231 (lambda (port)
232 (let ((token (read-token port)))
233 (if (or (eof-object? token)
234 (not token))
235 (list #f)
236 (list token)))))
237 ((exact-integer)
238 (lambda (port)
239 (list (parse-optional-exact-integer port))))
240 ((exact-nonnegative-integer)
241 (lambda (port)
242 (list (parse-optional-exact-nonnegative-integer port))))
243 ((object)
244 (lambda (port)
245 (list (parse-optional-object port))))
246 (else
247 (error "Malformed argument template: " template))))
248 (else
249 (error "Malformed argument template: " template))))
250
251 (define (parse-optional-exact-integer port)
252 (let ((object (parse-optional-object port)))
253 (if (or (not object)
254 (and (integer? object)
255 (exact? object)))
256 object
257 (user-error "Argument not an exact integer: " object))))
258
259 (define (parse-optional-exact-nonnegative-integer port)
260 (let ((object (parse-optional-object port)))
261 (if (or (not object)
262 (and (integer? object)
263 (exact? object)
264 (not (negative? object))))
265 object
266 (user-error "Argument not an exact non-negative integer: " object))))
267
268 (define (parse-optional-object port)
269 (let ((terminator (skip-whitespace port)))
270 (if (or (eof-object? terminator)
271 (eq? #\newline terminator))
272 #f
273 (let ((object (read port)))
274 (if (eof-object? object)
275 #f
276 object)))))
277 \f
278 ;;;; Command tables
279
280 (define (lookup-command name)
281 (let loop ((table command-table) (strings name))
282 (let ((value (command-table-value table (car strings))))
283 (cond ((or (not value) (null? (cdr strings))) value)
284 ((command-table? value) (loop value (cdr strings)))
285 (else #f)))))
286
287 (define (command-table-value table string)
288 (let ((entry (command-table-entry table string)))
289 (and entry
290 (caddr entry))))
291
292 (define (command-table-entry table string)
293 (let loop ((entries (command-table-entries table)))
294 (and (not (null? entries))
295 (let ((entry (car entries)))
296 (if (and (<= (cadr entry)
297 (string-length string)
298 (string-length (car entry)))
299 (= (string-length string)
300 (match-strings (car entry) string)))
301 entry
302 (loop (cdr entries)))))))
303
304 (define (match-strings s1 s2)
305 (let ((n (min (string-length s1) (string-length s2))))
306 (let loop ((i 0))
307 (cond ((= i n) i)
308 ((char=? (string-ref s1 i) (string-ref s2 i)) (loop (+ i 1)))
309 (else i)))))
310
311 (define (write-command-name name)
312 (display (car name))
313 (for-each (lambda (string)
314 (write-char #\space)
315 (display string))
316 (cdr name)))
317 \f
318 (define (add-command name value table)
319 (let loop ((strings name) (table table))
320 (let ((entry
321 (or (let loop ((entries (command-table-entries table)))
322 (and (not (null? entries))
323 (if (string=? (car strings) (caar entries))
324 (car entries)
325 (loop (cdr entries)))))
326 (let ((entry (list (car strings) #f #f)))
327 (let ((entries
328 (let ((entries (command-table-entries table)))
329 (if (or (null? entries)
330 (string<? (car strings) (caar entries)))
331 (cons entry entries)
332 (begin
333 (let loop ((prev entries) (this (cdr entries)))
334 (if (or (null? this)
335 (string<? (car strings) (caar this)))
336 (set-cdr! prev (cons entry this))
337 (loop this (cdr this))))
338 entries)))))
339 (compute-string-abbreviations! entries)
340 (set-command-table-entries! table entries))
341 entry))))
342 (if (null? (cdr strings))
343 (set-car! (cddr entry) value)
344 (loop (cdr strings)
345 (if (command-table? (caddr entry))
346 (caddr entry)
347 (let ((table (make-command-table '())))
348 (set-car! (cddr entry) table)
349 table)))))))
350
351 (define (canonicalize-command-name name)
352 (cond ((and (string? name)
353 (not (string-null? name)))
354 (list name))
355 ((let loop ((name name))
356 (and (pair? name)
357 (string? (car name))
358 (not (string-null? (car name)))
359 (or (null? (cdr name))
360 (loop (cdr name)))))
361 name)
362 (else
363 (error "Illegal command name: " name))))
364
365 (define (compute-string-abbreviations! entries)
366 (let loop ((entries entries) (index 0))
367 (let ((groups '()))
368 (for-each
369 (lambda (entry)
370 (let* ((char (string-ref (car entry) index))
371 (group (assv char groups)))
372 (if group
373 (set-cdr! group (cons entry (cdr group)))
374 (set! groups
375 (cons (list char entry)
376 groups)))))
377 entries)
378 (for-each
379 (lambda (group)
380 (let ((index (+ index 1)))
381 (if (null? (cddr group))
382 (set-car! (cdadr group) index)
383 (loop (let ((entry
384 (let loop ((entries (cdr group)))
385 (and (not (null? entries))
386 (if (= index (string-length (caar entries)))
387 (car entries)
388 (loop (cdr entries)))))))
389 (if entry
390 (begin
391 (set-car! (cdr entry) index)
392 (delq entry (cdr group)))
393 (cdr group)))
394 index))))
395 groups))))
396 \f
397 ;;;; Data structures
398
399 (define command-table-rtd (make-record-type "command-table" '(entries)))
400 (define make-command-table (record-constructor command-table-rtd '(entries)))
401 (define command-table? (record-predicate command-table-rtd))
402 (define command-table-entries (record-accessor command-table-rtd 'entries))
403 (define set-command-table-entries!
404 (record-modifier command-table-rtd 'entries))
405
406 (define command-rtd
407 (make-record-type "command"
408 '(name parser documentation procedure)))
409
410 (define make-command
411 (record-constructor command-rtd
412 '(name parser documentation procedure)))
413
414 (define command? (record-predicate command-rtd))
415 (define command-name (record-accessor command-rtd 'name))
416 (define command-parser (record-accessor command-rtd 'parser))
417 (define command-documentation (record-accessor command-rtd 'documentation))
418 (define command-procedure (record-accessor command-rtd 'procedure))
419
420 (define state-rtd (make-record-type "debugger-state" '(stack index)))
421 (define state? (record-predicate state-rtd))
422 (define make-state (record-constructor state-rtd '(stack index)))
423 (define state-stack (record-accessor state-rtd 'stack))
424 (define state-index (record-accessor state-rtd 'index))
425
426 (define (new-state-index state index)
427 (make-state (state-stack state) index))
428 \f
429 ;;;; Character parsing
430
431 (define (read-token port)
432 (letrec
433 ((loop
434 (lambda (chars)
435 (let ((char (peek-char port)))
436 (cond ((eof-object? char)
437 (do-eof char chars))
438 ((char=? #\newline char)
439 (do-eot chars))
440 ((char-whitespace? char)
441 (do-eot chars))
442 ((char=? #\# char)
443 (read-char port)
444 (let ((terminator (skip-comment port)))
445 (if (eof-object? char)
446 (do-eof char chars)
447 (do-eot chars))))
448 (else
449 (read-char port)
450 (loop (cons char chars)))))))
451 (do-eof
452 (lambda (eof chars)
453 (if (null? chars)
454 eof
455 (do-eot chars))))
456 (do-eot
457 (lambda (chars)
458 (if (null? chars)
459 #f
460 (list->string (reverse! chars))))))
461 (skip-whitespace port)
462 (loop '())))
463
464 (define (skip-whitespace port)
465 (let ((char (peek-char port)))
466 (cond ((or (eof-object? char)
467 (char=? #\newline char))
468 char)
469 ((char-whitespace? char)
470 (read-char port)
471 (skip-whitespace port))
472 ((char=? #\# char)
473 (read-char port)
474 (skip-comment port))
475 (else char))))
476
477 (define (skip-comment port)
478 (let ((char (peek-char port)))
479 (if (or (eof-object? char)
480 (char=? #\newline char))
481 char
482 (begin
483 (read-char port)
484 (skip-comment port)))))
485
486 (define (read-rest-of-line port)
487 (let loop ((chars '()))
488 (let ((char (read-char port)))
489 (if (or (eof-object? char)
490 (char=? #\newline char))
491 (list->string (reverse! chars))
492 (loop (cons char chars))))))
493
494 (define (discard-rest-of-line port)
495 (let loop ()
496 (if (not (let ((char (read-char port)))
497 (or (eof-object? char)
498 (char=? #\newline char))))
499 (loop))))
500 \f
501 ;;;; Commands
502
503 (define command-table (make-command-table '()))
504
505 (define-command "help" 'tokens
506 "Type \"help\" followed by a command name for full documentation."
507 (lambda (state tokens)
508 (let loop ((name (if (null? tokens) '("help") tokens)))
509 (let ((value (lookup-command name)))
510 (cond ((not value)
511 (write-command-name name)
512 (display " is not a known command name.")
513 (newline))
514 ((command? value)
515 (display (command-documentation value))
516 (newline)
517 (if (equal? '("help") (command-name value))
518 (begin
519 (display "Available commands are:")
520 (newline)
521 (for-each (lambda (entry)
522 (if (not (list? (caddr entry)))
523 (begin
524 (display " ")
525 (display (car entry))
526 (newline))))
527 (command-table-entries command-table)))))
528 ((command-table? value)
529 (display "The \"")
530 (write-command-name name)
531 (display "\" command requires a subcommand.")
532 (newline)
533 (display "Available subcommands are:")
534 (newline)
535 (for-each (lambda (entry)
536 (if (not (list? (caddr entry)))
537 (begin
538 (display " ")
539 (write-command-name name)
540 (write-char #\space)
541 (display (car entry))
542 (newline))))
543 (command-table-entries value)))
544 ((list? value)
545 (loop value))
546 (else
547 (error "Unknown value from lookup-command:" value)))))
548 state))
549
550 (define-command "frame" '('optional exact-nonnegative-integer)
551 "Select and print a stack frame.
552 With no argument, print the selected stack frame. (See also \"info frame\").
553 An argument specifies the frame to select; it must be a stack-frame number."
554 (lambda (state n)
555 (let ((state (if n (select-frame-absolute state n) state)))
556 (write-state-short state)
557 state)))
558
559 (define-command "position" '()
560 "Display the position of the current expression."
561 (lambda (state)
562 (let* ((frame (stack-ref (state-stack state) (state-index state)))
563 (source (frame-source frame)))
564 (if (not source)
565 (display "No source available for this frame.")
566 (let ((position (source-position source)))
567 (if (not position)
568 (display "No position information available for this frame.")
569 (display-position position)))))
570 (newline)
571 state))
572
573 (define-command "up" '('optional exact-integer)
574 "Move N frames up the stack. For positive numbers N, this advances
575 toward the outermost frame, to higher frame numbers, to frames
576 that have existed longer. N defaults to one."
577 (lambda (state n)
578 (let ((state (select-frame-relative state (or n 1))))
579 (write-state-short state)
580 state)))
581
582 (define-command "down" '('optional exact-integer)
583 "Move N frames down the stack. For positive numbers N, this
584 advances toward the innermost frame, to lower frame numbers, to
585 frames that were created more recently. N defaults to one."
586 (lambda (state n)
587 (let ((state (select-frame-relative state (- (or n 1)))))
588 (write-state-short state)
589 state)))
590 \f
591 (define (eval-handler key . args)
592 (let ((stack (make-stack #t eval-handler)))
593 (if (= (length args) 4)
594 (apply display-error stack (current-error-port) args)
595 ;; We want display-error to be the "final common pathway"
596 (catch #t
597 (lambda ()
598 (apply bad-throw key args))
599 (lambda (key . args)
600 (apply display-error stack (current-error-port) args)))))
601 (throw 'continue))
602
603 (define-command "evaluate" '(object)
604 "Evaluate an expression.
605 The expression must appear on the same line as the command,
606 however it may be continued over multiple lines."
607 (lambda (state expression)
608 (let ((source (frame-source (stack-ref (state-stack state)
609 (state-index state)))))
610 (if (not source)
611 (display "No environment for this frame.\n")
612 (catch 'continue
613 (lambda ()
614 (lazy-catch #t
615 (lambda ()
616 (let* ((env (memoized-environment source))
617 (value (local-eval expression env)))
618 (display ";value: ")
619 (write value)
620 (newline)))
621 eval-handler))
622 (lambda args args)))
623 state)))
624
625 (define-command "backtrace" '('optional exact-integer)
626 "Print backtrace of all stack frames, or innermost COUNT frames.
627 With a negative argument, print outermost -COUNT frames.
628 If the number of frames aren't explicitly given, the debug option
629 `depth' determines the maximum number of frames printed."
630 (lambda (state n-frames)
631 (let ((stack (state-stack state)))
632 ;; Kludge around lack of call-with-values.
633 (let ((values
634 (lambda (start end)
635 ;;(do ((index start (+ index 1)))
636 ;; ((= index end))
637 ;;(write-state-short* stack index))
638 ;;
639 ;; Use builtin backtrace instead:
640 (display-backtrace stack
641 (current-output-port)
642 (if (memq 'backwards (debug-options))
643 start
644 (- end 1))
645 (- end start))
646 )))
647 (let ((end (stack-length stack)))
648 (cond ((not n-frames) ;(>= (abs n-frames) end))
649 (values 0 (min end (cadr (memq 'depth (debug-options))))))
650 ((>= n-frames 0)
651 (values 0 n-frames))
652 (else
653 (values (+ end n-frames) end))))))
654 state))
655
656 (define-command "quit" '()
657 "Exit the debugger."
658 (lambda (state)
659 (throw 'exit-debugger)))
660
661 (define-command '("info" "frame") '()
662 "All about selected stack frame."
663 (lambda (state)
664 (write-state-long state)
665 state))
666
667 (define-command '("info" "args") '()
668 "Argument variables of current stack frame."
669 (lambda (state)
670 (let ((index (state-index state)))
671 (let ((frame (stack-ref (state-stack state) index)))
672 (write-frame-index-long frame)
673 (write-frame-args-long frame)))
674 state))
675
676 (define-command-alias "f" "frame")
677 (define-command-alias '("info" "f") '("info" "frame"))
678 (define-command-alias "bt" "backtrace")
679 (define-command-alias "where" "backtrace")
680 (define-command-alias "p" "evaluate")
681 (define-command-alias '("info" "stack") "backtrace")
682 \f
683 ;;;; Command Support
684
685 (define (select-frame-absolute state number)
686 (new-state-index state
687 (frame-number->index
688 (let ((end (stack-length (state-stack state))))
689 (if (>= number end)
690 (- end 1)
691 number))
692 (state-stack state))))
693
694 (define (select-frame-relative state delta)
695 (new-state-index state
696 (let ((index (+ (state-index state) delta))
697 (end (stack-length (state-stack state))))
698 (cond ((< index 0) 0)
699 ((>= index end) (- end 1))
700 (else index)))))
701
702 (define (write-state-short state)
703 (display "Frame ")
704 (write-state-short* (state-stack state) (state-index state)))
705
706 (define (write-state-short* stack index)
707 (write-frame-index-short stack index)
708 (write-char #\space)
709 (write-frame-short (stack-ref stack index))
710 (newline))
711
712 (define (write-frame-index-short stack index)
713 (let ((s (number->string (frame-number (stack-ref stack index)))))
714 (display s)
715 (write-char #\:)
716 (write-chars #\space (- 4 (string-length s)))))
717
718 (define (write-frame-short frame)
719 (if (frame-procedure? frame)
720 (write-frame-short/application frame)
721 (write-frame-short/expression frame)))
722
723 (define (write-frame-short/application frame)
724 (write-char #\[)
725 (write (let ((procedure (frame-procedure frame)))
726 (or (and (procedure? procedure)
727 (procedure-name procedure))
728 procedure)))
729 (if (frame-evaluating-args? frame)
730 (display " ...")
731 (begin
732 (for-each (lambda (argument)
733 (write-char #\space)
734 (write argument))
735 (frame-arguments frame))
736 (write-char #\]))))
737
738 ;;; Use builtin function instead:
739 (set! write-frame-short/application
740 (lambda (frame)
741 (display-application frame (current-output-port) 12)))
742
743 (define (write-frame-short/expression frame)
744 (write (let* ((source (frame-source frame))
745 (copy (source-property source 'copy)))
746 (if (pair? copy)
747 copy
748 (unmemoize source)))))
749 \f
750 (define (write-state-long state)
751 (let ((index (state-index state)))
752 (let ((frame (stack-ref (state-stack state) index)))
753 (write-frame-index-long frame)
754 (write-frame-long frame))))
755
756 (define (write-frame-index-long frame)
757 (display "Stack frame: ")
758 (write (frame-number frame))
759 (if (frame-real? frame)
760 (display " (real)"))
761 (newline))
762
763 (define (write-frame-long frame)
764 (if (frame-procedure? frame)
765 (write-frame-long/application frame)
766 (write-frame-long/expression frame)))
767
768 (define (write-frame-long/application frame)
769 (display "This frame is an application.")
770 (newline)
771 (if (frame-source frame)
772 (begin
773 (display "The corresponding expression is:")
774 (newline)
775 (display-source frame)
776 (newline)))
777 (display "The procedure being applied is: ")
778 (write (let ((procedure (frame-procedure frame)))
779 (or (and (procedure? procedure)
780 (procedure-name procedure))
781 procedure)))
782 (newline)
783 (display "The procedure's arguments are")
784 (if (frame-evaluating-args? frame)
785 (display " being evaluated.")
786 (begin
787 (display ": ")
788 (write (frame-arguments frame))))
789 (newline))
790
791 (define (display-source frame)
792 (let* ((source (frame-source frame))
793 (copy (source-property source 'copy)))
794 (cond ((source-position source)
795 => (lambda (p) (display-position p) (display ":\n"))))
796 (display " ")
797 (write (or copy (unmemoize source)))))
798
799 (define (source-position source)
800 (let ((fname (source-property source 'filename))
801 (line (source-property source 'line))
802 (column (source-property source 'column)))
803 (and fname
804 (list fname line column))))
805
806 (define (display-position pos)
807 (format #t "~A:~D:~D" (car pos) (+ 1 (cadr pos)) (+ 1 (caddr pos))))
808
809 (define (write-frame-long/expression frame)
810 (display "This frame is an evaluation.")
811 (newline)
812 (display "The expression being evaluated is:")
813 (newline)
814 (display-source frame)
815 (newline))
816
817 (define (write-frame-args-long frame)
818 (if (frame-procedure? frame)
819 (let ((arguments (frame-arguments frame)))
820 (let ((n (length arguments)))
821 (display "This frame has ")
822 (write n)
823 (display " argument")
824 (if (not (= n 1))
825 (display "s"))
826 (write-char (if (null? arguments) #\. #\:))
827 (newline))
828 (for-each (lambda (argument)
829 (display " ")
830 (write argument)
831 (newline))
832 arguments))
833 (begin
834 (display "This frame is an evaluation frame; it has no arguments.")
835 (newline))))
836
837 (define (write-chars char n)
838 (do ((i 0 (+ i 1)))
839 ((>= i n))
840 (write-char char)))