Merge branches 'tracing/fastboot', 'tracing/function-return-tracer' and 'tracing...
[deliverable/linux.git] / kernel / trace / trace.h
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 #include <trace/boot.h>
12
13 enum trace_type {
14 __TRACE_FIRST_TYPE = 0,
15
16 TRACE_FN,
17 TRACE_CTX,
18 TRACE_WAKE,
19 TRACE_CONT,
20 TRACE_STACK,
21 TRACE_PRINT,
22 TRACE_SPECIAL,
23 TRACE_MMIO_RW,
24 TRACE_MMIO_MAP,
25 TRACE_BOOT_CALL,
26 TRACE_BOOT_RET,
27 TRACE_FN_RET,
28
29 __TRACE_LAST_TYPE
30 };
31
32 /*
33 * The trace entry - the most basic unit of tracing. This is what
34 * is printed in the end as a single line in the trace output, such as:
35 *
36 * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
37 */
38 struct trace_entry {
39 unsigned char type;
40 unsigned char cpu;
41 unsigned char flags;
42 unsigned char preempt_count;
43 int pid;
44 };
45
46 /*
47 * Function trace entry - function address and parent function addres:
48 */
49 struct ftrace_entry {
50 struct trace_entry ent;
51 unsigned long ip;
52 unsigned long parent_ip;
53 };
54
55 /* Function return entry */
56 struct ftrace_ret_entry {
57 struct trace_entry ent;
58 unsigned long ip;
59 unsigned long parent_ip;
60 unsigned long long calltime;
61 unsigned long long rettime;
62 };
63 extern struct tracer boot_tracer;
64
65 /*
66 * Context switch trace entry - which task (and prio) we switched from/to:
67 */
68 struct ctx_switch_entry {
69 struct trace_entry ent;
70 unsigned int prev_pid;
71 unsigned char prev_prio;
72 unsigned char prev_state;
73 unsigned int next_pid;
74 unsigned char next_prio;
75 unsigned char next_state;
76 unsigned int next_cpu;
77 };
78
79 /*
80 * Special (free-form) trace entry:
81 */
82 struct special_entry {
83 struct trace_entry ent;
84 unsigned long arg1;
85 unsigned long arg2;
86 unsigned long arg3;
87 };
88
89 /*
90 * Stack-trace entry:
91 */
92
93 #define FTRACE_STACK_ENTRIES 8
94
95 struct stack_entry {
96 struct trace_entry ent;
97 unsigned long caller[FTRACE_STACK_ENTRIES];
98 };
99
100 /*
101 * ftrace_printk entry:
102 */
103 struct print_entry {
104 struct trace_entry ent;
105 unsigned long ip;
106 char buf[];
107 };
108
109 #define TRACE_OLD_SIZE 88
110
111 struct trace_field_cont {
112 unsigned char type;
113 /* Temporary till we get rid of this completely */
114 char buf[TRACE_OLD_SIZE - 1];
115 };
116
117 struct trace_mmiotrace_rw {
118 struct trace_entry ent;
119 struct mmiotrace_rw rw;
120 };
121
122 struct trace_mmiotrace_map {
123 struct trace_entry ent;
124 struct mmiotrace_map map;
125 };
126
127 struct trace_boot_call {
128 struct trace_entry ent;
129 struct boot_trace_call boot_call;
130 };
131
132 struct trace_boot_ret {
133 struct trace_entry ent;
134 struct boot_trace_ret boot_ret;
135 };
136
137 /*
138 * trace_flag_type is an enumeration that holds different
139 * states when a trace occurs. These are:
140 * IRQS_OFF - interrupts were disabled
141 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
142 * NEED_RESCED - reschedule is requested
143 * HARDIRQ - inside an interrupt handler
144 * SOFTIRQ - inside a softirq handler
145 * CONT - multiple entries hold the trace item
146 */
147 enum trace_flag_type {
148 TRACE_FLAG_IRQS_OFF = 0x01,
149 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
150 TRACE_FLAG_NEED_RESCHED = 0x04,
151 TRACE_FLAG_HARDIRQ = 0x08,
152 TRACE_FLAG_SOFTIRQ = 0x10,
153 TRACE_FLAG_CONT = 0x20,
154 };
155
156 #define TRACE_BUF_SIZE 1024
157
158 /*
159 * The CPU trace array - it consists of thousands of trace entries
160 * plus some other descriptor data: (for example which task started
161 * the trace, etc.)
162 */
163 struct trace_array_cpu {
164 atomic_t disabled;
165
166 /* these fields get copied into max-trace: */
167 unsigned long trace_idx;
168 unsigned long overrun;
169 unsigned long saved_latency;
170 unsigned long critical_start;
171 unsigned long critical_end;
172 unsigned long critical_sequence;
173 unsigned long nice;
174 unsigned long policy;
175 unsigned long rt_priority;
176 cycle_t preempt_timestamp;
177 pid_t pid;
178 uid_t uid;
179 char comm[TASK_COMM_LEN];
180 };
181
182 struct trace_iterator;
183
184 /*
185 * The trace array - an array of per-CPU trace arrays. This is the
186 * highest level data structure that individual tracers deal with.
187 * They have on/off state as well:
188 */
189 struct trace_array {
190 struct ring_buffer *buffer;
191 unsigned long entries;
192 int cpu;
193 cycle_t time_start;
194 struct task_struct *waiter;
195 struct trace_array_cpu *data[NR_CPUS];
196 };
197
198 #define FTRACE_CMP_TYPE(var, type) \
199 __builtin_types_compatible_p(typeof(var), type *)
200
201 #undef IF_ASSIGN
202 #define IF_ASSIGN(var, entry, etype, id) \
203 if (FTRACE_CMP_TYPE(var, etype)) { \
204 var = (typeof(var))(entry); \
205 WARN_ON(id && (entry)->type != id); \
206 break; \
207 }
208
209 /* Will cause compile errors if type is not found. */
210 extern void __ftrace_bad_type(void);
211
212 /*
213 * The trace_assign_type is a verifier that the entry type is
214 * the same as the type being assigned. To add new types simply
215 * add a line with the following format:
216 *
217 * IF_ASSIGN(var, ent, type, id);
218 *
219 * Where "type" is the trace type that includes the trace_entry
220 * as the "ent" item. And "id" is the trace identifier that is
221 * used in the trace_type enum.
222 *
223 * If the type can have more than one id, then use zero.
224 */
225 #define trace_assign_type(var, ent) \
226 do { \
227 IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
228 IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
229 IF_ASSIGN(var, ent, struct trace_field_cont, TRACE_CONT); \
230 IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
231 IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
232 IF_ASSIGN(var, ent, struct special_entry, 0); \
233 IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
234 TRACE_MMIO_RW); \
235 IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
236 TRACE_MMIO_MAP); \
237 IF_ASSIGN(var, ent, struct trace_boot_call, TRACE_BOOT_CALL);\
238 IF_ASSIGN(var, ent, struct trace_boot_ret, TRACE_BOOT_RET);\
239 IF_ASSIGN(var, ent, struct ftrace_ret_entry, TRACE_FN_RET);\
240 __ftrace_bad_type(); \
241 } while (0)
242
243 /* Return values for print_line callback */
244 enum print_line_t {
245 TRACE_TYPE_PARTIAL_LINE = 0, /* Retry after flushing the seq */
246 TRACE_TYPE_HANDLED = 1,
247 TRACE_TYPE_UNHANDLED = 2 /* Relay to other output functions */
248 };
249
250 /*
251 * A specific tracer, represented by methods that operate on a trace array:
252 */
253 struct tracer {
254 const char *name;
255 void (*init)(struct trace_array *tr);
256 void (*reset)(struct trace_array *tr);
257 void (*start)(struct trace_array *tr);
258 void (*stop)(struct trace_array *tr);
259 void (*open)(struct trace_iterator *iter);
260 void (*pipe_open)(struct trace_iterator *iter);
261 void (*close)(struct trace_iterator *iter);
262 ssize_t (*read)(struct trace_iterator *iter,
263 struct file *filp, char __user *ubuf,
264 size_t cnt, loff_t *ppos);
265 #ifdef CONFIG_FTRACE_STARTUP_TEST
266 int (*selftest)(struct tracer *trace,
267 struct trace_array *tr);
268 #endif
269 enum print_line_t (*print_line)(struct trace_iterator *iter);
270 struct tracer *next;
271 int print_max;
272 };
273
274 struct trace_seq {
275 unsigned char buffer[PAGE_SIZE];
276 unsigned int len;
277 unsigned int readpos;
278 };
279
280 /*
281 * Trace iterator - used by printout routines who present trace
282 * results to users and which routines might sleep, etc:
283 */
284 struct trace_iterator {
285 struct trace_array *tr;
286 struct tracer *trace;
287 void *private;
288 struct ring_buffer_iter *buffer_iter[NR_CPUS];
289
290 /* The below is zeroed out in pipe_read */
291 struct trace_seq seq;
292 struct trace_entry *ent;
293 int cpu;
294 u64 ts;
295
296 unsigned long iter_flags;
297 loff_t pos;
298 long idx;
299
300 cpumask_t started;
301 };
302
303 int tracing_is_enabled(void);
304 void trace_wake_up(void);
305 void tracing_reset(struct trace_array *tr, int cpu);
306 int tracing_open_generic(struct inode *inode, struct file *filp);
307 struct dentry *tracing_init_dentry(void);
308 void init_tracer_sysprof_debugfs(struct dentry *d_tracer);
309
310 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
311 struct trace_array_cpu *data);
312 void tracing_generic_entry_update(struct trace_entry *entry,
313 unsigned long flags,
314 int pc);
315
316 void ftrace(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 void tracing_sched_switch_trace(struct trace_array *tr,
322 struct trace_array_cpu *data,
323 struct task_struct *prev,
324 struct task_struct *next,
325 unsigned long flags, int pc);
326 void tracing_record_cmdline(struct task_struct *tsk);
327
328 void tracing_sched_wakeup_trace(struct trace_array *tr,
329 struct trace_array_cpu *data,
330 struct task_struct *wakee,
331 struct task_struct *cur,
332 unsigned long flags, int pc);
333 void trace_special(struct trace_array *tr,
334 struct trace_array_cpu *data,
335 unsigned long arg1,
336 unsigned long arg2,
337 unsigned long arg3, int pc);
338 void trace_function(struct trace_array *tr,
339 struct trace_array_cpu *data,
340 unsigned long ip,
341 unsigned long parent_ip,
342 unsigned long flags, int pc);
343 void
344 trace_function_return(struct ftrace_retfunc *trace);
345
346 void tracing_start_cmdline_record(void);
347 void tracing_stop_cmdline_record(void);
348 void tracing_sched_switch_assign_trace(struct trace_array *tr);
349 void tracing_stop_sched_switch_record(void);
350 void tracing_start_sched_switch_record(void);
351 int register_tracer(struct tracer *type);
352 void unregister_tracer(struct tracer *type);
353
354 extern unsigned long nsecs_to_usecs(unsigned long nsecs);
355
356 extern unsigned long tracing_max_latency;
357 extern unsigned long tracing_thresh;
358
359 void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
360 void update_max_tr_single(struct trace_array *tr,
361 struct task_struct *tsk, int cpu);
362
363 extern cycle_t ftrace_now(int cpu);
364
365 #ifdef CONFIG_FUNCTION_TRACER
366 void tracing_start_function_trace(void);
367 void tracing_stop_function_trace(void);
368 #else
369 # define tracing_start_function_trace() do { } while (0)
370 # define tracing_stop_function_trace() do { } while (0)
371 #endif
372
373 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
374 typedef void
375 (*tracer_switch_func_t)(void *private,
376 void *__rq,
377 struct task_struct *prev,
378 struct task_struct *next);
379
380 struct tracer_switch_ops {
381 tracer_switch_func_t func;
382 void *private;
383 struct tracer_switch_ops *next;
384 };
385
386 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
387
388 #ifdef CONFIG_DYNAMIC_FTRACE
389 extern unsigned long ftrace_update_tot_cnt;
390 #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
391 extern int DYN_FTRACE_TEST_NAME(void);
392 #endif
393
394 #ifdef CONFIG_FTRACE_STARTUP_TEST
395 extern int trace_selftest_startup_function(struct tracer *trace,
396 struct trace_array *tr);
397 extern int trace_selftest_startup_irqsoff(struct tracer *trace,
398 struct trace_array *tr);
399 extern int trace_selftest_startup_preemptoff(struct tracer *trace,
400 struct trace_array *tr);
401 extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
402 struct trace_array *tr);
403 extern int trace_selftest_startup_wakeup(struct tracer *trace,
404 struct trace_array *tr);
405 extern int trace_selftest_startup_nop(struct tracer *trace,
406 struct trace_array *tr);
407 extern int trace_selftest_startup_sched_switch(struct tracer *trace,
408 struct trace_array *tr);
409 extern int trace_selftest_startup_sysprof(struct tracer *trace,
410 struct trace_array *tr);
411 #endif /* CONFIG_FTRACE_STARTUP_TEST */
412
413 extern void *head_page(struct trace_array_cpu *data);
414 extern int trace_seq_printf(struct trace_seq *s, const char *fmt, ...);
415 extern void trace_seq_print_cont(struct trace_seq *s,
416 struct trace_iterator *iter);
417
418 extern int
419 seq_print_ip_sym(struct trace_seq *s, unsigned long ip,
420 unsigned long sym_flags);
421 extern ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
422 size_t cnt);
423 extern long ns2usecs(cycle_t nsec);
424 extern int trace_vprintk(unsigned long ip, const char *fmt, va_list args);
425
426 extern unsigned long trace_flags;
427
428 /* Standard output formatting function used for function return traces */
429 #ifdef CONFIG_FUNCTION_RET_TRACER
430 extern enum print_line_t print_return_function(struct trace_iterator *iter);
431 #else
432 static inline enum print_line_t
433 print_return_function(struct trace_iterator *iter)
434 {
435 return TRACE_TYPE_UNHANDLED;
436 }
437 #endif
438
439 /*
440 * trace_iterator_flags is an enumeration that defines bit
441 * positions into trace_flags that controls the output.
442 *
443 * NOTE: These bits must match the trace_options array in
444 * trace.c.
445 */
446 enum trace_iterator_flags {
447 TRACE_ITER_PRINT_PARENT = 0x01,
448 TRACE_ITER_SYM_OFFSET = 0x02,
449 TRACE_ITER_SYM_ADDR = 0x04,
450 TRACE_ITER_VERBOSE = 0x08,
451 TRACE_ITER_RAW = 0x10,
452 TRACE_ITER_HEX = 0x20,
453 TRACE_ITER_BIN = 0x40,
454 TRACE_ITER_BLOCK = 0x80,
455 TRACE_ITER_STACKTRACE = 0x100,
456 TRACE_ITER_SCHED_TREE = 0x200,
457 TRACE_ITER_PRINTK = 0x400,
458 TRACE_ITER_PREEMPTONLY = 0x800,
459 };
460
461 /*
462 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
463 * control the output of kernel symbols.
464 */
465 #define TRACE_ITER_SYM_MASK \
466 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
467
468 extern struct tracer nop_trace;
469
470 /**
471 * ftrace_preempt_disable - disable preemption scheduler safe
472 *
473 * When tracing can happen inside the scheduler, there exists
474 * cases that the tracing might happen before the need_resched
475 * flag is checked. If this happens and the tracer calls
476 * preempt_enable (after a disable), a schedule might take place
477 * causing an infinite recursion.
478 *
479 * To prevent this, we read the need_recshed flag before
480 * disabling preemption. When we want to enable preemption we
481 * check the flag, if it is set, then we call preempt_enable_no_resched.
482 * Otherwise, we call preempt_enable.
483 *
484 * The rational for doing the above is that if need resched is set
485 * and we have yet to reschedule, we are either in an atomic location
486 * (where we do not need to check for scheduling) or we are inside
487 * the scheduler and do not want to resched.
488 */
489 static inline int ftrace_preempt_disable(void)
490 {
491 int resched;
492
493 resched = need_resched();
494 preempt_disable_notrace();
495
496 return resched;
497 }
498
499 /**
500 * ftrace_preempt_enable - enable preemption scheduler safe
501 * @resched: the return value from ftrace_preempt_disable
502 *
503 * This is a scheduler safe way to enable preemption and not miss
504 * any preemption checks. The disabled saved the state of preemption.
505 * If resched is set, then we were either inside an atomic or
506 * are inside the scheduler (we would have already scheduled
507 * otherwise). In this case, we do not want to call normal
508 * preempt_enable, but preempt_enable_no_resched instead.
509 */
510 static inline void ftrace_preempt_enable(int resched)
511 {
512 if (resched)
513 preempt_enable_no_resched_notrace();
514 else
515 preempt_enable_notrace();
516 }
517
518 #endif /* _LINUX_KERNEL_TRACE_H */
This page took 0.050647 seconds and 6 git commands to generate.