A lot of updates
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
#include <string>
|
||||
|
||||
namespace base64 {
|
||||
inline std::string encode(const std::string data) {
|
||||
static constexpr char sEncodingTable[] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
||||
'4', '5', '6', '7', '8', '9', '+', '/'
|
||||
};
|
||||
|
||||
size_t in_len = data.size();
|
||||
size_t out_len = 4 * ((in_len + 2) / 3);
|
||||
std::string ret(out_len, '\0');
|
||||
size_t i;
|
||||
char *p = const_cast<char*>(ret.c_str());
|
||||
|
||||
for (i = 0; i < in_len - 2; i += 3) {
|
||||
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
|
||||
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
|
||||
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
|
||||
*p++ = sEncodingTable[data[i + 2] & 0x3F];
|
||||
}
|
||||
if (i < in_len) {
|
||||
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
|
||||
if (i == (in_len - 1)) {
|
||||
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
|
||||
*p++ = '=';
|
||||
}
|
||||
else {
|
||||
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
|
||||
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
|
||||
}
|
||||
*p++ = '=';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline std::string decode(const std::string& input, std::string& out) {
|
||||
static constexpr unsigned char kDecodingTable[] = {
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
|
||||
64, 0, 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, 64, 64, 64, 64, 64,
|
||||
64, 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, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
|
||||
};
|
||||
|
||||
size_t in_len = input.size();
|
||||
if (in_len % 4 != 0) return "Input data size is not a multiple of 4";
|
||||
|
||||
size_t out_len = in_len / 4 * 3;
|
||||
if (input[in_len - 1] == '=') out_len--;
|
||||
if (input[in_len - 2] == '=') out_len--;
|
||||
|
||||
out.resize(out_len);
|
||||
|
||||
for (size_t i = 0, j = 0; i < in_len;) {
|
||||
uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
|
||||
uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
|
||||
uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
|
||||
uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
|
||||
|
||||
uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
|
||||
|
||||
if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
|
||||
if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
|
||||
if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
#include "crash_handler.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <client/linux/handler/exception_handler.h>
|
||||
#endif
|
||||
|
||||
#include <experimental/filesystem>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include "base64.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace tc;
|
||||
using namespace tc::signal;
|
||||
namespace fs = std::experimental::filesystem;
|
||||
|
||||
#ifndef WIN32
|
||||
unique_ptr<google_breakpad::ExceptionHandler> global_crash_handler;
|
||||
#else
|
||||
PVOID global_crash_handler = nullptr;
|
||||
#endif
|
||||
unique_ptr<CrashContext> crash_context;
|
||||
|
||||
bool crash_callback(const fs::path&, CrashContext*, const std::string&, bool);
|
||||
std::string replace_all(std::string data, const std::string& needle, const std::string& replacement) {
|
||||
size_t pos = data.find(needle);
|
||||
while(pos != std::string::npos) {
|
||||
data.replace(pos, needle.size(), replacement);
|
||||
pos = data.find(needle, pos + replacement.size());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/* taken from the updater */
|
||||
bool rename_or_move(const fs::path& source, const fs::path& target, std::string& error) {
|
||||
try {
|
||||
fs::rename(source, target);
|
||||
return true;
|
||||
} catch(const fs::filesystem_error& ex) {
|
||||
#ifndef WIN32
|
||||
if(ex.code() == errc::cross_device_link) {
|
||||
/* try with move command */
|
||||
char buffer[2048];
|
||||
auto length = snprintf(buffer, 2048, R"(%s "%s" "%s")", "mv", source.c_str(), target.c_str()); /* build the move command */
|
||||
if(length < 1 || length >= 2049) {
|
||||
error = "failed to prepare move command!";
|
||||
return false;
|
||||
}
|
||||
auto code = system(buffer);
|
||||
if(code != 0) {
|
||||
error = "move command resulted in " + to_string(code);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
error = "rename returned error code " + to_string(ex.code().value()) + " (" + ex.code().message() + ")";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void crash_execute_detached(const std::string& command_line) {
|
||||
cout << "Exec command " << command_line << endl;
|
||||
#ifdef WIN32
|
||||
STARTUPINFO si;
|
||||
PROCESS_INFORMATION pi;
|
||||
|
||||
// set the size of the structures
|
||||
ZeroMemory( &si, sizeof(si) );
|
||||
si.cb = sizeof(si);
|
||||
ZeroMemory( &pi, sizeof(pi) );
|
||||
|
||||
// start the program up
|
||||
auto result = CreateProcess(nullptr, // the path
|
||||
(LPSTR) command_line.c_str(), // Command line
|
||||
nullptr, // Process handle not inheritable
|
||||
nullptr, // Thread handle not inheritable
|
||||
FALSE, // Set handle inheritance to FALSE
|
||||
CREATE_NEW_CONSOLE , // No creation flags
|
||||
nullptr, // Use parent's environment block
|
||||
nullptr, // Use parent's starting directory
|
||||
&si, // Pointer to STARTUPINFO structure
|
||||
&pi // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
|
||||
);
|
||||
// Close process and thread handles.
|
||||
CloseHandle( pi.hProcess );
|
||||
CloseHandle( pi.hThread );
|
||||
#else
|
||||
auto full_command_line = command_line + "&";
|
||||
system(full_command_line.c_str());
|
||||
std::cout << "Executed crash command " << full_command_line << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
/* we want to prevent allocations while we're within a crash */
|
||||
const static std::string _message_fail = "failed write crash dump";
|
||||
const static std::string _message_success;
|
||||
bool breakpad_crash_callback(const google_breakpad::MinidumpDescriptor& descriptor, void* _context, bool succeeded) {
|
||||
return crash_callback(descriptor.path(), &*crash_context, succeeded ? _message_success : _message_fail, succeeded);
|
||||
}
|
||||
#else
|
||||
extern LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* apExceptionInfo);
|
||||
void win_crash_callback(const fs::path& source_file, const std::string& error, bool success) {
|
||||
crash_callback(source_file, &*crash_context, error, success);
|
||||
}
|
||||
#endif
|
||||
|
||||
bool crash_callback(const fs::path& source_file, CrashContext* context, const std::string& error_message, bool succeeded) {
|
||||
if(!succeeded) {
|
||||
/* crash dump error handling xD */
|
||||
|
||||
crash_execute_detached(replace_all(
|
||||
context->error_command_line,
|
||||
"%error_message%",
|
||||
error_message
|
||||
));
|
||||
} else {
|
||||
/* "normal" crash handling */
|
||||
auto target_directory = fs::u8path(context->crash_dump_folder);
|
||||
if(!fs::exists(target_directory)) {
|
||||
try {
|
||||
fs::create_directories(target_directory);
|
||||
} catch(const fs::filesystem_error& error) {
|
||||
crash_execute_detached(replace_all(
|
||||
context->error_command_line,
|
||||
"%error_message%",
|
||||
base64::encode("failed write move crash dump (" + source_file.string() + "): Target directory could not be created: " + error.what())
|
||||
));
|
||||
return succeeded;
|
||||
}
|
||||
}
|
||||
auto target_file = target_directory / ("crash_dump_" + context->component_name + "_" + source_file.filename().string());
|
||||
string error;
|
||||
if(!rename_or_move(source_file, target_file, error)) {
|
||||
crash_execute_detached(replace_all(
|
||||
context->error_command_line,
|
||||
"%error_message%",
|
||||
base64::encode("failed write move crash dump (" + source_file.string() + "): " + error)
|
||||
));
|
||||
return succeeded;
|
||||
}
|
||||
crash_execute_detached(replace_all(
|
||||
context->success_command_line,
|
||||
"%crash_path%",
|
||||
base64::encode(target_file.string())
|
||||
));
|
||||
}
|
||||
return succeeded;
|
||||
}
|
||||
|
||||
extern void create_minidump(struct _EXCEPTION_POINTERS* apExceptionInfo);
|
||||
|
||||
bool signal::setup(std::unique_ptr<CrashContext>& context) {
|
||||
#ifndef WIN32
|
||||
global_crash_handler = make_unique<google_breakpad::ExceptionHandler>(google_breakpad::MinidumpDescriptor("/tmp"), nullptr, breakpad_crash_callback, nullptr, true, -1);
|
||||
#else
|
||||
global_crash_handler = AddVectoredExceptionHandler(0, unhandled_handler); /* this only works! */
|
||||
#endif
|
||||
crash_context = move(context);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool signal::active() {
|
||||
return !!crash_context;
|
||||
}
|
||||
|
||||
void signal::finalize() {
|
||||
#ifndef WIN32
|
||||
global_crash_handler.reset();
|
||||
#else
|
||||
if(global_crash_handler)
|
||||
RemoveVectoredExceptionHandler(global_crash_handler);
|
||||
|
||||
global_crash_handler = nullptr;
|
||||
#endif
|
||||
crash_context.reset();
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace tc {
|
||||
namespace signal {
|
||||
struct CrashContext {
|
||||
std::string component_name;
|
||||
std::string crash_dump_folder;
|
||||
|
||||
std::string success_command_line; /* %crash_path% for crash dumps */
|
||||
std::string error_command_line; /* %error_message% for the error message */
|
||||
};
|
||||
|
||||
extern bool active();
|
||||
extern bool setup(std::unique_ptr<CrashContext>& /* crash context (will be moved) */);
|
||||
extern void finalize();
|
||||
}
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
#include <Windows.h>
|
||||
#include <DbgHelp.h>
|
||||
#include <tchar.h>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <strsafe.h>
|
||||
#include <iostream>
|
||||
|
||||
#include <experimental/filesystem>
|
||||
namespace fs = std::experimental::filesystem;
|
||||
|
||||
using namespace std;
|
||||
extern void win_crash_callback(const fs::path& source_file, const std::string& error, bool success);
|
||||
|
||||
typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(
|
||||
HANDLE hProcess,
|
||||
DWORD dwPid,
|
||||
HANDLE hFile,
|
||||
MINIDUMP_TYPE DumpType,
|
||||
CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
|
||||
CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
|
||||
CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
|
||||
);
|
||||
|
||||
fs::path generate_temp_file(std::string& error) {
|
||||
WCHAR szPath[MAX_PATH];
|
||||
WCHAR szFileName[MAX_PATH];
|
||||
DWORD dwBufferSize = MAX_PATH;
|
||||
SYSTEMTIME stLocalTime;
|
||||
|
||||
GetLocalTime( &stLocalTime );
|
||||
GetTempPathW( dwBufferSize, szPath );
|
||||
|
||||
CreateDirectoryW( szFileName, nullptr );
|
||||
StringCchPrintfW(szFileName, MAX_PATH, L"%s\\%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
|
||||
szPath,
|
||||
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
|
||||
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
|
||||
GetCurrentProcessId(), GetCurrentThreadId());
|
||||
return fs::path(szFileName);
|
||||
}
|
||||
|
||||
void create_minidump(struct _EXCEPTION_POINTERS* apExceptionInfo)
|
||||
{
|
||||
string error;
|
||||
HANDLE hDumpFile = nullptr;
|
||||
fs::path file_path;
|
||||
HMODULE mhLib = ::LoadLibrary(_T("dbghelp.dll"));
|
||||
if(!mhLib) {
|
||||
error = "failed to file dbghelp.dll";
|
||||
goto error_handling;
|
||||
}
|
||||
|
||||
auto pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(mhLib, "MiniDumpWriteDump");
|
||||
if(!pDump) {
|
||||
error = "failed to file find MiniDumpWriteDump handle";
|
||||
goto error_handling;
|
||||
}
|
||||
|
||||
file_path = generate_temp_file(error);
|
||||
hDumpFile = CreateFileW(file_path.wstring().c_str(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, nullptr, CREATE_ALWAYS, 0, nullptr);
|
||||
if(!hDumpFile) {
|
||||
error = "failed to open file";
|
||||
goto error_handling;
|
||||
}
|
||||
|
||||
_MINIDUMP_EXCEPTION_INFORMATION ExInfo{};
|
||||
ExInfo.ThreadId = ::GetCurrentThreadId();
|
||||
ExInfo.ExceptionPointers = apExceptionInfo;
|
||||
ExInfo.ClientPointers = FALSE;
|
||||
|
||||
if(!pDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &ExInfo, nullptr, nullptr)) {
|
||||
error = "failed to generate dump file";
|
||||
goto error_handling;
|
||||
}
|
||||
::CloseHandle(hDumpFile);
|
||||
win_crash_callback(file_path, error, true);
|
||||
return;
|
||||
|
||||
error_handling:
|
||||
if(hDumpFile) {
|
||||
::CloseHandle(hDumpFile);
|
||||
}
|
||||
win_crash_callback(file_path, error, false);
|
||||
}
|
||||
|
||||
LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* apExceptionInfo) {
|
||||
auto code = apExceptionInfo->ExceptionRecord->ExceptionCode;
|
||||
auto crash = false;
|
||||
switch(code) {
|
||||
case EXCEPTION_ACCESS_VIOLATION:
|
||||
case EXCEPTION_ILLEGAL_INSTRUCTION:
|
||||
case EXCEPTION_STACK_OVERFLOW:
|
||||
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
||||
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
||||
case EXCEPTION_IN_PAGE_ERROR:
|
||||
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
|
||||
crash = true;
|
||||
break;
|
||||
default:
|
||||
crash = false;
|
||||
}
|
||||
|
||||
if(!crash)
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
|
||||
create_minidump(apExceptionInfo);
|
||||
exit(1);
|
||||
return EXCEPTION_EXECUTE_HANDLER;
|
||||
}
|
||||
Reference in New Issue
Block a user