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 #if defined(ENABLE_THREADS)
47 # include "threads/threads-common.h"
49 # include "threads/none/threads.h"
52 #include "toolbox/logging.h"
54 #include "vm/exceptions.h"
55 #include "vm/signallocal.h"
58 #include "vm/jit/codegen-common.h"
59 #include "vm/jit/disass.h"
60 #include "vm/jit/patcher-common.h"
62 #include "vmcore/options.h"
64 #if defined(ENABLE_STATISTICS)
65 # include "vmcore/statistics.h"
69 /* function prototypes ********************************************************/
71 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
74 /* signal_init *****************************************************************
76 Initializes the signal subsystem and installs the signal handler.
78 *******************************************************************************/
80 bool signal_init(void)
82 #if !defined(__CYGWIN__)
85 TRACESUBSYSTEMINITIALIZATION("signal_init");
87 #if defined(__LINUX__) && defined(ENABLE_THREADS)
88 /* XXX Remove for exact-GC. */
89 if (threads_pthreads_implementation_nptl) {
92 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
93 <ctrl>-\). We enable them later in signal_thread, but only for
96 if (sigemptyset(&mask) != 0)
97 vm_abort_errno("signal_init: sigemptyset failed");
99 #if !defined(WITH_CLASSPATH_SUN)
100 /* Let OpenJDK handle SIGINT itself. */
102 if (sigaddset(&mask, SIGINT) != 0)
103 vm_abort_errno("signal_init: sigaddset failed");
106 #if !defined(__FREEBSD__)
107 if (sigaddset(&mask, SIGQUIT) != 0)
108 vm_abort_errno("signal_init: sigaddset failed");
111 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
112 vm_abort_errno("signal_init: sigprocmask failed");
114 #if defined(__LINUX__) && defined(ENABLE_THREADS)
115 /* XXX Remove for exact-GC. */
119 #if defined(ENABLE_GC_BOEHM)
120 /* Allocate something so the garbage collector's signal handlers
126 /* Install signal handlers for signals we want to catch in all
129 #if defined(ENABLE_JIT)
130 # if defined(ENABLE_INTRP)
133 /* SIGSEGV handler */
135 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
136 SA_NODEFER | SA_SIGINFO);
139 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
140 SA_NODEFER | SA_SIGINFO);
143 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
146 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
147 SA_NODEFER | SA_SIGINFO);
150 # if defined(__ARM__) || defined(__I386__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
151 /* XXX use better defines for that (in arch.h) */
154 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
155 SA_NODEFER | SA_SIGINFO);
158 # if defined(__POWERPC__)
159 /* XXX use better defines for that (in arch.h) */
160 /* SIGTRAP handler */
162 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
163 SA_NODEFER | SA_SIGINFO);
165 # if defined(ENABLE_INTRP)
169 #if defined(__DARWIN__)
174 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
175 if (uname(&name) != 0)
178 /* Make sure the string is large enough */
179 /* Check the major number (ascii comparison) */
180 /* Verify that we're not looking at '10.' by checking for a trailing period. */
181 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
184 /* Reset CrashReporter's task signal handler */
185 kr = task_set_exception_ports(mach_task_self(),
187 # if defined(__I386__)
188 | EXC_MASK_BAD_INSTRUCTION
191 EXCEPTION_STATE_IDENTITY,
192 MACHINE_THREAD_STATE);
194 assert(kr == KERN_SUCCESS);
197 #endif /* !defined(ENABLE_JIT) */
199 #if defined(ENABLE_THREADS)
200 /* SIGHUP handler for threads_thread_interrupt */
202 signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
205 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
206 /* SIGUSR1 handler for the exact GC to suspend threads */
208 signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
212 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
213 /* SIGUSR2 handler for profiling sampling */
215 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
219 #endif /* !defined(__CYGWIN__) */
225 /* signal_register_signal ******************************************************
227 Register the specified handler with the specified signal.
229 *******************************************************************************/
231 void signal_register_signal(int signum, functionptr handler, int flags)
233 struct sigaction act;
235 void (*function)(int, siginfo_t *, void *);
237 function = (void (*)(int, siginfo_t *, void *)) handler;
239 if (sigemptyset(&act.sa_mask) != 0)
240 vm_abort_errno("signal_register_signal: sigemptyset failed");
242 act.sa_sigaction = function;
243 act.sa_flags = flags;
245 if (sigaction(signum, &act, NULL) != 0)
246 vm_abort_errno("signal_register_signal: sigaction failed");
250 /* signal_handle ***************************************************************
252 Handles the signal caught by a signal handler and calls the correct
255 *******************************************************************************/
257 void *signal_handle(int type, intptr_t val,
258 void *pv, void *sp, void *ra, void *xpc, void *context)
260 stackframeinfo_t sfi;
268 log_println("[signal_handle: trap %d]", type);
271 #if defined(ENABLE_VMLOG)
272 vmlog_cacao_signl_type(type);
275 /* Prevent compiler warnings. */
280 /* wrap the value into a handle if it is a reference */
281 /* BEFORE: creating stackframeinfo */
284 case EXCEPTION_HARDWARE_CLASSCAST:
285 o = LLNI_WRAP((java_object_t *) val);
288 case EXCEPTION_HARDWARE_COMPILER:
289 /* In this case the passed PV points to the compiler stub. We
290 get the methodinfo pointer here and set PV to NULL so
291 stacktrace_stackframeinfo_add determines the PV for the
292 parent Java method. */
294 m = code_get_methodinfo_for_pv(pv);
303 /* Fill and add a stackframeinfo. */
305 stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
308 case EXCEPTION_HARDWARE_NULLPOINTER:
309 p = exceptions_new_nullpointerexception();
312 case EXCEPTION_HARDWARE_ARITHMETIC:
313 p = exceptions_new_arithmeticexception();
316 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
318 p = exceptions_new_arrayindexoutofboundsexception(index);
321 case EXCEPTION_HARDWARE_ARRAYSTORE:
322 p = exceptions_new_arraystoreexception();
325 case EXCEPTION_HARDWARE_CLASSCAST:
326 p = exceptions_new_classcastexception(o);
329 case EXCEPTION_HARDWARE_EXCEPTION:
330 p = exceptions_fillinstacktrace();
333 case EXCEPTION_HARDWARE_PATCHER:
334 #if defined(ENABLE_REPLACEMENT)
335 if (replace_me_wrapper(xpc, context)) {
340 p = patcher_handler(xpc);
343 case EXCEPTION_HARDWARE_COMPILER:
344 p = jit_compile_handle(m, sfi.pv, ra, (void *) val);
348 /* Let's try to get a backtrace. */
350 codegen_get_pv_from_pc(xpc);
352 /* If that does not work, print more debug info. */
354 log_println("signal_handle: unknown hardware exception type %d", type);
356 #if SIZEOF_VOID_P == 8
357 log_println("PC=0x%016lx", xpc);
359 log_println("PC=0x%08x", xpc);
362 #if defined(ENABLE_DISASSEMBLER)
363 log_println("machine instruction at PC:");
367 vm_abort("Exiting...");
369 /* keep compiler happy */
374 /* Remove stackframeinfo. */
376 stacktrace_stackframeinfo_remove(&sfi);
378 /* unwrap and return the exception object */
379 /* AFTER: removing stackframeinfo */
381 if (type == EXCEPTION_HARDWARE_COMPILER)
384 return LLNI_UNWRAP(p);
388 /* signal_thread ************************************************************
390 This thread sets the signal mask to catch the user input signals
391 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
392 signals on every single thread running.
394 *******************************************************************************/
396 static void signal_thread(void)
404 if (sigemptyset(&mask) != 0)
405 vm_abort_errno("signal_thread: sigemptyset failed");
407 #if !defined(WITH_CLASSPATH_SUN)
408 /* Let OpenJDK handle SIGINT itself. */
410 if (sigaddset(&mask, SIGINT) != 0)
411 vm_abort_errno("signal_thread: sigaddset failed");
414 #if !defined(__FREEBSD__)
415 if (sigaddset(&mask, SIGQUIT) != 0)
416 vm_abort_errno("signal_thread: sigaddset failed");
420 /* just wait for a signal */
422 #if defined(ENABLE_THREADS)
423 threads_thread_state_waiting(t);
426 /* XXX We don't check for an error here, although the man-page
427 states sigwait does not return an error (which is wrong!),
428 but it seems to make problems with Boehm-GC. We should
429 revisit this code with our new exact-GC. */
431 /* if (sigwait(&mask, &sig) != 0) */
432 /* vm_abort_errno("signal_thread: sigwait failed"); */
433 (void) sigwait(&mask, &sig);
435 #if defined(ENABLE_THREADS)
436 threads_thread_state_runnable(t);
439 /* Handle the signal. */
441 signal_thread_handler(sig);
446 /* signal_thread_handler *******************************************************
448 Handles the signals caught in the signal handler thread. Also used
449 from sun.misc.Signal with OpenJDK.
451 *******************************************************************************/
453 void signal_thread_handler(int sig)
457 /* exit the vm properly */
463 /* print a thread dump */
464 #if defined(ENABLE_THREADS)
468 #if defined(ENABLE_STATISTICS)
470 statistics_print_memory_usage();
477 /* signal_start_thread *********************************************************
479 Starts the signal handler thread.
481 *******************************************************************************/
483 bool signal_start_thread(void)
485 #if defined(ENABLE_THREADS)
488 name = utf_new_char("Signal Handler");
490 if (!threads_thread_start_internal(name, signal_thread))
493 /* everything's ok */
502 /* signal_handler_sighup *******************************************************
504 This handler is required by threads_thread_interrupt and does
507 *******************************************************************************/
509 #if defined(ENABLE_THREADS)
510 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
518 * These are local overrides for various environment variables in Emacs.
519 * Please do not remove this and leave it at the end of the file, where
520 * Emacs will automagically detect them.
521 * ---------------------------------------------------------------------
524 * indent-tabs-mode: t