Skip to content

Commit b9ddecd

Browse files
committed
Add string as a primitve to struct defs
1 parent 737c4d3 commit b9ddecd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

examples/execve5.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class data_t:
1111
pid: c_uint64
1212
ts: c_uint64
13+
comm: str(16)
1314

1415

1516
@bpf

pythonbpf/structs_pass.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,25 @@ def process_bpf_struct(cls_node, module):
2828

2929
for item in cls_node.body:
3030
if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name):
31+
print(f"Field: {item.target.id}, Type: "
32+
f"{ast.dump(item.annotation)}")
3133
field_names.append(item.target.id)
32-
field_types.append(ctypes_to_ir(item.annotation.id))
34+
if isinstance(item.annotation, ast.Call) and isinstance(item.annotation.func, ast.Name) and item.annotation.func.id == "str":
35+
# This is a char array with fixed length
36+
# TODO: For now assuming str is always called with constant
37+
field_types.append(ir.ArrayType(
38+
ir.IntType(8), item.annotation.args[0].value))
39+
else:
40+
field_types.append(ctypes_to_ir(item.annotation.id))
3341

3442
curr_offset = 0
3543
for ftype in field_types:
3644
if isinstance(ftype, ir.IntType):
3745
fsize = ftype.width // 8
3846
alignment = fsize
47+
elif isinstance(ftype, ir.ArrayType):
48+
fsize = ftype.count * (ftype.element.width // 8)
49+
alignment = ftype.element.width // 8
3950
elif isinstance(ftype, ir.PointerType):
4051
fsize = 8
4152
alignment = 8

0 commit comments

Comments
 (0)