Merge branch 'samsung/pinctrl' into next/drivers
[deliverable/linux.git] / kernel / trace / trace_functions.c
CommitLineData
1b29b018
SR
1/*
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 */
23b4ff3a 12#include <linux/ring_buffer.h>
1b29b018
SR
13#include <linux/debugfs.h>
14#include <linux/uaccess.h>
15#include <linux/ftrace.h>
21f67940 16#include <linux/pstore.h>
2e0f5761 17#include <linux/fs.h>
1b29b018
SR
18
19#include "trace.h"
20
a225cdd2
SR
21/* function tracing enabled */
22static int ftrace_function_enabled;
23
53614991
SR
24static struct trace_array *func_trace;
25
a225cdd2
SR
26static void tracing_start_function_trace(void);
27static void tracing_stop_function_trace(void);
28
b6f11df2 29static int function_trace_init(struct trace_array *tr)
1b29b018 30{
bb3c3c95 31 func_trace = tr;
26bc83f4 32 tr->cpu = get_cpu();
26bc83f4
SR
33 put_cpu();
34
41bc8144 35 tracing_start_cmdline_record();
1b29b018 36 tracing_start_function_trace();
1c80025a 37 return 0;
1b29b018
SR
38}
39
e309b41d 40static void function_trace_reset(struct trace_array *tr)
1b29b018 41{
b6f11df2
ACM
42 tracing_stop_function_trace();
43 tracing_stop_cmdline_record();
1b29b018
SR
44}
45
9036990d
SR
46static void function_trace_start(struct trace_array *tr)
47{
213cc060 48 tracing_reset_online_cpus(tr);
9036990d
SR
49}
50
bb3c3c95
SR
51static void
52function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
53{
54 struct trace_array *tr = func_trace;
55 struct trace_array_cpu *data;
56 unsigned long flags;
57 long disabled;
5168ae50 58 int cpu;
bb3c3c95
SR
59 int pc;
60
61 if (unlikely(!ftrace_function_enabled))
62 return;
63
64 pc = preempt_count();
5168ae50 65 preempt_disable_notrace();
bb3c3c95
SR
66 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))
7be42151 72 trace_function(tr, ip, parent_ip, flags, pc);
bb3c3c95
SR
73
74 atomic_dec(&data->disabled);
5168ae50 75 preempt_enable_notrace();
bb3c3c95
SR
76}
77
21f67940
AV
78/* Our two options */
79enum {
80 TRACE_FUNC_OPT_STACK = 0x1,
81 TRACE_FUNC_OPT_PSTORE = 0x2,
82};
83
84static struct tracer_flags func_flags;
85
bb3c3c95
SR
86static void
87function_trace_call(unsigned long ip, unsigned long parent_ip)
88{
89 struct trace_array *tr = func_trace;
90 struct trace_array_cpu *data;
91 unsigned long flags;
92 long disabled;
93 int cpu;
94 int pc;
95
96 if (unlikely(!ftrace_function_enabled))
97 return;
98
99 /*
100 * Need to use raw, since this must be called before the
101 * recursive protection is performed.
102 */
103 local_irq_save(flags);
104 cpu = raw_smp_processor_id();
105 data = tr->data[cpu];
106 disabled = atomic_inc_return(&data->disabled);
107
108 if (likely(disabled == 1)) {
21f67940
AV
109 /*
110 * So far tracing doesn't support multiple buffers, so
111 * we make an explicit call for now.
112 */
113 if (unlikely(func_flags.val & TRACE_FUNC_OPT_PSTORE))
114 pstore_ftrace_call(ip, parent_ip);
bb3c3c95 115 pc = preempt_count();
7be42151 116 trace_function(tr, ip, parent_ip, flags, pc);
bb3c3c95
SR
117 }
118
119 atomic_dec(&data->disabled);
120 local_irq_restore(flags);
121}
122
53614991
SR
123static void
124function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
125{
126 struct trace_array *tr = func_trace;
127 struct trace_array_cpu *data;
128 unsigned long flags;
129 long disabled;
130 int cpu;
131 int pc;
132
133 if (unlikely(!ftrace_function_enabled))
134 return;
135
136 /*
137 * Need to use raw, since this must be called before the
138 * recursive protection is performed.
139 */
140 local_irq_save(flags);
141 cpu = raw_smp_processor_id();
142 data = tr->data[cpu];
143 disabled = atomic_inc_return(&data->disabled);
144
145 if (likely(disabled == 1)) {
146 pc = preempt_count();
7be42151 147 trace_function(tr, ip, parent_ip, flags, pc);
53614991
SR
148 /*
149 * skip over 5 funcs:
150 * __ftrace_trace_stack,
151 * __trace_stack,
152 * function_stack_trace_call
153 * ftrace_list_func
154 * ftrace_call
155 */
7be42151 156 __trace_stack(tr, flags, 5, pc);
53614991
SR
157 }
158
159 atomic_dec(&data->disabled);
160 local_irq_restore(flags);
161}
162
bb3c3c95
SR
163
164static struct ftrace_ops trace_ops __read_mostly =
165{
166 .func = function_trace_call,
b848914c 167 .flags = FTRACE_OPS_FL_GLOBAL,
bb3c3c95
SR
168};
169
53614991
SR
170static struct ftrace_ops trace_stack_ops __read_mostly =
171{
172 .func = function_stack_trace_call,
b848914c 173 .flags = FTRACE_OPS_FL_GLOBAL,
53614991
SR
174};
175
53614991
SR
176static struct tracer_opt func_opts[] = {
177#ifdef CONFIG_STACKTRACE
178 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
21f67940
AV
179#endif
180#ifdef CONFIG_PSTORE_FTRACE
181 { TRACER_OPT(func_pstore, TRACE_FUNC_OPT_PSTORE) },
53614991
SR
182#endif
183 { } /* Always set a last empty entry */
184};
185
186static struct tracer_flags func_flags = {
187 .val = 0, /* By default: all flags disabled */
188 .opts = func_opts
189};
190
a225cdd2 191static void tracing_start_function_trace(void)
3eb36aa0
SR
192{
193 ftrace_function_enabled = 0;
194
195 if (trace_flags & TRACE_ITER_PREEMPTONLY)
196 trace_ops.func = function_trace_call_preempt_only;
197 else
198 trace_ops.func = function_trace_call;
199
200 if (func_flags.val & TRACE_FUNC_OPT_STACK)
201 register_ftrace_function(&trace_stack_ops);
202 else
203 register_ftrace_function(&trace_ops);
204
205 ftrace_function_enabled = 1;
206}
207
a225cdd2 208static void tracing_stop_function_trace(void)
3eb36aa0
SR
209{
210 ftrace_function_enabled = 0;
c85a17e2
FW
211
212 if (func_flags.val & TRACE_FUNC_OPT_STACK)
213 unregister_ftrace_function(&trace_stack_ops);
214 else
215 unregister_ftrace_function(&trace_ops);
3eb36aa0
SR
216}
217
53614991
SR
218static int func_set_flag(u32 old_flags, u32 bit, int set)
219{
f555f123
AV
220 switch (bit) {
221 case TRACE_FUNC_OPT_STACK:
53614991
SR
222 /* do nothing if already set */
223 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
f555f123 224 break;
53614991 225
3eb36aa0
SR
226 if (set) {
227 unregister_ftrace_function(&trace_ops);
53614991 228 register_ftrace_function(&trace_stack_ops);
3eb36aa0 229 } else {
53614991 230 unregister_ftrace_function(&trace_stack_ops);
3eb36aa0
SR
231 register_ftrace_function(&trace_ops);
232 }
53614991 233
f555f123
AV
234 break;
235 case TRACE_FUNC_OPT_PSTORE:
236 break;
237 default:
238 return -EINVAL;
53614991
SR
239 }
240
f555f123 241 return 0;
53614991
SR
242}
243
1b29b018
SR
244static struct tracer function_trace __read_mostly =
245{
3eb36aa0
SR
246 .name = "function",
247 .init = function_trace_init,
248 .reset = function_trace_reset,
249 .start = function_trace_start,
6eaaa5d5 250 .wait_pipe = poll_wait_pipe,
53614991
SR
251 .flags = &func_flags,
252 .set_flag = func_set_flag,
60a11774 253#ifdef CONFIG_FTRACE_SELFTEST
3eb36aa0 254 .selftest = trace_selftest_startup_function,
60a11774 255#endif
1b29b018
SR
256};
257
23b4ff3a
SR
258#ifdef CONFIG_DYNAMIC_FTRACE
259static void
260ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data)
261{
262 long *count = (long *)data;
263
264 if (tracing_is_on())
265 return;
266
267 if (!*count)
268 return;
269
270 if (*count != -1)
271 (*count)--;
272
273 tracing_on();
274}
275
276static void
277ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data)
278{
279 long *count = (long *)data;
280
281 if (!tracing_is_on())
282 return;
283
284 if (!*count)
285 return;
286
287 if (*count != -1)
288 (*count)--;
289
290 tracing_off();
291}
292
e110e3d1
SR
293static int
294ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
b6887d79 295 struct ftrace_probe_ops *ops, void *data);
e110e3d1 296
b6887d79 297static struct ftrace_probe_ops traceon_probe_ops = {
23b4ff3a 298 .func = ftrace_traceon,
e110e3d1 299 .print = ftrace_trace_onoff_print,
23b4ff3a
SR
300};
301
b6887d79 302static struct ftrace_probe_ops traceoff_probe_ops = {
23b4ff3a 303 .func = ftrace_traceoff,
e110e3d1 304 .print = ftrace_trace_onoff_print,
23b4ff3a
SR
305};
306
e110e3d1
SR
307static int
308ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
b6887d79 309 struct ftrace_probe_ops *ops, void *data)
e110e3d1 310{
e110e3d1
SR
311 long count = (long)data;
312
b375a11a 313 seq_printf(m, "%ps:", (void *)ip);
e110e3d1 314
b6887d79 315 if (ops == &traceon_probe_ops)
e110e3d1
SR
316 seq_printf(m, "traceon");
317 else
318 seq_printf(m, "traceoff");
319
35ebf1ca
SR
320 if (count == -1)
321 seq_printf(m, ":unlimited\n");
322 else
00e54d08 323 seq_printf(m, ":count=%ld\n", count);
e110e3d1
SR
324
325 return 0;
326}
327
23b4ff3a
SR
328static int
329ftrace_trace_onoff_unreg(char *glob, char *cmd, char *param)
330{
b6887d79 331 struct ftrace_probe_ops *ops;
23b4ff3a
SR
332
333 /* we register both traceon and traceoff to this callback */
334 if (strcmp(cmd, "traceon") == 0)
b6887d79 335 ops = &traceon_probe_ops;
23b4ff3a 336 else
b6887d79 337 ops = &traceoff_probe_ops;
23b4ff3a 338
b6887d79 339 unregister_ftrace_function_probe_func(glob, ops);
23b4ff3a
SR
340
341 return 0;
342}
343
344static int
43dd61c9
SR
345ftrace_trace_onoff_callback(struct ftrace_hash *hash,
346 char *glob, char *cmd, char *param, int enable)
23b4ff3a 347{
b6887d79 348 struct ftrace_probe_ops *ops;
23b4ff3a
SR
349 void *count = (void *)-1;
350 char *number;
351 int ret;
352
353 /* hash funcs only work with set_ftrace_filter */
354 if (!enable)
355 return -EINVAL;
356
357 if (glob[0] == '!')
358 return ftrace_trace_onoff_unreg(glob+1, cmd, param);
359
360 /* we register both traceon and traceoff to this callback */
361 if (strcmp(cmd, "traceon") == 0)
b6887d79 362 ops = &traceon_probe_ops;
23b4ff3a 363 else
b6887d79 364 ops = &traceoff_probe_ops;
23b4ff3a
SR
365
366 if (!param)
367 goto out_reg;
368
369 number = strsep(&param, ":");
370
371 if (!strlen(number))
372 goto out_reg;
373
374 /*
375 * We use the callback data field (which is a pointer)
376 * as our counter.
377 */
378 ret = strict_strtoul(number, 0, (unsigned long *)&count);
379 if (ret)
380 return ret;
381
382 out_reg:
b6887d79 383 ret = register_ftrace_function_probe(glob, ops, count);
23b4ff3a 384
04aef32d 385 return ret < 0 ? ret : 0;
23b4ff3a
SR
386}
387
388static struct ftrace_func_command ftrace_traceon_cmd = {
389 .name = "traceon",
390 .func = ftrace_trace_onoff_callback,
391};
392
393static struct ftrace_func_command ftrace_traceoff_cmd = {
394 .name = "traceoff",
395 .func = ftrace_trace_onoff_callback,
396};
397
398static int __init init_func_cmd_traceon(void)
399{
400 int ret;
401
402 ret = register_ftrace_command(&ftrace_traceoff_cmd);
403 if (ret)
404 return ret;
405
406 ret = register_ftrace_command(&ftrace_traceon_cmd);
407 if (ret)
408 unregister_ftrace_command(&ftrace_traceoff_cmd);
409 return ret;
410}
411#else
412static inline int init_func_cmd_traceon(void)
413{
414 return 0;
415}
416#endif /* CONFIG_DYNAMIC_FTRACE */
417
1b29b018
SR
418static __init int init_function_trace(void)
419{
23b4ff3a 420 init_func_cmd_traceon();
1b29b018
SR
421 return register_tracer(&function_trace);
422}
1b29b018 423device_initcall(init_function_trace);
23b4ff3a 424
This page took 0.302223 seconds and 5 git commands to generate.