f1f1b462b970606fc4ecfecbfb54ea29c6f4a9db
[cacao.git] / src / vm / signal.c
1 /* src/vm/signal.c - machine independent signal functions
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: signal.c 7831 2007-04-26 12:48:16Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/mman.h>
38
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>
43 #endif
44
45 #include "vm/types.h"
46
47 #include "arch.h"
48
49 #include "mm/memory.h"
50
51 #if defined(ENABLE_THREADS)
52 # include "threads/threads-common.h"
53 #endif
54
55 #include "vm/exceptions.h"
56 #include "vm/signallocal.h"
57 #include "vm/vm.h"
58
59 #include "vmcore/options.h"
60
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
63 #endif
64
65
66 /* function prototypes ********************************************************/
67
68 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
69
70
71 /* signal_init *****************************************************************
72
73    Initializes the signal subsystem and installs the signal handler.
74
75 *******************************************************************************/
76
77 void signal_init(void)
78 {
79 #if !defined(__CYGWIN__)
80         int              pagesize;
81         sigset_t         mask;
82         struct sigaction act;
83
84         /* mmap a memory page at address 0x0, so our hardware-exceptions
85            work. */
86
87         pagesize = getpagesize();
88
89         (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
90
91         /* check if we get into trouble with our hardware-exceptions */
92
93         assert(OFFSET(java_bytearray, data) > EXCEPTION_HARDWARE_PATCHER);
94
95         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
96            <ctrl>-\).  We enable them later in signal_thread, but only for
97            this thread. */
98
99         if (sigemptyset(&mask) != 0)
100                 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
101
102         if (sigaddset(&mask, SIGINT) != 0)
103                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
104
105 #if !defined(__FREEBSD__)
106         if (sigaddset(&mask, SIGQUIT) != 0)
107                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
108 #endif
109
110         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
111                 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
112
113 #if defined(ENABLE_GC_BOEHM)
114         /* Allocate something so the garbage collector's signal handlers
115            are installed. */
116
117         (void) GCNEW(u1);
118 #endif
119
120         /* Install signal handlers for signals we want to catch in all
121            threads. */
122
123         sigemptyset(&act.sa_mask);
124
125 #if defined(ENABLE_JIT)
126 # if defined(ENABLE_INTRP)
127         if (!opt_intrp) {
128 # endif
129                 /* SIGSEGV handler */
130
131                 act.sa_sigaction = md_signal_handler_sigsegv;
132                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
133
134 #  if defined(SIGSEGV)
135                 sigaction(SIGSEGV, &act, NULL);
136 #  endif
137
138 #  if defined(SIGBUS)
139                 sigaction(SIGBUS, &act, NULL);
140 #  endif
141
142 #  if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
143                 /* SIGFPE handler */
144
145                 act.sa_sigaction = md_signal_handler_sigfpe;
146                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
147                 sigaction(SIGFPE, &act, NULL);
148 #  endif
149
150 #  if defined(__ARM__)
151                 /* XXX use better defines for that (in arch.h) */
152                 /* SIGILL handler */
153
154                 act.sa_sigaction = md_signal_handler_sigill;
155                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
156                 sigaction(SIGILL, &act, NULL);
157 #  endif
158
159 #  if defined(__POWERPC__)
160                 /* XXX use better defines for that (in arch.h) */
161                 /* SIGTRAP handler */
162
163                 act.sa_sigaction = md_signal_handler_sigtrap;
164                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
165                 sigaction(SIGTRAP, &act, NULL);
166 #  endif
167 # if defined(ENABLE_INTRP)
168         }
169 # endif
170 #endif /* !defined(ENABLE_INTRP) */
171
172 #if defined(ENABLE_THREADS)
173         /* SIGHUP handler for threads_thread_interrupt */
174
175         act.sa_sigaction = signal_handler_sighup;
176         act.sa_flags     = 0;
177         sigaction(SIGHUP, &act, NULL);
178 #endif
179
180 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
181         /* SIGUSR2 handler for profiling sampling */
182
183         act.sa_sigaction = md_signal_handler_sigusr2;
184         act.sa_flags     = SA_SIGINFO;
185         sigaction(SIGUSR2, &act, NULL);
186 #endif
187
188 #endif /* !defined(__CYGWIN__) */
189 }
190
191
192 /* signal_thread ************************************************************
193
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.
198
199 *******************************************************************************/
200
201 static void signal_thread(void)
202 {
203         sigset_t mask;
204         int      sig;
205
206         if (sigemptyset(&mask) != 0)
207                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
208
209         if (sigaddset(&mask, SIGINT) != 0)
210                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
211
212 #if !defined(__FREEBSD__)
213         if (sigaddset(&mask, SIGQUIT) != 0)
214                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
215 #endif
216
217         while (true) {
218                 /* just wait for a signal */
219
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. */
224
225 /*              if (sigwait(&mask, &sig) != 0) */
226 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
227                 (void) sigwait(&mask, &sig);
228
229                 switch (sig) {
230                 case SIGINT:
231                         /* exit the vm properly */
232
233                         vm_exit(0);
234                         break;
235
236                 case SIGQUIT:
237                         /* print a thread dump */
238 #if defined(ENABLE_THREADS)
239                         threads_dump();
240 #endif
241
242 #if defined(ENABLE_STATISTICS)
243                         if (opt_stat)
244                                 statistics_print_memory_usage();
245 #endif
246                         break;
247                 }
248         }
249
250         /* this should not happen */
251
252         vm_abort("signal_thread: this thread should not exit!");
253 }
254
255
256 /* signal_start_thread *********************************************************
257
258    Starts the signal handler thread.
259
260 *******************************************************************************/
261
262 bool signal_start_thread(void)
263 {
264 #if defined(ENABLE_THREADS)
265         utf *name;
266
267         name = utf_new_char("Signal Handler");
268
269         if (!threads_thread_start_internal(name, signal_thread))
270                 return false;
271
272         /* everything's ok */
273
274         return true;
275 #else
276 #warning FIX ME!
277 #endif
278 }
279
280
281 /* signal_handler_sighup *******************************************************
282
283    This handler is required by threads_thread_interrupt and does
284    nothing.
285
286 *******************************************************************************/
287
288 #if defined(ENABLE_THREADS)
289 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
290 {
291         /* do nothing */
292 }
293 #endif
294
295
296 /*
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  * ---------------------------------------------------------------------
301  * Local variables:
302  * mode: c
303  * indent-tabs-mode: t
304  * c-basic-offset: 4
305  * tab-width: 4
306  * End:
307  */