40a4a436b54052411e140a4460f474f0e6a8b475
[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 7471 2007-03-06 17:01:49Z twisti $
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 # if defined(ENABLE_INTRP)
147         }
148 # endif
149 #endif /* !defined(ENABLE_INTRP) */
150
151 #if defined(ENABLE_THREADS)
152         /* SIGHUP handler for threads_thread_interrupt */
153
154         act.sa_sigaction = signal_handler_sighup;
155         act.sa_flags     = 0;
156         sigaction(SIGHUP, &act, NULL);
157 #endif
158
159 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
160         /* SIGUSR2 handler for profiling sampling */
161
162         act.sa_sigaction = md_signal_handler_sigusr2;
163         act.sa_flags     = SA_SIGINFO;
164         sigaction(SIGUSR2, &act, NULL);
165 #endif
166
167 #endif /* !defined(__CYGWIN__) */
168 }
169
170
171 /* signal_thread ************************************************************
172
173    This thread sets the signal mask to catch the user input signals
174    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
175    signals on every single thread running.  Especially, this makes
176    problems on slow machines.
177
178 *******************************************************************************/
179
180 static void signal_thread(void)
181 {
182         sigset_t mask;
183         int      sig;
184
185         if (sigemptyset(&mask) != 0)
186                 vm_abort("signal_thread: sigemptyset failed: %s", strerror(errno));
187
188         sigaddset(&mask, SIGINT);
189 #if !defined(__FREEBSD__)
190         sigaddset(&mask, SIGQUIT);
191 #endif
192
193         while (true) {
194                 /* just wait for a signal */
195
196                 (void) sigwait(&mask, &sig);
197
198                 switch (sig) {
199                 case SIGINT:
200                         /* exit the vm properly */
201
202                         vm_exit(0);
203                         break;
204
205                 case SIGQUIT:
206                         /* print a thread dump */
207 #if defined(ENABLE_THREADS)
208                         threads_dump();
209 #endif
210
211 #if defined(ENABLE_STATISTICS)
212                         if (opt_stat)
213                                 statistics_print_memory_usage();
214 #endif
215                         break;
216                 }
217         }
218
219         /* this should not happen */
220
221         vm_abort("signal_thread: this thread should not exit!");
222 }
223
224
225 /* signal_start_thread *********************************************************
226
227    Starts the signal handler thread.
228
229 *******************************************************************************/
230
231 bool signal_start_thread(void)
232 {
233 #if defined(ENABLE_THREADS)
234         utf *name;
235
236         name = utf_new_char("Signal Handler");
237
238         thread_signal = threads_create_thread(name);
239
240         if (thread_signal == NULL)
241                 return false;
242
243         /* actually start the signal handler thread */
244
245         threads_start_thread(thread_signal, signal_thread);
246
247         /* everything's ok */
248
249         return true;
250 #else
251 #warning FIX ME!
252 #endif
253 }
254
255
256 /* signal_handler_sighup *******************************************************
257
258    This handler is required by threads_thread_interrupt and does
259    nothing.
260
261 *******************************************************************************/
262
263 #if defined(ENABLE_THREADS)
264 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
265 {
266         /* do nothing */
267 }
268 #endif
269
270
271 /*
272  * These are local overrides for various environment variables in Emacs.
273  * Please do not remove this and leave it at the end of the file, where
274  * Emacs will automagically detect them.
275  * ---------------------------------------------------------------------
276  * Local variables:
277  * mode: c
278  * indent-tabs-mode: t
279  * c-basic-offset: 4
280  * tab-width: 4
281  * End:
282  */