guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting scheme error informations when running scheme code from C


From: Alan Grover
Subject: Re: Getting scheme error informations when running scheme code from C
Date: Sat, 10 Sep 2005 19:40:41 -0400
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)


Christian Mauduit wrote:
> ...
> Well, using lazy-catch and a handler with the line:
> 
> (display-backtrace (make-stack #t) some-user-string-output-port)
> 
> actually got me very close from solving my problem completely. The only
> point is that the stack I obtain contains many useless things (such as
> the actual functions I'm using within the error handler, which are
> useless...) so I get a garbaged output. I guess there's some way to get
> rid of this by passing cryptic arguments to make-stack. BTW trying to
> handle the object returned by make-stack and produce a string output "by
> hand" from it sounded awfully hairy to me. Wee.

If we assume that the catch'ing lambda is always the same, then the
stack-frames related to it should be stable. So, you should be able to
discard the bottom n frames every time:

For example:
(lazy-catch #t
        (lambda () (+ 1 (hubert)))
        (lambda (key . args)
        (let ((s (make-stack #t)))
        (display (list key args))
        (newline)
        (display-backtrace s (current-output-port)
                (- (stack-length s) 1)
                (- (stack-length s) 3)))
        (newline)
        (display "throw on") (newline)
        (throw 'bob)))
Always truncates the stack at the "(hubert)" call (i.e. trims off the
lazy-catch). Note the need to subtract 1 from stack-length for the
"first" argument of display-backtrace. The documentation is not clear:
the "first" argument is "how far back" to start (i.e. 1  would mean "1
before bottom"), "depth" is number of frames to display. Since I know I
want to discard the last 3 (by examination), I subtracted 3 from the
stack-length. Capture the stack as the first thing in the catch-lambda,
then the "trim" is stable as you elaborate the body.


Note that changing the debugger option 'frames will possibly change the
depth. Probably some other debug/runtime things will disturb it too.


It does look like you could auto-magically "do the right thing" with
args to make-stack. Then you could use "display-error". I don't know how
to provide the right args to make-stack, however.


I had already decoded the frame for my own purposes (a logging
function), so here's the code above plus it outputs the decoded frame
info (I don't claim this is bullet proof!):
(lazy-catch #t
    (lambda () (+ 1 (hubert)))
    (lambda (key . args)
        (let* (
            (s (make-stack #t))
            (bottom-frames-to-trim 3)
            (frame-of-interest
                (stack-ref s
                    bottom-frames-to-trim))
            (source-me (frame-source frame-of-interest))
            (fname (and source-me (source-property source-me 'filename)))
            (procedure (frame-procedure (frame-of-interest))
            (source-line (and source-me (1+ (source-property source-me
'line))))
            )

        (display (list key args))
        (newline)
        (display-backtrace s (current-output-port)
            (- (stack-length s) 1)
            (- (stack-length s) bottom-frames-to-trim))
    (newline)
    (display ; the decoded frame
        (list
            "file" fname
            "line" source-line
            "argument-of" procedure
            "source" (if (memoized? source-me)
                (unmemoize source-me) source-me)))
    (newline)
    (display "throw on") (newline)
    (throw 'bob))))

Remove the parens from "(hubert)" to see a more interesting
"argument-of" value.


> 
> If I get an elegant solution to my problem, I'll try to package it and
> make a short text on the question, by searching the web I found out that
> I'm not the only one to wish to handle his errors himself, but the
> tutorial does not seem to be written yet 8-)
> 
> Have a nice day and thanks for your help,
> 
> Christian.
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]