blob: c7b0c6a7db0986b2a54e1b892367bc7be2ffb058 [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 Rostedt23b4ff3a2009-02-14 19:04:24 -050012#include <linux/ring_buffer.h>
Steven Rostedt1b29b012008-05-12 21:20:42 +020013#include <linux/debugfs.h>
14#include <linux/uaccess.h>
15#include <linux/ftrace.h>
Ingo Molnar2e0f5762008-05-12 21:20:49 +020016#include <linux/fs.h>
Steven Rostedt1b29b012008-05-12 21:20:42 +020017
18#include "trace.h"
19
Steven Rostedta225cdd2009-01-15 23:06:03 -050020/* function tracing enabled */
21static int ftrace_function_enabled;
22
Steven Rostedt53614992009-01-15 19:12:40 -050023static struct trace_array *func_trace;
24
Steven Rostedta225cdd2009-01-15 23:06:03 -050025static void tracing_start_function_trace(void);
26static void tracing_stop_function_trace(void);
27
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -020028static int function_trace_init(struct trace_array *tr)
Steven Rostedt1b29b012008-05-12 21:20:42 +020029{
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050030 func_trace = tr;
Steven Rostedt26bc83f2008-07-10 20:58:14 -040031 tr->cpu = get_cpu();
Steven Rostedt26bc83f2008-07-10 20:58:14 -040032 put_cpu();
33
Steven Rostedt41bc8142008-05-22 11:49:22 -040034 tracing_start_cmdline_record();
Steven Rostedt1b29b012008-05-12 21:20:42 +020035 tracing_start_function_trace();
Frederic Weisbecker1c800252008-11-16 05:57:26 +010036 return 0;
Steven Rostedt1b29b012008-05-12 21:20:42 +020037}
38
Ingo Molnare309b412008-05-12 21:20:51 +020039static void function_trace_reset(struct trace_array *tr)
Steven Rostedt1b29b012008-05-12 21:20:42 +020040{
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -020041 tracing_stop_function_trace();
42 tracing_stop_cmdline_record();
Steven Rostedt1b29b012008-05-12 21:20:42 +020043}
44
Steven Rostedt90369902008-11-05 16:05:44 -050045static void function_trace_start(struct trace_array *tr)
46{
Pekka J Enberg213cc062008-12-19 12:08:39 +020047 tracing_reset_online_cpus(tr);
Steven Rostedt90369902008-11-05 16:05:44 -050048}
49
Steven Rostedt53614992009-01-15 19:12:40 -050050static void
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050051function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
52{
53 struct trace_array *tr = func_trace;
54 struct trace_array_cpu *data;
55 unsigned long flags;
56 long disabled;
Steven Rostedt5168ae52010-06-03 09:36:50 -040057 int cpu;
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050058 int pc;
59
60 if (unlikely(!ftrace_function_enabled))
61 return;
62
63 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -040064 preempt_disable_notrace();
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050065 local_save_flags(flags);
66 cpu = raw_smp_processor_id();
67 data = tr->data[cpu];
68 disabled = atomic_inc_return(&data->disabled);
69
70 if (likely(disabled == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -050071 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050072
73 atomic_dec(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -040074 preempt_enable_notrace();
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050075}
76
77static void
78function_trace_call(unsigned long ip, unsigned long parent_ip)
79{
80 struct trace_array *tr = func_trace;
81 struct trace_array_cpu *data;
82 unsigned long flags;
83 long disabled;
84 int cpu;
85 int pc;
86
87 if (unlikely(!ftrace_function_enabled))
88 return;
89
90 /*
91 * Need to use raw, since this must be called before the
92 * recursive protection is performed.
93 */
94 local_irq_save(flags);
95 cpu = raw_smp_processor_id();
96 data = tr->data[cpu];
97 disabled = atomic_inc_return(&data->disabled);
98
99 if (likely(disabled == 1)) {
100 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500101 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500102 }
103
104 atomic_dec(&data->disabled);
105 local_irq_restore(flags);
106}
107
108static void
Steven Rostedt53614992009-01-15 19:12:40 -0500109function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
110{
111 struct trace_array *tr = func_trace;
112 struct trace_array_cpu *data;
113 unsigned long flags;
114 long disabled;
115 int cpu;
116 int pc;
117
118 if (unlikely(!ftrace_function_enabled))
119 return;
120
121 /*
122 * Need to use raw, since this must be called before the
123 * recursive protection is performed.
124 */
125 local_irq_save(flags);
126 cpu = raw_smp_processor_id();
127 data = tr->data[cpu];
128 disabled = atomic_inc_return(&data->disabled);
129
130 if (likely(disabled == 1)) {
131 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500132 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500133 /*
134 * skip over 5 funcs:
135 * __ftrace_trace_stack,
136 * __trace_stack,
137 * function_stack_trace_call
138 * ftrace_list_func
139 * ftrace_call
140 */
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500141 __trace_stack(tr, flags, 5, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500142 }
143
144 atomic_dec(&data->disabled);
145 local_irq_restore(flags);
146}
147
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500148
149static struct ftrace_ops trace_ops __read_mostly =
150{
151 .func = function_trace_call,
Steven Rostedtb8489142011-05-04 09:27:52 -0400152 .flags = FTRACE_OPS_FL_GLOBAL,
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500153};
154
Steven Rostedt53614992009-01-15 19:12:40 -0500155static struct ftrace_ops trace_stack_ops __read_mostly =
156{
157 .func = function_stack_trace_call,
Steven Rostedtb8489142011-05-04 09:27:52 -0400158 .flags = FTRACE_OPS_FL_GLOBAL,
Steven Rostedt53614992009-01-15 19:12:40 -0500159};
160
161/* Our two options */
162enum {
163 TRACE_FUNC_OPT_STACK = 0x1,
164};
165
166static struct tracer_opt func_opts[] = {
167#ifdef CONFIG_STACKTRACE
168 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
169#endif
170 { } /* Always set a last empty entry */
171};
172
173static struct tracer_flags func_flags = {
174 .val = 0, /* By default: all flags disabled */
175 .opts = func_opts
176};
177
Steven Rostedta225cdd2009-01-15 23:06:03 -0500178static void tracing_start_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500179{
180 ftrace_function_enabled = 0;
181
182 if (trace_flags & TRACE_ITER_PREEMPTONLY)
183 trace_ops.func = function_trace_call_preempt_only;
184 else
185 trace_ops.func = function_trace_call;
186
187 if (func_flags.val & TRACE_FUNC_OPT_STACK)
188 register_ftrace_function(&trace_stack_ops);
189 else
190 register_ftrace_function(&trace_ops);
191
192 ftrace_function_enabled = 1;
193}
194
Steven Rostedta225cdd2009-01-15 23:06:03 -0500195static void tracing_stop_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500196{
197 ftrace_function_enabled = 0;
Frederic Weisbeckerc85a17e2009-06-20 05:45:14 +0200198
199 if (func_flags.val & TRACE_FUNC_OPT_STACK)
200 unregister_ftrace_function(&trace_stack_ops);
201 else
202 unregister_ftrace_function(&trace_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500203}
204
Steven Rostedt53614992009-01-15 19:12:40 -0500205static int func_set_flag(u32 old_flags, u32 bit, int set)
206{
207 if (bit == TRACE_FUNC_OPT_STACK) {
208 /* do nothing if already set */
209 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
210 return 0;
211
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500212 if (set) {
213 unregister_ftrace_function(&trace_ops);
Steven Rostedt53614992009-01-15 19:12:40 -0500214 register_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500215 } else {
Steven Rostedt53614992009-01-15 19:12:40 -0500216 unregister_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500217 register_ftrace_function(&trace_ops);
218 }
Steven Rostedt53614992009-01-15 19:12:40 -0500219
220 return 0;
221 }
222
223 return -EINVAL;
224}
225
Steven Rostedt1b29b012008-05-12 21:20:42 +0200226static struct tracer function_trace __read_mostly =
227{
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500228 .name = "function",
229 .init = function_trace_init,
230 .reset = function_trace_reset,
231 .start = function_trace_start,
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100232 .wait_pipe = poll_wait_pipe,
Steven Rostedt53614992009-01-15 19:12:40 -0500233 .flags = &func_flags,
234 .set_flag = func_set_flag,
Steven Rostedt60a11772008-05-12 21:20:44 +0200235#ifdef CONFIG_FTRACE_SELFTEST
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500236 .selftest = trace_selftest_startup_function,
Steven Rostedt60a11772008-05-12 21:20:44 +0200237#endif
Steven Rostedt1b29b012008-05-12 21:20:42 +0200238};
239
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500240#ifdef CONFIG_DYNAMIC_FTRACE
241static void
242ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data)
243{
244 long *count = (long *)data;
245
246 if (tracing_is_on())
247 return;
248
249 if (!*count)
250 return;
251
252 if (*count != -1)
253 (*count)--;
254
255 tracing_on();
256}
257
258static void
259ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data)
260{
261 long *count = (long *)data;
262
263 if (!tracing_is_on())
264 return;
265
266 if (!*count)
267 return;
268
269 if (*count != -1)
270 (*count)--;
271
272 tracing_off();
273}
274
Steven Rostedte110e3d2009-02-16 23:38:13 -0500275static int
276ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
Steven Rostedtb6887d72009-02-17 12:32:04 -0500277 struct ftrace_probe_ops *ops, void *data);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500278
Steven Rostedtb6887d72009-02-17 12:32:04 -0500279static struct ftrace_probe_ops traceon_probe_ops = {
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500280 .func = ftrace_traceon,
Steven Rostedte110e3d2009-02-16 23:38:13 -0500281 .print = ftrace_trace_onoff_print,
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500282};
283
Steven Rostedtb6887d72009-02-17 12:32:04 -0500284static struct ftrace_probe_ops traceoff_probe_ops = {
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500285 .func = ftrace_traceoff,
Steven Rostedte110e3d2009-02-16 23:38:13 -0500286 .print = ftrace_trace_onoff_print,
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500287};
288
289static int
Steven Rostedte110e3d2009-02-16 23:38:13 -0500290ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
Steven Rostedtb6887d72009-02-17 12:32:04 -0500291 struct ftrace_probe_ops *ops, void *data)
Steven Rostedte110e3d2009-02-16 23:38:13 -0500292{
Steven Rostedte110e3d2009-02-16 23:38:13 -0500293 long count = (long)data;
294
Steven Rostedtb375a112009-09-17 00:05:58 -0400295 seq_printf(m, "%ps:", (void *)ip);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500296
Steven Rostedtb6887d72009-02-17 12:32:04 -0500297 if (ops == &traceon_probe_ops)
Steven Rostedte110e3d2009-02-16 23:38:13 -0500298 seq_printf(m, "traceon");
299 else
300 seq_printf(m, "traceoff");
301
Steven Rostedt35ebf1c2009-02-17 13:12:12 -0500302 if (count == -1)
303 seq_printf(m, ":unlimited\n");
304 else
Li Zefan00e54d02009-06-25 14:05:27 +0800305 seq_printf(m, ":count=%ld\n", count);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500306
307 return 0;
308}
309
310static int
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500311ftrace_trace_onoff_unreg(char *glob, char *cmd, char *param)
312{
Steven Rostedtb6887d72009-02-17 12:32:04 -0500313 struct ftrace_probe_ops *ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500314
315 /* we register both traceon and traceoff to this callback */
316 if (strcmp(cmd, "traceon") == 0)
Steven Rostedtb6887d72009-02-17 12:32:04 -0500317 ops = &traceon_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500318 else
Steven Rostedtb6887d72009-02-17 12:32:04 -0500319 ops = &traceoff_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500320
Steven Rostedtb6887d72009-02-17 12:32:04 -0500321 unregister_ftrace_function_probe_func(glob, ops);
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500322
323 return 0;
324}
325
326static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -0400327ftrace_trace_onoff_callback(struct ftrace_hash *hash,
328 char *glob, char *cmd, char *param, int enable)
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500329{
Steven Rostedtb6887d72009-02-17 12:32:04 -0500330 struct ftrace_probe_ops *ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500331 void *count = (void *)-1;
332 char *number;
333 int ret;
334
335 /* hash funcs only work with set_ftrace_filter */
336 if (!enable)
337 return -EINVAL;
338
339 if (glob[0] == '!')
340 return ftrace_trace_onoff_unreg(glob+1, cmd, param);
341
342 /* we register both traceon and traceoff to this callback */
343 if (strcmp(cmd, "traceon") == 0)
Steven Rostedtb6887d72009-02-17 12:32:04 -0500344 ops = &traceon_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500345 else
Steven Rostedtb6887d72009-02-17 12:32:04 -0500346 ops = &traceoff_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500347
348 if (!param)
349 goto out_reg;
350
351 number = strsep(&param, ":");
352
353 if (!strlen(number))
354 goto out_reg;
355
356 /*
357 * We use the callback data field (which is a pointer)
358 * as our counter.
359 */
360 ret = strict_strtoul(number, 0, (unsigned long *)&count);
361 if (ret)
362 return ret;
363
364 out_reg:
Steven Rostedtb6887d72009-02-17 12:32:04 -0500365 ret = register_ftrace_function_probe(glob, ops, count);
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500366
Xiao Guangrong04aef322009-07-15 12:29:06 +0800367 return ret < 0 ? ret : 0;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500368}
369
370static struct ftrace_func_command ftrace_traceon_cmd = {
371 .name = "traceon",
372 .func = ftrace_trace_onoff_callback,
373};
374
375static struct ftrace_func_command ftrace_traceoff_cmd = {
376 .name = "traceoff",
377 .func = ftrace_trace_onoff_callback,
378};
379
380static int __init init_func_cmd_traceon(void)
381{
382 int ret;
383
384 ret = register_ftrace_command(&ftrace_traceoff_cmd);
385 if (ret)
386 return ret;
387
388 ret = register_ftrace_command(&ftrace_traceon_cmd);
389 if (ret)
390 unregister_ftrace_command(&ftrace_traceoff_cmd);
391 return ret;
392}
393#else
394static inline int init_func_cmd_traceon(void)
395{
396 return 0;
397}
398#endif /* CONFIG_DYNAMIC_FTRACE */
399
Steven Rostedt1b29b012008-05-12 21:20:42 +0200400static __init int init_function_trace(void)
401{
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500402 init_func_cmd_traceon();
Steven Rostedt1b29b012008-05-12 21:20:42 +0200403 return register_tracer(&function_trace);
404}
Steven Rostedt1b29b012008-05-12 21:20:42 +0200405device_initcall(init_function_trace);
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500406