-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrace_printk.zig
50 lines (43 loc) · 1.63 KB
/
trace_printk.zig
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const std = @import("std");
const root = @import("root.zig");
const print = std.debug.print;
const testing = std.testing;
const allocator = root.allocator;
const libbpf = root.libbpf;
test "trace_printk" {
const bytes = @embedFile("@trace_printk");
_ = libbpf.libbpf_set_print(root.dbg_printf);
const obj = libbpf.bpf_object__open_mem(bytes.ptr, bytes.len, null);
if (obj == null) {
print("failed to open bpf object: {}\n", .{std.posix.errno(-1)});
return error.OPEN;
}
defer libbpf.bpf_object__close(obj);
var ret = libbpf.bpf_object__load(obj);
if (ret != 0) {
print("failed to load bpf object: {}\n", .{std.posix.errno(-1)});
return error.LOAD;
}
if (libbpf.bpf_object__next_program(obj, null)) |prog| {
const f = try root.open_tracebuf_pipe(true);
defer root.close_tracebuf_pipe(f);
const arg: u64 = 123;
// run bpf program
const fd = libbpf.bpf_program__fd(prog);
var attr = std.mem.zeroInit(libbpf.bpf_test_run_opts, .{
.sz = @sizeOf(libbpf.bpf_test_run_opts),
.ctx_in = &arg,
.ctx_size_in = 8,
});
ret = libbpf.bpf_prog_test_run_opts(fd, &attr);
if (ret != 0) {
print("failed run prog: {}\n", .{std.posix.errno(-1)});
return error.RUN;
}
try testing.expectEqual(std.fmt.count("{}", .{arg}), attr.retval);
const r = f.reader();
const l = try r.readUntilDelimiterAlloc(allocator, '\n', std.math.maxInt(u32));
defer allocator.free(l);
try testing.expectStringEndsWith(l, "bpf_trace_printk: 123");
}
}