2 * monodiet.c: an IL code garbage collector
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2004 Novell, Inc.
12 #include "mono/metadata/class-internals.h"
13 #include "mono/metadata/assembly.h"
14 #include "mono/metadata/tokentype.h"
15 #include "mono/metadata/opcodes.h"
16 #include "mono/metadata/tabledefs.h"
17 #include "mono/metadata/mono-endian.h"
18 #include "mono/metadata/appdomain.h" /* mono_init */
19 #include "mono/metadata/debug-helpers.h"
23 *) handle proprties, events in a smart way.
24 *) add option that takes a directory and outputs the il files and recompiles automatically
26 static GHashTable *type_table;
27 static GHashTable *method_table;
28 static GHashTable *field_table;
29 static GHashTable *image_table;
30 static GList *virtual_methods;
31 static int verbose = 0;
32 static int force_enums = 0;
33 static FILE *outf = NULL;
38 TYPE_METHODS = 1 << 2,
39 TYPE_PROPERTIES = 1 << 3,
41 TYPE_ALL = TYPE_BASIC | TYPE_FIELDS | TYPE_METHODS | TYPE_PROPERTIES | TYPE_EVENTS
44 static void handle_cattrs (MonoCustomAttrInfo* cattrs);
47 add_type (MonoClass* klass)
49 gpointer val = NULL, oldkey = NULL;
50 if (g_hash_table_lookup_extended (type_table, klass, &oldkey, &val))
52 g_hash_table_insert (type_table, klass, NULL);
53 g_hash_table_insert (image_table, klass->image, NULL);
57 add_types_from_signature (MonoMethodSignature *sig)
61 for (i = 0; i < sig->param_count; ++i) {
62 klass = mono_class_from_mono_type (sig->params [i]);
65 klass = mono_class_from_mono_type (sig->ret);
70 add_field (MonoClassField *field) {
72 MonoCustomAttrInfo* cattrs;
73 gpointer val = NULL, oldkey = NULL;
75 if (g_hash_table_lookup_extended (field_table, field, &oldkey, &val))
77 g_hash_table_insert (field_table, field, NULL);
78 add_type (field->parent);
79 k = mono_class_from_mono_type (field->type);
81 cattrs = mono_custom_attrs_from_field (field->parent, field);
82 handle_cattrs (cattrs);
86 add_types_from_method (MonoMethod *method) {
87 const MonoOpcode *opcode;
88 MonoMethodHeader *header;
89 const unsigned char *ip, *il_code_end;
90 gpointer val = NULL, oldkey = NULL;
94 MonoClassField *field;
95 MonoCustomAttrInfo* cattrs;
98 MonoExceptionClause clause;
100 if (g_hash_table_lookup_extended (method_table, method, &oldkey, &val))
102 g_hash_table_insert (method_table, method, NULL);
104 g_assert (method->klass);
107 g_print ("#processing method: %s\n", mono_method_full_name (method, TRUE));
108 mono_class_init (method->klass);
109 cattrs = mono_custom_attrs_from_method (method);
110 handle_cattrs (cattrs);
111 add_type (method->klass);
112 add_types_from_signature (mono_method_signature (method));
113 for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i) {
114 cattrs = mono_custom_attrs_from_param (method, i);
115 handle_cattrs (cattrs);
118 if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
119 virtual_methods = g_list_prepend (virtual_methods, method);
121 /* if no IL code to parse, return */
122 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
124 if (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))
127 header = mono_method_get_header (method);
129 locals = mono_method_header_get_locals (header, &n, NULL);
130 for (i = 0; i < n; ++i) {
131 klass = mono_class_from_mono_type (locals [i]);
134 for (exc_iter = NULL; mono_method_header_get_clauses (header, method, &exc_iter, &clause);) {
135 if (clause.flags == MONO_EXCEPTION_CLAUSE_NONE)
136 add_type (clause.data.catch_class);
139 ip = mono_method_header_get_code (header, &n, NULL);
140 il_code_end = ip + n;
142 while (ip < il_code_end) {
144 g_print ("#%s", mono_disasm_code_one (NULL, method, ip, NULL));
152 opcode = &mono_opcodes [i];
154 switch (opcode->argument) {
159 token = read32 (ip + 1);
160 add_type (mono_class_get (method->klass->image, token));
163 case MonoInlineField: {
164 token = read32 (ip + 1);
165 field = mono_field_from_token (method->klass->image, token, &klass, NULL);
174 case MonoInlineString:
175 case MonoShortInlineR:
176 case MonoInlineBrTarget:
183 case MonoShortInlineVar:
184 case MonoShortInlineI:
185 case MonoShortInlineBrTarget:
188 case MonoInlineSwitch:
198 case MonoInlineMethod:
200 MonoMethod *cm = mono_get_method (method->klass->image, read32 (ip + 1), NULL);
201 add_type (cm->klass);
202 add_types_from_method (cm);
207 g_assert_not_reached ();
213 handle_cattrs (MonoCustomAttrInfo* cattrs)
218 for (i = 0; i < cattrs->num_attrs; ++i) {
219 add_types_from_method (cattrs->attrs [i].ctor);
224 handle_type (MonoClass *klass, guint32 flags)
228 MonoCustomAttrInfo* cattrs;
229 gpointer val = NULL, oldkey = NULL;
233 MonoClassField* field;
236 if (g_hash_table_lookup_extended (type_table, klass, &oldkey, &val)) {
237 missing = flags & ~(GPOINTER_TO_UINT (val));
243 g_hash_table_insert (type_table, klass, GUINT_TO_POINTER (missing));
245 g_print ("#processing klass: %s.%s\n", klass->name_space, klass->name);
246 mono_class_init (klass);
248 add_type (klass->parent);
249 if (klass->nested_in)
250 add_type (klass->nested_in);
252 while ((method = mono_class_get_methods (klass, &iter))) {
253 if ((missing & TYPE_METHODS) || strcmp (method->name, ".cctor") == 0)
254 add_types_from_method (method);
256 if (klass->enumtype) {
257 add_field (mono_class_get_field_from_name (klass, "value__"));
259 if (force_enums || (missing & TYPE_FIELDS)) {
261 while ((field = mono_class_get_fields (klass, &iter)))
265 while ((prop = mono_class_get_properties (klass, &iter))) {
266 cattrs = mono_custom_attrs_from_property (klass, prop);
267 handle_cattrs (cattrs);
270 while ((event = mono_class_get_events (klass, &iter))) {
271 cattrs = mono_custom_attrs_from_event (klass, event);
272 handle_cattrs (cattrs);
274 for (i = 0; i < klass->interface_count; ++i)
275 add_type (klass->interfaces [i]);
276 cattrs = mono_custom_attrs_from_class (klass);
277 handle_cattrs (cattrs);
281 process_image (MonoImage *image, gboolean all) {
283 const MonoTableInfo *t;
289 g_print ("#processing image: %s\n", mono_image_get_name (image));
290 eptoken = mono_image_get_entry_point (image);
292 entry = mono_get_method (image, eptoken, NULL);
293 add_types_from_method (entry);
295 /* we always add the <Module> type */
296 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | 1);
297 handle_type (klass, all? TYPE_ALL: TYPE_BASIC);
299 t = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
300 for (i = 1; i < mono_table_info_get_rows (t); ++i) {
301 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | (i + 1));
302 handle_type (klass, all? TYPE_ALL: TYPE_BASIC);
308 process_assembly (MonoAssembly *assembly, gboolean all) {
309 MonoCustomAttrInfo* cattrs;
310 process_image (mono_assembly_get_image (assembly), all);
311 cattrs = mono_custom_attrs_from_assembly (assembly);
312 handle_cattrs (cattrs);
315 static GList *worklist = NULL;
318 collect_type (const gpointer key, const gpointer val, gpointer user_data)
320 MonoClass *klass = key;
321 if (klass->rank || klass->byval_arg.type == MONO_TYPE_PTR)
323 worklist = g_list_prepend (worklist, key);
327 check_vmethods (MonoClass *klass, MonoMethod *method)
330 if (method->klass == klass)
332 mono_class_init (klass);
333 mono_class_init (method->klass);
334 vtable = klass->vtable;
338 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
339 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, method->klass->interface_id)) {
340 int iface_offset = mono_class_interface_offset (klass, method->klass);
341 g_assert (method->slot != -1);
342 if (vtable [iface_offset + method->slot])
343 add_types_from_method (vtable [iface_offset + method->slot]);
346 if (mono_class_has_parent (klass, method->klass)) {
347 g_assert (method->slot != -1);
348 if (vtable [method->slot])
349 add_types_from_method (vtable [method->slot]);
355 process_images (void) {
356 int old_count, new_count;
357 GList *item, *vmethod;
358 new_count = g_hash_table_size (type_table);
359 new_count += g_hash_table_size (method_table);
360 new_count += g_hash_table_size (field_table);
362 old_count = new_count;
364 g_print ("#processing type table: %d\n", old_count);
365 g_list_free (worklist);
367 g_hash_table_foreach (type_table, collect_type, NULL);
368 for (item = worklist; item; item = item->next) {
369 for (vmethod = virtual_methods; vmethod; vmethod = vmethod->next) {
370 check_vmethods (item->data, vmethod->data);
373 g_list_free (worklist);
375 g_hash_table_foreach (type_table, collect_type, NULL);
376 for (item = worklist; item; item = item->next) {
377 handle_type (item->data, TYPE_BASIC);
379 new_count = g_hash_table_size (type_table);
380 new_count += g_hash_table_size (method_table);
381 new_count += g_hash_table_size (field_table);
382 } while (old_count != new_count);
386 foreach_method (const gpointer key, const gpointer val, gpointer user_data)
388 MonoMethod *method = key;
389 MonoClass *klass = user_data;
390 if (method->klass != klass)
392 /* FIXME: ensure it's the correct token */
393 fprintf (outf, "M:0x%x\n", mono_metadata_token_index (method->token));
397 foreach_field (const gpointer key, const gpointer val, gpointer user_data)
399 MonoClassField *field = key;
400 MonoClass *klass = user_data;
402 if (field->parent != klass)
404 idx = mono_metadata_token_index (mono_class_get_field_token (field));
405 fprintf (outf, "F:0x%x\n", idx);
409 foreach_type (const gpointer key, const gpointer val, gpointer user_data)
411 MonoClass *klass = key;
412 MonoImage *image = user_data;
413 if (klass->image != image)
415 if (klass->rank || klass->byval_arg.type == MONO_TYPE_PTR)
417 fprintf (outf, "T:0x%x\n", mono_metadata_token_index (klass->type_token));
418 g_hash_table_foreach (method_table, foreach_method, klass);
419 g_hash_table_foreach (field_table, foreach_field, klass);
423 foreach_image (const gpointer key, const gpointer val, gpointer user_data)
425 MonoImage *image = key;
427 aname = mono_image_get_name (image);
428 /* later print the guid as well to prevent mismatches */
429 fprintf (outf, "[%s]\n", aname);
430 g_hash_table_foreach (type_table, foreach_type, image);
434 dump_images (const char *filename)
437 FILE* f = fopen (filename, "w");
439 g_error ("cannot write to file '%s'\n", filename);
445 g_hash_table_foreach (image_table, foreach_image, NULL);
452 printf ("monodiet 0.1 Copyright (c) 2004 Novell, Inc\n");
453 printf ("List the metadata elements used by the named assemblies.\n");
454 printf ("Usage: monodiet [options] assembly [assembly2 ...]\n\n");
455 printf ("Options:\n");
456 printf ("\t-v increase verbose level\n");
457 printf ("\t-h show help screen\n");
458 printf ("\t-e force inclusion of enum members\n");
459 printf ("\t-o FILENAME output the result to filename\n");
460 printf ("\t-a FILENAME add metadata elements from description in filename\n");
461 printf ("\t-F ASSEMBLY force add all metadata elements from assembly\n");
466 find_image (char *name)
468 return mono_image_loaded (name);
472 find_class (MonoImage *image, char *name)
474 char *p = strrchr (name, '.');
477 return mono_class_from_name (image, name, p + 1);
479 return mono_class_from_name (image, "", name);
483 load_roots (const char* filename)
489 MonoImage *image = NULL;
490 MonoClass *klass = NULL;
491 MonoClassField *field;
492 MonoMethodDesc *mdesc;
495 if (!(file = fopen (filename, "r")))
498 while (fgets (buf, sizeof (buf), file)) {
500 * decide on the format to use to express types, fields, methods,
501 * maybe the same used on output from the tool, but with explicit
502 * names and signatures instead of token indexes
503 * add wildcard support
507 while (*s && g_ascii_isspace (*s)) ++s;
511 continue; /* comment */
515 g_error ("invalid assembly format at line %d\n", line);
518 image = find_image (p);
520 g_error ("image not loaded: %s\n", p);
525 g_error ("invalid type format at line %d\n", line);
528 klass = find_class (image, s + 2);
532 g_error ("invalid field format at line %d\n", line);
533 if (!image || !klass)
537 handle_type (klass, TYPE_FIELDS);
540 field = mono_class_get_field_from_name (klass, p);
542 g_warning ("no field '%s' at line %d\n", p, line);
548 g_error ("invalid method format at line %d\n", line);
549 if (!image || !klass)
553 handle_type (klass, TYPE_METHODS);
556 mdesc = mono_method_desc_new (p, FALSE);
558 g_error ("invalid method desc at line %d\n", line);
560 method = mono_method_desc_search_in_class (mdesc, klass);
562 g_warning ("no method '%s' at line %d\n", p, line);
564 add_types_from_method (method);
565 mono_method_desc_free (mdesc);
568 g_error ("invalid format at line %d\n", line);
575 main (int argc, char *argv[]) {
576 MonoAssembly *assembly = NULL;
577 const char *aname = NULL;
578 const char *outfile = NULL;
579 const char *rootfile = NULL;
581 gboolean all_meta = FALSE;
583 mono_init (argv [0]);
585 type_table = g_hash_table_new (NULL, NULL);
586 method_table = g_hash_table_new (NULL, NULL);
587 field_table = g_hash_table_new (NULL, NULL);
588 image_table = g_hash_table_new (NULL, NULL);
590 for (i = 1; i < argc; ++i) {
593 if (strcmp (aname, "-v") == 0) {
596 } else if (strcmp (aname, "-e") == 0) {
599 } else if (strcmp (aname, "-h") == 0) {
601 } else if (strcmp (aname, "-o") == 0) {
607 } else if (strcmp (aname, "-F") == 0) {
613 } else if (strcmp (aname, "-a") == 0) {
620 assembly = mono_assembly_open (aname, NULL);
622 g_print ("cannot open assembly %s\n", aname);
625 process_assembly (assembly, all_meta);
630 load_roots (rootfile);
632 dump_images (outfile);