Blame view

samples/bpf/tracex1_user.c 1.07 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
2
  #include <stdio.h>
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
3
  #include <unistd.h>
63841bc08   Daniel T. Lee   samples, bpf: Ref...
4
  #include <bpf/libbpf.h>
24a6034ac   Daniel T. Lee   samples, bpf: Mov...
5
  #include "trace_helpers.h"
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
6
7
8
  
  int main(int ac, char **argv)
  {
63841bc08   Daniel T. Lee   samples, bpf: Ref...
9
10
11
  	struct bpf_link *link = NULL;
  	struct bpf_program *prog;
  	struct bpf_object *obj;
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
12
  	char filename[256];
63841bc08   Daniel T. Lee   samples, bpf: Ref...
13
  	FILE *f;
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
14
15
  
  	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
63841bc08   Daniel T. Lee   samples, bpf: Ref...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  	obj = bpf_object__open_file(filename, NULL);
  	if (libbpf_get_error(obj)) {
  		fprintf(stderr, "ERROR: opening BPF object file failed
  ");
  		return 0;
  	}
  
  	prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
  	if (!prog) {
  		fprintf(stderr, "ERROR: finding a prog in obj file failed
  ");
  		goto cleanup;
  	}
  
  	/* load BPF program */
  	if (bpf_object__load(obj)) {
  		fprintf(stderr, "ERROR: loading BPF object file failed
  ");
  		goto cleanup;
  	}
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
36

63841bc08   Daniel T. Lee   samples, bpf: Ref...
37
38
39
40
41
42
  	link = bpf_program__attach(prog);
  	if (libbpf_get_error(link)) {
  		fprintf(stderr, "ERROR: bpf_program__attach failed
  ");
  		link = NULL;
  		goto cleanup;
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
43
44
45
46
47
48
  	}
  
  	f = popen("taskset 1 ping -c5 localhost", "r");
  	(void) f;
  
  	read_trace_pipe();
63841bc08   Daniel T. Lee   samples, bpf: Ref...
49
50
51
  cleanup:
  	bpf_link__destroy(link);
  	bpf_object__close(obj);
b896c4f95   Alexei Starovoitov   samples/bpf: Add ...
52
53
  	return 0;
  }