8aa02dd4a02f0f3fb8d9abe32d08d890e67a6a15
[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    $Id: signal.c 8370 2007-08-20 21:44:10Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <signal.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37
38 #if defined(__DARWIN__)
39 /* If we compile with -ansi on darwin, <sys/types.h> is not
40  included. So let's do it here. */
41 # include <sys/types.h>
42 #endif
43
44 #include "arch.h"
45
46 #include "mm/memory.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(__S390__)
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_PROFILING)
177         /* SIGUSR2 handler for profiling sampling */
178
179         signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
180                                                    SA_SIGINFO);
181 #endif
182
183 #endif /* !defined(__CYGWIN__) */
184
185         return true;
186 }
187
188
189 /* signal_register_signal ******************************************************
190
191    Register the specified handler with the specified signal.
192
193 *******************************************************************************/
194
195 void signal_register_signal(int signum, functionptr handler, int flags)
196 {
197         struct sigaction act;
198
199         void (*function)(int, siginfo_t *, void *);
200
201         function = (void (*)(int, siginfo_t *, void *)) handler;
202
203         if (sigemptyset(&act.sa_mask) != 0)
204                 vm_abort("signal_register_signal: sigemptyset failed: %s",
205                                  strerror(errno));
206
207         act.sa_sigaction = function;
208         act.sa_flags     = flags;
209
210         if (sigaction(signum, &act, NULL) != 0)
211                 vm_abort("signal_register_signal: sigaction failed: %s",
212                                  strerror(errno));
213 }
214
215
216 /* signal_handle ***************************************************************
217
218    Handles the signal caught by a signal handler and calls the correct
219    function.
220
221 *******************************************************************************/
222
223 void *signal_handle(void *xpc, int type, intptr_t val)
224 {
225         void          *p;
226         int32_t        index;
227         java_object_t *o;
228
229         switch (type) {
230         case EXCEPTION_HARDWARE_NULLPOINTER:
231                 p = exceptions_new_nullpointerexception();
232                 break;
233
234         case EXCEPTION_HARDWARE_ARITHMETIC:
235                 p = exceptions_new_arithmeticexception();
236                 break;
237
238         case EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS:
239                 index = (s4) val;
240                 p = exceptions_new_arrayindexoutofboundsexception(index);
241                 break;
242
243         case EXCEPTION_HARDWARE_CLASSCAST:
244                 o = (java_object_t *) val;
245                 p = exceptions_new_classcastexception(o);
246                 break;
247
248         case EXCEPTION_HARDWARE_EXCEPTION:
249                 p = exceptions_fillinstacktrace();
250                 break;
251
252         case EXCEPTION_HARDWARE_PATCHER:
253 #if defined(ENABLE_REPLACEMENT)
254                 if (replace_me_wrapper(xpc)) {
255                         p = NULL;
256                         break;
257                 }
258 #endif
259                 p = patcher_handler(xpc);
260                 break;
261
262         default:
263                 /* Let's try to get a backtrace. */
264
265                 codegen_get_pv_from_pc(xpc);
266
267                 /* If that does not work, print more debug info. */
268
269                 log_println("exceptions_new_hardware_exception: unknown exception type %d", type);
270
271 #if SIZEOF_VOID_P == 8
272                 log_println("PC=0x%016lx", xpc);
273 #else
274                 log_println("PC=0x%08x", xpc);
275 #endif
276
277 #if defined(ENABLE_DISASSEMBLER)
278                 log_println("machine instruction at PC:");
279                 disassinstr(xpc);
280 #endif
281
282                 vm_abort("Exiting...");
283
284                 /* keep compiler happy */
285
286                 p = NULL;
287         }
288
289         return p;
290 }
291
292
293 /* signal_thread ************************************************************
294
295    This thread sets the signal mask to catch the user input signals
296    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
297    signals on every single thread running.
298
299 *******************************************************************************/
300
301 static void signal_thread(void)
302 {
303         threadobject *t;
304         sigset_t      mask;
305         int           sig;
306
307         t = THREADOBJECT;
308
309         if (sigemptyset(&mask) != 0)
310                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
311
312 #if !defined(WITH_CLASSPATH_SUN)
313         /* Let OpenJDK handle SIGINT itself. */
314
315         if (sigaddset(&mask, SIGINT) != 0)
316                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
317 #endif
318
319 #if !defined(__FREEBSD__)
320         if (sigaddset(&mask, SIGQUIT) != 0)
321                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
322 #endif
323
324         for (;;) {
325                 /* just wait for a signal */
326
327 #if defined(ENABLE_THREADS)
328                 threads_thread_state_waiting(t);
329 #endif
330
331                 /* XXX We don't check for an error here, although the man-page
332                    states sigwait does not return an error (which is wrong!),
333                    but it seems to make problems with Boehm-GC.  We should
334                    revisit this code with our new exact-GC. */
335
336 /*              if (sigwait(&mask, &sig) != 0) */
337 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
338                 (void) sigwait(&mask, &sig);
339
340 #if defined(ENABLE_THREADS)
341                 threads_thread_state_runnable(t);
342 #endif
343
344                 /* Handle the signal. */
345
346                 signal_thread_handler(sig);
347         }
348 }
349
350
351 /* signal_thread_handler *******************************************************
352
353    Handles the signals caught in the signal handler thread.  Also used
354    from sun.misc.Signal with OpenJDK.
355
356 *******************************************************************************/
357
358 void signal_thread_handler(int sig)
359 {
360         switch (sig) {
361         case SIGINT:
362                 /* exit the vm properly */
363
364                 vm_exit(0);
365                 break;
366
367         case SIGQUIT:
368                 /* print a thread dump */
369 #if defined(ENABLE_THREADS)
370                 threads_dump();
371 #endif
372
373 #if defined(ENABLE_STATISTICS)
374                 if (opt_stat)
375                         statistics_print_memory_usage();
376 #endif
377                 break;
378         }
379 }
380
381
382 /* signal_start_thread *********************************************************
383
384    Starts the signal handler thread.
385
386 *******************************************************************************/
387
388 bool signal_start_thread(void)
389 {
390 #if defined(ENABLE_THREADS)
391         utf *name;
392
393         name = utf_new_char("Signal Handler");
394
395         if (!threads_thread_start_internal(name, signal_thread))
396                 return false;
397
398         /* everything's ok */
399
400         return true;
401 #else
402 #warning FIX ME!
403 #endif
404 }
405
406
407 /* signal_handler_sighup *******************************************************
408
409    This handler is required by threads_thread_interrupt and does
410    nothing.
411
412 *******************************************************************************/
413
414 #if defined(ENABLE_THREADS)
415 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
416 {
417         /* do nothing */
418 }
419 #endif
420
421
422 /*
423  * These are local overrides for various environment variables in Emacs.
424  * Please do not remove this and leave it at the end of the file, where
425  * Emacs will automagically detect them.
426  * ---------------------------------------------------------------------
427  * Local variables:
428  * mode: c
429  * indent-tabs-mode: t
430  * c-basic-offset: 4
431  * tab-width: 4
432  * End:
433  */