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 7831 2007-04-26 12:48:16Z 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"
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 void signal_init(void)
79 #if !defined(__CYGWIN__)
84 /* mmap a memory page at address 0x0, so our hardware-exceptions
87 pagesize = getpagesize();
89 (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
91 /* check if we get into trouble with our hardware-exceptions */
93 assert(OFFSET(java_bytearray, data) > EXCEPTION_HARDWARE_PATCHER);
95 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
96 <ctrl>-\). We enable them later in signal_thread, but only for
99 if (sigemptyset(&mask) != 0)
100 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
102 if (sigaddset(&mask, SIGINT) != 0)
103 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
105 #if !defined(__FREEBSD__)
106 if (sigaddset(&mask, SIGQUIT) != 0)
107 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
110 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
111 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
113 #if defined(ENABLE_GC_BOEHM)
114 /* Allocate something so the garbage collector's signal handlers
120 /* Install signal handlers for signals we want to catch in all
123 sigemptyset(&act.sa_mask);
125 #if defined(ENABLE_JIT)
126 # if defined(ENABLE_INTRP)
129 /* SIGSEGV handler */
131 act.sa_sigaction = md_signal_handler_sigsegv;
132 act.sa_flags = SA_NODEFER | SA_SIGINFO;
134 # if defined(SIGSEGV)
135 sigaction(SIGSEGV, &act, NULL);
139 sigaction(SIGBUS, &act, NULL);
142 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
145 act.sa_sigaction = md_signal_handler_sigfpe;
146 act.sa_flags = SA_NODEFER | SA_SIGINFO;
147 sigaction(SIGFPE, &act, NULL);
150 # if defined(__ARM__)
151 /* XXX use better defines for that (in arch.h) */
154 act.sa_sigaction = md_signal_handler_sigill;
155 act.sa_flags = SA_NODEFER | SA_SIGINFO;
156 sigaction(SIGILL, &act, NULL);
159 # if defined(__POWERPC__)
160 /* XXX use better defines for that (in arch.h) */
161 /* SIGTRAP handler */
163 act.sa_sigaction = md_signal_handler_sigtrap;
164 act.sa_flags = SA_NODEFER | SA_SIGINFO;
165 sigaction(SIGTRAP, &act, NULL);
167 # if defined(ENABLE_INTRP)
170 #endif /* !defined(ENABLE_INTRP) */
172 #if defined(ENABLE_THREADS)
173 /* SIGHUP handler for threads_thread_interrupt */
175 act.sa_sigaction = signal_handler_sighup;
177 sigaction(SIGHUP, &act, NULL);
180 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
181 /* SIGUSR2 handler for profiling sampling */
183 act.sa_sigaction = md_signal_handler_sigusr2;
184 act.sa_flags = SA_SIGINFO;
185 sigaction(SIGUSR2, &act, NULL);
188 #endif /* !defined(__CYGWIN__) */
192 /* signal_thread ************************************************************
194 This thread sets the signal mask to catch the user input signals
195 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
196 signals on every single thread running. Especially, this makes
197 problems on slow machines.
199 *******************************************************************************/
201 static void signal_thread(void)
206 if (sigemptyset(&mask) != 0)
207 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
209 if (sigaddset(&mask, SIGINT) != 0)
210 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
212 #if !defined(__FREEBSD__)
213 if (sigaddset(&mask, SIGQUIT) != 0)
214 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
218 /* just wait for a signal */
220 /* XXX We don't check for an error here, although the man-page
221 states sigwait does not return an error (which is wrong!),
222 but it seems to make problems with Boehm-GC. We should
223 revisit this code with our new exact-GC. */
225 /* if (sigwait(&mask, &sig) != 0) */
226 /* vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
227 (void) sigwait(&mask, &sig);
231 /* exit the vm properly */
237 /* print a thread dump */
238 #if defined(ENABLE_THREADS)
242 #if defined(ENABLE_STATISTICS)
244 statistics_print_memory_usage();
250 /* this should not happen */
252 vm_abort("signal_thread: this thread should not exit!");
256 /* signal_start_thread *********************************************************
258 Starts the signal handler thread.
260 *******************************************************************************/
262 bool signal_start_thread(void)
264 #if defined(ENABLE_THREADS)
267 name = utf_new_char("Signal Handler");
269 if (!threads_thread_start_internal(name, signal_thread))
272 /* everything's ok */
281 /* signal_handler_sighup *******************************************************
283 This handler is required by threads_thread_interrupt and does
286 *******************************************************************************/
288 #if defined(ENABLE_THREADS)
289 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
297 * These are local overrides for various environment variables in Emacs.
298 * Please do not remove this and leave it at the end of the file, where
299 * Emacs will automagically detect them.
300 * ---------------------------------------------------------------------
303 * indent-tabs-mode: t