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