First set of licensing changes
[mono.git] / mono / profiler / proflog.c
index c8adbd4a7de273d73fbf145ceda9b5561b848137..ed46b6d4cc9b1b5fe9dba94b1980c8cc8f02e09b 100644 (file)
@@ -7,6 +7,7 @@
  *
  * Copyright 2010 Novell, Inc (http://www.novell.com)
  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <config.h>
@@ -23,7 +24,7 @@
 #include <mono/utils/atomic.h>
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-counters.h>
-#include <mono/utils/mono-mutex.h>
+#include <mono/utils/mono-os-mutex.h>
 #include <mono/utils/mono-conc-hashtable.h>
 #include <mono/utils/lock-free-queue.h>
 #include <stdlib.h>
@@ -33,6 +34,9 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_SCHED_GETAFFINITY
+#include <sched.h>
+#endif
 #include <fcntl.h>
 #include <errno.h>
 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
 
 #include <unistd.h>
 #include <sys/syscall.h>
-#include "perf_event.h"
 
 #ifdef ENABLE_PERF_EVENTS
+#include <linux/perf_event.h>
+
 #define USE_PERF_EVENTS 1
 
 static int read_perf_mmap (MonoProfiler* prof, int cpu);
@@ -522,7 +527,7 @@ static char*
 pstrdup (const char *s)
 {
        int len = strlen (s) + 1;
-       char *p = malloc (len);
+       char *p = (char *)malloc (len);
        memcpy (p, s, len);
        return p;
 }
@@ -530,7 +535,7 @@ pstrdup (const char *s)
 static StatBuffer*
 create_stat_buffer (void)
 {
-       StatBuffer* buf = alloc_buffer (BUFFER_SIZE);
+       StatBuffer* buf = (StatBuffer *)alloc_buffer (BUFFER_SIZE);
        buf->size = BUFFER_SIZE;
        buf->data_end = (uintptr_t*)((unsigned char*)buf + buf->size);
        buf->data = buf->buf;
@@ -540,7 +545,7 @@ create_stat_buffer (void)
 static LogBuffer*
 create_buffer (void)
 {
-       LogBuffer* buf = alloc_buffer (BUFFER_SIZE);
+       LogBuffer* buf = (LogBuffer *)alloc_buffer (BUFFER_SIZE);
        buf->size = BUFFER_SIZE;
        buf->time_base = current_time ();
        buf->last_time = buf->time_base;
@@ -571,29 +576,29 @@ ensure_logbuf_inner (LogBuffer *old, int bytes)
        if (old && old->data + bytes + 100 < old->data_end)
                return old;
 
-       LogBuffer *new = create_buffer ();
-       new->thread_id = thread_id ();
-       new->next = old;
+       LogBuffer *new_ = (LogBuffer *)create_buffer ();
+       new_->thread_id = thread_id ();
+       new_->next = old;
 
        if (old)
-               new->call_depth = old->call_depth;
+               new_->call_depth = old->call_depth;
 
-       return new;
+       return new_;
 }
 
 static LogBuffer*
 ensure_logbuf (int bytes)
 {
        LogBuffer *old = TLS_GET (LogBuffer, tlsbuffer);
-       LogBuffer *new = ensure_logbuf_inner (old, bytes);
+       LogBuffer *new_ = ensure_logbuf_inner (old, bytes);
 
-       if (new == old)
+       if (new_ == old)
                return old; // Still enough space.
 
-       TLS_SET (tlsbuffer, new);
+       TLS_SET (tlsbuffer, new_);
        init_thread ();
 
-       return new;
+       return new_;
 }
 
 static void
@@ -615,8 +620,8 @@ static void
 emit_time (LogBuffer *logbuffer, uint64_t value)
 {
        uint64_t tdiff = value - logbuffer->last_time;
-       if (value < logbuffer->last_time)
-               printf ("time went backwards\n");
+       //if (value < logbuffer->last_time)
+       //      printf ("time went backwards\n");
        //if (tdiff > 1000000)
        //      printf ("large time offset: %llu\n", tdiff);
        encode_uleb128 (tdiff, logbuffer->data, &logbuffer->data);
@@ -661,6 +666,7 @@ emit_method_inner (LogBuffer *logbuffer, void *method)
        assert (logbuffer->data <= logbuffer->data_end);
 }
 
+/*
 typedef struct {
        MonoMethod *method;
        MonoJitInfo *found;
@@ -688,11 +694,20 @@ find_method (MonoDomain *domain, void *user_data)
        if (ji)
                search->found = ji;
 }
+*/
 
 static void
 register_method_local (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji)
 {
        if (!mono_conc_hashtable_lookup (prof->method_table, method)) {
+               /*
+                * FIXME: In some cases, we crash while looking up JIT info for AOT'd methods.
+                * This usually happens for static constructors. This code is disabled for now
+                * as we don't need this info for anything critical.
+                *
+                * https://bugzilla.xamarin.com/show_bug.cgi?id=35171
+                */
+               /*
                if (!ji) {
                        MethodSearch search = { method, NULL };
 
@@ -700,6 +715,7 @@ register_method_local (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji)
 
                        ji = search.found;
                }
+               */
 
                /*
                 * FIXME: We can't always find JIT info for a generic shared method, especially
@@ -709,7 +725,7 @@ register_method_local (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji)
                 */
                //g_assert (ji);
 
-               MethodInfo *info = malloc (sizeof (MethodInfo));
+               MethodInfo *info = (MethodInfo *)malloc (sizeof (MethodInfo));
 
                info->method = method;
                info->ji = ji;
@@ -834,7 +850,7 @@ dump_header (MonoProfiler *profiler)
 static void
 send_buffer (MonoProfiler *prof, GPtrArray *methods, LogBuffer *buffer)
 {
-       WriterQueueEntry *entry = calloc (1, sizeof (WriterQueueEntry));
+       WriterQueueEntry *entry = (WriterQueueEntry *)calloc (1, sizeof (WriterQueueEntry));
        mono_lock_free_queue_node_init (&entry->node, FALSE);
        entry->methods = methods;
        entry->buffer = buffer;
@@ -1045,7 +1061,7 @@ static int num_frames = MAX_FRAMES;
 static mono_bool
 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
 {
-       FrameData *frame = data;
+       FrameData *frame = (FrameData *)data;
        if (method && frame->count < num_frames) {
                frame->il_offsets [frame->count] = il_offset;
                frame->native_offsets [frame->count] = native_offset;
@@ -1260,7 +1276,7 @@ type_name (MonoClass *klass)
        char buf [1024];
        char *p;
        push_nesting (buf, klass);
-       p = malloc (strlen (buf) + 1);
+       p = (char *)malloc (strlen (buf) + 1);
        strcpy (p, buf);
        return p;
 }
@@ -1584,7 +1600,7 @@ code_buffer_new (MonoProfiler *prof, void *buffer, int size, MonoProfilerCodeBuf
        char *name;
        LogBuffer *logbuffer;
        if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
-               name = data;
+               name = (char *)data;
                nlen = strlen (name) + 1;
        } else {
                name = NULL;
@@ -1948,7 +1964,7 @@ typedef struct {
 static mono_bool
 async_walk_stack (MonoMethod *method, MonoDomain *domain, void *base_address, int offset, void *data)
 {
-       AsyncFrameData *frame = data;
+       AsyncFrameData *frame = (AsyncFrameData *)data;
        if (frame->count < num_frames) {
                frame->data [frame->count].method = method;
                frame->data [frame->count].domain = domain;
@@ -2006,7 +2022,7 @@ mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
                do {
                        oldsb = profiler->stat_buffers;
                        sbuf->next = oldsb;
-                       foundsb = InterlockedCompareExchangePointer ((void * volatile*)&profiler->stat_buffers, sbuf, oldsb);
+                       foundsb = (StatBuffer *)InterlockedCompareExchangePointer ((void * volatile*)&profiler->stat_buffers, sbuf, oldsb);
                } while (foundsb != oldsb);
                if (do_debug)
                        ign_res (write (2, "overflow\n", 9));
@@ -2021,7 +2037,7 @@ mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
        do {
                old_data = sbuf->data;
                new_data = old_data + SAMPLE_EVENT_SIZE_IN_SLOTS (bt_data.count);
-               data = InterlockedCompareExchangePointer ((void * volatile*)&sbuf->data, new_data, old_data);
+               data = (uintptr_t *)InterlockedCompareExchangePointer ((void * volatile*)&sbuf->data, new_data, old_data);
        } while (data != old_data);
        if (old_data >= sbuf->data_end)
                return; /* lost event */
@@ -2078,7 +2094,7 @@ add_code_pointer (uintptr_t ip)
                size_code_pages *= 2;
                if (size_code_pages == 0)
                        size_code_pages = 16;
-               n = calloc (sizeof (uintptr_t) * size_code_pages, 1);
+               n = (uintptr_t *)calloc (sizeof (uintptr_t) * size_code_pages, 1);
                for (i = 0; i < old_size; ++i) {
                        if (code_pages [i])
                                add_code_page (n, size_code_pages, code_pages [i]);
@@ -2414,7 +2430,7 @@ dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf)
        g_ptr_array_sort (prof->sorted_sample_events, compare_sample_events);
 
        for (guint sidx = 0; sidx < prof->sorted_sample_events->len; sidx++) {
-               uintptr_t *sample = g_ptr_array_index (prof->sorted_sample_events, sidx);
+               uintptr_t *sample = (uintptr_t *)g_ptr_array_index (prof->sorted_sample_events, sidx);
                int count = sample [0] & 0xff;
                int mbt_count = (sample [0] & 0xff00) >> 8;
                int type = sample [0] >> 16;
@@ -2427,7 +2443,7 @@ dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf)
                        void *address = (void*)managed_sample_base [i * 4 + 2];
 
                        if (!method) {
-                               MonoJitInfo *ji = mono_jit_info_table_find (domain, address);
+                               MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *)address);
 
                                if (ji)
                                        managed_sample_base [i * 4 + 0] = (uintptr_t)mono_jit_info_get_method (ji);
@@ -2481,12 +2497,12 @@ dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf)
 static int
 mono_cpu_count (void)
 {
-       int count = 0;
 #ifdef PLATFORM_ANDROID
        /* Android tries really hard to save power by powering off CPUs on SMP phones which
         * means the normal way to query cpu count returns a wrong value with userspace API.
         * Instead we use /sys entries to query the actual hardware CPU count.
         */
+       int count = 0;
        char buffer[8] = {'\0'};
        int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
        /* Format of the /sys entry is a cpulist of indexes which in the case
@@ -2501,13 +2517,42 @@ mono_cpu_count (void)
        if (count > 0)
                return count + 1;
 #endif
+
+#if defined(HOST_ARM) || defined (HOST_ARM64)
+
+       /* ARM platforms tries really hard to save power by powering off CPUs on SMP phones which
+        * means the normal way to query cpu count returns a wrong value with userspace API. */
+
+#ifdef _SC_NPROCESSORS_CONF
+       {
+               int count = sysconf (_SC_NPROCESSORS_CONF);
+               if (count > 0)
+                       return count;
+       }
+#endif
+
+#else
+
+#ifdef HAVE_SCHED_GETAFFINITY
+       {
+               cpu_set_t set;
+               if (sched_getaffinity (getpid (), sizeof (set), &set) == 0)
+                       return CPU_COUNT (&set);
+       }
+#endif
 #ifdef _SC_NPROCESSORS_ONLN
-       count = sysconf (_SC_NPROCESSORS_ONLN);
-       if (count > 0)
-               return count;
+       {
+               int count = sysconf (_SC_NPROCESSORS_ONLN);
+               if (count > 0)
+                       return count;
+       }
 #endif
+
+#endif /* defined(HOST_ARM) || defined (HOST_ARM64) */
+
 #ifdef USE_SYSCTL
        {
+               int count;
                int mib [2];
                size_t len = sizeof (int);
                mib [0] = CTL_HW;
@@ -2770,7 +2815,7 @@ counters_add_agent (MonoCounter *counter)
        if (!counters_initialized)
                return;
 
-       mono_mutex_lock (&counters_mutex);
+       mono_os_mutex_lock (&counters_mutex);
 
        for (agent = counters; agent; agent = agent->next) {
                if (agent->counter == counter) {
@@ -2779,12 +2824,12 @@ counters_add_agent (MonoCounter *counter)
                                free (agent->value);
                                agent->value = NULL;
                        }
-                       mono_mutex_unlock (&counters_mutex);
+                       mono_os_mutex_unlock (&counters_mutex);
                        return;
                }
        }
 
-       agent = malloc (sizeof (MonoCounterAgent));
+       agent = (MonoCounterAgent *)malloc (sizeof (MonoCounterAgent));
        agent->counter = counter;
        agent->value = NULL;
        agent->value_size = 0;
@@ -2801,7 +2846,7 @@ counters_add_agent (MonoCounter *counter)
                item->next = agent;
        }
 
-       mono_mutex_unlock (&counters_mutex);
+       mono_os_mutex_unlock (&counters_mutex);
 }
 
 static mono_bool
@@ -2816,7 +2861,7 @@ counters_init (MonoProfiler *profiler)
 {
        assert (!counters_initialized);
 
-       mono_mutex_init (&counters_mutex);
+       mono_os_mutex_init (&counters_mutex);
 
        counters_initialized = TRUE;
 
@@ -2838,7 +2883,7 @@ counters_emit (MonoProfiler *profiler)
        if (!counters_initialized)
                return;
 
-       mono_mutex_lock (&counters_mutex);
+       mono_os_mutex_lock (&counters_mutex);
 
        for (agent = counters; agent; agent = agent->next) {
                if (agent->emitted)
@@ -2857,7 +2902,7 @@ counters_emit (MonoProfiler *profiler)
        }
 
        if (!len) {
-               mono_mutex_unlock (&counters_mutex);
+               mono_os_mutex_unlock (&counters_mutex);
                return;
        }
 
@@ -2886,7 +2931,7 @@ counters_emit (MonoProfiler *profiler)
 
        safe_send (profiler, logbuffer);
 
-       mono_mutex_unlock (&counters_mutex);
+       mono_os_mutex_unlock (&counters_mutex);
 }
 
 static void
@@ -2908,7 +2953,7 @@ counters_sample (MonoProfiler *profiler, uint64_t timestamp)
        buffer_size = 8;
        buffer = calloc (1, buffer_size);
 
-       mono_mutex_lock (&counters_mutex);
+       mono_os_mutex_lock (&counters_mutex);
 
        size =
                EVENT_SIZE /* event */ +
@@ -3017,7 +3062,7 @@ counters_sample (MonoProfiler *profiler, uint64_t timestamp)
 
        safe_send (profiler, logbuffer);
 
-       mono_mutex_unlock (&counters_mutex);
+       mono_os_mutex_unlock (&counters_mutex);
 }
 
 typedef struct _PerfCounterAgent PerfCounterAgent;
@@ -3133,7 +3178,7 @@ perfcounters_sample (MonoProfiler *profiler, uint64_t timestamp)
        if (!counters_initialized)
                return;
 
-       mono_mutex_lock (&counters_mutex);
+       mono_os_mutex_lock (&counters_mutex);
 
        /* mark all perfcounters as deleted, foreach will unmark them as necessary */
        for (pcagent = perfcounters; pcagent; pcagent = pcagent->next)
@@ -3183,7 +3228,7 @@ perfcounters_sample (MonoProfiler *profiler, uint64_t timestamp)
 
        safe_send (profiler, logbuffer);
 
-       mono_mutex_unlock (&counters_mutex);
+       mono_os_mutex_unlock (&counters_mutex);
 }
 
 static void
@@ -3264,7 +3309,7 @@ parse_generic_type_names(char *name)
        if (name == NULL || *name == '\0')
                return g_strdup ("");
 
-       if (!(ret = new_name = calloc (strlen (name) * 4 + 1, sizeof (char))))
+       if (!(ret = new_name = (char *)calloc (strlen (name) * 4 + 1, sizeof (char))))
                return NULL;
 
        do {
@@ -3329,7 +3374,7 @@ build_method_buffer (gpointer key, gpointer value, gpointer userdata)
        method_name = mono_method_get_name (method);
 
        if (coverage_data->len != 0) {
-               CoverageEntry *entry = coverage_data->pdata[0];
+               CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[0];
                first_filename = entry->filename ? entry->filename : "";
        } else
                first_filename = "";
@@ -3366,7 +3411,7 @@ build_method_buffer (gpointer key, gpointer value, gpointer userdata)
        safe_send (prof, logbuffer);
 
        for (i = 0; i < coverage_data->len; i++) {
-               CoverageEntry *entry = coverage_data->pdata[i];
+               CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[i];
 
                logbuffer = ensure_logbuf (
                        EVENT_SIZE /* event */ +
@@ -3462,7 +3507,7 @@ build_class_buffer (gpointer key, gpointer value, gpointer userdata)
 static void
 get_coverage_for_image (MonoImage *image, int *number_of_methods, guint *fully_covered, int *partially_covered)
 {
-       MonoLockFreeQueue *image_methods = mono_conc_hashtable_lookup (image_to_methods, image);
+       MonoLockFreeQueue *image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
 
        *number_of_methods = mono_image_get_table_rows (image, MONO_TABLE_METHOD);
        if (image_methods)
@@ -3527,11 +3572,11 @@ dump_coverage (MonoProfiler *prof)
        COVERAGE_DEBUG(fprintf (stderr, "Coverage: Started dump\n");)
        method_id = 0;
 
-       mono_mutex_lock (&coverage_mutex);
+       mono_os_mutex_lock (&coverage_mutex);
        mono_conc_hashtable_foreach (coverage_assemblies, build_assembly_buffer, prof);
        mono_conc_hashtable_foreach (coverage_classes, build_class_buffer, prof);
        mono_conc_hashtable_foreach (coverage_methods, build_method_buffer, prof);
-       mono_mutex_unlock (&coverage_mutex);
+       mono_os_mutex_unlock (&coverage_mutex);
 
        COVERAGE_DEBUG(fprintf (stderr, "Coverage: Finished dump\n");)
 }
@@ -3551,15 +3596,15 @@ process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method)
        if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)))
                return;
 
-       mono_mutex_lock (&coverage_mutex);
+       mono_os_mutex_lock (&coverage_mutex);
        mono_conc_hashtable_insert (entered_methods, method, method);
-       mono_mutex_unlock (&coverage_mutex);
+       mono_os_mutex_unlock (&coverage_mutex);
 }
 
 static MonoLockFreeQueueNode *
 create_method_node (MonoMethod *method)
 {
-       MethodNode *node = g_malloc (sizeof (MethodNode));
+       MethodNode *node = (MethodNode *)g_malloc (sizeof (MethodNode));
        mono_lock_free_queue_node_init ((MonoLockFreeQueueNode *) node, FALSE);
        node->method = method;
 
@@ -3569,6 +3614,7 @@ create_method_node (MonoMethod *method)
 static gboolean
 coverage_filter (MonoProfiler *prof, MonoMethod *method)
 {
+       MonoError error;
        MonoClass *klass;
        MonoImage *image;
        MonoAssembly *assembly;
@@ -3620,7 +3666,7 @@ coverage_filter (MonoProfiler *prof, MonoMethod *method)
                has_positive = FALSE;
                found = FALSE;
                for (guint i = 0; i < prof->coverage_filters->len; ++i) {
-                       char *filter = g_ptr_array_index (prof->coverage_filters, i);
+                       char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
 
                        if (filter [0] == '+') {
                                filter = &filter [1];
@@ -3640,9 +3686,9 @@ coverage_filter (MonoProfiler *prof, MonoMethod *method)
                if (has_positive && !found) {
                        COVERAGE_DEBUG(fprintf (stderr, "   Positive match was not found\n");)
 
-                       mono_mutex_lock (&coverage_mutex);
+                       mono_os_mutex_lock (&coverage_mutex);
                        mono_conc_hashtable_insert (filtered_classes, klass, klass);
-                       mono_mutex_unlock (&coverage_mutex);
+                       mono_os_mutex_unlock (&coverage_mutex);
                        g_free (fqn);
                        g_free (classname);
 
@@ -3651,7 +3697,7 @@ coverage_filter (MonoProfiler *prof, MonoMethod *method)
 
                for (guint i = 0; i < prof->coverage_filters->len; ++i) {
                        // FIXME: Is substring search sufficient?
-                       char *filter = g_ptr_array_index (prof->coverage_filters, i);
+                       char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
                        if (filter [0] == '+')
                                continue;
 
@@ -3662,9 +3708,9 @@ coverage_filter (MonoProfiler *prof, MonoMethod *method)
                        if (strstr (fqn, filter) != NULL) {
                                COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
 
-                               mono_mutex_lock (&coverage_mutex);
+                               mono_os_mutex_lock (&coverage_mutex);
                                mono_conc_hashtable_insert (filtered_classes, klass, klass);
-                               mono_mutex_unlock (&coverage_mutex);
+                               mono_os_mutex_unlock (&coverage_mutex);
                                g_free (fqn);
                                g_free (classname);
 
@@ -3679,38 +3725,39 @@ coverage_filter (MonoProfiler *prof, MonoMethod *method)
        }
 
        COVERAGE_DEBUG(fprintf (stderr, "   Handling coverage for %s\n", mono_method_get_name (method));)
-       header = mono_method_get_header (method);
+       header = mono_method_get_header_checked (method, &error);
+       mono_error_cleanup (&error);
 
        mono_method_header_get_code (header, &code_size, NULL);
 
        assembly = mono_image_get_assembly (image);
 
-       mono_mutex_lock (&coverage_mutex);
+       mono_os_mutex_lock (&coverage_mutex);
        mono_conc_hashtable_insert (coverage_methods, method, method);
        mono_conc_hashtable_insert (coverage_assemblies, assembly, assembly);
-       mono_mutex_unlock (&coverage_mutex);
+       mono_os_mutex_unlock (&coverage_mutex);
 
-       image_methods = mono_conc_hashtable_lookup (image_to_methods, image);
+       image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
 
        if (image_methods == NULL) {
-               image_methods = g_malloc (sizeof (MonoLockFreeQueue));
+               image_methods = (MonoLockFreeQueue *)g_malloc (sizeof (MonoLockFreeQueue));
                mono_lock_free_queue_init (image_methods);
-               mono_mutex_lock (&coverage_mutex);
+               mono_os_mutex_lock (&coverage_mutex);
                mono_conc_hashtable_insert (image_to_methods, image, image_methods);
-               mono_mutex_unlock (&coverage_mutex);
+               mono_os_mutex_unlock (&coverage_mutex);
        }
 
        node = create_method_node (method);
        mono_lock_free_queue_enqueue (image_methods, node);
 
-       class_methods = mono_conc_hashtable_lookup (coverage_classes, klass);
+       class_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (coverage_classes, klass);
 
        if (class_methods == NULL) {
-               class_methods = g_malloc (sizeof (MonoLockFreeQueue));
+               class_methods = (MonoLockFreeQueue *)g_malloc (sizeof (MonoLockFreeQueue));
                mono_lock_free_queue_init (class_methods);
-               mono_mutex_lock (&coverage_mutex);
+               mono_os_mutex_lock (&coverage_mutex);
                mono_conc_hashtable_insert (coverage_classes, klass, class_methods);
-               mono_mutex_unlock (&coverage_mutex);
+               mono_os_mutex_unlock (&coverage_mutex);
        }
 
        node = create_method_node (method);
@@ -3745,7 +3792,7 @@ get_file_content (FILE *stream)
        if (filesize > MAX_FILE_SIZE)
          return NULL;
 
-       buffer = g_malloc ((filesize + 1) * sizeof (char));
+       buffer = (char *)g_malloc ((filesize + 1) * sizeof (char));
        while ((bytes_read = fread (buffer + offset, 1, LINE_BUFFER_SIZE, stream)) > 0)
                offset += bytes_read;
 
@@ -3813,7 +3860,7 @@ coverage_init (MonoProfiler *prof)
 
        COVERAGE_DEBUG(fprintf (stderr, "Coverage initialized\n");)
 
-       mono_mutex_init (&coverage_mutex);
+       mono_os_mutex_init (&coverage_mutex);
        coverage_methods = mono_conc_hashtable_new (NULL, NULL);
        coverage_assemblies = mono_conc_hashtable_new (NULL, NULL);
        coverage_classes = mono_conc_hashtable_new (NULL, NULL);
@@ -3872,7 +3919,7 @@ log_shutdown (MonoProfiler *prof)
                fclose (prof->file);
 
        mono_conc_hashtable_destroy (prof->method_table);
-       mono_mutex_destroy (&prof->method_table_mutex);
+       mono_os_mutex_destroy (&prof->method_table_mutex);
 
        if (coverage_initialized) {
                mono_conc_hashtable_destroy (coverage_methods);
@@ -3883,7 +3930,7 @@ log_shutdown (MonoProfiler *prof)
                mono_conc_hashtable_destroy (entered_methods);
                mono_conc_hashtable_destroy (image_to_methods);
                mono_conc_hashtable_destroy (suppressed_assemblies);
-               mono_mutex_destroy (&coverage_mutex);
+               mono_os_mutex_destroy (&coverage_mutex);
        }
 
        free (prof);
@@ -3921,7 +3968,7 @@ new_filename (const char* filename)
                1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
        s_date = strlen (time_buf);
        s_pid = strlen (pid_buf);
-       d = res = malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
+       d = res = (char *)malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
        for (p = filename; *p; p++) {
                if (*p != '%') {
                        *d++ = *p;
@@ -3956,7 +4003,7 @@ extern void mono_threads_attach_tools_thread (void);
 static void*
 helper_thread (void* arg)
 {
-       MonoProfiler* prof = arg;
+       MonoProfiler* prof = (MonoProfiler *)arg;
        int command_socket;
        int len;
        char buf [64];
@@ -4142,7 +4189,7 @@ start_helper_thread (MonoProfiler* prof)
 static void *
 writer_thread (void *arg)
 {
-       MonoProfiler *prof = arg;
+       MonoProfiler *prof = (MonoProfiler *)arg;
 
        mono_threads_attach_tools_thread ();
 
@@ -4164,7 +4211,7 @@ writer_thread (void *arg)
                         * methods have metadata emitted before they're referenced.
                         */
                        for (guint i = 0; i < entry->methods->len; i++) {
-                               MethodInfo *info = g_ptr_array_index (entry->methods, i);
+                               MethodInfo *info = (MethodInfo *)g_ptr_array_index (entry->methods, i);
 
                                if (mono_conc_hashtable_lookup (prof->method_table, info->method))
                                        continue;
@@ -4181,9 +4228,9 @@ writer_thread (void *arg)
                                 * method lists will just be empty for the rest of the
                                 * app's lifetime.
                                 */
-                               mono_mutex_lock (&prof->method_table_mutex);
+                               mono_os_mutex_lock (&prof->method_table_mutex);
                                mono_conc_hashtable_insert (prof->method_table, info->method, info->method);
-                               mono_mutex_unlock (&prof->method_table_mutex);
+                               mono_os_mutex_unlock (&prof->method_table_mutex);
 
                                char *name = mono_method_full_name (info->method, 1);
                                int nlen = strlen (name) + 1;
@@ -4263,7 +4310,7 @@ create_profiler (const char *filename, GPtrArray *filters)
        MonoProfiler *prof;
        char *nf;
        int force_delete = 0;
-       prof = calloc (1, sizeof (MonoProfiler));
+       prof = (MonoProfiler *)calloc (1, sizeof (MonoProfiler));
 
        prof->command_port = command_port;
        if (filename && *filename == '-') {
@@ -4280,7 +4327,7 @@ create_profiler (const char *filename, GPtrArray *filters)
                nf = new_filename (filename);
                if (do_report) {
                        int s = strlen (nf) + 32;
-                       char *p = malloc (s);
+                       char *p = (char *)malloc (s);
                        snprintf (p, s, "|mprof-report '--out=%s' -", nf);
                        free (nf);
                        nf = p;
@@ -4333,7 +4380,7 @@ create_profiler (const char *filename, GPtrArray *filters)
 #endif
 
        mono_lock_free_queue_init (&prof->writer_queue);
-       mono_mutex_init (&prof->method_table_mutex);
+       mono_os_mutex_init (&prof->method_table_mutex);
        prof->method_table = mono_conc_hashtable_new (NULL, NULL);
 
        if (do_coverage)
@@ -4393,7 +4440,7 @@ match_option (const char* p, const char *opt, char **rval)
                                } else {
                                        l = end - opt;
                                }
-                               val = malloc (l + 1);
+                               val = (char *)malloc (l + 1);
                                memcpy (val, opt, l);
                                val [l] = 0;
                                *rval = val;
@@ -4727,7 +4774,7 @@ mono_profiler_startup (const char *desc)
        mono_profiler_install_context (context_loaded, context_unloaded);
        mono_profiler_install_class (NULL, class_loaded, NULL, class_unloaded);
        mono_profiler_install_module (NULL, image_loaded, NULL, image_unloaded);
-       mono_profiler_install_assembly (NULL, assembly_loaded, NULL, assembly_unloaded);
+       mono_profiler_install_assembly (NULL, assembly_loaded, assembly_unloaded, NULL);
        mono_profiler_install_thread (thread_start, thread_end);
        mono_profiler_install_thread_name (thread_name);
        mono_profiler_install_enter_leave (method_enter, method_leave);
@@ -4745,7 +4792,7 @@ mono_profiler_startup (const char *desc)
                mono_profiler_install_statistical (mono_sample_hit);
        }
 
-       mono_profiler_set_events (events);
+       mono_profiler_set_events ((MonoProfileFlags)events);
 
        TLS_INIT (tlsbuffer);
        TLS_INIT (tlsmethodlist);