Merge pull request #2003 from esdrubal/seq_test_fix2
[mono.git] / mono / mini / mini-posix.c
index 2ee30e08dfad7d0a49370cf1c8dcf0bc9dc958d2..fe5cc782f833f26e4d965be3e4667654fc703f5d 100644 (file)
@@ -68,7 +68,7 @@
 
 #include "jit-icalls.h"
 
-#if defined(__native_client__)
+#if defined(__native_client__) || defined(HOST_WATCHOS)
 
 void
 mono_runtime_setup_stat_profiler (void)
@@ -89,9 +89,17 @@ MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
        return FALSE;
 }
 
+#ifndef PLATFORM_MACOSX
 void
 mono_runtime_install_handlers (void)
 {
+}
+#endif
+
+void
+mono_runtime_posix_install_handlers(void)
+{
+
 }
 
 void
@@ -104,6 +112,7 @@ mono_runtime_cleanup_handlers (void)
 {
 }
 
+#if !defined(PLATFORM_MACOSX)
 pid_t
 mono_runtime_syscall_fork (void)
 {
@@ -115,6 +124,7 @@ void
 mono_gdb_render_native_backtraces (pid_t crashed_pid)
 {
 }
+#endif
 
 #else
 
@@ -197,90 +207,18 @@ MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
 MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
 {
        MonoJitInfo *ji = NULL;
+       MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
        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_internal (mono_domain_get (), mono_arch_ip_from_context (ctx), TRUE, TRUE);
        if (!ji) {
         if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
                        return;
-               mono_handle_native_sigsegv (SIGABRT, ctx);
+               mono_handle_native_sigsegv (SIGABRT, ctx, info);
        }
 }
 
-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;
-       MONO_SIG_HANDLER_GET_CONTEXT;
-
-       if (!thread || !domain) {
-               /* The thread might not have started up yet */
-               /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
-               mono_debugger_agent_thread_interrupt (ctx, NULL);
-               return;
-       }
-
-       if (thread->ignore_next_signal) {
-               thread->ignore_next_signal = FALSE;
-               return;
-       }
-
-       if (thread->thread_dump_requested) {
-               thread->thread_dump_requested = FALSE;
-
-               mono_print_thread_dump (ctx);
-       }
-
-       /*
-        * This is an async signal, so the code below must not call anything which
-        * is not async safe. That includes the pthread locking functions. If we
-        * know that we interrupted managed code, then locking is safe.
-        */
-       /*
-        * On OpenBSD, ctx can be NULL if we are interrupting poll ().
-        */
-       if (ctx) {
-               ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
-               running_managed = ji != NULL;
-
-               if (mono_debugger_agent_thread_interrupt (ctx, ji))
-                       return;
-       } else {
-               running_managed = FALSE;
-       }
-
-       /* We can't do handler block checking from metadata since it requires doing
-        * a stack walk with context.
-        *
-        * FIXME add full-aot support.
-        */
-#ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
-       if (!mono_aot_only && ctx) {
-               MonoThreadUnwindState unwind_state;
-               if (mono_thread_state_init_from_sigctx (&unwind_state, ctx)) {
-                       if (mono_install_handler_block_guard (&unwind_state)) {
-#ifndef HOST_WIN32
-                               /*Clear current thread from been wapi interrupted otherwise things can go south*/
-                               wapi_clear_interruption ();
-#endif
-                               return;
-                       }
-               }
-       }
-#endif
-
-       exc = mono_thread_request_interruption (running_managed); 
-       if (!exc)
-               return;
-
-       mono_arch_handle_exception (ctx, exc);
-}
-
-
 #if defined(__i386__) || defined(__x86_64__)
 #define FULL_STAT_PROFILER_BACKTRACE 1
 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
@@ -297,7 +235,10 @@ MONO_SIG_HANDLER_FUNC (static, sigusr1_signal_handler)
 #endif
 
 #ifdef SIGPROF
-#if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
+
+static int profiling_signal_in_use;
+
+#if defined(__ia64__) || defined(__sparc__) || defined(sparc)
 
 MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
 {
@@ -309,39 +250,6 @@ MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
 
 #else
 
-static int
-get_stage2_signal_handler (void)
-{
-#if defined(PLATFORM_ANDROID)
-       // FIXME:
-       g_assert_not_reached ();
-       return -1;
-#elif !defined (SIGRTMIN)
-#ifdef SIGUSR2
-       return SIGUSR2;
-#else
-       return -1;
-#endif /* SIGUSR2 */
-#else
-       static int prof2_signum = -1;
-       int i;
-       if (prof2_signum != -1)
-               return prof2_signum;
-       /* we try to avoid SIGRTMIN and any one that might have been set already */
-       for (i = SIGRTMIN + 2; i < SIGRTMAX; ++i) {
-               struct sigaction sinfo;
-               sigaction (i, NULL, &sinfo);
-               if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
-                       prof2_signum = i;
-                       return i;
-               }
-       }
-       /* fallback to the old way */
-       return SIGRTMIN + 2;
-#endif
-}
-
-
 static void
 per_thread_profiler_hit (void *ctx)
 {
@@ -416,26 +324,6 @@ per_thread_profiler_hit (void *ctx)
        }
 }
 
-MONO_SIG_HANDLER_FUNC (static, sigprof_stage2_signal_handler)
-{
-       int old_errno = errno;
-       int hp_save_index;
-       MONO_SIG_HANDLER_GET_CONTEXT;
-
-       if (mono_thread_info_get_small_id () == -1)
-               return; //an non-attached thread got the signal
-
-       hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
-       mono_thread_info_set_is_async_context (TRUE);
-       per_thread_profiler_hit (ctx);
-       mono_thread_info_set_is_async_context (FALSE);
-
-       mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
-       errno = old_errno;
-
-       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
-}
-
 MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
 {
        MonoThreadInfo *info;
@@ -446,12 +334,21 @@ MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
        if (mono_thread_info_get_small_id () == -1)
                return; //an non-attached thread got the signal
 
+       if (!mono_domain_get () || !mono_native_tls_get_value (mono_jit_tls_id))
+               return; //thread in the process of dettaching
+
        hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
-       FOREACH_THREAD_SAFE (info) {
-               if (mono_thread_info_get_tid (info) == mono_native_thread_id_get ())
-                       continue;
-               mono_threads_pthread_kill (info, get_stage2_signal_handler ());
-       } END_FOREACH_THREAD_SAFE;
+
+       /* If we can't consume a profiling request it means we're the initiator. */
+       if (!(mono_threads_consume_async_jobs () & MONO_SERVICE_REQUEST_SAMPLE)) {
+               FOREACH_THREAD_SAFE (info) {
+                       if (mono_thread_info_get_tid (info) == mono_native_thread_id_get ())
+                               continue;
+
+                       mono_threads_add_async_job (info, MONO_SERVICE_REQUEST_SAMPLE);
+                       mono_threads_pthread_kill (info, profiling_signal_in_use);
+               } END_FOREACH_THREAD_SAFE;
+       }
 
        mono_thread_info_set_is_async_context (TRUE);
        per_thread_profiler_hit (ctx);
@@ -469,28 +366,13 @@ MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
 MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
 {
        gboolean res;
-       MONO_SIG_HANDLER_GET_CONTEXT;
 
        /* We use this signal to start the attach agent too */
        res = mono_attach_start ();
        if (res)
                return;
 
-       if (mono_thread_info_new_interrupt_enabled ()) {
-               mono_threads_request_thread_dump ();
-       } else {
-               printf ("Full thread dump:\n");
-
-               mono_threads_request_thread_dump ();
-
-               /*
-                * print_thread_dump () skips the current thread, since sending a signal
-                * to it would invoke the signal handler below the sigquit signal handler,
-                * and signal handlers don't create an lmf, so the stack walk could not
-                * be performed.
-                */
-               mono_print_thread_dump (ctx);
-       }
+       mono_threads_request_thread_dump ();
 
        mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
 }
@@ -517,7 +399,7 @@ add_signal_handler (int signo, gpointer handler)
 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
 
 /*Apple likes to deliver SIGBUS for *0 */
-#ifdef __APPLE__
+#ifdef PLATFORM_MACOSX
        if (signo == SIGSEGV || signo == SIGBUS) {
 #else
        if (signo == SIGSEGV) {
@@ -540,7 +422,6 @@ add_signal_handler (int signo, gpointer handler)
                sigset_t block_mask;
      
                sigemptyset (&block_mask);
-               sigaddset (&sa.sa_mask, mono_thread_get_abort_signal ());
        }
 #else
        sa.sa_handler = handler;
@@ -592,14 +473,11 @@ mono_runtime_posix_install_handlers (void)
        if (mono_jit_trace_calls != NULL)
                add_signal_handler (SIGUSR2, sigusr2_signal_handler);
 
-       if (!mono_thread_info_new_interrupt_enabled ())
-               add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
        /* it seems to have become a common bug for some programs that run as parents
         * of many processes to block signal delivery for real time signals.
         * We try to detect and work around their breakage here.
         */
        sigemptyset (&signal_set);
-       sigaddset (&signal_set, mono_thread_get_abort_signal ());
        if (mono_gc_get_suspend_signal () != -1)
                sigaddset (&signal_set, mono_gc_get_suspend_signal ());
        if (mono_gc_get_restart_signal () != -1)
@@ -636,8 +514,6 @@ mono_runtime_cleanup_handlers (void)
        if (mono_jit_trace_calls != NULL)
                remove_signal_handler (SIGUSR2);
 
-       remove_signal_handler (mono_thread_get_abort_signal ());
-
        remove_signal_handler (SIGABRT);
 
        remove_signal_handler (SIGSEGV);
@@ -725,7 +601,8 @@ mono_runtime_setup_stat_profiler (void)
                        perror ("open /dev/rtc");
                        return;
                }
-               add_signal_handler (SIGPROF, sigprof_signal_handler);
+               profiling_signal_in_use = SIGPROF;
+               add_signal_handler (profiling_signal_in_use, sigprof_signal_handler);
                if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
                        perror ("set rtc freq");
                        return;
@@ -755,17 +632,21 @@ mono_runtime_setup_stat_profiler (void)
        if (inited)
                return;
        inited = 1;
-       add_signal_handler (get_itimer_signal (), sigprof_signal_handler);
-       add_signal_handler (get_stage2_signal_handler (), sigprof_stage2_signal_handler);
+       profiling_signal_in_use = get_itimer_signal ();
+       add_signal_handler (profiling_signal_in_use, sigprof_signal_handler);
        setitimer (get_itimer_mode (), &itval, NULL);
 #endif
 }
 
-#if !defined(__APPLE__)
+#if !defined(PLATFORM_MACOSX)
 pid_t
 mono_runtime_syscall_fork ()
 {
-#if defined(SYS_fork)
+#if defined(PLATFORM_ANDROID)
+       /* SYS_fork is defined to be __NR_fork which is not defined in some ndk versions */
+       g_assert_not_reached ();
+       return 0;
+#elif defined(SYS_fork)
        return (pid_t) syscall (SYS_fork);
 #else
        g_assert_not_reached ();