Misc: Purge unused code and report startup error to host

This commit is contained in:
Stenzek
2024-05-05 21:32:04 +10:00
parent ca3cfbaa99
commit fa104acdd1
17 changed files with 146 additions and 179 deletions

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "crash_handler.h"
#include "dynamic_library.h"
#include "file_system.h"
#include "string_util.h"
#include <cinttypes>
@ -74,7 +75,7 @@ static bool WriteMinidump(HMODULE hDbgHelp, HANDLE hFile, HANDLE hProcess, DWORD
}
static std::wstring s_write_directory;
static HMODULE s_dbghelp_module = nullptr;
static DynamicLibrary s_dbghelp_module;
static PVOID s_veh_handle = nullptr;
static bool s_in_crash_handler = false;
@ -115,8 +116,8 @@ static void WriteMinidumpAndCallstack(PEXCEPTION_POINTERS exi)
MiniDumpWithThreadInfo | MiniDumpWithIndirectlyReferencedMemory);
const HANDLE hMinidumpFile = CreateFileW(filename, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
if (hMinidumpFile == INVALID_HANDLE_VALUE ||
!WriteMinidump(s_dbghelp_module, hMinidumpFile, GetCurrentProcess(), GetCurrentProcessId(), GetCurrentThreadId(),
exi, minidump_type))
!WriteMinidump(static_cast<HMODULE>(s_dbghelp_module.GetHandle()), hMinidumpFile, GetCurrentProcess(),
GetCurrentProcessId(), GetCurrentThreadId(), exi, minidump_type))
{
static const char error_message[] = "Failed to write minidump file.\n";
if (hFile != INVALID_HANDLE_VALUE)
@ -170,7 +171,9 @@ bool CrashHandler::Install()
{
// load dbghelp at install/startup, that way we're not LoadLibrary()'ing after a crash
// .. because that probably wouldn't go down well.
s_dbghelp_module = StackWalker::LoadDbgHelpLibrary();
HMODULE mod = StackWalker::LoadDbgHelpLibrary();
if (mod)
s_dbghelp_module.Adopt(mod);
s_veh_handle = AddVectoredExceptionHandler(0, ExceptionHandler);
return (s_veh_handle != nullptr);
@ -189,21 +192,6 @@ void CrashHandler::WriteDumpForCaller()
WriteMinidumpAndCallstack(nullptr);
}
void CrashHandler::Uninstall()
{
if (s_veh_handle)
{
RemoveVectoredExceptionHandler(s_veh_handle);
s_veh_handle = nullptr;
}
if (s_dbghelp_module)
{
FreeLibrary(s_dbghelp_module);
s_dbghelp_module = nullptr;
}
}
#elif defined(ENABLE_LIBBACKTRACE)
#include <backtrace.h>
@ -244,8 +232,8 @@ const char* CrashHandler::GetSignalName(int signal_no)
{
switch (signal_no)
{
// Don't need to list all of them, there's only a couple we register.
// clang-format off
// Don't need to list all of them, there's only a couple we register.
// clang-format off
case SIGSEGV: return "SIGSEGV";
case SIGBUS: return "SIGBUS";
default: return "UNKNOWN";
@ -392,11 +380,6 @@ void CrashHandler::WriteDumpForCaller()
{
}
void CrashHandler::Uninstall()
{
// We can't really unchain the signal handlers... so, YOLO.
}
#else
bool CrashHandler::Install()
@ -412,8 +395,4 @@ void CrashHandler::WriteDumpForCaller()
{
}
void CrashHandler::Uninstall()
{
}
#endif

View File

@ -8,5 +8,4 @@ namespace CrashHandler {
bool Install();
void SetWriteDirectory(std::string_view dump_directory);
void WriteDumpForCaller();
void Uninstall();
} // namespace CrashHandler

View File

@ -101,6 +101,15 @@ bool DynamicLibrary::Open(const char* filename, Error* error)
#endif
}
void DynamicLibrary::Adopt(void* handle)
{
AssertMsg(handle, "Handle is valid");
Close();
m_handle = handle;
}
void DynamicLibrary::Close()
{
if (!IsOpen())

View File

@ -45,6 +45,9 @@ public:
/// Returns true if the library was loaded and can be used.
bool Open(const char* filename, Error* error);
/// Adopts, or takes ownership of an existing opened library.
void Adopt(void* handle);
/// Unloads the library, any function pointers from this library are no longer valid.
void Close();
@ -61,6 +64,9 @@ public:
return *ptr != nullptr;
}
/// Returns the opaque OS-specific handle.
void* GetHandle() const { return m_handle; }
/// Move assignment, transfer ownership.
DynamicLibrary& operator=(DynamicLibrary&& move);

View File

@ -138,8 +138,8 @@ void Error::SetHResult(std::string_view prefix, long err)
static_cast<DWORD>(std::size(buf)), nullptr);
if (r > 0)
{
m_description =
fmt::format("{}HRESULT {:08X}: {}", prefix, err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
m_description = fmt::format("{}HRESULT {:08X}: {}", prefix, static_cast<unsigned>(err),
StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
}
else
{