profiler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. */
  5. #ifndef __MONO_PROFILER_H__
  6. #define __MONO_PROFILER_H__
  7. #include <mono/metadata/appdomain.h>
  8. #include <mono/metadata/mono-gc.h>
  9. #include <mono/metadata/object.h>
  10. MONO_BEGIN_DECLS
  11. /**
  12. * This value will be incremented whenever breaking changes to the profiler API
  13. * are made. This macro is intended for use in profiler modules that wish to
  14. * support older versions of the profiler API.
  15. *
  16. * Version 2:
  17. * - Major overhaul of the profiler API.
  18. * Version 3:
  19. * - Added mono_profiler_enable_clauses (). This must now be called to enable
  20. * raising exception_clause events.
  21. * - The exception argument to exception_clause events can now be NULL for
  22. * finally clauses invoked in the non-exceptional case.
  23. * - The type argument to exception_clause events will now correctly indicate
  24. * that the catch portion of the clause is being executed in the case of
  25. * try-filter-catch clauses.
  26. * - Removed the iomap_report event.
  27. * - Removed the old gc_event event and renamed gc_event2 to gc_event.
  28. */
  29. #define MONO_PROFILER_API_VERSION 3
  30. typedef struct _MonoProfiler MonoProfiler;
  31. typedef struct _MonoProfilerDesc *MonoProfilerHandle;
  32. typedef void (*MonoProfilerCleanupCallback) (MonoProfiler *prof);
  33. MONO_API void mono_profiler_load (const char *desc);
  34. MONO_API MonoProfilerHandle mono_profiler_create (MonoProfiler *prof);
  35. MONO_API void mono_profiler_set_cleanup_callback (MonoProfilerHandle handle, MonoProfilerCleanupCallback cb);
  36. typedef struct {
  37. MonoMethod *method;
  38. uint32_t il_offset;
  39. uint32_t counter;
  40. const char *file_name;
  41. uint32_t line;
  42. uint32_t column;
  43. } MonoProfilerCoverageData;
  44. typedef struct _MonoDomainCoverage MonoDomainCoverage;
  45. typedef mono_bool (*MonoProfilerCoverageFilterCallback) (MonoProfiler *prof, MonoMethod *method);
  46. typedef void (*MonoProfilerCoverageCallback) (MonoProfiler *prof, const MonoProfilerCoverageData *data);
  47. MONO_API mono_bool mono_profiler_enable_coverage (void);
  48. MONO_API void mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb);
  49. #ifndef RUNTIME_IL2CPP
  50. MONO_API mono_bool mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb);
  51. MONO_API mono_bool mono_profiler_get_all_coverage_data (MonoProfilerHandle handle, MonoProfilerCoverageCallback cb);
  52. MONO_API mono_bool mono_profiler_reset_coverage (MonoMethod* method);
  53. MONO_API void mono_profiler_reset_all_coverage (void);
  54. #endif
  55. typedef enum {
  56. /**
  57. * Do not perform sampling. Will make the sampling thread sleep until the
  58. * sampling mode is changed to one of the below modes.
  59. */
  60. MONO_PROFILER_SAMPLE_MODE_NONE = 0,
  61. /**
  62. * Try to base sampling frequency on process activity. Falls back to
  63. * MONO_PROFILER_SAMPLE_MODE_REAL if such a clock is not available.
  64. */
  65. MONO_PROFILER_SAMPLE_MODE_PROCESS = 1,
  66. /**
  67. * Base sampling frequency on wall clock time. Uses a monotonic clock when
  68. * available (all major platforms).
  69. */
  70. MONO_PROFILER_SAMPLE_MODE_REAL = 2,
  71. } MonoProfilerSampleMode;
  72. MONO_API mono_bool mono_profiler_enable_sampling (MonoProfilerHandle handle);
  73. MONO_API mono_bool mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint32_t freq);
  74. MONO_API mono_bool mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq);
  75. MONO_API mono_bool mono_profiler_enable_allocations (void);
  76. MONO_API mono_bool mono_profiler_enable_clauses (void);
  77. typedef struct _MonoProfilerCallContext MonoProfilerCallContext;
  78. typedef enum {
  79. /**
  80. * Do not instrument calls.
  81. */
  82. MONO_PROFILER_CALL_INSTRUMENTATION_NONE = 0,
  83. /**
  84. * Instrument method entries.
  85. */
  86. MONO_PROFILER_CALL_INSTRUMENTATION_ENTER = 1 << 1,
  87. /**
  88. * Also capture a call context for method entries.
  89. */
  90. MONO_PROFILER_CALL_INSTRUMENTATION_ENTER_CONTEXT = 1 << 2,
  91. /**
  92. * Instrument method exits.
  93. */
  94. MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE = 1 << 3,
  95. /**
  96. * Also capture a call context for method exits.
  97. */
  98. MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE_CONTEXT = 1 << 4,
  99. /**
  100. * Instrument method exits as a result of a tail call.
  101. */
  102. MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL = 1 << 5,
  103. /**
  104. * Instrument exceptional method exits.
  105. */
  106. MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE = 1 << 6,
  107. } MonoProfilerCallInstrumentationFlags;
  108. typedef MonoProfilerCallInstrumentationFlags (*MonoProfilerCallInstrumentationFilterCallback) (MonoProfiler *prof, MonoMethod *method);
  109. MONO_API void mono_profiler_set_call_instrumentation_filter_callback (MonoProfilerHandle handle, MonoProfilerCallInstrumentationFilterCallback cb);
  110. MONO_API mono_bool mono_profiler_enable_call_context_introspection (void);
  111. MONO_API void *mono_profiler_call_context_get_this (MonoProfilerCallContext *context);
  112. MONO_API void *mono_profiler_call_context_get_argument (MonoProfilerCallContext *context, uint32_t position);
  113. MONO_API void *mono_profiler_call_context_get_local (MonoProfilerCallContext *context, uint32_t position);
  114. MONO_API void *mono_profiler_call_context_get_result (MonoProfilerCallContext *context);
  115. MONO_API void mono_profiler_call_context_free_buffer (void *buffer);
  116. typedef enum {
  117. /**
  118. * The \c data parameter is a \c MonoMethod pointer.
  119. */
  120. MONO_PROFILER_CODE_BUFFER_METHOD = 0,
  121. /**
  122. * \deprecated No longer used.
  123. */
  124. MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE = 1,
  125. /**
  126. * The \c data parameter is a \c MonoMethod pointer.
  127. */
  128. MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE = 2,
  129. MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE = 3,
  130. MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE = 4,
  131. /**
  132. * The \c data parameter is a C string.
  133. */
  134. MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE = 5,
  135. MONO_PROFILER_CODE_BUFFER_HELPER = 6,
  136. /**
  137. * \deprecated No longer used.
  138. */
  139. MONO_PROFILER_CODE_BUFFER_MONITOR = 7,
  140. MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE = 8,
  141. MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING = 9,
  142. } MonoProfilerCodeBufferType;
  143. typedef enum {
  144. MONO_GC_EVENT_PRE_STOP_WORLD = 6,
  145. /**
  146. * When this event arrives, the GC and suspend locks are acquired.
  147. */
  148. MONO_GC_EVENT_PRE_STOP_WORLD_LOCKED = 10,
  149. MONO_GC_EVENT_POST_STOP_WORLD = 7,
  150. MONO_GC_EVENT_START = 0,
  151. MONO_GC_EVENT_END = 5,
  152. MONO_GC_EVENT_PRE_START_WORLD = 8,
  153. /**
  154. * When this event arrives, the GC and suspend locks are released.
  155. */
  156. MONO_GC_EVENT_POST_START_WORLD_UNLOCKED = 11,
  157. MONO_GC_EVENT_POST_START_WORLD = 9,
  158. } MonoProfilerGCEvent;
  159. /*
  160. * The macros below will generate the majority of the callback API. Refer to
  161. * mono/metadata/profiler-events.h for a list of callbacks. They are expanded
  162. * like so:
  163. *
  164. * typedef void (*MonoProfilerRuntimeInitializedCallback (MonoProfiler *prof);
  165. * MONO_API void mono_profiler_set_runtime_initialized_callback (MonoProfiler *prof, MonoProfilerRuntimeInitializedCallback cb);
  166. *
  167. * typedef void (*MonoProfilerRuntimeShutdownCallback (MonoProfiler *prof);
  168. * MONO_API void mono_profiler_set_runtime_shutdown_callback (MonoProfiler *prof, MonoProfilerRuntimeShutdownCallback cb);
  169. *
  170. * typedef void (*MonoProfilerContextLoadedCallback (MonoProfiler *prof);
  171. * MONO_API void mono_profiler_set_context_loaded_callback (MonoProfiler *prof, MonoProfilerContextLoadedCallback cb);
  172. *
  173. * typedef void (*MonoProfilerContextUnloadedCallback (MonoProfiler *prof);
  174. * MONO_API void mono_profiler_set_context_unloaded_callback (MonoProfiler *prof, MonoProfilerContextUnloadedCallback cb);
  175. *
  176. * Etc.
  177. *
  178. * To remove a callback, pass NULL instead of a valid function pointer.
  179. * Callbacks can be changed at any point, but note that doing so is inherently
  180. * racy with respect to threads that aren't suspended, i.e. you may still see a
  181. * call from another thread right after you change a callback.
  182. *
  183. * These functions are async safe.
  184. */
  185. #define _MONO_PROFILER_EVENT(type, ...) \
  186. typedef void (*MonoProfiler ## type ## Callback) (__VA_ARGS__);
  187. #define MONO_PROFILER_EVENT_0(name, type) \
  188. _MONO_PROFILER_EVENT(type, MonoProfiler *prof)
  189. #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
  190. _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name)
  191. #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
  192. _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name, arg2_type arg2_name)
  193. #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
  194. _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name)
  195. #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
  196. _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name, arg4_type arg4_name)
  197. #define MONO_PROFILER_EVENT_5(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name, arg5_type, arg5_name) \
  198. _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name, arg4_type arg4_name, arg5_type arg5_name)
  199. #include <mono/metadata/profiler-events.h>
  200. #undef MONO_PROFILER_EVENT_0
  201. #undef MONO_PROFILER_EVENT_1
  202. #undef MONO_PROFILER_EVENT_2
  203. #undef MONO_PROFILER_EVENT_3
  204. #undef MONO_PROFILER_EVENT_4
  205. #undef MONO_PROFILER_EVENT_5
  206. #undef _MONO_PROFILER_EVENT
  207. #define _MONO_PROFILER_EVENT(name, type) \
  208. MONO_API void mono_profiler_set_ ## name ## _callback (MonoProfilerHandle handle, MonoProfiler ## type ## Callback cb);
  209. #define MONO_PROFILER_EVENT_0(name, type) \
  210. _MONO_PROFILER_EVENT(name, type)
  211. #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
  212. _MONO_PROFILER_EVENT(name, type)
  213. #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
  214. _MONO_PROFILER_EVENT(name, type)
  215. #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
  216. _MONO_PROFILER_EVENT(name, type)
  217. #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
  218. _MONO_PROFILER_EVENT(name, type)
  219. #define MONO_PROFILER_EVENT_5(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name, arg5_type, arg5_name) \
  220. _MONO_PROFILER_EVENT(name, type)
  221. #include <mono/metadata/profiler-events.h>
  222. #undef MONO_PROFILER_EVENT_0
  223. #undef MONO_PROFILER_EVENT_1
  224. #undef MONO_PROFILER_EVENT_2
  225. #undef MONO_PROFILER_EVENT_3
  226. #undef MONO_PROFILER_EVENT_4
  227. #undef MONO_PROFILER_EVENT_5
  228. #undef _MONO_PROFILER_EVENT
  229. MONO_END_DECLS
  230. #endif // __MONO_PROFILER_H__