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/tracepoint.h>
11 #include <linux/ftrace.h>
12 #include <trace/boot.h>
13 #include <linux/kmemtrace.h>
15 #include <linux/trace_seq.h>
16 #include <linux/ftrace_event.h>
19 __TRACE_FIRST_TYPE = 0,
44 enum kmemtrace_type_id {
45 KMEMTRACE_TYPE_KMALLOC = 0, /* kmalloc() or kfree(). */
46 KMEMTRACE_TYPE_CACHE, /* kmem_cache_*(). */
47 KMEMTRACE_TYPE_PAGES, /* __get_free_pages() and friends. */
50 extern struct tracer boot_tracer;
53 #define __field(type, item) type item;
56 #define __field_struct(type, item) __field(type, item)
59 #define __field_desc(type, container, item)
62 #define __array(type, item, size) type item[size];
65 #define __array_desc(type, container, item, size)
67 #undef __dynamic_array
68 #define __dynamic_array(type, item) type item[];
71 #define F_STRUCT(args...) args
74 #define FTRACE_ENTRY(name, struct_name, id, tstruct, print) \
75 struct struct_name { \
76 struct trace_entry ent; \
81 #define TP_ARGS(args...) args
83 #undef FTRACE_ENTRY_DUP
84 #define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk)
86 #include "trace_entries.h"
89 * syscalls are special, and need special handling, this is why
90 * they are not included in trace_entries.h
92 struct syscall_trace_enter {
93 struct trace_entry ent;
98 struct syscall_trace_exit {
99 struct trace_entry ent;
104 struct kprobe_trace_entry {
105 struct trace_entry ent;
108 unsigned long args[];
111 #define SIZEOF_KPROBE_TRACE_ENTRY(n) \
112 (offsetof(struct kprobe_trace_entry, args) + \
113 (sizeof(unsigned long) * (n)))
115 struct kretprobe_trace_entry {
116 struct trace_entry ent;
118 unsigned long ret_ip;
120 unsigned long args[];
123 #define SIZEOF_KRETPROBE_TRACE_ENTRY(n) \
124 (offsetof(struct kretprobe_trace_entry, args) + \
125 (sizeof(unsigned long) * (n)))
128 * trace_flag_type is an enumeration that holds different
129 * states when a trace occurs. These are:
130 * IRQS_OFF - interrupts were disabled
131 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
132 * NEED_RESCHED - reschedule is requested
133 * HARDIRQ - inside an interrupt handler
134 * SOFTIRQ - inside a softirq handler
136 enum trace_flag_type {
137 TRACE_FLAG_IRQS_OFF = 0x01,
138 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
139 TRACE_FLAG_NEED_RESCHED = 0x04,
140 TRACE_FLAG_HARDIRQ = 0x08,
141 TRACE_FLAG_SOFTIRQ = 0x10,
144 #define TRACE_BUF_SIZE 1024
147 * The CPU trace array - it consists of thousands of trace entries
148 * plus some other descriptor data: (for example which task started
151 struct trace_array_cpu {
153 void *buffer_page; /* ring buffer spare */
155 unsigned long saved_latency;
156 unsigned long critical_start;
157 unsigned long critical_end;
158 unsigned long critical_sequence;
160 unsigned long policy;
161 unsigned long rt_priority;
162 unsigned long skipped_entries;
163 cycle_t preempt_timestamp;
166 char comm[TASK_COMM_LEN];
170 * The trace array - an array of per-CPU trace arrays. This is the
171 * highest level data structure that individual tracers deal with.
172 * They have on/off state as well:
175 struct ring_buffer *buffer;
176 unsigned long entries;
179 struct task_struct *waiter;
180 struct trace_array_cpu *data[NR_CPUS];
183 #define FTRACE_CMP_TYPE(var, type) \
184 __builtin_types_compatible_p(typeof(var), type *)
187 #define IF_ASSIGN(var, entry, etype, id) \
188 if (FTRACE_CMP_TYPE(var, etype)) { \
189 var = (typeof(var))(entry); \
190 WARN_ON(id && (entry)->type != id); \
194 /* Will cause compile errors if type is not found. */
195 extern void __ftrace_bad_type(void);
198 * The trace_assign_type is a verifier that the entry type is
199 * the same as the type being assigned. To add new types simply
200 * add a line with the following format:
202 * IF_ASSIGN(var, ent, type, id);
204 * Where "type" is the trace type that includes the trace_entry
205 * as the "ent" item. And "id" is the trace identifier that is
206 * used in the trace_type enum.
208 * If the type can have more than one id, then use zero.
210 #define trace_assign_type(var, ent) \
212 IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
213 IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
214 IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
215 IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
216 IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
217 IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT); \
218 IF_ASSIGN(var, ent, struct special_entry, 0); \
219 IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
221 IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
223 IF_ASSIGN(var, ent, struct trace_boot_call, TRACE_BOOT_CALL);\
224 IF_ASSIGN(var, ent, struct trace_boot_ret, TRACE_BOOT_RET);\
225 IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
226 IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry, \
228 IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \
230 IF_ASSIGN(var, ent, struct hw_branch_entry, TRACE_HW_BRANCHES);\
231 IF_ASSIGN(var, ent, struct kmemtrace_alloc_entry, \
233 IF_ASSIGN(var, ent, struct kmemtrace_free_entry, \
235 __ftrace_bad_type(); \
239 * An option specific to a tracer. This is a boolean value.
240 * The bit is the bit index that sets its value on the
241 * flags value in struct tracer_flags.
244 const char *name; /* Will appear on the trace_options file */
245 u32 bit; /* Mask assigned in val field in tracer_flags */
249 * The set of specific options for a tracer. Your tracer
250 * have to set the initial value of the flags val.
252 struct tracer_flags {
254 struct tracer_opt *opts;
257 /* Makes more easy to define a tracer opt */
258 #define TRACER_OPT(s, b) .name = #s, .bit = b
262 * struct tracer - a specific tracer and its callbacks to interact with debugfs
263 * @name: the name chosen to select it on the available_tracers file
264 * @init: called when one switches to this tracer (echo name > current_tracer)
265 * @reset: called when one switches to another tracer
266 * @start: called when tracing is unpaused (echo 1 > tracing_enabled)
267 * @stop: called when tracing is paused (echo 0 > tracing_enabled)
268 * @open: called when the trace file is opened
269 * @pipe_open: called when the trace_pipe file is opened
270 * @wait_pipe: override how the user waits for traces on trace_pipe
271 * @close: called when the trace file is released
272 * @read: override the default read callback on trace_pipe
273 * @splice_read: override the default splice_read callback on trace_pipe
274 * @selftest: selftest to run on boot (see trace_selftest.c)
275 * @print_headers: override the first lines that describe your columns
276 * @print_line: callback that prints a trace
277 * @set_flag: signals one of your private flags changed (trace_options file)
278 * @flags: your private flags
282 int (*init)(struct trace_array *tr);
283 void (*reset)(struct trace_array *tr);
284 void (*start)(struct trace_array *tr);
285 void (*stop)(struct trace_array *tr);
286 void (*open)(struct trace_iterator *iter);
287 void (*pipe_open)(struct trace_iterator *iter);
288 void (*wait_pipe)(struct trace_iterator *iter);
289 void (*close)(struct trace_iterator *iter);
290 ssize_t (*read)(struct trace_iterator *iter,
291 struct file *filp, char __user *ubuf,
292 size_t cnt, loff_t *ppos);
293 ssize_t (*splice_read)(struct trace_iterator *iter,
296 struct pipe_inode_info *pipe,
299 #ifdef CONFIG_FTRACE_STARTUP_TEST
300 int (*selftest)(struct tracer *trace,
301 struct trace_array *tr);
303 void (*print_header)(struct seq_file *m);
304 enum print_line_t (*print_line)(struct trace_iterator *iter);
305 /* If you handled the flag setting, return 0 */
306 int (*set_flag)(u32 old_flags, u32 bit, int set);
309 struct tracer_flags *flags;
313 #define TRACE_PIPE_ALL_CPU -1
315 int tracer_init(struct tracer *t, struct trace_array *tr);
316 int tracing_is_enabled(void);
317 void trace_wake_up(void);
318 void tracing_reset(struct trace_array *tr, int cpu);
319 void tracing_reset_online_cpus(struct trace_array *tr);
320 void tracing_reset_current(int cpu);
321 void tracing_reset_current_online_cpus(void);
322 int tracing_open_generic(struct inode *inode, struct file *filp);
323 struct dentry *trace_create_file(const char *name,
325 struct dentry *parent,
327 const struct file_operations *fops);
329 struct dentry *tracing_init_dentry(void);
330 void init_tracer_sysprof_debugfs(struct dentry *d_tracer);
332 struct ring_buffer_event;
334 struct ring_buffer_event *
335 trace_buffer_lock_reserve(struct ring_buffer *buffer,
340 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
341 struct ring_buffer_event *event,
342 unsigned long flags, int pc);
344 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
345 struct trace_array_cpu *data);
347 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
348 int *ent_cpu, u64 *ent_ts);
350 void default_wait_pipe(struct trace_iterator *iter);
351 void poll_wait_pipe(struct trace_iterator *iter);
353 void ftrace(struct trace_array *tr,
354 struct trace_array_cpu *data,
356 unsigned long parent_ip,
357 unsigned long flags, int pc);
358 void tracing_sched_switch_trace(struct trace_array *tr,
359 struct task_struct *prev,
360 struct task_struct *next,
361 unsigned long flags, int pc);
363 void tracing_sched_wakeup_trace(struct trace_array *tr,
364 struct task_struct *wakee,
365 struct task_struct *cur,
366 unsigned long flags, int pc);
367 void trace_special(struct trace_array *tr,
368 struct trace_array_cpu *data,
371 unsigned long arg3, int pc);
372 void trace_function(struct trace_array *tr,
374 unsigned long parent_ip,
375 unsigned long flags, int pc);
377 void trace_graph_return(struct ftrace_graph_ret *trace);
378 int trace_graph_entry(struct ftrace_graph_ent *trace);
379 void set_graph_array(struct trace_array *tr);
381 void tracing_start_cmdline_record(void);
382 void tracing_stop_cmdline_record(void);
383 void tracing_sched_switch_assign_trace(struct trace_array *tr);
384 void tracing_stop_sched_switch_record(void);
385 void tracing_start_sched_switch_record(void);
386 int register_tracer(struct tracer *type);
387 void unregister_tracer(struct tracer *type);
388 int is_tracing_stopped(void);
390 extern unsigned long nsecs_to_usecs(unsigned long nsecs);
392 #ifdef CONFIG_TRACER_MAX_TRACE
393 extern unsigned long tracing_max_latency;
394 extern unsigned long tracing_thresh;
396 void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
397 void update_max_tr_single(struct trace_array *tr,
398 struct task_struct *tsk, int cpu);
399 #endif /* CONFIG_TRACER_MAX_TRACE */
401 #ifdef CONFIG_STACKTRACE
402 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
405 void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
408 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
411 static inline void ftrace_trace_stack(struct trace_array *tr,
412 unsigned long flags, int skip, int pc)
416 static inline void ftrace_trace_userstack(struct trace_array *tr,
417 unsigned long flags, int pc)
421 static inline void __trace_stack(struct trace_array *tr, unsigned long flags,
425 #endif /* CONFIG_STACKTRACE */
427 extern cycle_t ftrace_now(int cpu);
429 extern void trace_find_cmdline(int pid, char comm[]);
431 #ifdef CONFIG_DYNAMIC_FTRACE
432 extern unsigned long ftrace_update_tot_cnt;
433 #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
434 extern int DYN_FTRACE_TEST_NAME(void);
437 extern int ring_buffer_expanded;
438 extern bool tracing_selftest_disabled;
439 DECLARE_PER_CPU(local_t, ftrace_cpu_disabled);
441 #ifdef CONFIG_FTRACE_STARTUP_TEST
442 extern int trace_selftest_startup_function(struct tracer *trace,
443 struct trace_array *tr);
444 extern int trace_selftest_startup_function_graph(struct tracer *trace,
445 struct trace_array *tr);
446 extern int trace_selftest_startup_irqsoff(struct tracer *trace,
447 struct trace_array *tr);
448 extern int trace_selftest_startup_preemptoff(struct tracer *trace,
449 struct trace_array *tr);
450 extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
451 struct trace_array *tr);
452 extern int trace_selftest_startup_wakeup(struct tracer *trace,
453 struct trace_array *tr);
454 extern int trace_selftest_startup_nop(struct tracer *trace,
455 struct trace_array *tr);
456 extern int trace_selftest_startup_sched_switch(struct tracer *trace,
457 struct trace_array *tr);
458 extern int trace_selftest_startup_sysprof(struct tracer *trace,
459 struct trace_array *tr);
460 extern int trace_selftest_startup_branch(struct tracer *trace,
461 struct trace_array *tr);
462 extern int trace_selftest_startup_hw_branches(struct tracer *trace,
463 struct trace_array *tr);
464 #endif /* CONFIG_FTRACE_STARTUP_TEST */
466 extern void *head_page(struct trace_array_cpu *data);
467 extern unsigned long long ns2usecs(cycle_t nsec);
469 trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
471 trace_vprintk(unsigned long ip, const char *fmt, va_list args);
473 trace_array_vprintk(struct trace_array *tr,
474 unsigned long ip, const char *fmt, va_list args);
475 int trace_array_printk(struct trace_array *tr,
476 unsigned long ip, const char *fmt, ...);
478 extern unsigned long trace_flags;
480 extern int trace_clock_id;
482 /* Standard output formatting function used for function return traces */
483 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
484 extern enum print_line_t print_graph_function(struct trace_iterator *iter);
485 extern enum print_line_t
486 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
488 #ifdef CONFIG_DYNAMIC_FTRACE
489 /* TODO: make this variable */
490 #define FTRACE_GRAPH_MAX_FUNCS 32
491 extern int ftrace_graph_count;
492 extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS];
494 static inline int ftrace_graph_addr(unsigned long addr)
498 if (!ftrace_graph_count || test_tsk_trace_graph(current))
501 for (i = 0; i < ftrace_graph_count; i++) {
502 if (addr == ftrace_graph_funcs[i])
509 static inline int ftrace_trace_addr(unsigned long addr)
513 static inline int ftrace_graph_addr(unsigned long addr)
517 #endif /* CONFIG_DYNAMIC_FTRACE */
518 #else /* CONFIG_FUNCTION_GRAPH_TRACER */
519 static inline enum print_line_t
520 print_graph_function(struct trace_iterator *iter)
522 return TRACE_TYPE_UNHANDLED;
524 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
526 extern struct pid *ftrace_pid_trace;
528 #ifdef CONFIG_FUNCTION_TRACER
529 static inline int ftrace_trace_task(struct task_struct *task)
531 if (!ftrace_pid_trace)
534 return test_tsk_trace_trace(task);
537 static inline int ftrace_trace_task(struct task_struct *task)
544 * struct trace_parser - servers for reading the user input separated by spaces
545 * @cont: set if the input is not complete - no final space char was found
546 * @buffer: holds the parsed user input
547 * @idx: user input lenght
550 struct trace_parser {
557 static inline bool trace_parser_loaded(struct trace_parser *parser)
559 return (parser->idx != 0);
562 static inline bool trace_parser_cont(struct trace_parser *parser)
567 static inline void trace_parser_clear(struct trace_parser *parser)
569 parser->cont = false;
573 extern int trace_parser_get_init(struct trace_parser *parser, int size);
574 extern void trace_parser_put(struct trace_parser *parser);
575 extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
576 size_t cnt, loff_t *ppos);
579 * trace_iterator_flags is an enumeration that defines bit
580 * positions into trace_flags that controls the output.
582 * NOTE: These bits must match the trace_options array in
585 enum trace_iterator_flags {
586 TRACE_ITER_PRINT_PARENT = 0x01,
587 TRACE_ITER_SYM_OFFSET = 0x02,
588 TRACE_ITER_SYM_ADDR = 0x04,
589 TRACE_ITER_VERBOSE = 0x08,
590 TRACE_ITER_RAW = 0x10,
591 TRACE_ITER_HEX = 0x20,
592 TRACE_ITER_BIN = 0x40,
593 TRACE_ITER_BLOCK = 0x80,
594 TRACE_ITER_STACKTRACE = 0x100,
595 TRACE_ITER_SCHED_TREE = 0x200,
596 TRACE_ITER_PRINTK = 0x400,
597 TRACE_ITER_PREEMPTONLY = 0x800,
598 TRACE_ITER_BRANCH = 0x1000,
599 TRACE_ITER_ANNOTATE = 0x2000,
600 TRACE_ITER_USERSTACKTRACE = 0x4000,
601 TRACE_ITER_SYM_USEROBJ = 0x8000,
602 TRACE_ITER_PRINTK_MSGONLY = 0x10000,
603 TRACE_ITER_CONTEXT_INFO = 0x20000, /* Print pid/cpu/time */
604 TRACE_ITER_LATENCY_FMT = 0x40000,
605 TRACE_ITER_SLEEP_TIME = 0x80000,
606 TRACE_ITER_GRAPH_TIME = 0x100000,
610 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
611 * control the output of kernel symbols.
613 #define TRACE_ITER_SYM_MASK \
614 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
616 extern struct tracer nop_trace;
619 * ftrace_preempt_disable - disable preemption scheduler safe
621 * When tracing can happen inside the scheduler, there exists
622 * cases that the tracing might happen before the need_resched
623 * flag is checked. If this happens and the tracer calls
624 * preempt_enable (after a disable), a schedule might take place
625 * causing an infinite recursion.
627 * To prevent this, we read the need_resched flag before
628 * disabling preemption. When we want to enable preemption we
629 * check the flag, if it is set, then we call preempt_enable_no_resched.
630 * Otherwise, we call preempt_enable.
632 * The rational for doing the above is that if need_resched is set
633 * and we have yet to reschedule, we are either in an atomic location
634 * (where we do not need to check for scheduling) or we are inside
635 * the scheduler and do not want to resched.
637 static inline int ftrace_preempt_disable(void)
641 resched = need_resched();
642 preempt_disable_notrace();
648 * ftrace_preempt_enable - enable preemption scheduler safe
649 * @resched: the return value from ftrace_preempt_disable
651 * This is a scheduler safe way to enable preemption and not miss
652 * any preemption checks. The disabled saved the state of preemption.
653 * If resched is set, then we are either inside an atomic or
654 * are inside the scheduler (we would have already scheduled
655 * otherwise). In this case, we do not want to call normal
656 * preempt_enable, but preempt_enable_no_resched instead.
658 static inline void ftrace_preempt_enable(int resched)
661 preempt_enable_no_resched_notrace();
663 preempt_enable_notrace();
666 #ifdef CONFIG_BRANCH_TRACER
667 extern int enable_branch_tracing(struct trace_array *tr);
668 extern void disable_branch_tracing(void);
669 static inline int trace_branch_enable(struct trace_array *tr)
671 if (trace_flags & TRACE_ITER_BRANCH)
672 return enable_branch_tracing(tr);
675 static inline void trace_branch_disable(void)
677 /* due to races, always disable */
678 disable_branch_tracing();
681 static inline int trace_branch_enable(struct trace_array *tr)
685 static inline void trace_branch_disable(void)
688 #endif /* CONFIG_BRANCH_TRACER */
690 /* set ring buffers to default size if not already done so */
691 int tracing_update_buffers(void);
693 /* trace event type bit fields, not numeric */
695 TRACE_EVENT_TYPE_PRINTF = 1,
696 TRACE_EVENT_TYPE_RAW = 2,
699 struct ftrace_event_field {
700 struct list_head link;
709 struct event_filter {
711 struct filter_pred **preds;
716 struct event_subsystem {
717 struct list_head list;
719 struct dentry *entry;
720 struct event_filter *filter;
726 typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event,
732 char str_val[MAX_FILTER_STR_VAL];
741 extern void print_event_filter(struct ftrace_event_call *call,
742 struct trace_seq *s);
743 extern int apply_event_filter(struct ftrace_event_call *call,
744 char *filter_string);
745 extern int apply_subsystem_event_filter(struct event_subsystem *system,
746 char *filter_string);
747 extern void print_subsystem_event_filter(struct event_subsystem *system,
748 struct trace_seq *s);
749 extern int filter_assign_type(const char *type);
752 filter_check_discard(struct ftrace_event_call *call, void *rec,
753 struct ring_buffer *buffer,
754 struct ring_buffer_event *event)
756 if (unlikely(call->filter_active) && !filter_match_preds(call, rec)) {
757 ring_buffer_discard_commit(buffer, event);
764 extern struct mutex event_mutex;
765 extern struct list_head ftrace_events;
767 extern const char *__start___trace_bprintk_fmt[];
768 extern const char *__stop___trace_bprintk_fmt[];
771 #define FTRACE_ENTRY(call, struct_name, id, tstruct, print) \
772 extern struct ftrace_event_call event_##call;
773 #undef FTRACE_ENTRY_DUP
774 #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print) \
775 FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print))
776 #include "trace_entries.h"
778 #endif /* _LINUX_KERNEL_TRACE_H */