save-stack.scm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2014
  3. ;;;; Free Software Foundation, Inc.
  4. ;;;;
  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.
  9. ;;;;
  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.
  14. ;;;;
  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 02110-1301 USA
  18. ;;;;
  19. ;;; Commentary:
  20. ;;; An older approack to debugging, in which the user installs a pre-unwind
  21. ;;; handler that saves the stack at the time of the error. The last stack can
  22. ;;; then be debugged later.
  23. ;;;
  24. ;;; Code:
  25. (define-module (ice-9 save-stack)
  26. ;; Replace deprecated root-module bindings, if present.
  27. #:export (stack-saved?
  28. the-last-stack
  29. save-stack))
  30. ;; FIXME: stack-saved? is broken in the presence of threads.
  31. (define stack-saved? #f)
  32. (define the-last-stack (make-fluid))
  33. (define (save-stack . narrowing)
  34. (if (not stack-saved?)
  35. (begin
  36. (let ((stacks (fluid-ref %stacks)))
  37. (fluid-set! the-last-stack
  38. ;; (make-stack obj inner outer inner outer ...)
  39. ;;
  40. ;; In this case, cut away the make-stack frame, the
  41. ;; save-stack frame, and then narrow as specified by the
  42. ;; user, delimited by the nearest start-stack invocation,
  43. ;; if any.
  44. (apply make-stack #t
  45. 2
  46. (if (pair? stacks) (cdr stacks) 0)
  47. narrowing)))
  48. (set! stack-saved? #t))))