2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Based on code from the latency_tracer, that is:
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 William Lee Irwin III
12 #include <linux/debugfs.h>
13 #include <linux/uaccess.h>
14 #include <linux/ftrace.h>
19 static struct trace_array *func_trace;
21 static void start_function_trace(struct trace_array *tr)
25 tracing_reset_online_cpus(tr);
28 tracing_start_cmdline_record();
29 tracing_start_function_trace();
32 static void stop_function_trace(struct trace_array *tr)
34 tracing_stop_function_trace();
35 tracing_stop_cmdline_record();
38 static int function_trace_init(struct trace_array *tr)
40 start_function_trace(tr);
44 static void function_trace_reset(struct trace_array *tr)
46 stop_function_trace(tr);
49 static void function_trace_start(struct trace_array *tr)
51 tracing_reset_online_cpus(tr);
55 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
57 struct trace_array *tr = func_trace;
58 struct trace_array_cpu *data;
64 if (unlikely(!ftrace_function_enabled))
68 resched = ftrace_preempt_disable();
69 local_save_flags(flags);
70 cpu = raw_smp_processor_id();
72 disabled = atomic_inc_return(&data->disabled);
74 if (likely(disabled == 1))
75 trace_function(tr, data, ip, parent_ip, flags, pc);
77 atomic_dec(&data->disabled);
78 ftrace_preempt_enable(resched);
82 function_trace_call(unsigned long ip, unsigned long parent_ip)
84 struct trace_array *tr = func_trace;
85 struct trace_array_cpu *data;
91 if (unlikely(!ftrace_function_enabled))
95 * Need to use raw, since this must be called before the
96 * recursive protection is performed.
98 local_irq_save(flags);
99 cpu = raw_smp_processor_id();
100 data = tr->data[cpu];
101 disabled = atomic_inc_return(&data->disabled);
103 if (likely(disabled == 1)) {
104 pc = preempt_count();
105 trace_function(tr, data, ip, parent_ip, flags, pc);
108 atomic_dec(&data->disabled);
109 local_irq_restore(flags);
113 function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
115 struct trace_array *tr = func_trace;
116 struct trace_array_cpu *data;
122 if (unlikely(!ftrace_function_enabled))
126 * Need to use raw, since this must be called before the
127 * recursive protection is performed.
129 local_irq_save(flags);
130 cpu = raw_smp_processor_id();
131 data = tr->data[cpu];
132 disabled = atomic_inc_return(&data->disabled);
134 if (likely(disabled == 1)) {
135 pc = preempt_count();
138 * __ftrace_trace_stack,
140 * function_stack_trace_call
144 __trace_stack(tr, data, flags, 5, pc);
147 atomic_dec(&data->disabled);
148 local_irq_restore(flags);
152 static struct ftrace_ops trace_ops __read_mostly =
154 .func = function_trace_call,
157 void tracing_start_function_trace(void)
159 ftrace_function_enabled = 0;
161 if (trace_flags & TRACE_ITER_PREEMPTONLY)
162 trace_ops.func = function_trace_call_preempt_only;
164 trace_ops.func = function_trace_call;
166 register_ftrace_function(&trace_ops);
167 ftrace_function_enabled = 1;
170 void tracing_stop_function_trace(void)
172 ftrace_function_enabled = 0;
173 unregister_ftrace_function(&trace_ops);
175 static struct ftrace_ops trace_stack_ops __read_mostly =
177 .func = function_stack_trace_call,
180 /* Our two options */
182 TRACE_FUNC_OPT_STACK = 0x1,
185 static struct tracer_opt func_opts[] = {
186 #ifdef CONFIG_STACKTRACE
187 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
189 { } /* Always set a last empty entry */
192 static struct tracer_flags func_flags = {
193 .val = 0, /* By default: all flags disabled */
197 static int func_set_flag(u32 old_flags, u32 bit, int set)
199 if (bit == TRACE_FUNC_OPT_STACK) {
200 /* do nothing if already set */
201 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
205 register_ftrace_function(&trace_stack_ops);
207 unregister_ftrace_function(&trace_stack_ops);
215 static struct tracer function_trace __read_mostly =
218 .init = function_trace_init,
219 .reset = function_trace_reset,
220 .start = function_trace_start,
221 .flags = &func_flags,
222 .set_flag = func_set_flag,
223 #ifdef CONFIG_FTRACE_SELFTEST
224 .selftest = trace_selftest_startup_function,
228 static __init int init_function_trace(void)
230 return register_tracer(&function_trace);
233 device_initcall(init_function_trace);