Skip to content

Commit

Permalink
libbpf-tools: Update tools to use BTFGen integration
Browse files Browse the repository at this point in the history
Use save_min_core_btf() to uncompress and save the BTF file for the
current system. If the file is available, then use it when opening the
object.

Signed-off-by: Mauricio Vásquez <[email protected]>
  • Loading branch information
mauriciovasquezbernal committed Mar 11, 2022
1 parent 49e2042 commit 6b9834b
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 21 deletions.
23 changes: 21 additions & 2 deletions libbpf-tools/bashreadline.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <bpf/bpf.h>
#include "bashreadline.h"
#include "bashreadline.skel.h"
#include "min_core_btf_helpers.h"
#include "trace_helpers.h"
#include "uprobe_helpers.h"

Expand Down Expand Up @@ -143,6 +144,7 @@ static void sig_int(int signo)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
Expand All @@ -168,9 +170,21 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

obj = bashreadline_bpf__open_and_load();
err = ensure_core_btf(&open_opts);
if (err) {
warn("failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
goto cleanup;
}

obj = bashreadline_bpf__open_opts(&open_opts);
if (!obj) {
warn("failed to open and load BPF object\n");
warn("failed to open BPF object\n");
goto cleanup;
}

err = bashreadline_bpf__load(obj);
if (err) {
warn("failed to load BPF object: %d\n", err);
goto cleanup;
}

Expand Down Expand Up @@ -218,5 +232,10 @@ int main(int argc, char **argv)
perf_buffer__free(pb);
bashreadline_bpf__destroy(obj);

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

return err != 0;
}
16 changes: 15 additions & 1 deletion libbpf-tools/bindsnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>

#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include "bindsnoop.h"
#include "bindsnoop.skel.h"
#include "trace_helpers.h"
#include "min_core_btf_helpers.h"

#define PERF_BUFFER_PAGES 16
#define PERF_POLL_TIMEOUT_MS 100
Expand Down Expand Up @@ -169,6 +171,7 @@ static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
Expand All @@ -187,7 +190,13 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

obj = bindsnoop_bpf__open();
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}

obj = bindsnoop_bpf__open_opts(&open_opts);
if (!obj) {
warn("failed to open BPF object\n");
return 1;
Expand All @@ -203,6 +212,11 @@ int main(int argc, char **argv)
goto cleanup;
}

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

if (target_ports) {
port_map_fd = bpf_map__fd(obj->maps.ports);
port = strtok(target_ports, ",");
Expand Down
15 changes: 14 additions & 1 deletion libbpf-tools/biopattern.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <bpf/bpf.h>
#include "biopattern.h"
#include "biopattern.skel.h"
#include "min_core_btf_helpers.h"
#include "trace_helpers.h"

static struct env {
Expand Down Expand Up @@ -158,6 +159,7 @@ static int print_map(struct bpf_map *counters, struct partitions *partitions)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
struct partitions *partitions = NULL;
const struct partition *partition;
static const struct argp argp = {
Expand All @@ -175,7 +177,13 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

obj = biopattern_bpf__open();
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}

obj = biopattern_bpf__open_opts(&open_opts);
if (!obj) {
fprintf(stderr, "failed to open BPF object\n");
return 1;
Expand Down Expand Up @@ -235,5 +243,10 @@ int main(int argc, char **argv)
biopattern_bpf__destroy(obj);
partitions__free(partitions);

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

return err != 0;
}
15 changes: 14 additions & 1 deletion libbpf-tools/execsnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <bpf/bpf.h>
#include "execsnoop.h"
#include "execsnoop.skel.h"
#include "min_core_btf_helpers.h"
#include "trace_helpers.h"

#define PERF_BUFFER_PAGES 64
Expand Down Expand Up @@ -258,6 +259,7 @@ static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
Expand All @@ -274,7 +276,13 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

obj = execsnoop_bpf__open();
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}

obj = execsnoop_bpf__open_opts(&open_opts);
if (!obj) {
fprintf(stderr, "failed to open BPF object\n");
return 1;
Expand Down Expand Up @@ -340,5 +348,10 @@ int main(int argc, char **argv)
perf_buffer__free(pb);
execsnoop_bpf__destroy(obj);

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

return err != 0;
}
16 changes: 15 additions & 1 deletion libbpf-tools/exitsnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
#include <signal.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include "exitsnoop.h"
#include "exitsnoop.skel.h"
#include "min_core_btf_helpers.h"
#include "trace_helpers.h"

#define PERF_BUFFER_PAGES 16
Expand Down Expand Up @@ -147,6 +149,7 @@ static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
Expand All @@ -163,7 +166,13 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

obj = exitsnoop_bpf__open();
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}

obj = exitsnoop_bpf__open_opts(&open_opts);
if (!obj) {
warn("failed to open BPF object\n");
return 1;
Expand Down Expand Up @@ -218,5 +227,10 @@ int main(int argc, char **argv)
perf_buffer__free(pb);
exitsnoop_bpf__destroy(obj);

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

return err != 0;
}
15 changes: 14 additions & 1 deletion libbpf-tools/filelife.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <bpf/bpf.h>
#include "filelife.h"
#include "filelife.skel.h"
#include "min_core_btf_helpers.h"
#include "trace_helpers.h"

#define PERF_BUFFER_PAGES 16
Expand Down Expand Up @@ -105,6 +106,7 @@ void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
Expand All @@ -121,7 +123,13 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

obj = filelife_bpf__open();
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}

obj = filelife_bpf__open_opts(&open_opts);
if (!obj) {
fprintf(stderr, "failed to open BPF object\n");
return 1;
Expand Down Expand Up @@ -173,5 +181,10 @@ int main(int argc, char **argv)
perf_buffer__free(pb);
filelife_bpf__destroy(obj);

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

return err != 0;
}
15 changes: 14 additions & 1 deletion libbpf-tools/filetop.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <bpf/bpf.h>
#include "filetop.h"
#include "filetop.skel.h"
#include "min_core_btf_helpers.h"
#include "trace_helpers.h"

#define warn(...) fprintf(stderr, __VA_ARGS__)
Expand Down Expand Up @@ -256,6 +257,7 @@ static int print_stat(struct filetop_bpf *obj)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
Expand All @@ -271,7 +273,13 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

obj = filetop_bpf__open();
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}

obj = filetop_bpf__open_opts(&open_opts);
if (!obj) {
warn("failed to open BPF object\n");
return 1;
Expand Down Expand Up @@ -319,5 +327,10 @@ int main(int argc, char **argv)
cleanup:
filetop_bpf__destroy(obj);

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

return err != 0;
}
15 changes: 14 additions & 1 deletion libbpf-tools/fsdist.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "fsdist.h"
#include "fsdist.skel.h"
#include "min_core_btf_helpers.h"
#include "trace_helpers.h"

#define warn(...) fprintf(stderr, __VA_ARGS__)
Expand Down Expand Up @@ -343,6 +344,7 @@ static int attach_kprobes(struct fsdist_bpf *obj)

int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
Expand All @@ -367,7 +369,13 @@ int main(int argc, char **argv)
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);

skel = fsdist_bpf__open();
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}

skel = fsdist_bpf__open_opts(&open_opts);
if (!skel) {
warn("failed to open BPF object\n");
return 1;
Expand Down Expand Up @@ -437,5 +445,10 @@ int main(int argc, char **argv)
cleanup:
fsdist_bpf__destroy(skel);

if (open_opts.btf_custom_path) {
unlink(open_opts.btf_custom_path);
free((void *)open_opts.btf_custom_path);
}

return err != 0;
}
Loading

0 comments on commit 6b9834b

Please sign in to comment.