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