src/vm/vm.c (vm_exit): only set JVMTI_PHASE_DEAD if a jvmti agent or a jvmti environm...
[cacao.git] / src / vm / signal.c
index 025644c7edebd0b0a659ef88483517428dbdf6e8..d4520fcd82487535ef6663c952442ed808bc9336 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/signal.c - machine independent signal functions
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Christian Thalinger
 
    Changes:
 
-   $Id: signal.c 2973 2005-07-10 15:54:50Z twisti $
+   $Id: signal.c 4921 2006-05-15 14:24:36Z twisti $
 
 */
 
 
+#include "config.h"
+
 #include <signal.h>
+#include <stdlib.h>
 
-#include "config.h"
+#include "vm/types.h"
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+#if defined(ENABLE_THREADS)
 # include "threads/native/threads.h"
 #endif
 
+#include "vm/signallocal.h"
 #include "vm/options.h"
+#include "vm/vm.h"
 #include "vm/jit/stacktrace.h"
 
 
 /* function prototypes ********************************************************/
 
-void signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p);
-void signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p);
 void signal_handler_sigquit(int sig, siginfo_t *siginfo, void *_p);
+void signal_handler_sigint(int sig, siginfo_t *siginfo, void *_p);
 void signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p);
 
 
@@ -68,34 +72,50 @@ void signal_init(void)
        sigemptyset(&act.sa_mask);
 
 
-       /* catch NullPointerException/StackOverFlowException */
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_INTRP)
+       if (!opt_intrp) {
+# endif
+               /* catch NullPointerException/StackOverFlowException */
 
-       if (!checknull) {
-               act.sa_sigaction = signal_handler_sigsegv;
-               act.sa_flags = SA_NODEFER | SA_SIGINFO;
+               if (!checknull) {
+                       act.sa_sigaction = md_signal_handler_sigsegv;
+                       act.sa_flags = SA_NODEFER | SA_SIGINFO;
 
 #if defined(SIGSEGV)
-               sigaction(SIGSEGV, &act, NULL);
+                       sigaction(SIGSEGV, &act, NULL);
 #endif
 
 #if defined(SIGBUS)
-               sigaction(SIGBUS, &act, NULL);
+                       sigaction(SIGBUS, &act, NULL);
 #endif
-       }
+               }
 
 
-       /* catch ArithmeticException */
+               /* catch ArithmeticException */
 
 #if defined(__I386__) || defined(__X86_64__)
-       act.sa_sigaction = signal_handler_sigfpe;
-       act.sa_flags = SA_NODEFER | SA_SIGINFO;
-       sigaction(SIGFPE, &act, NULL);
+               act.sa_sigaction = md_signal_handler_sigfpe;
+               act.sa_flags = SA_NODEFER | SA_SIGINFO;
+               sigaction(SIGFPE, &act, NULL);
 #endif
+# if defined(ENABLE_INTRP)
+       }
+# endif
+#endif /* !defined(ENABLE_INTRP) */
+
+
+       /* catch SIGINT for exiting properly on <ctrl>-c */
+
+       act.sa_sigaction = signal_handler_sigint;
+       act.sa_flags = SA_NODEFER | SA_SIGINFO;
+       sigaction(SIGINT, &act, NULL);
+
 
 
        /* catch SIGQUIT for thread dump */
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+#if defined(ENABLE_THREADS)
 #if !defined(__FREEBSD__)
        act.sa_sigaction = signal_handler_sigquit;
        act.sa_flags = SA_SIGINFO;
@@ -117,23 +137,45 @@ void signal_init(void)
 
 *******************************************************************************/
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+#if defined(ENABLE_THREADS)
 void signal_handler_sigquit(int sig, siginfo_t *siginfo, void *_p)
 {
        /* do thread dump */
 
-       thread_dump();
+       threads_dump();
 }
 #endif
 
 
+/* signal_handler_sigint *******************************************************
+
+   Handler for SIGINT (<ctrl>-c) which shuts down CACAO properly with
+   Runtime.exit(I)V.
+
+*******************************************************************************/
+
+void signal_handler_sigint(int sig, siginfo_t *siginfo, void *_p)
+{
+       /* if we are already in Runtime.exit(), just do it hardcore */
+
+       if (vm_exiting) {
+               fprintf(stderr, "Caught SIGINT while already shutting down. Shutdown aborted...\n");
+               exit(0);
+       }
+
+       /* exit the vm properly */
+
+       vm_exit(0);
+}
+
+
 /* signal_handler_sigusr1 ******************************************************
 
    XXX
 
 *******************************************************************************/
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+#if defined(ENABLE_THREADS)
 void signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
 {
        /* call stacktrace function */