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