xref: /openbmc/linux/samples/bpf/tracex4_user.c (revision 4a0ee788)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29811e353SAlexei Starovoitov /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
39811e353SAlexei Starovoitov  */
49811e353SAlexei Starovoitov #include <stdio.h>
59811e353SAlexei Starovoitov #include <stdlib.h>
69811e353SAlexei Starovoitov #include <signal.h>
79811e353SAlexei Starovoitov #include <unistd.h>
89811e353SAlexei Starovoitov #include <stdbool.h>
99811e353SAlexei Starovoitov #include <string.h>
109811e353SAlexei Starovoitov #include <time.h>
1155de1703SJesper Dangaard Brouer 
122bf3e2efSJakub Kicinski #include <bpf/bpf.h>
1363841bc0SDaniel T. Lee #include <bpf/libbpf.h>
149811e353SAlexei Starovoitov 
159811e353SAlexei Starovoitov struct pair {
169811e353SAlexei Starovoitov 	long long val;
179811e353SAlexei Starovoitov 	__u64 ip;
189811e353SAlexei Starovoitov };
199811e353SAlexei Starovoitov 
time_get_ns(void)209811e353SAlexei Starovoitov static __u64 time_get_ns(void)
219811e353SAlexei Starovoitov {
229811e353SAlexei Starovoitov 	struct timespec ts;
239811e353SAlexei Starovoitov 
249811e353SAlexei Starovoitov 	clock_gettime(CLOCK_MONOTONIC, &ts);
259811e353SAlexei Starovoitov 	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
269811e353SAlexei Starovoitov }
279811e353SAlexei Starovoitov 
print_old_objects(int fd)289811e353SAlexei Starovoitov static void print_old_objects(int fd)
299811e353SAlexei Starovoitov {
309811e353SAlexei Starovoitov 	long long val = time_get_ns();
319811e353SAlexei Starovoitov 	__u64 key, next_key;
329811e353SAlexei Starovoitov 	struct pair v;
339811e353SAlexei Starovoitov 
3450b796e6SKumar Kartikeya Dwivedi 	key = write(1, "\e[1;1H\e[2J", 11); /* clear screen */
359811e353SAlexei Starovoitov 
369811e353SAlexei Starovoitov 	key = -1;
3763841bc0SDaniel T. Lee 	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
3863841bc0SDaniel T. Lee 		bpf_map_lookup_elem(fd, &next_key, &v);
399811e353SAlexei Starovoitov 		key = next_key;
409811e353SAlexei Starovoitov 		if (val - v.val < 1000000000ll)
419811e353SAlexei Starovoitov 			/* object was allocated more then 1 sec ago */
429811e353SAlexei Starovoitov 			continue;
439811e353SAlexei Starovoitov 		printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
449811e353SAlexei Starovoitov 		       next_key, (val - v.val) / 1000000000ll, v.ip);
459811e353SAlexei Starovoitov 	}
469811e353SAlexei Starovoitov }
479811e353SAlexei Starovoitov 
main(int ac,char ** argv)489811e353SAlexei Starovoitov int main(int ac, char **argv)
499811e353SAlexei Starovoitov {
5063841bc0SDaniel T. Lee 	struct bpf_link *links[2];
5163841bc0SDaniel T. Lee 	struct bpf_program *prog;
5263841bc0SDaniel T. Lee 	struct bpf_object *obj;
539811e353SAlexei Starovoitov 	char filename[256];
5471135b77SDaniel T. Lee 	int map_fd, j = 0;
559811e353SAlexei Starovoitov 
56*4a0ee788SDaniel T. Lee 	snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
5763841bc0SDaniel T. Lee 	obj = bpf_object__open_file(filename, NULL);
5863841bc0SDaniel T. Lee 	if (libbpf_get_error(obj)) {
5963841bc0SDaniel T. Lee 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
6063841bc0SDaniel T. Lee 		return 0;
6163841bc0SDaniel T. Lee 	}
6263841bc0SDaniel T. Lee 
6363841bc0SDaniel T. Lee 	/* load BPF program */
6463841bc0SDaniel T. Lee 	if (bpf_object__load(obj)) {
6563841bc0SDaniel T. Lee 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
6663841bc0SDaniel T. Lee 		goto cleanup;
6763841bc0SDaniel T. Lee 	}
6863841bc0SDaniel T. Lee 
6963841bc0SDaniel T. Lee 	map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
7063841bc0SDaniel T. Lee 	if (map_fd < 0) {
7163841bc0SDaniel T. Lee 		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
7263841bc0SDaniel T. Lee 		goto cleanup;
7363841bc0SDaniel T. Lee 	}
7463841bc0SDaniel T. Lee 
7563841bc0SDaniel T. Lee 	bpf_object__for_each_program(prog, obj) {
7663841bc0SDaniel T. Lee 		links[j] = bpf_program__attach(prog);
7763841bc0SDaniel T. Lee 		if (libbpf_get_error(links[j])) {
7863841bc0SDaniel T. Lee 			fprintf(stderr, "ERROR: bpf_program__attach failed\n");
7963841bc0SDaniel T. Lee 			links[j] = NULL;
8063841bc0SDaniel T. Lee 			goto cleanup;
8163841bc0SDaniel T. Lee 		}
8263841bc0SDaniel T. Lee 		j++;
839811e353SAlexei Starovoitov 	}
849811e353SAlexei Starovoitov 
8571135b77SDaniel T. Lee 	while (1) {
8663841bc0SDaniel T. Lee 		print_old_objects(map_fd);
879811e353SAlexei Starovoitov 		sleep(1);
889811e353SAlexei Starovoitov 	}
899811e353SAlexei Starovoitov 
9063841bc0SDaniel T. Lee cleanup:
9163841bc0SDaniel T. Lee 	for (j--; j >= 0; j--)
9263841bc0SDaniel T. Lee 		bpf_link__destroy(links[j]);
9363841bc0SDaniel T. Lee 
9463841bc0SDaniel T. Lee 	bpf_object__close(obj);
959811e353SAlexei Starovoitov 	return 0;
969811e353SAlexei Starovoitov }
97