diff options
Diffstat (limited to '')
-rw-r--r-- | src/dbghelp.cpp | 19 | ||||
-rw-r--r-- | src/error.cpp | 68 | ||||
-rw-r--r-- | src/process.cpp | 7 | ||||
-rw-r--r-- | src/utils/file.cpp | 9 |
4 files changed, 19 insertions, 84 deletions
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 <pdb/all.hpp> +#include <winapi/error.hpp> #include <winapi/utf8.hpp> #include <dbghelp.h> @@ -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<ModuleInfo::Impl&>(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<DbgHelp::OnSymbol*>(&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_INFOW&>(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_INFOW&>(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 <Egor.Tensin@gmail.com> -// 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 <pdb/all.hpp> - -#include <winapi/utf8.hpp> - -#include <windows.h> - -#include <sstream> -#include <string> -#include <system_error> - -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<wchar_t*>(&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<int>(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 <pdb/all.hpp> +#include <winapi/error.hpp> #include <winapi/utf8.hpp> #include <windows.h> @@ -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 <pdb/all.hpp> +#include <winapi/error.hpp> #include <winapi/utf8.hpp> #include <windows.h> @@ -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}; } |