xref: /linux/samples/bpf/trace_event_user.c (revision 905e46acd3272d04566fec49afbd7ad9e2ed9ae3)
1 /* Copyright (c) 2016 Facebook
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <poll.h>
14 #include <sys/ioctl.h>
15 #include <linux/perf_event.h>
16 #include <linux/bpf.h>
17 #include <signal.h>
18 #include <assert.h>
19 #include <errno.h>
20 #include <sys/resource.h>
21 #include "libbpf.h"
22 #include "bpf_load.h"
23 #include "perf-sys.h"
24 
25 #define SAMPLE_FREQ 50
26 
27 static bool sys_read_seen, sys_write_seen;
28 
29 static void print_ksym(__u64 addr)
30 {
31 	struct ksym *sym;
32 
33 	if (!addr)
34 		return;
35 	sym = ksym_search(addr);
36 	printf("%s;", sym->name);
37 	if (!strcmp(sym->name, "sys_read"))
38 		sys_read_seen = true;
39 	else if (!strcmp(sym->name, "sys_write"))
40 		sys_write_seen = true;
41 }
42 
43 static void print_addr(__u64 addr)
44 {
45 	if (!addr)
46 		return;
47 	printf("%llx;", addr);
48 }
49 
50 #define TASK_COMM_LEN 16
51 
52 struct key_t {
53 	char comm[TASK_COMM_LEN];
54 	__u32 kernstack;
55 	__u32 userstack;
56 };
57 
58 static void print_stack(struct key_t *key, __u64 count)
59 {
60 	__u64 ip[PERF_MAX_STACK_DEPTH] = {};
61 	static bool warned;
62 	int i;
63 
64 	printf("%3lld %s;", count, key->comm);
65 	if (bpf_map_lookup_elem(map_fd[1], &key->kernstack, ip) != 0) {
66 		printf("---;");
67 	} else {
68 		for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
69 			print_ksym(ip[i]);
70 	}
71 	printf("-;");
72 	if (bpf_map_lookup_elem(map_fd[1], &key->userstack, ip) != 0) {
73 		printf("---;");
74 	} else {
75 		for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
76 			print_addr(ip[i]);
77 	}
78 	printf("\n");
79 
80 	if (key->kernstack == -EEXIST && !warned) {
81 		printf("stackmap collisions seen. Consider increasing size\n");
82 		warned = true;
83 	} else if ((int)key->kernstack < 0 && (int)key->userstack < 0) {
84 		printf("err stackid %d %d\n", key->kernstack, key->userstack);
85 	}
86 }
87 
88 static void int_exit(int sig)
89 {
90 	kill(0, SIGKILL);
91 	exit(0);
92 }
93 
94 static void print_stacks(void)
95 {
96 	struct key_t key = {}, next_key;
97 	__u64 value;
98 	__u32 stackid = 0, next_id;
99 	int fd = map_fd[0], stack_map = map_fd[1];
100 
101 	sys_read_seen = sys_write_seen = false;
102 	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
103 		bpf_map_lookup_elem(fd, &next_key, &value);
104 		print_stack(&next_key, value);
105 		bpf_map_delete_elem(fd, &next_key);
106 		key = next_key;
107 	}
108 
109 	if (!sys_read_seen || !sys_write_seen) {
110 		printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
111 		int_exit(0);
112 	}
113 
114 	/* clear stack map */
115 	while (bpf_map_get_next_key(stack_map, &stackid, &next_id) == 0) {
116 		bpf_map_delete_elem(stack_map, &next_id);
117 		stackid = next_id;
118 	}
119 }
120 
121 static void test_perf_event_all_cpu(struct perf_event_attr *attr)
122 {
123 	int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
124 	int *pmu_fd = malloc(nr_cpus * sizeof(int));
125 	int i;
126 
127 	/* open perf_event on all cpus */
128 	for (i = 0; i < nr_cpus; i++) {
129 		pmu_fd[i] = sys_perf_event_open(attr, -1, i, -1, 0);
130 		if (pmu_fd[i] < 0) {
131 			printf("sys_perf_event_open failed\n");
132 			goto all_cpu_err;
133 		}
134 		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
135 		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
136 	}
137 	system("dd if=/dev/zero of=/dev/null count=5000k");
138 	print_stacks();
139 all_cpu_err:
140 	for (i--; i >= 0; i--)
141 		close(pmu_fd[i]);
142 	free(pmu_fd);
143 }
144 
145 static void test_perf_event_task(struct perf_event_attr *attr)
146 {
147 	int pmu_fd;
148 
149 	/* open task bound event */
150 	pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
151 	if (pmu_fd < 0) {
152 		printf("sys_perf_event_open failed\n");
153 		return;
154 	}
155 	assert(ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
156 	assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0) == 0);
157 	system("dd if=/dev/zero of=/dev/null count=5000k");
158 	print_stacks();
159 	close(pmu_fd);
160 }
161 
162 static void test_bpf_perf_event(void)
163 {
164 	struct perf_event_attr attr_type_hw = {
165 		.sample_freq = SAMPLE_FREQ,
166 		.freq = 1,
167 		.type = PERF_TYPE_HARDWARE,
168 		.config = PERF_COUNT_HW_CPU_CYCLES,
169 		.inherit = 1,
170 	};
171 	struct perf_event_attr attr_type_sw = {
172 		.sample_freq = SAMPLE_FREQ,
173 		.freq = 1,
174 		.type = PERF_TYPE_SOFTWARE,
175 		.config = PERF_COUNT_SW_CPU_CLOCK,
176 		.inherit = 1,
177 	};
178 
179 	test_perf_event_all_cpu(&attr_type_hw);
180 	test_perf_event_task(&attr_type_hw);
181 	test_perf_event_all_cpu(&attr_type_sw);
182 	test_perf_event_task(&attr_type_sw);
183 }
184 
185 
186 int main(int argc, char **argv)
187 {
188 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
189 	char filename[256];
190 
191 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
192 	setrlimit(RLIMIT_MEMLOCK, &r);
193 
194 	signal(SIGINT, int_exit);
195 
196 	if (load_kallsyms()) {
197 		printf("failed to process /proc/kallsyms\n");
198 		return 1;
199 	}
200 
201 	if (load_bpf_file(filename)) {
202 		printf("%s", bpf_log_buf);
203 		return 2;
204 	}
205 
206 	if (fork() == 0) {
207 		read_trace_pipe();
208 		return 0;
209 	}
210 	test_bpf_perf_event();
211 
212 	int_exit(0);
213 	return 0;
214 }
215