init eACGM

This commit is contained in:
Tokisakix
2025-08-07 10:14:54 +08:00
commit 7a4a0b1b14
51 changed files with 11495 additions and 0 deletions

2
eacgm/bpf/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
from .base import BPFState, BaseBPF
from .bccBPF import BccBPF

38
eacgm/bpf/base.py Normal file
View File

@@ -0,0 +1,38 @@
class BPFState:
task:str
pid:int
cpu:int
timestamp:int
message:str
def __init__(self) -> None:
self.task = None
self.pid = None
self.cpu = None
self.timestamp = None
self.message = None
return
def is_none(self) -> bool:
return self.task is None
def __repr__(self) -> str:
info = f"BPFState {self.task} {self.pid} { self.cpu} {self.timestamp} {self.message}"
return info
class BaseBPF:
def __init__(self, name:str) -> None:
self.name = name
return
def attach_uprobe(self, exe_path:str, exe_sym:str, bpf_func:str) -> bool:
raise NotADirectoryError
def attach_uretprobe(self, exe_path:str, exe_sym:str, bpf_func:str) -> bool:
raise NotADirectoryError
def cleanup(self) -> None:
raise NotADirectoryError
def trace_ebpf(self) -> BPFState:
raise NotADirectoryError

34
eacgm/bpf/bccBPF.py Normal file
View File

@@ -0,0 +1,34 @@
from bcc import BPF
from typing import List
from .base import BPFState, BaseBPF
class BccBPF(BaseBPF):
def __init__(self, name:str, text:str, cflags:List=[]) -> None:
super().__init__(name)
self.bpf = BPF(text=text, cflags=cflags)
return
def attach_uprobe(self, exe_path:str, exe_sym:str, bpf_func:str) -> bool:
self.bpf.attach_uprobe(exe_path, exe_sym, fn_name=bpf_func)
return
def attach_uretprobe(self, exe_path:str, exe_sym:str, bpf_func:str) -> bool:
self.bpf.attach_uretprobe(exe_path, exe_sym, fn_name=bpf_func)
return
def cleanup(self) -> None:
self.bpf.cleanup()
return
def trace_ebpf(self, nonblocking:bool) -> BPFState:
(task, pid, cpu, _, _, message) = self.bpf.trace_fields(nonblocking)
state = BPFState()
if task is not None:
message = message.decode("utf-8")
state.task = task.decode("utf-8")
state.pid = int(pid)
state.cpu = int(cpu)
state.timestamp = int(message.split("@")[0])
state.message = message.split("@")[1:]
return state