2011-12-29 19:18:12 +01:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2011-12-29 19:18:12 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-02-05 22:09:20 +01:00
|
|
|
#if defined(WIN32) && defined(CRASH_HANDLER)
|
2012-06-12 18:50:43 +02:00
|
|
|
|
2011-12-29 19:18:12 +01:00
|
|
|
#include "crashhandler.h"
|
|
|
|
#include <framework/global.h>
|
2012-07-14 03:10:24 +02:00
|
|
|
#include <framework/core/application.h>
|
2011-12-29 19:18:12 +01:00
|
|
|
|
2013-11-13 16:55:34 +01:00
|
|
|
#include <winsock2.h>
|
2012-03-21 16:31:34 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <imagehlp.h>
|
2011-12-29 19:18:12 +01:00
|
|
|
|
2012-03-21 16:31:34 +01:00
|
|
|
const char *getExceptionName(DWORD exceptionCode)
|
2011-12-29 19:18:12 +01:00
|
|
|
{
|
2012-03-21 16:31:34 +01:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stacktrace(LPEXCEPTION_POINTERS e, std::stringstream& ss)
|
|
|
|
{
|
|
|
|
PIMAGEHLP_SYMBOL pSym;
|
|
|
|
STACKFRAME sf;
|
|
|
|
HANDLE process, thread;
|
2013-11-13 16:55:34 +01:00
|
|
|
ULONG_PTR dwModBase, Disp;
|
2012-03-21 16:31:34 +01:00
|
|
|
BOOL more = FALSE;
|
2013-11-13 16:55:34 +01:00
|
|
|
DWORD machineType;
|
2012-03-21 16:31:34 +01:00
|
|
|
int count = 0;
|
|
|
|
char modname[MAX_PATH];
|
2013-11-13 16:55:34 +01:00
|
|
|
char symBuffer[sizeof(IMAGEHLP_SYMBOL) + 255];
|
2012-03-21 16:31:34 +01:00
|
|
|
|
2013-11-13 16:55:34 +01:00
|
|
|
pSym = (PIMAGEHLP_SYMBOL)symBuffer;
|
2012-03-21 16:31:34 +01:00
|
|
|
|
|
|
|
ZeroMemory(&sf, sizeof(sf));
|
2013-12-15 19:08:51 +01:00
|
|
|
#ifdef _WIN64
|
2013-11-13 16:55:34 +01:00
|
|
|
sf.AddrPC.Offset = e->ContextRecord->Rip;
|
|
|
|
sf.AddrStack.Offset = e->ContextRecord->Rsp;
|
|
|
|
sf.AddrFrame.Offset = e->ContextRecord->Rbp;
|
|
|
|
machineType = IMAGE_FILE_MACHINE_AMD64;
|
|
|
|
#else
|
2012-03-21 16:31:34 +01:00
|
|
|
sf.AddrPC.Offset = e->ContextRecord->Eip;
|
|
|
|
sf.AddrStack.Offset = e->ContextRecord->Esp;
|
|
|
|
sf.AddrFrame.Offset = e->ContextRecord->Ebp;
|
2013-11-13 16:55:34 +01:00
|
|
|
machineType = IMAGE_FILE_MACHINE_I386;
|
|
|
|
#endif
|
|
|
|
|
2012-03-21 16:31:34 +01:00
|
|
|
sf.AddrPC.Mode = AddrModeFlat;
|
|
|
|
sf.AddrStack.Mode = AddrModeFlat;
|
|
|
|
sf.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
|
|
|
|
process = GetCurrentProcess();
|
|
|
|
thread = GetCurrentThread();
|
|
|
|
|
|
|
|
while(1) {
|
2013-11-13 16:55:34 +01:00
|
|
|
more = StackWalk(machineType, process, thread, &sf, e->ContextRecord, NULL, SymFunctionTableAccess, SymGetModuleBase, NULL);
|
2012-03-21 16:31:34 +01:00
|
|
|
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");
|
|
|
|
|
2013-11-13 16:55:34 +01:00
|
|
|
Disp = 0;
|
|
|
|
pSym->SizeOfStruct = sizeof(symBuffer);
|
|
|
|
pSym->MaxNameLength = 254;
|
2012-03-21 16:31:34 +01:00
|
|
|
|
2012-03-22 13:57:43 +01:00
|
|
|
if(SymGetSymFromAddr(process, sf.AddrPC.Offset, &Disp, pSym))
|
2013-11-13 16:55:34 +01:00
|
|
|
ss << stdext::format(" %d: %s(%s+%#0lx) [0x%016lX]\n", count, modname, pSym->Name, Disp, sf.AddrPC.Offset);
|
2012-03-22 13:57:43 +01:00
|
|
|
else
|
2013-11-13 16:55:34 +01:00
|
|
|
ss << stdext::format(" %d: %s [0x%016lX]\n", count, modname, sf.AddrPC.Offset);
|
2012-03-21 16:31:34 +01:00
|
|
|
++count;
|
2011-12-29 19:18:12 +01:00
|
|
|
}
|
2012-03-21 16:31:34 +01:00
|
|
|
GlobalFree(pSym);
|
|
|
|
}
|
|
|
|
|
|
|
|
LONG CALLBACK ExceptionHandler(LPEXCEPTION_POINTERS e)
|
|
|
|
{
|
|
|
|
// generate crash report
|
|
|
|
SymInitialize(GetCurrentProcess(), 0, TRUE);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "== application crashed\n";
|
2013-02-05 23:00:28 +01:00
|
|
|
ss << stdext::format("app name: %s\n", g_app.getName());
|
|
|
|
ss << stdext::format("app version: %s\n", g_app.getVersion());
|
2012-05-28 15:06:26 +02:00
|
|
|
ss << stdext::format("build compiler: %s\n", BUILD_COMPILER);
|
2012-08-09 08:20:26 +02:00
|
|
|
ss << stdext::format("build date: %s\n", __DATE__);
|
2012-05-28 15:06:26 +02:00
|
|
|
ss << stdext::format("build type: %s\n", BUILD_TYPE);
|
2012-07-08 18:46:09 +02:00
|
|
|
ss << stdext::format("build revision: %s (%s)\n", BUILD_REVISION, BUILD_COMMIT);
|
2013-02-05 23:00:28 +01:00
|
|
|
ss << stdext::format("crash date: %s\n", stdext::date_time_string());
|
2012-05-28 15:06:26 +02:00
|
|
|
ss << stdext::format("exception: %s (0x%08lx)\n", getExceptionName(e->ExceptionRecord->ExceptionCode), e->ExceptionRecord->ExceptionCode);
|
2013-11-13 16:55:34 +01:00
|
|
|
ss << stdext::format("exception address: 0x%08lx\n", (size_t)e->ExceptionRecord->ExceptionAddress);
|
2012-05-28 15:06:26 +02:00
|
|
|
ss << stdext::format(" backtrace:\n");
|
2012-03-21 16:31:34 +01:00
|
|
|
Stacktrace(e, ss);
|
|
|
|
ss << "\n";
|
|
|
|
SymCleanup(GetCurrentProcess());
|
|
|
|
|
2012-03-22 13:57:43 +01:00
|
|
|
// print in stdout
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.info(ss.str());
|
2012-03-22 13:57:43 +01:00
|
|
|
|
2012-06-24 13:29:42 +02:00
|
|
|
// write stacktrace to crashreport.log
|
2012-03-21 16:31:34 +01:00
|
|
|
char dir[MAX_PATH];
|
|
|
|
GetCurrentDirectory(sizeof(dir) - 1, dir);
|
2012-06-24 13:29:42 +02:00
|
|
|
std::string fileName = stdext::format("%s\\crashreport.log", dir);
|
2012-03-22 13:57:43 +01:00
|
|
|
std::ofstream fout(fileName.c_str(), std::ios::out | std::ios::app);
|
|
|
|
if(fout.is_open() && fout.good()) {
|
|
|
|
fout << ss.str();
|
|
|
|
fout.close();
|
2012-06-02 02:38:26 +02:00
|
|
|
g_logger.info(stdext::format("Crash report saved to file %s", fileName));
|
2012-03-22 13:57:43 +01:00
|
|
|
} else
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error("Failed to save crash report!");
|
2012-03-21 16:31:34 +01:00
|
|
|
|
|
|
|
// inform the user
|
2012-05-29 00:04:44 +02:00
|
|
|
std::string msg = stdext::format(
|
|
|
|
"The application has crashed.\n\n"
|
|
|
|
"A crash report has been written to:\n"
|
|
|
|
"%s", fileName.c_str());
|
2012-03-21 16:31:34 +01:00
|
|
|
MessageBox(NULL, msg.c_str(), "Application crashed", 0);
|
|
|
|
|
|
|
|
// this seems to silently close the application
|
2013-11-12 19:31:51 +01:00
|
|
|
//return EXCEPTION_EXECUTE_HANDLER;
|
2012-03-21 16:31:34 +01:00
|
|
|
|
|
|
|
// this triggers the microsoft "application has crashed" error dialog
|
2013-11-12 19:31:51 +01:00
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
2011-12-29 19:18:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void installCrashHandler()
|
|
|
|
{
|
2012-03-21 16:31:34 +01:00
|
|
|
SetUnhandledExceptionFilter(ExceptionHandler);
|
2011-12-29 19:18:12 +01:00
|
|
|
}
|
2012-06-12 18:50:43 +02:00
|
|
|
|
|
|
|
#endif
|