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