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