@@ -86,96 +86,14 @@ def process_bpf_chunk(func_node, module, return_type):
8686
8787 return func
8888
89-
90- def create_bpf_map (module , map_name , map_params ):
91- """Create a BPF map in the module with the given parameters"""
92-
93- key_type_str = map_params .get ('key_type' , 'c_uint32' )
94- value_type_str = map_params .get ('value_type' , 'c_uint32' )
95-
96- key_type = ctypes_to_ir (key_type_str )
97- value_type = ctypes_to_ir (value_type_str )
98-
99- map_struct_type = ir .LiteralStructType ([
100- ir .PointerType (), # type
101- ir .PointerType (), # max_entries
102- ir .PointerType (), # key_type
103- ir .PointerType () # value_type
104- ])
105-
106- map_global = ir .GlobalVariable (module , map_struct_type , name = map_name )
107- map_global .linkage = 'external'
108- map_global .initializer = ir .Constant (
109- map_struct_type , [None , None , None , None ])
110- map_global .section = ".maps"
111- map_global .align = 8
112-
113- # TODO: Store map parameters in metadata or a suitable structure
114- # maps[map_name] = {
115- # 'global': map_global,
116- # 'key_type': key_type,
117- # 'value_type': value_type,
118- # 'max_entries': map_params.get('max_entries', 1),
119- # 'map_type': map_params.get('map_type', 'BPF_MAP_TYPE_HASH')
120- # }
121-
122- print (f"Created BPF map: { map_name } " )
123- return map_global
124-
125-
126- def process_bpf_global (func_node , module ):
127- """Process a BPF global (a function decorated with @bpfglobal)"""
128- global_name = func_node .name
129- print (f"Processing BPF global: { global_name } " )
130-
131- # For now, assume single return statement
132- return_stmt = None
133- for stmt in func_node .body :
134- if isinstance (stmt , ast .Return ):
135- return_stmt = stmt
136- break
137- if return_stmt is None :
138- raise ValueError ("BPF global must have a return statement" )
139-
140- rval = return_stmt .value
141-
142- # For now, just handle maps
143- if isinstance (rval , ast .Call ) and isinstance (rval .func , ast .Name ) and rval .func .id == "HashMap" :
144- print (f"Creating HashMap global: { global_name } " )
145- map_params = {'map_type' : 'HASH' }
146- # Handle positional arguments
147- if rval .args :
148- # Assuming order is: key_type, value_type, max_entries
149- if len (rval .args ) >= 1 and isinstance (rval .args [0 ], ast .Name ):
150- map_params ['key_type' ] = rval .args [0 ].id
151- if len (rval .args ) >= 2 and isinstance (rval .args [1 ], ast .Name ):
152- map_params ['value_type' ] = rval .args [1 ].id
153- if len (rval .args ) >= 3 and isinstance (rval .args [2 ], ast .Constant ):
154- map_params ['max_entries' ] = rval .args [2 ].value
155-
156- # Handle keyword arguments (these will override any positional args)
157- for keyword in rval .keywords :
158- if keyword .arg == "key_type" and isinstance (keyword .value , ast .Name ):
159- map_params ['key_type' ] = keyword .value .id
160- elif keyword .arg == "value_type" and isinstance (keyword .value , ast .Name ):
161- map_params ['value_type' ] = keyword .value .id
162- elif keyword .arg == "max_entries" and isinstance (keyword .value , ast .Constant ):
163- map_params ['max_entries' ] = keyword .value .value
164- print (f"Map parameters: { map_params } " )
165- print (create_bpf_map (module , global_name , map_params ))
166-
167-
16889def func_proc (tree , module , chunks ):
16990 for func_node in chunks :
170- # Check if this function is a global
17191 is_global = False
17292 for decorator in func_node .decorator_list :
173- if isinstance (decorator , ast .Name ) and decorator .id == "bpfglobal " :
93+ if isinstance (decorator , ast .Name ) and decorator .id == "map " :
17494 is_global = True
17595 break
17696 if is_global :
177- print (f"Found BPF global: { func_node .name } " )
178- process_bpf_global (func_node , module )
17997 continue
18098 func_type = get_probe_string (func_node )
18199 print (f"Found probe_string of { func_node .name } : { func_type } " )
0 commit comments