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 8027 2007-06-07 10:30:33Z michi $
37 #if defined(__DARWIN__)
38 /* If we compile with -ansi on darwin, <sys/types.h> is not
39 included. So let's do it here. */
40 # include <sys/types.h>
47 #include "mm/memory.h"
49 #if defined(ENABLE_THREADS)
50 # include "threads/threads-common.h"
52 # include "threads/none/threads.h"
55 #include "vm/exceptions.h"
56 #include "vm/signallocal.h"
59 #include "vmcore/options.h"
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
66 /* function prototypes ********************************************************/
68 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
71 /* signal_init *****************************************************************
73 Initializes the signal subsystem and installs the signal handler.
75 *******************************************************************************/
77 bool signal_init(void)
79 #if !defined(__CYGWIN__)
83 #if defined(__LINUX__) && defined(ENABLE_THREADS)
84 /* XXX Remove for exact-GC. */
85 if (threads_pthreads_implementation_nptl) {
88 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
89 <ctrl>-\). We enable them later in signal_thread, but only for
92 if (sigemptyset(&mask) != 0)
93 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
95 if (sigaddset(&mask, SIGINT) != 0)
96 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
98 #if !defined(__FREEBSD__)
99 if (sigaddset(&mask, SIGQUIT) != 0)
100 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
103 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
104 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
106 #if defined(__LINUX__) && defined(ENABLE_THREADS)
107 /* XXX Remove for exact-GC. */
111 #if defined(ENABLE_GC_BOEHM)
112 /* Allocate something so the garbage collector's signal handlers
118 /* Install signal handlers for signals we want to catch in all
121 sigemptyset(&act.sa_mask);
123 #if defined(ENABLE_JIT)
124 # if defined(ENABLE_INTRP)
127 /* SIGSEGV handler */
129 act.sa_sigaction = md_signal_handler_sigsegv;
130 act.sa_flags = SA_NODEFER | SA_SIGINFO;
132 # if defined(SIGSEGV)
133 sigaction(SIGSEGV, &act, NULL);
137 sigaction(SIGBUS, &act, NULL);
140 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
143 act.sa_sigaction = md_signal_handler_sigfpe;
144 act.sa_flags = SA_NODEFER | SA_SIGINFO;
145 sigaction(SIGFPE, &act, NULL);
148 # if defined(__ARM__) || defined(__S390__)
149 /* XXX use better defines for that (in arch.h) */
152 act.sa_sigaction = md_signal_handler_sigill;
153 act.sa_flags = SA_NODEFER | SA_SIGINFO;
154 sigaction(SIGILL, &act, NULL);
157 # if defined(__POWERPC__)
158 /* XXX use better defines for that (in arch.h) */
159 /* SIGTRAP handler */
161 act.sa_sigaction = md_signal_handler_sigtrap;
162 act.sa_flags = SA_NODEFER | SA_SIGINFO;
163 sigaction(SIGTRAP, &act, NULL);
165 # if defined(ENABLE_INTRP)
168 #endif /* !defined(ENABLE_INTRP) */
170 #if defined(ENABLE_THREADS)
171 /* SIGHUP handler for threads_thread_interrupt */
173 act.sa_sigaction = signal_handler_sighup;
175 sigaction(SIGHUP, &act, NULL);
178 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
179 /* SIGUSR1 handler for the exact GC to suspend threads */
181 act.sa_sigaction = md_signal_handler_sigusr1;
182 act.sa_flags = SA_SIGINFO;
183 sigaction(SIGUSR1, &act, NULL);
186 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
187 /* SIGUSR2 handler for profiling sampling */
189 act.sa_sigaction = md_signal_handler_sigusr2;
190 act.sa_flags = SA_SIGINFO;
191 sigaction(SIGUSR2, &act, NULL);
194 #endif /* !defined(__CYGWIN__) */
200 /* signal_thread ************************************************************
202 This thread sets the signal mask to catch the user input signals
203 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
204 signals on every single thread running.
206 *******************************************************************************/
208 static void signal_thread(void)
216 if (sigemptyset(&mask) != 0)
217 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
219 if (sigaddset(&mask, SIGINT) != 0)
220 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
222 #if !defined(__FREEBSD__)
223 if (sigaddset(&mask, SIGQUIT) != 0)
224 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
228 /* just wait for a signal */
230 /* XXX We don't check for an error here, although the man-page
231 states sigwait does not return an error (which is wrong!),
232 but it seems to make problems with Boehm-GC. We should
233 revisit this code with our new exact-GC. */
235 #if defined(ENABLE_THREADS)
236 threads_thread_state_waiting(t);
239 /* if (sigwait(&mask, &sig) != 0) */
240 /* vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
241 (void) sigwait(&mask, &sig);
243 #if defined(ENABLE_THREADS)
244 threads_thread_state_runnable(t);
249 /* exit the vm properly */
255 /* print a thread dump */
256 #if defined(ENABLE_THREADS)
260 #if defined(ENABLE_STATISTICS)
262 statistics_print_memory_usage();
268 /* this should not happen */
270 vm_abort("signal_thread: this thread should not exit!");
274 /* signal_start_thread *********************************************************
276 Starts the signal handler thread.
278 *******************************************************************************/
280 bool signal_start_thread(void)
282 #if defined(ENABLE_THREADS)
285 name = utf_new_char("Signal Handler");
287 if (!threads_thread_start_internal(name, signal_thread))
290 /* everything's ok */
299 /* signal_handler_sighup *******************************************************
301 This handler is required by threads_thread_interrupt and does
304 *******************************************************************************/
306 #if defined(ENABLE_THREADS)
307 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
315 * These are local overrides for various environment variables in Emacs.
316 * Please do not remove this and leave it at the end of the file, where
317 * Emacs will automagically detect them.
318 * ---------------------------------------------------------------------
321 * indent-tabs-mode: t