profiler.h 10 KB

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