mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Fix line length nitpicks
This commit is contained in:
@ -1,8 +1,10 @@
|
|||||||
import ast
|
import ast
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
from pythonbpf.expr_pass import eval_expr
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from .helper_utils import HelperHandlerRegistry, get_or_create_ptr_from_arg, get_flags_val, _handle_fstring_print, _simple_string_print, _get_data_ptr_and_size
|
from .helper_utils import (HelperHandlerRegistry,
|
||||||
|
get_or_create_ptr_from_arg, get_flags_val,
|
||||||
|
handle_fstring_print, simple_string_print,
|
||||||
|
get_data_ptr_and_size)
|
||||||
|
|
||||||
|
|
||||||
class BPFHelperID(Enum):
|
class BPFHelperID(Enum):
|
||||||
@ -74,16 +76,17 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func,
|
|||||||
|
|
||||||
args = []
|
args = []
|
||||||
if isinstance(call.args[0], ast.JoinedStr):
|
if isinstance(call.args[0], ast.JoinedStr):
|
||||||
args = _handle_fstring_print(call.args[0], module, builder, func,
|
args = handle_fstring_print(call.args[0], module, builder, func,
|
||||||
local_sym_tab, struct_sym_tab,
|
local_sym_tab, struct_sym_tab,
|
||||||
local_var_metadata)
|
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 onbly supporting single arguments for now.
|
# TODO: We are onbly supporting single arguments for now.
|
||||||
# In case of multiple args, the first one will be taken.
|
# In case of multiple args, the first one will be taken.
|
||||||
args = _simple_string_print(call.args[0].value, module, builder, func)
|
args = simple_string_print(call.args[0].value, module, builder, func)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"Only simple string literals or f-strings are supported in bpf_printk.")
|
"Only simple strings or f-strings are supported in bpf_printk.")
|
||||||
|
|
||||||
fn_type = ir.FunctionType(
|
fn_type = ir.FunctionType(
|
||||||
ir.IntType(64), [ir.PointerType(), ir.IntType(32)], var_arg=True)
|
ir.IntType(64), [ir.PointerType(), ir.IntType(32)], var_arg=True)
|
||||||
@ -106,8 +109,8 @@ def bpf_map_update_elem_emitter(call, map_ptr, module, builder, func,
|
|||||||
if (not call.args or
|
if (not call.args or
|
||||||
len(call.args) < 2 or
|
len(call.args) < 2 or
|
||||||
len(call.args) > 3):
|
len(call.args) > 3):
|
||||||
raise ValueError("Map update expects 2 or 3 arguments (key, value, flags), got "
|
raise ValueError("Map update expects 2 or 3 args (key, value, flags), "
|
||||||
f"{len(call.args)}")
|
f"got {len(call.args)}")
|
||||||
|
|
||||||
key_arg = call.args[0]
|
key_arg = call.args[0]
|
||||||
value_arg = call.args[1]
|
value_arg = call.args[1]
|
||||||
@ -196,14 +199,14 @@ def bpf_perf_event_output_handler(call, map_ptr, module, builder, func,
|
|||||||
local_sym_tab=None, struct_sym_tab=None,
|
local_sym_tab=None, struct_sym_tab=None,
|
||||||
local_var_metadata=None):
|
local_var_metadata=None):
|
||||||
if len(call.args) != 1:
|
if len(call.args) != 1:
|
||||||
raise ValueError("Perf event output expects exactly one argument (data), got "
|
raise ValueError("Perf event output expects exactly one argument, "
|
||||||
f"{len(call.args)}")
|
f"got {len(call.args)}")
|
||||||
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_arg, local_sym_tab,
|
data_ptr, size_val = get_data_ptr_and_size(data_arg, local_sym_tab,
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
local_var_metadata)
|
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)
|
||||||
@ -224,7 +227,9 @@ def bpf_perf_event_output_handler(call, map_ptr, module, builder, func,
|
|||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
result = builder.call(
|
result = builder.call(
|
||||||
fn_ptr, [ctx_ptr, map_void_ptr, flags_val, data_void_ptr, size_val], tail=False)
|
fn_ptr,
|
||||||
|
[ctx_ptr, map_void_ptr, flags_val, data_void_ptr, size_val],
|
||||||
|
tail=False)
|
||||||
return result, None
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -75,10 +75,10 @@ def get_flags_val(arg, builder, local_sym_tab):
|
|||||||
return arg.value
|
return arg.value
|
||||||
|
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"Only simple variable names or integer constants are supported as flags in map helpers.")
|
"Only var names or int consts are supported as map helpers flags.")
|
||||||
|
|
||||||
|
|
||||||
def _simple_string_print(string_value, module, builder, func):
|
def simple_string_print(string_value, module, builder, func):
|
||||||
"""Emit code for a simple string print statement."""
|
"""Emit code for a simple string print statement."""
|
||||||
fmt_str = string_value + "\n\0"
|
fmt_str = string_value + "\n\0"
|
||||||
fmt_ptr = _create_format_string_global(fmt_str, func, module, builder)
|
fmt_ptr = _create_format_string_global(fmt_str, func, module, builder)
|
||||||
@ -87,9 +87,9 @@ def _simple_string_print(string_value, module, builder, func):
|
|||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def _handle_fstring_print(joined_str, module, builder, func,
|
def handle_fstring_print(joined_str, module, builder, func,
|
||||||
local_sym_tab=None, struct_sym_tab=None,
|
local_sym_tab=None, struct_sym_tab=None,
|
||||||
local_var_metadata=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 = []
|
||||||
exprs = []
|
exprs = []
|
||||||
@ -108,12 +108,12 @@ def _handle_fstring_print(joined_str, module, builder, func,
|
|||||||
f"Unsupported f-string value type: {type(value)}")
|
f"Unsupported f-string value type: {type(value)}")
|
||||||
|
|
||||||
fmt_str = "".join(fmt_parts)
|
fmt_str = "".join(fmt_parts)
|
||||||
args = _simple_string_print(fmt_str, module, builder, func)
|
args = simple_string_print(fmt_str, module, builder, func)
|
||||||
|
|
||||||
# NOTE: Process expressions (limited to 3 due to BPF constraints)
|
# NOTE: Process expressions (limited to 3 due to BPF constraints)
|
||||||
if len(exprs) > 3:
|
if len(exprs) > 3:
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"bpf_printk supports up to 3 arguments, extra arguments will be ignored.")
|
"bpf_printk supports up to 3 args, extra args will be ignored.")
|
||||||
|
|
||||||
for expr in exprs[:3]:
|
for expr in exprs[:3]:
|
||||||
arg_value = _prepare_expr_args(expr, func, module, builder,
|
arg_value = _prepare_expr_args(expr, func, module, builder,
|
||||||
@ -150,7 +150,7 @@ def _process_fval(fval, fmt_parts, exprs,
|
|||||||
local_var_metadata)
|
local_var_metadata)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
f"Unsupported formatted value type in f-string: {type(fval.value)}")
|
f"Unsupported formatted value in f-string: {type(fval.value)}")
|
||||||
|
|
||||||
|
|
||||||
def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab):
|
def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab):
|
||||||
@ -171,12 +171,12 @@ def _process_attr_in_fval(attr_node, fmt_parts, exprs,
|
|||||||
|
|
||||||
if not local_var_metadata or var_name not in local_var_metadata:
|
if not local_var_metadata or var_name not in local_var_metadata:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Variable metadata for '{var_name}' not found in local variable metadata")
|
f"Metadata for '{var_name}' not found in local var metadata")
|
||||||
|
|
||||||
var_type = local_var_metadata[var_name]
|
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 type '{var_type}' for variable '{var_name}' not found in struct symbol table")
|
f"Struct '{var_type}' for '{var_name}' not in symbol table")
|
||||||
|
|
||||||
struct_info = struct_sym_tab[var_type]
|
struct_info = struct_sym_tab[var_type]
|
||||||
if field_name not in struct_info.fields:
|
if field_name not in struct_info.fields:
|
||||||
@ -187,7 +187,7 @@ def _process_attr_in_fval(attr_node, fmt_parts, exprs,
|
|||||||
_populate_fval(field_type, attr_node, fmt_parts, exprs)
|
_populate_fval(field_type, attr_node, fmt_parts, exprs)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"Only simple attribute access on local variables is supported in f-strings.")
|
"Only simple attribute on local vars is supported in f-strings.")
|
||||||
|
|
||||||
|
|
||||||
def _populate_fval(ftype, node, fmt_parts, exprs):
|
def _populate_fval(ftype, node, fmt_parts, exprs):
|
||||||
@ -233,7 +233,7 @@ def _create_format_string_global(fmt_str, func, module, builder):
|
|||||||
def _prepare_expr_args(expr, func, module, builder,
|
def _prepare_expr_args(expr, func, module, builder,
|
||||||
local_sym_tab, struct_sym_tab,
|
local_sym_tab, struct_sym_tab,
|
||||||
local_var_metadata):
|
local_var_metadata):
|
||||||
"""Evaluate and prepare an expression to be used as an argument for bpf_printk."""
|
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
||||||
print(f"{ast.dump(expr)}")
|
print(f"{ast.dump(expr)}")
|
||||||
val, _ = eval_expr(func, module, builder, expr,
|
val, _ = eval_expr(func, module, builder, expr,
|
||||||
local_sym_tab, None, struct_sym_tab,
|
local_sym_tab, None, struct_sym_tab,
|
||||||
@ -247,17 +247,19 @@ def _prepare_expr_args(expr, func, module, builder,
|
|||||||
val = builder.sext(val, ir.IntType(64))
|
val = builder.sext(val, ir.IntType(64))
|
||||||
else:
|
else:
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"Only int and ptr supported in bpf_printk arguments. Others default to 0.")
|
"Only int and ptr supported in bpf_printk args. "
|
||||||
|
"Others default to 0.")
|
||||||
val = ir.Constant(ir.IntType(64), 0)
|
val = ir.Constant(ir.IntType(64), 0)
|
||||||
return val
|
return val
|
||||||
else:
|
else:
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"Failed to evaluate expression for bpf_printk argument. It will be converted to 0.")
|
"Failed to evaluate expression for bpf_printk argument. "
|
||||||
|
"It will be converted to 0.")
|
||||||
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,
|
def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab,
|
||||||
local_var_metadata):
|
local_var_metadata):
|
||||||
"""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
|
||||||
@ -276,10 +278,12 @@ def _get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab,
|
|||||||
return data_ptr, size_val
|
return data_ptr, size_val
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Struct type {data_type} for variable {data_name} not found in struct symbol table.")
|
f"Struct {data_type} for {data_name} not in symbol table.")
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Metadata for variable {data_name} not found in local variable metadata.")
|
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