aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/backtrace/backtrace_lib.cpp
blob: c0c7b2c667abf6eef1e2565bc286839652966aed (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
#include "backtrace_lib.h"

#include <util/generic/hash.h>
#include <util/system/execpath.h>

#include <algorithm>

#if defined(_linux_) && defined(_x86_64_)
#include <dlfcn.h>
#include <link.h>
#endif

namespace {
    const size_t Limit = 400;
    void* Stack[Limit];

    struct TDllInfo {
        const char* Path;
        ui64 BaseAddress;
    };

    const size_t MaxDLLCnt = 100;
    TDllInfo DLLs[MaxDLLCnt];
    size_t DLLCount = 0;

#if defined(_linux_) && defined(_x86_64_)
    int DlIterCallback(struct dl_phdr_info *info, size_t, void *data) {
        if (*info->dlpi_name) {
            if (DLLCount + 1 < MaxDLLCnt) {
                reinterpret_cast<std::remove_reference_t<decltype(DLLs[0])>*>(data)[DLLCount++] = { info->dlpi_name, (ui64)info->dlpi_addr };
            }
        }
        return 0;
    }
#endif
    bool comp(const TDllInfo& a, const TDllInfo& b) {
        return strcmp(a.Path, b.Path) < 0;
    }

}

namespace NYql {
    namespace NBacktrace {
        TCollectedFrame::TCollectedFrame(uintptr_t addr) {
            File = GetPersistentExecPath().c_str();
            Address = addr;
#if defined(_linux_) && defined(_x86_64_)
            Dl_info dlInfo;
            memset(&dlInfo, 0, sizeof(dlInfo));
            auto ret = dladdr(reinterpret_cast<void*>(addr), &dlInfo);
            if (ret) {
                auto it = std::lower_bound(DLLs, DLLs + DLLCount, std::remove_reference_t<decltype(DLLs[0])> {dlInfo.dli_fname, {}}, comp);
                if (it != DLLs + DLLCount && !strcmp(it->Path, dlInfo.dli_fname)) {
                    File = it->Path;
                    Address -= it->BaseAddress;
                }
            }
#endif
        }

        size_t CollectFrames(TCollectedFrame* frames, void* data) {
#if defined(_linux_) && defined(_x86_64_)
            DLLCount = 0;
            dl_iterate_phdr(DlIterCallback, &DLLs);
#endif
            std::stable_sort(DLLs, DLLs + DLLCount, comp);
            size_t cnt = CollectBacktrace(Stack, Limit, data);
            return CollectFrames(frames, Stack, cnt);
        }

        size_t CollectFrames(TCollectedFrame* frames, void** stack, size_t cnt) {
            for (size_t i = 0; i < cnt; ++i) {
                new (frames + i)TCollectedFrame(reinterpret_cast<uintptr_t>(stack[i]));
            }
            return cnt;
        }
    }
}