xref: /linux/kernel/trace/trace.h (revision 2cddfc2e8fc78c13b0f5286ea5dd48cdf527ad41)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #ifndef _LINUX_KERNEL_TRACE_H
4 #define _LINUX_KERNEL_TRACE_H
5 
6 #include <linux/fs.h>
7 #include <linux/atomic.h>
8 #include <linux/sched.h>
9 #include <linux/clocksource.h>
10 #include <linux/ring_buffer.h>
11 #include <linux/mmiotrace.h>
12 #include <linux/tracepoint.h>
13 #include <linux/ftrace.h>
14 #include <linux/trace.h>
15 #include <linux/hw_breakpoint.h>
16 #include <linux/trace_seq.h>
17 #include <linux/trace_events.h>
18 #include <linux/compiler.h>
19 #include <linux/glob.h>
20 #include <linux/irq_work.h>
21 #include <linux/workqueue.h>
22 #include <linux/ctype.h>
23 #include <linux/once_lite.h>
24 #include <linux/ftrace_regs.h>
25 #include <linux/llist.h>
26 
27 #include "pid_list.h"
28 
29 #ifdef CONFIG_FTRACE_SYSCALLS
30 #include <asm/unistd.h>		/* For NR_syscalls	     */
31 #include <asm/syscall.h>	/* some archs define it here */
32 #endif
33 
34 #define TRACE_MODE_WRITE	0640
35 #define TRACE_MODE_READ		0440
36 
37 enum trace_type {
38 	__TRACE_FIRST_TYPE = 0,
39 
40 	TRACE_FN,
41 	TRACE_CTX,
42 	TRACE_WAKE,
43 	TRACE_STACK,
44 	TRACE_PRINT,
45 	TRACE_BPRINT,
46 	TRACE_MMIO_RW,
47 	TRACE_MMIO_MAP,
48 	TRACE_BRANCH,
49 	TRACE_GRAPH_RET,
50 	TRACE_GRAPH_ENT,
51 	TRACE_GRAPH_RETADDR_ENT,
52 	TRACE_USER_STACK,
53 	TRACE_BLK,
54 	TRACE_BPUTS,
55 	TRACE_HWLAT,
56 	TRACE_OSNOISE,
57 	TRACE_TIMERLAT,
58 	TRACE_RAW_DATA,
59 	TRACE_FUNC_REPEATS,
60 
61 	__TRACE_LAST_TYPE,
62 };
63 
64 
65 #undef __field
66 #define __field(type, item)		type	item;
67 
68 #undef __field_fn
69 #define __field_fn(type, item)		type	item;
70 
71 #undef __field_struct
72 #define __field_struct(type, item)	__field(type, item)
73 
74 #undef __field_desc
75 #define __field_desc(type, container, item)
76 
77 #undef __field_packed
78 #define __field_packed(type, container, item)
79 
80 #undef __array
81 #define __array(type, item, size)	type	item[size];
82 
83 /*
84  * For backward compatibility, older user space expects to see the
85  * kernel_stack event with a fixed size caller field. But today the fix
86  * size is ignored by the kernel, and the real structure is dynamic.
87  * Expose to user space: "unsigned long caller[8];" but the real structure
88  * will be "unsigned long caller[] __counted_by(size)"
89  */
90 #undef __stack_array
91 #define __stack_array(type, item, size, field)		type item[] __counted_by(field);
92 
93 #undef __array_desc
94 #define __array_desc(type, container, item, size)
95 
96 #undef __dynamic_array
97 #define __dynamic_array(type, item)	type	item[];
98 
99 #undef __rel_dynamic_array
100 #define __rel_dynamic_array(type, item)	type	item[];
101 
102 #undef F_STRUCT
103 #define F_STRUCT(args...)		args
104 
105 #undef FTRACE_ENTRY
106 #define FTRACE_ENTRY(name, struct_name, id, tstruct, print)		\
107 	struct struct_name {						\
108 		struct trace_entry	ent;				\
109 		tstruct							\
110 	}
111 
112 #undef FTRACE_ENTRY_DUP
113 #define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk)
114 
115 #undef FTRACE_ENTRY_REG
116 #define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print,	regfn)	\
117 	FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print))
118 
119 #undef FTRACE_ENTRY_PACKED
120 #define FTRACE_ENTRY_PACKED(name, struct_name, id, tstruct, print)	\
121 	FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print)) __packed
122 
123 #include "trace_entries.h"
124 
125 /* Use this for memory failure errors */
126 #define MEM_FAIL(condition, fmt, ...)					\
127 	DO_ONCE_LITE_IF(condition, pr_err, "ERROR: " fmt, ##__VA_ARGS__)
128 
129 #define FAULT_STRING "(fault)"
130 
131 #define HIST_STACKTRACE_DEPTH	16
132 #define HIST_STACKTRACE_SIZE	(HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
133 #define HIST_STACKTRACE_SKIP	5
134 
135 #define SYSCALL_FAULT_USER_MAX 165
136 
137 /*
138  * syscalls are special, and need special handling, this is why
139  * they are not included in trace_entries.h
140  */
141 struct syscall_trace_enter {
142 	struct trace_entry	ent;
143 	int			nr;
144 	unsigned long		args[];
145 };
146 
147 struct syscall_trace_exit {
148 	struct trace_entry	ent;
149 	int			nr;
150 	long			ret;
151 };
152 
153 struct kprobe_trace_entry_head {
154 	struct trace_entry	ent;
155 	unsigned long		ip;
156 };
157 
158 struct eprobe_trace_entry_head {
159 	struct trace_entry	ent;
160 };
161 
162 struct kretprobe_trace_entry_head {
163 	struct trace_entry	ent;
164 	unsigned long		func;
165 	unsigned long		ret_ip;
166 };
167 
168 struct fentry_trace_entry_head {
169 	struct trace_entry	ent;
170 	unsigned long		ip;
171 };
172 
173 struct fexit_trace_entry_head {
174 	struct trace_entry	ent;
175 	unsigned long		func;
176 	unsigned long		ret_ip;
177 };
178 
179 #define TRACE_BUF_SIZE		1024
180 
181 struct trace_array;
182 
183 /*
184  * The CPU trace array - it consists of thousands of trace entries
185  * plus some other descriptor data: (for example which task started
186  * the trace, etc.)
187  */
188 struct trace_array_cpu {
189 	local_t			disabled;
190 
191 	unsigned long		entries;
192 	unsigned long		saved_latency;
193 	unsigned long		critical_start;
194 	unsigned long		critical_end;
195 	unsigned long		critical_sequence;
196 	unsigned long		nice;
197 	unsigned long		policy;
198 	unsigned long		rt_priority;
199 	unsigned long		skipped_entries;
200 	u64			preempt_timestamp;
201 	pid_t			pid;
202 	kuid_t			uid;
203 	char			comm[TASK_COMM_LEN];
204 
205 #ifdef CONFIG_FUNCTION_TRACER
206 	int			ftrace_ignore_pid;
207 #endif
208 	bool			ignore_pid;
209 };
210 
211 struct tracer;
212 struct trace_option_dentry;
213 
214 struct array_buffer {
215 	struct trace_array		*tr;
216 	struct trace_buffer		*buffer;
217 	struct trace_array_cpu __percpu	*data;
218 	u64				time_start;
219 	int				cpu;
220 };
221 
222 #define TRACE_FLAGS_MAX_SIZE		64
223 
224 struct trace_options {
225 	struct tracer			*tracer;
226 	struct trace_option_dentry	*topts;
227 };
228 
229 struct trace_pid_list *trace_pid_list_alloc(void);
230 void trace_pid_list_free(struct trace_pid_list *pid_list);
231 bool trace_pid_list_is_set(struct trace_pid_list *pid_list, unsigned int pid);
232 int trace_pid_list_set(struct trace_pid_list *pid_list, unsigned int pid);
233 int trace_pid_list_clear(struct trace_pid_list *pid_list, unsigned int pid);
234 int trace_pid_list_first(struct trace_pid_list *pid_list, unsigned int *pid);
235 int trace_pid_list_next(struct trace_pid_list *pid_list, unsigned int pid,
236 			unsigned int *next);
237 
238 enum {
239 	TRACE_PIDS		= BIT(0),
240 	TRACE_NO_PIDS		= BIT(1),
241 };
242 
243 static inline bool pid_type_enabled(int type, struct trace_pid_list *pid_list,
244 				    struct trace_pid_list *no_pid_list)
245 {
246 	/* Return true if the pid list in type has pids */
247 	return ((type & TRACE_PIDS) && pid_list) ||
248 		((type & TRACE_NO_PIDS) && no_pid_list);
249 }
250 
251 static inline bool still_need_pid_events(int type, struct trace_pid_list *pid_list,
252 					 struct trace_pid_list *no_pid_list)
253 {
254 	/*
255 	 * Turning off what is in @type, return true if the "other"
256 	 * pid list, still has pids in it.
257 	 */
258 	return (!(type & TRACE_PIDS) && pid_list) ||
259 		(!(type & TRACE_NO_PIDS) && no_pid_list);
260 }
261 
262 typedef bool (*cond_update_fn_t)(struct trace_array *tr, void *cond_data);
263 
264 /**
265  * struct cond_snapshot - conditional snapshot data and callback
266  *
267  * The cond_snapshot structure encapsulates a callback function and
268  * data associated with the snapshot for a given tracing instance.
269  *
270  * When a snapshot is taken conditionally, by invoking
271  * tracing_snapshot_cond(tr, cond_data), the cond_data passed in is
272  * passed in turn to the cond_snapshot.update() function.  That data
273  * can be compared by the update() implementation with the cond_data
274  * contained within the struct cond_snapshot instance associated with
275  * the trace_array.  Because the tr->max_lock is held throughout the
276  * update() call, the update() function can directly retrieve the
277  * cond_snapshot and cond_data associated with the per-instance
278  * snapshot associated with the trace_array.
279  *
280  * The cond_snapshot.update() implementation can save data to be
281  * associated with the snapshot if it decides to, and returns 'true'
282  * in that case, or it returns 'false' if the conditional snapshot
283  * shouldn't be taken.
284  *
285  * The cond_snapshot instance is created and associated with the
286  * user-defined cond_data by tracing_cond_snapshot_enable().
287  * Likewise, the cond_snapshot instance is destroyed and is no longer
288  * associated with the trace instance by
289  * tracing_cond_snapshot_disable().
290  *
291  * The method below is required.
292  *
293  * @update: When a conditional snapshot is invoked, the update()
294  *	callback function is invoked with the tr->max_lock held.  The
295  *	update() implementation signals whether or not to actually
296  *	take the snapshot, by returning 'true' if so, 'false' if no
297  *	snapshot should be taken.  Because the max_lock is held for
298  *	the duration of update(), the implementation is safe to
299  *	directly retrieved and save any implementation data it needs
300  *	to in association with the snapshot.
301  */
302 struct cond_snapshot {
303 	void				*cond_data;
304 	cond_update_fn_t		update;
305 };
306 
307 /*
308  * struct trace_func_repeats - used to keep track of the consecutive
309  * (on the same CPU) calls of a single function.
310  */
311 struct trace_func_repeats {
312 	unsigned long	ip;
313 	unsigned long	parent_ip;
314 	unsigned long	count;
315 	u64		ts_last_call;
316 };
317 
318 struct trace_module_delta {
319 	struct rcu_head	rcu;
320 	long		delta[];
321 };
322 
323 /*
324  * The trace array - an array of per-CPU trace arrays. This is the
325  * highest level data structure that individual tracers deal with.
326  * They have on/off state as well:
327  */
328 struct trace_array {
329 	struct list_head	list;
330 	char			*name;
331 	struct array_buffer	array_buffer;
332 #ifdef CONFIG_TRACER_MAX_TRACE
333 	/*
334 	 * The max_buffer is used to snapshot the trace when a maximum
335 	 * latency is reached, or when the user initiates a snapshot.
336 	 * Some tracers will use this to store a maximum trace while
337 	 * it continues examining live traces.
338 	 *
339 	 * The buffers for the max_buffer are set up the same as the array_buffer
340 	 * When a snapshot is taken, the buffer of the max_buffer is swapped
341 	 * with the buffer of the array_buffer and the buffers are reset for
342 	 * the array_buffer so the tracing can continue.
343 	 */
344 	struct array_buffer	max_buffer;
345 	bool			allocated_snapshot;
346 	spinlock_t		snapshot_trigger_lock;
347 	unsigned int		snapshot;
348 	unsigned long		max_latency;
349 #ifdef CONFIG_FSNOTIFY
350 	struct dentry		*d_max_latency;
351 	struct work_struct	fsnotify_work;
352 	struct irq_work		fsnotify_irqwork;
353 #endif
354 #endif
355 	/* The below is for memory mapped ring buffer */
356 	unsigned int		mapped;
357 	unsigned long		range_addr_start;
358 	unsigned long		range_addr_size;
359 	char			*range_name;
360 	long			text_delta;
361 	struct trace_module_delta *module_delta;
362 	void			*scratch; /* pointer in persistent memory */
363 	int			scratch_size;
364 
365 	int			buffer_disabled;
366 
367 	struct trace_pid_list	__rcu *filtered_pids;
368 	struct trace_pid_list	__rcu *filtered_no_pids;
369 	/*
370 	 * max_lock is used to protect the swapping of buffers
371 	 * when taking a max snapshot. The buffers themselves are
372 	 * protected by per_cpu spinlocks. But the action of the swap
373 	 * needs its own lock.
374 	 *
375 	 * This is defined as a arch_spinlock_t in order to help
376 	 * with performance when lockdep debugging is enabled.
377 	 *
378 	 * It is also used in other places outside the update_max_tr
379 	 * so it needs to be defined outside of the
380 	 * CONFIG_TRACER_MAX_TRACE.
381 	 */
382 	arch_spinlock_t		max_lock;
383 #ifdef CONFIG_FTRACE_SYSCALLS
384 	int			sys_refcount_enter;
385 	int			sys_refcount_exit;
386 	struct trace_event_file	*enter_syscall_files[NR_syscalls];
387 	struct trace_event_file	*exit_syscall_files[NR_syscalls];
388 #endif
389 	int			stop_count;
390 	int			clock_id;
391 	int			nr_topts;
392 	bool			clear_trace;
393 	int			buffer_percent;
394 	unsigned int		n_err_log_entries;
395 	struct tracer		*current_trace;
396 	struct tracer_flags	*current_trace_flags;
397 	u64			trace_flags;
398 	unsigned char		trace_flags_index[TRACE_FLAGS_MAX_SIZE];
399 	unsigned int		flags;
400 	raw_spinlock_t		start_lock;
401 	const char		*system_names;
402 	struct list_head	err_log;
403 	struct dentry		*dir;
404 	struct dentry		*options;
405 	struct dentry		*percpu_dir;
406 	struct eventfs_inode	*event_dir;
407 	struct trace_options	*topts;
408 	struct list_head	systems;
409 	struct list_head	events;
410 	struct list_head	marker_list;
411 	struct list_head	tracers;
412 	struct trace_event_file *trace_marker_file;
413 	cpumask_var_t		tracing_cpumask; /* only trace on set CPUs */
414 	/* one per_cpu trace_pipe can be opened by only one user */
415 	cpumask_var_t		pipe_cpumask;
416 	int			ref;
417 	int			trace_ref;
418 #ifdef CONFIG_MODULES
419 	struct list_head	mod_events;
420 #endif
421 #ifdef CONFIG_FUNCTION_TRACER
422 	struct ftrace_ops	*ops;
423 	struct trace_pid_list	__rcu *function_pids;
424 	struct trace_pid_list	__rcu *function_no_pids;
425 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
426 	struct fgraph_ops	*gops;
427 #endif
428 #ifdef CONFIG_DYNAMIC_FTRACE
429 	/* All of these are protected by the ftrace_lock */
430 	struct list_head	func_probes;
431 	struct list_head	mod_trace;
432 	struct list_head	mod_notrace;
433 #endif
434 	/* function tracing enabled */
435 	int			function_enabled;
436 #endif
437 	int			no_filter_buffering_ref;
438 	unsigned int		syscall_buf_sz;
439 	struct list_head	hist_vars;
440 #ifdef CONFIG_TRACER_SNAPSHOT
441 	struct cond_snapshot	*cond_snapshot;
442 #endif
443 	struct trace_func_repeats	__percpu *last_func_repeats;
444 	/*
445 	 * On boot up, the ring buffer is set to the minimum size, so that
446 	 * we do not waste memory on systems that are not using tracing.
447 	 */
448 	bool ring_buffer_expanded;
449 };
450 
451 enum {
452 	TRACE_ARRAY_FL_GLOBAL		= BIT(0),
453 	TRACE_ARRAY_FL_BOOT		= BIT(1),
454 	TRACE_ARRAY_FL_LAST_BOOT	= BIT(2),
455 	TRACE_ARRAY_FL_MOD_INIT		= BIT(3),
456 	TRACE_ARRAY_FL_MEMMAP		= BIT(4),
457 	TRACE_ARRAY_FL_VMALLOC		= BIT(5),
458 };
459 
460 #ifdef CONFIG_MODULES
461 bool module_exists(const char *module);
462 #else
463 static inline bool module_exists(const char *module)
464 {
465 	return false;
466 }
467 #endif
468 
469 extern struct list_head ftrace_trace_arrays;
470 
471 extern struct mutex trace_types_lock;
472 
473 extern int trace_array_get(struct trace_array *tr);
474 extern int tracing_check_open_get_tr(struct trace_array *tr);
475 extern struct trace_array *trace_array_find(const char *instance);
476 extern struct trace_array *trace_array_find_get(const char *instance);
477 
478 extern u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe);
479 extern int tracing_set_filter_buffering(struct trace_array *tr, bool set);
480 extern int tracing_set_clock(struct trace_array *tr, const char *clockstr);
481 
482 extern bool trace_clock_in_ns(struct trace_array *tr);
483 
484 extern unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr);
485 
486 /*
487  * The global tracer (top) should be the first trace array added,
488  * but we check the flag anyway.
489  */
490 static inline struct trace_array *top_trace_array(void)
491 {
492 	struct trace_array *tr;
493 
494 	if (list_empty(&ftrace_trace_arrays))
495 		return NULL;
496 
497 	tr = list_entry(ftrace_trace_arrays.prev,
498 			typeof(*tr), list);
499 	WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
500 	return tr;
501 }
502 
503 #define FTRACE_CMP_TYPE(var, type) \
504 	__builtin_types_compatible_p(typeof(var), type *)
505 
506 #undef IF_ASSIGN
507 #define IF_ASSIGN(var, entry, etype, id)			\
508 	if (FTRACE_CMP_TYPE(var, etype)) {			\
509 		var = (typeof(var))(entry);			\
510 		WARN_ON(id != 0 && (entry)->type != id);	\
511 		break;						\
512 	}
513 
514 /* Will cause compile errors if type is not found. */
515 extern void __ftrace_bad_type(void);
516 
517 /*
518  * The trace_assign_type is a verifier that the entry type is
519  * the same as the type being assigned. To add new types simply
520  * add a line with the following format:
521  *
522  * IF_ASSIGN(var, ent, type, id);
523  *
524  *  Where "type" is the trace type that includes the trace_entry
525  *  as the "ent" item. And "id" is the trace identifier that is
526  *  used in the trace_type enum.
527  *
528  *  If the type can have more than one id, then use zero.
529  */
530 #define trace_assign_type(var, ent)					\
531 	do {								\
532 		IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN);	\
533 		IF_ASSIGN(var, ent, struct ctx_switch_entry, 0);	\
534 		IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK);	\
535 		IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
536 		IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT);	\
537 		IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT);	\
538 		IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS);	\
539 		IF_ASSIGN(var, ent, struct hwlat_entry, TRACE_HWLAT);	\
540 		IF_ASSIGN(var, ent, struct osnoise_entry, TRACE_OSNOISE);\
541 		IF_ASSIGN(var, ent, struct timerlat_entry, TRACE_TIMERLAT);\
542 		IF_ASSIGN(var, ent, struct raw_data_entry, TRACE_RAW_DATA);\
543 		IF_ASSIGN(var, ent, struct trace_mmiotrace_rw,		\
544 			  TRACE_MMIO_RW);				\
545 		IF_ASSIGN(var, ent, struct trace_mmiotrace_map,		\
546 			  TRACE_MMIO_MAP);				\
547 		IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
548 		IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry,	\
549 			  TRACE_GRAPH_ENT);		\
550 		IF_ASSIGN(var, ent, struct fgraph_retaddr_ent_entry,\
551 			  TRACE_GRAPH_RETADDR_ENT);		\
552 		IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry,	\
553 			  TRACE_GRAPH_RET);		\
554 		IF_ASSIGN(var, ent, struct func_repeats_entry,		\
555 			  TRACE_FUNC_REPEATS);				\
556 		__ftrace_bad_type();					\
557 	} while (0)
558 
559 /*
560  * An option specific to a tracer. This is a boolean value.
561  * The bit is the bit index that sets its value on the
562  * flags value in struct tracer_flags.
563  */
564 struct tracer_opt {
565 	const char	*name; /* Will appear on the trace_options file */
566 	u32		bit; /* Mask assigned in val field in tracer_flags */
567 };
568 
569 /*
570  * The set of specific options for a tracer. Your tracer
571  * have to set the initial value of the flags val.
572  */
573 struct tracer_flags {
574 	u32			val;
575 	struct tracer_opt	*opts;
576 	struct tracer		*trace;
577 };
578 
579 /* Makes more easy to define a tracer opt */
580 #define TRACER_OPT(s, b)	.name = #s, .bit = b
581 
582 
583 struct trace_option_dentry {
584 	struct tracer_opt		*opt;
585 	struct tracer_flags		*flags;
586 	struct trace_array		*tr;
587 	struct dentry			*entry;
588 };
589 
590 /**
591  * struct tracer - a specific tracer and its callbacks to interact with tracefs
592  * @name: the name chosen to select it on the available_tracers file
593  * @init: called when one switches to this tracer (echo name > current_tracer)
594  * @reset: called when one switches to another tracer
595  * @start: called when tracing is unpaused (echo 1 > tracing_on)
596  * @stop: called when tracing is paused (echo 0 > tracing_on)
597  * @update_thresh: called when tracing_thresh is updated
598  * @open: called when the trace file is opened
599  * @pipe_open: called when the trace_pipe file is opened
600  * @close: called when the trace file is released
601  * @pipe_close: called when the trace_pipe file is released
602  * @read: override the default read callback on trace_pipe
603  * @splice_read: override the default splice_read callback on trace_pipe
604  * @selftest: selftest to run on boot (see trace_selftest.c)
605  * @print_headers: override the first lines that describe your columns
606  * @print_line: callback that prints a trace
607  * @set_flag: signals one of your private flags changed (trace_options file)
608  * @flags: your private flags
609  */
610 struct tracer {
611 	const char		*name;
612 	int			(*init)(struct trace_array *tr);
613 	void			(*reset)(struct trace_array *tr);
614 	void			(*start)(struct trace_array *tr);
615 	void			(*stop)(struct trace_array *tr);
616 	int			(*update_thresh)(struct trace_array *tr);
617 	void			(*open)(struct trace_iterator *iter);
618 	void			(*pipe_open)(struct trace_iterator *iter);
619 	void			(*close)(struct trace_iterator *iter);
620 	void			(*pipe_close)(struct trace_iterator *iter);
621 	ssize_t			(*read)(struct trace_iterator *iter,
622 					struct file *filp, char __user *ubuf,
623 					size_t cnt, loff_t *ppos);
624 	ssize_t			(*splice_read)(struct trace_iterator *iter,
625 					       struct file *filp,
626 					       loff_t *ppos,
627 					       struct pipe_inode_info *pipe,
628 					       size_t len,
629 					       unsigned int flags);
630 #ifdef CONFIG_FTRACE_STARTUP_TEST
631 	int			(*selftest)(struct tracer *trace,
632 					    struct trace_array *tr);
633 #endif
634 	void			(*print_header)(struct seq_file *m);
635 	enum print_line_t	(*print_line)(struct trace_iterator *iter);
636 	/* If you handled the flag setting, return 0 */
637 	int			(*set_flag)(struct trace_array *tr,
638 					    u32 old_flags, u32 bit, int set);
639 	/* Return 0 if OK with change, else return non-zero */
640 	int			(*flag_changed)(struct trace_array *tr,
641 						u64 mask, int set);
642 	struct tracer		*next;
643 	struct tracer_flags	*flags;
644 	struct tracer_flags	*default_flags;
645 	int			enabled;
646 	bool			print_max;
647 	bool			allow_instances;
648 #ifdef CONFIG_TRACER_MAX_TRACE
649 	bool			use_max_tr;
650 #endif
651 	/* True if tracer cannot be enabled in kernel param */
652 	bool			noboot;
653 };
654 
655 static inline struct ring_buffer_iter *
656 trace_buffer_iter(struct trace_iterator *iter, int cpu)
657 {
658 	return iter->buffer_iter ? iter->buffer_iter[cpu] : NULL;
659 }
660 
661 int tracer_init(struct tracer *t, struct trace_array *tr);
662 int tracing_is_enabled(void);
663 void tracing_reset_online_cpus(struct array_buffer *buf);
664 void tracing_reset_all_online_cpus(void);
665 void tracing_reset_all_online_cpus_unlocked(void);
666 int tracing_open_generic(struct inode *inode, struct file *filp);
667 int tracing_open_generic_tr(struct inode *inode, struct file *filp);
668 int tracing_release_generic_tr(struct inode *inode, struct file *file);
669 int tracing_open_file_tr(struct inode *inode, struct file *filp);
670 int tracing_release_file_tr(struct inode *inode, struct file *filp);
671 int tracing_single_release_file_tr(struct inode *inode, struct file *filp);
672 bool tracing_is_disabled(void);
673 bool tracer_tracing_is_on(struct trace_array *tr);
674 void tracer_tracing_on(struct trace_array *tr);
675 void tracer_tracing_off(struct trace_array *tr);
676 void tracer_tracing_disable(struct trace_array *tr);
677 void tracer_tracing_enable(struct trace_array *tr);
678 struct dentry *trace_create_file(const char *name,
679 				 umode_t mode,
680 				 struct dentry *parent,
681 				 void *data,
682 				 const struct file_operations *fops);
683 
684 
685 /**
686  * tracer_tracing_is_on_cpu - show real state of ring buffer enabled on for a cpu
687  * @tr : the trace array to know if ring buffer is enabled
688  * @cpu: The cpu buffer to check if enabled
689  *
690  * Shows real state of the per CPU buffer if it is enabled or not.
691  */
692 static inline bool tracer_tracing_is_on_cpu(struct trace_array *tr, int cpu)
693 {
694 	if (tr->array_buffer.buffer)
695 		return ring_buffer_record_is_on_cpu(tr->array_buffer.buffer, cpu);
696 	return false;
697 }
698 
699 int tracing_init_dentry(void);
700 
701 struct ring_buffer_event;
702 
703 struct ring_buffer_event *
704 trace_buffer_lock_reserve(struct trace_buffer *buffer,
705 			  int type,
706 			  unsigned long len,
707 			  unsigned int trace_ctx);
708 
709 int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, int cpu);
710 
711 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
712 						struct trace_array_cpu *data);
713 
714 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
715 					  int *ent_cpu, u64 *ent_ts);
716 
717 void trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
718 					struct ring_buffer_event *event);
719 
720 bool trace_is_tracepoint_string(const char *str);
721 const char *trace_event_format(struct trace_iterator *iter, const char *fmt);
722 char *trace_iter_expand_format(struct trace_iterator *iter);
723 bool ignore_event(struct trace_iterator *iter);
724 
725 int trace_empty(struct trace_iterator *iter);
726 
727 void *trace_find_next_entry_inc(struct trace_iterator *iter);
728 
729 void trace_init_global_iter(struct trace_iterator *iter);
730 
731 void tracing_iter_reset(struct trace_iterator *iter, int cpu);
732 
733 unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu);
734 unsigned long trace_total_entries(struct trace_array *tr);
735 
736 void trace_function(struct trace_array *tr,
737 		    unsigned long ip,
738 		    unsigned long parent_ip,
739 		    unsigned int trace_ctx,
740 		    struct ftrace_regs *regs);
741 void trace_graph_function(struct trace_array *tr,
742 		    unsigned long ip,
743 		    unsigned long parent_ip,
744 		    unsigned int trace_ctx);
745 void trace_latency_header(struct seq_file *m);
746 void trace_default_header(struct seq_file *m);
747 void print_trace_header(struct seq_file *m, struct trace_iterator *iter);
748 
749 void trace_graph_return(struct ftrace_graph_ret *trace, struct fgraph_ops *gops,
750 			struct ftrace_regs *fregs);
751 int trace_graph_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *gops,
752 		      struct ftrace_regs *fregs);
753 
754 void tracing_start_cmdline_record(void);
755 void tracing_stop_cmdline_record(void);
756 void tracing_start_tgid_record(void);
757 void tracing_stop_tgid_record(void);
758 
759 int register_tracer(struct tracer *type);
760 int is_tracing_stopped(void);
761 
762 loff_t tracing_lseek(struct file *file, loff_t offset, int whence);
763 
764 extern cpumask_var_t __read_mostly tracing_buffer_mask;
765 
766 #define for_each_tracing_cpu(cpu)	\
767 	for_each_cpu(cpu, tracing_buffer_mask)
768 
769 extern unsigned long nsecs_to_usecs(unsigned long nsecs);
770 
771 extern unsigned long tracing_thresh;
772 
773 /* PID filtering */
774 
775 bool trace_find_filtered_pid(struct trace_pid_list *filtered_pids,
776 			     pid_t search_pid);
777 bool trace_ignore_this_task(struct trace_pid_list *filtered_pids,
778 			    struct trace_pid_list *filtered_no_pids,
779 			    struct task_struct *task);
780 void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
781 				  struct task_struct *self,
782 				  struct task_struct *task);
783 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos);
784 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos);
785 int trace_pid_show(struct seq_file *m, void *v);
786 int trace_pid_write(struct trace_pid_list *filtered_pids,
787 		    struct trace_pid_list **new_pid_list,
788 		    const char __user *ubuf, size_t cnt);
789 
790 #ifdef CONFIG_TRACER_MAX_TRACE
791 void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
792 		   void *cond_data);
793 void update_max_tr_single(struct trace_array *tr,
794 			  struct task_struct *tsk, int cpu);
795 
796 #ifdef CONFIG_FSNOTIFY
797 #define LATENCY_FS_NOTIFY
798 #endif
799 #endif /* CONFIG_TRACER_MAX_TRACE */
800 
801 #ifdef LATENCY_FS_NOTIFY
802 void latency_fsnotify(struct trace_array *tr);
803 #else
804 static inline void latency_fsnotify(struct trace_array *tr) { }
805 #endif
806 
807 #ifdef CONFIG_STACKTRACE
808 void __trace_stack(struct trace_array *tr, unsigned int trace_ctx, int skip);
809 #else
810 static inline void __trace_stack(struct trace_array *tr, unsigned int trace_ctx,
811 				 int skip)
812 {
813 }
814 #endif /* CONFIG_STACKTRACE */
815 
816 void trace_last_func_repeats(struct trace_array *tr,
817 			     struct trace_func_repeats *last_info,
818 			     unsigned int trace_ctx);
819 
820 extern u64 ftrace_now(int cpu);
821 
822 extern void trace_find_cmdline(int pid, char comm[]);
823 extern int trace_find_tgid(int pid);
824 extern void trace_event_follow_fork(struct trace_array *tr, bool enable);
825 
826 extern int trace_events_enabled(struct trace_array *tr, const char *system);
827 
828 #ifdef CONFIG_DYNAMIC_FTRACE
829 extern unsigned long ftrace_update_tot_cnt;
830 extern unsigned long ftrace_number_of_pages;
831 extern unsigned long ftrace_number_of_groups;
832 extern u64 ftrace_update_time;
833 extern u64 ftrace_total_mod_time;
834 void ftrace_init_trace_array(struct trace_array *tr);
835 #else
836 static inline void ftrace_init_trace_array(struct trace_array *tr) { }
837 #endif
838 #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
839 extern int DYN_FTRACE_TEST_NAME(void);
840 #define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2
841 extern int DYN_FTRACE_TEST_NAME2(void);
842 
843 extern void trace_set_ring_buffer_expanded(struct trace_array *tr);
844 extern bool tracing_selftest_disabled;
845 
846 #ifdef CONFIG_FTRACE_STARTUP_TEST
847 extern void __init disable_tracing_selftest(const char *reason);
848 
849 extern int trace_selftest_startup_function(struct tracer *trace,
850 					   struct trace_array *tr);
851 extern int trace_selftest_startup_function_graph(struct tracer *trace,
852 						 struct trace_array *tr);
853 extern int trace_selftest_startup_irqsoff(struct tracer *trace,
854 					  struct trace_array *tr);
855 extern int trace_selftest_startup_preemptoff(struct tracer *trace,
856 					     struct trace_array *tr);
857 extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
858 						 struct trace_array *tr);
859 extern int trace_selftest_startup_wakeup(struct tracer *trace,
860 					 struct trace_array *tr);
861 extern int trace_selftest_startup_nop(struct tracer *trace,
862 					 struct trace_array *tr);
863 extern int trace_selftest_startup_branch(struct tracer *trace,
864 					 struct trace_array *tr);
865 /*
866  * Tracer data references selftest functions that only occur
867  * on boot up. These can be __init functions. Thus, when selftests
868  * are enabled, then the tracers need to reference __init functions.
869  */
870 #define __tracer_data		__refdata
871 #else
872 static inline void __init disable_tracing_selftest(const char *reason)
873 {
874 }
875 /* Tracers are seldom changed. Optimize when selftests are disabled. */
876 #define __tracer_data		__read_mostly
877 #endif /* CONFIG_FTRACE_STARTUP_TEST */
878 
879 extern void *head_page(struct trace_array_cpu *data);
880 extern unsigned long long ns2usecs(u64 nsec);
881 
882 __printf(2, 0)
883 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
884 __printf(2, 0)
885 int trace_vprintk(unsigned long ip, const char *fmt, va_list args);
886 __printf(3, 0)
887 int trace_array_vprintk(struct trace_array *tr,
888 			unsigned long ip, const char *fmt, va_list args);
889 __printf(3, 4)
890 int trace_array_printk_buf(struct trace_buffer *buffer,
891 			   unsigned long ip, const char *fmt, ...);
892 void trace_printk_seq(struct trace_seq *s);
893 enum print_line_t print_trace_line(struct trace_iterator *iter);
894 
895 extern char trace_find_mark(unsigned long long duration);
896 
897 struct ftrace_hash;
898 
899 struct ftrace_mod_load {
900 	struct list_head	list;
901 	char			*func;
902 	char			*module;
903 	int			 enable;
904 };
905 
906 enum {
907 	FTRACE_HASH_FL_MOD	= (1 << 0),
908 };
909 
910 struct ftrace_hash {
911 	unsigned long		size_bits;
912 	struct hlist_head	*buckets;
913 	unsigned long		count;
914 	unsigned long		flags;
915 	struct rcu_head		rcu;
916 };
917 
918 struct ftrace_func_entry *
919 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip);
920 
921 static __always_inline bool ftrace_hash_empty(struct ftrace_hash *hash)
922 {
923 	return !hash || !(hash->count || (hash->flags & FTRACE_HASH_FL_MOD));
924 }
925 
926 /* Standard output formatting function used for function return traces */
927 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
928 
929 /* Flag options */
930 #define TRACE_GRAPH_PRINT_OVERRUN       0x1
931 #define TRACE_GRAPH_PRINT_CPU           0x2
932 #define TRACE_GRAPH_PRINT_OVERHEAD      0x4
933 #define TRACE_GRAPH_PRINT_PROC          0x8
934 #define TRACE_GRAPH_PRINT_DURATION      0x10
935 #define TRACE_GRAPH_PRINT_ABS_TIME      0x20
936 #define TRACE_GRAPH_PRINT_REL_TIME      0x40
937 #define TRACE_GRAPH_PRINT_IRQS          0x80
938 #define TRACE_GRAPH_PRINT_TAIL          0x100
939 #define TRACE_GRAPH_SLEEP_TIME          0x200
940 #define TRACE_GRAPH_GRAPH_TIME          0x400
941 #define TRACE_GRAPH_PRINT_RETVAL        0x800
942 #define TRACE_GRAPH_PRINT_RETVAL_HEX    0x1000
943 #define TRACE_GRAPH_PRINT_RETADDR       0x2000
944 #define TRACE_GRAPH_ARGS		0x4000
945 #define TRACE_GRAPH_PRINT_FILL_SHIFT	28
946 #define TRACE_GRAPH_PRINT_FILL_MASK	(0x3 << TRACE_GRAPH_PRINT_FILL_SHIFT)
947 
948 #ifdef CONFIG_FUNCTION_PROFILER
949 extern void ftrace_graph_graph_time_control(bool enable);
950 #else
951 static inline void ftrace_graph_graph_time_control(bool enable) { }
952 #endif
953 
954 extern enum print_line_t
955 print_graph_function_flags(struct trace_iterator *iter, u32 flags);
956 extern void print_graph_headers_flags(struct seq_file *s, u32 flags);
957 extern void
958 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
959 extern void graph_trace_open(struct trace_iterator *iter);
960 extern void graph_trace_close(struct trace_iterator *iter);
961 extern int __trace_graph_entry(struct trace_array *tr,
962 			       struct ftrace_graph_ent *trace,
963 			       unsigned int trace_ctx);
964 extern int __trace_graph_retaddr_entry(struct trace_array *tr,
965 				struct ftrace_graph_ent *trace,
966 				unsigned int trace_ctx,
967 				unsigned long retaddr,
968 				struct ftrace_regs *fregs);
969 extern void __trace_graph_return(struct trace_array *tr,
970 				 struct ftrace_graph_ret *trace,
971 				 unsigned int trace_ctx,
972 				 u64 calltime, u64 rettime);
973 
974 extern void init_array_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops);
975 extern int allocate_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops);
976 extern void free_fgraph_ops(struct trace_array *tr);
977 
978 enum {
979 	TRACE_GRAPH_FL		= 1,
980 
981 	/*
982 	 * In the very unlikely case that an interrupt came in
983 	 * at a start of graph tracing, and we want to trace
984 	 * the function in that interrupt, the depth can be greater
985 	 * than zero, because of the preempted start of a previous
986 	 * trace. In an even more unlikely case, depth could be 2
987 	 * if a softirq interrupted the start of graph tracing,
988 	 * followed by an interrupt preempting a start of graph
989 	 * tracing in the softirq, and depth can even be 3
990 	 * if an NMI came in at the start of an interrupt function
991 	 * that preempted a softirq start of a function that
992 	 * preempted normal context!!!! Luckily, it can't be
993 	 * greater than 3, so the next two bits are a mask
994 	 * of what the depth is when we set TRACE_GRAPH_FL
995 	 */
996 
997 	TRACE_GRAPH_DEPTH_START_BIT,
998 	TRACE_GRAPH_DEPTH_END_BIT,
999 
1000 	/*
1001 	 * To implement set_graph_notrace, if this bit is set, we ignore
1002 	 * function graph tracing of called functions, until the return
1003 	 * function is called to clear it.
1004 	 */
1005 	TRACE_GRAPH_NOTRACE_BIT,
1006 };
1007 
1008 #define TRACE_GRAPH_NOTRACE		(1 << TRACE_GRAPH_NOTRACE_BIT)
1009 
1010 static inline unsigned long ftrace_graph_depth(unsigned long *task_var)
1011 {
1012 	return (*task_var >> TRACE_GRAPH_DEPTH_START_BIT) & 3;
1013 }
1014 
1015 static inline void ftrace_graph_set_depth(unsigned long *task_var, int depth)
1016 {
1017 	*task_var &= ~(3 << TRACE_GRAPH_DEPTH_START_BIT);
1018 	*task_var |= (depth & 3) << TRACE_GRAPH_DEPTH_START_BIT;
1019 }
1020 
1021 #ifdef CONFIG_DYNAMIC_FTRACE
1022 extern struct ftrace_hash __rcu *ftrace_graph_hash;
1023 extern struct ftrace_hash __rcu *ftrace_graph_notrace_hash;
1024 
1025 static inline int
1026 ftrace_graph_addr(unsigned long *task_var, struct ftrace_graph_ent *trace)
1027 {
1028 	unsigned long addr = trace->func;
1029 	int ret = 0;
1030 	struct ftrace_hash *hash;
1031 
1032 	preempt_disable_notrace();
1033 
1034 	/*
1035 	 * Have to open code "rcu_dereference_sched()" because the
1036 	 * function graph tracer can be called when RCU is not
1037 	 * "watching".
1038 	 * Protected with schedule_on_each_cpu(ftrace_sync)
1039 	 */
1040 	hash = rcu_dereference_protected(ftrace_graph_hash, !preemptible());
1041 
1042 	if (ftrace_hash_empty(hash)) {
1043 		ret = 1;
1044 		goto out;
1045 	}
1046 
1047 	if (ftrace_lookup_ip(hash, addr)) {
1048 		/*
1049 		 * This needs to be cleared on the return functions
1050 		 * when the depth is zero.
1051 		 */
1052 		*task_var |= TRACE_GRAPH_FL;
1053 		ftrace_graph_set_depth(task_var, trace->depth);
1054 
1055 		/*
1056 		 * If no irqs are to be traced, but a set_graph_function
1057 		 * is set, and called by an interrupt handler, we still
1058 		 * want to trace it.
1059 		 */
1060 		if (in_hardirq())
1061 			trace_recursion_set(TRACE_IRQ_BIT);
1062 		else
1063 			trace_recursion_clear(TRACE_IRQ_BIT);
1064 		ret = 1;
1065 	}
1066 
1067 out:
1068 	preempt_enable_notrace();
1069 	return ret;
1070 }
1071 
1072 static inline void
1073 ftrace_graph_addr_finish(struct fgraph_ops *gops, struct ftrace_graph_ret *trace)
1074 {
1075 	unsigned long *task_var = fgraph_get_task_var(gops);
1076 
1077 	if ((*task_var & TRACE_GRAPH_FL) &&
1078 	    trace->depth == ftrace_graph_depth(task_var))
1079 		*task_var &= ~TRACE_GRAPH_FL;
1080 }
1081 
1082 static inline int ftrace_graph_notrace_addr(unsigned long addr)
1083 {
1084 	int ret = 0;
1085 	struct ftrace_hash *notrace_hash;
1086 
1087 	preempt_disable_notrace();
1088 
1089 	/*
1090 	 * Have to open code "rcu_dereference_sched()" because the
1091 	 * function graph tracer can be called when RCU is not
1092 	 * "watching".
1093 	 * Protected with schedule_on_each_cpu(ftrace_sync)
1094 	 */
1095 	notrace_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
1096 						 !preemptible());
1097 
1098 	if (ftrace_lookup_ip(notrace_hash, addr))
1099 		ret = 1;
1100 
1101 	preempt_enable_notrace();
1102 	return ret;
1103 }
1104 #else
1105 static inline int ftrace_graph_addr(unsigned long *task_var, struct ftrace_graph_ent *trace)
1106 {
1107 	return 1;
1108 }
1109 
1110 static inline int ftrace_graph_notrace_addr(unsigned long addr)
1111 {
1112 	return 0;
1113 }
1114 static inline void ftrace_graph_addr_finish(struct fgraph_ops *gops, struct ftrace_graph_ret *trace)
1115 { }
1116 #endif /* CONFIG_DYNAMIC_FTRACE */
1117 
1118 extern unsigned int fgraph_max_depth;
1119 extern int fgraph_no_sleep_time;
1120 extern bool fprofile_no_sleep_time;
1121 
1122 static inline bool
1123 ftrace_graph_ignore_func(struct fgraph_ops *gops, struct ftrace_graph_ent *trace)
1124 {
1125 	unsigned long *task_var = fgraph_get_task_var(gops);
1126 
1127 	/* trace it when it is-nested-in or is a function enabled. */
1128 	return !((*task_var & TRACE_GRAPH_FL) ||
1129 		 ftrace_graph_addr(task_var, trace)) ||
1130 		(trace->depth < 0) ||
1131 		(fgraph_max_depth && trace->depth >= fgraph_max_depth);
1132 }
1133 
1134 void fgraph_init_ops(struct ftrace_ops *dst_ops,
1135 		     struct ftrace_ops *src_ops);
1136 
1137 #else /* CONFIG_FUNCTION_GRAPH_TRACER */
1138 static inline enum print_line_t
1139 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1140 {
1141 	return TRACE_TYPE_UNHANDLED;
1142 }
1143 static inline void free_fgraph_ops(struct trace_array *tr) { }
1144 /* ftrace_ops may not be defined */
1145 #define init_array_fgraph_ops(tr, ops) do { } while (0)
1146 #define allocate_fgraph_ops(tr, ops) ({ 0; })
1147 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1148 
1149 extern struct list_head ftrace_pids;
1150 
1151 #ifdef CONFIG_FUNCTION_TRACER
1152 
1153 #define FTRACE_PID_IGNORE	-1
1154 #define FTRACE_PID_TRACE	-2
1155 
1156 struct ftrace_func_command {
1157 	struct list_head	list;
1158 	char			*name;
1159 	int			(*func)(struct trace_array *tr,
1160 					struct ftrace_hash *hash,
1161 					char *func, char *cmd,
1162 					char *params, int enable);
1163 };
1164 extern bool ftrace_filter_param __initdata;
1165 extern int ftrace_is_dead(void);
1166 int ftrace_create_function_files(struct trace_array *tr,
1167 				 struct dentry *parent);
1168 void ftrace_destroy_function_files(struct trace_array *tr);
1169 int ftrace_allocate_ftrace_ops(struct trace_array *tr);
1170 void ftrace_free_ftrace_ops(struct trace_array *tr);
1171 void ftrace_init_global_array_ops(struct trace_array *tr);
1172 struct trace_array *trace_get_global_array(void);
1173 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func);
1174 void ftrace_reset_array_ops(struct trace_array *tr);
1175 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer);
1176 void ftrace_init_tracefs_toplevel(struct trace_array *tr,
1177 				  struct dentry *d_tracer);
1178 void ftrace_clear_pids(struct trace_array *tr);
1179 int init_function_trace(void);
1180 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable);
1181 #else
1182 static inline int ftrace_is_dead(void) { return 0; }
1183 static inline int
1184 ftrace_create_function_files(struct trace_array *tr,
1185 			     struct dentry *parent)
1186 {
1187 	return 0;
1188 }
1189 static inline int ftrace_allocate_ftrace_ops(struct trace_array *tr)
1190 {
1191 	return 0;
1192 }
1193 static inline void ftrace_free_ftrace_ops(struct trace_array *tr) { }
1194 static inline void ftrace_destroy_function_files(struct trace_array *tr) { }
1195 static inline __init void
1196 ftrace_init_global_array_ops(struct trace_array *tr) { }
1197 static inline void ftrace_reset_array_ops(struct trace_array *tr) { }
1198 static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { }
1199 static inline void ftrace_init_tracefs_toplevel(struct trace_array *tr, struct dentry *d) { }
1200 static inline void ftrace_clear_pids(struct trace_array *tr) { }
1201 static inline int init_function_trace(void) { return 0; }
1202 static inline void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) { }
1203 /* ftace_func_t type is not defined, use macro instead of static inline */
1204 #define ftrace_init_array_ops(tr, func) do { } while (0)
1205 #endif /* CONFIG_FUNCTION_TRACER */
1206 
1207 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
1208 
1209 struct ftrace_probe_ops {
1210 	void			(*func)(unsigned long ip,
1211 					unsigned long parent_ip,
1212 					struct trace_array *tr,
1213 					struct ftrace_probe_ops *ops,
1214 					void *data);
1215 	int			(*init)(struct ftrace_probe_ops *ops,
1216 					struct trace_array *tr,
1217 					unsigned long ip, void *init_data,
1218 					void **data);
1219 	void			(*free)(struct ftrace_probe_ops *ops,
1220 					struct trace_array *tr,
1221 					unsigned long ip, void *data);
1222 	int			(*print)(struct seq_file *m,
1223 					 unsigned long ip,
1224 					 struct ftrace_probe_ops *ops,
1225 					 void *data);
1226 };
1227 
1228 struct ftrace_func_mapper;
1229 typedef int (*ftrace_mapper_func)(void *data);
1230 
1231 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void);
1232 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
1233 					   unsigned long ip);
1234 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
1235 			       unsigned long ip, void *data);
1236 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
1237 				   unsigned long ip);
1238 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
1239 			     ftrace_mapper_func free_func);
1240 
1241 extern int
1242 register_ftrace_function_probe(char *glob, struct trace_array *tr,
1243 			       struct ftrace_probe_ops *ops, void *data);
1244 extern int
1245 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
1246 				      struct ftrace_probe_ops *ops);
1247 extern void clear_ftrace_function_probes(struct trace_array *tr);
1248 
1249 int register_ftrace_command(struct ftrace_func_command *cmd);
1250 int unregister_ftrace_command(struct ftrace_func_command *cmd);
1251 
1252 void ftrace_create_filter_files(struct ftrace_ops *ops,
1253 				struct dentry *parent);
1254 void ftrace_destroy_filter_files(struct ftrace_ops *ops);
1255 
1256 extern int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
1257 			     int len, int reset);
1258 extern int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
1259 			      int len, int reset);
1260 #else
1261 struct ftrace_func_command;
1262 
1263 static inline __init int register_ftrace_command(struct ftrace_func_command *cmd)
1264 {
1265 	return -EINVAL;
1266 }
1267 static inline __init int unregister_ftrace_command(char *cmd_name)
1268 {
1269 	return -EINVAL;
1270 }
1271 static inline void clear_ftrace_function_probes(struct trace_array *tr)
1272 {
1273 }
1274 
1275 /*
1276  * The ops parameter passed in is usually undefined.
1277  * This must be a macro.
1278  */
1279 #define ftrace_create_filter_files(ops, parent) do { } while (0)
1280 #define ftrace_destroy_filter_files(ops) do { } while (0)
1281 #endif /* CONFIG_FUNCTION_TRACER && CONFIG_DYNAMIC_FTRACE */
1282 
1283 bool ftrace_event_is_function(struct trace_event_call *call);
1284 
1285 /*
1286  * struct trace_parser - servers for reading the user input separated by spaces
1287  * @cont: set if the input is not complete - no final space char was found
1288  * @buffer: holds the parsed user input
1289  * @idx: user input length
1290  * @size: buffer size
1291  */
1292 struct trace_parser {
1293 	bool		cont;
1294 	bool		fail;
1295 	char		*buffer;
1296 	unsigned	idx;
1297 	unsigned	size;
1298 };
1299 
1300 static inline bool trace_parser_loaded(struct trace_parser *parser)
1301 {
1302 	return !parser->fail && parser->idx != 0;
1303 }
1304 
1305 static inline bool trace_parser_cont(struct trace_parser *parser)
1306 {
1307 	return parser->cont;
1308 }
1309 
1310 static inline void trace_parser_clear(struct trace_parser *parser)
1311 {
1312 	parser->cont = false;
1313 	parser->idx = 0;
1314 }
1315 
1316 static inline void trace_parser_fail(struct trace_parser *parser)
1317 {
1318 	parser->fail = true;
1319 }
1320 
1321 extern int trace_parser_get_init(struct trace_parser *parser, int size);
1322 extern void trace_parser_put(struct trace_parser *parser);
1323 extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1324 	size_t cnt, loff_t *ppos);
1325 
1326 /*
1327  * Only create function graph options if function graph is configured.
1328  */
1329 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1330 # define FGRAPH_FLAGS						\
1331 		C(DISPLAY_GRAPH,	"display-graph"),
1332 #else
1333 # define FGRAPH_FLAGS
1334 #endif
1335 
1336 #ifdef CONFIG_BRANCH_TRACER
1337 # define BRANCH_FLAGS					\
1338 		C(BRANCH,		"branch"),
1339 #else
1340 # define BRANCH_FLAGS
1341 #endif
1342 
1343 #ifdef CONFIG_FUNCTION_TRACER
1344 # define FUNCTION_FLAGS						\
1345 		C(FUNCTION,		"function-trace"),	\
1346 		C(FUNC_FORK,		"function-fork"),
1347 # define FUNCTION_DEFAULT_FLAGS		TRACE_ITER(FUNCTION)
1348 #else
1349 # define FUNCTION_FLAGS
1350 # define FUNCTION_DEFAULT_FLAGS		0UL
1351 # define TRACE_ITER_FUNC_FORK_BIT	-1
1352 #endif
1353 
1354 #ifdef CONFIG_STACKTRACE
1355 # define STACK_FLAGS				\
1356 		C(STACKTRACE,		"stacktrace"),
1357 #else
1358 # define STACK_FLAGS
1359 #endif
1360 
1361 #ifdef CONFIG_FUNCTION_PROFILER
1362 # define PROFILER_FLAGS					\
1363 		C(PROF_TEXT_OFFSET,	"prof-text-offset"),
1364 # ifdef CONFIG_FUNCTION_GRAPH_TRACER
1365 #  define FPROFILE_FLAGS				\
1366 		C(GRAPH_TIME,		"graph-time"),
1367 #  define FPROFILE_DEFAULT_FLAGS	TRACE_ITER(GRAPH_TIME)
1368 # else
1369 #  define FPROFILE_FLAGS
1370 #  define FPROFILE_DEFAULT_FLAGS	0UL
1371 # endif
1372 #else
1373 # define PROFILER_FLAGS
1374 # define FPROFILE_FLAGS
1375 # define FPROFILE_DEFAULT_FLAGS			0UL
1376 # define TRACE_ITER_PROF_TEXT_OFFSET_BIT	-1
1377 #endif
1378 
1379 /*
1380  * trace_iterator_flags is an enumeration that defines bit
1381  * positions into trace_flags that controls the output.
1382  *
1383  * NOTE: These bits must match the trace_options array in
1384  *       trace.c (this macro guarantees it).
1385  */
1386 #define TRACE_FLAGS						\
1387 		C(PRINT_PARENT,		"print-parent"),	\
1388 		C(SYM_OFFSET,		"sym-offset"),		\
1389 		C(SYM_ADDR,		"sym-addr"),		\
1390 		C(VERBOSE,		"verbose"),		\
1391 		C(RAW,			"raw"),			\
1392 		C(HEX,			"hex"),			\
1393 		C(BIN,			"bin"),			\
1394 		C(BLOCK,		"block"),		\
1395 		C(FIELDS,		"fields"),		\
1396 		C(PRINTK,		"trace_printk"),	\
1397 		C(ANNOTATE,		"annotate"),		\
1398 		C(USERSTACKTRACE,	"userstacktrace"),	\
1399 		C(SYM_USEROBJ,		"sym-userobj"),		\
1400 		C(PRINTK_MSGONLY,	"printk-msg-only"),	\
1401 		C(CONTEXT_INFO,		"context-info"),   /* Print pid/cpu/time */ \
1402 		C(LATENCY_FMT,		"latency-format"),	\
1403 		C(RECORD_CMD,		"record-cmd"),		\
1404 		C(RECORD_TGID,		"record-tgid"),		\
1405 		C(OVERWRITE,		"overwrite"),		\
1406 		C(STOP_ON_FREE,		"disable_on_free"),	\
1407 		C(IRQ_INFO,		"irq-info"),		\
1408 		C(MARKERS,		"markers"),		\
1409 		C(EVENT_FORK,		"event-fork"),		\
1410 		C(TRACE_PRINTK,		"trace_printk_dest"),	\
1411 		C(COPY_MARKER,		"copy_trace_marker"),	\
1412 		C(PAUSE_ON_TRACE,	"pause-on-trace"),	\
1413 		C(HASH_PTR,		"hash-ptr"),	/* Print hashed pointer */ \
1414 		C(BITMASK_LIST,		"bitmask-list"),	\
1415 		FUNCTION_FLAGS					\
1416 		FGRAPH_FLAGS					\
1417 		STACK_FLAGS					\
1418 		BRANCH_FLAGS					\
1419 		PROFILER_FLAGS					\
1420 		FPROFILE_FLAGS
1421 
1422 /*
1423  * By defining C, we can make TRACE_FLAGS a list of bit names
1424  * that will define the bits for the flag masks.
1425  */
1426 #undef C
1427 #define C(a, b) TRACE_ITER_##a##_BIT
1428 
1429 enum trace_iterator_bits {
1430 	TRACE_FLAGS
1431 	/* Make sure we don't go more than we have bits for */
1432 	TRACE_ITER_LAST_BIT
1433 };
1434 
1435 /*
1436  * And use TRACE_ITER(flag) to define the bit masks.
1437  */
1438 #define TRACE_ITER(flag)		\
1439 	(TRACE_ITER_##flag##_BIT < 0 ? 0 : 1ULL << (TRACE_ITER_##flag##_BIT))
1440 
1441 /*
1442  * TRACE_ITER_SYM_MASK masks the options in trace_flags that
1443  * control the output of kernel symbols.
1444  */
1445 #define TRACE_ITER_SYM_MASK \
1446 	(TRACE_ITER(PRINT_PARENT)|TRACE_ITER(SYM_OFFSET)|TRACE_ITER(SYM_ADDR))
1447 
1448 extern struct tracer nop_trace;
1449 
1450 #ifdef CONFIG_BRANCH_TRACER
1451 extern int enable_branch_tracing(struct trace_array *tr);
1452 extern void disable_branch_tracing(void);
1453 static inline int trace_branch_enable(struct trace_array *tr)
1454 {
1455 	if (tr->trace_flags & TRACE_ITER(BRANCH))
1456 		return enable_branch_tracing(tr);
1457 	return 0;
1458 }
1459 static inline void trace_branch_disable(void)
1460 {
1461 	/* due to races, always disable */
1462 	disable_branch_tracing();
1463 }
1464 #else
1465 static inline int trace_branch_enable(struct trace_array *tr)
1466 {
1467 	return 0;
1468 }
1469 static inline void trace_branch_disable(void)
1470 {
1471 }
1472 #endif /* CONFIG_BRANCH_TRACER */
1473 
1474 /* set ring buffers to default size if not already done so */
1475 int tracing_update_buffers(struct trace_array *tr);
1476 
1477 union trace_synth_field {
1478 	u8				as_u8;
1479 	u16				as_u16;
1480 	u32				as_u32;
1481 	u64				as_u64;
1482 	struct trace_dynamic_info	as_dynamic;
1483 };
1484 
1485 struct ftrace_event_field {
1486 	struct list_head	link;
1487 	const char		*name;
1488 	const char		*type;
1489 	int			filter_type;
1490 	int			offset;
1491 	int			size;
1492 	unsigned int		is_signed:1;
1493 	unsigned int		needs_test:1;
1494 	int			len;
1495 };
1496 
1497 struct prog_entry;
1498 
1499 struct event_filter {
1500 	struct prog_entry __rcu	*prog;
1501 	char			*filter_string;
1502 };
1503 
1504 struct event_subsystem {
1505 	struct list_head	list;
1506 	const char		*name;
1507 	struct event_filter	*filter;
1508 	int			ref_count;
1509 };
1510 
1511 struct trace_subsystem_dir {
1512 	struct list_head		list;
1513 	struct event_subsystem		*subsystem;
1514 	struct trace_array		*tr;
1515 	struct eventfs_inode		*ei;
1516 	int				ref_count;
1517 	int				nr_events;
1518 };
1519 
1520 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
1521 				     struct trace_buffer *buffer,
1522 				     struct ring_buffer_event *event,
1523 				     unsigned int trcace_ctx,
1524 				     struct pt_regs *regs);
1525 
1526 static inline void trace_buffer_unlock_commit(struct trace_array *tr,
1527 					      struct trace_buffer *buffer,
1528 					      struct ring_buffer_event *event,
1529 					      unsigned int trace_ctx)
1530 {
1531 	trace_buffer_unlock_commit_regs(tr, buffer, event, trace_ctx, NULL);
1532 }
1533 
1534 DECLARE_PER_CPU(bool, trace_taskinfo_save);
1535 int trace_save_cmdline(struct task_struct *tsk);
1536 int trace_create_savedcmd(void);
1537 int trace_alloc_tgid_map(void);
1538 void trace_free_saved_cmdlines_buffer(void);
1539 
1540 extern const struct file_operations tracing_saved_cmdlines_fops;
1541 extern const struct file_operations tracing_saved_tgids_fops;
1542 extern const struct file_operations tracing_saved_cmdlines_size_fops;
1543 
1544 DECLARE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
1545 DECLARE_PER_CPU(int, trace_buffered_event_cnt);
1546 void trace_buffered_event_disable(void);
1547 void trace_buffered_event_enable(void);
1548 
1549 void early_enable_events(struct trace_array *tr, char *buf, bool disable_first);
1550 
1551 struct trace_user_buf;
1552 struct trace_user_buf_info {
1553 	struct trace_user_buf __percpu	*tbuf;
1554 	size_t				size;
1555 	int				ref;
1556 };
1557 
1558 typedef int (*trace_user_buf_copy)(char *dst, const char __user *src,
1559 				  size_t size, void *data);
1560 int trace_user_fault_init(struct trace_user_buf_info *tinfo, size_t size);
1561 int trace_user_fault_get(struct trace_user_buf_info *tinfo);
1562 int trace_user_fault_put(struct trace_user_buf_info *tinfo);
1563 void trace_user_fault_destroy(struct trace_user_buf_info *tinfo);
1564 char *trace_user_fault_read(struct trace_user_buf_info *tinfo,
1565 			    const char __user *ptr, size_t size,
1566 			    trace_user_buf_copy copy_func, void *data);
1567 
1568 static inline void
1569 __trace_event_discard_commit(struct trace_buffer *buffer,
1570 			     struct ring_buffer_event *event)
1571 {
1572 	if (this_cpu_read(trace_buffered_event) == event) {
1573 		/* Simply release the temp buffer and enable preemption */
1574 		this_cpu_dec(trace_buffered_event_cnt);
1575 		preempt_enable_notrace();
1576 		return;
1577 	}
1578 	/* ring_buffer_discard_commit() enables preemption */
1579 	ring_buffer_discard_commit(buffer, event);
1580 }
1581 
1582 /*
1583  * Helper function for event_trigger_unlock_commit{_regs}().
1584  * If there are event triggers attached to this event that requires
1585  * filtering against its fields, then they will be called as the
1586  * entry already holds the field information of the current event.
1587  *
1588  * It also checks if the event should be discarded or not.
1589  * It is to be discarded if the event is soft disabled and the
1590  * event was only recorded to process triggers, or if the event
1591  * filter is active and this event did not match the filters.
1592  *
1593  * Returns true if the event is discarded, false otherwise.
1594  */
1595 static inline bool
1596 __event_trigger_test_discard(struct trace_event_file *file,
1597 			     struct trace_buffer *buffer,
1598 			     struct ring_buffer_event *event,
1599 			     void *entry,
1600 			     enum event_trigger_type *tt)
1601 {
1602 	unsigned long eflags = file->flags;
1603 
1604 	if (eflags & EVENT_FILE_FL_TRIGGER_COND)
1605 		*tt = event_triggers_call(file, buffer, entry, event);
1606 
1607 	if (likely(!(file->flags & (EVENT_FILE_FL_SOFT_DISABLED |
1608 				    EVENT_FILE_FL_FILTERED |
1609 				    EVENT_FILE_FL_PID_FILTER))))
1610 		return false;
1611 
1612 	if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
1613 		goto discard;
1614 
1615 	if (file->flags & EVENT_FILE_FL_FILTERED &&
1616 	    !filter_match_preds(file->filter, entry))
1617 		goto discard;
1618 
1619 	if ((file->flags & EVENT_FILE_FL_PID_FILTER) &&
1620 	    trace_event_ignore_this_pid(file))
1621 		goto discard;
1622 
1623 	return false;
1624  discard:
1625 	__trace_event_discard_commit(buffer, event);
1626 	return true;
1627 }
1628 
1629 /**
1630  * event_trigger_unlock_commit - handle triggers and finish event commit
1631  * @file: The file pointer associated with the event
1632  * @buffer: The ring buffer that the event is being written to
1633  * @event: The event meta data in the ring buffer
1634  * @entry: The event itself
1635  * @trace_ctx: The tracing context flags.
1636  *
1637  * This is a helper function to handle triggers that require data
1638  * from the event itself. It also tests the event against filters and
1639  * if the event is soft disabled and should be discarded.
1640  */
1641 static inline void
1642 event_trigger_unlock_commit(struct trace_event_file *file,
1643 			    struct trace_buffer *buffer,
1644 			    struct ring_buffer_event *event,
1645 			    void *entry, unsigned int trace_ctx)
1646 {
1647 	enum event_trigger_type tt = ETT_NONE;
1648 
1649 	if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
1650 		trace_buffer_unlock_commit(file->tr, buffer, event, trace_ctx);
1651 
1652 	if (tt)
1653 		event_triggers_post_call(file, tt);
1654 }
1655 
1656 #define FILTER_PRED_INVALID	((unsigned short)-1)
1657 #define FILTER_PRED_IS_RIGHT	(1 << 15)
1658 #define FILTER_PRED_FOLD	(1 << 15)
1659 
1660 /*
1661  * The max preds is the size of unsigned short with
1662  * two flags at the MSBs. One bit is used for both the IS_RIGHT
1663  * and FOLD flags. The other is reserved.
1664  *
1665  * 2^14 preds is way more than enough.
1666  */
1667 #define MAX_FILTER_PRED		16384
1668 
1669 struct filter_pred;
1670 struct regex;
1671 
1672 typedef int (*regex_match_func)(char *str, struct regex *r, int len);
1673 
1674 enum regex_type {
1675 	MATCH_FULL = 0,
1676 	MATCH_FRONT_ONLY,
1677 	MATCH_MIDDLE_ONLY,
1678 	MATCH_END_ONLY,
1679 	MATCH_GLOB,
1680 	MATCH_INDEX,
1681 };
1682 
1683 struct regex {
1684 	char			pattern[MAX_FILTER_STR_VAL];
1685 	int			len;
1686 	int			field_len;
1687 	regex_match_func	match;
1688 };
1689 
1690 static inline bool is_string_field(struct ftrace_event_field *field)
1691 {
1692 	return field->filter_type == FILTER_DYN_STRING ||
1693 	       field->filter_type == FILTER_RDYN_STRING ||
1694 	       field->filter_type == FILTER_STATIC_STRING ||
1695 	       field->filter_type == FILTER_PTR_STRING ||
1696 	       field->filter_type == FILTER_COMM;
1697 }
1698 
1699 static inline bool is_function_field(struct ftrace_event_field *field)
1700 {
1701 	return field->filter_type == FILTER_TRACE_FN;
1702 }
1703 
1704 extern enum regex_type
1705 filter_parse_regex(char *buff, int len, char **search, int *not);
1706 extern void print_event_filter(struct trace_event_file *file,
1707 			       struct trace_seq *s);
1708 extern int apply_event_filter(struct trace_event_file *file,
1709 			      char *filter_string);
1710 extern int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
1711 					char *filter_string);
1712 extern void print_subsystem_event_filter(struct event_subsystem *system,
1713 					 struct trace_seq *s);
1714 extern int filter_assign_type(const char *type);
1715 extern int create_event_filter(struct trace_array *tr,
1716 			       struct trace_event_call *call,
1717 			       char *filter_str, bool set_str,
1718 			       struct event_filter **filterp);
1719 extern void free_event_filter(struct event_filter *filter);
1720 
1721 struct ftrace_event_field *
1722 trace_find_event_field(struct trace_event_call *call, char *name);
1723 
1724 extern void trace_event_enable_cmd_record(bool enable);
1725 extern void trace_event_enable_tgid_record(bool enable);
1726 
1727 extern int event_trace_init(void);
1728 extern int init_events(void);
1729 extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
1730 extern int event_trace_del_tracer(struct trace_array *tr);
1731 extern void __trace_early_add_events(struct trace_array *tr);
1732 
1733 extern struct trace_event_file *__find_event_file(struct trace_array *tr,
1734 						  const char *system,
1735 						  const char *event);
1736 extern struct trace_event_file *find_event_file(struct trace_array *tr,
1737 						const char *system,
1738 						const char *event);
1739 
1740 static inline void *event_file_data(struct file *filp)
1741 {
1742 	return READ_ONCE(file_inode(filp)->i_private);
1743 }
1744 
1745 extern struct mutex event_mutex;
1746 extern struct list_head ftrace_events;
1747 
1748 /*
1749  * When the trace_event_file is the filp->i_private pointer,
1750  * it must be taken under the event_mutex lock, and then checked
1751  * if the EVENT_FILE_FL_FREED flag is set. If it is, then the
1752  * data pointed to by the trace_event_file can not be trusted.
1753  *
1754  * Use the event_file_file() to access the trace_event_file from
1755  * the filp the first time under the event_mutex and check for
1756  * NULL. If it is needed to be retrieved again and the event_mutex
1757  * is still held, then the event_file_data() can be used and it
1758  * is guaranteed to be valid.
1759  */
1760 static inline struct trace_event_file *event_file_file(struct file *filp)
1761 {
1762 	struct trace_event_file *file;
1763 
1764 	lockdep_assert_held(&event_mutex);
1765 	file = READ_ONCE(file_inode(filp)->i_private);
1766 	if (!file || file->flags & EVENT_FILE_FL_FREED)
1767 		return NULL;
1768 	return file;
1769 }
1770 
1771 extern const struct file_operations event_trigger_fops;
1772 extern const struct file_operations event_hist_fops;
1773 extern const struct file_operations event_hist_debug_fops;
1774 extern const struct file_operations event_inject_fops;
1775 
1776 #ifdef CONFIG_HIST_TRIGGERS
1777 extern int register_trigger_hist_cmd(void);
1778 extern int register_trigger_hist_enable_disable_cmds(void);
1779 #else
1780 static inline int register_trigger_hist_cmd(void) { return 0; }
1781 static inline int register_trigger_hist_enable_disable_cmds(void) { return 0; }
1782 #endif
1783 
1784 extern int register_trigger_cmds(void);
1785 extern void clear_event_triggers(struct trace_array *tr);
1786 
1787 enum {
1788 	EVENT_TRIGGER_FL_PROBE		= BIT(0),
1789 	EVENT_TRIGGER_FL_COUNT		= BIT(1),
1790 };
1791 
1792 struct event_trigger_data {
1793 	unsigned long			count;
1794 	int				ref;
1795 	int				flags;
1796 	struct event_command		*cmd_ops;
1797 	struct event_filter __rcu	*filter;
1798 	char				*filter_str;
1799 	void				*private_data;
1800 	bool				paused;
1801 	bool				paused_tmp;
1802 	struct list_head		list;
1803 	char				*name;
1804 	struct list_head		named_list;
1805 	struct event_trigger_data	*named_data;
1806 	struct llist_node		llist;
1807 };
1808 
1809 /* Avoid typos */
1810 #define ENABLE_EVENT_STR	"enable_event"
1811 #define DISABLE_EVENT_STR	"disable_event"
1812 #define ENABLE_HIST_STR		"enable_hist"
1813 #define DISABLE_HIST_STR	"disable_hist"
1814 
1815 struct enable_trigger_data {
1816 	struct trace_event_file		*file;
1817 	bool				enable;
1818 	bool				hist;
1819 };
1820 
1821 bool event_trigger_count(struct event_trigger_data *data,
1822 			 struct trace_buffer *buffer,  void *rec,
1823 			 struct ring_buffer_event *event);
1824 
1825 extern int event_enable_trigger_print(struct seq_file *m,
1826 				      struct event_trigger_data *data);
1827 extern void event_enable_trigger_free(struct event_trigger_data *data);
1828 extern int event_enable_trigger_parse(struct event_command *cmd_ops,
1829 				      struct trace_event_file *file,
1830 				      char *glob, char *cmd,
1831 				      char *param_and_filter);
1832 extern int event_enable_register_trigger(char *glob,
1833 					 struct event_trigger_data *data,
1834 					 struct trace_event_file *file);
1835 extern void event_enable_unregister_trigger(char *glob,
1836 					    struct event_trigger_data *test,
1837 					    struct trace_event_file *file);
1838 extern struct event_trigger_data *
1839 trigger_data_alloc(struct event_command *cmd_ops, char *cmd, char *param,
1840 		   void *private_data);
1841 extern void trigger_data_free(struct event_trigger_data *data);
1842 extern int event_trigger_init(struct event_trigger_data *data);
1843 extern int trace_event_trigger_enable_disable(struct trace_event_file *file,
1844 					      int trigger_enable);
1845 extern void update_cond_flag(struct trace_event_file *file);
1846 extern int set_trigger_filter(char *filter_str,
1847 			      struct event_trigger_data *trigger_data,
1848 			      struct trace_event_file *file);
1849 extern struct event_trigger_data *find_named_trigger(const char *name);
1850 extern bool is_named_trigger(struct event_trigger_data *test);
1851 extern int save_named_trigger(const char *name,
1852 			      struct event_trigger_data *data);
1853 extern void del_named_trigger(struct event_trigger_data *data);
1854 extern void pause_named_trigger(struct event_trigger_data *data);
1855 extern void unpause_named_trigger(struct event_trigger_data *data);
1856 extern void set_named_trigger_data(struct event_trigger_data *data,
1857 				   struct event_trigger_data *named_data);
1858 extern struct event_trigger_data *
1859 get_named_trigger_data(struct event_trigger_data *data);
1860 extern int register_event_command(struct event_command *cmd);
1861 extern int unregister_event_command(struct event_command *cmd);
1862 extern int register_trigger_hist_enable_disable_cmds(void);
1863 extern bool event_trigger_check_remove(const char *glob);
1864 extern bool event_trigger_empty_param(const char *param);
1865 extern int event_trigger_separate_filter(char *param_and_filter, char **param,
1866 					 char **filter, bool param_required);
1867 extern int event_trigger_parse_num(char *trigger,
1868 				   struct event_trigger_data *trigger_data);
1869 extern int event_trigger_set_filter(struct event_command *cmd_ops,
1870 				    struct trace_event_file *file,
1871 				    char *param,
1872 				    struct event_trigger_data *trigger_data);
1873 extern void event_trigger_reset_filter(struct event_command *cmd_ops,
1874 				       struct event_trigger_data *trigger_data);
1875 extern int event_trigger_register(struct event_command *cmd_ops,
1876 				  struct trace_event_file *file,
1877 				  char *glob,
1878 				  struct event_trigger_data *trigger_data);
1879 extern void event_trigger_unregister(struct event_command *cmd_ops,
1880 				     struct trace_event_file *file,
1881 				     char *glob,
1882 				     struct event_trigger_data *trigger_data);
1883 
1884 extern void event_file_get(struct trace_event_file *file);
1885 extern void event_file_put(struct trace_event_file *file);
1886 
1887 /**
1888  * struct event_command - callbacks and data members for event commands
1889  *
1890  * Event commands are invoked by users by writing the command name
1891  * into the 'trigger' file associated with a trace event.  The
1892  * parameters associated with a specific invocation of an event
1893  * command are used to create an event trigger instance, which is
1894  * added to the list of trigger instances associated with that trace
1895  * event.  When the event is hit, the set of triggers associated with
1896  * that event is invoked.
1897  *
1898  * The data members in this structure provide per-event command data
1899  * for various event commands.
1900  *
1901  * All the data members below, except for @post_trigger, must be set
1902  * for each event command.
1903  *
1904  * @name: The unique name that identifies the event command.  This is
1905  *	the name used when setting triggers via trigger files.
1906  *
1907  * @trigger_type: A unique id that identifies the event command
1908  *	'type'.  This value has two purposes, the first to ensure that
1909  *	only one trigger of the same type can be set at a given time
1910  *	for a particular event e.g. it doesn't make sense to have both
1911  *	a traceon and traceoff trigger attached to a single event at
1912  *	the same time, so traceon and traceoff have the same type
1913  *	though they have different names.  The @trigger_type value is
1914  *	also used as a bit value for deferring the actual trigger
1915  *	action until after the current event is finished.  Some
1916  *	commands need to do this if they themselves log to the trace
1917  *	buffer (see the @post_trigger() member below).  @trigger_type
1918  *	values are defined by adding new values to the trigger_type
1919  *	enum in include/linux/trace_events.h.
1920  *
1921  * @flags: See the enum event_command_flags below.
1922  *
1923  * All the methods below, except for @set_filter() and @unreg_all(),
1924  * must be implemented.
1925  *
1926  * @parse: The callback function responsible for parsing and
1927  *	registering the trigger written to the 'trigger' file by the
1928  *	user.  It allocates the trigger instance and registers it with
1929  *	the appropriate trace event.  It makes use of the other
1930  *	event_command callback functions to orchestrate this, and is
1931  *	usually implemented by the generic utility function
1932  *	@event_trigger_callback() (see trace_event_triggers.c).
1933  *
1934  * @reg: Adds the trigger to the list of triggers associated with the
1935  *	event, and enables the event trigger itself, after
1936  *	initializing it (via the event_command @init() function).
1937  *	This is also where commands can use the @trigger_type value to
1938  *	make the decision as to whether or not multiple instances of
1939  *	the trigger should be allowed.  This is usually implemented by
1940  *	the generic utility function @register_trigger() (see
1941  *	trace_event_triggers.c).
1942  *
1943  * @unreg: Removes the trigger from the list of triggers associated
1944  *	with the event, and disables the event trigger itself, after
1945  *	initializing it (via the event_command @free() function).
1946  *	This is usually implemented by the generic utility function
1947  *	@unregister_trigger() (see trace_event_triggers.c).
1948  *
1949  * @unreg_all: An optional function called to remove all the triggers
1950  *	from the list of triggers associated with the event.  Called
1951  *	when a trigger file is opened in truncate mode.
1952  *
1953  * @set_filter: An optional function called to parse and set a filter
1954  *	for the trigger.  If no @set_filter() method is set for the
1955  *	event command, filters set by the user for the command will be
1956  *	ignored.  This is usually implemented by the generic utility
1957  *	function @set_trigger_filter() (see trace_event_triggers.c).
1958  *
1959  * All the methods below, except for @init() and @free(), must be
1960  * implemented.
1961  *
1962  * @trigger: The trigger 'probe' function called when the triggering
1963  *	event occurs.  The data passed into this callback is the data
1964  *	that was supplied to the event_command @reg() function that
1965  *	registered the trigger (see struct event_command) along with
1966  *	the trace record, rec.
1967  *
1968  * @count_func: If defined and a numeric parameter is passed to the
1969  *	trigger, then this function will be called before @trigger
1970  *	is called. If this function returns false, then @trigger is not
1971  *	executed.
1972  *
1973  * @init: An optional initialization function called for the trigger
1974  *	when the trigger is registered (via the event_command reg()
1975  *	function).  This can be used to perform per-trigger
1976  *	initialization such as incrementing a per-trigger reference
1977  *	count, for instance.  This is usually implemented by the
1978  *	generic utility function @event_trigger_init() (see
1979  *	trace_event_triggers.c).
1980  *
1981  * @free: An optional de-initialization function called for the
1982  *	trigger when the trigger is unregistered (via the
1983  *	event_command @reg() function).  This can be used to perform
1984  *	per-trigger de-initialization such as decrementing a
1985  *	per-trigger reference count and freeing corresponding trigger
1986  *	data, for instance.  This is usually implemented by the
1987  *	generic utility function @event_trigger_free() (see
1988  *	trace_event_triggers.c).
1989  *
1990  * @print: The callback function invoked to have the trigger print
1991  *	itself.  This is usually implemented by a wrapper function
1992  *	that calls the generic utility function @event_trigger_print()
1993  *	(see trace_event_triggers.c).
1994  */
1995 struct event_command {
1996 	struct list_head	list;
1997 	char			*name;
1998 	enum event_trigger_type	trigger_type;
1999 	int			flags;
2000 	int			(*parse)(struct event_command *cmd_ops,
2001 					 struct trace_event_file *file,
2002 					 char *glob, char *cmd,
2003 					 char *param_and_filter);
2004 	int			(*reg)(char *glob,
2005 				       struct event_trigger_data *data,
2006 				       struct trace_event_file *file);
2007 	void			(*unreg)(char *glob,
2008 					 struct event_trigger_data *data,
2009 					 struct trace_event_file *file);
2010 	void			(*unreg_all)(struct trace_event_file *file);
2011 	int			(*set_filter)(char *filter_str,
2012 					      struct event_trigger_data *data,
2013 					      struct trace_event_file *file);
2014 	void			(*trigger)(struct event_trigger_data *data,
2015 					   struct trace_buffer *buffer,
2016 					   void *rec,
2017 					   struct ring_buffer_event *rbe);
2018 	bool			(*count_func)(struct event_trigger_data *data,
2019 					      struct trace_buffer *buffer,
2020 					      void *rec,
2021 					      struct ring_buffer_event *rbe);
2022 	int			(*init)(struct event_trigger_data *data);
2023 	void			(*free)(struct event_trigger_data *data);
2024 	int			(*print)(struct seq_file *m,
2025 					 struct event_trigger_data *data);
2026 };
2027 
2028 /**
2029  * enum event_command_flags - flags for struct event_command
2030  *
2031  * @POST_TRIGGER: A flag that says whether or not this command needs
2032  *	to have its action delayed until after the current event has
2033  *	been closed.  Some triggers need to avoid being invoked while
2034  *	an event is currently in the process of being logged, since
2035  *	the trigger may itself log data into the trace buffer.  Thus
2036  *	we make sure the current event is committed before invoking
2037  *	those triggers.  To do that, the trigger invocation is split
2038  *	in two - the first part checks the filter using the current
2039  *	trace record; if a command has the @post_trigger flag set, it
2040  *	sets a bit for itself in the return value, otherwise it
2041  *	directly invokes the trigger.  Once all commands have been
2042  *	either invoked or set their return flag, the current record is
2043  *	either committed or discarded.  At that point, if any commands
2044  *	have deferred their triggers, those commands are finally
2045  *	invoked following the close of the current event.  In other
2046  *	words, if the event_command @func() probe implementation
2047  *	itself logs to the trace buffer, this flag should be set,
2048  *	otherwise it can be left unspecified.
2049  *
2050  * @NEEDS_REC: A flag that says whether or not this command needs
2051  *	access to the trace record in order to perform its function,
2052  *	regardless of whether or not it has a filter associated with
2053  *	it (filters make a trigger require access to the trace record
2054  *	but are not always present).
2055  */
2056 enum event_command_flags {
2057 	EVENT_CMD_FL_POST_TRIGGER	= 1,
2058 	EVENT_CMD_FL_NEEDS_REC		= 2,
2059 };
2060 
2061 static inline bool event_command_post_trigger(struct event_command *cmd_ops)
2062 {
2063 	return cmd_ops->flags & EVENT_CMD_FL_POST_TRIGGER;
2064 }
2065 
2066 static inline bool event_command_needs_rec(struct event_command *cmd_ops)
2067 {
2068 	return cmd_ops->flags & EVENT_CMD_FL_NEEDS_REC;
2069 }
2070 
2071 extern int trace_event_enable_disable(struct trace_event_file *file,
2072 				      int enable, int soft_disable);
2073 extern int tracing_alloc_snapshot(void);
2074 extern void tracing_snapshot_cond(struct trace_array *tr, void *cond_data);
2075 extern int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update);
2076 
2077 extern int tracing_snapshot_cond_disable(struct trace_array *tr);
2078 extern void *tracing_cond_snapshot_data(struct trace_array *tr);
2079 
2080 extern const char *__start___trace_bprintk_fmt[];
2081 extern const char *__stop___trace_bprintk_fmt[];
2082 
2083 extern const char *__start___tracepoint_str[];
2084 extern const char *__stop___tracepoint_str[];
2085 
2086 void trace_printk_control(bool enabled);
2087 void trace_printk_start_comm(void);
2088 int trace_keep_overwrite(struct tracer *tracer, u64 mask, int set);
2089 int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled);
2090 
2091 /* Used from boot time tracer */
2092 extern int trace_set_options(struct trace_array *tr, char *option);
2093 extern int tracing_set_tracer(struct trace_array *tr, const char *buf);
2094 extern ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
2095 					  unsigned long size, int cpu_id);
2096 extern int tracing_set_cpumask(struct trace_array *tr,
2097 				cpumask_var_t tracing_cpumask_new);
2098 
2099 
2100 #define MAX_EVENT_NAME_LEN	64
2101 
2102 extern ssize_t trace_parse_run_command(struct file *file,
2103 		const char __user *buffer, size_t count, loff_t *ppos,
2104 		int (*createfn)(const char *));
2105 
2106 extern unsigned int err_pos(char *cmd, const char *str);
2107 extern void tracing_log_err(struct trace_array *tr,
2108 			    const char *loc, const char *cmd,
2109 			    const char **errs, u8 type, u16 pos);
2110 
2111 /*
2112  * Normal trace_printk() and friends allocates special buffers
2113  * to do the manipulation, as well as saves the print formats
2114  * into sections to display. But the trace infrastructure wants
2115  * to use these without the added overhead at the price of being
2116  * a bit slower (used mainly for warnings, where we don't care
2117  * about performance). The internal_trace_puts() is for such
2118  * a purpose.
2119  */
2120 #define internal_trace_puts(str) __trace_puts(_THIS_IP_, str, strlen(str))
2121 
2122 #undef FTRACE_ENTRY
2123 #define FTRACE_ENTRY(call, struct_name, id, tstruct, print)	\
2124 	extern struct trace_event_call					\
2125 	__aligned(4) event_##call;
2126 #undef FTRACE_ENTRY_DUP
2127 #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print)	\
2128 	FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print))
2129 #undef FTRACE_ENTRY_PACKED
2130 #define FTRACE_ENTRY_PACKED(call, struct_name, id, tstruct, print) \
2131 	FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print))
2132 
2133 #include "trace_entries.h"
2134 
2135 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_FUNCTION_TRACER)
2136 int perf_ftrace_event_register(struct trace_event_call *call,
2137 			       enum trace_reg type, void *data);
2138 #else
2139 #define perf_ftrace_event_register NULL
2140 #endif
2141 
2142 #ifdef CONFIG_FTRACE_SYSCALLS
2143 void init_ftrace_syscalls(void);
2144 const char *get_syscall_name(int syscall);
2145 #else
2146 static inline void init_ftrace_syscalls(void) { }
2147 static inline const char *get_syscall_name(int syscall)
2148 {
2149 	return NULL;
2150 }
2151 #endif
2152 
2153 #ifdef CONFIG_EVENT_TRACING
2154 void trace_event_init(void);
2155 void trace_event_update_all(struct trace_eval_map **map, int len);
2156 /* Used from boot time tracer */
2157 extern int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
2158 extern int trigger_process_regex(struct trace_event_file *file, char *buff);
2159 #else
2160 static inline void __init trace_event_init(void) { }
2161 static inline void trace_event_update_all(struct trace_eval_map **map, int len) { }
2162 #endif
2163 
2164 #ifdef CONFIG_TRACER_SNAPSHOT
2165 void tracing_snapshot_instance(struct trace_array *tr);
2166 int tracing_alloc_snapshot_instance(struct trace_array *tr);
2167 int tracing_arm_snapshot(struct trace_array *tr);
2168 void tracing_disarm_snapshot(struct trace_array *tr);
2169 #else
2170 static inline void tracing_snapshot_instance(struct trace_array *tr) { }
2171 static inline int tracing_alloc_snapshot_instance(struct trace_array *tr)
2172 {
2173 	return 0;
2174 }
2175 static inline int tracing_arm_snapshot(struct trace_array *tr) { return 0; }
2176 static inline void tracing_disarm_snapshot(struct trace_array *tr) { }
2177 #endif
2178 
2179 #ifdef CONFIG_PREEMPT_TRACER
2180 void tracer_preempt_on(unsigned long a0, unsigned long a1);
2181 void tracer_preempt_off(unsigned long a0, unsigned long a1);
2182 #else
2183 static inline void tracer_preempt_on(unsigned long a0, unsigned long a1) { }
2184 static inline void tracer_preempt_off(unsigned long a0, unsigned long a1) { }
2185 #endif
2186 #ifdef CONFIG_IRQSOFF_TRACER
2187 void tracer_hardirqs_on(unsigned long a0, unsigned long a1);
2188 void tracer_hardirqs_off(unsigned long a0, unsigned long a1);
2189 #else
2190 static inline void tracer_hardirqs_on(unsigned long a0, unsigned long a1) { }
2191 static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { }
2192 #endif
2193 
2194 /*
2195  * Reset the state of the trace_iterator so that it can read consumed data.
2196  * Normally, the trace_iterator is used for reading the data when it is not
2197  * consumed, and must retain state.
2198  */
2199 static __always_inline void trace_iterator_reset(struct trace_iterator *iter)
2200 {
2201 	memset_startat(iter, 0, seq);
2202 	iter->pos = -1;
2203 }
2204 
2205 /* Check the name is good for event/group/fields */
2206 static inline bool __is_good_name(const char *name, bool hash_ok)
2207 {
2208 	if (!isalpha(*name) && *name != '_' && (!hash_ok || *name != '-'))
2209 		return false;
2210 	while (*++name != '\0') {
2211 		if (!isalpha(*name) && !isdigit(*name) && *name != '_' &&
2212 		    (!hash_ok || *name != '-'))
2213 			return false;
2214 	}
2215 	return true;
2216 }
2217 
2218 /* Check the name is good for event/group/fields */
2219 static inline bool is_good_name(const char *name)
2220 {
2221 	return __is_good_name(name, false);
2222 }
2223 
2224 /* Check the name is good for system */
2225 static inline bool is_good_system_name(const char *name)
2226 {
2227 	return __is_good_name(name, true);
2228 }
2229 
2230 /* Convert certain expected symbols into '_' when generating event names */
2231 static inline void sanitize_event_name(char *name)
2232 {
2233 	while (*name++ != '\0')
2234 		if (*name == ':' || *name == '.' || *name == '*')
2235 			*name = '_';
2236 }
2237 
2238 /*
2239  * This is a generic way to read and write a u64 value from a file in tracefs.
2240  *
2241  * The value is stored on the variable pointed by *val. The value needs
2242  * to be at least *min and at most *max. The write is protected by an
2243  * existing *lock.
2244  */
2245 struct trace_min_max_param {
2246 	struct mutex	*lock;
2247 	u64		*val;
2248 	u64		*min;
2249 	u64		*max;
2250 };
2251 
2252 #define U64_STR_SIZE		24	/* 20 digits max */
2253 
2254 extern const struct file_operations trace_min_max_fops;
2255 
2256 #ifdef CONFIG_RV
2257 extern int rv_init_interface(void);
2258 #else
2259 static inline int rv_init_interface(void)
2260 {
2261 	return 0;
2262 }
2263 #endif
2264 
2265 /*
2266  * This is used only to distinguish
2267  * function address from trampoline code.
2268  * So this value has no meaning.
2269  */
2270 #define FTRACE_TRAMPOLINE_MARKER  ((unsigned long) INT_MAX)
2271 
2272 /*
2273  * This is used to get the address of the args array based on
2274  * the type of the entry.
2275  */
2276 #define FGRAPH_ENTRY_ARGS(e)						\
2277 	({								\
2278 		unsigned long *_args;					\
2279 		struct ftrace_graph_ent_entry *_e = e;			\
2280 									\
2281 		if (IS_ENABLED(CONFIG_FUNCTION_GRAPH_RETADDR) &&	\
2282 			e->ent.type == TRACE_GRAPH_RETADDR_ENT) {	\
2283 			struct fgraph_retaddr_ent_entry *_re;		\
2284 									\
2285 			_re = (typeof(_re))_e;				\
2286 			_args = _re->args;				\
2287 		} else {						\
2288 			_args = _e->args;				\
2289 		}							\
2290 		_args;							\
2291 	})
2292 
2293 #endif /* _LINUX_KERNEL_TRACE_H */
2294