* Merged executionstate branch.
[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 "mm/memory.h"
43
44 #include "native/llni.h"
45
46 #include "threads/thread.h"
47
48 #include "toolbox/logging.h"
49
50 #include "vm/exceptions.h"
51 #include "vm/signallocal.h"
52 #include "vm/vm.h"
53
54 #include "vm/jit/codegen-common.h"
55 #include "vm/jit/disass.h"
56 #include "vm/jit/methodtree.h"
57 #include "vm/jit/patcher-common.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 bool signal_init(void)
78 {
79 #if !defined(__CYGWIN__)
80         sigset_t mask;
81
82         TRACESUBSYSTEMINITIALIZATION("signal_init");
83
84 #if defined(__LINUX__) && defined(ENABLE_THREADS)
85         /* XXX Remove for exact-GC. */
86         if (threads_pthreads_implementation_nptl) {
87 #endif
88
89         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
90            <ctrl>-\).  We enable them later in signal_thread, but only for
91            this thread. */
92
93         if (sigemptyset(&mask) != 0)
94                 vm_abort_errno("signal_init: sigemptyset failed");
95
96 #if !defined(WITH_CLASSPATH_SUN)
97         /* Let OpenJDK handle SIGINT itself. */
98
99         if (sigaddset(&mask, SIGINT) != 0)
100                 vm_abort_errno("signal_init: sigaddset failed");
101 #endif
102
103 #if !defined(__FREEBSD__)
104         if (sigaddset(&mask, SIGQUIT) != 0)
105                 vm_abort_errno("signal_init: sigaddset failed");
106 #endif
107
108         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
109                 vm_abort_errno("signal_init: sigprocmask failed");
110
111 #if defined(__LINUX__) && defined(ENABLE_THREADS)
112         /* XXX Remove for exact-GC. */
113         }
114 #endif
115
116 #if defined(ENABLE_GC_BOEHM)
117         /* Allocate something so the garbage collector's signal handlers
118            are installed. */
119
120         (void) GCNEW(int);
121 #endif
122
123         /* Install signal handlers for signals we want to catch in all
124            threads. */
125
126 #if defined(ENABLE_JIT)
127 # if defined(ENABLE_INTRP)
128         if (!opt_intrp) {
129 # endif
130                 /* SIGSEGV handler */
131
132                 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
133                                                            SA_NODEFER | SA_SIGINFO);
134
135 #  if defined(SIGBUS)
136                 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
137                                                            SA_NODEFER | SA_SIGINFO);
138 #  endif
139
140 #  if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
141                 /* SIGFPE handler */
142
143                 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
144                                                            SA_NODEFER | SA_SIGINFO);
145 #  endif
146
147 #  if defined(__ARM__) || defined(__I386__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
148                 /* XXX use better defines for that (in arch.h) */
149                 /* SIGILL handler */
150
151                 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
152                                                            SA_NODEFER | SA_SIGINFO);
153 #  endif
154
155 #  if defined(__POWERPC__)
156                 /* XXX use better defines for that (in arch.h) */
157                 /* SIGTRAP handler */
158
159                 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
160                                                            SA_NODEFER | SA_SIGINFO);
161 #  endif
162 # if defined(ENABLE_INTRP)
163         }
164 # endif
165
166 #if defined(__DARWIN__)
167         do {
168                 struct utsname name;
169                 kern_return_t kr;
170
171                 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
172                 if (uname(&name) != 0) 
173                         break;
174
175                 /* Make sure the string is large enough */
176                 /* Check the major number (ascii comparison) */
177                 /* Verify that we're not looking at '10.' by checking for a trailing period. */
178                 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
179                         break;
180
181                 /* Reset CrashReporter's task signal handler */
182                 kr = task_set_exception_ports(mach_task_self(),
183                                                                           EXC_MASK_BAD_ACCESS
184 #  if defined(__I386__)
185                                                                           | EXC_MASK_BAD_INSTRUCTION
186 #endif
187                                                                           , MACH_PORT_NULL,
188                                                                           EXCEPTION_STATE_IDENTITY,
189                                                                           MACHINE_THREAD_STATE);
190
191                 assert(kr == KERN_SUCCESS);
192         } while (false);
193 #endif
194 #endif /* !defined(ENABLE_JIT) */
195
196 #if defined(ENABLE_THREADS)
197         /* SIGHUP handler for threads_thread_interrupt */
198
199         signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
200 #endif
201
202 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
203         /* SIGUSR1 handler for the exact GC to suspend threads */
204
205         signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
206                                                    SA_SIGINFO);
207 #endif
208
209 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
210         /* SIGUSR2 handler for profiling sampling */
211
212         signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
213                                                    SA_SIGINFO);
214 #endif
215
216 #endif /* !defined(__CYGWIN__) */
217
218         return true;
219 }
220
221
222 /* signal_register_signal ******************************************************
223
224    Register the specified handler with the specified signal.
225
226 *******************************************************************************/
227
228 void signal_register_signal(int signum, functionptr handler, int flags)
229 {
230         struct sigaction act;
231
232         void (*function)(int, siginfo_t *, void *);
233
234         function = (void (*)(int, siginfo_t *, void *)) handler;
235
236         if (sigemptyset(&act.sa_mask) != 0)
237                 vm_abort_errno("signal_register_signal: sigemptyset failed");
238
239         act.sa_sigaction = function;
240         act.sa_flags     = flags;
241
242         if (sigaction(signum, &act, NULL) != 0)
243                 vm_abort_errno("signal_register_signal: sigaction failed");
244 }
245
246
247 /* signal_handle ***************************************************************
248
249    Handles the signal caught by a signal handler and calls the correct
250    function.
251
252 *******************************************************************************/
253
254 void *signal_handle(int type, intptr_t val,
255                                         void *pv, void *sp, void *ra, void *xpc, void *context)
256 {
257         stackframeinfo_t  sfi;
258         int32_t           index;
259         java_handle_t    *o;
260         methodinfo       *m;
261         java_handle_t    *p;
262
263 #if !defined(NDEBUG)
264         if (opt_TraceTraps)
265                 log_println("[signal_handle: trap %d]", type);
266 #endif
267         
268 #if defined(ENABLE_VMLOG)
269         vmlog_cacao_signl_type(type);
270 #endif
271
272         /* Prevent compiler warnings. */
273
274         o = NULL;
275         m = NULL;
276
277         /* wrap the value into a handle if it is a reference */
278         /* BEFORE: creating stackframeinfo */
279
280         switch (type) {
281         case EXCEPTION_HARDWARE_CLASSCAST:
282                 o = LLNI_WRAP((java_object_t *) val);
283                 break;
284
285         case EXCEPTION_HARDWARE_COMPILER:
286                 /* In this case the passed PV points to the compiler stub.  We
287                    get the methodinfo pointer here and set PV to NULL so
288                    stacktrace_stackframeinfo_add determines the PV for the
289                    parent Java method. */
290
291                 m  = code_get_methodinfo_for_pv(pv);
292                 pv = NULL;
293                 break;
294
295         default:
296                 /* do nothing */
297                 break;
298         }
299
300         /* Fill and add a stackframeinfo. */
301
302         stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
303
304         switch (type) {
305         case EXCEPTION_HARDWARE_NULLPOINTER:
306                 p = exceptions_new_nullpointerexception();
307                 break;
308
309         case EXCEPTION_HARDWARE_ARITHMETIC:
310                 p = exceptions_new_arithmeticexception();
311                 break;
312
313         case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
314                 index = (s4) val;
315                 p = exceptions_new_arrayindexoutofboundsexception(index);
316                 break;
317
318         case EXCEPTION_HARDWARE_ARRAYSTORE:
319                 p = exceptions_new_arraystoreexception();
320                 break;
321
322         case EXCEPTION_HARDWARE_CLASSCAST:
323                 p = exceptions_new_classcastexception(o);
324                 break;
325
326         case EXCEPTION_HARDWARE_EXCEPTION:
327                 p = exceptions_fillinstacktrace();
328                 break;
329
330         case EXCEPTION_HARDWARE_PATCHER:
331 #if defined(ENABLE_REPLACEMENT)
332                 if (replace_me_wrapper(xpc, context)) {
333                         p = NULL;
334                         break;
335                 }
336 #endif
337                 p = patcher_handler(xpc);
338                 break;
339
340         case EXCEPTION_HARDWARE_COMPILER:
341                 p = jit_compile_handle(m, sfi.pv, ra, (void *) val);
342                 break;
343
344         default:
345                 /* Let's try to get a backtrace. */
346
347                 (void) methodtree_find(xpc);
348
349                 /* If that does not work, print more debug info. */
350
351                 log_println("signal_handle: unknown hardware exception type %d", type);
352
353 #if SIZEOF_VOID_P == 8
354                 log_println("PC=0x%016lx", xpc);
355 #else
356                 log_println("PC=0x%08x", xpc);
357 #endif
358
359 #if defined(ENABLE_DISASSEMBLER)
360                 log_println("machine instruction at PC:");
361                 disassinstr(xpc);
362 #endif
363
364                 vm_abort("Exiting...");
365
366                 /* keep compiler happy */
367
368                 p = NULL;
369         }
370
371         /* Remove stackframeinfo. */
372
373         stacktrace_stackframeinfo_remove(&sfi);
374
375         /* unwrap and return the exception object */
376         /* AFTER: removing stackframeinfo */
377
378         if (type == EXCEPTION_HARDWARE_COMPILER)
379                 return p;
380         else
381                 return LLNI_UNWRAP(p);
382 }
383
384
385 /* signal_thread ************************************************************
386
387    This thread sets the signal mask to catch the user input signals
388    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
389    signals on every single thread running.
390
391 *******************************************************************************/
392
393 static void signal_thread(void)
394 {
395         threadobject *t;
396         sigset_t      mask;
397         int           sig;
398
399         t = THREADOBJECT;
400
401         if (sigemptyset(&mask) != 0)
402                 vm_abort_errno("signal_thread: sigemptyset failed");
403
404 #if !defined(WITH_CLASSPATH_SUN)
405         /* Let OpenJDK handle SIGINT itself. */
406
407         if (sigaddset(&mask, SIGINT) != 0)
408                 vm_abort_errno("signal_thread: sigaddset failed");
409 #endif
410
411 #if !defined(__FREEBSD__)
412         if (sigaddset(&mask, SIGQUIT) != 0)
413                 vm_abort_errno("signal_thread: sigaddset failed");
414 #endif
415
416         for (;;) {
417                 /* just wait for a signal */
418
419 #if defined(ENABLE_THREADS)
420                 thread_set_state_waiting(t);
421 #endif
422
423                 /* XXX We don't check for an error here, although the man-page
424                    states sigwait does not return an error (which is wrong!),
425                    but it seems to make problems with Boehm-GC.  We should
426                    revisit this code with our new exact-GC. */
427
428 /*              if (sigwait(&mask, &sig) != 0) */
429 /*                      vm_abort_errno("signal_thread: sigwait failed"); */
430                 (void) sigwait(&mask, &sig);
431
432 #if defined(ENABLE_THREADS)
433                 thread_set_state_runnable(t);
434 #endif
435
436                 /* Handle the signal. */
437
438                 signal_thread_handler(sig);
439         }
440 }
441
442
443 /* signal_thread_handler *******************************************************
444
445    Handles the signals caught in the signal handler thread.  Also used
446    from sun.misc.Signal with OpenJDK.
447
448 *******************************************************************************/
449
450 void signal_thread_handler(int sig)
451 {
452         switch (sig) {
453         case SIGINT:
454                 /* exit the vm properly */
455
456                 vm_exit(0);
457                 break;
458
459         case SIGQUIT:
460                 /* print a thread dump */
461 #if defined(ENABLE_THREADS)
462                 threads_dump();
463 #endif
464
465 #if defined(ENABLE_STATISTICS)
466                 if (opt_stat)
467                         statistics_print_memory_usage();
468 #endif
469                 break;
470         }
471 }
472
473
474 /* signal_start_thread *********************************************************
475
476    Starts the signal handler thread.
477
478 *******************************************************************************/
479
480 bool signal_start_thread(void)
481 {
482 #if defined(ENABLE_THREADS)
483         utf *name;
484
485         name = utf_new_char("Signal Handler");
486
487         if (!threads_thread_start_internal(name, signal_thread))
488                 return false;
489
490         /* everything's ok */
491
492         return true;
493 #else
494 #warning FIX ME!
495 #endif
496 }
497
498
499 /* signal_handler_sighup *******************************************************
500
501    This handler is required by threads_thread_interrupt and does
502    nothing.
503
504 *******************************************************************************/
505
506 #if defined(ENABLE_THREADS)
507 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
508 {
509         /* do nothing */
510 }
511 #endif
512
513
514 /*
515  * These are local overrides for various environment variables in Emacs.
516  * Please do not remove this and leave it at the end of the file, where
517  * Emacs will automagically detect them.
518  * ---------------------------------------------------------------------
519  * Local variables:
520  * mode: c
521  * indent-tabs-mode: t
522  * c-basic-offset: 4
523  * tab-width: 4
524  * End:
525  */