0001
0002
0003
0004 #include <stdio.h>
0005 #include <unistd.h>
0006 #include <stdlib.h>
0007 #include <signal.h>
0008 #include <linux/perf_event.h>
0009 #include <errno.h>
0010 #include <stdbool.h>
0011 #include <bpf/libbpf.h>
0012 #include <bpf/bpf.h>
0013 #include "trace_helpers.h"
0014
0015 #define PRINT_RAW_ADDR 0
0016
0017
0018 static int map_fd[2];
0019
0020 static void print_ksym(__u64 addr)
0021 {
0022 struct ksym *sym;
0023
0024 if (!addr)
0025 return;
0026 sym = ksym_search(addr);
0027 if (!sym) {
0028 printf("ksym not found. Is kallsyms loaded?\n");
0029 return;
0030 }
0031
0032 if (PRINT_RAW_ADDR)
0033 printf("%s/%llx;", sym->name, addr);
0034 else
0035 printf("%s;", sym->name);
0036 }
0037
0038 #define TASK_COMM_LEN 16
0039
0040 struct key_t {
0041 char waker[TASK_COMM_LEN];
0042 char target[TASK_COMM_LEN];
0043 __u32 wret;
0044 __u32 tret;
0045 };
0046
0047 static void print_stack(struct key_t *key, __u64 count)
0048 {
0049 __u64 ip[PERF_MAX_STACK_DEPTH] = {};
0050 static bool warned;
0051 int i;
0052
0053 printf("%s;", key->target);
0054 if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) {
0055 printf("---;");
0056 } else {
0057 for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
0058 print_ksym(ip[i]);
0059 }
0060 printf("-;");
0061 if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) {
0062 printf("---;");
0063 } else {
0064 for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
0065 print_ksym(ip[i]);
0066 }
0067 printf(";%s %lld\n", key->waker, count);
0068
0069 if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) {
0070 printf("stackmap collisions seen. Consider increasing size\n");
0071 warned = true;
0072 } else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) {
0073 printf("err stackid %d %d\n", key->tret, key->wret);
0074 }
0075 }
0076
0077 static void print_stacks(int fd)
0078 {
0079 struct key_t key = {}, next_key;
0080 __u64 value;
0081
0082 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
0083 bpf_map_lookup_elem(fd, &next_key, &value);
0084 print_stack(&next_key, value);
0085 key = next_key;
0086 }
0087 }
0088
0089 static void int_exit(int sig)
0090 {
0091 print_stacks(map_fd[0]);
0092 exit(0);
0093 }
0094
0095 int main(int argc, char **argv)
0096 {
0097 struct bpf_object *obj = NULL;
0098 struct bpf_link *links[2];
0099 struct bpf_program *prog;
0100 int delay = 1, i = 0;
0101 char filename[256];
0102
0103 if (load_kallsyms()) {
0104 printf("failed to process /proc/kallsyms\n");
0105 return 2;
0106 }
0107
0108 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0109 obj = bpf_object__open_file(filename, NULL);
0110 if (libbpf_get_error(obj)) {
0111 fprintf(stderr, "ERROR: opening BPF object file failed\n");
0112 obj = NULL;
0113 goto cleanup;
0114 }
0115
0116
0117 if (bpf_object__load(obj)) {
0118 fprintf(stderr, "ERROR: loading BPF object file failed\n");
0119 goto cleanup;
0120 }
0121
0122 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
0123 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
0124 if (map_fd[0] < 0 || map_fd[1] < 0) {
0125 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
0126 goto cleanup;
0127 }
0128
0129 signal(SIGINT, int_exit);
0130 signal(SIGTERM, int_exit);
0131
0132 bpf_object__for_each_program(prog, obj) {
0133 links[i] = bpf_program__attach(prog);
0134 if (libbpf_get_error(links[i])) {
0135 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
0136 links[i] = NULL;
0137 goto cleanup;
0138 }
0139 i++;
0140 }
0141
0142 if (argc > 1)
0143 delay = atoi(argv[1]);
0144 sleep(delay);
0145 print_stacks(map_fd[0]);
0146
0147 cleanup:
0148 for (i--; i >= 0; i--)
0149 bpf_link__destroy(links[i]);
0150
0151 bpf_object__close(obj);
0152 return 0;
0153 }