18c2ecf20Sopenharmony_ci/* Copyright (c) 2016 Facebook
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
48c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public
58c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/ptrace.h>
88c2ecf20Sopenharmony_ci#include <linux/version.h>
98c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
108c2ecf20Sopenharmony_ci#include <uapi/linux/bpf_perf_event.h>
118c2ecf20Sopenharmony_ci#include <uapi/linux/perf_event.h>
128c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
138c2ecf20Sopenharmony_ci#include <bpf/bpf_tracing.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct key_t {
168c2ecf20Sopenharmony_ci	char comm[TASK_COMM_LEN];
178c2ecf20Sopenharmony_ci	u32 kernstack;
188c2ecf20Sopenharmony_ci	u32 userstack;
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct {
228c2ecf20Sopenharmony_ci	__uint(type, BPF_MAP_TYPE_HASH);
238c2ecf20Sopenharmony_ci	__type(key, struct key_t);
248c2ecf20Sopenharmony_ci	__type(value, u64);
258c2ecf20Sopenharmony_ci	__uint(max_entries, 10000);
268c2ecf20Sopenharmony_ci} counts SEC(".maps");
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct {
298c2ecf20Sopenharmony_ci	__uint(type, BPF_MAP_TYPE_STACK_TRACE);
308c2ecf20Sopenharmony_ci	__uint(key_size, sizeof(u32));
318c2ecf20Sopenharmony_ci	__uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
328c2ecf20Sopenharmony_ci	__uint(max_entries, 10000);
338c2ecf20Sopenharmony_ci} stackmap SEC(".maps");
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define KERN_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
368c2ecf20Sopenharmony_ci#define USER_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciSEC("perf_event")
398c2ecf20Sopenharmony_ciint bpf_prog1(struct bpf_perf_event_data *ctx)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	char time_fmt1[] = "Time Enabled: %llu, Time Running: %llu";
428c2ecf20Sopenharmony_ci	char time_fmt2[] = "Get Time Failed, ErrCode: %d";
438c2ecf20Sopenharmony_ci	char addr_fmt[] = "Address recorded on event: %llx";
448c2ecf20Sopenharmony_ci	char fmt[] = "CPU-%d period %lld ip %llx";
458c2ecf20Sopenharmony_ci	u32 cpu = bpf_get_smp_processor_id();
468c2ecf20Sopenharmony_ci	struct bpf_perf_event_value value_buf;
478c2ecf20Sopenharmony_ci	struct key_t key;
488c2ecf20Sopenharmony_ci	u64 *val, one = 1;
498c2ecf20Sopenharmony_ci	int ret;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	if (ctx->sample_period < 10000)
528c2ecf20Sopenharmony_ci		/* ignore warmup */
538c2ecf20Sopenharmony_ci		return 0;
548c2ecf20Sopenharmony_ci	bpf_get_current_comm(&key.comm, sizeof(key.comm));
558c2ecf20Sopenharmony_ci	key.kernstack = bpf_get_stackid(ctx, &stackmap, KERN_STACKID_FLAGS);
568c2ecf20Sopenharmony_ci	key.userstack = bpf_get_stackid(ctx, &stackmap, USER_STACKID_FLAGS);
578c2ecf20Sopenharmony_ci	if ((int)key.kernstack < 0 && (int)key.userstack < 0) {
588c2ecf20Sopenharmony_ci		bpf_trace_printk(fmt, sizeof(fmt), cpu, ctx->sample_period,
598c2ecf20Sopenharmony_ci				 PT_REGS_IP(&ctx->regs));
608c2ecf20Sopenharmony_ci		return 0;
618c2ecf20Sopenharmony_ci	}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	ret = bpf_perf_prog_read_value(ctx, (void *)&value_buf, sizeof(struct bpf_perf_event_value));
648c2ecf20Sopenharmony_ci	if (!ret)
658c2ecf20Sopenharmony_ci	  bpf_trace_printk(time_fmt1, sizeof(time_fmt1), value_buf.enabled, value_buf.running);
668c2ecf20Sopenharmony_ci	else
678c2ecf20Sopenharmony_ci	  bpf_trace_printk(time_fmt2, sizeof(time_fmt2), ret);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (ctx->addr != 0)
708c2ecf20Sopenharmony_ci	  bpf_trace_printk(addr_fmt, sizeof(addr_fmt), ctx->addr);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	val = bpf_map_lookup_elem(&counts, &key);
738c2ecf20Sopenharmony_ci	if (val)
748c2ecf20Sopenharmony_ci		(*val)++;
758c2ecf20Sopenharmony_ci	else
768c2ecf20Sopenharmony_ci		bpf_map_update_elem(&counts, &key, &one, BPF_NOEXIST);
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
81