Skip to content

Commit 2cf7b28

Browse files
extract fields from the class
1 parent d24d59c commit 2cf7b28

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

pythonbpf/vmlinux_parser/vmlinux_class_handler.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,38 @@
99

1010
@lru_cache(maxsize=1)
1111
def get_module_symbols(module_name: str):
12-
module = importlib.import_module(module_name)
13-
return [name for name in dir(module)]
12+
imported_module = importlib.import_module(module_name)
13+
return [name for name in dir(imported_module)], imported_module
1414

15-
def process_vmlinux_class(node, module, handler: DependencyHandler, parent=""):
16-
symbols_in_module = get_module_symbols("vmlinux")
15+
def process_vmlinux_class(node, llvm_module, handler: DependencyHandler, parent=""):
16+
symbols_in_module, imported_module = get_module_symbols("vmlinux")
1717
current_symbol_name = node.name
1818
if current_symbol_name not in symbols_in_module:
1919
raise ImportError(f"{current_symbol_name} not present in module vmlinux")
2020
logger.info(f"Resolving vmlinux class {current_symbol_name}")
21-
# Now we find fields present in current class
22-
pass
21+
field_table = {} # should contain the field and it's type.
22+
23+
# Get the class object from the module
24+
class_obj = getattr(imported_module, current_symbol_name)
25+
26+
# Below, I've written a general structure that gets class-info
27+
# everytime, no matter the format in which it is present
28+
29+
# Inspect the class fields
30+
# Assuming class_obj has fields stored in some standard way
31+
#If it's a ctypes-like structure with _fields_
32+
if hasattr(class_obj, '_fields_'):
33+
for field_name, field_type in class_obj._fields_:
34+
field_table[field_name] = field_type
35+
36+
# If it's using __annotations__
37+
elif hasattr(class_obj, '__annotations__'):
38+
for field_name, field_type in class_obj.__annotations__.items():
39+
field_table[field_name] = field_type
40+
41+
else:
42+
raise TypeError("Could not get required class and definition")
43+
44+
logger.info(f"Extracted fields for {current_symbol_name}: {field_table}")
45+
return field_table
2346

0 commit comments

Comments
 (0)