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