cb3a8327fa79fdf879f03ac68de78c160d0c2baf
[mono.git] / mono / profiler / decode.c
1 /*
2  * decode.c: mprof-report program source: decode and analyze the log profiler data
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2010 Novell, Inc (http://www.novell.com)
8  */
9 #include <config.h>
10 #include "utils.c"
11 #include "proflog.h"
12 #include <string.h>
13 #include <assert.h>
14 #include <stdio.h>
15 #if !defined(__APPLE__)
16 #include <malloc.h>
17 #endif
18 #include <unistd.h>
19 #include <stdlib.h>
20 #ifdef HOST_WIN32
21 #undef HAVE_ZLIB
22 #endif
23 #if defined (HAVE_ZLIB)
24 #include <zlib.h>
25 #endif
26 #include <mono/metadata/profiler.h>
27 #include <mono/metadata/object.h>
28 #include <mono/metadata/debug-helpers.h>
29
30 #define HASH_SIZE 9371
31 #define SMALL_HASH_SIZE 31
32
33 static int debug = 0;
34 static int collect_traces = 0;
35 static int show_traces = 0;
36 static int trace_max = 6;
37 static int verbose = 0;
38 static uintptr_t *tracked_objects = 0;
39 static int num_tracked_objects = 0;
40 static uintptr_t thread_filter = 0;
41 static uint64_t find_size = 0;
42 static const char* find_name = NULL;
43 static uint64_t time_from = 0;
44 static uint64_t time_to = 0xffffffffffffffffULL;
45 static uint64_t startup_time = 0;
46 static FILE* outfile = NULL;
47
48 static int32_t
49 read_int32 (unsigned char *p)
50 {
51         int32_t value = *p++;
52         value |= (*p++) << 8;
53         value |= (*p++) << 16;
54         value |= (uint32_t)(*p++) << 24;
55         return value;
56 }
57
58 static int64_t
59 read_int64 (unsigned char *p)
60 {
61         uint64_t value = *p++;
62         value |= (*p++) << 8;
63         value |= (*p++) << 16;
64         value |= (uint64_t)(*p++) << 24;
65         value |= (uint64_t)(*p++) << 32;
66         value |= (uint64_t)(*p++) << 40;
67         value |= (uint64_t)(*p++) << 48;
68         value |= (uint64_t)(*p++) << 54;
69         return value;
70 }
71
72 static char*
73 pstrdup (const char *s)
74 {
75         int len = strlen (s) + 1;
76         char *p = malloc (len);
77         memcpy (p, s, len);
78         return p;
79 }
80
81 static int num_images;
82 typedef struct _ImageDesc ImageDesc;
83 struct _ImageDesc {
84         ImageDesc *next;
85         intptr_t image;
86         char *filename;
87 };
88
89 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
90
91 static void
92 add_image (intptr_t image, char *name)
93 {
94         int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
95         ImageDesc *cd = malloc (sizeof (ImageDesc));
96         cd->image = image;
97         cd->filename = pstrdup (name);
98         cd->next = image_hash [slot];
99         image_hash [slot] = cd;
100         num_images++;
101 }
102
103 typedef struct _BackTrace BackTrace;
104 typedef struct {
105         uint64_t count;
106         BackTrace *bt;
107 } CallContext;
108
109 typedef struct {
110         int count;
111         int size;
112         CallContext *traces;
113 } TraceDesc;
114
115 typedef struct _ClassDesc ClassDesc;
116 struct _ClassDesc {
117         ClassDesc *next;
118         intptr_t klass;
119         char *name;
120         intptr_t allocs;
121         uint64_t alloc_size;
122         TraceDesc traces;
123 };
124
125 static ClassDesc* class_hash [HASH_SIZE] = {0};
126 static int num_classes = 0;
127
128 static ClassDesc*
129 add_class (intptr_t klass, const char *name)
130 {
131         int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
132         ClassDesc *cd;
133         cd = class_hash [slot];
134         while (cd && cd->klass != klass)
135                 cd = cd->next;
136         /* we resolved an unknown class (unless we had the code unloaded) */
137         if (cd) {
138                 /*printf ("resolved unknown: %s\n", name);*/
139                 free (cd->name);
140                 cd->name = pstrdup (name);
141                 return cd;
142         }
143         cd = calloc (sizeof (ClassDesc), 1);
144         cd->klass = klass;
145         cd->name = pstrdup (name);
146         cd->next = class_hash [slot];
147         cd->allocs = 0;
148         cd->alloc_size = 0;
149         cd->traces.count = 0;
150         cd->traces.size = 0;
151         cd->traces.traces = NULL;
152         class_hash [slot] = cd;
153         num_classes++;
154         return cd;
155 }
156
157 static ClassDesc *
158 lookup_class (intptr_t klass)
159 {
160         int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
161         ClassDesc *cd = class_hash [slot];
162         while (cd && cd->klass != klass)
163                 cd = cd->next;
164         if (!cd)
165                 return add_class (klass, "unresolved class");
166         return cd;
167 }
168
169 typedef struct _MethodDesc MethodDesc;
170 struct _MethodDesc {
171         MethodDesc *next;
172         intptr_t method;
173         char *name;
174         intptr_t code;
175         int len;
176         int recurse_count;
177         uint64_t calls;
178         uint64_t total_time;
179         uint64_t callee_time;
180         uint64_t self_time;
181         TraceDesc traces;
182 };
183
184 static MethodDesc* method_hash [HASH_SIZE] = {0};
185 static int num_methods = 0;
186
187 static MethodDesc*
188 add_method (intptr_t method, const char *name, intptr_t code, int len)
189 {
190         int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
191         MethodDesc *cd;
192         cd = method_hash [slot];
193         while (cd && cd->method != method)
194                 cd = cd->next;
195         /* we resolved an unknown method (unless we had the code unloaded) */
196         if (cd) {
197                 cd->code = code;
198                 cd->len = len;
199                 /*printf ("resolved unknown: %s\n", name);*/
200                 free (cd->name);
201                 cd->name = pstrdup (name);
202                 return cd;
203         }
204         cd = calloc (sizeof (MethodDesc), 1);
205         cd->method = method;
206         cd->name = pstrdup (name);
207         cd->code = code;
208         cd->len = len;
209         cd->calls = 0;
210         cd->total_time = 0;
211         cd->traces.count = 0;
212         cd->traces.size = 0;
213         cd->traces.traces = NULL;
214         cd->next = method_hash [slot];
215         method_hash [slot] = cd;
216         num_methods++;
217         return cd;
218 }
219
220 static MethodDesc *
221 lookup_method (intptr_t method)
222 {
223         int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
224         MethodDesc *cd = method_hash [slot];
225         while (cd && cd->method != method)
226                 cd = cd->next;
227         if (!cd)
228                 return add_method (method, "unknown method", 0, 0);
229         return cd;
230 }
231
232 typedef struct _HeapClassDesc HeapClassDesc;
233 typedef struct {
234         HeapClassDesc *klass;
235         uint64_t count;
236 } HeapClassRevRef;
237
238 struct _HeapClassDesc {
239         ClassDesc *klass;
240         int64_t count;
241         int64_t total_size;
242         HeapClassRevRef *rev_hash;
243         int rev_hash_size;
244         int rev_count;
245 };
246
247 static int
248 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
249 {
250         uintptr_t i;
251         uintptr_t start_pos;
252         start_pos = (hklass->klass->klass >> 2) % size;
253         assert (start_pos < size);
254         i = start_pos;
255         do {
256                 if (rev_hash [i].klass == hklass) {
257                         rev_hash [i].count += value;
258                         return 0;
259                 } else if (!rev_hash [i].klass) {
260                         rev_hash [i].klass = hklass;
261                         rev_hash [i].count += value;
262                         start_pos = 0;
263                         for (i = 0; i < size; ++i)
264                                 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
265                                         start_pos ++;
266                         assert (start_pos == 1);
267                         return 1;
268                 }
269                 /* wrap around */
270                 if (++i == size)
271                         i = 0;
272         } while (i != start_pos);
273         /* should not happen */
274         printf ("failed revref store\n");
275         return 0;
276 }
277
278 static void
279 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
280 {
281         uintptr_t i;
282         if (to->rev_count * 2 >= to->rev_hash_size) {
283                 HeapClassRevRef *n;
284                 uintptr_t old_size = to->rev_hash_size;
285                 to->rev_hash_size *= 2;
286                 if (to->rev_hash_size == 0)
287                         to->rev_hash_size = 4;
288                 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
289                 for (i = 0; i < old_size; ++i) {
290                         if (to->rev_hash [i].klass)
291                                 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
292                 }
293                 if (to->rev_hash)
294                         free (to->rev_hash);
295                 to->rev_hash = n;
296         }
297         to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
298 }
299
300 typedef struct {
301         uintptr_t objaddr;
302         HeapClassDesc *hklass;
303         uintptr_t num_refs;
304         uintptr_t refs [0];
305 } HeapObjectDesc;
306
307 typedef struct _HeapShot HeapShot;
308 struct _HeapShot {
309         HeapShot *next;
310         uint64_t timestamp;
311         int class_count;
312         int hash_size;
313         HeapClassDesc **class_hash;
314         HeapClassDesc **sorted;
315         HeapObjectDesc **objects_hash;
316         uintptr_t objects_count;
317         uintptr_t objects_hash_size;
318 };
319
320 static HeapShot *heap_shots = NULL;
321 static int num_heap_shots = 0;
322
323 static HeapShot*
324 new_heap_shot (uint64_t timestamp)
325 {
326         HeapShot *hs = calloc (sizeof (HeapShot), 1);
327         hs->hash_size = 4;
328         hs->class_hash = calloc (sizeof (void*), hs->hash_size);
329         hs->timestamp = timestamp;
330         num_heap_shots++;
331         hs->next = heap_shots;
332         heap_shots = hs;
333         return hs;
334 }
335
336 static HeapClassDesc*
337 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
338 {
339         int i;
340         unsigned int start_pos;
341         start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
342         i = start_pos;
343         do {
344                 HeapClassDesc* cd = hs->class_hash [i];
345                 if (!cd)
346                         return NULL;
347                 if (cd->klass == klass)
348                         return cd;
349                 /* wrap around */
350                 if (++i == hs->hash_size)
351                         i = 0;
352         } while (i != start_pos);
353         return NULL;
354 }
355
356 static int
357 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
358 {
359         uintptr_t i;
360         uintptr_t start_pos;
361         start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
362         i = start_pos;
363         do {
364                 if (hash [i] && hash [i]->klass == klass) {
365                         hash [i]->total_size += size;
366                         hash [i]->count += count;
367                         *retv = hash [i];
368                         return 0;
369                 } else if (!hash [i]) {
370                         if (*retv) {
371                                 hash [i] = *retv;
372                                 return 1;
373                         }
374                         hash [i] = calloc (sizeof (HeapClassDesc), 1);
375                         hash [i]->klass = klass;
376                         hash [i]->total_size += size;
377                         hash [i]->count += count;
378                         *retv = hash [i];
379                         return 1;
380                 }
381                 /* wrap around */
382                 if (++i == hsize)
383                         i = 0;
384         } while (i != start_pos);
385         /* should not happen */
386         printf ("failed heap class store\n");
387         return 0;
388 }
389
390 static HeapClassDesc*
391 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
392 {
393         HeapClassDesc *res;
394         int i;
395         if (hs->class_count * 2 >= hs->hash_size) {
396                 HeapClassDesc **n;
397                 int old_size = hs->hash_size;
398                 hs->hash_size *= 2;
399                 if (hs->hash_size == 0)
400                         hs->hash_size = 4;
401                 n = calloc (sizeof (void*) * hs->hash_size, 1);
402                 for (i = 0; i < old_size; ++i) {
403                         res = hs->class_hash [i];
404                         if (hs->class_hash [i])
405                                 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
406                 }
407                 if (hs->class_hash)
408                         free (hs->class_hash);
409                 hs->class_hash = n;
410         }
411         res = NULL;
412         hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
413         //if (res->count == 1)
414         //      printf ("added heap class: %s\n", res->klass->name);
415         return res;
416 }
417
418 static HeapObjectDesc*
419 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
420 {
421         HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
422         ho->objaddr = objaddr;
423         ho->hklass = hklass;
424         ho->num_refs = num_refs;
425         return ho;
426 }
427
428 static uintptr_t
429 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
430 {
431         uintptr_t i;
432         uintptr_t start_pos;
433         HeapObjectDesc **hash = hs->objects_hash;
434         start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
435         i = start_pos;
436         do {
437                 if (hash [i] && hash [i]->objaddr == objaddr) {
438                         return i;
439                 } else if (!hash [i]) {
440                         break; /* fail */
441                 }
442                 /* wrap around */
443                 if (++i == hs->objects_hash_size)
444                         i = 0;
445         } while (i != start_pos);
446         /* should not happen */
447         printf ("failed heap obj update\n");
448         return -1;
449 }
450
451 static HeapObjectDesc*
452 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
453 {
454         HeapObjectDesc **hash = hs->objects_hash;
455         uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
456         if (i >= 0) {
457                 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
458                 *ref_offset = hash [i]->num_refs;
459                 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
460                 free (hash [i]);
461                 hash [i] = ho;
462                 return ho;
463         }
464         /* should not happen */
465         printf ("failed heap obj update\n");
466         return NULL;
467         
468 }
469
470 static uintptr_t
471 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
472 {
473         uintptr_t i;
474         uintptr_t start_pos;
475         start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
476         i = start_pos;
477         do {
478                 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
479                         printf ("duplicate object!\n");
480                         return 0;
481                 } else if (!hash [i]) {
482                         hash [i] = obj;
483                         return 1;
484                 }
485                 /* wrap around */
486                 if (++i == hsize)
487                         i = 0;
488         } while (i != start_pos);
489         /* should not happen */
490         printf ("failed heap obj store\n");
491         return 0;
492 }
493
494 static void
495 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
496 {
497         uintptr_t i;
498         if (hs->objects_count * 2 >= hs->objects_hash_size) {
499                 HeapObjectDesc **n;
500                 uintptr_t old_size = hs->objects_hash_size;
501                 hs->objects_hash_size *= 2;
502                 if (hs->objects_hash_size == 0)
503                         hs->objects_hash_size = 4;
504                 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
505                 for (i = 0; i < old_size; ++i) {
506                         if (hs->objects_hash [i])
507                                 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
508                 }
509                 if (hs->objects_hash)
510                         free (hs->objects_hash);
511                 hs->objects_hash = n;
512         }
513         hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
514 }
515
516 static void
517 heap_shot_resolve_reverse_refs (HeapShot *hs)
518 {
519         uintptr_t i;
520         for (i = 0; i < hs->objects_hash_size; ++i) {
521                 uintptr_t r;
522                 HeapObjectDesc *ho = hs->objects_hash [i];
523                 if (!ho)
524                         continue;
525                 for (r = 0; r < ho->num_refs; ++r) {
526                         uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
527                         add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
528                 }
529         }
530 }
531
532 static void
533 heap_shot_free_objects (HeapShot *hs)
534 {
535         uintptr_t i;
536         for (i = 0; i < hs->objects_hash_size; ++i) {
537                 HeapObjectDesc *ho = hs->objects_hash [i];
538                 if (ho)
539                         free (ho);
540         }
541         if (hs->objects_hash)
542                 free (hs->objects_hash);
543         hs->objects_hash = NULL;
544         hs->objects_hash_size = 0;
545         hs->objects_count = 0;
546 }
547
548 struct _BackTrace {
549         BackTrace *next;
550         unsigned int hash;
551         int count;
552         int id;
553         MethodDesc *methods [1];
554 };
555
556 static BackTrace *backtrace_hash [HASH_SIZE];
557 static BackTrace **backtraces = NULL;
558 static int num_backtraces = 0;
559 static int next_backtrace = 0;
560
561 static int
562 hash_backtrace (int count, MethodDesc **methods)
563 {
564         int hash = count;
565         int i;
566         for (i = 0; i < count; ++i) {
567                 hash = (hash << 5) - hash + methods [i]->method;
568         }
569         return hash;
570 }
571
572 static int
573 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
574 {
575         int i;
576         if (bt->count != count)
577                 return 0;
578         for (i = 0; i < count; ++i)
579                 if (methods [i] != bt->methods [i])
580                         return 0;
581         return 1;
582 }
583
584 static BackTrace*
585 add_backtrace (int count, MethodDesc **methods)
586 {
587         int hash = hash_backtrace (count, methods);
588         int slot = (hash & 0xffff) % HASH_SIZE;
589         BackTrace *bt = backtrace_hash [slot];
590         while (bt) {
591                 if (bt->hash == hash && compare_backtrace (bt, count, methods))
592                         return bt;
593                 bt = bt->next;
594         }
595         bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
596         bt->next = backtrace_hash [slot];
597         backtrace_hash [slot] = bt;
598         if (next_backtrace == num_backtraces) {
599                 num_backtraces *= 2;
600                 if (!num_backtraces)
601                         num_backtraces = 16;
602                 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
603         }
604         bt->id = next_backtrace++;
605         backtraces [bt->id] = bt;
606         bt->count = count;
607         bt->hash = hash;
608         for (slot = 0; slot < count; ++slot)
609                 bt->methods [slot] = methods [slot];
610
611         return bt;
612 }
613
614 typedef struct _MonitorDesc MonitorDesc;
615 typedef struct _ThreadContext ThreadContext;
616
617 typedef struct {
618         FILE *file;
619 #if defined (HAVE_ZLIB)
620         gzFile *gzfile;
621 #endif
622         unsigned char *buf;
623         int size;
624         int data_version;
625         int version_major;
626         int version_minor;
627         int timer_overhead;
628         uint64_t startup_time;
629         ThreadContext *threads;
630         ThreadContext *current;
631 } ProfContext;
632
633 struct _ThreadContext {
634         ThreadContext *next;
635         intptr_t thread_id;
636         char *name;
637         /* emulated stack */
638         MethodDesc **stack;
639         uint64_t *time_stack;
640         uint64_t *callee_time_stack;
641         uint64_t last_time;
642         uint64_t contention_start;
643         MonitorDesc *monitor;
644         int stack_size;
645         int stack_id;
646         HeapShot *current_heap_shot;
647 };
648
649 static void
650 ensure_buffer (ProfContext *ctx, int size)
651 {
652         if (ctx->size < size) {
653                 ctx->buf = realloc (ctx->buf, size);
654                 ctx->size = size;
655         }
656 }
657
658 static int
659 load_data (ProfContext *ctx, int size)
660 {
661         ensure_buffer (ctx, size);
662 #if defined (HAVE_ZLIB)
663         if (ctx->gzfile)
664                 return gzread (ctx->gzfile, ctx->buf, size) == size;
665         else
666 #endif
667                 return fread (ctx->buf, size, 1, ctx->file);
668 }
669
670 static ThreadContext*
671 get_thread (ProfContext *ctx, intptr_t thread_id)
672 {
673         ThreadContext *thread;
674         if (ctx->current && ctx->current->thread_id == thread_id)
675                 return ctx->current;
676         thread = ctx->threads;
677         while (thread) {
678                 if (thread->thread_id == thread_id) {
679                         return thread;
680                 }
681                 thread = thread->next;
682         }
683         thread = calloc (sizeof (ThreadContext), 1);
684         thread->next = ctx->threads;
685         ctx->threads = thread;
686         thread->thread_id = thread_id;
687         thread->last_time = 0;
688         thread->stack_id = 0;
689         thread->stack_size = 32;
690         thread->stack = malloc (thread->stack_size * sizeof (void*));
691         thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
692         thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
693         return thread;
694 }
695
696 static ThreadContext*
697 load_thread (ProfContext *ctx, intptr_t thread_id)
698 {
699         ThreadContext *thread = get_thread (ctx, thread_id);
700         ctx->current = thread;
701         return thread;
702 }
703
704 static void
705 ensure_thread_stack (ThreadContext *thread)
706 {
707         if (thread->stack_id == thread->stack_size) {
708                 thread->stack_size *= 2;
709                 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
710                 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
711                 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
712         }
713 }
714
715 static int
716 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
717 {
718         int i;
719         unsigned int start_pos;
720         start_pos = bt->hash % size;
721         i = start_pos;
722         do {
723                 if (traces [i].bt == bt) {
724                         traces [i].count += value;
725                         return 0;
726                 } else if (!traces [i].bt) {
727                         traces [i].bt = bt;
728                         traces [i].count += value;
729                         return 1;
730                 }
731                 /* wrap around */
732                 if (++i == size)
733                         i = 0;
734         } while (i != start_pos);
735         /* should not happen */
736         printf ("failed trace store\n");
737         return 0;
738 }
739
740 static void
741 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
742 {
743         int i;
744         if (!collect_traces)
745                 return;
746         if (trace->count * 2 >= trace->size) {
747                 CallContext *n;
748                 int old_size = trace->size;
749                 trace->size *= 2;
750                 if (trace->size == 0)
751                         trace->size = 4;
752                 n = calloc (sizeof (CallContext) * trace->size, 1);
753                 for (i = 0; i < old_size; ++i) {
754                         if (trace->traces [i].bt)
755                                 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
756                 }
757                 if (trace->traces)
758                         free (trace->traces);
759                 trace->traces = n;
760         }
761         trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
762 }
763
764 static BackTrace*
765 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
766 {
767         BackTrace *bt;
768         int count = thread->stack_id;
769         if (!collect_traces)
770                 return NULL;
771         if (count > trace_max)
772                 count = trace_max;
773         bt = add_backtrace (count, thread->stack + thread->stack_id - count);
774         add_trace_bt (bt, trace, value);
775         return bt;
776 }
777
778 static BackTrace*
779 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
780 {
781         BackTrace *bt;
782         if (!collect_traces)
783                 return NULL;
784         if (count > trace_max)
785                 count = trace_max;
786         bt = add_backtrace (count, methods);
787         add_trace_bt (bt, trace, value);
788         return bt;
789 }
790
791 static int
792 compare_callc (const void *a, const void *b)
793 {
794         const CallContext *A = a;
795         const CallContext *B = b;
796         if (B->count == A->count)
797                 return 0;
798         if (B->count < A->count)
799                 return -1;
800         return 1;
801 }
802
803 static void
804 sort_context_array (TraceDesc* traces)
805 {
806         int i, j;
807         for (i = 0, j = 0; i < traces->size; ++i) {
808                 if (traces->traces [i].bt) {
809                         traces->traces [j].bt = traces->traces [i].bt;
810                         traces->traces [j].count = traces->traces [i].count;
811                         j++;
812                 }
813         }
814         qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
815 }
816
817 static void
818 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
819 {
820         ensure_thread_stack (thread);
821         thread->time_stack [thread->stack_id] = timestamp;
822         thread->callee_time_stack [thread->stack_id] = 0;
823         thread->stack [thread->stack_id++] = method;
824         method->recurse_count++;
825 }
826
827 static void
828 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
829 {
830         method->recurse_count--;
831         if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
832                 uint64_t tdiff;
833                 thread->stack_id--;
834                 method->calls++;
835                 if (timestamp < thread->time_stack [thread->stack_id])
836                         fprintf (outfile, "time went backwards for %s\n", method->name);
837                 tdiff = timestamp - thread->time_stack [thread->stack_id];
838                 if (thread->callee_time_stack [thread->stack_id] > tdiff)
839                         fprintf (outfile, "callee time bigger for %s\n", method->name);
840                 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
841                 method->callee_time += thread->callee_time_stack [thread->stack_id];
842                 if (thread->stack_id)
843                         thread->callee_time_stack [thread->stack_id - 1] += tdiff;
844                 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
845         } else {
846                 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
847         }
848 }
849
850 typedef struct {
851         uint64_t start_time; /* move this per-thread? */
852         uint64_t total_time;
853         uint64_t max_time;
854         int count;
855 } GCDesc;
856 static GCDesc gc_info [3];
857 static uint64_t max_heap_size;
858 static uint64_t gc_object_moves;
859 static int gc_resizes;
860 typedef struct {
861         uint64_t created;
862         uint64_t destroyed;
863         uint64_t max_live;
864         TraceDesc traces;
865 } HandleInfo;
866 static HandleInfo handle_info [4];
867
868 static const char*
869 gc_event_name (int ev)
870 {
871         switch (ev) {
872         case MONO_GC_EVENT_START: return "start";
873         case MONO_GC_EVENT_MARK_START: return "mark start";
874         case MONO_GC_EVENT_MARK_END: return "mark end";
875         case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
876         case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
877         case MONO_GC_EVENT_END: return "end";
878         case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
879         case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
880         case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
881         case MONO_GC_EVENT_POST_START_WORLD: return "post start";
882         default:
883                 return "unknown";
884         }
885 }
886
887 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
888 static uint64_t throw_count = 0;
889 static TraceDesc exc_traces;
890
891 static const char*
892 clause_name (int type)
893 {
894         switch (type) {
895         case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
896         case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
897         case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
898         case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
899         default: return "invalid";
900         }
901 }
902
903 static uint64_t monitor_contention;
904 static uint64_t monitor_failed;
905 static uint64_t monitor_acquired;
906
907 struct _MonitorDesc {
908         MonitorDesc *next;
909         uintptr_t objid;
910         uintptr_t contentions;
911         uint64_t wait_time;
912         uint64_t max_wait_time;
913         TraceDesc traces;
914 };
915
916 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
917 static int num_monitors = 0;
918
919 static MonitorDesc*
920 lookup_monitor (uintptr_t objid)
921 {
922         int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
923         MonitorDesc *cd = monitor_hash [slot];
924         while (cd && cd->objid != objid)
925                 cd = cd->next;
926         if (!cd) {
927                 cd = calloc (sizeof (MonitorDesc), 1);
928                 cd->objid = objid;
929                 cd->next = monitor_hash [slot];
930                 monitor_hash [slot] = cd;
931                 num_monitors++;
932         }
933         return cd;
934 }
935
936 static const char*
937 monitor_ev_name (int ev)
938 {
939         switch (ev) {
940         case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
941         case MONO_PROFILER_MONITOR_DONE: return "acquired";
942         case MONO_PROFILER_MONITOR_FAIL: return "not taken";
943         default: return "invalid";
944         }
945 }
946
947 static const char*
948 get_handle_name (int htype)
949 {
950         switch (htype) {
951         case 0: return "weak";
952         case 1: return "weaktrack";
953         case 2: return "normal";
954         case 3: return "pinned";
955         default: return "unknown";
956         }
957 }
958
959 static MethodDesc**
960 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
961 {
962         MethodDesc **frames;
963         int i;
964         int flags = decode_uleb128 (p, &p);
965         int count = decode_uleb128 (p, &p);
966         if (flags != 0)
967                 return NULL;
968         if (count > *size)
969                 frames = malloc (count * sizeof (void*));
970         else
971                 frames = sframes;
972         for (i = 0; i < count; ++i) {
973                 intptr_t ptrdiff = decode_sleb128 (p, &p);
974                 frames [i] = lookup_method (ptr_base + ptrdiff);
975         }
976         *size = count;
977         *endp = p;
978         return frames;
979 }
980
981 static void
982 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
983 {
984         int i;
985         for (i = 0; i < num_tracked_objects; ++i) {
986                 if (tracked_objects [i] != obj)
987                         continue;
988                 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
989                 if (bt && bt->count) {
990                         int k;
991                         for (k = 0; k < bt->count; ++k)
992                                 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
993                 }
994         }
995 }
996
997 static void
998 track_handle (uintptr_t obj, int htype, uint32_t handle)
999 {
1000         int i;
1001         for (i = 0; i < num_tracked_objects; ++i) {
1002                 if (tracked_objects [i] == obj)
1003                         fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1004         }
1005 }
1006
1007 static void
1008 track_move (uintptr_t src, uintptr_t dst)
1009 {
1010         int i;
1011         for (i = 0; i < num_tracked_objects; ++i) {
1012                 if (tracked_objects [i] == src)
1013                         fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1014                 else if (tracked_objects [i] == dst)
1015                         fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1016         }
1017 }
1018
1019 static void
1020 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1021 {
1022         int i;
1023         for (i = 0; i < num_tracked_objects; ++i) {
1024                 if (tracked_objects [i] == obj) 
1025                         fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1026         }
1027 }
1028
1029 static void
1030 found_object (uintptr_t obj)
1031 {
1032         num_tracked_objects ++;
1033         tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1034         tracked_objects [num_tracked_objects - 1] = obj;
1035 }
1036
1037 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1038 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1039
1040 static int
1041 decode_buffer (ProfContext *ctx)
1042 {
1043         unsigned char *p;
1044         unsigned char *end;
1045         intptr_t thread_id;
1046         intptr_t ptr_base;
1047         intptr_t obj_base;
1048         intptr_t method_base;
1049         uint64_t time_base;
1050         uint64_t file_offset;
1051         int len, i;
1052         ThreadContext *thread;
1053
1054         file_offset = ftell (ctx->file);
1055         if (!load_data (ctx, 48))
1056                 return 0;
1057         p = ctx->buf;
1058         if (read_int32 (p) != BUF_ID)
1059                 return 0;
1060         len = read_int32 (p + 4);
1061         time_base = read_int64 (p + 8);
1062         ptr_base = read_int64 (p + 16);
1063         obj_base = read_int64 (p + 24);
1064         thread_id = read_int64 (p + 32);
1065         method_base = read_int64 (p + 40);
1066         if (debug)
1067                 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1068         thread = load_thread (ctx, thread_id);
1069         if (!load_data (ctx, len))
1070                 return 0;
1071         if (!startup_time) {
1072                 startup_time = time_base;
1073                 if (time_from) {
1074                         time_from += startup_time;
1075                         time_to += startup_time;
1076                 }
1077                 if (!thread->name)
1078                         thread->name = pstrdup ("Main");
1079         }
1080         for (i = 0; i < thread->stack_id; ++i)
1081                 thread->stack [i]->recurse_count++;
1082         p = ctx->buf;
1083         end = p + len;
1084         while (p < end) {
1085                 switch (*p & 0xf) {
1086                 case TYPE_GC: {
1087                         int subtype = *p & 0xf0;
1088                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1089                         LOG_TIME (time_base, tdiff);
1090                         time_base += tdiff;
1091                         if (subtype == TYPE_GC_RESIZE) {
1092                                 uint64_t new_size = decode_uleb128 (p, &p);
1093                                 if (debug)
1094                                         fprintf (outfile, "gc heap resized to %llu\n", new_size);
1095                                 gc_resizes++;
1096                                 if (new_size > max_heap_size)
1097                                         max_heap_size = new_size;
1098                         } else if (subtype == TYPE_GC_EVENT) {
1099                                 uint64_t ev = decode_uleb128 (p, &p);
1100                                 int gen = decode_uleb128 (p, &p);
1101                                 if (debug)
1102                                         fprintf (outfile, "gc event for gen%d: %s at %llu\n", gen - 1, gc_event_name (ev), time_base);
1103                                 if (gen > 2) {
1104                                         fprintf (outfile, "incorrect gc gen: %d\n", gen);
1105                                         break;
1106                                 }
1107                                 if (ev == MONO_GC_EVENT_START) {
1108                                         gc_info [gen].start_time = time_base;
1109                                         gc_info [gen].count++;
1110                                 } else if (ev == MONO_GC_EVENT_END) {
1111                                         tdiff = time_base - gc_info [gen].start_time;
1112                                         gc_info [gen].total_time += tdiff;
1113                                         if (tdiff > gc_info [gen].max_time)
1114                                                 gc_info [gen].max_time = tdiff;
1115                                 }
1116                         } else if (subtype == TYPE_GC_MOVE) {
1117                                 int j, num = decode_uleb128 (p, &p);
1118                                 gc_object_moves += num / 2;
1119                                 for (j = 0; j < num; j += 2) {
1120                                         intptr_t obj1diff = decode_sleb128 (p, &p);
1121                                         intptr_t obj2diff = decode_sleb128 (p, &p);
1122                                         if (num_tracked_objects)
1123                                                 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1124                                         if (debug) {
1125                                                 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1126                                         }
1127                                 }
1128                         } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1129                                 int htype = decode_uleb128 (p, &p);
1130                                 uint32_t handle = decode_uleb128 (p, &p);
1131                                 intptr_t objdiff = decode_sleb128 (p, &p);
1132                                 if (htype > 3)
1133                                         return 0;
1134                                 handle_info [htype].created++;
1135                                 add_trace_thread (thread, &handle_info [htype].traces, 1);
1136                                 /* FIXME: we don't take into account timing here */
1137                                 if (handle_info [htype].created > handle_info [htype].max_live)
1138                                         handle_info [htype].max_live = handle_info [htype].created;
1139                                 if (num_tracked_objects)
1140                                         track_handle (OBJ_ADDR (objdiff), htype, handle);
1141                                 if (debug)
1142                                         fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
1143                         } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
1144                                 int htype = decode_uleb128 (p, &p);
1145                                 uint32_t handle = decode_uleb128 (p, &p);
1146                                 if (htype > 3)
1147                                         return 0;
1148                                 handle_info [htype].created--;
1149                                 if (debug)
1150                                         fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
1151                         }
1152                         break;
1153                 }
1154                 case TYPE_METADATA: {
1155                         int error = *p & TYPE_LOAD_ERR;
1156                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1157                         int mtype = *p++;
1158                         intptr_t ptrdiff = decode_sleb128 (p, &p);
1159                         LOG_TIME (time_base, tdiff);
1160                         time_base += tdiff;
1161                         if (mtype == TYPE_CLASS) {
1162                                 intptr_t imptrdiff = decode_sleb128 (p, &p);
1163                                 uint64_t flags = decode_uleb128 (p, &p);
1164                                 if (flags) {
1165                                         fprintf (outfile, "non-zero flags in class\n");
1166                                         return 0;
1167                                 }
1168                                 if (debug)
1169                                         fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
1170                                 if (!error)
1171                                         add_class (ptr_base + ptrdiff, (char*)p);
1172                                 while (*p) p++;
1173                                 p++;
1174                         } else if (mtype == TYPE_IMAGE) {
1175                                 uint64_t flags = decode_uleb128 (p, &p);
1176                                 if (flags) {
1177                                         fprintf (outfile, "non-zero flags in image\n");
1178                                         return 0;
1179                                 }
1180                                 if (debug)
1181                                         fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
1182                                 if (!error)
1183                                         add_image (ptr_base + ptrdiff, (char*)p);
1184                                 while (*p) p++;
1185                                 p++;
1186                         } else if (mtype == TYPE_THREAD) {
1187                                 ThreadContext *nt;
1188                                 uint64_t flags = decode_uleb128 (p, &p);
1189                                 if (flags) {
1190                                         fprintf (outfile, "non-zero flags in thread\n");
1191                                         return 0;
1192                                 }
1193                                 nt = get_thread (ctx, ptr_base * ptrdiff);
1194                                 nt->name = pstrdup ((char*)p);
1195                                 if (debug)
1196                                         fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
1197                                 while (*p) p++;
1198                                 p++;
1199                         }
1200                         break;
1201                 }
1202                 case TYPE_ALLOC: {
1203                         int has_bt = *p & TYPE_ALLOC_BT;
1204                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1205                         intptr_t ptrdiff = decode_sleb128 (p, &p);
1206                         intptr_t objdiff = decode_sleb128 (p, &p);
1207                         uint64_t len;
1208                         int num_bt = 0;
1209                         MethodDesc* sframes [8];
1210                         MethodDesc** frames = sframes;
1211                         ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1212                         len = decode_uleb128 (p, &p);
1213                         LOG_TIME (time_base, tdiff);
1214                         time_base += tdiff;
1215                         if (debug)
1216                                 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
1217                         if (has_bt) {
1218                                 num_bt = 8;
1219                                 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1220                                 if (!frames) {
1221                                         fprintf (outfile, "Cannot load backtrace\n");
1222                                         return 0;
1223                                 }
1224                         }
1225                         if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
1226                                 BackTrace *bt;
1227                                 cd->allocs++;
1228                                 cd->alloc_size += len;
1229                                 if (has_bt)
1230                                         bt = add_trace_methods (frames, num_bt, &cd->traces, len);
1231                                 else
1232                                         bt = add_trace_thread (thread, &cd->traces, len);
1233                                 if (find_size && len >= find_size) {
1234                                         if (!find_name || strstr (cd->name, find_name))
1235                                                 found_object (OBJ_ADDR (objdiff));
1236                                 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
1237                                         found_object (OBJ_ADDR (objdiff));
1238                                 }
1239                                 if (num_tracked_objects)
1240                                         tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
1241                         }
1242                         if (frames != sframes)
1243                                 free (frames);
1244                         break;
1245                 }
1246                 case TYPE_METHOD: {
1247                         int subtype = *p & 0xf0;
1248                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1249                         int64_t ptrdiff = decode_sleb128 (p, &p);
1250                         LOG_TIME (time_base, tdiff);
1251                         time_base += tdiff;
1252                         method_base += ptrdiff;
1253                         if (subtype == TYPE_JIT) {
1254                                 intptr_t codediff = decode_sleb128 (p, &p);
1255                                 int codelen = decode_uleb128 (p, &p);
1256                                 if (debug)
1257                                         fprintf (outfile, "jitted method %p (%s), size: %d\n", (void*)(method_base), p, codelen);
1258                                 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
1259                                 while (*p) p++;
1260                                 p++;
1261                         } else {
1262                                 MethodDesc *method;
1263                                 if ((thread_filter && thread_filter != thread->thread_id))
1264                                         break;
1265                                 method = lookup_method (method_base);
1266                                 if (subtype == TYPE_ENTER) {
1267                                         add_trace_thread (thread, &method->traces, 1);
1268                                         push_method (thread, method, time_base);
1269                                 } else {
1270                                         pop_method (thread, method, time_base);
1271                                 }
1272                                 if (debug)
1273                                         fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
1274                         }
1275                         break;
1276                 }
1277                 case TYPE_HEAP: {
1278                         int subtype = *p & 0xf0;
1279                         if (subtype == TYPE_HEAP_OBJECT) {
1280                                 HeapObjectDesc *ho;
1281                                 int i;
1282                                 intptr_t objdiff = decode_sleb128 (p + 1, &p);
1283                                 intptr_t ptrdiff = decode_sleb128 (p, &p);
1284                                 uint64_t size = decode_uleb128 (p, &p);
1285                                 uintptr_t num = decode_uleb128 (p, &p);
1286                                 uintptr_t ref_offset;
1287                                 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1288                                 if (size) {
1289                                         HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
1290                                         if (collect_traces) {
1291                                                 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
1292                                                 add_heap_shot_obj (thread->current_heap_shot, ho);
1293                                                 ref_offset = 0;
1294                                         }
1295                                 } else {
1296                                         if (collect_traces)
1297                                                 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
1298                                 }
1299                                 for (i = 0; i < num; ++i) {
1300                                         /* FIXME: use object distance to measure how good
1301                                          * the GC is at keeping related objects close
1302                                          */
1303                                         intptr_t obj1diff = decode_sleb128 (p, &p);
1304                                         if (collect_traces)
1305                                                 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
1306                                         if (num_tracked_objects)
1307                                                 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1308                                 }
1309                                 if (debug && size)
1310                                         fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1311                         } else if (subtype == TYPE_HEAP_END) {
1312                                 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1313                                 LOG_TIME (time_base, tdiff);
1314                                 time_base += tdiff;
1315                                 if (debug)
1316                                         fprintf (outfile, "heap shot end\n");
1317                                 if (collect_traces) {
1318                                         heap_shot_resolve_reverse_refs (thread->current_heap_shot);
1319                                         heap_shot_free_objects (thread->current_heap_shot);
1320                                 }
1321                                 thread->current_heap_shot = NULL;
1322                         } else if (subtype == TYPE_HEAP_START) {
1323                                 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1324                                 LOG_TIME (time_base, tdiff);
1325                                 time_base += tdiff;
1326                                 if (debug)
1327                                         fprintf (outfile, "heap shot start\n");
1328                                 thread->current_heap_shot = new_heap_shot (time_base);
1329                         }
1330                         break;
1331                 }
1332                 case TYPE_MONITOR: {
1333                         int event = (*p >> 4) & 0x3;
1334                         int has_bt = *p & TYPE_MONITOR_BT;
1335                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1336                         intptr_t objdiff = decode_sleb128 (p, &p);
1337                         MethodDesc* sframes [8];
1338                         MethodDesc** frames = sframes;
1339                         int record;
1340                         int num_bt = 0;
1341                         LOG_TIME (time_base, tdiff);
1342                         time_base += tdiff;
1343                         record = (!thread_filter || thread_filter == thread->thread_id);
1344                         if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1345                                 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1346                                 if (record) {
1347                                         monitor_contention++;
1348                                         mdesc->contentions++;
1349                                         thread->monitor = mdesc;
1350                                         thread->contention_start = time_base;
1351                                 }
1352                                 if (has_bt) {
1353                                         num_bt = 8;
1354                                         frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1355                                         if (!frames) {
1356                                                 fprintf (outfile, "Cannot load backtrace\n");
1357                                                 return 0;
1358                                         }
1359                                         if (record)
1360                                                 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1361                                 } else {
1362                                         if (record)
1363                                                 add_trace_thread (thread, &mdesc->traces, 1);
1364                                 }
1365                         } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1366                                 if (record) {
1367                                         monitor_failed++;
1368                                         if (thread->monitor && thread->contention_start) {
1369                                                 uint64_t wait_time = time_base - thread->contention_start;
1370                                                 if (wait_time > thread->monitor->max_wait_time)
1371                                                         thread->monitor->max_wait_time = wait_time;
1372                                                 thread->monitor->wait_time += wait_time;
1373                                                 thread->monitor = NULL;
1374                                                 thread->contention_start = 0;
1375                                         }
1376                                 }
1377                         } else if (event == MONO_PROFILER_MONITOR_DONE) {
1378                                 if (record) {
1379                                         monitor_acquired++;
1380                                         if (thread->monitor && thread->contention_start) {
1381                                                 uint64_t wait_time = time_base - thread->contention_start;
1382                                                 if (wait_time > thread->monitor->max_wait_time)
1383                                                         thread->monitor->max_wait_time = wait_time;
1384                                                 thread->monitor->wait_time += wait_time;
1385                                                 thread->monitor = NULL;
1386                                                 thread->contention_start = 0;
1387                                         }
1388                                 }
1389                         }
1390                         if (debug)
1391                                 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1392                         if (frames != sframes)
1393                                 free (frames);
1394                         break;
1395                 }
1396                 case TYPE_EXCEPTION: {
1397                         int subtype = *p & 0x70;
1398                         int has_bt = *p & TYPE_EXCEPTION_BT;
1399                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1400                         MethodDesc* sframes [8];
1401                         MethodDesc** frames = sframes;
1402                         int record;
1403                         LOG_TIME (time_base, tdiff);
1404                         time_base += tdiff;
1405                         record = (!thread_filter || thread_filter == thread->thread_id);
1406                         if (subtype == TYPE_CLAUSE) {
1407                                 int clause_type = decode_uleb128 (p, &p);
1408                                 int clause_num = decode_uleb128 (p, &p);
1409                                 int64_t ptrdiff = decode_sleb128 (p, &p);
1410                                 method_base += ptrdiff;
1411                                 if (record)
1412                                         clause_summary [clause_type]++;
1413                                 if (debug)
1414                                         fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1415                         } else {
1416                                 intptr_t objdiff = decode_sleb128 (p, &p);
1417                                 if (record)
1418                                         throw_count++;
1419                                 if (has_bt) {
1420                                         has_bt = 8;
1421                                         frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1422                                         if (!frames) {
1423                                                 fprintf (outfile, "Cannot load backtrace\n");
1424                                                 return 0;
1425                                         }
1426                                         if (record)
1427                                                 add_trace_methods (frames, has_bt, &exc_traces, 1);
1428                                 } else {
1429                                         if (record)
1430                                                 add_trace_thread (thread, &exc_traces, 1);
1431                                 }
1432                                 if (frames != sframes)
1433                                         free (frames);
1434                                 if (debug)
1435                                         fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1436                         }
1437                         break;
1438                 }
1439                 default:
1440                         fprintf (outfile, "unhandled profiler event: 0x%x\n", *p);
1441                         exit (1);
1442                 }
1443         }
1444         thread->last_time = time_base;
1445         for (i = 0; i < thread->stack_id; ++i)
1446                 thread->stack [i]->recurse_count = 0;
1447         return 1;
1448 }
1449
1450 static ProfContext*
1451 load_file (char *name)
1452 {
1453         unsigned char *p;
1454         ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1455         if (strcmp (name, "-") == 0)
1456                 ctx->file = stdin;
1457         else
1458                 ctx->file = fopen (name, "rb");
1459         if (!ctx->file) {
1460                 printf ("Cannot open file: %s\n", name);
1461                 exit (1);
1462         }
1463 #if defined (HAVE_ZLIB)
1464         ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
1465 #endif
1466         if (!load_data (ctx, 32))
1467                 return NULL;
1468         p = ctx->buf;
1469         if (read_int32 (p) != LOG_HEADER_ID || p [6] != LOG_DATA_VERSION)
1470                 return NULL;
1471         ctx->version_major = p [4];
1472         ctx->version_minor = p [5];
1473         ctx->data_version = p [6];
1474         /* reading 64 bit files on 32 bit systems not supported yet */
1475         if (p [7] > sizeof (void*))
1476                 return NULL;
1477         if (read_int32 (p + 20)) /* flags must be 0 */
1478                 return NULL;
1479         ctx->startup_time = read_int64 (p + 8);
1480         ctx->timer_overhead = read_int32 (p + 16);
1481         return ctx;
1482 }
1483
1484 enum {
1485         ALLOC_SORT_BYTES,
1486         ALLOC_SORT_COUNT
1487 };
1488 static int alloc_sort_mode = ALLOC_SORT_BYTES;
1489
1490 static int
1491 compare_class (const void *a, const void *b)
1492 {
1493         ClassDesc *const*A = a;
1494         ClassDesc *const*B = b;
1495         uint64_t vala, valb;
1496         if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1497                 vala = (*A)->alloc_size;
1498                 valb = (*B)->alloc_size;
1499         } else {
1500                 vala = (*A)->allocs;
1501                 valb = (*B)->allocs;
1502         }
1503         if (valb == vala)
1504                 return 0;
1505         if (valb < vala)
1506                 return -1;
1507         return 1;
1508 }
1509
1510 static void
1511 dump_header (ProfContext *ctx)
1512 {
1513         time_t st = ctx->startup_time / 1000;
1514         char *t = ctime (&st);
1515         fprintf (outfile, "\nMono log profiler data\n");
1516         fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
1517         fprintf (outfile, "\tData version: %d\n", ctx->data_version);
1518         fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
1519         fprintf (outfile, "\tProgram startup: %s\n", t);
1520 }
1521
1522 static void
1523 dump_traces (TraceDesc *traces, const char *desc)
1524 {
1525         int j;
1526         if (!show_traces)
1527                 return;
1528         if (!traces->count)
1529                 return;
1530         sort_context_array (traces);
1531         for (j = 0; j < traces->count; ++j) {
1532                 int k;
1533                 BackTrace *bt;
1534                 bt = traces->traces [j].bt;
1535                 if (!bt->count)
1536                         continue;
1537                 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
1538                 for (k = 0; k < bt->count; ++k)
1539                         fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
1540         }
1541 }
1542
1543 static void
1544 dump_threads (ProfContext *ctx)
1545 {
1546         ThreadContext *thread;
1547         fprintf (outfile, "\nThread summary\n");
1548         for (thread = ctx->threads; thread; thread = thread->next) {
1549                 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
1550         }
1551 }
1552
1553 static void
1554 dump_exceptions (void)
1555 {
1556         int i;
1557         fprintf (outfile, "\nException summary\n");
1558         fprintf (outfile, "\tThrows: %llu\n", throw_count);
1559         dump_traces (&exc_traces, "throws");
1560         for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
1561                 if (!clause_summary [i])
1562                         continue;
1563                 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
1564         }
1565 }
1566
1567 static int
1568 compare_monitor (const void *a, const void *b)
1569 {
1570         MonitorDesc *const*A = a;
1571         MonitorDesc *const*B = b;
1572         if ((*B)->wait_time == (*A)->wait_time)
1573                 return 0;
1574         if ((*B)->wait_time < (*A)->wait_time)
1575                 return -1;
1576         return 1;
1577 }
1578
1579 static void
1580 dump_monitors (void)
1581 {
1582         MonitorDesc **monitors;
1583         int i, j;
1584         if (!num_monitors)
1585                 return;
1586         monitors = malloc (sizeof (void*) * num_monitors);
1587         for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
1588                 MonitorDesc *mdesc = monitor_hash [i];
1589                 while (mdesc) {
1590                         monitors [j++] = mdesc;
1591                         mdesc = mdesc->next;
1592                 }
1593         }
1594         qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
1595         fprintf (outfile, "\nMonitor lock summary\n");
1596         for (i = 0; i < num_monitors; ++i) {
1597                 MonitorDesc *mdesc = monitors [i];
1598                 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
1599                 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
1600                         mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
1601                 dump_traces (&mdesc->traces, "contentions");
1602         }
1603         fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
1604         fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
1605         fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
1606 }
1607
1608 static void
1609 dump_gcs (void)
1610 {
1611         int i;
1612         fprintf (outfile, "\nGC summary\n");
1613         fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
1614         fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
1615         fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
1616         for (i = 0; i < 3; ++i) {
1617                 if (!gc_info [i].count)
1618                         continue;
1619                 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
1620                         i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
1621                         gc_info [i].total_time / gc_info [i].count / 1000);
1622         }
1623         for (i = 0; i < 3; ++i) {
1624                 if (!handle_info [i].max_live)
1625                         continue;
1626                 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
1627                         get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
1628                 dump_traces (&handle_info [i].traces, "created");
1629         }
1630 }
1631
1632 static void
1633 dump_allocations (void)
1634 {
1635         int i, c;
1636         intptr_t allocs = 0;
1637         uint64_t size = 0;
1638         int header_done = 0;
1639         ClassDesc **classes = malloc (num_classes * sizeof (void*));
1640         ClassDesc *cd;
1641         c = 0;
1642         for (i = 0; i < HASH_SIZE; ++i) {
1643                 cd = class_hash [i];
1644                 while (cd) {
1645                         classes [c++] = cd;
1646                         cd = cd->next;
1647                 }
1648         }
1649         qsort (classes, num_classes, sizeof (void*), compare_class);
1650         for (i = 0; i < num_classes; ++i) {
1651                 cd = classes [i];
1652                 if (!cd->allocs)
1653                         continue;
1654                 allocs += cd->allocs;
1655                 size += cd->alloc_size;
1656                 if (!header_done++) {
1657                         fprintf (outfile, "\nAllocation summary\n");
1658                         fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
1659                 }
1660                 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
1661                 dump_traces (&cd->traces, "bytes");
1662         }
1663         if (allocs)
1664                 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
1665 }
1666
1667 enum {
1668         METHOD_SORT_TOTAL,
1669         METHOD_SORT_SELF,
1670         METHOD_SORT_CALLS
1671 };
1672
1673 static int method_sort_mode = METHOD_SORT_TOTAL;
1674
1675 static int
1676 compare_method (const void *a, const void *b)
1677 {
1678         MethodDesc *const*A = a;
1679         MethodDesc *const*B = b;
1680         uint64_t vala, valb;
1681         if (method_sort_mode == METHOD_SORT_SELF) {
1682                 vala = (*A)->self_time;
1683                 valb = (*B)->self_time;
1684         } else if (method_sort_mode == METHOD_SORT_CALLS) {
1685                 vala = (*A)->calls;
1686                 valb = (*B)->calls;
1687         } else {
1688                 vala = (*A)->total_time;
1689                 valb = (*B)->total_time;
1690         }
1691         if (vala == valb)
1692                 return 0;
1693         if (valb < vala)
1694                 return -1;
1695         return 1;
1696 }
1697
1698 static void
1699 dump_metadata (void)
1700 {
1701         fprintf (outfile, "\nMetadata summary\n");
1702         fprintf (outfile, "\tLoaded images: %d\n", num_images);
1703         if (verbose) {
1704                 ImageDesc *image;
1705                 int i;
1706                 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
1707                         image = image_hash [i];
1708                         while (image) {
1709                                 fprintf (outfile, "\t\t%s\n", image->filename);
1710                                 image = image->next;
1711                         }
1712                 }
1713         }
1714
1715 }
1716
1717 static void
1718 dump_methods (void)
1719 {
1720         int i, c;
1721         uint64_t calls = 0;
1722         int header_done = 0;
1723         MethodDesc **methods = malloc (num_methods * sizeof (void*));
1724         MethodDesc *cd;
1725         c = 0;
1726         for (i = 0; i < HASH_SIZE; ++i) {
1727                 cd = method_hash [i];
1728                 while (cd) {
1729                         cd->total_time = cd->self_time + cd->callee_time;
1730                         methods [c++] = cd;
1731                         cd = cd->next;
1732                 }
1733         }
1734         qsort (methods, num_methods, sizeof (void*), compare_method);
1735         for (i = 0; i < num_methods; ++i) {
1736                 uint64_t msecs;
1737                 uint64_t smsecs;
1738                 cd = methods [i];
1739                 if (!cd->calls)
1740                         continue;
1741                 calls += cd->calls;
1742                 msecs = cd->total_time / 1000000;
1743                 smsecs = (cd->total_time - cd->callee_time) / 1000000;
1744                 if (!msecs && !verbose)
1745                         continue;
1746                 if (!header_done++) {
1747                         fprintf (outfile, "\nMethod call summary\n");
1748                         fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
1749                 }
1750                 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
1751                 dump_traces (&cd->traces, "calls");
1752         }
1753         if (calls)
1754                 fprintf (outfile, "Total calls: %llu\n", calls);
1755 }
1756
1757 static int
1758 compare_heap_class (const void *a, const void *b)
1759 {
1760         HeapClassDesc *const*A = a;
1761         HeapClassDesc *const*B = b;
1762         uint64_t vala, valb;
1763         if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1764                 vala = (*A)->total_size;
1765                 valb = (*B)->total_size;
1766         } else {
1767                 vala = (*A)->count;
1768                 valb = (*B)->count;
1769         }
1770         if (valb == vala)
1771                 return 0;
1772         if (valb < vala)
1773                 return -1;
1774         return 1;
1775 }
1776
1777 static int
1778 compare_rev_class (const void *a, const void *b)
1779 {
1780         const HeapClassRevRef *A = a;
1781         const HeapClassRevRef *B = b;
1782         if (B->count == A->count)
1783                 return 0;
1784         if (B->count < A->count)
1785                 return -1;
1786         return 1;
1787 }
1788
1789 static void
1790 dump_rev_claases (HeapClassRevRef *revs, int count)
1791 {
1792         int j;
1793         if (!show_traces)
1794                 return;
1795         if (!count)
1796                 return;
1797         for (j = 0; j < count; ++j) {
1798                 HeapClassDesc *cd = revs [j].klass;
1799                 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
1800         }
1801 }
1802
1803 static void
1804 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
1805 {
1806         uint64_t size = 0;
1807         uint64_t count = 0;
1808         int ccount = 0;
1809         int i;
1810         HeapClassDesc *cd;
1811         HeapClassDesc **sorted;
1812         sorted = malloc (sizeof (void*) * hs->class_count);
1813         for (i = 0; i < hs->hash_size; ++i) {
1814                 cd = hs->class_hash [i];
1815                 if (!cd)
1816                         continue;
1817                 count += cd->count;
1818                 size += cd->total_size;
1819                 sorted [ccount++] = cd;
1820         }
1821         hs->sorted = sorted;
1822         qsort (sorted, ccount, sizeof (void*), compare_heap_class);
1823         fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d\n",
1824                 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount);
1825         if (!verbose && ccount > 30)
1826                 ccount = 30;
1827         fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
1828         for (i = 0; i < ccount; ++i) {
1829                 HeapClassRevRef *rev_sorted;
1830                 int j, k;
1831                 HeapClassDesc *ocd = NULL;
1832                 cd = sorted [i];
1833                 if (last_hs)
1834                         ocd = heap_class_lookup (last_hs, cd->klass);
1835                 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
1836                 if (ocd) {
1837                         int64_t bdiff = cd->total_size - ocd->total_size;
1838                         int64_t cdiff = cd->count - ocd->count;
1839                         fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
1840                 } else {
1841                         fprintf (outfile, "\n");
1842                 }
1843                 if (!collect_traces)
1844                         continue;
1845                 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
1846                 k = 0;
1847                 for (j = 0; j < cd->rev_hash_size; ++j) {
1848                         if (cd->rev_hash [j].klass)
1849                                 rev_sorted [k++] = cd->rev_hash [j];
1850                 }
1851                 assert (cd->rev_count == k);
1852                 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
1853                 dump_rev_claases (rev_sorted, cd->rev_count);
1854                 free (rev_sorted);
1855         }
1856         free (sorted);
1857 }
1858
1859 static int
1860 compare_heap_shots (const void *a, const void *b)
1861 {
1862         HeapShot *const*A = a;
1863         HeapShot *const*B = b;
1864         if ((*B)->timestamp == (*A)->timestamp)
1865                 return 0;
1866         if ((*B)->timestamp > (*A)->timestamp)
1867                 return -1;
1868         return 1;
1869 }
1870
1871 static void
1872 dump_heap_shots (void)
1873 {
1874         HeapShot **hs_sorted;
1875         HeapShot *hs;
1876         HeapShot *last_hs = NULL;
1877         int i;
1878         if (!heap_shots)
1879                 return;
1880         hs_sorted = malloc (num_heap_shots * sizeof (void*));
1881         fprintf (outfile, "\nHeap shot summary\n");
1882         i = 0;
1883         for (hs = heap_shots; hs; hs = hs->next)
1884                 hs_sorted [i++] = hs;
1885         qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
1886         for (i = 0; i < num_heap_shots; ++i) {
1887                 hs = hs_sorted [i];
1888                 heap_shot_summary (hs, i, last_hs);
1889                 last_hs = hs;
1890         }
1891 }
1892
1893 static void
1894 flush_context (ProfContext *ctx)
1895 {
1896         ThreadContext *thread;
1897         /* FIXME: sometimes there are leftovers: indagate */
1898         for (thread = ctx->threads; thread; thread = thread->next) {
1899                 while (thread->stack_id) {
1900                         if (debug)
1901                                 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
1902                         pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
1903                 }
1904         }
1905 }
1906
1907 static const char *reports = "header,gc,alloc,call,metadata,exception,monitor,thread,heapshot";
1908
1909 static const char*
1910 match_option (const char *p, const char *opt)
1911 {
1912         int len = strlen (opt);
1913         if (strncmp (p, opt, len) == 0) {
1914                 if (p [len] == ',')
1915                         len++;
1916                 return p + len;
1917         }
1918         return p;
1919 }
1920
1921 static int
1922 print_reports (ProfContext *ctx, const char *reps, int parse_only)
1923 {
1924         const char *opt;
1925         const char *p;
1926         for (p = reps; *p; p = opt) {
1927                 if ((opt = match_option (p, "header")) != p) {
1928                         if (!parse_only)
1929                                 dump_header (ctx);
1930                         continue;
1931                 }
1932                 if ((opt = match_option (p, "thread")) != p) {
1933                         if (!parse_only)
1934                                 dump_threads (ctx);
1935                         continue;
1936                 }
1937                 if ((opt = match_option (p, "gc")) != p) {
1938                         if (!parse_only)
1939                                 dump_gcs ();
1940                         continue;
1941                 }
1942                 if ((opt = match_option (p, "alloc")) != p) {
1943                         if (!parse_only)
1944                                 dump_allocations ();
1945                         continue;
1946                 }
1947                 if ((opt = match_option (p, "call")) != p) {
1948                         if (!parse_only)
1949                                 dump_methods ();
1950                         continue;
1951                 }
1952                 if ((opt = match_option (p, "metadata")) != p) {
1953                         if (!parse_only)
1954                                 dump_metadata ();
1955                         continue;
1956                 }
1957                 if ((opt = match_option (p, "exception")) != p) {
1958                         if (!parse_only)
1959                                 dump_exceptions ();
1960                         continue;
1961                 }
1962                 if ((opt = match_option (p, "monitor")) != p) {
1963                         if (!parse_only)
1964                                 dump_monitors ();
1965                         continue;
1966                 }
1967                 if ((opt = match_option (p, "heapshot")) != p) {
1968                         if (!parse_only)
1969                                 dump_heap_shots ();
1970                         continue;
1971                 }
1972                 return 0;
1973         }
1974         return 1;
1975 }
1976
1977 static int
1978 add_find_spec (const char *p)
1979 {
1980         if (p [0] == 'S' && p [1] == ':') {
1981                 char *vale;
1982                 find_size = strtoul (p + 2, &vale, 10);
1983                 return 1;
1984         } else if (p [0] == 'T' && p [1] == ':') {
1985                 find_name = p + 2;
1986                 return 1;
1987         }
1988         return 0;
1989 }
1990
1991 static void
1992 usage (void)
1993 {
1994         printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
1995         printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
1996         printf ("FILENAME can be '-' to read from standard input.\n");
1997         printf ("Options:\n");
1998         printf ("\t--help               display this help\n");
1999         printf ("\t--out=FILE           write to FILE instead of stdout\n");
2000         printf ("\t--traces             collect and show backtraces\n"); 
2001         printf ("\t--maxframes=NUM      limit backtraces to NUM entries\n");
2002         printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
2003         printf ("\t                     %s\n", reports);
2004         printf ("\t--method-sort=MODE   sort methods according to MODE: total, self, calls\n");
2005         printf ("\t--alloc-sort=MODE    sort allocations according to MODE: bytes, count\n");
2006         printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
2007         printf ("\t--find=FINDSPEC      find and track objects matching FINFSPEC, where FINDSPEC is:\n");
2008         printf ("\t                     S:minimum_size or T:partial_name\n");
2009         printf ("\t--thread=THREADID    consider just the data for thread THREADID\n");
2010         printf ("\t--time=FROM-TO       consider data FROM seconds from startup up to TO seconds\n");
2011         printf ("\t--verbose            increase verbosity level\n");
2012         printf ("\t--debug              display decoding debug info for mprof-report devs\n");
2013 }
2014
2015 int
2016 main (int argc, char *argv[])
2017 {
2018         ProfContext *ctx;
2019         int i;
2020         outfile = stdout;
2021         for (i = 1; i < argc; ++i) {
2022                 if (strcmp ("--debug", argv [i]) == 0) {
2023                         debug++;
2024                 } else if (strcmp ("--help", argv [i]) == 0) {
2025                         usage ();
2026                         return 0;
2027                 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
2028                         const char *val = argv [i] + 13;
2029                         if (strcmp (val, "bytes") == 0) {
2030                                 alloc_sort_mode = ALLOC_SORT_BYTES;
2031                         } else if (strcmp (val, "count") == 0) {
2032                                 alloc_sort_mode = ALLOC_SORT_COUNT;
2033                         } else {
2034                                 usage ();
2035                                 return 1;
2036                         }
2037                 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
2038                         const char *val = argv [i] + 14;
2039                         if (strcmp (val, "total") == 0) {
2040                                 method_sort_mode = METHOD_SORT_TOTAL;
2041                         } else if (strcmp (val, "self") == 0) {
2042                                 method_sort_mode = METHOD_SORT_SELF;
2043                         } else if (strcmp (val, "calls") == 0) {
2044                                 method_sort_mode = METHOD_SORT_CALLS;
2045                         } else {
2046                                 usage ();
2047                                 return 1;
2048                         }
2049                 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
2050                         const char *val = argv [i] + 10;
2051                         if (!print_reports (NULL, val, 1)) {
2052                                 usage ();
2053                                 return 1;
2054                         }
2055                         reports = val;
2056                 } else if (strncmp ("--out=", argv [i], 6) == 0) {
2057                         const char *val = argv [i] + 6;
2058                         outfile = fopen (val, "w");
2059                         if (!outfile) {
2060                                 printf ("Cannot open output file: %s\n", val);
2061                                 return 1;
2062                         }
2063                 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
2064                         const char *val = argv [i] + 12;
2065                         char *vale;
2066                         trace_max = strtoul (val, &vale, 10);
2067                 } else if (strncmp ("--find=", argv [i], 7) == 0) {
2068                         const char *val = argv [i] + 7;
2069                         if (!add_find_spec (val)) {
2070                                 usage ();
2071                                 return 1;
2072                         }
2073                 } else if (strncmp ("--track=", argv [i], 8) == 0) {
2074                         const char *val = argv [i] + 8;
2075                         char *vale;
2076                         while (*val) {
2077                                 uintptr_t tracked_obj;
2078                                 if (*val == ',') {
2079                                         val++;
2080                                         continue;
2081                                 }
2082                                 tracked_obj = strtoul (val, &vale, 0);
2083                                 found_object (tracked_obj);
2084                                 val = vale;
2085                         }
2086                 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
2087                         const char *val = argv [i] + 9;
2088                         char *vale;
2089                         thread_filter = strtoul (val, &vale, 0);
2090                 } else if (strncmp ("--time=", argv [i], 7) == 0) {
2091                         char *val = pstrdup (argv [i] + 7);
2092                         double from_secs, to_secs;
2093                         char *top = strchr (val, '-');
2094                         if (!top) {
2095                                 usage ();
2096                                 return 1;
2097                         }
2098                         *top++ = 0;
2099                         from_secs = atof (val);
2100                         to_secs = atof (top);
2101                         free (val);
2102                         if (from_secs > to_secs) {
2103                                 usage ();
2104                                 return 1;
2105                         }
2106                         time_from = from_secs * 1000000000;
2107                         time_to = to_secs * 1000000000;
2108                 } else if (strcmp ("--verbose", argv [i]) == 0) {
2109                         verbose++;
2110                 } else if (strcmp ("--traces", argv [i]) == 0) {
2111                         show_traces = 1;
2112                         collect_traces = 1;
2113                 } else {
2114                         break;
2115                 }
2116         }
2117         if (i >= argc) {
2118                 usage ();
2119                 return 2;
2120         }
2121         ctx = load_file (argv [i]);
2122         if (!ctx) {
2123                 printf ("Not a log profiler data file (or unsupported version).\n");
2124                 return 1;
2125         }
2126         while (decode_buffer (ctx));
2127         flush_context (ctx);
2128         if (num_tracked_objects)
2129                 return 0;
2130         print_reports (ctx, reports, 0);
2131         return 0;
2132 }
2133