aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--.appveyor.yml2
-rw-r--r--include/pdb/all.hpp1
-rw-r--r--include/pdb/call_stack.hpp46
-rw-r--r--include/pdb/dbghelp.hpp4
-rw-r--r--src/call_stack.cpp101
-rw-r--r--src/dbghelp.cpp12
-rw-r--r--test/CMakeLists.txt11
-rw-r--r--test/call_stack.cpp41
8 files changed, 212 insertions, 6 deletions
diff --git a/.appveyor.yml b/.appveyor.yml
index c38ef60..7ae89eb 100644
--- a/.appveyor.yml
+++ b/.appveyor.yml
@@ -41,6 +41,8 @@ test_script:
- '"%install_dir%\bin\enum_symbols" --pdb "%install_dir%\bin\foobar.pdb" --mask "foobar!foobar_ns*"'
- '"%install_dir%\bin\enum_symbols" --pdb "%install_dir%\bin\foobar.pdb" --functions --mask "foobar_ns*"'
+ - '"%install_dir%\bin\call_stack"'
+
for:
# Only build Release builds on master to speed things up:
- branches:
diff --git a/include/pdb/all.hpp b/include/pdb/all.hpp
index 84be09e..36a68d3 100644
--- a/include/pdb/all.hpp
+++ b/include/pdb/all.hpp
@@ -6,6 +6,7 @@
#pragma once
#include "address.hpp"
+#include "call_stack.hpp"
#include "dbghelp.hpp"
#include "error.hpp"
#include "handle.hpp"
diff --git a/include/pdb/call_stack.hpp b/include/pdb/call_stack.hpp
new file mode 100644
index 0000000..7f0c8b0
--- /dev/null
+++ b/include/pdb/call_stack.hpp
@@ -0,0 +1,46 @@
+// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "PDB repository" project.
+// For details, see https://github.com/egor-tensin/pdb-repo.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include "address.hpp"
+#include "dbghelp.hpp"
+
+#include <SafeInt.hpp>
+
+#include <Windows.h>
+
+#include <array>
+#include <cstddef>
+#include <string>
+
+namespace pdb {
+
+class CallStack {
+public:
+ static constexpr std::size_t frames_to_skip = 0;
+ static constexpr std::size_t frames_to_capture = 62;
+
+ // Imposed by CaptureStackBackTrace:
+ static constexpr std::size_t max_length = 62;
+
+ static_assert(frames_to_skip + frames_to_capture <= max_length,
+ "Call stack length is too large");
+
+ static CallStack capture();
+
+ const std::array<Address, max_length> frames;
+ const std::size_t length;
+
+private:
+ CallStack() = default;
+};
+
+namespace call_stack {
+
+std::string pretty_print_address(const DbgHelp& dbghelp, Address addr);
+
+} // namespace call_stack
+} // namespace pdb
diff --git a/include/pdb/dbghelp.hpp b/include/pdb/dbghelp.hpp
index 2809eac..db9638e 100644
--- a/include/pdb/dbghelp.hpp
+++ b/include/pdb/dbghelp.hpp
@@ -18,7 +18,7 @@ namespace pdb {
class DbgHelp {
public:
- DbgHelp();
+ DbgHelp(bool invade_current_process = false);
~DbgHelp();
void close();
@@ -28,6 +28,8 @@ public:
typedef std::function<void(const ModuleInfo&)> OnModule;
void enum_modules(const OnModule&) const;
+ ModuleInfo resolve_module(Address) const;
+
typedef std::function<void(const SymbolInfo&)> OnSymbol;
static constexpr auto all_symbols = "*!*";
void enum_symbols(const ModuleInfo&, const std::string& mask, const OnSymbol&) const;
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 <Egor.Tensin@gmail.com>
+// 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 <SafeInt.hpp>
+
+#include <Windows.h>
+
+#include <algorithm>
+#include <array>
+#include <sstream>
+#include <stdexcept>
+#include <string>
+#include <system_error>
+
+namespace pdb {
+namespace call_stack {
+namespace {
+
+template <typename T>
+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<void*, max_length> frames_impl{nullptr};
+ const auto length =
+ CaptureStackBackTrace(frames_to_skip, frames_to_capture, frames_impl.data(), NULL);
+
+ std::array<Address, max_length> frames;
+ std::transform(frames_impl.cbegin(), frames_impl.cend(), frames.begin(), [](void* addr) {
+ return reinterpret_cast<Address>(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 {
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 619a51e..656caa9 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,4 +1,13 @@
-set(CC_STATIC_RUNTIME OFF)
+add_executable(call_stack call_stack.cpp)
+target_link_libraries(call_stack PRIVATE pdb_repo)
+
+install(TARGETS call_stack RUNTIME DESTINATION bin)
+if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
+ install(FILES "$<TARGET_PDB_FILE:call_stack>" DESTINATION bin OPTIONAL)
+endif()
+
+set(CC_STATIC_RUNTIME OFF) # Reduce the number of symbols in this test utility
+
add_executable(foobar foobar.cpp)
install(TARGETS foobar RUNTIME DESTINATION bin)
diff --git a/test/call_stack.cpp b/test/call_stack.cpp
new file mode 100644
index 0000000..c1fb0fd
--- /dev/null
+++ b/test/call_stack.cpp
@@ -0,0 +1,41 @@
+#include "pdb/all.hpp"
+
+#include <exception>
+#include <iostream>
+
+namespace test {
+
+void call_stack() {
+ pdb::DbgHelp dbghelp{true};
+ 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';
+}
+
+void __declspec(noinline) baz() {
+ std::cout << "baz\n";
+ call_stack();
+}
+
+void __declspec(noinline) bar() {
+ std::cout << "bar\n";
+ baz();
+}
+
+void __declspec(noinline) foo() {
+ std::cout << "foo\n";
+ bar();
+}
+
+} // namespace test
+
+int main() {
+ try {
+ test::foo();
+ } catch (const std::exception& e) {
+ std::cerr << e.what() << '\n';
+ return 1;
+ }
+ return 0;
+}