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 7246 2007-01-29 18:49:05Z 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);
100 #if defined(ENABLE_GC_BOEHM)
101 /* Allocate something so the garbage collector's signal handlers
107 /* Install signal handlers for signals we want to catch in all
110 sigemptyset(&act.sa_mask);
112 #if defined(ENABLE_JIT)
113 # if defined(ENABLE_INTRP)
116 /* SIGSEGV handler */
118 act.sa_sigaction = md_signal_handler_sigsegv;
119 act.sa_flags = SA_NODEFER | SA_SIGINFO;
122 sigaction(SIGSEGV, &act, NULL);
126 sigaction(SIGBUS, &act, NULL);
129 #if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
132 act.sa_sigaction = md_signal_handler_sigfpe;
133 act.sa_flags = SA_NODEFER | SA_SIGINFO;
134 sigaction(SIGFPE, &act, NULL);
136 # if defined(ENABLE_INTRP)
139 #endif /* !defined(ENABLE_INTRP) */
141 #if defined(ENABLE_THREADS)
142 /* SIGHUP handler for threads_thread_interrupt */
144 act.sa_sigaction = signal_handler_sighup;
146 sigaction(SIGHUP, &act, NULL);
149 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
150 /* SIGUSR2 handler for profiling sampling */
152 act.sa_sigaction = md_signal_handler_sigusr2;
153 act.sa_flags = SA_SIGINFO;
154 sigaction(SIGUSR2, &act, NULL);
157 /* Block the following signals (SIGINT for <ctrl>-c, SIGQUIT for
158 <ctrl>-\). We enable them later in signal_thread, but only for
162 sigaddset(&mask, SIGINT);
163 #if !defined(__FREEBSD__)
164 sigaddset(&mask, SIGQUIT);
166 sigprocmask(SIG_BLOCK, &mask, NULL);
168 #endif /* !defined(__CYGWIN__) */
172 /* signal_thread ************************************************************
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.
179 *******************************************************************************/
181 static void signal_thread(void)
187 sigaddset(&mask, SIGINT);
188 #if !defined(__FREEBSD__)
189 sigaddset(&mask, SIGQUIT);
193 /* just wait for a signal */
195 sigwait(&mask, &sig);
199 /* exit the vm properly */
205 /* print a thread dump */
209 #if defined(ENABLE_STATISTICS)
211 statistics_print_memory_usage();
217 /* this should not happen */
219 vm_abort("signal_thread: this thread should not exit!");
223 /* signal_start_thread *********************************************************
225 Starts the signal handler thread.
227 *******************************************************************************/
229 bool signal_start_thread(void)
231 #if defined(ENABLE_THREADS)
232 #if defined(WITH_CLASSPATH_GNU)
233 java_lang_VMThread *vmt;
236 /* create the finalizer object */
238 thread_signal = (threadobject *) builtin_new(class_java_lang_Thread);
240 if (thread_signal == NULL)
243 #if defined(WITH_CLASSPATH_GNU)
244 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
246 vmt->thread = (java_lang_Thread *) thread_signal;
248 thread_signal->o.vmThread = vmt;
251 thread_signal->flags = THREAD_FLAG_DAEMON;
253 thread_signal->o.name = javastring_new_from_ascii("Signal Handler");
254 #if defined(ENABLE_JAVASE)
255 thread_signal->o.daemon = true;
257 thread_signal->o.priority = 5;
259 /* actually start the finalizer thread */
261 threads_start_thread(thread_signal, signal_thread);
263 /* everything's ok */
272 /* signal_handler_sighup *******************************************************
274 This handler is required by threads_thread_interrupt and does
277 *******************************************************************************/
279 #if defined(ENABLE_THREADS)
280 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
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 * ---------------------------------------------------------------------
294 * indent-tabs-mode: t