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