-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathringbuf.zig
More file actions
34 lines (29 loc) · 945 Bytes
/
ringbuf.zig
File metadata and controls
34 lines (29 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const std = @import("std");
const bpf = @import("bpf");
const BPF = std.os.linux.BPF;
const helpers = BPF.kern.helpers;
var events = bpf.Map.RingBuffer("events", 1, 0).init();
var my_pid = bpf.Map.ArrayMap("my_pid", u32, 1, 0).init();
var n: u32 = 0;
export fn test_ringbuf() linksection("kprobe/do_nanosleep") c_int {
const pid = my_pid.lookup(0) orelse {
bpf.printErr(@src(), @as(c_long, 1));
return 1;
};
const cur_pid: u32 = @truncate(helpers.get_current_pid_tgid());
if (cur_pid == pid.*) {
var a: u8 = '1';
if (n % 2 == 1) {
events.event_output(std.mem.asBytes(&a), @enumFromInt(n % 3));
} else {
const resv = events.reserve(@TypeOf(a), 0) orelse {
bpf.printErr(@src(), @as(c_long, 1));
return 1;
};
resv.header_ptr.* = a;
resv.commit();
}
n += 1;
}
return 0;
}