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 7601 2007-03-28 23:02:50Z michi $
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>
48 #include "mm/memory.h"
50 #if defined(ENABLE_THREADS)
51 # include "threads/threads-common.h"
54 #include "vm/signallocal.h"
57 #include "vmcore/options.h"
60 /* global variables ***********************************************************/
62 #if defined(ENABLE_THREADS)
63 static threadobject *thread_signal;
67 /* function prototypes ********************************************************/
69 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
72 /* signal_init *****************************************************************
74 Initializes the signal subsystem and installs the signal handler.
76 *******************************************************************************/
78 void signal_init(void)
80 #if !defined(__CYGWIN__)
85 /* mmap a memory page at address 0x0, so our hardware-exceptions
88 pagesize = getpagesize();
90 (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
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(ENABLE_GC_BOEHM)
111 /* Allocate something so the garbage collector's signal handlers
117 /* Install signal handlers for signals we want to catch in all
120 sigemptyset(&act.sa_mask);
122 #if defined(ENABLE_JIT)
123 # if defined(ENABLE_INTRP)
126 /* SIGSEGV handler */
128 act.sa_sigaction = md_signal_handler_sigsegv;
129 act.sa_flags = SA_NODEFER | SA_SIGINFO;
131 # if defined(SIGSEGV)
132 sigaction(SIGSEGV, &act, NULL);
136 sigaction(SIGBUS, &act, NULL);
139 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
142 act.sa_sigaction = md_signal_handler_sigfpe;
143 act.sa_flags = SA_NODEFER | SA_SIGINFO;
144 sigaction(SIGFPE, &act, NULL);
147 # if defined(__ARM__)
148 /* XXX use better defines for that (in arch.h) */
151 act.sa_sigaction = md_signal_handler_sigill;
152 act.sa_flags = SA_NODEFER | SA_SIGINFO;
153 sigaction(SIGILL, &act, NULL);
156 # if defined(__POWERPC__)
157 /* XXX use better defines for that (in arch.h) */
158 /* SIGTRAP handler */
160 act.sa_sigaction = md_signal_handler_sigtrap;
161 act.sa_flags = SA_NODEFER | SA_SIGINFO;
162 sigaction(SIGTRAP, &act, NULL);
164 # if defined(ENABLE_INTRP)
167 #endif /* !defined(ENABLE_INTRP) */
169 #if defined(ENABLE_THREADS)
170 /* SIGHUP handler for threads_thread_interrupt */
172 act.sa_sigaction = signal_handler_sighup;
174 sigaction(SIGHUP, &act, NULL);
177 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
178 /* SIGUSR1 handler for the exact GC to suspend threads */
180 act.sa_sigaction = md_signal_handler_sigusr1;
181 act.sa_flags = SA_SIGINFO;
182 sigaction(SIGUSR1, &act, NULL);
185 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
186 /* SIGUSR2 handler for profiling sampling */
188 act.sa_sigaction = md_signal_handler_sigusr2;
189 act.sa_flags = SA_SIGINFO;
190 sigaction(SIGUSR2, &act, NULL);
193 #endif /* !defined(__CYGWIN__) */
197 /* signal_thread ************************************************************
199 This thread sets the signal mask to catch the user input signals
200 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
201 signals on every single thread running. Especially, this makes
202 problems on slow machines.
204 *******************************************************************************/
206 static void signal_thread(void)
211 if (sigemptyset(&mask) != 0)
212 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
214 sigaddset(&mask, SIGINT);
215 #if !defined(__FREEBSD__)
216 sigaddset(&mask, SIGQUIT);
220 /* just wait for a signal */
222 (void) sigwait(&mask, &sig);
226 /* exit the vm properly */
232 /* print a thread dump */
233 #if defined(ENABLE_THREADS)
237 #if defined(ENABLE_STATISTICS)
239 statistics_print_memory_usage();
245 /* this should not happen */
247 vm_abort("signal_thread: this thread should not exit!");
251 /* signal_start_thread *********************************************************
253 Starts the signal handler thread.
255 *******************************************************************************/
257 bool signal_start_thread(void)
259 #if defined(ENABLE_THREADS)
262 name = utf_new_char("Signal Handler");
264 thread_signal = threads_create_thread(name);
266 if (thread_signal == NULL)
269 /* actually start the signal handler thread */
271 threads_start_thread(thread_signal, signal_thread);
273 /* everything's ok */
282 /* signal_handler_sighup *******************************************************
284 This handler is required by threads_thread_interrupt and does
287 *******************************************************************************/
289 #if defined(ENABLE_THREADS)
290 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
298 * These are local overrides for various environment variables in Emacs.
299 * Please do not remove this and leave it at the end of the file, where
300 * Emacs will automagically detect them.
301 * ---------------------------------------------------------------------
304 * indent-tabs-mode: t