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