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