From 0d691865bc088b40a7cf5d97db19c3bddb18bf97 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 11:42:14 +0530 Subject: [PATCH 01/11] Add is_map in maps_pass --- pythonbpf/maps_pass.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pythonbpf/maps_pass.py b/pythonbpf/maps_pass.py index 38deecb..0553996 100644 --- a/pythonbpf/maps_pass.py +++ b/pythonbpf/maps_pass.py @@ -8,19 +8,20 @@ map_sym_tab = {} def maps_proc(tree, module, chunks): for func_node in chunks: - # Check if this function is a map - is_map = False - for decorator in func_node.decorator_list: - if isinstance(decorator, ast.Name) and decorator.id == "map": - is_map = True - break - if is_map: + if is_map(func_node): print(f"Found BPF map: {func_node.name}") process_bpf_map(func_node, module) continue return map_sym_tab +def is_map(func_node): + return any( + isinstance(decorator, ast.Name) and decorator.id == "map" + for decorator in func_node.decorator_list + ) + + BPF_MAP_MAPPINGS = { "HASH": 1, # BPF_MAP_TYPE_HASH "PERF_EVENT_ARRAY": 4, # BPF_MAP_TYPE_PERF_EVENT_ARRAY From 1a1f2cf634b357fe330591419230ec470b1bc9ad Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 12:20:07 +0530 Subject: [PATCH 02/11] create BPFMapType enum --- pythonbpf/maps_pass.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pythonbpf/maps_pass.py b/pythonbpf/maps_pass.py index 0553996..7484aea 100644 --- a/pythonbpf/maps_pass.py +++ b/pythonbpf/maps_pass.py @@ -2,6 +2,7 @@ import ast from llvmlite import ir from .type_deducer import ctypes_to_ir from . import dwarf_constants as dc +from enum import Enum map_sym_tab = {} @@ -22,6 +23,11 @@ def is_map(func_node): ) +class BPFMapType(Enum): + HASH = 1 + PERF_EVENT_ARRAY = 4 + + BPF_MAP_MAPPINGS = { "HASH": 1, # BPF_MAP_TYPE_HASH "PERF_EVENT_ARRAY": 4, # BPF_MAP_TYPE_PERF_EVENT_ARRAY @@ -186,7 +192,7 @@ def create_map_debug_info(module, map_global, map_name, map_params): def process_hash_map(map_name, rval, module): print(f"Creating HashMap map: {map_name}") - map_params: dict[str, object] = {"type": "HASH"} + map_params: = {"type": BPFMapType.HASH} # Assuming order: key_type, value_type, max_entries if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name): @@ -214,7 +220,7 @@ def process_hash_map(map_name, rval, module): def process_perf_event_map(map_name, rval, module): print(f"Creating PerfEventArray map: {map_name}") - map_params = {"type": "PERF_EVENT_ARRAY"} + map_params = {"type": BPFMapType.PERF_EVENT_ARRAY} if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name): map_params["key_size"] = rval.args[0].id From 9ae1b6ce15f63d033b18eefb4d0a857814673b68 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 13:28:18 +0530 Subject: [PATCH 03/11] Fix usage of map_params to use BPFMapType --- pythonbpf/maps_pass.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pythonbpf/maps_pass.py b/pythonbpf/maps_pass.py index 7484aea..eeb5338 100644 --- a/pythonbpf/maps_pass.py +++ b/pythonbpf/maps_pass.py @@ -37,8 +37,7 @@ BPF_MAP_MAPPINGS = { def create_bpf_map(module, map_name, map_params): """Create a BPF map in the module with the given parameters and debug info""" - map_type_str = map_params.get("type", "HASH") - map_type = BPF_MAP_MAPPINGS.get(map_type_str) + map_type = map_params.get("type", BPFMapType.HASH).value # Create the anonymous struct type for BPF map map_struct_type = ir.LiteralStructType( @@ -82,7 +81,7 @@ def create_map_debug_info(module, map_global, map_name, map_params): # Create array type for map type field (array of 1 unsigned int) array_subrange = module.add_debug_info( - "DISubrange", {"count": BPF_MAP_MAPPINGS[map_params.get("type", "HASH")]}) + "DISubrange", {"count": map_params.get("type", BPFMapType.HASH).value}) array_type = module.add_debug_info("DICompositeType", { "tag": dc.DW_TAG_array_type, "baseType": uint_type, From cbc6b93cd82b1cb4df929504ea945183302f41bf Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:01:46 +0530 Subject: [PATCH 04/11] restructure maps dir, fix imports --- examples/c-form/ex8.bpf.c | 47 +++++++++++++++++++++++++++++++ examples/execve2.py | 2 +- examples/execve3.py | 2 +- pythonbpf/codegen.py | 6 ++-- pythonbpf/{ => maps}/maps.py | 0 pythonbpf/{ => maps}/maps_pass.py | 12 ++------ 6 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 examples/c-form/ex8.bpf.c rename pythonbpf/{ => maps}/maps.py (100%) rename pythonbpf/{ => maps}/maps_pass.py (97%) diff --git a/examples/c-form/ex8.bpf.c b/examples/c-form/ex8.bpf.c new file mode 100644 index 0000000..4163abd --- /dev/null +++ b/examples/c-form/ex8.bpf.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#define __TARGET_ARCH_aarch64 +#define u64 unsigned long long + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 10240); + __type(key, struct request *); + __type(value, u64); +} start SEC(".maps"); + +SEC("kprobe/blk_start_request") +int BPF_KPROBE(trace_start_req, struct request *req) +{ + u64 ts = bpf_ktime_get_ns(); + bpf_map_update_elem(&start, &req, &ts, BPF_ANY); + return 0; +} + +SEC("kprobe/blk_mq_start_request") +int BPF_KPROBE(trace_start_mq, struct request *req) +{ + u64 ts = bpf_ktime_get_ns(); + bpf_map_update_elem(&start, &req, &ts, BPF_ANY); + return 0; +} + +SEC("kprobe/blk_account_io_completion") +int BPF_KPROBE(trace_completion, struct request *req) +{ + u64 *tsp, delta; + + tsp = bpf_map_lookup_elem(&start, &req); + if (tsp) { + delta = bpf_ktime_get_ns() - *tsp; + bpf_printk("%d %x %d\n", req->__data_len, + req->cmd_flags, delta / 1000); + bpf_map_delete_elem(&start, &req); + } + return 0; +} + +char LICENSE[] SEC("license") = "GPL"; diff --git a/examples/execve2.py b/examples/execve2.py index fadc396..a5272af 100644 --- a/examples/execve2.py +++ b/examples/execve2.py @@ -1,7 +1,7 @@ from pythonbpf.decorators import bpf, map, section, bpfglobal from ctypes import c_void_p, c_int64, c_int32, c_uint64 from pythonbpf.helpers import ktime -from pythonbpf.maps import HashMap +from pythonbpf.maps.maps import HashMap @bpf diff --git a/examples/execve3.py b/examples/execve3.py index f636a34..03e0b35 100644 --- a/examples/execve3.py +++ b/examples/execve3.py @@ -1,6 +1,6 @@ from pythonbpf import bpf, map, section, bpfglobal, compile from pythonbpf.helpers import ktime, deref -from pythonbpf.maps import HashMap +from pythonbpf.maps.maps import HashMap from ctypes import c_void_p, c_int64, c_int32, c_uint64 diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index 7eaa713..c3c1ae4 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -2,7 +2,7 @@ import ast from llvmlite import ir from .license_pass import license_processing from .functions_pass import func_proc -from .maps_pass import maps_proc +from pythonbpf.maps.maps_pass import maps_proc from .structs.structs_pass import structs_proc from .globals_pass import globals_processing import os @@ -125,8 +125,8 @@ def BPF() -> BpfProgram: caller_frame = inspect.stack()[1] src = inspect.getsource(caller_frame.frame) with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".py") as f, \ - tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter, \ - tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file: + tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter, \ + tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file: f.write(src) f.flush() source = f.name diff --git a/pythonbpf/maps.py b/pythonbpf/maps/maps.py similarity index 100% rename from pythonbpf/maps.py rename to pythonbpf/maps/maps.py diff --git a/pythonbpf/maps_pass.py b/pythonbpf/maps/maps_pass.py similarity index 97% rename from pythonbpf/maps_pass.py rename to pythonbpf/maps/maps_pass.py index eeb5338..cec0772 100644 --- a/pythonbpf/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -1,7 +1,7 @@ import ast from llvmlite import ir -from .type_deducer import ctypes_to_ir -from . import dwarf_constants as dc +from pythonbpf.type_deducer import ctypes_to_ir +from pythonbpf import dwarf_constants as dc from enum import Enum map_sym_tab = {} @@ -28,12 +28,6 @@ class BPFMapType(Enum): PERF_EVENT_ARRAY = 4 -BPF_MAP_MAPPINGS = { - "HASH": 1, # BPF_MAP_TYPE_HASH - "PERF_EVENT_ARRAY": 4, # BPF_MAP_TYPE_PERF_EVENT_ARRAY -} - - def create_bpf_map(module, map_name, map_params): """Create a BPF map in the module with the given parameters and debug info""" @@ -191,7 +185,7 @@ def create_map_debug_info(module, map_global, map_name, map_params): def process_hash_map(map_name, rval, module): print(f"Creating HashMap map: {map_name}") - map_params: = {"type": BPFMapType.HASH} + map_params = {"type": BPFMapType.HASH} # Assuming order: key_type, value_type, max_entries if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name): From 2e005f6eb5b0c6066af8cb3723e7c580a74c207c Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:08:56 +0530 Subject: [PATCH 05/11] Create MapProcessorRegistry to add more maps --- pythonbpf/maps/maps_pass.py | 11 +++++------ pythonbpf/maps/maps_utils.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 pythonbpf/maps/maps_utils.py diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index cec0772..10631b6 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -3,6 +3,7 @@ from llvmlite import ir from pythonbpf.type_deducer import ctypes_to_ir from pythonbpf import dwarf_constants as dc from enum import Enum +from .maps_utils import MapProcessorRegistry map_sym_tab = {} @@ -183,6 +184,7 @@ def create_map_debug_info(module, map_global, map_name, map_params): return global_var_expr +@MapProcessorRegistry.register("HashMap") def process_hash_map(map_name, rval, module): print(f"Creating HashMap map: {map_name}") map_params = {"type": BPFMapType.HASH} @@ -211,6 +213,7 @@ def process_hash_map(map_name, rval, module): return create_bpf_map(module, map_name, map_params) +@MapProcessorRegistry.register("PerfEventArray") def process_perf_event_map(map_name, rval, module): print(f"Creating PerfEventArray map: {map_name}") map_params = {"type": BPFMapType.PERF_EVENT_ARRAY} @@ -235,10 +238,6 @@ def process_bpf_map(func_node, module): map_name = func_node.name print(f"Processing BPF map: {map_name}") - BPF_MAP_TYPES = {"HashMap": process_hash_map, # BPF_MAP_TYPE_HASH - "PerfEventArray": process_perf_event_map, # BPF_MAP_TYPE_PERF_EVENT_ARRAY - } - # For now, assume single return statement return_stmt = None for stmt in func_node.body: @@ -252,8 +251,8 @@ def process_bpf_map(func_node, module): # Handle only HashMap maps if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name): - if rval.func.id in BPF_MAP_TYPES: - handler = BPF_MAP_TYPES[rval.func.id] + handler = MapProcessorRegistry.get(rval.func.id) + if handler: handler(map_name, rval, module) else: print(f"Unknown map type {rval.func.id}, defaulting to HashMap") diff --git a/pythonbpf/maps/maps_utils.py b/pythonbpf/maps/maps_utils.py new file mode 100644 index 0000000..e46330f --- /dev/null +++ b/pythonbpf/maps/maps_utils.py @@ -0,0 +1,16 @@ +class MapProcessorRegistry: + """Registry for map processor functions""" + _processors = {} + + @classmethod + def register(cls, map_type_name): + """Decorator to register a processor function for a map type""" + def decorator(func): + cls._processors[map_type_name] = func + return func + return decorator + + @classmethod + def get_processor(cls, map_type_name): + """Get the processor function for a map type""" + return cls._processors.get(map_type_name) From ca51b7ce01b9d9f8168ca57df2f6f61a4eaccb60 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:10:25 +0530 Subject: [PATCH 06/11] Fix invalid member func for MapProcessorRegistry --- pythonbpf/maps/maps_pass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 10631b6..3540c72 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -251,7 +251,7 @@ def process_bpf_map(func_node, module): # Handle only HashMap maps if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name): - handler = MapProcessorRegistry.get(rval.func.id) + handler = MapProcessorRegistry.get_processor(rval.func.id) if handler: handler(map_name, rval, module) else: From 9fa362ec6a5a8a068ba7bba427b313ef552fbb52 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:29:45 +0530 Subject: [PATCH 07/11] Remove global map_sym_tab --- pythonbpf/maps/maps_pass.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 3540c72..18d14b3 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -1,19 +1,17 @@ import ast from llvmlite import ir -from pythonbpf.type_deducer import ctypes_to_ir from pythonbpf import dwarf_constants as dc from enum import Enum from .maps_utils import MapProcessorRegistry -map_sym_tab = {} - def maps_proc(tree, module, chunks): + """ Process all functions decorated with @map to find BPF maps """ + map_sym_tab = {} for func_node in chunks: if is_map(func_node): print(f"Found BPF map: {func_node.name}") - process_bpf_map(func_node, module) - continue + map_sym_tab[func_node.name] = process_bpf_map(func_node, module) return map_sym_tab @@ -30,9 +28,7 @@ class BPFMapType(Enum): def create_bpf_map(module, map_name, map_params): - """Create a BPF map in the module with the given parameters and debug info""" - - map_type = map_params.get("type", BPFMapType.HASH).value + """Create a BPF map in the module with given parameters and debug info""" # Create the anonymous struct type for BPF map map_struct_type = ir.LiteralStructType( @@ -43,15 +39,14 @@ def create_bpf_map(module, map_name, map_params): map_global.linkage = 'dso_local' map_global.global_constant = False map_global.initializer = ir.Constant( - map_struct_type, None) # type: ignore + map_struct_type, None) map_global.section = ".maps" - map_global.align = 8 # type: ignore + map_global.align = 8 # Generate debug info for BTF create_map_debug_info(module, map_global, map_name, map_params) print(f"Created BPF map: {map_name}") - map_sym_tab[map_name] = map_global return map_global @@ -186,6 +181,7 @@ def create_map_debug_info(module, map_global, map_name, map_params): @MapProcessorRegistry.register("HashMap") def process_hash_map(map_name, rval, module): + """Process a BPF_HASH map declaration""" print(f"Creating HashMap map: {map_name}") map_params = {"type": BPFMapType.HASH} @@ -204,7 +200,8 @@ def process_hash_map(map_name, rval, module): map_params["key"] = keyword.value.id elif keyword.arg == "value" and isinstance(keyword.value, ast.Name): map_params["value"] = keyword.value.id - elif keyword.arg == "max_entries" and isinstance(keyword.value, ast.Constant): + elif keyword.arg == "max_entries" and \ + isinstance(keyword.value, ast.Constant): const_val = keyword.value.value if isinstance(const_val, (int, str)): map_params["max_entries"] = const_val @@ -215,6 +212,7 @@ def process_hash_map(map_name, rval, module): @MapProcessorRegistry.register("PerfEventArray") def process_perf_event_map(map_name, rval, module): + """Process a BPF_PERF_EVENT_ARRAY map declaration""" print(f"Creating PerfEventArray map: {map_name}") map_params = {"type": BPFMapType.PERF_EVENT_ARRAY} @@ -226,7 +224,8 @@ def process_perf_event_map(map_name, rval, module): for keyword in rval.keywords: if keyword.arg == "key_size" and isinstance(keyword.value, ast.Name): map_params["key_size"] = keyword.value.id - elif keyword.arg == "value_size" and isinstance(keyword.value, ast.Name): + elif keyword.arg == "value_size" and \ + isinstance(keyword.value, ast.Name): map_params["value_size"] = keyword.value.id print(f"Map parameters: {map_params}") @@ -249,11 +248,10 @@ def process_bpf_map(func_node, module): rval = return_stmt.value - # Handle only HashMap maps if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name): handler = MapProcessorRegistry.get_processor(rval.func.id) if handler: - handler(map_name, rval, module) + return handler(map_name, rval, module) else: print(f"Unknown map type {rval.func.id}, defaulting to HashMap") process_hash_map(map_name, rval, module) From 744aa3fbdfa235665d401a31769c5444f28da4c9 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:44:29 +0530 Subject: [PATCH 08/11] Use logger instead of prints in map_pass --- pythonbpf/maps/maps_pass.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 18d14b3..1bfa020 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -3,6 +3,9 @@ from llvmlite import ir from pythonbpf import dwarf_constants as dc from enum import Enum from .maps_utils import MapProcessorRegistry +import logging + +logger = logging.getLogger(__name__) def maps_proc(tree, module, chunks): @@ -46,7 +49,7 @@ def create_bpf_map(module, map_name, map_params): # Generate debug info for BTF create_map_debug_info(module, map_global, map_name, map_params) - print(f"Created BPF map: {map_name}") + logger.info(f"Created BPF map: {map_name} with params {map_params}") return map_global @@ -182,7 +185,7 @@ def create_map_debug_info(module, map_global, map_name, map_params): @MapProcessorRegistry.register("HashMap") def process_hash_map(map_name, rval, module): """Process a BPF_HASH map declaration""" - print(f"Creating HashMap map: {map_name}") + logger.info(f"Processing HashMap: {map_name}") map_params = {"type": BPFMapType.HASH} # Assuming order: key_type, value_type, max_entries @@ -206,14 +209,14 @@ def process_hash_map(map_name, rval, module): if isinstance(const_val, (int, str)): map_params["max_entries"] = const_val - print(f"Map parameters: {map_params}") + logger.info(f"Map parameters: {map_params}") return create_bpf_map(module, map_name, map_params) @MapProcessorRegistry.register("PerfEventArray") def process_perf_event_map(map_name, rval, module): """Process a BPF_PERF_EVENT_ARRAY map declaration""" - print(f"Creating PerfEventArray map: {map_name}") + logger.info(f"Processing PerfEventArray: {map_name}") map_params = {"type": BPFMapType.PERF_EVENT_ARRAY} if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name): @@ -228,14 +231,14 @@ def process_perf_event_map(map_name, rval, module): isinstance(keyword.value, ast.Name): map_params["value_size"] = keyword.value.id - print(f"Map parameters: {map_params}") + logger.info(f"Map parameters: {map_params}") return create_bpf_map(module, map_name, map_params) def process_bpf_map(func_node, module): """Process a BPF map (a function decorated with @map)""" map_name = func_node.name - print(f"Processing BPF map: {map_name}") + logger.info(f"Processing BPF map: {map_name}") # For now, assume single return statement return_stmt = None @@ -253,7 +256,8 @@ def process_bpf_map(func_node, module): if handler: return handler(map_name, rval, module) else: - print(f"Unknown map type {rval.func.id}, defaulting to HashMap") + logger.warning(f"Unknown map type { + rval.func.id}, defaulting to HashMap") process_hash_map(map_name, rval, module) else: raise ValueError("Function under @map must return a map") From d943b78a254673f3ce81fe3969a1ed1184f2250a Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:51:28 +0530 Subject: [PATCH 09/11] Add __init__ to maps to improve imports --- examples/execve2.py | 2 +- examples/execve3.py | 2 +- pythonbpf/codegen.py | 2 +- pythonbpf/maps/__init__.py | 2 ++ pythonbpf/maps/maps_pass.py | 4 ++-- 5 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 pythonbpf/maps/__init__.py diff --git a/examples/execve2.py b/examples/execve2.py index a5272af..fadc396 100644 --- a/examples/execve2.py +++ b/examples/execve2.py @@ -1,7 +1,7 @@ from pythonbpf.decorators import bpf, map, section, bpfglobal from ctypes import c_void_p, c_int64, c_int32, c_uint64 from pythonbpf.helpers import ktime -from pythonbpf.maps.maps import HashMap +from pythonbpf.maps import HashMap @bpf diff --git a/examples/execve3.py b/examples/execve3.py index 03e0b35..f636a34 100644 --- a/examples/execve3.py +++ b/examples/execve3.py @@ -1,6 +1,6 @@ from pythonbpf import bpf, map, section, bpfglobal, compile from pythonbpf.helpers import ktime, deref -from pythonbpf.maps.maps import HashMap +from pythonbpf.maps import HashMap from ctypes import c_void_p, c_int64, c_int32, c_uint64 diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index c3c1ae4..b749a46 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -2,7 +2,7 @@ import ast from llvmlite import ir from .license_pass import license_processing from .functions_pass import func_proc -from pythonbpf.maps.maps_pass import maps_proc +from pythonbpf.maps import maps_proc from .structs.structs_pass import structs_proc from .globals_pass import globals_processing import os diff --git a/pythonbpf/maps/__init__.py b/pythonbpf/maps/__init__.py new file mode 100644 index 0000000..7893e14 --- /dev/null +++ b/pythonbpf/maps/__init__.py @@ -0,0 +1,2 @@ +from .maps import HashMap, PerfEventArray +from .maps_pass import maps_proc diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 1bfa020..332a065 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -256,8 +256,8 @@ def process_bpf_map(func_node, module): if handler: return handler(map_name, rval, module) else: - logger.warning(f"Unknown map type { - rval.func.id}, defaulting to HashMap") + logger.warning(f"Unknown map type " + f"{rval.func.id}, defaulting to HashMap") process_hash_map(map_name, rval, module) else: raise ValueError("Function under @map must return a map") From 0f3cc434a3c84cf09581dd8a6a09929bc5a0e2c8 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:55:37 +0530 Subject: [PATCH 10/11] Fix return for unknown map types --- pythonbpf/maps/maps_pass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 332a065..f833bf3 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -258,6 +258,6 @@ def process_bpf_map(func_node, module): else: logger.warning(f"Unknown map type " f"{rval.func.id}, defaulting to HashMap") - process_hash_map(map_name, rval, module) + return process_hash_map(map_name, rval, module) else: raise ValueError("Function under @map must return a map") From 87908e871357ef00395b35e5b1e80ddf434ee882 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:57:58 +0530 Subject: [PATCH 11/11] Remove backslash for multiline conditions in maps --- pythonbpf/maps/maps_pass.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index f833bf3..a20a874 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -203,8 +203,8 @@ def process_hash_map(map_name, rval, module): map_params["key"] = keyword.value.id elif keyword.arg == "value" and isinstance(keyword.value, ast.Name): map_params["value"] = keyword.value.id - elif keyword.arg == "max_entries" and \ - isinstance(keyword.value, ast.Constant): + elif (keyword.arg == "max_entries" and + isinstance(keyword.value, ast.Constant)): const_val = keyword.value.value if isinstance(const_val, (int, str)): map_params["max_entries"] = const_val @@ -227,8 +227,8 @@ def process_perf_event_map(map_name, rval, module): for keyword in rval.keywords: if keyword.arg == "key_size" and isinstance(keyword.value, ast.Name): map_params["key_size"] = keyword.value.id - elif keyword.arg == "value_size" and \ - isinstance(keyword.value, ast.Name): + elif (keyword.arg == "value_size" and + isinstance(keyword.value, ast.Name)): map_params["value_size"] = keyword.value.id logger.info(f"Map parameters: {map_params}")