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",
43 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
46 g_string_append (res, "Unknown");
49 if (class->nested_in) {
50 append_class_name (res, class->nested_in, include_namespace);
51 g_string_append_c (res, '/');
53 if (include_namespace && *(class->name_space))
54 g_string_sprintfa (res, "%s.", class->name_space);
55 g_string_sprintfa (res, "%s", class->name);
59 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
62 g_string_append (res, "void"); break;
64 g_string_append (res, "char"); break;
65 case MONO_TYPE_BOOLEAN:
66 g_string_append (res, "bool"); break;
68 g_string_append (res, "byte"); break;
70 g_string_append (res, "sbyte"); break;
72 g_string_append (res, "uint16"); break;
74 g_string_append (res, "int16"); break;
76 g_string_append (res, "uint"); break;
78 g_string_append (res, "int"); break;
80 g_string_append (res, "ulong"); break;
82 g_string_append (res, "long"); break;
83 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
84 g_string_append (res, "*()"); break;
86 g_string_append (res, "uintptr"); break;
88 g_string_append (res, "intptr"); break;
90 g_string_append (res, "single"); break;
92 g_string_append (res, "double"); break;
93 case MONO_TYPE_STRING:
94 g_string_append (res, "string"); break;
95 case MONO_TYPE_OBJECT:
96 g_string_append (res, "object"); break;
98 mono_type_get_desc (res, type->data.type, include_namespace);
99 g_string_append_c (res, '*');
101 case MONO_TYPE_ARRAY:
102 append_class_name (res, type->data.array->eklass, include_namespace);
103 g_string_sprintfa (res, "[%d]", type->data.array->rank);
105 case MONO_TYPE_SZARRAY:
106 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
107 g_string_append (res, "[]");
109 case MONO_TYPE_CLASS:
110 case MONO_TYPE_VALUETYPE:
111 append_class_name (res, type->data.klass, include_namespace);
113 case MONO_TYPE_GENERICINST:
114 mono_type_get_desc (res, &type->data.generic_class->container_class->byval_arg, include_namespace);
120 g_string_append_c (res, '&');
124 mono_type_full_name (MonoType *type)
129 str = g_string_new ("");
130 mono_type_get_desc (str, type, TRUE);
132 res = g_strdup (str->str);
133 g_string_free (str, TRUE);
138 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
142 GString *res = g_string_new ("");
144 for (i = 0; i < sig->param_count; ++i) {
146 g_string_append_c (res, ',');
147 mono_type_get_desc (res, sig->params [i], include_namespace);
150 g_string_free (res, FALSE);
155 * mono_method_desc_new:
156 * @name: the method name.
157 * @include_namespace: whether the name includes a namespace or not.
159 * Creates a method description for @name, which conforms to the following
162 * [namespace.]classname:methodname[(args...)]
164 * in all the loaded assemblies.
166 * Returns: a parsed representation of the method description.
169 mono_method_desc_new (const char *name, gboolean include_namespace)
171 MonoMethodDesc *result;
172 char *class_name, *class_nspace, *method_name, *use_args, *end;
175 class_nspace = g_strdup (name);
176 use_args = strchr (class_nspace, '(');
179 end = strchr (use_args, ')');
181 g_free (class_nspace);
186 method_name = strrchr (class_nspace, ':');
188 g_free (class_nspace);
192 /* allow two :: to separate the method name */
193 if (*method_name == ':')
195 class_name = strrchr (class_nspace, '.');
200 class_name = class_nspace;
203 result = g_new0 (MonoMethodDesc, 1);
204 result->include_namespace = include_namespace;
205 result->name = method_name;
206 result->klass = class_name;
207 result->namespace = use_namespace? class_nspace: NULL;
208 result->args = use_args? use_args: NULL;
212 result->num_args = 1;
224 mono_method_desc_from_method (MonoMethod *method)
226 MonoMethodDesc *result;
228 result = g_new0 (MonoMethodDesc, 1);
229 result->include_namespace = TRUE;
230 result->name = g_strdup (method->name);
231 result->klass = g_strdup (method->klass->name);
232 result->namespace = g_strdup (method->klass->name_space);
238 * mono_method_desc_free:
239 * @desc: method description to be released
241 * Releases the MonoMethodDesc object @desc.
244 mono_method_desc_free (MonoMethodDesc *desc)
247 g_free (desc->namespace);
248 else if (desc->klass)
249 g_free (desc->klass);
254 * namespace and class are supposed to match already if this function is used.
257 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
260 if (strcmp (desc->name, method->name))
264 if (desc->num_args != method->signature->param_count)
266 sig = mono_signature_get_desc (method->signature, desc->include_namespace);
267 if (strcmp (sig, desc->args)) {
276 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
278 if (strcmp (desc->klass, method->klass->name))
280 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
282 return mono_method_desc_match (desc, method);
286 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
290 mono_class_init (klass);
291 for (i = 0; i < klass->method.count; ++i) {
292 if (mono_method_desc_match (desc, klass->methods [i]))
293 return klass->methods [i];
299 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
302 const MonoTableInfo *tdef;
303 const MonoTableInfo *methods;
307 if (desc->namespace && desc->klass) {
308 klass = mono_class_from_name (image, desc->namespace, desc->klass);
311 return mono_method_desc_search_in_class (desc, klass);
314 tdef = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
315 methods = mono_image_get_table_info (image, MONO_TABLE_METHOD);
316 for (i = 0; i < mono_table_info_get_rows (methods); ++i) {
317 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
318 const char *n = mono_metadata_string_heap (image, token);
320 if (strcmp (n, desc->name))
322 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
323 if (mono_method_desc_full_match (desc, method))
329 static const unsigned char*
330 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip, const unsigned char *end)
332 MonoMethodHeader *header = mono_method_get_header (method);
333 const MonoOpcode *opcode;
334 guint32 i, label, token;
338 label = ip - header->code;
340 tmp = dh->indenter (dh, method, label);
341 g_string_append (str, tmp);
344 if (dh->label_format)
345 g_string_sprintfa (str, dh->label_format, label);
347 i = mono_opcode_value (&ip, end);
349 opcode = &mono_opcodes [i];
350 g_string_sprintfa (str, "%-10s", mono_opcode_name (i));
352 switch (opcode->argument) {
356 case MonoInlineField:
357 case MonoInlineMethod:
362 tmp = dh->tokener (dh, method, token);
363 g_string_append (str, tmp);
366 g_string_sprintfa (str, "0x%08x", token);
370 case MonoInlineString:
375 g_string_sprintfa (str, "%d", read16 (ip));
378 case MonoShortInlineVar:
379 g_string_sprintfa (str, "%d", (*ip));
382 case MonoInlineBrTarget:
385 if (dh->label_target)
386 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
388 g_string_sprintfa (str, "%d", sval);
390 case MonoShortInlineBrTarget:
391 sval = *(const signed char*)ip;
393 if (dh->label_target)
394 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
396 g_string_sprintfa (str, "%d", sval);
398 case MonoInlineSwitch: {
399 const unsigned char *end;
403 g_string_append_c (str, '(');
404 for (i = 0; i < sval; ++i) {
406 g_string_append (str, ", ");
408 if (dh->label_target)
409 g_string_sprintfa (str, dh->label_target, end + label - header->code);
411 g_string_sprintfa (str, "%d", label);
414 g_string_append_c (str, ')');
420 g_string_sprintfa (str, "%g", r);
424 case MonoShortInlineR: {
427 g_string_sprintfa (str, "%g", r);
432 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
435 case MonoShortInlineI:
436 g_string_sprintfa (str, "%d", *(const signed char*)ip);
443 g_assert_not_reached ();
446 g_string_append (str, dh->newline);
454 "IL_%04x: ", /* label_format */
455 "IL_%04x", /* label_target */
462 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
465 GString *res = g_string_new ("");
469 /* set ip + 2 as the end: this is just a debugging method */
470 ip = dis_one (res, dh, method, ip, ip + 2);
475 g_string_free (res, FALSE);
480 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
483 GString *res = g_string_new ("");
488 ip = dis_one (res, dh, method, ip, end);
492 g_string_free (res, FALSE);
497 wrapper_type_to_str (guint32 wrapper_type)
499 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
501 return wrapper_type_names [wrapper_type];
505 mono_method_full_name (MonoMethod *method, gboolean signature)
509 const char *nspace = method->klass->name_space;
512 char *tmpsig = mono_signature_get_desc (method->signature, TRUE);
514 if (method->wrapper_type != MONO_WRAPPER_NONE)
515 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
517 strcpy (wrapper, "");
518 res = g_strdup_printf ("%s%s%s%s:%s (%s)", wrapper,
519 nspace, *nspace ? "." : "",
520 method->klass->name, method->name, tmpsig);
524 res = g_strdup_printf ("%02d %s%s%s:%s", method->wrapper_type,
525 nspace, *nspace ? "." : "",
526 method->klass->name, method->name);
533 mono_find_method_by_name (MonoClass *klass, const char *name, int param_count)
535 MonoMethod *res = NULL;
538 mono_class_init (klass);
540 for (i = 0; i < klass->method.count; ++i) {
541 if (klass->methods [i]->name[0] == name [0] &&
542 !strcmp (name, klass->methods [i]->name) &&
543 klass->methods [i]->signature->param_count == param_count) {
544 res = klass->methods [i];