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 append_class_name (res, type->data.array->eklass, 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 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
304 if (strcmp (desc->klass, method->klass->name))
306 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
308 return mono_method_desc_match (desc, method);
312 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
315 gpointer iter = NULL;
317 while ((m = mono_class_get_methods (klass, &iter)))
318 if (mono_method_desc_match (desc, m))
324 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
327 const MonoTableInfo *tdef;
328 const MonoTableInfo *methods;
332 if (desc->namespace && desc->klass) {
333 klass = mono_class_from_name (image, desc->namespace, desc->klass);
336 return mono_method_desc_search_in_class (desc, klass);
339 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
340 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
341 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
342 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
343 const char *n = mono_metadata_string_heap (image, token);
345 if (strcmp (n, desc->name))
347 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
348 if (mono_method_desc_full_match (desc, method))
354 static const unsigned char*
355 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
357 MonoMethodHeader *header = mono_method_get_header (method);
358 const MonoOpcode *opcode;
359 guint32 label, token;
363 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
365 label = ip - il_code;
367 tmp = dh->indenter (dh, method, label);
368 g_string_append (str, tmp);
371 if (dh->label_format)
372 g_string_sprintfa (str, dh->label_format, label);
374 i = mono_opcode_value (&ip, end);
376 opcode = &mono_opcodes [i];
377 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
379 switch (opcode->argument) {
383 case MonoInlineField:
384 case MonoInlineMethod:
389 tmp = dh->tokener (dh, method, token);
390 g_string_append (str, tmp);
393 g_string_sprintfa (str, "0x%08x", token);
397 case MonoInlineString:
402 g_string_sprintfa (str, "%d", read16 (ip));
405 case MonoShortInlineVar:
406 g_string_sprintfa (str, "%d", (*ip));
409 case MonoInlineBrTarget:
412 if (dh->label_target)
413 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
415 g_string_sprintfa (str, "%d", sval);
417 case MonoShortInlineBrTarget:
418 sval = *(const signed char*)ip;
420 if (dh->label_target)
421 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
423 g_string_sprintfa (str, "%d", sval);
425 case MonoInlineSwitch: {
426 const unsigned char *end;
430 g_string_append_c (str, '(');
431 for (i = 0; i < sval; ++i) {
433 g_string_append (str, ", ");
435 if (dh->label_target)
436 g_string_sprintfa (str, dh->label_target, end + label - il_code);
438 g_string_sprintfa (str, "%d", label);
441 g_string_append_c (str, ')');
447 g_string_sprintfa (str, "%g", r);
451 case MonoShortInlineR: {
454 g_string_sprintfa (str, "%g", r);
459 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
462 case MonoShortInlineI:
463 g_string_sprintfa (str, "%d", *(const signed char*)ip);
470 g_assert_not_reached ();
473 g_string_append (str, dh->newline);
481 "IL_%04x: ", /* label_format */
482 "IL_%04x", /* label_target */
489 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
492 GString *res = g_string_new ("");
496 /* set ip + 2 as the end: this is just a debugging method */
497 ip = dis_one (res, dh, method, ip, ip + 2);
502 g_string_free (res, FALSE);
507 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
510 GString *res = g_string_new ("");
515 ip = dis_one (res, dh, method, ip, end);
519 g_string_free (res, FALSE);
524 mono_method_full_name (MonoMethod *method, gboolean signature)
528 const char *nspace = method->klass->name_space;
531 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
533 if (method->wrapper_type != MONO_WRAPPER_NONE)
534 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
536 strcpy (wrapper, "");
537 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
538 nspace, *nspace ? "." : "",
539 method->klass->name, method->name, tmpsig);
543 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
544 nspace, *nspace ? "." : "",
545 method->klass->name, method->name);
552 print_name_space (MonoClass *klass)
554 if (klass->nested_in) {
555 print_name_space (klass->nested_in);
556 g_print (klass->nested_in->name);
559 if (klass->name_space [0]) {
560 g_print (klass->name_space);
567 * mono_object_describe:
569 * Prints to stdout a small description of the object @obj.
570 * For use in a debugger.
573 mono_object_describe (MonoObject *obj)
578 g_print ("(null)\n");
581 klass = mono_object_class (obj);
582 if (klass == mono_defaults.string_class) {
583 char *utf8 = mono_string_to_utf8 ((MonoString*)obj);
584 if (strlen (utf8) > 60) {
590 g_print ("String at %p, length: %d, '%s'\n", obj, mono_string_length ((MonoString*) obj), utf8);
592 } else if (klass->rank) {
593 MonoArray *array = (MonoArray*)obj;
594 sep = print_name_space (klass);
595 g_print ("%s%s", sep, klass->name);
596 g_print (" at %p, rank: %d, length: %d\n", obj, klass->rank, mono_array_length (array));
598 sep = print_name_space (klass);
599 g_print ("%s%s", sep, klass->name);
600 g_print (" object at %p (klass: %p)\n", obj, klass);
606 print_field_value (const char *field_ptr, MonoClassField *field, int type_offset)
609 g_print ("At %p (ofs: %2d) %s: ", field_ptr, field->offset + type_offset, field->name);
610 type = mono_type_get_underlying_type (field->type);
612 switch (type->type) {
616 case MONO_TYPE_FNPTR:
617 g_print ("%p\n", *(const void**)field_ptr);
619 case MONO_TYPE_STRING:
620 case MONO_TYPE_SZARRAY:
621 case MONO_TYPE_CLASS:
622 case MONO_TYPE_OBJECT:
623 case MONO_TYPE_ARRAY:
624 mono_object_describe (*(MonoObject**)field_ptr);
626 case MONO_TYPE_GENERICINST:
627 if (!mono_type_generic_inst_is_valuetype (type)) {
628 mono_object_describe (*(MonoObject**)field_ptr);
633 case MONO_TYPE_VALUETYPE: {
634 MonoClass *k = mono_class_from_mono_type (type);
635 g_print ("%s ValueType (type: %p) at %p\n", k->name, k, field_ptr);
639 g_print ("%d\n", *(gint8*)field_ptr);
642 g_print ("%d\n", *(guint8*)field_ptr);
645 g_print ("%d\n", *(gint16*)field_ptr);
648 g_print ("%d\n", *(guint16*)field_ptr);
651 g_print ("%d\n", *(gint32*)field_ptr);
654 g_print ("%u\n", *(guint32*)field_ptr);
657 g_print ("%lld\n", *(gint64*)field_ptr);
660 g_print ("%llu\n", *(guint64*)field_ptr);
663 g_print ("%f\n", *(gfloat*)field_ptr);
666 g_print ("%f\n", *(gdouble*)field_ptr);
668 case MONO_TYPE_BOOLEAN:
669 g_print ("%s (%d)\n", *(guint8*)field_ptr? "True": "False", *(guint8*)field_ptr);
672 g_print ("'%c' (%d 0x%04x)\n", *(guint16*)field_ptr, *(guint16*)field_ptr, *(guint16*)field_ptr);
675 g_assert_not_reached ();
681 objval_describe (MonoClass *class, const char *addr)
683 MonoClassField *field;
685 const char *field_ptr;
687 if (class->valuetype)
688 type_offset = -sizeof (MonoObject);
690 for (p = class; p != NULL; p = p->parent) {
691 gpointer iter = NULL;
692 int printed_header = FALSE;
693 while ((field = mono_class_get_fields (p, &iter))) {
694 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
697 if (p != class && !printed_header) {
699 g_print ("In class ");
700 sep = print_name_space (p);
701 g_print ("%s%s:\n", sep, p->name);
702 printed_header = TRUE;
704 field_ptr = (const char*)addr + field->offset + type_offset;
706 print_field_value (field_ptr, field, type_offset);
712 * mono_object_describe_fields:
714 * Prints to stdout a small description of each field of the object @obj.
715 * For use in a debugger.
718 mono_object_describe_fields (MonoObject *obj)
720 MonoClass *class = mono_object_class (obj);
721 objval_describe (class, (char*)obj);
725 * mono_value_describe_fields:
727 * Prints to stdout a small description of each field of the value type
728 * stored at @addr of type @klass.
729 * For use in a debugger.
732 mono_value_describe_fields (MonoClass* klass, const char* addr)
734 objval_describe (klass, addr);
738 * mono_class_describe_statics:
740 * Prints to stdout a small description of each static field of the type @klass
741 * in the current application domain.
742 * For use in a debugger.
745 mono_class_describe_statics (MonoClass* klass)
747 MonoClassField *field;
749 const char *field_ptr;
750 const char *addr = mono_class_vtable (mono_domain_get (), klass)->data;
754 for (p = klass; p != NULL; p = p->parent) {
755 gpointer iter = NULL;
756 while ((field = mono_class_get_fields (p, &iter))) {
757 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
759 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
762 field_ptr = (const char*)addr + field->offset;
764 print_field_value (field_ptr, field, 0);