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 |
| 10 | * Copyright (C) 2004 William Lee Irwin III |
| 11 | */ |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 12 | #include <linux/debugfs.h> |
| 13 | #include <linux/uaccess.h> |
| 14 | #include <linux/ftrace.h> |
Ingo Molnar | 2e0f576 | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 15 | #include <linux/fs.h> |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 16 | |
| 17 | #include "trace.h" |
| 18 | |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 19 | static struct trace_array *func_trace; |
| 20 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 21 | static void start_function_trace(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 22 | { |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 23 | func_trace = tr; |
Steven Rostedt | 26bc83f | 2008-07-10 20:58:14 -0400 | [diff] [blame] | 24 | tr->cpu = get_cpu(); |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 25 | tracing_reset_online_cpus(tr); |
Steven Rostedt | 26bc83f | 2008-07-10 20:58:14 -0400 | [diff] [blame] | 26 | put_cpu(); |
| 27 | |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 28 | tracing_start_cmdline_record(); |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 29 | tracing_start_function_trace(); |
| 30 | } |
| 31 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 32 | static void stop_function_trace(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 33 | { |
| 34 | tracing_stop_function_trace(); |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 35 | tracing_stop_cmdline_record(); |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 38 | static int function_trace_init(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 39 | { |
Steven Rostedt | c76f069 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 40 | start_function_trace(tr); |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 41 | return 0; |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 44 | static void function_trace_reset(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 45 | { |
Steven Rostedt | c76f069 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 46 | stop_function_trace(tr); |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 49 | static void function_trace_start(struct trace_array *tr) |
| 50 | { |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 51 | tracing_reset_online_cpus(tr); |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 54 | static void |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 55 | function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip) |
| 56 | { |
| 57 | struct trace_array *tr = func_trace; |
| 58 | struct trace_array_cpu *data; |
| 59 | unsigned long flags; |
| 60 | long disabled; |
| 61 | int cpu, resched; |
| 62 | int pc; |
| 63 | |
| 64 | if (unlikely(!ftrace_function_enabled)) |
| 65 | return; |
| 66 | |
| 67 | pc = preempt_count(); |
| 68 | resched = ftrace_preempt_disable(); |
| 69 | local_save_flags(flags); |
| 70 | cpu = raw_smp_processor_id(); |
| 71 | data = tr->data[cpu]; |
| 72 | disabled = atomic_inc_return(&data->disabled); |
| 73 | |
| 74 | if (likely(disabled == 1)) |
| 75 | trace_function(tr, data, ip, parent_ip, flags, pc); |
| 76 | |
| 77 | atomic_dec(&data->disabled); |
| 78 | ftrace_preempt_enable(resched); |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | function_trace_call(unsigned long ip, unsigned long parent_ip) |
| 83 | { |
| 84 | struct trace_array *tr = func_trace; |
| 85 | struct trace_array_cpu *data; |
| 86 | unsigned long flags; |
| 87 | long disabled; |
| 88 | int cpu; |
| 89 | int pc; |
| 90 | |
| 91 | if (unlikely(!ftrace_function_enabled)) |
| 92 | return; |
| 93 | |
| 94 | /* |
| 95 | * Need to use raw, since this must be called before the |
| 96 | * recursive protection is performed. |
| 97 | */ |
| 98 | local_irq_save(flags); |
| 99 | cpu = raw_smp_processor_id(); |
| 100 | data = tr->data[cpu]; |
| 101 | disabled = atomic_inc_return(&data->disabled); |
| 102 | |
| 103 | if (likely(disabled == 1)) { |
| 104 | pc = preempt_count(); |
| 105 | trace_function(tr, data, ip, parent_ip, flags, pc); |
| 106 | } |
| 107 | |
| 108 | atomic_dec(&data->disabled); |
| 109 | local_irq_restore(flags); |
| 110 | } |
| 111 | |
| 112 | static void |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 113 | function_stack_trace_call(unsigned long ip, unsigned long parent_ip) |
| 114 | { |
| 115 | struct trace_array *tr = func_trace; |
| 116 | struct trace_array_cpu *data; |
| 117 | unsigned long flags; |
| 118 | long disabled; |
| 119 | int cpu; |
| 120 | int pc; |
| 121 | |
| 122 | if (unlikely(!ftrace_function_enabled)) |
| 123 | return; |
| 124 | |
| 125 | /* |
| 126 | * Need to use raw, since this must be called before the |
| 127 | * recursive protection is performed. |
| 128 | */ |
| 129 | local_irq_save(flags); |
| 130 | cpu = raw_smp_processor_id(); |
| 131 | data = tr->data[cpu]; |
| 132 | disabled = atomic_inc_return(&data->disabled); |
| 133 | |
| 134 | if (likely(disabled == 1)) { |
| 135 | pc = preempt_count(); |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame^] | 136 | trace_function(tr, data, ip, parent_ip, flags, pc); |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 137 | /* |
| 138 | * skip over 5 funcs: |
| 139 | * __ftrace_trace_stack, |
| 140 | * __trace_stack, |
| 141 | * function_stack_trace_call |
| 142 | * ftrace_list_func |
| 143 | * ftrace_call |
| 144 | */ |
| 145 | __trace_stack(tr, data, flags, 5, pc); |
| 146 | } |
| 147 | |
| 148 | atomic_dec(&data->disabled); |
| 149 | local_irq_restore(flags); |
| 150 | } |
| 151 | |
Steven Rostedt | bb3c3c9 | 2009-01-15 20:40:23 -0500 | [diff] [blame] | 152 | |
| 153 | static struct ftrace_ops trace_ops __read_mostly = |
| 154 | { |
| 155 | .func = function_trace_call, |
| 156 | }; |
| 157 | |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 158 | static struct ftrace_ops trace_stack_ops __read_mostly = |
| 159 | { |
| 160 | .func = function_stack_trace_call, |
| 161 | }; |
| 162 | |
| 163 | /* Our two options */ |
| 164 | enum { |
| 165 | TRACE_FUNC_OPT_STACK = 0x1, |
| 166 | }; |
| 167 | |
| 168 | static struct tracer_opt func_opts[] = { |
| 169 | #ifdef CONFIG_STACKTRACE |
| 170 | { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) }, |
| 171 | #endif |
| 172 | { } /* Always set a last empty entry */ |
| 173 | }; |
| 174 | |
| 175 | static struct tracer_flags func_flags = { |
| 176 | .val = 0, /* By default: all flags disabled */ |
| 177 | .opts = func_opts |
| 178 | }; |
| 179 | |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame^] | 180 | void tracing_start_function_trace(void) |
| 181 | { |
| 182 | ftrace_function_enabled = 0; |
| 183 | |
| 184 | if (trace_flags & TRACE_ITER_PREEMPTONLY) |
| 185 | trace_ops.func = function_trace_call_preempt_only; |
| 186 | else |
| 187 | trace_ops.func = function_trace_call; |
| 188 | |
| 189 | if (func_flags.val & TRACE_FUNC_OPT_STACK) |
| 190 | register_ftrace_function(&trace_stack_ops); |
| 191 | else |
| 192 | register_ftrace_function(&trace_ops); |
| 193 | |
| 194 | ftrace_function_enabled = 1; |
| 195 | } |
| 196 | |
| 197 | void tracing_stop_function_trace(void) |
| 198 | { |
| 199 | ftrace_function_enabled = 0; |
| 200 | /* OK if they are not registered */ |
| 201 | unregister_ftrace_function(&trace_stack_ops); |
| 202 | unregister_ftrace_function(&trace_ops); |
| 203 | } |
| 204 | |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 205 | static int func_set_flag(u32 old_flags, u32 bit, int set) |
| 206 | { |
| 207 | if (bit == TRACE_FUNC_OPT_STACK) { |
| 208 | /* do nothing if already set */ |
| 209 | if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK)) |
| 210 | return 0; |
| 211 | |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame^] | 212 | if (set) { |
| 213 | unregister_ftrace_function(&trace_ops); |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 214 | register_ftrace_function(&trace_stack_ops); |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame^] | 215 | } else { |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 216 | unregister_ftrace_function(&trace_stack_ops); |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame^] | 217 | register_ftrace_function(&trace_ops); |
| 218 | } |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 226 | static struct tracer function_trace __read_mostly = |
| 227 | { |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame^] | 228 | .name = "function", |
| 229 | .init = function_trace_init, |
| 230 | .reset = function_trace_reset, |
| 231 | .start = function_trace_start, |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 232 | .flags = &func_flags, |
| 233 | .set_flag = func_set_flag, |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 234 | #ifdef CONFIG_FTRACE_SELFTEST |
Steven Rostedt | 3eb36aa | 2009-01-15 22:21:43 -0500 | [diff] [blame^] | 235 | .selftest = trace_selftest_startup_function, |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 236 | #endif |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | static __init int init_function_trace(void) |
| 240 | { |
| 241 | return register_tracer(&function_trace); |
| 242 | } |
| 243 | |
| 244 | device_initcall(init_function_trace); |