#include <mono/metadata/mono-config-dirs.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/profiler-private.h>
+#include <mono/metadata/debug-internals.h>
#include <mono/utils/mono-dl.h>
#include <mono/utils/mono-error-internals.h>
#include <mono/utils/mono-logger-internals.h>
return handle;
}
+void
+mono_profiler_set_cleanup_callback (MonoProfilerHandle handle, MonoProfilerCleanupCallback cb)
+{
+ InterlockedWritePointer (&handle->cleanup_callback, (gpointer) cb);
+}
+
void
mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb)
{
InterlockedWritePointer (&handle->coverage_filter, (gpointer) cb);
}
-static void
-initialize_coverage (void)
+mono_bool
+mono_profiler_enable_coverage (void)
{
+ if (mono_profiler_state.startup_done)
+ return FALSE;
+
mono_os_mutex_init (&mono_profiler_state.coverage_mutex);
mono_profiler_state.coverage_hash = g_hash_table_new (NULL, NULL);
-}
-static void
-lazy_initialize_coverage (void)
-{
- mono_lazy_initialize (&mono_profiler_state.coverage_status, initialize_coverage);
+ if (!mono_debug_enabled ())
+ mono_debug_init (MONO_DEBUG_FORMAT_MONO);
+
+ return mono_profiler_state.code_coverage = TRUE;
}
static void
mono_os_mutex_unlock (&mono_profiler_state.coverage_mutex);
}
-void
+mono_bool
mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb)
{
- lazy_initialize_coverage ();
+ if (!mono_profiler_state.code_coverage)
+ return FALSE;
coverage_lock ();
coverage_unlock ();
- if (!info)
- return;
-
MonoError error;
MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
mono_error_assert_ok (&error);
guint32 size;
const unsigned char *start = mono_method_header_get_code (header, &size, NULL);
- const unsigned char *end = start - size;
+ const unsigned char *end = start + size;
MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
+ if (!info) {
+ char *source_file;
+ int i, n_il_offsets;
+ int *source_files;
+ GPtrArray *source_file_list;
+ MonoSymSeqPoint *sym_seq_points;
+
+ /* Return 0 counts for all locations */
+
+ mono_debug_get_seq_points (minfo, &source_file, &source_file_list, &source_files, &sym_seq_points, &n_il_offsets);
+ for (i = 0; i < n_il_offsets; ++i) {
+ MonoSymSeqPoint *sp = &sym_seq_points [i];
+ const char *srcfile = "";
+
+ if (source_files [i] != -1) {
+ MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, source_files [i]);
+ srcfile = sinfo->source_file;
+ }
+
+ MonoProfilerCoverageData data = {
+ .method = method,
+ .il_offset = sp->il_offset,
+ .counter = 0,
+ .file_name = srcfile,
+ .line = sp->line,
+ .column = 0,
+ };
+
+ cb (handle->prof, &data);
+ }
+
+ g_free (source_files);
+ g_free (sym_seq_points);
+ g_ptr_array_free (source_file_list, TRUE);
+
+ mono_metadata_free_mh (header);
+ return TRUE;
+ }
+
for (guint32 i = 0; i < info->entries; i++) {
guchar *cil_code = info->data [i].cil_code;
}
mono_metadata_free_mh (header);
+
+ return TRUE;
}
MonoProfilerCoverageInfo *
mono_profiler_coverage_alloc (MonoMethod *method, guint32 entries)
{
- lazy_initialize_coverage ();
+ if (!mono_profiler_state.code_coverage)
+ return FALSE;
+
+ if (method->wrapper_type)
+ return FALSE;
gboolean cover = FALSE;
return info;
}
-void
-mono_profiler_coverage_free (MonoMethod *method)
-{
- lazy_initialize_coverage ();
-
- coverage_lock ();
-
- MonoProfilerCoverageInfo *info = g_hash_table_lookup (mono_profiler_state.coverage_hash, method);
-
- if (info) {
- g_hash_table_remove (mono_profiler_state.coverage_hash, method);
- g_free (info);
- }
-
- coverage_unlock ();
-}
-
mono_bool
mono_profiler_enable_sampling (MonoProfilerHandle handle)
{
#undef MONO_PROFILER_EVENT_3
#undef MONO_PROFILER_EVENT_4
#undef _MONO_PROFILER_EVENT
+
+ MonoProfilerHandle head = mono_profiler_state.profilers;
+
+ while (head) {
+ MonoProfilerCleanupCallback cb = head->cleanup_callback;
+
+ if (cb)
+ cb (head->prof);
+
+ MonoProfilerHandle cur = head;
+ head = head->next;
+
+ g_free (cur);
+ }
+
+ if (mono_profiler_state.code_coverage) {
+ mono_os_mutex_destroy (&mono_profiler_state.coverage_mutex);
+
+ GHashTableIter iter;
+
+ g_hash_table_iter_init (&iter, mono_profiler_state.coverage_hash);
+
+ MonoProfilerCoverageInfo *info;
+
+ while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info))
+ g_free (info);
+
+ g_hash_table_destroy (mono_profiler_state.coverage_hash);
+ }
+
+ if (mono_profiler_state.sampling_owner)
+ mono_os_sem_destroy (&mono_profiler_state.sampling_semaphore);
}
static void