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