aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/call_stack.cpp
blob: 4a13b3a9785f22c528dbf0746bba80e556c6cd95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "winapi-debug" project.
// For details, see https://github.com/egor-tensin/winapi-debug.
// Distributed under the MIT License.

#include <winapi/debug.hpp>

#include <windows.h>

#include <algorithm>
#include <array>
#include <cstddef>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <system_error>

namespace winapi {
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(address::format(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 + "+" + address::format(offset));
}

std::string offset_from_module(const ModuleInfo& module, Address addr) {
    Address offset = 0;
    if (addr < module.get_offline_base())
        throw std::range_error{"invalid address in module"};
    offset = addr - module.get_offline_base();
    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

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};
}

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 << address::format(addr) << ' ' << pretty_print_address(dbghelp, addr) << '\n';
        return true;
    });
}

} // namespace winapi