|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (system repl error-handling) |
|
#:use-module (system vm trap-state) |
|
#:use-module (system repl debug) |
|
#:use-module (ice-9 format) |
|
#:export (call-with-error-handling |
|
with-error-handling)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (error-string stack key args) |
|
(call-with-output-string |
|
(lambda (port) |
|
(let ((frame (and (< 0 (vector-length stack)) (vector-ref stack 0)))) |
|
(print-exception port frame key args))))) |
|
|
|
(define* (call-with-error-handling thunk #:key |
|
(on-error 'debug) (post-error 'catch) |
|
(pass-keys '(quit)) (trap-handler 'debug) |
|
(report-keys '(stack-overflow out-of-memory))) |
|
(let ((in (current-input-port)) |
|
(out (current-output-port)) |
|
(err (current-error-port))) |
|
(define (with-saved-ports thunk) |
|
(with-input-from-port in |
|
(lambda () |
|
(with-output-to-port out |
|
(lambda () |
|
(with-error-to-port err |
|
thunk)))))) |
|
|
|
(define (debug-trap-handler frame trap-idx trap-name) |
|
(let* ((tag (and (pair? (fluid-ref %stacks)) |
|
(cdr (fluid-ref %stacks)))) |
|
(stack (narrow-stack->vector |
|
(make-stack frame) |
|
|
|
|
|
0 |
|
|
|
|
|
tag |
|
|
|
|
|
|
|
0 (and tag 1))) |
|
(error-msg (if trap-idx |
|
(format #f "Trap ~d: ~a" trap-idx trap-name) |
|
trap-name)) |
|
(debug (make-debug stack 0 error-msg))) |
|
(with-saved-ports |
|
(lambda () |
|
(if trap-idx |
|
(begin |
|
(format #t "~a~%" error-msg) |
|
(format #t "Entering a new prompt. ") |
|
(format #t "Type `,bt' for a backtrace or `,q' to continue.\n"))) |
|
((@ (system repl repl) start-repl) #:debug debug))))) |
|
|
|
(define (null-trap-handler frame trap-idx trap-name) |
|
#t) |
|
|
|
(define le-trap-handler |
|
(case trap-handler |
|
((debug) debug-trap-handler) |
|
((pass) null-trap-handler) |
|
((disabled) #f) |
|
(else (error "Unknown trap-handler strategy" trap-handler)))) |
|
|
|
(define (report-error key args) |
|
(with-saved-ports |
|
(lambda () |
|
(run-hook before-error-hook) |
|
(print-exception err #f key args) |
|
(run-hook after-error-hook) |
|
(force-output err)))) |
|
|
|
(catch #t |
|
(lambda () |
|
(with-default-trap-handler le-trap-handler |
|
(lambda () (%start-stack #t thunk)))) |
|
|
|
(case post-error |
|
((report) |
|
(lambda (key . args) |
|
(if (memq key pass-keys) |
|
(apply throw key args) |
|
(begin |
|
(report-error key args) |
|
(if #f #f))))) |
|
((catch) |
|
(lambda (key . args) |
|
(when (memq key pass-keys) |
|
(apply throw key args)) |
|
(when (memq key report-keys) |
|
(report-error key args)) |
|
(if #f #f))) |
|
(else |
|
(if (procedure? post-error) |
|
(lambda (k . args) |
|
(apply (if (memq k pass-keys) throw post-error) k args)) |
|
(error "Unknown post-error strategy" post-error)))) |
|
|
|
(case on-error |
|
((debug) |
|
(lambda (key . args) |
|
(if (not (memq key pass-keys)) |
|
(let* ((tag (and (pair? (fluid-ref %stacks)) |
|
(cdr (fluid-ref %stacks)))) |
|
(stack (narrow-stack->vector |
|
(make-stack #t) |
|
|
|
|
|
3 |
|
|
|
|
|
tag |
|
|
|
|
|
0 (and tag 1))) |
|
(error-msg (error-string stack key args)) |
|
(debug (make-debug stack 0 error-msg))) |
|
(with-saved-ports |
|
(lambda () |
|
(format #t "~a~%" error-msg) |
|
(format #t "Entering a new prompt. ") |
|
(format #t "Type `,bt' for a backtrace or `,q' to continue.\n") |
|
((@ (system repl repl) start-repl) #:debug debug))))))) |
|
((report) |
|
(lambda (key . args) |
|
(unless (memq key pass-keys) |
|
(report-error key args)) |
|
(if #f #f))) |
|
((backtrace) |
|
(lambda (key . args) |
|
(if (not (memq key pass-keys)) |
|
(let* ((tag (and (pair? (fluid-ref %stacks)) |
|
(cdr (fluid-ref %stacks)))) |
|
(frames (narrow-stack->vector |
|
(make-stack #t) |
|
|
|
3 tag 0 (and tag 1)))) |
|
(with-saved-ports (lambda () (print-frames frames))) |
|
(report-error key args) |
|
(if #f #f))))) |
|
((pass) |
|
(lambda (key . args) |
|
|
|
#t)) |
|
(else |
|
(if (procedure? on-error) |
|
(lambda (k . args) |
|
(apply (if (memq k pass-keys) throw on-error) k args)) |
|
(error "Unknown on-error strategy" on-error))))))) |
|
|
|
(define-syntax-rule (with-error-handling form) |
|
(call-with-error-handling (lambda () form))) |
|
|