xref: /linux/tools/testing/selftests/bpf/progs/test_attach_probe.c (revision ba95c7452439756d4f6dceb5a188b7c31dbbe5b6)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Facebook
3 
4 #include <linux/ptrace.h>
5 #include <linux/bpf.h>
6 #include "bpf_helpers.h"
7 
8 struct {
9 	int type;
10 	int max_entries;
11 	int *key;
12 	int *value;
13 } results_map SEC(".maps") = {
14 	.type = BPF_MAP_TYPE_ARRAY,
15 	.max_entries = 4,
16 };
17 
18 SEC("kprobe/sys_nanosleep")
19 int handle_sys_nanosleep_entry(struct pt_regs *ctx)
20 {
21 	const int key = 0, value = 1;
22 
23 	bpf_map_update_elem(&results_map, &key, &value, 0);
24 	return 0;
25 }
26 
27 SEC("kretprobe/sys_nanosleep")
28 int handle_sys_getpid_return(struct pt_regs *ctx)
29 {
30 	const int key = 1, value = 2;
31 
32 	bpf_map_update_elem(&results_map, &key, &value, 0);
33 	return 0;
34 }
35 
36 SEC("uprobe/trigger_func")
37 int handle_uprobe_entry(struct pt_regs *ctx)
38 {
39 	const int key = 2, value = 3;
40 
41 	bpf_map_update_elem(&results_map, &key, &value, 0);
42 	return 0;
43 }
44 
45 SEC("uretprobe/trigger_func")
46 int handle_uprobe_return(struct pt_regs *ctx)
47 {
48 	const int key = 3, value = 4;
49 
50 	bpf_map_update_elem(&results_map, &key, &value, 0);
51 	return 0;
52 }
53 
54 char _license[] SEC("license") = "GPL";
55 __u32 _version SEC("version") = 1;
56