1 ;;; Error handling in the REPL
3 ;; Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 (define-module (system repl error-handling)
23 #:use-module (system base pmatch)
24 #:use-module (system vm trap-state)
25 #:use-module (system repl debug)
26 #:use-module (ice-9 format)
27 #:export (call-with-error-handling
34 ;;; Error handling via repl debugging
37 (define (error-string stack key args)
38 (call-with-output-string
40 (let ((frame (and (< 0 (vector-length stack)) (vector-ref stack 0))))
41 (print-exception port frame key args)))))
43 (define* (call-with-error-handling thunk #:key
44 (on-error 'debug) (post-error 'catch)
45 (pass-keys '(quit)) (trap-handler 'debug)
46 (report-keys '(stack-overflow out-of-memory)))
47 (let ((in (current-input-port))
48 (out (current-output-port))
49 (err (current-error-port)))
50 (define (with-saved-ports thunk)
51 (with-input-from-port in
53 (with-output-to-port out
55 (with-error-to-port err
58 (define (debug-trap-handler frame trap-idx trap-name)
59 (let* ((tag (and (pair? (fluid-ref %stacks))
60 (cdar (fluid-ref %stacks))))
61 (stack (narrow-stack->vector
63 ;; Take the stack from the given frame, cutting 0
66 ;; Narrow the end of the stack to the most recent
69 ;; And one more frame, because %start-stack
70 ;; invoking the start-stack thunk has its own frame
73 (error-msg (if trap-idx
74 (format #f "Trap ~d: ~a" trap-idx trap-name)
76 (debug (make-debug stack 0 error-msg)))
81 (format #t "~a~%" error-msg)
82 (format #t "Entering a new prompt. ")
83 (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")))
84 ((@ (system repl repl) start-repl) #:debug debug)))))
86 (define (null-trap-handler frame trap-idx trap-name)
89 (define le-trap-handler
91 ((debug) debug-trap-handler)
92 ((pass) null-trap-handler)
94 (else (error "Unknown trap-handler strategy" trap-handler))))
96 (define (report-error key args)
99 (run-hook before-error-hook)
100 (print-exception err #f key args)
101 (run-hook after-error-hook)
102 (force-output err))))
106 (with-default-trap-handler le-trap-handler
107 (lambda () (%start-stack #t thunk))))
112 (if (memq key pass-keys)
113 (apply throw key args)
115 (report-error key args)
119 (when (memq key pass-keys)
120 (apply throw key args))
121 (when (memq key report-keys)
122 (report-error key args))
125 (if (procedure? post-error)
127 (apply (if (memq k pass-keys) throw post-error) k args))
128 (error "Unknown post-error strategy" post-error))))
133 (if (not (memq key pass-keys))
134 (let* ((tag (and (pair? (fluid-ref %stacks))
135 (cdar (fluid-ref %stacks))))
136 (stack (narrow-stack->vector
138 ;; Cut three frames from the top of the stack:
139 ;; make-stack, this one, and the throw handler.
141 ;; Narrow the end of the stack to the most recent
144 ;; And one more frame, because %start-stack invoking
145 ;; the start-stack thunk has its own frame too.
147 (error-msg (error-string stack key args))
148 (debug (make-debug stack 0 error-msg)))
151 (format #t "~a~%" error-msg)
152 (format #t "Entering a new prompt. ")
153 (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")
154 ((@ (system repl repl) start-repl) #:debug debug)))))))
157 (unless (memq key pass-keys)
158 (report-error key args))
162 (if (not (memq key pass-keys))
163 (let* ((tag (and (pair? (fluid-ref %stacks))
164 (cdar (fluid-ref %stacks))))
165 (frames (narrow-stack->vector
167 ;; Narrow as above, for the debugging case.
168 3 tag 0 (and tag 1))))
169 (with-saved-ports (lambda () (print-frames frames)))
170 (report-error key args)
174 ;; fall through to rethrow
177 (if (procedure? on-error)
179 (apply (if (memq k pass-keys) throw on-error) k args))
180 (error "Unknown on-error strategy" on-error)))))))
182 (define-syntax-rule (with-error-handling form)
183 (call-with-error-handling (lambda () form)))