* src/vm/builtin.c (builtin_canstore): Throw an ArrayStoreException.
[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 #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 #if defined(__LINUX__) && defined(ENABLE_THREADS)
86         /* XXX Remove for exact-GC. */
87         if (threads_pthreads_implementation_nptl) {
88 #endif
89
90         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
91            <ctrl>-\).  We enable them later in signal_thread, but only for
92            this thread. */
93
94         if (sigemptyset(&mask) != 0)
95                 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
96
97 #if !defined(WITH_CLASSPATH_SUN)
98         /* Let OpenJDK handle SIGINT itself. */
99
100         if (sigaddset(&mask, SIGINT) != 0)
101                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
102 #endif
103
104 #if !defined(__FREEBSD__)
105         if (sigaddset(&mask, SIGQUIT) != 0)
106                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
107 #endif
108
109         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
110                 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
111
112 #if defined(__LINUX__) && defined(ENABLE_THREADS)
113         /* XXX Remove for exact-GC. */
114         }
115 #endif
116
117 #if defined(ENABLE_GC_BOEHM)
118         /* Allocate something so the garbage collector's signal handlers
119            are installed. */
120
121         (void) GCNEW(int);
122 #endif
123
124         /* Install signal handlers for signals we want to catch in all
125            threads. */
126
127 #if defined(ENABLE_JIT)
128 # if defined(ENABLE_INTRP)
129         if (!opt_intrp) {
130 # endif
131                 /* SIGSEGV handler */
132
133                 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
134                                                            SA_NODEFER | SA_SIGINFO);
135
136 #  if defined(SIGBUS)
137                 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
138                                                            SA_NODEFER | SA_SIGINFO);
139 #  endif
140
141 #  if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
142                 /* SIGFPE handler */
143
144                 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
145                                                            SA_NODEFER | SA_SIGINFO);
146 #  endif
147
148 #  if defined(__ARM__) || defined(__S390__)
149                 /* XXX use better defines for that (in arch.h) */
150                 /* SIGILL handler */
151
152                 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
153                                                            SA_NODEFER | SA_SIGINFO);
154 #  endif
155
156 #  if defined(__POWERPC__)
157                 /* XXX use better defines for that (in arch.h) */
158                 /* SIGTRAP handler */
159
160                 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
161                                                            SA_NODEFER | SA_SIGINFO);
162 #  endif
163 # if defined(ENABLE_INTRP)
164         }
165 # endif
166 #endif /* !defined(ENABLE_INTRP) */
167
168 #if defined(ENABLE_THREADS)
169         /* SIGHUP handler for threads_thread_interrupt */
170
171         signal_register_signal(SIGHUP, (functionptr) signal_handler_sighup, 0);
172 #endif
173
174 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
175         /* SIGUSR1 handler for the exact GC to suspend threads */
176
177         signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
178                                                    SA_SIGINFO);
179 #endif
180
181 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
182         /* SIGUSR2 handler for profiling sampling */
183
184         signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
185                                                    SA_SIGINFO);
186 #endif
187
188 #endif /* !defined(__CYGWIN__) */
189
190         return true;
191 }
192
193
194 /* signal_register_signal ******************************************************
195
196    Register the specified handler with the specified signal.
197
198 *******************************************************************************/
199
200 void signal_register_signal(int signum, functionptr handler, int flags)
201 {
202         struct sigaction act;
203
204         void (*function)(int, siginfo_t *, void *);
205
206         function = (void (*)(int, siginfo_t *, void *)) handler;
207
208         if (sigemptyset(&act.sa_mask) != 0)
209                 vm_abort("signal_register_signal: sigemptyset failed: %s",
210                                  strerror(errno));
211
212         act.sa_sigaction = function;
213         act.sa_flags     = flags;
214
215         if (sigaction(signum, &act, NULL) != 0)
216                 vm_abort("signal_register_signal: sigaction failed: %s",
217                                  strerror(errno));
218 }
219
220
221 /* signal_handle ***************************************************************
222
223    Handles the signal caught by a signal handler and calls the correct
224    function.
225
226 *******************************************************************************/
227
228 void *signal_handle(void *xpc, int type, intptr_t val)
229 {
230         void          *p;
231         int32_t        index;
232         java_object_t *o;
233
234         switch (type) {
235         case EXCEPTION_HARDWARE_NULLPOINTER:
236                 p = exceptions_new_nullpointerexception();
237                 break;
238
239         case EXCEPTION_HARDWARE_ARITHMETIC:
240                 p = exceptions_new_arithmeticexception();
241                 break;
242
243         case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
244                 index = (s4) val;
245                 p = exceptions_new_arrayindexoutofboundsexception(index);
246                 break;
247
248         case EXCEPTION_HARDWARE_ARRAYSTORE:
249                 p = exceptions_new_arraystoreexception();
250                 break;
251
252         case EXCEPTION_HARDWARE_CLASSCAST:
253                 o = (java_object_t *) val;
254                 p = exceptions_new_classcastexception(o);
255                 break;
256
257         case EXCEPTION_HARDWARE_EXCEPTION:
258                 p = exceptions_fillinstacktrace();
259                 break;
260
261         case EXCEPTION_HARDWARE_PATCHER:
262 #if defined(ENABLE_REPLACEMENT)
263                 if (replace_me_wrapper(xpc)) {
264                         p = NULL;
265                         break;
266                 }
267 #endif
268                 p = patcher_handler(xpc);
269                 break;
270
271         default:
272                 /* Let's try to get a backtrace. */
273
274                 codegen_get_pv_from_pc(xpc);
275
276                 /* If that does not work, print more debug info. */
277
278                 log_println("exceptions_new_hardware_exception: unknown exception type %d", type);
279
280 #if SIZEOF_VOID_P == 8
281                 log_println("PC=0x%016lx", xpc);
282 #else
283                 log_println("PC=0x%08x", xpc);
284 #endif
285
286 #if defined(ENABLE_DISASSEMBLER)
287                 log_println("machine instruction at PC:");
288                 disassinstr(xpc);
289 #endif
290
291                 vm_abort("Exiting...");
292
293                 /* keep compiler happy */
294
295                 p = NULL;
296         }
297
298         return p;
299 }
300
301
302 /* signal_thread ************************************************************
303
304    This thread sets the signal mask to catch the user input signals
305    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
306    signals on every single thread running.
307
308 *******************************************************************************/
309
310 static void signal_thread(void)
311 {
312         threadobject *t;
313         sigset_t      mask;
314         int           sig;
315
316         t = THREADOBJECT;
317
318         if (sigemptyset(&mask) != 0)
319                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
320
321 #if !defined(WITH_CLASSPATH_SUN)
322         /* Let OpenJDK handle SIGINT itself. */
323
324         if (sigaddset(&mask, SIGINT) != 0)
325                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
326 #endif
327
328 #if !defined(__FREEBSD__)
329         if (sigaddset(&mask, SIGQUIT) != 0)
330                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
331 #endif
332
333         for (;;) {
334                 /* just wait for a signal */
335
336 #if defined(ENABLE_THREADS)
337                 threads_thread_state_waiting(t);
338 #endif
339
340                 /* XXX We don't check for an error here, although the man-page
341                    states sigwait does not return an error (which is wrong!),
342                    but it seems to make problems with Boehm-GC.  We should
343                    revisit this code with our new exact-GC. */
344
345 /*              if (sigwait(&mask, &sig) != 0) */
346 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
347                 (void) sigwait(&mask, &sig);
348
349 #if defined(ENABLE_THREADS)
350                 threads_thread_state_runnable(t);
351 #endif
352
353                 /* Handle the signal. */
354
355                 signal_thread_handler(sig);
356         }
357 }
358
359
360 /* signal_thread_handler *******************************************************
361
362    Handles the signals caught in the signal handler thread.  Also used
363    from sun.misc.Signal with OpenJDK.
364
365 *******************************************************************************/
366
367 void signal_thread_handler(int sig)
368 {
369         switch (sig) {
370         case SIGINT:
371                 /* exit the vm properly */
372
373                 vm_exit(0);
374                 break;
375
376         case SIGQUIT:
377                 /* print a thread dump */
378 #if defined(ENABLE_THREADS)
379                 threads_dump();
380 #endif
381
382 #if defined(ENABLE_STATISTICS)
383                 if (opt_stat)
384                         statistics_print_memory_usage();
385 #endif
386                 break;
387         }
388 }
389
390
391 /* signal_start_thread *********************************************************
392
393    Starts the signal handler thread.
394
395 *******************************************************************************/
396
397 bool signal_start_thread(void)
398 {
399 #if defined(ENABLE_THREADS)
400         utf *name;
401
402         name = utf_new_char("Signal Handler");
403
404         if (!threads_thread_start_internal(name, signal_thread))
405                 return false;
406
407         /* everything's ok */
408
409         return true;
410 #else
411 #warning FIX ME!
412 #endif
413 }
414
415
416 /* signal_handler_sighup *******************************************************
417
418    This handler is required by threads_thread_interrupt and does
419    nothing.
420
421 *******************************************************************************/
422
423 #if defined(ENABLE_THREADS)
424 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
425 {
426         /* do nothing */
427 }
428 #endif
429
430
431 /*
432  * These are local overrides for various environment variables in Emacs.
433  * Please do not remove this and leave it at the end of the file, where
434  * Emacs will automagically detect them.
435  * ---------------------------------------------------------------------
436  * Local variables:
437  * mode: c
438  * indent-tabs-mode: t
439  * c-basic-offset: 4
440  * tab-width: 4
441  * End:
442  */