Only lazily compile where profitable
[bpt/guile.git] / module / ice-9 / save-stack.scm
CommitLineData
d8158b83
AW
1;;; -*- mode: scheme; coding: utf-8; -*-
2
28969427 3;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2014
d8158b83
AW
4;;;; Free Software Foundation, Inc.
5;;;;
6;;;; This library is free software; you can redistribute it and/or
7;;;; modify it under the terms of the GNU Lesser General Public
8;;;; License as published by the Free Software Foundation; either
9;;;; version 3 of the License, or (at your option) any later version.
10;;;;
11;;;; This library is distributed in the hope that it will be useful,
12;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14;;;; Lesser General Public License for more details.
15;;;;
16;;;; You should have received a copy of the GNU Lesser General Public
17;;;; License along with this library; if not, write to the Free Software
18;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19;;;;
20
21\f
22
23;;; Commentary:
24
25;;; An older approack to debugging, in which the user installs a pre-unwind
26;;; handler that saves the stack at the time of the error. The last stack can
27;;; then be debugged later.
28;;;
29
30;;; Code:
31
32(define-module (ice-9 save-stack)
33 ;; Replace deprecated root-module bindings, if present.
28969427
AW
34 #:export (stack-saved?
35 the-last-stack
36 save-stack))
d8158b83
AW
37
38;; FIXME: stack-saved? is broken in the presence of threads.
39(define stack-saved? #f)
40
ec16eb78
AW
41(define the-last-stack (make-fluid))
42
d8158b83
AW
43(define (save-stack . narrowing)
44 (if (not stack-saved?)
45 (begin
46 (let ((stacks (fluid-ref %stacks)))
47 (fluid-set! the-last-stack
48 ;; (make-stack obj inner outer inner outer ...)
49 ;;
50 ;; In this case, cut away the make-stack frame, the
51 ;; save-stack frame, and then narrow as specified by the
52 ;; user, delimited by the nearest start-stack invocation,
53 ;; if any.
54 (apply make-stack #t
55 2
56 (if (pair? stacks) (cdar stacks) 0)
57 narrowing)))
58 (set! stack-saved? #t))))