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",
28 "remoting-invoke-with-check",
50 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
53 g_string_append (res, "Unknown");
56 if (class->nested_in) {
57 append_class_name (res, class->nested_in, include_namespace);
58 g_string_append_c (res, '/');
60 if (include_namespace && *(class->name_space))
61 g_string_sprintfa (res, "%s.", class->name_space);
62 g_string_sprintfa (res, "%s", class->name);
66 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
69 g_string_append (res, "void"); break;
71 g_string_append (res, "char"); break;
72 case MONO_TYPE_BOOLEAN:
73 g_string_append (res, "bool"); break;
75 g_string_append (res, "byte"); break;
77 g_string_append (res, "sbyte"); break;
79 g_string_append (res, "uint16"); break;
81 g_string_append (res, "int16"); break;
83 g_string_append (res, "uint"); break;
85 g_string_append (res, "int"); break;
87 g_string_append (res, "ulong"); break;
89 g_string_append (res, "long"); break;
90 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
91 g_string_append (res, "*()"); break;
93 g_string_append (res, "uintptr"); break;
95 g_string_append (res, "intptr"); break;
97 g_string_append (res, "single"); break;
99 g_string_append (res, "double"); break;
100 case MONO_TYPE_STRING:
101 g_string_append (res, "string"); break;
102 case MONO_TYPE_OBJECT:
103 g_string_append (res, "object"); break;
105 mono_type_get_desc (res, type->data.type, include_namespace);
106 g_string_append_c (res, '*');
108 case MONO_TYPE_ARRAY:
109 append_class_name (res, type->data.array->eklass, include_namespace);
110 g_string_sprintfa (res, "[%d]", type->data.array->rank);
112 case MONO_TYPE_SZARRAY:
113 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
114 g_string_append (res, "[]");
116 case MONO_TYPE_CLASS:
117 case MONO_TYPE_VALUETYPE:
118 append_class_name (res, type->data.klass, include_namespace);
120 case MONO_TYPE_GENERICINST:
121 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
125 g_string_append (res, type->data.generic_param->name);
131 g_string_append_c (res, '&');
135 mono_type_full_name (MonoType *type)
140 str = g_string_new ("");
141 mono_type_get_desc (str, type, TRUE);
143 res = g_strdup (str->str);
144 g_string_free (str, TRUE);
149 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
153 GString *res = g_string_new ("");
155 for (i = 0; i < sig->param_count; ++i) {
157 g_string_append_c (res, ',');
158 mono_type_get_desc (res, sig->params [i], include_namespace);
161 g_string_free (res, FALSE);
166 * mono_method_desc_new:
167 * @name: the method name.
168 * @include_namespace: whether the name includes a namespace or not.
170 * Creates a method description for @name, which conforms to the following
173 * [namespace.]classname:methodname[(args...)]
175 * in all the loaded assemblies.
177 * Returns: a parsed representation of the method description.
180 mono_method_desc_new (const char *name, gboolean include_namespace)
182 MonoMethodDesc *result;
183 char *class_name, *class_nspace, *method_name, *use_args, *end;
186 class_nspace = g_strdup (name);
187 use_args = strchr (class_nspace, '(');
190 end = strchr (use_args, ')');
192 g_free (class_nspace);
197 method_name = strrchr (class_nspace, ':');
199 g_free (class_nspace);
203 /* allow two :: to separate the method name */
204 if (*method_name == ':')
206 class_name = strrchr (class_nspace, '.');
211 class_name = class_nspace;
214 result = g_new0 (MonoMethodDesc, 1);
215 result->include_namespace = include_namespace;
216 result->name = method_name;
217 result->klass = class_name;
218 result->namespace = use_namespace? class_nspace: NULL;
219 result->args = use_args? use_args: NULL;
223 result->num_args = 1;
235 mono_method_desc_from_method (MonoMethod *method)
237 MonoMethodDesc *result;
239 result = g_new0 (MonoMethodDesc, 1);
240 result->include_namespace = TRUE;
241 result->name = g_strdup (method->name);
242 result->klass = g_strdup (method->klass->name);
243 result->namespace = g_strdup (method->klass->name_space);
249 * mono_method_desc_free:
250 * @desc: method description to be released
252 * Releases the MonoMethodDesc object @desc.
255 mono_method_desc_free (MonoMethodDesc *desc)
258 g_free (desc->namespace);
259 else if (desc->klass)
260 g_free (desc->klass);
265 * namespace and class are supposed to match already if this function is used.
268 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
271 if (strcmp (desc->name, method->name))
275 if (desc->num_args != mono_method_signature (method)->param_count)
277 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
278 if (strcmp (sig, desc->args)) {
287 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
289 if (strcmp (desc->klass, method->klass->name))
291 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
293 return mono_method_desc_match (desc, method);
297 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
300 gpointer iter = NULL;
302 while ((m = mono_class_get_methods (klass, &iter)))
303 if (mono_method_desc_match (desc, m))
309 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
312 const MonoTableInfo *tdef;
313 const MonoTableInfo *methods;
317 if (desc->namespace && desc->klass) {
318 klass = mono_class_from_name (image, desc->namespace, desc->klass);
321 return mono_method_desc_search_in_class (desc, klass);
324 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
325 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
326 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
327 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
328 const char *n = mono_metadata_string_heap (image, token);
330 if (strcmp (n, desc->name))
332 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
333 if (mono_method_desc_full_match (desc, method))
339 static const unsigned char*
340 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
342 MonoMethodHeader *header = mono_method_get_header (method);
343 const MonoOpcode *opcode;
344 guint32 label, token;
348 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
350 label = ip - il_code;
352 tmp = dh->indenter (dh, method, label);
353 g_string_append (str, tmp);
356 if (dh->label_format)
357 g_string_sprintfa (str, dh->label_format, label);
359 i = mono_opcode_value (&ip, end);
361 opcode = &mono_opcodes [i];
362 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
364 switch (opcode->argument) {
368 case MonoInlineField:
369 case MonoInlineMethod:
374 tmp = dh->tokener (dh, method, token);
375 g_string_append (str, tmp);
378 g_string_sprintfa (str, "0x%08x", token);
382 case MonoInlineString:
387 g_string_sprintfa (str, "%d", read16 (ip));
390 case MonoShortInlineVar:
391 g_string_sprintfa (str, "%d", (*ip));
394 case MonoInlineBrTarget:
397 if (dh->label_target)
398 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
400 g_string_sprintfa (str, "%d", sval);
402 case MonoShortInlineBrTarget:
403 sval = *(const signed char*)ip;
405 if (dh->label_target)
406 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
408 g_string_sprintfa (str, "%d", sval);
410 case MonoInlineSwitch: {
411 const unsigned char *end;
415 g_string_append_c (str, '(');
416 for (i = 0; i < sval; ++i) {
418 g_string_append (str, ", ");
420 if (dh->label_target)
421 g_string_sprintfa (str, dh->label_target, end + label - il_code);
423 g_string_sprintfa (str, "%d", label);
426 g_string_append_c (str, ')');
432 g_string_sprintfa (str, "%g", r);
436 case MonoShortInlineR: {
439 g_string_sprintfa (str, "%g", r);
444 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
447 case MonoShortInlineI:
448 g_string_sprintfa (str, "%d", *(const signed char*)ip);
455 g_assert_not_reached ();
458 g_string_append (str, dh->newline);
466 "IL_%04x: ", /* label_format */
467 "IL_%04x", /* label_target */
474 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
477 GString *res = g_string_new ("");
481 /* set ip + 2 as the end: this is just a debugging method */
482 ip = dis_one (res, dh, method, ip, ip + 2);
487 g_string_free (res, FALSE);
492 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
495 GString *res = g_string_new ("");
500 ip = dis_one (res, dh, method, ip, end);
504 g_string_free (res, FALSE);
509 wrapper_type_to_str (guint32 wrapper_type)
511 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
513 return wrapper_type_names [wrapper_type];
517 mono_method_full_name (MonoMethod *method, gboolean signature)
521 const char *nspace = method->klass->name_space;
524 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
526 if (method->wrapper_type != MONO_WRAPPER_NONE)
527 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
529 strcpy (wrapper, "");
530 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
531 nspace, *nspace ? "." : "",
532 method->klass->name, method->name, tmpsig);
536 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
537 nspace, *nspace ? "." : "",
538 method->klass->name, method->name);