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_class->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:
155 * @name: the method name.
156 * @include_namespace: whether the name includes a namespace or not.
158 * Creates a method description for @name, which conforms to the following
161 * [namespace.]classname:methodname[(args...)]
163 * in all the loaded assemblies.
165 * Returns: a parsed representation of the method description.
168 mono_method_desc_new (const char *name, gboolean include_namespace)
170 MonoMethodDesc *result;
171 char *class_name, *class_nspace, *method_name, *use_args, *end;
174 class_nspace = g_strdup (name);
175 use_args = strchr (class_nspace, '(');
178 end = strchr (use_args, ')');
180 g_free (class_nspace);
185 method_name = strrchr (class_nspace, ':');
187 g_free (class_nspace);
191 /* allow two :: to separate the method name */
192 if (*method_name == ':')
194 class_name = strrchr (class_nspace, '.');
199 class_name = class_nspace;
202 result = g_new0 (MonoMethodDesc, 1);
203 result->include_namespace = include_namespace;
204 result->name = method_name;
205 result->klass = class_name;
206 result->namespace = use_namespace? class_nspace: NULL;
207 result->args = use_args? use_args: NULL;
211 result->num_args = 1;
223 mono_method_desc_from_method (MonoMethod *method)
225 MonoMethodDesc *result;
227 result = g_new0 (MonoMethodDesc, 1);
228 result->include_namespace = TRUE;
229 result->name = g_strdup (method->name);
230 result->klass = g_strdup (method->klass->name);
231 result->namespace = g_strdup (method->klass->name_space);
237 * mono_method_desc_free:
238 * @desc: method description to be released
240 * Releases the MonoMethodDesc object @desc.
243 mono_method_desc_free (MonoMethodDesc *desc)
246 g_free (desc->namespace);
247 else if (desc->klass)
248 g_free (desc->klass);
253 * namespace and class are supposed to match already if this function is used.
256 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
259 if (strcmp (desc->name, method->name))
263 if (desc->num_args != method->signature->param_count)
265 sig = mono_signature_get_desc (method->signature, desc->include_namespace);
266 if (strcmp (sig, desc->args)) {
275 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
277 if (strcmp (desc->klass, method->klass->name))
279 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
281 return mono_method_desc_match (desc, method);
285 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
289 mono_class_init (klass);
290 for (i = 0; i < klass->method.count; ++i) {
291 if (mono_method_desc_match (desc, klass->methods [i]))
292 return klass->methods [i];
298 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
301 const MonoTableInfo *tdef;
302 const MonoTableInfo *methods;
306 if (desc->namespace && desc->klass) {
307 klass = mono_class_from_name (image, desc->namespace, desc->klass);
310 return mono_method_desc_search_in_class (desc, klass);
313 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
314 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
315 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
316 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
317 const char *n = mono_metadata_string_heap (image, token);
319 if (strcmp (n, desc->name))
321 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
322 if (mono_method_desc_full_match (desc, method))
328 static const unsigned char*
329 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
331 MonoMethodHeader *header = mono_method_get_header (method);
332 const MonoOpcode *opcode;
333 guint32 i, label, token;
337 label = ip - header->code;
339 tmp = dh->indenter (dh, method, label);
340 g_string_append (str, tmp);
343 if (dh->label_format)
344 g_string_sprintfa (str, dh->label_format, label);
346 i = mono_opcode_value (&ip, end);
348 opcode = &mono_opcodes [i];
349 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
351 switch (opcode->argument) {
355 case MonoInlineField:
356 case MonoInlineMethod:
361 tmp = dh->tokener (dh, method, token);
362 g_string_append (str, tmp);
365 g_string_sprintfa (str, "0x%08x", token);
369 case MonoInlineString:
374 g_string_sprintfa (str, "%d", read16 (ip));
377 case MonoShortInlineVar:
378 g_string_sprintfa (str, "%d", (*ip));
381 case MonoInlineBrTarget:
384 if (dh->label_target)
385 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
387 g_string_sprintfa (str, "%d", sval);
389 case MonoShortInlineBrTarget:
390 sval = *(const signed char*)ip;
392 if (dh->label_target)
393 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
395 g_string_sprintfa (str, "%d", sval);
397 case MonoInlineSwitch: {
398 const unsigned char *end;
402 g_string_append_c (str, '(');
403 for (i = 0; i < sval; ++i) {
405 g_string_append (str, ", ");
407 if (dh->label_target)
408 g_string_sprintfa (str, dh->label_target, end + label - header->code);
410 g_string_sprintfa (str, "%d", label);
413 g_string_append_c (str, ')');
419 g_string_sprintfa (str, "%g", r);
423 case MonoShortInlineR: {
426 g_string_sprintfa (str, "%g", r);
431 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
434 case MonoShortInlineI:
435 g_string_sprintfa (str, "%d", *(const signed char*)ip);
442 g_assert_not_reached ();
445 g_string_append (str, dh->newline);
453 "IL_%04x: ", /* label_format */
454 "IL_%04x", /* label_target */
461 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
464 GString *res = g_string_new ("");
468 /* set ip + 2 as the end: this is just a debugging method */
469 ip = dis_one (res, dh, method, ip, ip + 2);
474 g_string_free (res, FALSE);
479 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
482 GString *res = g_string_new ("");
487 ip = dis_one (res, dh, method, ip, end);
491 g_string_free (res, FALSE);
496 wrapper_type_to_str (guint32 wrapper_type)
498 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
500 return wrapper_type_names [wrapper_type];
504 mono_method_full_name (MonoMethod *method, gboolean signature)
508 const char *nspace = method->klass->name_space;
511 char *tmpsig = mono_signature_get_desc (method->signature, TRUE);
513 if (method->wrapper_type != MONO_WRAPPER_NONE)
514 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
516 strcpy (wrapper, "");
517 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
518 nspace, *nspace ? "." : "",
519 method->klass->name, method->name, tmpsig);
523 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
524 nspace, *nspace ? "." : "",
525 method->klass->name, method->name);
532 mono_find_method_by_name (MonoClass *klass, const char *name, int param_count)
534 MonoMethod *res = NULL;
537 mono_class_init (klass);
539 for (i = 0; i < klass->method.count; ++i) {
540 if (klass->methods [i]->name[0] == name [0] &&
541 !strcmp (name, klass->methods [i]->name) &&
542 klass->methods [i]->signature->param_count == param_count) {
543 res = klass->methods [i];