是否有可能在 Chez-Scheme 的调试模式下获得当前的 raise continuation?

Is it possible to get the current raise continuation in debug mode in Chez-Scheme?

我有以下程序:

debugging.scm

(define (fn1 x)
    (printf "fn1/ x=~a\n" x)
    (fn2 x)
    (printf "fn1/ end\n"))

(define (fn2 x)
    (printf "fn2/ x=~a\n" x)
    (fn3 x)
    (printf "fn2/ end\n"))

(define (fn3 x)
    (printf "fn3/ x=~a\n" x)
    (cdr x)
    (printf "fn3/ end\n"))

我在 REPL 中 运行 它。它按预期崩溃了。

然后我进入调试模式并检查了 raise continuation。

$ scheme
Chez Scheme Version 9.5.2
Copyright 1984-2019 Cisco Systems, Inc.

> (load "debugging.scm")
> (fn1 100)
fn1/ x=100
fn2/ x=100
fn3/ x=100
Exception in cdr: 100 is not a pair
Type (debug) to enter the debugger.
> (debug)
debug> ?
Type i  to inspect the raise continuation (if available)
     s  to display the condition
     c  to inspect the condition
     e  or eof to exit the debugger, retaining error continuation
     q  to exit the debugger, discarding error continuation
debug> i
#<continuation in fn2>                                            : ?

   length(l) ........... display number of frame and closure variables
   depth ............... display number of frames in continuation stack
   ref(r) .............. inspect [named or nth] variable
   set!(!) ............. set [named or nth] variable to value, if assignable
   forward(f) .......... move to [nth] next frame
   back(b) ............. move to [nth] previous frame
   down(d) ............. inspect [nth] next frame
   closure(cp) ......... inspect the frame's closure, if any
   eval(e) ............. evaluate expression in context of current frame
   show(s) ............. show frame with free variables
   show-local(sl) ...... show frame without free variables
   show-frames(sf) ..... show the next [n] frames
   call ................ inspect the code for the pending call
   code(c) ............. inspect the code for the pending procedure
   file ................ switch to source file containing the pending call
   ?? .................. display more options

#<continuation in fn2>                                            : sf
  0: #<continuation in fn2>
  1: #<continuation in fn1>
  2: #<system continuation in new-cafe>
#<continuation in fn2>                                            : e
_ ????

我的问题是,是否可以在调试模式下获取当前的 raise continuation?

我想通过评估表达式来检查它,比如

(inspect/object current-raise-continuation)

谢谢。

您可以在调试器中向延续发送消息,如下所示:

$ scheme
Chez Scheme Version 9.5.2
Copyright 1984-2019 Cisco Systems, Inc.

> (load "debugging.scm")
> (fn1 100)
fn1/ x=100
fn2/ x=100
fn3/ x=100
Exception in cdr: 100 is not a pair
Type (debug) to enter the debugger.
> (debug)
debug> i
#<continuation in fn2>                                            : => (lambda (x) ((inspect/object x) 'source-path))
"debugging.scm"
8
5
#<continuation in fn2>                                            : 

或者您可以将此添加到 debugging.scm:

(define (message-continuation-on-exception thunk symbol)
  ;; (note: no error checks, may be Chez version dependent?)
  (guard (condition
           [else (call-with-values (lambda ()
                      (((inspect/object
                        (list-ref (simple-conditions condition) 5))
                          'ref 'k) symbol))
                    (lambda vs (for-each display `("Exception, " ,symbol ": " ,vs))))])
         (thunk)))

然后像这样使用它:

$ scheme
Chez Scheme Version 9.5.2
Copyright 1984-2019 Cisco Systems, Inc.

> (load "debugging.scm")
> (message-continuation-on-exception
    (lambda () (fn1 100)) 'source-path)
fn1/ x=100
fn2/ x=100
fn3/ x=100
Exception, source-path: (debugging.scm 8 5)
>