Skip to content

Instantly share code, notes, and snippets.

@kakkoyun
Last active September 14, 2023 18:16
Show Gist options
  • Save kakkoyun/b74497ead38e4c63e232d96fbf36ceca to your computer and use it in GitHub Desktop.
Save kakkoyun/b74497ead38e4c63e232d96fbf36ceca to your computer and use it in GitHub Desktop.
python print offset
static inline __attribute__((__always_inline__)) void print_python_version_offsets(PythonVersionOffsets *pvo) {
  bpf_printk("Python Version Offsets:\n");
  bpf_printk("  major_version: %u\n", pvo->major_version);
  bpf_printk("  minor_version: %u\n", pvo->minor_version);
  bpf_printk("  patch_version: %u\n", pvo->patch_version);
  bpf_printk("  py_object:\n");
  bpf_printk("    ob_type: %lld\n", pvo->py_object.ob_type);
  bpf_printk("  py_string:\n");
  bpf_printk("    data: %lld\n", pvo->py_string.data);
  bpf_printk("    size: %lld\n", pvo->py_string.size);
  bpf_printk("  py_type_object:\n");
  bpf_printk("    tp_name: %lld\n", pvo->py_type_object.tp_name);
  bpf_printk("  py_thread_state:\n");
  bpf_printk("    next: %lld\n", pvo->py_thread_state.next);
  bpf_printk("    interp: %lld\n", pvo->py_thread_state.interp);
  bpf_printk("    frame: %lld\n", pvo->py_thread_state.frame);
  bpf_printk("    thread: %lld\n", pvo->py_thread_state.thread);
  bpf_printk("    cframe: %lld\n", pvo->py_thread_state.cframe);
  bpf_printk("  py_cframe:\n");
  bpf_printk("    current_frame: %lld\n", pvo->py_cframe.current_frame);
  bpf_printk("  py_interpreter_state:\n");
  bpf_printk("    tstate_head: %lld\n", pvo->py_interpreter_state.tstate_head);
  bpf_printk("  py_runtime_state:\n");
  bpf_printk("    interp_main: %lld\n", pvo->py_runtime_state.interp_main);
  bpf_printk("  py_frame_object:\n");
  bpf_printk("    f_back: %lld\n", pvo->py_frame_object.f_back);
  bpf_printk("    f_code: %lld\n", pvo->py_frame_object.f_code);
  bpf_printk("    f_lineno: %lld\n", pvo->py_frame_object.f_lineno);
  bpf_printk("    f_localsplus: %lld\n", pvo->py_frame_object.f_localsplus);
  bpf_printk("  py_code_object:\n");
  bpf_printk("    co_filename: %lld\n", pvo->py_code_object.co_filename);
  bpf_printk("    co_name: %lld\n", pvo->py_code_object.co_name);
  bpf_printk("    co_varnames: %lld\n", pvo->py_code_object.co_varnames);
  bpf_printk("    co_firstlineno: %lld\n", pvo->py_code_object.co_firstlineno);
  bpf_printk("  py_tuple_object:\n");
  bpf_printk("    ob_item: %lld\n", pvo->py_tuple_object.ob_item);
}
// Updated:
static inline __attribute__((__always_inline__)) void print_python_version_offsets(PythonVersionOffsets *pvo) {
  LOG("Python Version Offsets:\n");
  LOG("  major_version: %u\n", pvo->major_version);
  LOG("  minor_version: %u\n", pvo->minor_version);
  LOG("  patch_version: %u\n", pvo->patch_version);
  LOG("  py_object:\n");
  LOG("    ob_type: %lld\n", pvo->py_object.ob_type);
  LOG("  py_string:\n");
  LOG("    data: %lld\n", pvo->py_string.data);
  LOG("    size: %lld\n", pvo->py_string.size);
  LOG("  py_type_object:\n");
  LOG("    tp_name: %lld\n", pvo->py_type_object.tp_name);
  LOG("  py_thread_state:\n");
  LOG("    next: %lld\n", pvo->py_thread_state.next);
  LOG("    interp: %lld\n", pvo->py_thread_state.interp);
  LOG("    frame: %lld\n", pvo->py_thread_state.frame);
  LOG("    thread_id: %lld\n", pvo->py_thread_state.thread_id);
  LOG("    native_thread_id: %lld\n", pvo->py_thread_state.native_thread_id);
  LOG("    cframe: %lld\n", pvo->py_thread_state.cframe);
  LOG("  py_cframe:\n");
  LOG("    current_frame: %lld\n", pvo->py_cframe.current_frame);
  LOG("  py_interpreter_state:\n");
  LOG("    tstate_head: %lld\n", pvo->py_interpreter_state.tstate_head);
  LOG("  py_runtime_state:\n");
  LOG("    interp_main: %lld\n", pvo->py_runtime_state.interp_main);
  LOG("  py_frame_object:\n");
  LOG("    f_back: %lld\n", pvo->py_frame_object.f_back);
  LOG("    f_code: %lld\n", pvo->py_frame_object.f_code);
  LOG("    f_lineno: %lld\n", pvo->py_frame_object.f_lineno);
  LOG("    f_localsplus: %lld\n", pvo->py_frame_object.f_localsplus);
  LOG("  py_code_object:\n");
  LOG("    co_filename: %lld\n", pvo->py_code_object.co_filename);
  LOG("    co_name: %lld\n", pvo->py_code_object.co_name);
  LOG("    co_varnames: %lld\n", pvo->py_code_object.co_varnames);
  LOG("    co_firstlineno: %lld\n", pvo->py_code_object.co_firstlineno);
  LOG("  py_tuple_object:\n");
  LOG("    ob_item: %lld\n", pvo->py_tuple_object.ob_item);
}
static inline __attribute__((__always_inline__)) void print_interpreter_info(InterpreterInfo *info) {
  LOG("Interpreter Info:\n");
  LOG("  thread_state_addr: 0x%llx\n", info->thread_state_addr);
  LOG("  py_version_offset_index: %u\n", info->py_version_offset_index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment