2011-12-03 22:41:37 +01:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2011-12-03 22:41:37 +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-03 22:41:37 +01:00
|
|
|
|
2012-04-15 04:39:14 +02:00
|
|
|
#ifndef __USE_GNU
|
|
|
|
#define __USE_GNU
|
|
|
|
#endif
|
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include <ucontext.h>
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
#define MAX_BACKTRACE_DEPTH 128
|
|
|
|
#define DEMANGLE_BACKTRACE_SYMBOLS
|
|
|
|
|
|
|
|
void crashHandler(int signum, siginfo_t* info, void* secret)
|
|
|
|
{
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error("Application crashed");
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
std::stringstream ss;
|
2012-06-20 02:15:56 +02: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);
|
2012-05-28 15:06:26 +02:00
|
|
|
ss << stdext::format("crash date: %s\n", stdext::date_time_string());
|
2011-12-03 22:41:37 +01:00
|
|
|
ss.flags(std::ios::hex | std::ios::showbase);
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-03-22 13:57:43 +01:00
|
|
|
ucontext_t context = *(ucontext_t*)secret;
|
2012-02-20 03:27:08 +01:00
|
|
|
#if __WORDSIZE == 64
|
2011-12-03 22:41:37 +01:00
|
|
|
ss << " at rip = " << context.uc_mcontext.gregs[REG_RIP] << std::endl;
|
|
|
|
ss << " rax = " << context.uc_mcontext.gregs[REG_RAX] << std::endl;
|
|
|
|
ss << " rbx = " << context.uc_mcontext.gregs[REG_RBX] << std::endl;
|
|
|
|
ss << " rcx = " << context.uc_mcontext.gregs[REG_RCX] << std::endl;
|
|
|
|
ss << " rdx = " << context.uc_mcontext.gregs[REG_RDX] << std::endl;
|
|
|
|
ss << " rsi = " << context.uc_mcontext.gregs[REG_RSI] << std::endl;
|
|
|
|
ss << " rdi = " << context.uc_mcontext.gregs[REG_RDI] << std::endl;
|
|
|
|
ss << " rbp = " << context.uc_mcontext.gregs[REG_RBP] << std::endl;
|
|
|
|
ss << " rsp = " << context.uc_mcontext.gregs[REG_RSP] << std::endl;
|
|
|
|
ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
|
|
|
|
ss << std::endl;
|
2012-04-04 21:02:43 +02:00
|
|
|
#elif defined(REG_EIP)
|
2012-02-20 03:27:08 +01:00
|
|
|
ss << " at eip = " << context.uc_mcontext.gregs[REG_EIP] << std::endl;
|
|
|
|
ss << " eax = " << context.uc_mcontext.gregs[REG_EAX] << std::endl;
|
|
|
|
ss << " ebx = " << context.uc_mcontext.gregs[REG_EBX] << std::endl;
|
|
|
|
ss << " ecx = " << context.uc_mcontext.gregs[REG_ECX] << std::endl;
|
|
|
|
ss << " edx = " << context.uc_mcontext.gregs[REG_EDX] << std::endl;
|
|
|
|
ss << " esi = " << context.uc_mcontext.gregs[REG_ESI] << std::endl;
|
|
|
|
ss << " edi = " << context.uc_mcontext.gregs[REG_EDI] << std::endl;
|
|
|
|
ss << " ebp = " << context.uc_mcontext.gregs[REG_EBP] << std::endl;
|
|
|
|
ss << " esp = " << context.uc_mcontext.gregs[REG_ESP] << std::endl;
|
|
|
|
ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
|
|
|
|
ss << std::endl;
|
2011-12-30 05:50:19 +01:00
|
|
|
#endif
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
ss.flags(std::ios::dec);
|
|
|
|
ss << " backtrace:" << std::endl;
|
|
|
|
|
|
|
|
void* buffer[MAX_BACKTRACE_DEPTH];
|
|
|
|
int numLevels = backtrace(buffer, MAX_BACKTRACE_DEPTH);
|
|
|
|
char **tracebackBuffer = backtrace_symbols(buffer, numLevels);
|
|
|
|
if(tracebackBuffer) {
|
|
|
|
for(int i = 2; i < numLevels; i++) {
|
|
|
|
std::string line = tracebackBuffer[i];
|
|
|
|
if(line.find("__libc_start_main") != std::string::npos)
|
|
|
|
break;
|
|
|
|
#ifdef DEMANGLE_BACKTRACE_SYMBOLS
|
|
|
|
std::size_t demanglePos = line.find("(_Z");
|
|
|
|
if(demanglePos != std::string::npos) {
|
|
|
|
demanglePos++;
|
|
|
|
int len = std::min(line.find_first_of("+", demanglePos), line.find_first_of(")", demanglePos)) - demanglePos;
|
|
|
|
std::string funcName = line.substr(demanglePos, len);
|
2012-05-28 15:06:26 +02:00
|
|
|
line.replace(demanglePos, len, stdext::demangle_name(funcName.c_str()));
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ss << " " << i-1 << ": " << line << std::endl;
|
|
|
|
}
|
|
|
|
free(tracebackBuffer);
|
|
|
|
}
|
|
|
|
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.info(ss.str());
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-06-24 13:29:42 +02:00
|
|
|
std::string fileName = "crash_report.log";
|
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 << "== application crashed\n";
|
|
|
|
fout << ss.str();
|
|
|
|
fout << "\n";
|
|
|
|
fout.close();
|
2013-02-05 23:00:28 +01: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!");
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
signal(SIGILL, SIG_DFL);
|
|
|
|
signal(SIGSEGV, SIG_DFL);
|
|
|
|
signal(SIGFPE, SIG_DFL);
|
2012-03-22 13:57:43 +01:00
|
|
|
signal(SIGABRT, SIG_DFL);
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
2011-12-29 19:18:12 +01:00
|
|
|
void installCrashHandler()
|
|
|
|
{
|
2011-12-03 22:41:37 +01:00
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_sigaction = &crashHandler;
|
|
|
|
sigemptyset (&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
|
|
|
|
|
|
|
sigaction(SIGILL, &sa, NULL); // illegal instruction
|
|
|
|
sigaction(SIGSEGV, &sa, NULL); // segmentation fault
|
|
|
|
sigaction(SIGFPE, &sa, NULL); // floating-point exception
|
2012-03-22 13:57:43 +01:00
|
|
|
sigaction(SIGABRT, &sa, NULL); // process aborted (asserts)
|
2011-12-29 19:18:12 +01:00
|
|
|
}
|
2012-06-12 18:50:43 +02:00
|
|
|
|
|
|
|
#endif
|