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> |
| 20 | #include <linux/debugfs.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 21 | #include <linux/hardirq.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 22 | #include <linux/kthread.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 24 | #include <linux/kprobes.h> |
Ingo Molnar | 2d8b820 | 2008-02-23 16:55:50 +0100 | [diff] [blame] | 25 | #include <linux/ftrace.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 26 | #include <linux/sysctl.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 27 | #include <linux/ctype.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 28 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 29 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 30 | #include <asm/ftrace.h> |
| 31 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 32 | #include "trace.h" |
| 33 | |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 34 | #define FTRACE_WARN_ON(cond) \ |
| 35 | do { \ |
| 36 | if (WARN_ON(cond)) \ |
| 37 | ftrace_kill(); \ |
| 38 | } while (0) |
| 39 | |
| 40 | #define FTRACE_WARN_ON_ONCE(cond) \ |
| 41 | do { \ |
| 42 | if (WARN_ON_ONCE(cond)) \ |
| 43 | ftrace_kill(); \ |
| 44 | } while (0) |
| 45 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 46 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 47 | int ftrace_enabled __read_mostly; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 48 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 49 | |
Steven Rostedt | 0ef8cde | 2008-12-03 15:36:58 -0500 | [diff] [blame] | 50 | /* set when tracing only a pid */ |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 51 | struct pid *ftrace_pid_trace; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 52 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 53 | /* Quick disabling of function tracer. */ |
| 54 | int function_trace_stop; |
| 55 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 56 | /* |
| 57 | * ftrace_disabled is set when an anomaly is discovered. |
| 58 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 59 | */ |
| 60 | static int ftrace_disabled __read_mostly; |
| 61 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 62 | static DEFINE_SPINLOCK(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 63 | static DEFINE_MUTEX(ftrace_sysctl_lock); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 64 | static DEFINE_MUTEX(ftrace_start_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 65 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 66 | static struct ftrace_ops ftrace_list_end __read_mostly = |
| 67 | { |
| 68 | .func = ftrace_stub, |
| 69 | }; |
| 70 | |
| 71 | static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end; |
| 72 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 73 | ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 74 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 75 | |
Ingo Molnar | f225293 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | struct ftrace_ops *op = ftrace_list; |
| 79 | |
| 80 | /* in case someone actually ports this to alpha! */ |
| 81 | read_barrier_depends(); |
| 82 | |
| 83 | while (op != &ftrace_list_end) { |
| 84 | /* silly alpha */ |
| 85 | read_barrier_depends(); |
| 86 | op->func(ip, parent_ip); |
| 87 | op = op->next; |
| 88 | }; |
| 89 | } |
| 90 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 91 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip) |
| 92 | { |
Steven Rostedt | 0ef8cde | 2008-12-03 15:36:58 -0500 | [diff] [blame] | 93 | if (!test_tsk_trace_trace(current)) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 94 | return; |
| 95 | |
| 96 | ftrace_pid_function(ip, parent_ip); |
| 97 | } |
| 98 | |
| 99 | static void set_ftrace_pid_function(ftrace_func_t func) |
| 100 | { |
| 101 | /* do not set ftrace_pid_function to itself! */ |
| 102 | if (func != ftrace_pid_func) |
| 103 | ftrace_pid_function = func; |
| 104 | } |
| 105 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 106 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 107 | * clear_ftrace_function - reset the ftrace function |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 108 | * |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 109 | * This NULLs the ftrace function and in essence stops |
| 110 | * tracing. There may be lag |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 111 | */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 112 | void clear_ftrace_function(void) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 113 | { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 114 | ftrace_trace_function = ftrace_stub; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 115 | __ftrace_trace_function = ftrace_stub; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 116 | ftrace_pid_function = ftrace_stub; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 117 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 118 | |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 119 | #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 120 | /* |
| 121 | * For those archs that do not test ftrace_trace_stop in their |
| 122 | * mcount call site, we need to do it from C. |
| 123 | */ |
| 124 | static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip) |
| 125 | { |
| 126 | if (function_trace_stop) |
| 127 | return; |
| 128 | |
| 129 | __ftrace_trace_function(ip, parent_ip); |
| 130 | } |
| 131 | #endif |
| 132 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 133 | static int __register_ftrace_function(struct ftrace_ops *ops) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 134 | { |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 135 | /* should not be called from interrupt context */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 136 | spin_lock(&ftrace_lock); |
| 137 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 138 | ops->next = ftrace_list; |
| 139 | /* |
| 140 | * We are entering ops into the ftrace_list but another |
| 141 | * CPU might be walking that list. We need to make sure |
| 142 | * the ops->next pointer is valid before another CPU sees |
| 143 | * the ops pointer included into the ftrace_list. |
| 144 | */ |
| 145 | smp_wmb(); |
| 146 | ftrace_list = ops; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 147 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 148 | if (ftrace_enabled) { |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 149 | ftrace_func_t func; |
| 150 | |
| 151 | if (ops->next == &ftrace_list_end) |
| 152 | func = ops->func; |
| 153 | else |
| 154 | func = ftrace_list_func; |
| 155 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 156 | if (ftrace_pid_trace) { |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 157 | set_ftrace_pid_function(func); |
| 158 | func = ftrace_pid_func; |
| 159 | } |
| 160 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 161 | /* |
| 162 | * For one func, simply call it directly. |
| 163 | * For more than one func, call the chain. |
| 164 | */ |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 165 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 166 | ftrace_trace_function = func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 167 | #else |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 168 | __ftrace_trace_function = func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 169 | ftrace_trace_function = ftrace_test_stop_func; |
| 170 | #endif |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 171 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 172 | |
| 173 | spin_unlock(&ftrace_lock); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 178 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 179 | { |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 180 | struct ftrace_ops **p; |
| 181 | int ret = 0; |
| 182 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 183 | /* should not be called from interrupt context */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 184 | spin_lock(&ftrace_lock); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 185 | |
| 186 | /* |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 187 | * If we are removing the last function, then simply point |
| 188 | * to the ftrace_stub. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 189 | */ |
| 190 | if (ftrace_list == ops && ops->next == &ftrace_list_end) { |
| 191 | ftrace_trace_function = ftrace_stub; |
| 192 | ftrace_list = &ftrace_list_end; |
| 193 | goto out; |
| 194 | } |
| 195 | |
| 196 | for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next) |
| 197 | if (*p == ops) |
| 198 | break; |
| 199 | |
| 200 | if (*p != ops) { |
| 201 | ret = -1; |
| 202 | goto out; |
| 203 | } |
| 204 | |
| 205 | *p = (*p)->next; |
| 206 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 207 | if (ftrace_enabled) { |
| 208 | /* If we only have one func left, then call that directly */ |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 209 | if (ftrace_list->next == &ftrace_list_end) { |
| 210 | ftrace_func_t func = ftrace_list->func; |
| 211 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 212 | if (ftrace_pid_trace) { |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 213 | set_ftrace_pid_function(func); |
| 214 | func = ftrace_pid_func; |
| 215 | } |
| 216 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 217 | ftrace_trace_function = func; |
| 218 | #else |
| 219 | __ftrace_trace_function = func; |
| 220 | #endif |
| 221 | } |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 222 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 223 | |
| 224 | out: |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 225 | spin_unlock(&ftrace_lock); |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 230 | static void ftrace_update_pid_func(void) |
| 231 | { |
| 232 | ftrace_func_t func; |
| 233 | |
| 234 | /* should not be called from interrupt context */ |
| 235 | spin_lock(&ftrace_lock); |
| 236 | |
| 237 | if (ftrace_trace_function == ftrace_stub) |
| 238 | goto out; |
| 239 | |
| 240 | func = ftrace_trace_function; |
| 241 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 242 | if (ftrace_pid_trace) { |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 243 | set_ftrace_pid_function(func); |
| 244 | func = ftrace_pid_func; |
| 245 | } else { |
Liming Wang | 66eafeb | 2008-12-02 10:33:08 +0800 | [diff] [blame] | 246 | if (func == ftrace_pid_func) |
| 247 | func = ftrace_pid_function; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
| 251 | ftrace_trace_function = func; |
| 252 | #else |
| 253 | __ftrace_trace_function = func; |
| 254 | #endif |
| 255 | |
| 256 | out: |
| 257 | spin_unlock(&ftrace_lock); |
| 258 | } |
| 259 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 260 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 261 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 262 | # error Dynamic ftrace depends on MCOUNT_RECORD |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 263 | #endif |
| 264 | |
Steven Noonan | 71c67d5 | 2008-09-20 01:00:37 -0700 | [diff] [blame] | 265 | /* |
| 266 | * Since MCOUNT_ADDR may point to mcount itself, we do not want |
| 267 | * to get it confused by reading a reference in the code as we |
| 268 | * are parsing on objcopy output of text. Use a variable for |
| 269 | * it instead. |
| 270 | */ |
| 271 | static unsigned long mcount_addr = MCOUNT_ADDR; |
| 272 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 273 | enum { |
| 274 | FTRACE_ENABLE_CALLS = (1 << 0), |
| 275 | FTRACE_DISABLE_CALLS = (1 << 1), |
| 276 | FTRACE_UPDATE_TRACE_FUNC = (1 << 2), |
| 277 | FTRACE_ENABLE_MCOUNT = (1 << 3), |
| 278 | FTRACE_DISABLE_MCOUNT = (1 << 4), |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 279 | FTRACE_START_FUNC_RET = (1 << 5), |
| 280 | FTRACE_STOP_FUNC_RET = (1 << 6), |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 281 | }; |
| 282 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 283 | static int ftrace_filtered; |
| 284 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 285 | static LIST_HEAD(ftrace_new_addrs); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 286 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 287 | static DEFINE_MUTEX(ftrace_regex_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 288 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 289 | struct ftrace_page { |
| 290 | struct ftrace_page *next; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 291 | unsigned long index; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 292 | struct dyn_ftrace records[]; |
David Miller | aa5e5ce | 2008-05-13 22:06:56 -0700 | [diff] [blame] | 293 | }; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 294 | |
| 295 | #define ENTRIES_PER_PAGE \ |
| 296 | ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace)) |
| 297 | |
| 298 | /* estimate from running different kernels */ |
| 299 | #define NR_TO_INIT 10000 |
| 300 | |
| 301 | static struct ftrace_page *ftrace_pages_start; |
| 302 | static struct ftrace_page *ftrace_pages; |
| 303 | |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 304 | static struct dyn_ftrace *ftrace_free_records; |
| 305 | |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 306 | |
| 307 | #ifdef CONFIG_KPROBES |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 308 | |
| 309 | static int frozen_record_count; |
| 310 | |
Abhishek Sagar | ecea656 | 2008-06-21 23:47:53 +0530 | [diff] [blame] | 311 | static inline void freeze_record(struct dyn_ftrace *rec) |
| 312 | { |
| 313 | if (!(rec->flags & FTRACE_FL_FROZEN)) { |
| 314 | rec->flags |= FTRACE_FL_FROZEN; |
| 315 | frozen_record_count++; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static inline void unfreeze_record(struct dyn_ftrace *rec) |
| 320 | { |
| 321 | if (rec->flags & FTRACE_FL_FROZEN) { |
| 322 | rec->flags &= ~FTRACE_FL_FROZEN; |
| 323 | frozen_record_count--; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | static inline int record_frozen(struct dyn_ftrace *rec) |
| 328 | { |
| 329 | return rec->flags & FTRACE_FL_FROZEN; |
| 330 | } |
| 331 | #else |
| 332 | # define freeze_record(rec) ({ 0; }) |
| 333 | # define unfreeze_record(rec) ({ 0; }) |
| 334 | # define record_frozen(rec) ({ 0; }) |
| 335 | #endif /* CONFIG_KPROBES */ |
| 336 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 337 | static void ftrace_free_rec(struct dyn_ftrace *rec) |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 338 | { |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 339 | rec->ip = (unsigned long)ftrace_free_records; |
| 340 | ftrace_free_records = rec; |
| 341 | rec->flags |= FTRACE_FL_FREE; |
| 342 | } |
| 343 | |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 344 | void ftrace_release(void *start, unsigned long size) |
| 345 | { |
| 346 | struct dyn_ftrace *rec; |
| 347 | struct ftrace_page *pg; |
| 348 | unsigned long s = (unsigned long)start; |
| 349 | unsigned long e = s + size; |
| 350 | int i; |
| 351 | |
Steven Rostedt | 00fd61a | 2008-08-15 21:40:04 -0400 | [diff] [blame] | 352 | if (ftrace_disabled || !start) |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 353 | return; |
| 354 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 355 | /* should not be called from interrupt context */ |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 356 | spin_lock(&ftrace_lock); |
| 357 | |
| 358 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 359 | for (i = 0; i < pg->index; i++) { |
| 360 | rec = &pg->records[i]; |
| 361 | |
| 362 | if ((rec->ip >= s) && (rec->ip < e)) |
| 363 | ftrace_free_rec(rec); |
| 364 | } |
| 365 | } |
| 366 | spin_unlock(&ftrace_lock); |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 369 | static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 370 | { |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 371 | struct dyn_ftrace *rec; |
| 372 | |
| 373 | /* First check for freed records */ |
| 374 | if (ftrace_free_records) { |
| 375 | rec = ftrace_free_records; |
| 376 | |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 377 | if (unlikely(!(rec->flags & FTRACE_FL_FREE))) { |
Steven Rostedt | 6912896e | 2008-10-23 09:33:03 -0400 | [diff] [blame] | 378 | FTRACE_WARN_ON_ONCE(1); |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 379 | ftrace_free_records = NULL; |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | ftrace_free_records = (void *)rec->ip; |
| 384 | memset(rec, 0, sizeof(*rec)); |
| 385 | return rec; |
| 386 | } |
| 387 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 388 | if (ftrace_pages->index == ENTRIES_PER_PAGE) { |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 389 | if (!ftrace_pages->next) { |
| 390 | /* allocate another page */ |
| 391 | ftrace_pages->next = |
| 392 | (void *)get_zeroed_page(GFP_KERNEL); |
| 393 | if (!ftrace_pages->next) |
| 394 | return NULL; |
| 395 | } |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 396 | ftrace_pages = ftrace_pages->next; |
| 397 | } |
| 398 | |
| 399 | return &ftrace_pages->records[ftrace_pages->index++]; |
| 400 | } |
| 401 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 402 | static struct dyn_ftrace * |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 403 | ftrace_record_ip(unsigned long ip) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 404 | { |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 405 | struct dyn_ftrace *rec; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 406 | |
Steven Rostedt | f3c7ac4 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 407 | if (ftrace_disabled) |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 408 | return NULL; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 409 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 410 | rec = ftrace_alloc_dyn_node(ip); |
| 411 | if (!rec) |
| 412 | return NULL; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 413 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 414 | rec->ip = ip; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 415 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 416 | list_add(&rec->list, &ftrace_new_addrs); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 417 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 418 | return rec; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Steven Rostedt | 05736a4 | 2008-09-22 14:55:47 -0700 | [diff] [blame] | 421 | static void print_ip_ins(const char *fmt, unsigned char *p) |
| 422 | { |
| 423 | int i; |
| 424 | |
| 425 | printk(KERN_CONT "%s", fmt); |
| 426 | |
| 427 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) |
| 428 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); |
| 429 | } |
| 430 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 431 | static void ftrace_bug(int failed, unsigned long ip) |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 432 | { |
| 433 | switch (failed) { |
| 434 | case -EFAULT: |
| 435 | FTRACE_WARN_ON_ONCE(1); |
| 436 | pr_info("ftrace faulted on modifying "); |
| 437 | print_ip_sym(ip); |
| 438 | break; |
| 439 | case -EINVAL: |
| 440 | FTRACE_WARN_ON_ONCE(1); |
| 441 | pr_info("ftrace failed to modify "); |
| 442 | print_ip_sym(ip); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 443 | print_ip_ins(" actual: ", (unsigned char *)ip); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 444 | printk(KERN_CONT "\n"); |
| 445 | break; |
| 446 | case -EPERM: |
| 447 | FTRACE_WARN_ON_ONCE(1); |
| 448 | pr_info("ftrace faulted on writing "); |
| 449 | print_ip_sym(ip); |
| 450 | break; |
| 451 | default: |
| 452 | FTRACE_WARN_ON_ONCE(1); |
| 453 | pr_info("ftrace faulted on unknown error "); |
| 454 | print_ip_sym(ip); |
| 455 | } |
| 456 | } |
| 457 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 458 | |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 459 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 460 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 461 | { |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 462 | unsigned long ip, fl; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 463 | unsigned long ftrace_addr; |
| 464 | |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 465 | ftrace_addr = (unsigned long)ftrace_caller; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 466 | |
| 467 | ip = rec->ip; |
| 468 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 469 | /* |
| 470 | * If this record is not to be traced and |
| 471 | * it is not enabled then do nothing. |
| 472 | * |
| 473 | * If this record is not to be traced and |
| 474 | * it is enabled then disabled it. |
| 475 | * |
| 476 | */ |
| 477 | if (rec->flags & FTRACE_FL_NOTRACE) { |
| 478 | if (rec->flags & FTRACE_FL_ENABLED) |
| 479 | rec->flags &= ~FTRACE_FL_ENABLED; |
| 480 | else |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 481 | return 0; |
| 482 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 483 | } else if (ftrace_filtered && enable) { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 484 | /* |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 485 | * Filtering is on: |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 486 | */ |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 487 | |
| 488 | fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED); |
| 489 | |
| 490 | /* Record is filtered and enabled, do nothing */ |
| 491 | if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) |
| 492 | return 0; |
| 493 | |
| 494 | /* Record is not filtered and is not enabled do nothing */ |
| 495 | if (!fl) |
| 496 | return 0; |
| 497 | |
| 498 | /* Record is not filtered but enabled, disable it */ |
| 499 | if (fl == FTRACE_FL_ENABLED) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 500 | rec->flags &= ~FTRACE_FL_ENABLED; |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 501 | else |
| 502 | /* Otherwise record is filtered but not enabled, enable it */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 503 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 504 | } else { |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 505 | /* Disable or not filtered */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 506 | |
| 507 | if (enable) { |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 508 | /* if record is enabled, do nothing */ |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 509 | if (rec->flags & FTRACE_FL_ENABLED) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 510 | return 0; |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 511 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 512 | rec->flags |= FTRACE_FL_ENABLED; |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 513 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 514 | } else { |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 515 | |
| 516 | /* if record is not enabled do nothing */ |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 517 | if (!(rec->flags & FTRACE_FL_ENABLED)) |
| 518 | return 0; |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 519 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 520 | rec->flags &= ~FTRACE_FL_ENABLED; |
| 521 | } |
| 522 | } |
| 523 | |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 524 | if (rec->flags & FTRACE_FL_ENABLED) |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 525 | return ftrace_make_call(rec, ftrace_addr); |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 526 | else |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 527 | return ftrace_make_nop(NULL, rec, ftrace_addr); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void ftrace_replace_code(int enable) |
| 531 | { |
| 532 | int i, failed; |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 533 | struct dyn_ftrace *rec; |
| 534 | struct ftrace_page *pg; |
| 535 | |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 536 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 537 | for (i = 0; i < pg->index; i++) { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 538 | rec = &pg->records[i]; |
| 539 | |
Steven Rostedt | 918c115 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 540 | /* |
| 541 | * Skip over free records and records that have |
| 542 | * failed. |
| 543 | */ |
| 544 | if (rec->flags & FTRACE_FL_FREE || |
| 545 | rec->flags & FTRACE_FL_FAILED) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 546 | continue; |
| 547 | |
| 548 | /* ignore updates to this record's mcount site */ |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 549 | if (get_kprobe((void *)rec->ip)) { |
| 550 | freeze_record(rec); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 551 | continue; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 552 | } else { |
| 553 | unfreeze_record(rec); |
| 554 | } |
| 555 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 556 | failed = __ftrace_replace_code(rec, enable); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 557 | if (failed && (rec->flags & FTRACE_FL_CONVERTED)) { |
| 558 | rec->flags |= FTRACE_FL_FAILED; |
| 559 | if ((system_state == SYSTEM_BOOTING) || |
| 560 | !core_kernel_text(rec->ip)) { |
| 561 | ftrace_free_rec(rec); |
Steven Rostedt | b17e8a3 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 562 | } else |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 563 | ftrace_bug(failed, rec->ip); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 564 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 565 | } |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 569 | static int |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 570 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 571 | { |
| 572 | unsigned long ip; |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 573 | int ret; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 574 | |
| 575 | ip = rec->ip; |
| 576 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 577 | ret = ftrace_make_nop(mod, rec, mcount_addr); |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 578 | if (ret) { |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 579 | ftrace_bug(ret, ip); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 580 | rec->flags |= FTRACE_FL_FAILED; |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 581 | return 0; |
Steven Rostedt | 37ad5084 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 582 | } |
Abhishek Sagar | 492a7ea5 | 2008-05-25 00:10:04 +0530 | [diff] [blame] | 583 | return 1; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 586 | static int __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 587 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 588 | int *command = data; |
| 589 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 590 | if (*command & FTRACE_ENABLE_CALLS) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 591 | ftrace_replace_code(1); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 592 | else if (*command & FTRACE_DISABLE_CALLS) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 593 | ftrace_replace_code(0); |
| 594 | |
| 595 | if (*command & FTRACE_UPDATE_TRACE_FUNC) |
| 596 | ftrace_update_ftrace_func(ftrace_trace_function); |
| 597 | |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 598 | if (*command & FTRACE_START_FUNC_RET) |
| 599 | ftrace_enable_ftrace_graph_caller(); |
| 600 | else if (*command & FTRACE_STOP_FUNC_RET) |
| 601 | ftrace_disable_ftrace_graph_caller(); |
| 602 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 603 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 604 | } |
| 605 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 606 | static void ftrace_run_update_code(int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 607 | { |
Rusty Russell | 784e2d7 | 2008-07-28 12:16:31 -0500 | [diff] [blame] | 608 | stop_machine(__ftrace_modify_code, &command, NULL); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 609 | } |
| 610 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 611 | static ftrace_func_t saved_ftrace_func; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 612 | static int ftrace_start_up; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 613 | |
| 614 | static void ftrace_startup_enable(int command) |
| 615 | { |
| 616 | if (saved_ftrace_func != ftrace_trace_function) { |
| 617 | saved_ftrace_func = ftrace_trace_function; |
| 618 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 619 | } |
| 620 | |
| 621 | if (!command || !ftrace_enabled) |
| 622 | return; |
| 623 | |
| 624 | ftrace_run_update_code(command); |
| 625 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 626 | |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 627 | static void ftrace_startup(int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 628 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 629 | if (unlikely(ftrace_disabled)) |
| 630 | return; |
| 631 | |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 632 | mutex_lock(&ftrace_start_lock); |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 633 | ftrace_start_up++; |
Steven Rostedt | 982c350 | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 634 | command |= FTRACE_ENABLE_CALLS; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 635 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 636 | ftrace_startup_enable(command); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 637 | |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 638 | mutex_unlock(&ftrace_start_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 641 | static void ftrace_shutdown(int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 642 | { |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 643 | if (unlikely(ftrace_disabled)) |
| 644 | return; |
| 645 | |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 646 | mutex_lock(&ftrace_start_lock); |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 647 | ftrace_start_up--; |
| 648 | if (!ftrace_start_up) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 649 | command |= FTRACE_DISABLE_CALLS; |
| 650 | |
| 651 | if (saved_ftrace_func != ftrace_trace_function) { |
| 652 | saved_ftrace_func = ftrace_trace_function; |
| 653 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 654 | } |
| 655 | |
| 656 | if (!command || !ftrace_enabled) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 657 | goto out; |
| 658 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 659 | ftrace_run_update_code(command); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 660 | out: |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 661 | mutex_unlock(&ftrace_start_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 662 | } |
| 663 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 664 | static void ftrace_startup_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 665 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 666 | int command = FTRACE_ENABLE_MCOUNT; |
| 667 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 668 | if (unlikely(ftrace_disabled)) |
| 669 | return; |
| 670 | |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 671 | mutex_lock(&ftrace_start_lock); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 672 | /* Force update next time */ |
| 673 | saved_ftrace_func = NULL; |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 674 | /* ftrace_start_up is true if we want ftrace running */ |
| 675 | if (ftrace_start_up) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 676 | command |= FTRACE_ENABLE_CALLS; |
| 677 | |
| 678 | ftrace_run_update_code(command); |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 679 | mutex_unlock(&ftrace_start_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 682 | static void ftrace_shutdown_sysctl(void) |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 683 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 684 | int command = FTRACE_DISABLE_MCOUNT; |
| 685 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 686 | if (unlikely(ftrace_disabled)) |
| 687 | return; |
| 688 | |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 689 | mutex_lock(&ftrace_start_lock); |
Steven Rostedt | 60a7ecf | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 690 | /* ftrace_start_up is true if ftrace is running */ |
| 691 | if (ftrace_start_up) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 692 | command |= FTRACE_DISABLE_CALLS; |
| 693 | |
| 694 | ftrace_run_update_code(command); |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 695 | mutex_unlock(&ftrace_start_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 698 | static cycle_t ftrace_update_time; |
| 699 | static unsigned long ftrace_update_cnt; |
| 700 | unsigned long ftrace_update_tot_cnt; |
| 701 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 702 | static int ftrace_update_code(struct module *mod) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 703 | { |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 704 | struct dyn_ftrace *p, *t; |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 705 | cycle_t start, stop; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 706 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 707 | start = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 708 | ftrace_update_cnt = 0; |
| 709 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 710 | list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) { |
Abhishek Sagar | f22f9a8 | 2008-06-21 23:50:29 +0530 | [diff] [blame] | 711 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 712 | /* If something went wrong, bail without enabling anything */ |
| 713 | if (unlikely(ftrace_disabled)) |
| 714 | return -1; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 715 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 716 | list_del_init(&p->list); |
Abhishek Sagar | 0eb9670 | 2008-06-01 21:47:30 +0530 | [diff] [blame] | 717 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 718 | /* convert record (i.e, patch mcount-call with NOP) */ |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 719 | if (ftrace_code_disable(mod, p)) { |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 720 | p->flags |= FTRACE_FL_CONVERTED; |
| 721 | ftrace_update_cnt++; |
| 722 | } else |
| 723 | ftrace_free_rec(p); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Ingo Molnar | 750ed1a | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 726 | stop = ftrace_now(raw_smp_processor_id()); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 727 | ftrace_update_time = stop - start; |
| 728 | ftrace_update_tot_cnt += ftrace_update_cnt; |
| 729 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 732 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 733 | static int __init ftrace_dyn_table_alloc(unsigned long num_to_init) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 734 | { |
| 735 | struct ftrace_page *pg; |
| 736 | int cnt; |
| 737 | int i; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 738 | |
| 739 | /* allocate a few pages */ |
| 740 | ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL); |
| 741 | if (!ftrace_pages_start) |
| 742 | return -1; |
| 743 | |
| 744 | /* |
| 745 | * Allocate a few more pages. |
| 746 | * |
| 747 | * TODO: have some parser search vmlinux before |
| 748 | * final linking to find all calls to ftrace. |
| 749 | * Then we can: |
| 750 | * a) know how many pages to allocate. |
| 751 | * and/or |
| 752 | * b) set up the table then. |
| 753 | * |
| 754 | * The dynamic code is still necessary for |
| 755 | * modules. |
| 756 | */ |
| 757 | |
| 758 | pg = ftrace_pages = ftrace_pages_start; |
| 759 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 760 | cnt = num_to_init / ENTRIES_PER_PAGE; |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 761 | pr_info("ftrace: allocating %ld entries in %d pages\n", |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 762 | num_to_init, cnt + 1); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 763 | |
| 764 | for (i = 0; i < cnt; i++) { |
| 765 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
| 766 | |
| 767 | /* If we fail, we'll try later anyway */ |
| 768 | if (!pg->next) |
| 769 | break; |
| 770 | |
| 771 | pg = pg->next; |
| 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 777 | enum { |
| 778 | FTRACE_ITER_FILTER = (1 << 0), |
| 779 | FTRACE_ITER_CONT = (1 << 1), |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 780 | FTRACE_ITER_NOTRACE = (1 << 2), |
Abhishek Sagar | eb9a7bf | 2008-06-01 21:47:54 +0530 | [diff] [blame] | 781 | FTRACE_ITER_FAILURES = (1 << 3), |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 785 | |
| 786 | struct ftrace_iterator { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 787 | struct ftrace_page *pg; |
| 788 | unsigned idx; |
| 789 | unsigned flags; |
| 790 | unsigned char buffer[FTRACE_BUFF_MAX+1]; |
| 791 | unsigned buffer_idx; |
| 792 | unsigned filtered; |
| 793 | }; |
| 794 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 795 | static void * |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 796 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 797 | { |
| 798 | struct ftrace_iterator *iter = m->private; |
| 799 | struct dyn_ftrace *rec = NULL; |
| 800 | |
| 801 | (*pos)++; |
| 802 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 803 | /* should not be called from interrupt context */ |
| 804 | spin_lock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 805 | retry: |
| 806 | if (iter->idx >= iter->pg->index) { |
| 807 | if (iter->pg->next) { |
| 808 | iter->pg = iter->pg->next; |
| 809 | iter->idx = 0; |
| 810 | goto retry; |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 811 | } else { |
| 812 | iter->idx = -1; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 813 | } |
| 814 | } else { |
| 815 | rec = &iter->pg->records[iter->idx++]; |
Steven Rostedt | a9fdda3 | 2008-08-14 22:47:17 -0400 | [diff] [blame] | 816 | if ((rec->flags & FTRACE_FL_FREE) || |
| 817 | |
| 818 | (!(iter->flags & FTRACE_ITER_FAILURES) && |
Abhishek Sagar | eb9a7bf | 2008-06-01 21:47:54 +0530 | [diff] [blame] | 819 | (rec->flags & FTRACE_FL_FAILED)) || |
| 820 | |
| 821 | ((iter->flags & FTRACE_ITER_FAILURES) && |
Steven Rostedt | a9fdda3 | 2008-08-14 22:47:17 -0400 | [diff] [blame] | 822 | !(rec->flags & FTRACE_FL_FAILED)) || |
Abhishek Sagar | eb9a7bf | 2008-06-01 21:47:54 +0530 | [diff] [blame] | 823 | |
Steven Rostedt | 0183fb1c | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 824 | ((iter->flags & FTRACE_ITER_FILTER) && |
| 825 | !(rec->flags & FTRACE_FL_FILTER)) || |
| 826 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 827 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
| 828 | !(rec->flags & FTRACE_FL_NOTRACE))) { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 829 | rec = NULL; |
| 830 | goto retry; |
| 831 | } |
| 832 | } |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 833 | spin_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 834 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 835 | return rec; |
| 836 | } |
| 837 | |
| 838 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 839 | { |
| 840 | struct ftrace_iterator *iter = m->private; |
| 841 | void *p = NULL; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 842 | |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 843 | if (*pos > 0) { |
| 844 | if (iter->idx < 0) |
| 845 | return p; |
| 846 | (*pos)--; |
| 847 | iter->idx--; |
| 848 | } |
walimis | 5821e1b | 2008-11-15 15:19:06 +0800 | [diff] [blame] | 849 | |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 850 | p = t_next(m, p, pos); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 851 | |
| 852 | return p; |
| 853 | } |
| 854 | |
| 855 | static void t_stop(struct seq_file *m, void *p) |
| 856 | { |
| 857 | } |
| 858 | |
| 859 | static int t_show(struct seq_file *m, void *v) |
| 860 | { |
| 861 | struct dyn_ftrace *rec = v; |
| 862 | char str[KSYM_SYMBOL_LEN]; |
| 863 | |
| 864 | if (!rec) |
| 865 | return 0; |
| 866 | |
| 867 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
| 868 | |
Liming Wang | 50cdaf0 | 2008-11-28 12:13:21 +0800 | [diff] [blame] | 869 | seq_printf(m, "%s\n", str); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static struct seq_operations show_ftrace_seq_ops = { |
| 875 | .start = t_start, |
| 876 | .next = t_next, |
| 877 | .stop = t_stop, |
| 878 | .show = t_show, |
| 879 | }; |
| 880 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 881 | static int |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 882 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 883 | { |
| 884 | struct ftrace_iterator *iter; |
| 885 | int ret; |
| 886 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 887 | if (unlikely(ftrace_disabled)) |
| 888 | return -ENODEV; |
| 889 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 890 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 891 | if (!iter) |
| 892 | return -ENOMEM; |
| 893 | |
| 894 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 895 | |
| 896 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 897 | if (!ret) { |
| 898 | struct seq_file *m = file->private_data; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 899 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 900 | m->private = iter; |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 901 | } else { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 902 | kfree(iter); |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 903 | } |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | int ftrace_avail_release(struct inode *inode, struct file *file) |
| 909 | { |
| 910 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 911 | struct ftrace_iterator *iter = m->private; |
| 912 | |
| 913 | seq_release(inode, file); |
| 914 | kfree(iter); |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 915 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 916 | return 0; |
| 917 | } |
| 918 | |
Abhishek Sagar | eb9a7bf | 2008-06-01 21:47:54 +0530 | [diff] [blame] | 919 | static int |
| 920 | ftrace_failures_open(struct inode *inode, struct file *file) |
| 921 | { |
| 922 | int ret; |
| 923 | struct seq_file *m; |
| 924 | struct ftrace_iterator *iter; |
| 925 | |
| 926 | ret = ftrace_avail_open(inode, file); |
| 927 | if (!ret) { |
| 928 | m = (struct seq_file *)file->private_data; |
| 929 | iter = (struct ftrace_iterator *)m->private; |
| 930 | iter->flags = FTRACE_ITER_FAILURES; |
| 931 | } |
| 932 | |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 937 | static void ftrace_filter_reset(int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 938 | { |
| 939 | struct ftrace_page *pg; |
| 940 | struct dyn_ftrace *rec; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 941 | unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 942 | unsigned i; |
| 943 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 944 | /* should not be called from interrupt context */ |
| 945 | spin_lock(&ftrace_lock); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 946 | if (enable) |
| 947 | ftrace_filtered = 0; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 948 | pg = ftrace_pages_start; |
| 949 | while (pg) { |
| 950 | for (i = 0; i < pg->index; i++) { |
| 951 | rec = &pg->records[i]; |
| 952 | if (rec->flags & FTRACE_FL_FAILED) |
| 953 | continue; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 954 | rec->flags &= ~type; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 955 | } |
| 956 | pg = pg->next; |
| 957 | } |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 958 | spin_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 961 | static int |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 962 | ftrace_regex_open(struct inode *inode, struct file *file, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 963 | { |
| 964 | struct ftrace_iterator *iter; |
| 965 | int ret = 0; |
| 966 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 967 | if (unlikely(ftrace_disabled)) |
| 968 | return -ENODEV; |
| 969 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 970 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 971 | if (!iter) |
| 972 | return -ENOMEM; |
| 973 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 974 | mutex_lock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 975 | if ((file->f_mode & FMODE_WRITE) && |
| 976 | !(file->f_flags & O_APPEND)) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 977 | ftrace_filter_reset(enable); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 978 | |
| 979 | if (file->f_mode & FMODE_READ) { |
| 980 | iter->pg = ftrace_pages_start; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 981 | iter->flags = enable ? FTRACE_ITER_FILTER : |
| 982 | FTRACE_ITER_NOTRACE; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 983 | |
| 984 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 985 | if (!ret) { |
| 986 | struct seq_file *m = file->private_data; |
| 987 | m->private = iter; |
| 988 | } else |
| 989 | kfree(iter); |
| 990 | } else |
| 991 | file->private_data = iter; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 992 | mutex_unlock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 993 | |
| 994 | return ret; |
| 995 | } |
| 996 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 997 | static int |
| 998 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 999 | { |
| 1000 | return ftrace_regex_open(inode, file, 1); |
| 1001 | } |
| 1002 | |
| 1003 | static int |
| 1004 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 1005 | { |
| 1006 | return ftrace_regex_open(inode, file, 0); |
| 1007 | } |
| 1008 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1009 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1010 | ftrace_regex_read(struct file *file, char __user *ubuf, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1011 | size_t cnt, loff_t *ppos) |
| 1012 | { |
| 1013 | if (file->f_mode & FMODE_READ) |
| 1014 | return seq_read(file, ubuf, cnt, ppos); |
| 1015 | else |
| 1016 | return -EPERM; |
| 1017 | } |
| 1018 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1019 | static loff_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1020 | ftrace_regex_lseek(struct file *file, loff_t offset, int origin) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1021 | { |
| 1022 | loff_t ret; |
| 1023 | |
| 1024 | if (file->f_mode & FMODE_READ) |
| 1025 | ret = seq_lseek(file, offset, origin); |
| 1026 | else |
| 1027 | file->f_pos = ret = 1; |
| 1028 | |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
| 1032 | enum { |
| 1033 | MATCH_FULL, |
| 1034 | MATCH_FRONT_ONLY, |
| 1035 | MATCH_MIDDLE_ONLY, |
| 1036 | MATCH_END_ONLY, |
| 1037 | }; |
| 1038 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1039 | static void |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1040 | ftrace_match(unsigned char *buff, int len, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1041 | { |
| 1042 | char str[KSYM_SYMBOL_LEN]; |
| 1043 | char *search = NULL; |
| 1044 | struct ftrace_page *pg; |
| 1045 | struct dyn_ftrace *rec; |
| 1046 | int type = MATCH_FULL; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1047 | unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1048 | unsigned i, match = 0, search_len = 0; |
| 1049 | |
| 1050 | for (i = 0; i < len; i++) { |
| 1051 | if (buff[i] == '*') { |
| 1052 | if (!i) { |
| 1053 | search = buff + i + 1; |
| 1054 | type = MATCH_END_ONLY; |
| 1055 | search_len = len - (i + 1); |
| 1056 | } else { |
| 1057 | if (type == MATCH_END_ONLY) { |
| 1058 | type = MATCH_MIDDLE_ONLY; |
| 1059 | } else { |
| 1060 | match = i; |
| 1061 | type = MATCH_FRONT_ONLY; |
| 1062 | } |
| 1063 | buff[i] = 0; |
| 1064 | break; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1069 | /* should not be called from interrupt context */ |
| 1070 | spin_lock(&ftrace_lock); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1071 | if (enable) |
| 1072 | ftrace_filtered = 1; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1073 | pg = ftrace_pages_start; |
| 1074 | while (pg) { |
| 1075 | for (i = 0; i < pg->index; i++) { |
| 1076 | int matched = 0; |
| 1077 | char *ptr; |
| 1078 | |
| 1079 | rec = &pg->records[i]; |
| 1080 | if (rec->flags & FTRACE_FL_FAILED) |
| 1081 | continue; |
| 1082 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
| 1083 | switch (type) { |
| 1084 | case MATCH_FULL: |
| 1085 | if (strcmp(str, buff) == 0) |
| 1086 | matched = 1; |
| 1087 | break; |
| 1088 | case MATCH_FRONT_ONLY: |
| 1089 | if (memcmp(str, buff, match) == 0) |
| 1090 | matched = 1; |
| 1091 | break; |
| 1092 | case MATCH_MIDDLE_ONLY: |
| 1093 | if (strstr(str, search)) |
| 1094 | matched = 1; |
| 1095 | break; |
| 1096 | case MATCH_END_ONLY: |
| 1097 | ptr = strstr(str, search); |
| 1098 | if (ptr && (ptr[search_len] == 0)) |
| 1099 | matched = 1; |
| 1100 | break; |
| 1101 | } |
| 1102 | if (matched) |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1103 | rec->flags |= flag; |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1104 | } |
| 1105 | pg = pg->next; |
| 1106 | } |
Steven Rostedt | 99ecdc4 | 2008-08-15 21:40:05 -0400 | [diff] [blame] | 1107 | spin_unlock(&ftrace_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1110 | static ssize_t |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1111 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 1112 | size_t cnt, loff_t *ppos, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1113 | { |
| 1114 | struct ftrace_iterator *iter; |
| 1115 | char ch; |
| 1116 | size_t read = 0; |
| 1117 | ssize_t ret; |
| 1118 | |
| 1119 | if (!cnt || cnt < 0) |
| 1120 | return 0; |
| 1121 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1122 | mutex_lock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1123 | |
| 1124 | if (file->f_mode & FMODE_READ) { |
| 1125 | struct seq_file *m = file->private_data; |
| 1126 | iter = m->private; |
| 1127 | } else |
| 1128 | iter = file->private_data; |
| 1129 | |
| 1130 | if (!*ppos) { |
| 1131 | iter->flags &= ~FTRACE_ITER_CONT; |
| 1132 | iter->buffer_idx = 0; |
| 1133 | } |
| 1134 | |
| 1135 | ret = get_user(ch, ubuf++); |
| 1136 | if (ret) |
| 1137 | goto out; |
| 1138 | read++; |
| 1139 | cnt--; |
| 1140 | |
| 1141 | if (!(iter->flags & ~FTRACE_ITER_CONT)) { |
| 1142 | /* skip white space */ |
| 1143 | while (cnt && isspace(ch)) { |
| 1144 | ret = get_user(ch, ubuf++); |
| 1145 | if (ret) |
| 1146 | goto out; |
| 1147 | read++; |
| 1148 | cnt--; |
| 1149 | } |
| 1150 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1151 | if (isspace(ch)) { |
| 1152 | file->f_pos += read; |
| 1153 | ret = read; |
| 1154 | goto out; |
| 1155 | } |
| 1156 | |
| 1157 | iter->buffer_idx = 0; |
| 1158 | } |
| 1159 | |
| 1160 | while (cnt && !isspace(ch)) { |
| 1161 | if (iter->buffer_idx < FTRACE_BUFF_MAX) |
| 1162 | iter->buffer[iter->buffer_idx++] = ch; |
| 1163 | else { |
| 1164 | ret = -EINVAL; |
| 1165 | goto out; |
| 1166 | } |
| 1167 | ret = get_user(ch, ubuf++); |
| 1168 | if (ret) |
| 1169 | goto out; |
| 1170 | read++; |
| 1171 | cnt--; |
| 1172 | } |
| 1173 | |
| 1174 | if (isspace(ch)) { |
| 1175 | iter->filtered++; |
| 1176 | iter->buffer[iter->buffer_idx] = 0; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1177 | ftrace_match(iter->buffer, iter->buffer_idx, enable); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1178 | iter->buffer_idx = 0; |
| 1179 | } else |
| 1180 | iter->flags |= FTRACE_ITER_CONT; |
| 1181 | |
| 1182 | |
| 1183 | file->f_pos += read; |
| 1184 | |
| 1185 | ret = read; |
| 1186 | out: |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1187 | mutex_unlock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1188 | |
| 1189 | return ret; |
| 1190 | } |
| 1191 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1192 | static ssize_t |
| 1193 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 1194 | size_t cnt, loff_t *ppos) |
| 1195 | { |
| 1196 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); |
| 1197 | } |
| 1198 | |
| 1199 | static ssize_t |
| 1200 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 1201 | size_t cnt, loff_t *ppos) |
| 1202 | { |
| 1203 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); |
| 1204 | } |
| 1205 | |
| 1206 | static void |
| 1207 | ftrace_set_regex(unsigned char *buf, int len, int reset, int enable) |
| 1208 | { |
| 1209 | if (unlikely(ftrace_disabled)) |
| 1210 | return; |
| 1211 | |
| 1212 | mutex_lock(&ftrace_regex_lock); |
| 1213 | if (reset) |
| 1214 | ftrace_filter_reset(enable); |
| 1215 | if (buf) |
| 1216 | ftrace_match(buf, len, enable); |
| 1217 | mutex_unlock(&ftrace_regex_lock); |
| 1218 | } |
| 1219 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 1220 | /** |
| 1221 | * ftrace_set_filter - set a function to filter on in ftrace |
| 1222 | * @buf - the string that holds the function filter text. |
| 1223 | * @len - the length of the string. |
| 1224 | * @reset - non zero to reset all filters before applying this filter. |
| 1225 | * |
| 1226 | * Filters denote which functions should be enabled when tracing is enabled. |
| 1227 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 1228 | */ |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1229 | void ftrace_set_filter(unsigned char *buf, int len, int reset) |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 1230 | { |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1231 | ftrace_set_regex(buf, len, reset, 1); |
| 1232 | } |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1233 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1234 | /** |
| 1235 | * ftrace_set_notrace - set a function to not trace in ftrace |
| 1236 | * @buf - the string that holds the function notrace text. |
| 1237 | * @len - the length of the string. |
| 1238 | * @reset - non zero to reset all filters before applying this filter. |
| 1239 | * |
| 1240 | * Notrace Filters denote which functions should not be enabled when tracing |
| 1241 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 1242 | * for tracing. |
| 1243 | */ |
| 1244 | void ftrace_set_notrace(unsigned char *buf, int len, int reset) |
| 1245 | { |
| 1246 | ftrace_set_regex(buf, len, reset, 0); |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1249 | static int |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1250 | ftrace_regex_release(struct inode *inode, struct file *file, int enable) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1251 | { |
| 1252 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 1253 | struct ftrace_iterator *iter; |
| 1254 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1255 | mutex_lock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1256 | if (file->f_mode & FMODE_READ) { |
| 1257 | iter = m->private; |
| 1258 | |
| 1259 | seq_release(inode, file); |
| 1260 | } else |
| 1261 | iter = file->private_data; |
| 1262 | |
| 1263 | if (iter->buffer_idx) { |
| 1264 | iter->filtered++; |
| 1265 | iter->buffer[iter->buffer_idx] = 0; |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1266 | ftrace_match(iter->buffer, iter->buffer_idx, enable); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | mutex_lock(&ftrace_sysctl_lock); |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1270 | mutex_lock(&ftrace_start_lock); |
Steven Rostedt | ee02a2e | 2008-11-15 16:31:41 -0500 | [diff] [blame] | 1271 | if (ftrace_start_up && ftrace_enabled) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1272 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); |
Steven Rostedt | cb7be3b | 2008-10-23 09:33:05 -0400 | [diff] [blame] | 1273 | mutex_unlock(&ftrace_start_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1274 | mutex_unlock(&ftrace_sysctl_lock); |
| 1275 | |
| 1276 | kfree(iter); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1277 | mutex_unlock(&ftrace_regex_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1278 | return 0; |
| 1279 | } |
| 1280 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1281 | static int |
| 1282 | ftrace_filter_release(struct inode *inode, struct file *file) |
| 1283 | { |
| 1284 | return ftrace_regex_release(inode, file, 1); |
| 1285 | } |
| 1286 | |
| 1287 | static int |
| 1288 | ftrace_notrace_release(struct inode *inode, struct file *file) |
| 1289 | { |
| 1290 | return ftrace_regex_release(inode, file, 0); |
| 1291 | } |
| 1292 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1293 | static struct file_operations ftrace_avail_fops = { |
| 1294 | .open = ftrace_avail_open, |
| 1295 | .read = seq_read, |
| 1296 | .llseek = seq_lseek, |
| 1297 | .release = ftrace_avail_release, |
| 1298 | }; |
| 1299 | |
Abhishek Sagar | eb9a7bf | 2008-06-01 21:47:54 +0530 | [diff] [blame] | 1300 | static struct file_operations ftrace_failures_fops = { |
| 1301 | .open = ftrace_failures_open, |
| 1302 | .read = seq_read, |
| 1303 | .llseek = seq_lseek, |
| 1304 | .release = ftrace_avail_release, |
| 1305 | }; |
| 1306 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1307 | static struct file_operations ftrace_filter_fops = { |
| 1308 | .open = ftrace_filter_open, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1309 | .read = ftrace_regex_read, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1310 | .write = ftrace_filter_write, |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1311 | .llseek = ftrace_regex_lseek, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1312 | .release = ftrace_filter_release, |
| 1313 | }; |
| 1314 | |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1315 | static struct file_operations ftrace_notrace_fops = { |
| 1316 | .open = ftrace_notrace_open, |
| 1317 | .read = ftrace_regex_read, |
| 1318 | .write = ftrace_notrace_write, |
| 1319 | .llseek = ftrace_regex_lseek, |
| 1320 | .release = ftrace_notrace_release, |
| 1321 | }; |
| 1322 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 1323 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 1324 | |
| 1325 | static DEFINE_MUTEX(graph_lock); |
| 1326 | |
| 1327 | int ftrace_graph_count; |
| 1328 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
| 1329 | |
| 1330 | static void * |
| 1331 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 1332 | { |
| 1333 | unsigned long *array = m->private; |
| 1334 | int index = *pos; |
| 1335 | |
| 1336 | (*pos)++; |
| 1337 | |
| 1338 | if (index >= ftrace_graph_count) |
| 1339 | return NULL; |
| 1340 | |
| 1341 | return &array[index]; |
| 1342 | } |
| 1343 | |
| 1344 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 1345 | { |
| 1346 | void *p = NULL; |
| 1347 | |
| 1348 | mutex_lock(&graph_lock); |
| 1349 | |
| 1350 | p = g_next(m, p, pos); |
| 1351 | |
| 1352 | return p; |
| 1353 | } |
| 1354 | |
| 1355 | static void g_stop(struct seq_file *m, void *p) |
| 1356 | { |
| 1357 | mutex_unlock(&graph_lock); |
| 1358 | } |
| 1359 | |
| 1360 | static int g_show(struct seq_file *m, void *v) |
| 1361 | { |
| 1362 | unsigned long *ptr = v; |
| 1363 | char str[KSYM_SYMBOL_LEN]; |
| 1364 | |
| 1365 | if (!ptr) |
| 1366 | return 0; |
| 1367 | |
| 1368 | kallsyms_lookup(*ptr, NULL, NULL, NULL, str); |
| 1369 | |
| 1370 | seq_printf(m, "%s\n", str); |
| 1371 | |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | static struct seq_operations ftrace_graph_seq_ops = { |
| 1376 | .start = g_start, |
| 1377 | .next = g_next, |
| 1378 | .stop = g_stop, |
| 1379 | .show = g_show, |
| 1380 | }; |
| 1381 | |
| 1382 | static int |
| 1383 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 1384 | { |
| 1385 | int ret = 0; |
| 1386 | |
| 1387 | if (unlikely(ftrace_disabled)) |
| 1388 | return -ENODEV; |
| 1389 | |
| 1390 | mutex_lock(&graph_lock); |
| 1391 | if ((file->f_mode & FMODE_WRITE) && |
| 1392 | !(file->f_flags & O_APPEND)) { |
| 1393 | ftrace_graph_count = 0; |
| 1394 | memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs)); |
| 1395 | } |
| 1396 | |
| 1397 | if (file->f_mode & FMODE_READ) { |
| 1398 | ret = seq_open(file, &ftrace_graph_seq_ops); |
| 1399 | if (!ret) { |
| 1400 | struct seq_file *m = file->private_data; |
| 1401 | m->private = ftrace_graph_funcs; |
| 1402 | } |
| 1403 | } else |
| 1404 | file->private_data = ftrace_graph_funcs; |
| 1405 | mutex_unlock(&graph_lock); |
| 1406 | |
| 1407 | return ret; |
| 1408 | } |
| 1409 | |
| 1410 | static ssize_t |
| 1411 | ftrace_graph_read(struct file *file, char __user *ubuf, |
| 1412 | size_t cnt, loff_t *ppos) |
| 1413 | { |
| 1414 | if (file->f_mode & FMODE_READ) |
| 1415 | return seq_read(file, ubuf, cnt, ppos); |
| 1416 | else |
| 1417 | return -EPERM; |
| 1418 | } |
| 1419 | |
| 1420 | static int |
| 1421 | ftrace_set_func(unsigned long *array, int idx, char *buffer) |
| 1422 | { |
| 1423 | char str[KSYM_SYMBOL_LEN]; |
| 1424 | struct dyn_ftrace *rec; |
| 1425 | struct ftrace_page *pg; |
| 1426 | int found = 0; |
| 1427 | int i; |
| 1428 | |
| 1429 | if (ftrace_disabled) |
| 1430 | return -ENODEV; |
| 1431 | |
| 1432 | /* should not be called from interrupt context */ |
| 1433 | spin_lock(&ftrace_lock); |
| 1434 | |
| 1435 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1436 | for (i = 0; i < pg->index; i++) { |
| 1437 | rec = &pg->records[i]; |
| 1438 | |
| 1439 | if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE)) |
| 1440 | continue; |
| 1441 | |
| 1442 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
| 1443 | if (strcmp(str, buffer) == 0) { |
| 1444 | found = 1; |
| 1445 | array[idx] = rec->ip; |
| 1446 | break; |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | spin_unlock(&ftrace_lock); |
| 1451 | |
| 1452 | return found ? 0 : -EINVAL; |
| 1453 | } |
| 1454 | |
| 1455 | static ssize_t |
| 1456 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 1457 | size_t cnt, loff_t *ppos) |
| 1458 | { |
| 1459 | unsigned char buffer[FTRACE_BUFF_MAX+1]; |
| 1460 | unsigned long *array; |
| 1461 | size_t read = 0; |
| 1462 | ssize_t ret; |
| 1463 | int index = 0; |
| 1464 | char ch; |
| 1465 | |
| 1466 | if (!cnt || cnt < 0) |
| 1467 | return 0; |
| 1468 | |
| 1469 | mutex_lock(&graph_lock); |
| 1470 | |
| 1471 | if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) { |
| 1472 | ret = -EBUSY; |
| 1473 | goto out; |
| 1474 | } |
| 1475 | |
| 1476 | if (file->f_mode & FMODE_READ) { |
| 1477 | struct seq_file *m = file->private_data; |
| 1478 | array = m->private; |
| 1479 | } else |
| 1480 | array = file->private_data; |
| 1481 | |
| 1482 | ret = get_user(ch, ubuf++); |
| 1483 | if (ret) |
| 1484 | goto out; |
| 1485 | read++; |
| 1486 | cnt--; |
| 1487 | |
| 1488 | /* skip white space */ |
| 1489 | while (cnt && isspace(ch)) { |
| 1490 | ret = get_user(ch, ubuf++); |
| 1491 | if (ret) |
| 1492 | goto out; |
| 1493 | read++; |
| 1494 | cnt--; |
| 1495 | } |
| 1496 | |
| 1497 | if (isspace(ch)) { |
| 1498 | *ppos += read; |
| 1499 | ret = read; |
| 1500 | goto out; |
| 1501 | } |
| 1502 | |
| 1503 | while (cnt && !isspace(ch)) { |
| 1504 | if (index < FTRACE_BUFF_MAX) |
| 1505 | buffer[index++] = ch; |
| 1506 | else { |
| 1507 | ret = -EINVAL; |
| 1508 | goto out; |
| 1509 | } |
| 1510 | ret = get_user(ch, ubuf++); |
| 1511 | if (ret) |
| 1512 | goto out; |
| 1513 | read++; |
| 1514 | cnt--; |
| 1515 | } |
| 1516 | buffer[index] = 0; |
| 1517 | |
| 1518 | /* we allow only one at a time */ |
| 1519 | ret = ftrace_set_func(array, ftrace_graph_count, buffer); |
| 1520 | if (ret) |
| 1521 | goto out; |
| 1522 | |
| 1523 | ftrace_graph_count++; |
| 1524 | |
| 1525 | file->f_pos += read; |
| 1526 | |
| 1527 | ret = read; |
| 1528 | out: |
| 1529 | mutex_unlock(&graph_lock); |
| 1530 | |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | static const struct file_operations ftrace_graph_fops = { |
| 1535 | .open = ftrace_graph_open, |
| 1536 | .read = ftrace_graph_read, |
| 1537 | .write = ftrace_graph_write, |
| 1538 | }; |
| 1539 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 1540 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1541 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1542 | { |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1543 | struct dentry *entry; |
| 1544 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1545 | entry = debugfs_create_file("available_filter_functions", 0444, |
| 1546 | d_tracer, NULL, &ftrace_avail_fops); |
| 1547 | if (!entry) |
| 1548 | pr_warning("Could not create debugfs " |
| 1549 | "'available_filter_functions' entry\n"); |
| 1550 | |
Abhishek Sagar | eb9a7bf | 2008-06-01 21:47:54 +0530 | [diff] [blame] | 1551 | entry = debugfs_create_file("failures", 0444, |
| 1552 | d_tracer, NULL, &ftrace_failures_fops); |
| 1553 | if (!entry) |
| 1554 | pr_warning("Could not create debugfs 'failures' entry\n"); |
| 1555 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1556 | entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer, |
| 1557 | NULL, &ftrace_filter_fops); |
| 1558 | if (!entry) |
| 1559 | pr_warning("Could not create debugfs " |
| 1560 | "'set_ftrace_filter' entry\n"); |
Steven Rostedt | 41c52c0 | 2008-05-22 11:46:33 -0400 | [diff] [blame] | 1561 | |
| 1562 | entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer, |
| 1563 | NULL, &ftrace_notrace_fops); |
| 1564 | if (!entry) |
| 1565 | pr_warning("Could not create debugfs " |
| 1566 | "'set_ftrace_notrace' entry\n"); |
Steven Rostedt | ad90c0e | 2008-05-27 20:48:37 -0400 | [diff] [blame] | 1567 | |
Steven Rostedt | ea4e2bc | 2008-12-03 15:36:57 -0500 | [diff] [blame] | 1568 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 1569 | entry = debugfs_create_file("set_graph_function", 0444, d_tracer, |
| 1570 | NULL, |
| 1571 | &ftrace_graph_fops); |
| 1572 | if (!entry) |
| 1573 | pr_warning("Could not create debugfs " |
| 1574 | "'set_graph_function' entry\n"); |
| 1575 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 1576 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1580 | static int ftrace_convert_nops(struct module *mod, |
| 1581 | unsigned long *start, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1582 | unsigned long *end) |
| 1583 | { |
| 1584 | unsigned long *p; |
| 1585 | unsigned long addr; |
| 1586 | unsigned long flags; |
| 1587 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1588 | mutex_lock(&ftrace_start_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1589 | p = start; |
| 1590 | while (p < end) { |
| 1591 | addr = ftrace_call_adjust(*p++); |
Steven Rostedt | 20e5227 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1592 | /* |
| 1593 | * Some architecture linkers will pad between |
| 1594 | * the different mcount_loc sections of different |
| 1595 | * object files to satisfy alignments. |
| 1596 | * Skip any NULL pointers. |
| 1597 | */ |
| 1598 | if (!addr) |
| 1599 | continue; |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1600 | ftrace_record_ip(addr); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1601 | } |
| 1602 | |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1603 | /* disable interrupts to prevent kstop machine */ |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1604 | local_irq_save(flags); |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1605 | ftrace_update_code(mod); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1606 | local_irq_restore(flags); |
Steven Rostedt | 08f5ac90 | 2008-10-23 09:33:07 -0400 | [diff] [blame] | 1607 | mutex_unlock(&ftrace_start_lock); |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1608 | |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1612 | void ftrace_init_module(struct module *mod, |
| 1613 | unsigned long *start, unsigned long *end) |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 1614 | { |
Steven Rostedt | 00fd61a | 2008-08-15 21:40:04 -0400 | [diff] [blame] | 1615 | if (ftrace_disabled || start == end) |
Steven Rostedt | fed1939 | 2008-08-14 22:47:19 -0400 | [diff] [blame] | 1616 | return; |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1617 | ftrace_convert_nops(mod, start, end); |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 1618 | } |
| 1619 | |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1620 | extern unsigned long __start_mcount_loc[]; |
| 1621 | extern unsigned long __stop_mcount_loc[]; |
| 1622 | |
| 1623 | void __init ftrace_init(void) |
| 1624 | { |
| 1625 | unsigned long count, addr, flags; |
| 1626 | int ret; |
| 1627 | |
| 1628 | /* Keep the ftrace pointer to the stub */ |
| 1629 | addr = (unsigned long)ftrace_stub; |
| 1630 | |
| 1631 | local_irq_save(flags); |
| 1632 | ftrace_dyn_arch_init(&addr); |
| 1633 | local_irq_restore(flags); |
| 1634 | |
| 1635 | /* ftrace_dyn_arch_init places the return code in addr */ |
| 1636 | if (addr) |
| 1637 | goto failed; |
| 1638 | |
| 1639 | count = __stop_mcount_loc - __start_mcount_loc; |
| 1640 | |
| 1641 | ret = ftrace_dyn_table_alloc(count); |
| 1642 | if (ret) |
| 1643 | goto failed; |
| 1644 | |
| 1645 | last_ftrace_enabled = ftrace_enabled = 1; |
| 1646 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 1647 | ret = ftrace_convert_nops(NULL, |
| 1648 | __start_mcount_loc, |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1649 | __stop_mcount_loc); |
| 1650 | |
| 1651 | return; |
| 1652 | failed: |
| 1653 | ftrace_disabled = 1; |
| 1654 | } |
Steven Rostedt | 68bf21a | 2008-08-14 15:45:08 -0400 | [diff] [blame] | 1655 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1656 | #else |
Frederic Weisbecker | 0b6e4d5 | 2008-10-28 20:17:38 +0100 | [diff] [blame] | 1657 | |
| 1658 | static int __init ftrace_nodyn_init(void) |
| 1659 | { |
| 1660 | ftrace_enabled = 1; |
| 1661 | return 0; |
| 1662 | } |
| 1663 | device_initcall(ftrace_nodyn_init); |
| 1664 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1665 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
| 1666 | static inline void ftrace_startup_enable(int command) { } |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 1667 | /* Keep as macros so we do not need to define the commands */ |
| 1668 | # define ftrace_startup(command) do { } while (0) |
| 1669 | # define ftrace_shutdown(command) do { } while (0) |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 1670 | # define ftrace_startup_sysctl() do { } while (0) |
| 1671 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1672 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 1673 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1674 | static ssize_t |
| 1675 | ftrace_pid_read(struct file *file, char __user *ubuf, |
| 1676 | size_t cnt, loff_t *ppos) |
| 1677 | { |
| 1678 | char buf[64]; |
| 1679 | int r; |
| 1680 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1681 | if (ftrace_pid_trace) |
| 1682 | r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace)); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1683 | else |
| 1684 | r = sprintf(buf, "no pid\n"); |
| 1685 | |
| 1686 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 1687 | } |
| 1688 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1689 | static void clear_ftrace_pid_task(struct pid **pid) |
| 1690 | { |
| 1691 | struct task_struct *p; |
| 1692 | |
| 1693 | do_each_pid_task(*pid, PIDTYPE_PID, p) { |
| 1694 | clear_tsk_trace_trace(p); |
| 1695 | } while_each_pid_task(*pid, PIDTYPE_PID, p); |
| 1696 | put_pid(*pid); |
| 1697 | |
| 1698 | *pid = NULL; |
| 1699 | } |
| 1700 | |
| 1701 | static void set_ftrace_pid_task(struct pid *pid) |
| 1702 | { |
| 1703 | struct task_struct *p; |
| 1704 | |
| 1705 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
| 1706 | set_tsk_trace_trace(p); |
| 1707 | } while_each_pid_task(pid, PIDTYPE_PID, p); |
| 1708 | } |
| 1709 | |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1710 | static ssize_t |
| 1711 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 1712 | size_t cnt, loff_t *ppos) |
| 1713 | { |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1714 | struct pid *pid; |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1715 | char buf[64]; |
| 1716 | long val; |
| 1717 | int ret; |
| 1718 | |
| 1719 | if (cnt >= sizeof(buf)) |
| 1720 | return -EINVAL; |
| 1721 | |
| 1722 | if (copy_from_user(&buf, ubuf, cnt)) |
| 1723 | return -EFAULT; |
| 1724 | |
| 1725 | buf[cnt] = 0; |
| 1726 | |
| 1727 | ret = strict_strtol(buf, 10, &val); |
| 1728 | if (ret < 0) |
| 1729 | return ret; |
| 1730 | |
| 1731 | mutex_lock(&ftrace_start_lock); |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1732 | if (val < 0) { |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1733 | /* disable pid tracing */ |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1734 | if (!ftrace_pid_trace) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1735 | goto out; |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1736 | |
| 1737 | clear_ftrace_pid_task(&ftrace_pid_trace); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1738 | |
| 1739 | } else { |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1740 | pid = find_get_pid(val); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1741 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1742 | if (pid == ftrace_pid_trace) { |
| 1743 | put_pid(pid); |
| 1744 | goto out; |
| 1745 | } |
| 1746 | |
| 1747 | if (ftrace_pid_trace) |
| 1748 | clear_ftrace_pid_task(&ftrace_pid_trace); |
| 1749 | |
| 1750 | if (!pid) |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1751 | goto out; |
| 1752 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1753 | ftrace_pid_trace = pid; |
Steven Rostedt | 0ef8cde | 2008-12-03 15:36:58 -0500 | [diff] [blame] | 1754 | |
Steven Rostedt | 978f3a4 | 2008-12-04 00:26:40 -0500 | [diff] [blame^] | 1755 | set_ftrace_pid_task(ftrace_pid_trace); |
Steven Rostedt | df4fc31 | 2008-11-26 00:16:23 -0500 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /* update the function call */ |
| 1759 | ftrace_update_pid_func(); |
| 1760 | ftrace_startup_enable(0); |
| 1761 | |
| 1762 | out: |
| 1763 | mutex_unlock(&ftrace_start_lock); |
| 1764 | |
| 1765 | return cnt; |
| 1766 | } |
| 1767 | |
| 1768 | static struct file_operations ftrace_pid_fops = { |
| 1769 | .read = ftrace_pid_read, |
| 1770 | .write = ftrace_pid_write, |
| 1771 | }; |
| 1772 | |
| 1773 | static __init int ftrace_init_debugfs(void) |
| 1774 | { |
| 1775 | struct dentry *d_tracer; |
| 1776 | struct dentry *entry; |
| 1777 | |
| 1778 | d_tracer = tracing_init_dentry(); |
| 1779 | if (!d_tracer) |
| 1780 | return 0; |
| 1781 | |
| 1782 | ftrace_init_dyn_debugfs(d_tracer); |
| 1783 | |
| 1784 | entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer, |
| 1785 | NULL, &ftrace_pid_fops); |
| 1786 | if (!entry) |
| 1787 | pr_warning("Could not create debugfs " |
| 1788 | "'set_ftrace_pid' entry\n"); |
| 1789 | return 0; |
| 1790 | } |
| 1791 | |
| 1792 | fs_initcall(ftrace_init_debugfs); |
| 1793 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1794 | /** |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 1795 | * ftrace_kill - kill ftrace |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 1796 | * |
| 1797 | * This function should be used by panic code. It stops ftrace |
| 1798 | * but in a not so nice way. If you need to simply kill ftrace |
| 1799 | * from a non-atomic section, use ftrace_kill. |
| 1800 | */ |
Steven Rostedt | 81adbdc | 2008-10-23 09:33:02 -0400 | [diff] [blame] | 1801 | void ftrace_kill(void) |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 1802 | { |
| 1803 | ftrace_disabled = 1; |
| 1804 | ftrace_enabled = 0; |
Steven Rostedt | a2bb6a3 | 2008-07-10 20:58:15 -0400 | [diff] [blame] | 1805 | clear_ftrace_function(); |
| 1806 | } |
| 1807 | |
| 1808 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1809 | * register_ftrace_function - register a function for profiling |
| 1810 | * @ops - ops structure that holds the function for profiling. |
| 1811 | * |
| 1812 | * Register a function to be called by all functions in the |
| 1813 | * kernel. |
| 1814 | * |
| 1815 | * Note: @ops->func and all the functions it calls must be labeled |
| 1816 | * with "notrace", otherwise it will go into a |
| 1817 | * recursive loop. |
| 1818 | */ |
| 1819 | int register_ftrace_function(struct ftrace_ops *ops) |
| 1820 | { |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1821 | int ret; |
| 1822 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1823 | if (unlikely(ftrace_disabled)) |
| 1824 | return -1; |
| 1825 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1826 | mutex_lock(&ftrace_sysctl_lock); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1827 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1828 | ret = __register_ftrace_function(ops); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 1829 | ftrace_startup(0); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1830 | |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1831 | mutex_unlock(&ftrace_sysctl_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1832 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * unregister_ftrace_function - unresgister a function for profiling. |
| 1837 | * @ops - ops structure that holds the function to unregister |
| 1838 | * |
| 1839 | * Unregister a function that was added to be called by ftrace profiling. |
| 1840 | */ |
| 1841 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 1842 | { |
| 1843 | int ret; |
| 1844 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1845 | mutex_lock(&ftrace_sysctl_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1846 | ret = __unregister_ftrace_function(ops); |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 1847 | ftrace_shutdown(0); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1848 | mutex_unlock(&ftrace_sysctl_lock); |
| 1849 | |
| 1850 | return ret; |
| 1851 | } |
| 1852 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1853 | int |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1854 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1855 | struct file *file, void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1856 | loff_t *ppos) |
| 1857 | { |
| 1858 | int ret; |
| 1859 | |
Steven Rostedt | 4eebcc8 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 1860 | if (unlikely(ftrace_disabled)) |
| 1861 | return -ENODEV; |
| 1862 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1863 | mutex_lock(&ftrace_sysctl_lock); |
| 1864 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1865 | ret = proc_dointvec(table, write, file, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1866 | |
| 1867 | if (ret || !write || (last_ftrace_enabled == ftrace_enabled)) |
| 1868 | goto out; |
| 1869 | |
| 1870 | last_ftrace_enabled = ftrace_enabled; |
| 1871 | |
| 1872 | if (ftrace_enabled) { |
| 1873 | |
| 1874 | ftrace_startup_sysctl(); |
| 1875 | |
| 1876 | /* we are starting ftrace again */ |
| 1877 | if (ftrace_list != &ftrace_list_end) { |
| 1878 | if (ftrace_list->next == &ftrace_list_end) |
| 1879 | ftrace_trace_function = ftrace_list->func; |
| 1880 | else |
| 1881 | ftrace_trace_function = ftrace_list_func; |
| 1882 | } |
| 1883 | |
| 1884 | } else { |
| 1885 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 1886 | ftrace_trace_function = ftrace_stub; |
| 1887 | |
| 1888 | ftrace_shutdown_sysctl(); |
| 1889 | } |
| 1890 | |
| 1891 | out: |
| 1892 | mutex_unlock(&ftrace_sysctl_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1893 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1894 | } |
Ingo Molnar | f17845e | 2008-10-24 12:47:10 +0200 | [diff] [blame] | 1895 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 1896 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1897 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 1898 | static atomic_t ftrace_graph_active; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 1899 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 1900 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 1901 | { |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 1905 | /* The callbacks that hook a function */ |
| 1906 | trace_func_graph_ret_t ftrace_graph_return = |
| 1907 | (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 1908 | trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 1909 | |
| 1910 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 1911 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 1912 | { |
| 1913 | int i; |
| 1914 | int ret = 0; |
| 1915 | unsigned long flags; |
| 1916 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 1917 | struct task_struct *g, *t; |
| 1918 | |
| 1919 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 1920 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH |
| 1921 | * sizeof(struct ftrace_ret_stack), |
| 1922 | GFP_KERNEL); |
| 1923 | if (!ret_stack_list[i]) { |
| 1924 | start = 0; |
| 1925 | end = i; |
| 1926 | ret = -ENOMEM; |
| 1927 | goto free; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | read_lock_irqsave(&tasklist_lock, flags); |
| 1932 | do_each_thread(g, t) { |
| 1933 | if (start == end) { |
| 1934 | ret = -EAGAIN; |
| 1935 | goto unlock; |
| 1936 | } |
| 1937 | |
| 1938 | if (t->ret_stack == NULL) { |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 1939 | t->curr_ret_stack = -1; |
Frederic Weisbecker | 48d68b2 | 2008-12-02 00:20:39 +0100 | [diff] [blame] | 1940 | /* Make sure IRQs see the -1 first: */ |
| 1941 | barrier(); |
| 1942 | t->ret_stack = ret_stack_list[start++]; |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 1943 | atomic_set(&t->trace_overrun, 0); |
| 1944 | } |
| 1945 | } while_each_thread(g, t); |
| 1946 | |
| 1947 | unlock: |
| 1948 | read_unlock_irqrestore(&tasklist_lock, flags); |
| 1949 | free: |
| 1950 | for (i = start; i < end; i++) |
| 1951 | kfree(ret_stack_list[i]); |
| 1952 | return ret; |
| 1953 | } |
| 1954 | |
| 1955 | /* Allocate a return stack for each task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 1956 | static int start_graph_tracing(void) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 1957 | { |
| 1958 | struct ftrace_ret_stack **ret_stack_list; |
| 1959 | int ret; |
| 1960 | |
| 1961 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * |
| 1962 | sizeof(struct ftrace_ret_stack *), |
| 1963 | GFP_KERNEL); |
| 1964 | |
| 1965 | if (!ret_stack_list) |
| 1966 | return -ENOMEM; |
| 1967 | |
| 1968 | do { |
| 1969 | ret = alloc_retstack_tasklist(ret_stack_list); |
| 1970 | } while (ret == -EAGAIN); |
| 1971 | |
| 1972 | kfree(ret_stack_list); |
| 1973 | return ret; |
| 1974 | } |
| 1975 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 1976 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 1977 | trace_func_graph_ent_t entryfunc) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 1978 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1979 | int ret = 0; |
| 1980 | |
| 1981 | mutex_lock(&ftrace_sysctl_lock); |
| 1982 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 1983 | atomic_inc(&ftrace_graph_active); |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 1984 | ret = start_graph_tracing(); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 1985 | if (ret) { |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 1986 | atomic_dec(&ftrace_graph_active); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 1987 | goto out; |
| 1988 | } |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 1989 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 1990 | ftrace_graph_return = retfunc; |
| 1991 | ftrace_graph_entry = entryfunc; |
Steven Rostedt | e53a631 | 2008-11-26 00:16:25 -0500 | [diff] [blame] | 1992 | |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 1993 | ftrace_startup(FTRACE_START_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 1994 | |
| 1995 | out: |
| 1996 | mutex_unlock(&ftrace_sysctl_lock); |
| 1997 | return ret; |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 2000 | void unregister_ftrace_graph(void) |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 2001 | { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 2002 | mutex_lock(&ftrace_sysctl_lock); |
| 2003 | |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 2004 | atomic_dec(&ftrace_graph_active); |
| 2005 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 2006 | ftrace_graph_entry = ftrace_graph_entry_stub; |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 2007 | ftrace_shutdown(FTRACE_STOP_FUNC_RET); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 2008 | |
| 2009 | mutex_unlock(&ftrace_sysctl_lock); |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 2010 | } |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 2011 | |
| 2012 | /* Allocate a return stack for newly created task */ |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 2013 | void ftrace_graph_init_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 2014 | { |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 2015 | if (atomic_read(&ftrace_graph_active)) { |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 2016 | t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH |
| 2017 | * sizeof(struct ftrace_ret_stack), |
| 2018 | GFP_KERNEL); |
| 2019 | if (!t->ret_stack) |
| 2020 | return; |
| 2021 | t->curr_ret_stack = -1; |
| 2022 | atomic_set(&t->trace_overrun, 0); |
| 2023 | } else |
| 2024 | t->ret_stack = NULL; |
| 2025 | } |
| 2026 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 2027 | void ftrace_graph_exit_task(struct task_struct *t) |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 2028 | { |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 2029 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 2030 | |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 2031 | t->ret_stack = NULL; |
Frederic Weisbecker | eae849c | 2008-11-23 17:33:12 +0100 | [diff] [blame] | 2032 | /* NULL must become visible to IRQs before we free it: */ |
| 2033 | barrier(); |
| 2034 | |
| 2035 | kfree(ret_stack); |
Frederic Weisbecker | f201ae2 | 2008-11-23 06:22:56 +0100 | [diff] [blame] | 2036 | } |
Steven Rostedt | 14a866c | 2008-12-02 23:50:02 -0500 | [diff] [blame] | 2037 | |
| 2038 | void ftrace_graph_stop(void) |
| 2039 | { |
| 2040 | ftrace_stop(); |
| 2041 | } |
Frederic Weisbecker | 15e6cb3 | 2008-11-11 07:14:25 +0100 | [diff] [blame] | 2042 | #endif |
| 2043 | |