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