error-handling.scm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ;;; Error handling in the REPL
  2. ;; Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system repl error-handling)
  19. #:use-module (system vm trap-state)
  20. #:use-module (system repl debug)
  21. #:use-module (ice-9 format)
  22. #:export (call-with-error-handling
  23. with-error-handling))
  24. ;;;
  25. ;;; Error handling via repl debugging
  26. ;;;
  27. (define (error-string stack key args)
  28. (call-with-output-string
  29. (lambda (port)
  30. (let ((frame (and (< 0 (vector-length stack)) (vector-ref stack 0))))
  31. (print-exception port frame key args)))))
  32. (define* (call-with-error-handling thunk #:key
  33. (on-error 'debug) (post-error 'catch)
  34. (pass-keys '(quit)) (trap-handler 'debug)
  35. (report-keys '(stack-overflow out-of-memory)))
  36. (let ((in (current-input-port))
  37. (out (current-output-port))
  38. (err (current-error-port)))
  39. (define (with-saved-ports thunk)
  40. (with-input-from-port in
  41. (lambda ()
  42. (with-output-to-port out
  43. (lambda ()
  44. (with-error-to-port err
  45. thunk))))))
  46. (define (debug-trap-handler frame trap-idx trap-name)
  47. (let* ((tag (and (pair? (fluid-ref %stacks))
  48. (cdr (fluid-ref %stacks))))
  49. (stack (narrow-stack->vector
  50. (make-stack frame)
  51. ;; Take the stack from the given frame, cutting 0
  52. ;; frames.
  53. 0
  54. ;; Narrow the end of the stack to the most recent
  55. ;; start-stack.
  56. tag
  57. ;; And one more frame, because %start-stack
  58. ;; invoking the start-stack thunk has its own frame
  59. ;; too.
  60. 0 (and tag 1)))
  61. (error-msg (if trap-idx
  62. (format #f "Trap ~d: ~a" trap-idx trap-name)
  63. trap-name))
  64. (debug (make-debug stack 0 error-msg)))
  65. (with-saved-ports
  66. (lambda ()
  67. (if trap-idx
  68. (begin
  69. (format #t "~a~%" error-msg)
  70. (format #t "Entering a new prompt. ")
  71. (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")))
  72. ((@ (system repl repl) start-repl) #:debug debug)))))
  73. (define (null-trap-handler frame trap-idx trap-name)
  74. #t)
  75. (define le-trap-handler
  76. (case trap-handler
  77. ((debug) debug-trap-handler)
  78. ((pass) null-trap-handler)
  79. ((disabled) #f)
  80. (else (error "Unknown trap-handler strategy" trap-handler))))
  81. (define (report-error key args)
  82. (with-saved-ports
  83. (lambda ()
  84. (run-hook before-error-hook)
  85. (print-exception err #f key args)
  86. (run-hook after-error-hook)
  87. (force-output err))))
  88. (catch #t
  89. (lambda ()
  90. (with-default-trap-handler le-trap-handler
  91. (lambda () (%start-stack #t thunk))))
  92. (case post-error
  93. ((report)
  94. (lambda (key . args)
  95. (if (memq key pass-keys)
  96. (apply throw key args)
  97. (begin
  98. (report-error key args)
  99. (if #f #f)))))
  100. ((catch)
  101. (lambda (key . args)
  102. (when (memq key pass-keys)
  103. (apply throw key args))
  104. (when (memq key report-keys)
  105. (report-error key args))
  106. (if #f #f)))
  107. (else
  108. (if (procedure? post-error)
  109. (lambda (k . args)
  110. (apply (if (memq k pass-keys) throw post-error) k args))
  111. (error "Unknown post-error strategy" post-error))))
  112. (case on-error
  113. ((debug)
  114. (lambda (key . args)
  115. (if (not (memq key pass-keys))
  116. (let* ((tag (and (pair? (fluid-ref %stacks))
  117. (cdr (fluid-ref %stacks))))
  118. (stack (narrow-stack->vector
  119. (make-stack #t)
  120. ;; Cut three frames from the top of the stack:
  121. ;; make-stack, this one, and the throw handler.
  122. 3
  123. ;; Narrow the end of the stack to the most recent
  124. ;; start-stack.
  125. tag
  126. ;; And one more frame, because %start-stack invoking
  127. ;; the start-stack thunk has its own frame too.
  128. 0 (and tag 1)))
  129. (error-msg (error-string stack key args))
  130. (debug (make-debug stack 0 error-msg)))
  131. (with-saved-ports
  132. (lambda ()
  133. (format #t "~a~%" error-msg)
  134. (format #t "Entering a new prompt. ")
  135. (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")
  136. ((@ (system repl repl) start-repl) #:debug debug)))))))
  137. ((report)
  138. (lambda (key . args)
  139. (unless (memq key pass-keys)
  140. (report-error key args))
  141. (if #f #f)))
  142. ((backtrace)
  143. (lambda (key . args)
  144. (if (not (memq key pass-keys))
  145. (let* ((tag (and (pair? (fluid-ref %stacks))
  146. (cdr (fluid-ref %stacks))))
  147. (frames (narrow-stack->vector
  148. (make-stack #t)
  149. ;; Narrow as above, for the debugging case.
  150. 3 tag 0 (and tag 1))))
  151. (with-saved-ports (lambda () (print-frames frames)))
  152. (report-error key args)
  153. (if #f #f)))))
  154. ((pass)
  155. (lambda (key . args)
  156. ;; fall through to rethrow
  157. #t))
  158. (else
  159. (if (procedure? on-error)
  160. (lambda (k . args)
  161. (apply (if (memq k pass-keys) throw on-error) k args))
  162. (error "Unknown on-error strategy" on-error)))))))
  163. (define-syntax-rule (with-error-handling form)
  164. (call-with-error-handling (lambda () form)))