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