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 | 26bc83f | 2008-07-10 20:58:14 -0400 | [diff] [blame] | 23 | tr->cpu = get_cpu(); |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 24 | tracing_reset_online_cpus(tr); |
Steven Rostedt | 26bc83f | 2008-07-10 20:58:14 -0400 | [diff] [blame] | 25 | put_cpu(); |
| 26 | |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 27 | tracing_start_cmdline_record(); |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 28 | tracing_start_function_trace(); |
| 29 | } |
| 30 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 31 | static void stop_function_trace(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 32 | { |
| 33 | tracing_stop_function_trace(); |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 34 | tracing_stop_cmdline_record(); |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 37 | static int function_trace_init(struct trace_array *tr) |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 38 | { |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame^] | 39 | func_trace = tr; |
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 |
| 55 | function_stack_trace_call(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; |
| 62 | int pc; |
| 63 | |
| 64 | if (unlikely(!ftrace_function_enabled)) |
| 65 | return; |
| 66 | |
| 67 | /* |
| 68 | * Need to use raw, since this must be called before the |
| 69 | * recursive protection is performed. |
| 70 | */ |
| 71 | local_irq_save(flags); |
| 72 | cpu = raw_smp_processor_id(); |
| 73 | data = tr->data[cpu]; |
| 74 | disabled = atomic_inc_return(&data->disabled); |
| 75 | |
| 76 | if (likely(disabled == 1)) { |
| 77 | pc = preempt_count(); |
| 78 | /* |
| 79 | * skip over 5 funcs: |
| 80 | * __ftrace_trace_stack, |
| 81 | * __trace_stack, |
| 82 | * function_stack_trace_call |
| 83 | * ftrace_list_func |
| 84 | * ftrace_call |
| 85 | */ |
| 86 | __trace_stack(tr, data, flags, 5, pc); |
| 87 | } |
| 88 | |
| 89 | atomic_dec(&data->disabled); |
| 90 | local_irq_restore(flags); |
| 91 | } |
| 92 | |
| 93 | static struct ftrace_ops trace_stack_ops __read_mostly = |
| 94 | { |
| 95 | .func = function_stack_trace_call, |
| 96 | }; |
| 97 | |
| 98 | /* Our two options */ |
| 99 | enum { |
| 100 | TRACE_FUNC_OPT_STACK = 0x1, |
| 101 | }; |
| 102 | |
| 103 | static struct tracer_opt func_opts[] = { |
| 104 | #ifdef CONFIG_STACKTRACE |
| 105 | { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) }, |
| 106 | #endif |
| 107 | { } /* Always set a last empty entry */ |
| 108 | }; |
| 109 | |
| 110 | static struct tracer_flags func_flags = { |
| 111 | .val = 0, /* By default: all flags disabled */ |
| 112 | .opts = func_opts |
| 113 | }; |
| 114 | |
| 115 | static int func_set_flag(u32 old_flags, u32 bit, int set) |
| 116 | { |
| 117 | if (bit == TRACE_FUNC_OPT_STACK) { |
| 118 | /* do nothing if already set */ |
| 119 | if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK)) |
| 120 | return 0; |
| 121 | |
| 122 | if (set) |
| 123 | register_ftrace_function(&trace_stack_ops); |
| 124 | else |
| 125 | unregister_ftrace_function(&trace_stack_ops); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 133 | static struct tracer function_trace __read_mostly = |
| 134 | { |
Steven Rostedt | 3ce83ae | 2008-10-06 19:06:13 -0400 | [diff] [blame] | 135 | .name = "function", |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 136 | .init = function_trace_init, |
| 137 | .reset = function_trace_reset, |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 138 | .start = function_trace_start, |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame^] | 139 | .flags = &func_flags, |
| 140 | .set_flag = func_set_flag, |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 141 | #ifdef CONFIG_FTRACE_SELFTEST |
| 142 | .selftest = trace_selftest_startup_function, |
| 143 | #endif |
Steven Rostedt | 1b29b01 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static __init int init_function_trace(void) |
| 147 | { |
| 148 | return register_tracer(&function_trace); |
| 149 | } |
| 150 | |
| 151 | device_initcall(init_function_trace); |