[profiler] Process requests in enter/leave callbacks regardless of call depth.
[mono.git] / mono / profiler / proflog.c
1 /*
2  * proflog.c: mono log profiler
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Alex Rønne Petersen (alexrp@xamarin.com)
7  *
8  * Copyright 2010 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12
13 #include <config.h>
14 #include "../mini/jit.h"
15 #include "../metadata/metadata-internals.h"
16 #include <mono/metadata/profiler.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/mono-gc.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/mono-perfcounters.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/tokentype.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/utils/atomic.h>
26 #include <mono/utils/mono-membar.h>
27 #include <mono/utils/mono-mmap.h>
28 #include <mono/utils/mono-counters.h>
29 #include <mono/utils/mono-os-mutex.h>
30 #include <mono/utils/mono-os-semaphore.h>
31 #include <mono/utils/mono-conc-hashtable.h>
32 #include <mono/utils/mono-linked-list-set.h>
33 #include <mono/utils/lock-free-alloc.h>
34 #include <mono/utils/lock-free-queue.h>
35 #include <mono/utils/hazard-pointer.h>
36 #include <mono/utils/mono-threads.h>
37 #include <mono/utils/mono-threads-api.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41 #include <glib.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 #ifdef HAVE_SCHED_GETAFFINITY
46 #include <sched.h>
47 #endif
48 #include <fcntl.h>
49 #include <errno.h>
50 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
51 #define DISABLE_HELPER_THREAD 1
52 #endif
53
54 #ifndef _GNU_SOURCE
55 #define _GNU_SOURCE
56 #endif
57 #ifdef HAVE_DLFCN_H
58 #include <dlfcn.h>
59 #endif
60 #ifdef HAVE_EXECINFO_H
61 #include <execinfo.h>
62 #endif
63 #ifdef HAVE_LINK_H
64 #include <link.h>
65 #endif
66
67 #ifndef DISABLE_HELPER_THREAD
68 #include <sys/types.h>
69 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <sys/select.h>
72 #endif
73
74 #ifdef HOST_WIN32
75 #include <windows.h>
76 #else
77 #include <pthread.h>
78 #endif
79
80 #ifdef HAVE_SYS_STAT_H
81 #include <sys/stat.h>
82 #endif
83
84 #include "utils.c"
85 #include "proflog.h"
86
87 #if defined (HAVE_SYS_ZLIB)
88 #include <zlib.h>
89 #endif
90
91 #if defined(__linux__)
92
93 #include <unistd.h>
94 #include <sys/syscall.h>
95
96 #ifdef ENABLE_PERF_EVENTS
97 #include <linux/perf_event.h>
98
99 #define USE_PERF_EVENTS 1
100
101 static int read_perf_mmap (MonoProfiler* prof, int cpu);
102 #endif
103
104 #endif
105
106 #define BUFFER_SIZE (4096 * 16)
107
108 /* Worst-case size in bytes of a 64-bit value encoded with LEB128. */
109 #define LEB128_SIZE 10
110 /* Size in bytes of the event ID prefix. */
111 #define EVENT_SIZE 1
112
113 static int nocalls = 0;
114 static int notraces = 0;
115 static int use_zip = 0;
116 static int do_report = 0;
117 static int do_heap_shot = 0;
118 static int max_call_depth = 100;
119 static volatile int runtime_inited = 0;
120 static int need_helper_thread = 0;
121 static int command_port = 0;
122 static int heapshot_requested = 0;
123 static int sample_type = 0;
124 static int sample_freq = 0;
125 static int do_mono_sample = 0;
126 static int in_shutdown = 0;
127 static int do_debug = 0;
128 static int do_counters = 0;
129 static int do_coverage = 0;
130 static gboolean debug_coverage = FALSE;
131 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
132 static int max_allocated_sample_hits;
133
134 static gint32 sample_hits;
135 static gint32 sample_flushes;
136 static gint32 sample_allocations;
137 static gint32 buffer_allocations;
138 static gint32 thread_starts;
139 static gint32 thread_ends;
140 static gint32 domain_loads;
141 static gint32 domain_unloads;
142 static gint32 context_loads;
143 static gint32 context_unloads;
144 static gint32 assembly_loads;
145 static gint32 assembly_unloads;
146 static gint32 image_loads;
147 static gint32 image_unloads;
148 static gint32 class_loads;
149 static gint32 class_unloads;
150
151 static MonoLinkedListSet profiler_thread_list;
152
153 /*
154  * file format:
155  * [header] [buffer]*
156  *
157  * The file is composed by a header followed by 0 or more buffers.
158  * Each buffer contains events that happened on a thread: for a given thread
159  * buffers that appear later in the file are guaranteed to contain events
160  * that happened later in time. Buffers from separate threads could be interleaved,
161  * though.
162  * Buffers are not required to be aligned.
163  *
164  * header format:
165  * [id: 4 bytes] constant value: LOG_HEADER_ID
166  * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
167  * [format: 1 byte] version of the data format for the rest of the file
168  * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
169  * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
170  * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
171  * [flags: 4 bytes] file format flags, should be 0 for now
172  * [pid: 4 bytes] pid of the profiled process
173  * [port: 2 bytes] tcp port for server if != 0
174  * [sysid: 2 bytes] operating system and architecture identifier
175  *
176  * The multiple byte integers are in little-endian format.
177  *
178  * buffer format:
179  * [buffer header] [event]*
180  * Buffers have a fixed-size header followed by 0 or more bytes of event data.
181  * Timing information and other values in the event data are usually stored
182  * as uleb128 or sleb128 integers. To save space, as noted for each item below,
183  * some data is represented as a difference between the actual value and
184  * either the last value of the same type (like for timing information) or
185  * as the difference from a value stored in a buffer header.
186  *
187  * For timing information the data is stored as uleb128, since timing
188  * increases in a monotonic way in each thread: the value is the number of
189  * nanoseconds to add to the last seen timing data in a buffer. The first value
190  * in a buffer will be calculated from the time_base field in the buffer head.
191  *
192  * Object or heap sizes are stored as uleb128.
193  * Pointer differences are stored as sleb128, instead.
194  *
195  * If an unexpected value is found, the rest of the buffer should be ignored,
196  * as generally the later values need the former to be interpreted correctly.
197  *
198  * buffer header format:
199  * [bufid: 4 bytes] constant value: BUF_ID
200  * [len: 4 bytes] size of the data following the buffer header
201  * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
202  * [ptr_base: 8 bytes] base value for pointers
203  * [obj_base: 8 bytes] base value for object addresses
204  * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
205  * [method_base: 8 bytes] base value for MonoMethod pointers
206  *
207  * event format:
208  * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
209  * The data that follows depends on type and the extended info.
210  * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
211  * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
212  * The extended info bits are interpreted based on type, see
213  * each individual event description below.
214  * strings are represented as a 0-terminated utf8 sequence.
215  *
216  * backtrace format:
217  * [flags: uleb128] must be 0
218  * [num: uleb128] number of frames following
219  * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
220  *
221  * type alloc format:
222  * type: TYPE_ALLOC
223  * exinfo: flags: TYPE_ALLOC_BT
224  * [time diff: uleb128] nanoseconds since last timing
225  * [ptr: sleb128] class as a byte difference from ptr_base
226  * [obj: sleb128] object address as a byte difference from obj_base
227  * [size: uleb128] size of the object in the heap
228  * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
229  *
230  * type GC format:
231  * type: TYPE_GC
232  * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED[_BT],
233  * TYPE_GC_HANDLE_DESTROYED[_BT]
234  * [time diff: uleb128] nanoseconds since last timing
235  * if exinfo == TYPE_GC_RESIZE
236  *      [heap_size: uleb128] new heap size
237  * if exinfo == TYPE_GC_EVENT
238  *      [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
239  *      [generation: uleb128] GC generation event refers to
240  * if exinfo == TYPE_GC_MOVE
241  *      [num_objects: uleb128] number of object moves that follow
242  *      [objaddr: sleb128]+ num_objects object pointer differences from obj_base
243  *      num is always an even number: the even items are the old
244  *      addresses, the odd numbers are the respective new object addresses
245  * if exinfo == TYPE_GC_HANDLE_CREATED[_BT]
246  *      [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
247  *      upper bits reserved as flags
248  *      [handle: uleb128] GC handle value
249  *      [objaddr: sleb128] object pointer differences from obj_base
250  *      If exinfo == TYPE_GC_HANDLE_CREATED_BT, a backtrace follows.
251  * if exinfo == TYPE_GC_HANDLE_DESTROYED[_BT]
252  *      [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
253  *      upper bits reserved as flags
254  *      [handle: uleb128] GC handle value
255  *      If exinfo == TYPE_GC_HANDLE_DESTROYED_BT, a backtrace follows.
256  *
257  * type metadata format:
258  * type: TYPE_METADATA
259  * exinfo: one of: TYPE_END_LOAD, TYPE_END_UNLOAD (optional for TYPE_THREAD and TYPE_DOMAIN)
260  * [time diff: uleb128] nanoseconds since last timing
261  * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
262  * TYPE_THREAD, TYPE_CONTEXT
263  * [pointer: sleb128] pointer of the metadata type depending on mtype
264  * if mtype == TYPE_CLASS
265  *      [image: sleb128] MonoImage* as a pointer difference from ptr_base
266  *      [flags: uleb128] must be 0
267  *      [name: string] full class name
268  * if mtype == TYPE_IMAGE
269  *      [flags: uleb128] must be 0
270  *      [name: string] image file name
271  * if mtype == TYPE_ASSEMBLY
272  *      [flags: uleb128] must be 0
273  *      [name: string] assembly name
274  * if mtype == TYPE_DOMAIN
275  *      [flags: uleb128] must be 0
276  * if mtype == TYPE_DOMAIN && exinfo == 0
277  *      [name: string] domain friendly name
278  * if mtype == TYPE_CONTEXT
279  *      [flags: uleb128] must be 0
280  *      [domain: sleb128] domain id as pointer
281  * if mtype == TYPE_THREAD && (format_version < 11 || (format_version > 10 && exinfo == 0))
282  *      [flags: uleb128] must be 0
283  *      [name: string] thread name
284  *
285  * type method format:
286  * type: TYPE_METHOD
287  * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
288  * [time diff: uleb128] nanoseconds since last timing
289  * [method: sleb128] MonoMethod* as a pointer difference from the last such
290  * pointer or the buffer method_base
291  * if exinfo == TYPE_JIT
292  *      [code address: sleb128] pointer to the native code as a diff from ptr_base
293  *      [code size: uleb128] size of the generated code
294  *      [name: string] full method name
295  *
296  * type runtime format:
297  * type: TYPE_RUNTIME
298  * exinfo: one of: TYPE_JITHELPER
299  * [time diff: uleb128] nanoseconds since last timing
300  * if exinfo == TYPE_JITHELPER
301  *      [type: uleb128] MonoProfilerCodeBufferType enum value
302  *      [buffer address: sleb128] pointer to the native code as a diff from ptr_base
303  *      [buffer size: uleb128] size of the generated code
304  *      if type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
305  *              [name: string] buffer description name
306  *
307  * type monitor format:
308  * type: TYPE_MONITOR
309  * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
310  * [time diff: uleb128] nanoseconds since last timing
311  * [object: sleb128] the lock object as a difference from obj_base
312  * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
313  *      If the TYPE_MONITOR_BT flag is set, a backtrace follows.
314  *
315  * type heap format
316  * type: TYPE_HEAP
317  * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
318  * if exinfo == TYPE_HEAP_START
319  *      [time diff: uleb128] nanoseconds since last timing
320  * if exinfo == TYPE_HEAP_END
321  *      [time diff: uleb128] nanoseconds since last timing
322  * if exinfo == TYPE_HEAP_OBJECT
323  *      [object: sleb128] the object as a difference from obj_base
324  *      [class: sleb128] the object MonoClass* as a difference from ptr_base
325  *      [size: uleb128] size of the object on the heap
326  *      [num_refs: uleb128] number of object references
327  *      if (format version > 1) each referenced objref is preceded by a
328  *      uleb128 encoded offset: the first offset is from the object address
329  *      and each next offset is relative to the previous one
330  *      [objrefs: sleb128]+ object referenced as a difference from obj_base
331  *      The same object can appear multiple times, but only the first time
332  *      with size != 0: in the other cases this data will only be used to
333  *      provide additional referenced objects.
334  * if exinfo == TYPE_HEAP_ROOT
335  *      [num_roots: uleb128] number of root references
336  *      [num_gc: uleb128] number of major gcs
337  *      [object: sleb128] the object as a difference from obj_base
338  *      [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
339  *      [extra_info: uleb128] the extra_info value
340  *      object, root_type and extra_info are repeated num_roots times
341  *
342  * type sample format
343  * type: TYPE_SAMPLE
344  * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN, TYPE_SAMPLE_COUNTERS_DESC, TYPE_SAMPLE_COUNTERS
345  * if exinfo == TYPE_SAMPLE_HIT
346  *      [sample_type: uleb128] type of sample (SAMPLE_*)
347  *      [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
348  *      if (format_version > 10)
349  *              [thread: sleb128] thread id as difference from ptr_base
350  *      [count: uleb128] number of following instruction addresses
351  *      [ip: sleb128]* instruction pointer as difference from ptr_base
352  *      if (format_version > 5)
353  *              [mbt_count: uleb128] number of managed backtrace info triplets (method + IL offset + native offset)
354  *              [method: sleb128]* MonoMethod* as a pointer difference from the last such
355  *              pointer or the buffer method_base (the first such method can be also indentified by ip, but this is not neccessarily true)
356  *              [il_offset: sleb128]* IL offset inside method where the hit occurred
357  *              [native_offset: sleb128]* native offset inside method where the hit occurred
358  * if exinfo == TYPE_SAMPLE_USYM
359  *      [address: sleb128] symbol address as a difference from ptr_base
360  *      [size: uleb128] symbol size (may be 0 if unknown)
361  *      [name: string] symbol name
362  * if exinfo == TYPE_SAMPLE_UBIN
363  *      [time diff: uleb128] nanoseconds since last timing
364  *      [address: sleb128] address where binary has been loaded
365  *      [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
366  *      [size: uleb128] memory size
367  *      [name: string] binary name
368  * if exinfo == TYPE_SAMPLE_COUNTERS_DESC
369  *      [len: uleb128] number of counters
370  *      for i = 0 to len
371  *              [section: uleb128] section of counter
372  *              if section == MONO_COUNTER_PERFCOUNTERS:
373  *                      [section_name: string] section name of counter
374  *              [name: string] name of counter
375  *              [type: uleb128] type of counter
376  *              [unit: uleb128] unit of counter
377  *              [variance: uleb128] variance of counter
378  *              [index: uleb128] unique index of counter
379  * if exinfo == TYPE_SAMPLE_COUNTERS
380  *      [timestamp: uleb128] sampling timestamp
381  *      while true:
382  *              [index: uleb128] unique index of counter
383  *              if index == 0:
384  *                      break
385  *              [type: uleb128] type of counter value
386  *              if type == string:
387  *                      if value == null:
388  *                              [0: uleb128] 0 -> value is null
389  *                      else:
390  *                              [1: uleb128] 1 -> value is not null
391  *                              [value: string] counter value
392  *              else:
393  *                      [value: uleb128/sleb128/double] counter value, can be sleb128, uleb128 or double (determined by using type)
394  *
395  * type coverage format
396  * type: TYPE_COVERAGE
397  * exinfo: one of TYPE_COVERAGE_METHOD, TYPE_COVERAGE_STATEMENT, TYPE_COVERAGE_ASSEMBLY, TYPE_COVERAGE_CLASS
398  * if exinfo == TYPE_COVERAGE_METHOD
399  *  [assembly: string] name of assembly
400  *  [class: string] name of the class
401  *  [name: string] name of the method
402  *  [signature: string] the signature of the method
403  *  [filename: string] the file path of the file that contains this method
404  *  [token: uleb128] the method token
405  *  [method_id: uleb128] an ID for this data to associate with the buffers of TYPE_COVERAGE_STATEMENTS
406  *  [len: uleb128] the number of TYPE_COVERAGE_BUFFERS associated with this method
407  * if exinfo == TYPE_COVERAGE_STATEMENTS
408  *  [method_id: uleb128] an the TYPE_COVERAGE_METHOD buffer to associate this with
409  *  [offset: uleb128] the il offset relative to the previous offset
410  *  [counter: uleb128] the counter for this instruction
411  *  [line: uleb128] the line of filename containing this instruction
412  *  [column: uleb128] the column containing this instruction
413  * if exinfo == TYPE_COVERAGE_ASSEMBLY
414  *  [name: string] assembly name
415  *  [guid: string] assembly GUID
416  *  [filename: string] assembly filename
417  *  [number_of_methods: uleb128] the number of methods in this assembly
418  *  [fully_covered: uleb128] the number of fully covered methods
419  *  [partially_covered: uleb128] the number of partially covered methods
420  *    currently partially_covered will always be 0, and fully_covered is the
421  *    number of methods that are fully and partially covered.
422  * if exinfo == TYPE_COVERAGE_CLASS
423  *  [name: string] assembly name
424  *  [class: string] class name
425  *  [number_of_methods: uleb128] the number of methods in this class
426  *  [fully_covered: uleb128] the number of fully covered methods
427  *  [partially_covered: uleb128] the number of partially covered methods
428  *    currently partially_covered will always be 0, and fully_covered is the
429  *    number of methods that are fully and partially covered.
430  */
431
432 /*
433  * Format oddities that we ought to fix:
434  *
435  * - Methods written in emit_bt () should be based on the buffer's base
436  *   method instead of the base pointer.
437  * - The TYPE_SAMPLE_HIT event contains (currently) pointless data like
438  *   always-one unmanaged frame count and always-zero IL offsets.
439  *
440  * These are mostly small things and are not worth a format change by
441  * themselves. They should be done when some other major change has to
442  * be done to the format.
443  */
444
445 // Pending data to be written to the log, for a single thread.
446 // Threads periodically flush their own LogBuffers by calling safe_send
447 typedef struct _LogBuffer LogBuffer;
448 struct _LogBuffer {
449         // Next (older) LogBuffer in processing queue
450         LogBuffer *next;
451
452         uint64_t time_base;
453         uint64_t last_time;
454         uintptr_t ptr_base;
455         uintptr_t method_base;
456         uintptr_t last_method;
457         uintptr_t obj_base;
458         uintptr_t thread_id;
459
460         // Bytes allocated for this LogBuffer
461         int size;
462
463         // Start of currently unused space in buffer
464         unsigned char* cursor;
465
466         // Pointer to start-of-structure-plus-size (for convenience)
467         unsigned char* buf_end;
468
469         // Start of data in buffer. Contents follow "buffer format" described above.
470         unsigned char buf [1];
471 };
472
473 typedef struct {
474         MonoLinkedListSetNode node;
475
476         // The current log buffer for this thread.
477         LogBuffer *buffer;
478
479         // Methods referenced by events in `buffer`, see `MethodInfo`.
480         GPtrArray *methods;
481
482         // Current call depth for enter/leave events.
483         int call_depth;
484
485         // Indicates whether this thread is currently writing to its `buffer`.
486         int busy;
487 } MonoProfilerThread;
488
489 static inline void
490 ign_res (int G_GNUC_UNUSED unused, ...)
491 {
492 }
493
494 /*
495  * These macros create a scope to avoid leaking the buffer returned
496  * from ensure_logbuf () as it may have been invalidated by a GC
497  * thread during STW. If you called init_thread () with add_to_lls =
498  * FALSE, then don't use these macros.
499  */
500
501 #define ENTER_LOG \
502         do { \
503                 buffer_lock (); \
504                 g_assert (!PROF_TLS_GET ()->busy++ && "Why are we trying to write a new event while already writing one?")
505
506 #define EXIT_LOG \
507                 PROF_TLS_GET ()->busy--; \
508                 buffer_unlock (); \
509         } while (0)
510
511 static volatile gint32 buffer_rwlock_count;
512 static volatile gpointer buffer_rwlock_exclusive;
513
514 // Can be used recursively.
515 static void
516 buffer_lock (void)
517 {
518         /*
519          * If the thread holding the exclusive lock tries to modify the
520          * reader count, just make it a no-op. This way, we also avoid
521          * invoking the GC safe point macros below, which could break if
522          * done from a thread that is currently the initiator of STW.
523          *
524          * In other words, we rely on the fact that the GC thread takes
525          * the exclusive lock in the gc_event () callback when the world
526          * is about to stop.
527          */
528         if (InterlockedReadPointer (&buffer_rwlock_exclusive) != (gpointer) thread_id ()) {
529                 MONO_ENTER_GC_SAFE;
530
531                 while (InterlockedReadPointer (&buffer_rwlock_exclusive))
532                         mono_thread_info_yield ();
533
534                 InterlockedIncrement (&buffer_rwlock_count);
535
536                 MONO_EXIT_GC_SAFE;
537         }
538
539         mono_memory_barrier ();
540 }
541
542 static void
543 buffer_unlock (void)
544 {
545         mono_memory_barrier ();
546
547         // See the comment in buffer_lock ().
548         if (InterlockedReadPointer (&buffer_rwlock_exclusive) == (gpointer) thread_id ())
549                 return;
550
551         g_assert (InterlockedRead (&buffer_rwlock_count) && "Why are we trying to decrement a zero reader count?");
552
553         InterlockedDecrement (&buffer_rwlock_count);
554 }
555
556 // Cannot be used recursively.
557 static void
558 buffer_lock_excl (void)
559 {
560         gpointer tid = (gpointer) thread_id ();
561
562         g_assert (InterlockedReadPointer (&buffer_rwlock_exclusive) != tid && "Why are we taking the exclusive lock twice?");
563
564         MONO_ENTER_GC_SAFE;
565
566         while (InterlockedCompareExchangePointer (&buffer_rwlock_exclusive, tid, 0))
567                 mono_thread_info_yield ();
568
569         while (InterlockedRead (&buffer_rwlock_count))
570                 mono_thread_info_yield ();
571
572         MONO_EXIT_GC_SAFE;
573
574         mono_memory_barrier ();
575 }
576
577 static void
578 buffer_unlock_excl (void)
579 {
580         mono_memory_barrier ();
581
582         g_assert (InterlockedReadPointer (&buffer_rwlock_exclusive) && "Why is the exclusive lock not held?");
583         g_assert (InterlockedReadPointer (&buffer_rwlock_exclusive) == (gpointer) thread_id () && "Why does another thread hold the exclusive lock?");
584         g_assert (!InterlockedRead (&buffer_rwlock_count) && "Why are there readers when the exclusive lock is held?");
585
586         InterlockedWritePointer (&buffer_rwlock_exclusive, NULL);
587 }
588
589 typedef struct _BinaryObject BinaryObject;
590 struct _BinaryObject {
591         BinaryObject *next;
592         void *addr;
593         char *name;
594 };
595
596 struct _MonoProfiler {
597         FILE* file;
598 #if defined (HAVE_SYS_ZLIB)
599         gzFile gzfile;
600 #endif
601         uint64_t startup_time;
602         int pipe_output;
603         int last_gc_gen_started;
604         int command_port;
605         int server_socket;
606         int pipes [2];
607 #ifndef HOST_WIN32
608         pthread_t helper_thread;
609         pthread_t writer_thread;
610         pthread_t dumper_thread;
611 #endif
612         volatile gint32 run_writer_thread;
613         MonoLockFreeAllocSizeClass writer_entry_size_class;
614         MonoLockFreeAllocator writer_entry_allocator;
615         MonoLockFreeQueue writer_queue;
616         MonoSemType writer_queue_sem;
617         MonoConcurrentHashTable *method_table;
618         mono_mutex_t method_table_mutex;
619         volatile gint32 run_dumper_thread;
620         MonoLockFreeQueue dumper_queue;
621         MonoSemType dumper_queue_sem;
622         MonoLockFreeAllocSizeClass sample_size_class;
623         MonoLockFreeAllocator sample_allocator;
624         MonoLockFreeQueue sample_reuse_queue;
625         BinaryObject *binary_objects;
626         GPtrArray *coverage_filters;
627 };
628
629 typedef struct {
630         MonoLockFreeQueueNode node;
631         GPtrArray *methods;
632         LogBuffer *buffer;
633 } WriterQueueEntry;
634
635 #define WRITER_ENTRY_BLOCK_SIZE (mono_pagesize ())
636
637 typedef struct {
638         MonoMethod *method;
639         MonoJitInfo *ji;
640         uint64_t time;
641 } MethodInfo;
642
643 #ifdef HOST_WIN32
644
645 #define PROF_TLS_SET(VAL) (TlsSetValue (profiler_tls, (VAL)))
646 #define PROF_TLS_GET() ((MonoProfilerThread *) TlsGetValue (profiler_tls))
647 #define PROF_TLS_INIT() (profiler_tls = TlsAlloc ())
648 #define PROF_TLS_FREE() (TlsFree (profiler_tls))
649
650 static DWORD profiler_tls;
651
652 #elif HAVE_KW_THREAD
653
654 #define PROF_TLS_SET(VAL) (profiler_tls = (VAL))
655 #define PROF_TLS_GET() (profiler_tls)
656 #define PROF_TLS_INIT()
657 #define PROF_TLS_FREE()
658
659 static __thread MonoProfilerThread *profiler_tls;
660
661 #else
662
663 #define PROF_TLS_SET(VAL) (pthread_setspecific (profiler_tls, (VAL)))
664 #define PROF_TLS_GET() ((MonoProfilerThread *) pthread_getspecific (profiler_tls))
665 #define PROF_TLS_INIT() (pthread_key_create (&profiler_tls, NULL))
666 #define PROF_TLS_FREE() (pthread_key_delete (&profiler_tls))
667
668 static pthread_key_t profiler_tls;
669
670 #endif
671
672 static char*
673 pstrdup (const char *s)
674 {
675         int len = strlen (s) + 1;
676         char *p = (char *)malloc (len);
677         memcpy (p, s, len);
678         return p;
679 }
680
681 static LogBuffer*
682 create_buffer (void)
683 {
684         LogBuffer* buf = (LogBuffer *)alloc_buffer (BUFFER_SIZE);
685
686         InterlockedIncrement (&buffer_allocations);
687
688         buf->size = BUFFER_SIZE;
689         buf->time_base = current_time ();
690         buf->last_time = buf->time_base;
691         buf->buf_end = (unsigned char*)buf + buf->size;
692         buf->cursor = buf->buf;
693         return buf;
694 }
695
696 static void
697 init_buffer_state (MonoProfilerThread *thread)
698 {
699         thread->buffer = create_buffer ();
700         thread->methods = NULL;
701 }
702
703 static void
704 clear_hazard_pointers (MonoThreadHazardPointers *hp)
705 {
706         mono_hazard_pointer_clear (hp, 0);
707         mono_hazard_pointer_clear (hp, 1);
708         mono_hazard_pointer_clear (hp, 2);
709 }
710
711 static MonoProfilerThread *
712 init_thread (gboolean add_to_lls)
713 {
714         MonoProfilerThread *thread = PROF_TLS_GET ();
715
716         /*
717          * Sometimes we may try to initialize a thread twice. One example is the
718          * main thread: We initialize it when setting up the profiler, but we will
719          * also get a thread_start () callback for it. Another example is when
720          * attaching new threads to the runtime: We may get a gc_alloc () callback
721          * for that thread's thread object (where we initialize it), soon followed
722          * by a thread_start () callback.
723          *
724          * These cases are harmless anyhow. Just return if we've already done the
725          * initialization work.
726          */
727         if (thread)
728                 return thread;
729
730         thread = malloc (sizeof (MonoProfilerThread));
731         thread->node.key = thread_id ();
732         thread->call_depth = 0;
733         thread->busy = 0;
734
735         init_buffer_state (thread);
736
737         /*
738          * Some internal profiler threads don't need to be cleaned up
739          * by the main thread on shutdown.
740          */
741         if (add_to_lls) {
742                 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
743                 g_assert (mono_lls_insert (&profiler_thread_list, hp, &thread->node) && "Why can't we insert the thread in the LLS?");
744                 clear_hazard_pointers (hp);
745         }
746
747         PROF_TLS_SET (thread);
748
749         return thread;
750 }
751
752 // Only valid if init_thread () was called with add_to_lls = FALSE.
753 static void
754 deinit_thread (MonoProfilerThread *thread)
755 {
756         free (thread);
757         PROF_TLS_SET (NULL);
758 }
759
760 static LogBuffer *
761 ensure_logbuf_inner (LogBuffer *old, int bytes)
762 {
763         if (old && old->cursor + bytes + 100 < old->buf_end)
764                 return old;
765
766         LogBuffer *new_ = create_buffer ();
767         new_->next = old;
768
769         return new_;
770 }
771
772 // Only valid if init_thread () was called with add_to_lls = FALSE.
773 static LogBuffer *
774 ensure_logbuf_unsafe (int bytes)
775 {
776         MonoProfilerThread *thread = PROF_TLS_GET ();
777         LogBuffer *old = thread->buffer;
778         LogBuffer *new_ = ensure_logbuf_inner (old, bytes);
779
780         if (new_ == old)
781                 return old; // Still enough space.
782
783         thread->buffer = new_;
784
785         return new_;
786 }
787
788 /*
789  * Any calls to this function should be wrapped in the ENTER_LOG and
790  * EXIT_LOG macros to prevent the returned pointer from leaking
791  * outside of the critical region created by the calls to buffer_lock ()
792  * and buffer_unlock () that those macros insert. If the pointer leaks,
793  * it can and will lead to crashes as the GC or helper thread may
794  * invalidate the pointer at any time.
795  *
796  * Note: If you're calling from a thread that called init_thread () with
797  * add_to_lls = FALSE, you should use ensure_logbuf_unsafe () and omit
798  * the macros.
799  */
800 static LogBuffer*
801 ensure_logbuf (int bytes)
802 {
803         g_assert (PROF_TLS_GET ()->busy && "Why are we trying to expand our buffer without the busy flag set?");
804
805         return ensure_logbuf_unsafe (bytes);
806 }
807
808 static void
809 emit_byte (LogBuffer *logbuffer, int value)
810 {
811         logbuffer->cursor [0] = value;
812         logbuffer->cursor++;
813         assert (logbuffer->cursor <= logbuffer->buf_end);
814 }
815
816 static void
817 emit_value (LogBuffer *logbuffer, int value)
818 {
819         encode_uleb128 (value, logbuffer->cursor, &logbuffer->cursor);
820         assert (logbuffer->cursor <= logbuffer->buf_end);
821 }
822
823 static void
824 emit_time (LogBuffer *logbuffer, uint64_t value)
825 {
826         uint64_t tdiff = value - logbuffer->last_time;
827         //if (value < logbuffer->last_time)
828         //      printf ("time went backwards\n");
829         //if (tdiff > 1000000)
830         //      printf ("large time offset: %llu\n", tdiff);
831         encode_uleb128 (tdiff, logbuffer->cursor, &logbuffer->cursor);
832         /*if (tdiff != decode_uleb128 (p, &p))
833                 printf ("incorrect encoding: %llu\n", tdiff);*/
834         logbuffer->last_time = value;
835         assert (logbuffer->cursor <= logbuffer->buf_end);
836 }
837
838 static void
839 emit_svalue (LogBuffer *logbuffer, int64_t value)
840 {
841         encode_sleb128 (value, logbuffer->cursor, &logbuffer->cursor);
842         assert (logbuffer->cursor <= logbuffer->buf_end);
843 }
844
845 static void
846 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
847 {
848         encode_uleb128 (value, logbuffer->cursor, &logbuffer->cursor);
849         assert (logbuffer->cursor <= logbuffer->buf_end);
850 }
851
852 static void
853 emit_ptr (LogBuffer *logbuffer, void *ptr)
854 {
855         if (!logbuffer->ptr_base)
856                 logbuffer->ptr_base = (uintptr_t)ptr;
857         emit_svalue (logbuffer, (intptr_t)ptr - logbuffer->ptr_base);
858         assert (logbuffer->cursor <= logbuffer->buf_end);
859 }
860
861 static void
862 emit_method_inner (LogBuffer *logbuffer, void *method)
863 {
864         if (!logbuffer->method_base) {
865                 logbuffer->method_base = (intptr_t)method;
866                 logbuffer->last_method = (intptr_t)method;
867         }
868         encode_sleb128 ((intptr_t)((char*)method - (char*)logbuffer->last_method), logbuffer->cursor, &logbuffer->cursor);
869         logbuffer->last_method = (intptr_t)method;
870         assert (logbuffer->cursor <= logbuffer->buf_end);
871 }
872
873 /*
874 typedef struct {
875         MonoMethod *method;
876         MonoJitInfo *found;
877 } MethodSearch;
878
879 static void
880 find_method (MonoDomain *domain, void *user_data)
881 {
882         MethodSearch *search = user_data;
883
884         if (search->found)
885                 return;
886
887         MonoJitInfo *ji = mono_get_jit_info_from_method (domain, search->method);
888
889         // It could be AOT'd, so we need to get it from the AOT runtime's cache.
890         if (!ji) {
891                 void *ip = mono_aot_get_method (domain, search->method);
892
893                 // Avoid a slow path in mono_jit_info_table_find ().
894                 if (ip)
895                         ji = mono_jit_info_table_find (domain, ip);
896         }
897
898         if (ji)
899                 search->found = ji;
900 }
901 */
902
903 static void
904 register_method_local (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji)
905 {
906         if (!mono_conc_hashtable_lookup (prof->method_table, method)) {
907                 /*
908                  * FIXME: In some cases, we crash while looking up JIT info for AOT'd methods.
909                  * This usually happens for static constructors. This code is disabled for now
910                  * as we don't need this info for anything critical.
911                  *
912                  * https://bugzilla.xamarin.com/show_bug.cgi?id=35171
913                  */
914                 /*
915                 if (!ji) {
916                         MethodSearch search = { method, NULL };
917
918                         mono_domain_foreach (find_method, &search);
919
920                         ji = search.found;
921                 }
922                 */
923
924                 /*
925                  * FIXME: We can't always find JIT info for a generic shared method, especially
926                  * if we obtained the MonoMethod during an async stack walk. For now, we deal
927                  * with this by giving the generic shared method name and dummy code start/size
928                  * information (i.e. zeroes).
929                  */
930                 //g_assert (ji);
931
932                 MethodInfo *info = (MethodInfo *) malloc (sizeof (MethodInfo));
933
934                 info->method = method;
935                 info->ji = ji;
936                 info->time = current_time ();
937
938                 MonoProfilerThread *thread = PROF_TLS_GET ();
939                 GPtrArray *arr = thread->methods ? thread->methods : (thread->methods = g_ptr_array_new ());
940                 g_ptr_array_add (arr, info);
941         }
942 }
943
944 static void
945 emit_method (MonoProfiler *prof, LogBuffer *logbuffer, MonoMethod *method)
946 {
947         register_method_local (prof, method, NULL);
948         emit_method_inner (logbuffer, method);
949 }
950
951 static void
952 emit_method_as_ptr (MonoProfiler *prof, LogBuffer *logbuffer, MonoMethod *method)
953 {
954         register_method_local (prof, method, NULL);
955         emit_ptr (logbuffer, method);
956 }
957
958 static void
959 emit_obj (LogBuffer *logbuffer, void *ptr)
960 {
961         if (!logbuffer->obj_base)
962                 logbuffer->obj_base = (uintptr_t)ptr >> 3;
963         emit_svalue (logbuffer, ((uintptr_t)ptr >> 3) - logbuffer->obj_base);
964         assert (logbuffer->cursor <= logbuffer->buf_end);
965 }
966
967 static void
968 emit_string (LogBuffer *logbuffer, const char *str, size_t size)
969 {
970         size_t i = 0;
971         if (str) {
972                 for (; i < size; i++) {
973                         if (str[i] == '\0')
974                                 break;
975                         emit_byte (logbuffer, str [i]);
976                 }
977         }
978         emit_byte (logbuffer, '\0');
979 }
980
981 static void
982 emit_double (LogBuffer *logbuffer, double value)
983 {
984         int i;
985         unsigned char buffer[8];
986         memcpy (buffer, &value, 8);
987 #if G_BYTE_ORDER == G_BIG_ENDIAN
988         for (i = 7; i >= 0; i--)
989 #else
990         for (i = 0; i < 8; i++)
991 #endif
992                 emit_byte (logbuffer, buffer[i]);
993 }
994
995 static char*
996 write_int16 (char *buf, int32_t value)
997 {
998         int i;
999         for (i = 0; i < 2; ++i) {
1000                 buf [i] = value;
1001                 value >>= 8;
1002         }
1003         return buf + 2;
1004 }
1005
1006 static char*
1007 write_int32 (char *buf, int32_t value)
1008 {
1009         int i;
1010         for (i = 0; i < 4; ++i) {
1011                 buf [i] = value;
1012                 value >>= 8;
1013         }
1014         return buf + 4;
1015 }
1016
1017 static char*
1018 write_int64 (char *buf, int64_t value)
1019 {
1020         int i;
1021         for (i = 0; i < 8; ++i) {
1022                 buf [i] = value;
1023                 value >>= 8;
1024         }
1025         return buf + 8;
1026 }
1027
1028 static void
1029 dump_header (MonoProfiler *profiler)
1030 {
1031         char hbuf [128];
1032         char *p = hbuf;
1033         p = write_int32 (p, LOG_HEADER_ID);
1034         *p++ = LOG_VERSION_MAJOR;
1035         *p++ = LOG_VERSION_MINOR;
1036         *p++ = LOG_DATA_VERSION;
1037         *p++ = sizeof (void*);
1038         p = write_int64 (p, ((uint64_t)time (NULL)) * 1000); /* startup time */
1039         p = write_int32 (p, get_timer_overhead ()); /* timer overhead */
1040         p = write_int32 (p, 0); /* flags */
1041         p = write_int32 (p, process_id ()); /* pid */
1042         p = write_int16 (p, profiler->command_port); /* port */
1043         p = write_int16 (p, 0); /* opsystem */
1044 #if defined (HAVE_SYS_ZLIB)
1045         if (profiler->gzfile) {
1046                 gzwrite (profiler->gzfile, hbuf, p - hbuf);
1047         } else {
1048                 fwrite (hbuf, p - hbuf, 1, profiler->file);
1049         }
1050 #else
1051         fwrite (hbuf, p - hbuf, 1, profiler->file);
1052         fflush (profiler->file);
1053 #endif
1054 }
1055
1056 static void
1057 send_buffer (MonoProfiler *prof, MonoProfilerThread *thread)
1058 {
1059         WriterQueueEntry *entry = mono_lock_free_alloc (&prof->writer_entry_allocator);
1060         entry->methods = thread->methods;
1061         entry->buffer = thread->buffer;
1062
1063         mono_lock_free_queue_node_init (&entry->node, FALSE);
1064
1065         mono_lock_free_queue_enqueue (&prof->writer_queue, &entry->node);
1066         mono_os_sem_post (&prof->writer_queue_sem);
1067 }
1068
1069 static void
1070 remove_thread (MonoProfiler *prof, MonoProfilerThread *thread, gboolean from_callback)
1071 {
1072         MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
1073
1074         if (mono_lls_remove (&profiler_thread_list, hp, &thread->node)) {
1075                 LogBuffer *buffer = thread->buffer;
1076
1077                 /*
1078                  * No need to take the buffer lock here as no other threads can
1079                  * be accessing this buffer anymore.
1080                  */
1081
1082                 if (!from_callback) {
1083                         /*
1084                          * The thread is being cleaned up by the main thread during
1085                          * shutdown. This typically happens for internal runtime
1086                          * threads. We need to synthesize a thread end event.
1087                          */
1088
1089                         buffer = ensure_logbuf_inner (buffer,
1090                                 EVENT_SIZE /* event */ +
1091                                 LEB128_SIZE /* time */ +
1092                                 EVENT_SIZE /* type */ +
1093                                 LEB128_SIZE /* tid */ +
1094                                 LEB128_SIZE /* flags */
1095                         );
1096
1097                         uint64_t now = current_time ();
1098
1099                         emit_byte (buffer, TYPE_END_UNLOAD | TYPE_METADATA);
1100                         emit_time (buffer, now);
1101                         emit_byte (buffer, TYPE_THREAD);
1102                         emit_ptr (buffer, (void *) thread->node.key);
1103                         emit_value (buffer, 0); /* flags */
1104                 }
1105
1106                 send_buffer (prof, thread);
1107
1108                 mono_thread_hazardous_try_free (thread, free);
1109         }
1110
1111         clear_hazard_pointers (hp);
1112
1113         if (from_callback)
1114                 PROF_TLS_SET (NULL);
1115 }
1116
1117 static void
1118 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
1119 {
1120         char hbuf [128];
1121         char *p = hbuf;
1122         if (buf->next)
1123                 dump_buffer (profiler, buf->next);
1124         p = write_int32 (p, BUF_ID);
1125         p = write_int32 (p, buf->cursor - buf->buf);
1126         p = write_int64 (p, buf->time_base);
1127         p = write_int64 (p, buf->ptr_base);
1128         p = write_int64 (p, buf->obj_base);
1129         p = write_int64 (p, buf->thread_id);
1130         p = write_int64 (p, buf->method_base);
1131 #if defined (HAVE_SYS_ZLIB)
1132         if (profiler->gzfile) {
1133                 gzwrite (profiler->gzfile, hbuf, p - hbuf);
1134                 gzwrite (profiler->gzfile, buf->buf, buf->cursor - buf->buf);
1135         } else {
1136 #endif
1137                 fwrite (hbuf, p - hbuf, 1, profiler->file);
1138                 fwrite (buf->buf, buf->cursor - buf->buf, 1, profiler->file);
1139                 fflush (profiler->file);
1140 #if defined (HAVE_SYS_ZLIB)
1141         }
1142 #endif
1143         free_buffer (buf, buf->size);
1144 }
1145
1146 static void
1147 dump_buffer_threadless (MonoProfiler *profiler, LogBuffer *buf)
1148 {
1149         for (LogBuffer *iter = buf; iter; iter = iter->next)
1150                 iter->thread_id = 0;
1151
1152         dump_buffer (profiler, buf);
1153 }
1154
1155 static void
1156 process_requests (MonoProfiler *profiler)
1157 {
1158         if (heapshot_requested)
1159                 mono_gc_collect (mono_gc_max_generation ());
1160 }
1161
1162 static void counters_init (MonoProfiler *profiler);
1163 static void counters_sample (MonoProfiler *profiler, uint64_t timestamp, gboolean threadless);
1164
1165 static void
1166 safe_send (MonoProfiler *profiler)
1167 {
1168         /* We need the runtime initialized so that we have threads and hazard
1169          * pointers available. Otherwise, the lock free queue will not work and
1170          * there won't be a thread to process the data.
1171          *
1172          * While the runtime isn't initialized, we just accumulate data in the
1173          * thread local buffer list.
1174          */
1175         if (!InterlockedRead (&runtime_inited))
1176                 return;
1177
1178         MonoProfilerThread *thread = PROF_TLS_GET ();
1179
1180         buffer_lock ();
1181
1182         send_buffer (profiler, thread);
1183         init_buffer_state (thread);
1184
1185         buffer_unlock ();
1186 }
1187
1188 static void
1189 send_if_needed (MonoProfiler *prof)
1190 {
1191         if (PROF_TLS_GET ()->buffer->next)
1192                 safe_send (prof);
1193 }
1194
1195 static void
1196 safe_send_threadless (MonoProfiler *prof)
1197 {
1198         LogBuffer *buf = PROF_TLS_GET ()->buffer;
1199
1200         for (LogBuffer *iter = buf; iter; iter = iter->next)
1201                 iter->thread_id = 0;
1202
1203         safe_send (prof);
1204 }
1205
1206 static int
1207 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
1208 {
1209         /* account for object alignment in the heap */
1210         size += 7;
1211         size &= ~7;
1212
1213         ENTER_LOG;
1214
1215         LogBuffer *logbuffer = ensure_logbuf (
1216                 EVENT_SIZE /* event */ +
1217                 LEB128_SIZE /* obj */ +
1218                 LEB128_SIZE /* klass */ +
1219                 LEB128_SIZE /* size */ +
1220                 LEB128_SIZE /* num */ +
1221                 num * (
1222                         LEB128_SIZE /* offset */ +
1223                         LEB128_SIZE /* ref */
1224                 )
1225         );
1226
1227         emit_byte (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
1228         emit_obj (logbuffer, obj);
1229         emit_ptr (logbuffer, klass);
1230         emit_value (logbuffer, size);
1231         emit_value (logbuffer, num);
1232
1233         uintptr_t last_offset = 0;
1234
1235         for (int i = 0; i < num; ++i) {
1236                 emit_value (logbuffer, offsets [i] - last_offset);
1237                 last_offset = offsets [i];
1238                 emit_obj (logbuffer, refs [i]);
1239         }
1240
1241         EXIT_LOG;
1242
1243         return 0;
1244 }
1245
1246 static unsigned int hs_mode_ms = 0;
1247 static unsigned int hs_mode_gc = 0;
1248 static unsigned int hs_mode_ondemand = 0;
1249 static unsigned int gc_count = 0;
1250 static uint64_t last_hs_time = 0;
1251
1252 static void
1253 heap_walk (MonoProfiler *profiler)
1254 {
1255         if (!do_heap_shot)
1256                 return;
1257
1258         gboolean do_walk = 0;
1259         uint64_t now = current_time ();
1260
1261         if (hs_mode_ms && (now - last_hs_time) / 1000000 >= hs_mode_ms)
1262                 do_walk = TRUE;
1263         else if (hs_mode_gc && (gc_count % hs_mode_gc) == 0)
1264                 do_walk = TRUE;
1265         else if (hs_mode_ondemand)
1266                 do_walk = heapshot_requested;
1267         else if (!hs_mode_ms && !hs_mode_gc && profiler->last_gc_gen_started == mono_gc_max_generation ())
1268                 do_walk = TRUE;
1269
1270         if (!do_walk)
1271                 return;
1272
1273         heapshot_requested = 0;
1274
1275         ENTER_LOG;
1276
1277         LogBuffer *logbuffer = ensure_logbuf (
1278                 EVENT_SIZE /* event */ +
1279                 LEB128_SIZE /* time */
1280         );
1281
1282         emit_byte (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
1283         emit_time (logbuffer, now);
1284
1285         EXIT_LOG;
1286
1287         mono_gc_walk_heap (0, gc_reference, NULL);
1288
1289         ENTER_LOG;
1290
1291         LogBuffer *logbuffer = ensure_logbuf (
1292                 EVENT_SIZE /* event */ +
1293                 LEB128_SIZE /* time */
1294         );
1295
1296         now = current_time ();
1297
1298         emit_byte (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
1299         emit_time (logbuffer, now);
1300
1301         EXIT_LOG;
1302
1303         last_hs_time = now;
1304 }
1305
1306 static void
1307 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation)
1308 {
1309         ENTER_LOG;
1310
1311         LogBuffer *logbuffer = ensure_logbuf (
1312                 EVENT_SIZE /* event */ +
1313                 LEB128_SIZE /* time */ +
1314                 LEB128_SIZE /* gc event */ +
1315                 LEB128_SIZE /* generation */
1316         );
1317
1318         uint64_t now = current_time ();
1319
1320         emit_byte (logbuffer, TYPE_GC_EVENT | TYPE_GC);
1321         emit_time (logbuffer, now);
1322         emit_value (logbuffer, ev);
1323         emit_value (logbuffer, generation);
1324
1325         EXIT_LOG;
1326
1327         switch (ev) {
1328         case MONO_GC_EVENT_START:
1329                 /* to deal with nested gen1 after gen0 started */
1330                 profiler->last_gc_gen_started = generation;
1331
1332                 if (generation == mono_gc_max_generation ())
1333                         gc_count++;
1334                 break;
1335         case MONO_GC_EVENT_PRE_STOP_WORLD:
1336                 /*
1337                  * Ensure that no thread can be in the middle of writing to
1338                  * a buffer when the world stops...
1339                  */
1340                 buffer_lock_excl ();
1341                 break;
1342         case MONO_GC_EVENT_POST_STOP_WORLD:
1343                 /*
1344                  * ... So that we now have a consistent view of all buffers.
1345                  * This allows us to flush them. We need to do this because
1346                  * they may contain object allocation events that need to be
1347                  * committed to the log file before any object move events
1348                  * that will be produced during this GC.
1349                  */
1350                 g_assert (InterlockedReadPointer (&buffer_rwlock_exclusive) == (gpointer) thread_id () && "Why don't we hold the exclusive lock?");
1351
1352                 MONO_LLS_FOREACH_SAFE (&profiler_thread_list, MonoProfilerThread, thread) {
1353                         send_buffer (profiler, thread);
1354                         init_buffer_state (thread);
1355                 } MONO_LLS_FOREACH_SAFE_END
1356                 break;
1357         case MONO_GC_EVENT_PRE_START_WORLD:
1358                 heap_walk (profiler);
1359                 break;
1360         case MONO_GC_EVENT_POST_START_WORLD:
1361                 /*
1362                  * Similarly, we must now make sure that any object moves
1363                  * written to the GC thread's buffer are flushed. Otherwise,
1364                  * object allocation events for certain addresses could come
1365                  * after the move events that made those addresses available.
1366                  */
1367                 safe_send (profiler);
1368
1369                 /*
1370                  * Finally, it is safe to allow other threads to write to
1371                  * their buffers again.
1372                  */
1373                 buffer_unlock_excl ();
1374                 break;
1375         default:
1376                 break;
1377         }
1378 }
1379
1380 static void
1381 gc_resize (MonoProfiler *profiler, int64_t new_size)
1382 {
1383         ENTER_LOG;
1384
1385         LogBuffer *logbuffer = ensure_logbuf (
1386                 EVENT_SIZE /* event */ +
1387                 LEB128_SIZE /* time */ +
1388                 LEB128_SIZE /* new size */
1389         );
1390
1391         uint64_t now = current_time ();
1392
1393         emit_byte (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
1394         emit_time (logbuffer, now);
1395         emit_value (logbuffer, new_size);
1396
1397         EXIT_LOG;
1398 }
1399
1400 // If you alter MAX_FRAMES, you may need to alter SAMPLE_BLOCK_SIZE too.
1401 #define MAX_FRAMES 32
1402
1403 typedef struct {
1404         int count;
1405         MonoMethod* methods [MAX_FRAMES];
1406         int32_t il_offsets [MAX_FRAMES];
1407         int32_t native_offsets [MAX_FRAMES];
1408 } FrameData;
1409
1410 static int num_frames = MAX_FRAMES;
1411
1412 static mono_bool
1413 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
1414 {
1415         FrameData *frame = (FrameData *)data;
1416         if (method && frame->count < num_frames) {
1417                 frame->il_offsets [frame->count] = il_offset;
1418                 frame->native_offsets [frame->count] = native_offset;
1419                 frame->methods [frame->count++] = method;
1420                 //printf ("In %d %s at %d (native: %d)\n", frame->count, mono_method_get_name (method), il_offset, native_offset);
1421         }
1422         return frame->count == num_frames;
1423 }
1424
1425 /*
1426  * a note about stack walks: they can cause more profiler events to fire,
1427  * so we need to make sure they don't happen after we started emitting an
1428  * event, hence the collect_bt/emit_bt split.
1429  */
1430 static void
1431 collect_bt (FrameData *data)
1432 {
1433         data->count = 0;
1434         mono_stack_walk_no_il (walk_stack, data);
1435 }
1436
1437 static void
1438 emit_bt (MonoProfiler *prof, LogBuffer *logbuffer, FrameData *data)
1439 {
1440         /* FIXME: this is actually tons of data and we should
1441          * just output it the first time and use an id the next
1442          */
1443         if (data->count > num_frames)
1444                 printf ("bad num frames: %d\n", data->count);
1445         emit_value (logbuffer, 0); /* flags */
1446         emit_value (logbuffer, data->count);
1447         //if (*p != data.count) {
1448         //      printf ("bad num frames enc at %d: %d -> %d\n", count, data.count, *p); printf ("frames end: %p->%p\n", p, logbuffer->cursor); exit(0);}
1449         while (data->count) {
1450                 emit_method_as_ptr (prof, logbuffer, data->methods [--data->count]);
1451         }
1452 }
1453
1454 static void
1455 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
1456 {
1457         init_thread (TRUE);
1458
1459         int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces) ? TYPE_ALLOC_BT : 0;
1460         FrameData data;
1461         uintptr_t len = mono_object_get_size (obj);
1462         /* account for object alignment in the heap */
1463         len += 7;
1464         len &= ~7;
1465
1466         if (do_bt)
1467                 collect_bt (&data);
1468
1469         ENTER_LOG;
1470
1471         LogBuffer *logbuffer = ensure_logbuf (
1472                 EVENT_SIZE /* event */ +
1473                 LEB128_SIZE /* time */ +
1474                 LEB128_SIZE /* klass */ +
1475                 LEB128_SIZE /* obj */ +
1476                 LEB128_SIZE /* size */ +
1477                 (do_bt ? (
1478                         LEB128_SIZE /* flags */ +
1479                         LEB128_SIZE /* count */ +
1480                         data.count * (
1481                                 LEB128_SIZE /* method */
1482                         )
1483                 ) : 0)
1484         );
1485
1486         uint64_t now = current_time ();
1487
1488         emit_byte (logbuffer, do_bt | TYPE_ALLOC);
1489         emit_time (logbuffer, now);
1490         emit_ptr (logbuffer, klass);
1491         emit_obj (logbuffer, obj);
1492         emit_value (logbuffer, len);
1493
1494         if (do_bt)
1495                 emit_bt (prof, logbuffer, &data);
1496
1497         EXIT_LOG;
1498
1499         send_if_needed (prof);
1500
1501         process_requests (prof);
1502 }
1503
1504 static void
1505 gc_moves (MonoProfiler *prof, void **objects, int num)
1506 {
1507         ENTER_LOG;
1508
1509         LogBuffer *logbuffer = ensure_logbuf (
1510                 EVENT_SIZE /* event */ +
1511                 LEB128_SIZE /* time */ +
1512                 LEB128_SIZE /* num */ +
1513                 num * (
1514                         LEB128_SIZE /* object */
1515                 )
1516         );
1517
1518         uint64_t now = current_time ();
1519
1520         emit_byte (logbuffer, TYPE_GC_MOVE | TYPE_GC);
1521         emit_time (logbuffer, now);
1522         emit_value (logbuffer, num);
1523
1524         for (int i = 0; i < num; ++i)
1525                 emit_obj (logbuffer, objects [i]);
1526
1527         EXIT_LOG;
1528 }
1529
1530 static void
1531 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
1532 {
1533         ENTER_LOG;
1534
1535         LogBuffer *logbuffer = ensure_logbuf (
1536                 EVENT_SIZE /* event */ +
1537                 LEB128_SIZE /* num */ +
1538                 LEB128_SIZE /* collections */ +
1539                 num * (
1540                         LEB128_SIZE /* object */ +
1541                         LEB128_SIZE /* root type */ +
1542                         LEB128_SIZE /* extra info */
1543                 )
1544         );
1545
1546         emit_byte (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
1547         emit_value (logbuffer, num);
1548         emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
1549
1550         for (int i = 0; i < num; ++i) {
1551                 emit_obj (logbuffer, objects [i]);
1552                 emit_value (logbuffer, root_types [i]);
1553                 emit_value (logbuffer, extra_info [i]);
1554         }
1555
1556         EXIT_LOG;
1557 }
1558
1559 static void
1560 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
1561 {
1562         int do_bt = nocalls && InterlockedRead (&runtime_inited) && !notraces;
1563         FrameData data;
1564
1565         if (do_bt)
1566                 collect_bt (&data);
1567
1568         ENTER_LOG;
1569
1570         LogBuffer *logbuffer = ensure_logbuf (
1571                 EVENT_SIZE /* event */ +
1572                 LEB128_SIZE /* time */ +
1573                 LEB128_SIZE /* type */ +
1574                 LEB128_SIZE /* handle */ +
1575                 (op == MONO_PROFILER_GC_HANDLE_CREATED ? (
1576                         LEB128_SIZE /* obj */
1577                 ) : 0) +
1578                 (do_bt ? (
1579                         LEB128_SIZE /* flags */ +
1580                         LEB128_SIZE /* count */ +
1581                         data.count * (
1582                                 LEB128_SIZE /* method */
1583                         )
1584                 ) : 0)
1585         );
1586
1587         uint64_t now = current_time ();
1588
1589         if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1590                 emit_byte (logbuffer, (do_bt ? TYPE_GC_HANDLE_CREATED_BT : TYPE_GC_HANDLE_CREATED) | TYPE_GC);
1591         else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
1592                 emit_byte (logbuffer, (do_bt ? TYPE_GC_HANDLE_DESTROYED_BT : TYPE_GC_HANDLE_DESTROYED) | TYPE_GC);
1593         else
1594                 g_assert_not_reached ();
1595
1596         emit_time (logbuffer, now);
1597         emit_value (logbuffer, type);
1598         emit_value (logbuffer, handle);
1599
1600         if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1601                 emit_obj (logbuffer, obj);
1602
1603         if (do_bt)
1604                 emit_bt (prof, logbuffer, &data);
1605
1606         EXIT_LOG;
1607
1608         process_requests (prof);
1609 }
1610
1611 static char*
1612 push_nesting (char *p, MonoClass *klass)
1613 {
1614         MonoClass *nesting;
1615         const char *name;
1616         const char *nspace;
1617         nesting = mono_class_get_nesting_type (klass);
1618         if (nesting) {
1619                 p = push_nesting (p, nesting);
1620                 *p++ = '/';
1621                 *p = 0;
1622         }
1623         name = mono_class_get_name (klass);
1624         nspace = mono_class_get_namespace (klass);
1625         if (*nspace) {
1626                 strcpy (p, nspace);
1627                 p += strlen (nspace);
1628                 *p++ = '.';
1629                 *p = 0;
1630         }
1631         strcpy (p, name);
1632         p += strlen (name);
1633         return p;
1634 }
1635
1636 static char*
1637 type_name (MonoClass *klass)
1638 {
1639         char buf [1024];
1640         char *p;
1641         push_nesting (buf, klass);
1642         p = (char *)malloc (strlen (buf) + 1);
1643         strcpy (p, buf);
1644         return p;
1645 }
1646
1647 static void
1648 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
1649 {
1650         if (result != MONO_PROFILE_OK)
1651                 return;
1652
1653         const char *name = mono_image_get_filename (image);
1654         int nlen = strlen (name) + 1;
1655
1656         ENTER_LOG;
1657
1658         LogBuffer *logbuffer = ensure_logbuf (
1659                 EVENT_SIZE /* event */ +
1660                 LEB128_SIZE /* time */ +
1661                 EVENT_SIZE /* type */ +
1662                 LEB128_SIZE /* image */ +
1663                 LEB128_SIZE /* flags */ +
1664                 nlen /* name */
1665         );
1666
1667         uint64_t now = current_time ();
1668
1669         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1670         emit_time (logbuffer, now);
1671         emit_byte (logbuffer, TYPE_IMAGE);
1672         emit_ptr (logbuffer, image);
1673         emit_value (logbuffer, 0); /* flags */
1674         memcpy (logbuffer->cursor, name, nlen);
1675         logbuffer->cursor += nlen;
1676
1677         EXIT_LOG;
1678
1679         send_if_needed (prof);
1680
1681         process_requests (prof);
1682
1683         InterlockedIncrement (&image_loads);
1684 }
1685
1686 static void
1687 image_unloaded (MonoProfiler *prof, MonoImage *image)
1688 {
1689         const char *name = mono_image_get_filename (image);
1690         int nlen = strlen (name) + 1;
1691
1692         ENTER_LOG;
1693
1694         LogBuffer *logbuffer = ensure_logbuf (
1695                 EVENT_SIZE /* event */ +
1696                 LEB128_SIZE /* time */ +
1697                 EVENT_SIZE /* type */ +
1698                 LEB128_SIZE /* image */ +
1699                 LEB128_SIZE /* flags */ +
1700                 nlen /* name */
1701         );
1702
1703         uint64_t now = current_time ();
1704
1705         emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1706         emit_time (logbuffer, now);
1707         emit_byte (logbuffer, TYPE_IMAGE);
1708         emit_ptr (logbuffer, image);
1709         emit_value (logbuffer, 0); /* flags */
1710         memcpy (logbuffer->cursor, name, nlen);
1711         logbuffer->cursor += nlen;
1712
1713         EXIT_LOG;
1714
1715         send_if_needed (prof);
1716
1717         process_requests (prof);
1718
1719         InterlockedIncrement (&image_unloads);
1720 }
1721
1722 static void
1723 assembly_loaded (MonoProfiler *prof, MonoAssembly *assembly, int result)
1724 {
1725         if (result != MONO_PROFILE_OK)
1726                 return;
1727
1728         char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1729         int nlen = strlen (name) + 1;
1730
1731         ENTER_LOG;
1732
1733         LogBuffer *logbuffer = ensure_logbuf (
1734                 EVENT_SIZE /* event */ +
1735                 LEB128_SIZE /* time */ +
1736                 EVENT_SIZE /* type */ +
1737                 LEB128_SIZE /* assembly */ +
1738                 LEB128_SIZE /* flags */ +
1739                 nlen /* name */
1740         );
1741
1742         uint64_t now = current_time ();
1743
1744         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1745         emit_time (logbuffer, now);
1746         emit_byte (logbuffer, TYPE_ASSEMBLY);
1747         emit_ptr (logbuffer, assembly);
1748         emit_value (logbuffer, 0); /* flags */
1749         memcpy (logbuffer->cursor, name, nlen);
1750         logbuffer->cursor += nlen;
1751
1752         EXIT_LOG;
1753
1754         mono_free (name);
1755
1756         send_if_needed (prof);
1757
1758         process_requests (prof);
1759
1760         InterlockedIncrement (&assembly_loads);
1761 }
1762
1763 static void
1764 assembly_unloaded (MonoProfiler *prof, MonoAssembly *assembly)
1765 {
1766         char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1767         int nlen = strlen (name) + 1;
1768
1769         ENTER_LOG;
1770
1771         LogBuffer *logbuffer = ensure_logbuf (
1772                 EVENT_SIZE /* event */ +
1773                 LEB128_SIZE /* time */ +
1774                 EVENT_SIZE /* type */ +
1775                 LEB128_SIZE /* assembly */ +
1776                 LEB128_SIZE /* flags */ +
1777                 nlen /* name */
1778         );
1779
1780         uint64_t now = current_time ();
1781
1782         emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1783         emit_time (logbuffer, now);
1784         emit_byte (logbuffer, TYPE_ASSEMBLY);
1785         emit_ptr (logbuffer, assembly);
1786         emit_value (logbuffer, 0); /* flags */
1787         memcpy (logbuffer->cursor, name, nlen);
1788         logbuffer->cursor += nlen;
1789
1790         EXIT_LOG;
1791
1792         mono_free (name);
1793
1794         send_if_needed (prof);
1795
1796         process_requests (prof);
1797
1798         InterlockedIncrement (&assembly_unloads);
1799 }
1800
1801 static void
1802 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
1803 {
1804         if (result != MONO_PROFILE_OK)
1805                 return;
1806
1807         char *name;
1808
1809         if (InterlockedRead (&runtime_inited))
1810                 name = mono_type_get_name (mono_class_get_type (klass));
1811         else
1812                 name = type_name (klass);
1813
1814         int nlen = strlen (name) + 1;
1815         MonoImage *image = mono_class_get_image (klass);
1816
1817         ENTER_LOG;
1818
1819         LogBuffer *logbuffer = ensure_logbuf (
1820                 EVENT_SIZE /* event */ +
1821                 LEB128_SIZE /* time */ +
1822                 EVENT_SIZE /* type */ +
1823                 LEB128_SIZE /* klass */ +
1824                 LEB128_SIZE /* image */ +
1825                 LEB128_SIZE /* flags */ +
1826                 nlen /* name */
1827         );
1828
1829         uint64_t now = current_time ();
1830
1831         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1832         emit_time (logbuffer, now);
1833         emit_byte (logbuffer, TYPE_CLASS);
1834         emit_ptr (logbuffer, klass);
1835         emit_ptr (logbuffer, image);
1836         emit_value (logbuffer, 0); /* flags */
1837         memcpy (logbuffer->cursor, name, nlen);
1838         logbuffer->cursor += nlen;
1839
1840         EXIT_LOG;
1841
1842         if (runtime_inited)
1843                 mono_free (name);
1844         else
1845                 free (name);
1846
1847         send_if_needed (prof);
1848
1849         process_requests (prof);
1850
1851         InterlockedIncrement (&class_loads);
1852 }
1853
1854 static void
1855 class_unloaded (MonoProfiler *prof, MonoClass *klass)
1856 {
1857         char *name;
1858
1859         if (InterlockedRead (&runtime_inited))
1860                 name = mono_type_get_name (mono_class_get_type (klass));
1861         else
1862                 name = type_name (klass);
1863
1864         int nlen = strlen (name) + 1;
1865         MonoImage *image = mono_class_get_image (klass);
1866
1867         ENTER_LOG;
1868
1869         LogBuffer *logbuffer = ensure_logbuf (
1870                 EVENT_SIZE /* event */ +
1871                 LEB128_SIZE /* time */ +
1872                 EVENT_SIZE /* type */ +
1873                 LEB128_SIZE /* klass */ +
1874                 LEB128_SIZE /* image */ +
1875                 LEB128_SIZE /* flags */ +
1876                 nlen /* name */
1877         );
1878
1879         uint64_t now = current_time ();
1880
1881         emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1882         emit_time (logbuffer, now);
1883         emit_byte (logbuffer, TYPE_CLASS);
1884         emit_ptr (logbuffer, klass);
1885         emit_ptr (logbuffer, image);
1886         emit_value (logbuffer, 0); /* flags */
1887         memcpy (logbuffer->cursor, name, nlen);
1888         logbuffer->cursor += nlen;
1889
1890         EXIT_LOG;
1891
1892         if (runtime_inited)
1893                 mono_free (name);
1894         else
1895                 free (name);
1896
1897         send_if_needed (prof);
1898
1899         process_requests (prof);
1900
1901         InterlockedIncrement (&class_unloads);
1902 }
1903
1904 #ifndef DISABLE_HELPER_THREAD
1905 static void process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method);
1906 #endif /* DISABLE_HELPER_THREAD */
1907
1908 static void
1909 method_enter (MonoProfiler *prof, MonoMethod *method)
1910 {
1911         uint64_t now = current_time ();
1912
1913 #ifndef DISABLE_HELPER_THREAD
1914         process_method_enter_coverage (prof, method);
1915 #endif /* DISABLE_HELPER_THREAD */
1916
1917         if (PROF_TLS_GET ()->call_depth++ <= max_call_depth) {
1918                 ENTER_LOG;
1919
1920                 LogBuffer *logbuffer = ensure_logbuf (
1921                         EVENT_SIZE /* event */ +
1922                         LEB128_SIZE /* time */ +
1923                         LEB128_SIZE /* method */
1924                 );
1925
1926                 emit_byte (logbuffer, TYPE_ENTER | TYPE_METHOD);
1927                 emit_time (logbuffer, now);
1928                 emit_method (prof, logbuffer, method);
1929
1930                 EXIT_LOG;
1931         }
1932
1933         send_if_needed (prof);
1934
1935         process_requests (prof);
1936 }
1937
1938 static void
1939 method_leave (MonoProfiler *prof, MonoMethod *method)
1940 {
1941         if (--PROF_TLS_GET ()->call_depth <= max_call_depth) {
1942                 ENTER_LOG;
1943
1944                 LogBuffer *logbuffer = ensure_logbuf (
1945                         EVENT_SIZE /* event */ +
1946                         LEB128_SIZE /* time */ +
1947                         LEB128_SIZE /* method */
1948                 );
1949
1950                 uint64_t now = current_time ();
1951
1952                 emit_byte (logbuffer, TYPE_LEAVE | TYPE_METHOD);
1953                 emit_time (logbuffer, now);
1954                 emit_method (prof, logbuffer, method);
1955
1956                 EXIT_LOG;
1957         }
1958
1959         send_if_needed (prof);
1960
1961         process_requests (prof);
1962 }
1963
1964 static void
1965 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
1966 {
1967         if (!nocalls && --PROF_TLS_GET ()->call_depth <= max_call_depth) {
1968                 ENTER_LOG;
1969
1970                 LogBuffer *logbuffer = ensure_logbuf (
1971                         EVENT_SIZE /* event */ +
1972                         LEB128_SIZE /* time */ +
1973                         LEB128_SIZE /* method */
1974                 );
1975
1976                 uint64_t now = current_time ();
1977
1978                 emit_byte (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
1979                 emit_time (logbuffer, now);
1980                 emit_method (prof, logbuffer, method);
1981
1982                 EXIT_LOG;
1983         }
1984
1985         send_if_needed (prof);
1986
1987         process_requests (prof);
1988 }
1989
1990 static void
1991 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji, int result)
1992 {
1993         if (result != MONO_PROFILE_OK)
1994                 return;
1995
1996         register_method_local (prof, method, ji);
1997
1998         process_requests (prof);
1999 }
2000
2001 static void
2002 code_buffer_new (MonoProfiler *prof, void *buffer, int size, MonoProfilerCodeBufferType type, void *data)
2003 {
2004         char *name;
2005         int nlen;
2006
2007         if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
2008                 name = (char *) data;
2009                 nlen = strlen (name) + 1;
2010         } else {
2011                 name = NULL;
2012                 nlen = 0;
2013         }
2014
2015         ENTER_LOG;
2016
2017         LogBuffer *logbuffer = ensure_logbuf (
2018                 EVENT_SIZE /* event */ +
2019                 LEB128_SIZE /* time */ +
2020                 LEB128_SIZE /* type */ +
2021                 LEB128_SIZE /* buffer */ +
2022                 LEB128_SIZE /* size */ +
2023                 (name ? (
2024                         nlen /* name */
2025                 ) : 0)
2026         );
2027
2028         uint64_t now = current_time ();
2029
2030         emit_byte (logbuffer, TYPE_JITHELPER | TYPE_RUNTIME);
2031         emit_time (logbuffer, now);
2032         emit_value (logbuffer, type);
2033         emit_ptr (logbuffer, buffer);
2034         emit_value (logbuffer, size);
2035
2036         if (name) {
2037                 memcpy (logbuffer->cursor, name, nlen);
2038                 logbuffer->cursor += nlen;
2039         }
2040
2041         EXIT_LOG;
2042
2043         process_requests (prof);
2044 }
2045
2046 static void
2047 throw_exc (MonoProfiler *prof, MonoObject *object)
2048 {
2049         int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces) ? TYPE_EXCEPTION_BT : 0;
2050         FrameData data;
2051
2052         if (do_bt)
2053                 collect_bt (&data);
2054
2055         ENTER_LOG;
2056
2057         LogBuffer *logbuffer = ensure_logbuf (
2058                 EVENT_SIZE /* event */ +
2059                 LEB128_SIZE /* time */ +
2060                 LEB128_SIZE /* object */ +
2061                 (do_bt ? (
2062                         LEB128_SIZE /* flags */ +
2063                         LEB128_SIZE /* count */ +
2064                         data.count * (
2065                                 LEB128_SIZE /* method */
2066                         )
2067                 ) : 0)
2068         );
2069
2070         uint64_t now = current_time ();
2071
2072         emit_byte (logbuffer, do_bt | TYPE_EXCEPTION);
2073         emit_time (logbuffer, now);
2074         emit_obj (logbuffer, object);
2075
2076         if (do_bt)
2077                 emit_bt (prof, logbuffer, &data);
2078
2079         EXIT_LOG;
2080
2081         process_requests (prof);
2082 }
2083
2084 static void
2085 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num)
2086 {
2087         ENTER_LOG;
2088
2089         LogBuffer *logbuffer = ensure_logbuf (
2090                 EVENT_SIZE /* event */ +
2091                 LEB128_SIZE /* time */ +
2092                 LEB128_SIZE /* clause type */ +
2093                 LEB128_SIZE /* clause num */ +
2094                 LEB128_SIZE /* method */
2095         );
2096
2097         uint64_t now = current_time ();
2098
2099         emit_byte (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
2100         emit_time (logbuffer, now);
2101         emit_value (logbuffer, clause_type);
2102         emit_value (logbuffer, clause_num);
2103         emit_method (prof, logbuffer, method);
2104
2105         EXIT_LOG;
2106
2107         process_requests (prof);
2108 }
2109
2110 static void
2111 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent event)
2112 {
2113         int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces && event == MONO_PROFILER_MONITOR_CONTENTION) ? TYPE_MONITOR_BT : 0;
2114         FrameData data;
2115
2116         if (do_bt)
2117                 collect_bt (&data);
2118
2119         ENTER_LOG;
2120
2121         LogBuffer *logbuffer = ensure_logbuf (
2122                 EVENT_SIZE /* event */ +
2123                 LEB128_SIZE /* time */ +
2124                 LEB128_SIZE /* object */ +
2125                 (do_bt ? (
2126                         LEB128_SIZE /* flags */ +
2127                         LEB128_SIZE /* count */ +
2128                         data.count * (
2129                                 LEB128_SIZE /* method */
2130                         )
2131                 ) : 0)
2132         );
2133
2134         uint64_t now = current_time ();
2135
2136         emit_byte (logbuffer, (event << 4) | do_bt | TYPE_MONITOR);
2137         emit_time (logbuffer, now);
2138         emit_obj (logbuffer, object);
2139
2140         if (do_bt)
2141                 emit_bt (profiler, logbuffer, &data);
2142
2143         EXIT_LOG;
2144
2145         process_requests (profiler);
2146 }
2147
2148 static void
2149 thread_start (MonoProfiler *prof, uintptr_t tid)
2150 {
2151         init_thread (TRUE);
2152
2153         ENTER_LOG;
2154
2155         LogBuffer *logbuffer = ensure_logbuf (
2156                 EVENT_SIZE /* event */ +
2157                 LEB128_SIZE /* time */ +
2158                 EVENT_SIZE /* type */ +
2159                 LEB128_SIZE /* tid */ +
2160                 LEB128_SIZE /* flags */
2161         );
2162
2163         uint64_t now = current_time ();
2164
2165         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
2166         emit_time (logbuffer, now);
2167         emit_byte (logbuffer, TYPE_THREAD);
2168         emit_ptr (logbuffer, (void*) tid);
2169         emit_value (logbuffer, 0); /* flags */
2170
2171         EXIT_LOG;
2172
2173         send_if_needed (prof);
2174
2175         process_requests (prof);
2176
2177         InterlockedIncrement (&thread_starts);
2178 }
2179
2180 static void
2181 thread_end (MonoProfiler *prof, uintptr_t tid)
2182 {
2183         ENTER_LOG;
2184
2185         LogBuffer *logbuffer = ensure_logbuf (
2186                 EVENT_SIZE /* event */ +
2187                 LEB128_SIZE /* time */ +
2188                 EVENT_SIZE /* type */ +
2189                 LEB128_SIZE /* tid */ +
2190                 LEB128_SIZE /* flags */
2191         );
2192
2193         uint64_t now = current_time ();
2194
2195         emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
2196         emit_time (logbuffer, now);
2197         emit_byte (logbuffer, TYPE_THREAD);
2198         emit_ptr (logbuffer, (void*) tid);
2199         emit_value (logbuffer, 0); /* flags */
2200
2201         EXIT_LOG;
2202
2203         // Don't process requests as the thread is detached from the runtime.
2204
2205         remove_thread (prof, PROF_TLS_GET (), TRUE);
2206
2207         InterlockedIncrement (&thread_ends);
2208 }
2209
2210 static void
2211 domain_loaded (MonoProfiler *prof, MonoDomain *domain, int result)
2212 {
2213         if (result != MONO_PROFILE_OK)
2214                 return;
2215
2216         ENTER_LOG;
2217
2218         LogBuffer *logbuffer = ensure_logbuf (
2219                 EVENT_SIZE /* event */ +
2220                 LEB128_SIZE /* time */ +
2221                 EVENT_SIZE /* type */ +
2222                 LEB128_SIZE /* domain id */ +
2223                 LEB128_SIZE /* flags */
2224         );
2225
2226         uint64_t now = current_time ();
2227
2228         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
2229         emit_time (logbuffer, now);
2230         emit_byte (logbuffer, TYPE_DOMAIN);
2231         emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
2232         emit_value (logbuffer, 0); /* flags */
2233
2234         EXIT_LOG;
2235
2236         send_if_needed (prof);
2237
2238         process_requests (prof);
2239
2240         InterlockedIncrement (&domain_loads);
2241 }
2242
2243 static void
2244 domain_unloaded (MonoProfiler *prof, MonoDomain *domain)
2245 {
2246         ENTER_LOG;
2247
2248         LogBuffer *logbuffer = ensure_logbuf (
2249                 EVENT_SIZE /* event */ +
2250                 LEB128_SIZE /* time */ +
2251                 EVENT_SIZE /* type */ +
2252                 LEB128_SIZE /* domain id */ +
2253                 LEB128_SIZE /* flags */
2254         );
2255
2256         uint64_t now = current_time ();
2257
2258         emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
2259         emit_time (logbuffer, now);
2260         emit_byte (logbuffer, TYPE_DOMAIN);
2261         emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
2262         emit_value (logbuffer, 0); /* flags */
2263
2264         EXIT_LOG;
2265
2266         send_if_needed (prof);
2267
2268         process_requests (prof);
2269
2270         InterlockedIncrement (&domain_unloads);
2271 }
2272
2273 static void
2274 domain_name (MonoProfiler *prof, MonoDomain *domain, const char *name)
2275 {
2276         int nlen = strlen (name) + 1;
2277
2278         ENTER_LOG;
2279
2280         LogBuffer *logbuffer = ensure_logbuf (
2281                 EVENT_SIZE /* event */ +
2282                 LEB128_SIZE /* time */ +
2283                 EVENT_SIZE /* type */ +
2284                 LEB128_SIZE /* domain id */ +
2285                 LEB128_SIZE /* flags */ +
2286                 nlen /* name */
2287         );
2288
2289         uint64_t now = current_time ();
2290
2291         emit_byte (logbuffer, TYPE_METADATA);
2292         emit_time (logbuffer, now);
2293         emit_byte (logbuffer, TYPE_DOMAIN);
2294         emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
2295         emit_value (logbuffer, 0); /* flags */
2296         memcpy (logbuffer->cursor, name, nlen);
2297         logbuffer->cursor += nlen;
2298
2299         EXIT_LOG;
2300
2301         send_if_needed (prof);
2302
2303         process_requests (prof);
2304 }
2305
2306 static void
2307 context_loaded (MonoProfiler *prof, MonoAppContext *context)
2308 {
2309         ENTER_LOG;
2310
2311         LogBuffer *logbuffer = ensure_logbuf (
2312                 EVENT_SIZE /* event */ +
2313                 LEB128_SIZE /* time */ +
2314                 EVENT_SIZE /* type */ +
2315                 LEB128_SIZE /* context id */ +
2316                 LEB128_SIZE /* flags */ +
2317                 LEB128_SIZE /* domain id */
2318         );
2319
2320         uint64_t now = current_time ();
2321
2322         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
2323         emit_time (logbuffer, now);
2324         emit_byte (logbuffer, TYPE_CONTEXT);
2325         emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
2326         emit_value (logbuffer, 0); /* flags */
2327         emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
2328
2329         EXIT_LOG;
2330
2331         send_if_needed (prof);
2332
2333         process_requests (prof);
2334
2335         InterlockedIncrement (&context_loads);
2336 }
2337
2338 static void
2339 context_unloaded (MonoProfiler *prof, MonoAppContext *context)
2340 {
2341         ENTER_LOG;
2342
2343         LogBuffer *logbuffer = ensure_logbuf (
2344                 EVENT_SIZE /* event */ +
2345                 LEB128_SIZE /* time */ +
2346                 EVENT_SIZE /* type */ +
2347                 LEB128_SIZE /* context id */ +
2348                 LEB128_SIZE /* flags */ +
2349                 LEB128_SIZE /* domain id */
2350         );
2351
2352         uint64_t now = current_time ();
2353
2354         emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
2355         emit_time (logbuffer, now);
2356         emit_byte (logbuffer, TYPE_CONTEXT);
2357         emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
2358         emit_value (logbuffer, 0); /* flags */
2359         emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
2360
2361         EXIT_LOG;
2362
2363         send_if_needed (prof);
2364
2365         process_requests (prof);
2366
2367         InterlockedIncrement (&context_unloads);
2368 }
2369
2370 static void
2371 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
2372 {
2373         int len = strlen (name) + 1;
2374
2375         ENTER_LOG;
2376
2377         LogBuffer *logbuffer = ensure_logbuf (
2378                 EVENT_SIZE /* event */ +
2379                 LEB128_SIZE /* time */ +
2380                 EVENT_SIZE /* type */ +
2381                 LEB128_SIZE /* tid */ +
2382                 LEB128_SIZE /* flags */ +
2383                 len /* name */
2384         );
2385
2386         uint64_t now = current_time ();
2387
2388         emit_byte (logbuffer, TYPE_METADATA);
2389         emit_time (logbuffer, now);
2390         emit_byte (logbuffer, TYPE_THREAD);
2391         emit_ptr (logbuffer, (void*)tid);
2392         emit_value (logbuffer, 0); /* flags */
2393         memcpy (logbuffer->cursor, name, len);
2394         logbuffer->cursor += len;
2395
2396         EXIT_LOG;
2397
2398         send_if_needed (prof);
2399
2400         process_requests (prof);
2401 }
2402
2403 typedef struct {
2404         MonoMethod *method;
2405         MonoDomain *domain;
2406         void *base_address;
2407         int offset;
2408 } AsyncFrameInfo;
2409
2410 typedef struct {
2411         MonoLockFreeQueueNode node;
2412         MonoProfiler *prof;
2413         uint64_t elapsed;
2414         uintptr_t tid;
2415         void *ip;
2416         int count;
2417         AsyncFrameInfo frames [MONO_ZERO_LEN_ARRAY];
2418 } SampleHit;
2419
2420 static mono_bool
2421 async_walk_stack (MonoMethod *method, MonoDomain *domain, void *base_address, int offset, void *data)
2422 {
2423         SampleHit *sample = (SampleHit *) data;
2424
2425         if (sample->count < num_frames) {
2426                 int i = sample->count;
2427
2428                 sample->frames [i].method = method;
2429                 sample->frames [i].domain = domain;
2430                 sample->frames [i].base_address = base_address;
2431                 sample->frames [i].offset = offset;
2432
2433                 sample->count++;
2434         }
2435
2436         return sample->count == num_frames;
2437 }
2438
2439 #define SAMPLE_SLOT_SIZE(FRAMES) (sizeof (SampleHit) + sizeof (AsyncFrameInfo) * (FRAMES - MONO_ZERO_LEN_ARRAY))
2440 #define SAMPLE_BLOCK_SIZE (mono_pagesize ())
2441
2442 static void
2443 enqueue_sample_hit (gpointer p)
2444 {
2445         SampleHit *sample = p;
2446
2447         mono_lock_free_queue_node_unpoison (&sample->node);
2448         mono_lock_free_queue_enqueue (&sample->prof->dumper_queue, &sample->node);
2449         mono_os_sem_post (&sample->prof->dumper_queue_sem);
2450
2451         InterlockedIncrement (&sample_flushes);
2452 }
2453
2454 static void
2455 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
2456 {
2457         /*
2458          * Please note: We rely on the runtime loading the profiler with
2459          * MONO_DL_EAGER (RTLD_NOW) so that references to runtime functions within
2460          * this function (and its siblings) are resolved when the profiler is
2461          * loaded. Otherwise, we would potentially invoke the dynamic linker when
2462          * invoking runtime functions, which is not async-signal-safe.
2463          */
2464
2465         if (in_shutdown)
2466                 return;
2467
2468         InterlockedIncrement (&sample_hits);
2469
2470         uint64_t now = current_time ();
2471
2472         SampleHit *sample = (SampleHit *) mono_lock_free_queue_dequeue (&profiler->sample_reuse_queue);
2473
2474         if (!sample) {
2475                 /*
2476                  * If we're out of reusable sample events and we're not allowed to
2477                  * allocate more, we have no choice but to drop the event.
2478                  */
2479                 if (InterlockedRead (&sample_allocations) >= max_allocated_sample_hits)
2480                         return;
2481
2482                 sample = mono_lock_free_alloc (&profiler->sample_allocator);
2483                 sample->prof = profiler;
2484                 mono_lock_free_queue_node_init (&sample->node, TRUE);
2485
2486                 InterlockedIncrement (&sample_allocations);
2487         }
2488
2489         sample->count = 0;
2490         mono_stack_walk_async_safe (&async_walk_stack, context, sample);
2491
2492         uintptr_t elapsed = (now - profiler->startup_time) / 10000;
2493
2494         sample->elapsed = elapsed;
2495         sample->tid = thread_id ();
2496         sample->ip = ip;
2497
2498         if (do_debug) {
2499                 int len;
2500                 char buf [256];
2501                 snprintf (buf, sizeof (buf), "hit at %p in thread %p after %llu ms\n", ip, (void *) thread_id (), (unsigned long long int) elapsed / 100);
2502                 len = strlen (buf);
2503                 ign_res (write (2, buf, len));
2504         }
2505
2506         mono_thread_hazardous_try_free (sample, enqueue_sample_hit);
2507 }
2508
2509 static uintptr_t *code_pages = 0;
2510 static int num_code_pages = 0;
2511 static int size_code_pages = 0;
2512 #define CPAGE_SHIFT (9)
2513 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
2514 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
2515 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
2516
2517 static uintptr_t
2518 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
2519 {
2520         uintptr_t i;
2521         uintptr_t start_pos;
2522         start_pos = (page >> CPAGE_SHIFT) % hsize;
2523         i = start_pos;
2524         do {
2525                 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
2526                         return 0;
2527                 } else if (!hash [i]) {
2528                         hash [i] = page;
2529                         return 1;
2530                 }
2531                 /* wrap around */
2532                 if (++i == hsize)
2533                         i = 0;
2534         } while (i != start_pos);
2535         /* should not happen */
2536         printf ("failed code page store\n");
2537         return 0;
2538 }
2539
2540 static void
2541 add_code_pointer (uintptr_t ip)
2542 {
2543         uintptr_t i;
2544         if (num_code_pages * 2 >= size_code_pages) {
2545                 uintptr_t *n;
2546                 uintptr_t old_size = size_code_pages;
2547                 size_code_pages *= 2;
2548                 if (size_code_pages == 0)
2549                         size_code_pages = 16;
2550                 n = (uintptr_t *)calloc (sizeof (uintptr_t) * size_code_pages, 1);
2551                 for (i = 0; i < old_size; ++i) {
2552                         if (code_pages [i])
2553                                 add_code_page (n, size_code_pages, code_pages [i]);
2554                 }
2555                 if (code_pages)
2556                         free (code_pages);
2557                 code_pages = n;
2558         }
2559         num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
2560 }
2561
2562 /* ELF code crashes on some systems. */
2563 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2564 #if 0
2565 static void
2566 dump_ubin (const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
2567 {
2568         int len = strlen (filename) + 1;
2569
2570         ENTER_LOG;
2571
2572         LogBuffer *logbuffer = ensure_logbuf (
2573                 EVENT_SIZE /* event */ +
2574                 LEB128_SIZE /* time */ +
2575                 LEB128_SIZE /* load address */ +
2576                 LEB128_SIZE /* offset */ +
2577                 LEB128_SIZE /* size */ +
2578                 nlen /* file name */
2579         );
2580
2581         uint64_t now = current_time ();
2582
2583         emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
2584         emit_time (logbuffer, now);
2585         emit_svalue (logbuffer, load_addr);
2586         emit_uvalue (logbuffer, offset);
2587         emit_uvalue (logbuffer, size);
2588         memcpy (logbuffer->cursor, filename, len);
2589         logbuffer->cursor += len;
2590
2591         EXIT_LOG;
2592 }
2593 #endif
2594
2595 static void
2596 dump_usym (const char *name, uintptr_t value, uintptr_t size)
2597 {
2598         int len = strlen (name) + 1;
2599
2600         ENTER_LOG;
2601
2602         LogBuffer *logbuffer = ensure_logbuf (
2603                 EVENT_SIZE /* event */ +
2604                 LEB128_SIZE /* value */ +
2605                 LEB128_SIZE /* size */ +
2606                 len /* name */
2607         );
2608
2609         emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
2610         emit_ptr (logbuffer, (void*)value);
2611         emit_value (logbuffer, size);
2612         memcpy (logbuffer->cursor, name, len);
2613         logbuffer->cursor += len;
2614
2615         EXIT_LOG;
2616 }
2617
2618 /* ELF code crashes on some systems. */
2619 //#if defined(ELFMAG0)
2620 #if 0
2621
2622 #if SIZEOF_VOID_P == 4
2623 #define ELF_WSIZE 32
2624 #else
2625 #define ELF_WSIZE 64
2626 #endif
2627 #ifndef ElfW
2628 #define ElfW(type)      _ElfW (Elf, ELF_WSIZE, type)
2629 #define _ElfW(e,w,t)    _ElfW_1 (e, w, _##t)
2630 #define _ElfW_1(e,w,t)  e##w##t
2631 #endif
2632
2633 static void
2634 dump_elf_symbols (ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
2635 {
2636         int i;
2637         for (i = 0; i < num_symbols; ++i) {
2638                 const char* sym;
2639                 sym =  strtab + symbols [i].st_name;
2640                 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
2641                         continue;
2642                 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
2643                 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
2644         }
2645 }
2646
2647 static int
2648 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
2649 {
2650         int fd, i;
2651         void *data;
2652         struct stat statb;
2653         uint64_t file_size;
2654         ElfW(Ehdr) *header;
2655         ElfW(Shdr) *sheader;
2656         ElfW(Shdr) *shstrtabh;
2657         ElfW(Shdr) *symtabh = NULL;
2658         ElfW(Shdr) *strtabh = NULL;
2659         ElfW(Sym) *symbols = NULL;
2660         const char *strtab;
2661         int num_symbols;
2662
2663         fd = open (filename, O_RDONLY);
2664         if (fd < 0)
2665                 return 0;
2666         if (fstat (fd, &statb) != 0) {
2667                 close (fd);
2668                 return 0;
2669         }
2670         file_size = statb.st_size;
2671         data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
2672         close (fd);
2673         if (data == MAP_FAILED)
2674                 return 0;
2675         header = data;
2676         if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2677                         header->e_ident [EI_MAG1] != ELFMAG1 ||
2678                         header->e_ident [EI_MAG2] != ELFMAG2 ||
2679                         header->e_ident [EI_MAG3] != ELFMAG3 ) {
2680                 munmap (data, file_size);
2681                 return 0;
2682         }
2683         sheader = (void*)((char*)data + header->e_shoff);
2684         shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
2685         strtab = (const char*)data + shstrtabh->sh_offset;
2686         for (i = 0; i < header->e_shnum; ++i) {
2687                 //printf ("section header: %d\n", sheader->sh_type);
2688                 if (sheader->sh_type == SHT_SYMTAB) {
2689                         symtabh = sheader;
2690                         strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
2691                         /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
2692                         break;
2693                 }
2694                 sheader = (void*)((char*)sheader + header->e_shentsize);
2695         }
2696         if (!symtabh || !strtabh) {
2697                 munmap (data, file_size);
2698                 return 0;
2699         }
2700         strtab = (const char*)data + strtabh->sh_offset;
2701         num_symbols = symtabh->sh_size / symtabh->sh_entsize;
2702         symbols = (void*)((char*)data + symtabh->sh_offset);
2703         dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
2704         munmap (data, file_size);
2705         return 1;
2706 }
2707 #endif
2708
2709 /* ELF code crashes on some systems. */
2710 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2711 #if 0
2712 static int
2713 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
2714 {
2715         MonoProfiler *prof = data;
2716         char buf [256];
2717         const char *filename;
2718         BinaryObject *obj;
2719         char *a = (void*)info->dlpi_addr;
2720         int i, num_sym;
2721         ElfW(Dyn) *dyn = NULL;
2722         ElfW(Sym) *symtab = NULL;
2723         ElfW(Word) *hash_table = NULL;
2724         ElfW(Ehdr) *header = NULL;
2725         const char* strtab = NULL;
2726         for (obj = prof->binary_objects; obj; obj = obj->next) {
2727                 if (obj->addr == a)
2728                         return 0;
2729         }
2730         filename = info->dlpi_name;
2731         if (!filename)
2732                 return 0;
2733         if (!info->dlpi_addr && !filename [0]) {
2734                 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
2735                 if (l > 0) {
2736                         buf [l] = 0;
2737                         filename = buf;
2738                 }
2739         }
2740         obj = calloc (sizeof (BinaryObject), 1);
2741         obj->addr = (void*)info->dlpi_addr;
2742         obj->name = pstrdup (filename);
2743         obj->next = prof->binary_objects;
2744         prof->binary_objects = obj;
2745         //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
2746         a = NULL;
2747         for (i = 0; i < info->dlpi_phnum; ++i) {
2748                 //printf ("segment type %d file offset: %d, size: %d\n", info->dlpi_phdr[i].p_type, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
2749                 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
2750                         header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2751                         if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2752                                         header->e_ident [EI_MAG1] != ELFMAG1 ||
2753                                         header->e_ident [EI_MAG2] != ELFMAG2 ||
2754                                         header->e_ident [EI_MAG3] != ELFMAG3 ) {
2755                                 header = NULL;
2756                         }
2757                         dump_ubin (filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
2758                 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
2759                         dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2760                 }
2761         }
2762         if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
2763                 return 0;
2764         if (!info->dlpi_name || !info->dlpi_name[0])
2765                 return 0;
2766         if (!dyn)
2767                 return 0;
2768         for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
2769                 if (dyn [i].d_tag == DT_SYMTAB) {
2770                         if (symtab && do_debug)
2771                                 printf ("multiple symtabs: %d\n", i);
2772                         symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
2773                 } else if (dyn [i].d_tag == DT_HASH) {
2774                         hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
2775                 } else if (dyn [i].d_tag == DT_STRTAB) {
2776                         strtab = (const char*)(a + dyn [i].d_un.d_ptr);
2777                 }
2778         }
2779         if (!hash_table)
2780                 return 0;
2781         num_sym = hash_table [1];
2782         dump_elf_symbols (symtab, num_sym, strtab, (void*)info->dlpi_addr);
2783         return 0;
2784 }
2785
2786 static int
2787 load_binaries (MonoProfiler *prof)
2788 {
2789         dl_iterate_phdr (elf_dl_callback, prof);
2790         return 1;
2791 }
2792 #else
2793 static int
2794 load_binaries (MonoProfiler *prof)
2795 {
2796         return 0;
2797 }
2798 #endif
2799
2800 static const char*
2801 symbol_for (uintptr_t code)
2802 {
2803 #ifdef HAVE_DLADDR
2804         void *ip = (void*)code;
2805         Dl_info di;
2806         if (dladdr (ip, &di)) {
2807                 if (di.dli_sname)
2808                         return di.dli_sname;
2809         } else {
2810         /*      char **names;
2811                 names = backtrace_symbols (&ip, 1);
2812                 if (names) {
2813                         const char* p = names [0];
2814                         free (names);
2815                         return p;
2816                 }
2817                 */
2818         }
2819 #endif
2820         return NULL;
2821 }
2822
2823 static void
2824 dump_unmanaged_coderefs (MonoProfiler *prof)
2825 {
2826         int i;
2827         const char* last_symbol;
2828         uintptr_t addr, page_end;
2829
2830         if (load_binaries (prof))
2831                 return;
2832         for (i = 0; i < size_code_pages; ++i) {
2833                 const char* sym;
2834                 if (!code_pages [i] || code_pages [i] & 1)
2835                         continue;
2836                 last_symbol = NULL;
2837                 addr = CPAGE_ADDR (code_pages [i]);
2838                 page_end = addr + CPAGE_SIZE;
2839                 code_pages [i] |= 1;
2840                 /* we dump the symbols for the whole page */
2841                 for (; addr < page_end; addr += 16) {
2842                         sym = symbol_for (addr);
2843                         if (sym && sym == last_symbol)
2844                                 continue;
2845                         last_symbol = sym;
2846                         if (!sym)
2847                                 continue;
2848                         dump_usym (sym, addr, 0); /* let's not guess the size */
2849                         //printf ("found symbol at %p: %s\n", (void*)addr, sym);
2850                 }
2851         }
2852 }
2853
2854 static int
2855 mono_cpu_count (void)
2856 {
2857 #ifdef PLATFORM_ANDROID
2858         /* Android tries really hard to save power by powering off CPUs on SMP phones which
2859          * means the normal way to query cpu count returns a wrong value with userspace API.
2860          * Instead we use /sys entries to query the actual hardware CPU count.
2861          */
2862         int count = 0;
2863         char buffer[8] = {'\0'};
2864         int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
2865         /* Format of the /sys entry is a cpulist of indexes which in the case
2866          * of present is always of the form "0-(n-1)" when there is more than
2867          * 1 core, n being the number of CPU cores in the system. Otherwise
2868          * the value is simply 0
2869          */
2870         if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
2871                 count = strtol (((char*)buffer) + 2, NULL, 10);
2872         if (present != -1)
2873                 close (present);
2874         if (count > 0)
2875                 return count + 1;
2876 #endif
2877
2878 #if defined(HOST_ARM) || defined (HOST_ARM64)
2879
2880         /* ARM platforms tries really hard to save power by powering off CPUs on SMP phones which
2881          * means the normal way to query cpu count returns a wrong value with userspace API. */
2882
2883 #ifdef _SC_NPROCESSORS_CONF
2884         {
2885                 int count = sysconf (_SC_NPROCESSORS_CONF);
2886                 if (count > 0)
2887                         return count;
2888         }
2889 #endif
2890
2891 #else
2892
2893 #ifdef HAVE_SCHED_GETAFFINITY
2894         {
2895                 cpu_set_t set;
2896                 if (sched_getaffinity (getpid (), sizeof (set), &set) == 0)
2897                         return CPU_COUNT (&set);
2898         }
2899 #endif
2900 #ifdef _SC_NPROCESSORS_ONLN
2901         {
2902                 int count = sysconf (_SC_NPROCESSORS_ONLN);
2903                 if (count > 0)
2904                         return count;
2905         }
2906 #endif
2907
2908 #endif /* defined(HOST_ARM) || defined (HOST_ARM64) */
2909
2910 #ifdef USE_SYSCTL
2911         {
2912                 int count;
2913                 int mib [2];
2914                 size_t len = sizeof (int);
2915                 mib [0] = CTL_HW;
2916                 mib [1] = HW_NCPU;
2917                 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
2918                         return count;
2919         }
2920 #endif
2921 #ifdef HOST_WIN32
2922         {
2923                 SYSTEM_INFO info;
2924                 GetSystemInfo (&info);
2925                 return info.dwNumberOfProcessors;
2926         }
2927 #endif
2928         /* FIXME: warn */
2929         return 1;
2930 }
2931
2932 #if USE_PERF_EVENTS
2933
2934 typedef struct {
2935         int perf_fd;
2936         unsigned int prev_pos;
2937         void *mmap_base;
2938         struct perf_event_mmap_page *page_desc;
2939 } PerfData ;
2940
2941 static PerfData *perf_data = NULL;
2942 static int num_perf;
2943 #define PERF_PAGES_SHIFT 4
2944 static int num_pages = 1 << PERF_PAGES_SHIFT;
2945 static unsigned int mmap_mask;
2946
2947 typedef struct {
2948         struct perf_event_header h;
2949         uint64_t ip;
2950         uint32_t pid;
2951         uint32_t tid;
2952         uint64_t timestamp;
2953         uint64_t period;
2954         uint64_t nframes;
2955 } PSample;
2956
2957 static int
2958 perf_event_syscall (struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)
2959 {
2960         attr->size = PERF_ATTR_SIZE_VER0;
2961         //printf ("perf attr size: %d\n", attr->size);
2962 #if defined(__x86_64__)
2963         return syscall(/*__NR_perf_event_open*/ 298, attr, pid, cpu, group_fd, flags);
2964 #elif defined(__i386__)
2965         return syscall(/*__NR_perf_event_open*/ 336, attr, pid, cpu, group_fd, flags);
2966 #elif defined(__arm__) || defined (__aarch64__)
2967         return syscall(/*__NR_perf_event_open*/ 364, attr, pid, cpu, group_fd, flags);
2968 #else
2969         return -1;
2970 #endif
2971 }
2972
2973 static int
2974 setup_perf_map (PerfData *perf)
2975 {
2976         perf->mmap_base = mmap (NULL, (num_pages + 1) * getpagesize (), PROT_READ|PROT_WRITE, MAP_SHARED, perf->perf_fd, 0);
2977         if (perf->mmap_base == MAP_FAILED) {
2978                 if (do_debug)
2979                         printf ("failed mmap\n");
2980                 return 0;
2981         }
2982         perf->page_desc = perf->mmap_base;
2983         if (do_debug)
2984                 printf ("mmap version: %d\n", perf->page_desc->version);
2985         return 1;
2986 }
2987
2988 static void
2989 dump_perf_hits (MonoProfiler *prof, void *buf, int size)
2990 {
2991         int count = 1;
2992         int mbt_count = 0;
2993         void *end = (char*)buf + size;
2994         int samples = 0;
2995         int pid = getpid ();
2996
2997         while (buf < end) {
2998                 PSample *s = buf;
2999                 if (s->h.size == 0)
3000                         break;
3001                 if (pid != s->pid) {
3002                         if (do_debug)
3003                                 printf ("event for different pid: %d\n", s->pid);
3004                         buf = (char*)buf + s->h.size;
3005                         continue;
3006                 }
3007                 /*ip = (void*)s->ip;
3008                 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
3009                         s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
3010
3011                 ENTER_LOG;
3012
3013                 LogBuffer *logbuffer = ensure_logbuf (
3014                         EVENT_SIZE /* event */ +
3015                         LEB128_SIZE /* type */ +
3016                         LEB128_SIZE /* time */ +
3017                         LEB128_SIZE /* tid */ +
3018                         LEB128_SIZE /* count */ +
3019                         count * (
3020                                 LEB128_SIZE /* ip */
3021                         ) +
3022                         LEB128_SIZE /* managed count */ +
3023                         mbt_count * (
3024                                 LEB128_SIZE /* method */ +
3025                                 LEB128_SIZE /* il offset */ +
3026                                 LEB128_SIZE /* native offset */
3027                         )
3028                 );
3029
3030                 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
3031                 emit_value (logbuffer, sample_type);
3032                 emit_uvalue (logbuffer, s->timestamp - prof->startup_time);
3033                 /*
3034                  * No useful thread ID to write here, since throughout the
3035                  * profiler we use pthread_self () but the ID we get from
3036                  * perf is the kernel's thread ID.
3037                  */
3038                 emit_ptr (logbuffer, 0);
3039                 emit_value (logbuffer, count);
3040                 emit_ptr (logbuffer, (void*)(uintptr_t)s->ip);
3041                 /* no support here yet for the managed backtrace */
3042                 emit_uvalue (logbuffer, mbt_count);
3043
3044                 EXIT_LOG;
3045
3046                 add_code_pointer (s->ip);
3047                 buf = (char*)buf + s->h.size;
3048                 samples++;
3049         }
3050         if (do_debug)
3051                 printf ("dumped %d samples\n", samples);
3052         dump_unmanaged_coderefs (prof);
3053 }
3054
3055 /* read events from the ring buffer */
3056 static int
3057 read_perf_mmap (MonoProfiler* prof, int cpu)
3058 {
3059         PerfData *perf = perf_data + cpu;
3060         unsigned char *buf;
3061         unsigned char *data = (unsigned char*)perf->mmap_base + getpagesize ();
3062         unsigned int head = perf->page_desc->data_head;
3063         int diff, size;
3064         unsigned int old;
3065
3066         mono_memory_read_barrier ();
3067
3068         old = perf->prev_pos;
3069         diff = head - old;
3070         if (diff < 0) {
3071                 if (do_debug)
3072                         printf ("lost mmap events: old: %d, head: %d\n", old, head);
3073                 old = head;
3074         }
3075         size = head - old;
3076         if ((old & mmap_mask) + size != (head & mmap_mask)) {
3077                 buf = data + (old & mmap_mask);
3078                 size = mmap_mask + 1 - (old & mmap_mask);
3079                 old += size;
3080                 /* size bytes at buf */
3081                 if (do_debug)
3082                         printf ("found1 bytes of events: %d\n", size);
3083                 dump_perf_hits (prof, buf, size);
3084         }
3085         buf = data + (old & mmap_mask);
3086         size = head - old;
3087         /* size bytes at buf */
3088         if (do_debug)
3089                 printf ("found bytes of events: %d\n", size);
3090         dump_perf_hits (prof, buf, size);
3091         old += size;
3092         perf->prev_pos = old;
3093         perf->page_desc->data_tail = old;
3094         return 0;
3095 }
3096
3097 static int
3098 setup_perf_event_for_cpu (PerfData *perf, int cpu)
3099 {
3100         struct perf_event_attr attr;
3101         memset (&attr, 0, sizeof (attr));
3102         attr.type = PERF_TYPE_HARDWARE;
3103         switch (sample_type) {
3104         case SAMPLE_CYCLES: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
3105         case SAMPLE_INSTRUCTIONS: attr.config = PERF_COUNT_HW_INSTRUCTIONS; break;
3106         case SAMPLE_CACHE_MISSES: attr.config = PERF_COUNT_HW_CACHE_MISSES; break;
3107         case SAMPLE_CACHE_REFS: attr.config = PERF_COUNT_HW_CACHE_REFERENCES; break;
3108         case SAMPLE_BRANCHES: attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; break;
3109         case SAMPLE_BRANCH_MISSES: attr.config = PERF_COUNT_HW_BRANCH_MISSES; break;
3110         default: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
3111         }
3112         attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD | PERF_SAMPLE_TIME;
3113 //      attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
3114         attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
3115         attr.inherit = 1;
3116         attr.freq = 1;
3117         attr.sample_freq = sample_freq;
3118
3119         perf->perf_fd = perf_event_syscall (&attr, getpid (), cpu, -1, 0);
3120         if (do_debug)
3121                 printf ("perf fd: %d, freq: %d, event: %llu\n", perf->perf_fd, sample_freq, attr.config);
3122         if (perf->perf_fd < 0) {
3123                 if (perf->perf_fd == -EPERM) {
3124                         fprintf (stderr, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
3125                 } else {
3126                         if (do_debug)
3127                                 perror ("open perf event");
3128                 }
3129                 return 0;
3130         }
3131         if (!setup_perf_map (perf)) {
3132                 close (perf->perf_fd);
3133                 perf->perf_fd = -1;
3134                 return 0;
3135         }
3136         return 1;
3137 }
3138
3139 static int
3140 setup_perf_event (void)
3141 {
3142         int i, count = 0;
3143         mmap_mask = num_pages * getpagesize () - 1;
3144         num_perf = mono_cpu_count ();
3145         perf_data = calloc (num_perf, sizeof (PerfData));
3146         for (i = 0; i < num_perf; ++i) {
3147                 count += setup_perf_event_for_cpu (perf_data + i, i);
3148         }
3149         if (count)
3150                 return 1;
3151         free (perf_data);
3152         perf_data = NULL;
3153         return 0;
3154 }
3155
3156 #endif /* USE_PERF_EVENTS */
3157
3158 #ifndef DISABLE_HELPER_THREAD
3159
3160 typedef struct MonoCounterAgent {
3161         MonoCounter *counter;
3162         // MonoCounterAgent specific data :
3163         void *value;
3164         size_t value_size;
3165         short index;
3166         short emitted;
3167         struct MonoCounterAgent *next;
3168 } MonoCounterAgent;
3169
3170 static MonoCounterAgent* counters;
3171 static gboolean counters_initialized = FALSE;
3172 static int counters_index = 1;
3173 static mono_mutex_t counters_mutex;
3174
3175 static void
3176 counters_add_agent (MonoCounter *counter)
3177 {
3178         MonoCounterAgent *agent, *item;
3179
3180         if (!counters_initialized)
3181                 return;
3182
3183         mono_os_mutex_lock (&counters_mutex);
3184
3185         for (agent = counters; agent; agent = agent->next) {
3186                 if (agent->counter == counter) {
3187                         agent->value_size = 0;
3188                         if (agent->value) {
3189                                 free (agent->value);
3190                                 agent->value = NULL;
3191                         }
3192                         mono_os_mutex_unlock (&counters_mutex);
3193                         return;
3194                 }
3195         }
3196
3197         agent = (MonoCounterAgent *)malloc (sizeof (MonoCounterAgent));
3198         agent->counter = counter;
3199         agent->value = NULL;
3200         agent->value_size = 0;
3201         agent->index = counters_index++;
3202         agent->emitted = 0;
3203         agent->next = NULL;
3204
3205         if (!counters) {
3206                 counters = agent;
3207         } else {
3208                 item = counters;
3209                 while (item->next)
3210                         item = item->next;
3211                 item->next = agent;
3212         }
3213
3214         mono_os_mutex_unlock (&counters_mutex);
3215 }
3216
3217 static mono_bool
3218 counters_init_foreach_callback (MonoCounter *counter, gpointer data)
3219 {
3220         counters_add_agent (counter);
3221         return TRUE;
3222 }
3223
3224 static void
3225 counters_init (MonoProfiler *profiler)
3226 {
3227         assert (!counters_initialized);
3228
3229         mono_os_mutex_init (&counters_mutex);
3230
3231         counters_initialized = TRUE;
3232
3233         mono_counters_on_register (&counters_add_agent);
3234         mono_counters_foreach (counters_init_foreach_callback, NULL);
3235 }
3236
3237 static void
3238 counters_emit (MonoProfiler *profiler, gboolean threadless)
3239 {
3240         MonoCounterAgent *agent;
3241         int len = 0;
3242         int size =
3243                 EVENT_SIZE /* event */ +
3244                 LEB128_SIZE /* len */
3245         ;
3246
3247         if (!counters_initialized)
3248                 return;
3249
3250         mono_os_mutex_lock (&counters_mutex);
3251
3252         for (agent = counters; agent; agent = agent->next) {
3253                 if (agent->emitted)
3254                         continue;
3255
3256                 size +=
3257                         LEB128_SIZE /* section */ +
3258                         strlen (mono_counter_get_name (agent->counter)) + 1 /* name */ +
3259                         LEB128_SIZE /* type */ +
3260                         LEB128_SIZE /* unit */ +
3261                         LEB128_SIZE /* variance */ +
3262                         LEB128_SIZE /* index */
3263                 ;
3264
3265                 len += 1;
3266         }
3267
3268         if (!len) {
3269                 mono_os_mutex_unlock (&counters_mutex);
3270                 return;
3271         }
3272
3273         ENTER_LOG;
3274
3275         LogBuffer *logbuffer = ensure_logbuf (size);
3276
3277         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
3278         emit_value (logbuffer, len);
3279
3280         for (agent = counters; agent; agent = agent->next) {
3281                 const char *name;
3282
3283                 if (agent->emitted)
3284                         continue;
3285
3286                 name = mono_counter_get_name (agent->counter);
3287                 emit_value (logbuffer, mono_counter_get_section (agent->counter));
3288                 emit_string (logbuffer, name, strlen (name) + 1);
3289                 emit_value (logbuffer, mono_counter_get_type (agent->counter));
3290                 emit_value (logbuffer, mono_counter_get_unit (agent->counter));
3291                 emit_value (logbuffer, mono_counter_get_variance (agent->counter));
3292                 emit_value (logbuffer, agent->index);
3293
3294                 agent->emitted = 1;
3295         }
3296
3297         EXIT_LOG;
3298
3299         if (threadless)
3300                 safe_send_threadless (profiler);
3301         else
3302                 safe_send (profiler);
3303
3304         mono_os_mutex_unlock (&counters_mutex);
3305 }
3306
3307 static void
3308 counters_sample (MonoProfiler *profiler, uint64_t timestamp, gboolean threadless)
3309 {
3310         MonoCounterAgent *agent;
3311         MonoCounter *counter;
3312         int type;
3313         int buffer_size;
3314         void *buffer;
3315         int size;
3316
3317         if (!counters_initialized)
3318                 return;
3319
3320         counters_emit (profiler, threadless);
3321
3322         buffer_size = 8;
3323         buffer = calloc (1, buffer_size);
3324
3325         mono_os_mutex_lock (&counters_mutex);
3326
3327         size =
3328                 EVENT_SIZE /* event */ +
3329                 LEB128_SIZE /* time */
3330         ;
3331
3332         for (agent = counters; agent; agent = agent->next) {
3333                 size +=
3334                         LEB128_SIZE /* index */ +
3335                         LEB128_SIZE /* type */ +
3336                         mono_counter_get_size (agent->counter) /* value */
3337                 ;
3338         }
3339
3340         size +=
3341                 LEB128_SIZE /* stop marker */
3342         ;
3343
3344         ENTER_LOG;
3345
3346         LogBuffer *logbuffer = ensure_logbuf (size);
3347
3348         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
3349         emit_uvalue (logbuffer, timestamp);
3350
3351         for (agent = counters; agent; agent = agent->next) {
3352                 size_t size;
3353
3354                 counter = agent->counter;
3355
3356                 size = mono_counter_get_size (counter);
3357                 if (size < 0) {
3358                         continue; // FIXME error
3359                 } else if (size > buffer_size) {
3360                         buffer_size = size;
3361                         buffer = realloc (buffer, buffer_size);
3362                 }
3363
3364                 memset (buffer, 0, buffer_size);
3365
3366                 if (mono_counters_sample (counter, buffer, size) < 0)
3367                         continue; // FIXME error
3368
3369                 type = mono_counter_get_type (counter);
3370
3371                 if (!agent->value) {
3372                         agent->value = calloc (1, size);
3373                         agent->value_size = size;
3374                 } else {
3375                         if (type == MONO_COUNTER_STRING) {
3376                                 if (strcmp (agent->value, buffer) == 0)
3377                                         continue;
3378                         } else {
3379                                 if (agent->value_size == size && memcmp (agent->value, buffer, size) == 0)
3380                                         continue;
3381                         }
3382                 }
3383
3384                 emit_uvalue (logbuffer, agent->index);
3385                 emit_uvalue (logbuffer, type);
3386                 switch (type) {
3387                 case MONO_COUNTER_INT:
3388 #if SIZEOF_VOID_P == 4
3389                 case MONO_COUNTER_WORD:
3390 #endif
3391                         emit_svalue (logbuffer, *(int*)buffer - *(int*)agent->value);
3392                         break;
3393                 case MONO_COUNTER_UINT:
3394                         emit_uvalue (logbuffer, *(guint*)buffer - *(guint*)agent->value);
3395                         break;
3396                 case MONO_COUNTER_TIME_INTERVAL:
3397                 case MONO_COUNTER_LONG:
3398 #if SIZEOF_VOID_P == 8
3399                 case MONO_COUNTER_WORD:
3400 #endif
3401                         emit_svalue (logbuffer, *(gint64*)buffer - *(gint64*)agent->value);
3402                         break;
3403                 case MONO_COUNTER_ULONG:
3404                         emit_uvalue (logbuffer, *(guint64*)buffer - *(guint64*)agent->value);
3405                         break;
3406                 case MONO_COUNTER_DOUBLE:
3407                         emit_double (logbuffer, *(double*)buffer);
3408                         break;
3409                 case MONO_COUNTER_STRING:
3410                         if (size == 0) {
3411                                 emit_byte (logbuffer, 0);
3412                         } else {
3413                                 emit_byte (logbuffer, 1);
3414                                 emit_string (logbuffer, (char*)buffer, size);
3415                         }
3416                         break;
3417                 default:
3418                         assert (0);
3419                 }
3420
3421                 if (type == MONO_COUNTER_STRING && size > agent->value_size) {
3422                         agent->value = realloc (agent->value, size);
3423                         agent->value_size = size;
3424                 }
3425
3426                 if (size > 0)
3427                         memcpy (agent->value, buffer, size);
3428         }
3429         free (buffer);
3430
3431         emit_value (logbuffer, 0);
3432
3433         EXIT_LOG;
3434
3435         if (threadless)
3436                 safe_send_threadless (profiler);
3437         else
3438                 safe_send (profiler);
3439
3440         mono_os_mutex_unlock (&counters_mutex);
3441 }
3442
3443 typedef struct _PerfCounterAgent PerfCounterAgent;
3444 struct _PerfCounterAgent {
3445         PerfCounterAgent *next;
3446         int index;
3447         char *category_name;
3448         char *name;
3449         int type;
3450         gint64 value;
3451         guint8 emitted;
3452         guint8 updated;
3453         guint8 deleted;
3454 };
3455
3456 static PerfCounterAgent *perfcounters = NULL;
3457
3458 static void
3459 perfcounters_emit (MonoProfiler *profiler, gboolean threadless)
3460 {
3461         PerfCounterAgent *pcagent;
3462         int len = 0;
3463         int size =
3464                 EVENT_SIZE /* event */ +
3465                 LEB128_SIZE /* len */
3466         ;
3467
3468         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3469                 if (pcagent->emitted)
3470                         continue;
3471
3472                 size +=
3473                         LEB128_SIZE /* section */ +
3474                         strlen (pcagent->category_name) + 1 /* category name */ +
3475                         strlen (pcagent->name) + 1 /* name */ +
3476                         LEB128_SIZE /* type */ +
3477                         LEB128_SIZE /* unit */ +
3478                         LEB128_SIZE /* variance */ +
3479                         LEB128_SIZE /* index */
3480                 ;
3481
3482                 len += 1;
3483         }
3484
3485         if (!len)
3486                 return;
3487
3488         ENTER_LOG;
3489
3490         LogBuffer *logbuffer = ensure_logbuf (size);
3491
3492         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
3493         emit_value (logbuffer, len);
3494
3495         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3496                 if (pcagent->emitted)
3497                         continue;
3498
3499                 emit_value (logbuffer, MONO_COUNTER_PERFCOUNTERS);
3500                 emit_string (logbuffer, pcagent->category_name, strlen (pcagent->category_name) + 1);
3501                 emit_string (logbuffer, pcagent->name, strlen (pcagent->name) + 1);
3502                 emit_value (logbuffer, MONO_COUNTER_LONG);
3503                 emit_value (logbuffer, MONO_COUNTER_RAW);
3504                 emit_value (logbuffer, MONO_COUNTER_VARIABLE);
3505                 emit_value (logbuffer, pcagent->index);
3506
3507                 pcagent->emitted = 1;
3508         }
3509
3510         EXIT_LOG;
3511
3512         if (threadless)
3513                 safe_send_threadless (profiler);
3514         else
3515                 safe_send (profiler);
3516 }
3517
3518 static gboolean
3519 perfcounters_foreach (char *category_name, char *name, unsigned char type, gint64 value, gpointer user_data)
3520 {
3521         PerfCounterAgent *pcagent;
3522
3523         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3524                 if (strcmp (pcagent->category_name, category_name) != 0 || strcmp (pcagent->name, name) != 0)
3525                         continue;
3526                 if (pcagent->value == value)
3527                         return TRUE;
3528
3529                 pcagent->value = value;
3530                 pcagent->updated = 1;
3531                 pcagent->deleted = 0;
3532                 return TRUE;
3533         }
3534
3535         pcagent = g_new0 (PerfCounterAgent, 1);
3536         pcagent->next = perfcounters;
3537         pcagent->index = counters_index++;
3538         pcagent->category_name = g_strdup (category_name);
3539         pcagent->name = g_strdup (name);
3540         pcagent->type = (int) type;
3541         pcagent->value = value;
3542         pcagent->emitted = 0;
3543         pcagent->updated = 1;
3544         pcagent->deleted = 0;
3545
3546         perfcounters = pcagent;
3547
3548         return TRUE;
3549 }
3550
3551 static void
3552 perfcounters_sample (MonoProfiler *profiler, uint64_t timestamp, gboolean threadless)
3553 {
3554         PerfCounterAgent *pcagent;
3555         int size;
3556
3557         if (!counters_initialized)
3558                 return;
3559
3560         mono_os_mutex_lock (&counters_mutex);
3561
3562         /* mark all perfcounters as deleted, foreach will unmark them as necessary */
3563         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next)
3564                 pcagent->deleted = 1;
3565
3566         mono_perfcounter_foreach (perfcounters_foreach, perfcounters);
3567
3568         perfcounters_emit (profiler, threadless);
3569
3570         size =
3571                 EVENT_SIZE /* event */ +
3572                 LEB128_SIZE /* time */
3573         ;
3574
3575         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3576                 if (pcagent->deleted || !pcagent->updated)
3577                         continue;
3578
3579                 size +=
3580                         LEB128_SIZE /* index */ +
3581                         LEB128_SIZE /* type */ +
3582                         LEB128_SIZE /* value */
3583                 ;
3584         }
3585
3586         size +=
3587                 LEB128_SIZE /* stop marker */
3588         ;
3589
3590         ENTER_LOG;
3591
3592         LogBuffer *logbuffer = ensure_logbuf (size);
3593
3594         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
3595         emit_uvalue (logbuffer, timestamp);
3596
3597         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3598                 if (pcagent->deleted || !pcagent->updated)
3599                         continue;
3600                 emit_uvalue (logbuffer, pcagent->index);
3601                 emit_uvalue (logbuffer, MONO_COUNTER_LONG);
3602                 emit_svalue (logbuffer, pcagent->value);
3603
3604                 pcagent->updated = 0;
3605         }
3606
3607         emit_value (logbuffer, 0);
3608
3609         EXIT_LOG;
3610
3611         if (threadless)
3612                 safe_send_threadless (profiler);
3613         else
3614                 safe_send (profiler);
3615
3616         mono_os_mutex_unlock (&counters_mutex);
3617 }
3618
3619 static void
3620 counters_and_perfcounters_sample (MonoProfiler *prof, gboolean threadless)
3621 {
3622         static uint64_t start = -1;
3623         uint64_t now;
3624
3625         if (start == -1)
3626                 start = current_time ();
3627
3628         now = current_time ();
3629         counters_sample (prof, (now - start) / 1000/ 1000, threadless);
3630         perfcounters_sample (prof, (now - start) / 1000/ 1000, threadless);
3631 }
3632
3633 #define COVERAGE_DEBUG(x) if (debug_coverage) {x}
3634 static mono_mutex_t coverage_mutex;
3635 static MonoConcurrentHashTable *coverage_methods = NULL;
3636 static MonoConcurrentHashTable *coverage_assemblies = NULL;
3637 static MonoConcurrentHashTable *coverage_classes = NULL;
3638
3639 static MonoConcurrentHashTable *filtered_classes = NULL;
3640 static MonoConcurrentHashTable *entered_methods = NULL;
3641 static MonoConcurrentHashTable *image_to_methods = NULL;
3642 static MonoConcurrentHashTable *suppressed_assemblies = NULL;
3643 static gboolean coverage_initialized = FALSE;
3644
3645 static GPtrArray *coverage_data = NULL;
3646 static int previous_offset = 0;
3647
3648 typedef struct {
3649         MonoLockFreeQueueNode node;
3650         MonoMethod *method;
3651 } MethodNode;
3652
3653 typedef struct {
3654         int offset;
3655         int counter;
3656         char *filename;
3657         int line;
3658         int column;
3659 } CoverageEntry;
3660
3661 static void
3662 free_coverage_entry (gpointer data, gpointer userdata)
3663 {
3664         CoverageEntry *entry = (CoverageEntry *)data;
3665         g_free (entry->filename);
3666         g_free (entry);
3667 }
3668
3669 static void
3670 obtain_coverage_for_method (MonoProfiler *prof, const MonoProfileCoverageEntry *entry)
3671 {
3672         int offset = entry->iloffset - previous_offset;
3673         CoverageEntry *e = g_new (CoverageEntry, 1);
3674
3675         previous_offset = entry->iloffset;
3676
3677         e->offset = offset;
3678         e->counter = entry->counter;
3679         e->filename = g_strdup(entry->filename ? entry->filename : "");
3680         e->line = entry->line;
3681         e->column = entry->col;
3682
3683         g_ptr_array_add (coverage_data, e);
3684 }
3685
3686 static char *
3687 parse_generic_type_names(char *name)
3688 {
3689         char *new_name, *ret;
3690         int within_generic_declaration = 0, generic_members = 1;
3691
3692         if (name == NULL || *name == '\0')
3693                 return g_strdup ("");
3694
3695         if (!(ret = new_name = (char *)calloc (strlen (name) * 4 + 1, sizeof (char))))
3696                 return NULL;
3697
3698         do {
3699                 switch (*name) {
3700                         case '<':
3701                                 within_generic_declaration = 1;
3702                                 break;
3703
3704                         case '>':
3705                                 within_generic_declaration = 0;
3706
3707                                 if (*(name - 1) != '<') {
3708                                         *new_name++ = '`';
3709                                         *new_name++ = '0' + generic_members;
3710                                 } else {
3711                                         memcpy (new_name, "&lt;&gt;", 8);
3712                                         new_name += 8;
3713                                 }
3714
3715                                 generic_members = 0;
3716                                 break;
3717
3718                         case ',':
3719                                 generic_members++;
3720                                 break;
3721
3722                         default:
3723                                 if (!within_generic_declaration)
3724                                         *new_name++ = *name;
3725
3726                                 break;
3727                 }
3728         } while (*name++);
3729
3730         return ret;
3731 }
3732
3733 static int method_id;
3734 static void
3735 build_method_buffer (gpointer key, gpointer value, gpointer userdata)
3736 {
3737         MonoMethod *method = (MonoMethod *)value;
3738         MonoProfiler *prof = (MonoProfiler *)userdata;
3739         MonoClass *klass;
3740         MonoImage *image;
3741         char *class_name;
3742         const char *image_name, *method_name, *sig, *first_filename;
3743         guint i;
3744
3745         previous_offset = 0;
3746         coverage_data = g_ptr_array_new ();
3747
3748         mono_profiler_coverage_get (prof, method, obtain_coverage_for_method);
3749
3750         klass = mono_method_get_class (method);
3751         image = mono_class_get_image (klass);
3752         image_name = mono_image_get_name (image);
3753
3754         sig = mono_signature_get_desc (mono_method_signature (method), TRUE);
3755         class_name = parse_generic_type_names (mono_type_get_name (mono_class_get_type (klass)));
3756         method_name = mono_method_get_name (method);
3757
3758         if (coverage_data->len != 0) {
3759                 CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[0];
3760                 first_filename = entry->filename ? entry->filename : "";
3761         } else
3762                 first_filename = "";
3763
3764         image_name = image_name ? image_name : "";
3765         sig = sig ? sig : "";
3766         method_name = method_name ? method_name : "";
3767
3768         ENTER_LOG;
3769
3770         LogBuffer *logbuffer = ensure_logbuf (
3771                 EVENT_SIZE /* event */ +
3772                 strlen (image_name) + 1 /* image name */ +
3773                 strlen (class_name) + 1 /* class name */ +
3774                 strlen (method_name) + 1 /* method name */ +
3775                 strlen (sig) + 1 /* signature */ +
3776                 strlen (first_filename) + 1 /* first file name */ +
3777                 LEB128_SIZE /* token */ +
3778                 LEB128_SIZE /* method id */ +
3779                 LEB128_SIZE /* entries */
3780         );
3781
3782         emit_byte (logbuffer, TYPE_COVERAGE_METHOD | TYPE_COVERAGE);
3783         emit_string (logbuffer, image_name, strlen (image_name) + 1);
3784         emit_string (logbuffer, class_name, strlen (class_name) + 1);
3785         emit_string (logbuffer, method_name, strlen (method_name) + 1);
3786         emit_string (logbuffer, sig, strlen (sig) + 1);
3787         emit_string (logbuffer, first_filename, strlen (first_filename) + 1);
3788
3789         emit_uvalue (logbuffer, mono_method_get_token (method));
3790         emit_uvalue (logbuffer, method_id);
3791         emit_value (logbuffer, coverage_data->len);
3792
3793         EXIT_LOG;
3794
3795         safe_send (prof);
3796
3797         for (i = 0; i < coverage_data->len; i++) {
3798                 CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[i];
3799
3800                 ENTER_LOG;
3801
3802                 LogBuffer *logbuffer = ensure_logbuf (
3803                         EVENT_SIZE /* event */ +
3804                         LEB128_SIZE /* method id */ +
3805                         LEB128_SIZE /* offset */ +
3806                         LEB128_SIZE /* counter */ +
3807                         LEB128_SIZE /* line */ +
3808                         LEB128_SIZE /* column */
3809                 );
3810
3811                 emit_byte (logbuffer, TYPE_COVERAGE_STATEMENT | TYPE_COVERAGE);
3812                 emit_uvalue (logbuffer, method_id);
3813                 emit_uvalue (logbuffer, entry->offset);
3814                 emit_uvalue (logbuffer, entry->counter);
3815                 emit_uvalue (logbuffer, entry->line);
3816                 emit_uvalue (logbuffer, entry->column);
3817
3818                 EXIT_LOG;
3819
3820                 safe_send (prof);
3821         }
3822
3823         method_id++;
3824
3825         g_free (class_name);
3826
3827         g_ptr_array_foreach (coverage_data, free_coverage_entry, NULL);
3828         g_ptr_array_free (coverage_data, TRUE);
3829         coverage_data = NULL;
3830 }
3831
3832 /* This empties the queue */
3833 static guint
3834 count_queue (MonoLockFreeQueue *queue)
3835 {
3836         MonoLockFreeQueueNode *node;
3837         guint count = 0;
3838
3839         while ((node = mono_lock_free_queue_dequeue (queue))) {
3840                 count++;
3841                 mono_thread_hazardous_try_free (node, free);
3842         }
3843
3844         return count;
3845 }
3846
3847 static void
3848 build_class_buffer (gpointer key, gpointer value, gpointer userdata)
3849 {
3850         MonoClass *klass = (MonoClass *)key;
3851         MonoLockFreeQueue *class_methods = (MonoLockFreeQueue *)value;
3852         MonoProfiler *prof = (MonoProfiler *)userdata;
3853         MonoImage *image;
3854         char *class_name;
3855         const char *assembly_name;
3856         int number_of_methods, partially_covered;
3857         guint fully_covered;
3858
3859         image = mono_class_get_image (klass);
3860         assembly_name = mono_image_get_name (image);
3861         class_name = mono_type_get_name (mono_class_get_type (klass));
3862
3863         assembly_name = assembly_name ? assembly_name : "";
3864         number_of_methods = mono_class_num_methods (klass);
3865         fully_covered = count_queue (class_methods);
3866         /* We don't handle partial covered yet */
3867         partially_covered = 0;
3868
3869         ENTER_LOG;
3870
3871         LogBuffer *logbuffer = ensure_logbuf (
3872                 EVENT_SIZE /* event */ +
3873                 strlen (assembly_name) + 1 /* assembly name */ +
3874                 strlen (class_name) + 1 /* class name */ +
3875                 LEB128_SIZE /* no. methods */ +
3876                 LEB128_SIZE /* fully covered */ +
3877                 LEB128_SIZE /* partially covered */
3878         );
3879
3880         emit_byte (logbuffer, TYPE_COVERAGE_CLASS | TYPE_COVERAGE);
3881         emit_string (logbuffer, assembly_name, strlen (assembly_name) + 1);
3882         emit_string (logbuffer, class_name, strlen (class_name) + 1);
3883         emit_uvalue (logbuffer, number_of_methods);
3884         emit_uvalue (logbuffer, fully_covered);
3885         emit_uvalue (logbuffer, partially_covered);
3886
3887         EXIT_LOG;
3888
3889         safe_send (prof);
3890
3891         g_free (class_name);
3892 }
3893
3894 static void
3895 get_coverage_for_image (MonoImage *image, int *number_of_methods, guint *fully_covered, int *partially_covered)
3896 {
3897         MonoLockFreeQueue *image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
3898
3899         *number_of_methods = mono_image_get_table_rows (image, MONO_TABLE_METHOD);
3900         if (image_methods)
3901                 *fully_covered = count_queue (image_methods);
3902         else
3903                 *fully_covered = 0;
3904
3905         // FIXME: We don't handle partially covered yet.
3906         *partially_covered = 0;
3907 }
3908
3909 static void
3910 build_assembly_buffer (gpointer key, gpointer value, gpointer userdata)
3911 {
3912         MonoAssembly *assembly = (MonoAssembly *)value;
3913         MonoProfiler *prof = (MonoProfiler *)userdata;
3914         MonoImage *image = mono_assembly_get_image (assembly);
3915         const char *name, *guid, *filename;
3916         int number_of_methods = 0, partially_covered = 0;
3917         guint fully_covered = 0;
3918
3919         name = mono_image_get_name (image);
3920         guid = mono_image_get_guid (image);
3921         filename = mono_image_get_filename (image);
3922
3923         name = name ? name : "";
3924         guid = guid ? guid : "";
3925         filename = filename ? filename : "";
3926
3927         get_coverage_for_image (image, &number_of_methods, &fully_covered, &partially_covered);
3928
3929         ENTER_LOG;
3930
3931         LogBuffer *logbuffer = ensure_logbuf (
3932                 EVENT_SIZE /* event */ +
3933                 strlen (name) + 1 /* name */ +
3934                 strlen (guid) + 1 /* guid */ +
3935                 strlen (filename) + 1 /* file name */ +
3936                 LEB128_SIZE /* no. methods */ +
3937                 LEB128_SIZE /* fully covered */ +
3938                 LEB128_SIZE /* partially covered */
3939         );
3940
3941         emit_byte (logbuffer, TYPE_COVERAGE_ASSEMBLY | TYPE_COVERAGE);
3942         emit_string (logbuffer, name, strlen (name) + 1);
3943         emit_string (logbuffer, guid, strlen (guid) + 1);
3944         emit_string (logbuffer, filename, strlen (filename) + 1);
3945         emit_uvalue (logbuffer, number_of_methods);
3946         emit_uvalue (logbuffer, fully_covered);
3947         emit_uvalue (logbuffer, partially_covered);
3948
3949         EXIT_LOG;
3950
3951         safe_send (prof);
3952 }
3953
3954 static void
3955 dump_coverage (MonoProfiler *prof)
3956 {
3957         if (!coverage_initialized)
3958                 return;
3959
3960         COVERAGE_DEBUG(fprintf (stderr, "Coverage: Started dump\n");)
3961         method_id = 0;
3962
3963         mono_os_mutex_lock (&coverage_mutex);
3964         mono_conc_hashtable_foreach (coverage_assemblies, build_assembly_buffer, prof);
3965         mono_conc_hashtable_foreach (coverage_classes, build_class_buffer, prof);
3966         mono_conc_hashtable_foreach (coverage_methods, build_method_buffer, prof);
3967         mono_os_mutex_unlock (&coverage_mutex);
3968
3969         COVERAGE_DEBUG(fprintf (stderr, "Coverage: Finished dump\n");)
3970 }
3971
3972 static void
3973 process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method)
3974 {
3975         MonoClass *klass;
3976         MonoImage *image;
3977
3978         if (!coverage_initialized)
3979                 return;
3980
3981         klass = mono_method_get_class (method);
3982         image = mono_class_get_image (klass);
3983
3984         if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)))
3985                 return;
3986
3987         mono_os_mutex_lock (&coverage_mutex);
3988         mono_conc_hashtable_insert (entered_methods, method, method);
3989         mono_os_mutex_unlock (&coverage_mutex);
3990 }
3991
3992 static MonoLockFreeQueueNode *
3993 create_method_node (MonoMethod *method)
3994 {
3995         MethodNode *node = (MethodNode *)g_malloc (sizeof (MethodNode));
3996         mono_lock_free_queue_node_init ((MonoLockFreeQueueNode *) node, FALSE);
3997         node->method = method;
3998
3999         return (MonoLockFreeQueueNode *) node;
4000 }
4001
4002 static gboolean
4003 coverage_filter (MonoProfiler *prof, MonoMethod *method)
4004 {
4005         MonoError error;
4006         MonoClass *klass;
4007         MonoImage *image;
4008         MonoAssembly *assembly;
4009         MonoMethodHeader *header;
4010         guint32 iflags, flags, code_size;
4011         char *fqn, *classname;
4012         gboolean has_positive, found;
4013         MonoLockFreeQueue *image_methods, *class_methods;
4014         MonoLockFreeQueueNode *node;
4015
4016         if (!coverage_initialized)
4017                 return FALSE;
4018
4019         COVERAGE_DEBUG(fprintf (stderr, "Coverage filter for %s\n", mono_method_get_name (method));)
4020
4021         flags = mono_method_get_flags (method, &iflags);
4022         if ((iflags & 0x1000 /*METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL*/) ||
4023             (flags & 0x2000 /*METHOD_ATTRIBUTE_PINVOKE_IMPL*/)) {
4024                 COVERAGE_DEBUG(fprintf (stderr, "   Internal call or pinvoke - ignoring\n");)
4025                 return FALSE;
4026         }
4027
4028         // Don't need to do anything else if we're already tracking this method
4029         if (mono_conc_hashtable_lookup (coverage_methods, method)) {
4030                 COVERAGE_DEBUG(fprintf (stderr, "   Already tracking\n");)
4031                 return TRUE;
4032         }
4033
4034         klass = mono_method_get_class (method);
4035         image = mono_class_get_image (klass);
4036
4037         // Don't handle coverage for the core assemblies
4038         if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)) != NULL)
4039                 return FALSE;
4040
4041         if (prof->coverage_filters) {
4042                 /* Check already filtered classes first */
4043                 if (mono_conc_hashtable_lookup (filtered_classes, klass)) {
4044                         COVERAGE_DEBUG(fprintf (stderr, "   Already filtered\n");)
4045                         return FALSE;
4046                 }
4047
4048                 classname = mono_type_get_name (mono_class_get_type (klass));
4049
4050                 fqn = g_strdup_printf ("[%s]%s", mono_image_get_name (image), classname);
4051
4052                 COVERAGE_DEBUG(fprintf (stderr, "   Looking for %s in filter\n", fqn);)
4053                 // Check positive filters first
4054                 has_positive = FALSE;
4055                 found = FALSE;
4056                 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
4057                         char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
4058
4059                         if (filter [0] == '+') {
4060                                 filter = &filter [1];
4061
4062                                 COVERAGE_DEBUG(fprintf (stderr, "   Checking against +%s ...", filter);)
4063
4064                                 if (strstr (fqn, filter) != NULL) {
4065                                         COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
4066                                         found = TRUE;
4067                                 } else
4068                                         COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
4069
4070                                 has_positive = TRUE;
4071                         }
4072                 }
4073
4074                 if (has_positive && !found) {
4075                         COVERAGE_DEBUG(fprintf (stderr, "   Positive match was not found\n");)
4076
4077                         mono_os_mutex_lock (&coverage_mutex);
4078                         mono_conc_hashtable_insert (filtered_classes, klass, klass);
4079                         mono_os_mutex_unlock (&coverage_mutex);
4080                         g_free (fqn);
4081                         g_free (classname);
4082
4083                         return FALSE;
4084                 }
4085
4086                 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
4087                         // FIXME: Is substring search sufficient?
4088                         char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
4089                         if (filter [0] == '+')
4090                                 continue;
4091
4092                         // Skip '-'
4093                         filter = &filter [1];
4094                         COVERAGE_DEBUG(fprintf (stderr, "   Checking against -%s ...", filter);)
4095
4096                         if (strstr (fqn, filter) != NULL) {
4097                                 COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
4098
4099                                 mono_os_mutex_lock (&coverage_mutex);
4100                                 mono_conc_hashtable_insert (filtered_classes, klass, klass);
4101                                 mono_os_mutex_unlock (&coverage_mutex);
4102                                 g_free (fqn);
4103                                 g_free (classname);
4104
4105                                 return FALSE;
4106                         } else
4107                                 COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
4108
4109                 }
4110
4111                 g_free (fqn);
4112                 g_free (classname);
4113         }
4114
4115         COVERAGE_DEBUG(fprintf (stderr, "   Handling coverage for %s\n", mono_method_get_name (method));)
4116         header = mono_method_get_header_checked (method, &error);
4117         mono_error_cleanup (&error);
4118
4119         mono_method_header_get_code (header, &code_size, NULL);
4120
4121         assembly = mono_image_get_assembly (image);
4122
4123         // Need to keep the assemblies around for as long as they are kept in the hashtable
4124         // Nunit, for example, has a habit of unloading them before the coverage statistics are
4125         // generated causing a crash. See https://bugzilla.xamarin.com/show_bug.cgi?id=39325
4126         mono_assembly_addref (assembly);
4127
4128         mono_os_mutex_lock (&coverage_mutex);
4129         mono_conc_hashtable_insert (coverage_methods, method, method);
4130         mono_conc_hashtable_insert (coverage_assemblies, assembly, assembly);
4131         mono_os_mutex_unlock (&coverage_mutex);
4132
4133         image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
4134
4135         if (image_methods == NULL) {
4136                 image_methods = (MonoLockFreeQueue *)g_malloc (sizeof (MonoLockFreeQueue));
4137                 mono_lock_free_queue_init (image_methods);
4138                 mono_os_mutex_lock (&coverage_mutex);
4139                 mono_conc_hashtable_insert (image_to_methods, image, image_methods);
4140                 mono_os_mutex_unlock (&coverage_mutex);
4141         }
4142
4143         node = create_method_node (method);
4144         mono_lock_free_queue_enqueue (image_methods, node);
4145
4146         class_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (coverage_classes, klass);
4147
4148         if (class_methods == NULL) {
4149                 class_methods = (MonoLockFreeQueue *)g_malloc (sizeof (MonoLockFreeQueue));
4150                 mono_lock_free_queue_init (class_methods);
4151                 mono_os_mutex_lock (&coverage_mutex);
4152                 mono_conc_hashtable_insert (coverage_classes, klass, class_methods);
4153                 mono_os_mutex_unlock (&coverage_mutex);
4154         }
4155
4156         node = create_method_node (method);
4157         mono_lock_free_queue_enqueue (class_methods, node);
4158
4159         return TRUE;
4160 }
4161
4162 #define LINE_BUFFER_SIZE 4096
4163 /* Max file limit of 128KB */
4164 #define MAX_FILE_SIZE 128 * 1024
4165 static char *
4166 get_file_content (FILE *stream)
4167 {
4168         char *buffer;
4169         ssize_t bytes_read;
4170         long filesize;
4171         int res, offset = 0;
4172
4173         res = fseek (stream, 0, SEEK_END);
4174         if (res < 0)
4175           return NULL;
4176
4177         filesize = ftell (stream);
4178         if (filesize < 0)
4179           return NULL;
4180
4181         res = fseek (stream, 0, SEEK_SET);
4182         if (res < 0)
4183           return NULL;
4184
4185         if (filesize > MAX_FILE_SIZE)
4186           return NULL;
4187
4188         buffer = (char *)g_malloc ((filesize + 1) * sizeof (char));
4189         while ((bytes_read = fread (buffer + offset, 1, LINE_BUFFER_SIZE, stream)) > 0)
4190                 offset += bytes_read;
4191
4192         /* NULL terminate our buffer */
4193         buffer[filesize] = '\0';
4194         return buffer;
4195 }
4196
4197 static char *
4198 get_next_line (char *contents, char **next_start)
4199 {
4200         char *p = contents;
4201
4202         if (p == NULL || *p == '\0') {
4203                 *next_start = NULL;
4204                 return NULL;
4205         }
4206
4207         while (*p != '\n' && *p != '\0')
4208                 p++;
4209
4210         if (*p == '\n') {
4211                 *p = '\0';
4212                 *next_start = p + 1;
4213         } else
4214                 *next_start = NULL;
4215
4216         return contents;
4217 }
4218
4219 static void
4220 init_suppressed_assemblies (void)
4221 {
4222         char *content;
4223         char *line;
4224         FILE *sa_file;
4225
4226         suppressed_assemblies = mono_conc_hashtable_new (g_str_hash, g_str_equal);
4227         sa_file = fopen (SUPPRESSION_DIR "/mono-profiler-log.suppression", "r");
4228         if (sa_file == NULL)
4229                 return;
4230
4231         /* Don't need to free @content as it is referred to by the lines stored in @suppressed_assemblies */
4232         content = get_file_content (sa_file);
4233         if (content == NULL) {
4234                 g_error ("mono-profiler-log.suppression is greater than 128kb - aborting\n");
4235         }
4236
4237         while ((line = get_next_line (content, &content))) {
4238                 line = g_strchomp (g_strchug (line));
4239                 /* No locking needed as we're doing initialization */
4240                 mono_conc_hashtable_insert (suppressed_assemblies, line, line);
4241         }
4242
4243         fclose (sa_file);
4244 }
4245
4246 #endif /* DISABLE_HELPER_THREAD */
4247
4248 static void
4249 coverage_init (MonoProfiler *prof)
4250 {
4251 #ifndef DISABLE_HELPER_THREAD
4252         assert (!coverage_initialized);
4253
4254         COVERAGE_DEBUG(fprintf (stderr, "Coverage initialized\n");)
4255
4256         mono_os_mutex_init (&coverage_mutex);
4257         coverage_methods = mono_conc_hashtable_new (NULL, NULL);
4258         coverage_assemblies = mono_conc_hashtable_new (NULL, NULL);
4259         coverage_classes = mono_conc_hashtable_new (NULL, NULL);
4260         filtered_classes = mono_conc_hashtable_new (NULL, NULL);
4261         entered_methods = mono_conc_hashtable_new (NULL, NULL);
4262         image_to_methods = mono_conc_hashtable_new (NULL, NULL);
4263         init_suppressed_assemblies ();
4264
4265         coverage_initialized = TRUE;
4266 #endif /* DISABLE_HELPER_THREAD */
4267 }
4268
4269 static void
4270 unref_coverage_assemblies (gpointer key, gpointer value, gpointer userdata)
4271 {
4272         MonoAssembly *assembly = (MonoAssembly *)value;
4273         mono_assembly_close (assembly);
4274 }
4275
4276 static void
4277 free_sample_hit (gpointer p)
4278 {
4279         mono_lock_free_free (p, SAMPLE_BLOCK_SIZE);
4280 }
4281
4282 static void
4283 cleanup_reusable_samples (MonoProfiler *prof)
4284 {
4285         SampleHit *sample;
4286
4287         while ((sample = (SampleHit *) mono_lock_free_queue_dequeue (&prof->sample_reuse_queue)))
4288                 mono_thread_hazardous_try_free (sample, free_sample_hit);
4289 }
4290
4291 static void
4292 log_shutdown (MonoProfiler *prof)
4293 {
4294         void *res;
4295
4296         in_shutdown = 1;
4297 #ifndef DISABLE_HELPER_THREAD
4298         counters_and_perfcounters_sample (prof, FALSE);
4299
4300         dump_coverage (prof);
4301
4302         if (prof->command_port) {
4303                 char c = 1;
4304                 ign_res (write (prof->pipes [1], &c, 1));
4305                 pthread_join (prof->helper_thread, &res);
4306         }
4307 #endif
4308 #if USE_PERF_EVENTS
4309         if (perf_data) {
4310                 int i;
4311                 for (i = 0; i < num_perf; ++i)
4312                         read_perf_mmap (prof, i);
4313         }
4314 #endif
4315
4316         /*
4317          * Ensure that we empty the LLS completely, even if some nodes are
4318          * not immediately removed upon calling mono_lls_remove (), by
4319          * iterating until the head is NULL.
4320          */
4321         while (profiler_thread_list.head) {
4322                 MONO_LLS_FOREACH_SAFE (&profiler_thread_list, MonoProfilerThread, thread) {
4323                         remove_thread (prof, thread, FALSE);
4324                 } MONO_LLS_FOREACH_SAFE_END
4325         }
4326
4327         InterlockedWrite (&prof->run_dumper_thread, 0);
4328         mono_os_sem_post (&prof->dumper_queue_sem);
4329         pthread_join (prof->dumper_thread, &res);
4330         mono_os_sem_destroy (&prof->dumper_queue_sem);
4331
4332         InterlockedWrite (&prof->run_writer_thread, 0);
4333         mono_os_sem_post (&prof->writer_queue_sem);
4334         pthread_join (prof->writer_thread, &res);
4335         mono_os_sem_destroy (&prof->writer_queue_sem);
4336
4337         cleanup_reusable_samples (prof);
4338
4339         g_assert (!InterlockedRead (&buffer_rwlock_count) && "Why is the reader count still non-zero?");
4340         g_assert (!InterlockedReadPointer (&buffer_rwlock_exclusive) && "Why does someone still hold the exclusive lock?");
4341
4342 #if defined (HAVE_SYS_ZLIB)
4343         if (prof->gzfile)
4344                 gzclose (prof->gzfile);
4345 #endif
4346         if (prof->pipe_output)
4347                 pclose (prof->file);
4348         else
4349                 fclose (prof->file);
4350
4351         mono_conc_hashtable_destroy (prof->method_table);
4352         mono_os_mutex_destroy (&prof->method_table_mutex);
4353
4354         if (coverage_initialized) {
4355                 mono_os_mutex_lock (&coverage_mutex);
4356                 mono_conc_hashtable_foreach (coverage_assemblies, unref_coverage_assemblies, prof);
4357                 mono_os_mutex_unlock (&coverage_mutex);
4358
4359                 mono_conc_hashtable_destroy (coverage_methods);
4360                 mono_conc_hashtable_destroy (coverage_assemblies);
4361                 mono_conc_hashtable_destroy (coverage_classes);
4362                 mono_conc_hashtable_destroy (filtered_classes);
4363
4364                 mono_conc_hashtable_destroy (entered_methods);
4365                 mono_conc_hashtable_destroy (image_to_methods);
4366                 mono_conc_hashtable_destroy (suppressed_assemblies);
4367                 mono_os_mutex_destroy (&coverage_mutex);
4368         }
4369
4370         PROF_TLS_FREE ();
4371
4372         free (prof);
4373 }
4374
4375 static char*
4376 new_filename (const char* filename)
4377 {
4378         time_t t = time (NULL);
4379         int pid = process_id ();
4380         char pid_buf [16];
4381         char time_buf [16];
4382         char *res, *d;
4383         const char *p;
4384         int count_dates = 0;
4385         int count_pids = 0;
4386         int s_date, s_pid;
4387         struct tm *ts;
4388         for (p = filename; *p; p++) {
4389                 if (*p != '%')
4390                         continue;
4391                 p++;
4392                 if (*p == 't')
4393                         count_dates++;
4394                 else if (*p == 'p')
4395                         count_pids++;
4396                 else if (*p == 0)
4397                         break;
4398         }
4399         if (!count_dates && !count_pids)
4400                 return pstrdup (filename);
4401         snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
4402         ts = gmtime (&t);
4403         snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
4404                 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
4405         s_date = strlen (time_buf);
4406         s_pid = strlen (pid_buf);
4407         d = res = (char *)malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
4408         for (p = filename; *p; p++) {
4409                 if (*p != '%') {
4410                         *d++ = *p;
4411                         continue;
4412                 }
4413                 p++;
4414                 if (*p == 't') {
4415                         strcpy (d, time_buf);
4416                         d += s_date;
4417                         continue;
4418                 } else if (*p == 'p') {
4419                         strcpy (d, pid_buf);
4420                         d += s_pid;
4421                         continue;
4422                 } else if (*p == '%') {
4423                         *d++ = '%';
4424                         continue;
4425                 } else if (*p == 0)
4426                         break;
4427                 *d++ = '%';
4428                 *d++ = *p;
4429         }
4430         *d = 0;
4431         return res;
4432 }
4433
4434 //this is exposed by the JIT, but it's not meant to be a supported API for now.
4435 extern void mono_threads_attach_tools_thread (void);
4436
4437 #ifndef DISABLE_HELPER_THREAD
4438
4439 static void*
4440 helper_thread (void* arg)
4441 {
4442         MonoProfiler* prof = (MonoProfiler *)arg;
4443         int command_socket;
4444         int len;
4445         char buf [64];
4446         MonoThread *thread = NULL;
4447
4448         mono_threads_attach_tools_thread ();
4449         mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler helper");
4450
4451         init_thread (FALSE);
4452
4453         //fprintf (stderr, "Server listening\n");
4454         command_socket = -1;
4455         while (1) {
4456                 fd_set rfds;
4457                 struct timeval tv;
4458                 int max_fd = -1;
4459                 FD_ZERO (&rfds);
4460                 FD_SET (prof->server_socket, &rfds);
4461                 max_fd = prof->server_socket;
4462                 FD_SET (prof->pipes [0], &rfds);
4463                 if (max_fd < prof->pipes [0])
4464                         max_fd = prof->pipes [0];
4465                 if (command_socket >= 0) {
4466                         FD_SET (command_socket, &rfds);
4467                         if (max_fd < command_socket)
4468                                 max_fd = command_socket;
4469                 }
4470 #if USE_PERF_EVENTS
4471                 if (perf_data) {
4472                         int i;
4473                         for ( i = 0; i < num_perf; ++i) {
4474                                 if (perf_data [i].perf_fd < 0)
4475                                         continue;
4476                                 FD_SET (perf_data [i].perf_fd, &rfds);
4477                                 if (max_fd < perf_data [i].perf_fd)
4478                                         max_fd = perf_data [i].perf_fd;
4479                         }
4480                 }
4481 #endif
4482
4483                 counters_and_perfcounters_sample (prof, TRUE);
4484
4485                 buffer_lock_excl ();
4486
4487                 // Periodically flush all thread-local buffers.
4488                 MONO_LLS_FOREACH_SAFE (&profiler_thread_list, MonoProfilerThread, thread) {
4489                         send_buffer (prof, thread);
4490                         init_buffer_state (thread);
4491                 } MONO_LLS_FOREACH_SAFE_END
4492
4493                 buffer_unlock_excl ();
4494
4495                 tv.tv_sec = 1;
4496                 tv.tv_usec = 0;
4497                 len = select (max_fd + 1, &rfds, NULL, NULL, &tv);
4498
4499                 if (len < 0) {
4500                         if (errno == EINTR)
4501                                 continue;
4502
4503                         g_warning ("Error in proflog server: %s", strerror (errno));
4504                         return NULL;
4505                 }
4506
4507                 if (FD_ISSET (prof->pipes [0], &rfds)) {
4508                         char c;
4509                         read (prof->pipes [0], &c, 1);
4510                         if (thread)
4511                                 mono_thread_detach (thread);
4512                         if (do_debug)
4513                                 fprintf (stderr, "helper shutdown\n");
4514 #if USE_PERF_EVENTS
4515                         if (perf_data) {
4516                                 int i;
4517                                 for ( i = 0; i < num_perf; ++i) {
4518                                         if (perf_data [i].perf_fd < 0)
4519                                                 continue;
4520                                         if (FD_ISSET (perf_data [i].perf_fd, &rfds))
4521                                                 read_perf_mmap (prof, i);
4522                                 }
4523                         }
4524 #endif
4525                         safe_send_threadless (prof);
4526                         return NULL;
4527                 }
4528 #if USE_PERF_EVENTS
4529                 if (perf_data) {
4530                         int i;
4531                         for ( i = 0; i < num_perf; ++i) {
4532                                 if (perf_data [i].perf_fd < 0)
4533                                         continue;
4534                                 if (FD_ISSET (perf_data [i].perf_fd, &rfds)) {
4535                                         read_perf_mmap (prof, i);
4536                                         safe_send_threadless (prof);
4537                                 }
4538                         }
4539                 }
4540 #endif
4541                 if (command_socket >= 0 && FD_ISSET (command_socket, &rfds)) {
4542                         len = read (command_socket, buf, sizeof (buf) - 1);
4543                         if (len < 0)
4544                                 continue;
4545                         if (len == 0) {
4546                                 close (command_socket);
4547                                 command_socket = -1;
4548                                 continue;
4549                         }
4550                         buf [len] = 0;
4551                         if (strcmp (buf, "heapshot\n") == 0) {
4552                                 heapshot_requested = 1;
4553                                 //fprintf (stderr, "perform heapshot\n");
4554                                 if (InterlockedRead (&runtime_inited) && !thread) {
4555                                         thread = mono_thread_attach (mono_get_root_domain ());
4556                                         /*fprintf (stderr, "attached\n");*/
4557                                 }
4558                                 if (thread) {
4559                                         process_requests (prof);
4560                                         mono_thread_detach (thread);
4561                                         thread = NULL;
4562                                 }
4563                         }
4564                         continue;
4565                 }
4566                 if (!FD_ISSET (prof->server_socket, &rfds)) {
4567                         continue;
4568                 }
4569                 command_socket = accept (prof->server_socket, NULL, NULL);
4570                 if (command_socket < 0)
4571                         continue;
4572                 //fprintf (stderr, "Accepted connection\n");
4573         }
4574
4575         mono_thread_info_detach ();
4576
4577         return NULL;
4578 }
4579
4580 static int
4581 start_helper_thread (MonoProfiler* prof)
4582 {
4583         struct sockaddr_in server_address;
4584         int r;
4585         socklen_t slen;
4586         if (pipe (prof->pipes) < 0) {
4587                 fprintf (stderr, "Cannot create pipe\n");
4588                 return 0;
4589         }
4590         prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
4591         if (prof->server_socket < 0) {
4592                 fprintf (stderr, "Cannot create server socket\n");
4593                 return 0;
4594         }
4595         memset (&server_address, 0, sizeof (server_address));
4596         server_address.sin_family = AF_INET;
4597         server_address.sin_addr.s_addr = INADDR_ANY;
4598         server_address.sin_port = htons (prof->command_port);
4599         if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
4600                 fprintf (stderr, "Cannot bind server socket, port: %d: %s\n", prof->command_port, strerror (errno));
4601                 close (prof->server_socket);
4602                 return 0;
4603         }
4604         if (listen (prof->server_socket, 1) < 0) {
4605                 fprintf (stderr, "Cannot listen server socket\n");
4606                 close (prof->server_socket);
4607                 return 0;
4608         }
4609         slen = sizeof (server_address);
4610         if (getsockname (prof->server_socket, (struct sockaddr *)&server_address, &slen) == 0) {
4611                 prof->command_port = ntohs (server_address.sin_port);
4612                 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
4613         }
4614
4615         r = pthread_create (&prof->helper_thread, NULL, helper_thread, prof);
4616         if (r) {
4617                 close (prof->server_socket);
4618                 return 0;
4619         }
4620         return 1;
4621 }
4622 #endif
4623
4624 static void
4625 free_writer_entry (gpointer p)
4626 {
4627         mono_lock_free_free (p, WRITER_ENTRY_BLOCK_SIZE);
4628 }
4629
4630 static gboolean
4631 handle_writer_queue_entry (MonoProfiler *prof)
4632 {
4633         WriterQueueEntry *entry;
4634
4635         if ((entry = (WriterQueueEntry *) mono_lock_free_queue_dequeue (&prof->writer_queue))) {
4636                 if (!entry->methods)
4637                         goto no_methods;
4638
4639                 LogBuffer *buf = NULL;
4640
4641                 /*
4642                  * Encode the method events in a temporary log buffer that we
4643                  * flush to disk before the main buffer, ensuring that all
4644                  * methods have metadata emitted before they're referenced.
4645                  *
4646                  * We use a 'proper' thread-local buffer for this as opposed
4647                  * to allocating and freeing a buffer by hand because the call
4648                  * to mono_method_full_name () below may trigger class load
4649                  * events when it retrieves the signature of the method. So a
4650                  * thread-local buffer needs to exist when such events occur.
4651                  */
4652                 for (guint i = 0; i < entry->methods->len; i++) {
4653                         MethodInfo *info = (MethodInfo *) g_ptr_array_index (entry->methods, i);
4654
4655                         if (mono_conc_hashtable_lookup (prof->method_table, info->method))
4656                                 goto free_info; // This method already has metadata emitted.
4657
4658                         /*
4659                          * Other threads use this hash table to get a general
4660                          * idea of whether a method has already been emitted to
4661                          * the stream. Due to the way we add to this table, it
4662                          * can easily happen that multiple threads queue up the
4663                          * same methods, but that's OK since eventually all
4664                          * methods will be in this table and the thread-local
4665                          * method lists will just be empty for the rest of the
4666                          * app's lifetime.
4667                          */
4668                         mono_os_mutex_lock (&prof->method_table_mutex);
4669                         mono_conc_hashtable_insert (prof->method_table, info->method, info->method);
4670                         mono_os_mutex_unlock (&prof->method_table_mutex);
4671
4672                         char *name = mono_method_full_name (info->method, 1);
4673                         int nlen = strlen (name) + 1;
4674                         void *cstart = info->ji ? mono_jit_info_get_code_start (info->ji) : NULL;
4675                         int csize = info->ji ? mono_jit_info_get_code_size (info->ji) : 0;
4676
4677                         buf = ensure_logbuf_unsafe (
4678                                 EVENT_SIZE /* event */ +
4679                                 LEB128_SIZE /* time */ +
4680                                 LEB128_SIZE /* method */ +
4681                                 LEB128_SIZE /* start */ +
4682                                 LEB128_SIZE /* size */ +
4683                                 nlen /* name */
4684                         );
4685
4686                         emit_byte (buf, TYPE_JIT | TYPE_METHOD);
4687                         emit_time (buf, info->time);
4688                         emit_method_inner (buf, info->method);
4689                         emit_ptr (buf, cstart);
4690                         emit_value (buf, csize);
4691
4692                         memcpy (buf->cursor, name, nlen);
4693                         buf->cursor += nlen;
4694
4695                         mono_free (name);
4696
4697                 free_info:
4698                         free (info);
4699                 }
4700
4701                 g_ptr_array_free (entry->methods, TRUE);
4702
4703                 if (buf) {
4704                         dump_buffer_threadless (prof, buf);
4705                         init_buffer_state (PROF_TLS_GET ());
4706                 }
4707
4708         no_methods:
4709                 dump_buffer (prof, entry->buffer);
4710
4711                 mono_thread_hazardous_try_free (entry, free_writer_entry);
4712
4713                 return TRUE;
4714         }
4715
4716         return FALSE;
4717 }
4718
4719 static void *
4720 writer_thread (void *arg)
4721 {
4722         MonoProfiler *prof = (MonoProfiler *)arg;
4723
4724         mono_threads_attach_tools_thread ();
4725         mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler writer");
4726
4727         dump_header (prof);
4728
4729         MonoProfilerThread *thread = init_thread (FALSE);
4730
4731         while (InterlockedRead (&prof->run_writer_thread)) {
4732                 mono_os_sem_wait (&prof->writer_queue_sem, MONO_SEM_FLAGS_NONE);
4733                 handle_writer_queue_entry (prof);
4734         }
4735
4736         /* Drain any remaining entries on shutdown. */
4737         while (handle_writer_queue_entry (prof));
4738
4739         free_buffer (thread->buffer, thread->buffer->size);
4740         deinit_thread (thread);
4741
4742         mono_thread_info_detach ();
4743
4744         return NULL;
4745 }
4746
4747 static int
4748 start_writer_thread (MonoProfiler* prof)
4749 {
4750         InterlockedWrite (&prof->run_writer_thread, 1);
4751
4752         return !pthread_create (&prof->writer_thread, NULL, writer_thread, prof);
4753 }
4754
4755 static void
4756 reuse_sample_hit (gpointer p)
4757 {
4758         SampleHit *sample = p;
4759
4760         mono_lock_free_queue_node_unpoison (&sample->node);
4761         mono_lock_free_queue_enqueue (&sample->prof->sample_reuse_queue, &sample->node);
4762 }
4763
4764 static gboolean
4765 handle_dumper_queue_entry (MonoProfiler *prof)
4766 {
4767         SampleHit *sample;
4768
4769         if ((sample = (SampleHit *) mono_lock_free_queue_dequeue (&prof->dumper_queue))) {
4770                 for (int i = 0; i < sample->count; ++i) {
4771                         MonoMethod *method = sample->frames [i].method;
4772                         MonoDomain *domain = sample->frames [i].domain;
4773                         void *address = sample->frames [i].base_address;
4774
4775                         if (!method) {
4776                                 g_assert (domain && "What happened to the domain pointer?");
4777                                 g_assert (address && "What happened to the instruction pointer?");
4778
4779                                 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *) address);
4780
4781                                 if (ji)
4782                                         sample->frames [i].method = mono_jit_info_get_method (ji);
4783                         }
4784                 }
4785
4786                 LogBuffer *logbuffer = ensure_logbuf_unsafe (
4787                         EVENT_SIZE /* event */ +
4788                         LEB128_SIZE /* type */ +
4789                         LEB128_SIZE /* time */ +
4790                         LEB128_SIZE /* tid */ +
4791                         LEB128_SIZE /* count */ +
4792                         1 * (
4793                                 LEB128_SIZE /* ip */
4794                         ) +
4795                         LEB128_SIZE /* managed count */ +
4796                         sample->count * (
4797                                 LEB128_SIZE /* method */ +
4798                                 LEB128_SIZE /* il offset */ +
4799                                 LEB128_SIZE /* native offset */
4800                         )
4801                 );
4802
4803                 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
4804                 emit_value (logbuffer, sample_type);
4805                 emit_uvalue (logbuffer, prof->startup_time + sample->elapsed * 10000);
4806                 emit_ptr (logbuffer, (void *) sample->tid);
4807                 emit_value (logbuffer, 1);
4808
4809                 // TODO: Actual native unwinding.
4810                 for (int i = 0; i < 1; ++i) {
4811                         emit_ptr (logbuffer, sample->ip);
4812                         add_code_pointer ((uintptr_t) sample->ip);
4813                 }
4814
4815                 /* new in data version 6 */
4816                 emit_uvalue (logbuffer, sample->count);
4817
4818                 for (int i = 0; i < sample->count; ++i) {
4819                         emit_method (prof, logbuffer, sample->frames [i].method);
4820                         emit_svalue (logbuffer, 0); /* il offset will always be 0 from now on */
4821                         emit_svalue (logbuffer, sample->frames [i].offset);
4822                 }
4823
4824                 mono_thread_hazardous_try_free (sample, reuse_sample_hit);
4825
4826                 dump_unmanaged_coderefs (prof);
4827
4828                 if (logbuffer->next)
4829                         safe_send_threadless (prof);
4830         }
4831
4832         return FALSE;
4833 }
4834
4835 static void *
4836 dumper_thread (void *arg)
4837 {
4838         MonoProfiler *prof = (MonoProfiler *)arg;
4839
4840         mono_threads_attach_tools_thread ();
4841         mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler dumper");
4842
4843         MonoProfilerThread *thread = init_thread (FALSE);
4844
4845         while (InterlockedRead (&prof->run_dumper_thread)) {
4846                 mono_os_sem_wait (&prof->dumper_queue_sem, MONO_SEM_FLAGS_NONE);
4847                 handle_dumper_queue_entry (prof);
4848         }
4849
4850         /* Drain any remaining entries on shutdown. */
4851         while (handle_dumper_queue_entry (prof));
4852
4853         safe_send_threadless (prof);
4854         deinit_thread (thread);
4855
4856         mono_thread_info_detach ();
4857
4858         return NULL;
4859 }
4860
4861 static int
4862 start_dumper_thread (MonoProfiler* prof)
4863 {
4864         InterlockedWrite (&prof->run_dumper_thread, 1);
4865
4866         return !pthread_create (&prof->dumper_thread, NULL, dumper_thread, prof);
4867 }
4868
4869 static void
4870 runtime_initialized (MonoProfiler *profiler)
4871 {
4872 #ifndef DISABLE_HELPER_THREAD
4873         if (hs_mode_ondemand || need_helper_thread) {
4874                 if (!start_helper_thread (profiler))
4875                         profiler->command_port = 0;
4876         }
4877 #endif
4878
4879         start_writer_thread (profiler);
4880         start_dumper_thread (profiler);
4881
4882         InterlockedWrite (&runtime_inited, 1);
4883 #ifndef DISABLE_HELPER_THREAD
4884         counters_init (profiler);
4885         counters_sample (profiler, 0, FALSE);
4886 #endif
4887         /* ensure the main thread data and startup are available soon */
4888         safe_send (profiler);
4889 }
4890
4891 static MonoProfiler*
4892 create_profiler (const char *filename, GPtrArray *filters)
4893 {
4894         MonoProfiler *prof;
4895         char *nf;
4896         int force_delete = 0;
4897         prof = (MonoProfiler *)calloc (1, sizeof (MonoProfiler));
4898
4899         prof->command_port = command_port;
4900         if (filename && *filename == '-') {
4901                 force_delete = 1;
4902                 filename++;
4903         }
4904         if (!filename) {
4905                 if (do_report)
4906                         filename = "|mprof-report -";
4907                 else
4908                         filename = "output.mlpd";
4909                 nf = (char*)filename;
4910         } else {
4911                 nf = new_filename (filename);
4912                 if (do_report) {
4913                         int s = strlen (nf) + 32;
4914                         char *p = (char *)malloc (s);
4915                         snprintf (p, s, "|mprof-report '--out=%s' -", nf);
4916                         free (nf);
4917                         nf = p;
4918                 }
4919         }
4920         if (*nf == '|') {
4921                 prof->file = popen (nf + 1, "w");
4922                 prof->pipe_output = 1;
4923         } else if (*nf == '#') {
4924                 int fd = strtol (nf + 1, NULL, 10);
4925                 prof->file = fdopen (fd, "a");
4926         } else {
4927                 if (force_delete)
4928                         unlink (nf);
4929                 prof->file = fopen (nf, "wb");
4930         }
4931         if (!prof->file) {
4932                 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
4933                 exit (1);
4934         }
4935 #if defined (HAVE_SYS_ZLIB)
4936         if (use_zip)
4937                 prof->gzfile = gzdopen (fileno (prof->file), "wb");
4938 #endif
4939 #if USE_PERF_EVENTS
4940         if (sample_type && !do_mono_sample)
4941                 need_helper_thread = setup_perf_event ();
4942         if (!perf_data) {
4943                 /* FIXME: warn if different freq or sample type */
4944                 do_mono_sample = 1;
4945         }
4946 #endif
4947         if (do_mono_sample) {
4948                 need_helper_thread = 1;
4949         }
4950         if (do_counters && !need_helper_thread) {
4951                 need_helper_thread = 1;
4952         }
4953
4954         /*
4955          * If you hit this assert while increasing MAX_FRAMES, you need to increase
4956          * SAMPLE_BLOCK_SIZE as well.
4957          */
4958         g_assert (SAMPLE_SLOT_SIZE (MAX_FRAMES) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (SAMPLE_BLOCK_SIZE));
4959
4960         // FIXME: We should free this stuff too.
4961         mono_lock_free_allocator_init_size_class (&prof->sample_size_class, SAMPLE_SLOT_SIZE (num_frames), SAMPLE_BLOCK_SIZE);
4962         mono_lock_free_allocator_init_allocator (&prof->sample_allocator, &prof->sample_size_class);
4963
4964         mono_lock_free_queue_init (&prof->sample_reuse_queue);
4965
4966 #ifdef DISABLE_HELPER_THREAD
4967         if (hs_mode_ondemand)
4968                 fprintf (stderr, "Ondemand heapshot unavailable on this arch.\n");
4969
4970         if (do_coverage)
4971                 fprintf (stderr, "Coverage unavailable on this arch.\n");
4972
4973 #endif
4974
4975         g_assert (sizeof (WriterQueueEntry) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (WRITER_ENTRY_BLOCK_SIZE));
4976
4977         // FIXME: We should free this stuff too.
4978         mono_lock_free_allocator_init_size_class (&prof->writer_entry_size_class, sizeof (WriterQueueEntry), WRITER_ENTRY_BLOCK_SIZE);
4979         mono_lock_free_allocator_init_allocator (&prof->writer_entry_allocator, &prof->writer_entry_size_class);
4980
4981         mono_lock_free_queue_init (&prof->writer_queue);
4982         mono_os_sem_init (&prof->writer_queue_sem, 0);
4983
4984         mono_lock_free_queue_init (&prof->dumper_queue);
4985         mono_os_sem_init (&prof->dumper_queue_sem, 0);
4986
4987         mono_os_mutex_init (&prof->method_table_mutex);
4988         prof->method_table = mono_conc_hashtable_new (NULL, NULL);
4989
4990         if (do_coverage)
4991                 coverage_init (prof);
4992         prof->coverage_filters = filters;
4993
4994         prof->startup_time = current_time ();
4995         return prof;
4996 }
4997
4998 static void
4999 usage (int do_exit)
5000 {
5001         printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
5002         printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
5003         printf ("Options:\n");
5004         printf ("\thelp                 show this usage info\n");
5005         printf ("\t[no]alloc            enable/disable recording allocation info\n");
5006         printf ("\t[no]calls            enable/disable recording enter/leave method events\n");
5007         printf ("\theapshot[=MODE]      record heap shot info (by default at each major collection)\n");
5008         printf ("\t                     MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
5009         printf ("\tcounters             sample counters every 1s\n");
5010         printf ("\tsample[=TYPE]        use statistical sampling mode (by default cycles/100)\n");
5011         printf ("\t                     TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
5012         printf ("\t                     TYPE can be followed by /FREQUENCY\n");
5013         printf ("\ttime=fast            use a faster (but more inaccurate) timer\n");
5014         printf ("\tmaxframes=NUM        collect up to NUM stack frames\n");
5015         printf ("\tcalldepth=NUM        ignore method events for call chain depth bigger than NUM\n");
5016         printf ("\toutput=FILENAME      write the data to file FILENAME (-FILENAME to overwrite)\n");
5017         printf ("\toutput=|PROGRAM      write the data to the stdin of PROGRAM\n");
5018         printf ("\t                     %%t is subtituted with date and time, %%p with the pid\n");
5019         printf ("\treport               create a report instead of writing the raw data to a file\n");
5020         printf ("\tzip                  compress the output data\n");
5021         printf ("\tport=PORTNUM         use PORTNUM for the listening command server\n");
5022         printf ("\tcoverage             enable collection of code coverage data\n");
5023         printf ("\tcovfilter=ASSEMBLY   add an assembly to the code coverage filters\n");
5024         printf ("\t                     add a + to include the assembly or a - to exclude it\n");
5025         printf ("\t                     filter=-mscorlib\n");
5026         printf ("\tcovfilter-file=FILE  use FILE to generate the list of assemblies to be filtered\n");
5027         if (do_exit)
5028                 exit (1);
5029 }
5030
5031 static const char*
5032 match_option (const char* p, const char *opt, char **rval)
5033 {
5034         int len = strlen (opt);
5035         if (strncmp (p, opt, len) == 0) {
5036                 if (rval) {
5037                         if (p [len] == '=' && p [len + 1]) {
5038                                 const char *opt = p + len + 1;
5039                                 const char *end = strchr (opt, ',');
5040                                 char *val;
5041                                 int l;
5042                                 if (end == NULL) {
5043                                         l = strlen (opt);
5044                                 } else {
5045                                         l = end - opt;
5046                                 }
5047                                 val = (char *)malloc (l + 1);
5048                                 memcpy (val, opt, l);
5049                                 val [l] = 0;
5050                                 *rval = val;
5051                                 return opt + l;
5052                         }
5053                         if (p [len] == 0 || p [len] == ',') {
5054                                 *rval = NULL;
5055                                 return p + len + (p [len] == ',');
5056                         }
5057                         usage (1);
5058                 } else {
5059                         if (p [len] == 0)
5060                                 return p + len;
5061                         if (p [len] == ',')
5062                                 return p + len + 1;
5063                 }
5064         }
5065         return p;
5066 }
5067
5068 typedef struct {
5069         const char *name;
5070         int sample_mode;
5071 } SampleMode;
5072
5073 static const SampleMode sample_modes [] = {
5074         {"cycles", SAMPLE_CYCLES},
5075         {"instr", SAMPLE_INSTRUCTIONS},
5076         {"cachemiss", SAMPLE_CACHE_MISSES},
5077         {"cacherefs", SAMPLE_CACHE_REFS},
5078         {"branches", SAMPLE_BRANCHES},
5079         {"branchmiss", SAMPLE_BRANCH_MISSES},
5080         {NULL, 0}
5081 };
5082
5083 static void
5084 set_sample_mode (char* val, int allow_empty)
5085 {
5086         char *end;
5087         char *maybe_freq = NULL;
5088         unsigned int count;
5089         const SampleMode *smode = sample_modes;
5090 #ifndef USE_PERF_EVENTS
5091         do_mono_sample = 1;
5092 #endif
5093         if (allow_empty && !val) {
5094                 sample_type = SAMPLE_CYCLES;
5095                 sample_freq = 100;
5096                 return;
5097         }
5098         if (strcmp (val, "mono") == 0) {
5099                 do_mono_sample = 1;
5100                 sample_type = SAMPLE_CYCLES;
5101                 free (val);
5102                 return;
5103         }
5104         for (smode = sample_modes; smode->name; smode++) {
5105                 int l = strlen (smode->name);
5106                 if (strncmp (val, smode->name, l) == 0) {
5107                         sample_type = smode->sample_mode;
5108                         maybe_freq = val + l;
5109                         break;
5110                 }
5111         }
5112         if (!smode->name)
5113                 usage (1);
5114         if (*maybe_freq == '/') {
5115                 count = strtoul (maybe_freq + 1, &end, 10);
5116                 if (maybe_freq + 1 == end)
5117                         usage (1);
5118                 sample_freq = count;
5119         } else if (*maybe_freq != 0) {
5120                 usage (1);
5121         } else {
5122                 sample_freq = 100;
5123         }
5124         free (val);
5125 }
5126
5127 static void
5128 set_hsmode (char* val, int allow_empty)
5129 {
5130         char *end;
5131         unsigned int count;
5132         if (allow_empty && !val)
5133                 return;
5134         if (strcmp (val, "ondemand") == 0) {
5135                 hs_mode_ondemand = 1;
5136                 free (val);
5137                 return;
5138         }
5139         count = strtoul (val, &end, 10);
5140         if (val == end)
5141                 usage (1);
5142         if (strcmp (end, "ms") == 0)
5143                 hs_mode_ms = count;
5144         else if (strcmp (end, "gc") == 0)
5145                 hs_mode_gc = count;
5146         else
5147                 usage (1);
5148         free (val);
5149 }
5150
5151 /*
5152  * declaration to silence the compiler: this is the entry point that
5153  * mono will load from the shared library and call.
5154  */
5155 extern void
5156 mono_profiler_startup (const char *desc);
5157
5158 extern void
5159 mono_profiler_startup_log (const char *desc);
5160
5161 /*
5162  * this is the entry point that will be used when the profiler
5163  * is embedded inside the main executable.
5164  */
5165 void
5166 mono_profiler_startup_log (const char *desc)
5167 {
5168         mono_profiler_startup (desc);
5169 }
5170
5171 void
5172 mono_profiler_startup (const char *desc)
5173 {
5174         MonoProfiler *prof;
5175         GPtrArray *filters = NULL;
5176         char *filename = NULL;
5177         const char *p;
5178         const char *opt;
5179         int fast_time = 0;
5180         int calls_enabled = 0;
5181         int allocs_enabled = 0;
5182         int only_counters = 0;
5183         int only_coverage = 0;
5184         int events = MONO_PROFILE_GC|MONO_PROFILE_ALLOCATIONS|
5185                 MONO_PROFILE_GC_MOVES|MONO_PROFILE_CLASS_EVENTS|MONO_PROFILE_THREADS|
5186                 MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_EXCEPTIONS|
5187                 MONO_PROFILE_MONITOR_EVENTS|MONO_PROFILE_MODULE_EVENTS|MONO_PROFILE_GC_ROOTS|
5188                 MONO_PROFILE_INS_COVERAGE|MONO_PROFILE_APPDOMAIN_EVENTS|MONO_PROFILE_CONTEXT_EVENTS|
5189                 MONO_PROFILE_ASSEMBLY_EVENTS;
5190
5191         max_allocated_sample_hits = mono_cpu_count () * 1000;
5192
5193         mono_counters_register ("Sample hits", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &sample_hits);
5194         mono_counters_register ("Sample flushes", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &sample_flushes);
5195         mono_counters_register ("Sample events allocated", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &sample_allocations);
5196         mono_counters_register ("Log buffers allocated", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &buffer_allocations);
5197         mono_counters_register ("Thread start events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &thread_starts);
5198         mono_counters_register ("Thread stop events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &thread_ends);
5199         mono_counters_register ("Domain load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &domain_loads);
5200         mono_counters_register ("Domain unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &domain_unloads);
5201         mono_counters_register ("Context load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &context_loads);
5202         mono_counters_register ("Context unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &context_unloads);
5203         mono_counters_register ("Assembly load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &assembly_loads);
5204         mono_counters_register ("Assembly unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &assembly_unloads);
5205         mono_counters_register ("Image load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &image_loads);
5206         mono_counters_register ("Image unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &image_unloads);
5207         mono_counters_register ("Class load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &class_loads);
5208         mono_counters_register ("Class unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &class_unloads);
5209
5210         p = desc;
5211         if (strncmp (p, "log", 3))
5212                 usage (1);
5213         p += 3;
5214         if (*p == ':')
5215                 p++;
5216         for (; *p; p = opt) {
5217                 char *val;
5218                 if (*p == ',') {
5219                         opt = p + 1;
5220                         continue;
5221                 }
5222                 if ((opt = match_option (p, "help", NULL)) != p) {
5223                         usage (0);
5224                         continue;
5225                 }
5226                 if ((opt = match_option (p, "calls", NULL)) != p) {
5227                         calls_enabled = 1;
5228                         continue;
5229                 }
5230                 if ((opt = match_option (p, "nocalls", NULL)) != p) {
5231                         events &= ~MONO_PROFILE_ENTER_LEAVE;
5232                         nocalls = 1;
5233                         continue;
5234                 }
5235                 if ((opt = match_option (p, "alloc", NULL)) != p) {
5236                         allocs_enabled = 1;
5237                         continue;
5238                 }
5239                 if ((opt = match_option (p, "noalloc", NULL)) != p) {
5240                         events &= ~MONO_PROFILE_ALLOCATIONS;
5241                         continue;
5242                 }
5243                 if ((opt = match_option (p, "time", &val)) != p) {
5244                         if (strcmp (val, "fast") == 0)
5245                                 fast_time = 1;
5246                         else if (strcmp (val, "null") == 0)
5247                                 fast_time = 2;
5248                         else
5249                                 usage (1);
5250                         free (val);
5251                         continue;
5252                 }
5253                 if ((opt = match_option (p, "report", NULL)) != p) {
5254                         do_report = 1;
5255                         continue;
5256                 }
5257                 if ((opt = match_option (p, "debug", NULL)) != p) {
5258                         do_debug = 1;
5259                         continue;
5260                 }
5261                 if ((opt = match_option (p, "sampling-real", NULL)) != p) {
5262                         sampling_mode = MONO_PROFILER_STAT_MODE_REAL;
5263                         continue;
5264                 }
5265                 if ((opt = match_option (p, "sampling-process", NULL)) != p) {
5266                         sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
5267                         continue;
5268                 }
5269                 if ((opt = match_option (p, "heapshot", &val)) != p) {
5270                         events &= ~MONO_PROFILE_ALLOCATIONS;
5271                         events &= ~MONO_PROFILE_ENTER_LEAVE;
5272                         nocalls = 1;
5273                         do_heap_shot = 1;
5274                         set_hsmode (val, 1);
5275                         continue;
5276                 }
5277                 if ((opt = match_option (p, "sample", &val)) != p) {
5278                         events &= ~MONO_PROFILE_ALLOCATIONS;
5279                         events &= ~MONO_PROFILE_ENTER_LEAVE;
5280                         nocalls = 1;
5281                         set_sample_mode (val, 1);
5282                         continue;
5283                 }
5284                 if ((opt = match_option (p, "hsmode", &val)) != p) {
5285                         fprintf (stderr, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
5286                         set_hsmode (val, 0);
5287                         continue;
5288                 }
5289                 if ((opt = match_option (p, "zip", NULL)) != p) {
5290                         use_zip = 1;
5291                         continue;
5292                 }
5293                 if ((opt = match_option (p, "output", &val)) != p) {
5294                         filename = val;
5295                         continue;
5296                 }
5297                 if ((opt = match_option (p, "port", &val)) != p) {
5298                         char *end;
5299                         command_port = strtoul (val, &end, 10);
5300                         free (val);
5301                         continue;
5302                 }
5303                 if ((opt = match_option (p, "maxframes", &val)) != p) {
5304                         char *end;
5305                         num_frames = strtoul (val, &end, 10);
5306                         if (num_frames > MAX_FRAMES)
5307                                 num_frames = MAX_FRAMES;
5308                         free (val);
5309                         notraces = num_frames == 0;
5310                         continue;
5311                 }
5312                 if ((opt = match_option (p, "maxsamples", &val)) != p) {
5313                         char *end;
5314                         max_allocated_sample_hits = strtoul (val, &end, 10);
5315                         if (!max_allocated_sample_hits)
5316                                 max_allocated_sample_hits = G_MAXINT32;
5317                         free (val);
5318                         continue;
5319                 }
5320                 if ((opt = match_option (p, "calldepth", &val)) != p) {
5321                         char *end;
5322                         max_call_depth = strtoul (val, &end, 10);
5323                         free (val);
5324                         continue;
5325                 }
5326                 if ((opt = match_option (p, "counters", NULL)) != p) {
5327                         do_counters = 1;
5328                         continue;
5329                 }
5330                 if ((opt = match_option (p, "countersonly", NULL)) != p) {
5331                         only_counters = 1;
5332                         continue;
5333                 }
5334                 if ((opt = match_option (p, "coverage", NULL)) != p) {
5335                         do_coverage = 1;
5336                         events |= MONO_PROFILE_ENTER_LEAVE;
5337                         debug_coverage = (g_getenv ("MONO_PROFILER_DEBUG_COVERAGE") != NULL);
5338                         continue;
5339                 }
5340                 if ((opt = match_option (p, "onlycoverage", NULL)) != p) {
5341                         only_coverage = 1;
5342                         continue;
5343                 }
5344                 if ((opt = match_option (p, "covfilter-file", &val)) != p) {
5345                         FILE *filter_file;
5346                         char *line, *content;
5347
5348                         if (filters == NULL)
5349                                 filters = g_ptr_array_new ();
5350
5351                         filter_file = fopen (val, "r");
5352                         if (filter_file == NULL) {
5353                                 fprintf (stderr, "Unable to open %s\n", val);
5354                                 exit (0);
5355                         }
5356
5357                         /* Don't need to free content as it is referred to by the lines stored in @filters */
5358                         content = get_file_content (filter_file);
5359                         if (content == NULL)
5360                                 fprintf (stderr, "WARNING: %s is greater than 128kb - ignoring\n", val);
5361
5362                         while ((line = get_next_line (content, &content)))
5363                                 g_ptr_array_add (filters, g_strchug (g_strchomp (line)));
5364
5365                         fclose (filter_file);
5366                         continue;
5367                 }
5368                 if ((opt = match_option (p, "covfilter", &val)) != p) {
5369                         if (filters == NULL)
5370                                 filters = g_ptr_array_new ();
5371
5372                         g_ptr_array_add (filters, val);
5373                         continue;
5374                 }
5375                 if (opt == p) {
5376                         usage (0);
5377                         exit (0);
5378                 }
5379         }
5380         if (calls_enabled) {
5381                 events |= MONO_PROFILE_ENTER_LEAVE;
5382                 nocalls = 0;
5383         }
5384         if (allocs_enabled)
5385                 events |= MONO_PROFILE_ALLOCATIONS;
5386         if (only_counters)
5387                 events = 0;
5388         if (only_coverage)
5389                 events = MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_INS_COVERAGE;
5390
5391         utils_init (fast_time);
5392
5393         PROF_TLS_INIT ();
5394
5395         prof = create_profiler (filename, filters);
5396         if (!prof) {
5397                 PROF_TLS_FREE ();
5398                 return;
5399         }
5400
5401         mono_lls_init (&profiler_thread_list, NULL);
5402
5403         init_thread (TRUE);
5404
5405         mono_profiler_install (prof, log_shutdown);
5406         mono_profiler_install_gc (gc_event, gc_resize);
5407         mono_profiler_install_allocation (gc_alloc);
5408         mono_profiler_install_gc_moves (gc_moves);
5409         mono_profiler_install_gc_roots (gc_handle, gc_roots);
5410         mono_profiler_install_appdomain (NULL, domain_loaded, domain_unloaded, NULL);
5411         mono_profiler_install_appdomain_name (domain_name);
5412         mono_profiler_install_context (context_loaded, context_unloaded);
5413         mono_profiler_install_class (NULL, class_loaded, class_unloaded, NULL);
5414         mono_profiler_install_module (NULL, image_loaded, image_unloaded, NULL);
5415         mono_profiler_install_assembly (NULL, assembly_loaded, assembly_unloaded, NULL);
5416         mono_profiler_install_thread (thread_start, thread_end);
5417         mono_profiler_install_thread_name (thread_name);
5418         mono_profiler_install_enter_leave (method_enter, method_leave);
5419         mono_profiler_install_jit_end (method_jitted);
5420         mono_profiler_install_code_buffer_new (code_buffer_new);
5421         mono_profiler_install_exception (throw_exc, method_exc_leave, clause_exc);
5422         mono_profiler_install_monitor (monitor_event);
5423         mono_profiler_install_runtime_initialized (runtime_initialized);
5424         if (do_coverage)
5425                 mono_profiler_install_coverage_filter (coverage_filter);
5426
5427         if (do_mono_sample && sample_type == SAMPLE_CYCLES && !only_counters) {
5428                 events |= MONO_PROFILE_STATISTICAL;
5429                 mono_profiler_set_statistical_mode (sampling_mode, sample_freq);
5430                 mono_profiler_install_statistical (mono_sample_hit);
5431         }
5432
5433         mono_profiler_set_events ((MonoProfileFlags)events);
5434 }