NaCl runtime fixes
[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__) && !defined(__FreeBSD__)
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 #if defined(__native_client__) || defined(__native_client_codegen__)
31 volatile int __nacl_thread_suspension_needed = 0;
32 void __nacl_suspend_thread_if_needed() {}
33 #endif
34
35 static int debug = 0;
36 static int collect_traces = 0;
37 static int show_traces = 0;
38 static int trace_max = 6;
39 static int verbose = 0;
40 static uintptr_t *tracked_objects = 0;
41 static int num_tracked_objects = 0;
42 static uintptr_t thread_filter = 0;
43 static uint64_t find_size = 0;
44 static const char* find_name = NULL;
45 static uint64_t time_from = 0;
46 static uint64_t time_to = 0xffffffffffffffffULL;
47 static uint64_t startup_time = 0;
48 static FILE* outfile = NULL;
49
50 static int32_t
51 read_int16 (unsigned char *p)
52 {
53         int32_t value = *p++;
54         value |= (*p++) << 8;
55         return value;
56 }
57
58 static int32_t
59 read_int32 (unsigned char *p)
60 {
61         int32_t value = *p++;
62         value |= (*p++) << 8;
63         value |= (*p++) << 16;
64         value |= (uint32_t)(*p++) << 24;
65         return value;
66 }
67
68 static int64_t
69 read_int64 (unsigned char *p)
70 {
71         uint64_t value = *p++;
72         value |= (*p++) << 8;
73         value |= (*p++) << 16;
74         value |= (uint64_t)(*p++) << 24;
75         value |= (uint64_t)(*p++) << 32;
76         value |= (uint64_t)(*p++) << 40;
77         value |= (uint64_t)(*p++) << 48;
78         value |= (uint64_t)(*p++) << 54;
79         return value;
80 }
81
82 static char*
83 pstrdup (const char *s)
84 {
85         int len = strlen (s) + 1;
86         char *p = malloc (len);
87         memcpy (p, s, len);
88         return p;
89 }
90
91 static int num_images;
92 typedef struct _ImageDesc ImageDesc;
93 struct _ImageDesc {
94         ImageDesc *next;
95         intptr_t image;
96         char *filename;
97 };
98
99 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
100
101 static void
102 add_image (intptr_t image, char *name)
103 {
104         int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
105         ImageDesc *cd = malloc (sizeof (ImageDesc));
106         cd->image = image;
107         cd->filename = pstrdup (name);
108         cd->next = image_hash [slot];
109         image_hash [slot] = cd;
110         num_images++;
111 }
112
113 typedef struct _BackTrace BackTrace;
114 typedef struct {
115         uint64_t count;
116         BackTrace *bt;
117 } CallContext;
118
119 typedef struct {
120         int count;
121         int size;
122         CallContext *traces;
123 } TraceDesc;
124
125 typedef struct _ClassDesc ClassDesc;
126 struct _ClassDesc {
127         ClassDesc *next;
128         intptr_t klass;
129         char *name;
130         intptr_t allocs;
131         uint64_t alloc_size;
132         TraceDesc traces;
133 };
134
135 static ClassDesc* class_hash [HASH_SIZE] = {0};
136 static int num_classes = 0;
137
138 static ClassDesc*
139 add_class (intptr_t klass, const char *name)
140 {
141         int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
142         ClassDesc *cd;
143         cd = class_hash [slot];
144         while (cd && cd->klass != klass)
145                 cd = cd->next;
146         /* we resolved an unknown class (unless we had the code unloaded) */
147         if (cd) {
148                 /*printf ("resolved unknown: %s\n", name);*/
149                 free (cd->name);
150                 cd->name = pstrdup (name);
151                 return cd;
152         }
153         cd = calloc (sizeof (ClassDesc), 1);
154         cd->klass = klass;
155         cd->name = pstrdup (name);
156         cd->next = class_hash [slot];
157         cd->allocs = 0;
158         cd->alloc_size = 0;
159         cd->traces.count = 0;
160         cd->traces.size = 0;
161         cd->traces.traces = NULL;
162         class_hash [slot] = cd;
163         num_classes++;
164         return cd;
165 }
166
167 static ClassDesc *
168 lookup_class (intptr_t klass)
169 {
170         int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
171         ClassDesc *cd = class_hash [slot];
172         while (cd && cd->klass != klass)
173                 cd = cd->next;
174         if (!cd) {
175                 char buf [128];
176                 snprintf (buf, sizeof (buf), "unresolved class %p", (void*)klass);
177                 return add_class (klass, buf);
178         }
179         return cd;
180 }
181
182 typedef struct _MethodDesc MethodDesc;
183 struct _MethodDesc {
184         MethodDesc *next;
185         intptr_t method;
186         char *name;
187         intptr_t code;
188         int len;
189         int recurse_count;
190         int sample_hits;
191         uint64_t calls;
192         uint64_t total_time;
193         uint64_t callee_time;
194         uint64_t self_time;
195         TraceDesc traces;
196 };
197
198 static MethodDesc* method_hash [HASH_SIZE] = {0};
199 static int num_methods = 0;
200
201 static MethodDesc*
202 add_method (intptr_t method, const char *name, intptr_t code, int len)
203 {
204         int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
205         MethodDesc *cd;
206         cd = method_hash [slot];
207         while (cd && cd->method != method)
208                 cd = cd->next;
209         /* we resolved an unknown method (unless we had the code unloaded) */
210         if (cd) {
211                 cd->code = code;
212                 cd->len = len;
213                 /*printf ("resolved unknown: %s\n", name);*/
214                 free (cd->name);
215                 cd->name = pstrdup (name);
216                 return cd;
217         }
218         cd = calloc (sizeof (MethodDesc), 1);
219         cd->method = method;
220         cd->name = pstrdup (name);
221         cd->code = code;
222         cd->len = len;
223         cd->calls = 0;
224         cd->total_time = 0;
225         cd->traces.count = 0;
226         cd->traces.size = 0;
227         cd->traces.traces = NULL;
228         cd->next = method_hash [slot];
229         method_hash [slot] = cd;
230         num_methods++;
231         return cd;
232 }
233
234 static MethodDesc *
235 lookup_method (intptr_t method)
236 {
237         int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
238         MethodDesc *cd = method_hash [slot];
239         while (cd && cd->method != method)
240                 cd = cd->next;
241         if (!cd) {
242                 char buf [128];
243                 snprintf (buf, sizeof (buf), "unknown method %p", (void*)method);
244                 return add_method (method, buf, 0, 0);
245         }
246         return cd;
247 }
248
249 static int num_stat_samples = 0;
250 static int size_stat_samples = 0;
251 uintptr_t *stat_samples = NULL;
252 int *stat_sample_desc = NULL;
253
254 static void
255 add_stat_sample (int type, uintptr_t ip) {
256         if (num_stat_samples == size_stat_samples) {
257                 size_stat_samples *= 2;
258                 if (!size_stat_samples)
259                 size_stat_samples = 32;
260                 stat_samples = realloc (stat_samples, size_stat_samples * sizeof (uintptr_t));
261                 stat_sample_desc = realloc (stat_sample_desc, size_stat_samples * sizeof (int));
262         }
263         stat_samples [num_stat_samples] = ip;
264         stat_sample_desc [num_stat_samples++] = type;
265 }
266
267 static MethodDesc*
268 lookup_method_by_ip (uintptr_t ip)
269 {
270         int i;
271         MethodDesc* m;
272         /* dumb */
273         for (i = 0; i < HASH_SIZE; ++i) {
274                 m = method_hash [i];
275                 while (m) {
276                         //printf ("checking %p against %p-%p\n", (void*)ip, (void*)(m->code), (void*)(m->code + m->len));
277                         if (ip >= (uintptr_t)m->code && ip < (uintptr_t)m->code + m->len) {
278                                 return m;
279                         }
280                         m = m->next;
281                 }
282         }
283         return NULL;
284 }
285
286 static int
287 compare_method_samples (const void *a, const void *b)
288 {
289         MethodDesc *const*A = a;
290         MethodDesc *const*B = b;
291         if ((*A)->sample_hits == (*B)->sample_hits)
292                 return 0;
293         if ((*B)->sample_hits < (*A)->sample_hits)
294                 return -1;
295         return 1;
296 }
297
298 typedef struct _UnmanagedSymbol UnmanagedSymbol;
299 struct _UnmanagedSymbol {
300         UnmanagedSymbol *parent;
301         char *name;
302         int is_binary;
303         uintptr_t addr;
304         uintptr_t size;
305         uintptr_t sample_hits;
306 };
307
308 static UnmanagedSymbol **usymbols = NULL;
309 static int usymbols_size = 0;
310 static int usymbols_num = 0;
311
312 static int
313 compare_usymbol_addr (const void *a, const void *b)
314 {
315         UnmanagedSymbol *const*A = a;
316         UnmanagedSymbol *const*B = b;
317         if ((*B)->addr == (*A)->addr)
318                 return 0;
319         if ((*B)->addr > (*A)->addr)
320                 return -1;
321         return 1;
322 }
323
324 static int
325 compare_usymbol_samples (const void *a, const void *b)
326 {
327         UnmanagedSymbol *const*A = a;
328         UnmanagedSymbol *const*B = b;
329         if ((*B)->sample_hits == (*A)->sample_hits)
330                 return 0;
331         if ((*B)->sample_hits < (*A)->sample_hits)
332                 return -1;
333         return 1;
334 }
335
336 static void
337 add_unmanaged_symbol (uintptr_t addr, char *name, uintptr_t size)
338 {
339         UnmanagedSymbol *sym;
340         if (usymbols_num == usymbols_size) {
341                 int new_size = usymbols_size * 2;
342                 if (!new_size)
343                         new_size = 16;
344                 usymbols = realloc (usymbols, sizeof (void*) * new_size);
345                 usymbols_size = new_size;
346         }
347         sym = calloc (sizeof (UnmanagedSymbol), 1);
348         sym->addr = addr;
349         sym->name = name;
350         sym->size = size;
351         usymbols [usymbols_num++] = sym;
352 }
353
354 /* only valid after the symbols are sorted */
355 static UnmanagedSymbol*
356 lookup_unmanaged_symbol (uintptr_t addr)
357 {
358         int r = usymbols_num - 1;
359         int l = 0;
360         UnmanagedSymbol *sym;
361         int last_best = -1;
362         while (r >= l) {
363                 int m = (l + r) / 2;
364                 sym = usymbols [m];
365                 if (addr == sym->addr)
366                         return sym;
367                 if (addr < sym->addr) {
368                         r = m - 1;
369                 } else if (addr > sym->addr) {
370                         l = m + 1;
371                         last_best = m;
372                 }
373         }
374         if (last_best >= 0 && (addr - usymbols [last_best]->addr) < 4096)
375                 return usymbols [last_best];
376         return NULL;
377 }
378
379 /* we use the same structure for binaries */
380 static UnmanagedSymbol **ubinaries = NULL;
381 static int ubinaries_size = 0;
382 static int ubinaries_num = 0;
383
384 static void
385 add_unmanaged_binary (uintptr_t addr, char *name, uintptr_t size)
386 {
387         UnmanagedSymbol *sym;
388         if (ubinaries_num == ubinaries_size) {
389                 int new_size = ubinaries_size * 2;
390                 if (!new_size)
391                         new_size = 16;
392                 ubinaries = realloc (ubinaries, sizeof (void*) * new_size);
393                 ubinaries_size = new_size;
394         }
395         sym = calloc (sizeof (UnmanagedSymbol), 1);
396         sym->addr = addr;
397         sym->name = name;
398         sym->size = size;
399         sym->is_binary = 1;
400         ubinaries [ubinaries_num++] = sym;
401 }
402
403 static UnmanagedSymbol*
404 lookup_unmanaged_binary (uintptr_t addr)
405 {
406         int i;
407         for (i = 0; i < ubinaries_num; ++i) {
408                 UnmanagedSymbol *ubin = ubinaries [i];
409                 if (addr >= ubin->addr && addr < ubin->addr + ubin->size) {
410                         return ubin;
411                 }
412         }
413         return NULL;
414 }
415
416 static const char*
417 sample_type_name (int type)
418 {
419         switch (type) {
420         case SAMPLE_CYCLES: return "cycles";
421         case SAMPLE_INSTRUCTIONS: return "instructions retired";
422         case SAMPLE_CACHE_MISSES: return "cache misses";
423         case SAMPLE_CACHE_REFS: return "cache references";
424         case SAMPLE_BRANCHES: return "executed branches";
425         case SAMPLE_BRANCH_MISSES: return "unpredicted branches";
426         }
427         return "unknown";
428 }
429
430 static void
431 set_usym_parent (UnmanagedSymbol** cachedus, int count)
432 {
433         int i;
434         for (i = 0; i < count; ++i) {
435                 UnmanagedSymbol *ubin = lookup_unmanaged_binary (cachedus [i]->addr);
436                 if (ubin == cachedus [i])
437                         continue;
438                 cachedus [i]->parent = ubin;
439         }
440 }
441
442 static void
443 print_usym (UnmanagedSymbol* um)
444 {
445         if (um->parent)
446                 fprintf (outfile, "\t%6d %6.2f %-36s in %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name, um->parent->name);
447         else
448                 fprintf (outfile, "\t%6d %6.2f %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name);
449 }
450
451 static int
452 sym_percent (uintptr_t sample_hits)
453 {
454         double pc;
455         if (verbose)
456                 return 1;
457         pc = sample_hits*100.0/num_stat_samples;
458         return pc >= 0.1;
459 }
460
461 static void
462 dump_samples (void)
463 {
464         int i, u;
465         int count = 0, msize = 0;
466         int unmanaged_hits = 0;
467         int unresolved_hits = 0;
468         MethodDesc** cachedm = NULL;
469         int ucount = 0, usize = 0;
470         UnmanagedSymbol** cachedus = NULL;
471         if (!num_stat_samples)
472                 return;
473         qsort (usymbols, usymbols_num, sizeof (UnmanagedSymbol*), compare_usymbol_addr);
474         for (i = 0; i < num_stat_samples; ++i) {
475                 MethodDesc *m = lookup_method_by_ip (stat_samples [i]);
476                 if (m) {
477                         if (!m->sample_hits) {
478                                 if (count == msize) {
479                                         msize *= 2;
480                                         if (!msize)
481                                                 msize = 4;
482                                         cachedm = realloc (cachedm, sizeof (void*) * msize);
483                                 }
484                                 cachedm [count++] = m;
485                         }
486                         m->sample_hits++;
487                 } else {
488                         UnmanagedSymbol *usym = lookup_unmanaged_symbol (stat_samples [i]);
489                         if (!usym) {
490                                 unresolved_hits++;
491                                 //printf ("unmanaged hit at %p\n", (void*)stat_samples [i]);
492                                 usym = lookup_unmanaged_binary (stat_samples [i]);
493                         }
494                         if (usym) {
495                                 if (!usym->sample_hits) {
496                                         if (ucount == usize) {
497                                                 usize *= 2;
498                                                 if (!usize)
499                                                         usize = 4;
500                                                 cachedus = realloc (cachedus, sizeof (void*) * usize);
501                                         }
502                                         cachedus [ucount++] = usym;
503                                 }
504                                 usym->sample_hits++;
505                         }
506                         unmanaged_hits++;
507                 }
508         }
509         qsort (cachedm, count, sizeof (MethodDesc*), compare_method_samples);
510         qsort (cachedus, ucount, sizeof (UnmanagedSymbol*), compare_usymbol_samples);
511         set_usym_parent (cachedus, ucount);
512         fprintf (outfile, "\nStatistical samples summary\n");
513         fprintf (outfile, "\tSample type: %s\n", sample_type_name (stat_sample_desc [0]));
514         fprintf (outfile, "\tUnmanaged hits:  %6d (%4.1f%%)\n", unmanaged_hits, (100.0*unmanaged_hits)/num_stat_samples);
515         fprintf (outfile, "\tManaged hits:    %6d (%4.1f%%)\n", num_stat_samples - unmanaged_hits, (100.0*(num_stat_samples-unmanaged_hits))/num_stat_samples);
516         fprintf (outfile, "\tUnresolved hits: %6d (%4.1f%%)\n", unresolved_hits, (100.0*unresolved_hits)/num_stat_samples);
517         fprintf (outfile, "\t%6s %6s %s\n", "Hits", "%", "Method name");
518         i = 0;
519         u = 0;
520         while (i < count || u < ucount) {
521                 if (i < count) {
522                         MethodDesc *m = cachedm [i];
523                         if (u < ucount) {
524                                 UnmanagedSymbol *um = cachedus [u];
525                                 if (um->sample_hits > m->sample_hits) {
526                                         if (!sym_percent (um->sample_hits))
527                                                 break;
528                                         print_usym (um);
529                                         u++;
530                                         continue;
531                                 }
532                         }
533                         if (!sym_percent (m->sample_hits))
534                                 break;
535                         fprintf (outfile, "\t%6d %6.2f %s\n", m->sample_hits, m->sample_hits*100.0/num_stat_samples, m->name);
536                         i++;
537                         continue;
538                 }
539                 if (u < ucount) {
540                         UnmanagedSymbol *um = cachedus [u];
541                         if (!sym_percent (um->sample_hits))
542                                 break;
543                         print_usym (um);
544                         u++;
545                         continue;
546                 }
547         }
548 }
549
550 typedef struct _HeapClassDesc HeapClassDesc;
551 typedef struct {
552         HeapClassDesc *klass;
553         uint64_t count;
554 } HeapClassRevRef;
555
556 struct _HeapClassDesc {
557         ClassDesc *klass;
558         int64_t count;
559         int64_t total_size;
560         HeapClassRevRef *rev_hash;
561         int rev_hash_size;
562         int rev_count;
563         uintptr_t pinned_references;
564         uintptr_t root_references;
565 };
566
567 static int
568 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
569 {
570         uintptr_t i;
571         uintptr_t start_pos;
572         start_pos = (hklass->klass->klass >> 2) % size;
573         assert (start_pos < size);
574         i = start_pos;
575         do {
576                 if (rev_hash [i].klass == hklass) {
577                         rev_hash [i].count += value;
578                         return 0;
579                 } else if (!rev_hash [i].klass) {
580                         rev_hash [i].klass = hklass;
581                         rev_hash [i].count += value;
582                         start_pos = 0;
583                         for (i = 0; i < size; ++i)
584                                 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
585                                         start_pos ++;
586                         assert (start_pos == 1);
587                         return 1;
588                 }
589                 /* wrap around */
590                 if (++i == size)
591                         i = 0;
592         } while (i != start_pos);
593         /* should not happen */
594         printf ("failed revref store\n");
595         return 0;
596 }
597
598 static void
599 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
600 {
601         uintptr_t i;
602         if (to->rev_count * 2 >= to->rev_hash_size) {
603                 HeapClassRevRef *n;
604                 uintptr_t old_size = to->rev_hash_size;
605                 to->rev_hash_size *= 2;
606                 if (to->rev_hash_size == 0)
607                         to->rev_hash_size = 4;
608                 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
609                 for (i = 0; i < old_size; ++i) {
610                         if (to->rev_hash [i].klass)
611                                 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
612                 }
613                 if (to->rev_hash)
614                         free (to->rev_hash);
615                 to->rev_hash = n;
616         }
617         to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
618 }
619
620 typedef struct {
621         uintptr_t objaddr;
622         HeapClassDesc *hklass;
623         uintptr_t num_refs;
624         uintptr_t refs [0];
625 } HeapObjectDesc;
626
627 typedef struct _HeapShot HeapShot;
628 struct _HeapShot {
629         HeapShot *next;
630         uint64_t timestamp;
631         int class_count;
632         int hash_size;
633         HeapClassDesc **class_hash;
634         HeapClassDesc **sorted;
635         HeapObjectDesc **objects_hash;
636         uintptr_t objects_count;
637         uintptr_t objects_hash_size;
638         uintptr_t num_roots;
639         uintptr_t *roots;
640         uintptr_t *roots_extra;
641         int *roots_types;
642 };
643
644 static HeapShot *heap_shots = NULL;
645 static int num_heap_shots = 0;
646
647 static HeapShot*
648 new_heap_shot (uint64_t timestamp)
649 {
650         HeapShot *hs = calloc (sizeof (HeapShot), 1);
651         hs->hash_size = 4;
652         hs->class_hash = calloc (sizeof (void*), hs->hash_size);
653         hs->timestamp = timestamp;
654         num_heap_shots++;
655         hs->next = heap_shots;
656         heap_shots = hs;
657         return hs;
658 }
659
660 static HeapClassDesc*
661 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
662 {
663         int i;
664         unsigned int start_pos;
665         start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
666         i = start_pos;
667         do {
668                 HeapClassDesc* cd = hs->class_hash [i];
669                 if (!cd)
670                         return NULL;
671                 if (cd->klass == klass)
672                         return cd;
673                 /* wrap around */
674                 if (++i == hs->hash_size)
675                         i = 0;
676         } while (i != start_pos);
677         return NULL;
678 }
679
680 static int
681 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
682 {
683         uintptr_t i;
684         uintptr_t start_pos;
685         start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
686         i = start_pos;
687         do {
688                 if (hash [i] && hash [i]->klass == klass) {
689                         hash [i]->total_size += size;
690                         hash [i]->count += count;
691                         *retv = hash [i];
692                         return 0;
693                 } else if (!hash [i]) {
694                         if (*retv) {
695                                 hash [i] = *retv;
696                                 return 1;
697                         }
698                         hash [i] = calloc (sizeof (HeapClassDesc), 1);
699                         hash [i]->klass = klass;
700                         hash [i]->total_size += size;
701                         hash [i]->count += count;
702                         *retv = hash [i];
703                         return 1;
704                 }
705                 /* wrap around */
706                 if (++i == hsize)
707                         i = 0;
708         } while (i != start_pos);
709         /* should not happen */
710         printf ("failed heap class store\n");
711         return 0;
712 }
713
714 static HeapClassDesc*
715 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
716 {
717         HeapClassDesc *res;
718         int i;
719         if (hs->class_count * 2 >= hs->hash_size) {
720                 HeapClassDesc **n;
721                 int old_size = hs->hash_size;
722                 hs->hash_size *= 2;
723                 if (hs->hash_size == 0)
724                         hs->hash_size = 4;
725                 n = calloc (sizeof (void*) * hs->hash_size, 1);
726                 for (i = 0; i < old_size; ++i) {
727                         res = hs->class_hash [i];
728                         if (hs->class_hash [i])
729                                 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
730                 }
731                 if (hs->class_hash)
732                         free (hs->class_hash);
733                 hs->class_hash = n;
734         }
735         res = NULL;
736         hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
737         //if (res->count == 1)
738         //      printf ("added heap class: %s\n", res->klass->name);
739         return res;
740 }
741
742 static HeapObjectDesc*
743 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
744 {
745         HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
746         ho->objaddr = objaddr;
747         ho->hklass = hklass;
748         ho->num_refs = num_refs;
749         return ho;
750 }
751
752 static uintptr_t
753 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
754 {
755         uintptr_t i;
756         uintptr_t start_pos;
757         HeapObjectDesc **hash = hs->objects_hash;
758         start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
759         i = start_pos;
760         do {
761                 if (hash [i] && hash [i]->objaddr == objaddr) {
762                         return i;
763                 } else if (!hash [i]) {
764                         break; /* fail */
765                 }
766                 /* wrap around */
767                 if (++i == hs->objects_hash_size)
768                         i = 0;
769         } while (i != start_pos);
770         /* should not happen */
771         //printf ("failed heap obj slot\n");
772         return -1;
773 }
774
775 static HeapObjectDesc*
776 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
777 {
778         HeapObjectDesc **hash = hs->objects_hash;
779         uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
780         if (i >= 0) {
781                 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
782                 *ref_offset = hash [i]->num_refs;
783                 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
784                 free (hash [i]);
785                 hash [i] = ho;
786                 return ho;
787         }
788         /* should not happen */
789         printf ("failed heap obj update\n");
790         return NULL;
791         
792 }
793
794 static uintptr_t
795 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
796 {
797         uintptr_t i;
798         uintptr_t start_pos;
799         start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
800         i = start_pos;
801         do {
802                 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
803                         printf ("duplicate object!\n");
804                         return 0;
805                 } else if (!hash [i]) {
806                         hash [i] = obj;
807                         return 1;
808                 }
809                 /* wrap around */
810                 if (++i == hsize)
811                         i = 0;
812         } while (i != start_pos);
813         /* should not happen */
814         printf ("failed heap obj store\n");
815         return 0;
816 }
817
818 static void
819 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
820 {
821         uintptr_t i;
822         if (hs->objects_count * 2 >= hs->objects_hash_size) {
823                 HeapObjectDesc **n;
824                 uintptr_t old_size = hs->objects_hash_size;
825                 hs->objects_hash_size *= 2;
826                 if (hs->objects_hash_size == 0)
827                         hs->objects_hash_size = 4;
828                 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
829                 for (i = 0; i < old_size; ++i) {
830                         if (hs->objects_hash [i])
831                                 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
832                 }
833                 if (hs->objects_hash)
834                         free (hs->objects_hash);
835                 hs->objects_hash = n;
836         }
837         hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
838 }
839
840 static void
841 heap_shot_resolve_reverse_refs (HeapShot *hs)
842 {
843         uintptr_t i;
844         for (i = 0; i < hs->objects_hash_size; ++i) {
845                 uintptr_t r;
846                 HeapObjectDesc *ho = hs->objects_hash [i];
847                 if (!ho)
848                         continue;
849                 for (r = 0; r < ho->num_refs; ++r) {
850                         uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
851                         add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
852                 }
853         }
854 }
855
856 #define MARK_GRAY 1
857 #define MARK_BLACK 2
858
859 static void
860 heap_shot_mark_objects (HeapShot *hs)
861 {
862         uintptr_t i, oi, r;
863         unsigned char *marks;
864         HeapObjectDesc *obj, *ref;
865         int marked_some;
866         uintptr_t num_marked = 0, num_unmarked;
867         for (i = 0; i < hs->num_roots; ++i) {
868                 HeapClassDesc *cd;
869                 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
870                 if (oi == -1) {
871                         continue;
872                 }
873                 obj = hs->objects_hash [oi];
874                 cd = obj->hklass;
875                 if (hs->roots_types [i] & MONO_PROFILE_GC_ROOT_PINNING)
876                         cd->pinned_references++;
877                 cd->root_references++;
878         }
879         if (!debug)
880                 return;
881         /* consistency checks: it seems not all the objects are walked in the heap in some cases */
882         marks = calloc (hs->objects_hash_size, 1);
883         if (!marks)
884                 return;
885         for (i = 0; i < hs->num_roots; ++i) {
886                 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
887                 if (oi == -1) {
888                         fprintf (outfile, "root type 0x%x for obj %p (%s) not found in heap\n", hs->roots_types [i], (void*)hs->roots [i], lookup_class (hs->roots_extra [i])->name);
889                         continue;
890                 }
891                 obj = hs->objects_hash [oi];
892                 if (!marks [oi]) {
893                         marks [oi] = obj->num_refs? MARK_GRAY: MARK_BLACK;
894                         num_marked++;
895                 }
896         }
897         marked_some = 1;
898         while (marked_some) {
899                 marked_some = 0;
900                 for (i = 0; i < hs->objects_hash_size; ++i) {
901                         if (marks [i] != MARK_GRAY)
902                                 continue;
903                         marks [i] = MARK_BLACK;
904                         obj = hs->objects_hash [i];
905                         for (r = 0; r < obj->num_refs; ++r) {
906                                 oi = heap_shot_find_obj_slot (hs, obj->refs [r]);
907                                 if (oi == -1) {
908                                         fprintf (outfile, "referenced obj %p not found in heap\n", (void*)obj->refs [r]);
909                                         continue;
910                                 }
911                                 ref = hs->objects_hash [oi];
912                                 if (!marks [oi]) {
913                                         marks [oi] = ref->num_refs? MARK_GRAY: MARK_BLACK;
914                                 }
915                         }
916                         marked_some++;
917                 }
918         }
919
920         num_unmarked = 0;
921         for (i = 0; i < hs->objects_hash_size; ++i) {
922                 if (hs->objects_hash [i] && !marks [i]) {
923                         num_unmarked++;
924                         fprintf (outfile, "object %p (%s) unmarked\n", (void*)hs->objects_hash [i], hs->objects_hash [i]->hklass->klass->name);
925                 }
926         }
927         fprintf (outfile, "Total unmarked: %d/%d\n", num_unmarked, hs->objects_count);
928         free (marks);
929 }
930
931 static void
932 heap_shot_free_objects (HeapShot *hs)
933 {
934         uintptr_t i;
935         for (i = 0; i < hs->objects_hash_size; ++i) {
936                 HeapObjectDesc *ho = hs->objects_hash [i];
937                 if (ho)
938                         free (ho);
939         }
940         if (hs->objects_hash)
941                 free (hs->objects_hash);
942         hs->objects_hash = NULL;
943         hs->objects_hash_size = 0;
944         hs->objects_count = 0;
945 }
946
947
948 struct _BackTrace {
949         BackTrace *next;
950         unsigned int hash;
951         int count;
952         int id;
953         MethodDesc *methods [1];
954 };
955
956 static BackTrace *backtrace_hash [HASH_SIZE];
957 static BackTrace **backtraces = NULL;
958 static int num_backtraces = 0;
959 static int next_backtrace = 0;
960
961 static int
962 hash_backtrace (int count, MethodDesc **methods)
963 {
964         int hash = count;
965         int i;
966         for (i = 0; i < count; ++i) {
967                 hash = (hash << 5) - hash + methods [i]->method;
968         }
969         return hash;
970 }
971
972 static int
973 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
974 {
975         int i;
976         if (bt->count != count)
977                 return 0;
978         for (i = 0; i < count; ++i)
979                 if (methods [i] != bt->methods [i])
980                         return 0;
981         return 1;
982 }
983
984 static BackTrace*
985 add_backtrace (int count, MethodDesc **methods)
986 {
987         int hash = hash_backtrace (count, methods);
988         int slot = (hash & 0xffff) % HASH_SIZE;
989         BackTrace *bt = backtrace_hash [slot];
990         while (bt) {
991                 if (bt->hash == hash && compare_backtrace (bt, count, methods))
992                         return bt;
993                 bt = bt->next;
994         }
995         bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
996         bt->next = backtrace_hash [slot];
997         backtrace_hash [slot] = bt;
998         if (next_backtrace == num_backtraces) {
999                 num_backtraces *= 2;
1000                 if (!num_backtraces)
1001                         num_backtraces = 16;
1002                 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
1003         }
1004         bt->id = next_backtrace++;
1005         backtraces [bt->id] = bt;
1006         bt->count = count;
1007         bt->hash = hash;
1008         for (slot = 0; slot < count; ++slot)
1009                 bt->methods [slot] = methods [slot];
1010
1011         return bt;
1012 }
1013
1014 typedef struct _MonitorDesc MonitorDesc;
1015 typedef struct _ThreadContext ThreadContext;
1016
1017 typedef struct {
1018         FILE *file;
1019 #if defined (HAVE_SYS_ZLIB)
1020         gzFile *gzfile;
1021 #endif
1022         unsigned char *buf;
1023         int size;
1024         int data_version;
1025         int version_major;
1026         int version_minor;
1027         int timer_overhead;
1028         int pid;
1029         int port;
1030         uint64_t startup_time;
1031         ThreadContext *threads;
1032         ThreadContext *current;
1033 } ProfContext;
1034
1035 struct _ThreadContext {
1036         ThreadContext *next;
1037         intptr_t thread_id;
1038         char *name;
1039         /* emulated stack */
1040         MethodDesc **stack;
1041         uint64_t *time_stack;
1042         uint64_t *callee_time_stack;
1043         uint64_t last_time;
1044         uint64_t contention_start;
1045         MonitorDesc *monitor;
1046         int stack_size;
1047         int stack_id;
1048         HeapShot *current_heap_shot;
1049         uintptr_t num_roots;
1050         uintptr_t size_roots;
1051         uintptr_t *roots;
1052         uintptr_t *roots_extra;
1053         int *roots_types;
1054         uint64_t gc_start_times [3];
1055 };
1056
1057 static void
1058 ensure_buffer (ProfContext *ctx, int size)
1059 {
1060         if (ctx->size < size) {
1061                 ctx->buf = realloc (ctx->buf, size);
1062                 ctx->size = size;
1063         }
1064 }
1065
1066 static int
1067 load_data (ProfContext *ctx, int size)
1068 {
1069         ensure_buffer (ctx, size);
1070 #if defined (HAVE_SYS_ZLIB)
1071         if (ctx->gzfile) {
1072                 int r = gzread (ctx->gzfile, ctx->buf, size);
1073                 if (r == 0)
1074                         return size == 0? 1: 0;
1075                 return r == size;
1076         } else 
1077 #endif
1078         {
1079                 int r = fread (ctx->buf, size, 1, ctx->file);
1080                 if (r == 0)
1081                         return size == 0? 1: 0;
1082                 return r;
1083         }
1084 }
1085
1086 static ThreadContext*
1087 get_thread (ProfContext *ctx, intptr_t thread_id)
1088 {
1089         ThreadContext *thread;
1090         if (ctx->current && ctx->current->thread_id == thread_id)
1091                 return ctx->current;
1092         thread = ctx->threads;
1093         while (thread) {
1094                 if (thread->thread_id == thread_id) {
1095                         return thread;
1096                 }
1097                 thread = thread->next;
1098         }
1099         thread = calloc (sizeof (ThreadContext), 1);
1100         thread->next = ctx->threads;
1101         ctx->threads = thread;
1102         thread->thread_id = thread_id;
1103         thread->last_time = 0;
1104         thread->stack_id = 0;
1105         thread->stack_size = 32;
1106         thread->stack = malloc (thread->stack_size * sizeof (void*));
1107         thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1108         thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1109         return thread;
1110 }
1111
1112 static ThreadContext*
1113 load_thread (ProfContext *ctx, intptr_t thread_id)
1114 {
1115         ThreadContext *thread = get_thread (ctx, thread_id);
1116         ctx->current = thread;
1117         return thread;
1118 }
1119
1120 static void
1121 ensure_thread_stack (ThreadContext *thread)
1122 {
1123         if (thread->stack_id == thread->stack_size) {
1124                 thread->stack_size *= 2;
1125                 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
1126                 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
1127                 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
1128         }
1129 }
1130
1131 static int
1132 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
1133 {
1134         int i;
1135         unsigned int start_pos;
1136         start_pos = bt->hash % size;
1137         i = start_pos;
1138         do {
1139                 if (traces [i].bt == bt) {
1140                         traces [i].count += value;
1141                         return 0;
1142                 } else if (!traces [i].bt) {
1143                         traces [i].bt = bt;
1144                         traces [i].count += value;
1145                         return 1;
1146                 }
1147                 /* wrap around */
1148                 if (++i == size)
1149                         i = 0;
1150         } while (i != start_pos);
1151         /* should not happen */
1152         printf ("failed trace store\n");
1153         return 0;
1154 }
1155
1156 static void
1157 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
1158 {
1159         int i;
1160         if (!collect_traces)
1161                 return;
1162         if (trace->count * 2 >= trace->size) {
1163                 CallContext *n;
1164                 int old_size = trace->size;
1165                 trace->size *= 2;
1166                 if (trace->size == 0)
1167                         trace->size = 4;
1168                 n = calloc (sizeof (CallContext) * trace->size, 1);
1169                 for (i = 0; i < old_size; ++i) {
1170                         if (trace->traces [i].bt)
1171                                 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
1172                 }
1173                 if (trace->traces)
1174                         free (trace->traces);
1175                 trace->traces = n;
1176         }
1177         trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
1178 }
1179
1180 static BackTrace*
1181 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
1182 {
1183         BackTrace *bt;
1184         int count = thread->stack_id;
1185         if (!collect_traces)
1186                 return NULL;
1187         if (count > trace_max)
1188                 count = trace_max;
1189         bt = add_backtrace (count, thread->stack + thread->stack_id - count);
1190         add_trace_bt (bt, trace, value);
1191         return bt;
1192 }
1193
1194 static BackTrace*
1195 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
1196 {
1197         BackTrace *bt;
1198         if (!collect_traces)
1199                 return NULL;
1200         if (count > trace_max)
1201                 count = trace_max;
1202         bt = add_backtrace (count, methods);
1203         add_trace_bt (bt, trace, value);
1204         return bt;
1205 }
1206
1207 static void
1208 thread_add_root (ThreadContext *ctx, uintptr_t obj, int root_type, uintptr_t extra_info)
1209 {
1210         if (ctx->num_roots == ctx->size_roots) {
1211                 int new_size = ctx->size_roots * 2;
1212                 if (!new_size)
1213                         new_size = 4;
1214                 ctx->roots = realloc (ctx->roots, new_size * sizeof (uintptr_t));
1215                 ctx->roots_extra = realloc (ctx->roots_extra, new_size * sizeof (uintptr_t));
1216                 ctx->roots_types = realloc (ctx->roots_types, new_size * sizeof (int));
1217                 ctx->size_roots = new_size;
1218         }
1219         ctx->roots_types [ctx->num_roots] = root_type;
1220         ctx->roots_extra [ctx->num_roots] = extra_info;
1221         ctx->roots [ctx->num_roots++] = obj;
1222 }
1223
1224 static int
1225 compare_callc (const void *a, const void *b)
1226 {
1227         const CallContext *A = a;
1228         const CallContext *B = b;
1229         if (B->count == A->count)
1230                 return 0;
1231         if (B->count < A->count)
1232                 return -1;
1233         return 1;
1234 }
1235
1236 static void
1237 sort_context_array (TraceDesc* traces)
1238 {
1239         int i, j;
1240         for (i = 0, j = 0; i < traces->size; ++i) {
1241                 if (traces->traces [i].bt) {
1242                         traces->traces [j].bt = traces->traces [i].bt;
1243                         traces->traces [j].count = traces->traces [i].count;
1244                         j++;
1245                 }
1246         }
1247         qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
1248 }
1249
1250 static void
1251 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1252 {
1253         ensure_thread_stack (thread);
1254         thread->time_stack [thread->stack_id] = timestamp;
1255         thread->callee_time_stack [thread->stack_id] = 0;
1256         thread->stack [thread->stack_id++] = method;
1257         method->recurse_count++;
1258 }
1259
1260 static void
1261 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1262 {
1263         method->recurse_count--;
1264         if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
1265                 uint64_t tdiff;
1266                 thread->stack_id--;
1267                 method->calls++;
1268                 if (timestamp < thread->time_stack [thread->stack_id])
1269                         fprintf (outfile, "time went backwards for %s\n", method->name);
1270                 tdiff = timestamp - thread->time_stack [thread->stack_id];
1271                 if (thread->callee_time_stack [thread->stack_id] > tdiff)
1272                         fprintf (outfile, "callee time bigger for %s\n", method->name);
1273                 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
1274                 method->callee_time += thread->callee_time_stack [thread->stack_id];
1275                 if (thread->stack_id)
1276                         thread->callee_time_stack [thread->stack_id - 1] += tdiff;
1277                 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
1278         } else {
1279                 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
1280         }
1281 }
1282
1283 typedef struct {
1284         uint64_t total_time;
1285         uint64_t max_time;
1286         int count;
1287 } GCDesc;
1288 static GCDesc gc_info [3];
1289 static uint64_t max_heap_size;
1290 static uint64_t gc_object_moves;
1291 static int gc_resizes;
1292 typedef struct {
1293         uint64_t created;
1294         uint64_t destroyed;
1295         uint64_t live;
1296         uint64_t max_live;
1297         TraceDesc traces;
1298 } HandleInfo;
1299 static HandleInfo handle_info [4];
1300
1301 static const char*
1302 gc_event_name (int ev)
1303 {
1304         switch (ev) {
1305         case MONO_GC_EVENT_START: return "start";
1306         case MONO_GC_EVENT_MARK_START: return "mark start";
1307         case MONO_GC_EVENT_MARK_END: return "mark end";
1308         case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
1309         case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
1310         case MONO_GC_EVENT_END: return "end";
1311         case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
1312         case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
1313         case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
1314         case MONO_GC_EVENT_POST_START_WORLD: return "post start";
1315         default:
1316                 return "unknown";
1317         }
1318 }
1319
1320 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
1321 static uint64_t throw_count = 0;
1322 static TraceDesc exc_traces;
1323
1324 static const char*
1325 clause_name (int type)
1326 {
1327         switch (type) {
1328         case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
1329         case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
1330         case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
1331         case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
1332         default: return "invalid";
1333         }
1334 }
1335
1336 static uint64_t monitor_contention;
1337 static uint64_t monitor_failed;
1338 static uint64_t monitor_acquired;
1339
1340 struct _MonitorDesc {
1341         MonitorDesc *next;
1342         uintptr_t objid;
1343         uintptr_t contentions;
1344         uint64_t wait_time;
1345         uint64_t max_wait_time;
1346         TraceDesc traces;
1347 };
1348
1349 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
1350 static int num_monitors = 0;
1351
1352 static MonitorDesc*
1353 lookup_monitor (uintptr_t objid)
1354 {
1355         int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
1356         MonitorDesc *cd = monitor_hash [slot];
1357         while (cd && cd->objid != objid)
1358                 cd = cd->next;
1359         if (!cd) {
1360                 cd = calloc (sizeof (MonitorDesc), 1);
1361                 cd->objid = objid;
1362                 cd->next = monitor_hash [slot];
1363                 monitor_hash [slot] = cd;
1364                 num_monitors++;
1365         }
1366         return cd;
1367 }
1368
1369 static const char*
1370 monitor_ev_name (int ev)
1371 {
1372         switch (ev) {
1373         case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
1374         case MONO_PROFILER_MONITOR_DONE: return "acquired";
1375         case MONO_PROFILER_MONITOR_FAIL: return "not taken";
1376         default: return "invalid";
1377         }
1378 }
1379
1380 static const char*
1381 get_handle_name (int htype)
1382 {
1383         switch (htype) {
1384         case 0: return "weak";
1385         case 1: return "weaktrack";
1386         case 2: return "normal";
1387         case 3: return "pinned";
1388         default: return "unknown";
1389         }
1390 }
1391
1392 static const char*
1393 get_root_name (int rtype)
1394 {
1395         switch (rtype & MONO_PROFILE_GC_ROOT_TYPEMASK) {
1396         case MONO_PROFILE_GC_ROOT_STACK: return "stack";
1397         case MONO_PROFILE_GC_ROOT_FINALIZER: return "finalizer";
1398         case MONO_PROFILE_GC_ROOT_HANDLE: return "handle";
1399         case MONO_PROFILE_GC_ROOT_OTHER: return "other";
1400         case MONO_PROFILE_GC_ROOT_MISC: return "misc";
1401         default: return "unknown";
1402         }
1403 }
1404
1405 static MethodDesc**
1406 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
1407 {
1408         MethodDesc **frames;
1409         int i;
1410         int flags = decode_uleb128 (p, &p);
1411         int count = decode_uleb128 (p, &p);
1412         if (flags != 0)
1413                 return NULL;
1414         if (count > *size)
1415                 frames = malloc (count * sizeof (void*));
1416         else
1417                 frames = sframes;
1418         for (i = 0; i < count; ++i) {
1419                 intptr_t ptrdiff = decode_sleb128 (p, &p);
1420                 frames [i] = lookup_method (ptr_base + ptrdiff);
1421         }
1422         *size = count;
1423         *endp = p;
1424         return frames;
1425 }
1426
1427 static void
1428 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
1429 {
1430         int i;
1431         for (i = 0; i < num_tracked_objects; ++i) {
1432                 if (tracked_objects [i] != obj)
1433                         continue;
1434                 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
1435                 if (bt && bt->count) {
1436                         int k;
1437                         for (k = 0; k < bt->count; ++k)
1438                                 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1439                 }
1440         }
1441 }
1442
1443 static void
1444 track_handle (uintptr_t obj, int htype, uint32_t handle)
1445 {
1446         int i;
1447         for (i = 0; i < num_tracked_objects; ++i) {
1448                 if (tracked_objects [i] == obj)
1449                         fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1450         }
1451 }
1452
1453 static void
1454 track_move (uintptr_t src, uintptr_t dst)
1455 {
1456         int i;
1457         for (i = 0; i < num_tracked_objects; ++i) {
1458                 if (tracked_objects [i] == src)
1459                         fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1460                 else if (tracked_objects [i] == dst)
1461                         fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1462         }
1463 }
1464
1465 static void
1466 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1467 {
1468         int i;
1469         for (i = 0; i < num_tracked_objects; ++i) {
1470                 if (tracked_objects [i] == obj) 
1471                         fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1472         }
1473 }
1474
1475 static void
1476 found_object (uintptr_t obj)
1477 {
1478         num_tracked_objects ++;
1479         tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1480         tracked_objects [num_tracked_objects - 1] = obj;
1481 }
1482
1483 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1484 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1485
1486 static int
1487 decode_buffer (ProfContext *ctx)
1488 {
1489         unsigned char *p;
1490         unsigned char *end;
1491         intptr_t thread_id;
1492         intptr_t ptr_base;
1493         intptr_t obj_base;
1494         intptr_t method_base;
1495         uint64_t time_base;
1496         uint64_t file_offset;
1497         int len, i;
1498         ThreadContext *thread;
1499
1500 #ifdef HAVE_SYS_ZLIB
1501         if (ctx->gzfile)
1502                 file_offset = gztell (ctx->gzfile);
1503         else
1504 #endif
1505                 file_offset = ftell (ctx->file);
1506         if (!load_data (ctx, 48))
1507                 return 0;
1508         p = ctx->buf;
1509         if (read_int32 (p) != BUF_ID) {
1510                 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
1511                 for (i = 0; i < 48; ++i) {
1512                         fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
1513                 }
1514                 return 0;
1515         }
1516         len = read_int32 (p + 4);
1517         time_base = read_int64 (p + 8);
1518         ptr_base = read_int64 (p + 16);
1519         obj_base = read_int64 (p + 24);
1520         thread_id = read_int64 (p + 32);
1521         method_base = read_int64 (p + 40);
1522         if (debug)
1523                 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1524         thread = load_thread (ctx, thread_id);
1525         if (!load_data (ctx, len))
1526                 return 0;
1527         if (!startup_time) {
1528                 startup_time = time_base;
1529                 if (time_from) {
1530                         time_from += startup_time;
1531                         time_to += startup_time;
1532                 }
1533                 if (!thread->name)
1534                         thread->name = pstrdup ("Main");
1535         }
1536         for (i = 0; i < thread->stack_id; ++i)
1537                 thread->stack [i]->recurse_count++;
1538         p = ctx->buf;
1539         end = p + len;
1540         while (p < end) {
1541                 switch (*p & 0xf) {
1542                 case TYPE_GC: {
1543                         int subtype = *p & 0xf0;
1544                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1545                         LOG_TIME (time_base, tdiff);
1546                         time_base += tdiff;
1547                         if (subtype == TYPE_GC_RESIZE) {
1548                                 uint64_t new_size = decode_uleb128 (p, &p);
1549                                 if (debug)
1550                                         fprintf (outfile, "gc heap resized to %llu\n", new_size);
1551                                 gc_resizes++;
1552                                 if (new_size > max_heap_size)
1553                                         max_heap_size = new_size;
1554                         } else if (subtype == TYPE_GC_EVENT) {
1555                                 uint64_t ev = decode_uleb128 (p, &p);
1556                                 int gen = decode_uleb128 (p, &p);
1557                                 if (debug)
1558                                         fprintf (outfile, "gc event for gen%d: %s at %llu (thread: 0x%x)\n", gen, gc_event_name (ev), time_base, thread->thread_id);
1559                                 if (gen > 2) {
1560                                         fprintf (outfile, "incorrect gc gen: %d\n", gen);
1561                                         break;
1562                                 }
1563                                 if (ev == MONO_GC_EVENT_START) {
1564                                         thread->gc_start_times [gen] = time_base;
1565                                         gc_info [gen].count++;
1566                                 } else if (ev == MONO_GC_EVENT_END) {
1567                                         tdiff = time_base - thread->gc_start_times [gen];
1568                                         gc_info [gen].total_time += tdiff;
1569                                         if (tdiff > gc_info [gen].max_time)
1570                                                 gc_info [gen].max_time = tdiff;
1571                                 }
1572                         } else if (subtype == TYPE_GC_MOVE) {
1573                                 int j, num = decode_uleb128 (p, &p);
1574                                 gc_object_moves += num / 2;
1575                                 for (j = 0; j < num; j += 2) {
1576                                         intptr_t obj1diff = decode_sleb128 (p, &p);
1577                                         intptr_t obj2diff = decode_sleb128 (p, &p);
1578                                         if (num_tracked_objects)
1579                                                 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1580                                         if (debug) {
1581                                                 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1582                                         }
1583                                 }
1584                         } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1585                                 int htype = decode_uleb128 (p, &p);
1586                                 uint32_t handle = decode_uleb128 (p, &p);
1587                                 intptr_t objdiff = decode_sleb128 (p, &p);
1588                                 if (htype > 3)
1589                                         return 0;
1590                                 handle_info [htype].created++;
1591                                 handle_info [htype].live++;
1592                                 add_trace_thread (thread, &handle_info [htype].traces, 1);
1593                                 /* FIXME: we don't take into account timing here */
1594                                 if (handle_info [htype].live > handle_info [htype].max_live)
1595                                         handle_info [htype].max_live = handle_info [htype].live;
1596                                 if (num_tracked_objects)
1597                                         track_handle (OBJ_ADDR (objdiff), htype, handle);
1598                                 if (debug)
1599                                         fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
1600                         } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
1601                                 int htype = decode_uleb128 (p, &p);
1602                                 uint32_t handle = decode_uleb128 (p, &p);
1603                                 if (htype > 3)
1604                                         return 0;
1605                                 handle_info [htype].destroyed ++;
1606                                 handle_info [htype].live--;
1607                                 if (debug)
1608                                         fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
1609                         }
1610                         break;
1611                 }
1612                 case TYPE_METADATA: {
1613                         int error = *p & TYPE_LOAD_ERR;
1614                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1615                         int mtype = *p++;
1616                         intptr_t ptrdiff = decode_sleb128 (p, &p);
1617                         LOG_TIME (time_base, tdiff);
1618                         time_base += tdiff;
1619                         if (mtype == TYPE_CLASS) {
1620                                 intptr_t imptrdiff = decode_sleb128 (p, &p);
1621                                 uint64_t flags = decode_uleb128 (p, &p);
1622                                 if (flags) {
1623                                         fprintf (outfile, "non-zero flags in class\n");
1624                                         return 0;
1625                                 }
1626                                 if (debug)
1627                                         fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
1628                                 if (!error)
1629                                         add_class (ptr_base + ptrdiff, (char*)p);
1630                                 while (*p) p++;
1631                                 p++;
1632                         } else if (mtype == TYPE_IMAGE) {
1633                                 uint64_t flags = decode_uleb128 (p, &p);
1634                                 if (flags) {
1635                                         fprintf (outfile, "non-zero flags in image\n");
1636                                         return 0;
1637                                 }
1638                                 if (debug)
1639                                         fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
1640                                 if (!error)
1641                                         add_image (ptr_base + ptrdiff, (char*)p);
1642                                 while (*p) p++;
1643                                 p++;
1644                         } else if (mtype == TYPE_THREAD) {
1645                                 ThreadContext *nt;
1646                                 uint64_t flags = decode_uleb128 (p, &p);
1647                                 if (flags) {
1648                                         fprintf (outfile, "non-zero flags in thread\n");
1649                                         return 0;
1650                                 }
1651                                 nt = get_thread (ctx, ptr_base * ptrdiff);
1652                                 nt->name = pstrdup ((char*)p);
1653                                 if (debug)
1654                                         fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
1655                                 while (*p) p++;
1656                                 p++;
1657                         }
1658                         break;
1659                 }
1660                 case TYPE_ALLOC: {
1661                         int has_bt = *p & TYPE_ALLOC_BT;
1662                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1663                         intptr_t ptrdiff = decode_sleb128 (p, &p);
1664                         intptr_t objdiff = decode_sleb128 (p, &p);
1665                         uint64_t len;
1666                         int num_bt = 0;
1667                         MethodDesc* sframes [8];
1668                         MethodDesc** frames = sframes;
1669                         ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1670                         len = decode_uleb128 (p, &p);
1671                         LOG_TIME (time_base, tdiff);
1672                         time_base += tdiff;
1673                         if (debug)
1674                                 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
1675                         if (has_bt) {
1676                                 num_bt = 8;
1677                                 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1678                                 if (!frames) {
1679                                         fprintf (outfile, "Cannot load backtrace\n");
1680                                         return 0;
1681                                 }
1682                         }
1683                         if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
1684                                 BackTrace *bt;
1685                                 cd->allocs++;
1686                                 cd->alloc_size += len;
1687                                 if (has_bt)
1688                                         bt = add_trace_methods (frames, num_bt, &cd->traces, len);
1689                                 else
1690                                         bt = add_trace_thread (thread, &cd->traces, len);
1691                                 if (find_size && len >= find_size) {
1692                                         if (!find_name || strstr (cd->name, find_name))
1693                                                 found_object (OBJ_ADDR (objdiff));
1694                                 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
1695                                         found_object (OBJ_ADDR (objdiff));
1696                                 }
1697                                 if (num_tracked_objects)
1698                                         tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
1699                         }
1700                         if (frames != sframes)
1701                                 free (frames);
1702                         break;
1703                 }
1704                 case TYPE_METHOD: {
1705                         int subtype = *p & 0xf0;
1706                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1707                         int64_t ptrdiff = decode_sleb128 (p, &p);
1708                         LOG_TIME (time_base, tdiff);
1709                         time_base += tdiff;
1710                         method_base += ptrdiff;
1711                         if (subtype == TYPE_JIT) {
1712                                 intptr_t codediff = decode_sleb128 (p, &p);
1713                                 int codelen = decode_uleb128 (p, &p);
1714                                 if (debug)
1715                                         fprintf (outfile, "jitted method %p (%s), size: %d, code: %p\n", (void*)(method_base), p, codelen, (void*)(ptr_base + codediff));
1716                                 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
1717                                 while (*p) p++;
1718                                 p++;
1719                         } else {
1720                                 MethodDesc *method;
1721                                 if ((thread_filter && thread_filter != thread->thread_id))
1722                                         break;
1723                                 method = lookup_method (method_base);
1724                                 if (subtype == TYPE_ENTER) {
1725                                         add_trace_thread (thread, &method->traces, 1);
1726                                         push_method (thread, method, time_base);
1727                                 } else {
1728                                         pop_method (thread, method, time_base);
1729                                 }
1730                                 if (debug)
1731                                         fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
1732                         }
1733                         break;
1734                 }
1735                 case TYPE_HEAP: {
1736                         int subtype = *p & 0xf0;
1737                         if (subtype == TYPE_HEAP_OBJECT) {
1738                                 HeapObjectDesc *ho;
1739                                 int i;
1740                                 intptr_t objdiff = decode_sleb128 (p + 1, &p);
1741                                 intptr_t ptrdiff = decode_sleb128 (p, &p);
1742                                 uint64_t size = decode_uleb128 (p, &p);
1743                                 uintptr_t num = decode_uleb128 (p, &p);
1744                                 uintptr_t ref_offset;
1745                                 uintptr_t last_obj_offset = 0;
1746                                 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1747                                 if (size) {
1748                                         HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
1749                                         if (collect_traces) {
1750                                                 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
1751                                                 add_heap_shot_obj (thread->current_heap_shot, ho);
1752                                                 ref_offset = 0;
1753                                         }
1754                                 } else {
1755                                         if (collect_traces)
1756                                                 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
1757                                 }
1758                                 for (i = 0; i < num; ++i) {
1759                                         /* FIXME: use object distance to measure how good
1760                                          * the GC is at keeping related objects close
1761                                          */
1762                                         uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
1763                                         intptr_t obj1diff = decode_sleb128 (p, &p);
1764                                         last_obj_offset = offset;
1765                                         if (collect_traces)
1766                                                 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
1767                                         if (num_tracked_objects)
1768                                                 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1769                                 }
1770                                 if (debug && size)
1771                                         fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1772                         } else if (subtype == TYPE_HEAP_ROOT) {
1773                                 uintptr_t num = decode_uleb128 (p + 1, &p);
1774                                 uintptr_t gc_num = decode_uleb128 (p, &p);
1775                                 int i;
1776                                 for (i = 0; i < num; ++i) {
1777                                         intptr_t objdiff = decode_sleb128 (p, &p);
1778                                         int root_type = decode_uleb128 (p, &p);
1779                                         /* we just discard the extra info for now */
1780                                         uintptr_t extra_info = decode_uleb128 (p, &p);
1781                                         if (debug)
1782                                                 fprintf (outfile, "object %p is a %s root\n", (void*)OBJ_ADDR (objdiff), get_root_name (root_type));
1783                                         if (collect_traces)
1784                                                 thread_add_root (thread, OBJ_ADDR (objdiff), root_type, extra_info);
1785                                 }
1786                         } else if (subtype == TYPE_HEAP_END) {
1787                                 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1788                                 LOG_TIME (time_base, tdiff);
1789                                 time_base += tdiff;
1790                                 if (debug)
1791                                         fprintf (outfile, "heap shot end\n");
1792                                 if (collect_traces) {
1793                                         HeapShot *hs = thread->current_heap_shot;
1794                                         if (hs && thread->num_roots) {
1795                                                 /* transfer the root ownershipt to the heapshot */
1796                                                 hs->num_roots = thread->num_roots;
1797                                                 hs->roots = thread->roots;
1798                                                 hs->roots_extra = thread->roots_extra;
1799                                                 hs->roots_types = thread->roots_types;
1800                                         } else {
1801                                                 free (thread->roots);
1802                                                 free (thread->roots_extra);
1803                                                 free (thread->roots_types);
1804                                         }
1805                                         thread->num_roots = 0;
1806                                         thread->size_roots = 0;
1807                                         thread->roots = NULL;
1808                                         thread->roots_extra = NULL;
1809                                         thread->roots_types = NULL;
1810                                         heap_shot_resolve_reverse_refs (hs);
1811                                         heap_shot_mark_objects (hs);
1812                                         heap_shot_free_objects (hs);
1813                                 }
1814                                 thread->current_heap_shot = NULL;
1815                         } else if (subtype == TYPE_HEAP_START) {
1816                                 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1817                                 LOG_TIME (time_base, tdiff);
1818                                 time_base += tdiff;
1819                                 if (debug)
1820                                         fprintf (outfile, "heap shot start\n");
1821                                 thread->current_heap_shot = new_heap_shot (time_base);
1822                         }
1823                         break;
1824                 }
1825                 case TYPE_MONITOR: {
1826                         int event = (*p >> 4) & 0x3;
1827                         int has_bt = *p & TYPE_MONITOR_BT;
1828                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1829                         intptr_t objdiff = decode_sleb128 (p, &p);
1830                         MethodDesc* sframes [8];
1831                         MethodDesc** frames = sframes;
1832                         int record;
1833                         int num_bt = 0;
1834                         LOG_TIME (time_base, tdiff);
1835                         time_base += tdiff;
1836                         record = (!thread_filter || thread_filter == thread->thread_id);
1837                         if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1838                                 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1839                                 if (record) {
1840                                         monitor_contention++;
1841                                         mdesc->contentions++;
1842                                         thread->monitor = mdesc;
1843                                         thread->contention_start = time_base;
1844                                 }
1845                                 if (has_bt) {
1846                                         num_bt = 8;
1847                                         frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1848                                         if (!frames) {
1849                                                 fprintf (outfile, "Cannot load backtrace\n");
1850                                                 return 0;
1851                                         }
1852                                         if (record)
1853                                                 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1854                                 } else {
1855                                         if (record)
1856                                                 add_trace_thread (thread, &mdesc->traces, 1);
1857                                 }
1858                         } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1859                                 if (record) {
1860                                         monitor_failed++;
1861                                         if (thread->monitor && thread->contention_start) {
1862                                                 uint64_t wait_time = time_base - thread->contention_start;
1863                                                 if (wait_time > thread->monitor->max_wait_time)
1864                                                         thread->monitor->max_wait_time = wait_time;
1865                                                 thread->monitor->wait_time += wait_time;
1866                                                 thread->monitor = NULL;
1867                                                 thread->contention_start = 0;
1868                                         }
1869                                 }
1870                         } else if (event == MONO_PROFILER_MONITOR_DONE) {
1871                                 if (record) {
1872                                         monitor_acquired++;
1873                                         if (thread->monitor && thread->contention_start) {
1874                                                 uint64_t wait_time = time_base - thread->contention_start;
1875                                                 if (wait_time > thread->monitor->max_wait_time)
1876                                                         thread->monitor->max_wait_time = wait_time;
1877                                                 thread->monitor->wait_time += wait_time;
1878                                                 thread->monitor = NULL;
1879                                                 thread->contention_start = 0;
1880                                         }
1881                                 }
1882                         }
1883                         if (debug)
1884                                 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1885                         if (frames != sframes)
1886                                 free (frames);
1887                         break;
1888                 }
1889                 case TYPE_EXCEPTION: {
1890                         int subtype = *p & 0x70;
1891                         int has_bt = *p & TYPE_EXCEPTION_BT;
1892                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1893                         MethodDesc* sframes [8];
1894                         MethodDesc** frames = sframes;
1895                         int record;
1896                         LOG_TIME (time_base, tdiff);
1897                         time_base += tdiff;
1898                         record = (!thread_filter || thread_filter == thread->thread_id);
1899                         if (subtype == TYPE_CLAUSE) {
1900                                 int clause_type = decode_uleb128 (p, &p);
1901                                 int clause_num = decode_uleb128 (p, &p);
1902                                 int64_t ptrdiff = decode_sleb128 (p, &p);
1903                                 method_base += ptrdiff;
1904                                 if (record)
1905                                         clause_summary [clause_type]++;
1906                                 if (debug)
1907                                         fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1908                         } else {
1909                                 intptr_t objdiff = decode_sleb128 (p, &p);
1910                                 if (record)
1911                                         throw_count++;
1912                                 if (has_bt) {
1913                                         has_bt = 8;
1914                                         frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1915                                         if (!frames) {
1916                                                 fprintf (outfile, "Cannot load backtrace\n");
1917                                                 return 0;
1918                                         }
1919                                         if (record)
1920                                                 add_trace_methods (frames, has_bt, &exc_traces, 1);
1921                                 } else {
1922                                         if (record)
1923                                                 add_trace_thread (thread, &exc_traces, 1);
1924                                 }
1925                                 if (frames != sframes)
1926                                         free (frames);
1927                                 if (debug)
1928                                         fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1929                         }
1930                         break;
1931                 }
1932                 case TYPE_SAMPLE: {
1933                         int subtype = *p & 0xf0;
1934                         if (subtype == TYPE_SAMPLE_HIT) {
1935                                 int i;
1936                                 int sample_type = decode_uleb128 (p + 1, &p);
1937                                 uint64_t tstamp = decode_uleb128 (p, &p);
1938                                 int count = decode_uleb128 (p, &p);
1939                                 for (i = 0; i < count; ++i) {
1940                                         uintptr_t ip = ptr_base + decode_sleb128 (p, &p);
1941                                         add_stat_sample (sample_type, ip);
1942                                         if (debug)
1943                                                 fprintf (outfile, "sample hit, type: %d at %p\n", sample_type, (void*)ip);
1944                                 }
1945                         } else if (subtype == TYPE_SAMPLE_USYM) {
1946                                 /* un unmanaged symbol description */
1947                                 uintptr_t addr = ptr_base + decode_sleb128 (p + 1, &p);
1948                                 uintptr_t size = decode_uleb128 (p, &p);
1949                                 char *name;
1950                                 name = pstrdup ((char*)p);
1951                                 add_unmanaged_symbol (addr, name, size);
1952                                 if (debug)
1953                                         fprintf (outfile, "unmanaged symbol %s at %p\n", name, (void*)addr);
1954                                 while (*p) p++;
1955                                 p++;
1956                         } else if (subtype == TYPE_SAMPLE_UBIN) {
1957                                 /* un unmanaged binary loaded in memory */
1958                                 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1959                                 uintptr_t addr = decode_sleb128 (p, &p);
1960                                 uint64_t offset = decode_uleb128 (p, &p);
1961                                 uintptr_t size = decode_uleb128 (p, &p);
1962                                 char *name;
1963                                 LOG_TIME (time_base, tdiff);
1964                                 time_base += tdiff;
1965                                 name = pstrdup ((char*)p);
1966                                 add_unmanaged_binary (addr, name, size);
1967                                 if (debug)
1968                                         fprintf (outfile, "unmanaged binary %s at %p\n", name, (void*)addr);
1969                                 while (*p) p++;
1970                                 p++;
1971                         } else {
1972                                 return 0;
1973                         }
1974                         break;
1975                 }
1976                 default:
1977                         fprintf (outfile, "unhandled profiler event: 0x%x at file offset: %llu + %d (len: %d\n)\n", *p, file_offset, p - ctx->buf, len);
1978                         exit (1);
1979                 }
1980         }
1981         thread->last_time = time_base;
1982         for (i = 0; i < thread->stack_id; ++i)
1983                 thread->stack [i]->recurse_count = 0;
1984         return 1;
1985 }
1986
1987 static ProfContext*
1988 load_file (char *name)
1989 {
1990         unsigned char *p;
1991         ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1992         if (strcmp (name, "-") == 0)
1993                 ctx->file = stdin;
1994         else
1995                 ctx->file = fopen (name, "rb");
1996         if (!ctx->file) {
1997                 printf ("Cannot open file: %s\n", name);
1998                 exit (1);
1999         }
2000 #if defined (HAVE_SYS_ZLIB)
2001         if (ctx->file != stdin)
2002                 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
2003 #endif
2004         if (!load_data (ctx, 32))
2005                 return NULL;
2006         p = ctx->buf;
2007         if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
2008                 return NULL;
2009         ctx->version_major = p [4];
2010         ctx->version_minor = p [5];
2011         ctx->data_version = p [6];
2012         /* reading 64 bit files on 32 bit systems not supported yet */
2013         if (p [7] > sizeof (void*))
2014                 return NULL;
2015         if (read_int32 (p + 20)) /* flags must be 0 */
2016                 return NULL;
2017         ctx->startup_time = read_int64 (p + 8);
2018         ctx->timer_overhead = read_int32 (p + 16);
2019         ctx->pid = read_int32 (p + 24);
2020         ctx->port = read_int16 (p + 28);
2021         return ctx;
2022 }
2023
2024 enum {
2025         ALLOC_SORT_BYTES,
2026         ALLOC_SORT_COUNT
2027 };
2028 static int alloc_sort_mode = ALLOC_SORT_BYTES;
2029
2030 static int
2031 compare_class (const void *a, const void *b)
2032 {
2033         ClassDesc *const*A = a;
2034         ClassDesc *const*B = b;
2035         uint64_t vala, valb;
2036         if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2037                 vala = (*A)->alloc_size;
2038                 valb = (*B)->alloc_size;
2039         } else {
2040                 vala = (*A)->allocs;
2041                 valb = (*B)->allocs;
2042         }
2043         if (valb == vala)
2044                 return 0;
2045         if (valb < vala)
2046                 return -1;
2047         return 1;
2048 }
2049
2050 static void
2051 dump_header (ProfContext *ctx)
2052 {
2053         time_t st = ctx->startup_time / 1000;
2054         char *t = ctime (&st);
2055         fprintf (outfile, "\nMono log profiler data\n");
2056         fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
2057         fprintf (outfile, "\tData version: %d\n", ctx->data_version);
2058         fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
2059         fprintf (outfile, "\tProgram startup: %s", t);
2060         if (ctx->pid)
2061                 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
2062         if (ctx->port)
2063                 fprintf (outfile, "\tServer listening on: %d\n", ctx->port);
2064 }
2065
2066 static void
2067 dump_traces (TraceDesc *traces, const char *desc)
2068 {
2069         int j;
2070         if (!show_traces)
2071                 return;
2072         if (!traces->count)
2073                 return;
2074         sort_context_array (traces);
2075         for (j = 0; j < traces->count; ++j) {
2076                 int k;
2077                 BackTrace *bt;
2078                 bt = traces->traces [j].bt;
2079                 if (!bt->count)
2080                         continue;
2081                 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
2082                 for (k = 0; k < bt->count; ++k)
2083                         fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
2084         }
2085 }
2086
2087 static void
2088 dump_threads (ProfContext *ctx)
2089 {
2090         ThreadContext *thread;
2091         fprintf (outfile, "\nThread summary\n");
2092         for (thread = ctx->threads; thread; thread = thread->next) {
2093                 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
2094         }
2095 }
2096
2097 static void
2098 dump_exceptions (void)
2099 {
2100         int i;
2101         fprintf (outfile, "\nException summary\n");
2102         fprintf (outfile, "\tThrows: %llu\n", throw_count);
2103         dump_traces (&exc_traces, "throws");
2104         for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
2105                 if (!clause_summary [i])
2106                         continue;
2107                 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
2108         }
2109 }
2110
2111 static int
2112 compare_monitor (const void *a, const void *b)
2113 {
2114         MonitorDesc *const*A = a;
2115         MonitorDesc *const*B = b;
2116         if ((*B)->wait_time == (*A)->wait_time)
2117                 return 0;
2118         if ((*B)->wait_time < (*A)->wait_time)
2119                 return -1;
2120         return 1;
2121 }
2122
2123 static void
2124 dump_monitors (void)
2125 {
2126         MonitorDesc **monitors;
2127         int i, j;
2128         if (!num_monitors)
2129                 return;
2130         monitors = malloc (sizeof (void*) * num_monitors);
2131         for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
2132                 MonitorDesc *mdesc = monitor_hash [i];
2133                 while (mdesc) {
2134                         monitors [j++] = mdesc;
2135                         mdesc = mdesc->next;
2136                 }
2137         }
2138         qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
2139         fprintf (outfile, "\nMonitor lock summary\n");
2140         for (i = 0; i < num_monitors; ++i) {
2141                 MonitorDesc *mdesc = monitors [i];
2142                 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
2143                 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
2144                         mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
2145                 dump_traces (&mdesc->traces, "contentions");
2146         }
2147         fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
2148         fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
2149         fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
2150 }
2151
2152 static void
2153 dump_gcs (void)
2154 {
2155         int i;
2156         fprintf (outfile, "\nGC summary\n");
2157         fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
2158         fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
2159         fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
2160         for (i = 0; i < 3; ++i) {
2161                 if (!gc_info [i].count)
2162                         continue;
2163                 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
2164                         i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
2165                         gc_info [i].total_time / gc_info [i].count / 1000);
2166         }
2167         for (i = 0; i < 3; ++i) {
2168                 if (!handle_info [i].max_live)
2169                         continue;
2170                 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
2171                         get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
2172                 dump_traces (&handle_info [i].traces, "created");
2173         }
2174 }
2175
2176 static void
2177 dump_jit (void)
2178 {
2179         int i;
2180         int code_size = 0;
2181         int compiled_methods = 0;
2182         MethodDesc* m;
2183         fprintf (outfile, "\nJIT summary\n");
2184         for (i = 0; i < HASH_SIZE; ++i) {
2185                 m = method_hash [i];
2186                 for (m = method_hash [i]; m; m = m->next) {
2187                         if (!m->code)
2188                                 continue;
2189                         compiled_methods++;
2190                         code_size += m->len;
2191                 }
2192         }
2193         fprintf (outfile, "\tCompiled methods: %d\n", compiled_methods);
2194         fprintf (outfile, "\tGenerated code size: %d\n", code_size);
2195 }
2196
2197 static void
2198 dump_allocations (void)
2199 {
2200         int i, c;
2201         intptr_t allocs = 0;
2202         uint64_t size = 0;
2203         int header_done = 0;
2204         ClassDesc **classes = malloc (num_classes * sizeof (void*));
2205         ClassDesc *cd;
2206         c = 0;
2207         for (i = 0; i < HASH_SIZE; ++i) {
2208                 cd = class_hash [i];
2209                 while (cd) {
2210                         classes [c++] = cd;
2211                         cd = cd->next;
2212                 }
2213         }
2214         qsort (classes, num_classes, sizeof (void*), compare_class);
2215         for (i = 0; i < num_classes; ++i) {
2216                 cd = classes [i];
2217                 if (!cd->allocs)
2218                         continue;
2219                 allocs += cd->allocs;
2220                 size += cd->alloc_size;
2221                 if (!header_done++) {
2222                         fprintf (outfile, "\nAllocation summary\n");
2223                         fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
2224                 }
2225                 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
2226                 dump_traces (&cd->traces, "bytes");
2227         }
2228         if (allocs)
2229                 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
2230 }
2231
2232 enum {
2233         METHOD_SORT_TOTAL,
2234         METHOD_SORT_SELF,
2235         METHOD_SORT_CALLS
2236 };
2237
2238 static int method_sort_mode = METHOD_SORT_TOTAL;
2239
2240 static int
2241 compare_method (const void *a, const void *b)
2242 {
2243         MethodDesc *const*A = a;
2244         MethodDesc *const*B = b;
2245         uint64_t vala, valb;
2246         if (method_sort_mode == METHOD_SORT_SELF) {
2247                 vala = (*A)->self_time;
2248                 valb = (*B)->self_time;
2249         } else if (method_sort_mode == METHOD_SORT_CALLS) {
2250                 vala = (*A)->calls;
2251                 valb = (*B)->calls;
2252         } else {
2253                 vala = (*A)->total_time;
2254                 valb = (*B)->total_time;
2255         }
2256         if (vala == valb)
2257                 return 0;
2258         if (valb < vala)
2259                 return -1;
2260         return 1;
2261 }
2262
2263 static void
2264 dump_metadata (void)
2265 {
2266         fprintf (outfile, "\nMetadata summary\n");
2267         fprintf (outfile, "\tLoaded images: %d\n", num_images);
2268         if (verbose) {
2269                 ImageDesc *image;
2270                 int i;
2271                 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
2272                         image = image_hash [i];
2273                         while (image) {
2274                                 fprintf (outfile, "\t\t%s\n", image->filename);
2275                                 image = image->next;
2276                         }
2277                 }
2278         }
2279
2280 }
2281
2282 static void
2283 dump_methods (void)
2284 {
2285         int i, c;
2286         uint64_t calls = 0;
2287         int header_done = 0;
2288         MethodDesc **methods = malloc (num_methods * sizeof (void*));
2289         MethodDesc *cd;
2290         c = 0;
2291         for (i = 0; i < HASH_SIZE; ++i) {
2292                 cd = method_hash [i];
2293                 while (cd) {
2294                         cd->total_time = cd->self_time + cd->callee_time;
2295                         methods [c++] = cd;
2296                         cd = cd->next;
2297                 }
2298         }
2299         qsort (methods, num_methods, sizeof (void*), compare_method);
2300         for (i = 0; i < num_methods; ++i) {
2301                 uint64_t msecs;
2302                 uint64_t smsecs;
2303                 cd = methods [i];
2304                 if (!cd->calls)
2305                         continue;
2306                 calls += cd->calls;
2307                 msecs = cd->total_time / 1000000;
2308                 smsecs = (cd->total_time - cd->callee_time) / 1000000;
2309                 if (!msecs && !verbose)
2310                         continue;
2311                 if (!header_done++) {
2312                         fprintf (outfile, "\nMethod call summary\n");
2313                         fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
2314                 }
2315                 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
2316                 dump_traces (&cd->traces, "calls");
2317         }
2318         if (calls)
2319                 fprintf (outfile, "Total calls: %llu\n", calls);
2320 }
2321
2322 static int
2323 compare_heap_class (const void *a, const void *b)
2324 {
2325         HeapClassDesc *const*A = a;
2326         HeapClassDesc *const*B = b;
2327         uint64_t vala, valb;
2328         if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2329                 vala = (*A)->total_size;
2330                 valb = (*B)->total_size;
2331         } else {
2332                 vala = (*A)->count;
2333                 valb = (*B)->count;
2334         }
2335         if (valb == vala)
2336                 return 0;
2337         if (valb < vala)
2338                 return -1;
2339         return 1;
2340 }
2341
2342 static int
2343 compare_rev_class (const void *a, const void *b)
2344 {
2345         const HeapClassRevRef *A = a;
2346         const HeapClassRevRef *B = b;
2347         if (B->count == A->count)
2348                 return 0;
2349         if (B->count < A->count)
2350                 return -1;
2351         return 1;
2352 }
2353
2354 static void
2355 dump_rev_claases (HeapClassRevRef *revs, int count)
2356 {
2357         int j;
2358         if (!show_traces)
2359                 return;
2360         if (!count)
2361                 return;
2362         for (j = 0; j < count; ++j) {
2363                 HeapClassDesc *cd = revs [j].klass;
2364                 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
2365         }
2366 }
2367
2368 static void
2369 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
2370 {
2371         uint64_t size = 0;
2372         uint64_t count = 0;
2373         int ccount = 0;
2374         int i;
2375         HeapClassDesc *cd;
2376         HeapClassDesc **sorted;
2377         sorted = malloc (sizeof (void*) * hs->class_count);
2378         for (i = 0; i < hs->hash_size; ++i) {
2379                 cd = hs->class_hash [i];
2380                 if (!cd)
2381                         continue;
2382                 count += cd->count;
2383                 size += cd->total_size;
2384                 sorted [ccount++] = cd;
2385         }
2386         hs->sorted = sorted;
2387         qsort (sorted, ccount, sizeof (void*), compare_heap_class);
2388         fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d, roots: %d\n",
2389                 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount, hs->num_roots);
2390         if (!verbose && ccount > 30)
2391                 ccount = 30;
2392         fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
2393         for (i = 0; i < ccount; ++i) {
2394                 HeapClassRevRef *rev_sorted;
2395                 int j, k;
2396                 HeapClassDesc *ocd = NULL;
2397                 cd = sorted [i];
2398                 if (last_hs)
2399                         ocd = heap_class_lookup (last_hs, cd->klass);
2400                 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
2401                 if (ocd) {
2402                         int64_t bdiff = cd->total_size - ocd->total_size;
2403                         int64_t cdiff = cd->count - ocd->count;
2404                         fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
2405                 } else {
2406                         fprintf (outfile, "\n");
2407                 }
2408                 if (!collect_traces)
2409                         continue;
2410                 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
2411                 k = 0;
2412                 for (j = 0; j < cd->rev_hash_size; ++j) {
2413                         if (cd->rev_hash [j].klass)
2414                                 rev_sorted [k++] = cd->rev_hash [j];
2415                 }
2416                 assert (cd->rev_count == k);
2417                 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
2418                 if (cd->root_references)
2419                         fprintf (outfile, "\t\t%d root references (%d pinning)\n", cd->root_references, cd->pinned_references);
2420                 dump_rev_claases (rev_sorted, cd->rev_count);
2421                 free (rev_sorted);
2422         }
2423         free (sorted);
2424 }
2425
2426 static int
2427 compare_heap_shots (const void *a, const void *b)
2428 {
2429         HeapShot *const*A = a;
2430         HeapShot *const*B = b;
2431         if ((*B)->timestamp == (*A)->timestamp)
2432                 return 0;
2433         if ((*B)->timestamp > (*A)->timestamp)
2434                 return -1;
2435         return 1;
2436 }
2437
2438 static void
2439 dump_heap_shots (void)
2440 {
2441         HeapShot **hs_sorted;
2442         HeapShot *hs;
2443         HeapShot *last_hs = NULL;
2444         int i;
2445         if (!heap_shots)
2446                 return;
2447         hs_sorted = malloc (num_heap_shots * sizeof (void*));
2448         fprintf (outfile, "\nHeap shot summary\n");
2449         i = 0;
2450         for (hs = heap_shots; hs; hs = hs->next)
2451                 hs_sorted [i++] = hs;
2452         qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
2453         for (i = 0; i < num_heap_shots; ++i) {
2454                 hs = hs_sorted [i];
2455                 heap_shot_summary (hs, i, last_hs);
2456                 last_hs = hs;
2457         }
2458 }
2459
2460 static void
2461 flush_context (ProfContext *ctx)
2462 {
2463         ThreadContext *thread;
2464         /* FIXME: sometimes there are leftovers: indagate */
2465         for (thread = ctx->threads; thread; thread = thread->next) {
2466                 while (thread->stack_id) {
2467                         if (debug)
2468                                 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
2469                         pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
2470                 }
2471         }
2472 }
2473
2474 static const char *reports = "header,jit,gc,sample,alloc,call,metadata,exception,monitor,thread,heapshot";
2475
2476 static const char*
2477 match_option (const char *p, const char *opt)
2478 {
2479         int len = strlen (opt);
2480         if (strncmp (p, opt, len) == 0) {
2481                 if (p [len] == ',')
2482                         len++;
2483                 return p + len;
2484         }
2485         return p;
2486 }
2487
2488 static int
2489 print_reports (ProfContext *ctx, const char *reps, int parse_only)
2490 {
2491         const char *opt;
2492         const char *p;
2493         for (p = reps; *p; p = opt) {
2494                 if ((opt = match_option (p, "header")) != p) {
2495                         if (!parse_only)
2496                                 dump_header (ctx);
2497                         continue;
2498                 }
2499                 if ((opt = match_option (p, "thread")) != p) {
2500                         if (!parse_only)
2501                                 dump_threads (ctx);
2502                         continue;
2503                 }
2504                 if ((opt = match_option (p, "gc")) != p) {
2505                         if (!parse_only)
2506                                 dump_gcs ();
2507                         continue;
2508                 }
2509                 if ((opt = match_option (p, "jit")) != p) {
2510                         if (!parse_only)
2511                                 dump_jit ();
2512                         continue;
2513                 }
2514                 if ((opt = match_option (p, "alloc")) != p) {
2515                         if (!parse_only)
2516                                 dump_allocations ();
2517                         continue;
2518                 }
2519                 if ((opt = match_option (p, "call")) != p) {
2520                         if (!parse_only)
2521                                 dump_methods ();
2522                         continue;
2523                 }
2524                 if ((opt = match_option (p, "metadata")) != p) {
2525                         if (!parse_only)
2526                                 dump_metadata ();
2527                         continue;
2528                 }
2529                 if ((opt = match_option (p, "exception")) != p) {
2530                         if (!parse_only)
2531                                 dump_exceptions ();
2532                         continue;
2533                 }
2534                 if ((opt = match_option (p, "monitor")) != p) {
2535                         if (!parse_only)
2536                                 dump_monitors ();
2537                         continue;
2538                 }
2539                 if ((opt = match_option (p, "heapshot")) != p) {
2540                         if (!parse_only)
2541                                 dump_heap_shots ();
2542                         continue;
2543                 }
2544                 if ((opt = match_option (p, "sample")) != p) {
2545                         if (!parse_only)
2546                                 dump_samples ();
2547                         continue;
2548                 }
2549                 return 0;
2550         }
2551         return 1;
2552 }
2553
2554 static int
2555 add_find_spec (const char *p)
2556 {
2557         if (p [0] == 'S' && p [1] == ':') {
2558                 char *vale;
2559                 find_size = strtoul (p + 2, &vale, 10);
2560                 return 1;
2561         } else if (p [0] == 'T' && p [1] == ':') {
2562                 find_name = p + 2;
2563                 return 1;
2564         }
2565         return 0;
2566 }
2567
2568 static void
2569 usage (void)
2570 {
2571         printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
2572         printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
2573         printf ("FILENAME can be '-' to read from standard input.\n");
2574         printf ("Options:\n");
2575         printf ("\t--help               display this help\n");
2576         printf ("\t--out=FILE           write to FILE instead of stdout\n");
2577         printf ("\t--traces             collect and show backtraces\n"); 
2578         printf ("\t--maxframes=NUM      limit backtraces to NUM entries\n");
2579         printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
2580         printf ("\t                     %s\n", reports);
2581         printf ("\t--method-sort=MODE   sort methods according to MODE: total, self, calls\n");
2582         printf ("\t--alloc-sort=MODE    sort allocations according to MODE: bytes, count\n");
2583         printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
2584         printf ("\t--find=FINDSPEC      find and track objects matching FINFSPEC, where FINDSPEC is:\n");
2585         printf ("\t                     S:minimum_size or T:partial_name\n");
2586         printf ("\t--thread=THREADID    consider just the data for thread THREADID\n");
2587         printf ("\t--time=FROM-TO       consider data FROM seconds from startup up to TO seconds\n");
2588         printf ("\t--verbose            increase verbosity level\n");
2589         printf ("\t--debug              display decoding debug info for mprof-report devs\n");
2590 }
2591
2592 int
2593 main (int argc, char *argv[])
2594 {
2595         ProfContext *ctx;
2596         int i;
2597         outfile = stdout;
2598         for (i = 1; i < argc; ++i) {
2599                 if (strcmp ("--debug", argv [i]) == 0) {
2600                         debug++;
2601                 } else if (strcmp ("--help", argv [i]) == 0) {
2602                         usage ();
2603                         return 0;
2604                 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
2605                         const char *val = argv [i] + 13;
2606                         if (strcmp (val, "bytes") == 0) {
2607                                 alloc_sort_mode = ALLOC_SORT_BYTES;
2608                         } else if (strcmp (val, "count") == 0) {
2609                                 alloc_sort_mode = ALLOC_SORT_COUNT;
2610                         } else {
2611                                 usage ();
2612                                 return 1;
2613                         }
2614                 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
2615                         const char *val = argv [i] + 14;
2616                         if (strcmp (val, "total") == 0) {
2617                                 method_sort_mode = METHOD_SORT_TOTAL;
2618                         } else if (strcmp (val, "self") == 0) {
2619                                 method_sort_mode = METHOD_SORT_SELF;
2620                         } else if (strcmp (val, "calls") == 0) {
2621                                 method_sort_mode = METHOD_SORT_CALLS;
2622                         } else {
2623                                 usage ();
2624                                 return 1;
2625                         }
2626                 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
2627                         const char *val = argv [i] + 10;
2628                         if (!print_reports (NULL, val, 1)) {
2629                                 usage ();
2630                                 return 1;
2631                         }
2632                         reports = val;
2633                 } else if (strncmp ("--out=", argv [i], 6) == 0) {
2634                         const char *val = argv [i] + 6;
2635                         outfile = fopen (val, "w");
2636                         if (!outfile) {
2637                                 printf ("Cannot open output file: %s\n", val);
2638                                 return 1;
2639                         }
2640                 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
2641                         const char *val = argv [i] + 12;
2642                         char *vale;
2643                         trace_max = strtoul (val, &vale, 10);
2644                 } else if (strncmp ("--find=", argv [i], 7) == 0) {
2645                         const char *val = argv [i] + 7;
2646                         if (!add_find_spec (val)) {
2647                                 usage ();
2648                                 return 1;
2649                         }
2650                 } else if (strncmp ("--track=", argv [i], 8) == 0) {
2651                         const char *val = argv [i] + 8;
2652                         char *vale;
2653                         while (*val) {
2654                                 uintptr_t tracked_obj;
2655                                 if (*val == ',') {
2656                                         val++;
2657                                         continue;
2658                                 }
2659                                 tracked_obj = strtoul (val, &vale, 0);
2660                                 found_object (tracked_obj);
2661                                 val = vale;
2662                         }
2663                 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
2664                         const char *val = argv [i] + 9;
2665                         char *vale;
2666                         thread_filter = strtoul (val, &vale, 0);
2667                 } else if (strncmp ("--time=", argv [i], 7) == 0) {
2668                         char *val = pstrdup (argv [i] + 7);
2669                         double from_secs, to_secs;
2670                         char *top = strchr (val, '-');
2671                         if (!top) {
2672                                 usage ();
2673                                 return 1;
2674                         }
2675                         *top++ = 0;
2676                         from_secs = atof (val);
2677                         to_secs = atof (top);
2678                         free (val);
2679                         if (from_secs > to_secs) {
2680                                 usage ();
2681                                 return 1;
2682                         }
2683                         time_from = from_secs * 1000000000;
2684                         time_to = to_secs * 1000000000;
2685                 } else if (strcmp ("--verbose", argv [i]) == 0) {
2686                         verbose++;
2687                 } else if (strcmp ("--traces", argv [i]) == 0) {
2688                         show_traces = 1;
2689                         collect_traces = 1;
2690                 } else {
2691                         break;
2692                 }
2693         }
2694         if (i >= argc) {
2695                 usage ();
2696                 return 2;
2697         }
2698         ctx = load_file (argv [i]);
2699         if (!ctx) {
2700                 printf ("Not a log profiler data file (or unsupported version).\n");
2701                 return 1;
2702         }
2703         while (decode_buffer (ctx));
2704         flush_context (ctx);
2705         if (num_tracked_objects)
2706                 return 0;
2707         print_reports (ctx, reports, 0);
2708         return 0;
2709 }
2710