From 3dc0b9a3ae90dcac2f1150e6b2dfb0a5a310355b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 15 Jan 2020 19:20:50 +0300 Subject: add call stack collection support --- src/call_stack.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/dbghelp.cpp | 12 ++++--- 2 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 src/call_stack.cpp (limited to 'src') diff --git a/src/call_stack.cpp b/src/call_stack.cpp new file mode 100644 index 0000000..e421d34 --- /dev/null +++ b/src/call_stack.cpp @@ -0,0 +1,101 @@ +// 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 +#include +#include + +namespace pdb { +namespace call_stack { +namespace { + +template +static std::string put_between_brackets(const T& x) { + std::ostringstream oss; + oss << "[" << x << "]"; + return oss.str(); +} + +std::string format_address_fallback(Address addr) { + return put_between_brackets(format_address(addr)); +} + +std::string offset_from(const std::string& thing, Address offset) { + if (offset == 0) + return put_between_brackets(thing); + else + return put_between_brackets(thing + "+" + format_address(offset)); +} + +std::string offset_from_module(const ModuleInfo& module, Address addr) { + Address offset = 0; + if (!SafeSubtract(addr, module.get_offline_base(), offset)) + throw std::range_error{"invalid address in module"}; + return offset_from(module.get_name(), offset); +} + +std::string offset_from_symbol(const SymbolInfo& symbol) { + return offset_from(symbol.get_name(), symbol.get_displacement()); +} + +std::string offset_from_symbol(const ModuleInfo& module, const SymbolInfo& symbol) { + return offset_from(module.get_name() + "!" + symbol.get_name(), symbol.get_displacement()); +} + +std::string resolve_and_format(const DbgHelp& dbghelp, const SymbolInfo& symbol, Address addr) { + try { + const auto module = dbghelp.resolve_module(addr); + return offset_from_symbol(module, symbol); + } catch (const std::system_error&) { + return offset_from_symbol(symbol); + } +} + +std::string resolve_and_format(const DbgHelp& dbghelp, Address addr) { + try { + const auto symbol = dbghelp.resolve_symbol(addr); + return resolve_and_format(dbghelp, symbol, addr); + } catch (const std::system_error&) { + try { + const auto module = dbghelp.resolve_module(addr); + return offset_from_module(module, addr); + } catch (const std::system_error&) { + return format_address_fallback(addr); + } + } + assert(false); + return {}; +} + +} // namespace + +std::string pretty_print_address(const DbgHelp& dbghelp, Address addr) { + return resolve_and_format(dbghelp, addr); +} + +} // namespace call_stack + +CallStack CallStack::capture() { + std::array frames_impl{nullptr}; + const auto length = + CaptureStackBackTrace(frames_to_skip, frames_to_capture, frames_impl.data(), NULL); + + std::array frames; + std::transform(frames_impl.cbegin(), frames_impl.cend(), frames.begin(), [](void* addr) { + return reinterpret_cast
(addr); + }); + return {frames, length}; +} + +} // namespace pdb diff --git a/src/dbghelp.cpp b/src/dbghelp.cpp index 399271e..0105682 100644 --- a/src/dbghelp.cpp +++ b/src/dbghelp.cpp @@ -23,10 +23,10 @@ void set_dbghelp_options() { SymSetOptions(SymGetOptions() | SYMOPT_DEBUG | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); } -void initialize(HANDLE id) { +void initialize(HANDLE id, bool invade_current_process) { set_dbghelp_options(); - if (!SymInitialize(id, NULL, FALSE)) + if (!SymInitialize(id, NULL, invade_current_process ? TRUE : FALSE)) throw error::windows(GetLastError()); } @@ -87,8 +87,8 @@ void enum_symbols(HANDLE id, } // namespace -DbgHelp::DbgHelp() { - initialize(id); +DbgHelp::DbgHelp(bool invade_current_process) { + initialize(id, invade_current_process); } DbgHelp::~DbgHelp() { @@ -132,6 +132,10 @@ void DbgHelp::enum_modules(const OnModule& callback) const { throw error::windows(GetLastError()); } +ModuleInfo DbgHelp::resolve_module(Address offline) const { + return get_module_info(id, offline); +} + void DbgHelp::enum_symbols(const ModuleInfo& module, const std::string& mask, const OnSymbol& callback) const { -- cgit v1.2.3