[profiler] Attach threads automatically in all callbacks.
authorAlex Rønne Petersen <alexrp@xamarin.com>
Mon, 19 Jun 2017 23:50:18 +0000 (01:50 +0200)
committerAlex Rønne Petersen <alexrp@xamarin.com>
Tue, 20 Jun 2017 03:38:51 +0000 (05:38 +0200)
This hardens the profiler against cases where an unattached thread causes some
event callback to be invoked. This is particularly relevant when embedding.

mono/profiler/log.c

index 7fbf71f6725b4b22bbd94c21a5dee1de4779a7f8..3808673dafa005d619f9ed4c4b3d2bf2edd45467 100644 (file)
@@ -558,17 +558,22 @@ init_time (void)
 }
 
 /*
- * These macros should be used when writing an event to a log buffer. They take
- * care of a bunch of stuff that can be repetitive and error-prone, such as
- * acquiring/releasing the buffer lock, incrementing the event counter,
- * expanding the log buffer, processing requests, etc. They also create a scope
- * so that it's harder to leak the LogBuffer pointer, which can be problematic
- * as the pointer is unstable when the buffer lock isn't acquired.
+ * These macros should be used when writing an event to a log buffer. They
+ * take care of a bunch of stuff that can be repetitive and error-prone, such
+ * as attaching the current thread, acquiring/releasing the buffer lock,
+ * incrementing the event counter, expanding the log buffer, etc. They also
+ * create a scope so that it's harder to leak the LogBuffer pointer, which can
+ * be problematic as the pointer is unstable when the buffer lock isn't
+ * acquired.
+ *
+ * If the calling thread is already attached, these macros will not alter its
+ * attach mode (i.e. whether it's added to the LLS). If the thread is not
+ * attached, init_thread () will be called with add_to_lls = TRUE.
  */
 
 #define ENTER_LOG(COUNTER, BUFFER, SIZE) \
        do { \
-               MonoProfilerThread *thread__ = PROF_TLS_GET (); \
+               MonoProfilerThread *thread__ = get_thread (); \
                if (thread__->attached) \
                        buffer_lock (); \
                g_assert (!thread__->busy && "Why are we trying to write a new event while already writing one?"); \
@@ -675,6 +680,8 @@ struct _BinaryObject {
        char *name;
 };
 
+static MonoProfiler *log_profiler;
+
 struct _MonoProfiler {
        FILE* file;
 #if defined (HAVE_SYS_ZLIB)
@@ -720,6 +727,8 @@ typedef struct {
        uint64_t time;
 } MethodInfo;
 
+// Do not use these TLS macros directly unless you know what you're doing.
+
 #ifdef HOST_WIN32
 
 #define PROF_TLS_SET(VAL) (TlsSetValue (profiler_tls, (VAL)))
@@ -862,6 +871,12 @@ deinit_thread (MonoProfilerThread *thread)
        PROF_TLS_SET (NULL);
 }
 
+static MonoProfilerThread *
+get_thread (void)
+{
+       return init_thread (log_profiler, TRUE);
+}
+
 // Only valid if init_thread () was called with add_to_lls = FALSE.
 static LogBuffer *
 ensure_logbuf_unsafe (MonoProfilerThread *thread, int bytes)
@@ -1014,7 +1029,7 @@ emit_method_inner (LogBuffer *logbuffer, void *method)
 static void
 register_method_local (MonoMethod *method, MonoJitInfo *ji)
 {
-       MonoProfilerThread *thread = PROF_TLS_GET ();
+       MonoProfilerThread *thread = get_thread ();
 
        if (!mono_conc_hashtable_lookup (thread->profiler->method_table, method)) {
                MethodInfo *info = (MethodInfo *) g_malloc (sizeof (MethodInfo));
@@ -1606,8 +1621,6 @@ emit_bt (MonoProfiler *prof, LogBuffer *logbuffer, FrameData *data)
 static void
 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
 {
-       init_thread (prof, TRUE);
-
        int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces) ? TYPE_ALLOC_BT : 0;
        FrameData data;
        uintptr_t len = mono_object_get_size (obj);
@@ -1984,7 +1997,7 @@ method_enter (MonoProfiler *prof, MonoMethod *method)
 {
        process_method_enter_coverage (prof, method);
 
-       if (!only_coverage && PROF_TLS_GET ()->call_depth++ <= max_call_depth) {
+       if (!only_coverage && get_thread ()->call_depth++ <= max_call_depth) {
                ENTER_LOG (&method_entries_ctr, logbuffer,
                        EVENT_SIZE /* event */ +
                        LEB128_SIZE /* method */
@@ -2000,7 +2013,7 @@ method_enter (MonoProfiler *prof, MonoMethod *method)
 static void
 method_leave (MonoProfiler *prof, MonoMethod *method)
 {
-       if (!only_coverage && --PROF_TLS_GET ()->call_depth <= max_call_depth) {
+       if (!only_coverage && --get_thread ()->call_depth <= max_call_depth) {
                ENTER_LOG (&method_exits_ctr, logbuffer,
                        EVENT_SIZE /* event */ +
                        LEB128_SIZE /* method */
@@ -2016,7 +2029,7 @@ method_leave (MonoProfiler *prof, MonoMethod *method)
 static void
 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
 {
-       if (!only_coverage && !nocalls && --PROF_TLS_GET ()->call_depth <= max_call_depth) {
+       if (!only_coverage && !nocalls && --get_thread ()->call_depth <= max_call_depth) {
                ENTER_LOG (&method_exception_exits_ctr, logbuffer,
                        EVENT_SIZE /* event */ +
                        LEB128_SIZE /* method */
@@ -2157,8 +2170,6 @@ monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEv
 static void
 thread_start (MonoProfiler *prof, uintptr_t tid)
 {
-       init_thread (prof, TRUE);
-
        if (ENABLED (PROFLOG_THREAD_EVENTS)) {
                ENTER_LOG (&thread_starts_ctr, logbuffer,
                        EVENT_SIZE /* event */ +
@@ -2191,7 +2202,7 @@ thread_end (MonoProfiler *prof, uintptr_t tid)
                EXIT_LOG_EXPLICIT (NO_SEND);
        }
 
-       MonoProfilerThread *thread = PROF_TLS_GET ();
+       MonoProfilerThread *thread = get_thread ();
 
        thread->ended = TRUE;
        remove_thread (thread);
@@ -4259,8 +4270,10 @@ handle_writer_queue_entry (MonoProfiler *prof)
                g_ptr_array_free (entry->methods, TRUE);
 
                if (wrote_methods) {
-                       dump_buffer_threadless (prof, PROF_TLS_GET ()->buffer);
-                       init_buffer_state (PROF_TLS_GET ());
+                       MonoProfilerThread *thread = PROF_TLS_GET ();
+
+                       dump_buffer_threadless (prof, thread->buffer);
+                       init_buffer_state (thread);
                }
 
        no_methods:
@@ -4503,16 +4516,16 @@ runtime_initialized (MonoProfiler *profiler)
        start_dumper_thread (profiler);
 }
 
-static MonoProfiler*
+static void
 create_profiler (const char *args, const char *filename, GPtrArray *filters)
 {
-       MonoProfiler *prof;
        char *nf;
        int force_delete = 0;
-       prof = (MonoProfiler *) g_calloc (1, sizeof (MonoProfiler));
 
-       prof->args = pstrdup (args);
-       prof->command_port = command_port;
+       log_profiler = (MonoProfiler *) g_calloc (1, sizeof (MonoProfiler));
+       log_profiler->args = pstrdup (args);
+       log_profiler->command_port = command_port;
+
        if (filename && *filename == '-') {
                force_delete = 1;
                filename++;
@@ -4541,24 +4554,24 @@ create_profiler (const char *args, const char *filename, GPtrArray *filters)
                }
        }
        if (*nf == '|') {
-               prof->file = popen (nf + 1, "w");
-               prof->pipe_output = 1;
+               log_profiler->file = popen (nf + 1, "w");
+               log_profiler->pipe_output = 1;
        } else if (*nf == '#') {
                int fd = strtol (nf + 1, NULL, 10);
-               prof->file = fdopen (fd, "a");
+               log_profiler->file = fdopen (fd, "a");
        } else {
                if (force_delete)
                        unlink (nf);
-               prof->file = fopen (nf, "wb");
+               log_profiler->file = fopen (nf, "wb");
        }
-       if (!prof->file) {
+       if (!log_profiler->file) {
                fprintf (stderr, "Cannot create profiler output: %s\n", nf);
                exit (1);
        }
 
 #if defined (HAVE_SYS_ZLIB)
        if (use_zip)
-               prof->gzfile = gzdopen (fileno (prof->file), "wb");
+               log_profiler->gzfile = gzdopen (fileno (log_profiler->file), "wb");
 #endif
 
        /*
@@ -4568,32 +4581,31 @@ create_profiler (const char *args, const char *filename, GPtrArray *filters)
        g_assert (SAMPLE_SLOT_SIZE (MAX_FRAMES) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (SAMPLE_BLOCK_SIZE));
 
        // FIXME: We should free this stuff too.
-       mono_lock_free_allocator_init_size_class (&prof->sample_size_class, SAMPLE_SLOT_SIZE (num_frames), SAMPLE_BLOCK_SIZE);
-       mono_lock_free_allocator_init_allocator (&prof->sample_allocator, &prof->sample_size_class, MONO_MEM_ACCOUNT_PROFILER);
+       mono_lock_free_allocator_init_size_class (&log_profiler->sample_size_class, SAMPLE_SLOT_SIZE (num_frames), SAMPLE_BLOCK_SIZE);
+       mono_lock_free_allocator_init_allocator (&log_profiler->sample_allocator, &log_profiler->sample_size_class, MONO_MEM_ACCOUNT_PROFILER);
 
-       mono_lock_free_queue_init (&prof->sample_reuse_queue);
+       mono_lock_free_queue_init (&log_profiler->sample_reuse_queue);
 
        g_assert (sizeof (WriterQueueEntry) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (WRITER_ENTRY_BLOCK_SIZE));
 
        // FIXME: We should free this stuff too.
-       mono_lock_free_allocator_init_size_class (&prof->writer_entry_size_class, sizeof (WriterQueueEntry), WRITER_ENTRY_BLOCK_SIZE);
-       mono_lock_free_allocator_init_allocator (&prof->writer_entry_allocator, &prof->writer_entry_size_class, MONO_MEM_ACCOUNT_PROFILER);
+       mono_lock_free_allocator_init_size_class (&log_profiler->writer_entry_size_class, sizeof (WriterQueueEntry), WRITER_ENTRY_BLOCK_SIZE);
+       mono_lock_free_allocator_init_allocator (&log_profiler->writer_entry_allocator, &log_profiler->writer_entry_size_class, MONO_MEM_ACCOUNT_PROFILER);
 
-       mono_lock_free_queue_init (&prof->writer_queue);
-       mono_os_sem_init (&prof->writer_queue_sem, 0);
+       mono_lock_free_queue_init (&log_profiler->writer_queue);
+       mono_os_sem_init (&log_profiler->writer_queue_sem, 0);
 
-       mono_lock_free_queue_init (&prof->dumper_queue);
-       mono_os_sem_init (&prof->dumper_queue_sem, 0);
+       mono_lock_free_queue_init (&log_profiler->dumper_queue);
+       mono_os_sem_init (&log_profiler->dumper_queue_sem, 0);
 
-       mono_os_mutex_init (&prof->method_table_mutex);
-       prof->method_table = mono_conc_hashtable_new (NULL, NULL);
+       mono_os_mutex_init (&log_profiler->method_table_mutex);
+       log_profiler->method_table = mono_conc_hashtable_new (NULL, NULL);
 
        if (do_coverage)
-               coverage_init (prof);
-       prof->coverage_filters = filters;
+               coverage_init (log_profiler);
+       log_profiler->coverage_filters = filters;
 
-       prof->startup_time = current_time ();
-       return prof;
+       log_profiler->startup_time = current_time ();
 }
 
 /*
@@ -4620,7 +4632,6 @@ void
 mono_profiler_startup (const char *desc)
 {
        GPtrArray *filters = NULL;
-       MonoProfiler *prof;
 
        proflog_parse_args (&config, desc [3] == ':' ? desc + 4 : "");
 
@@ -4657,21 +4668,15 @@ mono_profiler_startup (const char *desc)
 
        PROF_TLS_INIT ();
 
-       prof = create_profiler (desc, config.output_filename, filters);
-       if (!prof) {
-               PROF_TLS_FREE ();
-               return;
-       }
+       create_profiler (desc, config.output_filename, filters);
 
        mono_lls_init (&profiler_thread_list, NULL);
 
-       init_thread (prof, TRUE);
-
        //This two events are required for the profiler to work
        int events = MONO_PROFILE_THREADS | MONO_PROFILE_GC;
 
        //Required callbacks
-       mono_profiler_install (prof, log_shutdown);
+       mono_profiler_install (log_profiler, log_shutdown);
        mono_profiler_install_runtime_initialized (runtime_initialized);
 
        mono_profiler_install_gc (gc_event, gc_resize);