-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathhello.c
98 lines (83 loc) · 2 KB
/
hello.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <bpf/libbpf.h>
#include "hello.h"
#include "hello.skel.h"
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
if (level >= LIBBPF_DEBUG)
return 0;
return vfprintf(stderr, format, args);
}
void handle_event(void *ctx, int cpu, void *data, unsigned int data_sz)
{
struct data_t *m = data;
printf("%-6d %-6d %-16s %-16s %s\n", m->pid, m->uid, m->command, m->path, m->message);
}
void lost_event(void *ctx, int cpu, long long unsigned int data_sz)
{
printf("lost event\n");
}
int main()
{
struct hello_bpf *skel;
// struct bpf_object_open_opts *o;
int err;
struct perf_buffer *pb = NULL;
libbpf_set_print(libbpf_print_fn);
char log_buf[64 * 1024];
LIBBPF_OPTS(bpf_object_open_opts, opts,
.kernel_log_buf = log_buf,
.kernel_log_size = sizeof(log_buf),
.kernel_log_level = 1,
);
skel = hello_bpf__open_opts(&opts);
if (!skel) {
printf("Failed to open BPF object\n");
return 1;
}
err = hello_bpf__load(skel);
// Print the verifier log
for (int i=0; i < sizeof(log_buf); i++) {
if (log_buf[i] == 0 && log_buf[i+1] == 0) {
break;
}
printf("%c", log_buf[i]);
}
if (err) {
printf("Failed to load BPF object\n");
hello_bpf__destroy(skel);
return 1;
}
// Attach the progams to the events
err = hello_bpf__attach(skel);
if (err) {
fprintf(stderr, "Failed to attach BPF skeleton: %d\n", err);
hello_bpf__destroy(skel);
return 1;
}
pb = perf_buffer__new(bpf_map__fd(skel->maps.output), 8, handle_event, lost_event, NULL, NULL);
if (!pb) {
err = -1;
fprintf(stderr, "Failed to create ring buffer\n");
hello_bpf__destroy(skel);
return 1;
}
while (true) {
err = perf_buffer__poll(pb, 100 /* timeout, ms */);
// Ctrl-C gives -EINTR
if (err == -EINTR) {
err = 0;
break;
}
if (err < 0) {
printf("Error polling perf buffer: %d\n", err);
break;
}
}
perf_buffer__free(pb);
hello_bpf__destroy(skel);
return -err;
}