* src/vm/jit/codegen-common.cpp, src/vm/jit/x86_64/codegen.c: Generate
[cacao.git] / src / vm / signal.cpp
1 /* src/vm/signal.c - machine independent signal functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32
33 #if defined(__DARWIN__)
34 /* If we compile with -ansi on darwin, <sys/types.h> is not
35  included. So let's do it here. */
36 # include <sys/types.h>
37 # include <sys/utsname.h>
38 #endif
39
40 #include "arch.h"
41
42 #if defined(ENABLE_GC_BOEHM)
43 # include "mm/memory.hpp"
44 #endif
45
46 #include "threads/thread.hpp"
47 #include "threads/threadlist.hpp"
48
49 #include "vm/exceptions.hpp"
50 #include "vm/globals.hpp"
51 #include "vm/method.hpp"
52 #include "vm/options.h"
53 #include "vm/os.hpp"
54 #include "vm/signallocal.hpp"
55 #include "vm/vm.hpp"
56
57 #if defined(ENABLE_STATISTICS)
58 # include "vm/statistics.h"
59 #endif
60
61
62 /* function prototypes ********************************************************/
63
64 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
65
66
67 /* signal_init *****************************************************************
68
69    Initializes the signal subsystem and installs the signal handler.
70
71 *******************************************************************************/
72
73 bool signal_init(void)
74 {
75 #if !defined(__CYGWIN__)
76         sigset_t mask;
77
78         TRACESUBSYSTEMINITIALIZATION("signal_init");
79
80 #if defined(__LINUX__) && defined(ENABLE_THREADS)
81         /* XXX Remove for exact-GC. */
82         if (threads_pthreads_implementation_nptl) {
83 #endif
84
85         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
86            <ctrl>-\).  We enable them later in signal_thread, but only for
87            this thread. */
88
89         if (sigemptyset(&mask) != 0)
90                 os::abort_errno("signal_init: sigemptyset failed");
91
92 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
93         /* Let OpenJDK handle SIGINT itself. */
94
95         if (sigaddset(&mask, SIGINT) != 0)
96                 os::abort_errno("signal_init: sigaddset failed");
97 #endif
98
99 #if !defined(__FREEBSD__)
100         if (sigaddset(&mask, SIGQUIT) != 0)
101                 os::abort_errno("signal_init: sigaddset failed");
102 #endif
103
104         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
105                 os::abort_errno("signal_init: sigprocmask failed");
106
107 #if defined(__LINUX__) && defined(ENABLE_THREADS)
108         /* XXX Remove for exact-GC. */
109         }
110 #endif
111
112 #if defined(ENABLE_GC_BOEHM)
113         /* Allocate something so the garbage collector's signal handlers
114            are installed. */
115
116         (void) GCNEW(int);
117 #endif
118
119         /* Install signal handlers for signals we want to catch in all
120            threads. */
121
122 #if defined(ENABLE_JIT)
123 # if defined(ENABLE_INTRP)
124         if (!opt_intrp) {
125 # endif
126                 /* SIGSEGV handler */
127
128                 signal_register_signal(SIGSEGV, (functionptr) md_signal_handler_sigsegv,
129                                                            SA_NODEFER | SA_SIGINFO);
130
131 #  if defined(SIGBUS)
132                 signal_register_signal(SIGBUS, (functionptr) md_signal_handler_sigsegv,
133                                                            SA_NODEFER | SA_SIGINFO);
134 #  endif
135
136 #  if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
137                 /* SIGFPE handler */
138
139                 signal_register_signal(SIGFPE, (functionptr) md_signal_handler_sigfpe,
140                                                            SA_NODEFER | SA_SIGINFO);
141 #  endif
142
143 #  if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__POWERPC64__) || defined(__S390__) || defined(__X86_64__) || defined(__M68K__)
144                 /* XXX use better defines for that (in arch.h) */
145                 /* SIGILL handler */
146
147                 signal_register_signal(SIGILL, (functionptr) md_signal_handler_sigill,
148                                                            SA_NODEFER | SA_SIGINFO);
149 #  endif
150
151 #  if defined(__POWERPC__)
152                 /* XXX use better defines for that (in arch.h) */
153                 /* SIGTRAP handler */
154
155                 signal_register_signal(SIGTRAP, (functionptr) md_signal_handler_sigtrap,
156                                                            SA_NODEFER | SA_SIGINFO);
157 #  endif
158 # if defined(ENABLE_INTRP)
159         }
160 # endif
161
162 #if defined(__DARWIN__)
163         do {
164                 struct utsname name;
165                 kern_return_t kr;
166
167                 /* Check if we're on 10.4 (Tiger/8.x) or earlier */
168                 if (uname(&name) != 0) 
169                         break;
170
171                 /* Make sure the string is large enough */
172                 /* Check the major number (ascii comparison) */
173                 /* Verify that we're not looking at '10.' by checking for a trailing period. */
174                 if (name.release[0] == '\0' || name.release[0] > '8' || name.release[1] != '.')
175                         break;
176
177                 /* Reset CrashReporter's task signal handler */
178                 kr = task_set_exception_ports(mach_task_self(),
179                                                                           EXC_MASK_BAD_ACCESS
180 #  if defined(__I386__)
181                                                                           | EXC_MASK_BAD_INSTRUCTION
182 #endif
183                                                                           , MACH_PORT_NULL,
184                                                                           EXCEPTION_STATE_IDENTITY,
185                                                                           MACHINE_THREAD_STATE);
186
187                 assert(kr == KERN_SUCCESS);
188         } while (false);
189 #endif
190 #endif /* !defined(ENABLE_JIT) */
191
192 #if defined(ENABLE_THREADS)
193         /* SIGHUP handler for threads_thread_interrupt */
194
195         signal_register_signal(Signal_INTERRUPT_SYSTEM_CALL, (functionptr) signal_handler_sighup, 0);
196 #endif
197
198 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
199         /* SIGUSR1 handler for the exact GC to suspend threads */
200
201         signal_register_signal(SIGUSR1, (functionptr) md_signal_handler_sigusr1,
202                                                    SA_SIGINFO);
203 #endif
204
205 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
206         /* SIGUSR2 handler for profiling sampling */
207
208         signal_register_signal(SIGUSR2, (functionptr) md_signal_handler_sigusr2,
209                                                    SA_SIGINFO);
210 #endif
211
212 #endif /* !defined(__CYGWIN__) */
213
214         return true;
215 }
216
217
218 /* signal_register_signal ******************************************************
219
220    Register the specified handler with the specified signal.
221
222 *******************************************************************************/
223
224 void signal_register_signal(int signum, functionptr handler, int flags)
225 {
226         struct sigaction act;
227
228         void (*function)(int, siginfo_t *, void *);
229
230         function = (void (*)(int, siginfo_t *, void *)) handler;
231
232         if (sigemptyset(&act.sa_mask) != 0)
233                 os::abort_errno("signal_register_signal: sigemptyset failed");
234
235         act.sa_sigaction = function;
236         act.sa_flags     = flags;
237
238         if (sigaction(signum, &act, NULL) != 0)
239                 os::abort_errno("signal_register_signal: sigaction failed");
240 }
241
242
243 /* signal_thread ************************************************************
244
245    This thread sets the signal mask to catch the user input signals
246    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
247    signals on every single thread running.
248
249 *******************************************************************************/
250
251 static void signal_thread(void)
252 {
253         threadobject *t;
254         sigset_t      mask;
255         int           sig;
256         int result;
257
258         t = THREADOBJECT;
259
260         if (sigemptyset(&mask) != 0)
261                 os::abort_errno("signal_thread: sigemptyset failed");
262
263 #if !defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
264         /* Let OpenJDK handle SIGINT itself. */
265
266         if (sigaddset(&mask, SIGINT) != 0)
267                 os::abort_errno("signal_thread: sigaddset failed");
268 #endif
269
270 #if !defined(__FREEBSD__)
271         if (sigaddset(&mask, SIGQUIT) != 0)
272                 os::abort_errno("signal_thread: sigaddset failed");
273 #endif
274
275         for (;;) {
276                 /* just wait for a signal */
277
278 #if defined(ENABLE_THREADS)
279                 thread_set_state_waiting(t);
280 #endif
281
282                 // sigwait can return EINTR (unlike what the Linux man-page
283                 // says).
284                 do {
285                         result = sigwait(&mask, &sig);
286                 } while (result == EINTR);
287
288                 if (result != 0)
289                         os::abort_errnum(result, "signal_thread: sigwait failed");
290
291 #if defined(ENABLE_THREADS)
292                 thread_set_state_runnable(t);
293 #endif
294
295                 /* Handle the signal. */
296
297                 signal_thread_handler(sig);
298         }
299 }
300
301
302 /* signal_thread_handler *******************************************************
303
304    Handles the signals caught in the signal handler thread.  Also used
305    from sun.misc.Signal with OpenJDK.
306
307 *******************************************************************************/
308
309 void signal_thread_handler(int sig)
310 {
311         switch (sig) {
312         case SIGINT:
313                 /* exit the vm properly */
314
315                 vm_exit(1);
316                 break;
317
318         case SIGQUIT:
319                 /* print a thread dump */
320 #if defined(ENABLE_THREADS)
321                 ThreadList::dump_threads();
322 #endif
323
324 #if defined(ENABLE_STATISTICS)
325                 if (opt_stat)
326                         statistics_print_memory_usage();
327 #endif
328                 break;
329
330 #if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
331         default: {
332                 // For OpenJDK we dispatch all unknown signals to Java.
333                 methodinfo* m = class_resolvemethod(class_sun_misc_Signal, utf_dispatch, utf_int__void);
334                 (void) vm_call_method(m, NULL, sig);
335
336                 if (exceptions_get_exception()) {
337                         log_println("signal_thread_handler: Java signal handler throw an exception while dispatching signal %d:", sig);
338                         exceptions_print_stacktrace();
339                         vm_abort("signal_thread_handler: Aborting...");
340                 }
341                 break;
342         }
343 #else
344         default:
345                 vm_abort("signal_thread_handler: Unknown signal %d", sig);
346 #endif
347         }
348 }
349
350
351 /* signal_start_thread *********************************************************
352
353    Starts the signal handler thread.
354
355 *******************************************************************************/
356
357 bool signal_start_thread(void)
358 {
359 #if defined(ENABLE_THREADS)
360         utf *name;
361
362         name = utf_new_char("Signal Handler");
363
364         if (!threads_thread_start_internal(name, signal_thread))
365                 return false;
366
367         /* everything's ok */
368
369         return true;
370 #else
371 #warning FIX ME!
372 #endif
373 }
374
375
376 /* signal_handler_sighup *******************************************************
377
378    This handler is required by threads_thread_interrupt and does
379    nothing.
380
381 *******************************************************************************/
382
383 #if defined(ENABLE_THREADS)
384 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
385 {
386         /* do nothing */
387 }
388 #endif
389
390
391 /*
392  * These are local overrides for various environment variables in Emacs.
393  * Please do not remove this and leave it at the end of the file, where
394  * Emacs will automagically detect them.
395  * ---------------------------------------------------------------------
396  * Local variables:
397  * mode: c++
398  * indent-tabs-mode: t
399  * c-basic-offset: 4
400  * tab-width: 4
401  * End:
402  */