error-handling.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ;;; Error handling in the REPL
  2. ;; Copyright (C) 2001, 2009, 2010, 2011, 2012 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. (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. (cdar (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 #t)))
  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. (catch #t
  82. (lambda ()
  83. (with-default-trap-handler le-trap-handler
  84. (lambda () (%start-stack #t thunk))))
  85. (case post-error
  86. ((report)
  87. (lambda (key . args)
  88. (if (memq key pass-keys)
  89. (apply throw key args)
  90. (begin
  91. (with-saved-ports
  92. (lambda ()
  93. (run-hook before-error-hook)
  94. (print-exception err #f key args)
  95. (run-hook after-error-hook)
  96. (force-output err)))
  97. (if #f #f)))))
  98. ((catch)
  99. (lambda (key . args)
  100. (if (memq key pass-keys)
  101. (apply throw key args))))
  102. (else
  103. (if (procedure? post-error)
  104. (lambda (k . args)
  105. (apply (if (memq k pass-keys) throw post-error) k args))
  106. (error "Unknown post-error strategy" post-error))))
  107. (case on-error
  108. ((debug)
  109. (lambda (key . args)
  110. (if (not (memq key pass-keys))
  111. (let* ((tag (and (pair? (fluid-ref %stacks))
  112. (cdar (fluid-ref %stacks))))
  113. (stack (narrow-stack->vector
  114. (make-stack #t)
  115. ;; Cut three frames from the top of the stack:
  116. ;; make-stack, this one, and the throw handler.
  117. 3
  118. ;; Narrow the end of the stack to the most recent
  119. ;; start-stack.
  120. tag
  121. ;; And one more frame, because %start-stack invoking
  122. ;; the start-stack thunk has its own frame too.
  123. 0 (and tag 1)))
  124. (error-msg (error-string stack key args))
  125. (debug (make-debug stack 0 error-msg #f)))
  126. (with-saved-ports
  127. (lambda ()
  128. (format #t "~a~%" error-msg)
  129. (format #t "Entering a new prompt. ")
  130. (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")
  131. ((@ (system repl repl) start-repl) #:debug debug)))))))
  132. ((report)
  133. (lambda (key . args)
  134. (if (not (memq key pass-keys))
  135. (begin
  136. (with-saved-ports
  137. (lambda ()
  138. (run-hook before-error-hook)
  139. (print-exception err #f key args)
  140. (run-hook after-error-hook)
  141. (force-output err)))
  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. (cdar (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
  153. (lambda ()
  154. (print-frames frames)
  155. (run-hook before-error-hook)
  156. (print-exception err #f key args)
  157. (run-hook after-error-hook)
  158. (force-output err)))
  159. (if #f #f)))))
  160. ((pass)
  161. (lambda (key . args)
  162. ;; fall through to rethrow
  163. #t))
  164. (else
  165. (if (procedure? on-error)
  166. (lambda (k . args)
  167. (apply (if (memq k pass-keys) throw on-error) k args))
  168. (error "Unknown on-error strategy" on-error)))))))
  169. (define-syntax-rule (with-error-handling form)
  170. (call-with-error-handling (lambda () form)))