|
3 | 3 | from .expr_pass import eval_expr |
4 | 4 |
|
5 | 5 |
|
6 | | -def bpf_ktime_get_ns_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, local_var_metadata=None): |
| 6 | +def bpf_ktime_get_ns_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, struct_sym_tab=None, local_var_metadata=None): |
7 | 7 | """ |
8 | 8 | Emit LLVM IR for bpf_ktime_get_ns helper function call. |
9 | 9 | """ |
@@ -63,7 +63,7 @@ def bpf_map_lookup_elem_emitter(call, map_ptr, module, builder, func, local_sym_ |
63 | 63 | return result, ir.PointerType() |
64 | 64 |
|
65 | 65 |
|
66 | | -def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, local_var_metadata=None): |
| 66 | +def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, struct_sym_tab=None, local_var_metadata=None): |
67 | 67 | if not hasattr(func, "_fmt_counter"): |
68 | 68 | func._fmt_counter = 0 |
69 | 69 |
|
@@ -101,10 +101,42 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, |
101 | 101 | else: |
102 | 102 | raise NotImplementedError( |
103 | 103 | "Only integer and pointer types are supported in formatted values.") |
104 | | - print("Formatted value variable:", var_ptr, var_type) |
105 | 104 | else: |
106 | 105 | raise ValueError( |
107 | 106 | f"Variable {value.value.id} not found in local symbol table.") |
| 107 | + elif isinstance(value.value, ast.Attribute): |
| 108 | + # object field access from struct |
| 109 | + if isinstance(value.value.value, ast.Name) and local_sym_tab and value.value.value.id in local_sym_tab: |
| 110 | + var_name = value.value.value.id |
| 111 | + field_name = value.value.attr |
| 112 | + if local_var_metadata and var_name in local_var_metadata: |
| 113 | + var_type = local_var_metadata[var_name] |
| 114 | + if var_type in struct_sym_tab: |
| 115 | + struct_info = struct_sym_tab[var_type] |
| 116 | + if field_name in struct_info["fields"]: |
| 117 | + field_index = struct_info["fields"][field_name] |
| 118 | + field_type = struct_info["field_types"][field_index] |
| 119 | + if isinstance(field_type, ir.IntType): |
| 120 | + fmt_parts.append("%lld") |
| 121 | + exprs.append(value.value) |
| 122 | + elif field_type == ir.PointerType(ir.IntType(8)): |
| 123 | + fmt_parts.append("%s") |
| 124 | + exprs.append(value.value) |
| 125 | + else: |
| 126 | + raise NotImplementedError( |
| 127 | + "Only integer and pointer types are supported in formatted values.") |
| 128 | + else: |
| 129 | + raise ValueError( |
| 130 | + f"Field {field_name} not found in struct {var_type}.") |
| 131 | + else: |
| 132 | + raise ValueError( |
| 133 | + f"Struct type {var_type} for variable {var_name} not found in struct symbol table.") |
| 134 | + else: |
| 135 | + raise ValueError( |
| 136 | + f"Metadata for variable {var_name} not found in local variable metadata.") |
| 137 | + else: |
| 138 | + raise ValueError( |
| 139 | + f"Variable {value.value.value.id} not found in local symbol table.") |
108 | 140 | else: |
109 | 141 | raise NotImplementedError( |
110 | 142 | "Only simple variable names are supported in formatted values.") |
@@ -136,8 +168,9 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, |
136 | 168 | "Warning: bpf_printk supports up to 3 arguments, extra arguments will be ignored.") |
137 | 169 |
|
138 | 170 | for expr in exprs[:3]: |
| 171 | + print(f"{ast.dump(expr)}") |
139 | 172 | val, _ = eval_expr(func, module, builder, |
140 | | - expr, local_sym_tab, None) |
| 173 | + expr, local_sym_tab, None, struct_sym_tab, local_var_metadata) |
141 | 174 | if val: |
142 | 175 | if isinstance(val.type, ir.PointerType): |
143 | 176 | val = builder.ptrtoint(val, ir.IntType(64)) |
@@ -339,7 +372,7 @@ def bpf_map_delete_elem_emitter(call, map_ptr, module, builder, func, local_sym_ |
339 | 372 | return result, None |
340 | 373 |
|
341 | 374 |
|
342 | | -def bpf_get_current_pid_tgid_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, local_var_metadata=None): |
| 375 | +def bpf_get_current_pid_tgid_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, struct_sym_tab=None, local_var_metadata=None): |
343 | 376 | """ |
344 | 377 | Emit LLVM IR for bpf_get_current_pid_tgid helper function call. |
345 | 378 | """ |
@@ -420,11 +453,12 @@ def bpf_perf_event_output_handler(call, map_ptr, module, builder, func, local_sy |
420 | 453 |
|
421 | 454 |
|
422 | 455 | def handle_helper_call(call, module, builder, func, local_sym_tab=None, map_sym_tab=None, struct_sym_tab=None, local_var_metadata=None): |
| 456 | + print(local_var_metadata) |
423 | 457 | if isinstance(call.func, ast.Name): |
424 | 458 | func_name = call.func.id |
425 | 459 | if func_name in helper_func_list: |
426 | 460 | # it is not a map method call |
427 | | - return helper_func_list[func_name](call, None, module, builder, func, local_sym_tab) |
| 461 | + return helper_func_list[func_name](call, None, module, builder, func, local_sym_tab, struct_sym_tab, local_var_metadata) |
428 | 462 | else: |
429 | 463 | raise NotImplementedError( |
430 | 464 | f"Function {func_name} is not implemented as a helper function.") |
|
0 commit comments