Skip to content

Commit 09ba749

Browse files
Merge pull request #52 from pythonbpf/vmlinux-ir-gen
Dependency tree functionality to semantic analyser
2 parents 27ab3aa + a03d3e5 commit 09ba749

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ def process_vmlinux_post_ast(
112112
type_length = elem_type._length_
113113

114114
if containing_type.__module__ == "vmlinux":
115-
pass
115+
new_dep_node.add_dependent(
116+
elem_type._type_.__name__
117+
if hasattr(elem_type._type_, "__name__")
118+
else str(elem_type._type_)
119+
)
116120
elif containing_type.__module__ == ctypes.__name__:
117121
if isinstance(elem_type, type):
118122
if issubclass(elem_type, ctypes.Array):
@@ -149,6 +153,11 @@ def process_vmlinux_post_ast(
149153
"Module not supported in recursive resolution"
150154
)
151155
else:
156+
new_dep_node.add_dependent(
157+
elem_type.__name__
158+
if hasattr(elem_type, "__name__")
159+
else str(elem_type)
160+
)
152161
process_vmlinux_post_ast(
153162
elem_type, llvm_handler, handler, processing_stack
154163
)

pythonbpf/vmlinux_parser/dependency_node.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class DependencyNode:
106106
"""
107107

108108
name: str
109+
depends_on: Optional[list[str]] = None
109110
fields: Dict[str, Field] = field(default_factory=dict)
110111
_ready_cache: Optional[bool] = field(default=None, repr=False)
111112

@@ -121,6 +122,8 @@ def add_field(
121122
ready: bool = False,
122123
) -> None:
123124
"""Add a field to the node with an optional initial value and readiness state."""
125+
if self.depends_on is None:
126+
self.depends_on = []
124127
self.fields[name] = Field(
125128
name=name,
126129
type=field_type,
@@ -235,3 +238,9 @@ def get_ready_fields(self) -> Dict[str, Field]:
235238
def get_not_ready_fields(self) -> Dict[str, Field]:
236239
"""Get all fields that are marked as not ready."""
237240
return {name: elem for name, elem in self.fields.items() if not elem.ready}
241+
242+
def add_dependent(self, dep_type):
243+
if dep_type in self.depends_on:
244+
return
245+
else:
246+
self.depends_on.append(dep_type)

pythonbpf/vmlinux_parser/ir_generation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ def __init__(self, module, handler: DependencyHandler):
1212
raise ImportError(
1313
"Semantic analysis of vmlinux imports failed. Cannot generate IR"
1414
)
15+
for struct in handler:
16+
print(struct)
17+
print()

tests/c-form/ex7.bpf.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
#include <linux/bpf.h>
3+
#include "vmlinux.h"
44
#include <bpf/bpf_helpers.h>
55
#include <bpf/bpf_tracing.h>
66

7-
struct trace_entry {
8-
short unsigned int type;
9-
unsigned char flags;
10-
unsigned char preempt_count;
11-
int pid;
12-
};
13-
14-
struct trace_event_raw_sys_enter {
15-
struct trace_entry ent;
16-
long int id;
17-
long unsigned int args[6];
18-
char __data[0];
19-
};
20-
217
struct event {
228
__u32 pid;
239
__u32 uid;

tests/failing_tests/xdp_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pythonbpf.maps import HashMap
33
from pythonbpf.helper import XDP_PASS
44
from vmlinux import struct_xdp_md
5-
from vmlinux import struct_xdp_buff # noqa: F401
5+
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
66
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
77

88
from ctypes import c_int64

0 commit comments

Comments
 (0)