Merge branch 'ossau-gds-dev'
[bpt/guile.git] / libguile / stackchk.c
1 /* Copyright (C) 1995,1996,1997, 2000, 2001, 2006, 2008 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
30 #include "libguile/stackchk.h"
31 \f
32
33 /* {Stack Checking}
34 */
35
36 #ifdef STACK_CHECKING
37 int scm_stack_checking_enabled_p;
38
39 SCM_SYMBOL (scm_stack_overflow_key, "stack-overflow");
40
41 void
42 scm_report_stack_overflow ()
43 {
44 scm_stack_checking_enabled_p = 0;
45 scm_error (scm_stack_overflow_key,
46 NULL,
47 "Stack overflow",
48 SCM_BOOL_F,
49 SCM_BOOL_F);
50 }
51
52 #endif
53
54 long
55 scm_stack_size (SCM_STACKITEM *start)
56 {
57 SCM_STACKITEM stack;
58 #if SCM_STACK_GROWS_UP
59 return &stack - start;
60 #else
61 return start - &stack;
62 #endif /* SCM_STACK_GROWS_UP */
63 }
64
65
66 void
67 scm_stack_report ()
68 {
69 SCM port = scm_current_error_port ();
70 SCM_STACKITEM stack;
71 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
72
73 scm_uintprint ((scm_stack_size (thread->continuation_base)
74 * sizeof (SCM_STACKITEM)),
75 16, port);
76 scm_puts (" of stack: 0x", port);
77 scm_uintprint ((scm_t_bits) thread->continuation_base, 16, port);
78 scm_puts (" - 0x", port);
79 scm_uintprint ((scm_t_bits) &stack, 16, port);
80 scm_puts ("\n", port);
81 }
82
83
84 SCM_DEFINE (scm_sys_get_stack_size, "%get-stack-size", 0, 0, 0,
85 (),
86 "Return the current thread's C stack size (in Scheme objects).")
87 #define FUNC_NAME s_scm_sys_get_stack_size
88 {
89 return scm_from_long (scm_stack_size (SCM_I_CURRENT_THREAD->base));
90 }
91 #undef FUNC_NAME
92
93
94 void
95 scm_init_stackchk ()
96 {
97 #include "libguile/stackchk.x"
98 }
99
100 /*
101 Local Variables:
102 c-file-style: "gnu"
103 End:
104 */