From 1cbd0a3b824d1821549b4c7ede688f500de25443 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 20 Sep 2020 00:09:34 +0300 Subject: pdb::error: include function name in error message --- src/dbghelp.cpp | 18 +++++++++--------- src/error.cpp | 22 ++++++++++++++++++++-- src/process.cpp | 6 +++--- src/utils/file.cpp | 8 ++++---- 4 files changed, 36 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/dbghelp.cpp b/src/dbghelp.cpp index 4b8df7a..c9db499 100644 --- a/src/dbghelp.cpp +++ b/src/dbghelp.cpp @@ -28,12 +28,12 @@ void initialize(HANDLE id, bool invade_current_process) { set_dbghelp_options(); if (!SymInitialize(id, NULL, invade_current_process ? TRUE : FALSE)) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymInitialize"); } void clean_up(HANDLE id) { if (!SymCleanup(id)) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymCleanup"); } Address next_offline_base = 0x10000000; @@ -50,7 +50,7 @@ ModuleInfo get_module_info(HANDLE id, Address offline_base) { ModuleInfo info; if (!SymGetModuleInfoW64(id, offline_base, &static_cast(info))) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymGetModuleInfoW64"); return info; } @@ -83,7 +83,7 @@ void enum_symbols(HANDLE id, boost::nowide::widen(mask).c_str(), &enum_symbols_callback, const_cast(&callback))) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymEnumSymbolsW"); } } // namespace @@ -135,7 +135,7 @@ ModuleInfo DbgHelp::load_pdb(const std::string& path) const { SymLoadModule64(id, NULL, _path.data(), NULL, gen_next_offline_base(size), size); if (!offline_base) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymLoadModule64"); return get_module_info(id, offline_base); } @@ -143,7 +143,7 @@ ModuleInfo DbgHelp::load_pdb(const std::string& path) const { void DbgHelp::enum_modules(const OnModule& callback) const { ModuleEnumerator enumerator{id, callback}; if (!SymEnumerateModulesW64(id, &enum_modules_callback, &enumerator)) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymEnumerateModulesW64"); } ModuleInfo DbgHelp::resolve_module(Address offline) const { @@ -173,7 +173,7 @@ SymbolInfo DbgHelp::resolve_symbol(Address offline) const { SymbolInfo symbol; if (!SymFromAddrW(id, offline, &displacement, &static_cast(symbol))) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymFromAddrW"); symbol.set_displacement(displacement); return symbol; @@ -183,7 +183,7 @@ SymbolInfo DbgHelp::resolve_symbol(const std::string& name) const { SymbolInfo symbol; if (!SymFromNameW(id, boost::nowide::widen(name).c_str(), &static_cast(symbol))) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymFromNameW"); return symbol; } @@ -196,7 +196,7 @@ LineInfo DbgHelp::resolve_line(Address offline) const { DWORD displacement = 0; if (!SymGetLineFromAddrW64(id, offline, &displacement, &impl)) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "SymGetLineFromAddrW64"); return LineInfo{impl}; } diff --git a/src/error.cpp b/src/error.cpp index 74cf395..368feb7 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -9,7 +9,9 @@ #include +#include #include +#include namespace pdb { namespace error { @@ -22,9 +24,13 @@ std::wstring trim_trailing_newline(const std::wstring& s) { return s.substr(0, last_pos + 1); } -} // namespace +std::string build_what(DWORD code, const char* function) { + std::ostringstream what; + what << "Function " << function << " failed with error code " << code; + return what.str(); +} -std::string CategoryWindows::message(int code) const { +std::string format_message(int code) { wchar_t* buf; const auto len = FormatMessageW( @@ -46,5 +52,17 @@ std::string CategoryWindows::message(int code) const { return boost::nowide::narrow(trim_trailing_newline(msg)); } +} // namespace + +std::string CategoryWindows::message(int code) const { + return format_message(code); +} + +std::system_error windows(DWORD code, const char* function) { + static_assert(sizeof(DWORD) == sizeof(int), "Aren't DWORDs the same size as ints?"); + return std::system_error{ + static_cast(code), category_windows(), build_what(code, function)}; +} + } // namespace error } // namespace pdb diff --git a/src/process.cpp b/src/process.cpp index 08319fa..281ee86 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -24,7 +24,7 @@ constexpr DWORD permissions = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ; Handle open_process(DWORD id) { Handle process{OpenProcess(permissions, FALSE, id)}; if (!process) { - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "OpenProcess"); } return process; } @@ -61,7 +61,7 @@ std::string get_current_executable_path(PathBuffer& buffer) { const auto ec = ::GetModuleFileNameW(NULL, buffer.get_data(), buffer.get_size()); if (ec == 0) { - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "GetModuleFileNameW"); } if (ec == buffer.get_size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { @@ -91,7 +91,7 @@ std::string get_executable_path(const Handle& process, PathBuffer& buffer) { return get_executable_path(process, buffer); } - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "QueryFullProcessImageNameW"); } std::string get_executable_path(const Handle& process) { diff --git a/src/utils/file.cpp b/src/utils/file.cpp index 8380fe9..fb106ec 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -27,12 +27,12 @@ std::size_t get_size(const std::string& path) { NULL)}; if (handle.get() == INVALID_HANDLE_VALUE) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "CreateFileW"); LARGE_INTEGER size; if (!GetFileSizeEx(handle.get(), &size)) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "GetFileSizeEx"); std::size_t result = 0; @@ -52,12 +52,12 @@ ID query_id(const std::string& path) { NULL)}; if (handle.get() == INVALID_HANDLE_VALUE) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "CreateFileW"); FILE_ID_INFO id; if (!GetFileInformationByHandleEx(handle.get(), FileIdInfo, &id, sizeof(id))) - throw error::windows(GetLastError()); + throw error::windows(GetLastError(), "GetFileInformationByHandleEx"); return {id}; } -- cgit v1.2.3