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;
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_sprintfa (res, "%s.", class->name_space);
86 g_string_sprintfa (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_sprintfa (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 be '*' 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;
317 result->num_args = 1;
329 mono_method_desc_from_method (MonoMethod *method)
331 MonoMethodDesc *result;
333 result = g_new0 (MonoMethodDesc, 1);
334 result->include_namespace = TRUE;
335 result->name = g_strdup (method->name);
336 result->klass = g_strdup (method->klass->name);
337 result->namespace = g_strdup (method->klass->name_space);
343 * mono_method_desc_free:
344 * @desc: method description to be released
346 * Releases the MonoMethodDesc object @desc.
349 mono_method_desc_free (MonoMethodDesc *desc)
352 g_free (desc->namespace);
353 else if (desc->klass)
354 g_free (desc->klass);
359 * namespace and class are supposed to match already if this function is used.
362 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
365 if (strcmp (desc->name, method->name) && (strcmp (desc->name, "*") != 0))
369 if (desc->num_args != mono_method_signature (method)->param_count)
371 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
372 if (strcmp (sig, desc->args)) {
381 my_strrchr (const char *str, char ch, int *len)
385 for (pos = (*len)-1; pos >= 0; pos--) {
397 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
401 if (!strcmp (desc->klass, "*"))
403 p = my_strrchr (desc->klass, '/', &pos);
405 if (strncmp (desc->klass, klass->name, pos))
407 if (desc->namespace && strcmp (desc->namespace, klass->name_space))
412 if (strcmp (p+1, klass->name))
414 if (!klass->nested_in)
417 return match_class (desc, pos, klass->nested_in);
421 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
423 if (!match_class (desc, strlen (desc->klass), method->klass))
426 return mono_method_desc_match (desc, method);
430 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
433 gpointer iter = NULL;
435 while ((m = mono_class_get_methods (klass, &iter)))
436 if (mono_method_desc_match (desc, m))
442 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
445 const MonoTableInfo *tdef;
446 const MonoTableInfo *methods;
450 if (desc->namespace && desc->klass) {
451 klass = mono_class_from_name (image, desc->namespace, desc->klass);
454 return mono_method_desc_search_in_class (desc, klass);
457 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
458 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
459 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
460 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
461 const char *n = mono_metadata_string_heap (image, token);
463 if (strcmp (n, desc->name))
465 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
466 if (mono_method_desc_full_match (desc, method))
472 static const unsigned char*
473 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
475 MonoMethodHeader *header = mono_method_get_header (method);
476 const MonoOpcode *opcode;
477 guint32 label, token;
481 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
483 label = ip - il_code;
485 tmp = dh->indenter (dh, method, label);
486 g_string_append (str, tmp);
489 if (dh->label_format)
490 g_string_sprintfa (str, dh->label_format, label);
492 i = mono_opcode_value (&ip, end);
494 opcode = &mono_opcodes [i];
495 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
497 switch (opcode->argument) {
501 case MonoInlineField:
502 case MonoInlineMethod:
507 tmp = dh->tokener (dh, method, token);
508 g_string_append (str, tmp);
511 g_string_sprintfa (str, "0x%08x", token);
515 case MonoInlineString: {
520 if (!method->klass->image->dynamic) {
522 blob = mono_metadata_user_string (method->klass->image, mono_metadata_token_index (token));
524 len2 = mono_metadata_decode_blob_size (blob, &blob);
527 s = g_utf16_to_utf8 ((gunichar2*)blob, len2, NULL, NULL, NULL);
529 g_string_sprintfa (str, "\"%s\"", s);
536 g_string_sprintfa (str, "%d", read16 (ip));
539 case MonoShortInlineVar:
540 g_string_sprintfa (str, "%d", (*ip));
543 case MonoInlineBrTarget:
546 if (dh->label_target)
547 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
549 g_string_sprintfa (str, "%d", sval);
551 case MonoShortInlineBrTarget:
552 sval = *(const signed char*)ip;
554 if (dh->label_target)
555 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
557 g_string_sprintfa (str, "%d", sval);
559 case MonoInlineSwitch: {
560 const unsigned char *end;
564 g_string_append_c (str, '(');
565 for (i = 0; i < sval; ++i) {
567 g_string_append (str, ", ");
569 if (dh->label_target)
570 g_string_sprintfa (str, dh->label_target, end + label - il_code);
572 g_string_sprintfa (str, "%d", label);
575 g_string_append_c (str, ')');
581 g_string_sprintfa (str, "%g", r);
585 case MonoShortInlineR: {
588 g_string_sprintfa (str, "%g", r);
593 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
596 case MonoShortInlineI:
597 g_string_sprintfa (str, "%d", *(const signed char*)ip);
604 g_assert_not_reached ();
607 g_string_append (str, dh->newline);
615 "IL_%04x: ", /* label_format */
616 "IL_%04x", /* label_target */
623 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
626 GString *res = g_string_new ("");
630 /* set ip + 2 as the end: this is just a debugging method */
631 ip = dis_one (res, dh, method, ip, ip + 2);
636 g_string_free (res, FALSE);
641 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
644 GString *res = g_string_new ("");
649 ip = dis_one (res, dh, method, ip, end);
653 g_string_free (res, FALSE);
658 mono_field_full_name (MonoClassField *field)
661 const char *nspace = field->parent->name_space;
663 res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
664 field->parent->name, mono_field_get_name (field));
670 mono_method_full_name (MonoMethod *method, gboolean signature)
674 char *klass_desc = mono_type_full_name (&method->klass->byval_arg);
675 char *inst_desc = NULL;
677 if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
678 GString *str = g_string_new ("");
679 g_string_append (str, "<");
680 ginst_get_desc (str, ((MonoMethodInflated*)method)->context.method_inst);
681 g_string_append (str, ">");
683 inst_desc = str->str;
684 g_string_free (str, FALSE);
687 if (method->wrapper_type != MONO_WRAPPER_NONE)
688 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
690 strcpy (wrapper, "");
693 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
695 if (method->wrapper_type != MONO_WRAPPER_NONE)
696 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
698 strcpy (wrapper, "");
699 res = g_strdup_printf ("%s%s:%s%s (%s)", wrapper, klass_desc,
700 method->name, inst_desc ? inst_desc : "", tmpsig);
703 res = g_strdup_printf ("%s%s:%s%s", wrapper, klass_desc,
704 method->name, inst_desc ? inst_desc : "");
714 print_name_space (MonoClass *klass)
716 if (klass->nested_in) {
717 print_name_space (klass->nested_in);
718 g_print (klass->nested_in->name);
721 if (klass->name_space [0]) {
722 g_print (klass->name_space);
729 * mono_object_describe:
731 * Prints to stdout a small description of the object @obj.
732 * For use in a debugger.
735 mono_object_describe (MonoObject *obj)
740 g_print ("(null)\n");
743 klass = mono_object_class (obj);
744 if (klass == mono_defaults.string_class) {
745 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
746 if (strlen (utf8) > 60) {
752 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
754 } else if (klass->rank) {
755 MonoArray *array = (MonoArray*)obj;
756 sep = print_name_space (klass);
757 g_print ("%s%s", sep, klass->name);
758 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
760 sep = print_name_space (klass);
761 g_print ("%s%s", sep, klass->name);
762 g_print (" object at %p (klass: %p)\n", obj, klass);
768 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
771 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, mono_field_get_name (field));
772 type = mono_type_get_underlying_type (field->type);
774 switch (type->type) {
778 case MONO_TYPE_FNPTR:
779 g_print ("%p\n", *(const void**)field_ptr);
781 case MONO_TYPE_STRING:
782 case MONO_TYPE_SZARRAY:
783 case MONO_TYPE_CLASS:
784 case MONO_TYPE_OBJECT:
785 case MONO_TYPE_ARRAY:
786 mono_object_describe (*(MonoObject**)field_ptr);
788 case MONO_TYPE_GENERICINST:
789 if (!mono_type_generic_inst_is_valuetype (type)) {
790 mono_object_describe (*(MonoObject**)field_ptr);
795 case MONO_TYPE_VALUETYPE: {
796 MonoClass *k = mono_class_from_mono_type (type);
797 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
801 g_print ("%d\n", *(gint8*)field_ptr);
804 g_print ("%d\n", *(guint8*)field_ptr);
807 g_print ("%d\n", *(gint16*)field_ptr);
810 g_print ("%d\n", *(guint16*)field_ptr);
813 g_print ("%d\n", *(gint32*)field_ptr);
816 g_print ("%u\n", *(guint32*)field_ptr);
819 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
822 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
825 g_print ("%f\n", *(gfloat*)field_ptr);
828 g_print ("%f\n", *(gdouble*)field_ptr);
830 case MONO_TYPE_BOOLEAN:
831 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
834 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
837 g_assert_not_reached ();
843 objval_describe (MonoClass *class, const char *addr)
845 MonoClassField *field;
847 const char *field_ptr;
848 gssize type_offset = 0;
849 if (class->valuetype)
850 type_offset = -sizeof (MonoObject);
852 for (p = class; p != NULL; p = p->parent) {
853 gpointer iter = NULL;
854 int printed_header = FALSE;
855 while ((field = mono_class_get_fields (p, &iter))) {
856 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
859 if (p != class && !printed_header) {
861 g_print ("In class ");
862 sep = print_name_space (p);
863 g_print ("%s%s:\n", sep, p->name);
864 printed_header = TRUE;
866 field_ptr = (const char*)addr + field->offset + type_offset;
868 print_field_value (field_ptr, field, type_offset);
874 * mono_object_describe_fields:
876 * Prints to stdout a small description of each field of the object @obj.
877 * For use in a debugger.
880 mono_object_describe_fields (MonoObject *obj)
882 MonoClass *class = mono_object_class (obj);
883 objval_describe (class, (char*)obj);
887 * mono_value_describe_fields:
889 * Prints to stdout a small description of each field of the value type
890 * stored at @addr of type @klass.
891 * For use in a debugger.
894 mono_value_describe_fields (MonoClass* klass, const char* addr)
896 objval_describe (klass, addr);
900 * mono_class_describe_statics:
902 * Prints to stdout a small description of each static field of the type @klass
903 * in the current application domain.
904 * For use in a debugger.
907 mono_class_describe_statics (MonoClass* klass)
909 MonoClassField *field;
911 const char *field_ptr;
912 const char *addr = mono_class_vtable (mono_domain_get (), klass)->data;
916 for (p = klass; p != NULL; p = p->parent) {
917 gpointer iter = NULL;
918 while ((field = mono_class_get_fields (p, &iter))) {
919 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
921 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
924 field_ptr = (const char*)addr + field->offset;
926 print_field_value (field_ptr, field, 0);