2005-07-21 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / mini / driver.c
index 0f244e000185b4d61367e62148903cc895488d59..08dbd2f6c61338870b55a21e00db35f7e5b3abec 100644 (file)
@@ -37,6 +37,7 @@
 #include <mono/metadata/verify.h>
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/mono-debug-debugger.h>
+#include <mono/metadata/security-manager.h>
 #include <mono/os/gc_wrapper.h>
 
 #include "mini.h"
@@ -50,6 +51,11 @@ static FILE *mini_stats_fd = NULL;
 
 static void mini_usage (void);
 
+/* This turns off command line globbing under win32 */
+#ifdef PLATFORM_WIN32
+int _CRT_glob = 0;
+#endif
+
 typedef void (*OptFunc) (const char *p);
 
 /* keep in sync with enum in mini.h */
@@ -92,8 +98,7 @@ opt_names [] = {
        MONO_OPT_LOOP |  \
        MONO_OPT_AOT)
 
-/* SSAPRE does not work correctly on non x86 platforms (bug #70637) */
-#define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP | MONO_OPT_SSAPRE)
+#define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
 
 static guint32
 parse_optimizations (const char* p)
@@ -230,9 +235,7 @@ opt_sets [] = {
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
        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_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,
-          /* SSAPRE does not work correctly on non x86 platforms (bug #70637)
        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,
-          */
        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
 };
 
@@ -305,10 +308,10 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
                comp_time = elapsed = 0.0;
 
                /* fixme: ugly hack - delete all previously compiled methods */
-               for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
-                       method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
-                       method->info = NULL;
-               }
+               g_hash_table_destroy (mono_domain_get ()->jit_trampoline_hash);
+               mono_domain_get ()->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+               g_hash_table_destroy (mono_domain_get ()->jit_code_hash);
+               mono_domain_get ()->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
                g_timer_start (timer);
                if (mini_stats_fd)
@@ -334,8 +337,7 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
                                        result = func ();
                                        if (result != expected) {
                                                failed++;
-                                               if (verbose)
-                                                       g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
+                                               g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
                                        }
                                        code_size += cfg->code_len;
                                        mono_destroy_compile (cfg);
@@ -412,13 +414,27 @@ compile_all_methods_thread_main (CompileAllThreadArgs *args)
        int verbose = args->verbose;
        MonoImage *image = mono_assembly_get_image (ass);
        MonoMethod *method;
+       MonoCompile *cfg;
        int i, count = 0;
 
        for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
-               method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
-               if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
+               guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
+               MonoMethodSignature *sig;
+
+               if (mono_metadata_has_generic_params (image, token))
+                       continue;
+
+               method = mono_get_method (image, token, NULL);
+               if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
+                   (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
+                   (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
+                   (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
+                       continue;
+
+               if (method->klass->generic_container)
                        continue;
-               if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
+               sig = mono_method_signature (method);
+               if (sig->has_type_parameters)
                        continue;
 
                count++;
@@ -427,7 +443,8 @@ compile_all_methods_thread_main (CompileAllThreadArgs *args)
                        g_print ("Compiling %d %s\n", count, desc);
                        g_free (desc);
                }
-               mono_compile_method (method);
+               cfg = mini_method_compile (method, DEFAULT_OPTIMIZATIONS, mono_get_root_domain (), FALSE, FALSE, 0);
+               mono_destroy_compile (cfg);
        }
 
 }
@@ -513,47 +530,59 @@ static void main_thread_handler (gpointer user_data)
 }
 
 static void
-mini_usage (void)
+mini_usage_jitdeveloper (void)
 {
        int i;
-
+       
        fprintf (stdout,
-               "Usage is: mono [options] assembly\n\n"
-               "Runtime and JIT debugging:\n"
-               "    --compile METHOD       Just compile METHOD in assembly\n"
-               "    --ncompile N           Number of times to compile METHOD (default: 1)\n"
-               "    --regression           Runs the regression test contained in the assembly\n"
-               "    --print-vtable         Print the vtable of all used classes\n"
-               "    --trace[=EXPR]         Enable tracing, use --help-trace for details\n"
-               "    --compile-all          Compiles all the methods in the assembly\n"
-               "    --breakonex            Inserts a breakpoint on exceptions\n"
-               "    --break METHOD         Inserts a breakpoint at METHOD entry\n"
-               "    --debug                Enable debugging support\n"
-               "    --stats                Print statistics about the JIT operations\n"
-               "\n"
-               "Development:\n"
-               "    --statfile FILE        Sets the stat file to FILE\n"
-               "    --aot                  Compiles the assembly to native code\n"
-               "    --profile[=profiler]   Runs in profiling mode with the specified profiler module\n"
-               "    --graph[=TYPE] METHOD  Draws a graph of the specified method:\n");
+                "Runtime and JIT debugging options:\n"
+                "    --breakonex            Inserts a breakpoint on exceptions\n"
+                "    --break METHOD         Inserts a breakpoint at METHOD entry\n"
+                "    --compile METHOD       Just compile METHOD in assembly\n"
+                "    --compile-all          Compiles all the methods in the assembly\n"
+                "    --ncompile N           Number of times to compile METHOD (default: 1)\n"
+                "    --print-vtable         Print the vtable of all used classes\n"
+                "    --regression           Runs the regression test contained in the assembly\n"
+                "    --statfile FILE        Sets the stat file to FILE\n"
+                "    --stats                Print statistics about the JIT operations\n"
+                "\n"
+                "Other options:\n" 
+                "    --graph[=TYPE] METHOD  Draws a graph of the specified method:\n");
        
        for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
                fprintf (stdout, "                           %-10s %s\n", graph_names [i].name, graph_names [i].desc);
        }
+}
+
+static void
+mini_usage_list_opt (void)
+{
+       int i;
+       
+       for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
+               fprintf (stdout, "                           %-10s %s\n", opt_names [i].name, opt_names [i].desc);
+}
 
+static void
+mini_usage (void)
+{
        fprintf (stdout,
+               "Usage is: mono [options] program [program-options]\n"
+               "\n"
+               "Development:\n"
+               "    --aot                  Compiles the assembly to native code\n"
+               "    --debug                Enable debugging support\n"
+               "    --profile[=profiler]   Runs in profiling mode with the specified profiler module\n"
+               "    --trace[=EXPR]         Enable tracing, use --help-trace for details\n"
+               "    --help-devel           Shows more options available to developers\n"
                "\n"
                "Runtime:\n"
                "    --config FILE          Loads FILE as the Mono config\n"
                "    --verbose, -v          Increases the verbosity level\n"
                "    --help, -h             Show usage information\n"
                "    --version, -V          Show version information\n"
-               "    --optimize=OPT         Turns on a specific optimization:\n");
-
-       for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
-               fprintf (stdout, "                           %-10s %s\n", opt_names [i].name, opt_names [i].desc);
-
-       fprintf (stdout,
+               "    --optimize=OPT         Turns on or off a specific optimization\n"
+               "                           Use --list-opt to get a list of optimizations\n"
                "    --security             Turns on the security manager (unsupported, default is off)\n");
 }
 
@@ -573,7 +602,8 @@ mini_trace_usage (void)
                 "    N:Namespace          Specifies a namespace\n"
                 "    T:Type               Specifies a type\n"
                 "    +EXPR                Includes expression\n"
-                "    -EXPR                Excludes expression\n");
+                "    -EXPR                Excludes expression\n"
+                "    disabled             Don't print any output until toggled via SIGUSR2\n");
 }
 
 static const char *info = ""
@@ -601,9 +631,9 @@ static const char *info = ""
     "\tSIGSEGV      : normal\n"
 #endif
 #ifdef HAVE_ICU
-       "\tGlobalization: ICU\n"
+       "\tGlobalization: ICU (unsupported, don't use)\n"
 #else
-       "\tGlobalization: none\n"
+       "\tGlobalization: normal\n"
 #endif /* HAVE_ICU */
        "";
 
@@ -630,6 +660,22 @@ mono_main (int argc, char* argv[])
 
        setlocale (LC_ALL, "");
 
+       if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
+               GMemVTable mem_vtable;
+
+               /* 
+                * Instruct glib to use the system allocation functions so valgrind
+                * can track the memory allocated by the g_... functions.
+                */
+               memset (&mem_vtable, 0, sizeof (mem_vtable));
+               mem_vtable.malloc = malloc;
+               mem_vtable.realloc = realloc;
+               mem_vtable.free = free;
+               mem_vtable.calloc = calloc;
+
+               g_mem_set_vtable (&mem_vtable);
+       }
+
        g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
        g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
 
@@ -643,11 +689,11 @@ mono_main (int argc, char* argv[])
                } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
                        mini_verbose++;
                } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
-                       g_print ("Mono JIT compiler version %s, (C) 2002-2004 Novell, Inc and Contributors. www.go-mono.com\n", VERSION);
+                       g_print ("Mono JIT compiler version %s, (C) 2002-2005 Novell, Inc and Contributors. www.mono-project.com\n", VERSION);
                        g_print (info);
                        if (mini_verbose) {
-                               const guchar *cerror;
-                               const guchar *clibpath;
+                               const char *cerror;
+                               const char *clibpath;
                                mono_init ("mono");
                                cerror = mono_check_corlib_version ();
                                clibpath = mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown";
@@ -665,15 +711,33 @@ mono_main (int argc, char* argv[])
                } else if (strcmp (argv [i], "--help-trace") == 0){
                        mini_trace_usage ();
                        return 0;
+               } else if (strcmp (argv [i], "--help-devel") == 0){
+                       mini_usage_jitdeveloper ();
+                       return 0;
+               } else if (strcmp (argv [i], "--list-opt") == 0){
+                       mini_usage_list_opt ();
+                       return 0;
                } else if (strncmp (argv [i], "--statfile", 10) == 0) {
+                       if (i + 1 >= argc){
+                               fprintf (stderr, "error: --statfile requires a filename argument\n");
+                               return 1;
+                       }
                        mini_stats_fd = fopen (argv [++i], "w+");
                } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
                        opt = parse_optimizations (argv [i] + 11);
                } else if (strncmp (argv [i], "-O=", 3) == 0) {
                        opt = parse_optimizations (argv [i] + 3);
                } else if (strcmp (argv [i], "--config") == 0) {
+                       if (i +1 >= argc){
+                               fprintf (stderr, "error: --config requires a filename argument\n");
+                               return 1;
+                       }
                        config_file = argv [++i];
                } else if (strcmp (argv [i], "--ncompile") == 0) {
+                       if (i + 1 >= argc){
+                               fprintf (stderr, "error: --ncompile requires an argument\n");
+                               return 1;
+                       }
                        count = atoi (argv [++i]);
                        action = DO_BENCH;
                } else if (strcmp (argv [i], "--trace") == 0) {
@@ -683,8 +747,13 @@ mono_main (int argc, char* argv[])
                } else if (strcmp (argv [i], "--breakonex") == 0) {
                        mono_break_on_exc = TRUE;
                } else if (strcmp (argv [i], "--break") == 0) {
+                       if (i+1 >= argc){
+                               fprintf (stderr, "Missing method name in --break command line option\n");
+                               return 1;
+                       }
+                       
                        if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
-                               g_error ("Invalid method name '%s'", argv [i]);
+                               fprintf (stderr, "Error: invalid method name '%s'\n", argv [i]);
                } else if (strcmp (argv [i], "--print-vtable") == 0) {
                        mono_print_vtable = TRUE;
                } else if (strcmp (argv [i], "--stats") == 0) {
@@ -706,13 +775,28 @@ mono_main (int argc, char* argv[])
                        enable_profile = TRUE;
                        profile_options = argv [i] + 10;
                } else if (strcmp (argv [i], "--compile") == 0) {
+                       if (i + 1 >= argc){
+                               fprintf (stderr, "error: --compile option requires a method name argument\n");
+                               return 1;
+                       }
+                       
                        mname = argv [++i];
                        action = DO_BENCH;
                } else if (strncmp (argv [i], "--graph=", 8) == 0) {
+                       if (i + 1 >= argc){
+                               fprintf (stderr, "error: --graph option requires a method name argument\n");
+                               return 1;
+                       }
+                       
                        mono_graph_options = mono_parse_graph_options (argv [i] + 8);
                        mname = argv [++i];
                        action = DO_DRAW;
                } else if (strcmp (argv [i], "--graph") == 0) {
+                       if (i + 1 >= argc){
+                               fprintf (stderr, "error: --graph option requires a method name argument\n");
+                               return 1;
+                       }
+                       
                        mname = argv [++i];
                        mono_graph_options = MONO_GRAPH_CFG;
                        action = DO_DRAW;
@@ -720,6 +804,7 @@ mono_main (int argc, char* argv[])
                        enable_debugging = TRUE;
                } else if (strcmp (argv [i], "--security") == 0) {
                        mono_use_security_manager = TRUE;
+                       mono_activate_security_manager ();
                } else {
                        fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
                        return 1;
@@ -751,11 +836,6 @@ mono_main (int argc, char* argv[])
                        exit (1);
        }
 
-       if (mono_compile_aot && mono_use_security_manager) {
-               mono_use_security_manager = FALSE;
-               g_warning ("Current security manager implementation isn't compatible with AOT - disabling security manager.");
-       }
-
        mono_set_defaults (mini_verbose, opt);
        domain = mini_init (argv [i]);
        
@@ -826,7 +906,7 @@ mono_main (int argc, char* argv[])
                mono_debug_init_2 (assembly);
 
        if (mono_compile_aot || action == DO_EXEC) {
-               const guchar *error;
+               const char *error;
 
                //mono_set_rootdir ();
 
@@ -837,6 +917,12 @@ mono_main (int argc, char* argv[])
                        exit (1);
                }
 
+#ifdef PLATFORM_WIN32
+               /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
+               if (!mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
+                       FreeConsole ();
+#endif
+
                main_args.domain = domain;
                main_args.file = aname;         
                main_args.argc = argc - i;