1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/* Author: Tobi Vollebregt */
#include "StdAfx.h"
#include "lib/gml/gml.h"
#include <windows.h>
#include <process.h>
#include <imagehlp.h>
#include <signal.h>
#include <SDL.h> // for SDL_Quit
#include "CrashHandler.h"
#include "Game/GameVersion.h"
#include "LogOutput.h"
#include "NetProtocol.h"
#include "Util.h"
#include "ConfigHandler.h"
#include <boost/thread/thread.hpp>
#define BUFFER_SIZE 2048
#if defined(USE_GML) && GML_ENABLE_SIM
extern volatile int gmlMultiThreadSim;
#endif
HANDLE simthread = INVALID_HANDLE_VALUE; // used in gmlsrv.h as well
HANDLE drawthread = INVALID_HANDLE_VALUE;
namespace CrashHandler {
namespace Win32 {
boost::thread* hangdetectorthread = NULL;
volatile int keepRunning = 1;
// watchdog timers
unsigned volatile simwdt = 0, drawwdt = 0;
int hangTimeout = 0;
void SigAbrtHandler(int signal)
{
// cause an exception if on windows
// TODO FIXME do a proper stacktrace dump here
*((int*)(0)) = 0;
}
// Set this to the desired printf style output function.
// Currently we write through the logOutput class to infolog.txt
#define PRINT logOutput.Print
/** Convert exception code to human readable string. */
static const char *ExceptionName(DWORD exceptionCode)
{
switch (exceptionCode) {
case EXCEPTION_ACCESS_VIOLATION: return "Access violation";
case EXCEPTION_DATATYPE_MISALIGNMENT: return "Datatype misalignment";
case EXCEPTION_BREAKPOINT: return "Breakpoint";
case EXCEPTION_SINGLE_STEP: return "Single step";
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: return "Array bounds exceeded";
case EXCEPTION_FLT_DENORMAL_OPERAND: return "Float denormal operand";
case EXCEPTION_FLT_DIVIDE_BY_ZERO: return "Float divide by zero";
case EXCEPTION_FLT_INEXACT_RESULT: return "Float inexact result";
case EXCEPTION_FLT_INVALID_OPERATION: return "Float invalid operation";
case EXCEPTION_FLT_OVERFLOW: return "Float overflow";
case EXCEPTION_FLT_STACK_CHECK: return "Float stack check";
case EXCEPTION_FLT_UNDERFLOW: return "Float underflow";
case EXCEPTION_INT_DIVIDE_BY_ZERO: return "Integer divide by zero";
case EXCEPTION_INT_OVERFLOW: return "Integer overflow";
case EXCEPTION_PRIV_INSTRUCTION: return "Privileged instruction";
case EXCEPTION_IN_PAGE_ERROR: return "In page error";
case EXCEPTION_ILLEGAL_INSTRUCTION: return "Illegal instruction";
case EXCEPTION_NONCONTINUABLE_EXCEPTION: return "Noncontinuable exception";
case EXCEPTION_STACK_OVERFLOW: return "Stack overflow";
case EXCEPTION_INVALID_DISPOSITION: return "Invalid disposition";
case EXCEPTION_GUARD_PAGE: return "Guard page";
case EXCEPTION_INVALID_HANDLE: return "Invalid handle";
}
return "Unknown exception";
}
/** Print out a stacktrace. */
static void Stacktrace(LPEXCEPTION_POINTERS e, HANDLE hThread = INVALID_HANDLE_VALUE) {
PIMAGEHLP_SYMBOL pSym;
STACKFRAME sf;
HANDLE process, thread;
DWORD dwModBase, Disp;
BOOL more = FALSE;
int count = 0;
char modname[MAX_PATH];
pSym = (PIMAGEHLP_SYMBOL)GlobalAlloc(GMEM_FIXED, 16384);
BOOL suspended = FALSE;
CONTEXT c;
if (e) {
c = *e->ContextRecord;
thread = GetCurrentThread();
} else {
SuspendThread(hThread);
suspended = TRUE;
memset(&c, 0, sizeof(CONTEXT));
c.ContextFlags = CONTEXT_FULL;
// FIXME: This does not work if you want to dump the current thread's stack
if (!GetThreadContext(hThread, &c)) {
ResumeThread(hThread);
return;
}
thread = hThread;
}
ZeroMemory(&sf, sizeof(sf));
sf.AddrPC.Offset = c.Eip;
sf.AddrStack.Offset = c.Esp;
sf.AddrFrame.Offset = c.Ebp;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrStack.Mode = AddrModeFlat;
sf.AddrFrame.Mode = AddrModeFlat;
process = GetCurrentProcess();
// use globalalloc to reduce risk for allocator related deadlock
char* printstrings = (char*)GlobalAlloc(GMEM_FIXED, 0);
bool containsOglDll = false;
while (true) {
more = StackWalk(
IMAGE_FILE_MACHINE_I386, // TODO: fix this for 64 bit windows?
process,
thread,
&sf,
&c,
NULL,
SymFunctionTableAccess,
SymGetModuleBase,
NULL
);
if (!more || sf.AddrFrame.Offset == 0) {
break;
}
dwModBase = SymGetModuleBase(process, sf.AddrPC.Offset);
if (dwModBase) {
GetModuleFileName((HINSTANCE)dwModBase, modname, MAX_PATH);
} else {
strcpy(modname, "Unknown");
}
pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
pSym->MaxNameLength = MAX_PATH;
char *printstringsnew = (char *)GlobalAlloc(GMEM_FIXED, (count + 1) * BUFFER_SIZE);
memcpy(printstringsnew, printstrings, count * BUFFER_SIZE);
GlobalFree(printstrings);
printstrings = printstringsnew;
if (SymGetSymFromAddr(process, sf.AddrPC.Offset, &Disp, pSym)) {
// This is the code path taken on VC if debugging syms are found.
SNPRINTF(printstrings + count * BUFFER_SIZE, BUFFER_SIZE, "(%d) %s(%s+%#0x) [0x%08X]", count, modname, pSym->Name, Disp, sf.AddrPC.Offset);
} else {
// This is the code path taken on MinGW, and VC if no debugging syms are found.
SNPRINTF(printstrings + count * BUFFER_SIZE, BUFFER_SIZE, "(%d) %s [0x%08X]", count, modname, sf.AddrPC.Offset);
}
const std::string str_modname = modname;
// OpenGL lib names (ATI): "atioglxx.dll" "atioglx2.dll"
containsOglDll = (containsOglDll || (str_modname.find("atiogl") != std::string::npos));
// OpenGL lib names (Nvidia): "nvoglnt.dll" "nvoglv32.dll" "nvoglv64.dll" (last one is a guess)
containsOglDll = (containsOglDll || (str_modname.find("nvogl") != std::string::npos));
++count;
}
if (containsOglDll) {
PRINT("This stack trace indicates a problem with your graphic card driver. "
"Please try upgrading or downgrading it. "
"Specifically recommended is the latest driver, and one that is as old as your graphic card.");
}
if (suspended) {
ResumeThread(hThread);
}
for (int i = 0; i < count; ++i) {
PRINT("%s", printstrings + i * BUFFER_SIZE);
}
GlobalFree(printstrings);
GlobalFree(pSym);
}
/** Callback for SymEnumerateModules */
#if _MSC_VER >= 1500
static BOOL CALLBACK EnumModules(PCSTR moduleName, ULONG baseOfDll, PVOID userContext)
{
PRINT("0x%08x\t%s", baseOfDll, moduleName);
return TRUE;
}
#else // _MSC_VER >= 1500
static BOOL CALLBACK EnumModules(LPSTR moduleName, DWORD baseOfDll, PVOID userContext)
{
PRINT("0x%08x\t%s", baseOfDll, moduleName);
return TRUE;
}
#endif // _MSC_VER >= 1500
/** Called by windows if an exception happens. */
static LONG CALLBACK ExceptionHandler(LPEXCEPTION_POINTERS e)
{
// Prologue.
logOutput.RemoveAllSubscribers();
PRINT("Spring %s has crashed.", SpringVersion::GetFull().c_str());
#ifdef USE_GML
PRINT("MT with %d threads.", gmlThreadCount);
#endif
// Initialize IMAGEHLP.DLL.
SymInitialize(GetCurrentProcess(), ".", TRUE);
// Record exception info.
PRINT("Exception: %s (0x%08x)", ExceptionName(e->ExceptionRecord->ExceptionCode), e->ExceptionRecord->ExceptionCode);
PRINT("Exception Address: 0x%08x", e->ExceptionRecord->ExceptionAddress);
// Record list of loaded DLLs.
PRINT("DLL information:");
SymEnumerateModules(GetCurrentProcess(), EnumModules, NULL);
// Record stacktrace.
PRINT("Stacktrace:");
Stacktrace(e);
// Unintialize IMAGEHLP.DLL
SymCleanup(GetCurrentProcess());
// Cleanup.
SDL_Quit();
logOutput.End(); // Stop writing to log.
// FIXME: update closing of demo to new netcode
// Inform user.
char dir[MAX_PATH], msg[MAX_PATH+200];
GetCurrentDirectory(sizeof(dir) - 1, dir);
SNPRINTF(msg, sizeof(msg),
"Spring has crashed.\n\n"
"A stacktrace has been written to:\n"
"%s\\infolog.txt", dir);
MessageBox(NULL, msg, "Spring: Unhandled exception", 0);
// this seems to silently close the application
return EXCEPTION_EXECUTE_HANDLER;
// this triggers the microsoft "application has crashed" error dialog
//return EXCEPTION_CONTINUE_SEARCH;
// in practice, 100% CPU usage but no continuation of execution
// (tested segmentation fault and division by zero)
//return EXCEPTION_CONTINUE_EXECUTION;
}
/** Print stack traces for relevant threads. */
void HangHandler()
{
PRINT("Hang detection triggered for Spring %s.", SpringVersion::GetFull().c_str());
#ifdef USE_GML
PRINT("MT with %d threads.", gmlThreadCount);
#endif
// Initialize IMAGEHLP.DLL.
SymInitialize(GetCurrentProcess(), ".", TRUE);
// Record list of loaded DLLs.
PRINT("DLL information:");
SymEnumerateModules(GetCurrentProcess(), EnumModules, NULL);
if (drawthread != INVALID_HANDLE_VALUE) {
// Record stacktrace.
PRINT("Stacktrace:");
Stacktrace(NULL, drawthread);
}
#if defined(USE_GML) && GML_ENABLE_SIM
if (gmlMultiThreadSim && simthread != INVALID_HANDLE_VALUE) {
PRINT("Stacktrace (sim):");
Stacktrace(NULL, simthread);
}
#endif
// Unintialize IMAGEHLP.DLL
SymCleanup(GetCurrentProcess());
logOutput.Flush();
}
void HangDetector() {
while (keepRunning) {
unsigned curwdt = SDL_GetTicks();
#if defined(USE_GML) && GML_ENABLE_SIM
if (gmlMultiThreadSim) {
unsigned cursimwdt = simwdt;
if (cursimwdt && (curwdt - cursimwdt) > hangTimeout * 1000) {
HangHandler();
simwdt = curwdt;
}
}
#endif
unsigned curdrawwdt = drawwdt;
if (curdrawwdt && (curwdt - curdrawwdt) > hangTimeout * 1000) {
HangHandler();
drawwdt = curwdt;
}
SDL_Delay(1000);
}
}
void InstallHangHandler() {
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(),
&drawthread, 0, TRUE, DUPLICATE_SAME_ACCESS);
hangTimeout = configHandler->Get("HangTimeout", 0);
#ifdef USE_GML
if (hangTimeout == 0) {
// HangTimeout = -1 to force disable hang detection in MT build
hangTimeout = 10;
}
#endif
if (hangTimeout > 0) {
hangdetectorthread = new boost::thread(&HangDetector);
}
}
void ClearDrawWDT(bool disable) {
drawwdt = disable ? 0 : SDL_GetTicks();
}
void ClearSimWDT(bool disable) {
simwdt = disable ? 0 : SDL_GetTicks();
}
void UninstallHangHandler() {
if (hangdetectorthread) {
keepRunning = 0;
hangdetectorthread->join();
delete hangdetectorthread;
}
if (drawthread != INVALID_HANDLE_VALUE) {
CloseHandle(drawthread);
}
if (simthread != INVALID_HANDLE_VALUE) {
CloseHandle(simthread);
}
}
/** Install crash handler. */
void Install()
{
SetUnhandledExceptionFilter(ExceptionHandler);
signal(SIGABRT, SigAbrtHandler);
}
/** Uninstall crash handler. */
void Remove()
{
SetUnhandledExceptionFilter(NULL);
signal(SIGABRT, SIG_DFL);
}
}; // namespace Win32
}; // namespace CrashHandler
|