mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Multi string and multi program support
This commit is contained in:
@ -11,7 +11,7 @@ case "$1" in
|
|||||||
;;
|
;;
|
||||||
run)
|
run)
|
||||||
echo "[*] Loading and running $FILE"
|
echo "[*] Loading and running $FILE"
|
||||||
sudo bpftool prog load "$FILE" "$PIN_PATH" autoattach
|
sudo bpftool prog loadall "$FILE" "$PIN_PATH" autoattach
|
||||||
echo "[+] Program loaded. Press Ctrl+C to stop"
|
echo "[+] Program loaded. Press Ctrl+C to stop"
|
||||||
sudo cat /sys/kernel/debug/tracing/trace_pipe
|
sudo cat /sys/kernel/debug/tracing/trace_pipe
|
||||||
sudo rm -f "$PIN_PATH"
|
sudo rm -f "$PIN_PATH"
|
||||||
|
|||||||
@ -6,12 +6,13 @@ from ctypes import c_void_p, c_int64, c_int32
|
|||||||
@section("tracepoint/syscalls/sys_enter_execve")
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
def hello(ctx: c_void_p) -> c_int32:
|
def hello(ctx: c_void_p) -> c_int32:
|
||||||
print("entered")
|
print("entered")
|
||||||
|
print("multi constant support")
|
||||||
return c_int32(0)
|
return c_int32(0)
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@section("tracepoint/syscalls/sys_exit_execve")
|
@section("tracepoint/syscalls/sys_exit_execve")
|
||||||
def hello_again(ctx: c_void_p) -> c_int64:
|
def hello_again(ctx: c_void_p) -> c_int32:
|
||||||
print("exited")
|
print("exited")
|
||||||
return c_int64(0)
|
return c_int32(0)
|
||||||
|
|
||||||
LICENSE = "GPL"
|
LICENSE = "GPL"
|
||||||
|
|||||||
@ -2,13 +2,17 @@ import ast
|
|||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
|
|
||||||
def bpf_printk_emitter(call, module, builder, func):
|
def bpf_printk_emitter(call, module, builder, func):
|
||||||
# Handle print statement
|
if not hasattr(func, "_fmt_counter"):
|
||||||
|
func._fmt_counter = 0
|
||||||
|
|
||||||
for arg in call.args:
|
for arg in call.args:
|
||||||
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
|
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
|
||||||
fmt_str = arg.value + "\n" + "\0"
|
fmt_str = arg.value + "\n" + "\0"
|
||||||
# Create a global variable for the format string
|
fmt_name = f"{func.name}____fmt{func._fmt_counter}"
|
||||||
|
func._fmt_counter += 1
|
||||||
|
|
||||||
fmt_gvar = ir.GlobalVariable(
|
fmt_gvar = ir.GlobalVariable(
|
||||||
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=f"{func.name}____fmt")
|
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=fmt_name)
|
||||||
fmt_gvar.global_constant = True
|
fmt_gvar.global_constant = True
|
||||||
fmt_gvar.initializer = ir.Constant( # type: ignore
|
fmt_gvar.initializer = ir.Constant( # type: ignore
|
||||||
ir.ArrayType(ir.IntType(8), len(fmt_str)),
|
ir.ArrayType(ir.IntType(8), len(fmt_str)),
|
||||||
@ -17,17 +21,13 @@ def bpf_printk_emitter(call, module, builder, func):
|
|||||||
fmt_gvar.linkage = "internal"
|
fmt_gvar.linkage = "internal"
|
||||||
fmt_gvar.align = 1 # type: ignore
|
fmt_gvar.align = 1 # type: ignore
|
||||||
|
|
||||||
# Cast the global variable to i8*
|
fmt_ptr = builder.bitcast(fmt_gvar, ir.PointerType())
|
||||||
fmt_ptr = builder.bitcast(
|
|
||||||
fmt_gvar, ir.PointerType())
|
|
||||||
|
|
||||||
# Call bpf_trace_printk (assumed to be at address 6)
|
|
||||||
fn_type = ir.FunctionType(ir.IntType(
|
fn_type = ir.FunctionType(ir.IntType(
|
||||||
64), [ir.PointerType(), ir.IntType(32)], var_arg=True)
|
64), [ir.PointerType(), ir.IntType(32)], var_arg=True)
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
fn_addr = ir.Constant(ir.IntType(64), 6)
|
fn_addr = ir.Constant(ir.IntType(64), 6)
|
||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
# Call the function
|
|
||||||
builder.call(fn_ptr, [fmt_ptr, ir.Constant(
|
builder.call(fn_ptr, [fmt_ptr, ir.Constant(
|
||||||
ir.IntType(32), len(fmt_str))], tail=True)
|
ir.IntType(32), len(fmt_str))], tail=True)
|
||||||
|
|||||||
@ -2,7 +2,6 @@ from llvmlite import ir
|
|||||||
|
|
||||||
#TODO: THIS IS NOT SUPPOSED TO MATCH STRINGS :skull:
|
#TODO: THIS IS NOT SUPPOSED TO MATCH STRINGS :skull:
|
||||||
def ctypes_to_ir(ctype: str):
|
def ctypes_to_ir(ctype: str):
|
||||||
print("CTYPE", ctype)
|
|
||||||
mapping = {
|
mapping = {
|
||||||
"c_int8": ir.IntType(8),
|
"c_int8": ir.IntType(8),
|
||||||
"c_uint8": ir.IntType(8),
|
"c_uint8": ir.IntType(8),
|
||||||
|
|||||||
Reference in New Issue
Block a user