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