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 7343 2007-02-13 02:36:29Z ajordan $
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>
46 #include "mm/memory.h"
48 #if defined(ENABLE_THREADS)
49 # include "threads/threads-common.h"
52 #include "vm/signallocal.h"
54 #include "mm/memory.h"
56 #include "vmcore/options.h"
59 /* global variables ***********************************************************/
61 #if defined(ENABLE_THREADS)
62 static threadobject *thread_signal;
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);
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));
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;
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);
147 # if defined(ENABLE_INTRP)
150 #endif /* !defined(ENABLE_INTRP) */
152 #if defined(ENABLE_THREADS)
153 /* SIGHUP handler for threads_thread_interrupt */
155 act.sa_sigaction = signal_handler_sighup;
157 sigaction(SIGHUP, &act, NULL);
160 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
161 /* SIGUSR2 handler for profiling sampling */
163 act.sa_sigaction = md_signal_handler_sigusr2;
164 act.sa_flags = SA_SIGINFO;
165 sigaction(SIGUSR2, &act, NULL);
168 #endif /* !defined(__CYGWIN__) */
172 /* signal_thread ************************************************************
174 This thread sets the signal mask to catch the user input signals
175 (SIGINT, SIGQUIT). We use such a thread, so we don't get the
176 signals on every single thread running. Especially, this makes
177 problems on slow machines.
179 *******************************************************************************/
181 static void signal_thread(void)
186 if (sigemptyset(&mask) != 0)
187 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
189 sigaddset(&mask, SIGINT);
190 #if !defined(__FREEBSD__)
191 sigaddset(&mask, SIGQUIT);
195 /* just wait for a signal */
197 (void) sigwait(&mask, &sig);
201 /* exit the vm properly */
207 /* print a thread dump */
208 #if defined(ENABLE_THREADS)
212 #if defined(ENABLE_STATISTICS)
214 statistics_print_memory_usage();
220 /* this should not happen */
222 vm_abort("signal_thread: this thread should not exit!");
226 /* signal_start_thread *********************************************************
228 Starts the signal handler thread.
230 *******************************************************************************/
232 bool signal_start_thread(void)
234 #if defined(ENABLE_THREADS)
237 name = utf_new_char("Signal Handler");
239 thread_signal = threads_create_thread(name);
241 if (thread_signal == NULL)
244 /* actually start the signal handler thread */
246 threads_start_thread(thread_signal, signal_thread);
248 /* everything's ok */
257 /* signal_handler_sighup *******************************************************
259 This handler is required by threads_thread_interrupt and does
262 *******************************************************************************/
264 #if defined(ENABLE_THREADS)
265 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
273 * These are local overrides for various environment variables in Emacs.
274 * Please do not remove this and leave it at the end of the file, where
275 * Emacs will automagically detect them.
276 * ---------------------------------------------------------------------
279 * indent-tabs-mode: t