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",
40 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
43 g_string_append (res, "Unknown");
46 if (class->nested_in) {
47 append_class_name (res, class->nested_in, include_namespace);
48 g_string_append_c (res, '/');
50 if (include_namespace && *(class->name_space))
51 g_string_sprintfa (res, "%s.", class->name_space);
52 g_string_sprintfa (res, "%s", class->name);
56 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
59 g_string_append (res, "void"); break;
61 g_string_append (res, "char"); break;
62 case MONO_TYPE_BOOLEAN:
63 g_string_append (res, "bool"); break;
65 g_string_append (res, "byte"); break;
67 g_string_append (res, "sbyte"); break;
69 g_string_append (res, "uint16"); break;
71 g_string_append (res, "int16"); break;
73 g_string_append (res, "uint"); break;
75 g_string_append (res, "int"); break;
77 g_string_append (res, "ulong"); break;
79 g_string_append (res, "long"); break;
80 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
81 g_string_append (res, "*()"); break;
83 g_string_append (res, "uintptr"); break;
85 g_string_append (res, "intptr"); break;
87 g_string_append (res, "single"); break;
89 g_string_append (res, "double"); break;
90 case MONO_TYPE_STRING:
91 g_string_append (res, "string"); break;
92 case MONO_TYPE_OBJECT:
93 g_string_append (res, "object"); break;
95 mono_type_get_desc (res, type->data.type, include_namespace);
96 g_string_append_c (res, '*');
99 append_class_name (res, type->data.array->eklass, include_namespace);
100 g_string_sprintfa (res, "[%d]", type->data.array->rank);
102 case MONO_TYPE_SZARRAY:
103 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
104 g_string_append (res, "[]");
106 case MONO_TYPE_CLASS:
107 case MONO_TYPE_VALUETYPE:
108 append_class_name (res, type->data.klass, include_namespace);
114 g_string_append_c (res, '&');
118 mono_type_full_name (MonoType *type)
123 str = g_string_new ("");
124 mono_type_get_desc (str, type, TRUE);
126 res = g_strdup (str->str);
127 g_string_free (str, TRUE);
132 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
136 GString *res = g_string_new ("");
138 for (i = 0; i < sig->param_count; ++i) {
140 g_string_append_c (res, ',');
141 mono_type_get_desc (res, sig->params [i], include_namespace);
144 g_string_free (res, FALSE);
149 * mono_method_desc_new:
151 * Creates a method description for `name', which conforms to the following
154 * [namespace.]classname:methodname[(args...)]
156 * in all the loaded assemblies.
158 * Returns a parsed representation of the method description.
161 mono_method_desc_new (const char *name, gboolean include_namespace)
163 MonoMethodDesc *result;
164 char *class_name, *class_nspace, *method_name, *use_args, *end;
167 class_nspace = g_strdup (name);
168 use_args = strchr (class_nspace, '(');
171 end = strchr (use_args, ')');
173 g_free (class_nspace);
178 method_name = strrchr (class_nspace, ':');
180 g_free (class_nspace);
184 /* allow two :: to separate the method name */
185 if (*method_name == ':')
187 class_name = strrchr (class_nspace, '.');
192 class_name = class_nspace;
195 result = g_new0 (MonoMethodDesc, 1);
196 result->include_namespace = include_namespace;
197 result->name = method_name;
198 result->klass = class_name;
199 result->namespace = use_namespace? class_nspace: NULL;
200 result->args = use_args? use_args: NULL;
204 result->num_args = 1;
216 mono_method_desc_from_method (MonoMethod *method)
218 MonoMethodDesc *result;
220 result = g_new0 (MonoMethodDesc, 1);
221 result->include_namespace = TRUE;
222 result->name = g_strdup (method->name);
223 result->klass = g_strdup (method->klass->name);
224 result->namespace = g_strdup (method->klass->name_space);
230 * mono_method_desc_free:
232 * Releases the MonoMethodDesc object `desc'.
235 mono_method_desc_free (MonoMethodDesc *desc)
238 g_free (desc->namespace);
239 else if (desc->klass)
240 g_free (desc->klass);
245 * namespace and class are supposed to match already if this function is used.
248 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
251 if (strcmp (desc->name, method->name))
255 if (desc->num_args != method->signature->param_count)
257 sig = mono_signature_get_desc (method->signature, desc->include_namespace);
258 if (strcmp (sig, desc->args)) {
267 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
269 if (strcmp (desc->klass, method->klass->name))
271 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
273 return mono_method_desc_match (desc, method);
277 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
281 mono_class_init (klass);
282 for (i = 0; i < klass->method.count; ++i) {
283 if (mono_method_desc_match (desc, klass->methods [i]))
284 return klass->methods [i];
290 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
294 MonoTableInfo *methods;
298 if (desc->namespace && desc->klass) {
299 klass = mono_class_from_name (image, desc->namespace, desc->klass);
302 return mono_method_desc_search_in_class (desc, klass);
305 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
306 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
307 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
308 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
309 const char *n = mono_metadata_string_heap (image, token);
311 if (strcmp (n, desc->name))
313 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
314 if (mono_method_desc_full_match (desc, method))
320 static const unsigned char*
321 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
323 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
324 const MonoOpcode *opcode;
325 guint32 i, label, token;
329 label = ip - header->code;
331 tmp = dh->indenter (dh, method, label);
332 g_string_append (str, tmp);
335 if (dh->label_format)
336 g_string_sprintfa (str, dh->label_format, label);
338 i = mono_opcode_value (&ip, end);
340 opcode = &mono_opcodes [i];
341 g_string_sprintfa (str, "%-10s", mono_opcode_names [i]);
343 switch (opcode->argument) {
347 case MonoInlineField:
348 case MonoInlineMethod:
353 tmp = dh->tokener (dh, method, token);
354 g_string_append (str, tmp);
357 g_string_sprintfa (str, "0x%08x", token);
361 case MonoInlineString:
366 g_string_sprintfa (str, "%d", read16 (ip));
369 case MonoShortInlineVar:
370 g_string_sprintfa (str, "%d", (*ip));
373 case MonoInlineBrTarget:
376 if (dh->label_target)
377 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
379 g_string_sprintfa (str, "%d", sval);
381 case MonoShortInlineBrTarget:
382 sval = *(const signed char*)ip;
384 if (dh->label_target)
385 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
387 g_string_sprintfa (str, "%d", sval);
389 case MonoInlineSwitch: {
390 const unsigned char *end;
394 g_string_append_c (str, '(');
395 for (i = 0; i < sval; ++i) {
397 g_string_append (str, ", ");
399 if (dh->label_target)
400 g_string_sprintfa (str, dh->label_target, end + label - header->code);
402 g_string_sprintfa (str, "%d", label);
405 g_string_append_c (str, ')');
411 g_string_sprintfa (str, "%g", r);
415 case MonoShortInlineR: {
418 g_string_sprintfa (str, "%g", r);
423 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
426 case MonoShortInlineI:
427 g_string_sprintfa (str, "%d", *(const signed char*)ip);
434 g_assert_not_reached ();
437 g_string_append (str, dh->newline);
445 "IL_%04x: ", /* label_format */
446 "IL_%04x", /* label_target */
453 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
456 GString *res = g_string_new ("");
460 /* set ip + 2 as the end: this is just a debugging method */
461 ip = dis_one (res, dh, method, ip, ip + 2);
466 g_string_free (res, FALSE);
471 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
474 GString *res = g_string_new ("");
479 ip = dis_one (res, dh, method, ip, end);
483 g_string_free (res, FALSE);
488 wrapper_type_to_str (guint32 wrapper_type)
490 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
492 return wrapper_type_names [wrapper_type];
496 mono_method_full_name (MonoMethod *method, gboolean signature)
502 nspace = method->klass->name_space;
505 char *tmpsig = mono_signature_get_desc (method->signature, TRUE);
507 if (method->wrapper_type != MONO_WRAPPER_NONE)
508 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
510 strcpy (wrapper, "");
511 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
512 nspace, *nspace ? "." : "",
513 method->klass->name, method->name, tmpsig);
517 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
518 nspace, *nspace ? "." : "",
519 method->klass->name, method->name);