#include "mono/metadata/cil-coff.h" /* MonoCLIImageInfo */
#include "mono/metadata/mono-endian.h"
#include "mono/metadata/appdomain.h" /* mono_init */
+#include "mono/metadata/debug-helpers.h"
static FILE *output;
static int include_namespace = 0;
}
-static void
-get_type (GString *res, MonoType *type) {
- switch (type->type) {
- case MONO_TYPE_VOID:
- g_string_append (res, "void"); break;
- case MONO_TYPE_CHAR:
- g_string_append (res, "char"); break;
- case MONO_TYPE_BOOLEAN:
- g_string_append (res, "bool"); break;
- case MONO_TYPE_U1:
- g_string_append (res, "byte"); break;
- case MONO_TYPE_I1:
- g_string_append (res, "sbyte"); break;
- case MONO_TYPE_U2:
- g_string_append (res, "uint16"); break;
- case MONO_TYPE_I2:
- g_string_append (res, "int16"); break;
- case MONO_TYPE_U4:
- g_string_append (res, "int"); break;
- case MONO_TYPE_I4:
- g_string_append (res, "uint"); break;
- case MONO_TYPE_U8:
- g_string_append (res, "ulong"); break;
- case MONO_TYPE_I8:
- g_string_append (res, "long"); break;
- case MONO_TYPE_FNPTR: /* who cares for the exact signature? */
- g_string_append (res, "*()"); break;
- case MONO_TYPE_U:
- g_string_append (res, "intptr"); break;
- case MONO_TYPE_I:
- g_string_append (res, "uintptr"); break;
- case MONO_TYPE_R4:
- g_string_append (res, "single"); break;
- case MONO_TYPE_R8:
- g_string_append (res, "double"); break;
- case MONO_TYPE_STRING:
- g_string_append (res, "string"); break;
- case MONO_TYPE_OBJECT:
- g_string_append (res, "object"); break;
- case MONO_TYPE_PTR:
- get_type (res, type->data.type);
- g_string_append_c (res, '*');
- break;
- case MONO_TYPE_ARRAY:
- get_type (res, type->data.array->type);
- g_string_append (res, "[,]"); /* not the full array info.. */
- break;
- case MONO_TYPE_SZARRAY:
- get_type (res, type->data.type);
- g_string_append (res, "[]");
- break;
- case MONO_TYPE_CLASS:
- case MONO_TYPE_VALUETYPE: {
- MonoClass *class = type->data.klass;
- if (!class) {
- g_string_append (res, "Unknown");
- break;
- }
- if (include_namespace && *(class->name_space))
- g_string_sprintfa (res, "%s.", class->name_space);
- g_string_sprintfa (res, "%s", class->name);
- break;
- }
- default:
- break;
- }
- if (type->byref)
- g_string_append_c (res, '&');
-}
-
static char *
get_signature (MonoMethod *method) {
GString *res;
static GHashTable *hash = NULL;
char *result;
- int i;
if (!hash)
hash = g_hash_table_new (g_direct_hash, g_direct_equal);
res = g_string_new ("");
if (include_namespace && *(method->klass->name_space))
g_string_sprintfa (res, "%s.", method->klass->name_space);
- g_string_sprintfa (res, "%s:%s(", method->klass->name, method->name);
- for (i = 0; i < method->signature->param_count; ++i) {
- if (i > 0)
- g_string_append_c (res, ',');
- get_type (res, method->signature->params [i]);
- }
- g_string_sprintfa (res, ")");
+ result = mono_signature_get_desc (method->signature, include_namespace);
+ g_string_sprintfa (res, "%s:%s(%s)", method->klass->name, method->name, result);
+ g_free (result);
g_hash_table_insert (hash, method, res->str);
result = res->str;
g_error ("Cannot find entry point");
} else {
/* search the method */
- MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
- MonoTableInfo *methods = &image->tables [MONO_TABLE_METHOD];
- char *class_name, *class_nspace, *method_name, *use_args;
- int use_namespace, i;
-
- class_nspace = g_strdup (name);
- use_args = strchr (class_nspace, '(');
- if (use_args)
- *use_args++ = 0;
- method_name = strrchr (class_nspace, ':');
- if (!method_name)
+ MonoMethodDesc *desc;
+
+ desc = mono_method_desc_new (name, include_namespace);
+ if (!desc)
g_error ("Invalid method name %s", name);
- *method_name++ = 0;
- class_name = strrchr (class_nspace, '.');
- if (class_name) {
- *class_name++ = 0;
- use_namespace = 1;
+ method = mono_method_desc_search_in_image (desc, image);
+ if (!method)
+ g_error ("Cannot find method %s", name);
+ }
+ fprintf (output, "digraph blah {\n");
+ fprintf (output, "%s", graph_properties);
+
+ print_method (method, depth);
+
+ fprintf (output, "}\n");
+}
+
+typedef struct MonoBasicBlock MonoBasicBlock;
+
+struct MonoBasicBlock {
+ const unsigned char* cil_code;
+ gint32 cil_length;
+ gint dfn;
+ GList *in_bb;
+ GList *out_bb;
+};
+
+static const unsigned char *debug_start;
+
+static void
+link_bblock (MonoBasicBlock *from, MonoBasicBlock* to)
+{
+ from->out_bb = g_list_prepend (from->out_bb, to);
+ to->in_bb = g_list_prepend (to->in_bb, from);
+ /*fprintf (stderr, "linking IL_%04x to IL_%04x\n", from->cil_code-debug_start, to->cil_code-debug_start);*/
+}
+
+static int
+compare_bblock (void *a, void *b)
+{
+ MonoBasicBlock **ab = a;
+ MonoBasicBlock **bb = b;
+
+ return (*ab)->cil_code - (*bb)->cil_code;
+}
+
+static GPtrArray*
+mono_method_find_bblocks (MonoMethodHeader *header)
+{
+ const unsigned char *ip, *end, *start;
+ const MonoOpcode *opcode;
+ int i, block_end = 0;
+ GPtrArray *result = g_ptr_array_new ();
+ GHashTable *table = g_hash_table_new (g_direct_hash, g_direct_equal);
+ MonoBasicBlock *entry_bb, *end_bb, *bb, *target;
+
+ ip = header->code;
+ end = ip + header->code_size;
+ debug_start = ip;
+
+ entry_bb = g_new0 (MonoBasicBlock, 1);
+ end_bb = g_new0 (MonoBasicBlock, 1);
+ g_ptr_array_add (result, entry_bb);
+ g_ptr_array_add (result, end_bb);
+
+ bb = g_new0 (MonoBasicBlock, 1);
+ bb->cil_code = ip;
+ g_ptr_array_add (result, bb);
+ link_bblock (entry_bb, bb);
+ g_hash_table_insert (table, (char*)ip, bb);
+ block_end = TRUE;
+
+ /* handle exception code blocks... */
+ while (ip < end) {
+ start = ip;
+ if ((target = g_hash_table_lookup (table, ip)) && target != bb) {
+ link_bblock (bb, target);
+ bb = target;
+ block_end = FALSE;
+ }
+ if (block_end) {
+ /*fprintf (stderr, "processing bbclok at IL_%04x\n", ip - header->code);*/
+ if (!(bb = g_hash_table_lookup (table, ip))) {
+ bb = g_new0 (MonoBasicBlock, 1);
+ bb->cil_code = ip;
+ g_ptr_array_add (result, bb);
+ g_hash_table_insert (table, (char*)ip, bb);
+ }
+ block_end = FALSE;
+ }
+ if (*ip == 0xfe) {
+ ++ip;
+ i = *ip + 256;
} else {
- class_name = class_nspace;
- use_namespace = 0;
+ i = *ip;
+ }
+
+ opcode = &mono_opcodes [i];
+ switch (opcode->flow_type) {
+ case MONO_FLOW_RETURN:
+ link_bblock (bb, end_bb);
+ case MONO_FLOW_ERROR:
+ block_end = 1;
+ break;
+ case MONO_FLOW_BRANCH: /* we handle branch when checking the argument type */
+ case MONO_FLOW_COND_BRANCH:
+ case MONO_FLOW_CALL:
+ case MONO_FLOW_NEXT:
+ case MONO_FLOW_META:
+ break;
+ default:
+ g_assert_not_reached ();
}
- for (i = 0; i < methods->rows; ++i) {
- guint32 index = mono_metadata_decode_row_col (methods, i, MONO_METHOD_NAME);
- guint32 idx;
- const char *n = mono_metadata_string_heap (image, index);
-
- if (strcmp (n, method_name))
- continue;
- index = mono_metadata_typedef_from_method (image, i + 1);
- idx = mono_metadata_decode_row_col (tdef, index - 1, MONO_TYPEDEF_NAME);
- n = mono_metadata_string_heap (image, idx);
- if (strcmp (n, class_name))
- continue;
- if (use_namespace) {
- idx = mono_metadata_decode_row_col (tdef, index - 1, MONO_TYPEDEF_NAMESPACE);
- n = mono_metadata_string_heap (image, idx);
- if (strcmp (n, class_nspace))
- continue;
+ switch (opcode->argument) {
+ case MonoInlineNone:
+ ++ip;
+ break;
+ case MonoInlineType:
+ case MonoInlineField:
+ case MonoInlineMethod:
+ case MonoInlineTok:
+ case MonoInlineString:
+ case MonoInlineSig:
+ case MonoShortInlineR:
+ case MonoInlineI:
+ ip += 5;
+ break;
+ case MonoInlineVar:
+ ip += 3;
+ break;
+ case MonoShortInlineVar:
+ case MonoShortInlineI:
+ ip += 2;
+ break;
+ case MonoShortInlineBrTarget:
+ case MonoInlineBrTarget:
+ ip++;
+ if (opcode->argument == MonoShortInlineBrTarget) {
+ i = (signed char)*ip;
+ ip++;
+ } else {
+ i = (gint32) read32 (ip);
+ ip += 4;
}
- method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
- if (use_args) {
- /* check the signature */
- n = get_signature (method);
- if (strcmp (n, name) == 0)
- break;
- if (verbose)
- g_print ("signature check failed: '%s' != '%s'.\n", n, name);
- method = NULL;
+ if (opcode->flow_type == MONO_FLOW_COND_BRANCH) {
+ if (!(target = g_hash_table_lookup (table, ip))) {
+ target = g_new0 (MonoBasicBlock, 1);
+ target->cil_code = ip;
+ g_ptr_array_add (result, target);
+ g_hash_table_insert (table, (char*)ip, target);
+ }
+ link_bblock (bb, target);
+ }
+ if (!(target = g_hash_table_lookup (table, ip + i))) {
+ target = g_new0 (MonoBasicBlock, 1);
+ target->cil_code = ip + i;
+ g_ptr_array_add (result, target);
+ g_hash_table_insert (table, (char*)ip + i, target);
+ }
+ link_bblock (bb, target);
+ block_end = 1;
+ break;
+ case MonoInlineSwitch: {
+ gint32 n;
+ char *itarget, *st;
+ ++ip;
+ n = read32 (ip);
+ ip += 4;
+ st = (char*)ip + 4 * n;
+
+ for (i = 0; i < n; i++) {
+ itarget = st + read32 (ip);
+ ip += 4;
+ if (!(target = g_hash_table_lookup (table, itarget))) {
+ target = g_new0 (MonoBasicBlock, 1);
+ target->cil_code = itarget;
+ g_ptr_array_add (result, target);
+ g_hash_table_insert (table, itarget, target);
+ }
+ link_bblock (bb, target);
}
+ /*
+ * Note: the code didn't set block_end in switch.
+ */
+ break;
+ }
+ case MonoInlineR:
+ case MonoInlineI8:
+ ip += 9;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ }
+ g_hash_table_destroy (table);
+ qsort (result->pdata, result->len, sizeof (gpointer), compare_bblock);
+ /* skip entry and end */
+ bb = target = NULL;
+ for (i = 2; i < result->len; ++i) {
+ bb = (MonoBasicBlock*)g_ptr_array_index (result, i);
+ if (target)
+ target->cil_length = bb->cil_code - target->cil_code;
+ target = bb;
+ /*fprintf (stderr, "bblock %d at IL_%04x:\n", i, bb->cil_code - header->code);*/
+ }
+ bb->cil_length = header->code + header->code_size - bb->cil_code;
+ return result;
+}
+
+static char*
+indenter (MonoDisHelper *dh, MonoMethod *method, guint32 ip_offset)
+{
+ return g_strdup (" ");
+}
+
+static MonoDisHelper graph_dh = {
+ "\\l",
+ NULL,
+ "IL_%04x",
+ indenter,
+ NULL,
+ NULL
+};
+
+static void
+df_visit (MonoBasicBlock *bb, int *dfn, const unsigned char* code)
+{
+ GList *tmp;
+ MonoBasicBlock *next;
+
+ if (bb->dfn)
+ return;
+ ++(*dfn);
+ bb->dfn = *dfn;
+ for (tmp = bb->out_bb; tmp; tmp = tmp->next) {
+ next = tmp->data;
+ if (!next->dfn) {
+ if (!bb->cil_code)
+ fprintf (output, "\t\"DF entry\" -> \"IL_%04x (%d)\"\n", next->cil_code - code, *dfn + 1);
+ else
+ fprintf (output, "\t\"IL_%04x (%d)\" -> \"IL_%04x (%d)\"\n", bb->cil_code - code, bb->dfn, next->cil_code - code, *dfn + 1);
+ df_visit (next, dfn, code);
}
+ }
+}
+
+static void
+print_method_cfg (MonoMethod *method) {
+ GPtrArray *bblocks;
+ GList *tmp;
+ MonoBasicBlock *bb, *target;
+ MonoMethodHeader *header;
+ int i, dfn;
+ char *code;
+
+ header = ((MonoMethodNormal*)method)->header;
+ bblocks = mono_method_find_bblocks (header);
+ for (i = 0; i < bblocks->len; ++i) {
+ bb = (MonoBasicBlock*)g_ptr_array_index (bblocks, i);
+ if (i == 0)
+ fprintf (output, "\tB%p [shape=record,label=\"entry\"]\n", bb);
+ else if (i == 1)
+ fprintf (output, "\tB%p [shape=record,label=\"end\"]\n", bb);
+ else {
+ code = mono_disasm_code (&graph_dh, method, bb->cil_code, bb->cil_code + bb->cil_length);
+ fprintf (output, "\tB%p [shape=record,label=\"IL_%04x\\n%s\"]\n", bb, bb->cil_code - header->code, code);
+ g_free (code);
+ }
+ }
+ for (i = 0; i < bblocks->len; ++i) {
+ bb = (MonoBasicBlock*)g_ptr_array_index (bblocks, i);
+ for (tmp = bb->out_bb; tmp; tmp = tmp->next) {
+ target = tmp->data;
+ fprintf (output, "\tB%p -> B%p\n", bb, target);
+ }
+ }
+#if 0
+ for (i = 0; i < bblocks->len; ++i) {
+ bb = (MonoBasicBlock*)g_ptr_array_index (bblocks, i);
+ bb->dfn = 0;
+ }
+ dfn = 0;
+ for (i = 0; i < bblocks->len; ++i) {
+ bb = (MonoBasicBlock*)g_ptr_array_index (bblocks, i);
+ df_visit (bb, &dfn, header->code);
+ }
+#endif
+}
+
+/*
+ * TODO: change to create the DF tree, dominance relation etc.
+ */
+static void
+method_cfg (MonoImage *image, char *name) {
+ MonoMethod *method = NULL;
+ static char *cfg_graph_properties = "\tnode [fontsize=8.0]\n\tedge [len=1.5,color=red]\n";
+
+ if (!name) {
+ method = mono_get_method (image, ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point, NULL);
+ if (!method)
+ g_error ("Cannot find entry point");
+ } else {
+ /* search the method */
+ MonoMethodDesc *desc;
+
+ desc = mono_method_desc_new (name, include_namespace);
+ if (!desc)
+ g_error ("Invalid method name %s", name);
+ method = mono_method_desc_search_in_image (desc, image);
if (!method)
g_error ("Cannot find method %s", name);
- g_free (class_nspace);
}
fprintf (output, "digraph blah {\n");
- fprintf (output, "%s", graph_properties);
+ fprintf (output, "%s", cfg_graph_properties);
- print_method (method, depth);
+ print_method_cfg (method);
fprintf (output, "}\n");
}
printf ("Usage: monograph [options] [assembly [typename|methodname]]\n");
printf ("Valid options are:\n");
printf ("\t-c|--call output call graph instead of type hierarchy\n");
+ printf ("\t-C|--control-flow output control flow of methodname\n");
printf ("\t-d|--depth num max depth recursion (default: 6)\n");
printf ("\t-o|--output filename write graph to file filename (default: stdout)\n");
printf ("\t-f|--fullname include namespace in type and method names\n");
enum {
GRAPH_TYPES,
GRAPH_CALL,
- GRAPH_INTERFACE
+ GRAPH_INTERFACE,
+ GRAPH_CONTROL_FLOW
};
/*
break;
if (strcmp (argv [i], "--call") == 0 || strcmp (argv [i], "-c") == 0) {
graphtype = GRAPH_CALL;
+ } else if (strcmp (argv [i], "--control-flow") == 0 || strcmp (argv [i], "-C") == 0) {
+ graphtype = GRAPH_CONTROL_FLOW;
} else if (strcmp (argv [i], "--interface") == 0 || strcmp (argv [i], "-i") == 0) {
graphtype = GRAPH_INTERFACE;
} else if (strcmp (argv [i], "--fullname") == 0 || strcmp (argv [i], "-f") == 0) {
cname = argv [i + 1];
if (!aname)
aname = "corlib.dll";
- if (!cname && (graphtype != GRAPH_CALL))
+ if (!cname && (graphtype == GRAPH_TYPES))
cname = "System.Object";
assembly = mono_assembly_open (aname, NULL, NULL);
g_error ("Cannot open file: %s", outputfile);
}
/* if it looks like a method name, we want a callgraph. */
- if (cname && strchr (cname, ':'))
+ if (cname && strchr (cname, ':') && graphtype == GRAPH_TYPES)
graphtype = GRAPH_CALL;
switch (graphtype) {
case GRAPH_INTERFACE:
interface_graph (assembly->image, cname);
break;
+ case GRAPH_CONTROL_FLOW:
+ method_cfg (assembly->image, cname);
+ break;
default:
g_error ("wrong graph type");
}