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