summaryrefslogtreecommitdiffstats
path: root/src/libnetdata/bt/bt.cc
blob: d462d7eaf246932fa2d87e42e4f665c06f11e4f1 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "bt.h"

#include <backtrace.h>
#include <backtrace-supported.h>

#include <algorithm>
#include <cstdio>
#include <fstream>
#include <mutex>
#include <sstream>
#include <unordered_map>
#include <queue>

static backtrace_state *State = nullptr;

static int pcinfo_callback(void *data, uintptr_t pc, const char *filename, int lineno, const char *function)
{
    std::ostringstream *OS = static_cast<std::ostringstream*>(data);

    if (function)
        *OS << function << "() @ ";

    if (filename)
        *OS << filename << ":" << lineno;
    else
        *OS << pc << " (information not available)";

    *OS << "\n";
    return 0;
}

static void error_callback(void *data, const char *msg, int errnum)
{
    std::ostringstream *OS = static_cast<std::ostringstream*>(data);
    *OS << "Backtrace error: " << msg << " (error number " << errnum << ")\n";
}

struct UuidKey
{
    const uuid_t *Inner;

    bool operator==(const UuidKey& Other) const
    {
        return uuid_compare(*Inner, *Other.Inner) == 0;
    }
};

namespace std
{
    template<>
    struct hash<UuidKey>
    {
        size_t operator()(const UuidKey& Key) const
        {
            return XXH64(*Key.Inner, sizeof(uuid_t), 0);
        }
    };
}

class StackTrace
{
public:
    static const size_t MAX_ITEMS = 128;
    uintptr_t PCs[MAX_ITEMS] = { 0 };
    size_t Items = 0;

    void append(uintptr_t PC)
    {
        assert(Items < MAX_ITEMS);
        PCs[Items++] = PC;
    }

    bool operator==(const StackTrace& Other) const
    {
        if (Items != Other.Items)
            return false;

        for (size_t i = 0; i < Items; i++)
            if (PCs[i] != Other.PCs[i])
                return false;

        return true;
    }

    void dump(std::ostream &OS) const
    {
        for (size_t i = 0; i < Items; ++i)
            backtrace_pcinfo(State, PCs[i], pcinfo_callback, error_callback, &OS);
        OS << std::endl;
    }
};

namespace std
{
    template<>
    struct hash<StackTrace>
    {
        size_t operator()(const StackTrace& ST) const
        {
            return XXH64(ST.PCs, ST.Items * sizeof(uintptr_t), 0);
        }
    };
}

static std::vector<std::pair<uint64_t, StackTrace>> InternedStackTraces;

static size_t stackTraceID(const StackTrace &ST)
{
    std::hash<StackTrace> hasher;
    uint64_t K = hasher(ST);

    auto Pred = [](const std::pair<uint64_t, StackTrace>& a, const std::pair<uint64_t, StackTrace>& b) {
        return a.first < b.first;
    };

    std::pair<uint64_t, StackTrace> P(K, ST);
    auto It = std::lower_bound(InternedStackTraces.begin(), InternedStackTraces.end(), P, Pred);
    if (It != InternedStackTraces.end() && It->first == K)
        return K;

    InternedStackTraces.insert(It, {K, ST});
    return K;
}

static const StackTrace &lookupStackTrace(uint64_t ID)
{
    auto Pred =  [](const std::pair<uint64_t, StackTrace>& element, uint64_t value) {
        return element.first < value;
    };
    auto It = std::lower_bound(InternedStackTraces.begin(), InternedStackTraces.end(), ID, Pred);

    return It->second;
}

static std::unordered_map<UuidKey, std::queue<uint64_t>> USTs;
static std::mutex Mutex;

static int simple_callback(void *data, uintptr_t pc)
{
    StackTrace *ST = static_cast<StackTrace*>(data);
    if (ST->Items == StackTrace::MAX_ITEMS)
        fatal("StackTrace too big...");

    ST->append(pc);
    return 0;
}

const char *bt_path = NULL;

void bt_init(const char *exepath, const char *cache_dir)
{
    State = backtrace_create_state(exepath, 1, nullptr, nullptr);

    char buf[FILENAME_MAX + 1];
    snprintfz(buf, FILENAME_MAX, "%s/%s", cache_dir, "bt.log");
    bt_path = strdupz(buf);
}

void bt_collect(const uuid_t *uuid)
{
    // Enable collection on 1/16th of UUIDs to save on CPU and RAM consumption
    if (*uuid[0] != 0x0A)
        return;

    {
        std::lock_guard<std::mutex> lock(Mutex);

        UuidKey UK = { uuid };

        auto& Q = USTs[UK];
        if (Q.size() == 128)
            Q.pop();

        StackTrace ST;
        backtrace_simple(State, 1, simple_callback, error_callback, &ST);
        Q.push(stackTraceID(ST));
    }
}

void bt_dump(const uuid_t *uuid)
{
    std::lock_guard<std::mutex> lock(Mutex);

    UuidKey UK = { uuid };

    auto It = USTs.find(UK);
    if (It == USTs.end())
        return;

    std::queue<uint64_t> Q = It->second;
    std::ostringstream OS;

    size_t Idx = 0;
    while (!Q.empty())
    {
        OS << "Stack trace " << ++Idx << "/" << It->second.size() << ":\n";
        const StackTrace& ST = lookupStackTrace(Q.front());
        ST.dump(OS);
        Q.pop();
    }

    std::ofstream OF{bt_path};
    if (OF.is_open())
    {
        OF << OS.str();
        OF.close();
    }
}