mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Remove local_var_metadata from helpers
This commit is contained in:
@ -30,7 +30,6 @@ def bpf_ktime_get_ns_emitter(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Emit LLVM IR for bpf_ktime_get_ns helper function call.
|
Emit LLVM IR for bpf_ktime_get_ns helper function call.
|
||||||
@ -53,7 +52,6 @@ def bpf_map_lookup_elem_emitter(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Emit LLVM IR for bpf_map_lookup_elem helper function call.
|
Emit LLVM IR for bpf_map_lookup_elem helper function call.
|
||||||
@ -89,7 +87,6 @@ def bpf_printk_emitter(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""Emit LLVM IR for bpf_printk helper function call."""
|
"""Emit LLVM IR for bpf_printk helper function call."""
|
||||||
if not hasattr(func, "_fmt_counter"):
|
if not hasattr(func, "_fmt_counter"):
|
||||||
@ -107,7 +104,6 @@ def bpf_printk_emitter(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
elif isinstance(call.args[0], ast.Constant) and isinstance(call.args[0].value, str):
|
elif isinstance(call.args[0], ast.Constant) and isinstance(call.args[0].value, str):
|
||||||
# TODO: We are only supporting single arguments for now.
|
# TODO: We are only supporting single arguments for now.
|
||||||
@ -138,7 +134,6 @@ def bpf_map_update_elem_emitter(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Emit LLVM IR for bpf_map_update_elem helper function call.
|
Emit LLVM IR for bpf_map_update_elem helper function call.
|
||||||
@ -190,7 +185,6 @@ def bpf_map_delete_elem_emitter(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Emit LLVM IR for bpf_map_delete_elem helper function call.
|
Emit LLVM IR for bpf_map_delete_elem helper function call.
|
||||||
@ -228,7 +222,6 @@ def bpf_get_current_pid_tgid_emitter(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Emit LLVM IR for bpf_get_current_pid_tgid helper function call.
|
Emit LLVM IR for bpf_get_current_pid_tgid helper function call.
|
||||||
@ -255,7 +248,6 @@ def bpf_perf_event_output_handler(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
if len(call.args) != 1:
|
if len(call.args) != 1:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
@ -264,9 +256,7 @@ def bpf_perf_event_output_handler(
|
|||||||
data_arg = call.args[0]
|
data_arg = call.args[0]
|
||||||
ctx_ptr = func.args[0] # First argument to the function is ctx
|
ctx_ptr = func.args[0] # First argument to the function is ctx
|
||||||
|
|
||||||
data_ptr, size_val = get_data_ptr_and_size(
|
data_ptr, size_val = get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab)
|
||||||
data_arg, local_sym_tab, struct_sym_tab, local_var_metadata
|
|
||||||
)
|
|
||||||
|
|
||||||
# BPF_F_CURRENT_CPU is -1 in 32 bit
|
# BPF_F_CURRENT_CPU is -1 in 32 bit
|
||||||
flags_val = ir.Constant(ir.IntType(64), 0xFFFFFFFF)
|
flags_val = ir.Constant(ir.IntType(64), 0xFFFFFFFF)
|
||||||
@ -304,7 +294,6 @@ def handle_helper_call(
|
|||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
map_sym_tab=None,
|
map_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""Process a BPF helper function call and emit the appropriate LLVM IR."""
|
"""Process a BPF helper function call and emit the appropriate LLVM IR."""
|
||||||
|
|
||||||
@ -323,7 +312,6 @@ def handle_helper_call(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Handle direct function calls (e.g., print(), ktime())
|
# Handle direct function calls (e.g., print(), ktime())
|
||||||
|
|||||||
@ -100,7 +100,6 @@ def handle_fstring_print(
|
|||||||
func,
|
func,
|
||||||
local_sym_tab=None,
|
local_sym_tab=None,
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
"""Handle f-string formatting for bpf_printk emitter."""
|
"""Handle f-string formatting for bpf_printk emitter."""
|
||||||
fmt_parts = []
|
fmt_parts = []
|
||||||
@ -118,7 +117,6 @@ def handle_fstring_print(
|
|||||||
exprs,
|
exprs,
|
||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(f"Unsupported f-string value type: {type(value)}")
|
raise NotImplementedError(f"Unsupported f-string value type: {type(value)}")
|
||||||
@ -138,7 +136,6 @@ def handle_fstring_print(
|
|||||||
builder,
|
builder,
|
||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
args.append(arg_value)
|
args.append(arg_value)
|
||||||
|
|
||||||
@ -158,9 +155,7 @@ def _process_constant_in_fstring(cst, fmt_parts, exprs):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _process_fval(
|
def _process_fval(fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab):
|
||||||
fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab, local_var_metadata
|
|
||||||
):
|
|
||||||
"""Process formatted values in f-string."""
|
"""Process formatted values in f-string."""
|
||||||
logger.debug(f"Processing formatted value: {ast.dump(fval)}")
|
logger.debug(f"Processing formatted value: {ast.dump(fval)}")
|
||||||
|
|
||||||
@ -173,7 +168,6 @@ def _process_fval(
|
|||||||
exprs,
|
exprs,
|
||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
@ -188,9 +182,7 @@ def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab):
|
|||||||
_populate_fval(var_type, name_node, fmt_parts, exprs)
|
_populate_fval(var_type, name_node, fmt_parts, exprs)
|
||||||
|
|
||||||
|
|
||||||
def _process_attr_in_fval(
|
def _process_attr_in_fval(attr_node, fmt_parts, exprs, local_sym_tab, struct_sym_tab):
|
||||||
attr_node, fmt_parts, exprs, local_sym_tab, struct_sym_tab, local_var_metadata
|
|
||||||
):
|
|
||||||
"""Process attribute nodes in formatted values."""
|
"""Process attribute nodes in formatted values."""
|
||||||
if (
|
if (
|
||||||
isinstance(attr_node.value, ast.Name)
|
isinstance(attr_node.value, ast.Name)
|
||||||
@ -200,12 +192,7 @@ def _process_attr_in_fval(
|
|||||||
var_name = attr_node.value.id
|
var_name = attr_node.value.id
|
||||||
field_name = attr_node.attr
|
field_name = attr_node.attr
|
||||||
|
|
||||||
if not local_var_metadata or var_name not in local_var_metadata:
|
var_type = local_sym_tab[var_name].metadata
|
||||||
raise ValueError(
|
|
||||||
f"Metadata for '{var_name}' not found in local var metadata"
|
|
||||||
)
|
|
||||||
|
|
||||||
var_type = local_var_metadata[var_name]
|
|
||||||
if var_type not in struct_sym_tab:
|
if var_type not in struct_sym_tab:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Struct '{var_type}' for '{var_name}' not in symbol table"
|
f"Struct '{var_type}' for '{var_name}' not in symbol table"
|
||||||
@ -263,9 +250,7 @@ def _create_format_string_global(fmt_str, func, module, builder):
|
|||||||
return builder.bitcast(fmt_gvar, ir.PointerType())
|
return builder.bitcast(fmt_gvar, ir.PointerType())
|
||||||
|
|
||||||
|
|
||||||
def _prepare_expr_args(
|
def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_tab):
|
||||||
expr, func, module, builder, local_sym_tab, struct_sym_tab, local_var_metadata
|
|
||||||
):
|
|
||||||
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
||||||
val, _ = eval_expr(
|
val, _ = eval_expr(
|
||||||
func,
|
func,
|
||||||
@ -275,7 +260,6 @@ def _prepare_expr_args(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
None,
|
None,
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if val:
|
if val:
|
||||||
@ -298,7 +282,7 @@ def _prepare_expr_args(
|
|||||||
return ir.Constant(ir.IntType(64), 0)
|
return ir.Constant(ir.IntType(64), 0)
|
||||||
|
|
||||||
|
|
||||||
def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab, local_var_metadata):
|
def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab):
|
||||||
"""Extract data pointer and size information for perf event output."""
|
"""Extract data pointer and size information for perf event output."""
|
||||||
if isinstance(data_arg, ast.Name):
|
if isinstance(data_arg, ast.Name):
|
||||||
data_name = data_arg.id
|
data_name = data_arg.id
|
||||||
@ -310,22 +294,14 @@ def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab, local_var_met
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Check if data_name is a struct
|
# Check if data_name is a struct
|
||||||
if local_var_metadata and data_name in local_var_metadata:
|
data_type = local_sym_tab[data_name].metadata
|
||||||
data_type = local_var_metadata[data_name]
|
if data_type in struct_sym_tab:
|
||||||
if data_type in struct_sym_tab:
|
struct_info = struct_sym_tab[data_type]
|
||||||
struct_info = struct_sym_tab[data_type]
|
size_val = ir.Constant(ir.IntType(64), struct_info.size)
|
||||||
size_val = ir.Constant(ir.IntType(64), struct_info.size)
|
return data_ptr, size_val
|
||||||
return data_ptr, size_val
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Struct {data_type} for {data_name} not in symbol table."
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(f"Struct {data_type} for {data_name} not in symbol table.")
|
||||||
f"Metadata for variable {data_name} "
|
|
||||||
"not found in local variable metadata."
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"Only simple object names are supported " "as data in perf event output."
|
"Only simple object names are supported as data in perf event output."
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user