ftrace_event.h 14.4 KB
Newer Older
1 2 3 4
#ifndef _LINUX_FTRACE_EVENT_H
#define _LINUX_FTRACE_EVENT_H

#include <linux/ring_buffer.h>
5
#include <linux/trace_seq.h>
6
#include <linux/percpu.h>
7
#include <linux/hardirq.h>
8
#include <linux/perf_event.h>
9 10

struct trace_array;
11
struct trace_buffer;
12
struct tracer;
13
struct dentry;
14

15 16 17 18 19
struct trace_print_flags {
	unsigned long		mask;
	const char		*name;
};

20 21 22 23 24
struct trace_print_flags_u64 {
	unsigned long long	mask;
	const char		*name;
};

25 26 27 28
const char *ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
				   unsigned long flags,
				   const struct trace_print_flags *flag_array);

29 30 31
const char *ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
				     const struct trace_print_flags *symbol_array);

32 33 34 35 36 37 38
#if BITS_PER_LONG == 32
const char *ftrace_print_symbols_seq_u64(struct trace_seq *p,
					 unsigned long long val,
					 const struct trace_print_flags_u64
								 *symbol_array);
#endif

K
Kei Tokunaga 已提交
39 40 41
const char *ftrace_print_hex_seq(struct trace_seq *p,
				 const unsigned char *buf, int len);

42 43 44 45 46 47
struct trace_iterator;
struct trace_event;

int ftrace_raw_output_prep(struct trace_iterator *iter,
			   struct trace_event *event);

48 49 50 51 52 53 54
/*
 * The trace entry - the most basic unit of tracing. This is what
 * is printed in the end as a single line in the trace output, such as:
 *
 *     bash-15816 [01]   235.197585: idle_cpu <- irq_enter
 */
struct trace_entry {
55
	unsigned short		type;
56 57 58 59 60
	unsigned char		flags;
	unsigned char		preempt_count;
	int			pid;
};

61 62 63
#define FTRACE_MAX_EVENT						\
	((1 << (sizeof(((struct trace_entry *)0)->type) * 8)) - 1)

64 65 66 67 68 69 70
/*
 * Trace iterator - used by printout routines who present trace
 * results to users and which routines might sleep, etc:
 */
struct trace_iterator {
	struct trace_array	*tr;
	struct tracer		*trace;
71
	struct trace_buffer	*trace_buffer;
72 73 74
	void			*private;
	int			cpu_file;
	struct mutex		mutex;
75
	struct ring_buffer_iter	**buffer_iter;
76
	unsigned long		iter_flags;
77

78 79 80
	/* trace_seq for __print_flags() and __print_symbolic() etc. */
	struct trace_seq	tmp_seq;

81 82 83 84 85
	cpumask_var_t		started;

	/* it's true when current open file is snapshot */
	bool			snapshot;

86 87 88
	/* The below is zeroed out in pipe_read */
	struct trace_seq	seq;
	struct trace_entry	*ent;
89
	unsigned long		lost_events;
90
	int			leftover;
91
	int			ent_size;
92 93 94 95 96 97
	int			cpu;
	u64			ts;

	loff_t			pos;
	long			idx;

98
	/* All new field here will be zeroed out in pipe_read */
99 100
};

101 102 103 104 105 106
enum trace_iter_flags {
	TRACE_FILE_LAT_FMT	= 1,
	TRACE_FILE_ANNOTATE	= 2,
	TRACE_FILE_TIME_IN_NS	= 4,
};

107 108

typedef enum print_line_t (*trace_print_func)(struct trace_iterator *iter,
109 110 111
				      int flags, struct trace_event *event);

struct trace_event_functions {
112 113 114 115 116 117
	trace_print_func	trace;
	trace_print_func	raw;
	trace_print_func	hex;
	trace_print_func	binary;
};

118 119 120 121 122 123 124
struct trace_event {
	struct hlist_node		node;
	struct list_head		list;
	int				type;
	struct trace_event_functions	*funcs;
};

125 126 127 128 129 130 131 132 133 134 135
extern int register_ftrace_event(struct trace_event *event);
extern int unregister_ftrace_event(struct trace_event *event);

/* Return values for print_line callback */
enum print_line_t {
	TRACE_TYPE_PARTIAL_LINE	= 0,	/* Retry after flushing the seq */
	TRACE_TYPE_HANDLED	= 1,
	TRACE_TYPE_UNHANDLED	= 2,	/* Relay to other output functions */
	TRACE_TYPE_NO_CONSUME	= 3	/* Handled but ask to not consume */
};

136 137 138
void tracing_generic_entry_update(struct trace_entry *entry,
				  unsigned long flags,
				  int pc);
139 140 141 142 143 144 145
struct ftrace_event_file;

struct ring_buffer_event *
trace_event_buffer_lock_reserve(struct ring_buffer **current_buffer,
				struct ftrace_event_file *ftrace_file,
				int type, unsigned long len,
				unsigned long flags, int pc);
146
struct ring_buffer_event *
147 148
trace_current_buffer_lock_reserve(struct ring_buffer **current_buffer,
				  int type, unsigned long len,
149
				  unsigned long flags, int pc);
150 151
void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
					struct ring_buffer_event *event,
152
					unsigned long flags, int pc);
153 154 155 156 157 158 159
void trace_buffer_unlock_commit(struct ring_buffer *buffer,
				struct ring_buffer_event *event,
				unsigned long flags, int pc);
void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
				     struct ring_buffer_event *event,
				     unsigned long flags, int pc,
				     struct pt_regs *regs);
160 161
void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
					 struct ring_buffer_event *event);
162 163 164

void tracing_record_cmdline(struct task_struct *tsk);

165 166
struct event_filter;

167 168 169
enum trace_reg {
	TRACE_REG_REGISTER,
	TRACE_REG_UNREGISTER,
170
#ifdef CONFIG_PERF_EVENTS
171 172
	TRACE_REG_PERF_REGISTER,
	TRACE_REG_PERF_UNREGISTER,
173 174
	TRACE_REG_PERF_OPEN,
	TRACE_REG_PERF_CLOSE,
175 176
	TRACE_REG_PERF_ADD,
	TRACE_REG_PERF_DEL,
177
#endif
178 179 180 181
};

struct ftrace_event_call;

182 183
struct ftrace_event_class {
	char			*system;
184 185 186 187 188
	void			*probe;
#ifdef CONFIG_PERF_EVENTS
	void			*perf_probe;
#endif
	int			(*reg)(struct ftrace_event_call *event,
189
				       enum trace_reg type, void *data);
190 191 192
	int			(*define_fields)(struct ftrace_event_call *);
	struct list_head	*(*get_fields)(struct ftrace_event_call *);
	struct list_head	fields;
193
	int			(*raw_init)(struct ftrace_event_call *);
194 195
};

196
extern int ftrace_event_reg(struct ftrace_event_call *event,
197
			    enum trace_reg type, void *data);
198

199 200
enum {
	TRACE_EVENT_FL_FILTERED_BIT,
201
	TRACE_EVENT_FL_CAP_ANY_BIT,
L
Li Zefan 已提交
202
	TRACE_EVENT_FL_NO_SET_FILTER_BIT,
203
	TRACE_EVENT_FL_IGNORE_ENABLE_BIT,
204
	TRACE_EVENT_FL_WAS_ENABLED_BIT,
205
	TRACE_EVENT_FL_USE_CALL_FILTER_BIT,
206 207
};

208 209 210 211 212
/*
 * Event flags:
 *  FILTERED	  - The event has a filter attached
 *  CAP_ANY	  - Any user can enable for perf
 *  NO_SET_FILTER - Set when filter has error and is to be ignored
213
 *  IGNORE_ENABLE - For ftrace internal events, do not enable with debugfs file
214 215 216
 *  WAS_ENABLED   - Set and stays set when an event was ever enabled
 *                    (used for module unloading, if a module event is enabled,
 *                     it is best to clear the buffers that used it).
217
 *  USE_CALL_FILTER - For ftrace internal events, don't use file filter
218
 */
219
enum {
220
	TRACE_EVENT_FL_FILTERED		= (1 << TRACE_EVENT_FL_FILTERED_BIT),
221
	TRACE_EVENT_FL_CAP_ANY		= (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
L
Li Zefan 已提交
222
	TRACE_EVENT_FL_NO_SET_FILTER	= (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
223
	TRACE_EVENT_FL_IGNORE_ENABLE	= (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
224
	TRACE_EVENT_FL_WAS_ENABLED	= (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT),
225
	TRACE_EVENT_FL_USE_CALL_FILTER	= (1 << TRACE_EVENT_FL_USE_CALL_FILTER_BIT),
226 227
};

228
struct ftrace_event_call {
229
	struct list_head	list;
230
	struct ftrace_event_class *class;
231
	char			*name;
232
	struct trace_event	event;
L
Lai Jiangshan 已提交
233
	const char		*print_fmt;
234
	struct event_filter	*filter;
235
	struct list_head	*files;
236
	void			*mod;
237
	void			*data;
238 239 240 241 242 243
	/*
	 *   bit 0:		filter_active
	 *   bit 1:		allow trace by non root (cap any)
	 *   bit 2:		failed to apply filter
	 *   bit 3:		ftrace internal event (do not enable)
	 *   bit 4:		Event was enabled by module
244
	 *   bit 5:		use call filter rather than file filter
245
	 */
246 247 248 249 250
	int			flags; /* static flags of different events */

#ifdef CONFIG_PERF_EVENTS
	int				perf_refcount;
	struct hlist_head __percpu	*perf_events;
251 252 253

	int	(*perf_perm)(struct ftrace_event_call *,
			     struct perf_event *);
254 255 256 257 258 259 260 261 262
#endif
};

struct trace_array;
struct ftrace_subsystem_dir;

enum {
	FTRACE_EVENT_FL_ENABLED_BIT,
	FTRACE_EVENT_FL_RECORDED_CMD_BIT,
263 264
	FTRACE_EVENT_FL_FILTERED_BIT,
	FTRACE_EVENT_FL_NO_SET_FILTER_BIT,
265 266
	FTRACE_EVENT_FL_SOFT_MODE_BIT,
	FTRACE_EVENT_FL_SOFT_DISABLED_BIT,
267
	FTRACE_EVENT_FL_TRIGGER_MODE_BIT,
268 269 270 271
};

/*
 * Ftrace event file flags:
272
 *  ENABLED	  - The event is enabled
273
 *  RECORDED_CMD  - The comms should be recorded at sched_switch
274 275
 *  FILTERED	  - The event has a filter attached
 *  NO_SET_FILTER - Set when filter has error and is to be ignored
276 277 278
 *  SOFT_MODE     - The event is enabled/disabled by SOFT_DISABLED
 *  SOFT_DISABLED - When set, do not trace the event (even though its
 *                   tracepoint may be enabled)
279
 *  TRIGGER_MODE  - When set, invoke the triggers associated with the event
280 281 282 283
 */
enum {
	FTRACE_EVENT_FL_ENABLED		= (1 << FTRACE_EVENT_FL_ENABLED_BIT),
	FTRACE_EVENT_FL_RECORDED_CMD	= (1 << FTRACE_EVENT_FL_RECORDED_CMD_BIT),
284 285
	FTRACE_EVENT_FL_FILTERED	= (1 << FTRACE_EVENT_FL_FILTERED_BIT),
	FTRACE_EVENT_FL_NO_SET_FILTER	= (1 << FTRACE_EVENT_FL_NO_SET_FILTER_BIT),
286 287
	FTRACE_EVENT_FL_SOFT_MODE	= (1 << FTRACE_EVENT_FL_SOFT_MODE_BIT),
	FTRACE_EVENT_FL_SOFT_DISABLED	= (1 << FTRACE_EVENT_FL_SOFT_DISABLED_BIT),
288
	FTRACE_EVENT_FL_TRIGGER_MODE	= (1 << FTRACE_EVENT_FL_TRIGGER_MODE_BIT),
289 290 291 292 293
};

struct ftrace_event_file {
	struct list_head		list;
	struct ftrace_event_call	*event_call;
294
	struct event_filter		*filter;
295 296 297
	struct dentry			*dir;
	struct trace_array		*tr;
	struct ftrace_subsystem_dir	*system;
298
	struct list_head		triggers;
299

300 301
	/*
	 * 32 bit flags:
302 303
	 *   bit 0:		enabled
	 *   bit 1:		enabled cmd record
304 305
	 *   bit 2:		enable/disable with the soft disable bit
	 *   bit 3:		soft disabled
306
	 *   bit 4:		trigger enabled
307
	 *
308 309 310
	 * Note: The bits must be set atomically to prevent races
	 * from other writers. Reads of flags do not need to be in
	 * sync as they occur in critical sections. But the way flags
311
	 * is currently used, these changes do not affect the code
312 313
	 * except that when a change is made, it may have a slight
	 * delay in propagating the changes to other CPUs due to
314
	 * caching and such. Which is mostly OK ;-)
315
	 */
316
	unsigned long		flags;
317
	atomic_t		sm_ref;	/* soft-mode reference counter */
318
	atomic_t		tm_ref;	/* trigger-mode reference counter */
319 320
};

321 322 323 324 325 326 327 328
#define __TRACE_EVENT_FLAGS(name, value)				\
	static int __init trace_init_flags_##name(void)			\
	{								\
		event_##name.flags = value;				\
		return 0;						\
	}								\
	early_initcall(trace_init_flags_##name);

329 330 331 332 333 334 335 336 337 338 339 340 341
#define __TRACE_EVENT_PERF_PERM(name, expr...)				\
	static int perf_perm_##name(struct ftrace_event_call *tp_event, \
				    struct perf_event *p_event)		\
	{								\
		return ({ expr; });					\
	}								\
	static int __init trace_init_perf_perm_##name(void)		\
	{								\
		event_##name.perf_perm = &perf_perm_##name;		\
		return 0;						\
	}								\
	early_initcall(trace_init_perf_perm_##name);

342
#define PERF_MAX_TRACE_SIZE	2048
343

344
#define MAX_FILTER_STR_VAL	256	/* Should handle KSYM_SYMBOL_LEN */
345

346 347
enum event_trigger_type {
	ETT_NONE		= (0),
348
	ETT_TRACE_ONOFF		= (1 << 0),
349
	ETT_SNAPSHOT		= (1 << 1),
350 351
};

352 353
extern void destroy_preds(struct ftrace_event_file *file);
extern void destroy_call_preds(struct ftrace_event_call *call);
L
Li Zefan 已提交
354
extern int filter_match_preds(struct event_filter *filter, void *rec);
355 356 357 358 359 360 361

extern int filter_check_discard(struct ftrace_event_file *file, void *rec,
				struct ring_buffer *buffer,
				struct ring_buffer_event *event);
extern int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
				     struct ring_buffer *buffer,
				     struct ring_buffer_event *event);
362
extern void event_triggers_call(struct ftrace_event_file *file);
363

364 365 366 367
enum {
	FILTER_OTHER = 0,
	FILTER_STATIC_STRING,
	FILTER_DYN_STRING,
368
	FILTER_PTR_STRING,
369
	FILTER_TRACE_FN,
370 371
};

372 373 374 375
#define EVENT_STORAGE_SIZE 128
extern struct mutex event_storage_mutex;
extern char event_storage[EVENT_STORAGE_SIZE];

376
extern int trace_event_raw_init(struct ftrace_event_call *call);
377 378 379
extern int trace_define_field(struct ftrace_event_call *call, const char *type,
			      const char *name, int offset, int size,
			      int is_signed, int filter_type);
380
extern int trace_add_event_call(struct ftrace_event_call *call);
381
extern int trace_remove_event_call(struct ftrace_event_call *call);
382

383
#define is_signed_type(type)	(((type)(-1)) < (type)1)
384

385 386
int trace_set_clr_event(const char *system, const char *event, int set);

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/*
 * The double __builtin_constant_p is because gcc will give us an error
 * if we try to allocate the static variable to fmt if it is not a
 * constant. Even with the outer if statement optimizing out.
 */
#define event_trace_printk(ip, fmt, args...)				\
do {									\
	__trace_printk_check_format(fmt, ##args);			\
	tracing_record_cmdline(current);				\
	if (__builtin_constant_p(fmt)) {				\
		static const char *trace_printk_fmt			\
		  __attribute__((section("__trace_printk_fmt"))) =	\
			__builtin_constant_p(fmt) ? fmt : NULL;		\
									\
		__trace_bprintk(ip, trace_printk_fmt, ##args);		\
	} else								\
		__trace_printk(ip, fmt, ##args);			\
} while (0)

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/**
 * tracepoint_string - register constant persistent string to trace system
 * @str - a constant persistent string that will be referenced in tracepoints
 *
 * If constant strings are being used in tracepoints, it is faster and
 * more efficient to just save the pointer to the string and reference
 * that with a printf "%s" instead of saving the string in the ring buffer
 * and wasting space and time.
 *
 * The problem with the above approach is that userspace tools that read
 * the binary output of the trace buffers do not have access to the string.
 * Instead they just show the address of the string which is not very
 * useful to users.
 *
 * With tracepoint_string(), the string will be registered to the tracing
 * system and exported to userspace via the debugfs/tracing/printk_formats
 * file that maps the string address to the string text. This way userspace
 * tools that read the binary buffers have a way to map the pointers to
 * the ASCII strings they represent.
 *
 * The @str used must be a constant string and persistent as it would not
 * make sense to show a string that no longer exists. But it is still fine
 * to be used with modules, because when modules are unloaded, if they
 * had tracepoints, the ring buffers are cleared too. As long as the string
 * does not change during the life of the module, it is fine to use
 * tracepoint_string() within a module.
 */
#define tracepoint_string(str)						\
	({								\
		static const char *___tp_str __tracepoint_string = str; \
		___tp_str;						\
	})
#define __tracepoint_string	__attribute__((section("__tracepoint_str")))

440
#ifdef CONFIG_PERF_EVENTS
L
Li Zefan 已提交
441
struct perf_event;
442 443 444

DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);

445 446
extern int  perf_trace_init(struct perf_event *event);
extern void perf_trace_destroy(struct perf_event *event);
P
Peter Zijlstra 已提交
447 448
extern int  perf_trace_add(struct perf_event *event, int flags);
extern void perf_trace_del(struct perf_event *event, int flags);
449
extern int  ftrace_profile_set_filter(struct perf_event *event, int event_id,
L
Li Zefan 已提交
450 451
				     char *filter_str);
extern void ftrace_profile_free_filter(struct perf_event *event);
452 453
extern void *perf_trace_buf_prepare(int size, unsigned short type,
				    struct pt_regs *regs, int *rctxp);
454 455

static inline void
456
perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
457 458
		       u64 count, struct pt_regs *regs, void *head,
		       struct task_struct *task)
459
{
460
	perf_tp_event(addr, count, raw_data, size, regs, head, rctx, task);
461
}
L
Li Zefan 已提交
462 463
#endif

464
#endif /* _LINUX_FTRACE_EVENT_H */