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