From 3463234ebdd59b83f5a63a44cd1f2f1916eb33be Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Tue, 19 May 2020 16:21:52 -0400 Subject: [PATCH] fixed terminate handler never being called ripped error message from uibase --- src/conf.cpp | 8 ++++---- src/context.cpp | 30 +++++++++++++++++++++++++++--- src/utility.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++------ src/utility.h | 7 +++++-- 4 files changed, 78 insertions(+), 15 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index 35bf1e0..b96d76b 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -280,10 +280,10 @@ parsed_option parse_option(const std::string& s) "bad option {}, must be [task:]section/key=value", s); } - std::string task = trim_copy(m[1]); - std::string section = trim_copy(m[2]); - std::string key = trim_copy(m[3]); - std::string value = trim_copy(m[4]); + std::string task = trim_copy(m[1].str()); + std::string section = trim_copy(m[2].str()); + std::string key = trim_copy(m[3].str()); + std::string value = trim_copy(m[4].str()); return {task, section, key, value}; } diff --git a/src/context.cpp b/src/context.cpp index c9625dd..acc6210 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -112,10 +112,34 @@ std::string prefix(context::reason r) return std::string(total, ' '); } -std::string error_message(DWORD e) +std::string error_message(DWORD id) { - return std::error_code( - static_cast(e), std::system_category()).message(); + wchar_t* message = nullptr; + + const auto ret = FormatMessageW( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + id, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&message), + 0, NULL); + + std::wstring s; + + std::wostringstream oss; + oss << L"0x" << std::hex << id; + + if (ret == 0 || !message) { + s = oss.str(); + } else { + s = trim_copy(message) + L" (" + oss.str() + L")"; + } + + LocalFree(message); + + return utf16_to_utf8(s); } std::string timestamp() diff --git a/src/utility.cpp b/src/utility.cpp index eed97e2..0236e86 100644 --- a/src/utility.cpp +++ b/src/utility.cpp @@ -24,13 +24,15 @@ static unsigned char sym_buffer[sizeof(SYMBOL_INFOW) + max_name_length]; static SYMBOL_INFOW* sym = (SYMBOL_INFOW*)sym_buffer; static wchar_t exception_message[exception_message_length + 1] = {}; +static LPTOP_LEVEL_EXCEPTION_FILTER g_previous_handler = nullptr; void dump_stacktrace(const wchar_t* what) { std::scoped_lock lock(g_output_mutex); std::wcerr - << L"\n\n*****************************\n" + << "\n\nmob has crashed\n" + << L"*****************************\n\n" << what << L"\n\n"; @@ -43,7 +45,7 @@ void dump_stacktrace(const wchar_t* what) SymInitializeW(process, NULL, TRUE); const std::size_t frame_count = CaptureStackBackTrace( - 3, max_frames, frame_addresses, nullptr); + 0, max_frames, frame_addresses, nullptr); for (std::size_t i=0; iExceptionRecord->ExceptionCode == 0xE06D7363) + { + if (g_previous_handler) + return g_previous_handler(ep);; + } + wchar_t* p = exception_message; std::size_t remaining = exception_message_length; @@ -172,8 +181,10 @@ LONG WINAPI unhandled_exception_handler(LPEXCEPTION_POINTERS ep) noexcept void set_thread_exception_handlers() { + g_previous_handler = SetUnhandledExceptionFilter( + mob::unhandled_exception_handler); + std::set_terminate(mob::terminate_handler); - SetUnhandledExceptionFilter(mob::unhandled_exception_handler); } @@ -343,7 +354,8 @@ std::vector split(const std::string& s, const std::string& seps) return v; } -void trim(std::string& s, const std::string& what) +template +void trim_impl(std::basic_string& s, std::basic_string_view what) { while (!s.empty()) { @@ -356,13 +368,37 @@ void trim(std::string& s, const std::string& what) } } -std::string trim_copy(const std::string& s, const std::string& what) +template +std::basic_string trim_copy_impl( + std::basic_string_view s, std::basic_string_view what) { - std::string c = s; + std::basic_string c(s); trim(c, what); return c; } + +void trim(std::string& s, std::string_view what) +{ + trim_impl(s, what); +} + +void trim(std::wstring& s, std::wstring_view what) +{ + trim_impl(s, what); +} + +std::string trim_copy(std::string_view s, std::string_view what) +{ + return trim_copy_impl(s, what); +} + +std::wstring trim_copy(std::wstring_view s, std::wstring_view what) +{ + return trim_copy_impl(s, what); +} + + std::string pad_right(std::string s, std::size_t n, char c) { if (s.size() < n) diff --git a/src/utility.h b/src/utility.h index 05f5ef5..1e917ff 100644 --- a/src/utility.h +++ b/src/utility.h @@ -237,8 +237,11 @@ std::vector split(const std::string& s, const std::string& sep); std::string pad_right(std::string s, std::size_t n, char c=' '); std::string pad_left(std::string s, std::size_t n, char c=' '); -void trim(std::string& s, const std::string& what=" \t\r\n"); -std::string trim_copy(const std::string& s, const std::string& what=" \t\r\n"); +void trim(std::string& s, std::string_view what=" \t\r\n"); +void trim(std::wstring& s, std::wstring_view what=L" \t\r\n"); + +std::string trim_copy(std::string_view s, std::string_view what=" \t\r\n"); +std::wstring trim_copy(std::wstring_view s, std::wstring_view what=L" \t\r\n"); std::wstring utf8_to_utf16(std::string_view s); std::string utf16_to_utf8(std::wstring_view ws);