From 8744e829798768270935782afdce1304f8e94185 Mon Sep 17 00:00:00 2001 From: goaaats Date: Wed, 22 Jun 2022 16:58:53 +0200 Subject: [PATCH] feat: make ETW configurable, doesn't actually do anything yet because we're stuck on .NET 5 --- Dalamud.Boot/DalamudStartInfo.cpp | 2 ++ Dalamud.Boot/DalamudStartInfo.h | 1 + Dalamud.Boot/dllmain.cpp | 1 + Dalamud.Injector.Boot/main.cpp | 1 + Dalamud.Injector/EntryPoint.cs | 5 ++++- Dalamud/DalamudStartInfo.cs | 6 ++++++ lib/CoreCLR/boot.cpp | 6 ++---- lib/CoreCLR/boot.h | 1 + 8 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Dalamud.Boot/DalamudStartInfo.cpp b/Dalamud.Boot/DalamudStartInfo.cpp index 2daff8746..1dddce4d3 100644 --- a/Dalamud.Boot/DalamudStartInfo.cpp +++ b/Dalamud.Boot/DalamudStartInfo.cpp @@ -86,6 +86,7 @@ void from_json(const nlohmann::json& json, DalamudStartInfo& config) { config.BootWaitDebugger = json.value("BootWaitDebugger", config.BootWaitDebugger); config.BootVehEnabled = json.value("BootVehEnabled", config.BootVehEnabled); config.BootVehFull = json.value("BootVehFull", config.BootVehFull); + config.BootEnableEtw = json.value("BootEnableEtw", config.BootEnableEtw); config.BootDotnetOpenProcessHookMode = json.value("BootDotnetOpenProcessHookMode", config.BootDotnetOpenProcessHookMode); if (const auto it = json.find("BootEnabledGameFixes"); it != json.end() && it->is_array()) { config.BootEnabledGameFixes.clear(); @@ -107,6 +108,7 @@ void DalamudStartInfo::from_envvars() { BootWaitDebugger = utils::get_env(L"DALAMUD_WAIT_DEBUGGER"); BootVehEnabled = utils::get_env(L"DALAMUD_IS_VEH"); BootVehFull = utils::get_env(L"DALAMUD_IS_VEH_FULL"); + BootEnableEtw = utils::get_env(L"DALAMUD_ENABLE_ETW"); BootDotnetOpenProcessHookMode = static_cast(utils::get_env(L"DALAMUD_DOTNET_OPENPROCESS_HOOKMODE")); for (const auto& item : utils::get_env_list(L"DALAMUD_GAMEFIX_LIST")) BootEnabledGameFixes.insert(unicode::convert(item, &unicode::lower)); diff --git a/Dalamud.Boot/DalamudStartInfo.h b/Dalamud.Boot/DalamudStartInfo.h index dcf75d663..5477844a1 100644 --- a/Dalamud.Boot/DalamudStartInfo.h +++ b/Dalamud.Boot/DalamudStartInfo.h @@ -38,6 +38,7 @@ struct DalamudStartInfo { bool BootWaitDebugger = false; bool BootVehEnabled = false; bool BootVehFull = false; + bool BootEnableEtw = false; DotNetOpenProcessHookMode BootDotnetOpenProcessHookMode = DotNetOpenProcessHookMode::ImportHooks; std::set BootEnabledGameFixes{}; std::set BootUnhookDlls{}; diff --git a/Dalamud.Boot/dllmain.cpp b/Dalamud.Boot/dllmain.cpp index 85a014051..bae192641 100644 --- a/Dalamud.Boot/dllmain.cpp +++ b/Dalamud.Boot/dllmain.cpp @@ -116,6 +116,7 @@ DllExport DWORD WINAPI Initialize(LPVOID lpParam, HANDLE hMainThreadContinue) { void* entrypoint_vfn; int result = InitializeClrAndGetEntryPoint( g_hModule, + g_startInfo.BootEnableEtw, runtimeconfig_path, module_path, L"Dalamud.EntryPoint, Dalamud", diff --git a/Dalamud.Injector.Boot/main.cpp b/Dalamud.Injector.Boot/main.cpp index 5147b91e4..ee939968d 100644 --- a/Dalamud.Injector.Boot/main.cpp +++ b/Dalamud.Injector.Boot/main.cpp @@ -24,6 +24,7 @@ int wmain(int argc, wchar_t** argv) void* entrypoint_vfn; int result = InitializeClrAndGetEntryPoint( GetModuleHandleW(nullptr), + false, runtimeconfig_path, module_path, L"Dalamud.Injector.EntryPoint, Dalamud.Injector", diff --git a/Dalamud.Injector/EntryPoint.cs b/Dalamud.Injector/EntryPoint.cs index c1f1cfd3a..8be568063 100644 --- a/Dalamud.Injector/EntryPoint.cs +++ b/Dalamud.Injector/EntryPoint.cs @@ -83,7 +83,8 @@ namespace Dalamud.Injector } startInfo = ExtractAndInitializeStartInfoFromArguments(startInfo, args); - args.Remove("--console"); // Remove "console" flag + args.Remove("--console"); // Remove "console" flag, already handled + args.Remove("--etw"); // Remove "etw" flag, already handled var mainCommand = args[1].ToLowerInvariant(); if (mainCommand.Length > 0 && mainCommand.Length <= 6 && "inject"[..mainCommand.Length] == mainCommand) @@ -321,6 +322,7 @@ namespace Dalamud.Injector // Set boot defaults startInfo.BootShowConsole = args.Contains("--console"); + startInfo.BootEnableEtw = args.Contains("--etw"); startInfo.BootLogPath = GetLogPath("dalamud.boot"); startInfo.BootEnabledGameFixes = new List { "prevent_devicechange_crashes", "disable_game_openprocess_access_check", "redirect_openprocess" }; startInfo.BootDotnetOpenProcessHookMode = 0; @@ -361,6 +363,7 @@ namespace Dalamud.Injector Console.WriteLine("Verbose logging:\t[-v]"); Console.WriteLine("Show Console:\t[--console]"); + Console.WriteLine("Enable ETW:\t[--etw]"); return 0; } diff --git a/Dalamud/DalamudStartInfo.cs b/Dalamud/DalamudStartInfo.cs index e9bbc6f46..5ba919828 100644 --- a/Dalamud/DalamudStartInfo.cs +++ b/Dalamud/DalamudStartInfo.cs @@ -41,6 +41,7 @@ namespace Dalamud this.BootWaitDebugger = other.BootWaitDebugger; this.BootVehEnabled = other.BootVehEnabled; this.BootVehFull = other.BootVehFull; + this.BootEnableEtw = other.BootEnableEtw; this.BootDotnetOpenProcessHookMode = other.BootDotnetOpenProcessHookMode; this.BootEnabledGameFixes = other.BootEnabledGameFixes; this.BootUnhookDlls = other.BootUnhookDlls; @@ -122,6 +123,11 @@ namespace Dalamud /// public bool BootVehFull { get; set; } + /// + /// Gets or sets a value indicating whether or not ETW should be enabled. + /// + public bool BootEnableEtw { get; set; } + /// /// Gets or sets a value choosing the OpenProcess hookmode. /// diff --git a/lib/CoreCLR/boot.cpp b/lib/CoreCLR/boot.cpp index 0d0db4e31..aa4fdfa62 100644 --- a/lib/CoreCLR/boot.cpp +++ b/lib/CoreCLR/boot.cpp @@ -29,6 +29,7 @@ std::optional g_clr; int InitializeClrAndGetEntryPoint( void* calling_module, + bool enableEtw, std::wstring runtimeconfig_path, std::wstring module_path, std::wstring entrypoint_assembly_name, @@ -49,10 +50,7 @@ int InitializeClrAndGetEntryPoint( SetEnvironmentVariable(L"DOTNET_TC_QuickJitForLoops", L"1"); SetEnvironmentVariable(L"DOTNET_ReadyToRun", L"1"); -#if NDEBUG - // This might fix extremely bad performance in some algorithms on insider builds - SetEnvironmentVariable(L"COMPlus_ETWEnabled", L"0"); -#endif + SetEnvironmentVariable(L"COMPlus_ETWEnabled", enableEtw ? L"1" : L"0"); wchar_t* dotnet_path; wchar_t* _appdata; diff --git a/lib/CoreCLR/boot.h b/lib/CoreCLR/boot.h index f306563ad..75193f6fc 100644 --- a/lib/CoreCLR/boot.h +++ b/lib/CoreCLR/boot.h @@ -3,6 +3,7 @@ void ConsoleTeardown(); int InitializeClrAndGetEntryPoint( void* calling_module, + bool enableEtw, std::wstring runtimeconfig_path, std::wstring module_path, std::wstring entrypoint_assembly_name,