Skip to content

Commit dc1b243

Browse files
correct error size calculation for arrays
1 parent 1b4272b commit dc1b243

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

pythonbpf/debuginfo/debug_info_generator.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ def create_array_type(self, base_type: Any, count: int) -> Any:
8181
},
8282
)
8383

84+
def create_array_type_vmlinux(self, type_info: Any, count: int) -> Any:
85+
"""Create an array type of the given base type with specified count"""
86+
base_type, type_sizing = type_info
87+
subrange = self.module.add_debug_info("DISubrange", {"count": count})
88+
return self.module.add_debug_info(
89+
"DICompositeType",
90+
{
91+
"tag": dc.DW_TAG_array_type,
92+
"baseType": base_type,
93+
"size": type_sizing,
94+
"elements": [subrange],
95+
},
96+
)
97+
8498
@staticmethod
8599
def _compute_array_size(base_type: Any, count: int) -> int:
86100
# Extract size from base_type if possible
@@ -101,7 +115,9 @@ def create_struct_member(self, name: str, base_type: Any, offset: int) -> Any:
101115
},
102116
)
103117

104-
def create_struct_member_vmlinux(self, name: str, base_type_with_size: Any, offset: int) -> Any:
118+
def create_struct_member_vmlinux(
119+
self, name: str, base_type_with_size: Any, offset: int
120+
) -> Any:
105121
"""Create a struct member with the given name, type, and offset"""
106122
base_type, type_size = base_type_with_size
107123
return self.module.add_debug_info(

pythonbpf/vmlinux_parser/ir_gen/debug_info_gen.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
from ..dependency_node import DependencyNode
33
import ctypes
44
import logging
5-
from typing import List, Any, Tuple, Optional
5+
from typing import List, Any, Tuple
66

77
logger = logging.getLogger(__name__)
88

9+
910
def debug_info_generation(
10-
struct: DependencyNode, llvm_module, generated_debug_info: List[Tuple[DependencyNode, Any]]
11+
struct: DependencyNode,
12+
llvm_module,
13+
generated_debug_info: List[Tuple[DependencyNode, Any]],
1114
) -> Any:
1215
"""
1316
Generate DWARF debug information for a struct defined in a DependencyNode.
@@ -54,11 +57,11 @@ def debug_info_generation(
5457

5558

5659
def _get_field_debug_type(
57-
field_name: str,
58-
field,
59-
generator: DebugInfoGenerator,
60-
parent_struct: DependencyNode,
61-
generated_debug_info: List[Tuple[DependencyNode, Any]]
60+
field_name: str,
61+
field,
62+
generator: DebugInfoGenerator,
63+
parent_struct: DependencyNode,
64+
generated_debug_info: List[Tuple[DependencyNode, Any]],
6265
) -> tuple[Any, int]:
6366
"""
6467
Determine the appropriate debug type for a field based on its Python/ctypes type.
@@ -77,8 +80,12 @@ def _get_field_debug_type(
7780
if field.ctype_complex_type is not None:
7881
if issubclass(field.ctype_complex_type, ctypes.Array):
7982
# Handle array types
80-
element_type, base_type_size = _get_basic_debug_type(field.containing_type, generator)
81-
return generator.create_array_type(element_type, field.type_size), field.type_size * base_type_size
83+
element_type, base_type_size = _get_basic_debug_type(
84+
field.containing_type, generator
85+
)
86+
return generator.create_array_type_vmlinux(
87+
(element_type, base_type_size * field.type_size), field.type_size
88+
), field.type_size * base_type_size
8289
elif issubclass(field.ctype_complex_type, ctypes._Pointer):
8390
# Handle pointer types
8491
pointee_type, _ = _get_basic_debug_type(field.containing_type, generator)
@@ -136,7 +143,9 @@ def _get_basic_debug_type(ctype, generator: DebugInfoGenerator) -> Any:
136143
elif ctype == ctypes.c_longlong or ctype == ctypes.c_int64:
137144
return generator.get_basic_type("long long", 64, dc.DW_ATE_signed), 64
138145
elif ctype == ctypes.c_ulonglong or ctype == ctypes.c_uint64:
139-
return generator.get_basic_type("unsigned long long", 64, dc.DW_ATE_unsigned), 64
146+
return generator.get_basic_type(
147+
"unsigned long long", 64, dc.DW_ATE_unsigned
148+
), 64
140149
elif ctype == ctypes.c_float:
141150
return generator.get_basic_type("float", 32, dc.DW_ATE_float), 32
142151
elif ctype == ctypes.c_double:
@@ -147,9 +156,7 @@ def _get_basic_debug_type(ctype, generator: DebugInfoGenerator) -> Any:
147156
char_type = generator.get_basic_type("char", 8, dc.DW_ATE_signed_char), 8
148157
return generator.create_pointer_type(char_type)
149158
elif ctype == ctypes.c_void_p:
150-
void_type = generator.module.add_debug_info(
151-
"DIBasicType", {"name": "void"}
152-
)
159+
void_type = generator.module.add_debug_info("DIBasicType", {"name": "void"})
153160
return generator.create_pointer_type(void_type), 64
154161
else:
155162
return generator.get_uint64_type(), 64

tests/passing_tests/vmlinux/xdp_pass.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir, compile
2-
from pythonbpf.maps import HashMap
3-
from pythonbpf.helper import XDP_PASS
1+
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile
42
from vmlinux import TASK_COMM_LEN # noqa: F401
5-
from vmlinux import struct_xdp_md
63
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
74
from ctypes import c_int64
85

6+
97
# Instructions to how to run this program
108
# 1. Install PythonBPF: pip install pythonbpf
119
# 2. Run the program: python examples/xdp_pass.py
1210
# 3. Run the program with sudo: sudo tools/check.sh run examples/xdp_pass.o
1311
# 4. Attach object file to any network device with something like ./check.sh xdp examples/xdp_pass.o tailscale0
1412
# 5. send traffic through the device and observe effects
15-
1613
@bpf
17-
@section("xdp")
18-
def hello_world(ctx: struct_xdp_md) -> c_int64:
19-
return XDP_PASS
14+
@section("tracepoint/syscalls/sys_enter_execve")
15+
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int64:
16+
print("Hello, World!")
17+
return c_int64(0)
2018

2119

2220
@bpf

0 commit comments

Comments
 (0)