2009-12-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / driver.c
index ebaeda03ce6d270fb750dd1b1b881f6b80810dff..627603588facd2ed05bdab735d02a081239256b4 100644 (file)
 #include <mono/metadata/coree.h>
 #include <mono/metadata/attach.h>
 #include "mono/utils/mono-counters.h"
-#include <mono/os/gc_wrapper.h>
+#include <mono/utils/gc_wrapper.h>
 
 #include "mini.h"
 #include "jit.h"
 #include <string.h>
 #include <ctype.h>
-#include "inssel.h"
 #include <locale.h>
 #include "version.h"
+#include "debugger-agent.h"
 
 static FILE *mini_stats_fd = NULL;
 
 static void mini_usage (void);
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
 /* Need this to determine whether to detach console */
 #include <mono/metadata/cil-coff.h>
 /* This turns off command line globbing under win32 */
@@ -115,13 +115,13 @@ opt_funcs [sizeof (int) * 8] = {
        NULL
 };
 
+
 #define DEFAULT_OPTIMIZATIONS (        \
        MONO_OPT_PEEPHOLE |     \
        MONO_OPT_CFOLD |        \
        MONO_OPT_INLINE |       \
        MONO_OPT_CONSPROP |     \
        MONO_OPT_COPYPROP |     \
-       MONO_OPT_TREEPROP |     \
        MONO_OPT_DEADCE |       \
        MONO_OPT_BRANCH |       \
        MONO_OPT_LINEARS |      \
@@ -130,6 +130,7 @@ opt_funcs [sizeof (int) * 8] = {
        MONO_OPT_EXCEPTION |  \
     MONO_OPT_CMOV |  \
        MONO_OPT_GSHARED |      \
+       MONO_OPT_SIMD | \
        MONO_OPT_AOT)
 
 #define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
@@ -214,6 +215,9 @@ parse_debug_options (const char* p)
                } else if (!strncmp (p, "mdb-optimizations", 17)) {
                        opt->mdb_optimizations = TRUE;
                        p += 17;
+               } else if (!strncmp (p, "gdb", 3)) {
+                       opt->gdb = TRUE;
+                       p += 3;
                } else {
                        fprintf (stderr, "Invalid debug option `%s', use --help-debug for details\n", p);
                        return FALSE;
@@ -296,6 +300,11 @@ opt_sets [] = {
        MONO_OPT_BRANCH,
        MONO_OPT_CFOLD,
        MONO_OPT_FCMOV,
+#ifdef MONO_ARCH_SIMD_INTRINSICS
+       MONO_OPT_SIMD,
+       MONO_OPT_SSE2,
+       MONO_OPT_SIMD | MONO_OPT_SSE2,
+#endif
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
@@ -308,9 +317,9 @@ opt_sets [] = {
        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_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,
        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_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,
        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
+       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,
+       DEFAULT_OPTIMIZATIONS, 
 };
 
 typedef int (*TestMethod) (void);
@@ -324,7 +333,8 @@ domain_dump_native_code (MonoDomain *domain) {
 #endif
 
 static int
-mini_regression (MonoImage *image, int verbose, int *total_run) {
+mini_regression (MonoImage *image, int verbose, int *total_run)
+{
        guint32 i, opt, opt_flags;
        MonoMethod *method;
        MonoCompile *cfg;
@@ -332,6 +342,10 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
        int result, expected, failed, cfailed, run, code_size, total;
        TestMethod func;
        GTimer *timer = g_timer_new ();
+       MonoDomain *domain = mono_domain_get ();
+       guint32 exclude = 0;
+
+       mono_arch_cpu_optimizazions (&exclude);
 
        if (mini_stats_fd) {
                fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
@@ -356,7 +370,9 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
 
        /* load the metadata */
        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 = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
+               if (!method)
+                       continue;
                mono_class_init (method->klass);
 
                if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
@@ -372,7 +388,7 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
        for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
                double elapsed, comp_time, start_time;
 
-               opt_flags = opt_sets [opt];
+               opt_flags = opt_sets [opt] & ~exclude;
                mono_set_defaults (verbose, opt_flags);
                n = opt_descr (opt_flags);
                g_print ("Test run: image=%s, opts=%s\n", mono_image_get_filename (image), n);
@@ -381,16 +397,18 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
                comp_time = elapsed = 0.0;
 
                /* fixme: ugly hack - delete all previously compiled methods */
-               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);
-               mono_internal_hash_table_destroy (&(mono_domain_get ()->jit_code_hash));
-               mono_jit_code_hash_init (&(mono_domain_get ()->jit_code_hash));
+               g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
+               domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+               mono_internal_hash_table_destroy (&(domain->jit_code_hash));
+               mono_jit_code_hash_init (&(domain->jit_code_hash));
 
                g_timer_start (timer);
                if (mini_stats_fd)
                        fprintf (mini_stats_fd, "[");
                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 = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
+                       if (!method)
+                               continue;
                        if (strncmp (method->name, "test_", 5) == 0) {
                                expected = atoi (method->name + 5);
                                run++;
@@ -830,6 +848,8 @@ compile_all_methods_thread_main (CompileAllThreadArgs *args)
                        continue;
 
                method = mono_get_method (image, token, NULL);
+               if (!method)
+                       continue;
                if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
                    (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
                    (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
@@ -935,6 +955,17 @@ static void main_thread_handler (gpointer user_data)
                                fprintf (stderr, "Can not open image %s\n", main_args->argv [i]);
                                exit (1);
                        }
+                       /* Check that the assembly loaded matches the filename */
+                       {
+                               MonoImageOpenStatus status;
+                               MonoImage *img;
+
+                               img = mono_image_open (main_args->argv [i], &status);
+                               if (img && strcmp (img->name, assembly->image->name)) {
+                                       fprintf (stderr, "Error: Loaded assembly '%s' doesn't match original file name '%s'. Set MONO_PATH to the assembly's location.\n", assembly->image->name, img->name);
+                                       exit (1);
+                               }
+                       }
                        res = mono_compile_assembly (assembly, main_args->opts, main_args->aot_options);
                        if (res != 0) {
                                fprintf (stderr, "AOT of image %s failed.\n", main_args->argv [i]);
@@ -1047,6 +1078,7 @@ mini_usage_jitdeveloper (void)
                 "    --verify-all           Run the verifier on all methods\n"
                 "    --full-aot             Avoid JITting any code\n"
                 "    --agent=ASSEMBLY[:ARG] Loads the specific agent assembly and executes its Main method with the given argument before loading the main assembly.\n"
+                "    --no-x86-stack-align   Don't align stack on x86\n"
                 "\n"
                 "Other options:\n" 
                 "    --graph[=TYPE] METHOD  Draws a graph of the specified method:\n");
@@ -1074,6 +1106,7 @@ mini_usage (void)
                "Development:\n"
                "    --aot                  Compiles the assembly to native code\n"
                "    --debug[=<options>]    Enable debugging support, use --help-debug for details\n"
+               "    --debugger-agent=options Enable the debugger agent\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"
@@ -1090,6 +1123,7 @@ mini_usage (void)
                "                           mode is one of cas, core-clr, verifiable or validil\n"
                "    --attach=OPTIONS       Pass OPTIONS to the attach agent in the runtime.\n"
                "                           Currently the only supported option is 'disable'.\n"
+               "    --llvm                 Controls whenever the runtime uses LLVM compiled code.\n"
          );
 }
 
@@ -1108,8 +1142,9 @@ mini_trace_usage (void)
                 "    M:Type:Method        Specifies a method\n"
                 "    N:Namespace          Specifies a namespace\n"
                 "    T:Type               Specifies a type\n"
-                "    +EXPR                Includes expression\n"
+                "    EXPR                 Includes expression\n"
                 "    -EXPR                Excludes expression\n"
+                "    EXPR,EXPR            Multiple expressions\n"
                 "    disabled             Don't print any output until toggled via SIGUSR2\n");
 }
 
@@ -1129,14 +1164,10 @@ mini_debug_usage (void)
                 "                         process with the debugger.\n");
 }
 
-#if defined(__arm__) && defined(__ARM_EABI__)
+#if defined(MONO_ARCH_ARCHITECTURE)
 /* Redefine ARCHITECTURE to include more information */
 #undef ARCHITECTURE
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
-#define ARCHITECTURE "armel"
-#else
-#define ARCHITECTURE "armeb"
-#endif
+#define ARCHITECTURE MONO_ARCH_ARCHITECTURE
 #endif
 
 static const char info[] =
@@ -1166,7 +1197,7 @@ static const char info[] =
 #define error_if_aot_unsupported()
 #endif
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
 BOOL APIENTRY DllMain (HMODULE module_handle, DWORD reason, LPVOID reserved)
 {
        if (!GC_DllMain (module_handle, reason, reserved))
@@ -1186,6 +1217,41 @@ BOOL APIENTRY DllMain (HMODULE module_handle, DWORD reason, LPVOID reserved)
 }
 #endif
 
+static gboolean enable_debugging;
+
+/*
+ * mono_jit_parse_options:
+ *
+ *   Process the command line options in ARGV as done by the runtime executable. 
+ * This should be called before mono_jit_init ().
+ */
+void
+mono_jit_parse_options (int argc, char * argv[])
+{
+       int i;
+
+       /* 
+        * Some options have no effect here, since they influence the behavior of 
+        * mono_main ().
+        */
+
+       /* FIXME: Avoid code duplication */
+       for (i = 0; i < argc; ++i) {
+               if (argv [i] [0] != '-')
+                       break;
+               if (strncmp (argv [i], "--debugger-agent=", 17) == 0) {
+                       MonoDebugOptions *opt = mini_get_debug_options ();
+
+                       mono_debugger_agent_parse_options (argv [i] + 17);
+                       opt->mdb_optimizations = TRUE;
+                       enable_debugging = TRUE;
+               } else {
+                       fprintf (stderr, "Unsupported command line option: '%s'\n", argv [i]);
+                       exit (1);
+               }
+       }
+}
+
 int
 mono_main (int argc, char* argv[])
 {
@@ -1199,7 +1265,6 @@ mono_main (int argc, char* argv[])
        const char* aname, *mname = NULL;
        char *config_file = NULL;
        int i, count = 1;
-       int enable_debugging = FALSE;
        guint32 opt, action = DO_EXEC;
        MonoGraphOptions mono_graph_options = 0;
        int mini_verbose = 0;
@@ -1254,7 +1319,9 @@ 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 (%s)\nCopyright (C) 2002-2008 Novell, Inc and Contributors. www.mono-project.com\n", VERSION, FULL_VERSION);
+                       char *build = mono_get_runtime_build_info ();
+                       g_print ("Mono JIT compiler version %s (%s)\nCopyright (C) 2002-2009 Novell, Inc and Contributors. www.mono-project.com\n", VERSION, build);
+                       g_free (build);
                        g_print (info);
                        if (mini_verbose) {
                                const char *cerror;
@@ -1411,6 +1478,12 @@ mono_main (int argc, char* argv[])
                        enable_debugging = TRUE;
                        if (!parse_debug_options (argv [i] + 8))
                                return 1;
+               } else if (strncmp (argv [i], "--debugger-agent=", 17) == 0) {
+                       MonoDebugOptions *opt = mini_get_debug_options ();
+
+                       mono_debugger_agent_parse_options (argv [i] + 17);
+                       opt->mdb_optimizations = TRUE;
+                       enable_debugging = TRUE;
                } else if (strcmp (argv [i], "--security") == 0) {
                        mono_verifier_set_mode (MONO_VERIFIER_MODE_VERIFIABLE);
                        mono_security_set_mode (MONO_SECURITY_MODE_CAS);
@@ -1457,9 +1530,18 @@ mono_main (int argc, char* argv[])
                                fprintf (stderr, "Invalid --wapi suboption: '%s'\n", argv [i]);
                                return 1;
                        }
+               } else if (strcmp (argv [i], "--no-x86-stack-align") == 0) {
+                       mono_do_x86_stack_align = FALSE;
 #ifdef MONO_JIT_INFO_TABLE_TEST
                } else if (strcmp (argv [i], "--test-jit-info-table") == 0) {
                        test_jit_info_table = TRUE;
+#endif
+               } else if (strcmp (argv [i], "--llvm") == 0) {
+#ifndef MONO_ARCH_LLVM_SUPPORTED
+                       fprintf (stderr, "--llvm not supported on this platform.\n");
+                       return 1;
+#else
+                       mono_use_llvm = TRUE;
 #endif
                } else {
                        fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
@@ -1472,6 +1554,20 @@ mono_main (int argc, char* argv[])
                return 1;
        }
 
+       if (getenv ("MONO_XDEBUG"))
+               enable_debugging = TRUE;
+
+#ifdef MONO_CROSS_COMPILE
+       if (!mono_compile_aot) {
+                  fprintf (stderr, "This mono runtime is compiled for cross-compiling. Only the --aot option is supported.\n");
+                  exit (1);
+       }
+#if SIZEOF_VOID_P == 8 && defined(TARGET_ARM)
+       fprintf (stderr, "Can't cross-compile on 64 bit platforms to arm.\n");
+       exit (1);
+#endif
+#endif
+
        if ((action == DO_EXEC) && mono_debug_using_mono_debugger ())
                action = DO_DEBUGGER;
 
@@ -1479,11 +1575,8 @@ mono_main (int argc, char* argv[])
                g_set_prgname (argv[i]);
        }
 
-       if (enable_profile) {
-               /* Needed because of TLS accesses in mono_profiler_load () */
-               mono_gc_base_init ();
+       if (enable_profile)
                mono_profiler_load (profile_options);
-       }
 
        mono_attach_parse_options (attach_options);
 
@@ -1509,7 +1602,6 @@ mono_main (int argc, char* argv[])
 
 #ifdef MONO_DEBUGGER_SUPPORTED
                mono_debug_init (MONO_DEBUG_FORMAT_DEBUGGER);
-               mono_debugger_init ();
 #else
                g_print ("The Mono Debugger is not supported on this platform.\n");
                return 1;
@@ -1517,6 +1609,13 @@ mono_main (int argc, char* argv[])
        } else if (enable_debugging)
                mono_debug_init (MONO_DEBUG_FORMAT_MONO);
 
+#ifdef MONO_DEBUGGER_SUPPORTED
+       if (enable_debugging) {
+               if ((opt & MONO_OPT_GSHARED) == 0)
+                       mini_debugger_set_attach_ok ();
+       }
+#endif
+
        mono_set_defaults (mini_verbose, opt);
        mono_setup_vtable_in_class_init = FALSE;
        domain = mini_init (argv [i], forced_version);
@@ -1613,7 +1712,7 @@ mono_main (int argc, char* argv[])
                        exit (1);
                }
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
                /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
                if (!enable_debugging && !mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
                        FreeConsole ();
@@ -1631,6 +1730,28 @@ mono_main (int argc, char* argv[])
                main_thread_handler (&main_args);
                mono_thread_manage ();
 #endif
+
+       /* 
+        * On unix, WaitForMultipleObjects for threads is implemented by waiting on
+        * a cond variable, which is set by the thread when it exits _mono code_, 
+        * but it could still be running libc code. On amd64, the libc thread exit 
+        * code does a stack unwind, and if it encounters a frame pointing to native
+        * code which is in memory which is no longer mapped (because the runtime has
+        * shut down), it will crash:
+        * http://mail-archives.apache.org/mod_mbox/harmony-dev/200801.mbox/%3C200801130327.41572.gshimansky@apache.org%3E
+        * Testcase: tests/main-exit-background-change.exe.
+        * To make this race less frequent, we avoid freeing the global code manager.
+        * Since mono_main () is hopefully only used by the runtime executable, this 
+        * will only cause a shutdown leak. This workaround also has the advantage
+        * that it can be back-ported to 2.0 safely.
+        * FIXME: Fix this properly by waiting for threads to really exit using 
+        * pthread_join (). This cannot be done currently as the io-layer calls
+        * pthread_detach ().
+        */
+#ifdef __x86_64__
+               mono_dont_free_global_codeman = TRUE;
+#endif
+
                mini_cleanup (domain);
 
                /* Look up return value from System.Environment.ExitCode */
@@ -1651,7 +1772,7 @@ mono_main (int argc, char* argv[])
                        exit (1);
                }
 
-               mono_debugger_main (domain, assembly, argc - i, argv + i);
+               mini_debugger_main (domain, assembly, argc - i, argv + i);
                mini_cleanup (domain);
                return 0;
 #else
@@ -1804,6 +1925,12 @@ mono_jit_cleanup (MonoDomain *domain)
        mini_cleanup (domain);
 }
 
+void
+mono_jit_set_aot_only (gboolean val)
+{
+       mono_aot_only = val;
+}
+
 /**
  * mono_jit_set_trace_options:
  * @options: string representing the trace options
@@ -1823,3 +1950,21 @@ mono_jit_set_trace_options (const char* options)
        return TRUE;
 }
 
+/**
+ * mono_set_signal_chaining:
+ *
+ *   Enable/disable signal chaining. This should be called before mono_jit_init ().
+ * If signal chaining is enabled, the runtime saves the original signal handlers before
+ * installing its own handlers, and calls the original ones in the following cases:
+ * - a SIGSEGV/SIGABRT signal received while executing native (i.e. not JITted) code.
+ * - SIGPROF
+ * - SIGFPE
+ * - SIGQUIT
+ * - SIGUSR2
+ * Signal chaining only works on POSIX platforms.
+ */
+void
+mono_set_signal_chaining (gboolean chain_signals)
+{
+       mono_do_signal_chaining = chain_signals;
+}