From fd0a7ba871060c895fdf701799dbf649ee697078 Mon Sep 17 00:00:00 2001 From: Shauren Date: Sun, 15 Sep 2024 12:13:54 +0200 Subject: [PATCH] Core/CrashHandler: Compile WheatyExceptionReport only once and moved its global variable initializer to all projects using it --- src/CMakeLists.txt | 2 -- src/common/CMakeLists.txt | 15 ++++++++------- .../{ => Windows}/WheatyExceptionReport.cpp | 3 --- .../{ => Windows}/WheatyExceptionReport.h | 11 +++++++++-- src/server/bnetserver/Main.cpp | 6 ++++++ src/server/worldserver/Main.cpp | 6 ++++++ src/tools/map_extractor/System.cpp | 6 ++++++ src/tools/mmaps_generator/PathGenerator.cpp | 6 ++++++ src/tools/vmap4_assembler/VMapAssembler.cpp | 6 ++++++ src/tools/vmap4_extractor/vmapexport.cpp | 6 ++++++ 10 files changed, 53 insertions(+), 14 deletions(-) rename src/common/Debugging/{ => Windows}/WheatyExceptionReport.cpp (99%) rename src/common/Debugging/{ => Windows}/WheatyExceptionReport.h (97%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1d2ea3093f9..f13f506e8e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,8 +10,6 @@ if(WIN32 AND MSVC) set(sources_windows - ${CMAKE_SOURCE_DIR}/src/common/Debugging/WheatyExceptionReport.cpp - ${CMAKE_SOURCE_DIR}/src/common/Debugging/WheatyExceptionReport.h ${CMAKE_SOURCE_DIR}/src/common/WindowsSettings.manifest ) endif() diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 7b72bf845ca..0b365a7a712 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -12,16 +12,17 @@ CollectSourceFiles( ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE_SOURCES # Exclude - ${CMAKE_CURRENT_SOURCE_DIR}/Debugging + ${CMAKE_CURRENT_SOURCE_DIR}/Debugging/Windows ${CMAKE_CURRENT_SOURCE_DIR}/Platform ${CMAKE_CURRENT_SOURCE_DIR}/PrecompiledHeaders) -# Manually set sources for Debugging directory as we don't want to include WheatyExceptionReport in common project -# It needs to be included both in authserver and worldserver for the static global variable to be properly initialized -# and to handle crash logs on windows -list(APPEND PRIVATE_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/Debugging/Errors.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/Debugging/Errors.h) +if(WIN32) + CollectSourceFiles( + ${CMAKE_CURRENT_SOURCE_DIR}/Debugging/Windows + WINDOWS_DEBUGGING_SOURCES) + list(APPEND PRIVATE_SOURCES + ${WINDOWS_DEBUGGING_SOURCES}) +endif() if(USE_COREPCH) set(PRIVATE_PCH_HEADER PrecompiledHeaders/commonPCH.h) diff --git a/src/common/Debugging/WheatyExceptionReport.cpp b/src/common/Debugging/Windows/WheatyExceptionReport.cpp similarity index 99% rename from src/common/Debugging/WheatyExceptionReport.cpp rename to src/common/Debugging/Windows/WheatyExceptionReport.cpp index c9e3a985543..cec3a27e002 100644 --- a/src/common/Debugging/WheatyExceptionReport.cpp +++ b/src/common/Debugging/Windows/WheatyExceptionReport.cpp @@ -67,9 +67,6 @@ std::stack WheatyExceptionReport::symbolDetails; bool WheatyExceptionReport::alreadyCrashed; std::mutex WheatyExceptionReport::alreadyCrashedLock; WheatyExceptionReport::pRtlGetVersion WheatyExceptionReport::RtlGetVersion; - -// Declare global instance of class -WheatyExceptionReport g_WheatyExceptionReport; #pragma warning(pop) //============================== Class Methods ============================= diff --git a/src/common/Debugging/WheatyExceptionReport.h b/src/common/Debugging/Windows/WheatyExceptionReport.h similarity index 97% rename from src/common/Debugging/WheatyExceptionReport.h rename to src/common/Debugging/Windows/WheatyExceptionReport.h index baca9699c5b..d154fce0d13 100644 --- a/src/common/Debugging/WheatyExceptionReport.h +++ b/src/common/Debugging/Windows/WheatyExceptionReport.h @@ -3,6 +3,7 @@ #define _NO_CVCONST_H +#include "Define.h" #include "Optional.h" #include #include @@ -322,7 +323,7 @@ struct SymbolDetail bool HasChildren; }; -class WheatyExceptionReport +class TC_COMMON_API WheatyExceptionReport { public: @@ -396,5 +397,11 @@ class WheatyExceptionReport }; -extern WheatyExceptionReport g_WheatyExceptionReport; // global instance of class +#define INIT_CRASH_HANDLER() \ + __pragma(warning(push)) \ + __pragma(warning(disable:4073)) /* C4073: initializers put in library initialization area */ \ + __pragma(init_seg(lib)) \ + WheatyExceptionReport g_WheatyExceptionReport; \ + __pragma(warning(pop)) + #endif // _WHEATYEXCEPTIONREPORT_ diff --git a/src/server/bnetserver/Main.cpp b/src/server/bnetserver/Main.cpp index e9ae51847c5..31ccf0bfb77 100644 --- a/src/server/bnetserver/Main.cpp +++ b/src/server/bnetserver/Main.cpp @@ -434,3 +434,9 @@ variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, f return variablesMap; } + +#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS +#include "WheatyExceptionReport.h" +// must be at end of file because of init_seg pragma +INIT_CRASH_HANDLER(); +#endif diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp index c3ad9b4f420..9743580d74b 100644 --- a/src/server/worldserver/Main.cpp +++ b/src/server/worldserver/Main.cpp @@ -740,3 +740,9 @@ variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, f return vm; } + +#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS +#include "WheatyExceptionReport.h" +// must be at end of file because of init_seg pragma +INIT_CRASH_HANDLER(); +#endif diff --git a/src/tools/map_extractor/System.cpp b/src/tools/map_extractor/System.cpp index ca91068a234..698adff25c1 100644 --- a/src/tools/map_extractor/System.cpp +++ b/src/tools/map_extractor/System.cpp @@ -1567,3 +1567,9 @@ int main(int argc, char * arg[]) return 0; } + +#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS +#include "WheatyExceptionReport.h" +// must be at end of file because of init_seg pragma +INIT_CRASH_HANDLER(); +#endif diff --git a/src/tools/mmaps_generator/PathGenerator.cpp b/src/tools/mmaps_generator/PathGenerator.cpp index 4b9620bff1f..f3ba4ab48b6 100644 --- a/src/tools/mmaps_generator/PathGenerator.cpp +++ b/src/tools/mmaps_generator/PathGenerator.cpp @@ -458,3 +458,9 @@ int main(int argc, char** argv) printf("Finished. MMAPS were built in %s\n", secsToTimeString(GetMSTimeDiffToNow(start) / 1000).c_str()); return 0; } + +#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS +#include "WheatyExceptionReport.h" +// must be at end of file because of init_seg pragma +INIT_CRASH_HANDLER(); +#endif diff --git a/src/tools/vmap4_assembler/VMapAssembler.cpp b/src/tools/vmap4_assembler/VMapAssembler.cpp index 444f748753c..5cdc214665e 100644 --- a/src/tools/vmap4_assembler/VMapAssembler.cpp +++ b/src/tools/vmap4_assembler/VMapAssembler.cpp @@ -111,3 +111,9 @@ Optional HandleArgs(int argc, char* argv[], std::string* src, std::string* return {}; } + +#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS +#include "WheatyExceptionReport.h" +// must be at end of file because of init_seg pragma +INIT_CRASH_HANDLER(); +#endif diff --git a/src/tools/vmap4_extractor/vmapexport.cpp b/src/tools/vmap4_extractor/vmapexport.cpp index 9ab9ccfac19..cc518cfab50 100644 --- a/src/tools/vmap4_extractor/vmapexport.cpp +++ b/src/tools/vmap4_extractor/vmapexport.cpp @@ -604,3 +604,9 @@ int main(int argc, char ** argv) printf("Extract %s. Work complete. No errors.\n", VMAP::VMAP_MAGIC); return 0; } + +#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS +#include "WheatyExceptionReport.h" +// must be at end of file because of init_seg pragma +INIT_CRASH_HANDLER(); +#endif