-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathworker.cpp
More file actions
240 lines (216 loc) · 6.89 KB
/
worker.cpp
File metadata and controls
240 lines (216 loc) · 6.89 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
#include "tasks/worker.h"
#include <backtrace.h>
#include <cstdlib>
#include <cxxabi.h>
#include <dlfcn.h>
#include <memory>
#include <sstream>
namespace ccf::tasks
{
// Maximum number of frames to capture at the throw-point
static constexpr int throw_trace_max_frames = 128;
struct ThrowTrace
{
void* frames[throw_trace_max_frames] = {};
size_t num_frames = 0;
};
namespace
{
thread_local ThrowTrace current_throw_trace = {};
struct FreeDeleter
{
void operator()(char* p) const
{
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
free(p);
}
};
// Lazily initialise and return the process-wide backtrace_state.
// backtrace_create_state allocates resources that cannot be freed, so
// we call it at most once and cache the result.
backtrace_state* get_backtrace_state()
{
static backtrace_state* state = backtrace_create_state(
nullptr, // let libbacktrace find the executable
1, // threaded = true
nullptr, // ignore errors during init
nullptr);
return state;
}
// Demangle a C++ mangled symbol name. Returns the original string
// unchanged if demangling fails.
std::string demangle(const char* name)
{
if (name == nullptr)
{
return "<unknown>";
}
int status = 0;
std::unique_ptr<char, FreeDeleter> demangled(
abi::__cxa_demangle(name, nullptr, nullptr, &status));
if (status == 0 && demangled != nullptr)
{
return demangled.get();
}
return name;
}
// Data passed through libbacktrace callbacks to build up each frame's
// description.
struct PcinfoResult
{
bool resolved = false;
std::string function;
std::string filename;
int lineno = 0;
};
// Called by backtrace_pcinfo for each source location (may be called
// multiple times per PC when inlined calls are present).
int pcinfo_callback(
void* data,
uintptr_t /*pc*/,
const char* filename,
int lineno,
const char* function)
{
auto* result = static_cast<PcinfoResult*>(data);
if (function != nullptr)
{
result->resolved = true;
result->function = demangle(function);
result->filename = (filename != nullptr) ? filename : "";
result->lineno = lineno;
}
return 0; // continue
}
// Called by backtrace_syminfo when DWARF info is unavailable but the
// dynamic symbol table has an entry.
void syminfo_callback(
void* data,
uintptr_t /*pc*/,
const char* symname,
uintptr_t /*symval*/,
uintptr_t /*symsize*/)
{
auto* result = static_cast<PcinfoResult*>(data);
if (symname != nullptr)
{
result->resolved = true;
result->function = demangle(symname);
}
}
// Silently ignore libbacktrace errors in individual frame resolution —
// we fall back to printing the raw PC address.
void error_callback(void* /*data*/, const char* /*msg*/, int /*errnum*/) {}
// Format a stack trace using libbacktrace for DWARF-aware symbol,
// file and line resolution. This works in all build configurations
// without requiring -rdynamic.
std::string format_stacktrace(void** frames, int num_frames)
{
std::ostringstream oss;
auto* state = get_backtrace_state();
for (int i = 0; i < num_frames; ++i)
{
auto pc = reinterpret_cast<uintptr_t>(frames[i]);
PcinfoResult result;
if (state != nullptr)
{
// Try DWARF-based resolution first (gives file + line + function)
backtrace_pcinfo(state, pc, pcinfo_callback, error_callback, &result);
// If DWARF info wasn't available, try the symbol table
if (!result.resolved)
{
backtrace_syminfo(
state, pc, syminfo_callback, error_callback, &result);
}
}
oss << " #" << i << ": ";
if (result.resolved)
{
oss << result.function;
if (!result.filename.empty())
{
oss << " at " << result.filename << ":" << result.lineno;
}
}
else
{
oss << "0x" << std::hex << pc << std::dec;
}
oss << "\n";
}
return oss.str();
}
}
void dump_stacktrace(const std::string& msg)
{
LOG_FATAL_FMT("{}", msg);
auto& throw_trace = current_throw_trace;
if (throw_trace.num_frames > 0)
{
LOG_FATAL_FMT(
"Stack trace:\n{}",
format_stacktrace(throw_trace.frames, throw_trace.num_frames));
// Reset so that a subsequent dump does not re-use a stale trace
// (e.g. if an earlier throw was caught internally and a later
// throw; / re-throw escapes without calling __cxa_throw).
throw_trace.num_frames = 0;
}
else
{
LOG_FATAL_FMT("No throw-point stack trace available");
}
}
}
// Interpose __cxa_throw to capture a backtrace at each throw-point.
// This is called by the C++ runtime whenever `throw` is executed.
extern "C"
{
using CxaThrowFn = void (*)(void*, std::type_info*, void (*)(void*));
void __cxa_throw(
void* thrown_exception, std::type_info* tinfo, void (*dest)(void*))
{
// Capture the backtrace at the throw site using libbacktrace's own
// unwinder. glibc backtrace() can lose frames whose return address
// falls exactly at the end of a .eh_frame FDE range; libbacktrace's
// DWARF unwinder handles this correctly.
auto& trace = ccf::tasks::current_throw_trace;
trace.num_frames = 0;
auto* bt_state = ccf::tasks::get_backtrace_state();
if (bt_state != nullptr)
{
backtrace_simple(
bt_state,
0, // skip = 0, capture from here
[](void* data, uintptr_t pc) -> int {
auto* t = static_cast<ccf::tasks::ThrowTrace*>(data);
if (t->num_frames < ccf::tasks::throw_trace_max_frames)
{
t->frames[t->num_frames++] = reinterpret_cast<void*>(pc); // NOLINT
}
return 0;
},
nullptr, // ignore errors
&trace);
}
// Forward to the real __cxa_throw
static auto real_cxa_throw =
reinterpret_cast<CxaThrowFn>(dlsym(RTLD_NEXT, "__cxa_throw"));
if (real_cxa_throw != nullptr)
{
real_cxa_throw(thrown_exception, tinfo, dest);
// real_cxa_throw is [[noreturn]], so we never reach here
}
else
{
// If dlsym failed, we cannot safely proceed. Abort to prevent undefined
// behavior.
std::abort();
}
// Both real_cxa_throw and std::abort() are [[noreturn]], but the compiler
// may not recognize that for function pointers. This satisfies the compiler
// that we never return from this function.
__builtin_unreachable();
}
}