4 #include "mono/metadata/metadata-internals.h"
5 #include "mono/metadata/class-internals.h"
6 #include "mono/metadata/assembly.h"
7 #include "mono/metadata/tokentype.h"
8 #include "mono/metadata/opcodes.h"
9 #include "mono/metadata/tabledefs.h"
10 #include "mono/metadata/mono-endian.h"
11 #include "mono/metadata/appdomain.h" /* mono_init */
12 #include "mono/metadata/debug-helpers.h"
13 #include "mono/utils/mono-compiler.h"
16 static int include_namespace
= 0;
17 static int max_depth
= 6;
18 static int verbose
= 0;
19 static const char *graph_properties
= "\tnode [fontsize=8.0]\n\tedge [len=2,color=red]\n";
22 output_type_edge (MonoClass
*first
, MonoClass
*second
) {
23 if (include_namespace
)
24 fprintf (output
, "\t\"%s.%s\" -> \"%s.%s\"\n", first
->name_space
, first
->name
, second
->name_space
, second
->name
);
26 fprintf (output
, "\t\"%s\" -> \"%s\"\n", first
->name
, second
->name
);
30 print_subtypes (MonoImage
*image
, MonoClass
*class, int depth
) {
32 const MonoTableInfo
*t
;
35 if (depth
++ > max_depth
)
38 t
= mono_image_get_table_info (image
, MONO_TABLE_TYPEDEF
);
40 token
= mono_metadata_token_index (class->type_token
);
41 token
<<= MONO_TYPEDEFORREF_BITS
;
42 token
|= MONO_TYPEDEFORREF_TYPEDEF
;
45 for (i
= 0; i
< mono_table_info_get_rows (t
); ++i
) {
46 if (token
== mono_metadata_decode_row_col (t
, i
, MONO_TYPEDEF_EXTENDS
)) {
47 child
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| (i
+ 1));
48 output_type_edge (class, child
);
49 print_subtypes (image
, child
, depth
);
55 type_graph (MonoImage
*image
, const char* cname
) {
59 const char *name_space
;
63 cname
= g_strdup (cname
);
64 p
= strrchr (cname
, '.');
72 class = mono_class_from_name (image
, name_space
, cname
);
74 g_print ("class %s.%s not found\n", name_space
, cname
);
77 fprintf (output
, "digraph blah {\n");
78 fprintf (output
, "%s", graph_properties
);
80 /* go back and print the parents for the node as well: not sure it's a good idea */
81 for (parent
= class->parent
; parent
; parent
= parent
->parent
) {
82 output_type_edge (parent
, child
);
85 print_subtypes (image
, class, depth
);
86 fprintf (output
, "}\n");
90 interface_graph (MonoImage
*image
, const char* cname
) {
93 const char *name_space
;
95 guint32 cols
[MONO_INTERFACEIMPL_SIZE
];
96 guint32 token
, i
, count
= 0;
97 const MonoTableInfo
*intf
= mono_image_get_table_info (image
, MONO_TABLE_INTERFACEIMPL
);
99 cname
= g_strdup (cname
);
100 p
= strrchr (cname
, '.');
108 class = mono_class_from_name (image
, name_space
, cname
);
110 g_print ("interface %s.%s not found\n", name_space
, cname
);
113 /* chek if it's really an interface... */
114 fprintf (output
, "digraph interface {\n");
115 fprintf (output
, "%s", graph_properties
);
116 /* TODO: handle inetrface defined in one image and class defined in another. */
117 token
= mono_metadata_token_index (class->type_token
);
118 token
<<= MONO_TYPEDEFORREF_BITS
;
119 token
|= MONO_TYPEDEFORREF_TYPEDEF
;
120 for (i
= 0; i
< mono_table_info_get_rows (intf
); ++i
) {
121 mono_metadata_decode_row (intf
, i
, cols
, MONO_INTERFACEIMPL_SIZE
);
122 /*g_print ("index: %d [%d implements %d]\n", index, cols [MONO_INTERFACEIMPL_CLASS], cols [MONO_INTERFACEIMPL_INTERFACE]);*/
123 if (token
== cols
[MONO_INTERFACEIMPL_INTERFACE
]) {
124 child
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| cols
[MONO_INTERFACEIMPL_CLASS
]);
125 output_type_edge (class, child
);
129 fprintf (output
, "}\n");
130 if (verbose
&& !count
)
131 g_print ("No class implements %s.%s\n", class->name_space
, class->name
);
135 static int back_branch_waste
= 0;
136 static int branch_waste
= 0;
137 static int var_waste
= 0;
138 static int int_waste
= 0;
139 static int nop_waste
= 0;
140 static int has_exceptions
= 0;
141 static int num_exceptions
= 0;
142 static int max_exceptions
= 0;
143 static int has_locals
= 0;
144 static int num_locals
= 0;
145 static int max_locals
= 0;
146 static int has_args
= 0;
147 static int num_args
= 0;
148 static int max_args
= 0;
149 static int has_maxstack
= 0;
150 static int num_maxstack
= 0;
151 static int max_maxstack
= 0;
152 static int has_code
= 0;
153 static int num_code
= 0;
154 static int max_code
= 0;
155 static int has_branch
= 0;
156 static int num_branch
= 0;
157 static int max_branch
= 0;
158 static int has_condbranch
= 0;
159 static int num_condbranch
= 0;
160 static int max_condbranch
= 0;
161 static int has_calls
= 0;
162 static int num_calls
= 0;
163 static int max_calls
= 0;
164 static int has_throw
= 0;
165 static int num_throw
= 0;
166 static int max_throw
= 0;
167 static int has_switch
= 0;
168 static int num_switch
= 0;
169 static int max_switch
= 0;
170 static int cast_sealed
= 0;
171 static int cast_iface
= 0;
172 static int total_cast
= 0;
173 static int nonvirt_callvirt
= 0;
174 static int iface_callvirt
= 0;
175 static int total_callvirt
= 0;
178 method_stats (MonoMethod
*method
) {
179 const MonoOpcode
*opcode
;
180 MonoMethodHeader
*header
;
181 MonoMethodSignature
*sig
;
182 const unsigned char *ip
, *il_code_end
;
184 int local_branch
= 0, local_condbranch
= 0, local_throw
= 0, local_calls
= 0;
187 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
))
189 if (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))
192 header
= mono_method_get_header (method
);
193 n
= mono_method_header_get_num_clauses (header
);
197 if (max_exceptions
< n
)
199 mono_method_header_get_locals (header
, &n
, NULL
);
206 ip
= mono_method_header_get_code (header
, &n
, &i
);
207 il_code_end
= ip
+ n
;
208 if (max_maxstack
< i
)
211 if (i
!= 8) /* just a guess */
214 sig
= mono_method_signature (method
);
215 n
= sig
->hasthis
+ sig
->param_count
;
223 if (max_code
< il_code_end
- ip
)
224 max_code
= il_code_end
- ip
;
225 num_code
+= il_code_end
- ip
;
227 while (ip
< il_code_end
) {
235 opcode
= &mono_opcodes
[i
];
237 switch (opcode
->argument
) {
239 if (i
== MONO_CEE_NOP
)
245 if (n
>= -1 && n
<= 8) {
247 g_print ("%s %d\n", mono_opcode_name (i
), n
);
248 } else if (n
< 128 && n
>= -128) {
250 g_print ("%s %d\n", mono_opcode_name (i
), n
);
255 if (i
== MONO_CEE_CASTCLASS
|| i
== MONO_CEE_ISINST
) {
256 guint32 token
= read32 (ip
+ 1);
257 MonoClass
*k
= mono_class_get (method
->klass
->image
, token
);
258 if (k
&& k
->flags
& TYPE_ATTRIBUTE_SEALED
)
260 if (k
&& k
->flags
& TYPE_ATTRIBUTE_INTERFACE
)
266 case MonoInlineField
:
268 case MonoInlineString
:
270 case MonoShortInlineR
:
273 case MonoInlineBrTarget
:
275 if (n
< 128 && n
>= -128) {
278 back_branch_waste
+= 3;
291 /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
295 /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
300 /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
305 case MonoShortInlineVar
:
306 if ((signed char)ip
[1] < 4 && (signed char)ip
[1] >= 0) {
308 case MONO_CEE_LDARG_S
:
309 case MONO_CEE_LDLOC_S
:
310 case MONO_CEE_STLOC_S
:
312 /*g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);*/
320 case MonoShortInlineI
:
321 if ((signed char)ip
[1] <= 8 && (signed char)ip
[1] >= -1) {
322 /*g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);*/
327 case MonoShortInlineBrTarget
:
330 case MonoInlineSwitch
: {
347 /* should load and convert */
348 if (l
>= -1 && l
<= 8) {
350 } else if (l
< 128 && l
>= -128) {
352 } else if (l
<= 2147483647 && l
>= (-2147483647 -1)) {
357 case MonoInlineMethod
:
358 if (i
== MONO_CEE_CALLVIRT
) {
359 MonoMethod
*cm
= mono_get_method (method
->klass
->image
, read32 (ip
+ 1), NULL
);
360 if (cm
&& !(cm
->flags
& METHOD_ATTRIBUTE_VIRTUAL
))
362 if (cm
&& (cm
->klass
->flags
& TYPE_ATTRIBUTE_INTERFACE
))
369 g_assert_not_reached ();
372 switch (opcode
->flow_type
) {
373 case MONO_FLOW_BRANCH
:
376 case MONO_FLOW_COND_BRANCH
:
382 case MONO_FLOW_ERROR
:
390 if (max_branch
< local_branch
)
391 max_branch
= local_branch
;
392 num_branch
+= local_branch
;
394 if (local_condbranch
)
396 if (max_condbranch
< local_condbranch
)
397 max_condbranch
= local_condbranch
;
398 num_condbranch
+= local_condbranch
;
402 if (max_calls
< local_calls
)
403 max_calls
= local_calls
;
404 num_calls
+= local_calls
;
408 if (max_throw
< local_throw
)
409 max_throw
= local_throw
;
410 num_throw
+= local_throw
;
415 static int num_pdepth
= 0;
416 static int max_pdepth
= 0;
417 static int num_pdepth_ovf
= 0;
418 static int num_ifaces
= 0;
419 static int *pdepth_array
= NULL
;
420 static int pdepth_array_size
= 0;
421 static int pdepth_array_next
= 0;
424 type_stats (MonoClass
*klass
) {
428 if (klass
->flags
& TYPE_ATTRIBUTE_INTERFACE
) {
432 parent
= klass
->parent
;
435 parent
= parent
->parent
;
437 if (pdepth_array_next
>= pdepth_array_size
) {
438 pdepth_array_size
*= 2;
439 if (!pdepth_array_size
)
440 pdepth_array_size
= 128;
441 pdepth_array
= g_realloc (pdepth_array
, pdepth_array_size
* sizeof (int));
443 pdepth_array
[pdepth_array_next
++] = depth
;
445 if (max_pdepth
< depth
)
447 if (depth
> MONO_DEFAULT_SUPERTABLE_SIZE
) {
448 /*g_print ("overflow parent depth: %s.%s\n", klass->name_space, klass->name);*/
454 stats (MonoImage
*image
, const char *name
) {
455 int i
, num_methods
, num_types
;
459 num_methods
= mono_image_get_table_rows (image
, MONO_TABLE_METHOD
);
460 for (i
= 0; i
< num_methods
; ++i
) {
461 method
= mono_get_method (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
);
462 method_stats (method
);
464 num_types
= mono_image_get_table_rows (image
, MONO_TABLE_TYPEDEF
);
465 for (i
= 0; i
< num_types
; ++i
) {
466 klass
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| (i
+ 1));
470 g_print ("Methods and code stats:\n");
471 g_print ("back branch waste: %d\n", back_branch_waste
);
472 g_print ("branch waste: %d\n", branch_waste
);
473 g_print ("var waste: %d\n", var_waste
);
474 g_print ("int waste: %d\n", int_waste
);
475 g_print ("nop waste: %d\n", nop_waste
);
476 g_print ("has exceptions: %d/%d, total: %d, max: %d, mean: %f\n", has_exceptions
, num_methods
, num_exceptions
, max_exceptions
, num_exceptions
/(double)has_exceptions
);
477 g_print ("has locals: %d/%d, total: %d, max: %d, mean: %f\n", has_locals
, num_methods
, num_locals
, max_locals
, num_locals
/(double)has_locals
);
478 g_print ("has args: %d/%d, total: %d, max: %d, mean: %f\n", has_args
, num_methods
, num_args
, max_args
, num_args
/(double)has_args
);
479 g_print ("has maxstack: %d/%d, total: %d, max: %d, mean: %f\n", has_maxstack
, num_methods
, num_maxstack
, max_maxstack
, num_maxstack
/(double)i
);
480 g_print ("has code: %d/%d, total: %d, max: %d, mean: %f\n", has_code
, num_methods
, num_code
, max_code
, num_code
/(double)has_code
);
481 g_print ("has branch: %d/%d, total: %d, max: %d, mean: %f\n", has_branch
, num_methods
, num_branch
, max_branch
, num_branch
/(double)has_branch
);
482 g_print ("has condbranch: %d/%d, total: %d, max: %d, mean: %f\n", has_condbranch
, num_methods
, num_condbranch
, max_condbranch
, num_condbranch
/(double)has_condbranch
);
483 g_print ("has switch: %d/%d, total: %d, max: %d, mean: %f\n", has_switch
, num_methods
, num_switch
, max_switch
, num_switch
/(double)has_switch
);
484 g_print ("has calls: %d/%d, total: %d, max: %d, mean: %f\n", has_calls
, num_methods
, num_calls
, max_calls
, num_calls
/(double)has_calls
);
485 g_print ("has throw: %d/%d, total: %d, max: %d, mean: %f\n", has_throw
, num_methods
, num_throw
, max_throw
, num_throw
/(double)has_throw
);
486 g_print ("sealed type cast: %d/%d\n", cast_sealed
, total_cast
);
487 g_print ("interface type cast: %d/%d\n", cast_iface
, total_cast
);
488 g_print ("non virtual callvirt: %d/%d\n", nonvirt_callvirt
, total_callvirt
);
489 g_print ("interface callvirt: %d/%d\n", iface_callvirt
, total_callvirt
);
491 g_print ("\nType stats:\n");
492 g_print ("interface types: %d/%d\n", num_ifaces
, num_types
);
496 if (pdepth_array_next
) {
498 mean
= (double)num_pdepth
/pdepth_array_next
;
499 for (i
= 0; i
< pdepth_array_next
; ++i
) {
500 stddev
+= (pdepth_array
[i
] - mean
) * (pdepth_array
[i
] - mean
);
502 stddev
= sqrt (stddev
/pdepth_array_next
);
504 g_print ("parent depth: max: %d, mean: %f, sttdev: %f, overflowing: %d\n", max_pdepth
, mean
, stddev
, num_pdepth_ovf
);
509 type_size_stats (MonoClass
*klass
)
513 MonoMethodHeader
*header
;
517 while ((method
= mono_class_get_methods (klass
, &iter
))) {
519 header
= mono_method_get_header (method
);
522 mono_method_header_get_code (header
, &size
, &maxs
);
525 g_print ("%s.%s: code: %d\n", klass
->name_space
, klass
->name
, code_size
);
529 size_stats (MonoImage
*image
, const char *name
) {
533 num_types
= mono_image_get_table_rows (image
, MONO_TABLE_TYPEDEF
);
534 for (i
= 0; i
< num_types
; ++i
) {
535 klass
= mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| (i
+ 1));
536 type_size_stats (klass
);
542 get_signature (MonoMethod
*method
) {
544 static GHashTable
*hash
= NULL
;
548 hash
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
549 if ((result
= g_hash_table_lookup (hash
, method
)))
552 res
= g_string_new ("");
553 if (include_namespace
&& *(method
->klass
->name_space
))
554 g_string_append_printf (res
, "%s.", method
->klass
->name_space
);
555 result
= mono_signature_get_desc (mono_method_signature (method
), include_namespace
);
556 g_string_append_printf (res
, "%s:%s(%s)", method
->klass
->name
, method
->name
, result
);
558 g_hash_table_insert (hash
, method
, res
->str
);
561 g_string_free (res
, FALSE
);
567 output_method_edge (MonoMethod
*first
, MonoMethod
*second
) {
568 char * f
= get_signature (first
);
569 char * s
= get_signature (second
);
571 fprintf (output
, "\t\"%s\" -> \"%s\"\n", f
, s
);
575 * We need to handle virtual calls is derived types.
576 * We could check what types implement the method and
577 * disassemble all of them: this can make the graph to explode.
578 * We could try and keep track of the 'this' pointer type and
579 * consider only the methods in the classes derived from that:
580 * this can reduce the graph complexity somewhat (and it would
581 * be the more correct approach).
584 print_method (MonoMethod
*method
, int depth
) {
585 const MonoOpcode
*opcode
;
586 MonoMethodHeader
*header
;
588 static GHashTable
*visited
= NULL
;
589 const unsigned char *ip
, *il_code_end
;
592 if (depth
++ > max_depth
)
596 visited
= g_hash_table_new (NULL
, NULL
);
598 if (g_hash_table_lookup (visited
, method
))
601 g_hash_table_insert (visited
, method
, method
);
603 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
))
605 if (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))
608 header
= mono_method_get_header (method
);
609 ip
= mono_method_header_get_code (header
, &i
, NULL
);
610 il_code_end
= ip
+ i
;
612 hash
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
614 while (ip
< il_code_end
) {
622 opcode
= &mono_opcodes
[i
];
624 switch (opcode
->argument
) {
629 case MonoInlineField
:
631 case MonoInlineString
:
633 case MonoShortInlineR
:
635 case MonoInlineBrTarget
:
641 case MonoShortInlineVar
:
642 case MonoShortInlineI
:
643 case MonoShortInlineBrTarget
:
646 case MonoInlineSwitch
: {
658 case MonoInlineMethod
: {
664 called
= mono_get_method (method
->klass
->image
, token
, NULL
);
667 if (g_hash_table_lookup (hash
, called
))
669 g_hash_table_insert (hash
, called
, called
);
670 output_method_edge (method
, called
);
671 print_method (called
, depth
);
675 g_assert_not_reached ();
678 g_hash_table_destroy (hash
);
682 method_graph (MonoImage
*image
, const char *name
) {
684 MonoMethod
*method
= NULL
;
687 guint32 token
= mono_image_get_entry_point (image
);
688 if (!token
|| !(method
= mono_get_method (image
, token
, NULL
))) {
689 g_print ("Cannot find entry point in %s: specify an explict method name.\n", mono_image_get_filename (image
));
693 /* search the method */
694 MonoMethodDesc
*desc
;
696 desc
= mono_method_desc_new (name
, include_namespace
);
698 g_print ("Invalid method name %s\n", name
);
701 method
= mono_method_desc_search_in_image (desc
, image
);
703 g_print ("Cannot find method %s\n", name
);
707 fprintf (output
, "digraph blah {\n");
708 fprintf (output
, "%s", graph_properties
);
710 print_method (method
, depth
);
712 fprintf (output
, "}\n");
715 typedef struct MonoBasicBlock MonoBasicBlock
;
717 struct MonoBasicBlock
{
718 const unsigned char* cil_code
;
725 static const unsigned char *debug_start
;
728 link_bblock (MonoBasicBlock
*from
, MonoBasicBlock
* to
)
730 from
->out_bb
= g_list_prepend (from
->out_bb
, to
);
731 to
->in_bb
= g_list_prepend (to
->in_bb
, from
);
732 /*fprintf (stderr, "linking IL_%04x to IL_%04x\n", from->cil_code-debug_start, to->cil_code-debug_start);*/
736 compare_bblock (const void *a
, const void *b
)
738 MonoBasicBlock
* const *ab
= a
;
739 MonoBasicBlock
* const *bb
= b
;
741 return (*ab
)->cil_code
- (*bb
)->cil_code
;
745 mono_method_find_bblocks (MonoMethodHeader
*header
)
747 const unsigned char *ip
, *end
, *start
;
748 const MonoOpcode
*opcode
;
749 guint32 i
, block_end
= 0;
750 GPtrArray
*result
= g_ptr_array_new ();
751 GHashTable
*table
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
752 MonoBasicBlock
*entry_bb
, *end_bb
, *bb
, *target
;
754 ip
= mono_method_header_get_code (header
, &i
, NULL
);
758 entry_bb
= g_new0 (MonoBasicBlock
, 1);
759 end_bb
= g_new0 (MonoBasicBlock
, 1);
760 g_ptr_array_add (result
, entry_bb
);
761 g_ptr_array_add (result
, end_bb
);
763 bb
= g_new0 (MonoBasicBlock
, 1);
765 g_ptr_array_add (result
, bb
);
766 link_bblock (entry_bb
, bb
);
767 g_hash_table_insert (table
, (char*)ip
, bb
);
770 /* handle exception code blocks... */
773 if ((target
= g_hash_table_lookup (table
, ip
)) && target
!= bb
) {
775 link_bblock (bb
, target
);
780 /*fprintf (stderr, "processing bbclok at IL_%04x\n", ip - header->code);*/
781 if (!(bb
= g_hash_table_lookup (table
, ip
))) {
782 bb
= g_new0 (MonoBasicBlock
, 1);
784 g_ptr_array_add (result
, bb
);
785 g_hash_table_insert (table
, (char*)ip
, bb
);
796 opcode
= &mono_opcodes
[i
];
797 switch (opcode
->flow_type
) {
798 case MONO_FLOW_RETURN
:
799 link_bblock (bb
, end_bb
);
800 case MONO_FLOW_ERROR
:
803 case MONO_FLOW_BRANCH
: /* we handle branch when checking the argument type */
804 case MONO_FLOW_COND_BRANCH
:
810 g_assert_not_reached ();
812 switch (opcode
->argument
) {
817 case MonoInlineField
:
818 case MonoInlineMethod
:
820 case MonoInlineString
:
822 case MonoShortInlineR
:
829 case MonoShortInlineVar
:
830 case MonoShortInlineI
:
833 case MonoShortInlineBrTarget
:
834 case MonoInlineBrTarget
:
836 if (opcode
->argument
== MonoShortInlineBrTarget
) {
837 i
= (signed char)*ip
;
840 i
= (gint32
) read32 (ip
);
843 if (opcode
->flow_type
== MONO_FLOW_COND_BRANCH
) {
844 if (!(target
= g_hash_table_lookup (table
, ip
))) {
845 target
= g_new0 (MonoBasicBlock
, 1);
846 target
->cil_code
= ip
;
847 g_ptr_array_add (result
, target
);
848 g_hash_table_insert (table
, (char*)ip
, target
);
850 link_bblock (bb
, target
);
852 if (!(target
= g_hash_table_lookup (table
, ip
+ i
))) {
853 target
= g_new0 (MonoBasicBlock
, 1);
854 target
->cil_code
= ip
+ i
;
855 g_ptr_array_add (result
, target
);
856 g_hash_table_insert (table
, (char*)ip
+ i
, target
);
858 link_bblock (bb
, target
);
861 case MonoInlineSwitch
: {
863 const char *itarget
, *st
;
867 st
= (const char*)ip
+ 4 * n
;
869 for (i
= 0; i
< n
; i
++) {
870 itarget
= st
+ read32 (ip
);
872 if (!(target
= g_hash_table_lookup (table
, itarget
))) {
873 target
= g_new0 (MonoBasicBlock
, 1);
874 target
->cil_code
= (const guchar
*)itarget
;
875 g_ptr_array_add (result
, target
);
876 g_hash_table_insert (table
, (gpointer
) itarget
, target
);
878 link_bblock (bb
, target
);
881 * Note: the code didn't set block_end in switch.
890 g_assert_not_reached ();
894 g_hash_table_destroy (table
);
895 qsort (result
->pdata
, result
->len
, sizeof (gpointer
), compare_bblock
);
896 /* skip entry and end */
898 for (i
= 2; i
< result
->len
; ++i
) {
899 bb
= (MonoBasicBlock
*)g_ptr_array_index (result
, i
);
901 target
->cil_length
= bb
->cil_code
- target
->cil_code
;
903 /*fprintf (stderr, "bblock %d at IL_%04x:\n", i, bb->cil_code - header->code);*/
905 bb
->cil_length
= end
- bb
->cil_code
;
910 indenter (MonoDisHelper
*dh
, MonoMethod
*method
, guint32 ip_offset
)
912 return g_strdup (" ");
915 static MonoDisHelper graph_dh
= {
925 df_visit (MonoBasicBlock
*bb
, int *dfn
, const unsigned char* code
)
928 MonoBasicBlock
*next
;
934 for (tmp
= bb
->out_bb
; tmp
; tmp
= tmp
->next
) {
938 fprintf (output
, "\t\"DF entry\" -> \"IL_%04x (%d)\"\n", (unsigned int)(next
->cil_code
- code
), *dfn
+ 1);
940 fprintf (output
, "\t\"IL_%04x (%d)\" -> \"IL_%04x (%d)\"\n", (unsigned int)(bb
->cil_code
- code
), bb
->dfn
, (unsigned int)(next
->cil_code
- code
), *dfn
+ 1);
941 df_visit (next
, dfn
, code
);
947 print_method_cfg (MonoMethod
*method
) {
950 MonoBasicBlock
*bb
, *target
;
951 MonoMethodHeader
*header
;
954 const unsigned char *il_code
;
956 header
= mono_method_get_header (method
);
957 il_code
= mono_method_header_get_code (header
, NULL
, NULL
);
958 bblocks
= mono_method_find_bblocks (header
);
959 for (i
= 0; i
< bblocks
->len
; ++i
) {
960 bb
= (MonoBasicBlock
*)g_ptr_array_index (bblocks
, i
);
962 fprintf (output
, "\tB%p [shape=record,label=\"entry\"]\n", bb
);
964 fprintf (output
, "\tB%p [shape=record,label=\"end\"]\n", bb
);
966 code
= mono_disasm_code (&graph_dh
, method
, bb
->cil_code
, bb
->cil_code
+ bb
->cil_length
);
967 fprintf (output
, "\tB%p [shape=record,label=\"IL_%04x\\n%s\"]\n", bb
, (unsigned int)(bb
->cil_code
- il_code
), code
);
971 for (i
= 0; i
< bblocks
->len
; ++i
) {
972 bb
= (MonoBasicBlock
*)g_ptr_array_index (bblocks
, i
);
973 for (tmp
= bb
->out_bb
; tmp
; tmp
= tmp
->next
) {
975 fprintf (output
, "\tB%p -> B%p\n", bb
, target
);
979 for (i
= 0; i
< bblocks
->len
; ++i
) {
980 bb
= (MonoBasicBlock
*)g_ptr_array_index (bblocks
, i
);
984 for (i
= 0; i
< bblocks
->len
; ++i
) {
985 bb
= (MonoBasicBlock
*)g_ptr_array_index (bblocks
, i
);
986 df_visit (bb
, &dfn
, il_code
);
992 * TODO: change to create the DF tree, dominance relation etc.
995 method_cfg (MonoImage
*image
, const char *name
) {
996 MonoMethod
*method
= NULL
;
997 const static char *cfg_graph_properties
= "\tnode [fontsize=8.0]\n\tedge [len=1.5,color=red]\n";
1000 guint32 token
= mono_image_get_entry_point (image
);
1001 if (!token
|| !(method
= mono_get_method (image
, token
, NULL
))) {
1002 g_print ("Cannot find entry point in %s: specify an explict method name.\n", mono_image_get_filename (image
));
1006 /* search the method */
1007 MonoMethodDesc
*desc
;
1009 desc
= mono_method_desc_new (name
, include_namespace
);
1011 g_print ("Invalid method name %s\n", name
);
1014 method
= mono_method_desc_search_in_image (desc
, image
);
1016 g_print ("Cannot find method %s\n", name
);
1020 fprintf (output
, "digraph blah {\n");
1021 fprintf (output
, "%s", cfg_graph_properties
);
1023 print_method_cfg (method
);
1025 fprintf (output
, "}\n");
1030 printf ("monograph 0.2 Copyright (c) 2002 Ximian, Inc\n");
1031 printf ("Create call graph or type hierarchy information from CIL assemblies.\n");
1032 printf ("Usage: monograph [options] [assembly [typename|methodname]]\n\n");
1033 printf ("Valid options are:\n");
1034 printf ("\t-c|--call output call graph instead of type hierarchy\n");
1035 printf ("\t-C|--control-flow output control flow of methodname\n");
1036 printf ("\t--stats output some statistics about the assembly\n");
1037 printf ("\t--size output some size statistics about the assembly\n");
1038 printf ("\t-d|--depth num max depth recursion (default: 6)\n");
1039 printf ("\t-o|--output filename write graph to file filename (default: stdout)\n");
1040 printf ("\t-f|--fullname include namespace in type and method names\n");
1041 printf ("\t-n|--neato invoke neato directly\n");
1042 printf ("\t-v|--verbose verbose operation\n\n");
1043 printf ("The default assembly is mscorlib.dll. The default method for\n");
1044 printf ("the --call and --control-flow options is the entrypoint.\n\n");
1045 printf ("When the --neato option is used the output type info is taken\n");
1046 printf ("from the output filename extension. You need the graphviz package\n");
1047 printf ("installed to be able to use this option and build bitmap files.\n");
1048 printf ("Without --neato, monograph will create .dot files, a description\n");
1049 printf ("file for a graph.\n\n");
1050 printf ("Sample runs:\n");
1051 printf ("\tmonograph -n -o vt.png mscorlib.dll System.ValueType\n");
1052 printf ("\tmonograph -n -o expr.png mcs.exe Mono.CSharp.Expression\n");
1053 printf ("\tmonograph -n -o cfg.png -C mcs.exe Driver:Main\n");
1054 printf ("\tmonograph -d 3 -n -o callgraph.png -c mis.exe\n");
1069 * * virtual method calls as explained above
1070 * * maybe track field references
1071 * * track what exceptions a method could throw?
1072 * * for some inputs neato appears to hang or take a long time: kill it?
1073 * * allow passing additional command-line arguments to neato
1074 * * allow setting different graph/node/edge options directly
1075 * * option to avoid specialname methods
1076 * * make --neato option the default?
1077 * * use multiple classes/method roots?
1079 * * reverse call graph: given a method what methods call it?
1082 main (int argc
, char *argv
[]) {
1083 MonoAssembly
*assembly
;
1085 const char *cname
= NULL
;
1086 const char *aname
= NULL
;
1087 char *outputfile
= NULL
;
1088 int graphtype
= GRAPH_TYPES
;
1094 for (i
= 1; i
< argc
; ++i
) {
1095 if (argv
[i
] [0] != '-')
1097 if (strcmp (argv
[i
], "--call") == 0 || strcmp (argv
[i
], "-c") == 0) {
1098 graphtype
= GRAPH_CALL
;
1099 } else if (strcmp (argv
[i
], "--control-flow") == 0 || strcmp (argv
[i
], "-C") == 0) {
1100 graphtype
= GRAPH_CONTROL_FLOW
;
1101 } else if (strcmp (argv
[i
], "--interface") == 0 || strcmp (argv
[i
], "-i") == 0) {
1102 graphtype
= GRAPH_INTERFACE
;
1103 } else if (strcmp (argv
[i
], "--stats") == 0) {
1104 graphtype
= GRAPH_STATS
;
1105 } else if (strcmp (argv
[i
], "--size") == 0) {
1106 graphtype
= GRAPH_SIZE_STATS
;
1107 } else if (strcmp (argv
[i
], "--fullname") == 0 || strcmp (argv
[i
], "-f") == 0) {
1108 include_namespace
= 1;
1109 } else if (strcmp (argv
[i
], "--neato") == 0 || strcmp (argv
[i
], "-n") == 0) {
1111 } else if (strcmp (argv
[i
], "--verbose") == 0 || strcmp (argv
[i
], "-v") == 0) {
1113 } else if (strcmp (argv
[i
], "--output") == 0 || strcmp (argv
[i
], "-o") == 0) {
1116 outputfile
= argv
[++i
];
1117 } else if (strcmp (argv
[i
], "--depth") == 0 || strcmp (argv
[i
], "-d") == 0) {
1120 max_depth
= atoi (argv
[++i
]);
1129 cname
= argv
[i
+ 1];
1131 mono_init_from_assembly (argv
[0], aname
);
1132 assembly
= mono_assembly_open (aname
, NULL
);
1134 mono_init (argv
[0]);
1135 assembly
= mono_image_get_assembly (mono_get_corlib ());
1137 if (!cname
&& (graphtype
== GRAPH_TYPES
))
1138 cname
= "System.Object";
1141 g_print ("cannot open assembly %s\n", aname
);
1146 GString
*command
= g_string_new ("neato");
1150 type
= strrchr (outputfile
, '.');
1151 g_string_append_printf (command
, " -o %s", outputfile
);
1154 g_string_append_printf (command
, " -T%s", type
+ 1);
1155 output
= popen (command
->str
, "w");
1157 g_print ("Cannot run neato: you may need to install the graphviz package.\n");
1160 } else if (outputfile
) {
1161 output
= fopen (outputfile
, "w");
1163 g_print ("Cannot open file: %s\n", outputfile
);
1167 /* if it looks like a method name, we want a callgraph. */
1168 if (cname
&& strchr (cname
, ':') && graphtype
== GRAPH_TYPES
)
1169 graphtype
= GRAPH_CALL
;
1171 image
= mono_assembly_get_image (assembly
);
1172 switch (graphtype
) {
1174 type_graph (image
, cname
);
1177 method_graph (image
, cname
);
1179 case GRAPH_INTERFACE
:
1180 interface_graph (image
, cname
);
1182 case GRAPH_CONTROL_FLOW
:
1183 method_cfg (image
, cname
);
1186 stats (image
, cname
);
1188 case GRAPH_SIZE_STATS
:
1189 size_stats (image
, cname
);
1192 g_error ("wrong graph type");
1197 g_print ("waiting for neato.\n");
1199 } else if (outputfile
)