Skip to content

Commit 3628276

Browse files
committed
Add ktime
1 parent 8ee5d03 commit 3628276

5 files changed

Lines changed: 28 additions & 6 deletions

File tree

examples/c-form/ex4.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ long hello_again(void *ctx) {
3333
u64 delta = bpf_ktime_get_ns() - *tsp;
3434
if (delta < 1000000000) {
3535
// output if time is less than 1 second
36-
bpf_trace_printk("%d\\n", delta / 1000000);
36+
bpf_printk("execve called within last second");
3737
}
3838
bpf_map_delete_elem(&last, &key);
3939
}

examples/execve2.py

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

66

@@ -9,6 +9,7 @@
99
def last() -> HashMap:
1010
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
1111

12+
1213
@bpf
1314
@section("tracepoint/syscalls/sys_enter_execve")
1415
def hello(ctx: c_void_p) -> c_int32:
@@ -24,9 +25,10 @@ def hello_again(ctx: c_void_p) -> c_int64:
2425
key = 0
2526
tsp = last().lookup(key)
2627
print(tsp)
27-
ts = bpf_ktime_get_ns()
28+
ktime()
2829
return c_int64(0)
2930

31+
3032
@bpf
3133
@bpfglobal
3234
def LICENSE() -> str:

pythonbpf/bpf_helper_handler.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33

44

55
def bpf_ktime_get_ns_emitter(call, module, builder, func):
6-
pass
6+
"""
7+
Emit LLVM IR for bpf_ktime_get_ns helper function call.
8+
"""
9+
helper_id = ir.Constant(ir.IntType(64), 5)
10+
fn_type = ir.FunctionType(ir.IntType(64), [], var_arg=False)
11+
fn_ptr_type = ir.PointerType(fn_type)
12+
fn_ptr = builder.inttoptr(helper_id, fn_ptr_type)
13+
result = builder.call(fn_ptr, [], tail=False)
14+
return result
715

816

917
def bpf_map_lookup_elem_emitter(map_ptr, key_ptr, module, builder):
@@ -62,3 +70,14 @@ def bpf_printk_emitter(call, module, builder, func):
6270

6371
builder.call(fn_ptr, [fmt_ptr, ir.Constant(
6472
ir.IntType(32), len(fmt_str))], tail=True)
73+
74+
75+
helper_func_list = {
76+
"lookup": bpf_map_lookup_elem_emitter,
77+
"print": bpf_printk_emitter,
78+
"ktime": bpf_ktime_get_ns_emitter,
79+
}
80+
81+
82+
def handle_helper_call(call, module, builder, func):
83+
return None

pythonbpf/functions_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def process_func_body(module, builder, func_node, func, ret_type, map_sym_tab):
123123
call = stmt.value
124124
if isinstance(call.func, ast.Name) and call.func.id == "print":
125125
bpf_printk_emitter(call, module, builder, func)
126-
if isinstance(call.func, ast.Name) and call.func.id == "bpf_ktime_get_ns":
126+
if isinstance(call.func, ast.Name) and call.func.id == "ktime":
127127
bpf_ktime_get_ns_emitter(call, module, builder, func)
128128
elif isinstance(stmt, ast.Assign):
129129
handle_assign(module, builder, stmt, map_sym_tab, local_sym_tab)

pythonbpf/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ctypes
22

3-
def bpf_ktime_get_ns():
3+
4+
def ktime():
45
return ctypes.c_int64(0)

0 commit comments

Comments
 (0)