blob: 5734cfdaaacb8215b7c8fab2c60eaba0f0951619 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* Copyright (c) 2016 Facebook
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 */
4#include <stdio.h>
5#include <unistd.h>
6#include <stdlib.h>
7#include <signal.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008#include <linux/perf_event.h>
9#include <errno.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010#include <stdbool.h>
11#include <sys/resource.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020012#include <bpf/libbpf.h>
13#include <bpf/bpf.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include "trace_helpers.h"
15
16#define PRINT_RAW_ADDR 0
17
Olivier Deprez157378f2022-04-04 15:47:50 +020018/* counts, stackmap */
19static int map_fd[2];
20
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021static void print_ksym(__u64 addr)
22{
23 struct ksym *sym;
24
25 if (!addr)
26 return;
27 sym = ksym_search(addr);
David Brazdil0f672f62019-12-10 10:32:29 +000028 if (!sym) {
29 printf("ksym not found. Is kallsyms loaded?\n");
30 return;
31 }
32
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033 if (PRINT_RAW_ADDR)
34 printf("%s/%llx;", sym->name, addr);
35 else
36 printf("%s;", sym->name);
37}
38
39#define TASK_COMM_LEN 16
40
41struct key_t {
42 char waker[TASK_COMM_LEN];
43 char target[TASK_COMM_LEN];
44 __u32 wret;
45 __u32 tret;
46};
47
48static void print_stack(struct key_t *key, __u64 count)
49{
50 __u64 ip[PERF_MAX_STACK_DEPTH] = {};
51 static bool warned;
52 int i;
53
54 printf("%s;", key->target);
Olivier Deprez157378f2022-04-04 15:47:50 +020055 if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056 printf("---;");
57 } else {
58 for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
59 print_ksym(ip[i]);
60 }
61 printf("-;");
Olivier Deprez157378f2022-04-04 15:47:50 +020062 if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 printf("---;");
64 } else {
65 for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
66 print_ksym(ip[i]);
67 }
68 printf(";%s %lld\n", key->waker, count);
69
70 if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) {
71 printf("stackmap collisions seen. Consider increasing size\n");
72 warned = true;
73 } else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) {
74 printf("err stackid %d %d\n", key->tret, key->wret);
75 }
76}
77
78static void print_stacks(int fd)
79{
80 struct key_t key = {}, next_key;
81 __u64 value;
82
83 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
84 bpf_map_lookup_elem(fd, &next_key, &value);
85 print_stack(&next_key, value);
86 key = next_key;
87 }
88}
89
90static void int_exit(int sig)
91{
92 print_stacks(map_fd[0]);
93 exit(0);
94}
95
96int main(int argc, char **argv)
97{
98 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Olivier Deprez157378f2022-04-04 15:47:50 +020099 struct bpf_object *obj = NULL;
100 struct bpf_link *links[2];
101 struct bpf_program *prog;
102 int delay = 1, i = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103 char filename[256];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104
Olivier Deprez157378f2022-04-04 15:47:50 +0200105 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
106 perror("setrlimit(RLIMIT_MEMLOCK)");
107 return 1;
108 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
110 if (load_kallsyms()) {
111 printf("failed to process /proc/kallsyms\n");
112 return 2;
113 }
114
Olivier Deprez157378f2022-04-04 15:47:50 +0200115 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
116 obj = bpf_object__open_file(filename, NULL);
117 if (libbpf_get_error(obj)) {
118 fprintf(stderr, "ERROR: opening BPF object file failed\n");
119 obj = NULL;
120 goto cleanup;
121 }
122
123 /* load BPF program */
124 if (bpf_object__load(obj)) {
125 fprintf(stderr, "ERROR: loading BPF object file failed\n");
126 goto cleanup;
127 }
128
129 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
130 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
131 if (map_fd[0] < 0 || map_fd[1] < 0) {
132 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
133 goto cleanup;
134 }
135
136 signal(SIGINT, int_exit);
137 signal(SIGTERM, int_exit);
138
139 bpf_object__for_each_program(prog, obj) {
140 links[i] = bpf_program__attach(prog);
141 if (libbpf_get_error(links[i])) {
142 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
143 links[i] = NULL;
144 goto cleanup;
145 }
146 i++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147 }
148
149 if (argc > 1)
150 delay = atoi(argv[1]);
151 sleep(delay);
152 print_stacks(map_fd[0]);
153
Olivier Deprez157378f2022-04-04 15:47:50 +0200154cleanup:
155 for (i--; i >= 0; i--)
156 bpf_link__destroy(links[i]);
157
158 bpf_object__close(obj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000159 return 0;
160}