Skip to content

chore(ebpf) py312 support #2577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions ebpf/bpf/pyperf.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ enum {

};

#define PY_OFFSET_String_size 48
#define PY_OFFSET_PyVarObject_ob_size 16
#define PY_OFFSET_PyObject_ob_type 8
#define PY_OFFSET_PyTypeObject_tp_name 24


typedef struct {
int16_t PyThreadState_frame;
Expand All @@ -54,9 +51,14 @@ typedef struct {
int16_t PyCodeObject_co_localsplusnames;
int16_t PyTupleObject_ob_item;

int16_t PyVarObject_ob_size;
int16_t PyObject_ob_type;
int16_t PyTypeObject_tp_name;

int16_t VFrame_code; // PyFrameObject_f_code pre 311 or PyInterpreterFrame_f_code post 311
int16_t VFrame_previous; // PyFrameObject_f_back pre 311 or PyInterpreterFrame_previous post 311
int16_t VFrame_localsplus; // PyFrameObject_localsplus pre 311 or PyInterpreterFrame_localsplus post 311
int16_t String_size; // sizeof(PyASCIIObject)

} py_offset_config;

Expand Down Expand Up @@ -312,7 +314,7 @@ static __always_inline int check_first_arg(void *code_ptr,
}
}
if (bpf_probe_read_user(
&args_size, sizeof(args_size), args_ptr + PY_OFFSET_PyVarObject_ob_size)) {
&args_size, sizeof(args_size), args_ptr + offsets->PyVarObject_ob_size)) {
return -1;
}
if (args_size < 1) {
Expand All @@ -325,7 +327,7 @@ static __always_inline int check_first_arg(void *code_ptr,
return -1;
}
if (bpf_probe_read_user_str(
&symbol->name, sizeof(symbol->name), args_ptr + PY_OFFSET_String_size) < 0) {
&symbol->name, sizeof(symbol->name), args_ptr + offsets->String_size) < 0) {
return -1;
}
// compare strings as ints to save instructions
Expand Down Expand Up @@ -372,12 +374,12 @@ static __always_inline int get_names(
if (ptr) {
if (first_self) {
// we are working with an instance, first we need to get type
if (bpf_probe_read_user(&ptr, sizeof(void *), ptr + PY_OFFSET_PyObject_ob_type)) {
if (bpf_probe_read_user(&ptr, sizeof(void *), ptr + offsets->PyObject_ob_type)) {
bpf_dbg_printk("failed to read ob_type at %x\n", ptr + PY_OFFSET_PyObject_ob_type);
return -PY_ERROR_CLASS_NAME;
}
}
if (bpf_probe_read_user(&ptr, sizeof(void *), ptr + PY_OFFSET_PyTypeObject_tp_name)) {
if (bpf_probe_read_user(&ptr, sizeof(void *), ptr + offsets->PyTypeObject_tp_name)) {
bpf_dbg_printk("failed to read tp_name at %x\n", ptr + PY_OFFSET_PyTypeObject_tp_name);
return -PY_ERROR_CLASS_NAME;
}
Expand All @@ -400,7 +402,7 @@ static __always_inline int get_names(
return -PY_ERROR_FILE_NAME;
}
if (bpf_probe_read_user_str(
&symbol->file, sizeof(symbol->file), pystr_ptr + PY_OFFSET_String_size) < 0) {
&symbol->file, sizeof(symbol->file), pystr_ptr + offsets->String_size) < 0) {
return -PY_ERROR_FILE_NAME;
}
// read PyCodeObject's name into symbol
Expand All @@ -409,7 +411,7 @@ static __always_inline int get_names(
return -PY_ERROR_NAME;
}
if (bpf_probe_read_user_str(
&symbol->name, sizeof(symbol->name), pystr_ptr + PY_OFFSET_String_size) < 0) {
&symbol->name, sizeof(symbol->name), pystr_ptr + offsets->String_size) < 0) {
return -PY_ERROR_NAME;
}
return 0;
Expand Down
4 changes: 4 additions & 0 deletions ebpf/python/perf_bpfel_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified ebpf/python/perf_bpfel_arm64.o
Binary file not shown.
4 changes: 4 additions & 0 deletions ebpf/python/perf_bpfel_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified ebpf/python/perf_bpfel_x86.o
Binary file not shown.
6 changes: 5 additions & 1 deletion ebpf/python/pyperf_pid_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetPyPerfPidData(l log.Logger, pid uint32) (*PerfPyPidData, error) {

offsets, guess, err := GetUserOffsets(version)
if err != nil {
return nil, fmt.Errorf("unsupported python version %+v", version)
return nil, fmt.Errorf("unsupported python version %w %+v", err, version)
}
if guess {
level.Warn(l).Log("msg", "python offsets were not found, but guessed from the closest patch version")
Expand Down Expand Up @@ -121,6 +121,10 @@ func GetPyPerfPidData(l log.Logger, pid uint32) (*PerfPyPidData, error) {
VFrameCode: vframeCode,
VFramePrevious: vframeBack,
VFrameLocalsplus: vframeLocalPlus,
StringSize: offsets.StringSize,
PyVarObjectObSize: offsets.PyVarObject_ob_size,
PyObjectObType: offsets.PyObject_ob_type,
PyTypeObjectTpName: offsets.PyTypeObject_tp_name,
}
return data, nil
}
16 changes: 12 additions & 4 deletions ebpf/python/tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,19 @@ func getPyTssKey(pid uint32, version Version, offsets *UserOffsets, pyRuntime ui
//should never happen
return 0, fmt.Errorf("python missing symbols pyRuntime %d %v", pid, version)
}
if offsets.PyRuntimeState_gilstate == -1 || offsets.Gilstate_runtime_state_autoTSSkey == -1 || offsets.PyTssT_key == -1 {
// should never happen
return 0, fmt.Errorf("python missing offsets PyRuntimeStateGilstate GilstateRuntimeStateAutoTSSkey PyTssT_key %d %v", pid, version)
if version.Compare(Py312) >= 0 {
if offsets.PyRuntimeState_autoTSSkey == -1 {
// should never happen
return 0, fmt.Errorf("python missing offsets PyRuntimeState_autoTSSkey %d %v", pid, version)
}
pkey = int64(pyRuntime) + int64(offsets.PyRuntimeState_autoTSSkey)
} else {
if offsets.PyRuntimeState_gilstate == -1 || offsets.Gilstate_runtime_state_autoTSSkey == -1 {
// should never happen
return 0, fmt.Errorf("python missing offsets PyRuntimeStateGilstate GilstateRuntimeStateAutoTSSkey PyTssT_key %d %v", pid, version)
}
pkey = int64(pyRuntime) + int64(offsets.PyRuntimeState_gilstate+offsets.Gilstate_runtime_state_autoTSSkey)
}
pkey = int64(pyRuntime) + int64(offsets.PyRuntimeState_gilstate+offsets.Gilstate_runtime_state_autoTSSkey)
n, err := mem.ReadAt(key[:], int64(pkey))
if err != nil {
return 0, fmt.Errorf("python failed to read key %d %d %v %w", pid, pkey, version, err)
Expand Down
18 changes: 2 additions & 16 deletions ebpf/python/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Version struct {
Major, Minor, Patch int
}

var Py312 = &Version{Major: 3, Minor: 12}
var Py311 = &Version{Major: 3, Minor: 11}
var Py37 = &Version{Major: 3, Minor: 7}

Expand Down Expand Up @@ -157,21 +158,6 @@ func GetUserOffsets(version Version) (*UserOffsets, bool, error) {
}
patchGuess = true
}
//#define PY_OFFSET_String_size 48
//#define PY_OFFSET_PyVarObject_ob_size 16
//#define PY_OFFSET_PyObject_ob_type 8
//#define PY_OFFSET_PyTypeObject_tp_name 24
if offsets.StringSize != 48 {
return offsets, patchGuess, fmt.Errorf("python offsets.StringSize != 48 %+v %+v", offsets, version)
}
if offsets.PyVarObject_ob_size != 16 {
return offsets, patchGuess, fmt.Errorf("python offsets.PyVarObject_ob_size != 16 %+v %+v", offsets, version)
}
if offsets.PyObject_ob_type != 8 {
return offsets, patchGuess, fmt.Errorf("python offsets.PyObject_ob_type != 8 %+v %+v", offsets, version)
}
if offsets.PyTypeObject_tp_name != 24 {
return offsets, patchGuess, fmt.Errorf("python offsets.PyTypeObject_tp_name != 24 %+v %+v", offsets, version)
}

return offsets, patchGuess, nil
}
27 changes: 27 additions & 0 deletions ebpf/python/versions_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ebpf/testdata