60a38f2860cf69f3f0814650ed17a7c20b923f32
[mono.git] / mono / profiler / proflog.c
1 /*
2  * proflog.c: mono log profiler
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2010 Novell, Inc (http://www.novell.com)
8  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
9  */
10
11 #include <config.h>
12 #include "../mini/jit.h"
13 #include <mono/metadata/profiler.h>
14 #include <mono/metadata/threads.h>
15 #include <mono/metadata/mono-gc.h>
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/metadata/mono-perfcounters.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/utils/atomic.h>
20 #include <mono/utils/mono-membar.h>
21 #include <mono/utils/mono-counters.h>
22 #include <mono/utils/mono-mutex.h>
23 #include <mono/utils/mono-conc-hashtable.h>
24 #include <mono/utils/lock-free-queue.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <glib.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <fcntl.h>
33 #include <errno.h>
34 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
35 #define DISABLE_HELPER_THREAD 1
36 #endif
37
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41 #ifdef HAVE_DLFCN_H
42 #include <dlfcn.h>
43 #endif
44 #ifdef HAVE_EXECINFO_H
45 #include <execinfo.h>
46 #endif
47 #ifdef HAVE_LINK_H
48 #include <link.h>
49 #endif
50
51 #ifndef DISABLE_HELPER_THREAD
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <sys/select.h>
56 #endif
57
58 #ifdef HOST_WIN32
59 #include <windows.h>
60 #else
61 #include <pthread.h>
62 #endif
63
64 #ifdef HAVE_SYS_STAT_H
65 #include <sys/stat.h>
66 #endif
67
68 #include "utils.c"
69 #include "proflog.h"
70
71 #if defined (HAVE_SYS_ZLIB)
72 #include <zlib.h>
73 #endif
74
75 #if defined(__linux__)
76
77 #include <unistd.h>
78 #include <sys/syscall.h>
79 #include "perf_event.h"
80
81 #ifdef ENABLE_PERF_EVENTS
82 #define USE_PERF_EVENTS 1
83
84 static int read_perf_mmap (MonoProfiler* prof, int cpu);
85 #endif
86
87 #endif
88
89 #define BUFFER_SIZE (4096 * 16)
90 static int nocalls = 0;
91 static int notraces = 0;
92 static int use_zip = 0;
93 static int do_report = 0;
94 static int do_heap_shot = 0;
95 static int max_call_depth = 100;
96 static volatile int runtime_inited = 0;
97 static int need_helper_thread = 0;
98 static int command_port = 0;
99 static int heapshot_requested = 0;
100 static int sample_type = 0;
101 static int sample_freq = 0;
102 static int do_mono_sample = 0;
103 static int in_shutdown = 0;
104 static int do_debug = 0;
105 static int do_counters = 0;
106 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
107
108 /* For linux compile with:
109  * gcc -fPIC -shared -o libmono-profiler-log.so proflog.c utils.c -Wall -g -lz `pkg-config --cflags --libs mono-2`
110  * gcc -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
111  *
112  * For osx compile with:
113  * gcc -m32 -Dmono_free=free shared -o libmono-profiler-log.dylib proflog.c utils.c -Wall -g -lz `pkg-config --cflags mono-2` -undefined suppress -flat_namespace
114  * gcc -m32 -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
115  *
116  * Install with:
117  * sudo cp mprof-report /usr/local/bin
118  * sudo cp libmono-profiler-log.so /usr/local/lib
119  * sudo ldconfig
120  */
121
122 typedef struct _LogBuffer LogBuffer;
123
124 /*
125  * file format:
126  * [header] [buffer]*
127  *
128  * The file is composed by a header followed by 0 or more buffers.
129  * Each buffer contains events that happened on a thread: for a given thread
130  * buffers that appear later in the file are guaranteed to contain events
131  * that happened later in time. Buffers from separate threads could be interleaved,
132  * though.
133  * Buffers are not required to be aligned.
134  *
135  * header format:
136  * [id: 4 bytes] constant value: LOG_HEADER_ID
137  * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
138  * [format: 1 byte] version of the data format for the rest of the file
139  * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
140  * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
141  * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
142  * [flags: 4 bytes] file format flags, should be 0 for now
143  * [pid: 4 bytes] pid of the profiled process
144  * [port: 2 bytes] tcp port for server if != 0
145  * [sysid: 2 bytes] operating system and architecture identifier
146  *
147  * The multiple byte integers are in little-endian format.
148  *
149  * buffer format:
150  * [buffer header] [event]*
151  * Buffers have a fixed-size header followed by 0 or more bytes of event data.
152  * Timing information and other values in the event data are usually stored
153  * as uleb128 or sleb128 integers. To save space, as noted for each item below,
154  * some data is represented as a difference between the actual value and
155  * either the last value of the same type (like for timing information) or
156  * as the difference from a value stored in a buffer header.
157  *
158  * For timing information the data is stored as uleb128, since timing
159  * increases in a monotonic way in each thread: the value is the number of
160  * nanoseconds to add to the last seen timing data in a buffer. The first value
161  * in a buffer will be calculated from the time_base field in the buffer head.
162  *
163  * Object or heap sizes are stored as uleb128.
164  * Pointer differences are stored as sleb128, instead.
165  *
166  * If an unexpected value is found, the rest of the buffer should be ignored,
167  * as generally the later values need the former to be interpreted correctly.
168  *
169  * buffer header format:
170  * [bufid: 4 bytes] constant value: BUF_ID
171  * [len: 4 bytes] size of the data following the buffer header
172  * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
173  * [ptr_base: 8 bytes] base value for pointers
174  * [obj_base: 8 bytes] base value for object addresses
175  * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
176  * [method_base: 8 bytes] base value for MonoMethod pointers
177  *
178  * event format:
179  * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
180  * The data that follows depends on type and the extended info.
181  * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
182  * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
183  * The extended info bits are interpreted based on type, see
184  * each individual event description below.
185  * strings are represented as a 0-terminated utf8 sequence.
186  *
187  * backtrace format:
188  * [flags: uleb128] must be 0
189  * [num: uleb128] number of frames following
190  * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
191  *
192  * type alloc format:
193  * type: TYPE_ALLOC
194  * exinfo: flags: TYPE_ALLOC_BT
195  * [time diff: uleb128] nanoseconds since last timing
196  * [ptr: sleb128] class as a byte difference from ptr_base
197  * [obj: sleb128] object address as a byte difference from obj_base
198  * [size: uleb128] size of the object in the heap
199  * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
200  *
201  * type GC format:
202  * type: TYPE_GC
203  * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED,
204  * TYPE_GC_HANDLE_DESTROYED
205  * [time diff: uleb128] nanoseconds since last timing
206  * if exinfo == TYPE_GC_RESIZE
207  *      [heap_size: uleb128] new heap size
208  * if exinfo == TYPE_GC_EVENT
209  *      [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
210  *      [generation: uleb128] GC generation event refers to
211  * if exinfo == TYPE_GC_MOVE
212  *      [num_objects: uleb128] number of object moves that follow
213  *      [objaddr: sleb128]+ num_objects object pointer differences from obj_base
214  *      num is always an even number: the even items are the old
215  *      addresses, the odd numbers are the respective new object addresses
216  * if exinfo == TYPE_GC_HANDLE_CREATED
217  *      [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
218  *      upper bits reserved as flags
219  *      [handle: uleb128] GC handle value
220  *      [objaddr: sleb128] object pointer differences from obj_base
221  * if exinfo == TYPE_GC_HANDLE_DESTROYED
222  *      [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
223  *      upper bits reserved as flags
224  *      [handle: uleb128] GC handle value
225  *
226  * type metadata format:
227  * type: TYPE_METADATA
228  * exinfo: flags: TYPE_LOAD_ERR
229  * [time diff: uleb128] nanoseconds since last timing
230  * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
231  * TYPE_THREAD
232  * [pointer: sleb128] pointer of the metadata type depending on mtype
233  * if mtype == TYPE_CLASS
234  *      [image: sleb128] MonoImage* as a pointer difference from ptr_base
235  *      [flags: uleb128] must be 0
236  *      [name: string] full class name
237  * if mtype == TYPE_IMAGE
238  *      [flags: uleb128] must be 0
239  *      [name: string] image file name
240  * if mtype == TYPE_THREAD
241  *      [flags: uleb128] must be 0
242  *      [name: string] thread name
243  *
244  * type method format:
245  * type: TYPE_METHOD
246  * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
247  * [time diff: uleb128] nanoseconds since last timing
248  * [method: sleb128] MonoMethod* as a pointer difference from the last such
249  * pointer or the buffer method_base
250  * if exinfo == TYPE_JIT
251  *      [code address: sleb128] pointer to the native code as a diff from ptr_base
252  *      [code size: uleb128] size of the generated code
253  *      [name: string] full method name
254  *
255  * type runtime format:
256  * type: TYPE_RUNTIME
257  * exinfo: one of: TYPE_JITHELPER
258  * [time diff: uleb128] nanoseconds since last timing
259  * if exinfo == TYPE_JITHELPER
260  *      [type: uleb128] MonoProfilerCodeBufferType enum value
261  *      [buffer address: sleb128] pointer to the native code as a diff from ptr_base
262  *      [buffer size: uleb128] size of the generated code
263  *      if type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
264  *              [name: string] buffer description name
265  *
266  * type monitor format:
267  * type: TYPE_MONITOR
268  * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
269  * [time diff: uleb128] nanoseconds since last timing
270  * [object: sleb128] the lock object as a difference from obj_base
271  * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
272  *      If the TYPE_MONITOR_BT flag is set, a backtrace follows.
273  *
274  * type heap format
275  * type: TYPE_HEAP
276  * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
277  * if exinfo == TYPE_HEAP_START
278  *      [time diff: uleb128] nanoseconds since last timing
279  * if exinfo == TYPE_HEAP_END
280  *      [time diff: uleb128] nanoseconds since last timing
281  * if exinfo == TYPE_HEAP_OBJECT
282  *      [object: sleb128] the object as a difference from obj_base
283  *      [class: sleb128] the object MonoClass* as a difference from ptr_base
284  *      [size: uleb128] size of the object on the heap
285  *      [num_refs: uleb128] number of object references
286  *      if (format version > 1) each referenced objref is preceded by a
287  *      uleb128 encoded offset: the first offset is from the object address
288  *      and each next offset is relative to the previous one
289  *      [objrefs: sleb128]+ object referenced as a difference from obj_base
290  *      The same object can appear multiple times, but only the first time
291  *      with size != 0: in the other cases this data will only be used to
292  *      provide additional referenced objects.
293  * if exinfo == TYPE_HEAP_ROOT
294  *      [num_roots: uleb128] number of root references
295  *      [num_gc: uleb128] number of major gcs
296  *      [object: sleb128] the object as a difference from obj_base
297  *      [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
298  *      [extra_info: uleb128] the extra_info value
299  *      object, root_type and extra_info are repeated num_roots times
300  *
301  * type sample format
302  * type: TYPE_SAMPLE
303  * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN, TYPE_SAMPLE_COUNTERS_DESC, TYPE_SAMPLE_COUNTERS
304  * if exinfo == TYPE_SAMPLE_HIT
305  *      [sample_type: uleb128] type of sample (SAMPLE_*)
306  *      [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
307  *      [count: uleb128] number of following instruction addresses
308  *      [ip: sleb128]* instruction pointer as difference from ptr_base
309  *      if (format_version > 5)
310  *              [mbt_count: uleb128] number of managed backtrace info triplets (method + IL offset + native offset)
311  *              [method: sleb128]* MonoMethod* as a pointer difference from the last such
312  *              pointer or the buffer method_base (the first such method can be also indentified by ip, but this is not neccessarily true)
313  *              [il_offset: sleb128]* IL offset inside method where the hit occurred
314  *              [native_offset: sleb128]* native offset inside method where the hit occurred
315  * if exinfo == TYPE_SAMPLE_USYM
316  *      [address: sleb128] symbol address as a difference from ptr_base
317  *      [size: uleb128] symbol size (may be 0 if unknown)
318  *      [name: string] symbol name
319  * if exinfo == TYPE_SAMPLE_UBIN
320  *      [time diff: uleb128] nanoseconds since last timing
321  *      [address: sleb128] address where binary has been loaded
322  *      [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
323  *      [size: uleb128] memory size
324  *      [name: string] binary name
325  * if exinfo == TYPE_SAMPLE_COUNTERS_DESC
326  *      [len: uleb128] number of counters
327  *      for i = 0 to len
328  *              [section: uleb128] section of counter
329  *              if section == MONO_COUNTER_PERFCOUNTERS:
330  *                      [section_name: string] section name of counter
331  *              [name: string] name of counter
332  *              [type: uleb128] type of counter
333  *              [unit: uleb128] unit of counter
334  *              [variance: uleb128] variance of counter
335  *              [index: uleb128] unique index of counter
336  * if exinfo == TYPE_SAMPLE_COUNTERS
337  *      [timestamp: uleb128] sampling timestamp
338  *      while true:
339  *              [index: uleb128] unique index of counter
340  *              if index == 0:
341  *                      break
342  *              [type: uleb128] type of counter value
343  *              if type == string:
344  *                      if value == null:
345  *                              [0: uleb128] 0 -> value is null
346  *                      else:
347  *                              [1: uleb128] 1 -> value is not null
348  *                              [value: string] counter value
349  *              else:
350  *                      [value: uleb128/sleb128/double] counter value, can be sleb128, uleb128 or double (determined by using type)
351  *
352  */
353 struct _LogBuffer {
354         LogBuffer *next;
355         uint64_t time_base;
356         uint64_t last_time;
357         uintptr_t ptr_base;
358         uintptr_t method_base;
359         uintptr_t last_method;
360         uintptr_t obj_base;
361         uintptr_t thread_id;
362         unsigned char* data_end;
363         unsigned char* data;
364         int locked;
365         int size;
366         int call_depth;
367         unsigned char buf [1];
368 };
369
370 static inline void
371 ign_res (int G_GNUC_UNUSED unused, ...)
372 {
373 }
374
375 #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++;}
376 #define EXIT_LOG(lb) (lb)->locked--;
377
378 typedef struct _StatBuffer StatBuffer;
379 struct _StatBuffer {
380         StatBuffer *next;
381         uintptr_t size;
382         uintptr_t *data_end;
383         uintptr_t *data;
384         uintptr_t buf [1];
385 };
386
387 typedef struct _BinaryObject BinaryObject;
388
389 struct _BinaryObject {
390         BinaryObject *next;
391         void *addr;
392         char *name;
393 };
394
395 struct _MonoProfiler {
396         StatBuffer *stat_buffers;
397         FILE* file;
398 #if defined (HAVE_SYS_ZLIB)
399         gzFile gzfile;
400 #endif
401         uint64_t startup_time;
402         int pipe_output;
403         int last_gc_gen_started;
404         int command_port;
405         int server_socket;
406         int pipes [2];
407 #ifndef HOST_WIN32
408         pthread_t helper_thread;
409         pthread_t writer_thread;
410 #endif
411         volatile gint32 run_writer_thread;
412         MonoLockFreeQueue writer_queue;
413         MonoConcurrentHashTable *method_table;
414         mono_mutex_t method_table_mutex;
415         BinaryObject *binary_objects;
416 };
417
418 typedef struct _WriterQueueEntry WriterQueueEntry;
419 struct _WriterQueueEntry {
420         MonoLockFreeQueueNode node;
421         GPtrArray *methods;
422         LogBuffer *buffer;
423 };
424
425 typedef struct _MethodInfo MethodInfo;
426 struct _MethodInfo {
427         MonoMethod *method;
428         MonoJitInfo *ji;
429 };
430
431 #ifdef TLS_INIT
432 #undef TLS_INIT
433 #endif
434
435 #ifdef HOST_WIN32
436 #define TLS_SET(x,y) (TlsSetValue (x, y))
437 #define TLS_GET(t,x) ((t *) TlsGetValue (x))
438 #define TLS_INIT(x) (x = TlsAlloc ())
439 static int tlsbuffer;
440 static int tlsmethodlist;
441 #elif HAVE_KW_THREAD
442 #define TLS_SET(x,y) (x = y)
443 #define TLS_GET(t,x) (x)
444 #define TLS_INIT(x)
445 static __thread LogBuffer* tlsbuffer = NULL;
446 static __thread GPtrArray* tlsmethodlist = NULL;
447 #else
448 #define TLS_SET(x,y) (pthread_setspecific (x, y))
449 #define TLS_GET(t,x) ((t *) pthread_getspecific (x))
450 #define TLS_INIT(x) (pthread_key_create (&x, NULL))
451 static pthread_key_t tlsbuffer;
452 static pthread_key_t tlsmethodlist;
453 #endif
454
455 static void safe_send (MonoProfiler *profiler, LogBuffer *logbuffer);
456
457 static char*
458 pstrdup (const char *s)
459 {
460         int len = strlen (s) + 1;
461         char *p = malloc (len);
462         memcpy (p, s, len);
463         return p;
464 }
465
466 static StatBuffer*
467 create_stat_buffer (void)
468 {
469         StatBuffer* buf = alloc_buffer (BUFFER_SIZE);
470         buf->size = BUFFER_SIZE;
471         buf->data_end = (uintptr_t*)((unsigned char*)buf + buf->size);
472         buf->data = buf->buf;
473         return buf;
474 }
475
476 static LogBuffer*
477 create_buffer (void)
478 {
479         LogBuffer* buf = alloc_buffer (BUFFER_SIZE);
480         buf->size = BUFFER_SIZE;
481         buf->time_base = current_time ();
482         buf->last_time = buf->time_base;
483         buf->data_end = (unsigned char*)buf + buf->size;
484         buf->data = buf->buf;
485         return buf;
486 }
487
488 static void
489 init_thread (void)
490 {
491         if (!TLS_GET (LogBuffer, tlsbuffer)) {
492                 LogBuffer *logbuffer = create_buffer ();
493                 TLS_SET (tlsbuffer, logbuffer);
494                 logbuffer->thread_id = thread_id ();
495         }
496         if (!TLS_GET (GPtrArray, tlsmethodlist)) {
497                 GPtrArray *methodlist = g_ptr_array_new ();
498                 TLS_SET (tlsmethodlist, methodlist);
499         }
500
501         //printf ("thread %p at time %llu\n", (void*)logbuffer->thread_id, logbuffer->time_base);
502 }
503
504 static LogBuffer *
505 ensure_logbuf_inner (LogBuffer *old, int bytes)
506 {
507         if (old && old->data + bytes + 100 < old->data_end)
508                 return old;
509
510         LogBuffer *new = create_buffer ();
511         new->thread_id = thread_id ();
512         new->next = old;
513
514         if (old)
515                 new->call_depth = old->call_depth;
516
517         return new;
518 }
519
520 static LogBuffer*
521 ensure_logbuf (int bytes)
522 {
523         LogBuffer *old = TLS_GET (LogBuffer, tlsbuffer);
524         LogBuffer *new = ensure_logbuf_inner (old, bytes);
525
526         if (new == old)
527                 return old; // Still enough space.
528
529         TLS_SET (tlsbuffer, new);
530         init_thread ();
531
532         return new;
533 }
534
535 static void
536 emit_byte (LogBuffer *logbuffer, int value)
537 {
538         logbuffer->data [0] = value;
539         logbuffer->data++;
540         assert (logbuffer->data <= logbuffer->data_end);
541 }
542
543 static void
544 emit_value (LogBuffer *logbuffer, int value)
545 {
546         encode_uleb128 (value, logbuffer->data, &logbuffer->data);
547         assert (logbuffer->data <= logbuffer->data_end);
548 }
549
550 static void
551 emit_time (LogBuffer *logbuffer, uint64_t value)
552 {
553         uint64_t tdiff = value - logbuffer->last_time;
554         if (value < logbuffer->last_time)
555                 printf ("time went backwards\n");
556         //if (tdiff > 1000000)
557         //      printf ("large time offset: %llu\n", tdiff);
558         encode_uleb128 (tdiff, logbuffer->data, &logbuffer->data);
559         /*if (tdiff != decode_uleb128 (p, &p))
560                 printf ("incorrect encoding: %llu\n", tdiff);*/
561         logbuffer->last_time = value;
562         assert (logbuffer->data <= logbuffer->data_end);
563 }
564
565 static void
566 emit_svalue (LogBuffer *logbuffer, int64_t value)
567 {
568         encode_sleb128 (value, logbuffer->data, &logbuffer->data);
569         assert (logbuffer->data <= logbuffer->data_end);
570 }
571
572 static void
573 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
574 {
575         encode_uleb128 (value, logbuffer->data, &logbuffer->data);
576         assert (logbuffer->data <= logbuffer->data_end);
577 }
578
579 static void
580 emit_ptr (LogBuffer *logbuffer, void *ptr)
581 {
582         if (!logbuffer->ptr_base)
583                 logbuffer->ptr_base = (uintptr_t)ptr;
584         emit_svalue (logbuffer, (intptr_t)ptr - logbuffer->ptr_base);
585         assert (logbuffer->data <= logbuffer->data_end);
586 }
587
588 static void
589 emit_method_inner (LogBuffer *logbuffer, void *method)
590 {
591         if (!logbuffer->method_base) {
592                 logbuffer->method_base = (intptr_t)method;
593                 logbuffer->last_method = (intptr_t)method;
594         }
595         encode_sleb128 ((intptr_t)((char*)method - (char*)logbuffer->last_method), logbuffer->data, &logbuffer->data);
596         logbuffer->last_method = (intptr_t)method;
597         assert (logbuffer->data <= logbuffer->data_end);
598 }
599
600 typedef struct {
601         MonoMethod *method;
602         MonoJitInfo *found;
603 } MethodSearch;
604
605 static void
606 find_method (MonoDomain *domain, void *user_data)
607 {
608         MethodSearch *search = user_data;
609
610         if (search->found)
611                 return;
612
613         MonoJitInfo *ji = mono_get_jit_info_from_method (domain, search->method);
614
615         // It could be AOT'd, so we need to get it from the AOT runtime's cache.
616         if (!ji) {
617                 void *ip = mono_aot_get_method (domain, search->method);
618
619                 // Avoid a slow path in mono_jit_info_table_find ().
620                 if (ip)
621                         ji = mono_jit_info_table_find (domain, ip);
622         }
623
624         if (ji)
625                 search->found = ji;
626 }
627
628 static void
629 register_method_local (MonoProfiler *prof, MonoDomain *domain, MonoMethod *method, MonoJitInfo *ji)
630 {
631         if (!domain)
632                 g_assert (ji);
633
634         if (!mono_conc_hashtable_lookup (prof->method_table, method)) {
635                 if (!ji) {
636                         MethodSearch search = { method, NULL };
637
638                         mono_domain_foreach (find_method, &search);
639
640                         ji = search.found;
641                 }
642
643                 g_assert (ji);
644
645                 MethodInfo *info = malloc (sizeof (MethodInfo));
646
647                 info->method = method;
648                 info->ji = ji;
649
650                 g_ptr_array_add (TLS_GET (GPtrArray, tlsmethodlist), info);
651         }
652 }
653
654 static void
655 emit_method (MonoProfiler *prof, LogBuffer *logbuffer, MonoDomain *domain, MonoMethod *method)
656 {
657         register_method_local (prof, domain, method, NULL);
658         emit_method_inner (logbuffer, method);
659 }
660
661 static void
662 emit_obj (LogBuffer *logbuffer, void *ptr)
663 {
664         if (!logbuffer->obj_base)
665                 logbuffer->obj_base = (uintptr_t)ptr >> 3;
666         emit_svalue (logbuffer, ((uintptr_t)ptr >> 3) - logbuffer->obj_base);
667         assert (logbuffer->data <= logbuffer->data_end);
668 }
669
670 static void
671 emit_string (LogBuffer *logbuffer, const char *str, size_t size)
672 {
673         size_t i = 0;
674         if (str) {
675                 for (; i < size; i++) {
676                         if (str[i] == '\0')
677                                 break;
678                         emit_byte (logbuffer, str [i]);
679                 }
680         }
681         emit_byte (logbuffer, '\0');
682 }
683
684 static void
685 emit_double (LogBuffer *logbuffer, double value)
686 {
687         int i;
688         unsigned char buffer[8];
689         memcpy (buffer, &value, 8);
690 #if G_BYTE_ORDER == G_BIG_ENDIAN
691         for (i = 7; i >= 0; i--)
692 #else
693         for (i = 0; i < 8; i++)
694 #endif
695                 emit_byte (logbuffer, buffer[i]);
696 }
697
698 static char*
699 write_int16 (char *buf, int32_t value)
700 {
701         int i;
702         for (i = 0; i < 2; ++i) {
703                 buf [i] = value;
704                 value >>= 8;
705         }
706         return buf + 2;
707 }
708
709 static char*
710 write_int32 (char *buf, int32_t value)
711 {
712         int i;
713         for (i = 0; i < 4; ++i) {
714                 buf [i] = value;
715                 value >>= 8;
716         }
717         return buf + 4;
718 }
719
720 static char*
721 write_int64 (char *buf, int64_t value)
722 {
723         int i;
724         for (i = 0; i < 8; ++i) {
725                 buf [i] = value;
726                 value >>= 8;
727         }
728         return buf + 8;
729 }
730
731 static void
732 dump_header (MonoProfiler *profiler)
733 {
734         char hbuf [128];
735         char *p = hbuf;
736         p = write_int32 (p, LOG_HEADER_ID);
737         *p++ = LOG_VERSION_MAJOR;
738         *p++ = LOG_VERSION_MINOR;
739         *p++ = LOG_DATA_VERSION;
740         *p++ = sizeof (void*);
741         p = write_int64 (p, ((uint64_t)time (NULL)) * 1000); /* startup time */
742         p = write_int32 (p, get_timer_overhead ()); /* timer overhead */
743         p = write_int32 (p, 0); /* flags */
744         p = write_int32 (p, process_id ()); /* pid */
745         p = write_int16 (p, profiler->command_port); /* port */
746         p = write_int16 (p, 0); /* opsystem */
747 #if defined (HAVE_SYS_ZLIB)
748         if (profiler->gzfile) {
749                 gzwrite (profiler->gzfile, hbuf, p - hbuf);
750         } else {
751                 fwrite (hbuf, p - hbuf, 1, profiler->file);
752         }
753 #else
754         fwrite (hbuf, p - hbuf, 1, profiler->file);
755         fflush (profiler->file);
756 #endif
757 }
758
759 static void
760 send_buffer (MonoProfiler *prof, GPtrArray *methods, LogBuffer *buffer)
761 {
762         WriterQueueEntry *entry = calloc (1, sizeof (WriterQueueEntry));
763         mono_lock_free_queue_node_init (&entry->node, FALSE);
764         entry->methods = methods;
765         entry->buffer = buffer;
766         mono_lock_free_queue_enqueue (&prof->writer_queue, &entry->node);
767 }
768
769 static void
770 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
771 {
772         char hbuf [128];
773         char *p = hbuf;
774         if (buf->next)
775                 dump_buffer (profiler, buf->next);
776         p = write_int32 (p, BUF_ID);
777         p = write_int32 (p, buf->data - buf->buf);
778         p = write_int64 (p, buf->time_base);
779         p = write_int64 (p, buf->ptr_base);
780         p = write_int64 (p, buf->obj_base);
781         p = write_int64 (p, buf->thread_id);
782         p = write_int64 (p, buf->method_base);
783 #if defined (HAVE_SYS_ZLIB)
784         if (profiler->gzfile) {
785                 gzwrite (profiler->gzfile, hbuf, p - hbuf);
786                 gzwrite (profiler->gzfile, buf->buf, buf->data - buf->buf);
787         } else {
788 #endif
789                 fwrite (hbuf, p - hbuf, 1, profiler->file);
790                 fwrite (buf->buf, buf->data - buf->buf, 1, profiler->file);
791                 fflush (profiler->file);
792 #if defined (HAVE_SYS_ZLIB)
793         }
794 #endif
795         free_buffer (buf, buf->size);
796 }
797
798 static void
799 process_requests (MonoProfiler *profiler)
800 {
801         if (heapshot_requested)
802                 mono_gc_collect (mono_gc_max_generation ());
803 }
804
805 static void counters_init (MonoProfiler *profiler);
806 static void counters_sample (MonoProfiler *profiler, uint64_t timestamp);
807
808 /*
809  * Can be called only at safe callback locations.
810  */
811 static void
812 safe_send (MonoProfiler *profiler, LogBuffer *logbuffer)
813 {
814         /* We need the runtime initialized so that we have threads and hazard
815          * pointers available. Otherwise, the lock free queue will not work and
816          * there won't be a thread to process the data.
817          *
818          * While the runtime isn't initialized, we just accumulate data in the
819          * thread local buffer list.
820          */
821         if (!InterlockedRead (&runtime_inited))
822                 return;
823
824         int cd = logbuffer->call_depth;
825
826         send_buffer (profiler, TLS_GET (GPtrArray, tlsmethodlist), TLS_GET (LogBuffer, tlsbuffer));
827
828         TLS_SET (tlsbuffer, NULL);
829         TLS_SET (tlsmethodlist, NULL);
830
831         init_thread ();
832
833         TLS_GET (LogBuffer, tlsbuffer)->call_depth = cd;
834 }
835
836 static int
837 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
838 {
839         int i;
840         uintptr_t last_offset = 0;
841         //const char *name = mono_class_get_name (klass);
842         LogBuffer *logbuffer = ensure_logbuf (20 + num * 8);
843         emit_byte (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
844         emit_obj (logbuffer, obj);
845         emit_ptr (logbuffer, klass);
846         /* account for object alignment in the heap */
847         size += 7;
848         size &= ~7;
849         emit_value (logbuffer, size);
850         emit_value (logbuffer, num);
851         for (i = 0; i < num; ++i) {
852                 emit_value (logbuffer, offsets [i] - last_offset);
853                 last_offset = offsets [i];
854                 emit_obj (logbuffer, refs [i]);
855         }
856         //if (num)
857         //      printf ("obj: %p, klass: %s, refs: %d, size: %d\n", obj, name, (int)num, (int)size);
858         return 0;
859 }
860
861 static unsigned int hs_mode_ms = 0;
862 static unsigned int hs_mode_gc = 0;
863 static unsigned int hs_mode_ondemand = 0;
864 static unsigned int gc_count = 0;
865 static uint64_t last_hs_time = 0;
866
867 static void
868 heap_walk (MonoProfiler *profiler)
869 {
870         int do_walk = 0;
871         uint64_t now;
872         LogBuffer *logbuffer;
873         if (!do_heap_shot)
874                 return;
875         logbuffer = ensure_logbuf (10);
876         now = current_time ();
877         if (hs_mode_ms && (now - last_hs_time)/1000000 >= hs_mode_ms)
878                 do_walk = 1;
879         else if (hs_mode_gc && (gc_count % hs_mode_gc) == 0)
880                 do_walk = 1;
881         else if (hs_mode_ondemand)
882                 do_walk = heapshot_requested;
883         else if (!hs_mode_ms && !hs_mode_gc && profiler->last_gc_gen_started == mono_gc_max_generation ())
884                 do_walk = 1;
885
886         if (!do_walk)
887                 return;
888         heapshot_requested = 0;
889         emit_byte (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
890         emit_time (logbuffer, now);
891         mono_gc_walk_heap (0, gc_reference, NULL);
892         logbuffer = ensure_logbuf (10);
893         now = current_time ();
894         emit_byte (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
895         emit_time (logbuffer, now);
896         last_hs_time = now;
897 }
898
899 static void
900 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation) {
901         uint64_t now;
902         LogBuffer *logbuffer = ensure_logbuf (10);
903         now = current_time ();
904         ENTER_LOG (logbuffer, "gcevent");
905         emit_byte (logbuffer, TYPE_GC_EVENT | TYPE_GC);
906         emit_time (logbuffer, now);
907         emit_value (logbuffer, ev);
908         emit_value (logbuffer, generation);
909         /* to deal with nested gen1 after gen0 started */
910         if (ev == MONO_GC_EVENT_START) {
911                 profiler->last_gc_gen_started = generation;
912                 if (generation == mono_gc_max_generation ())
913                         gc_count++;
914         }
915         if (ev == MONO_GC_EVENT_PRE_START_WORLD)
916                 heap_walk (profiler);
917         EXIT_LOG (logbuffer);
918         if (ev == MONO_GC_EVENT_POST_START_WORLD)
919                 safe_send (profiler, logbuffer);
920         //printf ("gc event %d for generation %d\n", ev, generation);
921 }
922
923 static void
924 gc_resize (MonoProfiler *profiler, int64_t new_size) {
925         uint64_t now;
926         LogBuffer *logbuffer = ensure_logbuf (10);
927         now = current_time ();
928         ENTER_LOG (logbuffer, "gcresize");
929         emit_byte (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
930         emit_time (logbuffer, now);
931         emit_value (logbuffer, new_size);
932         //printf ("gc resized to %lld\n", new_size);
933         EXIT_LOG (logbuffer);
934 }
935
936 #define MAX_FRAMES 32
937 typedef struct {
938         int count;
939         MonoMethod* methods [MAX_FRAMES];
940         int32_t il_offsets [MAX_FRAMES];
941         int32_t native_offsets [MAX_FRAMES];
942 } FrameData;
943 static int num_frames = MAX_FRAMES;
944
945 static mono_bool
946 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
947 {
948         FrameData *frame = data;
949         if (method && frame->count < num_frames) {
950                 frame->il_offsets [frame->count] = il_offset;
951                 frame->native_offsets [frame->count] = native_offset;
952                 frame->methods [frame->count++] = method;
953                 //printf ("In %d %s at %d (native: %d)\n", frame->count, mono_method_get_name (method), il_offset, native_offset);
954         }
955         return frame->count == num_frames;
956 }
957
958 /*
959  * a note about stack walks: they can cause more profiler events to fire,
960  * so we need to make sure they don't happen after we started emitting an
961  * event, hence the collect_bt/emit_bt split.
962  */
963 static void
964 collect_bt (FrameData *data)
965 {
966         data->count = 0;
967         mono_stack_walk_no_il (walk_stack, data);
968 }
969
970 static void
971 emit_bt (LogBuffer *logbuffer, FrameData *data)
972 {
973         /* FIXME: this is actually tons of data and we should
974          * just output it the first time and use an id the next
975          */
976         if (data->count > num_frames)
977                 printf ("bad num frames: %d\n", data->count);
978         emit_value (logbuffer, 0); /* flags */
979         emit_value (logbuffer, data->count);
980         //if (*p != data.count) {
981         //      printf ("bad num frames enc at %d: %d -> %d\n", count, data.count, *p); printf ("frames end: %p->%p\n", p, logbuffer->data); exit(0);}
982         while (data->count) {
983                 emit_ptr (logbuffer, data->methods [--data->count]);
984         }
985 }
986
987 static void
988 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
989 {
990         uint64_t now;
991         uintptr_t len;
992         int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces)? TYPE_ALLOC_BT: 0;
993         FrameData data;
994         LogBuffer *logbuffer;
995         len = mono_object_get_size (obj);
996         /* account for object alignment in the heap */
997         len += 7;
998         len &= ~7;
999         if (do_bt)
1000                 collect_bt (&data);
1001         logbuffer = ensure_logbuf (32 + MAX_FRAMES * 8);
1002         now = current_time ();
1003         ENTER_LOG (logbuffer, "gcalloc");
1004         emit_byte (logbuffer, do_bt | TYPE_ALLOC);
1005         emit_time (logbuffer, now);
1006         emit_ptr (logbuffer, klass);
1007         emit_obj (logbuffer, obj);
1008         emit_value (logbuffer, len);
1009         if (do_bt)
1010                 emit_bt (logbuffer, &data);
1011         EXIT_LOG (logbuffer);
1012         if (logbuffer->next)
1013                 safe_send (prof, logbuffer);
1014         process_requests (prof);
1015         //printf ("gc alloc %s at %p\n", mono_class_get_name (klass), obj);
1016 }
1017
1018 static void
1019 gc_moves (MonoProfiler *prof, void **objects, int num)
1020 {
1021         int i;
1022         uint64_t now;
1023         LogBuffer *logbuffer = ensure_logbuf (10 + num * 8);
1024         now = current_time ();
1025         ENTER_LOG (logbuffer, "gcmove");
1026         emit_byte (logbuffer, TYPE_GC_MOVE | TYPE_GC);
1027         emit_time (logbuffer, now);
1028         emit_value (logbuffer, num);
1029         for (i = 0; i < num; ++i)
1030                 emit_obj (logbuffer, objects [i]);
1031         //printf ("gc moved %d objects\n", num/2);
1032         EXIT_LOG (logbuffer);
1033 }
1034
1035 static void
1036 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
1037 {
1038         int i;
1039         LogBuffer *logbuffer = ensure_logbuf (5 + num * 18);
1040         ENTER_LOG (logbuffer, "gcroots");
1041         emit_byte (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
1042         emit_value (logbuffer, num);
1043         emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
1044         for (i = 0; i < num; ++i) {
1045                 emit_obj (logbuffer, objects [i]);
1046                 emit_value (logbuffer, root_types [i]);
1047                 emit_value (logbuffer, extra_info [i]);
1048         }
1049         EXIT_LOG (logbuffer);
1050 }
1051
1052 static void
1053 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
1054 {
1055         uint64_t now;
1056         LogBuffer *logbuffer = ensure_logbuf (16);
1057         now = current_time ();
1058         ENTER_LOG (logbuffer, "gchandle");
1059         if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1060                 emit_byte (logbuffer, TYPE_GC_HANDLE_CREATED | TYPE_GC);
1061         else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
1062                 emit_byte (logbuffer, TYPE_GC_HANDLE_DESTROYED | TYPE_GC);
1063         else
1064                 return;
1065         emit_time (logbuffer, now);
1066         emit_value (logbuffer, type);
1067         emit_value (logbuffer, handle);
1068         if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1069                 emit_obj (logbuffer, obj);
1070         EXIT_LOG (logbuffer);
1071         process_requests (prof);
1072 }
1073
1074 static char*
1075 push_nesting (char *p, MonoClass *klass)
1076 {
1077         MonoClass *nesting;
1078         const char *name;
1079         const char *nspace;
1080         nesting = mono_class_get_nesting_type (klass);
1081         if (nesting) {
1082                 p = push_nesting (p, nesting);
1083                 *p++ = '/';
1084                 *p = 0;
1085         }
1086         name = mono_class_get_name (klass);
1087         nspace = mono_class_get_namespace (klass);
1088         if (*nspace) {
1089                 strcpy (p, nspace);
1090                 p += strlen (nspace);
1091                 *p++ = '.';
1092                 *p = 0;
1093         }
1094         strcpy (p, name);
1095         p += strlen (name);
1096         return p;
1097 }
1098
1099 static char*
1100 type_name (MonoClass *klass)
1101 {
1102         char buf [1024];
1103         char *p;
1104         push_nesting (buf, klass);
1105         p = malloc (strlen (buf) + 1);
1106         strcpy (p, buf);
1107         return p;
1108 }
1109
1110 static void
1111 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
1112 {
1113         uint64_t now;
1114         const char *name;
1115         int nlen;
1116         LogBuffer *logbuffer;
1117         if (result != MONO_PROFILE_OK)
1118                 return;
1119         name = mono_image_get_filename (image);
1120         nlen = strlen (name) + 1;
1121         logbuffer = ensure_logbuf (16 + nlen);
1122         now = current_time ();
1123         ENTER_LOG (logbuffer, "image");
1124         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1125         emit_time (logbuffer, now);
1126         emit_byte (logbuffer, TYPE_IMAGE);
1127         emit_ptr (logbuffer, image);
1128         emit_value (logbuffer, 0); /* flags */
1129         memcpy (logbuffer->data, name, nlen);
1130         logbuffer->data += nlen;
1131         //printf ("loaded image %p (%s)\n", image, name);
1132         EXIT_LOG (logbuffer);
1133         if (logbuffer->next)
1134                 safe_send (prof, logbuffer);
1135         process_requests (prof);
1136 }
1137
1138 static void
1139 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
1140 {
1141         uint64_t now;
1142         char *name;
1143         int nlen;
1144         MonoImage *image;
1145         LogBuffer *logbuffer;
1146         if (result != MONO_PROFILE_OK)
1147                 return;
1148         if (InterlockedRead (&runtime_inited))
1149                 name = mono_type_get_name (mono_class_get_type (klass));
1150         else
1151                 name = type_name (klass);
1152         nlen = strlen (name) + 1;
1153         image = mono_class_get_image (klass);
1154         logbuffer = ensure_logbuf (24 + nlen);
1155         now = current_time ();
1156         ENTER_LOG (logbuffer, "class");
1157         emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1158         emit_time (logbuffer, now);
1159         emit_byte (logbuffer, TYPE_CLASS);
1160         emit_ptr (logbuffer, klass);
1161         emit_ptr (logbuffer, image);
1162         emit_value (logbuffer, 0); /* flags */
1163         memcpy (logbuffer->data, name, nlen);
1164         logbuffer->data += nlen;
1165         //printf ("loaded class %p (%s)\n", klass, name);
1166         if (runtime_inited)
1167                 mono_free (name);
1168         else
1169                 free (name);
1170         EXIT_LOG (logbuffer);
1171         if (logbuffer->next)
1172                 safe_send (prof, logbuffer);
1173         process_requests (prof);
1174 }
1175
1176 static void
1177 method_enter (MonoProfiler *prof, MonoMethod *method)
1178 {
1179         uint64_t now;
1180         LogBuffer *logbuffer = ensure_logbuf (16);
1181         if (logbuffer->call_depth++ > max_call_depth)
1182                 return;
1183         now = current_time ();
1184         ENTER_LOG (logbuffer, "enter");
1185         emit_byte (logbuffer, TYPE_ENTER | TYPE_METHOD);
1186         emit_time (logbuffer, now);
1187         emit_method (prof, logbuffer, mono_domain_get (), method);
1188         EXIT_LOG (logbuffer);
1189         process_requests (prof);
1190 }
1191
1192 static void
1193 method_leave (MonoProfiler *prof, MonoMethod *method)
1194 {
1195         uint64_t now;
1196         LogBuffer *logbuffer = ensure_logbuf (16);
1197         if (--logbuffer->call_depth > max_call_depth)
1198                 return;
1199         now = current_time ();
1200         ENTER_LOG (logbuffer, "leave");
1201         emit_byte (logbuffer, TYPE_LEAVE | TYPE_METHOD);
1202         emit_time (logbuffer, now);
1203         emit_method (prof, logbuffer, mono_domain_get (), method);
1204         EXIT_LOG (logbuffer);
1205         if (logbuffer->next)
1206                 safe_send (prof, logbuffer);
1207         process_requests (prof);
1208 }
1209
1210 static void
1211 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
1212 {
1213         uint64_t now;
1214         LogBuffer *logbuffer;
1215         if (nocalls)
1216                 return;
1217         logbuffer = ensure_logbuf (16);
1218         if (--logbuffer->call_depth > max_call_depth)
1219                 return;
1220         now = current_time ();
1221         ENTER_LOG (logbuffer, "eleave");
1222         emit_byte (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
1223         emit_time (logbuffer, now);
1224         emit_method (prof, logbuffer, mono_domain_get (), method);
1225         EXIT_LOG (logbuffer);
1226         process_requests (prof);
1227 }
1228
1229 static void
1230 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji, int result)
1231 {
1232         if (result != MONO_PROFILE_OK)
1233                 return;
1234
1235         register_method_local (prof, NULL, method, ji);
1236 }
1237
1238 static void
1239 code_buffer_new (MonoProfiler *prof, void *buffer, int size, MonoProfilerCodeBufferType type, void *data)
1240 {
1241         uint64_t now;
1242         int nlen;
1243         char *name;
1244         LogBuffer *logbuffer;
1245         if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
1246                 name = data;
1247                 nlen = strlen (name) + 1;
1248         } else {
1249                 name = NULL;
1250                 nlen = 0;
1251         }
1252         logbuffer = ensure_logbuf (32 + nlen);
1253         now = current_time ();
1254         ENTER_LOG (logbuffer, "code buffer");
1255         emit_byte (logbuffer, TYPE_JITHELPER | TYPE_RUNTIME);
1256         emit_time (logbuffer, now);
1257         emit_value (logbuffer, type);
1258         emit_ptr (logbuffer, buffer);
1259         emit_value (logbuffer, size);
1260         if (name) {
1261                 memcpy (logbuffer->data, name, nlen);
1262                 logbuffer->data += nlen;
1263         }
1264         EXIT_LOG (logbuffer);
1265         process_requests (prof);
1266 }
1267
1268 static void
1269 throw_exc (MonoProfiler *prof, MonoObject *object)
1270 {
1271         int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces)? TYPE_EXCEPTION_BT: 0;
1272         uint64_t now;
1273         FrameData data;
1274         LogBuffer *logbuffer;
1275         if (do_bt)
1276                 collect_bt (&data);
1277         logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1278         now = current_time ();
1279         ENTER_LOG (logbuffer, "throw");
1280         emit_byte (logbuffer, do_bt | TYPE_EXCEPTION);
1281         emit_time (logbuffer, now);
1282         emit_obj (logbuffer, object);
1283         if (do_bt)
1284                 emit_bt (logbuffer, &data);
1285         EXIT_LOG (logbuffer);
1286         process_requests (prof);
1287 }
1288
1289 static void
1290 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num)
1291 {
1292         uint64_t now;
1293         LogBuffer *logbuffer = ensure_logbuf (16);
1294         now = current_time ();
1295         ENTER_LOG (logbuffer, "clause");
1296         emit_byte (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
1297         emit_time (logbuffer, now);
1298         emit_value (logbuffer, clause_type);
1299         emit_value (logbuffer, clause_num);
1300         emit_method (prof, logbuffer, mono_domain_get (), method);
1301         EXIT_LOG (logbuffer);
1302 }
1303
1304 static void
1305 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent event)
1306 {
1307         int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces && event == MONO_PROFILER_MONITOR_CONTENTION)? TYPE_MONITOR_BT: 0;
1308         uint64_t now;
1309         FrameData data;
1310         LogBuffer *logbuffer;
1311         if (do_bt)
1312                 collect_bt (&data);
1313         logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1314         now = current_time ();
1315         ENTER_LOG (logbuffer, "monitor");
1316         emit_byte (logbuffer, (event << 4) | do_bt | TYPE_MONITOR);
1317         emit_time (logbuffer, now);
1318         emit_obj (logbuffer, object);
1319         if (do_bt)
1320                 emit_bt (logbuffer, &data);
1321         EXIT_LOG (logbuffer);
1322         process_requests (profiler);
1323 }
1324
1325 static void
1326 thread_start (MonoProfiler *prof, uintptr_t tid)
1327 {
1328         //printf ("thread start %p\n", (void*)tid);
1329         init_thread ();
1330 }
1331
1332 static void
1333 thread_end (MonoProfiler *prof, uintptr_t tid)
1334 {
1335         if (TLS_GET (LogBuffer, tlsbuffer))
1336                 send_buffer (prof, TLS_GET (GPtrArray, tlsmethodlist), TLS_GET (LogBuffer, tlsbuffer));
1337
1338         TLS_SET (tlsbuffer, NULL);
1339         TLS_SET (tlsmethodlist, NULL);
1340 }
1341
1342 static void
1343 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
1344 {
1345         int len = strlen (name) + 1;
1346         uint64_t now;
1347         LogBuffer *logbuffer;
1348         logbuffer = ensure_logbuf (10 + len);
1349         now = current_time ();
1350         ENTER_LOG (logbuffer, "tname");
1351         emit_byte (logbuffer, TYPE_METADATA);
1352         emit_time (logbuffer, now);
1353         emit_byte (logbuffer, TYPE_THREAD);
1354         emit_ptr (logbuffer, (void*)tid);
1355         emit_value (logbuffer, 0); /* flags */
1356         memcpy (logbuffer->data, name, len);
1357         logbuffer->data += len;
1358         EXIT_LOG (logbuffer);
1359 }
1360
1361 typedef struct {
1362         MonoMethod *method;
1363         MonoDomain *domain;
1364         void *base_address;
1365         int offset;
1366 } AsyncFrameInfo;
1367
1368 typedef struct {
1369         int count;
1370         AsyncFrameInfo *data;
1371 } AsyncFrameData;
1372
1373 static mono_bool
1374 async_walk_stack (MonoMethod *method, MonoDomain *domain, void *base_address, int offset, void *data)
1375 {
1376         AsyncFrameData *frame = data;
1377         if (frame->count < num_frames) {
1378                 frame->data [frame->count].method = method;
1379                 frame->data [frame->count].domain = domain;
1380                 frame->data [frame->count].base_address = base_address;
1381                 frame->data [frame->count].offset = offset;
1382                 // printf ("In %d at %p (dom %p) (native: %p)\n", frame->count, method, domain, base_address);
1383                 frame->count++;
1384         }
1385         return frame->count == num_frames;
1386 }
1387
1388 /*
1389 (type | frame count), tid, time, ip, [method, domain, base address, offset] * frames
1390 */
1391 #define SAMPLE_EVENT_SIZE_IN_SLOTS(FRAMES) (4 + (FRAMES) * 4)
1392
1393 static void
1394 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
1395 {
1396         StatBuffer *sbuf;
1397         AsyncFrameInfo frames [num_frames];
1398         AsyncFrameData bt_data = { 0, &frames [0]};
1399         uint64_t now;
1400         uintptr_t *data, *new_data, *old_data;
1401         uintptr_t elapsed;
1402         int timedout = 0;
1403         int i;
1404         if (in_shutdown)
1405                 return;
1406         now = current_time ();
1407
1408         mono_stack_walk_async_safe (&async_walk_stack, context, &bt_data);
1409
1410         elapsed = (now - profiler->startup_time) / 10000;
1411         if (do_debug) {
1412                 int len;
1413                 char buf [256];
1414                 snprintf (buf, sizeof (buf), "hit at %p in thread %p after %llu ms\n", ip, (void*)thread_id (), (unsigned long long int)elapsed/100);
1415                 len = strlen (buf);
1416                 ign_res (write (2, buf, len));
1417         }
1418         sbuf = profiler->stat_buffers;
1419         if (!sbuf)
1420                 return;
1421         /* flush the buffer at 1 second intervals */
1422         if (sbuf->data > sbuf->buf && (elapsed - sbuf->buf [2]) > 100000) {
1423                 timedout = 1;
1424         }
1425         /* overflow: 400 slots is a big enough number to reduce the chance of losing this event if many
1426          * threads hit this same spot at the same time
1427          */
1428         if (timedout || (sbuf->data + 400 >= sbuf->data_end)) {
1429                 StatBuffer *oldsb, *foundsb;
1430                 sbuf = create_stat_buffer ();
1431                 do {
1432                         oldsb = profiler->stat_buffers;
1433                         sbuf->next = oldsb;
1434                         foundsb = InterlockedCompareExchangePointer ((void * volatile*)&profiler->stat_buffers, sbuf, oldsb);
1435                 } while (foundsb != oldsb);
1436                 if (do_debug)
1437                         ign_res (write (2, "overflow\n", 9));
1438                 /* notify the helper thread */
1439                 if (sbuf->next->next) {
1440                         char c = 0;
1441                         ign_res (write (profiler->pipes [1], &c, 1));
1442                         if (do_debug)
1443                                 ign_res (write (2, "notify\n", 7));
1444                 }
1445         }
1446         do {
1447                 old_data = sbuf->data;
1448                 new_data = old_data + SAMPLE_EVENT_SIZE_IN_SLOTS (bt_data.count);
1449                 data = InterlockedCompareExchangePointer ((void * volatile*)&sbuf->data, new_data, old_data);
1450         } while (data != old_data);
1451         if (old_data >= sbuf->data_end)
1452                 return; /* lost event */
1453         old_data [0] = 1 | (sample_type << 16) | (bt_data.count << 8);
1454         old_data [1] = thread_id ();
1455         old_data [2] = elapsed;
1456         old_data [3] = (uintptr_t)ip;
1457         for (i = 0; i < bt_data.count; ++i) {
1458                 old_data [4 + 4 * i + 0] = (uintptr_t)frames [i].method;
1459                 old_data [4 + 4 * i + 1] = (uintptr_t)frames [i].domain;
1460                 old_data [4 + 4 * i + 2] = (uintptr_t)frames [i].base_address;
1461                 old_data [4 + 4 * i + 3] = (uintptr_t)frames [i].offset;
1462         }
1463 }
1464
1465 static uintptr_t *code_pages = 0;
1466 static int num_code_pages = 0;
1467 static int size_code_pages = 0;
1468 #define CPAGE_SHIFT (9)
1469 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
1470 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
1471 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
1472
1473 static uintptr_t
1474 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
1475 {
1476         uintptr_t i;
1477         uintptr_t start_pos;
1478         start_pos = (page >> CPAGE_SHIFT) % hsize;
1479         i = start_pos;
1480         do {
1481                 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
1482                         return 0;
1483                 } else if (!hash [i]) {
1484                         hash [i] = page;
1485                         return 1;
1486                 }
1487                 /* wrap around */
1488                 if (++i == hsize)
1489                         i = 0;
1490         } while (i != start_pos);
1491         /* should not happen */
1492         printf ("failed code page store\n");
1493         return 0;
1494 }
1495
1496 static void
1497 add_code_pointer (uintptr_t ip)
1498 {
1499         uintptr_t i;
1500         if (num_code_pages * 2 >= size_code_pages) {
1501                 uintptr_t *n;
1502                 uintptr_t old_size = size_code_pages;
1503                 size_code_pages *= 2;
1504                 if (size_code_pages == 0)
1505                         size_code_pages = 16;
1506                 n = calloc (sizeof (uintptr_t) * size_code_pages, 1);
1507                 for (i = 0; i < old_size; ++i) {
1508                         if (code_pages [i])
1509                                 add_code_page (n, size_code_pages, code_pages [i]);
1510                 }
1511                 if (code_pages)
1512                         free (code_pages);
1513                 code_pages = n;
1514         }
1515         num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
1516 }
1517
1518 #if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
1519 static void
1520 dump_ubin (const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
1521 {
1522         uint64_t now;
1523         LogBuffer *logbuffer;
1524         int len;
1525         len = strlen (filename) + 1;
1526         now = current_time ();
1527         logbuffer = ensure_logbuf (20 + len);
1528         emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
1529         emit_time (logbuffer, now);
1530         emit_svalue (logbuffer, load_addr);
1531         emit_uvalue (logbuffer, offset);
1532         emit_uvalue (logbuffer, size);
1533         memcpy (logbuffer->data, filename, len);
1534         logbuffer->data += len;
1535 }
1536 #endif
1537
1538 static void
1539 dump_usym (const char *name, uintptr_t value, uintptr_t size)
1540 {
1541         LogBuffer *logbuffer;
1542         int len;
1543         len = strlen (name) + 1;
1544         logbuffer = ensure_logbuf (20 + len);
1545         emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
1546         emit_ptr (logbuffer, (void*)value);
1547         emit_value (logbuffer, size);
1548         memcpy (logbuffer->data, name, len);
1549         logbuffer->data += len;
1550 }
1551
1552 #ifdef ELFMAG0
1553
1554 #if SIZEOF_VOID_P == 4
1555 #define ELF_WSIZE 32
1556 #else
1557 #define ELF_WSIZE 64
1558 #endif
1559 #ifndef ElfW
1560 #define ElfW(type)      _ElfW (Elf, ELF_WSIZE, type)
1561 #define _ElfW(e,w,t)    _ElfW_1 (e, w, _##t)
1562 #define _ElfW_1(e,w,t)  e##w##t
1563 #endif
1564
1565 static void
1566 dump_elf_symbols (ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
1567 {
1568         int i;
1569         for (i = 0; i < num_symbols; ++i) {
1570                 const char* sym;
1571                 sym =  strtab + symbols [i].st_name;
1572                 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
1573                         continue;
1574                 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
1575                 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
1576         }
1577 }
1578
1579 static int
1580 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
1581 {
1582         int fd, i;
1583         void *data;
1584         struct stat statb;
1585         uint64_t file_size;
1586         ElfW(Ehdr) *header;
1587         ElfW(Shdr) *sheader;
1588         ElfW(Shdr) *shstrtabh;
1589         ElfW(Shdr) *symtabh = NULL;
1590         ElfW(Shdr) *strtabh = NULL;
1591         ElfW(Sym) *symbols = NULL;
1592         const char *strtab;
1593         int num_symbols;
1594
1595         fd = open (filename, O_RDONLY);
1596         if (fd < 0)
1597                 return 0;
1598         if (fstat (fd, &statb) != 0) {
1599                 close (fd);
1600                 return 0;
1601         }
1602         file_size = statb.st_size;
1603         data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1604         close (fd);
1605         if (data == MAP_FAILED)
1606                 return 0;
1607         header = data;
1608         if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1609                         header->e_ident [EI_MAG1] != ELFMAG1 ||
1610                         header->e_ident [EI_MAG2] != ELFMAG2 ||
1611                         header->e_ident [EI_MAG3] != ELFMAG3 ) {
1612                 munmap (data, file_size);
1613                 return 0;
1614         }
1615         sheader = (void*)((char*)data + header->e_shoff);
1616         shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
1617         strtab = (const char*)data + shstrtabh->sh_offset;
1618         for (i = 0; i < header->e_shnum; ++i) {
1619                 //printf ("section header: %d\n", sheader->sh_type);
1620                 if (sheader->sh_type == SHT_SYMTAB) {
1621                         symtabh = sheader;
1622                         strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
1623                         /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
1624                         break;
1625                 }
1626                 sheader = (void*)((char*)sheader + header->e_shentsize);
1627         }
1628         if (!symtabh || !strtabh) {
1629                 munmap (data, file_size);
1630                 return 0;
1631         }
1632         strtab = (const char*)data + strtabh->sh_offset;
1633         num_symbols = symtabh->sh_size / symtabh->sh_entsize;
1634         symbols = (void*)((char*)data + symtabh->sh_offset);
1635         dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
1636         munmap (data, file_size);
1637         return 1;
1638 }
1639 #endif
1640
1641 #if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
1642 static int
1643 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
1644 {
1645         MonoProfiler *prof = data;
1646         char buf [256];
1647         const char *filename;
1648         BinaryObject *obj;
1649         char *a = (void*)info->dlpi_addr;
1650         int i, num_sym;
1651         ElfW(Dyn) *dyn = NULL;
1652         ElfW(Sym) *symtab = NULL;
1653         ElfW(Word) *hash_table = NULL;
1654         ElfW(Ehdr) *header = NULL;
1655         const char* strtab = NULL;
1656         for (obj = prof->binary_objects; obj; obj = obj->next) {
1657                 if (obj->addr == a)
1658                         return 0;
1659         }
1660         filename = info->dlpi_name;
1661         if (!filename)
1662                 return 0;
1663         if (!info->dlpi_addr && !filename [0]) {
1664                 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
1665                 if (l > 0) {
1666                         buf [l] = 0;
1667                         filename = buf;
1668                 }
1669         }
1670         obj = calloc (sizeof (BinaryObject), 1);
1671         obj->addr = (void*)info->dlpi_addr;
1672         obj->name = pstrdup (filename);
1673         obj->next = prof->binary_objects;
1674         prof->binary_objects = obj;
1675         //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
1676         a = NULL;
1677         for (i = 0; i < info->dlpi_phnum; ++i) {
1678                 //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);
1679                 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
1680                         header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1681                         if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1682                                         header->e_ident [EI_MAG1] != ELFMAG1 ||
1683                                         header->e_ident [EI_MAG2] != ELFMAG2 ||
1684                                         header->e_ident [EI_MAG3] != ELFMAG3 ) {
1685                                 header = NULL;
1686                         }
1687                         dump_ubin (filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
1688                 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
1689                         dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1690                 }
1691         }
1692         if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
1693                 return 0;
1694         if (!info->dlpi_name || !info->dlpi_name[0])
1695                 return 0;
1696         if (!dyn)
1697                 return 0;
1698         for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
1699                 if (dyn [i].d_tag == DT_SYMTAB) {
1700                         if (symtab && do_debug)
1701                                 printf ("multiple symtabs: %d\n", i);
1702                         symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
1703                 } else if (dyn [i].d_tag == DT_HASH) {
1704                         hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
1705                 } else if (dyn [i].d_tag == DT_STRTAB) {
1706                         strtab = (const char*)(a + dyn [i].d_un.d_ptr);
1707                 }
1708         }
1709         if (!hash_table)
1710                 return 0;
1711         num_sym = hash_table [1];
1712         dump_elf_symbols (symtab, num_sym, strtab, (void*)info->dlpi_addr);
1713         return 0;
1714 }
1715
1716 static int
1717 load_binaries (MonoProfiler *prof)
1718 {
1719         dl_iterate_phdr (elf_dl_callback, prof);
1720         return 1;
1721 }
1722 #else
1723 static int
1724 load_binaries (MonoProfiler *prof)
1725 {
1726         return 0;
1727 }
1728 #endif
1729
1730 static const char*
1731 symbol_for (uintptr_t code)
1732 {
1733 #ifdef HAVE_DLADDR
1734         void *ip = (void*)code;
1735         Dl_info di;
1736         if (dladdr (ip, &di)) {
1737                 if (di.dli_sname)
1738                         return di.dli_sname;
1739         } else {
1740         /*      char **names;
1741                 names = backtrace_symbols (&ip, 1);
1742                 if (names) {
1743                         const char* p = names [0];
1744                         free (names);
1745                         return p;
1746                 }
1747                 */
1748         }
1749 #endif
1750         return NULL;
1751 }
1752
1753 static void
1754 dump_unmanaged_coderefs (MonoProfiler *prof)
1755 {
1756         int i;
1757         const char* last_symbol;
1758         uintptr_t addr, page_end;
1759
1760         if (load_binaries (prof))
1761                 return;
1762         for (i = 0; i < size_code_pages; ++i) {
1763                 const char* sym;
1764                 if (!code_pages [i] || code_pages [i] & 1)
1765                         continue;
1766                 last_symbol = NULL;
1767                 addr = CPAGE_ADDR (code_pages [i]);
1768                 page_end = addr + CPAGE_SIZE;
1769                 code_pages [i] |= 1;
1770                 /* we dump the symbols for the whole page */
1771                 for (; addr < page_end; addr += 16) {
1772                         sym = symbol_for (addr);
1773                         if (sym && sym == last_symbol)
1774                                 continue;
1775                         last_symbol = sym;
1776                         if (!sym)
1777                                 continue;
1778                         dump_usym (sym, addr, 0); /* let's not guess the size */
1779                         //printf ("found symbol at %p: %s\n", (void*)addr, sym);
1780                 }
1781         }
1782 }
1783
1784 static void
1785 dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf)
1786 {
1787         uintptr_t *sample;
1788         LogBuffer *logbuffer;
1789         if (!sbuf)
1790                 return;
1791         if (sbuf->next) {
1792                 dump_sample_hits (prof, sbuf->next);
1793                 free_buffer (sbuf->next, sbuf->next->size);
1794                 sbuf->next = NULL;
1795         }
1796         for (sample = sbuf->buf; sample < sbuf->data;) {
1797                 int i;
1798                 int count = sample [0] & 0xff;
1799                 int mbt_count = (sample [0] & 0xff00) >> 8;
1800                 int type = sample [0] >> 16;
1801                 uintptr_t *managed_sample_base = sample + count + 3;
1802
1803                 if (sample + SAMPLE_EVENT_SIZE_IN_SLOTS (mbt_count) > sbuf->data)
1804                         break;
1805
1806                 for (i = 0; i < mbt_count; ++i) {
1807                         MonoMethod *method = (MonoMethod*)managed_sample_base [i * 4 + 0];
1808                         MonoDomain *domain = (MonoDomain*)managed_sample_base [i * 4 + 1];
1809                         void *address = (void*)managed_sample_base [i * 4 + 2];
1810
1811                         if (!method) {
1812                                 MonoJitInfo *ji = mono_jit_info_table_find (domain, address);
1813
1814                                 if (ji)
1815                                         managed_sample_base [i * 4 + 0] = (uintptr_t)mono_jit_info_get_method (ji);
1816                         }
1817                 }
1818                 logbuffer = ensure_logbuf (20 + count * 8);
1819                 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1820                 emit_value (logbuffer, type);
1821                 emit_uvalue (logbuffer, prof->startup_time + (uint64_t)sample [2] * (uint64_t)10000);
1822                 emit_value (logbuffer, count);
1823                 for (i = 0; i < count; ++i) {
1824                         emit_ptr (logbuffer, (void*)sample [i + 3]);
1825                         add_code_pointer (sample [i + 3]);
1826                 }
1827
1828                 sample += count + 3;
1829                 /* new in data version 6 */
1830                 emit_uvalue (logbuffer, mbt_count);
1831                 for (i = 0; i < mbt_count; ++i) {
1832                         MonoMethod *method = (MonoMethod *) sample [i * 4 + 0];
1833                         MonoDomain *domain = (MonoDomain *) sample [i * 4 + 1];
1834                         uintptr_t native_offset = sample [i * 4 + 3];
1835
1836                         emit_method (prof, logbuffer, domain, method);
1837                         emit_svalue (logbuffer, 0); /* il offset will always be 0 from now on */
1838                         emit_svalue (logbuffer, native_offset);
1839                 }
1840                 sample += 4 * mbt_count;
1841         }
1842         dump_unmanaged_coderefs (prof);
1843 }
1844
1845 #if USE_PERF_EVENTS
1846
1847 static int
1848 mono_cpu_count (void)
1849 {
1850         int count = 0;
1851 #ifdef PLATFORM_ANDROID
1852         /* Android tries really hard to save power by powering off CPUs on SMP phones which
1853          * means the normal way to query cpu count returns a wrong value with userspace API.
1854          * Instead we use /sys entries to query the actual hardware CPU count.
1855          */
1856         char buffer[8] = {'\0'};
1857         int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
1858         /* Format of the /sys entry is a cpulist of indexes which in the case
1859          * of present is always of the form "0-(n-1)" when there is more than
1860          * 1 core, n being the number of CPU cores in the system. Otherwise
1861          * the value is simply 0
1862          */
1863         if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
1864                 count = strtol (((char*)buffer) + 2, NULL, 10);
1865         if (present != -1)
1866                 close (present);
1867         if (count > 0)
1868                 return count + 1;
1869 #endif
1870 #ifdef _SC_NPROCESSORS_ONLN
1871         count = sysconf (_SC_NPROCESSORS_ONLN);
1872         if (count > 0)
1873                 return count;
1874 #endif
1875 #ifdef USE_SYSCTL
1876         {
1877                 int mib [2];
1878                 size_t len = sizeof (int);
1879                 mib [0] = CTL_HW;
1880                 mib [1] = HW_NCPU;
1881                 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
1882                         return count;
1883         }
1884 #endif
1885 #ifdef HOST_WIN32
1886         {
1887                 SYSTEM_INFO info;
1888                 GetSystemInfo (&info);
1889                 return info.dwNumberOfProcessors;
1890         }
1891 #endif
1892         /* FIXME: warn */
1893         return 1;
1894 }
1895
1896 typedef struct {
1897         int perf_fd;
1898         unsigned int prev_pos;
1899         void *mmap_base;
1900         struct perf_event_mmap_page *page_desc;
1901 } PerfData ;
1902
1903 static PerfData *perf_data = NULL;
1904 static int num_perf;
1905 #define PERF_PAGES_SHIFT 4
1906 static int num_pages = 1 << PERF_PAGES_SHIFT;
1907 static unsigned int mmap_mask;
1908
1909 typedef struct {
1910         struct perf_event_header h;
1911         uint64_t ip;
1912         uint32_t pid;
1913         uint32_t tid;
1914         uint64_t timestamp;
1915         uint64_t period;
1916         uint64_t nframes;
1917 } PSample;
1918
1919 static int
1920 perf_event_syscall (struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)
1921 {
1922         attr->size = PERF_ATTR_SIZE_VER0;
1923         //printf ("perf attr size: %d\n", attr->size);
1924 #if defined(__x86_64__)
1925         return syscall(/*__NR_perf_event_open*/ 298, attr, pid, cpu, group_fd, flags);
1926 #elif defined(__i386__)
1927         return syscall(/*__NR_perf_event_open*/ 336, attr, pid, cpu, group_fd, flags);
1928 #elif defined(__arm__) || defined (__aarch64__)
1929         return syscall(/*__NR_perf_event_open*/ 364, attr, pid, cpu, group_fd, flags);
1930 #else
1931         return -1;
1932 #endif
1933 }
1934
1935 static int
1936 setup_perf_map (PerfData *perf)
1937 {
1938         perf->mmap_base = mmap (NULL, (num_pages + 1) * getpagesize (), PROT_READ|PROT_WRITE, MAP_SHARED, perf->perf_fd, 0);
1939         if (perf->mmap_base == MAP_FAILED) {
1940                 if (do_debug)
1941                         printf ("failed mmap\n");
1942                 return 0;
1943         }
1944         perf->page_desc = perf->mmap_base;
1945         if (do_debug)
1946                 printf ("mmap version: %d\n", perf->page_desc->version);
1947         return 1;
1948 }
1949
1950 static void
1951 dump_perf_hits (MonoProfiler *prof, void *buf, int size)
1952 {
1953         LogBuffer *logbuffer;
1954         void *end = (char*)buf + size;
1955         int samples = 0;
1956         int pid = getpid ();
1957
1958         while (buf < end) {
1959                 PSample *s = buf;
1960                 if (s->h.size == 0)
1961                         break;
1962                 if (pid != s->pid) {
1963                         if (do_debug)
1964                                 printf ("event for different pid: %d\n", s->pid);
1965                         buf = (char*)buf + s->h.size;
1966                         continue;
1967                 }
1968                 /*ip = (void*)s->ip;
1969                 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
1970                         s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
1971                 logbuffer = ensure_logbuf (20 + s->nframes * 8);
1972                 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1973                 emit_value (logbuffer, sample_type);
1974                 emit_uvalue (logbuffer, s->timestamp - prof->startup_time);
1975                 emit_value (logbuffer, 1); /* count */
1976                 emit_ptr (logbuffer, (void*)(uintptr_t)s->ip);
1977                 /* no support here yet for the managed backtrace */
1978                 emit_uvalue (logbuffer, 0);
1979                 add_code_pointer (s->ip);
1980                 buf = (char*)buf + s->h.size;
1981                 samples++;
1982         }
1983         if (do_debug)
1984                 printf ("dumped %d samples\n", samples);
1985         dump_unmanaged_coderefs (prof);
1986 }
1987
1988 /* read events from the ring buffer */
1989 static int
1990 read_perf_mmap (MonoProfiler* prof, int cpu)
1991 {
1992         PerfData *perf = perf_data + cpu;
1993         unsigned char *buf;
1994         unsigned char *data = (unsigned char*)perf->mmap_base + getpagesize ();
1995         unsigned int head = perf->page_desc->data_head;
1996         int diff, size;
1997         unsigned int old;
1998
1999         mono_memory_read_barrier ();
2000
2001         old = perf->prev_pos;
2002         diff = head - old;
2003         if (diff < 0) {
2004                 if (do_debug)
2005                         printf ("lost mmap events: old: %d, head: %d\n", old, head);
2006                 old = head;
2007         }
2008         size = head - old;
2009         if ((old & mmap_mask) + size != (head & mmap_mask)) {
2010                 buf = data + (old & mmap_mask);
2011                 size = mmap_mask + 1 - (old & mmap_mask);
2012                 old += size;
2013                 /* size bytes at buf */
2014                 if (do_debug)
2015                         printf ("found1 bytes of events: %d\n", size);
2016                 dump_perf_hits (prof, buf, size);
2017         }
2018         buf = data + (old & mmap_mask);
2019         size = head - old;
2020         /* size bytes at buf */
2021         if (do_debug)
2022                 printf ("found bytes of events: %d\n", size);
2023         dump_perf_hits (prof, buf, size);
2024         old += size;
2025         perf->prev_pos = old;
2026         perf->page_desc->data_tail = old;
2027         return 0;
2028 }
2029
2030 static int
2031 setup_perf_event_for_cpu (PerfData *perf, int cpu)
2032 {
2033         struct perf_event_attr attr;
2034         memset (&attr, 0, sizeof (attr));
2035         attr.type = PERF_TYPE_HARDWARE;
2036         switch (sample_type) {
2037         case SAMPLE_CYCLES: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
2038         case SAMPLE_INSTRUCTIONS: attr.config = PERF_COUNT_HW_INSTRUCTIONS; break;
2039         case SAMPLE_CACHE_MISSES: attr.config = PERF_COUNT_HW_CACHE_MISSES; break;
2040         case SAMPLE_CACHE_REFS: attr.config = PERF_COUNT_HW_CACHE_REFERENCES; break;
2041         case SAMPLE_BRANCHES: attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; break;
2042         case SAMPLE_BRANCH_MISSES: attr.config = PERF_COUNT_HW_BRANCH_MISSES; break;
2043         default: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
2044         }
2045         attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD | PERF_SAMPLE_TIME;
2046 //      attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2047         attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
2048         attr.inherit = 1;
2049         attr.freq = 1;
2050         attr.sample_freq = sample_freq;
2051
2052         perf->perf_fd = perf_event_syscall (&attr, getpid (), cpu, -1, 0);
2053         if (do_debug)
2054                 printf ("perf fd: %d, freq: %d, event: %llu\n", perf->perf_fd, sample_freq, attr.config);
2055         if (perf->perf_fd < 0) {
2056                 if (perf->perf_fd == -EPERM) {
2057                         fprintf (stderr, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
2058                 } else {
2059                         if (do_debug)
2060                                 perror ("open perf event");
2061                 }
2062                 return 0;
2063         }
2064         if (!setup_perf_map (perf)) {
2065                 close (perf->perf_fd);
2066                 perf->perf_fd = -1;
2067                 return 0;
2068         }
2069         return 1;
2070 }
2071
2072 static int
2073 setup_perf_event (void)
2074 {
2075         int i, count = 0;
2076         mmap_mask = num_pages * getpagesize () - 1;
2077         num_perf = mono_cpu_count ();
2078         perf_data = calloc (num_perf, sizeof (PerfData));
2079         for (i = 0; i < num_perf; ++i) {
2080                 count += setup_perf_event_for_cpu (perf_data + i, i);
2081         }
2082         if (count)
2083                 return 1;
2084         free (perf_data);
2085         perf_data = NULL;
2086         return 0;
2087 }
2088
2089 #endif /* USE_PERF_EVENTS */
2090
2091 #ifndef DISABLE_HELPER_THREAD
2092
2093 typedef struct MonoCounterAgent {
2094         MonoCounter *counter;
2095         // MonoCounterAgent specific data :
2096         void *value;
2097         size_t value_size;
2098         short index;
2099         short emitted;
2100         struct MonoCounterAgent *next;
2101 } MonoCounterAgent;
2102
2103 static MonoCounterAgent* counters;
2104 static gboolean counters_initialized = FALSE;
2105 static int counters_index = 1;
2106 static mono_mutex_t counters_mutex;
2107
2108 static void
2109 counters_add_agent (MonoCounter *counter)
2110 {
2111         MonoCounterAgent *agent, *item;
2112
2113         if (!counters_initialized)
2114                 return;
2115
2116         mono_mutex_lock (&counters_mutex);
2117
2118         for (agent = counters; agent; agent = agent->next) {
2119                 if (agent->counter == counter) {
2120                         agent->value_size = 0;
2121                         if (agent->value) {
2122                                 free (agent->value);
2123                                 agent->value = NULL;
2124                         }
2125                         mono_mutex_unlock (&counters_mutex);
2126                         return;
2127                 }
2128         }
2129
2130         agent = malloc (sizeof (MonoCounterAgent));
2131         agent->counter = counter;
2132         agent->value = NULL;
2133         agent->value_size = 0;
2134         agent->index = counters_index++;
2135         agent->emitted = 0;
2136         agent->next = NULL;
2137
2138         if (!counters) {
2139                 counters = agent;
2140         } else {
2141                 item = counters;
2142                 while (item->next)
2143                         item = item->next;
2144                 item->next = agent;
2145         }
2146
2147         mono_mutex_unlock (&counters_mutex);
2148 }
2149
2150 static mono_bool
2151 counters_init_foreach_callback (MonoCounter *counter, gpointer data)
2152 {
2153         counters_add_agent (counter);
2154         return TRUE;
2155 }
2156
2157 static void
2158 counters_init (MonoProfiler *profiler)
2159 {
2160         assert (!counters_initialized);
2161
2162         mono_mutex_init (&counters_mutex);
2163
2164         counters_initialized = TRUE;
2165
2166         mono_counters_on_register (&counters_add_agent);
2167         mono_counters_foreach (counters_init_foreach_callback, NULL);
2168 }
2169
2170 static void
2171 counters_emit (MonoProfiler *profiler)
2172 {
2173         MonoCounterAgent *agent;
2174         LogBuffer *logbuffer;
2175         int size = 1 + 5, len = 0;
2176
2177         if (!counters_initialized)
2178                 return;
2179
2180         mono_mutex_lock (&counters_mutex);
2181
2182         for (agent = counters; agent; agent = agent->next) {
2183                 if (agent->emitted)
2184                         continue;
2185
2186                 size += strlen (mono_counter_get_name (agent->counter)) + 1 + 5 * 5;
2187                 len += 1;
2188         }
2189
2190         if (!len) {
2191                 mono_mutex_unlock (&counters_mutex);
2192                 return;
2193         }
2194
2195         logbuffer = ensure_logbuf (size);
2196
2197         ENTER_LOG (logbuffer, "counters");
2198         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
2199         emit_value (logbuffer, len);
2200         for (agent = counters; agent; agent = agent->next) {
2201                 const char *name;
2202
2203                 if (agent->emitted)
2204                         continue;
2205
2206                 name = mono_counter_get_name (agent->counter);
2207                 emit_value (logbuffer, mono_counter_get_section (agent->counter));
2208                 emit_string (logbuffer, name, strlen (name) + 1);
2209                 emit_value (logbuffer, mono_counter_get_type (agent->counter));
2210                 emit_value (logbuffer, mono_counter_get_unit (agent->counter));
2211                 emit_value (logbuffer, mono_counter_get_variance (agent->counter));
2212                 emit_value (logbuffer, agent->index);
2213
2214                 agent->emitted = 1;
2215         }
2216         EXIT_LOG (logbuffer);
2217
2218         safe_send (profiler, ensure_logbuf (0));
2219
2220         mono_mutex_unlock (&counters_mutex);
2221 }
2222
2223 static void
2224 counters_sample (MonoProfiler *profiler, uint64_t timestamp)
2225 {
2226         MonoCounterAgent *agent;
2227         MonoCounter *counter;
2228         LogBuffer *logbuffer;
2229         int type;
2230         int buffer_size;
2231         void *buffer;
2232         int size;
2233
2234         if (!counters_initialized)
2235                 return;
2236
2237         counters_emit (profiler);
2238
2239         buffer_size = 8;
2240         buffer = calloc (1, buffer_size);
2241
2242         mono_mutex_lock (&counters_mutex);
2243
2244         size = 1 + 10 + 5;
2245         for (agent = counters; agent; agent = agent->next)
2246                 size += 10 * 2 + mono_counter_get_size (agent->counter);
2247
2248         logbuffer = ensure_logbuf (size);
2249
2250         ENTER_LOG (logbuffer, "counters");
2251         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
2252         emit_uvalue (logbuffer, timestamp);
2253         for (agent = counters; agent; agent = agent->next) {
2254                 size_t size;
2255
2256                 counter = agent->counter;
2257
2258                 size = mono_counter_get_size (counter);
2259                 if (size < 0) {
2260                         continue; // FIXME error
2261                 } else if (size > buffer_size) {
2262                         buffer_size = size;
2263                         buffer = realloc (buffer, buffer_size);
2264                 }
2265
2266                 memset (buffer, 0, buffer_size);
2267
2268                 if (mono_counters_sample (counter, buffer, size) < 0)
2269                         continue; // FIXME error
2270
2271                 type = mono_counter_get_type (counter);
2272
2273                 if (!agent->value) {
2274                         agent->value = calloc (1, size);
2275                         agent->value_size = size;
2276                 } else {
2277                         if (type == MONO_COUNTER_STRING) {
2278                                 if (strcmp (agent->value, buffer) == 0)
2279                                         continue;
2280                         } else {
2281                                 if (agent->value_size == size && memcmp (agent->value, buffer, size) == 0)
2282                                         continue;
2283                         }
2284                 }
2285
2286                 emit_uvalue (logbuffer, agent->index);
2287                 emit_uvalue (logbuffer, type);
2288                 switch (type) {
2289                 case MONO_COUNTER_INT:
2290 #if SIZEOF_VOID_P == 4
2291                 case MONO_COUNTER_WORD:
2292 #endif
2293                         emit_svalue (logbuffer, *(int*)buffer - *(int*)agent->value);
2294                         break;
2295                 case MONO_COUNTER_UINT:
2296                         emit_uvalue (logbuffer, *(guint*)buffer - *(guint*)agent->value);
2297                         break;
2298                 case MONO_COUNTER_TIME_INTERVAL:
2299                 case MONO_COUNTER_LONG:
2300 #if SIZEOF_VOID_P == 8
2301                 case MONO_COUNTER_WORD:
2302 #endif
2303                         emit_svalue (logbuffer, *(gint64*)buffer - *(gint64*)agent->value);
2304                         break;
2305                 case MONO_COUNTER_ULONG:
2306                         emit_uvalue (logbuffer, *(guint64*)buffer - *(guint64*)agent->value);
2307                         break;
2308                 case MONO_COUNTER_DOUBLE:
2309                         emit_double (logbuffer, *(double*)buffer);
2310                         break;
2311                 case MONO_COUNTER_STRING:
2312                         if (size == 0) {
2313                                 emit_byte (logbuffer, 0);
2314                         } else {
2315                                 emit_byte (logbuffer, 1);
2316                                 emit_string (logbuffer, (char*)buffer, size);
2317                         }
2318                         break;
2319                 default:
2320                         assert (0);
2321                 }
2322
2323                 if (type == MONO_COUNTER_STRING && size > agent->value_size) {
2324                         agent->value = realloc (agent->value, size);
2325                         agent->value_size = size;
2326                 }
2327
2328                 if (size > 0)
2329                         memcpy (agent->value, buffer, size);
2330         }
2331         free (buffer);
2332
2333         emit_value (logbuffer, 0);
2334         EXIT_LOG (logbuffer);
2335
2336         safe_send (profiler, ensure_logbuf (0));
2337
2338         mono_mutex_unlock (&counters_mutex);
2339 }
2340
2341 typedef struct _PerfCounterAgent PerfCounterAgent;
2342 struct _PerfCounterAgent {
2343         PerfCounterAgent *next;
2344         int index;
2345         char *category_name;
2346         char *name;
2347         int type;
2348         gint64 value;
2349         guint8 emitted;
2350         guint8 updated;
2351         guint8 deleted;
2352 };
2353
2354 static PerfCounterAgent *perfcounters = NULL;
2355
2356 static void
2357 perfcounters_emit (MonoProfiler *profiler)
2358 {
2359         PerfCounterAgent *pcagent;
2360         LogBuffer *logbuffer;
2361         int size = 1 + 5, len = 0;
2362
2363         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
2364                 if (pcagent->emitted)
2365                         continue;
2366
2367                 size += strlen (pcagent->name) + 1 + 5 * 5;
2368                 len += 1;
2369         }
2370
2371         if (!len)
2372                 return;
2373
2374         logbuffer = ensure_logbuf (size);
2375
2376         ENTER_LOG (logbuffer, "perfcounters");
2377         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
2378         emit_value (logbuffer, len);
2379         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
2380                 if (pcagent->emitted)
2381                         continue;
2382
2383                 emit_value (logbuffer, MONO_COUNTER_PERFCOUNTERS);
2384                 emit_string (logbuffer, pcagent->category_name, strlen (pcagent->category_name) + 1);
2385                 emit_string (logbuffer, pcagent->name, strlen (pcagent->name) + 1);
2386                 emit_value (logbuffer, MONO_COUNTER_LONG);
2387                 emit_value (logbuffer, MONO_COUNTER_RAW);
2388                 emit_value (logbuffer, MONO_COUNTER_VARIABLE);
2389                 emit_value (logbuffer, pcagent->index);
2390
2391                 pcagent->emitted = 1;
2392         }
2393         EXIT_LOG (logbuffer);
2394
2395         safe_send (profiler, ensure_logbuf (0));
2396 }
2397
2398 static gboolean
2399 perfcounters_foreach (char *category_name, char *name, unsigned char type, gint64 value, gpointer user_data)
2400 {
2401         PerfCounterAgent *pcagent;
2402
2403         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
2404                 if (strcmp (pcagent->category_name, category_name) != 0 || strcmp (pcagent->name, name) != 0)
2405                         continue;
2406                 if (pcagent->value == value)
2407                         return TRUE;
2408
2409                 pcagent->value = value;
2410                 pcagent->updated = 1;
2411                 pcagent->deleted = 0;
2412                 return TRUE;
2413         }
2414
2415         pcagent = g_new0 (PerfCounterAgent, 1);
2416         pcagent->next = perfcounters;
2417         pcagent->index = counters_index++;
2418         pcagent->category_name = g_strdup (category_name);
2419         pcagent->name = g_strdup (name);
2420         pcagent->type = (int) type;
2421         pcagent->value = value;
2422         pcagent->emitted = 0;
2423         pcagent->updated = 1;
2424         pcagent->deleted = 0;
2425
2426         perfcounters = pcagent;
2427
2428         return TRUE;
2429 }
2430
2431 static void
2432 perfcounters_sample (MonoProfiler *profiler, uint64_t timestamp)
2433 {
2434         PerfCounterAgent *pcagent;
2435         LogBuffer *logbuffer;
2436         int size;
2437
2438         if (!counters_initialized)
2439                 return;
2440
2441         mono_mutex_lock (&counters_mutex);
2442
2443         /* mark all perfcounters as deleted, foreach will unmark them as necessary */
2444         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next)
2445                 pcagent->deleted = 1;
2446
2447         mono_perfcounter_foreach (perfcounters_foreach, perfcounters);
2448
2449         perfcounters_emit (profiler);
2450
2451
2452         size = 1 + 10 + 5;
2453         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
2454                 if (pcagent->deleted || !pcagent->updated)
2455                         continue;
2456                 size += 10 * 2 + sizeof (gint64);
2457         }
2458
2459         logbuffer = ensure_logbuf (size);
2460
2461         ENTER_LOG (logbuffer, "perfcounters");
2462         emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
2463         emit_uvalue (logbuffer, timestamp);
2464         for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
2465                 if (pcagent->deleted || !pcagent->updated)
2466                         continue;
2467                 emit_uvalue (logbuffer, pcagent->index);
2468                 emit_uvalue (logbuffer, MONO_COUNTER_LONG);
2469                 emit_svalue (logbuffer, pcagent->value);
2470
2471                 pcagent->updated = 0;
2472         }
2473
2474         emit_value (logbuffer, 0);
2475         EXIT_LOG (logbuffer);
2476
2477         safe_send (profiler, ensure_logbuf (0));
2478
2479         mono_mutex_unlock (&counters_mutex);
2480 }
2481
2482 static void
2483 counters_and_perfcounters_sample (MonoProfiler *prof)
2484 {
2485         static uint64_t start = -1;
2486         uint64_t now;
2487
2488         if (start == -1)
2489                 start = current_time ();
2490
2491         now = current_time ();
2492         counters_sample (prof, (now - start) / 1000/ 1000);
2493         perfcounters_sample (prof, (now - start) / 1000/ 1000);
2494 }
2495
2496 #endif /* DISABLE_HELPER_THREAD */
2497
2498 static void
2499 log_shutdown (MonoProfiler *prof)
2500 {
2501         void *res;
2502
2503         in_shutdown = 1;
2504 #ifndef DISABLE_HELPER_THREAD
2505         counters_and_perfcounters_sample (prof);
2506
2507         if (prof->command_port) {
2508                 char c = 1;
2509                 ign_res (write (prof->pipes [1], &c, 1));
2510                 pthread_join (prof->helper_thread, &res);
2511         }
2512 #endif
2513 #if USE_PERF_EVENTS
2514         if (perf_data) {
2515                 int i;
2516                 for (i = 0; i < num_perf; ++i)
2517                         read_perf_mmap (prof, i);
2518         }
2519 #endif
2520         dump_sample_hits (prof, prof->stat_buffers);
2521
2522         if (TLS_GET (LogBuffer, tlsbuffer))
2523                 send_buffer (prof, TLS_GET (GPtrArray, tlsmethodlist), TLS_GET (LogBuffer, tlsbuffer));
2524
2525         TLS_SET (tlsbuffer, NULL);
2526         TLS_SET (tlsmethodlist, NULL);
2527
2528         InterlockedWrite (&prof->run_writer_thread, 0);
2529         pthread_join (prof->writer_thread, &res);
2530
2531 #if defined (HAVE_SYS_ZLIB)
2532         if (prof->gzfile)
2533                 gzclose (prof->gzfile);
2534 #endif
2535         if (prof->pipe_output)
2536                 pclose (prof->file);
2537         else
2538                 fclose (prof->file);
2539
2540         mono_conc_hashtable_destroy (prof->method_table);
2541         mono_mutex_destroy (&prof->method_table_mutex);
2542
2543         free (prof);
2544 }
2545
2546 static char*
2547 new_filename (const char* filename)
2548 {
2549         time_t t = time (NULL);
2550         int pid = process_id ();
2551         char pid_buf [16];
2552         char time_buf [16];
2553         char *res, *d;
2554         const char *p;
2555         int count_dates = 0;
2556         int count_pids = 0;
2557         int s_date, s_pid;
2558         struct tm *ts;
2559         for (p = filename; *p; p++) {
2560                 if (*p != '%')
2561                         continue;
2562                 p++;
2563                 if (*p == 't')
2564                         count_dates++;
2565                 else if (*p == 'p')
2566                         count_pids++;
2567                 else if (*p == 0)
2568                         break;
2569         }
2570         if (!count_dates && !count_pids)
2571                 return pstrdup (filename);
2572         snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
2573         ts = gmtime (&t);
2574         snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
2575                 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
2576         s_date = strlen (time_buf);
2577         s_pid = strlen (pid_buf);
2578         d = res = malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
2579         for (p = filename; *p; p++) {
2580                 if (*p != '%') {
2581                         *d++ = *p;
2582                         continue;
2583                 }
2584                 p++;
2585                 if (*p == 't') {
2586                         strcpy (d, time_buf);
2587                         d += s_date;
2588                         continue;
2589                 } else if (*p == 'p') {
2590                         strcpy (d, pid_buf);
2591                         d += s_pid;
2592                         continue;
2593                 } else if (*p == '%') {
2594                         *d++ = '%';
2595                         continue;
2596                 } else if (*p == 0)
2597                         break;
2598                 *d++ = '%';
2599                 *d++ = *p;
2600         }
2601         *d = 0;
2602         return res;
2603 }
2604
2605 //this is exposed by the JIT, but it's not meant to be a supported API for now.
2606 extern void mono_threads_attach_tools_thread (void);
2607
2608 #ifndef DISABLE_HELPER_THREAD
2609
2610 static void*
2611 helper_thread (void* arg)
2612 {
2613         MonoProfiler* prof = arg;
2614         int command_socket;
2615         int len;
2616         char buf [64];
2617         MonoThread *thread = NULL;
2618
2619         mono_threads_attach_tools_thread ();
2620         //fprintf (stderr, "Server listening\n");
2621         command_socket = -1;
2622         while (1) {
2623                 fd_set rfds;
2624                 struct timeval tv;
2625                 int max_fd = -1;
2626                 FD_ZERO (&rfds);
2627                 FD_SET (prof->server_socket, &rfds);
2628                 max_fd = prof->server_socket;
2629                 FD_SET (prof->pipes [0], &rfds);
2630                 if (max_fd < prof->pipes [0])
2631                         max_fd = prof->pipes [0];
2632                 if (command_socket >= 0) {
2633                         FD_SET (command_socket, &rfds);
2634                         if (max_fd < command_socket)
2635                                 max_fd = command_socket;
2636                 }
2637 #if USE_PERF_EVENTS
2638                 if (perf_data) {
2639                         int i;
2640                         for ( i = 0; i < num_perf; ++i) {
2641                                 if (perf_data [i].perf_fd < 0)
2642                                         continue;
2643                                 FD_SET (perf_data [i].perf_fd, &rfds);
2644                                 if (max_fd < perf_data [i].perf_fd)
2645                                         max_fd = perf_data [i].perf_fd;
2646                         }
2647                 }
2648 #endif
2649
2650                 counters_and_perfcounters_sample (prof);
2651
2652                 tv.tv_sec = 1;
2653                 tv.tv_usec = 0;
2654                 len = select (max_fd + 1, &rfds, NULL, NULL, &tv);
2655
2656                 if (len < 0) {
2657                         if (errno == EINTR)
2658                                 continue;
2659                         
2660                         g_warning ("Error in proflog server: %s", strerror (errno));
2661                         return NULL;
2662                 }
2663                 
2664                 if (FD_ISSET (prof->pipes [0], &rfds)) {
2665                         char c;
2666                         int r = read (prof->pipes [0], &c, 1);
2667                         if (r == 1 && c == 0) {
2668                                 StatBuffer *sbufbase = prof->stat_buffers;
2669                                 StatBuffer *sbuf;
2670                                 if (!sbufbase->next)
2671                                         continue;
2672                                 sbuf = sbufbase->next->next;
2673                                 sbufbase->next->next = NULL;
2674                                 if (do_debug)
2675                                         fprintf (stderr, "stat buffer dump\n");
2676                                 if (sbuf) {
2677                                         dump_sample_hits (prof, sbuf);
2678                                         free_buffer (sbuf, sbuf->size);
2679                                         safe_send (prof, ensure_logbuf (0));
2680                                 }
2681                                 continue;
2682                         }
2683                         /* time to shut down */
2684                         if (thread)
2685                                 mono_thread_detach (thread);
2686                         if (do_debug)
2687                                 fprintf (stderr, "helper shutdown\n");
2688 #if USE_PERF_EVENTS
2689                         if (perf_data) {
2690                                 int i;
2691                                 for ( i = 0; i < num_perf; ++i) {
2692                                         if (perf_data [i].perf_fd < 0)
2693                                                 continue;
2694                                         if (FD_ISSET (perf_data [i].perf_fd, &rfds))
2695                                                 read_perf_mmap (prof, i);
2696                                 }
2697                         }
2698 #endif
2699                         safe_send (prof, ensure_logbuf (0));
2700                         return NULL;
2701                 }
2702 #if USE_PERF_EVENTS
2703                 if (perf_data) {
2704                         int i;
2705                         for ( i = 0; i < num_perf; ++i) {
2706                                 if (perf_data [i].perf_fd < 0)
2707                                         continue;
2708                                 if (FD_ISSET (perf_data [i].perf_fd, &rfds)) {
2709                                         read_perf_mmap (prof, i);
2710                                         safe_send (prof, ensure_logbuf (0));
2711                                 }
2712                         }
2713                 }
2714 #endif
2715                 if (command_socket >= 0 && FD_ISSET (command_socket, &rfds)) {
2716                         len = read (command_socket, buf, sizeof (buf) - 1);
2717                         if (len < 0)
2718                                 continue;
2719                         if (len == 0) {
2720                                 close (command_socket);
2721                                 command_socket = -1;
2722                                 continue;
2723                         }
2724                         buf [len] = 0;
2725                         if (strcmp (buf, "heapshot\n") == 0) {
2726                                 heapshot_requested = 1;
2727                                 //fprintf (stderr, "perform heapshot\n");
2728                                 if (InterlockedRead (&runtime_inited) && !thread) {
2729                                         thread = mono_thread_attach (mono_get_root_domain ());
2730                                         /*fprintf (stderr, "attached\n");*/
2731                                 }
2732                                 if (thread) {
2733                                         process_requests (prof);
2734                                         mono_thread_detach (thread);
2735                                         thread = NULL;
2736                                 }
2737                         }
2738                         continue;
2739                 }
2740                 if (!FD_ISSET (prof->server_socket, &rfds)) {
2741                         continue;
2742                 }
2743                 command_socket = accept (prof->server_socket, NULL, NULL);
2744                 if (command_socket < 0)
2745                         continue;
2746                 //fprintf (stderr, "Accepted connection\n");
2747         }
2748         return NULL;
2749 }
2750
2751 static int
2752 start_helper_thread (MonoProfiler* prof)
2753 {
2754         struct sockaddr_in server_address;
2755         int r;
2756         socklen_t slen;
2757         if (pipe (prof->pipes) < 0) {
2758                 fprintf (stderr, "Cannot create pipe\n");
2759                 return 0;
2760         }
2761         prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
2762         if (prof->server_socket < 0) {
2763                 fprintf (stderr, "Cannot create server socket\n");
2764                 return 0;
2765         }
2766         memset (&server_address, 0, sizeof (server_address));
2767         server_address.sin_family = AF_INET;
2768         server_address.sin_addr.s_addr = INADDR_ANY;
2769         server_address.sin_port = htons (prof->command_port);
2770         if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
2771                 fprintf (stderr, "Cannot bind server socket, port: %d: %s\n", prof->command_port, strerror (errno));
2772                 close (prof->server_socket);
2773                 return 0;
2774         }
2775         if (listen (prof->server_socket, 1) < 0) {
2776                 fprintf (stderr, "Cannot listen server socket\n");
2777                 close (prof->server_socket);
2778                 return 0;
2779         }
2780         slen = sizeof (server_address);
2781         if (getsockname (prof->server_socket, (struct sockaddr *)&server_address, &slen) == 0) {
2782                 prof->command_port = ntohs (server_address.sin_port);
2783                 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
2784         }
2785
2786         r = pthread_create (&prof->helper_thread, NULL, helper_thread, prof);
2787         if (r) {
2788                 close (prof->server_socket);
2789                 return 0;
2790         }
2791         return 1;
2792 }
2793 #endif
2794
2795 static void *
2796 writer_thread (void *arg)
2797 {
2798         MonoProfiler *prof = arg;
2799
2800         mono_threads_attach_tools_thread ();
2801
2802         dump_header (prof);
2803
2804         while (InterlockedRead (&prof->run_writer_thread)) {
2805                 WriterQueueEntry *entry;
2806
2807                 while ((entry = (WriterQueueEntry *) mono_lock_free_queue_dequeue (&prof->writer_queue))) {
2808                         LogBuffer *method_buffer = NULL;
2809                         gboolean new_methods = FALSE;
2810
2811                         if (entry->methods->len)
2812                                 method_buffer = create_buffer ();
2813
2814                         /*
2815                          * Encode the method events in a temporary log buffer that we
2816                          * flush to disk before the main buffer, ensuring that all
2817                          * methods have metadata emitted before they're referenced.
2818                          */
2819                         for (guint i = 0; i < entry->methods->len; i++) {
2820                                 MethodInfo *info = g_ptr_array_index (entry->methods, i);
2821
2822                                 if (mono_conc_hashtable_lookup (prof->method_table, info->method))
2823                                         continue;
2824
2825                                 new_methods = TRUE;
2826
2827                                 /*
2828                                  * Other threads use this hash table to get a general
2829                                  * idea of whether a method has already been emitted to
2830                                  * the stream. Due to the way we add to this table, it
2831                                  * can easily happen that multiple threads queue up the
2832                                  * same methods, but that's OK since eventually all
2833                                  * methods will be in this table and the thread-local
2834                                  * method lists will just be empty for the rest of the
2835                                  * app's lifetime.
2836                                  */
2837                                 mono_conc_hashtable_insert (prof->method_table, info->method, info->method);
2838
2839                                 char *name = mono_method_full_name (info->method, 1);
2840                                 int nlen = strlen (name) + 1;
2841                                 uint64_t now = current_time ();
2842
2843                                 method_buffer = ensure_logbuf_inner (method_buffer, 32 + nlen);
2844
2845                                 emit_byte (method_buffer, TYPE_JIT | TYPE_METHOD);
2846                                 emit_time (method_buffer, now);
2847                                 emit_method_inner (method_buffer, info->method);
2848                                 emit_ptr (method_buffer, mono_jit_info_get_code_start (info->ji));
2849                                 emit_value (method_buffer, mono_jit_info_get_code_size (info->ji));
2850
2851                                 memcpy (method_buffer->data, name, nlen);
2852                                 method_buffer->data += nlen;
2853
2854                                 mono_free (name);
2855                                 free (info);
2856                         }
2857
2858                         g_ptr_array_free (entry->methods, TRUE);
2859
2860                         if (new_methods)
2861                                 dump_buffer (prof, method_buffer);
2862                         else if (method_buffer)
2863                                 free_buffer (method_buffer, method_buffer->size);
2864
2865                         dump_buffer (prof, entry->buffer);
2866
2867                         free (entry);
2868                 }
2869         }
2870
2871         return NULL;
2872 }
2873
2874 static int
2875 start_writer_thread (MonoProfiler* prof)
2876 {
2877         InterlockedWrite (&prof->run_writer_thread, 1);
2878
2879         return !pthread_create (&prof->writer_thread, NULL, writer_thread, prof);
2880 }
2881
2882 static void
2883 runtime_initialized (MonoProfiler *profiler)
2884 {
2885 #ifndef DISABLE_HELPER_THREAD
2886         if (hs_mode_ondemand || need_helper_thread) {
2887                 if (!start_helper_thread (profiler))
2888                         profiler->command_port = 0;
2889         }
2890 #endif
2891
2892         start_writer_thread (profiler);
2893
2894         InterlockedWrite (&runtime_inited, 1);
2895 #ifndef DISABLE_HELPER_THREAD
2896         counters_init (profiler);
2897         counters_sample (profiler, 0);
2898 #endif
2899         /* ensure the main thread data and startup are available soon */
2900         safe_send (profiler, ensure_logbuf (0));
2901 }
2902
2903 static MonoProfiler*
2904 create_profiler (const char *filename)
2905 {
2906         MonoProfiler *prof;
2907         char *nf;
2908         int force_delete = 0;
2909         prof = calloc (1, sizeof (MonoProfiler));
2910
2911         prof->command_port = command_port;
2912         if (filename && *filename == '-') {
2913                 force_delete = 1;
2914                 filename++;
2915         }
2916         if (!filename) {
2917                 if (do_report)
2918                         filename = "|mprof-report -";
2919                 else
2920                         filename = "output.mlpd";
2921                 nf = (char*)filename;
2922         } else {
2923                 nf = new_filename (filename);
2924                 if (do_report) {
2925                         int s = strlen (nf) + 32;
2926                         char *p = malloc (s);
2927                         snprintf (p, s, "|mprof-report '--out=%s' -", nf);
2928                         free (nf);
2929                         nf = p;
2930                 }
2931         }
2932         if (*nf == '|') {
2933                 prof->file = popen (nf + 1, "w");
2934                 prof->pipe_output = 1;
2935         } else if (*nf == '#') {
2936                 int fd = strtol (nf + 1, NULL, 10);
2937                 prof->file = fdopen (fd, "a");
2938         } else {
2939                 if (force_delete)
2940                         unlink (nf);
2941                 prof->file = fopen (nf, "wb");
2942         }
2943         if (!prof->file) {
2944                 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
2945                 exit (1);
2946         }
2947 #if defined (HAVE_SYS_ZLIB)
2948         if (use_zip)
2949                 prof->gzfile = gzdopen (fileno (prof->file), "wb");
2950 #endif
2951 #if USE_PERF_EVENTS
2952         if (sample_type && !do_mono_sample)
2953                 need_helper_thread = setup_perf_event ();
2954         if (!perf_data) {
2955                 /* FIXME: warn if different freq or sample type */
2956                 do_mono_sample = 1;
2957         }
2958 #endif
2959         if (do_mono_sample) {
2960                 prof->stat_buffers = create_stat_buffer ();
2961                 need_helper_thread = 1;
2962         }
2963         if (do_counters && !need_helper_thread) {
2964                 need_helper_thread = 1;
2965         }
2966 #ifdef DISABLE_HELPER_THREAD
2967         if (hs_mode_ondemand)
2968                 fprintf (stderr, "Ondemand heapshot unavailable on this arch.\n");
2969 #endif
2970
2971         mono_lock_free_queue_init (&prof->writer_queue);
2972         mono_mutex_init (&prof->method_table_mutex);
2973         prof->method_table = mono_conc_hashtable_new (&prof->method_table_mutex, NULL, NULL);
2974
2975         prof->startup_time = current_time ();
2976         return prof;
2977 }
2978
2979 static void
2980 usage (int do_exit)
2981 {
2982         printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
2983         printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
2984         printf ("Options:\n");
2985         printf ("\thelp             show this usage info\n");
2986         printf ("\t[no]alloc        enable/disable recording allocation info\n");
2987         printf ("\t[no]calls        enable/disable recording enter/leave method events\n");
2988         printf ("\theapshot[=MODE]  record heap shot info (by default at each major collection)\n");
2989         printf ("\t                 MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
2990         printf ("\tcounters         sample counters every 1s\n");
2991         printf ("\tsample[=TYPE]    use statistical sampling mode (by default cycles/1000)\n");
2992         printf ("\t                 TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
2993         printf ("\t                 TYPE can be followed by /FREQUENCY\n");
2994         printf ("\ttime=fast        use a faster (but more inaccurate) timer\n");
2995         printf ("\tmaxframes=NUM    collect up to NUM stack frames\n");
2996         printf ("\tcalldepth=NUM    ignore method events for call chain depth bigger than NUM\n");
2997         printf ("\toutput=FILENAME  write the data to file FILENAME (-FILENAME to overwrite)\n");
2998         printf ("\toutput=|PROGRAM  write the data to the stdin of PROGRAM\n");
2999         printf ("\t                 %%t is subtituted with date and time, %%p with the pid\n");
3000         printf ("\treport           create a report instead of writing the raw data to a file\n");
3001         printf ("\tzip              compress the output data\n");
3002         printf ("\tport=PORTNUM     use PORTNUM for the listening command server\n");
3003         if (do_exit)
3004                 exit (1);
3005 }
3006
3007 static const char*
3008 match_option (const char* p, const char *opt, char **rval)
3009 {
3010         int len = strlen (opt);
3011         if (strncmp (p, opt, len) == 0) {
3012                 if (rval) {
3013                         if (p [len] == '=' && p [len + 1]) {
3014                                 const char *opt = p + len + 1;
3015                                 const char *end = strchr (opt, ',');
3016                                 char *val;
3017                                 int l;
3018                                 if (end == NULL) {
3019                                         l = strlen (opt);
3020                                 } else {
3021                                         l = end - opt;
3022                                 }
3023                                 val = malloc (l + 1);
3024                                 memcpy (val, opt, l);
3025                                 val [l] = 0;
3026                                 *rval = val;
3027                                 return opt + l;
3028                         }
3029                         if (p [len] == 0 || p [len] == ',') {
3030                                 *rval = NULL;
3031                                 return p + len + (p [len] == ',');
3032                         }
3033                         usage (1);
3034                 } else {
3035                         if (p [len] == 0)
3036                                 return p + len;
3037                         if (p [len] == ',')
3038                                 return p + len + 1;
3039                 }
3040         }
3041         return p;
3042 }
3043
3044 typedef struct {
3045         const char *name;
3046         int sample_mode;
3047 } SampleMode;
3048
3049 static const SampleMode sample_modes [] = {
3050         {"cycles", SAMPLE_CYCLES},
3051         {"instr", SAMPLE_INSTRUCTIONS},
3052         {"cachemiss", SAMPLE_CACHE_MISSES},
3053         {"cacherefs", SAMPLE_CACHE_REFS},
3054         {"branches", SAMPLE_BRANCHES},
3055         {"branchmiss", SAMPLE_BRANCH_MISSES},
3056         {NULL, 0}
3057 };
3058
3059 static void
3060 set_sample_mode (char* val, int allow_empty)
3061 {
3062         char *end;
3063         char *maybe_freq = NULL;
3064         unsigned int count;
3065         const SampleMode *smode = sample_modes;
3066 #ifndef USE_PERF_EVENTS
3067         do_mono_sample = 1;
3068 #endif
3069         if (allow_empty && !val) {
3070                 sample_type = SAMPLE_CYCLES;
3071                 sample_freq = 1000;
3072                 return;
3073         }
3074         if (strcmp (val, "mono") == 0) {
3075                 do_mono_sample = 1;
3076                 sample_type = SAMPLE_CYCLES;
3077                 free (val);
3078                 return;
3079         }
3080         for (smode = sample_modes; smode->name; smode++) {
3081                 int l = strlen (smode->name);
3082                 if (strncmp (val, smode->name, l) == 0) {
3083                         sample_type = smode->sample_mode;
3084                         maybe_freq = val + l;
3085                         break;
3086                 }
3087         }
3088         if (!smode->name)
3089                 usage (1);
3090         if (*maybe_freq == '/') {
3091                 count = strtoul (maybe_freq + 1, &end, 10);
3092                 if (maybe_freq + 1 == end)
3093                         usage (1);
3094                 sample_freq = count;
3095         } else if (*maybe_freq != 0) {
3096                 usage (1);
3097         } else {
3098                 sample_freq = 1000;
3099         }
3100         free (val);
3101 }
3102
3103 static void
3104 set_hsmode (char* val, int allow_empty)
3105 {
3106         char *end;
3107         unsigned int count;
3108         if (allow_empty && !val)
3109                 return;
3110         if (strcmp (val, "ondemand") == 0) {
3111                 hs_mode_ondemand = 1;
3112                 free (val);
3113                 return;
3114         }
3115         count = strtoul (val, &end, 10);
3116         if (val == end)
3117                 usage (1);
3118         if (strcmp (end, "ms") == 0)
3119                 hs_mode_ms = count;
3120         else if (strcmp (end, "gc") == 0)
3121                 hs_mode_gc = count;
3122         else
3123                 usage (1);
3124         free (val);
3125 }
3126
3127 /* 
3128  * declaration to silence the compiler: this is the entry point that
3129  * mono will load from the shared library and call.
3130  */
3131 extern void
3132 mono_profiler_startup (const char *desc);
3133
3134 extern void
3135 mono_profiler_startup_log (const char *desc);
3136
3137 /*
3138  * this is the entry point that will be used when the profiler
3139  * is embedded inside the main executable.
3140  */
3141 void
3142 mono_profiler_startup_log (const char *desc)
3143 {
3144         mono_profiler_startup (desc);
3145 }
3146
3147 void
3148 mono_profiler_startup (const char *desc)
3149 {
3150         MonoProfiler *prof;
3151         char *filename = NULL;
3152         const char *p;
3153         const char *opt;
3154         int fast_time = 0;
3155         int calls_enabled = 0;
3156         int allocs_enabled = 0;
3157         int only_counters = 0;
3158         int events = MONO_PROFILE_GC|MONO_PROFILE_ALLOCATIONS|
3159                 MONO_PROFILE_GC_MOVES|MONO_PROFILE_CLASS_EVENTS|MONO_PROFILE_THREADS|
3160                 MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_EXCEPTIONS|
3161                 MONO_PROFILE_MONITOR_EVENTS|MONO_PROFILE_MODULE_EVENTS|MONO_PROFILE_GC_ROOTS;
3162
3163         p = desc;
3164         if (strncmp (p, "log", 3))
3165                 usage (1);
3166         p += 3;
3167         if (*p == ':')
3168                 p++;
3169         for (; *p; p = opt) {
3170                 char *val;
3171                 if (*p == ',') {
3172                         opt = p + 1;
3173                         continue;
3174                 }
3175                 if ((opt = match_option (p, "help", NULL)) != p) {
3176                         usage (0);
3177                         continue;
3178                 }
3179                 if ((opt = match_option (p, "calls", NULL)) != p) {
3180                         calls_enabled = 1;
3181                         continue;
3182                 }
3183                 if ((opt = match_option (p, "nocalls", NULL)) != p) {
3184                         events &= ~MONO_PROFILE_ENTER_LEAVE;
3185                         nocalls = 1;
3186                         continue;
3187                 }
3188                 if ((opt = match_option (p, "alloc", NULL)) != p) {
3189                         allocs_enabled = 1;
3190                         continue;
3191                 }
3192                 if ((opt = match_option (p, "noalloc", NULL)) != p) {
3193                         events &= ~MONO_PROFILE_ALLOCATIONS;
3194                         continue;
3195                 }
3196                 if ((opt = match_option (p, "time", &val)) != p) {
3197                         if (strcmp (val, "fast") == 0)
3198                                 fast_time = 1;
3199                         else if (strcmp (val, "null") == 0)
3200                                 fast_time = 2;
3201                         else
3202                                 usage (1);
3203                         free (val);
3204                         continue;
3205                 }
3206                 if ((opt = match_option (p, "report", NULL)) != p) {
3207                         do_report = 1;
3208                         continue;
3209                 }
3210                 if ((opt = match_option (p, "debug", NULL)) != p) {
3211                         do_debug = 1;
3212                         continue;
3213                 }
3214                 if ((opt = match_option (p, "sampling-real", NULL)) != p) {
3215                         sampling_mode = MONO_PROFILER_STAT_MODE_REAL;
3216                         continue;
3217                 }
3218                 if ((opt = match_option (p, "sampling-process", NULL)) != p) {
3219                         sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
3220                         continue;
3221                 }
3222                 if ((opt = match_option (p, "heapshot", &val)) != p) {
3223                         events &= ~MONO_PROFILE_ALLOCATIONS;
3224                         events &= ~MONO_PROFILE_ENTER_LEAVE;
3225                         nocalls = 1;
3226                         do_heap_shot = 1;
3227                         set_hsmode (val, 1);
3228                         continue;
3229                 }
3230                 if ((opt = match_option (p, "sample", &val)) != p) {
3231                         events &= ~MONO_PROFILE_ALLOCATIONS;
3232                         events &= ~MONO_PROFILE_ENTER_LEAVE;
3233                         nocalls = 1;
3234                         set_sample_mode (val, 1);
3235                         continue;
3236                 }
3237                 if ((opt = match_option (p, "hsmode", &val)) != p) {
3238                         fprintf (stderr, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
3239                         set_hsmode (val, 0);
3240                         continue;
3241                 }
3242                 if ((opt = match_option (p, "zip", NULL)) != p) {
3243                         use_zip = 1;
3244                         continue;
3245                 }
3246                 if ((opt = match_option (p, "output", &val)) != p) {
3247                         filename = val;
3248                         continue;
3249                 }
3250                 if ((opt = match_option (p, "port", &val)) != p) {
3251                         char *end;
3252                         command_port = strtoul (val, &end, 10);
3253                         free (val);
3254                         continue;
3255                 }
3256                 if ((opt = match_option (p, "maxframes", &val)) != p) {
3257                         char *end;
3258                         num_frames = strtoul (val, &end, 10);
3259                         if (num_frames > MAX_FRAMES)
3260                                 num_frames = MAX_FRAMES;
3261                         free (val);
3262                         notraces = num_frames == 0;
3263                         continue;
3264                 }
3265                 if ((opt = match_option (p, "calldepth", &val)) != p) {
3266                         char *end;
3267                         max_call_depth = strtoul (val, &end, 10);
3268                         free (val);
3269                         continue;
3270                 }
3271                 if ((opt = match_option (p, "counters", NULL)) != p) {
3272                         do_counters = 1;
3273                         continue;
3274                 }
3275                 if ((opt = match_option (p, "countersonly", NULL)) != p) {
3276                         only_counters = 1;
3277                         continue;
3278                 }
3279                 if (opt == p) {
3280                         usage (0);
3281                         exit (0);
3282                 }
3283         }
3284         if (calls_enabled) {
3285                 events |= MONO_PROFILE_ENTER_LEAVE;
3286                 nocalls = 0;
3287         }
3288         if (allocs_enabled)
3289                 events |= MONO_PROFILE_ALLOCATIONS;
3290         if (only_counters)
3291                 events = 0;
3292         utils_init (fast_time);
3293
3294         prof = create_profiler (filename);
3295         if (!prof)
3296                 return;
3297         init_thread ();
3298
3299         mono_profiler_install (prof, log_shutdown);
3300         mono_profiler_install_gc (gc_event, gc_resize);
3301         mono_profiler_install_allocation (gc_alloc);
3302         mono_profiler_install_gc_moves (gc_moves);
3303         mono_profiler_install_gc_roots (gc_handle, gc_roots);
3304         mono_profiler_install_class (NULL, class_loaded, NULL, NULL);
3305         mono_profiler_install_module (NULL, image_loaded, NULL, NULL);
3306         mono_profiler_install_thread (thread_start, thread_end);
3307         mono_profiler_install_thread_name (thread_name);
3308         mono_profiler_install_enter_leave (method_enter, method_leave);
3309         mono_profiler_install_jit_end (method_jitted);
3310         mono_profiler_install_code_buffer_new (code_buffer_new);
3311         mono_profiler_install_exception (throw_exc, method_exc_leave, clause_exc);
3312         mono_profiler_install_monitor (monitor_event);
3313         mono_profiler_install_runtime_initialized (runtime_initialized);
3314
3315         
3316         if (do_mono_sample && sample_type == SAMPLE_CYCLES && !only_counters) {
3317                 events |= MONO_PROFILE_STATISTICAL;
3318                 mono_profiler_set_statistical_mode (sampling_mode, 1000000 / sample_freq);
3319                 mono_profiler_install_statistical (mono_sample_hit);
3320         }
3321
3322         mono_profiler_set_events (events);
3323
3324         TLS_INIT (tlsbuffer);
3325         TLS_INIT (tlsmethodlist);
3326 }
3327