summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/perf_event_stackmap.c
blob: f793280a323809231e98095b9258cdebe0ce24c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2020 Facebook
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>

#ifndef PERF_MAX_STACK_DEPTH
#define PERF_MAX_STACK_DEPTH         127
#endif

typedef __u64 stack_trace_t[PERF_MAX_STACK_DEPTH];
struct {
	__uint(type, BPF_MAP_TYPE_STACK_TRACE);
	__uint(max_entries, 16384);
	__type(key, __u32);
	__type(value, stack_trace_t);
} stackmap SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
	__uint(max_entries, 1);
	__type(key, __u32);
	__type(value, stack_trace_t);
} stackdata_map SEC(".maps");

long stackid_kernel = 1;
long stackid_user = 1;
long stack_kernel = 1;
long stack_user = 1;

SEC("perf_event")
int oncpu(void *ctx)
{
	stack_trace_t *trace;
	__u32 key = 0;
	long val;

	val = bpf_get_stackid(ctx, &stackmap, 0);
	if (val >= 0)
		stackid_kernel = 2;
	val = bpf_get_stackid(ctx, &stackmap, BPF_F_USER_STACK);
	if (val >= 0)
		stackid_user = 2;

	trace = bpf_map_lookup_elem(&stackdata_map, &key);
	if (!trace)
		return 0;

	val = bpf_get_stack(ctx, trace, sizeof(stack_trace_t), 0);
	if (val > 0)
		stack_kernel = 2;

	val = bpf_get_stack(ctx, trace, sizeof(stack_trace_t), BPF_F_USER_STACK);
	if (val > 0)
		stack_user = 2;

	return 0;
}

char LICENSE[] SEC("license") = "GPL";