2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Based on code from the latency_tracer, that is:
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 Nadia Yvette Chambers
12 #include <linux/ring_buffer.h>
13 #include <linux/debugfs.h>
14 #include <linux/uaccess.h>
15 #include <linux/ftrace.h>
20 /* function tracing enabled */
21 static int ftrace_function_enabled;
23 static struct trace_array *func_trace;
25 static void tracing_start_function_trace(void);
26 static void tracing_stop_function_trace(void);
28 static int function_trace_init(struct trace_array *tr)
31 tr->trace_buffer.cpu = get_cpu();
34 tracing_start_cmdline_record();
35 tracing_start_function_trace();
39 static void function_trace_reset(struct trace_array *tr)
41 tracing_stop_function_trace();
42 tracing_stop_cmdline_record();
45 static void function_trace_start(struct trace_array *tr)
47 tracing_reset_online_cpus(&tr->trace_buffer);
52 TRACE_FUNC_OPT_STACK = 0x1,
55 static struct tracer_flags func_flags;
58 function_trace_call(unsigned long ip, unsigned long parent_ip,
59 struct ftrace_ops *op, struct pt_regs *pt_regs)
61 struct trace_array *tr = func_trace;
62 struct trace_array_cpu *data;
68 if (unlikely(!ftrace_function_enabled))
72 preempt_disable_notrace();
74 bit = trace_test_and_set_recursion(TRACE_FTRACE_START, TRACE_FTRACE_MAX);
78 cpu = smp_processor_id();
79 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
80 if (!atomic_read(&data->disabled)) {
81 local_save_flags(flags);
82 trace_function(tr, ip, parent_ip, flags, pc);
84 trace_clear_recursion(bit);
87 preempt_enable_notrace();
91 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
92 struct ftrace_ops *op, struct pt_regs *pt_regs)
94 struct trace_array *tr = func_trace;
95 struct trace_array_cpu *data;
101 if (unlikely(!ftrace_function_enabled))
105 * Need to use raw, since this must be called before the
106 * recursive protection is performed.
108 local_irq_save(flags);
109 cpu = raw_smp_processor_id();
110 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
111 disabled = atomic_inc_return(&data->disabled);
113 if (likely(disabled == 1)) {
114 pc = preempt_count();
115 trace_function(tr, ip, parent_ip, flags, pc);
118 * __ftrace_trace_stack,
120 * function_stack_trace_call
124 __trace_stack(tr, flags, 5, pc);
127 atomic_dec(&data->disabled);
128 local_irq_restore(flags);
132 static struct ftrace_ops trace_ops __read_mostly =
134 .func = function_trace_call,
135 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
138 static struct ftrace_ops trace_stack_ops __read_mostly =
140 .func = function_stack_trace_call,
141 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
144 static struct tracer_opt func_opts[] = {
145 #ifdef CONFIG_STACKTRACE
146 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
148 { } /* Always set a last empty entry */
151 static struct tracer_flags func_flags = {
152 .val = 0, /* By default: all flags disabled */
156 static void tracing_start_function_trace(void)
158 ftrace_function_enabled = 0;
160 if (func_flags.val & TRACE_FUNC_OPT_STACK)
161 register_ftrace_function(&trace_stack_ops);
163 register_ftrace_function(&trace_ops);
165 ftrace_function_enabled = 1;
168 static void tracing_stop_function_trace(void)
170 ftrace_function_enabled = 0;
172 if (func_flags.val & TRACE_FUNC_OPT_STACK)
173 unregister_ftrace_function(&trace_stack_ops);
175 unregister_ftrace_function(&trace_ops);
178 static int func_set_flag(u32 old_flags, u32 bit, int set)
181 case TRACE_FUNC_OPT_STACK:
182 /* do nothing if already set */
183 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
187 unregister_ftrace_function(&trace_ops);
188 register_ftrace_function(&trace_stack_ops);
190 unregister_ftrace_function(&trace_stack_ops);
191 register_ftrace_function(&trace_ops);
202 static struct tracer function_trace __tracer_data =
205 .init = function_trace_init,
206 .reset = function_trace_reset,
207 .start = function_trace_start,
208 .wait_pipe = poll_wait_pipe,
209 .flags = &func_flags,
210 .set_flag = func_set_flag,
211 #ifdef CONFIG_FTRACE_SELFTEST
212 .selftest = trace_selftest_startup_function,
216 #ifdef CONFIG_DYNAMIC_FTRACE
217 static int update_count(void **data)
219 unsigned long *count = (long *)data;
231 ftrace_traceon_count(unsigned long ip, unsigned long parent_ip, void **data)
236 if (update_count(data))
241 ftrace_traceoff_count(unsigned long ip, unsigned long parent_ip, void **data)
243 if (!tracing_is_on())
246 if (update_count(data))
251 ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data)
260 ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data)
262 if (!tracing_is_on())
270 * ftrace_stacktrace()
271 * function_trace_probe_call()
272 * ftrace_ops_list_func()
278 ftrace_stacktrace(unsigned long ip, unsigned long parent_ip, void **data)
280 trace_dump_stack(STACK_SKIP);
284 ftrace_stacktrace_count(unsigned long ip, unsigned long parent_ip, void **data)
286 if (!tracing_is_on())
289 if (update_count(data))
290 trace_dump_stack(STACK_SKIP);
294 ftrace_dump_probe(unsigned long ip, unsigned long parent_ip, void **data)
296 if (update_count(data))
297 ftrace_dump(DUMP_ALL);
300 /* Only dump the current CPU buffer. */
302 ftrace_cpudump_probe(unsigned long ip, unsigned long parent_ip, void **data)
304 if (update_count(data))
305 ftrace_dump(DUMP_ORIG);
309 ftrace_probe_print(const char *name, struct seq_file *m,
310 unsigned long ip, void *data)
312 long count = (long)data;
314 seq_printf(m, "%ps:%s", (void *)ip, name);
317 seq_printf(m, ":unlimited\n");
319 seq_printf(m, ":count=%ld\n", count);
325 ftrace_traceon_print(struct seq_file *m, unsigned long ip,
326 struct ftrace_probe_ops *ops, void *data)
328 return ftrace_probe_print("traceon", m, ip, data);
332 ftrace_traceoff_print(struct seq_file *m, unsigned long ip,
333 struct ftrace_probe_ops *ops, void *data)
335 return ftrace_probe_print("traceoff", m, ip, data);
339 ftrace_stacktrace_print(struct seq_file *m, unsigned long ip,
340 struct ftrace_probe_ops *ops, void *data)
342 return ftrace_probe_print("stacktrace", m, ip, data);
346 ftrace_dump_print(struct seq_file *m, unsigned long ip,
347 struct ftrace_probe_ops *ops, void *data)
349 return ftrace_probe_print("dump", m, ip, data);
353 ftrace_cpudump_print(struct seq_file *m, unsigned long ip,
354 struct ftrace_probe_ops *ops, void *data)
356 return ftrace_probe_print("cpudump", m, ip, data);
359 static struct ftrace_probe_ops traceon_count_probe_ops = {
360 .func = ftrace_traceon_count,
361 .print = ftrace_traceon_print,
364 static struct ftrace_probe_ops traceoff_count_probe_ops = {
365 .func = ftrace_traceoff_count,
366 .print = ftrace_traceoff_print,
369 static struct ftrace_probe_ops stacktrace_count_probe_ops = {
370 .func = ftrace_stacktrace_count,
371 .print = ftrace_stacktrace_print,
374 static struct ftrace_probe_ops dump_probe_ops = {
375 .func = ftrace_dump_probe,
376 .print = ftrace_dump_print,
379 static struct ftrace_probe_ops cpudump_probe_ops = {
380 .func = ftrace_cpudump_probe,
381 .print = ftrace_cpudump_print,
384 static struct ftrace_probe_ops traceon_probe_ops = {
385 .func = ftrace_traceon,
386 .print = ftrace_traceon_print,
389 static struct ftrace_probe_ops traceoff_probe_ops = {
390 .func = ftrace_traceoff,
391 .print = ftrace_traceoff_print,
394 static struct ftrace_probe_ops stacktrace_probe_ops = {
395 .func = ftrace_stacktrace,
396 .print = ftrace_stacktrace_print,
400 ftrace_trace_probe_callback(struct ftrace_probe_ops *ops,
401 struct ftrace_hash *hash, char *glob,
402 char *cmd, char *param, int enable)
404 void *count = (void *)-1;
408 /* hash funcs only work with set_ftrace_filter */
412 if (glob[0] == '!') {
413 unregister_ftrace_function_probe_func(glob+1, ops);
420 number = strsep(¶m, ":");
426 * We use the callback data field (which is a pointer)
429 ret = kstrtoul(number, 0, (unsigned long *)&count);
434 ret = register_ftrace_function_probe(glob, ops, count);
436 return ret < 0 ? ret : 0;
440 ftrace_trace_onoff_callback(struct ftrace_hash *hash,
441 char *glob, char *cmd, char *param, int enable)
443 struct ftrace_probe_ops *ops;
445 /* we register both traceon and traceoff to this callback */
446 if (strcmp(cmd, "traceon") == 0)
447 ops = param ? &traceon_count_probe_ops : &traceon_probe_ops;
449 ops = param ? &traceoff_count_probe_ops : &traceoff_probe_ops;
451 return ftrace_trace_probe_callback(ops, hash, glob, cmd,
456 ftrace_stacktrace_callback(struct ftrace_hash *hash,
457 char *glob, char *cmd, char *param, int enable)
459 struct ftrace_probe_ops *ops;
461 ops = param ? &stacktrace_count_probe_ops : &stacktrace_probe_ops;
463 return ftrace_trace_probe_callback(ops, hash, glob, cmd,
468 ftrace_dump_callback(struct ftrace_hash *hash,
469 char *glob, char *cmd, char *param, int enable)
471 struct ftrace_probe_ops *ops;
473 ops = &dump_probe_ops;
475 /* Only dump once. */
476 return ftrace_trace_probe_callback(ops, hash, glob, cmd,
481 ftrace_cpudump_callback(struct ftrace_hash *hash,
482 char *glob, char *cmd, char *param, int enable)
484 struct ftrace_probe_ops *ops;
486 ops = &cpudump_probe_ops;
488 /* Only dump once. */
489 return ftrace_trace_probe_callback(ops, hash, glob, cmd,
493 static struct ftrace_func_command ftrace_traceon_cmd = {
495 .func = ftrace_trace_onoff_callback,
498 static struct ftrace_func_command ftrace_traceoff_cmd = {
500 .func = ftrace_trace_onoff_callback,
503 static struct ftrace_func_command ftrace_stacktrace_cmd = {
504 .name = "stacktrace",
505 .func = ftrace_stacktrace_callback,
508 static struct ftrace_func_command ftrace_dump_cmd = {
510 .func = ftrace_dump_callback,
513 static struct ftrace_func_command ftrace_cpudump_cmd = {
515 .func = ftrace_cpudump_callback,
518 static int __init init_func_cmd_traceon(void)
522 ret = register_ftrace_command(&ftrace_traceoff_cmd);
526 ret = register_ftrace_command(&ftrace_traceon_cmd);
528 goto out_free_traceoff;
530 ret = register_ftrace_command(&ftrace_stacktrace_cmd);
532 goto out_free_traceon;
534 ret = register_ftrace_command(&ftrace_dump_cmd);
536 goto out_free_stacktrace;
538 ret = register_ftrace_command(&ftrace_cpudump_cmd);
545 unregister_ftrace_command(&ftrace_dump_cmd);
547 unregister_ftrace_command(&ftrace_stacktrace_cmd);
549 unregister_ftrace_command(&ftrace_traceon_cmd);
551 unregister_ftrace_command(&ftrace_traceoff_cmd);
556 static inline int init_func_cmd_traceon(void)
560 #endif /* CONFIG_DYNAMIC_FTRACE */
562 static __init int init_function_trace(void)
564 init_func_cmd_traceon();
565 return register_tracer(&function_trace);
567 core_initcall(init_function_trace);