From a9741c9cadcef7752ec0e1bcc0e1aba580477a5e Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 15 Jan 2020 03:58:41 +0300 Subject: split symbol.hpp --- include/pdb/symbol.hpp | 44 +++++--------------------------------- src/symbol.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 39 deletions(-) create mode 100644 src/symbol.cpp diff --git a/include/pdb/symbol.hpp b/include/pdb/symbol.hpp index 6c36092..2e24a5a 100644 --- a/include/pdb/symbol.hpp +++ b/include/pdb/symbol.hpp @@ -13,10 +13,9 @@ #include #include +#include #include #include -#include -#include #include namespace pdb { @@ -38,32 +37,18 @@ class SymbolInfo { public: typedef SYMBOL_INFO Impl; - SymbolInfo() : impl{*reinterpret_cast(buffer)} { - impl.SizeOfStruct = sizeof(Impl); - impl.MaxNameLen = MAX_SYM_NAME; - } - - explicit SymbolInfo(const Impl& impl) : SymbolInfo{} { - if (impl.SizeOfStruct != sizeof(impl)) - throw std::runtime_error{"invalid SYMBOL_INFO.SizeOfStruct"}; - const auto raw_size = calc_size(impl); - if (raw_size > sizeof(buffer)) - throw std::runtime_error{"SYMBOL_INFO is too large"}; - std::memcpy(buffer, &impl, raw_size); - } + SymbolInfo(); + explicit SymbolInfo(const Impl& impl); explicit operator Impl&() { return impl; } - explicit operator const Impl&() const { return impl; } Address get_displacement() const { return displacement; } - void set_displacement(Address new_value) { displacement = new_value; } std::string get_name() const { return {impl.Name, impl.NameLen}; } Address get_offline_base() const { return impl.ModBase; } - Address get_offline_address() const { return impl.Address; } symbol::Tag get_tag() const { return impl.Tag; } @@ -80,15 +65,7 @@ public: private: static constexpr std::size_t max_buffer_size = sizeof(Impl) + MAX_SYM_NAME - 1; - static std::size_t calc_size(const Impl& impl) { - try { - return SafeInt{impl.SizeOfStruct} + impl.NameLen - 1; - } catch (const SafeIntException&) { - throw std::runtime_error{"invalid SYMBOL_INFO size"}; - } - } - - unsigned char buffer[max_buffer_size] = {0}; + std::array buffer; Address displacement = 0; protected: @@ -110,21 +87,10 @@ class LineInfo { public: typedef IMAGEHLP_LINE64 Impl; - explicit LineInfo(const Impl& impl) - : file_path{impl.FileName}, line_number{cast_line_number(impl.LineNumber)} {} + explicit LineInfo(const Impl& impl); const std::string file_path; const unsigned long line_number; - -private: - static unsigned long cast_line_number(DWORD impl) { - unsigned long dest = 0; - - if (!SafeCast(impl, dest)) - throw std::runtime_error{"invalid line number"}; - - return dest; - } }; } // namespace pdb diff --git a/src/symbol.cpp b/src/symbol.cpp new file mode 100644 index 0000000..98ec427 --- /dev/null +++ b/src/symbol.cpp @@ -0,0 +1,57 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "PDB repository" project. +// For details, see https://github.com/egor-tensin/pdb-repo. +// Distributed under the MIT License. + +#include "pdb/all.hpp" + +#include + +#include +#include + +#include +#include +#include + +namespace pdb { +namespace { + +std::size_t calc_size(const SymbolInfo::Impl& impl) { + try { + return SafeInt{impl.SizeOfStruct} + impl.NameLen - 1; + } catch (const SafeIntException&) { + throw std::runtime_error{"invalid SYMBOL_INFO size"}; + } +} + +unsigned long cast_line_number(DWORD impl) { + unsigned long dest = 0; + + if (!SafeCast(impl, dest)) + throw std::runtime_error{"invalid line number"}; + + return dest; +} + +} // namespace + +SymbolInfo::SymbolInfo() : buffer{}, impl{*reinterpret_cast(buffer.data())} { + buffer.fill(0); + impl.SizeOfStruct = sizeof(Impl); + impl.MaxNameLen = MAX_SYM_NAME; +} + +SymbolInfo::SymbolInfo(const Impl& impl) : SymbolInfo{} { + if (impl.SizeOfStruct != sizeof(impl)) + throw std::runtime_error{"invalid SYMBOL_INFO.SizeOfStruct"}; + const auto impl_size = calc_size(impl); + if (impl_size > buffer.size()) + throw std::runtime_error{"SYMBOL_INFO is too large"}; + std::memcpy(buffer.data(), &impl, impl_size); +} + +LineInfo::LineInfo(const Impl& impl) + : file_path{impl.FileName}, line_number{cast_line_number(impl.LineNumber)} {} + +} // namespace pdb -- cgit v1.2.3