xref: /linux-6.15/include/linux/trace_events.h (revision 00389c58)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _LINUX_TRACE_EVENT_H
4 #define _LINUX_TRACE_EVENT_H
5 
6 #include <linux/ring_buffer.h>
7 #include <linux/trace_seq.h>
8 #include <linux/percpu.h>
9 #include <linux/hardirq.h>
10 #include <linux/perf_event.h>
11 #include <linux/tracepoint.h>
12 
13 struct trace_array;
14 struct array_buffer;
15 struct tracer;
16 struct dentry;
17 struct bpf_prog;
18 union bpf_attr;
19 
20 const char *trace_print_flags_seq(struct trace_seq *p, const char *delim,
21 				  unsigned long flags,
22 				  const struct trace_print_flags *flag_array);
23 
24 const char *trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
25 				    const struct trace_print_flags *symbol_array);
26 
27 #if BITS_PER_LONG == 32
28 const char *trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
29 		      unsigned long long flags,
30 		      const struct trace_print_flags_u64 *flag_array);
31 
32 const char *trace_print_symbols_seq_u64(struct trace_seq *p,
33 					unsigned long long val,
34 					const struct trace_print_flags_u64
35 								 *symbol_array);
36 #endif
37 
38 const char *trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
39 				    unsigned int bitmask_size);
40 
41 const char *trace_print_hex_seq(struct trace_seq *p,
42 				const unsigned char *buf, int len,
43 				bool concatenate);
44 
45 const char *trace_print_array_seq(struct trace_seq *p,
46 				   const void *buf, int count,
47 				   size_t el_size);
48 
49 const char *
50 trace_print_hex_dump_seq(struct trace_seq *p, const char *prefix_str,
51 			 int prefix_type, int rowsize, int groupsize,
52 			 const void *buf, size_t len, bool ascii);
53 
54 struct trace_iterator;
55 struct trace_event;
56 
57 int trace_raw_output_prep(struct trace_iterator *iter,
58 			  struct trace_event *event);
59 extern __printf(2, 3)
60 void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...);
61 
62 /*
63  * The trace entry - the most basic unit of tracing. This is what
64  * is printed in the end as a single line in the trace output, such as:
65  *
66  *     bash-15816 [01]   235.197585: idle_cpu <- irq_enter
67  */
68 struct trace_entry {
69 	unsigned short		type;
70 	unsigned char		flags;
71 	unsigned char		preempt_count;
72 	int			pid;
73 };
74 
75 #define TRACE_EVENT_TYPE_MAX						\
76 	((1 << (sizeof(((struct trace_entry *)0)->type) * 8)) - 1)
77 
78 /*
79  * Trace iterator - used by printout routines who present trace
80  * results to users and which routines might sleep, etc:
81  */
82 struct trace_iterator {
83 	struct trace_array	*tr;
84 	struct tracer		*trace;
85 	struct array_buffer	*array_buffer;
86 	void			*private;
87 	int			cpu_file;
88 	struct mutex		mutex;
89 	struct ring_buffer_iter	**buffer_iter;
90 	unsigned long		iter_flags;
91 	void			*temp;	/* temp holder */
92 	unsigned int		temp_size;
93 	char			*fmt;	/* modified format holder */
94 	unsigned int		fmt_size;
95 
96 	/* trace_seq for __print_flags() and __print_symbolic() etc. */
97 	struct trace_seq	tmp_seq;
98 
99 	cpumask_var_t		started;
100 
101 	/* it's true when current open file is snapshot */
102 	bool			snapshot;
103 
104 	/* The below is zeroed out in pipe_read */
105 	struct trace_seq	seq;
106 	struct trace_entry	*ent;
107 	unsigned long		lost_events;
108 	int			leftover;
109 	int			ent_size;
110 	int			cpu;
111 	u64			ts;
112 
113 	loff_t			pos;
114 	long			idx;
115 
116 	/* All new field here will be zeroed out in pipe_read */
117 };
118 
119 enum trace_iter_flags {
120 	TRACE_FILE_LAT_FMT	= 1,
121 	TRACE_FILE_ANNOTATE	= 2,
122 	TRACE_FILE_TIME_IN_NS	= 4,
123 };
124 
125 
126 typedef enum print_line_t (*trace_print_func)(struct trace_iterator *iter,
127 				      int flags, struct trace_event *event);
128 
129 struct trace_event_functions {
130 	trace_print_func	trace;
131 	trace_print_func	raw;
132 	trace_print_func	hex;
133 	trace_print_func	binary;
134 };
135 
136 struct trace_event {
137 	struct hlist_node		node;
138 	struct list_head		list;
139 	int				type;
140 	struct trace_event_functions	*funcs;
141 };
142 
143 extern int register_trace_event(struct trace_event *event);
144 extern int unregister_trace_event(struct trace_event *event);
145 
146 /* Return values for print_line callback */
147 enum print_line_t {
148 	TRACE_TYPE_PARTIAL_LINE	= 0,	/* Retry after flushing the seq */
149 	TRACE_TYPE_HANDLED	= 1,
150 	TRACE_TYPE_UNHANDLED	= 2,	/* Relay to other output functions */
151 	TRACE_TYPE_NO_CONSUME	= 3	/* Handled but ask to not consume */
152 };
153 
154 enum print_line_t trace_handle_return(struct trace_seq *s);
155 
156 static inline void tracing_generic_entry_update(struct trace_entry *entry,
157 						unsigned short type,
158 						unsigned int trace_ctx)
159 {
160 	entry->preempt_count		= trace_ctx & 0xff;
161 	entry->pid			= current->pid;
162 	entry->type			= type;
163 	entry->flags =			trace_ctx >> 16;
164 }
165 
166 unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status);
167 
168 enum trace_flag_type {
169 	TRACE_FLAG_IRQS_OFF		= 0x01,
170 	TRACE_FLAG_IRQS_NOSUPPORT	= 0x02,
171 	TRACE_FLAG_NEED_RESCHED		= 0x04,
172 	TRACE_FLAG_HARDIRQ		= 0x08,
173 	TRACE_FLAG_SOFTIRQ		= 0x10,
174 	TRACE_FLAG_PREEMPT_RESCHED	= 0x20,
175 	TRACE_FLAG_NMI			= 0x40,
176 	TRACE_FLAG_BH_OFF		= 0x80,
177 };
178 
179 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
180 static inline unsigned int tracing_gen_ctx_flags(unsigned long irqflags)
181 {
182 	unsigned int irq_status = irqs_disabled_flags(irqflags) ?
183 		TRACE_FLAG_IRQS_OFF : 0;
184 	return tracing_gen_ctx_irq_test(irq_status);
185 }
186 static inline unsigned int tracing_gen_ctx(void)
187 {
188 	unsigned long irqflags;
189 
190 	local_save_flags(irqflags);
191 	return tracing_gen_ctx_flags(irqflags);
192 }
193 #else
194 
195 static inline unsigned int tracing_gen_ctx_flags(unsigned long irqflags)
196 {
197 	return tracing_gen_ctx_irq_test(TRACE_FLAG_IRQS_NOSUPPORT);
198 }
199 static inline unsigned int tracing_gen_ctx(void)
200 {
201 	return tracing_gen_ctx_irq_test(TRACE_FLAG_IRQS_NOSUPPORT);
202 }
203 #endif
204 
205 static inline unsigned int tracing_gen_ctx_dec(void)
206 {
207 	unsigned int trace_ctx;
208 
209 	trace_ctx = tracing_gen_ctx();
210 	/*
211 	 * Subtract one from the preemption counter if preemption is enabled,
212 	 * see trace_event_buffer_reserve()for details.
213 	 */
214 	if (IS_ENABLED(CONFIG_PREEMPTION))
215 		trace_ctx--;
216 	return trace_ctx;
217 }
218 
219 struct trace_event_file;
220 
221 struct ring_buffer_event *
222 trace_event_buffer_lock_reserve(struct trace_buffer **current_buffer,
223 				struct trace_event_file *trace_file,
224 				int type, unsigned long len,
225 				unsigned int trace_ctx);
226 
227 #define TRACE_RECORD_CMDLINE	BIT(0)
228 #define TRACE_RECORD_TGID	BIT(1)
229 
230 void tracing_record_taskinfo(struct task_struct *task, int flags);
231 void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
232 					  struct task_struct *next, int flags);
233 
234 void tracing_record_cmdline(struct task_struct *task);
235 void tracing_record_tgid(struct task_struct *task);
236 
237 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...);
238 
239 struct event_filter;
240 
241 enum trace_reg {
242 	TRACE_REG_REGISTER,
243 	TRACE_REG_UNREGISTER,
244 #ifdef CONFIG_PERF_EVENTS
245 	TRACE_REG_PERF_REGISTER,
246 	TRACE_REG_PERF_UNREGISTER,
247 	TRACE_REG_PERF_OPEN,
248 	TRACE_REG_PERF_CLOSE,
249 	/*
250 	 * These (ADD/DEL) use a 'boolean' return value, where 1 (true) means a
251 	 * custom action was taken and the default action is not to be
252 	 * performed.
253 	 */
254 	TRACE_REG_PERF_ADD,
255 	TRACE_REG_PERF_DEL,
256 #endif
257 };
258 
259 struct trace_event_call;
260 
261 #define TRACE_FUNCTION_TYPE ((const char *)~0UL)
262 
263 struct trace_event_fields {
264 	const char *type;
265 	union {
266 		struct {
267 			const char *name;
268 			const int  size;
269 			const int  align;
270 			const int  is_signed;
271 			const int  filter_type;
272 		};
273 		int (*define_fields)(struct trace_event_call *);
274 	};
275 };
276 
277 struct trace_event_class {
278 	const char		*system;
279 	void			*probe;
280 #ifdef CONFIG_PERF_EVENTS
281 	void			*perf_probe;
282 #endif
283 	int			(*reg)(struct trace_event_call *event,
284 				       enum trace_reg type, void *data);
285 	struct trace_event_fields *fields_array;
286 	struct list_head	*(*get_fields)(struct trace_event_call *);
287 	struct list_head	fields;
288 	int			(*raw_init)(struct trace_event_call *);
289 };
290 
291 extern int trace_event_reg(struct trace_event_call *event,
292 			    enum trace_reg type, void *data);
293 
294 struct trace_event_buffer {
295 	struct trace_buffer		*buffer;
296 	struct ring_buffer_event	*event;
297 	struct trace_event_file		*trace_file;
298 	void				*entry;
299 	unsigned int			trace_ctx;
300 	struct pt_regs			*regs;
301 };
302 
303 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
304 				  struct trace_event_file *trace_file,
305 				  unsigned long len);
306 
307 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer);
308 
309 enum {
310 	TRACE_EVENT_FL_FILTERED_BIT,
311 	TRACE_EVENT_FL_CAP_ANY_BIT,
312 	TRACE_EVENT_FL_NO_SET_FILTER_BIT,
313 	TRACE_EVENT_FL_IGNORE_ENABLE_BIT,
314 	TRACE_EVENT_FL_TRACEPOINT_BIT,
315 	TRACE_EVENT_FL_DYNAMIC_BIT,
316 	TRACE_EVENT_FL_KPROBE_BIT,
317 	TRACE_EVENT_FL_UPROBE_BIT,
318 	TRACE_EVENT_FL_EPROBE_BIT,
319 };
320 
321 /*
322  * Event flags:
323  *  FILTERED	  - The event has a filter attached
324  *  CAP_ANY	  - Any user can enable for perf
325  *  NO_SET_FILTER - Set when filter has error and is to be ignored
326  *  IGNORE_ENABLE - For trace internal events, do not enable with debugfs file
327  *  TRACEPOINT    - Event is a tracepoint
328  *  DYNAMIC       - Event is a dynamic event (created at run time)
329  *  KPROBE        - Event is a kprobe
330  *  UPROBE        - Event is a uprobe
331  *  EPROBE        - Event is an event probe
332  */
333 enum {
334 	TRACE_EVENT_FL_FILTERED		= (1 << TRACE_EVENT_FL_FILTERED_BIT),
335 	TRACE_EVENT_FL_CAP_ANY		= (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
336 	TRACE_EVENT_FL_NO_SET_FILTER	= (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
337 	TRACE_EVENT_FL_IGNORE_ENABLE	= (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
338 	TRACE_EVENT_FL_TRACEPOINT	= (1 << TRACE_EVENT_FL_TRACEPOINT_BIT),
339 	TRACE_EVENT_FL_DYNAMIC		= (1 << TRACE_EVENT_FL_DYNAMIC_BIT),
340 	TRACE_EVENT_FL_KPROBE		= (1 << TRACE_EVENT_FL_KPROBE_BIT),
341 	TRACE_EVENT_FL_UPROBE		= (1 << TRACE_EVENT_FL_UPROBE_BIT),
342 	TRACE_EVENT_FL_EPROBE		= (1 << TRACE_EVENT_FL_EPROBE_BIT),
343 };
344 
345 #define TRACE_EVENT_FL_UKPROBE (TRACE_EVENT_FL_KPROBE | TRACE_EVENT_FL_UPROBE)
346 
347 struct trace_event_call {
348 	struct list_head	list;
349 	struct trace_event_class *class;
350 	union {
351 		char			*name;
352 		/* Set TRACE_EVENT_FL_TRACEPOINT flag when using "tp" */
353 		struct tracepoint	*tp;
354 	};
355 	struct trace_event	event;
356 	char			*print_fmt;
357 	struct event_filter	*filter;
358 	/*
359 	 * Static events can disappear with modules,
360 	 * where as dynamic ones need their own ref count.
361 	 */
362 	union {
363 		void				*module;
364 		atomic_t			refcnt;
365 	};
366 	void			*data;
367 
368 	/* See the TRACE_EVENT_FL_* flags above */
369 	int			flags; /* static flags of different events */
370 
371 #ifdef CONFIG_PERF_EVENTS
372 	int				perf_refcount;
373 	struct hlist_head __percpu	*perf_events;
374 	struct bpf_prog_array __rcu	*prog_array;
375 
376 	int	(*perf_perm)(struct trace_event_call *,
377 			     struct perf_event *);
378 #endif
379 };
380 
381 #ifdef CONFIG_DYNAMIC_EVENTS
382 bool trace_event_dyn_try_get_ref(struct trace_event_call *call);
383 void trace_event_dyn_put_ref(struct trace_event_call *call);
384 bool trace_event_dyn_busy(struct trace_event_call *call);
385 #else
386 static inline bool trace_event_dyn_try_get_ref(struct trace_event_call *call)
387 {
388 	/* Without DYNAMIC_EVENTS configured, nothing should be calling this */
389 	return false;
390 }
391 static inline void trace_event_dyn_put_ref(struct trace_event_call *call)
392 {
393 }
394 static inline bool trace_event_dyn_busy(struct trace_event_call *call)
395 {
396 	/* Nothing should call this without DYNAIMIC_EVENTS configured. */
397 	return true;
398 }
399 #endif
400 
401 static inline bool trace_event_try_get_ref(struct trace_event_call *call)
402 {
403 	if (call->flags & TRACE_EVENT_FL_DYNAMIC)
404 		return trace_event_dyn_try_get_ref(call);
405 	else
406 		return try_module_get(call->module);
407 }
408 
409 static inline void trace_event_put_ref(struct trace_event_call *call)
410 {
411 	if (call->flags & TRACE_EVENT_FL_DYNAMIC)
412 		trace_event_dyn_put_ref(call);
413 	else
414 		module_put(call->module);
415 }
416 
417 #ifdef CONFIG_PERF_EVENTS
418 static inline bool bpf_prog_array_valid(struct trace_event_call *call)
419 {
420 	/*
421 	 * This inline function checks whether call->prog_array
422 	 * is valid or not. The function is called in various places,
423 	 * outside rcu_read_lock/unlock, as a heuristic to speed up execution.
424 	 *
425 	 * If this function returns true, and later call->prog_array
426 	 * becomes false inside rcu_read_lock/unlock region,
427 	 * we bail out then. If this function return false,
428 	 * there is a risk that we might miss a few events if the checking
429 	 * were delayed until inside rcu_read_lock/unlock region and
430 	 * call->prog_array happened to become non-NULL then.
431 	 *
432 	 * Here, READ_ONCE() is used instead of rcu_access_pointer().
433 	 * rcu_access_pointer() requires the actual definition of
434 	 * "struct bpf_prog_array" while READ_ONCE() only needs
435 	 * a declaration of the same type.
436 	 */
437 	return !!READ_ONCE(call->prog_array);
438 }
439 #endif
440 
441 static inline const char *
442 trace_event_name(struct trace_event_call *call)
443 {
444 	if (call->flags & TRACE_EVENT_FL_TRACEPOINT)
445 		return call->tp ? call->tp->name : NULL;
446 	else
447 		return call->name;
448 }
449 
450 static inline struct list_head *
451 trace_get_fields(struct trace_event_call *event_call)
452 {
453 	if (!event_call->class->get_fields)
454 		return &event_call->class->fields;
455 	return event_call->class->get_fields(event_call);
456 }
457 
458 struct trace_subsystem_dir;
459 
460 enum {
461 	EVENT_FILE_FL_ENABLED_BIT,
462 	EVENT_FILE_FL_RECORDED_CMD_BIT,
463 	EVENT_FILE_FL_RECORDED_TGID_BIT,
464 	EVENT_FILE_FL_FILTERED_BIT,
465 	EVENT_FILE_FL_NO_SET_FILTER_BIT,
466 	EVENT_FILE_FL_SOFT_MODE_BIT,
467 	EVENT_FILE_FL_SOFT_DISABLED_BIT,
468 	EVENT_FILE_FL_TRIGGER_MODE_BIT,
469 	EVENT_FILE_FL_TRIGGER_COND_BIT,
470 	EVENT_FILE_FL_PID_FILTER_BIT,
471 	EVENT_FILE_FL_WAS_ENABLED_BIT,
472 };
473 
474 extern struct trace_event_file *trace_get_event_file(const char *instance,
475 						     const char *system,
476 						     const char *event);
477 extern void trace_put_event_file(struct trace_event_file *file);
478 
479 #define MAX_DYNEVENT_CMD_LEN	(2048)
480 
481 enum dynevent_type {
482 	DYNEVENT_TYPE_SYNTH = 1,
483 	DYNEVENT_TYPE_KPROBE,
484 	DYNEVENT_TYPE_NONE,
485 };
486 
487 struct dynevent_cmd;
488 
489 typedef int (*dynevent_create_fn_t)(struct dynevent_cmd *cmd);
490 
491 struct dynevent_cmd {
492 	struct seq_buf		seq;
493 	const char		*event_name;
494 	unsigned int		n_fields;
495 	enum dynevent_type	type;
496 	dynevent_create_fn_t	run_command;
497 	void			*private_data;
498 };
499 
500 extern int dynevent_create(struct dynevent_cmd *cmd);
501 
502 extern int synth_event_delete(const char *name);
503 
504 extern void synth_event_cmd_init(struct dynevent_cmd *cmd,
505 				 char *buf, int maxlen);
506 
507 extern int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd,
508 				       const char *name,
509 				       struct module *mod, ...);
510 
511 #define synth_event_gen_cmd_start(cmd, name, mod, ...)	\
512 	__synth_event_gen_cmd_start(cmd, name, mod, ## __VA_ARGS__, NULL)
513 
514 struct synth_field_desc {
515 	const char *type;
516 	const char *name;
517 };
518 
519 extern int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd,
520 					   const char *name,
521 					   struct module *mod,
522 					   struct synth_field_desc *fields,
523 					   unsigned int n_fields);
524 extern int synth_event_create(const char *name,
525 			      struct synth_field_desc *fields,
526 			      unsigned int n_fields, struct module *mod);
527 
528 extern int synth_event_add_field(struct dynevent_cmd *cmd,
529 				 const char *type,
530 				 const char *name);
531 extern int synth_event_add_field_str(struct dynevent_cmd *cmd,
532 				     const char *type_name);
533 extern int synth_event_add_fields(struct dynevent_cmd *cmd,
534 				  struct synth_field_desc *fields,
535 				  unsigned int n_fields);
536 
537 #define synth_event_gen_cmd_end(cmd)	\
538 	dynevent_create(cmd)
539 
540 struct synth_event;
541 
542 struct synth_event_trace_state {
543 	struct trace_event_buffer fbuffer;
544 	struct synth_trace_event *entry;
545 	struct trace_buffer *buffer;
546 	struct synth_event *event;
547 	unsigned int cur_field;
548 	unsigned int n_u64;
549 	bool disabled;
550 	bool add_next;
551 	bool add_name;
552 };
553 
554 extern int synth_event_trace(struct trace_event_file *file,
555 			     unsigned int n_vals, ...);
556 extern int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
557 				   unsigned int n_vals);
558 extern int synth_event_trace_start(struct trace_event_file *file,
559 				   struct synth_event_trace_state *trace_state);
560 extern int synth_event_add_next_val(u64 val,
561 				    struct synth_event_trace_state *trace_state);
562 extern int synth_event_add_val(const char *field_name, u64 val,
563 			       struct synth_event_trace_state *trace_state);
564 extern int synth_event_trace_end(struct synth_event_trace_state *trace_state);
565 
566 extern int kprobe_event_delete(const char *name);
567 
568 extern void kprobe_event_cmd_init(struct dynevent_cmd *cmd,
569 				  char *buf, int maxlen);
570 
571 #define kprobe_event_gen_cmd_start(cmd, name, loc, ...)			\
572 	__kprobe_event_gen_cmd_start(cmd, false, name, loc, ## __VA_ARGS__, NULL)
573 
574 #define kretprobe_event_gen_cmd_start(cmd, name, loc, ...)		\
575 	__kprobe_event_gen_cmd_start(cmd, true, name, loc, ## __VA_ARGS__, NULL)
576 
577 extern int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd,
578 					bool kretprobe,
579 					const char *name,
580 					const char *loc, ...);
581 
582 #define kprobe_event_add_fields(cmd, ...)	\
583 	__kprobe_event_add_fields(cmd, ## __VA_ARGS__, NULL)
584 
585 #define kprobe_event_add_field(cmd, field)	\
586 	__kprobe_event_add_fields(cmd, field, NULL)
587 
588 extern int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...);
589 
590 #define kprobe_event_gen_cmd_end(cmd)		\
591 	dynevent_create(cmd)
592 
593 #define kretprobe_event_gen_cmd_end(cmd)	\
594 	dynevent_create(cmd)
595 
596 /*
597  * Event file flags:
598  *  ENABLED	  - The event is enabled
599  *  RECORDED_CMD  - The comms should be recorded at sched_switch
600  *  RECORDED_TGID - The tgids should be recorded at sched_switch
601  *  FILTERED	  - The event has a filter attached
602  *  NO_SET_FILTER - Set when filter has error and is to be ignored
603  *  SOFT_MODE     - The event is enabled/disabled by SOFT_DISABLED
604  *  SOFT_DISABLED - When set, do not trace the event (even though its
605  *                   tracepoint may be enabled)
606  *  TRIGGER_MODE  - When set, invoke the triggers associated with the event
607  *  TRIGGER_COND  - When set, one or more triggers has an associated filter
608  *  PID_FILTER    - When set, the event is filtered based on pid
609  *  WAS_ENABLED   - Set when enabled to know to clear trace on module removal
610  */
611 enum {
612 	EVENT_FILE_FL_ENABLED		= (1 << EVENT_FILE_FL_ENABLED_BIT),
613 	EVENT_FILE_FL_RECORDED_CMD	= (1 << EVENT_FILE_FL_RECORDED_CMD_BIT),
614 	EVENT_FILE_FL_RECORDED_TGID	= (1 << EVENT_FILE_FL_RECORDED_TGID_BIT),
615 	EVENT_FILE_FL_FILTERED		= (1 << EVENT_FILE_FL_FILTERED_BIT),
616 	EVENT_FILE_FL_NO_SET_FILTER	= (1 << EVENT_FILE_FL_NO_SET_FILTER_BIT),
617 	EVENT_FILE_FL_SOFT_MODE		= (1 << EVENT_FILE_FL_SOFT_MODE_BIT),
618 	EVENT_FILE_FL_SOFT_DISABLED	= (1 << EVENT_FILE_FL_SOFT_DISABLED_BIT),
619 	EVENT_FILE_FL_TRIGGER_MODE	= (1 << EVENT_FILE_FL_TRIGGER_MODE_BIT),
620 	EVENT_FILE_FL_TRIGGER_COND	= (1 << EVENT_FILE_FL_TRIGGER_COND_BIT),
621 	EVENT_FILE_FL_PID_FILTER	= (1 << EVENT_FILE_FL_PID_FILTER_BIT),
622 	EVENT_FILE_FL_WAS_ENABLED	= (1 << EVENT_FILE_FL_WAS_ENABLED_BIT),
623 };
624 
625 struct trace_event_file {
626 	struct list_head		list;
627 	struct trace_event_call		*event_call;
628 	struct event_filter __rcu	*filter;
629 	struct dentry			*dir;
630 	struct trace_array		*tr;
631 	struct trace_subsystem_dir	*system;
632 	struct list_head		triggers;
633 
634 	/*
635 	 * 32 bit flags:
636 	 *   bit 0:		enabled
637 	 *   bit 1:		enabled cmd record
638 	 *   bit 2:		enable/disable with the soft disable bit
639 	 *   bit 3:		soft disabled
640 	 *   bit 4:		trigger enabled
641 	 *
642 	 * Note: The bits must be set atomically to prevent races
643 	 * from other writers. Reads of flags do not need to be in
644 	 * sync as they occur in critical sections. But the way flags
645 	 * is currently used, these changes do not affect the code
646 	 * except that when a change is made, it may have a slight
647 	 * delay in propagating the changes to other CPUs due to
648 	 * caching and such. Which is mostly OK ;-)
649 	 */
650 	unsigned long		flags;
651 	atomic_t		sm_ref;	/* soft-mode reference counter */
652 	atomic_t		tm_ref;	/* trigger-mode reference counter */
653 };
654 
655 #define __TRACE_EVENT_FLAGS(name, value)				\
656 	static int __init trace_init_flags_##name(void)			\
657 	{								\
658 		event_##name.flags |= value;				\
659 		return 0;						\
660 	}								\
661 	early_initcall(trace_init_flags_##name);
662 
663 #define __TRACE_EVENT_PERF_PERM(name, expr...)				\
664 	static int perf_perm_##name(struct trace_event_call *tp_event, \
665 				    struct perf_event *p_event)		\
666 	{								\
667 		return ({ expr; });					\
668 	}								\
669 	static int __init trace_init_perf_perm_##name(void)		\
670 	{								\
671 		event_##name.perf_perm = &perf_perm_##name;		\
672 		return 0;						\
673 	}								\
674 	early_initcall(trace_init_perf_perm_##name);
675 
676 #define PERF_MAX_TRACE_SIZE	8192
677 
678 #define MAX_FILTER_STR_VAL	256U	/* Should handle KSYM_SYMBOL_LEN */
679 
680 enum event_trigger_type {
681 	ETT_NONE		= (0),
682 	ETT_TRACE_ONOFF		= (1 << 0),
683 	ETT_SNAPSHOT		= (1 << 1),
684 	ETT_STACKTRACE		= (1 << 2),
685 	ETT_EVENT_ENABLE	= (1 << 3),
686 	ETT_EVENT_HIST		= (1 << 4),
687 	ETT_HIST_ENABLE		= (1 << 5),
688 	ETT_EVENT_EPROBE	= (1 << 6),
689 };
690 
691 extern int filter_match_preds(struct event_filter *filter, void *rec);
692 
693 extern enum event_trigger_type
694 event_triggers_call(struct trace_event_file *file,
695 		    struct trace_buffer *buffer, void *rec,
696 		    struct ring_buffer_event *event);
697 extern void
698 event_triggers_post_call(struct trace_event_file *file,
699 			 enum event_trigger_type tt);
700 
701 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file);
702 
703 bool __trace_trigger_soft_disabled(struct trace_event_file *file);
704 
705 /**
706  * trace_trigger_soft_disabled - do triggers and test if soft disabled
707  * @file: The file pointer of the event to test
708  *
709  * If any triggers without filters are attached to this event, they
710  * will be called here. If the event is soft disabled and has no
711  * triggers that require testing the fields, it will return true,
712  * otherwise false.
713  */
714 static __always_inline bool
715 trace_trigger_soft_disabled(struct trace_event_file *file)
716 {
717 	unsigned long eflags = file->flags;
718 
719 	if (likely(!(eflags & (EVENT_FILE_FL_TRIGGER_MODE |
720 			       EVENT_FILE_FL_SOFT_DISABLED |
721 			       EVENT_FILE_FL_PID_FILTER))))
722 		return false;
723 
724 	if (likely(eflags & EVENT_FILE_FL_TRIGGER_COND))
725 		return false;
726 
727 	return __trace_trigger_soft_disabled(file);
728 }
729 
730 #ifdef CONFIG_BPF_EVENTS
731 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx);
732 int perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
733 void perf_event_detach_bpf_prog(struct perf_event *event);
734 int perf_event_query_prog_array(struct perf_event *event, void __user *info);
735 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog);
736 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog);
737 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name);
738 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp);
739 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
740 			    u32 *fd_type, const char **buf,
741 			    u64 *probe_offset, u64 *probe_addr);
742 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
743 #else
744 static inline unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
745 {
746 	return 1;
747 }
748 
749 static inline int
750 perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie)
751 {
752 	return -EOPNOTSUPP;
753 }
754 
755 static inline void perf_event_detach_bpf_prog(struct perf_event *event) { }
756 
757 static inline int
758 perf_event_query_prog_array(struct perf_event *event, void __user *info)
759 {
760 	return -EOPNOTSUPP;
761 }
762 static inline int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *p)
763 {
764 	return -EOPNOTSUPP;
765 }
766 static inline int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *p)
767 {
768 	return -EOPNOTSUPP;
769 }
770 static inline struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
771 {
772 	return NULL;
773 }
774 static inline void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
775 {
776 }
777 static inline int bpf_get_perf_event_info(const struct perf_event *event,
778 					  u32 *prog_id, u32 *fd_type,
779 					  const char **buf, u64 *probe_offset,
780 					  u64 *probe_addr)
781 {
782 	return -EOPNOTSUPP;
783 }
784 static inline int
785 bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
786 {
787 	return -EOPNOTSUPP;
788 }
789 #endif
790 
791 enum {
792 	FILTER_OTHER = 0,
793 	FILTER_STATIC_STRING,
794 	FILTER_DYN_STRING,
795 	FILTER_RDYN_STRING,
796 	FILTER_PTR_STRING,
797 	FILTER_TRACE_FN,
798 	FILTER_COMM,
799 	FILTER_CPU,
800 };
801 
802 extern int trace_event_raw_init(struct trace_event_call *call);
803 extern int trace_define_field(struct trace_event_call *call, const char *type,
804 			      const char *name, int offset, int size,
805 			      int is_signed, int filter_type);
806 extern int trace_add_event_call(struct trace_event_call *call);
807 extern int trace_remove_event_call(struct trace_event_call *call);
808 extern int trace_event_get_offsets(struct trace_event_call *call);
809 
810 #define is_signed_type(type)	(((type)(-1)) < (type)1)
811 
812 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
813 int trace_set_clr_event(const char *system, const char *event, int set);
814 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
815 		const char *event, bool enable);
816 /*
817  * The double __builtin_constant_p is because gcc will give us an error
818  * if we try to allocate the static variable to fmt if it is not a
819  * constant. Even with the outer if statement optimizing out.
820  */
821 #define event_trace_printk(ip, fmt, args...)				\
822 do {									\
823 	__trace_printk_check_format(fmt, ##args);			\
824 	tracing_record_cmdline(current);				\
825 	if (__builtin_constant_p(fmt)) {				\
826 		static const char *trace_printk_fmt			\
827 		  __section("__trace_printk_fmt") =			\
828 			__builtin_constant_p(fmt) ? fmt : NULL;		\
829 									\
830 		__trace_bprintk(ip, trace_printk_fmt, ##args);		\
831 	} else								\
832 		__trace_printk(ip, fmt, ##args);			\
833 } while (0)
834 
835 #ifdef CONFIG_PERF_EVENTS
836 struct perf_event;
837 
838 DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
839 DECLARE_PER_CPU(int, bpf_kprobe_override);
840 
841 extern int  perf_trace_init(struct perf_event *event);
842 extern void perf_trace_destroy(struct perf_event *event);
843 extern int  perf_trace_add(struct perf_event *event, int flags);
844 extern void perf_trace_del(struct perf_event *event, int flags);
845 #ifdef CONFIG_KPROBE_EVENTS
846 extern int  perf_kprobe_init(struct perf_event *event, bool is_retprobe);
847 extern void perf_kprobe_destroy(struct perf_event *event);
848 extern int bpf_get_kprobe_info(const struct perf_event *event,
849 			       u32 *fd_type, const char **symbol,
850 			       u64 *probe_offset, u64 *probe_addr,
851 			       bool perf_type_tracepoint);
852 #endif
853 #ifdef CONFIG_UPROBE_EVENTS
854 extern int  perf_uprobe_init(struct perf_event *event,
855 			     unsigned long ref_ctr_offset, bool is_retprobe);
856 extern void perf_uprobe_destroy(struct perf_event *event);
857 extern int bpf_get_uprobe_info(const struct perf_event *event,
858 			       u32 *fd_type, const char **filename,
859 			       u64 *probe_offset, bool perf_type_tracepoint);
860 #endif
861 extern int  ftrace_profile_set_filter(struct perf_event *event, int event_id,
862 				     char *filter_str);
863 extern void ftrace_profile_free_filter(struct perf_event *event);
864 void perf_trace_buf_update(void *record, u16 type);
865 void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp);
866 
867 int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
868 void perf_event_free_bpf_prog(struct perf_event *event);
869 
870 void bpf_trace_run1(struct bpf_prog *prog, u64 arg1);
871 void bpf_trace_run2(struct bpf_prog *prog, u64 arg1, u64 arg2);
872 void bpf_trace_run3(struct bpf_prog *prog, u64 arg1, u64 arg2,
873 		    u64 arg3);
874 void bpf_trace_run4(struct bpf_prog *prog, u64 arg1, u64 arg2,
875 		    u64 arg3, u64 arg4);
876 void bpf_trace_run5(struct bpf_prog *prog, u64 arg1, u64 arg2,
877 		    u64 arg3, u64 arg4, u64 arg5);
878 void bpf_trace_run6(struct bpf_prog *prog, u64 arg1, u64 arg2,
879 		    u64 arg3, u64 arg4, u64 arg5, u64 arg6);
880 void bpf_trace_run7(struct bpf_prog *prog, u64 arg1, u64 arg2,
881 		    u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7);
882 void bpf_trace_run8(struct bpf_prog *prog, u64 arg1, u64 arg2,
883 		    u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
884 		    u64 arg8);
885 void bpf_trace_run9(struct bpf_prog *prog, u64 arg1, u64 arg2,
886 		    u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
887 		    u64 arg8, u64 arg9);
888 void bpf_trace_run10(struct bpf_prog *prog, u64 arg1, u64 arg2,
889 		     u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
890 		     u64 arg8, u64 arg9, u64 arg10);
891 void bpf_trace_run11(struct bpf_prog *prog, u64 arg1, u64 arg2,
892 		     u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
893 		     u64 arg8, u64 arg9, u64 arg10, u64 arg11);
894 void bpf_trace_run12(struct bpf_prog *prog, u64 arg1, u64 arg2,
895 		     u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
896 		     u64 arg8, u64 arg9, u64 arg10, u64 arg11, u64 arg12);
897 void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
898 			       struct trace_event_call *call, u64 count,
899 			       struct pt_regs *regs, struct hlist_head *head,
900 			       struct task_struct *task);
901 
902 static inline void
903 perf_trace_buf_submit(void *raw_data, int size, int rctx, u16 type,
904 		       u64 count, struct pt_regs *regs, void *head,
905 		       struct task_struct *task)
906 {
907 	perf_tp_event(type, count, raw_data, size, regs, head, rctx, task);
908 }
909 
910 #endif
911 
912 #endif /* _LINUX_TRACE_EVENT_H */
913