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