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
|
#include "symbolizer.h"
#include <contrib/libs/backtrace/backtrace.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/system/type_name.h>
#include <util/system/execpath.h>
#include <util/string/builder.h>
#include <mutex>
#include <numeric>
#include <algorithm>
#include <util/stream/mem.h>
#ifdef __GNUC__
#include <cxxabi.h>
#endif
namespace {
const size_t MaxStrLen = 512;
const size_t MaxDemangleLen = 1024 * 1024;
char Buff[MaxDemangleLen];
class TNoThrowingMemoryOutput : public TMemoryOutput {
public:
TNoThrowingMemoryOutput(void* c, size_t l) : TMemoryOutput(c, l) {}
void Truncate() {
*(Buf_ - 1) = '.';
*(Buf_ - 2) = '.';
*(Buf_ - 3) = '.';
}
void DoWrite(const void* buf, size_t len) override {
bool truncated = Buf_ + len > End_;
if (truncated) {
len = std::min(len, (size_t)(End_ - Buf_));
}
memcpy(Buf_, buf, len);
Buf_ += len;
if (truncated) {
Truncate();
}
}
void DoWriteC(char c) override {
if (Buf_ == End_) {
Truncate();
} else {
*Buf_++ = c;
}
}
};
void HandleLibBacktraceError(void* data, const char* msg, int) {
if (!data) {
Cerr << msg;
return;
}
TNoThrowingMemoryOutput out(data, MaxStrLen - 1);
out << msg;
}
const char* Demangle(const char* name) {
#ifndef __GNUC__
return name;
#else
int status;
size_t len = MaxDemangleLen - 1;
const char* res = __cxxabiv1::__cxa_demangle(name, Buff, &len, &status);
if (!res) {
return name;
}
return res;
#endif
}
int HandleLibBacktraceFrame(void* data, uintptr_t, const char* filename, int lineno, const char* function) {
TNoThrowingMemoryOutput out(data, MaxStrLen - 1);
const char* fileName = filename ? filename : "???";
const char* functionName = function ? Demangle(function) : "???";
out << functionName << " at " << fileName << ":" << lineno << ":0";
return 0;
}
}
namespace NYql {
namespace NBacktrace {
namespace {
std::mutex Mutex;
char* Result[Limit];
size_t Order[Limit];
char TmpBuffer[MaxStrLen * Limit]{};
auto CreateState(const char* filename) {
return backtrace_create_state(
filename,
0,
HandleLibBacktraceError,
nullptr
);
}
}
void Symbolize(const TStackFrame* frames, size_t count, IOutputStream* out) {
if (!count) {
return;
}
memset(TmpBuffer, 0, sizeof(TmpBuffer));
Result[0] = TmpBuffer;
for (size_t i = 1; i < Limit; ++i) {
Result[i] = Result[i - 1] + MaxStrLen;
}
const std::lock_guard lock{Mutex};
std::iota(Order, Order + count, 0u);
std::sort(Order, Order + count, [&frames](auto a, auto b) { return strcmp(frames[a].File, frames[b].File) < 0; });
struct backtrace_state* state = nullptr;
for (size_t i = 0; i < count; ++i) {
if (!i || frames[Order[i - 1]].File != frames[Order[i]].File) {
state = CreateState(frames[Order[i]].File);
}
if (!state) {
Result[Order[i]] = nullptr; // File not found
continue;
}
int status = backtrace_pcinfo(
state,
reinterpret_cast<uintptr_t>(frames[Order[i]].Address) - 1, // last byte of the call instruction
HandleLibBacktraceFrame,
HandleLibBacktraceError,
reinterpret_cast<void*>(Result[Order[i]]));
if (0 != status) {
break;
}
}
for (size_t i = 0; i < count; ++i) {
if (Result[i]) {
*out << Result[i] << "\n";
} else {
*out << "File `" << frames[i].File << "` not found\n";
}
}
}
}
}
|