Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Infrastructure for profiling code inserted by 'gcc -pg'. |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 5 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> |
| 6 | * |
| 7 | * Originally ported from the -rt patch by: |
| 8 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 9 | * |
| 10 | * Based on code in the latency_tracer, that is: |
| 11 | * |
| 12 | * Copyright (C) 2004-2006 Ingo Molnar |
| 13 | * Copyright (C) 2004 William Lee Irwin III |
| 14 | */ |
| 15 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 16 | #include <linux/stop_machine.h> |
| 17 | #include <linux/clocksource.h> |
| 18 | #include <linux/kallsyms.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 20 | #include <linux/suspend.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 21 | #include <linux/debugfs.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 22 | #include <linux/hardirq.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 23 | #include <linux/kthread.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 25 | #include <linux/bsearch.h> |
Paul Gortmaker | 56d82e0 | 2011-05-26 17:53:52 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 27 | #include <linux/ftrace.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 28 | #include <linux/sysctl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 30 | #include <linux/ctype.h> |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 31 | #include <linux/sort.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 32 | #include <linux/list.h> |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 33 | #include <linux/hash.h> |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 34 | #include <linux/rcupdate.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 35 | |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 36 | #include <trace/events/sched.h> |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 37 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 38 | #include <asm/setup.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 39 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 40 | #include "trace_output.h" |
Steven Rostedt | bac429f | 2009-03-20 12:50:56 -0400 | [diff] [blame] | 41 | #include "trace_stat.h" |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 42 | |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 43 | #define FTRACE_WARN_ON(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 44 | ({ \ |
| 45 | int ___r = cond; \ |
| 46 | if (WARN_ON(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 47 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 48 | ___r; \ |
| 49 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 50 | |
| 51 | #define FTRACE_WARN_ON_ONCE(cond) \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 52 | ({ \ |
| 53 | int ___r = cond; \ |
| 54 | if (WARN_ON_ONCE(___r)) \ |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 55 | ftrace_kill(); \ |
Steven Rostedt | 0778d9a | 2011-04-29 10:36:31 -0400 | [diff] [blame] | 56 | ___r; \ |
| 57 | }) |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 58 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 59 | /* hash bits for specific function selection */ |
| 60 | #define FTRACE_HASH_BITS 7 |
| 61 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 62 | #define FTRACE_HASH_DEFAULT_BITS 10 |
| 63 | #define FTRACE_HASH_MAX_BITS 12 |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 64 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 65 | #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL) |
| 66 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 67 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 68 | int ftrace_enabled __read_mostly; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 69 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 70 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 71 | /* Quick disabling of function tracer. */ |
| 72 | int function_trace_stop; |
| 73 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 74 | /* List for set_ftrace_pid's pids. */ |
| 75 | LIST_HEAD(ftrace_pids); |
| 76 | struct ftrace_pid { |
| 77 | struct list_head list; |
| 78 | struct pid *pid; |
| 79 | }; |
| 80 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 81 | /* |
| 82 | * ftrace_disabled is set when an anomaly is discovered. |
| 83 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 84 | */ |
| 85 | static int ftrace_disabled __read_mostly; |
| 86 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 87 | static DEFINE_MUTEX(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 88 | |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 89 | static struct ftrace_ops ftrace_list_end __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 90 | .func = ftrace_stub, |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 93 | static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end; |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 94 | static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 95 | static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 96 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | 6331c28 | 2011-07-13 15:11:02 -0400 | [diff] [blame] | 97 | static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 98 | ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 99 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 100 | static struct ftrace_ops global_ops; |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 101 | static struct ftrace_ops control_ops; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 102 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 103 | static void |
| 104 | ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip); |
| 105 | |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 106 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 107 | * Traverse the ftrace_global_list, invoking all entries. The reason that we |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 108 | * can use rcu_dereference_raw() is that elements removed from this list |
| 109 | * are simply leaked, so there is no need to interact with a grace-period |
| 110 | * mechanism. The rcu_dereference_raw() calls are needed to handle |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 111 | * concurrent insertions into the ftrace_global_list. |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 112 | * |
| 113 | * Silly Alpha and silly pointer-speculation compiler optimizations! |
| 114 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 115 | static void ftrace_global_list_func(unsigned long ip, |
| 116 | unsigned long parent_ip) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 117 | { |
Steven Rostedt | b1cff0a | 2011-05-25 14:27:43 -0400 | [diff] [blame] | 118 | struct ftrace_ops *op; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 119 | |
Steven Rostedt | b1cff0a | 2011-05-25 14:27:43 -0400 | [diff] [blame] | 120 | if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT))) |
| 121 | return; |
| 122 | |
| 123 | trace_recursion_set(TRACE_GLOBAL_BIT); |
| 124 | op = rcu_dereference_raw(ftrace_global_list); /*see above*/ |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 125 | while (op != &ftrace_list_end) { |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 126 | op->func(ip, parent_ip); |
Paul E. McKenney | 3f379b0 | 2010-03-05 15:03:25 -0800 | [diff] [blame] | 127 | op = rcu_dereference_raw(op->next); /*see above*/ |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 128 | }; |
Steven Rostedt | b1cff0a | 2011-05-25 14:27:43 -0400 | [diff] [blame] | 129 | trace_recursion_clear(TRACE_GLOBAL_BIT); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 132 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip) |
| 133 | { |
Steven Rostedt | 0ef8cde | 2008-12-03 15:36:58 -0500 | [diff] [blame] | 134 | if (!test_tsk_trace_trace(current)) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | ftrace_pid_function(ip, parent_ip); |
| 138 | } |
| 139 | |
| 140 | static void set_ftrace_pid_function(ftrace_func_t func) |
| 141 | { |
| 142 | /* do not set ftrace_pid_function to itself! */ |
| 143 | if (func != ftrace_pid_func) |
| 144 | ftrace_pid_function = func; |
| 145 | } |
| 146 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 147 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 148 | * clear_ftrace_function - reset the ftrace function |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 149 | * |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 150 | * This NULLs the ftrace function and in essence stops |
| 151 | * tracing. There may be lag |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 152 | */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 153 | void clear_ftrace_function(void) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 154 | { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 155 | ftrace_trace_function = ftrace_stub; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 156 | __ftrace_trace_function = ftrace_stub; |
Steven Rostedt | 6331c28 | 2011-07-13 15:11:02 -0400 | [diff] [blame] | 157 | __ftrace_trace_function_delay = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 158 | ftrace_pid_function = ftrace_stub; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 159 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 160 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 161 | #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 162 | /* |
| 163 | * For those archs that do not test ftrace_trace_stop in their |
| 164 | * mcount call site, we need to do it from C. |
| 165 | */ |
| 166 | static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip) |
| 167 | { |
| 168 | if (function_trace_stop) |
| 169 | return; |
| 170 | |
| 171 | __ftrace_trace_function(ip, parent_ip); |
| 172 | } |
| 173 | #endif |
| 174 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 175 | static void control_ops_disable_all(struct ftrace_ops *ops) |
| 176 | { |
| 177 | int cpu; |
| 178 | |
| 179 | for_each_possible_cpu(cpu) |
| 180 | *per_cpu_ptr(ops->disabled, cpu) = 1; |
| 181 | } |
| 182 | |
| 183 | static int control_ops_alloc(struct ftrace_ops *ops) |
| 184 | { |
| 185 | int __percpu *disabled; |
| 186 | |
| 187 | disabled = alloc_percpu(int); |
| 188 | if (!disabled) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | ops->disabled = disabled; |
| 192 | control_ops_disable_all(ops); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void control_ops_free(struct ftrace_ops *ops) |
| 197 | { |
| 198 | free_percpu(ops->disabled); |
| 199 | } |
| 200 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 201 | static void update_global_ops(void) |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 202 | { |
| 203 | ftrace_func_t func; |
| 204 | |
| 205 | /* |
| 206 | * If there's only one function registered, then call that |
| 207 | * function directly. Otherwise, we need to iterate over the |
| 208 | * registered callers. |
| 209 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 210 | if (ftrace_global_list == &ftrace_list_end || |
| 211 | ftrace_global_list->next == &ftrace_list_end) |
| 212 | func = ftrace_global_list->func; |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 213 | else |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 214 | func = ftrace_global_list_func; |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 215 | |
| 216 | /* If we filter on pids, update to use the pid function */ |
| 217 | if (!list_empty(&ftrace_pids)) { |
| 218 | set_ftrace_pid_function(func); |
| 219 | func = ftrace_pid_func; |
| 220 | } |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 221 | |
| 222 | global_ops.func = func; |
| 223 | } |
| 224 | |
| 225 | static void update_ftrace_function(void) |
| 226 | { |
| 227 | ftrace_func_t func; |
| 228 | |
| 229 | update_global_ops(); |
| 230 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 231 | /* |
| 232 | * If we are at the end of the list and this ops is |
| 233 | * not dynamic, then have the mcount trampoline call |
| 234 | * the function directly |
| 235 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 236 | if (ftrace_ops_list == &ftrace_list_end || |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 237 | (ftrace_ops_list->next == &ftrace_list_end && |
| 238 | !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC))) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 239 | func = ftrace_ops_list->func; |
| 240 | else |
| 241 | func = ftrace_ops_list_func; |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 242 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 243 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 244 | ftrace_trace_function = func; |
| 245 | #else |
Steven Rostedt | 6331c28 | 2011-07-13 15:11:02 -0400 | [diff] [blame] | 246 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 247 | /* do not update till all functions have been modified */ |
| 248 | __ftrace_trace_function_delay = func; |
| 249 | #else |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 250 | __ftrace_trace_function = func; |
Steven Rostedt | 6331c28 | 2011-07-13 15:11:02 -0400 | [diff] [blame] | 251 | #endif |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 252 | ftrace_trace_function = ftrace_test_stop_func; |
| 253 | #endif |
| 254 | } |
| 255 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 256 | static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 257 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 258 | ops->next = *list; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 259 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 260 | * We are entering ops into the list but another |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 261 | * CPU might be walking that list. We need to make sure |
| 262 | * the ops->next pointer is valid before another CPU sees |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 263 | * the ops pointer included into the list. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 264 | */ |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 265 | rcu_assign_pointer(*list, ops); |
| 266 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 267 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 268 | static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
| 269 | { |
| 270 | struct ftrace_ops **p; |
| 271 | |
| 272 | /* |
| 273 | * If we are removing the last function, then simply point |
| 274 | * to the ftrace_stub. |
| 275 | */ |
| 276 | if (*list == ops && ops->next == &ftrace_list_end) { |
| 277 | *list = &ftrace_list_end; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
| 282 | if (*p == ops) |
| 283 | break; |
| 284 | |
| 285 | if (*p != ops) |
| 286 | return -1; |
| 287 | |
| 288 | *p = (*p)->next; |
| 289 | return 0; |
| 290 | } |
| 291 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 292 | static void add_ftrace_list_ops(struct ftrace_ops **list, |
| 293 | struct ftrace_ops *main_ops, |
| 294 | struct ftrace_ops *ops) |
| 295 | { |
| 296 | int first = *list == &ftrace_list_end; |
| 297 | add_ftrace_ops(list, ops); |
| 298 | if (first) |
| 299 | add_ftrace_ops(&ftrace_ops_list, main_ops); |
| 300 | } |
| 301 | |
| 302 | static int remove_ftrace_list_ops(struct ftrace_ops **list, |
| 303 | struct ftrace_ops *main_ops, |
| 304 | struct ftrace_ops *ops) |
| 305 | { |
| 306 | int ret = remove_ftrace_ops(list, ops); |
| 307 | if (!ret && *list == &ftrace_list_end) |
| 308 | ret = remove_ftrace_ops(&ftrace_ops_list, main_ops); |
| 309 | return ret; |
| 310 | } |
| 311 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 312 | static int __register_ftrace_function(struct ftrace_ops *ops) |
| 313 | { |
| 314 | if (ftrace_disabled) |
| 315 | return -ENODEV; |
| 316 | |
| 317 | if (FTRACE_WARN_ON(ops == &global_ops)) |
| 318 | return -EINVAL; |
| 319 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 320 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 321 | return -EBUSY; |
| 322 | |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 323 | /* We don't support both control and global flags set. */ |
| 324 | if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK) |
| 325 | return -EINVAL; |
| 326 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 327 | if (!core_kernel_data((unsigned long)ops)) |
| 328 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; |
| 329 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 330 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 331 | add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 332 | ops->flags |= FTRACE_OPS_FL_ENABLED; |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 333 | } else if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
| 334 | if (control_ops_alloc(ops)) |
| 335 | return -ENOMEM; |
| 336 | add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 337 | } else |
| 338 | add_ftrace_ops(&ftrace_ops_list, ops); |
| 339 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 340 | if (ftrace_enabled) |
| 341 | update_ftrace_function(); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 342 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 346 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 347 | { |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 348 | int ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 349 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 350 | if (ftrace_disabled) |
| 351 | return -ENODEV; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 352 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 353 | if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) |
| 354 | return -EBUSY; |
| 355 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 356 | if (FTRACE_WARN_ON(ops == &global_ops)) |
| 357 | return -EINVAL; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 358 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 359 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 360 | ret = remove_ftrace_list_ops(&ftrace_global_list, |
| 361 | &global_ops, ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 362 | if (!ret) |
| 363 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 364 | } else if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
| 365 | ret = remove_ftrace_list_ops(&ftrace_control_list, |
| 366 | &control_ops, ops); |
| 367 | if (!ret) { |
| 368 | /* |
| 369 | * The ftrace_ops is now removed from the list, |
| 370 | * so there'll be no new users. We must ensure |
| 371 | * all current users are done before we free |
| 372 | * the control data. |
| 373 | */ |
| 374 | synchronize_sched(); |
| 375 | control_ops_free(ops); |
| 376 | } |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 377 | } else |
| 378 | ret = remove_ftrace_ops(&ftrace_ops_list, ops); |
| 379 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 380 | if (ret < 0) |
| 381 | return ret; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 382 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 383 | if (ftrace_enabled) |
| 384 | update_ftrace_function(); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 385 | |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 386 | /* |
| 387 | * Dynamic ops may be freed, we must make sure that all |
| 388 | * callers are done before leaving this function. |
| 389 | */ |
| 390 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) |
| 391 | synchronize_sched(); |
| 392 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 393 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 396 | static void ftrace_update_pid_func(void) |
| 397 | { |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 398 | /* Only do something if we are tracing something */ |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 399 | if (ftrace_trace_function == ftrace_stub) |
KOSAKI Motohiro | 10dd3eb | 2009-03-06 15:29:04 +0900 | [diff] [blame] | 400 | return; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 401 | |
Steven Rostedt | 491d0dc | 2011-04-27 21:43:36 -0400 | [diff] [blame] | 402 | update_ftrace_function(); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 403 | } |
| 404 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 405 | #ifdef CONFIG_FUNCTION_PROFILER |
| 406 | struct ftrace_profile { |
| 407 | struct hlist_node node; |
| 408 | unsigned long ip; |
| 409 | unsigned long counter; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 410 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 411 | unsigned long long time; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 412 | unsigned long long time_squared; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 413 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | struct ftrace_profile_page { |
| 417 | struct ftrace_profile_page *next; |
| 418 | unsigned long index; |
| 419 | struct ftrace_profile records[]; |
| 420 | }; |
| 421 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 422 | struct ftrace_profile_stat { |
| 423 | atomic_t disabled; |
| 424 | struct hlist_head *hash; |
| 425 | struct ftrace_profile_page *pages; |
| 426 | struct ftrace_profile_page *start; |
| 427 | struct tracer_stat stat; |
| 428 | }; |
| 429 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 430 | #define PROFILE_RECORDS_SIZE \ |
| 431 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) |
| 432 | |
| 433 | #define PROFILES_PER_PAGE \ |
| 434 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) |
| 435 | |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 436 | static int ftrace_profile_bits __read_mostly; |
| 437 | static int ftrace_profile_enabled __read_mostly; |
| 438 | |
| 439 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 440 | static DEFINE_MUTEX(ftrace_profile_lock); |
| 441 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 442 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 443 | |
| 444 | #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */ |
| 445 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 446 | static void * |
| 447 | function_stat_next(void *v, int idx) |
| 448 | { |
| 449 | struct ftrace_profile *rec = v; |
| 450 | struct ftrace_profile_page *pg; |
| 451 | |
| 452 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
| 453 | |
| 454 | again: |
Li Zefan | 0296e42 | 2009-06-26 11:15:37 +0800 | [diff] [blame] | 455 | if (idx != 0) |
| 456 | rec++; |
| 457 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 458 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
| 459 | pg = pg->next; |
| 460 | if (!pg) |
| 461 | return NULL; |
| 462 | rec = &pg->records[0]; |
| 463 | if (!rec->counter) |
| 464 | goto again; |
| 465 | } |
| 466 | |
| 467 | return rec; |
| 468 | } |
| 469 | |
| 470 | static void *function_stat_start(struct tracer_stat *trace) |
| 471 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 472 | struct ftrace_profile_stat *stat = |
| 473 | container_of(trace, struct ftrace_profile_stat, stat); |
| 474 | |
| 475 | if (!stat || !stat->start) |
| 476 | return NULL; |
| 477 | |
| 478 | return function_stat_next(&stat->start->records[0], 0); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 481 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 482 | /* function graph compares on total time */ |
| 483 | static int function_stat_cmp(void *p1, void *p2) |
| 484 | { |
| 485 | struct ftrace_profile *a = p1; |
| 486 | struct ftrace_profile *b = p2; |
| 487 | |
| 488 | if (a->time < b->time) |
| 489 | return -1; |
| 490 | if (a->time > b->time) |
| 491 | return 1; |
| 492 | else |
| 493 | return 0; |
| 494 | } |
| 495 | #else |
| 496 | /* not function graph compares against hits */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 497 | static int function_stat_cmp(void *p1, void *p2) |
| 498 | { |
| 499 | struct ftrace_profile *a = p1; |
| 500 | struct ftrace_profile *b = p2; |
| 501 | |
| 502 | if (a->counter < b->counter) |
| 503 | return -1; |
| 504 | if (a->counter > b->counter) |
| 505 | return 1; |
| 506 | else |
| 507 | return 0; |
| 508 | } |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 509 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 510 | |
| 511 | static int function_stat_headers(struct seq_file *m) |
| 512 | { |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 513 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 514 | seq_printf(m, " Function " |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 515 | "Hit Time Avg s^2\n" |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 516 | " -------- " |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 517 | "--- ---- --- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 518 | #else |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 519 | seq_printf(m, " Function Hit\n" |
| 520 | " -------- ---\n"); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 521 | #endif |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static int function_stat_show(struct seq_file *m, void *v) |
| 526 | { |
| 527 | struct ftrace_profile *rec = v; |
| 528 | char str[KSYM_SYMBOL_LEN]; |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 529 | int ret = 0; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 530 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 531 | static struct trace_seq s; |
| 532 | unsigned long long avg; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 533 | unsigned long long stddev; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 534 | #endif |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 535 | mutex_lock(&ftrace_profile_lock); |
| 536 | |
| 537 | /* we raced with function_profile_reset() */ |
| 538 | if (unlikely(rec->counter == 0)) { |
| 539 | ret = -EBUSY; |
| 540 | goto out; |
| 541 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 542 | |
| 543 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 544 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 545 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 546 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 547 | seq_printf(m, " "); |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 548 | avg = rec->time; |
| 549 | do_div(avg, rec->counter); |
| 550 | |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 551 | /* Sample standard deviation (s^2) */ |
| 552 | if (rec->counter <= 1) |
| 553 | stddev = 0; |
| 554 | else { |
| 555 | stddev = rec->time_squared - rec->counter * avg * avg; |
| 556 | /* |
| 557 | * Divide only 1000 for ns^2 -> us^2 conversion. |
| 558 | * trace_print_graph_duration will divide 1000 again. |
| 559 | */ |
| 560 | do_div(stddev, (rec->counter - 1) * 1000); |
| 561 | } |
| 562 | |
Steven Rostedt | 34886c8 | 2009-03-25 21:00:47 -0400 | [diff] [blame] | 563 | trace_seq_init(&s); |
| 564 | trace_print_graph_duration(rec->time, &s); |
| 565 | trace_seq_puts(&s, " "); |
| 566 | trace_print_graph_duration(avg, &s); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 567 | trace_seq_puts(&s, " "); |
| 568 | trace_print_graph_duration(stddev, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 569 | trace_print_seq(m, &s); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 570 | #endif |
| 571 | seq_putc(m, '\n'); |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 572 | out: |
| 573 | mutex_unlock(&ftrace_profile_lock); |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 574 | |
Li Zefan | 3aaba20 | 2010-08-23 16:50:12 +0800 | [diff] [blame] | 575 | return ret; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 578 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 579 | { |
| 580 | struct ftrace_profile_page *pg; |
| 581 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 582 | pg = stat->pages = stat->start; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 583 | |
| 584 | while (pg) { |
| 585 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); |
| 586 | pg->index = 0; |
| 587 | pg = pg->next; |
| 588 | } |
| 589 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 590 | memset(stat->hash, 0, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 591 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
| 592 | } |
| 593 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 594 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 595 | { |
| 596 | struct ftrace_profile_page *pg; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 597 | int functions; |
| 598 | int pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 599 | int i; |
| 600 | |
| 601 | /* If we already allocated, do nothing */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 602 | if (stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 603 | return 0; |
| 604 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 605 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
| 606 | if (!stat->pages) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 607 | return -ENOMEM; |
| 608 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 609 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 610 | functions = ftrace_update_tot_cnt; |
| 611 | #else |
| 612 | /* |
| 613 | * We do not know the number of functions that exist because |
| 614 | * dynamic tracing is what counts them. With past experience |
| 615 | * we have around 20K functions. That should be more than enough. |
| 616 | * It is highly unlikely we will execute every function in |
| 617 | * the kernel. |
| 618 | */ |
| 619 | functions = 20000; |
| 620 | #endif |
| 621 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 622 | pg = stat->start = stat->pages; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 623 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 624 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
| 625 | |
| 626 | for (i = 0; i < pages; i++) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 627 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 628 | if (!pg->next) |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 629 | goto out_free; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 630 | pg = pg->next; |
| 631 | } |
| 632 | |
| 633 | return 0; |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 634 | |
| 635 | out_free: |
| 636 | pg = stat->start; |
| 637 | while (pg) { |
| 638 | unsigned long tmp = (unsigned long)pg; |
| 639 | |
| 640 | pg = pg->next; |
| 641 | free_page(tmp); |
| 642 | } |
| 643 | |
| 644 | free_page((unsigned long)stat->pages); |
| 645 | stat->pages = NULL; |
| 646 | stat->start = NULL; |
| 647 | |
| 648 | return -ENOMEM; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 649 | } |
| 650 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 651 | static int ftrace_profile_init_cpu(int cpu) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 652 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 653 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 654 | int size; |
| 655 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 656 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 657 | |
| 658 | if (stat->hash) { |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 659 | /* If the profile is already created, simply reset it */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 660 | ftrace_profile_reset(stat); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * We are profiling all functions, but usually only a few thousand |
| 666 | * functions are hit. We'll make a hash of 1024 items. |
| 667 | */ |
| 668 | size = FTRACE_PROFILE_HASH_SIZE; |
| 669 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 670 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 671 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 672 | if (!stat->hash) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 673 | return -ENOMEM; |
| 674 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 675 | if (!ftrace_profile_bits) { |
| 676 | size--; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 677 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 678 | for (; size; size >>= 1) |
| 679 | ftrace_profile_bits++; |
| 680 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 681 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 682 | /* Preallocate the function profiling pages */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 683 | if (ftrace_profile_pages_init(stat) < 0) { |
| 684 | kfree(stat->hash); |
| 685 | stat->hash = NULL; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 686 | return -ENOMEM; |
| 687 | } |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 692 | static int ftrace_profile_init(void) |
| 693 | { |
| 694 | int cpu; |
| 695 | int ret = 0; |
| 696 | |
| 697 | for_each_online_cpu(cpu) { |
| 698 | ret = ftrace_profile_init_cpu(cpu); |
| 699 | if (ret) |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | return ret; |
| 704 | } |
| 705 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 706 | /* interrupts must be disabled */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 707 | static struct ftrace_profile * |
| 708 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 709 | { |
| 710 | struct ftrace_profile *rec; |
| 711 | struct hlist_head *hhd; |
| 712 | struct hlist_node *n; |
| 713 | unsigned long key; |
| 714 | |
| 715 | key = hash_long(ip, ftrace_profile_bits); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 716 | hhd = &stat->hash[key]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 717 | |
| 718 | if (hlist_empty(hhd)) |
| 719 | return NULL; |
| 720 | |
| 721 | hlist_for_each_entry_rcu(rec, n, hhd, node) { |
| 722 | if (rec->ip == ip) |
| 723 | return rec; |
| 724 | } |
| 725 | |
| 726 | return NULL; |
| 727 | } |
| 728 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 729 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
| 730 | struct ftrace_profile *rec) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 731 | { |
| 732 | unsigned long key; |
| 733 | |
| 734 | key = hash_long(rec->ip, ftrace_profile_bits); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 735 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 736 | } |
| 737 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 738 | /* |
| 739 | * The memory is already allocated, this simply finds a new record to use. |
| 740 | */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 741 | static struct ftrace_profile * |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 742 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 743 | { |
| 744 | struct ftrace_profile *rec = NULL; |
| 745 | |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 746 | /* prevent recursion (from NMIs) */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 747 | if (atomic_inc_return(&stat->disabled) != 1) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 748 | goto out; |
| 749 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 750 | /* |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 751 | * Try to find the function again since an NMI |
| 752 | * could have added it |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 753 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 754 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 755 | if (rec) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 756 | goto out; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 757 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 758 | if (stat->pages->index == PROFILES_PER_PAGE) { |
| 759 | if (!stat->pages->next) |
| 760 | goto out; |
| 761 | stat->pages = stat->pages->next; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 764 | rec = &stat->pages->records[stat->pages->index++]; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 765 | rec->ip = ip; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 766 | ftrace_add_profile(stat, rec); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 767 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 768 | out: |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 769 | atomic_dec(&stat->disabled); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 770 | |
| 771 | return rec; |
| 772 | } |
| 773 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 774 | static void |
| 775 | function_profile_call(unsigned long ip, unsigned long parent_ip) |
| 776 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 777 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 778 | struct ftrace_profile *rec; |
| 779 | unsigned long flags; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 780 | |
| 781 | if (!ftrace_profile_enabled) |
| 782 | return; |
| 783 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 784 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 785 | |
| 786 | stat = &__get_cpu_var(ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 787 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 788 | goto out; |
| 789 | |
| 790 | rec = ftrace_find_profiled_func(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 791 | if (!rec) { |
Steven Rostedt | 318e0a7 | 2009-03-25 20:06:34 -0400 | [diff] [blame] | 792 | rec = ftrace_profile_alloc(stat, ip); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 793 | if (!rec) |
| 794 | goto out; |
| 795 | } |
| 796 | |
| 797 | rec->counter++; |
| 798 | out: |
| 799 | local_irq_restore(flags); |
| 800 | } |
| 801 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 802 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 803 | static int profile_graph_entry(struct ftrace_graph_ent *trace) |
| 804 | { |
| 805 | function_profile_call(trace->func, 0); |
| 806 | return 1; |
| 807 | } |
| 808 | |
| 809 | static void profile_graph_return(struct ftrace_graph_ret *trace) |
| 810 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 811 | struct ftrace_profile_stat *stat; |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 812 | unsigned long long calltime; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 813 | struct ftrace_profile *rec; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 814 | unsigned long flags; |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 815 | |
| 816 | local_irq_save(flags); |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 817 | stat = &__get_cpu_var(ftrace_profile_stats); |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 818 | if (!stat->hash || !ftrace_profile_enabled) |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 819 | goto out; |
| 820 | |
Steven Rostedt | 37e44bc | 2010-04-27 21:04:24 -0400 | [diff] [blame] | 821 | /* If the calltime was zero'd ignore it */ |
| 822 | if (!trace->calltime) |
| 823 | goto out; |
| 824 | |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 825 | calltime = trace->rettime - trace->calltime; |
| 826 | |
| 827 | if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) { |
| 828 | int index; |
| 829 | |
| 830 | index = trace->depth; |
| 831 | |
| 832 | /* Append this call time to the parent time to subtract */ |
| 833 | if (index) |
| 834 | current->ret_stack[index - 1].subtime += calltime; |
| 835 | |
| 836 | if (current->ret_stack[index].subtime < calltime) |
| 837 | calltime -= current->ret_stack[index].subtime; |
| 838 | else |
| 839 | calltime = 0; |
| 840 | } |
| 841 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 842 | rec = ftrace_find_profiled_func(stat, trace->func); |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 843 | if (rec) { |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 844 | rec->time += calltime; |
Chase Douglas | e330b3b | 2010-04-26 14:02:05 -0400 | [diff] [blame] | 845 | rec->time_squared += calltime * calltime; |
| 846 | } |
Steven Rostedt | a2a16d6 | 2009-03-24 23:17:58 -0400 | [diff] [blame] | 847 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 848 | out: |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 849 | local_irq_restore(flags); |
| 850 | } |
| 851 | |
| 852 | static int register_ftrace_profiler(void) |
| 853 | { |
| 854 | return register_ftrace_graph(&profile_graph_return, |
| 855 | &profile_graph_entry); |
| 856 | } |
| 857 | |
| 858 | static void unregister_ftrace_profiler(void) |
| 859 | { |
| 860 | unregister_ftrace_graph(); |
| 861 | } |
| 862 | #else |
Paul McQuade | bd38c0e | 2011-05-31 20:51:55 +0100 | [diff] [blame] | 863 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 864 | .func = function_profile_call, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 865 | }; |
| 866 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 867 | static int register_ftrace_profiler(void) |
| 868 | { |
| 869 | return register_ftrace_function(&ftrace_profile_ops); |
| 870 | } |
| 871 | |
| 872 | static void unregister_ftrace_profiler(void) |
| 873 | { |
| 874 | unregister_ftrace_function(&ftrace_profile_ops); |
| 875 | } |
| 876 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 877 | |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 878 | static ssize_t |
| 879 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 880 | size_t cnt, loff_t *ppos) |
| 881 | { |
| 882 | unsigned long val; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 883 | int ret; |
| 884 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 885 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 886 | if (ret) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 887 | return ret; |
| 888 | |
| 889 | val = !!val; |
| 890 | |
| 891 | mutex_lock(&ftrace_profile_lock); |
| 892 | if (ftrace_profile_enabled ^ val) { |
| 893 | if (val) { |
| 894 | ret = ftrace_profile_init(); |
| 895 | if (ret < 0) { |
| 896 | cnt = ret; |
| 897 | goto out; |
| 898 | } |
| 899 | |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 900 | ret = register_ftrace_profiler(); |
| 901 | if (ret < 0) { |
| 902 | cnt = ret; |
| 903 | goto out; |
| 904 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 905 | ftrace_profile_enabled = 1; |
| 906 | } else { |
| 907 | ftrace_profile_enabled = 0; |
Steven Rostedt | 0f6ce3d | 2009-06-01 21:51:28 -0400 | [diff] [blame] | 908 | /* |
| 909 | * unregister_ftrace_profiler calls stop_machine |
| 910 | * so this acts like an synchronize_sched. |
| 911 | */ |
Steven Rostedt | 0706f1c | 2009-03-23 23:12:58 -0400 | [diff] [blame] | 912 | unregister_ftrace_profiler(); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | out: |
| 916 | mutex_unlock(&ftrace_profile_lock); |
| 917 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 918 | *ppos += cnt; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 919 | |
| 920 | return cnt; |
| 921 | } |
| 922 | |
| 923 | static ssize_t |
| 924 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 925 | size_t cnt, loff_t *ppos) |
| 926 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 927 | char buf[64]; /* big enough to hold a number */ |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 928 | int r; |
| 929 | |
| 930 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); |
| 931 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 932 | } |
| 933 | |
| 934 | static const struct file_operations ftrace_profile_fops = { |
| 935 | .open = tracing_open_generic, |
| 936 | .read = ftrace_profile_read, |
| 937 | .write = ftrace_profile_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 938 | .llseek = default_llseek, |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 939 | }; |
| 940 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 941 | /* used to initialize the real stat files */ |
| 942 | static struct tracer_stat function_stats __initdata = { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 943 | .name = "functions", |
| 944 | .stat_start = function_stat_start, |
| 945 | .stat_next = function_stat_next, |
| 946 | .stat_cmp = function_stat_cmp, |
| 947 | .stat_headers = function_stat_headers, |
| 948 | .stat_show = function_stat_show |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 949 | }; |
| 950 | |
Steven Rostedt | 6ab5d66 | 2009-06-04 00:55:45 -0400 | [diff] [blame] | 951 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 952 | { |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 953 | struct ftrace_profile_stat *stat; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 954 | struct dentry *entry; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 955 | char *name; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 956 | int ret; |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 957 | int cpu; |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 958 | |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 959 | for_each_possible_cpu(cpu) { |
| 960 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 961 | |
| 962 | /* allocate enough for function name + cpu number */ |
| 963 | name = kmalloc(32, GFP_KERNEL); |
| 964 | if (!name) { |
| 965 | /* |
| 966 | * The files created are permanent, if something happens |
| 967 | * we still do not free memory. |
| 968 | */ |
Steven Rostedt | cafb168 | 2009-03-24 20:50:39 -0400 | [diff] [blame] | 969 | WARN(1, |
| 970 | "Could not allocate stat file for cpu %d\n", |
| 971 | cpu); |
| 972 | return; |
| 973 | } |
| 974 | stat->stat = function_stats; |
| 975 | snprintf(name, 32, "function%d", cpu); |
| 976 | stat->stat.name = name; |
| 977 | ret = register_stat_tracer(&stat->stat); |
| 978 | if (ret) { |
| 979 | WARN(1, |
| 980 | "Could not register function stat for cpu %d\n", |
| 981 | cpu); |
| 982 | kfree(name); |
| 983 | return; |
| 984 | } |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | entry = debugfs_create_file("function_profile_enabled", 0644, |
| 988 | d_tracer, NULL, &ftrace_profile_fops); |
| 989 | if (!entry) |
| 990 | pr_warning("Could not create debugfs " |
| 991 | "'function_profile_enabled' entry\n"); |
| 992 | } |
| 993 | |
| 994 | #else /* CONFIG_FUNCTION_PROFILER */ |
Steven Rostedt | 6ab5d66 | 2009-06-04 00:55:45 -0400 | [diff] [blame] | 995 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 996 | { |
| 997 | } |
| 998 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 999 | |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1000 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
| 1001 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1002 | #ifdef CONFIG_DYNAMIC_FTRACE |
Ingo Molnar | 73d3fd9 | 2009-02-17 11:48:18 +0100 | [diff] [blame] | 1003 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1004 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1005 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1006 | #endif |
| 1007 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1008 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; |
| 1009 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1010 | struct ftrace_func_probe { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1011 | struct hlist_node node; |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 1012 | struct ftrace_probe_ops *ops; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 1013 | unsigned long flags; |
| 1014 | unsigned long ip; |
| 1015 | void *data; |
| 1016 | struct rcu_head rcu; |
| 1017 | }; |
| 1018 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1019 | struct ftrace_func_entry { |
| 1020 | struct hlist_node hlist; |
| 1021 | unsigned long ip; |
| 1022 | }; |
| 1023 | |
| 1024 | struct ftrace_hash { |
| 1025 | unsigned long size_bits; |
| 1026 | struct hlist_head *buckets; |
| 1027 | unsigned long count; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1028 | struct rcu_head rcu; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1029 | }; |
| 1030 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1031 | /* |
| 1032 | * We make these constant because no one should touch them, |
| 1033 | * but they are used as the default "empty hash", to avoid allocating |
| 1034 | * it all the time. These are in a read only section such that if |
| 1035 | * anyone does try to modify it, it will cause an exception. |
| 1036 | */ |
| 1037 | static const struct hlist_head empty_buckets[1]; |
| 1038 | static const struct ftrace_hash empty_hash = { |
| 1039 | .buckets = (struct hlist_head *)empty_buckets, |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1040 | }; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1041 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1042 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 1043 | static struct ftrace_ops global_ops = { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1044 | .func = ftrace_stub, |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1045 | .notrace_hash = EMPTY_HASH, |
| 1046 | .filter_hash = EMPTY_HASH, |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 1047 | }; |
| 1048 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1049 | static DEFINE_MUTEX(ftrace_regex_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1050 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1051 | struct ftrace_page { |
| 1052 | struct ftrace_page *next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1053 | struct dyn_ftrace *records; |
Steven Rostedt | 431aa3f | 2009-01-06 12:43:01 -0500 | [diff] [blame] | 1054 | int index; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1055 | int size; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 1056 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1057 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 1058 | static struct ftrace_page *ftrace_new_pgs; |
| 1059 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1060 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1061 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1062 | |
| 1063 | /* estimate from running different kernels */ |
| 1064 | #define NR_TO_INIT 10000 |
| 1065 | |
| 1066 | static struct ftrace_page *ftrace_pages_start; |
| 1067 | static struct ftrace_page *ftrace_pages; |
| 1068 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1069 | static bool ftrace_hash_empty(struct ftrace_hash *hash) |
| 1070 | { |
| 1071 | return !hash || !hash->count; |
| 1072 | } |
| 1073 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1074 | static struct ftrace_func_entry * |
| 1075 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1076 | { |
| 1077 | unsigned long key; |
| 1078 | struct ftrace_func_entry *entry; |
| 1079 | struct hlist_head *hhd; |
| 1080 | struct hlist_node *n; |
| 1081 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1082 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1083 | return NULL; |
| 1084 | |
| 1085 | if (hash->size_bits > 0) |
| 1086 | key = hash_long(ip, hash->size_bits); |
| 1087 | else |
| 1088 | key = 0; |
| 1089 | |
| 1090 | hhd = &hash->buckets[key]; |
| 1091 | |
| 1092 | hlist_for_each_entry_rcu(entry, n, hhd, hlist) { |
| 1093 | if (entry->ip == ip) |
| 1094 | return entry; |
| 1095 | } |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1099 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1100 | struct ftrace_func_entry *entry) |
| 1101 | { |
| 1102 | struct hlist_head *hhd; |
| 1103 | unsigned long key; |
| 1104 | |
| 1105 | if (hash->size_bits) |
| 1106 | key = hash_long(entry->ip, hash->size_bits); |
| 1107 | else |
| 1108 | key = 0; |
| 1109 | |
| 1110 | hhd = &hash->buckets[key]; |
| 1111 | hlist_add_head(&entry->hlist, hhd); |
| 1112 | hash->count++; |
| 1113 | } |
| 1114 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1115 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1116 | { |
| 1117 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1118 | |
| 1119 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1120 | if (!entry) |
| 1121 | return -ENOMEM; |
| 1122 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1123 | entry->ip = ip; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1124 | __add_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | static void |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1130 | free_hash_entry(struct ftrace_hash *hash, |
| 1131 | struct ftrace_func_entry *entry) |
| 1132 | { |
| 1133 | hlist_del(&entry->hlist); |
| 1134 | kfree(entry); |
| 1135 | hash->count--; |
| 1136 | } |
| 1137 | |
| 1138 | static void |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1139 | remove_hash_entry(struct ftrace_hash *hash, |
| 1140 | struct ftrace_func_entry *entry) |
| 1141 | { |
| 1142 | hlist_del(&entry->hlist); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1143 | hash->count--; |
| 1144 | } |
| 1145 | |
| 1146 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1147 | { |
| 1148 | struct hlist_head *hhd; |
| 1149 | struct hlist_node *tp, *tn; |
| 1150 | struct ftrace_func_entry *entry; |
| 1151 | int size = 1 << hash->size_bits; |
| 1152 | int i; |
| 1153 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1154 | if (!hash->count) |
| 1155 | return; |
| 1156 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1157 | for (i = 0; i < size; i++) { |
| 1158 | hhd = &hash->buckets[i]; |
| 1159 | hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1160 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 1161 | } |
| 1162 | FTRACE_WARN_ON(hash->count); |
| 1163 | } |
| 1164 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1165 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1166 | { |
| 1167 | if (!hash || hash == EMPTY_HASH) |
| 1168 | return; |
| 1169 | ftrace_hash_clear(hash); |
| 1170 | kfree(hash->buckets); |
| 1171 | kfree(hash); |
| 1172 | } |
| 1173 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1174 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1175 | { |
| 1176 | struct ftrace_hash *hash; |
| 1177 | |
| 1178 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1179 | free_ftrace_hash(hash); |
| 1180 | } |
| 1181 | |
| 1182 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1183 | { |
| 1184 | if (!hash || hash == EMPTY_HASH) |
| 1185 | return; |
| 1186 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); |
| 1187 | } |
| 1188 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame^] | 1189 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1190 | { |
| 1191 | free_ftrace_hash(ops->filter_hash); |
| 1192 | free_ftrace_hash(ops->notrace_hash); |
| 1193 | } |
| 1194 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1195 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1196 | { |
| 1197 | struct ftrace_hash *hash; |
| 1198 | int size; |
| 1199 | |
| 1200 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1201 | if (!hash) |
| 1202 | return NULL; |
| 1203 | |
| 1204 | size = 1 << size_bits; |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1205 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1206 | |
| 1207 | if (!hash->buckets) { |
| 1208 | kfree(hash); |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | |
| 1212 | hash->size_bits = size_bits; |
| 1213 | |
| 1214 | return hash; |
| 1215 | } |
| 1216 | |
| 1217 | static struct ftrace_hash * |
| 1218 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1219 | { |
| 1220 | struct ftrace_func_entry *entry; |
| 1221 | struct ftrace_hash *new_hash; |
| 1222 | struct hlist_node *tp; |
| 1223 | int size; |
| 1224 | int ret; |
| 1225 | int i; |
| 1226 | |
| 1227 | new_hash = alloc_ftrace_hash(size_bits); |
| 1228 | if (!new_hash) |
| 1229 | return NULL; |
| 1230 | |
| 1231 | /* Empty hash? */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1232 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1233 | return new_hash; |
| 1234 | |
| 1235 | size = 1 << hash->size_bits; |
| 1236 | for (i = 0; i < size; i++) { |
| 1237 | hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) { |
| 1238 | ret = add_hash_entry(new_hash, entry->ip); |
| 1239 | if (ret < 0) |
| 1240 | goto free_hash; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1245 | |
| 1246 | return new_hash; |
| 1247 | |
| 1248 | free_hash: |
| 1249 | free_ftrace_hash(new_hash); |
| 1250 | return NULL; |
| 1251 | } |
| 1252 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1253 | static void |
| 1254 | ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash); |
| 1255 | static void |
| 1256 | ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash); |
| 1257 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1258 | static int |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1259 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1260 | struct ftrace_hash **dst, struct ftrace_hash *src) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1261 | { |
| 1262 | struct ftrace_func_entry *entry; |
| 1263 | struct hlist_node *tp, *tn; |
| 1264 | struct hlist_head *hhd; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1265 | struct ftrace_hash *old_hash; |
| 1266 | struct ftrace_hash *new_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1267 | unsigned long key; |
| 1268 | int size = src->count; |
| 1269 | int bits = 0; |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1270 | int ret; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1271 | int i; |
| 1272 | |
| 1273 | /* |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1274 | * Remove the current set, update the hash and add |
| 1275 | * them back. |
| 1276 | */ |
| 1277 | ftrace_hash_rec_disable(ops, enable); |
| 1278 | |
| 1279 | /* |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1280 | * If the new source is empty, just free dst and assign it |
| 1281 | * the empty_hash. |
| 1282 | */ |
| 1283 | if (!src->count) { |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1284 | free_ftrace_hash_rcu(*dst); |
| 1285 | rcu_assign_pointer(*dst, EMPTY_HASH); |
Steven Rostedt | d4d34b9 | 2011-11-04 20:32:39 -0400 | [diff] [blame] | 1286 | /* still need to update the function records */ |
| 1287 | ret = 0; |
| 1288 | goto out; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1289 | } |
| 1290 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1291 | /* |
| 1292 | * Make the hash size about 1/2 the # found |
| 1293 | */ |
| 1294 | for (size /= 2; size; size >>= 1) |
| 1295 | bits++; |
| 1296 | |
| 1297 | /* Don't allocate too much */ |
| 1298 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1299 | bits = FTRACE_HASH_MAX_BITS; |
| 1300 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1301 | ret = -ENOMEM; |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1302 | new_hash = alloc_ftrace_hash(bits); |
| 1303 | if (!new_hash) |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1304 | goto out; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1305 | |
| 1306 | size = 1 << src->size_bits; |
| 1307 | for (i = 0; i < size; i++) { |
| 1308 | hhd = &src->buckets[i]; |
| 1309 | hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) { |
| 1310 | if (bits > 0) |
| 1311 | key = hash_long(entry->ip, bits); |
| 1312 | else |
| 1313 | key = 0; |
| 1314 | remove_hash_entry(src, entry); |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1315 | __add_hash_entry(new_hash, entry); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
Steven Rostedt | 07fd551 | 2011-05-05 18:03:47 -0400 | [diff] [blame] | 1319 | old_hash = *dst; |
| 1320 | rcu_assign_pointer(*dst, new_hash); |
| 1321 | free_ftrace_hash_rcu(old_hash); |
| 1322 | |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 1323 | ret = 0; |
| 1324 | out: |
| 1325 | /* |
| 1326 | * Enable regardless of ret: |
| 1327 | * On success, we enable the new hash. |
| 1328 | * On failure, we re-enable the original hash. |
| 1329 | */ |
| 1330 | ftrace_hash_rec_enable(ops, enable); |
| 1331 | |
| 1332 | return ret; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1335 | /* |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1336 | * Test the hashes for this ops to see if we want to call |
| 1337 | * the ops->func or not. |
| 1338 | * |
| 1339 | * It's a match if the ip is in the ops->filter_hash or |
| 1340 | * the filter_hash does not exist or is empty, |
| 1341 | * AND |
| 1342 | * the ip is not in the ops->notrace_hash. |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 1343 | * |
| 1344 | * This needs to be called with preemption disabled as |
| 1345 | * the hashes are freed with call_rcu_sched(). |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1346 | */ |
| 1347 | static int |
| 1348 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip) |
| 1349 | { |
| 1350 | struct ftrace_hash *filter_hash; |
| 1351 | struct ftrace_hash *notrace_hash; |
| 1352 | int ret; |
| 1353 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1354 | filter_hash = rcu_dereference_raw(ops->filter_hash); |
| 1355 | notrace_hash = rcu_dereference_raw(ops->notrace_hash); |
| 1356 | |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1357 | if ((ftrace_hash_empty(filter_hash) || |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1358 | ftrace_lookup_ip(filter_hash, ip)) && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1359 | (ftrace_hash_empty(notrace_hash) || |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1360 | !ftrace_lookup_ip(notrace_hash, ip))) |
| 1361 | ret = 1; |
| 1362 | else |
| 1363 | ret = 0; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1364 | |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1369 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1370 | * you must use a goto. |
| 1371 | */ |
| 1372 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1373 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1374 | int _____i; \ |
| 1375 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1376 | rec = &pg->records[_____i]; |
| 1377 | |
| 1378 | #define while_for_each_ftrace_rec() \ |
| 1379 | } \ |
| 1380 | } |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 1381 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1382 | |
| 1383 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1384 | { |
| 1385 | const struct dyn_ftrace *reca = a; |
| 1386 | const struct dyn_ftrace *recb = b; |
| 1387 | |
| 1388 | if (reca->ip > recb->ip) |
| 1389 | return 1; |
| 1390 | if (reca->ip < recb->ip) |
| 1391 | return -1; |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1395 | /** |
| 1396 | * ftrace_location - return true if the ip giving is a traced location |
| 1397 | * @ip: the instruction pointer to check |
| 1398 | * |
| 1399 | * Returns 1 if @ip given is a pointer to a ftrace location. |
| 1400 | * That is, the instruction that is either a NOP or call to |
| 1401 | * the function tracer. It checks the ftrace internal tables to |
| 1402 | * determine if the address belongs or not. |
| 1403 | */ |
| 1404 | int ftrace_location(unsigned long ip) |
| 1405 | { |
| 1406 | struct ftrace_page *pg; |
| 1407 | struct dyn_ftrace *rec; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1408 | struct dyn_ftrace key; |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1409 | |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1410 | key.ip = ip; |
| 1411 | |
| 1412 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1413 | rec = bsearch(&key, pg->records, pg->index, |
| 1414 | sizeof(struct dyn_ftrace), |
| 1415 | ftrace_cmp_recs); |
| 1416 | if (rec) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1417 | return 1; |
Steven Rostedt | 5855fea | 2011-12-16 19:27:42 -0500 | [diff] [blame] | 1418 | } |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1423 | static void __ftrace_hash_rec_update(struct ftrace_ops *ops, |
| 1424 | int filter_hash, |
| 1425 | bool inc) |
| 1426 | { |
| 1427 | struct ftrace_hash *hash; |
| 1428 | struct ftrace_hash *other_hash; |
| 1429 | struct ftrace_page *pg; |
| 1430 | struct dyn_ftrace *rec; |
| 1431 | int count = 0; |
| 1432 | int all = 0; |
| 1433 | |
| 1434 | /* Only update if the ops has been registered */ |
| 1435 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1436 | return; |
| 1437 | |
| 1438 | /* |
| 1439 | * In the filter_hash case: |
| 1440 | * If the count is zero, we update all records. |
| 1441 | * Otherwise we just update the items in the hash. |
| 1442 | * |
| 1443 | * In the notrace_hash case: |
| 1444 | * We enable the update in the hash. |
| 1445 | * As disabling notrace means enabling the tracing, |
| 1446 | * and enabling notrace means disabling, the inc variable |
| 1447 | * gets inversed. |
| 1448 | */ |
| 1449 | if (filter_hash) { |
| 1450 | hash = ops->filter_hash; |
| 1451 | other_hash = ops->notrace_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1452 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1453 | all = 1; |
| 1454 | } else { |
| 1455 | inc = !inc; |
| 1456 | hash = ops->notrace_hash; |
| 1457 | other_hash = ops->filter_hash; |
| 1458 | /* |
| 1459 | * If the notrace hash has no items, |
| 1460 | * then there's nothing to do. |
| 1461 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1462 | if (ftrace_hash_empty(hash)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1463 | return; |
| 1464 | } |
| 1465 | |
| 1466 | do_for_each_ftrace_rec(pg, rec) { |
| 1467 | int in_other_hash = 0; |
| 1468 | int in_hash = 0; |
| 1469 | int match = 0; |
| 1470 | |
| 1471 | if (all) { |
| 1472 | /* |
| 1473 | * Only the filter_hash affects all records. |
| 1474 | * Update if the record is not in the notrace hash. |
| 1475 | */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1476 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1477 | match = 1; |
| 1478 | } else { |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1479 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
| 1480 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1481 | |
| 1482 | /* |
| 1483 | * |
| 1484 | */ |
| 1485 | if (filter_hash && in_hash && !in_other_hash) |
| 1486 | match = 1; |
| 1487 | else if (!filter_hash && in_hash && |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 1488 | (in_other_hash || ftrace_hash_empty(other_hash))) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1489 | match = 1; |
| 1490 | } |
| 1491 | if (!match) |
| 1492 | continue; |
| 1493 | |
| 1494 | if (inc) { |
| 1495 | rec->flags++; |
| 1496 | if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX)) |
| 1497 | return; |
| 1498 | } else { |
| 1499 | if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0)) |
| 1500 | return; |
| 1501 | rec->flags--; |
| 1502 | } |
| 1503 | count++; |
| 1504 | /* Shortcut, if we handled all records, we are done. */ |
| 1505 | if (!all && count == hash->count) |
| 1506 | return; |
| 1507 | } while_for_each_ftrace_rec(); |
| 1508 | } |
| 1509 | |
| 1510 | static void ftrace_hash_rec_disable(struct ftrace_ops *ops, |
| 1511 | int filter_hash) |
| 1512 | { |
| 1513 | __ftrace_hash_rec_update(ops, filter_hash, 0); |
| 1514 | } |
| 1515 | |
| 1516 | static void ftrace_hash_rec_enable(struct ftrace_ops *ops, |
| 1517 | int filter_hash) |
| 1518 | { |
| 1519 | __ftrace_hash_rec_update(ops, filter_hash, 1); |
| 1520 | } |
| 1521 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1522 | static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1523 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 1524 | if (ftrace_pages->index == ftrace_pages->size) { |
| 1525 | /* We should have allocated enough */ |
| 1526 | if (WARN_ON(!ftrace_pages->next)) |
| 1527 | return NULL; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1528 | ftrace_pages = ftrace_pages->next; |
| 1529 | } |
| 1530 | |
| 1531 | return &ftrace_pages->records[ftrace_pages->index++]; |
| 1532 | } |
| 1533 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1534 | static struct dyn_ftrace * |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1535 | ftrace_record_ip(unsigned long ip) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1536 | { |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1537 | struct dyn_ftrace *rec; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1538 | |
Steven Rostedt | f3c7ac4 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1539 | if (ftrace_disabled) |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1540 | return NULL; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1541 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1542 | rec = ftrace_alloc_dyn_node(ip); |
| 1543 | if (!rec) |
| 1544 | return NULL; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1545 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1546 | rec->ip = ip; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1547 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1548 | return rec; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1549 | } |
| 1550 | |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 1551 | static void print_ip_ins(const char *fmt, unsigned char *p) |
| 1552 | { |
| 1553 | int i; |
| 1554 | |
| 1555 | printk(KERN_CONT "%s", fmt); |
| 1556 | |
| 1557 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 1558 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 1559 | } |
| 1560 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1561 | /** |
| 1562 | * ftrace_bug - report and shutdown function tracer |
| 1563 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
| 1564 | * @ip: The address that failed |
| 1565 | * |
| 1566 | * The arch code that enables or disables the function tracing |
| 1567 | * can call ftrace_bug() when it has detected a problem in |
| 1568 | * modifying the code. @failed should be one of either: |
| 1569 | * EFAULT - if the problem happens on reading the @ip address |
| 1570 | * EINVAL - if what is read at @ip is not what was expected |
| 1571 | * EPERM - if the problem happens on writting to the @ip address |
| 1572 | */ |
| 1573 | void ftrace_bug(int failed, unsigned long ip) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1574 | { |
| 1575 | switch (failed) { |
| 1576 | case -EFAULT: |
| 1577 | FTRACE_WARN_ON_ONCE(1); |
| 1578 | pr_info("ftrace faulted on modifying "); |
| 1579 | print_ip_sym(ip); |
| 1580 | break; |
| 1581 | case -EINVAL: |
| 1582 | FTRACE_WARN_ON_ONCE(1); |
| 1583 | pr_info("ftrace failed to modify "); |
| 1584 | print_ip_sym(ip); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1585 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1586 | printk(KERN_CONT "\n"); |
| 1587 | break; |
| 1588 | case -EPERM: |
| 1589 | FTRACE_WARN_ON_ONCE(1); |
| 1590 | pr_info("ftrace faulted on writing "); |
| 1591 | print_ip_sym(ip); |
| 1592 | break; |
| 1593 | default: |
| 1594 | FTRACE_WARN_ON_ONCE(1); |
| 1595 | pr_info("ftrace faulted on unknown error "); |
| 1596 | print_ip_sym(ip); |
| 1597 | } |
| 1598 | } |
| 1599 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1600 | |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 1601 | /* Return 1 if the address range is reserved for ftrace */ |
| 1602 | int ftrace_text_reserved(void *start, void *end) |
| 1603 | { |
| 1604 | struct dyn_ftrace *rec; |
| 1605 | struct ftrace_page *pg; |
| 1606 | |
| 1607 | do_for_each_ftrace_rec(pg, rec) { |
| 1608 | if (rec->ip <= (unsigned long)end && |
| 1609 | rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start) |
| 1610 | return 1; |
| 1611 | } while_for_each_ftrace_rec(); |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1615 | static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1616 | { |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1617 | unsigned long flag = 0UL; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1618 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1619 | /* |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1620 | * If we are updating calls: |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1621 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1622 | * If the record has a ref count, then we need to enable it |
| 1623 | * because someone is using it. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1624 | * |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1625 | * Otherwise we make sure its disabled. |
| 1626 | * |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1627 | * If we are disabling calls, then disable all records that |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1628 | * are enabled. |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1629 | */ |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1630 | if (enable && (rec->flags & ~FTRACE_FL_MASK)) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1631 | flag = FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1632 | |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1633 | /* If the state of this record hasn't changed, then do nothing */ |
| 1634 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1635 | return FTRACE_UPDATE_IGNORE; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1636 | |
| 1637 | if (flag) { |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1638 | if (update) |
| 1639 | rec->flags |= FTRACE_FL_ENABLED; |
| 1640 | return FTRACE_UPDATE_MAKE_CALL; |
Xiao Guangrong | 64fbcd1 | 2009-07-15 12:32:15 +0800 | [diff] [blame] | 1641 | } |
| 1642 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1643 | if (update) |
| 1644 | rec->flags &= ~FTRACE_FL_ENABLED; |
| 1645 | |
| 1646 | return FTRACE_UPDATE_MAKE_NOP; |
| 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * ftrace_update_record, set a record that now is tracing or not |
| 1651 | * @rec: the record to update |
| 1652 | * @enable: set to 1 if the record is tracing, zero to force disable |
| 1653 | * |
| 1654 | * The records that represent all functions that can be traced need |
| 1655 | * to be updated when tracing has been enabled. |
| 1656 | */ |
| 1657 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) |
| 1658 | { |
| 1659 | return ftrace_check_record(rec, enable, 1); |
| 1660 | } |
| 1661 | |
| 1662 | /** |
| 1663 | * ftrace_test_record, check if the record has been enabled or not |
| 1664 | * @rec: the record to test |
| 1665 | * @enable: set to 1 to check if enabled, 0 if it is disabled |
| 1666 | * |
| 1667 | * The arch code may need to test if a record is already set to |
| 1668 | * tracing to determine how to modify the function code that it |
| 1669 | * represents. |
| 1670 | */ |
| 1671 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) |
| 1672 | { |
| 1673 | return ftrace_check_record(rec, enable, 0); |
| 1674 | } |
| 1675 | |
| 1676 | static int |
| 1677 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
| 1678 | { |
| 1679 | unsigned long ftrace_addr; |
| 1680 | int ret; |
| 1681 | |
| 1682 | ftrace_addr = (unsigned long)FTRACE_ADDR; |
| 1683 | |
| 1684 | ret = ftrace_update_record(rec, enable); |
| 1685 | |
| 1686 | switch (ret) { |
| 1687 | case FTRACE_UPDATE_IGNORE: |
| 1688 | return 0; |
| 1689 | |
| 1690 | case FTRACE_UPDATE_MAKE_CALL: |
| 1691 | return ftrace_make_call(rec, ftrace_addr); |
| 1692 | |
| 1693 | case FTRACE_UPDATE_MAKE_NOP: |
| 1694 | return ftrace_make_nop(NULL, rec, ftrace_addr); |
| 1695 | } |
| 1696 | |
| 1697 | return -1; /* unknow ftrace bug */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1698 | } |
| 1699 | |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1700 | static void ftrace_replace_code(int update) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1701 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1702 | struct dyn_ftrace *rec; |
| 1703 | struct ftrace_page *pg; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 1704 | int failed; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1705 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 1706 | if (unlikely(ftrace_disabled)) |
| 1707 | return; |
| 1708 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1709 | do_for_each_ftrace_rec(pg, rec) { |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1710 | failed = __ftrace_replace_code(rec, update); |
Zhaolei | fa9d13c | 2009-03-13 17:16:34 +0800 | [diff] [blame] | 1711 | if (failed) { |
Steven Rostedt | 3279ba3 | 2009-10-07 16:57:56 -0400 | [diff] [blame] | 1712 | ftrace_bug(failed, rec->ip); |
| 1713 | /* Stop processing */ |
| 1714 | return; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 1715 | } |
| 1716 | } while_for_each_ftrace_rec(); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1717 | } |
| 1718 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1719 | struct ftrace_rec_iter { |
| 1720 | struct ftrace_page *pg; |
| 1721 | int index; |
| 1722 | }; |
| 1723 | |
| 1724 | /** |
| 1725 | * ftrace_rec_iter_start, start up iterating over traced functions |
| 1726 | * |
| 1727 | * Returns an iterator handle that is used to iterate over all |
| 1728 | * the records that represent address locations where functions |
| 1729 | * are traced. |
| 1730 | * |
| 1731 | * May return NULL if no records are available. |
| 1732 | */ |
| 1733 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 1734 | { |
| 1735 | /* |
| 1736 | * We only use a single iterator. |
| 1737 | * Protected by the ftrace_lock mutex. |
| 1738 | */ |
| 1739 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 1740 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 1741 | |
| 1742 | iter->pg = ftrace_pages_start; |
| 1743 | iter->index = 0; |
| 1744 | |
| 1745 | /* Could have empty pages */ |
| 1746 | while (iter->pg && !iter->pg->index) |
| 1747 | iter->pg = iter->pg->next; |
| 1748 | |
| 1749 | if (!iter->pg) |
| 1750 | return NULL; |
| 1751 | |
| 1752 | return iter; |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * ftrace_rec_iter_next, get the next record to process. |
| 1757 | * @iter: The handle to the iterator. |
| 1758 | * |
| 1759 | * Returns the next iterator after the given iterator @iter. |
| 1760 | */ |
| 1761 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 1762 | { |
| 1763 | iter->index++; |
| 1764 | |
| 1765 | if (iter->index >= iter->pg->index) { |
| 1766 | iter->pg = iter->pg->next; |
| 1767 | iter->index = 0; |
| 1768 | |
| 1769 | /* Could have empty pages */ |
| 1770 | while (iter->pg && !iter->pg->index) |
| 1771 | iter->pg = iter->pg->next; |
| 1772 | } |
| 1773 | |
| 1774 | if (!iter->pg) |
| 1775 | return NULL; |
| 1776 | |
| 1777 | return iter; |
| 1778 | } |
| 1779 | |
| 1780 | /** |
| 1781 | * ftrace_rec_iter_record, get the record at the iterator location |
| 1782 | * @iter: The current iterator location |
| 1783 | * |
| 1784 | * Returns the record that the current @iter is at. |
| 1785 | */ |
| 1786 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 1787 | { |
| 1788 | return &iter->pg->records[iter->index]; |
| 1789 | } |
| 1790 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 1791 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1792 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1793 | { |
| 1794 | unsigned long ip; |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 1795 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1796 | |
| 1797 | ip = rec->ip; |
| 1798 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 1799 | if (unlikely(ftrace_disabled)) |
| 1800 | return 0; |
| 1801 | |
Shaohua Li | 25aac9d | 2009-01-09 11:29:40 +0800 | [diff] [blame] | 1802 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 1803 | if (ret) { |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1804 | ftrace_bug(ret, ip); |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 1805 | return 0; |
Steven Rostedt | 37ad508 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1806 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 1807 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1808 | } |
| 1809 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 1810 | /* |
| 1811 | * archs can override this function if they must do something |
| 1812 | * before the modifying code is performed. |
| 1813 | */ |
| 1814 | int __weak ftrace_arch_code_modify_prepare(void) |
| 1815 | { |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
| 1819 | /* |
| 1820 | * archs can override this function if they must do something |
| 1821 | * after the modifying code is performed. |
| 1822 | */ |
| 1823 | int __weak ftrace_arch_code_modify_post_process(void) |
| 1824 | { |
| 1825 | return 0; |
| 1826 | } |
| 1827 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1828 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1829 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1830 | int *command = data; |
| 1831 | |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1832 | if (*command & FTRACE_UPDATE_CALLS) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1833 | ftrace_replace_code(1); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 1834 | else if (*command & FTRACE_DISABLE_CALLS) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1835 | ftrace_replace_code(0); |
| 1836 | |
| 1837 | if (*command & FTRACE_UPDATE_TRACE_FUNC) |
| 1838 | ftrace_update_ftrace_func(ftrace_trace_function); |
| 1839 | |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 1840 | if (*command & FTRACE_START_FUNC_RET) |
| 1841 | ftrace_enable_ftrace_graph_caller(); |
| 1842 | else if (*command & FTRACE_STOP_FUNC_RET) |
| 1843 | ftrace_disable_ftrace_graph_caller(); |
| 1844 | |
Steven Rostedt | c88fd86 | 2011-08-16 09:53:39 -0400 | [diff] [blame] | 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | /** |
| 1849 | * ftrace_run_stop_machine, go back to the stop machine method |
| 1850 | * @command: The command to tell ftrace what to do |
| 1851 | * |
| 1852 | * If an arch needs to fall back to the stop machine method, the |
| 1853 | * it can call this function. |
| 1854 | */ |
| 1855 | void ftrace_run_stop_machine(int command) |
| 1856 | { |
| 1857 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 1858 | } |
| 1859 | |
| 1860 | /** |
| 1861 | * arch_ftrace_update_code, modify the code to trace or not trace |
| 1862 | * @command: The command that needs to be done |
| 1863 | * |
| 1864 | * Archs can override this function if it does not need to |
| 1865 | * run stop_machine() to modify code. |
| 1866 | */ |
| 1867 | void __weak arch_ftrace_update_code(int command) |
| 1868 | { |
| 1869 | ftrace_run_stop_machine(command); |
| 1870 | } |
| 1871 | |
| 1872 | static void ftrace_run_update_code(int command) |
| 1873 | { |
| 1874 | int ret; |
| 1875 | |
| 1876 | ret = ftrace_arch_code_modify_prepare(); |
| 1877 | FTRACE_WARN_ON(ret); |
| 1878 | if (ret) |
| 1879 | return; |
| 1880 | /* |
| 1881 | * Do not call function tracer while we update the code. |
| 1882 | * We are in stop machine. |
| 1883 | */ |
| 1884 | function_trace_stop++; |
| 1885 | |
| 1886 | /* |
| 1887 | * By default we use stop_machine() to modify the code. |
| 1888 | * But archs can do what ever they want as long as it |
| 1889 | * is safe. The stop_machine() is the safest, but also |
| 1890 | * produces the most overhead. |
| 1891 | */ |
| 1892 | arch_ftrace_update_code(command); |
| 1893 | |
Steven Rostedt | 6331c28 | 2011-07-13 15:11:02 -0400 | [diff] [blame] | 1894 | #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 1895 | /* |
| 1896 | * For archs that call ftrace_test_stop_func(), we must |
| 1897 | * wait till after we update all the function callers |
| 1898 | * before we update the callback. This keeps different |
| 1899 | * ops that record different functions from corrupting |
| 1900 | * each other. |
| 1901 | */ |
| 1902 | __ftrace_trace_function = __ftrace_trace_function_delay; |
| 1903 | #endif |
| 1904 | function_trace_stop--; |
| 1905 | |
Steven Rostedt | 000ab69 | 2009-02-17 13:35:06 -0500 | [diff] [blame] | 1906 | ret = ftrace_arch_code_modify_post_process(); |
| 1907 | FTRACE_WARN_ON(ret); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1908 | } |
| 1909 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1910 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1911 | static int ftrace_start_up; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1912 | static int global_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1913 | |
| 1914 | static void ftrace_startup_enable(int command) |
| 1915 | { |
| 1916 | if (saved_ftrace_func != ftrace_trace_function) { |
| 1917 | saved_ftrace_func = ftrace_trace_function; |
| 1918 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 1919 | } |
| 1920 | |
| 1921 | if (!command || !ftrace_enabled) |
| 1922 | return; |
| 1923 | |
| 1924 | ftrace_run_update_code(command); |
| 1925 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1926 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 1927 | static int ftrace_startup(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1928 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1929 | bool hash_enable = true; |
| 1930 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1931 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 1932 | return -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1933 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1934 | ftrace_start_up++; |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1935 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1936 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1937 | /* ops marked global share the filter hashes */ |
| 1938 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { |
| 1939 | ops = &global_ops; |
| 1940 | /* Don't update hash if global is already set */ |
| 1941 | if (global_start_up) |
| 1942 | hash_enable = false; |
| 1943 | global_start_up++; |
| 1944 | } |
| 1945 | |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1946 | ops->flags |= FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1947 | if (hash_enable) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1948 | ftrace_hash_rec_enable(ops, 1); |
| 1949 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1950 | ftrace_startup_enable(command); |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 1951 | |
| 1952 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1953 | } |
| 1954 | |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 1955 | static void ftrace_shutdown(struct ftrace_ops *ops, int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1956 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1957 | bool hash_disable = true; |
| 1958 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1959 | if (unlikely(ftrace_disabled)) |
| 1960 | return; |
| 1961 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 1962 | ftrace_start_up--; |
Frederic Weisbecker | 9ea1a15 | 2009-06-20 06:52:21 +0200 | [diff] [blame] | 1963 | /* |
| 1964 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 1965 | * critical but the ftrace_call callers may be never nopped again after |
| 1966 | * further ftrace uses. |
| 1967 | */ |
| 1968 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 1969 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1970 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { |
| 1971 | ops = &global_ops; |
| 1972 | global_start_up--; |
| 1973 | WARN_ON_ONCE(global_start_up < 0); |
| 1974 | /* Don't update hash if global still has users */ |
| 1975 | if (global_start_up) { |
| 1976 | WARN_ON_ONCE(!ftrace_start_up); |
| 1977 | hash_disable = false; |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | if (hash_disable) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1982 | ftrace_hash_rec_disable(ops, 1); |
| 1983 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1984 | if (ops != &global_ops || !global_start_up) |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 1985 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 1986 | |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 1987 | command |= FTRACE_UPDATE_CALLS; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1988 | |
| 1989 | if (saved_ftrace_func != ftrace_trace_function) { |
| 1990 | saved_ftrace_func = ftrace_trace_function; |
| 1991 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 1992 | } |
| 1993 | |
| 1994 | if (!command || !ftrace_enabled) |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 1995 | return; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1996 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1997 | ftrace_run_update_code(command); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2000 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2001 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2002 | if (unlikely(ftrace_disabled)) |
| 2003 | return; |
| 2004 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2005 | /* Force update next time */ |
| 2006 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2007 | /* ftrace_start_up is true if we want ftrace running */ |
| 2008 | if (ftrace_start_up) |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 2009 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2010 | } |
| 2011 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2012 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2013 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2014 | if (unlikely(ftrace_disabled)) |
| 2015 | return; |
| 2016 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2017 | /* ftrace_start_up is true if ftrace is running */ |
| 2018 | if (ftrace_start_up) |
Steven Rostedt | 79e406d | 2010-09-14 22:19:46 -0400 | [diff] [blame] | 2019 | ftrace_run_update_code(FTRACE_DISABLE_CALLS); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2022 | static cycle_t ftrace_update_time; |
| 2023 | static unsigned long ftrace_update_cnt; |
| 2024 | unsigned long ftrace_update_tot_cnt; |
| 2025 | |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2026 | static int ops_traces_mod(struct ftrace_ops *ops) |
| 2027 | { |
| 2028 | struct ftrace_hash *hash; |
| 2029 | |
| 2030 | hash = ops->filter_hash; |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 2031 | return ftrace_hash_empty(hash); |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2032 | } |
| 2033 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 2034 | static int ftrace_update_code(struct module *mod) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2035 | { |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2036 | struct ftrace_page *pg; |
Lai Jiangshan | e94142a | 2009-03-13 17:51:27 +0800 | [diff] [blame] | 2037 | struct dyn_ftrace *p; |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2038 | cycle_t start, stop; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2039 | unsigned long ref = 0; |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2040 | int i; |
Steven Rostedt | f7bc8b6 | 2011-07-14 23:02:27 -0400 | [diff] [blame] | 2041 | |
| 2042 | /* |
| 2043 | * When adding a module, we need to check if tracers are |
| 2044 | * currently enabled and if they are set to trace all functions. |
| 2045 | * If they are, we need to enable the module functions as well |
| 2046 | * as update the reference counts for those function records. |
| 2047 | */ |
| 2048 | if (mod) { |
| 2049 | struct ftrace_ops *ops; |
| 2050 | |
| 2051 | for (ops = ftrace_ops_list; |
| 2052 | ops != &ftrace_list_end; ops = ops->next) { |
| 2053 | if (ops->flags & FTRACE_OPS_FL_ENABLED && |
| 2054 | ops_traces_mod(ops)) |
| 2055 | ref++; |
| 2056 | } |
| 2057 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2058 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2059 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2060 | ftrace_update_cnt = 0; |
| 2061 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2062 | for (pg = ftrace_new_pgs; pg; pg = pg->next) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 2063 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2064 | for (i = 0; i < pg->index; i++) { |
| 2065 | /* If something went wrong, bail without enabling anything */ |
| 2066 | if (unlikely(ftrace_disabled)) |
| 2067 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2068 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2069 | p = &pg->records[i]; |
| 2070 | p->flags = ref; |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 2071 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2072 | /* |
| 2073 | * Do the initial record conversion from mcount jump |
| 2074 | * to the NOP instructions. |
| 2075 | */ |
| 2076 | if (!ftrace_code_disable(mod, p)) |
| 2077 | break; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2078 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2079 | ftrace_update_cnt++; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2080 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2081 | /* |
| 2082 | * If the tracing is enabled, go ahead and enable the record. |
| 2083 | * |
| 2084 | * The reason not to enable the record immediatelly is the |
| 2085 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 2086 | * correct previous instructions. Making first the NOP |
| 2087 | * conversion puts the module to the correct state, thus |
| 2088 | * passing the ftrace_make_call check. |
| 2089 | */ |
| 2090 | if (ftrace_start_up && ref) { |
| 2091 | int failed = __ftrace_replace_code(p, 1); |
| 2092 | if (failed) |
| 2093 | ftrace_bug(failed, p->ip); |
| 2094 | } |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 2095 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2096 | } |
| 2097 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 2098 | ftrace_new_pgs = NULL; |
| 2099 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2100 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2101 | ftrace_update_time = stop - start; |
| 2102 | ftrace_update_tot_cnt += ftrace_update_cnt; |
| 2103 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
| 2106 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2107 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2108 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2109 | int order; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2110 | int cnt; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2111 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2112 | if (WARN_ON(!count)) |
| 2113 | return -EINVAL; |
| 2114 | |
| 2115 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2116 | |
| 2117 | /* |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2118 | * We want to fill as much as possible. No more than a page |
| 2119 | * may be empty. |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2120 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2121 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
| 2122 | order--; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2123 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 2124 | again: |
| 2125 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 2126 | |
| 2127 | if (!pg->records) { |
| 2128 | /* if we can't allocate this size, try something smaller */ |
| 2129 | if (!order) |
| 2130 | return -ENOMEM; |
| 2131 | order >>= 1; |
| 2132 | goto again; |
| 2133 | } |
| 2134 | |
| 2135 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 2136 | pg->size = cnt; |
| 2137 | |
| 2138 | if (cnt > count) |
| 2139 | cnt = count; |
| 2140 | |
| 2141 | return cnt; |
| 2142 | } |
| 2143 | |
| 2144 | static struct ftrace_page * |
| 2145 | ftrace_allocate_pages(unsigned long num_to_init) |
| 2146 | { |
| 2147 | struct ftrace_page *start_pg; |
| 2148 | struct ftrace_page *pg; |
| 2149 | int order; |
| 2150 | int cnt; |
| 2151 | |
| 2152 | if (!num_to_init) |
| 2153 | return 0; |
| 2154 | |
| 2155 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 2156 | if (!pg) |
| 2157 | return NULL; |
| 2158 | |
| 2159 | /* |
| 2160 | * Try to allocate as much as possible in one continues |
| 2161 | * location that fills in all of the space. We want to |
| 2162 | * waste as little space as possible. |
| 2163 | */ |
| 2164 | for (;;) { |
| 2165 | cnt = ftrace_allocate_records(pg, num_to_init); |
| 2166 | if (cnt < 0) |
| 2167 | goto free_pages; |
| 2168 | |
| 2169 | num_to_init -= cnt; |
| 2170 | if (!num_to_init) |
| 2171 | break; |
| 2172 | |
| 2173 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 2174 | if (!pg->next) |
| 2175 | goto free_pages; |
| 2176 | |
| 2177 | pg = pg->next; |
| 2178 | } |
| 2179 | |
| 2180 | return start_pg; |
| 2181 | |
| 2182 | free_pages: |
| 2183 | while (start_pg) { |
| 2184 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 2185 | free_pages((unsigned long)pg->records, order); |
| 2186 | start_pg = pg->next; |
| 2187 | kfree(pg); |
| 2188 | pg = start_pg; |
| 2189 | } |
| 2190 | pr_info("ftrace: FAILED to allocate memory for functions\n"); |
| 2191 | return NULL; |
| 2192 | } |
| 2193 | |
| 2194 | static int __init ftrace_dyn_table_alloc(unsigned long num_to_init) |
| 2195 | { |
| 2196 | int cnt; |
| 2197 | |
| 2198 | if (!num_to_init) { |
| 2199 | pr_info("ftrace: No functions to be traced?\n"); |
| 2200 | return -1; |
| 2201 | } |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2202 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 2203 | cnt = num_to_init / ENTRIES_PER_PAGE; |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 2204 | pr_info("ftrace: allocating %ld entries in %d pages\n", |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 2205 | num_to_init, cnt + 1); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2206 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2207 | return 0; |
| 2208 | } |
| 2209 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2210 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 2211 | |
| 2212 | struct ftrace_iterator { |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2213 | loff_t pos; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2214 | loff_t func_pos; |
| 2215 | struct ftrace_page *pg; |
| 2216 | struct dyn_ftrace *func; |
| 2217 | struct ftrace_func_probe *probe; |
| 2218 | struct trace_parser parser; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2219 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2220 | struct ftrace_ops *ops; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2221 | int hidx; |
| 2222 | int idx; |
| 2223 | unsigned flags; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2224 | }; |
| 2225 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2226 | static void * |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2227 | t_hash_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2228 | { |
| 2229 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2230 | struct hlist_node *hnd = NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2231 | struct hlist_head *hhd; |
| 2232 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2233 | (*pos)++; |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2234 | iter->pos = *pos; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2235 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2236 | if (iter->probe) |
| 2237 | hnd = &iter->probe->node; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2238 | retry: |
| 2239 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) |
| 2240 | return NULL; |
| 2241 | |
| 2242 | hhd = &ftrace_func_hash[iter->hidx]; |
| 2243 | |
| 2244 | if (hlist_empty(hhd)) { |
| 2245 | iter->hidx++; |
| 2246 | hnd = NULL; |
| 2247 | goto retry; |
| 2248 | } |
| 2249 | |
| 2250 | if (!hnd) |
| 2251 | hnd = hhd->first; |
| 2252 | else { |
| 2253 | hnd = hnd->next; |
| 2254 | if (!hnd) { |
| 2255 | iter->hidx++; |
| 2256 | goto retry; |
| 2257 | } |
| 2258 | } |
| 2259 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2260 | if (WARN_ON_ONCE(!hnd)) |
| 2261 | return NULL; |
| 2262 | |
| 2263 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); |
| 2264 | |
| 2265 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | static void *t_hash_start(struct seq_file *m, loff_t *pos) |
| 2269 | { |
| 2270 | struct ftrace_iterator *iter = m->private; |
| 2271 | void *p = NULL; |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 2272 | loff_t l; |
| 2273 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 2274 | if (!(iter->flags & FTRACE_ITER_DO_HASH)) |
| 2275 | return NULL; |
| 2276 | |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 2277 | if (iter->func_pos > *pos) |
| 2278 | return NULL; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2279 | |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 2280 | iter->hidx = 0; |
Steven Rostedt | 2bccfff | 2010-09-09 08:43:22 -0400 | [diff] [blame] | 2281 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2282 | p = t_hash_next(m, &l); |
Li Zefan | d82d624 | 2009-06-24 09:54:54 +0800 | [diff] [blame] | 2283 | if (!p) |
| 2284 | break; |
| 2285 | } |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2286 | if (!p) |
| 2287 | return NULL; |
| 2288 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2289 | /* Only set this if we have an item */ |
| 2290 | iter->flags |= FTRACE_ITER_HASH; |
| 2291 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2292 | return iter; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2293 | } |
| 2294 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2295 | static int |
| 2296 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2297 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2298 | struct ftrace_func_probe *rec; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2299 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2300 | rec = iter->probe; |
| 2301 | if (WARN_ON_ONCE(!rec)) |
| 2302 | return -EIO; |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2303 | |
Steven Rostedt | 809dcf2 | 2009-02-16 23:06:01 -0500 | [diff] [blame] | 2304 | if (rec->ops->print) |
| 2305 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); |
| 2306 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 2307 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2308 | |
| 2309 | if (rec->data) |
| 2310 | seq_printf(m, ":%p", rec->data); |
| 2311 | seq_putc(m, '\n'); |
| 2312 | |
| 2313 | return 0; |
| 2314 | } |
| 2315 | |
| 2316 | static void * |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2317 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 2318 | { |
| 2319 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2320 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2321 | struct dyn_ftrace *rec = NULL; |
| 2322 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2323 | if (unlikely(ftrace_disabled)) |
| 2324 | return NULL; |
| 2325 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2326 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2327 | return t_hash_next(m, pos); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2328 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2329 | (*pos)++; |
Jiri Olsa | 1106b69 | 2011-02-16 17:35:34 +0100 | [diff] [blame] | 2330 | iter->pos = iter->func_pos = *pos; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2331 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2332 | if (iter->flags & FTRACE_ITER_PRINTALL) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 2333 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2334 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2335 | retry: |
| 2336 | if (iter->idx >= iter->pg->index) { |
| 2337 | if (iter->pg->next) { |
| 2338 | iter->pg = iter->pg->next; |
| 2339 | iter->idx = 0; |
| 2340 | goto retry; |
| 2341 | } |
| 2342 | } else { |
| 2343 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 2344 | if (((iter->flags & FTRACE_ITER_FILTER) && |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2345 | !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) || |
Steven Rostedt | 0183fb1 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 2346 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2347 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2348 | !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) || |
| 2349 | |
| 2350 | ((iter->flags & FTRACE_ITER_ENABLED) && |
| 2351 | !(rec->flags & ~FTRACE_FL_MASK))) { |
| 2352 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2353 | rec = NULL; |
| 2354 | goto retry; |
| 2355 | } |
| 2356 | } |
| 2357 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2358 | if (!rec) |
Steven Rostedt | 57c072c | 2010-09-14 11:21:11 -0400 | [diff] [blame] | 2359 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2360 | |
| 2361 | iter->func = rec; |
| 2362 | |
| 2363 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2364 | } |
| 2365 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2366 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 2367 | { |
| 2368 | iter->pos = 0; |
| 2369 | iter->func_pos = 0; |
| 2370 | iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 2374 | { |
| 2375 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2376 | struct ftrace_ops *ops = iter->ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2377 | void *p = NULL; |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 2378 | loff_t l; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2379 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2380 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2381 | |
| 2382 | if (unlikely(ftrace_disabled)) |
| 2383 | return NULL; |
| 2384 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2385 | /* |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2386 | * If an lseek was done, then reset and start from beginning. |
| 2387 | */ |
| 2388 | if (*pos < iter->pos) |
| 2389 | reset_iter_read(iter); |
| 2390 | |
| 2391 | /* |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2392 | * For set_ftrace_filter reading, if we have the filter |
| 2393 | * off, we can short cut and just print out that all |
| 2394 | * functions are enabled. |
| 2395 | */ |
Steven Rostedt | 06a51d9 | 2011-12-19 19:07:36 -0500 | [diff] [blame] | 2396 | if (iter->flags & FTRACE_ITER_FILTER && |
| 2397 | ftrace_hash_empty(ops->filter_hash)) { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2398 | if (*pos > 0) |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2399 | return t_hash_start(m, pos); |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2400 | iter->flags |= FTRACE_ITER_PRINTALL; |
Chris Wright | df09162 | 2010-09-09 16:34:59 -0700 | [diff] [blame] | 2401 | /* reset in case of seek/pread */ |
| 2402 | iter->flags &= ~FTRACE_ITER_HASH; |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2403 | return iter; |
| 2404 | } |
| 2405 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2406 | if (iter->flags & FTRACE_ITER_HASH) |
| 2407 | return t_hash_start(m, pos); |
| 2408 | |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 2409 | /* |
| 2410 | * Unfortunately, we need to restart at ftrace_pages_start |
| 2411 | * every time we let go of the ftrace_mutex. This is because |
| 2412 | * those pointers can change without the lock. |
| 2413 | */ |
Li Zefan | 694ce0a | 2009-06-24 09:54:19 +0800 | [diff] [blame] | 2414 | iter->pg = ftrace_pages_start; |
| 2415 | iter->idx = 0; |
| 2416 | for (l = 0; l <= *pos; ) { |
| 2417 | p = t_next(m, p, &l); |
| 2418 | if (!p) |
| 2419 | break; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 2420 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 2421 | |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 2422 | if (!p) |
| 2423 | return t_hash_start(m, pos); |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2424 | |
| 2425 | return iter; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | static void t_stop(struct seq_file *m, void *p) |
| 2429 | { |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2430 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | static int t_show(struct seq_file *m, void *v) |
| 2434 | { |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2435 | struct ftrace_iterator *iter = m->private; |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2436 | struct dyn_ftrace *rec; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2437 | |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2438 | if (iter->flags & FTRACE_ITER_HASH) |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2439 | return t_hash_show(m, iter); |
Steven Rostedt | 8fc0c70 | 2009-02-16 15:28:00 -0500 | [diff] [blame] | 2440 | |
Steven Rostedt | 0c75a3e | 2009-02-16 11:21:52 -0500 | [diff] [blame] | 2441 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
| 2442 | seq_printf(m, "#### all functions enabled ####\n"); |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
Steven Rostedt | 4aeb696 | 2010-09-09 10:00:28 -0400 | [diff] [blame] | 2446 | rec = iter->func; |
| 2447 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2448 | if (!rec) |
| 2449 | return 0; |
| 2450 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2451 | seq_printf(m, "%ps", (void *)rec->ip); |
| 2452 | if (iter->flags & FTRACE_ITER_ENABLED) |
| 2453 | seq_printf(m, " (%ld)", |
| 2454 | rec->flags & ~FTRACE_FL_MASK); |
| 2455 | seq_printf(m, "\n"); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2456 | |
| 2457 | return 0; |
| 2458 | } |
| 2459 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 2460 | static const struct seq_operations show_ftrace_seq_ops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2461 | .start = t_start, |
| 2462 | .next = t_next, |
| 2463 | .stop = t_stop, |
| 2464 | .show = t_show, |
| 2465 | }; |
| 2466 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2467 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2468 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 2469 | { |
| 2470 | struct ftrace_iterator *iter; |
| 2471 | int ret; |
| 2472 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2473 | if (unlikely(ftrace_disabled)) |
| 2474 | return -ENODEV; |
| 2475 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2476 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 2477 | if (!iter) |
| 2478 | return -ENOMEM; |
| 2479 | |
| 2480 | iter->pg = ftrace_pages_start; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2481 | iter->ops = &global_ops; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2482 | |
| 2483 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 2484 | if (!ret) { |
| 2485 | struct seq_file *m = file->private_data; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2486 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2487 | m->private = iter; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2488 | } else { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2489 | kfree(iter); |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 2490 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2491 | |
| 2492 | return ret; |
| 2493 | } |
| 2494 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2495 | static int |
| 2496 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 2497 | { |
| 2498 | struct ftrace_iterator *iter; |
| 2499 | int ret; |
| 2500 | |
| 2501 | if (unlikely(ftrace_disabled)) |
| 2502 | return -ENODEV; |
| 2503 | |
| 2504 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 2505 | if (!iter) |
| 2506 | return -ENOMEM; |
| 2507 | |
| 2508 | iter->pg = ftrace_pages_start; |
| 2509 | iter->flags = FTRACE_ITER_ENABLED; |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2510 | iter->ops = &global_ops; |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 2511 | |
| 2512 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 2513 | if (!ret) { |
| 2514 | struct seq_file *m = file->private_data; |
| 2515 | |
| 2516 | m->private = iter; |
| 2517 | } else { |
| 2518 | kfree(iter); |
| 2519 | } |
| 2520 | |
| 2521 | return ret; |
| 2522 | } |
| 2523 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2524 | static void ftrace_filter_reset(struct ftrace_hash *hash) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2525 | { |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2526 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2527 | ftrace_hash_clear(hash); |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2528 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2529 | } |
| 2530 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2531 | /** |
| 2532 | * ftrace_regex_open - initialize function tracer filter files |
| 2533 | * @ops: The ftrace_ops that hold the hash filters |
| 2534 | * @flag: The type of filter to process |
| 2535 | * @inode: The inode, usually passed in to your open routine |
| 2536 | * @file: The file, usually passed in to your open routine |
| 2537 | * |
| 2538 | * ftrace_regex_open() initializes the filter files for the |
| 2539 | * @ops. Depending on @flag it may process the filter hash or |
| 2540 | * the notrace hash of @ops. With this called from the open |
| 2541 | * routine, you can use ftrace_filter_write() for the write |
| 2542 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 2543 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
| 2544 | * ftrace_regex_lseek() should be used as the lseek routine, and |
| 2545 | * release must call ftrace_regex_release(). |
| 2546 | */ |
| 2547 | int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2548 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2549 | struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2550 | { |
| 2551 | struct ftrace_iterator *iter; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2552 | struct ftrace_hash *hash; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2553 | int ret = 0; |
| 2554 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 2555 | if (unlikely(ftrace_disabled)) |
| 2556 | return -ENODEV; |
| 2557 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2558 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 2559 | if (!iter) |
| 2560 | return -ENOMEM; |
| 2561 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 2562 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
| 2563 | kfree(iter); |
| 2564 | return -ENOMEM; |
| 2565 | } |
| 2566 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2567 | if (flag & FTRACE_ITER_NOTRACE) |
| 2568 | hash = ops->notrace_hash; |
| 2569 | else |
| 2570 | hash = ops->filter_hash; |
| 2571 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2572 | iter->ops = ops; |
| 2573 | iter->flags = flag; |
| 2574 | |
| 2575 | if (file->f_mode & FMODE_WRITE) { |
| 2576 | mutex_lock(&ftrace_lock); |
| 2577 | iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash); |
| 2578 | mutex_unlock(&ftrace_lock); |
| 2579 | |
| 2580 | if (!iter->hash) { |
| 2581 | trace_parser_put(&iter->parser); |
| 2582 | kfree(iter); |
| 2583 | return -ENOMEM; |
| 2584 | } |
| 2585 | } |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2586 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2587 | mutex_lock(&ftrace_regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2588 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2589 | if ((file->f_mode & FMODE_WRITE) && |
Steven Rostedt | 8650ae3 | 2009-07-22 23:29:30 -0400 | [diff] [blame] | 2590 | (file->f_flags & O_TRUNC)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2591 | ftrace_filter_reset(iter->hash); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2592 | |
| 2593 | if (file->f_mode & FMODE_READ) { |
| 2594 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2595 | |
| 2596 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 2597 | if (!ret) { |
| 2598 | struct seq_file *m = file->private_data; |
| 2599 | m->private = iter; |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 2600 | } else { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2601 | /* Failed */ |
| 2602 | free_ftrace_hash(iter->hash); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 2603 | trace_parser_put(&iter->parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2604 | kfree(iter); |
Li Zefan | 79fe249 | 2009-09-22 13:54:28 +0800 | [diff] [blame] | 2605 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2606 | } else |
| 2607 | file->private_data = iter; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2608 | mutex_unlock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2609 | |
| 2610 | return ret; |
| 2611 | } |
| 2612 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2613 | static int |
| 2614 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 2615 | { |
Steven Rostedt | 69a3083 | 2011-12-19 15:21:16 -0500 | [diff] [blame] | 2616 | return ftrace_regex_open(&global_ops, |
| 2617 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, |
| 2618 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | static int |
| 2622 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 2623 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 2624 | return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2625 | inode, file); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2626 | } |
| 2627 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 2628 | loff_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 2629 | ftrace_regex_lseek(struct file *file, loff_t offset, int origin) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2630 | { |
| 2631 | loff_t ret; |
| 2632 | |
| 2633 | if (file->f_mode & FMODE_READ) |
| 2634 | ret = seq_lseek(file, offset, origin); |
| 2635 | else |
| 2636 | file->f_pos = ret = 1; |
| 2637 | |
| 2638 | return ret; |
| 2639 | } |
| 2640 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2641 | static int ftrace_match(char *str, char *regex, int len, int type) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2642 | { |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2643 | int matched = 0; |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 2644 | int slen; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2645 | |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2646 | switch (type) { |
| 2647 | case MATCH_FULL: |
| 2648 | if (strcmp(str, regex) == 0) |
| 2649 | matched = 1; |
| 2650 | break; |
| 2651 | case MATCH_FRONT_ONLY: |
| 2652 | if (strncmp(str, regex, len) == 0) |
| 2653 | matched = 1; |
| 2654 | break; |
| 2655 | case MATCH_MIDDLE_ONLY: |
| 2656 | if (strstr(str, regex)) |
| 2657 | matched = 1; |
| 2658 | break; |
| 2659 | case MATCH_END_ONLY: |
Li Zefan | 751e998 | 2010-01-14 10:53:02 +0800 | [diff] [blame] | 2660 | slen = strlen(str); |
| 2661 | if (slen >= len && memcmp(str + slen - len, regex, len) == 0) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2662 | matched = 1; |
| 2663 | break; |
| 2664 | } |
| 2665 | |
| 2666 | return matched; |
| 2667 | } |
| 2668 | |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2669 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2670 | enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not) |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 2671 | { |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2672 | struct ftrace_func_entry *entry; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2673 | int ret = 0; |
| 2674 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2675 | entry = ftrace_lookup_ip(hash, rec->ip); |
| 2676 | if (not) { |
| 2677 | /* Do nothing if it doesn't exist */ |
| 2678 | if (!entry) |
| 2679 | return 0; |
| 2680 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 2681 | free_hash_entry(hash, entry); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2682 | } else { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2683 | /* Do nothing if it exists */ |
| 2684 | if (entry) |
| 2685 | return 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2686 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2687 | ret = add_hash_entry(hash, rec->ip); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2688 | } |
| 2689 | return ret; |
Steven Rostedt | 996e87b | 2011-04-26 16:11:03 -0400 | [diff] [blame] | 2690 | } |
| 2691 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2692 | static int |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2693 | ftrace_match_record(struct dyn_ftrace *rec, char *mod, |
| 2694 | char *regex, int len, int type) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2695 | { |
| 2696 | char str[KSYM_SYMBOL_LEN]; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2697 | char *modname; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2698 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2699 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); |
| 2700 | |
| 2701 | if (mod) { |
| 2702 | /* module lookup requires matching the module */ |
| 2703 | if (!modname || strcmp(modname, mod)) |
| 2704 | return 0; |
| 2705 | |
| 2706 | /* blank search means to match all funcs in the mod */ |
| 2707 | if (!len) |
| 2708 | return 1; |
| 2709 | } |
| 2710 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2711 | return ftrace_match(str, regex, len, type); |
| 2712 | } |
| 2713 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2714 | static int |
| 2715 | match_records(struct ftrace_hash *hash, char *buff, |
| 2716 | int len, char *mod, int not) |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2717 | { |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2718 | unsigned search_len = 0; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2719 | struct ftrace_page *pg; |
| 2720 | struct dyn_ftrace *rec; |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2721 | int type = MATCH_FULL; |
| 2722 | char *search = buff; |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 2723 | int found = 0; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2724 | int ret; |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2725 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2726 | if (len) { |
| 2727 | type = filter_parse_regex(buff, len, &search, ¬); |
| 2728 | search_len = strlen(search); |
| 2729 | } |
Steven Rostedt | 9f4801e | 2009-02-13 15:56:43 -0500 | [diff] [blame] | 2730 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2731 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2732 | |
| 2733 | if (unlikely(ftrace_disabled)) |
| 2734 | goto out_unlock; |
| 2735 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2736 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2737 | if (ftrace_match_record(rec, mod, search, search_len, type)) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2738 | ret = enter_record(hash, rec, not); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2739 | if (ret < 0) { |
| 2740 | found = ret; |
| 2741 | goto out_unlock; |
| 2742 | } |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 2743 | found = 1; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 2744 | } |
| 2745 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2746 | out_unlock: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 2747 | mutex_unlock(&ftrace_lock); |
Li Zefan | 311d16d | 2009-12-08 11:15:11 +0800 | [diff] [blame] | 2748 | |
| 2749 | return found; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 2750 | } |
| 2751 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2752 | static int |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2753 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2754 | { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2755 | return match_records(hash, buff, len, NULL, 0); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2756 | } |
| 2757 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2758 | static int |
| 2759 | ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2760 | { |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2761 | int not = 0; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2762 | |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2763 | /* blank or '*' mean the same */ |
| 2764 | if (strcmp(buff, "*") == 0) |
| 2765 | buff[0] = 0; |
| 2766 | |
| 2767 | /* handle the case of 'dont filter this module' */ |
| 2768 | if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) { |
| 2769 | buff[0] = 0; |
| 2770 | not = 1; |
| 2771 | } |
| 2772 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2773 | return match_records(hash, buff, strlen(buff), mod, not); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 2774 | } |
| 2775 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2776 | /* |
| 2777 | * We register the module command as a template to show others how |
| 2778 | * to register the a command as well. |
| 2779 | */ |
| 2780 | |
| 2781 | static int |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 2782 | ftrace_mod_callback(struct ftrace_hash *hash, |
| 2783 | char *func, char *cmd, char *param, int enable) |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2784 | { |
| 2785 | char *mod; |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2786 | int ret = -EINVAL; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2787 | |
| 2788 | /* |
| 2789 | * cmd == 'mod' because we only registered this func |
| 2790 | * for the 'mod' ftrace_func_command. |
| 2791 | * But if you register one func with multiple commands, |
| 2792 | * you can tell which command was used by the cmd |
| 2793 | * parameter. |
| 2794 | */ |
| 2795 | |
| 2796 | /* we must have a module name */ |
| 2797 | if (!param) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2798 | return ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2799 | |
| 2800 | mod = strsep(¶m, ":"); |
| 2801 | if (!strlen(mod)) |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2802 | return ret; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2803 | |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 2804 | ret = ftrace_match_module_records(hash, func, mod); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 2805 | if (!ret) |
| 2806 | ret = -EINVAL; |
| 2807 | if (ret < 0) |
| 2808 | return ret; |
| 2809 | |
| 2810 | return 0; |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 2814 | .name = "mod", |
| 2815 | .func = ftrace_mod_callback, |
| 2816 | }; |
| 2817 | |
| 2818 | static int __init ftrace_mod_cmd_init(void) |
| 2819 | { |
| 2820 | return register_ftrace_command(&ftrace_mod_cmd); |
| 2821 | } |
| 2822 | device_initcall(ftrace_mod_cmd_init); |
| 2823 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2824 | static void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2825 | function_trace_probe_call(unsigned long ip, unsigned long parent_ip) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2826 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2827 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2828 | struct hlist_head *hhd; |
| 2829 | struct hlist_node *n; |
| 2830 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2831 | |
| 2832 | key = hash_long(ip, FTRACE_HASH_BITS); |
| 2833 | |
| 2834 | hhd = &ftrace_func_hash[key]; |
| 2835 | |
| 2836 | if (hlist_empty(hhd)) |
| 2837 | return; |
| 2838 | |
| 2839 | /* |
| 2840 | * Disable preemption for these calls to prevent a RCU grace |
| 2841 | * period. This syncs the hash iteration and freeing of items |
| 2842 | * on the hash. rcu_read_lock is too dangerous here. |
| 2843 | */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 2844 | preempt_disable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2845 | hlist_for_each_entry_rcu(entry, n, hhd, node) { |
| 2846 | if (entry->ip == ip) |
| 2847 | entry->ops->func(ip, parent_ip, &entry->data); |
| 2848 | } |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 2849 | preempt_enable_notrace(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2850 | } |
| 2851 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2852 | static struct ftrace_ops trace_probe_ops __read_mostly = |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2853 | { |
Steven Rostedt | fb9fb01 | 2009-03-25 13:26:41 -0400 | [diff] [blame] | 2854 | .func = function_trace_probe_call, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2855 | }; |
| 2856 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2857 | static int ftrace_probe_registered; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2858 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2859 | static void __enable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2860 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2861 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2862 | int i; |
| 2863 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2864 | if (ftrace_probe_registered) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2865 | return; |
| 2866 | |
| 2867 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 2868 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 2869 | if (hhd->first) |
| 2870 | break; |
| 2871 | } |
| 2872 | /* Nothing registered? */ |
| 2873 | if (i == FTRACE_FUNC_HASHSIZE) |
| 2874 | return; |
| 2875 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2876 | ret = __register_ftrace_function(&trace_probe_ops); |
| 2877 | if (!ret) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 2878 | ret = ftrace_startup(&trace_probe_ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2879 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2880 | ftrace_probe_registered = 1; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2881 | } |
| 2882 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2883 | static void __disable_ftrace_function_probe(void) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2884 | { |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2885 | int ret; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2886 | int i; |
| 2887 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2888 | if (!ftrace_probe_registered) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2889 | return; |
| 2890 | |
| 2891 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 2892 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 2893 | if (hhd->first) |
| 2894 | return; |
| 2895 | } |
| 2896 | |
| 2897 | /* no more funcs left */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 2898 | ret = __unregister_ftrace_function(&trace_probe_ops); |
| 2899 | if (!ret) |
| 2900 | ftrace_shutdown(&trace_probe_ops, 0); |
| 2901 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2902 | ftrace_probe_registered = 0; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | |
| 2906 | static void ftrace_free_entry_rcu(struct rcu_head *rhp) |
| 2907 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2908 | struct ftrace_func_probe *entry = |
| 2909 | container_of(rhp, struct ftrace_func_probe, rcu); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2910 | |
| 2911 | if (entry->ops->free) |
| 2912 | entry->ops->free(&entry->data); |
| 2913 | kfree(entry); |
| 2914 | } |
| 2915 | |
| 2916 | |
| 2917 | int |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2918 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2919 | void *data) |
| 2920 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2921 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2922 | struct ftrace_page *pg; |
| 2923 | struct dyn_ftrace *rec; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2924 | int type, len, not; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 2925 | unsigned long key; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2926 | int count = 0; |
| 2927 | char *search; |
| 2928 | |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 2929 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2930 | len = strlen(search); |
| 2931 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2932 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2933 | if (WARN_ON(not)) |
| 2934 | return -EINVAL; |
| 2935 | |
| 2936 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2937 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 2938 | if (unlikely(ftrace_disabled)) |
| 2939 | goto out_unlock; |
| 2940 | |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2941 | do_for_each_ftrace_rec(pg, rec) { |
| 2942 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 2943 | if (!ftrace_match_record(rec, NULL, search, len, type)) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2944 | continue; |
| 2945 | |
| 2946 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 2947 | if (!entry) { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2948 | /* If we did not process any, then return error */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2949 | if (!count) |
| 2950 | count = -ENOMEM; |
| 2951 | goto out_unlock; |
| 2952 | } |
| 2953 | |
| 2954 | count++; |
| 2955 | |
| 2956 | entry->data = data; |
| 2957 | |
| 2958 | /* |
| 2959 | * The caller might want to do something special |
| 2960 | * for each function we find. We call the callback |
| 2961 | * to give the caller an opportunity to do so. |
| 2962 | */ |
| 2963 | if (ops->callback) { |
| 2964 | if (ops->callback(rec->ip, &entry->data) < 0) { |
| 2965 | /* caller does not like this func */ |
| 2966 | kfree(entry); |
| 2967 | continue; |
| 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | entry->ops = ops; |
| 2972 | entry->ip = rec->ip; |
| 2973 | |
| 2974 | key = hash_long(entry->ip, FTRACE_HASH_BITS); |
| 2975 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); |
| 2976 | |
| 2977 | } while_for_each_ftrace_rec(); |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2978 | __enable_ftrace_function_probe(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2979 | |
| 2980 | out_unlock: |
| 2981 | mutex_unlock(&ftrace_lock); |
| 2982 | |
| 2983 | return count; |
| 2984 | } |
| 2985 | |
| 2986 | enum { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2987 | PROBE_TEST_FUNC = 1, |
| 2988 | PROBE_TEST_DATA = 2 |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2989 | }; |
| 2990 | |
| 2991 | static void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2992 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2993 | void *data, int flags) |
| 2994 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 2995 | struct ftrace_func_probe *entry; |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 2996 | struct hlist_node *n, *tmp; |
| 2997 | char str[KSYM_SYMBOL_LEN]; |
| 2998 | int type = MATCH_FULL; |
| 2999 | int i, len = 0; |
| 3000 | char *search; |
| 3001 | |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3002 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3003 | glob = NULL; |
Atsushi Tsuji | b36461d | 2009-09-15 19:06:30 +0900 | [diff] [blame] | 3004 | else if (glob) { |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3005 | int not; |
| 3006 | |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 3007 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3008 | len = strlen(search); |
| 3009 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3010 | /* we do not support '!' for function probes */ |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3011 | if (WARN_ON(not)) |
| 3012 | return; |
| 3013 | } |
| 3014 | |
| 3015 | mutex_lock(&ftrace_lock); |
| 3016 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
| 3017 | struct hlist_head *hhd = &ftrace_func_hash[i]; |
| 3018 | |
| 3019 | hlist_for_each_entry_safe(entry, n, tmp, hhd, node) { |
| 3020 | |
| 3021 | /* break up if statements for readability */ |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3022 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3023 | continue; |
| 3024 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3025 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3026 | continue; |
| 3027 | |
| 3028 | /* do this last, since it is the most expensive */ |
| 3029 | if (glob) { |
| 3030 | kallsyms_lookup(entry->ip, NULL, NULL, |
| 3031 | NULL, str); |
| 3032 | if (!ftrace_match(str, glob, len, type)) |
| 3033 | continue; |
| 3034 | } |
| 3035 | |
| 3036 | hlist_del(&entry->node); |
| 3037 | call_rcu(&entry->rcu, ftrace_free_entry_rcu); |
| 3038 | } |
| 3039 | } |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3040 | __disable_ftrace_function_probe(); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3041 | mutex_unlock(&ftrace_lock); |
| 3042 | } |
| 3043 | |
| 3044 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3045 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3046 | void *data) |
| 3047 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3048 | __unregister_ftrace_function_probe(glob, ops, data, |
| 3049 | PROBE_TEST_FUNC | PROBE_TEST_DATA); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | void |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3053 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3054 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3055 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3056 | } |
| 3057 | |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3058 | void unregister_ftrace_function_probe_all(char *glob) |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3059 | { |
Steven Rostedt | b6887d7 | 2009-02-17 12:32:04 -0500 | [diff] [blame] | 3060 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
Steven Rostedt | 59df055f | 2009-02-14 15:29:06 -0500 | [diff] [blame] | 3061 | } |
| 3062 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3063 | static LIST_HEAD(ftrace_commands); |
| 3064 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 3065 | |
| 3066 | int register_ftrace_command(struct ftrace_func_command *cmd) |
| 3067 | { |
| 3068 | struct ftrace_func_command *p; |
| 3069 | int ret = 0; |
| 3070 | |
| 3071 | mutex_lock(&ftrace_cmd_mutex); |
| 3072 | list_for_each_entry(p, &ftrace_commands, list) { |
| 3073 | if (strcmp(cmd->name, p->name) == 0) { |
| 3074 | ret = -EBUSY; |
| 3075 | goto out_unlock; |
| 3076 | } |
| 3077 | } |
| 3078 | list_add(&cmd->list, &ftrace_commands); |
| 3079 | out_unlock: |
| 3080 | mutex_unlock(&ftrace_cmd_mutex); |
| 3081 | |
| 3082 | return ret; |
| 3083 | } |
| 3084 | |
| 3085 | int unregister_ftrace_command(struct ftrace_func_command *cmd) |
| 3086 | { |
| 3087 | struct ftrace_func_command *p, *n; |
| 3088 | int ret = -ENODEV; |
| 3089 | |
| 3090 | mutex_lock(&ftrace_cmd_mutex); |
| 3091 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 3092 | if (strcmp(cmd->name, p->name) == 0) { |
| 3093 | ret = 0; |
| 3094 | list_del_init(&p->list); |
| 3095 | goto out_unlock; |
| 3096 | } |
| 3097 | } |
| 3098 | out_unlock: |
| 3099 | mutex_unlock(&ftrace_cmd_mutex); |
| 3100 | |
| 3101 | return ret; |
| 3102 | } |
| 3103 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3104 | static int ftrace_process_regex(struct ftrace_hash *hash, |
| 3105 | char *buff, int len, int enable) |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3106 | { |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3107 | char *func, *command, *next = buff; |
Steven Rostedt | 6a24a24 | 2009-02-17 11:20:26 -0500 | [diff] [blame] | 3108 | struct ftrace_func_command *p; |
GuoWen Li | 0aff1c0 | 2011-06-01 19:18:47 +0800 | [diff] [blame] | 3109 | int ret = -EINVAL; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3110 | |
| 3111 | func = strsep(&next, ":"); |
| 3112 | |
| 3113 | if (!next) { |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3114 | ret = ftrace_match_records(hash, func, len); |
Steven Rostedt | b448c4e | 2011-04-29 15:12:32 -0400 | [diff] [blame] | 3115 | if (!ret) |
| 3116 | ret = -EINVAL; |
| 3117 | if (ret < 0) |
| 3118 | return ret; |
| 3119 | return 0; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3120 | } |
| 3121 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3122 | /* command found */ |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3123 | |
| 3124 | command = strsep(&next, ":"); |
| 3125 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3126 | mutex_lock(&ftrace_cmd_mutex); |
| 3127 | list_for_each_entry(p, &ftrace_commands, list) { |
| 3128 | if (strcmp(p->name, command) == 0) { |
Steven Rostedt | 43dd61c | 2011-07-07 11:09:22 -0400 | [diff] [blame] | 3129 | ret = p->func(hash, func, command, next, enable); |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3130 | goto out_unlock; |
| 3131 | } |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3132 | } |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3133 | out_unlock: |
| 3134 | mutex_unlock(&ftrace_cmd_mutex); |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3135 | |
Steven Rostedt | f618077 | 2009-02-14 00:40:25 -0500 | [diff] [blame] | 3136 | return ret; |
Steven Rostedt | 64e7c44 | 2009-02-13 17:08:48 -0500 | [diff] [blame] | 3137 | } |
| 3138 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3139 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3140 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 3141 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3142 | { |
| 3143 | struct ftrace_iterator *iter; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3144 | struct trace_parser *parser; |
| 3145 | ssize_t ret, read; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3146 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3147 | if (!cnt) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3148 | return 0; |
| 3149 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3150 | mutex_lock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3151 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3152 | ret = -ENODEV; |
| 3153 | if (unlikely(ftrace_disabled)) |
| 3154 | goto out_unlock; |
| 3155 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3156 | if (file->f_mode & FMODE_READ) { |
| 3157 | struct seq_file *m = file->private_data; |
| 3158 | iter = m->private; |
| 3159 | } else |
| 3160 | iter = file->private_data; |
| 3161 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3162 | parser = &iter->parser; |
| 3163 | read = trace_get_user(parser, ubuf, cnt, ppos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3164 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3165 | if (read >= 0 && trace_parser_loaded(parser) && |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3166 | !trace_parser_cont(parser)) { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3167 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3168 | parser->idx, enable); |
Li Zefan | 313254a | 2009-12-08 11:15:30 +0800 | [diff] [blame] | 3169 | trace_parser_clear(parser); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3170 | if (ret) |
Li Zefan | ed146b25 | 2009-11-03 08:55:38 +0800 | [diff] [blame] | 3171 | goto out_unlock; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3174 | ret = read; |
Li Zefan | ed146b25 | 2009-11-03 08:55:38 +0800 | [diff] [blame] | 3175 | out_unlock: |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3176 | mutex_unlock(&ftrace_regex_lock); |
Li Zefan | ed146b25 | 2009-11-03 08:55:38 +0800 | [diff] [blame] | 3177 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3178 | return ret; |
| 3179 | } |
| 3180 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3181 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3182 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 3183 | size_t cnt, loff_t *ppos) |
| 3184 | { |
| 3185 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 3186 | } |
| 3187 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3188 | ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3189 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 3190 | size_t cnt, loff_t *ppos) |
| 3191 | { |
| 3192 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 3193 | } |
| 3194 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3195 | static int |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3196 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 3197 | int reset, int enable) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3198 | { |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3199 | struct ftrace_hash **orig_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3200 | struct ftrace_hash *hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3201 | int ret; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3202 | |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3203 | /* All global ops uses the global ops filters */ |
| 3204 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) |
| 3205 | ops = &global_ops; |
| 3206 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3207 | if (unlikely(ftrace_disabled)) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3208 | return -ENODEV; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3209 | |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3210 | if (enable) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3211 | orig_hash = &ops->filter_hash; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3212 | else |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3213 | orig_hash = &ops->notrace_hash; |
| 3214 | |
| 3215 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
| 3216 | if (!hash) |
| 3217 | return -ENOMEM; |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3218 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3219 | mutex_lock(&ftrace_regex_lock); |
| 3220 | if (reset) |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3221 | ftrace_filter_reset(hash); |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3222 | if (buf && !ftrace_match_records(hash, buf, len)) { |
| 3223 | ret = -EINVAL; |
| 3224 | goto out_regex_unlock; |
| 3225 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3226 | |
| 3227 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 3228 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
Steven Rostedt | 072126f | 2011-07-13 15:08:31 -0400 | [diff] [blame] | 3229 | if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED |
| 3230 | && ftrace_enabled) |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 3231 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
Steven Rostedt | 072126f | 2011-07-13 15:08:31 -0400 | [diff] [blame] | 3232 | |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3233 | mutex_unlock(&ftrace_lock); |
| 3234 | |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3235 | out_regex_unlock: |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3236 | mutex_unlock(&ftrace_regex_lock); |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3237 | |
| 3238 | free_ftrace_hash(hash); |
| 3239 | return ret; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3240 | } |
| 3241 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3242 | /** |
| 3243 | * ftrace_set_filter - set a function to filter on in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3244 | * @ops - the ops to set the filter with |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3245 | * @buf - the string that holds the function filter text. |
| 3246 | * @len - the length of the string. |
| 3247 | * @reset - non zero to reset all filters before applying this filter. |
| 3248 | * |
| 3249 | * Filters denote which functions should be enabled when tracing is enabled. |
| 3250 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 3251 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3252 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3253 | int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3254 | { |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3255 | return ftrace_set_regex(ops, buf, len, reset, 1); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3256 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3257 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 3258 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3259 | /** |
| 3260 | * ftrace_set_notrace - set a function to not trace in ftrace |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3261 | * @ops - the ops to set the notrace filter with |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3262 | * @buf - the string that holds the function notrace text. |
| 3263 | * @len - the length of the string. |
| 3264 | * @reset - non zero to reset all filters before applying this filter. |
| 3265 | * |
| 3266 | * Notrace Filters denote which functions should not be enabled when tracing |
| 3267 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 3268 | * for tracing. |
| 3269 | */ |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3270 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3271 | int len, int reset) |
| 3272 | { |
Jiri Olsa | ac483c4 | 2012-01-02 10:04:14 +0100 | [diff] [blame] | 3273 | return ftrace_set_regex(ops, buf, len, reset, 0); |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3274 | } |
| 3275 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 3276 | /** |
| 3277 | * ftrace_set_filter - set a function to filter on in ftrace |
| 3278 | * @ops - the ops to set the filter with |
| 3279 | * @buf - the string that holds the function filter text. |
| 3280 | * @len - the length of the string. |
| 3281 | * @reset - non zero to reset all filters before applying this filter. |
| 3282 | * |
| 3283 | * Filters denote which functions should be enabled when tracing is enabled. |
| 3284 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 3285 | */ |
| 3286 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 3287 | { |
| 3288 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
| 3289 | } |
| 3290 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 3291 | |
| 3292 | /** |
| 3293 | * ftrace_set_notrace - set a function to not trace in ftrace |
| 3294 | * @ops - the ops to set the notrace filter with |
| 3295 | * @buf - the string that holds the function notrace text. |
| 3296 | * @len - the length of the string. |
| 3297 | * @reset - non zero to reset all filters before applying this filter. |
| 3298 | * |
| 3299 | * Notrace Filters denote which functions should not be enabled when tracing |
| 3300 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 3301 | * for tracing. |
| 3302 | */ |
| 3303 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3304 | { |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3305 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3306 | } |
Steven Rostedt | 936e074 | 2011-05-05 22:54:01 -0400 | [diff] [blame] | 3307 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3308 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3309 | /* |
| 3310 | * command line interface to allow users to set filters on boot up. |
| 3311 | */ |
| 3312 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 3313 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 3314 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 3315 | |
| 3316 | static int __init set_ftrace_notrace(char *str) |
| 3317 | { |
| 3318 | strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 3319 | return 1; |
| 3320 | } |
| 3321 | __setup("ftrace_notrace=", set_ftrace_notrace); |
| 3322 | |
| 3323 | static int __init set_ftrace_filter(char *str) |
| 3324 | { |
| 3325 | strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
| 3326 | return 1; |
| 3327 | } |
| 3328 | __setup("ftrace_filter=", set_ftrace_filter); |
| 3329 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3330 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Lai Jiangshan | f6060f4 | 2009-11-05 11:16:17 +0800 | [diff] [blame] | 3331 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
Steven Rostedt | 801c29f | 2010-03-05 20:02:19 -0500 | [diff] [blame] | 3332 | static int ftrace_set_func(unsigned long *array, int *idx, char *buffer); |
| 3333 | |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3334 | static int __init set_graph_function(char *str) |
| 3335 | { |
Frederic Weisbecker | 06f43d6 | 2009-10-14 20:43:39 +0200 | [diff] [blame] | 3336 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3337 | return 1; |
| 3338 | } |
| 3339 | __setup("ftrace_graph_filter=", set_graph_function); |
| 3340 | |
| 3341 | static void __init set_ftrace_early_graph(char *buf) |
| 3342 | { |
| 3343 | int ret; |
| 3344 | char *func; |
| 3345 | |
| 3346 | while (buf) { |
| 3347 | func = strsep(&buf, ","); |
| 3348 | /* we allow only one expression at a time */ |
| 3349 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, |
| 3350 | func); |
| 3351 | if (ret) |
| 3352 | printk(KERN_DEBUG "ftrace: function %s not " |
| 3353 | "traceable\n", func); |
| 3354 | } |
| 3355 | } |
| 3356 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 3357 | |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 3358 | void __init |
| 3359 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3360 | { |
| 3361 | char *func; |
| 3362 | |
| 3363 | while (buf) { |
| 3364 | func = strsep(&buf, ","); |
Steven Rostedt | f45948e | 2011-05-02 12:29:25 -0400 | [diff] [blame] | 3365 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | static void __init set_ftrace_early_filters(void) |
| 3370 | { |
| 3371 | if (ftrace_filter_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 3372 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3373 | if (ftrace_notrace_buf[0]) |
Steven Rostedt | 2a85a37 | 2011-12-19 21:57:44 -0500 | [diff] [blame] | 3374 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
Stefan Assmann | 369bc18 | 2009-10-12 22:17:21 +0200 | [diff] [blame] | 3375 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 3376 | if (ftrace_graph_buf[0]) |
| 3377 | set_ftrace_early_graph(ftrace_graph_buf); |
| 3378 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3379 | } |
| 3380 | |
Steven Rostedt | fc13cb0 | 2011-12-19 14:41:25 -0500 | [diff] [blame] | 3381 | int ftrace_regex_release(struct inode *inode, struct file *file) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3382 | { |
| 3383 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 3384 | struct ftrace_iterator *iter; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3385 | struct ftrace_hash **orig_hash; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3386 | struct trace_parser *parser; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 3387 | int filter_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3388 | int ret; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3389 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3390 | mutex_lock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3391 | if (file->f_mode & FMODE_READ) { |
| 3392 | iter = m->private; |
| 3393 | |
| 3394 | seq_release(inode, file); |
| 3395 | } else |
| 3396 | iter = file->private_data; |
| 3397 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3398 | parser = &iter->parser; |
| 3399 | if (trace_parser_loaded(parser)) { |
| 3400 | parser->buffer[parser->idx] = 0; |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3401 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3402 | } |
| 3403 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3404 | trace_parser_put(parser); |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3405 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3406 | if (file->f_mode & FMODE_WRITE) { |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 3407 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 3408 | |
| 3409 | if (filter_hash) |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3410 | orig_hash = &iter->ops->filter_hash; |
Steven Rostedt | ed926f9 | 2011-05-03 13:25:24 -0400 | [diff] [blame] | 3411 | else |
| 3412 | orig_hash = &iter->ops->notrace_hash; |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3413 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3414 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 3415 | ret = ftrace_hash_move(iter->ops, filter_hash, |
| 3416 | orig_hash, iter->hash); |
| 3417 | if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED) |
| 3418 | && ftrace_enabled) |
Jiri Olsa | 30fb6aa | 2011-12-05 18:22:48 +0100 | [diff] [blame] | 3419 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
Steven Rostedt | 41fb61c | 2011-07-13 15:03:44 -0400 | [diff] [blame] | 3420 | |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3421 | mutex_unlock(&ftrace_lock); |
| 3422 | } |
Steven Rostedt | 33dc9b1 | 2011-05-02 17:34:47 -0400 | [diff] [blame] | 3423 | free_ftrace_hash(iter->hash); |
| 3424 | kfree(iter); |
Steven Rostedt | 058e297 | 2011-04-29 22:35:33 -0400 | [diff] [blame] | 3425 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3426 | mutex_unlock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3427 | return 0; |
| 3428 | } |
| 3429 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3430 | static const struct file_operations ftrace_avail_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3431 | .open = ftrace_avail_open, |
| 3432 | .read = seq_read, |
| 3433 | .llseek = seq_lseek, |
Li Zefan | 3be04b4 | 2009-08-17 16:54:03 +0800 | [diff] [blame] | 3434 | .release = seq_release_private, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3435 | }; |
| 3436 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3437 | static const struct file_operations ftrace_enabled_fops = { |
| 3438 | .open = ftrace_enabled_open, |
| 3439 | .read = seq_read, |
| 3440 | .llseek = seq_lseek, |
| 3441 | .release = seq_release_private, |
| 3442 | }; |
| 3443 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3444 | static const struct file_operations ftrace_filter_fops = { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3445 | .open = ftrace_filter_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 3446 | .read = seq_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3447 | .write = ftrace_filter_write, |
Steven Rostedt | 98c4fd0 | 2010-09-10 11:47:43 -0400 | [diff] [blame] | 3448 | .llseek = ftrace_regex_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3449 | .release = ftrace_regex_release, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3450 | }; |
| 3451 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3452 | static const struct file_operations ftrace_notrace_fops = { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3453 | .open = ftrace_notrace_open, |
Lai Jiangshan | 850a80c | 2009-03-13 17:47:23 +0800 | [diff] [blame] | 3454 | .read = seq_read, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3455 | .write = ftrace_notrace_write, |
| 3456 | .llseek = ftrace_regex_lseek, |
Steven Rostedt | 1cf41dd7 | 2011-04-29 20:59:51 -0400 | [diff] [blame] | 3457 | .release = ftrace_regex_release, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3458 | }; |
| 3459 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3460 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 3461 | |
| 3462 | static DEFINE_MUTEX(graph_lock); |
| 3463 | |
| 3464 | int ftrace_graph_count; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3465 | int ftrace_graph_filter_enabled; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3466 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
| 3467 | |
| 3468 | static void * |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3469 | __g_next(struct seq_file *m, loff_t *pos) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3470 | { |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3471 | if (*pos >= ftrace_graph_count) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3472 | return NULL; |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 3473 | return &ftrace_graph_funcs[*pos]; |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3474 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3475 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3476 | static void * |
| 3477 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 3478 | { |
| 3479 | (*pos)++; |
| 3480 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 3484 | { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3485 | mutex_lock(&graph_lock); |
| 3486 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3487 | /* Nothing, tell g_show to print all functions are enabled */ |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3488 | if (!ftrace_graph_filter_enabled && !*pos) |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3489 | return (void *)1; |
| 3490 | |
Li Zefan | 8595184 | 2009-06-24 09:54:00 +0800 | [diff] [blame] | 3491 | return __g_next(m, pos); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | static void g_stop(struct seq_file *m, void *p) |
| 3495 | { |
| 3496 | mutex_unlock(&graph_lock); |
| 3497 | } |
| 3498 | |
| 3499 | static int g_show(struct seq_file *m, void *v) |
| 3500 | { |
| 3501 | unsigned long *ptr = v; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3502 | |
| 3503 | if (!ptr) |
| 3504 | return 0; |
| 3505 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3506 | if (ptr == (unsigned long *)1) { |
| 3507 | seq_printf(m, "#### all functions enabled ####\n"); |
| 3508 | return 0; |
| 3509 | } |
| 3510 | |
Steven Rostedt | b375a11 | 2009-09-17 00:05:58 -0400 | [diff] [blame] | 3511 | seq_printf(m, "%ps\n", (void *)*ptr); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3512 | |
| 3513 | return 0; |
| 3514 | } |
| 3515 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 3516 | static const struct seq_operations ftrace_graph_seq_ops = { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3517 | .start = g_start, |
| 3518 | .next = g_next, |
| 3519 | .stop = g_stop, |
| 3520 | .show = g_show, |
| 3521 | }; |
| 3522 | |
| 3523 | static int |
| 3524 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 3525 | { |
| 3526 | int ret = 0; |
| 3527 | |
| 3528 | if (unlikely(ftrace_disabled)) |
| 3529 | return -ENODEV; |
| 3530 | |
| 3531 | mutex_lock(&graph_lock); |
| 3532 | if ((file->f_mode & FMODE_WRITE) && |
Steven Rostedt | 8650ae3 | 2009-07-22 23:29:30 -0400 | [diff] [blame] | 3533 | (file->f_flags & O_TRUNC)) { |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3534 | ftrace_graph_filter_enabled = 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3535 | ftrace_graph_count = 0; |
| 3536 | memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs)); |
| 3537 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3538 | mutex_unlock(&graph_lock); |
| 3539 | |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 3540 | if (file->f_mode & FMODE_READ) |
| 3541 | ret = seq_open(file, &ftrace_graph_seq_ops); |
| 3542 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3543 | return ret; |
| 3544 | } |
| 3545 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3546 | static int |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 3547 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 3548 | { |
| 3549 | if (file->f_mode & FMODE_READ) |
| 3550 | seq_release(inode, file); |
| 3551 | return 0; |
| 3552 | } |
| 3553 | |
| 3554 | static int |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3555 | ftrace_set_func(unsigned long *array, int *idx, char *buffer) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3556 | { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3557 | struct dyn_ftrace *rec; |
| 3558 | struct ftrace_page *pg; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3559 | int search_len; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3560 | int fail = 1; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3561 | int type, not; |
| 3562 | char *search; |
| 3563 | bool exists; |
| 3564 | int i; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3565 | |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3566 | /* decode regex */ |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 3567 | type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3568 | if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS) |
| 3569 | return -EBUSY; |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3570 | |
| 3571 | search_len = strlen(search); |
| 3572 | |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3573 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3574 | |
| 3575 | if (unlikely(ftrace_disabled)) { |
| 3576 | mutex_unlock(&ftrace_lock); |
| 3577 | return -ENODEV; |
| 3578 | } |
| 3579 | |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3580 | do_for_each_ftrace_rec(pg, rec) { |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3581 | |
Steven Rostedt | b9df92d | 2011-04-28 20:32:08 -0400 | [diff] [blame] | 3582 | if (ftrace_match_record(rec, NULL, search, search_len, type)) { |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3583 | /* if it is in the array */ |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3584 | exists = false; |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3585 | for (i = 0; i < *idx; i++) { |
Frederic Weisbecker | f9349a8 | 2009-02-19 21:13:12 +0100 | [diff] [blame] | 3586 | if (array[i] == rec->ip) { |
| 3587 | exists = true; |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3588 | break; |
| 3589 | } |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | if (!not) { |
| 3593 | fail = 0; |
| 3594 | if (!exists) { |
| 3595 | array[(*idx)++] = rec->ip; |
| 3596 | if (*idx >= FTRACE_GRAPH_MAX_FUNCS) |
| 3597 | goto out; |
| 3598 | } |
| 3599 | } else { |
| 3600 | if (exists) { |
| 3601 | array[i] = array[--(*idx)]; |
| 3602 | array[*idx] = 0; |
| 3603 | fail = 0; |
| 3604 | } |
| 3605 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3606 | } |
Steven Rostedt | 265c831 | 2009-02-13 12:43:56 -0500 | [diff] [blame] | 3607 | } while_for_each_ftrace_rec(); |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3608 | out: |
Steven Rostedt | 52baf11 | 2009-02-14 01:15:39 -0500 | [diff] [blame] | 3609 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3610 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3611 | if (fail) |
| 3612 | return -EINVAL; |
| 3613 | |
| 3614 | ftrace_graph_filter_enabled = 1; |
| 3615 | return 0; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3616 | } |
| 3617 | |
| 3618 | static ssize_t |
| 3619 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 3620 | size_t cnt, loff_t *ppos) |
| 3621 | { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3622 | struct trace_parser parser; |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3623 | ssize_t read, ret; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3624 | |
Li Zefan | c7c6b1f | 2010-02-10 15:43:04 +0800 | [diff] [blame] | 3625 | if (!cnt) |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3626 | return 0; |
| 3627 | |
| 3628 | mutex_lock(&graph_lock); |
| 3629 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3630 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) { |
| 3631 | ret = -ENOMEM; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 3632 | goto out_unlock; |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3633 | } |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3634 | |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3635 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
| 3636 | |
Li Zefan | 4ba7978 | 2009-09-22 13:52:20 +0800 | [diff] [blame] | 3637 | if (read >= 0 && trace_parser_loaded((&parser))) { |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3638 | parser.buffer[parser.idx] = 0; |
| 3639 | |
| 3640 | /* we allow only one expression at a time */ |
Li Zefan | a4ec5e0 | 2009-09-18 14:06:28 +0800 | [diff] [blame] | 3641 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3642 | parser.buffer); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3643 | if (ret) |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 3644 | goto out_free; |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3645 | } |
| 3646 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3647 | ret = read; |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 3648 | |
| 3649 | out_free: |
jolsa@redhat.com | 689fd8b | 2009-09-11 17:29:29 +0200 | [diff] [blame] | 3650 | trace_parser_put(&parser); |
Li Zefan | 1eb90f1 | 2009-09-22 13:52:57 +0800 | [diff] [blame] | 3651 | out_unlock: |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3652 | mutex_unlock(&graph_lock); |
| 3653 | |
| 3654 | return ret; |
| 3655 | } |
| 3656 | |
| 3657 | static const struct file_operations ftrace_graph_fops = { |
Li Zefan | 8782711 | 2009-07-23 11:29:11 +0800 | [diff] [blame] | 3658 | .open = ftrace_graph_open, |
| 3659 | .read = seq_read, |
| 3660 | .write = ftrace_graph_write, |
| 3661 | .release = ftrace_graph_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 3662 | .llseek = seq_lseek, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3663 | }; |
| 3664 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 3665 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 3666 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3667 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3668 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 3669 | trace_create_file("available_filter_functions", 0444, |
| 3670 | d_tracer, NULL, &ftrace_avail_fops); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3671 | |
Steven Rostedt | 647bcd0 | 2011-05-03 14:39:21 -0400 | [diff] [blame] | 3672 | trace_create_file("enabled_functions", 0444, |
| 3673 | d_tracer, NULL, &ftrace_enabled_fops); |
| 3674 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 3675 | trace_create_file("set_ftrace_filter", 0644, d_tracer, |
| 3676 | NULL, &ftrace_filter_fops); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3677 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 3678 | trace_create_file("set_ftrace_notrace", 0644, d_tracer, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 3679 | NULL, &ftrace_notrace_fops); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 3680 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3681 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 3682 | trace_create_file("set_graph_function", 0444, d_tracer, |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3683 | NULL, |
| 3684 | &ftrace_graph_fops); |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 3685 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 3686 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3687 | return 0; |
| 3688 | } |
| 3689 | |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 3690 | static void ftrace_swap_recs(void *a, void *b, int size) |
| 3691 | { |
| 3692 | struct dyn_ftrace *reca = a; |
| 3693 | struct dyn_ftrace *recb = b; |
| 3694 | struct dyn_ftrace t; |
| 3695 | |
| 3696 | t = *reca; |
| 3697 | *reca = *recb; |
| 3698 | *recb = t; |
| 3699 | } |
| 3700 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 3701 | static int ftrace_process_locs(struct module *mod, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 3702 | unsigned long *start, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3703 | unsigned long *end) |
| 3704 | { |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3705 | struct ftrace_page *pg; |
| 3706 | unsigned long count; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3707 | unsigned long *p; |
| 3708 | unsigned long addr; |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 3709 | unsigned long flags = 0; /* Shut up gcc */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3710 | int ret = -ENOMEM; |
| 3711 | |
| 3712 | count = end - start; |
| 3713 | |
| 3714 | if (!count) |
| 3715 | return 0; |
| 3716 | |
| 3717 | pg = ftrace_allocate_pages(count); |
| 3718 | if (!pg) |
| 3719 | return -ENOMEM; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3720 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 3721 | mutex_lock(&ftrace_lock); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3722 | |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3723 | /* |
| 3724 | * Core and each module needs their own pages, as |
| 3725 | * modules will free them when they are removed. |
| 3726 | * Force a new page to be allocated for modules. |
| 3727 | */ |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3728 | if (!mod) { |
| 3729 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 3730 | /* First initialization */ |
| 3731 | ftrace_pages = ftrace_pages_start = pg; |
| 3732 | } else { |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3733 | if (!ftrace_pages) |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3734 | goto out; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3735 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3736 | if (WARN_ON(ftrace_pages->next)) { |
| 3737 | /* Hmm, we have free pages? */ |
| 3738 | while (ftrace_pages->next) |
| 3739 | ftrace_pages = ftrace_pages->next; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3740 | } |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3741 | |
| 3742 | ftrace_pages->next = pg; |
| 3743 | ftrace_pages = pg; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3744 | } |
| 3745 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3746 | p = start; |
| 3747 | while (p < end) { |
| 3748 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 3749 | /* |
| 3750 | * Some architecture linkers will pad between |
| 3751 | * the different mcount_loc sections of different |
| 3752 | * object files to satisfy alignments. |
| 3753 | * Skip any NULL pointers. |
| 3754 | */ |
| 3755 | if (!addr) |
| 3756 | continue; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3757 | if (!ftrace_record_ip(addr)) |
| 3758 | break; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3759 | } |
| 3760 | |
Steven Rostedt | 85ae32a | 2011-12-16 16:30:31 -0500 | [diff] [blame] | 3761 | /* These new locations need to be initialized */ |
| 3762 | ftrace_new_pgs = pg; |
| 3763 | |
Steven Rostedt | 6895061 | 2011-12-16 17:06:45 -0500 | [diff] [blame] | 3764 | /* Make each individual set of pages sorted by ips */ |
| 3765 | for (; pg; pg = pg->next) |
| 3766 | sort(pg->records, pg->index, sizeof(struct dyn_ftrace), |
| 3767 | ftrace_cmp_recs, ftrace_swap_recs); |
| 3768 | |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 3769 | /* |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 3770 | * We only need to disable interrupts on start up |
| 3771 | * because we are modifying code that an interrupt |
| 3772 | * may execute, and the modification is not atomic. |
| 3773 | * But for modules, nothing runs the code we modify |
| 3774 | * until we are finished with it, and there's no |
| 3775 | * reason to cause large interrupt latencies while we do it. |
Steven Rostedt | a4f18ed | 2011-06-07 09:26:46 -0400 | [diff] [blame] | 3776 | */ |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 3777 | if (!mod) |
| 3778 | local_irq_save(flags); |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 3779 | ftrace_update_code(mod); |
Steven Rostedt | 4376cac | 2011-06-24 23:28:13 -0400 | [diff] [blame] | 3780 | if (!mod) |
| 3781 | local_irq_restore(flags); |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3782 | ret = 0; |
| 3783 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 3784 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3785 | |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3786 | return ret; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3787 | } |
| 3788 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3789 | #ifdef CONFIG_MODULES |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3790 | |
| 3791 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 3792 | |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 3793 | void ftrace_release_mod(struct module *mod) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3794 | { |
| 3795 | struct dyn_ftrace *rec; |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3796 | struct ftrace_page **last_pg; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3797 | struct ftrace_page *pg; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3798 | int order; |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3799 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3800 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3801 | |
| 3802 | if (ftrace_disabled) |
| 3803 | goto out_unlock; |
| 3804 | |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3805 | /* |
| 3806 | * Each module has its own ftrace_pages, remove |
| 3807 | * them from the list. |
| 3808 | */ |
| 3809 | last_pg = &ftrace_pages_start; |
| 3810 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 3811 | rec = &pg->records[0]; |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 3812 | if (within_module_core(rec->ip, mod)) { |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3813 | /* |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3814 | * As core pages are first, the first |
| 3815 | * page should never be a module page. |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3816 | */ |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3817 | if (WARN_ON(pg == ftrace_pages_start)) |
| 3818 | goto out_unlock; |
| 3819 | |
| 3820 | /* Check if we are deleting the last page */ |
| 3821 | if (pg == ftrace_pages) |
| 3822 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 3823 | |
| 3824 | *last_pg = pg->next; |
Steven Rostedt | a790087 | 2011-12-16 16:23:44 -0500 | [diff] [blame] | 3825 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
| 3826 | free_pages((unsigned long)pg->records, order); |
| 3827 | kfree(pg); |
Steven Rostedt | 3208230 | 2011-12-16 14:42:37 -0500 | [diff] [blame] | 3828 | } else |
| 3829 | last_pg = &pg->next; |
| 3830 | } |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 3831 | out_unlock: |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3832 | mutex_unlock(&ftrace_lock); |
| 3833 | } |
| 3834 | |
| 3835 | static void ftrace_init_module(struct module *mod, |
| 3836 | unsigned long *start, unsigned long *end) |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 3837 | { |
Steven Rostedt | 00fd61a | 2008-08-15 21:40:04 -0400 | [diff] [blame] | 3838 | if (ftrace_disabled || start == end) |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 3839 | return; |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 3840 | ftrace_process_locs(mod, start, end); |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 3841 | } |
| 3842 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3843 | static int ftrace_module_notify(struct notifier_block *self, |
| 3844 | unsigned long val, void *data) |
| 3845 | { |
| 3846 | struct module *mod = data; |
| 3847 | |
| 3848 | switch (val) { |
| 3849 | case MODULE_STATE_COMING: |
| 3850 | ftrace_init_module(mod, mod->ftrace_callsites, |
| 3851 | mod->ftrace_callsites + |
| 3852 | mod->num_ftrace_callsites); |
| 3853 | break; |
| 3854 | case MODULE_STATE_GOING: |
jolsa@redhat.com | e7247a1 | 2009-10-07 19:00:35 +0200 | [diff] [blame] | 3855 | ftrace_release_mod(mod); |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3856 | break; |
| 3857 | } |
| 3858 | |
| 3859 | return 0; |
| 3860 | } |
| 3861 | #else |
| 3862 | static int ftrace_module_notify(struct notifier_block *self, |
| 3863 | unsigned long val, void *data) |
| 3864 | { |
| 3865 | return 0; |
| 3866 | } |
| 3867 | #endif /* CONFIG_MODULES */ |
| 3868 | |
| 3869 | struct notifier_block ftrace_module_nb = { |
| 3870 | .notifier_call = ftrace_module_notify, |
| 3871 | .priority = 0, |
| 3872 | }; |
| 3873 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3874 | extern unsigned long __start_mcount_loc[]; |
| 3875 | extern unsigned long __stop_mcount_loc[]; |
| 3876 | |
| 3877 | void __init ftrace_init(void) |
| 3878 | { |
| 3879 | unsigned long count, addr, flags; |
| 3880 | int ret; |
| 3881 | |
| 3882 | /* Keep the ftrace pointer to the stub */ |
| 3883 | addr = (unsigned long)ftrace_stub; |
| 3884 | |
| 3885 | local_irq_save(flags); |
| 3886 | ftrace_dyn_arch_init(&addr); |
| 3887 | local_irq_restore(flags); |
| 3888 | |
| 3889 | /* ftrace_dyn_arch_init places the return code in addr */ |
| 3890 | if (addr) |
| 3891 | goto failed; |
| 3892 | |
| 3893 | count = __stop_mcount_loc - __start_mcount_loc; |
| 3894 | |
| 3895 | ret = ftrace_dyn_table_alloc(count); |
| 3896 | if (ret) |
| 3897 | goto failed; |
| 3898 | |
| 3899 | last_ftrace_enabled = ftrace_enabled = 1; |
| 3900 | |
Jiri Olsa | 5cb084b | 2009-10-13 16:33:53 -0400 | [diff] [blame] | 3901 | ret = ftrace_process_locs(NULL, |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 3902 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3903 | __stop_mcount_loc); |
| 3904 | |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3905 | ret = register_module_notifier(&ftrace_module_nb); |
Ming Lei | 24ed0c4 | 2009-05-17 15:31:38 +0800 | [diff] [blame] | 3906 | if (ret) |
Steven Rostedt | 93eb677 | 2009-04-15 13:24:06 -0400 | [diff] [blame] | 3907 | pr_warning("Failed to register trace ftrace module notifier\n"); |
| 3908 | |
Steven Rostedt | 2af15d6 | 2009-05-28 13:37:24 -0400 | [diff] [blame] | 3909 | set_ftrace_early_filters(); |
| 3910 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3911 | return; |
| 3912 | failed: |
| 3913 | ftrace_disabled = 1; |
| 3914 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 3915 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3916 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 3917 | |
Steven Rostedt | 2b49938 | 2011-05-03 22:49:52 -0400 | [diff] [blame] | 3918 | static struct ftrace_ops global_ops = { |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 3919 | .func = ftrace_stub, |
| 3920 | }; |
| 3921 | |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 3922 | static int __init ftrace_nodyn_init(void) |
| 3923 | { |
| 3924 | ftrace_enabled = 1; |
| 3925 | return 0; |
| 3926 | } |
| 3927 | device_initcall(ftrace_nodyn_init); |
| 3928 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 3929 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
| 3930 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 3931 | /* Keep as macros so we do not need to define the commands */ |
Steven Rostedt | 3b6cfdb | 2011-05-23 15:33:49 -0400 | [diff] [blame] | 3932 | # define ftrace_startup(ops, command) \ |
| 3933 | ({ \ |
| 3934 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ |
| 3935 | 0; \ |
| 3936 | }) |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 3937 | # define ftrace_shutdown(ops, command) do { } while (0) |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3938 | # define ftrace_startup_sysctl() do { } while (0) |
| 3939 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3940 | |
| 3941 | static inline int |
| 3942 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip) |
| 3943 | { |
| 3944 | return 1; |
| 3945 | } |
| 3946 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3947 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 3948 | |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3949 | static void |
Jiri Olsa | e248491 | 2012-02-15 15:51:48 +0100 | [diff] [blame] | 3950 | ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip) |
| 3951 | { |
| 3952 | struct ftrace_ops *op; |
| 3953 | |
| 3954 | if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT))) |
| 3955 | return; |
| 3956 | |
| 3957 | /* |
| 3958 | * Some of the ops may be dynamically allocated, |
| 3959 | * they must be freed after a synchronize_sched(). |
| 3960 | */ |
| 3961 | preempt_disable_notrace(); |
| 3962 | trace_recursion_set(TRACE_CONTROL_BIT); |
| 3963 | op = rcu_dereference_raw(ftrace_control_list); |
| 3964 | while (op != &ftrace_list_end) { |
| 3965 | if (!ftrace_function_local_disabled(op) && |
| 3966 | ftrace_ops_test(op, ip)) |
| 3967 | op->func(ip, parent_ip); |
| 3968 | |
| 3969 | op = rcu_dereference_raw(op->next); |
| 3970 | }; |
| 3971 | trace_recursion_clear(TRACE_CONTROL_BIT); |
| 3972 | preempt_enable_notrace(); |
| 3973 | } |
| 3974 | |
| 3975 | static struct ftrace_ops control_ops = { |
| 3976 | .func = ftrace_ops_control_func, |
| 3977 | }; |
| 3978 | |
| 3979 | static void |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3980 | ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip) |
| 3981 | { |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 3982 | struct ftrace_ops *op; |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3983 | |
Steven Rostedt | b1cff0a | 2011-05-25 14:27:43 -0400 | [diff] [blame] | 3984 | if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT))) |
| 3985 | return; |
| 3986 | |
| 3987 | trace_recursion_set(TRACE_INTERNAL_BIT); |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 3988 | /* |
| 3989 | * Some of the ops may be dynamically allocated, |
| 3990 | * they must be freed after a synchronize_sched(). |
| 3991 | */ |
| 3992 | preempt_disable_notrace(); |
| 3993 | op = rcu_dereference_raw(ftrace_ops_list); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 3994 | while (op != &ftrace_list_end) { |
| 3995 | if (ftrace_ops_test(op, ip)) |
| 3996 | op->func(ip, parent_ip); |
| 3997 | op = rcu_dereference_raw(op->next); |
| 3998 | }; |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 3999 | preempt_enable_notrace(); |
Steven Rostedt | b1cff0a | 2011-05-25 14:27:43 -0400 | [diff] [blame] | 4000 | trace_recursion_clear(TRACE_INTERNAL_BIT); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4001 | } |
| 4002 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4003 | static void clear_ftrace_swapper(void) |
| 4004 | { |
| 4005 | struct task_struct *p; |
| 4006 | int cpu; |
| 4007 | |
| 4008 | get_online_cpus(); |
| 4009 | for_each_online_cpu(cpu) { |
| 4010 | p = idle_task(cpu); |
| 4011 | clear_tsk_trace_trace(p); |
| 4012 | } |
| 4013 | put_online_cpus(); |
| 4014 | } |
| 4015 | |
| 4016 | static void set_ftrace_swapper(void) |
| 4017 | { |
| 4018 | struct task_struct *p; |
| 4019 | int cpu; |
| 4020 | |
| 4021 | get_online_cpus(); |
| 4022 | for_each_online_cpu(cpu) { |
| 4023 | p = idle_task(cpu); |
| 4024 | set_tsk_trace_trace(p); |
| 4025 | } |
| 4026 | put_online_cpus(); |
| 4027 | } |
| 4028 | |
| 4029 | static void clear_ftrace_pid(struct pid *pid) |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4030 | { |
| 4031 | struct task_struct *p; |
| 4032 | |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4033 | rcu_read_lock(); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4034 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4035 | clear_tsk_trace_trace(p); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4036 | } while_each_pid_task(pid, PIDTYPE_PID, p); |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4037 | rcu_read_unlock(); |
| 4038 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4039 | put_pid(pid); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4040 | } |
| 4041 | |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4042 | static void set_ftrace_pid(struct pid *pid) |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4043 | { |
| 4044 | struct task_struct *p; |
| 4045 | |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4046 | rcu_read_lock(); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4047 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
| 4048 | set_tsk_trace_trace(p); |
| 4049 | } while_each_pid_task(pid, PIDTYPE_PID, p); |
Oleg Nesterov | 229c4ef | 2009-02-03 20:39:04 +0100 | [diff] [blame] | 4050 | rcu_read_unlock(); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4051 | } |
| 4052 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4053 | static void clear_ftrace_pid_task(struct pid *pid) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4054 | { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4055 | if (pid == ftrace_swapper_pid) |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4056 | clear_ftrace_swapper(); |
| 4057 | else |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4058 | clear_ftrace_pid(pid); |
Steven Rostedt | e32d895 | 2008-12-04 00:26:41 -0500 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | static void set_ftrace_pid_task(struct pid *pid) |
| 4062 | { |
| 4063 | if (pid == ftrace_swapper_pid) |
| 4064 | set_ftrace_swapper(); |
| 4065 | else |
| 4066 | set_ftrace_pid(pid); |
| 4067 | } |
| 4068 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4069 | static int ftrace_pid_add(int p) |
| 4070 | { |
| 4071 | struct pid *pid; |
| 4072 | struct ftrace_pid *fpid; |
| 4073 | int ret = -EINVAL; |
| 4074 | |
| 4075 | mutex_lock(&ftrace_lock); |
| 4076 | |
| 4077 | if (!p) |
| 4078 | pid = ftrace_swapper_pid; |
| 4079 | else |
| 4080 | pid = find_get_pid(p); |
| 4081 | |
| 4082 | if (!pid) |
| 4083 | goto out; |
| 4084 | |
| 4085 | ret = 0; |
| 4086 | |
| 4087 | list_for_each_entry(fpid, &ftrace_pids, list) |
| 4088 | if (fpid->pid == pid) |
| 4089 | goto out_put; |
| 4090 | |
| 4091 | ret = -ENOMEM; |
| 4092 | |
| 4093 | fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); |
| 4094 | if (!fpid) |
| 4095 | goto out_put; |
| 4096 | |
| 4097 | list_add(&fpid->list, &ftrace_pids); |
| 4098 | fpid->pid = pid; |
| 4099 | |
| 4100 | set_ftrace_pid_task(pid); |
| 4101 | |
| 4102 | ftrace_update_pid_func(); |
| 4103 | ftrace_startup_enable(0); |
| 4104 | |
| 4105 | mutex_unlock(&ftrace_lock); |
| 4106 | return 0; |
| 4107 | |
| 4108 | out_put: |
| 4109 | if (pid != ftrace_swapper_pid) |
| 4110 | put_pid(pid); |
| 4111 | |
| 4112 | out: |
| 4113 | mutex_unlock(&ftrace_lock); |
| 4114 | return ret; |
| 4115 | } |
| 4116 | |
| 4117 | static void ftrace_pid_reset(void) |
| 4118 | { |
| 4119 | struct ftrace_pid *fpid, *safe; |
| 4120 | |
| 4121 | mutex_lock(&ftrace_lock); |
| 4122 | list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { |
| 4123 | struct pid *pid = fpid->pid; |
| 4124 | |
| 4125 | clear_ftrace_pid_task(pid); |
| 4126 | |
| 4127 | list_del(&fpid->list); |
| 4128 | kfree(fpid); |
| 4129 | } |
| 4130 | |
| 4131 | ftrace_update_pid_func(); |
| 4132 | ftrace_startup_enable(0); |
| 4133 | |
| 4134 | mutex_unlock(&ftrace_lock); |
| 4135 | } |
| 4136 | |
| 4137 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
| 4138 | { |
| 4139 | mutex_lock(&ftrace_lock); |
| 4140 | |
| 4141 | if (list_empty(&ftrace_pids) && (!*pos)) |
| 4142 | return (void *) 1; |
| 4143 | |
| 4144 | return seq_list_start(&ftrace_pids, *pos); |
| 4145 | } |
| 4146 | |
| 4147 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 4148 | { |
| 4149 | if (v == (void *)1) |
| 4150 | return NULL; |
| 4151 | |
| 4152 | return seq_list_next(v, &ftrace_pids, pos); |
| 4153 | } |
| 4154 | |
| 4155 | static void fpid_stop(struct seq_file *m, void *p) |
| 4156 | { |
| 4157 | mutex_unlock(&ftrace_lock); |
| 4158 | } |
| 4159 | |
| 4160 | static int fpid_show(struct seq_file *m, void *v) |
| 4161 | { |
| 4162 | const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); |
| 4163 | |
| 4164 | if (v == (void *)1) { |
| 4165 | seq_printf(m, "no pid\n"); |
| 4166 | return 0; |
| 4167 | } |
| 4168 | |
| 4169 | if (fpid->pid == ftrace_swapper_pid) |
| 4170 | seq_printf(m, "swapper tasks\n"); |
| 4171 | else |
| 4172 | seq_printf(m, "%u\n", pid_vnr(fpid->pid)); |
| 4173 | |
| 4174 | return 0; |
| 4175 | } |
| 4176 | |
| 4177 | static const struct seq_operations ftrace_pid_sops = { |
| 4178 | .start = fpid_start, |
| 4179 | .next = fpid_next, |
| 4180 | .stop = fpid_stop, |
| 4181 | .show = fpid_show, |
| 4182 | }; |
| 4183 | |
| 4184 | static int |
| 4185 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 4186 | { |
| 4187 | int ret = 0; |
| 4188 | |
| 4189 | if ((file->f_mode & FMODE_WRITE) && |
| 4190 | (file->f_flags & O_TRUNC)) |
| 4191 | ftrace_pid_reset(); |
| 4192 | |
| 4193 | if (file->f_mode & FMODE_READ) |
| 4194 | ret = seq_open(file, &ftrace_pid_sops); |
| 4195 | |
| 4196 | return ret; |
| 4197 | } |
| 4198 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4199 | static ssize_t |
| 4200 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 4201 | size_t cnt, loff_t *ppos) |
| 4202 | { |
Ingo Molnar | 457dc92 | 2009-11-23 11:03:28 +0100 | [diff] [blame] | 4203 | char buf[64], *tmp; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4204 | long val; |
| 4205 | int ret; |
| 4206 | |
| 4207 | if (cnt >= sizeof(buf)) |
| 4208 | return -EINVAL; |
| 4209 | |
| 4210 | if (copy_from_user(&buf, ubuf, cnt)) |
| 4211 | return -EFAULT; |
| 4212 | |
| 4213 | buf[cnt] = 0; |
| 4214 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4215 | /* |
| 4216 | * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" |
| 4217 | * to clean the filter quietly. |
| 4218 | */ |
Ingo Molnar | 457dc92 | 2009-11-23 11:03:28 +0100 | [diff] [blame] | 4219 | tmp = strstrip(buf); |
| 4220 | if (strlen(tmp) == 0) |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4221 | return 1; |
| 4222 | |
Ingo Molnar | 457dc92 | 2009-11-23 11:03:28 +0100 | [diff] [blame] | 4223 | ret = strict_strtol(tmp, 10, &val); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4224 | if (ret < 0) |
| 4225 | return ret; |
| 4226 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4227 | ret = ftrace_pid_add(val); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame] | 4228 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4229 | return ret ? ret : cnt; |
| 4230 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4231 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4232 | static int |
| 4233 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 4234 | { |
| 4235 | if (file->f_mode & FMODE_READ) |
| 4236 | seq_release(inode, file); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4237 | |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4238 | return 0; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4239 | } |
| 4240 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4241 | static const struct file_operations ftrace_pid_fops = { |
jolsa@redhat.com | 756d17e | 2009-10-13 16:33:52 -0400 | [diff] [blame] | 4242 | .open = ftrace_pid_open, |
| 4243 | .write = ftrace_pid_write, |
| 4244 | .read = seq_read, |
| 4245 | .llseek = seq_lseek, |
| 4246 | .release = ftrace_pid_release, |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4247 | }; |
| 4248 | |
| 4249 | static __init int ftrace_init_debugfs(void) |
| 4250 | { |
| 4251 | struct dentry *d_tracer; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4252 | |
| 4253 | d_tracer = tracing_init_dentry(); |
| 4254 | if (!d_tracer) |
| 4255 | return 0; |
| 4256 | |
| 4257 | ftrace_init_dyn_debugfs(d_tracer); |
| 4258 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 4259 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
| 4260 | NULL, &ftrace_pid_fops); |
Steven Rostedt | 493762f | 2009-03-23 17:12:36 -0400 | [diff] [blame] | 4261 | |
| 4262 | ftrace_profile_debugfs(d_tracer); |
| 4263 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4264 | return 0; |
| 4265 | } |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 4266 | fs_initcall(ftrace_init_debugfs); |
| 4267 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4268 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 4269 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 4270 | * |
| 4271 | * This function should be used by panic code. It stops ftrace |
| 4272 | * but in a not so nice way. If you need to simply kill ftrace |
| 4273 | * from a non-atomic section, use ftrace_kill. |
| 4274 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 4275 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 4276 | { |
| 4277 | ftrace_disabled = 1; |
| 4278 | ftrace_enabled = 0; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 4279 | clear_ftrace_function(); |
| 4280 | } |
| 4281 | |
| 4282 | /** |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 4283 | * Test if ftrace is dead or not. |
| 4284 | */ |
| 4285 | int ftrace_is_dead(void) |
| 4286 | { |
| 4287 | return ftrace_disabled; |
| 4288 | } |
| 4289 | |
| 4290 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4291 | * register_ftrace_function - register a function for profiling |
| 4292 | * @ops - ops structure that holds the function for profiling. |
| 4293 | * |
| 4294 | * Register a function to be called by all functions in the |
| 4295 | * kernel. |
| 4296 | * |
| 4297 | * Note: @ops->func and all the functions it calls must be labeled |
| 4298 | * with "notrace", otherwise it will go into a |
| 4299 | * recursive loop. |
| 4300 | */ |
| 4301 | int register_ftrace_function(struct ftrace_ops *ops) |
| 4302 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4303 | int ret = -1; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4304 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4305 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4306 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4307 | if (unlikely(ftrace_disabled)) |
| 4308 | goto out_unlock; |
| 4309 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4310 | ret = __register_ftrace_function(ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4311 | if (!ret) |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 4312 | ret = ftrace_startup(ops, 0); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4313 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4314 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4315 | out_unlock: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4316 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4317 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4318 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 4319 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4320 | |
| 4321 | /** |
Uwe Kleine-Koenig | 3263292 | 2009-01-12 23:35:50 +0100 | [diff] [blame] | 4322 | * unregister_ftrace_function - unregister a function for profiling. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4323 | * @ops - ops structure that holds the function to unregister |
| 4324 | * |
| 4325 | * Unregister a function that was added to be called by ftrace profiling. |
| 4326 | */ |
| 4327 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 4328 | { |
| 4329 | int ret; |
| 4330 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4331 | mutex_lock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4332 | ret = __unregister_ftrace_function(ops); |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4333 | if (!ret) |
| 4334 | ftrace_shutdown(ops, 0); |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4335 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4336 | |
| 4337 | return ret; |
| 4338 | } |
Steven Rostedt | cdbe61b | 2011-05-05 21:14:55 -0400 | [diff] [blame] | 4339 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4340 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4341 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4342 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 4343 | void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4344 | loff_t *ppos) |
| 4345 | { |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4346 | int ret = -ENODEV; |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 4347 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4348 | mutex_lock(&ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4349 | |
Steven Rostedt | 45a4a23 | 2011-04-21 23:16:46 -0400 | [diff] [blame] | 4350 | if (unlikely(ftrace_disabled)) |
| 4351 | goto out; |
| 4352 | |
| 4353 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4354 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 4355 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4356 | goto out; |
| 4357 | |
Li Zefan | a32c776 | 2009-06-26 16:55:51 +0800 | [diff] [blame] | 4358 | last_ftrace_enabled = !!ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4359 | |
| 4360 | if (ftrace_enabled) { |
| 4361 | |
| 4362 | ftrace_startup_sysctl(); |
| 4363 | |
| 4364 | /* we are starting ftrace again */ |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4365 | if (ftrace_ops_list != &ftrace_list_end) { |
| 4366 | if (ftrace_ops_list->next == &ftrace_list_end) |
| 4367 | ftrace_trace_function = ftrace_ops_list->func; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4368 | else |
Steven Rostedt | b848914 | 2011-05-04 09:27:52 -0400 | [diff] [blame] | 4369 | ftrace_trace_function = ftrace_ops_list_func; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | } else { |
| 4373 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 4374 | ftrace_trace_function = ftrace_stub; |
| 4375 | |
| 4376 | ftrace_shutdown_sysctl(); |
| 4377 | } |
| 4378 | |
| 4379 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4380 | mutex_unlock(&ftrace_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4381 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4382 | } |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 4383 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4384 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4385 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4386 | static int ftrace_graph_active; |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 4387 | static struct notifier_block ftrace_suspend_notifier; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4388 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 4389 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 4390 | { |
| 4391 | return 0; |
| 4392 | } |
| 4393 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 4394 | /* The callbacks that hook a function */ |
| 4395 | trace_func_graph_ret_t ftrace_graph_return = |
| 4396 | (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 4397 | trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4398 | |
| 4399 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 4400 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 4401 | { |
| 4402 | int i; |
| 4403 | int ret = 0; |
| 4404 | unsigned long flags; |
| 4405 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 4406 | struct task_struct *g, *t; |
| 4407 | |
| 4408 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 4409 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH |
| 4410 | * sizeof(struct ftrace_ret_stack), |
| 4411 | GFP_KERNEL); |
| 4412 | if (!ret_stack_list[i]) { |
| 4413 | start = 0; |
| 4414 | end = i; |
| 4415 | ret = -ENOMEM; |
| 4416 | goto free; |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | read_lock_irqsave(&tasklist_lock, flags); |
| 4421 | do_each_thread(g, t) { |
| 4422 | if (start == end) { |
| 4423 | ret = -EAGAIN; |
| 4424 | goto unlock; |
| 4425 | } |
| 4426 | |
| 4427 | if (t->ret_stack == NULL) { |
Frederic Weisbecker | 380c4b1 | 2008-12-06 03:43:41 +0100 | [diff] [blame] | 4428 | atomic_set(&t->tracing_graph_pause, 0); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4429 | atomic_set(&t->trace_overrun, 0); |
Steven Rostedt | 26c0162 | 2009-06-02 14:01:19 -0400 | [diff] [blame] | 4430 | t->curr_ret_stack = -1; |
| 4431 | /* Make sure the tasks see the -1 first: */ |
| 4432 | smp_wmb(); |
| 4433 | t->ret_stack = ret_stack_list[start++]; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4434 | } |
| 4435 | } while_each_thread(g, t); |
| 4436 | |
| 4437 | unlock: |
| 4438 | read_unlock_irqrestore(&tasklist_lock, flags); |
| 4439 | free: |
| 4440 | for (i = start; i < end; i++) |
| 4441 | kfree(ret_stack_list[i]); |
| 4442 | return ret; |
| 4443 | } |
| 4444 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4445 | static void |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 4446 | ftrace_graph_probe_sched_switch(void *ignore, |
| 4447 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4448 | { |
| 4449 | unsigned long long timestamp; |
| 4450 | int index; |
| 4451 | |
Steven Rostedt | be6f164 | 2009-03-24 11:06:24 -0400 | [diff] [blame] | 4452 | /* |
| 4453 | * Does the user want to count the time a function was asleep. |
| 4454 | * If so, do not update the time stamps. |
| 4455 | */ |
| 4456 | if (trace_flags & TRACE_ITER_SLEEP_TIME) |
| 4457 | return; |
| 4458 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4459 | timestamp = trace_clock_local(); |
| 4460 | |
| 4461 | prev->ftrace_timestamp = timestamp; |
| 4462 | |
| 4463 | /* only process tasks that we timestamped */ |
| 4464 | if (!next->ftrace_timestamp) |
| 4465 | return; |
| 4466 | |
| 4467 | /* |
| 4468 | * Update all the counters in next to make up for the |
| 4469 | * time next was sleeping. |
| 4470 | */ |
| 4471 | timestamp -= next->ftrace_timestamp; |
| 4472 | |
| 4473 | for (index = next->curr_ret_stack; index >= 0; index--) |
| 4474 | next->ret_stack[index].calltime += timestamp; |
| 4475 | } |
| 4476 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4477 | /* Allocate a return stack for each task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4478 | static int start_graph_tracing(void) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4479 | { |
| 4480 | struct ftrace_ret_stack **ret_stack_list; |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 4481 | int ret, cpu; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4482 | |
| 4483 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * |
| 4484 | sizeof(struct ftrace_ret_stack *), |
| 4485 | GFP_KERNEL); |
| 4486 | |
| 4487 | if (!ret_stack_list) |
| 4488 | return -ENOMEM; |
| 4489 | |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 4490 | /* The cpu_boot init_task->ret_stack will never be freed */ |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 4491 | for_each_online_cpu(cpu) { |
| 4492 | if (!idle_task(cpu)->ret_stack) |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 4493 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
Steven Rostedt | 179c498 | 2009-06-02 12:03:19 -0400 | [diff] [blame] | 4494 | } |
Frederic Weisbecker | 5b058bc | 2009-02-17 18:35:34 +0100 | [diff] [blame] | 4495 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4496 | do { |
| 4497 | ret = alloc_retstack_tasklist(ret_stack_list); |
| 4498 | } while (ret == -EAGAIN); |
| 4499 | |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4500 | if (!ret) { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 4501 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Steven Rostedt | 8aef2d2 | 2009-03-24 01:10:15 -0400 | [diff] [blame] | 4502 | if (ret) |
| 4503 | pr_info("ftrace_graph: Couldn't activate tracepoint" |
| 4504 | " probe to kernel_sched_switch\n"); |
| 4505 | } |
| 4506 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4507 | kfree(ret_stack_list); |
| 4508 | return ret; |
| 4509 | } |
| 4510 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 4511 | /* |
| 4512 | * Hibernation protection. |
| 4513 | * The state of the current task is too much unstable during |
| 4514 | * suspend/restore to disk. We want to protect against that. |
| 4515 | */ |
| 4516 | static int |
| 4517 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, |
| 4518 | void *unused) |
| 4519 | { |
| 4520 | switch (state) { |
| 4521 | case PM_HIBERNATION_PREPARE: |
| 4522 | pause_graph_tracing(); |
| 4523 | break; |
| 4524 | |
| 4525 | case PM_POST_HIBERNATION: |
| 4526 | unpause_graph_tracing(); |
| 4527 | break; |
| 4528 | } |
| 4529 | return NOTIFY_DONE; |
| 4530 | } |
| 4531 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 4532 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 4533 | trace_func_graph_ent_t entryfunc) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 4534 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4535 | int ret = 0; |
| 4536 | |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4537 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4538 | |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 4539 | /* we currently allow only one tracer registered at a time */ |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4540 | if (ftrace_graph_active) { |
Steven Rostedt | 05ce581 | 2009-03-24 00:18:31 -0400 | [diff] [blame] | 4541 | ret = -EBUSY; |
| 4542 | goto out; |
| 4543 | } |
| 4544 | |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 4545 | ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call; |
| 4546 | register_pm_notifier(&ftrace_suspend_notifier); |
| 4547 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4548 | ftrace_graph_active++; |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4549 | ret = start_graph_tracing(); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4550 | if (ret) { |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4551 | ftrace_graph_active--; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4552 | goto out; |
| 4553 | } |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 4554 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 4555 | ftrace_graph_return = retfunc; |
| 4556 | ftrace_graph_entry = entryfunc; |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 4557 | |
Steven Rostedt | a1cd617 | 2011-05-23 15:24:25 -0400 | [diff] [blame] | 4558 | ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4559 | |
| 4560 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4561 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4562 | return ret; |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 4563 | } |
| 4564 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4565 | void unregister_ftrace_graph(void) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 4566 | { |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4567 | mutex_lock(&ftrace_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4568 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4569 | if (unlikely(!ftrace_graph_active)) |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 4570 | goto out; |
| 4571 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4572 | ftrace_graph_active--; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 4573 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 4574 | ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt | bd69c30 | 2011-05-03 21:55:54 -0400 | [diff] [blame] | 4575 | ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET); |
Frederic Weisbecker | 4a2b8dd | 2009-01-14 13:33:27 -0800 | [diff] [blame] | 4576 | unregister_pm_notifier(&ftrace_suspend_notifier); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 4577 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 4578 | |
Steven Rostedt | 2aad1b7 | 2009-03-30 11:11:28 -0400 | [diff] [blame] | 4579 | out: |
Steven Rostedt | e6ea44e | 2009-02-14 01:42:44 -0500 | [diff] [blame] | 4580 | mutex_unlock(&ftrace_lock); |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 4581 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4582 | |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 4583 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
| 4584 | |
| 4585 | static void |
| 4586 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) |
| 4587 | { |
| 4588 | atomic_set(&t->tracing_graph_pause, 0); |
| 4589 | atomic_set(&t->trace_overrun, 0); |
| 4590 | t->ftrace_timestamp = 0; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4591 | /* make curr_ret_stack visible before we add the ret_stack */ |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 4592 | smp_wmb(); |
| 4593 | t->ret_stack = ret_stack; |
| 4594 | } |
| 4595 | |
| 4596 | /* |
| 4597 | * Allocate a return stack for the idle task. May be the first |
| 4598 | * time through, or it may be done by CPU hotplug online. |
| 4599 | */ |
| 4600 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) |
| 4601 | { |
| 4602 | t->curr_ret_stack = -1; |
| 4603 | /* |
| 4604 | * The idle task has no parent, it either has its own |
| 4605 | * stack or no stack at all. |
| 4606 | */ |
| 4607 | if (t->ret_stack) |
| 4608 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); |
| 4609 | |
| 4610 | if (ftrace_graph_active) { |
| 4611 | struct ftrace_ret_stack *ret_stack; |
| 4612 | |
| 4613 | ret_stack = per_cpu(idle_ret_stack, cpu); |
| 4614 | if (!ret_stack) { |
| 4615 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
| 4616 | * sizeof(struct ftrace_ret_stack), |
| 4617 | GFP_KERNEL); |
| 4618 | if (!ret_stack) |
| 4619 | return; |
| 4620 | per_cpu(idle_ret_stack, cpu) = ret_stack; |
| 4621 | } |
| 4622 | graph_init_task(t, ret_stack); |
| 4623 | } |
| 4624 | } |
| 4625 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4626 | /* Allocate a return stack for newly created task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4627 | void ftrace_graph_init_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4628 | { |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 4629 | /* Make sure we do not use the parent ret_stack */ |
| 4630 | t->ret_stack = NULL; |
Steven Rostedt | ea14eb7 | 2010-03-12 19:41:23 -0500 | [diff] [blame] | 4631 | t->curr_ret_stack = -1; |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 4632 | |
Steven Rostedt | 597af81 | 2009-04-03 15:24:12 -0400 | [diff] [blame] | 4633 | if (ftrace_graph_active) { |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 4634 | struct ftrace_ret_stack *ret_stack; |
| 4635 | |
| 4636 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4637 | * sizeof(struct ftrace_ret_stack), |
| 4638 | GFP_KERNEL); |
Steven Rostedt | 82310a3 | 2009-06-02 12:26:07 -0400 | [diff] [blame] | 4639 | if (!ret_stack) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4640 | return; |
Steven Rostedt | 868baf0 | 2011-02-10 21:26:13 -0500 | [diff] [blame] | 4641 | graph_init_task(t, ret_stack); |
Steven Rostedt | 84047e3 | 2009-06-02 16:51:55 -0400 | [diff] [blame] | 4642 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4643 | } |
| 4644 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 4645 | void ftrace_graph_exit_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4646 | { |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 4647 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 4648 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4649 | t->ret_stack = NULL; |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 4650 | /* NULL must become visible to IRQs before we free it: */ |
| 4651 | barrier(); |
| 4652 | |
| 4653 | kfree(ret_stack); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 4654 | } |
Steven Rostedt | 14a866c | 2008-12-02 23:50:02 -0500 | [diff] [blame] | 4655 | |
| 4656 | void ftrace_graph_stop(void) |
| 4657 | { |
| 4658 | ftrace_stop(); |
| 4659 | } |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 4660 | #endif |