From 46ef2fbd49b2e48206a0554ae9a7d84f5961d817 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 15 May 2021 23:36:29 +0300 Subject: use error reporting from winapi-common --- src/dbghelp.cpp | 19 +++++++-------- src/error.cpp | 68 ------------------------------------------------------ src/process.cpp | 7 +++--- src/utils/file.cpp | 9 ++++---- 4 files changed, 19 insertions(+), 84 deletions(-) delete mode 100644 src/error.cpp (limited to 'src') diff --git a/src/dbghelp.cpp b/src/dbghelp.cpp index 06c1008..fb94835 100644 --- a/src/dbghelp.cpp +++ b/src/dbghelp.cpp @@ -5,6 +5,7 @@ #include +#include #include #include @@ -28,12 +29,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(), "SymInitialize"); + throw winapi::error::windows(GetLastError(), "SymInitialize"); } void clean_up(HANDLE id) { if (!SymCleanup(id)) - throw error::windows(GetLastError(), "SymCleanup"); + throw winapi::error::windows(GetLastError(), "SymCleanup"); } Address next_offline_base = 0x10000000; @@ -54,7 +55,7 @@ ModuleInfo get_module_info(HANDLE id, Address offline_base) { ModuleInfo info; if (!SymGetModuleInfoW64(id, offline_base, &static_cast(info))) - throw error::windows(GetLastError(), "SymGetModuleInfoW64"); + throw winapi::error::windows(GetLastError(), "SymGetModuleInfoW64"); return info; } @@ -87,7 +88,7 @@ void enum_symbols(HANDLE id, winapi::widen(mask).c_str(), &enum_symbols_callback, const_cast(&callback))) - throw error::windows(GetLastError(), "SymEnumSymbolsW"); + throw winapi::error::windows(GetLastError(), "SymEnumSymbolsW"); } } // namespace @@ -143,7 +144,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(), "SymLoadModule64"); + throw winapi::error::windows(GetLastError(), "SymLoadModule64"); return get_module_info(id, offline_base); } @@ -151,7 +152,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(), "SymEnumerateModulesW64"); + throw winapi::error::windows(GetLastError(), "SymEnumerateModulesW64"); } ModuleInfo DbgHelp::resolve_module(Address offline) const { @@ -181,7 +182,7 @@ SymbolInfo DbgHelp::resolve_symbol(Address offline) const { SymbolInfo symbol; if (!SymFromAddrW(id, offline, &displacement, &static_cast(symbol))) - throw error::windows(GetLastError(), "SymFromAddrW"); + throw winapi::error::windows(GetLastError(), "SymFromAddrW"); symbol.set_displacement(displacement); return symbol; @@ -191,7 +192,7 @@ SymbolInfo DbgHelp::resolve_symbol(const std::string& name) const { SymbolInfo symbol; if (!SymFromNameW(id, winapi::widen(name).c_str(), &static_cast(symbol))) - throw error::windows(GetLastError(), "SymFromNameW"); + throw winapi::error::windows(GetLastError(), "SymFromNameW"); return symbol; } @@ -204,7 +205,7 @@ LineInfo DbgHelp::resolve_line(Address offline) const { DWORD displacement = 0; if (!SymGetLineFromAddrW64(id, offline, &displacement, &impl)) - throw error::windows(GetLastError(), "SymGetLineFromAddrW64"); + throw winapi::error::windows(GetLastError(), "SymGetLineFromAddrW64"); return LineInfo{impl}; } diff --git a/src/error.cpp b/src/error.cpp deleted file mode 100644 index 8e370e8..0000000 --- a/src/error.cpp +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2017 Egor Tensin -// This file is part of the "winapi-debug" project. -// For details, see https://github.com/egor-tensin/winapi-debug. -// Distributed under the MIT License. - -#include - -#include - -#include - -#include -#include -#include - -namespace pdb { -namespace error { -namespace { - -std::wstring trim_trailing_newline(const std::wstring& s) { - const auto last_pos = s.find_last_not_of(L"\r\n"); - if (std::wstring::npos == last_pos) - return {}; - return s.substr(0, last_pos + 1); -} - -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 format_message(int code) { - wchar_t* buf; - - const auto len = FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast(&buf), - 0, - NULL); - - if (0 == len) { - LocalFree(buf); - return "Couldn't format the error message"; - } - - std::wstring msg{buf, len}; - LocalFree(buf); - return winapi::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 b70bf89..7763720 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -5,6 +5,7 @@ #include +#include #include #include @@ -24,7 +25,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(), "OpenProcess"); + throw winapi::error::windows(GetLastError(), "OpenProcess"); } return process; } @@ -62,7 +63,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(), "GetModuleFileNameW"); + throw winapi::error::windows(GetLastError(), "GetModuleFileNameW"); } if (ec == buffer.get_size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { @@ -92,7 +93,7 @@ std::string get_executable_path(const Handle& process, PathBuffer& buffer) { return get_executable_path(process, buffer); } - throw error::windows(GetLastError(), "QueryFullProcessImageNameW"); + throw winapi::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 af1de6e..fdc695f 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -5,6 +5,7 @@ #include +#include #include #include @@ -27,12 +28,12 @@ std::size_t get_size(const std::string& path) { NULL)}; if (handle.get() == INVALID_HANDLE_VALUE) - throw error::windows(GetLastError(), "CreateFileW"); + throw winapi::error::windows(GetLastError(), "CreateFileW"); LARGE_INTEGER size; if (!GetFileSizeEx(handle.get(), &size)) - throw error::windows(GetLastError(), "GetFileSizeEx"); + throw winapi::error::windows(GetLastError(), "GetFileSizeEx"); if (size.QuadPart < 0 || size.QuadPart > SIZE_MAX) throw std::runtime_error{"invalid file size"}; @@ -49,12 +50,12 @@ ID query_id(const std::string& path) { NULL)}; if (handle.get() == INVALID_HANDLE_VALUE) - throw error::windows(GetLastError(), "CreateFileW"); + throw winapi::error::windows(GetLastError(), "CreateFileW"); FILE_ID_INFO id; if (!GetFileInformationByHandleEx(handle.get(), FileIdInfo, &id, sizeof(id))) - throw error::windows(GetLastError(), "GetFileInformationByHandleEx"); + throw winapi::error::windows(GetLastError(), "GetFileInformationByHandleEx"); return {id}; } -- cgit v1.2.3