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