096bb0c93a09b8605a67c6e397026f237c5e4ecf
[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 7601 2007-03-28 23:02:50Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <errno.h>
33 #include <signal.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <sys/mman.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 "vm/types.h"
45
46 #include "arch.h"
47
48 #include "mm/memory.h"
49
50 #if defined(ENABLE_THREADS)
51 # include "threads/threads-common.h"
52 #endif
53
54 #include "vm/signallocal.h"
55 #include "vm/vm.h"
56
57 #include "vmcore/options.h"
58
59
60 /* global variables ***********************************************************/
61
62 #if defined(ENABLE_THREADS)
63 static threadobject *thread_signal;
64 #endif
65
66
67 /* function prototypes ********************************************************/
68
69 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
70
71
72 /* signal_init *****************************************************************
73
74    Initializes the signal subsystem and installs the signal handler.
75
76 *******************************************************************************/
77
78 void signal_init(void)
79 {
80 #if !defined(__CYGWIN__)
81         int              pagesize;
82         sigset_t         mask;
83         struct sigaction act;
84
85         /* mmap a memory page at address 0x0, so our hardware-exceptions
86            work. */
87
88         pagesize = getpagesize();
89
90         (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
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(ENABLE_GC_BOEHM)
111         /* Allocate something so the garbage collector's signal handlers
112            are installed. */
113
114         (void) GCNEW(u1);
115 #endif
116
117         /* Install signal handlers for signals we want to catch in all
118            threads. */
119
120         sigemptyset(&act.sa_mask);
121
122 #if defined(ENABLE_JIT)
123 # if defined(ENABLE_INTRP)
124         if (!opt_intrp) {
125 # endif
126                 /* SIGSEGV handler */
127
128                 act.sa_sigaction = md_signal_handler_sigsegv;
129                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
130
131 #  if defined(SIGSEGV)
132                 sigaction(SIGSEGV, &act, NULL);
133 #  endif
134
135 #  if defined(SIGBUS)
136                 sigaction(SIGBUS, &act, NULL);
137 #  endif
138
139 #  if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
140                 /* SIGFPE handler */
141
142                 act.sa_sigaction = md_signal_handler_sigfpe;
143                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
144                 sigaction(SIGFPE, &act, NULL);
145 #  endif
146
147 #  if defined(__ARM__)
148                 /* XXX use better defines for that (in arch.h) */
149                 /* SIGILL handler */
150
151                 act.sa_sigaction = md_signal_handler_sigill;
152                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
153                 sigaction(SIGILL, &act, NULL);
154 #  endif
155
156 #  if defined(__POWERPC__)
157                 /* XXX use better defines for that (in arch.h) */
158                 /* SIGTRAP handler */
159
160                 act.sa_sigaction = md_signal_handler_sigtrap;
161                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
162                 sigaction(SIGTRAP, &act, NULL);
163 #  endif
164 # if defined(ENABLE_INTRP)
165         }
166 # endif
167 #endif /* !defined(ENABLE_INTRP) */
168
169 #if defined(ENABLE_THREADS)
170         /* SIGHUP handler for threads_thread_interrupt */
171
172         act.sa_sigaction = signal_handler_sighup;
173         act.sa_flags     = 0;
174         sigaction(SIGHUP, &act, NULL);
175 #endif
176
177 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
178         /* SIGUSR1 handler for the exact GC to suspend threads */
179
180         act.sa_sigaction = md_signal_handler_sigusr1;
181         act.sa_flags     = SA_SIGINFO;
182         sigaction(SIGUSR1, &act, NULL);
183 #endif
184
185 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
186         /* SIGUSR2 handler for profiling sampling */
187
188         act.sa_sigaction = md_signal_handler_sigusr2;
189         act.sa_flags     = SA_SIGINFO;
190         sigaction(SIGUSR2, &act, NULL);
191 #endif
192
193 #endif /* !defined(__CYGWIN__) */
194 }
195
196
197 /* signal_thread ************************************************************
198
199    This thread sets the signal mask to catch the user input signals
200    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
201    signals on every single thread running.  Especially, this makes
202    problems on slow machines.
203
204 *******************************************************************************/
205
206 static void signal_thread(void)
207 {
208         sigset_t mask;
209         int      sig;
210
211         if (sigemptyset(&mask) != 0)
212                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
213
214         sigaddset(&mask, SIGINT);
215 #if !defined(__FREEBSD__)
216         sigaddset(&mask, SIGQUIT);
217 #endif
218
219         while (true) {
220                 /* just wait for a signal */
221
222                 (void) sigwait(&mask, &sig);
223
224                 switch (sig) {
225                 case SIGINT:
226                         /* exit the vm properly */
227
228                         vm_exit(0);
229                         break;
230
231                 case SIGQUIT:
232                         /* print a thread dump */
233 #if defined(ENABLE_THREADS)
234                         threads_dump();
235 #endif
236
237 #if defined(ENABLE_STATISTICS)
238                         if (opt_stat)
239                                 statistics_print_memory_usage();
240 #endif
241                         break;
242                 }
243         }
244
245         /* this should not happen */
246
247         vm_abort("signal_thread: this thread should not exit!");
248 }
249
250
251 /* signal_start_thread *********************************************************
252
253    Starts the signal handler thread.
254
255 *******************************************************************************/
256
257 bool signal_start_thread(void)
258 {
259 #if defined(ENABLE_THREADS)
260         utf *name;
261
262         name = utf_new_char("Signal Handler");
263
264         thread_signal = threads_create_thread(name);
265
266         if (thread_signal == NULL)
267                 return false;
268
269         /* actually start the signal handler thread */
270
271         threads_start_thread(thread_signal, signal_thread);
272
273         /* everything's ok */
274
275         return true;
276 #else
277 #warning FIX ME!
278 #endif
279 }
280
281
282 /* signal_handler_sighup *******************************************************
283
284    This handler is required by threads_thread_interrupt and does
285    nothing.
286
287 *******************************************************************************/
288
289 #if defined(ENABLE_THREADS)
290 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
291 {
292         /* do nothing */
293 }
294 #endif
295
296
297 /*
298  * These are local overrides for various environment variables in Emacs.
299  * Please do not remove this and leave it at the end of the file, where
300  * Emacs will automagically detect them.
301  * ---------------------------------------------------------------------
302  * Local variables:
303  * mode: c
304  * indent-tabs-mode: t
305  * c-basic-offset: 4
306  * tab-width: 4
307  * End:
308  */