Merge pull request #2687 from lambdageek/dev/monoerror-mono_param_get_objects_internal
[mono.git] / mono / metadata / debug-helpers.c
1 /*
2  * debug-helpers.c:
3  *
4  * Author:
5  *      Mono Project (http://www.mono-project.com)
6  *
7  * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
8  */
9
10 #include <string.h>
11 #include "mono/metadata/tokentype.h"
12 #include "mono/metadata/opcodes.h"
13 #include "mono/metadata/metadata-internals.h"
14 #include "mono/metadata/class-internals.h"
15 #include "mono/metadata/object-internals.h"
16 #include "mono/metadata/mono-endian.h"
17 #include "mono/metadata/debug-helpers.h"
18 #include "mono/metadata/tabledefs.h"
19 #include "mono/metadata/appdomain.h"
20
21 struct MonoMethodDesc {
22         char *name_space;
23         char *klass;
24         char *name;
25         char *args;
26         guint num_args;
27         gboolean include_namespace, klass_glob, name_glob;
28 };
29
30 #ifdef HAVE_ARRAY_ELEM_INIT
31 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
32 #define MSGSTRFIELD1(line) str##line
33 static const struct msgstr_t {
34 #define WRAPPER(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
35 #include "wrapper-types.h"
36 #undef WRAPPER
37 } opstr = {
38 #define WRAPPER(a,b) b,
39 #include "wrapper-types.h"
40 #undef WRAPPER
41 };
42 static const gint16 opidx [] = {
43 #define WRAPPER(a,b) [MONO_WRAPPER_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
44 #include "wrapper-types.h"
45 #undef WRAPPER
46 };
47
48 static const char*
49 wrapper_type_to_str (guint32 wrapper_type)
50 {
51         g_assert (wrapper_type < MONO_WRAPPER_NUM);
52
53         return (const char*)&opstr + opidx [wrapper_type];
54 }
55
56 #else
57 #define WRAPPER(a,b) b,
58 static const char* const
59 wrapper_type_names [MONO_WRAPPER_NUM + 1] = {
60 #include "wrapper-types.h"
61         NULL
62 };
63
64 static const char*
65 wrapper_type_to_str (guint32 wrapper_type)
66 {
67         g_assert (wrapper_type < MONO_WRAPPER_NUM);
68
69         return wrapper_type_names [wrapper_type];
70 }
71
72 #endif
73
74 static void
75 append_class_name (GString *res, MonoClass *klass, gboolean include_namespace)
76 {
77         if (!klass) {
78                 g_string_append (res, "Unknown");
79                 return;
80         }
81         if (klass->nested_in) {
82                 append_class_name (res, klass->nested_in, include_namespace);
83                 g_string_append_c (res, '/');
84         }
85         if (include_namespace && *(klass->name_space)) {
86                 g_string_append (res, klass->name_space);
87                 g_string_append_c (res, '.');
88         }
89         g_string_append (res, klass->name);
90 }
91
92 static MonoClass*
93 find_system_class (const char *name)
94 {
95         if (!strcmp (name, "void")) 
96                 return mono_defaults.void_class;
97         else if (!strcmp (name, "char")) return mono_defaults.char_class;
98         else if (!strcmp (name, "bool")) return mono_defaults.boolean_class;
99         else if (!strcmp (name, "byte")) return mono_defaults.byte_class;
100         else if (!strcmp (name, "sbyte")) return mono_defaults.sbyte_class;
101         else if (!strcmp (name, "uint16")) return mono_defaults.uint16_class;
102         else if (!strcmp (name, "int16")) return mono_defaults.int16_class;
103         else if (!strcmp (name, "uint")) return mono_defaults.uint32_class;
104         else if (!strcmp (name, "int")) return mono_defaults.int32_class;
105         else if (!strcmp (name, "ulong")) return mono_defaults.uint64_class;
106         else if (!strcmp (name, "long")) return mono_defaults.int64_class;
107         else if (!strcmp (name, "uintptr")) return mono_defaults.uint_class;
108         else if (!strcmp (name, "intptr")) return mono_defaults.int_class;
109         else if (!strcmp (name, "single")) return mono_defaults.single_class;
110         else if (!strcmp (name, "double")) return mono_defaults.double_class;
111         else if (!strcmp (name, "string")) return mono_defaults.string_class;
112         else if (!strcmp (name, "object")) return mono_defaults.object_class;
113         else
114                 return NULL;
115 }
116
117 void
118 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace)
119 {
120         int i;
121
122         switch (type->type) {
123         case MONO_TYPE_VOID:
124                 g_string_append (res, "void"); break;
125         case MONO_TYPE_CHAR:
126                 g_string_append (res, "char"); break;
127         case MONO_TYPE_BOOLEAN:
128                 g_string_append (res, "bool"); break;
129         case MONO_TYPE_U1:
130                 g_string_append (res, "byte"); break;
131         case MONO_TYPE_I1:
132                 g_string_append (res, "sbyte"); break;
133         case MONO_TYPE_U2:
134                 g_string_append (res, "uint16"); break;
135         case MONO_TYPE_I2:
136                 g_string_append (res, "int16"); break;
137         case MONO_TYPE_U4:
138                 g_string_append (res, "uint"); break;
139         case MONO_TYPE_I4:
140                 g_string_append (res, "int"); break;
141         case MONO_TYPE_U8:
142                 g_string_append (res, "ulong"); break;
143         case MONO_TYPE_I8:
144                 g_string_append (res, "long"); break;
145         case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
146                 g_string_append (res, "*()"); break;
147         case MONO_TYPE_U:
148                 g_string_append (res, "uintptr"); break;
149         case MONO_TYPE_I:
150                 g_string_append (res, "intptr"); break;
151         case MONO_TYPE_R4:
152                 g_string_append (res, "single"); break;
153         case MONO_TYPE_R8:
154                 g_string_append (res, "double"); break;
155         case MONO_TYPE_STRING:
156                 g_string_append (res, "string"); break;
157         case MONO_TYPE_OBJECT:
158                 g_string_append (res, "object"); break;
159         case MONO_TYPE_PTR:
160                 mono_type_get_desc (res, type->data.type, include_namespace);
161                 g_string_append_c (res, '*');
162                 break;
163         case MONO_TYPE_ARRAY:
164                 mono_type_get_desc (res, &type->data.array->eklass->byval_arg, include_namespace);
165                 g_string_append_printf (res, "[%d]", type->data.array->rank);
166                 break;
167         case MONO_TYPE_SZARRAY:
168                 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
169                 g_string_append (res, "[]");
170                 break;
171         case MONO_TYPE_CLASS:
172         case MONO_TYPE_VALUETYPE:
173                 append_class_name (res, type->data.klass, include_namespace);
174                 break;
175         case MONO_TYPE_GENERICINST: {
176                 MonoGenericContext *context;
177
178                 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
179                 g_string_append (res, "<");
180                 context = &type->data.generic_class->context;
181                 if (context->class_inst) {
182                         for (i = 0; i < context->class_inst->type_argc; ++i) {
183                                 if (i > 0)
184                                         g_string_append (res, ", ");
185                                 mono_type_get_desc (res, context->class_inst->type_argv [i], include_namespace);
186                         }
187                 }
188                 if (context->method_inst) {
189                         if (context->class_inst)
190                                         g_string_append (res, "; ");
191                         for (i = 0; i < context->method_inst->type_argc; ++i) {
192                                 if (i > 0)
193                                         g_string_append (res, ", ");
194                                 mono_type_get_desc (res, context->method_inst->type_argv [i], include_namespace);
195                         }
196                 }
197                 g_string_append (res, ">");
198                 break;
199         }
200         case MONO_TYPE_VAR:
201         case MONO_TYPE_MVAR:
202                 if (type->data.generic_param) {
203                         const char *name = mono_generic_param_name (type->data.generic_param);
204                         if (name)
205                                 g_string_append (res, name);
206                         else
207                                 g_string_append_printf (res, "%s%d", type->type == MONO_TYPE_VAR ? "!" : "!!", mono_generic_param_num (type->data.generic_param));
208                 } else {
209                         g_string_append (res, "<unknown>");
210                 }
211                 break;
212         case MONO_TYPE_TYPEDBYREF:
213                 g_string_append (res, "typedbyref");
214                 break;
215         default:
216                 break;
217         }
218         if (type->byref)
219                 g_string_append_c (res, '&');
220 }
221
222 char*
223 mono_type_full_name (MonoType *type)
224 {
225         GString *str;
226
227         str = g_string_new ("");
228         mono_type_get_desc (str, type, TRUE);
229         return g_string_free (str, FALSE);
230 }
231
232 char*
233 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
234 {
235         int i;
236         char *result;
237         GString *res;
238
239         if (!sig)
240                 return g_strdup ("<invalid signature>");
241
242         res = g_string_new ("");
243
244         for (i = 0; i < sig->param_count; ++i) {
245                 if (i > 0)
246                         g_string_append_c (res, ',');
247                 mono_type_get_desc (res, sig->params [i], include_namespace);
248         }
249         result = res->str;
250         g_string_free (res, FALSE);
251         return result;
252 }
253
254 char*
255 mono_signature_full_name (MonoMethodSignature *sig)
256 {
257         int i;
258         char *result;
259         GString *res;
260
261         if (!sig)
262                 return g_strdup ("<invalid signature>");
263
264         res = g_string_new ("");
265
266         mono_type_get_desc (res, sig->ret, TRUE);
267         g_string_append_c (res, '(');
268         for (i = 0; i < sig->param_count; ++i) {
269                 if (i > 0)
270                         g_string_append_c (res, ',');
271                 mono_type_get_desc (res, sig->params [i], TRUE);
272         }
273         g_string_append_c (res, ')');
274         result = res->str;
275         g_string_free (res, FALSE);
276         return result;
277 }
278
279 static void
280 ginst_get_desc (GString *str, MonoGenericInst *ginst)
281 {
282         int i;
283
284         for (i = 0; i < ginst->type_argc; ++i) {
285                 if (i > 0)
286                         g_string_append (str, ", ");
287                 mono_type_get_desc (str, ginst->type_argv [i], TRUE);
288         }
289 }
290
291 char*
292 mono_context_get_desc (MonoGenericContext *context)
293 {
294         GString *str;
295         char *res;
296
297         str = g_string_new ("");
298         g_string_append (str, "<");
299
300         if (context->class_inst)
301                 ginst_get_desc (str, context->class_inst);
302         if (context->method_inst) {
303                 if (context->class_inst)
304                         g_string_append (str, "; ");
305                 ginst_get_desc (str, context->method_inst);
306         }
307
308         g_string_append (str, ">");
309         res = g_strdup (str->str);
310         g_string_free (str, TRUE);
311         return res;
312 }       
313
314 /**
315  * mono_method_desc_new:
316  * @name: the method name.
317  * @include_namespace: whether the name includes a namespace or not.
318  *
319  * Creates a method description for @name, which conforms to the following
320  * specification:
321  *
322  * [namespace.]classname:methodname[(args...)]
323  *
324  * in all the loaded assemblies.
325  *
326  * Both classname and methodname can contain '*' which matches anything.
327  *
328  * Returns: a parsed representation of the method description.
329  */
330 MonoMethodDesc*
331 mono_method_desc_new (const char *name, gboolean include_namespace)
332 {
333         MonoMethodDesc *result;
334         char *class_name, *class_nspace, *method_name, *use_args, *end;
335         int use_namespace;
336         
337         class_nspace = g_strdup (name);
338         use_args = strchr (class_nspace, '(');
339         if (use_args) {
340                 /* Allow a ' ' between the method name and the signature */
341                 if (use_args > class_nspace && use_args [-1] == ' ')
342                         use_args [-1] = 0;
343                 *use_args++ = 0;
344                 end = strchr (use_args, ')');
345                 if (!end) {
346                         g_free (class_nspace);
347                         return NULL;
348                 }
349                 *end = 0;
350         }
351         method_name = strrchr (class_nspace, ':');
352         if (!method_name) {
353                 g_free (class_nspace);
354                 return NULL;
355         }
356         /* allow two :: to separate the method name */
357         if (method_name != class_nspace && method_name [-1] == ':')
358                 method_name [-1] = 0;
359         *method_name++ = 0;
360         class_name = strrchr (class_nspace, '.');
361         if (class_name) {
362                 *class_name++ = 0;
363                 use_namespace = 1;
364         } else {
365                 class_name = class_nspace;
366                 use_namespace = 0;
367         }
368         result = g_new0 (MonoMethodDesc, 1);
369         result->include_namespace = include_namespace;
370         result->name = method_name;
371         result->klass = class_name;
372         result->name_space = use_namespace? class_nspace: NULL;
373         result->args = use_args? use_args: NULL;
374         if (strstr (result->name, "*"))
375                 result->name_glob = TRUE;
376         if (strstr (result->klass, "*"))
377                 result->klass_glob = TRUE;
378         if (use_args) {
379                 end = use_args;
380                 if (*end)
381                         result->num_args = 1;
382                 while (*end) {
383                         if (*end == ',')
384                                 result->num_args++;
385                         ++end;
386                 }
387         }
388
389         return result;
390 }
391
392 MonoMethodDesc*
393 mono_method_desc_from_method (MonoMethod *method)
394 {
395         MonoMethodDesc *result;
396         
397         result = g_new0 (MonoMethodDesc, 1);
398         result->include_namespace = TRUE;
399         result->name = g_strdup (method->name);
400         result->klass = g_strdup (method->klass->name);
401         result->name_space = g_strdup (method->klass->name_space);
402
403         return result;
404 }
405
406 /**
407  * mono_method_desc_free:
408  * @desc: method description to be released
409  *
410  * Releases the MonoMethodDesc object @desc.
411  */
412 void
413 mono_method_desc_free (MonoMethodDesc *desc)
414 {
415         if (desc->name_space)
416                 g_free (desc->name_space);
417         else if (desc->klass)
418                 g_free (desc->klass);
419         g_free (desc);
420 }
421
422 /**
423  * mono_method_descr_match:
424  * @desc: MonoMethoDescription
425  * @method: MonoMethod to test
426  *
427  * Determines whether the specified @method matches the provided @desc description.
428  *
429  * namespace and class are supposed to match already if this function is used.
430  * Returns: True if the method matches the description, false otherwise.
431  */
432 gboolean
433 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
434 {
435         char *sig;
436         gboolean name_match;
437
438         name_match = strcmp (desc->name, method->name) == 0;
439 #ifndef _EGLIB_MAJOR
440         if (!name_match && desc->name_glob)
441                 name_match = g_pattern_match_simple (desc->name, method->name);
442 #endif
443         if (!name_match)
444                 return FALSE;
445         if (!desc->args)
446                 return TRUE;
447         if (desc->num_args != mono_method_signature (method)->param_count)
448                 return FALSE;
449         sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
450         if (strcmp (sig, desc->args)) {
451                 g_free (sig);
452                 return FALSE;
453         }
454         g_free (sig);
455         return TRUE;
456 }
457
458 static const char *
459 my_strrchr (const char *str, char ch, int *len)
460 {
461         int pos;
462
463         for (pos = (*len)-1; pos >= 0; pos--) {
464                 if (str [pos] != ch)
465                         continue;
466
467                 *len = pos;
468                 return str + pos;
469         }
470
471         return NULL;
472 }
473
474 static gboolean
475 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
476 {
477         const char *p;
478
479         if (desc->klass_glob && !strcmp (desc->klass, "*"))
480                 return TRUE;
481 #ifndef _EGLIB_MAJOR
482         if (desc->klass_glob && g_pattern_match_simple (desc->klass, klass->name))
483                 return TRUE;
484 #endif
485         p = my_strrchr (desc->klass, '/', &pos);
486         if (!p) {
487                 if (strncmp (desc->klass, klass->name, pos))
488                         return FALSE;
489                 if (desc->name_space && strcmp (desc->name_space, klass->name_space))
490                         return FALSE;
491                 return TRUE;
492         }
493
494         if (strcmp (p+1, klass->name))
495                 return FALSE;
496         if (!klass->nested_in)
497                 return FALSE;
498
499         return match_class (desc, pos, klass->nested_in);
500 }
501
502 gboolean
503 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
504 {
505         if (!desc->klass)
506                 return FALSE;
507         if (!match_class (desc, strlen (desc->klass), method->klass))
508                 return FALSE;
509
510         return mono_method_desc_match (desc, method);
511 }
512
513 MonoMethod*
514 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
515 {
516         MonoMethod* m;
517         gpointer iter = NULL;
518         
519         while ((m = mono_class_get_methods (klass, &iter)))
520                 if (mono_method_desc_match (desc, m))
521                         return m;
522         return NULL;
523 }
524
525 MonoMethod*
526 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
527 {
528         MonoClass *klass;
529         const MonoTableInfo *methods;
530         MonoMethod *method;
531         int i;
532
533         /* Handle short names for system classes */
534         if (!desc->name_space && image == mono_defaults.corlib) {
535                 klass = find_system_class (desc->klass);
536                 if (klass)
537                         return mono_method_desc_search_in_class (desc, klass);
538         }
539
540         if (desc->name_space && desc->klass) {
541                 klass = mono_class_try_load_from_name (image, desc->name_space, desc->klass);
542                 if (!klass)
543                         return NULL;
544                 return mono_method_desc_search_in_class (desc, klass);
545         }
546
547         /* FIXME: Is this call necessary?  We don't use its result. */
548         mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
549         methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
550         for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
551                 MonoError error;
552                 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
553                 const char *n = mono_metadata_string_heap (image, token);
554
555                 if (strcmp (n, desc->name))
556                         continue;
557                 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
558                 if (!method) {
559                         mono_error_cleanup (&error);
560                         continue;
561                 }
562                 if (mono_method_desc_full_match (desc, method))
563                         return method;
564         }
565         return NULL;
566 }
567
568 static const unsigned char*
569 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
570 {
571         MonoError error;
572         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
573         const MonoOpcode *opcode;
574         guint32 label, token;
575         gint32 sval;
576         int i;
577         char *tmp;
578         const unsigned char* il_code;
579
580         if (!header) {
581                 g_string_append_printf (str, "could not disassemble, bad header due to %s", mono_error_get_message (&error));
582                 mono_error_cleanup (&error);
583                 return end;
584         }
585         il_code = mono_method_header_get_code (header, NULL, NULL);
586
587         label = ip - il_code;
588         if (dh->indenter) {
589                 tmp = dh->indenter (dh, method, label);
590                 g_string_append (str, tmp);
591                 g_free (tmp);
592         }
593         if (dh->label_format)
594                 g_string_append_printf (str, dh->label_format, label);
595         
596         i = mono_opcode_value (&ip, end);
597         ip++;
598         opcode = &mono_opcodes [i];
599         g_string_append_printf (str, "%-10s", mono_opcode_name (i));
600
601         switch (opcode->argument) {
602         case MonoInlineNone:
603                 break;
604         case MonoInlineType:
605         case MonoInlineField:
606         case MonoInlineMethod:
607         case MonoInlineTok:
608         case MonoInlineSig:
609                 token = read32 (ip);
610                 if (dh->tokener) {
611                         tmp = dh->tokener (dh, method, token);
612                         g_string_append (str, tmp);
613                         g_free (tmp);
614                 } else {
615                         g_string_append_printf (str, "0x%08x", token);
616                 }
617                 ip += 4;
618                 break;
619         case MonoInlineString: {
620                 const char *blob;
621                 char *s;
622                 size_t len2;
623                 char *blob2 = NULL;
624
625                 if (!image_is_dynamic (method->klass->image) && !method_is_dynamic (method)) {
626                         token = read32 (ip);
627                         blob = mono_metadata_user_string (method->klass->image, mono_metadata_token_index (token));
628
629                         len2 = mono_metadata_decode_blob_size (blob, &blob);
630                         len2 >>= 1;
631
632 #ifdef NO_UNALIGNED_ACCESS
633                         /* The blob might not be 2 byte aligned */
634                         blob2 = g_malloc ((len2 * 2) + 1);
635                         memcpy (blob2, blob, len2 * 2);
636 #else
637                         blob2 = (char*)blob;
638 #endif
639
640 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
641                         {
642                                 guint16 *buf = g_new (guint16, len2 + 1);
643                                 int i;
644
645                                 for (i = 0; i < len2; ++i)
646                                         buf [i] = GUINT16_FROM_LE (((guint16*)blob2) [i]);
647                                 s = g_utf16_to_utf8 (buf, len2, NULL, NULL, NULL);
648                                 g_free (buf);
649                         }
650 #else
651                                 s = g_utf16_to_utf8 ((gunichar2*)blob2, len2, NULL, NULL, NULL);
652 #endif
653
654                         g_string_append_printf (str, "\"%s\"", s);
655                         g_free (s);
656                         if (blob != blob2)
657                                 g_free (blob2);
658                 }
659                 ip += 4;
660                 break;
661         }
662         case MonoInlineVar:
663                 g_string_append_printf (str, "%d", read16 (ip));
664                 ip += 2;
665                 break;
666         case MonoShortInlineVar:
667                 g_string_append_printf (str, "%d", (*ip));
668                 ip ++;
669                 break;
670         case MonoInlineBrTarget:
671                 sval = read32 (ip);
672                 ip += 4;
673                 if (dh->label_target)
674                         g_string_append_printf (str, dh->label_target, ip + sval - il_code);
675                 else
676                         g_string_append_printf (str, "%d", sval);
677                 break;
678         case MonoShortInlineBrTarget:
679                 sval = *(const signed char*)ip;
680                 ip ++;
681                 if (dh->label_target)
682                         g_string_append_printf (str, dh->label_target, ip + sval - il_code);
683                 else
684                         g_string_append_printf (str, "%d", sval);
685                 break;
686         case MonoInlineSwitch: {
687                 const unsigned char *end;
688                 sval = read32 (ip);
689                 ip += 4;
690                 end = ip + sval * 4;
691                 g_string_append_c (str, '(');
692                 for (i = 0; i < sval; ++i) {
693                         if (i > 0)
694                                 g_string_append (str, ", ");
695                         label = read32 (ip);
696                         if (dh->label_target)
697                                 g_string_append_printf (str, dh->label_target, end + label - il_code);
698                         else
699                                 g_string_append_printf (str, "%d", label);
700                         ip += 4;
701                 }
702                 g_string_append_c (str, ')');
703                 break;
704         }
705         case MonoInlineR: {
706                 double r;
707                 readr8 (ip, &r);
708                 g_string_append_printf (str, "%g", r);
709                 ip += 8;
710                 break;
711         }
712         case MonoShortInlineR: {
713                 float r;
714                 readr4 (ip, &r);
715                 g_string_append_printf (str, "%g", r);
716                 ip += 4;
717                 break;
718         }
719         case MonoInlineI:
720                 g_string_append_printf (str, "%d", (gint32)read32 (ip));
721                 ip += 4;
722                 break;
723         case MonoShortInlineI:
724                 g_string_append_printf (str, "%d", *(const signed char*)ip);
725                 ip ++;
726                 break;
727         case MonoInlineI8:
728                 ip += 8;
729                 break;
730         default:
731                 g_assert_not_reached ();
732         }
733         if (dh->newline)
734                 g_string_append (str, dh->newline);
735
736         mono_metadata_free_mh (header);
737         return ip;
738 }
739
740 static MonoDisHelper
741 default_dh = {
742         "\n",
743         "IL_%04x: ", /* label_format */
744         "IL_%04x", /* label_target */
745         NULL, /* indenter */
746         NULL, /* tokener */
747         NULL  /* user data */
748 };
749
750 char*
751 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
752 {
753         char *result;
754         GString *res = g_string_new ("");
755
756         if (!dh)
757                 dh = &default_dh;
758         /* set ip + 2 as the end: this is just a debugging method */
759         ip = dis_one (res, dh, method, ip, ip + 2);
760         if (endp)
761                 *endp = ip;
762         
763         result = res->str;
764         g_string_free (res, FALSE);
765         return result;
766 }
767
768 char*
769 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
770 {
771         char *result;
772         GString *res = g_string_new ("");
773
774         if (!dh)
775                 dh = &default_dh;
776         while (ip < end) {
777                 ip = dis_one (res, dh, method, ip, end);
778         }
779         
780         result = res->str;
781         g_string_free (res, FALSE);
782         return result;
783 }
784
785 /**
786  * mono_field_full_name:
787  * @field: field to retrieve information for
788  *
789  * Returns: the full name for the field, made up of the namespace, type name and the field name.
790  */
791 char *
792 mono_field_full_name (MonoClassField *field)
793 {
794         char *res;
795         const char *nspace = field->parent->name_space;
796
797         res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
798                                                    field->parent->name, mono_field_get_name (field));
799
800         return res;
801 }
802
803 char *
804 mono_method_get_name_full (MonoMethod *method, gboolean signature, gboolean ret, MonoTypeNameFormat format)
805 {
806         char *res;
807         char wrapper [64];
808         char *klass_desc;
809         char *inst_desc = NULL;
810
811         if (format == MONO_TYPE_NAME_FORMAT_IL)
812                 klass_desc = mono_type_full_name (&method->klass->byval_arg);
813         else
814                 klass_desc = mono_type_get_name_full (&method->klass->byval_arg, format);
815
816         if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
817                 GString *str = g_string_new ("");
818                 if (format == MONO_TYPE_NAME_FORMAT_IL)
819                         g_string_append (str, "<");
820                 else
821                         g_string_append (str, "[");
822                 ginst_get_desc (str, ((MonoMethodInflated*)method)->context.method_inst);
823                 if (format == MONO_TYPE_NAME_FORMAT_IL)
824                         g_string_append_c (str, '>');
825                 else
826                         g_string_append_c (str, ']');
827
828                 inst_desc = str->str;
829                 g_string_free (str, FALSE);
830         } else if (method->is_generic) {
831                 MonoGenericContainer *container = mono_method_get_generic_container (method);
832
833                 GString *str = g_string_new ("");
834                 if (format == MONO_TYPE_NAME_FORMAT_IL)
835                         g_string_append (str, "<");
836                 else
837                         g_string_append (str, "[");
838                 ginst_get_desc (str, container->context.method_inst);
839                 if (format == MONO_TYPE_NAME_FORMAT_IL)
840                         g_string_append_c (str, '>');
841                 else
842                         g_string_append_c (str, ']');
843
844                 inst_desc = str->str;
845                 g_string_free (str, FALSE);
846         }
847
848         if (method->wrapper_type != MONO_WRAPPER_NONE)
849                 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
850         else
851                 strcpy (wrapper, "");
852
853         if (signature) {
854                 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
855
856                 if (method->wrapper_type != MONO_WRAPPER_NONE)
857                         sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
858                 else
859                         strcpy (wrapper, "");
860                 if (ret) {
861                         char *ret_str = mono_type_full_name (mono_method_signature (method)->ret);
862                         res = g_strdup_printf ("%s%s %s:%s%s (%s)", wrapper, ret_str, klass_desc,
863                                                                    method->name, inst_desc ? inst_desc : "", tmpsig);
864                         g_free (ret_str);
865                 } else {
866                         res = g_strdup_printf ("%s%s:%s%s (%s)", wrapper, klass_desc,
867                                                                    method->name, inst_desc ? inst_desc : "", tmpsig);
868                 }
869                 g_free (tmpsig);
870         } else {
871                 res = g_strdup_printf ("%s%s:%s%s", wrapper, klass_desc,
872                                                            method->name, inst_desc ? inst_desc : "");
873         }
874
875         g_free (klass_desc);
876         g_free (inst_desc);
877
878         return res;
879 }
880
881 char *
882 mono_method_full_name (MonoMethod *method, gboolean signature)
883 {
884         return mono_method_get_name_full (method, signature, FALSE, MONO_TYPE_NAME_FORMAT_IL);
885 }
886
887 char *
888 mono_method_get_full_name (MonoMethod *method)
889 {
890         return mono_method_get_name_full (method, TRUE, TRUE, MONO_TYPE_NAME_FORMAT_IL);
891 }
892
893 static const char*
894 print_name_space (MonoClass *klass)
895 {
896         if (klass->nested_in) {
897                 print_name_space (klass->nested_in);
898                 g_print ("%s", klass->nested_in->name);
899                 return "/";
900         }
901         if (klass->name_space [0]) {
902                 g_print ("%s", klass->name_space);
903                 return ".";
904         }
905         return "";
906 }
907
908 /**
909  * mono_object_describe:
910  *
911  * Prints to stdout a small description of the object @obj.
912  * For use in a debugger.
913  */
914 void
915 mono_object_describe (MonoObject *obj)
916 {
917         MonoClass* klass;
918         const char* sep;
919         if (!obj) {
920                 g_print ("(null)\n");
921                 return;
922         }
923         klass = mono_object_class (obj);
924         if (klass == mono_defaults.string_class) {
925                 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
926                 if (strlen (utf8) > 60) {
927                         utf8 [57] = '.';
928                         utf8 [58] = '.';
929                         utf8 [59] = '.';
930                         utf8 [60] = 0;
931                 }
932                 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
933                 g_free (utf8);
934         } else if (klass->rank) {
935                 MonoArray *array = (MonoArray*)obj;
936                 sep = print_name_space (klass);
937                 g_print ("%s%s", sep, klass->name);
938                 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, (int)mono_array_length (array));
939         } else {
940                 sep = print_name_space (klass);
941                 g_print ("%s%s", sep, klass->name);
942                 g_print (" object at %p (klass: %p)\n", obj, klass);
943         }
944
945 }
946
947 static void
948 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
949 {
950         MonoType *type;
951         g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, mono_field_get_name (field));
952         type = mono_type_get_underlying_type (field->type);
953
954         switch (type->type) {
955         case MONO_TYPE_I:
956         case MONO_TYPE_U:
957         case MONO_TYPE_PTR:
958         case MONO_TYPE_FNPTR:
959                 g_print ("%p\n", *(const void**)field_ptr);
960                 break;
961         case MONO_TYPE_STRING:
962         case MONO_TYPE_SZARRAY:
963         case MONO_TYPE_CLASS:
964         case MONO_TYPE_OBJECT:
965         case MONO_TYPE_ARRAY:
966                 mono_object_describe (*(MonoObject**)field_ptr);
967                 break;
968         case MONO_TYPE_GENERICINST:
969                 if (!mono_type_generic_inst_is_valuetype (type)) {
970                         mono_object_describe (*(MonoObject**)field_ptr);
971                         break;
972                 } else {
973                         /* fall through */
974                 }
975         case MONO_TYPE_VALUETYPE: {
976                 MonoClass *k = mono_class_from_mono_type (type);
977                 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
978                 break;
979         }
980         case MONO_TYPE_I1:
981                 g_print ("%d\n", *(gint8*)field_ptr);
982                 break;
983         case MONO_TYPE_U1:
984                 g_print ("%d\n", *(guint8*)field_ptr);
985                 break;
986         case MONO_TYPE_I2:
987                 g_print ("%d\n", *(gint16*)field_ptr);
988                 break;
989         case MONO_TYPE_U2:
990                 g_print ("%d\n", *(guint16*)field_ptr);
991                 break;
992         case MONO_TYPE_I4:
993                 g_print ("%d\n", *(gint32*)field_ptr);
994                 break;
995         case MONO_TYPE_U4:
996                 g_print ("%u\n", *(guint32*)field_ptr);
997                 break;
998         case MONO_TYPE_I8:
999                 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
1000                 break;
1001         case MONO_TYPE_U8:
1002                 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
1003                 break;
1004         case MONO_TYPE_R4:
1005                 g_print ("%f\n", *(gfloat*)field_ptr);
1006                 break;
1007         case MONO_TYPE_R8:
1008                 g_print ("%f\n", *(gdouble*)field_ptr);
1009                 break;
1010         case MONO_TYPE_BOOLEAN:
1011                 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
1012                 break;
1013         case MONO_TYPE_CHAR:
1014                 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
1015                 break;
1016         default:
1017                 g_assert_not_reached ();
1018                 break;
1019         }
1020 }
1021
1022 static void
1023 objval_describe (MonoClass *klass, const char *addr)
1024 {
1025         MonoClassField *field;
1026         MonoClass *p;
1027         const char *field_ptr;
1028         gssize type_offset = 0;
1029
1030         if (klass->valuetype)
1031                 type_offset = -sizeof (MonoObject);
1032
1033         for (p = klass; p != NULL; p = p->parent) {
1034                 gpointer iter = NULL;
1035                 int printed_header = FALSE;
1036                 while ((field = mono_class_get_fields (p, &iter))) {
1037                         if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
1038                                 continue;
1039
1040                         if (p != klass && !printed_header) {
1041                                 const char *sep;
1042                                 g_print ("In class ");
1043                                 sep = print_name_space (p);
1044                                 g_print ("%s%s:\n", sep, p->name);
1045                                 printed_header = TRUE;
1046                         }
1047                         field_ptr = (const char*)addr + field->offset + type_offset;
1048
1049                         print_field_value (field_ptr, field, type_offset);
1050                 }
1051         }
1052 }
1053
1054 /**
1055  * mono_object_describe_fields:
1056  *
1057  * Prints to stdout a small description of each field of the object @obj.
1058  * For use in a debugger.
1059  */
1060 void
1061 mono_object_describe_fields (MonoObject *obj)
1062 {
1063         MonoClass *klass = mono_object_class (obj);
1064         objval_describe (klass, (char*)obj);
1065 }
1066
1067 /**
1068  * mono_value_describe_fields:
1069  *
1070  * Prints to stdout a small description of each field of the value type
1071  * stored at @addr of type @klass.
1072  * For use in a debugger.
1073  */
1074 void
1075 mono_value_describe_fields (MonoClass* klass, const char* addr)
1076 {
1077         objval_describe (klass, addr);
1078 }
1079
1080 /**
1081  * mono_class_describe_statics:
1082  *
1083  * Prints to stdout a small description of each static field of the type @klass
1084  * in the current application domain.
1085  * For use in a debugger.
1086  */
1087 void
1088 mono_class_describe_statics (MonoClass* klass)
1089 {
1090         MonoError error;
1091         MonoClassField *field;
1092         MonoClass *p;
1093         const char *field_ptr;
1094         MonoVTable *vtable = mono_class_vtable_full (mono_domain_get (), klass, &error);
1095         const char *addr;
1096
1097         if (!vtable || !is_ok (&error)) {
1098                 mono_error_cleanup (&error);
1099                 return;
1100         }
1101
1102         if (!(addr = (const char *)mono_vtable_get_static_field_data (vtable)))
1103                 return;
1104
1105         for (p = klass; p != NULL; p = p->parent) {
1106                 gpointer iter = NULL;
1107                 while ((field = mono_class_get_fields (p, &iter))) {
1108                         if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1109                                 continue;
1110                         if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
1111                                 continue;
1112
1113                         field_ptr = (const char*)addr + field->offset;
1114
1115                         print_field_value (field_ptr, field, 0);
1116                 }
1117         }
1118 }
1119
1120 /**
1121  * mono_print_method_code
1122  * @MonoMethod: a pointer to the method
1123  *
1124  * This method is used from a debugger to print the code of the method.
1125  *
1126  * This prints the IL code of the method in the standard output.
1127  */
1128 void
1129 mono_method_print_code (MonoMethod *method)
1130 {
1131         MonoError error;
1132         char *code;
1133         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
1134         if (!header) {
1135                 printf ("METHOD HEADER NOT FOUND DUE TO: %s\n", mono_error_get_message (&error));
1136                 mono_error_cleanup (&error);
1137                 return;
1138         }
1139         code = mono_disasm_code (0, method, header->code, header->code + header->code_size);
1140         printf ("CODE FOR %s:\n%s\n", mono_method_full_name (method, TRUE), code);
1141         g_free (code);
1142 }