Commit | Line | Data |
---|---|---|
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 */ |
22 | static int ftrace_function_enabled; | |
23 | ||
53614991 SR |
24 | static struct trace_array *func_trace; |
25 | ||
a225cdd2 SR |
26 | static void tracing_start_function_trace(void); |
27 | static void tracing_stop_function_trace(void); | |
28 | ||
b6f11df2 | 29 | static 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 | 40 | static 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 |
46 | static void function_trace_start(struct trace_array *tr) |
47 | { | |
213cc060 | 48 | tracing_reset_online_cpus(tr); |
9036990d SR |
49 | } |
50 | ||
bb3c3c95 SR |
51 | static void |
52 | function_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 */ |
79 | enum { | |
80 | TRACE_FUNC_OPT_STACK = 0x1, | |
81 | TRACE_FUNC_OPT_PSTORE = 0x2, | |
82 | }; | |
83 | ||
84 | static struct tracer_flags func_flags; | |
85 | ||
bb3c3c95 SR |
86 | static void |
87 | function_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 |
123 | static void |
124 | function_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 | |
164 | static 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 |
170 | static 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 |
176 | static 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 | ||
186 | static struct tracer_flags func_flags = { | |
187 | .val = 0, /* By default: all flags disabled */ | |
188 | .opts = func_opts | |
189 | }; | |
190 | ||
a225cdd2 | 191 | static 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 | 208 | static 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 |
218 | static int func_set_flag(u32 old_flags, u32 bit, int set) |
219 | { | |
220 | if (bit == TRACE_FUNC_OPT_STACK) { | |
221 | /* do nothing if already set */ | |
222 | if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK)) | |
223 | return 0; | |
224 | ||
3eb36aa0 SR |
225 | if (set) { |
226 | unregister_ftrace_function(&trace_ops); | |
53614991 | 227 | register_ftrace_function(&trace_stack_ops); |
3eb36aa0 | 228 | } else { |
53614991 | 229 | unregister_ftrace_function(&trace_stack_ops); |
3eb36aa0 SR |
230 | register_ftrace_function(&trace_ops); |
231 | } | |
53614991 | 232 | |
21f67940 AV |
233 | return 0; |
234 | } else if (bit == TRACE_FUNC_OPT_PSTORE) { | |
53614991 SR |
235 | return 0; |
236 | } | |
237 | ||
238 | return -EINVAL; | |
239 | } | |
240 | ||
1b29b018 SR |
241 | static struct tracer function_trace __read_mostly = |
242 | { | |
3eb36aa0 SR |
243 | .name = "function", |
244 | .init = function_trace_init, | |
245 | .reset = function_trace_reset, | |
246 | .start = function_trace_start, | |
6eaaa5d5 | 247 | .wait_pipe = poll_wait_pipe, |
53614991 SR |
248 | .flags = &func_flags, |
249 | .set_flag = func_set_flag, | |
60a11774 | 250 | #ifdef CONFIG_FTRACE_SELFTEST |
3eb36aa0 | 251 | .selftest = trace_selftest_startup_function, |
60a11774 | 252 | #endif |
1b29b018 SR |
253 | }; |
254 | ||
23b4ff3a SR |
255 | #ifdef CONFIG_DYNAMIC_FTRACE |
256 | static void | |
257 | ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data) | |
258 | { | |
259 | long *count = (long *)data; | |
260 | ||
261 | if (tracing_is_on()) | |
262 | return; | |
263 | ||
264 | if (!*count) | |
265 | return; | |
266 | ||
267 | if (*count != -1) | |
268 | (*count)--; | |
269 | ||
270 | tracing_on(); | |
271 | } | |
272 | ||
273 | static void | |
274 | ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data) | |
275 | { | |
276 | long *count = (long *)data; | |
277 | ||
278 | if (!tracing_is_on()) | |
279 | return; | |
280 | ||
281 | if (!*count) | |
282 | return; | |
283 | ||
284 | if (*count != -1) | |
285 | (*count)--; | |
286 | ||
287 | tracing_off(); | |
288 | } | |
289 | ||
e110e3d1 SR |
290 | static int |
291 | ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip, | |
b6887d79 | 292 | struct ftrace_probe_ops *ops, void *data); |
e110e3d1 | 293 | |
b6887d79 | 294 | static struct ftrace_probe_ops traceon_probe_ops = { |
23b4ff3a | 295 | .func = ftrace_traceon, |
e110e3d1 | 296 | .print = ftrace_trace_onoff_print, |
23b4ff3a SR |
297 | }; |
298 | ||
b6887d79 | 299 | static struct ftrace_probe_ops traceoff_probe_ops = { |
23b4ff3a | 300 | .func = ftrace_traceoff, |
e110e3d1 | 301 | .print = ftrace_trace_onoff_print, |
23b4ff3a SR |
302 | }; |
303 | ||
e110e3d1 SR |
304 | static int |
305 | ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip, | |
b6887d79 | 306 | struct ftrace_probe_ops *ops, void *data) |
e110e3d1 | 307 | { |
e110e3d1 SR |
308 | long count = (long)data; |
309 | ||
b375a11a | 310 | seq_printf(m, "%ps:", (void *)ip); |
e110e3d1 | 311 | |
b6887d79 | 312 | if (ops == &traceon_probe_ops) |
e110e3d1 SR |
313 | seq_printf(m, "traceon"); |
314 | else | |
315 | seq_printf(m, "traceoff"); | |
316 | ||
35ebf1ca SR |
317 | if (count == -1) |
318 | seq_printf(m, ":unlimited\n"); | |
319 | else | |
00e54d08 | 320 | seq_printf(m, ":count=%ld\n", count); |
e110e3d1 SR |
321 | |
322 | return 0; | |
323 | } | |
324 | ||
23b4ff3a SR |
325 | static int |
326 | ftrace_trace_onoff_unreg(char *glob, char *cmd, char *param) | |
327 | { | |
b6887d79 | 328 | struct ftrace_probe_ops *ops; |
23b4ff3a SR |
329 | |
330 | /* we register both traceon and traceoff to this callback */ | |
331 | if (strcmp(cmd, "traceon") == 0) | |
b6887d79 | 332 | ops = &traceon_probe_ops; |
23b4ff3a | 333 | else |
b6887d79 | 334 | ops = &traceoff_probe_ops; |
23b4ff3a | 335 | |
b6887d79 | 336 | unregister_ftrace_function_probe_func(glob, ops); |
23b4ff3a SR |
337 | |
338 | return 0; | |
339 | } | |
340 | ||
341 | static int | |
43dd61c9 SR |
342 | ftrace_trace_onoff_callback(struct ftrace_hash *hash, |
343 | char *glob, char *cmd, char *param, int enable) | |
23b4ff3a | 344 | { |
b6887d79 | 345 | struct ftrace_probe_ops *ops; |
23b4ff3a SR |
346 | void *count = (void *)-1; |
347 | char *number; | |
348 | int ret; | |
349 | ||
350 | /* hash funcs only work with set_ftrace_filter */ | |
351 | if (!enable) | |
352 | return -EINVAL; | |
353 | ||
354 | if (glob[0] == '!') | |
355 | return ftrace_trace_onoff_unreg(glob+1, cmd, param); | |
356 | ||
357 | /* we register both traceon and traceoff to this callback */ | |
358 | if (strcmp(cmd, "traceon") == 0) | |
b6887d79 | 359 | ops = &traceon_probe_ops; |
23b4ff3a | 360 | else |
b6887d79 | 361 | ops = &traceoff_probe_ops; |
23b4ff3a SR |
362 | |
363 | if (!param) | |
364 | goto out_reg; | |
365 | ||
366 | number = strsep(¶m, ":"); | |
367 | ||
368 | if (!strlen(number)) | |
369 | goto out_reg; | |
370 | ||
371 | /* | |
372 | * We use the callback data field (which is a pointer) | |
373 | * as our counter. | |
374 | */ | |
375 | ret = strict_strtoul(number, 0, (unsigned long *)&count); | |
376 | if (ret) | |
377 | return ret; | |
378 | ||
379 | out_reg: | |
b6887d79 | 380 | ret = register_ftrace_function_probe(glob, ops, count); |
23b4ff3a | 381 | |
04aef32d | 382 | return ret < 0 ? ret : 0; |
23b4ff3a SR |
383 | } |
384 | ||
385 | static struct ftrace_func_command ftrace_traceon_cmd = { | |
386 | .name = "traceon", | |
387 | .func = ftrace_trace_onoff_callback, | |
388 | }; | |
389 | ||
390 | static struct ftrace_func_command ftrace_traceoff_cmd = { | |
391 | .name = "traceoff", | |
392 | .func = ftrace_trace_onoff_callback, | |
393 | }; | |
394 | ||
395 | static int __init init_func_cmd_traceon(void) | |
396 | { | |
397 | int ret; | |
398 | ||
399 | ret = register_ftrace_command(&ftrace_traceoff_cmd); | |
400 | if (ret) | |
401 | return ret; | |
402 | ||
403 | ret = register_ftrace_command(&ftrace_traceon_cmd); | |
404 | if (ret) | |
405 | unregister_ftrace_command(&ftrace_traceoff_cmd); | |
406 | return ret; | |
407 | } | |
408 | #else | |
409 | static inline int init_func_cmd_traceon(void) | |
410 | { | |
411 | return 0; | |
412 | } | |
413 | #endif /* CONFIG_DYNAMIC_FTRACE */ | |
414 | ||
1b29b018 SR |
415 | static __init int init_function_trace(void) |
416 | { | |
23b4ff3a | 417 | init_func_cmd_traceon(); |
1b29b018 SR |
418 | return register_tracer(&function_trace); |
419 | } | |
1b29b018 | 420 | device_initcall(init_function_trace); |
23b4ff3a | 421 |