schemechez-scheme

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


I have the following program:

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"))

I run it in REPL. It crashed as expected.

Then I entered the debug mode and inspected the 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
_ ????

My question is that is it possible to get the current raise continuation in the debug mode?

I'd like to inspect it by evaluating expression, something like

(inspect/object current-raise-continuation)

Thanks.


Solution

  • You can send a message to the continuation in the debugger, like this:

    $ 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>                                            : 
    

    or you can add this to 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)))
    

    and then use it like:

    $ 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)
    >