1 #ifndef _LINUX_KERNEL_TRACE_H
2 #define _LINUX_KERNEL_TRACE_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>
14 __TRACE_FIRST_TYPE = 0,
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:
35 * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
41 unsigned char preempt_count;
46 * Function trace entry - function address and parent function addres:
49 struct trace_entry ent;
51 unsigned long parent_ip;
54 /* Function return entry */
55 struct ftrace_ret_entry {
56 struct trace_entry ent;
58 unsigned long parent_ip;
59 unsigned long long calltime;
60 unsigned long long rettime;
62 extern struct tracer boot_tracer;
65 * Context switch trace entry - which task (and prio) we switched from/to:
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;
79 * Special (free-form) trace entry:
81 struct special_entry {
82 struct trace_entry ent;
92 #define FTRACE_STACK_ENTRIES 8
95 struct trace_entry ent;
96 unsigned long caller[FTRACE_STACK_ENTRIES];
100 * ftrace_printk entry:
103 struct trace_entry ent;
108 #define TRACE_OLD_SIZE 88
110 struct trace_field_cont {
112 /* Temporary till we get rid of this completely */
113 char buf[TRACE_OLD_SIZE - 1];
116 struct trace_mmiotrace_rw {
117 struct trace_entry ent;
118 struct mmiotrace_rw rw;
121 struct trace_mmiotrace_map {
122 struct trace_entry ent;
123 struct mmiotrace_map map;
127 struct trace_entry ent;
128 struct boot_trace initcall;
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
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,
150 #define TRACE_BUF_SIZE 1024
153 * The CPU trace array - it consists of thousands of trace entries
154 * plus some other descriptor data: (for example which task started
157 struct trace_array_cpu {
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;
168 unsigned long policy;
169 unsigned long rt_priority;
170 cycle_t preempt_timestamp;
173 char comm[TASK_COMM_LEN];
176 struct trace_iterator;
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:
184 struct ring_buffer *buffer;
185 unsigned long entries;
188 struct task_struct *waiter;
189 struct trace_array_cpu *data[NR_CPUS];
192 #define FTRACE_CMP_TYPE(var, type) \
193 __builtin_types_compatible_p(typeof(var), type *)
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); \
203 /* Will cause compile errors if type is not found. */
204 extern void __ftrace_bad_type(void);
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:
211 * IF_ASSIGN(var, ent, type, id);
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.
217 * If the type can have more than one id, then use zero.
219 #define trace_assign_type(var, ent) \
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, \
229 IF_ASSIGN(var, ent, struct trace_mmiotrace_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(); \
236 /* Return values for print_line callback */
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 */
244 * A specific tracer, represented by methods that operate on a trace array:
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);
262 enum print_line_t (*print_line)(struct trace_iterator *iter);
268 unsigned char buffer[PAGE_SIZE];
270 unsigned int readpos;
274 * Trace iterator - used by printout routines who present trace
275 * results to users and which routines might sleep, etc:
277 struct trace_iterator {
278 struct trace_array *tr;
279 struct tracer *trace;
281 struct ring_buffer_iter *buffer_iter[NR_CPUS];
283 /* The below is zeroed out in pipe_read */
284 struct trace_seq seq;
285 struct trace_entry *ent;
289 unsigned long iter_flags;
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);
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,
309 void ftrace(struct trace_array *tr,
310 struct trace_array_cpu *data,
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);
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,
330 unsigned long arg3, int pc);
331 void trace_function(struct trace_array *tr,
332 struct trace_array_cpu *data,
334 unsigned long parent_ip,
335 unsigned long flags, int pc);
337 trace_function_return(struct ftrace_retfunc *trace);
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);
347 extern unsigned long nsecs_to_usecs(unsigned long nsecs);
349 extern unsigned long tracing_max_latency;
350 extern unsigned long tracing_thresh;
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);
356 extern cycle_t ftrace_now(int cpu);
358 #ifdef CONFIG_FUNCTION_TRACER
359 void tracing_start_function_trace(void);
360 void tracing_stop_function_trace(void);
362 # define tracing_start_function_trace() do { } while (0)
363 # define tracing_stop_function_trace() do { } while (0)
366 #ifdef CONFIG_CONTEXT_SWITCH_TRACER
368 (*tracer_switch_func_t)(void *private,
370 struct task_struct *prev,
371 struct task_struct *next);
373 struct tracer_switch_ops {
374 tracer_switch_func_t func;
376 struct tracer_switch_ops *next;
379 #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
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);
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 */
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);
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,
416 extern long ns2usecs(cycle_t nsec);
417 extern int trace_vprintk(unsigned long ip, const char *fmt, va_list args);
419 extern unsigned long trace_flags;
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);
425 static inline enum print_line_t
426 print_return_function(struct trace_iterator *iter)
428 return TRACE_TYPE_UNHANDLED;
433 * trace_iterator_flags is an enumeration that defines bit
434 * positions into trace_flags that controls the output.
436 * NOTE: These bits must match the trace_options array in
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,
455 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
456 * control the output of kernel symbols.
458 #define TRACE_ITER_SYM_MASK \
459 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
461 extern struct tracer nop_trace;
464 * ftrace_preempt_disable - disable preemption scheduler safe
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.
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.
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.
482 static inline int ftrace_preempt_disable(void)
486 resched = need_resched();
487 preempt_disable_notrace();
493 * ftrace_preempt_enable - enable preemption scheduler safe
494 * @resched: the return value from ftrace_preempt_disable
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.
503 static inline void ftrace_preempt_enable(int resched)
506 preempt_enable_no_resched_notrace();
508 preempt_enable_notrace();
511 #endif /* _LINUX_KERNEL_TRACE_H */