diff options
Diffstat (limited to '')
-rw-r--r-- | include/pdb/error.hpp | 6 | ||||
-rw-r--r-- | src/dbghelp.cpp | 18 | ||||
-rw-r--r-- | src/error.cpp | 22 | ||||
-rw-r--r-- | src/process.cpp | 6 | ||||
-rw-r--r-- | src/utils/file.cpp | 8 | ||||
-rw-r--r-- | test/unit_tests/error.cpp | 6 |
6 files changed, 41 insertions, 25 deletions
diff --git a/include/pdb/error.hpp b/include/pdb/error.hpp index beb0489..c927719 100644 --- a/include/pdb/error.hpp +++ b/include/pdb/error.hpp @@ -27,11 +27,7 @@ inline const CategoryWindows& category_windows() { return instance; } -inline std::system_error windows(DWORD code) { - static_assert(sizeof(DWORD) == sizeof(int), "Aren't DWORDs the same size as ints?"); - - return std::system_error{static_cast<int>(code), category_windows()}; -} +std::system_error windows(DWORD code, const char* function); } // namespace error } // namespace pdb 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<ModuleInfo::Impl&>(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<DbgHelp::OnSymbol*>(&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_INFOW&>(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_INFOW&>(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 <windows.h> +#include <sstream> #include <string> +#include <system_error> 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<int>(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}; } diff --git a/test/unit_tests/error.cpp b/test/unit_tests/error.cpp index f18a2e7..a1ae977 100644 --- a/test/unit_tests/error.cpp +++ b/test/unit_tests/error.cpp @@ -14,8 +14,10 @@ BOOST_AUTO_TEST_SUITE(error_tests) BOOST_AUTO_TEST_CASE(file_not_found) { - const std::string actual{pdb::error::windows(ERROR_FILE_NOT_FOUND).what()}; - BOOST_TEST(actual == "The system cannot find the file specified."); + const std::string actual{pdb::error::windows(ERROR_FILE_NOT_FOUND, "CreateFileW").what()}; + BOOST_TEST(actual == + "Function CreateFileW failed with error code 2: The system cannot find the file " + "specified."); } BOOST_AUTO_TEST_SUITE_END() |