18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* Copyright (c) 2016 Facebook 38c2ecf20Sopenharmony_ci */ 48c2ecf20Sopenharmony_ci#include <stdio.h> 58c2ecf20Sopenharmony_ci#include <unistd.h> 68c2ecf20Sopenharmony_ci#include <stdlib.h> 78c2ecf20Sopenharmony_ci#include <signal.h> 88c2ecf20Sopenharmony_ci#include <linux/perf_event.h> 98c2ecf20Sopenharmony_ci#include <errno.h> 108c2ecf20Sopenharmony_ci#include <stdbool.h> 118c2ecf20Sopenharmony_ci#include <sys/resource.h> 128c2ecf20Sopenharmony_ci#include <bpf/libbpf.h> 138c2ecf20Sopenharmony_ci#include <bpf/bpf.h> 148c2ecf20Sopenharmony_ci#include "trace_helpers.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define PRINT_RAW_ADDR 0 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* counts, stackmap */ 198c2ecf20Sopenharmony_cistatic int map_fd[2]; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic void print_ksym(__u64 addr) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci struct ksym *sym; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci if (!addr) 268c2ecf20Sopenharmony_ci return; 278c2ecf20Sopenharmony_ci sym = ksym_search(addr); 288c2ecf20Sopenharmony_ci if (!sym) { 298c2ecf20Sopenharmony_ci printf("ksym not found. Is kallsyms loaded?\n"); 308c2ecf20Sopenharmony_ci return; 318c2ecf20Sopenharmony_ci } 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci if (PRINT_RAW_ADDR) 348c2ecf20Sopenharmony_ci printf("%s/%llx;", sym->name, addr); 358c2ecf20Sopenharmony_ci else 368c2ecf20Sopenharmony_ci printf("%s;", sym->name); 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define TASK_COMM_LEN 16 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistruct key_t { 428c2ecf20Sopenharmony_ci char waker[TASK_COMM_LEN]; 438c2ecf20Sopenharmony_ci char target[TASK_COMM_LEN]; 448c2ecf20Sopenharmony_ci __u32 wret; 458c2ecf20Sopenharmony_ci __u32 tret; 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic void print_stack(struct key_t *key, __u64 count) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci __u64 ip[PERF_MAX_STACK_DEPTH] = {}; 518c2ecf20Sopenharmony_ci static bool warned; 528c2ecf20Sopenharmony_ci int i; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci printf("%s;", key->target); 558c2ecf20Sopenharmony_ci if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) { 568c2ecf20Sopenharmony_ci printf("---;"); 578c2ecf20Sopenharmony_ci } else { 588c2ecf20Sopenharmony_ci for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--) 598c2ecf20Sopenharmony_ci print_ksym(ip[i]); 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci printf("-;"); 628c2ecf20Sopenharmony_ci if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) { 638c2ecf20Sopenharmony_ci printf("---;"); 648c2ecf20Sopenharmony_ci } else { 658c2ecf20Sopenharmony_ci for (i = 0; i < PERF_MAX_STACK_DEPTH; i++) 668c2ecf20Sopenharmony_ci print_ksym(ip[i]); 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci printf(";%s %lld\n", key->waker, count); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) { 718c2ecf20Sopenharmony_ci printf("stackmap collisions seen. Consider increasing size\n"); 728c2ecf20Sopenharmony_ci warned = true; 738c2ecf20Sopenharmony_ci } else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) { 748c2ecf20Sopenharmony_ci printf("err stackid %d %d\n", key->tret, key->wret); 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic void print_stacks(int fd) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci struct key_t key = {}, next_key; 818c2ecf20Sopenharmony_ci __u64 value; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { 848c2ecf20Sopenharmony_ci bpf_map_lookup_elem(fd, &next_key, &value); 858c2ecf20Sopenharmony_ci print_stack(&next_key, value); 868c2ecf20Sopenharmony_ci key = next_key; 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic void int_exit(int sig) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci print_stacks(map_fd[0]); 938c2ecf20Sopenharmony_ci exit(0); 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ciint main(int argc, char **argv) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 998c2ecf20Sopenharmony_ci struct bpf_object *obj = NULL; 1008c2ecf20Sopenharmony_ci struct bpf_link *links[2]; 1018c2ecf20Sopenharmony_ci struct bpf_program *prog; 1028c2ecf20Sopenharmony_ci int delay = 1, i = 0; 1038c2ecf20Sopenharmony_ci char filename[256]; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (setrlimit(RLIMIT_MEMLOCK, &r)) { 1068c2ecf20Sopenharmony_ci perror("setrlimit(RLIMIT_MEMLOCK)"); 1078c2ecf20Sopenharmony_ci return 1; 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci if (load_kallsyms()) { 1118c2ecf20Sopenharmony_ci printf("failed to process /proc/kallsyms\n"); 1128c2ecf20Sopenharmony_ci return 2; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 1168c2ecf20Sopenharmony_ci obj = bpf_object__open_file(filename, NULL); 1178c2ecf20Sopenharmony_ci if (libbpf_get_error(obj)) { 1188c2ecf20Sopenharmony_ci fprintf(stderr, "ERROR: opening BPF object file failed\n"); 1198c2ecf20Sopenharmony_ci obj = NULL; 1208c2ecf20Sopenharmony_ci goto cleanup; 1218c2ecf20Sopenharmony_ci } 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* load BPF program */ 1248c2ecf20Sopenharmony_ci if (bpf_object__load(obj)) { 1258c2ecf20Sopenharmony_ci fprintf(stderr, "ERROR: loading BPF object file failed\n"); 1268c2ecf20Sopenharmony_ci goto cleanup; 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts"); 1308c2ecf20Sopenharmony_ci map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap"); 1318c2ecf20Sopenharmony_ci if (map_fd[0] < 0 || map_fd[1] < 0) { 1328c2ecf20Sopenharmony_ci fprintf(stderr, "ERROR: finding a map in obj file failed\n"); 1338c2ecf20Sopenharmony_ci goto cleanup; 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci signal(SIGINT, int_exit); 1378c2ecf20Sopenharmony_ci signal(SIGTERM, int_exit); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci bpf_object__for_each_program(prog, obj) { 1408c2ecf20Sopenharmony_ci links[i] = bpf_program__attach(prog); 1418c2ecf20Sopenharmony_ci if (libbpf_get_error(links[i])) { 1428c2ecf20Sopenharmony_ci fprintf(stderr, "ERROR: bpf_program__attach failed\n"); 1438c2ecf20Sopenharmony_ci links[i] = NULL; 1448c2ecf20Sopenharmony_ci goto cleanup; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci i++; 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci if (argc > 1) 1508c2ecf20Sopenharmony_ci delay = atoi(argv[1]); 1518c2ecf20Sopenharmony_ci sleep(delay); 1528c2ecf20Sopenharmony_ci print_stacks(map_fd[0]); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_cicleanup: 1558c2ecf20Sopenharmony_ci for (i--; i >= 0; i--) 1568c2ecf20Sopenharmony_ci bpf_link__destroy(links[i]); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci bpf_object__close(obj); 1598c2ecf20Sopenharmony_ci return 0; 1608c2ecf20Sopenharmony_ci} 161