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>");
188 g_string_append_c (res, '&');
192 mono_type_full_name (MonoType *type)
197 str = g_string_new ("");
198 mono_type_get_desc (str, type, TRUE);
200 res = g_strdup (str->str);
201 g_string_free (str, TRUE);
206 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
210 GString *res = g_string_new ("");
212 for (i = 0; i < sig->param_count; ++i) {
214 g_string_append_c (res, ',');
215 mono_type_get_desc (res, sig->params [i], include_namespace);
218 g_string_free (res, FALSE);
223 ginst_get_desc (GString *str, MonoGenericInst *ginst)
227 for (i = 0; i < ginst->type_argc; ++i) {
229 g_string_append (str, ", ");
230 mono_type_get_desc (str, ginst->type_argv [i], TRUE);
235 mono_context_get_desc (MonoGenericContext *context)
240 str = g_string_new ("");
241 g_string_append (str, "<");
243 if (context->class_inst)
244 ginst_get_desc (str, context->class_inst);
245 if (context->method_inst) {
246 if (context->class_inst)
247 g_string_append (str, "; ");
248 ginst_get_desc (str, context->method_inst);
251 g_string_append (str, ">");
252 res = g_strdup (str->str);
253 g_string_free (str, TRUE);
258 * mono_method_desc_new:
259 * @name: the method name.
260 * @include_namespace: whether the name includes a namespace or not.
262 * Creates a method description for @name, which conforms to the following
265 * [namespace.]classname:methodname[(args...)]
267 * in all the loaded assemblies.
269 * Both classname and methodname can contain '*' which matches anything.
271 * Returns: a parsed representation of the method description.
274 mono_method_desc_new (const char *name, gboolean include_namespace)
276 MonoMethodDesc *result;
277 char *class_name, *class_nspace, *method_name, *use_args, *end;
280 class_nspace = g_strdup (name);
281 use_args = strchr (class_nspace, '(');
284 end = strchr (use_args, ')');
286 g_free (class_nspace);
291 method_name = strrchr (class_nspace, ':');
293 g_free (class_nspace);
297 /* allow two :: to separate the method name */
298 if (*method_name == ':')
300 class_name = strrchr (class_nspace, '.');
305 class_name = class_nspace;
308 result = g_new0 (MonoMethodDesc, 1);
309 result->include_namespace = include_namespace;
310 result->name = method_name;
311 result->klass = class_name;
312 result->namespace = use_namespace? class_nspace: NULL;
313 result->args = use_args? use_args: NULL;
314 if (strstr (result->name, "*"))
315 result->name_glob = TRUE;
316 if (strstr (result->klass, "*"))
317 result->klass_glob = TRUE;
321 result->num_args = 1;
333 mono_method_desc_from_method (MonoMethod *method)
335 MonoMethodDesc *result;
337 result = g_new0 (MonoMethodDesc, 1);
338 result->include_namespace = TRUE;
339 result->name = g_strdup (method->name);
340 result->klass = g_strdup (method->klass->name);
341 result->namespace = g_strdup (method->klass->name_space);
347 * mono_method_desc_free:
348 * @desc: method description to be released
350 * Releases the MonoMethodDesc object @desc.
353 mono_method_desc_free (MonoMethodDesc *desc)
356 g_free (desc->namespace);
357 else if (desc->klass)
358 g_free (desc->klass);
363 * namespace and class are supposed to match already if this function is used.
366 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
371 name_match = strcmp (desc->name, method->name) == 0;
373 if (!name_match && desc->name_glob)
374 name_match = g_pattern_match_simple (desc->name, method->name);
380 if (desc->num_args != mono_method_signature (method)->param_count)
382 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
383 if (strcmp (sig, desc->args)) {
392 my_strrchr (const char *str, char ch, int *len)
396 for (pos = (*len)-1; pos >= 0; pos--) {
408 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
412 if (desc->klass_glob && !strcmp (desc->klass, "*"))
415 if (desc->klass_glob && g_pattern_match_simple (desc->klass, klass->name))
418 p = my_strrchr (desc->klass, '/', &pos);
420 if (strncmp (desc->klass, klass->name, pos))
422 if (desc->namespace && strcmp (desc->namespace, klass->name_space))
427 if (strcmp (p+1, klass->name))
429 if (!klass->nested_in)
432 return match_class (desc, pos, klass->nested_in);
436 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
438 if (!match_class (desc, strlen (desc->klass), method->klass))
441 return mono_method_desc_match (desc, method);
445 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
448 gpointer iter = NULL;
450 while ((m = mono_class_get_methods (klass, &iter)))
451 if (mono_method_desc_match (desc, m))
457 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
460 const MonoTableInfo *tdef;
461 const MonoTableInfo *methods;
465 if (desc->namespace && desc->klass) {
466 klass = mono_class_from_name (image, desc->namespace, desc->klass);
469 return mono_method_desc_search_in_class (desc, klass);
472 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
473 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
474 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
475 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
476 const char *n = mono_metadata_string_heap (image, token);
478 if (strcmp (n, desc->name))
480 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
481 if (mono_method_desc_full_match (desc, method))
487 static const unsigned char*
488 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
490 MonoMethodHeader *header = mono_method_get_header (method);
491 const MonoOpcode *opcode;
492 guint32 label, token;
496 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
498 label = ip - il_code;
500 tmp = dh->indenter (dh, method, label);
501 g_string_append (str, tmp);
504 if (dh->label_format)
505 g_string_append_printf (str, dh->label_format, label);
507 i = mono_opcode_value (&ip, end);
509 opcode = &mono_opcodes [i];
510 g_string_append_printf (str, "%-10s", mono_opcode_name (i));
512 switch (opcode->argument) {
516 case MonoInlineField:
517 case MonoInlineMethod:
522 tmp = dh->tokener (dh, method, token);
523 g_string_append (str, tmp);
526 g_string_append_printf (str, "0x%08x", token);
530 case MonoInlineString: {
536 if (!method->klass->image->dynamic) {
538 blob = mono_metadata_user_string (method->klass->image, mono_metadata_token_index (token));
540 len2 = mono_metadata_decode_blob_size (blob, &blob);
543 #ifdef NO_UNALIGNED_ACCESS
544 /* The blob might not be 2 byte aligned */
545 blob2 = g_malloc (len2 * 2);
546 memcpy (blob2, blob, len2 * 2);
551 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
553 guint16 *buf = g_new (guint16, len2);
556 for (i = 0; i < len2; ++i)
557 buf [i] = GUINT16_FROM_LE (((guint16*)blob2) [i]);
558 s = g_utf16_to_utf8 (buf, len2, NULL, NULL, NULL);
562 s = g_utf16_to_utf8 ((gunichar2*)blob2, len2, NULL, NULL, NULL);
565 g_string_append_printf (str, "\"%s\"", s);
574 g_string_append_printf (str, "%d", read16 (ip));
577 case MonoShortInlineVar:
578 g_string_append_printf (str, "%d", (*ip));
581 case MonoInlineBrTarget:
584 if (dh->label_target)
585 g_string_append_printf (str, dh->label_target, ip + sval - il_code);
587 g_string_append_printf (str, "%d", sval);
589 case MonoShortInlineBrTarget:
590 sval = *(const signed char*)ip;
592 if (dh->label_target)
593 g_string_append_printf (str, dh->label_target, ip + sval - il_code);
595 g_string_append_printf (str, "%d", sval);
597 case MonoInlineSwitch: {
598 const unsigned char *end;
602 g_string_append_c (str, '(');
603 for (i = 0; i < sval; ++i) {
605 g_string_append (str, ", ");
607 if (dh->label_target)
608 g_string_append_printf (str, dh->label_target, end + label - il_code);
610 g_string_append_printf (str, "%d", label);
613 g_string_append_c (str, ')');
619 g_string_append_printf (str, "%g", r);
623 case MonoShortInlineR: {
626 g_string_append_printf (str, "%g", r);
631 g_string_append_printf (str, "%d", (gint32)read32 (ip));
634 case MonoShortInlineI:
635 g_string_append_printf (str, "%d", *(const signed char*)ip);
642 g_assert_not_reached ();
645 g_string_append (str, dh->newline);
653 "IL_%04x: ", /* label_format */
654 "IL_%04x", /* label_target */
661 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
664 GString *res = g_string_new ("");
668 /* set ip + 2 as the end: this is just a debugging method */
669 ip = dis_one (res, dh, method, ip, ip + 2);
674 g_string_free (res, FALSE);
679 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
682 GString *res = g_string_new ("");
687 ip = dis_one (res, dh, method, ip, end);
691 g_string_free (res, FALSE);
696 mono_field_full_name (MonoClassField *field)
699 const char *nspace = field->parent->name_space;
701 res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
702 field->parent->name, mono_field_get_name (field));
708 mono_method_full_name (MonoMethod *method, gboolean signature)
712 char *klass_desc = mono_type_full_name (&method->klass->byval_arg);
713 char *inst_desc = NULL;
715 if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
716 GString *str = g_string_new ("");
717 g_string_append (str, "<");
718 ginst_get_desc (str, ((MonoMethodInflated*)method)->context.method_inst);
719 g_string_append (str, ">");
721 inst_desc = str->str;
722 g_string_free (str, FALSE);
723 } else if (method->is_generic) {
724 MonoGenericContainer *container = mono_method_get_generic_container (method);
726 GString *str = g_string_new ("");
727 g_string_append (str, "<");
728 ginst_get_desc (str, container->context.method_inst);
729 g_string_append (str, ">");
731 inst_desc = str->str;
732 g_string_free (str, FALSE);
735 if (method->wrapper_type != MONO_WRAPPER_NONE)
736 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
738 strcpy (wrapper, "");
741 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
743 if (method->wrapper_type != MONO_WRAPPER_NONE)
744 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
746 strcpy (wrapper, "");
747 res = g_strdup_printf ("%s%s:%s%s (%s)", wrapper, klass_desc,
748 method->name, inst_desc ? inst_desc : "", tmpsig);
751 res = g_strdup_printf ("%s%s:%s%s", wrapper, klass_desc,
752 method->name, inst_desc ? inst_desc : "");
762 print_name_space (MonoClass *klass)
764 if (klass->nested_in) {
765 print_name_space (klass->nested_in);
766 g_print ("%s", klass->nested_in->name);
769 if (klass->name_space [0]) {
770 g_print ("%s", klass->name_space);
777 * mono_object_describe:
779 * Prints to stdout a small description of the object @obj.
780 * For use in a debugger.
783 mono_object_describe (MonoObject *obj)
788 g_print ("(null)\n");
791 klass = mono_object_class (obj);
792 if (klass == mono_defaults.string_class) {
793 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
794 if (strlen (utf8) > 60) {
800 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
802 } else if (klass->rank) {
803 MonoArray *array = (MonoArray*)obj;
804 sep = print_name_space (klass);
805 g_print ("%s%s", sep, klass->name);
806 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
808 sep = print_name_space (klass);
809 g_print ("%s%s", sep, klass->name);
810 g_print (" object at %p (klass: %p)\n", obj, klass);
816 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
819 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, mono_field_get_name (field));
820 type = mono_type_get_underlying_type (field->type);
822 switch (type->type) {
826 case MONO_TYPE_FNPTR:
827 g_print ("%p\n", *(const void**)field_ptr);
829 case MONO_TYPE_STRING:
830 case MONO_TYPE_SZARRAY:
831 case MONO_TYPE_CLASS:
832 case MONO_TYPE_OBJECT:
833 case MONO_TYPE_ARRAY:
834 mono_object_describe (*(MonoObject**)field_ptr);
836 case MONO_TYPE_GENERICINST:
837 if (!mono_type_generic_inst_is_valuetype (type)) {
838 mono_object_describe (*(MonoObject**)field_ptr);
843 case MONO_TYPE_VALUETYPE: {
844 MonoClass *k = mono_class_from_mono_type (type);
845 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
849 g_print ("%d\n", *(gint8*)field_ptr);
852 g_print ("%d\n", *(guint8*)field_ptr);
855 g_print ("%d\n", *(gint16*)field_ptr);
858 g_print ("%d\n", *(guint16*)field_ptr);
861 g_print ("%d\n", *(gint32*)field_ptr);
864 g_print ("%u\n", *(guint32*)field_ptr);
867 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
870 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
873 g_print ("%f\n", *(gfloat*)field_ptr);
876 g_print ("%f\n", *(gdouble*)field_ptr);
878 case MONO_TYPE_BOOLEAN:
879 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
882 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
885 g_assert_not_reached ();
891 objval_describe (MonoClass *class, const char *addr)
893 MonoClassField *field;
895 const char *field_ptr;
896 gssize type_offset = 0;
897 if (class->valuetype)
898 type_offset = -sizeof (MonoObject);
900 for (p = class; p != NULL; p = p->parent) {
901 gpointer iter = NULL;
902 int printed_header = FALSE;
903 while ((field = mono_class_get_fields (p, &iter))) {
904 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
907 if (p != class && !printed_header) {
909 g_print ("In class ");
910 sep = print_name_space (p);
911 g_print ("%s%s:\n", sep, p->name);
912 printed_header = TRUE;
914 field_ptr = (const char*)addr + field->offset + type_offset;
916 print_field_value (field_ptr, field, type_offset);
922 * mono_object_describe_fields:
924 * Prints to stdout a small description of each field of the object @obj.
925 * For use in a debugger.
928 mono_object_describe_fields (MonoObject *obj)
930 MonoClass *class = mono_object_class (obj);
931 objval_describe (class, (char*)obj);
935 * mono_value_describe_fields:
937 * Prints to stdout a small description of each field of the value type
938 * stored at @addr of type @klass.
939 * For use in a debugger.
942 mono_value_describe_fields (MonoClass* klass, const char* addr)
944 objval_describe (klass, addr);
948 * mono_class_describe_statics:
950 * Prints to stdout a small description of each static field of the type @klass
951 * in the current application domain.
952 * For use in a debugger.
955 mono_class_describe_statics (MonoClass* klass)
957 MonoClassField *field;
959 const char *field_ptr;
960 MonoVTable *vtable = mono_class_vtable_full (mono_domain_get (), klass, FALSE);
965 if (!(addr = vtable->data))
968 for (p = klass; p != NULL; p = p->parent) {
969 gpointer iter = NULL;
970 while ((field = mono_class_get_fields (p, &iter))) {
971 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
973 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
976 field_ptr = (const char*)addr + field->offset;
978 print_field_value (field_ptr, field, 0);