3 #include "mono/metadata/tokentype.h"
4 #include "mono/metadata/opcodes.h"
5 #include "mono/metadata/class-internals.h"
6 #include "mono/metadata/mono-endian.h"
7 #include "mono/metadata/debug-helpers.h"
8 #include "mono/metadata/tabledefs.h"
9 #include "mono/metadata/appdomain.h"
11 struct MonoMethodDesc {
17 gboolean include_namespace;
20 #ifdef HAVE_ARRAY_ELEM_INIT
21 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
22 #define MSGSTRFIELD1(line) str##line
23 static const struct msgstr_t {
24 #define WRAPPER(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
25 #include "wrapper-types.h"
28 #define WRAPPER(a,b) b,
29 #include "wrapper-types.h"
32 static const gint16 opidx [] = {
33 #define WRAPPER(a,b) [MONO_WRAPPER_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
34 #include "wrapper-types.h"
39 wrapper_type_to_str (guint32 wrapper_type)
41 g_assert (wrapper_type < MONO_WRAPPER_NUM);
43 return (const char*)&opstr + opidx [wrapper_type];
47 #define WRAPPER(a,b) b,
48 static const char* const
49 wrapper_type_names [MONO_WRAPPER_NUM + 1] = {
50 #include "wrapper-types.h"
55 wrapper_type_to_str (guint32 wrapper_type)
57 g_assert (wrapper_type < MONO_WRAPPER_NUM);
59 return wrapper_type_names [wrapper_type];
65 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
68 g_string_append (res, "Unknown");
71 if (class->nested_in) {
72 append_class_name (res, class->nested_in, include_namespace);
73 g_string_append_c (res, '/');
75 if (include_namespace && *(class->name_space))
76 g_string_sprintfa (res, "%s.", class->name_space);
77 g_string_sprintfa (res, "%s", class->name);
81 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace)
87 g_string_append (res, "void"); break;
89 g_string_append (res, "char"); break;
90 case MONO_TYPE_BOOLEAN:
91 g_string_append (res, "bool"); break;
93 g_string_append (res, "byte"); break;
95 g_string_append (res, "sbyte"); break;
97 g_string_append (res, "uint16"); break;
99 g_string_append (res, "int16"); break;
101 g_string_append (res, "uint"); break;
103 g_string_append (res, "int"); break;
105 g_string_append (res, "ulong"); break;
107 g_string_append (res, "long"); break;
108 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
109 g_string_append (res, "*()"); break;
111 g_string_append (res, "uintptr"); break;
113 g_string_append (res, "intptr"); break;
115 g_string_append (res, "single"); break;
117 g_string_append (res, "double"); break;
118 case MONO_TYPE_STRING:
119 g_string_append (res, "string"); break;
120 case MONO_TYPE_OBJECT:
121 g_string_append (res, "object"); break;
123 mono_type_get_desc (res, type->data.type, include_namespace);
124 g_string_append_c (res, '*');
126 case MONO_TYPE_ARRAY:
127 mono_type_get_desc (res, &type->data.array->eklass->byval_arg, include_namespace);
128 g_string_sprintfa (res, "[%d]", type->data.array->rank);
130 case MONO_TYPE_SZARRAY:
131 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
132 g_string_append (res, "[]");
134 case MONO_TYPE_CLASS:
135 case MONO_TYPE_VALUETYPE:
136 append_class_name (res, type->data.klass, include_namespace);
138 case MONO_TYPE_GENERICINST: {
139 MonoGenericContext *context;
141 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
142 g_string_append (res, "<");
143 context = &type->data.generic_class->context;
144 if (context->class_inst) {
145 for (i = 0; i < context->class_inst->type_argc; ++i) {
147 g_string_append (res, ", ");
148 mono_type_get_desc (res, context->class_inst->type_argv [i], include_namespace);
151 if (context->method_inst) {
152 if (context->class_inst
153 ) g_string_append (res, "; ");
154 for (i = 0; i < context->method_inst->type_argc; ++i) {
156 g_string_append (res, ", ");
157 mono_type_get_desc (res, context->method_inst->type_argv [i], include_namespace);
160 g_string_append (res, ">");
165 g_string_append (res, type->data.generic_param->name);
171 g_string_append_c (res, '&');
175 mono_type_full_name (MonoType *type)
180 str = g_string_new ("");
181 mono_type_get_desc (str, type, TRUE);
183 res = g_strdup (str->str);
184 g_string_free (str, TRUE);
189 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
193 GString *res = g_string_new ("");
195 for (i = 0; i < sig->param_count; ++i) {
197 g_string_append_c (res, ',');
198 mono_type_get_desc (res, sig->params [i], include_namespace);
201 g_string_free (res, FALSE);
206 mono_context_get_desc (MonoGenericContext *context)
212 str = g_string_new ("");
213 g_string_append (str, "<");
215 if (context->class_inst) {
216 for (i = 0; i < context->class_inst->type_argc; ++i)
217 mono_type_get_desc (str, context->class_inst->type_argv [i], TRUE);
219 if (context->method_inst) {
220 for (i = 0; i < context->method_inst->type_argc; ++i)
221 mono_type_get_desc (str, context->method_inst->type_argv [i], TRUE);
224 g_string_append (str, ">");
225 res = g_strdup (str->str);
226 g_string_free (str, TRUE);
231 * mono_method_desc_new:
232 * @name: the method name.
233 * @include_namespace: whether the name includes a namespace or not.
235 * Creates a method description for @name, which conforms to the following
238 * [namespace.]classname:methodname[(args...)]
240 * in all the loaded assemblies.
242 * Returns: a parsed representation of the method description.
245 mono_method_desc_new (const char *name, gboolean include_namespace)
247 MonoMethodDesc *result;
248 char *class_name, *class_nspace, *method_name, *use_args, *end;
251 class_nspace = g_strdup (name);
252 use_args = strchr (class_nspace, '(');
255 end = strchr (use_args, ')');
257 g_free (class_nspace);
262 method_name = strrchr (class_nspace, ':');
264 g_free (class_nspace);
268 /* allow two :: to separate the method name */
269 if (*method_name == ':')
271 class_name = strrchr (class_nspace, '.');
276 class_name = class_nspace;
279 result = g_new0 (MonoMethodDesc, 1);
280 result->include_namespace = include_namespace;
281 result->name = method_name;
282 result->klass = class_name;
283 result->namespace = use_namespace? class_nspace: NULL;
284 result->args = use_args? use_args: NULL;
288 result->num_args = 1;
300 mono_method_desc_from_method (MonoMethod *method)
302 MonoMethodDesc *result;
304 result = g_new0 (MonoMethodDesc, 1);
305 result->include_namespace = TRUE;
306 result->name = g_strdup (method->name);
307 result->klass = g_strdup (method->klass->name);
308 result->namespace = g_strdup (method->klass->name_space);
314 * mono_method_desc_free:
315 * @desc: method description to be released
317 * Releases the MonoMethodDesc object @desc.
320 mono_method_desc_free (MonoMethodDesc *desc)
323 g_free (desc->namespace);
324 else if (desc->klass)
325 g_free (desc->klass);
330 * namespace and class are supposed to match already if this function is used.
333 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
336 if (strcmp (desc->name, method->name))
340 if (desc->num_args != mono_method_signature (method)->param_count)
342 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
343 if (strcmp (sig, desc->args)) {
352 my_strrchr (const char *str, char ch, int *len)
356 for (pos = (*len)-1; pos >= 0; pos--) {
368 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
372 p = my_strrchr (desc->klass, '/', &pos);
374 if (strncmp (desc->klass, klass->name, pos))
376 if (desc->namespace && strcmp (desc->namespace, klass->name_space))
381 if (strcmp (p+1, klass->name))
383 if (!klass->nested_in)
386 return match_class (desc, pos, klass->nested_in);
390 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
392 if (!match_class (desc, strlen (desc->klass), method->klass))
395 return mono_method_desc_match (desc, method);
399 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
402 gpointer iter = NULL;
404 while ((m = mono_class_get_methods (klass, &iter)))
405 if (mono_method_desc_match (desc, m))
411 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
414 const MonoTableInfo *tdef;
415 const MonoTableInfo *methods;
419 if (desc->namespace && desc->klass) {
420 klass = mono_class_from_name (image, desc->namespace, desc->klass);
423 return mono_method_desc_search_in_class (desc, klass);
426 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
427 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
428 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
429 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
430 const char *n = mono_metadata_string_heap (image, token);
432 if (strcmp (n, desc->name))
434 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
435 if (mono_method_desc_full_match (desc, method))
441 static const unsigned char*
442 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
444 MonoMethodHeader *header = mono_method_get_header (method);
445 const MonoOpcode *opcode;
446 guint32 label, token;
450 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
452 label = ip - il_code;
454 tmp = dh->indenter (dh, method, label);
455 g_string_append (str, tmp);
458 if (dh->label_format)
459 g_string_sprintfa (str, dh->label_format, label);
461 i = mono_opcode_value (&ip, end);
463 opcode = &mono_opcodes [i];
464 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
466 switch (opcode->argument) {
470 case MonoInlineField:
471 case MonoInlineMethod:
476 tmp = dh->tokener (dh, method, token);
477 g_string_append (str, tmp);
480 g_string_sprintfa (str, "0x%08x", token);
484 case MonoInlineString:
489 g_string_sprintfa (str, "%d", read16 (ip));
492 case MonoShortInlineVar:
493 g_string_sprintfa (str, "%d", (*ip));
496 case MonoInlineBrTarget:
499 if (dh->label_target)
500 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
502 g_string_sprintfa (str, "%d", sval);
504 case MonoShortInlineBrTarget:
505 sval = *(const signed char*)ip;
507 if (dh->label_target)
508 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
510 g_string_sprintfa (str, "%d", sval);
512 case MonoInlineSwitch: {
513 const unsigned char *end;
517 g_string_append_c (str, '(');
518 for (i = 0; i < sval; ++i) {
520 g_string_append (str, ", ");
522 if (dh->label_target)
523 g_string_sprintfa (str, dh->label_target, end + label - il_code);
525 g_string_sprintfa (str, "%d", label);
528 g_string_append_c (str, ')');
534 g_string_sprintfa (str, "%g", r);
538 case MonoShortInlineR: {
541 g_string_sprintfa (str, "%g", r);
546 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
549 case MonoShortInlineI:
550 g_string_sprintfa (str, "%d", *(const signed char*)ip);
557 g_assert_not_reached ();
560 g_string_append (str, dh->newline);
568 "IL_%04x: ", /* label_format */
569 "IL_%04x", /* label_target */
576 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
579 GString *res = g_string_new ("");
583 /* set ip + 2 as the end: this is just a debugging method */
584 ip = dis_one (res, dh, method, ip, ip + 2);
589 g_string_free (res, FALSE);
594 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
597 GString *res = g_string_new ("");
602 ip = dis_one (res, dh, method, ip, end);
606 g_string_free (res, FALSE);
611 mono_field_full_name (MonoClassField *field)
614 const char *nspace = field->parent->name_space;
616 res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
617 field->parent->name, field->name);
623 mono_method_full_name (MonoMethod *method, gboolean signature)
627 char *klass_desc = mono_type_full_name (&method->klass->byval_arg);
630 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
632 if (method->wrapper_type != MONO_WRAPPER_NONE)
633 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
635 strcpy (wrapper, "");
636 res = g_strdup_printf ("%s%s:%s (%s)", wrapper, klass_desc,
637 method->name, tmpsig);
641 res = g_strdup_printf ("%02d %s:%s", method->wrapper_type, klass_desc,
651 print_name_space (MonoClass *klass)
653 if (klass->nested_in) {
654 print_name_space (klass->nested_in);
655 g_print (klass->nested_in->name);
658 if (klass->name_space [0]) {
659 g_print (klass->name_space);
666 * mono_object_describe:
668 * Prints to stdout a small description of the object @obj.
669 * For use in a debugger.
672 mono_object_describe (MonoObject *obj)
677 g_print ("(null)\n");
680 klass = mono_object_class (obj);
681 if (klass == mono_defaults.string_class) {
682 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
683 if (strlen (utf8) > 60) {
689 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
691 } else if (klass->rank) {
692 MonoArray *array = (MonoArray*)obj;
693 sep = print_name_space (klass);
694 g_print ("%s%s", sep, klass->name);
695 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
697 sep = print_name_space (klass);
698 g_print ("%s%s", sep, klass->name);
699 g_print (" object at %p (klass: %p)\n", obj, klass);
705 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
708 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, field->name);
709 type = mono_type_get_underlying_type (field->type);
711 switch (type->type) {
715 case MONO_TYPE_FNPTR:
716 g_print ("%p\n", *(const void**)field_ptr);
718 case MONO_TYPE_STRING:
719 case MONO_TYPE_SZARRAY:
720 case MONO_TYPE_CLASS:
721 case MONO_TYPE_OBJECT:
722 case MONO_TYPE_ARRAY:
723 mono_object_describe (*(MonoObject**)field_ptr);
725 case MONO_TYPE_GENERICINST:
726 if (!mono_type_generic_inst_is_valuetype (type)) {
727 mono_object_describe (*(MonoObject**)field_ptr);
732 case MONO_TYPE_VALUETYPE: {
733 MonoClass *k = mono_class_from_mono_type (type);
734 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
738 g_print ("%d\n", *(gint8*)field_ptr);
741 g_print ("%d\n", *(guint8*)field_ptr);
744 g_print ("%d\n", *(gint16*)field_ptr);
747 g_print ("%d\n", *(guint16*)field_ptr);
750 g_print ("%d\n", *(gint32*)field_ptr);
753 g_print ("%u\n", *(guint32*)field_ptr);
756 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
759 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
762 g_print ("%f\n", *(gfloat*)field_ptr);
765 g_print ("%f\n", *(gdouble*)field_ptr);
767 case MONO_TYPE_BOOLEAN:
768 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
771 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
774 g_assert_not_reached ();
780 objval_describe (MonoClass *class, const char *addr)
782 MonoClassField *field;
784 const char *field_ptr;
785 gssize type_offset = 0;
786 if (class->valuetype)
787 type_offset = -sizeof (MonoObject);
789 for (p = class; p != NULL; p = p->parent) {
790 gpointer iter = NULL;
791 int printed_header = FALSE;
792 while ((field = mono_class_get_fields (p, &iter))) {
793 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
796 if (p != class && !printed_header) {
798 g_print ("In class ");
799 sep = print_name_space (p);
800 g_print ("%s%s:\n", sep, p->name);
801 printed_header = TRUE;
803 field_ptr = (const char*)addr + field->offset + type_offset;
805 print_field_value (field_ptr, field, type_offset);
811 * mono_object_describe_fields:
813 * Prints to stdout a small description of each field of the object @obj.
814 * For use in a debugger.
817 mono_object_describe_fields (MonoObject *obj)
819 MonoClass *class = mono_object_class (obj);
820 objval_describe (class, (char*)obj);
824 * mono_value_describe_fields:
826 * Prints to stdout a small description of each field of the value type
827 * stored at @addr of type @klass.
828 * For use in a debugger.
831 mono_value_describe_fields (MonoClass* klass, const char* addr)
833 objval_describe (klass, addr);
837 * mono_class_describe_statics:
839 * Prints to stdout a small description of each static field of the type @klass
840 * in the current application domain.
841 * For use in a debugger.
844 mono_class_describe_statics (MonoClass* klass)
846 MonoClassField *field;
848 const char *field_ptr;
849 const char *addr = mono_class_vtable (mono_domain_get (), klass)->data;
853 for (p = klass; p != NULL; p = p->parent) {
854 gpointer iter = NULL;
855 while ((field = mono_class_get_fields (p, &iter))) {
856 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
858 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
861 field_ptr = (const char*)addr + field->offset;
863 print_field_value (field_ptr, field, 0);