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