• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2021, Oracle and/or its affiliates. */
3 
4 /* maximum number of different functions we can trace at once */
5 #define MAX_FUNC_TRACES			64
6 
7 enum arg {
8 	KSNOOP_ARG1,
9 	KSNOOP_ARG2,
10 	KSNOOP_ARG3,
11 	KSNOOP_ARG4,
12 	KSNOOP_ARG5,
13 	KSNOOP_RETURN
14 };
15 
16 /* we choose "return" as the name for the returned value because as
17  * a C keyword it can't clash with a function entry parameter.
18  */
19 #define KSNOOP_RETURN_NAME		"return"
20 
21 /* if we can't get a type id for a type (such as module-specific type)
22  * mark it as KSNOOP_ID_UNKNOWN since BTF lookup in bpf_snprintf_btf()
23  * will fail and the data will be simply displayed as a __u64.
24  */
25 #define KSNOOP_ID_UNKNOWN		0xffffffff
26 
27 #define MAX_NAME			96
28 #define MAX_STR				256
29 #define MAX_PATH			512
30 #define MAX_VALUES			6
31 #define MAX_ARGS			(MAX_VALUES - 1)
32 #define KSNOOP_F_PTR			0x1	/* value is a pointer */
33 #define KSNOOP_F_MEMBER			0x2	/* member reference */
34 #define KSNOOP_F_ENTRY			0x4
35 #define KSNOOP_F_RETURN			0x8
36 #define KSNOOP_F_CUSTOM			0x10	/* custom trace */
37 #define KSNOOP_F_STASH			0x20	/* store values on entry,
38 						 * no perf events.
39 						 */
40 #define KSNOOP_F_STASHED		0x40	/* values stored on entry */
41 
42 #define KSNOOP_F_PREDICATE_EQ		0x100
43 #define KSNOOP_F_PREDICATE_NOTEQ	0x200
44 #define KSNOOP_F_PREDICATE_GT		0x400
45 #define KSNOOP_F_PREDICATE_LT		0x800
46 
47 #define KSNOOP_F_PREDICATE_MASK		(KSNOOP_F_PREDICATE_EQ | \
48 					 KSNOOP_F_PREDICATE_NOTEQ | \
49 					 KSNOOP_F_PREDICATE_GT | \
50 					 KSNOOP_F_PREDICATE_LT)
51 
52 /* for kprobes, entry is function IP + sizeof(kprobe_opcode_t),
53  * subtract in BPF prog context to get fn address.
54  */
55 #ifdef __TARGET_ARCH_x86
56 #define KSNOOP_IP_FIX(ip)		(ip - sizeof(kprobe_opcode_t))
57 #else
58 #define KSNOOP_IP_FIX(ip)		ip
59 #endif
60 
61 struct value {
62 	char name[MAX_STR];
63 	enum arg base_arg;
64 	__u32 offset;
65 	__u32 size;
66 	__u64 type_id;
67 	__u64 flags;
68 	__u64 predicate_value;
69 };
70 
71 struct func {
72 	char name[MAX_NAME];
73 	char mod[MAX_NAME];
74 	__s32 id;
75 	__u8 nr_args;
76 	__u64 ip;
77 	struct value args[MAX_VALUES];
78 };
79 
80 #define MAX_TRACES MAX_VALUES
81 
82 #define MAX_TRACE_DATA	2048
83 
84 struct trace_data {
85 	__u64 raw_value;
86 	__u32 err_type_id;	/* type id we can't dereference */
87 	int err;
88 	__u32 buf_offset;
89 	__u16 buf_len;
90 };
91 
92 #define MAX_TRACE_BUF	(MAX_TRACES * MAX_TRACE_DATA)
93 
94 struct trace {
95 	/* initial values are readonly in tracing context */
96 	struct btf *btf;
97 	struct btf_dump *dump;
98 	struct func func;
99 	__u8 nr_traces;
100 	__u32 filter_pid;
101 	__u64 prev_ip; /* these are used in stack-mode tracing */
102 	__u64 next_ip;
103 	struct value traces[MAX_TRACES];
104 	__u64 flags;
105 	/* values below this point are set or modified in tracing context */
106 	__u64 task;
107 	__u32 pid;
108 	__u32 cpu;
109 	__u64 time;
110 	__u64 data_flags;
111 	struct trace_data trace_data[MAX_TRACES];
112 	__u16 buf_len;
113 	char buf[MAX_TRACE_BUF];
114 	char buf_end[0];
115 	struct bpf_link *links[2];
116 };
117 
118 #define PAGES_DEFAULT	16
119 
base_arg_is_entry(enum arg base_arg)120 static inline int base_arg_is_entry(enum arg base_arg)
121 {
122 	return base_arg != KSNOOP_RETURN;
123 }
124