Merge pull request #2305 from ludovic-henry/fix-threadpool-36414
[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 static void
255 ginst_get_desc (GString *str, MonoGenericInst *ginst)
256 {
257         int i;
258
259         for (i = 0; i < ginst->type_argc; ++i) {
260                 if (i > 0)
261                         g_string_append (str, ", ");
262                 mono_type_get_desc (str, ginst->type_argv [i], TRUE);
263         }
264 }
265
266 char*
267 mono_context_get_desc (MonoGenericContext *context)
268 {
269         GString *str;
270         char *res;
271
272         str = g_string_new ("");
273         g_string_append (str, "<");
274
275         if (context->class_inst)
276                 ginst_get_desc (str, context->class_inst);
277         if (context->method_inst) {
278                 if (context->class_inst)
279                         g_string_append (str, "; ");
280                 ginst_get_desc (str, context->method_inst);
281         }
282
283         g_string_append (str, ">");
284         res = g_strdup (str->str);
285         g_string_free (str, TRUE);
286         return res;
287 }       
288
289 /**
290  * mono_method_desc_new:
291  * @name: the method name.
292  * @include_namespace: whether the name includes a namespace or not.
293  *
294  * Creates a method description for @name, which conforms to the following
295  * specification:
296  *
297  * [namespace.]classname:methodname[(args...)]
298  *
299  * in all the loaded assemblies.
300  *
301  * Both classname and methodname can contain '*' which matches anything.
302  *
303  * Returns: a parsed representation of the method description.
304  */
305 MonoMethodDesc*
306 mono_method_desc_new (const char *name, gboolean include_namespace)
307 {
308         MonoMethodDesc *result;
309         char *class_name, *class_nspace, *method_name, *use_args, *end;
310         int use_namespace;
311         
312         class_nspace = g_strdup (name);
313         use_args = strchr (class_nspace, '(');
314         if (use_args) {
315                 /* Allow a ' ' between the method name and the signature */
316                 if (use_args > class_nspace && use_args [-1] == ' ')
317                         use_args [-1] = 0;
318                 *use_args++ = 0;
319                 end = strchr (use_args, ')');
320                 if (!end) {
321                         g_free (class_nspace);
322                         return NULL;
323                 }
324                 *end = 0;
325         }
326         method_name = strrchr (class_nspace, ':');
327         if (!method_name) {
328                 g_free (class_nspace);
329                 return NULL;
330         }
331         /* allow two :: to separate the method name */
332         if (method_name != class_nspace && method_name [-1] == ':')
333                 method_name [-1] = 0;
334         *method_name++ = 0;
335         class_name = strrchr (class_nspace, '.');
336         if (class_name) {
337                 *class_name++ = 0;
338                 use_namespace = 1;
339         } else {
340                 class_name = class_nspace;
341                 use_namespace = 0;
342         }
343         result = g_new0 (MonoMethodDesc, 1);
344         result->include_namespace = include_namespace;
345         result->name = method_name;
346         result->klass = class_name;
347         result->name_space = use_namespace? class_nspace: NULL;
348         result->args = use_args? use_args: NULL;
349         if (strstr (result->name, "*"))
350                 result->name_glob = TRUE;
351         if (strstr (result->klass, "*"))
352                 result->klass_glob = TRUE;
353         if (use_args) {
354                 end = use_args;
355                 if (*end)
356                         result->num_args = 1;
357                 while (*end) {
358                         if (*end == ',')
359                                 result->num_args++;
360                         ++end;
361                 }
362         }
363
364         return result;
365 }
366
367 MonoMethodDesc*
368 mono_method_desc_from_method (MonoMethod *method)
369 {
370         MonoMethodDesc *result;
371         
372         result = g_new0 (MonoMethodDesc, 1);
373         result->include_namespace = TRUE;
374         result->name = g_strdup (method->name);
375         result->klass = g_strdup (method->klass->name);
376         result->name_space = g_strdup (method->klass->name_space);
377
378         return result;
379 }
380
381 /**
382  * mono_method_desc_free:
383  * @desc: method description to be released
384  *
385  * Releases the MonoMethodDesc object @desc.
386  */
387 void
388 mono_method_desc_free (MonoMethodDesc *desc)
389 {
390         if (desc->name_space)
391                 g_free (desc->name_space);
392         else if (desc->klass)
393                 g_free (desc->klass);
394         g_free (desc);
395 }
396
397 /*
398  * namespace and class are supposed to match already if this function is used.
399  */
400 gboolean
401 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
402 {
403         char *sig;
404         gboolean name_match;
405
406         name_match = strcmp (desc->name, method->name) == 0;
407 #ifndef _EGLIB_MAJOR
408         if (!name_match && desc->name_glob)
409                 name_match = g_pattern_match_simple (desc->name, method->name);
410 #endif
411         if (!name_match)
412                 return FALSE;
413         if (!desc->args)
414                 return TRUE;
415         if (desc->num_args != mono_method_signature (method)->param_count)
416                 return FALSE;
417         sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
418         if (strcmp (sig, desc->args)) {
419                 g_free (sig);
420                 return FALSE;
421         }
422         g_free (sig);
423         return TRUE;
424 }
425
426 static const char *
427 my_strrchr (const char *str, char ch, int *len)
428 {
429         int pos;
430
431         for (pos = (*len)-1; pos >= 0; pos--) {
432                 if (str [pos] != ch)
433                         continue;
434
435                 *len = pos;
436                 return str + pos;
437         }
438
439         return NULL;
440 }
441
442 static gboolean
443 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
444 {
445         const char *p;
446
447         if (desc->klass_glob && !strcmp (desc->klass, "*"))
448                 return TRUE;
449 #ifndef _EGLIB_MAJOR
450         if (desc->klass_glob && g_pattern_match_simple (desc->klass, klass->name))
451                 return TRUE;
452 #endif
453         p = my_strrchr (desc->klass, '/', &pos);
454         if (!p) {
455                 if (strncmp (desc->klass, klass->name, pos))
456                         return FALSE;
457                 if (desc->name_space && strcmp (desc->name_space, klass->name_space))
458                         return FALSE;
459                 return TRUE;
460         }
461
462         if (strcmp (p+1, klass->name))
463                 return FALSE;
464         if (!klass->nested_in)
465                 return FALSE;
466
467         return match_class (desc, pos, klass->nested_in);
468 }
469
470 gboolean
471 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
472 {
473         if (!desc->klass)
474                 return FALSE;
475         if (!match_class (desc, strlen (desc->klass), method->klass))
476                 return FALSE;
477
478         return mono_method_desc_match (desc, method);
479 }
480
481 MonoMethod*
482 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
483 {
484         MonoMethod* m;
485         gpointer iter = NULL;
486         
487         while ((m = mono_class_get_methods (klass, &iter)))
488                 if (mono_method_desc_match (desc, m))
489                         return m;
490         return NULL;
491 }
492
493 MonoMethod*
494 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
495 {
496         MonoClass *klass;
497         const MonoTableInfo *methods;
498         MonoMethod *method;
499         int i;
500
501         /* Handle short names for system classes */
502         if (!desc->name_space && image == mono_defaults.corlib) {
503                 klass = find_system_class (desc->klass);
504                 if (klass)
505                         return mono_method_desc_search_in_class (desc, klass);
506         }
507
508         if (desc->name_space && desc->klass) {
509                 klass = mono_class_from_name (image, desc->name_space, desc->klass);
510                 if (!klass)
511                         return NULL;
512                 return mono_method_desc_search_in_class (desc, klass);
513         }
514
515         /* FIXME: Is this call necessary?  We don't use its result. */
516         mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
517         methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
518         for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
519                 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
520                 const char *n = mono_metadata_string_heap (image, token);
521
522                 if (strcmp (n, desc->name))
523                         continue;
524                 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
525                 if (mono_method_desc_full_match (desc, method))
526                         return method;
527         }
528         return NULL;
529 }
530
531 static const unsigned char*
532 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
533 {
534         MonoMethodHeader *header = mono_method_get_header (method);
535         const MonoOpcode *opcode;
536         guint32 label, token;
537         gint32 sval;
538         int i;
539         char *tmp;
540         const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
541
542         label = ip - il_code;
543         if (dh->indenter) {
544                 tmp = dh->indenter (dh, method, label);
545                 g_string_append (str, tmp);
546                 g_free (tmp);
547         }
548         if (dh->label_format)
549                 g_string_append_printf (str, dh->label_format, label);
550         
551         i = mono_opcode_value (&ip, end);
552         ip++;
553         opcode = &mono_opcodes [i];
554         g_string_append_printf (str, "%-10s", mono_opcode_name (i));
555
556         switch (opcode->argument) {
557         case MonoInlineNone:
558                 break;
559         case MonoInlineType:
560         case MonoInlineField:
561         case MonoInlineMethod:
562         case MonoInlineTok:
563         case MonoInlineSig:
564                 token = read32 (ip);
565                 if (dh->tokener) {
566                         tmp = dh->tokener (dh, method, token);
567                         g_string_append (str, tmp);
568                         g_free (tmp);
569                 } else {
570                         g_string_append_printf (str, "0x%08x", token);
571                 }
572                 ip += 4;
573                 break;
574         case MonoInlineString: {
575                 const char *blob;
576                 char *s;
577                 size_t len2;
578                 char *blob2 = NULL;
579
580                 if (!image_is_dynamic (method->klass->image) && !method_is_dynamic (method)) {
581                         token = read32 (ip);
582                         blob = mono_metadata_user_string (method->klass->image, mono_metadata_token_index (token));
583
584                         len2 = mono_metadata_decode_blob_size (blob, &blob);
585                         len2 >>= 1;
586
587 #ifdef NO_UNALIGNED_ACCESS
588                         /* The blob might not be 2 byte aligned */
589                         blob2 = g_malloc ((len2 * 2) + 1);
590                         memcpy (blob2, blob, len2 * 2);
591 #else
592                         blob2 = (char*)blob;
593 #endif
594
595 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
596                         {
597                                 guint16 *buf = g_new (guint16, len2 + 1);
598                                 int i;
599
600                                 for (i = 0; i < len2; ++i)
601                                         buf [i] = GUINT16_FROM_LE (((guint16*)blob2) [i]);
602                                 s = g_utf16_to_utf8 (buf, len2, NULL, NULL, NULL);
603                                 g_free (buf);
604                         }
605 #else
606                                 s = g_utf16_to_utf8 ((gunichar2*)blob2, len2, NULL, NULL, NULL);
607 #endif
608
609                         g_string_append_printf (str, "\"%s\"", s);
610                         g_free (s);
611                         if (blob != blob2)
612                                 g_free (blob2);
613                 }
614                 ip += 4;
615                 break;
616         }
617         case MonoInlineVar:
618                 g_string_append_printf (str, "%d", read16 (ip));
619                 ip += 2;
620                 break;
621         case MonoShortInlineVar:
622                 g_string_append_printf (str, "%d", (*ip));
623                 ip ++;
624                 break;
625         case MonoInlineBrTarget:
626                 sval = read32 (ip);
627                 ip += 4;
628                 if (dh->label_target)
629                         g_string_append_printf (str, dh->label_target, ip + sval - il_code);
630                 else
631                         g_string_append_printf (str, "%d", sval);
632                 break;
633         case MonoShortInlineBrTarget:
634                 sval = *(const signed char*)ip;
635                 ip ++;
636                 if (dh->label_target)
637                         g_string_append_printf (str, dh->label_target, ip + sval - il_code);
638                 else
639                         g_string_append_printf (str, "%d", sval);
640                 break;
641         case MonoInlineSwitch: {
642                 const unsigned char *end;
643                 sval = read32 (ip);
644                 ip += 4;
645                 end = ip + sval * 4;
646                 g_string_append_c (str, '(');
647                 for (i = 0; i < sval; ++i) {
648                         if (i > 0)
649                                 g_string_append (str, ", ");
650                         label = read32 (ip);
651                         if (dh->label_target)
652                                 g_string_append_printf (str, dh->label_target, end + label - il_code);
653                         else
654                                 g_string_append_printf (str, "%d", label);
655                         ip += 4;
656                 }
657                 g_string_append_c (str, ')');
658                 break;
659         }
660         case MonoInlineR: {
661                 double r;
662                 readr8 (ip, &r);
663                 g_string_append_printf (str, "%g", r);
664                 ip += 8;
665                 break;
666         }
667         case MonoShortInlineR: {
668                 float r;
669                 readr4 (ip, &r);
670                 g_string_append_printf (str, "%g", r);
671                 ip += 4;
672                 break;
673         }
674         case MonoInlineI:
675                 g_string_append_printf (str, "%d", (gint32)read32 (ip));
676                 ip += 4;
677                 break;
678         case MonoShortInlineI:
679                 g_string_append_printf (str, "%d", *(const signed char*)ip);
680                 ip ++;
681                 break;
682         case MonoInlineI8:
683                 ip += 8;
684                 break;
685         default:
686                 g_assert_not_reached ();
687         }
688         if (dh->newline)
689                 g_string_append (str, dh->newline);
690
691         mono_metadata_free_mh (header);
692         return ip;
693 }
694
695 static MonoDisHelper
696 default_dh = {
697         "\n",
698         "IL_%04x: ", /* label_format */
699         "IL_%04x", /* label_target */
700         NULL, /* indenter */
701         NULL, /* tokener */
702         NULL  /* user data */
703 };
704
705 char*
706 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
707 {
708         char *result;
709         GString *res = g_string_new ("");
710
711         if (!dh)
712                 dh = &default_dh;
713         /* set ip + 2 as the end: this is just a debugging method */
714         ip = dis_one (res, dh, method, ip, ip + 2);
715         if (endp)
716                 *endp = ip;
717         
718         result = res->str;
719         g_string_free (res, FALSE);
720         return result;
721 }
722
723 char*
724 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
725 {
726         char *result;
727         GString *res = g_string_new ("");
728
729         if (!dh)
730                 dh = &default_dh;
731         while (ip < end) {
732                 ip = dis_one (res, dh, method, ip, end);
733         }
734         
735         result = res->str;
736         g_string_free (res, FALSE);
737         return result;
738 }
739
740 char *
741 mono_field_full_name (MonoClassField *field)
742 {
743         char *res;
744         const char *nspace = field->parent->name_space;
745
746         res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
747                                                    field->parent->name, mono_field_get_name (field));
748
749         return res;
750 }
751
752 char *
753 mono_method_get_name_full (MonoMethod *method, gboolean signature, MonoTypeNameFormat format)
754 {
755         char *res;
756         char wrapper [64];
757         char *klass_desc;
758         char *inst_desc = NULL;
759
760         if (format == MONO_TYPE_NAME_FORMAT_IL)
761                 klass_desc = mono_type_full_name (&method->klass->byval_arg);
762         else
763                 klass_desc = mono_type_get_name_full (&method->klass->byval_arg, format);
764
765         if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
766                 GString *str = g_string_new ("");
767                 if (format == MONO_TYPE_NAME_FORMAT_IL)
768                         g_string_append (str, "<");
769                 else
770                         g_string_append (str, "[");
771                 ginst_get_desc (str, ((MonoMethodInflated*)method)->context.method_inst);
772                 if (format == MONO_TYPE_NAME_FORMAT_IL)
773                         g_string_append_c (str, '>');
774                 else
775                         g_string_append_c (str, ']');
776
777                 inst_desc = str->str;
778                 g_string_free (str, FALSE);
779         } else if (method->is_generic) {
780                 MonoGenericContainer *container = mono_method_get_generic_container (method);
781
782                 GString *str = g_string_new ("");
783                 if (format == MONO_TYPE_NAME_FORMAT_IL)
784                         g_string_append (str, "<");
785                 else
786                         g_string_append (str, "[");
787                 ginst_get_desc (str, container->context.method_inst);
788                 if (format == MONO_TYPE_NAME_FORMAT_IL)
789                         g_string_append_c (str, '>');
790                 else
791                         g_string_append_c (str, ']');
792
793                 inst_desc = str->str;
794                 g_string_free (str, FALSE);
795         }
796
797         if (method->wrapper_type != MONO_WRAPPER_NONE)
798                 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
799         else
800                 strcpy (wrapper, "");
801
802         if (signature) {
803                 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
804
805                 if (method->wrapper_type != MONO_WRAPPER_NONE)
806                         sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
807                 else
808                         strcpy (wrapper, "");
809                 res = g_strdup_printf ("%s%s:%s%s (%s)", wrapper, klass_desc, 
810                                                            method->name, inst_desc ? inst_desc : "", tmpsig);
811                 g_free (tmpsig);
812         } else {
813                 res = g_strdup_printf ("%s%s:%s%s", wrapper, klass_desc,
814                                                            method->name, inst_desc ? inst_desc : "");
815         }
816
817         g_free (klass_desc);
818         g_free (inst_desc);
819
820         return res;
821 }
822
823 char *
824 mono_method_full_name (MonoMethod *method, gboolean signature)
825 {
826         return mono_method_get_name_full (method, signature, MONO_TYPE_NAME_FORMAT_IL);
827 }
828
829 static const char*
830 print_name_space (MonoClass *klass)
831 {
832         if (klass->nested_in) {
833                 print_name_space (klass->nested_in);
834                 g_print ("%s", klass->nested_in->name);
835                 return "/";
836         }
837         if (klass->name_space [0]) {
838                 g_print ("%s", klass->name_space);
839                 return ".";
840         }
841         return "";
842 }
843
844 /**
845  * mono_object_describe:
846  *
847  * Prints to stdout a small description of the object @obj.
848  * For use in a debugger.
849  */
850 void
851 mono_object_describe (MonoObject *obj)
852 {
853         MonoClass* klass;
854         const char* sep;
855         if (!obj) {
856                 g_print ("(null)\n");
857                 return;
858         }
859         klass = mono_object_class (obj);
860         if (klass == mono_defaults.string_class) {
861                 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
862                 if (strlen (utf8) > 60) {
863                         utf8 [57] = '.';
864                         utf8 [58] = '.';
865                         utf8 [59] = '.';
866                         utf8 [60] = 0;
867                 }
868                 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
869                 g_free (utf8);
870         } else if (klass->rank) {
871                 MonoArray *array = (MonoArray*)obj;
872                 sep = print_name_space (klass);
873                 g_print ("%s%s", sep, klass->name);
874                 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, (int)mono_array_length (array));
875         } else {
876                 sep = print_name_space (klass);
877                 g_print ("%s%s", sep, klass->name);
878                 g_print (" object at %p (klass: %p)\n", obj, klass);
879         }
880
881 }
882
883 static void
884 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
885 {
886         MonoType *type;
887         g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, mono_field_get_name (field));
888         type = mono_type_get_underlying_type (field->type);
889
890         switch (type->type) {
891         case MONO_TYPE_I:
892         case MONO_TYPE_U:
893         case MONO_TYPE_PTR:
894         case MONO_TYPE_FNPTR:
895                 g_print ("%p\n", *(const void**)field_ptr);
896                 break;
897         case MONO_TYPE_STRING:
898         case MONO_TYPE_SZARRAY:
899         case MONO_TYPE_CLASS:
900         case MONO_TYPE_OBJECT:
901         case MONO_TYPE_ARRAY:
902                 mono_object_describe (*(MonoObject**)field_ptr);
903                 break;
904         case MONO_TYPE_GENERICINST:
905                 if (!mono_type_generic_inst_is_valuetype (type)) {
906                         mono_object_describe (*(MonoObject**)field_ptr);
907                         break;
908                 } else {
909                         /* fall through */
910                 }
911         case MONO_TYPE_VALUETYPE: {
912                 MonoClass *k = mono_class_from_mono_type (type);
913                 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
914                 break;
915         }
916         case MONO_TYPE_I1:
917                 g_print ("%d\n", *(gint8*)field_ptr);
918                 break;
919         case MONO_TYPE_U1:
920                 g_print ("%d\n", *(guint8*)field_ptr);
921                 break;
922         case MONO_TYPE_I2:
923                 g_print ("%d\n", *(gint16*)field_ptr);
924                 break;
925         case MONO_TYPE_U2:
926                 g_print ("%d\n", *(guint16*)field_ptr);
927                 break;
928         case MONO_TYPE_I4:
929                 g_print ("%d\n", *(gint32*)field_ptr);
930                 break;
931         case MONO_TYPE_U4:
932                 g_print ("%u\n", *(guint32*)field_ptr);
933                 break;
934         case MONO_TYPE_I8:
935                 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
936                 break;
937         case MONO_TYPE_U8:
938                 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
939                 break;
940         case MONO_TYPE_R4:
941                 g_print ("%f\n", *(gfloat*)field_ptr);
942                 break;
943         case MONO_TYPE_R8:
944                 g_print ("%f\n", *(gdouble*)field_ptr);
945                 break;
946         case MONO_TYPE_BOOLEAN:
947                 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
948                 break;
949         case MONO_TYPE_CHAR:
950                 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
951                 break;
952         default:
953                 g_assert_not_reached ();
954                 break;
955         }
956 }
957
958 static void
959 objval_describe (MonoClass *klass, const char *addr)
960 {
961         MonoClassField *field;
962         MonoClass *p;
963         const char *field_ptr;
964         gssize type_offset = 0;
965
966         if (klass->valuetype)
967                 type_offset = -sizeof (MonoObject);
968
969         for (p = klass; p != NULL; p = p->parent) {
970                 gpointer iter = NULL;
971                 int printed_header = FALSE;
972                 while ((field = mono_class_get_fields (p, &iter))) {
973                         if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
974                                 continue;
975
976                         if (p != klass && !printed_header) {
977                                 const char *sep;
978                                 g_print ("In class ");
979                                 sep = print_name_space (p);
980                                 g_print ("%s%s:\n", sep, p->name);
981                                 printed_header = TRUE;
982                         }
983                         field_ptr = (const char*)addr + field->offset + type_offset;
984
985                         print_field_value (field_ptr, field, type_offset);
986                 }
987         }
988 }
989
990 /**
991  * mono_object_describe_fields:
992  *
993  * Prints to stdout a small description of each field of the object @obj.
994  * For use in a debugger.
995  */
996 void
997 mono_object_describe_fields (MonoObject *obj)
998 {
999         MonoClass *klass = mono_object_class (obj);
1000         objval_describe (klass, (char*)obj);
1001 }
1002
1003 /**
1004  * mono_value_describe_fields:
1005  *
1006  * Prints to stdout a small description of each field of the value type
1007  * stored at @addr of type @klass.
1008  * For use in a debugger.
1009  */
1010 void
1011 mono_value_describe_fields (MonoClass* klass, const char* addr)
1012 {
1013         objval_describe (klass, addr);
1014 }
1015
1016 /**
1017  * mono_class_describe_statics:
1018  *
1019  * Prints to stdout a small description of each static field of the type @klass
1020  * in the current application domain.
1021  * For use in a debugger.
1022  */
1023 void
1024 mono_class_describe_statics (MonoClass* klass)
1025 {
1026         MonoClassField *field;
1027         MonoClass *p;
1028         const char *field_ptr;
1029         MonoVTable *vtable = mono_class_vtable_full (mono_domain_get (), klass, FALSE);
1030         const char *addr;
1031
1032         if (!vtable)
1033                 return;
1034         if (!(addr = (const char *)mono_vtable_get_static_field_data (vtable)))
1035                 return;
1036
1037         for (p = klass; p != NULL; p = p->parent) {
1038                 gpointer iter = NULL;
1039                 while ((field = mono_class_get_fields (p, &iter))) {
1040                         if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1041                                 continue;
1042                         if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
1043                                 continue;
1044
1045                         field_ptr = (const char*)addr + field->offset;
1046
1047                         print_field_value (field_ptr, field, 0);
1048                 }
1049         }
1050 }
1051
1052 /**
1053  * mono_print_method_code
1054  * @MonoMethod: a pointer to the method
1055  *
1056  * This method is used from a debugger to print the code of the method.
1057  *
1058  * This prints the IL code of the method in the standard output.
1059  */
1060 void
1061 mono_method_print_code (MonoMethod *method)
1062 {
1063         char *code;
1064         MonoMethodHeader *header = mono_method_get_header (method);
1065         if (!header) {
1066                 printf ("METHOD HEADER NOT FOUND\n");
1067                 return;
1068         }
1069         code = mono_disasm_code (0, method, header->code, header->code + header->code_size);
1070         printf ("CODE FOR %s:\n%s\n", mono_method_full_name (method, TRUE), code);
1071         g_free (code);
1072 }