11import ast
2+ from logging import Logger
23from llvmlite import ir
34from enum import Enum
45from .maps_utils import MapProcessorRegistry
56from ..debuginfo import DebugInfoGenerator
67import logging
78
8- logger = logging .getLogger (__name__ )
9+ logger : Logger = logging .getLogger (__name__ )
910
1011
1112def maps_proc (tree , module , chunks ):
1213 """Process all functions decorated with @map to find BPF maps"""
1314 map_sym_tab = {}
1415 for func_node in chunks :
1516 if is_map (func_node ):
16- print (f"Found BPF map: { func_node .name } " )
17+ logger . info (f"Found BPF map: { func_node .name } " )
1718 map_sym_tab [func_node .name ] = process_bpf_map (func_node , module )
1819 return map_sym_tab
1920
@@ -26,8 +27,41 @@ def is_map(func_node):
2627
2728
2829class BPFMapType (Enum ):
30+ UNSPEC = 0
2931 HASH = 1
32+ ARRAY = 2
33+ PROG_ARRAY = 3
3034 PERF_EVENT_ARRAY = 4
35+ PERCPU_HASH = 5
36+ PERCPU_ARRAY = 6
37+ STACK_TRACE = 7
38+ CGROUP_ARRAY = 8
39+ LRU_HASH = 9
40+ LRU_PERCPU_HASH = 10
41+ LPM_TRIE = 11
42+ ARRAY_OF_MAPS = 12
43+ HASH_OF_MAPS = 13
44+ DEVMAP = 14
45+ SOCKMAP = 15
46+ CPUMAP = 16
47+ XSKMAP = 17
48+ SOCKHASH = 18
49+ CGROUP_STORAGE_DEPRECATED = 19
50+ CGROUP_STORAGE = 19
51+ REUSEPORT_SOCKARRAY = 20
52+ PERCPU_CGROUP_STORAGE_DEPRECATED = 21
53+ PERCPU_CGROUP_STORAGE = 21
54+ QUEUE = 22
55+ STACK = 23
56+ SK_STORAGE = 24
57+ DEVMAP_HASH = 25
58+ STRUCT_OPS = 26
59+ RINGBUF = 27
60+ INODE_STORAGE = 28
61+ TASK_STORAGE = 29
62+ BLOOM_FILTER = 30
63+ USER_RINGBUF = 31
64+ CGRP_STORAGE = 32
3165
3266
3367def create_bpf_map (module , map_name , map_params ):
@@ -51,7 +85,7 @@ def create_bpf_map(module, map_name, map_params):
5185
5286
5387def create_map_debug_info (module , map_global , map_name , map_params ):
54- """Generate debug information metadata for BPF map """
88+ """Generate debug information metadata for BPF maps HASH and PERF_EVENT_ARRAY """
5589 generator = DebugInfoGenerator (module )
5690
5791 uint_type = generator .get_uint32_type ()
@@ -112,6 +146,60 @@ def create_map_debug_info(module, map_global, map_name, map_params):
112146 return global_var
113147
114148
149+ def create_ringbuf_debug_info (module , map_global , map_name , map_params ):
150+ """Generate debug information metadata for BPF RINGBUF map"""
151+ generator = DebugInfoGenerator (module )
152+
153+ int_type = generator .get_int32_type ()
154+
155+ type_array = generator .create_array_type (
156+ int_type , map_params .get ("type" , BPFMapType .RINGBUF ).value
157+ )
158+ type_ptr = generator .create_pointer_type (type_array , 64 )
159+ type_member = generator .create_struct_member ("type" , type_ptr , 0 )
160+
161+ max_entries_array = generator .create_array_type (int_type , map_params ["max_entries" ])
162+ max_entries_ptr = generator .create_pointer_type (max_entries_array , 64 )
163+ max_entries_member = generator .create_struct_member (
164+ "max_entries" , max_entries_ptr , 64
165+ )
166+
167+ elements_arr = [type_member , max_entries_member ]
168+
169+ struct_type = generator .create_struct_type (elements_arr , 128 , is_distinct = True )
170+
171+ global_var = generator .create_global_var_debug_info (
172+ map_name , struct_type , is_local = False
173+ )
174+ map_global .set_metadata ("dbg" , global_var )
175+ return global_var
176+
177+
178+ @MapProcessorRegistry .register ("RingBuf" )
179+ def process_ringbuf_map (map_name , rval , module ):
180+ """Process a BPF_RINGBUF map declaration"""
181+ logger .info (f"Processing Ringbuf: { map_name } " )
182+ map_params = {"type" : BPFMapType .RINGBUF }
183+
184+ # Parse max_entries if present
185+ if len (rval .args ) >= 1 and isinstance (rval .args [0 ], ast .Constant ):
186+ const_val = rval .args [0 ].value
187+ if isinstance (const_val , int ):
188+ map_params ["max_entries" ] = const_val
189+
190+ for keyword in rval .keywords :
191+ if keyword .arg == "max_entries" and isinstance (keyword .value , ast .Constant ):
192+ const_val = keyword .value .value
193+ if isinstance (const_val , int ):
194+ map_params ["max_entries" ] = const_val
195+
196+ logger .info (f"Ringbuf map parameters: { map_params } " )
197+
198+ map_global = create_bpf_map (module , map_name , map_params )
199+ create_ringbuf_debug_info (module , map_global , map_name , map_params )
200+ return map_global
201+
202+
115203@MapProcessorRegistry .register ("HashMap" )
116204def process_hash_map (map_name , rval , module ):
117205 """Process a BPF_HASH map declaration"""
0 commit comments