* src/vm/signal.c (signal_init): Use if-vm_abort instead of assert.
[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 8004 2007-06-04 12:59:04Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/mman.h>
38
39 #if defined(__DARWIN__)
40 /* If we compile with -ansi on darwin, <sys/types.h> is not
41  included. So let's do it here. */
42 # include <sys/types.h>
43 #endif
44
45 #include "vm/types.h"
46
47 #include "arch.h"
48
49 #include "mm/memory.h"
50
51 #if defined(ENABLE_THREADS)
52 # include "threads/threads-common.h"
53 #else
54 # include "threads/none/threads.h"
55 #endif
56
57 #include "vm/exceptions.h"
58 #include "vm/signallocal.h"
59 #include "vm/vm.h"
60
61 #include "vmcore/options.h"
62
63 #if defined(ENABLE_STATISTICS)
64 # include "vmcore/statistics.h"
65 #endif
66
67
68 /* function prototypes ********************************************************/
69
70 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
71
72
73 /* signal_init *****************************************************************
74
75    Initializes the signal subsystem and installs the signal handler.
76
77 *******************************************************************************/
78
79 bool signal_init(void)
80 {
81 #if !defined(__CYGWIN__)
82         int              pagesize;
83         sigset_t         mask;
84         struct sigaction act;
85
86         /* mmap a memory page at address 0x0, so our hardware-exceptions
87            work. */
88
89         pagesize = getpagesize();
90
91         (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
92
93         /* check if we get into trouble with our hardware-exceptions */
94
95         if (OFFSET(java_bytearray, data) <= EXCEPTION_HARDWARE_PATCHER)
96                 vm_abort("signal_init: array-data offset is less or equal the maximum hardware-exception displacement: %d <= %d", OFFSET(java_bytearray, data) <= EXCEPTION_HARDWARE_PATCHER);
97
98 #if defined(__LINUX__) && defined(ENABLE_THREADS)
99         /* XXX Remove for exact-GC. */
100         if (threads_pthreads_implementation_nptl) {
101 #endif
102
103         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
104            <ctrl>-\).  We enable them later in signal_thread, but only for
105            this thread. */
106
107         if (sigemptyset(&mask) != 0)
108                 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
109
110         if (sigaddset(&mask, SIGINT) != 0)
111                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
112
113 #if !defined(__FREEBSD__)
114         if (sigaddset(&mask, SIGQUIT) != 0)
115                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
116 #endif
117
118         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
119                 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
120
121 #if defined(__LINUX__) && defined(ENABLE_THREADS)
122         /* XXX Remove for exact-GC. */
123         }
124 #endif
125
126 #if defined(ENABLE_GC_BOEHM)
127         /* Allocate something so the garbage collector's signal handlers
128            are installed. */
129
130         (void) GCNEW(u1);
131 #endif
132
133         /* Install signal handlers for signals we want to catch in all
134            threads. */
135
136         sigemptyset(&act.sa_mask);
137
138 #if defined(ENABLE_JIT)
139 # if defined(ENABLE_INTRP)
140         if (!opt_intrp) {
141 # endif
142                 /* SIGSEGV handler */
143
144                 act.sa_sigaction = md_signal_handler_sigsegv;
145                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
146
147 #  if defined(SIGSEGV)
148                 sigaction(SIGSEGV, &act, NULL);
149 #  endif
150
151 #  if defined(SIGBUS)
152                 sigaction(SIGBUS, &act, NULL);
153 #  endif
154
155 #  if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
156                 /* SIGFPE handler */
157
158                 act.sa_sigaction = md_signal_handler_sigfpe;
159                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
160                 sigaction(SIGFPE, &act, NULL);
161 #  endif
162
163 #  if defined(__ARM__) || defined(__S390__)
164                 /* XXX use better defines for that (in arch.h) */
165                 /* SIGILL handler */
166
167                 act.sa_sigaction = md_signal_handler_sigill;
168                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
169                 sigaction(SIGILL, &act, NULL);
170 #  endif
171
172 #  if defined(__POWERPC__)
173                 /* XXX use better defines for that (in arch.h) */
174                 /* SIGTRAP handler */
175
176                 act.sa_sigaction = md_signal_handler_sigtrap;
177                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
178                 sigaction(SIGTRAP, &act, NULL);
179 #  endif
180 # if defined(ENABLE_INTRP)
181         }
182 # endif
183 #endif /* !defined(ENABLE_INTRP) */
184
185 #if defined(ENABLE_THREADS)
186         /* SIGHUP handler for threads_thread_interrupt */
187
188         act.sa_sigaction = signal_handler_sighup;
189         act.sa_flags     = 0;
190         sigaction(SIGHUP, &act, NULL);
191 #endif
192
193 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
194         /* SIGUSR2 handler for profiling sampling */
195
196         act.sa_sigaction = md_signal_handler_sigusr2;
197         act.sa_flags     = SA_SIGINFO;
198         sigaction(SIGUSR2, &act, NULL);
199 #endif
200
201 #endif /* !defined(__CYGWIN__) */
202
203         return true;
204 }
205
206
207 /* signal_thread ************************************************************
208
209    This thread sets the signal mask to catch the user input signals
210    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
211    signals on every single thread running.
212
213 *******************************************************************************/
214
215 static void signal_thread(void)
216 {
217         threadobject *t;
218         sigset_t      mask;
219         int           sig;
220
221         t = THREADOBJECT;
222
223         if (sigemptyset(&mask) != 0)
224                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
225
226         if (sigaddset(&mask, SIGINT) != 0)
227                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
228
229 #if !defined(__FREEBSD__)
230         if (sigaddset(&mask, SIGQUIT) != 0)
231                 vm_abort("signal_thread: sigaddset failed: %s", strerror(errno));
232 #endif
233
234         while (true) {
235                 /* just wait for a signal */
236
237                 /* XXX We don't check for an error here, although the man-page
238                    states sigwait does not return an error (which is wrong!),
239                    but it seems to make problems with Boehm-GC.  We should
240                    revisit this code with our new exact-GC. */
241
242 #if defined(ENABLE_THREADS)
243                 threads_thread_state_waiting(t);
244 #endif
245
246 /*              if (sigwait(&mask, &sig) != 0) */
247 /*                      vm_abort("signal_thread: sigwait failed: %s", strerror(errno)); */
248                 (void) sigwait(&mask, &sig);
249
250 #if defined(ENABLE_THREADS)
251                 threads_thread_state_runnable(t);
252 #endif
253
254                 switch (sig) {
255                 case SIGINT:
256                         /* exit the vm properly */
257
258                         vm_exit(0);
259                         break;
260
261                 case SIGQUIT:
262                         /* print a thread dump */
263 #if defined(ENABLE_THREADS)
264                         threads_dump();
265 #endif
266
267 #if defined(ENABLE_STATISTICS)
268                         if (opt_stat)
269                                 statistics_print_memory_usage();
270 #endif
271                         break;
272                 }
273         }
274
275         /* this should not happen */
276
277         vm_abort("signal_thread: this thread should not exit!");
278 }
279
280
281 /* signal_start_thread *********************************************************
282
283    Starts the signal handler thread.
284
285 *******************************************************************************/
286
287 bool signal_start_thread(void)
288 {
289 #if defined(ENABLE_THREADS)
290         utf *name;
291
292         name = utf_new_char("Signal Handler");
293
294         if (!threads_thread_start_internal(name, signal_thread))
295                 return false;
296
297         /* everything's ok */
298
299         return true;
300 #else
301 #warning FIX ME!
302 #endif
303 }
304
305
306 /* signal_handler_sighup *******************************************************
307
308    This handler is required by threads_thread_interrupt and does
309    nothing.
310
311 *******************************************************************************/
312
313 #if defined(ENABLE_THREADS)
314 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
315 {
316         /* do nothing */
317 }
318 #endif
319
320
321 /*
322  * These are local overrides for various environment variables in Emacs.
323  * Please do not remove this and leave it at the end of the file, where
324  * Emacs will automagically detect them.
325  * ---------------------------------------------------------------------
326  * Local variables:
327  * mode: c
328  * indent-tabs-mode: t
329  * c-basic-offset: 4
330  * tab-width: 4
331  * End:
332  */