1 /* src/vm/signal.c - machine independent signal functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #if defined(__DARWIN__)
34 /* If we compile with -ansi on darwin, <sys/types.h> is not
35 included. So let's do it here. */
36 # include <sys/types.h>
37 # include <sys/utsname.h>
42 #include "threads/thread.h"
44 #include "vm/exceptions.h"
45 #include "vm/signallocal.h"
48 #include "vmcore/options.h"
50 #if defined(ENABLE_STATISTICS)
51 # include "vmcore/statistics.h"
55 /* function prototypes ********************************************************/
57 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
60 /* signal_init *****************************************************************
62 Initializes the signal subsystem and installs the signal handler.
64 *******************************************************************************/
66 bool signal_init(void)
68 #if !defined(__CYGWIN__)
71 TRACESUBSYSTEMINITIALIZATION("signal_init");
73 #if defined(__LINUX__) && defined(ENABLE_THREADS)
74 /* XXX Remove for exact-GC. */
75 if (threads_pthreads_implementation_nptl) {
78 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
79 <ctrl>-\). We enable them later in signal_thread, but only for
82 if (sigemptyset(&mask) != 0)
83 vm_abort_errno("signal_init: sigemptyset failed");
85 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
86 /* Let OpenJDK handle SIGINT itself. */
88 if (sigaddset(&mask, SIGINT) != 0)
89 vm_abort_errno("signal_init: sigaddset failed");
92 #if !defined(__FREEBSD__)
93 if (sigaddset(&mask, SIGQUIT) != 0)
94 vm_abort_errno("signal_init: sigaddset failed");
97 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
98 vm_abort_errno("signal_init: sigprocmask failed");
100 #if defined(__LINUX__) && defined(ENABLE_THREADS)
101 /* XXX Remove for exact-GC. */
105 #if defined(ENABLE_GC_BOEHM)
106 /* Allocate something so the garbage collector's signal handlers
112 /* Install signal handlers for signals we want to catch in all
115 #if defined(ENABLE_JIT)
116 # if defined(ENABLE_INTRP)
119 /* SIGSEGV handler */
121 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
122 SA_NODEFER | SA_SIGINFO);
125 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
126 SA_NODEFER | SA_SIGINFO);
129 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
132 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
133 SA_NODEFER | SA_SIGINFO);
136 # if defined(__ARM__) || defined(__I386__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
137 /* XXX use better defines for that (in arch.h) */
140 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
141 SA_NODEFER | SA_SIGINFO);
144 # if defined(__POWERPC__)
145 /* XXX use better defines for that (in arch.h) */
146 /* SIGTRAP handler */
148 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
149 SA_NODEFER | SA_SIGINFO);
151 # if defined(ENABLE_INTRP)
155 #if defined(__DARWIN__)
160 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
161 if (uname(&name) != 0)
164 /* Make sure the string is large enough */
165 /* Check the major number (ascii comparison) */
166 /* Verify that we're not looking at '10.' by checking for a trailing period. */
167 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
170 /* Reset CrashReporter's task signal handler */
171 kr = task_set_exception_ports(mach_task_self(),
173 # if defined(__I386__)
174 | EXC_MASK_BAD_INSTRUCTION
177 EXCEPTION_STATE_IDENTITY,
178 MACHINE_THREAD_STATE);
180 assert(kr == KERN_SUCCESS);
183 #endif /* !defined(ENABLE_JIT) */
185 #if defined(ENABLE_THREADS)
186 /* SIGHUP handler for threads_thread_interrupt */
188 signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
191 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
192 /* SIGUSR1 handler for the exact GC to suspend threads */
194 signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
198 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
199 /* SIGUSR2 handler for profiling sampling */
201 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
205 #endif /* !defined(__CYGWIN__) */
211 /* signal_register_signal ******************************************************
213 Register the specified handler with the specified signal.
215 *******************************************************************************/
217 void signal_register_signal(int signum, functionptr handler, int flags)
219 struct sigaction act;
221 void (*function)(int, siginfo_t *, void *);
223 function = (void (*)(int, siginfo_t *, void *)) handler;
225 if (sigemptyset(&act.sa_mask) != 0)
226 vm_abort_errno("signal_register_signal: sigemptyset failed");
228 act.sa_sigaction = function;
229 act.sa_flags = flags;
231 if (sigaction(signum, &act, NULL) != 0)
232 vm_abort_errno("signal_register_signal: sigaction failed");
236 /* signal_thread ************************************************************
238 This thread sets the signal mask to catch the user input signals
239 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
240 signals on every single thread running.
242 *******************************************************************************/
244 static void signal_thread(void)
252 if (sigemptyset(&mask) != 0)
253 vm_abort_errno("signal_thread: sigemptyset failed");
255 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
256 /* Let OpenJDK handle SIGINT itself. */
258 if (sigaddset(&mask, SIGINT) != 0)
259 vm_abort_errno("signal_thread: sigaddset failed");
262 #if !defined(__FREEBSD__)
263 if (sigaddset(&mask, SIGQUIT) != 0)
264 vm_abort_errno("signal_thread: sigaddset failed");
268 /* just wait for a signal */
270 #if defined(ENABLE_THREADS)
271 thread_set_state_waiting(t);
274 /* XXX We don't check for an error here, although the man-page
275 states sigwait does not return an error (which is wrong!),
276 but it seems to make problems with Boehm-GC. We should
277 revisit this code with our new exact-GC. */
279 /* if (sigwait(&mask, &sig) != 0) */
280 /* vm_abort_errno("signal_thread: sigwait failed"); */
281 (void) sigwait(&mask, &sig);
283 #if defined(ENABLE_THREADS)
284 thread_set_state_runnable(t);
287 /* Handle the signal. */
289 signal_thread_handler(sig);
294 /* signal_thread_handler *******************************************************
296 Handles the signals caught in the signal handler thread. Also used
297 from sun.misc.Signal with OpenJDK.
299 *******************************************************************************/
301 void signal_thread_handler(int sig)
305 /* exit the vm properly */
311 /* print a thread dump */
312 #if defined(ENABLE_THREADS)
316 #if defined(ENABLE_STATISTICS)
318 statistics_print_memory_usage();
325 /* signal_start_thread *********************************************************
327 Starts the signal handler thread.
329 *******************************************************************************/
331 bool signal_start_thread(void)
333 #if defined(ENABLE_THREADS)
336 name = utf_new_char("Signal Handler");
338 if (!threads_thread_start_internal(name, signal_thread))
341 /* everything's ok */
350 /* signal_handler_sighup *******************************************************
352 This handler is required by threads_thread_interrupt and does
355 *******************************************************************************/
357 #if defined(ENABLE_THREADS)
358 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
366 * These are local overrides for various environment variables in Emacs.
367 * Please do not remove this and leave it at the end of the file, where
368 * Emacs will automagically detect them.
369 * ---------------------------------------------------------------------
372 * indent-tabs-mode: t