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 8004 2007-06-04 12:59:04Z twisti $
39 #if defined(__DARWIN__)
40 /* If we compile with -ansi on darwin, <sys/types.h> is not
41 included. So let's do it here. */
42 # include <sys/types.h>
49 #include "mm/memory.h"
51 #if defined(ENABLE_THREADS)
52 # include "threads/threads-common.h"
54 # include "threads/none/threads.h"
57 #include "vm/exceptions.h"
58 #include "vm/signallocal.h"
61 #include "vmcore/options.h"
63 #if defined(ENABLE_STATISTICS)
64 # include "vmcore/statistics.h"
68 /* function prototypes ********************************************************/
70 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
73 /* signal_init *****************************************************************
75 Initializes the signal subsystem and installs the signal handler.
77 *******************************************************************************/
79 bool signal_init(void)
81 #if !defined(__CYGWIN__)
86 /* mmap a memory page at address 0x0, so our hardware-exceptions
89 pagesize = getpagesize();
91 (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
93 /* check if we get into trouble with our hardware-exceptions */
95 if (OFFSET(java_bytearray, data) <= EXCEPTION_HARDWARE_PATCHER)
96 vm_abort("signal_init: array-data offset is less or equal the maximum hardware-exception displacement: %d <= %d", OFFSET(java_bytearray, data) <= EXCEPTION_HARDWARE_PATCHER);
98 #if defined(__LINUX__) && defined(ENABLE_THREADS)
99 /* XXX Remove for exact-GC. */
100 if (threads_pthreads_implementation_nptl) {
103 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
104 <ctrl>-\). We enable them later in signal_thread, but only for
107 if (sigemptyset(&mask) != 0)
108 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
110 if (sigaddset(&mask, SIGINT) != 0)
111 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
113 #if !defined(__FREEBSD__)
114 if (sigaddset(&mask, SIGQUIT) != 0)
115 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
118 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
119 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
121 #if defined(__LINUX__) && defined(ENABLE_THREADS)
122 /* XXX Remove for exact-GC. */
126 #if defined(ENABLE_GC_BOEHM)
127 /* Allocate something so the garbage collector's signal handlers
133 /* Install signal handlers for signals we want to catch in all
136 sigemptyset(&act.sa_mask);
138 #if defined(ENABLE_JIT)
139 # if defined(ENABLE_INTRP)
142 /* SIGSEGV handler */
144 act.sa_sigaction = md_signal_handler_sigsegv;
145 act.sa_flags = SA_NODEFER | SA_SIGINFO;
147 # if defined(SIGSEGV)
148 sigaction(SIGSEGV, &act, NULL);
152 sigaction(SIGBUS, &act, NULL);
155 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
158 act.sa_sigaction = md_signal_handler_sigfpe;
159 act.sa_flags = SA_NODEFER | SA_SIGINFO;
160 sigaction(SIGFPE, &act, NULL);
163 # if defined(__ARM__) || defined(__S390__)
164 /* XXX use better defines for that (in arch.h) */
167 act.sa_sigaction = md_signal_handler_sigill;
168 act.sa_flags = SA_NODEFER | SA_SIGINFO;
169 sigaction(SIGILL, &act, NULL);
172 # if defined(__POWERPC__)
173 /* XXX use better defines for that (in arch.h) */
174 /* SIGTRAP handler */
176 act.sa_sigaction = md_signal_handler_sigtrap;
177 act.sa_flags = SA_NODEFER | SA_SIGINFO;
178 sigaction(SIGTRAP, &act, NULL);
180 # if defined(ENABLE_INTRP)
183 #endif /* !defined(ENABLE_INTRP) */
185 #if defined(ENABLE_THREADS)
186 /* SIGHUP handler for threads_thread_interrupt */
188 act.sa_sigaction = signal_handler_sighup;
190 sigaction(SIGHUP, &act, NULL);
193 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
194 /* SIGUSR2 handler for profiling sampling */
196 act.sa_sigaction = md_signal_handler_sigusr2;
197 act.sa_flags = SA_SIGINFO;
198 sigaction(SIGUSR2, &act, NULL);
201 #endif /* !defined(__CYGWIN__) */
207 /* signal_thread ************************************************************
209 This thread sets the signal mask to catch the user input signals
210 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
211 signals on every single thread running.
213 *******************************************************************************/
215 static void signal_thread(void)
223 if (sigemptyset(&mask) != 0)
224 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
226 if (sigaddset(&mask, SIGINT) != 0)
227 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
229 #if !defined(__FREEBSD__)
230 if (sigaddset(&mask, SIGQUIT) != 0)
231 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
235 /* just wait for a signal */
237 /* XXX We don't check for an error here, although the man-page
238 states sigwait does not return an error (which is wrong!),
239 but it seems to make problems with Boehm-GC. We should
240 revisit this code with our new exact-GC. */
242 #if defined(ENABLE_THREADS)
243 threads_thread_state_waiting(t);
246 /* if (sigwait(&mask, &sig) != 0) */
247 /* vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
248 (void) sigwait(&mask, &sig);
250 #if defined(ENABLE_THREADS)
251 threads_thread_state_runnable(t);
256 /* exit the vm properly */
262 /* print a thread dump */
263 #if defined(ENABLE_THREADS)
267 #if defined(ENABLE_STATISTICS)
269 statistics_print_memory_usage();
275 /* this should not happen */
277 vm_abort("signal_thread: this thread should not exit!");
281 /* signal_start_thread *********************************************************
283 Starts the signal handler thread.
285 *******************************************************************************/
287 bool signal_start_thread(void)
289 #if defined(ENABLE_THREADS)
292 name = utf_new_char("Signal Handler");
294 if (!threads_thread_start_internal(name, signal_thread))
297 /* everything's ok */
306 /* signal_handler_sighup *******************************************************
308 This handler is required by threads_thread_interrupt and does
311 *******************************************************************************/
313 #if defined(ENABLE_THREADS)
314 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
322 * These are local overrides for various environment variables in Emacs.
323 * Please do not remove this and leave it at the end of the file, where
324 * Emacs will automagically detect them.
325 * ---------------------------------------------------------------------
328 * indent-tabs-mode: t