mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
big overhaul of debug info and params passed to maps
This commit is contained in:
@ -14,7 +14,7 @@ from ctypes import c_void_p, c_int64
|
|||||||
@bpf
|
@bpf
|
||||||
@map
|
@map
|
||||||
def count() -> HashMap:
|
def count() -> HashMap:
|
||||||
return HashMap(key_type=c_int64, value_type=c_int64, max_entries=1)
|
return HashMap(key=c_int64, value=c_int64, max_entries=1)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
@ -13,7 +13,7 @@ from ctypes import c_void_p, c_int64, c_uint64
|
|||||||
@bpf
|
@bpf
|
||||||
@map
|
@map
|
||||||
def last() -> HashMap:
|
def last() -> HashMap:
|
||||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=3)
|
return HashMap(key=c_uint64, value=c_uint64, max_entries=3)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
@ -13,7 +13,7 @@ from ctypes import c_void_p, c_int64, c_uint64
|
|||||||
@bpf
|
@bpf
|
||||||
@map
|
@map
|
||||||
def last() -> HashMap:
|
def last() -> HashMap:
|
||||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=3)
|
return HashMap(key=c_uint64, value=c_uint64, max_entries=3)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
@ -7,7 +7,7 @@ from pythonbpf.maps import HashMap
|
|||||||
@bpf
|
@bpf
|
||||||
@map
|
@map
|
||||||
def last() -> HashMap:
|
def last() -> HashMap:
|
||||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
|
return HashMap(key=c_uint64, value=c_uint64, max_entries=1)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
@ -8,7 +8,7 @@ from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
|||||||
@bpf
|
@bpf
|
||||||
@map
|
@map
|
||||||
def last() -> HashMap:
|
def last() -> HashMap:
|
||||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=3)
|
return HashMap(key=c_uint64, value=c_uint64, max_entries=3)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@ -25,23 +25,23 @@ def hello_again(ctx: c_void_p) -> c_int64:
|
|||||||
print("exited")
|
print("exited")
|
||||||
key = 0
|
key = 0
|
||||||
delta = 0
|
delta = 0
|
||||||
|
dddelta = 0
|
||||||
tsp = last().lookup(key)
|
tsp = last().lookup(key)
|
||||||
if True:
|
if True:
|
||||||
delta = ktime()
|
delta = ktime()
|
||||||
ddelta = deref(delta)
|
ddelta = deref(delta)
|
||||||
if ddelta < 1000000000:
|
ttsp = deref(deref(tsp))
|
||||||
|
dddelta = ddelta - ttsp
|
||||||
|
if dddelta < 1000000000:
|
||||||
print("execve called within last second")
|
print("execve called within last second")
|
||||||
last().delete(key)
|
last().delete(key)
|
||||||
ts = ktime()
|
ts = ktime()
|
||||||
last().update(key, ts)
|
last().update(key, ts)
|
||||||
|
|
||||||
va = 8
|
va = 8
|
||||||
nm = 5 ^ va
|
nm = 5 + va
|
||||||
al = 6 & 3
|
al = 6 & 3
|
||||||
ru = (nm + al) + al
|
print(f"this is a variable {nm}")
|
||||||
print(f"this is a variable {ru}")
|
|
||||||
# st = "st"
|
|
||||||
# last().update(key, ts)
|
|
||||||
|
|
||||||
return c_int64(0)
|
return c_int64(0)
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,7 @@ from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
|||||||
@bpf
|
@bpf
|
||||||
@map
|
@map
|
||||||
def last() -> HashMap:
|
def last() -> HashMap:
|
||||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=3)
|
return HashMap(key=c_uint64, value=c_uint64, max_entries=3)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
@ -7,7 +7,7 @@ from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
|||||||
@bpf
|
@bpf
|
||||||
@map
|
@map
|
||||||
def events() -> PerfEventArray:
|
def events() -> PerfEventArray:
|
||||||
return PerfEventArray(key_type=c_uint64, value_type=c_uint64)
|
return PerfEventArray(key_size=c_int32, value_size=c_int32)
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@section("tracepoint/syscalls/sys_enter_clone")
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import subprocess
|
|||||||
import inspect
|
import inspect
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def find_bpf_chunks(tree):
|
def find_bpf_chunks(tree):
|
||||||
"""Find all functions decorated with @bpf in the AST."""
|
"""Find all functions decorated with @bpf in the AST."""
|
||||||
bpf_functions = []
|
bpf_functions = []
|
||||||
@ -49,7 +50,7 @@ def compile_to_ir(filename: str, output: str):
|
|||||||
"filename": filename,
|
"filename": filename,
|
||||||
"directory": os.path.dirname(filename)
|
"directory": os.path.dirname(filename)
|
||||||
})
|
})
|
||||||
|
|
||||||
module._debug_compile_unit = module.add_debug_info("DICompileUnit", { # type: ignore
|
module._debug_compile_unit = module.add_debug_info("DICompileUnit", { # type: ignore
|
||||||
"language": 29, # DW_LANG_C11
|
"language": 29, # DW_LANG_C11
|
||||||
"file": module._file_metadata, # type: ignore
|
"file": module._file_metadata, # type: ignore
|
||||||
@ -61,7 +62,8 @@ def compile_to_ir(filename: str, output: str):
|
|||||||
"nameTableKind": 0
|
"nameTableKind": 0
|
||||||
}, is_distinct=True)
|
}, is_distinct=True)
|
||||||
|
|
||||||
module.add_named_metadata("llvm.dbg.cu", module._debug_compile_unit) # type: ignore
|
module.add_named_metadata(
|
||||||
|
"llvm.dbg.cu", module._debug_compile_unit) # type: ignore
|
||||||
|
|
||||||
processor(source, filename, module)
|
processor(source, filename, module)
|
||||||
|
|
||||||
@ -95,6 +97,7 @@ def compile_to_ir(filename: str, output: str):
|
|||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def compile():
|
def compile():
|
||||||
# Look one level up the stack to the caller of this function
|
# Look one level up the stack to the caller of this function
|
||||||
caller_frame = inspect.stack()[1]
|
caller_frame = inspect.stack()[1]
|
||||||
@ -110,4 +113,4 @@ def compile():
|
|||||||
str(ll_file), "-o", str(o_file)
|
str(ll_file), "-o", str(o_file)
|
||||||
], check=True)
|
], check=True)
|
||||||
|
|
||||||
print(f"Object written to {o_file}")
|
print(f"Object written to {o_file}, {ll_file} can be removed")
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
class HashMap:
|
class HashMap:
|
||||||
def __init__(self, key_type, value_type, max_entries):
|
def __init__(self, key, value, max_entries):
|
||||||
self.key_type = key_type
|
self.key = key
|
||||||
self.value_type = value_type
|
self.value = value
|
||||||
self.max_entries = max_entries
|
self.max_entries = max_entries
|
||||||
self.entries = {}
|
self.entries = {}
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ class HashMap:
|
|||||||
|
|
||||||
|
|
||||||
class PerfEventArray:
|
class PerfEventArray:
|
||||||
def __init__(self, key_type, value_type):
|
def __init__(self, key_size, value_size):
|
||||||
self.key_type = key_type
|
self.key_type = key_size
|
||||||
self.value_type = value_type
|
self.value_type = value_size
|
||||||
self.entries = {}
|
self.entries = {}
|
||||||
|
|||||||
@ -30,16 +30,12 @@ BPF_MAP_MAPPINGS = {
|
|||||||
def create_bpf_map(module, map_name, map_params):
|
def create_bpf_map(module, map_name, map_params):
|
||||||
"""Create a BPF map in the module with the given parameters and debug info"""
|
"""Create a BPF map in the module with the given parameters and debug info"""
|
||||||
|
|
||||||
map_type_str = map_params.get("map_type", "HASH")
|
map_type_str = map_params.get("type", "HASH")
|
||||||
map_type = BPF_MAP_MAPPINGS.get(map_type_str)
|
map_type = BPF_MAP_MAPPINGS.get(map_type_str)
|
||||||
|
|
||||||
# Create the anonymous struct type for BPF map
|
# Create the anonymous struct type for BPF map
|
||||||
map_struct_type = ir.LiteralStructType([
|
map_struct_type = ir.LiteralStructType(
|
||||||
ir.PointerType(),
|
[ir.PointerType() for _ in range(len(map_params))])
|
||||||
ir.PointerType(),
|
|
||||||
ir.PointerType(),
|
|
||||||
ir.PointerType()
|
|
||||||
])
|
|
||||||
|
|
||||||
# Create the global variable
|
# Create the global variable
|
||||||
map_global = ir.GlobalVariable(module, map_struct_type, name=map_name)
|
map_global = ir.GlobalVariable(module, map_struct_type, name=map_name)
|
||||||
@ -79,7 +75,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)
|
# Create array type for map type field (array of 1 unsigned int)
|
||||||
array_subrange = module.add_debug_info(
|
array_subrange = module.add_debug_info(
|
||||||
"DISubrange", {"count": BPF_MAP_MAPPINGS[map_params.get("map_type", "HASH")]})
|
"DISubrange", {"count": BPF_MAP_MAPPINGS[map_params.get("type", "HASH")]})
|
||||||
array_type = module.add_debug_info("DICompositeType", {
|
array_type = module.add_debug_info("DICompositeType", {
|
||||||
"tag": dc.DW_TAG_array_type,
|
"tag": dc.DW_TAG_array_type,
|
||||||
"baseType": uint_type,
|
"baseType": uint_type,
|
||||||
@ -96,13 +92,15 @@ def create_map_debug_info(module, map_global, map_name, map_params):
|
|||||||
|
|
||||||
key_ptr = module.add_debug_info("DIDerivedType", {
|
key_ptr = module.add_debug_info("DIDerivedType", {
|
||||||
"tag": dc.DW_TAG_pointer_type,
|
"tag": dc.DW_TAG_pointer_type,
|
||||||
"baseType": uint_type, # Adjust based on actual key type
|
# Adjust based on actual key type
|
||||||
|
"baseType": array_type if "key_size" in map_params else uint_type,
|
||||||
"size": 64
|
"size": 64
|
||||||
})
|
})
|
||||||
|
|
||||||
value_ptr = module.add_debug_info("DIDerivedType", {
|
value_ptr = module.add_debug_info("DIDerivedType", {
|
||||||
"tag": dc.DW_TAG_pointer_type,
|
"tag": dc.DW_TAG_pointer_type,
|
||||||
"baseType": ulong_type, # Adjust based on actual value type
|
# Adjust based on actual value type
|
||||||
|
"baseType": array_type if "value_size" in map_params else ulong_type,
|
||||||
"size": 64
|
"size": 64
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -110,15 +108,26 @@ def create_map_debug_info(module, map_global, map_name, map_params):
|
|||||||
|
|
||||||
# Create struct members
|
# Create struct members
|
||||||
# scope field does not appear for some reason
|
# scope field does not appear for some reason
|
||||||
type_member = module.add_debug_info("DIDerivedType", {
|
cnt = 0
|
||||||
"tag": dc.DW_TAG_member,
|
for elem in map_params:
|
||||||
"name": "type",
|
if elem == "max_entries":
|
||||||
"file": file_metadata,
|
continue
|
||||||
"baseType": type_ptr,
|
if elem == "type":
|
||||||
"size": 64,
|
ptr = type_ptr
|
||||||
"offset": 0
|
elif "key" in elem:
|
||||||
})
|
ptr = key_ptr
|
||||||
elements_arr.append(type_member)
|
else:
|
||||||
|
ptr = value_ptr
|
||||||
|
member = module.add_debug_info("DIDerivedType", {
|
||||||
|
"tag": dc.DW_TAG_member,
|
||||||
|
"name": elem,
|
||||||
|
"file": file_metadata,
|
||||||
|
"baseType": ptr,
|
||||||
|
"size": 64,
|
||||||
|
"offset": cnt * 64
|
||||||
|
})
|
||||||
|
elements_arr.append(member)
|
||||||
|
cnt += 1
|
||||||
|
|
||||||
if "max_entries" in map_params:
|
if "max_entries" in map_params:
|
||||||
array_subrange_max_entries = module.add_debug_info(
|
array_subrange_max_entries = module.add_debug_info(
|
||||||
@ -140,30 +149,10 @@ def create_map_debug_info(module, map_global, map_name, map_params):
|
|||||||
"file": file_metadata,
|
"file": file_metadata,
|
||||||
"baseType": max_entries_ptr,
|
"baseType": max_entries_ptr,
|
||||||
"size": 64,
|
"size": 64,
|
||||||
"offset": 64
|
"offset": cnt * 64
|
||||||
})
|
})
|
||||||
elements_arr.append(max_entries_member)
|
elements_arr.append(max_entries_member)
|
||||||
|
|
||||||
key_member = module.add_debug_info("DIDerivedType", {
|
|
||||||
"tag": dc.DW_TAG_member,
|
|
||||||
"name": "key",
|
|
||||||
"file": file_metadata,
|
|
||||||
"baseType": key_ptr,
|
|
||||||
"size": 64,
|
|
||||||
"offset": 128
|
|
||||||
})
|
|
||||||
elements_arr.append(key_member)
|
|
||||||
|
|
||||||
value_member = module.add_debug_info("DIDerivedType", {
|
|
||||||
"tag": dc.DW_TAG_member,
|
|
||||||
"name": "value",
|
|
||||||
"file": file_metadata,
|
|
||||||
"baseType": value_ptr,
|
|
||||||
"size": 64,
|
|
||||||
"offset": 192
|
|
||||||
})
|
|
||||||
elements_arr.append(value_member)
|
|
||||||
|
|
||||||
# Create the struct type
|
# Create the struct type
|
||||||
struct_type = module.add_debug_info("DICompositeType", {
|
struct_type = module.add_debug_info("DICompositeType", {
|
||||||
"tag": dc.DW_TAG_structure_type,
|
"tag": dc.DW_TAG_structure_type,
|
||||||
@ -196,23 +185,23 @@ def create_map_debug_info(module, map_global, map_name, map_params):
|
|||||||
|
|
||||||
def process_hash_map(map_name, rval, module):
|
def process_hash_map(map_name, rval, module):
|
||||||
print(f"Creating HashMap map: {map_name}")
|
print(f"Creating HashMap map: {map_name}")
|
||||||
map_params: dict[str, object] = {"map_type": "HASH"}
|
map_params: dict[str, object] = {"type": "HASH"}
|
||||||
|
|
||||||
# Assuming order: key_type, value_type, max_entries
|
# Assuming order: key_type, value_type, max_entries
|
||||||
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):
|
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):
|
||||||
map_params["key_type"] = rval.args[0].id
|
map_params["key"] = rval.args[0].id
|
||||||
if len(rval.args) >= 2 and isinstance(rval.args[1], ast.Name):
|
if len(rval.args) >= 2 and isinstance(rval.args[1], ast.Name):
|
||||||
map_params["value_type"] = rval.args[1].id
|
map_params["value"] = rval.args[1].id
|
||||||
if len(rval.args) >= 3 and isinstance(rval.args[2], ast.Constant):
|
if len(rval.args) >= 3 and isinstance(rval.args[2], ast.Constant):
|
||||||
const_val = rval.args[2].value
|
const_val = rval.args[2].value
|
||||||
if isinstance(const_val, (int, str)): # safe check
|
if isinstance(const_val, (int, str)): # safe check
|
||||||
map_params["max_entries"] = const_val
|
map_params["max_entries"] = const_val
|
||||||
|
|
||||||
for keyword in rval.keywords:
|
for keyword in rval.keywords:
|
||||||
if keyword.arg == "key_type" and isinstance(keyword.value, ast.Name):
|
if keyword.arg == "key" and isinstance(keyword.value, ast.Name):
|
||||||
map_params["key_type"] = keyword.value.id
|
map_params["key"] = keyword.value.id
|
||||||
elif keyword.arg == "value_type" and isinstance(keyword.value, ast.Name):
|
elif keyword.arg == "value" and isinstance(keyword.value, ast.Name):
|
||||||
map_params["value_type"] = keyword.value.id
|
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
|
const_val = keyword.value.value
|
||||||
if isinstance(const_val, (int, str)):
|
if isinstance(const_val, (int, str)):
|
||||||
@ -224,18 +213,18 @@ def process_hash_map(map_name, rval, module):
|
|||||||
|
|
||||||
def process_perf_event_map(map_name, rval, module):
|
def process_perf_event_map(map_name, rval, module):
|
||||||
print(f"Creating PerfEventArray map: {map_name}")
|
print(f"Creating PerfEventArray map: {map_name}")
|
||||||
map_params = {"map_type": "PERF_EVENT_ARRAY"}
|
map_params = {"type": "PERF_EVENT_ARRAY"}
|
||||||
|
|
||||||
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):
|
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):
|
||||||
map_params["key_type"] = rval.args[0].id
|
map_params["key_size"] = rval.args[0].id
|
||||||
if len(rval.args) >= 2 and isinstance(rval.args[1], ast.Name):
|
if len(rval.args) >= 2 and isinstance(rval.args[1], ast.Name):
|
||||||
map_params["value_type"] = rval.args[1].id
|
map_params["value_size"] = rval.args[1].id
|
||||||
|
|
||||||
for keyword in rval.keywords:
|
for keyword in rval.keywords:
|
||||||
if keyword.arg == "key_type" and isinstance(keyword.value, ast.Name):
|
if keyword.arg == "key_size" and isinstance(keyword.value, ast.Name):
|
||||||
map_params["key_type"] = keyword.value.id
|
map_params["key_size"] = keyword.value.id
|
||||||
elif keyword.arg == "value_type" and isinstance(keyword.value, ast.Name):
|
elif keyword.arg == "value_size" and isinstance(keyword.value, ast.Name):
|
||||||
map_params["value_type"] = keyword.value.id
|
map_params["value_size"] = keyword.value.id
|
||||||
|
|
||||||
print(f"Map parameters: {map_params}")
|
print(f"Map parameters: {map_params}")
|
||||||
return create_bpf_map(module, map_name, map_params)
|
return create_bpf_map(module, map_name, map_params)
|
||||||
|
|||||||
Reference in New Issue
Block a user