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 g_string_append (res, mono_generic_param_info (type->data.generic_param)->name);
180 g_string_append_c (res, '&');
184 mono_type_full_name (MonoType *type)
189 str = g_string_new ("");
190 mono_type_get_desc (str, type, TRUE);
192 res = g_strdup (str->str);
193 g_string_free (str, TRUE);
198 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
202 GString *res = g_string_new ("");
204 for (i = 0; i < sig->param_count; ++i) {
206 g_string_append_c (res, ',');
207 mono_type_get_desc (res, sig->params [i], include_namespace);
210 g_string_free (res, FALSE);
215 ginst_get_desc (GString *str, MonoGenericInst *ginst)
219 for (i = 0; i < ginst->type_argc; ++i) {
221 g_string_append (str, ", ");
222 mono_type_get_desc (str, ginst->type_argv [i], TRUE);
227 mono_context_get_desc (MonoGenericContext *context)
232 str = g_string_new ("");
233 g_string_append (str, "<");
235 if (context->class_inst)
236 ginst_get_desc (str, context->class_inst);
237 if (context->method_inst) {
238 if (context->class_inst)
239 g_string_append (str, "; ");
240 ginst_get_desc (str, context->method_inst);
243 g_string_append (str, ">");
244 res = g_strdup (str->str);
245 g_string_free (str, TRUE);
250 * mono_method_desc_new:
251 * @name: the method name.
252 * @include_namespace: whether the name includes a namespace or not.
254 * Creates a method description for @name, which conforms to the following
257 * [namespace.]classname:methodname[(args...)]
259 * in all the loaded assemblies.
261 * Both classname and methodname can be '*' which matches anything.
263 * Returns: a parsed representation of the method description.
266 mono_method_desc_new (const char *name, gboolean include_namespace)
268 MonoMethodDesc *result;
269 char *class_name, *class_nspace, *method_name, *use_args, *end;
272 class_nspace = g_strdup (name);
273 use_args = strchr (class_nspace, '(');
276 end = strchr (use_args, ')');
278 g_free (class_nspace);
283 method_name = strrchr (class_nspace, ':');
285 g_free (class_nspace);
289 /* allow two :: to separate the method name */
290 if (*method_name == ':')
292 class_name = strrchr (class_nspace, '.');
297 class_name = class_nspace;
300 result = g_new0 (MonoMethodDesc, 1);
301 result->include_namespace = include_namespace;
302 result->name = method_name;
303 result->klass = class_name;
304 result->namespace = use_namespace? class_nspace: NULL;
305 result->args = use_args? use_args: NULL;
309 result->num_args = 1;
321 mono_method_desc_from_method (MonoMethod *method)
323 MonoMethodDesc *result;
325 result = g_new0 (MonoMethodDesc, 1);
326 result->include_namespace = TRUE;
327 result->name = g_strdup (method->name);
328 result->klass = g_strdup (method->klass->name);
329 result->namespace = g_strdup (method->klass->name_space);
335 * mono_method_desc_free:
336 * @desc: method description to be released
338 * Releases the MonoMethodDesc object @desc.
341 mono_method_desc_free (MonoMethodDesc *desc)
344 g_free (desc->namespace);
345 else if (desc->klass)
346 g_free (desc->klass);
351 * namespace and class are supposed to match already if this function is used.
354 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
357 if (strcmp (desc->name, method->name) && (strcmp (desc->name, "*") != 0))
361 if (desc->num_args != mono_method_signature (method)->param_count)
363 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
364 if (strcmp (sig, desc->args)) {
373 my_strrchr (const char *str, char ch, int *len)
377 for (pos = (*len)-1; pos >= 0; pos--) {
389 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
393 if (!strcmp (desc->klass, "*"))
395 p = my_strrchr (desc->klass, '/', &pos);
397 if (strncmp (desc->klass, klass->name, pos))
399 if (desc->namespace && strcmp (desc->namespace, klass->name_space))
404 if (strcmp (p+1, klass->name))
406 if (!klass->nested_in)
409 return match_class (desc, pos, klass->nested_in);
413 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
415 if (!match_class (desc, strlen (desc->klass), method->klass))
418 return mono_method_desc_match (desc, method);
422 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
425 gpointer iter = NULL;
427 while ((m = mono_class_get_methods (klass, &iter)))
428 if (mono_method_desc_match (desc, m))
434 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
437 const MonoTableInfo *tdef;
438 const MonoTableInfo *methods;
442 if (desc->namespace && desc->klass) {
443 klass = mono_class_from_name (image, desc->namespace, desc->klass);
446 return mono_method_desc_search_in_class (desc, klass);
449 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
450 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
451 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
452 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
453 const char *n = mono_metadata_string_heap (image, token);
455 if (strcmp (n, desc->name))
457 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
458 if (mono_method_desc_full_match (desc, method))
464 static const unsigned char*
465 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
467 MonoMethodHeader *header = mono_method_get_header (method);
468 const MonoOpcode *opcode;
469 guint32 label, token;
473 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
475 label = ip - il_code;
477 tmp = dh->indenter (dh, method, label);
478 g_string_append (str, tmp);
481 if (dh->label_format)
482 g_string_sprintfa (str, dh->label_format, label);
484 i = mono_opcode_value (&ip, end);
486 opcode = &mono_opcodes [i];
487 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
489 switch (opcode->argument) {
493 case MonoInlineField:
494 case MonoInlineMethod:
499 tmp = dh->tokener (dh, method, token);
500 g_string_append (str, tmp);
503 g_string_sprintfa (str, "0x%08x", token);
507 case MonoInlineString: {
512 if (!method->klass->image->dynamic) {
514 blob = mono_metadata_user_string (method->klass->image, mono_metadata_token_index (token));
516 len2 = mono_metadata_decode_blob_size (blob, &blob);
519 s = g_utf16_to_utf8 ((gunichar2*)blob, len2, NULL, NULL, NULL);
521 g_string_sprintfa (str, "\"%s\"", s);
528 g_string_sprintfa (str, "%d", read16 (ip));
531 case MonoShortInlineVar:
532 g_string_sprintfa (str, "%d", (*ip));
535 case MonoInlineBrTarget:
538 if (dh->label_target)
539 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
541 g_string_sprintfa (str, "%d", sval);
543 case MonoShortInlineBrTarget:
544 sval = *(const signed char*)ip;
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 MonoInlineSwitch: {
552 const unsigned char *end;
556 g_string_append_c (str, '(');
557 for (i = 0; i < sval; ++i) {
559 g_string_append (str, ", ");
561 if (dh->label_target)
562 g_string_sprintfa (str, dh->label_target, end + label - il_code);
564 g_string_sprintfa (str, "%d", label);
567 g_string_append_c (str, ')');
573 g_string_sprintfa (str, "%g", r);
577 case MonoShortInlineR: {
580 g_string_sprintfa (str, "%g", r);
585 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
588 case MonoShortInlineI:
589 g_string_sprintfa (str, "%d", *(const signed char*)ip);
596 g_assert_not_reached ();
599 g_string_append (str, dh->newline);
607 "IL_%04x: ", /* label_format */
608 "IL_%04x", /* label_target */
615 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
618 GString *res = g_string_new ("");
622 /* set ip + 2 as the end: this is just a debugging method */
623 ip = dis_one (res, dh, method, ip, ip + 2);
628 g_string_free (res, FALSE);
633 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
636 GString *res = g_string_new ("");
641 ip = dis_one (res, dh, method, ip, end);
645 g_string_free (res, FALSE);
650 mono_field_full_name (MonoClassField *field)
653 const char *nspace = field->parent->name_space;
655 res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
656 field->parent->name, mono_field_get_name (field));
662 mono_method_full_name (MonoMethod *method, gboolean signature)
666 char *klass_desc = mono_type_full_name (&method->klass->byval_arg);
667 char *inst_desc = NULL;
669 if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
670 GString *str = g_string_new ("");
671 g_string_append (str, "<");
672 ginst_get_desc (str, ((MonoMethodInflated*)method)->context.method_inst);
673 g_string_append (str, ">");
675 inst_desc = str->str;
676 g_string_free (str, FALSE);
679 if (method->wrapper_type != MONO_WRAPPER_NONE)
680 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
682 strcpy (wrapper, "");
685 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
687 if (method->wrapper_type != MONO_WRAPPER_NONE)
688 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
690 strcpy (wrapper, "");
691 res = g_strdup_printf ("%s%s:%s%s (%s)", wrapper, klass_desc,
692 method->name, inst_desc ? inst_desc : "", tmpsig);
695 res = g_strdup_printf ("%s%s:%s%s", wrapper, klass_desc,
696 method->name, inst_desc ? inst_desc : "");
706 print_name_space (MonoClass *klass)
708 if (klass->nested_in) {
709 print_name_space (klass->nested_in);
710 g_print (klass->nested_in->name);
713 if (klass->name_space [0]) {
714 g_print (klass->name_space);
721 * mono_object_describe:
723 * Prints to stdout a small description of the object @obj.
724 * For use in a debugger.
727 mono_object_describe (MonoObject *obj)
732 g_print ("(null)\n");
735 klass = mono_object_class (obj);
736 if (klass == mono_defaults.string_class) {
737 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
738 if (strlen (utf8) > 60) {
744 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
746 } else if (klass->rank) {
747 MonoArray *array = (MonoArray*)obj;
748 sep = print_name_space (klass);
749 g_print ("%s%s", sep, klass->name);
750 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
752 sep = print_name_space (klass);
753 g_print ("%s%s", sep, klass->name);
754 g_print (" object at %p (klass: %p)\n", obj, klass);
760 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
763 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, mono_field_get_name (field));
764 type = mono_type_get_underlying_type (field->type);
766 switch (type->type) {
770 case MONO_TYPE_FNPTR:
771 g_print ("%p\n", *(const void**)field_ptr);
773 case MONO_TYPE_STRING:
774 case MONO_TYPE_SZARRAY:
775 case MONO_TYPE_CLASS:
776 case MONO_TYPE_OBJECT:
777 case MONO_TYPE_ARRAY:
778 mono_object_describe (*(MonoObject**)field_ptr);
780 case MONO_TYPE_GENERICINST:
781 if (!mono_type_generic_inst_is_valuetype (type)) {
782 mono_object_describe (*(MonoObject**)field_ptr);
787 case MONO_TYPE_VALUETYPE: {
788 MonoClass *k = mono_class_from_mono_type (type);
789 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
793 g_print ("%d\n", *(gint8*)field_ptr);
796 g_print ("%d\n", *(guint8*)field_ptr);
799 g_print ("%d\n", *(gint16*)field_ptr);
802 g_print ("%d\n", *(guint16*)field_ptr);
805 g_print ("%d\n", *(gint32*)field_ptr);
808 g_print ("%u\n", *(guint32*)field_ptr);
811 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
814 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
817 g_print ("%f\n", *(gfloat*)field_ptr);
820 g_print ("%f\n", *(gdouble*)field_ptr);
822 case MONO_TYPE_BOOLEAN:
823 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
826 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
829 g_assert_not_reached ();
835 objval_describe (MonoClass *class, const char *addr)
837 MonoClassField *field;
839 const char *field_ptr;
840 gssize type_offset = 0;
841 if (class->valuetype)
842 type_offset = -sizeof (MonoObject);
844 for (p = class; p != NULL; p = p->parent) {
845 gpointer iter = NULL;
846 int printed_header = FALSE;
847 while ((field = mono_class_get_fields (p, &iter))) {
848 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
851 if (p != class && !printed_header) {
853 g_print ("In class ");
854 sep = print_name_space (p);
855 g_print ("%s%s:\n", sep, p->name);
856 printed_header = TRUE;
858 field_ptr = (const char*)addr + field->offset + type_offset;
860 print_field_value (field_ptr, field, type_offset);
866 * mono_object_describe_fields:
868 * Prints to stdout a small description of each field of the object @obj.
869 * For use in a debugger.
872 mono_object_describe_fields (MonoObject *obj)
874 MonoClass *class = mono_object_class (obj);
875 objval_describe (class, (char*)obj);
879 * mono_value_describe_fields:
881 * Prints to stdout a small description of each field of the value type
882 * stored at @addr of type @klass.
883 * For use in a debugger.
886 mono_value_describe_fields (MonoClass* klass, const char* addr)
888 objval_describe (klass, addr);
892 * mono_class_describe_statics:
894 * Prints to stdout a small description of each static field of the type @klass
895 * in the current application domain.
896 * For use in a debugger.
899 mono_class_describe_statics (MonoClass* klass)
901 MonoClassField *field;
903 const char *field_ptr;
904 const char *addr = mono_class_vtable (mono_domain_get (), klass)->data;
908 for (p = klass; p != NULL; p = p->parent) {
909 gpointer iter = NULL;
910 while ((field = mono_class_get_fields (p, &iter))) {
911 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
913 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
916 field_ptr = (const char*)addr + field->offset;
918 print_field_value (field_ptr, field, 0);