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 /* function tracing enabled */
20 static int ftrace_function_enabled;
22 static struct trace_array *func_trace;
24 static void tracing_start_function_trace(void);
25 static void tracing_stop_function_trace(void);
27 static int function_trace_init(struct trace_array *tr)
33 tracing_start_cmdline_record();
34 tracing_start_function_trace();
38 static void function_trace_reset(struct trace_array *tr)
40 tracing_stop_function_trace();
41 tracing_stop_cmdline_record();
44 static void function_trace_start(struct trace_array *tr)
46 tracing_reset_online_cpus(tr);
50 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
52 struct trace_array *tr = func_trace;
53 struct trace_array_cpu *data;
59 if (unlikely(!ftrace_function_enabled))
63 resched = ftrace_preempt_disable();
64 local_save_flags(flags);
65 cpu = raw_smp_processor_id();
67 disabled = atomic_inc_return(&data->disabled);
69 if (likely(disabled == 1))
70 trace_function(tr, ip, parent_ip, flags, pc);
72 atomic_dec(&data->disabled);
73 ftrace_preempt_enable(resched);
77 function_trace_call(unsigned long ip, unsigned long parent_ip)
79 struct trace_array *tr = func_trace;
80 struct trace_array_cpu *data;
86 if (unlikely(!ftrace_function_enabled))
90 * Need to use raw, since this must be called before the
91 * recursive protection is performed.
93 local_irq_save(flags);
94 cpu = raw_smp_processor_id();
96 disabled = atomic_inc_return(&data->disabled);
98 if (likely(disabled == 1)) {
100 trace_function(tr, ip, parent_ip, flags, pc);
103 atomic_dec(&data->disabled);
104 local_irq_restore(flags);
108 function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
110 struct trace_array *tr = func_trace;
111 struct trace_array_cpu *data;
117 if (unlikely(!ftrace_function_enabled))
121 * Need to use raw, since this must be called before the
122 * recursive protection is performed.
124 local_irq_save(flags);
125 cpu = raw_smp_processor_id();
126 data = tr->data[cpu];
127 disabled = atomic_inc_return(&data->disabled);
129 if (likely(disabled == 1)) {
130 pc = preempt_count();
131 trace_function(tr, ip, parent_ip, flags, pc);
134 * __ftrace_trace_stack,
136 * function_stack_trace_call
140 __trace_stack(tr, flags, 5, pc);
143 atomic_dec(&data->disabled);
144 local_irq_restore(flags);
148 static struct ftrace_ops trace_ops __read_mostly =
150 .func = function_trace_call,
153 static struct ftrace_ops trace_stack_ops __read_mostly =
155 .func = function_stack_trace_call,
158 /* Our two options */
160 TRACE_FUNC_OPT_STACK = 0x1,
163 static struct tracer_opt func_opts[] = {
164 #ifdef CONFIG_STACKTRACE
165 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
167 { } /* Always set a last empty entry */
170 static struct tracer_flags func_flags = {
171 .val = 0, /* By default: all flags disabled */
175 static void tracing_start_function_trace(void)
177 ftrace_function_enabled = 0;
179 if (trace_flags & TRACE_ITER_PREEMPTONLY)
180 trace_ops.func = function_trace_call_preempt_only;
182 trace_ops.func = function_trace_call;
184 if (func_flags.val & TRACE_FUNC_OPT_STACK)
185 register_ftrace_function(&trace_stack_ops);
187 register_ftrace_function(&trace_ops);
189 ftrace_function_enabled = 1;
192 static void tracing_stop_function_trace(void)
194 ftrace_function_enabled = 0;
195 /* OK if they are not registered */
196 unregister_ftrace_function(&trace_stack_ops);
197 unregister_ftrace_function(&trace_ops);
200 static int func_set_flag(u32 old_flags, u32 bit, int set)
202 if (bit == TRACE_FUNC_OPT_STACK) {
203 /* do nothing if already set */
204 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
208 unregister_ftrace_function(&trace_ops);
209 register_ftrace_function(&trace_stack_ops);
211 unregister_ftrace_function(&trace_stack_ops);
212 register_ftrace_function(&trace_ops);
221 static struct tracer function_trace __read_mostly =
224 .init = function_trace_init,
225 .reset = function_trace_reset,
226 .start = function_trace_start,
227 .flags = &func_flags,
228 .set_flag = func_set_flag,
229 #ifdef CONFIG_FTRACE_SELFTEST
230 .selftest = trace_selftest_startup_function,
234 static __init int init_function_trace(void)
236 return register_tracer(&function_trace);
239 device_initcall(init_function_trace);