aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/backtrace/cursors/libunwind/libunwind_cursor.cpp
blob: f814753034a9e7373c34b7e22f373ac523b8eeec (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
#include "libunwind_cursor.h"

namespace NYT::NBacktrace {

////////////////////////////////////////////////////////////////////////////////

TLibunwindCursor::TLibunwindCursor()
{
    if (unw_getcontext(&Context_) != 0) {
        Finished_ = true;
        return;
    }

    Initialize();
}

TLibunwindCursor::TLibunwindCursor(const unw_context_t& context)
    : Context_(context)
{
    Initialize();
}

void TLibunwindCursor::Initialize()
{
    if (unw_init_local(&Cursor_, &Context_) != 0) {
        Finished_ = true;
        return;
    }

    ReadCurrentIP();
}

bool TLibunwindCursor::IsFinished() const
{
    return Finished_;
}

const void* TLibunwindCursor::GetCurrentIP() const
{
    return CurrentIP_;
}

void TLibunwindCursor::MoveNext()
{
    if (Finished_) {
        return;
    }

    if (unw_step(&Cursor_) <= 0) {
        Finished_ = true;
        return;
    }

    ReadCurrentIP();
}

void TLibunwindCursor::ReadCurrentIP()
{
    unw_word_t ip = 0;
    if (unw_get_reg(&Cursor_, UNW_REG_IP, &ip) < 0) {
        Finished_ = true;
        return;
    }

    CurrentIP_ = reinterpret_cast<const void*>(ip);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NBacktrace