[profiler] Clean up logging to use the new logging functions.
[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 *desc, const char *suffix)
25 {
26         if (!module)
27                 return FALSE;
28
29         char *old_name;
30
31         if (suffix)
32                 old_name = g_strdup_printf (OLD_INITIALIZER_NAME "_%s", suffix);
33         else
34                 old_name = g_strdup_printf (OLD_INITIALIZER_NAME);
35
36         MonoProfilerInitializer func;
37
38         char *err;
39
40         if (!(err = mono_dl_symbol (module, old_name, (gpointer) &func))) {
41                 mono_profiler_printf_err ("Found old-style startup symbol %s for %s; profiler has not been migrated to the new API.", old_name, desc);
42                 g_free (old_name);
43                 return FALSE;
44         }
45
46         g_free (err);
47         g_free (old_name);
48
49         char *new_name;
50
51         if (suffix)
52                 new_name = g_strdup_printf (NEW_INITIALIZER_NAME "_%s", suffix);
53         else
54                 new_name = g_strdup_printf (NEW_INITIALIZER_NAME);
55
56         if ((err = mono_dl_symbol (module, new_name, (gpointer *) &func))) {
57                 g_free (err);
58                 g_free (new_name);
59                 return FALSE;
60         }
61
62         g_free (new_name);
63
64         func (desc);
65
66         return TRUE;
67 }
68
69 static gboolean
70 load_profiler_from_executable (const char *desc, const char *name)
71 {
72         char *err;
73
74         /*
75          * Some profilers (such as ours) may need to call back into the runtime
76          * from their sampling callback (which is called in async-signal context).
77          * They need to be able to know that all references back to the runtime
78          * have been resolved; otherwise, calling runtime functions may result in
79          * invoking the dynamic linker which is not async-signal-safe. Passing
80          * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
81          */
82         MonoDl *module = mono_dl_open (NULL, MONO_DL_EAGER, &err);
83
84         if (!module) {
85                 mono_profiler_printf_err ("Could not open main executable: %s", err);
86                 g_free (err);
87                 return FALSE;
88         }
89
90         return load_profiler (module, desc, name);
91 }
92
93 static gboolean
94 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
95 {
96         char* path;
97         void *iter = NULL;
98
99         while ((path = mono_dl_build_path (directory, libname, &iter))) {
100                 // See the comment in load_embedded_profiler ().
101                 MonoDl *module = mono_dl_open (path, MONO_DL_EAGER, NULL);
102
103                 g_free (path);
104
105                 if (module)
106                         return load_profiler (module, desc, NULL);
107         }
108
109         return FALSE;
110 }
111
112 static gboolean
113 load_profiler_from_installation (const char *libname, const char *desc)
114 {
115         char *err;
116         MonoDl *module = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
117
118         g_free (err);
119
120         if (module)
121                 return load_profiler (module, desc, NULL);
122
123         return FALSE;
124 }
125
126 void
127 mono_profiler_load (const char *desc)
128 {
129         mono_gc_base_init ();
130
131         if (!desc || !strcmp ("default", desc))
132                 desc = "log:report";
133
134         const char *col = strchr (desc, ':');
135         char *mname;
136
137         if (col != NULL) {
138                 mname = (char *) g_memdup (desc, col - desc + 1);
139                 mname [col - desc] = 0;
140         } else
141                 mname = g_strdup (desc);
142
143         if (!load_profiler_from_executable (desc, mname)) {
144                 char *libname = g_strdup_printf ("mono-profiler-%s", mname);
145                 gboolean res = load_profiler_from_installation (libname, desc);
146
147                 if (!res && mono_config_get_assemblies_dir ())
148                         res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
149
150                 if (!res)
151                         res = load_profiler_from_directory (NULL, libname, desc);
152
153                 if (!res)
154                         mono_profiler_printf_err ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);
155
156                 g_free (libname);
157         }
158
159         g_free (mname);
160 }
161
162 MonoProfilerHandle
163 mono_profiler_install (MonoProfiler *prof)
164 {
165         MonoProfilerHandle handle = g_new0 (struct _MonoProfilerDesc, 1);
166
167         handle->prof = prof;
168         handle->next = mono_profiler_state.profilers;
169
170         mono_profiler_state.profilers = handle;
171
172         return handle;
173 }
174
175 void
176 mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb)
177 {
178         InterlockedWritePointer (&handle->coverage_filter, (gpointer) cb);
179 }
180
181 static void
182 initialize_coverage (void)
183 {
184         mono_os_mutex_init (&mono_profiler_state.coverage_mutex);
185         mono_profiler_state.coverage_hash = g_hash_table_new (NULL, NULL);
186 }
187
188 static void
189 lazy_initialize_coverage (void)
190 {
191         mono_lazy_initialize (&mono_profiler_state.coverage_status, initialize_coverage);
192 }
193
194 static void
195 coverage_lock (void)
196 {
197         mono_os_mutex_lock (&mono_profiler_state.coverage_mutex);
198 }
199
200 static void
201 coverage_unlock (void)
202 {
203         mono_os_mutex_unlock (&mono_profiler_state.coverage_mutex);
204 }
205
206 void
207 mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb)
208 {
209         lazy_initialize_coverage ();
210
211         coverage_lock ();
212
213         MonoProfilerCoverageInfo *info = g_hash_table_lookup (mono_profiler_state.coverage_hash, method);
214
215         coverage_unlock ();
216
217         if (!info)
218                 return;
219
220         MonoError error;
221         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
222         mono_error_assert_ok (&error);
223
224         guint32 size;
225
226         const unsigned char *start = mono_method_header_get_code (header, &size, NULL);
227         const unsigned char *end = start - size;
228         MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
229
230         for (guint32 i = 0; i < info->entries; i++) {
231                 guchar *cil_code = info->data [i].cil_code;
232
233                 if (cil_code && cil_code >= start && cil_code < end) {
234                         guint32 offset = cil_code - start;
235
236                         MonoProfilerCoverageData data = {
237                                 .method = method,
238                                 .il_offset = offset,
239                                 .counter = info->data [i].count,
240                                 .line = 1,
241                                 .column = 1,
242                         };
243
244                         if (minfo) {
245                                 MonoDebugSourceLocation *loc = mono_debug_method_lookup_location (minfo, offset);
246
247                                 if (loc) {
248                                         data.file_name = g_strdup (loc->source_file);
249                                         data.line = loc->row;
250                                         data.column = loc->column;
251
252                                         mono_debug_free_source_location (loc);
253                                 }
254                         }
255
256                         cb (handle->prof, &data);
257
258                         g_free ((char *) data.file_name);
259                 }
260         }
261
262         mono_metadata_free_mh (header);
263 }
264
265 MonoProfilerCoverageInfo *
266 mono_profiler_coverage_alloc (MonoMethod *method, guint32 entries)
267 {
268         lazy_initialize_coverage ();
269
270         gboolean cover = FALSE;
271
272         for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
273                 MonoProfilerCoverageFilterCallback cb = handle->coverage_filter;
274
275                 if (cb)
276                         cover |= cb (handle->prof, method);
277         }
278
279         if (!cover)
280                 return NULL;
281
282         coverage_lock ();
283
284         MonoProfilerCoverageInfo *info = g_malloc0 (sizeof (MonoProfilerCoverageInfo) + SIZEOF_VOID_P * 2 * entries);
285
286         info->entries = entries;
287
288         g_hash_table_insert (mono_profiler_state.coverage_hash, method, info);
289
290         coverage_unlock ();
291
292         return info;
293 }
294
295 void
296 mono_profiler_coverage_free (MonoMethod *method)
297 {
298         lazy_initialize_coverage ();
299
300         coverage_lock ();
301
302         MonoProfilerCoverageInfo *info = g_hash_table_lookup (mono_profiler_state.coverage_hash, method);
303
304         if (info) {
305                 g_hash_table_remove (mono_profiler_state.coverage_hash, method);
306                 g_free (info);
307         }
308
309         coverage_unlock ();
310 }
311
312 mono_bool
313 mono_profiler_enable_sampling (MonoProfilerHandle handle)
314 {
315         if (mono_profiler_state.startup_done)
316                 return FALSE;
317
318         if (mono_profiler_state.sampling_owner)
319                 return TRUE;
320
321         mono_profiler_state.sampling_owner = handle;
322         mono_profiler_state.sample_mode = MONO_PROFILER_SAMPLE_MODE_NONE;
323         mono_profiler_state.sample_freq = 100;
324         mono_os_sem_init (&mono_profiler_state.sampling_semaphore, 0);
325
326         return TRUE;
327 }
328
329 mono_bool
330 mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint64_t freq)
331 {
332         if (handle != mono_profiler_state.sampling_owner)
333                 return FALSE;
334
335         mono_profiler_state.sample_mode = mode;
336         mono_profiler_state.sample_freq = freq;
337
338         mono_os_sem_post (&mono_profiler_state.sampling_semaphore);
339
340         return TRUE;
341 }
342
343 mono_bool
344 mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint64_t *freq)
345 {
346         if (mode)
347                 *mode = mono_profiler_state.sample_mode;
348
349         if (freq)
350                 *freq = mono_profiler_state.sample_freq;
351
352         return handle == mono_profiler_state.sampling_owner;
353 }
354
355 gboolean
356 mono_profiler_sampling_enabled (void)
357 {
358         return !!mono_profiler_state.sampling_owner;
359 }
360
361 void
362 mono_profiler_sampling_thread_sleep (void)
363 {
364         mono_os_sem_wait (&mono_profiler_state.sampling_semaphore, MONO_SEM_FLAGS_NONE);
365 }
366
367 mono_bool
368 mono_profiler_enable_allocations (void)
369 {
370         if (mono_profiler_state.startup_done)
371                 return FALSE;
372
373         mono_profiler_state.allocations = TRUE;
374
375         return TRUE;
376 }
377
378 void
379 mono_profiler_set_call_instrumentation_filter_callback (MonoProfilerHandle handle, MonoProfilerCallInstrumentationFilterCallback cb)
380 {
381         InterlockedWritePointer (&handle->call_instrumentation_filter, (gpointer) cb);
382 }
383
384 gboolean
385 mono_profiler_should_instrument_method (MonoMethod *method, gboolean entry)
386 {
387         MonoProfilerCallInstrumentationFlags flags = MONO_PROFILER_CALL_INSTRUMENTATION_NONE;
388
389         for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
390                 MonoProfilerCallInstrumentationFilterCallback cb = handle->call_instrumentation_filter;
391
392                 if (cb)
393                         flags |= cb (handle->prof, method);
394         }
395
396         if (entry)
397                 return flags & MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE;
398         else
399                 return flags & MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE;
400 }
401
402 void
403 mono_profiler_started (void)
404 {
405         mono_profiler_state.startup_done = TRUE;
406 }
407
408 void
409 mono_profiler_cleanup (void)
410 {
411         for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
412 #define _MONO_PROFILER_EVENT(name) \
413         mono_profiler_set_ ## name ## _callback (handle, NULL); \
414         g_assert (!handle->name ## _cb);
415 #define MONO_PROFILER_EVENT_0(name, type) \
416         _MONO_PROFILER_EVENT(name)
417 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
418         _MONO_PROFILER_EVENT(name)
419 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
420         _MONO_PROFILER_EVENT(name)
421 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
422         _MONO_PROFILER_EVENT(name)
423 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
424         _MONO_PROFILER_EVENT(name)
425 #include <mono/metadata/profiler-events.h>
426 #undef MONO_PROFILER_EVENT_0
427 #undef MONO_PROFILER_EVENT_1
428 #undef MONO_PROFILER_EVENT_2
429 #undef MONO_PROFILER_EVENT_3
430 #undef MONO_PROFILER_EVENT_4
431 #undef _MONO_PROFILER_EVENT
432         }
433
434 #define _MONO_PROFILER_EVENT(name, type) \
435         g_assert (!mono_profiler_state.name ## _count);
436 #define MONO_PROFILER_EVENT_0(name, type) \
437         _MONO_PROFILER_EVENT(name, type)
438 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
439         _MONO_PROFILER_EVENT(name, type)
440 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
441         _MONO_PROFILER_EVENT(name, type)
442 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
443         _MONO_PROFILER_EVENT(name, type)
444 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
445         _MONO_PROFILER_EVENT(name, type)
446 #include <mono/metadata/profiler-events.h>
447 #undef MONO_PROFILER_EVENT_0
448 #undef MONO_PROFILER_EVENT_1
449 #undef MONO_PROFILER_EVENT_2
450 #undef MONO_PROFILER_EVENT_3
451 #undef MONO_PROFILER_EVENT_4
452 #undef _MONO_PROFILER_EVENT
453 }
454
455 static void
456 update_callback (volatile gpointer *location, gpointer new_, volatile gint32 *counter)
457 {
458         gpointer old;
459
460         do {
461                 old = InterlockedReadPointer (location);
462         } while (InterlockedCompareExchangePointer (location, new_, old) != old);
463
464         /*
465          * At this point, we could have installed a NULL callback while the counter
466          * is still non-zero, i.e. setting the callback and modifying the counter
467          * is not a single atomic operation. This is fine as we make sure callbacks
468          * are non-NULL before invoking them (see the code below that generates the
469          * raise functions), and besides, updating callbacks at runtime is an
470          * inherently racy operation.
471          */
472
473         if (old)
474                 InterlockedDecrement (counter);
475
476         if (new_)
477                 InterlockedIncrement (counter);
478 }
479
480 #define _MONO_PROFILER_EVENT(name, type) \
481         void \
482         mono_profiler_set_ ## name ## _callback (MonoProfilerHandle handle, MonoProfiler ## type ## Callback cb) \
483         { \
484                 update_callback (&handle->name ## _cb, (gpointer) cb, &mono_profiler_state.name ## _count); \
485         }
486 #define MONO_PROFILER_EVENT_0(name, type) \
487         _MONO_PROFILER_EVENT(name, type)
488 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
489         _MONO_PROFILER_EVENT(name, type)
490 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
491         _MONO_PROFILER_EVENT(name, type)
492 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
493         _MONO_PROFILER_EVENT(name, type)
494 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
495         _MONO_PROFILER_EVENT(name, type)
496 #include <mono/metadata/profiler-events.h>
497 #undef MONO_PROFILER_EVENT_0
498 #undef MONO_PROFILER_EVENT_1
499 #undef MONO_PROFILER_EVENT_2
500 #undef MONO_PROFILER_EVENT_3
501 #undef MONO_PROFILER_EVENT_4
502 #undef _MONO_PROFILER_EVENT
503
504 #define _MONO_PROFILER_EVENT(name, type, params, args) \
505         void \
506         mono_profiler_raise_ ## name params \
507         { \
508                 for (MonoProfilerHandle h = mono_profiler_state.profilers; h; h = h->next) { \
509                         MonoProfiler ## type ## Callback cb = h->name ## _cb; \
510                         if (cb) \
511                                 cb args; \
512                 } \
513         }
514 #define MONO_PROFILER_EVENT_0(name, type) \
515         _MONO_PROFILER_EVENT(name, type, (void), (h->prof))
516 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
517         _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name), (h->prof, arg1_name))
518 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
519         _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name, arg2_type arg2_name), (h->prof, arg1_name, arg2_name))
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, (arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name), (h->prof, arg1_name, arg2_name, arg3_name))
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, (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))
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