blob: 36bf9568ccd9d7e2ab29a815be5659cf1d53e92c [file] [log] [blame]
Steven Rostedt1b29b012008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Based on code from the latency_tracer, that is:
8 *
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 William Lee Irwin III
11 */
Steven Rostedt1b29b012008-05-12 21:20:42 +020012#include <linux/debugfs.h>
13#include <linux/uaccess.h>
14#include <linux/ftrace.h>
Ingo Molnar2e0f5762008-05-12 21:20:49 +020015#include <linux/fs.h>
Steven Rostedt1b29b012008-05-12 21:20:42 +020016
17#include "trace.h"
18
Steven Rostedta225cdd2009-01-15 23:06:03 -050019/* function tracing enabled */
20static int ftrace_function_enabled;
21
Steven Rostedt53614992009-01-15 19:12:40 -050022static struct trace_array *func_trace;
23
Steven Rostedta225cdd2009-01-15 23:06:03 -050024static void tracing_start_function_trace(void);
25static void tracing_stop_function_trace(void);
26
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -020027static int function_trace_init(struct trace_array *tr)
Steven Rostedt1b29b012008-05-12 21:20:42 +020028{
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050029 func_trace = tr;
Steven Rostedt26bc83f2008-07-10 20:58:14 -040030 tr->cpu = get_cpu();
Steven Rostedt26bc83f2008-07-10 20:58:14 -040031 put_cpu();
32
Steven Rostedt41bc8142008-05-22 11:49:22 -040033 tracing_start_cmdline_record();
Steven Rostedt1b29b012008-05-12 21:20:42 +020034 tracing_start_function_trace();
Frederic Weisbecker1c800252008-11-16 05:57:26 +010035 return 0;
Steven Rostedt1b29b012008-05-12 21:20:42 +020036}
37
Ingo Molnare309b412008-05-12 21:20:51 +020038static void function_trace_reset(struct trace_array *tr)
Steven Rostedt1b29b012008-05-12 21:20:42 +020039{
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -020040 tracing_stop_function_trace();
41 tracing_stop_cmdline_record();
Steven Rostedt1b29b012008-05-12 21:20:42 +020042}
43
Steven Rostedt90369902008-11-05 16:05:44 -050044static void function_trace_start(struct trace_array *tr)
45{
Pekka J Enberg213cc062008-12-19 12:08:39 +020046 tracing_reset_online_cpus(tr);
Steven Rostedt90369902008-11-05 16:05:44 -050047}
48
Steven Rostedt53614992009-01-15 19:12:40 -050049static void
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050050function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
51{
52 struct trace_array *tr = func_trace;
53 struct trace_array_cpu *data;
54 unsigned long flags;
55 long disabled;
56 int cpu, resched;
57 int pc;
58
59 if (unlikely(!ftrace_function_enabled))
60 return;
61
62 pc = preempt_count();
63 resched = ftrace_preempt_disable();
64 local_save_flags(flags);
65 cpu = raw_smp_processor_id();
66 data = tr->data[cpu];
67 disabled = atomic_inc_return(&data->disabled);
68
69 if (likely(disabled == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -050070 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050071
72 atomic_dec(&data->disabled);
73 ftrace_preempt_enable(resched);
74}
75
76static void
77function_trace_call(unsigned long ip, unsigned long parent_ip)
78{
79 struct trace_array *tr = func_trace;
80 struct trace_array_cpu *data;
81 unsigned long flags;
82 long disabled;
83 int cpu;
84 int pc;
85
86 if (unlikely(!ftrace_function_enabled))
87 return;
88
89 /*
90 * Need to use raw, since this must be called before the
91 * recursive protection is performed.
92 */
93 local_irq_save(flags);
94 cpu = raw_smp_processor_id();
95 data = tr->data[cpu];
96 disabled = atomic_inc_return(&data->disabled);
97
98 if (likely(disabled == 1)) {
99 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500100 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500101 }
102
103 atomic_dec(&data->disabled);
104 local_irq_restore(flags);
105}
106
107static void
Steven Rostedt53614992009-01-15 19:12:40 -0500108function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
109{
110 struct trace_array *tr = func_trace;
111 struct trace_array_cpu *data;
112 unsigned long flags;
113 long disabled;
114 int cpu;
115 int pc;
116
117 if (unlikely(!ftrace_function_enabled))
118 return;
119
120 /*
121 * Need to use raw, since this must be called before the
122 * recursive protection is performed.
123 */
124 local_irq_save(flags);
125 cpu = raw_smp_processor_id();
126 data = tr->data[cpu];
127 disabled = atomic_inc_return(&data->disabled);
128
129 if (likely(disabled == 1)) {
130 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500131 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500132 /*
133 * skip over 5 funcs:
134 * __ftrace_trace_stack,
135 * __trace_stack,
136 * function_stack_trace_call
137 * ftrace_list_func
138 * ftrace_call
139 */
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500140 __trace_stack(tr, flags, 5, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500141 }
142
143 atomic_dec(&data->disabled);
144 local_irq_restore(flags);
145}
146
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500147
148static struct ftrace_ops trace_ops __read_mostly =
149{
150 .func = function_trace_call,
151};
152
Steven Rostedt53614992009-01-15 19:12:40 -0500153static struct ftrace_ops trace_stack_ops __read_mostly =
154{
155 .func = function_stack_trace_call,
156};
157
158/* Our two options */
159enum {
160 TRACE_FUNC_OPT_STACK = 0x1,
161};
162
163static struct tracer_opt func_opts[] = {
164#ifdef CONFIG_STACKTRACE
165 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
166#endif
167 { } /* Always set a last empty entry */
168};
169
170static struct tracer_flags func_flags = {
171 .val = 0, /* By default: all flags disabled */
172 .opts = func_opts
173};
174
Steven Rostedta225cdd2009-01-15 23:06:03 -0500175static void tracing_start_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500176{
177 ftrace_function_enabled = 0;
178
179 if (trace_flags & TRACE_ITER_PREEMPTONLY)
180 trace_ops.func = function_trace_call_preempt_only;
181 else
182 trace_ops.func = function_trace_call;
183
184 if (func_flags.val & TRACE_FUNC_OPT_STACK)
185 register_ftrace_function(&trace_stack_ops);
186 else
187 register_ftrace_function(&trace_ops);
188
189 ftrace_function_enabled = 1;
190}
191
Steven Rostedta225cdd2009-01-15 23:06:03 -0500192static void tracing_stop_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500193{
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);
198}
199
Steven Rostedt53614992009-01-15 19:12:40 -0500200static int func_set_flag(u32 old_flags, u32 bit, int set)
201{
202 if (bit == TRACE_FUNC_OPT_STACK) {
203 /* do nothing if already set */
204 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
205 return 0;
206
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500207 if (set) {
208 unregister_ftrace_function(&trace_ops);
Steven Rostedt53614992009-01-15 19:12:40 -0500209 register_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500210 } else {
Steven Rostedt53614992009-01-15 19:12:40 -0500211 unregister_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500212 register_ftrace_function(&trace_ops);
213 }
Steven Rostedt53614992009-01-15 19:12:40 -0500214
215 return 0;
216 }
217
218 return -EINVAL;
219}
220
Steven Rostedt1b29b012008-05-12 21:20:42 +0200221static struct tracer function_trace __read_mostly =
222{
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500223 .name = "function",
224 .init = function_trace_init,
225 .reset = function_trace_reset,
226 .start = function_trace_start,
Steven Rostedt53614992009-01-15 19:12:40 -0500227 .flags = &func_flags,
228 .set_flag = func_set_flag,
Steven Rostedt60a11772008-05-12 21:20:44 +0200229#ifdef CONFIG_FTRACE_SELFTEST
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500230 .selftest = trace_selftest_startup_function,
Steven Rostedt60a11772008-05-12 21:20:44 +0200231#endif
Steven Rostedt1b29b012008-05-12 21:20:42 +0200232};
233
234static __init int init_function_trace(void)
235{
236 return register_tracer(&function_trace);
237}
238
239device_initcall(init_function_trace);