090b7ef169c0bfe077a47754804c6778c38c370c
[cacao.git] / src / vm / signal.c
1 /* src/vm/signal.c - machine independent signal functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35
36 #if defined(__DARWIN__)
37 /* If we compile with -ansi on darwin, <sys/types.h> is not
38  included. So let's do it here. */
39 # include <sys/types.h>
40 #endif
41
42 #include "arch.h"
43
44 #include "mm/memory.h"
45
46 #include "native/llni.h"
47
48 #if defined(ENABLE_THREADS)
49 # include "threads/threads-common.h"
50 #else
51 # include "threads/none/threads.h"
52 #endif
53
54 #include "toolbox/logging.h"
55
56 #include "vm/exceptions.h"
57 #include "vm/signallocal.h"
58 #include "vm/vm.h"
59
60 #include "vm/jit/codegen-common.h"
61 #include "vm/jit/disass.h"
62 #include "vm/jit/patcher-common.h"
63
64 #include "vmcore/options.h"
65
66 #if defined(ENABLE_STATISTICS)
67 # include "vmcore/statistics.h"
68 #endif
69
70
71 /* function prototypes ********************************************************/
72
73 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
74
75
76 /* signal_init *****************************************************************
77
78    Initializes the signal subsystem and installs the signal handler.
79
80 *******************************************************************************/
81
82 bool signal_init(void)
83 {
84 #if !defined(__CYGWIN__)
85         sigset_t mask;
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("signal_init: sigemptyset failed: %s", strerror(errno));
98
99 #if !defined(WITH_CLASSPATH_SUN)
100         /* Let OpenJDK handle SIGINT itself. */
101
102         if (sigaddset(&mask, SIGINT) != 0)
103                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
104 #endif
105
106 #if !defined(__FREEBSD__)
107         if (sigaddset(&mask, SIGQUIT) != 0)
108                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
109 #endif
110
111         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
112                 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
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__)
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 #endif /* !defined(ENABLE_INTRP) */
169
170 #if defined(ENABLE_THREADS)
171         /* SIGHUP handler for threads_thread_interrupt */
172
173         signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
174 #endif
175
176 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
177         /* SIGUSR1 handler for the exact GC to suspend threads */
178
179         signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
180                                                    SA_SIGINFO);
181 #endif
182
183 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
184         /* SIGUSR2 handler for profiling sampling */
185
186         signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
187                                                    SA_SIGINFO);
188 #endif
189
190 #endif /* !defined(__CYGWIN__) */
191
192         return true;
193 }
194
195
196 /* signal_register_signal ******************************************************
197
198    Register the specified handler with the specified signal.
199
200 *******************************************************************************/
201
202 void signal_register_signal(int signum, functionptr handler, int flags)
203 {
204         struct sigaction act;
205
206         void (*function)(int, siginfo_t *, void *);
207
208         function = (void (*)(int, siginfo_t *, void *)) handler;
209
210         if (sigemptyset(&act.sa_mask) != 0)
211                 vm_abort("signal_register_signal: sigemptyset failed: %s",
212                                  strerror(errno));
213
214         act.sa_sigaction = function;
215         act.sa_flags     = flags;
216
217         if (sigaction(signum, &act, NULL) != 0)
218                 vm_abort("signal_register_signal: sigaction failed: %s",
219                                  strerror(errno));
220 }
221
222
223 /* signal_handle ***************************************************************
224
225    Handles the signal caught by a signal handler and calls the correct
226    function.
227
228 *******************************************************************************/
229
230 void *signal_handle(int type, intptr_t val,
231                                         void *pv, void *sp, void *ra, void *xpc, void *context)
232 {
233         stackframeinfo_t  sfi;
234         int32_t           index;
235         java_handle_t    *o;
236         methodinfo       *m;
237         java_handle_t    *p;
238
239         /* wrap the value into a handle if it is a reference */
240         /* BEFORE: creating stackframeinfo */
241
242         switch (type) {
243         case EXCEPTION_HARDWARE_CLASSCAST:
244                 o = LLNI_WRAP((java_object_t *) val);
245                 break;
246
247         case EXCEPTION_HARDWARE_COMPILER:
248                 /* In this case the passed PV points to the compiler stub.  We
249                    get the methodinfo pointer here and set PV to NULL so
250                    stacktrace_stackframeinfo_add determines the PV for the
251                    parent Java method. */
252
253                 m  = code_get_methodinfo_for_pv(pv);
254                 pv = NULL;
255                 break;
256
257         default:
258                 /* do nothing */
259                 break;
260         }
261
262         /* Fill and add a stackframeinfo. */
263
264         stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
265
266         switch (type) {
267         case EXCEPTION_HARDWARE_NULLPOINTER:
268                 p = exceptions_new_nullpointerexception();
269                 break;
270
271         case EXCEPTION_HARDWARE_ARITHMETIC:
272                 p = exceptions_new_arithmeticexception();
273                 break;
274
275         case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
276                 index = (s4) val;
277                 p = exceptions_new_arrayindexoutofboundsexception(index);
278                 break;
279
280         case EXCEPTION_HARDWARE_ARRAYSTORE:
281                 p = exceptions_new_arraystoreexception();
282                 break;
283
284         case EXCEPTION_HARDWARE_CLASSCAST:
285                 p = exceptions_new_classcastexception(o);
286                 break;
287
288         case EXCEPTION_HARDWARE_EXCEPTION:
289                 p = exceptions_fillinstacktrace();
290                 break;
291
292         case EXCEPTION_HARDWARE_PATCHER:
293 #if defined(ENABLE_REPLACEMENT)
294                 if (replace_me_wrapper(xpc, context)) {
295                         p = NULL;
296                         break;
297                 }
298 #endif
299                 p = patcher_handler(xpc);
300                 break;
301
302         case EXCEPTION_HARDWARE_COMPILER:
303                 p = jit_compile_handle(m, sfi.pv, ra, (void *) val);
304                 break;
305
306         default:
307                 /* Let's try to get a backtrace. */
308
309                 codegen_get_pv_from_pc(xpc);
310
311                 /* If that does not work, print more debug info. */
312
313                 log_println("signal_handle: unknown hardware exception type %d", type);
314
315 #if SIZEOF_VOID_P == 8
316                 log_println("PC=0x%016lx", xpc);
317 #else
318                 log_println("PC=0x%08x", xpc);
319 #endif
320
321 #if defined(ENABLE_DISASSEMBLER)
322                 log_println("machine instruction at PC:");
323                 disassinstr(xpc);
324 #endif
325
326                 vm_abort("Exiting...");
327
328                 /* keep compiler happy */
329
330                 p = NULL;
331         }
332
333         /* Remove stackframeinfo. */
334
335         stacktrace_stackframeinfo_remove(&sfi);
336
337         /* unwrap and return the exception object */
338         /* AFTER: removing stackframeinfo */
339
340         if (type == EXCEPTION_HARDWARE_COMPILER)
341                 return p;
342         else
343                 return LLNI_UNWRAP(p);
344 }
345
346
347 /* signal_thread ************************************************************
348
349    This thread sets the signal mask to catch the user input signals
350    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
351    signals on every single thread running.
352
353 *******************************************************************************/
354
355 static void signal_thread(void)
356 {
357         threadobject *t;
358         sigset_t      mask;
359         int           sig;
360
361         t = THREADOBJECT;
362
363         if (sigemptyset(&mask) != 0)
364                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
365
366 #if !defined(WITH_CLASSPATH_SUN)
367         /* Let OpenJDK handle SIGINT itself. */
368
369         if (sigaddset(&mask, SIGINT) != 0)
370                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
371 #endif
372
373 #if !defined(__FREEBSD__)
374         if (sigaddset(&mask, SIGQUIT) != 0)
375                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
376 #endif
377
378         for (;;) {
379                 /* just wait for a signal */
380
381 #if defined(ENABLE_THREADS)
382                 threads_thread_state_waiting(t);
383 #endif
384
385                 /* XXX We don't check for an error here, although the man-page
386                    states sigwait does not return an error (which is wrong!),
387                    but it seems to make problems with Boehm-GC.  We should
388                    revisit this code with our new exact-GC. */
389
390 /*              if (sigwait(&mask, &sig) != 0) */
391 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
392                 (void) sigwait(&mask, &sig);
393
394 #if defined(ENABLE_THREADS)
395                 threads_thread_state_runnable(t);
396 #endif
397
398                 /* Handle the signal. */
399
400                 signal_thread_handler(sig);
401         }
402 }
403
404
405 /* signal_thread_handler *******************************************************
406
407    Handles the signals caught in the signal handler thread.  Also used
408    from sun.misc.Signal with OpenJDK.
409
410 *******************************************************************************/
411
412 void signal_thread_handler(int sig)
413 {
414         switch (sig) {
415         case SIGINT:
416                 /* exit the vm properly */
417
418                 vm_exit(0);
419                 break;
420
421         case SIGQUIT:
422                 /* print a thread dump */
423 #if defined(ENABLE_THREADS)
424                 threads_dump();
425 #endif
426
427 #if defined(ENABLE_STATISTICS)
428                 if (opt_stat)
429                         statistics_print_memory_usage();
430 #endif
431                 break;
432         }
433 }
434
435
436 /* signal_start_thread *********************************************************
437
438    Starts the signal handler thread.
439
440 *******************************************************************************/
441
442 bool signal_start_thread(void)
443 {
444 #if defined(ENABLE_THREADS)
445         utf *name;
446
447         name = utf_new_char("Signal Handler");
448
449         if (!threads_thread_start_internal(name, signal_thread))
450                 return false;
451
452         /* everything's ok */
453
454         return true;
455 #else
456 #warning FIX ME!
457 #endif
458 }
459
460
461 /* signal_handler_sighup *******************************************************
462
463    This handler is required by threads_thread_interrupt and does
464    nothing.
465
466 *******************************************************************************/
467
468 #if defined(ENABLE_THREADS)
469 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
470 {
471         /* do nothing */
472 }
473 #endif
474
475
476 /*
477  * These are local overrides for various environment variables in Emacs.
478  * Please do not remove this and leave it at the end of the file, where
479  * Emacs will automagically detect them.
480  * ---------------------------------------------------------------------
481  * Local variables:
482  * mode: c
483  * indent-tabs-mode: t
484  * c-basic-offset: 4
485  * tab-width: 4
486  * End:
487  */