1 // SPDX-License-Identifier: GPL-2.0
3 * ring buffer based function tracer
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
8 * Based on code from the latency_tracer, that is:
10 * Copyright (C) 2004-2006 Ingo Molnar
11 * Copyright (C) 2004 Nadia Yvette Chambers
13 #include <linux/ring_buffer.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/ftrace.h>
17 #include <linux/slab.h>
22 static void tracing_start_function_trace(struct trace_array *tr);
23 static void tracing_stop_function_trace(struct trace_array *tr);
25 function_trace_call(unsigned long ip, unsigned long parent_ip,
26 struct ftrace_ops *op, struct ftrace_regs *fregs);
28 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
29 struct ftrace_ops *op, struct ftrace_regs *fregs);
30 static struct tracer_flags func_flags;
34 TRACE_FUNC_OPT_STACK = 0x1,
37 int ftrace_allocate_ftrace_ops(struct trace_array *tr)
39 struct ftrace_ops *ops;
41 /* The top level array uses the "global_ops" */
42 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
45 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
49 /* Currently only the non stack version is supported */
50 ops->func = function_trace_call;
51 ops->flags = FTRACE_OPS_FL_PID;
59 void ftrace_free_ftrace_ops(struct trace_array *tr)
65 int ftrace_create_function_files(struct trace_array *tr,
66 struct dentry *parent)
69 * The top level array uses the "global_ops", and the files are
72 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
78 ftrace_create_filter_files(tr->ops, parent);
83 void ftrace_destroy_function_files(struct trace_array *tr)
85 ftrace_destroy_filter_files(tr->ops);
86 ftrace_free_ftrace_ops(tr);
89 static int function_trace_init(struct trace_array *tr)
93 * Instance trace_arrays get their ops allocated
94 * at instance creation. Unless it failed
100 /* Currently only the global instance can do stack tracing */
101 if (tr->flags & TRACE_ARRAY_FL_GLOBAL &&
102 func_flags.val & TRACE_FUNC_OPT_STACK)
103 func = function_stack_trace_call;
105 func = function_trace_call;
107 ftrace_init_array_ops(tr, func);
109 tr->array_buffer.cpu = raw_smp_processor_id();
111 tracing_start_cmdline_record();
112 tracing_start_function_trace(tr);
116 static void function_trace_reset(struct trace_array *tr)
118 tracing_stop_function_trace(tr);
119 tracing_stop_cmdline_record();
120 ftrace_reset_array_ops(tr);
123 static void function_trace_start(struct trace_array *tr)
125 tracing_reset_online_cpus(&tr->array_buffer);
129 function_trace_call(unsigned long ip, unsigned long parent_ip,
130 struct ftrace_ops *op, struct ftrace_regs *fregs)
132 struct trace_array *tr = op->private;
133 struct trace_array_cpu *data;
134 unsigned int trace_ctx;
138 if (unlikely(!tr->function_enabled))
141 bit = ftrace_test_recursion_trylock(ip, parent_ip);
145 trace_ctx = tracing_gen_ctx();
146 preempt_disable_notrace();
148 cpu = smp_processor_id();
149 data = per_cpu_ptr(tr->array_buffer.data, cpu);
150 if (!atomic_read(&data->disabled))
151 trace_function(tr, ip, parent_ip, trace_ctx);
153 ftrace_test_recursion_unlock(bit);
154 preempt_enable_notrace();
157 #ifdef CONFIG_UNWINDER_ORC
161 * function_stack_trace_call()
169 * function_stack_trace_call()
176 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
177 struct ftrace_ops *op, struct ftrace_regs *fregs)
179 struct trace_array *tr = op->private;
180 struct trace_array_cpu *data;
184 unsigned int trace_ctx;
186 if (unlikely(!tr->function_enabled))
190 * Need to use raw, since this must be called before the
191 * recursive protection is performed.
193 local_irq_save(flags);
194 cpu = raw_smp_processor_id();
195 data = per_cpu_ptr(tr->array_buffer.data, cpu);
196 disabled = atomic_inc_return(&data->disabled);
198 if (likely(disabled == 1)) {
199 trace_ctx = tracing_gen_ctx_flags(flags);
200 trace_function(tr, ip, parent_ip, trace_ctx);
201 __trace_stack(tr, trace_ctx, STACK_SKIP);
204 atomic_dec(&data->disabled);
205 local_irq_restore(flags);
208 static struct tracer_opt func_opts[] = {
209 #ifdef CONFIG_STACKTRACE
210 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
212 { } /* Always set a last empty entry */
215 static struct tracer_flags func_flags = {
216 .val = 0, /* By default: all flags disabled */
220 static void tracing_start_function_trace(struct trace_array *tr)
222 tr->function_enabled = 0;
223 register_ftrace_function(tr->ops);
224 tr->function_enabled = 1;
227 static void tracing_stop_function_trace(struct trace_array *tr)
229 tr->function_enabled = 0;
230 unregister_ftrace_function(tr->ops);
233 static struct tracer function_trace;
236 func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
239 case TRACE_FUNC_OPT_STACK:
240 /* do nothing if already set */
241 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
244 /* We can change this flag when not running. */
245 if (tr->current_trace != &function_trace)
248 unregister_ftrace_function(tr->ops);
251 tr->ops->func = function_stack_trace_call;
252 register_ftrace_function(tr->ops);
254 tr->ops->func = function_trace_call;
255 register_ftrace_function(tr->ops);
266 static struct tracer function_trace __tracer_data =
269 .init = function_trace_init,
270 .reset = function_trace_reset,
271 .start = function_trace_start,
272 .flags = &func_flags,
273 .set_flag = func_set_flag,
274 .allow_instances = true,
275 #ifdef CONFIG_FTRACE_SELFTEST
276 .selftest = trace_selftest_startup_function,
280 #ifdef CONFIG_DYNAMIC_FTRACE
281 static void update_traceon_count(struct ftrace_probe_ops *ops,
283 struct trace_array *tr, bool on,
286 struct ftrace_func_mapper *mapper = data;
291 * Tracing gets disabled (or enabled) once per count.
292 * This function can be called at the same time on multiple CPUs.
293 * It is fine if both disable (or enable) tracing, as disabling
294 * (or enabling) the second time doesn't do anything as the
295 * state of the tracer is already disabled (or enabled).
296 * What needs to be synchronized in this case is that the count
297 * only gets decremented once, even if the tracer is disabled
298 * (or enabled) twice, as the second one is really a nop.
300 * The memory barriers guarantee that we only decrement the
301 * counter once. First the count is read to a local variable
302 * and a read barrier is used to make sure that it is loaded
303 * before checking if the tracer is in the state we want.
304 * If the tracer is not in the state we want, then the count
305 * is guaranteed to be the old count.
307 * Next the tracer is set to the state we want (disabled or enabled)
308 * then a write memory barrier is used to make sure that
309 * the new state is visible before changing the counter by
310 * one minus the old counter. This guarantees that another CPU
311 * executing this code will see the new state before seeing
312 * the new counter value, and would not do anything if the new
315 * Note, there is no synchronization between this and a user
316 * setting the tracing_on file. But we currently don't care
319 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
325 /* Make sure we see count before checking tracing state */
328 if (on == !!tracer_tracing_is_on(tr))
332 tracer_tracing_on(tr);
334 tracer_tracing_off(tr);
336 /* Make sure tracing state is visible before updating count */
339 *count = old_count - 1;
343 ftrace_traceon_count(unsigned long ip, unsigned long parent_ip,
344 struct trace_array *tr, struct ftrace_probe_ops *ops,
347 update_traceon_count(ops, ip, tr, 1, data);
351 ftrace_traceoff_count(unsigned long ip, unsigned long parent_ip,
352 struct trace_array *tr, struct ftrace_probe_ops *ops,
355 update_traceon_count(ops, ip, tr, 0, data);
359 ftrace_traceon(unsigned long ip, unsigned long parent_ip,
360 struct trace_array *tr, struct ftrace_probe_ops *ops,
363 if (tracer_tracing_is_on(tr))
366 tracer_tracing_on(tr);
370 ftrace_traceoff(unsigned long ip, unsigned long parent_ip,
371 struct trace_array *tr, struct ftrace_probe_ops *ops,
374 if (!tracer_tracing_is_on(tr))
377 tracer_tracing_off(tr);
380 #ifdef CONFIG_UNWINDER_ORC
384 * function_trace_probe_call()
385 * ftrace_ops_assist_func()
388 #define FTRACE_STACK_SKIP 3
394 * ftrace_stacktrace()
395 * function_trace_probe_call()
396 * ftrace_ops_assist_func()
399 #define FTRACE_STACK_SKIP 5
402 static __always_inline void trace_stack(struct trace_array *tr)
404 unsigned int trace_ctx;
406 trace_ctx = tracing_gen_ctx();
408 __trace_stack(tr, trace_ctx, FTRACE_STACK_SKIP);
412 ftrace_stacktrace(unsigned long ip, unsigned long parent_ip,
413 struct trace_array *tr, struct ftrace_probe_ops *ops,
420 ftrace_stacktrace_count(unsigned long ip, unsigned long parent_ip,
421 struct trace_array *tr, struct ftrace_probe_ops *ops,
424 struct ftrace_func_mapper *mapper = data;
429 if (!tracing_is_on())
438 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
441 * Stack traces should only execute the number of times the
442 * user specified in the counter.
450 new_count = old_count - 1;
451 new_count = cmpxchg(count, old_count, new_count);
452 if (new_count == old_count)
455 if (!tracing_is_on())
458 } while (new_count != old_count);
461 static int update_count(struct ftrace_probe_ops *ops, unsigned long ip,
464 struct ftrace_func_mapper *mapper = data;
468 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
480 ftrace_dump_probe(unsigned long ip, unsigned long parent_ip,
481 struct trace_array *tr, struct ftrace_probe_ops *ops,
484 if (update_count(ops, ip, data))
485 ftrace_dump(DUMP_ALL);
488 /* Only dump the current CPU buffer. */
490 ftrace_cpudump_probe(unsigned long ip, unsigned long parent_ip,
491 struct trace_array *tr, struct ftrace_probe_ops *ops,
494 if (update_count(ops, ip, data))
495 ftrace_dump(DUMP_ORIG);
499 ftrace_probe_print(const char *name, struct seq_file *m,
500 unsigned long ip, struct ftrace_probe_ops *ops,
503 struct ftrace_func_mapper *mapper = data;
506 seq_printf(m, "%ps:%s", (void *)ip, name);
509 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
512 seq_printf(m, ":count=%ld\n", *count);
514 seq_puts(m, ":unlimited\n");
520 ftrace_traceon_print(struct seq_file *m, unsigned long ip,
521 struct ftrace_probe_ops *ops,
524 return ftrace_probe_print("traceon", m, ip, ops, data);
528 ftrace_traceoff_print(struct seq_file *m, unsigned long ip,
529 struct ftrace_probe_ops *ops, void *data)
531 return ftrace_probe_print("traceoff", m, ip, ops, data);
535 ftrace_stacktrace_print(struct seq_file *m, unsigned long ip,
536 struct ftrace_probe_ops *ops, void *data)
538 return ftrace_probe_print("stacktrace", m, ip, ops, data);
542 ftrace_dump_print(struct seq_file *m, unsigned long ip,
543 struct ftrace_probe_ops *ops, void *data)
545 return ftrace_probe_print("dump", m, ip, ops, data);
549 ftrace_cpudump_print(struct seq_file *m, unsigned long ip,
550 struct ftrace_probe_ops *ops, void *data)
552 return ftrace_probe_print("cpudump", m, ip, ops, data);
557 ftrace_count_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
558 unsigned long ip, void *init_data, void **data)
560 struct ftrace_func_mapper *mapper = *data;
563 mapper = allocate_ftrace_func_mapper();
569 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
573 ftrace_count_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
574 unsigned long ip, void *data)
576 struct ftrace_func_mapper *mapper = data;
579 free_ftrace_func_mapper(mapper, NULL);
583 ftrace_func_mapper_remove_ip(mapper, ip);
586 static struct ftrace_probe_ops traceon_count_probe_ops = {
587 .func = ftrace_traceon_count,
588 .print = ftrace_traceon_print,
589 .init = ftrace_count_init,
590 .free = ftrace_count_free,
593 static struct ftrace_probe_ops traceoff_count_probe_ops = {
594 .func = ftrace_traceoff_count,
595 .print = ftrace_traceoff_print,
596 .init = ftrace_count_init,
597 .free = ftrace_count_free,
600 static struct ftrace_probe_ops stacktrace_count_probe_ops = {
601 .func = ftrace_stacktrace_count,
602 .print = ftrace_stacktrace_print,
603 .init = ftrace_count_init,
604 .free = ftrace_count_free,
607 static struct ftrace_probe_ops dump_probe_ops = {
608 .func = ftrace_dump_probe,
609 .print = ftrace_dump_print,
610 .init = ftrace_count_init,
611 .free = ftrace_count_free,
614 static struct ftrace_probe_ops cpudump_probe_ops = {
615 .func = ftrace_cpudump_probe,
616 .print = ftrace_cpudump_print,
619 static struct ftrace_probe_ops traceon_probe_ops = {
620 .func = ftrace_traceon,
621 .print = ftrace_traceon_print,
624 static struct ftrace_probe_ops traceoff_probe_ops = {
625 .func = ftrace_traceoff,
626 .print = ftrace_traceoff_print,
629 static struct ftrace_probe_ops stacktrace_probe_ops = {
630 .func = ftrace_stacktrace,
631 .print = ftrace_stacktrace_print,
635 ftrace_trace_probe_callback(struct trace_array *tr,
636 struct ftrace_probe_ops *ops,
637 struct ftrace_hash *hash, char *glob,
638 char *cmd, char *param, int enable)
640 void *count = (void *)-1;
644 /* hash funcs only work with set_ftrace_filter */
649 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
654 number = strsep(¶m, ":");
660 * We use the callback data field (which is a pointer)
663 ret = kstrtoul(number, 0, (unsigned long *)&count);
668 ret = register_ftrace_function_probe(glob, tr, ops, count);
670 return ret < 0 ? ret : 0;
674 ftrace_trace_onoff_callback(struct trace_array *tr, struct ftrace_hash *hash,
675 char *glob, char *cmd, char *param, int enable)
677 struct ftrace_probe_ops *ops;
682 /* we register both traceon and traceoff to this callback */
683 if (strcmp(cmd, "traceon") == 0)
684 ops = param ? &traceon_count_probe_ops : &traceon_probe_ops;
686 ops = param ? &traceoff_count_probe_ops : &traceoff_probe_ops;
688 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
693 ftrace_stacktrace_callback(struct trace_array *tr, struct ftrace_hash *hash,
694 char *glob, char *cmd, char *param, int enable)
696 struct ftrace_probe_ops *ops;
701 ops = param ? &stacktrace_count_probe_ops : &stacktrace_probe_ops;
703 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
708 ftrace_dump_callback(struct trace_array *tr, struct ftrace_hash *hash,
709 char *glob, char *cmd, char *param, int enable)
711 struct ftrace_probe_ops *ops;
716 ops = &dump_probe_ops;
718 /* Only dump once. */
719 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
724 ftrace_cpudump_callback(struct trace_array *tr, struct ftrace_hash *hash,
725 char *glob, char *cmd, char *param, int enable)
727 struct ftrace_probe_ops *ops;
732 ops = &cpudump_probe_ops;
734 /* Only dump once. */
735 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
739 static struct ftrace_func_command ftrace_traceon_cmd = {
741 .func = ftrace_trace_onoff_callback,
744 static struct ftrace_func_command ftrace_traceoff_cmd = {
746 .func = ftrace_trace_onoff_callback,
749 static struct ftrace_func_command ftrace_stacktrace_cmd = {
750 .name = "stacktrace",
751 .func = ftrace_stacktrace_callback,
754 static struct ftrace_func_command ftrace_dump_cmd = {
756 .func = ftrace_dump_callback,
759 static struct ftrace_func_command ftrace_cpudump_cmd = {
761 .func = ftrace_cpudump_callback,
764 static int __init init_func_cmd_traceon(void)
768 ret = register_ftrace_command(&ftrace_traceoff_cmd);
772 ret = register_ftrace_command(&ftrace_traceon_cmd);
774 goto out_free_traceoff;
776 ret = register_ftrace_command(&ftrace_stacktrace_cmd);
778 goto out_free_traceon;
780 ret = register_ftrace_command(&ftrace_dump_cmd);
782 goto out_free_stacktrace;
784 ret = register_ftrace_command(&ftrace_cpudump_cmd);
791 unregister_ftrace_command(&ftrace_dump_cmd);
793 unregister_ftrace_command(&ftrace_stacktrace_cmd);
795 unregister_ftrace_command(&ftrace_traceon_cmd);
797 unregister_ftrace_command(&ftrace_traceoff_cmd);
802 static inline int init_func_cmd_traceon(void)
806 #endif /* CONFIG_DYNAMIC_FTRACE */
808 __init int init_function_trace(void)
810 init_func_cmd_traceon();
811 return register_tracer(&function_trace);