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 7486 2007-03-08 13:50:07Z 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>
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;
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);
146 # if defined(ENABLE_INTRP)
149 #endif /* !defined(ENABLE_INTRP) */
151 #if defined(ENABLE_THREADS)
152 /* SIGHUP handler for threads_thread_interrupt */
154 act.sa_sigaction = signal_handler_sighup;
156 sigaction(SIGHUP, &act, NULL);
159 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
160 /* SIGUSR2 handler for profiling sampling */
162 act.sa_sigaction = md_signal_handler_sigusr2;
163 act.sa_flags = SA_SIGINFO;
164 sigaction(SIGUSR2, &act, NULL);
167 #endif /* !defined(__CYGWIN__) */
171 /* signal_thread ************************************************************
173 This thread sets the signal mask to catch the user input signals
174 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
175 signals on every single thread running. Especially, this makes
176 problems on slow machines.
178 *******************************************************************************/
180 static void signal_thread(void)
185 if (sigemptyset(&mask) != 0)
186 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
188 sigaddset(&mask, SIGINT);
189 #if !defined(__FREEBSD__)
190 sigaddset(&mask, SIGQUIT);
194 /* just wait for a signal */
196 (void) sigwait(&mask, &sig);
200 /* exit the vm properly */
206 /* print a thread dump */
207 #if defined(ENABLE_THREADS)
211 #if defined(ENABLE_STATISTICS)
213 statistics_print_memory_usage();
219 /* this should not happen */
221 vm_abort("signal_thread: this thread should not exit!");
225 /* signal_start_thread *********************************************************
227 Starts the signal handler thread.
229 *******************************************************************************/
231 bool signal_start_thread(void)
233 #if defined(ENABLE_THREADS)
236 name = utf_new_char("Signal Handler");
238 thread_signal = threads_create_thread(name);
240 if (thread_signal == NULL)
243 /* actually start the signal handler thread */
245 threads_start_thread(thread_signal, signal_thread);
247 /* everything's ok */
256 /* signal_handler_sighup *******************************************************
258 This handler is required by threads_thread_interrupt and does
261 *******************************************************************************/
263 #if defined(ENABLE_THREADS)
264 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
272 * These are local overrides for various environment variables in Emacs.
273 * Please do not remove this and leave it at the end of the file, where
274 * Emacs will automagically detect them.
275 * ---------------------------------------------------------------------
278 * indent-tabs-mode: t