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.hpp"
49 #include "vm/signallocal.h"
52 #include "vmcore/method.h"
53 #include "vmcore/options.h"
55 #if defined(ENABLE_STATISTICS)
56 # include "vmcore/statistics.h"
60 /* function prototypes ********************************************************/
62 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
65 /* signal_init *****************************************************************
67 Initializes the signal subsystem and installs the signal handler.
69 *******************************************************************************/
71 bool signal_init(void)
73 #if !defined(__CYGWIN__)
76 TRACESUBSYSTEMINITIALIZATION("signal_init");
78 #if defined(__LINUX__) && defined(ENABLE_THREADS)
79 /* XXX Remove for exact-GC. */
80 if (threads_pthreads_implementation_nptl) {
83 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
84 <ctrl>-\). We enable them later in signal_thread, but only for
87 if (sigemptyset(&mask) != 0)
88 vm_abort_errno("signal_init: sigemptyset failed");
90 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
91 /* Let OpenJDK handle SIGINT itself. */
93 if (sigaddset(&mask, SIGINT) != 0)
94 vm_abort_errno("signal_init: sigaddset failed");
97 #if !defined(__FREEBSD__)
98 if (sigaddset(&mask, SIGQUIT) != 0)
99 vm_abort_errno("signal_init: sigaddset failed");
102 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
103 vm_abort_errno("signal_init: sigprocmask failed");
105 #if defined(__LINUX__) && defined(ENABLE_THREADS)
106 /* XXX Remove for exact-GC. */
110 #if defined(ENABLE_GC_BOEHM)
111 /* Allocate something so the garbage collector's signal handlers
117 /* Install signal handlers for signals we want to catch in all
120 #if defined(ENABLE_JIT)
121 # if defined(ENABLE_INTRP)
124 /* SIGSEGV handler */
126 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
127 SA_NODEFER | SA_SIGINFO);
130 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
131 SA_NODEFER | SA_SIGINFO);
134 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
137 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
138 SA_NODEFER | SA_SIGINFO);
141 # if defined(__ARM__) || defined(__I386__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
142 /* XXX use better defines for that (in arch.h) */
145 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
146 SA_NODEFER | SA_SIGINFO);
149 # if defined(__POWERPC__)
150 /* XXX use better defines for that (in arch.h) */
151 /* SIGTRAP handler */
153 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
154 SA_NODEFER | SA_SIGINFO);
156 # if defined(ENABLE_INTRP)
160 #if defined(__DARWIN__)
165 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
166 if (uname(&name) != 0)
169 /* Make sure the string is large enough */
170 /* Check the major number (ascii comparison) */
171 /* Verify that we're not looking at '10.' by checking for a trailing period. */
172 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
175 /* Reset CrashReporter's task signal handler */
176 kr = task_set_exception_ports(mach_task_self(),
178 # if defined(__I386__)
179 | EXC_MASK_BAD_INSTRUCTION
182 EXCEPTION_STATE_IDENTITY,
183 MACHINE_THREAD_STATE);
185 assert(kr == KERN_SUCCESS);
188 #endif /* !defined(ENABLE_JIT) */
190 #if defined(ENABLE_THREADS)
191 /* SIGHUP handler for threads_thread_interrupt */
193 signal_register_signal(Signal_THREAD_INTERRUPT, (functionptr) signal_handler_sighup, 0);
196 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
197 /* SIGUSR1 handler for the exact GC to suspend threads */
199 signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
203 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
204 /* SIGUSR2 handler for profiling sampling */
206 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
210 #endif /* !defined(__CYGWIN__) */
216 /* signal_register_signal ******************************************************
218 Register the specified handler with the specified signal.
220 *******************************************************************************/
222 void signal_register_signal(int signum, functionptr handler, int flags)
224 struct sigaction act;
226 void (*function)(int, siginfo_t *, void *);
228 function = (void (*)(int, siginfo_t *, void *)) handler;
230 if (sigemptyset(&act.sa_mask) != 0)
231 vm_abort_errno("signal_register_signal: sigemptyset failed");
233 act.sa_sigaction = function;
234 act.sa_flags = flags;
236 if (sigaction(signum, &act, NULL) != 0)
237 vm_abort_errno("signal_register_signal: sigaction failed");
241 /* signal_thread ************************************************************
243 This thread sets the signal mask to catch the user input signals
244 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
245 signals on every single thread running.
247 *******************************************************************************/
249 static void signal_thread(void)
257 if (sigemptyset(&mask) != 0)
258 vm_abort_errno("signal_thread: sigemptyset failed");
260 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
261 /* Let OpenJDK handle SIGINT itself. */
263 if (sigaddset(&mask, SIGINT) != 0)
264 vm_abort_errno("signal_thread: sigaddset failed");
267 #if !defined(__FREEBSD__)
268 if (sigaddset(&mask, SIGQUIT) != 0)
269 vm_abort_errno("signal_thread: sigaddset failed");
273 /* just wait for a signal */
275 #if defined(ENABLE_THREADS)
276 thread_set_state_waiting(t);
279 /* XXX We don't check for an error here, although the man-page
280 states sigwait does not return an error (which is wrong!),
281 but it seems to make problems with Boehm-GC. We should
282 revisit this code with our new exact-GC. */
284 /* if (sigwait(&mask, &sig) != 0) */
285 /* vm_abort_errno("signal_thread: sigwait failed"); */
286 (void) sigwait(&mask, &sig);
288 #if defined(ENABLE_THREADS)
289 thread_set_state_runnable(t);
292 /* Handle the signal. */
294 signal_thread_handler(sig);
299 /* signal_thread_handler *******************************************************
301 Handles the signals caught in the signal handler thread. Also used
302 from sun.misc.Signal with OpenJDK.
304 *******************************************************************************/
306 void signal_thread_handler(int sig)
310 /* exit the vm properly */
316 /* print a thread dump */
317 #if defined(ENABLE_THREADS)
321 #if defined(ENABLE_STATISTICS)
323 statistics_print_memory_usage();
327 #if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
329 // For OpenJDK we dispatch all unknown signals to Java.
330 methodinfo* m = class_resolvemethod(class_sun_misc_Signal, utf_dispatch, utf_int__void);
331 (void) vm_call_method(m, NULL, sig);
333 if (exceptions_get_exception()) {
334 exceptions_print_stacktrace();
335 vm_abort("signal_thread_handler: Java signal handler throw an exception. Exiting...");
344 /* signal_start_thread *********************************************************
346 Starts the signal handler thread.
348 *******************************************************************************/
350 bool signal_start_thread(void)
352 #if defined(ENABLE_THREADS)
355 name = utf_new_char("Signal Handler");
357 if (!threads_thread_start_internal(name, signal_thread))
360 /* everything's ok */
369 /* signal_handler_sighup *******************************************************
371 This handler is required by threads_thread_interrupt and does
374 *******************************************************************************/
376 #if defined(ENABLE_THREADS)
377 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
385 * These are local overrides for various environment variables in Emacs.
386 * Please do not remove this and leave it at the end of the file, where
387 * Emacs will automagically detect them.
388 * ---------------------------------------------------------------------
391 * indent-tabs-mode: t