I am debugging the following program in Chez Scheme.
(define (proc3 x3)
(printf "proc3 ~a\n" (+ 1 x3)))
(define (proc2 x2)
(printf "proc2 ~a\n" x2)
(proc3 x2))
(define (proc1 x1)
(printf "proc1 ~a\n" x1)
(proc2 x1))
(proc1 'a)
Run this program will raise a error.
(debug)
this program results the following information:
$ scheme
Chez Scheme Version 9.5
Copyright 1984-2017 Cisco Systems, Inc.
> (load "debugging.scm")
proc1 a
proc2 a
Exception in +: a is not a number
Type (debug) to enter the debugger.
> (debug)
debug> i
#<continuation in proc3> : sf
0: #<continuation in proc3>
1: #<system continuation in ksrc>
2: #<system continuation>
3: #<system continuation in dynamic-wind>
4: #<system continuation in dynamic-wind>
5: #<system continuation in $reset-protect>
6: #<system continuation in new-cafe>
#<continuation in proc3> :
Notice that the stack frames only contains the last frame proc3
, but I'd like to show proc2
and proc1
. This seems to be due to Scheme's TCO (tail call optimization) prevents the debugger from inspecting the precise call stack frames.
If I put the call (proc3 x2)
and (proc2 x1)
not at the tail position, the debugger can print the precise call stack frames normally.
(define (proc3 x3)
(printf "proc3 ~a\n" (+ 1 x3)))
(define (proc2 x2)
(printf "proc2 ~a\n" x2)
(proc3 x2)
(printf "proc2 leave\n"))
(define (proc1 x1)
(printf "proc1 ~a\n" x1)
(proc2 x1)
(printf "proc1 leave\n"))
(proc1 'a)
$ scheme
Chez Scheme Version 9.5
Copyright 1984-2017 Cisco Systems, Inc.
> (load "debugging.scm")
proc1 a
proc2 a
Exception in +: a is not a number
Type (debug) to enter the debugger.
> (debug)
debug> i
#<continuation in proc3> : sf
0: #<continuation in proc3>
1: #<continuation in proc2>
2: #<continuation in proc1>
3: #<system continuation in ksrc>
4: #<system continuation>
5: #<system continuation in dynamic-wind>
6: #<system continuation in dynamic-wind>
7: #<system continuation in $reset-protect>
8: #<system continuation in new-cafe>
#<continuation in proc3> :
Is there way to turn off the TCO temporarily in Chez Scheme? (Just for debugging)
Thanks.
This is for sure not possible, as scheme specification imposes tail recursive calls to become iterative processes.
In the implementation, there is an APPLY return code
that drops the stack frames of the iterative processes made via tail recursion -- the interpreter does SCODE
check for "NULL == CDR(SCODE)"
as a special condition for returning code of type APPLY
.
If you want to happen as you with, you can modify this condition in the eval and recompile the scheme -- which will not be scheme any more.
If you want to debug, other methods can be applied.