[profiler] Some improvements to the code coverage API.
[mono.git] / mono / metadata / profiler.c
1 /*
2  * Licensed to the .NET Foundation under one or more agreements.
3  * The .NET Foundation licenses this file to you under the MIT license.
4  * See the LICENSE file in the project root for more information.
5  */
6
7 #include <mono/metadata/assembly.h>
8 #include <mono/metadata/gc-internals.h>
9 #include <mono/metadata/mono-config-dirs.h>
10 #include <mono/metadata/mono-debug.h>
11 #include <mono/metadata/profiler-private.h>
12 #include <mono/utils/mono-dl.h>
13 #include <mono/utils/mono-error-internals.h>
14 #include <mono/utils/mono-logger-internals.h>
15
16 MonoProfilerState mono_profiler_state;
17
18 typedef void (*MonoProfilerInitializer) (const char *);
19
20 #define OLD_INITIALIZER_NAME "mono_profiler_startup"
21 #define NEW_INITIALIZER_NAME "mono_profiler_init"
22
23 static gboolean
24 load_profiler (MonoDl *module, const char *name, const char *desc)
25 {
26         if (!module)
27                 return FALSE;
28
29         char *err, *old_name = g_strdup_printf (OLD_INITIALIZER_NAME);
30         MonoProfilerInitializer func;
31
32         if (!(err = mono_dl_symbol (module, old_name, (gpointer) &func))) {
33                 mono_profiler_printf_err ("Found old-style startup symbol '%s' for the '%s' profiler; it has not been migrated to the new API.", old_name, name);
34                 g_free (old_name);
35                 return FALSE;
36         }
37
38         g_free (err);
39         g_free (old_name);
40
41         char *new_name = g_strdup_printf (NEW_INITIALIZER_NAME "_%s", name);
42
43         if ((err = mono_dl_symbol (module, new_name, (gpointer *) &func))) {
44                 g_free (err);
45                 g_free (new_name);
46                 return FALSE;
47         }
48
49         g_free (new_name);
50
51         func (desc);
52
53         return TRUE;
54 }
55
56 static gboolean
57 load_profiler_from_executable (const char *name, const char *desc)
58 {
59         char *err;
60
61         /*
62          * Some profilers (such as ours) may need to call back into the runtime
63          * from their sampling callback (which is called in async-signal context).
64          * They need to be able to know that all references back to the runtime
65          * have been resolved; otherwise, calling runtime functions may result in
66          * invoking the dynamic linker which is not async-signal-safe. Passing
67          * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
68          */
69         MonoDl *module = mono_dl_open (NULL, MONO_DL_EAGER, &err);
70
71         if (!module) {
72                 mono_profiler_printf_err ("Could not open main executable: %s", err);
73                 g_free (err);
74                 return FALSE;
75         }
76
77         return load_profiler (module, name, desc);
78 }
79
80 static gboolean
81 load_profiler_from_directory (const char *directory, const char *libname, const char *name, const char *desc)
82 {
83         char* path;
84         void *iter = NULL;
85
86         while ((path = mono_dl_build_path (directory, libname, &iter))) {
87                 // See the comment in load_embedded_profiler ().
88                 MonoDl *module = mono_dl_open (path, MONO_DL_EAGER, NULL);
89
90                 g_free (path);
91
92                 if (module)
93                         return load_profiler (module, name, desc);
94         }
95
96         return FALSE;
97 }
98
99 static gboolean
100 load_profiler_from_installation (const char *libname, const char *name, const char *desc)
101 {
102         char *err;
103         MonoDl *module = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
104
105         g_free (err);
106
107         if (module)
108                 return load_profiler (module, name, desc);
109
110         return FALSE;
111 }
112
113 void
114 mono_profiler_load (const char *desc)
115 {
116         if (!desc || !strcmp ("default", desc))
117                 desc = "log:report";
118
119         const char *col = strchr (desc, ':');
120         char *mname;
121
122         if (col != NULL) {
123                 mname = (char *) g_memdup (desc, col - desc + 1);
124                 mname [col - desc] = 0;
125         } else
126                 mname = g_strdup (desc);
127
128         if (!load_profiler_from_executable (mname, desc)) {
129                 char *libname = g_strdup_printf ("mono-profiler-%s", mname);
130                 gboolean res = load_profiler_from_installation (libname, mname, desc);
131
132                 if (!res && mono_config_get_assemblies_dir ())
133                         res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, mname, desc);
134
135                 if (!res)
136                         res = load_profiler_from_directory (NULL, libname, mname, desc);
137
138                 if (!res)
139                         mono_profiler_printf_err ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);
140
141                 g_free (libname);
142         }
143
144         g_free (mname);
145 }
146
147 MonoProfilerHandle
148 mono_profiler_create (MonoProfiler *prof)
149 {
150         MonoProfilerHandle handle = g_new0 (struct _MonoProfilerDesc, 1);
151
152         handle->prof = prof;
153         handle->next = mono_profiler_state.profilers;
154
155         mono_profiler_state.profilers = handle;
156
157         return handle;
158 }
159
160 void
161 mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb)
162 {
163         InterlockedWritePointer (&handle->coverage_filter, (gpointer) cb);
164 }
165
166 mono_bool
167 mono_profiler_enable_coverage (void)
168 {
169         if (mono_profiler_state.startup_done)
170                 return FALSE;
171
172         mono_os_mutex_init (&mono_profiler_state.coverage_mutex);
173         mono_profiler_state.coverage_hash = g_hash_table_new (NULL, NULL);
174
175         if (!mono_debug_enabled ())
176                 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
177
178         return TRUE;
179 }
180
181 static void
182 coverage_lock (void)
183 {
184         mono_os_mutex_lock (&mono_profiler_state.coverage_mutex);
185 }
186
187 static void
188 coverage_unlock (void)
189 {
190         mono_os_mutex_unlock (&mono_profiler_state.coverage_mutex);
191 }
192
193 mono_bool
194 mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb)
195 {
196         if (!mono_profiler_state.code_coverage)
197                 return FALSE;
198
199         coverage_lock ();
200
201         MonoProfilerCoverageInfo *info = g_hash_table_lookup (mono_profiler_state.coverage_hash, method);
202
203         coverage_unlock ();
204
205         if (!info)
206                 return FALSE;
207
208         MonoError error;
209         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
210         mono_error_assert_ok (&error);
211
212         guint32 size;
213
214         const unsigned char *start = mono_method_header_get_code (header, &size, NULL);
215         const unsigned char *end = start - size;
216         MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
217
218         for (guint32 i = 0; i < info->entries; i++) {
219                 guchar *cil_code = info->data [i].cil_code;
220
221                 if (cil_code && cil_code >= start && cil_code < end) {
222                         guint32 offset = cil_code - start;
223
224                         MonoProfilerCoverageData data = {
225                                 .method = method,
226                                 .il_offset = offset,
227                                 .counter = info->data [i].count,
228                                 .line = 1,
229                                 .column = 1,
230                         };
231
232                         if (minfo) {
233                                 MonoDebugSourceLocation *loc = mono_debug_method_lookup_location (minfo, offset);
234
235                                 if (loc) {
236                                         data.file_name = g_strdup (loc->source_file);
237                                         data.line = loc->row;
238                                         data.column = loc->column;
239
240                                         mono_debug_free_source_location (loc);
241                                 }
242                         }
243
244                         cb (handle->prof, &data);
245
246                         g_free ((char *) data.file_name);
247                 }
248         }
249
250         mono_metadata_free_mh (header);
251
252         return TRUE;
253 }
254
255 MonoProfilerCoverageInfo *
256 mono_profiler_coverage_alloc (MonoMethod *method, guint32 entries)
257 {
258         if (!mono_profiler_state.code_coverage)
259                 return FALSE;
260
261         gboolean cover = FALSE;
262
263         for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
264                 MonoProfilerCoverageFilterCallback cb = handle->coverage_filter;
265
266                 if (cb)
267                         cover |= cb (handle->prof, method);
268         }
269
270         if (!cover)
271                 return NULL;
272
273         coverage_lock ();
274
275         MonoProfilerCoverageInfo *info = g_malloc0 (sizeof (MonoProfilerCoverageInfo) + SIZEOF_VOID_P * 2 * entries);
276
277         info->entries = entries;
278
279         g_hash_table_insert (mono_profiler_state.coverage_hash, method, info);
280
281         coverage_unlock ();
282
283         return info;
284 }
285
286 mono_bool
287 mono_profiler_enable_sampling (MonoProfilerHandle handle)
288 {
289         if (mono_profiler_state.startup_done)
290                 return FALSE;
291
292         if (mono_profiler_state.sampling_owner)
293                 return TRUE;
294
295         mono_profiler_state.sampling_owner = handle;
296         mono_profiler_state.sample_mode = MONO_PROFILER_SAMPLE_MODE_NONE;
297         mono_profiler_state.sample_freq = 100;
298         mono_os_sem_init (&mono_profiler_state.sampling_semaphore, 0);
299
300         return TRUE;
301 }
302
303 mono_bool
304 mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint32_t freq)
305 {
306         if (handle != mono_profiler_state.sampling_owner)
307                 return FALSE;
308
309         mono_profiler_state.sample_mode = mode;
310         mono_profiler_state.sample_freq = freq;
311
312         mono_profiler_sampling_thread_post ();
313
314         return TRUE;
315 }
316
317 mono_bool
318 mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq)
319 {
320         if (mode)
321                 *mode = mono_profiler_state.sample_mode;
322
323         if (freq)
324                 *freq = mono_profiler_state.sample_freq;
325
326         return handle == mono_profiler_state.sampling_owner;
327 }
328
329 gboolean
330 mono_profiler_sampling_enabled (void)
331 {
332         return !!mono_profiler_state.sampling_owner;
333 }
334
335 void
336 mono_profiler_sampling_thread_post (void)
337 {
338         mono_os_sem_post (&mono_profiler_state.sampling_semaphore);
339 }
340
341 void
342 mono_profiler_sampling_thread_wait (void)
343 {
344         mono_os_sem_wait (&mono_profiler_state.sampling_semaphore, MONO_SEM_FLAGS_NONE);
345 }
346
347 mono_bool
348 mono_profiler_enable_allocations (void)
349 {
350         if (mono_profiler_state.startup_done)
351                 return FALSE;
352
353         return mono_profiler_state.allocations = TRUE;
354 }
355
356 void
357 mono_profiler_set_call_instrumentation_filter_callback (MonoProfilerHandle handle, MonoProfilerCallInstrumentationFilterCallback cb)
358 {
359         InterlockedWritePointer (&handle->call_instrumentation_filter, (gpointer) cb);
360 }
361
362 mono_bool
363 mono_profiler_enable_call_context_introspection (void)
364 {
365         if (mono_profiler_state.startup_done)
366                 return FALSE;
367
368         mono_profiler_state.context_enable ();
369
370         return mono_profiler_state.call_contexts = TRUE;
371 }
372
373 void *
374 mono_profiler_call_context_get_this (MonoProfilerCallContext *context)
375 {
376         if (!mono_profiler_state.call_contexts)
377                 return NULL;
378
379         return mono_profiler_state.context_get_this (context);
380 }
381
382 void *
383 mono_profiler_call_context_get_argument (MonoProfilerCallContext *context, uint32_t position)
384 {
385         if (!mono_profiler_state.call_contexts)
386                 return NULL;
387
388         return mono_profiler_state.context_get_argument (context, position);
389 }
390
391 void *
392 mono_profiler_call_context_get_local (MonoProfilerCallContext *context, uint32_t position)
393 {
394         if (!mono_profiler_state.call_contexts)
395                 return NULL;
396
397         return mono_profiler_state.context_get_local (context, position);
398 }
399
400 void *
401 mono_profiler_call_context_get_result (MonoProfilerCallContext *context)
402 {
403         if (!mono_profiler_state.call_contexts)
404                 return NULL;
405
406         return mono_profiler_state.context_get_result (context);
407 }
408
409 void
410 mono_profiler_call_context_free_buffer (void *buffer)
411 {
412         mono_profiler_state.context_free_buffer (buffer);
413 }
414
415 MonoProfilerCallInstrumentationFlags
416 mono_profiler_get_call_instrumentation_flags (MonoMethod *method)
417 {
418         MonoProfilerCallInstrumentationFlags flags = MONO_PROFILER_CALL_INSTRUMENTATION_NONE;
419
420         for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
421                 MonoProfilerCallInstrumentationFilterCallback cb = handle->call_instrumentation_filter;
422
423                 if (cb)
424                         flags |= cb (handle->prof, method);
425         }
426
427         return flags;
428 }
429
430 void
431 mono_profiler_started (void)
432 {
433         mono_profiler_state.startup_done = TRUE;
434 }
435
436 void
437 mono_profiler_cleanup (void)
438 {
439         for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
440 #define _MONO_PROFILER_EVENT(name) \
441         mono_profiler_set_ ## name ## _callback (handle, NULL); \
442         g_assert (!handle->name ## _cb);
443 #define MONO_PROFILER_EVENT_0(name, type) \
444         _MONO_PROFILER_EVENT(name)
445 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
446         _MONO_PROFILER_EVENT(name)
447 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
448         _MONO_PROFILER_EVENT(name)
449 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
450         _MONO_PROFILER_EVENT(name)
451 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
452         _MONO_PROFILER_EVENT(name)
453 #include <mono/metadata/profiler-events.h>
454 #undef MONO_PROFILER_EVENT_0
455 #undef MONO_PROFILER_EVENT_1
456 #undef MONO_PROFILER_EVENT_2
457 #undef MONO_PROFILER_EVENT_3
458 #undef MONO_PROFILER_EVENT_4
459 #undef _MONO_PROFILER_EVENT
460         }
461
462 #define _MONO_PROFILER_EVENT(name, type) \
463         g_assert (!mono_profiler_state.name ## _count);
464 #define MONO_PROFILER_EVENT_0(name, type) \
465         _MONO_PROFILER_EVENT(name, type)
466 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
467         _MONO_PROFILER_EVENT(name, type)
468 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
469         _MONO_PROFILER_EVENT(name, type)
470 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
471         _MONO_PROFILER_EVENT(name, type)
472 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
473         _MONO_PROFILER_EVENT(name, type)
474 #include <mono/metadata/profiler-events.h>
475 #undef MONO_PROFILER_EVENT_0
476 #undef MONO_PROFILER_EVENT_1
477 #undef MONO_PROFILER_EVENT_2
478 #undef MONO_PROFILER_EVENT_3
479 #undef MONO_PROFILER_EVENT_4
480 #undef _MONO_PROFILER_EVENT
481 }
482
483 static void
484 update_callback (volatile gpointer *location, gpointer new_, volatile gint32 *counter)
485 {
486         gpointer old;
487
488         do {
489                 old = InterlockedReadPointer (location);
490         } while (InterlockedCompareExchangePointer (location, new_, old) != old);
491
492         /*
493          * At this point, we could have installed a NULL callback while the counter
494          * is still non-zero, i.e. setting the callback and modifying the counter
495          * is not a single atomic operation. This is fine as we make sure callbacks
496          * are non-NULL before invoking them (see the code below that generates the
497          * raise functions), and besides, updating callbacks at runtime is an
498          * inherently racy operation.
499          */
500
501         if (old)
502                 InterlockedDecrement (counter);
503
504         if (new_)
505                 InterlockedIncrement (counter);
506 }
507
508 #define _MONO_PROFILER_EVENT(name, type) \
509         void \
510         mono_profiler_set_ ## name ## _callback (MonoProfilerHandle handle, MonoProfiler ## type ## Callback cb) \
511         { \
512                 update_callback (&handle->name ## _cb, (gpointer) cb, &mono_profiler_state.name ## _count); \
513         }
514 #define MONO_PROFILER_EVENT_0(name, type) \
515         _MONO_PROFILER_EVENT(name, type)
516 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
517         _MONO_PROFILER_EVENT(name, type)
518 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
519         _MONO_PROFILER_EVENT(name, type)
520 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
521         _MONO_PROFILER_EVENT(name, type)
522 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
523         _MONO_PROFILER_EVENT(name, type)
524 #include <mono/metadata/profiler-events.h>
525 #undef MONO_PROFILER_EVENT_0
526 #undef MONO_PROFILER_EVENT_1
527 #undef MONO_PROFILER_EVENT_2
528 #undef MONO_PROFILER_EVENT_3
529 #undef MONO_PROFILER_EVENT_4
530 #undef _MONO_PROFILER_EVENT
531
532 #define _MONO_PROFILER_EVENT(name, type, params, args) \
533         void \
534         mono_profiler_raise_ ## name params \
535         { \
536                 for (MonoProfilerHandle h = mono_profiler_state.profilers; h; h = h->next) { \
537                         MonoProfiler ## type ## Callback cb = h->name ## _cb; \
538                         if (cb) \
539                                 cb args; \
540                 } \
541         }
542 #define MONO_PROFILER_EVENT_0(name, type) \
543         _MONO_PROFILER_EVENT(name, type, (void), (h->prof))
544 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
545         _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name), (h->prof, arg1_name))
546 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
547         _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name, arg2_type arg2_name), (h->prof, arg1_name, arg2_name))
548 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
549         _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name), (h->prof, arg1_name, arg2_name, arg3_name))
550 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
551         _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name, arg4_type arg4_name), (h->prof, arg1_name, arg2_name, arg3_name, arg4_name))
552 #include <mono/metadata/profiler-events.h>
553 #undef MONO_PROFILER_EVENT_0
554 #undef MONO_PROFILER_EVENT_1
555 #undef MONO_PROFILER_EVENT_2
556 #undef MONO_PROFILER_EVENT_3
557 #undef MONO_PROFILER_EVENT_4
558 #undef _MONO_PROFILER_EVENT
559
560 /*
561  * The following code is here to maintain compatibility with a few profiler API
562  * functions used by Xamarin.{Android,iOS,Mac} so that they keep working
563  * regardless of which system Mono version is used.
564  *
565  * TODO: Remove this some day if we're OK with breaking compatibility.
566  */
567
568 typedef void *MonoLegacyProfiler;
569
570 typedef void (*MonoLegacyProfileFunc) (MonoLegacyProfiler *prof);
571 typedef void (*MonoLegacyProfileThreadFunc) (MonoLegacyProfiler *prof, uintptr_t tid);
572 typedef void (*MonoLegacyProfileGCFunc) (MonoLegacyProfiler *prof, MonoProfilerGCEvent event, int generation);
573 typedef void (*MonoLegacyProfileGCResizeFunc) (MonoLegacyProfiler *prof, int64_t new_size);
574 typedef void (*MonoLegacyProfileJitResult) (MonoLegacyProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result);
575
576 struct _MonoProfiler {
577         MonoProfilerHandle handle;
578         MonoLegacyProfiler *profiler;
579         MonoLegacyProfileFunc shutdown_callback;
580         MonoLegacyProfileThreadFunc thread_start, thread_end;
581         MonoLegacyProfileGCFunc gc_event;
582         MonoLegacyProfileGCResizeFunc gc_heap_resize;
583         MonoLegacyProfileJitResult jit_end2;
584 };
585
586 static MonoProfiler *current;
587
588 MONO_API void mono_profiler_install (MonoLegacyProfiler *prof, MonoLegacyProfileFunc callback);
589 MONO_API void mono_profiler_install_thread (MonoLegacyProfileThreadFunc start, MonoLegacyProfileThreadFunc end);
590 MONO_API void mono_profiler_install_gc (MonoLegacyProfileGCFunc callback, MonoLegacyProfileGCResizeFunc heap_resize_callback);
591 MONO_API void mono_profiler_install_jit_end (MonoLegacyProfileJitResult end);
592 MONO_API void mono_profiler_set_events (int flags);
593
594 static void
595 shutdown_cb (MonoProfiler *prof)
596 {
597         prof->shutdown_callback (prof->profiler);
598 }
599
600 void
601 mono_profiler_install (MonoLegacyProfiler *prof, MonoLegacyProfileFunc callback)
602 {
603         current = g_new0 (MonoProfiler, 1);
604         current->handle = mono_profiler_create (current);
605         current->profiler = prof;
606         current->shutdown_callback = callback;
607
608         if (callback)
609                 mono_profiler_set_runtime_shutdown_end_callback (current->handle, shutdown_cb);
610 }
611
612 static void
613 thread_start_cb (MonoProfiler *prof, uintptr_t tid)
614 {
615         prof->thread_start (prof->profiler, tid);
616 }
617
618 static void
619 thread_stop_cb (MonoProfiler *prof, uintptr_t tid)
620 {
621         prof->thread_end (prof->profiler, tid);
622 }
623
624 void
625 mono_profiler_install_thread (MonoLegacyProfileThreadFunc start, MonoLegacyProfileThreadFunc end)
626 {
627         current->thread_start = start;
628         current->thread_end = end;
629
630         if (start)
631                 mono_profiler_set_thread_started_callback (current->handle, thread_start_cb);
632
633         if (end)
634                 mono_profiler_set_thread_stopped_callback (current->handle, thread_stop_cb);
635 }
636
637 static void
638 gc_event_cb (MonoProfiler *prof, MonoProfilerGCEvent event, uint32_t generation)
639 {
640         prof->gc_event (prof->profiler, event, generation);
641 }
642
643 static void
644 gc_resize_cb (MonoProfiler *prof, uintptr_t size)
645 {
646         prof->gc_heap_resize (prof->profiler, size);
647 }
648
649 void
650 mono_profiler_install_gc (MonoLegacyProfileGCFunc callback, MonoLegacyProfileGCResizeFunc heap_resize_callback)
651 {
652         current->gc_event = callback;
653         current->gc_heap_resize = heap_resize_callback;
654
655         if (callback)
656                 mono_profiler_set_gc_event_callback (current->handle, gc_event_cb);
657
658         if (heap_resize_callback)
659                 mono_profiler_set_gc_resize_callback (current->handle, gc_resize_cb);
660 }
661
662 static void
663 jit_done_cb (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo)
664 {
665         prof->jit_end2 (prof->profiler, method, jinfo, 0);
666 }
667
668 static void
669 jit_failed_cb (MonoProfiler *prof, MonoMethod *method)
670 {
671         prof->jit_end2 (prof->profiler, method, NULL, 1);
672 }
673
674 void
675 mono_profiler_install_jit_end (MonoLegacyProfileJitResult end)
676 {
677         current->jit_end2 = end;
678
679         if (end) {
680                 mono_profiler_set_jit_done_callback (current->handle, jit_done_cb);
681                 mono_profiler_set_jit_failed_callback (current->handle, jit_failed_cb);
682         }
683 }
684
685 void
686 mono_profiler_set_events (int flags)
687 {
688         /* Do nothing. */
689 }