error-handling.scm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 base pmatch)
  20. #:use-module (system vm trap-state)
  21. #:use-module (system repl debug)
  22. #:use-module (ice-9 format)
  23. #:export (call-with-error-handling
  24. with-error-handling))
  25. ;;;
  26. ;;; Error handling via repl debugging
  27. ;;;
  28. (define (error-string stack key args)
  29. (call-with-output-string
  30. (lambda (port)
  31. (let ((frame (and (< 0 (vector-length stack)) (vector-ref stack 0))))
  32. (print-exception port frame key args)))))
  33. (define* (call-with-error-handling thunk #:key
  34. (on-error 'debug) (post-error 'catch)
  35. (pass-keys '(quit)) (trap-handler 'debug)
  36. (report-keys '(stack-overflow out-of-memory)))
  37. (let ((in (current-input-port))
  38. (out (current-output-port))
  39. (err (current-error-port)))
  40. (define (with-saved-ports thunk)
  41. (with-input-from-port in
  42. (lambda ()
  43. (with-output-to-port out
  44. (lambda ()
  45. (with-error-to-port err
  46. thunk))))))
  47. (define (debug-trap-handler frame trap-idx trap-name)
  48. (let* ((tag (and (pair? (fluid-ref %stacks))
  49. (cdr (fluid-ref %stacks))))
  50. (stack (narrow-stack->vector
  51. (make-stack frame)
  52. ;; Take the stack from the given frame, cutting 0
  53. ;; frames.
  54. 0
  55. ;; Narrow the end of the stack to the most recent
  56. ;; start-stack.
  57. tag
  58. ;; And one more frame, because %start-stack
  59. ;; invoking the start-stack thunk has its own frame
  60. ;; too.
  61. 0 (and tag 1)))
  62. (error-msg (if trap-idx
  63. (format #f "Trap ~d: ~a" trap-idx trap-name)
  64. trap-name))
  65. (debug (make-debug stack 0 error-msg)))
  66. (with-saved-ports
  67. (lambda ()
  68. (if trap-idx
  69. (begin
  70. (format #t "~a~%" error-msg)
  71. (format #t "Entering a new prompt. ")
  72. (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")))
  73. ((@ (system repl repl) start-repl) #:debug debug)))))
  74. (define (null-trap-handler frame trap-idx trap-name)
  75. #t)
  76. (define le-trap-handler
  77. (case trap-handler
  78. ((debug) debug-trap-handler)
  79. ((pass) null-trap-handler)
  80. ((disabled) #f)
  81. (else (error "Unknown trap-handler strategy" trap-handler))))
  82. (define (report-error key args)
  83. (with-saved-ports
  84. (lambda ()
  85. (run-hook before-error-hook)
  86. (print-exception err #f key args)
  87. (run-hook after-error-hook)
  88. (force-output err))))
  89. (catch #t
  90. (lambda ()
  91. (with-default-trap-handler le-trap-handler
  92. (lambda () (%start-stack #t thunk))))
  93. (case post-error
  94. ((report)
  95. (lambda (key . args)
  96. (if (memq key pass-keys)
  97. (apply throw key args)
  98. (begin
  99. (report-error key args)
  100. (if #f #f)))))
  101. ((catch)
  102. (lambda (key . args)
  103. (when (memq key pass-keys)
  104. (apply throw key args))
  105. (when (memq key report-keys)
  106. (report-error key args))
  107. (if #f #f)))
  108. (else
  109. (if (procedure? post-error)
  110. (lambda (k . args)
  111. (apply (if (memq k pass-keys) throw post-error) k args))
  112. (error "Unknown post-error strategy" post-error))))
  113. (case on-error
  114. ((debug)
  115. (lambda (key . args)
  116. (if (not (memq key pass-keys))
  117. (let* ((tag (and (pair? (fluid-ref %stacks))
  118. (cdr (fluid-ref %stacks))))
  119. (stack (narrow-stack->vector
  120. (make-stack #t)
  121. ;; Cut three frames from the top of the stack:
  122. ;; make-stack, this one, and the throw handler.
  123. 3
  124. ;; Narrow the end of the stack to the most recent
  125. ;; start-stack.
  126. tag
  127. ;; And one more frame, because %start-stack invoking
  128. ;; the start-stack thunk has its own frame too.
  129. 0 (and tag 1)))
  130. (error-msg (error-string stack key args))
  131. (debug (make-debug stack 0 error-msg)))
  132. (with-saved-ports
  133. (lambda ()
  134. (format #t "~a~%" error-msg)
  135. (format #t "Entering a new prompt. ")
  136. (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")
  137. ((@ (system repl repl) start-repl) #:debug debug)))))))
  138. ((report)
  139. (lambda (key . args)
  140. (unless (memq key pass-keys)
  141. (report-error key args))
  142. (if #f #f)))
  143. ((backtrace)
  144. (lambda (key . args)
  145. (if (not (memq key pass-keys))
  146. (let* ((tag (and (pair? (fluid-ref %stacks))
  147. (cdr (fluid-ref %stacks))))
  148. (frames (narrow-stack->vector
  149. (make-stack #t)
  150. ;; Narrow as above, for the debugging case.
  151. 3 tag 0 (and tag 1))))
  152. (with-saved-ports (lambda () (print-frames frames)))
  153. (report-error key args)
  154. (if #f #f)))))
  155. ((pass)
  156. (lambda (key . args)
  157. ;; fall through to rethrow
  158. #t))
  159. (else
  160. (if (procedure? on-error)
  161. (lambda (k . args)
  162. (apply (if (memq k pass-keys) throw on-error) k args))
  163. (error "Unknown on-error strategy" on-error)))))))
  164. (define-syntax-rule (with-error-handling form)
  165. (call-with-error-handling (lambda () form)))