trace.h 58.7 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
	cpumask_var_t		tracing_cpumask; /* only trace on set CPUs */
263
	int			ref;
264 265
#ifdef CONFIG_FUNCTION_TRACER
	struct ftrace_ops	*ops;
266
	struct trace_pid_list	__rcu *function_pids;
267
#ifdef CONFIG_DYNAMIC_FTRACE
268
	/* All of these are protected by the ftrace_lock */
269
	struct list_head	func_probes;
270 271
	struct list_head	mod_trace;
	struct list_head	mod_notrace;
272
#endif
273 274 275
	/* function tracing enabled */
	int			function_enabled;
#endif
276 277
};

278 279 280 281 282 283
enum {
	TRACE_ARRAY_FL_GLOBAL	= (1 << 0)
};

extern struct list_head ftrace_trace_arrays;

284 285
extern struct mutex trace_types_lock;

286 287 288
extern int trace_array_get(struct trace_array *tr);
extern void trace_array_put(struct trace_array *tr);

289 290 291 292 293 294 295 296
/*
 * 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;

297
	if (list_empty(&ftrace_trace_arrays))
298 299
		return NULL;

300 301 302 303 304 305
	tr = list_entry(ftrace_trace_arrays.prev,
			typeof(*tr), list);
	WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
	return tr;
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
#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);	\
338
		IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
339
		IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT);	\
340
		IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT);	\
341
		IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS);	\
342
		IF_ASSIGN(var, ent, struct hwlat_entry, TRACE_HWLAT);	\
343
		IF_ASSIGN(var, ent, struct raw_data_entry, TRACE_RAW_DATA);\
344 345 346 347
		IF_ASSIGN(var, ent, struct trace_mmiotrace_rw,		\
			  TRACE_MMIO_RW);				\
		IF_ASSIGN(var, ent, struct trace_mmiotrace_map,		\
			  TRACE_MMIO_MAP);				\
348
		IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
349 350 351 352
		IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry,	\
			  TRACE_GRAPH_ENT);		\
		IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry,	\
			  TRACE_GRAPH_RET);		\
353 354
		__ftrace_bad_type();					\
	} while (0)
355

356 357 358 359 360 361
/*
 * 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 {
362 363
	const char	*name; /* Will appear on the trace_options file */
	u32		bit; /* Mask assigned in val field in tracer_flags */
364 365 366 367 368 369 370 371
};

/*
 * 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;
372
	struct tracer_opt	*opts;
373
	struct tracer		*trace;
374 375 376 377 378
};

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

379

380 381 382 383 384 385 386
struct trace_option_dentry {
	struct tracer_opt		*opt;
	struct tracer_flags		*flags;
	struct trace_array		*tr;
	struct dentry			*entry;
};

387
/**
388
 * struct tracer - a specific tracer and its callbacks to interact with tracefs
389 390 391
 * @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
392 393
 * @start: called when tracing is unpaused (echo 1 > tracing_on)
 * @stop: called when tracing is paused (echo 0 > tracing_on)
394
 * @update_thresh: called when tracing_thresh is updated
395 396 397
 * @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
398
 * @pipe_close: called when the trace_pipe file is released
399 400 401 402 403 404 405
 * @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
406 407 408
 */
struct tracer {
	const char		*name;
409
	int			(*init)(struct trace_array *tr);
410
	void			(*reset)(struct trace_array *tr);
411 412
	void			(*start)(struct trace_array *tr);
	void			(*stop)(struct trace_array *tr);
413
	int			(*update_thresh)(struct trace_array *tr);
414
	void			(*open)(struct trace_iterator *iter);
415
	void			(*pipe_open)(struct trace_iterator *iter);
416
	void			(*close)(struct trace_iterator *iter);
417
	void			(*pipe_close)(struct trace_iterator *iter);
418 419 420
	ssize_t			(*read)(struct trace_iterator *iter,
					struct file *filp, char __user *ubuf,
					size_t cnt, loff_t *ppos);
421 422 423 424 425 426
	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
427 428 429 430
#ifdef CONFIG_FTRACE_STARTUP_TEST
	int			(*selftest)(struct tracer *trace,
					    struct trace_array *tr);
#endif
431
	void			(*print_header)(struct seq_file *m);
432
	enum print_line_t	(*print_line)(struct trace_iterator *iter);
433
	/* If you handled the flag setting, return 0 */
434 435
	int			(*set_flag)(struct trace_array *tr,
					    u32 old_flags, u32 bit, int set);
436
	/* Return 0 if OK with change, else return non-zero */
437
	int			(*flag_changed)(struct trace_array *tr,
438
						u32 mask, int set);
439
	struct tracer		*next;
440
	struct tracer_flags	*flags;
441
	int			enabled;
442
	int			ref;
443
	bool			print_max;
444
	bool			allow_instances;
445
#ifdef CONFIG_TRACER_MAX_TRACE
446
	bool			use_max_tr;
447
#endif
448 449
	/* True if tracer cannot be enabled in kernel param */
	bool			noboot;
450 451
};

452

453 454
/* Only current can touch trace_recursion */

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/*
 * 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.
 */
480
enum {
481 482 483 484 485 486 487
	TRACE_BUFFER_BIT,
	TRACE_BUFFER_NMI_BIT,
	TRACE_BUFFER_IRQ_BIT,
	TRACE_BUFFER_SIRQ_BIT,

	/* Start of function recursion bits */
	TRACE_FTRACE_BIT,
488 489 490
	TRACE_FTRACE_NMI_BIT,
	TRACE_FTRACE_IRQ_BIT,
	TRACE_FTRACE_SIRQ_BIT,
491

492
	/* INTERNAL_BITs must be greater than FTRACE_BITs */
493 494 495 496 497
	TRACE_INTERNAL_BIT,
	TRACE_INTERNAL_NMI_BIT,
	TRACE_INTERNAL_IRQ_BIT,
	TRACE_INTERNAL_SIRQ_BIT,

498
	TRACE_BRANCH_BIT,
499 500 501 502 503 504 505
/*
 * 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.
 */
506
	TRACE_IRQ_BIT,
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

	/* 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,
529
};
530

531 532 533
#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)))
534

535 536 537 538 539 540 541 542 543 544
#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)

545 546 547 548 549 550 551 552 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
#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;
}

607 608 609 610 611 612 613 614
static inline struct ring_buffer_iter *
trace_buffer_iter(struct trace_iterator *iter, int cpu)
{
	if (iter->buffer_iter && iter->buffer_iter[cpu])
		return iter->buffer_iter[cpu];
	return NULL;
}

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

632
struct dentry *tracing_init_dentry(void);
Ingo Molnar's avatar
Ingo Molnar committed
633

634 635
struct ring_buffer_event;

636 637 638 639 640 641
struct ring_buffer_event *
trace_buffer_lock_reserve(struct ring_buffer *buffer,
			  int type,
			  unsigned long len,
			  unsigned long flags,
			  int pc);
642

643 644
struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
						struct trace_array_cpu *data);
645 646 647 648

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

649 650
void trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
					struct ring_buffer_event *event);
651

652 653 654 655 656 657 658 659
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);

660 661 662
void trace_function(struct trace_array *tr,
		    unsigned long ip,
		    unsigned long parent_ip,
663
		    unsigned long flags, int pc);
664 665 666 667
void trace_graph_function(struct trace_array *tr,
		    unsigned long ip,
		    unsigned long parent_ip,
		    unsigned long flags, int pc);
668
void trace_latency_header(struct seq_file *m);
669 670 671
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);
672

673
void trace_graph_return(struct ftrace_graph_ret *trace);
674
int trace_graph_entry(struct ftrace_graph_ent *trace);
675
void set_graph_array(struct trace_array *tr);
676

677 678
void tracing_start_cmdline_record(void);
void tracing_stop_cmdline_record(void);
679 680 681
void tracing_start_tgid_record(void);
void tracing_stop_tgid_record(void);

682
int register_tracer(struct tracer *type);
683
int is_tracing_stopped(void);
684

685 686
loff_t tracing_lseek(struct file *file, loff_t offset, int whence);

687 688 689 690
extern cpumask_var_t __read_mostly tracing_buffer_mask;

#define for_each_tracing_cpu(cpu)	\
	for_each_cpu(cpu, tracing_buffer_mask)
691 692 693

extern unsigned long nsecs_to_usecs(unsigned long nsecs);

694 695
extern unsigned long tracing_thresh;

696
/* PID filtering */
697 698 699

extern int pid_max;

700 701 702 703 704 705 706
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);
707 708 709
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);
710 711 712 713
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);
714

715
#ifdef CONFIG_TRACER_MAX_TRACE
716 717 718
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);
719
#endif /* CONFIG_TRACER_MAX_TRACE */
720

721
#ifdef CONFIG_STACKTRACE
722
void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
723 724 725 726 727
			    int pc);

void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
		   int pc);
#else
728
static inline void ftrace_trace_userstack(struct ring_buffer *buffer,
729 730 731 732 733 734 735 736 737
					  unsigned long flags, int pc)
{
}

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

739
extern u64 ftrace_now(int cpu);
740

741
extern void trace_find_cmdline(int pid, char comm[]);
742
extern int trace_find_tgid(int pid);
743
extern void trace_event_follow_fork(struct trace_array *tr, bool enable);
744

745 746
#ifdef CONFIG_DYNAMIC_FTRACE
extern unsigned long ftrace_update_tot_cnt;
747 748 749
void ftrace_init_trace_array(struct trace_array *tr);
#else
static inline void ftrace_init_trace_array(struct trace_array *tr) { }
750
#endif
751 752
#define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
extern int DYN_FTRACE_TEST_NAME(void);
753 754
#define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2
extern int DYN_FTRACE_TEST_NAME2(void);
755

756
extern bool ring_buffer_expanded;
757 758
extern bool tracing_selftest_disabled;

Steven Rostedt's avatar
Steven Rostedt committed
759 760 761
#ifdef CONFIG_FTRACE_STARTUP_TEST
extern int trace_selftest_startup_function(struct tracer *trace,
					   struct trace_array *tr);
762 763
extern int trace_selftest_startup_function_graph(struct tracer *trace,
						 struct trace_array *tr);
Steven Rostedt's avatar
Steven Rostedt committed
764 765 766 767 768 769 770 771
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
772 773
extern int trace_selftest_startup_nop(struct tracer *trace,
					 struct trace_array *tr);
Steven Rostedt's avatar
Steven Rostedt committed
774 775
extern int trace_selftest_startup_sched_switch(struct tracer *trace,
					       struct trace_array *tr);
776 777
extern int trace_selftest_startup_branch(struct tracer *trace,
					 struct trace_array *tr);
778 779 780 781 782 783 784 785 786
/*
 * 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
787 788
#endif /* CONFIG_FTRACE_STARTUP_TEST */

Ingo Molnar's avatar
Ingo Molnar committed
789
extern void *head_page(struct trace_array_cpu *data);
790
extern unsigned long long ns2usecs(u64 nsec);
791
extern int
792
trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
793
extern int
794
trace_vprintk(unsigned long ip, const char *fmt, va_list args);
795 796 797 798 799
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, ...);
800 801
int trace_array_printk_buf(struct ring_buffer *buffer,
			   unsigned long ip, const char *fmt, ...);
802 803
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
804

805 806
extern char trace_find_mark(unsigned long long duration);

807 808 809 810 811 812 813 814 815
struct ftrace_hash;

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

816 817 818 819
enum {
	FTRACE_HASH_FL_MOD	= (1 << 0),
};

820 821 822 823
struct ftrace_hash {
	unsigned long		size_bits;
	struct hlist_head	*buckets;
	unsigned long		count;
824
	unsigned long		flags;
825 826 827 828 829 830
	struct rcu_head		rcu;
};

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

831
static __always_inline bool ftrace_hash_empty(struct ftrace_hash *hash)
832
{
833
	return !hash || !(hash->count || (hash->flags & FTRACE_HASH_FL_MOD));
834 835
}

836
/* Standard output formatting function used for function return traces */
837
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
838 839 840 841 842 843 844 845

/* 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
846
#define TRACE_GRAPH_PRINT_IRQS          0x40
847
#define TRACE_GRAPH_PRINT_TAIL          0x80
848 849
#define TRACE_GRAPH_SLEEP_TIME		0x100
#define TRACE_GRAPH_GRAPH_TIME		0x200
850 851
#define TRACE_GRAPH_PRINT_FILL_SHIFT	28
#define TRACE_GRAPH_PRINT_FILL_MASK	(0x3 << TRACE_GRAPH_PRINT_FILL_SHIFT)
852

853 854 855
extern void ftrace_graph_sleep_time_control(bool enable);
extern void ftrace_graph_graph_time_control(bool enable);

856 857 858
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);
859
extern void
860
trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
861 862 863 864 865 866 867 868 869
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);

870
#ifdef CONFIG_DYNAMIC_FTRACE
871 872
extern struct ftrace_hash *ftrace_graph_hash;
extern struct ftrace_hash *ftrace_graph_notrace_hash;
873

874
static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)
875
{
876
	unsigned long addr = trace->func;
877 878 879 880 881 882 883
	int ret = 0;

	preempt_disable_notrace();

	if (ftrace_hash_empty(ftrace_graph_hash)) {
		ret = 1;
		goto out;
884 885
	}

886
	if (ftrace_lookup_ip(ftrace_graph_hash, addr)) {
887 888 889 890 891 892 893 894

		/*
		 * 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);

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
		/*
		 * 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;
910
}
911

912 913 914 915 916 917 918
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);
}

919 920
static inline int ftrace_graph_notrace_addr(unsigned long addr)
{
921
	int ret = 0;
922

923
	preempt_disable_notrace();
924

925 926
	if (ftrace_lookup_ip(ftrace_graph_notrace_hash, addr))
		ret = 1;
927

928 929
	preempt_enable_notrace();
	return ret;
930
}
931
#else
932
static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)
933 934
{
	return 1;
935
}
936 937 938 939 940

static inline int ftrace_graph_notrace_addr(unsigned long addr)
{
	return 0;
}
941 942
static inline void ftrace_graph_addr_finish(struct ftrace_graph_ret *trace)
{ }
943
#endif /* CONFIG_DYNAMIC_FTRACE */
944 945 946 947 948 949

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. */
950 951
	return !(trace_recursion_test(TRACE_GRAPH_BIT) ||
		 ftrace_graph_addr(trace)) ||
952 953 954 955
		(trace->depth < 0) ||
		(fgraph_max_depth && trace->depth >= fgraph_max_depth);
}

956
#else /* CONFIG_FUNCTION_GRAPH_TRACER */
957
static inline enum print_line_t
958
print_graph_function_flags(struct trace_iterator *iter, u32 flags)
959 960 961
{
	return TRACE_TYPE_UNHANDLED;
}
962
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
963

964
extern struct list_head ftrace_pids;
965

966
#ifdef CONFIG_FUNCTION_TRACER
967 968 969
struct ftrace_func_command {
	struct list_head	list;
	char			*name;
970 971
	int			(*func)(struct trace_array *tr,
					struct ftrace_hash *hash,
972 973 974
					char *func, char *cmd,
					char *params, int enable);
};
975
extern bool ftrace_filter_param __initdata;
976
static inline int ftrace_trace_task(struct trace_array *tr)
977
{
978
	return !this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid);
979
}
980
extern int ftrace_is_dead(void);
981 982 983
int ftrace_create_function_files(struct trace_array *tr,
				 struct dentry *parent);
void ftrace_destroy_function_files(struct trace_array *tr);
984 985 986
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);
987
int using_ftrace_ops_list_func(void);
988
void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer);
989 990
void ftrace_init_tracefs_toplevel(struct trace_array *tr,
				  struct dentry *d_tracer);
991
void ftrace_clear_pids(struct trace_array *tr);
992
int init_function_trace(void);
993
void ftrace_pid_follow_fork(struct trace_array *tr, bool enable);
994
#else
995
static inline int ftrace_trace_task(struct trace_array *tr)
996 997 998
{
	return 1;
}
999
static inline int ftrace_is_dead(void) { return 0; }
1000 1001 1002 1003 1004 1005 1006
static inline int
ftrace_create_function_files(struct trace_array *tr,
			     struct dentry *parent)
{
	return 0;
}
static inline void ftrace_destroy_function_files(struct trace_array *tr) { }
1007 1008 1009
static inline __init void
ftrace_init_global_array_ops(struct trace_array *tr) { }
static inline void ftrace_reset_array_ops(struct trace_array *tr) { }
1010
static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { }
1011
static inline void ftrace_init_tracefs_toplevel(struct trace_array *tr, struct dentry *d) { }
1012
static inline void ftrace_clear_pids(struct trace_array *tr) { }
1013
static inline int init_function_trace(void) { return 0; }
1014
static inline void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) { }
1015 1016
/* ftace_func_t type is not defined, use macro instead of static inline */
#define ftrace_init_array_ops(tr, func) do { } while (0)
1017 1018 1019
#endif /* CONFIG_FUNCTION_TRACER */

#if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
1020 1021 1022 1023

struct ftrace_probe_ops {
	void			(*func)(unsigned long ip,
					unsigned long parent_ip,
1024
					struct trace_array *tr,
1025
					struct ftrace_probe_ops *ops,
1026
					void *data);
1027
	int			(*init)(struct ftrace_probe_ops *ops,
1028
					struct trace_array *tr,
1029 1030
					unsigned long ip, void *init_data,
					void **data);
1031
	void			(*free)(struct ftrace_probe_ops *ops,
1032
					struct trace_array *tr,
1033
					unsigned long ip, void *data);
1034 1035 1036 1037 1038 1039
	int			(*print)(struct seq_file *m,
					 unsigned long ip,
					 struct ftrace_probe_ops *ops,
					 void *data);
};

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
struct ftrace_func_mapper;
typedef int (*ftrace_mapper_func)(void *data);

struct ftrace_func_mapper *allocate_ftrace_func_mapper(void);
void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
					   unsigned long ip);
int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
			       unsigned long ip, void *data);
void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
				   unsigned long ip);
void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
			     ftrace_mapper_func free_func);

1053
extern int
1054 1055
register_ftrace_function_probe(char *glob, struct trace_array *tr,
			       struct ftrace_probe_ops *ops, void *data);
1056
extern int
1057 1058
unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
				      struct ftrace_probe_ops *ops);
1059
extern void clear_ftrace_function_probes(struct trace_array *tr);
1060

1061 1062 1063
int register_ftrace_command(struct ftrace_func_command *cmd);
int unregister_ftrace_command(struct ftrace_func_command *cmd);

1064 1065 1066 1067
void ftrace_create_filter_files(struct ftrace_ops *ops,
				struct dentry *parent);
void ftrace_destroy_filter_files(struct ftrace_ops *ops);
#else
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
struct ftrace_func_command;

static inline __init int register_ftrace_command(struct ftrace_func_command *cmd)
{
	return -EINVAL;
}
static inline __init int unregister_ftrace_command(char *cmd_name)
{
	return -EINVAL;
}
1078 1079 1080 1081
static inline void clear_ftrace_function_probes(struct trace_array *tr)
{
}

1082 1083 1084 1085 1086 1087 1088
/*
 * The ops parameter passed in is usually undefined.
 * This must be a macro.
 */
#define ftrace_create_filter_files(ops, parent) do { } while (0)
#define ftrace_destroy_filter_files(ops) do { } while (0)
#endif /* CONFIG_FUNCTION_TRACER && CONFIG_DYNAMIC_FTRACE */
1089

1090
bool ftrace_event_is_function(struct trace_event_call *call);
1091

1092 1093 1094 1095
/*
 * struct trace_parser - servers for reading the user input separated by spaces
 * @cont: set if the input is not complete - no final space char was found
 * @buffer: holds the parsed user input
1096
 * @idx: user input length
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
 * @size: buffer size
 */
struct trace_parser {
	bool		cont;
	char		*buffer;
	unsigned	idx;
	unsigned	size;
};

static inline bool trace_parser_loaded(struct trace_parser *parser)
{
	return (parser->idx != 0);
}

static inline bool trace_parser_cont(struct trace_parser *parser)
{
	return parser->cont;
}

static inline void trace_parser_clear(struct trace_parser *parser)
{
	parser->cont = false;
	parser->idx = 0;
}

extern int trace_parser_get_init(struct trace_parser *parser, int size);
extern void trace_parser_put(struct trace_parser *parser);
extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
	size_t cnt, loff_t *ppos);

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
/*
 * Only create function graph options if function graph is configured.
 */
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
# define FGRAPH_FLAGS						\
		C(DISPLAY_GRAPH,	"display-graph"),
#else
# define FGRAPH_FLAGS
#endif

1137 1138 1139 1140 1141 1142 1143
#ifdef CONFIG_BRANCH_TRACER
# define BRANCH_FLAGS					\
		C(BRANCH,		"branch"),
#else
# define BRANCH_FLAGS
#endif

1144 1145
#ifdef CONFIG_FUNCTION_TRACER
# define FUNCTION_FLAGS						\
1146 1147
		C(FUNCTION,		"function-trace"),	\
		C(FUNC_FORK,		"function-fork"),
1148 1149 1150 1151
# define FUNCTION_DEFAULT_FLAGS		TRACE_ITER_FUNCTION
#else
# define FUNCTION_FLAGS
# define FUNCTION_DEFAULT_FLAGS		0UL
1152
# define TRACE_ITER_FUNC_FORK		0UL
1153 1154
#endif

1155 1156 1157 1158 1159 1160 1161
#ifdef CONFIG_STACKTRACE
# define STACK_FLAGS				\
		C(STACKTRACE,		"stacktrace"),
#else
# define STACK_FLAGS
#endif

Steven Rostedt's avatar
Steven Rostedt committed
1162 1163 1164 1165 1166
/*
 * trace_iterator_flags is an enumeration that defines bit
 * positions into trace_flags that controls the output.
 *
 * NOTE: These bits must match the trace_options array in
1167
 *       trace.c (this macro guarantees it).
Steven Rostedt's avatar
Steven Rostedt committed
1168
 */