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",
46 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
49 g_string_append (res, "Unknown");
52 if (class->nested_in) {
53 append_class_name (res, class->nested_in, include_namespace);
54 g_string_append_c (res, '/');
56 if (include_namespace && *(class->name_space))
57 g_string_sprintfa (res, "%s.", class->name_space);
58 g_string_sprintfa (res, "%s", class->name);
62 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
65 g_string_append (res, "void"); break;
67 g_string_append (res, "char"); break;
68 case MONO_TYPE_BOOLEAN:
69 g_string_append (res, "bool"); break;
71 g_string_append (res, "byte"); break;
73 g_string_append (res, "sbyte"); break;
75 g_string_append (res, "uint16"); break;
77 g_string_append (res, "int16"); break;
79 g_string_append (res, "uint"); break;
81 g_string_append (res, "int"); break;
83 g_string_append (res, "ulong"); break;
85 g_string_append (res, "long"); break;
86 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
87 g_string_append (res, "*()"); break;
89 g_string_append (res, "uintptr"); break;
91 g_string_append (res, "intptr"); break;
93 g_string_append (res, "single"); break;
95 g_string_append (res, "double"); break;
96 case MONO_TYPE_STRING:
97 g_string_append (res, "string"); break;
98 case MONO_TYPE_OBJECT:
99 g_string_append (res, "object"); break;
101 mono_type_get_desc (res, type->data.type, include_namespace);
102 g_string_append_c (res, '*');
104 case MONO_TYPE_ARRAY:
105 append_class_name (res, type->data.array->eklass, include_namespace);
106 g_string_sprintfa (res, "[%d]", type->data.array->rank);
108 case MONO_TYPE_SZARRAY:
109 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
110 g_string_append (res, "[]");
112 case MONO_TYPE_CLASS:
113 case MONO_TYPE_VALUETYPE:
114 append_class_name (res, type->data.klass, include_namespace);
116 case MONO_TYPE_GENERICINST:
117 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
123 g_string_append_c (res, '&');
127 mono_type_full_name (MonoType *type)
132 str = g_string_new ("");
133 mono_type_get_desc (str, type, TRUE);
135 res = g_strdup (str->str);
136 g_string_free (str, TRUE);
141 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
145 GString *res = g_string_new ("");
147 for (i = 0; i < sig->param_count; ++i) {
149 g_string_append_c (res, ',');
150 mono_type_get_desc (res, sig->params [i], include_namespace);
153 g_string_free (res, FALSE);
158 * mono_method_desc_new:
159 * @name: the method name.
160 * @include_namespace: whether the name includes a namespace or not.
162 * Creates a method description for @name, which conforms to the following
165 * [namespace.]classname:methodname[(args...)]
167 * in all the loaded assemblies.
169 * Returns: a parsed representation of the method description.
172 mono_method_desc_new (const char *name, gboolean include_namespace)
174 MonoMethodDesc *result;
175 char *class_name, *class_nspace, *method_name, *use_args, *end;
178 class_nspace = g_strdup (name);
179 use_args = strchr (class_nspace, '(');
182 end = strchr (use_args, ')');
184 g_free (class_nspace);
189 method_name = strrchr (class_nspace, ':');
191 g_free (class_nspace);
195 /* allow two :: to separate the method name */
196 if (*method_name == ':')
198 class_name = strrchr (class_nspace, '.');
203 class_name = class_nspace;
206 result = g_new0 (MonoMethodDesc, 1);
207 result->include_namespace = include_namespace;
208 result->name = method_name;
209 result->klass = class_name;
210 result->namespace = use_namespace? class_nspace: NULL;
211 result->args = use_args? use_args: NULL;
215 result->num_args = 1;
227 mono_method_desc_from_method (MonoMethod *method)
229 MonoMethodDesc *result;
231 result = g_new0 (MonoMethodDesc, 1);
232 result->include_namespace = TRUE;
233 result->name = g_strdup (method->name);
234 result->klass = g_strdup (method->klass->name);
235 result->namespace = g_strdup (method->klass->name_space);
241 * mono_method_desc_free:
242 * @desc: method description to be released
244 * Releases the MonoMethodDesc object @desc.
247 mono_method_desc_free (MonoMethodDesc *desc)
250 g_free (desc->namespace);
251 else if (desc->klass)
252 g_free (desc->klass);
257 * namespace and class are supposed to match already if this function is used.
260 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
263 if (strcmp (desc->name, method->name))
267 if (desc->num_args != mono_method_signature (method)->param_count)
269 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
270 if (strcmp (sig, desc->args)) {
279 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
281 if (strcmp (desc->klass, method->klass->name))
283 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
285 return mono_method_desc_match (desc, method);
289 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
292 gpointer iter = NULL;
294 while ((m = mono_class_get_methods (klass, &iter)))
295 if (mono_method_desc_match (desc, m))
301 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
304 const MonoTableInfo *tdef;
305 const MonoTableInfo *methods;
309 if (desc->namespace && desc->klass) {
310 klass = mono_class_from_name (image, desc->namespace, desc->klass);
313 return mono_method_desc_search_in_class (desc, klass);
316 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
317 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
318 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
319 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
320 const char *n = mono_metadata_string_heap (image, token);
322 if (strcmp (n, desc->name))
324 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
325 if (mono_method_desc_full_match (desc, method))
331 static const unsigned char*
332 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
334 MonoMethodHeader *header = mono_method_get_header (method);
335 const MonoOpcode *opcode;
336 guint32 i, label, token;
340 label = ip - header->code;
342 tmp = dh->indenter (dh, method, label);
343 g_string_append (str, tmp);
346 if (dh->label_format)
347 g_string_sprintfa (str, dh->label_format, label);
349 i = mono_opcode_value (&ip, end);
351 opcode = &mono_opcodes [i];
352 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
354 switch (opcode->argument) {
358 case MonoInlineField:
359 case MonoInlineMethod:
364 tmp = dh->tokener (dh, method, token);
365 g_string_append (str, tmp);
368 g_string_sprintfa (str, "0x%08x", token);
372 case MonoInlineString:
377 g_string_sprintfa (str, "%d", read16 (ip));
380 case MonoShortInlineVar:
381 g_string_sprintfa (str, "%d", (*ip));
384 case MonoInlineBrTarget:
387 if (dh->label_target)
388 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
390 g_string_sprintfa (str, "%d", sval);
392 case MonoShortInlineBrTarget:
393 sval = *(const signed char*)ip;
395 if (dh->label_target)
396 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
398 g_string_sprintfa (str, "%d", sval);
400 case MonoInlineSwitch: {
401 const unsigned char *end;
405 g_string_append_c (str, '(');
406 for (i = 0; i < sval; ++i) {
408 g_string_append (str, ", ");
410 if (dh->label_target)
411 g_string_sprintfa (str, dh->label_target, end + label - header->code);
413 g_string_sprintfa (str, "%d", label);
416 g_string_append_c (str, ')');
422 g_string_sprintfa (str, "%g", r);
426 case MonoShortInlineR: {
429 g_string_sprintfa (str, "%g", r);
434 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
437 case MonoShortInlineI:
438 g_string_sprintfa (str, "%d", *(const signed char*)ip);
445 g_assert_not_reached ();
448 g_string_append (str, dh->newline);
456 "IL_%04x: ", /* label_format */
457 "IL_%04x", /* label_target */
464 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
467 GString *res = g_string_new ("");
471 /* set ip + 2 as the end: this is just a debugging method */
472 ip = dis_one (res, dh, method, ip, ip + 2);
477 g_string_free (res, FALSE);
482 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
485 GString *res = g_string_new ("");
490 ip = dis_one (res, dh, method, ip, end);
494 g_string_free (res, FALSE);
499 wrapper_type_to_str (guint32 wrapper_type)
501 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
503 return wrapper_type_names [wrapper_type];
507 mono_method_full_name (MonoMethod *method, gboolean signature)
511 const char *nspace = method->klass->name_space;
514 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
516 if (method->wrapper_type != MONO_WRAPPER_NONE)
517 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
519 strcpy (wrapper, "");
520 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
521 nspace, *nspace ? "." : "",
522 method->klass->name, method->name, tmpsig);
526 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
527 nspace, *nspace ? "." : "",
528 method->klass->name, method->name);