blob: 5675ebd541f06485744519a773a83de5533bae32 [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 Rostedt23b4ff32009-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 Rostedt2f5f6ad2011-08-08 16:57:47 -040051function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -040052 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050053{
54 struct trace_array *tr = func_trace;
55 struct trace_array_cpu *data;
56 unsigned long flags;
57 long disabled;
Steven Rostedt5168ae52010-06-03 09:36:50 -040058 int cpu;
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050059 int pc;
60
61 if (unlikely(!ftrace_function_enabled))
62 return;
63
64 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -040065 preempt_disable_notrace();
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050066 local_save_flags(flags);
67 cpu = raw_smp_processor_id();
68 data = tr->data[cpu];
69 disabled = atomic_inc_return(&data->disabled);
70
71 if (likely(disabled == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -050072 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050073
74 atomic_dec(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -040075 preempt_enable_notrace();
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050076}
77
78static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079function_trace_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -040080 struct ftrace_ops *op, struct pt_regs *pt_regs)
81
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050082{
83 struct trace_array *tr = func_trace;
84 struct trace_array_cpu *data;
85 unsigned long flags;
86 long disabled;
87 int cpu;
88 int pc;
89
90 if (unlikely(!ftrace_function_enabled))
91 return;
92
93 /*
94 * Need to use raw, since this must be called before the
95 * recursive protection is performed.
96 */
97 local_irq_save(flags);
98 cpu = raw_smp_processor_id();
99 data = tr->data[cpu];
100 disabled = atomic_inc_return(&data->disabled);
101
102 if (likely(disabled == 1)) {
103 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500104 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500105 }
106
107 atomic_dec(&data->disabled);
108 local_irq_restore(flags);
109}
110
111static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400112function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400113 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt53614992009-01-15 19:12:40 -0500114{
115 struct trace_array *tr = func_trace;
116 struct trace_array_cpu *data;
117 unsigned long flags;
118 long disabled;
119 int cpu;
120 int pc;
121
122 if (unlikely(!ftrace_function_enabled))
123 return;
124
125 /*
126 * Need to use raw, since this must be called before the
127 * recursive protection is performed.
128 */
129 local_irq_save(flags);
130 cpu = raw_smp_processor_id();
131 data = tr->data[cpu];
132 disabled = atomic_inc_return(&data->disabled);
133
134 if (likely(disabled == 1)) {
135 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500136 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500137 /*
138 * skip over 5 funcs:
139 * __ftrace_trace_stack,
140 * __trace_stack,
141 * function_stack_trace_call
142 * ftrace_list_func
143 * ftrace_call
144 */
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500145 __trace_stack(tr, flags, 5, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500146 }
147
148 atomic_dec(&data->disabled);
149 local_irq_restore(flags);
150}
151
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500152
153static struct ftrace_ops trace_ops __read_mostly =
154{
155 .func = function_trace_call,
Steven Rostedtb8489142011-05-04 09:27:52 -0400156 .flags = FTRACE_OPS_FL_GLOBAL,
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500157};
158
Steven Rostedt53614992009-01-15 19:12:40 -0500159static struct ftrace_ops trace_stack_ops __read_mostly =
160{
161 .func = function_stack_trace_call,
Steven Rostedtb8489142011-05-04 09:27:52 -0400162 .flags = FTRACE_OPS_FL_GLOBAL,
Steven Rostedt53614992009-01-15 19:12:40 -0500163};
164
165/* Our two options */
166enum {
167 TRACE_FUNC_OPT_STACK = 0x1,
168};
169
170static struct tracer_opt func_opts[] = {
171#ifdef CONFIG_STACKTRACE
172 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
173#endif
174 { } /* Always set a last empty entry */
175};
176
177static struct tracer_flags func_flags = {
178 .val = 0, /* By default: all flags disabled */
179 .opts = func_opts
180};
181
Steven Rostedta225cdd2009-01-15 23:06:03 -0500182static void tracing_start_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500183{
184 ftrace_function_enabled = 0;
185
186 if (trace_flags & TRACE_ITER_PREEMPTONLY)
187 trace_ops.func = function_trace_call_preempt_only;
188 else
189 trace_ops.func = function_trace_call;
190
191 if (func_flags.val & TRACE_FUNC_OPT_STACK)
192 register_ftrace_function(&trace_stack_ops);
193 else
194 register_ftrace_function(&trace_ops);
195
196 ftrace_function_enabled = 1;
197}
198
Steven Rostedta225cdd2009-01-15 23:06:03 -0500199static void tracing_stop_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500200{
201 ftrace_function_enabled = 0;
Frederic Weisbeckerc85a17e2009-06-20 05:45:14 +0200202
203 if (func_flags.val & TRACE_FUNC_OPT_STACK)
204 unregister_ftrace_function(&trace_stack_ops);
205 else
206 unregister_ftrace_function(&trace_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500207}
208
Steven Rostedt53614992009-01-15 19:12:40 -0500209static int func_set_flag(u32 old_flags, u32 bit, int set)
210{
211 if (bit == TRACE_FUNC_OPT_STACK) {
212 /* do nothing if already set */
213 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
214 return 0;
215
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500216 if (set) {
217 unregister_ftrace_function(&trace_ops);
Steven Rostedt53614992009-01-15 19:12:40 -0500218 register_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500219 } else {
Steven Rostedt53614992009-01-15 19:12:40 -0500220 unregister_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500221 register_ftrace_function(&trace_ops);
222 }
Steven Rostedt53614992009-01-15 19:12:40 -0500223
224 return 0;
225 }
226
227 return -EINVAL;
228}
229
Steven Rostedt1b29b012008-05-12 21:20:42 +0200230static struct tracer function_trace __read_mostly =
231{
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500232 .name = "function",
233 .init = function_trace_init,
234 .reset = function_trace_reset,
235 .start = function_trace_start,
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100236 .wait_pipe = poll_wait_pipe,
Steven Rostedt53614992009-01-15 19:12:40 -0500237 .flags = &func_flags,
238 .set_flag = func_set_flag,
Steven Rostedt60a11772008-05-12 21:20:44 +0200239#ifdef CONFIG_FTRACE_SELFTEST
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500240 .selftest = trace_selftest_startup_function,
Steven Rostedt60a11772008-05-12 21:20:44 +0200241#endif
Steven Rostedt1b29b012008-05-12 21:20:42 +0200242};
243
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500244#ifdef CONFIG_DYNAMIC_FTRACE
245static void
246ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data)
247{
248 long *count = (long *)data;
249
250 if (tracing_is_on())
251 return;
252
253 if (!*count)
254 return;
255
256 if (*count != -1)
257 (*count)--;
258
259 tracing_on();
260}
261
262static void
263ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data)
264{
265 long *count = (long *)data;
266
267 if (!tracing_is_on())
268 return;
269
270 if (!*count)
271 return;
272
273 if (*count != -1)
274 (*count)--;
275
276 tracing_off();
277}
278
Steven Rostedte110e3d2009-02-16 23:38:13 -0500279static int
280ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
Steven Rostedtb6887d72009-02-17 12:32:04 -0500281 struct ftrace_probe_ops *ops, void *data);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500282
Steven Rostedtb6887d72009-02-17 12:32:04 -0500283static struct ftrace_probe_ops traceon_probe_ops = {
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500284 .func = ftrace_traceon,
Steven Rostedte110e3d2009-02-16 23:38:13 -0500285 .print = ftrace_trace_onoff_print,
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500286};
287
Steven Rostedtb6887d72009-02-17 12:32:04 -0500288static struct ftrace_probe_ops traceoff_probe_ops = {
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500289 .func = ftrace_traceoff,
Steven Rostedte110e3d2009-02-16 23:38:13 -0500290 .print = ftrace_trace_onoff_print,
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500291};
292
293static int
Steven Rostedte110e3d2009-02-16 23:38:13 -0500294ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
Steven Rostedtb6887d72009-02-17 12:32:04 -0500295 struct ftrace_probe_ops *ops, void *data)
Steven Rostedte110e3d2009-02-16 23:38:13 -0500296{
Steven Rostedte110e3d2009-02-16 23:38:13 -0500297 long count = (long)data;
298
Steven Rostedtb375a112009-09-17 00:05:58 -0400299 seq_printf(m, "%ps:", (void *)ip);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500300
Steven Rostedtb6887d72009-02-17 12:32:04 -0500301 if (ops == &traceon_probe_ops)
Steven Rostedte110e3d2009-02-16 23:38:13 -0500302 seq_printf(m, "traceon");
303 else
304 seq_printf(m, "traceoff");
305
Steven Rostedt35ebf1c2009-02-17 13:12:12 -0500306 if (count == -1)
307 seq_printf(m, ":unlimited\n");
308 else
Li Zefan00e54d02009-06-25 14:05:27 +0800309 seq_printf(m, ":count=%ld\n", count);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500310
311 return 0;
312}
313
314static int
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500315ftrace_trace_onoff_unreg(char *glob, char *cmd, char *param)
316{
Steven Rostedtb6887d72009-02-17 12:32:04 -0500317 struct ftrace_probe_ops *ops;
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500318
319 /* we register both traceon and traceoff to this callback */
320 if (strcmp(cmd, "traceon") == 0)
Steven Rostedtb6887d72009-02-17 12:32:04 -0500321 ops = &traceon_probe_ops;
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500322 else
Steven Rostedtb6887d72009-02-17 12:32:04 -0500323 ops = &traceoff_probe_ops;
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500324
Steven Rostedtb6887d72009-02-17 12:32:04 -0500325 unregister_ftrace_function_probe_func(glob, ops);
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500326
327 return 0;
328}
329
330static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -0400331ftrace_trace_onoff_callback(struct ftrace_hash *hash,
332 char *glob, char *cmd, char *param, int enable)
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500333{
Steven Rostedtb6887d72009-02-17 12:32:04 -0500334 struct ftrace_probe_ops *ops;
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500335 void *count = (void *)-1;
336 char *number;
337 int ret;
338
339 /* hash funcs only work with set_ftrace_filter */
340 if (!enable)
341 return -EINVAL;
342
343 if (glob[0] == '!')
344 return ftrace_trace_onoff_unreg(glob+1, cmd, param);
345
346 /* we register both traceon and traceoff to this callback */
347 if (strcmp(cmd, "traceon") == 0)
Steven Rostedtb6887d72009-02-17 12:32:04 -0500348 ops = &traceon_probe_ops;
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500349 else
Steven Rostedtb6887d72009-02-17 12:32:04 -0500350 ops = &traceoff_probe_ops;
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500351
352 if (!param)
353 goto out_reg;
354
355 number = strsep(&param, ":");
356
357 if (!strlen(number))
358 goto out_reg;
359
360 /*
361 * We use the callback data field (which is a pointer)
362 * as our counter.
363 */
364 ret = strict_strtoul(number, 0, (unsigned long *)&count);
365 if (ret)
366 return ret;
367
368 out_reg:
Steven Rostedtb6887d72009-02-17 12:32:04 -0500369 ret = register_ftrace_function_probe(glob, ops, count);
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500370
Xiao Guangrong04aef322009-07-15 12:29:06 +0800371 return ret < 0 ? ret : 0;
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500372}
373
374static struct ftrace_func_command ftrace_traceon_cmd = {
375 .name = "traceon",
376 .func = ftrace_trace_onoff_callback,
377};
378
379static struct ftrace_func_command ftrace_traceoff_cmd = {
380 .name = "traceoff",
381 .func = ftrace_trace_onoff_callback,
382};
383
384static int __init init_func_cmd_traceon(void)
385{
386 int ret;
387
388 ret = register_ftrace_command(&ftrace_traceoff_cmd);
389 if (ret)
390 return ret;
391
392 ret = register_ftrace_command(&ftrace_traceon_cmd);
393 if (ret)
394 unregister_ftrace_command(&ftrace_traceoff_cmd);
395 return ret;
396}
397#else
398static inline int init_func_cmd_traceon(void)
399{
400 return 0;
401}
402#endif /* CONFIG_DYNAMIC_FTRACE */
403
Steven Rostedt1b29b012008-05-12 21:20:42 +0200404static __init int init_function_trace(void)
405{
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500406 init_func_cmd_traceon();
Steven Rostedt1b29b012008-05-12 21:20:42 +0200407 return register_tracer(&function_trace);
408}
Steven Rostedt1b29b012008-05-12 21:20:42 +0200409device_initcall(init_function_trace);
Steven Rostedt23b4ff32009-02-14 19:04:24 -0500410