1 /* Page fault handling library.
2 Copyright (C) 1998-1999, 2002, 2004-2008 Bruno Haible <bruno@clisp.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 @FAULT_CONTEXT_INCLUDE@
23 /* HAVE_SIGSEGV_RECOVERY
24 is defined if the system supports catching SIGSEGV. */
25 #if @HAVE_SIGSEGV_RECOVERY@
26 # define HAVE_SIGSEGV_RECOVERY 1
29 /* HAVE_STACK_OVERFLOW_RECOVERY
30 is defined if stack overflow can be caught. */
31 #if @HAVE_STACK_OVERFLOW_RECOVERY@
32 # define HAVE_STACK_OVERFLOW_RECOVERY 1
40 #define LIBSIGSEGV_VERSION 0x0206 /* version number: (major<<8) + minor */
41 extern int libsigsegv_version
; /* Likewise */
43 /* -------------------------------------------------------------------------- */
46 * The type of a global SIGSEGV handler.
47 * The fault address is passed as argument.
48 * The access type (read access or write access) is not passed; your handler
49 * has to know itself how to distinguish these two cases.
50 * The second argument is 0, meaning it could also be a stack overflow, or 1,
51 * meaning the handler should seriously try to fix the fault.
52 * The return value should be nonzero if the handler has done its job
53 * and no other handler should be called, or 0 if the handler declines
54 * responsibility for the given address.
56 typedef int (*sigsegv_handler_t
) (void* fault_address
, int serious
);
59 * Installs a global SIGSEGV handler.
60 * This should be called once only, and it ignores any previously installed
62 * Returns 0 on success, or -1 if the system doesn't support catching SIGSEGV.
64 extern int sigsegv_install_handler (sigsegv_handler_t handler
);
67 * Deinstalls the global SIGSEGV handler.
68 * This goes back to the state where no SIGSEGV handler is installed.
70 extern void sigsegv_deinstall_handler (void);
73 * Prepares leaving a SIGSEGV handler (through longjmp or similar means).
75 extern void sigsegv_leave_handler (void);
78 * The type of a context passed to a stack overflow handler.
79 * This type is system dependent; on some platforms it is an 'ucontext_t *',
80 * on some platforms it is a 'struct sigcontext *', on others merely an
83 typedef @FAULT_CONTEXT@
*stackoverflow_context_t
;
86 * The type of a stack overflow handler.
87 * Such a handler should perform a longjmp call in order to reduce the amount
88 * of stack needed. It must not return.
89 * The emergency argument is 0 when the stack could be repared, or 1 if the
90 * application should better save its state and exit now.
92 typedef void (*stackoverflow_handler_t
) (int emergency
, stackoverflow_context_t scp
);
95 * Installs a stack overflow handler.
96 * The extra_stack argument is a pointer to a pre-allocated area used as a
97 * stack for executing the handler. It is typically allocated by use of
98 * `alloca' during `main'. Its size should be sufficiently large.
99 * The following code determines an appropriate size:
100 * #include <signal.h>
101 * #ifndef SIGSTKSZ / * glibc defines SIGSTKSZ for this purpose * /
102 * # define SIGSTKSZ 16384 / * on most platforms, 16 KB are sufficient * /
104 * Returns 0 on success, or -1 if the system doesn't support catching stack
107 extern int stackoverflow_install_handler (stackoverflow_handler_t handler
,
108 void* extra_stack
, unsigned long extra_stack_size
);
111 * Deinstalls the stack overflow handler.
113 extern void stackoverflow_deinstall_handler (void);
115 /* -------------------------------------------------------------------------- */
118 * The following structure and functions permit to define different SIGSEGV
119 * policies on different address ranges.
123 * The type of a local SIGSEGV handler.
124 * The fault address is passed as argument.
125 * The second argument is fixed arbitrary user data.
126 * The return value should be nonzero if the handler has done its job
127 * and no other handler should be called, or 0 if the handler declines
128 * responsibility for the given address.
130 typedef int (*sigsegv_area_handler_t
) (void* fault_address
, void* user_arg
);
133 * This structure represents a table of memory areas (address range intervals),
134 * with an local SIGSEGV handler for each.
137 struct sigsegv_dispatcher
{
143 * Initializes a sigsegv_dispatcher structure.
145 extern void sigsegv_init (sigsegv_dispatcher
* dispatcher
);
148 * Adds a local SIGSEGV handler to a sigsegv_dispatcher structure.
149 * It will cover the interval [address..address+len-1].
150 * Returns a "ticket" that can be used to remove the handler later.
152 extern void* sigsegv_register (sigsegv_dispatcher
* dispatcher
,
153 void* address
, unsigned long len
,
154 sigsegv_area_handler_t handler
, void* handler_arg
);
157 * Removes a local SIGSEGV handler.
159 extern void sigsegv_unregister (sigsegv_dispatcher
* dispatcher
, void* ticket
);
162 * Call the local SIGSEGV handler responsible for the given fault address.
163 * Return the handler's return value. 0 means that no handler has been found,
164 * or that a handler was found but declined responsibility.
166 extern int sigsegv_dispatch (sigsegv_dispatcher
* dispatcher
, void* fault_address
);
168 /* -------------------------------------------------------------------------- */
174 #endif /* _SIGSEGV_H */