[runtime] Clean up the signal handling code by moving the signal handler signature...
authorZoltan Varga <vargaz@gmail.com>
Mon, 20 Oct 2014 22:07:13 +0000 (18:07 -0400)
committerZoltan Varga <vargaz@gmail.com>
Mon, 20 Oct 2014 22:07:24 +0000 (18:07 -0400)
mono/metadata/console-unix.c
mono/metadata/sgen-os-posix.c
mono/mini/mini-posix.c
mono/mini/mini.c
mono/mini/mini.h
mono/utils/mono-signal-handler.h

index 6a70b350085b9d7183412a63aaf9f9860fc28cf0..dffba190e31ed5d3113c5705f6e8dcf8d4b8f670 100644 (file)
@@ -277,7 +277,7 @@ mono_console_handle_async_ops (void)
 
 static gboolean in_sigint;
 
-MONO_SIGNAL_HANDLER_FUNC (static, sigint_handler, (int signo))
+MONO_SIG_HANDLER_FUNC (static, sigint_handler)
 {
        int save_errno;
        MONO_ARCH_SAVE_REGS;
@@ -295,7 +295,7 @@ MONO_SIGNAL_HANDLER_FUNC (static, sigint_handler, (int signo))
 
 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
 
-MONO_SIGNAL_HANDLER_FUNC (static, sigcont_handler, (int signo, void *the_siginfo, void *data))
+MONO_SIG_HANDLER_FUNC (static, sigcont_handler)
 {
        int unused;
        // Ignore error, there is not much we can do in the sigcont handler.
@@ -308,10 +308,10 @@ MONO_SIGNAL_HANDLER_FUNC (static, sigcont_handler, (int signo, void *the_siginfo
        if (save_sigcont.sa_sigaction != NULL &&
            save_sigcont.sa_sigaction != (void *)SIG_DFL &&
            save_sigcont.sa_sigaction != (void *)SIG_IGN)
-               (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
+               (*save_sigcont.sa_sigaction) (MONO_SIG_HANDLER_PARAMS);
 }
 
-MONO_SIGNAL_HANDLER_FUNC (static, sigwinch_handler, (int signo, void *the_siginfo, void *data))
+MONO_SIG_HANDLER_FUNC (static, sigwinch_handler)
 {
        int dims = terminal_get_dimensions ();
        if (dims != -1)
@@ -321,7 +321,7 @@ MONO_SIGNAL_HANDLER_FUNC (static, sigwinch_handler, (int signo, void *the_siginf
        if (save_sigwinch.sa_sigaction != NULL &&
            save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
            save_sigwinch.sa_sigaction != (void *)SIG_IGN)
-               (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
+               (*save_sigwinch.sa_sigaction) (MONO_SIG_HANDLER_PARAMS);
 }
 
 /*
index 9918d16adc58182836069bc29449f642c598c4e1..55acf5137c38a9b740e1f4f2eb025ea779a10c36 100644 (file)
@@ -135,7 +135,7 @@ suspend_thread (SgenThreadInfo *info, void *context)
 }
 
 /* LOCKING: assumes the GC lock is held (by the stopping thread) */
-MONO_SIGNAL_HANDLER_FUNC (static, suspend_handler, (int sig, siginfo_t *siginfo, void *context))
+MONO_SIG_HANDLER_FUNC (static, suspend_handler)
 {
        /*
         * The suspend signal handler potentially uses syscalls that
@@ -144,19 +144,19 @@ MONO_SIGNAL_HANDLER_FUNC (static, suspend_handler, (int sig, siginfo_t *siginfo,
         * must restore those to the values they had when we
         * interrupted.
         */
-
        SgenThreadInfo *info;
        int old_errno = errno;
        int hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        info = mono_thread_info_current ();
-       suspend_thread (info, context);
+       suspend_thread (info, ctx);
 
        mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
        errno = old_errno;
 }
 
-MONO_SIGNAL_HANDLER_FUNC (static, restart_handler, (int sig))
+MONO_SIG_HANDLER_FUNC (static, restart_handler)
 {
        SgenThreadInfo *info;
        int old_errno = errno;
index d26b4af4e92b4f23f34af747865109a140a43c2a..98b396bbf260d1320fd3a6c16ed346315049ba28 100644 (file)
@@ -81,7 +81,7 @@ mono_runtime_shutdown_stat_profiler (void)
 
 
 gboolean
-SIG_HANDLER_SIGNATURE (mono_chain_signal)
+MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
 {
        return FALSE;
 }
@@ -173,12 +173,11 @@ free_saved_signal_handlers (void)
  * was called, false otherwise.
  */
 gboolean
-SIG_HANDLER_SIGNATURE (mono_chain_signal)
+MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
 {
-       int signal = _dummy;
+       int signal = MONO_SIG_HANDLER_GET_SIGNO ();
        struct sigaction *saved_handler = get_saved_signal_handler (signal);
-
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        if (saved_handler && saved_handler->sa_handler) {
                if (!(saved_handler->sa_flags & SA_SIGINFO)) {
@@ -193,29 +192,28 @@ SIG_HANDLER_SIGNATURE (mono_chain_signal)
        return FALSE;
 }
 
-SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
+MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
 {
        MonoJitInfo *ji = NULL;
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        if (mono_thread_internal_current ())
-               ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
+               ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
        if (!ji) {
-        if (mono_chain_signal (SIG_HANDLER_PARAMS))
+        if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
                        return;
                mono_handle_native_sigsegv (SIGABRT, ctx);
        }
 }
 
-SIG_HANDLER_FUNC (static, sigusr1_signal_handler)
+MONO_SIG_HANDLER_FUNC (static, sigusr1_signal_handler)
 {
        gboolean running_managed;
        MonoException *exc;
        MonoInternalThread *thread = mono_thread_internal_current ();
        MonoDomain *domain = mono_domain_get ();
        void *ji;
-       
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        if (!thread || !domain) {
                /* The thread might not have started up yet */
@@ -299,9 +297,9 @@ SIG_HANDLER_FUNC (static, sigusr1_signal_handler)
 #ifdef SIGPROF
 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
 
-SIG_HANDLER_FUNC (static, sigprof_signal_handler)
+MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
 {
-       if (mono_chain_signal (SIG_HANDLER_PARAMS))
+       if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
                return;
 
        NOT_IMPLEMENTED;
@@ -309,11 +307,11 @@ SIG_HANDLER_FUNC (static, sigprof_signal_handler)
 
 #else
 
-SIG_HANDLER_FUNC (static, sigprof_signal_handler)
+MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
 {
        int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
        MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
        
        if (call_chain_depth == 0) {
                mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
@@ -382,17 +380,16 @@ SIG_HANDLER_FUNC (static, sigprof_signal_handler)
                mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
        }
 
-       mono_chain_signal (SIG_HANDLER_PARAMS);
+       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
 }
 
 #endif
 #endif
 
-SIG_HANDLER_FUNC (static, sigquit_signal_handler)
+MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
 {
        gboolean res;
-
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        /* We use this signal to start the attach agent too */
        res = mono_attach_start ();
@@ -415,16 +412,16 @@ SIG_HANDLER_FUNC (static, sigquit_signal_handler)
                mono_print_thread_dump (ctx);
        }
 
-       mono_chain_signal (SIG_HANDLER_PARAMS);
+       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
 }
 
-SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
+MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
 {
        gboolean enabled = mono_trace_is_enabled ();
 
        mono_trace_enable (!enabled);
 
-       mono_chain_signal (SIG_HANDLER_PARAMS);
+       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
 }
 
 static void
index 1ab0ac22f2047393c5087998fd3cb0ae948fc6e6..971ac30d295d80eb0c7f270c920ebaa69455042a 100755 (executable)
@@ -6724,14 +6724,11 @@ mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObjec
        return runtime_invoke (obj, params, exc, info->compiled_method);
 }
 
-SIG_HANDLER_FUNC (, mono_sigfpe_signal_handler)
+MONO_SIG_HANDLER_FUNC (, mono_sigfpe_signal_handler)
 {
        MonoException *exc = NULL;
        MonoJitInfo *ji;
-#if !(defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32))
-       void *info = NULL;
-#endif
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
 
@@ -6749,12 +6746,12 @@ SIG_HANDLER_FUNC (, mono_sigfpe_signal_handler)
 #endif
 
        if (!ji) {
-               if (!mono_do_crash_chaining && mono_chain_signal (SIG_HANDLER_PARAMS))
+               if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
                        return;
 
                mono_handle_native_sigsegv (SIGSEGV, ctx);
                if (mono_do_crash_chaining) {
-                       mono_chain_signal (SIG_HANDLER_PARAMS);
+                       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
                        return;
                }
        }
@@ -6762,10 +6759,10 @@ SIG_HANDLER_FUNC (, mono_sigfpe_signal_handler)
        mono_arch_handle_exception (ctx, exc);
 }
 
-SIG_HANDLER_FUNC (, mono_sigill_signal_handler)
+MONO_SIG_HANDLER_FUNC (, mono_sigill_signal_handler)
 {
        MonoException *exc;
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        exc = mono_get_exception_execution_engine ("SIGILL");
        
@@ -6776,13 +6773,12 @@ SIG_HANDLER_FUNC (, mono_sigill_signal_handler)
 #define HAVE_SIG_INFO
 #endif
 
-SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
+MONO_SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
 {
        MonoJitInfo *ji;
        MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        gpointer fault_addr = NULL;
-
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
        if (mono_arch_is_single_step_event (info, ctx)) {
@@ -6804,11 +6800,11 @@ SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
 
        /* The thread might no be registered with the runtime */
        if (!mono_domain_get () || !jit_tls) {
-               if (!mono_do_crash_chaining && mono_chain_signal (SIG_HANDLER_PARAMS))
+               if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
                        return;
                mono_handle_native_sigsegv (SIGSEGV, ctx);
                if (mono_do_crash_chaining) {
-                       mono_chain_signal (SIG_HANDLER_PARAMS);
+                       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
                        return;
                }
        }
@@ -6841,7 +6837,7 @@ SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
                g_assert_not_reached ();
        } else {
                /* The original handler might not like that it is executed on an altstack... */
-               if (!ji && mono_chain_signal (SIG_HANDLER_PARAMS))
+               if (!ji && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
                        return;
 
                mono_arch_handle_altstack_exception (ctx, info->si_addr, FALSE);
@@ -6849,13 +6845,13 @@ SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
 #else
 
        if (!ji) {
-               if (!mono_do_crash_chaining && mono_chain_signal (SIG_HANDLER_PARAMS))
+               if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
                        return;
 
                mono_handle_native_sigsegv (SIGSEGV, ctx);
 
                if (mono_do_crash_chaining) {
-                       mono_chain_signal (SIG_HANDLER_PARAMS);
+                       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
                        return;
                }
        }
@@ -6864,10 +6860,10 @@ SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
 #endif
 }
 
-SIG_HANDLER_FUNC (, mono_sigint_signal_handler)
+MONO_SIG_HANDLER_FUNC (, mono_sigint_signal_handler)
 {
        MonoException *exc;
-       GET_CONTEXT;
+       MONO_SIG_HANDLER_GET_CONTEXT;
 
        exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
        
index cd38442570cf84b53cd81d559bebf33afeec7dd9..df6c54a7a5262a16b0b52abca03d799e9d1bb4a5 100755 (executable)
@@ -31,6 +31,7 @@
 #include <mono/utils/mono-tls.h>
 #include <mono/utils/atomic.h>
 #include <mono/utils/mono-conc-hashtable.h>
+#include <mono/utils/mono-signal-handler.h>
 
 #define MONO_BREAKPOINT_ARRAY_SIZE 64
 
@@ -2826,53 +2827,12 @@ void mono_cross_helpers_run (void) MONO_INTERNAL;
 /*
  * Signal handling
  */
-#ifdef MONO_GET_CONTEXT
-#define GET_CONTEXT MONO_GET_CONTEXT
-#endif
-
-#ifndef GET_CONTEXT
-#ifdef HOST_WIN32
-/* seh_vectored_exception_handler () passes in a CONTEXT* */
-#define GET_CONTEXT \
-    void *ctx = context;
-#else
-#ifdef MONO_ARCH_USE_SIGACTION
-#define GET_CONTEXT \
-    void *ctx = context;
-#elif defined(__HAIKU__)
-#define GET_CONTEXT \
-       void *ctx = &regs;
-#else
-#define GET_CONTEXT \
-       void **_p = (void **)&_dummy; \
-       struct sigcontext *ctx = (struct sigcontext *)++_p;
-#endif
-#endif
-#endif
-
-#if defined(MONO_ARCH_USE_SIGACTION) && !defined(HOST_WIN32)
-#define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
-#define SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy, siginfo_t *info, void *context))
-#define SIG_HANDLER_PARAMS _dummy, info, context
-#elif defined(HOST_WIN32)
-#define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, EXCEPTION_POINTERS *info, void *context)
-#define SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy, EXCEPTION_POINTERS *info, void *context))
-#define SIG_HANDLER_PARAMS _dummy, info, context
-#elif defined(__HAIKU__)
-#define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, void *userData, vregs regs)
-#define SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy, void *userData, vregs regs))
-#define SIG_HANDLER_PARAMS _dummy, userData, regs
-#else
-#define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
-#define SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy))
-#define SIG_HANDLER_PARAMS _dummy
-#endif
 
-void SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler)  MONO_INTERNAL;
-void SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler)  MONO_INTERNAL;
-void SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler) MONO_INTERNAL;
-void SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler)  MONO_INTERNAL;
-gboolean SIG_HANDLER_SIGNATURE (mono_chain_signal) MONO_INTERNAL;
+void MONO_SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler)  MONO_INTERNAL;
+void MONO_SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler)  MONO_INTERNAL;
+void MONO_SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler) MONO_INTERNAL;
+void MONO_SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler)  MONO_INTERNAL;
+gboolean MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal) MONO_INTERNAL;
 
 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
 #define ARCH_HAVE_DELEGATE_TRAMPOLINES 1
index 3a01146d5e179d7a9c9a35b4aabd1b4f8023445b..92cb299a15023ced10adeef5e568f6fa7effcc6d 100644 (file)
 #ifdef ENABLE_EXTENSION_MODULE
 #include "../../../mono-extensions/mono/utils/mono-signal-handler.h"
 #endif
-/*
-Not all platforms support signal handlers in the same way. Some have the same concept but
-for some weird reason pass different kind of arguments.
-
-All signal handler helpers should go here so they can be properly shared across the JIT,
-utils and sgen.
-
-TODO: Cleanup & move mini's macros to here so they can leveraged by other parts.
 
-*/
+/* Don't use this */
 #ifndef MONO_SIGNAL_HANDLER_FUNC
 #define MONO_SIGNAL_HANDLER_FUNC(access, name, arglist) access void name arglist
 #endif
 
+/*
+ * Macros to work around signal handler differences on various platforms.
+ *
+ * To declare a signal handler function:
+ * void MONO_SIG_HANDLER_SIGNATURE (handler_func)
+ * To define a signal handler function:
+ * MONO_SIG_HANDLER_FUNC(access, name)
+ * To call another signal handler function:
+ * handler_func (SIG_HANDLER_PARAMS);
+ * To obtain the signal number:
+ * int signo = MONO_SIG_HANDLER_GET_SIGNO ();
+ * To obtain the signal context:
+ * MONO_SIG_HANDLER_GET_CONTEXT ().
+ * This will define a variable name 'ctx'.
+ */
+
+#ifdef HOST_WIN32
+#define MONO_SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, EXCEPTION_POINTERS *info, void *context)
+#define MONO_SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy, EXCEPTION_POINTERS *info, void *context))
+#define MONO_SIG_HANDLER_PARAMS _dummy, info, context
+#define MONO_SIG_HANDLER_GET_SIGNO() (_dummy)
+/* seh_vectored_exception_handler () passes in a CONTEXT* */
+#define MONO_SIG_HANDLER_GET_CONTEXT \
+    void *ctx = context;
+#else
+/* sigaction */
+#define MONO_SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
+#define MONO_SIG_HANDLER_FUNC(access, ftn) MONO_SIGNAL_HANDLER_FUNC (access, ftn, (int _dummy, siginfo_t *info, void *context))
+#define MONO_SIG_HANDLER_PARAMS _dummy, info, context
+#define MONO_SIG_HANDLER_GET_SIGNO() (_dummy)
+#define MONO_SIG_HANDLER_GET_CONTEXT \
+    void *ctx = context;
+#endif
+
 #endif