xref: /linux/kernel/trace/trace.h (revision aad108aa9d1aca5be178ef40325dcc99b448e866)
1 
2 #ifndef _LINUX_KERNEL_TRACE_H
3 #define _LINUX_KERNEL_TRACE_H
4 
5 #include <linux/fs.h>
6 #include <linux/atomic.h>
7 #include <linux/sched.h>
8 #include <linux/clocksource.h>
9 #include <linux/ring_buffer.h>
10 #include <linux/mmiotrace.h>
11 #include <linux/tracepoint.h>
12 #include <linux/ftrace.h>
13 #include <linux/hw_breakpoint.h>
14 #include <linux/trace_seq.h>
15 #include <linux/trace_events.h>
16 #include <linux/compiler.h>
17 #include <linux/trace_seq.h>
18 
19 #ifdef CONFIG_FTRACE_SYSCALLS
20 #include <asm/unistd.h>		/* For NR_SYSCALLS	     */
21 #include <asm/syscall.h>	/* some archs define it here */
22 #endif
23 
24 enum trace_type {
25 	__TRACE_FIRST_TYPE = 0,
26 
27 	TRACE_FN,
28 	TRACE_CTX,
29 	TRACE_WAKE,
30 	TRACE_STACK,
31 	TRACE_PRINT,
32 	TRACE_BPRINT,
33 	TRACE_MMIO_RW,
34 	TRACE_MMIO_MAP,
35 	TRACE_BRANCH,
36 	TRACE_GRAPH_RET,
37 	TRACE_GRAPH_ENT,
38 	TRACE_USER_STACK,
39 	TRACE_BLK,
40 	TRACE_BPUTS,
41 
42 	__TRACE_LAST_TYPE,
43 };
44 
45 
46 #undef __field
47 #define __field(type, item)		type	item;
48 
49 #undef __field_struct
50 #define __field_struct(type, item)	__field(type, item)
51 
52 #undef __field_desc
53 #define __field_desc(type, container, item)
54 
55 #undef __array
56 #define __array(type, item, size)	type	item[size];
57 
58 #undef __array_desc
59 #define __array_desc(type, container, item, size)
60 
61 #undef __dynamic_array
62 #define __dynamic_array(type, item)	type	item[];
63 
64 #undef F_STRUCT
65 #define F_STRUCT(args...)		args
66 
67 #undef FTRACE_ENTRY
68 #define FTRACE_ENTRY(name, struct_name, id, tstruct, print, filter)	\
69 	struct struct_name {						\
70 		struct trace_entry	ent;				\
71 		tstruct							\
72 	}
73 
74 #undef FTRACE_ENTRY_DUP
75 #define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk, filter)
76 
77 #undef FTRACE_ENTRY_REG
78 #define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print,	\
79 			 filter, regfn) \
80 	FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print), \
81 		     filter)
82 
83 #include "trace_entries.h"
84 
85 /*
86  * syscalls are special, and need special handling, this is why
87  * they are not included in trace_entries.h
88  */
89 struct syscall_trace_enter {
90 	struct trace_entry	ent;
91 	int			nr;
92 	unsigned long		args[];
93 };
94 
95 struct syscall_trace_exit {
96 	struct trace_entry	ent;
97 	int			nr;
98 	long			ret;
99 };
100 
101 struct kprobe_trace_entry_head {
102 	struct trace_entry	ent;
103 	unsigned long		ip;
104 };
105 
106 struct kretprobe_trace_entry_head {
107 	struct trace_entry	ent;
108 	unsigned long		func;
109 	unsigned long		ret_ip;
110 };
111 
112 /*
113  * trace_flag_type is an enumeration that holds different
114  * states when a trace occurs. These are:
115  *  IRQS_OFF		- interrupts were disabled
116  *  IRQS_NOSUPPORT	- arch does not support irqs_disabled_flags
117  *  NEED_RESCHED	- reschedule is requested
118  *  HARDIRQ		- inside an interrupt handler
119  *  SOFTIRQ		- inside a softirq handler
120  */
121 enum trace_flag_type {
122 	TRACE_FLAG_IRQS_OFF		= 0x01,
123 	TRACE_FLAG_IRQS_NOSUPPORT	= 0x02,
124 	TRACE_FLAG_NEED_RESCHED		= 0x04,
125 	TRACE_FLAG_HARDIRQ		= 0x08,
126 	TRACE_FLAG_SOFTIRQ		= 0x10,
127 	TRACE_FLAG_PREEMPT_RESCHED	= 0x20,
128 	TRACE_FLAG_NMI			= 0x40,
129 };
130 
131 #define TRACE_BUF_SIZE		1024
132 
133 struct trace_array;
134 
135 /*
136  * The CPU trace array - it consists of thousands of trace entries
137  * plus some other descriptor data: (for example which task started
138  * the trace, etc.)
139  */
140 struct trace_array_cpu {
141 	atomic_t		disabled;
142 	void			*buffer_page;	/* ring buffer spare */
143 
144 	unsigned long		entries;
145 	unsigned long		saved_latency;
146 	unsigned long		critical_start;
147 	unsigned long		critical_end;
148 	unsigned long		critical_sequence;
149 	unsigned long		nice;
150 	unsigned long		policy;
151 	unsigned long		rt_priority;
152 	unsigned long		skipped_entries;
153 	cycle_t			preempt_timestamp;
154 	pid_t			pid;
155 	kuid_t			uid;
156 	char			comm[TASK_COMM_LEN];
157 
158 	bool			ignore_pid;
159 #ifdef CONFIG_FUNCTION_TRACER
160 	bool			ftrace_ignore_pid;
161 #endif
162 };
163 
164 struct tracer;
165 struct trace_option_dentry;
166 
167 struct trace_buffer {
168 	struct trace_array		*tr;
169 	struct ring_buffer		*buffer;
170 	struct trace_array_cpu __percpu	*data;
171 	cycle_t				time_start;
172 	int				cpu;
173 };
174 
175 #define TRACE_FLAGS_MAX_SIZE		32
176 
177 struct trace_options {
178 	struct tracer			*tracer;
179 	struct trace_option_dentry	*topts;
180 };
181 
182 struct trace_pid_list {
183 	int				pid_max;
184 	unsigned long			*pids;
185 };
186 
187 /*
188  * The trace array - an array of per-CPU trace arrays. This is the
189  * highest level data structure that individual tracers deal with.
190  * They have on/off state as well:
191  */
192 struct trace_array {
193 	struct list_head	list;
194 	char			*name;
195 	struct trace_buffer	trace_buffer;
196 #ifdef CONFIG_TRACER_MAX_TRACE
197 	/*
198 	 * The max_buffer is used to snapshot the trace when a maximum
199 	 * latency is reached, or when the user initiates a snapshot.
200 	 * Some tracers will use this to store a maximum trace while
201 	 * it continues examining live traces.
202 	 *
203 	 * The buffers for the max_buffer are set up the same as the trace_buffer
204 	 * When a snapshot is taken, the buffer of the max_buffer is swapped
205 	 * with the buffer of the trace_buffer and the buffers are reset for
206 	 * the trace_buffer so the tracing can continue.
207 	 */
208 	struct trace_buffer	max_buffer;
209 	bool			allocated_snapshot;
210 	unsigned long		max_latency;
211 #endif
212 	struct trace_pid_list	__rcu *filtered_pids;
213 	/*
214 	 * max_lock is used to protect the swapping of buffers
215 	 * when taking a max snapshot. The buffers themselves are
216 	 * protected by per_cpu spinlocks. But the action of the swap
217 	 * needs its own lock.
218 	 *
219 	 * This is defined as a arch_spinlock_t in order to help
220 	 * with performance when lockdep debugging is enabled.
221 	 *
222 	 * It is also used in other places outside the update_max_tr
223 	 * so it needs to be defined outside of the
224 	 * CONFIG_TRACER_MAX_TRACE.
225 	 */
226 	arch_spinlock_t		max_lock;
227 	int			buffer_disabled;
228 #ifdef CONFIG_FTRACE_SYSCALLS
229 	int			sys_refcount_enter;
230 	int			sys_refcount_exit;
231 	struct trace_event_file __rcu *enter_syscall_files[NR_syscalls];
232 	struct trace_event_file __rcu *exit_syscall_files[NR_syscalls];
233 #endif
234 	int			stop_count;
235 	int			clock_id;
236 	int			nr_topts;
237 	struct tracer		*current_trace;
238 	unsigned int		trace_flags;
239 	unsigned char		trace_flags_index[TRACE_FLAGS_MAX_SIZE];
240 	unsigned int		flags;
241 	raw_spinlock_t		start_lock;
242 	struct dentry		*dir;
243 	struct dentry		*options;
244 	struct dentry		*percpu_dir;
245 	struct dentry		*event_dir;
246 	struct trace_options	*topts;
247 	struct list_head	systems;
248 	struct list_head	events;
249 	cpumask_var_t		tracing_cpumask; /* only trace on set CPUs */
250 	int			ref;
251 #ifdef CONFIG_FUNCTION_TRACER
252 	struct ftrace_ops	*ops;
253 	struct trace_pid_list	__rcu *function_pids;
254 	/* function tracing enabled */
255 	int			function_enabled;
256 #endif
257 };
258 
259 enum {
260 	TRACE_ARRAY_FL_GLOBAL	= (1 << 0)
261 };
262 
263 extern struct list_head ftrace_trace_arrays;
264 
265 extern struct mutex trace_types_lock;
266 
267 extern int trace_array_get(struct trace_array *tr);
268 extern void trace_array_put(struct trace_array *tr);
269 
270 /*
271  * The global tracer (top) should be the first trace array added,
272  * but we check the flag anyway.
273  */
274 static inline struct trace_array *top_trace_array(void)
275 {
276 	struct trace_array *tr;
277 
278 	if (list_empty(&ftrace_trace_arrays))
279 		return NULL;
280 
281 	tr = list_entry(ftrace_trace_arrays.prev,
282 			typeof(*tr), list);
283 	WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
284 	return tr;
285 }
286 
287 #define FTRACE_CMP_TYPE(var, type) \
288 	__builtin_types_compatible_p(typeof(var), type *)
289 
290 #undef IF_ASSIGN
291 #define IF_ASSIGN(var, entry, etype, id)		\
292 	if (FTRACE_CMP_TYPE(var, etype)) {		\
293 		var = (typeof(var))(entry);		\
294 		WARN_ON(id && (entry)->type != id);	\
295 		break;					\
296 	}
297 
298 /* Will cause compile errors if type is not found. */
299 extern void __ftrace_bad_type(void);
300 
301 /*
302  * The trace_assign_type is a verifier that the entry type is
303  * the same as the type being assigned. To add new types simply
304  * add a line with the following format:
305  *
306  * IF_ASSIGN(var, ent, type, id);
307  *
308  *  Where "type" is the trace type that includes the trace_entry
309  *  as the "ent" item. And "id" is the trace identifier that is
310  *  used in the trace_type enum.
311  *
312  *  If the type can have more than one id, then use zero.
313  */
314 #define trace_assign_type(var, ent)					\
315 	do {								\
316 		IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN);	\
317 		IF_ASSIGN(var, ent, struct ctx_switch_entry, 0);	\
318 		IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK);	\
319 		IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
320 		IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT);	\
321 		IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT);	\
322 		IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS);	\
323 		IF_ASSIGN(var, ent, struct trace_mmiotrace_rw,		\
324 			  TRACE_MMIO_RW);				\
325 		IF_ASSIGN(var, ent, struct trace_mmiotrace_map,		\
326 			  TRACE_MMIO_MAP);				\
327 		IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
328 		IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry,	\
329 			  TRACE_GRAPH_ENT);		\
330 		IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry,	\
331 			  TRACE_GRAPH_RET);		\
332 		__ftrace_bad_type();					\
333 	} while (0)
334 
335 /*
336  * An option specific to a tracer. This is a boolean value.
337  * The bit is the bit index that sets its value on the
338  * flags value in struct tracer_flags.
339  */
340 struct tracer_opt {
341 	const char	*name; /* Will appear on the trace_options file */
342 	u32		bit; /* Mask assigned in val field in tracer_flags */
343 };
344 
345 /*
346  * The set of specific options for a tracer. Your tracer
347  * have to set the initial value of the flags val.
348  */
349 struct tracer_flags {
350 	u32			val;
351 	struct tracer_opt	*opts;
352 	struct tracer		*trace;
353 };
354 
355 /* Makes more easy to define a tracer opt */
356 #define TRACER_OPT(s, b)	.name = #s, .bit = b
357 
358 
359 struct trace_option_dentry {
360 	struct tracer_opt		*opt;
361 	struct tracer_flags		*flags;
362 	struct trace_array		*tr;
363 	struct dentry			*entry;
364 };
365 
366 /**
367  * struct tracer - a specific tracer and its callbacks to interact with tracefs
368  * @name: the name chosen to select it on the available_tracers file
369  * @init: called when one switches to this tracer (echo name > current_tracer)
370  * @reset: called when one switches to another tracer
371  * @start: called when tracing is unpaused (echo 1 > tracing_on)
372  * @stop: called when tracing is paused (echo 0 > tracing_on)
373  * @update_thresh: called when tracing_thresh is updated
374  * @open: called when the trace file is opened
375  * @pipe_open: called when the trace_pipe file is opened
376  * @close: called when the trace file is released
377  * @pipe_close: called when the trace_pipe file is released
378  * @read: override the default read callback on trace_pipe
379  * @splice_read: override the default splice_read callback on trace_pipe
380  * @selftest: selftest to run on boot (see trace_selftest.c)
381  * @print_headers: override the first lines that describe your columns
382  * @print_line: callback that prints a trace
383  * @set_flag: signals one of your private flags changed (trace_options file)
384  * @flags: your private flags
385  */
386 struct tracer {
387 	const char		*name;
388 	int			(*init)(struct trace_array *tr);
389 	void			(*reset)(struct trace_array *tr);
390 	void			(*start)(struct trace_array *tr);
391 	void			(*stop)(struct trace_array *tr);
392 	int			(*update_thresh)(struct trace_array *tr);
393 	void			(*open)(struct trace_iterator *iter);
394 	void			(*pipe_open)(struct trace_iterator *iter);
395 	void			(*close)(struct trace_iterator *iter);
396 	void			(*pipe_close)(struct trace_iterator *iter);
397 	ssize_t			(*read)(struct trace_iterator *iter,
398 					struct file *filp, char __user *ubuf,
399 					size_t cnt, loff_t *ppos);
400 	ssize_t			(*splice_read)(struct trace_iterator *iter,
401 					       struct file *filp,
402 					       loff_t *ppos,
403 					       struct pipe_inode_info *pipe,
404 					       size_t len,
405 					       unsigned int flags);
406 #ifdef CONFIG_FTRACE_STARTUP_TEST
407 	int			(*selftest)(struct tracer *trace,
408 					    struct trace_array *tr);
409 #endif
410 	void			(*print_header)(struct seq_file *m);
411 	enum print_line_t	(*print_line)(struct trace_iterator *iter);
412 	/* If you handled the flag setting, return 0 */
413 	int			(*set_flag)(struct trace_array *tr,
414 					    u32 old_flags, u32 bit, int set);
415 	/* Return 0 if OK with change, else return non-zero */
416 	int			(*flag_changed)(struct trace_array *tr,
417 						u32 mask, int set);
418 	struct tracer		*next;
419 	struct tracer_flags	*flags;
420 	int			enabled;
421 	int			ref;
422 	bool			print_max;
423 	bool			allow_instances;
424 #ifdef CONFIG_TRACER_MAX_TRACE
425 	bool			use_max_tr;
426 #endif
427 };
428 
429 
430 /* Only current can touch trace_recursion */
431 
432 /*
433  * For function tracing recursion:
434  *  The order of these bits are important.
435  *
436  *  When function tracing occurs, the following steps are made:
437  *   If arch does not support a ftrace feature:
438  *    call internal function (uses INTERNAL bits) which calls...
439  *   If callback is registered to the "global" list, the list
440  *    function is called and recursion checks the GLOBAL bits.
441  *    then this function calls...
442  *   The function callback, which can use the FTRACE bits to
443  *    check for recursion.
444  *
445  * Now if the arch does not suppport a feature, and it calls
446  * the global list function which calls the ftrace callback
447  * all three of these steps will do a recursion protection.
448  * There's no reason to do one if the previous caller already
449  * did. The recursion that we are protecting against will
450  * go through the same steps again.
451  *
452  * To prevent the multiple recursion checks, if a recursion
453  * bit is set that is higher than the MAX bit of the current
454  * check, then we know that the check was made by the previous
455  * caller, and we can skip the current check.
456  */
457 enum {
458 	TRACE_BUFFER_BIT,
459 	TRACE_BUFFER_NMI_BIT,
460 	TRACE_BUFFER_IRQ_BIT,
461 	TRACE_BUFFER_SIRQ_BIT,
462 
463 	/* Start of function recursion bits */
464 	TRACE_FTRACE_BIT,
465 	TRACE_FTRACE_NMI_BIT,
466 	TRACE_FTRACE_IRQ_BIT,
467 	TRACE_FTRACE_SIRQ_BIT,
468 
469 	/* INTERNAL_BITs must be greater than FTRACE_BITs */
470 	TRACE_INTERNAL_BIT,
471 	TRACE_INTERNAL_NMI_BIT,
472 	TRACE_INTERNAL_IRQ_BIT,
473 	TRACE_INTERNAL_SIRQ_BIT,
474 
475 	TRACE_BRANCH_BIT,
476 /*
477  * Abuse of the trace_recursion.
478  * As we need a way to maintain state if we are tracing the function
479  * graph in irq because we want to trace a particular function that
480  * was called in irq context but we have irq tracing off. Since this
481  * can only be modified by current, we can reuse trace_recursion.
482  */
483 	TRACE_IRQ_BIT,
484 };
485 
486 #define trace_recursion_set(bit)	do { (current)->trace_recursion |= (1<<(bit)); } while (0)
487 #define trace_recursion_clear(bit)	do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
488 #define trace_recursion_test(bit)	((current)->trace_recursion & (1<<(bit)))
489 
490 #define TRACE_CONTEXT_BITS	4
491 
492 #define TRACE_FTRACE_START	TRACE_FTRACE_BIT
493 #define TRACE_FTRACE_MAX	((1 << (TRACE_FTRACE_START + TRACE_CONTEXT_BITS)) - 1)
494 
495 #define TRACE_LIST_START	TRACE_INTERNAL_BIT
496 #define TRACE_LIST_MAX		((1 << (TRACE_LIST_START + TRACE_CONTEXT_BITS)) - 1)
497 
498 #define TRACE_CONTEXT_MASK	TRACE_LIST_MAX
499 
500 static __always_inline int trace_get_context_bit(void)
501 {
502 	int bit;
503 
504 	if (in_interrupt()) {
505 		if (in_nmi())
506 			bit = 0;
507 
508 		else if (in_irq())
509 			bit = 1;
510 		else
511 			bit = 2;
512 	} else
513 		bit = 3;
514 
515 	return bit;
516 }
517 
518 static __always_inline int trace_test_and_set_recursion(int start, int max)
519 {
520 	unsigned int val = current->trace_recursion;
521 	int bit;
522 
523 	/* A previous recursion check was made */
524 	if ((val & TRACE_CONTEXT_MASK) > max)
525 		return 0;
526 
527 	bit = trace_get_context_bit() + start;
528 	if (unlikely(val & (1 << bit)))
529 		return -1;
530 
531 	val |= 1 << bit;
532 	current->trace_recursion = val;
533 	barrier();
534 
535 	return bit;
536 }
537 
538 static __always_inline void trace_clear_recursion(int bit)
539 {
540 	unsigned int val = current->trace_recursion;
541 
542 	if (!bit)
543 		return;
544 
545 	bit = 1 << bit;
546 	val &= ~bit;
547 
548 	barrier();
549 	current->trace_recursion = val;
550 }
551 
552 static inline struct ring_buffer_iter *
553 trace_buffer_iter(struct trace_iterator *iter, int cpu)
554 {
555 	if (iter->buffer_iter && iter->buffer_iter[cpu])
556 		return iter->buffer_iter[cpu];
557 	return NULL;
558 }
559 
560 int tracer_init(struct tracer *t, struct trace_array *tr);
561 int tracing_is_enabled(void);
562 void tracing_reset(struct trace_buffer *buf, int cpu);
563 void tracing_reset_online_cpus(struct trace_buffer *buf);
564 void tracing_reset_current(int cpu);
565 void tracing_reset_all_online_cpus(void);
566 int tracing_open_generic(struct inode *inode, struct file *filp);
567 bool tracing_is_disabled(void);
568 struct dentry *trace_create_file(const char *name,
569 				 umode_t mode,
570 				 struct dentry *parent,
571 				 void *data,
572 				 const struct file_operations *fops);
573 
574 struct dentry *tracing_init_dentry(void);
575 
576 struct ring_buffer_event;
577 
578 struct ring_buffer_event *
579 trace_buffer_lock_reserve(struct ring_buffer *buffer,
580 			  int type,
581 			  unsigned long len,
582 			  unsigned long flags,
583 			  int pc);
584 
585 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
586 						struct trace_array_cpu *data);
587 
588 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
589 					  int *ent_cpu, u64 *ent_ts);
590 
591 void __buffer_unlock_commit(struct ring_buffer *buffer,
592 			    struct ring_buffer_event *event);
593 
594 int trace_empty(struct trace_iterator *iter);
595 
596 void *trace_find_next_entry_inc(struct trace_iterator *iter);
597 
598 void trace_init_global_iter(struct trace_iterator *iter);
599 
600 void tracing_iter_reset(struct trace_iterator *iter, int cpu);
601 
602 void trace_function(struct trace_array *tr,
603 		    unsigned long ip,
604 		    unsigned long parent_ip,
605 		    unsigned long flags, int pc);
606 void trace_graph_function(struct trace_array *tr,
607 		    unsigned long ip,
608 		    unsigned long parent_ip,
609 		    unsigned long flags, int pc);
610 void trace_latency_header(struct seq_file *m);
611 void trace_default_header(struct seq_file *m);
612 void print_trace_header(struct seq_file *m, struct trace_iterator *iter);
613 int trace_empty(struct trace_iterator *iter);
614 
615 void trace_graph_return(struct ftrace_graph_ret *trace);
616 int trace_graph_entry(struct ftrace_graph_ent *trace);
617 void set_graph_array(struct trace_array *tr);
618 
619 void tracing_start_cmdline_record(void);
620 void tracing_stop_cmdline_record(void);
621 int register_tracer(struct tracer *type);
622 int is_tracing_stopped(void);
623 
624 loff_t tracing_lseek(struct file *file, loff_t offset, int whence);
625 
626 extern cpumask_var_t __read_mostly tracing_buffer_mask;
627 
628 #define for_each_tracing_cpu(cpu)	\
629 	for_each_cpu(cpu, tracing_buffer_mask)
630 
631 extern unsigned long nsecs_to_usecs(unsigned long nsecs);
632 
633 extern unsigned long tracing_thresh;
634 
635 /* PID filtering */
636 
637 extern int pid_max;
638 
639 bool trace_find_filtered_pid(struct trace_pid_list *filtered_pids,
640 			     pid_t search_pid);
641 bool trace_ignore_this_task(struct trace_pid_list *filtered_pids,
642 			    struct task_struct *task);
643 void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
644 				  struct task_struct *self,
645 				  struct task_struct *task);
646 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos);
647 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos);
648 int trace_pid_show(struct seq_file *m, void *v);
649 void trace_free_pid_list(struct trace_pid_list *pid_list);
650 int trace_pid_write(struct trace_pid_list *filtered_pids,
651 		    struct trace_pid_list **new_pid_list,
652 		    const char __user *ubuf, size_t cnt);
653 
654 #ifdef CONFIG_TRACER_MAX_TRACE
655 void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
656 void update_max_tr_single(struct trace_array *tr,
657 			  struct task_struct *tsk, int cpu);
658 #endif /* CONFIG_TRACER_MAX_TRACE */
659 
660 #ifdef CONFIG_STACKTRACE
661 void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
662 			    int pc);
663 
664 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
665 		   int pc);
666 #else
667 static inline void ftrace_trace_userstack(struct ring_buffer *buffer,
668 					  unsigned long flags, int pc)
669 {
670 }
671 
672 static inline void __trace_stack(struct trace_array *tr, unsigned long flags,
673 				 int skip, int pc)
674 {
675 }
676 #endif /* CONFIG_STACKTRACE */
677 
678 extern cycle_t ftrace_now(int cpu);
679 
680 extern void trace_find_cmdline(int pid, char comm[]);
681 extern void trace_event_follow_fork(struct trace_array *tr, bool enable);
682 
683 #ifdef CONFIG_DYNAMIC_FTRACE
684 extern unsigned long ftrace_update_tot_cnt;
685 #endif
686 #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
687 extern int DYN_FTRACE_TEST_NAME(void);
688 #define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2
689 extern int DYN_FTRACE_TEST_NAME2(void);
690 
691 extern bool ring_buffer_expanded;
692 extern bool tracing_selftest_disabled;
693 
694 #ifdef CONFIG_FTRACE_STARTUP_TEST
695 extern int trace_selftest_startup_function(struct tracer *trace,
696 					   struct trace_array *tr);
697 extern int trace_selftest_startup_function_graph(struct tracer *trace,
698 						 struct trace_array *tr);
699 extern int trace_selftest_startup_irqsoff(struct tracer *trace,
700 					  struct trace_array *tr);
701 extern int trace_selftest_startup_preemptoff(struct tracer *trace,
702 					     struct trace_array *tr);
703 extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
704 						 struct trace_array *tr);
705 extern int trace_selftest_startup_wakeup(struct tracer *trace,
706 					 struct trace_array *tr);
707 extern int trace_selftest_startup_nop(struct tracer *trace,
708 					 struct trace_array *tr);
709 extern int trace_selftest_startup_sched_switch(struct tracer *trace,
710 					       struct trace_array *tr);
711 extern int trace_selftest_startup_branch(struct tracer *trace,
712 					 struct trace_array *tr);
713 /*
714  * Tracer data references selftest functions that only occur
715  * on boot up. These can be __init functions. Thus, when selftests
716  * are enabled, then the tracers need to reference __init functions.
717  */
718 #define __tracer_data		__refdata
719 #else
720 /* Tracers are seldom changed. Optimize when selftests are disabled. */
721 #define __tracer_data		__read_mostly
722 #endif /* CONFIG_FTRACE_STARTUP_TEST */
723 
724 extern void *head_page(struct trace_array_cpu *data);
725 extern unsigned long long ns2usecs(cycle_t nsec);
726 extern int
727 trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
728 extern int
729 trace_vprintk(unsigned long ip, const char *fmt, va_list args);
730 extern int
731 trace_array_vprintk(struct trace_array *tr,
732 		    unsigned long ip, const char *fmt, va_list args);
733 int trace_array_printk(struct trace_array *tr,
734 		       unsigned long ip, const char *fmt, ...);
735 int trace_array_printk_buf(struct ring_buffer *buffer,
736 			   unsigned long ip, const char *fmt, ...);
737 void trace_printk_seq(struct trace_seq *s);
738 enum print_line_t print_trace_line(struct trace_iterator *iter);
739 
740 extern char trace_find_mark(unsigned long long duration);
741 
742 /* Standard output formatting function used for function return traces */
743 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
744 
745 /* Flag options */
746 #define TRACE_GRAPH_PRINT_OVERRUN       0x1
747 #define TRACE_GRAPH_PRINT_CPU           0x2
748 #define TRACE_GRAPH_PRINT_OVERHEAD      0x4
749 #define TRACE_GRAPH_PRINT_PROC          0x8
750 #define TRACE_GRAPH_PRINT_DURATION      0x10
751 #define TRACE_GRAPH_PRINT_ABS_TIME      0x20
752 #define TRACE_GRAPH_PRINT_IRQS          0x40
753 #define TRACE_GRAPH_PRINT_TAIL          0x80
754 #define TRACE_GRAPH_SLEEP_TIME		0x100
755 #define TRACE_GRAPH_GRAPH_TIME		0x200
756 #define TRACE_GRAPH_PRINT_FILL_SHIFT	28
757 #define TRACE_GRAPH_PRINT_FILL_MASK	(0x3 << TRACE_GRAPH_PRINT_FILL_SHIFT)
758 
759 extern void ftrace_graph_sleep_time_control(bool enable);
760 extern void ftrace_graph_graph_time_control(bool enable);
761 
762 extern enum print_line_t
763 print_graph_function_flags(struct trace_iterator *iter, u32 flags);
764 extern void print_graph_headers_flags(struct seq_file *s, u32 flags);
765 extern void
766 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
767 extern void graph_trace_open(struct trace_iterator *iter);
768 extern void graph_trace_close(struct trace_iterator *iter);
769 extern int __trace_graph_entry(struct trace_array *tr,
770 			       struct ftrace_graph_ent *trace,
771 			       unsigned long flags, int pc);
772 extern void __trace_graph_return(struct trace_array *tr,
773 				 struct ftrace_graph_ret *trace,
774 				 unsigned long flags, int pc);
775 
776 
777 #ifdef CONFIG_DYNAMIC_FTRACE
778 /* TODO: make this variable */
779 #define FTRACE_GRAPH_MAX_FUNCS		32
780 extern int ftrace_graph_count;
781 extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS];
782 extern int ftrace_graph_notrace_count;
783 extern unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS];
784 
785 static inline int ftrace_graph_addr(unsigned long addr)
786 {
787 	int i;
788 
789 	if (!ftrace_graph_count)
790 		return 1;
791 
792 	for (i = 0; i < ftrace_graph_count; i++) {
793 		if (addr == ftrace_graph_funcs[i]) {
794 			/*
795 			 * If no irqs are to be traced, but a set_graph_function
796 			 * is set, and called by an interrupt handler, we still
797 			 * want to trace it.
798 			 */
799 			if (in_irq())
800 				trace_recursion_set(TRACE_IRQ_BIT);
801 			else
802 				trace_recursion_clear(TRACE_IRQ_BIT);
803 			return 1;
804 		}
805 	}
806 
807 	return 0;
808 }
809 
810 static inline int ftrace_graph_notrace_addr(unsigned long addr)
811 {
812 	int i;
813 
814 	if (!ftrace_graph_notrace_count)
815 		return 0;
816 
817 	for (i = 0; i < ftrace_graph_notrace_count; i++) {
818 		if (addr == ftrace_graph_notrace_funcs[i])
819 			return 1;
820 	}
821 
822 	return 0;
823 }
824 #else
825 static inline int ftrace_graph_addr(unsigned long addr)
826 {
827 	return 1;
828 }
829 
830 static inline int ftrace_graph_notrace_addr(unsigned long addr)
831 {
832 	return 0;
833 }
834 #endif /* CONFIG_DYNAMIC_FTRACE */
835 #else /* CONFIG_FUNCTION_GRAPH_TRACER */
836 static inline enum print_line_t
837 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
838 {
839 	return TRACE_TYPE_UNHANDLED;
840 }
841 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
842 
843 extern struct list_head ftrace_pids;
844 
845 #ifdef CONFIG_FUNCTION_TRACER
846 extern bool ftrace_filter_param __initdata;
847 static inline int ftrace_trace_task(struct trace_array *tr)
848 {
849 	return !this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid);
850 }
851 extern int ftrace_is_dead(void);
852 int ftrace_create_function_files(struct trace_array *tr,
853 				 struct dentry *parent);
854 void ftrace_destroy_function_files(struct trace_array *tr);
855 void ftrace_init_global_array_ops(struct trace_array *tr);
856 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func);
857 void ftrace_reset_array_ops(struct trace_array *tr);
858 int using_ftrace_ops_list_func(void);
859 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer);
860 #else
861 static inline int ftrace_trace_task(struct trace_array *tr)
862 {
863 	return 1;
864 }
865 static inline int ftrace_is_dead(void) { return 0; }
866 static inline int
867 ftrace_create_function_files(struct trace_array *tr,
868 			     struct dentry *parent)
869 {
870 	return 0;
871 }
872 static inline void ftrace_destroy_function_files(struct trace_array *tr) { }
873 static inline __init void
874 ftrace_init_global_array_ops(struct trace_array *tr) { }
875 static inline void ftrace_reset_array_ops(struct trace_array *tr) { }
876 static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { }
877 /* ftace_func_t type is not defined, use macro instead of static inline */
878 #define ftrace_init_array_ops(tr, func) do { } while (0)
879 #endif /* CONFIG_FUNCTION_TRACER */
880 
881 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
882 void ftrace_create_filter_files(struct ftrace_ops *ops,
883 				struct dentry *parent);
884 void ftrace_destroy_filter_files(struct ftrace_ops *ops);
885 #else
886 /*
887  * The ops parameter passed in is usually undefined.
888  * This must be a macro.
889  */
890 #define ftrace_create_filter_files(ops, parent) do { } while (0)
891 #define ftrace_destroy_filter_files(ops) do { } while (0)
892 #endif /* CONFIG_FUNCTION_TRACER && CONFIG_DYNAMIC_FTRACE */
893 
894 bool ftrace_event_is_function(struct trace_event_call *call);
895 
896 /*
897  * struct trace_parser - servers for reading the user input separated by spaces
898  * @cont: set if the input is not complete - no final space char was found
899  * @buffer: holds the parsed user input
900  * @idx: user input length
901  * @size: buffer size
902  */
903 struct trace_parser {
904 	bool		cont;
905 	char		*buffer;
906 	unsigned	idx;
907 	unsigned	size;
908 };
909 
910 static inline bool trace_parser_loaded(struct trace_parser *parser)
911 {
912 	return (parser->idx != 0);
913 }
914 
915 static inline bool trace_parser_cont(struct trace_parser *parser)
916 {
917 	return parser->cont;
918 }
919 
920 static inline void trace_parser_clear(struct trace_parser *parser)
921 {
922 	parser->cont = false;
923 	parser->idx = 0;
924 }
925 
926 extern int trace_parser_get_init(struct trace_parser *parser, int size);
927 extern void trace_parser_put(struct trace_parser *parser);
928 extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
929 	size_t cnt, loff_t *ppos);
930 
931 /*
932  * Only create function graph options if function graph is configured.
933  */
934 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
935 # define FGRAPH_FLAGS						\
936 		C(DISPLAY_GRAPH,	"display-graph"),
937 #else
938 # define FGRAPH_FLAGS
939 #endif
940 
941 #ifdef CONFIG_BRANCH_TRACER
942 # define BRANCH_FLAGS					\
943 		C(BRANCH,		"branch"),
944 #else
945 # define BRANCH_FLAGS
946 #endif
947 
948 #ifdef CONFIG_FUNCTION_TRACER
949 # define FUNCTION_FLAGS						\
950 		C(FUNCTION,		"function-trace"),
951 # define FUNCTION_DEFAULT_FLAGS		TRACE_ITER_FUNCTION
952 #else
953 # define FUNCTION_FLAGS
954 # define FUNCTION_DEFAULT_FLAGS		0UL
955 #endif
956 
957 #ifdef CONFIG_STACKTRACE
958 # define STACK_FLAGS				\
959 		C(STACKTRACE,		"stacktrace"),
960 #else
961 # define STACK_FLAGS
962 #endif
963 
964 /*
965  * trace_iterator_flags is an enumeration that defines bit
966  * positions into trace_flags that controls the output.
967  *
968  * NOTE: These bits must match the trace_options array in
969  *       trace.c (this macro guarantees it).
970  */
971 #define TRACE_FLAGS						\
972 		C(PRINT_PARENT,		"print-parent"),	\
973 		C(SYM_OFFSET,		"sym-offset"),		\
974 		C(SYM_ADDR,		"sym-addr"),		\
975 		C(VERBOSE,		"verbose"),		\
976 		C(RAW,			"raw"),			\
977 		C(HEX,			"hex"),			\
978 		C(BIN,			"bin"),			\
979 		C(BLOCK,		"block"),		\
980 		C(PRINTK,		"trace_printk"),	\
981 		C(ANNOTATE,		"annotate"),		\
982 		C(USERSTACKTRACE,	"userstacktrace"),	\
983 		C(SYM_USEROBJ,		"sym-userobj"),		\
984 		C(PRINTK_MSGONLY,	"printk-msg-only"),	\
985 		C(CONTEXT_INFO,		"context-info"),   /* Print pid/cpu/time */ \
986 		C(LATENCY_FMT,		"latency-format"),	\
987 		C(RECORD_CMD,		"record-cmd"),		\
988 		C(OVERWRITE,		"overwrite"),		\
989 		C(STOP_ON_FREE,		"disable_on_free"),	\
990 		C(IRQ_INFO,		"irq-info"),		\
991 		C(MARKERS,		"markers"),		\
992 		C(EVENT_FORK,		"event-fork"),		\
993 		FUNCTION_FLAGS					\
994 		FGRAPH_FLAGS					\
995 		STACK_FLAGS					\
996 		BRANCH_FLAGS
997 
998 /*
999  * By defining C, we can make TRACE_FLAGS a list of bit names
1000  * that will define the bits for the flag masks.
1001  */
1002 #undef C
1003 #define C(a, b) TRACE_ITER_##a##_BIT
1004 
1005 enum trace_iterator_bits {
1006 	TRACE_FLAGS
1007 	/* Make sure we don't go more than we have bits for */
1008 	TRACE_ITER_LAST_BIT
1009 };
1010 
1011 /*
1012  * By redefining C, we can make TRACE_FLAGS a list of masks that
1013  * use the bits as defined above.
1014  */
1015 #undef C
1016 #define C(a, b) TRACE_ITER_##a = (1 << TRACE_ITER_##a##_BIT)
1017 
1018 enum trace_iterator_flags { TRACE_FLAGS };
1019 
1020 /*
1021  * TRACE_ITER_SYM_MASK masks the options in trace_flags that
1022  * control the output of kernel symbols.
1023  */
1024 #define TRACE_ITER_SYM_MASK \
1025 	(TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
1026 
1027 extern struct tracer nop_trace;
1028 
1029 #ifdef CONFIG_BRANCH_TRACER
1030 extern int enable_branch_tracing(struct trace_array *tr);
1031 extern void disable_branch_tracing(void);
1032 static inline int trace_branch_enable(struct trace_array *tr)
1033 {
1034 	if (tr->trace_flags & TRACE_ITER_BRANCH)
1035 		return enable_branch_tracing(tr);
1036 	return 0;
1037 }
1038 static inline void trace_branch_disable(void)
1039 {
1040 	/* due to races, always disable */
1041 	disable_branch_tracing();
1042 }
1043 #else
1044 static inline int trace_branch_enable(struct trace_array *tr)
1045 {
1046 	return 0;
1047 }
1048 static inline void trace_branch_disable(void)
1049 {
1050 }
1051 #endif /* CONFIG_BRANCH_TRACER */
1052 
1053 /* set ring buffers to default size if not already done so */
1054 int tracing_update_buffers(void);
1055 
1056 struct ftrace_event_field {
1057 	struct list_head	link;
1058 	const char		*name;
1059 	const char		*type;
1060 	int			filter_type;
1061 	int			offset;
1062 	int			size;
1063 	int			is_signed;
1064 };
1065 
1066 struct event_filter {
1067 	int			n_preds;	/* Number assigned */
1068 	int			a_preds;	/* allocated */
1069 	struct filter_pred	*preds;
1070 	struct filter_pred	*root;
1071 	char			*filter_string;
1072 };
1073 
1074 struct event_subsystem {
1075 	struct list_head	list;
1076 	const char		*name;
1077 	struct event_filter	*filter;
1078 	int			ref_count;
1079 };
1080 
1081 struct trace_subsystem_dir {
1082 	struct list_head		list;
1083 	struct event_subsystem		*subsystem;
1084 	struct trace_array		*tr;
1085 	struct dentry			*entry;
1086 	int				ref_count;
1087 	int				nr_events;
1088 };
1089 
1090 extern int call_filter_check_discard(struct trace_event_call *call, void *rec,
1091 				     struct ring_buffer *buffer,
1092 				     struct ring_buffer_event *event);
1093 
1094 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
1095 				     struct ring_buffer *buffer,
1096 				     struct ring_buffer_event *event,
1097 				     unsigned long flags, int pc,
1098 				     struct pt_regs *regs);
1099 
1100 static inline void trace_buffer_unlock_commit(struct trace_array *tr,
1101 					      struct ring_buffer *buffer,
1102 					      struct ring_buffer_event *event,
1103 					      unsigned long flags, int pc)
1104 {
1105 	trace_buffer_unlock_commit_regs(tr, buffer, event, flags, pc, NULL);
1106 }
1107 
1108 DECLARE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
1109 DECLARE_PER_CPU(int, trace_buffered_event_cnt);
1110 void trace_buffered_event_disable(void);
1111 void trace_buffered_event_enable(void);
1112 
1113 static inline void
1114 __trace_event_discard_commit(struct ring_buffer *buffer,
1115 			     struct ring_buffer_event *event)
1116 {
1117 	if (this_cpu_read(trace_buffered_event) == event) {
1118 		/* Simply release the temp buffer */
1119 		this_cpu_dec(trace_buffered_event_cnt);
1120 		return;
1121 	}
1122 	ring_buffer_discard_commit(buffer, event);
1123 }
1124 
1125 /*
1126  * Helper function for event_trigger_unlock_commit{_regs}().
1127  * If there are event triggers attached to this event that requires
1128  * filtering against its fields, then they wil be called as the
1129  * entry already holds the field information of the current event.
1130  *
1131  * It also checks if the event should be discarded or not.
1132  * It is to be discarded if the event is soft disabled and the
1133  * event was only recorded to process triggers, or if the event
1134  * filter is active and this event did not match the filters.
1135  *
1136  * Returns true if the event is discarded, false otherwise.
1137  */
1138 static inline bool
1139 __event_trigger_test_discard(struct trace_event_file *file,
1140 			     struct ring_buffer *buffer,
1141 			     struct ring_buffer_event *event,
1142 			     void *entry,
1143 			     enum event_trigger_type *tt)
1144 {
1145 	unsigned long eflags = file->flags;
1146 
1147 	if (eflags & EVENT_FILE_FL_TRIGGER_COND)
1148 		*tt = event_triggers_call(file, entry);
1149 
1150 	if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
1151 	    (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
1152 	     !filter_match_preds(file->filter, entry))) {
1153 		__trace_event_discard_commit(buffer, event);
1154 		return true;
1155 	}
1156 
1157 	return false;
1158 }
1159 
1160 /**
1161  * event_trigger_unlock_commit - handle triggers and finish event commit
1162  * @file: The file pointer assoctiated to the event
1163  * @buffer: The ring buffer that the event is being written to
1164  * @event: The event meta data in the ring buffer
1165  * @entry: The event itself
1166  * @irq_flags: The state of the interrupts at the start of the event
1167  * @pc: The state of the preempt count at the start of the event.
1168  *
1169  * This is a helper function to handle triggers that require data
1170  * from the event itself. It also tests the event against filters and
1171  * if the event is soft disabled and should be discarded.
1172  */
1173 static inline void
1174 event_trigger_unlock_commit(struct trace_event_file *file,
1175 			    struct ring_buffer *buffer,
1176 			    struct ring_buffer_event *event,
1177 			    void *entry, unsigned long irq_flags, int pc)
1178 {
1179 	enum event_trigger_type tt = ETT_NONE;
1180 
1181 	if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
1182 		trace_buffer_unlock_commit(file->tr, buffer, event, irq_flags, pc);
1183 
1184 	if (tt)
1185 		event_triggers_post_call(file, tt, entry);
1186 }
1187 
1188 /**
1189  * event_trigger_unlock_commit_regs - handle triggers and finish event commit
1190  * @file: The file pointer assoctiated to the event
1191  * @buffer: The ring buffer that the event is being written to
1192  * @event: The event meta data in the ring buffer
1193  * @entry: The event itself
1194  * @irq_flags: The state of the interrupts at the start of the event
1195  * @pc: The state of the preempt count at the start of the event.
1196  *
1197  * This is a helper function to handle triggers that require data
1198  * from the event itself. It also tests the event against filters and
1199  * if the event is soft disabled and should be discarded.
1200  *
1201  * Same as event_trigger_unlock_commit() but calls
1202  * trace_buffer_unlock_commit_regs() instead of trace_buffer_unlock_commit().
1203  */
1204 static inline void
1205 event_trigger_unlock_commit_regs(struct trace_event_file *file,
1206 				 struct ring_buffer *buffer,
1207 				 struct ring_buffer_event *event,
1208 				 void *entry, unsigned long irq_flags, int pc,
1209 				 struct pt_regs *regs)
1210 {
1211 	enum event_trigger_type tt = ETT_NONE;
1212 
1213 	if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
1214 		trace_buffer_unlock_commit_regs(file->tr, buffer, event,
1215 						irq_flags, pc, regs);
1216 
1217 	if (tt)
1218 		event_triggers_post_call(file, tt, entry);
1219 }
1220 
1221 #define FILTER_PRED_INVALID	((unsigned short)-1)
1222 #define FILTER_PRED_IS_RIGHT	(1 << 15)
1223 #define FILTER_PRED_FOLD	(1 << 15)
1224 
1225 /*
1226  * The max preds is the size of unsigned short with
1227  * two flags at the MSBs. One bit is used for both the IS_RIGHT
1228  * and FOLD flags. The other is reserved.
1229  *
1230  * 2^14 preds is way more than enough.
1231  */
1232 #define MAX_FILTER_PRED		16384
1233 
1234 struct filter_pred;
1235 struct regex;
1236 
1237 typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event);
1238 
1239 typedef int (*regex_match_func)(char *str, struct regex *r, int len);
1240 
1241 enum regex_type {
1242 	MATCH_FULL = 0,
1243 	MATCH_FRONT_ONLY,
1244 	MATCH_MIDDLE_ONLY,
1245 	MATCH_END_ONLY,
1246 };
1247 
1248 struct regex {
1249 	char			pattern[MAX_FILTER_STR_VAL];
1250 	int			len;
1251 	int			field_len;
1252 	regex_match_func	match;
1253 };
1254 
1255 struct filter_pred {
1256 	filter_pred_fn_t 	fn;
1257 	u64 			val;
1258 	struct regex		regex;
1259 	unsigned short		*ops;
1260 	struct ftrace_event_field *field;
1261 	int 			offset;
1262 	int 			not;
1263 	int 			op;
1264 	unsigned short		index;
1265 	unsigned short		parent;
1266 	unsigned short		left;
1267 	unsigned short		right;
1268 };
1269 
1270 static inline bool is_string_field(struct ftrace_event_field *field)
1271 {
1272 	return field->filter_type == FILTER_DYN_STRING ||
1273 	       field->filter_type == FILTER_STATIC_STRING ||
1274 	       field->filter_type == FILTER_PTR_STRING;
1275 }
1276 
1277 static inline bool is_function_field(struct ftrace_event_field *field)
1278 {
1279 	return field->filter_type == FILTER_TRACE_FN;
1280 }
1281 
1282 extern enum regex_type
1283 filter_parse_regex(char *buff, int len, char **search, int *not);
1284 extern void print_event_filter(struct trace_event_file *file,
1285 			       struct trace_seq *s);
1286 extern int apply_event_filter(struct trace_event_file *file,
1287 			      char *filter_string);
1288 extern int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
1289 					char *filter_string);
1290 extern void print_subsystem_event_filter(struct event_subsystem *system,
1291 					 struct trace_seq *s);
1292 extern int filter_assign_type(const char *type);
1293 extern int create_event_filter(struct trace_event_call *call,
1294 			       char *filter_str, bool set_str,
1295 			       struct event_filter **filterp);
1296 extern void free_event_filter(struct event_filter *filter);
1297 
1298 struct ftrace_event_field *
1299 trace_find_event_field(struct trace_event_call *call, char *name);
1300 
1301 extern void trace_event_enable_cmd_record(bool enable);
1302 extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
1303 extern int event_trace_del_tracer(struct trace_array *tr);
1304 
1305 extern struct trace_event_file *find_event_file(struct trace_array *tr,
1306 						const char *system,
1307 						const char *event);
1308 
1309 static inline void *event_file_data(struct file *filp)
1310 {
1311 	return ACCESS_ONCE(file_inode(filp)->i_private);
1312 }
1313 
1314 extern struct mutex event_mutex;
1315 extern struct list_head ftrace_events;
1316 
1317 extern const struct file_operations event_trigger_fops;
1318 extern const struct file_operations event_hist_fops;
1319 
1320 #ifdef CONFIG_HIST_TRIGGERS
1321 extern int register_trigger_hist_cmd(void);
1322 extern int register_trigger_hist_enable_disable_cmds(void);
1323 #else
1324 static inline int register_trigger_hist_cmd(void) { return 0; }
1325 static inline int register_trigger_hist_enable_disable_cmds(void) { return 0; }
1326 #endif
1327 
1328 extern int register_trigger_cmds(void);
1329 extern void clear_event_triggers(struct trace_array *tr);
1330 
1331 struct event_trigger_data {
1332 	unsigned long			count;
1333 	int				ref;
1334 	struct event_trigger_ops	*ops;
1335 	struct event_command		*cmd_ops;
1336 	struct event_filter __rcu	*filter;
1337 	char				*filter_str;
1338 	void				*private_data;
1339 	bool				paused;
1340 	bool				paused_tmp;
1341 	struct list_head		list;
1342 	char				*name;
1343 	struct list_head		named_list;
1344 	struct event_trigger_data	*named_data;
1345 };
1346 
1347 /* Avoid typos */
1348 #define ENABLE_EVENT_STR	"enable_event"
1349 #define DISABLE_EVENT_STR	"disable_event"
1350 #define ENABLE_HIST_STR		"enable_hist"
1351 #define DISABLE_HIST_STR	"disable_hist"
1352 
1353 struct enable_trigger_data {
1354 	struct trace_event_file		*file;
1355 	bool				enable;
1356 	bool				hist;
1357 };
1358 
1359 extern int event_enable_trigger_print(struct seq_file *m,
1360 				      struct event_trigger_ops *ops,
1361 				      struct event_trigger_data *data);
1362 extern void event_enable_trigger_free(struct event_trigger_ops *ops,
1363 				      struct event_trigger_data *data);
1364 extern int event_enable_trigger_func(struct event_command *cmd_ops,
1365 				     struct trace_event_file *file,
1366 				     char *glob, char *cmd, char *param);
1367 extern int event_enable_register_trigger(char *glob,
1368 					 struct event_trigger_ops *ops,
1369 					 struct event_trigger_data *data,
1370 					 struct trace_event_file *file);
1371 extern void event_enable_unregister_trigger(char *glob,
1372 					    struct event_trigger_ops *ops,
1373 					    struct event_trigger_data *test,
1374 					    struct trace_event_file *file);
1375 extern void trigger_data_free(struct event_trigger_data *data);
1376 extern int event_trigger_init(struct event_trigger_ops *ops,
1377 			      struct event_trigger_data *data);
1378 extern int trace_event_trigger_enable_disable(struct trace_event_file *file,
1379 					      int trigger_enable);
1380 extern void update_cond_flag(struct trace_event_file *file);
1381 extern void unregister_trigger(char *glob, struct event_trigger_ops *ops,
1382 			       struct event_trigger_data *test,
1383 			       struct trace_event_file *file);
1384 extern int set_trigger_filter(char *filter_str,
1385 			      struct event_trigger_data *trigger_data,
1386 			      struct trace_event_file *file);
1387 extern struct event_trigger_data *find_named_trigger(const char *name);
1388 extern bool is_named_trigger(struct event_trigger_data *test);
1389 extern int save_named_trigger(const char *name,
1390 			      struct event_trigger_data *data);
1391 extern void del_named_trigger(struct event_trigger_data *data);
1392 extern void pause_named_trigger(struct event_trigger_data *data);
1393 extern void unpause_named_trigger(struct event_trigger_data *data);
1394 extern void set_named_trigger_data(struct event_trigger_data *data,
1395 				   struct event_trigger_data *named_data);
1396 extern int register_event_command(struct event_command *cmd);
1397 extern int unregister_event_command(struct event_command *cmd);
1398 extern int register_trigger_hist_enable_disable_cmds(void);
1399 
1400 /**
1401  * struct event_trigger_ops - callbacks for trace event triggers
1402  *
1403  * The methods in this structure provide per-event trigger hooks for
1404  * various trigger operations.
1405  *
1406  * All the methods below, except for @init() and @free(), must be
1407  * implemented.
1408  *
1409  * @func: The trigger 'probe' function called when the triggering
1410  *	event occurs.  The data passed into this callback is the data
1411  *	that was supplied to the event_command @reg() function that
1412  *	registered the trigger (see struct event_command) along with
1413  *	the trace record, rec.
1414  *
1415  * @init: An optional initialization function called for the trigger
1416  *	when the trigger is registered (via the event_command reg()
1417  *	function).  This can be used to perform per-trigger
1418  *	initialization such as incrementing a per-trigger reference
1419  *	count, for instance.  This is usually implemented by the
1420  *	generic utility function @event_trigger_init() (see
1421  *	trace_event_triggers.c).
1422  *
1423  * @free: An optional de-initialization function called for the
1424  *	trigger when the trigger is unregistered (via the
1425  *	event_command @reg() function).  This can be used to perform
1426  *	per-trigger de-initialization such as decrementing a
1427  *	per-trigger reference count and freeing corresponding trigger
1428  *	data, for instance.  This is usually implemented by the
1429  *	generic utility function @event_trigger_free() (see
1430  *	trace_event_triggers.c).
1431  *
1432  * @print: The callback function invoked to have the trigger print
1433  *	itself.  This is usually implemented by a wrapper function
1434  *	that calls the generic utility function @event_trigger_print()
1435  *	(see trace_event_triggers.c).
1436  */
1437 struct event_trigger_ops {
1438 	void			(*func)(struct event_trigger_data *data,
1439 					void *rec);
1440 	int			(*init)(struct event_trigger_ops *ops,
1441 					struct event_trigger_data *data);
1442 	void			(*free)(struct event_trigger_ops *ops,
1443 					struct event_trigger_data *data);
1444 	int			(*print)(struct seq_file *m,
1445 					 struct event_trigger_ops *ops,
1446 					 struct event_trigger_data *data);
1447 };
1448 
1449 /**
1450  * struct event_command - callbacks and data members for event commands
1451  *
1452  * Event commands are invoked by users by writing the command name
1453  * into the 'trigger' file associated with a trace event.  The
1454  * parameters associated with a specific invocation of an event
1455  * command are used to create an event trigger instance, which is
1456  * added to the list of trigger instances associated with that trace
1457  * event.  When the event is hit, the set of triggers associated with
1458  * that event is invoked.
1459  *
1460  * The data members in this structure provide per-event command data
1461  * for various event commands.
1462  *
1463  * All the data members below, except for @post_trigger, must be set
1464  * for each event command.
1465  *
1466  * @name: The unique name that identifies the event command.  This is
1467  *	the name used when setting triggers via trigger files.
1468  *
1469  * @trigger_type: A unique id that identifies the event command
1470  *	'type'.  This value has two purposes, the first to ensure that
1471  *	only one trigger of the same type can be set at a given time
1472  *	for a particular event e.g. it doesn't make sense to have both
1473  *	a traceon and traceoff trigger attached to a single event at
1474  *	the same time, so traceon and traceoff have the same type
1475  *	though they have different names.  The @trigger_type value is
1476  *	also used as a bit value for deferring the actual trigger
1477  *	action until after the current event is finished.  Some
1478  *	commands need to do this if they themselves log to the trace
1479  *	buffer (see the @post_trigger() member below).  @trigger_type
1480  *	values are defined by adding new values to the trigger_type
1481  *	enum in include/linux/trace_events.h.
1482  *
1483  * @flags: See the enum event_command_flags below.
1484  *
1485  * All the methods below, except for @set_filter() and @unreg_all(),
1486  * must be implemented.
1487  *
1488  * @func: The callback function responsible for parsing and
1489  *	registering the trigger written to the 'trigger' file by the
1490  *	user.  It allocates the trigger instance and registers it with
1491  *	the appropriate trace event.  It makes use of the other
1492  *	event_command callback functions to orchestrate this, and is
1493  *	usually implemented by the generic utility function
1494  *	@event_trigger_callback() (see trace_event_triggers.c).
1495  *
1496  * @reg: Adds the trigger to the list of triggers associated with the
1497  *	event, and enables the event trigger itself, after
1498  *	initializing it (via the event_trigger_ops @init() function).
1499  *	This is also where commands can use the @trigger_type value to
1500  *	make the decision as to whether or not multiple instances of
1501  *	the trigger should be allowed.  This is usually implemented by
1502  *	the generic utility function @register_trigger() (see
1503  *	trace_event_triggers.c).
1504  *
1505  * @unreg: Removes the trigger from the list of triggers associated
1506  *	with the event, and disables the event trigger itself, after
1507  *	initializing it (via the event_trigger_ops @free() function).
1508  *	This is usually implemented by the generic utility function
1509  *	@unregister_trigger() (see trace_event_triggers.c).
1510  *
1511  * @unreg_all: An optional function called to remove all the triggers
1512  *	from the list of triggers associated with the event.  Called
1513  *	when a trigger file is opened in truncate mode.
1514  *
1515  * @set_filter: An optional function called to parse and set a filter
1516  *	for the trigger.  If no @set_filter() method is set for the
1517  *	event command, filters set by the user for the command will be
1518  *	ignored.  This is usually implemented by the generic utility
1519  *	function @set_trigger_filter() (see trace_event_triggers.c).
1520  *
1521  * @get_trigger_ops: The callback function invoked to retrieve the
1522  *	event_trigger_ops implementation associated with the command.
1523  */
1524 struct event_command {
1525 	struct list_head	list;
1526 	char			*name;
1527 	enum event_trigger_type	trigger_type;
1528 	int			flags;
1529 	int			(*func)(struct event_command *cmd_ops,
1530 					struct trace_event_file *file,
1531 					char *glob, char *cmd, char *params);
1532 	int			(*reg)(char *glob,
1533 				       struct event_trigger_ops *ops,
1534 				       struct event_trigger_data *data,
1535 				       struct trace_event_file *file);
1536 	void			(*unreg)(char *glob,
1537 					 struct event_trigger_ops *ops,
1538 					 struct event_trigger_data *data,
1539 					 struct trace_event_file *file);
1540 	void			(*unreg_all)(struct trace_event_file *file);
1541 	int			(*set_filter)(char *filter_str,
1542 					      struct event_trigger_data *data,
1543 					      struct trace_event_file *file);
1544 	struct event_trigger_ops *(*get_trigger_ops)(char *cmd, char *param);
1545 };
1546 
1547 /**
1548  * enum event_command_flags - flags for struct event_command
1549  *
1550  * @POST_TRIGGER: A flag that says whether or not this command needs
1551  *	to have its action delayed until after the current event has
1552  *	been closed.  Some triggers need to avoid being invoked while
1553  *	an event is currently in the process of being logged, since
1554  *	the trigger may itself log data into the trace buffer.  Thus
1555  *	we make sure the current event is committed before invoking
1556  *	those triggers.  To do that, the trigger invocation is split
1557  *	in two - the first part checks the filter using the current
1558  *	trace record; if a command has the @post_trigger flag set, it
1559  *	sets a bit for itself in the return value, otherwise it
1560  *	directly invokes the trigger.  Once all commands have been
1561  *	either invoked or set their return flag, the current record is
1562  *	either committed or discarded.  At that point, if any commands
1563  *	have deferred their triggers, those commands are finally
1564  *	invoked following the close of the current event.  In other
1565  *	words, if the event_trigger_ops @func() probe implementation
1566  *	itself logs to the trace buffer, this flag should be set,
1567  *	otherwise it can be left unspecified.
1568  *
1569  * @NEEDS_REC: A flag that says whether or not this command needs
1570  *	access to the trace record in order to perform its function,
1571  *	regardless of whether or not it has a filter associated with
1572  *	it (filters make a trigger require access to the trace record
1573  *	but are not always present).
1574  */
1575 enum event_command_flags {
1576 	EVENT_CMD_FL_POST_TRIGGER	= 1,
1577 	EVENT_CMD_FL_NEEDS_REC		= 2,
1578 };
1579 
1580 static inline bool event_command_post_trigger(struct event_command *cmd_ops)
1581 {
1582 	return cmd_ops->flags & EVENT_CMD_FL_POST_TRIGGER;
1583 }
1584 
1585 static inline bool event_command_needs_rec(struct event_command *cmd_ops)
1586 {
1587 	return cmd_ops->flags & EVENT_CMD_FL_NEEDS_REC;
1588 }
1589 
1590 extern int trace_event_enable_disable(struct trace_event_file *file,
1591 				      int enable, int soft_disable);
1592 extern int tracing_alloc_snapshot(void);
1593 
1594 extern const char *__start___trace_bprintk_fmt[];
1595 extern const char *__stop___trace_bprintk_fmt[];
1596 
1597 extern const char *__start___tracepoint_str[];
1598 extern const char *__stop___tracepoint_str[];
1599 
1600 void trace_printk_control(bool enabled);
1601 void trace_printk_init_buffers(void);
1602 void trace_printk_start_comm(void);
1603 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set);
1604 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled);
1605 
1606 /*
1607  * Normal trace_printk() and friends allocates special buffers
1608  * to do the manipulation, as well as saves the print formats
1609  * into sections to display. But the trace infrastructure wants
1610  * to use these without the added overhead at the price of being
1611  * a bit slower (used mainly for warnings, where we don't care
1612  * about performance). The internal_trace_puts() is for such
1613  * a purpose.
1614  */
1615 #define internal_trace_puts(str) __trace_puts(_THIS_IP_, str, strlen(str))
1616 
1617 #undef FTRACE_ENTRY
1618 #define FTRACE_ENTRY(call, struct_name, id, tstruct, print, filter)	\
1619 	extern struct trace_event_call					\
1620 	__aligned(4) event_##call;
1621 #undef FTRACE_ENTRY_DUP
1622 #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print, filter)	\
1623 	FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print), \
1624 		     filter)
1625 #include "trace_entries.h"
1626 
1627 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_FUNCTION_TRACER)
1628 int perf_ftrace_event_register(struct trace_event_call *call,
1629 			       enum trace_reg type, void *data);
1630 #else
1631 #define perf_ftrace_event_register NULL
1632 #endif
1633 
1634 #ifdef CONFIG_FTRACE_SYSCALLS
1635 void init_ftrace_syscalls(void);
1636 const char *get_syscall_name(int syscall);
1637 #else
1638 static inline void init_ftrace_syscalls(void) { }
1639 static inline const char *get_syscall_name(int syscall)
1640 {
1641 	return NULL;
1642 }
1643 #endif
1644 
1645 #ifdef CONFIG_EVENT_TRACING
1646 void trace_event_init(void);
1647 void trace_event_enum_update(struct trace_enum_map **map, int len);
1648 #else
1649 static inline void __init trace_event_init(void) { }
1650 static inline void trace_event_enum_update(struct trace_enum_map **map, int len) { }
1651 #endif
1652 
1653 extern struct trace_iterator *tracepoint_print_iter;
1654 
1655 #endif /* _LINUX_KERNEL_TRACE_H */
1656