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 --- .gitmodules | 6 ++-- 3rdparty/winapi/common | 1 + 3rdparty/winapi/utf8 | 1 - CMakeLists.txt | 4 +-- include/pdb/all.hpp | 1 - include/pdb/error.hpp | 33 -------------------- src/dbghelp.cpp | 19 ++++++------ src/error.cpp | 68 ------------------------------------------ src/process.cpp | 7 +++-- src/utils/file.cpp | 9 +++--- test/unit_tests/CMakeLists.txt | 2 +- test/unit_tests/error.cpp | 23 -------------- 12 files changed, 26 insertions(+), 148 deletions(-) create mode 160000 3rdparty/winapi/common delete mode 160000 3rdparty/winapi/utf8 delete mode 100644 include/pdb/error.hpp delete mode 100644 src/error.cpp delete mode 100644 test/unit_tests/error.cpp diff --git a/.gitmodules b/.gitmodules index 9ea0616..4a8d34f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "cmake"] path = cmake url = https://github.com/egor-tensin/cmake-common.git -[submodule "3rdparty/winapi/utf8"] - path = 3rdparty/winapi/utf8 - url = https://github.com/egor-tensin/winapi-utf8.git +[submodule "3rdparty/winapi/common"] + path = 3rdparty/winapi/common + url = https://github.com/egor-tensin/winapi-common.git diff --git a/3rdparty/winapi/common b/3rdparty/winapi/common new file mode 160000 index 0000000..8deb061 --- /dev/null +++ b/3rdparty/winapi/common @@ -0,0 +1 @@ +Subproject commit 8deb061efcfc33f5606d6a39e565a6779ccc2a15 diff --git a/3rdparty/winapi/utf8 b/3rdparty/winapi/utf8 deleted file mode 160000 index 1d6ff27..0000000 --- a/3rdparty/winapi/utf8 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1d6ff271a9826c5f205fee0ef29b34f5e49190c9 diff --git a/CMakeLists.txt b/CMakeLists.txt index 20e528a..081e8e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,14 +27,14 @@ endif() include(cmake/common.cmake) find_package(Boost REQUIRED) -add_subdirectory(3rdparty/winapi/utf8) +add_subdirectory(3rdparty/winapi/common) file(GLOB_RECURSE winapi_debug_include "include/*.hpp") file(GLOB_RECURSE winapi_debug_src "src/*.cpp") add_library(winapi_debug ${winapi_debug_include} ${winapi_debug_src}) target_compile_definitions(winapi_debug PUBLIC _NO_CVCONST_H) target_include_directories(winapi_debug PUBLIC include/) -target_link_libraries(winapi_debug PRIVATE winapi_utf8) +target_link_libraries(winapi_debug PRIVATE winapi_common winapi_utf8) target_link_libraries(winapi_debug PUBLIC Boost::boost PRIVATE Boost::disable_autolinking) diff --git a/include/pdb/all.hpp b/include/pdb/all.hpp index c09af31..0f02c98 100644 --- a/include/pdb/all.hpp +++ b/include/pdb/all.hpp @@ -8,7 +8,6 @@ #include "address.hpp" #include "call_stack.hpp" #include "dbghelp.hpp" -#include "error.hpp" #include "handle.hpp" #include "module.hpp" #include "process.hpp" diff --git a/include/pdb/error.hpp b/include/pdb/error.hpp deleted file mode 100644 index 5ea885f..0000000 --- a/include/pdb/error.hpp +++ /dev/null @@ -1,33 +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. - -#pragma once - -#include - -#include -#include - -namespace pdb { -namespace error { - -class CategoryWindows : public std::error_category { -public: - CategoryWindows() = default; - - const char* name() const noexcept { return "Windows"; } - - std::string message(int) const; -}; - -inline const CategoryWindows& category_windows() { - static const CategoryWindows instance; - return instance; -} - -std::system_error windows(DWORD code, const char* function); - -} // namespace error -} // namespace pdb 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}; } diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt index f566c0b..93b7b8b 100644 --- a/test/unit_tests/CMakeLists.txt +++ b/test/unit_tests/CMakeLists.txt @@ -1,6 +1,6 @@ find_package(Boost REQUIRED COMPONENTS filesystem unit_test_framework) -add_executable(unit_tests main.cpp call_stack.cpp dbghelp.cpp error.cpp) +add_executable(unit_tests main.cpp call_stack.cpp dbghelp.cpp) target_link_libraries(unit_tests PRIVATE test_lib winapi_debug) target_link_libraries(unit_tests PRIVATE Boost::disable_autolinking diff --git a/test/unit_tests/error.cpp b/test/unit_tests/error.cpp deleted file mode 100644 index 9faf1e0..0000000 --- a/test/unit_tests/error.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2020 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 - -BOOST_AUTO_TEST_SUITE(error_tests) - -BOOST_AUTO_TEST_CASE(file_not_found) { - 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() -- cgit v1.2.3