From 5dc92e796291992dd81567cf5cb58624f58d0e43 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 21 Mar 2020 20:18:19 +0300 Subject: CallStack: overhaul the API --- include/pdb/call_stack.hpp | 14 +++++++++----- src/call_stack.cpp | 29 ++++++++++++++++++++++------- test/call_stack.cpp | 4 +--- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/include/pdb/call_stack.hpp b/include/pdb/call_stack.hpp index 968b1b8..f0e13af 100644 --- a/include/pdb/call_stack.hpp +++ b/include/pdb/call_stack.hpp @@ -14,6 +14,8 @@ #include #include +#include +#include #include namespace pdb { @@ -31,6 +33,13 @@ public: static CallStack capture(); + using AddressCallback = std::function; + bool for_each_address(const AddressCallback& callback) const; + + static std::string pretty_print_address(const DbgHelp& dbghelp, Address addr); + + void dump(std::ostream& os, const DbgHelp&) const; + const std::array frames; const std::size_t length; @@ -38,9 +47,4 @@ private: CallStack() = default; }; -namespace call_stack { - -std::string pretty_print_address(const DbgHelp& dbghelp, Address addr); - -} // namespace call_stack } // namespace pdb diff --git a/src/call_stack.cpp b/src/call_stack.cpp index 4edba46..d9e0637 100644 --- a/src/call_stack.cpp +++ b/src/call_stack.cpp @@ -11,13 +11,14 @@ #include #include +#include +#include #include #include #include #include namespace pdb { -namespace call_stack { namespace { template @@ -80,12 +81,6 @@ std::string resolve_and_format(const DbgHelp& dbghelp, Address addr) { } // 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 = @@ -98,4 +93,24 @@ CallStack CallStack::capture() { return {frames, length}; } +bool CallStack::for_each_address(const AddressCallback& callback) const { + for (std::size_t i = 0; i < length; ++i) { + if (!callback(frames[i])) { + return false; + } + } + return true; +} + +std::string CallStack::pretty_print_address(const DbgHelp& dbghelp, Address addr) { + return resolve_and_format(dbghelp, addr); +} + +void CallStack::dump(std::ostream& os, const DbgHelp& dbghelp) const { + for_each_address([&](Address addr) { + os << format_address(addr) << ' ' << pretty_print_address(dbghelp, addr) << '\n'; + return true; + }); +} + } // namespace pdb diff --git a/test/call_stack.cpp b/test/call_stack.cpp index e193b01..1f6ce37 100644 --- a/test/call_stack.cpp +++ b/test/call_stack.cpp @@ -8,9 +8,7 @@ namespace test { void call_stack() { const auto dbghelp = pdb::DbgHelp::current_process(); const auto call_stack = pdb::CallStack::capture(); - for (std::size_t i = 0; i < call_stack.length; ++i) - std::cout << pdb::format_address(call_stack.frames[i]) << ' ' - << pdb::call_stack::pretty_print_address(dbghelp, call_stack.frames[i]) << '\n'; + call_stack.dump(std::cout, dbghelp); } void __declspec(noinline) baz() { -- cgit v1.2.3