xref: /linux/tools/perf/util/bpf_trace_augment.c (revision 559e1b470d32751b08a6f986451448625d5af09f)
1 #include <assert.h>
2 #include <bpf/libbpf.h>
3 #include <errno.h>
4 #include <internal/xyarray.h>
5 #include <perf/threadmap.h>
6 #include <string.h>
7 
8 #include "bpf_skel/augmented_raw_syscalls.skel.h"
9 #include "debug.h"
10 #include "evlist.h"
11 #include "parse-events.h"
12 #include "thread_map.h"
13 #include "trace_augment.h"
14 
15 static struct augmented_raw_syscalls_bpf *skel;
16 static struct evsel *bpf_output;
17 
18 int augmented_syscalls__prepare(void)
19 {
20 	struct bpf_program *prog;
21 	char buf[128];
22 	int err;
23 
24 	skel = augmented_raw_syscalls_bpf__open();
25 	if (!skel) {
26 		pr_debug("Failed to open augmented syscalls BPF skeleton\n");
27 		return -errno;
28 	}
29 
30 	/*
31 	 * Attach just the programs maintaining pids_to_trace now. sys_enter and
32 	 * sys_exit wait for augmented_syscalls__attach(), the rest are tail called.
33 	 */
34 	bpf_object__for_each_program(prog, skel->obj) {
35 		if (prog != skel->progs.sched_process_fork &&
36 		    prog != skel->progs.sched_process_exit &&
37 		    prog != skel->progs.sched_process_exec)
38 			bpf_program__set_autoattach(prog, /*autoattach=*/false);
39 	}
40 
41 	err = augmented_raw_syscalls_bpf__load(skel);
42 	if (err < 0) {
43 		libbpf_strerror(err, buf, sizeof(buf));
44 		pr_debug("Failed to load augmented syscalls BPF skeleton: %s\n", buf);
45 		augmented_syscalls__cleanup();
46 		return err;
47 	}
48 
49 	err = augmented_raw_syscalls_bpf__attach(skel);
50 	if (err < 0) {
51 		libbpf_strerror(err, buf, sizeof(buf));
52 		pr_debug("Failed to attach augmented syscalls BPF skeleton: %s\n", buf);
53 		augmented_syscalls__cleanup();
54 		return err;
55 	}
56 	return 0;
57 }
58 
59 /* Attach sys_enter and sys_exit, once the maps they use are populated. */
60 int augmented_syscalls__attach(void)
61 {
62 	if (skel == NULL)
63 		return 0;
64 
65 	skel->links.sys_enter = bpf_program__attach(skel->progs.sys_enter);
66 	if (skel->links.sys_enter == NULL)
67 		return -errno;
68 
69 	skel->links.sys_exit = bpf_program__attach(skel->progs.sys_exit);
70 	if (skel->links.sys_exit == NULL)
71 		return -errno;
72 
73 	return 0;
74 }
75 
76 int augmented_syscalls__create_bpf_output(struct evlist *evlist)
77 {
78 	int err = parse_event(evlist, "bpf-output/no-inherit=1,name=__augmented_syscalls__/");
79 
80 	if (err) {
81 		pr_err("ERROR: Setup BPF output event failed: %d\n", err);
82 		return err;
83 	}
84 
85 	bpf_output = evlist__last(evlist);
86 	assert(evsel__name_is(bpf_output, "__augmented_syscalls__"));
87 
88 	return 0;
89 }
90 
91 void augmented_syscalls__setup_bpf_output(void)
92 {
93 	struct perf_cpu cpu;
94 	unsigned int i;
95 
96 	if (bpf_output == NULL)
97 		return;
98 
99 	/*
100 	 * Set up the __augmented_syscalls__ BPF map to hold for each
101 	 * CPU the bpf-output event's file descriptor.
102 	 */
103 	perf_cpu_map__for_each_cpu(cpu, i, bpf_output->core.cpus) {
104 		int mycpu = cpu.cpu;
105 
106 		bpf_map__update_elem(skel->maps.__augmented_syscalls__,
107 				     &mycpu, sizeof(mycpu),
108 				     xyarray__entry(bpf_output->core.fd, i, 0),
109 				     sizeof(__u32), BPF_ANY);
110 	}
111 }
112 
113 int augmented_syscalls__set_filter_pids(unsigned int nr, pid_t *pids)
114 {
115 	bool value = true;
116 	int err = 0;
117 
118 	if (skel == NULL)
119 		return 0;
120 
121 	for (size_t i = 0; i < nr; ++i) {
122 		err = bpf_map__update_elem(skel->maps.pids_filtered, &pids[i],
123 					   sizeof(*pids), &value, sizeof(value),
124 					   BPF_ANY);
125 		if (err)
126 			break;
127 	}
128 	return err;
129 }
130 
131 /* Trace just the target's tasks, or their processes, from their next exec if on_exec. */
132 void augmented_syscalls__set_target_pids(struct perf_thread_map *threads, bool inherit,
133 					 bool uses_tgid, bool on_exec)
134 {
135 	bool traced = !on_exec;
136 	pid_t last = -1;
137 
138 	if (skel == NULL)
139 		return;
140 
141 	skel->bss->uses_tgid = uses_tgid;
142 	/* Before seeding, so the forks of tasks already added are followed. */
143 	skel->bss->inherit = inherit;
144 	for (int i = 0; i < perf_thread_map__nr(threads); i++) {
145 		pid_t pid = perf_thread_map__pid(threads, i);
146 
147 		/* A process's threads are adjacent, add it once, skipping exited threads. */
148 		if (uses_tgid) {
149 			pid = thread_map__tgid(threads, i);
150 			if (pid < 0 || pid == last)
151 				continue;
152 			last = pid;
153 		}
154 		/* Count a lost task atomically, as the BPF programs do too. */
155 		if (bpf_map__update_elem(skel->maps.pids_to_trace, &pid, sizeof(pid),
156 					 &traced, sizeof(traced), BPF_ANY))
157 			__atomic_fetch_add(&skel->bss->lost_tasks, 1, __ATOMIC_RELAXED);
158 	}
159 	skel->bss->has_pids_to_trace = true;
160 }
161 
162 int augmented_syscalls__lost_tasks(void)
163 {
164 	if (skel == NULL)
165 		return 0;
166 
167 	return skel->bss->lost_tasks;
168 }
169 
170 int augmented_syscalls__get_map_fds(int *enter_fd, int *exit_fd, int *beauty_fd)
171 {
172 	if (skel == NULL)
173 		return -1;
174 
175 	*enter_fd = bpf_map__fd(skel->maps.syscalls_sys_enter);
176 	*exit_fd  = bpf_map__fd(skel->maps.syscalls_sys_exit);
177 	*beauty_fd = bpf_map__fd(skel->maps.beauty_map_enter);
178 
179 	if (*enter_fd < 0 || *exit_fd < 0 || *beauty_fd < 0) {
180 		pr_err("Error: failed to get syscall or beauty map fd\n");
181 		return -1;
182 	}
183 
184 	return 0;
185 }
186 
187 struct bpf_program *augmented_syscalls__unaugmented_enter(void)
188 {
189 	return skel->progs.sys_enter_unaugmented;
190 }
191 
192 struct bpf_program *augmented_syscalls__unaugmented_exit(void)
193 {
194 	return skel->progs.sys_exit_unaugmented;
195 }
196 
197 struct bpf_program *augmented_syscalls__find_by_title(const char *name)
198 {
199 	struct bpf_program *pos;
200 	const char *sec_name;
201 
202 	if (skel->obj == NULL)
203 		return NULL;
204 
205 	bpf_object__for_each_program(pos, skel->obj) {
206 		sec_name = bpf_program__section_name(pos);
207 		if (sec_name && !strcmp(sec_name, name))
208 			return pos;
209 	}
210 
211 	return NULL;
212 }
213 
214 void augmented_syscalls__cleanup(void)
215 {
216 	augmented_raw_syscalls_bpf__destroy(skel);
217 	skel = NULL;
218 }
219