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 "mm/memory.h"
44 #include "native/llni.h"
46 #include "threads/thread.h"
48 #include "toolbox/logging.h"
50 #include "vm/exceptions.h"
51 #include "vm/signallocal.h"
54 #include "vm/jit/codegen-common.h"
55 #include "vm/jit/disass.h"
56 #include "vm/jit/methodtree.h"
57 #include "vm/jit/patcher-common.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 bool signal_init(void)
79 #if !defined(__CYGWIN__)
82 TRACESUBSYSTEMINITIALIZATION("signal_init");
84 #if defined(__LINUX__) && defined(ENABLE_THREADS)
85 /* XXX Remove for exact-GC. */
86 if (threads_pthreads_implementation_nptl) {
89 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
90 <ctrl>-\). We enable them later in signal_thread, but only for
93 if (sigemptyset(&mask) != 0)
94 vm_abort_errno("signal_init: sigemptyset failed");
96 #if !defined(WITH_CLASSPATH_SUN)
97 /* Let OpenJDK handle SIGINT itself. */
99 if (sigaddset(&mask, SIGINT) != 0)
100 vm_abort_errno("signal_init: sigaddset failed");
103 #if !defined(__FREEBSD__)
104 if (sigaddset(&mask, SIGQUIT) != 0)
105 vm_abort_errno("signal_init: sigaddset failed");
108 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
109 vm_abort_errno("signal_init: sigprocmask failed");
111 #if defined(__LINUX__) && defined(ENABLE_THREADS)
112 /* XXX Remove for exact-GC. */
116 #if defined(ENABLE_GC_BOEHM)
117 /* Allocate something so the garbage collector's signal handlers
123 /* Install signal handlers for signals we want to catch in all
126 #if defined(ENABLE_JIT)
127 # if defined(ENABLE_INTRP)
130 /* SIGSEGV handler */
132 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
133 SA_NODEFER | SA_SIGINFO);
136 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
137 SA_NODEFER | SA_SIGINFO);
140 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
143 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
144 SA_NODEFER | SA_SIGINFO);
147 # if defined(__ARM__) || defined(__I386__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
148 /* XXX use better defines for that (in arch.h) */
151 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
152 SA_NODEFER | SA_SIGINFO);
155 # if defined(__POWERPC__)
156 /* XXX use better defines for that (in arch.h) */
157 /* SIGTRAP handler */
159 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
160 SA_NODEFER | SA_SIGINFO);
162 # if defined(ENABLE_INTRP)
166 #if defined(__DARWIN__)
171 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
172 if (uname(&name) != 0)
175 /* Make sure the string is large enough */
176 /* Check the major number (ascii comparison) */
177 /* Verify that we're not looking at '10.' by checking for a trailing period. */
178 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
181 /* Reset CrashReporter's task signal handler */
182 kr = task_set_exception_ports(mach_task_self(),
184 # if defined(__I386__)
185 | EXC_MASK_BAD_INSTRUCTION
188 EXCEPTION_STATE_IDENTITY,
189 MACHINE_THREAD_STATE);
191 assert(kr == KERN_SUCCESS);
194 #endif /* !defined(ENABLE_JIT) */
196 #if defined(ENABLE_THREADS)
197 /* SIGHUP handler for threads_thread_interrupt */
199 signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
202 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
203 /* SIGUSR1 handler for the exact GC to suspend threads */
205 signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
209 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
210 /* SIGUSR2 handler for profiling sampling */
212 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
216 #endif /* !defined(__CYGWIN__) */
222 /* signal_register_signal ******************************************************
224 Register the specified handler with the specified signal.
226 *******************************************************************************/
228 void signal_register_signal(int signum, functionptr handler, int flags)
230 struct sigaction act;
232 void (*function)(int, siginfo_t *, void *);
234 function = (void (*)(int, siginfo_t *, void *)) handler;
236 if (sigemptyset(&act.sa_mask) != 0)
237 vm_abort_errno("signal_register_signal: sigemptyset failed");
239 act.sa_sigaction = function;
240 act.sa_flags = flags;
242 if (sigaction(signum, &act, NULL) != 0)
243 vm_abort_errno("signal_register_signal: sigaction failed");
247 /* signal_handle ***************************************************************
249 Handles the signal caught by a signal handler and calls the correct
252 *******************************************************************************/
254 void *signal_handle(int type, intptr_t val,
255 void *pv, void *sp, void *ra, void *xpc, void *context)
257 stackframeinfo_t sfi;
265 log_println("[signal_handle: trap %d]", type);
268 #if defined(ENABLE_VMLOG)
269 vmlog_cacao_signl_type(type);
272 /* Prevent compiler warnings. */
277 /* wrap the value into a handle if it is a reference */
278 /* BEFORE: creating stackframeinfo */
281 case EXCEPTION_HARDWARE_CLASSCAST:
282 o = LLNI_WRAP((java_object_t *) val);
285 case EXCEPTION_HARDWARE_COMPILER:
286 /* In this case the passed PV points to the compiler stub. We
287 get the methodinfo pointer here and set PV to NULL so
288 stacktrace_stackframeinfo_add determines the PV for the
289 parent Java method. */
291 m = code_get_methodinfo_for_pv(pv);
300 /* Fill and add a stackframeinfo. */
302 stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
305 case EXCEPTION_HARDWARE_NULLPOINTER:
306 p = exceptions_new_nullpointerexception();
309 case EXCEPTION_HARDWARE_ARITHMETIC:
310 p = exceptions_new_arithmeticexception();
313 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
315 p = exceptions_new_arrayindexoutofboundsexception(index);
318 case EXCEPTION_HARDWARE_ARRAYSTORE:
319 p = exceptions_new_arraystoreexception();
322 case EXCEPTION_HARDWARE_CLASSCAST:
323 p = exceptions_new_classcastexception(o);
326 case EXCEPTION_HARDWARE_EXCEPTION:
327 p = exceptions_fillinstacktrace();
330 case EXCEPTION_HARDWARE_PATCHER:
331 #if defined(ENABLE_REPLACEMENT)
332 if (replace_me_wrapper(xpc, context)) {
337 p = patcher_handler(xpc);
340 case EXCEPTION_HARDWARE_COMPILER:
341 p = jit_compile_handle(m, sfi.pv, ra, (void *) val);
345 /* Let's try to get a backtrace. */
347 (void) methodtree_find(xpc);
349 /* If that does not work, print more debug info. */
351 log_println("signal_handle: unknown hardware exception type %d", type);
353 #if SIZEOF_VOID_P == 8
354 log_println("PC=0x%016lx", xpc);
356 log_println("PC=0x%08x", xpc);
359 #if defined(ENABLE_DISASSEMBLER)
360 log_println("machine instruction at PC:");
364 vm_abort("Exiting...");
366 /* keep compiler happy */
371 /* Remove stackframeinfo. */
373 stacktrace_stackframeinfo_remove(&sfi);
375 /* unwrap and return the exception object */
376 /* AFTER: removing stackframeinfo */
378 if (type == EXCEPTION_HARDWARE_COMPILER)
381 return LLNI_UNWRAP(p);
385 /* signal_thread ************************************************************
387 This thread sets the signal mask to catch the user input signals
388 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
389 signals on every single thread running.
391 *******************************************************************************/
393 static void signal_thread(void)
401 if (sigemptyset(&mask) != 0)
402 vm_abort_errno("signal_thread: sigemptyset failed");
404 #if !defined(WITH_CLASSPATH_SUN)
405 /* Let OpenJDK handle SIGINT itself. */
407 if (sigaddset(&mask, SIGINT) != 0)
408 vm_abort_errno("signal_thread: sigaddset failed");
411 #if !defined(__FREEBSD__)
412 if (sigaddset(&mask, SIGQUIT) != 0)
413 vm_abort_errno("signal_thread: sigaddset failed");
417 /* just wait for a signal */
419 #if defined(ENABLE_THREADS)
420 thread_set_state_waiting(t);
423 /* XXX We don't check for an error here, although the man-page
424 states sigwait does not return an error (which is wrong!),
425 but it seems to make problems with Boehm-GC. We should
426 revisit this code with our new exact-GC. */
428 /* if (sigwait(&mask, &sig) != 0) */
429 /* vm_abort_errno("signal_thread: sigwait failed"); */
430 (void) sigwait(&mask, &sig);
432 #if defined(ENABLE_THREADS)
433 thread_set_state_runnable(t);
436 /* Handle the signal. */
438 signal_thread_handler(sig);
443 /* signal_thread_handler *******************************************************
445 Handles the signals caught in the signal handler thread. Also used
446 from sun.misc.Signal with OpenJDK.
448 *******************************************************************************/
450 void signal_thread_handler(int sig)
454 /* exit the vm properly */
460 /* print a thread dump */
461 #if defined(ENABLE_THREADS)
465 #if defined(ENABLE_STATISTICS)
467 statistics_print_memory_usage();
474 /* signal_start_thread *********************************************************
476 Starts the signal handler thread.
478 *******************************************************************************/
480 bool signal_start_thread(void)
482 #if defined(ENABLE_THREADS)
485 name = utf_new_char("Signal Handler");
487 if (!threads_thread_start_internal(name, signal_thread))
490 /* everything's ok */
499 /* signal_handler_sighup *******************************************************
501 This handler is required by threads_thread_interrupt and does
504 *******************************************************************************/
506 #if defined(ENABLE_THREADS)
507 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
515 * These are local overrides for various environment variables in Emacs.
516 * Please do not remove this and leave it at the end of the file, where
517 * Emacs will automagically detect them.
518 * ---------------------------------------------------------------------
521 * indent-tabs-mode: t