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