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