Skip to content

Commit f884410

Browse files
add support for single depth pointer resolution
1 parent 3343bed commit f884410

File tree

3 files changed

+81
-17
lines changed

3 files changed

+81
-17
lines changed

pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
6161
return True
6262
else:
6363
new_dep_node = DependencyNode(name=current_symbol_name)
64+
handler.add_node(new_dep_node)
6465
for elem_name, elem_type in field_table.items():
6566
module_name = getattr(elem_type, "__module__", None)
6667
if module_name == ctypes.__name__:
@@ -69,36 +70,48 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
6970
new_dep_node.add_field(elem_name, elem_type, ready=False)
7071
print("elem_name:", elem_name, "elem_type:", elem_type)
7172
# currently fails when a non-normal type appears which is basically everytime
72-
identify_ctypes_type(elem_type)
73+
identify_ctypes_type(elem_name, elem_type, new_dep_node)
7374
symbol_name = (
7475
elem_type.__name__
7576
if hasattr(elem_type, "__name__")
7677
else str(elem_type)
7778
)
78-
vmlinux_symbol = getattr(imported_module, symbol_name)
79+
vmlinux_symbol = None
80+
if hasattr(elem_type, "_type_"):
81+
containing_module_name = getattr(
82+
(elem_type._type_), "__module__", None
83+
)
84+
if containing_module_name == ctypes.__name__:
85+
new_dep_node.set_field_ready(elem_name, True)
86+
continue
87+
elif containing_module_name == "vmlinux":
88+
symbol_name = (
89+
(elem_type._type_).__name__
90+
if hasattr((elem_type._type_), "__name__")
91+
else str(elem_type._type_)
92+
)
93+
vmlinux_symbol = getattr(imported_module, symbol_name)
94+
else:
95+
vmlinux_symbol = getattr(imported_module, symbol_name)
7996
if process_vmlinux_class(vmlinux_symbol, llvm_module, handler):
8097
new_dep_node.set_field_ready(elem_name, True)
8198
else:
8299
raise ValueError(
83100
f"{elem_name} with type {elem_type} not supported in recursive resolver"
84101
)
85-
handler.add_node(new_dep_node)
86102
logger.info(f"added node: {current_symbol_name}")
87103

88104
return True
89105

90106

91-
def identify_ctypes_type(t):
92-
if isinstance(t, type): # t is a type/class
93-
if issubclass(t, ctypes.Array):
94-
print("Array type")
95-
print("Element type:", t._type_)
96-
print("Length:", t._length_)
97-
elif issubclass(t, ctypes._Pointer):
98-
print("Pointer type")
99-
print("Points to:", t._type_)
100-
elif issubclass(t, ctypes._SimpleCData):
101-
print("Scalar type")
102-
print("Base type:", t)
107+
def identify_ctypes_type(elem_name, elem_type, new_dep_node: DependencyNode):
108+
if isinstance(elem_type, type):
109+
if issubclass(elem_type, ctypes.Array):
110+
new_dep_node.set_field_type(elem_name, ctypes.Array)
111+
new_dep_node.set_field_containing_type(elem_name, elem_type._type_)
112+
new_dep_node.set_field_type_size(elem_name, elem_type._length_)
113+
elif issubclass(elem_type, ctypes._Pointer):
114+
new_dep_node.set_field_type(elem_name, ctypes._Pointer)
115+
new_dep_node.set_field_containing_type(elem_name, elem_type._type_)
103116
else:
104117
raise TypeError("Instance sent instead of Class")

pythonbpf/vmlinux_parser/dependency_node.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass, field
22
from typing import Dict, Any, Optional
33

4-
4+
#TODO: FIX THE FUCKING TYPE NAME CONVENTION.
55
@dataclass
66
class Field:
77
"""Represents a field in a dependency node with its type and readiness state."""
@@ -23,6 +23,26 @@ def set_value(self, value: Any, mark_ready: bool = True) -> None:
2323
if mark_ready:
2424
self.ready = True
2525

26+
def set_type(self, given_type, mark_ready: bool = True) -> None:
27+
"""Set value of the type field and mark as ready"""
28+
self.type = given_type
29+
if mark_ready:
30+
self.ready = True
31+
32+
def set_containing_type(
33+
self, containing_type: Optional[Any], mark_ready: bool = True
34+
) -> None:
35+
"""Set the containing_type of this field and optionally mark it as ready."""
36+
self.containing_type = containing_type
37+
if mark_ready:
38+
self.ready = True
39+
40+
def set_type_size(self, type_size: Any, mark_ready: bool = True) -> None:
41+
"""Set the type_size of this field and optionally mark it as ready."""
42+
self.type_size = type_size
43+
if mark_ready:
44+
self.ready = True
45+
2646

2747
@dataclass
2848
class DependencyNode:
@@ -106,6 +126,37 @@ def set_field_value(self, name: str, value: Any, mark_ready: bool = True) -> Non
106126
# Invalidate readiness cache
107127
self._ready_cache = None
108128

129+
def set_field_type(self, name: str, type: Any, mark_ready: bool = True) -> None:
130+
"""Set a field's type and optionally mark it as ready."""
131+
if name not in self.fields:
132+
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
133+
134+
self.fields[name].set_type(type, mark_ready)
135+
# Invalidate readiness cache
136+
self._ready_cache = None
137+
138+
def set_field_containing_type(
139+
self, name: str, containing_type: Any, mark_ready: bool = True
140+
) -> None:
141+
"""Set a field's containing_type and optionally mark it as ready."""
142+
if name not in self.fields:
143+
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
144+
145+
self.fields[name].set_containing_type(containing_type, mark_ready)
146+
# Invalidate readiness cache
147+
self._ready_cache = None
148+
149+
def set_field_type_size(
150+
self, name: str, type_size: Any, mark_ready: bool = True
151+
) -> None:
152+
"""Set a field's type_size and optionally mark it as ready."""
153+
if name not in self.fields:
154+
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
155+
156+
self.fields[name].set_type_size(type_size, mark_ready)
157+
# Invalidate readiness cache
158+
self._ready_cache = None
159+
109160
def set_field_ready(self, name: str, is_ready: bool = True) -> None:
110161
"""Mark a field as ready or not ready."""
111162
if name not in self.fields:

tests/failing_tests/xdp_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pythonbpf.maps import HashMap
33
from pythonbpf.helper import XDP_PASS
44
from vmlinux import struct_xdp_md
5-
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
5+
# from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
66
from vmlinux import struct_xdp_buff # noqa: F401
77
from ctypes import c_int64
88

0 commit comments

Comments
 (0)