* src/vm/jit/jit.c [ENABLE_PYTHON]: Changed name of hardcoded script.
[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__) || 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 #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         /* Prevent compiler warnings. */
240
241         o = NULL;
242         m = NULL;
243
244         /* wrap the value into a handle if it is a reference */
245         /* BEFORE: creating stackframeinfo */
246
247         switch (type) {
248         case EXCEPTION_HARDWARE_CLASSCAST:
249                 o = LLNI_WRAP((java_object_t *) val);
250                 break;
251
252         case EXCEPTION_HARDWARE_COMPILER:
253                 /* In this case the passed PV points to the compiler stub.  We
254                    get the methodinfo pointer here and set PV to NULL so
255                    stacktrace_stackframeinfo_add determines the PV for the
256                    parent Java method. */
257
258                 m  = code_get_methodinfo_for_pv(pv);
259                 pv = NULL;
260                 break;
261
262         default:
263                 /* do nothing */
264                 break;
265         }
266
267         /* Fill and add a stackframeinfo. */
268
269         stacktrace_stackframeinfo_add(&sfi, pv, sp, ra, xpc);
270
271         switch (type) {
272         case EXCEPTION_HARDWARE_NULLPOINTER:
273                 p = exceptions_new_nullpointerexception();
274                 break;
275
276         case EXCEPTION_HARDWARE_ARITHMETIC:
277                 p = exceptions_new_arithmeticexception();
278                 break;
279
280         case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
281                 index = (s4) val;
282                 p = exceptions_new_arrayindexoutofboundsexception(index);
283                 break;
284
285         case EXCEPTION_HARDWARE_ARRAYSTORE:
286                 p = exceptions_new_arraystoreexception();
287                 break;
288
289         case EXCEPTION_HARDWARE_CLASSCAST:
290                 p = exceptions_new_classcastexception(o);
291                 break;
292
293         case EXCEPTION_HARDWARE_EXCEPTION:
294                 p = exceptions_fillinstacktrace();
295                 break;
296
297         case EXCEPTION_HARDWARE_PATCHER:
298 #if defined(ENABLE_REPLACEMENT)
299                 if (replace_me_wrapper(xpc, context)) {
300                         p = NULL;
301                         break;
302                 }
303 #endif
304                 p = patcher_handler(xpc);
305                 break;
306
307         case EXCEPTION_HARDWARE_COMPILER:
308                 p = jit_compile_handle(m, sfi.pv, ra, (void *) val);
309                 break;
310
311         default:
312                 /* Let's try to get a backtrace. */
313
314                 codegen_get_pv_from_pc(xpc);
315
316                 /* If that does not work, print more debug info. */
317
318                 log_println("signal_handle: unknown hardware exception type %d", type);
319
320 #if SIZEOF_VOID_P == 8
321                 log_println("PC=0x%016lx", xpc);
322 #else
323                 log_println("PC=0x%08x", xpc);
324 #endif
325
326 #if defined(ENABLE_DISASSEMBLER)
327                 log_println("machine instruction at PC:");
328                 disassinstr(xpc);
329 #endif
330
331                 vm_abort("Exiting...");
332
333                 /* keep compiler happy */
334
335                 p = NULL;
336         }
337
338         /* Remove stackframeinfo. */
339
340         stacktrace_stackframeinfo_remove(&sfi);
341
342         /* unwrap and return the exception object */
343         /* AFTER: removing stackframeinfo */
344
345         if (type == EXCEPTION_HARDWARE_COMPILER)
346                 return p;
347         else
348                 return LLNI_UNWRAP(p);
349 }
350
351
352 /* signal_thread ************************************************************
353
354    This thread sets the signal mask to catch the user input signals
355    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
356    signals on every single thread running.
357
358 *******************************************************************************/
359
360 static void signal_thread(void)
361 {
362         threadobject *t;
363         sigset_t      mask;
364         int           sig;
365
366         t = THREADOBJECT;
367
368         if (sigemptyset(&mask) != 0)
369                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
370
371 #if !defined(WITH_CLASSPATH_SUN)
372         /* Let OpenJDK handle SIGINT itself. */
373
374         if (sigaddset(&mask, SIGINT) != 0)
375                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
376 #endif
377
378 #if !defined(__FREEBSD__)
379         if (sigaddset(&mask, SIGQUIT) != 0)
380                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
381 #endif
382
383         for (;;) {
384                 /* just wait for a signal */
385
386 #if defined(ENABLE_THREADS)
387                 threads_thread_state_waiting(t);
388 #endif
389
390                 /* XXX We don't check for an error here, although the man-page
391                    states sigwait does not return an error (which is wrong!),
392                    but it seems to make problems with Boehm-GC.  We should
393                    revisit this code with our new exact-GC. */
394
395 /*              if (sigwait(&mask, &sig) != 0) */
396 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
397                 (void) sigwait(&mask, &sig);
398
399 #if defined(ENABLE_THREADS)
400                 threads_thread_state_runnable(t);
401 #endif
402
403                 /* Handle the signal. */
404
405                 signal_thread_handler(sig);
406         }
407 }
408
409
410 /* signal_thread_handler *******************************************************
411
412    Handles the signals caught in the signal handler thread.  Also used
413    from sun.misc.Signal with OpenJDK.
414
415 *******************************************************************************/
416
417 void signal_thread_handler(int sig)
418 {
419         switch (sig) {
420         case SIGINT:
421                 /* exit the vm properly */
422
423                 vm_exit(0);
424                 break;
425
426         case SIGQUIT:
427                 /* print a thread dump */
428 #if defined(ENABLE_THREADS)
429                 threads_dump();
430 #endif
431
432 #if defined(ENABLE_STATISTICS)
433                 if (opt_stat)
434                         statistics_print_memory_usage();
435 #endif
436                 break;
437         }
438 }
439
440
441 /* signal_start_thread *********************************************************
442
443    Starts the signal handler thread.
444
445 *******************************************************************************/
446
447 bool signal_start_thread(void)
448 {
449 #if defined(ENABLE_THREADS)
450         utf *name;
451
452         name = utf_new_char("Signal Handler");
453
454         if (!threads_thread_start_internal(name, signal_thread))
455                 return false;
456
457         /* everything's ok */
458
459         return true;
460 #else
461 #warning FIX ME!
462 #endif
463 }
464
465
466 /* signal_handler_sighup *******************************************************
467
468    This handler is required by threads_thread_interrupt and does
469    nothing.
470
471 *******************************************************************************/
472
473 #if defined(ENABLE_THREADS)
474 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
475 {
476         /* do nothing */
477 }
478 #endif
479
480
481 /*
482  * These are local overrides for various environment variables in Emacs.
483  * Please do not remove this and leave it at the end of the file, where
484  * Emacs will automagically detect them.
485  * ---------------------------------------------------------------------
486  * Local variables:
487  * mode: c
488  * indent-tabs-mode: t
489  * c-basic-offset: 4
490  * tab-width: 4
491  * End:
492  */