Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / stackchk.c
1 /* Copyright (C) 1995,1996,1997, 2000, 2001, 2006, 2008, 2010, 2011 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * 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 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "libguile/_scm.h"
26 #include "libguile/ports.h"
27 #include "libguile/root.h"
28 #include "libguile/threads.h"
29 #include "libguile/dynwind.h"
30
31 #include "libguile/stackchk.h"
32 \f
33
34 /* {Stack Checking}
35 */
36
37 int scm_stack_checking_enabled_p;
38
39 SCM_SYMBOL (scm_stack_overflow_key, "stack-overflow");
40
41 static void
42 reset_scm_stack_checking_enabled_p (void *arg)
43 {
44 scm_stack_checking_enabled_p = (int)(scm_t_bits)arg;
45 }
46
47 void
48 scm_report_stack_overflow ()
49 {
50 scm_dynwind_begin (0); /* non-rewindable frame */
51 scm_dynwind_unwind_handler (reset_scm_stack_checking_enabled_p,
52 (void*)(scm_t_bits)scm_stack_checking_enabled_p,
53 SCM_F_WIND_EXPLICITLY);
54
55 scm_stack_checking_enabled_p = 0;
56
57 scm_error (scm_stack_overflow_key,
58 NULL,
59 "Stack overflow",
60 SCM_BOOL_F,
61 SCM_BOOL_F);
62
63 /* not reached */
64 scm_dynwind_end ();
65 }
66
67 long
68 scm_stack_size (SCM_STACKITEM *start)
69 {
70 SCM_STACKITEM stack;
71 #if SCM_STACK_GROWS_UP
72 return &stack - start;
73 #else
74 return start - &stack;
75 #endif /* SCM_STACK_GROWS_UP */
76 }
77
78
79 void
80 scm_stack_report ()
81 {
82 SCM port = scm_current_error_port ();
83 SCM_STACKITEM stack;
84 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
85
86 scm_uintprint ((scm_stack_size (thread->continuation_base)
87 * sizeof (SCM_STACKITEM)),
88 16, port);
89 scm_puts_unlocked (" of stack: 0x", port);
90 scm_uintprint ((scm_t_bits) thread->continuation_base, 16, port);
91 scm_puts_unlocked (" - 0x", port);
92 scm_uintprint ((scm_t_bits) &stack, 16, port);
93 scm_puts_unlocked ("\n", port);
94 }
95
96
97 SCM_DEFINE (scm_sys_get_stack_size, "%get-stack-size", 0, 0, 0,
98 (),
99 "Return the current thread's C stack size (in Scheme objects).")
100 #define FUNC_NAME s_scm_sys_get_stack_size
101 {
102 return scm_from_long (scm_stack_size (SCM_I_CURRENT_THREAD->base));
103 }
104 #undef FUNC_NAME
105
106
107 void
108 scm_init_stackchk ()
109 {
110 #include "libguile/stackchk.x"
111 }
112
113 /*
114 Local Variables:
115 c-file-style: "gnu"
116 End:
117 */