-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.html
1906 lines (1392 loc) · 65 KB
/
debug.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><!-- DO NOT EDIT THIS FILE-->
<!-- Edit the .tex version instead-->
<html>
<head>
<title>Debugging</title>
<link href="csug.css" rel="stylesheet" type="text/css">
</head>
<body>
<a name="g15"></a>
<a name="./debug:h0"></a>
<h1>Chapter 3. Debugging<a name="CHPTDEBUG"></a></h1>
<p>
<i>Chez Scheme</i> has several features that support debugging.
In addition to providing error messages when fully type-checked code is
run, <i>Chez Scheme</i> also permits tracing of procedure calls, interruption
of any computation, redefinition of exception and interrupt handlers,
and inspection of any object, including the continuations of exceptions and
interrupts.
<p>
Programmers new to Scheme or <i>Chez Scheme</i>, and even more experienced
Scheme programmers, might want to consult
the tutorial "How to Debug Chez Scheme Programs."
HTML and PDF versions
are available at
<a href=http://www.cs.indiana.edu/chezscheme/debug/>http://www.cs.indiana.edu/chezscheme/debug/</a>.
<p>
<h3><a name="g16"></a><a name="./debug:h1"></a>Section 3.1. Tracing<a name="SECTDEBUGTRACING"></a></h3>
<p>
Tracing is one of the most useful mechanisms for debugging Scheme programs.
<i>Chez Scheme</i> permits any primitive or user-defined procedure to be traced.
The trace package prints the arguments and return values for each
traced procedure with a compact indentation mechanism that shows the
nesting depth of calls.
The distinction between tail calls and nontail calls is reflected
properly by an increase in indentation for nontail calls only.
For nesting depths of 10 or greater, a number in brackets is used in
place of indentation to signify nesting depth.
<p>
This section covers the mechanisms for tracing procedures and
controlling trace output.
<p>
<a name="./debug:s0"></a><span class=formdef><b>syntax</b>: <tt>(trace-lambda <i>name</i> <i>formals</i> <i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>a traced procedure
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><a name="./debug:s1"></a>A <tt>trace-lambda</tt> expression is equivalent to a
<tt>lambda</tt> expression with the same formals and body
except that trace information is printed to the trace output port whenever
the procedure is invoked, using <tt><i>name</i></tt> to identify the procedure.
The trace information shows the value of the arguments passed to the
procedure and the values returned by the procedure, with indentation to
show the nesting of calls.
<p>
The traced procedure <a name="./debug:s2"></a><tt>half</tt> defined below
returns the integer quotient of its argument and 2.
<p>
<p><tt>(define half<br>
(trace-lambda half (x)<br>
(cond<br>
[(zero? x) 0]<br>
[(odd? x) (half (- x 1))]<br>
[(even? x) (+ (half (- x 1)) 1)])))</tt>
<p>A trace of the call <tt>(half 5)</tt>, which returns 2, is shown below.
<p>
<p><tt>|(half 5)<br>
|(half 4)<br>
| (half 3)<br>
| (half 2)<br>
| |(half 1)<br>
| |(half 0)<br>
| |0<br>
| 1<br>
|2</tt>
<p>This example highlights the proper treatment of tail and nontail calls
by the trace package.
Since <tt>half</tt> tail calls itself when its argument is odd, the call
<tt>(half 4)</tt> appears at the same level of indentation as the call
<tt>(half 5)</tt>.
Furthermore, since the return values of <tt>(half 5)</tt> and
<tt>(half 4)</tt> are necessarily the same, only one return value is
shown for both calls.
<p>
<a name="./debug:s3"></a><span class=formdef><b>syntax</b>: <tt>(trace-case-lambda <i>name</i> <i>clause</i> ...)</tt></span>
<br>
<b>returns: </b>a traced procedure
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><a name="./debug:s4"></a>A <tt>trace-case-lambda</tt> expression is
equivalent to a <tt>case-lambda</tt> expression with the same clauses
except that trace information is printed to the trace output port whenever
the procedure is invoked, using <tt><i>name</i></tt> to identify the procedure.
The trace information shows the value of the arguments passed to the
procedure and the values returned by the procedure, with indentation to
show the nesting of calls.
<p>
<a name="./debug:s5"></a><span class=formdef><b>syntax</b>: <tt>(trace-let <i>name</i> ((<i>var</i> <i>expr</i>) ...) <i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>the values of the body <tt><i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...</tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><a name="./debug:s6"></a>A <tt>trace-let</tt> expression is equivalent to a
named <tt>let</tt> expression with the same name, bindings, and body
except that trace information is printed to the trace output port on
entry or reentry (via invocation of the procedure bound to <tt>name</tt>)
into the <tt>trace-let</tt> expression.
<p>
A <tt>trace-let</tt> expression of the form
<p>
<p><tt>(trace-let <i>name</i> ([<i>var</i> <i>expr</i>] ...)<br>
<i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt>
<p>can be rewritten in terms of <tt>trace-lambda</tt> as follows:
<p>
<p><tt>((letrec ([<i>name</i><br>
(trace-lambda <i>name</i> (<i>var</i> ...)<br>
<i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)])<br>
<i>name</i>)<br>
<i>expr</i> ...)</tt>
<p><tt>trace-let</tt> may be used to trace ordinary <tt>let</tt> expressions
as well as <tt>let</tt> expressions as long as the name inserted along
with the <tt>trace-let</tt> keyword in place of <tt>let</tt> does not
appear free within the body of the <tt>let</tt> expression.
It is also sometimes useful to insert a <tt>trace-let</tt> expression
into a program simply to display the value of an arbitrary expression
at the current trace indentation.
For example, a call to the following variant of <tt>half</tt>
<p>
<p><tt>(define half<br>
(trace-lambda half (x)<br>
(cond<br>
[(zero? x) 0]<br>
[(odd? x) (half (trace-let decr-value () (- x 1)))]<br>
[(even? x) (+ (half (- x 1)) 1)])))</tt>
<p>with argument 5 results in the trace:
<p>
<p><tt>|(half 5)<br>
| (decr-value)<br>
| 4<br>
|(half 4)<br>
| (half 3)<br>
| |(decr-value)<br>
| |2<br>
| (half 2)<br>
| |(half 1)<br>
| | (decr-value)<br>
| | 0<br>
| |(half 0)<br>
| 1<br>
|2</tt>
<p><a name="./debug:s7"></a><span class=formdef><b>syntax</b>: <tt>(trace-do ((<i>var</i> <i>init</i> <i>update</i>) ...) (<i>test</i> <i>result</i> ...) <i>expr</i> ...)</tt></span>
<br>
<b>returns: </b>the values of the last <tt><i>result</i></tt> expression
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><a name="./debug:s8"></a>A <tt>trace-do</tt> expression is equivalent to a
<tt>do</tt> expression with the same subforms,
except that trace information is printed to the trace output port,
showing the values of <tt><i>var</i> ...</tt> and each iteration and
the final value of the loop on termination.
For example, the expression
<p>
<p><tt>(trace-do ([old '(a b c) (cdr old)]<br>
[new '() (cons (car old) new)])<br>
((null? old) new))</tt>
<p>produces the trace
<p>
<p><tt>|(do (a b c) ())<br>
|(do (b c) (a))<br>
|(do (c) (b a))<br>
|(do () (c b a))<br>
|(c b a)</tt>
<p>and returns <tt>(c b a)</tt>.
<p>
<a name="./debug:s9"></a><span class=formdef><b>syntax</b>: <tt>(trace <i>var<sub>1</sub></i> <i>var<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>a list of <tt><i>var<sub>1</sub></i> <i>var<sub>2</sub></i> ...</tt>
<br><span class=formdef><b>syntax</b>: <tt>(trace)</tt></span>
<br>
<b>returns: </b>a list of all currently traced top-level variables
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>In the first form, <tt>trace</tt> reassigns the top-level values of
<tt><i>var<sub>1</sub></i> <i>var<sub>2</sub></i> ...</tt>, whose values must be procedures,
to equivalent procedures that display trace information in the manner
of <tt>trace-lambda</tt>.
<p>
<tt>trace</tt> works by encapsulating the old value of each var in a
traced procedure.
It could be defined approximately as follows. (The actual version
records and returns information about traced variables.)
<p>
<p><tt>(define-syntax trace<br>
(syntax-rules ()<br>
[(_ var ...)<br>
(begin<br>
(set-top-level-value! 'var<br>
(let ([p (top-level-value 'var)])<br>
(trace-lambda var args (apply p args))))<br>
...)]))</tt>
<p>Tracing for a procedure traced in this manner may be disabled via
<tt>untrace</tt> (see below), an assignment of the corresponding
variable to a different, untraced value, or a subsequent use of
<tt>trace</tt> for the same variable.
Because the value is traced and not the binding, however, a traced
value obtained before tracing is disabled and retained after tracing is
disabled will remain traced.
<p>
<tt>trace</tt> without subexpressions evaluates to a list of all
currently traced variables.
A variable is currently traced if it has been traced and
not subsequently untraced or assigned to a different value.
<p>
The following transcript demonstrates the use of <tt>trace</tt> in
an interactive session.
<p>
<p><tt>> (define half<br>
(lambda (x)<br>
(cond<br>
[(zero? x) 0]<br>
[(odd? x) (half (- x 1))]<br>
[(even? x) (+ (half (- x 1)) 1)])))<br>
> (half 5)<br>
2<br>
> (trace half)<br>
(half)<br>
> (half 5)<br>
|(half 5)<br>
|(half 4)<br>
| (half 3)<br>
| (half 2)<br>
| |(half 1)<br>
| |(half 0)<br>
| |0<br>
| 1<br>
|2<br>
2<br>
> (define traced-half half)<br>
> (untrace half)<br>
(half)<br>
> (half 2)<br>
1<br>
> (traced-half 2)<br>
|(half 2)<br>
|1<br>
1</tt>
<p><a name="./debug:s10"></a><span class=formdef><b>syntax</b>: <tt>(untrace <i>var<sub>1</sub></i> <i>var<sub>2</sub></i> ...)</tt></span>
<br><span class=formdef><b>syntax</b>: <tt>(untrace)</tt></span>
<br>
<b>returns: </b>a list of untraced variables
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>untrace</tt> restores the original (pre-<tt>trace</tt>) top-level values
of each currently traced variable in
<tt><i>var<sub>1</sub></i> <i>var<sub>2</sub></i> ...</tt>,
effectively disabling the tracing of the values of these variables.
Any variable in <tt><i>var<sub>1</sub></i> <i>var<sub>2</sub></i> ...</tt> that is not
currently traced is ignored.
If <tt>untrace</tt> is called without arguments, the values of all
currently traced variables are restored.
<p>
The following transcript demonstrates the use of <tt>trace</tt> and
<tt>untrace</tt> in an interactive session to debug an incorrect
procedure definition.
<p>
<p><tt>> (define square-minus-one<br>
(lambda (x)<br>
(- (* x x) 2)))<br>
> (square-minus-one 3)<br>
7<br>
> (trace square-minus-one * -)<br>
(square-minus-one * -)<br>
> (square-minus-one 3)<br>
|(square-minus-one 3)<br>
| (* 3 3)<br>
| 9<br>
|(- 9 2)<br>
|7<br>
7<br>
> (define square-minus-one<br>
(lambda (x)<br>
(- (* x x) 1))) ; change the 2 to 1<br>
> (trace)<br>
(- *)<br>
> (square-minus-one 3)<br>
|(* 3 3)<br>
|9<br>
|(- 9 1)<br>
|8<br>
8<br>
> (untrace square-minus-one)<br>
()<br>
> (untrace * -)<br>
(- *)<br>
> (square-minus-one 3)<br>
8</tt>
<p>The first call to <tt>square-minus-one</tt> indicates there is an error,
the second (traced) call indicates the step at which the error occurs,
the third call demonstrates that the fix works,
and the fourth call demonstrates that
<tt>untrace</tt> does not wipe out the fix.
<p>
<a name="./debug:s11"></a><span class=formdef><b>thread parameter</b>: <tt>trace-output-port</tt></span>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>trace-output-port</tt> is a parameter that determines the
output port to which tracing information is sent.
When called with no arguments, <tt>trace-output-port</tt> returns the
current trace output port.
When called with one argument, which must be a textual output port,
<tt>trace-output-port</tt> changes the value of the current
trace output port.
<p>
<a name="./debug:s12"></a><span class=formdef><b>thread parameter</b>: <tt>trace-print</tt></span>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>The value of <tt>trace-print</tt> must be a procedure of two arguments,
an object and an output port.
The trace package uses the value of <tt>trace-print</tt> to print the
arguments and return values for each call to a traced procedure.
<tt>trace-print</tt> is set to <tt>pretty-print</tt> by default.
<p>
The trace package sets
<a name="./debug:s13"></a><tt>pretty-initial-indent</tt>
to an appropriate value for the current nesting level before calling
the value of <tt>trace-print</tt> so that multiline output can be
indented properly.
<p>
<a name="./debug:s14"></a><span class=formdef><b>syntax</b>: <tt>(trace-define <i>var</i> <i>expr</i>)</tt></span>
<br><span class=formdef><b>syntax</b>: <tt>(trace-define (<i>var</i> . <i>idspec</i>) <i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>trace-define</tt> is a convenient shorthand for defining variables bound
to traced procedures of the same name.
The first form is equivalent to
<p>
<p><tt>(define <i>var</i><br>
(let ([x <i>expr</i>])<br>
(trace-lambda <i>var</i> args<br>
(apply x args))))</tt>
<p>and the second is equivalent to
<p>
<p><tt>(define <i>var</i><br>
(trace-lambda <i>var</i> <i>idspec</i><br>
<i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...))</tt>
<p>In the former case, <tt><i>expr</i></tt> must evaluate to a procedure.
<p>
<p><tt>> (let ()<br>
(trace-define plus<br>
(lambda (x y) <br>
(+ x y)))<br>
(list (plus 3 4) (+ 5 6)))<br>
|(plus 3 4)<br>
|7<br>
(7 11)</tt>
<p><a name="./debug:s15"></a><span class=formdef><b>syntax</b>: <tt>(trace-define-syntax <i>keyword</i> <i>expr</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>trace-define-syntax</tt> traces the input and output to the
transformer value of <tt><i>expr</i></tt>, stripped of the contextual
information used by the expander to maintain lexical scoping.
<p>
<p><tt>> (trace-define-syntax let*<br>
(syntax-rules ()<br>
[(_ () b1 b2 ...)<br>
(let () b1 b2 ...)]<br>
[(_ ((x e) m ...) b1 b2 ...)<br>
(let ((x e))<br>
(let* (m ...) b1 b2 ...))]))<br>
> (let* ([x 3] [y (+ x x)]) (list x y))<br>
|(let* (let* [(x 3) (y (+ x x))] [list x y]))<br>
|(let ([x 3]) (let* ([y (+ x x)]) (list x y)))<br>
|(let* (let* [(y (+ x x))] [list x y]))<br>
|(let ([y (+ x x)]) (let* () (list x y)))<br>
|(let* (let* () [list x y]))<br>
|(let () (list x y))<br>
(3 6)</tt>
<p>Without contextual information, the displayed forms are more readable
but less precise, since different identifiers with the same name are
indistinguishable, as shown in the example below.
<p>
<p><tt>> (let ([x 0])<br>
(trace-define-syntax a<br>
(syntax-rules ()<br>
[(_ y) (eq? x y)]))<br>
(let ([x 1])<br>
(a x)))<br>
|(a (a x))<br>
|(eq? x x)<br>
#f</tt>
<p>
<h3><a name="g17"></a><a name="./debug:h2"></a>Section 3.2. The Interactive Debugger<a name="SECTDEBUGINTERACTIVE"></a></h3>
<p>
The interactive debugger is entered as a result of
a call to the procedure <tt>debug</tt> after an exception is handled
by the default exception handler.
It can also be entered directly from the default exception handler, for
serious or non-warning conditions, if the parameter
<tt>debug-on-exception</tt> is true.
<p>
Within the debugger, the command "?" lists the debugger command options.
These include commands to:
<p>
<ul>
<li>inspect the raise continuation,
<li>display the condition,
<li>inspect the condition, and
<li>exit the debugger.
</ul>
<p>
<p>
The raise continuation is the continuation encapsulated within the
condition, if any.
The standard exception reporting procedures and forms <tt>assert</tt>,
<tt>assertion-violation</tt>, and <tt>error</tt> as well as the
<i>Chez Scheme</i> procedures <tt>assertion-violationf</tt>, <tt>errorf</tt>,
and <tt>syntax-error</tt> all raise exceptions with conditions that
encapsulate the continuations of their calls, allowing the programmer to
inspect the frames of pending calls at the point of a violation, error, or
failed assertion.
<p>
A variant of the interactive debugger, the break handler, is entered as
the result of a keyboard interrupt handled by the default
keyboard-interrupt handler or an explicit call to the procedure
<tt>break</tt> handled by the default break handler.
Again, the command "?" lists the command options.
These include commands to:
<p>
<ul>
<li>exit the break handler and continue,
<li>reset to the current café,
<li>abort the entire Scheme session,
<li>enter a new café,
<li>inspect the current continuation, and
<li>display program statistics (run time and memory usage).
</ul>
<p>
<p>
It is also usually possible to exit from the debugger or break handler by
typing the end-of-file character ("control-D" under Unix, "control-Z"
under Windows).
<p>
<a name="./debug:s16"></a><span class=formdef><b>procedure</b>: <tt>(debug)</tt></span>
<br>
<b>returns: </b>does not return
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>When the default exception handler receives a serious or non-warning
condition, it displays the condition and resets to the current café.
Before it resets, it saves the condition in the parameter
<tt>debug-condition</tt>.
The <tt>debug</tt> procedure may be used to inspect the condition.
Whenever one of the built-in error-reporting mechanisms is used to
raise an exception, the continuation at the point where the
exception was raised can be inspected as well.
More generally, <tt>debug</tt> allows the continuation contained
within any continuation condition created by
<tt>make-continuation-condition</tt> to be inspected.
<p>
If the parameter <tt>debug-on-exception</tt> is set to <tt>#t</tt>,
the default exception handler enters the debugger directly for all
serious and non-warning conditions, delaying its reset until after
the debugger exits.
The <a name="./debug:s17"></a><tt>--debug-on-exception</tt>
command-line option may be used to set <tt>debug-on-exception</tt> to
<tt>#t</tt> from the command line, which is particularly useful when
debugging scripts or top-level programs run via the
<a name="./debug:s18"></a><tt>--script</tt> or
<a name="./debug:s19"></a><tt>--program</tt>
command-line options.
<p>
<h3><a name="g18"></a><a name="./debug:h3"></a>Section 3.3. The Interactive Inspector<a name="SECTDEBUGINSPECTOR"></a></h3>
<p>
The <a name="./debug:s20"></a>inspector may be called directly via the procedure <tt>inspect</tt> or
indirectly from the debugger.
It allows the programmer to examine circular objects, objects such as
ports and procedures that do not have a reader syntax, and objects such
as continuations and variables that are not directly accessible by the
programmer, as well as ordinary printable Scheme objects.
<p>
The primary intent of the inspector is examination, not alteration, of
objects.
The values of <a name="./debug:s21"></a>assignable variables may be changed from within the
inspector, however.
Assignable variables are generally limited to those for which
assignments occur in the source program.
It is also possible to invoke arbitrary procedures
(including mutation procedures such as <tt>set-car!</tt>) on an object.
No mechanism is provided for altering objects that are inherently
immutable, e.g., nonassignable variables, procedures, and bignums, since
doing so can violate assumptions made by the compiler and run-time
system.
<p>
The user is presented with a prompt line that includes a printed
representation of the current object, abbreviated if necessary to
fit on the line.
Various commands are provided for displaying objects and moving around
inside of objects.
On-line descriptions of the command options are provided.
The command "?" displays commands that apply specifically to the
current object.
The command "??" displays commands that are always applicable.
The command "h" provides a brief description of how to use the
inspector.
The end-of-file character or the command "q" exits the inspector.
<p>
<a name="./debug:s22"></a><span class=formdef><b>procedure</b>: <tt>(inspect <i>obj</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>Invokes the inspector on <tt><i>obj</i></tt>, as described above.
The commands recognized by the inspector are listed below, categorized
by the type of the current object.
<p>
<p>
<p><b>Generally applicable commands</b><p><p><tt>help</tt> or <tt>h</tt> displays a brief description of how to use the
inspector.
<p>
<p><tt>?</tt> displays commands applicable to the current type of
object.
<p>
<p><tt>??</tt> displays the generally applicable commands.
<p>
<p><tt>print</tt> or <tt>p</tt> prints the current object (using <tt>pretty-print</tt>).
<p>
<p><tt>write</tt> or <tt>w</tt> writes the current object (using <tt>write</tt>).
<p>
<p><tt>size</tt> writes the size in bytes occupied by the current object
(determined via <a name="./debug:s23"></a><tt>compute-size</tt>),
including any objects accessible from the current object except those
for which the size was previously requested during the same interactive
inspector session.
<p>
<p><tt>find</tt> <tt><i>expr</i></tt> [ <tt><i>g</i></tt> ] evaluates <tt><i>expr</i></tt>, which should evaluate
to a procedure of one argument, and searches
(via <a name="./debug:s24"></a><tt>make-object-finder</tt>)
for the first occurrence
of an object within the current object for which the predicate returns
a true value, treating immediate values (e.g., fixnums), values in
generations older than <tt><i>g</i></tt>, and values already visited during the
search as leaves.
If <tt><i>g</i></tt> is not unspecified, it defaults to the current maximum
generation, i.e., the value of <tt>collect-maximum-generation</tt>.
If specified, <tt><i>g</i></tt> must be an exact nonnegative integer less than or
equal to the current maximum generation or the symbol <tt>static</tt>
representing the static generation.
If such an object is found, the inspector's focus moves to that object
as if through a series of steps that lead from the current object to the
located object, so that the <tt>up</tt> command can be used to determine
where the object was found relative to the original object.
<p>
<p><tt>find-next</tt> repeats the last <tt>find</tt>, locating an
occurrence not previously found, if any.
<p>
<p><tt>up</tt> or <tt>u</tt> <tt><i>n</i></tt> returns to the <tt><i>nth</i></tt> previous level.
Used to move outwards in the structure of the inspected object.
<tt><i>n</i></tt> defaults to 1.
<p>
<p><tt>top</tt> or <tt>t</tt> returns to the outermost level of the inspected
object.
<p>
<p><tt>forward</tt> or <tt>f</tt> moves to the <tt><i>nth</i></tt> next expression.
Used to move from one element to another of an object containing
a sequence of elements, such as a list, vector, record, frame, or closure.
<tt><i>n</i></tt> defaults to 1.
<p>
<p><tt>back</tt> or <tt>b</tt> moves to the <tt><i>nth</i></tt> previous expression.
Used to move from one element to another of an object containing
a sequence of elements, such as a list, vector, record, frame, or closure.
<tt><i>n</i></tt> defaults to 1.
<p>
<p><tt>=></tt> <tt><i>expr</i></tt> sends the current object to the procedure value
of <tt><i>expr</i></tt>.
<tt><i>expr</i></tt> may begin on the current or following line and may
span multiple lines.
<p>
<p><tt>file</tt> <tt><i>path</i></tt> opens the source file at the specified path for
listing.
The parameter <tt>source-directories</tt> (Section <a href="./system.html#g112">12.5</a>)
determines the set of directories
searched for source files.
<p>
<p><tt>list</tt> <tt><i>line</i></tt> <tt><i>count</i></tt> lists <tt><i>count</i></tt> lines of the
current source file (see <tt>file</tt>) starting at <tt><i>line</i></tt>.
<tt><i>line</i></tt> defaults to the end of the previous set of lines listed and
<tt><i>count</i></tt> defaults to ten or the number of lines previously listed.
If <tt><i>line</i></tt> is negative, listing begins <tt><i>line</i></tt> lines before the
previous set of lines listed.
<p>
<p><tt>files</tt> shows the currently open source files.
<p>
<p><tt>mark</tt> or <tt>m</tt> <tt><i>m</i></tt> marks the current location with the
symbolic mark <tt><i>m</i></tt>.
If <tt><i>m</i></tt> is not specified, the current location is marked with
a unique default mark.
<p>
<p><tt>goto</tt> or <tt>g</tt> <tt><i>m</i></tt> returns to the location marked <tt><i>m</i></tt>.
If <tt><i>m</i></tt> is not specified, the inspector returns to the location
marked with the default mark.
<p>
<p><tt>new-cafe</tt> or <tt>n</tt> enters a new read-eval-print loop
(café), giving access to the normal top-level environment.
<p>
<p><tt>quit</tt> or <tt>q</tt> exits from the inspector.
<p>
<p><tt>reset</tt> or <tt>r</tt> resets to the current café.
<p>
<p><tt>abort</tt> or <tt>a</tt> <tt><i>x</i></tt> aborts from Scheme with exit
status <tt><i>x</i></tt>, which defaults to -1.
<p>
<p><b>Continuation commands</b><p><p><tt>show-frames</tt> or <tt>sf</tt> shows the next <tt><i>n</i></tt> frames.
If <tt><i>n</i></tt> is not specified, all frames are displayed.
<p>
<p><tt>depth</tt> displays the number of frames in the continuation.
<p>
<p><tt>down</tt> or <tt>d</tt> <tt><i>n</i></tt> move to the <tt><i>nth</i></tt> frame down in the
continuation.
<tt><i>n</i></tt> defaults to 1.
<p>
<p><tt>show</tt> or <tt>s</tt> shows the continuation (next frame) and,
if available, the calling procedure source, the pending call source,
the closure, and the frame and free-variable values.
Source is available only if generation of inspector information
was enabled during compilation of the corresponding lambda
expression.
<p>
<p><tt>show-local</tt> or <tt>sl</tt> is like <tt>show</tt> or <tt>s</tt>
except that free variable values are not shown. (If present, free variable
values can be found by inspecting the closure.)
<p>
<p><tt>length</tt> or <tt>l</tt> displays the number of elements
in the topmost frame of the continuation.
<p>
<p><tt>ref</tt> or <tt>r</tt> moves to the <tt><i>nth</i></tt> or named
frame element. <tt><i>n</i></tt> defaults to 0.
If multiple elements have the same name, only one is
accessible by name, and the others must be accessed by number.
<p>
<p><tt>code</tt> or <tt>c</tt> moves to the source for the calling procedure.
<p>
<p><tt>call</tt> moves to the source for the pending call.
<p>
<p><tt>file</tt> opens the source file containing the pending call,
if known.
The parameter <tt>source-directories</tt> (Section <a href="./system.html#g112">12.5</a>)
determines the list of source directories searched for source files
identified by relative path names.
<p>
For absolute pathnames starting with a <tt>/</tt> (or <tt>\</tt> or a
directory specifier under Windows), the inspector tries the absolute
pathname first, then looks for the last (filename) component of the path in
the list of source directories.
For pathnames starting with <tt>./</tt> (or <tt>.\</tt> under Windows)
or <tt>../</tt> (or <tt>..\</tt> under Windows), the inspector looks in
<tt>"."</tt> or <tt>".."</tt> first, as appropriate, then for the entire
<tt>.</tt>- or <tt>..</tt>-prefixed
pathname in the source directories, then for the last (filename)
component in the source directories.
For other (relative) pathnames, the inspector looks for the entire
relative pathname in the list of source directories, then the last
(filename) component in the list of source directories.
<p>
If a file by the same name as but different contents from the original
source file is found during this process, it will be skipped over.
This typically happens because the file has been modified since it was
compiled.
Pass an explicit filename argument to force opening of a particular file
(see the generally applicable commands above).
<p>
<p><tt>eval</tt> or <tt>e</tt> <tt><i>expr</i></tt> evaluates the expression
<tt><i>expr</i></tt> in an environment containing bindings for the elements of
the frame. Within the evaluated expression, the value of each frame
element <tt><i>n</i></tt> is accessible via the variable <tt>%<i>n</i></tt>.
Named elements are accessible via their names as well. Names are
available only if generation of inspector information was enabled
during compilation of the corresponding lambda expression.
<p>
<p><tt>set!</tt> or <tt>!</tt> <tt><i>n</i></tt> <tt><i>e</i></tt> sets the value of the <tt><i>nth</i></tt> frame
element to <tt><i>e</i></tt>, if the frame element corresponds to
an assignable variable.
<tt><i>n</i></tt> defaults to 0.
<p>
<p><b>Procedure commands</b><p><p><tt>show</tt> or <tt>s</tt> shows the source and free variables of the
procedure.
Source is available only if generation of inspector information
was enabled during compilation of the corresponding lambda
expression.
<p>
<p><tt>code</tt> or <tt>c</tt> moves to the source for the procedure.
<p>
<p><tt>file</tt> opens the file containing the procedure's source code,
if known.
See the description of the continuation <tt>file</tt> entry above for more
information.
<p>
<p><tt>length</tt> or <tt>l</tt> displays the number of free variables
whose values are recorded in the procedure object.
<p>
<p><tt>ref</tt> or <tt>r</tt> moves to the <tt><i>nth</i></tt> or named
free variable. <tt><i>n</i></tt> defaults to 0.
If multiple free variables have the same name, only one is
accessible by name, and the others must be accessed by number.