Merged revisions 7797-7917 via svnmerge from
[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 7918 2007-05-20 20:42:18Z michi $
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_GC_CACAO)
181         /* SIGUSR1 handler for the exact GC to suspend threads */
182
183         act.sa_sigaction = md_signal_handler_sigusr1;
184         act.sa_flags     = SA_SIGINFO;
185         sigaction(SIGUSR1, &act, NULL);
186 #endif
187
188 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
189         /* SIGUSR2 handler for profiling sampling */
190
191         act.sa_sigaction = md_signal_handler_sigusr2;
192         act.sa_flags     = SA_SIGINFO;
193         sigaction(SIGUSR2, &act, NULL);
194 #endif
195
196 #endif /* !defined(__CYGWIN__) */
197 }
198
199
200 /* signal_thread ************************************************************
201
202    This thread sets the signal mask to catch the user input signals
203    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
204    signals on every single thread running.  Especially, this makes
205    problems on slow machines.
206
207 *******************************************************************************/
208
209 static void signal_thread(void)
210 {
211         sigset_t mask;
212         int      sig;
213
214         if (sigemptyset(&mask) != 0)
215                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
216
217         if (sigaddset(&mask, SIGINT) != 0)
218                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
219
220 #if !defined(__FREEBSD__)
221         if (sigaddset(&mask, SIGQUIT) != 0)
222                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
223 #endif
224
225         while (true) {
226                 /* just wait for a signal */
227
228                 /* XXX We don't check for an error here, although the man-page
229                    states sigwait does not return an error (which is wrong!),
230                    but it seems to make problems with Boehm-GC.  We should
231                    revisit this code with our new exact-GC. */
232
233 /*              if (sigwait(&mask, &sig) != 0) */
234 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
235                 (void) sigwait(&mask, &sig);
236
237                 switch (sig) {
238                 case SIGINT:
239                         /* exit the vm properly */
240
241                         vm_exit(0);
242                         break;
243
244                 case SIGQUIT:
245                         /* print a thread dump */
246 #if defined(ENABLE_THREADS)
247                         threads_dump();
248 #endif
249
250 #if defined(ENABLE_STATISTICS)
251                         if (opt_stat)
252                                 statistics_print_memory_usage();
253 #endif
254                         break;
255                 }
256         }
257
258         /* this should not happen */
259
260         vm_abort("signal_thread: this thread should not exit!");
261 }
262
263
264 /* signal_start_thread *********************************************************
265
266    Starts the signal handler thread.
267
268 *******************************************************************************/
269
270 bool signal_start_thread(void)
271 {
272 #if defined(ENABLE_THREADS)
273         utf *name;
274
275         name = utf_new_char("Signal Handler");
276
277         if (!threads_thread_start_internal(name, signal_thread))
278                 return false;
279
280         /* everything's ok */
281
282         return true;
283 #else
284 #warning FIX ME!
285 #endif
286 }
287
288
289 /* signal_handler_sighup *******************************************************
290
291    This handler is required by threads_thread_interrupt and does
292    nothing.
293
294 *******************************************************************************/
295
296 #if defined(ENABLE_THREADS)
297 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
298 {
299         /* do nothing */
300 }
301 #endif
302
303
304 /*
305  * These are local overrides for various environment variables in Emacs.
306  * Please do not remove this and leave it at the end of the file, where
307  * Emacs will automagically detect them.
308  * ---------------------------------------------------------------------
309  * Local variables:
310  * mode: c
311  * indent-tabs-mode: t
312  * c-basic-offset: 4
313  * tab-width: 4
314  * End:
315  */