* signal_handler_sigint: Added, exits properly on <ctrl>-c.
[cacao.git] / src / vm / signal.c
1 /* src/vm/signal.c - machine independent signal functions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: signal.c 3290 2005-09-27 23:03:15Z twisti $
32
33 */
34
35
36 #include <signal.h>
37
38 #include "config.h"
39
40 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
41 # include "threads/native/threads.h"
42 #endif
43
44 #include "vm/options.h"
45 #include "vm/jit/stacktrace.h"
46
47
48 /* function prototypes ********************************************************/
49
50 void signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p);
51 void signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p);
52 void signal_handler_sigquit(int sig, siginfo_t *siginfo, void *_p);
53 void signal_handler_sigint(int sig, siginfo_t *siginfo, void *_p);
54 void signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p);
55
56
57 /* signal_init *****************************************************************
58
59    Initializes the signal subsystem and installs the signal handler.
60
61 *******************************************************************************/
62
63 void signal_init(void)
64 {
65         struct sigaction act;
66
67         /* install signal handlers we need to convert to exceptions */
68
69         sigemptyset(&act.sa_mask);
70
71
72 #if defined(ENABLE_JIT)
73 # if defined(ENABLE_INTRP)
74         if (!opt_intrp) {
75 # endif
76                 /* catch NullPointerException/StackOverFlowException */
77
78                 if (!checknull) {
79                         act.sa_sigaction = signal_handler_sigsegv;
80                         act.sa_flags = SA_NODEFER | SA_SIGINFO;
81
82 #if defined(SIGSEGV)
83                         sigaction(SIGSEGV, &act, NULL);
84 #endif
85
86 #if defined(SIGBUS)
87                         sigaction(SIGBUS, &act, NULL);
88 #endif
89                 }
90
91
92                 /* catch ArithmeticException */
93
94 #if defined(__I386__) || defined(__X86_64__)
95                 act.sa_sigaction = signal_handler_sigfpe;
96                 act.sa_flags = SA_NODEFER | SA_SIGINFO;
97                 sigaction(SIGFPE, &act, NULL);
98 #endif
99 # if defined(ENABLE_INTRP)
100         }
101 # endif
102 #endif /* !defined(__INTRP__) */
103
104
105         /* catch SIGINT for exiting properly on <ctrl>-c */
106
107         act.sa_sigaction = signal_handler_sigint;
108         act.sa_flags = SA_SIGINFO;
109         sigaction(SIGINT, &act, NULL);
110
111
112
113         /* catch SIGQUIT for thread dump */
114
115 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
116 #if !defined(__FREEBSD__)
117         act.sa_sigaction = signal_handler_sigquit;
118         act.sa_flags = SA_SIGINFO;
119         sigaction(SIGQUIT, &act, NULL);
120
121         /* XXX boehm uses SIGUSR1 for suspend on freebsd */
122
123         act.sa_sigaction = signal_handler_sigusr1;
124         act.sa_flags = SA_SIGINFO;
125         sigaction(SIGUSR1, &act, NULL);
126 #endif
127 #endif
128 }
129
130
131 /* signal_handler_sigquit ******************************************************
132
133    XXX
134
135 *******************************************************************************/
136
137 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
138 void signal_handler_sigquit(int sig, siginfo_t *siginfo, void *_p)
139 {
140         /* do thread dump */
141
142         thread_dump();
143 }
144 #endif
145
146
147 /* signal_handler_sigint *******************************************************
148
149    XXX
150
151 *******************************************************************************/
152
153 void signal_handler_sigint(int sig, siginfo_t *siginfo, void *_p)
154 {
155         /* exit the vm properly */
156
157         cacao_exit(0);
158 }
159
160
161 /* signal_handler_sigusr1 ******************************************************
162
163    XXX
164
165 *******************************************************************************/
166
167 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
168 void signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
169 {
170         /* call stacktrace function */
171
172         stacktrace_dump_trace();
173 }
174 #endif
175
176
177 /*
178  * These are local overrides for various environment variables in Emacs.
179  * Please do not remove this and leave it at the end of the file, where
180  * Emacs will automagically detect them.
181  * ---------------------------------------------------------------------
182  * Local variables:
183  * mode: c
184  * indent-tabs-mode: t
185  * c-basic-offset: 4
186  * tab-width: 4
187  * End:
188  */