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