[jit] Don't sample threads that are not fully attached.
[mono.git] / mono / mini / mini-posix.c
index 605a1ff088f80942d573dddded5c4ab02ae4dbd1..031cb6c0019744de9e8fb2a6c33cda05461780e9 100644 (file)
@@ -25,6 +25,8 @@
 #ifdef HAVE_SYS_SYSCALL_H
 #include <sys/syscall.h>
 #endif
+#include <errno.h>
+
 
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/loader.h>
@@ -54,6 +56,8 @@
 #include <mono/utils/mono-logger-internal.h>
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/dtrace.h>
+#include <mono/utils/mono-signal-handler.h>
+#include <mono/utils/mono-threads.h>
 
 #include "mini.h"
 #include <string.h>
@@ -80,7 +84,7 @@ mono_runtime_shutdown_stat_profiler (void)
 
 
 gboolean
-SIG_HANDLER_SIGNATURE (mono_chain_signal)
+MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
 {
        return FALSE;
 }
@@ -100,7 +104,17 @@ mono_runtime_cleanup_handlers (void)
 {
 }
 
+pid_t
+mono_runtime_syscall_fork (void)
+{
+       g_assert_not_reached();
+       return 0;
+}
 
+void
+mono_gdb_render_native_backtraces (pid_t crashed_pid)
+{
+}
 
 #else
 
@@ -162,19 +176,17 @@ 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;
-
        if (saved_handler && saved_handler->sa_handler) {
                if (!(saved_handler->sa_flags & SA_SIGINFO)) {
                        saved_handler->sa_handler (signal);
                } else {
 #ifdef MONO_ARCH_USE_SIGACTION
-                       saved_handler->sa_sigaction (signal, info, ctx);
+                       saved_handler->sa_sigaction (MONO_SIG_HANDLER_PARAMS);
 #endif /* MONO_ARCH_USE_SIGACTION */
                }
                return TRUE;
@@ -182,36 +194,35 @@ SIG_HANDLER_SIGNATURE (mono_chain_signal)
        return FALSE;
 }
 
-static void
-SIG_HANDLER_SIGNATURE (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);
        }
 }
 
-static void
-SIG_HANDLER_SIGNATURE (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)
+       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;
@@ -269,6 +280,7 @@ SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
        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))
@@ -284,12 +296,12 @@ SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
 #define FULL_STAT_PROFILER_BACKTRACE 0
 #endif
 
+#ifdef SIGPROF
 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
 
-static void
-SIG_HANDLER_SIGNATURE (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;
@@ -297,13 +309,14 @@ SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
 
 #else
 
+static int profiling_signal_in_use;
+
 static void
-SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
+per_thread_profiler_hit (void *ctx)
 {
        int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
        MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
-       GET_CONTEXT;
-       
+
        if (call_chain_depth == 0) {
                mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
        } else {
@@ -312,7 +325,7 @@ SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
                MonoContext mono_context;
                guchar *ips [call_chain_depth + 1];
 
-               mono_arch_sigctx_to_monoctx (ctx, &mono_context);
+               mono_sigctx_to_monoctx (ctx, &mono_context);
                ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
                
                if (jit_tls != NULL) {
@@ -370,18 +383,51 @@ SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
                
                mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
        }
+}
+
+MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
+{
+       MonoThreadInfo *info;
+       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
+
+       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 ();
+
+       /* 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);
+       mono_thread_info_set_is_async_context (FALSE);
+
+       mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
+       errno = old_errno;
 
-       mono_chain_signal (SIG_HANDLER_PARAMS);
+       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
 }
 
+#endif
 #endif
 
-static void
-SIG_HANDLER_SIGNATURE (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 ();
@@ -404,17 +450,16 @@ SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
                mono_print_thread_dump (ctx);
        }
 
-       mono_chain_signal (SIG_HANDLER_PARAMS);
+       mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
 }
 
-static void
-SIG_HANDLER_SIGNATURE (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
@@ -513,6 +558,11 @@ mono_runtime_posix_install_handlers (void)
         */
        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)
+               sigaddset (&signal_set, mono_gc_get_restart_signal ());
+       sigaddset (&signal_set, SIGCHLD);
        sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
 
        signal (SIGPIPE, SIG_IGN);
@@ -589,6 +639,30 @@ mono_runtime_shutdown_stat_profiler (void)
 #endif
 }
 
+#ifdef ITIMER_PROF
+static int
+get_itimer_mode (void)
+{
+       switch (mono_profiler_get_sampling_mode ()) {
+       case MONO_PROFILER_STAT_MODE_PROCESS: return ITIMER_PROF;
+       case MONO_PROFILER_STAT_MODE_REAL: return ITIMER_REAL;
+       }
+       g_assert_not_reached ();
+       return 0;
+}
+
+static int
+get_itimer_signal (void)
+{
+       switch (mono_profiler_get_sampling_mode ()) {
+       case MONO_PROFILER_STAT_MODE_PROCESS: return SIGPROF;
+       case MONO_PROFILER_STAT_MODE_REAL: return SIGALRM;
+       }
+       g_assert_not_reached ();
+       return 0;
+}
+#endif
+
 void
 mono_runtime_setup_stat_profiler (void)
 {
@@ -609,7 +683,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;
@@ -633,14 +708,15 @@ mono_runtime_setup_stat_profiler (void)
                return;
 #endif
 
-       itval.it_interval.tv_usec = 999;
+       itval.it_interval.tv_usec = (1000000 / mono_profiler_get_sampling_rate ()) - 1;
        itval.it_interval.tv_sec = 0;
        itval.it_value = itval.it_interval;
-       setitimer (ITIMER_PROF, &itval, NULL);
        if (inited)
                return;
        inited = 1;
-       add_signal_handler (SIGPROF, sigprof_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
 }
 
@@ -660,24 +736,54 @@ void
 mono_gdb_render_native_backtraces (pid_t crashed_pid)
 {
        const char *argv [9];
+       char template [] = "/tmp/mono-lldb-commands.XXXXXX";
        char buf1 [128];
+       FILE *commands;
+       gboolean using_lldb = FALSE;
 
        argv [0] = g_find_program_in_path ("gdb");
        if (argv [0] == NULL) {
-               return;
+               argv [0] = g_find_program_in_path ("lldb");
+               using_lldb = TRUE;
        }
 
-       argv [1] = "-ex";
-       sprintf (buf1, "attach %ld", (long) crashed_pid);
-       argv [2] = buf1;
-       argv [3] = "--ex";
-       argv [4] = "info threads";
-       argv [5] = "--ex";
-       argv [6] = "thread apply all bt";
-       argv [7] = "--batch";
-       argv [8] = 0;
+       if (argv [0] == NULL)
+               return;
+
+       if (using_lldb) {
+               if (mkstemp (template) == -1)
+                       return;
+
+               commands = fopen (template, "w");
+
+               fprintf (commands, "process attach --pid %ld\n", (long) crashed_pid);
+               fprintf (commands, "thread list\n");
+               fprintf (commands, "thread backtrace all\n");
+               fprintf (commands, "detach\n");
+               fprintf (commands, "quit\n");
+
+               fflush (commands);
+               fclose (commands);
+
+               argv [1] = "--source";
+               argv [2] = template;
+               argv [3] = 0;
+       } else {
+               argv [1] = "-ex";
+               sprintf (buf1, "attach %ld", (long) crashed_pid);
+               argv [2] = buf1;
+               argv [3] = "--ex";
+               argv [4] = "info threads";
+               argv [5] = "--ex";
+               argv [6] = "thread apply all bt";
+               argv [7] = "--batch";
+               argv [8] = 0;
+       }
 
        execv (argv [0], (char**)argv);
+
+       if (using_lldb)
+               unlink (template);
 }
 #endif
 #endif /* __native_client__ */
@@ -685,7 +791,7 @@ mono_gdb_render_native_backtraces (pid_t crashed_pid)
 #if !defined (__MACH__)
 
 gboolean
-mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoNativeThreadId thread_id, MonoNativeThreadHandle thread_handle)
+mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
 {
        g_error ("Posix systems don't support mono_thread_state_init_from_handle");
        return FALSE;