|
9 | 9 |
|
10 | 10 | @lru_cache(maxsize=1) |
11 | 11 | 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 |
14 | 14 |
|
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") |
17 | 17 | current_symbol_name = node.name |
18 | 18 | if current_symbol_name not in symbols_in_module: |
19 | 19 | raise ImportError(f"{current_symbol_name} not present in module vmlinux") |
20 | 20 | 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 |
23 | 46 |
|
0 commit comments