* This is a rather huge commit, which changes the build order of
[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 7246 2007-01-29 18:49:05Z 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 "mm/memory.h"
47
48 #include "native/jni.h"
49 #include "native/include/java_lang_Thread.h"
50
51 #if defined(WITH_CLASSPATH_GNU)
52 # include "native/include/java_lang_VMThread.h"
53 #endif
54
55 #if defined(ENABLE_THREADS)
56 # include "threads/native/threads.h"
57 #endif
58
59 #include "vm/builtin.h"
60 #include "vm/signallocal.h"
61 #include "vm/stringlocal.h"
62 #include "vm/vm.h"
63 #include "vm/jit/stacktrace.h"
64
65 #include "vmcore/options.h"
66
67
68 /* global variables ***********************************************************/
69
70 #if defined(ENABLE_THREADS)
71 static threadobject *thread_signal;
72 #endif
73
74
75 /* function prototypes ********************************************************/
76
77 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
78
79
80 /* signal_init *****************************************************************
81
82    Initializes the signal subsystem and installs the signal handler.
83
84 *******************************************************************************/
85
86 void signal_init(void)
87 {
88 #if !defined(__CYGWIN__)
89         int              pagesize;
90         struct sigaction act;
91         sigset_t         mask;
92
93         /* mmap a memory page at address 0x0, so our hardware-exceptions
94            work. */
95
96         pagesize = getpagesize();
97
98         (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
99
100 #if defined(ENABLE_GC_BOEHM)
101         /* Allocate something so the garbage collector's signal handlers
102            are installed. */
103
104         (void) GCNEW(u1);
105 #endif
106
107         /* Install signal handlers for signals we want to catch in all
108            threads. */
109
110         sigemptyset(&act.sa_mask);
111
112 #if defined(ENABLE_JIT)
113 # if defined(ENABLE_INTRP)
114         if (!opt_intrp) {
115 # endif
116                 /* SIGSEGV handler */
117
118                 act.sa_sigaction = md_signal_handler_sigsegv;
119                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
120
121 #if defined(SIGSEGV)
122                 sigaction(SIGSEGV, &act, NULL);
123 #endif
124
125 #if defined(SIGBUS)
126                 sigaction(SIGBUS, &act, NULL);
127 #endif
128
129 #if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
130                 /* SIGFPE handler */
131
132                 act.sa_sigaction = md_signal_handler_sigfpe;
133                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
134                 sigaction(SIGFPE, &act, NULL);
135 #endif
136 # if defined(ENABLE_INTRP)
137         }
138 # endif
139 #endif /* !defined(ENABLE_INTRP) */
140
141 #if defined(ENABLE_THREADS)
142         /* SIGHUP handler for threads_thread_interrupt */
143
144         act.sa_sigaction = signal_handler_sighup;
145         act.sa_flags     = 0;
146         sigaction(SIGHUP, &act, NULL);
147 #endif
148
149 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
150         /* SIGUSR2 handler for profiling sampling */
151
152         act.sa_sigaction = md_signal_handler_sigusr2;
153         act.sa_flags     = SA_SIGINFO;
154         sigaction(SIGUSR2, &act, NULL);
155 #endif
156
157         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
158            <ctrl>-\).  We enable them later in signal_thread, but only for
159            this thread. */
160
161         sigemptyset(&mask);
162         sigaddset(&mask, SIGINT);
163 #if !defined(__FREEBSD__)
164         sigaddset(&mask, SIGQUIT);
165 #endif
166         sigprocmask(SIG_BLOCK, &mask, NULL);
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         sigemptyset(&mask);
187         sigaddset(&mask, SIGINT);
188 #if !defined(__FREEBSD__)
189         sigaddset(&mask, SIGQUIT);
190 #endif
191
192         while (true) {
193                 /* just wait for a signal */
194
195                 sigwait(&mask, &sig);
196
197                 switch (sig) {
198                 case SIGINT:
199                         /* exit the vm properly */
200
201                         vm_exit(0);
202                         break;
203
204                 case SIGQUIT:
205                         /* print a thread dump */
206
207                         threads_dump();
208
209 #if defined(ENABLE_STATISTICS)
210                         if (opt_stat)
211                                 statistics_print_memory_usage();
212 #endif
213                         break;
214                 }
215         }
216
217         /* this should not happen */
218
219         vm_abort("signal_thread: this thread should not exit!");
220 }
221
222
223 /* signal_start_thread *********************************************************
224
225    Starts the signal handler thread.
226
227 *******************************************************************************/
228
229 bool signal_start_thread(void)
230 {
231 #if defined(ENABLE_THREADS)
232 #if defined(WITH_CLASSPATH_GNU)
233         java_lang_VMThread *vmt;
234 #endif
235
236         /* create the finalizer object */
237
238         thread_signal = (threadobject *) builtin_new(class_java_lang_Thread);
239
240         if (thread_signal == NULL)
241                 return false;
242
243 #if defined(WITH_CLASSPATH_GNU)
244         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
245
246         vmt->thread = (java_lang_Thread *) thread_signal;
247
248         thread_signal->o.vmThread = vmt;
249 #endif
250
251         thread_signal->flags      = THREAD_FLAG_DAEMON;
252
253         thread_signal->o.name     = javastring_new_from_ascii("Signal Handler");
254 #if defined(ENABLE_JAVASE)
255         thread_signal->o.daemon   = true;
256 #endif
257         thread_signal->o.priority = 5;
258
259         /* actually start the finalizer thread */
260
261         threads_start_thread(thread_signal, signal_thread);
262
263         /* everything's ok */
264
265         return true;
266 #else
267 #error FIX ME!
268 #endif
269 }
270
271
272 /* signal_handler_sighup *******************************************************
273
274    This handler is required by threads_thread_interrupt and does
275    nothing.
276
277 *******************************************************************************/
278
279 #if defined(ENABLE_THREADS)
280 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
281 {
282         /* do nothing */
283 }
284 #endif
285
286
287 /*
288  * These are local overrides for various environment variables in Emacs.
289  * Please do not remove this and leave it at the end of the file, where
290  * Emacs will automagically detect them.
291  * ---------------------------------------------------------------------
292  * Local variables:
293  * mode: c
294  * indent-tabs-mode: t
295  * c-basic-offset: 4
296  * tab-width: 4
297  * End:
298  */