1 /* src/vm/signal.c - machine independent signal functions
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
8 This file is part of CACAO.
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.
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.
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
25 $Id: signal.c 7258 2007-01-30 13:53:35Z twisti $
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>
46 #include "mm/memory.h"
48 #include "native/jni.h"
49 #include "native/include/java_lang_Thread.h"
51 #if defined(WITH_CLASSPATH_GNU)
52 # include "native/include/java_lang_VMThread.h"
55 #if defined(ENABLE_THREADS)
56 # include "threads/native/threads.h"
59 #include "vm/builtin.h"
60 #include "vm/signallocal.h"
61 #include "vm/stringlocal.h"
63 #include "vm/jit/stacktrace.h"
65 #include "vmcore/options.h"
68 /* global variables ***********************************************************/
70 #if defined(ENABLE_THREADS)
71 static threadobject *thread_signal;
75 /* function prototypes ********************************************************/
77 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
80 /* signal_init *****************************************************************
82 Initializes the signal subsystem and installs the signal handler.
84 *******************************************************************************/
86 void signal_init(void)
88 #if !defined(__CYGWIN__)
93 /* mmap a memory page at address 0x0, so our hardware-exceptions
96 pagesize = getpagesize();
98 (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
101 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
102 <ctrl>-\). We enable them later in signal_thread, but only for
105 if (sigemptyset(&mask) != 0)
106 vm_abort("signal_init: sigemptyset failed: %s", strerror(errno));
108 if (sigaddset(&mask, SIGINT) != 0)
109 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
111 #if !defined(__FREEBSD__)
112 if (sigaddset(&mask, SIGQUIT) != 0)
113 vm_abort("signal_init: sigaddset failed: %s", strerror(errno));
116 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
117 vm_abort("signal_init: sigprocmask failed: %s", strerror(errno));
120 #if defined(ENABLE_GC_BOEHM)
121 /* Allocate something so the garbage collector's signal handlers
127 /* Install signal handlers for signals we want to catch in all
130 sigemptyset(&act.sa_mask);
132 #if defined(ENABLE_JIT)
133 # if defined(ENABLE_INTRP)
136 /* SIGSEGV handler */
138 act.sa_sigaction = md_signal_handler_sigsegv;
139 act.sa_flags = SA_NODEFER | SA_SIGINFO;
142 sigaction(SIGSEGV, &act, NULL);
146 sigaction(SIGBUS, &act, NULL);
149 #if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
152 act.sa_sigaction = md_signal_handler_sigfpe;
153 act.sa_flags = SA_NODEFER | SA_SIGINFO;
154 sigaction(SIGFPE, &act, NULL);
156 # if defined(ENABLE_INTRP)
159 #endif /* !defined(ENABLE_INTRP) */
161 #if defined(ENABLE_THREADS)
162 /* SIGHUP handler for threads_thread_interrupt */
164 act.sa_sigaction = signal_handler_sighup;
166 sigaction(SIGHUP, &act, NULL);
169 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
170 /* SIGUSR2 handler for profiling sampling */
172 act.sa_sigaction = md_signal_handler_sigusr2;
173 act.sa_flags = SA_SIGINFO;
174 sigaction(SIGUSR2, &act, NULL);
177 #endif /* !defined(__CYGWIN__) */
181 /* signal_thread ************************************************************
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.
188 *******************************************************************************/
190 static void signal_thread(void)
196 sigaddset(&mask, SIGINT);
197 #if !defined(__FREEBSD__)
198 sigaddset(&mask, SIGQUIT);
202 /* just wait for a signal */
204 sigwait(&mask, &sig);
205 log_println("signal caught: %d", sig);
209 /* exit the vm properly */
215 /* print a thread dump */
219 #if defined(ENABLE_STATISTICS)
221 statistics_print_memory_usage();
227 /* this should not happen */
229 vm_abort("signal_thread: this thread should not exit!");
233 /* signal_start_thread *********************************************************
235 Starts the signal handler thread.
237 *******************************************************************************/
239 bool signal_start_thread(void)
241 #if defined(ENABLE_THREADS)
242 #if defined(WITH_CLASSPATH_GNU)
243 java_lang_VMThread *vmt;
246 /* create the finalizer object */
248 thread_signal = (threadobject *) builtin_new(class_java_lang_Thread);
250 if (thread_signal == NULL)
253 #if defined(WITH_CLASSPATH_GNU)
254 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
256 vmt->thread = (java_lang_Thread *) thread_signal;
258 thread_signal->o.vmThread = vmt;
261 thread_signal->flags = THREAD_FLAG_DAEMON;
263 thread_signal->o.name = javastring_new_from_ascii("Signal Handler");
264 #if defined(ENABLE_JAVASE)
265 thread_signal->o.daemon = true;
267 thread_signal->o.priority = 5;
269 /* actually start the finalizer thread */
271 threads_start_thread(thread_signal, signal_thread);
273 /* everything's ok */
282 /* signal_handler_sighup *******************************************************
284 This handler is required by threads_thread_interrupt and does
287 *******************************************************************************/
289 #if defined(ENABLE_THREADS)
290 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
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 * ---------------------------------------------------------------------
304 * indent-tabs-mode: t