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