This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

handling errors in callouts from C



i'm having trouble trapping errors and displaying a backtrace during a
callout from a c program.  i have some code kindly provided by marious
vollmer that seems to work, but sometimes my program dies with a
SIGSEGV during printing of the backtrace.

here is the function i am using:

(define (call-with-error-catching thunk)
  (let ((the-last-stack #f)
	(stack-saved? #f))
    
    (define (handle-error key args)
      (let ((cep (current-error-port)))
	(if the-last-stack
	    (display-backtrace the-last-stack cep) ; barfs here sometimes
	    (display "no backtrace available.\n" cep))
	(apply display-error the-last-stack cep args)
	(force-output cep)
	;(throw 'abort key)
	))

    (define (save-stack)
      (cond (stack-saved?)
	    ((not (memq 'debug (debug-options-interface)))
	     (set! the-last-stack #f)
	     (set! stack-saved? #t))
	    (else
	     (set! the-last-stack (make-stack #t lazy-dispatch 4))
	     (set! stack-saved? #t))))

    (define (lazy-dispatch key . args)
      (save-stack)
      (apply throw key args))

    (start-stack #t
		 (catch #t
			(lambda ()
			  (lazy-catch #t
				      thunk
				      lazy-dispatch))
			(lambda (key . args)
			  (if (= (length args) 4)
			      (handle-error key args)
			      (apply throw key args)
			      ))))))

i had some trouble understanding the implementation of goodies like
lazy-catch, so i'm not quite sure what the above code is doing.  anyone
have the inclination to share a high level explanation?

i am wondering whether the above code could be failing because of a
lack of rigorous use of SCM_DEFER_INTS and SCM_ALLOW_INTS.  a simpler
implementation of call-with-error-catching that doesn't do any funny
business with stacks works fine; the program never SEGV's.

i really like getting the backtraces when there are errors.  any
strategies to track this one down?

kind regards,
-russ