|
1 | | -from pythonbpf.debuginfo import DebugInfoGenerator |
| 1 | +from pythonbpf.debuginfo import DebugInfoGenerator, dwarf_constants as dc |
| 2 | +from ..dependency_node import DependencyNode |
| 3 | +import ctypes |
| 4 | +import logging |
| 5 | +from typing import List, Any, Tuple |
2 | 6 |
|
| 7 | +logger = logging.getLogger(__name__) |
3 | 8 |
|
4 | | -def debug_info_generation(struct, llvm_module): |
| 9 | + |
| 10 | +def debug_info_generation( |
| 11 | + struct: DependencyNode, |
| 12 | + llvm_module, |
| 13 | + generated_debug_info: List[Tuple[DependencyNode, Any]], |
| 14 | +) -> Any: |
| 15 | + """ |
| 16 | + Generate DWARF debug information for a struct defined in a DependencyNode. |
| 17 | +
|
| 18 | + Args: |
| 19 | + struct: The dependency node containing struct information |
| 20 | + llvm_module: The LLVM module to add debug info to |
| 21 | + generated_debug_info: List of tuples (struct, debug_info) to track generated debug info |
| 22 | +
|
| 23 | + Returns: |
| 24 | + The generated global variable debug info |
| 25 | + """ |
| 26 | + # Set up debug info generator |
5 | 27 | generator = DebugInfoGenerator(llvm_module) |
6 | | - # this is sample debug info generation |
7 | | - # i64type = generator.get_uint64_type() |
8 | 28 |
|
9 | | - struct_type = generator.create_struct_type([], 64 * 4, is_distinct=True) |
| 29 | + # Check if debug info for this struct has already been generated |
| 30 | + for existing_struct, debug_info in generated_debug_info: |
| 31 | + if existing_struct.name == struct.name: |
| 32 | + return debug_info |
| 33 | + |
| 34 | + # Process all fields and create members for the struct |
| 35 | + members = [] |
| 36 | + for field_name, field in struct.fields.items(): |
| 37 | + # Get appropriate debug type for this field |
| 38 | + field_type = _get_field_debug_type( |
| 39 | + field_name, field, generator, struct, generated_debug_info |
| 40 | + ) |
| 41 | + # Create struct member with proper offset |
| 42 | + member = generator.create_struct_member_vmlinux( |
| 43 | + field_name, field_type, field.offset * 8 |
| 44 | + ) |
| 45 | + members.append(member) |
10 | 46 |
|
11 | | - global_var = generator.create_global_var_debug_info( |
12 | | - struct.name, struct_type, is_local=False |
| 47 | + if struct.name.startswith("struct_"): |
| 48 | + struct_name = struct.name.removeprefix("struct_") |
| 49 | + else: |
| 50 | + raise ValueError("Unions are not supported in the current version") |
| 51 | + # Create struct type with all members |
| 52 | + struct_type = generator.create_struct_type_with_name( |
| 53 | + struct_name, members, struct.__sizeof__() * 8, is_distinct=True |
13 | 54 | ) |
14 | 55 |
|
15 | | - return global_var |
| 56 | + return struct_type |
| 57 | + |
| 58 | + |
| 59 | +def _get_field_debug_type( |
| 60 | + field_name: str, |
| 61 | + field, |
| 62 | + generator: DebugInfoGenerator, |
| 63 | + parent_struct: DependencyNode, |
| 64 | + generated_debug_info: List[Tuple[DependencyNode, Any]], |
| 65 | +) -> tuple[Any, int]: |
| 66 | + """ |
| 67 | + Determine the appropriate debug type for a field based on its Python/ctypes type. |
| 68 | +
|
| 69 | + Args: |
| 70 | + field_name: Name of the field |
| 71 | + field: Field object containing type information |
| 72 | + generator: DebugInfoGenerator instance |
| 73 | + parent_struct: The parent struct containing this field |
| 74 | + generated_debug_info: List of already generated debug info |
| 75 | +
|
| 76 | + Returns: |
| 77 | + The debug info type for this field |
| 78 | + """ |
| 79 | + # Handle complex types (arrays, pointers) |
| 80 | + if field.ctype_complex_type is not None: |
| 81 | + if issubclass(field.ctype_complex_type, ctypes.Array): |
| 82 | + # Handle array types |
| 83 | + element_type, base_type_size = _get_basic_debug_type( |
| 84 | + field.containing_type, generator |
| 85 | + ) |
| 86 | + return generator.create_array_type_vmlinux( |
| 87 | + (element_type, base_type_size * field.type_size), field.type_size |
| 88 | + ), field.type_size * base_type_size |
| 89 | + elif issubclass(field.ctype_complex_type, ctypes._Pointer): |
| 90 | + # Handle pointer types |
| 91 | + pointee_type, _ = _get_basic_debug_type(field.containing_type, generator) |
| 92 | + return generator.create_pointer_type(pointee_type), 64 |
| 93 | + |
| 94 | + # Handle other vmlinux types (nested structs) |
| 95 | + if field.type.__module__ == "vmlinux": |
| 96 | + # If it's a struct from vmlinux, check if we've already generated debug info for it |
| 97 | + struct_name = field.type.__name__ |
| 98 | + |
| 99 | + # Look for existing debug info in the list |
| 100 | + for existing_struct, debug_info in generated_debug_info: |
| 101 | + if existing_struct.name == struct_name: |
| 102 | + # Use existing debug info |
| 103 | + return debug_info, existing_struct.__sizeof__() |
| 104 | + |
| 105 | + # If not found, create a forward declaration |
| 106 | + # This will be completed when the actual struct is processed |
| 107 | + logger.warning("Forward declaration in struct created") |
| 108 | + forward_type = generator.create_struct_type([], 0, is_distinct=True) |
| 109 | + return forward_type, 0 |
| 110 | + |
| 111 | + # Handle basic C types |
| 112 | + return _get_basic_debug_type(field.type, generator) |
| 113 | + |
| 114 | + |
| 115 | +def _get_basic_debug_type(ctype, generator: DebugInfoGenerator) -> Any: |
| 116 | + """ |
| 117 | + Map a ctypes type to a DWARF debug type. |
| 118 | +
|
| 119 | + Args: |
| 120 | + ctype: A ctypes type or Python type |
| 121 | + generator: DebugInfoGenerator instance |
| 122 | +
|
| 123 | + Returns: |
| 124 | + The corresponding debug type |
| 125 | + """ |
| 126 | + # Map ctypes to debug info types |
| 127 | + if ctype == ctypes.c_char or ctype == ctypes.c_byte: |
| 128 | + return generator.get_basic_type("char", 8, dc.DW_ATE_signed_char), 8 |
| 129 | + elif ctype == ctypes.c_ubyte or ctype == ctypes.c_uint8: |
| 130 | + return generator.get_basic_type("unsigned char", 8, dc.DW_ATE_unsigned_char), 8 |
| 131 | + elif ctype == ctypes.c_short or ctype == ctypes.c_int16: |
| 132 | + return generator.get_basic_type("short", 16, dc.DW_ATE_signed), 16 |
| 133 | + elif ctype == ctypes.c_ushort or ctype == ctypes.c_uint16: |
| 134 | + return generator.get_basic_type("unsigned short", 16, dc.DW_ATE_unsigned), 16 |
| 135 | + elif ctype == ctypes.c_int or ctype == ctypes.c_int32: |
| 136 | + return generator.get_basic_type("int", 32, dc.DW_ATE_signed), 32 |
| 137 | + elif ctype == ctypes.c_uint or ctype == ctypes.c_uint32: |
| 138 | + return generator.get_basic_type("unsigned int", 32, dc.DW_ATE_unsigned), 32 |
| 139 | + elif ctype == ctypes.c_long: |
| 140 | + return generator.get_basic_type("long", 64, dc.DW_ATE_signed), 64 |
| 141 | + elif ctype == ctypes.c_ulong: |
| 142 | + return generator.get_basic_type("unsigned long", 64, dc.DW_ATE_unsigned), 64 |
| 143 | + elif ctype == ctypes.c_longlong or ctype == ctypes.c_int64: |
| 144 | + return generator.get_basic_type("long long", 64, dc.DW_ATE_signed), 64 |
| 145 | + elif ctype == ctypes.c_ulonglong or ctype == ctypes.c_uint64: |
| 146 | + return generator.get_basic_type( |
| 147 | + "unsigned long long", 64, dc.DW_ATE_unsigned |
| 148 | + ), 64 |
| 149 | + elif ctype == ctypes.c_float: |
| 150 | + return generator.get_basic_type("float", 32, dc.DW_ATE_float), 32 |
| 151 | + elif ctype == ctypes.c_double: |
| 152 | + return generator.get_basic_type("double", 64, dc.DW_ATE_float), 64 |
| 153 | + elif ctype == ctypes.c_bool: |
| 154 | + return generator.get_basic_type("bool", 8, dc.DW_ATE_boolean), 8 |
| 155 | + elif ctype == ctypes.c_char_p: |
| 156 | + char_type = generator.get_basic_type("char", 8, dc.DW_ATE_signed_char), 8 |
| 157 | + return generator.create_pointer_type(char_type) |
| 158 | + elif ctype == ctypes.c_void_p: |
| 159 | + return generator.create_pointer_type(None), 64 |
| 160 | + else: |
| 161 | + return generator.get_uint64_type(), 64 |
0 commit comments