mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 18:08:36 +00:00
Compare commits
4 Commits
testing-fr
...
fix/probe-
| Author | SHA1 | Date | |
|---|---|---|---|
| d7a763639a | |||
| b47063c49c | |||
| c2de7dad96 | |||
| d1bb0191c9 |
2
Makefile
2
Makefile
@ -7,7 +7,7 @@ clean:
|
|||||||
rm -rf htmlcov .coverage
|
rm -rf htmlcov .coverage
|
||||||
|
|
||||||
test:
|
test:
|
||||||
pytest tests/ -v --tb=short -m "not verifier"
|
pytest tests/ -W ignore::DeprecationWarning -v --tb=short -m "not verifier"
|
||||||
|
|
||||||
test-cov:
|
test-cov:
|
||||||
pytest tests/ -v --tb=short -m "not verifier" \
|
pytest tests/ -v --tb=short -m "not verifier" \
|
||||||
|
|||||||
@ -340,9 +340,15 @@ def _allocate_for_attribute(
|
|||||||
|
|
||||||
struct_type: type = local_sym_tab[struct_var].metadata
|
struct_type: type = local_sym_tab[struct_var].metadata
|
||||||
if not struct_type or struct_type not in structs_sym_tab:
|
if not struct_type or struct_type not in structs_sym_tab:
|
||||||
if VmlinuxHandlerRegistry.is_vmlinux_struct(struct_type.__name__):
|
# `metadata` only names a struct for struct-typed symbols. For anything
|
||||||
|
# else (None, an IR type, or a plain ctypes class such as a `c_void_p`
|
||||||
|
# context parameter) there is no vmlinux struct name to look up, so guard
|
||||||
|
# the attribute access instead of blowing up with an AttributeError.
|
||||||
|
vmlinux_struct_name = getattr(struct_type, "__name__", None)
|
||||||
|
if vmlinux_struct_name and VmlinuxHandlerRegistry.is_vmlinux_struct(
|
||||||
|
vmlinux_struct_name
|
||||||
|
):
|
||||||
# Handle vmlinux struct field access
|
# Handle vmlinux struct field access
|
||||||
vmlinux_struct_name = struct_type.__name__
|
|
||||||
if not VmlinuxHandlerRegistry.has_field(vmlinux_struct_name, field_name):
|
if not VmlinuxHandlerRegistry.has_field(vmlinux_struct_name, field_name):
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Field '{field_name}' not found in vmlinux struct '{vmlinux_struct_name}'"
|
f"Field '{field_name}' not found in vmlinux struct '{vmlinux_struct_name}'"
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
import ast
|
import ast
|
||||||
|
import ctypes
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pythonbpf.helper import (
|
from pythonbpf.helper import (
|
||||||
HelperHandlerRegistry,
|
HelperHandlerRegistry,
|
||||||
)
|
)
|
||||||
from pythonbpf.type_deducer import ctypes_to_ir
|
from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes
|
||||||
from pythonbpf.expr import (
|
from pythonbpf.expr import (
|
||||||
eval_expr,
|
eval_expr,
|
||||||
handle_expr,
|
handle_expr,
|
||||||
@ -325,6 +326,9 @@ def process_func_body(
|
|||||||
f"Unsupported annotation type: {ast.dump(context_arg.annotation)}"
|
f"Unsupported annotation type: {ast.dump(context_arg.annotation)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# NOTE: `var` is None for the context parameter. It is the sentinel
|
||||||
|
# that tells consumers the symbol is an incoming function argument
|
||||||
|
# (`func.args[0]`) rather than a stack slot they can load from.
|
||||||
if VmlinuxHandlerRegistry.is_vmlinux_struct(context_type_name):
|
if VmlinuxHandlerRegistry.is_vmlinux_struct(context_type_name):
|
||||||
resolved_type = VmlinuxHandlerRegistry.get_struct_type(
|
resolved_type = VmlinuxHandlerRegistry.get_struct_type(
|
||||||
context_type_name
|
context_type_name
|
||||||
@ -332,6 +336,18 @@ def process_func_body(
|
|||||||
context_type = LocalSymbol(None, None, resolved_type)
|
context_type = LocalSymbol(None, None, resolved_type)
|
||||||
local_sym_tab[context_name] = context_type
|
local_sym_tab[context_name] = context_type
|
||||||
logger.info(f"Added argument '{context_name}' to local symbol table")
|
logger.info(f"Added argument '{context_name}' to local symbol table")
|
||||||
|
elif is_ctypes(context_type_name):
|
||||||
|
# Plain ctypes annotation, e.g. `ctx: c_void_p`. Register it so
|
||||||
|
# helpers that take the raw context (probe_read, ...) can resolve
|
||||||
|
# the name. Metadata is the annotated Python type, mirroring the
|
||||||
|
# vmlinux branch above.
|
||||||
|
context_type = LocalSymbol(
|
||||||
|
None,
|
||||||
|
ir.PointerType(),
|
||||||
|
getattr(ctypes, context_type_name, None),
|
||||||
|
)
|
||||||
|
local_sym_tab[context_name] = context_type
|
||||||
|
logger.info(f"Added argument '{context_name}' to local symbol table")
|
||||||
|
|
||||||
# pre-allocate dynamic variables
|
# pre-allocate dynamic variables
|
||||||
local_sym_tab = allocate_mem(
|
local_sym_tab = allocate_mem(
|
||||||
|
|||||||
@ -50,6 +50,18 @@ def get_or_create_ptr_from_arg(
|
|||||||
logger.info(f"Getting pointer from arg: {ast.dump(arg)}")
|
logger.info(f"Getting pointer from arg: {ast.dump(arg)}")
|
||||||
sz = None
|
sz = None
|
||||||
if isinstance(arg, ast.Name):
|
if isinstance(arg, ast.Name):
|
||||||
|
symbol = local_sym_tab.get(arg.id) if local_sym_tab else None
|
||||||
|
if symbol is not None and symbol.var is None:
|
||||||
|
# A None `var` marks the context parameter (see process_func_body):
|
||||||
|
# it arrives as the function's first argument, not as a stack slot,
|
||||||
|
# so there is no alloca to hand back. Use the argument itself.
|
||||||
|
if not func.args:
|
||||||
|
raise ValueError(
|
||||||
|
f"'{arg.id}' is the context parameter but "
|
||||||
|
f"'{func.name}' takes no arguments"
|
||||||
|
)
|
||||||
|
ptr = builder.bitcast(func.args[0], ir.PointerType())
|
||||||
|
else:
|
||||||
# Stack space is already allocated
|
# Stack space is already allocated
|
||||||
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
|
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
|
||||||
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
|
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
|
||||||
|
|||||||
Reference in New Issue
Block a user