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