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",
49 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
52 g_string_append (res, "Unknown");
55 if (class->nested_in) {
56 append_class_name (res, class->nested_in, include_namespace);
57 g_string_append_c (res, '/');
59 if (include_namespace && *(class->name_space))
60 g_string_sprintfa (res, "%s.", class->name_space);
61 g_string_sprintfa (res, "%s", class->name);
65 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
68 g_string_append (res, "void"); break;
70 g_string_append (res, "char"); break;
71 case MONO_TYPE_BOOLEAN:
72 g_string_append (res, "bool"); break;
74 g_string_append (res, "byte"); break;
76 g_string_append (res, "sbyte"); break;
78 g_string_append (res, "uint16"); break;
80 g_string_append (res, "int16"); break;
82 g_string_append (res, "uint"); break;
84 g_string_append (res, "int"); break;
86 g_string_append (res, "ulong"); break;
88 g_string_append (res, "long"); break;
89 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
90 g_string_append (res, "*()"); break;
92 g_string_append (res, "uintptr"); break;
94 g_string_append (res, "intptr"); break;
96 g_string_append (res, "single"); break;
98 g_string_append (res, "double"); break;
99 case MONO_TYPE_STRING:
100 g_string_append (res, "string"); break;
101 case MONO_TYPE_OBJECT:
102 g_string_append (res, "object"); break;
104 mono_type_get_desc (res, type->data.type, include_namespace);
105 g_string_append_c (res, '*');
107 case MONO_TYPE_ARRAY:
108 append_class_name (res, type->data.array->eklass, include_namespace);
109 g_string_sprintfa (res, "[%d]", type->data.array->rank);
111 case MONO_TYPE_SZARRAY:
112 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
113 g_string_append (res, "[]");
115 case MONO_TYPE_CLASS:
116 case MONO_TYPE_VALUETYPE:
117 append_class_name (res, type->data.klass, include_namespace);
119 case MONO_TYPE_GENERICINST:
120 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
124 g_string_append (res, type->data.generic_param->name);
130 g_string_append_c (res, '&');
134 mono_type_full_name (MonoType *type)
139 str = g_string_new ("");
140 mono_type_get_desc (str, type, TRUE);
142 res = g_strdup (str->str);
143 g_string_free (str, TRUE);
148 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
152 GString *res = g_string_new ("");
154 for (i = 0; i < sig->param_count; ++i) {
156 g_string_append_c (res, ',');
157 mono_type_get_desc (res, sig->params [i], include_namespace);
160 g_string_free (res, FALSE);
165 * mono_method_desc_new:
166 * @name: the method name.
167 * @include_namespace: whether the name includes a namespace or not.
169 * Creates a method description for @name, which conforms to the following
172 * [namespace.]classname:methodname[(args...)]
174 * in all the loaded assemblies.
176 * Returns: a parsed representation of the method description.
179 mono_method_desc_new (const char *name, gboolean include_namespace)
181 MonoMethodDesc *result;
182 char *class_name, *class_nspace, *method_name, *use_args, *end;
185 class_nspace = g_strdup (name);
186 use_args = strchr (class_nspace, '(');
189 end = strchr (use_args, ')');
191 g_free (class_nspace);
196 method_name = strrchr (class_nspace, ':');
198 g_free (class_nspace);
202 /* allow two :: to separate the method name */
203 if (*method_name == ':')
205 class_name = strrchr (class_nspace, '.');
210 class_name = class_nspace;
213 result = g_new0 (MonoMethodDesc, 1);
214 result->include_namespace = include_namespace;
215 result->name = method_name;
216 result->klass = class_name;
217 result->namespace = use_namespace? class_nspace: NULL;
218 result->args = use_args? use_args: NULL;
222 result->num_args = 1;
234 mono_method_desc_from_method (MonoMethod *method)
236 MonoMethodDesc *result;
238 result = g_new0 (MonoMethodDesc, 1);
239 result->include_namespace = TRUE;
240 result->name = g_strdup (method->name);
241 result->klass = g_strdup (method->klass->name);
242 result->namespace = g_strdup (method->klass->name_space);
248 * mono_method_desc_free:
249 * @desc: method description to be released
251 * Releases the MonoMethodDesc object @desc.
254 mono_method_desc_free (MonoMethodDesc *desc)
257 g_free (desc->namespace);
258 else if (desc->klass)
259 g_free (desc->klass);
264 * namespace and class are supposed to match already if this function is used.
267 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
270 if (strcmp (desc->name, method->name))
274 if (desc->num_args != mono_method_signature (method)->param_count)
276 sig = mono_signature_get_desc (mono_method_signature (method), desc->include_namespace);
277 if (strcmp (sig, desc->args)) {
286 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
288 if (strcmp (desc->klass, method->klass->name))
290 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
292 return mono_method_desc_match (desc, method);
296 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
299 gpointer iter = NULL;
301 while ((m = mono_class_get_methods (klass, &iter)))
302 if (mono_method_desc_match (desc, m))
308 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
311 const MonoTableInfo *tdef;
312 const MonoTableInfo *methods;
316 if (desc->namespace && desc->klass) {
317 klass = mono_class_from_name (image, desc->namespace, desc->klass);
320 return mono_method_desc_search_in_class (desc, klass);
323 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
324 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
325 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
326 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
327 const char *n = mono_metadata_string_heap (image, token);
329 if (strcmp (n, desc->name))
331 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
332 if (mono_method_desc_full_match (desc, method))
338 static const unsigned char*
339 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
341 MonoMethodHeader *header = mono_method_get_header (method);
342 const MonoOpcode *opcode;
343 guint32 label, token;
347 const unsigned char* il_code = mono_method_header_get_code (header, NULL, NULL);
349 label = ip - il_code;
351 tmp = dh->indenter (dh, method, label);
352 g_string_append (str, tmp);
355 if (dh->label_format)
356 g_string_sprintfa (str, dh->label_format, label);
358 i = mono_opcode_value (&ip, end);
360 opcode = &mono_opcodes [i];
361 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
363 switch (opcode->argument) {
367 case MonoInlineField:
368 case MonoInlineMethod:
373 tmp = dh->tokener (dh, method, token);
374 g_string_append (str, tmp);
377 g_string_sprintfa (str, "0x%08x", token);
381 case MonoInlineString:
386 g_string_sprintfa (str, "%d", read16 (ip));
389 case MonoShortInlineVar:
390 g_string_sprintfa (str, "%d", (*ip));
393 case MonoInlineBrTarget:
396 if (dh->label_target)
397 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
399 g_string_sprintfa (str, "%d", sval);
401 case MonoShortInlineBrTarget:
402 sval = *(const signed char*)ip;
404 if (dh->label_target)
405 g_string_sprintfa (str, dh->label_target, ip + sval - il_code);
407 g_string_sprintfa (str, "%d", sval);
409 case MonoInlineSwitch: {
410 const unsigned char *end;
414 g_string_append_c (str, '(');
415 for (i = 0; i < sval; ++i) {
417 g_string_append (str, ", ");
419 if (dh->label_target)
420 g_string_sprintfa (str, dh->label_target, end + label - il_code);
422 g_string_sprintfa (str, "%d", label);
425 g_string_append_c (str, ')');
431 g_string_sprintfa (str, "%g", r);
435 case MonoShortInlineR: {
438 g_string_sprintfa (str, "%g", r);
443 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
446 case MonoShortInlineI:
447 g_string_sprintfa (str, "%d", *(const signed char*)ip);
454 g_assert_not_reached ();
457 g_string_append (str, dh->newline);
465 "IL_%04x: ", /* label_format */
466 "IL_%04x", /* label_target */
473 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
476 GString *res = g_string_new ("");
480 /* set ip + 2 as the end: this is just a debugging method */
481 ip = dis_one (res, dh, method, ip, ip + 2);
486 g_string_free (res, FALSE);
491 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
494 GString *res = g_string_new ("");
499 ip = dis_one (res, dh, method, ip, end);
503 g_string_free (res, FALSE);
508 wrapper_type_to_str (guint32 wrapper_type)
510 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
512 return wrapper_type_names [wrapper_type];
516 mono_method_full_name (MonoMethod *method, gboolean signature)
520 const char *nspace = method->klass->name_space;
523 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
525 if (method->wrapper_type != MONO_WRAPPER_NONE)
526 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
528 strcpy (wrapper, "");
529 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
530 nspace, *nspace ? "." : "",
531 method->klass->name, method->name, tmpsig);
535 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
536 nspace, *nspace ? "." : "",
537 method->klass->name, method->name);