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