Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 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 |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 10 | * Copyright (C) 2004 Nadia Yvette Chambers |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 11 | */ |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 12 | #include <linux/ring_buffer.h> |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 13 | #include <linux/debugfs.h> |
| 14 | #include <linux/uaccess.h> |
| 15 | #include <linux/ftrace.h> |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 16 | #include <linux/slab.h> |
Ingo Molnar | 2e0f576 | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 17 | #include <linux/fs.h> |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 18 | |
| 19 | #include "trace.h" |
| 20 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 21 | static void tracing_start_function_trace(struct trace_array *tr); |
| 22 | static void tracing_stop_function_trace(struct trace_array *tr); |
| 23 | static void |
| 24 | function_trace_call(unsigned long ip, unsigned long parent_ip, |
| 25 | struct ftrace_ops *op, struct pt_regs *pt_regs); |
| 26 | static void |
| 27 | function_stack_trace_call(unsigned long ip, unsigned long parent_ip, |
| 28 | struct ftrace_ops *op, struct pt_regs *pt_regs); |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 29 | static struct tracer_flags func_flags; |
Steven Rostedt | a225cdd | 2009-01-15 23:06:03 -0500 | [diff] [blame] | 30 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 31 | /* Our option */ |
| 32 | enum { |
| 33 | TRACE_FUNC_OPT_STACK = 0x1, |
| 34 | }; |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 35 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 36 | static int allocate_ftrace_ops(struct trace_array *tr) |
| 37 | { |
| 38 | struct ftrace_ops *ops; |
| 39 | |
| 40 | ops = kzalloc(sizeof(*ops), GFP_KERNEL); |
| 41 | if (!ops) |
| 42 | return -ENOMEM; |
| 43 | |
| 44 | /* Currently only the non stack verision is supported */ |
| 45 | ops->func = function_trace_call; |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 46 | ops->flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_PID; |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 47 | |
| 48 | tr->ops = ops; |
| 49 | ops->private = tr; |
| 50 | return 0; |
| 51 | } |
Steven Rostedt | a225cdd | 2009-01-15 23:06:03 -0500 | [diff] [blame] | 52 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 53 | |
| 54 | int ftrace_create_function_files(struct trace_array *tr, |
| 55 | struct dentry *parent) |
| 56 | { |
| 57 | int ret; |
| 58 | |
Steven Rostedt (Red Hat) | 5d6c97c | 2014-04-16 19:21:53 -0400 | [diff] [blame] | 59 | /* |
| 60 | * The top level array uses the "global_ops", and the files are |
| 61 | * created on boot up. |
| 62 | */ |
| 63 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| 64 | return 0; |
| 65 | |
| 66 | ret = allocate_ftrace_ops(tr); |
| 67 | if (ret) |
| 68 | return ret; |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 69 | |
| 70 | ftrace_create_filter_files(tr->ops, parent); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | void ftrace_destroy_function_files(struct trace_array *tr) |
| 76 | { |
| 77 | ftrace_destroy_filter_files(tr->ops); |
| 78 | kfree(tr->ops); |
| 79 | tr->ops = NULL; |
| 80 | } |
| 81 | |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 82 | static int function_trace_init(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 83 | { |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 84 | ftrace_func_t func; |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 85 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 86 | /* |
| 87 | * Instance trace_arrays get their ops allocated |
| 88 | * at instance creation. Unless it failed |
| 89 | * the allocation. |
| 90 | */ |
| 91 | if (!tr->ops) |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 92 | return -ENOMEM; |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 93 | |
| 94 | /* Currently only the global instance can do stack tracing */ |
| 95 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL && |
| 96 | func_flags.val & TRACE_FUNC_OPT_STACK) |
| 97 | func = function_stack_trace_call; |
| 98 | else |
| 99 | func = function_trace_call; |
| 100 | |
| 101 | ftrace_init_array_ops(tr, func); |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 102 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 103 | tr->trace_buffer.cpu = get_cpu(); |
Steven Rostedt | 26bc83f | 2008-07-10 20:58:14 -0400 | [diff] [blame] | 104 | put_cpu(); |
| 105 | |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 106 | tracing_start_cmdline_record(); |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 107 | tracing_start_function_trace(tr); |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 108 | return 0; |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 111 | static void function_trace_reset(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 112 | { |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 113 | tracing_stop_function_trace(tr); |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 114 | tracing_stop_cmdline_record(); |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 115 | ftrace_reset_array_ops(tr); |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 118 | static void function_trace_start(struct trace_array *tr) |
| 119 | { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 120 | tracing_reset_online_cpus(&tr->trace_buffer); |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 123 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 124 | function_trace_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 125 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 126 | { |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 127 | struct trace_array *tr = op->private; |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 128 | struct trace_array_cpu *data; |
| 129 | unsigned long flags; |
Steven Rostedt | d41032a | 2013-01-24 07:52:34 -0500 | [diff] [blame] | 130 | int bit; |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 131 | int cpu; |
| 132 | int pc; |
| 133 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 134 | if (unlikely(!tr->function_enabled)) |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 135 | return; |
| 136 | |
Steven Rostedt | 897f68a | 2012-11-02 17:52:35 -0400 | [diff] [blame] | 137 | pc = preempt_count(); |
| 138 | preempt_disable_notrace(); |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 139 | |
Steven Rostedt | 897f68a | 2012-11-02 17:52:35 -0400 | [diff] [blame] | 140 | bit = trace_test_and_set_recursion(TRACE_FTRACE_START, TRACE_FTRACE_MAX); |
| 141 | if (bit < 0) |
| 142 | goto out; |
| 143 | |
| 144 | cpu = smp_processor_id(); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 145 | data = per_cpu_ptr(tr->trace_buffer.data, cpu); |
Steven Rostedt | 897f68a | 2012-11-02 17:52:35 -0400 | [diff] [blame] | 146 | if (!atomic_read(&data->disabled)) { |
| 147 | local_save_flags(flags); |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 148 | trace_function(tr, ip, parent_ip, flags, pc); |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 149 | } |
Steven Rostedt | 897f68a | 2012-11-02 17:52:35 -0400 | [diff] [blame] | 150 | trace_clear_recursion(bit); |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 151 | |
Steven Rostedt | 897f68a | 2012-11-02 17:52:35 -0400 | [diff] [blame] | 152 | out: |
| 153 | preempt_enable_notrace(); |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 157 | function_stack_trace_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 158 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 159 | { |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 160 | struct trace_array *tr = op->private; |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 161 | struct trace_array_cpu *data; |
| 162 | unsigned long flags; |
| 163 | long disabled; |
| 164 | int cpu; |
| 165 | int pc; |
| 166 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 167 | if (unlikely(!tr->function_enabled)) |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 168 | return; |
| 169 | |
| 170 | /* |
| 171 | * Need to use raw, since this must be called before the |
| 172 | * recursive protection is performed. |
| 173 | */ |
| 174 | local_irq_save(flags); |
| 175 | cpu = raw_smp_processor_id(); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 176 | data = per_cpu_ptr(tr->trace_buffer.data, cpu); |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 177 | disabled = atomic_inc_return(&data->disabled); |
| 178 | |
| 179 | if (likely(disabled == 1)) { |
| 180 | pc = preempt_count(); |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 181 | trace_function(tr, ip, parent_ip, flags, pc); |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 182 | /* |
| 183 | * skip over 5 funcs: |
| 184 | * __ftrace_trace_stack, |
| 185 | * __trace_stack, |
| 186 | * function_stack_trace_call |
| 187 | * ftrace_list_func |
| 188 | * ftrace_call |
| 189 | */ |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 190 | __trace_stack(tr, flags, 5, pc); |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | atomic_dec(&data->disabled); |
| 194 | local_irq_restore(flags); |
| 195 | } |
| 196 | |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 197 | static struct tracer_opt func_opts[] = { |
| 198 | #ifdef CONFIG_STACKTRACE |
| 199 | { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) }, |
| 200 | #endif |
| 201 | { } /* Always set a last empty entry */ |
| 202 | }; |
| 203 | |
| 204 | static struct tracer_flags func_flags = { |
| 205 | .val = 0, /* By default: all flags disabled */ |
| 206 | .opts = func_opts |
| 207 | }; |
| 208 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 209 | static void tracing_start_function_trace(struct trace_array *tr) |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 210 | { |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 211 | tr->function_enabled = 0; |
| 212 | register_ftrace_function(tr->ops); |
| 213 | tr->function_enabled = 1; |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 216 | static void tracing_stop_function_trace(struct trace_array *tr) |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 217 | { |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 218 | tr->function_enabled = 0; |
| 219 | unregister_ftrace_function(tr->ops); |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 220 | } |
| 221 | |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 222 | static struct tracer function_trace; |
| 223 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 224 | static int |
| 225 | func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 226 | { |
Anton Vorontsov | f555f12 | 2012-07-09 17:10:46 -0700 | [diff] [blame] | 227 | switch (bit) { |
| 228 | case TRACE_FUNC_OPT_STACK: |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 229 | /* do nothing if already set */ |
| 230 | if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK)) |
Anton Vorontsov | f555f12 | 2012-07-09 17:10:46 -0700 | [diff] [blame] | 231 | break; |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 232 | |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 233 | /* We can change this flag when not running. */ |
| 234 | if (tr->current_trace != &function_trace) |
| 235 | break; |
| 236 | |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 237 | unregister_ftrace_function(tr->ops); |
| 238 | |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 239 | if (set) { |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 240 | tr->ops->func = function_stack_trace_call; |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 241 | register_ftrace_function(tr->ops); |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 242 | } else { |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 243 | tr->ops->func = function_trace_call; |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 244 | register_ftrace_function(tr->ops); |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 245 | } |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 246 | |
Anton Vorontsov | f555f12 | 2012-07-09 17:10:46 -0700 | [diff] [blame] | 247 | break; |
Anton Vorontsov | f555f12 | 2012-07-09 17:10:46 -0700 | [diff] [blame] | 248 | default: |
| 249 | return -EINVAL; |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Anton Vorontsov | f555f12 | 2012-07-09 17:10:46 -0700 | [diff] [blame] | 252 | return 0; |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Steven Rostedt (Red Hat) | 8f76899 | 2013-07-18 14:41:51 -0400 | [diff] [blame] | 255 | static struct tracer function_trace __tracer_data = |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 256 | { |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 257 | .name = "function", |
| 258 | .init = function_trace_init, |
| 259 | .reset = function_trace_reset, |
| 260 | .start = function_trace_start, |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 261 | .flags = &func_flags, |
| 262 | .set_flag = func_set_flag, |
Steven Rostedt (Red Hat) | f20a580 | 2013-11-07 20:08:58 -0500 | [diff] [blame] | 263 | .allow_instances = true, |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 264 | #ifdef CONFIG_FTRACE_SELFTEST |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame] | 265 | .selftest = trace_selftest_startup_function, |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 266 | #endif |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 267 | }; |
| 268 | |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 269 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 270 | static void update_traceon_count(struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 271 | unsigned long ip, |
| 272 | struct trace_array *tr, bool on, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 273 | void *data) |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 274 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 275 | struct ftrace_func_mapper *mapper = data; |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 276 | long *count; |
| 277 | long old_count; |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 278 | |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 279 | /* |
| 280 | * Tracing gets disabled (or enabled) once per count. |
Steven Rostedt (Red Hat) | 0af2649 | 2014-11-20 10:05:36 -0500 | [diff] [blame] | 281 | * This function can be called at the same time on multiple CPUs. |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 282 | * It is fine if both disable (or enable) tracing, as disabling |
| 283 | * (or enabling) the second time doesn't do anything as the |
| 284 | * state of the tracer is already disabled (or enabled). |
| 285 | * What needs to be synchronized in this case is that the count |
| 286 | * only gets decremented once, even if the tracer is disabled |
| 287 | * (or enabled) twice, as the second one is really a nop. |
| 288 | * |
| 289 | * The memory barriers guarantee that we only decrement the |
| 290 | * counter once. First the count is read to a local variable |
| 291 | * and a read barrier is used to make sure that it is loaded |
| 292 | * before checking if the tracer is in the state we want. |
| 293 | * If the tracer is not in the state we want, then the count |
| 294 | * is guaranteed to be the old count. |
| 295 | * |
| 296 | * Next the tracer is set to the state we want (disabled or enabled) |
| 297 | * then a write memory barrier is used to make sure that |
| 298 | * the new state is visible before changing the counter by |
| 299 | * one minus the old counter. This guarantees that another CPU |
| 300 | * executing this code will see the new state before seeing |
Steven Rostedt (Red Hat) | 0af2649 | 2014-11-20 10:05:36 -0500 | [diff] [blame] | 301 | * the new counter value, and would not do anything if the new |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 302 | * counter is seen. |
| 303 | * |
| 304 | * Note, there is no synchronization between this and a user |
| 305 | * setting the tracing_on file. But we currently don't care |
| 306 | * about that. |
| 307 | */ |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 308 | count = (long *)ftrace_func_mapper_find_ip(mapper, ip); |
| 309 | old_count = *count; |
| 310 | |
| 311 | if (old_count <= 0) |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 312 | return; |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 313 | |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 314 | /* Make sure we see count before checking tracing state */ |
| 315 | smp_rmb(); |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 316 | |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 317 | if (on == !!tracer_tracing_is_on(tr)) |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 318 | return; |
| 319 | |
| 320 | if (on) |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 321 | tracer_tracing_on(tr); |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 322 | else |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 323 | tracer_tracing_off(tr); |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 324 | |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 325 | /* Make sure tracing state is visible before updating count */ |
| 326 | smp_wmb(); |
| 327 | |
| 328 | *count = old_count - 1; |
Steven Rostedt (Red Hat) | 1c31714 | 2013-03-09 08:36:53 -0500 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 332 | ftrace_traceon_count(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 333 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 334 | void *data) |
Steven Rostedt (Red Hat) | 1c31714 | 2013-03-09 08:36:53 -0500 | [diff] [blame] | 335 | { |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 336 | update_traceon_count(ops, ip, tr, 1, data); |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 340 | ftrace_traceoff_count(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 341 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 342 | void *data) |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 343 | { |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 344 | update_traceon_count(ops, ip, tr, 0, data); |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 347 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 348 | ftrace_traceon(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 349 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 350 | void *data) |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 351 | { |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 352 | if (tracer_tracing_is_on(tr)) |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 353 | return; |
| 354 | |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 355 | tracer_tracing_on(tr); |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 359 | ftrace_traceoff(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 360 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 361 | void *data) |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 362 | { |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 363 | if (!tracer_tracing_is_on(tr)) |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 364 | return; |
| 365 | |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 366 | tracer_tracing_off(tr); |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 367 | } |
| 368 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 369 | /* |
| 370 | * Skip 4: |
| 371 | * ftrace_stacktrace() |
| 372 | * function_trace_probe_call() |
| 373 | * ftrace_ops_list_func() |
| 374 | * ftrace_call() |
| 375 | */ |
| 376 | #define STACK_SKIP 4 |
Steven Rostedt | e110e3d | 2009-02-16 23:38:13 -0500 | [diff] [blame] | 377 | |
Steven Rostedt (VMware) | dcc19d2 | 2017-04-20 11:59:18 -0400 | [diff] [blame] | 378 | static __always_inline void trace_stack(struct trace_array *tr) |
| 379 | { |
| 380 | unsigned long flags; |
| 381 | int pc; |
| 382 | |
| 383 | local_save_flags(flags); |
| 384 | pc = preempt_count(); |
| 385 | |
| 386 | __trace_stack(tr, flags, STACK_SKIP, pc); |
| 387 | } |
| 388 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 389 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 390 | ftrace_stacktrace(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 391 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 392 | void *data) |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 393 | { |
Steven Rostedt (VMware) | dcc19d2 | 2017-04-20 11:59:18 -0400 | [diff] [blame] | 394 | trace_stack(tr); |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 395 | } |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 396 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 397 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 398 | ftrace_stacktrace_count(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 399 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 400 | void *data) |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 401 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 402 | struct ftrace_func_mapper *mapper = data; |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 403 | long *count; |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 404 | long old_count; |
| 405 | long new_count; |
Steven Rostedt (Red Hat) | 8380d24 | 2013-03-09 08:56:43 -0500 | [diff] [blame] | 406 | |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 407 | if (!tracing_is_on()) |
| 408 | return; |
| 409 | |
| 410 | /* unlimited? */ |
| 411 | if (!mapper) { |
Steven Rostedt (VMware) | dcc19d2 | 2017-04-20 11:59:18 -0400 | [diff] [blame] | 412 | trace_stack(tr); |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | count = (long *)ftrace_func_mapper_find_ip(mapper, ip); |
| 417 | |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 418 | /* |
| 419 | * Stack traces should only execute the number of times the |
| 420 | * user specified in the counter. |
| 421 | */ |
| 422 | do { |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 423 | old_count = *count; |
| 424 | |
| 425 | if (!old_count) |
| 426 | return; |
| 427 | |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 428 | new_count = old_count - 1; |
| 429 | new_count = cmpxchg(count, old_count, new_count); |
| 430 | if (new_count == old_count) |
Steven Rostedt (VMware) | dcc19d2 | 2017-04-20 11:59:18 -0400 | [diff] [blame] | 431 | trace_stack(tr); |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 432 | |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 433 | if (!tracing_is_on()) |
| 434 | return; |
| 435 | |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 436 | } while (new_count != old_count); |
| 437 | } |
| 438 | |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 439 | static int update_count(struct ftrace_probe_ops *ops, unsigned long ip, |
| 440 | void *data) |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 441 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 442 | struct ftrace_func_mapper *mapper = data; |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 443 | long *count = NULL; |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 444 | |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 445 | if (mapper) |
| 446 | count = (long *)ftrace_func_mapper_find_ip(mapper, ip); |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 447 | |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 448 | if (count) { |
| 449 | if (*count <= 0) |
| 450 | return 0; |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 451 | (*count)--; |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 452 | } |
Steven Rostedt (Red Hat) | a9ce7c3 | 2014-11-17 23:08:24 -0500 | [diff] [blame] | 453 | |
| 454 | return 1; |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 455 | } |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 456 | |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 457 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 458 | ftrace_dump_probe(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 459 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 460 | void *data) |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 461 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 462 | if (update_count(ops, ip, data)) |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 463 | ftrace_dump(DUMP_ALL); |
| 464 | } |
| 465 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 466 | /* Only dump the current CPU buffer. */ |
| 467 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 468 | ftrace_cpudump_probe(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 469 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 470 | void *data) |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 471 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 472 | if (update_count(ops, ip, data)) |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 473 | ftrace_dump(DUMP_ORIG); |
| 474 | } |
| 475 | |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 476 | static int |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 477 | ftrace_probe_print(const char *name, struct seq_file *m, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 478 | unsigned long ip, struct ftrace_probe_ops *ops, |
| 479 | void *data) |
Steven Rostedt | e110e3d | 2009-02-16 23:38:13 -0500 | [diff] [blame] | 480 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 481 | struct ftrace_func_mapper *mapper = data; |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 482 | long *count = NULL; |
Steven Rostedt | e110e3d | 2009-02-16 23:38:13 -0500 | [diff] [blame] | 483 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 484 | seq_printf(m, "%ps:%s", (void *)ip, name); |
Steven Rostedt | e110e3d | 2009-02-16 23:38:13 -0500 | [diff] [blame] | 485 | |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 486 | if (mapper) |
| 487 | count = (long *)ftrace_func_mapper_find_ip(mapper, ip); |
| 488 | |
| 489 | if (count) |
| 490 | seq_printf(m, ":count=%ld\n", *count); |
Steven Rostedt | 35ebf1c | 2009-02-17 13:12:12 -0500 | [diff] [blame] | 491 | else |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 492 | seq_puts(m, ":unlimited\n"); |
Steven Rostedt | e110e3d | 2009-02-16 23:38:13 -0500 | [diff] [blame] | 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static int |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 498 | ftrace_traceon_print(struct seq_file *m, unsigned long ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 499 | struct ftrace_probe_ops *ops, |
| 500 | void *data) |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 501 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 502 | return ftrace_probe_print("traceon", m, ip, ops, data); |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static int |
| 506 | ftrace_traceoff_print(struct seq_file *m, unsigned long ip, |
| 507 | struct ftrace_probe_ops *ops, void *data) |
| 508 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 509 | return ftrace_probe_print("traceoff", m, ip, ops, data); |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static int |
| 513 | ftrace_stacktrace_print(struct seq_file *m, unsigned long ip, |
| 514 | struct ftrace_probe_ops *ops, void *data) |
| 515 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 516 | return ftrace_probe_print("stacktrace", m, ip, ops, data); |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 519 | static int |
| 520 | ftrace_dump_print(struct seq_file *m, unsigned long ip, |
| 521 | struct ftrace_probe_ops *ops, void *data) |
| 522 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 523 | return ftrace_probe_print("dump", m, ip, ops, data); |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 524 | } |
| 525 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 526 | static int |
| 527 | ftrace_cpudump_print(struct seq_file *m, unsigned long ip, |
| 528 | struct ftrace_probe_ops *ops, void *data) |
| 529 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 530 | return ftrace_probe_print("cpudump", m, ip, ops, data); |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
| 534 | static int |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 535 | ftrace_count_init(struct ftrace_probe_ops *ops, struct trace_array *tr, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 536 | unsigned long ip, void *init_data, void **data) |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 537 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 538 | struct ftrace_func_mapper *mapper = *data; |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 539 | |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 540 | if (!mapper) { |
| 541 | mapper = allocate_ftrace_func_mapper(); |
| 542 | if (!mapper) |
| 543 | return -ENOMEM; |
| 544 | *data = mapper; |
| 545 | } |
| 546 | |
| 547 | return ftrace_func_mapper_add_ip(mapper, ip, init_data); |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static void |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 551 | ftrace_count_free(struct ftrace_probe_ops *ops, struct trace_array *tr, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 552 | unsigned long ip, void *data) |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 553 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 554 | struct ftrace_func_mapper *mapper = data; |
| 555 | |
| 556 | if (!ip) { |
| 557 | free_ftrace_func_mapper(mapper, NULL); |
| 558 | return; |
| 559 | } |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 560 | |
| 561 | ftrace_func_mapper_remove_ip(mapper, ip); |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 564 | static struct ftrace_probe_ops traceon_count_probe_ops = { |
| 565 | .func = ftrace_traceon_count, |
| 566 | .print = ftrace_traceon_print, |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 567 | .init = ftrace_count_init, |
| 568 | .free = ftrace_count_free, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | static struct ftrace_probe_ops traceoff_count_probe_ops = { |
| 572 | .func = ftrace_traceoff_count, |
| 573 | .print = ftrace_traceoff_print, |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 574 | .init = ftrace_count_init, |
| 575 | .free = ftrace_count_free, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | static struct ftrace_probe_ops stacktrace_count_probe_ops = { |
| 579 | .func = ftrace_stacktrace_count, |
| 580 | .print = ftrace_stacktrace_print, |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 581 | .init = ftrace_count_init, |
| 582 | .free = ftrace_count_free, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 583 | }; |
| 584 | |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 585 | static struct ftrace_probe_ops dump_probe_ops = { |
| 586 | .func = ftrace_dump_probe, |
| 587 | .print = ftrace_dump_print, |
Steven Rostedt (VMware) | fe014e2 | 2017-04-03 23:22:41 -0400 | [diff] [blame] | 588 | .init = ftrace_count_init, |
| 589 | .free = ftrace_count_free, |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 590 | }; |
| 591 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 592 | static struct ftrace_probe_ops cpudump_probe_ops = { |
| 593 | .func = ftrace_cpudump_probe, |
| 594 | .print = ftrace_cpudump_print, |
| 595 | }; |
| 596 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 597 | static struct ftrace_probe_ops traceon_probe_ops = { |
| 598 | .func = ftrace_traceon, |
| 599 | .print = ftrace_traceon_print, |
| 600 | }; |
| 601 | |
| 602 | static struct ftrace_probe_ops traceoff_probe_ops = { |
| 603 | .func = ftrace_traceoff, |
| 604 | .print = ftrace_traceoff_print, |
| 605 | }; |
| 606 | |
| 607 | static struct ftrace_probe_ops stacktrace_probe_ops = { |
| 608 | .func = ftrace_stacktrace, |
| 609 | .print = ftrace_stacktrace_print, |
| 610 | }; |
| 611 | |
| 612 | static int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 613 | ftrace_trace_probe_callback(struct trace_array *tr, |
| 614 | struct ftrace_probe_ops *ops, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 615 | struct ftrace_hash *hash, char *glob, |
| 616 | char *cmd, char *param, int enable) |
| 617 | { |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 618 | void *count = (void *)-1; |
| 619 | char *number; |
| 620 | int ret; |
| 621 | |
| 622 | /* hash funcs only work with set_ftrace_filter */ |
| 623 | if (!enable) |
| 624 | return -EINVAL; |
| 625 | |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 626 | if (glob[0] == '!') |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 627 | return unregister_ftrace_function_probe_func(glob+1, tr, ops); |
Steven Rostedt (Red Hat) | 8b8fa62 | 2013-03-12 09:25:00 -0400 | [diff] [blame] | 628 | |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 629 | if (!param) |
| 630 | goto out_reg; |
| 631 | |
| 632 | number = strsep(¶m, ":"); |
| 633 | |
| 634 | if (!strlen(number)) |
| 635 | goto out_reg; |
| 636 | |
| 637 | /* |
| 638 | * We use the callback data field (which is a pointer) |
| 639 | * as our counter. |
| 640 | */ |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 641 | ret = kstrtoul(number, 0, (unsigned long *)&count); |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 642 | if (ret) |
| 643 | return ret; |
| 644 | |
| 645 | out_reg: |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 646 | ret = register_ftrace_function_probe(glob, tr, ops, count); |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 647 | |
Xiao Guangrong | 04aef32 | 2009-07-15 12:29:06 +0800 | [diff] [blame] | 648 | return ret < 0 ? ret : 0; |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 649 | } |
| 650 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 651 | static int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 652 | ftrace_trace_onoff_callback(struct trace_array *tr, struct ftrace_hash *hash, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 653 | char *glob, char *cmd, char *param, int enable) |
| 654 | { |
| 655 | struct ftrace_probe_ops *ops; |
| 656 | |
Steven Rostedt (VMware) | 0f17976 | 2017-06-29 10:05:45 -0400 | [diff] [blame] | 657 | if (!tr) |
| 658 | return -ENODEV; |
| 659 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 660 | /* we register both traceon and traceoff to this callback */ |
| 661 | if (strcmp(cmd, "traceon") == 0) |
| 662 | ops = param ? &traceon_count_probe_ops : &traceon_probe_ops; |
| 663 | else |
| 664 | ops = param ? &traceoff_count_probe_ops : &traceoff_probe_ops; |
| 665 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 666 | return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 667 | param, enable); |
| 668 | } |
| 669 | |
| 670 | static int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 671 | ftrace_stacktrace_callback(struct trace_array *tr, struct ftrace_hash *hash, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 672 | char *glob, char *cmd, char *param, int enable) |
| 673 | { |
| 674 | struct ftrace_probe_ops *ops; |
| 675 | |
Steven Rostedt (VMware) | 0f17976 | 2017-06-29 10:05:45 -0400 | [diff] [blame] | 676 | if (!tr) |
| 677 | return -ENODEV; |
| 678 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 679 | ops = param ? &stacktrace_count_probe_ops : &stacktrace_probe_ops; |
| 680 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 681 | return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 682 | param, enable); |
| 683 | } |
| 684 | |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 685 | static int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 686 | ftrace_dump_callback(struct trace_array *tr, struct ftrace_hash *hash, |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 687 | char *glob, char *cmd, char *param, int enable) |
| 688 | { |
| 689 | struct ftrace_probe_ops *ops; |
| 690 | |
Steven Rostedt (VMware) | 0f17976 | 2017-06-29 10:05:45 -0400 | [diff] [blame] | 691 | if (!tr) |
| 692 | return -ENODEV; |
| 693 | |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 694 | ops = &dump_probe_ops; |
| 695 | |
| 696 | /* Only dump once. */ |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 697 | return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 698 | "1", enable); |
| 699 | } |
| 700 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 701 | static int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 702 | ftrace_cpudump_callback(struct trace_array *tr, struct ftrace_hash *hash, |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 703 | char *glob, char *cmd, char *param, int enable) |
| 704 | { |
| 705 | struct ftrace_probe_ops *ops; |
| 706 | |
Steven Rostedt (VMware) | 0f17976 | 2017-06-29 10:05:45 -0400 | [diff] [blame] | 707 | if (!tr) |
| 708 | return -ENODEV; |
| 709 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 710 | ops = &cpudump_probe_ops; |
| 711 | |
| 712 | /* Only dump once. */ |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 713 | return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 714 | "1", enable); |
| 715 | } |
| 716 | |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 717 | static struct ftrace_func_command ftrace_traceon_cmd = { |
| 718 | .name = "traceon", |
| 719 | .func = ftrace_trace_onoff_callback, |
| 720 | }; |
| 721 | |
| 722 | static struct ftrace_func_command ftrace_traceoff_cmd = { |
| 723 | .name = "traceoff", |
| 724 | .func = ftrace_trace_onoff_callback, |
| 725 | }; |
| 726 | |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 727 | static struct ftrace_func_command ftrace_stacktrace_cmd = { |
| 728 | .name = "stacktrace", |
| 729 | .func = ftrace_stacktrace_callback, |
| 730 | }; |
| 731 | |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 732 | static struct ftrace_func_command ftrace_dump_cmd = { |
| 733 | .name = "dump", |
| 734 | .func = ftrace_dump_callback, |
| 735 | }; |
| 736 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 737 | static struct ftrace_func_command ftrace_cpudump_cmd = { |
| 738 | .name = "cpudump", |
| 739 | .func = ftrace_cpudump_callback, |
| 740 | }; |
| 741 | |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 742 | static int __init init_func_cmd_traceon(void) |
| 743 | { |
| 744 | int ret; |
| 745 | |
| 746 | ret = register_ftrace_command(&ftrace_traceoff_cmd); |
| 747 | if (ret) |
| 748 | return ret; |
| 749 | |
| 750 | ret = register_ftrace_command(&ftrace_traceon_cmd); |
| 751 | if (ret) |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 752 | goto out_free_traceoff; |
Steven Rostedt (Red Hat) | dd42cd3 | 2013-03-13 10:17:50 -0400 | [diff] [blame] | 753 | |
| 754 | ret = register_ftrace_command(&ftrace_stacktrace_cmd); |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 755 | if (ret) |
| 756 | goto out_free_traceon; |
| 757 | |
| 758 | ret = register_ftrace_command(&ftrace_dump_cmd); |
| 759 | if (ret) |
| 760 | goto out_free_stacktrace; |
| 761 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 762 | ret = register_ftrace_command(&ftrace_cpudump_cmd); |
| 763 | if (ret) |
| 764 | goto out_free_dump; |
| 765 | |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 766 | return 0; |
| 767 | |
Steven Rostedt (Red Hat) | 90e3c03 | 2013-04-30 19:00:46 -0400 | [diff] [blame] | 768 | out_free_dump: |
| 769 | unregister_ftrace_command(&ftrace_dump_cmd); |
Steven Rostedt (Red Hat) | ad71d88 | 2013-04-30 15:46:14 -0400 | [diff] [blame] | 770 | out_free_stacktrace: |
| 771 | unregister_ftrace_command(&ftrace_stacktrace_cmd); |
| 772 | out_free_traceon: |
| 773 | unregister_ftrace_command(&ftrace_traceon_cmd); |
| 774 | out_free_traceoff: |
| 775 | unregister_ftrace_command(&ftrace_traceoff_cmd); |
| 776 | |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 777 | return ret; |
| 778 | } |
| 779 | #else |
| 780 | static inline int init_func_cmd_traceon(void) |
| 781 | { |
| 782 | return 0; |
| 783 | } |
| 784 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 785 | |
Steven Rostedt (VMware) | dbeafd0 | 2017-03-03 13:48:42 -0500 | [diff] [blame] | 786 | __init int init_function_trace(void) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 787 | { |
Steven Rostedt | 23b4ff3a | 2009-02-14 19:04:24 -0500 | [diff] [blame] | 788 | init_func_cmd_traceon(); |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 789 | return register_tracer(&function_trace); |
| 790 | } |