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 void start_function_trace(struct trace_array *tr)
31 tracing_reset_online_cpus(tr);
34 tracing_start_cmdline_record();
35 tracing_start_function_trace();
38 static void stop_function_trace(struct trace_array *tr)
40 tracing_stop_function_trace();
41 tracing_stop_cmdline_record();
44 static int function_trace_init(struct trace_array *tr)
46 start_function_trace(tr);
50 static void function_trace_reset(struct trace_array *tr)
52 stop_function_trace(tr);
55 static void function_trace_start(struct trace_array *tr)
57 tracing_reset_online_cpus(tr);
61 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
63 struct trace_array *tr = func_trace;
64 struct trace_array_cpu *data;
70 if (unlikely(!ftrace_function_enabled))
74 resched = ftrace_preempt_disable();
75 local_save_flags(flags);
76 cpu = raw_smp_processor_id();
78 disabled = atomic_inc_return(&data->disabled);
80 if (likely(disabled == 1))
81 trace_function(tr, ip, parent_ip, flags, pc);
83 atomic_dec(&data->disabled);
84 ftrace_preempt_enable(resched);
88 function_trace_call(unsigned long ip, unsigned long parent_ip)
90 struct trace_array *tr = func_trace;
91 struct trace_array_cpu *data;
97 if (unlikely(!ftrace_function_enabled))
101 * Need to use raw, since this must be called before the
102 * recursive protection is performed.
104 local_irq_save(flags);
105 cpu = raw_smp_processor_id();
106 data = tr->data[cpu];
107 disabled = atomic_inc_return(&data->disabled);
109 if (likely(disabled == 1)) {
110 pc = preempt_count();
111 trace_function(tr, ip, parent_ip, flags, pc);
114 atomic_dec(&data->disabled);
115 local_irq_restore(flags);
119 function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
121 struct trace_array *tr = func_trace;
122 struct trace_array_cpu *data;
128 if (unlikely(!ftrace_function_enabled))
132 * Need to use raw, since this must be called before the
133 * recursive protection is performed.
135 local_irq_save(flags);
136 cpu = raw_smp_processor_id();
137 data = tr->data[cpu];
138 disabled = atomic_inc_return(&data->disabled);
140 if (likely(disabled == 1)) {
141 pc = preempt_count();
142 trace_function(tr, ip, parent_ip, flags, pc);
145 * __ftrace_trace_stack,
147 * function_stack_trace_call
151 __trace_stack(tr, flags, 5, pc);
154 atomic_dec(&data->disabled);
155 local_irq_restore(flags);
159 static struct ftrace_ops trace_ops __read_mostly =
161 .func = function_trace_call,
164 static struct ftrace_ops trace_stack_ops __read_mostly =
166 .func = function_stack_trace_call,
169 /* Our two options */
171 TRACE_FUNC_OPT_STACK = 0x1,
174 static struct tracer_opt func_opts[] = {
175 #ifdef CONFIG_STACKTRACE
176 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
178 { } /* Always set a last empty entry */
181 static struct tracer_flags func_flags = {
182 .val = 0, /* By default: all flags disabled */
186 static void tracing_start_function_trace(void)
188 ftrace_function_enabled = 0;
190 if (trace_flags & TRACE_ITER_PREEMPTONLY)
191 trace_ops.func = function_trace_call_preempt_only;
193 trace_ops.func = function_trace_call;
195 if (func_flags.val & TRACE_FUNC_OPT_STACK)
196 register_ftrace_function(&trace_stack_ops);
198 register_ftrace_function(&trace_ops);
200 ftrace_function_enabled = 1;
203 static void tracing_stop_function_trace(void)
205 ftrace_function_enabled = 0;
206 /* OK if they are not registered */
207 unregister_ftrace_function(&trace_stack_ops);
208 unregister_ftrace_function(&trace_ops);
211 static int func_set_flag(u32 old_flags, u32 bit, int set)
213 if (bit == TRACE_FUNC_OPT_STACK) {
214 /* do nothing if already set */
215 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
219 unregister_ftrace_function(&trace_ops);
220 register_ftrace_function(&trace_stack_ops);
222 unregister_ftrace_function(&trace_stack_ops);
223 register_ftrace_function(&trace_ops);
232 static struct tracer function_trace __read_mostly =
235 .init = function_trace_init,
236 .reset = function_trace_reset,
237 .start = function_trace_start,
238 .flags = &func_flags,
239 .set_flag = func_set_flag,
240 #ifdef CONFIG_FTRACE_SELFTEST
241 .selftest = trace_selftest_startup_function,
245 static __init int init_function_trace(void)
247 return register_tracer(&function_trace);
250 device_initcall(init_function_trace);