1 /* src/vm/signal.c - machine independent signal functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: signal.c 7918 2007-05-20 20:42:18Z michi $
39 #if defined(__DARWIN__)
40 /* If we compile with -ansi on darwin, <sys/types.h> is not
41 included. So let's do it here. */
42 # include <sys/types.h>
49 #include "mm/memory.h"
51 #if defined(ENABLE_THREADS)
52 # include "threads/threads-common.h"
55 #include "vm/exceptions.h"
56 #include "vm/signallocal.h"
59 #include "vmcore/options.h"
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
66 /* function prototypes ********************************************************/
68 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
71 /* signal_init *****************************************************************
73 Initializes the signal subsystem and installs the signal handler.
75 *******************************************************************************/
77 void signal_init(void)
79 #if !defined(__CYGWIN__)
84 /* mmap a memory page at address 0x0, so our hardware-exceptions
87 pagesize = getpagesize();
89 (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
91 /* check if we get into trouble with our hardware-exceptions */
93 assert(OFFSET(java_bytearray, data) > EXCEPTION_HARDWARE_PATCHER);
95 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
96 <ctrl>-\). We enable them later in signal_thread, but only for
99 if (sigemptyset(&mask) != 0)
100 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
102 if (sigaddset(&mask, SIGINT) != 0)
103 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
105 #if !defined(__FREEBSD__)
106 if (sigaddset(&mask, SIGQUIT) != 0)
107 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
110 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
111 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
113 #if defined(ENABLE_GC_BOEHM)
114 /* Allocate something so the garbage collector's signal handlers
120 /* Install signal handlers for signals we want to catch in all
123 sigemptyset(&act.sa_mask);
125 #if defined(ENABLE_JIT)
126 # if defined(ENABLE_INTRP)
129 /* SIGSEGV handler */
131 act.sa_sigaction = md_signal_handler_sigsegv;
132 act.sa_flags = SA_NODEFER | SA_SIGINFO;
134 # if defined(SIGSEGV)
135 sigaction(SIGSEGV, &act, NULL);
139 sigaction(SIGBUS, &act, NULL);
142 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
145 act.sa_sigaction = md_signal_handler_sigfpe;
146 act.sa_flags = SA_NODEFER | SA_SIGINFO;
147 sigaction(SIGFPE, &act, NULL);
150 # if defined(__ARM__)
151 /* XXX use better defines for that (in arch.h) */
154 act.sa_sigaction = md_signal_handler_sigill;
155 act.sa_flags = SA_NODEFER | SA_SIGINFO;
156 sigaction(SIGILL, &act, NULL);
159 # if defined(__POWERPC__)
160 /* XXX use better defines for that (in arch.h) */
161 /* SIGTRAP handler */
163 act.sa_sigaction = md_signal_handler_sigtrap;
164 act.sa_flags = SA_NODEFER | SA_SIGINFO;
165 sigaction(SIGTRAP, &act, NULL);
167 # if defined(ENABLE_INTRP)
170 #endif /* !defined(ENABLE_INTRP) */
172 #if defined(ENABLE_THREADS)
173 /* SIGHUP handler for threads_thread_interrupt */
175 act.sa_sigaction = signal_handler_sighup;
177 sigaction(SIGHUP, &act, NULL);
180 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
181 /* SIGUSR1 handler for the exact GC to suspend threads */
183 act.sa_sigaction = md_signal_handler_sigusr1;
184 act.sa_flags = SA_SIGINFO;
185 sigaction(SIGUSR1, &act, NULL);
188 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
189 /* SIGUSR2 handler for profiling sampling */
191 act.sa_sigaction = md_signal_handler_sigusr2;
192 act.sa_flags = SA_SIGINFO;
193 sigaction(SIGUSR2, &act, NULL);
196 #endif /* !defined(__CYGWIN__) */
200 /* signal_thread ************************************************************
202 This thread sets the signal mask to catch the user input signals
203 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
204 signals on every single thread running. Especially, this makes
205 problems on slow machines.
207 *******************************************************************************/
209 static void signal_thread(void)
214 if (sigemptyset(&mask) != 0)
215 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
217 if (sigaddset(&mask, SIGINT) != 0)
218 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
220 #if !defined(__FREEBSD__)
221 if (sigaddset(&mask, SIGQUIT) != 0)
222 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
226 /* just wait for a signal */
228 /* XXX We don't check for an error here, although the man-page
229 states sigwait does not return an error (which is wrong!),
230 but it seems to make problems with Boehm-GC. We should
231 revisit this code with our new exact-GC. */
233 /* if (sigwait(&mask, &sig) != 0) */
234 /* vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
235 (void) sigwait(&mask, &sig);
239 /* exit the vm properly */
245 /* print a thread dump */
246 #if defined(ENABLE_THREADS)
250 #if defined(ENABLE_STATISTICS)
252 statistics_print_memory_usage();
258 /* this should not happen */
260 vm_abort("signal_thread: this thread should not exit!");
264 /* signal_start_thread *********************************************************
266 Starts the signal handler thread.
268 *******************************************************************************/
270 bool signal_start_thread(void)
272 #if defined(ENABLE_THREADS)
275 name = utf_new_char("Signal Handler");
277 if (!threads_thread_start_internal(name, signal_thread))
280 /* everything's ok */
289 /* signal_handler_sighup *******************************************************
291 This handler is required by threads_thread_interrupt and does
294 *******************************************************************************/
296 #if defined(ENABLE_THREADS)
297 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
305 * These are local overrides for various environment variables in Emacs.
306 * Please do not remove this and leave it at the end of the file, where
307 * Emacs will automagically detect them.
308 * ---------------------------------------------------------------------
311 * indent-tabs-mode: t