Skip to content

Commit

Permalink
ktrace-CO-RE/ktrace01: Add --debug option to userspace loader
Browse files Browse the repository at this point in the history
Use this --debug to better understand what libbpf does of
CO-RE relocation tricks and lookup of BTF-IDs.

Example output:

 [...]
 libbpf: loading kernel BTF '/sys/kernel/btf/vmlinux': 0
 libbpf: map 'ktrace01.data': created successfully, fd=4
 libbpf: sec 'kprobe/udp_send_skb': found 4 CO-RE relocations
 libbpf: prog 'udp_send_skb': relo #0: kind <byte_off> (0), spec is [2] struct pt_regs.di (0:14 @ offset 112)
 libbpf: CO-RE relocating [0] struct pt_regs: found target candidate [176] struct pt_regs in [vmlinux]
 libbpf: prog 'udp_send_skb': relo #0: matching candidate #0 [176] struct pt_regs.di (0:14 @ offset 112)
 libbpf: prog 'udp_send_skb': relo #0: patched insn #0 (LDX/ST/STX) off 112 -> 112
 libbpf: prog 'udp_send_skb': relo #1: kind <byte_off> (0), spec is [7] struct sk_buff___local.hash (0:1 @ offset 4)
 libbpf: CO-RE relocating [0] struct sk_buff___local: found target candidate [2965] struct sk_buff in [vmlinux]
 [...]

Notice how the BTF-ID of 'struct sk_buff' is resolved to be 2965.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
  • Loading branch information
netoptimizer committed Jun 24, 2021
1 parent 2390b4b commit 0542d8a
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions ktrace-CO-RE/ktrace01.c
Expand Up @@ -10,6 +10,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>

#define pr_err(fmt, ...) \
fprintf(stderr, "%s:%d - " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
Expand All @@ -36,21 +37,45 @@ void read_trace_pipe(void)
}
}

int print_all_levels(enum libbpf_print_level level,
const char *format, va_list args)
{
return vfprintf(stderr, format, args);
}

static const struct option long_options[] = {
{ "debug", no_argument, NULL, 'd' },
{ 0, 0, NULL, 0 }
};

int main(int argc, char **argv)
{
struct bpf_object *obj = NULL;
struct bpf_link *link = NULL;
struct bpf_program *prog;
int opt, longindex = 0;
char filename[256];
//char pin_file[256];
// char *pin_file;
char buf[100];
int err;
// int c;

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);

/* Parse commands line args */
while ((opt = getopt_long(argc, argv, "d",
long_options, &longindex)) != -1) {
switch (opt) {
case 'd':
libbpf_set_print(print_all_levels);
// verifier_logs = true;
break;
default:
pr_err("Unrecognized option '%s'\n", argv[optind - 1]);
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;

obj = bpf_object__open_file(filename, NULL);
err = libbpf_get_error(obj);
if (err) {
Expand Down

0 comments on commit 0542d8a

Please sign in to comment.