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