Skip to content

Commit a5b850e

Browse files
committed
Add cvar to disable native exe crash handling
So server owners who want to get Unix core dumps for native exe gamelogic could set sgame.nativeExeCrashHandler to 0. Doing this stops the crash handler from being installed. In case of a fatal C++ exception we still install the terminate handler so we can log the exception message, but afterward, if the cvar is off, we call abort() instead of shutting down with the ErrorMsg IPC.
1 parent 3664ef7 commit a5b850e

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

cmake/DaemonGame.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ function(buildGameModule module_slug)
119119
set_target_properties(${module_target} PROPERTIES PREFIX "")
120120
set(GAMEMODULE_DEFINITIONS "${GAMEMODULE_DEFINITIONS};BUILD_VM_IN_PROCESS")
121121
else()
122+
if (module_slug STREQUAL "native-exe")
123+
set(GAMEMODULE_DEFINITIONS "${GAMEMODULE_DEFINITIONS};BUILD_VM_NATIVE_EXE")
124+
endif()
122125
add_executable("${module_target}" ${module_target_args})
123126
endif()
124127

src/shared/CommonProxies.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,17 @@ namespace VM {
437437

438438
void InitializeProxies(int milliseconds) {
439439
baseTime = Sys::SteadyClock::now() - std::chrono::milliseconds(milliseconds);
440-
Cmd::InitializeProxy();
441440
Cvar::InitializeProxy();
441+
442+
#ifdef BUILD_VM_NATIVE_EXE
443+
// Set up the crash handler now that we have cvars to know if we want to use it.
444+
// The handler wouldn't have worked before StaticInitMsg anyway since it needs IPC to log anything.
445+
if (useNativeExeCrashHandler.Get()) {
446+
Sys::SetupCrashHandler();
447+
}
448+
#endif
449+
450+
Cmd::InitializeProxy();
442451
}
443452

444453
static void HandleMiscSyscall(int minor, Util::Reader& reader, IPC::Channel& channel) {

src/shared/VMMain.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3737

3838
IPC::Channel VM::rootChannel;
3939

40+
#ifdef BUILD_VM_NATIVE_EXE
41+
// When using native exe, turn this off if you want to use core dumps (*nix) or attach a debugger
42+
// upon crashing (Windows). Disabling it is like -nocrashhandler for Daemon.
43+
Cvar::Cvar<bool> VM::useNativeExeCrashHandler(
44+
VM_STRING_PREFIX "nativeExeCrashHandler", "catch and log crashes/fatal exceptions in native exe VM", Cvar::NONE, true);
45+
#endif
46+
4047
#ifdef BUILD_VM_IN_PROCESS
4148
// Special exception type used to cleanly exit a thread for in-process VMs
4249
// Using an anonymous namespace so the compiler knows that the exception is
@@ -159,9 +166,15 @@ NORETURN static void TerminateHandler()
159166
#ifdef __native_client__
160167
// Using a lambda triggers -Wformat-security...
161168
# define DispatchError(...) snprintf(realErrorMessage, sizeof(realErrorMessage), __VA_ARGS__)
162-
#else
169+
#elif defined(BUILD_VM_NATIVE_EXE)
163170
auto DispatchError = [](const char* msg, const auto&... fmtArgs) {
164-
Sys::Error(msg, fmtArgs...);
171+
if (VM::useNativeExeCrashHandler.Get()) {
172+
Sys::Error(msg, fmtArgs...);
173+
} else {
174+
Log::Warn(XSTRING(VM_NAME) " VM terminating:");
175+
Log::Warn(msg, fmtArgs...);
176+
// fall through to abort() for core dump etc.
177+
}
165178
};
166179
#endif
167180

@@ -199,7 +212,9 @@ int main(int argc, char** argv)
199212
std::set_terminate(TerminateHandler);
200213
// Set up crash handling for this process. This will allow crashes to be
201214
// sent back to the engine and reported to the user.
215+
#ifdef __native_client__
202216
Sys::SetupCrashHandler();
217+
#endif
203218

204219
try {
205220
CommonInit(rootSocket);

src/shared/VMMain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ namespace VM {
3838
// Root channel used to communicate with the engine
3939
extern IPC::Channel rootChannel;
4040

41+
#ifdef BUILD_VM_NATIVE_EXE
42+
extern Cvar::Cvar<bool> useNativeExeCrashHandler;
43+
#endif
44+
4145
// Functions each specific gamelogic should implement
4246
void VMInit();
4347
void VMHandleSyscall(uint32_t id, Util::Reader reader);

0 commit comments

Comments
 (0)