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",
45 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
48 g_string_append (res, "Unknown");
51 if (class->nested_in) {
52 append_class_name (res, class->nested_in, include_namespace);
53 g_string_append_c (res, '/');
55 if (include_namespace && *(class->name_space))
56 g_string_sprintfa (res, "%s.", class->name_space);
57 g_string_sprintfa (res, "%s", class->name);
61 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
64 g_string_append (res, "void"); break;
66 g_string_append (res, "char"); break;
67 case MONO_TYPE_BOOLEAN:
68 g_string_append (res, "bool"); break;
70 g_string_append (res, "byte"); break;
72 g_string_append (res, "sbyte"); break;
74 g_string_append (res, "uint16"); break;
76 g_string_append (res, "int16"); break;
78 g_string_append (res, "uint"); break;
80 g_string_append (res, "int"); break;
82 g_string_append (res, "ulong"); break;
84 g_string_append (res, "long"); break;
85 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
86 g_string_append (res, "*()"); break;
88 g_string_append (res, "uintptr"); break;
90 g_string_append (res, "intptr"); break;
92 g_string_append (res, "single"); break;
94 g_string_append (res, "double"); break;
95 case MONO_TYPE_STRING:
96 g_string_append (res, "string"); break;
97 case MONO_TYPE_OBJECT:
98 g_string_append (res, "object"); break;
100 mono_type_get_desc (res, type->data.type, include_namespace);
101 g_string_append_c (res, '*');
103 case MONO_TYPE_ARRAY:
104 append_class_name (res, type->data.array->eklass, include_namespace);
105 g_string_sprintfa (res, "[%d]", type->data.array->rank);
107 case MONO_TYPE_SZARRAY:
108 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
109 g_string_append (res, "[]");
111 case MONO_TYPE_CLASS:
112 case MONO_TYPE_VALUETYPE:
113 append_class_name (res, type->data.klass, include_namespace);
115 case MONO_TYPE_GENERICINST:
116 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
122 g_string_append_c (res, '&');
126 mono_type_full_name (MonoType *type)
131 str = g_string_new ("");
132 mono_type_get_desc (str, type, TRUE);
134 res = g_strdup (str->str);
135 g_string_free (str, TRUE);
140 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
144 GString *res = g_string_new ("");
146 for (i = 0; i < sig->param_count; ++i) {
148 g_string_append_c (res, ',');
149 mono_type_get_desc (res, sig->params [i], include_namespace);
152 g_string_free (res, FALSE);
157 * mono_method_desc_new:
158 * @name: the method name.
159 * @include_namespace: whether the name includes a namespace or not.
161 * Creates a method description for @name, which conforms to the following
164 * [namespace.]classname:methodname[(args...)]
166 * in all the loaded assemblies.
168 * Returns: a parsed representation of the method description.
171 mono_method_desc_new (const char *name, gboolean include_namespace)
173 MonoMethodDesc *result;
174 char *class_name, *class_nspace, *method_name, *use_args, *end;
177 class_nspace = g_strdup (name);
178 use_args = strchr (class_nspace, '(');
181 end = strchr (use_args, ')');
183 g_free (class_nspace);
188 method_name = strrchr (class_nspace, ':');
190 g_free (class_nspace);
194 /* allow two :: to separate the method name */
195 if (*method_name == ':')
197 class_name = strrchr (class_nspace, '.');
202 class_name = class_nspace;
205 result = g_new0 (MonoMethodDesc, 1);
206 result->include_namespace = include_namespace;
207 result->name = method_name;
208 result->klass = class_name;
209 result->namespace = use_namespace? class_nspace: NULL;
210 result->args = use_args? use_args: NULL;
214 result->num_args = 1;
226 mono_method_desc_from_method (MonoMethod *method)
228 MonoMethodDesc *result;
230 result = g_new0 (MonoMethodDesc, 1);
231 result->include_namespace = TRUE;
232 result->name = g_strdup (method->name);
233 result->klass = g_strdup (method->klass->name);
234 result->namespace = g_strdup (method->klass->name_space);
240 * mono_method_desc_free:
241 * @desc: method description to be released
243 * Releases the MonoMethodDesc object @desc.
246 mono_method_desc_free (MonoMethodDesc *desc)
249 g_free (desc->namespace);
250 else if (desc->klass)
251 g_free (desc->klass);
256 * namespace and class are supposed to match already if this function is used.
259 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
262 if (strcmp (desc->name, method->name))
266 if (desc->num_args != mono_method_signature (method)->param_count)
268 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
269 if (strcmp (sig, desc->args)) {
278 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
280 if (strcmp (desc->klass, method->klass->name))
282 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
284 return mono_method_desc_match (desc, method);
288 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
291 gpointer iter = NULL;
293 while ((m = mono_class_get_methods (klass, &iter)))
294 if (mono_method_desc_match (desc, m))
300 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
303 const MonoTableInfo *tdef;
304 const MonoTableInfo *methods;
308 if (desc->namespace && desc->klass) {
309 klass = mono_class_from_name (image, desc->namespace, desc->klass);
312 return mono_method_desc_search_in_class (desc, klass);
315 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
316 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
317 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
318 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
319 const char *n = mono_metadata_string_heap (image, token);
321 if (strcmp (n, desc->name))
323 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
324 if (mono_method_desc_full_match (desc, method))
330 static const unsigned char*
331 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
333 MonoMethodHeader *header = mono_method_get_header (method);
334 const MonoOpcode *opcode;
335 guint32 i, label, token;
339 label = ip - header->code;
341 tmp = dh->indenter (dh, method, label);
342 g_string_append (str, tmp);
345 if (dh->label_format)
346 g_string_sprintfa (str, dh->label_format, label);
348 i = mono_opcode_value (&ip, end);
350 opcode = &mono_opcodes [i];
351 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
353 switch (opcode->argument) {
357 case MonoInlineField:
358 case MonoInlineMethod:
363 tmp = dh->tokener (dh, method, token);
364 g_string_append (str, tmp);
367 g_string_sprintfa (str, "0x%08x", token);
371 case MonoInlineString:
376 g_string_sprintfa (str, "%d", read16 (ip));
379 case MonoShortInlineVar:
380 g_string_sprintfa (str, "%d", (*ip));
383 case MonoInlineBrTarget:
386 if (dh->label_target)
387 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
389 g_string_sprintfa (str, "%d", sval);
391 case MonoShortInlineBrTarget:
392 sval = *(const signed char*)ip;
394 if (dh->label_target)
395 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
397 g_string_sprintfa (str, "%d", sval);
399 case MonoInlineSwitch: {
400 const unsigned char *end;
404 g_string_append_c (str, '(');
405 for (i = 0; i < sval; ++i) {
407 g_string_append (str, ", ");
409 if (dh->label_target)
410 g_string_sprintfa (str, dh->label_target, end + label - header->code);
412 g_string_sprintfa (str, "%d", label);
415 g_string_append_c (str, ')');
421 g_string_sprintfa (str, "%g", r);
425 case MonoShortInlineR: {
428 g_string_sprintfa (str, "%g", r);
433 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
436 case MonoShortInlineI:
437 g_string_sprintfa (str, "%d", *(const signed char*)ip);
444 g_assert_not_reached ();
447 g_string_append (str, dh->newline);
455 "IL_%04x: ", /* label_format */
456 "IL_%04x", /* label_target */
463 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
466 GString *res = g_string_new ("");
470 /* set ip + 2 as the end: this is just a debugging method */
471 ip = dis_one (res, dh, method, ip, ip + 2);
476 g_string_free (res, FALSE);
481 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
484 GString *res = g_string_new ("");
489 ip = dis_one (res, dh, method, ip, end);
493 g_string_free (res, FALSE);
498 wrapper_type_to_str (guint32 wrapper_type)
500 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
502 return wrapper_type_names [wrapper_type];
506 mono_method_full_name (MonoMethod *method, gboolean signature)
510 const char *nspace = method->klass->name_space;
513 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
515 if (method->wrapper_type != MONO_WRAPPER_NONE)
516 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
518 strcpy (wrapper, "");
519 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
520 nspace, *nspace ? "." : "",
521 method->klass->name, method->name, tmpsig);
525 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
526 nspace, *nspace ? "." : "",
527 method->klass->name, method->name);