Program.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // See https://aka.ms/new-console-template for more information
  2. using System.Reflection;
  3. using System.Runtime.CompilerServices;
  4. using JitInspect;
  5. using Lua;
  6. using Lua.Runtime;
  7. using Lua.Standard;
  8. // dotnet run --configuration Release /p:DefineConstants="CASE_MARKER"
  9. // to activate the CASE_MARKER
  10. // JitInspect can be run in Windows and Linux (MacOS is not supported yet)
  11. var luaState = LuaGlobalState.Create();
  12. luaState.OpenStandardLibraries();
  13. var closure = luaState.Load(File.ReadAllBytes(GetAbsolutePath("test.lua")), "test.lua");
  14. for (var i = 0; i < 1000; i++)
  15. {
  16. await luaState.RootAccess.RunAsync(closure);
  17. luaState.MainThread.Stack.Clear();
  18. }
  19. var savePath = GetAbsolutePath("history");
  20. var thisDir = GetThisDirectoryName();
  21. var newJIitPath = Path.Join(thisDir, $"jit_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.txt");
  22. var lastJitPaths = Directory.GetFiles(thisDir).Where(x => x.Contains("jit_"));
  23. if (!Directory.Exists(savePath))
  24. {
  25. Directory.CreateDirectory(savePath);
  26. }
  27. if (lastJitPaths.Any())
  28. {
  29. Console.WriteLine("Last:" + File.ReadAllLines(lastJitPaths.First())[^1]);
  30. foreach (var jitPath in lastJitPaths)
  31. {
  32. var last = jitPath;
  33. var dest = Path.Join(savePath, Path.GetFileName(jitPath));
  34. File.Move(last, dest);
  35. }
  36. }
  37. var method = typeof(LuaVirtualMachine).GetMethod("MoveNext", BindingFlags.Static | BindingFlags.NonPublic)!;
  38. using var disassembler = JitDisassembler.Create();
  39. var nextJitText = disassembler.Disassemble(method, new() { PrintInstructionAddresses = true });
  40. File.WriteAllText(newJIitPath, nextJitText);
  41. //Console.WriteLine("New:" + nextJitText.Split("\n")[^1]);
  42. static string GetThisDirectoryName([CallerFilePath] string callerFilePath = "")
  43. {
  44. return Path.GetDirectoryName(callerFilePath)!;
  45. }
  46. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  47. {
  48. return Path.Join(Path.GetDirectoryName(callerFilePath)!, relativePath);
  49. }