mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Add string as a primitve to struct defs
This commit is contained in:
@ -10,6 +10,7 @@ from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
|||||||
class data_t:
|
class data_t:
|
||||||
pid: c_uint64
|
pid: c_uint64
|
||||||
ts: c_uint64
|
ts: c_uint64
|
||||||
|
comm: str(16)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
@ -28,14 +28,25 @@ def process_bpf_struct(cls_node, module):
|
|||||||
|
|
||||||
for item in cls_node.body:
|
for item in cls_node.body:
|
||||||
if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name):
|
if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name):
|
||||||
|
print(f"Field: {item.target.id}, Type: "
|
||||||
|
f"{ast.dump(item.annotation)}")
|
||||||
field_names.append(item.target.id)
|
field_names.append(item.target.id)
|
||||||
field_types.append(ctypes_to_ir(item.annotation.id))
|
if isinstance(item.annotation, ast.Call) and isinstance(item.annotation.func, ast.Name) and item.annotation.func.id == "str":
|
||||||
|
# This is a char array with fixed length
|
||||||
|
# TODO: For now assuming str is always called with constant
|
||||||
|
field_types.append(ir.ArrayType(
|
||||||
|
ir.IntType(8), item.annotation.args[0].value))
|
||||||
|
else:
|
||||||
|
field_types.append(ctypes_to_ir(item.annotation.id))
|
||||||
|
|
||||||
curr_offset = 0
|
curr_offset = 0
|
||||||
for ftype in field_types:
|
for ftype in field_types:
|
||||||
if isinstance(ftype, ir.IntType):
|
if isinstance(ftype, ir.IntType):
|
||||||
fsize = ftype.width // 8
|
fsize = ftype.width // 8
|
||||||
alignment = fsize
|
alignment = fsize
|
||||||
|
elif isinstance(ftype, ir.ArrayType):
|
||||||
|
fsize = ftype.count * (ftype.element.width // 8)
|
||||||
|
alignment = ftype.element.width // 8
|
||||||
elif isinstance(ftype, ir.PointerType):
|
elif isinstance(ftype, ir.PointerType):
|
||||||
fsize = 8
|
fsize = 8
|
||||||
alignment = 8
|
alignment = 8
|
||||||
|
|||||||
Reference in New Issue
Block a user