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) {
84 g_string_append (res, "void"); break;
86 g_string_append (res, "char"); break;
87 case MONO_TYPE_BOOLEAN:
88 g_string_append (res, "bool"); break;
90 g_string_append (res, "byte"); break;
92 g_string_append (res, "sbyte"); break;
94 g_string_append (res, "uint16"); break;
96 g_string_append (res, "int16"); break;
98 g_string_append (res, "uint"); break;
100 g_string_append (res, "int"); break;
102 g_string_append (res, "ulong"); break;
104 g_string_append (res, "long"); break;
105 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
106 g_string_append (res, "*()"); break;
108 g_string_append (res, "uintptr"); break;
110 g_string_append (res, "intptr"); break;
112 g_string_append (res, "single"); break;
114 g_string_append (res, "double"); break;
115 case MONO_TYPE_STRING:
116 g_string_append (res, "string"); break;
117 case MONO_TYPE_OBJECT:
118 g_string_append (res, "object"); break;
120 mono_type_get_desc (res, type->data.type, include_namespace);
121 g_string_append_c (res, '*');
123 case MONO_TYPE_ARRAY:
124 mono_type_get_desc (res, &type->data.array->eklass->byval_arg, include_namespace);
125 g_string_sprintfa (res, "[%d]", type->data.array->rank);
127 case MONO_TYPE_SZARRAY:
128 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
129 g_string_append (res, "[]");
131 case MONO_TYPE_CLASS:
132 case MONO_TYPE_VALUETYPE:
133 append_class_name (res, type->data.klass, include_namespace);
135 case MONO_TYPE_GENERICINST:
136 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
140 g_string_append (res, type->data.generic_param->name);
146 g_string_append_c (res, '&');
150 mono_type_full_name (MonoType *type)
155 str = g_string_new ("");
156 mono_type_get_desc (str, type, TRUE);
158 res = g_strdup (str->str);
159 g_string_free (str, TRUE);
164 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
168 GString *res = g_string_new ("");
170 for (i = 0; i < sig->param_count; ++i) {
172 g_string_append_c (res, ',');
173 mono_type_get_desc (res, sig->params [i], include_namespace);
176 g_string_free (res, FALSE);
181 * mono_method_desc_new:
182 * @name: the method name.
183 * @include_namespace: whether the name includes a namespace or not.
185 * Creates a method description for @name, which conforms to the following
188 * [namespace.]classname:methodname[(args...)]
190 * in all the loaded assemblies.
192 * Returns: a parsed representation of the method description.
195 mono_method_desc_new (const char *name, gboolean include_namespace)
197 MonoMethodDesc *result;
198 char *class_name, *class_nspace, *method_name, *use_args, *end;
201 class_nspace = g_strdup (name);
202 use_args = strchr (class_nspace, '(');
205 end = strchr (use_args, ')');
207 g_free (class_nspace);
212 method_name = strrchr (class_nspace, ':');
214 g_free (class_nspace);
218 /* allow two :: to separate the method name */
219 if (*method_name == ':')
221 class_name = strrchr (class_nspace, '.');
226 class_name = class_nspace;
229 result = g_new0 (MonoMethodDesc, 1);
230 result->include_namespace = include_namespace;
231 result->name = method_name;
232 result->klass = class_name;
233 result->namespace = use_namespace? class_nspace: NULL;
234 result->args = use_args? use_args: NULL;
238 result->num_args = 1;
250 mono_method_desc_from_method (MonoMethod *method)
252 MonoMethodDesc *result;
254 result = g_new0 (MonoMethodDesc, 1);
255 result->include_namespace = TRUE;
256 result->name = g_strdup (method->name);
257 result->klass = g_strdup (method->klass->name);
258 result->namespace = g_strdup (method->klass->name_space);
264 * mono_method_desc_free:
265 * @desc: method description to be released
267 * Releases the MonoMethodDesc object @desc.
270 mono_method_desc_free (MonoMethodDesc *desc)
273 g_free (desc->namespace);
274 else if (desc->klass)
275 g_free (desc->klass);
280 * namespace and class are supposed to match already if this function is used.
283 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
286 if (strcmp (desc->name, method->name))
290 if (desc->num_args != mono_method_signature (method)->param_count)
292 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
293 if (strcmp (sig, desc->args)) {
302 my_strrchr (const char *str, char ch, int *len)
306 for (pos = (*len)-1; pos >= 0; pos--) {
318 match_class (MonoMethodDesc *desc, int pos, MonoClass *klass)
322 p = my_strrchr (desc->klass, '/', &pos);
324 if (strncmp (desc->klass, klass->name, pos))
326 if (desc->namespace && strcmp (desc->namespace, klass->name_space))
331 if (strcmp (p+1, klass->name))
333 if (!klass->nested_in)
336 return match_class (desc, pos, klass->nested_in);
340 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
342 if (!match_class (desc, strlen (desc->klass), method->klass))
345 return mono_method_desc_match (desc, method);
349 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
352 gpointer iter = NULL;
354 while ((m = mono_class_get_methods (klass, &iter)))
355 if (mono_method_desc_match (desc, m))
361 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
364 const MonoTableInfo *tdef;
365 const MonoTableInfo *methods;
369 if (desc->namespace && desc->klass) {
370 klass = mono_class_from_name (image, desc->namespace, desc->klass);
373 return mono_method_desc_search_in_class (desc, klass);
376 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
377 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
378 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
379 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
380 const char *n = mono_metadata_string_heap (image, token);
382 if (strcmp (n, desc->name))
384 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
385 if (mono_method_desc_full_match (desc, method))
391 static const unsigned char*
392 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
394 MonoMethodHeader *header = mono_method_get_header (method);
395 const MonoOpcode *opcode;
396 guint32 label, token;
400 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
402 label = ip - il_code;
404 tmp = dh->indenter (dh, method, label);
405 g_string_append (str, tmp);
408 if (dh->label_format)
409 g_string_sprintfa (str, dh->label_format, label);
411 i = mono_opcode_value (&ip, end);
413 opcode = &mono_opcodes [i];
414 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
416 switch (opcode->argument) {
420 case MonoInlineField:
421 case MonoInlineMethod:
426 tmp = dh->tokener (dh, method, token);
427 g_string_append (str, tmp);
430 g_string_sprintfa (str, "0x%08x", token);
434 case MonoInlineString:
439 g_string_sprintfa (str, "%d", read16 (ip));
442 case MonoShortInlineVar:
443 g_string_sprintfa (str, "%d", (*ip));
446 case MonoInlineBrTarget:
449 if (dh->label_target)
450 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
452 g_string_sprintfa (str, "%d", sval);
454 case MonoShortInlineBrTarget:
455 sval = *(const signed char*)ip;
457 if (dh->label_target)
458 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
460 g_string_sprintfa (str, "%d", sval);
462 case MonoInlineSwitch: {
463 const unsigned char *end;
467 g_string_append_c (str, '(');
468 for (i = 0; i < sval; ++i) {
470 g_string_append (str, ", ");
472 if (dh->label_target)
473 g_string_sprintfa (str, dh->label_target, end + label - il_code);
475 g_string_sprintfa (str, "%d", label);
478 g_string_append_c (str, ')');
484 g_string_sprintfa (str, "%g", r);
488 case MonoShortInlineR: {
491 g_string_sprintfa (str, "%g", r);
496 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
499 case MonoShortInlineI:
500 g_string_sprintfa (str, "%d", *(const signed char*)ip);
507 g_assert_not_reached ();
510 g_string_append (str, dh->newline);
518 "IL_%04x: ", /* label_format */
519 "IL_%04x", /* label_target */
526 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
529 GString *res = g_string_new ("");
533 /* set ip + 2 as the end: this is just a debugging method */
534 ip = dis_one (res, dh, method, ip, ip + 2);
539 g_string_free (res, FALSE);
544 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
547 GString *res = g_string_new ("");
552 ip = dis_one (res, dh, method, ip, end);
556 g_string_free (res, FALSE);
561 mono_method_full_name (MonoMethod *method, gboolean signature)
565 const char *nspace = method->klass->name_space;
568 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
570 if (method->wrapper_type != MONO_WRAPPER_NONE)
571 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
573 strcpy (wrapper, "");
574 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
575 nspace, *nspace ? "." : "",
576 method->klass->name, method->name, tmpsig);
580 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
581 nspace, *nspace ? "." : "",
582 method->klass->name, method->name);
589 print_name_space (MonoClass *klass)
591 if (klass->nested_in) {
592 print_name_space (klass->nested_in);
593 g_print (klass->nested_in->name);
596 if (klass->name_space [0]) {
597 g_print (klass->name_space);
604 * mono_object_describe:
606 * Prints to stdout a small description of the object @obj.
607 * For use in a debugger.
610 mono_object_describe (MonoObject *obj)
615 g_print ("(null)\n");
618 klass = mono_object_class (obj);
619 if (klass == mono_defaults.string_class) {
620 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
621 if (strlen (utf8) > 60) {
627 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
629 } else if (klass->rank) {
630 MonoArray *array = (MonoArray*)obj;
631 sep = print_name_space (klass);
632 g_print ("%s%s", sep, klass->name);
633 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
635 sep = print_name_space (klass);
636 g_print ("%s%s", sep, klass->name);
637 g_print (" object at %p (klass: %p)\n", obj, klass);
643 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
646 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, field->name);
647 type = mono_type_get_underlying_type (field->type);
649 switch (type->type) {
653 case MONO_TYPE_FNPTR:
654 g_print ("%p\n", *(const void**)field_ptr);
656 case MONO_TYPE_STRING:
657 case MONO_TYPE_SZARRAY:
658 case MONO_TYPE_CLASS:
659 case MONO_TYPE_OBJECT:
660 case MONO_TYPE_ARRAY:
661 mono_object_describe (*(MonoObject**)field_ptr);
663 case MONO_TYPE_GENERICINST:
664 if (!mono_type_generic_inst_is_valuetype (type)) {
665 mono_object_describe (*(MonoObject**)field_ptr);
670 case MONO_TYPE_VALUETYPE: {
671 MonoClass *k = mono_class_from_mono_type (type);
672 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
676 g_print ("%d\n", *(gint8*)field_ptr);
679 g_print ("%d\n", *(guint8*)field_ptr);
682 g_print ("%d\n", *(gint16*)field_ptr);
685 g_print ("%d\n", *(guint16*)field_ptr);
688 g_print ("%d\n", *(gint32*)field_ptr);
691 g_print ("%u\n", *(guint32*)field_ptr);
694 g_print ("%lld\n", *(gint64*)field_ptr);
697 g_print ("%llu\n", *(guint64*)field_ptr);
700 g_print ("%f\n", *(gfloat*)field_ptr);
703 g_print ("%f\n", *(gdouble*)field_ptr);
705 case MONO_TYPE_BOOLEAN:
706 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
709 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
712 g_assert_not_reached ();
718 objval_describe (MonoClass *class, const char *addr)
720 MonoClassField *field;
722 const char *field_ptr;
724 if (class->valuetype)
725 type_offset = -sizeof (MonoObject);
727 for (p = class; p != NULL; p = p->parent) {
728 gpointer iter = NULL;
729 int printed_header = FALSE;
730 while ((field = mono_class_get_fields (p, &iter))) {
731 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
734 if (p != class && !printed_header) {
736 g_print ("In class ");
737 sep = print_name_space (p);
738 g_print ("%s%s:\n", sep, p->name);
739 printed_header = TRUE;
741 field_ptr = (const char*)addr + field->offset + type_offset;
743 print_field_value (field_ptr, field, type_offset);
749 * mono_object_describe_fields:
751 * Prints to stdout a small description of each field of the object @obj.
752 * For use in a debugger.
755 mono_object_describe_fields (MonoObject *obj)
757 MonoClass *class = mono_object_class (obj);
758 objval_describe (class, (char*)obj);
762 * mono_value_describe_fields:
764 * Prints to stdout a small description of each field of the value type
765 * stored at @addr of type @klass.
766 * For use in a debugger.
769 mono_value_describe_fields (MonoClass* klass, const char* addr)
771 objval_describe (klass, addr);
775 * mono_class_describe_statics:
777 * Prints to stdout a small description of each static field of the type @klass
778 * in the current application domain.
779 * For use in a debugger.
782 mono_class_describe_statics (MonoClass* klass)
784 MonoClassField *field;
786 const char *field_ptr;
787 const char *addr = mono_class_vtable (mono_domain_get (), klass)->data;
791 for (p = klass; p != NULL; p = p->parent) {
792 gpointer iter = NULL;
793 while ((field = mono_class_get_fields (p, &iter))) {
794 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
796 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
799 field_ptr = (const char*)addr + field->offset;
801 print_field_value (field_ptr, field, 0);