blob: 8e3ad8082ab7074fde4ea9da403961afd2eadc45 [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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010010 * Copyright (C) 2004 Nadia Yvette Chambers
Steven Rostedt1b29b012008-05-12 21:20:42 +020011 */
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 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
Anton Vorontsov65f8c952012-07-17 14:26:15 -070078/* Our option */
Anton Vorontsov21f67942012-07-09 17:10:42 -070079enum {
80 TRACE_FUNC_OPT_STACK = 0x1,
Anton Vorontsov21f67942012-07-09 17:10:42 -070081};
82
83static struct tracer_flags func_flags;
84
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050085static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040086function_trace_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -040087 struct ftrace_ops *op, struct pt_regs *pt_regs)
88
Steven Rostedtbb3c3c92009-01-15 20:40:23 -050089{
90 struct trace_array *tr = func_trace;
91 struct trace_array_cpu *data;
92 unsigned long flags;
93 long disabled;
94 int cpu;
95 int pc;
96
97 if (unlikely(!ftrace_function_enabled))
98 return;
99
100 /*
101 * Need to use raw, since this must be called before the
102 * recursive protection is performed.
103 */
104 local_irq_save(flags);
105 cpu = raw_smp_processor_id();
106 data = tr->data[cpu];
107 disabled = atomic_inc_return(&data->disabled);
108
109 if (likely(disabled == 1)) {
110 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500111 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500112 }
113
114 atomic_dec(&data->disabled);
115 local_irq_restore(flags);
116}
117
118static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400120 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt53614992009-01-15 19:12:40 -0500121{
122 struct trace_array *tr = func_trace;
123 struct trace_array_cpu *data;
124 unsigned long flags;
125 long disabled;
126 int cpu;
127 int pc;
128
129 if (unlikely(!ftrace_function_enabled))
130 return;
131
132 /*
133 * Need to use raw, since this must be called before the
134 * recursive protection is performed.
135 */
136 local_irq_save(flags);
137 cpu = raw_smp_processor_id();
138 data = tr->data[cpu];
139 disabled = atomic_inc_return(&data->disabled);
140
141 if (likely(disabled == 1)) {
142 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500143 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500144 /*
145 * skip over 5 funcs:
146 * __ftrace_trace_stack,
147 * __trace_stack,
148 * function_stack_trace_call
149 * ftrace_list_func
150 * ftrace_call
151 */
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500152 __trace_stack(tr, flags, 5, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500153 }
154
155 atomic_dec(&data->disabled);
156 local_irq_restore(flags);
157}
158
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500159
160static struct ftrace_ops trace_ops __read_mostly =
161{
162 .func = function_trace_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400163 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbb3c3c92009-01-15 20:40:23 -0500164};
165
Steven Rostedt53614992009-01-15 19:12:40 -0500166static struct ftrace_ops trace_stack_ops __read_mostly =
167{
168 .func = function_stack_trace_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400169 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt53614992009-01-15 19:12:40 -0500170};
171
Steven Rostedt53614992009-01-15 19:12:40 -0500172static struct tracer_opt func_opts[] = {
173#ifdef CONFIG_STACKTRACE
174 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
175#endif
176 { } /* Always set a last empty entry */
177};
178
179static struct tracer_flags func_flags = {
180 .val = 0, /* By default: all flags disabled */
181 .opts = func_opts
182};
183
Steven Rostedta225cdd2009-01-15 23:06:03 -0500184static void tracing_start_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500185{
186 ftrace_function_enabled = 0;
187
188 if (trace_flags & TRACE_ITER_PREEMPTONLY)
189 trace_ops.func = function_trace_call_preempt_only;
190 else
191 trace_ops.func = function_trace_call;
192
193 if (func_flags.val & TRACE_FUNC_OPT_STACK)
194 register_ftrace_function(&trace_stack_ops);
195 else
196 register_ftrace_function(&trace_ops);
197
198 ftrace_function_enabled = 1;
199}
200
Steven Rostedta225cdd2009-01-15 23:06:03 -0500201static void tracing_stop_function_trace(void)
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500202{
203 ftrace_function_enabled = 0;
Frederic Weisbeckerc85a17e2009-06-20 05:45:14 +0200204
205 if (func_flags.val & TRACE_FUNC_OPT_STACK)
206 unregister_ftrace_function(&trace_stack_ops);
207 else
208 unregister_ftrace_function(&trace_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500209}
210
Steven Rostedt53614992009-01-15 19:12:40 -0500211static int func_set_flag(u32 old_flags, u32 bit, int set)
212{
Anton Vorontsovf555f122012-07-09 17:10:46 -0700213 switch (bit) {
214 case TRACE_FUNC_OPT_STACK:
Steven Rostedt53614992009-01-15 19:12:40 -0500215 /* do nothing if already set */
216 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
Anton Vorontsovf555f122012-07-09 17:10:46 -0700217 break;
Steven Rostedt53614992009-01-15 19:12:40 -0500218
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500219 if (set) {
220 unregister_ftrace_function(&trace_ops);
Steven Rostedt53614992009-01-15 19:12:40 -0500221 register_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500222 } else {
Steven Rostedt53614992009-01-15 19:12:40 -0500223 unregister_ftrace_function(&trace_stack_ops);
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500224 register_ftrace_function(&trace_ops);
225 }
Steven Rostedt53614992009-01-15 19:12:40 -0500226
Anton Vorontsovf555f122012-07-09 17:10:46 -0700227 break;
Anton Vorontsovf555f122012-07-09 17:10:46 -0700228 default:
229 return -EINVAL;
Steven Rostedt53614992009-01-15 19:12:40 -0500230 }
231
Anton Vorontsovf555f122012-07-09 17:10:46 -0700232 return 0;
Steven Rostedt53614992009-01-15 19:12:40 -0500233}
234
Steven Rostedt1b29b012008-05-12 21:20:42 +0200235static struct tracer function_trace __read_mostly =
236{
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500237 .name = "function",
238 .init = function_trace_init,
239 .reset = function_trace_reset,
240 .start = function_trace_start,
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100241 .wait_pipe = poll_wait_pipe,
Steven Rostedt53614992009-01-15 19:12:40 -0500242 .flags = &func_flags,
243 .set_flag = func_set_flag,
Steven Rostedt60a11772008-05-12 21:20:44 +0200244#ifdef CONFIG_FTRACE_SELFTEST
Steven Rostedt3eb36aa2009-01-15 22:21:43 -0500245 .selftest = trace_selftest_startup_function,
Steven Rostedt60a11772008-05-12 21:20:44 +0200246#endif
Steven Rostedt1b29b012008-05-12 21:20:42 +0200247};
248
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500249#ifdef CONFIG_DYNAMIC_FTRACE
250static void
251ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data)
252{
253 long *count = (long *)data;
254
255 if (tracing_is_on())
256 return;
257
258 if (!*count)
259 return;
260
261 if (*count != -1)
262 (*count)--;
263
264 tracing_on();
265}
266
267static void
268ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data)
269{
270 long *count = (long *)data;
271
272 if (!tracing_is_on())
273 return;
274
275 if (!*count)
276 return;
277
278 if (*count != -1)
279 (*count)--;
280
281 tracing_off();
282}
283
Steven Rostedte110e3d2009-02-16 23:38:13 -0500284static int
285ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
Steven Rostedtb6887d72009-02-17 12:32:04 -0500286 struct ftrace_probe_ops *ops, void *data);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500287
Steven Rostedtb6887d72009-02-17 12:32:04 -0500288static struct ftrace_probe_ops traceon_probe_ops = {
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500289 .func = ftrace_traceon,
Steven Rostedte110e3d2009-02-16 23:38:13 -0500290 .print = ftrace_trace_onoff_print,
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500291};
292
Steven Rostedtb6887d72009-02-17 12:32:04 -0500293static struct ftrace_probe_ops traceoff_probe_ops = {
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500294 .func = ftrace_traceoff,
Steven Rostedte110e3d2009-02-16 23:38:13 -0500295 .print = ftrace_trace_onoff_print,
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500296};
297
298static int
Steven Rostedte110e3d2009-02-16 23:38:13 -0500299ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
Steven Rostedtb6887d72009-02-17 12:32:04 -0500300 struct ftrace_probe_ops *ops, void *data)
Steven Rostedte110e3d2009-02-16 23:38:13 -0500301{
Steven Rostedte110e3d2009-02-16 23:38:13 -0500302 long count = (long)data;
303
Steven Rostedtb375a112009-09-17 00:05:58 -0400304 seq_printf(m, "%ps:", (void *)ip);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500305
Steven Rostedtb6887d72009-02-17 12:32:04 -0500306 if (ops == &traceon_probe_ops)
Steven Rostedte110e3d2009-02-16 23:38:13 -0500307 seq_printf(m, "traceon");
308 else
309 seq_printf(m, "traceoff");
310
Steven Rostedt35ebf1c2009-02-17 13:12:12 -0500311 if (count == -1)
312 seq_printf(m, ":unlimited\n");
313 else
Li Zefan00e54d02009-06-25 14:05:27 +0800314 seq_printf(m, ":count=%ld\n", count);
Steven Rostedte110e3d2009-02-16 23:38:13 -0500315
316 return 0;
317}
318
319static int
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500320ftrace_trace_onoff_unreg(char *glob, char *cmd, char *param)
321{
Steven Rostedtb6887d72009-02-17 12:32:04 -0500322 struct ftrace_probe_ops *ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500323
324 /* we register both traceon and traceoff to this callback */
325 if (strcmp(cmd, "traceon") == 0)
Steven Rostedtb6887d72009-02-17 12:32:04 -0500326 ops = &traceon_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500327 else
Steven Rostedtb6887d72009-02-17 12:32:04 -0500328 ops = &traceoff_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500329
Steven Rostedtb6887d72009-02-17 12:32:04 -0500330 unregister_ftrace_function_probe_func(glob, ops);
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500331
332 return 0;
333}
334
335static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -0400336ftrace_trace_onoff_callback(struct ftrace_hash *hash,
337 char *glob, char *cmd, char *param, int enable)
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500338{
Steven Rostedtb6887d72009-02-17 12:32:04 -0500339 struct ftrace_probe_ops *ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500340 void *count = (void *)-1;
341 char *number;
342 int ret;
343
344 /* hash funcs only work with set_ftrace_filter */
345 if (!enable)
346 return -EINVAL;
347
348 if (glob[0] == '!')
349 return ftrace_trace_onoff_unreg(glob+1, cmd, param);
350
351 /* we register both traceon and traceoff to this callback */
352 if (strcmp(cmd, "traceon") == 0)
Steven Rostedtb6887d72009-02-17 12:32:04 -0500353 ops = &traceon_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500354 else
Steven Rostedtb6887d72009-02-17 12:32:04 -0500355 ops = &traceoff_probe_ops;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500356
357 if (!param)
358 goto out_reg;
359
360 number = strsep(&param, ":");
361
362 if (!strlen(number))
363 goto out_reg;
364
365 /*
366 * We use the callback data field (which is a pointer)
367 * as our counter.
368 */
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200369 ret = kstrtoul(number, 0, (unsigned long *)&count);
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500370 if (ret)
371 return ret;
372
373 out_reg:
Steven Rostedtb6887d72009-02-17 12:32:04 -0500374 ret = register_ftrace_function_probe(glob, ops, count);
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500375
Xiao Guangrong04aef322009-07-15 12:29:06 +0800376 return ret < 0 ? ret : 0;
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500377}
378
379static struct ftrace_func_command ftrace_traceon_cmd = {
380 .name = "traceon",
381 .func = ftrace_trace_onoff_callback,
382};
383
384static struct ftrace_func_command ftrace_traceoff_cmd = {
385 .name = "traceoff",
386 .func = ftrace_trace_onoff_callback,
387};
388
389static int __init init_func_cmd_traceon(void)
390{
391 int ret;
392
393 ret = register_ftrace_command(&ftrace_traceoff_cmd);
394 if (ret)
395 return ret;
396
397 ret = register_ftrace_command(&ftrace_traceon_cmd);
398 if (ret)
399 unregister_ftrace_command(&ftrace_traceoff_cmd);
400 return ret;
401}
402#else
403static inline int init_func_cmd_traceon(void)
404{
405 return 0;
406}
407#endif /* CONFIG_DYNAMIC_FTRACE */
408
Steven Rostedt1b29b012008-05-12 21:20:42 +0200409static __init int init_function_trace(void)
410{
Steven Rostedt23b4ff3a2009-02-14 19:04:24 -0500411 init_func_cmd_traceon();
Steven Rostedt1b29b012008-05-12 21:20:42 +0200412 return register_tracer(&function_trace);
413}
Steven Rostedt6f415672012-10-05 12:13:07 -0400414core_initcall(init_function_trace);