* m4/classpath.m4: Renamed --with-classpath* options to
[cacao.git] / src / vm / signal.c
1 /* src/vm/signal.c - machine independent signal functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32
33 #if defined(__DARWIN__)
34 /* If we compile with -ansi on darwin, <sys/types.h> is not
35  included. So let's do it here. */
36 # include <sys/types.h>
37 # include <sys/utsname.h>
38 #endif
39
40 #include "arch.h"
41
42 #include "threads/thread.h"
43
44 #include "vm/exceptions.h"
45 #include "vm/signallocal.h"
46 #include "vm/vm.h"
47
48 #include "vmcore/options.h"
49
50 #if defined(ENABLE_STATISTICS)
51 # include "vmcore/statistics.h"
52 #endif
53
54
55 /* function prototypes ********************************************************/
56
57 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
58
59
60 /* signal_init *****************************************************************
61
62    Initializes the signal subsystem and installs the signal handler.
63
64 *******************************************************************************/
65
66 bool signal_init(void)
67 {
68 #if !defined(__CYGWIN__)
69         sigset_t mask;
70
71         TRACESUBSYSTEMINITIALIZATION("signal_init");
72
73 #if defined(__LINUX__) && defined(ENABLE_THREADS)
74         /* XXX Remove for exact-GC. */
75         if (threads_pthreads_implementation_nptl) {
76 #endif
77
78         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
79            <ctrl>-\).  We enable them later in signal_thread, but only for
80            this thread. */
81
82         if (sigemptyset(&mask) != 0)
83                 vm_abort_errno("signal_init: sigemptyset failed");
84
85 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
86         /* Let OpenJDK handle SIGINT itself. */
87
88         if (sigaddset(&mask, SIGINT) != 0)
89                 vm_abort_errno("signal_init: sigaddset failed");
90 #endif
91
92 #if !defined(__FREEBSD__)
93         if (sigaddset(&mask, SIGQUIT) != 0)
94                 vm_abort_errno("signal_init: sigaddset failed");
95 #endif
96
97         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
98                 vm_abort_errno("signal_init: sigprocmask failed");
99
100 #if defined(__LINUX__) && defined(ENABLE_THREADS)
101         /* XXX Remove for exact-GC. */
102         }
103 #endif
104
105 #if defined(ENABLE_GC_BOEHM)
106         /* Allocate something so the garbage collector's signal handlers
107            are installed. */
108
109         (void) GCNEW(int);
110 #endif
111
112         /* Install signal handlers for signals we want to catch in all
113            threads. */
114
115 #if defined(ENABLE_JIT)
116 # if defined(ENABLE_INTRP)
117         if (!opt_intrp) {
118 # endif
119                 /* SIGSEGV handler */
120
121                 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
122                                                            SA_NODEFER | SA_SIGINFO);
123
124 #  if defined(SIGBUS)
125                 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
126                                                            SA_NODEFER | SA_SIGINFO);
127 #  endif
128
129 #  if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
130                 /* SIGFPE handler */
131
132                 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
133                                                            SA_NODEFER | SA_SIGINFO);
134 #  endif
135
136 #  if defined(__ARM__) || defined(__I386__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
137                 /* XXX use better defines for that (in arch.h) */
138                 /* SIGILL handler */
139
140                 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
141                                                            SA_NODEFER | SA_SIGINFO);
142 #  endif
143
144 #  if defined(__POWERPC__)
145                 /* XXX use better defines for that (in arch.h) */
146                 /* SIGTRAP handler */
147
148                 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
149                                                            SA_NODEFER | SA_SIGINFO);
150 #  endif
151 # if defined(ENABLE_INTRP)
152         }
153 # endif
154
155 #if defined(__DARWIN__)
156         do {
157                 struct utsname name;
158                 kern_return_t kr;
159
160                 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
161                 if (uname(&name) != 0) 
162                         break;
163
164                 /* Make sure the string is large enough */
165                 /* Check the major number (ascii comparison) */
166                 /* Verify that we're not looking at '10.' by checking for a trailing period. */
167                 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
168                         break;
169
170                 /* Reset CrashReporter's task signal handler */
171                 kr = task_set_exception_ports(mach_task_self(),
172                                                                           EXC_MASK_BAD_ACCESS
173 #  if defined(__I386__)
174                                                                           | EXC_MASK_BAD_INSTRUCTION
175 #endif
176                                                                           , MACH_PORT_NULL,
177                                                                           EXCEPTION_STATE_IDENTITY,
178                                                                           MACHINE_THREAD_STATE);
179
180                 assert(kr == KERN_SUCCESS);
181         } while (false);
182 #endif
183 #endif /* !defined(ENABLE_JIT) */
184
185 #if defined(ENABLE_THREADS)
186         /* SIGHUP handler for threads_thread_interrupt */
187
188         signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
189 #endif
190
191 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
192         /* SIGUSR1 handler for the exact GC to suspend threads */
193
194         signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
195                                                    SA_SIGINFO);
196 #endif
197
198 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
199         /* SIGUSR2 handler for profiling sampling */
200
201         signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
202                                                    SA_SIGINFO);
203 #endif
204
205 #endif /* !defined(__CYGWIN__) */
206
207         return true;
208 }
209
210
211 /* signal_register_signal ******************************************************
212
213    Register the specified handler with the specified signal.
214
215 *******************************************************************************/
216
217 void signal_register_signal(int signum, functionptr handler, int flags)
218 {
219         struct sigaction act;
220
221         void (*function)(int, siginfo_t *, void *);
222
223         function = (void (*)(int, siginfo_t *, void *)) handler;
224
225         if (sigemptyset(&act.sa_mask) != 0)
226                 vm_abort_errno("signal_register_signal: sigemptyset failed");
227
228         act.sa_sigaction = function;
229         act.sa_flags     = flags;
230
231         if (sigaction(signum, &act, NULL) != 0)
232                 vm_abort_errno("signal_register_signal: sigaction failed");
233 }
234
235
236 /* signal_thread ************************************************************
237
238    This thread sets the signal mask to catch the user input signals
239    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
240    signals on every single thread running.
241
242 *******************************************************************************/
243
244 static void signal_thread(void)
245 {
246         threadobject *t;
247         sigset_t      mask;
248         int           sig;
249
250         t = THREADOBJECT;
251
252         if (sigemptyset(&mask) != 0)
253                 vm_abort_errno("signal_thread: sigemptyset failed");
254
255 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
256         /* Let OpenJDK handle SIGINT itself. */
257
258         if (sigaddset(&mask, SIGINT) != 0)
259                 vm_abort_errno("signal_thread: sigaddset failed");
260 #endif
261
262 #if !defined(__FREEBSD__)
263         if (sigaddset(&mask, SIGQUIT) != 0)
264                 vm_abort_errno("signal_thread: sigaddset failed");
265 #endif
266
267         for (;;) {
268                 /* just wait for a signal */
269
270 #if defined(ENABLE_THREADS)
271                 thread_set_state_waiting(t);
272 #endif
273
274                 /* XXX We don't check for an error here, although the man-page
275                    states sigwait does not return an error (which is wrong!),
276                    but it seems to make problems with Boehm-GC.  We should
277                    revisit this code with our new exact-GC. */
278
279 /*              if (sigwait(&mask, &sig) != 0) */
280 /*                      vm_abort_errno("signal_thread: sigwait failed"); */
281                 (void) sigwait(&mask, &sig);
282
283 #if defined(ENABLE_THREADS)
284                 thread_set_state_runnable(t);
285 #endif
286
287                 /* Handle the signal. */
288
289                 signal_thread_handler(sig);
290         }
291 }
292
293
294 /* signal_thread_handler *******************************************************
295
296    Handles the signals caught in the signal handler thread.  Also used
297    from sun.misc.Signal with OpenJDK.
298
299 *******************************************************************************/
300
301 void signal_thread_handler(int sig)
302 {
303         switch (sig) {
304         case SIGINT:
305                 /* exit the vm properly */
306
307                 vm_exit(0);
308                 break;
309
310         case SIGQUIT:
311                 /* print a thread dump */
312 #if defined(ENABLE_THREADS)
313                 threads_dump();
314 #endif
315
316 #if defined(ENABLE_STATISTICS)
317                 if (opt_stat)
318                         statistics_print_memory_usage();
319 #endif
320                 break;
321         }
322 }
323
324
325 /* signal_start_thread *********************************************************
326
327    Starts the signal handler thread.
328
329 *******************************************************************************/
330
331 bool signal_start_thread(void)
332 {
333 #if defined(ENABLE_THREADS)
334         utf *name;
335
336         name = utf_new_char("Signal Handler");
337
338         if (!threads_thread_start_internal(name, signal_thread))
339                 return false;
340
341         /* everything's ok */
342
343         return true;
344 #else
345 #warning FIX ME!
346 #endif
347 }
348
349
350 /* signal_handler_sighup *******************************************************
351
352    This handler is required by threads_thread_interrupt and does
353    nothing.
354
355 *******************************************************************************/
356
357 #if defined(ENABLE_THREADS)
358 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
359 {
360         /* do nothing */
361 }
362 #endif
363
364
365 /*
366  * These are local overrides for various environment variables in Emacs.
367  * Please do not remove this and leave it at the end of the file, where
368  * Emacs will automagically detect them.
369  * ---------------------------------------------------------------------
370  * Local variables:
371  * mode: c
372  * indent-tabs-mode: t
373  * c-basic-offset: 4
374  * tab-width: 4
375  * End:
376  */