xref: /openbmc/linux/samples/bpf/tracex1_user.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b896c4f9SAlexei Starovoitov #include <stdio.h>
3b896c4f9SAlexei Starovoitov #include <unistd.h>
463841bc0SDaniel T. Lee #include <bpf/libbpf.h>
524a6034aSDaniel T. Lee #include "trace_helpers.h"
6b896c4f9SAlexei Starovoitov 
main(int ac,char ** argv)7b896c4f9SAlexei Starovoitov int main(int ac, char **argv)
8b896c4f9SAlexei Starovoitov {
963841bc0SDaniel T. Lee 	struct bpf_link *link = NULL;
1063841bc0SDaniel T. Lee 	struct bpf_program *prog;
1163841bc0SDaniel T. Lee 	struct bpf_object *obj;
12b896c4f9SAlexei Starovoitov 	char filename[256];
1363841bc0SDaniel T. Lee 	FILE *f;
14b896c4f9SAlexei Starovoitov 
15*4a0ee788SDaniel T. Lee 	snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
1663841bc0SDaniel T. Lee 	obj = bpf_object__open_file(filename, NULL);
1763841bc0SDaniel T. Lee 	if (libbpf_get_error(obj)) {
1863841bc0SDaniel T. Lee 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
1963841bc0SDaniel T. Lee 		return 0;
2063841bc0SDaniel T. Lee 	}
21b896c4f9SAlexei Starovoitov 
2263841bc0SDaniel T. Lee 	prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
2363841bc0SDaniel T. Lee 	if (!prog) {
2463841bc0SDaniel T. Lee 		fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
2563841bc0SDaniel T. Lee 		goto cleanup;
2663841bc0SDaniel T. Lee 	}
2763841bc0SDaniel T. Lee 
2863841bc0SDaniel T. Lee 	/* load BPF program */
2963841bc0SDaniel T. Lee 	if (bpf_object__load(obj)) {
3063841bc0SDaniel T. Lee 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
3163841bc0SDaniel T. Lee 		goto cleanup;
3263841bc0SDaniel T. Lee 	}
3363841bc0SDaniel T. Lee 
3463841bc0SDaniel T. Lee 	link = bpf_program__attach(prog);
3563841bc0SDaniel T. Lee 	if (libbpf_get_error(link)) {
3663841bc0SDaniel T. Lee 		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
3763841bc0SDaniel T. Lee 		link = NULL;
3863841bc0SDaniel T. Lee 		goto cleanup;
39b896c4f9SAlexei Starovoitov 	}
40b896c4f9SAlexei Starovoitov 
41b896c4f9SAlexei Starovoitov 	f = popen("taskset 1 ping -c5 localhost", "r");
42b896c4f9SAlexei Starovoitov 	(void) f;
43b896c4f9SAlexei Starovoitov 
44b896c4f9SAlexei Starovoitov 	read_trace_pipe();
45b896c4f9SAlexei Starovoitov 
4663841bc0SDaniel T. Lee cleanup:
4763841bc0SDaniel T. Lee 	bpf_link__destroy(link);
4863841bc0SDaniel T. Lee 	bpf_object__close(obj);
49b896c4f9SAlexei Starovoitov 	return 0;
50b896c4f9SAlexei Starovoitov }
51