diff options
-rw-r--r-- | include/pdb/call_stack.hpp | 14 | ||||
-rw-r--r-- | src/call_stack.cpp | 29 | ||||
-rw-r--r-- | 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 <array> #include <cstddef> +#include <functional> +#include <ostream> #include <string> namespace pdb { @@ -31,6 +33,13 @@ public: static CallStack capture(); + using AddressCallback = std::function<bool(Address)>; + 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<Address, max_length> 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 <algorithm> #include <array> +#include <cstddef> +#include <ostream> #include <sstream> #include <stdexcept> #include <string> #include <system_error> namespace pdb { -namespace call_stack { namespace { template <typename T> @@ -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<void*, max_length> 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() { |