2 * console-io.c: ConsoleDriver internal calls
5 * Mono Project (http://www.mono-project.com)
7 * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
11 #include "mono/metadata/tokentype.h"
12 #include "mono/metadata/opcodes.h"
13 #include "mono/metadata/metadata-internals.h"
14 #include "mono/metadata/class-internals.h"
15 #include "mono/metadata/mono-endian.h"
16 #include "mono/metadata/debug-helpers.h"
17 #include "mono/metadata/tabledefs.h"
18 #include "mono/metadata/appdomain.h"
20 struct MonoMethodDesc {
26 gboolean include_namespace, klass_glob, name_glob;
29 #ifdef HAVE_ARRAY_ELEM_INIT
30 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
31 #define MSGSTRFIELD1(line) str##line
32 static const struct msgstr_t {
33 #define WRAPPER(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
34 #include "wrapper-types.h"
37 #define WRAPPER(a,b) b,
38 #include "wrapper-types.h"
41 static const gint16 opidx [] = {
42 #define WRAPPER(a,b) [MONO_WRAPPER_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
43 #include "wrapper-types.h"
48 wrapper_type_to_str (guint32 wrapper_type)
50 g_assert (wrapper_type < MONO_WRAPPER_NUM);
52 return (const char*)&opstr + opidx [wrapper_type];
56 #define WRAPPER(a,b) b,
57 static const char* const
58 wrapper_type_names [MONO_WRAPPER_NUM + 1] = {
59 #include "wrapper-types.h"
64 wrapper_type_to_str (guint32 wrapper_type)
66 g_assert (wrapper_type < MONO_WRAPPER_NUM);
68 return wrapper_type_names [wrapper_type];
74 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
77 g_string_append (res, "Unknown");
80 if (class->nested_in) {
81 append_class_name (res, class->nested_in, include_namespace);
82 g_string_append_c (res, '/');
84 if (include_namespace && *(class->name_space))
85 g_string_append_printf (res, "%s.", class->name_space);
86 g_string_append_printf (res, "%s", class->name);
90 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace)
96 g_string_append (res, "void"); break;
98 g_string_append (res, "char"); break;
99 case MONO_TYPE_BOOLEAN:
100 g_string_append (res, "bool"); break;
102 g_string_append (res, "byte"); break;
104 g_string_append (res, "sbyte"); break;
106 g_string_append (res, "uint16"); break;
108 g_string_append (res, "int16"); break;
110 g_string_append (res, "uint"); break;
112 g_string_append (res, "int"); break;
114 g_string_append (res, "ulong"); break;
116 g_string_append (res, "long"); break;
117 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
118 g_string_append (res, "*()"); break;
120 g_string_append (res, "uintptr"); break;
122 g_string_append (res, "intptr"); break;
124 g_string_append (res, "single"); break;
126 g_string_append (res, "double"); break;
127 case MONO_TYPE_STRING:
128 g_string_append (res, "string"); break;
129 case MONO_TYPE_OBJECT:
130 g_string_append (res, "object"); break;
132 mono_type_get_desc (res, type->data.type, include_namespace);
133 g_string_append_c (res, '*');
135 case MONO_TYPE_ARRAY:
136 mono_type_get_desc (res, &type->data.array->eklass->byval_arg, include_namespace);
137 g_string_append_printf (res, "[%d]", type->data.array->rank);
139 case MONO_TYPE_SZARRAY:
140 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
141 g_string_append (res, "[]");
143 case MONO_TYPE_CLASS:
144 case MONO_TYPE_VALUETYPE:
145 append_class_name (res, type->data.klass, include_namespace);
147 case MONO_TYPE_GENERICINST: {
148 MonoGenericContext *context;
150 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
151 g_string_append (res, "<");
152 context = &type->data.generic_class->context;
153 if (context->class_inst) {
154 for (i = 0; i < context->class_inst->type_argc; ++i) {
156 g_string_append (res, ", ");
157 mono_type_get_desc (res, context->class_inst->type_argv [i], include_namespace);
160 if (context->method_inst) {
161 if (context->class_inst)
162 g_string_append (res, "; ");
163 for (i = 0; i < context->method_inst->type_argc; ++i) {
165 g_string_append (res, ", ");
166 mono_type_get_desc (res, context->method_inst->type_argv [i], include_namespace);
169 g_string_append (res, ">");
174 if (type->data.generic_param) {
175 MonoGenericParamInfo *info = mono_generic_param_info (type->data.generic_param);
177 g_string_append (res, info->name);
179 g_string_append_printf (res, "%s%d", type->type == MONO_TYPE_VAR ? "!" : "!!", mono_generic_param_num (type->data.generic_param));
181 g_string_append (res, "<unknown>");
184 case MONO_TYPE_TYPEDBYREF:
185 g_string_append (res, "typedbyref");
191 g_string_append_c (res, '&');
195 mono_type_full_name (MonoType *type)
199 str = g_string_new ("");
200 mono_type_get_desc (str, type, TRUE);
201 return g_string_free (str, FALSE);
205 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
212 return g_strdup ("<invalid signature>");
214 res = g_string_new ("");
216 for (i = 0; i < sig->param_count; ++i) {
218 g_string_append_c (res, ',');
219 mono_type_get_desc (res, sig->params [i], include_namespace);
222 g_string_free (res, FALSE);
227 ginst_get_desc (GString *str, MonoGenericInst *ginst)
231 for (i = 0; i < ginst->type_argc; ++i) {
233 g_string_append (str, ", ");
234 mono_type_get_desc (str, ginst->type_argv [i], TRUE);
239 mono_context_get_desc (MonoGenericContext *context)
244 str = g_string_new ("");
245 g_string_append (str, "<");
247 if (context->class_inst)
248 ginst_get_desc (str, context->class_inst);
249 if (context->method_inst) {
250 if (context->class_inst)
251 g_string_append (str, "; ");
252 ginst_get_desc (str, context->method_inst);
255 g_string_append (str, ">");
256 res = g_strdup (str->str);
257 g_string_free (str, TRUE);
262 * mono_method_desc_new:
263 * @name: the method name.
264 * @include_namespace: whether the name includes a namespace or not.
266 * Creates a method description for @name, which conforms to the following
269 * [namespace.]classname:methodname[(args...)]
271 * in all the loaded assemblies.
273 * Both classname and methodname can contain '*' which matches anything.
275 * Returns: a parsed representation of the method description.
278 mono_method_desc_new (const char *name, gboolean include_namespace)
280 MonoMethodDesc *result;
281 char *class_name, *class_nspace, *method_name, *use_args, *end;
284 class_nspace = g_strdup (name);
285 use_args = strchr (class_nspace, '(');
288 end = strchr (use_args, ')');
290 g_free (class_nspace);
295 method_name = strrchr (class_nspace, ':');
297 g_free (class_nspace);
301 /* allow two :: to separate the method name */
302 if (*method_name == ':')
304 class_name = strrchr (class_nspace, '.');
309 class_name = class_nspace;
312 result = g_new0 (MonoMethodDesc, 1);
313 result->include_namespace = include_namespace;
314 result->name = method_name;
315 result->klass = class_name;
316 result->namespace = use_namespace? class_nspace: NULL;
317 result->args = use_args? use_args: NULL;
318 if (strstr (result->name, "*"))
319 result->name_glob = TRUE;
320 if (strstr (result->klass, "*"))
321 result->klass_glob = TRUE;
325 result->num_args = 1;
337 mono_method_desc_from_method (MonoMethod *method)
339 MonoMethodDesc *result;
341 result = g_new0 (MonoMethodDesc, 1);
342 result->include_namespace = TRUE;
343 result->name = g_strdup (method->name);
344 result->klass = g_strdup (method->klass->name);
345 result->namespace = g_strdup (method->klass->name_space);
351 * mono_method_desc_free:
352 * @desc: method description to be released
354 * Releases the MonoMethodDesc object @desc.
357 mono_method_desc_free (MonoMethodDesc *desc)
360 g_free (desc->namespace);
361 else if (desc->klass)
362 g_free (desc->klass);
367 * namespace and class are supposed to match already if this function is used.
370 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
375 name_match = strcmp (desc->name, method->name) == 0;
377 if (!name_match && desc->name_glob)
378 name_match = g_pattern_match_simple (desc->name, method->name);
384 if (desc->num_args != mono_method_signature (method)->param_count)
386 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
387 if (strcmp (sig, desc->args)) {
396 my_strrchr (const char *str, char ch, int *len)
400 for (pos = (*len)-1; pos >= 0; pos--) {
412 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
416 if (desc->klass_glob && !strcmp (desc->klass, "*"))
419 if (desc->klass_glob && g_pattern_match_simple (desc->klass, klass->name))
422 p = my_strrchr (desc->klass, '/', &pos);
424 if (strncmp (desc->klass, klass->name, pos))
426 if (desc->namespace && strcmp (desc->namespace, klass->name_space))
431 if (strcmp (p+1, klass->name))
433 if (!klass->nested_in)
436 return match_class (desc, pos, klass->nested_in);
440 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
442 if (!match_class (desc, strlen (desc->klass), method->klass))
445 return mono_method_desc_match (desc, method);
449 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
452 gpointer iter = NULL;
454 while ((m = mono_class_get_methods (klass, &iter)))
455 if (mono_method_desc_match (desc, m))
461 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
464 const MonoTableInfo *tdef;
465 const MonoTableInfo *methods;
469 if (desc->namespace && desc->klass) {
470 klass = mono_class_from_name (image, desc->namespace, desc->klass);
473 return mono_method_desc_search_in_class (desc, klass);
476 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
477 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
478 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
479 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
480 const char *n = mono_metadata_string_heap (image, token);
482 if (strcmp (n, desc->name))
484 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
485 if (mono_method_desc_full_match (desc, method))
491 static const unsigned char*
492 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
494 MonoMethodHeader *header = mono_method_get_header (method);
495 const MonoOpcode *opcode;
496 guint32 label, token;
500 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
502 label = ip - il_code;
504 tmp = dh->indenter (dh, method, label);
505 g_string_append (str, tmp);
508 if (dh->label_format)
509 g_string_append_printf (str, dh->label_format, label);
511 i = mono_opcode_value (&ip, end);
513 opcode = &mono_opcodes [i];
514 g_string_append_printf (str, "%-10s", mono_opcode_name (i));
516 switch (opcode->argument) {
520 case MonoInlineField:
521 case MonoInlineMethod:
526 tmp = dh->tokener (dh, method, token);
527 g_string_append (str, tmp);
530 g_string_append_printf (str, "0x%08x", token);
534 case MonoInlineString: {
540 if (!method->klass->image->dynamic) {
542 blob = mono_metadata_user_string (method->klass->image, mono_metadata_token_index (token));
544 len2 = mono_metadata_decode_blob_size (blob, &blob);
547 #ifdef NO_UNALIGNED_ACCESS
548 /* The blob might not be 2 byte aligned */
549 blob2 = g_malloc ((len2 * 2) + 1);
550 memcpy (blob2, blob, len2 * 2);
555 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
557 guint16 *buf = g_new (guint16, len2);
560 for (i = 0; i < len2; ++i)
561 buf [i] = GUINT16_FROM_LE (((guint16*)blob2) [i]);
562 s = g_utf16_to_utf8 (buf, len2, NULL, NULL, NULL);
566 s = g_utf16_to_utf8 ((gunichar2*)blob2, len2, NULL, NULL, NULL);
569 g_string_append_printf (str, "\"%s\"", s);
578 g_string_append_printf (str, "%d", read16 (ip));
581 case MonoShortInlineVar:
582 g_string_append_printf (str, "%d", (*ip));
585 case MonoInlineBrTarget:
588 if (dh->label_target)
589 g_string_append_printf (str, dh->label_target, ip + sval - il_code);
591 g_string_append_printf (str, "%d", sval);
593 case MonoShortInlineBrTarget:
594 sval = *(const signed char*)ip;
596 if (dh->label_target)
597 g_string_append_printf (str, dh->label_target, ip + sval - il_code);
599 g_string_append_printf (str, "%d", sval);
601 case MonoInlineSwitch: {
602 const unsigned char *end;
606 g_string_append_c (str, '(');
607 for (i = 0; i < sval; ++i) {
609 g_string_append (str, ", ");
611 if (dh->label_target)
612 g_string_append_printf (str, dh->label_target, end + label - il_code);
614 g_string_append_printf (str, "%d", label);
617 g_string_append_c (str, ')');
623 g_string_append_printf (str, "%g", r);
627 case MonoShortInlineR: {
630 g_string_append_printf (str, "%g", r);
635 g_string_append_printf (str, "%d", (gint32)read32 (ip));
638 case MonoShortInlineI:
639 g_string_append_printf (str, "%d", *(const signed char*)ip);
646 g_assert_not_reached ();
649 g_string_append (str, dh->newline);
657 "IL_%04x: ", /* label_format */
658 "IL_%04x", /* label_target */
665 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
668 GString *res = g_string_new ("");
672 /* set ip + 2 as the end: this is just a debugging method */
673 ip = dis_one (res, dh, method, ip, ip + 2);
678 g_string_free (res, FALSE);
683 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
686 GString *res = g_string_new ("");
691 ip = dis_one (res, dh, method, ip, end);
695 g_string_free (res, FALSE);
700 mono_field_full_name (MonoClassField *field)
703 const char *nspace = field->parent->name_space;
705 res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
706 field->parent->name, mono_field_get_name (field));
712 mono_method_full_name (MonoMethod *method, gboolean signature)
716 char *klass_desc = mono_type_full_name (&method->klass->byval_arg);
717 char *inst_desc = NULL;
719 if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
720 GString *str = g_string_new ("");
721 g_string_append (str, "<");
722 ginst_get_desc (str, ((MonoMethodInflated*)method)->context.method_inst);
723 g_string_append (str, ">");
725 inst_desc = str->str;
726 g_string_free (str, FALSE);
727 } else if (method->is_generic) {
728 MonoGenericContainer *container = mono_method_get_generic_container (method);
730 GString *str = g_string_new ("");
731 g_string_append (str, "<");
732 ginst_get_desc (str, container->context.method_inst);
733 g_string_append (str, ">");
735 inst_desc = str->str;
736 g_string_free (str, FALSE);
739 if (method->wrapper_type != MONO_WRAPPER_NONE)
740 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
742 strcpy (wrapper, "");
745 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
747 if (method->wrapper_type != MONO_WRAPPER_NONE)
748 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
750 strcpy (wrapper, "");
751 res = g_strdup_printf ("%s%s:%s%s (%s)", wrapper, klass_desc,
752 method->name, inst_desc ? inst_desc : "", tmpsig);
755 res = g_strdup_printf ("%s%s:%s%s", wrapper, klass_desc,
756 method->name, inst_desc ? inst_desc : "");
766 print_name_space (MonoClass *klass)
768 if (klass->nested_in) {
769 print_name_space (klass->nested_in);
770 g_print ("%s", klass->nested_in->name);
773 if (klass->name_space [0]) {
774 g_print ("%s", klass->name_space);
781 * mono_object_describe:
783 * Prints to stdout a small description of the object @obj.
784 * For use in a debugger.
787 mono_object_describe (MonoObject *obj)
792 g_print ("(null)\n");
795 klass = mono_object_class (obj);
796 if (klass == mono_defaults.string_class) {
797 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
798 if (strlen (utf8) > 60) {
804 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
806 } else if (klass->rank) {
807 MonoArray *array = (MonoArray*)obj;
808 sep = print_name_space (klass);
809 g_print ("%s%s", sep, klass->name);
810 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
812 sep = print_name_space (klass);
813 g_print ("%s%s", sep, klass->name);
814 g_print (" object at %p (klass: %p)\n", obj, klass);
820 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
823 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, mono_field_get_name (field));
824 type = mono_type_get_underlying_type (field->type);
826 switch (type->type) {
830 case MONO_TYPE_FNPTR:
831 g_print ("%p\n", *(const void**)field_ptr);
833 case MONO_TYPE_STRING:
834 case MONO_TYPE_SZARRAY:
835 case MONO_TYPE_CLASS:
836 case MONO_TYPE_OBJECT:
837 case MONO_TYPE_ARRAY:
838 mono_object_describe (*(MonoObject**)field_ptr);
840 case MONO_TYPE_GENERICINST:
841 if (!mono_type_generic_inst_is_valuetype (type)) {
842 mono_object_describe (*(MonoObject**)field_ptr);
847 case MONO_TYPE_VALUETYPE: {
848 MonoClass *k = mono_class_from_mono_type (type);
849 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
853 g_print ("%d\n", *(gint8*)field_ptr);
856 g_print ("%d\n", *(guint8*)field_ptr);
859 g_print ("%d\n", *(gint16*)field_ptr);
862 g_print ("%d\n", *(guint16*)field_ptr);
865 g_print ("%d\n", *(gint32*)field_ptr);
868 g_print ("%u\n", *(guint32*)field_ptr);
871 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
874 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
877 g_print ("%f\n", *(gfloat*)field_ptr);
880 g_print ("%f\n", *(gdouble*)field_ptr);
882 case MONO_TYPE_BOOLEAN:
883 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
886 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
889 g_assert_not_reached ();
895 objval_describe (MonoClass *class, const char *addr)
897 MonoClassField *field;
899 const char *field_ptr;
900 gssize type_offset = 0;
901 if (class->valuetype)
902 type_offset = -sizeof (MonoObject);
904 for (p = class; p != NULL; p = p->parent) {
905 gpointer iter = NULL;
906 int printed_header = FALSE;
907 while ((field = mono_class_get_fields (p, &iter))) {
908 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
911 if (p != class && !printed_header) {
913 g_print ("In class ");
914 sep = print_name_space (p);
915 g_print ("%s%s:\n", sep, p->name);
916 printed_header = TRUE;
918 field_ptr = (const char*)addr + field->offset + type_offset;
920 print_field_value (field_ptr, field, type_offset);
926 * mono_object_describe_fields:
928 * Prints to stdout a small description of each field of the object @obj.
929 * For use in a debugger.
932 mono_object_describe_fields (MonoObject *obj)
934 MonoClass *class = mono_object_class (obj);
935 objval_describe (class, (char*)obj);
939 * mono_value_describe_fields:
941 * Prints to stdout a small description of each field of the value type
942 * stored at @addr of type @klass.
943 * For use in a debugger.
946 mono_value_describe_fields (MonoClass* klass, const char* addr)
948 objval_describe (klass, addr);
952 * mono_class_describe_statics:
954 * Prints to stdout a small description of each static field of the type @klass
955 * in the current application domain.
956 * For use in a debugger.
959 mono_class_describe_statics (MonoClass* klass)
961 MonoClassField *field;
963 const char *field_ptr;
964 MonoVTable *vtable = mono_class_vtable_full (mono_domain_get (), klass, FALSE);
969 if (!(addr = vtable->data))
972 for (p = klass; p != NULL; p = p->parent) {
973 gpointer iter = NULL;
974 while ((field = mono_class_get_fields (p, &iter))) {
975 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
977 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
980 field_ptr = (const char*)addr + field->offset;
982 print_field_value (field_ptr, field, 0);