xref: /linux/kernel/trace/trace.h (revision 3eeebf17f31c583f83e081b17b3076477cb96886)
1 #ifndef _LINUX_KERNEL_TRACE_H
2 #define _LINUX_KERNEL_TRACE_H
3 
4 #include <linux/fs.h>
5 #include <asm/atomic.h>
6 #include <linux/sched.h>
7 #include <linux/clocksource.h>
8 #include <linux/ring_buffer.h>
9 #include <linux/mmiotrace.h>
10 #include <linux/ftrace.h>
11 
12 enum trace_type {
13 	__TRACE_FIRST_TYPE = 0,
14 
15 	TRACE_FN,
16 	TRACE_CTX,
17 	TRACE_WAKE,
18 	TRACE_CONT,
19 	TRACE_STACK,
20 	TRACE_PRINT,
21 	TRACE_SPECIAL,
22 	TRACE_MMIO_RW,
23 	TRACE_MMIO_MAP,
24 	TRACE_BOOT,
25 
26 	__TRACE_LAST_TYPE
27 };
28 
29 /*
30  * The trace entry - the most basic unit of tracing. This is what
31  * is printed in the end as a single line in the trace output, such as:
32  *
33  *     bash-15816 [01]   235.197585: idle_cpu <- irq_enter
34  */
35 struct trace_entry {
36 	unsigned char		type;
37 	unsigned char		cpu;
38 	unsigned char		flags;
39 	unsigned char		preempt_count;
40 	int			pid;
41 };
42 
43 /*
44  * Function trace entry - function address and parent function addres:
45  */
46 struct ftrace_entry {
47 	struct trace_entry	ent;
48 	unsigned long		ip;
49 	unsigned long		parent_ip;
50 };
51 extern struct tracer boot_tracer;
52 
53 /*
54  * Context switch trace entry - which task (and prio) we switched from/to:
55  */
56 struct ctx_switch_entry {
57 	struct trace_entry	ent;
58 	unsigned int		prev_pid;
59 	unsigned char		prev_prio;
60 	unsigned char		prev_state;
61 	unsigned int		next_pid;
62 	unsigned char		next_prio;
63 	unsigned char		next_state;
64 	unsigned int		next_cpu;
65 };
66 
67 /*
68  * Special (free-form) trace entry:
69  */
70 struct special_entry {
71 	struct trace_entry	ent;
72 	unsigned long		arg1;
73 	unsigned long		arg2;
74 	unsigned long		arg3;
75 };
76 
77 /*
78  * Stack-trace entry:
79  */
80 
81 #define FTRACE_STACK_ENTRIES	8
82 
83 struct stack_entry {
84 	struct trace_entry	ent;
85 	unsigned long		caller[FTRACE_STACK_ENTRIES];
86 };
87 
88 /*
89  * ftrace_printk entry:
90  */
91 struct print_entry {
92 	struct trace_entry	ent;
93 	unsigned long		ip;
94 	char			buf[];
95 };
96 
97 #define TRACE_OLD_SIZE		88
98 
99 struct trace_field_cont {
100 	unsigned char		type;
101 	/* Temporary till we get rid of this completely */
102 	char			buf[TRACE_OLD_SIZE - 1];
103 };
104 
105 struct trace_mmiotrace_rw {
106 	struct trace_entry	ent;
107 	struct mmiotrace_rw	rw;
108 };
109 
110 struct trace_mmiotrace_map {
111 	struct trace_entry	ent;
112 	struct mmiotrace_map	map;
113 };
114 
115 struct trace_boot {
116 	struct trace_entry	ent;
117 	struct boot_trace	initcall;
118 };
119 
120 /*
121  * trace_flag_type is an enumeration that holds different
122  * states when a trace occurs. These are:
123  *  IRQS_OFF	- interrupts were disabled
124  *  NEED_RESCED - reschedule is requested
125  *  HARDIRQ	- inside an interrupt handler
126  *  SOFTIRQ	- inside a softirq handler
127  *  CONT	- multiple entries hold the trace item
128  */
129 enum trace_flag_type {
130 	TRACE_FLAG_IRQS_OFF		= 0x01,
131 	TRACE_FLAG_NEED_RESCHED		= 0x02,
132 	TRACE_FLAG_HARDIRQ		= 0x04,
133 	TRACE_FLAG_SOFTIRQ		= 0x08,
134 	TRACE_FLAG_CONT			= 0x10,
135 };
136 
137 #define TRACE_BUF_SIZE		1024
138 
139 /*
140  * The CPU trace array - it consists of thousands of trace entries
141  * plus some other descriptor data: (for example which task started
142  * the trace, etc.)
143  */
144 struct trace_array_cpu {
145 	atomic_t		disabled;
146 
147 	/* these fields get copied into max-trace: */
148 	unsigned long		trace_idx;
149 	unsigned long		overrun;
150 	unsigned long		saved_latency;
151 	unsigned long		critical_start;
152 	unsigned long		critical_end;
153 	unsigned long		critical_sequence;
154 	unsigned long		nice;
155 	unsigned long		policy;
156 	unsigned long		rt_priority;
157 	cycle_t			preempt_timestamp;
158 	pid_t			pid;
159 	uid_t			uid;
160 	char			comm[TASK_COMM_LEN];
161 };
162 
163 struct trace_iterator;
164 
165 /*
166  * The trace array - an array of per-CPU trace arrays. This is the
167  * highest level data structure that individual tracers deal with.
168  * They have on/off state as well:
169  */
170 struct trace_array {
171 	struct ring_buffer	*buffer;
172 	unsigned long		entries;
173 	long			ctrl;
174 	int			cpu;
175 	cycle_t			time_start;
176 	struct task_struct	*waiter;
177 	struct trace_array_cpu	*data[NR_CPUS];
178 };
179 
180 #define FTRACE_CMP_TYPE(var, type) \
181 	__builtin_types_compatible_p(typeof(var), type *)
182 
183 #undef IF_ASSIGN
184 #define IF_ASSIGN(var, entry, etype, id)		\
185 	if (FTRACE_CMP_TYPE(var, etype)) {		\
186 		var = (typeof(var))(entry);		\
187 		WARN_ON(id && (entry)->type != id);	\
188 		break;					\
189 	}
190 
191 /* Will cause compile errors if type is not found. */
192 extern void __ftrace_bad_type(void);
193 
194 /*
195  * The trace_assign_type is a verifier that the entry type is
196  * the same as the type being assigned. To add new types simply
197  * add a line with the following format:
198  *
199  * IF_ASSIGN(var, ent, type, id);
200  *
201  *  Where "type" is the trace type that includes the trace_entry
202  *  as the "ent" item. And "id" is the trace identifier that is
203  *  used in the trace_type enum.
204  *
205  *  If the type can have more than one id, then use zero.
206  */
207 #define trace_assign_type(var, ent)					\
208 	do {								\
209 		IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN);	\
210 		IF_ASSIGN(var, ent, struct ctx_switch_entry, 0);	\
211 		IF_ASSIGN(var, ent, struct trace_field_cont, TRACE_CONT); \
212 		IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK);	\
213 		IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT);	\
214 		IF_ASSIGN(var, ent, struct special_entry, 0);		\
215 		IF_ASSIGN(var, ent, struct trace_mmiotrace_rw,		\
216 			  TRACE_MMIO_RW);				\
217 		IF_ASSIGN(var, ent, struct trace_mmiotrace_map,		\
218 			  TRACE_MMIO_MAP);				\
219 		IF_ASSIGN(var, ent, struct trace_boot, TRACE_BOOT);	\
220 		__ftrace_bad_type();					\
221 	} while (0)
222 
223 /* Return values for print_line callback */
224 enum print_line_t {
225 	TRACE_TYPE_PARTIAL_LINE	= 0,	/* Retry after flushing the seq */
226 	TRACE_TYPE_HANDLED	= 1,
227 	TRACE_TYPE_UNHANDLED	= 2	/* Relay to other output functions */
228 };
229 
230 /*
231  * A specific tracer, represented by methods that operate on a trace array:
232  */
233 struct tracer {
234 	const char		*name;
235 	void			(*init)(struct trace_array *tr);
236 	void			(*reset)(struct trace_array *tr);
237 	void			(*open)(struct trace_iterator *iter);
238 	void			(*pipe_open)(struct trace_iterator *iter);
239 	void			(*close)(struct trace_iterator *iter);
240 	void			(*start)(struct trace_iterator *iter);
241 	void			(*stop)(struct trace_iterator *iter);
242 	ssize_t			(*read)(struct trace_iterator *iter,
243 					struct file *filp, char __user *ubuf,
244 					size_t cnt, loff_t *ppos);
245 	void			(*ctrl_update)(struct trace_array *tr);
246 #ifdef CONFIG_FTRACE_STARTUP_TEST
247 	int			(*selftest)(struct tracer *trace,
248 					    struct trace_array *tr);
249 #endif
250 	enum print_line_t	(*print_line)(struct trace_iterator *iter);
251 	struct tracer		*next;
252 	int			print_max;
253 };
254 
255 struct trace_seq {
256 	unsigned char		buffer[PAGE_SIZE];
257 	unsigned int		len;
258 	unsigned int		readpos;
259 };
260 
261 /*
262  * Trace iterator - used by printout routines who present trace
263  * results to users and which routines might sleep, etc:
264  */
265 struct trace_iterator {
266 	struct trace_array	*tr;
267 	struct tracer		*trace;
268 	void			*private;
269 	struct ring_buffer_iter	*buffer_iter[NR_CPUS];
270 
271 	/* The below is zeroed out in pipe_read */
272 	struct trace_seq	seq;
273 	struct trace_entry	*ent;
274 	int			cpu;
275 	u64			ts;
276 
277 	unsigned long		iter_flags;
278 	loff_t			pos;
279 	long			idx;
280 };
281 
282 void trace_wake_up(void);
283 void tracing_reset(struct trace_array *tr, int cpu);
284 int tracing_open_generic(struct inode *inode, struct file *filp);
285 struct dentry *tracing_init_dentry(void);
286 void init_tracer_sysprof_debugfs(struct dentry *d_tracer);
287 
288 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
289 						struct trace_array_cpu *data);
290 void tracing_generic_entry_update(struct trace_entry *entry,
291 				  unsigned long flags,
292 				  int pc);
293 
294 void ftrace(struct trace_array *tr,
295 			    struct trace_array_cpu *data,
296 			    unsigned long ip,
297 			    unsigned long parent_ip,
298 			    unsigned long flags, int pc);
299 void tracing_sched_switch_trace(struct trace_array *tr,
300 				struct trace_array_cpu *data,
301 				struct task_struct *prev,
302 				struct task_struct *next,
303 				unsigned long flags, int pc);
304 void tracing_record_cmdline(struct task_struct *tsk);
305 
306 void tracing_sched_wakeup_trace(struct trace_array *tr,
307 				struct trace_array_cpu *data,
308 				struct task_struct *wakee,
309 				struct task_struct *cur,
310 				unsigned long flags, int pc);
311 void trace_special(struct trace_array *tr,
312 		   struct trace_array_cpu *data,
313 		   unsigned long arg1,
314 		   unsigned long arg2,
315 		   unsigned long arg3, int pc);
316 void trace_function(struct trace_array *tr,
317 		    struct trace_array_cpu *data,
318 		    unsigned long ip,
319 		    unsigned long parent_ip,
320 		    unsigned long flags, int pc);
321 
322 void tracing_start_cmdline_record(void);
323 void tracing_stop_cmdline_record(void);
324 int register_tracer(struct tracer *type);
325 void unregister_tracer(struct tracer *type);
326 
327 extern unsigned long nsecs_to_usecs(unsigned long nsecs);
328 
329 extern unsigned long tracing_max_latency;
330 extern unsigned long tracing_thresh;
331 
332 void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
333 void update_max_tr_single(struct trace_array *tr,
334 			  struct task_struct *tsk, int cpu);
335 
336 extern cycle_t ftrace_now(int cpu);
337 
338 #ifdef CONFIG_FTRACE
339 void tracing_start_function_trace(void);
340 void tracing_stop_function_trace(void);
341 #else
342 # define tracing_start_function_trace()		do { } while (0)
343 # define tracing_stop_function_trace()		do { } while (0)
344 #endif
345 
346 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
347 typedef void
348 (*tracer_switch_func_t)(void *private,
349 			void *__rq,
350 			struct task_struct *prev,
351 			struct task_struct *next);
352 
353 struct tracer_switch_ops {
354 	tracer_switch_func_t		func;
355 	void				*private;
356 	struct tracer_switch_ops	*next;
357 };
358 
359 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
360 
361 #ifdef CONFIG_DYNAMIC_FTRACE
362 extern unsigned long ftrace_update_tot_cnt;
363 #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
364 extern int DYN_FTRACE_TEST_NAME(void);
365 #endif
366 
367 #ifdef CONFIG_FTRACE_STARTUP_TEST
368 extern int trace_selftest_startup_function(struct tracer *trace,
369 					   struct trace_array *tr);
370 extern int trace_selftest_startup_irqsoff(struct tracer *trace,
371 					  struct trace_array *tr);
372 extern int trace_selftest_startup_preemptoff(struct tracer *trace,
373 					     struct trace_array *tr);
374 extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
375 						 struct trace_array *tr);
376 extern int trace_selftest_startup_wakeup(struct tracer *trace,
377 					 struct trace_array *tr);
378 extern int trace_selftest_startup_nop(struct tracer *trace,
379 					 struct trace_array *tr);
380 extern int trace_selftest_startup_sched_switch(struct tracer *trace,
381 					       struct trace_array *tr);
382 extern int trace_selftest_startup_sysprof(struct tracer *trace,
383 					       struct trace_array *tr);
384 #endif /* CONFIG_FTRACE_STARTUP_TEST */
385 
386 extern void *head_page(struct trace_array_cpu *data);
387 extern int trace_seq_printf(struct trace_seq *s, const char *fmt, ...);
388 extern void trace_seq_print_cont(struct trace_seq *s,
389 				 struct trace_iterator *iter);
390 extern ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
391 				 size_t cnt);
392 extern long ns2usecs(cycle_t nsec);
393 extern int trace_vprintk(unsigned long ip, const char *fmt, va_list args);
394 
395 extern unsigned long trace_flags;
396 
397 /*
398  * trace_iterator_flags is an enumeration that defines bit
399  * positions into trace_flags that controls the output.
400  *
401  * NOTE: These bits must match the trace_options array in
402  *       trace.c.
403  */
404 enum trace_iterator_flags {
405 	TRACE_ITER_PRINT_PARENT		= 0x01,
406 	TRACE_ITER_SYM_OFFSET		= 0x02,
407 	TRACE_ITER_SYM_ADDR		= 0x04,
408 	TRACE_ITER_VERBOSE		= 0x08,
409 	TRACE_ITER_RAW			= 0x10,
410 	TRACE_ITER_HEX			= 0x20,
411 	TRACE_ITER_BIN			= 0x40,
412 	TRACE_ITER_BLOCK		= 0x80,
413 	TRACE_ITER_STACKTRACE		= 0x100,
414 	TRACE_ITER_SCHED_TREE		= 0x200,
415 	TRACE_ITER_PRINTK		= 0x400,
416 };
417 
418 extern struct tracer nop_trace;
419 
420 #endif /* _LINUX_KERNEL_TRACE_H */
421