2 * driver.c: The new mono JIT compiler.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002-2003 Ximian, Inc.
9 * (C) 2003-2006 Novell, Inc.
14 #if HAVE_SCHED_SETAFFINITY
21 #include <mono/metadata/assembly.h>
22 #include <mono/metadata/loader.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/class.h>
25 #include <mono/metadata/object.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/opcodes.h>
28 #include <mono/metadata/mono-endian.h>
29 #include <mono/metadata/tokentype.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/threads.h>
32 #include <mono/metadata/marshal.h>
33 #include <mono/metadata/socket-io.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/verify.h>
42 #include <mono/metadata/mono-debug.h>
43 #include <mono/metadata/security-manager.h>
44 #include <mono/metadata/security-core-clr.h>
45 #include <mono/metadata/gc-internal.h>
46 #include "mono/utils/mono-counters.h"
56 static FILE *mini_stats_fd = NULL;
58 static void mini_usage (void);
61 /* Need this to determine whether to detach console */
62 #include <mono/metadata/cil-coff.h>
63 /* This turns off command line globbing under win32 */
67 typedef void (*OptFunc) (const char *p);
70 #ifdef HAVE_ARRAY_ELEM_INIT
71 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
72 #define MSGSTRFIELD1(line) str##line
74 static const struct msgstr_t {
75 #define OPTFLAG(id,shift,name,desc) char MSGSTRFIELD(__LINE__) [sizeof (name) + sizeof (desc)];
76 #include "optflags-def.h"
79 #define OPTFLAG(id,shift,name,desc) name "\0" desc,
80 #include "optflags-def.h"
83 static const gint16 opt_names [] = {
84 #define OPTFLAG(id,shift,name,desc) [(shift)] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
85 #include "optflags-def.h"
89 #define optflag_get_name(id) ((const char*)&opstr + opt_names [(id)])
90 #define optflag_get_desc(id) (optflag_get_name(id) + 1 + strlen (optflag_get_name(id)))
92 #else /* !HAVE_ARRAY_ELEM_INIT */
98 #define OPTFLAG(id,shift,name,desc) {name,desc},
101 #include "optflags-def.h"
104 #define optflag_get_name(id) (opt_names [(id)].name)
105 #define optflag_get_desc(id) (opt_names [(id)].desc)
110 opt_funcs [sizeof (int) * 8] = {
114 #define DEFAULT_OPTIMIZATIONS ( \
115 MONO_OPT_PEEPHOLE | \
118 MONO_OPT_CONSPROP | \
119 MONO_OPT_COPYPROP | \
120 MONO_OPT_TREEPROP | \
126 MONO_OPT_EXCEPTION | \
129 #define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
132 parse_optimizations (const char* p)
134 /* the default value */
135 guint32 opt = DEFAULT_OPTIMIZATIONS;
140 /* call out to cpu detection code here that sets the defaults ... */
141 opt |= mono_arch_cpu_optimizazions (&exclude);
153 for (i = 0; i < G_N_ELEMENTS (opt_names) && optflag_get_name (i); ++i) {
154 n = optflag_get_name (i);
156 if (strncmp (p, n, len) == 0) {
165 } else if (*p == '=') {
169 while (*p && *p++ != ',');
176 if (i == G_N_ELEMENTS (opt_names) || !optflag_get_name (i)) {
177 if (strncmp (p, "all", 3) == 0) {
181 opt = ~(EXCLUDED_FROM_ALL | exclude);
186 fprintf (stderr, "Invalid optimization name `%s'\n", p);
196 const char desc [18];
197 MonoGraphOptions value;
200 static const GraphName
202 {"cfg", "Control Flow", MONO_GRAPH_CFG},
203 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
204 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
205 {"ssa", "CFG after SSA", MONO_GRAPH_CFG_SSA},
206 {"optc", "CFG after IR opts", MONO_GRAPH_CFG_OPTCODE}
209 static MonoGraphOptions
210 mono_parse_graph_options (const char* p)
215 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
216 n = graph_names [i].name;
218 if (strncmp (p, n, len) == 0)
219 return graph_names [i].value;
222 fprintf (stderr, "Invalid graph name provided: %s\n", p);
227 mono_parse_default_optimizations (const char* p)
231 opt = parse_optimizations (p);
236 opt_descr (guint32 flags) {
237 GString *str = g_string_new ("");
241 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
242 if (flags & (1 << i)) {
244 g_string_append_c (str, ',');
245 g_string_append (str, optflag_get_name (i));
249 return g_string_free (str, FALSE);
259 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
260 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
261 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
262 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
263 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
264 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS,
265 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_SSA,
266 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_EXCEPTION,
267 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_EXCEPTION | MONO_OPT_ABCREM,
268 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_EXCEPTION | MONO_OPT_ABCREM | MONO_OPT_SSAPRE,
269 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_ABCREM,
270 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_TREEPROP,
271 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_SSAPRE,
272 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_ABCREM | MONO_OPT_SHARED
275 typedef int (*TestMethod) (void);
279 domain_dump_native_code (MonoDomain *domain) {
280 // need to poke into the domain, move to metadata/domain.c
281 // need to empty jit_info_table and code_mp
286 mini_regression (MonoImage *image, int verbose, int *total_run) {
287 guint32 i, opt, opt_flags;
291 int result, expected, failed, cfailed, run, code_size, total;
293 GTimer *timer = g_timer_new ();
296 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
298 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
299 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
300 opt_flags = opt_sets [opt];
301 n = opt_descr (opt_flags);
305 fprintf (mini_stats_fd, " ");
306 fprintf (mini_stats_fd, "%s", n);
310 fprintf (mini_stats_fd, "));\n");
312 fprintf (mini_stats_fd, "@data = (\n");
313 fprintf (mini_stats_fd, "[");
316 /* load the metadata */
317 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
318 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
319 mono_class_init (method->klass);
321 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
322 fprintf (mini_stats_fd, "\"%s\",", method->name);
326 fprintf (mini_stats_fd, "],\n");
331 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
332 double elapsed, comp_time, start_time;
334 opt_flags = opt_sets [opt];
335 mono_set_defaults (verbose, opt_flags);
336 n = opt_descr (opt_flags);
337 g_print ("Test run: image=%s, opts=%s\n", mono_image_get_filename (image), n);
339 cfailed = failed = run = code_size = 0;
340 comp_time = elapsed = 0.0;
342 /* fixme: ugly hack - delete all previously compiled methods */
343 g_hash_table_destroy (mono_domain_get ()->jit_trampoline_hash);
344 mono_domain_get ()->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
345 mono_internal_hash_table_destroy (&(mono_domain_get ()->jit_code_hash));
346 mono_jit_code_hash_init (&(mono_domain_get ()->jit_code_hash));
348 g_timer_start (timer);
350 fprintf (mini_stats_fd, "[");
351 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
352 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
353 if (strncmp (method->name, "test_", 5) == 0) {
354 expected = atoi (method->name + 5);
356 start_time = g_timer_elapsed (timer, NULL);
357 comp_time -= start_time;
358 cfg = mini_method_compile (method, opt_flags, mono_get_root_domain (), TRUE, FALSE, 0);
359 comp_time += g_timer_elapsed (timer, NULL);
360 if (cfg->exception_type == MONO_EXCEPTION_NONE) {
362 g_print ("Running '%s' ...\n", method->name);
363 #ifdef MONO_USE_AOT_COMPILER
364 if ((func = mono_aot_get_method (mono_get_root_domain (), method)))
368 func = (TestMethod)(gpointer)cfg->native_code;
369 func = (TestMethod)mono_create_ftnptr (mono_get_root_domain (), func);
371 if (result != expected) {
373 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
375 code_size += cfg->code_len;
376 mono_destroy_compile (cfg);
381 g_print ("Test '%s' failed compilation.\n", method->name);
384 fprintf (mini_stats_fd, "%f, ",
385 g_timer_elapsed (timer, NULL) - start_time);
389 fprintf (mini_stats_fd, "],\n");
390 g_timer_stop (timer);
391 elapsed = g_timer_elapsed (timer, NULL);
392 if (failed > 0 || cfailed > 0){
393 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
394 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
396 g_print ("Results: total tests: %d, all pass \n", run);
399 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
400 elapsed - comp_time, comp_time, code_size);
401 total += failed + cfailed;
406 fprintf (mini_stats_fd, ");\n");
407 fflush (mini_stats_fd);
410 g_timer_destroy (timer);
415 mini_regression_list (int verbose, int count, char *images [])
417 int i, total, total_run, run;
420 total_run = total = 0;
421 for (i = 0; i < count; ++i) {
422 ass = mono_assembly_open (images [i], NULL);
424 g_warning ("failed to load assembly: %s", images [i]);
427 total += mini_regression (mono_assembly_get_image (ass), verbose, &run);
431 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
432 total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
434 g_print ("Overall results: tests: %d, 100%% pass, opt combinations: %d\n",
435 total_run, (int)G_N_ELEMENTS (opt_sets));
441 #ifdef MONO_JIT_INFO_TABLE_TEST
442 typedef struct _JitInfoData
447 struct _JitInfoData *next;
467 static int num_threads;
468 static ThreadData *thread_datas;
469 static MonoDomain *test_domain;
472 alloc_random_data (Region *region)
485 data = ®ion->data;
486 pos = random () % (region->num_datas + 1);
488 while (*data != NULL) {
492 data = &(*data)->next;
496 g_assert (*data == region->data);
498 g_assert (prev->next == *data);
501 prev_end = region->start;
503 prev_end = prev->start + prev->length;
506 next_start = region->start + region->length;
508 next_start = (*data)->start;
510 g_assert (prev_end <= next_start);
512 max_len = next_start - prev_end;
514 if (++num_retries >= 10)
521 d = g_new0 (JitInfoData, 1);
522 d->start = prev_end + random () % (max_len / 2);
523 d->length = random () % MIN (max_len, next_start - d->start) + 1;
525 g_assert (d->start >= prev_end && d->start + d->length <= next_start);
527 d->ji = g_new0 (MonoJitInfo, 1);
528 d->ji->method = (MonoMethod*) 0xABadBabe;
529 d->ji->code_start = (gpointer)(gulong) d->start;
530 d->ji->code_size = d->length;
531 d->ji->cas_inited = 1; /* marks an allocated jit info */
542 choose_random_data (Region *region)
548 g_assert (region->num_datas > 0);
550 n = random () % region->num_datas;
552 for (d = ®ion->data, i = 0;
554 d = &(*d)->next, ++i)
561 choose_random_region (ThreadData *td)
563 return &td->regions [random () % td->num_regions];
567 choose_random_thread (void)
569 return &thread_datas [random () % num_threads];
573 free_jit_info_data (ThreadData *td, JitInfoData *free)
575 free->next = td->frees;
578 if (++td->num_frees >= 1000) {
581 for (i = 0; i < 500; ++i)
584 while (free->next != NULL) {
585 JitInfoData *next = free->next->next;
587 g_free (free->next->ji);
596 #define NUM_THREADS 8
597 #define REGIONS_PER_THREAD 10
598 #define REGION_SIZE 0x10000
600 #define MAX_ADDR (REGION_SIZE * REGIONS_PER_THREAD * NUM_THREADS)
606 test_thread_func (ThreadData *td)
608 int mode = MODE_ALLOC;
610 gulong lookup_successes = 0, lookup_failures = 0;
611 MonoDomain *domain = test_domain;
612 int thread_num = (int)(td - thread_datas);
613 gboolean modify_thread = thread_num < NUM_THREADS / 2; /* only half of the threads modify the table */
619 if (td->num_datas == 0) {
622 } else if (modify_thread && random () % 1000 < 5) {
624 if (mode == MODE_ALLOC)
625 alloc = (random () % 100) < 70;
626 else if (mode == MODE_FREE)
627 alloc = (random () % 100) < 30;
631 /* modify threads sometimes look up their own jit infos */
632 if (modify_thread && random () % 10 < 5) {
633 Region *region = choose_random_region (td);
635 if (region->num_datas > 0) {
636 JitInfoData **data = choose_random_data (region);
637 guint pos = (*data)->start + random () % (*data)->length;
638 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char*)(gulong) pos);
640 g_assert ((*data)->ji == ji);
641 g_assert (ji->cas_inited);
644 int pos = random () % MAX_ADDR;
645 char *addr = (char*)(gulong) pos;
646 MonoJitInfo *ji = mono_jit_info_table_find (domain, addr);
649 g_assert (addr >= (char*)ji->code_start && addr < (char*)ji->code_start + ji->code_size);
655 JitInfoData *data = alloc_random_data (choose_random_region (td));
658 mono_jit_info_table_add (domain, data->ji);
663 Region *region = choose_random_region (td);
665 if (region->num_datas > 0) {
666 JitInfoData **data = choose_random_data (region);
669 mono_jit_info_table_remove (domain, (*data)->ji);
671 (*data)->ji->cas_inited = 0; /* marks a free jit info */
674 *data = (*data)->next;
676 free_jit_info_data (td, free);
683 if (++i % 100000 == 0) {
685 g_print ("num datas %d (%ld - %ld): %d", (int)(td - thread_datas),
686 lookup_successes, lookup_failures, td->num_datas);
687 for (j = 0; j < td->num_regions; ++j)
688 g_print (" %d", td->regions [j].num_datas);
692 if (td->num_datas < 100)
694 else if (td->num_datas > 2000)
701 small_id_thread_func (gpointer arg)
703 MonoThread *thread = mono_thread_current ();
704 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
706 g_print ("my small id is %d\n", (int)thread->small_id);
707 mono_hazard_pointer_clear (hp, 1);
709 g_print ("done %d\n", (int)thread->small_id);
714 jit_info_table_test (MonoDomain *domain)
718 g_print ("testing jit_info_table\n");
720 num_threads = NUM_THREADS;
721 thread_datas = g_new0 (ThreadData, num_threads);
723 for (i = 0; i < num_threads; ++i) {
726 thread_datas [i].num_regions = REGIONS_PER_THREAD;
727 thread_datas [i].regions = g_new0 (Region, REGIONS_PER_THREAD);
729 for (j = 0; j < REGIONS_PER_THREAD; ++j) {
730 thread_datas [i].regions [j].start = (num_threads * j + i) * REGION_SIZE;
731 thread_datas [i].regions [j].length = REGION_SIZE;
735 test_domain = domain;
738 for (i = 0; i < 72; ++i)
739 mono_thread_create (domain, small_id_thread_func, NULL);
744 for (i = 0; i < num_threads; ++i)
745 mono_thread_create (domain, test_thread_func, &thread_datas [i]);
758 typedef struct CompileAllThreadArgs {
762 } CompileAllThreadArgs;
765 compile_all_methods_thread_main (CompileAllThreadArgs *args)
767 MonoAssembly *ass = args->ass;
768 int verbose = args->verbose;
769 MonoImage *image = mono_assembly_get_image (ass);
774 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
775 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
776 MonoMethodSignature *sig;
778 if (mono_metadata_has_generic_params (image, token))
781 method = mono_get_method (image, token, NULL);
782 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
783 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
784 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
785 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
788 if (method->klass->generic_container)
790 sig = mono_method_signature (method);
791 if (sig->has_type_parameters)
796 char * desc = mono_method_full_name (method, TRUE);
797 g_print ("Compiling %d %s\n", count, desc);
800 cfg = mini_method_compile (method, args->opts, mono_get_root_domain (), FALSE, FALSE, 0);
801 mono_destroy_compile (cfg);
807 compile_all_methods (MonoAssembly *ass, int verbose, guint32 opts)
809 CompileAllThreadArgs args;
812 args.verbose = verbose;
816 * Need to create a mono thread since compilation might trigger
817 * running of managed code.
819 mono_thread_create (mono_domain_get (), compile_all_methods_thread_main, &args);
821 mono_thread_manage ();
826 * @assembly: reference to an assembly
827 * @argc: argument count
828 * @argv: argument vector
830 * Start execution of a program.
833 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
835 MonoImage *image = mono_assembly_get_image (assembly);
837 guint32 entry = mono_image_get_entry_point (image);
840 g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
841 /* FIXME: remove this silly requirement. */
842 mono_environment_exitcode_set (1);
846 method = mono_get_method (image, entry, NULL);
848 g_print ("The entry point method could not be loaded\n");
849 mono_environment_exitcode_set (1);
853 return mono_runtime_run_main (method, argc, argv, NULL);
866 static void main_thread_handler (gpointer user_data)
868 MainThreadArgs *main_args = user_data;
869 MonoAssembly *assembly;
871 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
873 fprintf (stderr, "Can not open image %s\n", main_args->file);
877 if (mono_compile_aot) {
878 int res = mono_compile_assembly (assembly, main_args->opts, main_args->aot_options);
879 printf ("AOT RESULT %d\n", res);
882 * This must be done in a thread managed by mono since it can invoke
885 if (main_args->opts & MONO_OPT_PRECOMP)
886 mono_precompile_assemblies ();
888 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
893 mini_usage_jitdeveloper (void)
898 "Runtime and JIT debugging options:\n"
899 " --breakonex Inserts a breakpoint on exceptions\n"
900 " --break METHOD Inserts a breakpoint at METHOD entry\n"
901 " --compile METHOD Just compile METHOD in assembly\n"
902 " --compile-all Compiles all the methods in the assembly\n"
903 " --ncompile N Number of times to compile METHOD (default: 1)\n"
904 " --print-vtable Print the vtable of all used classes\n"
905 " --regression Runs the regression test contained in the assembly\n"
906 " --statfile FILE Sets the stat file to FILE\n"
907 " --stats Print statistics about the JIT operations\n"
908 " --wapi=hps|semdel IO-layer maintenance\n"
909 " --inject-async-exc METHOD OFFSET Inject an asynchronous exception at METHOD\n"
912 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
914 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
915 fprintf (stdout, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
920 mini_usage_list_opt (void)
924 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
925 fprintf (stdout, " %-10s %s\n", optflag_get_name (i), optflag_get_desc (i));
932 "Usage is: mono [options] program [program-options]\n"
935 " --aot Compiles the assembly to native code\n"
936 " --debug Enable debugging support\n"
937 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
938 " --trace[=EXPR] Enable tracing, use --help-trace for details\n"
939 " --help-devel Shows more options available to developers\n"
942 " --config FILE Loads FILE as the Mono config\n"
943 " --verbose, -v Increases the verbosity level\n"
944 " --help, -h Show usage information\n"
945 " --version, -V Show version information\n"
946 " --runtime=VERSION Use the VERSION runtime, instead of autodetecting\n"
947 " --optimize=OPT Turns on or off a specific optimization\n"
948 " Use --list-opt to get a list of optimizations\n"
949 " --security[=mode] Turns on the unsupported security manager (off by default)\n"
950 " mode is one of cas or core-clr\n");
954 mini_trace_usage (void)
958 " --trace[=EXPR] Trace every call, optional EXPR controls the scope\n"
960 "EXPR is composed of:\n"
961 " all All assemblies\n"
962 " none No assemblies\n"
963 " program Entry point assembly\n"
964 " assembly Specifies an assembly\n"
965 " M:Type:Method Specifies a method\n"
966 " N:Namespace Specifies a namespace\n"
967 " T:Type Specifies a type\n"
968 " +EXPR Includes expression\n"
969 " -EXPR Excludes expression\n"
970 " disabled Don't print any output until toggled via SIGUSR2\n");
973 static const char info[] =
974 #ifdef HAVE_KW_THREAD
978 #endif /* HAVE_KW_THREAD */
979 "\tGC: " USED_GC_NAME "\n"
980 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
981 "\tSIGSEGV: altstack\n"
983 "\tSIGSEGV: normal\n"
986 "\tNotifications: epoll\n"
988 "\tNotification: Thread + polling\n"
990 "\tArchitecture: " ARCHITECTURE "\n"
991 "\tDisabled: " DISABLED_FEATURES "\n"
994 #ifndef MONO_ARCH_AOT_SUPPORTED
995 #define error_if_aot_unsupported() do {fprintf (stderr, "AOT compilation is not supported on this platform.\n"); exit (1);} while (0)
997 #define error_if_aot_unsupported()
1001 mono_main (int argc, char* argv[])
1003 MainThreadArgs main_args;
1004 MonoAssembly *assembly;
1005 MonoMethodDesc *desc;
1009 const char* aname, *mname = NULL;
1010 char *config_file = NULL;
1012 int enable_debugging = FALSE;
1013 guint32 opt, action = DO_EXEC;
1014 MonoGraphOptions mono_graph_options = 0;
1015 int mini_verbose = 0;
1016 gboolean enable_profile = FALSE;
1017 char *trace_options = NULL;
1018 char *profile_options = NULL;
1019 char *aot_options = NULL;
1020 char *forced_version = NULL;
1021 #ifdef MONO_JIT_INFO_TABLE_TEST
1022 int test_jit_info_table = FALSE;
1025 setlocale (LC_ALL, "");
1027 #if HAVE_SCHED_SETAFFINITY
1028 if (getenv ("MONO_NO_SMP")) {
1029 unsigned long proc_mask = 1;
1030 sched_setaffinity (getpid(), sizeof (unsigned long), (gpointer)&proc_mask);
1033 if (!g_thread_supported ())
1034 g_thread_init (NULL);
1036 if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
1037 GMemVTable mem_vtable;
1040 * Instruct glib to use the system allocation functions so valgrind
1041 * can track the memory allocated by the g_... functions.
1043 memset (&mem_vtable, 0, sizeof (mem_vtable));
1044 mem_vtable.malloc = malloc;
1045 mem_vtable.realloc = realloc;
1046 mem_vtable.free = free;
1047 mem_vtable.calloc = calloc;
1049 g_mem_set_vtable (&mem_vtable);
1052 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
1053 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
1055 opt = parse_optimizations (NULL);
1057 for (i = 1; i < argc; ++i) {
1058 if (argv [i] [0] != '-')
1060 if (strcmp (argv [i], "--regression") == 0) {
1061 action = DO_REGRESSION;
1062 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
1064 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
1065 g_print ("Mono JIT compiler version %s (%s)\nCopyright (C) 2002-2007 Novell, Inc and Contributors. www.mono-project.com\n", VERSION, FULL_VERSION);
1069 const char *clibpath;
1071 cerror = mono_check_corlib_version ();
1072 clibpath = mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown";
1074 g_print ("The currently installed mscorlib doesn't match this runtime version.\n");
1075 g_print ("The error is: %s\n", cerror);
1076 g_print ("mscorlib.dll loaded at: %s\n", clibpath);
1081 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
1084 } else if (strcmp (argv [i], "--help-trace") == 0){
1085 mini_trace_usage ();
1087 } else if (strcmp (argv [i], "--help-devel") == 0){
1088 mini_usage_jitdeveloper ();
1090 } else if (strcmp (argv [i], "--list-opt") == 0){
1091 mini_usage_list_opt ();
1093 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
1095 fprintf (stderr, "error: --statfile requires a filename argument\n");
1098 mini_stats_fd = fopen (argv [++i], "w+");
1099 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
1100 opt = parse_optimizations (argv [i] + 11);
1101 } else if (strncmp (argv [i], "-O=", 3) == 0) {
1102 opt = parse_optimizations (argv [i] + 3);
1103 } else if (strcmp (argv [i], "--config") == 0) {
1105 fprintf (stderr, "error: --config requires a filename argument\n");
1108 config_file = argv [++i];
1109 } else if (strcmp (argv [i], "--ncompile") == 0) {
1111 fprintf (stderr, "error: --ncompile requires an argument\n");
1114 count = atoi (argv [++i]);
1116 } else if (strcmp (argv [i], "--trace") == 0) {
1117 trace_options = (char*)"";
1118 } else if (strncmp (argv [i], "--trace=", 8) == 0) {
1119 trace_options = &argv [i][8];
1120 } else if (strcmp (argv [i], "--breakonex") == 0) {
1121 mono_break_on_exc = TRUE;
1122 } else if (strcmp (argv [i], "--break") == 0) {
1124 fprintf (stderr, "Missing method name in --break command line option\n");
1128 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
1129 fprintf (stderr, "Error: invalid method name '%s'\n", argv [i]);
1130 } else if (strcmp (argv [i], "--inject-async-exc") == 0) {
1131 if (i + 2 >= argc) {
1132 fprintf (stderr, "Missing method name or position in --inject-async-exc command line option\n");
1135 mono_inject_async_exc_method = mono_method_desc_new (argv [++i], TRUE);
1136 if (mono_inject_async_exc_method == NULL) {
1137 fprintf (stderr, "Method name is in a bad format in --inject-async-exc command line option\n");
1140 mono_inject_async_exc_pos = atoi (argv [++i]);
1141 } else if (strcmp (argv [i], "--print-vtable") == 0) {
1142 mono_print_vtable = TRUE;
1143 } else if (strcmp (argv [i], "--stats") == 0) {
1144 mono_counters_enable (-1);
1145 mono_stats.enabled = TRUE;
1146 mono_jit_stats.enabled = TRUE;
1148 } else if (strcmp (argv [i], "--aot") == 0) {
1149 error_if_aot_unsupported ();
1150 mono_compile_aot = TRUE;
1151 } else if (strncmp (argv [i], "--aot=", 6) == 0) {
1152 error_if_aot_unsupported ();
1153 mono_compile_aot = TRUE;
1154 aot_options = &argv [i][6];
1156 } else if (strcmp (argv [i], "--compile-all") == 0) {
1157 action = DO_COMPILE;
1158 } else if (strncmp (argv [i], "--runtime=", 10) == 0) {
1159 forced_version = &argv [i][10];
1160 } else if (strcmp (argv [i], "--profile") == 0) {
1161 enable_profile = TRUE;
1162 profile_options = NULL;
1163 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
1164 enable_profile = TRUE;
1165 profile_options = argv [i] + 10;
1166 } else if (strcmp (argv [i], "--compile") == 0) {
1168 fprintf (stderr, "error: --compile option requires a method name argument\n");
1174 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
1176 fprintf (stderr, "error: --graph option requires a method name argument\n");
1180 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
1183 } else if (strcmp (argv [i], "--graph") == 0) {
1185 fprintf (stderr, "error: --graph option requires a method name argument\n");
1190 mono_graph_options = MONO_GRAPH_CFG;
1192 } else if (strcmp (argv [i], "--debug") == 0) {
1193 enable_debugging = TRUE;
1194 } else if (strcmp (argv [i], "--security") == 0) {
1195 mono_security_set_mode (MONO_SECURITY_MODE_CAS);
1196 mono_activate_security_manager ();
1197 } else if (strncmp (argv [i], "--security=", 11) == 0) {
1198 if (strcmp (argv [i] + 11, "temporary-smcs-hack") == 0) {
1199 mono_security_set_mode (MONO_SECURITY_MODE_SMCS_HACK);
1200 } else if (strcmp (argv [i] + 11, "core-clr") == 0) {
1201 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
1202 } else if (strcmp (argv [i] + 11, "core-clr-test") == 0) {
1203 mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
1204 mono_security_core_clr_test = TRUE;
1205 } else if (strcmp (argv [i] + 11, "cas") == 0){
1206 mono_security_set_mode (MONO_SECURITY_MODE_CAS);
1207 mono_activate_security_manager ();
1209 fprintf (stderr, "error: --security= option has invalid argument (cas or core-clr)\n");
1212 } else if (strcmp (argv [i], "--desktop") == 0) {
1213 #if defined (HAVE_BOEHM_GC)
1216 /* Put desktop-specific optimizations here */
1217 } else if (strcmp (argv [i], "--server") == 0){
1218 /* Put server-specific optimizations here */
1219 } else if (strcmp (argv [i], "--inside-mdb") == 0) {
1220 action = DO_DEBUGGER;
1221 } else if (strncmp (argv [i], "--wapi=", 7) == 0) {
1222 if (strcmp (argv [i] + 7, "hps") == 0) {
1223 return mini_wapi_hps (argc - i, argv + i);
1224 } else if (strcmp (argv [i] + 7, "semdel") == 0) {
1225 return mini_wapi_semdel (argc - i, argv + i);
1226 } else if (strcmp (argv [i] + 7, "seminfo") == 0) {
1227 return mini_wapi_seminfo (argc - i, argv + i);
1229 fprintf (stderr, "Invalid --wapi suboption: '%s'\n", argv [i]);
1232 #ifdef MONO_JIT_INFO_TABLE_TEST
1233 } else if (strcmp (argv [i], "--test-jit-info-table") == 0) {
1234 test_jit_info_table = TRUE;
1237 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
1247 if ((action == DO_EXEC) && g_getenv ("MONO_INSIDE_MDB"))
1248 action = DO_DEBUGGER;
1250 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
1251 g_set_prgname (argv[i]);
1254 if (enable_profile) {
1255 /* Needed because of TLS accesses in mono_profiler_load () */
1256 mono_gc_base_init ();
1257 mono_profiler_load (profile_options);
1260 if (trace_options != NULL){
1262 * Need to call this before mini_init () so we can trace methods
1263 * compiled there too.
1265 mono_jit_trace_calls = mono_trace_parse_options (trace_options);
1266 if (mono_jit_trace_calls == NULL)
1270 if (action == DO_DEBUGGER) {
1271 // opt |= MONO_OPT_SHARED;
1272 opt &= ~MONO_OPT_INLINE;
1273 opt &= ~MONO_OPT_COPYPROP;
1274 opt &= ~MONO_OPT_CONSPROP;
1275 enable_debugging = TRUE;
1277 #ifdef MONO_DEBUGGER_SUPPORTED
1278 mono_debug_init (MONO_DEBUG_FORMAT_DEBUGGER);
1279 mono_debugger_init ();
1281 g_print ("The Mono Debugger is not supported on this platform.\n");
1284 } else if (enable_debugging)
1285 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
1287 mono_set_defaults (mini_verbose, opt);
1288 domain = mini_init (argv [i], forced_version);
1292 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
1293 g_print ("Regression ERRORS!\n");
1294 mini_cleanup (domain);
1297 mini_cleanup (domain);
1300 if (argc - i != 1 || mname == NULL) {
1301 g_print ("Usage: mini --ncompile num --compile method assembly\n");
1302 mini_cleanup (domain);
1308 if (argc - i != 1) {
1310 mini_cleanup (domain);
1316 if (argc - i != 1 || mname == NULL) {
1318 mini_cleanup (domain);
1326 mini_cleanup (domain);
1333 /* Parse gac loading options before loading assemblies. */
1334 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
1335 mono_config_parse (config_file);
1338 #ifdef MONO_JIT_INFO_TABLE_TEST
1339 if (test_jit_info_table)
1340 jit_info_table_test (domain);
1343 assembly = mono_assembly_open (aname, NULL);
1345 fprintf (stderr, "Cannot open assembly %s.\n", aname);
1346 mini_cleanup (domain);
1350 if (trace_options != NULL)
1351 mono_trace_set_assembly (assembly);
1353 if (mono_compile_aot || action == DO_EXEC) {
1356 //mono_set_rootdir ();
1358 error = mono_check_corlib_version ();
1360 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
1361 fprintf (stderr, "Loaded from: %s\n",
1362 mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown");
1363 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
1367 #ifdef PLATFORM_WIN32
1368 /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
1369 if (!enable_debugging && !mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
1373 main_args.domain = domain;
1374 main_args.file = aname;
1375 main_args.argc = argc - i;
1376 main_args.argv = argv + i;
1377 main_args.opts = opt;
1378 main_args.aot_options = aot_options;
1379 #if RUN_IN_SUBTHREAD
1380 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
1382 main_thread_handler (&main_args);
1383 mono_thread_manage ();
1385 mini_cleanup (domain);
1387 /* Look up return value from System.Environment.ExitCode */
1388 i = mono_environment_exitcode_get ();
1390 } else if (action == DO_COMPILE) {
1391 compile_all_methods (assembly, mini_verbose, opt);
1392 mini_cleanup (domain);
1394 } else if (action == DO_DEBUGGER) {
1395 #ifdef MONO_DEBUGGER_SUPPORTED
1398 error = mono_check_corlib_version ();
1400 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
1401 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
1405 mono_debugger_main (domain, assembly, argc - i, argv + i);
1406 mini_cleanup (domain);
1412 desc = mono_method_desc_new (mname, 0);
1414 g_print ("Invalid method name %s\n", mname);
1415 mini_cleanup (domain);
1418 method = mono_method_desc_search_in_image (desc, mono_assembly_get_image (assembly));
1420 g_print ("Cannot find method %s\n", mname);
1421 mini_cleanup (domain);
1425 if (action == DO_DRAW) {
1428 switch (mono_graph_options) {
1429 case MONO_GRAPH_DTREE:
1431 opt |= MONO_OPT_LOOP;
1433 case MONO_GRAPH_CFG_CODE:
1436 case MONO_GRAPH_CFG_SSA:
1439 case MONO_GRAPH_CFG_OPTCODE:
1446 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1447 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
1449 nm = mono_marshal_get_native_wrapper (method);
1450 cfg = mini_method_compile (nm, opt, mono_get_root_domain (), FALSE, FALSE, part);
1453 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, part);
1454 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
1455 g_warning ("no SSA info available (use -O=deadce)");
1458 mono_draw_graph (cfg, mono_graph_options);
1459 mono_destroy_compile (cfg);
1461 } else if (action == DO_BENCH) {
1462 if (mini_stats_fd) {
1464 double no_opt_time = 0.0;
1465 GTimer *timer = g_timer_new ();
1466 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
1467 mono_method_full_name (method, TRUE));
1468 fprintf (mini_stats_fd, "@data = (\n");
1469 fprintf (mini_stats_fd, "[");
1470 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1472 n = opt_descr (opt);
1475 fprintf (mini_stats_fd, "\"%s\",", n);
1477 fprintf (mini_stats_fd, "],\n[");
1479 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1483 g_timer_start (timer);
1484 for (j = 0; j < count; ++j) {
1485 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1486 mono_destroy_compile (cfg);
1488 g_timer_stop (timer);
1489 elapsed = g_timer_elapsed (timer, NULL);
1491 no_opt_time = elapsed;
1492 fprintf (mini_stats_fd, "%f, ", elapsed);
1494 fprintf (mini_stats_fd, "]");
1495 if (no_opt_time > 0.0) {
1496 fprintf (mini_stats_fd, ", \n[");
1497 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
1498 fprintf (mini_stats_fd, "%f,", no_opt_time);
1499 fprintf (mini_stats_fd, "]");
1501 fprintf (mini_stats_fd, ");\n");
1503 for (i = 0; i < count; ++i) {
1504 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1505 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1506 method = mono_marshal_get_native_wrapper (method);
1508 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1509 mono_destroy_compile (cfg);
1513 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1514 mono_destroy_compile (cfg);
1517 mini_cleanup (domain);
1522 mono_jit_init (const char *file)
1524 return mini_init (file, NULL);
1528 * mono_jit_init_version:
1529 * @file: the initial assembly to load
1530 * @runtime_version: the version of the runtime to load
1532 * Use this version when you want to force a particular runtime
1533 * version to be used. By default Mono will pick the runtime that is
1534 * referenced by the initial assembly (specified in @file), this
1535 * routine allows programmers to specify the actual runtime to be used
1536 * as the initial runtime is inherited by all future assemblies loaded
1537 * (since Mono does not support having more than one mscorlib runtime
1540 * The @runtime_version can be one of these strings: "v1.1.4322" for
1541 * the 1.1 runtime or "v2.0.50727" for the 2.0 runtime.
1543 * Returns: the MonoDomain representing the domain where the assembly
1547 mono_jit_init_version (const char *file, const char *runtime_version)
1549 return mini_init (file, runtime_version);
1553 mono_jit_cleanup (MonoDomain *domain)
1555 mini_cleanup (domain);