aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/test_lib.cpp
blob: cb3d1d79499c73f14fdc4deabbd0b497e732e5d1 (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
// 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 "test_lib.hpp"

#include <pdb/all.hpp>

#include <iostream>

// Prevent frame pointer omission (FPO) and/or inlining.
#ifdef _MSC_VER
#pragma optimize("", off)
#endif

namespace test_ns {
namespace {

void do_print_call_stack() {
    const auto dbghelp = pdb::DbgHelp::current_process();
    const auto call_stack = pdb::CallStack::capture();
    call_stack.dump(std::cout, dbghelp);
}

void do_throw_call_stack() {
    throw pdb::CallStack::capture();
}

} // namespace

volatile int var = 42;

void baz(F f) {
    std::cout << "baz " << pdb::format_address(reinterpret_cast<void*>(&baz)) << '\n';
    f();
}

void bar(F f) {
    std::cout << "bar " << pdb::format_address(reinterpret_cast<void*>(&bar)) << '\n';
    baz(f);
}

void foo(F f) {
    std::cout << "foo " << pdb::format_address(reinterpret_cast<void*>(&foo)) << '\n';
    bar(f);
}

void print_call_stack() {
    foo(&do_print_call_stack);
}

void throw_call_stack() {
    foo(&do_throw_call_stack);
}

} // namespace test_ns