Skip to content

Commit e1f9ac6

Browse files
add dependency tree functionality
1 parent 27ab3aa commit e1f9ac6

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ 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(elem_type._type_.__name__ if hasattr(elem_type._type_, "__name__") else str(elem_type._type_))
116116
elif containing_type.__module__ == ctypes.__name__:
117117
if isinstance(elem_type, type):
118118
if issubclass(elem_type, ctypes.Array):
@@ -149,6 +149,7 @@ def process_vmlinux_post_ast(
149149
"Module not supported in recursive resolution"
150150
)
151151
else:
152+
new_dep_node.add_dependent(elem_type.__name__ if hasattr(elem_type, "__name__") else str(elem_type))
152153
process_vmlinux_post_ast(
153154
elem_type, llvm_handler, handler, processing_stack
154155
)

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
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
6-
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
5+
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
6+
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
77

88
from ctypes import c_int64
99

0 commit comments

Comments
 (0)