1 #ifndef __MONO_PROFLOG_H__
2 #define __MONO_PROFLOG_H__
5 #define MONO_PROFILER_UNSTABLE_GC_ROOTS
6 #include <mono/metadata/profiler.h>
8 #define BUF_ID 0x4D504C01
9 #define LOG_HEADER_ID 0x4D505A01
10 #define LOG_VERSION_MAJOR 2
11 #define LOG_VERSION_MINOR 0
12 #define LOG_DATA_VERSION 14
15 * Changes in major/minor versions:
16 * version 1.0: removed sysid field from header
17 * added args, arch, os fields to header
19 * Changes in data versions:
20 * version 2: added offsets in heap walk
21 * version 3: added GC roots
22 * version 4: added sample/statistical profiling
23 * version 5: added counters sampling
24 * version 6: added optional backtrace in sampling info
25 * version 8: added TYPE_RUNTIME and JIT helpers/trampolines
26 * version 9: added MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
27 * version 10: added TYPE_COVERAGE
28 * version 11: added thread ID to TYPE_SAMPLE_HIT
29 added more load/unload events
32 load/unload for appdomain
33 load/unload for contexts
34 load/unload/name for assemblies
35 removed TYPE_LOAD_ERR flag (profiler never generated it, now removed from the format itself)
36 added TYPE_GC_HANDLE_{CREATED,DESTROYED}_BT
37 TYPE_JIT events are no longer guaranteed to have code start/size info (can be zero)
38 * version 12: added MONO_COUNTER_PROFILER
39 * version 13: added MONO_GC_EVENT_{PRE_STOP_WORLD_LOCKED,POST_START_WORLD_UNLOCKED}
40 added TYPE_META + TYPE_SYNC_POINT
41 removed il and native offset in TYPE_SAMPLE_HIT
42 methods in backtraces are now encoded as proper method pointers
43 removed flags in backtrace format
44 removed flags in metadata events
45 changed the following fields to a single byte rather than leb128
46 TYPE_GC_EVENT: event_type, generation
47 TYPE_HEAP_ROOT: root_type
49 TYPE_SAMPLE_HIT: sample_type
50 TYPE_CLAUSE: clause_type
51 TYPE_SAMPLE_COUNTERS_DESC: type, unit, variance
52 TYPE_SAMPLE_COUNTERS: type
53 added time fields to all events that were missing one
57 TYPE_SAMPLE_COUNTERS_DESC
59 TYPE_COVERAGE_STATEMENT
61 TYPE_COVERAGE_ASSEMBLY
62 moved the time field in TYPE_SAMPLE_HIT to right after the event byte, now encoded as a regular time field
63 changed the time field in TYPE_SAMPLE_COUNTERS to be encoded as a regular time field (in nanoseconds)
64 added TYPE_GC_FINALIZE_{START,END,OBJECT_START,OBJECT_END}
65 * version 14: added event field to TYPE_MONITOR instead of encoding it in the extended info
66 all TYPE_MONITOR events can now contain backtraces
67 changed address field in TYPE_SAMPLE_UBIN to be based on ptr_base
68 added an image pointer field to assembly load events
69 added an exception object field to TYPE_CLAUSE
70 class unload events no longer exist (they were never emitted)
71 removed type field from TYPE_SAMPLE_HIT
78 * The file is composed by a header followed by 0 or more buffers.
79 * Each buffer contains events that happened on a thread: for a given thread
80 * buffers that appear later in the file are guaranteed to contain events
81 * that happened later in time. Buffers from separate threads could be interleaved,
83 * Buffers are not required to be aligned.
86 * [id: 4 bytes] constant value: LOG_HEADER_ID
87 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
88 * [format: 1 byte] version of the data format for the rest of the file
89 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
90 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
91 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
92 * [flags: 4 bytes] file format flags, should be 0 for now
93 * [pid: 4 bytes] pid of the profiled process
94 * [port: 2 bytes] tcp port for server if != 0
95 * [args size: 4 bytes] size of args
96 * [args: string] arguments passed to the profiler
97 * [arch size: 4 bytes] size of arch
98 * [arch: string] architecture the profiler is running on
99 * [os size: 4 bytes] size of os
100 * [os: string] operating system the profiler is running on
102 * The multiple byte integers are in little-endian format.
105 * [buffer header] [event]*
106 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
107 * Timing information and other values in the event data are usually stored
108 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
109 * some data is represented as a difference between the actual value and
110 * either the last value of the same type (like for timing information) or
111 * as the difference from a value stored in a buffer header.
113 * For timing information the data is stored as uleb128, since timing
114 * increases in a monotonic way in each thread: the value is the number of
115 * nanoseconds to add to the last seen timing data in a buffer. The first value
116 * in a buffer will be calculated from the time_base field in the buffer head.
118 * Object or heap sizes are stored as uleb128.
119 * Pointer differences are stored as sleb128, instead.
121 * If an unexpected value is found, the rest of the buffer should be ignored,
122 * as generally the later values need the former to be interpreted correctly.
124 * buffer header format:
125 * [bufid: 4 bytes] constant value: BUF_ID
126 * [len: 4 bytes] size of the data following the buffer header
127 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
128 * [ptr_base: 8 bytes] base value for pointers
129 * [obj_base: 8 bytes] base value for object addresses
130 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
131 * [method_base: 8 bytes] base value for MonoMethod pointers
134 * [extended info: upper 4 bits] [type: lower 4 bits]
135 * [time diff: uleb128] nanoseconds since last timing
137 * The data that follows depends on type and the extended info.
138 * Type is one of the enum values in mono-profiler-log.h: TYPE_ALLOC, TYPE_GC,
139 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
140 * The extended info bits are interpreted based on type, see
141 * each individual event description below.
142 * strings are represented as a 0-terminated utf8 sequence.
145 * [num: uleb128] number of frames following
146 * [frame: sleb128]* mum MonoMethod* as a pointer difference from the last such
147 * pointer or the buffer method_base
151 * exinfo: zero or TYPE_ALLOC_BT
152 * [ptr: sleb128] class as a byte difference from ptr_base
153 * [obj: sleb128] object address as a byte difference from obj_base
154 * [size: uleb128] size of the object in the heap
155 * If exinfo == TYPE_ALLOC_BT, a backtrace follows.
159 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED[_BT],
160 * TYPE_GC_HANDLE_DESTROYED[_BT], TYPE_GC_FINALIZE_START, TYPE_GC_FINALIZE_END,
161 * TYPE_GC_FINALIZE_OBJECT_START, TYPE_GC_FINALIZE_OBJECT_END
162 * if exinfo == TYPE_GC_RESIZE
163 * [heap_size: uleb128] new heap size
164 * if exinfo == TYPE_GC_EVENT
165 * [event type: byte] GC event (MONO_GC_EVENT_* from profiler.h)
166 * [generation: byte] GC generation event refers to
167 * if exinfo == TYPE_GC_MOVE
168 * [num_objects: uleb128] number of object moves that follow
169 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
170 * num is always an even number: the even items are the old
171 * addresses, the odd numbers are the respective new object addresses
172 * if exinfo == TYPE_GC_HANDLE_CREATED[_BT]
173 * [handle_type: uleb128] MonoGCHandleType enum value
174 * upper bits reserved as flags
175 * [handle: uleb128] GC handle value
176 * [objaddr: sleb128] object pointer differences from obj_base
177 * If exinfo == TYPE_GC_HANDLE_CREATED_BT, a backtrace follows.
178 * if exinfo == TYPE_GC_HANDLE_DESTROYED[_BT]
179 * [handle_type: uleb128] MonoGCHandleType enum value
180 * upper bits reserved as flags
181 * [handle: uleb128] GC handle value
182 * If exinfo == TYPE_GC_HANDLE_DESTROYED_BT, a backtrace follows.
183 * if exinfo == TYPE_GC_FINALIZE_OBJECT_{START,END}
184 * [object: sleb128] the object as a difference from obj_base
186 * type metadata format:
187 * type: TYPE_METADATA
188 * exinfo: one of: TYPE_END_LOAD, TYPE_END_UNLOAD (optional for TYPE_THREAD and TYPE_DOMAIN,
189 * doesn't occur for TYPE_CLASS)
190 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
191 * TYPE_THREAD, TYPE_CONTEXT
192 * [pointer: sleb128] pointer of the metadata type depending on mtype
193 * if mtype == TYPE_CLASS
194 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
195 * [name: string] full class name
196 * if mtype == TYPE_IMAGE
197 * [name: string] image file name
198 * if mtype == TYPE_ASSEMBLY
199 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
200 * [name: string] assembly name
201 * if mtype == TYPE_DOMAIN && exinfo == 0
202 * [name: string] domain friendly name
203 * if mtype == TYPE_CONTEXT
204 * [domain: sleb128] domain id as pointer
205 * if mtype == TYPE_THREAD && exinfo == 0
206 * [name: string] thread name
208 * type method format:
210 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
211 * [method: sleb128] MonoMethod* as a pointer difference from the last such
212 * pointer or the buffer method_base
213 * if exinfo == TYPE_JIT
214 * [code address: sleb128] pointer to the native code as a diff from ptr_base
215 * [code size: uleb128] size of the generated code
216 * [name: string] full method name
218 * type exception format:
219 * type: TYPE_EXCEPTION
220 * exinfo: zero, TYPE_CLAUSE, or TYPE_THROW_BT
221 * if exinfo == TYPE_CLAUSE
222 * [clause type: byte] MonoExceptionEnum enum value
223 * [clause index: uleb128] index of the current clause
224 * [method: sleb128] MonoMethod* as a pointer difference from the last such
225 * pointer or the buffer method_base
226 * [object: sleb128] the exception object as a difference from obj_base
228 * [object: sleb128] the exception object as a difference from obj_base
229 * If exinfo == TYPE_THROW_BT, a backtrace follows.
231 * type runtime format:
233 * exinfo: one of: TYPE_JITHELPER
234 * if exinfo == TYPE_JITHELPER
235 * [type: byte] MonoProfilerCodeBufferType enum value
236 * [buffer address: sleb128] pointer to the native code as a diff from ptr_base
237 * [buffer size: uleb128] size of the generated code
238 * if type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
239 * [name: string] buffer description name
241 * type monitor format:
243 * exinfo: zero or TYPE_MONITOR_BT
244 * [type: byte] MonoProfilerMonitorEvent enum value
245 * [object: sleb128] the lock object as a difference from obj_base
246 * If exinfo == TYPE_MONITOR_BT, a backtrace follows.
250 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
251 * if exinfo == TYPE_HEAP_OBJECT
252 * [object: sleb128] the object as a difference from obj_base
253 * [class: sleb128] the object MonoClass* as a difference from ptr_base
254 * [size: uleb128] size of the object on the heap
255 * [num_refs: uleb128] number of object references
256 * each referenced objref is preceded by a uleb128 encoded offset: the
257 * first offset is from the object address and each next offset is relative
258 * to the previous one
259 * [objrefs: sleb128]+ object referenced as a difference from obj_base
260 * The same object can appear multiple times, but only the first time
261 * with size != 0: in the other cases this data will only be used to
262 * provide additional referenced objects.
263 * if exinfo == TYPE_HEAP_ROOT
264 * [num_roots: uleb128] number of root references
265 * [num_gc: uleb128] number of major gcs
266 * [object: sleb128] the object as a difference from obj_base
267 * [root_type: byte] the root_type: MonoProfileGCRootType (profiler.h)
268 * [extra_info: uleb128] the extra_info value
269 * object, root_type and extra_info are repeated num_roots times
273 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN, TYPE_SAMPLE_COUNTERS_DESC, TYPE_SAMPLE_COUNTERS
274 * if exinfo == TYPE_SAMPLE_HIT
275 * [thread: sleb128] thread id as difference from ptr_base
276 * [count: uleb128] number of following instruction addresses
277 * [ip: sleb128]* instruction pointer as difference from ptr_base
278 * [mbt_count: uleb128] number of managed backtrace frames
279 * [method: sleb128]* MonoMethod* as a pointer difference from the last such
280 * pointer or the buffer method_base (the first such method can be also indentified by ip, but this is not neccessarily true)
281 * if exinfo == TYPE_SAMPLE_USYM
282 * [address: sleb128] symbol address as a difference from ptr_base
283 * [size: uleb128] symbol size (may be 0 if unknown)
284 * [name: string] symbol name
285 * if exinfo == TYPE_SAMPLE_UBIN
286 * [address: sleb128] address where binary has been loaded as a difference from ptr_base
287 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
288 * [size: uleb128] memory size
289 * [name: string] binary name
290 * if exinfo == TYPE_SAMPLE_COUNTERS_DESC
291 * [len: uleb128] number of counters
293 * [section: uleb128] section of counter
294 * if section == MONO_COUNTER_PERFCOUNTERS:
295 * [section_name: string] section name of counter
296 * [name: string] name of counter
297 * [type: byte] type of counter
298 * [unit: byte] unit of counter
299 * [variance: byte] variance of counter
300 * [index: uleb128] unique index of counter
301 * if exinfo == TYPE_SAMPLE_COUNTERS
303 * [index: uleb128] unique index of counter
306 * [type: byte] type of counter value
309 * [0: byte] 0 -> value is null
311 * [1: byte] 1 -> value is not null
312 * [value: string] counter value
314 * [value: uleb128/sleb128/double] counter value, can be sleb128, uleb128 or double (determined by using type)
316 * type coverage format
317 * type: TYPE_COVERAGE
318 * exinfo: one of TYPE_COVERAGE_METHOD, TYPE_COVERAGE_STATEMENT, TYPE_COVERAGE_ASSEMBLY, TYPE_COVERAGE_CLASS
319 * if exinfo == TYPE_COVERAGE_METHOD
320 * [assembly: string] name of assembly
321 * [class: string] name of the class
322 * [name: string] name of the method
323 * [signature: string] the signature of the method
324 * [filename: string] the file path of the file that contains this method
325 * [token: uleb128] the method token
326 * [method_id: uleb128] an ID for this data to associate with the buffers of TYPE_COVERAGE_STATEMENTS
327 * [len: uleb128] the number of TYPE_COVERAGE_BUFFERS associated with this method
328 * if exinfo == TYPE_COVERAGE_STATEMENTS
329 * [method_id: uleb128] an the TYPE_COVERAGE_METHOD buffer to associate this with
330 * [offset: uleb128] the il offset relative to the previous offset
331 * [counter: uleb128] the counter for this instruction
332 * [line: uleb128] the line of filename containing this instruction
333 * [column: uleb128] the column containing this instruction
334 * if exinfo == TYPE_COVERAGE_ASSEMBLY
335 * [name: string] assembly name
336 * [guid: string] assembly GUID
337 * [filename: string] assembly filename
338 * [number_of_methods: uleb128] the number of methods in this assembly
339 * [fully_covered: uleb128] the number of fully covered methods
340 * [partially_covered: uleb128] the number of partially covered methods
341 * currently partially_covered will always be 0, and fully_covered is the
342 * number of methods that are fully and partially covered.
343 * if exinfo == TYPE_COVERAGE_CLASS
344 * [name: string] assembly name
345 * [class: string] class name
346 * [number_of_methods: uleb128] the number of methods in this class
347 * [fully_covered: uleb128] the number of fully covered methods
348 * [partially_covered: uleb128] the number of partially covered methods
349 * currently partially_covered will always be 0, and fully_covered is the
350 * number of methods that are fully and partially covered.
354 * exinfo: one of: TYPE_SYNC_POINT
355 * if exinfo == TYPE_SYNC_POINT
356 * [type: byte] MonoProfilerSyncPointType enum value
371 /* extended type for TYPE_HEAP */
372 TYPE_HEAP_START = 0 << 4,
373 TYPE_HEAP_END = 1 << 4,
374 TYPE_HEAP_OBJECT = 2 << 4,
375 TYPE_HEAP_ROOT = 3 << 4,
376 /* extended type for TYPE_METADATA */
377 TYPE_END_LOAD = 2 << 4,
378 TYPE_END_UNLOAD = 4 << 4,
379 /* extended type for TYPE_GC */
380 TYPE_GC_EVENT = 1 << 4,
381 TYPE_GC_RESIZE = 2 << 4,
382 TYPE_GC_MOVE = 3 << 4,
383 TYPE_GC_HANDLE_CREATED = 4 << 4,
384 TYPE_GC_HANDLE_DESTROYED = 5 << 4,
385 TYPE_GC_HANDLE_CREATED_BT = 6 << 4,
386 TYPE_GC_HANDLE_DESTROYED_BT = 7 << 4,
387 TYPE_GC_FINALIZE_START = 8 << 4,
388 TYPE_GC_FINALIZE_END = 9 << 4,
389 TYPE_GC_FINALIZE_OBJECT_START = 10 << 4,
390 TYPE_GC_FINALIZE_OBJECT_END = 11 << 4,
391 /* extended type for TYPE_METHOD */
394 TYPE_EXC_LEAVE = 3 << 4,
396 /* extended type for TYPE_EXCEPTION */
397 TYPE_THROW_NO_BT = 0 << 7,
398 TYPE_THROW_BT = 1 << 7,
399 TYPE_CLAUSE = 1 << 4,
400 /* extended type for TYPE_ALLOC */
401 TYPE_ALLOC_NO_BT = 0 << 4,
402 TYPE_ALLOC_BT = 1 << 4,
403 /* extended type for TYPE_MONITOR */
404 TYPE_MONITOR_NO_BT = 0 << 7,
405 TYPE_MONITOR_BT = 1 << 7,
406 /* extended type for TYPE_SAMPLE */
407 TYPE_SAMPLE_HIT = 0 << 4,
408 TYPE_SAMPLE_USYM = 1 << 4,
409 TYPE_SAMPLE_UBIN = 2 << 4,
410 TYPE_SAMPLE_COUNTERS_DESC = 3 << 4,
411 TYPE_SAMPLE_COUNTERS = 4 << 4,
412 /* extended type for TYPE_RUNTIME */
413 TYPE_JITHELPER = 1 << 4,
414 /* extended type for TYPE_COVERAGE */
415 TYPE_COVERAGE_ASSEMBLY = 0 << 4,
416 TYPE_COVERAGE_METHOD = 1 << 4,
417 TYPE_COVERAGE_STATEMENT = 2 << 4,
418 TYPE_COVERAGE_CLASS = 3 << 4,
419 /* extended type for TYPE_META */
420 TYPE_SYNC_POINT = 0 << 4,
424 /* metadata type byte for TYPE_METADATA */
434 SYNC_POINT_PERIODIC = 0,
435 SYNC_POINT_WORLD_STOP = 1,
436 SYNC_POINT_WORLD_START = 2,
437 } MonoProfilerSyncPointType;
440 MONO_PROFILER_MONITOR_CONTENTION = 1,
441 MONO_PROFILER_MONITOR_DONE = 2,
442 MONO_PROFILER_MONITOR_FAIL = 3,
443 } MonoProfilerMonitorEvent;
446 MONO_PROFILER_GC_HANDLE_CREATED = 0,
447 MONO_PROFILER_GC_HANDLE_DESTROYED = 1,
451 MONO_PROFILER_HEAPSHOT_NONE = 0,
452 MONO_PROFILER_HEAPSHOT_MAJOR = 1,
453 MONO_PROFILER_HEAPSHOT_ON_DEMAND = 2,
454 MONO_PROFILER_HEAPSHOT_X_GC = 3,
455 MONO_PROFILER_HEAPSHOT_X_MS = 4,
456 } MonoProfilerHeapshotMode;
458 // If you alter MAX_FRAMES, you may need to alter SAMPLE_BLOCK_SIZE too.
459 #define MAX_FRAMES 32
461 //The following flags control emitting individual events
462 #define PROFLOG_EXCEPTION_EVENTS (1 << 0)
463 #define PROFLOG_MONITOR_EVENTS (1 << 1)
464 #define PROFLOG_GC_EVENTS (1 << 2)
465 #define PROFLOG_GC_ALLOCATION_EVENTS (1 << 3)
466 #define PROFLOG_GC_MOVE_EVENTS (1 << 4)
467 #define PROFLOG_GC_ROOT_EVENTS (1 << 5)
468 #define PROFLOG_GC_HANDLE_EVENTS (1 << 6)
469 #define PROFLOG_FINALIZATION_EVENTS (1 << 7)
470 #define PROFLOG_COUNTER_EVENTS (1 << 8)
471 #define PROFLOG_SAMPLE_EVENTS (1 << 9)
472 #define PROFLOG_JIT_EVENTS (1 << 10)
474 #define PROFLOG_ALLOC_ALIAS (PROFLOG_GC_EVENTS | PROFLOG_GC_ALLOCATION_EVENTS | PROFLOG_GC_MOVE_EVENTS)
475 #define PROFLOG_HEAPSHOT_ALIAS (PROFLOG_GC_EVENTS | PROFLOG_GC_ROOT_EVENTS)
476 #define PROFLOG_LEGACY_ALIAS (PROFLOG_EXCEPTION_EVENTS | PROFLOG_MONITOR_EVENTS | PROFLOG_GC_EVENTS | PROFLOG_GC_MOVE_EVENTS | PROFLOG_GC_ROOT_EVENTS | PROFLOG_GC_HANDLE_EVENTS | PROFLOG_FINALIZATION_EVENTS | PROFLOG_COUNTER_EVENTS)
479 //Events explicitly enabled
482 //Events explicitly disabled
485 // Actual mask the profiler should use. Can be changed at runtime.
488 // Whether to do method prologue/epilogue instrumentation. Only used at startup.
489 gboolean enter_leave;
491 // Whether to collect code coverage by instrumenting basic blocks.
492 gboolean collect_coverage;
494 //Emit a report at the end of execution
497 //Enable profiler internal debugging
500 //Where to compress the output file
503 // Heapshot mode (every major, on demand, XXgc, XXms). Can be changed at runtime.
504 MonoProfilerHeapshotMode hs_mode;
506 // Heapshot frequency in milliseconds (for MONO_HEAPSHOT_X_MS). Can be changed at runtime.
507 unsigned int hs_freq_ms;
509 // Heapshot frequency in number of collections (for MONO_HEAPSHOT_X_GC). Can be changed at runtime.
510 unsigned int hs_freq_gc;
512 // Sample frequency in Hertz. Only used at startup.
515 // Maximum number of frames to collect. Can be changed at runtime.
518 // Max depth to record enter/leave events. Can be changed at runtime.
521 //Name of the generated mlpd file
522 const char *output_filename;
524 //Filter files used by the code coverage mode
525 GPtrArray *cov_filter_files;
527 // Port to listen for profiling commands (e.g. "heapshot" for on-demand heapshot).
530 // Maximum number of SampleHit structures. We'll drop samples if this number is not sufficient.
531 int max_allocated_sample_hits;
533 // Sample mode. Only used at startup.
534 MonoProfilerSampleMode sampling_mode;
537 void proflog_parse_args (ProfilerConfig *config, const char *desc);
539 #endif /* __MONO_PROFLOG_H__ */