c8e5a31dbbdf7368601310fc8adff4db5a4361ea
[cacao.git] / src / vm / signal.c
1 /* src/vm/signal.c - machine independent signal functions
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    $Id: signal.c 6256 2006-12-28 12:30:09Z twisti $
30
31 */
32
33
34 #include "config.h"
35
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/mman.h>
41
42 #if defined(__DARWIN__)
43 /* If we compile with -ansi on darwin, <sys/types.h> is not
44  included. So let's do it here. */
45 # include <sys/types.h>
46 #endif
47
48 #include "vm/types.h"
49
50 #if defined(ENABLE_THREADS)
51 # include "threads/native/threads.h"
52 #endif
53
54 #include "mm/memory.h"
55 #include "vm/signallocal.h"
56 #include "vm/options.h"
57 #include "vm/vm.h"
58 #include "vm/jit/stacktrace.h"
59
60
61 /* function prototypes ********************************************************/
62
63 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p);
64 void signal_handler_sigint(int sig, siginfo_t *siginfo, void *_p);
65 void signal_handler_sigquit(int sig, siginfo_t *siginfo, void *_p);
66
67
68 /* signal_init *****************************************************************
69
70    Initializes the signal subsystem and installs the signal handler.
71
72 *******************************************************************************/
73
74 void signal_init(void)
75 {
76 #if !defined(__CYGWIN__)
77         int              pagesize;
78         struct sigaction act;
79
80         /* mmap a memory page at address 0x0, so our hardware-exceptions
81            work. */
82
83         pagesize = getpagesize();
84
85         (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
86
87 #if defined(ENABLE_GC_BOEHM)
88         /* Allocate something so the garbage collector's signal handlers
89            are installed. */
90
91         (void) GCNEW(u1);
92 #endif
93
94         /* install signal handlers we need to convert to exceptions */
95
96         sigemptyset(&act.sa_mask);
97
98 #if defined(ENABLE_JIT)
99 # if defined(ENABLE_INTRP)
100         if (!opt_intrp) {
101 # endif
102                 /* catch NullPointerException/StackOverFlowException */
103
104                 act.sa_sigaction = md_signal_handler_sigsegv;
105                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
106
107 #if defined(SIGSEGV)
108                 sigaction(SIGSEGV, &act, NULL);
109 #endif
110
111 #if defined(SIGBUS)
112                 sigaction(SIGBUS, &act, NULL);
113 #endif
114
115                 /* catch ArithmeticException */
116
117 #if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
118                 act.sa_sigaction = md_signal_handler_sigfpe;
119                 act.sa_flags     = SA_NODEFER | SA_SIGINFO;
120                 sigaction(SIGFPE, &act, NULL);
121 #endif
122 # if defined(ENABLE_INTRP)
123         }
124 # endif
125 #endif /* !defined(ENABLE_INTRP) */
126
127 #if defined(ENABLE_THREADS)
128         /* catch SIGHUP for threads_thread_interrupt */
129
130         act.sa_sigaction = signal_handler_sighup;
131         act.sa_flags     = 0;
132         sigaction(SIGHUP, &act, NULL);
133 #endif
134
135         /* catch SIGINT for exiting properly on <ctrl>-c */
136
137         act.sa_sigaction = signal_handler_sigint;
138         act.sa_flags     = SA_NODEFER | SA_SIGINFO;
139         sigaction(SIGINT, &act, NULL);
140
141 #if defined(ENABLE_THREADS)
142         /* catch SIGQUIT for thread dump */
143
144 # if !defined(__FREEBSD__)
145         act.sa_sigaction = signal_handler_sigquit;
146         act.sa_flags     = SA_SIGINFO;
147         sigaction(SIGQUIT, &act, NULL);
148 # endif
149 #endif
150
151 #if defined(ENABLE_THREADS) && defined(ENABLE_PROFILING)
152         /* install signal handler for profiling sampling */
153
154         act.sa_sigaction = md_signal_handler_sigusr2;
155         act.sa_flags     = SA_SIGINFO;
156         sigaction(SIGUSR2, &act, NULL);
157 #endif
158
159 #endif /* !defined(__CYGWIN__) */
160 }
161
162
163 /* signal_handler_sighup *******************************************************
164
165    This handler is required by threads_thread_interrupt and does
166    nothing.
167
168 *******************************************************************************/
169
170 #if defined(ENABLE_THREADS)
171 void signal_handler_sighup(int sig, siginfo_t *siginfo, void *_p)
172 {
173         /* do nothing */
174 }
175 #endif
176
177
178 /* signal_handler_sigquit ******************************************************
179
180    Handle for SIGQUIT (<ctrl>-\) which print a stacktrace for every
181    running thread.
182
183 *******************************************************************************/
184
185 #if defined(ENABLE_THREADS)
186 void signal_handler_sigquit(int sig, siginfo_t *siginfo, void *_p)
187 {
188         /* do thread dump */
189
190         threads_dump();
191 }
192 #endif
193
194
195 /* signal_handler_sigint *******************************************************
196
197    Handler for SIGINT (<ctrl>-c) which shuts down CACAO properly with
198    Runtime.exit(I)V.
199
200 *******************************************************************************/
201
202 void signal_handler_sigint(int sig, siginfo_t *siginfo, void *_p)
203 {
204         /* if we are already in Runtime.exit(), just do it hardcore */
205
206         if (vm_exiting) {
207                 fprintf(stderr, "Caught SIGINT while already shutting down. Shutdown aborted...\n");
208                 exit(0);
209         }
210
211         /* exit the vm properly */
212
213         vm_exit(0);
214 }
215
216
217 /*
218  * These are local overrides for various environment variables in Emacs.
219  * Please do not remove this and leave it at the end of the file, where
220  * Emacs will automagically detect them.
221  * ---------------------------------------------------------------------
222  * Local variables:
223  * mode: c
224  * indent-tabs-mode: t
225  * c-basic-offset: 4
226  * tab-width: 4
227  * End:
228  */