mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
add dependency debug info list
This commit is contained in:
@ -1,10 +1,14 @@
|
|||||||
from pythonbpf.debuginfo import DebugInfoGenerator
|
from pythonbpf.debuginfo import DebugInfoGenerator
|
||||||
|
from ..dependency_node import DependencyNode
|
||||||
|
|
||||||
|
|
||||||
def debug_info_generation(struct, llvm_module):
|
def debug_info_generation(
|
||||||
|
struct: DependencyNode, llvm_module, generated_debug_info: list
|
||||||
|
):
|
||||||
generator = DebugInfoGenerator(llvm_module)
|
generator = DebugInfoGenerator(llvm_module)
|
||||||
# this is sample debug info generation
|
print("DEBUG1", generated_debug_info)
|
||||||
# i64type = generator.get_uint64_type()
|
for field in struct.fields:
|
||||||
|
print("DEBUG", field)
|
||||||
|
|
||||||
struct_type = generator.create_struct_type([], 64 * 4, is_distinct=True)
|
struct_type = generator.create_struct_type([], 64 * 4, is_distinct=True)
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,7 @@ class IRGenerator:
|
|||||||
self.llvm_module = llvm_module
|
self.llvm_module = llvm_module
|
||||||
self.handler: DependencyHandler = handler
|
self.handler: DependencyHandler = handler
|
||||||
self.generated: list[str] = []
|
self.generated: list[str] = []
|
||||||
|
self.generated_debug_info: list = []
|
||||||
if not handler.is_ready:
|
if not handler.is_ready:
|
||||||
raise ImportError(
|
raise ImportError(
|
||||||
"Semantic analysis of vmlinux imports failed. Cannot generate IR"
|
"Semantic analysis of vmlinux imports failed. Cannot generate IR"
|
||||||
@ -67,18 +68,22 @@ class IRGenerator:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Actual processor logic here after dependencies are resolved
|
# Actual processor logic here after dependencies are resolved
|
||||||
self.gen_ir(struct)
|
self.generated_debug_info.append(
|
||||||
|
(struct, self.gen_ir(struct, self.generated_debug_info))
|
||||||
|
)
|
||||||
self.generated.append(struct.name)
|
self.generated.append(struct.name)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# Remove from processing stack after we're done
|
# Remove from processing stack after we're done
|
||||||
processing_stack.discard(struct.name)
|
processing_stack.discard(struct.name)
|
||||||
|
|
||||||
def gen_ir(self, struct):
|
def gen_ir(self, struct, generated_debug_info):
|
||||||
# TODO: we add the btf_ama attribute by monkey patching in the end of compilation, but once llvmlite
|
# TODO: we add the btf_ama attribute by monkey patching in the end of compilation, but once llvmlite
|
||||||
# accepts our issue, we will resort to normal accessed attribute based attribute addition
|
# accepts our issue, we will resort to normal accessed attribute based attribute addition
|
||||||
# currently we generate all possible field accesses for CO-RE and put into the assignment table
|
# currently we generate all possible field accesses for CO-RE and put into the assignment table
|
||||||
debug_info = debug_info_generation(struct, self.llvm_module)
|
debug_info = debug_info_generation(
|
||||||
|
struct, self.llvm_module, generated_debug_info
|
||||||
|
)
|
||||||
field_index = 0
|
field_index = 0
|
||||||
for field_name, field in struct.fields.items():
|
for field_name, field in struct.fields.items():
|
||||||
# does not take arrays and similar types into consideration yet.
|
# does not take arrays and similar types into consideration yet.
|
||||||
@ -126,6 +131,7 @@ class IRGenerator:
|
|||||||
)
|
)
|
||||||
globvar.linkage = "external"
|
globvar.linkage = "external"
|
||||||
globvar.set_metadata("llvm.preserve.access.index", debug_info)
|
globvar.set_metadata("llvm.preserve.access.index", debug_info)
|
||||||
|
return debug_info
|
||||||
|
|
||||||
def _struct_name_generator(
|
def _struct_name_generator(
|
||||||
self,
|
self,
|
||||||
|
|||||||
46
tests/passing_tests/vmlinux/xdp_pass.py
Normal file
46
tests/passing_tests/vmlinux/xdp_pass.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
from pythonbpf.helper import XDP_PASS
|
||||||
|
from vmlinux import TASK_COMM_LEN # noqa: F401
|
||||||
|
from vmlinux import struct_xdp_md
|
||||||
|
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
|
||||||
|
from ctypes import c_int64
|
||||||
|
|
||||||
|
# Instructions to how to run this program
|
||||||
|
# 1. Install PythonBPF: pip install pythonbpf
|
||||||
|
# 2. Run the program: python examples/xdp_pass.py
|
||||||
|
# 3. Run the program with sudo: sudo tools/check.sh run examples/xdp_pass.o
|
||||||
|
# 4. Attach object file to any network device with something like ./check.sh xdp examples/xdp_pass.o tailscale0
|
||||||
|
# 5. send traffic through the device and observe effects
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def count() -> HashMap:
|
||||||
|
return HashMap(key=c_int64, value=c_int64, max_entries=1)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("xdp")
|
||||||
|
def hello_world(ctx: struct_xdp_md) -> c_int64:
|
||||||
|
key = 0
|
||||||
|
one = 1
|
||||||
|
prev = count().lookup(key)
|
||||||
|
if prev:
|
||||||
|
prevval = prev + 1
|
||||||
|
print(f"count: {prevval}")
|
||||||
|
count().update(key, prevval)
|
||||||
|
return XDP_PASS
|
||||||
|
else:
|
||||||
|
count().update(key, one)
|
||||||
|
|
||||||
|
return XDP_PASS
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile_to_ir("xdp_pass.py", "xdp_pass.ll")
|
||||||
Reference in New Issue
Block a user