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