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 #if defined(ENABLE_GC_BOEHM)
43 # include "mm/memory.h"
46 #include "threads/thread.hpp"
48 #include "vm/exceptions.h"
49 #include "vm/signallocal.h"
52 #include "vmcore/options.h"
54 #if defined(ENABLE_STATISTICS)
55 # include "vmcore/statistics.h"
59 /* function prototypes ********************************************************/
61 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
64 /* signal_init *****************************************************************
66 Initializes the signal subsystem and installs the signal handler.
68 *******************************************************************************/
70 bool signal_init(void)
72 #if !defined(__CYGWIN__)
75 TRACESUBSYSTEMINITIALIZATION("signal_init");
77 #if defined(__LINUX__) && defined(ENABLE_THREADS)
78 /* XXX Remove for exact-GC. */
79 if (threads_pthreads_implementation_nptl) {
82 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
83 <ctrl>-\). We enable them later in signal_thread, but only for
86 if (sigemptyset(&mask) != 0)
87 vm_abort_errno("signal_init: sigemptyset failed");
89 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
90 /* Let OpenJDK handle SIGINT itself. */
92 if (sigaddset(&mask, SIGINT) != 0)
93 vm_abort_errno("signal_init: sigaddset failed");
96 #if !defined(__FREEBSD__)
97 if (sigaddset(&mask, SIGQUIT) != 0)
98 vm_abort_errno("signal_init: sigaddset failed");
101 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
102 vm_abort_errno("signal_init: sigprocmask failed");
104 #if defined(__LINUX__) && defined(ENABLE_THREADS)
105 /* XXX Remove for exact-GC. */
109 #if defined(ENABLE_GC_BOEHM)
110 /* Allocate something so the garbage collector's signal handlers
116 /* Install signal handlers for signals we want to catch in all
119 #if defined(ENABLE_JIT)
120 # if defined(ENABLE_INTRP)
123 /* SIGSEGV handler */
125 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
126 SA_NODEFER | SA_SIGINFO);
129 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
130 SA_NODEFER | SA_SIGINFO);
133 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
136 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
137 SA_NODEFER | SA_SIGINFO);
140 # if defined(__ARM__) || defined(__I386__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
141 /* XXX use better defines for that (in arch.h) */
144 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
145 SA_NODEFER | SA_SIGINFO);
148 # if defined(__POWERPC__)
149 /* XXX use better defines for that (in arch.h) */
150 /* SIGTRAP handler */
152 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
153 SA_NODEFER | SA_SIGINFO);
155 # if defined(ENABLE_INTRP)
159 #if defined(__DARWIN__)
164 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
165 if (uname(&name) != 0)
168 /* Make sure the string is large enough */
169 /* Check the major number (ascii comparison) */
170 /* Verify that we're not looking at '10.' by checking for a trailing period. */
171 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
174 /* Reset CrashReporter's task signal handler */
175 kr = task_set_exception_ports(mach_task_self(),
177 # if defined(__I386__)
178 | EXC_MASK_BAD_INSTRUCTION
181 EXCEPTION_STATE_IDENTITY,
182 MACHINE_THREAD_STATE);
184 assert(kr == KERN_SUCCESS);
187 #endif /* !defined(ENABLE_JIT) */
189 #if defined(ENABLE_THREADS)
190 /* SIGHUP handler for threads_thread_interrupt */
192 signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
195 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
196 /* SIGUSR1 handler for the exact GC to suspend threads */
198 signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
202 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
203 /* SIGUSR2 handler for profiling sampling */
205 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
209 #endif /* !defined(__CYGWIN__) */
215 /* signal_register_signal ******************************************************
217 Register the specified handler with the specified signal.
219 *******************************************************************************/
221 void signal_register_signal(int signum, functionptr handler, int flags)
223 struct sigaction act;
225 void (*function)(int, siginfo_t *, void *);
227 function = (void (*)(int, siginfo_t *, void *)) handler;
229 if (sigemptyset(&act.sa_mask) != 0)
230 vm_abort_errno("signal_register_signal: sigemptyset failed");
232 act.sa_sigaction = function;
233 act.sa_flags = flags;
235 if (sigaction(signum, &act, NULL) != 0)
236 vm_abort_errno("signal_register_signal: sigaction failed");
240 /* signal_thread ************************************************************
242 This thread sets the signal mask to catch the user input signals
243 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
244 signals on every single thread running.
246 *******************************************************************************/
248 static void signal_thread(void)
256 if (sigemptyset(&mask) != 0)
257 vm_abort_errno("signal_thread: sigemptyset failed");
259 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
260 /* Let OpenJDK handle SIGINT itself. */
262 if (sigaddset(&mask, SIGINT) != 0)
263 vm_abort_errno("signal_thread: sigaddset failed");
266 #if !defined(__FREEBSD__)
267 if (sigaddset(&mask, SIGQUIT) != 0)
268 vm_abort_errno("signal_thread: sigaddset failed");
272 /* just wait for a signal */
274 #if defined(ENABLE_THREADS)
275 thread_set_state_waiting(t);
278 /* XXX We don't check for an error here, although the man-page
279 states sigwait does not return an error (which is wrong!),
280 but it seems to make problems with Boehm-GC. We should
281 revisit this code with our new exact-GC. */
283 /* if (sigwait(&mask, &sig) != 0) */
284 /* vm_abort_errno("signal_thread: sigwait failed"); */
285 (void) sigwait(&mask, &sig);
287 #if defined(ENABLE_THREADS)
288 thread_set_state_runnable(t);
291 /* Handle the signal. */
293 signal_thread_handler(sig);
298 /* signal_thread_handler *******************************************************
300 Handles the signals caught in the signal handler thread. Also used
301 from sun.misc.Signal with OpenJDK.
303 *******************************************************************************/
305 void signal_thread_handler(int sig)
309 /* exit the vm properly */
315 /* print a thread dump */
316 #if defined(ENABLE_THREADS)
320 #if defined(ENABLE_STATISTICS)
322 statistics_print_memory_usage();
329 /* signal_start_thread *********************************************************
331 Starts the signal handler thread.
333 *******************************************************************************/
335 bool signal_start_thread(void)
337 #if defined(ENABLE_THREADS)
340 name = utf_new_char("Signal Handler");
342 if (!threads_thread_start_internal(name, signal_thread))
345 /* everything's ok */
354 /* signal_handler_sighup *******************************************************
356 This handler is required by threads_thread_interrupt and does
359 *******************************************************************************/
361 #if defined(ENABLE_THREADS)
362 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
370 * These are local overrides for various environment variables in Emacs.
371 * Please do not remove this and leave it at the end of the file, where
372 * Emacs will automagically detect them.
373 * ---------------------------------------------------------------------
376 * indent-tabs-mode: t