mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
add support with ctypes getattr offset. Also supports bitfields.
* breaks when struct_ring_buffer_per_cpu
This commit is contained in:
@ -25,7 +25,7 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
|
|||||||
|
|
||||||
|
|
||||||
def process_vmlinux_post_ast(
|
def process_vmlinux_post_ast(
|
||||||
elem_type_class, llvm_handler, handler: DependencyHandler, processing_stack=None
|
elem_type_class, llvm_handler, handler: DependencyHandler, processing_stack=None
|
||||||
):
|
):
|
||||||
# Initialize processing stack on first call
|
# Initialize processing stack on first call
|
||||||
if processing_stack is None:
|
if processing_stack is None:
|
||||||
@ -60,6 +60,10 @@ def process_vmlinux_post_ast(
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
new_dep_node = DependencyNode(name=current_symbol_name)
|
new_dep_node = DependencyNode(name=current_symbol_name)
|
||||||
|
|
||||||
|
# elem_type_class is the actual vmlinux struct/class
|
||||||
|
new_dep_node.set_ctype_struct(elem_type_class)
|
||||||
|
|
||||||
handler.add_node(new_dep_node)
|
handler.add_node(new_dep_node)
|
||||||
class_obj = getattr(imported_module, current_symbol_name)
|
class_obj = getattr(imported_module, current_symbol_name)
|
||||||
# Inspect the class fields
|
# Inspect the class fields
|
||||||
@ -71,9 +75,6 @@ def process_vmlinux_post_ast(
|
|||||||
if len(field_elem) == 2:
|
if len(field_elem) == 2:
|
||||||
field_name, field_type = field_elem
|
field_name, field_type = field_elem
|
||||||
elif len(field_elem) == 3:
|
elif len(field_elem) == 3:
|
||||||
raise NotImplementedError(
|
|
||||||
"Bitfields are not supported in the current version"
|
|
||||||
)
|
|
||||||
field_name, field_type, bitfield_size = field_elem
|
field_name, field_type, bitfield_size = field_elem
|
||||||
field_table[field_name] = [field_type, bitfield_size]
|
field_table[field_name] = [field_type, bitfield_size]
|
||||||
elif hasattr(class_obj, "__annotations__"):
|
elif hasattr(class_obj, "__annotations__"):
|
||||||
|
|||||||
@ -35,7 +35,7 @@ class Field:
|
|||||||
self.ready = True
|
self.ready = True
|
||||||
|
|
||||||
def set_containing_type(
|
def set_containing_type(
|
||||||
self, containing_type: Optional[Any], mark_ready: bool = False
|
self, containing_type: Optional[Any], mark_ready: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set the containing_type of this field and optionally mark it as ready."""
|
"""Set the containing_type of this field and optionally mark it as ready."""
|
||||||
self.containing_type = containing_type
|
self.containing_type = containing_type
|
||||||
@ -49,7 +49,7 @@ class Field:
|
|||||||
self.ready = True
|
self.ready = True
|
||||||
|
|
||||||
def set_ctype_complex_type(
|
def set_ctype_complex_type(
|
||||||
self, ctype_complex_type: Any, mark_ready: bool = False
|
self, ctype_complex_type: Any, mark_ready: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set the ctype_complex_type of this field and optionally mark it as ready."""
|
"""Set the ctype_complex_type of this field and optionally mark it as ready."""
|
||||||
self.ctype_complex_type = ctype_complex_type
|
self.ctype_complex_type = ctype_complex_type
|
||||||
@ -116,18 +116,19 @@ class DependencyNode:
|
|||||||
fields: Dict[str, Field] = field(default_factory=dict)
|
fields: Dict[str, Field] = field(default_factory=dict)
|
||||||
_ready_cache: Optional[bool] = field(default=None, repr=False)
|
_ready_cache: Optional[bool] = field(default=None, repr=False)
|
||||||
current_offset: int = 0
|
current_offset: int = 0
|
||||||
|
ctype_struct: Optional[Any] = field(default=None, repr=False)
|
||||||
|
|
||||||
def add_field(
|
def add_field(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
field_type: type,
|
field_type: type,
|
||||||
initial_value: Any = None,
|
initial_value: Any = None,
|
||||||
containing_type: Optional[Any] = None,
|
containing_type: Optional[Any] = None,
|
||||||
type_size: Optional[int] = None,
|
type_size: Optional[int] = None,
|
||||||
ctype_complex_type: Optional[int] = None,
|
ctype_complex_type: Optional[int] = None,
|
||||||
bitfield_size: Optional[int] = None,
|
bitfield_size: Optional[int] = None,
|
||||||
ready: bool = False,
|
ready: bool = False,
|
||||||
offset: int = 0,
|
offset: int = 0,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add a field to the node with an optional initial value and readiness state."""
|
"""Add a field to the node with an optional initial value and readiness state."""
|
||||||
if self.depends_on is None:
|
if self.depends_on is None:
|
||||||
@ -146,7 +147,14 @@ class DependencyNode:
|
|||||||
# Invalidate readiness cache
|
# Invalidate readiness cache
|
||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
|
def set_ctype_struct(self, ctype_struct: Any) -> None:
|
||||||
|
"""Set the ctypes structure for automatic offset calculation."""
|
||||||
|
self.ctype_struct = ctype_struct
|
||||||
|
|
||||||
def __sizeof__(self):
|
def __sizeof__(self):
|
||||||
|
# If we have a ctype_struct, use its size
|
||||||
|
if self.ctype_struct is not None:
|
||||||
|
return ctypes.sizeof(self.ctype_struct)
|
||||||
return self.current_offset
|
return self.current_offset
|
||||||
|
|
||||||
def get_field(self, name: str) -> Field:
|
def get_field(self, name: str) -> Field:
|
||||||
@ -172,7 +180,7 @@ class DependencyNode:
|
|||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
def set_field_containing_type(
|
def set_field_containing_type(
|
||||||
self, name: str, containing_type: Any, mark_ready: bool = False
|
self, name: str, containing_type: Any, mark_ready: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set a field's containing_type and optionally mark it as ready."""
|
"""Set a field's containing_type and optionally mark it as ready."""
|
||||||
if name not in self.fields:
|
if name not in self.fields:
|
||||||
@ -183,7 +191,7 @@ class DependencyNode:
|
|||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
def set_field_type_size(
|
def set_field_type_size(
|
||||||
self, name: str, type_size: Any, mark_ready: bool = False
|
self, name: str, type_size: Any, mark_ready: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set a field's type_size and optionally mark it as ready."""
|
"""Set a field's type_size and optionally mark it as ready."""
|
||||||
if name not in self.fields:
|
if name not in self.fields:
|
||||||
@ -194,7 +202,7 @@ class DependencyNode:
|
|||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
def set_field_ctype_complex_type(
|
def set_field_ctype_complex_type(
|
||||||
self, name: str, ctype_complex_type: Any, mark_ready: bool = False
|
self, name: str, ctype_complex_type: Any, mark_ready: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set a field's ctype_complex_type and optionally mark it as ready."""
|
"""Set a field's ctype_complex_type and optionally mark it as ready."""
|
||||||
if name not in self.fields:
|
if name not in self.fields:
|
||||||
@ -205,7 +213,7 @@ class DependencyNode:
|
|||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
def set_field_bitfield_size(
|
def set_field_bitfield_size(
|
||||||
self, name: str, bitfield_size: Any, mark_ready: bool = False
|
self, name: str, bitfield_size: Any, mark_ready: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set a field's bitfield_size and optionally mark it as ready."""
|
"""Set a field's bitfield_size and optionally mark it as ready."""
|
||||||
if name not in self.fields:
|
if name not in self.fields:
|
||||||
@ -216,23 +224,35 @@ class DependencyNode:
|
|||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
def set_field_ready(
|
def set_field_ready(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
is_ready: bool = False,
|
is_ready: bool = False,
|
||||||
size_of_containing_type: Optional[int] = None,
|
size_of_containing_type: Optional[int] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Mark a field as ready or not ready."""
|
"""Mark a field as ready or not ready."""
|
||||||
if name not in self.fields:
|
if name not in self.fields:
|
||||||
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
|
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
|
||||||
|
|
||||||
self.fields[name].set_ready(is_ready)
|
self.fields[name].set_ready(is_ready)
|
||||||
self.fields[name].set_offset(self.current_offset)
|
|
||||||
self.current_offset += self._calculate_size(name, size_of_containing_type)
|
# Use ctypes built-in offset if available
|
||||||
|
if self.ctype_struct is not None:
|
||||||
|
try:
|
||||||
|
self.fields[name].set_offset(getattr(self.ctype_struct, name).offset)
|
||||||
|
except AttributeError:
|
||||||
|
# Fallback to manual calculation if field not found in ctype_struct
|
||||||
|
self.fields[name].set_offset(self.current_offset)
|
||||||
|
self.current_offset += self._calculate_size(name, size_of_containing_type)
|
||||||
|
else:
|
||||||
|
# Manual offset calculation when no ctype_struct is available
|
||||||
|
self.fields[name].set_offset(self.current_offset)
|
||||||
|
self.current_offset += self._calculate_size(name, size_of_containing_type)
|
||||||
|
|
||||||
# Invalidate readiness cache
|
# Invalidate readiness cache
|
||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
def _calculate_size(
|
def _calculate_size(
|
||||||
self, name: str, size_of_containing_type: Optional[int] = None
|
self, name: str, size_of_containing_type: Optional[int] = None
|
||||||
) -> int:
|
) -> int:
|
||||||
processing_field = self.fields[name]
|
processing_field = self.fields[name]
|
||||||
# size_of_field will be in bytes
|
# size_of_field will be in bytes
|
||||||
@ -240,17 +260,16 @@ class DependencyNode:
|
|||||||
size_of_field = ctypes.sizeof(processing_field.type)
|
size_of_field = ctypes.sizeof(processing_field.type)
|
||||||
return size_of_field
|
return size_of_field
|
||||||
elif processing_field.type.__module__ == "vmlinux":
|
elif processing_field.type.__module__ == "vmlinux":
|
||||||
#TODO: does not take into account offset calculation when not array but has type size
|
|
||||||
if processing_field.ctype_complex_type is not None:
|
if processing_field.ctype_complex_type is not None:
|
||||||
if issubclass(processing_field.ctype_complex_type, ctypes.Array):
|
if issubclass(processing_field.ctype_complex_type, ctypes.Array):
|
||||||
if processing_field.containing_type.__module__ == ctypes.__name__:
|
if processing_field.containing_type.__module__ == ctypes.__name__:
|
||||||
if (
|
if (
|
||||||
processing_field.containing_type is not None
|
processing_field.containing_type is not None
|
||||||
and processing_field.type_size is not None
|
and processing_field.type_size is not None
|
||||||
):
|
):
|
||||||
size_of_field = (
|
size_of_field = (
|
||||||
ctypes.sizeof(processing_field.containing_type)
|
ctypes.sizeof(processing_field.containing_type)
|
||||||
* processing_field.type_size
|
* processing_field.type_size
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
@ -259,11 +278,11 @@ class DependencyNode:
|
|||||||
return size_of_field
|
return size_of_field
|
||||||
elif processing_field.containing_type.__module__ == "vmlinux":
|
elif processing_field.containing_type.__module__ == "vmlinux":
|
||||||
if (
|
if (
|
||||||
size_of_containing_type is not None
|
size_of_containing_type is not None
|
||||||
and processing_field.type_size is not None
|
and processing_field.type_size is not None
|
||||||
):
|
):
|
||||||
size_of_field = (
|
size_of_field = (
|
||||||
size_of_containing_type * processing_field.type_size
|
size_of_containing_type * processing_field.type_size
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
@ -276,8 +295,28 @@ class DependencyNode:
|
|||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"This subclass of ctype not supported yet"
|
"This subclass of ctype not supported yet"
|
||||||
)
|
)
|
||||||
|
elif processing_field.type_size is not None:
|
||||||
|
# Handle vmlinux types with type_size but no ctype_complex_type
|
||||||
|
# This means it's a direct vmlinux struct field (not array/pointer wrapped)
|
||||||
|
# The type_size should already contain the full size of the struct
|
||||||
|
# But if there's a containing_type from vmlinux, we need that size
|
||||||
|
if processing_field.containing_type is not None:
|
||||||
|
if processing_field.containing_type.__module__ == "vmlinux":
|
||||||
|
# For vmlinux containing types, we need the pre-calculated size
|
||||||
|
if size_of_containing_type is not None:
|
||||||
|
return size_of_containing_type * processing_field.type_size
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Field {name}: vmlinux containing_type requires size_of_containing_type"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ModuleNotFoundError(
|
||||||
|
f"Containing type module {processing_field.containing_type.__module__} not supported"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Wrong type found with no containing type")
|
||||||
else:
|
else:
|
||||||
# search up pre-created stuff and get size
|
# No ctype_complex_type and no type_size, must rely on size_of_containing_type
|
||||||
if size_of_containing_type is None:
|
if size_of_containing_type is None:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"Size of containing type {size_of_containing_type} is None"
|
f"Size of containing type {size_of_containing_type} is None"
|
||||||
|
|||||||
Reference in New Issue
Block a user