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