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)
146 mono_type_get_desc (res, context->class_inst->type_argv [i], include_namespace);
148 if (context->method_inst) {
149 for (i = 0; i < context->method_inst->type_argc; ++i)
150 mono_type_get_desc (res, context->method_inst->type_argv [i], include_namespace);
152 g_string_append (res, ">");
157 g_string_append (res, type->data.generic_param->name);
163 g_string_append_c (res, '&');
167 mono_type_full_name (MonoType *type)
172 str = g_string_new ("");
173 mono_type_get_desc (str, type, TRUE);
175 res = g_strdup (str->str);
176 g_string_free (str, TRUE);
181 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
185 GString *res = g_string_new ("");
187 for (i = 0; i < sig->param_count; ++i) {
189 g_string_append_c (res, ',');
190 mono_type_get_desc (res, sig->params [i], include_namespace);
193 g_string_free (res, FALSE);
198 mono_context_get_desc (MonoGenericContext *context)
204 str = g_string_new ("");
205 g_string_append (str, "<");
207 if (context->class_inst) {
208 for (i = 0; i < context->class_inst->type_argc; ++i)
209 mono_type_get_desc (str, context->class_inst->type_argv [i], TRUE);
211 if (context->method_inst) {
212 for (i = 0; i < context->method_inst->type_argc; ++i)
213 mono_type_get_desc (str, context->method_inst->type_argv [i], TRUE);
216 g_string_append (str, ">");
217 res = g_strdup (str->str);
218 g_string_free (str, TRUE);
223 * mono_method_desc_new:
224 * @name: the method name.
225 * @include_namespace: whether the name includes a namespace or not.
227 * Creates a method description for @name, which conforms to the following
230 * [namespace.]classname:methodname[(args...)]
232 * in all the loaded assemblies.
234 * Returns: a parsed representation of the method description.
237 mono_method_desc_new (const char *name, gboolean include_namespace)
239 MonoMethodDesc *result;
240 char *class_name, *class_nspace, *method_name, *use_args, *end;
243 class_nspace = g_strdup (name);
244 use_args = strchr (class_nspace, '(');
247 end = strchr (use_args, ')');
249 g_free (class_nspace);
254 method_name = strrchr (class_nspace, ':');
256 g_free (class_nspace);
260 /* allow two :: to separate the method name */
261 if (*method_name == ':')
263 class_name = strrchr (class_nspace, '.');
268 class_name = class_nspace;
271 result = g_new0 (MonoMethodDesc, 1);
272 result->include_namespace = include_namespace;
273 result->name = method_name;
274 result->klass = class_name;
275 result->namespace = use_namespace? class_nspace: NULL;
276 result->args = use_args? use_args: NULL;
280 result->num_args = 1;
292 mono_method_desc_from_method (MonoMethod *method)
294 MonoMethodDesc *result;
296 result = g_new0 (MonoMethodDesc, 1);
297 result->include_namespace = TRUE;
298 result->name = g_strdup (method->name);
299 result->klass = g_strdup (method->klass->name);
300 result->namespace = g_strdup (method->klass->name_space);
306 * mono_method_desc_free:
307 * @desc: method description to be released
309 * Releases the MonoMethodDesc object @desc.
312 mono_method_desc_free (MonoMethodDesc *desc)
315 g_free (desc->namespace);
316 else if (desc->klass)
317 g_free (desc->klass);
322 * namespace and class are supposed to match already if this function is used.
325 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
328 if (strcmp (desc->name, method->name))
332 if (desc->num_args != mono_method_signature (method)->param_count)
334 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
335 if (strcmp (sig, desc->args)) {
344 my_strrchr (const char *str, char ch, int *len)
348 for (pos = (*len)-1; pos >= 0; pos--) {
360 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
364 p = my_strrchr (desc->klass, '/', &pos);
366 if (strncmp (desc->klass, klass->name, pos))
368 if (desc->namespace && strcmp (desc->namespace, klass->name_space))
373 if (strcmp (p+1, klass->name))
375 if (!klass->nested_in)
378 return match_class (desc, pos, klass->nested_in);
382 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
384 if (!match_class (desc, strlen (desc->klass), method->klass))
387 return mono_method_desc_match (desc, method);
391 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
394 gpointer iter = NULL;
396 while ((m = mono_class_get_methods (klass, &iter)))
397 if (mono_method_desc_match (desc, m))
403 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
406 const MonoTableInfo *tdef;
407 const MonoTableInfo *methods;
411 if (desc->namespace && desc->klass) {
412 klass = mono_class_from_name (image, desc->namespace, desc->klass);
415 return mono_method_desc_search_in_class (desc, klass);
418 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
419 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
420 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
421 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
422 const char *n = mono_metadata_string_heap (image, token);
424 if (strcmp (n, desc->name))
426 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
427 if (mono_method_desc_full_match (desc, method))
433 static const unsigned char*
434 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
436 MonoMethodHeader *header = mono_method_get_header (method);
437 const MonoOpcode *opcode;
438 guint32 label, token;
442 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
444 label = ip - il_code;
446 tmp = dh->indenter (dh, method, label);
447 g_string_append (str, tmp);
450 if (dh->label_format)
451 g_string_sprintfa (str, dh->label_format, label);
453 i = mono_opcode_value (&ip, end);
455 opcode = &mono_opcodes [i];
456 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
458 switch (opcode->argument) {
462 case MonoInlineField:
463 case MonoInlineMethod:
468 tmp = dh->tokener (dh, method, token);
469 g_string_append (str, tmp);
472 g_string_sprintfa (str, "0x%08x", token);
476 case MonoInlineString:
481 g_string_sprintfa (str, "%d", read16 (ip));
484 case MonoShortInlineVar:
485 g_string_sprintfa (str, "%d", (*ip));
488 case MonoInlineBrTarget:
491 if (dh->label_target)
492 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
494 g_string_sprintfa (str, "%d", sval);
496 case MonoShortInlineBrTarget:
497 sval = *(const signed char*)ip;
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 MonoInlineSwitch: {
505 const unsigned char *end;
509 g_string_append_c (str, '(');
510 for (i = 0; i < sval; ++i) {
512 g_string_append (str, ", ");
514 if (dh->label_target)
515 g_string_sprintfa (str, dh->label_target, end + label - il_code);
517 g_string_sprintfa (str, "%d", label);
520 g_string_append_c (str, ')');
526 g_string_sprintfa (str, "%g", r);
530 case MonoShortInlineR: {
533 g_string_sprintfa (str, "%g", r);
538 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
541 case MonoShortInlineI:
542 g_string_sprintfa (str, "%d", *(const signed char*)ip);
549 g_assert_not_reached ();
552 g_string_append (str, dh->newline);
560 "IL_%04x: ", /* label_format */
561 "IL_%04x", /* label_target */
568 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
571 GString *res = g_string_new ("");
575 /* set ip + 2 as the end: this is just a debugging method */
576 ip = dis_one (res, dh, method, ip, ip + 2);
581 g_string_free (res, FALSE);
586 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
589 GString *res = g_string_new ("");
594 ip = dis_one (res, dh, method, ip, end);
598 g_string_free (res, FALSE);
603 mono_field_full_name (MonoClassField *field)
606 const char *nspace = field->parent->name_space;
608 res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
609 field->parent->name, field->name);
615 mono_method_full_name (MonoMethod *method, gboolean signature)
619 char *klass_desc = mono_type_full_name (&method->klass->byval_arg);
622 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
624 if (method->wrapper_type != MONO_WRAPPER_NONE)
625 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
627 strcpy (wrapper, "");
628 res = g_strdup_printf ("%s%s:%s (%s)", wrapper, klass_desc,
629 method->name, tmpsig);
633 res = g_strdup_printf ("%02d %s:%s", method->wrapper_type, klass_desc,
643 print_name_space (MonoClass *klass)
645 if (klass->nested_in) {
646 print_name_space (klass->nested_in);
647 g_print (klass->nested_in->name);
650 if (klass->name_space [0]) {
651 g_print (klass->name_space);
658 * mono_object_describe:
660 * Prints to stdout a small description of the object @obj.
661 * For use in a debugger.
664 mono_object_describe (MonoObject *obj)
669 g_print ("(null)\n");
672 klass = mono_object_class (obj);
673 if (klass == mono_defaults.string_class) {
674 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
675 if (strlen (utf8) > 60) {
681 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
683 } else if (klass->rank) {
684 MonoArray *array = (MonoArray*)obj;
685 sep = print_name_space (klass);
686 g_print ("%s%s", sep, klass->name);
687 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
689 sep = print_name_space (klass);
690 g_print ("%s%s", sep, klass->name);
691 g_print (" object at %p (klass: %p)\n", obj, klass);
697 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
700 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, field->name);
701 type = mono_type_get_underlying_type (field->type);
703 switch (type->type) {
707 case MONO_TYPE_FNPTR:
708 g_print ("%p\n", *(const void**)field_ptr);
710 case MONO_TYPE_STRING:
711 case MONO_TYPE_SZARRAY:
712 case MONO_TYPE_CLASS:
713 case MONO_TYPE_OBJECT:
714 case MONO_TYPE_ARRAY:
715 mono_object_describe (*(MonoObject**)field_ptr);
717 case MONO_TYPE_GENERICINST:
718 if (!mono_type_generic_inst_is_valuetype (type)) {
719 mono_object_describe (*(MonoObject**)field_ptr);
724 case MONO_TYPE_VALUETYPE: {
725 MonoClass *k = mono_class_from_mono_type (type);
726 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
730 g_print ("%d\n", *(gint8*)field_ptr);
733 g_print ("%d\n", *(guint8*)field_ptr);
736 g_print ("%d\n", *(gint16*)field_ptr);
739 g_print ("%d\n", *(guint16*)field_ptr);
742 g_print ("%d\n", *(gint32*)field_ptr);
745 g_print ("%u\n", *(guint32*)field_ptr);
748 g_print ("%lld\n", (long long int)*(gint64*)field_ptr);
751 g_print ("%llu\n", (long long unsigned int)*(guint64*)field_ptr);
754 g_print ("%f\n", *(gfloat*)field_ptr);
757 g_print ("%f\n", *(gdouble*)field_ptr);
759 case MONO_TYPE_BOOLEAN:
760 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
763 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
766 g_assert_not_reached ();
772 objval_describe (MonoClass *class, const char *addr)
774 MonoClassField *field;
776 const char *field_ptr;
777 gssize type_offset = 0;
778 if (class->valuetype)
779 type_offset = -sizeof (MonoObject);
781 for (p = class; p != NULL; p = p->parent) {
782 gpointer iter = NULL;
783 int printed_header = FALSE;
784 while ((field = mono_class_get_fields (p, &iter))) {
785 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
788 if (p != class && !printed_header) {
790 g_print ("In class ");
791 sep = print_name_space (p);
792 g_print ("%s%s:\n", sep, p->name);
793 printed_header = TRUE;
795 field_ptr = (const char*)addr + field->offset + type_offset;
797 print_field_value (field_ptr, field, type_offset);
803 * mono_object_describe_fields:
805 * Prints to stdout a small description of each field of the object @obj.
806 * For use in a debugger.
809 mono_object_describe_fields (MonoObject *obj)
811 MonoClass *class = mono_object_class (obj);
812 objval_describe (class, (char*)obj);
816 * mono_value_describe_fields:
818 * Prints to stdout a small description of each field of the value type
819 * stored at @addr of type @klass.
820 * For use in a debugger.
823 mono_value_describe_fields (MonoClass* klass, const char* addr)
825 objval_describe (klass, addr);
829 * mono_class_describe_statics:
831 * Prints to stdout a small description of each static field of the type @klass
832 * in the current application domain.
833 * For use in a debugger.
836 mono_class_describe_statics (MonoClass* klass)
838 MonoClassField *field;
840 const char *field_ptr;
841 const char *addr = mono_class_vtable (mono_domain_get (), klass)->data;
845 for (p = klass; p != NULL; p = p->parent) {
846 gpointer iter = NULL;
847 while ((field = mono_class_get_fields (p, &iter))) {
848 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
850 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
853 field_ptr = (const char*)addr + field->offset;
855 print_field_value (field_ptr, field, 0);