3 #include "mono/metadata/tokentype.h"
4 #include "mono/metadata/opcodes.h"
5 #include "mono/metadata/mono-endian.h"
6 #include "mono/metadata/debug-helpers.h"
8 struct MonoMethodDesc {
14 gboolean include_namespace;
17 static const char *wrapper_type_names [] = {
20 "delegate-begin-invoke",
21 "delegate-end-invoke",
26 "remoting-invoke-with-check",
35 append_class_name (GString *res, MonoClass *class, gboolean include_namespace)
38 g_string_append (res, "Unknown");
41 if (class->nested_in) {
42 append_class_name (res, class->nested_in, include_namespace);
43 g_string_append_c (res, '/');
45 if (include_namespace && *(class->name_space))
46 g_string_sprintfa (res, "%s.", class->name_space);
47 g_string_sprintfa (res, "%s", class->name);
51 mono_type_get_desc (GString *res, MonoType *type, gboolean include_namespace) {
54 g_string_append (res, "void"); break;
56 g_string_append (res, "char"); break;
57 case MONO_TYPE_BOOLEAN:
58 g_string_append (res, "bool"); break;
60 g_string_append (res, "byte"); break;
62 g_string_append (res, "sbyte"); break;
64 g_string_append (res, "uint16"); break;
66 g_string_append (res, "int16"); break;
68 g_string_append (res, "uint"); break;
70 g_string_append (res, "int"); break;
72 g_string_append (res, "ulong"); break;
74 g_string_append (res, "long"); break;
75 case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
76 g_string_append (res, "*()"); break;
78 g_string_append (res, "uintptr"); break;
80 g_string_append (res, "intptr"); break;
82 g_string_append (res, "single"); break;
84 g_string_append (res, "double"); break;
85 case MONO_TYPE_STRING:
86 g_string_append (res, "string"); break;
87 case MONO_TYPE_OBJECT:
88 g_string_append (res, "object"); break;
90 mono_type_get_desc (res, type->data.type, include_namespace);
91 g_string_append_c (res, '*');
94 append_class_name (res, type->data.array->eklass, include_namespace);
95 g_string_sprintfa (res, "[%d]", type->data.array->rank);
97 case MONO_TYPE_SZARRAY:
98 mono_type_get_desc (res, &type->data.klass->byval_arg, include_namespace);
99 g_string_append (res, "[]");
101 case MONO_TYPE_CLASS:
102 case MONO_TYPE_VALUETYPE:
103 append_class_name (res, type->data.klass, include_namespace);
109 g_string_append_c (res, '&');
113 mono_type_full_name (MonoType *type)
118 str = g_string_new ("");
119 mono_type_get_desc (str, type, TRUE);
121 res = g_strdup (str->str);
122 g_string_free (str, TRUE);
127 mono_signature_get_desc (MonoMethodSignature *sig, gboolean include_namespace)
131 GString *res = g_string_new ("");
133 for (i = 0; i < sig->param_count; ++i) {
135 g_string_append_c (res, ',');
136 mono_type_get_desc (res, sig->params [i], include_namespace);
139 g_string_free (res, FALSE);
144 * The allowed format of a method description string is:
145 * [namespace.]classname:methodname[(args...)]
146 * TODO: describe instance methods.
149 mono_method_desc_new (const char *name, gboolean include_namespace)
151 MonoMethodDesc *result;
152 char *class_name, *class_nspace, *method_name, *use_args, *end;
155 class_nspace = g_strdup (name);
156 use_args = strchr (class_nspace, '(');
159 end = strchr (use_args, ')');
161 g_free (class_nspace);
166 method_name = strrchr (class_nspace, ':');
168 g_free (class_nspace);
172 /* allow two :: to separate the method name */
173 if (*method_name == ':')
175 class_name = strrchr (class_nspace, '.');
180 class_name = class_nspace;
183 result = g_new0 (MonoMethodDesc, 1);
184 result->include_namespace = include_namespace;
185 result->name = method_name;
186 result->klass = class_name;
187 result->namespace = use_namespace? class_nspace: NULL;
188 result->args = use_args? use_args: NULL;
192 result->num_args = 1;
204 mono_method_desc_from_method (MonoMethod *method)
206 MonoMethodDesc *result;
208 result = g_new0 (MonoMethodDesc, 1);
209 result->include_namespace = TRUE;
210 result->name = g_strdup (method->name);
211 result->klass = g_strdup (method->klass->name);
212 result->namespace = g_strdup (method->klass->name_space);
218 mono_method_desc_free (MonoMethodDesc *desc)
221 g_free (desc->namespace);
222 else if (desc->klass)
223 g_free (desc->klass);
228 * namespace and class are supposed to match already if this function is used.
231 mono_method_desc_match (MonoMethodDesc *desc, MonoMethod *method)
234 if (strcmp (desc->name, method->name))
238 if (desc->num_args != method->signature->param_count)
240 sig = mono_signature_get_desc (method->signature, desc->include_namespace);
241 if (strcmp (sig, desc->args)) {
250 mono_method_desc_full_match (MonoMethodDesc *desc, MonoMethod *method)
252 if (strcmp (desc->klass, method->klass->name))
254 if (desc->namespace && strcmp (desc->namespace, method->klass->name_space))
256 return mono_method_desc_match (desc, method);
260 mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass)
264 mono_class_init (klass);
265 for (i = 0; i < klass->method.count; ++i) {
266 if (mono_method_desc_match (desc, klass->methods [i]))
267 return klass->methods [i];
273 mono_method_desc_search_in_image (MonoMethodDesc *desc, MonoImage *image)
277 MonoTableInfo *methods;
281 if (desc->namespace && desc->klass) {
282 klass = mono_class_from_name (image, desc->namespace, desc->klass);
285 return mono_method_desc_search_in_class (desc, klass);
288 tdef = &image->tables [MONO_TABLE_TYPEDEF];
289 methods = &image->tables [MONO_TABLE_METHOD];
290 for (i = 0; i < methods->rows; ++i) {
291 guint32 token = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
292 const char *n = mono_metadata_string_heap (image, token);
294 if (strcmp (n, desc->name))
296 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
297 if (mono_method_desc_full_match (desc, method))
303 static const unsigned char*
304 dis_one (GString *str, MonoDisHelper *dh, MonoMethod *method, const unsigned char *ip)
306 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
307 const MonoOpcode *opcode;
308 guint32 i, label, token;
312 label = ip - header->code;
314 tmp = dh->indenter (dh, method, label);
315 g_string_append (str, tmp);
318 if (dh->label_format)
319 g_string_sprintfa (str, dh->label_format, label);
321 i = mono_opcode_value (&ip);
323 opcode = &mono_opcodes [i];
324 g_string_sprintfa (str, "%-10s", mono_opcode_names [i]);
326 switch (opcode->argument) {
330 case MonoInlineField:
331 case MonoInlineMethod:
336 tmp = dh->tokener (dh, method, token);
337 g_string_append (str, tmp);
340 g_string_sprintfa (str, "0x%08x", token);
344 case MonoInlineString:
349 g_string_sprintfa (str, "%d", read16 (ip));
352 case MonoShortInlineVar:
353 g_string_sprintfa (str, "%d", (*ip));
356 case MonoInlineBrTarget:
359 if (dh->label_target)
360 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
362 g_string_sprintfa (str, "%d", sval);
364 case MonoShortInlineBrTarget:
365 sval = *(const signed char*)ip;
367 if (dh->label_target)
368 g_string_sprintfa (str, dh->label_target, ip + sval - header->code);
370 g_string_sprintfa (str, "%d", sval);
372 case MonoInlineSwitch: {
373 const unsigned char *end;
377 g_string_append_c (str, '(');
378 for (i = 0; i < sval; ++i) {
380 g_string_append (str, ", ");
382 if (dh->label_target)
383 g_string_sprintfa (str, dh->label_target, end + label - header->code);
385 g_string_sprintfa (str, "%d", label);
388 g_string_append_c (str, ')');
394 g_string_sprintfa (str, "%g", r);
398 case MonoShortInlineR: {
401 g_string_sprintfa (str, "%g", r);
406 g_string_sprintfa (str, "%d", (gint32)read32 (ip));
409 case MonoShortInlineI:
410 g_string_sprintfa (str, "%d", *(const signed char*)ip);
417 g_assert_not_reached ();
420 g_string_append (str, dh->newline);
428 "IL_%04x: ", /* label_format */
429 "IL_%04x", /* label_target */
436 mono_disasm_code_one (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar **endp)
439 GString *res = g_string_new ("");
443 ip = dis_one (res, dh, method, ip);
448 g_string_free (res, FALSE);
453 mono_disasm_code (MonoDisHelper *dh, MonoMethod *method, const guchar *ip, const guchar* end)
456 GString *res = g_string_new ("");
461 ip = dis_one (res, dh, method, ip);
465 g_string_free (res, FALSE);
470 wrapper_type_to_str (guint32 wrapper_type)
472 g_assert (wrapper_type < sizeof (wrapper_type_names) / sizeof (char*));
474 return wrapper_type_names [wrapper_type];
478 mono_method_full_name (MonoMethod *method, gboolean signature)
484 char *tmpsig = mono_signature_get_desc (method->signature, TRUE);
486 if (method->wrapper_type != MONO_WRAPPER_NONE)
487 sprintf (wrapper, "(wrapper %s) ", wrapper_type_to_str (method->wrapper_type));
489 strcpy (wrapper, "");
490 res = g_strdup_printf ("%s%s.%s:%s (%s)", wrapper, method->klass->name_space,
491 method->klass->name, method->name, tmpsig);
495 res = g_strdup_printf ("%02d %s.%s:%s", method->wrapper_type, method->klass->name_space,
496 method->klass->name, method->name);