^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* Copyright (c) 2016 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <uapi/linux/bpf_perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <uapi/linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <bpf/bpf_tracing.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct key_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) char comm[TASK_COMM_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) u32 kernstack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u32 userstack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) __uint(type, BPF_MAP_TYPE_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) __type(key, struct key_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) __type(value, u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) __uint(max_entries, 10000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) } counts SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) __uint(type, BPF_MAP_TYPE_STACK_TRACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) __uint(key_size, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) __uint(max_entries, 10000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) } stackmap SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define KERN_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define USER_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) SEC("perf_event")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int bpf_prog1(struct bpf_perf_event_data *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) char time_fmt1[] = "Time Enabled: %llu, Time Running: %llu";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) char time_fmt2[] = "Get Time Failed, ErrCode: %d";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) char addr_fmt[] = "Address recorded on event: %llx";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) char fmt[] = "CPU-%d period %lld ip %llx";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u32 cpu = bpf_get_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct bpf_perf_event_value value_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct key_t key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u64 *val, one = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (ctx->sample_period < 10000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* ignore warmup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) bpf_get_current_comm(&key.comm, sizeof(key.comm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) key.kernstack = bpf_get_stackid(ctx, &stackmap, KERN_STACKID_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) key.userstack = bpf_get_stackid(ctx, &stackmap, USER_STACKID_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if ((int)key.kernstack < 0 && (int)key.userstack < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) bpf_trace_printk(fmt, sizeof(fmt), cpu, ctx->sample_period,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) PT_REGS_IP(&ctx->regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = bpf_perf_prog_read_value(ctx, (void *)&value_buf, sizeof(struct bpf_perf_event_value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) bpf_trace_printk(time_fmt1, sizeof(time_fmt1), value_buf.enabled, value_buf.running);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) bpf_trace_printk(time_fmt2, sizeof(time_fmt2), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (ctx->addr != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) bpf_trace_printk(addr_fmt, sizeof(addr_fmt), ctx->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) val = bpf_map_lookup_elem(&counts, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) (*val)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) bpf_map_update_elem(&counts, &key, &one, BPF_NOEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) char _license[] SEC("license") = "GPL";