Skip to content

Commit 85a62d6

Browse files
add example and support unsigned i64
1 parent c3fc790 commit 85a62d6

File tree

4 files changed

+118
-7
lines changed

4 files changed

+118
-7
lines changed

pythonbpf/type_deducer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"c_double": ir.DoubleType(),
1515
"c_void_p": ir.IntType(64),
1616
"c_long": ir.IntType(64),
17+
"c_ulong": ir.IntType(64),
1718
"c_longlong": ir.IntType(64),
1819
# Not so sure about this one
1920
"str": ir.PointerType(ir.IntType(8)),

tests/c-form/kprobe.bpf.c

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,75 @@
22
#include <bpf/bpf_helpers.h>
33
#include <bpf/bpf_tracing.h>
44

5-
char LICENSE[] SEC("license") = "Dual BSD/GPL";
5+
char LICENSE[] SEC("license") = "GPL";
66

77
SEC("kprobe/do_unlinkat")
88
int kprobe_execve(struct pt_regs *ctx)
99
{
1010
bpf_printk("unlinkat created");
11-
return 0;
12-
}
1311

14-
SEC("kretprobe/do_unlinkat")
15-
int kretprobe_execve(struct pt_regs *ctx)
16-
{
17-
bpf_printk("unlinkat returned\n");
12+
unsigned long r15 = ctx->r15;
13+
bpf_printk("r15: %lld", r15);
14+
15+
unsigned long r14 = ctx->r14;
16+
bpf_printk("r14: %lld", r14);
17+
18+
unsigned long r13 = ctx->r13;
19+
bpf_printk("r13: %lld", r13);
20+
21+
unsigned long r12 = ctx->r12;
22+
bpf_printk("r12: %lld", r12);
23+
24+
unsigned long bp = ctx->bp;
25+
bpf_printk("rbp: %lld", bp);
26+
27+
unsigned long bx = ctx->bx;
28+
bpf_printk("rbx: %lld", bx);
29+
30+
unsigned long r11 = ctx->r11;
31+
bpf_printk("r11: %lld", r11);
32+
33+
unsigned long r10 = ctx->r10;
34+
bpf_printk("r10: %lld", r10);
35+
36+
unsigned long r9 = ctx->r9;
37+
bpf_printk("r9: %lld", r9);
38+
39+
unsigned long r8 = ctx->r8;
40+
bpf_printk("r8: %lld", r8);
41+
42+
unsigned long ax = ctx->ax;
43+
bpf_printk("rax: %lld", ax);
44+
45+
unsigned long cx = ctx->cx;
46+
bpf_printk("rcx: %lld", cx);
47+
48+
unsigned long dx = ctx->dx;
49+
bpf_printk("rdx: %lld", dx);
50+
51+
unsigned long si = ctx->si;
52+
bpf_printk("rsi: %lld", si);
53+
54+
unsigned long di = ctx->di;
55+
bpf_printk("rdi: %lld", di);
56+
57+
unsigned long orig_ax = ctx->orig_ax;
58+
bpf_printk("orig_rax: %lld", orig_ax);
59+
60+
unsigned long ip = ctx->ip;
61+
bpf_printk("rip: %lld", ip);
62+
63+
unsigned long cs = ctx->cs;
64+
bpf_printk("cs: %lld", cs);
65+
66+
unsigned long flags = ctx->flags;
67+
bpf_printk("eflags: %lld", flags);
68+
69+
unsigned long sp = ctx->sp;
70+
bpf_printk("rsp: %lld", sp);
71+
72+
unsigned long ss = ctx->ss;
73+
bpf_printk("ss: %lld", ss);
74+
1875
return 0;
1976
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from pythonbpf import bpf, section, bpfglobal, BPF, trace_pipe
2+
from pythonbpf import compile # noqa: F401
3+
from vmlinux import struct_pt_regs
4+
from ctypes import c_int64, c_int32, c_void_p # noqa: F401
5+
6+
7+
@bpf
8+
@section("kprobe/do_unlinkat")
9+
def kprobe_execve(ctx: struct_pt_regs) -> c_int64:
10+
r15 = ctx.r15
11+
r14 = ctx.r14
12+
r13 = ctx.r13
13+
r12 = ctx.r12
14+
bp = ctx.bp
15+
bx = ctx.bx
16+
r11 = ctx.r11
17+
r10 = ctx.r10
18+
r9 = ctx.r9
19+
r8 = ctx.r8
20+
ax = ctx.ax
21+
cx = ctx.cx
22+
dx = ctx.dx
23+
si = ctx.si
24+
di = ctx.di
25+
orig_ax = ctx.orig_ax
26+
ip = ctx.ip
27+
cs = ctx.cs
28+
flags = ctx.flags
29+
sp = ctx.sp
30+
ss = ctx.ss
31+
32+
print(f"r15={r15} r14={r14} r13={r13}")
33+
print(f"r12={r12} rbp={bp} rbx={bx}")
34+
print(f"r11={r11} r10={r10} r9={r9}")
35+
print(f"r8={r8} rax={ax} rcx={cx}")
36+
print(f"rdx={dx} rsi={si} rdi={di}")
37+
print(f"orig_rax={orig_ax} rip={ip} cs={cs}")
38+
print(f"eflags={flags} rsp={sp} ss={ss}")
39+
40+
return c_int64(0)
41+
42+
43+
@bpf
44+
@bpfglobal
45+
def LICENSE() -> str:
46+
return "GPL"
47+
48+
49+
b = BPF()
50+
b.load()
51+
b.attach_all()
52+
53+
trace_pipe()

0 commit comments

Comments
 (0)