Skip to content

Commit d87decd

Browse files
Add bpf_ktime_get_ns helper function without IR gen
1 parent 3b5425c commit d87decd

4 files changed

Lines changed: 12 additions & 4 deletions

File tree

examples/execve2.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pythonbpf.decorators import bpf, section
22
from ctypes import c_void_p, c_int64, c_int32
3-
3+
from pythonbpf.helpers import bpf_ktime_get_ns
44

55
@bpf
66
@section("tracepoint/syscalls/sys_enter_execve")
@@ -11,8 +11,9 @@ def hello(ctx: c_void_p) -> c_int32:
1111

1212
@bpf
1313
@section("tracepoint/syscalls/sys_exit_execve")
14-
def hello_again(ctx: c_void_p) -> c_int32:
14+
def hello_again(ctx: c_void_p) -> c_int64:
1515
print("exited")
16-
return c_int32(0)
16+
ts = bpf_ktime_get_ns()
17+
return c_int64(0)
1718

1819
LICENSE = "GPL"

pythonbpf/bpf_helper_handler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import ast
22
from llvmlite import ir
33

4+
def bpf_ktime_get_ns_emitter(call, module, builder, func):
5+
pass
6+
47
def bpf_printk_emitter(call, module, builder, func):
58
if not hasattr(func, "_fmt_counter"):
69
func._fmt_counter = 0

pythonbpf/functions_pass.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from llvmlite import ir
22
import ast
33

4-
from .bpf_helper_handler import bpf_printk_emitter
4+
from .bpf_helper_handler import bpf_printk_emitter, bpf_ktime_get_ns_emitter
55
from .type_deducer import ctypes_to_ir
66

77
def get_probe_string(func_node):
@@ -29,6 +29,8 @@ def process_func_body(module, builder, func_node, func, ret_type):
2929
call = stmt.value
3030
if isinstance(call.func, ast.Name) and call.func.id == "print":
3131
bpf_printk_emitter(call, module, builder, func)
32+
if isinstance(call.func, ast.Name) and call.func.id == "bpf_ktime_get_ns":
33+
bpf_ktime_get_ns_emitter(call, module, builder, func)
3234
elif isinstance(stmt, ast.Return):
3335
if stmt.value is None:
3436
builder.ret(ir.Constant(ir.IntType(32), 0))

pythonbpf/helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def bpf_ktime_get_ns():
2+
return bpf_ktime_get_ns()

0 commit comments

Comments
 (0)