Skip to content

Commit 9b7aa6d

Browse files
add dependency debug info list
1 parent 51a1be0 commit 9b7aa6d

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

pythonbpf/vmlinux_parser/ir_gen/debug_info_gen.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from pythonbpf.debuginfo import DebugInfoGenerator
2+
from ..dependency_node import DependencyNode
23

34

4-
def debug_info_generation(struct, llvm_module):
5+
def debug_info_generation(
6+
struct: DependencyNode, llvm_module, generated_debug_info: list
7+
):
58
generator = DebugInfoGenerator(llvm_module)
6-
# this is sample debug info generation
7-
# i64type = generator.get_uint64_type()
9+
print("DEBUG1", generated_debug_info)
10+
for field in struct.fields:
11+
print("DEBUG", field)
812

913
struct_type = generator.create_struct_type([], 64 * 4, is_distinct=True)
1014

pythonbpf/vmlinux_parser/ir_gen/ir_generation.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self, llvm_module, handler: DependencyHandler, assignment=None):
1414
self.llvm_module = llvm_module
1515
self.handler: DependencyHandler = handler
1616
self.generated: list[str] = []
17+
self.generated_debug_info: list = []
1718
if not handler.is_ready:
1819
raise ImportError(
1920
"Semantic analysis of vmlinux imports failed. Cannot generate IR"
@@ -67,18 +68,22 @@ def struct_processor(self, struct, processing_stack=None):
6768
)
6869

6970
# Actual processor logic here after dependencies are resolved
70-
self.gen_ir(struct)
71+
self.generated_debug_info.append(
72+
(struct, self.gen_ir(struct, self.generated_debug_info))
73+
)
7174
self.generated.append(struct.name)
7275

7376
finally:
7477
# Remove from processing stack after we're done
7578
processing_stack.discard(struct.name)
7679

77-
def gen_ir(self, struct):
80+
def gen_ir(self, struct, generated_debug_info):
7881
# TODO: we add the btf_ama attribute by monkey patching in the end of compilation, but once llvmlite
7982
# accepts our issue, we will resort to normal accessed attribute based attribute addition
8083
# currently we generate all possible field accesses for CO-RE and put into the assignment table
81-
debug_info = debug_info_generation(struct, self.llvm_module)
84+
debug_info = debug_info_generation(
85+
struct, self.llvm_module, generated_debug_info
86+
)
8287
field_index = 0
8388
for field_name, field in struct.fields.items():
8489
# does not take arrays and similar types into consideration yet.
@@ -126,6 +131,7 @@ def gen_ir(self, struct):
126131
)
127132
globvar.linkage = "external"
128133
globvar.set_metadata("llvm.preserve.access.index", debug_info)
134+
return debug_info
129135

130136
def _struct_name_generator(
131137
self,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir
2+
from pythonbpf.maps import HashMap
3+
from pythonbpf.helper import XDP_PASS
4+
from vmlinux import TASK_COMM_LEN # noqa: F401
5+
from vmlinux import struct_xdp_md
6+
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
7+
from ctypes import c_int64
8+
9+
# Instructions to how to run this program
10+
# 1. Install PythonBPF: pip install pythonbpf
11+
# 2. Run the program: python examples/xdp_pass.py
12+
# 3. Run the program with sudo: sudo tools/check.sh run examples/xdp_pass.o
13+
# 4. Attach object file to any network device with something like ./check.sh xdp examples/xdp_pass.o tailscale0
14+
# 5. send traffic through the device and observe effects
15+
16+
17+
@bpf
18+
@map
19+
def count() -> HashMap:
20+
return HashMap(key=c_int64, value=c_int64, max_entries=1)
21+
22+
23+
@bpf
24+
@section("xdp")
25+
def hello_world(ctx: struct_xdp_md) -> c_int64:
26+
key = 0
27+
one = 1
28+
prev = count().lookup(key)
29+
if prev:
30+
prevval = prev + 1
31+
print(f"count: {prevval}")
32+
count().update(key, prevval)
33+
return XDP_PASS
34+
else:
35+
count().update(key, one)
36+
37+
return XDP_PASS
38+
39+
40+
@bpf
41+
@bpfglobal
42+
def LICENSE() -> str:
43+
return "GPL"
44+
45+
46+
compile_to_ir("xdp_pass.py", "xdp_pass.ll")

0 commit comments

Comments
 (0)