* Merged with default branch at rev 16f3633aaa5a.
[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_CLASSCAST:
249                 o = (java_object_t *) val;
250                 p = exceptions_new_classcastexception(o);
251                 break;
252
253         case EXCEPTION_HARDWARE_EXCEPTION:
254                 p = exceptions_fillinstacktrace();
255                 break;
256
257         case EXCEPTION_HARDWARE_PATCHER:
258 #if defined(ENABLE_REPLACEMENT)
259                 if (replace_me_wrapper(xpc)) {
260                         p = NULL;
261                         break;
262                 }
263 #endif
264                 p = patcher_handler(xpc);
265                 break;
266
267         default:
268                 /* Let's try to get a backtrace. */
269
270                 codegen_get_pv_from_pc(xpc);
271
272                 /* If that does not work, print more debug info. */
273
274                 log_println("exceptions_new_hardware_exception: unknown exception type %d", type);
275
276 #if SIZEOF_VOID_P == 8
277                 log_println("PC=0x%016lx", xpc);
278 #else
279                 log_println("PC=0x%08x", xpc);
280 #endif
281
282 #if defined(ENABLE_DISASSEMBLER)
283                 log_println("machine instruction at PC:");
284                 disassinstr(xpc);
285 #endif
286
287                 vm_abort("Exiting...");
288
289                 /* keep compiler happy */
290
291                 p = NULL;
292         }
293
294         return p;
295 }
296
297
298 /* signal_thread ************************************************************
299
300    This thread sets the signal mask to catch the user input signals
301    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
302    signals on every single thread running.
303
304 *******************************************************************************/
305
306 static void signal_thread(void)
307 {
308         threadobject *t;
309         sigset_t      mask;
310         int           sig;
311
312         t = THREADOBJECT;
313
314         if (sigemptyset(&mask) != 0)
315                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
316
317 #if !defined(WITH_CLASSPATH_SUN)
318         /* Let OpenJDK handle SIGINT itself. */
319
320         if (sigaddset(&mask, SIGINT) != 0)
321                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
322 #endif
323
324 #if !defined(__FREEBSD__)
325         if (sigaddset(&mask, SIGQUIT) != 0)
326                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
327 #endif
328
329         for (;;) {
330                 /* just wait for a signal */
331
332 #if defined(ENABLE_THREADS)
333                 threads_thread_state_waiting(t);
334 #endif
335
336                 /* XXX We don't check for an error here, although the man-page
337                    states sigwait does not return an error (which is wrong!),
338                    but it seems to make problems with Boehm-GC.  We should
339                    revisit this code with our new exact-GC. */
340
341 /*              if (sigwait(&mask, &sig) != 0) */
342 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
343                 (void) sigwait(&mask, &sig);
344
345 #if defined(ENABLE_THREADS)
346                 threads_thread_state_runnable(t);
347 #endif
348
349                 /* Handle the signal. */
350
351                 signal_thread_handler(sig);
352         }
353 }
354
355
356 /* signal_thread_handler *******************************************************
357
358    Handles the signals caught in the signal handler thread.  Also used
359    from sun.misc.Signal with OpenJDK.
360
361 *******************************************************************************/
362
363 void signal_thread_handler(int sig)
364 {
365         switch (sig) {
366         case SIGINT:
367                 /* exit the vm properly */
368
369                 vm_exit(0);
370                 break;
371
372         case SIGQUIT:
373                 /* print a thread dump */
374 #if defined(ENABLE_THREADS)
375                 threads_dump();
376 #endif
377
378 #if defined(ENABLE_STATISTICS)
379                 if (opt_stat)
380                         statistics_print_memory_usage();
381 #endif
382                 break;
383         }
384 }
385
386
387 /* signal_start_thread *********************************************************
388
389    Starts the signal handler thread.
390
391 *******************************************************************************/
392
393 bool signal_start_thread(void)
394 {
395 #if defined(ENABLE_THREADS)
396         utf *name;
397
398         name = utf_new_char("Signal Handler");
399
400         if (!threads_thread_start_internal(name, signal_thread))
401                 return false;
402
403         /* everything's ok */
404
405         return true;
406 #else
407 #warning FIX ME!
408 #endif
409 }
410
411
412 /* signal_handler_sighup *******************************************************
413
414    This handler is required by threads_thread_interrupt and does
415    nothing.
416
417 *******************************************************************************/
418
419 #if defined(ENABLE_THREADS)
420 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
421 {
422         /* do nothing */
423 }
424 #endif
425
426
427 /*
428  * These are local overrides for various environment variables in Emacs.
429  * Please do not remove this and leave it at the end of the file, where
430  * Emacs will automagically detect them.
431  * ---------------------------------------------------------------------
432  * Local variables:
433  * mode: c
434  * indent-tabs-mode: t
435  * c-basic-offset: 4
436  * tab-width: 4
437  * End:
438  */