|
2 | 2 | from llvmlite import ir |
3 | 3 | from pythonbpf.expr_pass import eval_expr |
4 | 4 | from enum import Enum |
5 | | -from .helper_utils import HelperHandlerRegistry, get_or_create_ptr_from_arg, get_flags_val, _handle_fstring_print, _simple_string_print |
| 5 | +from .helper_utils import HelperHandlerRegistry, get_or_create_ptr_from_arg, get_flags_val, _handle_fstring_print, _simple_string_print, _get_data_ptr_and_size |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class BPFHelperID(Enum): |
@@ -201,50 +201,31 @@ def bpf_perf_event_output_handler(call, map_ptr, module, builder, func, |
201 | 201 | data_arg = call.args[0] |
202 | 202 | ctx_ptr = func.args[0] # First argument to the function is ctx |
203 | 203 |
|
204 | | - if isinstance(data_arg, ast.Name): |
205 | | - data_name = data_arg.id |
206 | | - if local_sym_tab and data_name in local_sym_tab: |
207 | | - data_ptr = local_sym_tab[data_name][0] |
208 | | - else: |
209 | | - raise ValueError( |
210 | | - f"Data variable {data_name} not found in local symbol table.") |
211 | | - # Check is data_name is a struct |
212 | | - if local_var_metadata and data_name in local_var_metadata: |
213 | | - data_type = local_var_metadata[data_name] |
214 | | - if data_type in struct_sym_tab: |
215 | | - struct_info = struct_sym_tab[data_type] |
216 | | - size_val = ir.Constant(ir.IntType(64), struct_info.size) |
217 | | - else: |
218 | | - raise ValueError( |
219 | | - f"Struct type {data_type} for variable {data_name} not found in struct symbol table.") |
220 | | - else: |
221 | | - raise ValueError( |
222 | | - f"Metadata for variable {data_name} not found in local variable metadata.") |
223 | | - |
224 | | - # BPF_F_CURRENT_CPU is -1 in 32 bit |
225 | | - flags_val = ir.Constant(ir.IntType(64), 0xFFFFFFFF) |
226 | | - |
227 | | - map_void_ptr = builder.bitcast(map_ptr, ir.PointerType()) |
228 | | - data_void_ptr = builder.bitcast(data_ptr, ir.PointerType()) |
229 | | - fn_type = ir.FunctionType( |
230 | | - ir.IntType(64), |
231 | | - [ir.PointerType(ir.IntType(8)), ir.PointerType(), ir.IntType(64), |
232 | | - ir.PointerType(), ir.IntType(64)], |
233 | | - var_arg=False |
234 | | - ) |
235 | | - fn_ptr_type = ir.PointerType(fn_type) |
236 | | - |
237 | | - # helper id |
238 | | - fn_addr = ir.Constant(ir.IntType( |
239 | | - 64), BPFHelperID.BPF_PERF_EVENT_OUTPUT.value) |
240 | | - fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type) |
241 | | - |
242 | | - result = builder.call( |
243 | | - fn_ptr, [ctx_ptr, map_void_ptr, flags_val, data_void_ptr, size_val], tail=False) |
244 | | - return result, None |
245 | | - else: |
246 | | - raise NotImplementedError( |
247 | | - "Only simple object names are supported as data in perf event output.") |
| 204 | + data_ptr, size_val = _get_data_ptr_and_size(data_arg, local_sym_tab, |
| 205 | + struct_sym_tab, |
| 206 | + local_var_metadata) |
| 207 | + |
| 208 | + # BPF_F_CURRENT_CPU is -1 in 32 bit |
| 209 | + flags_val = ir.Constant(ir.IntType(64), 0xFFFFFFFF) |
| 210 | + |
| 211 | + map_void_ptr = builder.bitcast(map_ptr, ir.PointerType()) |
| 212 | + data_void_ptr = builder.bitcast(data_ptr, ir.PointerType()) |
| 213 | + fn_type = ir.FunctionType( |
| 214 | + ir.IntType(64), |
| 215 | + [ir.PointerType(ir.IntType(8)), ir.PointerType(), ir.IntType(64), |
| 216 | + ir.PointerType(), ir.IntType(64)], |
| 217 | + var_arg=False |
| 218 | + ) |
| 219 | + fn_ptr_type = ir.PointerType(fn_type) |
| 220 | + |
| 221 | + # helper id |
| 222 | + fn_addr = ir.Constant(ir.IntType(64), |
| 223 | + BPFHelperID.BPF_PERF_EVENT_OUTPUT.value) |
| 224 | + fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type) |
| 225 | + |
| 226 | + result = builder.call( |
| 227 | + fn_ptr, [ctx_ptr, map_void_ptr, flags_val, data_void_ptr, size_val], tail=False) |
| 228 | + return result, None |
248 | 229 |
|
249 | 230 |
|
250 | 231 | def handle_helper_call(call, module, builder, func, |
|
0 commit comments