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