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