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 #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 #if defined(__LINUX__) && defined(ENABLE_THREADS)
86 /* XXX Remove for exact-GC. */
87 if (threads_pthreads_implementation_nptl) {
90 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
91 <ctrl>-\). We enable them later in signal_thread, but only for
94 if (sigemptyset(&mask) != 0)
95 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
97 #if !defined(WITH_CLASSPATH_SUN)
98 /* Let OpenJDK handle SIGINT itself. */
100 if (sigaddset(&mask, SIGINT) != 0)
101 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
104 #if !defined(__FREEBSD__)
105 if (sigaddset(&mask, SIGQUIT) != 0)
106 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
109 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
110 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
112 #if defined(__LINUX__) && defined(ENABLE_THREADS)
113 /* XXX Remove for exact-GC. */
117 #if defined(ENABLE_GC_BOEHM)
118 /* Allocate something so the garbage collector's signal handlers
124 /* Install signal handlers for signals we want to catch in all
127 #if defined(ENABLE_JIT)
128 # if defined(ENABLE_INTRP)
131 /* SIGSEGV handler */
133 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
134 SA_NODEFER | SA_SIGINFO);
137 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
138 SA_NODEFER | SA_SIGINFO);
141 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
144 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
145 SA_NODEFER | SA_SIGINFO);
148 # if defined(__ARM__) || defined(__S390__)
149 /* XXX use better defines for that (in arch.h) */
152 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
153 SA_NODEFER | SA_SIGINFO);
156 # if defined(__POWERPC__)
157 /* XXX use better defines for that (in arch.h) */
158 /* SIGTRAP handler */
160 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
161 SA_NODEFER | SA_SIGINFO);
163 # if defined(ENABLE_INTRP)
166 #endif /* !defined(ENABLE_INTRP) */
168 #if defined(ENABLE_THREADS)
169 /* SIGHUP handler for threads_thread_interrupt */
171 signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
174 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
175 /* SIGUSR1 handler for the exact GC to suspend threads */
177 signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
181 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
182 /* SIGUSR2 handler for profiling sampling */
184 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
188 #endif /* !defined(__CYGWIN__) */
194 /* signal_register_signal ******************************************************
196 Register the specified handler with the specified signal.
198 *******************************************************************************/
200 void signal_register_signal(int signum, functionptr handler, int flags)
202 struct sigaction act;
204 void (*function)(int, siginfo_t *, void *);
206 function = (void (*)(int, siginfo_t *, void *)) handler;
208 if (sigemptyset(&act.sa_mask) != 0)
209 vm_abort("signal_register_signal: sigemptyset failed: %s",
212 act.sa_sigaction = function;
213 act.sa_flags = flags;
215 if (sigaction(signum, &act, NULL) != 0)
216 vm_abort("signal_register_signal: sigaction failed: %s",
221 /* signal_handle ***************************************************************
223 Handles the signal caught by a signal handler and calls the correct
226 *******************************************************************************/
228 void *signal_handle(void *xpc, int type, intptr_t val)
235 case EXCEPTION_HARDWARE_NULLPOINTER:
236 p = exceptions_new_nullpointerexception();
239 case EXCEPTION_HARDWARE_ARITHMETIC:
240 p = exceptions_new_arithmeticexception();
243 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
245 p = exceptions_new_arrayindexoutofboundsexception(index);
248 case EXCEPTION_HARDWARE_CLASSCAST:
249 o = (java_object_t *) val;
250 p = exceptions_new_classcastexception(o);
253 case EXCEPTION_HARDWARE_EXCEPTION:
254 p = exceptions_fillinstacktrace();
257 case EXCEPTION_HARDWARE_PATCHER:
258 #if defined(ENABLE_REPLACEMENT)
259 if (replace_me_wrapper(xpc)) {
264 p = patcher_handler(xpc);
268 /* Let's try to get a backtrace. */
270 codegen_get_pv_from_pc(xpc);
272 /* If that does not work, print more debug info. */
274 log_println("exceptions_new_hardware_exception: unknown exception type %d", type);
276 #if SIZEOF_VOID_P == 8
277 log_println("PC=0x%016lx", xpc);
279 log_println("PC=0x%08x", xpc);
282 #if defined(ENABLE_DISASSEMBLER)
283 log_println("machine instruction at PC:");
287 vm_abort("Exiting...");
289 /* keep compiler happy */
298 /* signal_thread ************************************************************
300 This thread sets the signal mask to catch the user input signals
301 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
302 signals on every single thread running.
304 *******************************************************************************/
306 static void signal_thread(void)
314 if (sigemptyset(&mask) != 0)
315 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
317 #if !defined(WITH_CLASSPATH_SUN)
318 /* Let OpenJDK handle SIGINT itself. */
320 if (sigaddset(&mask, SIGINT) != 0)
321 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
324 #if !defined(__FREEBSD__)
325 if (sigaddset(&mask, SIGQUIT) != 0)
326 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
330 /* just wait for a signal */
332 #if defined(ENABLE_THREADS)
333 threads_thread_state_waiting(t);
336 /* XXX We don't check for an error here, although the man-page
337 states sigwait does not return an error (which is wrong!),
338 but it seems to make problems with Boehm-GC. We should
339 revisit this code with our new exact-GC. */
341 /* if (sigwait(&mask, &sig) != 0) */
342 /* vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
343 (void) sigwait(&mask, &sig);
345 #if defined(ENABLE_THREADS)
346 threads_thread_state_runnable(t);
349 /* Handle the signal. */
351 signal_thread_handler(sig);
356 /* signal_thread_handler *******************************************************
358 Handles the signals caught in the signal handler thread. Also used
359 from sun.misc.Signal with OpenJDK.
361 *******************************************************************************/
363 void signal_thread_handler(int sig)
367 /* exit the vm properly */
373 /* print a thread dump */
374 #if defined(ENABLE_THREADS)
378 #if defined(ENABLE_STATISTICS)
380 statistics_print_memory_usage();
387 /* signal_start_thread *********************************************************
389 Starts the signal handler thread.
391 *******************************************************************************/
393 bool signal_start_thread(void)
395 #if defined(ENABLE_THREADS)
398 name = utf_new_char("Signal Handler");
400 if (!threads_thread_start_internal(name, signal_thread))
403 /* everything's ok */
412 /* signal_handler_sighup *******************************************************
414 This handler is required by threads_thread_interrupt and does
417 *******************************************************************************/
419 #if defined(ENABLE_THREADS)
420 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
428 * These are local overrides for various environment variables in Emacs.
429 * Please do not remove this and leave it at the end of the file, where
430 * Emacs will automagically detect them.
431 * ---------------------------------------------------------------------
434 * indent-tabs-mode: t