[2018-06] [interp] Overflow fixes (#11426)
[mono-project.git] / samples / profiler / sample.c
blob35e578be155d939288a42da3f37af2ec85528121
1 /*
2 * Bare bones profiler showing how a profiler module should be structured.
4 * Compilation:
5 * - Linux: gcc -fPIC -shared -o libmono-profiler-sample.so sample.c `pkg-config --cflags mono-2`
6 * - OS X: clang -undefined suppress -flat_namespace -o mono-profiler-sample.dylib sample.c `pkg-config --cflags mono-2`
8 * If you're using a custom prefix for your Mono installation (e.g. /opt/mono),
9 * pkg-config must be invoked like this: PKG_CONFIG_PATH=/opt/mono pkg-config --cflags mono-2
11 * Install the resulting shared library where the dynamic loader can find it,
12 * e.g. /usr/local/lib or /opt/mono (custom prefix).
14 * To use the module: mono --profile=sample hello.exe
17 #include <mono/metadata/profiler.h>
20 * Defining a type called _MonoProfiler will complete the opaque MonoProfiler
21 * type, which is used throughout the profiler API.
23 struct _MonoProfiler {
24 /* Handle obtained from mono_profiler_create (). */
25 MonoProfilerHandle handle;
27 /* Counts the number of calls observed. */
28 unsigned long long ncalls;
32 * Use static storage for the profiler structure for simplicity. The structure
33 * can be allocated dynamically as well, if needed.
35 static MonoProfiler profiler;
38 * Callback invoked after the runtime finishes shutting down. Managed code can
39 * no longer run and most runtime services are unavailable.
41 static void
42 sample_shutdown_end (MonoProfiler *prof)
44 printf ("Total number of calls: %llu\n", prof->ncalls);
48 * Method enter callback invoked on entry to all instrumented methods.
50 static void
51 sample_method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
53 prof->ncalls++;
57 * Filter callback that decides which methods to instrument and how.
59 static MonoProfilerCallInstrumentationFlags
60 sample_call_instrumentation_filter (MonoProfiler *prof, MonoMethod *method)
62 return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER;
66 * The entry point function invoked by the Mono runtime.
68 void
69 mono_profiler_init_sample (const char *desc)
71 profiler.handle = mono_profiler_create (&profiler);
73 mono_profiler_set_runtime_shutdown_end_callback (profiler.handle, sample_shutdown_end);
74 mono_profiler_set_call_instrumentation_filter_callback (profiler.handle, sample_call_instrumentation_filter);
75 mono_profiler_set_method_enter_callback (profiler.handle, sample_method_enter);