trace.h 59.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2

3 4 5 6
#ifndef _LINUX_KERNEL_TRACE_H
#define _LINUX_KERNEL_TRACE_H

#include <linux/fs.h>
Arun Sharma's avatar
Arun Sharma committed
7
#include <linux/atomic.h>
8 9
#include <linux/sched.h>
#include <linux/clocksource.h>
10
#include <linux/ring_buffer.h>
Pekka Paalanen's avatar
Pekka Paalanen committed
11
#include <linux/mmiotrace.h>
12
#include <linux/tracepoint.h>
13
#include <linux/ftrace.h>
14
#include <linux/hw_breakpoint.h>
15
#include <linux/trace_seq.h>
16
#include <linux/trace_events.h>
17
#include <linux/compiler.h>
18
#include <linux/trace_seq.h>
19
#include <linux/glob.h>
20

21 22 23 24 25
#ifdef CONFIG_FTRACE_SYSCALLS
#include <asm/unistd.h>		/* For NR_SYSCALLS	     */
#include <asm/syscall.h>	/* some archs define it here */
#endif

26 27 28 29 30 31 32
enum trace_type {
	__TRACE_FIRST_TYPE = 0,

	TRACE_FN,
	TRACE_CTX,
	TRACE_WAKE,
	TRACE_STACK,
33
	TRACE_PRINT,
34
	TRACE_BPRINT,
Pekka Paalanen's avatar
Pekka Paalanen committed
35 36
	TRACE_MMIO_RW,
	TRACE_MMIO_MAP,
37
	TRACE_BRANCH,
38 39
	TRACE_GRAPH_RET,
	TRACE_GRAPH_ENT,
40
	TRACE_USER_STACK,
41
	TRACE_BLK,
42
	TRACE_BPUTS,
43
	TRACE_HWLAT,
44
	TRACE_RAW_DATA,
45

46
	__TRACE_LAST_TYPE,
47 48
};

49

50 51
#undef __field
#define __field(type, item)		type	item;
Ingo Molnar's avatar
Ingo Molnar committed
52

53 54
#undef __field_struct
#define __field_struct(type, item)	__field(type, item)
Ingo Molnar's avatar
Ingo Molnar committed
55

56 57
#undef __field_desc
#define __field_desc(type, container, item)
58

59 60
#undef __array
#define __array(type, item, size)	type	item[size];
61

62 63
#undef __array_desc
#define __array_desc(type, container, item, size)
64

65 66
#undef __dynamic_array
#define __dynamic_array(type, item)	type	item[];
67

68 69
#undef F_STRUCT
#define F_STRUCT(args...)		args
70

71
#undef FTRACE_ENTRY
72 73 74 75
#define FTRACE_ENTRY(name, struct_name, id, tstruct, print, filter)	\
	struct struct_name {						\
		struct trace_entry	ent;				\
		tstruct							\
76
	}
77

78
#undef FTRACE_ENTRY_DUP
79
#define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk, filter)
80

81
#undef FTRACE_ENTRY_REG
82 83 84 85
#define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print,	\
			 filter, regfn) \
	FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print), \
		     filter)
86

87 88 89 90 91 92
#undef FTRACE_ENTRY_PACKED
#define FTRACE_ENTRY_PACKED(name, struct_name, id, tstruct, print,	\
			    filter)					\
	FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print), \
		     filter) __packed

93
#include "trace_entries.h"
94

95 96 97 98
/*
 * syscalls are special, and need special handling, this is why
 * they are not included in trace_entries.h
 */
99 100 101 102 103 104 105 106 107
struct syscall_trace_enter {
	struct trace_entry	ent;
	int			nr;
	unsigned long		args[];
};

struct syscall_trace_exit {
	struct trace_entry	ent;
	int			nr;
108
	long			ret;
109 110
};

111
struct kprobe_trace_entry_head {
112 113 114 115
	struct trace_entry	ent;
	unsigned long		ip;
};

116
struct kretprobe_trace_entry_head {
117 118 119 120 121
	struct trace_entry	ent;
	unsigned long		func;
	unsigned long		ret_ip;
};

122 123 124
/*
 * trace_flag_type is an enumeration that holds different
 * states when a trace occurs. These are:
125
 *  IRQS_OFF		- interrupts were disabled
126
 *  IRQS_NOSUPPORT	- arch does not support irqs_disabled_flags
Li Zefan's avatar
Li Zefan committed
127
 *  NEED_RESCHED	- reschedule is requested
128 129
 *  HARDIRQ		- inside an interrupt handler
 *  SOFTIRQ		- inside a softirq handler
130 131 132
 */
enum trace_flag_type {
	TRACE_FLAG_IRQS_OFF		= 0x01,
133 134 135 136
	TRACE_FLAG_IRQS_NOSUPPORT	= 0x02,
	TRACE_FLAG_NEED_RESCHED		= 0x04,
	TRACE_FLAG_HARDIRQ		= 0x08,
	TRACE_FLAG_SOFTIRQ		= 0x10,
137
	TRACE_FLAG_PREEMPT_RESCHED	= 0x20,
138
	TRACE_FLAG_NMI			= 0x40,
139 140
};

141
#define TRACE_BUF_SIZE		1024
142

143 144
struct trace_array;

145 146 147 148 149 150 151
/*
 * The CPU trace array - it consists of thousands of trace entries
 * plus some other descriptor data: (for example which task started
 * the trace, etc.)
 */
struct trace_array_cpu {
	atomic_t		disabled;
152
	void			*buffer_page;	/* ring buffer spare */
Ingo Molnar's avatar
Ingo Molnar committed
153

154
	unsigned long		entries;
155 156 157 158 159 160 161
	unsigned long		saved_latency;
	unsigned long		critical_start;
	unsigned long		critical_end;
	unsigned long		critical_sequence;
	unsigned long		nice;
	unsigned long		policy;
	unsigned long		rt_priority;
162
	unsigned long		skipped_entries;
163
	u64			preempt_timestamp;
164
	pid_t			pid;
165
	kuid_t			uid;
166
	char			comm[TASK_COMM_LEN];
167 168

	bool			ignore_pid;
169 170 171
#ifdef CONFIG_FUNCTION_TRACER
	bool			ftrace_ignore_pid;
#endif
172 173
};

174
struct tracer;
175
struct trace_option_dentry;
176

177 178 179 180
struct trace_buffer {
	struct trace_array		*tr;
	struct ring_buffer		*buffer;
	struct trace_array_cpu __percpu	*data;
181
	u64				time_start;
182 183 184
	int				cpu;
};

185 186
#define TRACE_FLAGS_MAX_SIZE		32

187 188 189 190 191
struct trace_options {
	struct tracer			*tracer;
	struct trace_option_dentry	*topts;
};

192
struct trace_pid_list {
193 194
	int				pid_max;
	unsigned long			*pids;
195 196
};

197 198 199 200 201 202
/*
 * The trace array - an array of per-CPU trace arrays. This is the
 * highest level data structure that individual tracers deal with.
 * They have on/off state as well:
 */
struct trace_array {
203
	struct list_head	list;
204
	char			*name;
205 206 207 208 209 210 211 212 213 214 215 216 217 218
	struct trace_buffer	trace_buffer;
#ifdef CONFIG_TRACER_MAX_TRACE
	/*
	 * The max_buffer is used to snapshot the trace when a maximum
	 * latency is reached, or when the user initiates a snapshot.
	 * Some tracers will use this to store a maximum trace while
	 * it continues examining live traces.
	 *
	 * The buffers for the max_buffer are set up the same as the trace_buffer
	 * When a snapshot is taken, the buffer of the max_buffer is swapped
	 * with the buffer of the trace_buffer and the buffers are reset for
	 * the trace_buffer so the tracing can continue.
	 */
	struct trace_buffer	max_buffer;
219
	bool			allocated_snapshot;
220 221
#endif
#if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)
222
	unsigned long		max_latency;
223
#endif
224
	struct trace_pid_list	__rcu *filtered_pids;
225 226 227 228 229 230 231 232 233 234 235 236 237 238
	/*
	 * max_lock is used to protect the swapping of buffers
	 * when taking a max snapshot. The buffers themselves are
	 * protected by per_cpu spinlocks. But the action of the swap
	 * needs its own lock.
	 *
	 * This is defined as a arch_spinlock_t in order to help
	 * with performance when lockdep debugging is enabled.
	 *
	 * It is also used in other places outside the update_max_tr
	 * so it needs to be defined outside of the
	 * CONFIG_TRACER_MAX_TRACE.
	 */
	arch_spinlock_t		max_lock;
239
	int			buffer_disabled;
240 241 242
#ifdef CONFIG_FTRACE_SYSCALLS
	int			sys_refcount_enter;
	int			sys_refcount_exit;
243 244
	struct trace_event_file __rcu *enter_syscall_files[NR_syscalls];
	struct trace_event_file __rcu *exit_syscall_files[NR_syscalls];
245
#endif
246 247
	int			stop_count;
	int			clock_id;
248
	int			nr_topts;
249
	bool			clear_trace;
250
	struct tracer		*current_trace;
251
	unsigned int		trace_flags;
252
	unsigned char		trace_flags_index[TRACE_FLAGS_MAX_SIZE];
253
	unsigned int		flags;
254
	raw_spinlock_t		start_lock;
255
	struct dentry		*dir;
256 257
	struct dentry		*options;
	struct dentry		*percpu_dir;
258
	struct dentry		*event_dir;
259
	struct trace_options	*topts;
260 261
	struct list_head	systems;
	struct list_head	events;
262
	struct trace_event_file *trace_marker_file;
263
	cpumask_var_t		tracing_cpumask; /* only trace on set CPUs */
264
	int			ref;
265 266
#ifdef CONFIG_FUNCTION_TRACER
	struct ftrace_ops	*ops;
267
	struct trace_pid_list	__rcu *function_pids;
268
#ifdef CONFIG_DYNAMIC_FTRACE
269
	/* All of these are protected by the ftrace_lock */
270
	struct list_head	func_probes;
271 272
	struct list_head	mod_trace;
	struct list_head	mod_notrace;
273
#endif
274 275 276
	/* function tracing enabled */
	int			function_enabled;
#endif
277
	int			time_stamp_abs_ref;
278
	struct list_head	hist_vars;
279 280
};

281 282 283 284 285 286
enum {
	TRACE_ARRAY_FL_GLOBAL	= (1 << 0)
};

extern struct list_head ftrace_trace_arrays;

287 288
extern struct mutex trace_types_lock;

289 290 291
extern int trace_array_get(struct trace_array *tr);
extern void trace_array_put(struct trace_array *tr);

292
extern int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs);
293
extern int tracing_set_clock(struct trace_array *tr, const char *clockstr);
294

295 296
extern bool trace_clock_in_ns(struct trace_array *tr);

297 298 299 300 301 302 303 304
/*
 * The global tracer (top) should be the first trace array added,
 * but we check the flag anyway.
 */
static inline struct trace_array *top_trace_array(void)
{
	struct trace_array *tr;

305
	if (list_empty(&ftrace_trace_arrays))
306 307
		return NULL;

308 309 310 311 312 313
	tr = list_entry(ftrace_trace_arrays.prev,
			typeof(*tr), list);
	WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
	return tr;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
#define FTRACE_CMP_TYPE(var, type) \
	__builtin_types_compatible_p(typeof(var), type *)

#undef IF_ASSIGN
#define IF_ASSIGN(var, entry, etype, id)		\
	if (FTRACE_CMP_TYPE(var, etype)) {		\
		var = (typeof(var))(entry);		\
		WARN_ON(id && (entry)->type != id);	\
		break;					\
	}

/* Will cause compile errors if type is not found. */
extern void __ftrace_bad_type(void);

/*
 * The trace_assign_type is a verifier that the entry type is
 * the same as the type being assigned. To add new types simply
 * add a line with the following format:
 *
 * IF_ASSIGN(var, ent, type, id);
 *
 *  Where "type" is the trace type that includes the trace_entry
 *  as the "ent" item. And "id" is the trace identifier that is
 *  used in the trace_type enum.
 *
 *  If the type can have more than one id, then use zero.
 */
#define trace_assign_type(var, ent)					\
	do {								\
		IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN);	\
		IF_ASSIGN(var, ent, struct ctx_switch_entry, 0);	\
		IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK);	\
346
		IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
347
		IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT);	\
348
		IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT);	\
349
		IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS);	\
350
		IF_ASSIGN(var, ent, struct hwlat_entry, TRACE_HWLAT);	\
351
		IF_ASSIGN(var, ent, struct raw_data_entry, TRACE_RAW_DATA);\
352 353 354 355
		IF_ASSIGN(var, ent, struct trace_mmiotrace_rw,		\
			  TRACE_MMIO_RW);				\
		IF_ASSIGN(var, ent, struct trace_mmiotrace_map,		\
			  TRACE_MMIO_MAP);				\
356
		IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
357 358 359 360
		IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry,	\
			  TRACE_GRAPH_ENT);		\
		IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry,	\
			  TRACE_GRAPH_RET);		\
361 362
		__ftrace_bad_type();					\
	} while (0)
363

364 365 366 367 368 369
/*
 * An option specific to a tracer. This is a boolean value.
 * The bit is the bit index that sets its value on the
 * flags value in struct tracer_flags.
 */
struct tracer_opt {
370 371
	const char	*name; /* Will appear on the trace_options file */
	u32		bit; /* Mask assigned in val field in tracer_flags */
372 373 374 375 376 377 378 379
};

/*
 * The set of specific options for a tracer. Your tracer
 * have to set the initial value of the flags val.
 */
struct tracer_flags {
	u32			val;
380
	struct tracer_opt	*opts;
381
	struct tracer		*trace;
382 383 384 385 386
};

/* Makes more easy to define a tracer opt */
#define TRACER_OPT(s, b)	.name = #s, .bit = b

387

388 389 390 391 392 393 394
struct trace_option_dentry {
	struct tracer_opt		*opt;
	struct tracer_flags		*flags;
	struct trace_array		*tr;
	struct dentry			*entry;
};

395
/**
396
 * struct tracer - a specific tracer and its callbacks to interact with tracefs
397 398 399
 * @name: the name chosen to select it on the available_tracers file
 * @init: called when one switches to this tracer (echo name > current_tracer)
 * @reset: called when one switches to another tracer
400 401
 * @start: called when tracing is unpaused (echo 1 > tracing_on)
 * @stop: called when tracing is paused (echo 0 > tracing_on)
402
 * @update_thresh: called when tracing_thresh is updated
403 404 405
 * @open: called when the trace file is opened
 * @pipe_open: called when the trace_pipe file is opened
 * @close: called when the trace file is released
406
 * @pipe_close: called when the trace_pipe file is released
407 408 409 410 411 412 413
 * @read: override the default read callback on trace_pipe
 * @splice_read: override the default splice_read callback on trace_pipe
 * @selftest: selftest to run on boot (see trace_selftest.c)
 * @print_headers: override the first lines that describe your columns
 * @print_line: callback that prints a trace
 * @set_flag: signals one of your private flags changed (trace_options file)
 * @flags: your private flags
414 415 416
 */
struct tracer {
	const char		*name;
417
	int			(*init)(struct trace_array *tr);
418
	void			(*reset)(struct trace_array *tr);
419 420
	void			(*start)(struct trace_array *tr);
	void			(*stop)(struct trace_array *tr);
421
	int			(*update_thresh)(struct trace_array *tr);
422
	void			(*open)(struct trace_iterator *iter);
423
	void			(*pipe_open)(struct trace_iterator *iter);
424
	void			(*close)(struct trace_iterator *iter);
425
	void			(*pipe_close)(struct trace_iterator *iter);
426 427 428
	ssize_t			(*read)(struct trace_iterator *iter,
					struct file *filp, char __user *ubuf,
					size_t cnt, loff_t *ppos);
429 430 431 432 433 434
	ssize_t			(*splice_read)(struct trace_iterator *iter,
					       struct file *filp,
					       loff_t *ppos,
					       struct pipe_inode_info *pipe,
					       size_t len,
					       unsigned int flags);
Steven Rostedt's avatar
Steven Rostedt committed
435 436 437 438
#ifdef CONFIG_FTRACE_STARTUP_TEST
	int			(*selftest)(struct tracer *trace,
					    struct trace_array *tr);
#endif
439
	void			(*print_header)(struct seq_file *m);
440
	enum print_line_t	(*print_line)(struct trace_iterator *iter);
441
	/* If you handled the flag setting, return 0 */
442 443
	int			(*set_flag)(struct trace_array *tr,
					    u32 old_flags, u32 bit, int set);
444
	/* Return 0 if OK with change, else return non-zero */
445
	int			(*flag_changed)(struct trace_array *tr,
446
						u32 mask, int set);
447
	struct tracer		*next;
448
	struct tracer_flags	*flags;
449
	int			enabled;
450
	int			ref;
451
	bool			print_max;
452
	bool			allow_instances;
453
#ifdef CONFIG_TRACER_MAX_TRACE
454
	bool			use_max_tr;
455
#endif
456 457
	/* True if tracer cannot be enabled in kernel param */
	bool			noboot;
458 459
};

460

461 462
/* Only current can touch trace_recursion */

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/*
 * For function tracing recursion:
 *  The order of these bits are important.
 *
 *  When function tracing occurs, the following steps are made:
 *   If arch does not support a ftrace feature:
 *    call internal function (uses INTERNAL bits) which calls...
 *   If callback is registered to the "global" list, the list
 *    function is called and recursion checks the GLOBAL bits.
 *    then this function calls...
 *   The function callback, which can use the FTRACE bits to
 *    check for recursion.
 *
 * Now if the arch does not suppport a feature, and it calls
 * the global list function which calls the ftrace callback
 * all three of these steps will do a recursion protection.
 * There's no reason to do one if the previous caller already
 * did. The recursion that we are protecting against will
 * go through the same steps again.
 *
 * To prevent the multiple recursion checks, if a recursion
 * bit is set that is higher than the MAX bit of the current
 * check, then we know that the check was made by the previous
 * caller, and we can skip the current check.
 */
488
enum {
489 490 491 492 493 494 495
	TRACE_BUFFER_BIT,
	TRACE_BUFFER_NMI_BIT,
	TRACE_BUFFER_IRQ_BIT,
	TRACE_BUFFER_SIRQ_BIT,

	/* Start of function recursion bits */
	TRACE_FTRACE_BIT,
496 497 498
	TRACE_FTRACE_NMI_BIT,
	TRACE_FTRACE_IRQ_BIT,
	TRACE_FTRACE_SIRQ_BIT,
499

500
	/* INTERNAL_BITs must be greater than FTRACE_BITs */
501 502 503 504 505
	TRACE_INTERNAL_BIT,
	TRACE_INTERNAL_NMI_BIT,
	TRACE_INTERNAL_IRQ_BIT,
	TRACE_INTERNAL_SIRQ_BIT,

506
	TRACE_BRANCH_BIT,
507 508 509 510 511 512 513
/*
 * Abuse of the trace_recursion.
 * As we need a way to maintain state if we are tracing the function
 * graph in irq because we want to trace a particular function that
 * was called in irq context but we have irq tracing off. Since this
 * can only be modified by current, we can reuse trace_recursion.
 */
514
	TRACE_IRQ_BIT,
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

	/* Set if the function is in the set_graph_function file */
	TRACE_GRAPH_BIT,

	/*
	 * In the very unlikely case that an interrupt came in
	 * at a start of graph tracing, and we want to trace
	 * the function in that interrupt, the depth can be greater
	 * than zero, because of the preempted start of a previous
	 * trace. In an even more unlikely case, depth could be 2
	 * if a softirq interrupted the start of graph tracing,
	 * followed by an interrupt preempting a start of graph
	 * tracing in the softirq, and depth can even be 3
	 * if an NMI came in at the start of an interrupt function
	 * that preempted a softirq start of a function that
	 * preempted normal context!!!! Luckily, it can't be
	 * greater than 3, so the next two bits are a mask
	 * of what the depth is when we set TRACE_GRAPH_BIT
	 */

	TRACE_GRAPH_DEPTH_START_BIT,
	TRACE_GRAPH_DEPTH_END_BIT,
537
};
538

539 540 541
#define trace_recursion_set(bit)	do { (current)->trace_recursion |= (1<<(bit)); } while (0)
#define trace_recursion_clear(bit)	do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
#define trace_recursion_test(bit)	((current)->trace_recursion & (1<<(bit)))
542

543 544 545 546 547 548 549 550 551 552
#define trace_recursion_depth() \
	(((current)->trace_recursion >> TRACE_GRAPH_DEPTH_START_BIT) & 3)
#define trace_recursion_set_depth(depth) \
	do {								\
		current->trace_recursion &=				\
			~(3 << TRACE_GRAPH_DEPTH_START_BIT);		\
		current->trace_recursion |=				\
			((depth) & 3) << TRACE_GRAPH_DEPTH_START_BIT;	\
	} while (0)

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
#define TRACE_CONTEXT_BITS	4

#define TRACE_FTRACE_START	TRACE_FTRACE_BIT
#define TRACE_FTRACE_MAX	((1 << (TRACE_FTRACE_START + TRACE_CONTEXT_BITS)) - 1)

#define TRACE_LIST_START	TRACE_INTERNAL_BIT
#define TRACE_LIST_MAX		((1 << (TRACE_LIST_START + TRACE_CONTEXT_BITS)) - 1)

#define TRACE_CONTEXT_MASK	TRACE_LIST_MAX

static __always_inline int trace_get_context_bit(void)
{
	int bit;

	if (in_interrupt()) {
		if (in_nmi())
			bit = 0;

		else if (in_irq())
			bit = 1;
		else
			bit = 2;
	} else
		bit = 3;

	return bit;
}

static __always_inline int trace_test_and_set_recursion(int start, int max)
{
	unsigned int val = current->trace_recursion;
	int bit;

	/* A previous recursion check was made */
	if ((val & TRACE_CONTEXT_MASK) > max)
		return 0;

	bit = trace_get_context_bit() + start;
	if (unlikely(val & (1 << bit)))
		return -1;

	val |= 1 << bit;
	current->trace_recursion = val;
	barrier();

	return bit;
}

static __always_inline void trace_clear_recursion(int bit)
{
	unsigned int val = current->trace_recursion;

	if (!bit)
		return;

	bit = 1 << bit;
	val &= ~bit;

	barrier();
	current->trace_recursion = val;
}

615 616 617
static inline struct ring_buffer_iter *
trace_buffer_iter(struct trace_iterator *iter, int cpu)
{
618
	return iter->buffer_iter ? iter->buffer_iter[cpu] : NULL;
619 620
}

621
int tracer_init(struct tracer *t, struct trace_array *tr);
622
int tracing_is_enabled(void);
623 624
void tracing_reset(struct trace_buffer *buf, int cpu);
void tracing_reset_online_cpus(struct trace_buffer *buf);
625
void tracing_reset_current(int cpu);
626
void tracing_reset_all_online_cpus(void);
627
int tracing_open_generic(struct inode *inode, struct file *filp);
628
bool tracing_is_disabled(void);
629
bool tracer_tracing_is_on(struct trace_array *tr);
630 631
void tracer_tracing_on(struct trace_array *tr);
void tracer_tracing_off(struct trace_array *tr);
632
struct dentry *trace_create_file(const char *name,
Al Viro's avatar
Al Viro committed
633
				 umode_t mode,
634 635 636 637
				 struct dentry *parent,
				 void *data,
				 const struct file_operations *fops);

638
struct dentry *tracing_init_dentry(void);
Ingo Molnar's avatar
Ingo Molnar committed
639

640 641
struct ring_buffer_event;

642 643 644 645 646 647
struct ring_buffer_event *
trace_buffer_lock_reserve(struct ring_buffer *buffer,
			  int type,
			  unsigned long len,
			  unsigned long flags,
			  int pc);
648

649 650
struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
						struct trace_array_cpu *data);
651 652 653 654

struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
					  int *ent_cpu, u64 *ent_ts);

655 656
void trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
					struct ring_buffer_event *event);
657

658 659 660 661 662 663 664 665
int trace_empty(struct trace_iterator *iter);

void *trace_find_next_entry_inc(struct trace_iterator *iter);

void trace_init_global_iter(struct trace_iterator *iter);

void tracing_iter_reset(struct trace_iterator *iter, int cpu);

666 667 668
void trace_function(struct trace_array *tr,
		    unsigned long ip,
		    unsigned long parent_ip,
669
		    unsigned long flags, int pc);
670 671 672 673
void trace_graph_function(struct trace_array *tr,
		    unsigned long ip,
		    unsigned long parent_ip,
		    unsigned long flags, int pc);
674
void trace_latency_header(struct seq_file *m);
675 676 677
void trace_default_header(struct seq_file *m);
void print_trace_header(struct seq_file *m, struct trace_iterator *iter);
int trace_empty(struct trace_iterator *iter);
678

679
void trace_graph_return(struct ftrace_graph_ret *trace);
680
int trace_graph_entry(struct ftrace_graph_ent *trace);
681
void set_graph_array(struct trace_array *tr);
682

683 684
void tracing_start_cmdline_record(void);
void tracing_stop_cmdline_record(void);
685 686 687
void tracing_start_tgid_record(void);
void tracing_stop_tgid_record(void);

688
int register_tracer(struct tracer *type);
689
int is_tracing_stopped(void);
690

691 692
loff_t tracing_lseek(struct file *file, loff_t offset, int whence);

693 694 695 696
extern cpumask_var_t __read_mostly tracing_buffer_mask;

#define for_each_tracing_cpu(cpu)	\
	for_each_cpu(cpu, tracing_buffer_mask)
697 698 699

extern unsigned long nsecs_to_usecs(unsigned long nsecs);

700 701
extern unsigned long tracing_thresh;

702
/* PID filtering */
703 704 705

extern int pid_max;

706 707 708 709 710 711 712
bool trace_find_filtered_pid(struct trace_pid_list *filtered_pids,
			     pid_t search_pid);
bool trace_ignore_this_task(struct trace_pid_list *filtered_pids,
			    struct task_struct *task);
void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
				  struct task_struct *self,
				  struct task_struct *task);
713 714 715
void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos);
void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos);
int trace_pid_show(struct seq_file *m, void *v);
716 717 718 719
void trace_free_pid_list(struct trace_pid_list *pid_list);
int trace_pid_write(struct trace_pid_list *filtered_pids,
		    struct trace_pid_list **new_pid_list,
		    const char __user *ubuf, size_t cnt);
720

721
#ifdef CONFIG_TRACER_MAX_TRACE
722 723 724
void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
void update_max_tr_single(struct trace_array *tr,
			  struct task_struct *tsk, int cpu);
725
#endif /* CONFIG_TRACER_MAX_TRACE */
726

727
#ifdef CONFIG_STACKTRACE
728
void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
729 730 731 732 733
			    int pc);

void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
		   int pc);
#else
734
static inline void ftrace_trace_userstack(struct ring_buffer *buffer,
735 736 737 738 739 740 741 742 743
					  unsigned long flags, int pc)
{
}

static inline void __trace_stack(struct trace_array *tr, unsigned long flags,
				 int skip, int pc)
{
}
#endif /* CONFIG_STACKTRACE */
744

745
extern u64 ftrace_now(int cpu);
746

747
extern void trace_find_cmdline(int pid, char comm[]);
748
extern int trace_find_tgid(int pid);
749
extern void trace_event_follow_fork(struct trace_array *tr, bool enable);
750

751 752
#ifdef CONFIG_DYNAMIC_FTRACE
extern unsigned long ftrace_update_tot_cnt;
753 754 755
void ftrace_init_trace_array(struct trace_array *tr);
#else
static inline void ftrace_init_trace_array(struct trace_array *tr) { }
756
#endif
757 758
#define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
extern int DYN_FTRACE_TEST_NAME(void);
759 760
#define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2
extern int DYN_FTRACE_TEST_NAME2(void);
761

762
extern bool ring_buffer_expanded;
763 764
extern bool tracing_selftest_disabled;

Steven Rostedt's avatar
Steven Rostedt committed
765 766 767
#ifdef CONFIG_FTRACE_STARTUP_TEST
extern int trace_selftest_startup_function(struct tracer *trace,
					   struct trace_array *tr);
768 769
extern int trace_selftest_startup_function_graph(struct tracer *trace,
						 struct trace_array *tr);
Steven Rostedt's avatar
Steven Rostedt committed
770 771 772 773 774 775 776 777
extern int trace_selftest_startup_irqsoff(struct tracer *trace,
					  struct trace_array *tr);
extern int trace_selftest_startup_preemptoff(struct tracer *trace,
					     struct trace_array *tr);
extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
						 struct trace_array *tr);
extern int trace_selftest_startup_wakeup(struct tracer *trace,
					 struct trace_array *tr);
Steven Noonan's avatar
Steven Noonan committed
778 779
extern int trace_selftest_startup_nop(struct tracer *trace,
					 struct trace_array *tr);
780 781
extern int trace_selftest_startup_branch(struct tracer *trace,
					 struct trace_array *tr);
782 783 784 785 786 787 788 789 790
/*
 * Tracer data references selftest functions that only occur
 * on boot up. These can be __init functions. Thus, when selftests
 * are enabled, then the tracers need to reference __init functions.
 */
#define __tracer_data		__refdata
#else
/* Tracers are seldom changed. Optimize when selftests are disabled. */
#define __tracer_data		__read_mostly
Steven Rostedt's avatar
Steven Rostedt committed
791 792
#endif /* CONFIG_FTRACE_STARTUP_TEST */

Ingo Molnar's avatar
Ingo Molnar committed
793
extern void *head_page(struct trace_array_cpu *data);
794
extern unsigned long long ns2usecs(u64 nsec);
795
extern int
796
trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
797
extern int
798
trace_vprintk(unsigned long ip, const char *fmt, va_list args);
799 800 801 802 803
extern int
trace_array_vprintk(struct trace_array *tr,
		    unsigned long ip, const char *fmt, va_list args);
int trace_array_printk(struct trace_array *tr,
		       unsigned long ip, const char *fmt, ...);
804 805
int trace_array_printk_buf(struct ring_buffer *buffer,
			   unsigned long ip, const char *fmt, ...);
806 807
void trace_printk_seq(struct trace_seq *s);
enum print_line_t print_trace_line(struct trace_iterator *iter);
Ingo Molnar's avatar
Ingo Molnar committed
808

809 810
extern char trace_find_mark(unsigned long long duration);

811 812 813 814 815 816 817 818 819
struct ftrace_hash;

struct ftrace_mod_load {
	struct list_head	list;
	char			*func;
	char			*module;
	int			 enable;
};

820 821 822 823
enum {
	FTRACE_HASH_FL_MOD	= (1 << 0),
};

824 825 826 827
struct ftrace_hash {
	unsigned long		size_bits;
	struct hlist_head	*buckets;
	unsigned long		count;
828
	unsigned long		flags;
829 830 831 832 833 834
	struct rcu_head		rcu;
};

struct ftrace_func_entry *
ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip);

835
static __always_inline bool ftrace_hash_empty(struct ftrace_hash *hash)
836
{
837
	return !hash || !(hash->count || (hash->flags & FTRACE_HASH_FL_MOD));
838 839
}

840
/* Standard output formatting function used for function return traces */
841
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
842 843 844 845 846 847 848 849

/* Flag options */
#define TRACE_GRAPH_PRINT_OVERRUN       0x1
#define TRACE_GRAPH_PRINT_CPU           0x2
#define TRACE_GRAPH_PRINT_OVERHEAD      0x4
#define TRACE_GRAPH_PRINT_PROC          0x8
#define TRACE_GRAPH_PRINT_DURATION      0x10
#define TRACE_GRAPH_PRINT_ABS_TIME      0x20
850
#define TRACE_GRAPH_PRINT_IRQS          0x40
851
#define TRACE_GRAPH_PRINT_TAIL          0x80
852 853
#define TRACE_GRAPH_SLEEP_TIME		0x100
#define TRACE_GRAPH_GRAPH_TIME		0x200
854 855
#define TRACE_GRAPH_PRINT_FILL_SHIFT	28
#define TRACE_GRAPH_PRINT_FILL_MASK	(0x3 << TRACE_GRAPH_PRINT_FILL_SHIFT)
856

857 858 859
extern void ftrace_graph_sleep_time_control(bool enable);
extern void ftrace_graph_graph_time_control(bool enable);

860 861 862
extern enum print_line_t
print_graph_function_flags(struct trace_iterator *iter, u32 flags);
extern void print_graph_headers_flags(struct seq_file *s, u32 flags);
863
extern void
864
trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
865 866 867 868 869 870 871 872 873
extern void graph_trace_open(struct trace_iterator *iter);
extern void graph_trace_close(struct trace_iterator *iter);
extern int __trace_graph_entry(struct trace_array *tr,
			       struct ftrace_graph_ent *trace,
			       unsigned long flags, int pc);
extern void __trace_graph_return(struct trace_array *tr,
				 struct ftrace_graph_ret *trace,
				 unsigned long flags, int pc);

874
#ifdef CONFIG_DYNAMIC_FTRACE
875 876
extern struct ftrace_hash *ftrace_graph_hash;
extern struct ftrace_hash *ftrace_graph_notrace_hash;
877

878
static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)
879
{
880
	unsigned long addr = trace->func;
881 882 883 884 885 886 887
	int ret = 0;

	preempt_disable_notrace();

	if (ftrace_hash_empty(ftrace_graph_hash)) {
		ret = 1;
		goto out;
888 889
	}

890
	if (ftrace_lookup_ip(ftrace_graph_hash, addr)) {
891 892 893 894 895 896 897 898

		/*
		 * This needs to be cleared on the return functions
		 * when the depth is zero.
		 */
		trace_recursion_set(TRACE_GRAPH_BIT);
		trace_recursion_set_depth(trace->depth);

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
		/*
		 * If no irqs are to be traced, but a set_graph_function
		 * is set, and called by an interrupt handler, we still
		 * want to trace it.
		 */
		if (in_irq())
			trace_recursion_set(TRACE_IRQ_BIT);
		else
			trace_recursion_clear(TRACE_IRQ_BIT);
		ret = 1;
	}

out:
	preempt_enable_notrace();
	return ret;
914
}
915

916 917 918 919 920 921 922
static inline void ftrace_graph_addr_finish(struct ftrace_graph_ret *trace)
{
	if (trace_recursion_test(TRACE_GRAPH_BIT) &&
	    trace->depth == trace_recursion_depth())
		trace_recursion_clear(TRACE_GRAPH_BIT);
}

923 924
static inline int ftrace_graph_notrace_addr(unsigned long addr)
{
925
	int ret = 0;
926

927
	preempt_disable_notrace();
928

929 930
	if (ftrace_lookup_ip(ftrace_graph_notrace_hash, addr))
		ret = 1;
931

932 933
	preempt_enable_notrace();
	return ret;
934
}
935
#else
936
static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)
937 938
{
	return 1;
939
}
940 941 942 943 944

static inline int ftrace_graph_notrace_addr(unsigned long addr)
{
	return 0;
}
945 946
static inline void ftrace_graph_addr_finish(struct ftrace_graph_ret *trace)
{ }
947
#endif /* CONFIG_DYNAMIC_FTRACE */
948 949 950 951 952 953

extern unsigned int fgraph_max_depth;

static inline bool ftrace_graph_ignore_func(struct ftrace_graph_ent *trace)
{
	/* trace it when it is-nested-in or is a function enabled. */
954 955
	return !(trace_recursion_test(TRACE_GRAPH_BIT) ||
		 ftrace_graph_addr(trace)) ||
956 957 958 959
		(trace->depth < 0) ||
		(fgraph_max_depth && trace->depth >= fgraph_max_depth);
}

960
#else /* CONFIG_FUNCTION_GRAPH_TRACER */
961
static inline enum print_line_t
962
print_graph_function_flags(struct trace_iterator *iter, u32 flags)
963 964 965
{
	return TRACE_TYPE_UNHANDLED;
}
966
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
967

968
extern struct list_head ftrace_pids;
969

970
#ifdef CONFIG_FUNCTION_TRACER
971 972 973
struct ftrace_func_command {
	struct list_head	list;
	char			*name;
974 975
	int			(*func)(struct trace_array *tr,
					struct ftrace_hash *hash,
976 977 978
					char *func, char *cmd,
					char *params, int enable);
};
979
extern bool ftrace_filter_param __initdata;
980
static inline int ftrace_trace_task(struct trace_array *tr)
981
{
982
	return !this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid);
983
}
984
extern int ftrace_is_dead(void);
985 986 987
int ftrace_create_function_files(struct trace_array *tr,
				 struct dentry *parent);
void ftrace_destroy_function_files(struct trace_array *tr);
988 989 990
void ftrace_init_global_array_ops(struct trace_array *tr);
void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func);
void ftrace_reset_array_ops(struct trace_array *tr);
991
void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer);
992 993
void ftrace_init_tracefs_toplevel(struct trace_array *tr,
				  struct dentry *d_tracer);
994
void ftrace_clear_pids(struct trace_array *tr);
995
int init_function_trace(void);
996
void ftrace_pid_follow_fork(struct trace_array *tr, bool enable);
997
#else
998
static inline int ftrace_trace_task(struct trace_array *tr)
999 1000 1001
{
	return 1;
}
1002
static inline int ftrace_is_dead(void) { return 0; }
1003 1004 1005