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