1 /* src/vm/signal.c - machine independent signal functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
36 #if defined(__DARWIN__)
37 /* If we compile with -ansi on darwin, <sys/types.h> is not
38 included. So let's do it here. */
39 # include <sys/types.h>
44 #include "mm/memory.h"
46 #include "native/llni.h"
48 #if defined(ENABLE_THREADS)
49 # include "threads/threads-common.h"
51 # include "threads/none/threads.h"
54 #include "toolbox/logging.h"
56 #include "vm/exceptions.h"
57 #include "vm/signallocal.h"
60 #include "vm/jit/codegen-common.h"
61 #include "vm/jit/disass.h"
62 #include "vm/jit/patcher-common.h"
64 #include "vmcore/options.h"
66 #if defined(ENABLE_STATISTICS)
67 # include "vmcore/statistics.h"
71 /* function prototypes ********************************************************/
73 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
76 /* signal_init *****************************************************************
78 Initializes the signal subsystem and installs the signal handler.
80 *******************************************************************************/
82 bool signal_init(void)
84 #if !defined(__CYGWIN__)
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("signal_init: sigemptyset failed: %s", strerror(errno));
99 #if !defined(WITH_CLASSPATH_SUN)
100 /* Let OpenJDK handle SIGINT itself. */
102 if (sigaddset(&mask, SIGINT) != 0)
103 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
106 #if !defined(__FREEBSD__)
107 if (sigaddset(&mask, SIGQUIT) != 0)
108 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
111 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
112 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
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)
168 #endif /* !defined(ENABLE_INTRP) */
170 #if defined(ENABLE_THREADS)
171 /* SIGHUP handler for threads_thread_interrupt */
173 signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
176 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
177 /* SIGUSR1 handler for the exact GC to suspend threads */
179 signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
183 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
184 /* SIGUSR2 handler for profiling sampling */
186 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
190 #endif /* !defined(__CYGWIN__) */
196 /* signal_register_signal ******************************************************
198 Register the specified handler with the specified signal.
200 *******************************************************************************/
202 void signal_register_signal(int signum, functionptr handler, int flags)
204 struct sigaction act;
206 void (*function)(int, siginfo_t *, void *);
208 function = (void (*)(int, siginfo_t *, void *)) handler;
210 if (sigemptyset(&act.sa_mask) != 0)
211 vm_abort("signal_register_signal: sigemptyset failed: %s",
214 act.sa_sigaction = function;
215 act.sa_flags = flags;
217 if (sigaction(signum, &act, NULL) != 0)
218 vm_abort("signal_register_signal: sigaction failed: %s",
223 /* signal_handle ***************************************************************
225 Handles the signal caught by a signal handler and calls the correct
228 *******************************************************************************/
230 void *signal_handle(int type, intptr_t val,
231 void *pv, void *sp, void *ra, void *xpc, void *context)
233 stackframeinfo_t sfi;
239 /* Prevent compiler warnings. */
244 /* wrap the value into a handle if it is a reference */
245 /* BEFORE: creating stackframeinfo */
248 case EXCEPTION_HARDWARE_CLASSCAST:
249 o = LLNI_WRAP((java_object_t *) val);
252 case EXCEPTION_HARDWARE_COMPILER:
253 /* In this case the passed PV points to the compiler stub. We
254 get the methodinfo pointer here and set PV to NULL so
255 stacktrace_stackframeinfo_add determines the PV for the
256 parent Java method. */
258 m = code_get_methodinfo_for_pv(pv);
267 /* Fill and add a stackframeinfo. */
269 stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
272 case EXCEPTION_HARDWARE_NULLPOINTER:
273 p = exceptions_new_nullpointerexception();
276 case EXCEPTION_HARDWARE_ARITHMETIC:
277 p = exceptions_new_arithmeticexception();
280 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
282 p = exceptions_new_arrayindexoutofboundsexception(index);
285 case EXCEPTION_HARDWARE_ARRAYSTORE:
286 p = exceptions_new_arraystoreexception();
289 case EXCEPTION_HARDWARE_CLASSCAST:
290 p = exceptions_new_classcastexception(o);
293 case EXCEPTION_HARDWARE_EXCEPTION:
294 p = exceptions_fillinstacktrace();
297 case EXCEPTION_HARDWARE_PATCHER:
298 #if defined(ENABLE_REPLACEMENT)
299 if (replace_me_wrapper(xpc, context)) {
304 p = patcher_handler(xpc);
307 case EXCEPTION_HARDWARE_COMPILER:
308 p = jit_compile_handle(m, sfi.pv, ra, (void *) val);
312 /* Let's try to get a backtrace. */
314 codegen_get_pv_from_pc(xpc);
316 /* If that does not work, print more debug info. */
318 log_println("signal_handle: unknown hardware exception type %d", type);
320 #if SIZEOF_VOID_P == 8
321 log_println("PC=0x%016lx", xpc);
323 log_println("PC=0x%08x", xpc);
326 #if defined(ENABLE_DISASSEMBLER)
327 log_println("machine instruction at PC:");
331 vm_abort("Exiting...");
333 /* keep compiler happy */
338 /* Remove stackframeinfo. */
340 stacktrace_stackframeinfo_remove(&sfi);
342 /* unwrap and return the exception object */
343 /* AFTER: removing stackframeinfo */
345 if (type == EXCEPTION_HARDWARE_COMPILER)
348 return LLNI_UNWRAP(p);
352 /* signal_thread ************************************************************
354 This thread sets the signal mask to catch the user input signals
355 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
356 signals on every single thread running.
358 *******************************************************************************/
360 static void signal_thread(void)
368 if (sigemptyset(&mask) != 0)
369 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
371 #if !defined(WITH_CLASSPATH_SUN)
372 /* Let OpenJDK handle SIGINT itself. */
374 if (sigaddset(&mask, SIGINT) != 0)
375 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
378 #if !defined(__FREEBSD__)
379 if (sigaddset(&mask, SIGQUIT) != 0)
380 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
384 /* just wait for a signal */
386 #if defined(ENABLE_THREADS)
387 threads_thread_state_waiting(t);
390 /* XXX We don't check for an error here, although the man-page
391 states sigwait does not return an error (which is wrong!),
392 but it seems to make problems with Boehm-GC. We should
393 revisit this code with our new exact-GC. */
395 /* if (sigwait(&mask, &sig) != 0) */
396 /* vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
397 (void) sigwait(&mask, &sig);
399 #if defined(ENABLE_THREADS)
400 threads_thread_state_runnable(t);
403 /* Handle the signal. */
405 signal_thread_handler(sig);
410 /* signal_thread_handler *******************************************************
412 Handles the signals caught in the signal handler thread. Also used
413 from sun.misc.Signal with OpenJDK.
415 *******************************************************************************/
417 void signal_thread_handler(int sig)
421 /* exit the vm properly */
427 /* print a thread dump */
428 #if defined(ENABLE_THREADS)
432 #if defined(ENABLE_STATISTICS)
434 statistics_print_memory_usage();
441 /* signal_start_thread *********************************************************
443 Starts the signal handler thread.
445 *******************************************************************************/
447 bool signal_start_thread(void)
449 #if defined(ENABLE_THREADS)
452 name = utf_new_char("Signal Handler");
454 if (!threads_thread_start_internal(name, signal_thread))
457 /* everything's ok */
466 /* signal_handler_sighup *******************************************************
468 This handler is required by threads_thread_interrupt and does
471 *******************************************************************************/
473 #if defined(ENABLE_THREADS)
474 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
482 * These are local overrides for various environment variables in Emacs.
483 * Please do not remove this and leave it at the end of the file, where
484 * Emacs will automagically detect them.
485 * ---------------------------------------------------------------------
488 * indent-tabs-mode: t