guile-user
[Top][All Lists]
Advanced

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

Re: Displaying a backtrace?


From: Martin Grabmueller
Subject: Re: Displaying a backtrace?
Date: Thu, 25 Jan 2001 23:43:17 +0100

> From: address@hidden (Bill Gribble)
> Date: Thu, 25 Jan 2001 09:13:24 -0600
> 
> For debugging purposes I want to be able to print a backtrace in a
> (catch) handler while continuing to run... basically
> false-if-exception except printing a backtrace in addition to
> returning false.
> 
> However, I can't ascertain how to print out a backtrace from within my
> catch handler.  (backtrace) displays the message "No backtrace
> available", even if I have explicitly done (debug-enable 'backtrace).
> (display-backtrace (fluid-ref the-last-stack) (current-output-port))
> prints nothing.  I'm just guessing that the-last-stack is the stack I
> want to print.
> 
> Any thoughts? 

I don't claim that I really have understood the whole stack thing, but
I have played around a bit (so please correct me if I am wrong here).

After defining a macro like this

(define-macro (false-if-exception-dump-backtrace expr)
  `(catch #t
          (lambda () ,expr)
          (lambda ignore
            (backtrace)
            #f)))

the following code _did_ print a backtrace, and returned #f.

(define (foo)
  (write-line (false-if-exception-dump-backtrace
               (begin (error 's) "braz"))))
(foo)

but the following did not:

(define (foo)
  (write-line (false-if-exception-dump-backtrace
               (begin extremely-unknown "braz"))))
(foo)

The reason is that `error' calls `save-stack' before signalling the
error, and that is the stack which can be printed.  Changing the above
code accordingly gives

(define (foo)
  (write-line (false-if-exception-dump-backtrace
               (begin (save-stack) extremely-unknown "braz"))))
(foo)

which will print a backtrace and return #f.

Changing the macro from the beginning to

(define-macro (false-if-exception-dump-backtrace expr)
  `(catch #t
          (lambda () (save-stack) ,expr)
          (lambda ignore
            (backtrace)
            #f)))

will do the stack saving and printing, but I don't know if that is
what you want, because it will only print the stack up to the call to
`save-stack'; e.g. no frames of the evaluation of `expr' will be
printed.

Hope this will be helpful anyway,
  'Martin
-- 
Martin Grabmueller              address@hidden
http://www.pintus.de/mgrabmue/  address@hidden on EFnet



reply via email to

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