示例#1
0
文件: Program.cs 项目: Orvid/Cosmos
    private static void DoScan()
    {

        var xSW = new Stopwatch();
        xSW.Start();
        string MDFFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.mdf";
        if (File.Exists(MDFFile))
            File.Delete(MDFFile);

        var outFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.out";
        if (File.Exists(outFile))
            File.Delete(outFile);

        var logFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.log";
        if (File.Exists(logFile))
            File.Delete(logFile);

        var xAsmblr = new AppAssembler(1, "Cosmos.Assembler.Log");
        using (var xScanner = new ILScanner(xAsmblr))
        {
            xScanner.LogException = (Exception e) =>
            {
                Console.WriteLine("ILScanner exception : " + e.Message);
            };
            using (var xDebugInfo = new DebugInfo(MDFFile, true, true))
            {
                xAsmblr.DebugInfo = xDebugInfo;
                xAsmblr.DebugEnabled = true;
                xAsmblr.DebugMode = DebugMode.Source;
                xAsmblr.TraceAssemblies = TraceAssemblies.All;
                xAsmblr.IgnoreDebugStubAttribute = false;

                xAsmblr.Assembler.Initialize();
                //TODO: Add plugs into the scanning equation to profile scanning them too
                //System.Reflection.MethodInfo[] name = typeof(SSchockeTest.Kernel).GetMethods();
                Type xFoundType = typeof(FakeKernel);
                var xCtor = xFoundType.GetConstructor(Type.EmptyTypes);
                typeof(Cosmos.System.Plugs.System.ConsoleImpl).IsSubclassOf(typeof(object));
                var xEntryPoint = typeof(Kernel).GetMethod("Start", BindingFlags.Public | BindingFlags.Instance);
                //var xEntryPoint = typeof(Program).GetMethod("ScannerEntryPoint", BindingFlags.NonPublic | BindingFlags.Static);
                //EnableLogging(pathToLogFile)
                xScanner.EnableLogging(logFile);
                //xScanner.TempDebug += new Action<string>(xScanner_TempDebug);
                //xScanner.
                xScanner.QueueMethod(xEntryPoint);
                xScanner.Execute(xCtor);
                using (var xOut = new StreamWriter(outFile, false))
                {
                    //if (EmitDebugSymbols) {
                    xAsmblr.Assembler.FlushText(xOut);
                    xAsmblr.FinalizeDebugInfo();
                }
                xSW.Stop();
                Console.WriteLine("Total time : {0}", xSW.Elapsed);
                Console.WriteLine("Method count: {0}", xScanner.MethodCount);
                //Console.WriteLine("Instruction count: {0}", xScanner.InstructionCount);
            }
        }
    }
示例#2
0
        public bool Execute()
        {
            try
            {
                LogMessage("Executing IL2CPU on assembly");
                if (!Initialize())
                {
                    return(false);
                }
                LogTime("Engine execute started");

                // Find the kernel's entry point. We are looking for a public class Kernel, with public static void Boot()
                var xInitMethod = LoadAssemblies();
                if (xInitMethod == null)
                {
                    return(false);
                }

                var xOutputFilename = Path.Combine(Path.GetDirectoryName(OutputFilename), Path.GetFileNameWithoutExtension(OutputFilename));
                if (!DebugEnabled)
                {
                    // Default of 1 is in Cosmos.Targets. Need to change to use proj props.
                    DebugCom = 0;
                }

                using (var xAsm = new AppAssembler(DebugCom))
                {
                    using (var xDebugInfo = new DebugInfo(xOutputFilename + ".cdb", true))
                    {
                        xAsm.DebugInfo                = xDebugInfo;
                        xAsm.DebugEnabled             = DebugEnabled;
                        xAsm.StackCorruptionDetection = StackCorruptionDetectionEnabled;
                        xAsm.DebugMode                = mDebugMode;
                        xAsm.TraceAssemblies          = mTraceAssemblies;
                        xAsm.IgnoreDebugStubAttribute = IgnoreDebugStubAttribute;
                        if (DebugEnabled == false)
                        {
                            xAsm.ShouldOptimize = true;
                        }

                        xAsm.Assembler.Initialize();
                        using (var xScanner = new ILScanner(xAsm))
                        {
                            xScanner.LogException = LogException;
                            xScanner.TempDebug   += x => LogMessage(x);
                            if (EnableLogging)
                            {
                                var xLogFile = xOutputFilename + ".log.html";
                                if (false == xScanner.EnableLogging(xLogFile))
                                {
                                    // file creation not possible
                                    EnableLogging = false;
                                    LogWarning("Could not create the file \"" + xLogFile + "\"! No log will be created!");
                                }
                            }
                            xScanner.QueueMethod(xInitMethod.DeclaringType.BaseType.GetMethod("Start"));
                            xScanner.Execute(xInitMethod);

                            AppAssemblerRingsCheck.Execute(xScanner);

                            using (var xOut = new StreamWriter(OutputFilename, false, Encoding.ASCII, 128 * 1024))
                            {
                                //if (EmitDebugSymbols) {
                                xAsm.Assembler.FlushText(xOut);
                                xAsm.FinalizeDebugInfo();
                                //// for now: write debug info to console
                                //Console.WriteLine("Wrote {0} instructions and {1} datamembers", xAsm.Assembler.Instructions.Count, xAsm.Assembler.DataMembers.Count);
                                //var dict = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);
                                //foreach (var instr in xAsm.Assembler.Instructions)
                                //{
                                //    var mn = instr.Mnemonic ?? "";
                                //    if (dict.ContainsKey(mn))
                                //    {
                                //        dict[mn] = dict[mn] + 1;
                                //    }
                                //    else
                                //    {
                                //        dict[mn] = 1;
                                //    }
                                //}
                                //foreach (var entry in dict)
                                //{
                                //    Console.WriteLine("{0}|{1}", entry.Key, entry.Value);
                                //}
                            }
                        }
                        // If you want to uncomment this line make sure to enable PERSISTANCE_PROFILING symbol in
                        // DebugInfo.cs file.
                        //LogMessage(string.Format("DebugInfo flatening {0} seconds, persistance : {1} seconds",
                        //    (int)xDebugInfo.FlateningDuration.TotalSeconds,
                        //    (int)xDebugInfo.PersistanceDuration.TotalSeconds));
                    }
                }
                LogTime("Engine execute finished");
                return(true);
            }
            catch (Exception ex)
            {
                LogException(ex);
                LogMessage("Loaded assemblies: ");
                foreach (var xAsm in AppDomain.CurrentDomain.GetAssemblies())
                {
                    // HACK: find another way to skip dynamic assemblies (which belong to dynamic methods)
                    try
                    {
                        LogMessage(xAsm.Location);
                    }
                    catch
                    {
                    }
                }
                return(false);
            }
        }
示例#3
0
        private static void DoScan()
        {
            var xSW = new Stopwatch();

            xSW.Start();
            string MDFFile = AppContext.BaseDirectory + "TestKernel.mdf";

            if (File.Exists(MDFFile))
            {
                File.Delete(MDFFile);
            }

            var outFile = AppContext.BaseDirectory + "TestKernel.out";

            if (File.Exists(outFile))
            {
                File.Delete(outFile);
            }

            var logFile = AppContext.BaseDirectory + "TestKernel.log";

            if (File.Exists(logFile))
            {
                File.Delete(logFile);
            }

            var xAsmblr = new AppAssembler(1, "Cosmos.Assembler.Log");

            using (var xScanner = new ILScanner(xAsmblr))
            {
                xScanner.LogException = (Exception e) =>
                {
                    Console.WriteLine("ILScanner exception : " + e.Message);
                };
                using (var xDebugInfo = new DebugInfo(MDFFile, true, true))
                {
                    xAsmblr.DebugInfo                = xDebugInfo;
                    xAsmblr.DebugEnabled             = true;
                    xAsmblr.DebugMode                = DebugMode.Source;
                    xAsmblr.TraceAssemblies          = TraceAssemblies.All;
                    xAsmblr.IgnoreDebugStubAttribute = false;

                    xAsmblr.Assembler.Initialize();
                    //TODO: Add plugs into the scanning equation to profile scanning them too
                    Type xFoundType  = typeof(FakeKernel);
                    var  xCtor       = xFoundType.GetConstructor(Type.EmptyTypes);
                    var  xEntryPoint = typeof(Kernel).GetMethod("Start", BindingFlags.Public | BindingFlags.Instance);
                    xScanner.EnableLogging(logFile);
                    xScanner.QueueMethod(xEntryPoint);
                    xScanner.Execute(xCtor);
                    using (var xOut = new StreamWriter(File.OpenWrite(outFile)))
                    {
                        xAsmblr.Assembler.FlushText(xOut);
                        xAsmblr.FinalizeDebugInfo();
                    }
                    xSW.Stop();
                    Console.WriteLine("Total time : {0}", xSW.Elapsed);
                    Console.WriteLine("Method count: {0}", xScanner.MethodCount);
                    //Console.WriteLine("Instruction count: {0}", xScanner.InstructionCount);
                }
            }
        }
示例#4
0
        private static void DoScan()
        {
            var xSW = new Stopwatch();

            xSW.Start();
            string MDFFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.mdf";

            if (File.Exists(MDFFile))
            {
                File.Delete(MDFFile);
            }

            var outFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.out";

            if (File.Exists(outFile))
            {
                File.Delete(outFile);
            }

            var logFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.log";

            if (File.Exists(logFile))
            {
                File.Delete(logFile);
            }

            var xAsmblr = new AppAssembler(1, "Cosmos.Assembler.Log");

            using (var xScanner = new ILScanner(xAsmblr))
            {
                xScanner.LogException = (Exception e) =>
                {
                    Console.WriteLine("ILScanner exception : " + e.Message);
                };
                using (var xDebugInfo = new DebugInfo(MDFFile, true))
                {
                    xAsmblr.DebugInfo                = xDebugInfo;
                    xAsmblr.DebugEnabled             = true;
                    xAsmblr.DebugMode                = DebugMode.Source;
                    xAsmblr.TraceAssemblies          = TraceAssemblies.All;
                    xAsmblr.IgnoreDebugStubAttribute = false;

                    xAsmblr.Assembler.Initialize();
                    //TODO: Add plugs into the scanning equation to profile scanning them too
                    //System.Reflection.MethodInfo[] name = typeof(SSchockeTest.Kernel).GetMethods();
                    Type xFoundType = typeof(FakeKernel);
                    var  xCtor      = xFoundType.GetConstructor(Type.EmptyTypes);
                    typeof(Cosmos.System.Plugs.System.ConsoleImpl).IsSubclassOf(typeof(object));
                    var xEntryPoint = typeof(Kernel).GetMethod("Start", BindingFlags.Public | BindingFlags.Instance);
                    //var xEntryPoint = typeof(Program).GetMethod("ScannerEntryPoint", BindingFlags.NonPublic | BindingFlags.Static);
                    //EnableLogging(pathToLogFile)
                    xScanner.EnableLogging(logFile);
                    //xScanner.TempDebug += new Action<string>(xScanner_TempDebug);
                    //xScanner.
                    xScanner.QueueMethod(xEntryPoint);
                    xScanner.Execute(xCtor);
                    using (var xOut = new StreamWriter(outFile, false))
                    {
                        //if (EmitDebugSymbols) {
                        xAsmblr.Assembler.FlushText(xOut);
                        xAsmblr.FinalizeDebugInfo();
                    }
                    xSW.Stop();
                    Console.WriteLine("Total time : {0}", xSW.Elapsed);
                    Console.WriteLine("Method count: {0}", xScanner.MethodCount);
                    //Console.WriteLine("Instruction count: {0}", xScanner.InstructionCount);
                }
            }
        }