forked from open-telemetry/opentelemetry-ebpf-profiler
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpython_tracer.ebpf.c
More file actions
381 lines (333 loc) · 13.5 KB
/
python_tracer.ebpf.c
File metadata and controls
381 lines (333 loc) · 13.5 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// This file contains the code and map definitions for the Python tracer
#include "bpfdefs.h"
#include "errors.h"
#include "stackdeltatypes.h"
#include "tracemgmt.h"
#include "tsd.h"
#include "types.h"
// Forward declaration to avoid warnings like
// "declaration of 'struct pt_regs' will not be visible outside of this function [-Wvisibility]".
struct pt_regs;
// Map from Python process IDs to a structure containing addresses of variables
// we require in order to build the stack trace
struct py_procs_t {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, pid_t);
__type(value, PyProcInfo);
__uint(max_entries, 1024);
} py_procs SEC(".maps");
// Record a Python frame
static EBPF_INLINE ErrorCode push_python(UnwindState *state, Trace *trace, u64 file, u64 line)
{
u64 *data = push_frame(state, trace, FRAME_MARKER_PYTHON, FRAME_FLAG_PID_SPECIFIC, 0, 2);
if (!data) {
return ERR_STACK_LENGTH_EXCEEDED;
}
data[0] = file;
data[1] = line;
return ERR_OK;
}
static EBPF_INLINE u64 py_encode_lineno(u32 object_id, u32 f_lasti)
{
return (object_id | (((u64)f_lasti) << 32));
}
static EBPF_INLINE ErrorCode process_python_frame(
PerCPURecord *record,
const PyProcInfo *pyinfo,
void **py_frameobjectptr,
bool *continue_with_next)
{
Trace *trace = &record->trace;
const void *py_frameobject = *py_frameobjectptr;
u64 lineno = FUNC_TYPE_UNKNOWN, file_id = UNKNOWN_FILE;
u32 codeobject_id;
*continue_with_next = false;
// Vars used in extracting data from the Python interpreter
PythonUnwindScratchSpace *pss = &record->pythonUnwindScratch;
// Make verifier happy for PyFrameObject offsets
if (
pyinfo->PyFrameObject_f_code > sizeof(pss->frame) - sizeof(void *) ||
pyinfo->PyFrameObject_f_back > sizeof(pss->frame) - sizeof(void *) ||
pyinfo->PyFrameObject_f_lasti > sizeof(pss->frame) - sizeof(u64) ||
pyinfo->PyFrameObject_entry_member > sizeof(pss->frame) - sizeof(u8)) {
return ERR_UNREACHABLE;
}
// Read PyFrameObject
if (bpf_probe_read_user(pss->frame, sizeof(pss->frame), py_frameobject)) {
// DEBUG_PRINT("Failed to read PyFrameObject 0x%lx", (unsigned long)py_frameobject);
increment_metric(metricID_UnwindPythonErrBadFrameCodeObjectAddr);
return ERR_PYTHON_BAD_FRAME_OBJECT_ADDR;
}
void *py_codeobject = *(void **)(&pss->frame[pyinfo->PyFrameObject_f_code]);
*py_frameobjectptr = *(void **)(&pss->frame[pyinfo->PyFrameObject_f_back]);
// Stop unwinding if `f_executable` is None. See comment when getting the
// ´noneStruct´ address in python.go for details.
void *noneStructAddr = (void *)pyinfo->noneStructAddr;
if (noneStructAddr && py_codeobject == noneStructAddr) {
*continue_with_next = true;
return ERR_OK;
}
// See experiments/python/README.md for a longer version of this. In short, we
// cannot directly obtain the correct Python line number. It has to be calculated
// using information found in the PyCodeObject for the current frame. This
// calculation involves iterating over potentially unbounded data, and so we don't
// want to do it in eBPF. Instead, we log the bytecode instruction that is being
// executed, and then convert this to a line number in the user-land component.
// Bytecode instructions are identified as an offset within a code object. The
// offset is easy to retrieve (PyFrameObject->f_lasti). Code objects are a little
// more tricky. We need to log enough information to uniquely identify the code
// object for the current frame, so that in the user-land component we can load
// it from the .pyc. There is no unique identifier for code objects though, so we
// try to construct one below by hashing together a few fields. These fields are
// selected in the *hope* that no collisions occur between code objects.
int py_f_lasti = 0;
if (pyinfo->lasti_is_codeunit) {
// With Python 3.11 the element f_lasti not only got renamed but also its
// type changed from int to a _Py_CODEUNIT* and needs to be translated to lastI.
// It is a direct pointer to the bytecode, so calculate the byte code index.
// sizeof(_Py_CODEUNIT) == 2.
// https://github.com/python/cpython/commit/ef6a482b0285870c45f39c9b17ed827362b334ae
u64 prev_instr = *(u64 *)(&pss->frame[pyinfo->PyFrameObject_f_lasti]);
s64 instr_diff = (s64)prev_instr - (s64)py_codeobject - pyinfo->PyCodeObject_sizeof;
if (instr_diff < -2 || instr_diff > 0x10000000)
instr_diff = -2;
py_f_lasti = (int)instr_diff >> 1;
// Python 3.11+ the frame object has some field that can be used to determine
// if this is the last frame in the interpreter loop. This generalized test
// works on 3.11 and 3.12 though the actual struct members are different.
if (
*(u8 *)(&pss->frame[pyinfo->PyFrameObject_entry_member]) == pyinfo->PyFrameObject_entry_val) {
*continue_with_next = true;
}
} else {
py_f_lasti = *(int *)(&pss->frame[pyinfo->PyFrameObject_f_lasti]);
}
if (!py_codeobject) {
// DEBUG_PRINT(
// "Null codeobject for PyFrameObject 0x%lx 0x%lx",
// (unsigned long)py_frameobject,
// (unsigned long)(py_frameobject + pyinfo->PyFrameObject_f_code));
increment_metric(metricID_UnwindPythonZeroFrameCodeObject);
goto push_frame;
}
// Make verifier happy for PyCodeObject offsets
if (
pyinfo->PyCodeObject_co_argcount > sizeof(pss->code) - sizeof(int) ||
pyinfo->PyCodeObject_co_kwonlyargcount > sizeof(pss->code) - sizeof(int) ||
pyinfo->PyCodeObject_co_flags > sizeof(pss->code) - sizeof(int) ||
pyinfo->PyCodeObject_co_firstlineno > sizeof(pss->code) - sizeof(int)) {
return ERR_UNREACHABLE;
}
// Read PyCodeObject
long pycode_err = bpf_probe_read_user(pss->code, sizeof(pss->code), py_codeobject);
if (pycode_err) {
// DEBUG_PRINT(
// "Failed to read PyCodeObject at 0x%lx err=%ld", (unsigned long)(py_codeobject), pycode_err);
increment_metric(metricID_UnwindPythonErrBadCodeObjectArgCountAddr);
// Push the frame with the code object address so the agent can try to
// read it via /proc/pid/mem (which supports page faults unlike BPF).
// codeobject_id=0 distinguishes this from a successful read.
file_id = (u64)py_codeobject;
lineno = py_encode_lineno(0, (u32)py_f_lasti);
goto push_frame;
}
int py_argcount = *(int *)(&pss->code[pyinfo->PyCodeObject_co_argcount]);
int py_kwonlyargcount = *(int *)(&pss->code[pyinfo->PyCodeObject_co_kwonlyargcount]);
int py_flags = *(int *)(&pss->code[pyinfo->PyCodeObject_co_flags]);
int py_firstlineno = *(int *)(&pss->code[pyinfo->PyCodeObject_co_firstlineno]);
codeobject_id =
(py_argcount << 25) + (py_kwonlyargcount << 18) + (py_flags << 10) + py_firstlineno;
file_id = (u64)py_codeobject;
lineno = py_encode_lineno(codeobject_id, (u32)py_f_lasti);
push_frame:
DEBUG_PRINT("Pushing Python %lx %lu", (unsigned long)file_id, (unsigned long)lineno);
ErrorCode error = push_python(&record->state, trace, file_id, lineno);
if (error) {
DEBUG_PRINT("failed to push python frame");
return error;
}
increment_metric(metricID_UnwindPythonFrames);
return ERR_OK;
}
// get_PyThreadState retrieves the PyThreadState* for the current thread.
//
// Python 3.12 and earlier set the thread_state using pthread_setspecific with the key
// stored in a global variable autoTLSkey.
// Python 3.13+ uses a direct thread-local variable _Py_tss_tstate instead.
static EBPF_INLINE ErrorCode get_PyThreadState(
const PyProcInfo *pyinfo, void *tsd_base, void *autoTLSkeyAddr, void **thread_state)
{
if (pyinfo->tls_offset != 0) {
if (bpf_probe_read_user(thread_state, sizeof(void *), tsd_base + pyinfo->tls_offset)) {
DEBUG_PRINT(
"Failed to read direct TLS at base 0x%lx offset %d",
(unsigned long)tsd_base,
pyinfo->tls_offset);
increment_metric(metricID_UnwindPythonErrReadThreadStateAddr);
return ERR_PYTHON_READ_THREAD_STATE_ADDR;
}
return ERR_OK;
}
// Python 3.12 and earlier: use pthread TLS
int key;
if (bpf_probe_read_user(&key, sizeof(key), autoTLSkeyAddr)) {
DEBUG_PRINT("Failed to read autoTLSkey from 0x%lx", (unsigned long)autoTLSkeyAddr);
increment_metric(metricID_UnwindPythonErrBadAutoTlsKeyAddr);
return ERR_PYTHON_BAD_AUTO_TLS_KEY_ADDR;
}
if (tsd_read(&pyinfo->tsdInfo, tsd_base, key, thread_state)) {
increment_metric(metricID_UnwindPythonErrReadThreadStateAddr);
return ERR_PYTHON_READ_THREAD_STATE_ADDR;
}
return ERR_OK;
}
static EBPF_INLINE ErrorCode get_PyFrame(const PyProcInfo *pyinfo, void **frame)
{
void *tsd_base;
if (tsd_get_base(&tsd_base)) {
DEBUG_PRINT("Failed to get TSD base address");
increment_metric(metricID_UnwindPythonErrReadTsdBase);
return ERR_PYTHON_READ_TSD_BASE;
}
DEBUG_PRINT(
"TSD Base 0x%lx, autoTLSKeyAddr 0x%lx",
(unsigned long)tsd_base,
(unsigned long)pyinfo->autoTLSKeyAddr);
// Get the PyThreadState from TSD
void *py_tsd_thread_state;
ErrorCode error =
get_PyThreadState(pyinfo, tsd_base, (void *)pyinfo->autoTLSKeyAddr, &py_tsd_thread_state);
if (error) {
return error;
}
if (!py_tsd_thread_state) {
DEBUG_PRINT("PyThreadState is 0x0");
increment_metric(metricID_UnwindPythonErrZeroThreadState);
return ERR_PYTHON_ZERO_THREAD_STATE;
}
// Get PyThreadState.frame
if (bpf_probe_read_user(
frame, sizeof(void *), py_tsd_thread_state + pyinfo->PyThreadState_frame)) {
DEBUG_PRINT(
"Failed to read PyThreadState.frame at 0x%lx",
(unsigned long)(py_tsd_thread_state + pyinfo->PyThreadState_frame));
increment_metric(metricID_UnwindPythonErrBadThreadStateFrameAddr);
return ERR_PYTHON_BAD_THREAD_STATE_FRAME_ADDR;
}
if (pyinfo->frame_is_cframe) {
if (bpf_probe_read_user(frame, sizeof(void *), *frame + pyinfo->PyCFrame_current_frame)) {
DEBUG_PRINT(
"Failed to read _PyCFrame.current_frame at 0x%lx",
(unsigned long)(*frame + pyinfo->PyCFrame_current_frame));
increment_metric(metricID_UnwindPythonErrBadCFrameFrameAddr);
return ERR_PYTHON_BAD_CFRAME_CURRENT_FRAME_ADDR;
}
}
return ERR_OK;
}
#include "native_stack_trace.h"
// Number of loop iterations in unwind_python. Each iteration handles either
// one Python frame or one native frame depending on the current unwinder state.
// This bounds the BPF verifier instruction count.
#define PYTHON_NATIVE_LOOP_ITERS 9
// step_python processes one Python frame and updates *unwinder to indicate
// what should happen next
static EBPF_INLINE ErrorCode
step_python(PerCPURecord *record, const PyProcInfo *pyinfo, void **py_frame, int *unwinder)
{
bool continue_with_next;
ErrorCode error = process_python_frame(record, pyinfo, py_frame, &continue_with_next);
if (error) {
*unwinder = PROG_UNWIND_STOP;
return error;
}
if (continue_with_next) {
*unwinder = get_next_unwinder_after_interpreter();
} else if (!*py_frame) {
*unwinder = PROG_UNWIND_STOP;
} else {
*unwinder = PROG_UNWIND_PYTHON;
}
return ERR_OK;
}
// step_native processes one native frame at an interpreter boundary and
// updates *unwinder
static EBPF_INLINE ErrorCode step_native(PerCPURecord *record, int *unwinder)
{
Trace *trace = &record->trace;
*unwinder = PROG_UNWIND_STOP;
increment_metric(metricID_UnwindNativeAttempts);
ErrorCode error = push_native(
&record->state,
trace,
record->state.text_section_id,
record->state.text_section_offset,
record->state.return_address);
if (error) {
return error;
}
bool stop;
error = unwind_one_frame(record, &stop);
if (error || stop) {
return error;
}
return get_next_unwinder_after_native_frame(record, unwinder);
}
// unwind_python is the entry point for tracing when invoked from the native tracer
// or interpreter dispatcher. It does not reset the trace object and will append the
// Python stack frames to the trace object for the current CPU.
static EBPF_INLINE int unwind_python(struct pt_regs *ctx)
{
PerCPURecord *record = get_per_cpu_record();
if (!record)
return -1;
ErrorCode error = ERR_OK;
int unwinder = PROG_UNWIND_PYTHON;
Trace *trace = &record->trace;
u32 pid = trace->pid;
DEBUG_PRINT("unwind_python()");
const PyProcInfo *pyinfo = bpf_map_lookup_elem(&py_procs, &pid);
if (!pyinfo) {
// Not a Python process that we have info on
DEBUG_PRINT("Can't build Python stack, no address info");
increment_metric(metricID_UnwindPythonErrNoProcInfo);
error = ERR_PYTHON_NO_PROC_INFO;
goto exit;
}
DEBUG_PRINT("Building Python stack for 0x%x", pyinfo->version);
if (!record->pythonUnwindState.py_frame) {
increment_metric(metricID_UnwindPythonAttempts);
error = get_PyFrame(pyinfo, &record->pythonUnwindState.py_frame);
if (error) {
goto exit;
}
}
if (!record->pythonUnwindState.py_frame) {
DEBUG_PRINT(" -> Python frames are handled");
unwinder_mark_done(record, PROG_UNWIND_PYTHON);
goto exit;
}
{
void *py_frame = record->pythonUnwindState.py_frame;
for (int t = 0; t < PYTHON_NATIVE_LOOP_ITERS; t++) {
switch (unwinder) {
case PROG_UNWIND_PYTHON: error = step_python(record, pyinfo, &py_frame, &unwinder); break;
case PROG_UNWIND_NATIVE: error = step_native(record, &unwinder); break;
default: goto done;
}
if (error) {
goto done;
}
}
done:
if (error || !py_frame) {
unwinder_mark_done(record, PROG_UNWIND_PYTHON);
}
record->pythonUnwindState.py_frame = py_frame;
}
exit:
record->state.unwind_error = error;
tail_call(ctx, unwinder);
return -1;
}
MULTI_USE_FUNC(unwind_python)