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