* src/vm/jit/code.h (codeinfo) [ENABLE_PROFILING]: Made frequency,
[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 7258 2007-01-30 13:53:35Z 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         sigset_t         mask;
91         struct sigaction act;
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 0
101         /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
102            <ctrl>-\).  We enable them later in signal_thread, but only for
103            this thread. */
104
105         if (sigemptyset(&mask) != 0)
106                 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
107
108         if (sigaddset(&mask, SIGINT) != 0)
109                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
110
111 #if !defined(__FREEBSD__)
112         if (sigaddset(&mask, SIGQUIT) != 0)
113                 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
114 #endif
115
116         if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
117                 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
118 #endif
119
120 #if defined(ENABLE_GC_BOEHM)
121         /* Allocate something so the garbage collector's signal handlers
122            are installed. */
123
124         (void) GCNEW(u1);
125 #endif
126
127         /* Install signal handlers for signals we want to catch in all
128            threads. */
129
130         sigemptyset(&act.sa_mask);
131
132 #if defined(ENABLE_JIT)
133 # if defined(ENABLE_INTRP)
134         if (!opt_intrp) {
135 # endif
136                 /* SIGSEGV handler */
137
138                 act.sa_sigaction = md_signal_handler_sigsegv;
139                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
140
141 #if defined(SIGSEGV)
142                 sigaction(SIGSEGV, &act, NULL);
143 #endif
144
145 #if defined(SIGBUS)
146                 sigaction(SIGBUS, &act, NULL);
147 #endif
148
149 #if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
150                 /* SIGFPE handler */
151
152                 act.sa_sigaction = md_signal_handler_sigfpe;
153                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
154                 sigaction(SIGFPE, &act, NULL);
155 #endif
156 # if defined(ENABLE_INTRP)
157         }
158 # endif
159 #endif /* !defined(ENABLE_INTRP) */
160
161 #if defined(ENABLE_THREADS)
162         /* SIGHUP handler for threads_thread_interrupt */
163
164         act.sa_sigaction = signal_handler_sighup;
165         act.sa_flags     = 0;
166         sigaction(SIGHUP, &act, NULL);
167 #endif
168
169 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
170         /* SIGUSR2 handler for profiling sampling */
171
172         act.sa_sigaction = md_signal_handler_sigusr2;
173         act.sa_flags     = SA_SIGINFO;
174         sigaction(SIGUSR2, &act, NULL);
175 #endif
176
177 #endif /* !defined(__CYGWIN__) */
178 }
179
180
181 /* signal_thread ************************************************************
182
183    This thread sets the signal mask to catch the user input signals
184    (SIGINT, SIGQUIT).  We use such a thread, so we don't get the
185    signals on every single thread running.  Especially, this makes
186    problems on slow machines.
187
188 *******************************************************************************/
189
190 static void signal_thread(void)
191 {
192         sigset_t mask;
193         int      sig;
194
195         sigemptyset(&mask);
196         sigaddset(&mask, SIGINT);
197 #if !defined(__FREEBSD__)
198         sigaddset(&mask, SIGQUIT);
199 #endif
200
201         while (true) {
202                 /* just wait for a signal */
203
204                 sigwait(&mask, &sig);
205                 log_println("signal caught: %d", sig);
206
207                 switch (sig) {
208                 case SIGINT:
209                         /* exit the vm properly */
210
211                         vm_exit(0);
212                         break;
213
214                 case SIGQUIT:
215                         /* print a thread dump */
216
217                         threads_dump();
218
219 #if defined(ENABLE_STATISTICS)
220                         if (opt_stat)
221                                 statistics_print_memory_usage();
222 #endif
223                         break;
224                 }
225         }
226
227         /* this should not happen */
228
229         vm_abort("signal_thread: this thread should not exit!");
230 }
231
232
233 /* signal_start_thread *********************************************************
234
235    Starts the signal handler thread.
236
237 *******************************************************************************/
238
239 bool signal_start_thread(void)
240 {
241 #if defined(ENABLE_THREADS)
242 #if defined(WITH_CLASSPATH_GNU)
243         java_lang_VMThread *vmt;
244 #endif
245
246         /* create the finalizer object */
247
248         thread_signal = (threadobject *) builtin_new(class_java_lang_Thread);
249
250         if (thread_signal == NULL)
251                 return false;
252
253 #if defined(WITH_CLASSPATH_GNU)
254         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
255
256         vmt->thread = (java_lang_Thread *) thread_signal;
257
258         thread_signal->o.vmThread = vmt;
259 #endif
260
261         thread_signal->flags      = THREAD_FLAG_DAEMON;
262
263         thread_signal->o.name     = javastring_new_from_ascii("Signal Handler");
264 #if defined(ENABLE_JAVASE)
265         thread_signal->o.daemon   = true;
266 #endif
267         thread_signal->o.priority = 5;
268
269         /* actually start the finalizer thread */
270
271         threads_start_thread(thread_signal, signal_thread);
272
273         /* everything's ok */
274
275         return true;
276 #else
277 #error 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  */