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",
36 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
39 g_string_append (res, "Unknown");
42 if (class->nested_in) {
43 append_class_name (res, class->nested_in, include_namespace);
44 g_string_append_c (res, '/');
46 if (include_namespace && *(class->name_space))
47 g_string_sprintfa (res, "%s.", class->name_space);
48 g_string_sprintfa (res, "%s", class->name);
52 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
55 g_string_append (res, "void"); break;
57 g_string_append (res, "char"); break;
58 case MONO_TYPE_BOOLEAN:
59 g_string_append (res, "bool"); break;
61 g_string_append (res, "byte"); break;
63 g_string_append (res, "sbyte"); break;
65 g_string_append (res, "uint16"); break;
67 g_string_append (res, "int16"); break;
69 g_string_append (res, "uint"); break;
71 g_string_append (res, "int"); break;
73 g_string_append (res, "ulong"); break;
75 g_string_append (res, "long"); break;
76 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
77 g_string_append (res, "*()"); break;
79 g_string_append (res, "uintptr"); break;
81 g_string_append (res, "intptr"); break;
83 g_string_append (res, "single"); break;
85 g_string_append (res, "double"); break;
86 case MONO_TYPE_STRING:
87 g_string_append (res, "string"); break;
88 case MONO_TYPE_OBJECT:
89 g_string_append (res, "object"); break;
91 mono_type_get_desc (res, type->data.type, include_namespace);
92 g_string_append_c (res, '*');
95 append_class_name (res, type->data.array->eklass, include_namespace);
96 g_string_sprintfa (res, "[%d]", type->data.array->rank);
98 case MONO_TYPE_SZARRAY:
99 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
100 g_string_append (res, "[]");
102 case MONO_TYPE_CLASS:
103 case MONO_TYPE_VALUETYPE:
104 append_class_name (res, type->data.klass, include_namespace);
110 g_string_append_c (res, '&');
114 mono_type_full_name (MonoType *type)
119 str = g_string_new ("");
120 mono_type_get_desc (str, type, TRUE);
122 res = g_strdup (str->str);
123 g_string_free (str, TRUE);
128 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
132 GString *res = g_string_new ("");
134 for (i = 0; i < sig->param_count; ++i) {
136 g_string_append_c (res, ',');
137 mono_type_get_desc (res, sig->params [i], include_namespace);
140 g_string_free (res, FALSE);
145 * mono_method_desc_new:
147 * Creates a method description for `name', which conforms to the following
150 * [namespace.]classname:methodname[(args...)]
152 * in all the loaded assemblies.
154 * Returns a parsed representation of the method description.
157 mono_method_desc_new (const char *name, gboolean include_namespace)
159 MonoMethodDesc *result;
160 char *class_name, *class_nspace, *method_name, *use_args, *end;
163 class_nspace = g_strdup (name);
164 use_args = strchr (class_nspace, '(');
167 end = strchr (use_args, ')');
169 g_free (class_nspace);
174 method_name = strrchr (class_nspace, ':');
176 g_free (class_nspace);
180 /* allow two :: to separate the method name */
181 if (*method_name == ':')
183 class_name = strrchr (class_nspace, '.');
188 class_name = class_nspace;
191 result = g_new0 (MonoMethodDesc, 1);
192 result->include_namespace = include_namespace;
193 result->name = method_name;
194 result->klass = class_name;
195 result->namespace = use_namespace? class_nspace: NULL;
196 result->args = use_args? use_args: NULL;
200 result->num_args = 1;
212 mono_method_desc_from_method (MonoMethod *method)
214 MonoMethodDesc *result;
216 result = g_new0 (MonoMethodDesc, 1);
217 result->include_namespace = TRUE;
218 result->name = g_strdup (method->name);
219 result->klass = g_strdup (method->klass->name);
220 result->namespace = g_strdup (method->klass->name_space);
226 * mono_method_desc_free:
228 * Releases the MonoMethodDesc object `desc'.
231 mono_method_desc_free (MonoMethodDesc *desc)
234 g_free (desc->namespace);
235 else if (desc->klass)
236 g_free (desc->klass);
241 * namespace and class are supposed to match already if this function is used.
244 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
247 if (strcmp (desc->name, method->name))
251 if (desc->num_args != method->signature->param_count)
253 sig = mono_signature_get_desc (method->signature, desc->include_namespace);
254 if (strcmp (sig, desc->args)) {
263 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
265 if (strcmp (desc->klass, method->klass->name))
267 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
269 return mono_method_desc_match (desc, method);
273 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
277 mono_class_init (klass);
278 for (i = 0; i < klass->method.count; ++i) {
279 if (mono_method_desc_match (desc, klass->methods [i]))
280 return klass->methods [i];
286 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
290 MonoTableInfo *methods;
294 if (desc->namespace && desc->klass) {
295 klass = mono_class_from_name (image, desc->namespace, desc->klass);
298 return mono_method_desc_search_in_class (desc, klass);
301 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
302 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
303 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
304 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
305 const char *n = mono_metadata_string_heap (image, token);
307 if (strcmp (n, desc->name))
309 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
310 if (mono_method_desc_full_match (desc, method))
316 static const unsigned char*
317 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
319 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
320 const MonoOpcode *opcode;
321 guint32 i, label, token;
325 label = ip - header->code;
327 tmp = dh->indenter (dh, method, label);
328 g_string_append (str, tmp);
331 if (dh->label_format)
332 g_string_sprintfa (str, dh->label_format, label);
334 i = mono_opcode_value (&ip, end);
336 opcode = &mono_opcodes [i];
337 g_string_sprintfa (str, "%-10s", mono_opcode_names [i]);
339 switch (opcode->argument) {
343 case MonoInlineField:
344 case MonoInlineMethod:
349 tmp = dh->tokener (dh, method, token);
350 g_string_append (str, tmp);
353 g_string_sprintfa (str, "0x%08x", token);
357 case MonoInlineString:
362 g_string_sprintfa (str, "%d", read16 (ip));
365 case MonoShortInlineVar:
366 g_string_sprintfa (str, "%d", (*ip));
369 case MonoInlineBrTarget:
372 if (dh->label_target)
373 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
375 g_string_sprintfa (str, "%d", sval);
377 case MonoShortInlineBrTarget:
378 sval = *(const signed char*)ip;
380 if (dh->label_target)
381 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
383 g_string_sprintfa (str, "%d", sval);
385 case MonoInlineSwitch: {
386 const unsigned char *end;
390 g_string_append_c (str, '(');
391 for (i = 0; i < sval; ++i) {
393 g_string_append (str, ", ");
395 if (dh->label_target)
396 g_string_sprintfa (str, dh->label_target, end + label - header->code);
398 g_string_sprintfa (str, "%d", label);
401 g_string_append_c (str, ')');
407 g_string_sprintfa (str, "%g", r);
411 case MonoShortInlineR: {
414 g_string_sprintfa (str, "%g", r);
419 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
422 case MonoShortInlineI:
423 g_string_sprintfa (str, "%d", *(const signed char*)ip);
430 g_assert_not_reached ();
433 g_string_append (str, dh->newline);
441 "IL_%04x: ", /* label_format */
442 "IL_%04x", /* label_target */
449 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
452 GString *res = g_string_new ("");
456 /* set ip + 2 as the end: this is just a debugging method */
457 ip = dis_one (res, dh, method, ip, ip + 2);
462 g_string_free (res, FALSE);
467 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
470 GString *res = g_string_new ("");
475 ip = dis_one (res, dh, method, ip, end);
479 g_string_free (res, FALSE);
484 wrapper_type_to_str (guint32 wrapper_type)
486 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
488 return wrapper_type_names [wrapper_type];
492 mono_method_full_name (MonoMethod *method, gboolean signature)
498 nspace = method->klass->name_space;
501 char *tmpsig = mono_signature_get_desc (method->signature, TRUE);
503 if (method->wrapper_type != MONO_WRAPPER_NONE)
504 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
506 strcpy (wrapper, "");
507 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
508 nspace, *nspace ? "." : "",
509 method->klass->name, method->name, tmpsig);
513 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
514 nspace, *nspace ? "." : "",
515 method->klass->name, method->name);