Remove the mono-profiler- prefix from all source files in the profiler subdir.
[mono.git] / mono / dis / dump.c
1 /*
2  * dump.c: Dumping routines for the disassembler.
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9 #include <config.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <glib.h>
13 #include <math.h>
14 #include "meta.h"
15 #include "util.h"
16 #include "dump.h"
17 #include "get.h"
18 #include "declsec.h"
19 #include "mono/metadata/loader.h"
20 #include "mono/metadata/class.h"
21 #include "mono/metadata/class-internals.h"
22 #include "mono/utils/mono-compiler.h"
23
24 #if defined(__native_client__) && defined(__GLIBC__)
25 volatile int __nacl_thread_suspension_needed = 0;
26 void __nacl_suspend_thread_if_needed() {}
27 #endif
28
29 void
30 dump_table_assembly (MonoImage *m)
31 {
32         MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
33         guint32 cols [MONO_ASSEMBLY_SIZE];
34         const char *ptr;
35         int len;
36
37         fprintf (output, "Assembly Table\n");
38
39         if (!t->rows)
40                 return;
41
42         mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
43
44         fprintf (output, "Name:          %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
45         fprintf (output, "Hash Algoritm: 0x%08x\n", cols [MONO_ASSEMBLY_HASH_ALG]);
46         fprintf (output, "Version:       %d.%d.%d.%d\n", cols [MONO_ASSEMBLY_MAJOR_VERSION], 
47                                         cols [MONO_ASSEMBLY_MINOR_VERSION], 
48                                         cols [MONO_ASSEMBLY_BUILD_NUMBER], 
49                                         cols [MONO_ASSEMBLY_REV_NUMBER]);
50         fprintf (output, "Flags:         0x%08x\n", cols [MONO_ASSEMBLY_FLAGS]);
51         fprintf (output, "PublicKey:     BlobPtr (0x%08x)\n", cols [MONO_ASSEMBLY_PUBLIC_KEY]);
52
53         ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
54         len = mono_metadata_decode_value (ptr, &ptr);
55         if (len > 0){
56                 fprintf (output, "\tDump:");
57                 hex_dump (ptr, 0, len);
58                 fprintf (output, "\n");
59         } else
60                 fprintf (output, "\tZero sized public key\n");
61         
62         fprintf (output, "Culture:       %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
63         fprintf (output, "\n");
64 }
65
66 void
67 dump_table_typeref (MonoImage *m)
68 {
69         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEREF];
70         int i;
71
72         fprintf (output, "Typeref Table\n");
73         
74         for (i = 1; i <= t->rows; i++){
75                 char *s = get_typeref (m, i);
76                 
77                 fprintf (output, "%d: %s\n", i, s);
78                 g_free (s);
79         }
80         fprintf (output, "\n");
81 }
82
83 void
84 dump_table_typedef (MonoImage *m)
85 {
86         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
87         int i;
88
89         fprintf (output, "Typedef Table\n");
90         
91         for (i = 1; i <= t->rows; i++){
92                 char *s = get_typedef (m, i);
93                 guint32 cols [MONO_TYPEDEF_SIZE];
94
95                 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], i - 1, cols, MONO_TYPEDEF_SIZE);
96
97                 fprintf (output, "%d: %s (flist=%d, mlist=%d, flags=0x%x, extends=0x%x)\n", i, s, 
98                                         cols [MONO_TYPEDEF_FIELD_LIST], cols [MONO_TYPEDEF_METHOD_LIST],
99                                         cols [MONO_TYPEDEF_FLAGS], cols [MONO_TYPEDEF_EXTENDS]);
100                 g_free (s);
101         }
102         fprintf (output, "\n");
103 }
104
105 void
106 dump_table_typespec (MonoImage *m)
107 {
108         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPESPEC];
109         int i;
110
111         fprintf (output, "Typespec Table\n");
112         
113         for (i = 1; i <= t->rows; i++){         
114                 char *typespec = get_typespec (m, i, TRUE, NULL);
115
116                 fprintf (output, "%d: %s\n", i, typespec);
117                 g_free (typespec);
118         }
119         fprintf (output, "\n");
120 }
121
122 void
123 dump_table_assemblyref (MonoImage *m)
124 {
125         MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
126         int i;
127
128         fprintf (output, "AssemblyRef Table\n");
129         
130         for (i = 0; i < t->rows; i++){
131                 const char *ptr;
132                 int len;
133                 guint32 cols [MONO_ASSEMBLYREF_SIZE];
134
135                 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
136                 fprintf (output, "%d: Version=%d.%d.%d.%d\n\tName=%s\n", i + 1,
137                          cols [MONO_ASSEMBLYREF_MAJOR_VERSION], 
138                          cols [MONO_ASSEMBLYREF_MINOR_VERSION], 
139                          cols [MONO_ASSEMBLYREF_BUILD_NUMBER], 
140                          cols [MONO_ASSEMBLYREF_REV_NUMBER],
141                          mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
142                 fprintf (output, "\tFlags=0x%08x\n", cols [MONO_ASSEMBLYREF_FLAGS]);
143                 ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
144                 len = mono_metadata_decode_value (ptr, &ptr);
145                 if (len > 0){
146                         fprintf (output, "\tPublic Key:");
147                         hex_dump (ptr, 0, len);
148                         fprintf (output, "\n");
149                 } else
150                         fprintf (output, "\tZero sized public key\n");
151                 
152         }
153         fprintf (output, "\n");
154 }
155
156 void
157 dump_table_param (MonoImage *m)
158 {
159         MonoTableInfo *t = &m->tables [MONO_TABLE_PARAM];
160         int i;
161
162         fprintf (output, "Param Table\n");
163         
164         for (i = 0; i < t->rows; i++){
165                 guint32 cols [MONO_PARAM_SIZE];
166
167                 mono_metadata_decode_row (t, i, cols, MONO_PARAM_SIZE);
168                 fprintf (output, "%d: 0x%04x %d %s\n",
169                          i + 1,
170                          cols [MONO_PARAM_FLAGS], cols [MONO_PARAM_SEQUENCE], 
171                          mono_metadata_string_heap (m, cols [MONO_PARAM_NAME]));
172         }
173         fprintf (output, "\n");
174 }
175
176 void
177 dump_table_field (MonoImage *m)
178 {
179         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
180         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
181         MonoTableInfo *fl = &m->tables [MONO_TABLE_FIELDLAYOUT];
182         MonoTableInfo *rva = &m->tables [MONO_TABLE_FIELDRVA];
183         int i, current_type, offset_row, rva_row;
184         guint32 first_m, last_m;
185
186         fprintf (output, "Field Table (1..%d)\n", t->rows);
187         
188         rva_row = offset_row = current_type = 1;
189         last_m = first_m = 1;
190         for (i = 1; i <= t->rows; i++){
191                 guint32 cols [MONO_FIELD_SIZE];
192                 char *sig, *flags;
193
194                 /*
195                  * Find the next type.
196                  */
197                 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_FIELD_LIST))) {
198                         current_type++;
199                 }
200                 if (i == first_m) {
201                         fprintf (output, "########## %s.%s\n",
202                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
203                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
204                         first_m = last_m;
205                 }
206                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_SIZE);
207                 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE], NULL);
208                 flags = field_flags (cols [MONO_FIELD_FLAGS]);
209                 fprintf (output, "%d: %s %s: %s\n",
210                          i,
211                          sig,
212                          mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]),
213                          flags);
214                 g_free (sig);
215                 g_free (flags);
216                 if (offset_row <= fl->rows && (mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_FIELD) == i)) {
217                         fprintf (output, "\texplicit offset: %d\n", mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_OFFSET));
218                         offset_row ++;
219                 }
220                 if (rva_row <= rva->rows && (mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_FIELD) == i)) {
221                         fprintf (output, "\trva: %d\n", mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_RVA));
222                         rva_row ++;
223                 }
224         }
225         fprintf (output, "\n");
226 }
227
228 void
229 dump_table_memberref (MonoImage *m)
230 {
231         MonoTableInfo *t = &m->tables [MONO_TABLE_MEMBERREF];
232         int i, kind, idx;
233         char *x, *xx;
234         char *sig;
235         const char *blob, *ks = NULL;
236
237         fprintf (output, "MemberRef Table (1..%d)\n", t->rows);
238
239         for (i = 0; i < t->rows; i++){
240                 guint32 cols [MONO_MEMBERREF_SIZE];
241
242                 mono_metadata_decode_row (t, i, cols, MONO_MEMBERREF_SIZE);
243                 
244                 kind = cols [MONO_MEMBERREF_CLASS] & 7;
245                 idx = cols [MONO_MEMBERREF_CLASS] >> 3;
246
247                 x = g_strdup ("UNHANDLED CASE");
248                 
249                 switch (kind){
250                 case 0:
251                         ks = "TypeDef";
252                         xx = get_typedef (m, idx);
253                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
254                         g_free (xx);
255                         break;
256                 case 1:
257                         ks = "TypeRef";
258                         xx = get_typeref (m, idx);
259                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
260                         g_free (xx);
261                         break;
262                 case 2:
263                         ks = "ModuleRef"; break;
264                 case 3:
265                         ks = "MethodDef";
266                         x = get_methoddef (m, idx);
267                         break;
268                 case 4:
269                         ks = "TypeSpec";
270                         xx = get_typespec (m, idx, FALSE, NULL);
271                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
272                         g_free (xx);
273                         break;
274                 default:
275                         g_error ("Unknown tag: %d\n", kind);
276                 }
277                 blob = mono_metadata_blob_heap (m, cols [MONO_MEMBERREF_SIGNATURE]);
278                 mono_metadata_decode_blob_size (blob, &blob);
279                 if (*blob == 0x6) { /* it's a field */
280                         sig = get_field_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
281                 } else {
282                         sig = get_methodref_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
283                 }
284                 fprintf (output, "%d: %s[%d] %s\n\tResolved: %s\n\tSignature: %s\n\t\n",
285                          i + 1,
286                          ks, idx,
287                          mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]),
288                          x ? x : "",
289                          sig);
290
291                 if (x)
292                         g_free (x);
293                 g_free (sig);
294         }
295 }
296
297 void
298 dump_table_class_layout (MonoImage *m)
299 {
300         MonoTableInfo *t = &m->tables [MONO_TABLE_CLASSLAYOUT];
301         int i;
302         fprintf (output, "ClassLayout Table (1..%d)\n", t->rows);
303
304         for (i = 0; i < t->rows; i++){
305                 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
306                 
307                 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
308
309                 fprintf (output, "%d: PackingSize=%d  ClassSize=%d  Parent=%s\n",
310                          i + 1, cols [MONO_CLASS_LAYOUT_PACKING_SIZE], 
311                          cols [MONO_CLASS_LAYOUT_CLASS_SIZE], 
312                          get_typedef (m, cols [MONO_CLASS_LAYOUT_PARENT]));
313         }
314 }
315
316 void
317 dump_table_constant (MonoImage *m)
318 {
319         MonoTableInfo *t = &m->tables [MONO_TABLE_CONSTANT];
320         int i;
321         const char *desc [] = {
322                 "Field",
323                 "Param",
324                 "Property",
325                 ""
326         };
327         fprintf (output, "Constant Table (1..%d)\n", t->rows);
328
329         for (i = 0; i < t->rows; i++){
330                 guint32 cols [MONO_CONSTANT_SIZE];
331                 const char *parent;
332                 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
333                 parent = desc [cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK];
334
335                 fprintf (output, "%d: Parent= %s: %d %s\n",
336                          i + 1, parent, cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS, 
337                          get_constant (m, (MonoTypeEnum) cols [MONO_CONSTANT_TYPE], cols [MONO_CONSTANT_VALUE]));
338         }
339         
340 }
341
342 void
343 dump_table_property_map (MonoImage *m)
344 {
345         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTYMAP];
346         int i;
347         char *s;
348         
349         fprintf (output, "Property Map Table (1..%d)\n", t->rows);
350         
351         for (i = 0; i < t->rows; i++){
352                 guint32 cols [MONO_PROPERTY_MAP_SIZE];
353                 
354                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_MAP_SIZE);
355                 s = get_typedef (m, cols [MONO_PROPERTY_MAP_PARENT]);
356                 fprintf (output, "%d: %s (%d) %d\n", i + 1, s, cols [MONO_PROPERTY_MAP_PARENT], cols [MONO_PROPERTY_MAP_PROPERTY_LIST]);
357                 g_free (s);
358         }
359 }
360
361 void
362 dump_table_property (MonoImage *m)
363 {
364         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTY];
365         int i, j, pcount;
366         const char *ptr;
367         char flags[128];
368
369         fprintf (output, "Property Table (1..%d)\n", t->rows);
370
371         for (i = 0; i < t->rows; i++){
372                 guint32 cols [MONO_PROPERTY_SIZE];
373                 char *type;
374                 int prop_flags;
375                 
376                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_SIZE);
377                 flags [0] = 0;
378                 prop_flags = cols [MONO_PROPERTY_FLAGS];
379                 if (prop_flags & 0x0200)
380                         strcat (flags, "special ");
381                 if (prop_flags & 0x0400)
382                         strcat (flags, "runtime ");
383                 if (prop_flags & 0x1000)
384                         strcat (flags, "hasdefault ");
385
386                 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
387                 /* bsize = */ mono_metadata_decode_blob_size (ptr, &ptr);
388                 /* ECMA claims 0x08 ... */
389                 if (*ptr != 0x28 && *ptr != 0x08)
390                         g_warning("incorrect signature in propert blob: 0x%x", *ptr);
391                 ptr++;
392                 pcount = mono_metadata_decode_value (ptr, &ptr);
393                 ptr = get_type (m, ptr, &type, FALSE, NULL);
394                 fprintf (output, "%d: %s %s (",
395                          i + 1, type, mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]));
396                 g_free (type);
397
398                 for (j = 0; j < pcount; j++){
399                         ptr = get_param (m, ptr, &type, NULL);
400                         fprintf (output, "%s%s", j > 0? ", " : "",type);
401                         g_free (type);
402                 }
403                 fprintf (output, ") %s\n", flags);
404         }
405 }
406
407 void
408 dump_table_event (MonoImage *m)
409 {
410         MonoTableInfo *t = &m->tables [MONO_TABLE_EVENT];
411         int i;
412         fprintf (output, "Event Table (1..%d)\n", t->rows);
413
414         for (i = 0; i < t->rows; i++){
415                 guint32 cols [MONO_EVENT_SIZE];
416                 const char *name;
417                 char *type;
418                 
419                 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
420
421                 name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
422                 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE], NULL);
423                 fprintf (output, "%d: %s %s %s\n", i + 1, type, name,
424                          cols [MONO_EVENT_FLAGS] & 0x200 ? "specialname " : "");
425                 g_free (type);
426         }
427         
428 }
429
430 void
431 dump_table_file (MonoImage *m)
432 {
433         MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
434         int i, j, len;
435         fprintf (output, "File Table (1..%d)\n", t->rows);
436
437         for (i = 0; i < t->rows; i++){
438                 guint32 cols [MONO_FILE_SIZE];
439                 const char *name, *hash;
440                 
441                 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
442
443                 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
444                 fprintf (output, "%d: %s %s [", i + 1, name, 
445                                 cols [MONO_FILE_FLAGS] & 0x1 ? "nometadata" : "containsmetadata");
446                 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
447                 len = mono_metadata_decode_blob_size (hash, &hash);
448                 for (j = 0; j < len; ++j)
449                         fprintf (output, "%s%02X", j? " ": "", hash [j] & 0xff);
450                 fprintf (output, "]\n");
451         }
452         
453 }
454
455 static char*
456 get_manifest_implementation (MonoImage *m, guint32 idx)
457 {
458         guint32 row;
459         const char* table = "";
460         if (!idx)
461                 return g_strdup ("current module");
462         row = idx >> MONO_IMPLEMENTATION_BITS;
463         switch (idx & MONO_IMPLEMENTATION_MASK) {
464         case MONO_IMPLEMENTATION_FILE:
465                 table = "file";
466                 break;
467         case MONO_IMPLEMENTATION_ASSEMBLYREF:
468                 table = "assemblyref";
469                 break;
470         case MONO_IMPLEMENTATION_EXP_TYPE:
471                 table = "exportedtype";
472                 break;
473         default:
474                 g_assert_not_reached ();
475         }
476         return g_strdup_printf ("%s %d", table, row);
477 }
478
479 static const char*
480 get_manifest_flags (guint32 mf)
481 {
482         mf &= 3;
483         switch (mf) {
484         case 1: return "public";
485         case 2: return "private";
486         default:
487                 return "";
488         }
489 }
490
491 void
492 dump_table_manifest (MonoImage *m)
493 {
494         MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
495         int i;
496         fprintf (output, "Manifestresource Table (1..%d)\n", t->rows);
497
498         for (i = 0; i < t->rows; i++){
499                 guint32 cols [MONO_MANIFEST_SIZE];
500                 const char *name, *mf;
501                 char *impl;
502                 
503                 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
504
505                 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
506                 mf = get_manifest_flags (cols [MONO_MANIFEST_FLAGS]);
507                 impl = get_manifest_implementation (m, cols [MONO_MANIFEST_IMPLEMENTATION]);
508                 fprintf (output, "%d: %s '%s' at offset %u in %s\n", i + 1, mf, name, cols [MONO_MANIFEST_OFFSET], impl);
509                 g_free (impl);
510         }
511         
512 }
513
514 void
515 dump_table_moduleref (MonoImage *m)
516 {
517         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
518         int i;
519         fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
520
521         for (i = 0; i < t->rows; i++){
522                 guint32 cols [MONO_MODULEREF_SIZE];
523                 const char *name;
524                 
525                 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
526
527                 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
528                 fprintf (output, "%d: %s\n", i + 1, name);
529         }
530         
531 }
532
533 void
534 dump_table_module (MonoImage *m)
535 {
536         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
537         int i;
538         fprintf (output, "Module Table (1..%d)\n", t->rows);
539
540         for (i = 0; i < t->rows; i++){
541                 guint32 cols [MONO_MODULE_SIZE];
542                 const char *name;
543                 char *guid;
544                 
545                 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
546
547                 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
548                 guid = get_guid (m, cols [MONO_MODULE_MVID]);
549                 fprintf (output, "%d: %s %d %s\n", i + 1, name, cols [MONO_MODULE_MVID], guid);
550         }       
551 }
552
553 void
554 dump_table_method (MonoImage *m)
555 {
556         MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
557         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
558         int i, current_type;
559         guint32 first_m, last_m;
560         /* Generic container for Type & method */
561         MonoGenericContainer *type_container = NULL, *method_container = NULL;
562
563         fprintf (output, "Method Table (1..%d)\n", t->rows);
564
565         current_type = 1;
566         last_m = first_m = 1;
567         for (i = 1; i <= t->rows; i++){
568                 MonoError error;
569                 guint32 cols [MONO_METHOD_SIZE];
570                 char *sig, *impl_flags;
571                 const char *sigblob;
572                 MonoMethodSignature *method;
573
574                 /*
575                  * Find the next type.
576                  */
577                 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_METHOD_LIST))) {
578                         current_type++;
579                 }
580                 if (i == first_m) {
581                         fprintf (output, "########## %s.%s\n",
582                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
583                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
584                         first_m = last_m;
585                         type_container = mono_metadata_load_generic_params (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), NULL);
586                         if (type_container) {
587                                 mono_metadata_load_generic_param_constraints_checked (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), type_container, &error);
588                                 g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
589                         }
590                 }
591
592                 method_container = mono_metadata_load_generic_params (m, MONO_TOKEN_METHOD_DEF | i, type_container);
593                 if (method_container) {
594                         mono_metadata_load_generic_param_constraints_checked (m, MONO_TOKEN_METHOD_DEF | i, method_container, &error);
595                         g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
596                 }
597                 mono_metadata_decode_table_row (m, MONO_TABLE_METHOD, i - 1, cols, MONO_METHOD_SIZE);
598                 sigblob = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
599                 mono_metadata_decode_blob_size (sigblob, &sigblob);
600                 method = mono_metadata_parse_method_signature_full (m, method_container ? method_container : type_container, i, sigblob, &sigblob, &error);
601                 if (!mono_error_ok (&error)) {
602                         fprintf (output,"%d: failed to parse due to %s\n", i, mono_error_get_message (&error));
603                         mono_error_cleanup (&error);
604                         continue;
605                 }
606
607                 g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
608                 sig = dis_stringify_method_signature (m, method, i, method_container ? method_container : type_container, FALSE);
609                 impl_flags = get_method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
610                 fprintf (output, "%d: %s (param: %d impl_flags: %s)\n", i, sig, cols [MONO_METHOD_PARAMLIST], impl_flags);
611                 g_free (sig);
612                 g_free (impl_flags);
613                 mono_metadata_free_method_signature (method);
614         }
615         
616 }
617
618 void
619 dump_table_implmap (MonoImage *m)
620 {
621         MonoTableInfo *t = &m->tables [MONO_TABLE_IMPLMAP];
622         MonoTableInfo *td = &m->tables [MONO_TABLE_MODULEREF];
623         int i;
624
625         fprintf (output, "ImplMap Table (1..%d)\n", t->rows);
626
627         for (i = 1; i <= t->rows; i++){
628                 guint32 cols [MONO_IMPLMAP_SIZE];
629                 char *method;
630
631                 mono_metadata_decode_row (t, i - 1, cols, MONO_IMPLMAP_SIZE);
632
633                 method = get_method (m, MONO_TOKEN_METHOD_DEF | (cols [MONO_IMPLMAP_MEMBER] >> MONO_MEMBERFORWD_BITS), NULL);
634                 
635                 fprintf (output, "%d: %s %d (%s %s)\n", i, 
636                                  method,
637                                  cols [MONO_IMPLMAP_FLAGS], 
638                                  mono_metadata_string_heap (m, cols [MONO_IMPLMAP_NAME]),
639                                  mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME)));
640         }
641 }
642
643 void
644 dump_table_fieldrva  (MonoImage *m)
645 {
646         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
647         int i;
648
649         fprintf (output, "FieldRVA Table (1..%d)\n", t->rows);
650
651         for (i = 1; i <= t->rows; i++){
652                 guint32 cols [MONO_FIELD_RVA_SIZE];
653
654                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_RVA_SIZE);
655                 fprintf (output, "%d: Field %d: %x\n", i, cols [MONO_FIELD_RVA_FIELD], cols [MONO_FIELD_RVA_RVA]);
656         }
657 }
658
659 void
660 dump_table_methodimpl (MonoImage *m)
661 {
662         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODIMPL];
663         /*MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];*/
664         int i;
665
666         fprintf (output, "MethodImpl Table (1..%d)\n", t->rows);
667
668         for (i = 1; i <= t->rows; i++){
669                 guint32 cols [MONO_METHODIMPL_SIZE];
670                 char *klass, *impl, *decl;
671
672                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODIMPL_SIZE);
673                 klass = get_typedef (m, cols [MONO_METHODIMPL_CLASS]);
674                 impl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_BODY]), NULL);
675                 decl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_DECLARATION]), NULL);
676                 fprintf (output, "%d: %s\n\tdecl: %s\n\timpl: %s\n", i, klass, decl, impl);
677                 g_free (klass);
678                 g_free (impl);
679                 g_free (decl);
680         }
681         
682 }
683
684 static dis_map_t semantics_map [] = {
685                 {1, "setter"},
686                 {2, "getter"},
687                 {4, "other"},
688                 {8, "add-on"},
689                 {0x10, "remove-on"},
690                 {0x20, "fire"},
691                 {0, NULL},
692 };
693
694 void
695 dump_table_methodsem (MonoImage *m)
696 {
697         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSEMANTICS];
698         int i, is_property, index;
699         const char *semantics;
700         
701         fprintf (output, "Method Semantics Table (1..%d)\n", t->rows);
702         for (i = 1; i <= t->rows; i++){
703                 guint32 cols [MONO_METHOD_SEMA_SIZE];
704                 
705                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SEMA_SIZE);
706                 semantics = flags (cols [MONO_METHOD_SEMA_SEMANTICS], semantics_map);
707                 is_property = cols [MONO_METHOD_SEMA_ASSOCIATION] & MONO_HAS_SEMANTICS_MASK;
708                 index = cols [MONO_METHOD_SEMA_ASSOCIATION] >> MONO_HAS_SEMANTICS_BITS;
709                 fprintf (output, "%d: [%d] %s method: %d %s %d\n", i, cols [MONO_METHOD_SEMA_ASSOCIATION], semantics,
710                                                 cols [MONO_METHOD_SEMA_METHOD] - 1, 
711                                                 is_property? "property" : "event",
712                                                 index);
713         }
714 }
715
716 void 
717 dump_table_interfaceimpl (MonoImage *m)
718 {
719         MonoTableInfo *t = &m->tables [MONO_TABLE_INTERFACEIMPL];
720         int i;
721
722         fprintf (output, "Interface Implementation Table (1..%d)\n", t->rows);
723         for (i = 1; i <= t->rows; i++) {
724                 guint32 cols [MONO_INTERFACEIMPL_SIZE];
725                 
726                 mono_metadata_decode_row (t, i - 1, cols, MONO_INTERFACEIMPL_SIZE);
727                 fprintf (output, "%d: %s implements %s\n", i,
728                          get_typedef (m, cols [MONO_INTERFACEIMPL_CLASS]),
729                          get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE], NULL));
730         }
731 }
732
733 static char*
734 has_cattr_get_table (MonoImage *m, guint32 val)
735 {
736         guint32 t = val & MONO_CUSTOM_ATTR_MASK;
737         guint32 index = val >> MONO_CUSTOM_ATTR_BITS;
738         const char *table;
739
740         switch (t) {
741         case MONO_CUSTOM_ATTR_METHODDEF:
742                 table = "MethodDef";
743                 break;
744         case MONO_CUSTOM_ATTR_FIELDDEF:
745                 table = "FieldDef";
746                 break;
747         case MONO_CUSTOM_ATTR_TYPEREF:
748                 table = "TypeRef";
749                 break;
750         case MONO_CUSTOM_ATTR_TYPEDEF:
751                 table = "TypeDef";
752                 break;
753         case MONO_CUSTOM_ATTR_PARAMDEF:
754                 table = "Param";
755                 break;
756         case MONO_CUSTOM_ATTR_INTERFACE:
757                 table = "InterfaceImpl";
758                 break;
759         case MONO_CUSTOM_ATTR_MEMBERREF:
760                 table = "MemberRef";
761                 break;
762         case MONO_CUSTOM_ATTR_MODULE:
763                 table = "Module";
764                 break;
765         case MONO_CUSTOM_ATTR_PERMISSION:
766                 table = "DeclSecurity?";
767                 break;
768         case MONO_CUSTOM_ATTR_PROPERTY:
769                 table = "Property";
770                 break;
771         case MONO_CUSTOM_ATTR_EVENT:
772                 table = "Event";
773                 break;
774         case MONO_CUSTOM_ATTR_SIGNATURE:
775                 table = "StandAloneSignature";
776                 break;
777         case MONO_CUSTOM_ATTR_MODULEREF:
778                 table = "ModuleRef";
779                 break;
780         case MONO_CUSTOM_ATTR_TYPESPEC:
781                 table = "TypeSpec";
782                 break;
783         case MONO_CUSTOM_ATTR_ASSEMBLY:
784                 table = "Assembly";
785                 break;
786         case MONO_CUSTOM_ATTR_ASSEMBLYREF:
787                 table = "AssemblyRef";
788                 break;
789         case MONO_CUSTOM_ATTR_FILE:
790                 table = "File";
791                 break;
792         case MONO_CUSTOM_ATTR_EXP_TYPE:
793                 table = "ExportedType";
794                 break;
795         case MONO_CUSTOM_ATTR_MANIFEST:
796                 table = "Manifest";
797                 break;
798         case MONO_CUSTOM_ATTR_GENERICPAR:
799                 table = "GenericParam";
800                 break;
801         default:
802                 table = "Unknown";
803                 break;
804         }
805         /*
806          * FIXME: we should decode the index into something more uman-friendly.
807          */
808         return g_strdup_printf ("%s: %d", table, index);
809 }
810
811 static char*
812 custom_attr_params (MonoImage *m, MonoMethodSignature* sig, const char* value)
813 {
814         int len, i, slen, type;
815         GString *res;
816         char *s;
817         const char *p = value;
818
819         len = mono_metadata_decode_value (p, &p);
820         if (len < 2 || read16 (p) != 0x0001) /* Prolog */
821                 return g_strdup ("");
822
823         /* skip prolog */
824         p += 2;
825         res = g_string_new ("");
826         for (i = 0; i < sig->param_count; ++i) {
827                 if (i != 0)
828                         g_string_append (res, ", ");
829                 type = sig->params [i]->type;
830 handle_enum:
831                 switch (type) {
832                 case MONO_TYPE_U1:
833                         g_string_append_printf (res, "%d", (unsigned int)*p);
834                         ++p;
835                         break;
836                 case MONO_TYPE_I1:
837                         g_string_append_printf (res, "%d", *p);
838                         ++p;
839                         break;
840                 case MONO_TYPE_BOOLEAN:
841                         g_string_append_printf (res, "%s", *p?"true":"false");
842                         ++p;
843                         break;
844                 case MONO_TYPE_CHAR:
845                         g_string_append_printf (res, "'%c'", read16 (p));
846                         p += 2;
847                         break;
848                 case MONO_TYPE_U2:
849                         g_string_append_printf (res, "%d", read16 (p));
850                         p += 2;
851                         break;
852                 case MONO_TYPE_I2:
853                         g_string_append_printf (res, "%d", (gint16)read16 (p));
854                         p += 2;
855                         break;
856                 case MONO_TYPE_U4:
857                         g_string_append_printf (res, "%d", read32 (p));
858                         p += 4;
859                         break;
860                 case MONO_TYPE_I4:
861                         g_string_append_printf (res, "%d", (gint32)read32 (p));
862                         p += 4;
863                         break;
864                 case MONO_TYPE_U8:
865                         g_string_append_printf (res, "%lld", (long long)read64 (p));
866                         p += 8;
867                         break;
868                 case MONO_TYPE_I8:
869                         g_string_append_printf (res, "%lld", (long long)read64 (p));
870                         p += 8;
871                         break;
872                 case MONO_TYPE_R4: {
873                         float val;
874                         int inf;
875                         readr4 (p, &val);
876                         inf = dis_isinf (val);
877                         if (inf == -1) 
878                                 g_string_append_printf (res, "(00 00 80 ff)"); /* negative infinity */
879                         else if (inf == 1)
880                                 g_string_append_printf (res, "(00 00 80 7f)"); /* positive infinity */
881                         else if (dis_isnan (val))
882                                 g_string_append_printf (res, "(00 00 c0 ff)"); /* NaN */
883                         else
884                                 g_string_append_printf (res, "%g", val);
885                         p += 4;
886                         break;
887                 }
888                 case MONO_TYPE_R8: {
889                         double val;
890                         int inf;
891                         
892                         readr8 (p, &val);
893                         inf = dis_isinf (val);
894                         if (inf == -1) 
895                                 g_string_append_printf (res, "(00 00 00 00 00 00 f0 ff)"); /* negative infinity */
896                         else if (inf == 1)
897                                 g_string_append_printf (res, "(00 00 00 00 00 00 f0 7f)"); /* positive infinity */
898                         else if (isnan (val))
899                                 g_string_append_printf (res, "(00 00 00 00 00 00 f8 ff)"); /* NaN */
900                         else
901                                 g_string_append_printf (res, "%g", val);
902                         p += 8;
903                         break;
904                 }
905                 case MONO_TYPE_VALUETYPE:
906                         if (mono_class_is_enum (sig->params [i]->data.klass)) {
907                                 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
908                                 goto handle_enum;
909                         } else {
910                                 g_warning ("generic valutype not handled in custom attr value decoding");
911                         }
912                         break;
913                 case MONO_TYPE_CLASS: /* It must be a Type: check? */
914                 case MONO_TYPE_STRING:
915                         if (*p == (char)0xff) {
916                                 g_string_append (res, "null");
917                                 p++;
918                                 break;
919                         }
920                         slen = mono_metadata_decode_value (p, &p);
921                         g_string_append_c (res, '"');
922                         g_string_append (res, p);
923                         g_string_append_c (res, '"');
924                         p += slen;
925                         break;
926                 default:
927                         g_warning ("Type %02x not handled in custom attr value decoding", sig->params [i]->type);
928                         break;
929                 }
930         }
931         slen = read16 (p);
932         if (slen) {
933                 g_string_append_printf (res, " %d named args: (", slen);
934                 slen = len - (p - value) + 1;
935                 for (i = 0; i < slen; ++i) {
936                         g_string_append_printf (res, " %02X", (p [i] & 0xff));
937                 }
938                 g_string_append_c (res, ')');
939         }
940         s = res->str;
941         g_string_free (res, FALSE);
942         return s;
943 }
944
945 void
946 dump_table_customattr (MonoImage *m)
947 {
948         MonoTableInfo *t = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
949         int i;
950
951         fprintf (output, "Custom Attributes Table (1..%d)\n", t->rows);
952         for (i = 1; i <= t->rows; i++) {
953                 MonoError error;
954                 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
955                 guint32 mtoken;
956                 char * desc;
957                 char *method;
958                 char *params;
959                 MonoMethod *meth;
960                 
961                 mono_metadata_decode_row (t, i - 1, cols, MONO_CUSTOM_ATTR_SIZE);
962                 desc = has_cattr_get_table (m, cols [MONO_CUSTOM_ATTR_PARENT]);
963                 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
964                 switch (cols [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
965                 case MONO_CUSTOM_ATTR_TYPE_METHODDEF:
966                         mtoken |= MONO_TOKEN_METHOD_DEF;
967                         break;
968                 case MONO_CUSTOM_ATTR_TYPE_MEMBERREF:
969                         mtoken |= MONO_TOKEN_MEMBER_REF;
970                         break;
971                 default:
972                         g_warning ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
973                         break;
974                 }
975                 method = get_method (m, mtoken, NULL);
976                 meth = mono_get_method_checked (m, mtoken, NULL, NULL, &error);
977                 if (meth) {
978                         params = custom_attr_params (m, mono_method_signature (meth), mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]));
979                         fprintf (output, "%d: %s: %s [%s]\n", i, desc, method, params);
980                         g_free (params);
981                 } else {
982                         fprintf (output, "Could not decode method due to %s", mono_error_get_message (&error));
983                         mono_error_cleanup (&error);
984                 }
985
986                 g_free (desc);
987                 g_free (method);
988         }
989 }
990
991 void
992 dump_table_nestedclass (MonoImage *m)
993 {
994         MonoTableInfo *t = &m->tables [MONO_TABLE_NESTEDCLASS];
995         guint32 cols [MONO_NESTED_CLASS_SIZE];
996         int i;
997         char *nested, *nesting;
998         fprintf (output, "NestedClass Table (1..%d)\n", t->rows);
999
1000         for (i = 1; i <= t->rows; i++){
1001                 mono_metadata_decode_row (t, i - 1, cols, MONO_NESTED_CLASS_SIZE);
1002                 nested = get_typedef (m, cols [MONO_NESTED_CLASS_NESTED]);
1003                 nesting = get_typedef (m, cols [MONO_NESTED_CLASS_ENCLOSING]);
1004                 fprintf (output, "%d: %d %d: %s in %s\n", i,
1005                                 cols [MONO_NESTED_CLASS_NESTED], 
1006                                 cols [MONO_NESTED_CLASS_ENCLOSING], nested, nesting);
1007                 g_free (nested);
1008                 g_free (nesting);
1009         }
1010         
1011 }
1012
1013 void
1014 dump_table_exported (MonoImage *m)
1015 {
1016         MonoTableInfo *t = &m->tables [MONO_TABLE_EXPORTEDTYPE];
1017         guint32 cols [MONO_EXP_TYPE_SIZE];
1018         int i;
1019         const char *name, *nspace;
1020         char *impl;
1021         guint32 index, flags;
1022         fprintf (output, "ExportedType Table (1..%d)\n", t->rows);
1023
1024         for (i = 1; i <= t->rows; i++) {
1025                 mono_metadata_decode_row (t, i - 1, cols, MONO_EXP_TYPE_SIZE);
1026                 name = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAME]);
1027                 nspace = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAMESPACE]);
1028                 impl = get_manifest_implementation (m, cols [MONO_EXP_TYPE_IMPLEMENTATION]);
1029                 index = cols [MONO_EXP_TYPE_TYPEDEF];
1030                 flags = cols [MONO_EXP_TYPE_FLAGS];
1031                 fprintf (output, "%d: %s%s%s is in %s, index=%x, flags=0x%x\n", i, nspace, *nspace ? "." : "", name, impl, index, flags);
1032                 g_free (impl);
1033         }
1034         
1035 }
1036
1037 static void
1038 dump_blob (MonoImage *m, const char* blob)
1039 {
1040         int j, bsize;
1041
1042         bsize = mono_metadata_decode_blob_size (blob, &blob);
1043
1044         for (j = 0; j < bsize; j++) {
1045                 fprintf (output, "%02x ", blob [j] & 0xff);
1046         }
1047 }
1048
1049 void
1050 dump_table_field_marshal (MonoImage *m)
1051 {
1052         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDMARSHAL];
1053         guint32 cols [MONO_FIELD_MARSHAL_SIZE];
1054         int i, is_field, idx;
1055         const char *blob;
1056         char *native;
1057         
1058         fprintf (output, "FieldMarshal Table (1..%d)\n", t->rows);
1059
1060         for (i = 1; i <= t->rows; i++) {
1061                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_MARSHAL_SIZE);
1062                 blob = mono_metadata_blob_heap (m, cols [MONO_FIELD_MARSHAL_NATIVE_TYPE]);
1063                 native = get_marshal_info (m, blob);
1064                 is_field = (cols [MONO_FIELD_MARSHAL_PARENT] & MONO_HAS_FIELD_MARSHAL_MASK) == MONO_HAS_FIELD_MARSHAL_FIELDSREF;
1065                 idx = cols [MONO_FIELD_MARSHAL_PARENT] >> MONO_HAS_FIELD_MARSHAL_BITS;
1066                 fprintf (output, "%d: (0x%04x) %s %d: %s\n", i, cols [MONO_FIELD_MARSHAL_PARENT], is_field? "Field" : "Param", idx, native);
1067                 fprintf (output, "\tblob encoding: ");
1068                 dump_blob (m, blob);
1069                 fprintf (output, "\n");
1070                 g_free (native);
1071         }
1072         
1073 }
1074
1075 static const char*
1076 get_security_action (int val) {
1077         static char buf [32];
1078
1079         switch (val) {
1080         case SECURITY_ACTION_DEMAND:
1081                 return "Demand";
1082         case SECURITY_ACTION_ASSERT:
1083                 return "Assert";
1084         case SECURITY_ACTION_DENY:
1085                 return "Deny";
1086         case SECURITY_ACTION_PERMITONLY:
1087                 return "PermitOnly";
1088         case SECURITY_ACTION_LINKDEMAND:
1089                 return "LinkDemand";
1090         case SECURITY_ACTION_INHERITDEMAND:
1091                 return "InheritanceDemand";
1092         case SECURITY_ACTION_REQMIN:
1093                 return "RequestMinimum";
1094         case SECURITY_ACTION_REQOPT:
1095                 return "RequestOptional";
1096         case SECURITY_ACTION_REQREFUSE:
1097                 return "RequestRefuse";
1098         /* Special actions (for non CAS permissions) */
1099         case SECURITY_ACTION_NONCASDEMAND:
1100                 return "NonCasDemand";
1101         case SECURITY_ACTION_NONCASLINKDEMAND:
1102                 return "NonCasLinkDemand";
1103         case SECURITY_ACTION_NONCASINHERITANCE:
1104                 return "NonCasInheritance";
1105         /* Fx 2.0 actions (for both CAS and non-CAS permissions) */
1106         case SECURITY_ACTION_LINKDEMANDCHOICE:
1107                 return "LinkDemandChoice";
1108         case SECURITY_ACTION_INHERITDEMANDCHOICE:
1109                 return "InheritanceDemandChoice";
1110         case SECURITY_ACTION_DEMANDCHOICE:
1111                 return "DemandChoice";
1112         default:
1113                 g_snprintf (buf, sizeof (buf), "0x%04X", val);
1114                 return buf;
1115         }
1116 }
1117
1118 void 
1119 dump_table_declsec (MonoImage *m)
1120 {
1121         MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
1122         guint32 cols [MONO_DECL_SECURITY_SIZE];
1123         int i, len;
1124         guint32 idx;
1125         const char *blob, *action;
1126         const char* parent[] = {
1127                 "TypeDef", "MethodDef", "Assembly", ""
1128         };
1129         
1130         fprintf (output, "DeclSecurity Table (1..%d)\n", t->rows);
1131
1132         for (i = 1; i <= t->rows; i++) {
1133                 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
1134                 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
1135                 len = mono_metadata_decode_blob_size (blob, &blob);
1136                 action = get_security_action (cols [MONO_DECL_SECURITY_ACTION]);
1137                 idx = cols [MONO_DECL_SECURITY_PARENT];
1138                 fprintf (output, "%d: %s on %s %d%s", i, action, parent [idx & MONO_HAS_DECL_SECURITY_MASK], idx >> MONO_HAS_DECL_SECURITY_BITS, len? ":\n\t":"\n");
1139                 if (!len)
1140                         continue;
1141                 if (blob [0] == MONO_DECLSEC_FORMAT_20) {
1142                         /* 2.0 declarative security format */
1143                         char *declsec = dump_declsec_entry20 (m, blob, "\t");
1144                         fprintf (output, "%s", declsec);
1145                         g_free (declsec);
1146                 } else {
1147                         /* 1.0 declarative security format - Unicode XML */
1148                         for (idx = 0; idx < len; ++idx)
1149                                 fprintf (output, "%c", blob [idx]);
1150                 }
1151                 fprintf (output, "\n");
1152         }
1153 }
1154
1155 void 
1156 dump_table_genericpar (MonoImage *m)
1157 {
1158         MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
1159         guint32 cols [MONO_GENERICPARAM_SIZE];
1160         int i;
1161
1162         fprintf (output, "GenericParameters (1..%d)\n", t->rows);
1163         
1164         for (i = 1; i <= t->rows; i++) {
1165                 char *sig;
1166                 mono_metadata_decode_row (t, i - 1, cols, MONO_GENERICPARAM_SIZE);
1167
1168                 // sig = get_type_or_methdef (m, cols [MONO_GENERICPARAM_OWNER]);
1169                 sig = g_strdup_printf ("%x", cols [MONO_GENERICPARAM_OWNER]);
1170                 fprintf (output, "%d: %d, flags=%d, owner=%s %s\n", i,
1171                          cols [MONO_GENERICPARAM_NUMBER],
1172                          cols [MONO_GENERICPARAM_FLAGS], sig,
1173                          mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
1174                 g_free (sig);
1175         }
1176 }
1177
1178 void
1179 dump_table_methodspec (MonoImage *m)
1180 {
1181         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSPEC];
1182         guint32 cols [MONO_METHODSPEC_SIZE];
1183         int i;
1184         
1185         fprintf (output, "MethodSpec (1..%d)\n", t->rows);
1186
1187         for (i = 1; i <= t->rows; i++) {
1188                 char *sig;
1189                 char *method;
1190                 guint32 token;
1191                 
1192                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODSPEC_SIZE);
1193
1194                 /* build a methodspec token to get the method */
1195                 token = MONO_TOKEN_METHOD_SPEC | i;
1196                 method = get_method (m, token, NULL);
1197                 
1198                 sig = get_method_type_param (m, cols [MONO_METHODSPEC_SIGNATURE], NULL);
1199                 fprintf (output, "%d: %s, %s\n", i, method, sig);
1200                 g_free (sig);
1201                 g_free (method);
1202         }
1203 }
1204
1205 void
1206 dump_table_parconstraint (MonoImage *m)
1207 {
1208         MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
1209         guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
1210         int i;
1211         
1212         fprintf (output, "Generic Param Constraint (1..%d)\n", t->rows);
1213
1214         for (i = 1; i <= t->rows; i++) {
1215                 char *sig;
1216                 mono_metadata_decode_row (t, i - 1, cols, MONO_GENPARCONSTRAINT_SIZE);
1217
1218                 // sig = get_typedef_or_ref (m, cols [MONO_GENPARCONSTRAINT_CONSTRAINT], NULL);
1219                 sig = g_strdup_printf ("%x", cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
1220                 fprintf (output, "%d: gen-par=%d, Constraint=%s\n", i,
1221                          cols [MONO_GENPARCONSTRAINT_GENERICPAR], sig);
1222                 g_free (sig);
1223         }
1224 }
1225
1226 void
1227 dump_stream_blob (MonoImage *m)
1228 {
1229         int i;
1230
1231         fprintf (output, "Blob heap contents\n");
1232
1233         for (i = 0; i < m->heap_blob.size; i++) {
1234                 if (i > 0) {
1235                         if ((i % 16) == 0)
1236                                 fprintf (output, "\n");
1237                         else if ((i % 8) == 0)
1238                                 fprintf (output, "- ");
1239                 }
1240                 fprintf (output, "%02x ", m->heap_blob.data [i] & 0xff);
1241         }
1242
1243         fprintf (output, "\n");
1244 }
1245
1246 void
1247 dump_stream_strings (MonoImage *m)
1248 {
1249         guint32 i;
1250
1251         fprintf (output, "Strings heap contents\n");
1252
1253         for (i = 0; i < m->heap_strings.size; ) {
1254                 const char *str = mono_metadata_string_heap (m, i);
1255                 fprintf (output, "%02x: \"%s\"\n", i, str);
1256                 i += strlen (str) + 1;
1257         }
1258 }
1259
1260 void
1261 dump_stream_us (MonoImage *m)
1262 {
1263         guint32 i;
1264
1265         fprintf (output, "User Strings heap contents\n");
1266
1267         for (i = 0; i < m->heap_us.size; ) {
1268                 const char *us_ptr = mono_metadata_user_string (m, i);
1269                 int len = mono_metadata_decode_blob_size (us_ptr, (const char**)&us_ptr);
1270
1271                 char *str = get_encoded_user_string_or_bytearray ((const guchar*)us_ptr, len);
1272                 fprintf (output, "%02x: %s\n", i, str);
1273                 g_free (str);
1274                 i += len + 1;
1275         }
1276 }
1277
1278 void
1279 dump_table_standalonesig (MonoImage *m)
1280 {
1281         MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1282         guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1283         int i;
1284         
1285         fprintf (output, "Stand alone signature (1..%d)\n", t->rows);
1286
1287         for (i = 1; i <= t->rows; i++) {
1288                 const char *locals_ptr;
1289                 int j, bsize;
1290
1291                 mono_metadata_decode_row (t, i - 1, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1292
1293                 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1294                 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1295
1296                 fprintf (output, "%d: blob[0x%x] = ", i, cols [MONO_STAND_ALONE_SIGNATURE]);
1297
1298                 for (j = 0; j < bsize; j++) {
1299                         fprintf (output, "%02x ", locals_ptr [j] & 0xff);
1300                 }
1301                 fprintf (output, "\n");
1302         }
1303 }
1304
1305 static void
1306 dump_table_ptr (MonoImage *m, int table, const char *name)
1307 {
1308         MonoTableInfo *t = &m->tables [table];
1309         guint32 cols [1];
1310         int i;
1311         
1312         fprintf (output, "%s (1..%d)\n", name, t->rows);
1313
1314         for (i = 1; i <= t->rows; i++) {
1315                 mono_metadata_decode_row (t, i - 1, cols, 1);
1316
1317                 fprintf (output, "%d: %d\n", i, cols [0]);
1318         }
1319 }
1320
1321 void
1322 dump_table_methodptr (MonoImage *m)
1323 {
1324         dump_table_ptr (m, MONO_TABLE_METHOD_POINTER, "Method Ptr");
1325 }
1326
1327 void
1328 dump_table_fieldptr (MonoImage *m)
1329 {
1330         dump_table_ptr (m, MONO_TABLE_FIELD_POINTER, "Field Ptr");
1331 }
1332
1333 void
1334 dump_table_paramptr (MonoImage *m)
1335 {
1336         dump_table_ptr (m, MONO_TABLE_PARAM_POINTER, "Param Ptr");
1337 }
1338
1339 void
1340 dump_table_eventptr (MonoImage *m)
1341 {
1342         dump_table_ptr (m, MONO_TABLE_EVENT_POINTER, "Event Ptr");
1343 }
1344
1345 void
1346 dump_table_propertyptr (MonoImage *m)
1347 {
1348         dump_table_ptr (m, MONO_TABLE_PROPERTY_POINTER, "Property Ptr");
1349 }