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
25 $Id: signal.c 8306 2007-08-15 14:47:11Z twisti $
38 #if defined(__DARWIN__)
39 /* If we compile with -ansi on darwin, <sys/types.h> is not
40 included. So let's do it here. */
41 # include <sys/types.h>
46 #include "mm/memory.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 (sigaddset(&mask, SIGINT) != 0)
100 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
102 #if !defined(__FREEBSD__)
103 if (sigaddset(&mask, SIGQUIT) != 0)
104 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
107 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
108 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
110 #if defined(__LINUX__) && defined(ENABLE_THREADS)
111 /* XXX Remove for exact-GC. */
115 #if defined(ENABLE_GC_BOEHM)
116 /* Allocate something so the garbage collector's signal handlers
122 /* Install signal handlers for signals we want to catch in all
125 #if defined(ENABLE_JIT)
126 # if defined(ENABLE_INTRP)
129 /* SIGSEGV handler */
131 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
132 SA_NODEFER | SA_SIGINFO);
135 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
136 SA_NODEFER | SA_SIGINFO);
139 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
142 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
143 SA_NODEFER | SA_SIGINFO);
146 # if defined(__ARM__) || defined(__S390__)
147 /* XXX use better defines for that (in arch.h) */
150 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
151 SA_NODEFER | SA_SIGINFO);
154 # if defined(__POWERPC__)
155 /* XXX use better defines for that (in arch.h) */
156 /* SIGTRAP handler */
158 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
159 SA_NODEFER | SA_SIGINFO);
161 # if defined(ENABLE_INTRP)
164 #endif /* !defined(ENABLE_INTRP) */
166 #if defined(ENABLE_THREADS)
167 /* SIGHUP handler for threads_thread_interrupt */
169 signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
172 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
173 /* SIGUSR2 handler for profiling sampling */
175 signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
179 #endif /* !defined(__CYGWIN__) */
185 /* signal_register_signal ******************************************************
187 Register the specified handler with the specified signal.
189 *******************************************************************************/
191 void signal_register_signal(int signum, functionptr handler, int flags)
193 struct sigaction act;
194 void (*function)(int, siginfo_t *, void *);
196 function = (void (*)(int, siginfo_t *, void *)) handler;
198 if (sigemptyset(&act.sa_mask) != 0)
199 vm_abort("signal_register_signal: sigemptyset failed: %s",
202 act.sa_sigaction = function;
203 act.sa_flags = flags;
205 if (sigaction(signum, &act, NULL) != 0)
206 vm_abort("signal_register_signal: sigaction failed: %s",
211 /* signal_handle ***************************************************************
213 Handles the signal caught by a signal handler and calls the correct
216 *******************************************************************************/
218 void *signal_handle(void *xpc, int type, intptr_t val)
225 case EXCEPTION_HARDWARE_NULLPOINTER:
226 p = exceptions_new_nullpointerexception();
229 case EXCEPTION_HARDWARE_ARITHMETIC:
230 p = exceptions_new_arithmeticexception();
233 case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
235 p = exceptions_new_arrayindexoutofboundsexception(index);
238 case EXCEPTION_HARDWARE_CLASSCAST:
239 o = (java_object_t *) val;
240 p = exceptions_new_classcastexception(o);
243 case EXCEPTION_HARDWARE_EXCEPTION:
244 p = exceptions_fillinstacktrace();
247 case EXCEPTION_HARDWARE_PATCHER:
248 #if defined(ENABLE_REPLACEMENT)
249 if (replace_me_wrapper(xpc)) {
254 p = patcher_handler(xpc);
258 /* Let's try to get a backtrace. */
260 codegen_get_pv_from_pc(xpc);
262 /* If that does not work, print more debug info. */
264 log_println("exceptions_new_hardware_exception: unknown exception type %d", type);
266 #if SIZEOF_VOID_P == 8
267 log_println("PC=0x%016lx", xpc);
269 log_println("PC=0x%08x", xpc);
272 #if defined(ENABLE_DISASSEMBLER)
273 log_println("machine instruction at PC:");
277 vm_abort("Exiting...");
279 /* keep compiler happy */
288 /* signal_thread ************************************************************
290 This thread sets the signal mask to catch the user input signals
291 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
292 signals on every single thread running.
294 *******************************************************************************/
296 static void signal_thread(void)
304 if (sigemptyset(&mask) != 0)
305 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
307 if (sigaddset(&mask, SIGINT) != 0)
308 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
310 #if !defined(__FREEBSD__)
311 if (sigaddset(&mask, SIGQUIT) != 0)
312 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
316 /* just wait for a signal */
318 /* XXX We don't check for an error here, although the man-page
319 states sigwait does not return an error (which is wrong!),
320 but it seems to make problems with Boehm-GC. We should
321 revisit this code with our new exact-GC. */
323 #if defined(ENABLE_THREADS)
324 threads_thread_state_waiting(t);
327 /* if (sigwait(&mask, &sig) != 0) */
328 /* vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
329 (void) sigwait(&mask, &sig);
331 #if defined(ENABLE_THREADS)
332 threads_thread_state_runnable(t);
337 /* exit the vm properly */
343 /* print a thread dump */
344 #if defined(ENABLE_THREADS)
348 #if defined(ENABLE_STATISTICS)
350 statistics_print_memory_usage();
356 /* this should not happen */
358 vm_abort("signal_thread: this thread should not exit!");
362 /* signal_start_thread *********************************************************
364 Starts the signal handler thread.
366 *******************************************************************************/
368 bool signal_start_thread(void)
370 #if defined(ENABLE_THREADS)
373 name = utf_new_char("Signal Handler");
375 if (!threads_thread_start_internal(name, signal_thread))
378 /* everything's ok */
387 /* signal_handler_sighup *******************************************************
389 This handler is required by threads_thread_interrupt and does
392 *******************************************************************************/
394 #if defined(ENABLE_THREADS)
395 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
403 * These are local overrides for various environment variables in Emacs.
404 * Please do not remove this and leave it at the end of the file, where
405 * Emacs will automagically detect them.
406 * ---------------------------------------------------------------------
409 * indent-tabs-mode: t