unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Alan Grover <awgrover@mail.msen.com>
Subject: Re: Getting scheme error informations when running scheme code from C
Date: Sat, 10 Sep 2005 19:40:41 -0400	[thread overview]
Message-ID: <43236EF9.5050400@mail.msen.com> (raw)
In-Reply-To: <432351D1.8010102@ufoot.org>



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.
> 


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


  reply	other threads:[~2005-09-10 23:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-10 13:22 Getting scheme error informations when running scheme code from C Christian Mauduit
2005-09-10 14:33 ` Alan Grover
2005-09-10 21:36   ` Christian Mauduit
2005-09-10 23:40     ` Alan Grover [this message]
2005-09-11 20:19       ` Christian Mauduit
2005-09-11 21:12         ` Stephen Compall
2005-09-10 23:50     ` Neil Jerram
2005-09-12 12:11       ` Ludovic Courtès
2005-09-12 18:49         ` Neil Jerram

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43236EF9.5050400@mail.msen.com \
    --to=awgrover@mail.msen.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).