xref: /openbmc/linux/kernel/trace/trace_probe.h (revision 747774d6b018ca02493fd3f321624dfce749da61)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common header file for probe-based Dynamic events.
4  *
5  * This code was copied from kernel/trace/trace_kprobe.h written by
6  * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
7  *
8  * Updates to make this generic:
9  * Copyright (C) IBM Corporation, 2010-2011
10  * Author:     Srikar Dronamraju
11  */
12 
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/smp.h>
16 #include <linux/tracefs.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
20 #include <linux/ptrace.h>
21 #include <linux/perf_event.h>
22 #include <linux/kprobes.h>
23 #include <linux/stringify.h>
24 #include <linux/limits.h>
25 #include <linux/uaccess.h>
26 #include <linux/bitops.h>
27 #include <asm/bitsperlong.h>
28 
29 #include "trace.h"
30 #include "trace_output.h"
31 
32 #define MAX_TRACE_ARGS		128
33 #define MAX_ARGSTR_LEN		63
34 #define MAX_ARRAY_LEN		64
35 #define MAX_ARG_NAME_LEN	32
36 #define MAX_STRING_SIZE		PATH_MAX
37 
38 /* Reserved field names */
39 #define FIELD_STRING_IP		"__probe_ip"
40 #define FIELD_STRING_RETIP	"__probe_ret_ip"
41 #define FIELD_STRING_FUNC	"__probe_func"
42 
43 #undef DEFINE_FIELD
44 #define DEFINE_FIELD(type, item, name, is_signed)			\
45 	do {								\
46 		ret = trace_define_field(event_call, #type, name,	\
47 					 offsetof(typeof(field), item),	\
48 					 sizeof(field.item), is_signed, \
49 					 FILTER_OTHER);			\
50 		if (ret)						\
51 			return ret;					\
52 	} while (0)
53 
54 
55 /* Flags for trace_probe */
56 #define TP_FLAG_TRACE		1
57 #define TP_FLAG_PROFILE		2
58 #define TP_FLAG_REGISTERED	4
59 
60 /* data_loc: data location, compatible with u32 */
61 #define make_data_loc(len, offs)	\
62 	(((u32)(len) << 16) | ((u32)(offs) & 0xffff))
63 #define get_loc_len(dl)		((u32)(dl) >> 16)
64 #define get_loc_offs(dl)	((u32)(dl) & 0xffff)
65 
66 static nokprobe_inline void *get_loc_data(u32 *dl, void *ent)
67 {
68 	return (u8 *)ent + get_loc_offs(*dl);
69 }
70 
71 static nokprobe_inline u32 update_data_loc(u32 loc, int consumed)
72 {
73 	u32 maxlen = get_loc_len(loc);
74 	u32 offset = get_loc_offs(loc);
75 
76 	return make_data_loc(maxlen - consumed, offset + consumed);
77 }
78 
79 /* Printing function type */
80 typedef int (*print_type_func_t)(struct trace_seq *, void *, void *);
81 
82 enum fetch_op {
83 	FETCH_OP_NOP = 0,
84 	// Stage 1 (load) ops
85 	FETCH_OP_REG,		/* Register : .param = offset */
86 	FETCH_OP_STACK,		/* Stack : .param = index */
87 	FETCH_OP_STACKP,	/* Stack pointer */
88 	FETCH_OP_RETVAL,	/* Return value */
89 	FETCH_OP_IMM,		/* Immediate : .immediate */
90 	FETCH_OP_COMM,		/* Current comm */
91 	FETCH_OP_ARG,		/* Function argument : .param */
92 	FETCH_OP_FOFFS,		/* File offset: .immediate */
93 	// Stage 2 (dereference) op
94 	FETCH_OP_DEREF,		/* Dereference: .offset */
95 	FETCH_OP_UDEREF,	/* User-space Dereference: .offset */
96 	// Stage 3 (store) ops
97 	FETCH_OP_ST_RAW,	/* Raw: .size */
98 	FETCH_OP_ST_MEM,	/* Mem: .offset, .size */
99 	FETCH_OP_ST_UMEM,	/* Mem: .offset, .size */
100 	FETCH_OP_ST_STRING,	/* String: .offset, .size */
101 	FETCH_OP_ST_USTRING,	/* User String: .offset, .size */
102 	// Stage 4 (modify) op
103 	FETCH_OP_MOD_BF,	/* Bitfield: .basesize, .lshift, .rshift */
104 	// Stage 5 (loop) op
105 	FETCH_OP_LP_ARRAY,	/* Array: .param = loop count */
106 	FETCH_OP_END,
107 	FETCH_NOP_SYMBOL,	/* Unresolved Symbol holder */
108 };
109 
110 struct fetch_insn {
111 	enum fetch_op op;
112 	union {
113 		unsigned int param;
114 		struct {
115 			unsigned int size;
116 			int offset;
117 		};
118 		struct {
119 			unsigned char basesize;
120 			unsigned char lshift;
121 			unsigned char rshift;
122 		};
123 		unsigned long immediate;
124 		void *data;
125 	};
126 };
127 
128 /* fetch + deref*N + store + mod + end <= 16, this allows N=12, enough */
129 #define FETCH_INSN_MAX	16
130 #define FETCH_TOKEN_COMM	(-ECOMM)
131 
132 /* Fetch type information table */
133 struct fetch_type {
134 	const char		*name;		/* Name of type */
135 	size_t			size;		/* Byte size of type */
136 	int			is_signed;	/* Signed flag */
137 	print_type_func_t	print;		/* Print functions */
138 	const char		*fmt;		/* Fromat string */
139 	const char		*fmttype;	/* Name in format file */
140 };
141 
142 /* For defining macros, define string/string_size types */
143 typedef u32 string;
144 typedef u32 string_size;
145 
146 #define PRINT_TYPE_FUNC_NAME(type)	print_type_##type
147 #define PRINT_TYPE_FMT_NAME(type)	print_type_format_##type
148 
149 /* Printing  in basic type function template */
150 #define DECLARE_BASIC_PRINT_TYPE_FUNC(type)				\
151 int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, void *data, void *ent);\
152 extern const char PRINT_TYPE_FMT_NAME(type)[]
153 
154 DECLARE_BASIC_PRINT_TYPE_FUNC(u8);
155 DECLARE_BASIC_PRINT_TYPE_FUNC(u16);
156 DECLARE_BASIC_PRINT_TYPE_FUNC(u32);
157 DECLARE_BASIC_PRINT_TYPE_FUNC(u64);
158 DECLARE_BASIC_PRINT_TYPE_FUNC(s8);
159 DECLARE_BASIC_PRINT_TYPE_FUNC(s16);
160 DECLARE_BASIC_PRINT_TYPE_FUNC(s32);
161 DECLARE_BASIC_PRINT_TYPE_FUNC(s64);
162 DECLARE_BASIC_PRINT_TYPE_FUNC(x8);
163 DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
164 DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
165 DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
166 
167 DECLARE_BASIC_PRINT_TYPE_FUNC(string);
168 DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);
169 
170 /* Default (unsigned long) fetch type */
171 #define __DEFAULT_FETCH_TYPE(t) x##t
172 #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
173 #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
174 #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
175 
176 #define __ADDR_FETCH_TYPE(t) u##t
177 #define _ADDR_FETCH_TYPE(t) __ADDR_FETCH_TYPE(t)
178 #define ADDR_FETCH_TYPE _ADDR_FETCH_TYPE(BITS_PER_LONG)
179 
180 #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype)	\
181 	{.name = _name,				\
182 	 .size = _size,					\
183 	 .is_signed = sign,				\
184 	 .print = PRINT_TYPE_FUNC_NAME(ptype),		\
185 	 .fmt = PRINT_TYPE_FMT_NAME(ptype),		\
186 	 .fmttype = _fmttype,				\
187 	}
188 #define _ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype)	\
189 	__ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, #_fmttype)
190 #define ASSIGN_FETCH_TYPE(ptype, ftype, sign)			\
191 	_ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, ptype)
192 
193 /* If ptype is an alias of atype, use this macro (show atype in format) */
194 #define ASSIGN_FETCH_TYPE_ALIAS(ptype, atype, ftype, sign)		\
195 	_ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, atype)
196 
197 #define ASSIGN_FETCH_TYPE_END {}
198 #define MAX_ARRAY_LEN	64
199 
200 #ifdef CONFIG_KPROBE_EVENTS
201 bool trace_kprobe_on_func_entry(struct trace_event_call *call);
202 bool trace_kprobe_error_injectable(struct trace_event_call *call);
203 #else
204 static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call)
205 {
206 	return false;
207 }
208 
209 static inline bool trace_kprobe_error_injectable(struct trace_event_call *call)
210 {
211 	return false;
212 }
213 #endif /* CONFIG_KPROBE_EVENTS */
214 
215 struct probe_arg {
216 	struct fetch_insn	*code;
217 	bool			dynamic;/* Dynamic array (string) is used */
218 	unsigned int		offset;	/* Offset from argument entry */
219 	unsigned int		count;	/* Array count */
220 	const char		*name;	/* Name of this argument */
221 	const char		*comm;	/* Command of this argument */
222 	char			*fmt;	/* Format string if needed */
223 	const struct fetch_type	*type;	/* Type of this argument */
224 };
225 
226 struct trace_probe {
227 	unsigned int			flags;	/* For TP_FLAG_* */
228 	struct trace_event_class	class;
229 	struct trace_event_call		call;
230 	struct list_head 		files;
231 	ssize_t				size;	/* trace entry size */
232 	unsigned int			nr_args;
233 	struct probe_arg		args[];
234 };
235 
236 struct event_file_link {
237 	struct trace_event_file		*file;
238 	struct list_head		list;
239 };
240 
241 static inline bool trace_probe_test_flag(struct trace_probe *tp,
242 					 unsigned int flag)
243 {
244 	return !!(tp->flags & flag);
245 }
246 
247 static inline void trace_probe_set_flag(struct trace_probe *tp,
248 					unsigned int flag)
249 {
250 	tp->flags |= flag;
251 }
252 
253 static inline void trace_probe_clear_flag(struct trace_probe *tp,
254 					  unsigned int flag)
255 {
256 	tp->flags &= ~flag;
257 }
258 
259 static inline bool trace_probe_is_enabled(struct trace_probe *tp)
260 {
261 	return trace_probe_test_flag(tp, TP_FLAG_TRACE | TP_FLAG_PROFILE);
262 }
263 
264 static inline bool trace_probe_is_registered(struct trace_probe *tp)
265 {
266 	return trace_probe_test_flag(tp, TP_FLAG_REGISTERED);
267 }
268 
269 static inline int trace_probe_unregister_event_call(struct trace_probe *tp)
270 {
271 	/* tp->event is unregistered in trace_remove_event_call() */
272 	return trace_remove_event_call(&tp->call);
273 }
274 
275 static inline bool trace_probe_has_single_file(struct trace_probe *tp)
276 {
277 	return !!list_is_singular(&tp->files);
278 }
279 
280 int trace_probe_init(struct trace_probe *tp, const char *event,
281 		     const char *group);
282 void trace_probe_cleanup(struct trace_probe *tp);
283 int trace_probe_register_event_call(struct trace_probe *tp);
284 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file);
285 int trace_probe_remove_file(struct trace_probe *tp,
286 			    struct trace_event_file *file);
287 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
288 						struct trace_event_file *file);
289 
290 #define trace_probe_for_each_link(pos, tp)	\
291 	list_for_each_entry(pos, &(tp)->files, list)
292 #define trace_probe_for_each_link_rcu(pos, tp)	\
293 	list_for_each_entry_rcu(pos, &(tp)->files, list)
294 
295 /* Check the name is good for event/group/fields */
296 static inline bool is_good_name(const char *name)
297 {
298 	if (!isalpha(*name) && *name != '_')
299 		return false;
300 	while (*++name != '\0') {
301 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
302 			return false;
303 	}
304 	return true;
305 }
306 
307 #define TPARG_FL_RETURN BIT(0)
308 #define TPARG_FL_KERNEL BIT(1)
309 #define TPARG_FL_FENTRY BIT(2)
310 #define TPARG_FL_MASK	GENMASK(2, 0)
311 
312 extern int traceprobe_parse_probe_arg(struct trace_probe *tp, int i,
313 				char *arg, unsigned int flags);
314 
315 extern int traceprobe_update_arg(struct probe_arg *arg);
316 extern void traceprobe_free_probe_arg(struct probe_arg *arg);
317 
318 extern int traceprobe_split_symbol_offset(char *symbol, long *offset);
319 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
320 				char *buf, int offset);
321 
322 extern int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return);
323 
324 #ifdef CONFIG_PERF_EVENTS
325 extern struct trace_event_call *
326 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
327 			  bool is_return);
328 extern void destroy_local_trace_kprobe(struct trace_event_call *event_call);
329 
330 extern struct trace_event_call *
331 create_local_trace_uprobe(char *name, unsigned long offs,
332 			  unsigned long ref_ctr_offset, bool is_return);
333 extern void destroy_local_trace_uprobe(struct trace_event_call *event_call);
334 #endif
335 extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,
336 					size_t offset, struct trace_probe *tp);
337 
338 #undef ERRORS
339 #define ERRORS	\
340 	C(FILE_NOT_FOUND,	"Failed to find the given file"),	\
341 	C(NO_REGULAR_FILE,	"Not a regular file"),			\
342 	C(BAD_REFCNT,		"Invalid reference counter offset"),	\
343 	C(REFCNT_OPEN_BRACE,	"Reference counter brace is not closed"), \
344 	C(BAD_REFCNT_SUFFIX,	"Reference counter has wrong suffix"),	\
345 	C(BAD_UPROBE_OFFS,	"Invalid uprobe offset"),		\
346 	C(MAXACT_NO_KPROBE,	"Maxactive is not for kprobe"),		\
347 	C(BAD_MAXACT,		"Invalid maxactive number"),		\
348 	C(MAXACT_TOO_BIG,	"Maxactive is too big"),		\
349 	C(BAD_PROBE_ADDR,	"Invalid probed address or symbol"),	\
350 	C(BAD_RETPROBE,		"Retprobe address must be an function entry"), \
351 	C(NO_GROUP_NAME,	"Group name is not specified"),		\
352 	C(GROUP_TOO_LONG,	"Group name is too long"),		\
353 	C(BAD_GROUP_NAME,	"Group name must follow the same rules as C identifiers"), \
354 	C(NO_EVENT_NAME,	"Event name is not specified"),		\
355 	C(EVENT_TOO_LONG,	"Event name is too long"),		\
356 	C(BAD_EVENT_NAME,	"Event name must follow the same rules as C identifiers"), \
357 	C(RETVAL_ON_PROBE,	"$retval is not available on probe"),	\
358 	C(BAD_STACK_NUM,	"Invalid stack number"),		\
359 	C(BAD_ARG_NUM,		"Invalid argument number"),		\
360 	C(BAD_VAR,		"Invalid $-valiable specified"),	\
361 	C(BAD_REG_NAME,		"Invalid register name"),		\
362 	C(BAD_MEM_ADDR,		"Invalid memory address"),		\
363 	C(FILE_ON_KPROBE,	"File offset is not available with kprobe"), \
364 	C(BAD_FILE_OFFS,	"Invalid file offset value"),		\
365 	C(SYM_ON_UPROBE,	"Symbol is not available with uprobe"),	\
366 	C(TOO_MANY_OPS,		"Dereference is too much nested"), 	\
367 	C(DEREF_NEED_BRACE,	"Dereference needs a brace"),		\
368 	C(BAD_DEREF_OFFS,	"Invalid dereference offset"),		\
369 	C(DEREF_OPEN_BRACE,	"Dereference brace is not closed"),	\
370 	C(COMM_CANT_DEREF,	"$comm can not be dereferenced"),	\
371 	C(BAD_FETCH_ARG,	"Invalid fetch argument"),		\
372 	C(ARRAY_NO_CLOSE,	"Array is not closed"),			\
373 	C(BAD_ARRAY_SUFFIX,	"Array has wrong suffix"),		\
374 	C(BAD_ARRAY_NUM,	"Invalid array size"),			\
375 	C(ARRAY_TOO_BIG,	"Array number is too big"),		\
376 	C(BAD_TYPE,		"Unknown type is specified"),		\
377 	C(BAD_STRING,		"String accepts only memory argument"),	\
378 	C(BAD_BITFIELD,		"Invalid bitfield"),			\
379 	C(ARG_NAME_TOO_LONG,	"Argument name is too long"),		\
380 	C(NO_ARG_NAME,		"Argument name is not specified"),	\
381 	C(BAD_ARG_NAME,		"Argument name must follow the same rules as C identifiers"), \
382 	C(USED_ARG_NAME,	"This argument name is already used"),	\
383 	C(ARG_TOO_LONG,		"Argument expression is too long"),	\
384 	C(NO_ARG_BODY,		"No argument expression"),		\
385 	C(BAD_INSN_BNDRY,	"Probe point is not an instruction boundary"),\
386 	C(FAIL_REG_PROBE,	"Failed to register probe event"),
387 
388 #undef C
389 #define C(a, b)		TP_ERR_##a
390 
391 /* Define TP_ERR_ */
392 enum { ERRORS };
393 
394 /* Error text is defined in trace_probe.c */
395 
396 struct trace_probe_log {
397 	const char	*subsystem;
398 	const char	**argv;
399 	int		argc;
400 	int		index;
401 };
402 
403 void trace_probe_log_init(const char *subsystem, int argc, const char **argv);
404 void trace_probe_log_set_index(int index);
405 void trace_probe_log_clear(void);
406 void __trace_probe_log_err(int offset, int err);
407 
408 #define trace_probe_log_err(offs, err)	\
409 	__trace_probe_log_err(offs, TP_ERR_##err)
410