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