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"
9 struct MonoMethodDesc {
15 gboolean include_namespace;
18 static const char *wrapper_type_names [] = {
21 "delegate-begin-invoke",
22 "delegate-end-invoke",
27 "remoting-invoke-with-check",
42 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
45 g_string_append (res, "Unknown");
48 if (class->nested_in) {
49 append_class_name (res, class->nested_in, include_namespace);
50 g_string_append_c (res, '/');
52 if (include_namespace && *(class->name_space))
53 g_string_sprintfa (res, "%s.", class->name_space);
54 g_string_sprintfa (res, "%s", class->name);
58 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
61 g_string_append (res, "void"); break;
63 g_string_append (res, "char"); break;
64 case MONO_TYPE_BOOLEAN:
65 g_string_append (res, "bool"); break;
67 g_string_append (res, "byte"); break;
69 g_string_append (res, "sbyte"); break;
71 g_string_append (res, "uint16"); break;
73 g_string_append (res, "int16"); break;
75 g_string_append (res, "uint"); break;
77 g_string_append (res, "int"); break;
79 g_string_append (res, "ulong"); break;
81 g_string_append (res, "long"); break;
82 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
83 g_string_append (res, "*()"); break;
85 g_string_append (res, "uintptr"); break;
87 g_string_append (res, "intptr"); break;
89 g_string_append (res, "single"); break;
91 g_string_append (res, "double"); break;
92 case MONO_TYPE_STRING:
93 g_string_append (res, "string"); break;
94 case MONO_TYPE_OBJECT:
95 g_string_append (res, "object"); break;
97 mono_type_get_desc (res, type->data.type, include_namespace);
98 g_string_append_c (res, '*');
100 case MONO_TYPE_ARRAY:
101 append_class_name (res, type->data.array->eklass, include_namespace);
102 g_string_sprintfa (res, "[%d]", type->data.array->rank);
104 case MONO_TYPE_SZARRAY:
105 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
106 g_string_append (res, "[]");
108 case MONO_TYPE_CLASS:
109 case MONO_TYPE_VALUETYPE:
110 append_class_name (res, type->data.klass, include_namespace);
112 case MONO_TYPE_GENERICINST:
113 mono_type_get_desc (res, type->data.generic_inst->generic_type, include_namespace);
119 g_string_append_c (res, '&');
123 mono_type_full_name (MonoType *type)
128 str = g_string_new ("");
129 mono_type_get_desc (str, type, TRUE);
131 res = g_strdup (str->str);
132 g_string_free (str, TRUE);
137 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
141 GString *res = g_string_new ("");
143 for (i = 0; i < sig->param_count; ++i) {
145 g_string_append_c (res, ',');
146 mono_type_get_desc (res, sig->params [i], include_namespace);
149 g_string_free (res, FALSE);
154 * mono_method_desc_new:
156 * Creates a method description for `name', which conforms to the following
159 * [namespace.]classname:methodname[(args...)]
161 * in all the loaded assemblies.
163 * Returns a parsed representation of the method description.
166 mono_method_desc_new (const char *name, gboolean include_namespace)
168 MonoMethodDesc *result;
169 char *class_name, *class_nspace, *method_name, *use_args, *end;
172 class_nspace = g_strdup (name);
173 use_args = strchr (class_nspace, '(');
176 end = strchr (use_args, ')');
178 g_free (class_nspace);
183 method_name = strrchr (class_nspace, ':');
185 g_free (class_nspace);
189 /* allow two :: to separate the method name */
190 if (*method_name == ':')
192 class_name = strrchr (class_nspace, '.');
197 class_name = class_nspace;
200 result = g_new0 (MonoMethodDesc, 1);
201 result->include_namespace = include_namespace;
202 result->name = method_name;
203 result->klass = class_name;
204 result->namespace = use_namespace? class_nspace: NULL;
205 result->args = use_args? use_args: NULL;
209 result->num_args = 1;
221 mono_method_desc_from_method (MonoMethod *method)
223 MonoMethodDesc *result;
225 result = g_new0 (MonoMethodDesc, 1);
226 result->include_namespace = TRUE;
227 result->name = g_strdup (method->name);
228 result->klass = g_strdup (method->klass->name);
229 result->namespace = g_strdup (method->klass->name_space);
235 * mono_method_desc_free:
237 * Releases the MonoMethodDesc object `desc'.
240 mono_method_desc_free (MonoMethodDesc *desc)
243 g_free (desc->namespace);
244 else if (desc->klass)
245 g_free (desc->klass);
250 * namespace and class are supposed to match already if this function is used.
253 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
256 if (strcmp (desc->name, method->name))
260 if (desc->num_args != method->signature->param_count)
262 sig = mono_signature_get_desc (method->signature, desc->include_namespace);
263 if (strcmp (sig, desc->args)) {
272 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
274 if (strcmp (desc->klass, method->klass->name))
276 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
278 return mono_method_desc_match (desc, method);
282 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
286 mono_class_init (klass);
287 for (i = 0; i < klass->method.count; ++i) {
288 if (mono_method_desc_match (desc, klass->methods [i]))
289 return klass->methods [i];
295 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
298 const MonoTableInfo *tdef;
299 const MonoTableInfo *methods;
303 if (desc->namespace && desc->klass) {
304 klass = mono_class_from_name (image, desc->namespace, desc->klass);
307 return mono_method_desc_search_in_class (desc, klass);
310 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
311 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
312 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
313 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
314 const char *n = mono_metadata_string_heap (image, token);
316 if (strcmp (n, desc->name))
318 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
319 if (mono_method_desc_full_match (desc, method))
325 static const unsigned char*
326 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
328 MonoMethodHeader *header = mono_method_get_header (method);
329 const MonoOpcode *opcode;
330 guint32 i, label, token;
334 label = ip - header->code;
336 tmp = dh->indenter (dh, method, label);
337 g_string_append (str, tmp);
340 if (dh->label_format)
341 g_string_sprintfa (str, dh->label_format, label);
343 i = mono_opcode_value (&ip, end);
345 opcode = &mono_opcodes [i];
346 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
348 switch (opcode->argument) {
352 case MonoInlineField:
353 case MonoInlineMethod:
358 tmp = dh->tokener (dh, method, token);
359 g_string_append (str, tmp);
362 g_string_sprintfa (str, "0x%08x", token);
366 case MonoInlineString:
371 g_string_sprintfa (str, "%d", read16 (ip));
374 case MonoShortInlineVar:
375 g_string_sprintfa (str, "%d", (*ip));
378 case MonoInlineBrTarget:
381 if (dh->label_target)
382 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
384 g_string_sprintfa (str, "%d", sval);
386 case MonoShortInlineBrTarget:
387 sval = *(const signed char*)ip;
389 if (dh->label_target)
390 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
392 g_string_sprintfa (str, "%d", sval);
394 case MonoInlineSwitch: {
395 const unsigned char *end;
399 g_string_append_c (str, '(');
400 for (i = 0; i < sval; ++i) {
402 g_string_append (str, ", ");
404 if (dh->label_target)
405 g_string_sprintfa (str, dh->label_target, end + label - header->code);
407 g_string_sprintfa (str, "%d", label);
410 g_string_append_c (str, ')');
416 g_string_sprintfa (str, "%g", r);
420 case MonoShortInlineR: {
423 g_string_sprintfa (str, "%g", r);
428 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
431 case MonoShortInlineI:
432 g_string_sprintfa (str, "%d", *(const signed char*)ip);
439 g_assert_not_reached ();
442 g_string_append (str, dh->newline);
450 "IL_%04x: ", /* label_format */
451 "IL_%04x", /* label_target */
458 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
461 GString *res = g_string_new ("");
465 /* set ip + 2 as the end: this is just a debugging method */
466 ip = dis_one (res, dh, method, ip, ip + 2);
471 g_string_free (res, FALSE);
476 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
479 GString *res = g_string_new ("");
484 ip = dis_one (res, dh, method, ip, end);
488 g_string_free (res, FALSE);
493 wrapper_type_to_str (guint32 wrapper_type)
495 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
497 return wrapper_type_names [wrapper_type];
501 mono_method_full_name (MonoMethod *method, gboolean signature)
505 const char *nspace = method->klass->name_space;
508 char *tmpsig = mono_signature_get_desc (method->signature, TRUE);
510 if (method->wrapper_type != MONO_WRAPPER_NONE)
511 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
513 strcpy (wrapper, "");
514 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
515 nspace, *nspace ? "." : "",
516 method->klass->name, method->name, tmpsig);
520 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
521 nspace, *nspace ? "." : "",
522 method->klass->name, method->name);
529 mono_find_method_by_name (MonoClass *klass, const char *name, int param_count)
531 MonoMethod *res = NULL;
534 mono_class_init (klass);
536 for (i = 0; i < klass->method.count; ++i) {
537 if (klass->methods [i]->name[0] == name [0] &&
538 !strcmp (name, klass->methods [i]->name) &&
539 klass->methods [i]->signature->param_count == param_count) {
540 res = klass->methods [i];